forked from giorgisio/goav
/
avcodec.go
executable file
·287 lines (235 loc) · 9.82 KB
/
avcodec.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
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// Giorgis (habtom@giorgis.io)
//Package avcodec contains the codecs (decoders and encoders) provided by the libavcodec library
//Provides some generic global options, which can be set on all the encoders and decoders.
package avcodec
//#cgo pkg-config: libavformat libavcodec libavutil libswresample
//#include <stdio.h>
//#include <stdlib.h>
//#include <inttypes.h>
//#include <stdint.h>
//#include <string.h>
//#include <libavformat/avformat.h>
//#include <libavcodec/avcodec.h>
//#include <libavutil/avutil.h>
import "C"
import (
"unsafe"
)
const (
AV_NOPTS_VALUE = int64(C.AV_NOPTS_VALUE)
)
type (
Codec C.struct_AVCodec
Context C.struct_AVCodecContext
Descriptor C.struct_AVCodecDescriptor
Parser C.struct_AVCodecParser
ParserContext C.struct_AVCodecParserContext
Dictionary C.struct_AVDictionary
Frame C.struct_AVFrame
MediaType C.enum_AVMediaType
Packet C.struct_AVPacket
BitStreamFilter C.struct_AVBitStreamFilter
BitStreamFilterContext C.struct_AVBitStreamFilterContext
Rational C.struct_AVRational
Class C.struct_AVClass
AvCodecParameters C.struct_AVCodecParameters
AvHWAccel C.struct_AVHWAccel
AvPacketSideData C.struct_AVPacketSideData
AvPanScan C.struct_AVPanScan
Picture C.struct_AVPicture
AvProfile C.struct_AVProfile
AvSubtitle C.struct_AVSubtitle
AvSubtitleRect C.struct_AVSubtitleRect
RcOverride C.struct_RcOverride
AvBufferRef C.struct_AVBufferRef
AvAudioServiceType C.enum_AVAudioServiceType
AvChromaLocation C.enum_AVChromaLocation
CodecId C.enum_AVCodecID
AvColorPrimaries C.enum_AVColorPrimaries
AvColorRange C.enum_AVColorRange
AvColorSpace C.enum_AVColorSpace
AvColorTransferCharacteristic C.enum_AVColorTransferCharacteristic
AvDiscard C.enum_AVDiscard
AvFieldOrder C.enum_AVFieldOrder
AvPacketSideDataType C.enum_AVPacketSideDataType
PixelFormat C.enum_AVPixelFormat
AvSampleFormat C.enum_AVSampleFormat
)
func (cp *AvCodecParameters) AvCodecGetId() CodecId {
return *((*CodecId)(unsafe.Pointer(&cp.codec_id)))
}
func (cp *AvCodecParameters) AvCodecGetType() MediaType {
return *((*MediaType)(unsafe.Pointer(&cp.codec_type)))
}
func (cp *AvCodecParameters) AvCodecGetWidth() int {
return *((*int)(unsafe.Pointer(&cp.width)))
}
func (cp *AvCodecParameters) AvCodecGetHeight() int {
return *((*int)(unsafe.Pointer(&cp.height)))
}
func (cp *AvCodecParameters) AvCodecGetChannels() int {
return *((*int)(unsafe.Pointer(&cp.channels)))
}
func (cp *AvCodecParameters) AvCodecGetSampleRate() int {
return *((*int)(unsafe.Pointer(&cp.sample_rate)))
}
func (c *Codec) AvCodecGetMaxLowres() int {
return int(C.av_codec_get_max_lowres((*C.struct_AVCodec)(c)))
}
// AvCodecNext If c is NULL, returns the first registered codec, if c is non-NULL,
func (c *Codec) AvCodecNext() *Codec {
return (*Codec)(C.av_codec_next((*C.struct_AVCodec)(c)))
}
// Register the codec codec and initialize libavcodec.
func (c *Codec) AvcodecRegister() {
C.avcodec_register((*C.struct_AVCodec)(c))
}
//Return a name for the specified profile, if available.
func (c *Codec) AvGetProfileName(p int) string {
return C.GoString(C.av_get_profile_name((*C.struct_AVCodec)(c), C.int(p)))
}
//Allocate an Context and set its fields to default values.
func (c *Codec) AvcodecAllocContext3() *Context {
return (*Context)(C.avcodec_alloc_context3((*C.struct_AVCodec)(c)))
}
func (c *Codec) AvCodecIsEncoder() int {
return int(C.av_codec_is_encoder((*C.struct_AVCodec)(c)))
}
func (c *Codec) AvCodecIsDecoder() int {
return int(C.av_codec_is_decoder((*C.struct_AVCodec)(c)))
}
//Same behaviour av_fast_malloc but the buffer has additional FF_INPUT_BUFFER_PADDING_SIZE at the end which will always be 0.
func AvFastPaddedMalloc(p unsafe.Pointer, s *uint, t uintptr) {
C.av_fast_padded_malloc(p, (*C.uint)(unsafe.Pointer(s)), (C.size_t)(t))
}
//Return the LIBAvCODEC_VERSION_INT constant.
func AvcodecVersion() uint {
return uint(C.avcodec_version())
}
//Return the libavcodec build-time configuration.
func AvcodecConfiguration() string {
return C.GoString(C.avcodec_configuration())
}
//Return the libavcodec license.
func AvcodecLicense() string {
return C.GoString(C.avcodec_license())
}
//Register all the codecs, parsers and bitstream filters which were enabled at configuration time.
func AvcodecRegisterAll() {
C.av_register_all()
C.avcodec_register_all()
// C.av_log_set_level(0xffff)
}
//Get the Class for Context.
func AvcodecGetClass() *Class {
return (*Class)(C.avcodec_get_class())
}
//Get the Class for Frame.
func AvcodecGetFrameClass() *Class {
return (*Class)(C.avcodec_get_frame_class())
}
//Get the Class for AvSubtitleRect.
func AvcodecGetSubtitleRectClass() *Class {
return (*Class)(C.avcodec_get_subtitle_rect_class())
}
//Free all allocated data in the given subtitle struct.
func AvsubtitleFree(s *AvSubtitle) {
C.avsubtitle_free((*C.struct_AVSubtitle)(s))
}
func AvPacketAlloc() *Packet {
return (*Packet)(C.av_packet_alloc())
}
//Pack a dictionary for use in side_data.
func AvPacketPackDictionary(d *Dictionary, s *int) *uint8 {
return (*uint8)(C.av_packet_pack_dictionary((*C.struct_AVDictionary)(d), (*C.int)(unsafe.Pointer(s))))
}
//Unpack a dictionary from side_data.
func AvPacketUnpackDictionary(d *uint8, s int, dt **Dictionary) int {
return int(C.av_packet_unpack_dictionary((*C.uint8_t)(d), C.int(s), (**C.struct_AVDictionary)(unsafe.Pointer(dt))))
}
//Find a registered decoder with a matching codec ID.
func AvcodecFindDecoder(id CodecId) *Codec {
return (*Codec)(C.avcodec_find_decoder((C.enum_AVCodecID)(id)))
}
func AvCodecIterate(p *unsafe.Pointer) *Codec {
return (*Codec)(C.av_codec_iterate(p))
}
//Find a registered decoder with the specified name.
func AvcodecFindDecoderByName(n string) *Codec {
return (*Codec)(C.avcodec_find_decoder_by_name(C.CString(n)))
}
//Converts AvChromaLocation to swscale x/y chroma position.
func AvcodecEnumToChromaPos(x, y *int, l AvChromaLocation) int {
return int(C.avcodec_enum_to_chroma_pos((*C.int)(unsafe.Pointer(x)), (*C.int)(unsafe.Pointer(y)), (C.enum_AVChromaLocation)(l)))
}
//Converts swscale x/y chroma position to AvChromaLocation.
func AvcodecChromaPosToEnum(x, y int) AvChromaLocation {
return (AvChromaLocation)(C.avcodec_chroma_pos_to_enum(C.int(x), C.int(y)))
}
//Find a registered encoder with a matching codec ID.
func AvcodecFindEncoder(id CodecId) *Codec {
return (*Codec)(C.avcodec_find_encoder((C.enum_AVCodecID)(id)))
}
//Find a registered encoder with the specified name.
func AvcodecFindEncoderByName(c string) *Codec {
return (*Codec)(C.avcodec_find_encoder_by_name(C.CString(c)))
}
//Put a string representing the codec tag codec_tag in buf.
func AvGetCodecTagString(b string, bf uintptr, c uint) uintptr {
return uintptr(C.av_get_codec_tag_string(C.CString(b), C.size_t(bf), C.uint(c)))
}
func AvcodecString(b string, bs int, ctxt *Context, e int) {
C.avcodec_string(C.CString(b), C.int(bs), (*C.struct_AVCodecContext)(ctxt), C.int(e))
}
//Fill Frame audio data and linesize pointers.
func AvcodecFillAudioFrame(f *Frame, c int, s AvSampleFormat, b *uint8, bs, a int) int {
return int(C.avcodec_fill_audio_frame((*C.struct_AVFrame)(f), C.int(c), (C.enum_AVSampleFormat)(s), (*C.uint8_t)(b), C.int(bs), C.int(a)))
}
//Return codec bits per sample.
func AvGetBitsPerSample(c CodecId) int {
return int(C.av_get_bits_per_sample((C.enum_AVCodecID)(c)))
}
//Return the PCM codec associated with a sample format.
func AvGetPcmCodec(f AvSampleFormat, b int) CodecId {
return (CodecId)(C.av_get_pcm_codec((C.enum_AVSampleFormat)(f), C.int(b)))
}
//Return codec bits per sample.
func AvGetExactBitsPerSample(c CodecId) int {
return int(C.av_get_exact_bits_per_sample((C.enum_AVCodecID)(c)))
}
//Same behaviour av_fast_padded_malloc except that buffer will always be 0-initialized after call.
func AvFastPaddedMallocz(p unsafe.Pointer, s *uint, t uintptr) {
C.av_fast_padded_mallocz(p, (*C.uint)(unsafe.Pointer(s)), (C.size_t)(t))
}
//Encode extradata length to a buffer.
func AvXiphlacing(s *string, v uint) uint {
return uint(C.av_xiphlacing((*C.uchar)(unsafe.Pointer(s)), (C.uint)(v)))
}
//If hwaccel is NULL, returns the first registered hardware accelerator, if hwaccel is non-NULL,
//returns the next registered hardware accelerator after hwaccel, or NULL if hwaccel is the last one.
func (a *AvHWAccel) AvHwaccelNext() *AvHWAccel {
return (*AvHWAccel)(C.av_hwaccel_next((*C.struct_AVHWAccel)(a)))
}
//Get the type of the given codec.
func AvcodecGetType(c CodecId) MediaType {
return (MediaType)(C.avcodec_get_type((C.enum_AVCodecID)(c)))
}
//Get the name of a codec.
func AvcodecGetName(d CodecId) string {
return C.GoString(C.avcodec_get_name((C.enum_AVCodecID)(d)))
}
//const Descriptor *avcodec_descriptor_get (enum CodecId id)
func AvcodecDescriptorGet(id CodecId) *Descriptor {
return (*Descriptor)(C.avcodec_descriptor_get((C.enum_AVCodecID)(id)))
}
//Iterate over all codec descriptors known to libavcodec.
func (d *Descriptor) AvcodecDescriptorNext() *Descriptor {
return (*Descriptor)(C.avcodec_descriptor_next((*C.struct_AVCodecDescriptor)(d)))
}
func AvcodecDescriptorGetByName(n string) *Descriptor {
return (*Descriptor)(C.avcodec_descriptor_get_by_name(C.CString(n)))
}
func AvFetSampleFmtName(sample_fmt AvSampleFormat) string {
return C.GoString(C.av_get_sample_fmt_name(C.enum_AVSampleFormat(sample_fmt)))
}