Coverage Report

Created: 2024-09-06 07:53

/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 "h264chroma.h"
36
#include "internal.h"
37
#include "mpegutils.h"
38
#include "mpegvideo.h"
39
#include "mpegvideodec.h"
40
#include "mpeg4videodec.h"
41
#include "refstruct.h"
42
#include "thread.h"
43
#include "threadprogress.h"
44
#include "wmv2dec.h"
45
46
int ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx)
47
80.0k
{
48
80.0k
    enum ThreadingStatus thread_status;
49
50
80.0k
    ff_mpv_common_defaults(s);
51
52
80.0k
    s->avctx           = avctx;
53
80.0k
    s->width           = avctx->coded_width;
54
80.0k
    s->height          = avctx->coded_height;
55
80.0k
    s->codec_id        = avctx->codec->id;
56
80.0k
    s->workaround_bugs = avctx->workaround_bugs;
57
58
    /* convert fourcc to upper case */
59
80.0k
    s->codec_tag       = ff_toupper4(avctx->codec_tag);
60
61
80.0k
    ff_mpv_idct_init(s);
62
63
80.0k
    ff_h264chroma_init(&s->h264chroma, 8); //for lowres
64
65
80.0k
    if (s->picture_pool)  // VC-1 can call this multiple times
66
31.7k
        return 0;
67
68
48.2k
    thread_status = ff_thread_sync_ref(avctx, offsetof(MpegEncContext, picture_pool));
69
48.2k
    if (thread_status != FF_THREAD_IS_COPY) {
70
48.2k
        s->picture_pool = ff_mpv_alloc_pic_pool(thread_status != FF_THREAD_NO_FRAME_THREADING);
71
48.2k
        if (!s->picture_pool)
72
0
            return AVERROR(ENOMEM);
73
48.2k
    }
74
48.2k
    return 0;
75
48.2k
}
76
77
int ff_mpeg_update_thread_context(AVCodecContext *dst,
78
                                  const AVCodecContext *src)
79
0
{
80
0
    MpegEncContext *const s1 = src->priv_data;
81
0
    MpegEncContext *const s  = dst->priv_data;
82
0
    int ret;
83
84
0
    if (dst == src)
85
0
        return 0;
86
87
0
    av_assert0(s != s1);
88
89
    // FIXME can parameters change on I-frames?
90
    // in that case dst may need a reinit
91
0
    if (!s->context_initialized) {
92
0
        void *private_ctx = s->private_ctx;
93
0
        int err;
94
0
        memcpy(s, s1, sizeof(*s));
95
96
0
        s->context_initialized   = 0;
97
0
        s->context_reinit        = 0;
98
0
        s->avctx                 = dst;
99
0
        s->private_ctx           = private_ctx;
100
0
        s->bitstream_buffer      = NULL;
101
0
        s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
102
103
0
        if (s1->context_initialized) {
104
0
            if ((err = ff_mpv_common_init(s)) < 0)
105
0
                return err;
106
0
        }
107
0
    }
108
109
0
    if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
110
0
        s->height = s1->height;
111
0
        s->width  = s1->width;
112
0
        if ((ret = ff_mpv_common_frame_size_change(s)) < 0)
113
0
            return ret;
114
0
    }
115
116
0
    s->quarter_sample       = s1->quarter_sample;
117
118
0
    s->picture_number       = s1->picture_number;
119
120
0
    ff_mpv_replace_picture(&s->cur_pic,  &s1->cur_pic);
121
0
    ff_mpv_replace_picture(&s->last_pic, &s1->last_pic);
122
0
    ff_mpv_replace_picture(&s->next_pic, &s1->next_pic);
123
124
0
    s->linesize   = s1->linesize;
125
0
    s->uvlinesize = s1->uvlinesize;
126
127
    // Error/bug resilience
128
0
    s->workaround_bugs      = s1->workaround_bugs;
129
0
    s->padding_bug_score    = s1->padding_bug_score;
130
131
    // MPEG-4 timing info
132
0
    memcpy(&s->last_time_base, &s1->last_time_base,
133
0
           (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
134
0
           (char *) &s1->last_time_base);
135
136
    // B-frame info
137
0
    s->max_b_frames = s1->max_b_frames;
138
0
    s->low_delay    = s1->low_delay;
139
140
    // DivX handling (doesn't work)
141
0
    s->divx_packed  = s1->divx_packed;
142
143
0
    if (s1->bitstream_buffer) {
144
0
        av_fast_padded_malloc(&s->bitstream_buffer,
145
0
                              &s->allocated_bitstream_buffer_size,
146
0
                              s1->bitstream_buffer_size);
147
0
        if (!s->bitstream_buffer) {
148
0
            s->bitstream_buffer_size = 0;
149
0
            return AVERROR(ENOMEM);
150
0
        }
151
0
        s->bitstream_buffer_size = s1->bitstream_buffer_size;
152
0
        memcpy(s->bitstream_buffer, s1->bitstream_buffer,
153
0
               s1->bitstream_buffer_size);
154
0
    }
155
156
    // MPEG-2/interlacing info
157
0
    memcpy(&s->progressive_sequence, &s1->progressive_sequence,
158
0
           (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
159
160
0
    return 0;
161
0
}
162
163
int ff_mpv_decode_close(AVCodecContext *avctx)
164
49.3k
{
165
49.3k
    MpegEncContext *s = avctx->priv_data;
166
167
49.3k
    ff_refstruct_pool_uninit(&s->picture_pool);
168
49.3k
    ff_mpv_common_end(s);
169
49.3k
    return 0;
170
49.3k
}
171
172
int ff_mpv_common_frame_size_change(MpegEncContext *s)
173
33.4k
{
174
33.4k
    int err = 0;
175
176
33.4k
    if (!s->context_initialized)
177
0
        return AVERROR(EINVAL);
178
179
33.4k
    ff_mpv_free_context_frame(s);
180
181
33.4k
    ff_mpv_unref_picture(&s->last_pic);
182
33.4k
    ff_mpv_unref_picture(&s->next_pic);
183
33.4k
    ff_mpv_unref_picture(&s->cur_pic);
184
185
33.4k
    if ((s->width || s->height) &&
186
33.4k
        (err = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
187
0
        goto fail;
188
189
    /* set chroma shifts */
190
33.4k
    err = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
191
33.4k
                                           &s->chroma_x_shift,
192
33.4k
                                           &s->chroma_y_shift);
193
33.4k
    if (err < 0)
194
0
        goto fail;
195
196
33.4k
    if ((err = ff_mpv_init_context_frame(s)))
197
0
        goto fail;
198
199
33.4k
    memset(s->thread_context, 0, sizeof(s->thread_context));
200
33.4k
    s->thread_context[0]   = s;
201
202
33.4k
    if (s->width && s->height) {
203
33.4k
        err = ff_mpv_init_duplicate_contexts(s);
204
33.4k
        if (err < 0)
205
0
            goto fail;
206
33.4k
    }
207
33.4k
    s->context_reinit = 0;
208
209
33.4k
    return 0;
210
0
 fail:
211
0
    ff_mpv_free_context_frame(s);
212
0
    s->context_reinit = 1;
213
0
    return err;
214
33.4k
}
215
216
static int alloc_picture(MpegEncContext *s, MPVWorkPicture *dst, int reference)
217
1.13M
{
218
1.13M
    AVCodecContext *avctx = s->avctx;
219
1.13M
    MPVPicture *pic = ff_refstruct_pool_get(s->picture_pool);
220
1.13M
    int ret;
221
222
1.13M
    if (!pic)
223
0
        return AVERROR(ENOMEM);
224
225
1.13M
    dst->ptr = pic;
226
227
1.13M
    pic->reference = reference;
228
229
    /* WM Image / Screen codecs allocate internal buffers with different
230
     * dimensions / colorspaces; ignore user-defined callbacks for these. */
231
1.13M
    if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
232
1.13M
        avctx->codec_id != AV_CODEC_ID_VC1IMAGE  &&
233
1.13M
        avctx->codec_id != AV_CODEC_ID_MSS2) {
234
982k
        ret = ff_thread_get_buffer(avctx, pic->f,
235
982k
                                   reference ? AV_GET_BUFFER_FLAG_REF : 0);
236
982k
    } else {
237
155k
        pic->f->width  = avctx->width;
238
155k
        pic->f->height = avctx->height;
239
155k
        pic->f->format = avctx->pix_fmt;
240
155k
        ret = avcodec_default_get_buffer2(avctx, pic->f, 0);
241
155k
    }
242
1.13M
    if (ret < 0)
243
1.91k
        goto fail;
244
245
1.13M
    ret = ff_mpv_pic_check_linesize(avctx, pic->f, &s->linesize, &s->uvlinesize);
246
1.13M
    if (ret < 0)
247
232
        goto fail;
248
249
1.13M
    ret = ff_hwaccel_frame_priv_alloc(avctx, &pic->hwaccel_picture_private);
250
1.13M
    if (ret < 0)
251
0
        goto fail;
252
253
1.13M
    av_assert1(s->mb_width  == s->buffer_pools.alloc_mb_width);
254
1.13M
    av_assert1(s->mb_height == s->buffer_pools.alloc_mb_height ||
255
1.13M
               FFALIGN(s->mb_height, 2) == s->buffer_pools.alloc_mb_height);
256
1.13M
    av_assert1(s->mb_stride == s->buffer_pools.alloc_mb_stride);
257
1.13M
    ret = ff_mpv_alloc_pic_accessories(s->avctx, dst, &s->sc,
258
1.13M
                                       &s->buffer_pools, s->mb_height);
259
1.13M
    if (ret < 0)
260
2.75k
        goto fail;
261
262
1.13M
    return 0;
263
4.90k
fail:
264
4.90k
    ff_mpv_unref_picture(dst);
265
4.90k
    return ret;
266
1.13M
}
267
268
static int av_cold alloc_dummy_frame(MpegEncContext *s, MPVWorkPicture *dst)
269
159k
{
270
159k
    MPVPicture *pic;
271
159k
    int ret = alloc_picture(s, dst, 1);
272
159k
    if (ret < 0)
273
0
        return ret;
274
275
159k
    pic = dst->ptr;
276
159k
    pic->dummy = 1;
277
278
159k
    ff_thread_progress_report(&pic->progress, INT_MAX);
279
280
159k
    return 0;
281
159k
}
282
283
static void color_frame(AVFrame *frame, int luma)
284
149k
{
285
149k
    int h_chroma_shift, v_chroma_shift;
286
287
182M
    for (int i = 0; i < frame->height; i++)
288
182M
        memset(frame->data[0] + frame->linesize[0] * i, luma, frame->width);
289
290
149k
    if (!frame->data[1])
291
0
        return;
292
149k
    av_pix_fmt_get_chroma_sub_sample(frame->format, &h_chroma_shift, &v_chroma_shift);
293
93.5M
    for (int i = 0; i < AV_CEIL_RSHIFT(frame->height, v_chroma_shift); i++) {
294
93.3M
        memset(frame->data[1] + frame->linesize[1] * i,
295
93.3M
               0x80, AV_CEIL_RSHIFT(frame->width, h_chroma_shift));
296
93.3M
        memset(frame->data[2] + frame->linesize[2] * i,
297
93.3M
               0x80, AV_CEIL_RSHIFT(frame->width, h_chroma_shift));
298
93.3M
    }
299
149k
}
300
301
int ff_mpv_alloc_dummy_frames(MpegEncContext *s)
302
982k
{
303
982k
    AVCodecContext *avctx = s->avctx;
304
982k
    int ret;
305
306
982k
    av_assert1(!s->last_pic.ptr || s->last_pic.ptr->f->buf[0]);
307
982k
    av_assert1(!s->next_pic.ptr || s->next_pic.ptr->f->buf[0]);
308
982k
    if (!s->last_pic.ptr && s->pict_type != AV_PICTURE_TYPE_I) {
309
149k
        if (s->pict_type == AV_PICTURE_TYPE_B && s->next_pic.ptr)
310
322
            av_log(avctx, AV_LOG_DEBUG,
311
322
                   "allocating dummy last picture for B frame\n");
312
149k
        else if (s->codec_id != AV_CODEC_ID_H261 /* H.261 has no keyframes */ &&
313
149k
                 (s->picture_structure == PICT_FRAME || s->first_field))
314
148k
            av_log(avctx, AV_LOG_ERROR,
315
148k
                   "warning: first frame is no keyframe\n");
316
317
        /* Allocate a dummy frame */
318
149k
        ret = alloc_dummy_frame(s, &s->last_pic);
319
149k
        if (ret < 0)
320
0
            return ret;
321
322
149k
        if (!avctx->hwaccel) {
323
149k
            int luma_val = s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263 ? 16 : 0x80;
324
149k
            color_frame(s->last_pic.ptr->f, luma_val);
325
149k
        }
326
149k
    }
327
982k
    if (!s->next_pic.ptr && s->pict_type == AV_PICTURE_TYPE_B) {
328
        /* Allocate a dummy frame */
329
9.70k
        ret = alloc_dummy_frame(s, &s->next_pic);
330
9.70k
        if (ret < 0)
331
0
            return ret;
332
9.70k
    }
333
334
982k
    av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_pic.ptr &&
335
982k
                                                 s->last_pic.ptr->f->buf[0]));
336
337
982k
    return 0;
338
982k
}
339
340
/**
341
 * generic function called after decoding
342
 * the header and before a frame is decoded.
343
 */
344
int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
345
978k
{
346
978k
    int ret;
347
348
978k
    s->mb_skipped = 0;
349
350
978k
    if (!ff_thread_can_start_frame(avctx)) {
351
0
        av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
352
0
        return AVERROR_BUG;
353
0
    }
354
355
978k
    ff_mpv_unref_picture(&s->cur_pic);
356
978k
    ret = alloc_picture(s, &s->cur_pic,
357
978k
                        s->pict_type != AV_PICTURE_TYPE_B && !s->droppable);
358
978k
    if (ret < 0)
359
4.90k
        return ret;
360
361
973k
    s->cur_pic.ptr->f->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST * !!s->top_field_first;
362
973k
    s->cur_pic.ptr->f->flags |= AV_FRAME_FLAG_INTERLACED *
363
973k
                                (!s->progressive_frame && !s->progressive_sequence);
364
973k
    s->cur_pic.ptr->field_picture = s->picture_structure != PICT_FRAME;
365
366
973k
    s->cur_pic.ptr->f->pict_type = s->pict_type;
367
973k
    if (s->pict_type == AV_PICTURE_TYPE_I)
368
470k
        s->cur_pic.ptr->f->flags |= AV_FRAME_FLAG_KEY;
369
502k
    else
370
502k
        s->cur_pic.ptr->f->flags &= ~AV_FRAME_FLAG_KEY;
371
372
973k
    if (s->pict_type != AV_PICTURE_TYPE_B) {
373
769k
        ff_mpv_workpic_from_pic(&s->last_pic, s->next_pic.ptr);
374
769k
        if (!s->droppable)
375
769k
            ff_mpv_workpic_from_pic(&s->next_pic, s->cur_pic.ptr);
376
769k
    }
377
973k
    ff_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
378
973k
            (void*)s->last_pic.ptr, (void*)s->next_pic.ptr, (void*)s->cur_pic.ptr,
379
973k
            s->last_pic.ptr ? s->last_pic.ptr->f->data[0] : NULL,
380
973k
            s->next_pic.ptr ? s->next_pic.ptr->f->data[0] : NULL,
381
973k
            s->cur_pic.ptr  ? s->cur_pic.ptr->f->data[0]  : NULL,
382
973k
            s->pict_type, s->droppable);
383
384
973k
    ret = ff_mpv_alloc_dummy_frames(s);
385
973k
    if (ret < 0)
386
0
        return ret;
387
388
973k
    if (s->avctx->debug & FF_DEBUG_NOMC)
389
0
        color_frame(s->cur_pic.ptr->f, 0x80);
390
391
973k
    return 0;
392
973k
}
393
394
/* called after a frame has been decoded. */
395
void ff_mpv_frame_end(MpegEncContext *s)
396
892k
{
397
892k
    emms_c();
398
399
892k
    if (s->cur_pic.reference)
400
737k
        ff_thread_progress_report(&s->cur_pic.ptr->progress, INT_MAX);
401
892k
}
402
403
void ff_print_debug_info(const MpegEncContext *s, const MPVPicture *p, AVFrame *pict)
404
446k
{
405
446k
    ff_print_debug_info2(s->avctx, pict, p->mb_type,
406
446k
                         p->qscale_table, p->motion_val,
407
446k
                         s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
408
446k
}
409
410
int ff_mpv_export_qp_table(const MpegEncContext *s, AVFrame *f,
411
                           const MPVPicture *p, int qp_type)
412
105k
{
413
105k
    AVVideoEncParams *par;
414
105k
    int mult = (qp_type == FF_MPV_QSCALE_TYPE_MPEG1) ? 2 : 1;
415
105k
    unsigned int nb_mb = p->mb_height * p->mb_width;
416
417
105k
    if (!(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS))
418
56.6k
        return 0;
419
420
48.8k
    par = av_video_enc_params_create_side_data(f, AV_VIDEO_ENC_PARAMS_MPEG2, nb_mb);
421
48.8k
    if (!par)
422
0
        return AVERROR(ENOMEM);
423
424
4.67M
    for (unsigned y = 0; y < p->mb_height; y++)
425
47.5M
        for (unsigned x = 0; x < p->mb_width; x++) {
426
42.9M
            const unsigned int block_idx = y * p->mb_width + x;
427
42.9M
            const unsigned int     mb_xy = y * p->mb_stride + x;
428
42.9M
            AVVideoBlockParams *const b = av_video_enc_params_block(par, block_idx);
429
430
42.9M
            b->src_x = x * 16;
431
42.9M
            b->src_y = y * 16;
432
42.9M
            b->w     = 16;
433
42.9M
            b->h     = 16;
434
435
42.9M
            b->delta_qp = p->qscale_table[mb_xy] * mult;
436
42.9M
        }
437
438
48.8k
    return 0;
439
48.8k
}
440
441
void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
442
885k
{
443
885k
    ff_draw_horiz_band(s->avctx, s->cur_pic.ptr->f,
444
885k
                       s->last_pic.ptr ? s->last_pic.ptr->f : NULL,
445
885k
                       y, h, s->picture_structure,
446
885k
                       s->first_field, s->low_delay);
447
885k
}
448
449
void ff_mpeg_flush(AVCodecContext *avctx)
450
532k
{
451
532k
    MpegEncContext *const s = avctx->priv_data;
452
453
532k
    ff_mpv_unref_picture(&s->cur_pic);
454
532k
    ff_mpv_unref_picture(&s->last_pic);
455
532k
    ff_mpv_unref_picture(&s->next_pic);
456
457
532k
    s->mb_x = s->mb_y = 0;
458
459
532k
    s->bitstream_buffer_size = 0;
460
532k
    s->pp_time = 0;
461
532k
}
462
463
void ff_mpv_report_decode_progress(MpegEncContext *s)
464
885k
{
465
885k
    if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
466
480k
        ff_thread_progress_report(&s->cur_pic.ptr->progress, s->mb_y);
467
885k
}
468
469
470
static inline int hpel_motion_lowres(MpegEncContext *s,
471
                                     uint8_t *dest, const uint8_t *src,
472
                                     int field_based, int field_select,
473
                                     int src_x, int src_y,
474
                                     int width, int height, ptrdiff_t stride,
475
                                     int h_edge_pos, int v_edge_pos,
476
                                     int w, int h, const h264_chroma_mc_func *pix_op,
477
                                     int motion_x, int motion_y)
478
0
{
479
0
    const int lowres   = s->avctx->lowres;
480
0
    const int op_index = lowres;
481
0
    const int s_mask   = (2 << lowres) - 1;
482
0
    int emu = 0;
483
0
    int sx, sy;
484
485
0
    av_assert2(op_index <= 3);
486
487
0
    if (s->quarter_sample) {
488
0
        motion_x /= 2;
489
0
        motion_y /= 2;
490
0
    }
491
492
0
    sx = motion_x & s_mask;
493
0
    sy = motion_y & s_mask;
494
0
    src_x += motion_x >> lowres + 1;
495
0
    src_y += motion_y >> lowres + 1;
496
497
0
    src   += src_y * stride + src_x;
498
499
0
    if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w,                 0) ||
500
0
        (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
501
0
        s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, src,
502
0
                                 s->linesize, s->linesize,
503
0
                                 w + 1, (h + 1) << field_based,
504
0
                                 src_x, src_y * (1 << field_based),
505
0
                                 h_edge_pos, v_edge_pos);
506
0
        src = s->sc.edge_emu_buffer;
507
0
        emu = 1;
508
0
    }
509
510
0
    sx = (sx << 2) >> lowres;
511
0
    sy = (sy << 2) >> lowres;
512
0
    if (field_select)
513
0
        src += s->linesize;
514
0
    pix_op[op_index](dest, src, stride, h, sx, sy);
515
0
    return emu;
516
0
}
517
518
/* apply one mpeg motion vector to the three components */
519
static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
520
                                                uint8_t *dest_y,
521
                                                uint8_t *dest_cb,
522
                                                uint8_t *dest_cr,
523
                                                int field_based,
524
                                                int bottom_field,
525
                                                int field_select,
526
                                                uint8_t *const *ref_picture,
527
                                                const h264_chroma_mc_func *pix_op,
528
                                                int motion_x, int motion_y,
529
                                                int h, int mb_y)
530
1.59M
{
531
1.59M
    const uint8_t *ptr_y, *ptr_cb, *ptr_cr;
532
1.59M
    int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
533
1.59M
    ptrdiff_t uvlinesize, linesize;
534
1.59M
    const int lowres     = s->avctx->lowres;
535
1.59M
    const int op_index   = lowres - 1 + s->chroma_x_shift;
536
1.59M
    const int block_s    = 8 >> lowres;
537
1.59M
    const int s_mask     = (2 << lowres) - 1;
538
1.59M
    const int h_edge_pos = s->h_edge_pos >> lowres;
539
1.59M
    const int v_edge_pos = s->v_edge_pos >> lowres;
540
1.59M
    int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;
541
542
1.59M
    av_assert2(op_index <= 3);
543
544
1.59M
    linesize   = s->cur_pic.linesize[0] << field_based;
545
1.59M
    uvlinesize = s->cur_pic.linesize[1] << field_based;
546
547
    // FIXME obviously not perfect but qpel will not work in lowres anyway
548
1.59M
    if (s->quarter_sample) {
549
0
        motion_x /= 2;
550
0
        motion_y /= 2;
551
0
    }
552
553
1.59M
    if (field_based) {
554
23.9k
        motion_y += (bottom_field - field_select)*((1 << lowres)-1);
555
23.9k
    }
556
557
1.59M
    sx = motion_x & s_mask;
558
1.59M
    sy = motion_y & s_mask;
559
1.59M
    src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
560
1.59M
    src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
561
562
1.59M
    if (s->out_format == FMT_H263) {
563
970k
        uvsx    = ((motion_x >> 1) & s_mask) | (sx & 1);
564
970k
        uvsy    = ((motion_y >> 1) & s_mask) | (sy & 1);
565
970k
        uvsrc_x = src_x >> 1;
566
970k
        uvsrc_y = src_y >> 1;
567
970k
    } else if (s->out_format == FMT_H261) {
568
        // even chroma mv's are full pel in H261
569
0
        mx      = motion_x / 4;
570
0
        my      = motion_y / 4;
571
0
        uvsx    = (2 * mx) & s_mask;
572
0
        uvsy    = (2 * my) & s_mask;
573
0
        uvsrc_x = s->mb_x * block_s + (mx >> lowres);
574
0
        uvsrc_y =    mb_y * block_s + (my >> lowres);
575
627k
    } else {
576
627k
        if (s->chroma_y_shift) {
577
598k
            mx      = motion_x / 2;
578
598k
            my      = motion_y / 2;
579
598k
            uvsx    = mx & s_mask;
580
598k
            uvsy    = my & s_mask;
581
598k
            uvsrc_x = s->mb_x * block_s                 + (mx >> lowres + 1);
582
598k
            uvsrc_y =   (mb_y * block_s >> field_based) + (my >> lowres + 1);
583
598k
        } else {
584
29.5k
            if (s->chroma_x_shift) {
585
            //Chroma422
586
18.5k
                mx = motion_x / 2;
587
18.5k
                uvsx = mx & s_mask;
588
18.5k
                uvsy = motion_y & s_mask;
589
18.5k
                uvsrc_y = src_y;
590
18.5k
                uvsrc_x = s->mb_x*block_s               + (mx >> (lowres+1));
591
18.5k
            } else {
592
            //Chroma444
593
10.9k
                uvsx = motion_x & s_mask;
594
10.9k
                uvsy = motion_y & s_mask;
595
10.9k
                uvsrc_x = src_x;
596
10.9k
                uvsrc_y = src_y;
597
10.9k
            }
598
29.5k
        }
599
627k
    }
600
601
1.59M
    ptr_y  = ref_picture[0] + src_y   * linesize   + src_x;
602
1.59M
    ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
603
1.59M
    ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
604
605
1.59M
    if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s,       0) || uvsrc_y<0 ||
606
1.59M
        (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - FFMAX(h, hc<<s->chroma_y_shift), 0)) {
607
89.6k
        s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr_y,
608
89.6k
                                 linesize >> field_based, linesize >> field_based,
609
89.6k
                                 17, 17 + field_based,
610
89.6k
                                src_x, src_y * (1 << field_based), h_edge_pos,
611
89.6k
                                v_edge_pos);
612
89.6k
        ptr_y = s->sc.edge_emu_buffer;
613
89.6k
        if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
614
89.6k
            uint8_t *ubuf = s->sc.edge_emu_buffer + 18 * s->linesize;
615
89.6k
            uint8_t *vbuf =ubuf + 10 * s->uvlinesize;
616
89.6k
            if (s->workaround_bugs & FF_BUG_IEDGE)
617
15.6k
                vbuf -= s->uvlinesize;
618
89.6k
            s->vdsp.emulated_edge_mc(ubuf,  ptr_cb,
619
89.6k
                                     uvlinesize >> field_based, uvlinesize >> field_based,
620
89.6k
                                     9, 9 + field_based,
621
89.6k
                                    uvsrc_x, uvsrc_y * (1 << field_based),
622
89.6k
                                    h_edge_pos >> 1, v_edge_pos >> 1);
623
89.6k
            s->vdsp.emulated_edge_mc(vbuf,  ptr_cr,
624
89.6k
                                     uvlinesize >> field_based,uvlinesize >> field_based,
625
89.6k
                                     9, 9 + field_based,
626
89.6k
                                    uvsrc_x, uvsrc_y * (1 << field_based),
627
89.6k
                                    h_edge_pos >> 1, v_edge_pos >> 1);
628
89.6k
            ptr_cb = ubuf;
629
89.6k
            ptr_cr = vbuf;
630
89.6k
        }
631
89.6k
    }
632
633
    // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f->data
634
1.59M
    if (bottom_field) {
635
11.9k
        dest_y  += s->linesize;
636
11.9k
        dest_cb += s->uvlinesize;
637
11.9k
        dest_cr += s->uvlinesize;
638
11.9k
    }
639
640
1.59M
    if (field_select) {
641
28.0k
        ptr_y   += s->linesize;
642
28.0k
        ptr_cb  += s->uvlinesize;
643
28.0k
        ptr_cr  += s->uvlinesize;
644
28.0k
    }
645
646
1.59M
    sx = (sx << 2) >> lowres;
647
1.59M
    sy = (sy << 2) >> lowres;
648
1.59M
    pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
649
650
1.59M
    if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
651
1.59M
        uvsx = (uvsx << 2) >> lowres;
652
1.59M
        uvsy = (uvsy << 2) >> lowres;
653
1.59M
        if (hc) {
654
1.59M
            pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
655
1.59M
            pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
656
1.59M
        }
657
1.59M
    }
658
    // FIXME h261 lowres loop filter
659
1.59M
}
660
661
static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
662
                                            uint8_t *dest_cb, uint8_t *dest_cr,
663
                                            uint8_t *const *ref_picture,
664
                                            const h264_chroma_mc_func * pix_op,
665
                                            int mx, int my)
666
0
{
667
0
    const int lowres     = s->avctx->lowres;
668
0
    const int op_index   = lowres;
669
0
    const int block_s    = 8 >> lowres;
670
0
    const int s_mask     = (2 << lowres) - 1;
671
0
    const int h_edge_pos = s->h_edge_pos >> lowres + 1;
672
0
    const int v_edge_pos = s->v_edge_pos >> lowres + 1;
673
0
    int emu = 0, src_x, src_y, sx, sy;
674
0
    ptrdiff_t offset;
675
0
    const uint8_t *ptr;
676
677
0
    av_assert2(op_index <= 3);
678
679
0
    if (s->quarter_sample) {
680
0
        mx /= 2;
681
0
        my /= 2;
682
0
    }
683
684
    /* In case of 8X8, we construct a single chroma motion vector
685
       with a special rounding */
686
0
    mx = ff_h263_round_chroma(mx);
687
0
    my = ff_h263_round_chroma(my);
688
689
0
    sx = mx & s_mask;
690
0
    sy = my & s_mask;
691
0
    src_x = s->mb_x * block_s + (mx >> lowres + 1);
692
0
    src_y = s->mb_y * block_s + (my >> lowres + 1);
693
694
0
    offset = src_y * s->uvlinesize + src_x;
695
0
    ptr = ref_picture[1] + offset;
696
0
    if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
697
0
        (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
698
0
        s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
699
0
                                 s->uvlinesize, s->uvlinesize,
700
0
                                 9, 9,
701
0
                                 src_x, src_y, h_edge_pos, v_edge_pos);
702
0
        ptr = s->sc.edge_emu_buffer;
703
0
        emu = 1;
704
0
    }
705
0
    sx = (sx << 2) >> lowres;
706
0
    sy = (sy << 2) >> lowres;
707
0
    pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
708
709
0
    ptr = ref_picture[2] + offset;
710
0
    if (emu) {
711
0
        s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
712
0
                                 s->uvlinesize, s->uvlinesize,
713
0
                                 9, 9,
714
0
                                 src_x, src_y, h_edge_pos, v_edge_pos);
715
0
        ptr = s->sc.edge_emu_buffer;
716
0
    }
717
0
    pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
718
0
}
719
720
/**
721
 * motion compensation of a single macroblock
722
 * @param s context
723
 * @param dest_y luma destination pointer
724
 * @param dest_cb chroma cb/u destination pointer
725
 * @param dest_cr chroma cr/v destination pointer
726
 * @param dir direction (0->forward, 1->backward)
727
 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
728
 * @param pix_op halfpel motion compensation function (average or put normally)
729
 * the motion vectors are taken from s->mv and the MV type from s->mv_type
730
 */
731
static inline void MPV_motion_lowres(MpegEncContext *s,
732
                                     uint8_t *dest_y, uint8_t *dest_cb,
733
                                     uint8_t *dest_cr,
734
                                     int dir, uint8_t *const *ref_picture,
735
                                     const h264_chroma_mc_func *pix_op)
736
1.57M
{
737
1.57M
    int mx, my;
738
1.57M
    int mb_x, mb_y;
739
1.57M
    const int lowres  = s->avctx->lowres;
740
1.57M
    const int block_s = 8 >>lowres;
741
742
1.57M
    mb_x = s->mb_x;
743
1.57M
    mb_y = s->mb_y;
744
745
1.57M
    switch (s->mv_type) {
746
1.54M
    case MV_TYPE_16X16:
747
1.54M
        mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
748
1.54M
                           0, 0, 0,
749
1.54M
                           ref_picture, pix_op,
750
1.54M
                           s->mv[dir][0][0], s->mv[dir][0][1],
751
1.54M
                           2 * block_s, mb_y);
752
1.54M
        break;
753
0
    case MV_TYPE_8X8:
754
0
        mx = 0;
755
0
        my = 0;
756
0
        for (int i = 0; i < 4; i++) {
757
0
            hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
758
0
                               s->linesize) * block_s,
759
0
                               ref_picture[0], 0, 0,
760
0
                               (2 * mb_x + (i & 1)) * block_s,
761
0
                               (2 * mb_y + (i >> 1)) * block_s,
762
0
                               s->width, s->height, s->linesize,
763
0
                               s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
764
0
                               block_s, block_s, pix_op,
765
0
                               s->mv[dir][i][0], s->mv[dir][i][1]);
766
767
0
            mx += s->mv[dir][i][0];
768
0
            my += s->mv[dir][i][1];
769
0
        }
770
771
0
        if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY))
772
0
            chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
773
0
                                     pix_op, mx, my);
774
0
        break;
775
21.3k
    case MV_TYPE_FIELD:
776
21.3k
        if (s->picture_structure == PICT_FRAME) {
777
            /* top field */
778
5.47k
            mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
779
5.47k
                               1, 0, s->field_select[dir][0],
780
5.47k
                               ref_picture, pix_op,
781
5.47k
                               s->mv[dir][0][0], s->mv[dir][0][1],
782
5.47k
                               block_s, mb_y);
783
            /* bottom field */
784
5.47k
            mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
785
5.47k
                               1, 1, s->field_select[dir][1],
786
5.47k
                               ref_picture, pix_op,
787
5.47k
                               s->mv[dir][1][0], s->mv[dir][1][1],
788
5.47k
                               block_s, mb_y);
789
15.9k
        } else {
790
15.9k
            if (s->picture_structure != s->field_select[dir][0] + 1 &&
791
15.9k
                s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
792
494
                ref_picture = s->cur_pic.ptr->f->data;
793
494
            }
794
15.9k
            mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
795
15.9k
                               0, 0, s->field_select[dir][0],
796
15.9k
                               ref_picture, pix_op,
797
15.9k
                               s->mv[dir][0][0],
798
15.9k
                               s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
799
15.9k
            }
800
21.3k
        break;
801
3.67k
    case MV_TYPE_16X8:
802
11.0k
        for (int i = 0; i < 2; i++) {
803
7.35k
            uint8_t *const *ref2picture;
804
805
7.35k
            if (s->picture_structure == s->field_select[dir][i] + 1 ||
806
7.35k
                s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
807
6.80k
                ref2picture = ref_picture;
808
6.80k
            } else {
809
551
                ref2picture = s->cur_pic.ptr->f->data;
810
551
            }
811
812
7.35k
            mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
813
7.35k
                               0, 0, s->field_select[dir][i],
814
7.35k
                               ref2picture, pix_op,
815
7.35k
                               s->mv[dir][i][0], s->mv[dir][i][1] +
816
7.35k
                               2 * block_s * i, block_s, mb_y >> 1);
817
818
7.35k
            dest_y  +=  2 * block_s *  s->linesize;
819
7.35k
            dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
820
7.35k
            dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
821
7.35k
        }
822
3.67k
        break;
823
7.33k
    case MV_TYPE_DMV:
824
7.33k
        if (s->picture_structure == PICT_FRAME) {
825
9.76k
            for (int i = 0; i < 2; i++) {
826
19.5k
                for (int j = 0; j < 2; j++) {
827
13.0k
                    mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
828
13.0k
                                       1, j, j ^ i,
829
13.0k
                                       ref_picture, pix_op,
830
13.0k
                                       s->mv[dir][2 * i + j][0],
831
13.0k
                                       s->mv[dir][2 * i + j][1],
832
13.0k
                                       block_s, mb_y);
833
13.0k
                }
834
6.51k
                pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
835
6.51k
            }
836
4.07k
        } else {
837
12.2k
            for (int i = 0; i < 2; i++) {
838
8.15k
                mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
839
8.15k
                                   0, 0, s->picture_structure != i + 1,
840
8.15k
                                   ref_picture, pix_op,
841
8.15k
                                   s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
842
8.15k
                                   2 * block_s, mb_y >> 1);
843
844
                // after put we make avg of the same block
845
8.15k
                pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
846
847
                // opposite parity is always in the same
848
                // frame if this is second field
849
8.15k
                if (!s->first_field) {
850
3.01k
                    ref_picture = s->cur_pic.ptr->f->data;
851
3.01k
                }
852
8.15k
            }
853
4.07k
        }
854
7.33k
        break;
855
0
    default:
856
0
        av_assert2(0);
857
1.57M
    }
858
1.57M
}
859
860
/**
861
 * find the lowest MB row referenced in the MVs
862
 */
863
static int lowest_referenced_row(MpegEncContext *s, int dir)
864
0
{
865
0
    int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
866
0
    int off, mvs;
867
868
0
    if (s->picture_structure != PICT_FRAME || s->mcsel)
869
0
        goto unhandled;
870
871
0
    switch (s->mv_type) {
872
0
        case MV_TYPE_16X16:
873
0
            mvs = 1;
874
0
            break;
875
0
        case MV_TYPE_16X8:
876
0
            mvs = 2;
877
0
            break;
878
0
        case MV_TYPE_8X8:
879
0
            mvs = 4;
880
0
            break;
881
0
        default:
882
0
            goto unhandled;
883
0
    }
884
885
0
    for (int i = 0; i < mvs; i++) {
886
0
        int my = s->mv[dir][i][1];
887
0
        my_max = FFMAX(my_max, my);
888
0
        my_min = FFMIN(my_min, my);
889
0
    }
890
891
0
    off = ((FFMAX(-my_min, my_max) << qpel_shift) + 63) >> 6;
892
893
0
    return av_clip(s->mb_y + off, 0, s->mb_height - 1);
894
0
unhandled:
895
0
    return s->mb_height - 1;
896
0
}
897
898
/* add block[] to dest[] */
899
static inline void add_dct(MpegEncContext *s,
900
                           int16_t *block, int i, uint8_t *dest, int line_size)
901
1.92G
{
902
1.92G
    if (s->block_last_index[i] >= 0) {
903
470k
        s->idsp.idct_add(dest, line_size, block);
904
470k
    }
905
1.92G
}
906
907
560M
#define IS_ENCODER 0
908
#include "mpv_reconstruct_mb_template.c"
909
910
void ff_mpv_reconstruct_mb(MpegEncContext *s, int16_t block[12][64])
911
401M
{
912
401M
    if (s->avctx->debug & FF_DEBUG_DCT_COEFF) {
913
       /* print DCT coefficients */
914
0
       av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
915
0
       for (int i = 0; i < 6; i++) {
916
0
           for (int j = 0; j < 64; j++) {
917
0
               av_log(s->avctx, AV_LOG_DEBUG, "%5d",
918
0
                      block[i][s->idsp.idct_permutation[j]]);
919
0
           }
920
0
           av_log(s->avctx, AV_LOG_DEBUG, "\n");
921
0
       }
922
0
    }
923
924
401M
    av_assert2((s->out_format <= FMT_H261) == (s->out_format == FMT_H261 || s->out_format == FMT_MPEG1));
925
401M
    if (!s->avctx->lowres) {
926
399M
#if !CONFIG_SMALL
927
399M
        if (s->out_format <= FMT_H261)
928
7.41M
            mpv_reconstruct_mb_internal(s, block, 0, DEFINITELY_MPEG12_H261);
929
392M
        else
930
392M
            mpv_reconstruct_mb_internal(s, block, 0, NOT_MPEG12_H261);
931
#else
932
        mpv_reconstruct_mb_internal(s, block, 0, MAY_BE_MPEG12_H261);
933
#endif
934
399M
    } else
935
1.80M
        mpv_reconstruct_mb_internal(s, block, 1, MAY_BE_MPEG12_H261);
936
401M
}