mpegvideo.c 109 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"
Benoit Fouet's avatar
Benoit Fouet committed
36
#include "mpegvideo_common.h"
37
#include "mjpegenc.h"
38
#include "msmpeg4.h"
Michael Niedermayer's avatar
Michael Niedermayer committed
39
#include "faandct.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,
48
                                   DCTELEM *block, int n, int qscale);
49
static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
50
                                   DCTELEM *block, int n, int qscale);
51 52
static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
                                   DCTELEM *block, int n, int qscale);
53 54
static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
                                   DCTELEM *block, int n, int qscale);
55 56
static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
                                   DCTELEM *block, int n, int qscale);
57
static void dct_unquantize_h263_intra_c(MpegEncContext *s,
58
                                  DCTELEM *block, int n, int qscale);
59
static void dct_unquantize_h263_inter_c(MpegEncContext *s,
60
                                  DCTELEM *block, int n, int qscale);
Michael Niedermayer's avatar
Michael Niedermayer committed
61

Fabrice Bellard's avatar
Fabrice Bellard committed
62 63 64 65 66 67

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

//#define DEBUG

68

69 70 71 72
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
73 74
};

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

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

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

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

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

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

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

143 144 145 146
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
147 148
    int i;

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

153 154 155 156
    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
157 158 159
            return p;
    }

160 161 162 163 164
    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
165 166 167 168 169
            p++;
            break;
        }
    }

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

173
    return p + 4;
Michael Niedermayer's avatar
Michael Niedermayer committed
174 175
}

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

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

190
#if HAVE_MMX
191
    MPV_common_init_mmx(s);
192
#elif ARCH_ALPHA
193
    MPV_common_init_axp(s);
194
#elif HAVE_MMI
195
    MPV_common_init_mmi(s);
196
#elif ARCH_ARM
197
    MPV_common_init_arm(s);
198
#elif HAVE_ALTIVEC
199
    MPV_common_init_altivec(s);
200
#elif ARCH_BFIN
201 202
    MPV_common_init_bfin(s);
#endif
203

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

217 218 219
    return 0;
}

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

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

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

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

259
    if (s->codec_id != CODEC_ID_WMV3IMAGE && s->codec_id != CODEC_ID_VC1IMAGE)
260
        r = ff_thread_get_buffer(s->avctx, (AVFrame *) pic);
261
    else
262
        r = avcodec_default_get_buffer(s->avctx, (AVFrame *) pic);
263

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

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

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

    return 0;
}

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

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

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

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

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

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

370
    pic->owner2 = s;
371

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

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

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

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

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

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

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

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

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

459 460
    return 0;
fail:
461
    return -1; // free() through MPV_common_end()
462 463
}

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

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

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

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

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

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

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

535 536 537
    // FIXME can parameters change on I-frames?
    // in that case dst may need a reinit
    if (!s->context_initialized) {
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
        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;

        MPV_common_init(s);
    }

    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));
559 560
    memcpy(&s->last_picture, &s1->last_picture,
           (char *) &s1->last_picture_ptr - (char *) &s1->last_picture);
561

562 563 564
    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);
565

566
    // Error/bug resilience
567 568 569
    s->next_p_frame_damaged = s1->next_p_frame_damaged;
    s->workaround_bugs      = s1->workaround_bugs;

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

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

599 600 601 602
    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;
603

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

    return 0;
}

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

    s->picture_in_gop_number = 0;
632

633 634
    s->f_code                = 1;
    s->b_code                = 1;
635

636 637
    s->picture_range_start   = 0;
    s->picture_range_end     = MAX_PICTURE_COUNT;
638 639

    s->slice_context_count   = 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
640 641 642
}

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

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

666
    if (s->codec_id == CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
667
        s->mb_height = (s->height + 31) / 32 * 2;
668
    else if (s->codec_id != CODEC_ID_H264)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
669
        s->mb_height = (s->height + 15) / 16;
670

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

677 678 679 680 681 682 683 684 685
    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
686 687
    }

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

692
    ff_dct_common_init(s);
693

694 695
    s->flags  = s->avctx->flags;
    s->flags2 = s->avctx->flags2;
696

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

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

709 710 711 712
        /* 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;
713

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

716 717 718 719 720 721
        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;
722

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

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

730
        s->stream_codec_tag   = avpriv_toupper4(s->avctx->stream_codec_tag);
731

732
        s->avctx->coded_frame = (AVFrame *)&s->current_picture;
733

734 735 736 737 738 739 740 741
        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?
742

Ronald S. Bultje's avatar
Ronald S. Bultje committed
743 744
        if (s->encoding) {
            /* Allocate MV tables */
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 772 773
            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
774 775
            }
            FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
776

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

804
    s->picture_count = MAX_PICTURE_COUNT * FFMAX(1, s->avctx->thread_count);
805