mpegvideo.c 89.2 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
 * 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 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"
Benoit Fouet's avatar
Benoit Fouet committed
33
#include "mpegvideo_common.h"
34
#include "mjpegenc.h"
35
#include "msmpeg4.h"
Michael Niedermayer's avatar
Michael Niedermayer committed
36
#include "faandct.h"
37
#include <limits.h>
Fabrice Bellard's avatar
Fabrice Bellard committed
38

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

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

Ivan Kalvachev's avatar
Ivan Kalvachev committed
58 59 60
#ifdef HAVE_XVMC
extern int  XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx);
extern void XVMC_field_end(MpegEncContext *s);
61
extern void XVMC_decode_mb(MpegEncContext *s);
Ivan Kalvachev's avatar
Ivan Kalvachev committed
62 63
#endif

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

Fabrice Bellard's avatar
Fabrice Bellard committed
66 67 68 69 70 71

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

//#define DEBUG

72

Michael Niedermayer's avatar
Michael Niedermayer committed
73 74 75 76 77
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
};

Michael Niedermayer's avatar
Michael Niedermayer committed
78
void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){
79 80
    int i;
    int end;
81

Michael Niedermayer's avatar
Michael Niedermayer committed
82
    st->scantable= src_scantable;
83 84 85 86

    for(i=0; i<64; i++){
        int j;
        j = src_scantable[i];
Michael Niedermayer's avatar
Michael Niedermayer committed
87
        st->permutated[i] = permutation[j];
88 89 90
#ifdef ARCH_POWERPC
        st->inverse[j] = i;
#endif
91
    }
92

93 94 95 96 97 98 99 100 101
    end=-1;
    for(i=0; i<64; i++){
        int j;
        j = st->permutated[i];
        if(j>end) end=j;
        st->raster_end[i]= end;
    }
}

Michael Niedermayer's avatar
Michael Niedermayer committed
102 103 104
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
105 106 107 108
    assert(p<=end);
    if(p>=end)
        return end;

Michael Niedermayer's avatar
Michael Niedermayer committed
109 110 111 112 113 114 115 116
    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
117 118 119
        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
120 121 122 123 124 125
        else{
            p++;
            break;
        }
    }

Michael Niedermayer's avatar
Michael Niedermayer committed
126
    p= FFMIN(p, end)-4;
127
    *state= AV_RB32(p);
Michael Niedermayer's avatar
Michael Niedermayer committed
128 129 130 131

    return p+4;
}

132
/* init common dct for both encoder and decoder */
133
static int DCT_common_init(MpegEncContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
134
{
135 136 137 138 139
    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;
140 141
    if(s->flags & CODEC_FLAG_BITEXACT)
        s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
142
    s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
143

144
#if defined(HAVE_MMX)
145
    MPV_common_init_mmx(s);
146
#elif defined(ARCH_ALPHA)
147
    MPV_common_init_axp(s);
148
#elif defined(HAVE_MLIB)
149
    MPV_common_init_mlib(s);
150
#elif defined(HAVE_MMI)
151
    MPV_common_init_mmi(s);
152
#elif defined(ARCH_ARMV4L)
153
    MPV_common_init_armv4l(s);
154
#elif defined(ARCH_POWERPC)
155
    MPV_common_init_ppc(s);
156
#elif defined(ARCH_BFIN)
157 158
    MPV_common_init_bfin(s);
#endif
159

160
    /* load & permutate scantables
161
       note: only wmv uses different ones
162
    */
163 164 165 166 167 168 169
    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
170 171
    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);
172

173 174 175
    return 0;
}

Benoit Fouet's avatar
Benoit Fouet committed
176
void copy_picture(Picture *dst, Picture *src){
177 178 179 180
    *dst = *src;
    dst->type= FF_BUFFER_TYPE_COPY;
}

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
181
/**
Michael Niedermayer's avatar
Michael Niedermayer committed
182 183
 * allocates a Picture
 * The pixels are allocated/set by calling get_buffer() if shared=0
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
184
 */
Benoit Fouet's avatar
Benoit Fouet committed
185
int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
Diego Biurrun's avatar
Diego Biurrun committed
186
    const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) does not sig11
187
    const int mb_array_size= s->mb_stride*s->mb_height;
188 189
    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
190
    int i;
Daniel Kristjansson's avatar
Daniel Kristjansson committed
191
    int r= -1;
192

Michael Niedermayer's avatar
Michael Niedermayer committed
193 194 195 196 197 198
    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]);
199

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

Michael Niedermayer's avatar
Michael Niedermayer committed
202
        if(r<0 || !pic->age || !pic->type || !pic->data[0]){
203
            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
204 205 206 207
            return -1;
        }

        if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
208
            av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
Daniel Kristjansson's avatar
Daniel Kristjansson committed
209
            s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
Michael Niedermayer's avatar
Michael Niedermayer committed
210 211 212 213
            return -1;
        }

        if(pic->linesize[1] != pic->linesize[2]){
Diego Biurrun's avatar
Diego Biurrun committed
214
            av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride mismatch)\n");
Daniel Kristjansson's avatar
Daniel Kristjansson committed
215
            s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
Michael Niedermayer's avatar
Michael Niedermayer committed
216 217 218 219 220
            return -1;
        }

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

Michael Niedermayer's avatar
Michael Niedermayer committed
223
    if(pic->qscale_table==NULL){
224
        if (s->encoding) {
225 226 227
            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
228
        }
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
229

230
        CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2) //the +2 is for the slice end check
231
        CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
232
        CHECKED_ALLOCZ(pic->mb_type_base , big_mb_num    * sizeof(uint32_t))
233
        pic->mb_type= pic->mb_type_base + s->mb_stride+1;
Michael Niedermayer's avatar
Michael Niedermayer committed
234 235
        if(s->out_format == FMT_H264){
            for(i=0; i<2; i++){
Michael Niedermayer's avatar
Michael Niedermayer committed
236 237
                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;
238
                CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
239
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
240
            pic->motion_subsample_log2= 2;
241
        }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_MV) || (s->avctx->debug_mv)){
242
            for(i=0; i<2; i++){
Michael Niedermayer's avatar
Michael Niedermayer committed
243 244
                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;
245
                CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
Michael Niedermayer's avatar
Michael Niedermayer committed
246
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
247
            pic->motion_subsample_log2= 3;
Michael Niedermayer's avatar
Michael Niedermayer committed
248
        }
249 250 251
        if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
            CHECKED_ALLOCZ(pic->dct_coeff, 64 * mb_array_size * sizeof(DCTELEM)*6)
        }
252
        pic->qstride= s->mb_stride;
253
        CHECKED_ALLOCZ(pic->pan_scan , 1 * sizeof(AVPanScan))
Michael Niedermayer's avatar
Michael Niedermayer committed
254
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
255

Diego Biurrun's avatar
Diego Biurrun committed
256 257
    /* It might be nicer if the application would keep track of these
     * but it would require an API change. */
258 259 260
    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)
Diego Biurrun's avatar
Diego Biurrun committed
261
        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.
262

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
263 264
    return 0;
fail: //for the CHECKED_ALLOCZ macro
Daniel Kristjansson's avatar
Daniel Kristjansson committed
265 266
    if(r>=0)
        s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
267 268 269
    return -1;
}

Michael Niedermayer's avatar
Michael Niedermayer committed
270 271 272
/**
 * deallocates a picture
 */
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
273 274
static void free_picture(MpegEncContext *s, Picture *pic){
    int i;
Michael Niedermayer's avatar
Michael Niedermayer committed
275 276

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

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
280 281 282 283 284
    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
285
    av_freep(&pic->mb_type_base);
286
    av_freep(&pic->dct_coeff);
287
    av_freep(&pic->pan_scan);
Michael Niedermayer's avatar
Michael Niedermayer committed
288 289
    pic->mb_type= NULL;
    for(i=0; i<2; i++){
290
        av_freep(&pic->motion_val_base[i]);
Michael Niedermayer's avatar
Michael Niedermayer committed
291 292
        av_freep(&pic->ref_index[i]);
    }
293

294
    if(pic->type == FF_BUFFER_TYPE_SHARED){
Michael Niedermayer's avatar
Michael Niedermayer committed
295 296 297 298
        for(i=0; i<4; i++){
            pic->base[i]=
            pic->data[i]= NULL;
        }
299
        pic->type= 0;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
300 301 302
    }
}

303 304 305
static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base){
    int i;

306
    // edge emu needs blocksize + filter length - 1 (=17x17 for halfpel / 21x21 for h264)
307 308
    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;
309

Diego Biurrun's avatar
Diego Biurrun committed
310
     //FIXME should be linesize instead of s->width*2 but that is not known before get_buffer()
311
    CHECKED_ALLOCZ(s->me.scratchpad,  (s->width+64)*4*16*2*sizeof(uint8_t))
312 313 314 315 316 317 318 319 320
    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))
        }
321
    }
322
    CHECKED_ALLOCZ(s->blocks, 64*12*2 * sizeof(DCTELEM))
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
    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);
338 339
    s->rd_scratchpad=
    s->b_scratchpad=
340
    s->obmc_scratchpad= NULL;
341

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    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);
366 367
    COPY(dct_count[0]);
    COPY(dct_count[1]);
368 369 370
#undef COPY
}

371
void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src){
372
    MpegEncContext bak;
373
    int i;
374 375 376 377 378
    //FIXME copy only needed parts
//START_TIMER
    backup_duplicate_context(&bak, dst);
    memcpy(dst, src, sizeof(MpegEncContext));
    backup_duplicate_context(dst, &bak);
379 380 381
    for(i=0;i<12;i++){
        dst->pblocks[i] = (short *)(&dst->block[i]);
    }
382 383 384
//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
385 386 387 388
/**
 * 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
389
void MPV_common_defaults(MpegEncContext *s){
Michael Niedermayer's avatar
Michael Niedermayer committed
390 391 392 393 394 395 396 397 398 399 400 401
    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;
402 403 404

    s->f_code = 1;
    s->b_code = 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
405 406 407 408 409 410 411 412 413 414
}

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

415
/**
Michael Niedermayer's avatar
Michael Niedermayer committed
416 417 418
 * init common structure for both encoder and decoder.
 * this assumes that some variables like width/height are already set
 */
419 420
int MPV_common_init(MpegEncContext *s)
{
421
    int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y, threads;
422

423 424 425
    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
426 427 428 429
        av_log(s->avctx, AV_LOG_ERROR, "too many threads\n");
        return -1;
    }

430 431 432
    if((s->width || s->height) && avcodec_check_dimensions(s->avctx, s->width, s->height))
        return -1;

433
    dsputil_init(&s->dsp, s->avctx);
434
    DCT_common_init(s);
435

436
    s->flags= s->avctx->flags;
437
    s->flags2= s->avctx->flags2;
438

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
439
    s->mb_width  = (s->width  + 15) / 16;
440
    s->mb_stride = s->mb_width + 1;
441 442
    s->b8_stride = s->mb_width*2 + 1;
    s->b4_stride = s->mb_width*4 + 1;
443
    mb_array_size= s->mb_height * s->mb_stride;
444
    mv_table_size= (s->mb_height+2) * s->mb_stride + 1;
445

446 447 448 449
    /* set chroma shifts */
    avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,&(s->chroma_x_shift),
                                                    &(s->chroma_y_shift) );

450 451 452
    /* 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;
453 454

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

456 457 458
    s->block_wrap[0]=
    s->block_wrap[1]=
    s->block_wrap[2]=
459
    s->block_wrap[3]= s->b8_stride;
460
    s->block_wrap[4]=
461
    s->block_wrap[5]= s->mb_stride;
462

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

467
    /* convert fourcc to upper case */
468
    s->codec_tag=          toupper( s->avctx->codec_tag     &0xFF)
469
                        + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
470
                        + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16)
471
                        + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
472

473
    s->stream_codec_tag=          toupper( s->avctx->stream_codec_tag     &0xFF)
474
                               + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 )
475
                               + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16)
476 477
                               + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24);

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

480 481 482 483 484 485 486
    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?
487

488
    if (s->encoding) {
489
        /* Allocate MV tables */
490 491 492 493 494 495 496 497 498 499 500 501
        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;
502

503 504 505
        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
506
        CHECKED_ALLOCZ(s->avctx->stats_out, 256);
507 508

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

511
        CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int))
512

513 514
        CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int))
        CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int))
515 516
        CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t))
        CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t))
517 518
        CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
        CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
519

520 521 522
        if(s->avctx->noise_reduction){
            CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t))
        }
523
    }
524 525
    CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))

526
    CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
527

528
    if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){
529
        /* interlaced direct mode decoding tables */
530 531 532 533 534 535 536 537 538 539 540 541 542
            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
543
    }
544
    if (s->out_format == FMT_H263) {
Fabrice Bellard's avatar
Fabrice Bellard committed
545
        /* ac values */
546 547 548
        CHECKED_ALLOCZ(s->ac_val_base, yc_size * sizeof(int16_t) * 16);
        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
549
        s->ac_val[2] = s->ac_val[1] + c_size;
550

Fabrice Bellard's avatar
Fabrice Bellard committed
551
        /* cbp values */
552 553
        CHECKED_ALLOCZ(s->coded_block_base, y_size);
        s->coded_block= s->coded_block_base + s->b8_stride + 1;
554

555
        /* cbp, ac_pred, pred_dir */
556 557
        CHECKED_ALLOCZ(s->cbp_table  , mb_array_size * sizeof(uint8_t))
        CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))
558
    }
559

560 561 562
    if (s->h263_pred || s->h263_plus || !s->encoding) {
        /* dc values */
        //MN: we need these for error resilience of intra-frames
563 564 565
        CHECKED_ALLOCZ(s->dc_val_base, yc_size * sizeof(int16_t));
        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;
566 567
        s->dc_val[2] = s->dc_val[1] + c_size;
        for(i=0;i<yc_size;i++)
568
            s->dc_val_base[i] = 1024;
569 570
    }

571
    /* which mb is a intra block */
572 573
    CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);
    memset(s->mbintra_table, 1, mb_array_size);
574

575
    /* init macroblock skip table */
576
    CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);
577
    //Note the +1 is for a quicker mpeg4 slice_end detection
578
    CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
579

580
    s->parse_context.state= -1;
581 582 583 584 585
    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);
       s->visualization_buffer[1] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH);
       s->visualization_buffer[2] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH);
    }
586

Fabrice Bellard's avatar
Fabrice Bellard committed
587
    s->context_initialized = 1;
588 589

    s->thread_context[0]= s;
590 591 592 593 594
    /* h264 does thread context setup itself, but it needs context[0]
     * to be fully initialized for the error resilience code */
    threads = s->codec_id == CODEC_ID_H264 ? 1 : s->avctx->thread_count;

    for(i=1; i<threads; i++){
595 596 597 598
        s->thread_context[i]= av_malloc(sizeof(MpegEncContext));
        memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
    }

599
    for(i=0; i<threads; i++){
600 601 602 603 604 605
        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
606 607
    return 0;
 fail:
608
    MPV_common_end(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
609 610 611 612 613 614
    return -1;
}

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

617 618 619 620 621 622 623
    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]);
    }

624 625 626
    av_freep(&s->parse_context.buffer);
    s->parse_context.buffer_size=0;

627
    av_freep(&s->mb_type);
628 629 630 631 632 633 634 635 636 637 638 639
    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;
640 641 642 643 644 645 646 647 648 649 650 651
    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]);
    }
652

653 654 655
    av_freep(&s->dc_val_base);
    av_freep(&s->ac_val_base);
    av_freep(&s->coded_block_base);
656
    av_freep(&s->mbintra_table);
657 658
    av_freep(&s->cbp_table);
    av_freep(&s->pred_dir_table);
659

660
    av_freep(&s->mbskip_table);
661
    av_freep(&s->prev_pict_types);
662
    av_freep(&s->bitstream_buffer);
663 664
    s->allocated_bitstream_buffer_size=0;

Michael Niedermayer's avatar
Michael Niedermayer committed
665
    av_freep(&s->avctx->stats_out);
666
    av_freep(&s->ac_stats);
667
    av_freep(&s->error_status_table);
668
    av_freep(&s->mb_index2xy);
669
    av_freep(&s->lambda_table);
670 671
    av_freep(&s->q_intra_matrix);
    av_freep(&s->q_inter_matrix);
672 673
    av_freep(&s->q_intra_matrix16);
    av_freep(&s->q_inter_matrix16);
674 675
    av_freep(&s->input_picture);
    av_freep(&s->reordered_input_picture);
676
    av_freep(&s->dct_offset);
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
677

Michael Niedermayer's avatar
Michael Niedermayer committed
678 679 680 681
    if(s->picture){
        for(i=0; i<MAX_PICTURE_COUNT; i++){
            free_picture(s, &s->picture[i]);
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
682
    }
683
    av_freep(&s->picture);
Fabrice Bellard's avatar
Fabrice Bellard committed
684
    s->context_initialized = 0;
685 686 687
    s->last_picture_ptr=
    s->next_picture_ptr=
    s->current_picture_ptr= NULL;
688
    s->linesize= s->uvlinesize= 0;
689

690
    for(i=0; i<3; i++)
691
        av_freep(&s->visualization_buffer[i]);
692 693

    avcodec_default_free_buffers(s->avctx);
Fabrice Bellard's avatar
Fabrice Bellard committed
694 695
}

696
void init_rl(RLTable *rl, uint8_t static_store[2][2*MAX_RUN + MAX_LEVEL + 3])
Michael Niedermayer's avatar
Michael Niedermayer committed
697
{
698 699
    int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
    uint8_t index_run[MAX_RUN+1];
Michael Niedermayer's avatar
Michael Niedermayer committed
700 701
    int last, run, level, start, end, i;

702
    /* If table is static, we can quit if rl->max_level[0] is not NULL */
703
    if(static_store && rl->max_level[0])
704 705
        return;

Michael Niedermayer's avatar
Michael Niedermayer committed
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
    /* compute max_level[], max_run[] and index_run[] */
    for(last=0;last<2;last++) {
        if (last == 0) {
            start = 0;
            end = rl->last;
        } else {
            start = rl->last;
            end = rl->n;
        }

        memset(max_level, 0, MAX_RUN + 1);
        memset(max_run, 0, MAX_LEVEL + 1);
        memset(index_run, rl->n, MAX_RUN + 1);
        for(i=start;i<end;i++) {
            run = rl->table_run[i];
            level = rl->table_level[i];
            if (index_run[run] == rl->n)
                index_run[run] = i;
            if (level > max_level[run])
                max_level[run] = level;
            if (run > max_run[level])
                max_run[level] = run;
        }
729 730
        if(static_store)
            rl->max_level[last] = static_store[last];
731 732
        else
            rl->max_level[last] = av_malloc(MAX_RUN + 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
733
        memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
734 735
        if(static_store)
            rl->max_run[last] = static_store[last] + MAX_RUN + 1;
736 737
        else
            rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
738
        memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
739 740
        if(static_store)
            rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
741 742
        else
            rl->index_run[last] = av_malloc(MAX_RUN + 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
743 744 745 746
        memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
    }
}

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
void init_vlc_rl(RLTable *rl, int use_static)
{
    int i, q;

    /* Return if static table is already initialized */
    if(use_static && rl->rl_vlc[0])
        return;

    init_vlc(&rl->vlc, 9, rl->n + 1,
             &rl->table_vlc[0][1], 4, 2,
             &rl->table_vlc[0][0], 4, 2, use_static);


    for(q=0; q<32; q++){
        int qmul= q*2;
        int qadd= (q-1)|1;

        if(q==0){
            qmul=1;
            qadd=0;
        }
        if(use_static)
            rl->rl_vlc[q]= av_mallocz_static(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
        else
            rl->rl_vlc[q]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
        for(i=0; i<rl->vlc.table_size; i++){
            int code= rl->vlc.table[i][0];
            int len = rl->vlc.table[i][1];
            int level, run;

            if(len==0){ // illegal code
                run= 66;
                level= MAX_LEVEL;
            }else if(len<0){ //more bits needed
                run= 0;
                level= code;
            }else{
                if(code==rl->n){ //esc
                    run= 66;
                    level= 0;
                }else{
                    run=   rl->table_run  [code] + 1;
                    level= rl->table_level[code] * qmul + qadd;
                    if(code >= rl->last) run+=192;
                }
            }
            rl->rl_vlc[q][i].len= len;
            rl->rl_vlc[q][i].level= level;
            rl->rl_vlc[q][i].run= run;
        }
    }
}

Fabrice Bellard's avatar
Fabrice Bellard committed
800
/* draw the edges of width 'w' of an image of size width, height */
801
//FIXME check that this is ok for mpeg4 interlaced
802
static void draw_edges_c(uint8_t *buf, int wrap, int width, int height,