FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_dec.c
Date: 2022-01-18 10:05:52
Exec Total Coverage
Lines: 186 311 59.8%
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 "thread.h"
35
36 590 void ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx)
37 {
38 590 ff_mpv_common_defaults(s);
39
40 590 s->avctx = avctx;
41 590 s->width = avctx->coded_width;
42 590 s->height = avctx->coded_height;
43 590 s->codec_id = avctx->codec->id;
44 590 s->workaround_bugs = avctx->workaround_bugs;
45
46 /* convert fourcc to upper case */
47 590 s->codec_tag = ff_toupper4(avctx->codec_tag);
48 590 }
49
50 int ff_mpeg_update_thread_context(AVCodecContext *dst,
51 const AVCodecContext *src)
52 {
53 MpegEncContext *const s1 = src->priv_data;
54 MpegEncContext *const s = dst->priv_data;
55 int ret;
56
57 if (dst == src)
58 return 0;
59
60 av_assert0(s != s1);
61
62 // FIXME can parameters change on I-frames?
63 // in that case dst may need a reinit
64 if (!s->context_initialized) {
65 int err;
66 memcpy(s, s1, sizeof(*s));
67
68 s->avctx = dst;
69 s->bitstream_buffer = NULL;
70 s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
71
72 if (s1->context_initialized) {
73 // s->picture_range_start += MAX_PICTURE_COUNT;
74 // s->picture_range_end += MAX_PICTURE_COUNT;
75 ff_mpv_idct_init(s);
76 if ((err = ff_mpv_common_init(s)) < 0) {
77 memset(s, 0, sizeof(*s));
78 s->avctx = dst;
79 return err;
80 }
81 }
82 }
83
84 if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
85 s->height = s1->height;
86 s->width = s1->width;
87 if ((ret = ff_mpv_common_frame_size_change(s)) < 0)
88 return ret;
89 }
90
91 s->avctx->coded_height = s1->avctx->coded_height;
92 s->avctx->coded_width = s1->avctx->coded_width;
93 s->avctx->width = s1->avctx->width;
94 s->avctx->height = s1->avctx->height;
95
96 s->quarter_sample = s1->quarter_sample;
97
98 s->coded_picture_number = s1->coded_picture_number;
99 s->picture_number = s1->picture_number;
100
101 av_assert0(!s->picture || s->picture != s1->picture);
102 if (s->picture)
103 for (int i = 0; i < MAX_PICTURE_COUNT; i++) {
104 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
105 if (s1->picture && s1->picture[i].f->buf[0] &&
106 (ret = ff_mpeg_ref_picture(s->avctx, &s->picture[i], &s1->picture[i])) < 0)
107 return ret;
108 }
109
110 #define UPDATE_PICTURE(pic)\
111 do {\
112 ff_mpeg_unref_picture(s->avctx, &s->pic);\
113 if (s1->pic.f && s1->pic.f->buf[0])\
114 ret = ff_mpeg_ref_picture(s->avctx, &s->pic, &s1->pic);\
115 else\
116 ret = ff_update_picture_tables(&s->pic, &s1->pic);\
117 if (ret < 0)\
118 return ret;\
119 } while (0)
120
121 UPDATE_PICTURE(current_picture);
122 UPDATE_PICTURE(last_picture);
123 UPDATE_PICTURE(next_picture);
124
125 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
126 ((pic && pic >= old_ctx->picture && \
127 pic < old_ctx->picture + MAX_PICTURE_COUNT) ? \
128 &new_ctx->picture[pic - old_ctx->picture] : NULL)
129
130 s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
131 s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
132 s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
133
134 // Error/bug resilience
135 s->workaround_bugs = s1->workaround_bugs;
136 s->padding_bug_score = s1->padding_bug_score;
137
138 // MPEG-4 timing info
139 memcpy(&s->last_time_base, &s1->last_time_base,
140 (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
141 (char *) &s1->last_time_base);
142
143 // B-frame info
144 s->max_b_frames = s1->max_b_frames;
145 s->low_delay = s1->low_delay;
146 s->droppable = s1->droppable;
147
148 // DivX handling (doesn't work)
149 s->divx_packed = s1->divx_packed;
150
151 if (s1->bitstream_buffer) {
152 if (s1->bitstream_buffer_size +
153 AV_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size) {
154 av_fast_malloc(&s->bitstream_buffer,
155 &s->allocated_bitstream_buffer_size,
156 s1->allocated_bitstream_buffer_size);
157 if (!s->bitstream_buffer) {
158 s->bitstream_buffer_size = 0;
159 return AVERROR(ENOMEM);
160 }
161 }
162 s->bitstream_buffer_size = s1->bitstream_buffer_size;
163 memcpy(s->bitstream_buffer, s1->bitstream_buffer,
164 s1->bitstream_buffer_size);
165 memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
166 AV_INPUT_BUFFER_PADDING_SIZE);
167 }
168
169 // linesize-dependent scratch buffer allocation
170 if (!s->sc.edge_emu_buffer)
171 if (s1->linesize) {
172 if (ff_mpeg_framesize_alloc(s->avctx, &s->me,
173 &s->sc, s1->linesize) < 0) {
174 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
175 "scratch buffers.\n");
176 return AVERROR(ENOMEM);
177 }
178 } else {
179 av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
180 "be allocated due to unknown size.\n");
181 }
182
183 // MPEG-2/interlacing info
184 memcpy(&s->progressive_sequence, &s1->progressive_sequence,
185 (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
186
187 return 0;
188 }
189
190 39 int ff_mpv_common_frame_size_change(MpegEncContext *s)
191 {
192 39 int err = 0;
193
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (!s->context_initialized)
195 return AVERROR(EINVAL);
196
197 39 ff_mpv_free_context_frame(s);
198
199
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 if (s->picture)
200
2/2
✓ Branch 0 taken 1404 times.
✓ Branch 1 taken 39 times.
1443 for (int i = 0; i < MAX_PICTURE_COUNT; i++)
201 1404 s->picture[i].needs_realloc = 1;
202
203 39 s->last_picture_ptr =
204 39 s->next_picture_ptr =
205 39 s->current_picture_ptr = NULL;
206
207 // init
208
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
39 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
209 s->mb_height = (s->height + 31) / 32 * 2;
210 else
211 39 s->mb_height = (s->height + 15) / 16;
212
213
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 39 times.
78 if ((s->width || s->height) &&
214 39 (err = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
215 goto fail;
216
217 /* set chroma shifts */
218 39 err = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
219 &s->chroma_x_shift,
220 &s->chroma_y_shift);
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (err < 0)
222 goto fail;
223
224
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
39 if ((err = ff_mpv_init_context_frame(s)))
225 goto fail;
226
227 39 memset(s->thread_context, 0, sizeof(s->thread_context));
228 39 s->thread_context[0] = s;
229
230
2/4
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
✗ Branch 3 not taken.
39 if (s->width && s->height) {
231 39 err = ff_mpv_init_duplicate_contexts(s);
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (err < 0)
233 goto fail;
234 }
235 39 s->context_reinit = 0;
236
237 39 return 0;
238 fail:
239 ff_mpv_free_context_frame(s);
240 s->context_reinit = 1;
241 return err;
242 }
243
244 10666 static int alloc_picture(MpegEncContext *s, Picture *pic)
245 {
246 21332 return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, 0, 0,
247 10666 s->chroma_x_shift, s->chroma_y_shift, s->out_format,
248 s->mb_stride, s->mb_width, s->mb_height, s->b8_stride,
249 &s->linesize, &s->uvlinesize);
250 }
251
252 static void gray_frame(AVFrame *frame)
253 {
254 int h_chroma_shift, v_chroma_shift;
255
256 av_pix_fmt_get_chroma_sub_sample(frame->format, &h_chroma_shift, &v_chroma_shift);
257
258 for (int i = 0; i < frame->height; i++)
259 memset(frame->data[0] + frame->linesize[0] * i, 0x80, frame->width);
260 for (int i = 0; i < AV_CEIL_RSHIFT(frame->height, v_chroma_shift); i++) {
261 memset(frame->data[1] + frame->linesize[1] * i,
262 0x80, AV_CEIL_RSHIFT(frame->width, h_chroma_shift));
263 memset(frame->data[2] + frame->linesize[2] * i,
264 0x80, AV_CEIL_RSHIFT(frame->width, h_chroma_shift));
265 }
266 }
267
268 /**
269 * generic function called after decoding
270 * the header and before a frame is decoded.
271 */
272 10660 int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
273 {
274 Picture *pic;
275 int idx, ret;
276
277 10660 s->mb_skipped = 0;
278
279
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10660 times.
10660 if (!ff_thread_can_start_frame(avctx)) {
280 av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
281 return -1;
282 }
283
284 /* mark & release old frames */
285
4/4
✓ Branch 0 taken 8193 times.
✓ Branch 1 taken 2467 times.
✓ Branch 2 taken 7697 times.
✓ Branch 3 taken 496 times.
10660 if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
286
1/2
✓ Branch 0 taken 7697 times.
✗ Branch 1 not taken.
7697 s->last_picture_ptr != s->next_picture_ptr &&
287
1/2
✓ Branch 0 taken 7697 times.
✗ Branch 1 not taken.
7697 s->last_picture_ptr->f->buf[0]) {
288 7697 ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
289 }
290
291 /* release forgotten pictures */
292 /* if (MPEG-124 / H.263) */
293
2/2
✓ Branch 0 taken 383760 times.
✓ Branch 1 taken 10660 times.
394420 for (int i = 0; i < MAX_PICTURE_COUNT; i++) {
294
2/2
✓ Branch 0 taken 373596 times.
✓ Branch 1 taken 10164 times.
383760 if (&s->picture[i] != s->last_picture_ptr &&
295
2/2
✓ Branch 0 taken 363229 times.
✓ Branch 1 taken 10367 times.
373596 &s->picture[i] != s->next_picture_ptr &&
296
3/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 363209 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
363229 s->picture[i].reference && !s->picture[i].needs_realloc) {
297 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
298 }
299 }
300
301 10660 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
302 10660 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
303 10660 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
304
305 /* release non reference frames */
306
2/2
✓ Branch 0 taken 383760 times.
✓ Branch 1 taken 10660 times.
394420 for (int i = 0; i < MAX_PICTURE_COUNT; i++) {
307
2/2
✓ Branch 0 taken 370906 times.
✓ Branch 1 taken 12854 times.
383760 if (!s->picture[i].reference)
308 370906 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
309 }
310
311
4/4
✓ Branch 0 taken 6756 times.
✓ Branch 1 taken 3904 times.
✓ Branch 2 taken 5895 times.
✓ Branch 3 taken 861 times.
10660 if (s->current_picture_ptr && !s->current_picture_ptr->f->buf[0]) {
312 // we already have an unused image
313 // (maybe it was set before reading the header)
314 5895 pic = s->current_picture_ptr;
315 } else {
316 4765 idx = ff_find_unused_picture(s->avctx, s->picture, 0);
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4765 times.
4765 if (idx < 0) {
318 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
319 return idx;
320 }
321 4765 pic = &s->picture[idx];
322 }
323
324 10660 pic->reference = 0;
325
1/2
✓ Branch 0 taken 10660 times.
✗ Branch 1 not taken.
10660 if (!s->droppable) {
326
2/2
✓ Branch 0 taken 8193 times.
✓ Branch 1 taken 2467 times.
10660 if (s->pict_type != AV_PICTURE_TYPE_B)
327 8193 pic->reference = 3;
328 }
329
330 10660 pic->f->coded_picture_number = s->coded_picture_number++;
331
332
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10660 times.
10660 if (alloc_picture(s, pic) < 0)
333 return -1;
334
335 10660 s->current_picture_ptr = pic;
336 // FIXME use only the vars from current_pic
337 10660 s->current_picture_ptr->f->top_field_first = s->top_field_first;
338
2/2
✓ Branch 0 taken 10180 times.
✓ Branch 1 taken 480 times.
10660 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
339
2/2
✓ Branch 0 taken 2923 times.
✓ Branch 1 taken 7257 times.
10180 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
340
2/2
✓ Branch 0 taken 542 times.
✓ Branch 1 taken 2861 times.
3403 if (s->picture_structure != PICT_FRAME)
341 542 s->current_picture_ptr->f->top_field_first =
342 542 (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
343 }
344
2/2
✓ Branch 0 taken 1842 times.
✓ Branch 1 taken 8818 times.
12502 s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame &&
345
1/2
✓ Branch 0 taken 1842 times.
✗ Branch 1 not taken.
1842 !s->progressive_sequence;
346 10660 s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
347
348 10660 s->current_picture_ptr->f->pict_type = s->pict_type;
349 // if (s->avctx->flags && AV_CODEC_FLAG_QSCALE)
350 // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
351 10660 s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
352
353
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10660 times.
10660 if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
354 s->current_picture_ptr)) < 0)
355 return ret;
356
357
2/2
✓ Branch 0 taken 8193 times.
✓ Branch 1 taken 2467 times.
10660 if (s->pict_type != AV_PICTURE_TYPE_B) {
358 8193 s->last_picture_ptr = s->next_picture_ptr;
359
1/2
✓ Branch 0 taken 8193 times.
✗ Branch 1 not taken.
8193 if (!s->droppable)
360 8193 s->next_picture_ptr = s->current_picture_ptr;
361 }
362 ff_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
363 s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
364 s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
365 s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
366 s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
367 s->pict_type, s->droppable);
368
369
3/4
✓ Branch 0 taken 10367 times.
✓ Branch 1 taken 293 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10367 times.
10660 if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) &&
370
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 287 times.
293 (s->pict_type != AV_PICTURE_TYPE_I)) {
371 int h_chroma_shift, v_chroma_shift;
372 6 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
373 &h_chroma_shift, &v_chroma_shift);
374
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])
375 av_log(avctx, AV_LOG_DEBUG,
376 "allocating dummy last picture for B frame\n");
377
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 else if (s->pict_type != AV_PICTURE_TYPE_I)
378 6 av_log(avctx, AV_LOG_ERROR,
379 "warning: first frame is no keyframe\n");
380
381 /* Allocate a dummy frame */
382 6 idx = ff_find_unused_picture(s->avctx, s->picture, 0);
383
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (idx < 0) {
384 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
385 return idx;
386 }
387 6 s->last_picture_ptr = &s->picture[idx];
388
389 6 s->last_picture_ptr->reference = 3;
390 6 s->last_picture_ptr->f->key_frame = 0;
391 6 s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
392
393
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (alloc_picture(s, s->last_picture_ptr) < 0) {
394 s->last_picture_ptr = NULL;
395 return -1;
396 }
397
398
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (!avctx->hwaccel) {
399
2/2
✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 6 times.
1734 for (int i = 0; i < avctx->height; i++)
400 1728 memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i,
401 1728 0x80, avctx->width);
402
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (s->last_picture_ptr->f->data[2]) {
403
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++) {
404 864 memset(s->last_picture_ptr->f->data[1] + s->last_picture_ptr->f->linesize[1]*i,
405 864 0x80, AV_CEIL_RSHIFT(avctx->width, h_chroma_shift));
406 864 memset(s->last_picture_ptr->f->data[2] + s->last_picture_ptr->f->linesize[2]*i,
407 864 0x80, AV_CEIL_RSHIFT(avctx->width, h_chroma_shift));
408 }
409 }
410
411
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) {
412 for (int i = 0; i < avctx->height; i++)
413 memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0] * i,
414 16, avctx->width);
415 }
416 }
417
418 6 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
419 6 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
420 }
421
2/4
✓ Branch 0 taken 10660 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10660 times.
10660 if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
422 s->pict_type == AV_PICTURE_TYPE_B) {
423 /* Allocate a dummy frame */
424 idx = ff_find_unused_picture(s->avctx, s->picture, 0);
425 if (idx < 0) {
426 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
427 return idx;
428 }
429 s->next_picture_ptr = &s->picture[idx];
430
431 s->next_picture_ptr->reference = 3;
432 s->next_picture_ptr->f->key_frame = 0;
433 s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
434
435 if (alloc_picture(s, s->next_picture_ptr) < 0) {
436 s->next_picture_ptr = NULL;
437 return -1;
438 }
439 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
440 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
441 }
442
443 #if 0 // BUFREF-FIXME
444 memset(s->last_picture.f->data, 0, sizeof(s->last_picture.f->data));
445 memset(s->next_picture.f->data, 0, sizeof(s->next_picture.f->data));
446 #endif
447
2/2
✓ Branch 0 taken 10373 times.
✓ Branch 1 taken 287 times.
10660 if (s->last_picture_ptr) {
448
2/4
✓ Branch 0 taken 10373 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10373 times.
20746 if (s->last_picture_ptr->f->buf[0] &&
449 10373 (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
450 s->last_picture_ptr)) < 0)
451 return ret;
452 }
453
1/2
✓ Branch 0 taken 10660 times.
✗ Branch 1 not taken.
10660 if (s->next_picture_ptr) {
454
2/4
✓ Branch 0 taken 10660 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10660 times.
21320 if (s->next_picture_ptr->f->buf[0] &&
455 10660 (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
456 s->next_picture_ptr)) < 0)
457 return ret;
458 }
459
460
4/6
✓ Branch 0 taken 9486 times.
✓ Branch 1 taken 1174 times.
✓ Branch 2 taken 9486 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 9486 times.
10660 av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
461 s->last_picture_ptr->f->buf[0]));
462
463
2/2
✓ Branch 0 taken 542 times.
✓ Branch 1 taken 10118 times.
10660 if (s->picture_structure != PICT_FRAME) {
464
2/2
✓ Branch 0 taken 2168 times.
✓ Branch 1 taken 542 times.
2710 for (int i = 0; i < 4; i++) {
465
2/2
✓ Branch 0 taken 2164 times.
✓ Branch 1 taken 4 times.
2168 if (s->picture_structure == PICT_BOTTOM_FIELD) {
466 2164 s->current_picture.f->data[i] +=
467 2164 s->current_picture.f->linesize[i];
468 }
469 2168 s->current_picture.f->linesize[i] *= 2;
470 2168 s->last_picture.f->linesize[i] *= 2;
471 2168 s->next_picture.f->linesize[i] *= 2;
472 }
473 }
474
475 /* set dequantizer, we can't do it during init as
476 * it might change for MPEG-4 and we can't do it in the header
477 * decode as init is not called for MPEG-4 there yet */
478
4/4
✓ Branch 0 taken 10620 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 2923 times.
✓ Branch 3 taken 7697 times.
10660 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
479 2963 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
480 2963 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
481
4/4
✓ Branch 0 taken 780 times.
✓ Branch 1 taken 6917 times.
✓ Branch 2 taken 300 times.
✓ Branch 3 taken 480 times.
7697 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
482 7217 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
483 7217 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
484 } else {
485 480 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
486 480 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
487 }
488
489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10660 times.
10660 if (s->avctx->debug & FF_DEBUG_NOMC)
490 gray_frame(s->current_picture_ptr->f);
491
492 10660 return 0;
493 }
494
495 /* called after a frame has been decoded. */
496 10660 void ff_mpv_frame_end(MpegEncContext *s)
497 {
498 10660 emms_c();
499
500
2/2
✓ Branch 0 taken 8193 times.
✓ Branch 1 taken 2467 times.
10660 if (s->current_picture.reference)
501 8193 ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
502 10660 }
503
504 10443 void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
505 {
506 10443 ff_print_debug_info2(s->avctx, pict, s->mbskip_table, p->mb_type,
507 10443 p->qscale_table, p->motion_val,
508 s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
509 10443 }
510
511 9710 int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
512 {
513 AVVideoEncParams *par;
514
2/2
✓ Branch 0 taken 6365 times.
✓ Branch 1 taken 3345 times.
9710 int mult = (qp_type == FF_QSCALE_TYPE_MPEG1) ? 2 : 1;
515 9710 unsigned int nb_mb = p->alloc_mb_height * p->alloc_mb_width;
516
517
2/2
✓ Branch 0 taken 9695 times.
✓ Branch 1 taken 15 times.
9710 if (!(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS))
518 9695 return 0;
519
520 15 par = av_video_enc_params_create_side_data(f, AV_VIDEO_ENC_PARAMS_MPEG2, nb_mb);
521
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!par)
522 return AVERROR(ENOMEM);
523
524
2/2
✓ Branch 0 taken 270 times.
✓ Branch 1 taken 15 times.
285 for (unsigned y = 0; y < p->alloc_mb_height; y++)
525
2/2
✓ Branch 0 taken 5940 times.
✓ Branch 1 taken 270 times.
6210 for (unsigned x = 0; x < p->alloc_mb_width; x++) {
526 5940 const unsigned int block_idx = y * p->alloc_mb_width + x;
527 5940 const unsigned int mb_xy = y * p->alloc_mb_stride + x;
528 5940 AVVideoBlockParams *const b = av_video_enc_params_block(par, block_idx);
529
530 5940 b->src_x = x * 16;
531 5940 b->src_y = y * 16;
532 5940 b->w = 16;
533 5940 b->h = 16;
534
535 5940 b->delta_qp = p->qscale_table[mb_xy] * mult;
536 }
537
538 15 return 0;
539 }
540
541 162764 void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
542 {
543 162764 ff_draw_horiz_band(s->avctx, s->current_picture_ptr->f,
544
2/2
✓ Branch 0 taken 158421 times.
✓ Branch 1 taken 4343 times.
162764 s->last_picture_ptr ? s->last_picture_ptr->f : NULL,
545 y, h, s->picture_structure,
546 s->first_field, s->low_delay);
547 162764 }
548
549 79 void ff_mpeg_flush(AVCodecContext *avctx)
550 {
551 79 MpegEncContext *const s = avctx->priv_data;
552
553
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 if (!s->picture)
554 return;
555
556
2/2
✓ Branch 0 taken 2844 times.
✓ Branch 1 taken 79 times.
2923 for (int i = 0; i < MAX_PICTURE_COUNT; i++)
557 2844 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
558 79 s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
559
560 79 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
561 79 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
562 79 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
563
564 79 s->mb_x = s->mb_y = 0;
565
566 #if FF_API_FLAG_TRUNCATED
567 79 s->parse_context.state = -1;
568 79 s->parse_context.frame_start_found = 0;
569 79 s->parse_context.overread = 0;
570 79 s->parse_context.overread_index = 0;
571 79 s->parse_context.index = 0;
572 79 s->parse_context.last_index = 0;
573 #endif
574 79 s->bitstream_buffer_size = 0;
575 79 s->pp_time = 0;
576 }
577
578 152996 void ff_mpv_report_decode_progress(MpegEncContext *s)
579 {
580
5/6
✓ Branch 0 taken 119122 times.
✓ Branch 1 taken 33874 times.
✓ Branch 2 taken 112539 times.
✓ Branch 3 taken 6583 times.
✓ Branch 4 taken 112539 times.
✗ Branch 5 not taken.
152996 if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
581 112539 ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
582 152996 }
583