mpegvideo.c 113 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.
Fabrice Bellard's avatar
Fabrice Bellard committed
4
 *
Fabrice Bellard's avatar
Fabrice Bellard committed
5 6 7 8
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
9
 *
Fabrice Bellard's avatar
Fabrice Bellard committed
10
 * This library is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Fabrice Bellard's avatar
Fabrice Bellard committed
12 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
14
 *
Fabrice Bellard's avatar
Fabrice Bellard committed
15 16 17
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Michael Niedermayer's avatar
Michael Niedermayer committed
18
 *
19
 * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
Fabrice Bellard's avatar
Fabrice Bellard committed
20
 */
21 22
 
#include <ctype.h>
Fabrice Bellard's avatar
Fabrice Bellard committed
23 24 25
#include "avcodec.h"
#include "dsputil.h"
#include "mpegvideo.h"
26
#include "simple_idct.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
27

28 29 30 31
#ifdef USE_FASTMEMCPY
#include "fastmemcpy.h"
#endif

32 33 34
//#undef NDEBUG
//#include <assert.h>

35 36 37
static void encode_picture(MpegEncContext *s, int picture_number);
static void dct_unquantize_mpeg1_c(MpegEncContext *s, 
                                   DCTELEM *block, int n, int qscale);
38 39
static void dct_unquantize_mpeg2_c(MpegEncContext *s,
                                   DCTELEM *block, int n, int qscale);
40 41
static void dct_unquantize_h263_c(MpegEncContext *s, 
                                  DCTELEM *block, int n, int qscale);
Michael Niedermayer's avatar
Michael Niedermayer committed
42
static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w);
43
static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
Michael Niedermayer's avatar
Michael Niedermayer committed
44 45

void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c;
46 47
static void emulated_edge_mc(MpegEncContext *s, UINT8 *src, int linesize, int block_w, int block_h, 
                                    int src_x, int src_y, int w, int h);
Michael Niedermayer's avatar
Michael Niedermayer committed
48

Fabrice Bellard's avatar
Fabrice Bellard committed
49 50 51 52 53 54 55
#define EDGE_WIDTH 16

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

//#define DEBUG

56

Fabrice Bellard's avatar
Fabrice Bellard committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
/* for jpeg fast DCT */
#define CONST_BITS 14

static const unsigned short aanscales[64] = {
    /* 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,
    8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
    4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
};

static UINT8 h263_chroma_roundtab[16] = {
    0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
};

76
static UINT16 default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
77 78
static UINT8 default_fcode_tab[MAX_MV*2+1];

79 80 81
/* default motion estimation */
int motion_estimation_method = ME_EPZS;

82
static void convert_matrix(MpegEncContext *s, int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64],
83
                           const UINT16 *quant_matrix, int bias, int qmin, int qmax)
Fabrice Bellard's avatar
Fabrice Bellard committed
84
{
85 86
    int qscale;

87
    for(qscale=qmin; qscale<=qmax; qscale++){
88
        int i;
89 90
        if (s->fdct == ff_jpeg_fdct_islow) {
            for(i=0;i<64;i++) {
91
                const int j= s->idct_permutation[i];
92 93 94 95 96
                /* 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 */
                
Michael Niedermayer's avatar
Michael Niedermayer committed
97
                qmat[qscale][j] = (int)((UINT64_C(1) << QMAT_SHIFT) / 
98 99 100
                                (qscale * quant_matrix[j]));
            }
        } else if (s->fdct == fdct_ifast) {
101
            for(i=0;i<64;i++) {
102
                const int j= s->idct_permutation[i];
103 104 105 106 107
                /* 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 */
                
Michael Niedermayer's avatar
Michael Niedermayer committed
108
                qmat[qscale][j] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) / 
109 110 111 112
                                (aanscales[i] * qscale * quant_matrix[j]));
            }
        } else {
            for(i=0;i<64;i++) {
113
                const int j= s->idct_permutation[i];
114 115 116 117 118 119
                /* 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
                */
                qmat  [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
120
                qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
121 122 123 124

                if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1;
                qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]);
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
125 126 127
        }
    }
}
128 129 130 131 132 133 134 135 136
// move into common.c perhaps 
#define CHECKED_ALLOCZ(p, size)\
{\
    p= av_mallocz(size);\
    if(p==NULL){\
        perror("malloc");\
        goto fail;\
    }\
}
137

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
void ff_init_scantable(MpegEncContext *s, ScanTable *st, const UINT8 *src_scantable){
    int i;
    int end;

    for(i=0; i<64; i++){
        int j;
        j = src_scantable[i];
        st->permutated[i] = s->idct_permutation[j];
    }
    
    end=-1;
    for(i=0; i<64; i++){
        int j;
        j = st->permutated[i];
        if(j>end) end=j;
        st->raster_end[i]= end;
    }
}

/* XXX: those functions should be suppressed ASAP when all IDCTs are
   converted */
static void ff_jref_idct_put(UINT8 *dest, int line_size, DCTELEM *block)
{
    j_rev_dct (block);
    put_pixels_clamped(block, dest, line_size);
}
static void ff_jref_idct_add(UINT8 *dest, int line_size, DCTELEM *block)
{
    j_rev_dct (block);
    add_pixels_clamped(block, dest, line_size);
}
Fabrice Bellard's avatar
Fabrice Bellard committed
169

170 171
/* init common dct for both encoder and decoder */
int DCT_common_init(MpegEncContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
172
{
173
    int i;
Fabrice Bellard's avatar
Fabrice Bellard committed
174

175
    s->dct_unquantize_h263 = dct_unquantize_h263_c;
176 177
    s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
    s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
178 179 180 181 182
    s->dct_quantize= dct_quantize_c;

    if(s->avctx->dct_algo==FF_DCT_FASTINT)
        s->fdct = fdct_ifast;
    else
183 184 185 186 187 188 189 190 191 192 193 194 195
        s->fdct = ff_jpeg_fdct_islow; //slow/accurate/default

    if(s->avctx->idct_algo==FF_IDCT_INT){
        s->idct_put= ff_jref_idct_put;
        s->idct_add= ff_jref_idct_add;
        for(i=0; i<64; i++)
            s->idct_permutation[i]= (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
    }else{ //accurate/default
        s->idct_put= simple_idct_put;
        s->idct_add= simple_idct_add;
        for(i=0; i<64; i++)
            s->idct_permutation[i]= i;
    }
196 197 198
        
#ifdef HAVE_MMX
    MPV_common_init_mmx(s);
199 200 201
#endif
#ifdef ARCH_ALPHA
    MPV_common_init_axp(s);
Nick Kurshev's avatar
Nick Kurshev committed
202
#endif
203 204 205
#ifdef HAVE_MLIB
    MPV_common_init_mlib(s);
#endif
206 207 208
#ifdef HAVE_MMI
    MPV_common_init_mmi(s);
#endif
209 210 211 212
#ifdef ARCH_ARMV4L
    MPV_common_init_armv4l();
#endif

213 214 215 216 217 218 219 220

    /* load & permutate scantables
       note: only wmv uses differnt ones 
    */
    ff_init_scantable(s, &s->inter_scantable  , ff_zigzag_direct);
    ff_init_scantable(s, &s->intra_scantable  , ff_zigzag_direct);
    ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan);
    ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
221

222 223 224 225 226 227 228 229 230 231
    return 0;
}

/* init common structure for both encoder and decoder */
int MPV_common_init(MpegEncContext *s)
{
    int c_size, i;
    UINT8 *pict;

    DCT_common_init(s);
232 233
    
    s->flags= s->avctx->flags;
234

Fabrice Bellard's avatar
Fabrice Bellard committed
235 236
    s->mb_width = (s->width + 15) / 16;
    s->mb_height = (s->height + 15) / 16;
237 238 239 240
    
    /* 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;
241 242 243 244 245 246
    
    /* convert fourcc to upper case */
    s->avctx->fourcc=   toupper( s->avctx->fourcc     &0xFF)          
                     + (toupper((s->avctx->fourcc>>8 )&0xFF)<<8 )
                     + (toupper((s->avctx->fourcc>>16)&0xFF)<<16) 
                     + (toupper((s->avctx->fourcc>>24)&0xFF)<<24);
247

248
    s->mb_num = s->mb_width * s->mb_height;
249
    if(!(s->flags&CODEC_FLAG_DR1)){
Michael Niedermayer's avatar
Michael Niedermayer committed
250 251
      s->linesize   = s->mb_width * 16 + 2 * EDGE_WIDTH;
      s->uvlinesize = s->mb_width * 8  +     EDGE_WIDTH;
Fabrice Bellard's avatar
Fabrice Bellard committed
252

253
      for(i=0;i<3;i++) {
Fabrice Bellard's avatar
Fabrice Bellard committed
254 255 256 257 258
        int w, h, shift, pict_start;

        w = s->linesize;
        h = s->mb_height * 16 + 2 * EDGE_WIDTH;
        shift = (i == 0) ? 0 : 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
259 260
        c_size = (s->linesize>>shift) * (h >> shift);
        pict_start = (s->linesize>>shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
Fabrice Bellard's avatar
Fabrice Bellard committed
261

262
        CHECKED_ALLOCZ(pict, c_size)
Fabrice Bellard's avatar
Fabrice Bellard committed
263 264
        s->last_picture_base[i] = pict;
        s->last_picture[i] = pict + pict_start;
Michael Niedermayer's avatar
Michael Niedermayer committed
265
        if(i>0) memset(s->last_picture_base[i], 128, c_size);
Fabrice Bellard's avatar
Fabrice Bellard committed
266
    
267
        CHECKED_ALLOCZ(pict, c_size)
Fabrice Bellard's avatar
Fabrice Bellard committed
268 269
        s->next_picture_base[i] = pict;
        s->next_picture[i] = pict + pict_start;
Michael Niedermayer's avatar
Michael Niedermayer committed
270
        if(i>0) memset(s->next_picture_base[i], 128, c_size);
271
        
272 273 274
        if (s->has_b_frames || s->codec_id==CODEC_ID_MPEG4) {
        /* Note the MPEG4 stuff is here cuz of buggy encoders which dont set the low_delay flag but 
           do low-delay encoding, so we cant allways distinguish b-frame containing streams from low_delay streams */
275
            CHECKED_ALLOCZ(pict, c_size)
Fabrice Bellard's avatar
Fabrice Bellard committed
276 277
            s->aux_picture_base[i] = pict;
            s->aux_picture[i] = pict + pict_start;
Michael Niedermayer's avatar
Michael Niedermayer committed
278
            if(i>0) memset(s->aux_picture_base[i], 128, c_size);
Fabrice Bellard's avatar
Fabrice Bellard committed
279
        }
280
      }
Michael Niedermayer's avatar
Michael Niedermayer committed
281
      s->ip_buffer_count= 2;
Fabrice Bellard's avatar
Fabrice Bellard committed
282
    }
283
    
284
    CHECKED_ALLOCZ(s->edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
285
    
286
    if (s->encoding) {
287 288
        int j;
        int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
Juanjo's avatar
Juanjo committed
289
        
290 291
        CHECKED_ALLOCZ(s->mb_var   , s->mb_num * sizeof(INT16))
        CHECKED_ALLOCZ(s->mc_mb_var, s->mb_num * sizeof(INT16))
292
        CHECKED_ALLOCZ(s->mb_mean  , s->mb_num * sizeof(INT8))
293 294

        /* Allocate MV tables */
295 296 297 298 299 300 301 302 303 304 305 306 307
        CHECKED_ALLOCZ(s->p_mv_table            , mv_table_size * 2 * sizeof(INT16))
        CHECKED_ALLOCZ(s->b_forw_mv_table       , mv_table_size * 2 * sizeof(INT16))
        CHECKED_ALLOCZ(s->b_back_mv_table       , mv_table_size * 2 * sizeof(INT16))
        CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(INT16))
        CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(INT16))
        CHECKED_ALLOCZ(s->b_direct_forw_mv_table, mv_table_size * 2 * sizeof(INT16))
        CHECKED_ALLOCZ(s->b_direct_back_mv_table, mv_table_size * 2 * sizeof(INT16))
        CHECKED_ALLOCZ(s->b_direct_mv_table     , mv_table_size * 2 * sizeof(INT16))

        CHECKED_ALLOCZ(s->me_scratchpad,  s->linesize*16*3*sizeof(uint8_t))
        
        CHECKED_ALLOCZ(s->me_map      , ME_MAP_SIZE*sizeof(uint32_t))
        CHECKED_ALLOCZ(s->me_score_map, ME_MAP_SIZE*sizeof(uint16_t))
308

309 310 311 312 313 314 315 316 317 318 319
        if(s->max_b_frames){
            for(j=0; j<REORDER_BUFFER_SIZE; j++){
                int i;
                for(i=0;i<3;i++) {
                    int w, h, shift;

                    w = s->linesize;
                    h = s->mb_height * 16;
                    shift = (i == 0) ? 0 : 1;
                    c_size = (w >> shift) * (h >> shift);

320
                    CHECKED_ALLOCZ(pict, c_size);
321 322 323 324
                    s->picture_buffer[j][i] = pict;
                }
            }
        }
325 326 327 328 329

        if(s->codec_id==CODEC_ID_MPEG4){
            CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
            CHECKED_ALLOCZ(   s->pb2_buffer, PB_BUFFER_SIZE);
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
330
        
331 332 333
        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
334
        CHECKED_ALLOCZ(s->avctx->stats_out, 256);
335
    }
336 337
        
    CHECKED_ALLOCZ(s->error_status_table, s->mb_num*sizeof(UINT8))
338
    
339
    if (s->out_format == FMT_H263 || s->encoding) {
Fabrice Bellard's avatar
Fabrice Bellard committed
340
        int size;
341 342 343
        /* Allocate MB type table */
        CHECKED_ALLOCZ(s->mb_type  , s->mb_num * sizeof(UINT8))

Fabrice Bellard's avatar
Fabrice Bellard committed
344 345
        /* MV prediction */
        size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
346
        CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(INT16));
347 348 349
    }

    if(s->codec_id==CODEC_ID_MPEG4){
350
        /* interlaced direct mode decoding tables */
351 352
        CHECKED_ALLOCZ(s->field_mv_table, s->mb_num*2*2 * sizeof(INT16))
        CHECKED_ALLOCZ(s->field_select_table, s->mb_num*2* sizeof(INT8))
Fabrice Bellard's avatar
Fabrice Bellard committed
353
    }
354 355 356
    /* 4mv b frame decoding table */
    //note this is needed for h263 without b frames too (segfault on damaged streams otherwise)
    CHECKED_ALLOCZ(s->co_located_type_table, s->mb_num * sizeof(UINT8))
Fabrice Bellard's avatar
Fabrice Bellard committed
357

358
    if (s->h263_pred || s->h263_plus) {
Fabrice Bellard's avatar
Fabrice Bellard committed
359 360 361 362 363 364 365
        int y_size, c_size, i, size;
        
        /* dc values */

        y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
        c_size = (s->mb_width + 2) * (s->mb_height + 2);
        size = y_size + 2 * c_size;
366
        CHECKED_ALLOCZ(s->dc_val[0], size * sizeof(INT16));
Fabrice Bellard's avatar
Fabrice Bellard committed
367 368 369 370 371 372
        s->dc_val[1] = s->dc_val[0] + y_size;
        s->dc_val[2] = s->dc_val[1] + c_size;
        for(i=0;i<size;i++)
            s->dc_val[0][i] = 1024;

        /* ac values */
373
        CHECKED_ALLOCZ(s->ac_val[0], size * sizeof(INT16) * 16);
Fabrice Bellard's avatar
Fabrice Bellard committed
374 375 376 377
        s->ac_val[1] = s->ac_val[0] + y_size;
        s->ac_val[2] = s->ac_val[1] + c_size;
        
        /* cbp values */
378
        CHECKED_ALLOCZ(s->coded_block, y_size);
Michael Niedermayer's avatar
Michael Niedermayer committed
379 380
        
        /* divx501 bitstream reorder buffer */
381 382 383 384 385
        CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
        
        /* cbp, ac_pred, pred_dir */
        CHECKED_ALLOCZ(s->cbp_table  , s->mb_num * sizeof(UINT8))
        CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(UINT8))
386
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
387 388
    CHECKED_ALLOCZ(s->qscale_table  , s->mb_num * sizeof(UINT8))
    
389 390 391 392
    /* which mb is a intra block */
    CHECKED_ALLOCZ(s->mbintra_table, s->mb_num);
    memset(s->mbintra_table, 1, s->mb_num);
    
Fabrice Bellard's avatar
Fabrice Bellard committed
393 394
    /* default structure is frame */
    s->picture_structure = PICT_FRAME;
395
    
396
    /* init macroblock skip table */
397 398
    CHECKED_ALLOCZ(s->mbskip_table, s->mb_num+1);
    //Note the +1 is for a quicker mpeg4 slice_end detection
399
    
400
    s->block= s->blocks[0];
401

Fabrice Bellard's avatar
Fabrice Bellard committed
402 403 404
    s->context_initialized = 1;
    return 0;
 fail:
405
    MPV_common_end(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
406 407 408
    return -1;
}

409 410 411

//extern int sads;

Fabrice Bellard's avatar
Fabrice Bellard committed
412 413 414 415 416
/* init common structure for both encoder and decoder */
void MPV_common_end(MpegEncContext *s)
{
    int i;

417 418
    av_freep(&s->mb_type);
    av_freep(&s->mb_var);
419
    av_freep(&s->mc_mb_var);
420
    av_freep(&s->mb_mean);
421 422 423 424 425 426 427 428 429 430 431 432 433
    av_freep(&s->p_mv_table);
    av_freep(&s->b_forw_mv_table);
    av_freep(&s->b_back_mv_table);
    av_freep(&s->b_bidir_forw_mv_table);
    av_freep(&s->b_bidir_back_mv_table);
    av_freep(&s->b_direct_forw_mv_table);
    av_freep(&s->b_direct_back_mv_table);
    av_freep(&s->b_direct_mv_table);
    av_freep(&s->motion_val);
    av_freep(&s->dc_val[0]);
    av_freep(&s->ac_val[0]);
    av_freep(&s->coded_block);
    av_freep(&s->mbintra_table);
434 435 436
    av_freep(&s->cbp_table);
    av_freep(&s->pred_dir_table);
    av_freep(&s->qscale_table);
437
    av_freep(&s->me_scratchpad);
438 439 440
    av_freep(&s->me_map);
    av_freep(&s->me_score_map);
    
441 442
    av_freep(&s->mbskip_table);
    av_freep(&s->bitstream_buffer);
443 444
    av_freep(&s->tex_pb_buffer);
    av_freep(&s->pb2_buffer);
445
    av_freep(&s->edge_emu_buffer);
446 447 448
    av_freep(&s->co_located_type_table);
    av_freep(&s->field_mv_table);
    av_freep(&s->field_select_table);
Michael Niedermayer's avatar
Michael Niedermayer committed
449
    av_freep(&s->avctx->stats_out);
450
    av_freep(&s->ac_stats);
451
    av_freep(&s->error_status_table);
Michael Niedermayer's avatar
Michael Niedermayer committed
452
    
Fabrice Bellard's avatar
Fabrice Bellard committed
453
    for(i=0;i<3;i++) {
454
        int j;
455 456 457 458 459 460 461 462 463 464 465 466
        if(!(s->flags&CODEC_FLAG_DR1)){
            av_freep(&s->last_picture_base[i]);
            av_freep(&s->next_picture_base[i]);
            av_freep(&s->aux_picture_base[i]);
        }
        s->last_picture_base[i]=
        s->next_picture_base[i]=
        s->aux_picture_base [i] = NULL;
        s->last_picture[i]=
        s->next_picture[i]=
        s->aux_picture [i] = NULL;

467
        for(j=0; j<REORDER_BUFFER_SIZE; j++){
468
            av_freep(&s->picture_buffer[j][i]);
469
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
470 471 472 473 474 475 476 477
    }
    s->context_initialized = 0;
}

/* init video encoder */
int MPV_encode_init(AVCodecContext *avctx)
{
    MpegEncContext *s = avctx->priv_data;
478
    int i;
Fabrice Bellard's avatar
Fabrice Bellard committed
479

480 481
    avctx->pix_fmt = PIX_FMT_YUV420P;

Fabrice Bellard's avatar
Fabrice Bellard committed
482
    s->bit_rate = avctx->bit_rate;
483
    s->bit_rate_tolerance = avctx->bit_rate_tolerance;
Fabrice Bellard's avatar
Fabrice Bellard committed
484 485 486
    s->frame_rate = avctx->frame_rate;
    s->width = avctx->width;
    s->height = avctx->height;
487
    if(avctx->gop_size > 600){
488
        fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n");
489 490
        avctx->gop_size=600;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
491
    s->gop_size = avctx->gop_size;
492 493
    s->rtp_mode = avctx->rtp_mode;
    s->rtp_payload_size = avctx->rtp_payload_size;
494 495
    if (avctx->rtp_callback)
        s->rtp_callback = avctx->rtp_callback;
496 497 498 499 500
    s->qmin= avctx->qmin;
    s->qmax= avctx->qmax;
    s->max_qdiff= avctx->max_qdiff;
    s->qcompress= avctx->qcompress;
    s->qblur= avctx->qblur;
501
    s->avctx = avctx;
502
    s->aspect_ratio_info= avctx->aspect_ratio_info;
503 504 505 506 507
    if (avctx->aspect_ratio_info == FF_ASPECT_EXTENDED)
    {
	s->aspected_width = avctx->aspected_width;
	s->aspected_height = avctx->aspected_height;
    }
508
    s->flags= avctx->flags;
509
    s->max_b_frames= avctx->max_b_frames;
510
    s->b_frame_strategy= avctx->b_frame_strategy;
511
    s->codec_id= avctx->codec->id;
512 513 514 515
    s->luma_elim_threshold  = avctx->luma_elim_threshold;
    s->chroma_elim_threshold= avctx->chroma_elim_threshold;
    s->strict_std_compliance= avctx->strict_std_compliance;
    s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
516
    s->mpeg_quant= avctx->mpeg_quant;
517

Fabrice Bellard's avatar
Fabrice Bellard committed
518 519 520 521 522 523
    if (s->gop_size <= 1) {
        s->intra_only = 1;
        s->gop_size = 12;
    } else {
        s->intra_only = 0;
    }
524

525
    /* ME algorithm */
526 527 528 529 530
    if (avctx->me_method == 0)
        /* For compatibility */
        s->me_method = motion_estimation_method;
    else
        s->me_method = avctx->me_method;
531

532
    /* Fixed QSCALE */
Fabrice Bellard's avatar
Fabrice Bellard committed
533
    s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
534
    
535
    s->adaptive_quant= (   s->avctx->lumi_masking
536
                        || s->avctx->dark_masking
537 538 539 540
                        || s->avctx->temporal_cplx_masking 
                        || s->avctx->spatial_cplx_masking
                        || s->avctx->p_masking)
                       && !s->fixed_qscale;
541 542
    
    s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
543

Fabrice Bellard's avatar
Fabrice Bellard committed
544 545 546
    switch(avctx->codec->id) {
    case CODEC_ID_MPEG1VIDEO:
        s->out_format = FMT_MPEG1;
Michael Niedermayer's avatar
Michael Niedermayer committed
547
        avctx->delay=0; //FIXME not sure, should check the spec
Fabrice Bellard's avatar
Fabrice Bellard committed
548 549 550 551
        break;
    case CODEC_ID_MJPEG:
        s->out_format = FMT_MJPEG;
        s->intra_only = 1; /* force intra only for jpeg */
552
        s->mjpeg_write_tables = 1; /* write all tables */
553
	s->mjpeg_data_only_frames = 0; /* write all the needed headers */
554 555 556
        s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
        s->mjpeg_vsample[1] = 1; /* the only currently supported values */
        s->mjpeg_vsample[2] = 1; 
557
        s->mjpeg_hsample[0] = 2;
558 559
        s->mjpeg_hsample[1] = 1; 
        s->mjpeg_hsample[2] = 1; 
Fabrice Bellard's avatar
Fabrice Bellard committed
560 561
        if (mjpeg_init(s) < 0)
            return -1;
Michael Niedermayer's avatar
Michael Niedermayer committed
562
        avctx->delay=0;
Fabrice Bellard's avatar
Fabrice Bellard committed
563 564
        break;
    case CODEC_ID_H263:
565 566
        if (h263_get_picture_format(s->width, s->height) == 7) {
            printf("Input picture size isn't suitable for h263 codec! try h263+\n");
Fabrice Bellard's avatar
Fabrice Bellard committed
567
            return -1;
568
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
569
        s->out_format = FMT_H263;
Michael Niedermayer's avatar
Michael Niedermayer committed
570
        avctx->delay=0;
Fabrice Bellard's avatar
Fabrice Bellard committed
571 572 573
        break;
    case CODEC_ID_H263P:
        s->out_format = FMT_H263;
574 575
        s->rtp_mode = 1;
        s->rtp_payload_size = 1200; 
Fabrice Bellard's avatar
Fabrice Bellard committed
576
        s->h263_plus = 1;
577
        s->unrestricted_mv = 1;
578
        s->h263_aic = 1;
579 580 581 582
        
        /* These are just to be sure */
        s->umvplus = 0;
        s->umvplus_dec = 0;
Michael Niedermayer's avatar
Michael Niedermayer committed
583
        avctx->delay=0;
Fabrice Bellard's avatar
Fabrice Bellard committed
584 585 586 587
        break;
    case CODEC_ID_RV10:
        s->out_format = FMT_H263;
        s->h263_rv10 = 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
588
        avctx->delay=0;
Fabrice Bellard's avatar
Fabrice Bellard committed
589
        break;
Fabrice Bellard's avatar
Fabrice Bellard committed
590
    case CODEC_ID_MPEG4:
Fabrice Bellard's avatar
Fabrice Bellard committed
591 592 593
        s->out_format = FMT_H263;
        s->h263_pred = 1;
        s->unrestricted_mv = 1;
594
        s->has_b_frames= s->max_b_frames ? 1 : 0;
595
        s->low_delay= !s->has_b_frames;
596
        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
Fabrice Bellard's avatar
Fabrice Bellard committed
597
        break;
598
    case CODEC_ID_MSMPEG4V1:
Fabrice Bellard's avatar
Fabrice Bellard committed
599 600 601 602
        s->out_format = FMT_H263;
        s->h263_msmpeg4 = 1;
        s->h263_pred = 1;
        s->unrestricted_mv = 1;
603
        s->msmpeg4_version= 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
604
        avctx->delay=0;
605 606 607 608 609 610 611
        break;
    case CODEC_ID_MSMPEG4V2:
        s->out_format = FMT_H263;
        s->h263_msmpeg4 = 1;
        s->h263_pred = 1;
        s->unrestricted_mv = 1;
        s->msmpeg4_version= 2;
Michael Niedermayer's avatar
Michael Niedermayer committed
612
        avctx->delay=0;
613 614 615 616 617 618 619
        break;
    case CODEC_ID_MSMPEG4V3:
        s->out_format = FMT_H263;
        s->h263_msmpeg4 = 1;
        s->h263_pred = 1;
        s->unrestricted_mv = 1;
        s->msmpeg4_version= 3;
Michael Niedermayer's avatar
Michael Niedermayer committed
620
        avctx->delay=0;
Fabrice Bellard's avatar
Fabrice Bellard committed
621
        break;
Michael Niedermayer's avatar
Michael Niedermayer committed
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
    case CODEC_ID_WMV1:
        s->out_format = FMT_H263;
        s->h263_msmpeg4 = 1;
        s->h263_pred = 1;
        s->unrestricted_mv = 1;
        s->msmpeg4_version= 4;
        avctx->delay=0;
        break;
    case CODEC_ID_WMV2:
        s->out_format = FMT_H263;
        s->h263_msmpeg4 = 1;
        s->h263_pred = 1;
        s->unrestricted_mv = 1;
        s->msmpeg4_version= 5;
        avctx->delay=0;
        break;
Fabrice Bellard's avatar
Fabrice Bellard committed
638 639 640
    default:
        return -1;
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
641
    
642 643 644 645 646
    { /* set up some save defaults, some codecs might override them later */
        static int done=0;
        if(!done){
            int i;
            done=1;
647
            memset(default_mv_penalty, 0, sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1));
648 649 650 651 652 653 654 655 656
            memset(default_fcode_tab , 0, sizeof(UINT8)*(2*MAX_MV+1));

            for(i=-16; i<16; i++){
                default_fcode_tab[i + MAX_MV]= 1;
            }
        }
    }
    s->mv_penalty= default_mv_penalty;
    s->fcode_tab= default_fcode_tab;
657 658 659
    s->y_dc_scale_table=
    s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
 
660
    /* dont use mv_penalty table for crap MV as it would be confused */
661
    if (s->me_method < ME_EPZS) s->mv_penalty = default_mv_penalty;
662

663 664
    s->encoding = 1;

Fabrice Bellard's avatar
Fabrice Bellard committed
665 666 667 668
    /* init */
    if (MPV_common_init(s) < 0)
        return -1;
    
669 670 671 672 673 674 675
    if (s->out_format == FMT_H263)
        h263_encode_init(s);
    else if (s->out_format == FMT_MPEG1)
        ff_mpeg1_encode_init(s);
    if(s->msmpeg4_version)
        ff_msmpeg4_encode_init(s);

676 677
    /* init default q matrix */
    for(i=0;i<64;i++) {
678
        int j= s->idct_permutation[i];
679
        if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
680 681
            s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
            s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
682
        }else if(s->out_format == FMT_H263){
683 684
            s->intra_matrix[j] =
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
685
        }else{ /* mpeg1 */
686 687
            s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
688
        }
689 690 691
    }

    /* precompute matrix */
692
    /* for mjpeg, we do include qscale in the matrix */
693
    if (s->out_format != FMT_MJPEG) {
694
        convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias, 
695
                       s->intra_matrix, s->intra_quant_bias, 1, 31);
696
        convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias, 
697
                       s->inter_matrix, s->inter_quant_bias, 1, 31);
698 699
    }

700 701
    if(ff_rate_control_init(s) < 0)
        return -1;
Fabrice Bellard's avatar
Fabrice Bellard committed
702 703

    s->picture_number = 0;
704
    s->picture_in_gop_number = 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
705 706 707
    s->fake_picture_number = 0;
    /* motion detector init */
    s->f_code = 1;
708
    s->b_code = 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
709 710 711 712 713 714 715 716 717 718 719

    return 0;
}

int MPV_encode_end(AVCodecContext *avctx)
{
    MpegEncContext *s = avctx->priv_data;

#ifdef STATS
    print_stats();
#endif
720 721 722

    ff_rate_control_uninit(s);

Fabrice Bellard's avatar
Fabrice Bellard committed
723 724 725
    MPV_common_end(s);
    if (s->out_format == FMT_MJPEG)
        mjpeg_close(s);
726
      
Fabrice Bellard's avatar
Fabrice Bellard committed
727 728 729 730
    return 0;
}

/* draw the edges of width 'w' of an image of size width, height */
731
//FIXME check that this is ok for mpeg4 interlaced
Michael Niedermayer's avatar
Michael Niedermayer committed
732
static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
Fabrice Bellard's avatar
Fabrice Bellard committed
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
{
    UINT8 *ptr, *last_line;
    int i;

    last_line = buf + (height - 1) * wrap;
    for(i=0;i<w;i++) {
        /* top and bottom */
        memcpy(buf - (i + 1) * wrap, buf, width);
        memcpy(last_line + (i + 1) * wrap, last_line, width);
    }
    /* left and right */
    ptr = buf;
    for(i=0;i<height;i++) {
        memset(ptr - w, ptr[0], w);
        memset(ptr + width, ptr[width-1], w);
        ptr += wrap;
    }
    /* corners */
    for(i=0;i<w;i++) {
        memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
        memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
        memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
        memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
    }
}

/* generic function for encode/decode called before a frame is coded/decoded */
760
void MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
Fabrice Bellard's avatar
Fabrice Bellard committed
761 762 763 764
{
    int i;
    UINT8 *tmp;

765
    s->mb_skiped = 0;
766
    avctx->mbskip_table= s->mbskip_table;
767

768 769 770
    if(avctx->flags&CODEC_FLAG_DR1){
        avctx->get_buffer_callback(avctx, s->width, s->height, s->pict_type);

Michael Niedermayer's avatar
Michael Niedermayer committed
771 772 773
        s->linesize  = avctx->dr_stride;
        s->uvlinesize= avctx->dr_uvstride;
        s->ip_buffer_count= avctx->dr_ip_buffer_count;
774
    }
775
    avctx->dr_ip_buffer_count= s->ip_buffer_count;
776
    
Fabrice Bellard's avatar
Fabrice Bellard committed
777 778
    if (s->pict_type == B_TYPE) {
        for(i=0;i<3;i++) {
779 780
            if(avctx->flags&CODEC_FLAG_DR1)
                s->aux_picture[i]= avctx->dr_buffer[i];
781 782 783 784 785 786
            
            //FIXME the following should never be needed, the decoder should drop b frames if no reference is available
            if(s->next_picture[i]==NULL)
                s->next_picture[i]= s->aux_picture[i];
            if(s->last_picture[i]==NULL)
                s->last_picture[i]= s->next_picture[i];
787

Fabrice Bellard's avatar
Fabrice Bellard committed
788 789 790 791 792
            s->current_picture[i] = s->aux_picture[i];
        }
    } else {
        for(i=0;i<3;i++) {
            /* swap next and last */
793 794 795 796 797
            if(avctx->flags&CODEC_FLAG_DR1)
                tmp= avctx->dr_buffer[i];
            else
                tmp = s->last_picture[i];

Fabrice Bellard's avatar
Fabrice Bellard committed
798 799 800
            s->last_picture[i] = s->next_picture[i];
            s->next_picture[i] = tmp;
            s->current_picture[i] = tmp;
801

802 803 804
            if(s->last_picture[i]==NULL)
                s->last_picture[i]= s->next_picture[i];

805 806 807
            s->last_dr_opaque= s->next_dr_opaque;
            s->next_dr_opaque= avctx->dr_opaque_frame;

808
            if(s->has_b_frames && s->last_dr_opaque && s->codec_id!=CODEC_ID_SVQ1)
809 810 811
                avctx->dr_opaque_frame= s->last_dr_opaque;
            else
                avctx->dr_opaque_frame= s->next_dr_opaque;
Fabrice Bellard's avatar
Fabrice Bellard committed
812 813
        }
    }
814 815 816 817 818 819 820 821 822
    /* set dequantizer, we cant do it during init as it might change for mpeg4
       and we cant do it in the header decode as init isnt called for mpeg4 there yet */
    if(s->out_format == FMT_H263){
        if(s->mpeg_quant)
            s->dct_unquantize = s->dct_unquantize_mpeg2;
        else
            s->dct_unquantize = s->dct_unquantize_h263;
    }else 
        s->dct_unquantize = s->dct_unquantize_mpeg1;
Fabrice Bellard's avatar
Fabrice Bellard committed
823
}
824

Fabrice Bellard's avatar
Fabrice Bellard committed
825 826 827
/* generic function for encode/decode called after a frame has been coded/decoded */
void MPV_frame_end(MpegEncContext *s)
{
828 829
    s->avctx->key_frame   = (s->pict_type == I_TYPE);
    s->avctx->pict_type   = s->pict_type;
830

Fabrice Bellard's avatar
Fabrice Bellard committed
831
    /* draw edge for correct motion prediction if outside */
832
    if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
833 834 835
        draw_edges(s->current_picture[0], s->linesize  , s->h_edge_pos   , s->v_edge_pos   , EDGE_WIDTH  );
        draw_edges(s->current_picture[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
        draw_edges(s->current_picture[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
Fabrice Bellard's avatar
Fabrice Bellard committed
836
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
837
    emms_c();
838
    
Michael Niedermayer's avatar
Michael Niedermayer committed
839
    s->last_pict_type    = s->pict_type;
840 841
    if(s->pict_type!=B_TYPE){
        s->last_non_b_pict_type= s->pict_type;
Michael Niedermayer's avatar
Michael Niedermayer committed
842 843
        s->num_available_buffers++;
        if(s->num_available_buffers>2) s->num_available_buffers= 2;
844
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
845 846
}

847 848 849 850 851 852 853 854 855 856 857 858 859
/* reorder input for encoding */
void reorder_input(MpegEncContext *s, AVPicture *pict)
{
    int i, j, index;
            
    if(s->max_b_frames > FF_MAX_B_FRAMES) s->max_b_frames= FF_MAX_B_FRAMES;

//        delay= s->max_b_frames+1; (or 0 if no b frames cuz decoder diff)

    for(j=0; j<REORDER_BUFFER_SIZE-1; j++){
        s->coded_order[j]= s->coded_order[j+1];
    }
    s->coded_order[j].picture[0]= s->coded_order[j].picture[1]= s->coded_order[j].picture[2]= NULL; //catch uninitalized buffers
Michael Niedermayer's avatar
Michael Niedermayer committed
860
    s->coded_order[j].pict_type=0;
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877

    switch(s->input_pict_type){
    default: 
    case I_TYPE:
    case S_TYPE:
    case P_TYPE:
        index= s->max_b_frames - s->b_frames_since_non_b;
        s->b_frames_since_non_b=0;
        break;            
    case B_TYPE:
        index= s->max_b_frames + 1;
        s->b_frames_since_non_b++;
        break;          
    }
//printf("index:%d type:%d strides: %d %d\n", index, s->input_pict_type, pict->linesize[0], s->linesize);
    if(   (index==0 || (s->flags&CODEC_FLAG_INPUT_PRESERVED))
       && pict->linesize[0] == s->linesize
Michael Niedermayer's avatar
Michael Niedermayer committed
878 879
       && pict->linesize[1] == s->uvlinesize
       && pict->linesize[2] == s->uvlinesize){
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899