mpegvideo.c 100 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/imgutils.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
31 32
#include "avcodec.h"
#include "dsputil.h"
33
#include "internal.h"
34
#include "mathops.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,
Diego Biurrun's avatar
Diego Biurrun committed
46
                                   int16_t *block, int n, int qscale);
47
static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
48
                                   int16_t *block, int n, int qscale);
49
static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
50
                                   int16_t *block, int n, int qscale);
51
static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
52
                                   int16_t *block, int n, int qscale);
53
static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
54
                                   int16_t *block, int n, int qscale);
55
static void dct_unquantize_h263_intra_c(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
56
                                  int16_t *block, int n, int qscale);
57
static void dct_unquantize_h263_inter_c(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
58
                                  int16_t *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 129 130
const enum AVPixelFormat ff_pixfmt_list_420[] = {
    AV_PIX_FMT_YUV420P,
    AV_PIX_FMT_NONE
131 132
};

133
const enum AVPixelFormat ff_hwaccel_pixfmt_list_420[] = {
134
#if CONFIG_DXVA2
135
    AV_PIX_FMT_DXVA2_VLD,
136 137
#endif
#if CONFIG_VAAPI
138
    AV_PIX_FMT_VAAPI_VLD,
139 140
#endif
#if CONFIG_VDA
141
    AV_PIX_FMT_VDA_VLD,
142 143
#endif
#if CONFIG_VDPAU
144
    AV_PIX_FMT_VDPAU,
145
#endif
146 147
    AV_PIX_FMT_YUV420P,
    AV_PIX_FMT_NONE
148
};
149

150 151 152 153
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
154 155
    int i;

156 157
    assert(p <= end);
    if (p >= end)
Michael Niedermayer's avatar
Michael Niedermayer committed
158 159
        return end;

160 161 162 163
    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
164 165 166
            return p;
    }

167 168 169 170 171
    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
172 173 174 175 176
            p++;
            break;
        }
    }

177 178
    p = FFMIN(p, end) - 4;
    *state = AV_RB32(p);
Michael Niedermayer's avatar
Michael Niedermayer committed
179

180
    return p + 4;
Michael Niedermayer's avatar
Michael Niedermayer committed
181 182
}

183
/* init common dct for both encoder and decoder */
184
av_cold int ff_dct_common_init(MpegEncContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
185
{
186
    ff_dsputil_init(&s->dsp, s->avctx);
187
    ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
188

189 190 191 192 193
    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;
194
    if (s->flags & CODEC_FLAG_BITEXACT)
195
        s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
196
    s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
197

198 199
#if ARCH_X86
    ff_MPV_common_init_x86(s);
200
#elif ARCH_ALPHA
201
    ff_MPV_common_init_axp(s);
202
#elif ARCH_ARM
203
    ff_MPV_common_init_arm(s);
204
#elif HAVE_ALTIVEC
205
    ff_MPV_common_init_altivec(s);
206
#elif ARCH_BFIN
207
    ff_MPV_common_init_bfin(s);
208
#endif
209

210
    /* load & permutate scantables
211 212 213
     * note: only wmv uses different ones
     */
    if (s->alternate_scan) {
214 215
        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);
216
    } else {
217 218 219
        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
220 221
    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);
222

223 224 225
    return 0;
}

226 227
void ff_copy_picture(Picture *dst, Picture *src)
{
228
    *dst = *src;
229
    dst->f.type = FF_BUFFER_TYPE_COPY;
230 231
}

232
/**
233
 * Release a frame buffer
234 235 236
 */
static void free_frame_buffer(MpegEncContext *s, Picture *pic)
{
Alberto Delmás's avatar
Alberto Delmás committed
237 238 239 240 241
    /* 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)
242
        ff_thread_release_buffer(s->avctx, &pic->f);
243
    else
244
        avcodec_default_release_buffer(s->avctx, &pic->f);
245
    av_freep(&pic->f.hwaccel_picture_private);
246 247
}

248 249 250 251 252 253
int ff_mpv_frame_size_alloc(MpegEncContext *s, int linesize)
{
    int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);

    // edge emu needs blocksize + filter length - 1
    // (= 17x17 for  halfpel / 21x21 for  h264)
254 255
    // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
    // at uvlinesize. It supports only YUV420 so 24x24 is enough
256
    // linesize * interlaced * MBsize
257
    FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 2 * 24,
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
                      fail);

    FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 2 * 16 * 2,
                      fail)
    s->me.temp         = s->me.scratchpad;
    s->rd_scratchpad   = s->me.scratchpad;
    s->b_scratchpad    = s->me.scratchpad;
    s->obmc_scratchpad = s->me.scratchpad + 16;

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

273
/**
274
 * Allocate a frame buffer
275 276 277
 */
static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
{
278
    int r, ret;
279

280
    if (s->avctx->hwaccel) {
281
        assert(!pic->f.hwaccel_picture_private);
282
        if (s->avctx->hwaccel->priv_data_size) {
283 284
            pic->f.hwaccel_picture_private = av_mallocz(s->avctx->hwaccel->priv_data_size);
            if (!pic->f.hwaccel_picture_private) {
285 286 287 288 289 290
                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
291 292 293
    if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
        s->codec_id != AV_CODEC_ID_VC1IMAGE  &&
        s->codec_id != AV_CODEC_ID_MSS2)
294
        r = ff_thread_get_buffer(s->avctx, &pic->f);
295
    else
296
        r = avcodec_default_get_buffer(s->avctx, &pic->f);
297

Mans Rullgard's avatar
Mans Rullgard committed
298 299 300
    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]);
301
        av_freep(&pic->f.hwaccel_picture_private);
302 303 304
        return -1;
    }

305 306 307 308
    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");
309 310 311 312
        free_frame_buffer(s, pic);
        return -1;
    }

313
    if (pic->f.linesize[1] != pic->f.linesize[2]) {
314 315
        av_log(s->avctx, AV_LOG_ERROR,
               "get_buffer() failed (uv stride mismatch)\n");
316 317 318 319
        free_frame_buffer(s, pic);
        return -1;
    }

320 321 322 323 324 325 326 327
    if (!s->edge_emu_buffer &&
        (ret = ff_mpv_frame_size_alloc(s, pic->f.linesize[0])) < 0) {
        av_log(s->avctx, AV_LOG_ERROR,
               "get_buffer() failed to allocate context scratch buffers.\n");
        free_frame_buffer(s, pic);
        return ret;
    }

328 329 330
    return 0;
}

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
331
/**
332
 * Allocate a Picture.
333
 * The pixels are allocated/set by calling get_buffer() if shared = 0
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
334
 */
335 336 337 338 339 340 341 342 343
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
344
    int i;
345
    int r = -1;
346

347
    if (shared) {
348
        assert(pic->f.data[0]);
349
        assert(pic->f.type == 0 || pic->f.type == FF_BUFFER_TYPE_SHARED);
350
        pic->f.type = FF_BUFFER_TYPE_SHARED;
351
    } else {
352
        assert(!pic->f.data[0]);
353

354
        if (alloc_frame_buffer(s, pic) < 0)
Michael Niedermayer's avatar
Michael Niedermayer committed
355 356
            return -1;

357 358
        s->linesize   = pic->f.linesize[0];
        s->uvlinesize = pic->f.linesize[1];
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
359
    }
360

361
    if (pic->f.qscale_table == NULL) {
362
        if (s->encoding) {
363 364 365 366 367 368
            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
369
        }
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
370

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
        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)
386
                pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
387 388
                FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.ref_index[i],
                                  4 * mb_array_size * sizeof(uint8_t), fail)
389
            }
390
            pic->f.motion_subsample_log2 = 2;
391 392 393 394 395 396
        } 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)
397
                pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
398 399
                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
400
            }
401
            pic->f.motion_subsample_log2 = 3;
Michael Niedermayer's avatar
Michael Niedermayer committed
402
        }
403 404
        if (s->avctx->debug&FF_DEBUG_DCT_COEFF) {
            FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.dct_coeff,
Diego Biurrun's avatar
Diego Biurrun committed
405
                              64 * mb_array_size * sizeof(int16_t) * 6, fail)
406
        }
407
        pic->f.qstride = s->mb_stride;
408 409
        FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.pan_scan,
                          1 * sizeof(AVPanScan), fail)
Michael Niedermayer's avatar
Michael Niedermayer committed
410
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
411

412
    pic->owner2 = s;
413

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
414
    return 0;
415 416
fail: // for  the FF_ALLOCZ_OR_GOTO macro
    if (r >= 0)
417
        free_frame_buffer(s, pic);
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
418 419 420
    return -1;
}

Michael Niedermayer's avatar
Michael Niedermayer committed
421
/**
422
 * Deallocate a picture.
Michael Niedermayer's avatar
Michael Niedermayer committed
423
 */
424 425
static void free_picture(MpegEncContext *s, Picture *pic)
{
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
426
    int i;
Michael Niedermayer's avatar
Michael Niedermayer committed
427

428
    if (pic->f.data[0] && pic->f.type != FF_BUFFER_TYPE_SHARED) {
429
        free_frame_buffer(s, pic);
Michael Niedermayer's avatar
Michael Niedermayer committed
430 431
    }

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
432 433 434
    av_freep(&pic->mb_var);
    av_freep(&pic->mc_mb_var);
    av_freep(&pic->mb_mean);
435
    av_freep(&pic->f.mbskip_table);
436
    av_freep(&pic->qscale_table_base);
437
    pic->f.qscale_table = NULL;
Michael Niedermayer's avatar
Michael Niedermayer committed
438
    av_freep(&pic->mb_type_base);
439
    pic->f.mb_type = NULL;
440 441 442
    av_freep(&pic->f.dct_coeff);
    av_freep(&pic->f.pan_scan);
    pic->f.mb_type = NULL;
443
    for (i = 0; i < 2; i++) {
444
        av_freep(&pic->motion_val_base[i]);
445
        av_freep(&pic->f.ref_index[i]);
446
        pic->f.motion_val[i] = NULL;
Michael Niedermayer's avatar
Michael Niedermayer committed
447
    }
448

449
    if (pic->f.type == FF_BUFFER_TYPE_SHARED) {
450
        for (i = 0; i < 4; i++) {
451 452
            pic->f.base[i] =
            pic->f.data[i] = NULL;
Michael Niedermayer's avatar
Michael Niedermayer committed
453
        }
454
        pic->f.type = 0;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
455 456 457
    }
}

458 459
static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base)
{
460 461 462
    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;
463 464
    int i;

465 466 467 468 469 470
    s->edge_emu_buffer =
    s->me.scratchpad   =
    s->me.temp         =
    s->rd_scratchpad   =
    s->b_scratchpad    =
    s->obmc_scratchpad = NULL;
471

472
    if (s->encoding) {
473 474 475 476 477 478 479
        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)
480
        }
481
    }
Diego Biurrun's avatar
Diego Biurrun committed
482
    FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
483
    s->block = s->blocks[0];
484

485
    for (i = 0; i < 12; i++) {
486
        s->pblocks[i] = &s->block[i];
487
    }
488

489 490
    if (s->out_format == FMT_H263) {
        /* ac values */
491 492
        FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
                          yc_size * sizeof(int16_t) * 16, fail);
493 494 495 496 497
        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;
    }

498 499
    return 0;
fail:
500
    return -1; // free() through ff_MPV_common_end()
501 502
}

503 504 505 506
static void free_duplicate_context(MpegEncContext *s)
{
    if (s == NULL)
        return;
507

508
    av_freep(&s->edge_emu_buffer);
509
    av_freep(&s->me.scratchpad);
510 511 512 513
    s->me.temp =
    s->rd_scratchpad =
    s->b_scratchpad =
    s->obmc_scratchpad = NULL;
514

515 516 517 518
    av_freep(&s->dct_error_sum);
    av_freep(&s->me.map);
    av_freep(&s->me.score_map);
    av_freep(&s->blocks);
519
    av_freep(&s->ac_val_base);
520
    s->block = NULL;
521 522
}

523 524 525
static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
{
#define COPY(a) bak->a = src->a
526 527
    COPY(edge_emu_buffer);
    COPY(me.scratchpad);
528
    COPY(me.temp);
529 530 531 532 533 534 535 536 537 538 539 540
    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);
541 542
    COPY(dct_count[0]);
    COPY(dct_count[1]);
543 544 545 546
    COPY(ac_val_base);
    COPY(ac_val[0]);
    COPY(ac_val[1]);
    COPY(ac_val[2]);
547 548 549
#undef COPY
}

550
int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
551
{
552
    MpegEncContext bak;
553
    int i, ret;
554 555
    // FIXME copy only needed parts
    // START_TIMER
556 557 558
    backup_duplicate_context(&bak, dst);
    memcpy(dst, src, sizeof(MpegEncContext));
    backup_duplicate_context(dst, &bak);
559
    for (i = 0; i < 12; i++) {
560
        dst->pblocks[i] = &dst->block[i];
561
    }
562 563 564 565 566 567
    if (!dst->edge_emu_buffer &&
        (ret = ff_mpv_frame_size_alloc(dst, dst->linesize)) < 0) {
        av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
               "scratch buffers.\n");
        return ret;
    }
568 569
    // STOP_TIMER("update_duplicate_context")
    // about 10k cycles / 0.01 sec for  1000frames on 1ghz with 2 threads
570
    return 0;
571 572
}

573 574
int ff_mpeg_update_thread_context(AVCodecContext *dst,
                                  const AVCodecContext *src)
575
{
576
    int i;
577 578
    MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;

579 580
    if (dst == src || !s1->context_initialized)
        return 0;
581

582 583 584
    // FIXME can parameters change on I-frames?
    // in that case dst may need a reinit
    if (!s->context_initialized) {
585 586 587 588 589 590 591 592
        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;

593
        ff_MPV_common_init(s);
594 595
    }

596 597 598 599 600 601 602 603 604
    if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
        int err;
        s->context_reinit = 0;
        s->height = s1->height;
        s->width  = s1->width;
        if ((err = ff_MPV_common_frame_size_change(s)) < 0)
            return err;
    }

605 606 607 608 609 610 611 612 613 614
    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));
615 616
    memcpy(&s->last_picture, &s1->last_picture,
           (char *) &s1->last_picture_ptr - (char *) &s1->last_picture);
617

618 619 620 621
    // reset s->picture[].f.extended_data to s->picture[].f.data
    for (i = 0; i < s->picture_count; i++)
        s->picture[i].f.extended_data = s->picture[i].f.data;

622 623 624
    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);
625

626
    // Error/bug resilience
627 628 629
    s->next_p_frame_damaged = s1->next_p_frame_damaged;
    s->workaround_bugs      = s1->workaround_bugs;

630 631 632 633 634 635 636
    // 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;
637
    s->droppable    = s1->droppable;
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652

    // 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);
653 654
    }

655 656 657 658 659 660 661 662 663 664 665 666 667 668
    // linesize dependend scratch buffer allocation
    if (!s->edge_emu_buffer)
        if (s1->linesize) {
            if (ff_mpv_frame_size_alloc(s, s1->linesize) < 0) {
                av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
                       "scratch buffers.\n");
                return AVERROR(ENOMEM);
            }
        } else {
            av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
                   "be allocated due to unknown size.\n");
            return AVERROR_BUG;
        }

669 670 671
    // MPEG2/interlacing info
    memcpy(&s->progressive_sequence, &s1->progressive_sequence,
           (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
672

673 674 675 676
    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;
677

678
        if (s1->pict_type != AV_PICTURE_TYPE_B) {
679
            s->last_non_b_pict_type = s1->pict_type;
680 681 682 683 684 685
        }
    }

    return 0;
}

Michael Niedermayer's avatar
Michael Niedermayer committed
686
/**
687
 * Set the given MpegEncContext to common defaults
688
 * (same for encoding and decoding).
689
 * The changed fields will not depend upon the
690
 * prior state of the MpegEncContext.
Michael Niedermayer's avatar
Michael Niedermayer committed
691
 */
692
void ff_MPV_common_defaults(MpegEncContext *s)
693 694 695 696 697 698 699 700 701 702 703
{
    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
704 705

    s->picture_in_gop_number = 0;
706

707 708
    s->f_code                = 1;
    s->b_code                = 1;
709

710 711
    s->picture_range_start   = 0;
    s->picture_range_end     = MAX_PICTURE_COUNT;
712 713

    s->slice_context_count   = 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
714 715 716
}

/**
717
 * Set the given MpegEncContext to defaults for decoding.
718 719
 * the changed fields will not depend upon
 * the prior state of the MpegEncContext.
Michael Niedermayer's avatar
Michael Niedermayer committed
720
 */
721
void ff_MPV_decode_defaults(MpegEncContext *s)
722
{
723
    ff_MPV_common_defaults(s);
Michael Niedermayer's avatar
Michael Niedermayer committed
724 725
}

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 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 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
/**
 * Initialize and allocates MpegEncContext fields dependent on the resolution.
 */
static int init_context_frame(MpegEncContext *s)
{
    int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;

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

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

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

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

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

    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?

    if (s->encoding) {
        /* Allocate MV tables */
        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