mpegvideo.c 122.0 KB
Newer Older
F
Fabrice Bellard 已提交
1 2
/*
 * The simplest mpeg encoder (well, it was the simplest!)
F
Fabrice Bellard 已提交
3
 * Copyright (c) 2000,2001 Fabrice Bellard.
F
Fabrice Bellard 已提交
4
 *
F
Fabrice Bellard 已提交
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.
F
Fabrice Bellard 已提交
9
 *
F
Fabrice Bellard 已提交
10
 * This library is distributed in the hope that it will be useful,
F
Fabrice Bellard 已提交
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
F
Fabrice Bellard 已提交
12 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
F
Fabrice Bellard 已提交
14
 *
F
Fabrice Bellard 已提交
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
M
Michael Niedermayer 已提交
18
 *
19
 * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
F
Fabrice Bellard 已提交
20
 */
21 22
 
#include <ctype.h>
F
Fabrice Bellard 已提交
23 24 25
#include "avcodec.h"
#include "dsputil.h"
#include "mpegvideo.h"
26
#include "simple_idct.h"
F
Fabrice Bellard 已提交
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);
M
Michael Niedermayer 已提交
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);
M
Michael Niedermayer 已提交
44 45

void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c;
M
Michael Niedermayer 已提交
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);
M
Michael Niedermayer 已提交
48

F
Fabrice Bellard 已提交
49 50 51 52 53 54

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

//#define DEBUG

55

F
Fabrice Bellard 已提交
56 57 58
/* for jpeg fast DCT */
#define CONST_BITS 14

59
static const uint16_t aanscales[64] = {
F
Fabrice Bellard 已提交
60 61 62 63 64 65 66 67 68 69 70
    /* 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
};

71
/* Input permutation for the simple_idct_mmx */
72
static const uint8_t simple_mmx_permutation[64]={
73 74 75 76 77 78 79 80 81 82
	0x00, 0x08, 0x04, 0x09, 0x01, 0x0C, 0x05, 0x0D, 
	0x10, 0x18, 0x14, 0x19, 0x11, 0x1C, 0x15, 0x1D, 
	0x20, 0x28, 0x24, 0x29, 0x21, 0x2C, 0x25, 0x2D, 
	0x12, 0x1A, 0x16, 0x1B, 0x13, 0x1E, 0x17, 0x1F, 
	0x02, 0x0A, 0x06, 0x0B, 0x03, 0x0E, 0x07, 0x0F, 
	0x30, 0x38, 0x34, 0x39, 0x31, 0x3C, 0x35, 0x3D, 
	0x22, 0x2A, 0x26, 0x2B, 0x23, 0x2E, 0x27, 0x2F, 
	0x32, 0x3A, 0x36, 0x3B, 0x33, 0x3E, 0x37, 0x3F,
};

83
static const uint8_t h263_chroma_roundtab[16] = {
F
Fabrice Bellard 已提交
84 85 86
    0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
};

87
static UINT16 (*default_mv_penalty)[MAX_MV*2+1]=NULL;
88 89
static UINT8 default_fcode_tab[MAX_MV*2+1];

90 91 92
/* default motion estimation */
int motion_estimation_method = ME_EPZS;

93
static void convert_matrix(MpegEncContext *s, int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64],
94
                           const UINT16 *quant_matrix, int bias, int qmin, int qmax)
F
Fabrice Bellard 已提交
95
{
96 97
    int qscale;

98
    for(qscale=qmin; qscale<=qmax; qscale++){
99
        int i;
100 101
        if (s->fdct == ff_jpeg_fdct_islow) {
            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 */
                
M
Michael Niedermayer 已提交
108
                qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) / 
109 110 111
                                (qscale * quant_matrix[j]));
            }
        } else if (s->fdct == fdct_ifast) {
112
            for(i=0;i<64;i++) {
113
                const int j= s->idct_permutation[i];
114 115 116 117 118
                /* 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 */
                
M
Michael Niedermayer 已提交
119
                qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) / 
120 121 122 123
                                (aanscales[i] * qscale * quant_matrix[j]));
            }
        } else {
            for(i=0;i<64;i++) {
124
                const int j= s->idct_permutation[i];
125 126 127 128 129 130
                /* 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]);
131
                qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
132 133 134 135

                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]);
            }
F
Fabrice Bellard 已提交
136 137 138
        }
    }
}
139 140 141 142 143 144 145 146 147
// move into common.c perhaps 
#define CHECKED_ALLOCZ(p, size)\
{\
    p= av_mallocz(size);\
    if(p==NULL){\
        perror("malloc");\
        goto fail;\
    }\
}
148

149 150 151
void ff_init_scantable(MpegEncContext *s, ScanTable *st, const UINT8 *src_scantable){
    int i;
    int end;
M
Michael Niedermayer 已提交
152 153
    
    st->scantable= src_scantable;
154 155 156 157 158

    for(i=0; i<64; i++){
        int j;
        j = src_scantable[i];
        st->permutated[i] = s->idct_permutation[j];
159 160 161
#ifdef ARCH_POWERPC
        st->inverse[j] = i;
#endif
162 163 164 165 166 167 168 169 170 171 172 173
    }
    
    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
174 175 176 177
 converted */
// *FIXME* this is ugly hack using local static
static void (*ff_put_pixels_clamped)(const DCTELEM *block, UINT8 *pixels, int line_size);
static void (*ff_add_pixels_clamped)(const DCTELEM *block, UINT8 *pixels, int line_size);
178 179 180
static void ff_jref_idct_put(UINT8 *dest, int line_size, DCTELEM *block)
{
    j_rev_dct (block);
181
    ff_put_pixels_clamped(block, dest, line_size);
182 183 184 185
}
static void ff_jref_idct_add(UINT8 *dest, int line_size, DCTELEM *block)
{
    j_rev_dct (block);
186
    ff_add_pixels_clamped(block, dest, line_size);
187
}
F
Fabrice Bellard 已提交
188

189 190
/* init common dct for both encoder and decoder */
int DCT_common_init(MpegEncContext *s)
F
Fabrice Bellard 已提交
191
{
192
    int i;
F
Fabrice Bellard 已提交
193

194 195 196
    ff_put_pixels_clamped = s->dsp.put_pixels_clamped;
    ff_add_pixels_clamped = s->dsp.add_pixels_clamped;

197
    s->dct_unquantize_h263 = dct_unquantize_h263_c;
198 199
    s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
    s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
200 201 202 203 204
    s->dct_quantize= dct_quantize_c;

    if(s->avctx->dct_algo==FF_DCT_FASTINT)
        s->fdct = fdct_ifast;
    else
205 206 207 208 209
        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;
210
        s->idct_permutation_type= FF_LIBMPEG2_IDCT_PERM;
211 212 213
    }else{ //accurate/default
        s->idct_put= simple_idct_put;
        s->idct_add= simple_idct_add;
214
        s->idct_permutation_type= FF_NO_IDCT_PERM;
215
    }
216 217 218
        
#ifdef HAVE_MMX
    MPV_common_init_mmx(s);
219 220 221
#endif
#ifdef ARCH_ALPHA
    MPV_common_init_axp(s);
N
Nick Kurshev 已提交
222
#endif
223 224 225
#ifdef HAVE_MLIB
    MPV_common_init_mlib(s);
#endif
226 227 228
#ifdef HAVE_MMI
    MPV_common_init_mmi(s);
#endif
229
#ifdef ARCH_ARMV4L
230
    MPV_common_init_armv4l(s);
231
#endif
232 233 234
#ifdef ARCH_POWERPC
    MPV_common_init_ppc(s);
#endif
235

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    switch(s->idct_permutation_type){
    case FF_NO_IDCT_PERM:
        for(i=0; i<64; i++)
            s->idct_permutation[i]= i;
        break;
    case FF_LIBMPEG2_IDCT_PERM:
        for(i=0; i<64; i++)
            s->idct_permutation[i]= (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
        break;
    case FF_SIMPLE_IDCT_PERM:
        for(i=0; i<64; i++)
            s->idct_permutation[i]= simple_mmx_permutation[i];
        break;
    case FF_TRANSPOSE_IDCT_PERM:
        for(i=0; i<64; i++)
            s->idct_permutation[i]= ((i&7)<<3) | (i>>3);
        break;
    default:
        fprintf(stderr, "Internal error, IDCT permutation not set\n");
        return -1;
    }

258 259 260 261 262 263 264 265

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

267 268 269
    return 0;
}

M
cleanup  
Michael Niedermayer 已提交
270
/**
M
Michael Niedermayer 已提交
271 272
 * allocates a Picture
 * The pixels are allocated/set by calling get_buffer() if shared=0
M
cleanup  
Michael Niedermayer 已提交
273
 */
M
Michael Niedermayer 已提交
274 275 276 277 278 279 280 281 282 283 284
static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
    
    if(shared){
        assert(pic->data[0]);
        assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
        pic->type= FF_BUFFER_TYPE_SHARED;
    }else{
        int r;
        
        assert(!pic->data[0]);
        
M
Michael Niedermayer 已提交
285
        r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
M
Michael Niedermayer 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
        
        if(r<0 || !pic->age || !pic->type || !pic->data[0]){
            fprintf(stderr, "get_buffer() failed (%d %d %d %X)\n", r, pic->age, pic->type, (int)pic->data[0]);
            return -1;
        }

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

        if(pic->linesize[1] != pic->linesize[2]){
            fprintf(stderr, "get_buffer() failed (uv stride missmatch)\n");
            return -1;
        }

        s->linesize  = pic->linesize[0];
        s->uvlinesize= pic->linesize[1];
M
cleanup  
Michael Niedermayer 已提交
304
    }
M
Michael Niedermayer 已提交
305 306 307 308 309 310 311
    
    if(pic->qscale_table==NULL){
        if (s->encoding) {        
            CHECKED_ALLOCZ(pic->mb_var   , s->mb_num * sizeof(INT16))
            CHECKED_ALLOCZ(pic->mc_mb_var, s->mb_num * sizeof(INT16))
            CHECKED_ALLOCZ(pic->mb_mean  , s->mb_num * sizeof(INT8))
        }
M
cleanup  
Michael Niedermayer 已提交
312

M
Michael Niedermayer 已提交
313 314 315 316
        CHECKED_ALLOCZ(pic->mbskip_table , s->mb_num * sizeof(UINT8)+1) //the +1 is for the slice end check
        CHECKED_ALLOCZ(pic->qscale_table , s->mb_num * sizeof(UINT8))
        pic->qstride= s->mb_width;
    }
M
cleanup  
Michael Niedermayer 已提交
317 318 319 320 321 322
    
    return 0;
fail: //for the CHECKED_ALLOCZ macro
    return -1;
}

M
Michael Niedermayer 已提交
323 324 325
/**
 * deallocates a picture
 */
M
cleanup  
Michael Niedermayer 已提交
326 327
static void free_picture(MpegEncContext *s, Picture *pic){
    int i;
M
Michael Niedermayer 已提交
328 329

    if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
M
Michael Niedermayer 已提交
330
        s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
M
Michael Niedermayer 已提交
331 332
    }

M
cleanup  
Michael Niedermayer 已提交
333 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);
    
M
Michael Niedermayer 已提交
339
    if(pic->type == FF_BUFFER_TYPE_INTERNAL){
M
cleanup  
Michael Niedermayer 已提交
340 341 342 343 344
        for(i=0; i<4; i++){
            av_freep(&pic->base[i]);
            pic->data[i]= NULL;
        }
        av_freep(&pic->opaque);
M
Michael Niedermayer 已提交
345 346 347 348 349 350 351
        pic->type= 0;
    }else if(pic->type == FF_BUFFER_TYPE_SHARED){
        for(i=0; i<4; i++){
            pic->base[i]=
            pic->data[i]= NULL;
        }
        pic->type= 0;        
M
cleanup  
Michael Niedermayer 已提交
352 353 354
    }
}

355 356 357
/* init common structure for both encoder and decoder */
int MPV_common_init(MpegEncContext *s)
{
358
    int y_size, c_size, yc_size, i;
359

360
    dsputil_init(&s->dsp, s->avctx->dsp_mask);
361
    DCT_common_init(s);
362

363
    s->flags= s->avctx->flags;
364

M
cleanup  
Michael Niedermayer 已提交
365
    s->mb_width  = (s->width  + 15) / 16;
F
Fabrice Bellard 已提交
366
    s->mb_height = (s->height + 15) / 16;
367

368 369 370
    /* 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;
371 372 373 374 375 376 377

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

    y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
    c_size = (s->mb_width + 2) * (s->mb_height + 2);
    yc_size = y_size + 2 * c_size;

378 379 380 381 382
    /* 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);
383

384
    CHECKED_ALLOCZ(s->edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
M
cleanup  
Michael Niedermayer 已提交
385

M
Michael Niedermayer 已提交
386
    s->avctx->coded_frame= (AVFrame*)&s->current_picture;
M
cleanup  
Michael Niedermayer 已提交
387

388
    if (s->encoding) {
389 390 391
        int mv_table_size= (s->mb_width+2)*(s->mb_height+2);

        /* Allocate MV tables */
392 393 394 395 396 397 398 399 400
        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))

M
cleanup  
Michael Niedermayer 已提交
401 402
        //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
        CHECKED_ALLOCZ(s->me_scratchpad,  s->width*2*16*3*sizeof(uint8_t)) 
403 404 405
        
        CHECKED_ALLOCZ(s->me_map      , ME_MAP_SIZE*sizeof(uint32_t))
        CHECKED_ALLOCZ(s->me_score_map, ME_MAP_SIZE*sizeof(uint16_t))
406

407 408 409 410
        if(s->codec_id==CODEC_ID_MPEG4){
            CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
            CHECKED_ALLOCZ(   s->pb2_buffer, PB_BUFFER_SIZE);
        }
M
Michael Niedermayer 已提交
411
        
412 413 414
        if(s->msmpeg4_version){
            CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
        }
M
Michael Niedermayer 已提交
415
        CHECKED_ALLOCZ(s->avctx->stats_out, 256);
416
    }
417 418
        
    CHECKED_ALLOCZ(s->error_status_table, s->mb_num*sizeof(UINT8))
419
    
420
    if (s->out_format == FMT_H263 || s->encoding) {
F
Fabrice Bellard 已提交
421
        int size;
422 423 424
        /* Allocate MB type table */
        CHECKED_ALLOCZ(s->mb_type  , s->mb_num * sizeof(UINT8))

F
Fabrice Bellard 已提交
425 426
        /* MV prediction */
        size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
427
        CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(INT16));
428 429 430
    }

    if(s->codec_id==CODEC_ID_MPEG4){
431
        /* interlaced direct mode decoding tables */
432 433
        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))
F
Fabrice Bellard 已提交
434
    }
435 436 437
    /* 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))
M
Michael Niedermayer 已提交
438
    if (s->out_format == FMT_H263) {
F
Fabrice Bellard 已提交
439
        /* ac values */
440
        CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(INT16) * 16);
F
Fabrice Bellard 已提交
441 442 443 444
        s->ac_val[1] = s->ac_val[0] + y_size;
        s->ac_val[2] = s->ac_val[1] + c_size;
        
        /* cbp values */
445
        CHECKED_ALLOCZ(s->coded_block, y_size);
M
Michael Niedermayer 已提交
446 447
        
        /* divx501 bitstream reorder buffer */
448
        CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
M
cleanup  
Michael Niedermayer 已提交
449

450 451 452
        /* 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))
453
    }
454 455 456 457 458 459 460 461 462 463 464
    
    if (s->h263_pred || s->h263_plus || !s->encoding) {
        /* dc values */
        //MN: we need these for error resilience of intra-frames
        CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(INT16));
        s->dc_val[1] = s->dc_val[0] + y_size;
        s->dc_val[2] = s->dc_val[1] + c_size;
        for(i=0;i<yc_size;i++)
            s->dc_val[0][i] = 1024;
    }

465 466 467 468
    /* which mb is a intra block */
    CHECKED_ALLOCZ(s->mbintra_table, s->mb_num);
    memset(s->mbintra_table, 1, s->mb_num);
    
F
Fabrice Bellard 已提交
469 470
    /* default structure is frame */
    s->picture_structure = PICT_FRAME;
471
    
472
    /* init macroblock skip table */
473 474
    CHECKED_ALLOCZ(s->mbskip_table, s->mb_num+1);
    //Note the +1 is for a quicker mpeg4 slice_end detection
475
    
476
    s->block= s->blocks[0];
477

478 479
    s->parse_context.state= -1;

F
Fabrice Bellard 已提交
480 481 482
    s->context_initialized = 1;
    return 0;
 fail:
483
    MPV_common_end(s);
F
Fabrice Bellard 已提交
484 485 486
    return -1;
}

487 488 489

//extern int sads;

F
Fabrice Bellard 已提交
490 491 492 493 494
/* init common structure for both encoder and decoder */
void MPV_common_end(MpegEncContext *s)
{
    int i;

495 496 497 498 499 500 501 502 503 504 505 506 507 508
    av_freep(&s->mb_type);
    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);
509 510
    av_freep(&s->cbp_table);
    av_freep(&s->pred_dir_table);
511
    av_freep(&s->me_scratchpad);
512 513 514
    av_freep(&s->me_map);
    av_freep(&s->me_score_map);
    
515 516
    av_freep(&s->mbskip_table);
    av_freep(&s->bitstream_buffer);
517 518
    av_freep(&s->tex_pb_buffer);
    av_freep(&s->pb2_buffer);
519
    av_freep(&s->edge_emu_buffer);
520 521 522
    av_freep(&s->co_located_type_table);
    av_freep(&s->field_mv_table);
    av_freep(&s->field_select_table);
M
Michael Niedermayer 已提交
523
    av_freep(&s->avctx->stats_out);
524
    av_freep(&s->ac_stats);
525
    av_freep(&s->error_status_table);
M
cleanup  
Michael Niedermayer 已提交
526 527 528

    for(i=0; i<MAX_PICTURE_COUNT; i++){
        free_picture(s, &s->picture[i]);
F
Fabrice Bellard 已提交
529 530 531 532 533 534 535 536
    }
    s->context_initialized = 0;
}

/* init video encoder */
int MPV_encode_init(AVCodecContext *avctx)
{
    MpegEncContext *s = avctx->priv_data;
537
    int i;
F
Fabrice Bellard 已提交
538

539 540
    avctx->pix_fmt = PIX_FMT_YUV420P;

F
Fabrice Bellard 已提交
541
    s->bit_rate = avctx->bit_rate;
542
    s->bit_rate_tolerance = avctx->bit_rate_tolerance;
F
Fabrice Bellard 已提交
543 544 545
    s->frame_rate = avctx->frame_rate;
    s->width = avctx->width;
    s->height = avctx->height;
546
    if(avctx->gop_size > 600){
547
        fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n");
548 549
        avctx->gop_size=600;
    }
F
Fabrice Bellard 已提交
550
    s->gop_size = avctx->gop_size;
551 552
    s->rtp_mode = avctx->rtp_mode;
    s->rtp_payload_size = avctx->rtp_payload_size;
553 554
    if (avctx->rtp_callback)
        s->rtp_callback = avctx->rtp_callback;
555 556 557 558 559
    s->qmin= avctx->qmin;
    s->qmax= avctx->qmax;
    s->max_qdiff= avctx->max_qdiff;
    s->qcompress= avctx->qcompress;
    s->qblur= avctx->qblur;
560
    s->avctx = avctx;
561
    s->flags= avctx->flags;
562
    s->max_b_frames= avctx->max_b_frames;
563
    s->b_frame_strategy= avctx->b_frame_strategy;
564
    s->codec_id= avctx->codec->id;
565 566 567 568
    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;
569
    s->mpeg_quant= avctx->mpeg_quant;
570

F
Fabrice Bellard 已提交
571 572 573 574 575 576
    if (s->gop_size <= 1) {
        s->intra_only = 1;
        s->gop_size = 12;
    } else {
        s->intra_only = 0;
    }
577

578
    /* ME algorithm */
579 580 581 582 583
    if (avctx->me_method == 0)
        /* For compatibility */
        s->me_method = motion_estimation_method;
    else
        s->me_method = avctx->me_method;
584

585
    /* Fixed QSCALE */
F
Fabrice Bellard 已提交
586
    s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
587
    
588
    s->adaptive_quant= (   s->avctx->lumi_masking
589
                        || s->avctx->dark_masking
590 591 592 593
                        || s->avctx->temporal_cplx_masking 
                        || s->avctx->spatial_cplx_masking
                        || s->avctx->p_masking)
                       && !s->fixed_qscale;
594 595
    
    s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
596

F
Fabrice Bellard 已提交
597 598 599
    switch(avctx->codec->id) {
    case CODEC_ID_MPEG1VIDEO:
        s->out_format = FMT_MPEG1;
M
Michael Niedermayer 已提交
600
        avctx->delay=0; //FIXME not sure, should check the spec
F
Fabrice Bellard 已提交
601 602 603 604
        break;
    case CODEC_ID_MJPEG:
        s->out_format = FMT_MJPEG;
        s->intra_only = 1; /* force intra only for jpeg */
605
        s->mjpeg_write_tables = 1; /* write all tables */
606
	s->mjpeg_data_only_frames = 0; /* write all the needed headers */
607 608 609
        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; 
610
        s->mjpeg_hsample[0] = 2;
611 612
        s->mjpeg_hsample[1] = 1; 
        s->mjpeg_hsample[2] = 1; 
F
Fabrice Bellard 已提交
613 614
        if (mjpeg_init(s) < 0)
            return -1;
M
Michael Niedermayer 已提交
615
        avctx->delay=0;
M
Michael Niedermayer 已提交
616
        s->low_delay=1;
F
Fabrice Bellard 已提交
617 618
        break;
    case CODEC_ID_H263:
619 620
        if (h263_get_picture_format(s->width, s->height) == 7) {
            printf("Input picture size isn't suitable for h263 codec! try h263+\n");
F
Fabrice Bellard 已提交
621
            return -1;
622
        }
F
Fabrice Bellard 已提交
623
        s->out_format = FMT_H263;
M
Michael Niedermayer 已提交
624
        avctx->delay=0;
M
Michael Niedermayer 已提交
625
        s->low_delay=1;
F
Fabrice Bellard 已提交
626 627 628 629
        break;
    case CODEC_ID_H263P:
        s->out_format = FMT_H263;
        s->h263_plus = 1;
630
        s->unrestricted_mv = 1;
631
        s->h263_aic = 1;
632 633 634 635
        
        /* These are just to be sure */
        s->umvplus = 0;
        s->umvplus_dec = 0;
M
Michael Niedermayer 已提交
636
        avctx->delay=0;
M
Michael Niedermayer 已提交
637
        s->low_delay=1;
F
Fabrice Bellard 已提交
638 639 640 641
        break;
    case CODEC_ID_RV10:
        s->out_format = FMT_H263;
        s->h263_rv10 = 1;
M
Michael Niedermayer 已提交
642
        avctx->delay=0;
M
Michael Niedermayer 已提交
643
        s->low_delay=1;
F
Fabrice Bellard 已提交
644
        break;
F
Fabrice Bellard 已提交
645
    case CODEC_ID_MPEG4:
F
Fabrice Bellard 已提交
646 647 648
        s->out_format = FMT_H263;
        s->h263_pred = 1;
        s->unrestricted_mv = 1;
M
Michael Niedermayer 已提交
649
        s->low_delay= s->max_b_frames ? 0 : 1;
650
        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
F
Fabrice Bellard 已提交
651
        break;
652
    case CODEC_ID_MSMPEG4V1:
F
Fabrice Bellard 已提交
653 654 655 656
        s->out_format = FMT_H263;
        s->h263_msmpeg4 = 1;
        s->h263_pred = 1;
        s->unrestricted_mv = 1;
657
        s->msmpeg4_version= 1;
M
Michael Niedermayer 已提交
658
        avctx->delay=0;
M
Michael Niedermayer 已提交
659
        s->low_delay=1;
660 661 662 663 664 665 666
        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;
M
Michael Niedermayer 已提交
667
        avctx->delay=0;
M
Michael Niedermayer 已提交
668
        s->low_delay=1;
669 670 671 672 673 674 675
        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;
M
Michael Niedermayer 已提交
676
        avctx->delay=0;
M
Michael Niedermayer 已提交
677
        s->low_delay=1;
F
Fabrice Bellard 已提交
678
        break;
M
Michael Niedermayer 已提交
679 680 681 682 683 684 685
    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;
M
Michael Niedermayer 已提交
686
        s->low_delay=1;
M
Michael Niedermayer 已提交
687 688 689 690 691 692 693 694
        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;
M
Michael Niedermayer 已提交
695
        s->low_delay=1;
M
Michael Niedermayer 已提交
696
        break;
F
Fabrice Bellard 已提交
697 698 699
    default:
        return -1;
    }
M
Michael Niedermayer 已提交
700
    
701 702 703 704 705
    { /* set up some save defaults, some codecs might override them later */
        static int done=0;
        if(!done){
            int i;
            done=1;
706 707

            default_mv_penalty= av_mallocz( sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1) );
708
            memset(default_mv_penalty, 0, sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1));
709 710 711 712 713 714 715 716 717
            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;
718 719 720
    s->y_dc_scale_table=
    s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
 
721
    /* dont use mv_penalty table for crap MV as it would be confused */
722
    if (s->me_method < ME_EPZS) s->mv_penalty = default_mv_penalty;
723

724 725
    s->encoding = 1;

F
Fabrice Bellard 已提交
726 727 728 729
    /* init */
    if (MPV_common_init(s) < 0)
        return -1;
    
730
#ifdef CONFIG_ENCODERS
731 732 733 734 735 736
    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);
737
#endif
738

739 740
    /* init default q matrix */
    for(i=0;i<64;i++) {
741
        int j= s->idct_permutation[i];
742
        if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
743 744
            s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
            s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
745
        }else if(s->out_format == FMT_H263){
746 747
            s->intra_matrix[j] =
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
748
        }else{ /* mpeg1 */
749 750
            s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
751
        }
752 753 754
    }

    /* precompute matrix */
755
    /* for mjpeg, we do include qscale in the matrix */
756
    if (s->out_format != FMT_MJPEG) {
757
        convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias, 
758
                       s->intra_matrix, s->intra_quant_bias, 1, 31);
759
        convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias, 
760
                       s->inter_matrix, s->inter_quant_bias, 1, 31);
761 762
    }

763 764
    if(ff_rate_control_init(s) < 0)
        return -1;
F
Fabrice Bellard 已提交
765 766

    s->picture_number = 0;
767
    s->picture_in_gop_number = 0;
F
Fabrice Bellard 已提交
768 769 770
    s->fake_picture_number = 0;
    /* motion detector init */
    s->f_code = 1;
771
    s->b_code = 1;
F
Fabrice Bellard 已提交
772 773 774 775 776 777 778 779 780 781 782

    return 0;
}

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

#ifdef STATS
    print_stats();
#endif
783 784 785

    ff_rate_control_uninit(s);

F
Fabrice Bellard 已提交
786 787 788
    MPV_common_end(s);
    if (s->out_format == FMT_MJPEG)
        mjpeg_close(s);
789
      
F
Fabrice Bellard 已提交
790 791 792 793
    return 0;
}

/* draw the edges of width 'w' of an image of size width, height */
794
//FIXME check that this is ok for mpeg4 interlaced
M
Michael Niedermayer 已提交
795
static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
F
Fabrice Bellard 已提交
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
{
    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 */
    }
}

M
Michael Niedermayer 已提交
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
static int find_unused_picture(MpegEncContext *s, int shared){
    int i;
    
    if(shared){
        for(i=0; i<MAX_PICTURE_COUNT; i++){
            if(s->picture[i].data[0]==NULL && s->picture[i].type==0) break;
        }
    }else{
        for(i=0; i<MAX_PICTURE_COUNT; i++){
            if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) break;
        }
        for(i=0; i<MAX_PICTURE_COUNT; i++){
            if(s->picture[i].data[0]==NULL) break;
        }
    }

    assert(i<MAX_PICTURE_COUNT);
    return i;
}

F
Fabrice Bellard 已提交
842
/* generic function for encode/decode called before a frame is coded/decoded */
843
int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
F
Fabrice Bellard 已提交
844
{
M
Michael Niedermayer 已提交
845
    int i;
M
Michael Niedermayer 已提交
846
    AVFrame *pic;
F
Fabrice Bellard 已提交
847

848
    s->mb_skiped = 0;
M
cleanup  
Michael Niedermayer 已提交
849 850 851 852
    
    /* mark&release old frames */
    if (s->pict_type != B_TYPE && s->last_picture.data[0]) {
        for(i=0; i<MAX_PICTURE_COUNT; i++){
M
Michael Niedermayer 已提交
853
//printf("%8X %d %d %X %X\n", s->picture[i].data[0], s->picture[i].type, i, s->next_picture.data[0], s->last_picture.data[0]);
M
cleanup  
Michael Niedermayer 已提交
854 855
            if(s->picture[i].data[0] == s->last_picture.data[0]){
//                s->picture[i].reference=0;
M
Michael Niedermayer 已提交
856
                avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
M
cleanup  
Michael Niedermayer 已提交
857 858 859 860 861 862 863 864 865 866 867
                break;
            }    
        }
        assert(i<MAX_PICTURE_COUNT);

        /* release forgotten pictures */
        /* if(mpeg124/h263) */
        if(!s->encoding){
            for(i=0; i<MAX_PICTURE_COUNT; i++){
                if(s->picture[i].data[0] && s->picture[i].data[0] != s->next_picture.data[0] && s->picture[i].reference){
                    fprintf(stderr, "releasing zombie picture\n");
M
Michael Niedermayer 已提交
868
                    avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);                
M
cleanup  
Michael Niedermayer 已提交
869 870
                }
            }
871
        }
872
    }
873 874
alloc:
    if(!s->encoding){
M
Michael Niedermayer 已提交
875
        i= find_unused_picture(s, 0);
M
cleanup  
Michael Niedermayer 已提交
876
    
M
Michael Niedermayer 已提交
877
        pic= (AVFrame*)&s->picture[i];
M
cleanup  
Michael Niedermayer 已提交
878 879 880
        pic->reference= s->pict_type != B_TYPE;
        pic->coded_picture_number= s->current_picture.coded_picture_number+1;
        
M
Michael Niedermayer 已提交
881
        alloc_picture(s, (Picture*)pic, 0);
882

M
cleanup  
Michael Niedermayer 已提交
883 884
        s->current_picture= s->picture[i];
    }
885

M
cleanup  
Michael Niedermayer 已提交
886 887 888
    if (s->pict_type != B_TYPE) {
        s->last_picture= s->next_picture;
        s->next_picture= s->current_picture;
F
Fabrice Bellard 已提交
889
    }
890 891 892 893 894 895
    
    if(s->pict_type != I_TYPE && s->last_picture.data[0]==NULL){
        fprintf(stderr, "warning: first frame is no keyframe\n");
        assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
        goto alloc;
    }
M
cleanup  
Michael Niedermayer 已提交
896
   
897 898 899
    s->hurry_up= s->avctx->hurry_up;
    s->error_resilience= avctx->error_resilience;

900 901 902 903 904 905 906 907 908
    /* 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;
909 910

    return 0;
F
Fabrice Bellard 已提交
911
}
912

F
Fabrice Bellard 已提交
913 914 915
/* generic function for encode/decode called after a frame has been coded/decoded */
void MPV_frame_end(MpegEncContext *s)
{
M
cleanup  
Michael Niedermayer 已提交
916
    int i;
F
Fabrice Bellard 已提交
917
    /* draw edge for correct motion prediction if outside */
M
cleanup  
Michael Niedermayer 已提交
918 919 920 921 922 923
    if(s->codec_id!=CODEC_ID_SVQ1){
        if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
            draw_edges(s->current_picture.data[0], s->linesize  , s->h_edge_pos   , s->v_edge_pos   , EDGE_WIDTH  );
            draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
            draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
        }
F
Fabrice Bellard 已提交
924
    }
M
Michael Niedermayer 已提交
925
    emms_c();
926
    
M
Michael Niedermayer 已提交
927
    s->last_pict_type    = s->pict_type;
928 929 930
    if(s->pict_type!=B_TYPE){
        s->last_non_b_pict_type= s->pict_type;
    }
M
cleanup  
Michael Niedermayer 已提交
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
    
    s->current_picture.quality= s->qscale; //FIXME get average of qscale_table
    s->current_picture.pict_type= s->pict_type;
    s->current_picture.key_frame= s->pict_type == I_TYPE;
    
    /* copy back current_picture variables */
    for(i=0; i<MAX_PICTURE_COUNT; i++){
        if(s->picture[i].data[0] == s->current_picture.data[0]){
            s->picture[i]= s->current_picture;
            break;
        }    
    }
    assert(i<MAX_PICTURE_COUNT);

    /* release non refernce frames */
    for(i=0; i<MAX_PICTURE_COUNT; i++){
M
Michael Niedermayer 已提交
947
        if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/)
M
Michael Niedermayer 已提交
948
            s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
M
cleanup  
Michael Niedermayer 已提交
949
    }
F
Fabrice Bellard 已提交
950 951
}

M
Michael Niedermayer 已提交
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
static int get_sae(uint8_t *src, int ref, int stride){
    int x,y;
    int acc=0;
    
    for(y=0; y<16; y++){
        for(x=0; x<16; x++){
            acc+= ABS(src[x+y*stride] - ref);
        }
    }
    
    return acc;
}

static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
    int x, y, w, h;
    int acc=0;
    
    w= s->width &~15;
    h= s->height&~15;
    
    for(y=0; y<h; y+=16){
        for(x=0; x<w; x+=16){
            int offset= x + y*stride;
            int sad = s->dsp.pix_abs16x16(src + offset, ref + offset, stride);
            int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
            int sae = get_sae(src + offset, mean, stride);
            
            acc+= sae + 500 < sad;
        }
    }
    return acc;
}

M
Michael Niedermayer 已提交
985

M
Michael Niedermayer 已提交
986 987
static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
    AVFrame *pic;
M
Michael Niedermayer 已提交
988
    int i;
M
cleanup  
Michael Niedermayer 已提交
989
    const int encoding_delay= s->max_b_frames;
M
Michael Niedermayer 已提交
990
    int direct=1;
M
cleanup  
Michael Niedermayer 已提交
991

M
Michael Niedermayer 已提交
992 993 994 995 996 997
    if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
    if(pic_arg->linesize[0] != s->linesize) direct=0;
    if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
    if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
  
//    printf("%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
M
cleanup  
Michael Niedermayer 已提交
998
    
M
Michael Niedermayer 已提交
999 1000
    if(direct){
        i= find_unused_picture(s, 1);
M
cleanup  
Michael Niedermayer 已提交
1001

M
Michael Niedermayer 已提交
1002
        pic= (AVFrame*)&s->picture[i];
M
Michael Niedermayer 已提交
1003
        pic->reference= 1;
M
cleanup  
Michael Niedermayer 已提交
1004
    
M
Michael Niedermayer 已提交
1005 1006 1007 1008 1009 1010 1011
        for(i=0; i<4; i++){
            pic->data[i]= pic_arg->data[i];
            pic->linesize[i]= pic_arg->linesize[i];
        }
        alloc_picture(s, (Picture*)pic, 1);
    }else{
        i= find_unused_picture(s, 0);
M
cleanup  
Michael Niedermayer 已提交
1012

M
Michael Niedermayer 已提交
1013
        pic= (AVFrame*)&s->picture[i];
M
Michael Niedermayer 已提交
1014
        pic->reference= 1;
M
cleanup  
Michael Niedermayer 已提交
1015

M
Michael Niedermayer 已提交
1016 1017 1018 1019 1020
        alloc_picture(s, (Picture*)pic, 0);

        if(   pic->data[0] == pic_arg->data[0] 
           && pic->data[1] == pic_arg->data[1]
           && pic->data[2] == pic_arg->data[2]){
M
cleanup  
Michael Niedermayer 已提交
1021
       // empty
M
Michael Niedermayer 已提交
1022 1023
        }else{
            int h_chroma_shift, v_chroma_shift;
M
cleanup  
Michael Niedermayer 已提交
1024
        
M
Michael Niedermayer 已提交
1025
            avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
M
cleanup  
Michael Niedermayer 已提交
1026
        
M
Michael Niedermayer 已提交
1027 1028 1029 1030 1031 1032 1033 1034 1035
            for(i=0; i<3; i++){
                int src_stride= pic_arg->linesize[i];
                int dst_stride= i ? s->uvlinesize : s->linesize;
                int h_shift= i ? h_chroma_shift : 0;
                int v_shift= i ? v_chroma_shift : 0;
                int w= s->width >>h_shift;
                int h= s->height>>v_shift;
                uint8_t *src= pic_arg->data[i];
                uint8_t *dst= pic->data[i];
M
cleanup  
Michael Niedermayer 已提交
1036
            
M
Michael Niedermayer 已提交
1037 1038 1039 1040 1041 1042 1043 1044
                if(src_stride==dst_stride)
                    memcpy(dst, src, src_stride*h);
                else{
                    while(h--){
                        memcpy(dst, src, w);
                        dst += dst_stride;
                        src += src_stride;
                    }
M
cleanup  
Michael Niedermayer 已提交
1045
                }
1046
            }
M
cleanup  
Michael Niedermayer 已提交
1047 1048
        }
    }
M
Michael Niedermayer 已提交
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
    pic->quality= pic_arg->quality;
    pic->pict_type= pic_arg->pict_type;
    
    if(s->input_picture[encoding_delay])
        pic->display_picture_number= s->input_picture[encoding_delay]->display_picture_number + 1;

    /* shift buffer entries */
    for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
        s->input_picture[i-1]= s->input_picture[i];
        
    s->input_picture[encoding_delay]= (Picture*)pic;
M
cleanup  
Michael Niedermayer 已提交
1060 1061 1062

    return 0;
}
1063

M
cleanup  
Michael Niedermayer 已提交
1064 1065 1066 1067 1068 1069 1070
static void select_input_picture(MpegEncContext *s){
    int i;
    const int encoding_delay= s->max_b_frames;
    int coded_pic_num=0;    

    if(s->reordered_input_picture[0])
        coded_pic_num= s->reordered_input_picture[0]->coded_picture_number + 1;
M
Michael Niedermayer 已提交
1071

M
cleanup  
Michael Niedermayer 已提交
1072 1073 1074 1075 1076 1077
    for(i=1; i<MAX_PICTURE_COUNT; i++)
        s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
    s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;

    /* set next picture types & ordering */
    if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
M
Michael Niedermayer 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
        if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture.data[0]==NULL || s->intra_only){
            s->reordered_input_picture[0]= s->input_picture[0];
            s->reordered_input_picture[0]->pict_type= I_TYPE;
            s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
        }else{
            int b_frames;
            
            if(s->flags&CODEC_FLAG_PASS2){
                for(i=0; i<s->max_b_frames+1; i++){
                    int pict_num= s->input_picture[0]->display_picture_number + i;
                    int pict_type= s->rc_context.entry[pict_num].new_pict_type;
                    s->input_picture[i]->pict_type= pict_type;
                    
                    if(i + 1 >= s->rc_context.num_entries) break;
                }
            }
M
Michael Niedermayer 已提交
1094

M
Michael Niedermayer 已提交
1095 1096
            if(s->input_picture[0]->pict_type){
                /* user selected pict_type */
1097 1098 1099
                for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){
                    if(s->input_picture[b_frames]->pict_type!=B_TYPE) break;
                }
M
Michael Niedermayer 已提交
1100
            
1101 1102 1103 1104
                if(b_frames > s->max_b_frames){
                    fprintf(stderr, "warning, too many bframes in a row\n");
                    b_frames = s->max_b_frames;
                }
M
Michael Niedermayer 已提交
1105 1106 1107 1108 1109 1110
            }else if(s->b_frame_strategy==0){
                b_frames= s->max_b_frames;
            }else if(s->b_frame_strategy==1){
                for(i=1; i<s->max_b_frames+1; i++){
                    if(s->input_picture[i]->b_frame_score==0){
                        s->input_picture[i]->b_frame_score= 
M
Michael Niedermayer 已提交
1111 1112
                            get_intra_count(s, s->input_picture[i  ]->data[0], 
                                               s->input_picture[i-1]->data[0], s->linesize) + 1;
M
Michael Niedermayer 已提交
1113 1114 1115 1116 1117 1118 1119
                    }
                }
                for(i=0; i<s->max_b_frames; i++){
                    if(s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
                }
                                
                b_frames= FFMAX(0, i-1);
1120
                
M
Michael Niedermayer 已提交
1121 1122 1123 1124 1125 1126 1127
                /* reset scores */
                for(i=0; i<b_frames+1; i++){
                    s->input_picture[i]->b_frame_score=0;
                }
            }else{
                fprintf(stderr, "illegal b frame strategy\n");
                b_frames=0;
1128
            }
M
Michael Niedermayer 已提交
1129 1130 1131 1132 1133 1134 1135 1136 1137

            emms_c();
//static int b_count=0;
//b_count+= b_frames;
//printf("b_frames: %d\n", b_count);
                        
            s->reordered_input_picture[0]= s->input_picture[b_frames];
            if(   s->picture_in_gop_number + b_frames >= s->gop_size 
               || s->reordered_input_picture[0]->pict_type== I_TYPE)
M
cleanup  
Michael Niedermayer 已提交
1138
                s->reordered_input_picture[0]->pict_type= I_TYPE;
M
Michael Niedermayer 已提交
1139 1140 1141 1142 1143 1144 1145 1146
            else
                s->reordered_input_picture[0]->pict_type= P_TYPE;
            s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
            for(i=0; i<b_frames; i++){
                coded_pic_num++;
                s->reordered_input_picture[i+1]= s->input_picture[i];
                s->reordered_input_picture[i+1]->pict_type= B_TYPE;
                s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num;
1147 1148 1149
            }
        }
    }
M
cleanup  
Michael Niedermayer 已提交
1150 1151
    
    if(s->reordered_input_picture[0]){
M
Michael Niedermayer 已提交
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
       s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE;

        if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
            int i= find_unused_picture(s, 0);
            Picture *pic= &s->picture[i];

            s->new_picture= *s->reordered_input_picture[0];

            /* mark us unused / free shared pic */
            for(i=0; i<4; i++)
                s->reordered_input_picture[0]->data[i]= NULL;
            s->reordered_input_picture[0]->type= 0;
            
            pic->pict_type = s->reordered_input_picture[0]->pict_type;
            pic->quality   = s->reordered_input_picture[0]->quality;
            pic->coded_picture_number = s->reordered_input_picture[0]->coded_picture_number;
            pic->reference = s->reordered_input_picture[0]->reference;
            
            alloc_picture(s, pic, 0);

            s->current_picture= *pic;
        }else{
            assert(   s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER 
                   || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
            
            s->new_picture= *s->reordered_input_picture[0];

            for(i=0; i<4; i++){
                s->reordered_input_picture[0]->data[i]-=16; //FIXME dirty
            }
            s->current_picture= *s->reordered_input_picture[0];
M
cleanup  
Michael Niedermayer 已提交
1183 1184 1185 1186 1187 1188
        }
    
        s->picture_number= s->new_picture.display_picture_number;
//printf("dpn:%d\n", s->picture_number);
    }else{
       memset(&s->new_picture, 0, sizeof(Picture));
1189 1190 1191
    }
}

F
Fabrice Bellard 已提交
1192 1193 1194 1195
int MPV_encode_picture(AVCodecContext *avctx,
                       unsigned char *buf, int buf_size, void *data)
{
    MpegEncContext *s = avctx->priv_data;
M
Michael Niedermayer 已提交
1196
    AVFrame *pic_arg = data;
1197
    int i;
F
Fabrice Bellard 已提交
1198 1199 1200

    init_put_bits(&s->pb, buf, buf_size, NULL, NULL);

M
cleanup  
Michael Niedermayer 已提交
1201
    s->picture_in_gop_number++;
F
Fabrice Bellard 已提交
1202

M
cleanup  
Michael Niedermayer 已提交
1203
    load_input_picture(s, pic_arg);
1204
    
M
cleanup  
Michael Niedermayer 已提交
1205
    select_input_picture(s);
1206 1207
    
    /* output? */
M
cleanup  
Michael Niedermayer 已提交
1208
    if(s->new_picture.data[0]){
1209

M
cleanup  
Michael Niedermayer 已提交
1210 1211 1212 1213 1214 1215 1216
        s->pict_type= s->new_picture.pict_type;
        if (s->fixed_qscale){ /* the ratecontrol needs the last qscale so we dont touch it for CBR */
            s->qscale= (int)(s->new_picture.quality+0.5);
            assert(s->qscale);
        }
//emms_c();
//printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
1217
        MPV_frame_start(s, avctx);
1218 1219

        encode_picture(s, s->picture_number);
1220
        
J
Juanjo 已提交
1221
        avctx->real_pict_num  = s->picture_number;
1222 1223 1224 1225 1226 1227
        avctx->header_bits = s->header_bits;
        avctx->mv_bits     = s->mv_bits;
        avctx->misc_bits   = s->misc_bits;
        avctx->i_tex_bits  = s->i_tex_bits;
        avctx->p_tex_bits  = s->p_tex_bits;
        avctx->i_count     = s->i_count;
1228
        avctx->p_count     = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
1229 1230 1231 1232 1233 1234
        avctx->skip_count  = s->skip_count;

        MPV_frame_end(s);

        if (s->out_format == FMT_MJPEG)
            mjpeg_picture_trailer(s);
1235 1236 1237
        
        if(s->flags&CODEC_FLAG_PASS1)
            ff_write_pass1_stats(s);
F
Fabrice Bellard 已提交
1238 1239
    }

1240
    s->input_picture_number++;
F
Fabrice Bellard 已提交
1241 1242

    flush_put_bits(&s->pb);
1243
    s->frame_bits  = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
M
Michael Niedermayer 已提交
1244
    
1245
    s->total_bits += s->frame_bits;
1246
    avctx->frame_bits  = s->frame_bits;
M
cleanup  
Michael Niedermayer 已提交
1247

1248 1249 1250 1251
    for(i=0; i<4; i++){
        avctx->error[i] += s->current_picture.error[i];
    }
    
1252
    return pbBufPtr(&s->pb) - s->pb.buf;
F
Fabrice Bellard 已提交
1253 1254
}

M
Michael Niedermayer 已提交
1255 1256 1257
static inline void gmc1_motion(MpegEncContext *s,
                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
                               int dest_offset,
1258
                               UINT8 **ref_picture, int src_offset)
M
Michael Niedermayer 已提交
1259 1260
{
    UINT8 *ptr;
M
Michael Niedermayer 已提交
1261
    int offset, src_x, src_y, linesize, uvlinesize;
M
Michael Niedermayer 已提交
1262
    int motion_x, motion_y;
M
Michael Niedermayer 已提交
1263
    int emu=0;
M
Michael Niedermayer 已提交
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276

    motion_x= s->sprite_offset[0][0];
    motion_y= s->sprite_offset[0][1];
    src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
    src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
    motion_x<<=(3-s->sprite_warping_accuracy);
    motion_y<<=(3-s->sprite_warping_accuracy);
    src_x = clip(src_x, -16, s->width);
    if (src_x == s->width)
        motion_x =0;
    src_y = clip(src_y, -16, s->height);
    if (src_y == s->height)
        motion_y =0;
1277

M
Michael Niedermayer 已提交
1278
    linesize = s->linesize;
M
Michael Niedermayer 已提交
1279
    uvlinesize = s->uvlinesize;
1280
    
M
Michael Niedermayer 已提交
1281 1282 1283
    ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;

    dest_y+=dest_offset;
M
Michael Niedermayer 已提交
1284
    if(s->flags&CODEC_FLAG_EMU_EDGE){
1285
        if(src_x<0 || src_y<0 || src_x + (motion_x&15) + 16 > s->h_edge_pos
1286 1287
                              || src_y + (motion_y&15) + 16 > s->v_edge_pos){
            emulated_edge_mc(s, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
M
Michael Niedermayer 已提交
1288 1289 1290 1291
            ptr= s->edge_emu_buffer;
            emu=1;
        }
    }
1292 1293
    
    if((motion_x|motion_y)&7){
1294 1295
        s->dsp.gmc1(dest_y  , ptr  , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
        s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1296 1297 1298 1299 1300
    }else{
        int dxy;
        
        dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
        if (s->no_rounding){
1301
	    s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
1302
        }else{
1303 1304
            s->dsp.put_pixels_tab       [0][dxy](dest_y, ptr, linesize, 16);
        }
1305 1306 1307
    }
    
    if(s->flags&CODEC_FLAG_GRAY) return;
M
Michael Niedermayer 已提交
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321

    motion_x= s->sprite_offset[1][0];
    motion_y= s->sprite_offset[1][1];
    src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
    src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
    motion_x<<=(3-s->sprite_warping_accuracy);
    motion_y<<=(3-s->sprite_warping_accuracy);
    src_x = clip(src_x, -8, s->width>>1);
    if (src_x == s->width>>1)
        motion_x =0;
    src_y = clip(src_y, -8, s->height>>1);
    if (src_y == s->height>>1)
        motion_y =0;

M
Michael Niedermayer 已提交
1322
    offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
M
Michael Niedermayer 已提交
1323
    ptr = ref_picture[1] + offset;
M
Michael Niedermayer 已提交
1324
    if(emu){
1325
        emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
M
Michael Niedermayer 已提交
1326 1327
        ptr= s->edge_emu_buffer;
    }
1328
    s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
M
Michael Niedermayer 已提交
1329
    
M
Michael Niedermayer 已提交
1330
    ptr = ref_picture[2] + offset;
M
Michael Niedermayer 已提交
1331
    if(emu){
1332
        emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
M
Michael Niedermayer 已提交
1333 1334
        ptr= s->edge_emu_buffer;
    }
1335
    s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
M
Michael Niedermayer 已提交
1336 1337 1338 1339
    
    return;
}

1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
static inline void gmc_motion(MpegEncContext *s,
                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
                               int dest_offset,
                               UINT8 **ref_picture, int src_offset)
{
    UINT8 *ptr;
    int linesize, uvlinesize;
    const int a= s->sprite_warping_accuracy;
    int ox, oy;

    linesize = s->linesize;
    uvlinesize = s->uvlinesize;

    ptr = ref_picture[0] + src_offset;

    dest_y+=dest_offset;
    
    ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
    oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;

1360
    s->dsp.gmc(dest_y, ptr, linesize, 16,
1361 1362 1363 1364 1365 1366
           ox, 
           oy, 
           s->sprite_delta[0][0], s->sprite_delta[0][1],
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
           a+1, (1<<(2*a+1)) - s->no_rounding,
           s->h_edge_pos, s->v_edge_pos);
1367
    s->dsp.gmc(dest_y+8, ptr, linesize, 16,
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
           ox + s->sprite_delta[0][0]*8, 
           oy + s->sprite_delta[1][0]*8, 
           s->sprite_delta[0][0], s->sprite_delta[0][1],
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
           a+1, (1<<(2*a+1)) - s->no_rounding,
           s->h_edge_pos, s->v_edge_pos);

    if(s->flags&CODEC_FLAG_GRAY) return;


    dest_cb+=dest_offset>>1;
    dest_cr+=dest_offset>>1;
    
    ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
    oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;

    ptr = ref_picture[1] + (src_offset>>1);
1385
    s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
1386 1387 1388 1389 1390 1391 1392 1393
           ox, 
           oy, 
           s->sprite_delta[0][0], s->sprite_delta[0][1],
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
           a+1, (1<<(2*a+1)) - s->no_rounding,
           s->h_edge_pos>>1, s->v_edge_pos>>1);
    
    ptr = ref_picture[2] + (src_offset>>1);
1394
    s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
1395 1396 1397 1398 1399 1400 1401 1402 1403
           ox, 
           oy, 
           s->sprite_delta[0][0], s->sprite_delta[0][1],
           s->sprite_delta[1][0], s->sprite_delta[1][1], 
           a+1, (1<<(2*a+1)) - s->no_rounding,
           s->h_edge_pos>>1, s->v_edge_pos>>1);
}


M
Michael Niedermayer 已提交
1404
static void emulated_edge_mc(MpegEncContext *s, UINT8 *src, int linesize, int block_w, int block_h, 
1405 1406 1407
                                    int src_x, int src_y, int w, int h){
    int x, y;
    int start_y, start_x, end_y, end_x;
M
Michael Niedermayer 已提交
1408
    UINT8 *buf= s->edge_emu_buffer;
1409

1410 1411 1412
    if(src_y>= h){
        src+= (h-1-src_y)*linesize;
        src_y=h-1;
M
Michael Niedermayer 已提交
1413 1414 1415
    }else if(src_y<=-block_h){
        src+= (1-block_h-src_y)*linesize;
        src_y=1-block_h;
1416 1417 1418 1419
    }
    if(src_x>= w){
        src+= (w-1-src_x);
        src_x=w-1;
M
Michael Niedermayer 已提交
1420 1421 1422
    }else if(src_x<=-block_w){
        src+= (1-block_w-src_x);
        src_x=1-block_w;
1423 1424
    }

M
Michael Niedermayer 已提交
1425 1426 1427 1428
    start_y= FFMAX(0, -src_y);
    start_x= FFMAX(0, -src_x);
    end_y= FFMIN(block_h, h-src_y);
    end_x= FFMIN(block_w, w-src_x);
M
Michael Niedermayer 已提交
1429

1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
    // copy existing part
    for(y=start_y; y<end_y; y++){
        for(x=start_x; x<end_x; x++){
            buf[x + y*linesize]= src[x + y*linesize];
        }
    }

    //top
    for(y=0; y<start_y; y++){
        for(x=start_x; x<end_x; x++){
            buf[x + y*linesize]= buf[x + start_y*linesize];
        }
    }

    //bottom
    for(y=end_y; y<block_h; y++){
        for(x=start_x; x<end_x; x++){
            buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
        }
    }
                                    
    for(y=0; y<block_h; y++){
       //left
        for(x=0; x<start_x; x++){
            buf[x + y*linesize]= buf[start_x + y*linesize];
        }
       
       //right
        for(x=end_x; x<block_w; x++){
            buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
        }
    }
}


F
Fabrice Bellard 已提交
1465 1466 1467 1468 1469
/* apply one mpeg motion vector to the three components */
static inline void mpeg_motion(MpegEncContext *s,
                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
                               int dest_offset,
                               UINT8 **ref_picture, int src_offset,
M
Michael Niedermayer 已提交
1470
                               int field_based, op_pixels_func (*pix_op)[4],
F
Fabrice Bellard 已提交
1471 1472 1473
                               int motion_x, int motion_y, int h)
{
    UINT8 *ptr;
1474
    int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
1475
    int emu=0;
M
Michael Niedermayer 已提交
1476
#if 0    
M
Michael Niedermayer 已提交
1477 1478 1479 1480 1481
if(s->quarter_sample)
{
    motion_x>>=1;
    motion_y>>=1;
}
M
Michael Niedermayer 已提交
1482
#endif
F
Fabrice Bellard 已提交
1483 1484 1485 1486 1487 1488
    dxy = ((motion_y & 1) << 1) | (motion_x & 1);
    src_x = s->mb_x * 16 + (motion_x >> 1);
    src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
                
    /* WARNING: do no forget half pels */
    height = s->height >> field_based;
1489
    v_edge_pos = s->v_edge_pos >> field_based;
F
Fabrice Bellard 已提交
1490 1491 1492 1493 1494 1495
    src_x = clip(src_x, -16, s->width);
    if (src_x == s->width)
        dxy &= ~1;
    src_y = clip(src_y, -16, height);
    if (src_y == height)
        dxy &= ~2;
1496 1497
    linesize   = s->linesize << field_based;
    uvlinesize = s->uvlinesize << field_based;
F
Fabrice Bellard 已提交
1498 1499
    ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
    dest_y += dest_offset;
1500 1501

    if(s->flags&CODEC_FLAG_EMU_EDGE){
1502 1503
        if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->h_edge_pos
                              || src_y + (motion_y&1) + h  > v_edge_pos){
1504 1505 1506
            emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based, 
                             src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
            ptr= s->edge_emu_buffer + src_offset;
1507 1508 1509
            emu=1;
        }
    }
M
Michael Niedermayer 已提交
1510
    pix_op[0][dxy](dest_y, ptr, linesize, h);
F
Fabrice Bellard 已提交
1511

M
Michael Niedermayer 已提交
1512 1513
    if(s->flags&CODEC_FLAG_GRAY) return;

F
Fabrice Bellard 已提交
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
    if (s->out_format == FMT_H263) {
        dxy = 0;
        if ((motion_x & 3) != 0)
            dxy |= 1;
        if ((motion_y & 3) != 0)
            dxy |= 2;
        mx = motion_x >> 2;
        my = motion_y >> 2;
    } else {
        mx = motion_x / 2;
        my = motion_y / 2;
        dxy = ((my & 1) << 1) | (mx & 1);
        mx >>= 1;
        my >>= 1;
    }
    
    src_x = s->mb_x * 8 + mx;
    src_y = s->mb_y * (8 >> field_based) + my;
    src_x = clip(src_x, -8, s->width >> 1);
    if (src_x == (s->width >> 1))
        dxy &= ~1;
    src_y = clip(src_y, -8, height >> 1);
    if (src_y == (height >> 1))
        dxy &= ~2;
1538
    offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
F
Fabrice Bellard 已提交
1539
    ptr = ref_picture[1] + offset;
1540
    if(emu){
1541 1542 1543
        emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based, 
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
        ptr= s->edge_emu_buffer + (src_offset >> 1);
1544
    }
M
Michael Niedermayer 已提交
1545
    pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
1546

F
Fabrice Bellard 已提交
1547
    ptr = ref_picture[2] + offset;
1548
    if(emu){
1549 1550 1551
        emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based, 
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
        ptr= s->edge_emu_buffer + (src_offset >> 1);
1552
    }
M
Michael Niedermayer 已提交
1553
    pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
F
Fabrice Bellard 已提交
1554 1555
}

M
Michael Niedermayer 已提交
1556 1557 1558 1559
static inline void qpel_motion(MpegEncContext *s,
                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
                               int dest_offset,
                               UINT8 **ref_picture, int src_offset,
M
Michael Niedermayer 已提交
1560 1561
                               int field_based, op_pixels_func (*pix_op)[4],
                               qpel_mc_func (*qpix_op)[16],
M
Michael Niedermayer 已提交
1562 1563 1564
                               int motion_x, int motion_y, int h)
{
    UINT8 *ptr;
1565
    int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
M
Michael Niedermayer 已提交
1566
    int emu=0;
M
Michael Niedermayer 已提交
1567 1568 1569 1570 1571 1572

    dxy = ((motion_y & 3) << 2) | (motion_x & 3);
    src_x = s->mb_x * 16 + (motion_x >> 2);
    src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);

    height = s->height >> field_based;
1573
    v_edge_pos = s->v_edge_pos >> field_based;
M
Michael Niedermayer 已提交
1574 1575 1576 1577 1578 1579 1580
    src_x = clip(src_x, -16, s->width);
    if (src_x == s->width)
        dxy &= ~3;
    src_y = clip(src_y, -16, height);
    if (src_y == height)
        dxy &= ~12;
    linesize = s->linesize << field_based;
1581
    uvlinesize = s->uvlinesize << field_based;
M
Michael Niedermayer 已提交
1582 1583 1584
    ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
    dest_y += dest_offset;
//printf("%d %d %d\n", src_x, src_y, dxy);
M
Michael Niedermayer 已提交
1585 1586
    
    if(s->flags&CODEC_FLAG_EMU_EDGE){
1587 1588
        if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->h_edge_pos
                              || src_y + (motion_y&3) + h  > v_edge_pos){
1589 1590 1591
            emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based, 
                             src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
            ptr= s->edge_emu_buffer + src_offset;
M
Michael Niedermayer 已提交
1592 1593 1594
            emu=1;
        }
    }
1595 1596 1597 1598 1599 1600 1601 1602
    if(!field_based)
        qpix_op[0][dxy](dest_y, ptr, linesize);
    else{
        //damn interlaced mode
        //FIXME boundary mirroring is not exactly correct here
        qpix_op[1][dxy](dest_y  , ptr  , linesize);
        qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
    }
M
Michael Niedermayer 已提交
1603

M
Michael Niedermayer 已提交
1604 1605
    if(s->flags&CODEC_FLAG_GRAY) return;

1606 1607 1608
    if(field_based){
        mx= motion_x/2;
        my= motion_y>>1;
M
Michael Niedermayer 已提交
1609
    }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
        mx= (motion_x>>1)|(motion_x&1);
        my= (motion_y>>1)|(motion_y&1);
    }else{
        mx= motion_x/2;
        my= motion_y/2;
    }
    mx= (mx>>1)|(mx&1);
    my= (my>>1)|(my&1);
    dxy= (mx&1) | ((my&1)<<1);
    mx>>=1;
    my>>=1;
M
Michael Niedermayer 已提交
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630

    src_x = s->mb_x * 8 + mx;
    src_y = s->mb_y * (8 >> field_based) + my;
    src_x = clip(src_x, -8, s->width >> 1);
    if (src_x == (s->width >> 1))
        dxy &= ~1;
    src_y = clip(src_y, -8, height >> 1);
    if (src_y == (height >> 1))
        dxy &= ~2;

1631
    offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
M
Michael Niedermayer 已提交
1632
    ptr = ref_picture[1] + offset;
M
Michael Niedermayer 已提交
1633
    if(emu){
1634 1635 1636
        emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based, 
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
        ptr= s->edge_emu_buffer + (src_offset >> 1);
M
Michael Niedermayer 已提交
1637
    }
1638
    pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr,  uvlinesize, h >> 1);
M
Michael Niedermayer 已提交
1639
    
M
Michael Niedermayer 已提交
1640
    ptr = ref_picture[2] + offset;
M
Michael Niedermayer 已提交
1641
    if(emu){
1642 1643 1644
        emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based, 
                         src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
        ptr= s->edge_emu_buffer + (src_offset >> 1);
M
Michael Niedermayer 已提交
1645
    }
1646
    pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr,  uvlinesize, h >> 1);
M
Michael Niedermayer 已提交
1647 1648 1649
}


F
Fabrice Bellard 已提交
1650 1651 1652
static inline void MPV_motion(MpegEncContext *s, 
                              UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
                              int dir, UINT8 **ref_picture, 
M
Michael Niedermayer 已提交
1653
                              op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
F
Fabrice Bellard 已提交
1654 1655 1656 1657
{
    int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
    int mb_x, mb_y, i;
    UINT8 *ptr, *dest;
M
Michael Niedermayer 已提交
1658
    int emu=0;
F
Fabrice Bellard 已提交
1659 1660 1661 1662 1663 1664

    mb_x = s->mb_x;
    mb_y = s->mb_y;

    switch(s->mv_type) {
    case MV_TYPE_16X16:
M
Michael Niedermayer 已提交
1665
        if(s->mcsel){
1666 1667 1668 1669 1670 1671 1672
            if(s->real_sprite_warping_points==1){
                gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
                            ref_picture, 0);
            }else{
                gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
                            ref_picture, 0);
            }
M
Michael Niedermayer 已提交
1673
        }else if(s->quarter_sample){
M
Michael Niedermayer 已提交
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
            qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
                        ref_picture, 0,
                        0, pix_op, qpix_op,
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
        }else{
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
                        ref_picture, 0,
                        0, pix_op,
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
        }           
F
Fabrice Bellard 已提交
1684 1685
        break;
    case MV_TYPE_8X8:
M
Michael Niedermayer 已提交
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
        mx = 0;
        my = 0;
        if(s->quarter_sample){
            for(i=0;i<4;i++) {
                motion_x = s->mv[dir][i][0];
                motion_y = s->mv[dir][i][1];

                dxy = ((motion_y & 3) << 2) | (motion_x & 3);
                src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
                src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
                    
                /* WARNING: do no forget half pels */
                src_x = clip(src_x, -16, s->width);
                if (src_x == s->width)
                    dxy &= ~3;
                src_y = clip(src_y, -16, s->height);
                if (src_y == s->height)
                    dxy &= ~12;
                    
                ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
                if(s->flags&CODEC_FLAG_EMU_EDGE){
                    if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 8 > s->h_edge_pos
                                          || src_y + (motion_y&3) + 8 > s->v_edge_pos){
                        emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
                        ptr= s->edge_emu_buffer;
                    }
                }
                dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
                qpix_op[1][dxy](dest, ptr, s->linesize);

                mx += s->mv[dir][i][0]/2;
                my += s->mv[dir][i][1]/2;
            }
        }else{
            for(i=0;i<4;i++) {
                motion_x = s->mv[dir][i][0];
                motion_y = s->mv[dir][i][1];
F
Fabrice Bellard 已提交
1723

M
Michael Niedermayer 已提交
1724 1725 1726
                dxy = ((motion_y & 1) << 1) | (motion_x & 1);
                src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
                src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
F
Fabrice Bellard 已提交
1727
                    
M
Michael Niedermayer 已提交
1728 1729 1730 1731 1732 1733 1734
                /* WARNING: do no forget half pels */
                src_x = clip(src_x, -16, s->width);
                if (src_x == s->width)
                    dxy &= ~1;
                src_y = clip(src_y, -16, s->height);
                if (src_y == s->height)
                    dxy &= ~2;
F
Fabrice Bellard 已提交
1735
                    
M
Michael Niedermayer 已提交
1736 1737 1738 1739 1740 1741 1742
                ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
                if(s->flags&CODEC_FLAG_EMU_EDGE){
                    if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->h_edge_pos
                                          || src_y + (motion_y&1) + 8 > s->v_edge_pos){
                        emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
                        ptr= s->edge_emu_buffer;
                    }
M
Michael Niedermayer 已提交
1743
                }
M
Michael Niedermayer 已提交
1744 1745 1746 1747 1748
                dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
                pix_op[1][dxy](dest, ptr, s->linesize, 8);

                mx += s->mv[dir][i][0];
                my += s->mv[dir][i][1];
M
Michael Niedermayer 已提交
1749
            }
F
Fabrice Bellard 已提交
1750
        }
M
Michael Niedermayer 已提交
1751

M
Michael Niedermayer 已提交
1752
        if(s->flags&CODEC_FLAG_GRAY) break;
F
Fabrice Bellard 已提交
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
        /* In case of 8X8, we construct a single chroma motion vector
           with a special rounding */
        for(i=0;i<4;i++) {
        }
        if (mx >= 0)
            mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
        else {
            mx = -mx;
            mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
        }
        if (my >= 0)
            my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
        else {
            my = -my;
            my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
        }
        dxy = ((my & 1) << 1) | (mx & 1);
        mx >>= 1;
        my >>= 1;

        src_x = mb_x * 8 + mx;
        src_y = mb_y * 8 + my;
        src_x = clip(src_x, -8, s->width/2);
        if (src_x == s->width/2)
            dxy &= ~1;
        src_y = clip(src_y, -8, s->height/2);
        if (src_y == s->height/2)
            dxy &= ~2;
        
M
Michael Niedermayer 已提交
1782
        offset = (src_y * (s->uvlinesize)) + src_x;
F
Fabrice Bellard 已提交
1783
        ptr = ref_picture[1] + offset;
M
Michael Niedermayer 已提交
1784
        if(s->flags&CODEC_FLAG_EMU_EDGE){
1785 1786 1787
                if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->h_edge_pos>>1
                                      || src_y + (dxy>>1) + 8 > s->v_edge_pos>>1){
                    emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
M
Michael Niedermayer 已提交
1788 1789 1790 1791
                    ptr= s->edge_emu_buffer;
                    emu=1;
                }
            }
M
Michael Niedermayer 已提交
1792
        pix_op[1][dxy](dest_cb, ptr, s->uvlinesize, 8);
M
Michael Niedermayer 已提交
1793

F
Fabrice Bellard 已提交
1794
        ptr = ref_picture[2] + offset;
M
Michael Niedermayer 已提交
1795
        if(emu){
1796
            emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
M
Michael Niedermayer 已提交
1797 1798
            ptr= s->edge_emu_buffer;
        }
M
Michael Niedermayer 已提交
1799
        pix_op[1][dxy](dest_cr, ptr, s->uvlinesize, 8);
F
Fabrice Bellard 已提交
1800 1801 1802
        break;
    case MV_TYPE_FIELD:
        if (s->picture_structure == PICT_FRAME) {
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
            if(s->quarter_sample){
                /* top field */
                qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
                            ref_picture, s->field_select[dir][0] ? s->linesize : 0,
                            1, pix_op, qpix_op,
                            s->mv[dir][0][0], s->mv[dir][0][1], 8);
                /* bottom field */
                qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
                            ref_picture, s->field_select[dir][1] ? s->linesize : 0,
                            1, pix_op, qpix_op,
                            s->mv[dir][1][0], s->mv[dir][1][1], 8);
            }else{
                /* top field */       
                mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
                            ref_picture, s->field_select[dir][0] ? s->linesize : 0,
                            1, pix_op,
                            s->mv[dir][0][0], s->mv[dir][0][1], 8);
                /* bottom field */
                mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
                            ref_picture, s->field_select[dir][1] ? s->linesize : 0,
                            1, pix_op,
                            s->mv[dir][1][0], s->mv[dir][1][1], 8);
            }
F
Fabrice Bellard 已提交
1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
        } else {
            

        }
        break;
    }
}


/* put block[] to dest[] */
static inline void put_dct(MpegEncContext *s, 
                           DCTELEM *block, int i, UINT8 *dest, int line_size)
{
1839
    s->dct_unquantize(s, block, i, s->qscale);
1840
    s->idct_put (dest, line_size, block);
F
Fabrice Bellard 已提交
1841 1842 1843 1844 1845 1846
}

/* add block[] to dest[] */
static inline void add_dct(MpegEncContext *s, 
                           DCTELEM *block, int i, UINT8 *dest, int line_size)
{
M
Michael Niedermayer 已提交
1847
    if (s->block_last_index[i] >= 0) {
1848
        s->idct_add (dest, line_size, block);
M
Michael Niedermayer 已提交
1849 1850
    }
}
1851

M
Michael Niedermayer 已提交
1852 1853 1854
static inline void add_dequant_dct(MpegEncContext *s, 
                           DCTELEM *block, int i, UINT8 *dest, int line_size)
{
F
Fabrice Bellard 已提交
1855
    if (s->block_last_index[i] >= 0) {
M
Michael Niedermayer 已提交
1856
        s->dct_unquantize(s, block, i, s->qscale);
1857

1858
        s->idct_add (dest, line_size, block);
F
Fabrice Bellard 已提交
1859 1860 1861
    }
}

1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894
/**
 * cleans dc, ac, coded_block for the current non intra MB
 */
void ff_clean_intra_table_entries(MpegEncContext *s)
{
    int wrap = s->block_wrap[0];
    int xy = s->block_index[0];
    
    s->dc_val[0][xy           ] = 
    s->dc_val[0][xy + 1       ] = 
    s->dc_val[0][xy     + wrap] =
    s->dc_val[0][xy + 1 + wrap] = 1024;
    /* ac pred */
    memset(s->ac_val[0][xy       ], 0, 32 * sizeof(INT16));
    memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(INT16));
    if (s->msmpeg4_version>=3) {
        s->coded_block[xy           ] =
        s->coded_block[xy + 1       ] =
        s->coded_block[xy     + wrap] =
        s->coded_block[xy + 1 + wrap] = 0;
    }
    /* chroma */
    wrap = s->block_wrap[4];
    xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
    s->dc_val[1][xy] =
    s->dc_val[2][xy] = 1024;
    /* ac pred */
    memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
    memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
    
    s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0;
}

F
Fabrice Bellard 已提交
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906
/* generic function called after a macroblock has been parsed by the
   decoder or after it has been encoded by the encoder.

   Important variables used:
   s->mb_intra : true if intra macroblock
   s->mv_dir   : motion vector direction
   s->mv_type  : motion vector type
   s->mv       : motion vector
   s->interlaced_dct : true if interlaced dct used (mpeg2)
 */
void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
{
1907
    int mb_x, mb_y;
M
Michael Niedermayer 已提交
1908
    const int mb_xy = s->mb_y * s->mb_width + s->mb_x;
F
Fabrice Bellard 已提交
1909 1910 1911 1912

    mb_x = s->mb_x;
    mb_y = s->mb_y;

M
cleanup  
Michael Niedermayer 已提交
1913
    s->current_picture.qscale_table[mb_xy]= s->qscale;
1914

F
Fabrice Bellard 已提交
1915 1916
    /* update DC predictors for P macroblocks */
    if (!s->mb_intra) {
1917
        if (s->h263_pred || s->h263_aic) {
M
Michael Niedermayer 已提交
1918
            if(s->mbintra_table[mb_xy])
1919
                ff_clean_intra_table_entries(s);
F
Fabrice Bellard 已提交
1920
        } else {
1921 1922
            s->last_dc[0] =
            s->last_dc[1] =
F
Fabrice Bellard 已提交
1923 1924 1925
            s->last_dc[2] = 128 << s->intra_dc_precision;
        }
    }
1926
    else if (s->h263_pred || s->h263_aic)
M
Michael Niedermayer 已提交
1927
        s->mbintra_table[mb_xy]=1;
1928

1929
    /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
M
Michael Niedermayer 已提交
1930
    if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
F
Fabrice Bellard 已提交
1931
        
M
Michael Niedermayer 已提交
1932 1933
        const int wrap = s->block_wrap[0];
        const int xy = s->block_index[0];
1934
        const int mb_index= s->mb_x + s->mb_y*s->mb_width;
1935
        if(s->mv_type == MV_TYPE_8X8){
1936
            s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_4MV;
1937 1938 1939 1940 1941
        } else {
            int motion_x, motion_y;
            if (s->mb_intra) {
                motion_x = 0;
                motion_y = 0;
1942 1943
                if(s->co_located_type_table)
                    s->co_located_type_table[mb_index]= 0;
1944 1945 1946
            } else if (s->mv_type == MV_TYPE_16X16) {
                motion_x = s->mv[0][0][0];
                motion_y = s->mv[0][0][1];
1947 1948
                if(s->co_located_type_table)
                    s->co_located_type_table[mb_index]= 0;
1949
            } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
1950
                int i;
1951 1952 1953
                motion_x = s->mv[0][0][0] + s->mv[0][1][0];
                motion_y = s->mv[0][0][1] + s->mv[0][1][1];
                motion_x = (motion_x>>1) | (motion_x&1);
1954 1955 1956 1957 1958 1959
                for(i=0; i<2; i++){
                    s->field_mv_table[mb_index][i][0]= s->mv[0][i][0];
                    s->field_mv_table[mb_index][i][1]= s->mv[0][i][1];
                    s->field_select_table[mb_index][i]= s->field_select[0][i];
                }
                s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_FIELDMV;
1960
            }
F
Fabrice Bellard 已提交
1961
            /* no update if 8X8 because it has been done during parsing */
1962 1963 1964 1965 1966 1967 1968 1969
            s->motion_val[xy][0] = motion_x;
            s->motion_val[xy][1] = motion_y;
            s->motion_val[xy + 1][0] = motion_x;
            s->motion_val[xy + 1][1] = motion_y;
            s->motion_val[xy + wrap][0] = motion_x;
            s->motion_val[xy + wrap][1] = motion_y;
            s->motion_val[xy + 1 + wrap][0] = motion_x;
            s->motion_val[xy + 1 + wrap][1] = motion_y;
F
Fabrice Bellard 已提交
1970 1971 1972
        }
    }
    
1973
    if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
F
Fabrice Bellard 已提交
1974
        UINT8 *dest_y, *dest_cb, *dest_cr;
M
Michael Niedermayer 已提交
1975
        int dct_linesize, dct_offset;
M
Michael Niedermayer 已提交
1976 1977
        op_pixels_func (*op_pix)[4];
        qpel_mc_func (*op_qpix)[16];
1978

M
cleanup  
Michael Niedermayer 已提交
1979
        /* avoid copy if macroblock skipped in last frame too */
1980
        if (s->pict_type != B_TYPE) {
M
cleanup  
Michael Niedermayer 已提交
1981 1982 1983 1984 1985
            s->current_picture.mbskip_table[mb_xy]= s->mb_skiped;
        }

        /* skip only during decoding as we might trash the buffers during encoding a bit */
        if(!s->encoding){
M
Michael Niedermayer 已提交
1986
            UINT8 *mbskip_ptr = &s->mbskip_table[mb_xy];
M
cleanup  
Michael Niedermayer 已提交
1987
            const int age= s->current_picture.age;
M
Michael Niedermayer 已提交
1988

M
cleanup  
Michael Niedermayer 已提交
1989 1990 1991 1992 1993 1994
            assert(age);

            if (s->mb_skiped) {
                s->mb_skiped= 0;
                assert(s->pict_type!=I_TYPE);
 
M
Michael Niedermayer 已提交
1995 1996 1997
                (*mbskip_ptr) ++; /* indicate that this time we skiped it */
                if(*mbskip_ptr >99) *mbskip_ptr= 99;

M
cleanup  
Michael Niedermayer 已提交
1998 1999 2000 2001 2002 2003 2004 2005
                /* if previous was skipped too, then nothing to do !  */
                if (*mbskip_ptr >= age){
//if(s->pict_type!=B_TYPE && s->mb_x==0) printf("\n");
//if(s->pict_type!=B_TYPE) printf("%d%d ", *mbskip_ptr, age);
                    if(s->pict_type!=B_TYPE) return;
                    if(s->avctx->draw_horiz_band==NULL && *mbskip_ptr > age) return; 
                    /* we dont draw complete frames here so we cant skip */
                }
2006 2007 2008
            } else {
                *mbskip_ptr = 0; /* not skipped */
            }
M
cleanup  
Michael Niedermayer 已提交
2009 2010
        }else
            s->mb_skiped= 0;
F
Fabrice Bellard 已提交
2011

M
Michael Niedermayer 已提交
2012
        if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band){
M
cleanup  
Michael Niedermayer 已提交
2013 2014 2015
            dest_y  = s->current_picture.data[0] + mb_x * 16;
            dest_cb = s->current_picture.data[1] + mb_x * 8;
            dest_cr = s->current_picture.data[2] + mb_x * 8;
M
Michael Niedermayer 已提交
2016
        }else{
M
cleanup  
Michael Niedermayer 已提交
2017 2018 2019
            dest_y  = s->current_picture.data[0] + (mb_y * 16* s->linesize  ) + mb_x * 16;
            dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
            dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
M
Michael Niedermayer 已提交
2020
        }
F
Fabrice Bellard 已提交
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031

        if (s->interlaced_dct) {
            dct_linesize = s->linesize * 2;
            dct_offset = s->linesize;
        } else {
            dct_linesize = s->linesize;
            dct_offset = s->linesize * 8;
        }

        if (!s->mb_intra) {
            /* motion handling */
2032
            /* decoding or more than one mb_type (MC was allready done otherwise) */
M
Michael Niedermayer 已提交
2033
            if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){
2034
                if ((!s->no_rounding) || s->pict_type==B_TYPE){                
2035 2036
		    op_pix = s->dsp.put_pixels_tab;
                    op_qpix= s->dsp.put_qpel_pixels_tab;
2037
                }else{
2038 2039
                    op_pix = s->dsp.put_no_rnd_pixels_tab;
                    op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
2040
                }
F
Fabrice Bellard 已提交
2041

2042
                if (s->mv_dir & MV_DIR_FORWARD) {
M
cleanup  
Michael Niedermayer 已提交
2043
                    MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
2044 2045
		    op_pix = s->dsp.avg_pixels_tab;
                    op_qpix= s->dsp.avg_qpel_pixels_tab;
2046 2047
                }
                if (s->mv_dir & MV_DIR_BACKWARD) {
M
cleanup  
Michael Niedermayer 已提交
2048
                    MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
2049
                }
F
Fabrice Bellard 已提交
2050 2051
            }

M
Michael Niedermayer 已提交
2052
            /* skip dequant / idct if we are really late ;) */
2053
            if(s->hurry_up>1) return;
M
Michael Niedermayer 已提交
2054

F
Fabrice Bellard 已提交
2055
            /* add dct residue */
2056 2057
            if(s->encoding || !(   s->mpeg2 || s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO 
                                || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
M
Michael Niedermayer 已提交
2058 2059 2060 2061 2062
                add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
                add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
                add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
                add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);

M
Michael Niedermayer 已提交
2063
                if(!(s->flags&CODEC_FLAG_GRAY)){
M
Michael Niedermayer 已提交
2064 2065
                    add_dequant_dct(s, block[4], 4, dest_cb, s->uvlinesize);
                    add_dequant_dct(s, block[5], 5, dest_cr, s->uvlinesize);
M
Michael Niedermayer 已提交
2066
                }
M
Michael Niedermayer 已提交
2067 2068 2069 2070 2071
            } else {
                add_dct(s, block[0], 0, dest_y, dct_linesize);
                add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
                add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
                add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
F
Fabrice Bellard 已提交
2072

M
Michael Niedermayer 已提交
2073
                if(!(s->flags&CODEC_FLAG_GRAY)){
M
Michael Niedermayer 已提交
2074 2075
                    add_dct(s, block[4], 4, dest_cb, s->uvlinesize);
                    add_dct(s, block[5], 5, dest_cr, s->uvlinesize);
M
Michael Niedermayer 已提交
2076
                }
M
Michael Niedermayer 已提交
2077
            }
F
Fabrice Bellard 已提交
2078 2079
        } else {
            /* dct only in intra block */
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
            if(s->encoding || !(s->mpeg2 || s->codec_id==CODEC_ID_MPEG1VIDEO)){
                put_dct(s, block[0], 0, dest_y, dct_linesize);
                put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
                put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
                put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);

                if(!(s->flags&CODEC_FLAG_GRAY)){
                    put_dct(s, block[4], 4, dest_cb, s->uvlinesize);
                    put_dct(s, block[5], 5, dest_cr, s->uvlinesize);
                }
            }else{
                s->idct_put(dest_y                 , dct_linesize, block[0]);
                s->idct_put(dest_y              + 8, dct_linesize, block[1]);
                s->idct_put(dest_y + dct_offset    , dct_linesize, block[2]);
                s->idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);

                if(!(s->flags&CODEC_FLAG_GRAY)){
                    s->idct_put(dest_cb, s->uvlinesize, block[4]);
                    s->idct_put(dest_cr, s->uvlinesize, block[5]);
                }
M
Michael Niedermayer 已提交
2100
            }
F
Fabrice Bellard 已提交
2101 2102 2103 2104
        }
    }
}

2105
static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120
{
    static const char tab[64]=
        {3,2,2,1,1,1,1,1,
         1,1,1,1,1,1,1,1,
         1,1,1,1,1,1,1,1,
         0,0,0,0,0,0,0,0,
         0,0,0,0,0,0,0,0,
         0,0,0,0,0,0,0,0,
         0,0,0,0,0,0,0,0,
         0,0,0,0,0,0,0,0};
    int score=0;
    int run=0;
    int i;
    DCTELEM *block= s->block[n];
    const int last_index= s->block_last_index[n];
2121
    int skip_dc;
2122

2123 2124 2125
    if(threshold<0){
        skip_dc=0;
        threshold= -threshold;
2126 2127
    }else
        skip_dc=1;
2128

2129 2130 2131 2132
    /* are all which we could set to zero are allready zero? */
    if(last_index<=skip_dc - 1) return;

    for(i=0; i<=last_index; i++){
2133
        const int j = s->intra_scantable.permutated[i];
2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
        const int level = ABS(block[j]);
        if(level==1){
            if(skip_dc && i==0) continue;
            score+= tab[run];
            run=0;
        }else if(level>1){
            return;
        }else{
            run++;
        }
    }
    if(score >= threshold) return;
    for(i=skip_dc; i<=last_index; i++){
2147
        const int j = s->intra_scantable.permutated[i];
2148 2149 2150 2151 2152 2153
        block[j]=0;
    }
    if(block[0]) s->block_last_index[n]= 0;
    else         s->block_last_index[n]= -1;
}

2154 2155 2156 2157 2158
static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
{
    int i;
    const int maxlevel= s->max_qcoeff;
    const int minlevel= s->min_qcoeff;
2159 2160 2161 2162 2163 2164 2165 2166
    
    if(s->mb_intra){
        i=1; //skip clipping of intra dc
    }else
        i=0;
    
    for(;i<=last_index; i++){
        const int j= s->intra_scantable.permutated[i];
2167 2168 2169 2170 2171 2172 2173
        int level = block[j];
       
        if     (level>maxlevel) level=maxlevel;
        else if(level<minlevel) level=minlevel;
        block[j]= level;
    }
}
2174

2175 2176 2177
static inline void requantize_coeffs(MpegEncContext *s, DCTELEM block[64], int oldq, int newq, int n)
{
    int i;
2178 2179 2180 2181

    if(s->mb_intra){
        i=1; //skip clipping of intra dc
         //FIXME requantize, note (mpeg1/h263/h263p-aic dont need it,...)
2182 2183 2184 2185
    }else
        i=0;
    
    for(;i<=s->block_last_index[n]; i++){
2186
        const int j = s->intra_scantable.permutated[i];
2187 2188 2189 2190 2191 2192
        int level = block[j];
        
        block[j]= ROUNDED_DIV(level*oldq, newq);
    }

    for(i=s->block_last_index[n]; i>=0; i--){
2193
        const int j = s->intra_scantable.permutated[i];
2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
        if(block[j]) break;
    }
    s->block_last_index[n]= i;
}

static inline void auto_requantize_coeffs(MpegEncContext *s, DCTELEM block[6][64])
{
    int i,n, newq;
    const int maxlevel= s->max_qcoeff;
    const int minlevel= s->min_qcoeff;
    int largest=0, smallest=0;

    assert(s->adaptive_quant);
    
    for(n=0; n<6; n++){
2209 2210 2211 2212 2213
        if(s->mb_intra){
            i=1; //skip clipping of intra dc
             //FIXME requantize, note (mpeg1/h263/h263p-aic dont need it,...)
        }else
            i=0;
2214 2215

        for(;i<=s->block_last_index[n]; i++){
2216
            const int j = s->intra_scantable.permutated[i];
2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306
            int level = block[n][j];
            if(largest  < level) largest = level;
            if(smallest > level) smallest= level;
        }
    }
    
    for(newq=s->qscale+1; newq<32; newq++){
        if(   ROUNDED_DIV(smallest*s->qscale, newq) >= minlevel
           && ROUNDED_DIV(largest *s->qscale, newq) <= maxlevel) 
            break;
    }
        
    if(s->out_format==FMT_H263){
        /* h263 like formats cannot change qscale by more than 2 easiely */
        if(s->avctx->qmin + 2 < newq)
            newq= s->avctx->qmin + 2;
    }

    for(n=0; n<6; n++){
        requantize_coeffs(s, block[n], s->qscale, newq, n);
        clip_coeffs(s, block[n], s->block_last_index[n]);
    }
     
    s->dquant+= newq - s->qscale;
    s->qscale= newq;
}
#if 0
static int pix_vcmp16x8(UINT8 *s, int stride){ //FIXME move to dsputil & optimize
    int score=0;
    int x,y;
    
    for(y=0; y<7; y++){
        for(x=0; x<16; x+=4){
            score+= ABS(s[x  ] - s[x  +stride]) + ABS(s[x+1] - s[x+1+stride]) 
                   +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]);
        }
        s+= stride;
    }
    
    return score;
}

static int pix_diff_vcmp16x8(UINT8 *s1, UINT8*s2, int stride){ //FIXME move to dsputil & optimize
    int score=0;
    int x,y;
    
    for(y=0; y<7; y++){
        for(x=0; x<16; x++){
            score+= ABS(s1[x  ] - s2[x ] - s1[x  +stride] + s2[x +stride]);
        }
        s1+= stride;
        s2+= stride;
    }
    
    return score;
}
#else
#define SQ(a) ((a)*(a))

static int pix_vcmp16x8(UINT8 *s, int stride){ //FIXME move to dsputil & optimize
    int score=0;
    int x,y;
    
    for(y=0; y<7; y++){
        for(x=0; x<16; x+=4){
            score+= SQ(s[x  ] - s[x  +stride]) + SQ(s[x+1] - s[x+1+stride]) 
                   +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
        }
        s+= stride;
    }
    
    return score;
}

static int pix_diff_vcmp16x8(UINT8 *s1, UINT8*s2, int stride){ //FIXME move to dsputil & optimize
    int score=0;
    int x,y;
    
    for(y=0; y<7; y++){
        for(x=0; x<16; x++){
            score+= SQ(s1[x  ] - s2[x ] - s1[x  +stride] + s2[x +stride]);
        }
        s1+= stride;
        s2+= stride;
    }
    
    return score;
}

#endif
2307 2308 2309

void ff_draw_horiz_band(MpegEncContext *s){
    if (    s->avctx->draw_horiz_band 
M
Michael Niedermayer 已提交
2310
        && (s->last_picture.data[0] || s->low_delay) ) {
2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322
        UINT8 *src_ptr[3];
        int y, h, offset;
        y = s->mb_y * 16;
        h = s->height - y;
        if (h > 16)
            h = 16;

        if(s->pict_type==B_TYPE)
            offset = 0;
        else
            offset = y * s->linesize;

M
Michael Niedermayer 已提交
2323
        if(s->pict_type==B_TYPE || s->low_delay){
M
cleanup  
Michael Niedermayer 已提交
2324 2325 2326
            src_ptr[0] = s->current_picture.data[0] + offset;
            src_ptr[1] = s->current_picture.data[1] + (offset >> 2);
            src_ptr[2] = s->current_picture.data[2] + (offset >> 2);
2327
        } else {
M
cleanup  
Michael Niedermayer 已提交
2328 2329 2330
            src_ptr[0] = s->last_picture.data[0] + offset;
            src_ptr[1] = s->last_picture.data[1] + (offset >> 2);
            src_ptr[2] = s->last_picture.data[2] + (offset >> 2);
2331
        }
M
cleanup  
Michael Niedermayer 已提交
2332 2333
        emms_c();

2334 2335 2336 2337 2338
        s->avctx->draw_horiz_band(s->avctx, src_ptr, s->linesize,
                               y, s->width, h);
    }
}

2339
static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
F
Fabrice Bellard 已提交
2340
{
2341 2342 2343
    const int mb_x= s->mb_x;
    const int mb_y= s->mb_y;
    int i;
2344
    int skip_dct[6];
2345 2346
    int dct_offset   = s->linesize*8; //default for progressive frames
    
2347
    for(i=0; i<6; i++) skip_dct[i]=0;
2348 2349
    
    if(s->adaptive_quant){
M
cleanup  
Michael Niedermayer 已提交
2350
        s->dquant= s->current_picture.qscale_table[mb_x + mb_y*s->mb_width] - s->qscale;
2351 2352

        if(s->out_format==FMT_H263){
2353 2354
            if     (s->dquant> 2) s->dquant= 2;
            else if(s->dquant<-2) s->dquant=-2;
2355 2356 2357
        }
            
        if(s->codec_id==CODEC_ID_MPEG4){        
2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368
            if(!s->mb_intra){
                assert(s->dquant==0 || s->mv_type!=MV_TYPE_8X8);

                if(s->mv_dir&MV_DIRECT)
                    s->dquant=0;
            }
        }
        s->qscale+= s->dquant;
        s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
        s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
    }
2369

2370 2371
    if (s->mb_intra) {
        UINT8 *ptr;
2372
        int wrap_y;
2373
        int emu=0;
2374

2375
        wrap_y = s->linesize;
M
cleanup  
Michael Niedermayer 已提交
2376
        ptr = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
2377

2378
        if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
2379
            emulated_edge_mc(s, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
2380 2381 2382
            ptr= s->edge_emu_buffer;
            emu=1;
        }
2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398
        
        if(s->flags&CODEC_FLAG_INTERLACED_DCT){
            int progressive_score, interlaced_score;
            
            progressive_score= pix_vcmp16x8(ptr, wrap_y  ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );
            interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y  , wrap_y*2);
            
            if(progressive_score > interlaced_score + 100){
                s->interlaced_dct=1;
            
                dct_offset= wrap_y;
                wrap_y<<=1;
            }else
                s->interlaced_dct=0;
        }
        
2399 2400 2401 2402
	s->dsp.get_pixels(s->block[0], ptr                 , wrap_y);
        s->dsp.get_pixels(s->block[1], ptr              + 8, wrap_y);
        s->dsp.get_pixels(s->block[2], ptr + dct_offset    , wrap_y);
        s->dsp.get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
2403

M
Michael Niedermayer 已提交
2404 2405 2406 2407
        if(s->flags&CODEC_FLAG_GRAY){
            skip_dct[4]= 1;
            skip_dct[5]= 1;
        }else{
2408
            int wrap_c = s->uvlinesize;
M
cleanup  
Michael Niedermayer 已提交
2409
            ptr = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
2410
            if(emu){
2411
                emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2412 2413
                ptr= s->edge_emu_buffer;
            }
2414
	    s->dsp.get_pixels(s->block[4], ptr, wrap_c);
2415

M
cleanup  
Michael Niedermayer 已提交
2416
            ptr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
2417
            if(emu){
2418
                emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
2419 2420
                ptr= s->edge_emu_buffer;
            }
2421
            s->dsp.get_pixels(s->block[5], ptr, wrap_c);
M
Michael Niedermayer 已提交
2422
        }
2423
    }else{
M
Michael Niedermayer 已提交
2424 2425
        op_pixels_func (*op_pix)[4];
        qpel_mc_func (*op_qpix)[16];
2426
        UINT8 *dest_y, *dest_cb, *dest_cr;
2427 2428
        UINT8 *ptr_y, *ptr_cb, *ptr_cr;
        int wrap_y, wrap_c;
2429
        int emu=0;
2430

M
cleanup  
Michael Niedermayer 已提交
2431 2432 2433
        dest_y  = s->current_picture.data[0] + (mb_y * 16 * s->linesize    ) + mb_x * 16;
        dest_cb = s->current_picture.data[1] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;
        dest_cr = s->current_picture.data[2] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;
2434
        wrap_y = s->linesize;
2435
        wrap_c = s->uvlinesize;
M
cleanup  
Michael Niedermayer 已提交
2436 2437 2438
        ptr_y  = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
        ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
        ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
2439

2440
        if ((!s->no_rounding) || s->pict_type==B_TYPE){
2441 2442
	    op_pix = s->dsp.put_pixels_tab;
            op_qpix= s->dsp.put_qpel_pixels_tab;
M
Michael Niedermayer 已提交
2443
        }else{
2444 2445
            op_pix = s->dsp.put_no_rnd_pixels_tab;
            op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
2446 2447 2448
        }

        if (s->mv_dir & MV_DIR_FORWARD) {
M
cleanup  
Michael Niedermayer 已提交
2449
            MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
2450 2451
            op_pix = s->dsp.avg_pixels_tab;
            op_qpix= s->dsp.avg_qpel_pixels_tab;
2452 2453
        }
        if (s->mv_dir & MV_DIR_BACKWARD) {
M
cleanup  
Michael Niedermayer 已提交
2454
            MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
2455
        }
2456

2457 2458 2459 2460 2461
        if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
            emulated_edge_mc(s, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
            ptr_y= s->edge_emu_buffer;
            emu=1;
        }
2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479
        
        if(s->flags&CODEC_FLAG_INTERLACED_DCT){
            int progressive_score, interlaced_score;
            
            progressive_score= pix_diff_vcmp16x8(ptr_y           , dest_y           , wrap_y  ) 
                             + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y  );
            interlaced_score = pix_diff_vcmp16x8(ptr_y           , dest_y           , wrap_y*2)
                             + pix_diff_vcmp16x8(ptr_y + wrap_y  , dest_y + wrap_y  , wrap_y*2);
            
            if(progressive_score > interlaced_score + 600){
                s->interlaced_dct=1;
            
                dct_offset= wrap_y;
                wrap_y<<=1;
            }else
                s->interlaced_dct=0;
        }
        
2480 2481 2482 2483
	s->dsp.diff_pixels(s->block[0], ptr_y                 , dest_y                 , wrap_y);
        s->dsp.diff_pixels(s->block[1], ptr_y              + 8, dest_y              + 8, wrap_y);
        s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset    , dest_y + dct_offset    , wrap_y);
        s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
M
Michael Niedermayer 已提交
2484 2485 2486 2487 2488
        
        if(s->flags&CODEC_FLAG_GRAY){
            skip_dct[4]= 1;
            skip_dct[5]= 1;
        }else{
2489 2490 2491 2492
            if(emu){
                emulated_edge_mc(s, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
                ptr_cb= s->edge_emu_buffer;
            }
2493
            s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2494 2495 2496 2497
            if(emu){
                emulated_edge_mc(s, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
                ptr_cr= s->edge_emu_buffer;
            }
2498
            s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
M
Michael Niedermayer 已提交
2499
        }
2500
        /* pre quantization */         
M
cleanup  
Michael Niedermayer 已提交
2501
        if(s->current_picture.mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
2502
            //FIXME optimize
2503 2504 2505 2506
	    if(s->dsp.pix_abs8x8(ptr_y               , dest_y               , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
            if(s->dsp.pix_abs8x8(ptr_y            + 8, dest_y            + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
            if(s->dsp.pix_abs8x8(ptr_y +dct_offset   , dest_y +dct_offset   , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
            if(s->dsp.pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
2507 2508
            if(s->dsp.pix_abs8x8(ptr_cb              , dest_cb              , wrap_c) < 20*s->qscale) skip_dct[4]= 1;
            if(s->dsp.pix_abs8x8(ptr_cr              , dest_cr              , wrap_c) < 20*s->qscale) skip_dct[5]= 1;
2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524
#if 0
{
 static int stat[7];
 int num=0;
 for(i=0; i<6; i++)
  if(skip_dct[i]) num++;
 stat[num]++;
 
 if(s->mb_x==0 && s->mb_y==0){
  for(i=0; i<7; i++){
   printf("%6d %1d\n", stat[i], i);
  }
 }
}
#endif
        }
2525

2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541
    }
            
#if 0
            {
                float adap_parm;
                
                adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
                            ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
            
                printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d", 
                        (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P', 
                        s->qscale, adap_parm, s->qscale*adap_parm,
                        s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
            }
#endif
    /* DCT & quantize */
2542 2543 2544
    if(s->out_format==FMT_MJPEG){
        for(i=0;i<6;i++) {
            int overflow;
2545
            s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
2546
            if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
2547 2548 2549
        }
    }else{
        for(i=0;i<6;i++) {
2550 2551
            if(!skip_dct[i]){
                int overflow;
2552
                s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2553
            // FIXME we could decide to change to quantizer instead of clipping
2554 2555
            // JS: I don't think that would be a good idea it could lower quality instead
            //     of improve it. Just INTRADC clipping deserves changes in quantizer
2556 2557 2558
                if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
            }else
                s->block_last_index[i]= -1;
2559
        }
2560 2561
        if(s->luma_elim_threshold && !s->mb_intra)
            for(i=0; i<4; i++)
2562
                dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2563 2564
        if(s->chroma_elim_threshold && !s->mb_intra)
            for(i=4; i<6; i++)
2565
                dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2566 2567
    }

M
Michael Niedermayer 已提交
2568 2569 2570 2571 2572 2573 2574
    if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
        s->block_last_index[4]=
        s->block_last_index[5]= 0;
        s->block[4][0]=
        s->block[5][0]= 128;
    }

2575
#ifdef CONFIG_ENCODERS
2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592
    /* huffman encode */
    switch(s->out_format) {
    case FMT_MPEG1:
        mpeg1_encode_mb(s, s->block, motion_x, motion_y);
        break;
    case FMT_H263:
        if (s->h263_msmpeg4)
            msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
        else if(s->h263_pred)
            mpeg4_encode_mb(s, s->block, motion_x, motion_y);
        else
            h263_encode_mb(s, s->block, motion_x, motion_y);
        break;
    case FMT_MJPEG:
        mjpeg_encode_mb(s, s->block);
        break;
    }
2593
#endif
2594 2595
}

2596
void ff_copy_bits(PutBitContext *pb, UINT8 *src, int length)
2597
{
2598 2599 2600 2601
    int bytes= length>>4;
    int bits= length&15;
    int i;

2602 2603
    if(length==0) return;

2604 2605
    for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
    put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
2606 2607
}

2608
static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622
    int i;

    memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?

    /* mpeg1 */
    d->mb_incr= s->mb_incr;
    for(i=0; i<3; i++)
        d->last_dc[i]= s->last_dc[i];
    
    /* statistics */
    d->mv_bits= s->mv_bits;
    d->i_tex_bits= s->i_tex_bits;
    d->p_tex_bits= s->p_tex_bits;
    d->i_count= s->i_count;
2623 2624
    d->f_count= s->f_count;
    d->b_count= s->b_count;
2625 2626
    d->skip_count= s->skip_count;
    d->misc_bits= s->misc_bits;
2627
    d->last_bits= 0;
2628 2629

    d->mb_skiped= s->mb_skiped;
M
Michael Niedermayer 已提交
2630
    d->qscale= s->qscale;
2631 2632
}

2633
static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648
    int i;

    memcpy(d->mv, s->mv, 2*4*2*sizeof(int)); 
    memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
    
    /* mpeg1 */
    d->mb_incr= s->mb_incr;
    for(i=0; i<3; i++)
        d->last_dc[i]= s->last_dc[i];
    
    /* statistics */
    d->mv_bits= s->mv_bits;
    d->i_tex_bits= s->i_tex_bits;
    d->p_tex_bits= s->p_tex_bits;
    d->i_count= s->i_count;
2649 2650
    d->f_count= s->f_count;
    d->b_count= s->b_count;
2651 2652 2653 2654
    d->skip_count= s->skip_count;
    d->misc_bits= s->misc_bits;

    d->mb_intra= s->mb_intra;
2655
    d->mb_skiped= s->mb_skiped;
2656 2657 2658
    d->mv_type= s->mv_type;
    d->mv_dir= s->mv_dir;
    d->pb= s->pb;
2659 2660 2661 2662
    if(s->data_partitioning){
        d->pb2= s->pb2;
        d->tex_pb= s->tex_pb;
    }
2663 2664 2665
    d->block= s->block;
    for(i=0; i<6; i++)
        d->block_last_index[i]= s->block_last_index[i];
2666
    d->interlaced_dct= s->interlaced_dct;
M
Michael Niedermayer 已提交
2667
    d->qscale= s->qscale;
2668 2669
}

2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699
static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type, 
                           PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
                           int *dmin, int *next_block, int motion_x, int motion_y)
{
    int bits_count;
    
    copy_context_before_encode(s, backup, type);

    s->block= s->blocks[*next_block];
    s->pb= pb[*next_block];
    if(s->data_partitioning){
        s->pb2   = pb2   [*next_block];
        s->tex_pb= tex_pb[*next_block];
    }

    encode_mb(s, motion_x, motion_y);

    bits_count= get_bit_count(&s->pb);
    if(s->data_partitioning){
        bits_count+= get_bit_count(&s->pb2);
        bits_count+= get_bit_count(&s->tex_pb);
    }

    if(bits_count<*dmin){
        *dmin= bits_count;
        *next_block^=1;

        copy_context_after_encode(best, s, type);
    }
}
2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715
                
static inline int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
    uint32_t *sq = squareTbl + 256;
    int acc=0;
    int x,y;
    
    if(w==16 && h==16) 
        return s->dsp.pix_norm(src1, src2, stride);
    
    for(y=0; y<h; y++){
        for(x=0; x<w; x++){
            acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
        } 
    }
    return acc;
}
2716

2717 2718
static void encode_picture(MpegEncContext *s, int picture_number)
{
M
Michael Niedermayer 已提交
2719
    int mb_x, mb_y, pdif = 0;
2720
    int i;
2721
    int bits;
2722
    MpegEncContext best_s, backup_s;
2723 2724 2725 2726 2727 2728 2729 2730 2731 2732
    UINT8 bit_buf[2][3000];
    UINT8 bit_buf2[2][3000];
    UINT8 bit_buf_tex[2][3000];
    PutBitContext pb[2], pb2[2], tex_pb[2];

    for(i=0; i<2; i++){
        init_put_bits(&pb    [i], bit_buf    [i], 3000, NULL, NULL);
        init_put_bits(&pb2   [i], bit_buf2   [i], 3000, NULL, NULL);
        init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
    }
F
Fabrice Bellard 已提交
2733 2734

    s->picture_number = picture_number;
2735

2736 2737 2738 2739 2740 2741 2742
    s->block_wrap[0]=
    s->block_wrap[1]=
    s->block_wrap[2]=
    s->block_wrap[3]= s->mb_width*2 + 2;
    s->block_wrap[4]=
    s->block_wrap[5]= s->mb_width + 2;
    
2743
    /* Reset the average MB variance */
M
cleanup  
Michael Niedermayer 已提交
2744 2745
    s->current_picture.mb_var_sum = 0;
    s->current_picture.mc_mb_var_sum = 0;
2746 2747 2748 2749 2750

    /* we need to initialize some time vars before we can encode b-frames */
    if (s->h263_pred && !s->h263_msmpeg4)
        ff_set_mpeg4_time(s, s->picture_number); 

2751
    s->scene_change_score=0;
2752 2753
    
    s->qscale= (int)(s->frame_qscale + 0.5); //FIXME qscale / ... stuff for ME ratedistoration
2754

2755
    /* Estimate motion for every MB */
2756
    if(s->pict_type != I_TYPE){
2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770
        for(mb_y=0; mb_y < s->mb_height; mb_y++) {
            s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
            s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
            s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
            s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
            for(mb_x=0; mb_x < s->mb_width; mb_x++) {
                s->mb_x = mb_x;
                s->mb_y = mb_y;
                s->block_index[0]+=2;
                s->block_index[1]+=2;
                s->block_index[2]+=2;
                s->block_index[3]+=2;

                /* compute motion vector & mb_type and store in context */
2771 2772 2773 2774
                if(s->pict_type==B_TYPE)
                    ff_estimate_b_frame_motion(s, mb_x, mb_y);
                else
                    ff_estimate_p_frame_motion(s, mb_x, mb_y);
2775
//                s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER;
2776 2777
            }
        }
2778
    }else /* if(s->pict_type == I_TYPE) */{
2779 2780 2781
        /* I-Frame */
        //FIXME do we need to zero them?
        memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
2782
        memset(s->p_mv_table   , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);
2783
        memset(s->mb_type      , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
M
Michael Niedermayer 已提交
2784 2785 2786 2787 2788 2789 2790
        
        if(!s->fixed_qscale){
            /* finding spatial complexity for I-frame rate control */
            for(mb_y=0; mb_y < s->mb_height; mb_y++) {
                for(mb_x=0; mb_x < s->mb_width; mb_x++) {
                    int xx = mb_x * 16;
                    int yy = mb_y * 16;
M
cleanup  
Michael Niedermayer 已提交
2791
                    uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
M
Michael Niedermayer 已提交
2792
                    int varc;
2793
		    int sum = s->dsp.pix_sum(pix, s->linesize);
M
Michael Niedermayer 已提交
2794
    
2795
		    varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
M
Michael Niedermayer 已提交
2796

M
cleanup  
Michael Niedermayer 已提交
2797 2798 2799
                    s->current_picture.mb_var [s->mb_width * mb_y + mb_x] = varc;
                    s->current_picture.mb_mean[s->mb_width * mb_y + mb_x] = (sum+128)>>8;
                    s->current_picture.mb_var_sum    += varc;
M
Michael Niedermayer 已提交
2800 2801 2802
                }
            }
        }
2803
    }
2804 2805
    emms_c();

2806
    if(s->scene_change_score > 0 && s->pict_type == P_TYPE){
M
Michael Niedermayer 已提交
2807
        s->pict_type= I_TYPE;
2808
        memset(s->mb_type   , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
M
cleanup  
Michael Niedermayer 已提交
2809
//printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
M
Michael Niedermayer 已提交
2810
    }
M
cleanup  
Michael Niedermayer 已提交
2811

2812 2813 2814 2815 2816 2817
    if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) 
        s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
        ff_fix_long_p_mvs(s);
    if(s->pict_type==B_TYPE){
        s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
        s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
2818 2819 2820 2821 2822

        ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
        ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
        ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
        ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
2823
    }
2824
    
2825
    if (s->fixed_qscale) 
M
cleanup  
Michael Niedermayer 已提交
2826
        s->frame_qscale = s->current_picture.quality;
2827 2828
    else
        s->frame_qscale = ff_rate_estimate_qscale(s);
2829

2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840
    if(s->adaptive_quant){
        switch(s->codec_id){
        case CODEC_ID_MPEG4:
            ff_clean_mpeg4_qscales(s);
            break;
        case CODEC_ID_H263:
        case CODEC_ID_H263P:
            ff_clean_h263_qscales(s);
            break;
        }

M
cleanup  
Michael Niedermayer 已提交
2841
        s->qscale= s->current_picture.qscale_table[0];
2842
    }else
2843 2844
        s->qscale= (int)(s->frame_qscale + 0.5);
        
F
Fabrice Bellard 已提交
2845 2846
    if (s->out_format == FMT_MJPEG) {
        /* for mjpeg, we do include qscale in the matrix */
2847
        s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
2848 2849 2850 2851 2852
        for(i=1;i<64;i++){
            int j= s->idct_permutation[i];

            s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
        }
2853
        convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, 
2854
                       s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias, 8, 8);
F
Fabrice Bellard 已提交
2855
    }
M
cleanup  
Michael Niedermayer 已提交
2856 2857 2858 2859 2860 2861 2862
    
    //FIXME var duplication
    s->current_picture.key_frame= s->pict_type == I_TYPE;
    s->current_picture.pict_type= s->pict_type;

    if(s->current_picture.key_frame)
        s->picture_in_gop_number=0;
F
Fabrice Bellard 已提交
2863

2864
    s->last_bits= get_bit_count(&s->pb);
F
Fabrice Bellard 已提交
2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882
    switch(s->out_format) {
    case FMT_MJPEG:
        mjpeg_picture_header(s);
        break;
    case FMT_H263:
        if (s->h263_msmpeg4) 
            msmpeg4_encode_picture_header(s, picture_number);
        else if (s->h263_pred)
            mpeg4_encode_picture_header(s, picture_number);
        else if (s->h263_rv10) 
            rv10_encode_picture_header(s, picture_number);
        else
            h263_encode_picture_header(s, picture_number);
        break;
    case FMT_MPEG1:
        mpeg1_encode_picture_header(s, picture_number);
        break;
    }
2883 2884 2885 2886 2887 2888 2889 2890
    bits= get_bit_count(&s->pb);
    s->header_bits= bits - s->last_bits;
    s->last_bits= bits;
    s->mv_bits=0;
    s->misc_bits=0;
    s->i_tex_bits=0;
    s->p_tex_bits=0;
    s->i_count=0;
2891 2892
    s->f_count=0;
    s->b_count=0;
2893 2894
    s->skip_count=0;

2895 2896 2897 2898 2899 2900 2901
    for(i=0; i<3; i++){
        /* init last dc values */
        /* note: quant matrix value (8) is implied here */
        s->last_dc[i] = 128;
        
        s->current_picture.error[i] = 0;
    }
F
Fabrice Bellard 已提交
2902 2903 2904 2905
    s->mb_incr = 1;
    s->last_mv[0][0][0] = 0;
    s->last_mv[0][0][1] = 0;

M
Michael Niedermayer 已提交
2906
    if (s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P)
2907
        s->gob_index = ff_h263_get_gob_height(s);
2908

2909
    if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
2910 2911 2912 2913
        ff_mpeg4_init_partitions(s);

    s->resync_mb_x=0;
    s->resync_mb_y=0;
2914
    s->first_slice_line = 1;
M
Michael Niedermayer 已提交
2915 2916
    s->ptr_lastgob = s->pb.buf;
    s->ptr_last_mb_line = s->pb.buf;
2917
    for(mb_y=0; mb_y < s->mb_height; mb_y++) {
M
Michael Niedermayer 已提交
2918 2919
        s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
        s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
2920
        
M
Michael Niedermayer 已提交
2921 2922 2923 2924 2925 2926
        s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
        s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
        s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
        s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
        s->block_index[4]= s->block_wrap[4]*(mb_y + 1)                    + s->block_wrap[0]*(s->mb_height*2 + 2);
        s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
2927
        for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2928 2929
            const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
            const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
2930
//            int d;
2931
            int dmin=10000000;
2932 2933 2934

            s->mb_x = mb_x;
            s->mb_y = mb_y;
M
Michael Niedermayer 已提交
2935 2936 2937 2938 2939 2940
            s->block_index[0]+=2;
            s->block_index[1]+=2;
            s->block_index[2]+=2;
            s->block_index[3]+=2;
            s->block_index[4]++;
            s->block_index[5]++;
M
Michael Niedermayer 已提交
2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952

            /* write gob / video packet header  */
            if(s->rtp_mode){
                int current_packet_size, is_gob_start;
                
                current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
                is_gob_start=0;
                
                if(s->codec_id==CODEC_ID_MPEG4){
                    if(current_packet_size + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size
                       && s->mb_y + s->mb_x>0){

2953
                        if(s->partitioned_frame){
2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964
                            ff_mpeg4_merge_partitions(s);
                            ff_mpeg4_init_partitions(s);
                        }
                        ff_mpeg4_encode_video_packet_header(s);

                        if(s->flags&CODEC_FLAG_PASS1){
                            int bits= get_bit_count(&s->pb);
                            s->misc_bits+= bits - s->last_bits;
                            s->last_bits= bits;
                        }
                        ff_mpeg4_clean_buffers(s);
M
Michael Niedermayer 已提交
2965
                        is_gob_start=1;
2966
                    }
M
Michael Niedermayer 已提交
2967 2968 2969 2970 2971 2972 2973 2974 2975 2976
                }else{
                    if(current_packet_size + s->mb_line_avgsize*s->gob_index >= s->rtp_payload_size
                       && s->mb_x==0 && s->mb_y>0 && s->mb_y%s->gob_index==0){
                       
                        h263_encode_gob_header(s, mb_y);                       
                        is_gob_start=1;
                    }
                }

                if(is_gob_start){
2977 2978 2979 2980 2981
                    s->ptr_lastgob = pbBufPtr(&s->pb);
                    s->first_slice_line=1;
                    s->resync_mb_x=mb_x;
                    s->resync_mb_y=mb_y;
                }
2982
            }
2983

2984 2985 2986
            if(  (s->resync_mb_x   == s->mb_x)
               && s->resync_mb_y+1 == s->mb_y){
                s->first_slice_line=0; 
2987 2988
            }

2989
            if(mb_type & (mb_type-1)){ // more than 1 MB type possible
2990
                int next_block=0;
2991
                int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2992 2993

                copy_context_before_encode(&backup_s, s, -1);
2994 2995
                backup_s.pb= s->pb;
                best_s.data_partitioning= s->data_partitioning;
2996
                best_s.partitioned_frame= s->partitioned_frame;
2997 2998 2999 3000
                if(s->data_partitioning){
                    backup_s.pb2= s->pb2;
                    backup_s.tex_pb= s->tex_pb;
                }
3001

3002
                if(mb_type&MB_TYPE_INTER){
3003
                    s->mv_dir = MV_DIR_FORWARD;
M
Michael Niedermayer 已提交
3004
                    s->mv_type = MV_TYPE_16X16;
3005
                    s->mb_intra= 0;
3006 3007
                    s->mv[0][0][0] = s->p_mv_table[xy][0];
                    s->mv[0][0][1] = s->p_mv_table[xy][1];
3008 3009
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb, 
                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
F
Fabrice Bellard 已提交
3010
                }
3011
                if(mb_type&MB_TYPE_INTER4V){                 
3012
                    s->mv_dir = MV_DIR_FORWARD;
M
Michael Niedermayer 已提交
3013 3014 3015 3016 3017 3018
                    s->mv_type = MV_TYPE_8X8;
                    s->mb_intra= 0;
                    for(i=0; i<4; i++){
                        s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
                        s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
                    }
3019 3020
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb, 
                                 &dmin, &next_block, 0, 0);
3021 3022 3023 3024 3025 3026 3027
                }
                if(mb_type&MB_TYPE_FORWARD){
                    s->mv_dir = MV_DIR_FORWARD;
                    s->mv_type = MV_TYPE_16X16;
                    s->mb_intra= 0;
                    s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
                    s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3028 3029
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb, 
                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3030 3031 3032 3033 3034 3035 3036
                }
                if(mb_type&MB_TYPE_BACKWARD){
                    s->mv_dir = MV_DIR_BACKWARD;
                    s->mv_type = MV_TYPE_16X16;
                    s->mb_intra= 0;
                    s->mv[1][0][0] = s->b_back_mv_table[xy][0];
                    s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3037 3038
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb, 
                                 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
3039 3040 3041 3042 3043 3044 3045 3046 3047
                }
                if(mb_type&MB_TYPE_BIDIR){
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
                    s->mv_type = MV_TYPE_16X16;
                    s->mb_intra= 0;
                    s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3048 3049
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb, 
                                 &dmin, &next_block, 0, 0);
3050 3051 3052 3053 3054 3055 3056 3057 3058
                }
                if(mb_type&MB_TYPE_DIRECT){
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
                    s->mv_type = MV_TYPE_16X16; //FIXME
                    s->mb_intra= 0;
                    s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
                    s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
                    s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
                    s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
3059 3060
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb, 
                                 &dmin, &next_block, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]);
M
Michael Niedermayer 已提交
3061
                }
3062
                if(mb_type&MB_TYPE_INTRA){
3063
                    s->mv_dir = MV_DIR_FORWARD;
M
Michael Niedermayer 已提交
3064
                    s->mv_type = MV_TYPE_16X16;
3065 3066 3067
                    s->mb_intra= 1;
                    s->mv[0][0][0] = 0;
                    s->mv[0][0][1] = 0;
3068 3069
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb, 
                                 &dmin, &next_block, 0, 0);
3070 3071 3072
                    /* force cleaning of ac/dc pred stuff if needed ... */
                    if(s->h263_pred || s->h263_aic)
                        s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
M
Michael Niedermayer 已提交
3073
                }
3074
                copy_context_after_encode(s, &best_s, -1);
3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091
                
                pb_bits_count= get_bit_count(&s->pb);
                flush_put_bits(&s->pb);
                ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
                s->pb= backup_s.pb;
                
                if(s->data_partitioning){
                    pb2_bits_count= get_bit_count(&s->pb2);
                    flush_put_bits(&s->pb2);
                    ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
                    s->pb2= backup_s.pb2;
                    
                    tex_pb_bits_count= get_bit_count(&s->tex_pb);
                    flush_put_bits(&s->tex_pb);
                    ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
                    s->tex_pb= backup_s.tex_pb;
                }
3092
                s->last_bits= get_bit_count(&s->pb);
F
Fabrice Bellard 已提交
3093
            } else {
3094 3095
                int motion_x, motion_y;
                s->mv_type=MV_TYPE_16X16;
3096
                // only one MB-Type possible
3097 3098
                switch(mb_type){
                case MB_TYPE_INTRA:
3099
                    s->mv_dir = MV_DIR_FORWARD;
3100
                    s->mb_intra= 1;
3101 3102
                    motion_x= s->mv[0][0][0] = 0;
                    motion_y= s->mv[0][0][1] = 0;
3103 3104
                    break;
                case MB_TYPE_INTER:
3105 3106 3107 3108
                    s->mv_dir = MV_DIR_FORWARD;
                    s->mb_intra= 0;
                    motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
                    motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
3109
                    break;
3110 3111 3112 3113 3114 3115 3116 3117 3118 3119
                case MB_TYPE_INTER4V:
                    s->mv_dir = MV_DIR_FORWARD;
                    s->mv_type = MV_TYPE_8X8;
                    s->mb_intra= 0;
                    for(i=0; i<4; i++){
                        s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
                        s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
                    }
                    motion_x= motion_y= 0;
                    break;
3120
                case MB_TYPE_DIRECT:
3121 3122
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
                    s->mb_intra= 0;
3123 3124 3125 3126 3127 3128 3129 3130
                    motion_x=s->b_direct_mv_table[xy][0];
                    motion_y=s->b_direct_mv_table[xy][1];
                    s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
                    s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
                    s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
                    s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
                    break;
                case MB_TYPE_BIDIR:
3131
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3132
                    s->mb_intra= 0;
3133 3134 3135 3136 3137 3138
                    motion_x=0;
                    motion_y=0;
                    s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3139 3140
                    break;
                case MB_TYPE_BACKWARD:
3141 3142 3143 3144
                    s->mv_dir = MV_DIR_BACKWARD;
                    s->mb_intra= 0;
                    motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
                    motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3145 3146
                    break;
                case MB_TYPE_FORWARD:
3147 3148 3149 3150 3151
                    s->mv_dir = MV_DIR_FORWARD;
                    s->mb_intra= 0;
                    motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
                    motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
//                    printf(" %d %d ", motion_x, motion_y);
3152 3153
                    break;
                default:
3154 3155
                    motion_x=motion_y=0; //gcc warning fix
                    printf("illegal MB type\n");
3156
                }
3157
                encode_mb(s, motion_x, motion_y);
F
Fabrice Bellard 已提交
3158
            }
3159 3160 3161 3162 3163
            /* clean the MV table in IPS frames for direct mode in B frames */
            if(s->mb_intra /* && I,P,S_TYPE */){
                s->p_mv_table[xy][0]=0;
                s->p_mv_table[xy][1]=0;
            }
F
Fabrice Bellard 已提交
3164

3165
            MPV_decode_mb(s, s->block);
3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189
            
            if(s->flags&CODEC_FLAG_PSNR){
                int w= 16;
                int h= 16;

                if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
                if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
                
                s->current_picture.error[0] += sse(
                    s,
                    s->new_picture    .data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
                    s->current_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
                    w, h, s->linesize);
                s->current_picture.error[1] += sse(
                    s,
                    s->new_picture    .data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,
                    s->current_picture.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,
                    w>>1, h>>1, s->uvlinesize);
                s->current_picture.error[2] += sse(
                    s,
                    s->new_picture    .data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,
                    s->current_picture.data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,
                    w>>1, h>>1, s->uvlinesize);
            }
3190
//printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb));
F
Fabrice Bellard 已提交
3191
        }
3192 3193


M
Michael Niedermayer 已提交
3194
        /* Obtain average mb_row size for RTP */
3195
        if (s->rtp_mode) {
M
Michael Niedermayer 已提交
3196
            if (mb_y==0)
3197
                s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
M
Michael Niedermayer 已提交
3198
            else {    
3199
                s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
3200
            }
M
Michael Niedermayer 已提交
3201
            s->ptr_last_mb_line = pbBufPtr(&s->pb);
3202
        }
F
Fabrice Bellard 已提交
3203
    }
3204
    emms_c();
3205

3206
    if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
3207 3208 3209
        ff_mpeg4_merge_partitions(s);

    if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
M
Michael Niedermayer 已提交
3210 3211
        msmpeg4_encode_ext_header(s);

3212 3213 3214
    if(s->codec_id==CODEC_ID_MPEG4) 
        ff_mpeg4_stuffing(&s->pb);

3215 3216
    //if (s->gob_number)
    //    fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
3217 3218 3219 3220
    
    /* Send the last GOB if RTP */    
    if (s->rtp_mode) {
        flush_put_bits(&s->pb);
3221
        pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
3222 3223 3224
        /* Call the RTP callback to send the last GOB */
        if (s->rtp_callback)
            s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
3225
        s->ptr_lastgob = pbBufPtr(&s->pb);
3226 3227
        //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
    }
F
Fabrice Bellard 已提交
3228 3229
}

M
Michael Niedermayer 已提交
3230
static int dct_quantize_c(MpegEncContext *s, 
F
Fabrice Bellard 已提交
3231
                        DCTELEM *block, int n,
3232
                        int qscale, int *overflow)
F
Fabrice Bellard 已提交
3233 3234 3235
{
    int i, j, level, last_non_zero, q;
    const int *qmat;
M
Michael Niedermayer 已提交
3236
    const UINT8 *scantable= s->intra_scantable.scantable;
3237 3238 3239
    int bias;
    int max=0;
    unsigned int threshold1, threshold2;
3240

3241
    s->fdct (block);
F
Fabrice Bellard 已提交
3242 3243

    if (s->mb_intra) {
3244 3245 3246 3247 3248 3249 3250 3251 3252 3253
        if (!s->h263_aic) {
            if (n < 4)
                q = s->y_dc_scale;
            else
                q = s->c_dc_scale;
            q = q << 3;
        } else
            /* For AIC we skip quant/dequant of INTRADC */
            q = 1 << 3;
            
F
Fabrice Bellard 已提交
3254 3255 3256 3257
        /* note: block[0] is assumed to be positive */
        block[0] = (block[0] + (q >> 1)) / q;
        i = 1;
        last_non_zero = 0;
3258
        qmat = s->q_intra_matrix[qscale];
M
Michael Niedermayer 已提交
3259
        bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
F
Fabrice Bellard 已提交
3260 3261 3262
    } else {
        i = 0;
        last_non_zero = -1;
3263
        qmat = s->q_inter_matrix[qscale];
M
Michael Niedermayer 已提交
3264
        bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
F
Fabrice Bellard 已提交
3265
    }
M
Michael Niedermayer 已提交
3266 3267
    threshold1= (1<<QMAT_SHIFT) - bias - 1;
    threshold2= (threshold1<<1);
F
Fabrice Bellard 已提交
3268 3269

    for(;i<64;i++) {
M
Michael Niedermayer 已提交
3270
        j = scantable[i];
F
Fabrice Bellard 已提交
3271 3272 3273
        level = block[j];
        level = level * qmat[j];

3274 3275 3276 3277
//        if(   bias+level >= (1<<(QMAT_SHIFT - 3))
//           || bias-level >= (1<<(QMAT_SHIFT - 3))){
        if(((unsigned)(level+threshold1))>threshold2){
            if(level>0){
M
Michael Niedermayer 已提交
3278
                level= (bias + level)>>QMAT_SHIFT;
3279 3280
                block[j]= level;
            }else{
M
Michael Niedermayer 已提交
3281
                level= (bias - level)>>QMAT_SHIFT;
3282 3283 3284
                block[j]= -level;
            }
            max |=level;
F
Fabrice Bellard 已提交
3285
            last_non_zero = i;
3286 3287
        }else{
            block[j]=0;
F
Fabrice Bellard 已提交
3288 3289
        }
    }
3290 3291
    *overflow= s->max_qcoeff < max; //overflow might have happend
    
M
Michael Niedermayer 已提交
3292
    /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
3293 3294
    if (s->idct_permutation_type != FF_NO_IDCT_PERM)
	ff_block_permute(block, s->idct_permutation, scantable, last_non_zero);
M
Michael Niedermayer 已提交
3295

F
Fabrice Bellard 已提交
3296 3297 3298
    return last_non_zero;
}

3299 3300
static void dct_unquantize_mpeg1_c(MpegEncContext *s, 
                                   DCTELEM *block, int n, int qscale)
F
Fabrice Bellard 已提交
3301
{
M
Michael Niedermayer 已提交
3302
    int i, level, nCoeffs;
F
Fabrice Bellard 已提交
3303 3304
    const UINT16 *quant_matrix;

3305
    nCoeffs= s->block_last_index[n];
M
Michael Niedermayer 已提交
3306
    
F
Fabrice Bellard 已提交
3307 3308 3309 3310 3311 3312 3313
    if (s->mb_intra) {
        if (n < 4) 
            block[0] = block[0] * s->y_dc_scale;
        else
            block[0] = block[0] * s->c_dc_scale;
        /* XXX: only mpeg1 */
        quant_matrix = s->intra_matrix;
3314 3315
        for(i=1;i<=nCoeffs;i++) {
            int j= s->intra_scantable.permutated[i];
M
Michael Niedermayer 已提交
3316
            level = block[j];
F
Fabrice Bellard 已提交
3317 3318 3319
            if (level) {
                if (level < 0) {
                    level = -level;
M
Michael Niedermayer 已提交
3320
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
F
Fabrice Bellard 已提交
3321 3322 3323
                    level = (level - 1) | 1;
                    level = -level;
                } else {
M
Michael Niedermayer 已提交
3324
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
F
Fabrice Bellard 已提交
3325 3326 3327 3328 3329 3330
                    level = (level - 1) | 1;
                }
#ifdef PARANOID
                if (level < -2048 || level > 2047)
                    fprintf(stderr, "unquant error %d %d\n", i, level);
#endif
M
Michael Niedermayer 已提交
3331
                block[j] = level;
F
Fabrice Bellard 已提交
3332 3333 3334 3335
            }
        }
    } else {
        i = 0;
3336
        quant_matrix = s->inter_matrix;
3337 3338
        for(;i<=nCoeffs;i++) {
            int j= s->intra_scantable.permutated[i];
M
Michael Niedermayer 已提交
3339
            level = block[j];
F
Fabrice Bellard 已提交
3340 3341 3342 3343
            if (level) {
                if (level < 0) {
                    level = -level;
                    level = (((level << 1) + 1) * qscale *
M
Michael Niedermayer 已提交
3344
                             ((int) (quant_matrix[j]))) >> 4;
F
Fabrice Bellard 已提交
3345 3346 3347 3348
                    level = (level - 1) | 1;
                    level = -level;
                } else {
                    level = (((level << 1) + 1) * qscale *
M
Michael Niedermayer 已提交
3349
                             ((int) (quant_matrix[j]))) >> 4;
F
Fabrice Bellard 已提交
3350 3351 3352 3353 3354 3355
                    level = (level - 1) | 1;
                }
#ifdef PARANOID
                if (level < -2048 || level > 2047)
                    fprintf(stderr, "unquant error %d %d\n", i, level);
#endif
M
Michael Niedermayer 已提交
3356
                block[j] = level;
F
Fabrice Bellard 已提交
3357 3358 3359 3360
            }
        }
    }
}
3361

3362 3363 3364 3365 3366 3367
static void dct_unquantize_mpeg2_c(MpegEncContext *s, 
                                   DCTELEM *block, int n, int qscale)
{
    int i, level, nCoeffs;
    const UINT16 *quant_matrix;

3368 3369
    if(s->alternate_scan) nCoeffs= 63;
    else nCoeffs= s->block_last_index[n];
3370 3371 3372 3373 3374 3375 3376
    
    if (s->mb_intra) {
        if (n < 4) 
            block[0] = block[0] * s->y_dc_scale;
        else
            block[0] = block[0] * s->c_dc_scale;
        quant_matrix = s->intra_matrix;
3377 3378
        for(i=1;i<=nCoeffs;i++) {
            int j= s->intra_scantable.permutated[i];
3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397
            level = block[j];
            if (level) {
                if (level < 0) {
                    level = -level;
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
                    level = -level;
                } else {
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
                }
#ifdef PARANOID
                if (level < -2048 || level > 2047)
                    fprintf(stderr, "unquant error %d %d\n", i, level);
#endif
                block[j] = level;
            }
        }
    } else {
        int sum=-1;
        i = 0;
3398
        quant_matrix = s->inter_matrix;
3399 3400
        for(;i<=nCoeffs;i++) {
            int j= s->intra_scantable.permutated[i];
3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424
            level = block[j];
            if (level) {
                if (level < 0) {
                    level = -level;
                    level = (((level << 1) + 1) * qscale *
                             ((int) (quant_matrix[j]))) >> 4;
                    level = -level;
                } else {
                    level = (((level << 1) + 1) * qscale *
                             ((int) (quant_matrix[j]))) >> 4;
                }
#ifdef PARANOID
                if (level < -2048 || level > 2047)
                    fprintf(stderr, "unquant error %d %d\n", i, level);
#endif
                block[j] = level;
                sum+=level;
            }
        }
        block[63]^=sum&1;
    }
}


3425 3426 3427 3428
static void dct_unquantize_h263_c(MpegEncContext *s, 
                                  DCTELEM *block, int n, int qscale)
{
    int i, level, qmul, qadd;
M
Michael Niedermayer 已提交
3429
    int nCoeffs;
3430
    
3431 3432 3433 3434 3435
    assert(s->block_last_index[n]>=0);
    
    qadd = (qscale - 1) | 1;
    qmul = qscale << 1;
    
3436
    if (s->mb_intra) {
3437 3438 3439 3440 3441
        if (!s->h263_aic) {
            if (n < 4) 
                block[0] = block[0] * s->y_dc_scale;
            else
                block[0] = block[0] * s->c_dc_scale;
3442 3443
        }else
            qadd = 0;
3444
        i = 1;
3445
        nCoeffs= 63; //does not allways use zigzag table 
3446 3447
    } else {
        i = 0;
3448
        nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
3449 3450
    }

3451
    for(;i<=nCoeffs;i++) {
3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466
        level = block[i];
        if (level) {
            if (level < 0) {
                level = level * qmul - qadd;
            } else {
                level = level * qmul + qadd;
            }
#ifdef PARANOID
                if (level < -2048 || level > 2047)
                    fprintf(stderr, "unquant error %d %d\n", i, level);
#endif
            block[i] = level;
        }
    }
}
F
Fabrice Bellard 已提交
3467

M
Michael Niedermayer 已提交
3468 3469 3470 3471 3472 3473 3474 3475 3476
char ff_get_pict_type_char(int pict_type){
    switch(pict_type){
    case I_TYPE: return 'I'; 
    case P_TYPE: return 'P'; 
    case B_TYPE: return 'B'; 
    case S_TYPE: return 'S'; 
    }
}

F
Fabrice Bellard 已提交
3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526
AVCodec mpeg1video_encoder = {
    "mpeg1video",
    CODEC_TYPE_VIDEO,
    CODEC_ID_MPEG1VIDEO,
    sizeof(MpegEncContext),
    MPV_encode_init,
    MPV_encode_picture,
    MPV_encode_end,
};

AVCodec h263_encoder = {
    "h263",
    CODEC_TYPE_VIDEO,
    CODEC_ID_H263,
    sizeof(MpegEncContext),
    MPV_encode_init,
    MPV_encode_picture,
    MPV_encode_end,
};

AVCodec h263p_encoder = {
    "h263p",
    CODEC_TYPE_VIDEO,
    CODEC_ID_H263P,
    sizeof(MpegEncContext),
    MPV_encode_init,
    MPV_encode_picture,
    MPV_encode_end,
};

AVCodec rv10_encoder = {
    "rv10",
    CODEC_TYPE_VIDEO,
    CODEC_ID_RV10,
    sizeof(MpegEncContext),
    MPV_encode_init,
    MPV_encode_picture,
    MPV_encode_end,
};

AVCodec mjpeg_encoder = {
    "mjpeg",
    CODEC_TYPE_VIDEO,
    CODEC_ID_MJPEG,
    sizeof(MpegEncContext),
    MPV_encode_init,
    MPV_encode_picture,
    MPV_encode_end,
};

F
Fabrice Bellard 已提交
3527 3528
AVCodec mpeg4_encoder = {
    "mpeg4",
F
Fabrice Bellard 已提交
3529
    CODEC_TYPE_VIDEO,
F
Fabrice Bellard 已提交
3530
    CODEC_ID_MPEG4,
F
Fabrice Bellard 已提交
3531 3532 3533 3534 3535 3536
    sizeof(MpegEncContext),
    MPV_encode_init,
    MPV_encode_picture,
    MPV_encode_end,
};

3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557
AVCodec msmpeg4v1_encoder = {
    "msmpeg4v1",
    CODEC_TYPE_VIDEO,
    CODEC_ID_MSMPEG4V1,
    sizeof(MpegEncContext),
    MPV_encode_init,
    MPV_encode_picture,
    MPV_encode_end,
};

AVCodec msmpeg4v2_encoder = {
    "msmpeg4v2",
    CODEC_TYPE_VIDEO,
    CODEC_ID_MSMPEG4V2,
    sizeof(MpegEncContext),
    MPV_encode_init,
    MPV_encode_picture,
    MPV_encode_end,
};

AVCodec msmpeg4v3_encoder = {
F
Fabrice Bellard 已提交
3558 3559
    "msmpeg4",
    CODEC_TYPE_VIDEO,
3560
    CODEC_ID_MSMPEG4V3,
F
Fabrice Bellard 已提交
3561 3562 3563 3564 3565
    sizeof(MpegEncContext),
    MPV_encode_init,
    MPV_encode_picture,
    MPV_encode_end,
};
M
Michael Niedermayer 已提交
3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585

AVCodec wmv1_encoder = {
    "wmv1",
    CODEC_TYPE_VIDEO,
    CODEC_ID_WMV1,
    sizeof(MpegEncContext),
    MPV_encode_init,
    MPV_encode_picture,
    MPV_encode_end,
};

AVCodec wmv2_encoder = {
    "wmv2",
    CODEC_TYPE_VIDEO,
    CODEC_ID_WMV2,
    sizeof(MpegEncContext),
    MPV_encode_init,
    MPV_encode_picture,
    MPV_encode_end,
};