Skip to content

Commit

Permalink
fixed audio decode length wrong.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackarain committed May 14, 2012
1 parent 379a6c8 commit 4089b6f
Showing 1 changed file with 15 additions and 38 deletions.
53 changes: 15 additions & 38 deletions libav/src/avplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -948,46 +948,15 @@ void destory(avplay *play)
static
void audio_copy(avplay *play, AVFrame *dst, AVFrame* src)
{
int src_size = 0;
*dst = *src;
dst->data[0] = (uint8_t*) av_malloc(src->linesize[0]);
dst->linesize[0] = src->linesize[0];
dst->type = 0;
assert(dst->data[0]);

src_size = av_samples_get_buffer_size(NULL,
play->m_audio_ctx->channels,
src->nb_samples,
play->m_audio_ctx->sample_fmt, 1);

/*转换音频格式.*/
if (play->m_audio_ctx->sample_fmt != AV_SAMPLE_FMT_S16)
{
if (!play->m_audio_convert_ctx)
play->m_audio_convert_ctx = av_audio_convert_alloc(
AV_SAMPLE_FMT_S16, 1, play->m_audio_ctx->sample_fmt, 1,
NULL, 0);
if (play->m_audio_convert_ctx)
{
const void *ibuf[6] = { src->data[0] };
void *obuf[6] = { dst->data[0] };
int istride[6] = { av_get_bytes_per_sample(play->m_audio_ctx->sample_fmt) };
int ostride[6] = { 2 };
int len = src_size / istride[0];
if (av_audio_convert(play->m_audio_convert_ctx, obuf, ostride, ibuf,
istride, len) < 0)
{
assert(0);
}
src_size = len * 2;
memcpy(src->data[0], dst->data[0], src_size);
/* FIXME: existing code assume that data_size equals framesize*channels*2
remove this legacy cruft */
}
}

/* 重采样到双声道. */
if (play->m_audio_ctx->channels > 2)
if (play->m_audio_ctx->channels > 2 ||
play->m_audio_ctx->sample_fmt != AV_SAMPLE_FMT_S16)
{
if (!play->m_resample_ctx)
{
Expand All @@ -1000,16 +969,17 @@ void audio_copy(avplay *play, AVFrame *dst, AVFrame* src)

if (play->m_resample_ctx)
{
int samples = src_size / (2 * play->m_audio_ctx->channels);
int samples = src->linesize[0] / (av_get_bytes_per_sample(play->m_audio_ctx->sample_fmt)
* play->m_audio_ctx->channels);
dst->linesize[0] = audio_resample(play->m_resample_ctx,
(short *) dst->data[0], (short *) src->data[0], samples)
* 4;
}
}
else
{
dst->linesize[0] = src_size;
memcpy(dst->data[0], src->data[0], src_size);
dst->linesize[0] = dst->linesize[0];
memcpy(dst->data[0], src->data[0], dst->linesize[0]);
}
}

Expand Down Expand Up @@ -1297,13 +1267,17 @@ void* audio_dec_thrd(void *param)
avplay *play = (avplay*) param;
int64_t v_start_time = 0;
int64_t a_start_time = 0;
uint8_t* audio_buf;

if (play->m_video_st && play->m_audio_st)
{
v_start_time = play->m_video_st->start_time;
a_start_time = play->m_audio_st->start_time;
}

avframe.data[0] = av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3);
audio_buf = avframe.data[0];

for (; !play->m_abort;)
{
av_init_packet(&pkt);
Expand Down Expand Up @@ -1338,11 +1312,12 @@ void* audio_dec_thrd(void *param)
/* 解码音频. */
out_size = 0;
pkt2 = pkt;
avframe.linesize[0] = 0;
while (!play->m_abort)
{
int audio_out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
ret = avcodec_decode_audio4(play->m_audio_ctx, &avframe,
&audio_out_size, &pkt2);
ret = avcodec_decode_audio3(play->m_audio_ctx,
(int16_t*)((uint8_t*)audio_buf + out_size), &audio_out_size, &pkt2);
if (ret < 0)
{
printf("Audio error while decoding one frame!!!\n");
Expand All @@ -1360,6 +1335,7 @@ void* audio_dec_thrd(void *param)
{
avframe.pts = pkt2.pts;
avframe.best_effort_timestamp = pkt2.pts;
avframe.linesize[0] = out_size;

/* 拷贝到avcopy用于添加到队列. */
audio_copy(play, &avcopy, &avframe);
Expand Down Expand Up @@ -1390,6 +1366,7 @@ void* audio_dec_thrd(void *param)
av_free_packet(&pkt);
}
}
av_free(audio_buf);
return NULL;
}

Expand Down

0 comments on commit 4089b6f

Please sign in to comment.