go-astiav/codec.go

116 lines
2.5 KiB
Go
Raw Normal View History

2022-02-05 17:27:03 +08:00
package astiav
2023-03-23 21:59:40 +08:00
//#cgo pkg-config: libavcodec libavutil
2022-02-05 17:27:03 +08:00
//#include <libavcodec/avcodec.h>
2023-03-23 21:59:40 +08:00
//#include <libavutil/channel_layout.h>
2022-02-05 17:27:03 +08:00
import "C"
import (
"unsafe"
)
// https://github.com/FFmpeg/FFmpeg/blob/n5.0/libavcodec/codec.h#L202
type Codec struct {
c *C.struct_AVCodec
}
func newCodecFromC(c *C.struct_AVCodec) *Codec {
if c == nil {
return nil
}
return &Codec{c: c}
}
func (c *Codec) Name() string {
return C.GoString(c.c.name)
}
func (c *Codec) String() string {
return c.Name()
}
func (c *Codec) ChannelLayouts() (o []ChannelLayout) {
2023-03-23 21:59:40 +08:00
if c.c.ch_layouts == nil {
2022-02-05 17:27:03 +08:00
return nil
}
2023-03-23 21:59:40 +08:00
size := unsafe.Sizeof(*c.c.ch_layouts)
2022-02-05 17:27:03 +08:00
for i := 0; ; i++ {
v, _ := newChannelLayoutFromC((*C.struct_AVChannelLayout)(unsafe.Pointer(uintptr(unsafe.Pointer(c.c.ch_layouts)) + uintptr(i)*size))).clone()
2023-03-23 21:59:40 +08:00
if !v.Valid() {
2022-02-05 17:27:03 +08:00
break
}
2023-03-23 21:59:40 +08:00
o = append(o, v)
2022-02-05 17:27:03 +08:00
}
return
}
func (c *Codec) IsDecoder() bool {
return int(C.av_codec_is_decoder(c.c)) != 0
}
func (c *Codec) IsEncoder() bool {
return int(C.av_codec_is_encoder(c.c)) != 0
}
func (c *Codec) PixelFormats() (o []PixelFormat) {
if c.c.pix_fmts == nil {
return nil
}
size := unsafe.Sizeof(*c.c.pix_fmts)
for i := 0; ; i++ {
p := *(*C.int)(unsafe.Pointer(uintptr(unsafe.Pointer(c.c.pix_fmts)) + uintptr(i)*size))
if p == C.AV_PIX_FMT_NONE {
break
}
o = append(o, PixelFormat(p))
}
return
}
func (c *Codec) SampleFormats() (o []SampleFormat) {
if c.c.sample_fmts == nil {
return nil
}
size := unsafe.Sizeof(*c.c.sample_fmts)
for i := 0; ; i++ {
p := *(*C.int)(unsafe.Pointer(uintptr(unsafe.Pointer(c.c.sample_fmts)) + uintptr(i)*size))
if p == C.AV_SAMPLE_FMT_NONE {
break
}
o = append(o, SampleFormat(p))
}
return
}
func FindDecoder(id CodecID) *Codec {
return newCodecFromC(C.avcodec_find_decoder((C.enum_AVCodecID)(id)))
}
func FindDecoderByName(n string) *Codec {
cn := C.CString(n)
defer C.free(unsafe.Pointer(cn))
return newCodecFromC(C.avcodec_find_decoder_by_name(cn))
}
func FindEncoder(id CodecID) *Codec {
return newCodecFromC(C.avcodec_find_encoder((C.enum_AVCodecID)(id)))
}
func FindEncoderByName(n string) *Codec {
cn := C.CString(n)
defer C.free(unsafe.Pointer(cn))
return newCodecFromC(C.avcodec_find_encoder_by_name(cn))
}
Implementation for HW Context (#32) * Adding HW Context * Check for hw pixel format and transfer data on ReciveFrame when its hw pixel format * Remove hw_decoding example and update demuxing_dec example * unref hw_device_ctx, and improve example Unref correctly hw_device_ctx on free decoding example has now a flag for hw decoding Use error on hw context because its good to know the error that happen Add a method to list supported hw codecs so user could check if system has a specif hwcodec before the create a hw context * Fix test to use err, and update hw with device to also return error * revert changes in demux example * Add/Modify according to review Seperate Hardware Device Context and Test Renaming HW to Hardware Create dedicated hardware_decoding Example Update Readme Add TransferHardwareData to frame.go Add SetHardwareDeviceContext to codec_context.go * Review changes add HardwareConfigs to codec create codec_hardware_config create codec_hardware_config_method_flag update example update flags better error handling in TransferHardwareData update CreateHardwareDeviceContext, better null handling remove hw ctx test because it fails when no nvidia is present some reordering of conss in hardware_device_type * Minor tweaks (review changes) HardwareConfigs only returns a slice an no error anymore remove if HardwareDeviceType checking in codec.go this should be done by user order codec hw_config_method_flags constants alphabetically some small changes in example ## hardware_device_context.go change link update optionsC
2024-01-25 01:23:12 +08:00
2024-01-25 01:29:35 +08:00
func (c *Codec) HardwareConfigs(dt HardwareDeviceType) (configs []CodecHardwareConfig) {
Implementation for HW Context (#32) * Adding HW Context * Check for hw pixel format and transfer data on ReciveFrame when its hw pixel format * Remove hw_decoding example and update demuxing_dec example * unref hw_device_ctx, and improve example Unref correctly hw_device_ctx on free decoding example has now a flag for hw decoding Use error on hw context because its good to know the error that happen Add a method to list supported hw codecs so user could check if system has a specif hwcodec before the create a hw context * Fix test to use err, and update hw with device to also return error * revert changes in demux example * Add/Modify according to review Seperate Hardware Device Context and Test Renaming HW to Hardware Create dedicated hardware_decoding Example Update Readme Add TransferHardwareData to frame.go Add SetHardwareDeviceContext to codec_context.go * Review changes add HardwareConfigs to codec create codec_hardware_config create codec_hardware_config_method_flag update example update flags better error handling in TransferHardwareData update CreateHardwareDeviceContext, better null handling remove hw ctx test because it fails when no nvidia is present some reordering of conss in hardware_device_type * Minor tweaks (review changes) HardwareConfigs only returns a slice an no error anymore remove if HardwareDeviceType checking in codec.go this should be done by user order codec hw_config_method_flags constants alphabetically some small changes in example ## hardware_device_context.go change link update optionsC
2024-01-25 01:23:12 +08:00
var i int
for {
config := C.avcodec_get_hw_config(c.c, C.int(i))
if config == nil {
break
}
configs = append(configs, CodecHardwareConfig{c: config})
i++
}
2024-01-25 01:29:35 +08:00
return
Implementation for HW Context (#32) * Adding HW Context * Check for hw pixel format and transfer data on ReciveFrame when its hw pixel format * Remove hw_decoding example and update demuxing_dec example * unref hw_device_ctx, and improve example Unref correctly hw_device_ctx on free decoding example has now a flag for hw decoding Use error on hw context because its good to know the error that happen Add a method to list supported hw codecs so user could check if system has a specif hwcodec before the create a hw context * Fix test to use err, and update hw with device to also return error * revert changes in demux example * Add/Modify according to review Seperate Hardware Device Context and Test Renaming HW to Hardware Create dedicated hardware_decoding Example Update Readme Add TransferHardwareData to frame.go Add SetHardwareDeviceContext to codec_context.go * Review changes add HardwareConfigs to codec create codec_hardware_config create codec_hardware_config_method_flag update example update flags better error handling in TransferHardwareData update CreateHardwareDeviceContext, better null handling remove hw ctx test because it fails when no nvidia is present some reordering of conss in hardware_device_type * Minor tweaks (review changes) HardwareConfigs only returns a slice an no error anymore remove if HardwareDeviceType checking in codec.go this should be done by user order codec hw_config_method_flags constants alphabetically some small changes in example ## hardware_device_context.go change link update optionsC
2024-01-25 01:23:12 +08:00
}