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