forked from asticode/go-astiav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_context.go
270 lines (228 loc) · 6.35 KB
/
format_context.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
package astiav
//#cgo pkg-config: libavcodec libavformat
//#include <libavcodec/avcodec.h>
//#include <libavformat/avformat.h>
import "C"
import (
"math"
"unsafe"
)
// https://github.com/FFmpeg/FFmpeg/blob/n5.0/libavformat/avformat.h#L1202
type FormatContext struct {
c *C.struct_AVFormatContext
}
func newFormatContextFromC(c *C.struct_AVFormatContext) *FormatContext {
if c == nil {
return nil
}
fc := &FormatContext{c: c}
classers.set(fc)
return fc
}
var _ Classer = (*FormatContext)(nil)
func AllocFormatContext() *FormatContext {
return newFormatContextFromC(C.avformat_alloc_context())
}
func AllocOutputFormatContext(of *OutputFormat, formatName, filename string) (*FormatContext, error) {
fonc := (*C.char)(nil)
if len(formatName) > 0 {
fonc = C.CString(formatName)
defer C.free(unsafe.Pointer(fonc))
}
finc := (*C.char)(nil)
if len(filename) > 0 {
finc = C.CString(filename)
defer C.free(unsafe.Pointer(finc))
}
var ofc *C.struct_AVOutputFormat
if of != nil {
ofc = of.c
}
var fcc *C.struct_AVFormatContext
if err := newError(C.avformat_alloc_output_context2(&fcc, ofc, fonc, finc)); err != nil {
return nil, err
}
return newFormatContextFromC(fcc), nil
}
func (fc *FormatContext) Free() {
classers.del(fc)
C.avformat_free_context(fc.c)
}
func (fc *FormatContext) BitRate() int64 {
return int64(fc.c.bit_rate)
}
func (fc *FormatContext) Class() *Class {
return newClassFromC(unsafe.Pointer(fc.c))
}
func (fc *FormatContext) CtxFlags() FormatContextCtxFlags {
return FormatContextCtxFlags(fc.c.ctx_flags)
}
func (fc *FormatContext) Duration() int64 {
return int64(fc.c.duration)
}
func (fc *FormatContext) EventFlags() FormatEventFlags {
return FormatEventFlags(fc.c.event_flags)
}
func (fc *FormatContext) Flags() FormatContextFlags {
return FormatContextFlags(fc.c.flags)
}
func (fc *FormatContext) SetFlags(f FormatContextFlags) {
fc.c.flags = C.int(f)
}
func (fc *FormatContext) SetInterruptCallback() IOInterrupter {
i := newDefaultIOInterrupter()
fc.c.interrupt_callback = i.c
return i
}
func (fc *FormatContext) InputFormat() *InputFormat {
return newInputFormatFromC(fc.c.iformat)
}
func (fc *FormatContext) IOFlags() IOContextFlags {
return IOContextFlags(fc.c.avio_flags)
}
func (fc *FormatContext) MaxAnalyzeDuration() int64 {
return int64(fc.c.max_analyze_duration)
}
func (fc *FormatContext) Metadata() *Dictionary {
return newDictionaryFromC(fc.c.metadata)
}
func (fc *FormatContext) NbStreams() int {
return int(fc.c.nb_streams)
}
func (fc *FormatContext) OutputFormat() *OutputFormat {
return newOutputFormatFromC(fc.c.oformat)
}
func (fc *FormatContext) Pb() *IOContext {
// If the io context has been created using the format context's OpenInput() method, we need to
// make sure to return the same go struct as the one stored in classers
if c, ok := classers.get(unsafe.Pointer(fc.c.pb)); ok {
if v, ok := c.(*IOContext); ok {
return v
}
}
return newIOContextFromC(fc.c.pb)
}
func (fc *FormatContext) SetPb(i *IOContext) {
fc.c.pb = i.c
}
func (fc *FormatContext) StartTime() int64 {
return int64(fc.c.start_time)
}
func (fc *FormatContext) Streams() (ss []*Stream) {
scs := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.struct_AVStream)(nil))](*C.struct_AVStream))(unsafe.Pointer(fc.c.streams))
for i := 0; i < fc.NbStreams(); i++ {
ss = append(ss, newStreamFromC(scs[i]))
}
return
}
func (fc *FormatContext) StrictStdCompliance() StrictStdCompliance {
return StrictStdCompliance(fc.c.strict_std_compliance)
}
func (fc *FormatContext) SetStrictStdCompliance(strictStdCompliance StrictStdCompliance) {
fc.c.strict_std_compliance = C.int(strictStdCompliance)
}
func (fc *FormatContext) OpenInput(url string, fmt *InputFormat, d *Dictionary) error {
urlc := C.CString(url)
defer C.free(unsafe.Pointer(urlc))
var dc **C.struct_AVDictionary
if d != nil {
dc = &d.c
}
var fmtc *C.struct_AVInputFormat
if fmt != nil {
fmtc = fmt.c
}
if err := newError(C.avformat_open_input(&fc.c, urlc, fmtc, dc)); err != nil {
return err
}
if pb := fc.Pb(); pb != nil {
classers.set(pb)
}
return nil
}
func (fc *FormatContext) CloseInput() {
if pb := fc.Pb(); pb != nil {
classers.del(pb)
}
classers.del(fc)
if fc.c != nil {
C.avformat_close_input(&fc.c)
}
}
func (fc *FormatContext) NewStream(c *Codec) *Stream {
var cc *C.struct_AVCodec
if c != nil {
cc = c.c
}
return newStreamFromC(C.avformat_new_stream(fc.c, cc))
}
func (fc *FormatContext) FindStreamInfo(d *Dictionary) error {
var dc **C.struct_AVDictionary
if d != nil {
dc = &d.c
}
return newError(C.avformat_find_stream_info(fc.c, dc))
}
func (fc *FormatContext) ReadFrame(p *Packet) error {
var pc *C.struct_AVPacket
if p != nil {
pc = p.c
}
return newError(C.av_read_frame(fc.c, pc))
}
func (fc *FormatContext) SeekFrame(streamIndex int, timestamp int64, f SeekFlags) error {
return newError(C.av_seek_frame(fc.c, C.int(streamIndex), C.int64_t(timestamp), C.int(f)))
}
func (fc *FormatContext) Flush() error {
return newError(C.avformat_flush(fc.c))
}
func (fc *FormatContext) WriteHeader(d *Dictionary) error {
var dc **C.struct_AVDictionary
if d != nil {
dc = &d.c
}
return newError(C.avformat_write_header(fc.c, dc))
}
func (fc *FormatContext) WriteFrame(p *Packet) error {
var pc *C.struct_AVPacket
if p != nil {
pc = p.c
}
return newError(C.av_write_frame(fc.c, pc))
}
func (fc *FormatContext) WriteInterleavedFrame(p *Packet) error {
var pc *C.struct_AVPacket
if p != nil {
pc = p.c
}
return newError(C.av_interleaved_write_frame(fc.c, pc))
}
func (fc *FormatContext) WriteTrailer() error {
return newError(C.av_write_trailer(fc.c))
}
func (fc *FormatContext) GuessSampleAspectRatio(s *Stream, f *Frame) Rational {
var cf *C.struct_AVFrame
if f != nil {
cf = f.c
}
return newRationalFromC(C.av_guess_sample_aspect_ratio(fc.c, s.c, cf))
}
func (fc *FormatContext) GuessFrameRate(s *Stream, f *Frame) Rational {
var cf *C.struct_AVFrame
if f != nil {
cf = f.c
}
return newRationalFromC(C.av_guess_frame_rate(fc.c, s.c, cf))
}
func (fc *FormatContext) SDPCreate() (string, error) {
return sdpCreate([]*FormatContext{fc})
}
func sdpCreate(fcs []*FormatContext) (string, error) {
return stringFromC(1024, func(buf *C.char, size C.size_t) error {
fccs := []*C.struct_AVFormatContext{}
for _, fc := range fcs {
fccs = append(fccs, fc.c)
}
return newError(C.av_sdp_create(&fccs[0], C.int(len(fcs)), buf, C.int(size)))
})
}