History

avcodec/pngdec: Switch to ProgressFrames

Avoids implicit av_frame_ref() and therefore allocations and error checks. It also avoids explicitly allocating the AVFrames (done implicitly when getting the buffer).

Andreas Rheinhardt committed 4 years ago

Commit 2f291471 parent a807e461 file · +27 −40Browse files

Unified · Split

libavcodec/pngdec.c+27−40
@@ -42,8 +42,8 @@ · 41 unmodified lines
42#include "apng.h"42#include "apng.h"
43#include "png.h"43#include "png.h"
44#include "pngdsp.h"44#include "pngdsp.h"
45#include "progressframe.h"
45#include "thread.h"46#include "thread.h"
46#include "threadframe.h"
47#include "zlib_wrapper.h"47#include "zlib_wrapper.h"
48 48
49#include <zlib.h>49#include <zlib.h>
@@ -63,8 +63,8 @@ typedef struct PNGDecContext { · 13 unmodified lines
63 AVCodecContext *avctx;63 AVCodecContext *avctx;
64 64
65 GetByteContext gb;65 GetByteContext gb;
66 ThreadFrame last_picture;66 ProgressFrame last_picture;
67 ThreadFrame picture;67 ProgressFrame picture;
68 68
69 AVDictionary *frame_metadata;69 AVDictionary *frame_metadata;
70 70
@@ -874,7 +874,7 @@ static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s, · 803 unmodified lines
874 s->bpp += byte_depth;874 s->bpp += byte_depth;
875 }875 }
876 876
877 ff_thread_release_ext_buffer(&s->picture);877 ff_progress_frame_unref(&s->picture);
878 if (s->dispose_op == APNG_DISPOSE_OP_PREVIOUS) {878 if (s->dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
879 /* We only need a buffer for the current picture. */879 /* We only need a buffer for the current picture. */
880 ret = ff_thread_get_buffer(avctx, p, 0);880 ret = ff_thread_get_buffer(avctx, p, 0);
@@ -883,8 +883,8 @@ static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s, · 2 unmodified lines
883 } else if (s->dispose_op == APNG_DISPOSE_OP_BACKGROUND) {883 } else if (s->dispose_op == APNG_DISPOSE_OP_BACKGROUND) {
884 /* We need a buffer for the current picture as well as884 /* We need a buffer for the current picture as well as
885 * a buffer for the reference to retain. */885 * a buffer for the reference to retain. */
886 ret = ff_thread_get_ext_buffer(avctx, &s->picture,886 ret = ff_progress_frame_get_buffer(avctx, &s->picture,
887 AV_GET_BUFFER_FLAG_REF);887 AV_GET_BUFFER_FLAG_REF);
888 if (ret < 0)888 if (ret < 0)
889 return ret;889 return ret;
890 ret = ff_thread_get_buffer(avctx, p, 0);890 ret = ff_thread_get_buffer(avctx, p, 0);
@@ -892,8 +892,9 @@ static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s, · 1 unmodified line
892 return ret;892 return ret;
893 } else {893 } else {
894 /* The picture output this time and the reference to retain coincide. */894 /* The picture output this time and the reference to retain coincide. */
895 if ((ret = ff_thread_get_ext_buffer(avctx, &s->picture,895 ret = ff_progress_frame_get_buffer(avctx, &s->picture,
896 AV_GET_BUFFER_FLAG_REF)) < 0)896 AV_GET_BUFFER_FLAG_REF);
897 if (ret < 0)
897 return ret;898 return ret;
898 ret = av_frame_ref(p, s->picture.f);899 ret = av_frame_ref(p, s->picture.f);
899 if (ret < 0)900 if (ret < 0)
@@ -1254,7 +1255,7 @@ static void handle_p_frame_png(PNGDecContext *s, AVFrame *p) · 354 unmodified lines
1254 1255
1255 ls = FFMIN(ls, s->width * s->bpp);1256 ls = FFMIN(ls, s->width * s->bpp);
1256 1257
1257 ff_thread_await_progress(&s->last_picture, INT_MAX, 0);1258 ff_progress_frame_await(&s->last_picture, INT_MAX);
1258 for (j = 0; j < s->height; j++) {1259 for (j = 0; j < s->height; j++) {
1259 for (i = 0; i < ls; i++)1260 for (i = 0; i < ls; i++)
1260 pd[i] += pd_last[i];1261 pd[i] += pd_last[i];
@@ -1286,7 +1287,7 @@ static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s, · 25 unmodified lines
1286 return AVERROR_PATCHWELCOME;1287 return AVERROR_PATCHWELCOME;
1287 }1288 }
1288 1289
1289 ff_thread_await_progress(&s->last_picture, INT_MAX, 0);1290 ff_progress_frame_await(&s->last_picture, INT_MAX);
1290 1291
1291 // copy unchanged rectangles from the last frame1292 // copy unchanged rectangles from the last frame
1292 for (y = 0; y < s->y_offset; y++)1293 for (y = 0; y < s->y_offset; y++)
@@ -1674,7 +1675,7 @@ exit_loop: · 381 unmodified lines
1674 }1675 }
1675 1676
1676 /* handle P-frames only if a predecessor frame is available */1677 /* handle P-frames only if a predecessor frame is available */
1677 if (s->last_picture.f->data[0]) {1678 if (s->last_picture.f) {
1678 if ( !(avpkt->flags & AV_PKT_FLAG_KEY) && avctx->codec_tag != AV_RL32("MPNG")1679 if ( !(avpkt->flags & AV_PKT_FLAG_KEY) && avctx->codec_tag != AV_RL32("MPNG")
1679 && s->last_picture.f->width == p->width1680 && s->last_picture.f->width == p->width
1680 && s->last_picture.f->height== p->height1681 && s->last_picture.f->height== p->height
@@ -1691,12 +1692,11 @@ exit_loop: · 10 unmodified lines
1691 if (CONFIG_APNG_DECODER && s->dispose_op == APNG_DISPOSE_OP_BACKGROUND)1692 if (CONFIG_APNG_DECODER && s->dispose_op == APNG_DISPOSE_OP_BACKGROUND)
1692 apng_reset_background(s, p);1693 apng_reset_background(s, p);
1693 1694
1694 ff_thread_report_progress(&s->picture, INT_MAX, 0);1695 ret = 0;
1695
1696 return 0;
1697
1698fail:1696fail:
1699 ff_thread_report_progress(&s->picture, INT_MAX, 0);1697 if (s->picture.f)
1698 ff_progress_frame_report(&s->picture, INT_MAX);
1699
1700 return ret;1700 return ret;
1701}1701}
1702 1702
@@ -1783,8 +1783,8 @@ static int decode_frame_png(AVCodecContext *avctx, AVFrame *p, · 80 unmodified lines
1783 goto the_end;1783 goto the_end;
1784 1784
1785 if (!(avctx->active_thread_type & FF_THREAD_FRAME)) {1785 if (!(avctx->active_thread_type & FF_THREAD_FRAME)) {
1786 ff_thread_release_ext_buffer(&s->last_picture);1786 ff_progress_frame_unref(&s->last_picture);
1787 FFSWAP(ThreadFrame, s->picture, s->last_picture);1787 FFSWAP(ProgressFrame, s->picture, s->last_picture);
1788 }1788 }
1789 1789
1790 *got_frame = 1;1790 *got_frame = 1;
@@ -1835,12 +1835,9 @@ static int decode_frame_apng(AVCodecContext *avctx, AVFrame *p, · 44 unmodified lines
1835 return ret;1835 return ret;
1836 1836
1837 if (!(avctx->active_thread_type & FF_THREAD_FRAME)) {1837 if (!(avctx->active_thread_type & FF_THREAD_FRAME)) {
1838 if (s->dispose_op == APNG_DISPOSE_OP_PREVIOUS) {1838 if (s->dispose_op != APNG_DISPOSE_OP_PREVIOUS)
1839 ff_thread_release_ext_buffer(&s->picture);1839 FFSWAP(ProgressFrame, s->picture, s->last_picture);
1840 } else {1840 ff_progress_frame_unref(&s->picture);
1841 ff_thread_release_ext_buffer(&s->last_picture);
1842 FFSWAP(ThreadFrame, s->picture, s->last_picture);
1843 }
1844 }1841 }
1845 1842
1846 *got_frame = 1;1843 *got_frame = 1;
@@ -1853,8 +1850,7 @@ static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src) · 6 unmodified lines
1853{1850{
1854 PNGDecContext *psrc = src->priv_data;1851 PNGDecContext *psrc = src->priv_data;
1855 PNGDecContext *pdst = dst->priv_data;1852 PNGDecContext *pdst = dst->priv_data;
1856 ThreadFrame *src_frame = NULL;1853 const ProgressFrame *src_frame;
1857 int ret;
1858 1854
1859 if (dst == src)1855 if (dst == src)
1860 return 0;1856 return 0;
@@ -1879,12 +1875,7 @@ static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src) · 18 unmodified lines
1879 src_frame = psrc->dispose_op == APNG_DISPOSE_OP_PREVIOUS ?1875 src_frame = psrc->dispose_op == APNG_DISPOSE_OP_PREVIOUS ?
1880 &psrc->last_picture : &psrc->picture;1876 &psrc->last_picture : &psrc->picture;
1881 1877
1882 ff_thread_release_ext_buffer(&pdst->last_picture);1878 ff_progress_frame_replace(&pdst->last_picture, src_frame);
1883 if (src_frame && src_frame->f->data[0]) {
1884 ret = ff_thread_ref_frame(&pdst->last_picture, src_frame);
1885 if (ret < 0)
1886 return ret;
1887 }
1888 1879
1889 return 0;1880 return 0;
1890}1881}
@@ -1895,10 +1886,6 @@ static av_cold int png_dec_init(AVCodecContext *avctx) · 4 unmodified lines
1895 PNGDecContext *s = avctx->priv_data;1886 PNGDecContext *s = avctx->priv_data;
1896 1887
1897 s->avctx = avctx;1888 s->avctx = avctx;
1898 s->last_picture.f = av_frame_alloc();
1899 s->picture.f = av_frame_alloc();
1900 if (!s->last_picture.f || !s->picture.f)
1901 return AVERROR(ENOMEM);
1902 1889
1903 ff_pngdsp_init(&s->dsp);1890 ff_pngdsp_init(&s->dsp);
1904 1891
@@ -1909,10 +1896,8 @@ static av_cold int png_dec_end(AVCodecContext *avctx) · 4 unmodified lines
1909{1896{
1910 PNGDecContext *s = avctx->priv_data;1897 PNGDecContext *s = avctx->priv_data;
1911 1898
1912 ff_thread_release_ext_buffer(&s->last_picture);1899 ff_progress_frame_unref(&s->last_picture);
1913 av_frame_free(&s->last_picture.f);1900 ff_progress_frame_unref(&s->picture);
1914 ff_thread_release_ext_buffer(&s->picture);
1915 av_frame_free(&s->picture.f);
1916 av_freep(&s->buffer);1901 av_freep(&s->buffer);
1917 s->buffer_size = 0;1902 s->buffer_size = 0;
1918 av_freep(&s->last_row);1903 av_freep(&s->last_row);
@@ -1940,6 +1925,7 @@ const FFCodec ff_apng_decoder = { · 21 unmodified lines
1940 UPDATE_THREAD_CONTEXT(update_thread_context),1925 UPDATE_THREAD_CONTEXT(update_thread_context),
1941 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,1926 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1942 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |1927 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
1928 FF_CODEC_CAP_USES_PROGRESSFRAMES |
1943 FF_CODEC_CAP_ICC_PROFILES,1929 FF_CODEC_CAP_ICC_PROFILES,
1944};1930};
1945#endif1931#endif
@@ -1958,6 +1944,7 @@ const FFCodec ff_png_decoder = { · 12 unmodified lines
1958 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,1944 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1959 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |1945 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
1960 FF_CODEC_CAP_INIT_CLEANUP |1946 FF_CODEC_CAP_INIT_CLEANUP |
1947 FF_CODEC_CAP_USES_PROGRESSFRAMES |
1961 FF_CODEC_CAP_ICC_PROFILES,1948 FF_CODEC_CAP_ICC_PROFILES,
1962};1949};
1963#endif1950#endif

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