avconv: split audio transcoding out of output_packet().

This commit is contained in:
Anton Khirnov 2011-11-21 14:39:22 +01:00
parent 78162b4ea2
commit ded28ba35b
1 changed files with 114 additions and 95 deletions

251
avconv.c
View File

@ -1615,136 +1615,41 @@ static void rate_emu_sleep(InputStream *ist)
}
}
/* pkt = NULL means EOF (needed to flush decoder buffers) */
static int output_packet(InputStream *ist, int ist_index,
OutputStream *ost_table, int nb_ostreams,
const AVPacket *pkt)
static int transcode_audio(InputStream *ist, AVPacket *pkt, int *got_output)
{
AVFormatContext *os;
OutputStream *ost;
int ret = 0, i;
int got_output;
void *buffer_to_free = NULL;
static unsigned int samples_size= 0;
AVSubtitle subtitle, *subtitle_to_free;
int64_t pkt_pts = AV_NOPTS_VALUE;
#if CONFIG_AVFILTER
int frame_available;
#endif
float quality;
AVPacket avpkt;
static unsigned int samples_size = 0;
int bps = av_get_bytes_per_sample(ist->st->codec->sample_fmt);
uint8_t *decoded_data_buf = NULL;
int decoded_data_size = 0;
int i, ret;
if(ist->next_pts == AV_NOPTS_VALUE)
ist->next_pts= ist->pts;
if (pkt == NULL) {
/* EOF handling */
av_init_packet(&avpkt);
avpkt.data = NULL;
avpkt.size = 0;
goto handle_eof;
} else {
avpkt = *pkt;
}
if(pkt->dts != AV_NOPTS_VALUE)
ist->next_pts = ist->pts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
if(pkt->pts != AV_NOPTS_VALUE)
pkt_pts = av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q);
//while we have more to decode or while the decoder did output something on EOF
while (ist->decoding_needed && (avpkt.size > 0 || (!pkt && got_output))) {
uint8_t *decoded_data_buf;
int decoded_data_size;
AVFrame *decoded_frame, *filtered_frame;
handle_eof:
ist->pts= ist->next_pts;
if(avpkt.size && avpkt.size != pkt->size)
av_log(NULL, ist->showed_multi_packet_warning ? AV_LOG_VERBOSE : AV_LOG_WARNING,
"Multiple frames in a packet from stream %d\n", pkt->stream_index);
ist->showed_multi_packet_warning=1;
/* decode the packet if needed */
decoded_frame = filtered_frame = NULL;
decoded_data_buf = NULL; /* fail safe */
decoded_data_size= 0;
subtitle_to_free = NULL;
switch(ist->st->codec->codec_type) {
case AVMEDIA_TYPE_AUDIO:{
if(pkt && samples_size < FFMAX(pkt->size * bps, AVCODEC_MAX_AUDIO_FRAME_SIZE)) {
samples_size = FFMAX(pkt->size * bps, AVCODEC_MAX_AUDIO_FRAME_SIZE);
if (pkt && samples_size < FFMAX(pkt->size * bps, AVCODEC_MAX_AUDIO_FRAME_SIZE)) {
av_free(samples);
samples= av_malloc(samples_size);
samples_size = FFMAX(pkt->size * bps, AVCODEC_MAX_AUDIO_FRAME_SIZE);
samples = av_malloc(samples_size);
}
decoded_data_size= samples_size;
/* XXX: could avoid copy if PCM 16 bits with same
endianness as CPU */
decoded_data_size = samples_size;
ret = avcodec_decode_audio3(ist->st->codec, samples, &decoded_data_size,
&avpkt);
pkt);
if (ret < 0)
return ret;
avpkt.data += ret;
avpkt.size -= ret;
got_output = decoded_data_size > 0;
pkt->data += ret;
pkt->size -= ret;
*got_output = decoded_data_size > 0;
/* Some bug in mpeg audio decoder gives */
/* decoded_data_size < 0, it seems they are overflows */
if (!got_output) {
if (!*got_output) {
/* no audio frame */
continue;
return 0;
}
decoded_data_buf = (uint8_t *)samples;
ist->next_pts += ((int64_t)AV_TIME_BASE/bps * decoded_data_size) /
(ist->st->codec->sample_rate * ist->st->codec->channels);
break;}
case AVMEDIA_TYPE_VIDEO:
if (!(decoded_frame = avcodec_alloc_frame()))
return AVERROR(ENOMEM);
avpkt.pts = pkt_pts;
avpkt.dts = ist->pts;
pkt_pts = AV_NOPTS_VALUE;
ret = avcodec_decode_video2(ist->st->codec,
decoded_frame, &got_output, &avpkt);
quality = same_quant ? decoded_frame->quality : 0;
if (ret < 0)
goto fail;
if (!got_output) {
/* no picture yet */
av_freep(&decoded_frame);
goto discard_packet;
}
ist->next_pts = ist->pts = guess_correct_pts(&ist->pts_ctx, decoded_frame->pkt_pts,
decoded_frame->pkt_dts);
if (ist->st->codec->time_base.num != 0) {
int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;
ist->next_pts += ((int64_t)AV_TIME_BASE *
ist->st->codec->time_base.num * ticks) /
ist->st->codec->time_base.den;
}
avpkt.size = 0;
buffer_to_free = NULL;
pre_process_video_frame(ist, (AVPicture *)decoded_frame, &buffer_to_free);
break;
case AVMEDIA_TYPE_SUBTITLE:
ret = avcodec_decode_subtitle2(ist->st->codec,
&subtitle, &got_output, &avpkt);
if (ret < 0)
return ret;
if (!got_output) {
goto discard_packet;
}
subtitle_to_free = &subtitle;
avpkt.size = 0;
break;
default:
return -1;
}
// preprocess audio (volume)
if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
if (audio_volume != 256) {
switch (ist->st->codec->sample_fmt) {
case AV_SAMPLE_FMT_U8:
@ -1799,6 +1704,123 @@ static int output_packet(InputStream *ist, int ist_index,
exit_program(1);
}
}
rate_emu_sleep(ist);
for (i = 0; i < nb_output_streams; i++) {
OutputStream *ost = &output_streams[i];
if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
continue;
do_audio_out(output_files[ost->file_index].ctx, ost, ist,
decoded_data_buf, decoded_data_size);
}
return 0;
}
/* pkt = NULL means EOF (needed to flush decoder buffers) */
static int output_packet(InputStream *ist, int ist_index,
OutputStream *ost_table, int nb_ostreams,
const AVPacket *pkt)
{
AVFormatContext *os;
OutputStream *ost;
int ret = 0, i;
int got_output;
void *buffer_to_free = NULL;
AVSubtitle subtitle, *subtitle_to_free;
int64_t pkt_pts = AV_NOPTS_VALUE;
#if CONFIG_AVFILTER
int frame_available;
#endif
float quality;
AVPacket avpkt;
if(ist->next_pts == AV_NOPTS_VALUE)
ist->next_pts= ist->pts;
if (pkt == NULL) {
/* EOF handling */
av_init_packet(&avpkt);
avpkt.data = NULL;
avpkt.size = 0;
goto handle_eof;
} else {
avpkt = *pkt;
}
if(pkt->dts != AV_NOPTS_VALUE)
ist->next_pts = ist->pts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
if(pkt->pts != AV_NOPTS_VALUE)
pkt_pts = av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q);
//while we have more to decode or while the decoder did output something on EOF
while (ist->decoding_needed && (avpkt.size > 0 || (!pkt && got_output))) {
AVFrame *decoded_frame, *filtered_frame;
handle_eof:
ist->pts= ist->next_pts;
if(avpkt.size && avpkt.size != pkt->size)
av_log(NULL, ist->showed_multi_packet_warning ? AV_LOG_VERBOSE : AV_LOG_WARNING,
"Multiple frames in a packet from stream %d\n", pkt->stream_index);
ist->showed_multi_packet_warning=1;
// XXX temporary hack, will be turned to a switch() once all codec
// types are split out
if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
ret = transcode_audio(ist, &avpkt, &got_output);
if (ret < 0)
return ret;
continue;
}
/* decode the packet if needed */
decoded_frame = filtered_frame = NULL;
subtitle_to_free = NULL;
switch(ist->st->codec->codec_type) {
case AVMEDIA_TYPE_VIDEO:
if (!(decoded_frame = avcodec_alloc_frame()))
return AVERROR(ENOMEM);
avpkt.pts = pkt_pts;
avpkt.dts = ist->pts;
pkt_pts = AV_NOPTS_VALUE;
ret = avcodec_decode_video2(ist->st->codec,
decoded_frame, &got_output, &avpkt);
quality = same_quant ? decoded_frame->quality : 0;
if (ret < 0)
goto fail;
if (!got_output) {
/* no picture yet */
av_freep(&decoded_frame);
goto discard_packet;
}
ist->next_pts = ist->pts = guess_correct_pts(&ist->pts_ctx, decoded_frame->pkt_pts,
decoded_frame->pkt_dts);
if (ist->st->codec->time_base.num != 0) {
int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;
ist->next_pts += ((int64_t)AV_TIME_BASE *
ist->st->codec->time_base.num * ticks) /
ist->st->codec->time_base.den;
}
avpkt.size = 0;
buffer_to_free = NULL;
pre_process_video_frame(ist, (AVPicture *)decoded_frame, &buffer_to_free);
break;
case AVMEDIA_TYPE_SUBTITLE:
ret = avcodec_decode_subtitle2(ist->st->codec,
&subtitle, &got_output, &avpkt);
if (ret < 0)
return ret;
if (!got_output) {
goto discard_packet;
}
subtitle_to_free = &subtitle;
avpkt.size = 0;
break;
default:
return -1;
}
/* frame rate emulation */
@ -1846,9 +1868,6 @@ static int output_packet(InputStream *ist, int ist_index,
av_assert0(ist->decoding_needed);
switch(ost->st->codec->codec_type) {
case AVMEDIA_TYPE_AUDIO:
do_audio_out(os, ost, ist, decoded_data_buf, decoded_data_size);
break;
case AVMEDIA_TYPE_VIDEO:
#if CONFIG_AVFILTER
if (ost->picref->video && !ost->frame_aspect_ratio)