History

avcodec/h264dec: Use RefStruct-pool API instead of AVBufferPool API

It involves less allocations and therefore has the nice property that deriving a reference from a reference can't fail. This allows for considerable simplifications in ff_h264_(ref|replace)_picture(). Switching to the RefStruct API also allows to make H264Picture smaller, because some AVBufferRef* pointers could be removed without replacement.

Andreas Rheinhardt committed 4 years ago

Commit 736b5101 parent 26c0a734 files · +62 −96Browse files

Showing the changes to libavcodec (4 of 4 files). Show all

Unified · Split

4 files
  1. libavcodec/h264_picture.c +20 −52
  2. libavcodec/h264_slice.c +21 −23
  3. libavcodec/h264dec.c +10 −9
  4. libavcodec/h264dec.h +11 −12
libavcodec/h264_picture.c+20−52
@@ -48,29 +48,39 @@ void ff_h264_unref_picture(H264Picture *pic) · 47 unmodified lines
48 av_frame_unref(pic->f_grain);48 av_frame_unref(pic->f_grain);
49 ff_refstruct_unref(&pic->hwaccel_picture_private);49 ff_refstruct_unref(&pic->hwaccel_picture_private);
50 50
51 av_buffer_unref(&pic->qscale_table_buf);51 ff_refstruct_unref(&pic->qscale_table_base);
52 av_buffer_unref(&pic->mb_type_buf);52 ff_refstruct_unref(&pic->mb_type_base);
53 ff_refstruct_unref(&pic->pps);53 ff_refstruct_unref(&pic->pps);
54 for (i = 0; i < 2; i++) {54 for (i = 0; i < 2; i++) {
55 av_buffer_unref(&pic->motion_val_buf[i]);55 ff_refstruct_unref(&pic->motion_val_base[i]);
56 av_buffer_unref(&pic->ref_index_buf[i]);56 ff_refstruct_unref(&pic->ref_index[i]);
57 }57 }
58 av_buffer_unref(&pic->decode_error_flags);58 ff_refstruct_unref(&pic->decode_error_flags);
59 59
60 memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);60 memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
61}61}
62 62
63static void h264_copy_picture_params(H264Picture *dst, const H264Picture *src)63static void h264_copy_picture_params(H264Picture *dst, const H264Picture *src)
64{64{
65 ff_refstruct_replace(&dst->qscale_table_base, src->qscale_table_base);
66 ff_refstruct_replace(&dst->mb_type_base, src->mb_type_base);
65 ff_refstruct_replace(&dst->pps, src->pps);67 ff_refstruct_replace(&dst->pps, src->pps);
66 68
69 for (int i = 0; i < 2; i++) {
70 ff_refstruct_replace(&dst->motion_val_base[i], src->motion_val_base[i]);
71 ff_refstruct_replace(&dst->ref_index[i], src->ref_index[i]);
72 }
73
74 ff_refstruct_replace(&dst->hwaccel_picture_private,
75 src->hwaccel_picture_private);
76
77 ff_refstruct_replace(&dst->decode_error_flags, src->decode_error_flags);
78
67 dst->qscale_table = src->qscale_table;79 dst->qscale_table = src->qscale_table;
68 dst->mb_type = src->mb_type;80 dst->mb_type = src->mb_type;
69 81
70 for (int i = 0; i < 2; i++) {82 for (int i = 0; i < 2; i++)
71 dst->motion_val[i] = src->motion_val[i];83 dst->motion_val[i] = src->motion_val[i];
72 dst->ref_index[i] = src->ref_index[i];
73 }
74 84
75 for (int i = 0; i < 2; i++)85 for (int i = 0; i < 2; i++)
76 dst->field_poc[i] = src->field_poc[i];86 dst->field_poc[i] = src->field_poc[i];
@@ -96,7 +106,7 @@ static void h264_copy_picture_params(H264Picture *dst, const H264Picture *src) · 19 unmodified lines
96 106
97int ff_h264_ref_picture(H264Picture *dst, const H264Picture *src)107int ff_h264_ref_picture(H264Picture *dst, const H264Picture *src)
98{108{
99 int ret, i;109 int ret;
100 110
101 av_assert0(!dst->f->buf[0]);111 av_assert0(!dst->f->buf[0]);
102 av_assert0(src->f->buf[0]);112 av_assert0(src->f->buf[0]);
@@ -113,29 +123,6 @@ int ff_h264_ref_picture(H264Picture *dst, const H264Picture *src) · 10 unmodified lines
113 goto fail;123 goto fail;
114 }124 }
115 125
116 dst->qscale_table_buf = av_buffer_ref(src->qscale_table_buf);
117 dst->mb_type_buf = av_buffer_ref(src->mb_type_buf);
118 if (!dst->qscale_table_buf || !dst->mb_type_buf) {
119 ret = AVERROR(ENOMEM);
120 goto fail;
121 }
122
123 for (i = 0; i < 2; i++) {
124 dst->motion_val_buf[i] = av_buffer_ref(src->motion_val_buf[i]);
125 dst->ref_index_buf[i] = av_buffer_ref(src->ref_index_buf[i]);
126 if (!dst->motion_val_buf[i] || !dst->ref_index_buf[i]) {
127 ret = AVERROR(ENOMEM);
128 goto fail;
129 }
130 }
131
132 ff_refstruct_replace(&dst->hwaccel_picture_private,
133 src->hwaccel_picture_private);
134
135 ret = av_buffer_replace(&dst->decode_error_flags, src->decode_error_flags);
136 if (ret < 0)
137 goto fail;
138
139 h264_copy_picture_params(dst, src);126 h264_copy_picture_params(dst, src);
140 127
141 return 0;128 return 0;
@@ -146,7 +133,7 @@ fail: · 4 unmodified lines
146 133
147int ff_h264_replace_picture(H264Picture *dst, const H264Picture *src)134int ff_h264_replace_picture(H264Picture *dst, const H264Picture *src)
148{135{
149 int ret, i;136 int ret;
150 137
151 if (!src->f || !src->f->buf[0]) {138 if (!src->f || !src->f->buf[0]) {
152 ff_h264_unref_picture(dst);139 ff_h264_unref_picture(dst);
@@ -167,25 +154,6 @@ int ff_h264_replace_picture(H264Picture *dst, const H264Picture *src) · 14 unmodified lines
167 goto fail;154 goto fail;
168 }155 }
169 156
170 ret = av_buffer_replace(&dst->qscale_table_buf, src->qscale_table_buf);
171 ret |= av_buffer_replace(&dst->mb_type_buf, src->mb_type_buf);
172 if (ret < 0)
173 goto fail;
174
175 for (i = 0; i < 2; i++) {
176 ret = av_buffer_replace(&dst->motion_val_buf[i], src->motion_val_buf[i]);
177 ret |= av_buffer_replace(&dst->ref_index_buf[i], src->ref_index_buf[i]);
178 if (ret < 0)
179 goto fail;
180 }
181
182 ff_refstruct_replace(&dst->hwaccel_picture_private,
183 src->hwaccel_picture_private);
184
185 ret = av_buffer_replace(&dst->decode_error_flags, src->decode_error_flags);
186 if (ret < 0)
187 goto fail;
188
189 h264_copy_picture_params(dst, src);157 h264_copy_picture_params(dst, src);
190 158
191 return 0;159 return 0;
libavcodec/h264_slice.c+21−23
@@ -165,20 +165,19 @@ static int init_table_pools(H264Context *h) · 164 unmodified lines
165 const int b4_stride = h->mb_width * 4 + 1;165 const int b4_stride = h->mb_width * 4 + 1;
166 const int b4_array_size = b4_stride * h->mb_height * 4;166 const int b4_array_size = b4_stride * h->mb_height * 4;
167 167
168 h->qscale_table_pool = av_buffer_pool_init(big_mb_num + h->mb_stride,168 h->qscale_table_pool = ff_refstruct_pool_alloc(big_mb_num + h->mb_stride, 0);
169 av_buffer_allocz);169 h->mb_type_pool = ff_refstruct_pool_alloc((big_mb_num + h->mb_stride) *
170 h->mb_type_pool = av_buffer_pool_init((big_mb_num + h->mb_stride) *170 sizeof(uint32_t), 0);
171 sizeof(uint32_t), av_buffer_allocz);171 h->motion_val_pool = ff_refstruct_pool_alloc(2 * (b4_array_size + 4) *
172 h->motion_val_pool = av_buffer_pool_init(2 * (b4_array_size + 4) *172 sizeof(int16_t), 0);
173 sizeof(int16_t), av_buffer_allocz);173 h->ref_index_pool = ff_refstruct_pool_alloc(4 * mb_array_size, 0);
174 h->ref_index_pool = av_buffer_pool_init(4 * mb_array_size, av_buffer_allocz);
175 174
176 if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||175 if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||
177 !h->ref_index_pool) {176 !h->ref_index_pool) {
178 av_buffer_pool_uninit(&h->qscale_table_pool);177 ff_refstruct_pool_uninit(&h->qscale_table_pool);
179 av_buffer_pool_uninit(&h->mb_type_pool);178 ff_refstruct_pool_uninit(&h->mb_type_pool);
180 av_buffer_pool_uninit(&h->motion_val_pool);179 ff_refstruct_pool_uninit(&h->motion_val_pool);
181 av_buffer_pool_uninit(&h->ref_index_pool);180 ff_refstruct_pool_uninit(&h->ref_index_pool);
182 return AVERROR(ENOMEM);181 return AVERROR(ENOMEM);
183 }182 }
184 183
@@ -211,10 +210,10 @@ static int alloc_picture(H264Context *h, H264Picture *pic) · 26 unmodified lines
211 goto fail;210 goto fail;
212 211
213 if (h->decode_error_flags_pool) {212 if (h->decode_error_flags_pool) {
214 pic->decode_error_flags = av_buffer_pool_get(h->decode_error_flags_pool);213 pic->decode_error_flags = ff_refstruct_pool_get(h->decode_error_flags_pool);
215 if (!pic->decode_error_flags)214 if (!pic->decode_error_flags)
216 goto fail;215 goto fail;
217 atomic_init((atomic_int*)pic->decode_error_flags->data, 0);216 atomic_init(pic->decode_error_flags, 0);
218 }217 }
219 218
220 if (CONFIG_GRAY && !h->avctx->hwaccel && h->flags & AV_CODEC_FLAG_GRAY && pic->f->data[2]) {219 if (CONFIG_GRAY && !h->avctx->hwaccel && h->flags & AV_CODEC_FLAG_GRAY && pic->f->data[2]) {
@@ -236,22 +235,21 @@ static int alloc_picture(H264Context *h, H264Picture *pic) · 15 unmodified lines
236 goto fail;235 goto fail;
237 }236 }
238 237
239 pic->qscale_table_buf = av_buffer_pool_get(h->qscale_table_pool);238 pic->qscale_table_base = ff_refstruct_pool_get(h->qscale_table_pool);
240 pic->mb_type_buf = av_buffer_pool_get(h->mb_type_pool);239 pic->mb_type_base = ff_refstruct_pool_get(h->mb_type_pool);
241 if (!pic->qscale_table_buf || !pic->mb_type_buf)240 if (!pic->qscale_table_base || !pic->mb_type_base)
242 goto fail;241 goto fail;
243 242
244 pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1;243 pic->mb_type = pic->mb_type_base + 2 * h->mb_stride + 1;
245 pic->qscale_table = pic->qscale_table_buf->data + 2 * h->mb_stride + 1;244 pic->qscale_table = pic->qscale_table_base + 2 * h->mb_stride + 1;
246 245
247 for (i = 0; i < 2; i++) {246 for (i = 0; i < 2; i++) {
248 pic->motion_val_buf[i] = av_buffer_pool_get(h->motion_val_pool);247 pic->motion_val_base[i] = ff_refstruct_pool_get(h->motion_val_pool);
249 pic->ref_index_buf[i] = av_buffer_pool_get(h->ref_index_pool);248 pic->ref_index[i] = ff_refstruct_pool_get(h->ref_index_pool);
250 if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])249 if (!pic->motion_val_base[i] || !pic->ref_index[i])
251 goto fail;250 goto fail;
252 251
253 pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;252 pic->motion_val[i] = pic->motion_val_base[i] + 4;
254 pic->ref_index[i] = pic->ref_index_buf[i]->data;
255 }253 }
256 254
257 pic->pps = ff_refstruct_ref_c(h->ps.pps);255 pic->pps = ff_refstruct_ref_c(h->ps.pps);
libavcodec/h264dec.c+10−9
@@ -51,6 +51,7 @@ · 50 unmodified lines
51#include "mpegutils.h"51#include "mpegutils.h"
52#include "profiles.h"52#include "profiles.h"
53#include "rectangle.h"53#include "rectangle.h"
54#include "refstruct.h"
54#include "thread.h"55#include "thread.h"
55#include "threadframe.h"56#include "threadframe.h"
56 57
@@ -151,10 +152,10 @@ void ff_h264_free_tables(H264Context *h) · 94 unmodified lines
151 av_freep(&h->mb2b_xy);152 av_freep(&h->mb2b_xy);
152 av_freep(&h->mb2br_xy);153 av_freep(&h->mb2br_xy);
153 154
154 av_buffer_pool_uninit(&h->qscale_table_pool);155 ff_refstruct_pool_uninit(&h->qscale_table_pool);
155 av_buffer_pool_uninit(&h->mb_type_pool);156 ff_refstruct_pool_uninit(&h->mb_type_pool);
156 av_buffer_pool_uninit(&h->motion_val_pool);157 ff_refstruct_pool_uninit(&h->motion_val_pool);
157 av_buffer_pool_uninit(&h->ref_index_pool);158 ff_refstruct_pool_uninit(&h->ref_index_pool);
158 159
159#if CONFIG_ERROR_RESILIENCE160#if CONFIG_ERROR_RESILIENCE
160 av_freep(&h->er.mb_index2xy);161 av_freep(&h->er.mb_index2xy);
@@ -308,7 +309,7 @@ static int h264_init_context(AVCodecContext *avctx, H264Context *h) · 147 unmodified lines
308 ff_h264_sei_uninit(&h->sei);309 ff_h264_sei_uninit(&h->sei);
309 310
310 if (avctx->active_thread_type & FF_THREAD_FRAME) {311 if (avctx->active_thread_type & FF_THREAD_FRAME) {
311 h->decode_error_flags_pool = av_buffer_pool_init(sizeof(atomic_int), NULL);312 h->decode_error_flags_pool = ff_refstruct_pool_alloc(sizeof(atomic_int), 0);
312 if (!h->decode_error_flags_pool)313 if (!h->decode_error_flags_pool)
313 return AVERROR(ENOMEM);314 return AVERROR(ENOMEM);
314 }315 }
@@ -359,7 +360,7 @@ static av_cold int h264_decode_end(AVCodecContext *avctx) · 44 unmodified lines
359 360
360 h->cur_pic_ptr = NULL;361 h->cur_pic_ptr = NULL;
361 362
362 av_buffer_pool_uninit(&h->decode_error_flags_pool);363 ff_refstruct_pool_uninit(&h->decode_error_flags_pool);
363 364
364 av_freep(&h->slice_ctx);365 av_freep(&h->slice_ctx);
365 h->nb_slice_ctx = 0;366 h->nb_slice_ctx = 0;
@@ -749,7 +750,7 @@ static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size) · 383 unmodified lines
749 if ((ret < 0 || h->er.error_occurred) && h->cur_pic_ptr) {750 if ((ret < 0 || h->er.error_occurred) && h->cur_pic_ptr) {
750 if (h->cur_pic_ptr->decode_error_flags) {751 if (h->cur_pic_ptr->decode_error_flags) {
751 /* Frame-threading in use */752 /* Frame-threading in use */
752 atomic_int *decode_error = (atomic_int*)h->cur_pic_ptr->decode_error_flags->data;753 atomic_int *decode_error = h->cur_pic_ptr->decode_error_flags;
753 /* Using atomics here is not supposed to provide syncronisation;754 /* Using atomics here is not supposed to provide syncronisation;
754 * they are merely used to allow to set decode_error from both755 * they are merely used to allow to set decode_error from both
755 * decoding threads in case of coded slices. */756 * decoding threads in case of coded slices. */
@@ -800,7 +801,7 @@ end: · 44 unmodified lines
800 ff_er_frame_end(&h->er, &decode_error_flags);801 ff_er_frame_end(&h->er, &decode_error_flags);
801 if (decode_error_flags) {802 if (decode_error_flags) {
802 if (h->cur_pic_ptr->decode_error_flags) {803 if (h->cur_pic_ptr->decode_error_flags) {
803 atomic_int *decode_error = (atomic_int*)h->cur_pic_ptr->decode_error_flags->data;804 atomic_int *decode_error = h->cur_pic_ptr->decode_error_flags;
804 atomic_fetch_or_explicit(decode_error, decode_error_flags,805 atomic_fetch_or_explicit(decode_error, decode_error_flags,
805 memory_order_relaxed);806 memory_order_relaxed);
806 } else807 } else
@@ -878,7 +879,7 @@ static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp) · 71 unmodified lines
878 return ret;879 return ret;
879 880
880 if (srcp->decode_error_flags) {881 if (srcp->decode_error_flags) {
881 atomic_int *decode_error = (atomic_int*)srcp->decode_error_flags->data;882 atomic_int *decode_error = srcp->decode_error_flags;
882 /* The following is not supposed to provide synchronisation at all:883 /* The following is not supposed to provide synchronisation at all:
883 * given that srcp has already finished decoding, decode_error884 * given that srcp has already finished decoding, decode_error
884 * has already been set to its final value. */885 * has already been set to its final value. */
libavcodec/h264dec.h+11−12
@@ -109,20 +109,19 @@ typedef struct H264Picture { · 108 unmodified lines
109 109
110 AVFrame *f_grain;110 AVFrame *f_grain;
111 111
112 AVBufferRef *qscale_table_buf;112 int8_t *qscale_table_base; ///< RefStruct reference
113 int8_t *qscale_table;113 int8_t *qscale_table;
114 114
115 AVBufferRef *motion_val_buf[2];115 int16_t (*motion_val_base[2])[2]; ///< RefStruct reference
116 int16_t (*motion_val[2])[2];116 int16_t (*motion_val[2])[2];
117 117
118 AVBufferRef *mb_type_buf;118 uint32_t *mb_type_base; ///< RefStruct reference
119 uint32_t *mb_type;119 uint32_t *mb_type;
120 120
121 /// RefStruct reference for hardware accelerator private data121 /// RefStruct reference for hardware accelerator private data
122 void *hwaccel_picture_private;122 void *hwaccel_picture_private;
123 123
124 AVBufferRef *ref_index_buf[2];124 int8_t *ref_index[2]; ///< RefStruct reference
125 int8_t *ref_index[2];
126 125
127 int field_poc[2]; ///< top/bottom POC126 int field_poc[2]; ///< top/bottom POC
128 int poc; ///< frame POC127 int poc; ///< frame POC
@@ -153,8 +152,8 @@ typedef struct H264Picture { · 24 unmodified lines
153 int mb_width, mb_height;152 int mb_width, mb_height;
154 int mb_stride;153 int mb_stride;
155 154
156 /* data points to an atomic_int */155 /// RefStruct reference; its pointee is shared between decoding threads.
157 AVBufferRef *decode_error_flags;156 atomic_int *decode_error_flags;
158} H264Picture;157} H264Picture;
159 158
160typedef struct H264Ref {159typedef struct H264Ref {
@@ -548,11 +547,11 @@ typedef struct H264Context { · 387 unmodified lines
548 547
549 H264SEIContext sei;548 H264SEIContext sei;
550 549
551 AVBufferPool *qscale_table_pool;550 struct FFRefStructPool *qscale_table_pool;
552 AVBufferPool *mb_type_pool;551 struct FFRefStructPool *mb_type_pool;
553 AVBufferPool *motion_val_pool;552 struct FFRefStructPool *motion_val_pool;
554 AVBufferPool *ref_index_pool;553 struct FFRefStructPool *ref_index_pool;
555 AVBufferPool *decode_error_flags_pool;554 struct FFRefStructPool *decode_error_flags_pool;
556 int ref2frm[MAX_SLICES][2][64]; ///< reference to frame number lists, used in the loop filter, the first 2 are for -2,-1555 int ref2frm[MAX_SLICES][2][64]; ///< reference to frame number lists, used in the loop filter, the first 2 are for -2,-1
557} H264Context;556} H264Context;
558 557

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