-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame_go112.go
278 lines (219 loc) · 5.74 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
// +build go1.12
package gmf
/*
#cgo pkg-config: libavcodec libavutil
#include "libavcodec/avcodec.h"
#include "libavutil/frame.h"
#include "libavutil/imgutils.h"
#include "libavutil/timestamp.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]);
}
*/
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.pkt_pts)
}
func (f *Frame) PktPos() int64 {
return int64(f.avFrame.pkt_pos)
}
func (f *Frame) SetPktPts(val int64) {
f.avFrame.pkt_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()))
}