forked from 3d0c/gmf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
frame.go
236 lines (184 loc) · 5.55 KB
/
frame.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package gmf
/*
#cgo pkg-config: libavcodec libavutil
#include "libavcodec/avcodec.h"
#include "libavutil/frame.h"
#include "libavutil/imgutils.h"
void gmf_set_frame_data(AVFrame *frame, int idx, int l_size, uint8_t data) {
if(!frame) {
fprintf(stderr, "frame is NULL\n");
}
frame->data[idx][l_size] = data;
}
int gmf_get_frame_line_size(AVFrame *frame, int idx) {
return frame->linesize[idx];
}
*/
import "C"
import (
"errors"
"fmt"
"unsafe"
)
type Frame struct {
avFrame *C.struct_AVFrame
mediaType int32
CgoMemoryManage
}
func NewFrame() *Frame {
return &Frame{avFrame: C.av_frame_alloc()}
}
func (this *Frame) EncodeNewPacket(cc *CodecCtx) (*Packet, bool, error) {
return encode(cc, this.avFrame, this.mediaType)
}
func (this *Frame) FlushNewPacket(cc *CodecCtx) (*Packet, bool, error) {
return encode(cc, nil, this.mediaType)
}
func encode(cc *CodecCtx, avFrame *C.struct_AVFrame, mediaType int32) (*Packet, bool, error) {
var gotOutput int
var ret int
p := NewPacket()
switch mediaType {
case AVMEDIA_TYPE_AUDIO:
ret = int(C.avcodec_encode_audio2(cc.avCodecCtx, &p.avPacket, avFrame, (*C.int)(unsafe.Pointer(&gotOutput))))
if ret < 0 {
return nil, false, errors.New(fmt.Sprintf("Unable to encode video packet, averror: %s", AvError(int(ret))))
}
case AVMEDIA_TYPE_VIDEO:
cc.avCodecCtx.field_order = C.AV_FIELD_PROGRESSIVE
ret = int(C.avcodec_encode_video2(cc.avCodecCtx, &p.avPacket, avFrame, (*C.int)(unsafe.Pointer(&gotOutput))))
if ret < 0 {
return nil, false, errors.New(fmt.Sprintf("Unable to encode video packet, averror: %s", AvError(int(ret))))
}
default:
return nil, false, errors.New(fmt.Sprintf("Unknown codec type: %v", mediaType))
}
return p, (gotOutput > 0), nil
}
func (this *Frame) Pts() int64 {
return int64(this.avFrame.pts)
}
func (this *Frame) Unref() {
C.av_frame_unref(this.avFrame)
}
func (this *Frame) SetPts(val int64) {
this.avFrame.pts = (_Ctype_int64_t)(val)
}
func (this *Frame) SetBestPts() {
this.avFrame.pts = C.av_frame_get_best_effort_timestamp(this.avFrame)
}
// AVPixelFormat for video frames, AVSampleFormat for audio
func (this *Frame) Format() int {
return int(this.avFrame.format)
}
func (this *Frame) Width() int {
return int(this.avFrame.width)
}
func (this *Frame) Height() int {
return int(this.avFrame.height)
}
func (this *Frame) PktPts() int64 {
return int64(this.avFrame.pkt_pts)
}
func (this *Frame) SetPktPts(val int64) {
this.avFrame.pkt_pts = (_Ctype_int64_t)(val)
}
func (this *Frame) PktDts() int {
return int(this.avFrame.pkt_dts)
}
func (this *Frame) SetPktDts(val int) {
this.avFrame.pkt_dts = (_Ctype_int64_t)(val)
}
func (this *Frame) TimeStamp() int {
return int(C.av_frame_get_best_effort_timestamp(this.avFrame))
}
func (this *Frame) PktPos() int {
return int(C.av_frame_get_pkt_pos(this.avFrame))
}
func (this *Frame) PktDuration() int {
return int(C.av_frame_get_pkt_duration(this.avFrame))
}
func (this *Frame) KeyFrame() int {
return int(this.avFrame.key_frame)
}
func (this *Frame) NbSamples() int {
return int(this.avFrame.nb_samples)
}
func (this *Frame) Channels() int {
return int(this.avFrame.channels)
}
func (this *Frame) SetFormat(val int32) *Frame {
this.avFrame.format = C.int(val)
return this
}
func (this *Frame) SetWidth(val int) *Frame {
this.avFrame.width = C.int(val)
return this
}
func (this *Frame) SetHeight(val int) *Frame {
this.avFrame.height = C.int(val)
return this
}
func (this *Frame) ImgAlloc() error {
if ret := int(C.av_image_alloc(
(**C.uint8_t)(unsafe.Pointer(&this.avFrame.data)),
(*_Ctype_int)(unsafe.Pointer(&this.avFrame.linesize)),
C.int(this.Width()), C.int(this.Height()), int32(this.Format()), 32)); ret < 0 {
return errors.New(fmt.Sprintf("Unable to allocate raw image buffer: %v", AvError(ret)))
}
return nil
}
func NewAudioFrame(sampleFormat int32, channels, nb_samples int) (*Frame, error) {
this := NewFrame()
this.mediaType = AVMEDIA_TYPE_AUDIO
this.SetNbSamples(nb_samples)
this.SetFormat(sampleFormat)
this.SetChannelLayout(channels)
//the codec gives us the frame size, in samples,
//we calculate the size of the samples buffer in bytes
size := C.av_samples_get_buffer_size(nil, C.int(channels), C.int(nb_samples),
sampleFormat, 0)
if size < 0 {
return nil, errors.New("Could not get sample buffer size")
}
samples := (*_Ctype_uint8_t)(C.av_malloc(C.size_t(size)))
if samples == nil {
return nil, errors.New(fmt.Sprintf("Could not allocate %d bytes for samples buffer", size))
}
//setup the data pointers in the AVFrame
ret := int(C.avcodec_fill_audio_frame(this.avFrame, C.int(channels), sampleFormat,
samples, C.int(size), 0))
if ret < 0 {
return nil, errors.New("Could not setup audio frame")
}
return this, nil
}
func (this *Frame) SetData(idx int, lineSize int, data int) *Frame {
C.gmf_set_frame_data(this.avFrame, C.int(idx), C.int(lineSize), (_Ctype_uint8_t)(data))
return this
}
func (this *Frame) LineSize(idx int) int {
return int(C.gmf_get_frame_line_size(this.avFrame, C.int(idx)))
}
func (this *Frame) CloneNewFrame() *Frame {
return &Frame{avFrame: C.av_frame_clone(this.avFrame)}
}
func (this *Frame) Free() {
C.av_frame_free(&this.avFrame)
}
func (this *Frame) SetNbSamples(val int) *Frame {
this.avFrame.nb_samples = C.int(val)
return this
}
func (this *Frame) SetChannelLayout(val int) *Frame {
this.avFrame.channel_layout = (_Ctype_uint64_t)(val)
return this
}
func (this *Frame) SetChannels(val int) *Frame {
this.avFrame.channels = C.int(val)
return this
}
func (this *Frame) SetQuality(val int) *Frame {
this.avFrame.quality = C.int(val)
return this
}