FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_dec.c
Date: 2026-04-24 19:58:39
Exec Total Coverage
Lines: 428 590 72.5%
Functions: 24 27 88.9%
Branches: 182 338 53.8%

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