/
context.go
240 lines (191 loc) · 9.75 KB
/
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
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// Giorgis (habtom@giorgis.io)
package avformat
//#cgo pkg-config: libavformat
//#include <libavformat/avformat.h>
import "C"
import (
"time"
"unsafe"
"github.com/giorgisio/goav/avcodec"
)
const (
AvseekFlagBackward = 1 ///< seek backward
AvseekFlagByte = 2 ///< seeking based on position in bytes
AvseekFlagAny = 4 ///< seek to any frame, even non-keyframes
AvseekFlagFrame = 8 ///< seeking based on frame number
)
func (s *Context) AvFormatGetProbeScore() int {
return int(C.av_format_get_probe_score((*C.struct_AVFormatContext)(s)))
}
func (s *Context) AvFormatGetVideoCodec() *AvCodec {
return (*AvCodec)(C.av_format_get_video_codec((*C.struct_AVFormatContext)(s)))
}
func (s *Context) AvFormatSetVideoCodec(c *AvCodec) {
C.av_format_set_video_codec((*C.struct_AVFormatContext)(s), (*C.struct_AVCodec)(c))
}
func (s *Context) AvFormatGetAudioCodec() *AvCodec {
return (*AvCodec)(C.av_format_get_audio_codec((*C.struct_AVFormatContext)(s)))
}
func (s *Context) AvFormatSetAudioCodec(c *AvCodec) {
C.av_format_set_audio_codec((*C.struct_AVFormatContext)(s), (*C.struct_AVCodec)(c))
}
func (s *Context) AvFormatGetSubtitleCodec() *AvCodec {
return (*AvCodec)(C.av_format_get_subtitle_codec((*C.struct_AVFormatContext)(s)))
}
func (s *Context) AvFormatSetSubtitleCodec(c *AvCodec) {
C.av_format_set_subtitle_codec((*C.struct_AVFormatContext)(s), (*C.struct_AVCodec)(c))
}
func (s *Context) AvFormatGetMetadataHeaderPadding() int {
return int(C.av_format_get_metadata_header_padding((*C.struct_AVFormatContext)(s)))
}
func (s *Context) AvFormatSetMetadataHeaderPadding(c int) {
C.av_format_set_metadata_header_padding((*C.struct_AVFormatContext)(s), C.int(c))
}
func (s *Context) AvFormatGetOpaque() {
C.av_format_get_opaque((*C.struct_AVFormatContext)(s))
}
func (s *Context) AvFormatSetOpaque(o int) {
C.av_format_set_opaque((*C.struct_AVFormatContext)(s), unsafe.Pointer(&o))
}
//This function will cause global side data to be injected in the next packet of each stream as well as after any subsequent seek.
func (s *Context) AvFormatInjectGlobalSideData() {
C.av_format_inject_global_side_data((*C.struct_AVFormatContext)(s))
}
//Returns the method used to set ctx->duration.
func (s *Context) AvFmtCtxGetDurationEstimationMethod() AvDurationEstimationMethod {
return (AvDurationEstimationMethod)(C.av_fmt_ctx_get_duration_estimation_method((*C.struct_AVFormatContext)(s)))
}
//Free an Context and all its streams.
func (s *Context) AvformatFreeContext() {
C.avformat_free_context((*C.struct_AVFormatContext)(s))
}
//Add a new stream to a media file.
func (s *Context) AvformatNewStream(c *AvCodec) *Stream {
return (*Stream)(C.avformat_new_stream((*C.struct_AVFormatContext)(s), (*C.struct_AVCodec)(c)))
}
func (s *Context) AvNewProgram(id int) *AvProgram {
return (*AvProgram)(C.av_new_program((*C.struct_AVFormatContext)(s), C.int(id)))
}
//Read packets of a media file to get stream information.
func (s *Context) AvformatFindStreamInfo(d **Dictionary) int {
return int(C.avformat_find_stream_info((*C.struct_AVFormatContext)(s), (**C.struct_AVDictionary)(unsafe.Pointer(d))))
}
//Find the programs which belong to a given stream.
func (s *Context) AvFindProgramFromStream(l *AvProgram, su int) *AvProgram {
return (*AvProgram)(C.av_find_program_from_stream((*C.struct_AVFormatContext)(s), (*C.struct_AVProgram)(l), C.int(su)))
}
//Find the "best" stream in the file.
func AvFindBestStream(ic *Context, t MediaType, ws, rs int, c **AvCodec, f int) int {
return int(C.av_find_best_stream((*C.struct_AVFormatContext)(ic), (C.enum_AVMediaType)(t), C.int(ws), C.int(rs), (**C.struct_AVCodec)(unsafe.Pointer(c)), C.int(f)))
}
//Return the next frame of a stream.
func (s *Context) AvReadFrame(pkt *avcodec.Packet) int {
return int(C.av_read_frame((*C.struct_AVFormatContext)(unsafe.Pointer(s)), toCPacket(pkt)))
}
//Seek to the keyframe at timestamp.
func (s *Context) AvSeekFrame(st int, t int64, f int) int {
return int(C.av_seek_frame((*C.struct_AVFormatContext)(s), C.int(st), C.int64_t(t), C.int(f)))
}
// AvSeekFrameTime seeks to a specified time location.
// |timebase| is codec specific and can be obtained by calling AvCodecGetPktTimebase2
func (s *Context) AvSeekFrameTime(st int, at time.Duration, timebase avcodec.Rational) int {
t2 := C.double(C.double(at.Seconds())*C.double(timebase.Den())) / (C.double(timebase.Num()))
// log.Printf("Seeking to time :%v TimebaseTime:%v ActualTimebase:%v", at, t2, timebase)
return int(C.av_seek_frame((*C.struct_AVFormatContext)(s), C.int(st), C.int64_t(t2), AvseekFlagBackward))
}
//Seek to timestamp ts.
func (s *Context) AvformatSeekFile(si int, mit, ts, mat int64, f int) int {
return int(C.avformat_seek_file((*C.struct_AVFormatContext)(s), C.int(si), C.int64_t(mit), C.int64_t(ts), C.int64_t(mat), C.int(f)))
}
//Start playing a network-based stream (e.g.
func (s *Context) AvReadPlay() int {
return int(C.av_read_play((*C.struct_AVFormatContext)(s)))
}
//Pause a network-based stream (e.g.
func (s *Context) AvReadPause() int {
return int(C.av_read_pause((*C.struct_AVFormatContext)(s)))
}
//Close an opened input Context.
func (s *Context) AvformatCloseInput() {
C.avformat_close_input((**C.struct_AVFormatContext)(unsafe.Pointer(&s)))
}
//Allocate the stream private data and write the stream header to an output media file.
func (s *Context) AvformatWriteHeader(o **Dictionary) int {
return int(C.avformat_write_header((*C.struct_AVFormatContext)(s), (**C.struct_AVDictionary)(unsafe.Pointer(o))))
}
//Write a packet to an output media file.
func (s *Context) AvWriteFrame(pkt *avcodec.Packet) int {
return int(C.av_write_frame((*C.struct_AVFormatContext)(s), toCPacket(pkt)))
}
//Write a packet to an output media file ensuring correct interleaving.
func (s *Context) AvInterleavedWriteFrame(pkt *avcodec.Packet) int {
return int(C.av_interleaved_write_frame((*C.struct_AVFormatContext)(s), toCPacket(pkt)))
}
//Write a uncoded frame to an output media file.
func (s *Context) AvWriteUncodedFrame(si int, f *Frame) int {
return int(C.av_write_uncoded_frame((*C.struct_AVFormatContext)(s), C.int(si), (*C.struct_AVFrame)(f)))
}
//Write a uncoded frame to an output media file.
func (s *Context) AvInterleavedWriteUncodedFrame(si int, f *Frame) int {
return int(C.av_interleaved_write_uncoded_frame((*C.struct_AVFormatContext)(s), C.int(si), (*C.struct_AVFrame)(f)))
}
//Test whether a muxer supports uncoded frame.
func (s *Context) AvWriteUncodedFrameQuery(si int) int {
return int(C.av_write_uncoded_frame_query((*C.struct_AVFormatContext)(s), C.int(si)))
}
//Write the stream trailer to an output media file and free the file private data.
func (s *Context) AvWriteTrailer() int {
return int(C.av_write_trailer((*C.struct_AVFormatContext)(s)))
}
//Get timing information for the data currently output.
func (s *Context) AvGetOutputTimestamp(st int, dts, wall *int) int {
return int(C.av_get_output_timestamp((*C.struct_AVFormatContext)(s), C.int(st), (*C.int64_t)(unsafe.Pointer(&dts)), (*C.int64_t)(unsafe.Pointer(&wall))))
}
func (s *Context) AvFindDefaultStreamIndex() int {
return int(C.av_find_default_stream_index((*C.struct_AVFormatContext)(s)))
}
//Print detailed information about the input or output format, such as duration, bitrate, streams, container, programs, metadata, side data, codec and time base.
func (s *Context) AvDumpFormat(i int, url string, io int) {
C.av_dump_format((*C.struct_AVFormatContext)(unsafe.Pointer(s)), C.int(i), C.CString(url), C.int(io))
}
//Guess the sample aspect ratio of a frame, based on both the stream and the frame aspect ratio.
func (s *Context) AvGuessSampleAspectRatio(st *Stream, fr *Frame) avcodec.Rational {
return newRational(C.av_guess_sample_aspect_ratio((*C.struct_AVFormatContext)(s), (*C.struct_AVStream)(st), (*C.struct_AVFrame)(fr)))
}
//Guess the frame rate, based on both the container and codec information.
func (s *Context) AvGuessFrameRate(st *Stream, fr *Frame) avcodec.Rational {
return newRational(C.av_guess_frame_rate((*C.struct_AVFormatContext)(s), (*C.struct_AVStream)(st), (*C.struct_AVFrame)(fr)))
}
//Check if the stream st contained in s is matched by the stream specifier spec.
func (s *Context) AvformatMatchStreamSpecifier(st *Stream, spec string) int {
return int(C.avformat_match_stream_specifier((*C.struct_AVFormatContext)(s), (*C.struct_AVStream)(st), C.CString(spec)))
}
func (s *Context) AvformatQueueAttachedPictures() int {
return int(C.avformat_queue_attached_pictures((*C.struct_AVFormatContext)(s)))
}
func (s *Context) AvformatNewStream2(c *AvCodec) *Stream {
stream := (*Stream)(C.avformat_new_stream((*C.struct_AVFormatContext)(s), (*C.struct_AVCodec)(c)))
stream.codec.pix_fmt = int32(avcodec.AV_PIX_FMT_YUV)
stream.codec.width = 640
stream.codec.height = 480
stream.time_base.num = 1
stream.time_base.num = 25
return stream
}
// //av_format_control_message av_format_get_control_message_cb (const Context *s)
// func (s *Context) AvFormatControlMessage() C.av_format_get_control_message_cb {
// return C.av_format_get_control_message_cb((*C.struct_AVFormatContext)(s))
// }
// //void av_format_set_control_message_cb (Context *s, av_format_control_message callback)
// func (s *Context) AvFormatSetControlMessageCb(c AvFormatControlMessage) C.av_format_get_control_message_cb {
// C.av_format_set_control_message_cb((*C.struct_AVFormatContext)(s), (C.struct_AVFormatControlMessage)(c))
// }
// //AvCodec * av_format_get_data_codec (const Context *s)
// func (s *Context)AvFormatGetDataCodec() *AvCodec {
// return (*AvCodec)(C.av_format_get_data_codec((*C.struct_AVFormatContext)(s)))
// }
// //void av_format_set_data_codec (Context *s, AvCodec *c)
// func (s *Context)AvFormatSetDataCodec( c *AvCodec) {
// C.av_format_set_data_codec((*C.struct_AVFormatContext)(s), (*C.struct_AVCodec)(c))
// }