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

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
4848 av_frame_unref(pic->f_grain);
4949 ff_refstruct_unref(&pic->hwaccel_picture_private);
5050
51− av_buffer_unref(&pic->qscale_table_buf);
52− av_buffer_unref(&pic->mb_type_buf);
51+ ff_refstruct_unref(&pic->qscale_table_base);
52+ ff_refstruct_unref(&pic->mb_type_base);
5353 ff_refstruct_unref(&pic->pps);
5454 for (i = 0; i < 2; i++) {
55− av_buffer_unref(&pic->motion_val_buf[i]);
56− av_buffer_unref(&pic->ref_index_buf[i]);
55+ ff_refstruct_unref(&pic->motion_val_base[i]);
56+ ff_refstruct_unref(&pic->ref_index[i]);
5757 }
58− av_buffer_unref(&pic->decode_error_flags);
58+ ff_refstruct_unref(&pic->decode_error_flags);
5959
6060 memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
6161 }
6262
6363 static void h264_copy_picture_params(H264Picture *dst, const H264Picture *src)
6464 {
65+ ff_refstruct_replace(&dst->qscale_table_base, src->qscale_table_base);
66+ ff_refstruct_replace(&dst->mb_type_base, src->mb_type_base);
6567 ff_refstruct_replace(&dst->pps, src->pps);
6668
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+
6779 dst->qscale_table = src->qscale_table;
6880 dst->mb_type = src->mb_type;
6981
70− for (int i = 0; i < 2; i++) {
82+ for (int i = 0; i < 2; i++)
7183 dst->motion_val[i] = src->motion_val[i];
72− dst->ref_index[i] = src->ref_index[i];
73− }
7484
7585 for (int i = 0; i < 2; i++)
7686 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
96106
97107 int ff_h264_ref_picture(H264Picture *dst, const H264Picture *src)
98108 {
99− int ret, i;
109+ int ret;
100110
101111 av_assert0(!dst->f->buf[0]);
102112 av_assert0(src->f->buf[0]);
@@ -113,29 +123,6 @@ int ff_h264_ref_picture(H264Picture *dst, const H264Picture *src) · 10 unmodified lines
113123 goto fail;
114124 }
115125
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−
139126 h264_copy_picture_params(dst, src);
140127
141128 return 0;
@@ -146,7 +133,7 @@ fail: · 4 unmodified lines
146133
147134 int ff_h264_replace_picture(H264Picture *dst, const H264Picture *src)
148135 {
149− int ret, i;
136+ int ret;
150137
151138 if (!src->f || !src->f->buf[0]) {
152139 ff_h264_unref_picture(dst);
@@ -167,25 +154,6 @@ int ff_h264_replace_picture(H264Picture *dst, const H264Picture *src) · 14 unmodified lines
167154 goto fail;
168155 }
169156
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−
189157 h264_copy_picture_params(dst, src);
190158
191159 return 0;
libavcodec/h264_slice.c+21−23
@@ -165,20 +165,19 @@ static int init_table_pools(H264Context *h) · 164 unmodified lines
165165 const int b4_stride = h->mb_width * 4 + 1;
166166 const int b4_array_size = b4_stride * h->mb_height * 4;
167167
168− h->qscale_table_pool = av_buffer_pool_init(big_mb_num + h->mb_stride,
169− av_buffer_allocz);
170− h->mb_type_pool = av_buffer_pool_init((big_mb_num + h->mb_stride) *
171− sizeof(uint32_t), av_buffer_allocz);
172− h->motion_val_pool = av_buffer_pool_init(2 * (b4_array_size + 4) *
173− sizeof(int16_t), av_buffer_allocz);
174− h->ref_index_pool = av_buffer_pool_init(4 * mb_array_size, av_buffer_allocz);
168+ h->qscale_table_pool = ff_refstruct_pool_alloc(big_mb_num + h->mb_stride, 0);
169+ h->mb_type_pool = ff_refstruct_pool_alloc((big_mb_num + h->mb_stride) *
170+ sizeof(uint32_t), 0);
171+ h->motion_val_pool = ff_refstruct_pool_alloc(2 * (b4_array_size + 4) *
172+ sizeof(int16_t), 0);
173+ h->ref_index_pool = ff_refstruct_pool_alloc(4 * mb_array_size, 0);
175174
176175 if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||
177176 !h->ref_index_pool) {
178− av_buffer_pool_uninit(&h->qscale_table_pool);
179− av_buffer_pool_uninit(&h->mb_type_pool);
180− av_buffer_pool_uninit(&h->motion_val_pool);
181− av_buffer_pool_uninit(&h->ref_index_pool);
177+ ff_refstruct_pool_uninit(&h->qscale_table_pool);
178+ ff_refstruct_pool_uninit(&h->mb_type_pool);
179+ ff_refstruct_pool_uninit(&h->motion_val_pool);
180+ ff_refstruct_pool_uninit(&h->ref_index_pool);
182181 return AVERROR(ENOMEM);
183182 }
184183
@@ -211,10 +210,10 @@ static int alloc_picture(H264Context *h, H264Picture *pic) · 26 unmodified lines
211210 goto fail;
212211
213212 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);
215214 if (!pic->decode_error_flags)
216215 goto fail;
217− atomic_init((atomic_int*)pic->decode_error_flags->data, 0);
216+ atomic_init(pic->decode_error_flags, 0);
218217 }
219218
220219 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
236235 goto fail;
237236 }
238237
239− pic->qscale_table_buf = av_buffer_pool_get(h->qscale_table_pool);
240− pic->mb_type_buf = av_buffer_pool_get(h->mb_type_pool);
241− if (!pic->qscale_table_buf || !pic->mb_type_buf)
238+ pic->qscale_table_base = ff_refstruct_pool_get(h->qscale_table_pool);
239+ pic->mb_type_base = ff_refstruct_pool_get(h->mb_type_pool);
240+ if (!pic->qscale_table_base || !pic->mb_type_base)
242241 goto fail;
243242
244− pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1;
245− pic->qscale_table = pic->qscale_table_buf->data + 2 * h->mb_stride + 1;
243+ pic->mb_type = pic->mb_type_base + 2 * h->mb_stride + 1;
244+ pic->qscale_table = pic->qscale_table_base + 2 * h->mb_stride + 1;
246245
247246 for (i = 0; i < 2; i++) {
248− pic->motion_val_buf[i] = av_buffer_pool_get(h->motion_val_pool);
249− pic->ref_index_buf[i] = av_buffer_pool_get(h->ref_index_pool);
250− if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
247+ pic->motion_val_base[i] = ff_refstruct_pool_get(h->motion_val_pool);
248+ pic->ref_index[i] = ff_refstruct_pool_get(h->ref_index_pool);
249+ if (!pic->motion_val_base[i] || !pic->ref_index[i])
251250 goto fail;
252251
253− pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
254− pic->ref_index[i] = pic->ref_index_buf[i]->data;
252+ pic->motion_val[i] = pic->motion_val_base[i] + 4;
255253 }
256254
257255 pic->pps = ff_refstruct_ref_c(h->ps.pps);
libavcodec/h264dec.c+10−9
@@ -51,6 +51,7 @@ · 50 unmodified lines
5151 #include "mpegutils.h"
5252 #include "profiles.h"
5353 #include "rectangle.h"
54+#include "refstruct.h"
5455 #include "thread.h"
5556 #include "threadframe.h"
5657
@@ -151,10 +152,10 @@ void ff_h264_free_tables(H264Context *h) · 94 unmodified lines
151152 av_freep(&h->mb2b_xy);
152153 av_freep(&h->mb2br_xy);
153154
154− av_buffer_pool_uninit(&h->qscale_table_pool);
155− av_buffer_pool_uninit(&h->mb_type_pool);
156− av_buffer_pool_uninit(&h->motion_val_pool);
157− av_buffer_pool_uninit(&h->ref_index_pool);
155+ ff_refstruct_pool_uninit(&h->qscale_table_pool);
156+ ff_refstruct_pool_uninit(&h->mb_type_pool);
157+ ff_refstruct_pool_uninit(&h->motion_val_pool);
158+ ff_refstruct_pool_uninit(&h->ref_index_pool);
158159
159160 #if CONFIG_ERROR_RESILIENCE
160161 av_freep(&h->er.mb_index2xy);
@@ -308,7 +309,7 @@ static int h264_init_context(AVCodecContext *avctx, H264Context *h) · 147 unmodified lines
308309 ff_h264_sei_uninit(&h->sei);
309310
310311 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);
312313 if (!h->decode_error_flags_pool)
313314 return AVERROR(ENOMEM);
314315 }
@@ -359,7 +360,7 @@ static av_cold int h264_decode_end(AVCodecContext *avctx) · 44 unmodified lines
359360
360361 h->cur_pic_ptr = NULL;
361362
362− av_buffer_pool_uninit(&h->decode_error_flags_pool);
363+ ff_refstruct_pool_uninit(&h->decode_error_flags_pool);
363364
364365 av_freep(&h->slice_ctx);
365366 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
749750 if ((ret < 0 || h->er.error_occurred) && h->cur_pic_ptr) {
750751 if (h->cur_pic_ptr->decode_error_flags) {
751752 /* 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;
753754 /* Using atomics here is not supposed to provide syncronisation;
754755 * they are merely used to allow to set decode_error from both
755756 * decoding threads in case of coded slices. */
@@ -800,7 +801,7 @@ end: · 44 unmodified lines
800801 ff_er_frame_end(&h->er, &decode_error_flags);
801802 if (decode_error_flags) {
802803 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;
804805 atomic_fetch_or_explicit(decode_error, decode_error_flags,
805806 memory_order_relaxed);
806807 } else
@@ -878,7 +879,7 @@ static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp) · 71 unmodified lines
878879 return ret;
879880
880881 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;
882883 /* The following is not supposed to provide synchronisation at all:
883884 * given that srcp has already finished decoding, decode_error
884885 * has already been set to its final value. */
libavcodec/h264dec.h+11−12
@@ -109,20 +109,19 @@ typedef struct H264Picture { · 108 unmodified lines
109109
110110 AVFrame *f_grain;
111111
112− AVBufferRef *qscale_table_buf;
112+ int8_t *qscale_table_base; ///< RefStruct reference
113113 int8_t *qscale_table;
114114
115− AVBufferRef *motion_val_buf[2];
115+ int16_t (*motion_val_base[2])[2]; ///< RefStruct reference
116116 int16_t (*motion_val[2])[2];
117117
118− AVBufferRef *mb_type_buf;
118+ uint32_t *mb_type_base; ///< RefStruct reference
119119 uint32_t *mb_type;
120120
121121 /// RefStruct reference for hardware accelerator private data
122122 void *hwaccel_picture_private;
123123
124− AVBufferRef *ref_index_buf[2];
125− int8_t *ref_index[2];
124+ int8_t *ref_index[2]; ///< RefStruct reference
126125
127126 int field_poc[2]; ///< top/bottom POC
128127 int poc; ///< frame POC
@@ -153,8 +152,8 @@ typedef struct H264Picture { · 24 unmodified lines
153152 int mb_width, mb_height;
154153 int mb_stride;
155154
156− /* data points to an atomic_int */
157− AVBufferRef *decode_error_flags;
155+ /// RefStruct reference; its pointee is shared between decoding threads.
156+ atomic_int *decode_error_flags;
158157 } H264Picture;
159158
160159 typedef struct H264Ref {
@@ -548,11 +547,11 @@ typedef struct H264Context { · 387 unmodified lines
548547
549548 H264SEIContext sei;
550549
551− AVBufferPool *qscale_table_pool;
552− AVBufferPool *mb_type_pool;
553− AVBufferPool *motion_val_pool;
554− AVBufferPool *ref_index_pool;
555− AVBufferPool *decode_error_flags_pool;
550+ struct FFRefStructPool *qscale_table_pool;
551+ struct FFRefStructPool *mb_type_pool;
552+ struct FFRefStructPool *motion_val_pool;
553+ struct FFRefStructPool *ref_index_pool;
554+ struct FFRefStructPool *decode_error_flags_pool;
556555 int ref2frm[MAX_SLICES][2][64]; ///< reference to frame number lists, used in the loop filter, the first 2 are for -2,-1
557556 } H264Context;
558557

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