From 9183e4f12342b8dbc456252b7ec4191ffffa64ab Mon Sep 17 00:00:00 2001 From: Jonathan Viney Date: Fri, 19 Apr 2019 00:30:46 +1200 Subject: [PATCH] Add bindings for avutil AVDictionary and AVDictionaryEntry. --- avformat/context.go | 4 +- avformat/context_struct.go | 4 +- avformat/stream_struct.go | 4 +- avutil/avutil.go | 1 - avutil/dict.go | 133 +++++++++++++++++++++++++++++++++++++ 5 files changed, 139 insertions(+), 7 deletions(-) create mode 100644 avutil/dict.go diff --git a/avformat/context.go b/avformat/context.go index c25b536..476f8e5 100644 --- a/avformat/context.go +++ b/avformat/context.go @@ -89,7 +89,7 @@ func (s *Context) AvNewProgram(id int) *AvProgram { } //Read packets of a media file to get stream information. -func (s *Context) AvformatFindStreamInfo(d **Dictionary) int { +func (s *Context) AvformatFindStreamInfo(d **avutil.Dictionary) int { return int(C.avformat_find_stream_info((*C.struct_AVFormatContext)(s), (**C.struct_AVDictionary)(unsafe.Pointer(d)))) } @@ -142,7 +142,7 @@ func (s *Context) AvformatCloseInput() { } //Allocate the stream private data and write the stream header to an output media file. -func (s *Context) AvformatWriteHeader(o **Dictionary) int { +func (s *Context) AvformatWriteHeader(o **avutil.Dictionary) int { return int(C.avformat_write_header((*C.struct_AVFormatContext)(s), (**C.struct_AVDictionary)(unsafe.Pointer(o)))) } diff --git a/avformat/context_struct.go b/avformat/context_struct.go index 6ad373a..2d2696a 100644 --- a/avformat/context_struct.go +++ b/avformat/context_struct.go @@ -27,8 +27,8 @@ func (ctxt *Context) VideoCodec() *AvCodec { return (*AvCodec)(unsafe.Pointer(ctxt.video_codec)) } -func (ctxt *Context) Metadata() *Dictionary { - return (*Dictionary)(unsafe.Pointer(ctxt.metadata)) +func (ctxt *Context) Metadata() *avutil.Dictionary { + return (*avutil.Dictionary)(unsafe.Pointer(ctxt.metadata)) } func (ctxt *Context) Internal() *AvFormatInternal { diff --git a/avformat/stream_struct.go b/avformat/stream_struct.go index d7947eb..fe87264 100644 --- a/avformat/stream_struct.go +++ b/avformat/stream_struct.go @@ -20,8 +20,8 @@ func (avs *Stream) Codec() *CodecContext { return (*CodecContext)(unsafe.Pointer(avs.codec)) } -func (avs *Stream) Metadata() *Dictionary { - return (*Dictionary)(unsafe.Pointer(avs.metadata)) +func (avs *Stream) Metadata() *avutil.Dictionary { + return (*avutil.Dictionary)(unsafe.Pointer(avs.metadata)) } func (avs *Stream) IndexEntries() *AvIndexEntry { diff --git a/avutil/avutil.go b/avutil/avutil.go index e915b46..1a41667 100644 --- a/avutil/avutil.go +++ b/avutil/avutil.go @@ -17,7 +17,6 @@ import ( type ( Options C.struct_AVOptions - Dictionary C.struct_AVDictionary AvTree C.struct_AVTree Rational C.struct_AVRational MediaType C.enum_AVMediaType diff --git a/avutil/dict.go b/avutil/dict.go new file mode 100644 index 0000000..77091c4 --- /dev/null +++ b/avutil/dict.go @@ -0,0 +1,133 @@ +// Use of this source code is governed by a MIT license that can be found in the LICENSE file. +// Giorgis (habtom@giorgis.io) + +// Package avutil is a utility library to aid portable multimedia programming. +// It contains safe portable string functions, random number generators, data structures, +// additional mathematics functions, cryptography and multimedia related functionality. +// Some generic features and utilities provided by the libavutil library +package avutil + +//#cgo pkg-config: libavutil +//#include +//#include +//#include +import "C" +import ( + "unsafe" +) + +type ( + Dictionary C.struct_AVDictionary + DictionaryEntry C.struct_AVDictionaryEntry +) + +const ( + AV_DICT_MATCH_CASE = int(C.AV_DICT_MATCH_CASE) + AV_DICT_IGNORE_SUFFIX = int(C.AV_DICT_IGNORE_SUFFIX) + AV_DICT_DONT_STRDUP_KEY = int(C.AV_DICT_DONT_STRDUP_KEY) + AV_DICT_DONT_STRDUP_VAL = int(C.AV_DICT_DONT_STRDUP_VAL) + AV_DICT_DONT_OVERWRITE = int(C.AV_DICT_DONT_OVERWRITE) + AV_DICT_APPEND = int(C.AV_DICT_APPEND) + AV_DICT_MULTIKEY = int(C.AV_DICT_MULTIKEY) +) + +func (d *Dictionary) AvDictGet(key string, prev *DictionaryEntry, flags int) *DictionaryEntry { + Ckey := C.CString(key) + defer C.free(unsafe.Pointer(Ckey)) + + return (*DictionaryEntry)(C.av_dict_get( + (*C.struct_AVDictionary)(d), + Ckey, + (*C.struct_AVDictionaryEntry)(prev), + C.int(flags), + )) +} + +func (d *Dictionary) AvDictCount() int { + return int(C.av_dict_count((*C.struct_AVDictionary)(d))) +} + +func (d *Dictionary) AvDictSet(key, value string, flags int) int { + Ckey := C.CString(key) + defer C.free(unsafe.Pointer(Ckey)) + + Cvalue := C.CString(value) + defer C.free(unsafe.Pointer(Cvalue)) + + return int(C.av_dict_set( + (**C.struct_AVDictionary)(unsafe.Pointer(&d)), + Ckey, + Cvalue, + C.int(flags), + )) +} + +func (d *Dictionary) AvDictSetInt(key string, value int64, flags int) int { + Ckey := C.CString(key) + defer C.free(unsafe.Pointer(Ckey)) + + return int(C.av_dict_set_int( + (**C.struct_AVDictionary)(unsafe.Pointer(&d)), + Ckey, + C.int64_t(value), + C.int(flags), + )) +} + +func (d *Dictionary) AvDictParseString(str, key_val_sep, pairs_sep string, flags int) int { + Cstr := C.CString(str) + defer C.free(unsafe.Pointer(Cstr)) + + Ckey_val_sep := C.CString(key_val_sep) + defer C.free(unsafe.Pointer(Ckey_val_sep)) + + Cpairs_sep := C.CString(pairs_sep) + defer C.free(unsafe.Pointer(Cpairs_sep)) + + return int(C.av_dict_parse_string( + (**C.struct_AVDictionary)(unsafe.Pointer(&d)), + Cstr, + Ckey_val_sep, + Cpairs_sep, + C.int(flags), + )) +} + +func (d *Dictionary) AvDictCopy(src *Dictionary, flags int) int { + return int(C.av_dict_copy( + (**C.struct_AVDictionary)(unsafe.Pointer(&d)), + (*C.struct_AVDictionary)(unsafe.Pointer(src)), + C.int(flags), + )) +} + +func (d *Dictionary) AvDictFree() { + C.av_dict_free((**C.struct_AVDictionary)(unsafe.Pointer(&d))) +} + +func (d *Dictionary) AvDictGetString(key_val_sep, pairs_sep byte) (int, string) { + var Cbuf *C.char + + ret := int(C.av_dict_get_string( + (*C.struct_AVDictionary)(d), + (**C.char)(&Cbuf), + C.char(key_val_sep), + C.char(pairs_sep), + )) + + var buf string + if ret == 0 { + buf = C.GoString(Cbuf) + C.free(unsafe.Pointer(Cbuf)) + } + + return ret, buf +} + +func (d *DictionaryEntry) Key() string { + return C.GoString(d.key) +} + +func (d *DictionaryEntry) Value() string { + return C.GoString(d.value) +}