mpegvideo.c 94.6 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
 * This file is part of Libav.
9
 *
10
 * Libav is free software; you can redistribute it and/or
Fabrice Bellard's avatar
Fabrice Bellard committed
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
 * Libav 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
Fabrice Bellard's avatar
Fabrice Bellard committed
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
 *
Fabrice Bellard's avatar
Fabrice Bellard committed
20
 * You should have received a copy of the GNU Lesser General Public
21
 * License along with Libav; 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/intmath.h"
31
#include "libavutil/imgutils.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
32 33
#include "avcodec.h"
#include "dsputil.h"
34
#include "internal.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
35
#include "mpegvideo.h"
36
#include "mjpegenc.h"
37
#include "msmpeg4.h"
38
#include "xvmc_internal.h"
39
#include "thread.h"
40
#include <limits.h>
Fabrice Bellard's avatar
Fabrice Bellard committed
41

42 43
//#undef NDEBUG
//#include <assert.h>
44

45
static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
46
                                   DCTELEM *block, int n, int qscale);
47
static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
48
                                   DCTELEM *block, int n, int qscale);
49 50
static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
                                   DCTELEM *block, int n, int qscale);
51 52
static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
                                   DCTELEM *block, int n, int qscale);
53 54
static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
                                   DCTELEM *block, int n, int qscale);
55
static void dct_unquantize_h263_intra_c(MpegEncContext *s,
56
                                  DCTELEM *block, int n, int qscale);
57
static void dct_unquantize_h263_inter_c(MpegEncContext *s,
58
                                  DCTELEM *block, int n, int qscale);
Michael Niedermayer's avatar
Michael Niedermayer committed
59

Fabrice Bellard's avatar
Fabrice Bellard committed
60 61 62 63 64 65

/* enable all paranoid tests for rounding, overflows, etc... */
//#define PARANOID

//#define DEBUG

66

67 68 69 70
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
71 72
};

73 74 75 76 77 78 79 80 81 82
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,
83 84
};

85 86 87 88 89 90 91 92 93 94
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,
95 96
};

97 98 99 100 101 102 103 104 105 106
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,
107 108
};

109 110 111 112 113 114 115 116 117 118
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,
119 120
};

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

128
const enum PixelFormat ff_pixfmt_list_420[] = {
129 130 131 132 133
    PIX_FMT_YUV420P,
    PIX_FMT_NONE
};

const enum PixelFormat ff_hwaccel_pixfmt_list_420[] = {
Laurent Aimar's avatar
Laurent Aimar committed
134
    PIX_FMT_DXVA2_VLD,
135
    PIX_FMT_VAAPI_VLD,
136
    PIX_FMT_VDA_VLD,
137 138 139
    PIX_FMT_YUV420P,
    PIX_FMT_NONE
};
140

141 142 143 144
const uint8_t *avpriv_mpv_find_start_code(const uint8_t *restrict p,
                                          const uint8_t *end,
                                          uint32_t * restrict state)
{
Michael Niedermayer's avatar
Michael Niedermayer committed
145 146
    int i;

147 148
    assert(p <= end);
    if (p >= end)
Michael Niedermayer's avatar
Michael Niedermayer committed
149 150
        return end;

151 152 153 154
    for (i = 0; i < 3; i++) {
        uint32_t tmp = *state << 8;
        *state = tmp + *(p++);
        if (tmp == 0x100 || p == end)
Michael Niedermayer's avatar
Michael Niedermayer committed
155 156 157
            return p;
    }

158 159 160 161 162
    while (p < end) {
        if      (p[-1] > 1      ) p += 3;
        else if (p[-2]          ) p += 2;
        else if (p[-3]|(p[-1]-1)) p++;
        else {
Michael Niedermayer's avatar
Michael Niedermayer committed
163 164 165 166 167
            p++;
            break;
        }
    }

168 169
    p = FFMIN(p, end) - 4;
    *state = AV_RB32(p);
Michael Niedermayer's avatar
Michael Niedermayer committed
170

171
    return p + 4;
Michael Niedermayer's avatar
Michael Niedermayer committed
172 173
}

174
/* init common dct for both encoder and decoder */
175
av_cold int ff_dct_common_init(MpegEncContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
176
{
177
    ff_dsputil_init(&s->dsp, s->avctx);
178

179 180 181 182 183
    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;
184
    if (s->flags & CODEC_FLAG_BITEXACT)
185
        s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
186
    s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
187

188
#if HAVE_MMX
189
    ff_MPV_common_init_mmx(s);
190
#elif ARCH_ALPHA
191
    ff_MPV_common_init_axp(s);
192
#elif HAVE_MMI
193
    ff_MPV_common_init_mmi(s);
194
#elif ARCH_ARM
195
    ff_MPV_common_init_arm(s);
196
#elif HAVE_ALTIVEC
197
    ff_MPV_common_init_altivec(s);
198
#elif ARCH_BFIN
199
    ff_MPV_common_init_bfin(s);
200
#endif
201

202
    /* load & permutate scantables
203 204 205
     * note: only wmv uses different ones
     */
    if (s->alternate_scan) {
206 207
        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);
208
    } else {
209 210 211
        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
212 213
    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);
214

215 216 217
    return 0;
}

218 219
void ff_copy_picture(Picture *dst, Picture *src)
{
220
    *dst = *src;
221
    dst->f.type = FF_BUFFER_TYPE_COPY;
222 223
}

224
/**
225
 * Release a frame buffer
226 227 228
 */
static void free_frame_buffer(MpegEncContext *s, Picture *pic)
{
229
    /* Windows Media Image codecs allocate internal buffers with different
230 231
     * dimensions; ignore user defined callbacks for these
     */
232
    if (s->codec_id != AV_CODEC_ID_WMV3IMAGE && s->codec_id != AV_CODEC_ID_VC1IMAGE)
233
        ff_thread_release_buffer(s->avctx, &pic->f);
234
    else
235
        avcodec_default_release_buffer(s->avctx, &pic->f);
236
    av_freep(&pic->f.hwaccel_picture_private);
237 238 239
}

/**
240
 * Allocate a frame buffer
241 242 243 244 245
 */
static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
{
    int r;

246
    if (s->avctx->hwaccel) {
247
        assert(!pic->f.hwaccel_picture_private);
248
        if (s->avctx->hwaccel->priv_data_size) {
249 250
            pic->f.hwaccel_picture_private = av_mallocz(s->avctx->hwaccel->priv_data_size);
            if (!pic->f.hwaccel_picture_private) {
251 252 253 254 255 256
                av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
                return -1;
            }
        }
    }

257
    if (s->codec_id != AV_CODEC_ID_WMV3IMAGE && s->codec_id != AV_CODEC_ID_VC1IMAGE)
258
        r = ff_thread_get_buffer(s->avctx, &pic->f);
259
    else
260
        r = avcodec_default_get_buffer(s->avctx, &pic->f);
261

Mans Rullgard's avatar
Mans Rullgard committed
262 263 264
    if (r < 0 || !pic->f.type || !pic->f.data[0]) {
        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %p)\n",
               r, pic->f.type, pic->f.data[0]);
265
        av_freep(&pic->f.hwaccel_picture_private);
266 267 268
        return -1;
    }

269 270 271 272
    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");
273 274 275 276
        free_frame_buffer(s, pic);
        return -1;
    }

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

    return 0;
}

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
287
/**
288
 * Allocate a Picture.
289
 * The pixels are allocated/set by calling get_buffer() if shared = 0
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
290
 */
291 292 293 294 295 296 297 298 299
int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
{
    const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;

    // the + 1 is needed so memset(,,stride*height) does not sig11

    const int mb_array_size = s->mb_stride * s->mb_height;
    const int b8_array_size = s->b8_stride * s->mb_height * 2;
    const int b4_array_size = s->b4_stride * s->mb_height * 4;
Michael Niedermayer's avatar
Michael Niedermayer committed
300
    int i;
301
    int r = -1;
302

303
    if (shared) {
304
        assert(pic->f.data[0]);
305
        assert(pic->f.type == 0 || pic->f.type == FF_BUFFER_TYPE_SHARED);
306
        pic->f.type = FF_BUFFER_TYPE_SHARED;
307
    } else {
308
        assert(!pic->f.data[0]);
309

310
        if (alloc_frame_buffer(s, pic) < 0)
Michael Niedermayer's avatar
Michael Niedermayer committed
311 312
            return -1;

313 314
        s->linesize   = pic->f.linesize[0];
        s->uvlinesize = pic->f.linesize[1];
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
315
    }
316

317
    if (pic->f.qscale_table == NULL) {
318
        if (s->encoding) {
319 320 321 322 323 324
            FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_var,
                              mb_array_size * sizeof(int16_t), fail)
            FF_ALLOCZ_OR_GOTO(s->avctx, pic->mc_mb_var,
                              mb_array_size * sizeof(int16_t), fail)
            FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_mean,
                              mb_array_size * sizeof(int8_t ), fail)
Michael Niedermayer's avatar
Michael Niedermayer committed
325
        }
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
326

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
        FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.mbskip_table,
                          mb_array_size * sizeof(uint8_t) + 2, fail)// the + 2 is for the slice end check
        FF_ALLOCZ_OR_GOTO(s->avctx, pic->qscale_table_base,
                          (big_mb_num + s->mb_stride) * sizeof(uint8_t),
                          fail)
        FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_type_base,
                          (big_mb_num + s->mb_stride) * sizeof(uint32_t),
                          fail)
        pic->f.mb_type = pic->mb_type_base + 2 * s->mb_stride + 1;
        pic->f.qscale_table = pic->qscale_table_base + 2 * s->mb_stride + 1;
        if (s->out_format == FMT_H264) {
            for (i = 0; i < 2; i++) {
                FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i],
                                  2 * (b4_array_size + 4) * sizeof(int16_t),
                                  fail)
342
                pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
343 344
                FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.ref_index[i],
                                  4 * mb_array_size * sizeof(uint8_t), fail)
345
            }
346
            pic->f.motion_subsample_log2 = 2;
347 348 349 350 351 352
        } else if (s->out_format == FMT_H263 || s->encoding ||
                   (s->avctx->debug & FF_DEBUG_MV) || s->avctx->debug_mv) {
            for (i = 0; i < 2; i++) {
                FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i],
                                  2 * (b8_array_size + 4) * sizeof(int16_t),
                                  fail)
353
                pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
354 355
                FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.ref_index[i],
                                  4 * mb_array_size * sizeof(uint8_t), fail)
Michael Niedermayer's avatar
Michael Niedermayer committed
356
            }
357
            pic->f.motion_subsample_log2 = 3;
Michael Niedermayer's avatar
Michael Niedermayer committed
358
        }
359 360 361
        if (s->avctx->debug&FF_DEBUG_DCT_COEFF) {
            FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.dct_coeff,
                              64 * mb_array_size * sizeof(DCTELEM) * 6, fail)
362
        }
363
        pic->f.qstride = s->mb_stride;
364 365
        FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.pan_scan,
                          1 * sizeof(AVPanScan), fail)
Michael Niedermayer's avatar
Michael Niedermayer committed
366
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
367

368
    pic->owner2 = s;
369

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
370
    return 0;
371 372
fail: // for  the FF_ALLOCZ_OR_GOTO macro
    if (r >= 0)
373
        free_frame_buffer(s, pic);
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
374 375 376
    return -1;
}

Michael Niedermayer's avatar
Michael Niedermayer committed
377
/**
378
 * Deallocate a picture.
Michael Niedermayer's avatar
Michael Niedermayer committed
379
 */
380 381
static void free_picture(MpegEncContext *s, Picture *pic)
{
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
382
    int i;
Michael Niedermayer's avatar
Michael Niedermayer committed
383

384
    if (pic->f.data[0] && pic->f.type != FF_BUFFER_TYPE_SHARED) {
385
        free_frame_buffer(s, pic);
Michael Niedermayer's avatar
Michael Niedermayer committed
386 387
    }

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
388 389 390
    av_freep(&pic->mb_var);
    av_freep(&pic->mc_mb_var);
    av_freep(&pic->mb_mean);
391
    av_freep(&pic->f.mbskip_table);
392
    av_freep(&pic->qscale_table_base);
Michael Niedermayer's avatar
Michael Niedermayer committed
393
    av_freep(&pic->mb_type_base);
394 395 396
    av_freep(&pic->f.dct_coeff);
    av_freep(&pic->f.pan_scan);
    pic->f.mb_type = NULL;
397
    for (i = 0; i < 2; i++) {
398
        av_freep(&pic->motion_val_base[i]);
399
        av_freep(&pic->f.ref_index[i]);
Michael Niedermayer's avatar
Michael Niedermayer committed
400
    }
401

402
    if (pic->f.type == FF_BUFFER_TYPE_SHARED) {
403
        for (i = 0; i < 4; i++) {
404 405
            pic->f.base[i] =
            pic->f.data[i] = NULL;
Michael Niedermayer's avatar
Michael Niedermayer committed
406
        }
407
        pic->f.type = 0;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
408 409 410
    }
}

411 412
static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base)
{
413 414 415
    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;
416 417
    int i;

418 419 420 421 422 423 424 425 426 427 428 429 430
    // edge emu needs blocksize + filter length - 1
    // (= 17x17 for  halfpel / 21x21 for  h264)
    FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer,
                      (s->width + 64) * 2 * 21 * 2, fail);    // (width + edge + align)*interlaced*MBsize*tolerance

    // FIXME should be linesize instead of s->width * 2
    // but that is not known before get_buffer()
    FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad,
                      (s->width + 64) * 4 * 16 * 2 * sizeof(uint8_t), 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;
431
    if (s->encoding) {
432 433 434 435 436 437 438
        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)
439
        }
440
    }
441 442
    FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(DCTELEM), fail)
    s->block = s->blocks[0];
443

444
    for (i = 0; i < 12; i++) {
445
        s->pblocks[i] = &s->block[i];
446
    }
447

448 449
    if (s->out_format == FMT_H263) {
        /* ac values */
450 451
        FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
                          yc_size * sizeof(int16_t) * 16, fail);
452 453 454 455 456
        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;
    }

457 458
    return 0;
fail:
459
    return -1; // free() through ff_MPV_common_end()
460 461
}

462 463 464 465
static void free_duplicate_context(MpegEncContext *s)
{
    if (s == NULL)
        return;
466

467
    av_freep(&s->edge_emu_buffer);
468
    av_freep(&s->me.scratchpad);
469 470 471 472
    s->me.temp =
    s->rd_scratchpad =
    s->b_scratchpad =
    s->obmc_scratchpad = NULL;
473

474 475 476 477
    av_freep(&s->dct_error_sum);
    av_freep(&s->me.map);
    av_freep(&s->me.score_map);
    av_freep(&s->blocks);
478
    av_freep(&s->ac_val_base);
479
    s->block = NULL;
480 481
}

482 483 484
static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
{
#define COPY(a) bak->a = src->a
485 486
    COPY(edge_emu_buffer);
    COPY(me.scratchpad);
487
    COPY(me.temp);
488 489 490 491 492 493 494 495 496 497 498 499
    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);
500 501
    COPY(dct_count[0]);
    COPY(dct_count[1]);
502 503 504 505
    COPY(ac_val_base);
    COPY(ac_val[0]);
    COPY(ac_val[1]);
    COPY(ac_val[2]);
506 507 508
#undef COPY
}

509 510
void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
{
511
    MpegEncContext bak;
512
    int i;
513 514
    // FIXME copy only needed parts
    // START_TIMER
515 516 517
    backup_duplicate_context(&bak, dst);
    memcpy(dst, src, sizeof(MpegEncContext));
    backup_duplicate_context(dst, &bak);
518
    for (i = 0; i < 12; i++) {
519
        dst->pblocks[i] = &dst->block[i];
520
    }
521 522
    // STOP_TIMER("update_duplicate_context")
    // about 10k cycles / 0.01 sec for  1000frames on 1ghz with 2 threads
523 524
}

525 526
int ff_mpeg_update_thread_context(AVCodecContext *dst,
                                  const AVCodecContext *src)
527 528 529
{
    MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;

530 531
    if (dst == src || !s1->context_initialized)
        return 0;
532

533 534 535
    // FIXME can parameters change on I-frames?
    // in that case dst may need a reinit
    if (!s->context_initialized) {
536 537 538 539 540 541 542 543
        memcpy(s, s1, sizeof(MpegEncContext));

        s->avctx                 = dst;
        s->picture_range_start  += MAX_PICTURE_COUNT;
        s->picture_range_end    += MAX_PICTURE_COUNT;
        s->bitstream_buffer      = NULL;
        s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;

544
        ff_MPV_common_init(s);
545 546 547 548 549 550 551 552 553 554 555 556
    }

    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;

    memcpy(s->picture, s1->picture, s1->picture_count * sizeof(Picture));
557 558
    memcpy(&s->last_picture, &s1->last_picture,
           (char *) &s1->last_picture_ptr - (char *) &s1->last_picture);
559

560 561 562
    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);
563

564
    // Error/bug resilience
565 566 567
    s->next_p_frame_damaged = s1->next_p_frame_damaged;
    s->workaround_bugs      = s1->workaround_bugs;

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
    // 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;
    s->dropable     = s1->dropable;

    // 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);
591 592
    }

593 594 595
    // MPEG2/interlacing info
    memcpy(&s->progressive_sequence, &s1->progressive_sequence,
           (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
596

597 598 599 600
    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;
601

602
        if (s1->pict_type != AV_PICTURE_TYPE_B) {
603
            s->last_non_b_pict_type = s1->pict_type;
604 605 606 607 608 609
        }
    }

    return 0;
}

Michael Niedermayer's avatar
Michael Niedermayer committed
610
/**
611
 * Set the given MpegEncContext to common defaults
612
 * (same for encoding and decoding).
613
 * The changed fields will not depend upon the
614
 * prior state of the MpegEncContext.
Michael Niedermayer's avatar
Michael Niedermayer committed
615
 */
616
void ff_MPV_common_defaults(MpegEncContext *s)
617 618 619 620 621 622 623 624 625 626 627
{
    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
628 629

    s->picture_in_gop_number = 0;
630

631 632
    s->f_code                = 1;
    s->b_code                = 1;
633

634 635
    s->picture_range_start   = 0;
    s->picture_range_end     = MAX_PICTURE_COUNT;
636 637

    s->slice_context_count   = 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
638 639 640
}

/**
641
 * Set the given MpegEncContext to defaults for decoding.
642 643
 * the changed fields will not depend upon
 * the prior state of the MpegEncContext.
Michael Niedermayer's avatar
Michael Niedermayer committed
644
 */
645
void ff_MPV_decode_defaults(MpegEncContext *s)
646
{
647
    ff_MPV_common_defaults(s);
Michael Niedermayer's avatar
Michael Niedermayer committed
648 649
}

650
/**
Michael Niedermayer's avatar
Michael Niedermayer committed
651 652 653
 * init common structure for both encoder and decoder.
 * this assumes that some variables like width/height are already set
 */
654
av_cold int ff_MPV_common_init(MpegEncContext *s)
655
{
656 657 658 659 660 661 662
    int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
    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;
663

664
    if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
665
        s->mb_height = (s->height + 31) / 32 * 2;
666
    else if (s->codec_id != AV_CODEC_ID_H264)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
667
        s->mb_height = (s->height + 15) / 16;
668

669 670 671
    if (s->avctx->pix_fmt == PIX_FMT_NONE) {
        av_log(s->avctx, AV_LOG_ERROR,
               "decoding to PIX_FMT_NONE is not supported.\n");
672 673 674
        return -1;
    }

675 676 677 678 679 680 681 682 683
    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
10l  
Michael Niedermayer committed
684 685
    }

686 687
    if ((s->width || s->height) &&
        av_image_check_size(s->width, s->height, 0, s->avctx))
688 689
        return -1;

690
    ff_dct_common_init(s);
691

692 693
    s->flags  = s->avctx->flags;
    s->flags2 = s->avctx->flags2;
694

695
    if (s->width && s->height) {
696 697 698 699 700 701
        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;
702

Ronald S. Bultje's avatar
Ronald S. Bultje committed
703
        /* set chroma shifts */
704 705
        avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &s->chroma_x_shift,
                                      &s->chroma_y_shift);
706

707 708 709 710
        /* 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;
711

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

714 715 716 717 718 719
        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;
720

721 722 723
        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;
724

Ronald S. Bultje's avatar
Ronald S. Bultje committed
725
        /* convert fourcc to upper case */
726
        s->codec_tag          = avpriv_toupper4(s->avctx->codec_tag);
727

728
        s->stream_codec_tag   = avpriv_toupper4(s->avctx->stream_codec_tag);
729

730
        s->avctx->coded_frame = &s->current_picture.f;
731

732 733 734 735 736 737 738 739
        FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
                          fail); // error ressilience code looks cleaner with this
        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;

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

Ronald S. Bultje's avatar
Ronald S. Bultje committed
741 742
        if (s->encoding) {
            /* Allocate MV tables */
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
            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);
            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;
            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;
            s->b_direct_mv_table     = s->b_direct_mv_table_base +
                                       s->mb_stride + 1;

            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);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
772 773
            }
            FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
774

Ronald S. Bultje's avatar
Ronald S. Bultje committed
775
            /* Allocate MB type table */
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
            FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
                              sizeof(uint16_t), fail); // needed for encoding

            FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
                              sizeof(int), fail);

            FF_ALLOCZ_OR_GOTO(s->avctx, s->q_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_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);

            if (s->avctx->noise_reduction) {
                FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,
                                  2 * 64 * sizeof(uint16_t), fail);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
798
            }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
799 800 801 802 803

            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);
804
        }
805
    }
806

807
    s->picture_count = MAX_PICTURE_COUNT * FFMAX(1, s->avctx->thread_count);
808 809 810
    FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
                      s->picture_count * sizeof(Picture), fail);
    for (i = 0; i < s->picture_count; i++) {
811
        avcodec_get_frame_defaults(&s->picture[i].f);
812
    }
813

814
    if (s->width && s->height) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
815 816
        FF_ALLOC_OR_GOTO(s->avctx, s->er_temp_buffer,
                         mb_array_size * sizeof(uint8_t), fail);
817 818
        FF_ALLOCZ_OR_GOTO(s->avctx, s->error_status_table,
                          mb_array_size * sizeof(uint8_t), fail);
819

820
        if (s->codec_id == AV_CODEC_ID_MPEG4 ||
821
            (s->flags & CODEC_FLAG_INTERLACED_ME)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
822
            /* interlaced direct mode decoding tables */