Skip to content

Commit

Permalink
Fix audio decode bug in LAVF muxer. Fixes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
nextghost committed Oct 28, 2014
1 parent 81c31ed commit abf4434
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#define MAXVIDEOSTREAMS (1)
#define MAXAVSTREAMS (MAXVIDEOSTREAMS+MAXAUDIOSTREAMS)
#define VIDEOSTREAM (MAXAUDIOSTREAMS)
#define MAX_AUDIO_FRAME_SIZE 192000

static inline int audiostream(int s=0)
{
Expand Down
42 changes: 39 additions & 3 deletions src/lavfmuxer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,43 @@ extern "C" {
#define guess_format av_guess_format
#define av_alloc_format_context avformat_alloc_context
#define CODEC_TYPE_AUDIO AVMEDIA_TYPE_AUDIO

int decode_audio3(AVCodecContext *avctx, int16_t *samples, int *frame_size_ptr, AVPacket *avpkt) {
AVFrame *frame = av_frame_alloc();
int ret, got_frame = 0;

if (!frame)
return AVERROR(ENOMEM);

ret = avcodec_decode_audio4(avctx, frame, &got_frame, avpkt);

if (ret >= 0 && got_frame) {
int ch, plane_size;
int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
frame->nb_samples,
avctx->sample_fmt, 1);
if (*frame_size_ptr < data_size) {
av_frame_free(&frame);
return AVERROR(EINVAL);
}

memcpy(samples, frame->extended_data[0], plane_size);

if (planar && avctx->channels > 1) {
uint8_t *out = ((uint8_t *)samples) + plane_size;
for (ch = 1; ch < avctx->channels; ch++) {
memcpy(out, frame->extended_data[ch], plane_size);
out += plane_size;
}
}
*frame_size_ptr = data_size;
} else {
*frame_size_ptr = 0;
}
av_frame_free(&frame);
return ret;
}
#endif

lavfmuxer::lavfmuxer(const char *format, uint32_t audiostreammask, mpgfile &mpg, const char *filename)
Expand Down Expand Up @@ -127,16 +164,15 @@ lavfmuxer::lavfmuxer(const char *format, uint32_t audiostreammask, mpgfile &mpg,
#else
if (!avcodec_open(s->codec, avcodec_find_decoder(s->codec->codec_id))) {
#endif
int16_t samples[AVCODEC_MAX_AUDIO_FRAME_SIZE/sizeof(int16_t)];
int16_t samples[MAX_AUDIO_FRAME_SIZE/sizeof(int16_t)];
int frame_size=sizeof(samples);
//fprintf(stderr, "** decode audio size=%d\n", sd->inbytes());
#if LIBAVCODEC_VERSION_INT >= ((53<<16)+(0<<8)+0)
AVPacket packet;
AVFrame frame;
av_init_packet(&packet);
packet.data = (uint8_t*)sd->getdata();
packet.size = sd->inbytes();
avcodec_decode_audio4(s->codec, &frame, &frame_size, &packet);
decode_audio3(s->codec, samples, &frame_size, &packet);
#elif LIBAVCODEC_VERSION_INT >= ((52<<16)+(0<<8)+0)
avcodec_decode_audio2
(s->codec,samples,&frame_size,
Expand Down

0 comments on commit abf4434

Please sign in to comment.