Coverage Report

Created: 2025-08-28 07:12

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