mpegvideo.c 92 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 9 10
 * This file is part of FFmpeg.
 *
 * FFmpeg 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
 * FFmpeg 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 FFmpeg; 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 libavcodec/mpegvideo.c
Michael Niedermayer's avatar
Michael Niedermayer committed
27
 * The simplest mpeg encoder (well, it was the simplest!).
28 29
 */

30
#include "libavutil/intmath.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
31 32 33
#include "avcodec.h"
#include "dsputil.h"
#include "mpegvideo.h"
Benoit Fouet's avatar
Benoit Fouet committed
34
#include "mpegvideo_common.h"
35
#include "mjpegenc.h"
36
#include "msmpeg4.h"
Michael Niedermayer's avatar
Michael Niedermayer committed
37
#include "faandct.h"
38
#include "xvmc_internal.h"
39
#include <limits.h>
Fabrice Bellard's avatar
Fabrice Bellard committed
40

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

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

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

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

//#define DEBUG

65

Michael Niedermayer's avatar
Michael Niedermayer committed
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 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
};

71 72 73 74 75 76 77 78
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 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
};

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
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 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
};

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 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
};

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 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};

103
const uint8_t * const ff_mpeg2_dc_scale_table[4]={
104 105 106 107 108 109
    ff_mpeg1_dc_scale_table,
    mpeg2_dc_scale_table1,
    mpeg2_dc_scale_table2,
    mpeg2_dc_scale_table3,
};

110
const enum PixelFormat ff_pixfmt_list_420[] = {
111 112 113 114 115
    PIX_FMT_YUV420P,
    PIX_FMT_NONE
};

const enum PixelFormat ff_hwaccel_pixfmt_list_420[] = {
Laurent Aimar's avatar
Laurent Aimar committed
116
    PIX_FMT_DXVA2_VLD,
117
    PIX_FMT_VAAPI_VLD,
118 119 120
    PIX_FMT_YUV420P,
    PIX_FMT_NONE
};
121

Michael Niedermayer's avatar
Michael Niedermayer committed
122 123 124
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
125 126 127 128
    assert(p<=end);
    if(p>=end)
        return end;

Michael Niedermayer's avatar
Michael Niedermayer committed
129 130 131 132 133 134 135 136
    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
137 138 139
        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
140 141 142 143 144 145
        else{
            p++;
            break;
        }
    }

Michael Niedermayer's avatar
Michael Niedermayer committed
146
    p= FFMIN(p, end)-4;
147
    *state= AV_RB32(p);
Michael Niedermayer's avatar
Michael Niedermayer committed
148 149 150 151

    return p+4;
}

152
/* init common dct for both encoder and decoder */
153
av_cold int ff_dct_common_init(MpegEncContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
154
{
155 156 157 158 159
    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;
160 161
    if(s->flags & CODEC_FLAG_BITEXACT)
        s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
162
    s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
163

164
#if   HAVE_MMX
165
    MPV_common_init_mmx(s);
166
#elif ARCH_ALPHA
167
    MPV_common_init_axp(s);
168
#elif CONFIG_MLIB
169
    MPV_common_init_mlib(s);
170
#elif HAVE_MMI
171
    MPV_common_init_mmi(s);
172
#elif ARCH_ARM
173
    MPV_common_init_arm(s);
174
#elif HAVE_ALTIVEC
175
    MPV_common_init_altivec(s);
176
#elif ARCH_BFIN
177 178
    MPV_common_init_bfin(s);
#endif
179

180
    /* load & permutate scantables
181
       note: only wmv uses different ones
182
    */
183 184 185 186 187 188 189
    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
190 191
    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);
192

193 194 195
    return 0;
}

196
void ff_copy_picture(Picture *dst, Picture *src){
197 198 199 200
    *dst = *src;
    dst->type= FF_BUFFER_TYPE_COPY;
}

201 202 203 204 205 206
/**
 * Releases a frame buffer
 */
static void free_frame_buffer(MpegEncContext *s, Picture *pic)
{
    s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
207
    av_freep(&pic->hwaccel_picture_private);
208 209 210 211 212 213 214 215 216
}

/**
 * Allocates a frame buffer
 */
static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
{
    int r;

217
    if (s->avctx->hwaccel) {
218
        assert(!pic->hwaccel_picture_private);
219
        if (s->avctx->hwaccel->priv_data_size) {
220 221
            pic->hwaccel_picture_private = av_mallocz(s->avctx->hwaccel->priv_data_size);
            if (!pic->hwaccel_picture_private) {
222 223 224 225 226 227
                av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
                return -1;
            }
        }
    }

228 229 230 231
    r = s->avctx->get_buffer(s->avctx, (AVFrame*)pic);

    if (r<0 || !pic->age || !pic->type || !pic->data[0]) {
        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
232
        av_freep(&pic->hwaccel_picture_private);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
        return -1;
    }

    if (s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])) {
        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
        free_frame_buffer(s, pic);
        return -1;
    }

    if (pic->linesize[1] != pic->linesize[2]) {
        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride mismatch)\n");
        free_frame_buffer(s, pic);
        return -1;
    }

    return 0;
}

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
251
/**
Michael Niedermayer's avatar
Michael Niedermayer committed
252 253
 * allocates a Picture
 * The pixels are allocated/set by calling get_buffer() if shared=0
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
254
 */
255
int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared){
Diego Biurrun's avatar
Diego Biurrun committed
256
    const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) does not sig11
257
    const int mb_array_size= s->mb_stride*s->mb_height;
258 259
    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
260
    int i;
Daniel Kristjansson's avatar
Daniel Kristjansson committed
261
    int r= -1;
262

Michael Niedermayer's avatar
Michael Niedermayer committed
263 264 265 266 267 268
    if(shared){
        assert(pic->data[0]);
        assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
        pic->type= FF_BUFFER_TYPE_SHARED;
    }else{
        assert(!pic->data[0]);
269

270
        if (alloc_frame_buffer(s, pic) < 0)
Michael Niedermayer's avatar
Michael Niedermayer committed
271 272 273 274
            return -1;

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

Michael Niedermayer's avatar
Michael Niedermayer committed
277
    if(pic->qscale_table==NULL){
278
        if (s->encoding) {
279 280 281
            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
282
        }
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
283

284 285 286
        FF_ALLOCZ_OR_GOTO(s->avctx, pic->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 , mb_array_size * sizeof(uint8_t)  , fail)
        FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_type_base , (big_mb_num + s->mb_stride) * sizeof(uint32_t), fail)
287
        pic->mb_type= pic->mb_type_base + 2*s->mb_stride+1;
Michael Niedermayer's avatar
Michael Niedermayer committed
288 289
        if(s->out_format == FMT_H264){
            for(i=0; i<2; i++){
290
                FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i], 2 * (b4_array_size+4)  * sizeof(int16_t), fail)
Michael Niedermayer's avatar
Michael Niedermayer committed
291
                pic->motion_val[i]= pic->motion_val_base[i]+4;
292
                FF_ALLOCZ_OR_GOTO(s->avctx, pic->ref_index[i], 4*mb_array_size * sizeof(uint8_t), fail)
293
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
294
            pic->motion_subsample_log2= 2;
295
        }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_MV) || (s->avctx->debug_mv)){
296
            for(i=0; i<2; i++){
297
                FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i], 2 * (b8_array_size+4) * sizeof(int16_t), fail)
Michael Niedermayer's avatar
Michael Niedermayer committed
298
                pic->motion_val[i]= pic->motion_val_base[i]+4;
299
                FF_ALLOCZ_OR_GOTO(s->avctx, pic->ref_index[i], b8_array_size * sizeof(uint8_t), fail)
Michael Niedermayer's avatar
Michael Niedermayer committed
300
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
301
            pic->motion_subsample_log2= 3;
Michael Niedermayer's avatar
Michael Niedermayer committed
302
        }
303
        if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
304
            FF_ALLOCZ_OR_GOTO(s->avctx, pic->dct_coeff, 64 * mb_array_size * sizeof(DCTELEM)*6, fail)
305
        }
306
        pic->qstride= s->mb_stride;
307
        FF_ALLOCZ_OR_GOTO(s->avctx, pic->pan_scan , 1 * sizeof(AVPanScan), fail)
Michael Niedermayer's avatar
Michael Niedermayer committed
308
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
309

Diego Biurrun's avatar
Diego Biurrun committed
310 311
    /* It might be nicer if the application would keep track of these
     * but it would require an API change. */
312
    memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
313
    s->prev_pict_types[0]= s->dropable ? FF_B_TYPE : s->pict_type;
314
    if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == FF_B_TYPE)
Diego Biurrun's avatar
Diego Biurrun committed
315
        pic->age= INT_MAX; // Skipped MBs in B-frames are quite rare in MPEG-1/2 and it is a bit tricky to skip them anyway.
316

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
317
    return 0;
318
fail: //for the FF_ALLOCZ_OR_GOTO macro
Daniel Kristjansson's avatar
Daniel Kristjansson committed
319
    if(r>=0)
320
        free_frame_buffer(s, pic);
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
321 322 323
    return -1;
}

Michael Niedermayer's avatar
Michael Niedermayer committed
324 325 326
/**
 * deallocates a picture
 */
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
327 328
static void free_picture(MpegEncContext *s, Picture *pic){
    int i;
Michael Niedermayer's avatar
Michael Niedermayer committed
329 330

    if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
331
        free_frame_buffer(s, pic);
Michael Niedermayer's avatar
Michael Niedermayer committed
332 333
    }

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
334 335 336 337 338
    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
339
    av_freep(&pic->mb_type_base);
340
    av_freep(&pic->dct_coeff);
341
    av_freep(&pic->pan_scan);
Michael Niedermayer's avatar
Michael Niedermayer committed
342 343
    pic->mb_type= NULL;
    for(i=0; i<2; i++){
344
        av_freep(&pic->motion_val_base[i]);
Michael Niedermayer's avatar
Michael Niedermayer committed
345 346
        av_freep(&pic->ref_index[i]);
    }
347

348
    if(pic->type == FF_BUFFER_TYPE_SHARED){
Michael Niedermayer's avatar
Michael Niedermayer committed
349 350 351 352
        for(i=0; i<4; i++){
            pic->base[i]=
            pic->data[i]= NULL;
        }
353
        pic->type= 0;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
354 355 356
    }
}

357 358 359
static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base){
    int i;

360
    // edge emu needs blocksize + filter length - 1 (=17x17 for halfpel / 21x21 for h264)
361
    FF_ALLOCZ_OR_GOTO(s->avctx, s->allocated_edge_emu_buffer, (s->width+64)*2*21*2, fail); //(width + edge + align)*interlaced*MBsize*tolerance
362
    s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*21;
363

Diego Biurrun's avatar
Diego Biurrun committed
364
     //FIXME should be linesize instead of s->width*2 but that is not known before get_buffer()
365
    FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad,  (s->width+64)*4*16*2*sizeof(uint8_t), fail)
366
    s->me.temp=         s->me.scratchpad;
367 368 369 370
    s->rd_scratchpad=   s->me.scratchpad;
    s->b_scratchpad=    s->me.scratchpad;
    s->obmc_scratchpad= s->me.scratchpad + 16;
    if (s->encoding) {
371 372
        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)
373
        if(s->avctx->noise_reduction){
374
            FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum, 2 * 64 * sizeof(int), fail)
375
        }
376
    }
377
    FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64*12*2 * sizeof(DCTELEM), fail)
378 379 380
    s->block= s->blocks[0];

    for(i=0;i<12;i++){
381
        s->pblocks[i] = &s->block[i];
382 383 384 385 386 387 388 389 390 391 392
    }
    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);
393
    s->me.temp=
394 395
    s->rd_scratchpad=
    s->b_scratchpad=
396
    s->obmc_scratchpad= NULL;
397

398 399 400 401 402 403 404 405 406 407 408 409
    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);
410
    COPY(me.temp);
411 412 413 414 415 416 417 418 419 420 421 422
    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);
423 424
    COPY(dct_count[0]);
    COPY(dct_count[1]);
425 426 427
#undef COPY
}

428
void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src){
429
    MpegEncContext bak;
430
    int i;
431 432 433 434 435
    //FIXME copy only needed parts
//START_TIMER
    backup_duplicate_context(&bak, dst);
    memcpy(dst, src, sizeof(MpegEncContext));
    backup_duplicate_context(dst, &bak);
436
    for(i=0;i<12;i++){
437
        dst->pblocks[i] = &dst->block[i];
438
    }
439 440 441
//STOP_TIMER("update_duplicate_context") //about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
}

Michael Niedermayer's avatar
Michael Niedermayer committed
442 443 444 445
/**
 * 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.
 */
Benoit Fouet's avatar
Benoit Fouet committed
446
void MPV_common_defaults(MpegEncContext *s){
Michael Niedermayer's avatar
Michael Niedermayer committed
447 448 449 450 451 452 453 454 455 456 457 458
    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;
459 460 461

    s->f_code = 1;
    s->b_code = 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
462 463 464 465 466 467 468 469 470 471
}

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

472
/**
Michael Niedermayer's avatar
Michael Niedermayer committed
473 474 475
 * init common structure for both encoder and decoder.
 * this assumes that some variables like width/height are already set
 */
476
av_cold int MPV_common_init(MpegEncContext *s)
477
{
478
    int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y, threads;
479

480 481 482
    if(s->codec_id == CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
        s->mb_height = (s->height + 31) / 32 * 2;
    else
Baptiste Coudurier's avatar
Baptiste Coudurier committed
483
        s->mb_height = (s->height + 15) / 16;
484

485 486 487 488 489
    if(s->avctx->pix_fmt == PIX_FMT_NONE){
        av_log(s->avctx, AV_LOG_ERROR, "decoding to PIX_FMT_NONE is not supported.\n");
        return -1;
    }

490
    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
491 492 493 494
        av_log(s->avctx, AV_LOG_ERROR, "too many threads\n");
        return -1;
    }

495 496 497
    if((s->width || s->height) && avcodec_check_dimensions(s->avctx, s->width, s->height))
        return -1;

498
    dsputil_init(&s->dsp, s->avctx);
499
    ff_dct_common_init(s);
500

501
    s->flags= s->avctx->flags;
502
    s->flags2= s->avctx->flags2;
503

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
504
    s->mb_width  = (s->width  + 15) / 16;
505
    s->mb_stride = s->mb_width + 1;
506 507
    s->b8_stride = s->mb_width*2 + 1;
    s->b4_stride = s->mb_width*4 + 1;
508
    mb_array_size= s->mb_height * s->mb_stride;
509
    mv_table_size= (s->mb_height+2) * s->mb_stride + 1;
510

511 512 513 514
    /* set chroma shifts */
    avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,&(s->chroma_x_shift),
                                                    &(s->chroma_y_shift) );

515 516 517
    /* 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;
518 519

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

521 522 523
    s->block_wrap[0]=
    s->block_wrap[1]=
    s->block_wrap[2]=
524
    s->block_wrap[3]= s->b8_stride;
525
    s->block_wrap[4]=
526
    s->block_wrap[5]= s->mb_stride;
527

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

532
    /* convert fourcc to upper case */
533
    s->codec_tag=          toupper( s->avctx->codec_tag     &0xFF)
534
                        + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
535
                        + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16)
536
                        + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
537

538
    s->stream_codec_tag=          toupper( s->avctx->stream_codec_tag     &0xFF)
539
                               + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 )
540
                               + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16)
541 542
                               + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24);

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

545
    FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num+1)*sizeof(int), fail) //error ressilience code looks cleaner with this
546 547 548 549 550 551
    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?
552

553
    if (s->encoding) {
554
        /* Allocate MV tables */
555 556 557 558 559 560
        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)
561 562 563 564 565 566
        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;
567

568
        if(s->msmpeg4_version){
569
            FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int), fail);
570
        }
571
        FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
572 573

        /* Allocate MB type table */
574
        FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type  , mb_array_size * sizeof(uint16_t), fail) //needed for encoding
575

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

578 579 580 581 582 583
        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)
584

585
        if(s->avctx->noise_reduction){
586
            FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset, 2 * 64 * sizeof(uint16_t), fail)
587
        }
588
    }
589
    FF_ALLOCZ_OR_GOTO(s->avctx, s->picture, MAX_PICTURE_COUNT * sizeof(Picture), fail)
590 591 592
    for(i = 0; i < MAX_PICTURE_COUNT; i++) {
        avcodec_get_frame_defaults((AVFrame *)&s->picture[i]);
    }
593

594
    FF_ALLOCZ_OR_GOTO(s->avctx, s->error_status_table, mb_array_size*sizeof(uint8_t), fail)
595

596
    if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){
597
        /* interlaced direct mode decoding tables */
598 599 600 601
            for(i=0; i<2; i++){
                int j, k;
                for(j=0; j<2; j++){
                    for(k=0; k<2; k++){
602 603
                        FF_ALLOCZ_OR_GOTO(s->avctx,    s->b_field_mv_table_base[i][j][k], mv_table_size * 2 * sizeof(int16_t), fail)
                        s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1;
604
                    }
605 606 607
                    FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
                    FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
                    s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]+ s->mb_stride + 1;
608
                }
609
                FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
610
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
611
    }
612
    if (s->out_format == FMT_H263) {
Fabrice Bellard's avatar
Fabrice Bellard committed
613
        /* ac values */
614
        FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base, yc_size * sizeof(int16_t) * 16, fail);
615 616
        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;
Fabrice Bellard's avatar
Fabrice Bellard committed
617
        s->ac_val[2] = s->ac_val[1] + c_size;
618

Fabrice Bellard's avatar
Fabrice Bellard committed
619
        /* cbp values */
620
        FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
621
        s->coded_block= s->coded_block_base + s->b8_stride + 1;
622

623
        /* cbp, ac_pred, pred_dir */
624 625
        FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table     , mb_array_size * sizeof(uint8_t), fail)
        FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail)
626
    }
627

628 629 630
    if (s->h263_pred || s->h263_plus || !s->encoding) {
        /* dc values */
        //MN: we need these for error resilience of intra-frames
631
        FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
632 633
        s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
        s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
634 635
        s->dc_val[2] = s->dc_val[1] + c_size;
        for(i=0;i<yc_size;i++)
636
            s->dc_val_base[i] = 1024;
637 638
    }

639
    /* which mb is a intra block */
640
    FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
641
    memset(s->mbintra_table, 1, mb_array_size);
642

643
    /* init macroblock skip table */
644
    FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size+2, fail);
645
    //Note the +1 is for a quicker mpeg4 slice_end detection
646
    FF_ALLOCZ_OR_GOTO(s->avctx, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE, fail);
647

648
    s->parse_context.state= -1;
649 650
    if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
       s->visualization_buffer[0] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
651 652
       s->visualization_buffer[1] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
       s->visualization_buffer[2] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
653
    }
654

Fabrice Bellard's avatar
Fabrice Bellard committed
655
    s->context_initialized = 1;
656 657

    s->thread_context[0]= s;
658
    threads = s->avctx->thread_count;
659 660

    for(i=1; i<threads; i++){
661 662 663 664
        s->thread_context[i]= av_malloc(sizeof(MpegEncContext));
        memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
    }

665
    for(i=0; i<threads; i++){
666 667 668 669 670 671
        if(init_duplicate_context(s->thread_context[i], s) < 0)
           goto fail;
        s->thread_context[i]->start_mb_y= (s->mb_height*(i  ) + s->avctx->thread_count/2) / s->avctx->thread_count;
        s->thread_context[i]->end_mb_y  = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count;
    }

Fabrice Bellard's avatar
Fabrice Bellard committed
672 673
    return 0;
 fail:
674
    MPV_common_end(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
675 676 677 678 679 680
    return -1;
}

/* init common structure for both encoder and decoder */
void MPV_common_end(MpegEncContext *s)
{
681
    int i, j, k;
Fabrice Bellard's avatar
Fabrice Bellard committed
682

683 684 685 686 687 688 689
    for(i=0; i<s->avctx->thread_count; i++){
        free_duplicate_context(s->thread_context[i]);
    }
    for(i=1; i<s->avctx->thread_count; i++){
        av_freep(&s->thread_context[i]);
    }

690 691 692
    av_freep(&s->parse_context.buffer);
    s->parse_context.buffer_size=0;

693
    av_freep(&s->mb_type);
694 695 696 697 698 699 700 701 702 703 704 705
    av_freep(&s->p_mv_table_base);
    av_freep(&s->b_forw_mv_table_base);
    av_freep(&s->b_back_mv_table_base);
    av_freep(&s->b_bidir_forw_mv_table_base);
    av_freep(&s->b_bidir_back_mv_table_base);
    av_freep(&s->b_direct_mv_table_base);
    s->p_mv_table= NULL;
    s->b_forw_mv_table= NULL;
    s->b_back_mv_table= NULL;
    s->b_bidir_forw_mv_table= NULL;
    s->b_bidir_back_mv_table= NULL;
    s->b_direct_mv_table= NULL;
706 707 708 709 710 711 712 713 714 715 716 717
    for(i=0; i<2; i++){
        for(j=0; j<2; j++){
            for(k=0; k<2; k++){
                av_freep(&s->b_field_mv_table_base[i][j][k]);
                s->b_field_mv_table[i][j][k]=NULL;
            }
            av_freep(&s->b_field_select_table[i][j]);
            av_freep(&s->p_field_mv_table_base[i][j]);
            s->p_field_mv_table[i][j]=NULL;
        }
        av_freep(&s->p_field_select_table[i]);
    }
718

719 720 721
    av_freep(&s->dc_val_base);
    av_freep(&s->ac_val_base);
    av_freep(&s->coded_block_base);
722
    av_freep(&s->mbintra_table);
723 724
    av_freep(&s->cbp_table);
    av_freep(&s->pred_dir_table);
725

726
    av_freep(&s->mbskip_table);
727
    av_freep(&s->prev_pict_types);
728
    av_freep(&s->bitstream_buffer);
729 730
    s->allocated_bitstream_buffer_size=0;

Michael Niedermayer's avatar
Michael Niedermayer committed
731
    av_freep(&s->avctx->stats_out);
732
    av_freep(&s->ac_stats);
733
    av_freep(&s->error_status_table);