diff --git a/cgo/ffmpeg/audio.go b/cgo/ffmpeg/audio.go index c76a5805..eef1c5e2 100644 --- a/cgo/ffmpeg/audio.go +++ b/cgo/ffmpeg/audio.go @@ -49,11 +49,12 @@ func (self *Resampler) Resample(in av.AudioFrame) (out av.AudioFrame, err error) flush.SampleFormat = self.OutSampleFormat flush.SampleRate = self.OutSampleRate - convertSamples := int(C.wrap_swresample_convert( + convertSamples := int(C.swr_convert( self.avr, - (*C.int)(unsafe.Pointer(&outData[0])), C.int(outLinesize), C.int(outSampleCount), - nil, C.int(0), C.int(0), + (**C.uint8_t)(unsafe.Pointer(&outData[0])), C.int(outSampleCount), + nil, C.int(0), )) + if convertSamples < 0 { err = fmt.Errorf("ffmpeg: avresample_convert_frame failed") return @@ -87,14 +88,12 @@ func (self *Resampler) Resample(in av.AudioFrame) (out av.AudioFrame, err error) self.avr = avr } - var inChannels, inLinesize int + var inChannels int inSampleCount := in.SampleCount if !self.inSampleFormat.IsPlanar() { inChannels = 1 - inLinesize = inSampleCount * in.SampleFormat.BytesPerSample() * self.inChannelLayout.Count() } else { inChannels = self.inChannelLayout.Count() - inLinesize = inSampleCount * in.SampleFormat.BytesPerSample() } inData := make([]*C.uint8_t, inChannels) for i := 0; i < inChannels; i++ { @@ -122,10 +121,10 @@ func (self *Resampler) Resample(in av.AudioFrame) (out av.AudioFrame, err error) out.SampleFormat = self.OutSampleFormat out.SampleRate = self.OutSampleRate - convertSamples := int(C.wrap_swresample_convert( + convertSamples := int(C.swr_convert( self.avr, - (*C.int)(unsafe.Pointer(&outData[0])), C.int(outLinesize), C.int(outSampleCount), - (*C.int)(unsafe.Pointer(&inData[0])), C.int(inLinesize), C.int(inSampleCount), + (**C.uint8_t)(unsafe.Pointer(&outData[0])), C.int(outSampleCount), + (**C.uint8_t)(unsafe.Pointer(&inData[0])), C.int(inSampleCount), )) if convertSamples < 0 { err = fmt.Errorf("ffmpeg: avresample_convert_frame failed") @@ -586,7 +585,7 @@ func (self *AudioDecoder) Decode(pkt []byte) (gotframe bool, frame av.AudioFrame cgotframe := C.int(0) - cerr := C.wrap_decode(ff.codecCtx, ff.frame, (*C.uchar)(unsafe.Pointer(&pkt[0])), C.int(len(pkt)), &cgotframe) + cerr := C.decode(ff.codecCtx, ff.frame, (*C.uchar)(unsafe.Pointer(&pkt[0])), C.int(len(pkt)), &cgotframe) if cerr < C.int(0) { err = fmt.Errorf("ffmpeg: avcodec_decode_audio4 failed: %d", cerr) diff --git a/cgo/ffmpeg/ffmpeg.c b/cgo/ffmpeg/ffmpeg.c index 4319c198..6d3bff83 100644 --- a/cgo/ffmpeg/ffmpeg.c +++ b/cgo/ffmpeg/ffmpeg.c @@ -7,19 +7,20 @@ #include #include "ffmpeg.h" -int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt) +int decode(AVCodecContext *avctx, AVFrame *frame, uint8_t *data, int size, int *got_frame) { int ret; + struct AVPacket pkt = {.data = data, .size = size}; *got_frame = 0; - - if (pkt) { - ret = avcodec_send_packet(avctx, pkt); - av_packet_unref(pkt); - - if (ret < 0) - return ret == AVERROR_EOF ? 0 : ret; - } + + ret = avcodec_send_packet(avctx, &pkt); + + av_packet_unref(&pkt); + + if (ret < 0) + return ret == AVERROR_EOF ? 0 : ret; + ret = avcodec_receive_frame(avctx, frame); if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) @@ -50,13 +51,8 @@ int encode(AVCodecContext *avctx, AVPacket *pkt, int *got_packet, AVFrame *frame } -int wrap_decode(AVCodecContext *avctx, AVFrame *frame,uint8_t *data, int size, int *got_frame) -{ - struct AVPacket pkt = {.data = data, .size = size}; - return decode(avctx, frame, got_frame,&pkt); -} -int wrap_avcodec_encode_jpeg(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket *packet) { +int avcodec_encode_jpeg(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket *packet) { AVCodec *jpegCodec = avcodec_find_encoder(AV_CODEC_ID_MJPEG); int ret = -1; @@ -95,6 +91,3 @@ int wrap_avcodec_encode_jpeg(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket return ret; } -int wrap_swresample_convert(SwrContext *avr, int *out, int outsize, int outcount, int *in, int insize, int incount) { - return swr_convert(avr, (uint8_t **)out, outcount, (const uint8_t **)out, incount) ; -} diff --git a/cgo/ffmpeg/ffmpeg.h b/cgo/ffmpeg/ffmpeg.h index aabda3c1..a5511c87 100644 --- a/cgo/ffmpeg/ffmpeg.h +++ b/cgo/ffmpeg/ffmpeg.h @@ -23,7 +23,5 @@ static inline int avcodec_profile_name_to_int(AVCodec *codec, const char *name) return FF_PROFILE_UNKNOWN; } int encode(AVCodecContext *avctx, AVPacket *pkt, int *got_packet, AVFrame *frame); -int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt); -int wrap_avcodec_encode_jpeg(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket *packet); -int wrap_decode(AVCodecContext *avctx, AVFrame *frame,uint8_t *data, int size, int *got_frame); -int wrap_swresample_convert(SwrContext *avr, int *out, int outsize, int outcount, int *in, int insize, int incount) ; \ No newline at end of file +int decode(AVCodecContext *avctx, AVFrame *frame, uint8_t *data, int size, int *got_frame); +int avcodec_encode_jpeg(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket *packet); diff --git a/cgo/ffmpeg/video.go b/cgo/ffmpeg/video.go index d5ddd06b..31932ffe 100644 --- a/cgo/ffmpeg/video.go +++ b/cgo/ffmpeg/video.go @@ -62,7 +62,7 @@ func (self *VideoDecoder) Decode(pkt []byte) (img *VideoFrame, err error) { frame := C.av_frame_alloc() defer C.av_frame_free(&frame) - cerr := C.wrap_decode(ff.codecCtx, frame, (*C.uchar)(unsafe.Pointer(&pkt[0])), C.int(len(pkt)), &cgotimg) + cerr := C.decode(ff.codecCtx, frame, (*C.uchar)(unsafe.Pointer(&pkt[0])), C.int(len(pkt)), &cgotimg) if cerr < C.int(0) { err = fmt.Errorf("ffmpeg: avcodec_decode_video2 failed: %d", cerr) @@ -90,7 +90,7 @@ func (self *VideoDecoder) Decode(pkt []byte) (img *VideoFrame, err error) { packet := C.AVPacket{} defer C.av_packet_unref(&packet) - cerr := C.wrap_avcodec_encode_jpeg(ff.codecCtx, frame, &packet) + cerr := C.avcodec_encode_jpeg(ff.codecCtx, frame, &packet) if cerr != C.int(0) { err = fmt.Errorf("ffmpeg: avcodec_decode_video2 failed: %d", cerr) @@ -112,7 +112,7 @@ func (self *VideoDecoder) DecodeBac(pkt []byte) (img *VideoFrame, err error) { frame := C.av_frame_alloc() defer C.av_frame_free(&frame) - cerr := C.wrap_decode(ff.codecCtx, frame, (*C.uchar)(unsafe.Pointer(&pkt[0])), C.int(len(pkt)), &cgotimg) + cerr := C.decode(ff.codecCtx, frame, (*C.uchar)(unsafe.Pointer(&pkt[0])), C.int(len(pkt)), &cgotimg) if cerr < C.int(0) { err = fmt.Errorf("ffmpeg: avcodec_decode_video2 failed: %d", cerr) @@ -139,7 +139,7 @@ func (self *VideoDecoder) DecodeBac(pkt []byte) (img *VideoFrame, err error) { packet := C.AVPacket{} defer C.av_packet_unref(&packet) - cerr := C.wrap_avcodec_encode_jpeg(ff.codecCtx, frame, &packet) + cerr := C.avcodec_encode_jpeg(ff.codecCtx, frame, &packet) if cerr != C.int(0) { err = fmt.Errorf("ffmpeg: avcodec_decode_video2 failed: %d", cerr)