Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
Danile71 committed Oct 30, 2019
1 parent c93f15a commit bb246d9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 39 deletions.
19 changes: 9 additions & 10 deletions cgo/ffmpeg/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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++ {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
29 changes: 11 additions & 18 deletions cgo/ffmpeg/ffmpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@
#include <libswscale/swscale.h>
#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)
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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) ;
}
6 changes: 2 additions & 4 deletions cgo/ffmpeg/ffmpeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) ;
int decode(AVCodecContext *avctx, AVFrame *frame, uint8_t *data, int size, int *got_frame);
int avcodec_encode_jpeg(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket *packet);
14 changes: 7 additions & 7 deletions cgo/ffmpeg/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ 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)
err = fmt.Errorf("ffmpeg: decode failed: %d", cerr)
return
}

Expand All @@ -90,10 +90,10 @@ 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)
err = fmt.Errorf("ffmpeg: avcodec_encode_jpeg failed: %d", cerr)
return
}

Expand All @@ -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)
Expand All @@ -139,10 +139,10 @@ 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)
err = fmt.Errorf("ffmpeg: avcodec_encode_jpeg failed: %d", cerr)
return
}

Expand Down

0 comments on commit bb246d9

Please sign in to comment.