History

avcodec/mpegpicture: Use RefStruct-pool API

It involves less allocations and therefore has less potential errors to be checked. One consequence thereof is that updating the picture tables can no longer fail.

Andreas Rheinhardt committed 4 years ago

Commit bed17eb1 parent 6450cfc3 files · +53 −78Browse files

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

Unified · Split

3 files
  1. libavcodec/mpegpicture.c +29 −53
  2. libavcodec/mpegpicture.h +8 −13
  3. libavcodec/mpegvideo.c +16 −12
libavcodec/mpegpicture.c+29−53
@@ -18,8 +18,6 @@ · 17 unmodified lines
1818 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1919 */
2020
21−#include <stdint.h>
22−
2321 #include "libavutil/avassert.h"
2422 #include "libavutil/common.h"
2523 #include "libavutil/mem.h"
@@ -34,13 +32,13 @@ · 8 unmodified lines
3432
3533 static void av_noinline free_picture_tables(Picture *pic)
3634 {
37− av_buffer_unref(&pic->mbskip_table_buf);
38− av_buffer_unref(&pic->qscale_table_buf);
39− av_buffer_unref(&pic->mb_type_buf);
35+ ff_refstruct_unref(&pic->mbskip_table);
36+ ff_refstruct_unref(&pic->qscale_table_base);
37+ ff_refstruct_unref(&pic->mb_type_base);
4038
4139 for (int i = 0; i < 2; i++) {
42− av_buffer_unref(&pic->motion_val_buf[i]);
43− av_buffer_unref(&pic->ref_index_buf[i]);
40+ ff_refstruct_unref(&pic->motion_val_base[i]);
41+ ff_refstruct_unref(&pic->ref_index[i]);
4442 }
4543
4644 pic->mb_width =
@@ -135,18 +133,18 @@ static int handle_pic_linesizes(AVCodecContext *avctx, Picture *pic, · 88 unmodified lines
135133 static int alloc_picture_tables(BufferPoolContext *pools, Picture *pic,
136134 int mb_height)
137135 {
138−#define GET_BUFFER(name, idx_suffix) do { \
139− pic->name ## _buf idx_suffix = av_buffer_pool_get(pools->name ## _pool); \
140− if (!pic->name ## _buf idx_suffix) \
136+#define GET_BUFFER(name, buf_suffix, idx_suffix) do { \
137+ pic->name ## buf_suffix idx_suffix = ff_refstruct_pool_get(pools->name ## _pool); \
138+ if (!pic->name ## buf_suffix idx_suffix) \
141139 return AVERROR(ENOMEM); \
142140 } while (0)
143− GET_BUFFER(mbskip_table,);
144− GET_BUFFER(qscale_table,);
145− GET_BUFFER(mb_type,);
141+ GET_BUFFER(mbskip_table,,);
142+ GET_BUFFER(qscale_table, _base,);
143+ GET_BUFFER(mb_type, _base,);
146144 if (pools->motion_val_pool) {
147145 for (int i = 0; i < 2; i++) {
148− GET_BUFFER(motion_val, [i]);
149− GET_BUFFER(ref_index, [i]);
146+ GET_BUFFER(ref_index,, [i]);
147+ GET_BUFFER(motion_val, _base, [i]);
150148 }
151149 }
152150 #undef GET_BUFFER
@@ -166,7 +164,7 @@ int ff_alloc_picture(AVCodecContext *avctx, Picture *pic, MotionEstContext *me, · 13 unmodified lines
166164 ScratchpadContext *sc, BufferPoolContext *pools,
167165 int mb_height, ptrdiff_t *linesize, ptrdiff_t *uvlinesize)
168166 {
169− int i, ret;
167+ int ret;
170168
171169 if (handle_pic_linesizes(avctx, pic, me, sc,
172170 *linesize, *uvlinesize) < 0)
@@ -179,20 +177,12 @@ int ff_alloc_picture(AVCodecContext *avctx, Picture *pic, MotionEstContext *me, · 6 unmodified lines
179177 if (ret < 0)
180178 goto fail;
181179
182− pic->mbskip_table = pic->mbskip_table_buf->data;
183− memset(pic->mbskip_table, 0, pic->mbskip_table_buf->size);
184− pic->qscale_table = pic->qscale_table_buf->data + 2 * pic->mb_stride + 1;
185− pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * pic->mb_stride + 1;
186−
187− if (pic->motion_val_buf[0]) {
188− for (i = 0; i < 2; i++) {
189− pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
190− pic->ref_index[i] = pic->ref_index_buf[i]->data;
191− /* FIXME: The output of H.263 with OBMC depends upon
192− * the earlier content of the buffer; therefore we
193− * reset it here. */
194− memset(pic->motion_val_buf[i]->data, 0, pic->motion_val_buf[i]->size);
195− }
180+ pic->qscale_table = pic->qscale_table_base + 2 * pic->mb_stride + 1;
181+ pic->mb_type = pic->mb_type_base + 2 * pic->mb_stride + 1;
182+
183+ if (pic->motion_val_base[0]) {
184+ for (int i = 0; i < 2; i++)
185+ pic->motion_val[i] = pic->motion_val_base[i] + 4;
196186 }
197187
198188 return 0;
@@ -224,36 +214,24 @@ void ff_mpeg_unref_picture(Picture *pic) · 25 unmodified lines
224214 pic->coded_picture_number = 0;
225215 }
226216
227−static int update_picture_tables(Picture *dst, const Picture *src)
217+static void update_picture_tables(Picture *dst, const Picture *src)
228218 {
229− int i, ret;
230−
231− ret = av_buffer_replace(&dst->mbskip_table_buf, src->mbskip_table_buf);
232− ret |= av_buffer_replace(&dst->qscale_table_buf, src->qscale_table_buf);
233− ret |= av_buffer_replace(&dst->mb_type_buf, src->mb_type_buf);
234− for (i = 0; i < 2; i++) {
235− ret |= av_buffer_replace(&dst->motion_val_buf[i], src->motion_val_buf[i]);
236− ret |= av_buffer_replace(&dst->ref_index_buf[i], src->ref_index_buf[i]);
237− }
238−
239− if (ret < 0) {
240− free_picture_tables(dst);
241− return ret;
219+ ff_refstruct_replace(&dst->mbskip_table, src->mbskip_table);
220+ ff_refstruct_replace(&dst->qscale_table_base, src->qscale_table_base);
221+ ff_refstruct_replace(&dst->mb_type_base, src->mb_type_base);
222+ for (int i = 0; i < 2; i++) {
223+ ff_refstruct_replace(&dst->motion_val_base[i], src->motion_val_base[i]);
224+ ff_refstruct_replace(&dst->ref_index[i], src->ref_index[i]);
242225 }
243226
244− dst->mbskip_table = src->mbskip_table;
245227 dst->qscale_table = src->qscale_table;
246228 dst->mb_type = src->mb_type;
247− for (i = 0; i < 2; i++) {
229+ for (int i = 0; i < 2; i++)
248230 dst->motion_val[i] = src->motion_val[i];
249− dst->ref_index[i] = src->ref_index[i];
250− }
251231
252232 dst->mb_width = src->mb_width;
253233 dst->mb_height = src->mb_height;
254234 dst->mb_stride = src->mb_stride;
255−
256− return 0;
257235 }
258236
259237 int ff_mpeg_ref_picture(Picture *dst, Picture *src)
@@ -269,9 +247,7 @@ int ff_mpeg_ref_picture(Picture *dst, Picture *src) · 9 unmodified lines
269247 if (ret < 0)
270248 goto fail;
271249
272− ret = update_picture_tables(dst, src);
273− if (ret < 0)
274− goto fail;
250+ update_picture_tables(dst, src);
275251
276252 ff_refstruct_replace(&dst->hwaccel_picture_private,
277253 src->hwaccel_picture_private);
libavcodec/mpegpicture.h+8−13
@@ -23,9 +23,6 @@ · 22 unmodified lines
2323
2424 #include <stdint.h>
2525
26−#include "libavutil/buffer.h"
27−#include "libavutil/frame.h"
28−
2926 #include "avcodec.h"
3027 #include "motion_est.h"
3128 #include "threadframe.h"
@@ -43,11 +40,11 @@ typedef struct ScratchpadContext { · 11 unmodified lines
4340 } ScratchpadContext;
4441
4542 typedef struct BufferPoolContext {
46− AVBufferPool *mbskip_table_pool;
47− AVBufferPool *qscale_table_pool;
48− AVBufferPool *mb_type_pool;
49− AVBufferPool *motion_val_pool;
50− AVBufferPool *ref_index_pool;
43+ struct FFRefStructPool *mbskip_table_pool;
44+ struct FFRefStructPool *qscale_table_pool;
45+ struct FFRefStructPool *mb_type_pool;
46+ struct FFRefStructPool *motion_val_pool;
47+ struct FFRefStructPool *ref_index_pool;
5148 int alloc_mb_width; ///< mb_width used to allocate tables
5249 int alloc_mb_height; ///< mb_height used to allocate tables
5350 int alloc_mb_stride; ///< mb_stride used to allocate tables
@@ -60,19 +57,17 @@ typedef struct Picture { · 6 unmodified lines
6057 struct AVFrame *f;
6158 ThreadFrame tf;
6259
63− AVBufferRef *qscale_table_buf;
60+ int8_t *qscale_table_base;
6461 int8_t *qscale_table;
6562
66− AVBufferRef *motion_val_buf[2];
63+ int16_t (*motion_val_base[2])[2];
6764 int16_t (*motion_val[2])[2];
6865
69− AVBufferRef *mb_type_buf;
66+ uint32_t *mb_type_base;
7067 uint32_t *mb_type; ///< types and macros are defined in mpegutils.h
7168
72− AVBufferRef *mbskip_table_buf;
7369 uint8_t *mbskip_table;
7470
75− AVBufferRef *ref_index_buf[2];
7671 int8_t *ref_index[2];
7772
7873 /// RefStruct reference for hardware accelerator private data
libavcodec/mpegvideo.c+16−12
@@ -41,6 +41,7 @@ · 40 unmodified lines
4141 #include "mpegutils.h"
4242 #include "mpegvideo.h"
4343 #include "mpegvideodata.h"
44+#include "refstruct.h"
4445
4546 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
4647 int16_t *block, int n, int qscale)
@@ -536,11 +537,11 @@ void ff_mpv_common_defaults(MpegEncContext *s) · 489 unmodified lines
536537
537538 static void free_buffer_pools(BufferPoolContext *pools)
538539 {
539− av_buffer_pool_uninit(&pools->mbskip_table_pool);
540− av_buffer_pool_uninit(&pools->qscale_table_pool);
541− av_buffer_pool_uninit(&pools->mb_type_pool);
542− av_buffer_pool_uninit(&pools->motion_val_pool);
543− av_buffer_pool_uninit(&pools->ref_index_pool);
540+ ff_refstruct_pool_uninit(&pools->mbskip_table_pool);
541+ ff_refstruct_pool_uninit(&pools->qscale_table_pool);
542+ ff_refstruct_pool_uninit(&pools->mb_type_pool);
543+ ff_refstruct_pool_uninit(&pools->motion_val_pool);
544+ ff_refstruct_pool_uninit(&pools->ref_index_pool);
544545 pools->alloc_mb_height = pools->alloc_mb_width = pools->alloc_mb_stride = 0;
545546 }
546547
@@ -641,15 +642,15 @@ int ff_mpv_init_context_frame(MpegEncContext *s) · 94 unmodified lines
641642 return AVERROR(ENOMEM);
642643 memset(s->mbintra_table, 1, mb_array_size);
643644
644−#define ALLOC_POOL(name, size) do { \
645− pools->name ##_pool = av_buffer_pool_init((size), av_buffer_allocz); \
645+#define ALLOC_POOL(name, size, flags) do { \
646+ pools->name ##_pool = ff_refstruct_pool_alloc((size), (flags)); \
646647 if (!pools->name ##_pool) \
647648 return AVERROR(ENOMEM); \
648649 } while (0)
649650
650− ALLOC_POOL(mbskip_table, mb_array_size + 2);
651− ALLOC_POOL(qscale_table, mv_table_size);
652− ALLOC_POOL(mb_type, mv_table_size * sizeof(uint32_t));
651+ ALLOC_POOL(mbskip_table, mb_array_size + 2, FF_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME);
652+ ALLOC_POOL(qscale_table, mv_table_size, 0);
653+ ALLOC_POOL(mb_type, mv_table_size * sizeof(uint32_t), 0);
653654
654655 if (s->out_format == FMT_H263 || s->encoding ||
655656 (s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_MVS)) {
@@ -657,8 +658,11 @@ int ff_mpv_init_context_frame(MpegEncContext *s) · 1 unmodified line
657658 int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
658659 int ref_index_size = 4 * mb_array_size;
659660
660− ALLOC_POOL(motion_val, mv_size);
661− ALLOC_POOL(ref_index, ref_index_size);
661+ /* FIXME: The output of H.263 with OBMC depends upon
662+ * the earlier content of the buffer; therefore we set
663+ * the flags to always reset returned buffers here. */
664+ ALLOC_POOL(motion_val, mv_size, FF_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME);
665+ ALLOC_POOL(ref_index, ref_index_size, 0);
662666 }
663667 #undef ALLOC_POOL
664668 pools->alloc_mb_width = s->mb_width;

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