From 3c6c076e442acec45aabbf53cafa3d2b168fd252 Mon Sep 17 00:00:00 2001 From: nareix Date: Sun, 12 Jun 2016 16:07:28 +0800 Subject: [PATCH] split ffctx to single file --- audio.go | 50 +++++++++++--------------------------------------- ffmpeg.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 39 deletions(-) diff --git a/audio.go b/audio.go index 016463f..eb32e70 100644 --- a/audio.go +++ b/audio.go @@ -1,20 +1,17 @@ package ffmpeg +/* +#include "ffmpeg.h" +int wrap_avcodec_decode_audio4(AVCodecContext *ctx, AVFrame *frame, void *data, int size, int *got) { + struct AVPacket pkt = {.data = data, .size = size}; + return avcodec_decode_audio4(ctx, frame, got, &pkt); +} +int wrap_avresample_convert(AVAudioResampleContext *avr, int *out, int outsize, int outcount, int *in, int insize, int incount) { + return avresample_convert(avr, (void *)out, outsize, outcount, (void *)in, insize, incount); +} +*/ +import "C" import ( - /* - #include "ffmpeg.h" - - int wrap_avcodec_decode_audio4(AVCodecContext *ctx, AVFrame *frame, void *data, int size, int *got) { - struct AVPacket pkt = {.data = data, .size = size}; - return avcodec_decode_audio4(ctx, frame, got, &pkt); - } - - int wrap_avresample_convert(AVAudioResampleContext *avr, int *out, int outsize, int outcount, int *in, int insize, int incount) { - return avresample_convert(avr, (void *)out, outsize, outcount, (void *)in, insize, incount); - } - - */ - "C" "unsafe" "runtime" "fmt" @@ -24,10 +21,6 @@ import ( const debug = false -type ffctx struct { - ff C.FFCtx -} - type Resampler struct { inSampleFormat, OutSampleFormat av.SampleFormat inChannelLayout, OutChannelLayout av.ChannelLayout @@ -546,27 +539,6 @@ func HasDecoder(name string) bool { //func EncodersList() []string //func DecodersList() []string -func newFFCtxByCodec(codec *C.AVCodec) (ff *ffctx, err error) { - ff = &ffctx{} - ff.ff.codec = codec - ff.ff.codecCtx = C.avcodec_alloc_context3(codec) - runtime.SetFinalizer(ff, freeFFCtx) - return -} - -func freeFFCtx(self *ffctx) { - ff := &self.ff - if ff.frame != nil { - C.av_frame_free(&ff.frame) - ff.frame = nil - } - if ff.codecCtx != nil { - C.avcodec_close(ff.codecCtx) - C.av_free(unsafe.Pointer(ff.codecCtx)) - ff.codecCtx = nil - } -} - func NewAudioEncoderByCodecType(typ int) (enc *AudioEncoder, err error) { var id uint32 diff --git a/ffmpeg.go b/ffmpeg.go index 04f596c..d217f03 100644 --- a/ffmpeg.go +++ b/ffmpeg.go @@ -8,6 +8,10 @@ void ffinit() { } */ import "C" +import ( + "runtime" + "unsafe" +) const ( QUIET = int(C.AV_LOG_QUIET) @@ -29,3 +33,28 @@ func init() { C.ffinit() } +type ffctx struct { + ff C.FFCtx +} + +func newFFCtxByCodec(codec *C.AVCodec) (ff *ffctx, err error) { + ff = &ffctx{} + ff.ff.codec = codec + ff.ff.codecCtx = C.avcodec_alloc_context3(codec) + runtime.SetFinalizer(ff, freeFFCtx) + return +} + +func freeFFCtx(self *ffctx) { + ff := &self.ff + if ff.frame != nil { + C.av_frame_free(&ff.frame) + ff.frame = nil + } + if ff.codecCtx != nil { + C.avcodec_close(ff.codecCtx) + C.av_free(unsafe.Pointer(ff.codecCtx)) + ff.codecCtx = nil + } +} +