FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegpicture.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 201 252 79.8%
Branches: 128 190 67.4%

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