FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_dec.c
Date: 2022-07-06 18:02:43
Exec Total Coverage
Lines: 186 314 59.2%
Branches: 120 262 45.8%

Line Branch Exec Source
1 /*
2 * Common mpeg video decoding code
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <limits.h>
24
25 #include "libavutil/avassert.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/video_enc_params.h"
29
30 #include "avcodec.h"
31 #include "internal.h"
32 #include "mpegutils.h"
33 #include "mpegvideo.h"
34 #include "mpegvideodec.h"
35 #include "threadframe.h"
36
37 602 void ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx)
38 {
39 602 ff_mpv_common_defaults(s);
40
41 602 s->avctx = avctx;
42 602 s->width = avctx->coded_width;
43 602 s->height = avctx->coded_height;
44 602 s->codec_id = avctx->codec->id;
45 602 s->workaround_bugs = avctx->workaround_bugs;
46
47 /* convert fourcc to upper case */
48 602 s->codec_tag = ff_toupper4(avctx->codec_tag);
49 602 }
50
51 int ff_mpeg_update_thread_context(AVCodecContext *dst,
52 const AVCodecContext *src)
53 {
54 MpegEncContext *const s1 = src->priv_data;
55 MpegEncContext *const s = dst->priv_data;
56 int ret;
57
58 if (dst == src)
59 return 0;
60
61 av_assert0(s != s1);
62
63 // FIXME can parameters change on I-frames?
64 // in that case dst may need a reinit
65 if (!s->context_initialized) {
66 void *private_ctx = s->private_ctx;
67 int err;
68 memcpy(s, s1, sizeof(*s));
69
70 s->avctx = dst;
71 s->private_ctx = private_ctx;
72 s->bitstream_buffer = NULL;
73 s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
74
75 if (s1->context_initialized) {
76 // s->picture_range_start += MAX_PICTURE_COUNT;
77 // s->picture_range_end += MAX_PICTURE_COUNT;
78 ff_mpv_idct_init(s);
79 if ((err = ff_mpv_common_init(s)) < 0) {
80 memset(s, 0, sizeof(*s));
81 s->avctx = dst;
82 s->private_ctx = private_ctx;
83 return err;
84 }
85 }
86 }
87
88 if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
89 s->height = s1->height;
90 s->width = s1->width;
91 if ((ret = ff_mpv_common_frame_size_change(s)) < 0)
92 return ret;
93 }
94
95 s->avctx->coded_height = s1->avctx->coded_height;
96 s->avctx->coded_width = s1->avctx->coded_width;
97 s->avctx->width = s1->avctx->width;
98 s->avctx->height = s1->avctx->height;
99
100 s->quarter_sample = s1->quarter_sample;
101
102 s->coded_picture_number = s1->coded_picture_number;
103 s->picture_number = s1->picture_number;
104
105 av_assert0(!s->picture || s->picture != s1->picture);
106 if (s->picture)
107 for (int i = 0; i < MAX_PICTURE_COUNT; i++) {
108 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
109 if (s1->picture && s1->picture[i].f->buf[0] &&
110 (ret = ff_mpeg_ref_picture(s->avctx, &s->picture[i], &s1->picture[i])) < 0)
111 return ret;
112 }
113
114 #define UPDATE_PICTURE(pic)\
115 do {\
116 ff_mpeg_unref_picture(s->avctx, &s->pic);\
117 if (s1->pic.f && s1->pic.f->buf[0])\
118 ret = ff_mpeg_ref_picture(s->avctx, &s->pic, &s1->pic);\
119 else\
120 ret = ff_update_picture_tables(&s->pic, &s1->pic);\
121 if (ret < 0)\
122 return ret;\
123 } while (0)
124
125 UPDATE_PICTURE(current_picture);
126 UPDATE_PICTURE(last_picture);
127 UPDATE_PICTURE(next_picture);
128
129 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
130 ((pic && pic >= old_ctx->picture && \
131 pic < old_ctx->picture + MAX_PICTURE_COUNT) ? \
132 &new_ctx->picture[pic - old_ctx->picture] : NULL)
133
134 s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
135 s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
136 s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
137
138 // Error/bug resilience
139 s->workaround_bugs = s1->workaround_bugs;
140 s->padding_bug_score = s1->padding_bug_score;
141
142 // MPEG-4 timing info
143 memcpy(&s->last_time_base, &s1->last_time_base,
144 (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
145 (char *) &s1->last_time_base);
146
147 // B-frame info
148 s->max_b_frames = s1->max_b_frames;
149 s->low_delay = s1->low_delay;
150 s->droppable = s1->droppable;
151
152 // DivX handling (doesn't work)
153 s->divx_packed = s1->divx_packed;
154
155 if (s1->bitstream_buffer) {
156 if (s1->bitstream_buffer_size +
157 AV_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size) {
158 av_fast_malloc(&s->bitstream_buffer,
159 &s->allocated_bitstream_buffer_size,
160 s1->allocated_bitstream_buffer_size);
161 if (!s->bitstream_buffer) {
162 s->bitstream_buffer_size = 0;
163 return AVERROR(ENOMEM);
164 }
165 }
166 s->bitstream_buffer_size = s1->bitstream_buffer_size;
167 memcpy(s->bitstream_buffer, s1->bitstream_buffer,
168 s1->bitstream_buffer_size);
169 memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
170 AV_INPUT_BUFFER_PADDING_SIZE);
171 }
172
173 // linesize-dependent scratch buffer allocation
174 if (!s->sc.edge_emu_buffer)
175 if (s1->linesize) {
176 if (ff_mpeg_framesize_alloc(s->avctx, &s->me,
177 &s->sc, s1->linesize) < 0) {
178 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
179 "scratch buffers.\n");
180 return AVERROR(ENOMEM);
181 }
182 } else {
183 av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
184 "be allocated due to unknown size.\n");
185 }
186
187 // MPEG-2/interlacing info
188 memcpy(&s->progressive_sequence, &s1->progressive_sequence,
189 (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
190
191 return 0;
192 }
193
194 41 int ff_mpv_common_frame_size_change(MpegEncContext *s)
195 {
196 41 int err = 0;
197
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 if (!s->context_initialized)
199 return AVERROR(EINVAL);
200
201 41 ff_mpv_free_context_frame(s);
202
203
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 if (s->picture)
204
2/2
✓ Branch 0 taken 1476 times.
✓ Branch 1 taken 41 times.
1517 for (int i = 0; i < MAX_PICTURE_COUNT; i++)
205 1476 s->picture[i].needs_realloc = 1;
206
207 41 s->last_picture_ptr =
208 41 s->next_picture_ptr =
209 41 s->current_picture_ptr = NULL;
210
211 // init
212
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
41 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
213 s->mb_height = (s->height + 31) / 32 * 2;
214 else
215 41 s->mb_height = (s->height + 15) / 16;
216
217
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 41 times.
82 if ((s->width || s->height) &&
218 41 (err = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
219 goto fail;
220
221 /* set chroma shifts */
222 41 err = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
223 &s->chroma_x_shift,
224 &s->chroma_y_shift);
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 if (err < 0)
226 goto fail;
227
228
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
41 if ((err = ff_mpv_init_context_frame(s)))
229 goto fail;
230
231 41 memset(s->thread_context, 0, sizeof(s->thread_context));
232 41 s->thread_context[0] = s;
233
234
2/4
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
41 if (s->width && s->height) {
235 41 err = ff_mpv_init_duplicate_contexts(s);
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 if (err < 0)
237 goto fail;
238 }
239 41 s->context_reinit = 0;
240
241 41 return 0;
242 fail:
243 ff_mpv_free_context_frame(s);
244 s->context_reinit = 1;
245 return err;
246 }
247
248 10802 static int alloc_picture(MpegEncContext *s, Picture *pic)
249 {
250 21604 return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, 0, 0,
251 10802 s->chroma_x_shift, s->chroma_y_shift, s->out_format,
252 s->mb_stride, s->mb_width, s->mb_height, s->b8_stride,
253 &s->linesize, &s->uvlinesize);
254 }
255
256 static void gray_frame(AVFrame *frame)
257 {
258 int h_chroma_shift, v_chroma_shift;
259
260 av_pix_fmt_get_chroma_sub_sample(frame->format, &h_chroma_shift, &v_chroma_shift);
261
262 for (int i = 0; i < frame->height; i++)
263 memset(frame->data[0] + frame->linesize[0] * i, 0x80, frame->width);
264 for (int i = 0; i < AV_CEIL_RSHIFT(frame->height, v_chroma_shift); i++) {
265 memset(frame->data[1] + frame->linesize[1] * i,
266 0x80, AV_CEIL_RSHIFT(frame->width, h_chroma_shift));
267 memset(frame->data[2] + frame->linesize[2] * i,
268 0x80, AV_CEIL_RSHIFT(frame->width, h_chroma_shift));
269 }
270 }
271
272 /**
273 * generic function called after decoding
274 * the header and before a frame is decoded.
275 */
276 10796 int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
277 {
278 Picture *pic;
279 int idx, ret;
280
281 10796 s->mb_skipped = 0;
282
283
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10796 times.
10796 if (!ff_thread_can_start_frame(avctx)) {
284 av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
285 return -1;
286 }
287
288 /* mark & release old frames */
289
4/4
✓ Branch 0 taken 8240 times.
✓ Branch 1 taken 2556 times.
✓ Branch 2 taken 7741 times.
✓ Branch 3 taken 499 times.
10796 if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
290
1/2
✓ Branch 0 taken 7741 times.
✗ Branch 1 not taken.
7741 s->last_picture_ptr != s->next_picture_ptr &&
291
1/2
✓ Branch 0 taken 7741 times.
✗ Branch 1 not taken.
7741 s->last_picture_ptr->f->buf[0]) {
292 7741 ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
293 }
294
295 /* release forgotten pictures */
296 /* if (MPEG-124 / H.263) */
297
2/2
✓ Branch 0 taken 388656 times.
✓ Branch 1 taken 10796 times.
399452 for (int i = 0; i < MAX_PICTURE_COUNT; i++) {
298
2/2
✓ Branch 0 taken 378359 times.
✓ Branch 1 taken 10297 times.
388656 if (&s->picture[i] != s->last_picture_ptr &&
299
2/2
✓ Branch 0 taken 367858 times.
✓ Branch 1 taken 10501 times.
378359 &s->picture[i] != s->next_picture_ptr &&
300
3/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 367838 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
367858 s->picture[i].reference && !s->picture[i].needs_realloc) {
301 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
302 }
303 }
304
305 10796 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
306 10796 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
307 10796 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
308
309 /* release non reference frames */
310
2/2
✓ Branch 0 taken 388656 times.
✓ Branch 1 taken 10796 times.
399452 for (int i = 0; i < MAX_PICTURE_COUNT; i++) {
311
2/2
✓ Branch 0 taken 375579 times.
✓ Branch 1 taken 13077 times.
388656 if (!s->picture[i].reference)
312 375579 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
313 }
314
315
4/4
✓ Branch 0 taken 6757 times.
✓ Branch 1 taken 4039 times.
✓ Branch 2 taken 5895 times.
✓ Branch 3 taken 862 times.
10796 if (s->current_picture_ptr && !s->current_picture_ptr->f->buf[0]) {
316 // we already have an unused image
317 // (maybe it was set before reading the header)
318 5895 pic = s->current_picture_ptr;
319 } else {
320 4901 idx = ff_find_unused_picture(s->avctx, s->picture, 0);
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4901 times.
4901 if (idx < 0) {
322 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
323 return idx;
324 }
325 4901 pic = &s->picture[idx];
326 }
327
328 10796 pic->reference = 0;
329
1/2
✓ Branch 0 taken 10796 times.
✗ Branch 1 not taken.
10796 if (!s->droppable) {
330
2/2
✓ Branch 0 taken 8240 times.
✓ Branch 1 taken 2556 times.
10796 if (s->pict_type != AV_PICTURE_TYPE_B)
331 8240 pic->reference = 3;
332 }
333
334 10796 pic->f->coded_picture_number = s->coded_picture_number++;
335
336
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10796 times.
10796 if (alloc_picture(s, pic) < 0)
337 return -1;
338
339 10796 s->current_picture_ptr = pic;
340 // FIXME use only the vars from current_pic
341 10796 s->current_picture_ptr->f->top_field_first = s->top_field_first;
342
2/2
✓ Branch 0 taken 10316 times.
✓ Branch 1 taken 480 times.
10796 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
343
2/2
✓ Branch 0 taken 3058 times.
✓ Branch 1 taken 7258 times.
10316 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
344
2/2
✓ Branch 0 taken 542 times.
✓ Branch 1 taken 2996 times.
3538 if (s->picture_structure != PICT_FRAME)
345 542 s->current_picture_ptr->f->top_field_first =
346 542 (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
347 }
348
2/2
✓ Branch 0 taken 1977 times.
✓ Branch 1 taken 8819 times.
12773 s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame &&
349
1/2
✓ Branch 0 taken 1977 times.
✗ Branch 1 not taken.
1977 !s->progressive_sequence;
350 10796 s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
351
352 10796 s->current_picture_ptr->f->pict_type = s->pict_type;
353 10796 s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
354
355
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10796 times.
10796 if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
356 s->current_picture_ptr)) < 0)
357 return ret;
358
359
2/2
✓ Branch 0 taken 8240 times.
✓ Branch 1 taken 2556 times.
10796 if (s->pict_type != AV_PICTURE_TYPE_B) {
360 8240 s->last_picture_ptr = s->next_picture_ptr;
361
1/2
✓ Branch 0 taken 8240 times.
✗ Branch 1 not taken.
8240 if (!s->droppable)
362 8240 s->next_picture_ptr = s->current_picture_ptr;
363 }
364 ff_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
365 s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
366 s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
367 s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
368 s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
369 s->pict_type, s->droppable);
370
371
3/4
✓ Branch 0 taken 10501 times.
✓ Branch 1 taken 295 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10501 times.
10796 if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) &&
372
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 289 times.
295 (s->pict_type != AV_PICTURE_TYPE_I)) {
373 int h_chroma_shift, v_chroma_shift;
374 6 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
375 &h_chroma_shift, &v_chroma_shift);
376
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
6 if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture_ptr && s->next_picture_ptr->f->buf[0])
377 av_log(avctx, AV_LOG_DEBUG,
378 "allocating dummy last picture for B frame\n");
379
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 else if (s->pict_type != AV_PICTURE_TYPE_I)
380 6 av_log(avctx, AV_LOG_ERROR,
381 "warning: first frame is no keyframe\n");
382
383 /* Allocate a dummy frame */
384 6 idx = ff_find_unused_picture(s->avctx, s->picture, 0);
385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (idx < 0) {
386 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
387 return idx;
388 }
389 6 s->last_picture_ptr = &s->picture[idx];
390
391 6 s->last_picture_ptr->reference = 3;
392 6 s->last_picture_ptr->f->key_frame = 0;
393 6 s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
394
395
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (alloc_picture(s, s->last_picture_ptr) < 0) {
396 s->last_picture_ptr = NULL;
397 return -1;
398 }
399
400
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (!avctx->hwaccel) {
401
2/2
✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 6 times.
1734 for (int i = 0; i < avctx->height; i++)
402 1728 memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i,
403 1728 0x80, avctx->width);
404
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (s->last_picture_ptr->f->data[2]) {
405
2/2
✓ Branch 0 taken 864 times.
✓ Branch 1 taken 6 times.
870 for (int i = 0; i < AV_CEIL_RSHIFT(avctx->height, v_chroma_shift); i++) {
406 864 memset(s->last_picture_ptr->f->data[1] + s->last_picture_ptr->f->linesize[1]*i,
407 864 0x80, AV_CEIL_RSHIFT(avctx->width, h_chroma_shift));
408 864 memset(s->last_picture_ptr->f->data[2] + s->last_picture_ptr->f->linesize[2]*i,
409 864 0x80, AV_CEIL_RSHIFT(avctx->width, h_chroma_shift));
410 }
411 }
412
413
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263) {
414 for (int i = 0; i < avctx->height; i++)
415 memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0] * i,
416 16, avctx->width);
417 }
418 }
419
420 6 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
421 6 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
422 }
423
2/4
✓ Branch 0 taken 10796 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10796 times.
10796 if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
424 s->pict_type == AV_PICTURE_TYPE_B) {
425 /* Allocate a dummy frame */
426 idx = ff_find_unused_picture(s->avctx, s->picture, 0);
427 if (idx < 0) {
428 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
429 return idx;
430 }
431 s->next_picture_ptr = &s->picture[idx];
432
433 s->next_picture_ptr->reference = 3;
434 s->next_picture_ptr->f->key_frame = 0;
435 s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
436
437 if (alloc_picture(s, s->next_picture_ptr) < 0) {
438 s->next_picture_ptr = NULL;
439 return -1;
440 }
441 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
442 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
443 }
444
445 #if 0 // BUFREF-FIXME
446 memset(s->last_picture.f->data, 0, sizeof(s->last_picture.f->data));
447 memset(s->next_picture.f->data, 0, sizeof(s->next_picture.f->data));
448 #endif
449
2/2
✓ Branch 0 taken 10507 times.
✓ Branch 1 taken 289 times.
10796 if (s->last_picture_ptr) {
450
2/4
✓ Branch 0 taken 10507 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10507 times.
21014 if (s->last_picture_ptr->f->buf[0] &&
451 10507 (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
452 s->last_picture_ptr)) < 0)
453 return ret;
454 }
455
1/2
✓ Branch 0 taken 10796 times.
✗ Branch 1 not taken.
10796 if (s->next_picture_ptr) {
456
2/4
✓ Branch 0 taken 10796 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10796 times.
21592 if (s->next_picture_ptr->f->buf[0] &&
457 10796 (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
458 s->next_picture_ptr)) < 0)
459 return ret;
460 }
461
462
4/6
✓ Branch 0 taken 9611 times.
✓ Branch 1 taken 1185 times.
✓ Branch 2 taken 9611 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 9611 times.
10796 av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
463 s->last_picture_ptr->f->buf[0]));
464
465
2/2
✓ Branch 0 taken 542 times.
✓ Branch 1 taken 10254 times.
10796 if (s->picture_structure != PICT_FRAME) {
466
2/2
✓ Branch 0 taken 2168 times.
✓ Branch 1 taken 542 times.
2710 for (int i = 0; i < 4; i++) {
467
2/2
✓ Branch 0 taken 2164 times.
✓ Branch 1 taken 4 times.
2168 if (s->picture_structure == PICT_BOTTOM_FIELD) {
468 2164 s->current_picture.f->data[i] +=
469 2164 s->current_picture.f->linesize[i];
470 }
471 2168 s->current_picture.f->linesize[i] *= 2;
472 2168 s->last_picture.f->linesize[i] *= 2;
473 2168 s->next_picture.f->linesize[i] *= 2;
474 }
475 }
476
477 /* set dequantizer, we can't do it during init as
478 * it might change for MPEG-4 and we can't do it in the header
479 * decode as init is not called for MPEG-4 there yet */
480
4/4
✓ Branch 0 taken 10756 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 3058 times.
✓ Branch 3 taken 7698 times.
10796 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
481 3098 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
482 3098 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
483
4/4
✓ Branch 0 taken 780 times.
✓ Branch 1 taken 6918 times.
✓ Branch 2 taken 300 times.
✓ Branch 3 taken 480 times.
7698 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
484 7218 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
485 7218 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
486 } else {
487 480 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
488 480 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
489 }
490
491
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10796 times.
10796 if (s->avctx->debug & FF_DEBUG_NOMC)
492 gray_frame(s->current_picture_ptr->f);
493
494 10796 return 0;
495 }
496
497 /* called after a frame has been decoded. */
498 10796 void ff_mpv_frame_end(MpegEncContext *s)
499 {
500 10796 emms_c();
501
502
2/2
✓ Branch 0 taken 8240 times.
✓ Branch 1 taken 2556 times.
10796 if (s->current_picture.reference)
503 8240 ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
504 10796 }
505
506 10578 void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
507 {
508 10578 ff_print_debug_info2(s->avctx, pict, s->mbskip_table, p->mb_type,
509 10578 p->qscale_table, p->motion_val,
510 s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
511 10578 }
512
513 9845 int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
514 {
515 AVVideoEncParams *par;
516
2/2
✓ Branch 0 taken 6366 times.
✓ Branch 1 taken 3479 times.
9845 int mult = (qp_type == FF_MPV_QSCALE_TYPE_MPEG1) ? 2 : 1;
517 9845 unsigned int nb_mb = p->alloc_mb_height * p->alloc_mb_width;
518
519
2/2
✓ Branch 0 taken 9830 times.
✓ Branch 1 taken 15 times.
9845 if (!(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS))
520 9830 return 0;
521
522 15 par = av_video_enc_params_create_side_data(f, AV_VIDEO_ENC_PARAMS_MPEG2, nb_mb);
523
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!par)
524 return AVERROR(ENOMEM);
525
526
2/2
✓ Branch 0 taken 270 times.
✓ Branch 1 taken 15 times.
285 for (unsigned y = 0; y < p->alloc_mb_height; y++)
527
2/2
✓ Branch 0 taken 5940 times.
✓ Branch 1 taken 270 times.
6210 for (unsigned x = 0; x < p->alloc_mb_width; x++) {
528 5940 const unsigned int block_idx = y * p->alloc_mb_width + x;
529 5940 const unsigned int mb_xy = y * p->alloc_mb_stride + x;
530 5940 AVVideoBlockParams *const b = av_video_enc_params_block(par, block_idx);
531
532 5940 b->src_x = x * 16;
533 5940 b->src_y = y * 16;
534 5940 b->w = 16;
535 5940 b->h = 16;
536
537 5940 b->delta_qp = p->qscale_table[mb_xy] * mult;
538 }
539
540 15 return 0;
541 }
542
543 166844 void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
544 {
545 166844 ff_draw_horiz_band(s->avctx, s->current_picture_ptr->f,
546
2/2
✓ Branch 0 taken 162441 times.
✓ Branch 1 taken 4403 times.
166844 s->last_picture_ptr ? s->last_picture_ptr->f : NULL,
547 y, h, s->picture_structure,
548 s->first_field, s->low_delay);
549 166844 }
550
551 79 void ff_mpeg_flush(AVCodecContext *avctx)
552 {
553 79 MpegEncContext *const s = avctx->priv_data;
554
555
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 if (!s->picture)
556 return;
557
558
2/2
✓ Branch 0 taken 2844 times.
✓ Branch 1 taken 79 times.
2923 for (int i = 0; i < MAX_PICTURE_COUNT; i++)
559 2844 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
560 79 s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
561
562 79 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
563 79 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
564 79 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
565
566 79 s->mb_x = s->mb_y = 0;
567
568 #if FF_API_FLAG_TRUNCATED
569 79 s->parse_context.state = -1;
570 79 s->parse_context.frame_start_found = 0;
571 79 s->parse_context.overread = 0;
572 79 s->parse_context.overread_index = 0;
573 79 s->parse_context.index = 0;
574 79 s->parse_context.last_index = 0;
575 #endif
576 79 s->bitstream_buffer_size = 0;
577 79 s->pp_time = 0;
578 }
579
580 157076 void ff_mpv_report_decode_progress(MpegEncContext *s)
581 {
582
5/6
✓ Branch 0 taken 120532 times.
✓ Branch 1 taken 36544 times.
✓ Branch 2 taken 113949 times.
✓ Branch 3 taken 6583 times.
✓ Branch 4 taken 113949 times.
✗ Branch 5 not taken.
157076 if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
583 113949 ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
584 157076 }
585