FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_dec.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 375 524 71.6%
Functions: 20 20 100.0%
Branches: 193 350 55.1%

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 "config_components.h"
26
27 #include "libavutil/avassert.h"
28 #include "libavutil/emms.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/video_enc_params.h"
32
33 #include "avcodec.h"
34 #include "decode.h"
35 #include "h264chroma.h"
36 #include "internal.h"
37 #include "mpegutils.h"
38 #include "mpegvideo.h"
39 #include "mpegvideodec.h"
40 #include "mpeg4videodec.h"
41 #include "thread.h"
42 #include "threadframe.h"
43 #include "wmv2dec.h"
44
45 626 void ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx)
46 {
47 626 ff_mpv_common_defaults(s);
48
49 626 s->avctx = avctx;
50 626 s->width = avctx->coded_width;
51 626 s->height = avctx->coded_height;
52 626 s->codec_id = avctx->codec->id;
53 626 s->workaround_bugs = avctx->workaround_bugs;
54
55 /* convert fourcc to upper case */
56 626 s->codec_tag = ff_toupper4(avctx->codec_tag);
57
58 626 ff_mpv_idct_init(s);
59 626 ff_h264chroma_init(&s->h264chroma, 8); //for lowres
60 626 }
61
62 18 int ff_mpeg_update_thread_context(AVCodecContext *dst,
63 const AVCodecContext *src)
64 {
65 18 MpegEncContext *const s1 = src->priv_data;
66 18 MpegEncContext *const s = dst->priv_data;
67 int ret;
68
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (dst == src)
70 return 0;
71
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 av_assert0(s != s1);
73
74 // FIXME can parameters change on I-frames?
75 // in that case dst may need a reinit
76
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
18 if (!s->context_initialized) {
77 8 void *private_ctx = s->private_ctx;
78 int err;
79 8 memcpy(s, s1, sizeof(*s));
80
81 8 s->context_initialized = 0;
82 8 s->context_reinit = 0;
83 8 s->avctx = dst;
84 8 s->private_ctx = private_ctx;
85 8 s->bitstream_buffer = NULL;
86 8 s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
87
88
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (s1->context_initialized) {
89
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if ((err = ff_mpv_common_init(s)) < 0)
90 return err;
91 }
92 }
93
94
3/6
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 18 times.
18 if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
95 s->height = s1->height;
96 s->width = s1->width;
97 if ((ret = ff_mpv_common_frame_size_change(s)) < 0)
98 return ret;
99 }
100
101 18 s->quarter_sample = s1->quarter_sample;
102
103 18 s->coded_picture_number = s1->coded_picture_number;
104 18 s->picture_number = s1->picture_number;
105
106
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18 av_assert0(!s->picture || s->picture != s1->picture);
107
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (s->picture)
108
2/2
✓ Branch 0 taken 648 times.
✓ Branch 1 taken 18 times.
666 for (int i = 0; i < MAX_PICTURE_COUNT; i++) {
109 648 ff_mpeg_unref_picture(&s->picture[i]);
110
4/6
✓ Branch 0 taken 648 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
✓ Branch 3 taken 613 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 35 times.
683 if (s1->picture && s1->picture[i].f->buf[0] &&
111 35 (ret = ff_mpeg_ref_picture(&s->picture[i], &s1->picture[i])) < 0)
112 return ret;
113 }
114
115 #define UPDATE_PICTURE(pic)\
116 do {\
117 ff_mpeg_unref_picture(&s->pic);\
118 if (s1->pic.f && s1->pic.f->buf[0])\
119 ret = ff_mpeg_ref_picture(&s->pic, &s1->pic);\
120 else\
121 ret = ff_update_picture_tables(&s->pic, &s1->pic);\
122 if (ret < 0)\
123 return ret;\
124 } while (0)
125
126
3/6
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
✗ Branch 4 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 18 times.
18 UPDATE_PICTURE(current_picture);
127
4/6
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
✓ Branch 4 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 18 times.
18 UPDATE_PICTURE(last_picture);
128
3/6
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
✗ Branch 4 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 18 times.
18 UPDATE_PICTURE(next_picture);
129
130 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
131 ((pic && pic >= old_ctx->picture && \
132 pic < old_ctx->picture + MAX_PICTURE_COUNT) ? \
133 &new_ctx->picture[pic - old_ctx->picture] : NULL)
134
135
4/6
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 17 times.
✗ Branch 5 not taken.
18 s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
136
3/6
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
18 s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
137
3/6
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
18 s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
138
139 // Error/bug resilience
140 18 s->workaround_bugs = s1->workaround_bugs;
141 18 s->padding_bug_score = s1->padding_bug_score;
142
143 // MPEG-4 timing info
144 18 memcpy(&s->last_time_base, &s1->last_time_base,
145 18 (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
146 18 (char *) &s1->last_time_base);
147
148 // B-frame info
149 18 s->max_b_frames = s1->max_b_frames;
150 18 s->low_delay = s1->low_delay;
151 18 s->droppable = s1->droppable;
152
153 // DivX handling (doesn't work)
154 18 s->divx_packed = s1->divx_packed;
155
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (s1->bitstream_buffer) {
157 av_fast_padded_malloc(&s->bitstream_buffer,
158 &s->allocated_bitstream_buffer_size,
159 s1->bitstream_buffer_size);
160 if (!s->bitstream_buffer) {
161 s->bitstream_buffer_size = 0;
162 return AVERROR(ENOMEM);
163 }
164 s->bitstream_buffer_size = s1->bitstream_buffer_size;
165 memcpy(s->bitstream_buffer, s1->bitstream_buffer,
166 s1->bitstream_buffer_size);
167 }
168
169 // linesize-dependent scratch buffer allocation
170
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
18 if (!s->sc.edge_emu_buffer)
171
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (s1->linesize) {
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ff_mpeg_framesize_alloc(s->avctx, &s->me,
173 8 &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 18 memcpy(&s->progressive_sequence, &s1->progressive_sequence,
185 (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
186
187 18 return 0;
188 }
189
190 42 int ff_mpv_common_frame_size_change(MpegEncContext *s)
191 {
192 42 int err = 0;
193
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!s->context_initialized)
195 return AVERROR(EINVAL);
196
197 42 ff_mpv_free_context_frame(s);
198
199
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (s->picture)
200
2/2
✓ Branch 0 taken 1512 times.
✓ Branch 1 taken 42 times.
1554 for (int i = 0; i < MAX_PICTURE_COUNT; i++)
201 1512 s->picture[i].needs_realloc = 1;
202
203 42 s->last_picture_ptr =
204 42 s->next_picture_ptr =
205 42 s->current_picture_ptr = NULL;
206
207
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 42 times.
84 if ((s->width || s->height) &&
208 42 (err = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
209 goto fail;
210
211 /* set chroma shifts */
212 42 err = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
213 &s->chroma_x_shift,
214 &s->chroma_y_shift);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (err < 0)
216 goto fail;
217
218
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
42 if ((err = ff_mpv_init_context_frame(s)))
219 goto fail;
220
221 42 memset(s->thread_context, 0, sizeof(s->thread_context));
222 42 s->thread_context[0] = s;
223
224
2/4
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
42 if (s->width && s->height) {
225 42 err = ff_mpv_init_duplicate_contexts(s);
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (err < 0)
227 goto fail;
228 }
229 42 s->context_reinit = 0;
230
231 42 return 0;
232 fail:
233 ff_mpv_free_context_frame(s);
234 s->context_reinit = 1;
235 return err;
236 }
237
238 11220 static int alloc_picture(MpegEncContext *s, Picture **picp, int reference)
239 {
240 11220 AVCodecContext *avctx = s->avctx;
241 11220 int idx = ff_find_unused_picture(s->avctx, s->picture, 0);
242 Picture *pic;
243 int ret;
244
245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11220 times.
11220 if (idx < 0)
246 return idx;
247
248 11220 pic = &s->picture[idx];
249
250 11220 pic->tf.f = pic->f;
251 11220 pic->reference = reference;
252
253 /* WM Image / Screen codecs allocate internal buffers with different
254 * dimensions / colorspaces; ignore user-defined callbacks for these. */
255
1/2
✓ Branch 0 taken 11220 times.
✗ Branch 1 not taken.
11220 if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
256
1/2
✓ Branch 0 taken 11220 times.
✗ Branch 1 not taken.
11220 avctx->codec_id != AV_CODEC_ID_VC1IMAGE &&
257
2/2
✓ Branch 0 taken 11138 times.
✓ Branch 1 taken 82 times.
11220 avctx->codec_id != AV_CODEC_ID_MSS2) {
258 11138 ret = ff_thread_get_ext_buffer(avctx, &pic->tf,
259 reference ? AV_GET_BUFFER_FLAG_REF : 0);
260 } else {
261 82 pic->f->width = avctx->width;
262 82 pic->f->height = avctx->height;
263 82 pic->f->format = avctx->pix_fmt;
264 82 ret = avcodec_default_get_buffer2(avctx, pic->f, 0);
265 }
266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11220 times.
11220 if (ret < 0)
267 goto fail;
268
269 11220 ret = ff_hwaccel_frame_priv_alloc(avctx, &pic->hwaccel_picture_private);
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11220 times.
11220 if (ret < 0)
271 goto fail;
272
273 11220 ret = ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, 0, s->out_format,
274 s->mb_stride, s->mb_width, s->mb_height, s->b8_stride,
275 &s->linesize, &s->uvlinesize);
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11220 times.
11220 if (ret < 0)
277 goto fail;
278 11220 *picp = pic;
279
280 11220 return 0;
281 fail:
282 ff_mpeg_unref_picture(pic);
283 return ret;
284 }
285
286 6 static int av_cold alloc_dummy_frame(MpegEncContext *s, Picture **picp)
287 {
288 Picture *pic;
289 6 int ret = alloc_picture(s, picp, 1);
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
291 return ret;
292
293 6 pic = *picp;
294
295 6 ff_thread_report_progress(&pic->tf, INT_MAX, 0);
296 6 ff_thread_report_progress(&pic->tf, INT_MAX, 1);
297
298 6 return 0;
299 }
300
301 6 static void color_frame(AVFrame *frame, int luma)
302 {
303 int h_chroma_shift, v_chroma_shift;
304
305
2/2
✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 6 times.
1734 for (int i = 0; i < frame->height; i++)
306 1728 memset(frame->data[0] + frame->linesize[0] * i, luma, frame->width);
307
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!frame->data[1])
309 return;
310 6 av_pix_fmt_get_chroma_sub_sample(frame->format, &h_chroma_shift, &v_chroma_shift);
311
2/2
✓ Branch 0 taken 864 times.
✓ Branch 1 taken 6 times.
870 for (int i = 0; i < AV_CEIL_RSHIFT(frame->height, v_chroma_shift); i++) {
312 864 memset(frame->data[1] + frame->linesize[1] * i,
313 864 0x80, AV_CEIL_RSHIFT(frame->width, h_chroma_shift));
314 864 memset(frame->data[2] + frame->linesize[2] * i,
315 864 0x80, AV_CEIL_RSHIFT(frame->width, h_chroma_shift));
316 }
317 }
318
319 /**
320 * generic function called after decoding
321 * the header and before a frame is decoded.
322 */
323 11214 int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
324 {
325 int ret;
326
327 11214 s->mb_skipped = 0;
328
329
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11214 times.
11214 if (!ff_thread_can_start_frame(avctx)) {
330 av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
331 return AVERROR_BUG;
332 }
333
334 /* mark & release old frames */
335
4/4
✓ Branch 0 taken 8542 times.
✓ Branch 1 taken 2672 times.
✓ Branch 2 taken 8010 times.
✓ Branch 3 taken 532 times.
11214 if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
336
1/2
✓ Branch 0 taken 8010 times.
✗ Branch 1 not taken.
8010 s->last_picture_ptr != s->next_picture_ptr &&
337
1/2
✓ Branch 0 taken 8010 times.
✗ Branch 1 not taken.
8010 s->last_picture_ptr->f->buf[0]) {
338 8010 ff_mpeg_unref_picture(s->last_picture_ptr);
339 }
340
341 /* release non reference/forgotten frames */
342
2/2
✓ Branch 0 taken 403704 times.
✓ Branch 1 taken 11214 times.
414918 for (int i = 0; i < MAX_PICTURE_COUNT; i++) {
343
2/2
✓ Branch 0 taken 13597 times.
✓ Branch 1 taken 390107 times.
403704 if (!s->picture[i].reference ||
344
2/2
✓ Branch 0 taken 10925 times.
✓ Branch 1 taken 2672 times.
13597 (&s->picture[i] != s->last_picture_ptr &&
345
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10895 times.
10925 &s->picture[i] != s->next_picture_ptr &&
346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 !s->picture[i].needs_realloc)) {
347 390107 ff_mpeg_unref_picture(&s->picture[i]);
348 }
349 }
350
351 11214 ff_mpeg_unref_picture(&s->current_picture);
352 11214 ff_mpeg_unref_picture(&s->last_picture);
353 11214 ff_mpeg_unref_picture(&s->next_picture);
354
355 11214 ret = alloc_picture(s, &s->current_picture_ptr,
356
3/4
✓ Branch 0 taken 8542 times.
✓ Branch 1 taken 2672 times.
✓ Branch 2 taken 8542 times.
✗ Branch 3 not taken.
11214 s->pict_type != AV_PICTURE_TYPE_B && !s->droppable);
357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11214 times.
11214 if (ret < 0)
358 return ret;
359
360
2/2
✓ Branch 0 taken 780 times.
✓ Branch 1 taken 10434 times.
11214 s->current_picture_ptr->f->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST * !!s->top_field_first;
361
2/2
✓ Branch 0 taken 2286 times.
✓ Branch 1 taken 8928 times.
13500 s->current_picture_ptr->f->flags |= AV_FRAME_FLAG_INTERLACED * (!s->progressive_frame &&
362
1/2
✓ Branch 0 taken 2286 times.
✗ Branch 1 not taken.
2286 !s->progressive_sequence);
363 11214 s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
364
365 11214 s->current_picture_ptr->f->pict_type = s->pict_type;
366
2/2
✓ Branch 0 taken 1236 times.
✓ Branch 1 taken 9978 times.
11214 if (s->pict_type == AV_PICTURE_TYPE_I)
367 1236 s->current_picture_ptr->f->flags |= AV_FRAME_FLAG_KEY;
368 else
369 9978 s->current_picture_ptr->f->flags &= ~AV_FRAME_FLAG_KEY;
370
371
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11214 times.
11214 if ((ret = ff_mpeg_ref_picture(&s->current_picture,
372 s->current_picture_ptr)) < 0)
373 return ret;
374
375
2/2
✓ Branch 0 taken 8542 times.
✓ Branch 1 taken 2672 times.
11214 if (s->pict_type != AV_PICTURE_TYPE_B) {
376 8542 s->last_picture_ptr = s->next_picture_ptr;
377
1/2
✓ Branch 0 taken 8542 times.
✗ Branch 1 not taken.
8542 if (!s->droppable)
378 8542 s->next_picture_ptr = s->current_picture_ptr;
379 }
380 ff_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
381 s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
382 s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
383 s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
384 s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
385 s->pict_type, s->droppable);
386
387
3/4
✓ Branch 0 taken 10895 times.
✓ Branch 1 taken 319 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10895 times.
11214 if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) &&
388
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 313 times.
319 (s->pict_type != AV_PICTURE_TYPE_I)) {
389
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])
390 av_log(avctx, AV_LOG_DEBUG,
391 "allocating dummy last picture for B frame\n");
392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 else if (s->codec_id != AV_CODEC_ID_H261)
393 av_log(avctx, AV_LOG_ERROR,
394 "warning: first frame is no keyframe\n");
395
396 /* Allocate a dummy frame */
397 6 ret = alloc_dummy_frame(s, &s->last_picture_ptr);
398
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
399 return ret;
400
401
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (!avctx->hwaccel) {
402
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 int luma_val = s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263 ? 16 : 0x80;
403 6 color_frame(s->last_picture_ptr->f, luma_val);
404 }
405
406 }
407
2/4
✓ Branch 0 taken 11214 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11214 times.
11214 if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
408 s->pict_type == AV_PICTURE_TYPE_B) {
409 /* Allocate a dummy frame */
410 ret = alloc_dummy_frame(s, &s->next_picture_ptr);
411 if (ret < 0)
412 return ret;
413 }
414
415
2/2
✓ Branch 0 taken 10901 times.
✓ Branch 1 taken 313 times.
11214 if (s->last_picture_ptr) {
416
2/4
✓ Branch 0 taken 10901 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10901 times.
21802 if (s->last_picture_ptr->f->buf[0] &&
417 10901 (ret = ff_mpeg_ref_picture(&s->last_picture,
418 s->last_picture_ptr)) < 0)
419 return ret;
420 }
421
1/2
✓ Branch 0 taken 11214 times.
✗ Branch 1 not taken.
11214 if (s->next_picture_ptr) {
422
2/4
✓ Branch 0 taken 11214 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11214 times.
22428 if (s->next_picture_ptr->f->buf[0] &&
423 11214 (ret = ff_mpeg_ref_picture(&s->next_picture,
424 s->next_picture_ptr)) < 0)
425 return ret;
426 }
427
428
4/6
✓ Branch 0 taken 9978 times.
✓ Branch 1 taken 1236 times.
✓ Branch 2 taken 9978 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 9978 times.
11214 av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
429 s->last_picture_ptr->f->buf[0]));
430
431 /* set dequantizer, we can't do it during init as
432 * it might change for MPEG-4 and we can't do it in the header
433 * decode as init is not called for MPEG-4 there yet */
434
4/4
✓ Branch 0 taken 11174 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 3420 times.
✓ Branch 3 taken 7754 times.
11214 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
435 3460 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
436 3460 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
437
4/4
✓ Branch 0 taken 780 times.
✓ Branch 1 taken 6974 times.
✓ Branch 2 taken 300 times.
✓ Branch 3 taken 480 times.
7754 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
438 7274 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
439 7274 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
440 } else {
441 480 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
442 480 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
443 }
444
445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11214 times.
11214 if (s->avctx->debug & FF_DEBUG_NOMC)
446 color_frame(s->current_picture_ptr->f, 0x80);
447
448 11214 return 0;
449 }
450
451 /* called after a frame has been decoded. */
452 11214 void ff_mpv_frame_end(MpegEncContext *s)
453 {
454 11214 emms_c();
455
456
2/2
✓ Branch 0 taken 8542 times.
✓ Branch 1 taken 2672 times.
11214 if (s->current_picture.reference)
457 8542 ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
458 11214 }
459
460 10981 void ff_print_debug_info(const MpegEncContext *s, const Picture *p, AVFrame *pict)
461 {
462 10981 ff_print_debug_info2(s->avctx, pict, s->mbskip_table, p->mb_type,
463 10981 p->qscale_table, p->motion_val,
464 10981 s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
465 10981 }
466
467 10244 int ff_mpv_export_qp_table(const MpegEncContext *s, AVFrame *f, const Picture *p, int qp_type)
468 {
469 AVVideoEncParams *par;
470
2/2
✓ Branch 0 taken 6406 times.
✓ Branch 1 taken 3838 times.
10244 int mult = (qp_type == FF_MPV_QSCALE_TYPE_MPEG1) ? 2 : 1;
471 10244 unsigned int nb_mb = p->alloc_mb_height * p->alloc_mb_width;
472
473
2/2
✓ Branch 0 taken 10202 times.
✓ Branch 1 taken 42 times.
10244 if (!(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS))
474 10202 return 0;
475
476 42 par = av_video_enc_params_create_side_data(f, AV_VIDEO_ENC_PARAMS_MPEG2, nb_mb);
477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!par)
478 return AVERROR(ENOMEM);
479
480
2/2
✓ Branch 0 taken 756 times.
✓ Branch 1 taken 42 times.
798 for (unsigned y = 0; y < p->alloc_mb_height; y++)
481
2/2
✓ Branch 0 taken 16632 times.
✓ Branch 1 taken 756 times.
17388 for (unsigned x = 0; x < p->alloc_mb_width; x++) {
482 16632 const unsigned int block_idx = y * p->alloc_mb_width + x;
483 16632 const unsigned int mb_xy = y * p->alloc_mb_stride + x;
484 16632 AVVideoBlockParams *const b = av_video_enc_params_block(par, block_idx);
485
486 16632 b->src_x = x * 16;
487 16632 b->src_y = y * 16;
488 16632 b->w = 16;
489 16632 b->h = 16;
490
491 16632 b->delta_qp = p->qscale_table[mb_xy] * mult;
492 }
493
494 42 return 0;
495 }
496
497 170018 void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
498 {
499 170018 ff_draw_horiz_band(s->avctx, s->current_picture_ptr->f,
500
2/2
✓ Branch 0 taken 166323 times.
✓ Branch 1 taken 3695 times.
170018 s->last_picture_ptr ? s->last_picture_ptr->f : NULL,
501 y, h, s->picture_structure,
502 s->first_field, s->low_delay);
503 170018 }
504
505 82 void ff_mpeg_flush(AVCodecContext *avctx)
506 {
507 82 MpegEncContext *const s = avctx->priv_data;
508
509
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 if (!s->picture)
510 return;
511
512
2/2
✓ Branch 0 taken 2952 times.
✓ Branch 1 taken 82 times.
3034 for (int i = 0; i < MAX_PICTURE_COUNT; i++)
513 2952 ff_mpeg_unref_picture(&s->picture[i]);
514 82 s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
515
516 82 ff_mpeg_unref_picture(&s->current_picture);
517 82 ff_mpeg_unref_picture(&s->last_picture);
518 82 ff_mpeg_unref_picture(&s->next_picture);
519
520 82 s->mb_x = s->mb_y = 0;
521
522 82 s->bitstream_buffer_size = 0;
523 82 s->pp_time = 0;
524 }
525
526 170018 void ff_mpv_report_decode_progress(MpegEncContext *s)
527 {
528
5/6
✓ Branch 0 taken 130228 times.
✓ Branch 1 taken 39790 times.
✓ Branch 2 taken 123645 times.
✓ Branch 3 taken 6583 times.
✓ Branch 4 taken 123645 times.
✗ Branch 5 not taken.
170018 if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
529 123645 ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
530 170018 }
531
532
533 126300 static inline int hpel_motion_lowres(MpegEncContext *s,
534 uint8_t *dest, const uint8_t *src,
535 int field_based, int field_select,
536 int src_x, int src_y,
537 int width, int height, ptrdiff_t stride,
538 int h_edge_pos, int v_edge_pos,
539 int w, int h, const h264_chroma_mc_func *pix_op,
540 int motion_x, int motion_y)
541 {
542 126300 const int lowres = s->avctx->lowres;
543 126300 const int op_index = FFMIN(lowres, 3);
544 126300 const int s_mask = (2 << lowres) - 1;
545 126300 int emu = 0;
546 int sx, sy;
547
548
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126300 times.
126300 if (s->quarter_sample) {
549 motion_x /= 2;
550 motion_y /= 2;
551 }
552
553 126300 sx = motion_x & s_mask;
554 126300 sy = motion_y & s_mask;
555 126300 src_x += motion_x >> lowres + 1;
556 126300 src_y += motion_y >> lowres + 1;
557
558 126300 src += src_y * stride + src_x;
559
560
1/2
✓ Branch 0 taken 126300 times.
✗ Branch 1 not taken.
126300 if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
561
2/2
✓ Branch 0 taken 298 times.
✓ Branch 1 taken 126002 times.
126300 (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
562 298 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, src,
563 s->linesize, s->linesize,
564 298 w + 1, (h + 1) << field_based,
565 src_x, src_y * (1 << field_based),
566 h_edge_pos, v_edge_pos);
567 298 src = s->sc.edge_emu_buffer;
568 298 emu = 1;
569 }
570
571 126300 sx = (sx << 2) >> lowres;
572 126300 sy = (sy << 2) >> lowres;
573
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126300 times.
126300 if (field_select)
574 src += s->linesize;
575 126300 pix_op[op_index](dest, src, stride, h, sx, sy);
576 126300 return emu;
577 }
578
579 /* apply one mpeg motion vector to the three components */
580 24814 static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
581 uint8_t *dest_y,
582 uint8_t *dest_cb,
583 uint8_t *dest_cr,
584 int field_based,
585 int bottom_field,
586 int field_select,
587 uint8_t *const *ref_picture,
588 const h264_chroma_mc_func *pix_op,
589 int motion_x, int motion_y,
590 int h, int mb_y)
591 {
592 const uint8_t *ptr_y, *ptr_cb, *ptr_cr;
593 int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
594 ptrdiff_t uvlinesize, linesize;
595 24814 const int lowres = s->avctx->lowres;
596 24814 const int op_index = FFMIN(lowres - 1 + s->chroma_x_shift, 3);
597 24814 const int block_s = 8 >> lowres;
598 24814 const int s_mask = (2 << lowres) - 1;
599 24814 const int h_edge_pos = s->h_edge_pos >> lowres;
600 24814 const int v_edge_pos = s->v_edge_pos >> lowres;
601
1/2
✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
24814 int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;
602 24814 linesize = s->current_picture.f->linesize[0] << field_based;
603 24814 uvlinesize = s->current_picture.f->linesize[1] << field_based;
604
605 // FIXME obviously not perfect but qpel will not work in lowres anyway
606
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
24814 if (s->quarter_sample) {
607 motion_x /= 2;
608 motion_y /= 2;
609 }
610
611
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
24814 if (field_based) {
612 motion_y += (bottom_field - field_select)*((1 << lowres)-1);
613 }
614
615 24814 sx = motion_x & s_mask;
616 24814 sy = motion_y & s_mask;
617 24814 src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
618 24814 src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
619
620
1/2
✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
24814 if (s->out_format == FMT_H263) {
621 24814 uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
622 24814 uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
623 24814 uvsrc_x = src_x >> 1;
624 24814 uvsrc_y = src_y >> 1;
625 } else if (s->out_format == FMT_H261) {
626 // even chroma mv's are full pel in H261
627 mx = motion_x / 4;
628 my = motion_y / 4;
629 uvsx = (2 * mx) & s_mask;
630 uvsy = (2 * my) & s_mask;
631 uvsrc_x = s->mb_x * block_s + (mx >> lowres);
632 uvsrc_y = mb_y * block_s + (my >> lowres);
633 } else {
634 if (s->chroma_y_shift) {
635 mx = motion_x / 2;
636 my = motion_y / 2;
637 uvsx = mx & s_mask;
638 uvsy = my & s_mask;
639 uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
640 uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
641 } else {
642 if (s->chroma_x_shift) {
643 //Chroma422
644 mx = motion_x / 2;
645 uvsx = mx & s_mask;
646 uvsy = motion_y & s_mask;
647 uvsrc_y = src_y;
648 uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
649 } else {
650 //Chroma444
651 uvsx = motion_x & s_mask;
652 uvsy = motion_y & s_mask;
653 uvsrc_x = src_x;
654 uvsrc_y = src_y;
655 }
656 }
657 }
658
659 24814 ptr_y = ref_picture[0] + src_y * linesize + src_x;
660 24814 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
661 24814 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
662
663
3/4
✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24712 times.
✓ Branch 3 taken 102 times.
24814 if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
664
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 24617 times.
24712 (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - FFMAX(h, hc<<s->chroma_y_shift), 0)) {
665 197 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr_y,
666 linesize >> field_based, linesize >> field_based,
667 17, 17 + field_based,
668 src_x, src_y * (1 << field_based), h_edge_pos,
669 v_edge_pos);
670 197 ptr_y = s->sc.edge_emu_buffer;
671 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
672 197 uint8_t *ubuf = s->sc.edge_emu_buffer + 18 * s->linesize;
673 197 uint8_t *vbuf =ubuf + 10 * s->uvlinesize;
674
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 197 times.
197 if (s->workaround_bugs & FF_BUG_IEDGE)
675 vbuf -= s->uvlinesize;
676 197 s->vdsp.emulated_edge_mc(ubuf, ptr_cb,
677 uvlinesize >> field_based, uvlinesize >> field_based,
678 9, 9 + field_based,
679 uvsrc_x, uvsrc_y * (1 << field_based),
680 h_edge_pos >> 1, v_edge_pos >> 1);
681 197 s->vdsp.emulated_edge_mc(vbuf, ptr_cr,
682 uvlinesize >> field_based,uvlinesize >> field_based,
683 9, 9 + field_based,
684 uvsrc_x, uvsrc_y * (1 << field_based),
685 h_edge_pos >> 1, v_edge_pos >> 1);
686 197 ptr_cb = ubuf;
687 197 ptr_cr = vbuf;
688 }
689 }
690
691 // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f->data
692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
24814 if (bottom_field) {
693 dest_y += s->linesize;
694 dest_cb += s->uvlinesize;
695 dest_cr += s->uvlinesize;
696 }
697
698
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
24814 if (field_select) {
699 ptr_y += s->linesize;
700 ptr_cb += s->uvlinesize;
701 ptr_cr += s->uvlinesize;
702 }
703
704 24814 sx = (sx << 2) >> lowres;
705 24814 sy = (sy << 2) >> lowres;
706 24814 pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
707
708 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
709 24814 uvsx = (uvsx << 2) >> lowres;
710 24814 uvsy = (uvsy << 2) >> lowres;
711
1/2
✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
24814 if (hc) {
712 24814 pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
713 24814 pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
714 }
715 }
716 // FIXME h261 lowres loop filter
717 24814 }
718
719 31575 static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
720 uint8_t *dest_cb, uint8_t *dest_cr,
721 uint8_t *const *ref_picture,
722 const h264_chroma_mc_func * pix_op,
723 int mx, int my)
724 {
725 31575 const int lowres = s->avctx->lowres;
726 31575 const int op_index = FFMIN(lowres, 3);
727 31575 const int block_s = 8 >> lowres;
728 31575 const int s_mask = (2 << lowres) - 1;
729 31575 const int h_edge_pos = s->h_edge_pos >> lowres + 1;
730 31575 const int v_edge_pos = s->v_edge_pos >> lowres + 1;
731 31575 int emu = 0, src_x, src_y, sx, sy;
732 ptrdiff_t offset;
733 const uint8_t *ptr;
734
735
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31575 times.
31575 if (s->quarter_sample) {
736 mx /= 2;
737 my /= 2;
738 }
739
740 /* In case of 8X8, we construct a single chroma motion vector
741 with a special rounding */
742 31575 mx = ff_h263_round_chroma(mx);
743 31575 my = ff_h263_round_chroma(my);
744
745 31575 sx = mx & s_mask;
746 31575 sy = my & s_mask;
747 31575 src_x = s->mb_x * block_s + (mx >> lowres + 1);
748 31575 src_y = s->mb_y * block_s + (my >> lowres + 1);
749
750 31575 offset = src_y * s->uvlinesize + src_x;
751 31575 ptr = ref_picture[1] + offset;
752
1/2
✓ Branch 0 taken 31575 times.
✗ Branch 1 not taken.
31575 if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
753
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 31431 times.
31575 (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
754 144 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
755 s->uvlinesize, s->uvlinesize,
756 9, 9,
757 src_x, src_y, h_edge_pos, v_edge_pos);
758 144 ptr = s->sc.edge_emu_buffer;
759 144 emu = 1;
760 }
761 31575 sx = (sx << 2) >> lowres;
762 31575 sy = (sy << 2) >> lowres;
763 31575 pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
764
765 31575 ptr = ref_picture[2] + offset;
766
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 31431 times.
31575 if (emu) {
767 144 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
768 s->uvlinesize, s->uvlinesize,
769 9, 9,
770 src_x, src_y, h_edge_pos, v_edge_pos);
771 144 ptr = s->sc.edge_emu_buffer;
772 }
773 31575 pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
774 31575 }
775
776 /**
777 * motion compensation of a single macroblock
778 * @param s context
779 * @param dest_y luma destination pointer
780 * @param dest_cb chroma cb/u destination pointer
781 * @param dest_cr chroma cr/v destination pointer
782 * @param dir direction (0->forward, 1->backward)
783 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
784 * @param pix_op halfpel motion compensation function (average or put normally)
785 * the motion vectors are taken from s->mv and the MV type from s->mv_type
786 */
787 56389 static inline void MPV_motion_lowres(MpegEncContext *s,
788 uint8_t *dest_y, uint8_t *dest_cb,
789 uint8_t *dest_cr,
790 int dir, uint8_t *const *ref_picture,
791 const h264_chroma_mc_func *pix_op)
792 {
793 int mx, my;
794 int mb_x, mb_y;
795 56389 const int lowres = s->avctx->lowres;
796 56389 const int block_s = 8 >>lowres;
797
798 56389 mb_x = s->mb_x;
799 56389 mb_y = s->mb_y;
800
801
2/6
✓ Branch 0 taken 24814 times.
✓ Branch 1 taken 31575 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
56389 switch (s->mv_type) {
802 24814 case MV_TYPE_16X16:
803 24814 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
804 0, 0, 0,
805 ref_picture, pix_op,
806 s->mv[dir][0][0], s->mv[dir][0][1],
807 2 * block_s, mb_y);
808 24814 break;
809 31575 case MV_TYPE_8X8:
810 31575 mx = 0;
811 31575 my = 0;
812
2/2
✓ Branch 0 taken 126300 times.
✓ Branch 1 taken 31575 times.
157875 for (int i = 0; i < 4; i++) {
813 126300 hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
814 126300 s->linesize) * block_s,
815 ref_picture[0], 0, 0,
816 126300 (2 * mb_x + (i & 1)) * block_s,
817 126300 (2 * mb_y + (i >> 1)) * block_s,
818 s->width, s->height, s->linesize,
819 126300 s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
820 block_s, block_s, pix_op,
821 s->mv[dir][i][0], s->mv[dir][i][1]);
822
823 126300 mx += s->mv[dir][i][0];
824 126300 my += s->mv[dir][i][1];
825 }
826
827 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY))
828 31575 chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
829 pix_op, mx, my);
830 31575 break;
831 case MV_TYPE_FIELD:
832 if (s->picture_structure == PICT_FRAME) {
833 /* top field */
834 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
835 1, 0, s->field_select[dir][0],
836 ref_picture, pix_op,
837 s->mv[dir][0][0], s->mv[dir][0][1],
838 block_s, mb_y);
839 /* bottom field */
840 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
841 1, 1, s->field_select[dir][1],
842 ref_picture, pix_op,
843 s->mv[dir][1][0], s->mv[dir][1][1],
844 block_s, mb_y);
845 } else {
846 if ( s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != AV_PICTURE_TYPE_B && !s->first_field
847 || !ref_picture[0]) {
848 ref_picture = s->current_picture_ptr->f->data;
849 }
850 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
851 0, 0, s->field_select[dir][0],
852 ref_picture, pix_op,
853 s->mv[dir][0][0],
854 s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
855 }
856 break;
857 case MV_TYPE_16X8:
858 for (int i = 0; i < 2; i++) {
859 uint8_t *const *ref2picture;
860
861 if ((s->picture_structure == s->field_select[dir][i] + 1 ||
862 s->pict_type == AV_PICTURE_TYPE_B || s->first_field) &&
863 ref_picture[0]) {
864 ref2picture = ref_picture;
865 } else {
866 ref2picture = s->current_picture_ptr->f->data;
867 }
868
869 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
870 0, 0, s->field_select[dir][i],
871 ref2picture, pix_op,
872 s->mv[dir][i][0], s->mv[dir][i][1] +
873 2 * block_s * i, block_s, mb_y >> 1);
874
875 dest_y += 2 * block_s * s->linesize;
876 dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
877 dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
878 }
879 break;
880 case MV_TYPE_DMV:
881 if (s->picture_structure == PICT_FRAME) {
882 for (int i = 0; i < 2; i++) {
883 for (int j = 0; j < 2; j++) {
884 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
885 1, j, j ^ i,
886 ref_picture, pix_op,
887 s->mv[dir][2 * i + j][0],
888 s->mv[dir][2 * i + j][1],
889 block_s, mb_y);
890 }
891 pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
892 }
893 } else {
894 if (!ref_picture[0]) {
895 ref_picture = s->current_picture_ptr->f->data;
896 }
897 for (int i = 0; i < 2; i++) {
898 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
899 0, 0, s->picture_structure != i + 1,
900 ref_picture, pix_op,
901 s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
902 2 * block_s, mb_y >> 1);
903
904 // after put we make avg of the same block
905 pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
906
907 // opposite parity is always in the same
908 // frame if this is second field
909 if (!s->first_field) {
910 ref_picture = s->current_picture_ptr->f->data;
911 }
912 }
913 }
914 break;
915 56389 default:
916 av_assert2(0);
917 }
918 56389 }
919
920 /**
921 * find the lowest MB row referenced in the MVs
922 */
923 60133 static int lowest_referenced_row(MpegEncContext *s, int dir)
924 {
925 60133 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
926 int off, mvs;
927
928
2/4
✓ Branch 0 taken 60133 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 60133 times.
60133 if (s->picture_structure != PICT_FRAME || s->mcsel)
929 goto unhandled;
930
931
1/4
✓ Branch 0 taken 60133 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
60133 switch (s->mv_type) {
932 60133 case MV_TYPE_16X16:
933 60133 mvs = 1;
934 60133 break;
935 case MV_TYPE_16X8:
936 mvs = 2;
937 break;
938 case MV_TYPE_8X8:
939 mvs = 4;
940 break;
941 default:
942 goto unhandled;
943 }
944
945
2/2
✓ Branch 0 taken 60133 times.
✓ Branch 1 taken 60133 times.
120266 for (int i = 0; i < mvs; i++) {
946 60133 int my = s->mv[dir][i][1];
947 60133 my_max = FFMAX(my_max, my);
948 60133 my_min = FFMIN(my_min, my);
949 }
950
951 60133 off = ((FFMAX(-my_min, my_max) << qpel_shift) + 63) >> 6;
952
953 60133 return av_clip(s->mb_y + off, 0, s->mb_height - 1);
954 unhandled:
955 return s->mb_height - 1;
956 }
957
958 /* add block[] to dest[] */
959 22791378 static inline void add_dct(MpegEncContext *s,
960 int16_t *block, int i, uint8_t *dest, int line_size)
961 {
962
2/2
✓ Branch 0 taken 8416490 times.
✓ Branch 1 taken 14374888 times.
22791378 if (s->block_last_index[i] >= 0) {
963 8416490 s->idsp.idct_add(dest, line_size, block);
964 }
965 22791378 }
966
967 #define IS_ENCODER 0
968 #include "mpv_reconstruct_mb_template.c"
969
970 5136232 void ff_mpv_reconstruct_mb(MpegEncContext *s, int16_t block[12][64])
971 {
972
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5136232 times.
5136232 if (s->avctx->debug & FF_DEBUG_DCT_COEFF) {
973 /* print DCT coefficients */
974 av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
975 for (int i = 0; i < 6; i++) {
976 for (int j = 0; j < 64; j++) {
977 av_log(s->avctx, AV_LOG_DEBUG, "%5d",
978 block[i][s->idsp.idct_permutation[j]]);
979 }
980 av_log(s->avctx, AV_LOG_DEBUG, "\n");
981 }
982 }
983
984
2/2
✓ Branch 0 taken 5092732 times.
✓ Branch 1 taken 43500 times.
5136232 if (!s->avctx->lowres) {
985 #if !CONFIG_SMALL
986
2/2
✓ Branch 0 taken 2603154 times.
✓ Branch 1 taken 2489578 times.
5092732 if (s->out_format == FMT_MPEG1)
987 2603154 mpv_reconstruct_mb_internal(s, block, 0, DEFINITELY_MPEG12);
988 else
989 2489578 mpv_reconstruct_mb_internal(s, block, 0, NOT_MPEG12);
990 #else
991 mpv_reconstruct_mb_internal(s, block, 0, MAY_BE_MPEG12);
992 #endif
993 } else
994 43500 mpv_reconstruct_mb_internal(s, block, 1, MAY_BE_MPEG12);
995 5136232 }
996