Coverage Report

Created: 2026-02-14 06:59

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