h263.c 211 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1 2
/*
 * H263/MPEG4 backend for ffmpeg encoder and decoder
Fabrice Bellard's avatar
Fabrice Bellard committed
3
 * Copyright (c) 2000,2001 Fabrice Bellard.
4
 * H263+ support.
Fabrice Bellard's avatar
Fabrice Bellard committed
5
 * Copyright (c) 2001 Juan J. Sierralta P.
6
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
Fabrice Bellard's avatar
Fabrice Bellard committed
7
 *
Fabrice Bellard's avatar
Fabrice Bellard committed
8 9 10 11
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
12
 *
Fabrice Bellard's avatar
Fabrice Bellard committed
13
 * This library is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Fabrice Bellard's avatar
Fabrice Bellard committed
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
17
 *
Fabrice Bellard's avatar
Fabrice Bellard committed
18 19 20
 * 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
21
 *
22 23 24
 * ac prediction encoding, b-frame support, error resilience, optimizations,
 * qpel decoding, gmc decoding, interlaced decoding, 
 * by Michael Niedermayer <michaelni@gmx.at>
Fabrice Bellard's avatar
Fabrice Bellard committed
25
 */
Michael Niedermayer's avatar
Michael Niedermayer committed
26 27 28

/**
 * @file h263.c
Michael Niedermayer's avatar
Michael Niedermayer committed
29
 * h263/mpeg4 codec.
Michael Niedermayer's avatar
Michael Niedermayer committed
30
 */
31 32
 
//#define DEBUG
33 34
#include <limits.h>

Fabrice Bellard's avatar
Fabrice Bellard committed
35 36 37 38 39 40 41
#include "common.h"
#include "dsputil.h"
#include "avcodec.h"
#include "mpegvideo.h"
#include "h263data.h"
#include "mpeg4data.h"

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

45
#define INTRA_MCBPC_VLC_BITS 6
Michael Niedermayer's avatar
Michael Niedermayer committed
46
#define INTER_MCBPC_VLC_BITS 7
47 48 49 50 51 52
#define CBPY_VLC_BITS 6
#define MV_VLC_BITS 9
#define DC_VLC_BITS 9
#define SPRITE_TRAJ_VLC_BITS 6
#define MB_TYPE_B_VLC_BITS 4
#define TEX_VLC_BITS 9
53 54
#define H263_MBTYPE_B_VLC_BITS 6
#define CBPC_B_VLC_BITS 3
55

56
#ifdef CONFIG_ENCODERS
Fabrice Bellard's avatar
Fabrice Bellard committed
57
static void h263_encode_block(MpegEncContext * s, DCTELEM * block,
58
                              int n);
59
static void h263p_encode_umotion(MpegEncContext * s, int val);
60
static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block,
61
                               int n, int dc, uint8_t *scan_table, 
62
                               PutBitContext *dc_pb, PutBitContext *ac_pb);
63 64
#endif

Michael Niedermayer's avatar
Michael Niedermayer committed
65
static int h263_decode_motion(MpegEncContext * s, int pred, int fcode);
66
static int h263p_decode_umotion(MpegEncContext * s, int pred);
Fabrice Bellard's avatar
Fabrice Bellard committed
67 68
static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
                             int n, int coded);
69 70
static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
Michael Niedermayer's avatar
Michael Niedermayer committed
71
                              int n, int coded, int intra, int rvlc);
72 73
static int mpeg4_get_block_length(MpegEncContext * s, DCTELEM * block, int n, int intra_dc, 
                               uint8_t *scan_table);
74
static int h263_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr);
75
#ifdef CONFIG_ENCODERS
76 77
static void mpeg4_encode_visual_object_header(MpegEncContext * s);
static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_number);
78
#endif //CONFIG_ENCODERS
Michael Niedermayer's avatar
Michael Niedermayer committed
79
static void mpeg4_decode_sprite_trajectory(MpegEncContext * s, GetBitContext *gb);
80
static inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, int level, int *dir_ptr, int encoding);
81

82
#ifdef CONFIG_ENCODERS
83 84 85 86
static uint8_t uni_DCtab_lum_len[512];
static uint8_t uni_DCtab_chrom_len[512];
static uint16_t uni_DCtab_lum_bits[512];
static uint16_t uni_DCtab_chrom_bits[512];
87

88
static uint8_t (*mv_penalty)[MAX_MV*2+1]= NULL;
89 90
static uint8_t fcode_tab[MAX_MV*2+1];
static uint8_t umv_fcode_tab[MAX_MV*2+1];
91

Michael Niedermayer's avatar
Michael Niedermayer committed
92 93 94 95
static uint32_t uni_mpeg4_intra_rl_bits[64*64*2*2];
static uint8_t  uni_mpeg4_intra_rl_len [64*64*2*2];
static uint32_t uni_mpeg4_inter_rl_bits[64*64*2*2];
static uint8_t  uni_mpeg4_inter_rl_len [64*64*2*2];
96 97
static uint8_t  uni_h263_intra_aic_rl_len [64*64*2*2];
static uint8_t  uni_h263_inter_rl_len [64*64*2*2];
Michael Niedermayer's avatar
Michael Niedermayer committed
98
//#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))
99
//#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)
Michael Niedermayer's avatar
Michael Niedermayer committed
100
#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
101 102 103 104 105 106 107 108 109 110

/* mpeg4
inter
max level: 24/6
max run: 53/63

intra
max level: 53/16
max run: 29/41
*/
111 112
#endif

113 114 115 116 117
#if 0 //3IV1 is quite rare and tis slows things down a tiny bit
#define IS_3IV1 s->avctx->codec_tag == ff_get_fourcc("3IV1")
#else 
#define IS_3IV1 0
#endif
118

Fabrice Bellard's avatar
Fabrice Bellard committed
119 120 121 122 123
int h263_get_picture_format(int width, int height)
{
    int format;

    if (width == 128 && height == 96)
124
        format = 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
125
    else if (width == 176 && height == 144)
126
        format = 2;
Fabrice Bellard's avatar
Fabrice Bellard committed
127
    else if (width == 352 && height == 288)
128
        format = 3;
Fabrice Bellard's avatar
Fabrice Bellard committed
129
    else if (width == 704 && height == 576)
130
        format = 4;
Fabrice Bellard's avatar
Fabrice Bellard committed
131
    else if (width == 1408 && height == 1152)
132
        format = 5;
Fabrice Bellard's avatar
Fabrice Bellard committed
133 134 135 136 137
    else
        format = 7;
    return format;
}

138 139
#ifdef CONFIG_ENCODERS

Michael Niedermayer's avatar
Michael Niedermayer committed
140
static void aspect_to_info(MpegEncContext * s, AVRational aspect){
Michael Niedermayer's avatar
Michael Niedermayer committed
141 142
    int i;

Michael Niedermayer's avatar
Michael Niedermayer committed
143
    if(aspect.num==0) aspect= (AVRational){1,1};
Michael Niedermayer's avatar
Michael Niedermayer committed
144 145

    for(i=1; i<6; i++){
Michael Niedermayer's avatar
Michael Niedermayer committed
146
        if(av_cmp_q(pixel_aspect[i], aspect) == 0){
Michael Niedermayer's avatar
Michael Niedermayer committed
147 148 149 150
            s->aspect_ratio_info=i;
            return;
        }
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
151
    
Michael Niedermayer's avatar
Michael Niedermayer committed
152
    s->aspect_ratio_info= FF_ASPECT_EXTENDED;
Michael Niedermayer's avatar
Michael Niedermayer committed
153 154
}

Michael Niedermayer's avatar
Michael Niedermayer committed
155
void ff_flv_encode_picture_header(MpegEncContext * s, int picture_number)
Fabrice Bellard's avatar
Fabrice Bellard committed
156
{
Michael Niedermayer's avatar
Michael Niedermayer committed
157
      int format;
Fabrice Bellard's avatar
Fabrice Bellard committed
158

Michael Niedermayer's avatar
Michael Niedermayer committed
159
      align_put_bits(&s->pb);
160

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
      put_bits(&s->pb, 17, 1);
      put_bits(&s->pb, 5, (s->h263_flv-1)); /* 0: h263 escape codes 1: 11-bit escape codes */
      put_bits(&s->pb, 8, (((int64_t)s->picture_number * 30 * s->avctx->frame_rate_base) / 
                           s->avctx->frame_rate) & 0xff); /* TemporalReference */
      if (s->width == 352 && s->height == 288)
        format = 2;
      else if (s->width == 176 && s->height == 144)
        format = 3;
      else if (s->width == 128 && s->height == 96)
        format = 4;
      else if (s->width == 320 && s->height == 240)
        format = 5;
      else if (s->width == 160 && s->height == 120)
        format = 6;
      else if (s->width <= 255 && s->height <= 255)
        format = 0; /* use 1 byte width & height */
      else
        format = 1; /* use 2 bytes width & height */
      put_bits(&s->pb, 3, format); /* PictureSize */
      if (format == 0) {
        put_bits(&s->pb, 8, s->width);
        put_bits(&s->pb, 8, s->height);
      } else if (format == 1) {
        put_bits(&s->pb, 16, s->width);
        put_bits(&s->pb, 16, s->height);
      }
      put_bits(&s->pb, 2, s->pict_type == P_TYPE); /* PictureType */
188
      put_bits(&s->pb, 1, 1); /* DeblockingFlag: on */
189 190 191 192 193
      put_bits(&s->pb, 5, s->qscale); /* Quantizer */
      put_bits(&s->pb, 1, 0); /* ExtraInformation */

      if(s->h263_aic){
        s->y_dc_scale_table= 
Michael Niedermayer's avatar
Michael Niedermayer committed
194
          s->c_dc_scale_table= ff_aic_dc_scale_table;
195 196 197 198
      }else{
        s->y_dc_scale_table=
          s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
      }
Michael Niedermayer's avatar
Michael Niedermayer committed
199 200 201 202
}

void h263_encode_picture_header(MpegEncContext * s, int picture_number)
{
Michael Niedermayer's avatar
Michael Niedermayer committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    int format, coded_frame_rate, coded_frame_rate_base, i, temp_ref;
    int best_clock_code=1;
    int best_divisor=60;
    int best_error= INT_MAX;
   
    if(s->h263_plus){
        for(i=0; i<2; i++){
            int div, error;
            div= (s->avctx->frame_rate_base*1800000LL + 500LL*s->avctx->frame_rate) / ((1000LL+i)*s->avctx->frame_rate);
            div= clip(1, div, 127);
            error= ABS(s->avctx->frame_rate_base*1800000LL - (1000LL+i)*s->avctx->frame_rate*div);
            if(error < best_error){
                best_error= error;
                best_divisor= div;
                best_clock_code= i;
            }
        }
    }
    s->custom_pcf= best_clock_code!=1 || best_divisor!=60;
    coded_frame_rate= 1800000;
    coded_frame_rate_base= (1000+best_clock_code)*best_divisor;
Michael Niedermayer's avatar
Michael Niedermayer committed
224 225

    align_put_bits(&s->pb);
226

227
    /* Update the pointer to last GOB */
228
    s->ptr_lastgob = pbBufPtr(&s->pb);
229
    put_bits(&s->pb, 22, 0x20); /* PSC */
Michael Niedermayer's avatar
Michael Niedermayer committed
230 231 232
    temp_ref= s->picture_number * (int64_t)coded_frame_rate * s->avctx->frame_rate_base / 
                         (coded_frame_rate_base * (int64_t)s->avctx->frame_rate);
    put_bits(&s->pb, 8, temp_ref & 0xff); /* TemporalReference */
Fabrice Bellard's avatar
Fabrice Bellard committed
233 234 235 236 237 238

    put_bits(&s->pb, 1, 1);	/* marker */
    put_bits(&s->pb, 1, 0);	/* h263 id */
    put_bits(&s->pb, 1, 0);	/* split screen off */
    put_bits(&s->pb, 1, 0);	/* camera  off */
    put_bits(&s->pb, 1, 0);	/* freeze picture release off */
239 240
    
    format = h263_get_picture_format(s->width, s->height);
Fabrice Bellard's avatar
Fabrice Bellard committed
241 242 243 244 245 246 247 248 249
    if (!s->h263_plus) {
        /* H.263v1 */
        put_bits(&s->pb, 3, format);
        put_bits(&s->pb, 1, (s->pict_type == P_TYPE));
        /* By now UMV IS DISABLED ON H.263v1, since the restrictions
        of H.263v1 UMV implies to check the predicted MV after
        calculation of the current MB to see if we're on the limits */
        put_bits(&s->pb, 1, 0);	/* unrestricted motion vector: off */
        put_bits(&s->pb, 1, 0);	/* SAC: off */
Michael Niedermayer's avatar
Michael Niedermayer committed
250
        put_bits(&s->pb, 1, s->obmc);	/* advanced prediction mode */
Fabrice Bellard's avatar
Fabrice Bellard committed
251 252 253 254
        put_bits(&s->pb, 1, 0);	/* not PB frame */
        put_bits(&s->pb, 5, s->qscale);
        put_bits(&s->pb, 1, 0);	/* Continuous Presence Multipoint mode: off */
    } else {
Michael Niedermayer's avatar
Michael Niedermayer committed
255
        int ufep=1;
Fabrice Bellard's avatar
Fabrice Bellard committed
256 257
        /* H.263v2 */
        /* H.263 Plus PTYPE */
Michael Niedermayer's avatar
Michael Niedermayer committed
258
        
Fabrice Bellard's avatar
Fabrice Bellard committed
259
        put_bits(&s->pb, 3, 7);
Michael Niedermayer's avatar
Michael Niedermayer committed
260
        put_bits(&s->pb,3,ufep); /* Update Full Extended PTYPE */
261 262 263 264 265
        if (format == 7)
            put_bits(&s->pb,3,6); /* Custom Source Format */
        else
            put_bits(&s->pb, 3, format);
            
Michael Niedermayer's avatar
Michael Niedermayer committed
266
        put_bits(&s->pb,1, s->custom_pcf);
267
        put_bits(&s->pb,1, s->umvplus); /* Unrestricted Motion Vector */
Fabrice Bellard's avatar
Fabrice Bellard committed
268
        put_bits(&s->pb,1,0); /* SAC: off */
Michael Niedermayer's avatar
Michael Niedermayer committed
269
        put_bits(&s->pb,1,s->obmc); /* Advanced Prediction Mode */
270
        put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */
Michael Niedermayer's avatar
Michael Niedermayer committed
271
        put_bits(&s->pb,1,s->loop_filter); /* Deblocking Filter */
272
        put_bits(&s->pb,1,s->h263_slice_structured); /* Slice Structured */
Fabrice Bellard's avatar
Fabrice Bellard committed
273 274
        put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
        put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
275
        put_bits(&s->pb,1,s->alt_inter_vlc); /* Alternative Inter VLC */
Michael Niedermayer's avatar
Michael Niedermayer committed
276
        put_bits(&s->pb,1,s->modified_quant); /* Modified Quantization: */
Fabrice Bellard's avatar
Fabrice Bellard committed
277 278 279 280 281 282 283
        put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
        put_bits(&s->pb,3,0); /* Reserved */
		
        put_bits(&s->pb, 3, s->pict_type == P_TYPE);
		
        put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
        put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
284
        put_bits(&s->pb,1,s->no_rounding); /* Rounding Type */
Fabrice Bellard's avatar
Fabrice Bellard committed
285 286 287 288 289 290
        put_bits(&s->pb,2,0); /* Reserved */
        put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
		
        /* This should be here if PLUSPTYPE */
        put_bits(&s->pb, 1, 0);	/* Continuous Presence Multipoint mode: off */
		
291 292
		if (format == 7) {
            /* Custom Picture Format (CPFMT) */
Michael Niedermayer's avatar
Michael Niedermayer committed
293
            aspect_to_info(s, s->avctx->sample_aspect_ratio);
Michael Niedermayer's avatar
Michael Niedermayer committed
294 295

            put_bits(&s->pb,4,s->aspect_ratio_info);
296 297 298
            put_bits(&s->pb,9,(s->width >> 2) - 1);
            put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
            put_bits(&s->pb,9,(s->height >> 2));
Michael Niedermayer's avatar
Michael Niedermayer committed
299 300 301
            if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
                put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
                put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
302
	    }
303
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
304 305 306 307 308 309 310
        if(s->custom_pcf){
            if(ufep){
                put_bits(&s->pb, 1, best_clock_code);
                put_bits(&s->pb, 7, best_divisor);
            }
            put_bits(&s->pb, 2, (temp_ref>>8)&3);
        }
311
        
Fabrice Bellard's avatar
Fabrice Bellard committed
312
        /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
313
        if (s->umvplus)
314
//            put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
Michael Niedermayer's avatar
Michael Niedermayer committed
315
//FIXME check actual requested range
316
            put_bits(&s->pb,2,1); /* unlimited */
317 318
        if(s->h263_slice_structured)
            put_bits(&s->pb,2,0); /* no weird submodes */
319

Fabrice Bellard's avatar
Fabrice Bellard committed
320 321 322 323
        put_bits(&s->pb, 5, s->qscale);
    }

    put_bits(&s->pb, 1, 0);	/* no PEI */
324

Michael Niedermayer's avatar
Michael Niedermayer committed
325 326 327 328 329 330 331 332 333
    if(s->h263_slice_structured){
        put_bits(&s->pb, 1, 1);
        
        assert(s->mb_x == 0 && s->mb_y == 0);
        ff_h263_encode_mba(s);

        put_bits(&s->pb, 1, 1);
    }

334 335
    if(s->h263_aic){
         s->y_dc_scale_table= 
Michael Niedermayer's avatar
Michael Niedermayer committed
336
         s->c_dc_scale_table= ff_aic_dc_scale_table;
337 338 339 340
    }else{
        s->y_dc_scale_table=
        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
341 342
}

Michael Niedermayer's avatar
Michael Niedermayer committed
343 344 345
/**
 * Encodes a group of blocks header.
 */
346
void h263_encode_gob_header(MpegEncContext * s, int mb_line)
347
{
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
    put_bits(&s->pb, 17, 1); /* GBSC */

    if(s->h263_slice_structured){
        put_bits(&s->pb, 1, 1);

        ff_h263_encode_mba(s);

        if(s->mb_num > 1583)
            put_bits(&s->pb, 1, 1);
        put_bits(&s->pb, 5, s->qscale); /* GQUANT */
        put_bits(&s->pb, 1, 1);
        put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
    }else{
        int gob_number= mb_line / s->gob_index;

        put_bits(&s->pb, 5, gob_number); /* GN */
        put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
        put_bits(&s->pb, 5, s->qscale); /* GQUANT */
    }
367
}
368

Michael Niedermayer's avatar
Michael Niedermayer committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
static inline int get_block_rate(MpegEncContext * s, DCTELEM block[64], int block_last_index, uint8_t scantable[64]){
    int last=0;
    int j;
    int rate=0;

    for(j=1; j<=block_last_index; j++){
        const int index= scantable[j];
        int level= block[index];
        if(level){
            level+= 64;
            if((level&(~127)) == 0){
                if(j<block_last_index) rate+= s->intra_ac_vlc_length     [UNI_AC_ENC_INDEX(j-last-1, level)];
                else                   rate+= s->intra_ac_vlc_last_length[UNI_AC_ENC_INDEX(j-last-1, level)];
            }else
                rate += s->ac_esc_length;
            level-= 64;

            last= j;
        }
    }
    
    return rate;
}

static inline int decide_ac_pred(MpegEncContext * s, DCTELEM block[6][64], int dir[6], uint8_t *st[6], int zigzag_last_index[6])
394
{
Michael Niedermayer's avatar
Michael Niedermayer committed
395
    int score= 0;
396
    int i, n;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
397
    int8_t * const qscale_table= s->current_picture.qscale_table;
398

Michael Niedermayer's avatar
Michael Niedermayer committed
399 400
    memcpy(zigzag_last_index, s->block_last_index, sizeof(int)*6);
    
401
    for(n=0; n<6; n++){
402
        int16_t *ac_val, *ac_val1;
Michael Niedermayer's avatar
Michael Niedermayer committed
403 404
        
        score -= get_block_rate(s, block[n], s->block_last_index[n], s->intra_scantable.permutated);
405

Michael Niedermayer's avatar
Michael Niedermayer committed
406
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
407 408
        ac_val1= ac_val;
        if(dir[n]){
409
            const int xy= s->mb_x + s->mb_y*s->mb_stride - s->mb_stride;
410
            /* top prediction */
Michael Niedermayer's avatar
Michael Niedermayer committed
411
            ac_val-= s->block_wrap[n]*16;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
412
            if(s->mb_y==0 || s->qscale == qscale_table[xy] || n==2 || n==3){
413 414
                /* same qscale */
                for(i=1; i<8; i++){
415
                    const int level= block[n][s->dsp.idct_permutation[i   ]];
Michael Niedermayer's avatar
Michael Niedermayer committed
416
                    block[n][s->dsp.idct_permutation[i   ]] = level - ac_val[i+8];
417
                    ac_val1[i  ]=    block[n][s->dsp.idct_permutation[i<<3]];
418 419 420 421 422
                    ac_val1[i+8]= level;
                }
            }else{
                /* different qscale, we must rescale */
                for(i=1; i<8; i++){
423
                    const int level= block[n][s->dsp.idct_permutation[i   ]];
Michael Niedermayer's avatar
Michael Niedermayer committed
424
                    block[n][s->dsp.idct_permutation[i   ]] = level - ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s->qscale);
425
                    ac_val1[i  ]=    block[n][s->dsp.idct_permutation[i<<3]];
426 427
                    ac_val1[i+8]= level;
                }
428
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
429
            st[n]= s->intra_h_scantable.permutated;
430
        }else{
431
            const int xy= s->mb_x-1 + s->mb_y*s->mb_stride;
432
            /* left prediction */
433
            ac_val-= 16;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
434
            if(s->mb_x==0 || s->qscale == qscale_table[xy] || n==1 || n==3){
435 436
                /* same qscale */
                for(i=1; i<8; i++){
437
                    const int level= block[n][s->dsp.idct_permutation[i<<3]];
Michael Niedermayer's avatar
Michael Niedermayer committed
438
                    block[n][s->dsp.idct_permutation[i<<3]]= level - ac_val[i];
439
                    ac_val1[i  ]= level;
440
                    ac_val1[i+8]=    block[n][s->dsp.idct_permutation[i   ]];
441 442 443 444
                }
            }else{
                /* different qscale, we must rescale */
                for(i=1; i<8; i++){
445
                    const int level= block[n][s->dsp.idct_permutation[i<<3]];
Michael Niedermayer's avatar
Michael Niedermayer committed
446
                    block[n][s->dsp.idct_permutation[i<<3]]= level - ROUNDED_DIV(ac_val[i]*qscale_table[xy], s->qscale);
447
                    ac_val1[i  ]= level;
448
                    ac_val1[i+8]=    block[n][s->dsp.idct_permutation[i   ]];
449
                }
450
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
451
            st[n]= s->intra_v_scantable.permutated;
452
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
453 454 455 456 457 458

        for(i=63; i>0; i--) //FIXME optimize
            if(block[n][ st[n][i] ]) break;
        s->block_last_index[n]= i;

        score += get_block_rate(s, block[n], s->block_last_index[n], st[n]);
459 460
    }

Michael Niedermayer's avatar
Michael Niedermayer committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
    return score < 0;
}

static inline void restore_ac_coeffs(MpegEncContext * s, DCTELEM block[6][64], int dir[6], uint8_t *st[6], int zigzag_last_index[6])
{
    int i, n;
    memcpy(s->block_last_index, zigzag_last_index, sizeof(int)*6);

    for(n=0; n<6; n++){
        int16_t *ac_val = s->ac_val[0][0] + s->block_index[n] * 16;

        st[n]= s->intra_scantable.permutated;
        if(dir[n]){
            /* top prediction */
            for(i=1; i<8; i++){
                block[n][s->dsp.idct_permutation[i   ]] = ac_val[i+8];
            }
        }else{
            /* left prediction */
            for(i=1; i<8; i++){
                block[n][s->dsp.idct_permutation[i<<3]]= ac_val[i  ];
            }
        }
    }
485 486
}

487 488 489
/**
 * modify qscale so that encoding is acually possible in h263 (limit difference to -2..2)
 */
490
void ff_clean_h263_qscales(MpegEncContext *s){
491
    int i;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
492
    int8_t * const qscale_table= s->current_picture.qscale_table;
493 494
    
    for(i=1; i<s->mb_num; i++){
495 496
        if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i-1] ] >2)
            qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i-1] ]+2;
497 498
    }
    for(i=s->mb_num-2; i>=0; i--){
499 500
        if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i+1] ] >2)
            qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i+1] ]+2;
501
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
502 503 504 505 506 507 508 509 510 511 512

    if(s->codec_id != CODEC_ID_H263P){
        for(i=1; i<s->mb_num; i++){
            int mb_xy= s->mb_index2xy[i];
        
            if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_INTER4V)){
                s->mb_type[mb_xy]&= ~CANDIDATE_MB_TYPE_INTER4V;
                s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_INTER;
            }
        }
    }
513 514
}

515 516 517
/**
 * modify mb_type & qscale so that encoding is acually possible in mpeg4
 */
518 519
void ff_clean_mpeg4_qscales(MpegEncContext *s){
    int i;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
520 521
    int8_t * const qscale_table= s->current_picture.qscale_table;

522
    ff_clean_h263_qscales(s);
523 524 525 526 527 528 529
    
    if(s->pict_type== B_TYPE){
        int odd=0;
        /* ok, come on, this isnt funny anymore, theres more code for handling this mpeg4 mess than
           for the actual adaptive quantization */
        
        for(i=0; i<s->mb_num; i++){
530 531
            int mb_xy= s->mb_index2xy[i];
            odd += qscale_table[mb_xy]&1;
532 533 534 535 536 537
        }
        
        if(2*odd > s->mb_num) odd=1;
        else                  odd=0;
        
        for(i=0; i<s->mb_num; i++){
538 539 540 541 542
            int mb_xy= s->mb_index2xy[i];
            if((qscale_table[mb_xy]&1) != odd)
                qscale_table[mb_xy]++;
            if(qscale_table[mb_xy] > 31)
                qscale_table[mb_xy]= 31;
543 544 545
        }            
    
        for(i=1; i<s->mb_num; i++){
546
            int mb_xy= s->mb_index2xy[i];
547 548 549
            if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_DIRECT)){
                s->mb_type[mb_xy]&= ~CANDIDATE_MB_TYPE_DIRECT;
                s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_BIDIR;
550 551 552 553 554
            }
        }
    }
}

555
#endif //CONFIG_ENCODERS
556 557 558 559 560 561
/**
 *
 * @return the mb_type
 */
int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my){
    const int mb_index= s->mb_x + s->mb_y*s->mb_stride;
562
    const int colocated_mb_type= s->next_picture.mb_type[mb_index];
Michael Niedermayer's avatar
Michael Niedermayer committed
563 564 565 566
    int xy= s->block_index[0];
    uint16_t time_pp= s->pp_time;
    uint16_t time_pb= s->pb_time;
    int i;
567
    
Michael Niedermayer's avatar
Michael Niedermayer committed
568
    //FIXME avoid divides
569 570
    
    if(IS_8X8(colocated_mb_type)){
Michael Niedermayer's avatar
Michael Niedermayer committed
571 572 573
        s->mv_type = MV_TYPE_8X8;
        for(i=0; i<4; i++){
            xy= s->block_index[i];
574 575 576 577 578 579
            s->mv[0][i][0] = s->next_picture.motion_val[0][xy][0]*time_pb/time_pp + mx;
            s->mv[0][i][1] = s->next_picture.motion_val[0][xy][1]*time_pb/time_pp + my;
            s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->next_picture.motion_val[0][xy][0]
                                : s->next_picture.motion_val[0][xy][0]*(time_pb - time_pp)/time_pp;
            s->mv[1][i][1] = my ? s->mv[0][i][1] - s->next_picture.motion_val[0][xy][1] 
                                : s->next_picture.motion_val[0][xy][1]*(time_pb - time_pp)/time_pp;
Michael Niedermayer's avatar
Michael Niedermayer committed
580
        }
581 582
        return MB_TYPE_DIRECT2 | MB_TYPE_8x8 | MB_TYPE_L0L1;
    } else if(IS_INTERLACED(colocated_mb_type)){
Michael Niedermayer's avatar
Michael Niedermayer committed
583 584
        s->mv_type = MV_TYPE_FIELD;
        for(i=0; i<2; i++){
585
            int field_select= s->next_picture.ref_index[0][s->block_index[2*i]];
Michael Niedermayer's avatar
Michael Niedermayer committed
586
            if(s->top_field_first){
587 588
                time_pp= s->pp_field_time - field_select + i;
                time_pb= s->pb_field_time - field_select + i;
Michael Niedermayer's avatar
Michael Niedermayer committed
589
            }else{
590 591
                time_pp= s->pp_field_time + field_select - i;
                time_pb= s->pb_field_time + field_select - i;
Michael Niedermayer's avatar
Michael Niedermayer committed
592
            }
593 594 595 596 597 598
            s->mv[0][i][0] = s->p_field_mv_table[i][0][mb_index][0]*time_pb/time_pp + mx;
            s->mv[0][i][1] = s->p_field_mv_table[i][0][mb_index][1]*time_pb/time_pp + my;
            s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->p_field_mv_table[i][0][mb_index][0]
                                : s->p_field_mv_table[i][0][mb_index][0]*(time_pb - time_pp)/time_pp;
            s->mv[1][i][1] = my ? s->mv[0][i][1] - s->p_field_mv_table[i][0][mb_index][1] 
                                : s->p_field_mv_table[i][0][mb_index][1]*(time_pb - time_pp)/time_pp;
Michael Niedermayer's avatar
Michael Niedermayer committed
599
        }
600 601
        return MB_TYPE_DIRECT2 | MB_TYPE_16x8 | MB_TYPE_L0L1 | MB_TYPE_INTERLACED;
    }else{
602 603 604 605 606 607
        s->mv[0][0][0] = s->mv[0][1][0] = s->mv[0][2][0] = s->mv[0][3][0] = s->next_picture.motion_val[0][xy][0]*time_pb/time_pp + mx;
        s->mv[0][0][1] = s->mv[0][1][1] = s->mv[0][2][1] = s->mv[0][3][1] = s->next_picture.motion_val[0][xy][1]*time_pb/time_pp + my;
        s->mv[1][0][0] = s->mv[1][1][0] = s->mv[1][2][0] = s->mv[1][3][0] = mx ? s->mv[0][0][0] - s->next_picture.motion_val[0][xy][0]
                            : s->next_picture.motion_val[0][xy][0]*(time_pb - time_pp)/time_pp;
        s->mv[1][0][1] = s->mv[1][1][1] = s->mv[1][2][1] = s->mv[1][3][1] = my ? s->mv[0][0][1] - s->next_picture.motion_val[0][xy][1] 
                            : s->next_picture.motion_val[0][xy][1]*(time_pb - time_pp)/time_pp;
608 609 610 611 612
        if((s->avctx->workaround_bugs & FF_BUG_DIRECT_BLOCKSIZE) || !s->quarter_sample)
            s->mv_type= MV_TYPE_16X16;
        else
            s->mv_type= MV_TYPE_8X8;
        return MB_TYPE_DIRECT2 | MB_TYPE_16x16 | MB_TYPE_L0L1; //Note see prev line
Michael Niedermayer's avatar
Michael Niedermayer committed
613 614 615
    }
}

616 617 618
void ff_h263_update_motion_val(MpegEncContext * s){
    const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
               //FIXME a lot of thet is only needed for !low_delay
619
    const int wrap = s->b8_stride;
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
    const int xy = s->block_index[0];
    
    s->current_picture.mbskip_table[mb_xy]= s->mb_skiped; 

    if(s->mv_type != MV_TYPE_8X8){
        int motion_x, motion_y;
        if (s->mb_intra) {
            motion_x = 0;
            motion_y = 0;
        } else if (s->mv_type == MV_TYPE_16X16) {
            motion_x = s->mv[0][0][0];
            motion_y = s->mv[0][0][1];
        } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
            int i;
            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);
            for(i=0; i<2; i++){
638 639
                s->p_field_mv_table[i][0][mb_xy][0]= s->mv[0][i][0];
                s->p_field_mv_table[i][0][mb_xy][1]= s->mv[0][i][1];
640
            }
641 642 643 644
            s->current_picture.ref_index[0][xy           ]=
            s->current_picture.ref_index[0][xy        + 1]= s->field_select[0][0];
            s->current_picture.ref_index[0][xy + wrap    ]=
            s->current_picture.ref_index[0][xy + wrap + 1]= s->field_select[0][1];
645
        }
646

647
        /* no update if 8X8 because it has been done during parsing */
648 649 650 651 652 653 654 655
        s->current_picture.motion_val[0][xy][0] = motion_x;
        s->current_picture.motion_val[0][xy][1] = motion_y;
        s->current_picture.motion_val[0][xy + 1][0] = motion_x;
        s->current_picture.motion_val[0][xy + 1][1] = motion_y;
        s->current_picture.motion_val[0][xy + wrap][0] = motion_x;
        s->current_picture.motion_val[0][xy + wrap][1] = motion_y;
        s->current_picture.motion_val[0][xy + 1 + wrap][0] = motion_x;
        s->current_picture.motion_val[0][xy + 1 + wrap][1] = motion_y;
656 657 658 659 660
    }

    if(s->encoding){ //FIXME encoding MUST be cleaned up
        if (s->mv_type == MV_TYPE_8X8) 
            s->current_picture.mb_type[mb_xy]= MB_TYPE_L0 | MB_TYPE_8x8;
Michael Niedermayer's avatar
Michael Niedermayer committed
661 662
        else if(s->mb_intra)
            s->current_picture.mb_type[mb_xy]= MB_TYPE_INTRA;
663 664 665 666 667
        else
            s->current_picture.mb_type[mb_xy]= MB_TYPE_L0 | MB_TYPE_16x16;
    }
}

668
#ifdef CONFIG_ENCODERS
669

670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
static inline int h263_get_motion_length(MpegEncContext * s, int val, int f_code){
    int l, bit_size, code;

    if (val == 0) {
        return mvtab[0][1];
    } else {
        bit_size = f_code - 1;
        /* modulo encoding */
        l= INT_BIT - 6 - bit_size;
        val = (val<<l)>>l;
        val--;
        code = (val >> bit_size) + 1;

        return mvtab[code][1] + 1 + bit_size;
    }
}

static inline void ff_h263_encode_motion_vector(MpegEncContext * s, int x, int y, int f_code){
    if(s->flags2 & CODEC_FLAG2_NO_OUTPUT){
        skip_put_bits(&s->pb, 
            h263_get_motion_length(s, x, f_code)
           +h263_get_motion_length(s, y, f_code));
    }else{
        ff_h263_encode_motion(s, x, f_code);
        ff_h263_encode_motion(s, y, f_code);
    }
}

698 699 700 701 702 703 704 705
static inline int get_p_cbp(MpegEncContext * s,
                      DCTELEM block[6][64],
                      int motion_x, int motion_y){
    int cbp, i;

    if(s->flags & CODEC_FLAG_CBP_RD){
        int best_cbpy_score= INT_MAX;
        int best_cbpc_score= INT_MAX;
706
        int cbpc = (-1), cbpy= (-1);
707
        const int offset= (s->mv_type==MV_TYPE_16X16 ? 0 : 16) + (s->dquant ? 8 : 0);
708
        const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760

        for(i=0; i<4; i++){
            int score= inter_MCBPC_bits[i + offset] * lambda;
            if(i&1) score += s->coded_score[5];
            if(i&2) score += s->coded_score[4];

            if(score < best_cbpc_score){
                best_cbpc_score= score;
                cbpc= i;
            }
        }

        for(i=0; i<16; i++){
            int score= cbpy_tab[i ^ 0xF][1] * lambda;
            if(i&1) score += s->coded_score[3];
            if(i&2) score += s->coded_score[2];
            if(i&4) score += s->coded_score[1];
            if(i&8) score += s->coded_score[0];

            if(score < best_cbpy_score){
                best_cbpy_score= score;
                cbpy= i;
            }
        }
        cbp= cbpc + 4*cbpy;
        if ((motion_x | motion_y | s->dquant) == 0 && s->mv_type==MV_TYPE_16X16){
            if(best_cbpy_score + best_cbpc_score + 2*lambda >= 0)
                cbp= 0;
        }

        for (i = 0; i < 6; i++) {
            if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i))&1)==0 ){
                s->block_last_index[i]= -1;
                memset(s->block[i], 0, sizeof(DCTELEM)*64);
            }
        }
    }else{
        cbp= 0;
        for (i = 0; i < 6; i++) {
            if (s->block_last_index[i] >= 0)
                cbp |= 1 << (5 - i);
        }
    }
    return cbp;
}

static inline int get_b_cbp(MpegEncContext * s, DCTELEM block[6][64],
                            int motion_x, int motion_y, int mb_type){
    int cbp=0, i;

    if(s->flags & CODEC_FLAG_CBP_RD){
        int score=0;
761
        const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
        
        for(i=0; i<6; i++){
            if(s->coded_score[i] < 0){
                score += s->coded_score[i];
                cbp |= 1 << (5 - i);
            }
        }
        
        if(cbp){
            int zero_score= -6;
            if ((motion_x | motion_y | s->dquant | mb_type) == 0){
                zero_score-= 4; //2*MV + mb_type + cbp bit
            }

            zero_score*= lambda;
            if(zero_score <= score){
                cbp=0;
            }
        }

        for (i = 0; i < 6; i++) {
            if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i))&1)==0 ){
                s->block_last_index[i]= -1;
                memset(s->block[i], 0, sizeof(DCTELEM)*64);
            }
        }
    }else{
        for (i = 0; i < 6; i++) {
            if (s->block_last_index[i] >= 0)
                cbp |= 1 << (5 - i);
        }
    }
    return cbp;
}

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 822 823 824 825
static inline void mpeg4_encode_blocks(MpegEncContext * s, DCTELEM block[6][64], int intra_dc[6], 
                               uint8_t **scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb){
    int i;
    
    if(scan_table){
        if(s->flags2 & CODEC_FLAG2_NO_OUTPUT){
            for (i = 0; i < 6; i++) {
                skip_put_bits(&s->pb, mpeg4_get_block_length(s, block[i], i, intra_dc[i], scan_table[i]));
            }
        }else{
            /* encode each block */
            for (i = 0; i < 6; i++) {
                mpeg4_encode_block(s, block[i], i, intra_dc[i], scan_table[i], dc_pb, ac_pb);
            }
        }
    }else{
        if(s->flags2 & CODEC_FLAG2_NO_OUTPUT){
            for (i = 0; i < 6; i++) {
                skip_put_bits(&s->pb, mpeg4_get_block_length(s, block[i], i, 0, s->intra_scantable.permutated));
            }
        }else{
            /* encode each block */
            for (i = 0; i < 6; i++) {
                mpeg4_encode_block(s, block[i], i, 0, s->intra_scantable.permutated, dc_pb, ac_pb);
            }
        }
    }
}

826 827 828 829
void mpeg4_encode_mb(MpegEncContext * s,
		    DCTELEM block[6][64],
		    int motion_x, int motion_y)
{
830
    int cbpc, cbpy, pred_x, pred_y;
831 832 833 834
    PutBitContext * const pb2    = s->data_partitioning                         ? &s->pb2    : &s->pb;
    PutBitContext * const tex_pb = s->data_partitioning && s->pict_type!=B_TYPE ? &s->tex_pb : &s->pb;
    PutBitContext * const dc_pb  = s->data_partitioning && s->pict_type!=I_TYPE ? &s->pb2    : &s->pb;
    const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1) && !s->data_partitioning ? 1 : 0;
835
    const int dquant_code[5]= {1,0,9,2,3};
836
    
837 838
    //    printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
    if (!s->mb_intra) {
839 840
        int i, cbp;
        
841 842 843
        if(s->pict_type==B_TYPE){
            static const int mb_type_table[8]= {-1, 2, 3, 1,-1,-1,-1, 0}; /* convert from mv_dir to type */
            int mb_type=  mb_type_table[s->mv_dir];
844

845
            if(s->mb_x==0){
846 847 848 849 850 851
                for(i=0; i<2; i++){
                    s->last_mv[i][0][0]= 
                    s->last_mv[i][0][1]= 
                    s->last_mv[i][1][0]= 
                    s->last_mv[i][1][1]= 0;
                }
852
            }
853 854 855 856
            
            assert(s->dquant>=-2 && s->dquant<=2);
            assert((s->dquant&1)==0);
            assert(mb_type>=0);
857 858

            /* nothing to do if this MB was skiped in the next P Frame */
859
            if(s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]){ //FIXME avoid DCT & ...
860 861 862 863 864
                s->skip_count++;
                s->mv[0][0][0]= 
                s->mv[0][0][1]= 
                s->mv[1][0][0]= 
                s->mv[1][0][1]= 0;
865
                s->mv_dir= MV_DIR_FORWARD; //doesnt matter
866
                s->qscale -= s->dquant;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
867 868
//                s->mb_skiped=1;

869 870
                return;
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
871
            
872 873
            cbp= get_b_cbp(s, block, motion_x, motion_y, mb_type);
            
874 875
            if ((cbp | motion_x | motion_y | mb_type) ==0) {
                /* direct MB with MV={0,0} */
876 877
                assert(s->dquant==0);
                
878
                put_bits(&s->pb, 1, 1); /* mb not coded modb1=1 */
879 880 881 882 883

                if(interleaved_stats){
                    s->misc_bits++;
                    s->last_bits++;
                }
884 885 886
                s->skip_count++;
                return;
            }
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
887
            
888 889 890 891 892
            put_bits(&s->pb, 1, 0);	/* mb coded modb1=0 */
            put_bits(&s->pb, 1, cbp ? 0 : 1); /* modb2 */ //FIXME merge
            put_bits(&s->pb, mb_type+1, 1); // this table is so simple that we dont need it :)
            if(cbp) put_bits(&s->pb, 6, cbp);
            
893 894 895 896 897 898 899
            if(cbp && mb_type){
                if(s->dquant)
                    put_bits(&s->pb, 2, (s->dquant>>2)+3);
                else
                    put_bits(&s->pb, 1, 0);
            }else
                s->qscale -= s->dquant;
900 901 902 903 904
            
            if(!s->progressive_sequence){
                if(cbp)
                    put_bits(&s->pb, 1, s->interlaced_dct);
                if(mb_type) // not diect mode
905
                    put_bits(&s->pb, 1, s->mv_type == MV_TYPE_FIELD);
906
            }
907

908
            if(interleaved_stats){
909
                s->misc_bits+= get_bits_diff(s);
910
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
911

912 913
            if(mb_type == 0){
                assert(s->mv_dir & MV_DIRE