History

avconv: only allocate 1 AVFrame per input stream

This avoids allocating/freeing an AVFrame for each packet decode. It also simplifies error handling in transcode_audio() and transcode_video().

Justin Ruggles committed 14 years ago

Commit 9179f271 parent 110d2af1 file · +21 −16Browse files

Unified · Split

avconv.c+21−16
@@ -145,6 +145,8 @@ typedef struct InputStream { · 144 unmodified lines
145145 int discard; /* true if stream data should be discarded */
146146 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
147147 AVCodec *dec;
148+ AVFrame *decoded_frame;
149+ AVFrame *filtered_frame;
148150
149151 int64_t start; /* time when read started */
150152 int64_t next_pts; /* synthetic pts for cases where pkt.pts
@@ -523,8 +525,11 @@ void exit_program(int ret) · 372 unmodified lines
523525 for(i=0;i<nb_input_files;i++) {
524526 av_close_input_file(input_files[i].ctx);
525527 }
526− for (i = 0; i < nb_input_streams; i++)
528+ for (i = 0; i < nb_input_streams; i++) {
529+ av_freep(&input_streams[i].decoded_frame);
530+ av_freep(&input_streams[i].filtered_frame);
527531 av_dict_free(&input_streams[i].opts);
532+ }
528533
529534 if (vstats_file)
530535 fclose(vstats_file);
@@ -1622,18 +1627,19 @@ static int transcode_audio(InputStream *ist, AVPacket *pkt, int *got_output) · 1091 unmodified lines
16221627 int bps = av_get_bytes_per_sample(ist->st->codec->sample_fmt);
16231628 int i, ret;
16241629
1625− if (!(decoded_frame = avcodec_alloc_frame()))
1630+ if (!ist->decoded_frame && !(ist->decoded_frame = avcodec_alloc_frame()))
16261631 return AVERROR(ENOMEM);
1632+ else
1633+ avcodec_get_frame_defaults(ist->decoded_frame);
1634+ decoded_frame = ist->decoded_frame;
16271635
16281636 ret = avcodec_decode_audio4(avctx, decoded_frame, got_output, pkt);
16291637 if (ret < 0) {
1630− av_freep(&decoded_frame);
16311638 return ret;
16321639 }
16331640
16341641 if (!*got_output) {
16351642 /* no audio frame */
1636− av_freep(&decoded_frame);
16371643 return ret;
16381644 }
16391645
@@ -1701,7 +1707,6 @@ static int transcode_audio(InputStream *ist, AVPacket *pkt, int *got_output) · 61 unmodified lines
17011707 av_log(NULL, AV_LOG_FATAL,
17021708 "Audio volume adjustment on sample format %s is not supported.\n",
17031709 av_get_sample_fmt_name(ist->st->codec->sample_fmt));
1704− av_freep(&decoded_frame);
17051710 exit_program(1);
17061711 }
17071712 }
@@ -1716,7 +1721,6 @@ static int transcode_audio(InputStream *ist, AVPacket *pkt, int *got_output) · 8 unmodified lines
17161721 do_audio_out(output_files[ost->file_index].ctx, ost, ist, decoded_frame);
17171722 }
17181723
1719− av_freep(&decoded_frame);
17201724 return ret;
17211725 }
17221726
@@ -1730,8 +1734,11 @@ static int transcode_video(InputStream *ist, AVPacket *pkt, int *got_output, int · 7 unmodified lines
17301734 int frame_available = 1;
17311735 #endif
17321736
1733− if (!(decoded_frame = avcodec_alloc_frame()))
1737+ if (!ist->decoded_frame && !(ist->decoded_frame = avcodec_alloc_frame()))
17341738 return AVERROR(ENOMEM);
1739+ else
1740+ avcodec_get_frame_defaults(ist->decoded_frame);
1741+ decoded_frame = ist->decoded_frame;
17351742 pkt->pts = *pkt_pts;
17361743 pkt->dts = ist->pts;
17371744 *pkt_pts = AV_NOPTS_VALUE;
@@ -1739,12 +1746,11 @@ static int transcode_video(InputStream *ist, AVPacket *pkt, int *got_output, int · 1 unmodified line
17391746 ret = avcodec_decode_video2(ist->st->codec,
17401747 decoded_frame, got_output, pkt);
17411748 if (ret < 0)
1742− goto fail;
1749+ return ret;
17431750
17441751 quality = same_quant ? decoded_frame->quality : 0;
17451752 if (!*got_output) {
17461753 /* no picture yet */
1747− av_freep(&decoded_frame);
17481754 return ret;
17491755 }
17501756 ist->next_pts = ist->pts = guess_correct_pts(&ist->pts_ctx, decoded_frame->pkt_pts,
@@ -1778,10 +1784,12 @@ static int transcode_video(InputStream *ist, AVPacket *pkt, int *got_output, int · 27 unmodified lines
17781784 else
17791785 sar = ist->st->codec->sample_aspect_ratio;
17801786 av_vsrc_buffer_add_frame(ost->input_video_filter, decoded_frame, ist->pts, sar);
1781− if (!(filtered_frame = avcodec_alloc_frame())) {
1782− ret = AVERROR(ENOMEM);
1783− goto fail;
1784− }
1787+ if (!ist->filtered_frame && !(ist->filtered_frame = avcodec_alloc_frame())) {
1788+ av_free(buffer_to_free);
1789+ return AVERROR(ENOMEM);
1790+ } else
1791+ avcodec_get_frame_defaults(ist->filtered_frame);
1792+ filtered_frame = ist->filtered_frame;
17851793 frame_available = avfilter_poll_frame(ost->output_video_filter->inputs[0]);
17861794 }
17871795 while (frame_available) {
@@ -1805,13 +1813,10 @@ static int transcode_video(InputStream *ist, AVPacket *pkt, int *got_output, int · 17 unmodified lines
18051813 if (ost->picref)
18061814 avfilter_unref_buffer(ost->picref);
18071815 }
1808− av_freep(&filtered_frame);
18091816 #endif
18101817 }
18111818
1812−fail:
18131819 av_free(buffer_to_free);
1814− av_freep(&decoded_frame);
18151820 return ret;
18161821 }
18171822

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t