FFmpeg coverage


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