FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_dec.c
Date: 2025-06-01 09:29:47
Exec Total Coverage
Lines: 426 580 73.4%
Functions: 24 24 100.0%
Branches: 182 312 58.3%

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