mpegvideo.c 121 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1 2
/*
 * The simplest mpeg encoder (well, it was the simplest!)
3
 * Copyright (c) 2000,2001 Fabrice Bellard
4
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
Fabrice Bellard's avatar
Fabrice Bellard committed
5
 *
6 7
 * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
 *
8 9 10
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
11 12
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
13
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
14
 *
15
 * FFmpeg is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
19
 *
20
 * You should have received a copy of the GNU Lesser General Public
21
 * License along with FFmpeg; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Fabrice Bellard's avatar
Fabrice Bellard committed
23
 */
24

Michael Niedermayer's avatar
Michael Niedermayer committed
25
/**
26
 * @file
Michael Niedermayer's avatar
Michael Niedermayer committed
27
 * The simplest mpeg encoder (well, it was the simplest!).
28 29
 */

30
#include "libavutil/avassert.h"
31
#include "libavutil/imgutils.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
32 33
#include "avcodec.h"
#include "dsputil.h"
34
#include "h264chroma.h"
35
#include "internal.h"
36
#include "mathops.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
37
#include "mpegvideo.h"
38
#include "mjpegenc.h"
39
#include "msmpeg4.h"
40
#include "xvmc_internal.h"
41
#include "thread.h"
42
#include <limits.h>
Fabrice Bellard's avatar
Fabrice Bellard committed
43

44 45
//#undef NDEBUG
//#include <assert.h>
46

47
static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
48
                                   int16_t *block, int n, int qscale);
49
static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
50
                                   int16_t *block, int n, int qscale);
51
static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
52
                                   int16_t *block, int n, int qscale);
53
static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
54
                                   int16_t *block, int n, int qscale);
55
static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
56
                                   int16_t *block, int n, int qscale);
57
static void dct_unquantize_h263_intra_c(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
58
                                  int16_t *block, int n, int qscale);
59
static void dct_unquantize_h263_inter_c(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
60
                                  int16_t *block, int n, int qscale);
61

Fabrice Bellard's avatar
Fabrice Bellard committed
62 63 64

//#define DEBUG

65

66 67 68 69
static const uint8_t ff_default_chroma_qscale_table[32] = {
//   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
     0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
    16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
Michael Niedermayer's avatar
Michael Niedermayer committed
70 71
};

72 73 74 75 76 77 78 79 80 81
const uint8_t ff_mpeg1_dc_scale_table[128] = {
//  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
82 83
};

84 85 86 87 88 89 90 91 92 93
static const uint8_t mpeg2_dc_scale_table1[128] = {
//  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
94 95
};

96 97 98 99 100 101 102 103 104 105
static const uint8_t mpeg2_dc_scale_table2[128] = {
//  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
106 107
};

108 109 110 111 112 113 114 115 116 117
static const uint8_t mpeg2_dc_scale_table3[128] = {
//  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
118 119
};

120
const uint8_t *const ff_mpeg2_dc_scale_table[4] = {
121 122 123 124 125 126
    ff_mpeg1_dc_scale_table,
    mpeg2_dc_scale_table1,
    mpeg2_dc_scale_table2,
    mpeg2_dc_scale_table3,
};

127 128 129
const enum AVPixelFormat ff_pixfmt_list_420[] = {
    AV_PIX_FMT_YUV420P,
    AV_PIX_FMT_NONE
130 131
};

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
                              int (*mv)[2][4][2],
                              int mb_x, int mb_y, int mb_intra, int mb_skipped)
{
    MpegEncContext *s = opaque;

    s->mv_dir     = mv_dir;
    s->mv_type    = mv_type;
    s->mb_intra   = mb_intra;
    s->mb_skipped = mb_skipped;
    s->mb_x       = mb_x;
    s->mb_y       = mb_y;
    memcpy(s->mv, mv, sizeof(*mv));

    ff_init_block_index(s);
    ff_update_block_index(s);

    s->dsp.clear_blocks(s->block[0]);

    s->dest[0] = s->current_picture.f.data[0] + (s->mb_y *  16                       * s->linesize)   + s->mb_x *  16;
    s->dest[1] = s->current_picture.f.data[1] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
    s->dest[2] = s->current_picture.f.data[2] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);

    assert(ref == 0);
    ff_MPV_decode_mb(s, s->block);
}

159
/* init common dct for both encoder and decoder */
160
av_cold int ff_dct_common_init(MpegEncContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
161
{
162
    ff_dsputil_init(&s->dsp, s->avctx);
163
    ff_h264chroma_init(&s->h264chroma, 8); //for lowres
164
    ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
165
    ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
166

167 168 169 170 171
    s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
    s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
    s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
    s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
    s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
172
    if (s->flags & CODEC_FLAG_BITEXACT)
173
        s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
174
    s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
175

176 177
#if ARCH_X86
    ff_MPV_common_init_x86(s);
178
#elif ARCH_ALPHA
179
    ff_MPV_common_init_axp(s);
180
#elif ARCH_ARM
181
    ff_MPV_common_init_arm(s);
182
#elif ARCH_BFIN
183
    ff_MPV_common_init_bfin(s);
184 185
#elif ARCH_PPC
    ff_MPV_common_init_ppc(s);
186
#endif
187

188
    /* load & permutate scantables
189 190 191
     * note: only wmv uses different ones
     */
    if (s->alternate_scan) {
192 193
        ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_alternate_vertical_scan);
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_alternate_vertical_scan);
194
    } else {
195 196 197
        ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_zigzag_direct);
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_zigzag_direct);
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
198 199
    ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
    ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
200

201 202 203
    return 0;
}

204 205
int ff_mpv_frame_size_alloc(MpegEncContext *s, int linesize)
{
206
    int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
207 208 209

    // edge emu needs blocksize + filter length - 1
    // (= 17x17 for  halfpel / 21x21 for  h264)
210 211
    // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
    // at uvlinesize. It supports only YUV420 so 24x24 is enough
212
    // linesize * interlaced * MBsize
213
    FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 4 * 24,
214 215
                      fail);

216
    FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 4 * 16 * 2,
217 218 219 220 221 222 223 224 225 226 227 228
                      fail)
    s->me.temp         = s->me.scratchpad;
    s->rd_scratchpad   = s->me.scratchpad;
    s->b_scratchpad    = s->me.scratchpad;
    s->obmc_scratchpad = s->me.scratchpad + 16;

    return 0;
fail:
    av_freep(&s->edge_emu_buffer);
    return AVERROR(ENOMEM);
}

229
/**
230
 * Allocate a frame buffer
231 232 233
 */
static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
{
234
    int r, ret;
235

236
    pic->tf.f = &pic->f;
Alberto Delmás's avatar
Alberto Delmás committed
237 238 239
    if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
        s->codec_id != AV_CODEC_ID_VC1IMAGE  &&
        s->codec_id != AV_CODEC_ID_MSS2)
240 241 242 243 244 245 246 247 248 249 250 251
        r = ff_thread_get_buffer(s->avctx, &pic->tf,
                                 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
    else {
        pic->f.width  = s->avctx->width;
        pic->f.height = s->avctx->height;
        pic->f.format = s->avctx->pix_fmt;
        r = avcodec_default_get_buffer2(s->avctx, &pic->f, 0);
    }

    if (r < 0 || !pic->f.data[0]) {
        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
               r, pic->f.data[0]);
252 253 254
        return -1;
    }

255 256 257 258 259 260 261 262 263 264 265 266
    if (s->avctx->hwaccel) {
        assert(!pic->hwaccel_picture_private);
        if (s->avctx->hwaccel->priv_data_size) {
            pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->priv_data_size);
            if (!pic->hwaccel_priv_buf) {
                av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
                return -1;
            }
            pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
        }
    }

267 268 269 270
    if (s->linesize && (s->linesize   != pic->f.linesize[0] ||
                        s->uvlinesize != pic->f.linesize[1])) {
        av_log(s->avctx, AV_LOG_ERROR,
               "get_buffer() failed (stride changed)\n");
271
        ff_mpeg_unref_picture(s, pic);
272 273 274
        return -1;
    }

275
    if (pic->f.linesize[1] != pic->f.linesize[2]) {
276 277
        av_log(s->avctx, AV_LOG_ERROR,
               "get_buffer() failed (uv stride mismatch)\n");
278
        ff_mpeg_unref_picture(s, pic);
279 280 281
        return -1;
    }

282 283 284 285
    if (!s->edge_emu_buffer &&
        (ret = ff_mpv_frame_size_alloc(s, pic->f.linesize[0])) < 0) {
        av_log(s->avctx, AV_LOG_ERROR,
               "get_buffer() failed to allocate context scratch buffers.\n");
286
        ff_mpeg_unref_picture(s, pic);
287 288 289
        return ret;
    }

290 291 292
    return 0;
}

293
static void free_picture_tables(Picture *pic)
294
{
295
    int i;
296

297 298 299
    pic->alloc_mb_width  =
    pic->alloc_mb_height = 0;

300 301 302 303 304 305
    av_buffer_unref(&pic->mb_var_buf);
    av_buffer_unref(&pic->mc_mb_var_buf);
    av_buffer_unref(&pic->mb_mean_buf);
    av_buffer_unref(&pic->mbskip_table_buf);
    av_buffer_unref(&pic->qscale_table_buf);
    av_buffer_unref(&pic->mb_type_buf);
306

307 308 309 310 311
    for (i = 0; i < 2; i++) {
        av_buffer_unref(&pic->motion_val_buf[i]);
        av_buffer_unref(&pic->ref_index_buf[i]);
    }
}
312

313 314 315
static int alloc_picture_tables(MpegEncContext *s, Picture *pic)
{
    const int big_mb_num    = s->mb_stride * (s->mb_height + 1) + 1;
316 317
    const int mb_array_size = s->mb_stride * s->mb_height;
    const int b8_array_size = s->b8_stride * s->mb_height * 2;
318
    int i;
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348


    pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
    pic->qscale_table_buf = av_buffer_allocz(big_mb_num + s->mb_stride);
    pic->mb_type_buf      = av_buffer_allocz((big_mb_num + s->mb_stride) *
                                             sizeof(uint32_t));
    if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
        return AVERROR(ENOMEM);

    if (s->encoding) {
        pic->mb_var_buf    = av_buffer_allocz(mb_array_size * sizeof(int16_t));
        pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
        pic->mb_mean_buf   = av_buffer_allocz(mb_array_size);
        if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
            return AVERROR(ENOMEM);
    }

    if (s->out_format == FMT_H263 || s->encoding ||
               (s->avctx->debug & FF_DEBUG_MV) || s->avctx->debug_mv) {
        int mv_size        = 2 * (b8_array_size + 4) * sizeof(int16_t);
        int ref_index_size = 4 * mb_array_size;

        for (i = 0; mv_size && i < 2; i++) {
            pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
            pic->ref_index_buf[i]  = av_buffer_allocz(ref_index_size);
            if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
                return AVERROR(ENOMEM);
        }
    }

349 350 351
    pic->alloc_mb_width  = s->mb_width;
    pic->alloc_mb_height = s->mb_height;

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
    return 0;
}

static int make_tables_writable(Picture *pic)
{
    int ret, i;
#define MAKE_WRITABLE(table) \
do {\
    if (pic->table &&\
       (ret = av_buffer_make_writable(&pic->table)) < 0)\
    return ret;\
} while (0)

    MAKE_WRITABLE(mb_var_buf);
    MAKE_WRITABLE(mc_mb_var_buf);
    MAKE_WRITABLE(mb_mean_buf);
    MAKE_WRITABLE(mbskip_table_buf);
    MAKE_WRITABLE(qscale_table_buf);
    MAKE_WRITABLE(mb_type_buf);

    for (i = 0; i < 2; i++) {
        MAKE_WRITABLE(motion_val_buf[i]);
        MAKE_WRITABLE(ref_index_buf[i]);
    }

    return 0;
}

/**
 * Allocate a Picture.
 * The pixels are allocated/set by calling get_buffer() if shared = 0
 */
int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
{
    int i, ret;
387

388
    if (pic->qscale_table_buf)
389 390
        if (   pic->alloc_mb_width  != s->mb_width
            || pic->alloc_mb_height != s->mb_height)
391 392
            free_picture_tables(pic);

393
    if (shared) {
394
        assert(pic->f.data[0]);
395
        pic->shared = 1;
396
    } else {
397
        assert(!pic->f.data[0]);
398

399
        if (alloc_frame_buffer(s, pic) < 0)
Michael Niedermayer's avatar
Michael Niedermayer committed
400 401
            return -1;

402 403
        s->linesize   = pic->f.linesize[0];
        s->uvlinesize = pic->f.linesize[1];
Michael Niedermayer's avatar
Michael Niedermayer committed
404
    }
405

406 407 408 409 410 411
    if (!pic->qscale_table_buf)
        ret = alloc_picture_tables(s, pic);
    else
        ret = make_tables_writable(pic);
    if (ret < 0)
        goto fail;
Michael Niedermayer's avatar
Michael Niedermayer committed
412

413 414 415 416
    if (s->encoding) {
        pic->mb_var    = (uint16_t*)pic->mb_var_buf->data;
        pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
        pic->mb_mean   = pic->mb_mean_buf->data;
Michael Niedermayer's avatar
Michael Niedermayer committed
417
    }
418

419 420 421 422 423 424 425 426 427 428
    pic->mbskip_table = pic->mbskip_table_buf->data;
    pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1;
    pic->mb_type      = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;

    if (pic->motion_val_buf[0]) {
        for (i = 0; i < 2; i++) {
            pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
            pic->ref_index[i]  = pic->ref_index_buf[i]->data;
        }
    }
429

Michael Niedermayer's avatar
Michael Niedermayer committed
430
    return 0;
431 432 433 434 435
fail:
    av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
    ff_mpeg_unref_picture(s, pic);
    free_picture_tables(pic);
    return AVERROR(ENOMEM);
Michael Niedermayer's avatar
Michael Niedermayer committed
436 437
}

Michael Niedermayer's avatar
Michael Niedermayer committed
438
/**
439
 * Deallocate a picture.
Michael Niedermayer's avatar
Michael Niedermayer committed
440
 */
441
void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic)
442
{
443
    int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
Michael Niedermayer's avatar
Michael Niedermayer committed
444

445 446 447 448 449 450 451 452 453 454 455
    pic->tf.f = &pic->f;
    /* WM Image / Screen codecs allocate internal buffers with different
     * dimensions / colorspaces; ignore user-defined callbacks for these. */
    if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
        s->codec_id != AV_CODEC_ID_VC1IMAGE  &&
        s->codec_id != AV_CODEC_ID_MSS2)
        ff_thread_release_buffer(s->avctx, &pic->tf);
    else
        av_frame_unref(&pic->f);

    av_buffer_unref(&pic->hwaccel_priv_buf);
Michael Niedermayer's avatar
Michael Niedermayer committed
456

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
    memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
}

static int update_picture_tables(Picture *dst, Picture *src)
{
     int i;

#define UPDATE_TABLE(table)\
do {\
    if (src->table &&\
        (!dst->table || dst->table->buffer != src->table->buffer)) {\
        av_buffer_unref(&dst->table);\
        dst->table = av_buffer_ref(src->table);\
        if (!dst->table) {\
            free_picture_tables(dst);\
            return AVERROR(ENOMEM);\
        }\
    }\
} while (0)

    UPDATE_TABLE(mb_var_buf);
    UPDATE_TABLE(mc_mb_var_buf);
    UPDATE_TABLE(mb_mean_buf);
    UPDATE_TABLE(mbskip_table_buf);
    UPDATE_TABLE(qscale_table_buf);
    UPDATE_TABLE(mb_type_buf);
483
    for (i = 0; i < 2; i++) {
484 485
        UPDATE_TABLE(motion_val_buf[i]);
        UPDATE_TABLE(ref_index_buf[i]);
486
    }
487

488 489 490 491 492 493 494 495 496
    dst->mb_var        = src->mb_var;
    dst->mc_mb_var     = src->mc_mb_var;
    dst->mb_mean       = src->mb_mean;
    dst->mbskip_table  = src->mbskip_table;
    dst->qscale_table  = src->qscale_table;
    dst->mb_type       = src->mb_type;
    for (i = 0; i < 2; i++) {
        dst->motion_val[i] = src->motion_val[i];
        dst->ref_index[i]  = src->ref_index[i];
Michael Niedermayer's avatar
Michael Niedermayer committed
497
    }
498

499 500 501
    dst->alloc_mb_width  = src->alloc_mb_width;
    dst->alloc_mb_height = src->alloc_mb_height;

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
    return 0;
}

int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src)
{
    int ret;

    av_assert0(!dst->f.buf[0]);
    av_assert0(src->f.buf[0]);

    src->tf.f = &src->f;
    dst->tf.f = &dst->f;
    ret = ff_thread_ref_frame(&dst->tf, &src->tf);
    if (ret < 0)
        goto fail;

    ret = update_picture_tables(dst, src);
    if (ret < 0)
        goto fail;

    if (src->hwaccel_picture_private) {
        dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
        if (!dst->hwaccel_priv_buf)
            goto fail;
        dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
    }

    dst->field_picture           = src->field_picture;
    dst->mb_var_sum              = src->mb_var_sum;
    dst->mc_mb_var_sum           = src->mc_mb_var_sum;
    dst->b_frame_score           = src->b_frame_score;
    dst->needs_realloc           = src->needs_realloc;
    dst->reference               = src->reference;
    dst->shared                  = src->shared;

    return 0;
fail:
    ff_mpeg_unref_picture(s, dst);
    return ret;
Michael Niedermayer's avatar
Michael Niedermayer committed
541 542
}

543
static int init_duplicate_context(MpegEncContext *s)
544
{
545 546 547
    int y_size = s->b8_stride * (2 * s->mb_height + 1);
    int c_size = s->mb_stride * (s->mb_height + 1);
    int yc_size = y_size + 2 * c_size;
548 549
    int i;

550 551 552 553 554 555
    s->edge_emu_buffer =
    s->me.scratchpad   =
    s->me.temp         =
    s->rd_scratchpad   =
    s->b_scratchpad    =
    s->obmc_scratchpad = NULL;
556

557
    if (s->encoding) {
558 559 560 561 562 563 564
        FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
                          ME_MAP_SIZE * sizeof(uint32_t), fail)
        FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
                          ME_MAP_SIZE * sizeof(uint32_t), fail)
        if (s->avctx->noise_reduction) {
            FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
                              2 * 64 * sizeof(int), fail)
565
        }
566
    }
Diego Biurrun's avatar
Diego Biurrun committed
567
    FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
568
    s->block = s->blocks[0];
569

570
    for (i = 0; i < 12; i++) {
571
        s->pblocks[i] = &s->block[i];
572
    }
573

574 575
    if (s->out_format == FMT_H263) {
        /* ac values */
576 577
        FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
                          yc_size * sizeof(int16_t) * 16, fail);
578 579 580 581 582
        s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
        s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
        s->ac_val[2] = s->ac_val[1] + c_size;
    }

583 584
    return 0;
fail:
585
    return -1; // free() through ff_MPV_common_end()
586 587
}

588 589 590 591
static void free_duplicate_context(MpegEncContext *s)
{
    if (s == NULL)
        return;
592

593
    av_freep(&s->edge_emu_buffer);
594
    av_freep(&s->me.scratchpad);
595 596 597 598
    s->me.temp =
    s->rd_scratchpad =
    s->b_scratchpad =
    s->obmc_scratchpad = NULL;
599

600 601 602 603
    av_freep(&s->dct_error_sum);
    av_freep(&s->me.map);
    av_freep(&s->me.score_map);
    av_freep(&s->blocks);
604
    av_freep(&s->ac_val_base);
605
    s->block = NULL;
606 607
}

608 609 610
static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
{
#define COPY(a) bak->a = src->a
611 612
    COPY(edge_emu_buffer);
    COPY(me.scratchpad);
613
    COPY(me.temp);
614 615 616 617 618 619 620 621 622 623 624 625
    COPY(rd_scratchpad);
    COPY(b_scratchpad);
    COPY(obmc_scratchpad);
    COPY(me.map);
    COPY(me.score_map);
    COPY(blocks);
    COPY(block);
    COPY(start_mb_y);
    COPY(end_mb_y);
    COPY(me.map_generation);
    COPY(pb);
    COPY(dct_error_sum);
626 627
    COPY(dct_count[0]);
    COPY(dct_count[1]);
628 629 630 631
    COPY(ac_val_base);
    COPY(ac_val[0]);
    COPY(ac_val[1]);
    COPY(ac_val[2]);
632 633 634
#undef COPY
}

635
int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
636
{
637
    MpegEncContext bak;
638
    int i, ret;
639 640
    // FIXME copy only needed parts
    // START_TIMER
641 642 643
    backup_duplicate_context(&bak, dst);
    memcpy(dst, src, sizeof(MpegEncContext));
    backup_duplicate_context(dst, &bak);
644
    for (i = 0; i < 12; i++) {
645
        dst->pblocks[i] = &dst->block[i];
646
    }
647 648 649 650 651 652
    if (!dst->edge_emu_buffer &&
        (ret = ff_mpv_frame_size_alloc(dst, dst->linesize)) < 0) {
        av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
               "scratch buffers.\n");
        return ret;
    }
653 654
    // STOP_TIMER("update_duplicate_context")
    // about 10k cycles / 0.01 sec for  1000frames on 1ghz with 2 threads
655
    return 0;
656 657
}

658 659
int ff_mpeg_update_thread_context(AVCodecContext *dst,
                                  const AVCodecContext *src)
660
{
661
    int i, ret;
662 663
    MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;

664
    if (dst == src)
665
        return 0;
666

667 668
    av_assert0(s != s1);

669 670 671
    // FIXME can parameters change on I-frames?
    // in that case dst may need a reinit
    if (!s->context_initialized) {
672 673 674 675 676 677
        memcpy(s, s1, sizeof(MpegEncContext));

        s->avctx                 = dst;
        s->bitstream_buffer      = NULL;
        s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;

678
        if (s1->context_initialized){
679 680 681
//             s->picture_range_start  += MAX_PICTURE_COUNT;
//             s->picture_range_end    += MAX_PICTURE_COUNT;
            if((ret = ff_MPV_common_init(s)) < 0){
682 683
                memset(s, 0, sizeof(MpegEncContext));
                s->avctx = dst;
684
                return ret;
685
            }
686
        }
687 688
    }

689 690 691 692
    if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
        s->context_reinit = 0;
        s->height = s1->height;
        s->width  = s1->width;
693 694
        if ((ret = ff_MPV_common_frame_size_change(s)) < 0)
            return ret;
695 696
    }

697 698 699 700 701 702 703 704 705
    s->avctx->coded_height  = s1->avctx->coded_height;
    s->avctx->coded_width   = s1->avctx->coded_width;
    s->avctx->width         = s1->avctx->width;
    s->avctx->height        = s1->avctx->height;

    s->coded_picture_number = s1->coded_picture_number;
    s->picture_number       = s1->picture_number;
    s->input_picture_number = s1->input_picture_number;

706
    av_assert0(!s->picture || s->picture != s1->picture);
707
    if(s->picture)
708 709 710 711 712
    for (i = 0; i < MAX_PICTURE_COUNT; i++) {
        ff_mpeg_unref_picture(s, &s->picture[i]);
        if (s1->picture[i].f.data[0] &&
            (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0)
            return ret;
713
    }
714

715 716 717 718 719 720 721 722 723 724 725 726 727 728
#define UPDATE_PICTURE(pic)\
do {\
    ff_mpeg_unref_picture(s, &s->pic);\
    if (s1->pic.f.data[0])\
        ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\
    else\
        ret = update_picture_tables(&s->pic, &s1->pic);\
    if (ret < 0)\
        return ret;\
} while (0)

    UPDATE_PICTURE(current_picture);
    UPDATE_PICTURE(last_picture);
    UPDATE_PICTURE(next_picture);
729

730 731 732
    s->last_picture_ptr    = REBASE_PICTURE(s1->last_picture_ptr,    s, s1);
    s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
    s->next_picture_ptr    = REBASE_PICTURE(s1->next_picture_ptr,    s, s1);
733

734
    // Error/bug resilience
735 736
    s->next_p_frame_damaged = s1->next_p_frame_damaged;
    s->workaround_bugs      = s1->workaround_bugs;
737
    s->padding_bug_score    = s1->padding_bug_score;
738

739 740 741 742 743 744 745
    // MPEG4 timing info
    memcpy(&s->time_increment_bits, &s1->time_increment_bits,
           (char *) &s1->shape - (char *) &s1->time_increment_bits);

    // B-frame info
    s->max_b_frames = s1->max_b_frames;
    s->low_delay    = s1->low_delay;
746
    s->droppable    = s1->droppable;
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761

    // DivX handling (doesn't work)
    s->divx_packed  = s1->divx_packed;

    if (s1->bitstream_buffer) {
        if (s1->bitstream_buffer_size +
            FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
            av_fast_malloc(&s->bitstream_buffer,
                           &s->allocated_bitstream_buffer_size,
                           s1->allocated_bitstream_buffer_size);
            s->bitstream_buffer_size = s1->bitstream_buffer_size;
        memcpy(s->bitstream_buffer, s1->bitstream_buffer,
               s1->bitstream_buffer_size);
        memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
               FF_INPUT_BUFFER_PADDING_SIZE);
762 763
    }

764 765 766 767 768 769 770 771 772 773 774 775 776
    // linesize dependend scratch buffer allocation
    if (!s->edge_emu_buffer)
        if (s1->linesize) {
            if (ff_mpv_frame_size_alloc(s, s1->linesize) < 0) {
                av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
                       "scratch buffers.\n");
                return AVERROR(ENOMEM);
            }
        } else {
            av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
                   "be allocated due to unknown size.\n");
        }

777 778 779
    // MPEG2/interlacing info
    memcpy(&s->progressive_sequence, &s1->progressive_sequence,
           (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
780

781 782 783 784
    if (!s1->first_field) {
        s->last_pict_type = s1->pict_type;
        if (s1->current_picture_ptr)
            s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f.quality;
785

786
        if (s1->pict_type != AV_PICTURE_TYPE_B) {
787
            s->last_non_b_pict_type = s1->pict_type;
788 789 790 791 792 793
        }
    }

    return 0;
}

Michael Niedermayer's avatar
Michael Niedermayer committed
794
/**
795
 * Set the given MpegEncContext to common defaults
796
 * (same for encoding and decoding).
797
 * The changed fields will not depend upon the
798
 * prior state of the MpegEncContext.
Michael Niedermayer's avatar
Michael Niedermayer committed
799
 */
800
void ff_MPV_common_defaults(MpegEncContext *s)
801 802 803 804 805 806 807 808 809 810 811
{
    s->y_dc_scale_table      =
    s->c_dc_scale_table      = ff_mpeg1_dc_scale_table;
    s->chroma_qscale_table   = ff_default_chroma_qscale_table;
    s->progressive_frame     = 1;
    s->progressive_sequence  = 1;
    s->picture_structure     = PICT_FRAME;

    s->coded_picture_number  = 0;
    s->picture_number        = 0;
    s->input_picture_number  = 0;
Michael Niedermayer's avatar
Michael Niedermayer committed
812 813

    s->picture_in_gop_number = 0;
814

815 816
    s->f_code                = 1;
    s->b_code                = 1;
817

818
    s->slice_context_count   = 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
819 820 821
}

/**
822
 * Set the given MpegEncContext to defaults for decoding.
823 824
 * the changed fields will not depend upon
 * the prior state of the MpegEncContext.
Michael Niedermayer's avatar
Michael Niedermayer committed
825
 */
826
void ff_MPV_decode_defaults(MpegEncContext *s)
827
{
828
    ff_MPV_common_defaults(s);
Michael Niedermayer's avatar
Michael Niedermayer committed
829 830
}

831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
static int init_er(MpegEncContext *s)
{
    ERContext *er = &s->er;
    int mb_array_size = s->mb_height * s->mb_stride;
    int i;

    er->avctx       = s->avctx;
    er->dsp         = &s->dsp;

    er->mb_index2xy = s->mb_index2xy;
    er->mb_num      = s->mb_num;
    er->mb_width    = s->mb_width;
    er->mb_height   = s->mb_height;
    er->mb_stride   = s->mb_stride;
    er->b8_stride   = s->b8_stride;

    er->er_temp_buffer     = av_malloc(s->mb_height * s->mb_stride);
    er->error_status_table = av_mallocz(mb_array_size);
    if (!er->er_temp_buffer || !er->error_status_table)
        goto fail;

    er->mbskip_table  = s->mbskip_table;
    er->mbintra_table = s->mbintra_table;

    for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
        er->dc_val[i] = s->dc_val[i];

    er->decode_mb = mpeg_er_decode_mb;
    er->opaque    = s;

    return 0;
fail:
    av_freep(&er->er_temp_buffer);
    av_freep(&er->error_status_table);
    return AVERROR(ENOMEM);
}

868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
/**
 * Initialize and allocates MpegEncContext fields dependent on the resolution.
 */
static int init_context_frame(MpegEncContext *s)
{
    int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;

    s->mb_width   = (s->width + 15) / 16;
    s->mb_stride  = s->mb_width + 1;
    s->b8_stride  = s->mb_width * 2 + 1;
    s->b4_stride  = s->mb_width * 4 + 1;
    mb_array_size = s->mb_height * s->mb_stride;
    mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;

    /* set default edge pos, will be overriden
     * in decode_header if needed */
    s->h_edge_pos = s->mb_width * 16;
    s->v_edge_pos = s->mb_height * 16;

    s->mb_num     = s->mb_width * s->mb_height;

    s->block_wrap[0] =
    s->block_wrap[1] =
    s->block_wrap[2] =
    s->block_wrap[3] = s->b8_stride;
    s->block_wrap[4] =
    s->block_wrap[5] = s->mb_stride;

    y_size  = s->b8_stride * (2 * s->mb_height + 1);
    c_size  = s->mb_stride * (s->mb_height + 1);
    yc_size = y_size + 2   * c_size;

900
    FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), fail); // error ressilience code looks cleaner with this
901 902 903 904
    for (y = 0; y < s->mb_height; y++)
        for (x = 0; x < s->mb_width; x++)
            s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;

905
    s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
906 907 908

    if (s->encoding) {
        /* Allocate MV tables */
909 910 911 912 913 914
        FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,                 mv_table_size * 2 * sizeof(int16_t), fail)
        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,            mv_table_size * 2 * sizeof(int16_t), fail)
        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,            mv_table_size * 2 * sizeof(int16_t), fail)
        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,      mv_table_size * 2 * sizeof(int16_t), fail)
        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,      mv_table_size * 2 * sizeof(int16_t), fail)
        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,          mv_table_size * 2 * sizeof(int16_t), fail)
915 916 917
        s->p_mv_table            = s->p_mv_table_base + s->mb_stride + 1;
        s->b_forw_mv_table       = s->b_forw_mv_table_base + s->mb_stride + 1;
        s->b_back_mv_table       = s->b_back_mv_table_base + s->mb_stride + 1;
918 919
        s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
        s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1;
920 921 922
        s->b_direct_mv_table     = s->b_direct_mv_table_base + s->mb_stride + 1;

        /* Allocate MB type table */
923
        FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * sizeof(uint16_t), fail) // needed for encoding
924

925
        FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947

        FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
                         mb_array_size * sizeof(float), fail);
        FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
                         mb_array_size * sizeof(float), fail);

    }

    if (s->codec_id == AV_CODEC_ID_MPEG4 ||
        (s->flags & CODEC_FLAG_INTERLACED_ME)) {
        /* interlaced direct mode decoding tables */
        for (i = 0; i < 2; i++) {
            int j, k;
            for (j = 0; j < 2; j++) {
                for (k = 0; k < 2; k++) {
                    FF_ALLOCZ_OR_GOTO(s->avctx,
                                      s->b_field_mv_table_base[i][j][k],
                                      mv_table_size * 2 * sizeof(int16_t),
                                      fail);
                    s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
                                                   s->mb_stride + 1;
                }
948 949 950
                FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
                FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
                s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
951
            }
952
            FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
953 954 955 956 957 958 959 960
        }
    }
    if (s->out_format == FMT_H263) {
        /* cbp values */
        FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
        s->coded_block = s->coded_block_base + s->b8_stride + 1;

        /* cbp, ac_pred, pred_dir */
961 962
        FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table     , mb_array_size * sizeof(uint8_t), fail);
        FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail);
963 964 965 966 967
    }

    if (s->h263_pred || s->h263_plus || !s->encoding) {
        /* dc values */
        // MN: we need these for  error resilience of intra-frames
968
        FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
        s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
        s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
        s->dc_val[2] = s->dc_val[1] + c_size;
        for (i = 0; i < yc_size; i++)
            s->dc_val_base[i] = 1024;
    }

    /* which mb is a intra block */
    FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
    memset(s->mbintra_table, 1, mb_array_size);

    /* init macroblock skip table */
    FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
    // Note the + 1 is for  a quicker mpeg4 slice_end detection

984
    return init_er(s);
985 986 987 988
fail:
    return AVERROR(ENOMEM);
}

989
/**
Michael Niedermayer's avatar
Michael Niedermayer committed
990 991 992
 * init common structure for both encoder and decoder.
 * this assumes that some variables like width/height are already set
 */
993
av_cold int ff_MPV_common_init(MpegEncContext *s)
994
{
995
    int i;
996 997 998 999 1000 1001
    int nb_slices = (HAVE_THREADS &&
                     s->avctx->active_thread_type & FF_THREAD_SLICE) ?
                    s->avctx->thread_count : 1;

    if (s->encoding && s->avctx->slices)
        nb_slices = s->avctx->slices;
1002

1003
    if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
1004
        s->mb_height = (s->height + 31) / 32 * 2;
1005
    else
Baptiste Coudurier's avatar
Baptiste Coudurier committed
1006
        s->mb_height = (s->height + 15) / 16;
1007

1008
    if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
1009
        av_log(s->avctx, AV_LOG_ERROR,
1010
               "decoding to AV_PIX_FMT_NONE is not supported.\n");
1011 1012 1013
        return -1;
    }

1014 1015 1016 1017 1018 1019 1020 1021 1022
    if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
        int max_slices;
        if (s->mb_height)
            max_slices = FFMIN(MAX_THREADS, s->mb_height);
        else
            max_slices = MAX_THREADS;
        av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
               " reducing to %d\n", nb_slices, max_slices);
        nb_slices = max_slices;
Michael Niedermayer's avatar
Michael Niedermayer committed
1023 1024
    }

1025 1026
    if ((s->width || s->height) &&
        av_image_check_size(s->width, s->height, 0, s->avctx))
1027 1028
        return -1;

1029
    ff_dct_common_init(s);
1030

1031 1032
    s->flags  = s->avctx->flags;
    s->flags2 = s->avctx->flags2;
1033

1034 1035
    /* set chroma shifts */
    avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
1036

1037
    /* convert fourcc to upper case */
1038 1039
    s->codec_tag        = avpriv_toupper4(s->avctx->codec_tag);
    s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
1040

1041
    s->avctx->coded_frame = &s->current_picture.f;
Michael Niedermayer's avatar
Michael Niedermayer committed
1042

1043
    if (s->encoding) {
1044 1045 1046 1047
        if (s->msmpeg4_version) {
            FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
                                2 * 2 * (MAX_LEVEL + 1) *
                                (MAX_RUN + 1) * 2 * sizeof(int), fail);
1048
        }
1049
        FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
1050

1051 1052 1053 1054 1055 1056 1057 1058
        FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix,          64 * 32   * sizeof(int), fail)
        FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix,   64 * 32   * sizeof(int), fail)
        FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix,          64 * 32   * sizeof(int), fail)
        FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16,        64 * 32 * 2 * sizeof(uint16_t), fail)
        FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail)
        FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16,        64 * 32 * 2 * sizeof(uint16_t), fail)
        FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,           MAX_PICTURE_COUNT * sizeof(Picture *), fail)
        FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture *), fail)
1059

1060 1061
        if (s->avctx->noise_reduction) {
            FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset, 2 * 64 * sizeof(uint16_t), fail);
1062
        }
1063
    }
1064

1065
    FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
1066 1067
                      MAX_PICTURE_COUNT * sizeof(Picture), fail);
    for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1068
        avcodec_get_frame_defaults(&s->picture[i].f);
1069
    }
1070 1071 1072 1073 1074 1075
    memset(&s->next_picture, 0, sizeof(s->next_picture));
    memset(&s->last_picture, 0, sizeof(s->last_picture));
    memset(&s->current_picture, 0, sizeof(s->current_picture));
    avcodec_get_frame_defaults(&s->next_picture.f);
    avcodec_get_frame_defaults(&s->last_picture.f);
    avcodec_get_frame_defaults(&s->current_picture.f);
1076

1077
        if (init_context_frame(s))
1078
            goto fail;
1079

1080
        s->parse_context.state = -1;
1081

1082 1083
        s->context_initialized = 1;
        s->thread_context[0]   = s;
1084

1085
//     if (s->width && s->height) {
1086 1087
        if (nb_slices > 1) {
            for (i = 1; i < nb_slices; i++) {
1088 1089 1090
                s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
                memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
            }
1091

1092
            for (i = 0; i < nb_slices; i++) {
1093
                if (init_duplicate_context(s->thread_context[i]) < 0)
1094 1095
                    goto fail;
                    s->thread_context[i]->start_mb_y =
1096
                        (s->mb_height * (i) + nb_slices / 2) / nb_slices;
1097
                    s->thread_context[i]->end_mb_y   =
1098
                        (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1099 1100
            }
        } else {
1101
            if (init_duplicate_context(s) < 0)
1102
                goto fail;
1103 1104
            s->start_mb_y = 0;
            s->end_mb_y   = s->mb_height;
1105
        }
1106
        s->slice_context_count = nb_slices;
1107
//     }
1108

Fabrice Bellard's avatar
Fabrice Bellard committed
1109 1110
    return 0;
 fail:
1111
    ff_MPV_common_end(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
1112 1113 1114
    return -1;
}

1115 1116 1117 1118 1119 1120
/**
 * Frees and resets MpegEncContext fields depending on the resolution.
 * Is used during resolution changes to avoid a full reinitialization of the
 * codec.
 */
static int free_context_frame(MpegEncContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
1121
{
1122
    int i, j, k;
Fabrice Bellard's avatar
Fabrice Bellard committed
1123

1124
    av_freep(&s->mb_type);
1125 1126 1127 1128 1129 1130
    av_freep(&s->p_mv_table_base);
    av_freep(&s->b_forw_mv_table_base);
    av_freep(&s->b_back_mv_table_base);
    av_freep(&s->b_bidir_forw_mv_table_base);
    av_freep(&s->b_bidir_back_mv_table_base);
    av_freep(&s->b_direct_mv_table_base);
1131 1132 1133 1134 1135 1136 1137 1138 1139
    s->p_mv_table            = NULL;
    s->b_forw_mv_table       = NULL;
    s->b_back_mv_table       = NULL;
    s->b_bidir_forw_mv_table = NULL;
    s->b_bidir_back_mv_table = NULL;
    s->b_direct_mv_table     = NULL;
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            for (k = 0; k < 2; k++) {
1140
                av_freep(&s->b_field_mv_table_base[i][j][k]);
1141
                s->b_field_mv_table[i][j][k] = NULL;
1142 1143 1144
            }
            av_freep(&s->b_field_select_table[i][j]);
            av_freep(&s->p_field_mv_table_base[i][j]);
1145
            s->p_field_mv_table[i][j] = NULL;
1146 1147 1148
        }
        av_freep(&s->p_field_select_table[i]);
    }
1149

1150 1151
    av_freep(&s->dc_val_base);
    av_freep(&s->coded_block_base);
1152
    av_freep(&s->mbintra_table);
1153 1154
    av_freep(&s->cbp_table);
    av_freep(&s->pred_dir_table);
1155

1156
    av_freep(&s->mbskip_table);
1157

1158 1159
    av_freep(&s->er.error_status_table);
    av_freep(&s->er.er_temp_buffer);
1160
    av_freep(&s->mb_index2xy);
1161
    av_freep(&s->lambda_table);
1162

1163 1164 1165 1166 1167 1168 1169 1170
    av_freep(&s->cplx_tab);
    av_freep(&s->bits_tab);

    s->linesize = s->uvlinesize = 0;

    return 0;
}

1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
int ff_MPV_common_frame_size_change(MpegEncContext *s)
{
    int i, err = 0;

    if (s->slice_context_count > 1) {
        for (i = 0; i < s->slice_context_count; i++) {
            free_duplicate_context(s->thread_context[i]);
        }
        for (i = 1; i < s->slice_context_count; i++) {
            av_freep(&s->thread_context[i]);
        }
    } else
        free_duplicate_context(s);

1185 1186
    if ((err = free_context_frame(s)) < 0)
        return err;
1187 1188

    if (s->picture)
1189
        for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
                s->picture[i].needs_realloc = 1;
        }

    s->last_picture_ptr         =
    s->next_picture_ptr         =
    s->current_picture_ptr      = NULL;

    // init
    if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
        s->mb_height = (s->height + 31) / 32 * 2;
1200
    else
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
        s->mb_height = (s->height + 15) / 16;

    if ((s->width || s->height) &&
        av_image_check_size(s->width, s->height, 0, s->avctx))
        return AVERROR_INVALIDDATA;

    if ((err = init_context_frame(s)))
        goto fail;

    s->thread_context[0]   = s;

    if (s->width && s->height) {
        int nb_slices = s->slice_context_count;
        if (nb_slices > 1) {
            for (i = 1; i < nb_slices; i++) {
                s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
                memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
            }

            for (i = 0; i < nb_slices; i++) {
1221
                if (init_duplicate_context(s->thread_context[i]) < 0)
1222 1223 1224 1225 1226 1227 1228
                    goto fail;
                    s->thread_context[i]->start_mb_y =
                        (s->mb_height * (i) + nb_slices / 2) / nb_slices;
                    s->thread_context[i]->end_mb_y   =
                        (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
            }
        } else {
1229
            if (init_duplicate_context(s) < 0)
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
                goto fail;
            s->start_mb_y = 0;
            s->end_mb_y   = s->mb_height;
        }
        s->slice_context_count = nb_slices;
    }

    return 0;
 fail:
    ff_MPV_common_end(s);
    return err;
}

1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
/* init common structure for both encoder and decoder */
void ff_MPV_common_end(MpegEncContext *s)
{
    int i;

    if (s->slice_context_count > 1) {
        for (i = 0; i < s->slice_context_count; i++) {
            free_duplicate_context(s->thread_context[i]);
        }
        for (i = 1; i < s->slice_context_count; i++) {
            av_freep(&s->thread_context[i]);
        }
        s->slice_context_count = 1;
    } else free_duplicate_context(s);

    av_freep(&s->parse_context.buffer);
    s->parse_context.buffer_size = 0;

    av_freep(&s->bitstream_buffer);
    s->allocated_bitstream_buffer_size = 0;

    av_freep(&s->avctx->stats_out);
    av_freep(&s->ac_stats);

1267 1268 1269 1270
    if(s->q_chroma_intra_matrix   != s->q_intra_matrix  ) av_freep(&s->q_chroma_intra_matrix);
    if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
    s->q_chroma_intra_matrix=   NULL;
    s->q_chroma_intra_matrix16= NULL;
1271 1272
    av_freep(&s->q_intra_matrix);
    av_freep(&s->q_inter_matrix);
1273 1274
    av_freep(&s->q_intra_matrix16);
    av_freep(&s->q_inter_matrix16);
1275 1276
    av_freep(&s->input_picture);
    av_freep(&s->reordered_input_picture);
1277
    av_freep(&s->dct_offset);
Michael Niedermayer's avatar
Michael Niedermayer committed
1278

1279 1280 1281 1282
    if (s->picture) {
        for (i = 0; i < MAX_PICTURE_COUNT; i++) {
            free_picture_tables(&s->picture[i]);
            ff_mpeg_unref_picture(s, &s->picture[i]);
Michael Niedermayer's avatar
Michael Niedermayer committed
1283
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
1284
    }
1285
    av_freep(&s->picture);
1286 1287 1288 1289 1290 1291 1292 1293
    free_picture_tables(&s->last_picture);
    ff_mpeg_unref_picture(s, &s->last_picture);
    free_picture_tables(&s->current_picture);
    ff_mpeg_unref_picture(s, &s->current_picture);
    free_picture_tables(&s->next_picture);
    ff_mpeg_unref_picture(s, &s->next_picture);
    free_picture_tables(&s->new_picture);
    ff_mpeg_unref_picture(s, &s->new_picture);
1294 1295 1296

    free_context_frame(s);

1297 1298 1299 1300 1301
    s->context_initialized      = 0;
    s->last_picture_ptr         =
    s->next_picture_ptr         =
    s->current_picture_ptr      = NULL;
    s->linesize = s->uvlinesize = 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
1302 1303
}

1304 1305
void ff_init_rl(RLTable *rl,
                uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
1306
{
1307 1308
    int8_t  max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
    uint8_t index_run[MAX_RUN + 1];
1309 1310
    int last, run, level, start, end, i;

1311
    /* If table is static, we can quit if rl->max_level[0] is not NULL */
1312
    if (static_store && rl->max_level[0])
1313 1314
        return;

1315
    /* compute max_level[], max_run[] and index_run[] */
1316
    for (last = 0; last < 2; last++) {
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
        if (last == 0) {
            start = 0;
            end = rl->last;
        } else {
            start = rl->last;
            end = rl->n;
        }

        memset(max_level, 0, MAX_RUN + 1);
        memset(max_run, 0, MAX_LEVEL + 1);
        memset(index_run, rl->n, MAX_RUN + 1);
1328 1329
        for (i = start; i < end; i++) {
            run   = rl->table_run[i];
1330 1331 1332 1333 1334 1335 1336 1337
            level = rl->table_level[i];
            if (index_run[run] == rl->n)
                index_run[run] = i;
            if (level > max_level[run])
                max_level[run] = level;
            if (run > max_run[level])
                max_run[level] = run;
        }
1338
        if (static_store)
1339
            rl->max_level[last] = static_store[last];
1340 1341
        else
            rl->max_level[last] = av_malloc(MAX_RUN + 1);
1342
        memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
1343 1344
        if (static_store)
            rl->max_run[last]   = static_store[last] + MAX_RUN + 1;
1345
        else
1346
            rl->max_run[last]   = av_malloc(MAX_LEVEL + 1);
1347
        memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
1348
        if (static_store)
1349
            rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
1350 1351
        else
            rl->index_run[last] = av_malloc(MAX_RUN + 1);
1352 1353 1354 1355
        memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
    }
}

1356
void ff_init_vlc_rl(RLTable *rl)
1357 1358 1359
{
    int i, q;

1360 1361 1362
    for (q = 0; q < 32; q++) {
        int qmul = q * 2;
        int qadd = (q - 1) | 1;
1363

1364 1365 1366
        if (q == 0) {
            qmul = 1;
            qadd = 0;
1367
        }
1368 1369 1370
        for (i = 0; i < rl->vlc.table_size; i++) {
            int code = rl->vlc.table[i][0];
            int len  = rl->vlc.table[i][1];
1371 1372
            int level, run;

1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
            if (len == 0) { // illegal code
                run   = 66;
                level = MAX_LEVEL;
            } else if (len < 0) { // more bits needed
                run   = 0;
                level = code;
            } else {
                if (code == rl->n) { // esc
                    run   = 66;
                    level =  0;
                } else {
                    run   = rl->table_run[code] + 1;
                    level = rl->table_level[code] * qmul + qadd;
                    if (code >= rl->last) run += 192;
1387 1388
                }
            }
1389 1390 1391
            rl->rl_vlc[q][i].len   = len;
            rl->rl_vlc[q][i].level = level;
            rl->rl_vlc[q][i].run   = run;
1392 1393 1394 1395
        }
    }
}

1396
void ff_release_unused_pictures(MpegEncContext*s, int remove_current)
1397 1398 1399 1400
{
    int i;

    /* release non reference frames */
1401 1402 1403 1404
    for (i = 0; i < MAX_PICTURE_COUNT; i++) {
        if (!s->picture[i].reference &&
            (remove_current || &s->picture[i] !=  s->current_picture_ptr)) {
            ff_mpeg_unref_picture(s, &s->picture[i]);
1405 1406 1407 1408
        }
    }
}

1409 1410
static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
{
1411 1412
    if (pic == s->last_picture_ptr)
        return 0;
1413 1414
    if (pic->f.data[0] == NULL)
        return 1;
1415 1416
    if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
        return 1;
1417 1418 1419 1420
    return 0;
}

static int find_unused_picture(MpegEncContext *s, int shared)
1421
{
Michael Niedermayer's avatar
Michael Niedermayer committed
1422
    int i;
1423

1424
    if (shared) {
1425
        for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1426
            if (s->picture[i].f.data[0] == NULL && &s->picture[i] != s->last_picture_ptr)
1427
                return i;
Michael Niedermayer's avatar
Michael Niedermayer committed
1428
        }
1429
    } else {
1430
        for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1431
            if (pic_is_unused(s, &s->picture[i]))
1432
                return i;
Michael Niedermayer's avatar
Michael Niedermayer committed
1433 1434 1435
        }
    }

1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
    av_log(s->avctx, AV_LOG_FATAL,
           "Internal error, picture buffer overflow\n");
    /* We could return -1, but the codec would crash trying to draw into a
     * non-existing frame anyway. This is safer than waiting for a random crash.
     * Also the return of this is never useful, an encoder must only allocate
     * as much as allowed in the specification. This has no relationship to how
     * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
     * enough for such valid streams).
     * Plus, a decoder has to check stream validity and remove frames if too
     * many reference frames are around. Waiting for "OOM" is not correct at
     * all. Similarly, missing reference frames have to be replaced by
     * interpolated/MC frames, anything else is a bug in the codec ...
     */
    abort();
    return -1;
Michael Niedermayer's avatar
Michael Niedermayer committed
1451 1452
}

1453 1454 1455 1456
int ff_find_unused_picture(MpegEncContext *s, int shared)
{
    int ret = find_unused_picture(s, shared);

1457
    if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
1458 1459
        if (s->picture[ret].needs_realloc) {
            s->picture[ret].needs_realloc = 0;
1460 1461
            free_picture_tables(&s->picture[ret]);
            ff_mpeg_unref_picture(s, &s->picture[ret]);
1462 1463 1464 1465 1466 1467
            avcodec_get_frame_defaults(&s->picture[ret].f);
        }
    }
    return ret;
}

1468 1469
static void update_noise_reduction(MpegEncContext *s)
{
1470 1471
    int intra, i;

1472 1473 1474 1475
    for (intra = 0; intra < 2; intra++) {
        if (s->dct_count[intra] > (1 << 16)) {
            for (i = 0; i < 64; i++) {
                s->dct_error_sum[intra][i] >>= 1;
1476 1477 1478
            }
            s->dct_count[intra] >>= 1;
        }
1479

1480 1481 1482 1483 1484
        for (i = 0; i < 64; i++) {
            s->dct_offset[intra][i] = (s->avctx->noise_reduction *
                                       s->dct_count[intra] +
                                       s->dct_error_sum[intra][i] / 2) /
                                      (s->dct_error_sum[intra][i] + 1);
1485 1486 1487 1488
        }
    }
}

1489
/**
1490 1491
 * generic function for encode/decode called after coding/decoding
 * the header and before a frame is coded/decoded.
1492
 */
1493
int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
Fabrice Bellard's avatar
Fabrice Bellard committed
1494
{
1495
    int i, ret;
1496
    Picture *pic;
1497
    s->mb_skipped = 0;
1498

1499 1500 1501 1502 1503
    if (!ff_thread_can_start_frame(avctx)) {
        av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
        return -1;
    }

1504
    /* mark & release old frames */
Anton Khirnov's avatar
Anton Khirnov committed
1505 1506 1507 1508 1509
    if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
        s->last_picture_ptr != s->next_picture_ptr &&
        s->last_picture_ptr->f.data[0]) {
        ff_mpeg_unref_picture(s, s->last_picture_ptr);
    }
1510

Anton Khirnov's avatar
Anton Khirnov committed
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
    /* release forgotten pictures */
    /* if (mpeg124/h263) */
    if (!s->encoding) {
        for (i = 0; i < MAX_PICTURE_COUNT; i++) {
            if (&s->picture[i] != s->last_picture_ptr &&
                &s->picture[i] != s->next_picture_ptr &&
                s->picture[i].reference && !s->picture[i].needs_realloc) {
                if (!(avctx->active_thread_type & FF_THREAD_FRAME))
                    av_log(avctx, AV_LOG_ERROR,
                           "releasing zombie picture\n");
                ff_mpeg_unref_picture(s, &s->picture[i]);
Michael Niedermayer's avatar
Michael Niedermayer committed
1522
            }
1523
        }
Anton Khirnov's avatar
Anton Khirnov committed
1524
    }
1525

1526
    if (!s->encoding) {
1527
        ff_release_unused_pictures(s, 1);
1528

1529 1530 1531 1532 1533 1534 1535
        if (s->current_picture_ptr &&
            s->current_picture_ptr->f.data[0] == NULL) {
            // we already have a unused image
            // (maybe it was set before reading the header)
            pic = s->current_picture_ptr;
        } else {
            i   = ff_find_unused_picture(s, 0);
1536 1537
            if (i < 0) {
                av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1538
                return i;
1539
            }
1540
            pic = &s->picture[i];
1541 1542
        }

1543
        pic->reference = 0;
1544
        if (!s->droppable) {
1545
            if (s->pict_type != AV_PICTURE_TYPE_B)
1546
                pic->reference = 3;
1547
        }
1548

1549
        pic->f.coded_picture_number = s->coded_picture_number++;
1550

1551
        if (ff_alloc_picture(s, pic, 0) < 0)
1552
            return -1;
1553

1554 1555
        s->current_picture_ptr = pic;
        // FIXME use only the vars from current_pic
1556
        s->current_picture_ptr->f.top_field_first = s->top_field_first;
1557 1558
        if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
            s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1559 1560 1561
            if (s->picture_structure != PICT_FRAME)
                s->current_picture_ptr->f.top_field_first =
                    (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
1562
        }
1563 1564 1565
        s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame &&
                                                     !s->progressive_sequence;
        s->current_picture_ptr->field_picture      =  s->picture_structure != PICT_FRAME;
Michael Niedermayer's avatar
Michael Niedermayer committed
1566
    }
1567

1568
    s->current_picture_ptr->f.pict_type = s->pict_type;
1569 1570
    // if (s->flags && CODEC_FLAG_QSCALE)
    //     s->current_picture_ptr->quality = s->new_picture_ptr->quality;
1571
    s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1572

1573 1574 1575 1576
    ff_mpeg_unref_picture(s, &s->current_picture);
    if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
                                   s->current_picture_ptr)) < 0)
        return ret;
1577

1578
    if (s->pict_type != AV_PICTURE_TYPE_B) {
1579
        s->last_picture_ptr = s->next_picture_ptr;
1580
        if (!s->droppable)
1581
            s->next_picture_ptr = s->current_picture_ptr;
Fabrice Bellard's avatar
Fabrice Bellard committed
1582
    }
1583 1584 1585 1586 1587
    av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
            s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
            s->last_picture_ptr    ? s->last_picture_ptr->f.data[0]    : NULL,
            s->next_picture_ptr    ? s->next_picture_ptr->f.data[0]    : NULL,
            s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
1588
            s->pict_type, s->droppable);
1589

Anton Khirnov's avatar
Anton Khirnov committed
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
    if ((s->last_picture_ptr == NULL ||
         s->last_picture_ptr->f.data[0] == NULL) &&
        (s->pict_type != AV_PICTURE_TYPE_I ||
         s->picture_structure != PICT_FRAME)) {
        int h_chroma_shift, v_chroma_shift;
        av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
                                         &h_chroma_shift, &v_chroma_shift);
        if (s->pict_type != AV_PICTURE_TYPE_I)
            av_log(avctx, AV_LOG_ERROR,
                   "warning: first frame is no keyframe\n");
        else if (s->picture_structure != PICT_FRAME)
            av_log(avctx, AV_LOG_INFO,
                   "allocate dummy last picture for field based first keyframe\n");

        /* Allocate a dummy frame */
        i = ff_find_unused_picture(s, 0);
        if (i < 0) {
            av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
            return i;
1609
        }
Anton Khirnov's avatar
Anton Khirnov committed
1610
        s->last_picture_ptr = &s->picture[i];
1611
        s->last_picture_ptr->f.key_frame = 0;
Anton Khirnov's avatar
Anton Khirnov committed
1612 1613 1614
        if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
            s->last_picture_ptr = NULL;
            return -1;
1615
        }
1616

1617
        memset(s->last_picture_ptr->f.data[0], 0x80,
Anton Khirnov's avatar
Anton Khirnov committed
1618 1619 1620 1621 1622 1623 1624 1625
               avctx->height * s->last_picture_ptr->f.linesize[0]);
        memset(s->last_picture_ptr->f.data[1], 0x80,
               (avctx->height >> v_chroma_shift) *
               s->last_picture_ptr->f.linesize[1]);
        memset(s->last_picture_ptr->f.data[2], 0x80,
               (avctx->height >> v_chroma_shift) *
               s->last_picture_ptr->f.linesize[2]);

1626 1627 1628
        if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
            for(i=0; i<avctx->height; i++)
            memset(s->last_picture_ptr->f.data[0] + s->last_picture_ptr->f.linesize[0]*i, 16, avctx->width);
1629
        }
1630

Anton Khirnov's avatar
Anton Khirnov committed
1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
        ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
        ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
    }
    if ((s->next_picture_ptr == NULL ||
         s->next_picture_ptr->f.data[0] == NULL) &&
        s->pict_type == AV_PICTURE_TYPE_B) {
        /* Allocate a dummy frame */
        i = ff_find_unused_picture(s, 0);
        if (i < 0) {
            av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
            return i;
1642
        }
Anton Khirnov's avatar
Anton Khirnov committed
1643
        s->next_picture_ptr = &s->picture[i];
1644
        s->next_picture_ptr->f.key_frame = 0;
Anton Khirnov's avatar
Anton Khirnov committed
1645 1646 1647
        if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
            s->next_picture_ptr = NULL;
            return -1;
1648
        }
Anton Khirnov's avatar
Anton Khirnov committed
1649 1650 1651
        ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
        ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
    }
1652

1653
#if 0 // BUFREF-FIXME
1654 1655
    memset(s->last_picture.f.data, 0, sizeof(s->last_picture.f.data));
    memset(s->next_picture.f.data, 0, sizeof(s->next_picture.f.data));
1656
#endif
Anton Khirnov's avatar
Anton Khirnov committed
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
    if (s->last_picture_ptr) {
        ff_mpeg_unref_picture(s, &s->last_picture);
        if (s->last_picture_ptr->f.data[0] &&
            (ret = ff_mpeg_ref_picture(s, &s->last_picture,
                                       s->last_picture_ptr)) < 0)
            return ret;
    }
    if (s->next_picture_ptr) {
        ff_mpeg_unref_picture(s, &s->next_picture);
        if (s->next_picture_ptr->f.data[0] &&
            (ret = ff_mpeg_ref_picture(s, &s->next_picture,
                                       s->next_picture_ptr)) < 0)
            return ret;
    }
1671

Anton Khirnov's avatar
Anton Khirnov committed
1672 1673
    assert(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
                                                 s->last_picture_ptr->f.data[0]));
1674

1675
    if (s->picture_structure!= PICT_FRAME) {
1676
        int i;
1677 1678 1679 1680
        for (i = 0; i < 4; i++) {
            if (s->picture_structure == PICT_BOTTOM_FIELD) {
                s->current_picture.f.data[i] +=
                    s->current_picture.f.linesize[i];
1681
            }
1682 1683 1684
            s->current_picture.f.linesize[i] *= 2;
            s->last_picture.f.linesize[i]    *= 2;
            s->next_picture.f.linesize[i]    *= 2;
1685 1686
        }
    }
1687

1688
    s->err_recognition = avctx->err_recognition;
1689

1690 1691 1692
    /* set dequantizer, we can't do it during init as
     * it might change for mpeg4 and we can't do it in the header
     * decode as init is not called for mpeg4 there yet */
1693
    if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1694 1695
        s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
        s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1696
    } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1697 1698
        s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
        s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1699
    } else {
1700 1701 1702
        s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
        s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
    }
1703

1704
    if (s->dct_error_sum) {
1705
        av_assert2(s->avctx->noise_reduction && s->encoding);
1706 1707
        update_noise_reduction(s);
    }
1708

1709
    if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1710
        return ff_xvmc_field_start(s, avctx);
1711

1712
    return 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
1713
}
1714

1715 1716
/* generic function for encode/decode called after a
 * frame has been coded/decoded. */
1717
void ff_MPV_frame_end(MpegEncContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
1718
{
Michael Niedermayer's avatar
Michael Niedermayer committed
1719
    int i;
1720
    /* redraw edges for the frame if decoding didn't complete */
1721 1722
    // just to make sure that all data is rendered.
    if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
1723
        ff_xvmc_field_end(s);
1724
   } else if ((s->er.error_count || s->encoding || !(s->avctx->codec->capabilities&CODEC_CAP_DRAW_HORIZ_BAND)) &&
1725 1726 1727
              !s->avctx->hwaccel &&
              !(s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
              s->unrestricted_mv &&
1728
              s->current_picture.reference &&
1729
              !s->intra_only &&
1730 1731 1732
              !(s->flags & CODEC_FLAG_EMU_EDGE) &&
              !s->avctx->lowres
            ) {
1733 1734 1735
        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
        int hshift = desc->log2_chroma_w;
        int vshift = desc->log2_chroma_h;
1736
        s->dsp.draw_edges(s->current_picture.f.data[0], s->current_picture.f.linesize[0],
1737 1738 1739
                          s->h_edge_pos, s->v_edge_pos,
                          EDGE_WIDTH, EDGE_WIDTH,
                          EDGE_TOP | EDGE_BOTTOM);
1740
        s->dsp.draw_edges(s->current_picture.f.data[1], s->current_picture.f.linesize[1],
1741 1742 1743
                          s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
                          EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
                          EDGE_TOP | EDGE_BOTTOM);
1744
        s->dsp.draw_edges(s->current_picture.f.data[2], s->current_picture.f.linesize[2],
1745 1746 1747
                          s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
                          EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
                          EDGE_TOP | EDGE_BOTTOM);
Fabrice Bellard's avatar
Fabrice Bellard committed
1748
    }
1749

1750
    emms_c();
1751

1752 1753 1754 1755
    s->last_pict_type                 = s->pict_type;
    s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f.quality;
    if (s->pict_type!= AV_PICTURE_TYPE_B) {
        s->last_non_b_pict_type = s->pict_type;
1756
    }
1757
#if 0
1758 1759 1760 1761
    /* copy back current_picture variables */
    for (i = 0; i < MAX_PICTURE_COUNT; i++) {
        if (s->picture[i].f.data[0] == s->current_picture.f.data[0]) {
            s->picture[i] = s->current_picture;
Michael Niedermayer's avatar
Michael Niedermayer committed
1762
            break;
1763
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
1764
    }
1765
    assert(i < MAX_PICTURE_COUNT);
1766
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
1767

1768
    if (s->encoding) {
Diego Biurrun's avatar
Diego Biurrun committed
1769
        /* release non-reference frames */
1770 1771 1772
        for (i = 0; i < MAX_PICTURE_COUNT; i++) {
            if (!s->picture[i].reference)
                ff_mpeg_unref_picture(s, &s->picture[i]);
1773
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
1774
    }
1775 1776
    // clear copies, to avoid confusion
#if 0
1777 1778
    memset(&s->last_picture,    0, sizeof(Picture));
    memset(&s->next_picture,    0, sizeof(Picture));
1779 1780
    memset(&s->current_picture, 0, sizeof(Picture));
#endif
1781
    s->avctx->coded_frame = &s->current_picture_ptr->f;
1782

1783
    if (s->current_picture.reference)
1784
        ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
Fabrice Bellard's avatar
Fabrice Bellard committed
1785 1786
}

1787
/**
1788
 * Draw a line from (ex, ey) -> (sx, sy).
1789 1790 1791 1792 1793
 * @param w width of the image
 * @param h height of the image
 * @param stride stride/linesize of the image
 * @param color color of the arrow
 */
1794 1795 1796
static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
                      int w, int h, int stride, int color)
{
Michael Niedermayer's avatar
Michael Niedermayer committed
1797
    int x, y, fr, f;
1798

1799 1800 1801 1802
    sx = av_clip(sx, 0, w - 1);
    sy = av_clip(sy, 0, h - 1);
    ex = av_clip(ex, 0, w - 1);
    ey = av_clip(ey, 0, h - 1);
1803

1804
    buf[sy * stride + sx] += color;
1805

1806 1807
    if (FFABS(ex - sx) > FFABS(ey - sy)) {
        if (sx > ex) {
1808 1809
            FFSWAP(int, sx, ex);
            FFSWAP(int, sy, ey);
1810
        }
1811 1812 1813
        buf += sx + sy * stride;
        ex  -= sx;
        f    = ((ey - sy) << 16) / ex;
1814
        for (x = 0; x <= ex; x++) {
1815 1816 1817
            y  = (x * f) >> 16;
            fr = (x * f) & 0xFFFF;
            buf[y * stride + x]       += (color * (0x10000 - fr)) >> 16;
1818
            if(fr) buf[(y + 1) * stride + x] += (color *            fr ) >> 16;
1819
        }
1820 1821
    } else {
        if (sy > ey) {
1822 1823
            FFSWAP(int, sx, ex);
            FFSWAP(int, sy, ey);
1824
        }
1825 1826 1827
        buf += sx + sy * stride;
        ey  -= sy;
        if (ey)
1828
            f = ((ex - sx) << 16) / ey;
1829 1830
        else
            f = 0;
1831
        for(y= 0; y <= ey; y++){
1832 1833
            x  = (y*f) >> 16;
            fr = (y*f) & 0xFFFF;
1834
            buf[y * stride + x]     += (color * (0x10000 - fr)) >> 16;
1835
            if(fr) buf[y * stride + x + 1] += (color *            fr ) >> 16;
1836 1837 1838 1839 1840
        }
    }
}

/**
1841
 * Draw an arrow from (ex, ey) -> (sx, sy).
1842 1843 1844 1845 1846
 * @param w width of the image
 * @param h height of the image
 * @param stride stride/linesize of the image
 * @param color color of the arrow
 */
1847 1848 1849
static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
                       int ey, int w, int h, int stride, int color)
{
1850 1851
    int dx,dy;

1852 1853 1854 1855
    sx = av_clip(sx, -100, w + 100);
    sy = av_clip(sy, -100, h + 100);
    ex = av_clip(ex, -100, w + 100);
    ey = av_clip(ey, -100, h + 100);
1856

1857 1858
    dx = ex - sx;
    dy = ey - sy;
1859

1860 1861 1862 1863
    if (dx * dx + dy * dy > 3 * 3) {
        int rx =  dx + dy;
        int ry = -dx + dy;
        int length = ff_sqrt((rx * rx + ry * ry) << 8);
1864

1865 1866 1867
        // FIXME subpixel accuracy
        rx = ROUNDED_DIV(rx * 3 << 4, length);
        ry = ROUNDED_DIV(ry * 3 << 4, length);
1868

1869 1870 1871 1872 1873 1874
        draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
        draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
    }
    draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
}

1875
/**
1876
 * Print debugging info for the given picture.
1877
 */
1878
void ff_print_debug_info2(AVCodecContext *avctx, Picture *p, AVFrame *pict, uint8_t *mbskip_table,
1879
                         int *low_delay,
1880
                         int mb_width, int mb_height, int mb_stride, int quarter_sample)
1881
{
1882
    if (avctx->hwaccel || !p || !p->mb_type
1883
        || (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU))
1884
        return;
1885

1886

1887
    if (avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
1888
        int x,y;
1889

1890
        av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
1891
               av_get_picture_type_char(pict->pict_type));
1892 1893 1894 1895
        for (y = 0; y < mb_height; y++) {
            for (x = 0; x < mb_width; x++) {
                if (avctx->debug & FF_DEBUG_SKIP) {
                    int count = mbskip_table[x + y * mb_stride];
1896 1897
                    if (count > 9)
                        count = 9;
1898
                    av_log(avctx, AV_LOG_DEBUG, "%1d", count);
1899
                }
1900 1901
                if (avctx->debug & FF_DEBUG_QP) {
                    av_log(avctx, AV_LOG_DEBUG, "%2d",
1902
                           p->qscale_table[x + y * mb_stride]);
1903
                }
1904
                if (avctx->debug & FF_DEBUG_MB_TYPE) {
1905
                    int mb_type = p->mb_type[x + y * mb_stride];
1906 1907
                    // Type & MV direction
                    if (IS_PCM(mb_type))
1908
                        av_log(avctx, AV_LOG_DEBUG, "P");
1909
                    else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
1910
                        av_log(avctx, AV_LOG_DEBUG, "A");
1911
                    else if (IS_INTRA4x4(mb_type))
1912
                        av_log(avctx, AV_LOG_DEBUG, "i");
1913
                    else if (IS_INTRA16x16(mb_type))
1914
                        av_log(avctx, AV_LOG_DEBUG, "I");
1915
                    else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
1916
                        av_log(avctx, AV_LOG_DEBUG, "d");
1917
                    else if (IS_DIRECT(mb_type))
1918
                        av_log(avctx, AV_LOG_DEBUG, "D");
1919
                    else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
1920
                        av_log(avctx, AV_LOG_DEBUG, "g");
1921
                    else if (IS_GMC(mb_type))
1922
                        av_log(avctx, AV_LOG_DEBUG, "G");
1923
                    else if (IS_SKIP(mb_type))
1924
                        av_log(avctx, AV_LOG_DEBUG, "S");
1925
                    else if (!USES_LIST(mb_type, 1))
1926
                        av_log(avctx, AV_LOG_DEBUG, ">");
1927
                    else if (!USES_LIST(mb_type, 0))
1928
                        av_log(avctx, AV_LOG_DEBUG, "<");
1929
                    else {
1930
                        av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
1931
                        av_log(avctx, AV_LOG_DEBUG, "X");
1932
                    }
1933

1934 1935
                    // segmentation
                    if (IS_8X8(mb_type))
1936
                        av_log(avctx, AV_LOG_DEBUG, "+");
1937
                    else if (IS_16X8(mb_type))
1938
                        av_log(avctx, AV_LOG_DEBUG, "-");
1939
                    else if (IS_8X16(mb_type))
1940
                        av_log(avctx, AV_LOG_DEBUG, "|");
1941
                    else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
1942
                        av_log(avctx, AV_LOG_DEBUG, " ");
1943
                    else
1944
                        av_log(avctx, AV_LOG_DEBUG, "?");
1945 1946


1947
                    if (IS_INTERLACED(mb_type))
1948
                        av_log(avctx, AV_LOG_DEBUG, "=");
1949
                    else
1950
                        av_log(avctx, AV_LOG_DEBUG, " ");
1951 1952
                }
            }
1953
            av_log(avctx, AV_LOG_DEBUG, "\n");
1954 1955
        }
    }
1956

1957 1958 1959
    if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
        (avctx->debug_mv)) {
        const int shift = 1 + quarter_sample;
1960
        int mb_y;
1961 1962
        uint8_t *ptr;
        int i;
1963
        int h_chroma_shift, v_chroma_shift, block_height;
1964 1965
        const int width          = avctx->width;
        const int height         = avctx->height;
1966
        const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
1967 1968
        const int mv_stride      = (mb_width << mv_sample_log2) +
                                   (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
1969

1970
        *low_delay = 0; // needed to see the vectors without trashing the buffers
1971

1972
        avcodec_get_chroma_sub_sample(avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1973

1974 1975
        av_frame_make_writable(pict);

1976
        pict->opaque = NULL;
1977 1978
        ptr          = pict->data[0];
        block_height = 16 >> v_chroma_shift;
1979

1980
        for (mb_y = 0; mb_y < mb_height; mb_y++) {
1981
            int mb_x;
1982 1983
            for (mb_x = 0; mb_x < mb_width; mb_x++) {
                const int mb_index = mb_x + mb_y * mb_stride;
1984
                if ((avctx->debug_mv) && p->motion_val[0]) {
1985 1986 1987 1988 1989
                    int type;
                    for (type = 0; type < 3; type++) {
                        int direction = 0;
                        switch (type) {
                        case 0:
1990
                            if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
1991
                                (pict->pict_type!= AV_PICTURE_TYPE_P))
1992
                                continue;
1993 1994 1995
                            direction = 0;
                            break;
                        case 1:
1996
                            if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
1997
                                (pict->pict_type!= AV_PICTURE_TYPE_B))
1998
                                continue;
1999 2000 2001
                            direction = 0;
                            break;
                        case 2:
2002
                            if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
2003
                                (pict->pict_type!= AV_PICTURE_TYPE_B))
2004
                                continue;
2005 2006 2007
                            direction = 1;
                            break;
                        }
2008
                        if (!USES_LIST(p->mb_type[mb_index], direction))
2009 2010
                            continue;

2011
                        if (IS_8X8(p->mb_type[mb_index])) {
2012 2013 2014 2015 2016 2017
                            int i;
                            for (i = 0; i < 4; i++) {
                                int sx = mb_x * 16 + 4 + 8 * (i & 1);
                                int sy = mb_y * 16 + 4 + 8 * (i >> 1);
                                int xy = (mb_x * 2 + (i & 1) +
                                          (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
2018 2019
                                int mx = (p->motion_val[direction][xy][0] >> shift) + sx;
                                int my = (p->motion_val[direction][xy][1] >> shift) + sy;
2020
                                draw_arrow(ptr, sx, sy, mx, my, width,
2021
                                           height, pict->linesize[0], 100);
2022
                            }
2023
                        } else if (IS_16X8(p->mb_type[mb_index])) {
2024 2025 2026 2027 2028
                            int i;
                            for (i = 0; i < 2; i++) {
                                int sx = mb_x * 16 + 8;
                                int sy = mb_y * 16 + 4 + 8 * i;
                                int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
2029 2030
                                int mx = (p->motion_val[direction][xy][0] >> shift);
                                int my = (p->motion_val[direction][xy][1] >> shift);
2031

2032
                                if (IS_INTERLACED(p->mb_type[mb_index]))
2033 2034 2035
                                    my *= 2;

                            draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
2036
                                       height, pict->linesize[0], 100);
2037
                            }
2038
                        } else if (IS_8X16(p->mb_type[mb_index])) {
2039 2040 2041 2042 2043
                            int i;
                            for (i = 0; i < 2; i++) {
                                int sx = mb_x * 16 + 4 + 8 * i;
                                int sy = mb_y * 16 + 8;
                                int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
2044 2045
                                int mx = p->motion_val[direction][xy][0] >> shift;
                                int my = p->motion_val[direction][xy][1] >> shift;
2046

2047
                                if (IS_INTERLACED(p->mb_type[mb_index]))
2048 2049 2050
                                    my *= 2;

                                draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
2051
                                           height, pict->linesize[0], 100);
2052 2053
                            }
                        } else {
2054 2055 2056
                              int sx= mb_x * 16 + 8;
                              int sy= mb_y * 16 + 8;
                              int xy= (mb_x + mb_y * mv_stride) << mv_sample_log2;
2057 2058
                              int mx= (p->motion_val[direction][xy][0]>>shift) + sx;
                              int my= (p->motion_val[direction][xy][1]>>shift) + sy;
2059
                              draw_arrow(ptr, sx, sy, mx, my, width, height, pict->linesize[0], 100);
2060
                        }
2061
                    }
2062
                }
2063
                if ((avctx->debug & FF_DEBUG_VIS_QP)) {
2064
                    uint64_t c = (p->qscale_table[mb_index] * 128 / 31) *
2065
                                 0x0101010101010101ULL;
2066
                    int y;
2067 2068 2069 2070 2071 2072 2073
                    for (y = 0; y < block_height; y++) {
                        *(uint64_t *)(pict->data[1] + 8 * mb_x +
                                      (block_height * mb_y + y) *
                                      pict->linesize[1]) = c;
                        *(uint64_t *)(pict->data[2] + 8 * mb_x +
                                      (block_height * mb_y + y) *
                                      pict->linesize[2]) = c;
2074 2075
                    }
                }
2076
                if ((avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
2077
                    p->motion_val[0]) {
2078
                    int mb_type = p->mb_type[mb_index];
2079 2080
                    uint64_t u,v;
                    int y;
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108
#define COLOR(theta, r) \
    u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
    v = (int)(128 + r * sin(theta * 3.141592 / 180));


                    u = v = 128;
                    if (IS_PCM(mb_type)) {
                        COLOR(120, 48)
                    } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
                               IS_INTRA16x16(mb_type)) {
                        COLOR(30, 48)
                    } else if (IS_INTRA4x4(mb_type)) {
                        COLOR(90, 48)
                    } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
                        // COLOR(120, 48)
                    } else if (IS_DIRECT(mb_type)) {
                        COLOR(150, 48)
                    } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
                        COLOR(170, 48)
                    } else if (IS_GMC(mb_type)) {
                        COLOR(190, 48)
                    } else if (IS_SKIP(mb_type)) {
                        // COLOR(180, 48)
                    } else if (!USES_LIST(mb_type, 1)) {
                        COLOR(240, 48)
                    } else if (!USES_LIST(mb_type, 0)) {
                        COLOR(0, 48)
                    } else {
2109
                        av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
2110 2111 2112
                        COLOR(300,48)
                    }

2113 2114 2115 2116 2117 2118 2119
                    u *= 0x0101010101010101ULL;
                    v *= 0x0101010101010101ULL;
                    for (y = 0; y < block_height; y++) {
                        *(uint64_t *)(pict->data[1] + 8 * mb_x +
                                      (block_height * mb_y + y) * pict->linesize[1]) = u;
                        *(uint64_t *)(pict->data[2] + 8 * mb_x +
                                      (block_height * mb_y + y) * pict->linesize[2]) = v;
2120 2121
                    }

2122 2123 2124 2125 2126 2127
                    // segmentation
                    if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
                        *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
                                      (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
                        *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
                                      (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
2128
                    }
2129 2130 2131 2132
                    if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
                        for (y = 0; y < 16; y++)
                            pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
                                          pict->linesize[0]] ^= 0x80;
2133
                    }
2134 2135 2136 2137 2138 2139 2140 2141
                    if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
                        int dm = 1 << (mv_sample_log2 - 2);
                        for (i = 0; i < 4; i++) {
                            int sx = mb_x * 16 + 8 * (i & 1);
                            int sy = mb_y * 16 + 8 * (i >> 1);
                            int xy = (mb_x * 2 + (i & 1) +
                                     (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
                            // FIXME bidir
2142
                            int32_t *mv = (int32_t *) &p->motion_val[0][xy];
2143 2144 2145 2146 2147 2148 2149
                            if (mv[0] != mv[dm] ||
                                mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
                                for (y = 0; y < 8; y++)
                                    pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
                            if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
                                *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
                                              pict->linesize[0]) ^= 0x8080808080808080ULL;
2150 2151
                        }
                    }
2152

2153
                    if (IS_INTERLACED(mb_type) &&
2154
                        avctx->codec->id == AV_CODEC_ID_H264) {
2155 2156
                        // hmm
                    }
2157
                }
2158
                mbskip_table[mb_index] = 0;
2159 2160 2161
            }
        }
    }
2162 2163
}

2164
void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
2165
{
2166
    ff_print_debug_info2(s->avctx, p, pict, s->mbskip_table, &s->low_delay,
2167
                         s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
2168 2169
}

2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181
int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
{
    AVBufferRef *ref = av_buffer_ref(p->qscale_table_buf);
    int offset = 2*s->mb_stride + 1;
    if(!ref)
        return AVERROR(ENOMEM);
    av_assert0(ref->size >= offset + s->mb_stride * ((f->height+15)/16));
    ref->size -= offset;
    ref->data += offset;
    return av_frame_set_qp_table(f, ref, s->mb_stride, qp_type);
}

2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210
static inline int hpel_motion_lowres(MpegEncContext *s,
                                     uint8_t *dest, uint8_t *src,
                                     int field_based, int field_select,
                                     int src_x, int src_y,
                                     int width, int height, int stride,
                                     int h_edge_pos, int v_edge_pos,
                                     int w, int h, h264_chroma_mc_func *pix_op,
                                     int motion_x, int motion_y)
{
    const int lowres   = s->avctx->lowres;
    const int op_index = FFMIN(lowres, 2);
    const int s_mask   = (2 << lowres) - 1;
    int emu = 0;
    int sx, sy;

    if (s->quarter_sample) {
        motion_x /= 2;
        motion_y /= 2;
    }

    sx = motion_x & s_mask;
    sy = motion_y & s_mask;
    src_x += motion_x >> lowres + 1;
    src_y += motion_y >> lowres + 1;

    src   += src_y * stride + src_x;

    if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w,                 0) ||
        (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
2211
        s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w + 1,
2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312
                                (h + 1) << field_based, src_x,
                                src_y   << field_based,
                                h_edge_pos,
                                v_edge_pos);
        src = s->edge_emu_buffer;
        emu = 1;
    }

    sx = (sx << 2) >> lowres;
    sy = (sy << 2) >> lowres;
    if (field_select)
        src += s->linesize;
    pix_op[op_index](dest, src, stride, h, sx, sy);
    return emu;
}

/* apply one mpeg motion vector to the three components */
static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
                                                uint8_t *dest_y,
                                                uint8_t *dest_cb,
                                                uint8_t *dest_cr,
                                                int field_based,
                                                int bottom_field,
                                                int field_select,
                                                uint8_t **ref_picture,
                                                h264_chroma_mc_func *pix_op,
                                                int motion_x, int motion_y,
                                                int h, int mb_y)
{
    uint8_t *ptr_y, *ptr_cb, *ptr_cr;
    int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy,
        uvsx, uvsy;
    const int lowres     = s->avctx->lowres;
    const int op_index   = FFMIN(lowres-1+s->chroma_x_shift, 2);
    const int block_s    = 8>>lowres;
    const int s_mask     = (2 << lowres) - 1;
    const int h_edge_pos = s->h_edge_pos >> lowres;
    const int v_edge_pos = s->v_edge_pos >> lowres;
    linesize   = s->current_picture.f.linesize[0] << field_based;
    uvlinesize = s->current_picture.f.linesize[1] << field_based;

    // FIXME obviously not perfect but qpel will not work in lowres anyway
    if (s->quarter_sample) {
        motion_x /= 2;
        motion_y /= 2;
    }

    if(field_based){
        motion_y += (bottom_field - field_select)*((1 << lowres)-1);
    }

    sx = motion_x & s_mask;
    sy = motion_y & s_mask;
    src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
    src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);

    if (s->out_format == FMT_H263) {
        uvsx    = ((motion_x >> 1) & s_mask) | (sx & 1);
        uvsy    = ((motion_y >> 1) & s_mask) | (sy & 1);
        uvsrc_x = src_x >> 1;
        uvsrc_y = src_y >> 1;
    } else if (s->out_format == FMT_H261) {
        // even chroma mv's are full pel in H261
        mx      = motion_x / 4;
        my      = motion_y / 4;
        uvsx    = (2 * mx) & s_mask;
        uvsy    = (2 * my) & s_mask;
        uvsrc_x = s->mb_x * block_s + (mx >> lowres);
        uvsrc_y =    mb_y * block_s + (my >> lowres);
    } else {
        if(s->chroma_y_shift){
            mx      = motion_x / 2;
            my      = motion_y / 2;
            uvsx    = mx & s_mask;
            uvsy    = my & s_mask;
            uvsrc_x = s->mb_x * block_s                 + (mx >> lowres + 1);
            uvsrc_y =   (mb_y * block_s >> field_based) + (my >> lowres + 1);
        } else {
            if(s->chroma_x_shift){
            //Chroma422
                mx = motion_x / 2;
                uvsx = mx & s_mask;
                uvsy = motion_y & s_mask;
                uvsrc_y = src_y;
                uvsrc_x = s->mb_x*block_s               + (mx >> (lowres+1));
            } else {
            //Chroma444
                uvsx = motion_x & s_mask;
                uvsy = motion_y & s_mask;
                uvsrc_x = src_x;
                uvsrc_y = src_y;
            }
        }
    }

    ptr_y  = ref_picture[0] + src_y   * linesize   + src_x;
    ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
    ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;

    if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s,       0) ||
        (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
2313
        s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
2314
                                linesize >> field_based, 17, 17 + field_based,
2315 2316 2317 2318 2319
                                src_x, src_y << field_based, h_edge_pos,
                                v_edge_pos);
        ptr_y = s->edge_emu_buffer;
        if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
            uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
2320
            s->vdsp.emulated_edge_mc(uvbuf , ptr_cb, uvlinesize >> field_based, 9,
2321 2322 2323
                                    9 + field_based,
                                    uvsrc_x, uvsrc_y << field_based,
                                    h_edge_pos >> 1, v_edge_pos >> 1);
2324
            s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr, uvlinesize >> field_based, 9,
2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395
                                    9 + field_based,
                                    uvsrc_x, uvsrc_y << field_based,
                                    h_edge_pos >> 1, v_edge_pos >> 1);
            ptr_cb = uvbuf;
            ptr_cr = uvbuf + 16;
        }
    }

    // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f.data
    if (bottom_field) {
        dest_y  += s->linesize;
        dest_cb += s->uvlinesize;
        dest_cr += s->uvlinesize;
    }

    if (field_select) {
        ptr_y   += s->linesize;
        ptr_cb  += s->uvlinesize;
        ptr_cr  += s->uvlinesize;
    }

    sx = (sx << 2) >> lowres;
    sy = (sy << 2) >> lowres;
    pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);

    if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
        uvsx = (uvsx << 2) >> lowres;
        uvsy = (uvsy << 2) >> lowres;
        if (h >> s->chroma_y_shift) {
            pix_op[op_index](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
            pix_op[op_index](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
        }
    }
    // FIXME h261 lowres loop filter
}

static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
                                            uint8_t *dest_cb, uint8_t *dest_cr,
                                            uint8_t **ref_picture,
                                            h264_chroma_mc_func * pix_op,
                                            int mx, int my)
{
    const int lowres     = s->avctx->lowres;
    const int op_index   = FFMIN(lowres, 2);
    const int block_s    = 8 >> lowres;
    const int s_mask     = (2 << lowres) - 1;
    const int h_edge_pos = s->h_edge_pos >> lowres + 1;
    const int v_edge_pos = s->v_edge_pos >> lowres + 1;
    int emu = 0, src_x, src_y, offset, sx, sy;
    uint8_t *ptr;

    if (s->quarter_sample) {
        mx /= 2;
        my /= 2;
    }

    /* In case of 8X8, we construct a single chroma motion vector
       with a special rounding */
    mx = ff_h263_round_chroma(mx);
    my = ff_h263_round_chroma(my);

    sx = mx & s_mask;
    sy = my & s_mask;
    src_x = s->mb_x * block_s + (mx >> lowres + 1);
    src_y = s->mb_y * block_s + (my >> lowres + 1);

    offset = src_y * s->uvlinesize + src_x;
    ptr = ref_picture[1] + offset;
    if (s->flags & CODEC_FLAG_EMU_EDGE) {
        if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
            (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
2396
            s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407
                                    9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
            ptr = s->edge_emu_buffer;
            emu = 1;
        }
    }
    sx = (sx << 2) >> lowres;
    sy = (sy << 2) >> lowres;
    pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);

    ptr = ref_picture[2] + offset;
    if (emu) {
2408
        s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9,
2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530
                                src_x, src_y, h_edge_pos, v_edge_pos);
        ptr = s->edge_emu_buffer;
    }
    pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
}

/**
 * motion compensation of a single macroblock
 * @param s context
 * @param dest_y luma destination pointer
 * @param dest_cb chroma cb/u destination pointer
 * @param dest_cr chroma cr/v destination pointer
 * @param dir direction (0->forward, 1->backward)
 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
 * @param pix_op halfpel motion compensation function (average or put normally)
 * the motion vectors are taken from s->mv and the MV type from s->mv_type
 */
static inline void MPV_motion_lowres(MpegEncContext *s,
                                     uint8_t *dest_y, uint8_t *dest_cb,
                                     uint8_t *dest_cr,
                                     int dir, uint8_t **ref_picture,
                                     h264_chroma_mc_func *pix_op)
{
    int mx, my;
    int mb_x, mb_y, i;
    const int lowres  = s->avctx->lowres;
    const int block_s = 8 >>lowres;

    mb_x = s->mb_x;
    mb_y = s->mb_y;

    switch (s->mv_type) {
    case MV_TYPE_16X16:
        mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
                           0, 0, 0,
                           ref_picture, pix_op,
                           s->mv[dir][0][0], s->mv[dir][0][1],
                           2 * block_s, mb_y);
        break;
    case MV_TYPE_8X8:
        mx = 0;
        my = 0;
        for (i = 0; i < 4; i++) {
            hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
                               s->linesize) * block_s,
                               ref_picture[0], 0, 0,
                               (2 * mb_x + (i & 1)) * block_s,
                               (2 * mb_y + (i >> 1)) * block_s,
                               s->width, s->height, s->linesize,
                               s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
                               block_s, block_s, pix_op,
                               s->mv[dir][i][0], s->mv[dir][i][1]);

            mx += s->mv[dir][i][0];
            my += s->mv[dir][i][1];
        }

        if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
            chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
                                     pix_op, mx, my);
        break;
    case MV_TYPE_FIELD:
        if (s->picture_structure == PICT_FRAME) {
            /* top field */
            mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
                               1, 0, s->field_select[dir][0],
                               ref_picture, pix_op,
                               s->mv[dir][0][0], s->mv[dir][0][1],
                               block_s, mb_y);
            /* bottom field */
            mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
                               1, 1, s->field_select[dir][1],
                               ref_picture, pix_op,
                               s->mv[dir][1][0], s->mv[dir][1][1],
                               block_s, mb_y);
        } else {
            if (s->picture_structure != s->field_select[dir][0] + 1 &&
                s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
                ref_picture = s->current_picture_ptr->f.data;

            }
            mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
                               0, 0, s->field_select[dir][0],
                               ref_picture, pix_op,
                               s->mv[dir][0][0],
                               s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
            }
        break;
    case MV_TYPE_16X8:
        for (i = 0; i < 2; i++) {
            uint8_t **ref2picture;

            if (s->picture_structure == s->field_select[dir][i] + 1 ||
                s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
                ref2picture = ref_picture;
            } else {
                ref2picture = s->current_picture_ptr->f.data;
            }

            mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
                               0, 0, s->field_select[dir][i],
                               ref2picture, pix_op,
                               s->mv[dir][i][0], s->mv[dir][i][1] +
                               2 * block_s * i, block_s, mb_y >> 1);

            dest_y  +=  2 * block_s *  s->linesize;
            dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
            dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
        }
        break;
    case MV_TYPE_DMV:
        if (s->picture_structure == PICT_FRAME) {
            for (i = 0; i < 2; i++) {
                int j;
                for (j = 0; j < 2; j++) {
                    mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
                                       1, j, j ^ i,
                                       ref_picture, pix_op,
                                       s->mv[dir][2 * i + j][0],
                                       s->mv[dir][2 * i + j][1],
                                       block_s, mb_y);
                }
2531
                pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
2532 2533 2534 2535 2536 2537 2538 2539 2540 2541
            }
        } else {
            for (i = 0; i < 2; i++) {
                mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
                                   0, 0, s->picture_structure != i + 1,
                                   ref_picture, pix_op,
                                   s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
                                   2 * block_s, mb_y >> 1);

                // after put we make avg of the same block
2542
                pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
2543 2544 2545 2546 2547 2548 2549 2550 2551 2552

                // opposite parity is always in the same
                // frame if this is second field
                if (!s->first_field) {
                    ref_picture = s->current_picture_ptr->f.data;
                }
            }
        }
        break;
    default:
2553
        av_assert2(0);
2554 2555 2556
    }
}

2557 2558 2559
/**
 * find the lowest MB row referenced in the MVs
 */
2560
int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
2561 2562 2563 2564
{
    int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
    int my, off, i, mvs;

2565 2566
    if (s->picture_structure != PICT_FRAME || s->mcsel)
        goto unhandled;
2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594

    switch (s->mv_type) {
        case MV_TYPE_16X16:
            mvs = 1;
            break;
        case MV_TYPE_16X8:
            mvs = 2;
            break;
        case MV_TYPE_8X8:
            mvs = 4;
            break;
        default:
            goto unhandled;
    }

    for (i = 0; i < mvs; i++) {
        my = s->mv[dir][i][1]<<qpel_shift;
        my_max = FFMAX(my_max, my);
        my_min = FFMIN(my_min, my);
    }

    off = (FFMAX(-my_min, my_max) + 63) >> 6;

    return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
unhandled:
    return s->mb_height-1;
}

2595 2596
/* put block[] to dest[] */
static inline void put_dct(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
2597
                           int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
2598
{
2599 2600 2601
    s->dct_unquantize_intra(s, block, i, qscale);
    s->dsp.idct_put (dest, line_size, block);
}
2602

2603 2604
/* add block[] to dest[] */
static inline void add_dct(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
2605
                           int16_t *block, int i, uint8_t *dest, int line_size)
2606 2607 2608 2609 2610
{
    if (s->block_last_index[i] >= 0) {
        s->dsp.idct_add (dest, line_size, block);
    }
}
2611

2612
static inline void add_dequant_dct(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
2613
                           int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
2614
{
Fabrice Bellard's avatar
Fabrice Bellard committed
2615
    if (s->block_last_index[i] >= 0) {
2616
        s->dct_unquantize_inter(s, block, i, qscale);
2617

2618
        s->dsp.idct_add (dest, line_size, block);
Fabrice Bellard's avatar
Fabrice Bellard committed
2619 2620 2621
    }
}

2622
/**
2623
 * Clean dc, ac, coded_block for the current non-intra MB.
2624 2625 2626
 */
void ff_clean_intra_table_entries(MpegEncContext *s)
{
2627
    int wrap = s->b8_stride;
2628
    int xy = s->block_index[0];
2629 2630 2631

    s->dc_val[0][xy           ] =
    s->dc_val[0][xy + 1       ] =
2632 2633 2634
    s->dc_val[0][xy     + wrap] =
    s->dc_val[0][xy + 1 + wrap] = 1024;
    /* ac pred */
2635 2636
    memset(s->ac_val[0][xy       ], 0, 32 * sizeof(int16_t));
    memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
2637 2638 2639 2640 2641 2642 2643
    if (s->msmpeg4_version>=3) {
        s->coded_block[xy           ] =
        s->coded_block[xy + 1       ] =
        s->coded_block[xy     + wrap] =
        s->coded_block[xy + 1 + wrap] = 0;
    }
    /* chroma */
2644 2645
    wrap = s->mb_stride;
    xy = s->mb_x + s->mb_y * wrap;
2646 2647 2648
    s->dc_val[1][xy] =
    s->dc_val[2][xy] = 1024;
    /* ac pred */
2649 2650
    memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
    memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
2651

2652
    s->mbintra_table[xy]= 0;
2653 2654
}

Fabrice Bellard's avatar
Fabrice Bellard committed
2655 2656 2657 2658 2659 2660 2661 2662 2663 2664
/* generic function called after a macroblock has been parsed by the
   decoder or after it has been encoded by the encoder.

   Important variables used:
   s->mb_intra : true if intra macroblock
   s->mv_dir   : motion vector direction
   s->mv_type  : motion vector type
   s->mv       : motion vector
   s->interlaced_dct : true if interlaced dct used (mpeg2)
 */
2665
static av_always_inline
Diego Biurrun's avatar
Diego Biurrun committed
2666
void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
2667
                            int lowres_flag, int is_mpeg12)
Fabrice Bellard's avatar
Fabrice Bellard committed
2668
{
2669
    const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
2670
    if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
2671
        ff_xvmc_decode_mb(s);//xvmc uses pblocks
Ivan Kalvachev's avatar
Ivan Kalvachev committed
2672 2673
        return;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
2674

2675
    if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
2676
       /* print DCT coefficients */
2677
       int i,j;
2678 2679 2680
       av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
       for(i=0; i<6; i++){
           for(j=0; j<64; j++){
2681
               av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]);
2682 2683 2684
           }
           av_log(s->avctx, AV_LOG_DEBUG, "\n");
       }
2685 2686
    }

2687
    s->current_picture.qscale_table[mb_xy] = s->qscale;
2688

Fabrice Bellard's avatar
Fabrice Bellard committed
2689 2690
    /* update DC predictors for P macroblocks */
    if (!s->mb_intra) {
Keiji Costantini's avatar
Keiji Costantini committed
2691
        if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
2692
            if(s->mbintra_table[mb_xy])
2693
                ff_clean_intra_table_entries(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
2694
        } else {
2695 2696
            s->last_dc[0] =
            s->last_dc[1] =
Fabrice Bellard's avatar
Fabrice Bellard committed
2697 2698 2699
            s->last_dc[2] = 128 << s->intra_dc_precision;
        }
    }
Keiji Costantini's avatar
Keiji Costantini committed
2700
    else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
2701
        s->mbintra_table[mb_xy]=1;
2702

2703
    if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
2704
        uint8_t *dest_y, *dest_cb, *dest_cr;
2705
        int dct_linesize, dct_offset;
Michael Niedermayer's avatar
Michael Niedermayer committed
2706 2707
        op_pixels_func (*op_pix)[4];
        qpel_mc_func (*op_qpix)[16];
2708 2709
        const int linesize   = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
        const int uvlinesize = s->current_picture.f.linesize[1];
2710 2711
        const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
        const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
2712

Michael Niedermayer's avatar
Michael Niedermayer committed
2713 2714 2715
        /* avoid copy if macroblock skipped in last frame too */
        /* skip only during decoding as we might trash the buffers during encoding a bit */
        if(!s->encoding){
2716
            uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
Michael Niedermayer's avatar
Michael Niedermayer committed
2717

2718 2719
            if (s->mb_skipped) {
                s->mb_skipped= 0;
2720
                av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
2721
                *mbskip_ptr = 1;
2722
            } else if(!s->current_picture.reference) {
2723
                *mbskip_ptr = 1;
2724
            } else{
2725 2726
                *mbskip_ptr = 0; /* not skipped */
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
2727
        }
2728

2729
        dct_linesize = linesize << s->interlaced_dct;
2730
        dct_offset   = s->interlaced_dct ? linesize : linesize * block_size;
2731

2732 2733 2734 2735 2736
        if(readable){
            dest_y=  s->dest[0];
            dest_cb= s->dest[1];
            dest_cr= s->dest[2];
        }else{
2737
            dest_y = s->b_scratchpad;
2738
            dest_cb= s->b_scratchpad+16*linesize;
2739
            dest_cr= s->b_scratchpad+32*linesize;
2740
        }
2741

Fabrice Bellard's avatar
Fabrice Bellard committed
2742 2743
        if (!s->mb_intra) {
            /* motion handling */
2744
            /* decoding or more than one mb_type (MC was already done otherwise) */
2745
            if(!s->encoding){
2746

2747
                if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
2748
                    if (s->mv_dir & MV_DIR_FORWARD) {
2749
                        ff_thread_await_progress(&s->last_picture_ptr->tf,
2750 2751
                                                 ff_MPV_lowest_referenced_row(s, 0),
                                                 0);
2752 2753
                    }
                    if (s->mv_dir & MV_DIR_BACKWARD) {
2754
                        ff_thread_await_progress(&s->next_picture_ptr->tf,
2755 2756
                                                 ff_MPV_lowest_referenced_row(s, 1),
                                                 0);
2757 2758 2759
                    }
                }

2760
                if(lowres_flag){
2761
                    h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
2762 2763 2764

                    if (s->mv_dir & MV_DIR_FORWARD) {
                        MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix);
2765
                        op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
2766 2767 2768 2769
                    }
                    if (s->mv_dir & MV_DIR_BACKWARD) {
                        MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix);
                    }
2770
                }else{
2771 2772
                    op_qpix= s->me.qpel_put;
                    if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
2773
                        op_pix = s->hdsp.put_pixels_tab;
2774
                    }else{
2775
                        op_pix = s->hdsp.put_no_rnd_pixels_tab;
2776 2777
                    }
                    if (s->mv_dir & MV_DIR_FORWARD) {
2778
                        ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
2779
                        op_pix = s->hdsp.avg_pixels_tab;
2780 2781 2782
                        op_qpix= s->me.qpel_avg;
                    }
                    if (s->mv_dir & MV_DIR_BACKWARD) {
2783
                        ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
2784
                    }
2785
                }
Fabrice Bellard's avatar
Fabrice Bellard committed
2786 2787
            }

2788
            /* skip dequant / idct if we are really late ;) */
Michael Niedermayer's avatar
Michael Niedermayer committed
2789
            if(s->avctx->skip_idct){
2790 2791
                if(  (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
                   ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
Michael Niedermayer's avatar
Michael Niedermayer committed
2792 2793 2794
                   || s->avctx->skip_idct >= AVDISCARD_ALL)
                    goto skip_idct;
            }
2795

Fabrice Bellard's avatar
Fabrice Bellard committed
2796
            /* add dct residue */
2797 2798
            if(s->encoding || !(   s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
                                || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
2799
                add_dequant_dct(s, block[0], 0, dest_y                          , dct_linesize, s->qscale);
2800
                add_dequant_dct(s, block[1], 1, dest_y              + block_size, dct_linesize, s->qscale);
2801
                add_dequant_dct(s, block[2], 2, dest_y + dct_offset             , dct_linesize, s->qscale);
2802
                add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
2803

2804
                if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815
                    if (s->chroma_y_shift){
                        add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
                        add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
                    }else{
                        dct_linesize >>= 1;
                        dct_offset >>=1;
                        add_dequant_dct(s, block[4], 4, dest_cb,              dct_linesize, s->chroma_qscale);
                        add_dequant_dct(s, block[5], 5, dest_cr,              dct_linesize, s->chroma_qscale);
                        add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
                        add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
                    }
2816
                }
2817
            } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
2818
                add_dct(s, block[0], 0, dest_y                          , dct_linesize);
2819
                add_dct(s, block[1], 1, dest_y              + block_size, dct_linesize);
2820
                add_dct(s, block[2], 2, dest_y + dct_offset             , dct_linesize);
2821
                add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
Fabrice Bellard's avatar
Fabrice Bellard committed
2822

2823
                if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2824 2825 2826 2827 2828 2829
                    if(s->chroma_y_shift){//Chroma420
                        add_dct(s, block[4], 4, dest_cb, uvlinesize);
                        add_dct(s, block[5], 5, dest_cr, uvlinesize);
                    }else{
                        //chroma422
                        dct_linesize = uvlinesize << s->interlaced_dct;
2830
                        dct_offset   = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
2831 2832 2833 2834 2835 2836

                        add_dct(s, block[4], 4, dest_cb, dct_linesize);
                        add_dct(s, block[5], 5, dest_cr, dct_linesize);
                        add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
                        add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
                        if(!s->chroma_x_shift){//Chroma444
2837 2838 2839 2840
                            add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
                            add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
                            add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
                            add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
2841 2842 2843 2844
                        }
                    }
                }//fi gray
            }
2845
            else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
Michael Niedermayer's avatar
Michael Niedermayer committed
2846
                ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
2847
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
2848 2849
        } else {
            /* dct only in intra block */
2850
            if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
2851 2852 2853 2854
                put_dct(s, block[0], 0, dest_y                          , dct_linesize, s->qscale);
                put_dct(s, block[1], 1, dest_y              + block_size, dct_linesize, s->qscale);
                put_dct(s, block[2], 2, dest_y + dct_offset             , dct_linesize, s->qscale);
                put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
2855

2856
                if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2857 2858 2859 2860 2861 2862 2863 2864 2865
                    if(s->chroma_y_shift){
                        put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
                        put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
                    }else{
                        dct_offset >>=1;
                        dct_linesize >>=1;
                        put_dct(s, block[4], 4, dest_cb,              dct_linesize, s->chroma_qscale);
                        put_dct(s, block[5], 5, dest_cr,              dct_linesize, s->chroma_qscale);
                        put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
2866
                        put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
2867 2868 2869
                    }
                }
            }else{
2870
                s->dsp.idct_put(dest_y                          , dct_linesize, block[0]);
2871
                s->dsp.idct_put(dest_y              + block_size, dct_linesize, block[1]);
2872
                s->dsp.idct_put(dest_y + dct_offset             , dct_linesize, block[2]);
2873
                s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
2874

2875
                if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2876 2877 2878 2879
                    if(s->chroma_y_shift){
                        s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
                        s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
                    }else{
2880

2881
                        dct_linesize = uvlinesize << s->interlaced_dct;
2882
                        dct_offset   = s->interlaced_dct? uvlinesize : uvlinesize*block_size;
2883

2884 2885 2886 2887 2888
                        s->dsp.idct_put(dest_cb,              dct_linesize, block[4]);
                        s->dsp.idct_put(dest_cr,              dct_linesize, block[5]);
                        s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
                        s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
                        if(!s->chroma_x_shift){//Chroma444
2889 2890 2891 2892
                            s->dsp.idct_put(dest_cb + block_size,              dct_linesize, block[8]);
                            s->dsp.idct_put(dest_cr + block_size,              dct_linesize, block[9]);
                            s->dsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
                            s->dsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
2893 2894 2895
                        }
                    }
                }//gray
2896
            }
2897
        }
2898 2899
skip_idct:
        if(!readable){
2900 2901 2902
            s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y ,   linesize,16);
            s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
            s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
2903
        }
2904 2905 2906
    }
}

Diego Biurrun's avatar
Diego Biurrun committed
2907
void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
2908
#if !CONFIG_SMALL
Keiji Costantini's avatar
Keiji Costantini committed
2909
    if(s->out_format == FMT_MPEG1) {
2910 2911
        if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
        else                 MPV_decode_mb_internal(s, block, 0, 1);
Keiji Costantini's avatar
Keiji Costantini committed
2912 2913
    } else
#endif
2914 2915
    if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
    else                  MPV_decode_mb_internal(s, block, 0, 0);
2916 2917
}

2918 2919 2920
/**
 * @param h is the normal height, this will be reduced automatically if needed for the last row
 */
2921 2922 2923 2924 2925 2926 2927 2928 2929
void ff_draw_horiz_band(AVCodecContext *avctx, DSPContext *dsp, Picture *cur,
                        Picture *last, int y, int h, int picture_structure,
                        int first_field, int draw_edges, int low_delay,
                        int v_edge_pos, int h_edge_pos)
{
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
    int hshift = desc->log2_chroma_w;
    int vshift = desc->log2_chroma_h;
    const int field_pic = picture_structure != PICT_FRAME;
2930 2931 2932 2933 2934
    if(field_pic){
        h <<= 1;
        y <<= 1;
    }

2935 2936 2937
    if (!avctx->hwaccel &&
        !(avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
        draw_edges &&
2938
        cur->reference &&
2939 2940
        !(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
        int *linesize = cur->f.linesize;
2941 2942
        int sides = 0, edge_h;
        if (y==0) sides |= EDGE_TOP;
2943 2944
        if (y + h >= v_edge_pos)
            sides |= EDGE_BOTTOM;
2945

2946
        edge_h= FFMIN(h, v_edge_pos - y);
2947

2948 2949 2950 2951 2952 2953 2954 2955 2956
        dsp->draw_edges(cur->f.data[0] + y * linesize[0],
                        linesize[0], h_edge_pos, edge_h,
                        EDGE_WIDTH, EDGE_WIDTH, sides);
        dsp->draw_edges(cur->f.data[1] + (y >> vshift) * linesize[1],
                        linesize[1], h_edge_pos >> hshift, edge_h >> vshift,
                        EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides);
        dsp->draw_edges(cur->f.data[2] + (y >> vshift) * linesize[2],
                        linesize[2], h_edge_pos >> hshift, edge_h >> vshift,
                        EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides);
2957 2958
    }

2959
    h = FFMIN(h, avctx->height - y);
2960

2961
    if(field_pic && first_field && !(avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
2962

2963
    if (avctx->draw_horiz_band) {
2964
        AVFrame *src;
2965 2966
        int offset[AV_NUM_DATA_POINTERS];
        int i;
2967

2968 2969 2970 2971 2972
        if(cur->f.pict_type == AV_PICTURE_TYPE_B || low_delay ||
           (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
            src = &cur->f;
        else if (last)
            src = &last->f;
2973 2974
        else
            return;
2975

2976
        if (cur->f.pict_type == AV_PICTURE_TYPE_B &&
2977
            picture_structure == PICT_FRAME &&
2978
            avctx->codec_id != AV_CODEC_ID_SVQ3) {
2979 2980
            for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
                offset[i] = 0;
2981
        }else{
2982
            offset[0]= y * src->linesize[0];
2983
            offset[1]=
2984
            offset[2]= (y >> vshift) * src->linesize[1];
2985 2986
            for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
                offset[i] = 0;
2987
        }
2988

2989
        emms_c();
2990

2991 2992
        avctx->draw_horiz_band(avctx, src, offset,
                               y, picture_structure, h);
2993 2994
    }
}
2995

2996 2997 2998 2999 3000 3001 3002 3003 3004
void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
{
    int draw_edges = s->unrestricted_mv && !s->intra_only;
    ff_draw_horiz_band(s->avctx, &s->dsp, &s->current_picture,
                       &s->last_picture, y, h, s->picture_structure,
                       s->first_field, draw_edges, s->low_delay,
                       s->v_edge_pos, s->h_edge_pos);
}

3005
void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
3006 3007
    const int linesize   = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
    const int uvlinesize = s->current_picture.f.linesize[1];
Michael Niedermayer's avatar
Michael Niedermayer committed
3008
    const int mb_size= 4 - s->avctx->lowres;
3009

3010 3011 3012 3013 3014 3015 3016
    s->block_index[0]= s->b8_stride*(s->mb_y*2    ) - 2 + s->mb_x*2;
    s->block_index[1]= s->b8_stride*(s->mb_y*2    ) - 1 + s->mb_x*2;
    s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
    s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
    s->block_index[4]= s->mb_stride*(s->mb_y + 1)                + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
    s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
    //block_index is not used by mpeg2, so it is not affected by chroma_format
3017

3018 3019 3020
    s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) <<  mb_size);
    s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
    s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
3021

3022
    if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
3023
    {
3024
        if(s->picture_structure==PICT_FRAME){
3025 3026 3027
        s->dest[0] += s->mb_y *   linesize << mb_size;
        s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
        s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
3028 3029 3030 3031
        }else{
            s->dest[0] += (s->mb_y>>1) *   linesize << mb_size;
            s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
            s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
3032
            av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
3033
        }
3034 3035
    }
}
3036

3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065
/**
 * Permute an 8x8 block.
 * @param block the block which will be permuted according to the given permutation vector
 * @param permutation the permutation vector
 * @param last the last non zero coefficient in scantable order, used to speed the permutation up
 * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
 *                  (inverse) permutated to scantable order!
 */
void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
{
    int i;
    int16_t temp[64];

    if(last<=0) return;
    //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations

    for(i=0; i<=last; i++){
        const int j= scantable[i];
        temp[j]= block[j];
        block[j]=0;
    }

    for(i=0; i<=last; i++){
        const int j= scantable[i];
        const int perm_j= permutation[j];
        block[perm_j]= temp[j];
    }
}

3066 3067 3068
void ff_mpeg_flush(AVCodecContext *avctx){
    int i;
    MpegEncContext *s = avctx->priv_data;
3069

3070 3071
    if(s==NULL || s->picture==NULL)
        return;
3072

3073 3074
    for (i = 0; i < MAX_PICTURE_COUNT; i++)
        ff_mpeg_unref_picture(s, &s->picture[i]);
3075
    s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
3076

3077 3078 3079 3080
    ff_mpeg_unref_picture(s, &s->current_picture);
    ff_mpeg_unref_picture(s, &s->last_picture);
    ff_mpeg_unref_picture(s, &s->next_picture);

3081
    s->mb_x= s->mb_y= 0;
3082
    s->closed_gop= 0;
3083

3084 3085 3086 3087 3088 3089 3090 3091
    s->parse_context.state= -1;
    s->parse_context.frame_start_found= 0;
    s->parse_context.overread= 0;
    s->parse_context.overread_index= 0;
    s->parse_context.index= 0;
    s->parse_context.last_index= 0;
    s->bitstream_buffer_size=0;
    s->pp_time=0;
Fabrice Bellard's avatar
Fabrice Bellard committed
3092 3093
}

3094
static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
3095
                                   int16_t *block, int n, int qscale)
Fabrice Bellard's avatar
Fabrice Bellard committed
3096
{
3097
    int i, level, nCoeffs;
3098
    const uint16_t *quant_matrix;
Fabrice Bellard's avatar
Fabrice Bellard committed
3099

3100
    nCoeffs= s->block_last_index[n];
3101

3102
    block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116
    /* XXX: only mpeg1 */
    quant_matrix = s->intra_matrix;
    for(i=1;i<=nCoeffs;i++) {
        int j= s->intra_scantable.permutated[i];
        level = block[j];
        if (level) {
            if (level < 0) {
                level = -level;
                level = (int)(level * qscale * quant_matrix[j]) >> 3;
                level = (level - 1) | 1;
                level = -level;
            } else {
                level = (int)(level * qscale * quant_matrix[j]) >> 3;
                level = (level - 1) | 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
3117
            }
3118
            block[j] = level;
Fabrice Bellard's avatar
Fabrice Bellard committed
3119
        }
3120 3121 3122
    }
}

3123
static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
3124
                                   int16_t *block, int n, int qscale)
3125 3126 3127 3128 3129
{
    int i, level, nCoeffs;
    const uint16_t *quant_matrix;

    nCoeffs= s->block_last_index[n];
3130

3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145
    quant_matrix = s->inter_matrix;
    for(i=0; i<=nCoeffs; i++) {
        int j= s->intra_scantable.permutated[i];
        level = block[j];
        if (level) {
            if (level < 0) {
                level = -level;
                level = (((level << 1) + 1) * qscale *
                         ((int) (quant_matrix[j]))) >> 4;
                level = (level - 1) | 1;
                level = -level;
            } else {
                level = (((level << 1) + 1) * qscale *
                         ((int) (quant_matrix[j]))) >> 4;
                level = (level - 1) | 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
3146
            }
3147
            block[j] = level;
Fabrice Bellard's avatar
Fabrice Bellard committed
3148 3149 3150
        }
    }
}
3151

3152
static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
3153
                                   int16_t *block, int n, int qscale)
3154 3155
{
    int i, level, nCoeffs;
3156
    const uint16_t *quant_matrix;
3157

3158 3159
    if(s->alternate_scan) nCoeffs= 63;
    else nCoeffs= s->block_last_index[n];
3160

3161
    block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178
    quant_matrix = s->intra_matrix;
    for(i=1;i<=nCoeffs;i++) {
        int j= s->intra_scantable.permutated[i];
        level = block[j];
        if (level) {
            if (level < 0) {
                level = -level;
                level = (int)(level * qscale * quant_matrix[j]) >> 3;
                level = -level;
            } else {
                level = (int)(level * qscale * quant_matrix[j]) >> 3;
            }
            block[j] = level;
        }
    }
}

3179
static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
3180
                                   int16_t *block, int n, int qscale)
3181 3182 3183 3184 3185 3186 3187 3188
{
    int i, level, nCoeffs;
    const uint16_t *quant_matrix;
    int sum=-1;

    if(s->alternate_scan) nCoeffs= 63;
    else nCoeffs= s->block_last_index[n];

3189
    block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
3190
    sum += block[0];
3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209
    quant_matrix = s->intra_matrix;
    for(i=1;i<=nCoeffs;i++) {
        int j= s->intra_scantable.permutated[i];
        level = block[j];
        if (level) {
            if (level < 0) {
                level = -level;
                level = (int)(level * qscale * quant_matrix[j]) >> 3;
                level = -level;
            } else {
                level = (int)(level * qscale * quant_matrix[j]) >> 3;
            }
            block[j] = level;
            sum+=level;
        }
    }
    block[63]^=sum&1;
}

3210
static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
3211
                                   int16_t *block, int n, int qscale)
3212 3213 3214 3215 3216 3217 3218
{
    int i, level, nCoeffs;
    const uint16_t *quant_matrix;
    int sum=-1;

    if(s->alternate_scan) nCoeffs= 63;
    else nCoeffs= s->block_last_index[n];
3219

3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240
    quant_matrix = s->inter_matrix;
    for(i=0; i<=nCoeffs; i++) {
        int j= s->intra_scantable.permutated[i];
        level = block[j];
        if (level) {
            if (level < 0) {
                level = -level;
                level = (((level << 1) + 1) * qscale *
                         ((int) (quant_matrix[j]))) >> 4;
                level = -level;
            } else {
                level = (((level << 1) + 1) * qscale *
                         ((int) (quant_matrix[j]))) >> 4;
            }
            block[j] = level;
            sum+=level;
        }
    }
    block[63]^=sum&1;
}

3241
static void dct_unquantize_h263_intra_c(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
3242
                                  int16_t *block, int n, int qscale)
3243 3244 3245
{
    int i, level, qmul, qadd;
    int nCoeffs;
3246

3247
    av_assert2(s->block_last_index[n]>=0);
3248

3249
    qmul = qscale << 1;
3250

3251
    if (!s->h263_aic) {
3252
        block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268
        qadd = (qscale - 1) | 1;
    }else{
        qadd = 0;
    }
    if(s->ac_pred)
        nCoeffs=63;
    else
        nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];

    for(i=1; i<=nCoeffs; i++) {
        level = block[i];
        if (level) {
            if (level < 0) {
                level = level * qmul - qadd;
            } else {
                level = level * qmul + qadd;
3269
            }
3270
            block[i] = level;
3271 3272 3273 3274
        }
    }
}

3275
static void dct_unquantize_h263_inter_c(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
3276
                                  int16_t *block, int n, int qscale)
3277 3278
{
    int i, level, qmul, qadd;
3279
    int nCoeffs;
3280

3281
    av_assert2(s->block_last_index[n]>=0);
3282

3283 3284
    qadd = (qscale - 1) | 1;
    qmul = qscale << 1;
3285

3286
    nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
3287

3288
    for(i=0; i<=nCoeffs; i++) {
3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299
        level = block[i];
        if (level) {
            if (level < 0) {
                level = level * qmul - qadd;
            } else {
                level = level * qmul + qadd;
            }
            block[i] = level;
        }
    }
}
Fabrice Bellard's avatar
Fabrice Bellard committed
3300

3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316
/**
 * set qscale and update qscale dependent variables.
 */
void ff_set_qscale(MpegEncContext * s, int qscale)
{
    if (qscale < 1)
        qscale = 1;
    else if (qscale > 31)
        qscale = 31;

    s->qscale = qscale;
    s->chroma_qscale= s->chroma_qscale_table[qscale];

    s->y_dc_scale= s->y_dc_scale_table[ qscale ];
    s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
}
3317

3318
void ff_MPV_report_decode_progress(MpegEncContext *s)
3319
{
3320
    if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
3321
        ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
3322
}
3323

3324
#if CONFIG_ERROR_RESILIENCE
3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339
void ff_mpeg_er_frame_start(MpegEncContext *s)
{
    ERContext *er = &s->er;

    er->cur_pic  = s->current_picture_ptr;
    er->last_pic = s->last_picture_ptr;
    er->next_pic = s->next_picture_ptr;

    er->pp_time           = s->pp_time;
    er->pb_time           = s->pb_time;
    er->quarter_sample    = s->quarter_sample;
    er->partitioned_frame = s->partitioned_frame;

    ff_er_frame_start(er);
}
3340
#endif /* CONFIG_ERROR_RESILIENCE */