FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegpicture.c
Date: 2022-07-07 01:21:54
Exec Total Coverage
Lines: 205 256 80.1%
Branches: 127 188 67.6%

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/pixdesc.h"
26 #include "libavutil/imgutils.h"
27
28 #include "avcodec.h"
29 #include "encode.h"
30 #include "motion_est.h"
31 #include "mpegpicture.h"
32 #include "mpegutils.h"
33 #include "threadframe.h"
34
35 31257 static void av_noinline free_picture_tables(Picture *pic)
36 {
37 31257 pic->alloc_mb_width =
38 31257 pic->alloc_mb_height = 0;
39
40 31257 av_buffer_unref(&pic->mb_var_buf);
41 31257 av_buffer_unref(&pic->mc_mb_var_buf);
42 31257 av_buffer_unref(&pic->mb_mean_buf);
43 31257 av_buffer_unref(&pic->mbskip_table_buf);
44 31257 av_buffer_unref(&pic->qscale_table_buf);
45 31257 av_buffer_unref(&pic->mb_type_buf);
46
47
2/2
✓ Branch 0 taken 62514 times.
✓ Branch 1 taken 31257 times.
93771 for (int i = 0; i < 2; i++) {
48 62514 av_buffer_unref(&pic->motion_val_buf[i]);
49 62514 av_buffer_unref(&pic->ref_index_buf[i]);
50 }
51 31257 }
52
53 18752 static int make_tables_writable(Picture *pic)
54 {
55 int ret, i;
56 #define MAKE_WRITABLE(table) \
57 do {\
58 if (pic->table &&\
59 (ret = av_buffer_make_writable(&pic->table)) < 0)\
60 return ret;\
61 } while (0)
62
63
3/4
✓ Branch 0 taken 8560 times.
✓ Branch 1 taken 10192 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8560 times.
18752 MAKE_WRITABLE(mb_var_buf);
64
3/4
✓ Branch 0 taken 8560 times.
✓ Branch 1 taken 10192 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8560 times.
18752 MAKE_WRITABLE(mc_mb_var_buf);
65
3/4
✓ Branch 0 taken 8560 times.
✓ Branch 1 taken 10192 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8560 times.
18752 MAKE_WRITABLE(mb_mean_buf);
66
2/4
✓ Branch 0 taken 18752 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 18752 times.
18752 MAKE_WRITABLE(mbskip_table_buf);
67
2/4
✓ Branch 0 taken 18752 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 18752 times.
18752 MAKE_WRITABLE(qscale_table_buf);
68
2/4
✓ Branch 0 taken 18752 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 18752 times.
18752 MAKE_WRITABLE(mb_type_buf);
69
70
2/2
✓ Branch 0 taken 37504 times.
✓ Branch 1 taken 18752 times.
56256 for (i = 0; i < 2; i++) {
71
3/4
✓ Branch 0 taken 30176 times.
✓ Branch 1 taken 7328 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30176 times.
37504 MAKE_WRITABLE(motion_val_buf[i]);
72
3/4
✓ Branch 0 taken 30176 times.
✓ Branch 1 taken 7328 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30176 times.
37504 MAKE_WRITABLE(ref_index_buf[i]);
73 }
74
75 18752 return 0;
76 }
77
78 448 int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me,
79 ScratchpadContext *sc, int linesize)
80 {
81 # define EMU_EDGE_HEIGHT (4 * 70)
82 448 int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
83
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448 times.
448 if (avctx->hwaccel)
85 return 0;
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448 times.
448 if (linesize < 24) {
88 av_log(avctx, AV_LOG_ERROR, "Image too small, temporary buffers cannot function\n");
89 return AVERROR_PATCHWELCOME;
90 }
91
92
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448 times.
448 if (av_image_check_size2(alloc_size, EMU_EDGE_HEIGHT, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)
93 return AVERROR(ENOMEM);
94
95 // edge emu needs blocksize + filter length - 1
96 // (= 17x17 for halfpel / 21x21 for H.264)
97 // VC-1 computes luma and chroma simultaneously and needs 19X19 + 9x9
98 // at uvlinesize. It supports only YUV420 so 24x24 is enough
99 // linesize * interlaced * MBsize
100 // we also use this buffer for encoding in encode_mb_internal() needig an additional 32 lines
101
1/2
✓ Branch 1 taken 448 times.
✗ Branch 2 not taken.
448 if (!FF_ALLOCZ_TYPED_ARRAY(sc->edge_emu_buffer, alloc_size * EMU_EDGE_HEIGHT) ||
102
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448 times.
448 !FF_ALLOCZ_TYPED_ARRAY(me->scratchpad, alloc_size * 4 * 16 * 2)) {
103 av_freep(&sc->edge_emu_buffer);
104 return AVERROR(ENOMEM);
105 }
106
107 448 me->temp = me->scratchpad;
108 448 sc->rd_scratchpad = me->scratchpad;
109 448 sc->b_scratchpad = me->scratchpad;
110 448 sc->obmc_scratchpad = me->scratchpad + 16;
111
112 448 return 0;
113 }
114
115 /**
116 * Allocate a frame buffer
117 */
118 19416 static int alloc_frame_buffer(AVCodecContext *avctx, Picture *pic,
119 MotionEstContext *me, ScratchpadContext *sc,
120 int chroma_x_shift, int chroma_y_shift,
121 int linesize, int uvlinesize)
122 {
123 19416 int edges_needed = av_codec_is_encoder(avctx->codec);
124 int r, ret;
125
126 19416 pic->tf.f = pic->f;
127
128
2/2
✓ Branch 0 taken 8614 times.
✓ Branch 1 taken 10802 times.
19416 if (edges_needed) {
129 8614 pic->f->width = avctx->width + 2 * EDGE_WIDTH;
130 8614 pic->f->height = avctx->height + 2 * EDGE_WIDTH;
131
132 8614 r = ff_encode_alloc_frame(avctx, pic->f);
133
1/2
✓ Branch 0 taken 10802 times.
✗ Branch 1 not taken.
10802 } else if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
134
1/2
✓ Branch 0 taken 10802 times.
✗ Branch 1 not taken.
10802 avctx->codec_id != AV_CODEC_ID_VC1IMAGE &&
135
2/2
✓ Branch 0 taken 10723 times.
✓ Branch 1 taken 79 times.
10802 avctx->codec_id != AV_CODEC_ID_MSS2) {
136 10723 r = ff_thread_get_ext_buffer(avctx, &pic->tf,
137 10723 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
138 } else {
139 79 pic->f->width = avctx->width;
140 79 pic->f->height = avctx->height;
141 79 pic->f->format = avctx->pix_fmt;
142 79 r = avcodec_default_get_buffer2(avctx, pic->f, 0);
143 }
144
145
2/4
✓ Branch 0 taken 19416 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19416 times.
19416 if (r < 0 || !pic->f->buf[0]) {
146 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
147 r, pic->f->data[0]);
148 return -1;
149 }
150
151
2/2
✓ Branch 0 taken 8614 times.
✓ Branch 1 taken 10802 times.
19416 if (edges_needed) {
152 int i;
153
2/2
✓ Branch 0 taken 25842 times.
✓ Branch 1 taken 8614 times.
34456 for (i = 0; pic->f->data[i]; i++) {
154
2/2
✓ Branch 0 taken 17228 times.
✓ Branch 1 taken 8614 times.
25842 int offset = (EDGE_WIDTH >> (i ? chroma_y_shift : 0)) *
155 25842 pic->f->linesize[i] +
156
2/2
✓ Branch 0 taken 17228 times.
✓ Branch 1 taken 8614 times.
25842 (EDGE_WIDTH >> (i ? chroma_x_shift : 0));
157 25842 pic->f->data[i] += offset;
158 }
159 8614 pic->f->width = avctx->width;
160 8614 pic->f->height = avctx->height;
161 }
162
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19416 times.
19416 if (avctx->hwaccel) {
164 assert(!pic->hwaccel_picture_private);
165 if (avctx->hwaccel->frame_priv_data_size) {
166 pic->hwaccel_priv_buf = av_buffer_allocz(avctx->hwaccel->frame_priv_data_size);
167 if (!pic->hwaccel_priv_buf) {
168 av_log(avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
169 return -1;
170 }
171 pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
172 }
173 }
174
175
5/6
✓ Branch 0 taken 19012 times.
✓ Branch 1 taken 404 times.
✓ Branch 2 taken 19012 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19012 times.
✓ Branch 5 taken 404 times.
19416 if ((linesize && linesize != pic->f->linesize[0]) ||
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19012 times.
19012 (uvlinesize && uvlinesize != pic->f->linesize[1])) {
177 av_log(avctx, AV_LOG_ERROR,
178 "get_buffer() failed (stride changed: linesize=%d/%d uvlinesize=%d/%d)\n",
179 linesize, pic->f->linesize[0],
180 uvlinesize, pic->f->linesize[1]);
181 ff_mpeg_unref_picture(avctx, pic);
182 return -1;
183 }
184
185
1/2
✓ Branch 1 taken 19416 times.
✗ Branch 2 not taken.
19416 if (av_pix_fmt_count_planes(pic->f->format) > 2 &&
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19416 times.
19416 pic->f->linesize[1] != pic->f->linesize[2]) {
187 av_log(avctx, AV_LOG_ERROR,
188 "get_buffer() failed (uv stride mismatch)\n");
189 ff_mpeg_unref_picture(avctx, pic);
190 return -1;
191 }
192
193
3/4
✓ Branch 0 taken 404 times.
✓ Branch 1 taken 19012 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 404 times.
19820 if (!sc->edge_emu_buffer &&
194 404 (ret = ff_mpeg_framesize_alloc(avctx, me, sc,
195 404 pic->f->linesize[0])) < 0) {
196 av_log(avctx, AV_LOG_ERROR,
197 "get_buffer() failed to allocate context scratch buffers.\n");
198 ff_mpeg_unref_picture(avctx, pic);
199 return ret;
200 }
201
202 19416 return 0;
203 }
204
205 1313 static int alloc_picture_tables(AVCodecContext *avctx, Picture *pic, int encoding, int out_format,
206 int mb_stride, int mb_width, int mb_height, int b8_stride)
207 {
208 1313 const int big_mb_num = mb_stride * (mb_height + 1) + 1;
209 1313 const int mb_array_size = mb_stride * mb_height;
210 1313 const int b8_array_size = b8_stride * mb_height * 2;
211 int i;
212
213
214 1313 pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
215 1313 pic->qscale_table_buf = av_buffer_allocz(big_mb_num + mb_stride);
216 1313 pic->mb_type_buf = av_buffer_allocz((big_mb_num + mb_stride) *
217 sizeof(uint32_t));
218
3/6
✓ Branch 0 taken 1313 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1313 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1313 times.
1313 if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
219 return AVERROR(ENOMEM);
220
221
2/2
✓ Branch 0 taken 703 times.
✓ Branch 1 taken 610 times.
1313 if (encoding) {
222 703 pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
223 703 pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
224 703 pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
225
3/6
✓ Branch 0 taken 703 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 703 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 703 times.
703 if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
226 return AVERROR(ENOMEM);
227 }
228
229
4/4
✓ Branch 0 taken 525 times.
✓ Branch 1 taken 788 times.
✓ Branch 2 taken 180 times.
✓ Branch 3 taken 345 times.
1313 if (out_format == FMT_H263 || encoding ||
230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 180 times.
180 (avctx->export_side_data & AV_CODEC_EXPORT_DATA_MVS)) {
231 1133 int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
232 1133 int ref_index_size = 4 * mb_array_size;
233
234
3/4
✓ Branch 0 taken 3399 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2266 times.
✓ Branch 3 taken 1133 times.
3399 for (i = 0; mv_size && i < 2; i++) {
235 2266 pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
236 2266 pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
237
2/4
✓ Branch 0 taken 2266 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2266 times.
2266 if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
238 return AVERROR(ENOMEM);
239 }
240 }
241
242 1313 pic->alloc_mb_width = mb_width;
243 1313 pic->alloc_mb_height = mb_height;
244 1313 pic->alloc_mb_stride = mb_stride;
245
246 1313 return 0;
247 }
248
249 /**
250 * Allocate a Picture.
251 * The pixels are allocated/set by calling get_buffer() if shared = 0
252 */
253 20065 int ff_alloc_picture(AVCodecContext *avctx, Picture *pic, MotionEstContext *me,
254 ScratchpadContext *sc, int shared, int encoding,
255 int chroma_x_shift, int chroma_y_shift, int out_format,
256 int mb_stride, int mb_width, int mb_height, int b8_stride,
257 ptrdiff_t *linesize, ptrdiff_t *uvlinesize)
258 {
259 int i, ret;
260
261
2/2
✓ Branch 0 taken 18752 times.
✓ Branch 1 taken 1313 times.
20065 if (pic->qscale_table_buf)
262
1/2
✓ Branch 0 taken 18752 times.
✗ Branch 1 not taken.
18752 if ( pic->alloc_mb_width != mb_width
263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18752 times.
18752 || pic->alloc_mb_height != mb_height)
264 free_picture_tables(pic);
265
266
2/2
✓ Branch 0 taken 649 times.
✓ Branch 1 taken 19416 times.
20065 if (shared) {
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 649 times.
649 av_assert0(pic->f->data[0]);
268 649 pic->shared = 1;
269 } else {
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19416 times.
19416 av_assert0(!pic->f->buf[0]);
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19416 times.
19416 if (alloc_frame_buffer(avctx, pic, me, sc,
272 chroma_x_shift, chroma_y_shift,
273 19416 *linesize, *uvlinesize) < 0)
274 return -1;
275
276 19416 *linesize = pic->f->linesize[0];
277 19416 *uvlinesize = pic->f->linesize[1];
278 }
279
280
2/2
✓ Branch 0 taken 1313 times.
✓ Branch 1 taken 18752 times.
20065 if (!pic->qscale_table_buf)
281 1313 ret = alloc_picture_tables(avctx, pic, encoding, out_format,
282 mb_stride, mb_width, mb_height, b8_stride);
283 else
284 18752 ret = make_tables_writable(pic);
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20065 times.
20065 if (ret < 0)
286 goto fail;
287
288
2/2
✓ Branch 0 taken 9263 times.
✓ Branch 1 taken 10802 times.
20065 if (encoding) {
289 9263 pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
290 9263 pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
291 9263 pic->mb_mean = pic->mb_mean_buf->data;
292 }
293
294 20065 pic->mbskip_table = pic->mbskip_table_buf->data;
295 20065 pic->qscale_table = pic->qscale_table_buf->data + 2 * mb_stride + 1;
296 20065 pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * mb_stride + 1;
297
298
2/2
✓ Branch 0 taken 16221 times.
✓ Branch 1 taken 3844 times.
20065 if (pic->motion_val_buf[0]) {
299
2/2
✓ Branch 0 taken 32442 times.
✓ Branch 1 taken 16221 times.
48663 for (i = 0; i < 2; i++) {
300 32442 pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
301 32442 pic->ref_index[i] = pic->ref_index_buf[i]->data;
302 }
303 }
304
305 20065 return 0;
306 fail:
307 av_log(avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
308 ff_mpeg_unref_picture(avctx, pic);
309 free_picture_tables(pic);
310 return AVERROR(ENOMEM);
311 }
312
313 /**
314 * Deallocate a picture; frees the picture tables in case they
315 * need to be reallocated anyway.
316 */
317 774049 void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *pic)
318 {
319 774049 int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
320
321 774049 pic->tf.f = pic->f;
322 /* WM Image / Screen codecs allocate internal buffers with different
323 * dimensions / colorspaces; ignore user-defined callbacks for these. */
324
1/2
✓ Branch 0 taken 774049 times.
✗ Branch 1 not taken.
774049 if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
325
1/2
✓ Branch 0 taken 774049 times.
✗ Branch 1 not taken.
774049 avctx->codec_id != AV_CODEC_ID_VC1IMAGE &&
326
2/2
✓ Branch 0 taken 767497 times.
✓ Branch 1 taken 6552 times.
774049 avctx->codec_id != AV_CODEC_ID_MSS2)
327 767497 ff_thread_release_ext_buffer(avctx, &pic->tf);
328
1/2
✓ Branch 0 taken 6552 times.
✗ Branch 1 not taken.
6552 else if (pic->f)
329 6552 av_frame_unref(pic->f);
330
331 774049 av_buffer_unref(&pic->hwaccel_priv_buf);
332
333
2/2
✓ Branch 0 taken 1476 times.
✓ Branch 1 taken 772573 times.
774049 if (pic->needs_realloc)
334 1476 free_picture_tables(pic);
335
336 774049 memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
337 774049 }
338
339 57604 int ff_update_picture_tables(Picture *dst, const Picture *src)
340 {
341 int i, ret;
342
343 57604 ret = av_buffer_replace(&dst->mb_var_buf, src->mb_var_buf);
344 57604 ret |= av_buffer_replace(&dst->mc_mb_var_buf, src->mc_mb_var_buf);
345 57604 ret |= av_buffer_replace(&dst->mb_mean_buf, src->mb_mean_buf);
346 57604 ret |= av_buffer_replace(&dst->mbskip_table_buf, src->mbskip_table_buf);
347 57604 ret |= av_buffer_replace(&dst->qscale_table_buf, src->qscale_table_buf);
348 57604 ret |= av_buffer_replace(&dst->mb_type_buf, src->mb_type_buf);
349
2/2
✓ Branch 0 taken 115208 times.
✓ Branch 1 taken 57604 times.
172812 for (i = 0; i < 2; i++) {
350 115208 ret |= av_buffer_replace(&dst->motion_val_buf[i], src->motion_val_buf[i]);
351 115208 ret |= av_buffer_replace(&dst->ref_index_buf[i], src->ref_index_buf[i]);
352 }
353
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57604 times.
57604 if (ret < 0) {
355 free_picture_tables(dst);
356 return ret;
357 }
358
359 57604 dst->mb_var = src->mb_var;
360 57604 dst->mc_mb_var = src->mc_mb_var;
361 57604 dst->mb_mean = src->mb_mean;
362 57604 dst->mbskip_table = src->mbskip_table;
363 57604 dst->qscale_table = src->qscale_table;
364 57604 dst->mb_type = src->mb_type;
365
2/2
✓ Branch 0 taken 115208 times.
✓ Branch 1 taken 57604 times.
172812 for (i = 0; i < 2; i++) {
366 115208 dst->motion_val[i] = src->motion_val[i];
367 115208 dst->ref_index[i] = src->ref_index[i];
368 }
369
370 57604 dst->alloc_mb_width = src->alloc_mb_width;
371 57604 dst->alloc_mb_height = src->alloc_mb_height;
372 57604 dst->alloc_mb_stride = src->alloc_mb_stride;
373
374 57604 return 0;
375 }
376
377 57604 int ff_mpeg_ref_picture(AVCodecContext *avctx, Picture *dst, Picture *src)
378 {
379 int ret;
380
381
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57604 times.
57604 av_assert0(!dst->f->buf[0]);
382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57604 times.
57604 av_assert0(src->f->buf[0]);
383
384 57604 src->tf.f = src->f;
385 57604 dst->tf.f = dst->f;
386 57604 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57604 times.
57604 if (ret < 0)
388 goto fail;
389
390 57604 ret = ff_update_picture_tables(dst, src);
391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57604 times.
57604 if (ret < 0)
392 goto fail;
393
394
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57604 times.
57604 if (src->hwaccel_picture_private) {
395 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
396 if (!dst->hwaccel_priv_buf) {
397 ret = AVERROR(ENOMEM);
398 goto fail;
399 }
400 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
401 }
402
403 57604 dst->field_picture = src->field_picture;
404 57604 dst->mb_var_sum = src->mb_var_sum;
405 57604 dst->mc_mb_var_sum = src->mc_mb_var_sum;
406 57604 dst->b_frame_score = src->b_frame_score;
407 57604 dst->needs_realloc = src->needs_realloc;
408 57604 dst->reference = src->reference;
409 57604 dst->shared = src->shared;
410
411 57604 memcpy(dst->encoding_error, src->encoding_error,
412 sizeof(dst->encoding_error));
413
414 57604 return 0;
415 fail:
416 ff_mpeg_unref_picture(avctx, dst);
417 return ret;
418 }
419
420 41707 static inline int pic_is_unused(Picture *pic)
421 {
422
2/2
✓ Branch 0 taken 19053 times.
✓ Branch 1 taken 22654 times.
41707 if (!pic->f->buf[0])
423 19053 return 1;
424
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 22634 times.
22654 if (pic->needs_realloc)
425 20 return 1;
426 22634 return 0;
427 }
428
429 19722 static int find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
430 {
431 int i;
432
433
2/2
✓ Branch 0 taken 649 times.
✓ Branch 1 taken 19073 times.
19722 if (shared) {
434
1/2
✓ Branch 0 taken 852 times.
✗ Branch 1 not taken.
852 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
435
2/2
✓ Branch 0 taken 649 times.
✓ Branch 1 taken 203 times.
852 if (!picture[i].f->buf[0])
436 649 return i;
437 }
438 } else {
439
1/2
✓ Branch 0 taken 41707 times.
✗ Branch 1 not taken.
41707 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
440
2/2
✓ Branch 1 taken 19073 times.
✓ Branch 2 taken 22634 times.
41707 if (pic_is_unused(&picture[i]))
441 19073 return i;
442 }
443 }
444
445 av_log(avctx, AV_LOG_FATAL,
446 "Internal error, picture buffer overflow\n");
447 /* We could return -1, but the codec would crash trying to draw into a
448 * non-existing frame anyway. This is safer than waiting for a random crash.
449 * Also the return of this is never useful, an encoder must only allocate
450 * as much as allowed in the specification. This has no relationship to how
451 * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
452 * enough for such valid streams).
453 * Plus, a decoder has to check stream validity and remove frames if too
454 * many reference frames are around. Waiting for "OOM" is not correct at
455 * all. Similarly, missing reference frames have to be replaced by
456 * interpolated/MC frames, anything else is a bug in the codec ...
457 */
458 abort();
459 return -1;
460 }
461
462 19722 int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
463 {
464 19722 int ret = find_unused_picture(avctx, picture, shared);
465
466
2/4
✓ Branch 0 taken 19722 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19722 times.
✗ Branch 3 not taken.
19722 if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
467
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 19702 times.
19722 if (picture[ret].needs_realloc) {
468 20 ff_mpeg_unref_picture(avctx, &picture[ret]);
469 }
470 }
471 19722 return ret;
472 }
473
474 29781 void av_cold ff_mpv_picture_free(AVCodecContext *avctx, Picture *pic)
475 {
476 29781 free_picture_tables(pic);
477 29781 ff_mpeg_unref_picture(avctx, pic);
478 29781 av_frame_free(&pic->f);
479 29781 }
480