mpegvideo.c 94.7 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 189
#if ARCH_X86
    ff_MPV_common_init_x86(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)
{
Alberto Delmás's avatar
Alberto Delmás committed
229 230 231 232 233
    /* 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)
234
        ff_thread_release_buffer(s->avctx, &pic->f);
235
    else
236
        avcodec_default_release_buffer(s->avctx, &pic->f);
237
    av_freep(&pic->f.hwaccel_picture_private);
238 239 240
}

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

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

Alberto Delmás's avatar
Alberto Delmás committed
258 259 260
    if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
        s->codec_id != AV_CODEC_ID_VC1IMAGE  &&
        s->codec_id != AV_CODEC_ID_MSS2)
261
        r = ff_thread_get_buffer(s->avctx, &pic->f);
262
    else
263
        r = avcodec_default_get_buffer(s->avctx, &pic->f);
264

Mans Rullgard's avatar
Mans Rullgard committed
265 266 267
    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]);
268
        av_freep(&pic->f.hwaccel_picture_private);
269 270 271
        return -1;
    }

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

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

    return 0;
}

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
290
/**
291
 * Allocate a Picture.
292
 * The pixels are allocated/set by calling get_buffer() if shared = 0
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
293
 */
294 295 296 297 298 299 300 301 302
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
303
    int i;
304
    int r = -1;
305

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

313
        if (alloc_frame_buffer(s, pic) < 0)
Michael Niedermayer's avatar
Michael Niedermayer committed
314 315
            return -1;

316 317
        s->linesize   = pic->f.linesize[0];
        s->uvlinesize = pic->f.linesize[1];
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
318
    }
319

320
    if (pic->f.qscale_table == NULL) {
321
        if (s->encoding) {
322 323 324 325 326 327
            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
328
        }
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
329

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
        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)
345
                pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
346 347
                FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.ref_index[i],
                                  4 * mb_array_size * sizeof(uint8_t), fail)
348
            }
349
            pic->f.motion_subsample_log2 = 2;
350 351 352 353 354 355
        } 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)
356
                pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
357 358
                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
359
            }
360
            pic->f.motion_subsample_log2 = 3;
Michael Niedermayer's avatar
Michael Niedermayer committed
361
        }
362 363 364
        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)
365
        }
366
        pic->f.qstride = s->mb_stride;
367 368
        FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.pan_scan,
                          1 * sizeof(AVPanScan), fail)
Michael Niedermayer's avatar
Michael Niedermayer committed
369
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
370

371
    pic->owner2 = s;
372

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

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

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

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

408
    if (pic->f.type == FF_BUFFER_TYPE_SHARED) {
409
        for (i = 0; i < 4; i++) {
410 411
            pic->f.base[i] =
            pic->f.data[i] = NULL;
Michael Niedermayer's avatar
Michael Niedermayer committed
412
        }
413
        pic->f.type = 0;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
414 415 416
    }
}

417 418
static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base)
{
419 420 421
    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;
422 423
    int i;

424 425 426 427 428 429 430 431 432 433 434 435 436
    // 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;
437
    if (s->encoding) {
438 439 440 441 442 443 444
        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)
445
        }
446
    }
447 448
    FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(DCTELEM), fail)
    s->block = s->blocks[0];
449

450
    for (i = 0; i < 12; i++) {
451
        s->pblocks[i] = &s->block[i];
452
    }
453

454 455
    if (s->out_format == FMT_H263) {
        /* ac values */
456 457
        FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
                          yc_size * sizeof(int16_t) * 16, fail);
458 459 460 461 462
        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;
    }

463 464
    return 0;
fail:
465
    return -1; // free() through ff_MPV_common_end()
466 467
}

468 469 470 471
static void free_duplicate_context(MpegEncContext *s)
{
    if (s == NULL)
        return;
472

473
    av_freep(&s->edge_emu_buffer);
474
    av_freep(&s->me.scratchpad);
475 476 477 478
    s->me.temp =
    s->rd_scratchpad =
    s->b_scratchpad =
    s->obmc_scratchpad = NULL;
479

480 481 482 483
    av_freep(&s->dct_error_sum);
    av_freep(&s->me.map);
    av_freep(&s->me.score_map);
    av_freep(&s->blocks);
484
    av_freep(&s->ac_val_base);
485
    s->block = NULL;
486 487
}

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

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

531 532
int ff_mpeg_update_thread_context(AVCodecContext *dst,
                                  const AVCodecContext *src)
533 534 535
{
    MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;

536 537
    if (dst == src || !s1->context_initialized)
        return 0;
538

539 540 541
    // FIXME can parameters change on I-frames?
    // in that case dst may need a reinit
    if (!s->context_initialized) {
542 543 544 545 546 547 548 549
        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;

550
        ff_MPV_common_init(s);
551 552 553 554 555 556 557 558 559 560 561 562
    }

    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));
563 564
    memcpy(&s->last_picture, &s1->last_picture,
           (char *) &s1->last_picture_ptr - (char *) &s1->last_picture);
565

566 567 568
    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);
569

570
    // Error/bug resilience
571 572 573
    s->next_p_frame_damaged = s1->next_p_frame_damaged;
    s->workaround_bugs      = s1->workaround_bugs;

574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
    // 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);
597 598
    }

599 600 601
    // MPEG2/interlacing info
    memcpy(&s->progressive_sequence, &s1->progressive_sequence,
           (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
602

603 604 605 606
    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;
607

608
        if (s1->pict_type != AV_PICTURE_TYPE_B) {
609
            s->last_non_b_pict_type = s1->pict_type;
610 611 612 613 614 615
        }
    }

    return 0;
}

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

    s->picture_in_gop_number = 0;
636

637 638
    s->f_code                = 1;
    s->b_code                = 1;
639

640 641
    s->picture_range_start   = 0;
    s->picture_range_end     = MAX_PICTURE_COUNT;
642 643

    s->slice_context_count   = 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
644 645 646
}

/**
647
 * Set the given MpegEncContext to defaults for decoding.
648 649
 * the changed fields will not depend upon
 * the prior state of the MpegEncContext.
Michael Niedermayer's avatar
Michael Niedermayer committed
650
 */
651
void ff_MPV_decode_defaults(MpegEncContext *s)
652
{
653
    ff_MPV_common_defaults(s);
Michael Niedermayer's avatar
Michael Niedermayer committed
654 655
}

656
/**
Michael Niedermayer's avatar
Michael Niedermayer committed
657 658 659
 * init common structure for both encoder and decoder.
 * this assumes that some variables like width/height are already set
 */
660
av_cold int ff_MPV_common_init(MpegEncContext *s)
661
{
662 663 664 665 666 667 668
    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;
669

670
    if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
671
        s->mb_height = (s->height + 31) / 32 * 2;
672
    else if (s->codec_id != AV_CODEC_ID_H264)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
673
        s->mb_height = (s->height + 15) / 16;
674

675 676 677
    if (s->avctx->pix_fmt == PIX_FMT_NONE) {
        av_log(s->avctx, AV_LOG_ERROR,
               "decoding to PIX_FMT_NONE is not supported.\n");
678 679 680
        return -1;
    }

681 682 683 684 685 686 687 688 689
    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
690 691
    }

692 693
    if ((s->width || s->height) &&
        av_image_check_size(s->width, s->height, 0, s->avctx))
694 695
        return -1;

696
    ff_dct_common_init(s);
697

698 699
    s->flags  = s->avctx->flags;
    s->flags2 = s->avctx->flags2;
700

701
    if (s->width && s->height) {
702 703 704 705 706 707
        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;
708

Ronald S. Bultje's avatar
Ronald S. Bultje committed
709
        /* set chroma shifts */
710 711
        avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &s->chroma_x_shift,
                                      &s->chroma_y_shift);
712

713 714 715 716
        /* 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;
717

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

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

727 728 729
        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;
730

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

734
        s->stream_codec_tag   = avpriv_toupper4(s->avctx->stream_codec_tag);
735

736
        s->avctx->coded_frame = &s->current_picture.f;
737

738 739 740 741 742 743 744 745
        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?
746

Ronald S. Bultje's avatar
Ronald S. Bultje committed
747 748
        if (s->encoding) {
            /* Allocate MV tables */
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
            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
778 779
            }
            FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
780

Ronald S. Bultje's avatar
Ronald S. Bultje committed
781
            /* Allocate MB type table */
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
            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
804
            }
Ronald S. Bultje's avatar
Ronald S. Bultje committed