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.
Unified · Split
| @@ -48,29 +48,39 @@ void ff_h264_unref_picture(H264Picture *pic) · 47 unmodified lines | ||
| 48 | 48 | av_frame_unref(pic->f_grain); |
| 49 | 49 | ff_refstruct_unref(&pic->hwaccel_picture_private); |
| 50 | 50 | |
| 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); | |
| 53 | 53 | ff_refstruct_unref(&pic->pps); |
| 54 | 54 | 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]); | |
| 57 | 57 | } |
| 58 | − av_buffer_unref(&pic->decode_error_flags); | |
| 58 | + ff_refstruct_unref(&pic->decode_error_flags); | |
| 59 | 59 | |
| 60 | 60 | memset((uint8_t*)pic + off, 0, sizeof(*pic) - off); |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | static 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 | 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 | 79 | dst->qscale_table = src->qscale_table; |
| 68 | 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 | 83 | dst->motion_val[i] = src->motion_val[i]; |
| 72 | − dst->ref_index[i] = src->ref_index[i]; | |
| 73 | − } | |
| 74 | 84 | |
| 75 | 85 | for (int i = 0; i < 2; i++) |
| 76 | 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 | |
| 97 | 107 | int ff_h264_ref_picture(H264Picture *dst, const H264Picture *src) |
| 98 | 108 | { |
| 99 | − int ret, i; | |
| 109 | + int ret; | |
| 100 | 110 | |
| 101 | 111 | av_assert0(!dst->f->buf[0]); |
| 102 | 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 | 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 | 126 | h264_copy_picture_params(dst, src); |
| 140 | 127 | |
| 141 | 128 | return 0; |
| @@ -146,7 +133,7 @@ fail: · 4 unmodified lines | ||
| 146 | 133 | |
| 147 | 134 | int ff_h264_replace_picture(H264Picture *dst, const H264Picture *src) |
| 148 | 135 | { |
| 149 | − int ret, i; | |
| 136 | + int ret; | |
| 150 | 137 | |
| 151 | 138 | if (!src->f || !src->f->buf[0]) { |
| 152 | 139 | ff_h264_unref_picture(dst); |
| @@ -167,25 +154,6 @@ int ff_h264_replace_picture(H264Picture *dst, const H264Picture *src) · 14 unmodified lines | ||
| 167 | 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 | 157 | h264_copy_picture_params(dst, src); |
| 190 | 158 | |
| 191 | 159 | return 0; |
| @@ -165,20 +165,19 @@ static int init_table_pools(H264Context *h) · 164 unmodified lines | ||
| 165 | 165 | const int b4_stride = h->mb_width * 4 + 1; |
| 166 | 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, | |
| 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); | |
| 175 | 174 | |
| 176 | 175 | if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool || |
| 177 | 176 | !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); | |
| 182 | 181 | return AVERROR(ENOMEM); |
| 183 | 182 | } |
| 184 | 183 | |
| @@ -211,10 +210,10 @@ static int alloc_picture(H264Context *h, H264Picture *pic) · 26 unmodified lines | ||
| 211 | 210 | goto fail; |
| 212 | 211 | |
| 213 | 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 | 214 | if (!pic->decode_error_flags) |
| 216 | 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 | 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 | 235 | goto fail; |
| 237 | 236 | } |
| 238 | 237 | |
| 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) | |
| 242 | 241 | goto fail; |
| 243 | 242 | |
| 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; | |
| 246 | 245 | |
| 247 | 246 | 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]) | |
| 251 | 250 | goto fail; |
| 252 | 251 | |
| 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; | |
| 255 | 253 | } |
| 256 | 254 | |
| 257 | 255 | pic->pps = ff_refstruct_ref_c(h->ps.pps); |
| @@ -51,6 +51,7 @@ · 50 unmodified lines | ||
| 51 | 51 | #include "mpegutils.h" |
| 52 | 52 | #include "profiles.h" |
| 53 | 53 | #include "rectangle.h" |
| 54 | +#include "refstruct.h" | |
| 54 | 55 | #include "thread.h" |
| 55 | 56 | #include "threadframe.h" |
| 56 | 57 | |
| @@ -151,10 +152,10 @@ void ff_h264_free_tables(H264Context *h) · 94 unmodified lines | ||
| 151 | 152 | av_freep(&h->mb2b_xy); |
| 152 | 153 | av_freep(&h->mb2br_xy); |
| 153 | 154 | |
| 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); | |
| 158 | 159 | |
| 159 | 160 | #if CONFIG_ERROR_RESILIENCE |
| 160 | 161 | av_freep(&h->er.mb_index2xy); |
| @@ -308,7 +309,7 @@ static int h264_init_context(AVCodecContext *avctx, H264Context *h) · 147 unmodified lines | ||
| 308 | 309 | ff_h264_sei_uninit(&h->sei); |
| 309 | 310 | |
| 310 | 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 | 313 | if (!h->decode_error_flags_pool) |
| 313 | 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 | 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 | 365 | av_freep(&h->slice_ctx); |
| 365 | 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 | 750 | if ((ret < 0 || h->er.error_occurred) && h->cur_pic_ptr) { |
| 750 | 751 | if (h->cur_pic_ptr->decode_error_flags) { |
| 751 | 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 | 754 | /* Using atomics here is not supposed to provide syncronisation; |
| 754 | 755 | * they are merely used to allow to set decode_error from both |
| 755 | 756 | * decoding threads in case of coded slices. */ |
| @@ -800,7 +801,7 @@ end: · 44 unmodified lines | ||
| 800 | 801 | ff_er_frame_end(&h->er, &decode_error_flags); |
| 801 | 802 | if (decode_error_flags) { |
| 802 | 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 | 805 | atomic_fetch_or_explicit(decode_error, decode_error_flags, |
| 805 | 806 | memory_order_relaxed); |
| 806 | 807 | } else |
| @@ -878,7 +879,7 @@ static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp) · 71 unmodified lines | ||
| 878 | 879 | return ret; |
| 879 | 880 | |
| 880 | 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 | 883 | /* The following is not supposed to provide synchronisation at all: |
| 883 | 884 | * given that srcp has already finished decoding, decode_error |
| 884 | 885 | * has already been set to its final value. */ |
| @@ -109,20 +109,19 @@ typedef struct H264Picture { · 108 unmodified lines | ||
| 109 | 109 | |
| 110 | 110 | AVFrame *f_grain; |
| 111 | 111 | |
| 112 | − AVBufferRef *qscale_table_buf; | |
| 112 | + int8_t *qscale_table_base; ///< RefStruct reference | |
| 113 | 113 | int8_t *qscale_table; |
| 114 | 114 | |
| 115 | − AVBufferRef *motion_val_buf[2]; | |
| 115 | + int16_t (*motion_val_base[2])[2]; ///< RefStruct reference | |
| 116 | 116 | int16_t (*motion_val[2])[2]; |
| 117 | 117 | |
| 118 | − AVBufferRef *mb_type_buf; | |
| 118 | + uint32_t *mb_type_base; ///< RefStruct reference | |
| 119 | 119 | uint32_t *mb_type; |
| 120 | 120 | |
| 121 | 121 | /// RefStruct reference for hardware accelerator private data |
| 122 | 122 | void *hwaccel_picture_private; |
| 123 | 123 | |
| 124 | − AVBufferRef *ref_index_buf[2]; | |
| 125 | − int8_t *ref_index[2]; | |
| 124 | + int8_t *ref_index[2]; ///< RefStruct reference | |
| 126 | 125 | |
| 127 | 126 | int field_poc[2]; ///< top/bottom POC |
| 128 | 127 | int poc; ///< frame POC |
| @@ -153,8 +152,8 @@ typedef struct H264Picture { · 24 unmodified lines | ||
| 153 | 152 | int mb_width, mb_height; |
| 154 | 153 | int mb_stride; |
| 155 | 154 | |
| 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; | |
| 158 | 157 | } H264Picture; |
| 159 | 158 | |
| 160 | 159 | typedef struct H264Ref { |
| @@ -548,11 +547,11 @@ typedef struct H264Context { · 387 unmodified lines | ||
| 548 | 547 | |
| 549 | 548 | H264SEIContext sei; |
| 550 | 549 | |
| 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; | |
| 556 | 555 | int ref2frm[MAX_SLICES][2][64]; ///< reference to frame number lists, used in the loop filter, the first 2 are for -2,-1 |
| 557 | 556 | } H264Context; |
| 558 | 557 | |