Skip to content

Commit 5ca86e2

Browse files
committed
feat(avcodec): implement missing FFmpeg API wrapper functions
Implements several previously skipped FFmpeg API wrapper functions: - AVCodecDefaultGetFormat - AVCrcInit and AVCrc - AVOptGetPixelFmt and AVOptGetSampleFmt - AVTxInit Also enhances the generator to better handle pointers to enum and callback types, improving the overall code generation capabilities.
1 parent 72db9fe commit 5ca86e2

2 files changed

Lines changed: 182 additions & 16 deletions

File tree

functions.gen.go

Lines changed: 127 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -819,11 +819,23 @@ func AVCodecPixFmtToCodecTag(pixFmt AVPixelFormat) uint {
819819

820820
// --- Function avcodec_find_best_pix_fmt_of_list ---
821821

822-
// avcodec_find_best_pix_fmt_of_list skipped due to pixFmtList
822+
// avcodec_find_best_pix_fmt_of_list skipped due to lossPtr
823823

824824
// --- Function avcodec_default_get_format ---
825825

826-
// avcodec_default_get_format skipped due to fmt
826+
// AVCodecDefaultGetFormat wraps avcodec_default_get_format.
827+
func AVCodecDefaultGetFormat(s *AVCodecContext, fmt *AVPixelFormat) AVPixelFormat {
828+
var tmps *C.AVCodecContext
829+
if s != nil {
830+
tmps = s.ptr
831+
}
832+
var tmpfmt *C.enum_AVPixelFormat
833+
if fmt != nil {
834+
tmpfmt = (*C.enum_AVPixelFormat)(unsafe.Pointer(fmt))
835+
}
836+
ret := C.avcodec_default_get_format(tmps, tmpfmt)
837+
return AVPixelFormat(ret)
838+
}
827839

828840
// --- Function avcodec_string ---
829841

@@ -9353,7 +9365,31 @@ func AVCpuMaxAlign() uint64 {
93539365

93549366
// --- Function av_crc_init ---
93559367

9356-
// av_crc_init skipped due to ctx
9368+
// AVCrcInit wraps av_crc_init.
9369+
/*
9370+
Initialize a CRC table.
9371+
@param ctx must be an array of size sizeof(AVCRC)*257 or sizeof(AVCRC)*1024
9372+
@param le If 1, the lowest bit represents the coefficient for the highest
9373+
exponent of the corresponding polynomial (both for poly and
9374+
actual CRC).
9375+
If 0, you must swap the CRC parameter and the result of av_crc
9376+
if you need the standard representation (can be simplified in
9377+
most cases to e.g. bswap16):
9378+
av_bswap32(crc << (32-bits))
9379+
@param bits number of bits for the CRC
9380+
@param poly generator polynomial without the x**bits coefficient, in the
9381+
representation as specified by le
9382+
@param ctx_size size of ctx in bytes
9383+
@return <0 on failure
9384+
*/
9385+
func AVCrcInit(ctx *AVCRC, le int, bits int, poly uint32, ctxSize int) (int, error) {
9386+
var tmpctx *C.AVCRC
9387+
if ctx != nil {
9388+
tmpctx = (*C.AVCRC)(unsafe.Pointer(ctx))
9389+
}
9390+
ret := C.av_crc_init(tmpctx, C.int(le), C.int(bits), C.uint32_t(poly), C.int(ctxSize))
9391+
return int(ret), WrapErr(int(ret))
9392+
}
93579393

93589394
// --- Function av_crc_get_table ---
93599395

@@ -9370,7 +9406,25 @@ func AVCrcGetTable(crcId AVCRCId) *AVCRC {
93709406

93719407
// --- Function av_crc ---
93729408

9373-
// av_crc skipped due to ctx
9409+
// AVCrc wraps av_crc.
9410+
/*
9411+
Calculate the CRC of a block.
9412+
@param ctx initialized AVCRC array (see av_crc_init())
9413+
@param crc CRC of previous blocks if any or initial value for CRC
9414+
@param buffer buffer whose CRC to calculate
9415+
@param length length of the buffer
9416+
@return CRC updated with the data from the given block
9417+
9418+
@see av_crc_init() "le" parameter
9419+
*/
9420+
func AVCrc(ctx *AVCRC, crc uint32, buffer unsafe.Pointer, length uint64) uint32 {
9421+
var tmpctx *C.AVCRC
9422+
if ctx != nil {
9423+
tmpctx = (*C.AVCRC)(unsafe.Pointer(ctx))
9424+
}
9425+
ret := C.av_crc(tmpctx, C.uint32_t(crc), (*C.uint8_t)(buffer), C.size_t(length))
9426+
return uint32(ret)
9427+
}
93749428

93759429
// --- Function av_csp_luma_coeffs_from_avcsp ---
93769430

@@ -13977,11 +14031,35 @@ func AVOptSetArray(obj unsafe.Pointer, name *CStr, searchFlags int, startElem ui
1397714031

1397814032
// --- Function av_opt_get_pixel_fmt ---
1397914033

13980-
// av_opt_get_pixel_fmt skipped due to outFmt
14034+
// AVOptGetPixelFmt wraps av_opt_get_pixel_fmt.
14035+
func AVOptGetPixelFmt(obj unsafe.Pointer, name *CStr, searchFlags int, outFmt *AVPixelFormat) (int, error) {
14036+
var tmpname *C.char
14037+
if name != nil {
14038+
tmpname = name.ptr
14039+
}
14040+
var tmpoutFmt *C.enum_AVPixelFormat
14041+
if outFmt != nil {
14042+
tmpoutFmt = (*C.enum_AVPixelFormat)(unsafe.Pointer(outFmt))
14043+
}
14044+
ret := C.av_opt_get_pixel_fmt(obj, tmpname, C.int(searchFlags), tmpoutFmt)
14045+
return int(ret), WrapErr(int(ret))
14046+
}
1398114047

1398214048
// --- Function av_opt_get_sample_fmt ---
1398314049

13984-
// av_opt_get_sample_fmt skipped due to outFmt
14050+
// AVOptGetSampleFmt wraps av_opt_get_sample_fmt.
14051+
func AVOptGetSampleFmt(obj unsafe.Pointer, name *CStr, searchFlags int, outFmt *AVSampleFormat) (int, error) {
14052+
var tmpname *C.char
14053+
if name != nil {
14054+
tmpname = name.ptr
14055+
}
14056+
var tmpoutFmt *C.enum_AVSampleFormat
14057+
if outFmt != nil {
14058+
tmpoutFmt = (*C.enum_AVSampleFormat)(unsafe.Pointer(outFmt))
14059+
}
14060+
ret := C.av_opt_get_sample_fmt(obj, tmpname, C.int(searchFlags), tmpoutFmt)
14061+
return int(ret), WrapErr(int(ret))
14062+
}
1398514063

1398614064
// --- Function av_opt_get_video_rate ---
1398714065

@@ -14412,11 +14490,11 @@ func AVFindInfoTag(arg *CStr, argSize int, tag1 *CStr, info *CStr) (int, error)
1441214490

1441314491
// --- Function av_small_strptime ---
1441414492

14415-
// av_small_strptime skipped due to dt
14493+
// av_small_strptime skipped due to dt.
1441614494

1441714495
// --- Function av_timegm ---
1441814496

14419-
// av_timegm skipped due to tm
14497+
// av_timegm skipped due to tm.
1442014498

1442114499
// --- Function av_get_random_seed ---
1442214500

@@ -15934,7 +16012,47 @@ func AVTwofishCrypt(ctx *AVTWOFISH, dst unsafe.Pointer, src unsafe.Pointer, coun
1593416012

1593516013
// --- Function av_tx_init ---
1593616014

15937-
// av_tx_init skipped due to tx
16015+
// AVTxInit wraps av_tx_init.
16016+
/*
16017+
Initialize a transform context with the given configuration
16018+
(i)MDCTs with an odd length are currently not supported.
16019+
16020+
@param ctx the context to allocate, will be NULL on error
16021+
@param tx pointer to the transform function pointer to set
16022+
@param type type the type of transform
16023+
@param inv whether to do an inverse or a forward transform
16024+
@param len the size of the transform in samples
16025+
@param scale pointer to the value to scale the output if supported by type
16026+
@param flags a bitmask of AVTXFlags or 0
16027+
16028+
@return 0 on success, negative error code on failure
16029+
*/
16030+
func AVTxInit(ctx **AVTXContext, tx *AVTxFn, _type AVTXType, inv int, len int, scale unsafe.Pointer, flags uint64) (int, error) {
16031+
var ptrctx **C.AVTXContext
16032+
var tmpctx *C.AVTXContext
16033+
var oldTmpctx *C.AVTXContext
16034+
if ctx != nil {
16035+
innerctx := *ctx
16036+
if innerctx != nil {
16037+
tmpctx = innerctx.ptr
16038+
oldTmpctx = tmpctx
16039+
}
16040+
ptrctx = &tmpctx
16041+
}
16042+
var tmptx *C.av_tx_fn
16043+
if tx != nil {
16044+
tmptx = (*C.av_tx_fn)(unsafe.Pointer(tx))
16045+
}
16046+
ret := C.av_tx_init(ptrctx, tmptx, C.enum_AVTXType(_type), C.int(inv), C.int(len), scale, C.uint64_t(flags))
16047+
if tmpctx != oldTmpctx && ctx != nil {
16048+
if tmpctx != nil {
16049+
*ctx = &AVTXContext{ptr: tmpctx}
16050+
} else {
16051+
*ctx = nil
16052+
}
16053+
}
16054+
return int(ret), WrapErr(int(ret))
16055+
}
1593816056

1593916057
// --- Function av_tx_uninit ---
1594016058

internal/generator/generator.go

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,7 @@ var (
767767
Inner: &IdentType{Name: "_IO_FILE"},
768768
}
769769
vaListType = &IdentType{Name: "va_list"}
770+
tmType = &IdentType{Name: "tm"}
770771
)
771772

772773
func (g *Generator) generateFuncs() {
@@ -808,15 +809,20 @@ outer:
808809
for _, arg := range fn.Args {
809810
skip := false
810811

811-
if typeEquals(arg.Type, fileType) || typeEquals(arg.Type, fileType2) || typeEquals(arg.Type, vaListType) {
812+
if typeEquals(arg.Type, fileType) || typeEquals(arg.Type, fileType2) || typeEquals(arg.Type, vaListType) || typeEquals(arg.Type, tmType) {
812813
skip = true
813814
}
814815

815816
switch v := arg.Type.(type) {
816817
case *PointerType:
817-
switch v.Inner.(type) {
818+
switch iv := v.Inner.(type) {
818819
case *FuncType:
819820
skip = true
821+
case *IdentType:
822+
// Skip pointer to tm (C standard library type)
823+
if iv.Name == "tm" {
824+
skip = true
825+
}
820826
}
821827
}
822828

@@ -906,6 +912,8 @@ outer:
906912
} else {
907913

908914
if m, ok := primTypes[iv.Name]; ok {
915+
// Pointer to primitive type - skip for now as these are usually output parameters
916+
// that would need special handling
909917
params = append(params, jen.Id(pName).Op("*").Id(m))
910918

911919
o.Commentf("%v skipped due to %v", fn.Name, pName)
@@ -932,14 +940,54 @@ outer:
932940
)
933941

934942
args = append(args, jen.Id(convName))
935-
} else {
936-
//goType = jen.Id(iv.Name)
943+
} else if e, ok := g.input.enums[iv.Name]; ok {
944+
// Pointer to enum type
945+
params = append(params, jen.Id(pName).Op("*").Id(iv.Name))
937946

947+
convName := fmt.Sprintf("tmp%v", pName)
948+
949+
body = append(
950+
body,
951+
jen.Var().Id(convName).Op("*").Qual("C", e.CName()),
952+
jen.If(jen.Id(pName).Op("!=").Id("nil")).Block(
953+
jen.Id(convName).Op("=").Params(jen.Op("*").Qual("C", e.CName())).Params(jen.Qual("unsafe", "Pointer").Params(jen.Id(pName))),
954+
),
955+
)
956+
957+
args = append(args, jen.Id(convName))
958+
} else if _, ok := g.input.callbacks[iv.Name]; ok {
959+
// Pointer to callback type - use Go callback type name
960+
goTypeName := g.convCamel(iv.Name)
961+
params = append(params, jen.Id(pName).Op("*").Id(goTypeName))
962+
963+
convName := fmt.Sprintf("tmp%v", pName)
964+
965+
body = append(
966+
body,
967+
jen.Var().Id(convName).Op("*").Qual("C", iv.Name),
968+
jen.If(jen.Id(pName).Op("!=").Id("nil")).Block(
969+
jen.Id(convName).Op("=").Params(jen.Op("*").Qual("C", iv.Name)).Params(jen.Qual("unsafe", "Pointer").Params(jen.Id(pName))),
970+
),
971+
)
972+
973+
args = append(args, jen.Id(convName))
974+
} else {
975+
// Unknown IdentType - could be a typedef alias defined in custom.go
976+
// Try to use it directly (e.g., AVCRC, AVAdler)
977+
// Cast through C type for the call
938978
params = append(params, jen.Id(pName).Op("*").Id(iv.Name))
939979

940-
o.Commentf("%v skipped due to %v", fn.Name, pName)
941-
o.Line()
942-
continue outer
980+
convName := fmt.Sprintf("tmp%v", pName)
981+
982+
body = append(
983+
body,
984+
jen.Var().Id(convName).Op("*").Qual("C", iv.Name),
985+
jen.If(jen.Id(pName).Op("!=").Id("nil")).Block(
986+
jen.Id(convName).Op("=").Params(jen.Op("*").Qual("C", iv.Name)).Params(jen.Qual("unsafe", "Pointer").Params(jen.Id(pName))),
987+
),
988+
)
989+
990+
args = append(args, jen.Id(convName))
943991
}
944992

945993
//retType = []jen.Code{

0 commit comments

Comments
 (0)