This avoids allocating/freeing an AVFrame for each packet decode. It also simplifies error handling in transcode_audio() and transcode_video().
Unified · Split
| @@ -145,6 +145,8 @@ typedef struct InputStream { · 144 unmodified lines | ||
| 145 | 145 | int discard; /* true if stream data should be discarded */ |
| 146 | 146 | int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */ |
| 147 | 147 | AVCodec *dec; |
| 148 | + AVFrame *decoded_frame; | |
| 149 | + AVFrame *filtered_frame; | |
| 148 | 150 | |
| 149 | 151 | int64_t start; /* time when read started */ |
| 150 | 152 | int64_t next_pts; /* synthetic pts for cases where pkt.pts |
| @@ -523,8 +525,11 @@ void exit_program(int ret) · 372 unmodified lines | ||
| 523 | 525 | for(i=0;i<nb_input_files;i++) { |
| 524 | 526 | av_close_input_file(input_files[i].ctx); |
| 525 | 527 | } |
| 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); | |
| 527 | 531 | av_dict_free(&input_streams[i].opts); |
| 532 | + } | |
| 528 | 533 | |
| 529 | 534 | if (vstats_file) |
| 530 | 535 | fclose(vstats_file); |
| @@ -1622,18 +1627,19 @@ static int transcode_audio(InputStream *ist, AVPacket *pkt, int *got_output) · 1091 unmodified lines | ||
| 1622 | 1627 | int bps = av_get_bytes_per_sample(ist->st->codec->sample_fmt); |
| 1623 | 1628 | int i, ret; |
| 1624 | 1629 | |
| 1625 | − if (!(decoded_frame = avcodec_alloc_frame())) | |
| 1630 | + if (!ist->decoded_frame && !(ist->decoded_frame = avcodec_alloc_frame())) | |
| 1626 | 1631 | return AVERROR(ENOMEM); |
| 1632 | + else | |
| 1633 | + avcodec_get_frame_defaults(ist->decoded_frame); | |
| 1634 | + decoded_frame = ist->decoded_frame; | |
| 1627 | 1635 | |
| 1628 | 1636 | ret = avcodec_decode_audio4(avctx, decoded_frame, got_output, pkt); |
| 1629 | 1637 | if (ret < 0) { |
| 1630 | − av_freep(&decoded_frame); | |
| 1631 | 1638 | return ret; |
| 1632 | 1639 | } |
| 1633 | 1640 | |
| 1634 | 1641 | if (!*got_output) { |
| 1635 | 1642 | /* no audio frame */ |
| 1636 | − av_freep(&decoded_frame); | |
| 1637 | 1643 | return ret; |
| 1638 | 1644 | } |
| 1639 | 1645 | |
| @@ -1701,7 +1707,6 @@ static int transcode_audio(InputStream *ist, AVPacket *pkt, int *got_output) · 61 unmodified lines | ||
| 1701 | 1707 | av_log(NULL, AV_LOG_FATAL, |
| 1702 | 1708 | "Audio volume adjustment on sample format %s is not supported.\n", |
| 1703 | 1709 | av_get_sample_fmt_name(ist->st->codec->sample_fmt)); |
| 1704 | − av_freep(&decoded_frame); | |
| 1705 | 1710 | exit_program(1); |
| 1706 | 1711 | } |
| 1707 | 1712 | } |
| @@ -1716,7 +1721,6 @@ static int transcode_audio(InputStream *ist, AVPacket *pkt, int *got_output) · 8 unmodified lines | ||
| 1716 | 1721 | do_audio_out(output_files[ost->file_index].ctx, ost, ist, decoded_frame); |
| 1717 | 1722 | } |
| 1718 | 1723 | |
| 1719 | − av_freep(&decoded_frame); | |
| 1720 | 1724 | return ret; |
| 1721 | 1725 | } |
| 1722 | 1726 | |
| @@ -1730,8 +1734,11 @@ static int transcode_video(InputStream *ist, AVPacket *pkt, int *got_output, int · 7 unmodified lines | ||
| 1730 | 1734 | int frame_available = 1; |
| 1731 | 1735 | #endif |
| 1732 | 1736 | |
| 1733 | − if (!(decoded_frame = avcodec_alloc_frame())) | |
| 1737 | + if (!ist->decoded_frame && !(ist->decoded_frame = avcodec_alloc_frame())) | |
| 1734 | 1738 | return AVERROR(ENOMEM); |
| 1739 | + else | |
| 1740 | + avcodec_get_frame_defaults(ist->decoded_frame); | |
| 1741 | + decoded_frame = ist->decoded_frame; | |
| 1735 | 1742 | pkt->pts = *pkt_pts; |
| 1736 | 1743 | pkt->dts = ist->pts; |
| 1737 | 1744 | *pkt_pts = AV_NOPTS_VALUE; |
| @@ -1739,12 +1746,11 @@ static int transcode_video(InputStream *ist, AVPacket *pkt, int *got_output, int · 1 unmodified line | ||
| 1739 | 1746 | ret = avcodec_decode_video2(ist->st->codec, |
| 1740 | 1747 | decoded_frame, got_output, pkt); |
| 1741 | 1748 | if (ret < 0) |
| 1742 | − goto fail; | |
| 1749 | + return ret; | |
| 1743 | 1750 | |
| 1744 | 1751 | quality = same_quant ? decoded_frame->quality : 0; |
| 1745 | 1752 | if (!*got_output) { |
| 1746 | 1753 | /* no picture yet */ |
| 1747 | − av_freep(&decoded_frame); | |
| 1748 | 1754 | return ret; |
| 1749 | 1755 | } |
| 1750 | 1756 | 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 | ||
| 1778 | 1784 | else |
| 1779 | 1785 | sar = ist->st->codec->sample_aspect_ratio; |
| 1780 | 1786 | 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; | |
| 1785 | 1793 | frame_available = avfilter_poll_frame(ost->output_video_filter->inputs[0]); |
| 1786 | 1794 | } |
| 1787 | 1795 | while (frame_available) { |
| @@ -1805,13 +1813,10 @@ static int transcode_video(InputStream *ist, AVPacket *pkt, int *got_output, int · 17 unmodified lines | ||
| 1805 | 1813 | if (ost->picref) |
| 1806 | 1814 | avfilter_unref_buffer(ost->picref); |
| 1807 | 1815 | } |
| 1808 | − av_freep(&filtered_frame); | |
| 1809 | 1816 | #endif |
| 1810 | 1817 | } |
| 1811 | 1818 | |
| 1812 | −fail: | |
| 1813 | 1819 | av_free(buffer_to_free); |
| 1814 | − av_freep(&decoded_frame); | |
| 1815 | 1820 | return ret; |
| 1816 | 1821 | } |
| 1817 | 1822 | |