Skip to content

Commit

Permalink
Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
Danile71 committed Aug 30, 2019
1 parent 7175d41 commit 19b4e91
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 65 deletions.
54 changes: 2 additions & 52 deletions cgo/ffmpeg/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,7 @@ package ffmpeg
/*
#include "ffmpeg.h"
int wrap_avcodec_decode_audio4(AVCodecContext *avctx, AVFrame *frame,void *data, int size, int *got_frame)
{
int ret;
struct AVPacket pkt = {.data = data, .size = size};
*got_frame = 0;
if (data) {
ret = avcodec_send_packet(avctx, &pkt);
// In particular, we don't expect AVERROR(EAGAIN), because we read all
// decoded frames with avcodec_receive_frame() until done.
if (ret < 0)
return ret == AVERROR_EOF ? 0 : ret;
}
ret = avcodec_receive_frame(avctx, frame);
if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
return ret;
if (ret >= 0)
*got_frame = 1;
return 0;
}
int wrap_avcodec_encode_audio2(AVCodecContext *avctx,AVPacket *pkt, AVFrame *frame, int *got_frame)
{
int ret;
*got_frame = 0;
if (pkt) {
ret = avcodec_send_packet(avctx, pkt);
// In particular, we don't expect AVERROR(EAGAIN), because we read all
// decoded frames with avcodec_receive_frame() until done.
if (ret < 0)
return ret == AVERROR_EOF ? 0 : ret;
}
ret = avcodec_receive_frame(avctx, frame);
if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
return ret;
if (ret >= 0)
*got_frame = 1;
return 0;
}
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 (
Expand Down Expand Up @@ -414,7 +364,7 @@ func (self *AudioEncoder) encodeOne(frame av.AudioFrame) (gotpkt bool, pkt []byt
}
fmt.Println(farr)
}
cerr := C.wrap_avcodec_encode_audio2(ff.codecCtx, &cpkt, ff.frame, &cgotpkt)
cerr := C.encode(ff.codecCtx, &cpkt, &cgotpkt, ff.frame)
if cerr < C.int(0) {
err = fmt.Errorf("ffmpeg: avcodec_encode_audio2 failed: %d", cerr)
return
Expand Down Expand Up @@ -635,7 +585,7 @@ func (self *AudioDecoder) Decode(pkt []byte) (gotframe bool, frame av.AudioFrame
ff := &self.ff.ff

cgotframe := C.int(0)
cerr := C.wrap_avcodec_decode_audio4(ff.codecCtx, ff.frame, unsafe.Pointer(&pkt[0]), C.int(len(pkt)), &cgotframe)
cerr := C.wrap_avcodec_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)
return
Expand Down
41 changes: 35 additions & 6 deletions cgo/ffmpeg/ffmpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
#include <libswscale/swscale.h>
#include "ffmpeg.h"

int wrap_avcodec_decode_video2(AVCodecContext *avctx, AVFrame *frame,uint8_t *data, int size, int *got_frame)
int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
{
int ret;
struct AVPacket pkt = {.data = data, .size = size};

*got_frame = 0;

if (data) {
ret = avcodec_send_packet(avctx, &pkt);
if (pkt) {
ret = avcodec_send_packet(avctx, pkt);
// In particular, we don't expect AVERROR(EAGAIN), because we read all
// decoded frames with avcodec_receive_frame() until done.
if (ret < 0)
Expand All @@ -31,6 +30,32 @@ int wrap_avcodec_decode_video2(AVCodecContext *avctx, AVFrame *frame,uint8_t *da
return 0;
}

int encode(AVCodecContext *avctx, AVPacket *pkt, int *got_packet, AVFrame *frame)
{
int ret;

*got_packet = 0;

ret = avcodec_send_frame(avctx, frame);
if (ret < 0)
return ret;

ret = avcodec_receive_packet(avctx, pkt);
if (!ret)
*got_packet = 1;
if (ret == AVERROR(EAGAIN))
return 0;

return ret;
}


int wrap_avcodec_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) {
AVCodec *jpegCodec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);

Expand All @@ -56,12 +81,16 @@ int wrap_avcodec_encode_jpeg(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket
}

int gotFrame;

if (avcodec_encode_video2(jpegContext, packet, pFrame, &gotFrame) < 0) {
if (encode(jpegContext, packet, &gotFrame, pFrame) < 0) {
avcodec_close(jpegContext);
return -1;
}

avcodec_close(jpegContext);
return 0;
}

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);
}
8 changes: 5 additions & 3 deletions cgo/ffmpeg/ffmpeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ static inline int avcodec_profile_name_to_int(AVCodec *codec, const char *name)
return p->profile;
return FF_PROFILE_UNKNOWN;
}

int wrap_avcodec_decode_video2(AVCodecContext *avctx, AVFrame *frame,uint8_t *data, int size, int *got_frame);
int wrap_avcodec_encode_jpeg(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket *packet);
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_decode(AVCodecContext *avctx, AVFrame *frame,uint8_t *data, int size, int *got_frame);
int wrap_avcodec_encode_jpeg(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket *packet);
int wrap_avresample_convert(AVAudioResampleContext *avr, int *out, int outsize, int outcount, int *in, int insize, int incount);
6 changes: 2 additions & 4 deletions cgo/ffmpeg/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (self *VideoDecoder) Decode(pkt []byte) (img *VideoFrame, err error) {

cgotimg := C.int(0)
frame := C.av_frame_alloc()
cerr := C.wrap_avcodec_decode_video2(ff.codecCtx, frame, (*C.uchar)(unsafe.Pointer(&pkt[0])), C.int(len(pkt)), &cgotimg)
cerr := C.wrap_avcodec_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)
return
Expand All @@ -84,9 +84,7 @@ func (self *VideoDecoder) Decode(pkt []byte) (img *VideoFrame, err error) {
}, frame: frame}
runtime.SetFinalizer(img, freeVideoFrame)

var packet C.AVPacket
C.av_init_packet(&packet)
defer C.av_free_packet(&packet)
packet := C.AVPacket{}

err := C.wrap_avcodec_encode_jpeg(ff.codecCtx, frame, &packet)

Expand Down

0 comments on commit 19b4e91

Please sign in to comment.