FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegpicture.c
Date: 2024-04-23 16:28:37
Exec Total Coverage
Lines: 165 202 81.7%
Functions: 14 14 100.0%
Branches: 82 118 69.5%

Line Branch Exec Source
1 /*
2 * Mpeg video formats-related picture management functions
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <stdint.h>
22
23 #include "libavutil/avassert.h"
24 #include "libavutil/common.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/imgutils.h"
28
29 #include "avcodec.h"
30 #include "motion_est.h"
31 #include "mpegpicture.h"
32 #include "mpegvideo.h"
33 #include "refstruct.h"
34 #include "threadframe.h"
35
36 32775 static void av_noinline free_picture_tables(Picture *pic)
37 {
38 32775 pic->alloc_mb_width =
39 32775 pic->alloc_mb_height = 0;
40
41 32775 av_buffer_unref(&pic->mbskip_table_buf);
42 32775 av_buffer_unref(&pic->qscale_table_buf);
43 32775 av_buffer_unref(&pic->mb_type_buf);
44
45
2/2
✓ Branch 0 taken 65550 times.
✓ Branch 1 taken 32775 times.
98325 for (int i = 0; i < 2; i++) {
46 65550 av_buffer_unref(&pic->motion_val_buf[i]);
47 65550 av_buffer_unref(&pic->ref_index_buf[i]);
48 }
49 32775 }
50
51 125481 static int make_table_writable(AVBufferRef **ref)
52 {
53 125481 AVBufferRef *old = *ref, *new;
54
55
2/2
✓ Branch 1 taken 64398 times.
✓ Branch 2 taken 61083 times.
125481 if (av_buffer_is_writable(old))
56 64398 return 0;
57 61083 new = av_buffer_allocz(old->size);
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61083 times.
61083 if (!new)
59 return AVERROR(ENOMEM);
60 61083 av_buffer_unref(ref);
61 61083 *ref = new;
62 61083 return 0;
63 }
64
65 20215 static int make_tables_writable(Picture *pic)
66 {
67 #define MAKE_WRITABLE(table) \
68 do {\
69 int ret = make_table_writable(&pic->table); \
70 if (ret < 0) \
71 return ret; \
72 } while (0)
73
74
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20215 times.
20215 MAKE_WRITABLE(mbskip_table_buf);
75
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20215 times.
20215 MAKE_WRITABLE(qscale_table_buf);
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20215 times.
20215 MAKE_WRITABLE(mb_type_buf);
77
78
2/2
✓ Branch 0 taken 16209 times.
✓ Branch 1 taken 4006 times.
20215 if (pic->motion_val_buf[0]) {
79
2/2
✓ Branch 0 taken 32418 times.
✓ Branch 1 taken 16209 times.
48627 for (int i = 0; i < 2; i++) {
80
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32418 times.
32418 MAKE_WRITABLE(motion_val_buf[i]);
81
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32418 times.
32418 MAKE_WRITABLE(ref_index_buf[i]);
82 }
83 }
84
85 20215 return 0;
86 }
87
88 508 int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me,
89 ScratchpadContext *sc, int linesize)
90 {
91 # define EMU_EDGE_HEIGHT (4 * 70)
92 508 int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
93
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 508 times.
508 if (avctx->hwaccel)
95 return 0;
96
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 508 times.
508 if (linesize < 24) {
98 av_log(avctx, AV_LOG_ERROR, "Image too small, temporary buffers cannot function\n");
99 return AVERROR_PATCHWELCOME;
100 }
101
102
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 508 times.
508 if (av_image_check_size2(alloc_size, EMU_EDGE_HEIGHT, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)
103 return AVERROR(ENOMEM);
104
105 // edge emu needs blocksize + filter length - 1
106 // (= 17x17 for halfpel / 21x21 for H.264)
107 // VC-1 computes luma and chroma simultaneously and needs 19X19 + 9x9
108 // at uvlinesize. It supports only YUV420 so 24x24 is enough
109 // linesize * interlaced * MBsize
110 // we also use this buffer for encoding in encode_mb_internal() needig an additional 32 lines
111
1/2
✓ Branch 1 taken 508 times.
✗ Branch 2 not taken.
508 if (!FF_ALLOCZ_TYPED_ARRAY(sc->edge_emu_buffer, alloc_size * EMU_EDGE_HEIGHT) ||
112
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 508 times.
508 !FF_ALLOCZ_TYPED_ARRAY(me->scratchpad, alloc_size * 4 * 16 * 2)) {
113 av_freep(&sc->edge_emu_buffer);
114 return AVERROR(ENOMEM);
115 }
116
117 508 me->temp = me->scratchpad;
118 508 sc->rd_scratchpad = me->scratchpad;
119 508 sc->b_scratchpad = me->scratchpad;
120 508 sc->obmc_scratchpad = me->scratchpad + 16;
121
122 508 return 0;
123 }
124
125 /**
126 * Check the pic's linesize and allocate linesize dependent scratch buffers
127 */
128 21490 static int handle_pic_linesizes(AVCodecContext *avctx, Picture *pic,
129 MotionEstContext *me, ScratchpadContext *sc,
130 int linesize, int uvlinesize)
131 {
132 int ret;
133
134
5/6
✓ Branch 0 taken 21050 times.
✓ Branch 1 taken 440 times.
✓ Branch 2 taken 21050 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 21050 times.
✓ Branch 5 taken 440 times.
21490 if ((linesize && linesize != pic->f->linesize[0]) ||
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21050 times.
21050 (uvlinesize && uvlinesize != pic->f->linesize[1])) {
136 av_log(avctx, AV_LOG_ERROR,
137 "get_buffer() failed (stride changed: linesize=%d/%d uvlinesize=%d/%d)\n",
138 linesize, pic->f->linesize[0],
139 uvlinesize, pic->f->linesize[1]);
140 ff_mpeg_unref_picture(pic);
141 return -1;
142 }
143
144
1/2
✓ Branch 1 taken 21490 times.
✗ Branch 2 not taken.
21490 if (av_pix_fmt_count_planes(pic->f->format) > 2 &&
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21490 times.
21490 pic->f->linesize[1] != pic->f->linesize[2]) {
146 av_log(avctx, AV_LOG_ERROR,
147 "get_buffer() failed (uv stride mismatch)\n");
148 ff_mpeg_unref_picture(pic);
149 return -1;
150 }
151
152
3/4
✓ Branch 0 taken 440 times.
✓ Branch 1 taken 21050 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 440 times.
21930 if (!sc->edge_emu_buffer &&
153 440 (ret = ff_mpeg_framesize_alloc(avctx, me, sc,
154 440 pic->f->linesize[0])) < 0) {
155 av_log(avctx, AV_LOG_ERROR,
156 "get_buffer() failed to allocate context scratch buffers.\n");
157 ff_mpeg_unref_picture(pic);
158 return ret;
159 }
160
161 21490 return 0;
162 }
163
164 1275 static int alloc_picture_tables(AVCodecContext *avctx, Picture *pic, int encoding, int out_format,
165 int mb_stride, int mb_width, int mb_height, int b8_stride)
166 {
167 1275 const int big_mb_num = mb_stride * (mb_height + 1) + 1;
168 1275 const int mb_array_size = mb_stride * mb_height;
169 1275 const int b8_array_size = b8_stride * mb_height * 2;
170 int i;
171
172
173 1275 pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
174 1275 pic->qscale_table_buf = av_buffer_allocz(big_mb_num + mb_stride);
175 1275 pic->mb_type_buf = av_buffer_allocz((big_mb_num + mb_stride) *
176 sizeof(uint32_t));
177
3/6
✓ Branch 0 taken 1275 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1275 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1275 times.
1275 if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
178 return AVERROR(ENOMEM);
179
180
4/4
✓ Branch 0 taken 587 times.
✓ Branch 1 taken 688 times.
✓ Branch 2 taken 199 times.
✓ Branch 3 taken 388 times.
1275 if (out_format == FMT_H263 || encoding ||
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 199 times.
199 (avctx->export_side_data & AV_CODEC_EXPORT_DATA_MVS)) {
182 1076 int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
183 1076 int ref_index_size = 4 * mb_array_size;
184
185
3/4
✓ Branch 0 taken 3228 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2152 times.
✓ Branch 3 taken 1076 times.
3228 for (i = 0; mv_size && i < 2; i++) {
186 2152 pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
187 2152 pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
188
2/4
✓ Branch 0 taken 2152 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2152 times.
2152 if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
189 return AVERROR(ENOMEM);
190 }
191 }
192
193 1275 pic->alloc_mb_width = mb_width;
194 1275 pic->alloc_mb_height = mb_height;
195 1275 pic->alloc_mb_stride = mb_stride;
196
197 1275 return 0;
198 }
199
200 /**
201 * Allocate a Picture.
202 * The pixels are allocated/set by calling get_buffer() if shared = 0
203 */
204 21490 int ff_alloc_picture(AVCodecContext *avctx, Picture *pic, MotionEstContext *me,
205 ScratchpadContext *sc, int encoding, int out_format,
206 int mb_stride, int mb_width, int mb_height, int b8_stride,
207 ptrdiff_t *linesize, ptrdiff_t *uvlinesize)
208 {
209 int i, ret;
210
211
2/2
✓ Branch 0 taken 20215 times.
✓ Branch 1 taken 1275 times.
21490 if (pic->qscale_table_buf)
212
1/2
✓ Branch 0 taken 20215 times.
✗ Branch 1 not taken.
20215 if ( pic->alloc_mb_width != mb_width
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20215 times.
20215 || pic->alloc_mb_height != mb_height)
214 free_picture_tables(pic);
215
216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21490 times.
21490 if (handle_pic_linesizes(avctx, pic, me, sc,
217 21490 *linesize, *uvlinesize) < 0)
218 return -1;
219
220 21490 *linesize = pic->f->linesize[0];
221 21490 *uvlinesize = pic->f->linesize[1];
222
223
2/2
✓ Branch 0 taken 1275 times.
✓ Branch 1 taken 20215 times.
21490 if (!pic->qscale_table_buf)
224 1275 ret = alloc_picture_tables(avctx, pic, encoding, out_format,
225 mb_stride, mb_width, mb_height, b8_stride);
226 else
227 20215 ret = make_tables_writable(pic);
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21490 times.
21490 if (ret < 0)
229 goto fail;
230
231 21490 pic->mbskip_table = pic->mbskip_table_buf->data;
232 21490 pic->qscale_table = pic->qscale_table_buf->data + 2 * mb_stride + 1;
233 21490 pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * mb_stride + 1;
234
235
2/2
✓ Branch 0 taken 17285 times.
✓ Branch 1 taken 4205 times.
21490 if (pic->motion_val_buf[0]) {
236
2/2
✓ Branch 0 taken 34570 times.
✓ Branch 1 taken 17285 times.
51855 for (i = 0; i < 2; i++) {
237 34570 pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
238 34570 pic->ref_index[i] = pic->ref_index_buf[i]->data;
239 }
240 }
241
242 21490 return 0;
243 fail:
244 av_log(avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
245 ff_mpeg_unref_picture(pic);
246 free_picture_tables(pic);
247 return AVERROR(ENOMEM);
248 }
249
250 /**
251 * Deallocate a picture; frees the picture tables in case they
252 * need to be reallocated anyway.
253 */
254 854447 void ff_mpeg_unref_picture(Picture *pic)
255 {
256 854447 pic->tf.f = pic->f;
257 854447 ff_thread_release_ext_buffer(&pic->tf);
258
259 854447 ff_refstruct_unref(&pic->hwaccel_picture_private);
260
261
2/2
✓ Branch 0 taken 1512 times.
✓ Branch 1 taken 852935 times.
854447 if (pic->needs_realloc)
262 1512 free_picture_tables(pic);
263
264 854447 pic->field_picture = 0;
265 854447 pic->b_frame_score = 0;
266 854447 pic->needs_realloc = 0;
267 854447 pic->reference = 0;
268 854447 pic->shared = 0;
269 854447 pic->display_picture_number = 0;
270 854447 pic->coded_picture_number = 0;
271 854447 }
272
273 63877 int ff_update_picture_tables(Picture *dst, const Picture *src)
274 {
275 int i, ret;
276
277 63877 ret = av_buffer_replace(&dst->mbskip_table_buf, src->mbskip_table_buf);
278 63877 ret |= av_buffer_replace(&dst->qscale_table_buf, src->qscale_table_buf);
279 63877 ret |= av_buffer_replace(&dst->mb_type_buf, src->mb_type_buf);
280
2/2
✓ Branch 0 taken 127754 times.
✓ Branch 1 taken 63877 times.
191631 for (i = 0; i < 2; i++) {
281 127754 ret |= av_buffer_replace(&dst->motion_val_buf[i], src->motion_val_buf[i]);
282 127754 ret |= av_buffer_replace(&dst->ref_index_buf[i], src->ref_index_buf[i]);
283 }
284
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63877 times.
63877 if (ret < 0) {
286 free_picture_tables(dst);
287 return ret;
288 }
289
290 63877 dst->mbskip_table = src->mbskip_table;
291 63877 dst->qscale_table = src->qscale_table;
292 63877 dst->mb_type = src->mb_type;
293
2/2
✓ Branch 0 taken 127754 times.
✓ Branch 1 taken 63877 times.
191631 for (i = 0; i < 2; i++) {
294 127754 dst->motion_val[i] = src->motion_val[i];
295 127754 dst->ref_index[i] = src->ref_index[i];
296 }
297
298 63877 dst->alloc_mb_width = src->alloc_mb_width;
299 63877 dst->alloc_mb_height = src->alloc_mb_height;
300 63877 dst->alloc_mb_stride = src->alloc_mb_stride;
301
302 63877 return 0;
303 }
304
305 63876 int ff_mpeg_ref_picture(Picture *dst, Picture *src)
306 {
307 int ret;
308
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63876 times.
63876 av_assert0(!dst->f->buf[0]);
310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63876 times.
63876 av_assert0(src->f->buf[0]);
311
312 63876 src->tf.f = src->f;
313 63876 dst->tf.f = dst->f;
314 63876 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63876 times.
63876 if (ret < 0)
316 goto fail;
317
318 63876 ret = ff_update_picture_tables(dst, src);
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63876 times.
63876 if (ret < 0)
320 goto fail;
321
322 63876 ff_refstruct_replace(&dst->hwaccel_picture_private,
323 63876 src->hwaccel_picture_private);
324
325 63876 dst->field_picture = src->field_picture;
326 63876 dst->b_frame_score = src->b_frame_score;
327 63876 dst->needs_realloc = src->needs_realloc;
328 63876 dst->reference = src->reference;
329 63876 dst->shared = src->shared;
330 63876 dst->display_picture_number = src->display_picture_number;
331 63876 dst->coded_picture_number = src->coded_picture_number;
332
333 63876 return 0;
334 fail:
335 ff_mpeg_unref_picture(dst);
336 return ret;
337 }
338
339 45246 static inline int pic_is_unused(Picture *pic)
340 {
341
2/2
✓ Branch 0 taken 21470 times.
✓ Branch 1 taken 23776 times.
45246 if (!pic->f->buf[0])
342 21470 return 1;
343
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 23756 times.
23776 if (pic->needs_realloc)
344 20 return 1;
345 23756 return 0;
346 }
347
348 22139 static int find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
349 {
350 int i;
351
352
2/2
✓ Branch 0 taken 649 times.
✓ Branch 1 taken 21490 times.
22139 if (shared) {
353
1/2
✓ Branch 0 taken 852 times.
✗ Branch 1 not taken.
852 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
354
2/2
✓ Branch 0 taken 649 times.
✓ Branch 1 taken 203 times.
852 if (!picture[i].f->buf[0])
355 649 return i;
356 }
357 } else {
358
1/2
✓ Branch 0 taken 45246 times.
✗ Branch 1 not taken.
45246 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
359
2/2
✓ Branch 1 taken 21490 times.
✓ Branch 2 taken 23756 times.
45246 if (pic_is_unused(&picture[i]))
360 21490 return i;
361 }
362 }
363
364 av_log(avctx, AV_LOG_FATAL,
365 "Internal error, picture buffer overflow\n");
366 /* We could return -1, but the codec would crash trying to draw into a
367 * non-existing frame anyway. This is safer than waiting for a random crash.
368 * Also the return of this is never useful, an encoder must only allocate
369 * as much as allowed in the specification. This has no relationship to how
370 * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
371 * enough for such valid streams).
372 * Plus, a decoder has to check stream validity and remove frames if too
373 * many reference frames are around. Waiting for "OOM" is not correct at
374 * all. Similarly, missing reference frames have to be replaced by
375 * interpolated/MC frames, anything else is a bug in the codec ...
376 */
377 abort();
378 return -1;
379 }
380
381 22139 int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
382 {
383 22139 int ret = find_unused_picture(avctx, picture, shared);
384
385
2/4
✓ Branch 0 taken 22139 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22139 times.
✗ Branch 3 not taken.
22139 if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
386
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 22119 times.
22139 if (picture[ret].needs_realloc) {
387 20 ff_mpeg_unref_picture(&picture[ret]);
388 }
389 }
390 22139 return ret;
391 }
392
393 31263 void av_cold ff_mpv_picture_free(Picture *pic)
394 {
395 31263 free_picture_tables(pic);
396 31263 ff_mpeg_unref_picture(pic);
397 31263 av_frame_free(&pic->f);
398 31263 }
399