FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_dec.c
Date: 2025-07-06 04:07:02
Exec Total Coverage
Lines: 424 584 72.6%
Functions: 24 25 96.0%
Branches: 182 314 58.0%

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 "h263.h"
36 #include "h264chroma.h"
37 #include "internal.h"
38 #include "mpegutils.h"
39 #include "mpegvideo.h"
40 #include "mpegvideodec.h"
41 #include "mpeg4videodec.h"
42 #include "libavutil/refstruct.h"
43 #include "thread.h"
44 #include "threadprogress.h"
45 #include "wmv2dec.h"
46
47 634 av_cold int ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx)
48 {
49 enum ThreadingStatus thread_status;
50
51 634 ff_mpv_common_defaults(s);
52
53 634 s->avctx = avctx;
54 634 s->width = avctx->coded_width;
55 634 s->height = avctx->coded_height;
56 634 s->codec_id = avctx->codec->id;
57 634 s->workaround_bugs = avctx->workaround_bugs;
58
59 /* convert fourcc to upper case */
60 634 s->codec_tag = ff_toupper4(avctx->codec_tag);
61
62 634 ff_mpv_idct_init(s);
63
64 634 ff_h264chroma_init(&s->h264chroma, 8); //for lowres
65
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 634 times.
634 if (s->picture_pool) // VC-1 can call this multiple times
67 return 0;
68
69 634 thread_status = ff_thread_sync_ref(avctx, offsetof(MpegEncContext, picture_pool));
70
2/2
✓ Branch 0 taken 626 times.
✓ Branch 1 taken 8 times.
634 if (thread_status != FF_THREAD_IS_COPY) {
71 626 s->picture_pool = ff_mpv_alloc_pic_pool(thread_status != FF_THREAD_NO_FRAME_THREADING);
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 626 times.
626 if (!s->picture_pool)
73 return AVERROR(ENOMEM);
74 }
75 634 return 0;
76 }
77
78 18 int ff_mpeg_update_thread_context(AVCodecContext *dst,
79 const AVCodecContext *src)
80 {
81 18 MpegEncContext *const s1 = src->priv_data;
82 18 MpegEncContext *const s = dst->priv_data;
83 18 int ret = 0;
84
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (dst == src)
86 return 0;
87
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 av_assert0(s != s1);
89
90
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) {
91 s->height = s1->height;
92 s->width = s1->width;
93 if ((ret = ff_mpv_common_frame_size_change(s)) < 0)
94 return ret;
95 ret = 1;
96 }
97
98 18 s->quarter_sample = s1->quarter_sample;
99
100 18 ff_mpv_replace_picture(&s->cur_pic, &s1->cur_pic);
101 18 ff_mpv_replace_picture(&s->last_pic, &s1->last_pic);
102 18 ff_mpv_replace_picture(&s->next_pic, &s1->next_pic);
103
104 18 s->linesize = s1->linesize;
105 18 s->uvlinesize = s1->uvlinesize;
106
107 // Error/bug resilience
108 18 s->workaround_bugs = s1->workaround_bugs;
109
110 // MPEG-4 timing info
111 18 memcpy(&s->last_time_base, &s1->last_time_base,
112 18 (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
113 18 (char *) &s1->last_time_base);
114
115 // B-frame info
116 18 s->low_delay = s1->low_delay;
117
118 // MPEG-2/interlacing info
119 18 memcpy(&s->progressive_sequence, &s1->progressive_sequence,
120 18 (char *) &s1->first_field + sizeof(s1->first_field) - (char *) &s1->progressive_sequence);
121
122 18 return ret;
123 }
124
125 634 av_cold int ff_mpv_decode_close(AVCodecContext *avctx)
126 {
127 634 MpegEncContext *s = avctx->priv_data;
128
129 634 av_refstruct_pool_uninit(&s->picture_pool);
130 634 ff_mpv_common_end(s);
131 634 return 0;
132 }
133
134 47 av_cold int ff_mpv_common_frame_size_change(MpegEncContext *s)
135 {
136 47 int err = 0;
137
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (!s->context_initialized)
139 return AVERROR(EINVAL);
140
141 47 ff_mpv_free_context_frame(s);
142
143 47 ff_mpv_unref_picture(&s->last_pic);
144 47 ff_mpv_unref_picture(&s->next_pic);
145 47 ff_mpv_unref_picture(&s->cur_pic);
146
147
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 47 times.
94 if ((s->width || s->height) &&
148 47 (err = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
149 goto fail;
150
151 /* set chroma shifts */
152 47 err = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
153 &s->chroma_x_shift,
154 &s->chroma_y_shift);
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (err < 0)
156 goto fail;
157
158
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
47 if ((err = ff_mpv_init_context_frame(s)))
159 goto fail;
160
161 47 memset(s->thread_context, 0, sizeof(s->thread_context));
162 47 s->thread_context[0] = s;
163
164
2/4
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
47 if (s->width && s->height) {
165 47 err = ff_mpv_init_duplicate_contexts(s);
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (err < 0)
167 goto fail;
168 }
169 47 s->context_reinit = 0;
170
171 47 return 0;
172 fail:
173 ff_mpv_free_context_frame(s);
174 s->context_reinit = 1;
175 return err;
176 }
177
178 11251 static int alloc_picture(MpegEncContext *s, MPVWorkPicture *dst, int reference)
179 {
180 11251 AVCodecContext *avctx = s->avctx;
181 11251 MPVPicture *pic = av_refstruct_pool_get(s->picture_pool);
182 int ret;
183
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11251 times.
11251 if (!pic)
185 return AVERROR(ENOMEM);
186
187 11251 dst->ptr = pic;
188
189 11251 pic->reference = reference;
190
191 /* WM Image / Screen codecs allocate internal buffers with different
192 * dimensions / colorspaces; ignore user-defined callbacks for these. */
193
1/2
✓ Branch 0 taken 11251 times.
✗ Branch 1 not taken.
11251 if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
194
1/2
✓ Branch 0 taken 11251 times.
✗ Branch 1 not taken.
11251 avctx->codec_id != AV_CODEC_ID_VC1IMAGE &&
195
2/2
✓ Branch 0 taken 11168 times.
✓ Branch 1 taken 83 times.
11251 avctx->codec_id != AV_CODEC_ID_MSS2) {
196 11168 ret = ff_thread_get_buffer(avctx, pic->f,
197 reference ? AV_GET_BUFFER_FLAG_REF : 0);
198 } else {
199 83 pic->f->width = avctx->width;
200 83 pic->f->height = avctx->height;
201 83 pic->f->format = avctx->pix_fmt;
202 83 ret = avcodec_default_get_buffer2(avctx, pic->f, 0);
203 }
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11251 times.
11251 if (ret < 0)
205 goto fail;
206
207 11251 ret = ff_mpv_pic_check_linesize(avctx, pic->f, &s->linesize, &s->uvlinesize);
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11251 times.
11251 if (ret < 0)
209 goto fail;
210
211 11251 ret = ff_hwaccel_frame_priv_alloc(avctx, &pic->hwaccel_picture_private);
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11251 times.
11251 if (ret < 0)
213 goto fail;
214
215 av_assert1(s->mb_width == s->buffer_pools.alloc_mb_width);
216 av_assert1(s->mb_height == s->buffer_pools.alloc_mb_height ||
217 FFALIGN(s->mb_height, 2) == s->buffer_pools.alloc_mb_height);
218 av_assert1(s->mb_stride == s->buffer_pools.alloc_mb_stride);
219 11251 ret = ff_mpv_alloc_pic_accessories(s->avctx, dst, &s->sc,
220 &s->buffer_pools, s->mb_height);
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11251 times.
11251 if (ret < 0)
222 goto fail;
223
224 11251 return 0;
225 fail:
226 ff_mpv_unref_picture(dst);
227 return ret;
228 }
229
230 21 static int av_cold alloc_dummy_frame(MpegEncContext *s, MPVWorkPicture *dst)
231 {
232 MPVPicture *pic;
233 21 int ret = alloc_picture(s, dst, 1);
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ret < 0)
235 return ret;
236
237 21 pic = dst->ptr;
238 21 pic->dummy = 1;
239
240 21 ff_thread_progress_report(&pic->progress, INT_MAX);
241
242 21 return 0;
243 }
244
245 21 static void color_frame(AVFrame *frame, int luma)
246 {
247 int h_chroma_shift, v_chroma_shift;
248
249
2/2
✓ Branch 0 taken 9888 times.
✓ Branch 1 taken 21 times.
9909 for (int i = 0; i < frame->height; i++)
250 9888 memset(frame->data[0] + frame->linesize[0] * i, luma, frame->width);
251
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!frame->data[1])
253 return;
254 21 av_pix_fmt_get_chroma_sub_sample(frame->format, &h_chroma_shift, &v_chroma_shift);
255
2/2
✓ Branch 0 taken 4944 times.
✓ Branch 1 taken 21 times.
4965 for (int i = 0; i < AV_CEIL_RSHIFT(frame->height, v_chroma_shift); i++) {
256 4944 memset(frame->data[1] + frame->linesize[1] * i,
257 4944 0x80, AV_CEIL_RSHIFT(frame->width, h_chroma_shift));
258 4944 memset(frame->data[2] + frame->linesize[2] * i,
259 4944 0x80, AV_CEIL_RSHIFT(frame->width, h_chroma_shift));
260 }
261 }
262
263 11940 int ff_mpv_alloc_dummy_frames(MpegEncContext *s)
264 {
265 11940 AVCodecContext *avctx = s->avctx;
266 int ret;
267
268 av_assert1(!s->last_pic.ptr || s->last_pic.ptr->f->buf[0]);
269 av_assert1(!s->next_pic.ptr || s->next_pic.ptr->f->buf[0]);
270
4/4
✓ Branch 0 taken 335 times.
✓ Branch 1 taken 11605 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 314 times.
11940 if (!s->last_pic.ptr && s->pict_type != AV_PICTURE_TYPE_I) {
271
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
21 if (s->pict_type == AV_PICTURE_TYPE_B && s->next_pic.ptr)
272 av_log(avctx, AV_LOG_DEBUG,
273 "allocating dummy last picture for B frame\n");
274
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 6 times.
21 else if (s->codec_id != AV_CODEC_ID_H261 /* H.261 has no keyframes */ &&
275
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
15 (s->picture_structure == PICT_FRAME || s->first_field))
276 av_log(avctx, AV_LOG_ERROR,
277 "warning: first frame is no keyframe\n");
278
279 /* Allocate a dummy frame */
280 21 ret = alloc_dummy_frame(s, &s->last_pic);
281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ret < 0)
282 return ret;
283
284
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (!avctx->hwaccel) {
285
2/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 21 times.
21 int luma_val = s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263 ? 16 : 0x80;
286 21 color_frame(s->last_pic.ptr->f, luma_val);
287 }
288 }
289
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 11940 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
11940 if (!s->next_pic.ptr && s->pict_type == AV_PICTURE_TYPE_B) {
290 /* Allocate a dummy frame */
291 ret = alloc_dummy_frame(s, &s->next_pic);
292 if (ret < 0)
293 return ret;
294 }
295
296
4/6
✓ Branch 0 taken 10703 times.
✓ Branch 1 taken 1237 times.
✓ Branch 2 taken 10703 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 10703 times.
11940 av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_pic.ptr &&
297 s->last_pic.ptr->f->buf[0]));
298
299 11940 return 0;
300 }
301
302 /**
303 * generic function called after decoding
304 * the header and before a frame is decoded.
305 */
306 11230 int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
307 {
308 int ret;
309
310 11230 s->mb_skipped = 0;
311
312
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11230 times.
11230 if (!ff_thread_can_start_frame(avctx)) {
313 av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
314 return AVERROR_BUG;
315 }
316
317 11230 ff_mpv_unref_picture(&s->cur_pic);
318 11230 ret = alloc_picture(s, &s->cur_pic,
319
3/4
✓ Branch 0 taken 8617 times.
✓ Branch 1 taken 2613 times.
✓ Branch 2 taken 8617 times.
✗ Branch 3 not taken.
11230 s->pict_type != AV_PICTURE_TYPE_B && !s->droppable);
320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11230 times.
11230 if (ret < 0)
321 return ret;
322
323
2/2
✓ Branch 0 taken 781 times.
✓ Branch 1 taken 10449 times.
11230 s->cur_pic.ptr->f->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST * !!s->top_field_first;
324 22460 s->cur_pic.ptr->f->flags |= AV_FRAME_FLAG_INTERLACED *
325
3/4
✓ Branch 0 taken 2487 times.
✓ Branch 1 taken 8743 times.
✓ Branch 2 taken 2487 times.
✗ Branch 3 not taken.
11230 (!s->progressive_frame && !s->progressive_sequence);
326 11230 s->cur_pic.ptr->field_picture = s->picture_structure != PICT_FRAME;
327
328 11230 s->cur_pic.ptr->f->pict_type = s->pict_type;
329
2/2
✓ Branch 0 taken 1237 times.
✓ Branch 1 taken 9993 times.
11230 if (s->pict_type == AV_PICTURE_TYPE_I)
330 1237 s->cur_pic.ptr->f->flags |= AV_FRAME_FLAG_KEY;
331 else
332 9993 s->cur_pic.ptr->f->flags &= ~AV_FRAME_FLAG_KEY;
333
334
2/2
✓ Branch 0 taken 8617 times.
✓ Branch 1 taken 2613 times.
11230 if (s->pict_type != AV_PICTURE_TYPE_B) {
335 8617 ff_mpv_workpic_from_pic(&s->last_pic, s->next_pic.ptr);
336
1/2
✓ Branch 0 taken 8617 times.
✗ Branch 1 not taken.
8617 if (!s->droppable)
337 8617 ff_mpv_workpic_from_pic(&s->next_pic, s->cur_pic.ptr);
338 }
339 ff_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
340 (void*)s->last_pic.ptr, (void*)s->next_pic.ptr, (void*)s->cur_pic.ptr,
341 s->last_pic.ptr ? s->last_pic.ptr->f->data[0] : NULL,
342 s->next_pic.ptr ? s->next_pic.ptr->f->data[0] : NULL,
343 s->cur_pic.ptr ? s->cur_pic.ptr->f->data[0] : NULL,
344 s->pict_type, s->droppable);
345
346 11230 ret = ff_mpv_alloc_dummy_frames(s);
347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11230 times.
11230 if (ret < 0)
348 return ret;
349
350
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11230 times.
11230 if (s->avctx->debug & FF_DEBUG_NOMC)
351 color_frame(s->cur_pic.ptr->f, 0x80);
352
353 11230 return 0;
354 }
355
356 /* called after a frame has been decoded. */
357 11230 void ff_mpv_frame_end(MpegEncContext *s)
358 {
359 11230 emms_c();
360
361
2/2
✓ Branch 0 taken 8617 times.
✓ Branch 1 taken 2613 times.
11230 if (s->cur_pic.reference)
362 8617 ff_thread_progress_report(&s->cur_pic.ptr->progress, INT_MAX);
363 11230 }
364
365 11032 void ff_print_debug_info(const MpegEncContext *s, const MPVPicture *p, AVFrame *pict)
366 {
367 11032 ff_print_debug_info2(s->avctx, pict, p->mb_type,
368 11032 p->qscale_table, p->motion_val,
369 11032 p->mb_width, p->mb_height, p->mb_stride, s->quarter_sample);
370 11032 }
371
372 10260 int ff_mpv_export_qp_table(const MpegEncContext *s, AVFrame *f,
373 const MPVPicture *p, int qp_type)
374 {
375 AVVideoEncParams *par;
376
2/2
✓ Branch 0 taken 6421 times.
✓ Branch 1 taken 3839 times.
10260 int mult = (qp_type == FF_MPV_QSCALE_TYPE_MPEG1) ? 2 : 1;
377 10260 unsigned int nb_mb = p->mb_height * p->mb_width;
378
379
2/2
✓ Branch 0 taken 10230 times.
✓ Branch 1 taken 30 times.
10260 if (!(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS))
380 10230 return 0;
381
382 30 par = av_video_enc_params_create_side_data(f, AV_VIDEO_ENC_PARAMS_MPEG2, nb_mb);
383
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (!par)
384 return AVERROR(ENOMEM);
385
386
2/2
✓ Branch 0 taken 540 times.
✓ Branch 1 taken 30 times.
570 for (unsigned y = 0; y < p->mb_height; y++)
387
2/2
✓ Branch 0 taken 11880 times.
✓ Branch 1 taken 540 times.
12420 for (unsigned x = 0; x < p->mb_width; x++) {
388 11880 const unsigned int block_idx = y * p->mb_width + x;
389 11880 const unsigned int mb_xy = y * p->mb_stride + x;
390 11880 AVVideoBlockParams *const b = av_video_enc_params_block(par, block_idx);
391
392 11880 b->src_x = x * 16;
393 11880 b->src_y = y * 16;
394 11880 b->w = 16;
395 11880 b->h = 16;
396
397 11880 b->delta_qp = p->qscale_table[mb_xy] * mult;
398 }
399
400 30 return 0;
401 }
402
403 170338 void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
404 {
405 170338 ff_draw_horiz_band(s->avctx, s->cur_pic.ptr->f,
406
2/2
✓ Branch 0 taken 166897 times.
✓ Branch 1 taken 3441 times.
170338 s->last_pic.ptr ? s->last_pic.ptr->f : NULL,
407 y, h, s->picture_structure,
408 s->first_field, s->low_delay);
409 170338 }
410
411 83 av_cold void ff_mpeg_flush(AVCodecContext *avctx)
412 {
413 83 MpegEncContext *const s = avctx->priv_data;
414
415 83 ff_mpv_unref_picture(&s->cur_pic);
416 83 ff_mpv_unref_picture(&s->last_pic);
417 83 ff_mpv_unref_picture(&s->next_pic);
418
419 83 s->mb_x = s->mb_y = 0;
420
421 83 s->pp_time = 0;
422 83 }
423
424 126300 static inline int hpel_motion_lowres(MpegEncContext *s,
425 uint8_t *dest, const uint8_t *src,
426 int field_based, int field_select,
427 int src_x, int src_y,
428 int width, int height, ptrdiff_t stride,
429 int h_edge_pos, int v_edge_pos,
430 int w, int h, const h264_chroma_mc_func *pix_op,
431 int motion_x, int motion_y)
432 {
433 126300 const int lowres = s->avctx->lowres;
434 126300 const int op_index = lowres;
435 126300 const int s_mask = (2 << lowres) - 1;
436 126300 int emu = 0;
437 int sx, sy;
438
439 av_assert2(op_index <= 3);
440
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126300 times.
126300 if (s->quarter_sample) {
442 motion_x /= 2;
443 motion_y /= 2;
444 }
445
446 126300 sx = motion_x & s_mask;
447 126300 sy = motion_y & s_mask;
448 126300 src_x += motion_x >> lowres + 1;
449 126300 src_y += motion_y >> lowres + 1;
450
451 126300 src += src_y * stride + src_x;
452
453
1/2
✓ Branch 0 taken 126300 times.
✗ Branch 1 not taken.
126300 if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
454
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)) {
455 298 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, src,
456 s->linesize, s->linesize,
457 298 w + 1, (h + 1) << field_based,
458 src_x, src_y * (1 << field_based),
459 h_edge_pos, v_edge_pos);
460 298 src = s->sc.edge_emu_buffer;
461 298 emu = 1;
462 }
463
464 126300 sx = (sx << 2) >> lowres;
465 126300 sy = (sy << 2) >> lowres;
466
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126300 times.
126300 if (field_select)
467 src += s->linesize;
468 126300 pix_op[op_index](dest, src, stride, h, sx, sy);
469 126300 return emu;
470 }
471
472 /* apply one mpeg motion vector to the three components */
473 24814 static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
474 uint8_t *dest_y,
475 uint8_t *dest_cb,
476 uint8_t *dest_cr,
477 int field_based,
478 int bottom_field,
479 int field_select,
480 uint8_t *const *ref_picture,
481 const h264_chroma_mc_func *pix_op,
482 int motion_x, int motion_y,
483 int h, int mb_y)
484 {
485 const uint8_t *ptr_y, *ptr_cb, *ptr_cr;
486 int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
487 ptrdiff_t uvlinesize, linesize;
488 24814 const int lowres = s->avctx->lowres;
489 24814 const int op_index = lowres - 1 + s->chroma_x_shift;
490 24814 const int block_s = 8 >> lowres;
491 24814 const int s_mask = (2 << lowres) - 1;
492 24814 const int h_edge_pos = s->h_edge_pos >> lowres;
493 24814 const int v_edge_pos = s->v_edge_pos >> lowres;
494
1/2
✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
24814 int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;
495
496 av_assert2(op_index <= 3);
497
498 24814 linesize = s->cur_pic.linesize[0] << field_based;
499 24814 uvlinesize = s->cur_pic.linesize[1] << field_based;
500
501 // FIXME obviously not perfect but qpel will not work in lowres anyway
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
24814 if (s->quarter_sample) {
503 motion_x /= 2;
504 motion_y /= 2;
505 }
506
507
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
24814 if (field_based) {
508 motion_y += (bottom_field - field_select)*((1 << lowres)-1);
509 }
510
511 24814 sx = motion_x & s_mask;
512 24814 sy = motion_y & s_mask;
513 24814 src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
514 24814 src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
515
516
1/2
✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
24814 if (s->out_format == FMT_H263) {
517 24814 uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
518 24814 uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
519 24814 uvsrc_x = src_x >> 1;
520 24814 uvsrc_y = src_y >> 1;
521 } else if (s->out_format == FMT_H261) {
522 // even chroma mv's are full pel in H261
523 mx = motion_x / 4;
524 my = motion_y / 4;
525 uvsx = (2 * mx) & s_mask;
526 uvsy = (2 * my) & s_mask;
527 uvsrc_x = s->mb_x * block_s + (mx >> lowres);
528 uvsrc_y = mb_y * block_s + (my >> lowres);
529 } else {
530 if (s->chroma_y_shift) {
531 mx = motion_x / 2;
532 my = motion_y / 2;
533 uvsx = mx & s_mask;
534 uvsy = my & s_mask;
535 uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
536 uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
537 } else {
538 if (s->chroma_x_shift) {
539 //Chroma422
540 mx = motion_x / 2;
541 uvsx = mx & s_mask;
542 uvsy = motion_y & s_mask;
543 uvsrc_y = src_y;
544 uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
545 } else {
546 //Chroma444
547 uvsx = motion_x & s_mask;
548 uvsy = motion_y & s_mask;
549 uvsrc_x = src_x;
550 uvsrc_y = src_y;
551 }
552 }
553 }
554
555 24814 ptr_y = ref_picture[0] + src_y * linesize + src_x;
556 24814 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
557 24814 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
558
559
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 ||
560
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)) {
561 197 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr_y,
562 linesize >> field_based, linesize >> field_based,
563 17, 17 + field_based,
564 src_x, src_y * (1 << field_based), h_edge_pos,
565 v_edge_pos);
566 197 ptr_y = s->sc.edge_emu_buffer;
567 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
568 197 uint8_t *ubuf = s->sc.edge_emu_buffer + 18 * s->linesize;
569 197 uint8_t *vbuf =ubuf + 10 * s->uvlinesize;
570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 197 times.
197 if (s->workaround_bugs & FF_BUG_IEDGE)
571 vbuf -= s->uvlinesize;
572 197 s->vdsp.emulated_edge_mc(ubuf, ptr_cb,
573 uvlinesize >> field_based, uvlinesize >> field_based,
574 9, 9 + field_based,
575 uvsrc_x, uvsrc_y * (1 << field_based),
576 h_edge_pos >> 1, v_edge_pos >> 1);
577 197 s->vdsp.emulated_edge_mc(vbuf, ptr_cr,
578 uvlinesize >> field_based,uvlinesize >> field_based,
579 9, 9 + field_based,
580 uvsrc_x, uvsrc_y * (1 << field_based),
581 h_edge_pos >> 1, v_edge_pos >> 1);
582 197 ptr_cb = ubuf;
583 197 ptr_cr = vbuf;
584 }
585 }
586
587 // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f->data
588
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
24814 if (bottom_field) {
589 dest_y += s->linesize;
590 dest_cb += s->uvlinesize;
591 dest_cr += s->uvlinesize;
592 }
593
594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
24814 if (field_select) {
595 ptr_y += s->linesize;
596 ptr_cb += s->uvlinesize;
597 ptr_cr += s->uvlinesize;
598 }
599
600 24814 sx = (sx << 2) >> lowres;
601 24814 sy = (sy << 2) >> lowres;
602 24814 pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
603
604 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
605 24814 uvsx = (uvsx << 2) >> lowres;
606 24814 uvsy = (uvsy << 2) >> lowres;
607
1/2
✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
24814 if (hc) {
608 24814 pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
609 24814 pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
610 }
611 }
612 // FIXME h261 lowres loop filter
613 24814 }
614
615 31575 static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
616 uint8_t *dest_cb, uint8_t *dest_cr,
617 uint8_t *const *ref_picture,
618 const h264_chroma_mc_func * pix_op,
619 int mx, int my)
620 {
621 31575 const int lowres = s->avctx->lowres;
622 31575 const int op_index = lowres;
623 31575 const int block_s = 8 >> lowres;
624 31575 const int s_mask = (2 << lowres) - 1;
625 31575 const int h_edge_pos = s->h_edge_pos >> lowres + 1;
626 31575 const int v_edge_pos = s->v_edge_pos >> lowres + 1;
627 31575 int emu = 0, src_x, src_y, sx, sy;
628 ptrdiff_t offset;
629 const uint8_t *ptr;
630
631 av_assert2(op_index <= 3);
632
633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31575 times.
31575 if (s->quarter_sample) {
634 mx /= 2;
635 my /= 2;
636 }
637
638 /* In case of 8X8, we construct a single chroma motion vector
639 with a special rounding */
640 31575 mx = ff_h263_round_chroma(mx);
641 31575 my = ff_h263_round_chroma(my);
642
643 31575 sx = mx & s_mask;
644 31575 sy = my & s_mask;
645 31575 src_x = s->mb_x * block_s + (mx >> lowres + 1);
646 31575 src_y = s->mb_y * block_s + (my >> lowres + 1);
647
648 31575 offset = src_y * s->uvlinesize + src_x;
649 31575 ptr = ref_picture[1] + offset;
650
1/2
✓ Branch 0 taken 31575 times.
✗ Branch 1 not taken.
31575 if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
651
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 31431 times.
31575 (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
652 144 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
653 s->uvlinesize, s->uvlinesize,
654 9, 9,
655 src_x, src_y, h_edge_pos, v_edge_pos);
656 144 ptr = s->sc.edge_emu_buffer;
657 144 emu = 1;
658 }
659 31575 sx = (sx << 2) >> lowres;
660 31575 sy = (sy << 2) >> lowres;
661 31575 pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
662
663 31575 ptr = ref_picture[2] + offset;
664
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 31431 times.
31575 if (emu) {
665 144 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
666 s->uvlinesize, s->uvlinesize,
667 9, 9,
668 src_x, src_y, h_edge_pos, v_edge_pos);
669 144 ptr = s->sc.edge_emu_buffer;
670 }
671 31575 pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
672 31575 }
673
674 /**
675 * motion compensation of a single macroblock
676 * @param s context
677 * @param dest_y luma destination pointer
678 * @param dest_cb chroma cb/u destination pointer
679 * @param dest_cr chroma cr/v destination pointer
680 * @param dir direction (0->forward, 1->backward)
681 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
682 * @param pix_op halfpel motion compensation function (average or put normally)
683 * the motion vectors are taken from s->mv and the MV type from s->mv_type
684 */
685 56389 static inline void MPV_motion_lowres(MpegEncContext *s,
686 uint8_t *dest_y, uint8_t *dest_cb,
687 uint8_t *dest_cr,
688 int dir, uint8_t *const *ref_picture,
689 const h264_chroma_mc_func *pix_op)
690 {
691 int mx, my;
692 int mb_x, mb_y;
693 56389 const int lowres = s->avctx->lowres;
694 56389 const int block_s = 8 >>lowres;
695
696 56389 mb_x = s->mb_x;
697 56389 mb_y = s->mb_y;
698
699
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) {
700 24814 case MV_TYPE_16X16:
701 24814 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
702 0, 0, 0,
703 ref_picture, pix_op,
704 s->mv[dir][0][0], s->mv[dir][0][1],
705 2 * block_s, mb_y);
706 24814 break;
707 31575 case MV_TYPE_8X8:
708 31575 mx = 0;
709 31575 my = 0;
710
2/2
✓ Branch 0 taken 126300 times.
✓ Branch 1 taken 31575 times.
157875 for (int i = 0; i < 4; i++) {
711 126300 hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
712 126300 s->linesize) * block_s,
713 ref_picture[0], 0, 0,
714 126300 (2 * mb_x + (i & 1)) * block_s,
715 126300 (2 * mb_y + (i >> 1)) * block_s,
716 s->width, s->height, s->linesize,
717 126300 s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
718 block_s, block_s, pix_op,
719 s->mv[dir][i][0], s->mv[dir][i][1]);
720
721 126300 mx += s->mv[dir][i][0];
722 126300 my += s->mv[dir][i][1];
723 }
724
725 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY))
726 31575 chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
727 pix_op, mx, my);
728 31575 break;
729 case MV_TYPE_FIELD:
730 if (s->picture_structure == PICT_FRAME) {
731 /* top field */
732 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
733 1, 0, s->field_select[dir][0],
734 ref_picture, pix_op,
735 s->mv[dir][0][0], s->mv[dir][0][1],
736 block_s, mb_y);
737 /* bottom field */
738 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
739 1, 1, s->field_select[dir][1],
740 ref_picture, pix_op,
741 s->mv[dir][1][0], s->mv[dir][1][1],
742 block_s, mb_y);
743 } else {
744 if (s->picture_structure != s->field_select[dir][0] + 1 &&
745 s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
746 ref_picture = s->cur_pic.ptr->f->data;
747 }
748 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
749 0, 0, s->field_select[dir][0],
750 ref_picture, pix_op,
751 s->mv[dir][0][0],
752 s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
753 }
754 break;
755 case MV_TYPE_16X8:
756 for (int i = 0; i < 2; i++) {
757 uint8_t *const *ref2picture;
758
759 if (s->picture_structure == s->field_select[dir][i] + 1 ||
760 s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
761 ref2picture = ref_picture;
762 } else {
763 ref2picture = s->cur_pic.ptr->f->data;
764 }
765
766 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
767 0, 0, s->field_select[dir][i],
768 ref2picture, pix_op,
769 s->mv[dir][i][0], s->mv[dir][i][1] +
770 2 * block_s * i, block_s, mb_y >> 1);
771
772 dest_y += 2 * block_s * s->linesize;
773 dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
774 dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
775 }
776 break;
777 case MV_TYPE_DMV:
778 if (s->picture_structure == PICT_FRAME) {
779 for (int i = 0; i < 2; i++) {
780 for (int j = 0; j < 2; j++) {
781 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
782 1, j, j ^ i,
783 ref_picture, pix_op,
784 s->mv[dir][2 * i + j][0],
785 s->mv[dir][2 * i + j][1],
786 block_s, mb_y);
787 }
788 pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
789 }
790 } else {
791 for (int i = 0; i < 2; i++) {
792 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
793 0, 0, s->picture_structure != i + 1,
794 ref_picture, pix_op,
795 s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
796 2 * block_s, mb_y >> 1);
797
798 // after put we make avg of the same block
799 pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
800
801 // opposite parity is always in the same
802 // frame if this is second field
803 if (!s->first_field) {
804 ref_picture = s->cur_pic.ptr->f->data;
805 }
806 }
807 }
808 break;
809 default:
810 av_unreachable("No other mpegvideo MV types exist");
811 }
812 56389 }
813
814 /**
815 * find the lowest MB row referenced in the MVs
816 */
817 60133 static int lowest_referenced_row(MpegEncContext *s, int dir)
818 {
819 60133 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
820 int off, mvs;
821
822
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)
823 goto unhandled;
824
825
1/4
✓ Branch 0 taken 60133 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
60133 switch (s->mv_type) {
826 60133 case MV_TYPE_16X16:
827 60133 mvs = 1;
828 60133 break;
829 case MV_TYPE_16X8:
830 mvs = 2;
831 break;
832 case MV_TYPE_8X8:
833 mvs = 4;
834 break;
835 default:
836 goto unhandled;
837 }
838
839
2/2
✓ Branch 0 taken 60133 times.
✓ Branch 1 taken 60133 times.
120266 for (int i = 0; i < mvs; i++) {
840 60133 int my = s->mv[dir][i][1];
841 60133 my_max = FFMAX(my_max, my);
842 60133 my_min = FFMIN(my_min, my);
843 }
844
845 60133 off = ((FFMAX(-my_min, my_max) << qpel_shift) + 63) >> 6;
846
847 60133 return av_clip(s->mb_y + off, 0, s->mb_height - 1);
848 unhandled:
849 return s->mb_height - 1;
850 }
851
852 /* add block[] to dest[] */
853 23114952 static inline void add_dct(MpegEncContext *s,
854 int16_t *block, int i, uint8_t *dest, int line_size)
855 {
856
2/2
✓ Branch 0 taken 8315893 times.
✓ Branch 1 taken 14799059 times.
23114952 if (s->block_last_index[i] >= 0) {
857 8315893 s->idsp.idct_add(dest, line_size, block);
858 }
859 23114952 }
860
861 /* put block[] to dest[] */
862 1751772 static inline void put_dct(MpegEncContext *s,
863 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
864 {
865 1751772 s->dct_unquantize_intra(s, block, i, qscale);
866 1751772 s->idsp.idct_put(dest, line_size, block);
867 1751772 }
868
869 2942796 static inline void add_dequant_dct(MpegEncContext *s,
870 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
871 {
872
2/2
✓ Branch 0 taken 1108259 times.
✓ Branch 1 taken 1834537 times.
2942796 if (s->block_last_index[i] >= 0) {
873 1108259 s->dct_unquantize_inter(s, block, i, qscale);
874
875 1108259 s->idsp.idct_add(dest, line_size, block);
876 }
877 2942796 }
878
879 #define NOT_MPEG12_H261 0
880 #define MAY_BE_MPEG12_H261 1
881 #define DEFINITELY_MPEG12_H261 2
882
883 /* generic function called after a macroblock has been parsed by the decoder.
884
885 Important variables used:
886 s->mb_intra : true if intra macroblock
887 s->mv_dir : motion vector direction
888 s->mv_type : motion vector type
889 s->mv : motion vector
890 s->interlaced_dct : true if interlaced dct used (mpeg2)
891 */
892 static av_always_inline
893 5142754 void mpv_reconstruct_mb_internal(MpegEncContext *s, int16_t block[12][64],
894 int lowres_flag, int is_mpeg12)
895 {
896 #define IS_MPEG12_H261(s) (is_mpeg12 == MAY_BE_MPEG12_H261 ? ((s)->out_format <= FMT_H261) : is_mpeg12)
897 5142754 uint8_t *dest_y = s->dest[0], *dest_cb = s->dest[1], *dest_cr = s->dest[2];
898 int dct_linesize, dct_offset;
899 5142754 const int linesize = s->cur_pic.linesize[0]; //not s->linesize as this would be wrong for field pics
900 5142754 const int uvlinesize = s->cur_pic.linesize[1];
901
2/2
✓ Branch 0 taken 43500 times.
✓ Branch 1 taken 5099254 times.
5142754 const int block_size = lowres_flag ? 8 >> s->avctx->lowres : 8;
902
903 5142754 dct_linesize = linesize << s->interlaced_dct;
904
2/2
✓ Branch 0 taken 4959173 times.
✓ Branch 1 taken 183581 times.
5142754 dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
905
906
2/2
✓ Branch 0 taken 4510790 times.
✓ Branch 1 taken 631964 times.
5142754 if (!s->mb_intra) {
907 /* motion handling */
908
2/2
✓ Branch 0 taken 2126114 times.
✓ Branch 1 taken 2384676 times.
4510790 if (HAVE_THREADS && is_mpeg12 != DEFINITELY_MPEG12_H261 &&
909
2/2
✓ Branch 0 taken 60133 times.
✓ Branch 1 taken 2065981 times.
2126114 s->avctx->active_thread_type & FF_THREAD_FRAME) {
910
1/2
✓ Branch 0 taken 60133 times.
✗ Branch 1 not taken.
60133 if (s->mv_dir & MV_DIR_FORWARD) {
911 60133 ff_thread_progress_await(&s->last_pic.ptr->progress,
912 lowest_referenced_row(s, 0));
913 }
914
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60133 times.
60133 if (s->mv_dir & MV_DIR_BACKWARD) {
915 ff_thread_progress_await(&s->next_pic.ptr->progress,
916 lowest_referenced_row(s, 1));
917 }
918 }
919
920
2/2
✓ Branch 0 taken 41123 times.
✓ Branch 1 taken 4469667 times.
4510790 if (lowres_flag) {
921 41123 const h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
922
923
2/2
✓ Branch 0 taken 37429 times.
✓ Branch 1 taken 3694 times.
41123 if (s->mv_dir & MV_DIR_FORWARD) {
924 37429 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_pic.data, op_pix);
925 37429 op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
926 }
927
2/2
✓ Branch 0 taken 18960 times.
✓ Branch 1 taken 22163 times.
41123 if (s->mv_dir & MV_DIR_BACKWARD) {
928 18960 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_pic.data, op_pix);
929 }
930 } else {
931 const op_pixels_func (*op_pix)[4];
932 const qpel_mc_func (*op_qpix)[16];
933
934
5/6
✓ Branch 0 taken 2084991 times.
✓ Branch 1 taken 2384676 times.
✓ Branch 2 taken 804284 times.
✓ Branch 3 taken 1280707 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 804284 times.
4469667 if ((is_mpeg12 == DEFINITELY_MPEG12_H261 || !s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
935 3665383 op_pix = s->hdsp.put_pixels_tab;
936 3665383 op_qpix = s->qdsp.put_qpel_pixels_tab;
937 } else {
938 804284 op_pix = s->hdsp.put_no_rnd_pixels_tab;
939 804284 op_qpix = s->qdsp.put_no_rnd_qpel_pixels_tab;
940 }
941
2/2
✓ Branch 0 taken 4166025 times.
✓ Branch 1 taken 303642 times.
4469667 if (s->mv_dir & MV_DIR_FORWARD) {
942 4166025 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_pic.data, op_pix, op_qpix);
943 4166025 op_pix = s->hdsp.avg_pixels_tab;
944 4166025 op_qpix = s->qdsp.avg_qpel_pixels_tab;
945 }
946
2/2
✓ Branch 0 taken 927875 times.
✓ Branch 1 taken 3541792 times.
4469667 if (s->mv_dir & MV_DIR_BACKWARD) {
947 927875 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_pic.data, op_pix, op_qpix);
948 }
949 }
950
951 /* skip dequant / idct if we are really late ;) */
952
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4510790 times.
4510790 if (s->avctx->skip_idct) {
953 if ( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
954 ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
955 || s->avctx->skip_idct >= AVDISCARD_ALL)
956 return;
957 }
958
959 /* add dct residue */
960
4/4
✓ Branch 0 taken 2126114 times.
✓ Branch 1 taken 2384676 times.
✓ Branch 2 taken 490466 times.
✓ Branch 3 taken 1635648 times.
4510790 if (is_mpeg12 != DEFINITELY_MPEG12_H261 && s->dct_unquantize_inter) {
961 // H.263, H.263+, H.263I, FLV, RV10, RV20 and MPEG-4 with MPEG-2 quantization
962 490466 add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
963 490466 add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
964 490466 add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
965 490466 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
966
967 490466 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
968 av_assert2(s->chroma_y_shift);
969 490466 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
970 490466 add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
971 }
972
5/6
✓ Branch 0 taken 1635648 times.
✓ Branch 1 taken 2384676 times.
✓ Branch 2 taken 1635648 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1445499 times.
✓ Branch 5 taken 190149 times.
4020324 } else if (is_mpeg12 == DEFINITELY_MPEG12_H261 || lowres_flag || (s->codec_id != AV_CODEC_ID_WMV2)) {
973 // H.261, MPEG-1, MPEG-2, MPEG-4 with H.263 quantization,
974 // MSMP4V1-3 and WMV1.
975 // Also RV30, RV40 and the VC-1 family when performing error resilience,
976 // but all blocks are skipped in this case.
977 3830175 add_dct(s, block[0], 0, dest_y , dct_linesize);
978 3830175 add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
979 3830175 add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
980 3830175 add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
981
982 3830175 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
983
2/2
✓ Branch 0 taken 3763224 times.
✓ Branch 1 taken 66951 times.
3830175 if (s->chroma_y_shift) {//Chroma420
984 3763224 add_dct(s, block[4], 4, dest_cb, uvlinesize);
985 3763224 add_dct(s, block[5], 5, dest_cr, uvlinesize);
986 } else {
987 //chroma422
988 66951 dct_linesize = uvlinesize << s->interlaced_dct;
989
2/2
✓ Branch 0 taken 65730 times.
✓ Branch 1 taken 1221 times.
66951 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
990
991 66951 add_dct(s, block[4], 4, dest_cb, dct_linesize);
992 66951 add_dct(s, block[5], 5, dest_cr, dct_linesize);
993 66951 add_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize);
994 66951 add_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize);
995
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66951 times.
66951 if (!s->chroma_x_shift) {//Chroma444
996 add_dct(s, block[8], 8, dest_cb + block_size, dct_linesize);
997 add_dct(s, block[9], 9, dest_cr + block_size, dct_linesize);
998 add_dct(s, block[10], 10, dest_cb + block_size + dct_offset, dct_linesize);
999 add_dct(s, block[11], 11, dest_cr + block_size + dct_offset, dct_linesize);
1000 }
1001 }
1002 } //fi gray
1003 } else if (CONFIG_WMV2_DECODER) {
1004 190149 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
1005 }
1006 } else {
1007 /* Only MPEG-4 Simple Studio Profile is supported in > 8-bit mode.
1008 TODO: Integrate 10-bit properly into mpegvideo.c so that ER works properly */
1009
2/2
✓ Branch 0 taken 293312 times.
✓ Branch 1 taken 338652 times.
631964 if (is_mpeg12 != DEFINITELY_MPEG12_H261 && CONFIG_MPEG4_DECODER &&
1010 /* s->codec_id == AV_CODEC_ID_MPEG4 && */
1011
2/2
✓ Branch 0 taken 1350 times.
✓ Branch 1 taken 291962 times.
293312 s->avctx->bits_per_raw_sample > 8) {
1012 1350 ff_mpeg4_decode_studio(s, dest_y, dest_cb, dest_cr, block_size,
1013 uvlinesize, dct_linesize, dct_offset);
1014
4/4
✓ Branch 0 taken 2377 times.
✓ Branch 1 taken 628237 times.
✓ Branch 2 taken 291962 times.
✓ Branch 3 taken 338652 times.
630614 } else if (!IS_MPEG12_H261(s)) {
1015 /* dct only in intra block */
1016 291962 put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
1017 291962 put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
1018 291962 put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
1019 291962 put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
1020
1021 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1022
1/2
✓ Branch 0 taken 291962 times.
✗ Branch 1 not taken.
291962 if (s->chroma_y_shift) {
1023 291962 put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
1024 291962 put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
1025 } else {
1026 dct_offset >>= 1;
1027 dct_linesize >>= 1;
1028 put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
1029 put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
1030 put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
1031 put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
1032 }
1033 }
1034 } else {
1035 338652 s->idsp.idct_put(dest_y, dct_linesize, block[0]);
1036 338652 s->idsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
1037 338652 s->idsp.idct_put(dest_y + dct_offset, dct_linesize, block[2]);
1038 338652 s->idsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
1039
1040 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1041
2/2
✓ Branch 0 taken 284493 times.
✓ Branch 1 taken 54159 times.
338652 if (s->chroma_y_shift) {
1042 284493 s->idsp.idct_put(dest_cb, uvlinesize, block[4]);
1043 284493 s->idsp.idct_put(dest_cr, uvlinesize, block[5]);
1044 } else {
1045 54159 dct_linesize = uvlinesize << s->interlaced_dct;
1046
2/2
✓ Branch 0 taken 54119 times.
✓ Branch 1 taken 40 times.
54159 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
1047
1048 54159 s->idsp.idct_put(dest_cb, dct_linesize, block[4]);
1049 54159 s->idsp.idct_put(dest_cr, dct_linesize, block[5]);
1050 54159 s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
1051 54159 s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
1052
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54159 times.
54159 if (!s->chroma_x_shift) { //Chroma444
1053 s->idsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
1054 s->idsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
1055 s->idsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
1056 s->idsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
1057 }
1058 }
1059 } //gray
1060 }
1061 }
1062 }
1063
1064 static av_cold void debug_dct_coeffs(MPVContext *s, const int16_t block[][64])
1065 {
1066 if (!block) // happens when called via error resilience
1067 return;
1068
1069 void *const logctx = s->avctx;
1070 const uint8_t *const idct_permutation = s->idsp.idct_permutation;
1071
1072 /* print DCT coefficients */
1073 av_log(logctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
1074 for (int i = 0; i < 6; i++) {
1075 for (int j = 0; j < 64; j++) {
1076 av_log(logctx, AV_LOG_DEBUG, "%5d",
1077 block[i][idct_permutation[j]]);
1078 }
1079 av_log(logctx, AV_LOG_DEBUG, "\n");
1080 }
1081 }
1082
1083 5142754 void ff_mpv_reconstruct_mb(MPVContext *s, int16_t block[][64])
1084 {
1085 5142754 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
1086 5142754 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
1087
1088 5142754 s->cur_pic.qscale_table[mb_xy] = s->qscale;
1089
1090 /* avoid copy if macroblock skipped in last frame too */
1091
2/2
✓ Branch 0 taken 388002 times.
✓ Branch 1 taken 4754752 times.
5142754 if (s->mb_skipped) {
1092 388002 s->mb_skipped = 0;
1093 av_assert2(s->pict_type != AV_PICTURE_TYPE_I);
1094 388002 *mbskip_ptr = 1;
1095
2/2
✓ Branch 0 taken 1117555 times.
✓ Branch 1 taken 3637197 times.
4754752 } else if (!s->cur_pic.reference) {
1096 1117555 *mbskip_ptr = 1;
1097 } else{
1098 3637197 *mbskip_ptr = 0; /* not skipped */
1099 }
1100
1101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5142754 times.
5142754 if (s->avctx->debug & FF_DEBUG_DCT_COEFF)
1102 debug_dct_coeffs(s, block);
1103
1104 av_assert2((s->out_format <= FMT_H261) == (s->out_format == FMT_H261 || s->out_format == FMT_MPEG1));
1105
2/2
✓ Branch 0 taken 5099254 times.
✓ Branch 1 taken 43500 times.
5142754 if (!s->avctx->lowres) {
1106 #if !CONFIG_SMALL
1107
2/2
✓ Branch 0 taken 2723328 times.
✓ Branch 1 taken 2375926 times.
5099254 if (s->out_format <= FMT_H261)
1108 2723328 mpv_reconstruct_mb_internal(s, block, 0, DEFINITELY_MPEG12_H261);
1109 else
1110 2375926 mpv_reconstruct_mb_internal(s, block, 0, NOT_MPEG12_H261);
1111 #else
1112 mpv_reconstruct_mb_internal(s, block, 0, MAY_BE_MPEG12_H261);
1113 #endif
1114 } else
1115 43500 mpv_reconstruct_mb_internal(s, block, 1, MAY_BE_MPEG12_H261);
1116 5142754 }
1117