-
Notifications
You must be signed in to change notification settings - Fork 169
/
frame_go112.go
384 lines (306 loc) · 9.06 KB
/
frame_go112.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//go:build go1.12
// +build go1.12
package gmf
/*
#cgo pkg-config: libavcodec libavutil
#include <stdlib.h>
#include "libavcodec/avcodec.h"
#include "libavutil/frame.h"
#include "libavutil/imgutils.h"
#include "libavutil/timestamp.h"
#include "libavutil/timecode.h"
#include "libavutil/common.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];
}
void gmf_free_data(AVFrame *frame) {
av_freep(&frame->data[0]);
}
int gmf_get_timecode(AVFrameSideData *sd, AVRational avgFrameRate, char *out) {
uint32_t *tc = (uint32_t*)sd->data;
char tcbuf[AV_TIMECODE_STR_SIZE];
av_timecode_make_smpte_tc_string2(tcbuf, avgFrameRate, tc[1], 0, 0);
int n;
n = sprintf(out, "%s", tcbuf);
return n;
}
*/
import "C"
import (
"errors"
"fmt"
"syscall"
"unsafe"
)
const (
AV_PICTURE_TYPE_NONE = iota // Undefined
AV_PICTURE_TYPE_I // Intra
AV_PICTURE_TYPE_P // Predicted
AV_PICTURE_TYPE_B // Bi-dir predicted
AV_PICTURE_TYPE_S // S(GMC)-VOP MPEG-4
AV_PICTURE_TYPE_SI // Switching Intra
AV_PICTURE_TYPE_SP // Switching Predicted
AV_PICTURE_TYPE_BI // BI type
)
type Frame struct {
avFrame *C.struct_AVFrame
samples *C.uint8_t
mediaType int32
err error
freeData bool
}
func NewFrame() *Frame {
return &Frame{avFrame: C.av_frame_alloc()}
}
func (f *Frame) Encode(enc *CodecCtx) (*Packet, error) {
pkt := NewPacket()
if pkt == nil {
return nil, fmt.Errorf("unable to initialize new packet")
}
if ret := int(C.avcodec_send_frame(enc.avCodecCtx, f.avFrame)); ret < 0 {
return nil, fmt.Errorf("error sending frame - %v", AvErrno(ret))
}
for {
ret := int(C.avcodec_receive_packet(enc.avCodecCtx, &pkt.avPacket))
if AvErrno(ret) == syscall.EAGAIN {
return nil, nil
}
if ret < 0 {
return nil, fmt.Errorf("%v", AvErrno(ret))
}
if ret >= 0 {
break
}
}
return pkt, nil
}
func (f *Frame) Pts() int64 {
return int64(f.avFrame.pts)
}
func (f *Frame) Unref() {
C.av_frame_unref(f.avFrame)
}
func (f *Frame) SetPts(val int64) {
f.avFrame.pts = (C.int64_t)(val)
}
// AVPixelFormat for video frames, AVSampleFormat for audio
func (f *Frame) Format() int {
return int(f.avFrame.format)
}
func (f *Frame) Width() int {
return int(f.avFrame.width)
}
func (f *Frame) Height() int {
return int(f.avFrame.height)
}
func (f *Frame) PktPts() int64 {
return int64(f.avFrame.pts)
}
func (f *Frame) PktPos() int64 {
return int64(f.avFrame.pkt_pos)
}
func (f *Frame) SetPktPts(val int64) {
f.avFrame.pts = (C.int64_t)(val)
}
func (f *Frame) PktDts() int {
return int(f.avFrame.pkt_dts)
}
func (f *Frame) SetPktDts(val int) {
f.avFrame.pkt_dts = (C.int64_t)(val)
}
func (f *Frame) KeyFrame() int {
return int(f.avFrame.key_frame)
}
func (f *Frame) NbSamples() int {
return int(f.avFrame.nb_samples)
}
func (f *Frame) Channels() int {
return int(f.avFrame.channels)
}
func (f *Frame) SetFormat(val int32) *Frame {
f.avFrame.format = C.int(val)
return f
}
func (f *Frame) SetWidth(val int) *Frame {
f.avFrame.width = C.int(val)
return f
}
func (f *Frame) SetHeight(val int) *Frame {
f.avFrame.height = C.int(val)
return f
}
func (f *Frame) ImgAlloc() error {
if ret := int(C.av_image_alloc(
(**C.uint8_t)(unsafe.Pointer(&f.avFrame.data)),
(*C.int)(unsafe.Pointer(&f.avFrame.linesize)),
C.int(f.Width()), C.int(f.Height()), int32(f.Format()), 32)); ret < 0 {
return errors.New(fmt.Sprintf("Unable to allocate raw image buffer: %v", AvError(ret)))
}
f.freeData = true
return nil
}
func NewAudioFrame(sampleFormat int32, channels, nb_samples int) (*Frame, error) {
f := NewFrame()
f.mediaType = AVMEDIA_TYPE_AUDIO
f.SetNbSamples(nb_samples)
f.SetFormat(sampleFormat)
f.SetChannels(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")
}
f.samples = (*C.uint8_t)(C.av_malloc(C.size_t(size)))
if f.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(f.avFrame, C.int(channels), sampleFormat,
f.samples, C.int(size), 0))
if ret < 0 {
return nil, errors.New("could not setup audio frame")
}
f.freeData = true
return f, nil
}
func (f *Frame) SetData(idx int, lineSize int, data int) *Frame {
C.gmf_set_frame_data(f.avFrame, C.int(idx), C.int(lineSize), (C.uint8_t)(data))
return f
}
func (f *Frame) LineSize(idx int) int {
return int(C.gmf_get_frame_line_size(f.avFrame, C.int(idx)))
}
func (f *Frame) Dump() {
fmt.Printf("%v\n", f.avFrame)
}
func (f *Frame) CloneNewFrame() *Frame {
return &Frame{avFrame: C.av_frame_clone(f.avFrame)}
}
func (f *Frame) Free() {
if f.freeData && f.avFrame != nil {
C.gmf_free_data(f.avFrame)
}
if f.avFrame != nil {
C.av_frame_free(&f.avFrame)
}
}
func (f *Frame) SetNbSamples(val int) *Frame {
f.avFrame.nb_samples = C.int(val)
return f
}
func (f *Frame) SetChannelLayout(val int) *Frame {
f.avFrame.channel_layout = (C.uint64_t)(val)
return f
}
func (f *Frame) GetChannelLayout() int {
return int(f.avFrame.channel_layout)
}
func (f *Frame) SetChannels(val int) *Frame {
f.avFrame.channels = C.int(val)
return f
}
func (f *Frame) SetQuality(val int) *Frame {
f.avFrame.quality = C.int(val)
return f
}
func (f *Frame) SetPictType(val uint32) {
f.avFrame.pict_type = val
}
func (f *Frame) IsNil() bool {
if f.avFrame == nil {
return true
}
return false
}
func (f *Frame) GetRawFrame() *C.struct_AVFrame {
return f.avFrame
}
func (f *Frame) GetRawAudioData(plane int) []byte {
return C.GoBytes(unsafe.Pointer(f.avFrame.data[plane]), C.int(f.LineSize(plane)))
}
func (f *Frame) Time(timebase AVRational) int {
return int(float64(timebase.AVR().Num) / float64(timebase.AVR().Den) * float64(f.Pts()))
}
var frameSideDataTypes []uint32 = []uint32{
C.AV_FRAME_DATA_PANSCAN,
C.AV_FRAME_DATA_A53_CC,
C.AV_FRAME_DATA_AFD,
C.AV_FRAME_DATA_AUDIO_SERVICE_TYPE,
C.AV_FRAME_DATA_CONTENT_LIGHT_LEVEL,
C.AV_FRAME_DATA_DISPLAYMATRIX,
C.AV_FRAME_DATA_DOWNMIX_INFO,
C.AV_FRAME_DATA_DYNAMIC_HDR_PLUS,
C.AV_FRAME_DATA_FILM_GRAIN_PARAMS,
C.AV_FRAME_DATA_GOP_TIMECODE,
C.AV_FRAME_DATA_ICC_PROFILE,
C.AV_FRAME_DATA_MASTERING_DISPLAY_METADATA,
C.AV_FRAME_DATA_MATRIXENCODING,
C.AV_FRAME_DATA_MOTION_VECTORS,
C.AV_FRAME_DATA_REGIONS_OF_INTEREST,
C.AV_FRAME_DATA_REPLAYGAIN,
C.AV_FRAME_DATA_S12M_TIMECODE,
C.AV_FRAME_DATA_SEI_UNREGISTERED,
C.AV_FRAME_DATA_SKIP_SAMPLES,
C.AV_FRAME_DATA_SPHERICAL,
C.AV_FRAME_DATA_STEREO3D,
C.AV_FRAME_DATA_VIDEO_ENC_PARAMS,
}
func (f *Frame) GetSideDataTypes() (map[uint32]string, error) {
result := make(map[uint32]string)
// get a pointer to the side data of the frame
for _, sideDataType := range frameSideDataTypes {
sideDataPtr := C.av_frame_get_side_data(f.avFrame, sideDataType)
if sideDataPtr != nil {
// cast the pointer to the side data to a AVFrameSideData struct
sideData := (*C.struct_AVFrameSideData)(unsafe.Pointer(sideDataPtr))
sideDataType := C.GoString(C.av_frame_side_data_name(sideData._type))
result[uint32(sideData._type)] = sideDataType
}
}
return result, nil
}
func (f *Frame) GetUserData() ([]byte, error) {
// get a pointer to the side data of the frame
sideDataPtr := C.av_frame_get_side_data(f.avFrame, C.AV_FRAME_DATA_SEI_UNREGISTERED)
if sideDataPtr == nil {
return nil, errors.New("no user data found")
}
// cast the pointer to the side data to a AVFrameSideData struct
sideData := (*C.struct_AVFrameSideData)(unsafe.Pointer(sideDataPtr))
// gets the bytes from the pointer
data := C.GoBytes(unsafe.Pointer(sideData.data), C.int(sideData.size))
return data, nil
}
func (f *Frame) GetTimeCode(avgFrameRate AVRational) (string, error) {
// get a pointer to the side data of the frame
sideDataPtr := C.av_frame_get_side_data(f.avFrame, C.AV_FRAME_DATA_S12M_TIMECODE)
if sideDataPtr == nil {
return "", errors.New("no timecode data found")
}
// cast the pointer to the side data to a AVFrameSideData struct
sideData := (*C.struct_AVFrameSideData)(unsafe.Pointer(sideDataPtr))
// check if the side data is valid
if sideData._type != C.AV_FRAME_DATA_S12M_TIMECODE || sideData.size != 16 {
return "", errors.New("invalid timecode side data")
}
// prepare a byte slice to hold the timecode
ptr := C.malloc(C.sizeof_char * 1024)
defer C.free(unsafe.Pointer(ptr))
// cast the go avgFrameRate to a C AVRational
afr := (C.struct_AVRational)(avgFrameRate)
// get the timecode by calling the C function and passing the sideData, the AVRational and the pointer to the byte slice to hold the timecode
// returns the length of the timecode in bytes
tcSize := C.gmf_get_timecode(sideData, afr, (*C.char)(ptr))
// gets the bytes from the pointer
tc := C.GoBytes(ptr, tcSize)
tcString := string(tc)
return tcString, nil
}