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

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

Fabrice Bellard's avatar
Fabrice Bellard committed
30 31 32
#include "avcodec.h"
#include "dsputil.h"
#include "mpegvideo.h"
Michael Niedermayer's avatar
Michael Niedermayer committed
33
#include "faandct.h"
34
#include <limits.h>
Fabrice Bellard's avatar
Fabrice Bellard committed
35

36
#ifdef USE_FASTMEMCPY
37
#include "libvo/fastmemcpy.h"
38 39
#endif

40 41
//#undef NDEBUG
//#include <assert.h>
42

43
#ifdef CONFIG_ENCODERS
44
static int encode_picture(MpegEncContext *s, int picture_number);
45
#endif //CONFIG_ENCODERS
46
static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
47
                                   DCTELEM *block, int n, int qscale);
48
static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
49
                                   DCTELEM *block, int n, int qscale);
50 51
static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
                                   DCTELEM *block, int n, int qscale);
52 53
static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
                                   DCTELEM *block, int n, int qscale);
54 55
static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
                                   DCTELEM *block, int n, int qscale);
56
static void dct_unquantize_h263_intra_c(MpegEncContext *s,
57
                                  DCTELEM *block, int n, int qscale);
58
static void dct_unquantize_h263_inter_c(MpegEncContext *s,
59
                                  DCTELEM *block, int n, int qscale);
60
static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
61
#ifdef CONFIG_ENCODERS
62
static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
Michael Niedermayer's avatar
Michael Niedermayer committed
63
static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
Michael Niedermayer's avatar
Michael Niedermayer committed
64
static int dct_quantize_refine(MpegEncContext *s, DCTELEM *block, int16_t *weight, DCTELEM *orig, int n, int qscale);
65
static int sse_mb(MpegEncContext *s);
Michael Niedermayer's avatar
Michael Niedermayer committed
66
static void  denoise_dct_c(MpegEncContext *s, DCTELEM *block);
67
#endif //CONFIG_ENCODERS
Michael Niedermayer's avatar
Michael Niedermayer committed
68

Ivan Kalvachev's avatar
Ivan Kalvachev committed
69 70 71
#ifdef HAVE_XVMC
extern int  XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx);
extern void XVMC_field_end(MpegEncContext *s);
72
extern void XVMC_decode_mb(MpegEncContext *s);
Ivan Kalvachev's avatar
Ivan Kalvachev committed
73 74
#endif

75
void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w)= draw_edges_c;
Michael Niedermayer's avatar
Michael Niedermayer committed
76

Fabrice Bellard's avatar
Fabrice Bellard committed
77 78 79 80 81 82

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

//#define DEBUG

83

Fabrice Bellard's avatar
Fabrice Bellard committed
84 85 86
/* for jpeg fast DCT */
#define CONST_BITS 14

87
static const uint16_t aanscales[64] = {
Fabrice Bellard's avatar
Fabrice Bellard committed
88 89 90 91 92 93 94
    /* precomputed values scaled up by 14 bits */
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
    22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
    21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
    19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
    12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
Michael Niedermayer's avatar
Michael Niedermayer committed
95 96
    8867 , 12299, 11585, 10426,  8867,  6967,  4799,  2446,
    4520 ,  6270,  5906,  5315,  4520,  3552,  2446,  1247
Fabrice Bellard's avatar
Fabrice Bellard committed
97 98
};

99
static const uint8_t h263_chroma_roundtab[16] = {
100
//  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
Fabrice Bellard's avatar
Fabrice Bellard committed
101 102 103
    0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
};

Michael Niedermayer's avatar
Michael Niedermayer committed
104 105 106 107 108
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 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    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
};

109
#ifdef CONFIG_ENCODERS
110
static uint8_t (*default_mv_penalty)[MAX_MV*2+1]=NULL;
111
static uint8_t default_fcode_tab[MAX_MV*2+1];
112

Michael Niedermayer's avatar
Michael Niedermayer committed
113 114
enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};

115
static void convert_matrix(DSPContext *dsp, int (*qmat)[64], uint16_t (*qmat16)[2][64],
116
                           const uint16_t *quant_matrix, int bias, int qmin, int qmax, int intra)
Fabrice Bellard's avatar
Fabrice Bellard committed
117
{
118
    int qscale;
119
    int shift=0;
120

121
    for(qscale=qmin; qscale<=qmax; qscale++){
122
        int i;
123
        if (dsp->fdct == ff_jpeg_fdct_islow
124
#ifdef FAAN_POSTSCALE
125
            || dsp->fdct == ff_faandct
126 127
#endif
            ) {
128
            for(i=0;i<64;i++) {
129
                const int j= dsp->idct_permutation[i];
130 131 132 133
                /* 16 <= qscale * quant_matrix[i] <= 7905 */
                /* 19952         <= aanscales[i] * qscale * quant_matrix[i]           <= 249205026 */
                /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
                /* 3444240       >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
134

135
                qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
136 137
                                (qscale * quant_matrix[j]));
            }
138
        } else if (dsp->fdct == fdct_ifast
139
#ifndef FAAN_POSTSCALE
140
                   || dsp->fdct == ff_faandct
141 142
#endif
                   ) {
143
            for(i=0;i<64;i++) {
144
                const int j= dsp->idct_permutation[i];
145 146 147 148
                /* 16 <= qscale * quant_matrix[i] <= 7905 */
                /* 19952         <= aanscales[i] * qscale * quant_matrix[i]           <= 249205026 */
                /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
                /* 3444240       >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
149

150
                qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) /
151 152 153 154
                                (aanscales[i] * qscale * quant_matrix[j]));
            }
        } else {
            for(i=0;i<64;i++) {
155
                const int j= dsp->idct_permutation[i];
156 157 158 159 160
                /* We can safely suppose that 16 <= quant_matrix[i] <= 255
                   So 16           <= qscale * quant_matrix[i]             <= 7905
                   so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
                   so 32768        >= (1<<19) / (qscale * quant_matrix[i]) >= 67
                */
161
                qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
Michael Niedermayer's avatar
Michael Niedermayer committed
162
//                qmat  [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
163
                qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
164

165 166
                if(qmat16[qscale][0][i]==0 || qmat16[qscale][0][i]==128*256) qmat16[qscale][0][i]=128*256-1;
                qmat16[qscale][1][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][0][i]);
167
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
168
        }
169

170
        for(i=intra; i<64; i++){
171 172 173 174 175 176 177 178
            int64_t max= 8191;
            if (dsp->fdct == fdct_ifast
#ifndef FAAN_POSTSCALE
                   || dsp->fdct == ff_faandct
#endif
                   ) {
                max= (8191LL*aanscales[i]) >> 14;
            }
179
            while(((max * qmat[qscale][i]) >> shift) > INT_MAX){
180 181 182 183 184 185
                shift++;
            }
        }
    }
    if(shift){
        av_log(NULL, AV_LOG_INFO, "Warning, QMAT_SHIFT is larger then %d, overflows possible\n", QMAT_SHIFT - shift);
Fabrice Bellard's avatar
Fabrice Bellard committed
186 187
    }
}
188 189 190 191

static inline void update_qscale(MpegEncContext *s){
    s->qscale= (s->lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
    s->qscale= clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
192

193 194
    s->lambda2= (s->lambda*s->lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
}
195 196
#endif //CONFIG_ENCODERS

Michael Niedermayer's avatar
Michael Niedermayer committed
197
void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){
198 199
    int i;
    int end;
200

Michael Niedermayer's avatar
Michael Niedermayer committed
201
    st->scantable= src_scantable;
202 203 204 205

    for(i=0; i<64; i++){
        int j;
        j = src_scantable[i];
Michael Niedermayer's avatar
Michael Niedermayer committed
206
        st->permutated[i] = permutation[j];
207 208 209
#ifdef ARCH_POWERPC
        st->inverse[j] = i;
#endif
210
    }
211

212 213 214 215 216 217 218 219 220
    end=-1;
    for(i=0; i<64; i++){
        int j;
        j = st->permutated[i];
        if(j>end) end=j;
        st->raster_end[i]= end;
    }
}

221
#ifdef CONFIG_ENCODERS
Måns Rullgård's avatar
Måns Rullgård committed
222
void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix){
223 224 225 226 227 228 229 230 231 232
    int i;

    if(matrix){
        put_bits(pb, 1, 1);
        for(i=0;i<64;i++) {
            put_bits(pb, 8, matrix[ ff_zigzag_direct[i] ]);
        }
    }else
        put_bits(pb, 1, 0);
}
233
#endif //CONFIG_ENCODERS
234

Michael Niedermayer's avatar
Michael Niedermayer committed
235 236 237
const uint8_t *ff_find_start_code(const uint8_t * restrict p, const uint8_t *end, uint32_t * restrict state){
    int i;

Michael Niedermayer's avatar
Michael Niedermayer committed
238 239 240 241
    assert(p<=end);
    if(p>=end)
        return end;

Michael Niedermayer's avatar
Michael Niedermayer committed
242 243 244 245 246 247 248 249
    for(i=0; i<3; i++){
        uint32_t tmp= *state << 8;
        *state= tmp + *(p++);
        if(tmp == 0x100 || p==end)
            return p;
    }

    while(p<end){
Michael Niedermayer's avatar
Michael Niedermayer committed
250 251 252
        if     (p[-1] > 1      ) p+= 3;
        else if(p[-2]          ) p+= 2;
        else if(p[-3]|(p[-1]-1)) p++;
Michael Niedermayer's avatar
Michael Niedermayer committed
253 254 255 256 257 258
        else{
            p++;
            break;
        }
    }

Michael Niedermayer's avatar
Michael Niedermayer committed
259
    p= FFMIN(p, end)-4;
Michael Niedermayer's avatar
Michael Niedermayer committed
260 261 262 263 264
    *state=  be2me_32(unaligned32(p));

    return p+4;
}

265 266
/* init common dct for both encoder and decoder */
int DCT_common_init(MpegEncContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
267
{
268 269 270 271 272
    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;
273 274
    if(s->flags & CODEC_FLAG_BITEXACT)
        s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
275
    s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
276

277
#ifdef CONFIG_ENCODERS
278
    s->dct_quantize= dct_quantize_c;
Michael Niedermayer's avatar
Michael Niedermayer committed
279
    s->denoise_dct= denoise_dct_c;
280
#endif //CONFIG_ENCODERS
281

282
#ifdef HAVE_MMX
283
    MPV_common_init_mmx(s);
284 285 286
#endif
#ifdef ARCH_ALPHA
    MPV_common_init_axp(s);
Nick Kurshev's avatar
Nick Kurshev committed
287
#endif
288 289 290
#ifdef HAVE_MLIB
    MPV_common_init_mlib(s);
#endif
291 292 293
#ifdef HAVE_MMI
    MPV_common_init_mmi(s);
#endif
294
#ifdef ARCH_ARMV4L
295
    MPV_common_init_armv4l(s);
296
#endif
297 298 299
#ifdef ARCH_POWERPC
    MPV_common_init_ppc(s);
#endif
300

301
#ifdef CONFIG_ENCODERS
302 303
    s->fast_dct_quantize= s->dct_quantize;

Michael Niedermayer's avatar
Michael Niedermayer committed
304 305 306 307
    if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
        s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_*
    }

308 309
#endif //CONFIG_ENCODERS

310
    /* load & permutate scantables
311
       note: only wmv uses different ones
312
    */
313 314 315 316 317 318 319
    if(s->alternate_scan){
        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);
    }else{
        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
320 321
    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);
322

323 324 325
    return 0;
}

326 327 328 329 330
static void copy_picture(Picture *dst, Picture *src){
    *dst = *src;
    dst->type= FF_BUFFER_TYPE_COPY;
}

331
#ifdef CONFIG_ENCODERS
332 333 334
static void copy_picture_attributes(MpegEncContext *s, AVFrame *dst, AVFrame *src){
    int i;

335 336 337 338 339 340 341 342
    dst->pict_type              = src->pict_type;
    dst->quality                = src->quality;
    dst->coded_picture_number   = src->coded_picture_number;
    dst->display_picture_number = src->display_picture_number;
//    dst->reference              = src->reference;
    dst->pts                    = src->pts;
    dst->interlaced_frame       = src->interlaced_frame;
    dst->top_field_first        = src->top_field_first;
343

344 345 346 347 348 349 350
    if(s->avctx->me_threshold){
        if(!src->motion_val[0])
            av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_val not set!\n");
        if(!src->mb_type)
            av_log(s->avctx, AV_LOG_ERROR, "AVFrame.mb_type not set!\n");
        if(!src->ref_index[0])
            av_log(s->avctx, AV_LOG_ERROR, "AVFrame.ref_index not set!\n");
351
        if(src->motion_subsample_log2 != dst->motion_subsample_log2)
352
            av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_subsample_log2 doesn't match! (%d!=%d)\n",
353
            src->motion_subsample_log2, dst->motion_subsample_log2);
354 355

        memcpy(dst->mb_type, src->mb_type, s->mb_stride * s->mb_height * sizeof(dst->mb_type[0]));
356

357
        for(i=0; i<2; i++){
358 359 360
            int stride= ((16*s->mb_width )>>src->motion_subsample_log2) + 1;
            int height= ((16*s->mb_height)>>src->motion_subsample_log2);

361 362 363 364
            if(src->motion_val[i] && src->motion_val[i] != dst->motion_val[i]){
                memcpy(dst->motion_val[i], src->motion_val[i], 2*stride*height*sizeof(int16_t));
            }
            if(src->ref_index[i] && src->ref_index[i] != dst->ref_index[i]){
365
                memcpy(dst->ref_index[i], src->ref_index[i], s->b8_stride*2*s->mb_height*sizeof(int8_t));
366
            }
367 368
        }
    }
369
}
370
#endif
371

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
372
/**
Michael Niedermayer's avatar
Michael Niedermayer committed
373 374
 * allocates a Picture
 * The pixels are allocated/set by calling get_buffer() if shared=0
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
375
 */
Michael Niedermayer's avatar
Michael Niedermayer committed
376
static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
377 378
    const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) doesnt sig11
    const int mb_array_size= s->mb_stride*s->mb_height;
379 380
    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
381
    int i;
382

Michael Niedermayer's avatar
Michael Niedermayer committed
383 384 385 386 387 388
    if(shared){
        assert(pic->data[0]);
        assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
        pic->type= FF_BUFFER_TYPE_SHARED;
    }else{
        int r;
389

Michael Niedermayer's avatar
Michael Niedermayer committed
390
        assert(!pic->data[0]);
391

Michael Niedermayer's avatar
Michael Niedermayer committed
392
        r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
393

Michael Niedermayer's avatar
Michael Niedermayer committed
394
        if(r<0 || !pic->age || !pic->type || !pic->data[0]){
395
            av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
Michael Niedermayer's avatar
Michael Niedermayer committed
396 397 398 399
            return -1;
        }

        if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
400
            av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
Michael Niedermayer's avatar
Michael Niedermayer committed
401 402 403 404
            return -1;
        }

        if(pic->linesize[1] != pic->linesize[2]){
Diego Biurrun's avatar
Diego Biurrun committed
405
            av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride mismatch)\n");
Michael Niedermayer's avatar
Michael Niedermayer committed
406 407 408 409 410
            return -1;
        }

        s->linesize  = pic->linesize[0];
        s->uvlinesize= pic->linesize[1];
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
411
    }
412

Michael Niedermayer's avatar
Michael Niedermayer committed
413
    if(pic->qscale_table==NULL){
414
        if (s->encoding) {
415 416 417
            CHECKED_ALLOCZ(pic->mb_var   , mb_array_size * sizeof(int16_t))
            CHECKED_ALLOCZ(pic->mc_mb_var, mb_array_size * sizeof(int16_t))
            CHECKED_ALLOCZ(pic->mb_mean  , mb_array_size * sizeof(int8_t))
Michael Niedermayer's avatar
Michael Niedermayer committed
418
        }
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
419

420
        CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2) //the +2 is for the slice end check
421
        CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
422
        CHECKED_ALLOCZ(pic->mb_type_base , big_mb_num    * sizeof(uint32_t))
423
        pic->mb_type= pic->mb_type_base + s->mb_stride+1;
Michael Niedermayer's avatar
Michael Niedermayer committed
424 425
        if(s->out_format == FMT_H264){
            for(i=0; i<2; i++){
Michael Niedermayer's avatar
Michael Niedermayer committed
426 427
                CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b4_array_size+4)  * sizeof(int16_t))
                pic->motion_val[i]= pic->motion_val_base[i]+4;
428
                CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
429
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
430
            pic->motion_subsample_log2= 2;
431
        }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_MV) || (s->avctx->debug_mv)){
432
            for(i=0; i<2; i++){
Michael Niedermayer's avatar
Michael Niedermayer committed
433 434
                CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b8_array_size+4) * sizeof(int16_t))
                pic->motion_val[i]= pic->motion_val_base[i]+4;
435
                CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
Michael Niedermayer's avatar
Michael Niedermayer committed
436
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
437
            pic->motion_subsample_log2= 3;
Michael Niedermayer's avatar
Michael Niedermayer committed
438
        }
439 440 441
        if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
            CHECKED_ALLOCZ(pic->dct_coeff, 64 * mb_array_size * sizeof(DCTELEM)*6)
        }
442
        pic->qstride= s->mb_stride;
443
        CHECKED_ALLOCZ(pic->pan_scan , 1 * sizeof(AVPanScan))
Michael Niedermayer's avatar
Michael Niedermayer committed
444
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
445

446 447 448 449
    //it might be nicer if the application would keep track of these but it would require a API change
    memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
    s->prev_pict_types[0]= s->pict_type;
    if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
450
        pic->age= INT_MAX; // skipped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
451

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
452 453 454 455 456
    return 0;
fail: //for the CHECKED_ALLOCZ macro
    return -1;
}

Michael Niedermayer's avatar
Michael Niedermayer committed
457 458 459
/**
 * deallocates a picture
 */
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
460 461
static void free_picture(MpegEncContext *s, Picture *pic){
    int i;
Michael Niedermayer's avatar
Michael Niedermayer committed
462 463

    if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
Michael Niedermayer's avatar
Michael Niedermayer committed
464
        s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
Michael Niedermayer's avatar
Michael Niedermayer committed
465 466
    }

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
467 468 469 470 471
    av_freep(&pic->mb_var);
    av_freep(&pic->mc_mb_var);
    av_freep(&pic->mb_mean);
    av_freep(&pic->mbskip_table);
    av_freep(&pic->qscale_table);
Michael Niedermayer's avatar
Michael Niedermayer committed
472
    av_freep(&pic->mb_type_base);
473
    av_freep(&pic->dct_coeff);
474
    av_freep(&pic->pan_scan);
Michael Niedermayer's avatar
Michael Niedermayer committed
475 476
    pic->mb_type= NULL;
    for(i=0; i<2; i++){
477
        av_freep(&pic->motion_val_base[i]);
Michael Niedermayer's avatar
Michael Niedermayer committed
478 479
        av_freep(&pic->ref_index[i]);
    }
480

481
    if(pic->type == FF_BUFFER_TYPE_SHARED){
Michael Niedermayer's avatar
Michael Niedermayer committed
482 483 484 485
        for(i=0; i<4; i++){
            pic->base[i]=
            pic->data[i]= NULL;
        }
486
        pic->type= 0;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
487 488 489
    }
}

490 491 492
static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base){
    int i;

493
    // edge emu needs blocksize + filter length - 1 (=17x17 for halfpel / 21x21 for h264)
494 495
    CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*21*2); //(width + edge + align)*interlaced*MBsize*tolerance
    s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*21;
496 497

     //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
498
    CHECKED_ALLOCZ(s->me.scratchpad,  (s->width+64)*4*16*2*sizeof(uint8_t))
499 500 501 502 503 504 505 506 507
    s->rd_scratchpad=   s->me.scratchpad;
    s->b_scratchpad=    s->me.scratchpad;
    s->obmc_scratchpad= s->me.scratchpad + 16;
    if (s->encoding) {
        CHECKED_ALLOCZ(s->me.map      , ME_MAP_SIZE*sizeof(uint32_t))
        CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
        if(s->avctx->noise_reduction){
            CHECKED_ALLOCZ(s->dct_error_sum, 2 * 64 * sizeof(int))
        }
508
    }
509
    CHECKED_ALLOCZ(s->blocks, 64*12*2 * sizeof(DCTELEM))
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
    s->block= s->blocks[0];

    for(i=0;i<12;i++){
        s->pblocks[i] = (short *)(&s->block[i]);
    }
    return 0;
fail:
    return -1; //free() through MPV_common_end()
}

static void free_duplicate_context(MpegEncContext *s){
    if(s==NULL) return;

    av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
    av_freep(&s->me.scratchpad);
525 526
    s->rd_scratchpad=
    s->b_scratchpad=
527
    s->obmc_scratchpad= NULL;
528

529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
    av_freep(&s->dct_error_sum);
    av_freep(&s->me.map);
    av_freep(&s->me.score_map);
    av_freep(&s->blocks);
    s->block= NULL;
}

static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src){
#define COPY(a) bak->a= src->a
    COPY(allocated_edge_emu_buffer);
    COPY(edge_emu_buffer);
    COPY(me.scratchpad);
    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);
553 554
    COPY(dct_count[0]);
    COPY(dct_count[1]);
555 556 557
#undef COPY
}

558
void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src){
559
    MpegEncContext bak;
560
    int i;
561 562 563 564 565
    //FIXME copy only needed parts
//START_TIMER
    backup_duplicate_context(&bak, dst);
    memcpy(dst, src, sizeof(MpegEncContext));
    backup_duplicate_context(dst, &bak);
566 567 568
    for(i=0;i<12;i++){
        dst->pblocks[i] = (short *)(&dst->block[i]);
    }
569 570 571
//STOP_TIMER("update_duplicate_context") //about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
}

572
#ifdef CONFIG_ENCODERS
573 574 575 576 577 578 579 580 581 582 583
static void update_duplicate_context_after_me(MpegEncContext *dst, MpegEncContext *src){
#define COPY(a) dst->a= src->a
    COPY(pict_type);
    COPY(current_picture);
    COPY(f_code);
    COPY(b_code);
    COPY(qscale);
    COPY(lambda);
    COPY(lambda2);
    COPY(picture_in_gop_number);
    COPY(gop_picture_number);
Diego Biurrun's avatar
Diego Biurrun committed
584 585 586
    COPY(frame_pred_frame_dct); //FIXME don't set in encode_header
    COPY(progressive_frame); //FIXME don't set in encode_header
    COPY(partitioned_frame); //FIXME don't set in encode_header
587 588
#undef COPY
}
589
#endif
590

Michael Niedermayer's avatar
Michael Niedermayer committed
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
/**
 * sets the given MpegEncContext to common defaults (same for encoding and decoding).
 * the changed fields will not depend upon the prior state of the MpegEncContext.
 */
static 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;

    s->picture_in_gop_number = 0;
608 609 610

    s->f_code = 1;
    s->b_code = 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624
}

/**
 * sets the given MpegEncContext to defaults for decoding.
 * the changed fields will not depend upon the prior state of the MpegEncContext.
 */
void MPV_decode_defaults(MpegEncContext *s){
    MPV_common_defaults(s);
}

/**
 * sets the given MpegEncContext to defaults for encoding.
 * the changed fields will not depend upon the prior state of the MpegEncContext.
 */
625 626

#ifdef CONFIG_ENCODERS
627
static void MPV_encode_defaults(MpegEncContext *s){
Michael Niedermayer's avatar
Michael Niedermayer committed
628
    static int done=0;
629

Michael Niedermayer's avatar
Michael Niedermayer committed
630
    MPV_common_defaults(s);
631

Michael Niedermayer's avatar
Michael Niedermayer committed
632 633 634 635 636 637 638 639 640 641 642 643 644 645
    if(!done){
        int i;
        done=1;

        default_mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
        memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));

        for(i=-16; i<16; i++){
            default_fcode_tab[i + MAX_MV]= 1;
        }
    }
    s->me.mv_penalty= default_mv_penalty;
    s->fcode_tab= default_fcode_tab;
}
646
#endif //CONFIG_ENCODERS
Michael Niedermayer's avatar
Michael Niedermayer committed
647

648
/**
Michael Niedermayer's avatar
Michael Niedermayer committed
649 650 651
 * init common structure for both encoder and decoder.
 * this assumes that some variables like width/height are already set
 */
652 653
int MPV_common_init(MpegEncContext *s)
{
654
    int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
655

656 657 658
    s->mb_height = (s->height + 15) / 16;

    if(s->avctx->thread_count > MAX_THREADS || (s->avctx->thread_count > s->mb_height && s->mb_height)){
Michael Niedermayer's avatar
10l  
Michael Niedermayer committed
659 660 661 662
        av_log(s->avctx, AV_LOG_ERROR, "too many threads\n");
        return -1;
    }

663 664 665
    if((s->width || s->height) && avcodec_check_dimensions(s->avctx, s->width, s->height))
        return -1;

666
    dsputil_init(&s->dsp, s->avctx);
667
    DCT_common_init(s);
668

669
    s->flags= s->avctx->flags;
670
    s->flags2= s->avctx->flags2;
671

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
672
    s->mb_width  = (s->width  + 15) / 16;
673
    s->mb_stride = s->mb_width + 1;
674 675
    s->b8_stride = s->mb_width*2 + 1;
    s->b4_stride = s->mb_width*4 + 1;
676
    mb_array_size= s->mb_height * s->mb_stride;
677
    mv_table_size= (s->mb_height+2) * s->mb_stride + 1;
678

679 680 681 682
    /* set chroma shifts */
    avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,&(s->chroma_x_shift),
                                                    &(s->chroma_y_shift) );

683 684 685
    /* 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;
686 687

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

689 690 691
    s->block_wrap[0]=
    s->block_wrap[1]=
    s->block_wrap[2]=
692
    s->block_wrap[3]= s->b8_stride;
693
    s->block_wrap[4]=
694
    s->block_wrap[5]= s->mb_stride;
695

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

700
    /* convert fourcc to upper case */
701
    s->codec_tag=          toupper( s->avctx->codec_tag     &0xFF)
702
                        + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
703
                        + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16)
704
                        + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
705

706
    s->stream_codec_tag=          toupper( s->avctx->stream_codec_tag     &0xFF)
707
                               + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 )
708
                               + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16)
709 710
                               + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24);

Michael Niedermayer's avatar
Michael Niedermayer committed
711
    s->avctx->coded_frame= (AVFrame*)&s->current_picture;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
712

713 714 715 716 717 718 719
    CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int)) //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?
720

721
    if (s->encoding) {
722
        /* Allocate MV tables */
723 724 725 726 727 728 729 730 731 732 733 734
        CHECKED_ALLOCZ(s->p_mv_table_base            , mv_table_size * 2 * sizeof(int16_t))
        CHECKED_ALLOCZ(s->b_forw_mv_table_base       , mv_table_size * 2 * sizeof(int16_t))
        CHECKED_ALLOCZ(s->b_back_mv_table_base       , mv_table_size * 2 * sizeof(int16_t))
        CHECKED_ALLOCZ(s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
        CHECKED_ALLOCZ(s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
        CHECKED_ALLOCZ(s->b_direct_mv_table_base     , mv_table_size * 2 * sizeof(int16_t))
        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;
735

736 737 738
        if(s->msmpeg4_version){
            CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
739
        CHECKED_ALLOCZ(s->avctx->stats_out, 256);
740 741

        /* Allocate MB type table */
742
        CHECKED_ALLOCZ(s->mb_type  , mb_array_size * sizeof(uint16_t)) //needed for encoding
743

744
        CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int))
745

746 747
        CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int))
        CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int))
748 749
        CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t))
        CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t))
750 751
        CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
        CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
752

753 754 755
        if(s->avctx->noise_reduction){
            CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t))
        }
756
    }
757 758
    CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))

759
    CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
760

761
    if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){
762
        /* interlaced direct mode decoding tables */
763 764 765 766 767 768 769 770 771 772 773 774 775
            for(i=0; i<2; i++){
                int j, k;
                for(j=0; j<2; j++){
                    for(k=0; k<2; k++){
                        CHECKED_ALLOCZ(s->b_field_mv_table_base[i][j][k]     , mv_table_size * 2 * sizeof(int16_t))
                        s->b_field_mv_table[i][j][k]    = s->b_field_mv_table_base[i][j][k]     + s->mb_stride + 1;
                    }
                    CHECKED_ALLOCZ(s->b_field_select_table[i][j]     , mb_array_size * 2 * sizeof(uint8_t))
                    CHECKED_ALLOCZ(s->p_field_mv_table_base[i][j]     , mv_table_size * 2 * sizeof(int16_t))
                    s->p_field_mv_table[i][j]    = s->p_field_mv_table_base[i][j]     + s->mb_stride + 1;
                }
                CHECKED_ALLOCZ(s->p_field_select_table[i]      , mb_array_size * 2 * sizeof(uint8_t))
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
776
    }
777
    if (s->out_format == FMT_H263) {