msmpeg4.c 60 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1 2
/*
 * MSMPEG4 backend for ffmpeg encoder and decoder
Fabrice Bellard's avatar
Fabrice Bellard committed
3
 * Copyright (c) 2001 Fabrice Bellard.
4
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
Fabrice Bellard's avatar
Fabrice Bellard committed
5
 *
6 7 8
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
Fabrice Bellard's avatar
Fabrice Bellard committed
9 10
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
12
 *
13
 * FFmpeg 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
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 *
Michael Niedermayer's avatar
Michael Niedermayer committed
22
 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
Fabrice Bellard's avatar
Fabrice Bellard committed
23
 */
Michael Niedermayer's avatar
Michael Niedermayer committed
24 25 26 27 28 29

/**
 * @file msmpeg4.c
 * MSMPEG4 backend for ffmpeg encoder and decoder.
 */

30
#include "avcodec.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
31 32
#include "dsputil.h"
#include "mpegvideo.h"
33

Fabrice Bellard's avatar
Fabrice Bellard committed
34
/*
35
 * You can also call this codec : MPEG4 with a twist !
Fabrice Bellard's avatar
Fabrice Bellard committed
36
 *
37
 * TODO:
Fabrice Bellard's avatar
Fabrice Bellard committed
38
 *        - (encoding) select best mv table (two choices)
39
 *        - (encoding) select best vlc/dc table
Fabrice Bellard's avatar
Fabrice Bellard committed
40 41 42
 */
//#define DEBUG

Michael Niedermayer's avatar
Michael Niedermayer committed
43 44 45 46 47 48 49 50 51 52 53 54 55
#define DC_VLC_BITS 9
#define CBPY_VLC_BITS 6
#define INTER_INTRA_VLC_BITS 3
#define V1_INTRA_CBPC_VLC_BITS 6
#define V1_INTER_CBPC_VLC_BITS 6
#define V2_INTRA_CBPC_VLC_BITS 3
#define V2_MB_TYPE_VLC_BITS 7
#define MV_VLC_BITS 9
#define V2_MV_VLC_BITS 9
#define TEX_VLC_BITS 9
#define MB_NON_INTRA_VLC_BITS 9
#define MB_INTRA_VLC_BITS 9

56 57 58
#define II_BITRATE 128*1024
#define MBAC_BITRATE 50*1024

Michael Niedermayer's avatar
Michael Niedermayer committed
59 60
#define DEFAULT_INTER_INDEX 3

61 62
static uint32_t v2_dc_lum_table[512][2];
static uint32_t v2_dc_chroma_table[512][2];
63

Michael Niedermayer's avatar
Michael Niedermayer committed
64 65
static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
Michael Niedermayer's avatar
Michael Niedermayer committed
66
                                       int n, int coded, const uint8_t *scantable);
Fabrice Bellard's avatar
Fabrice Bellard committed
67
static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
68
static int msmpeg4_decode_motion(MpegEncContext * s,
Fabrice Bellard's avatar
Fabrice Bellard committed
69
                                 int *mx_ptr, int *my_ptr);
Falk Hüffner's avatar
Falk Hüffner committed
70
static void init_h263_dc_for_msmpeg4(void);
71
static inline void msmpeg4_memsetw(short *tab, int val, int n);
72
#ifdef CONFIG_ENCODERS
73
static void msmpeg4v2_encode_motion(MpegEncContext * s, int val);
74
static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra);
75
#endif //CONFIG_ENCODERS
76 77
static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
Michael Niedermayer's avatar
Michael Niedermayer committed
78
static int wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
Fabrice Bellard's avatar
Fabrice Bellard committed
79

80
/* vc1 externs */
anonymous's avatar
anonymous committed
81
extern uint8_t wmv3_dc_scale_table[32];
82

Fabrice Bellard's avatar
Fabrice Bellard committed
83 84 85 86 87 88
#ifdef DEBUG
int intra_count = 0;
int frame_count = 0;
#endif

#include "msmpeg4data.h"
89
#include "msmpeg4tab.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
90

Diego Biurrun's avatar
Diego Biurrun committed
91
#ifdef CONFIG_ENCODERS //strangely gcc includes this even if it is not references
92
static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2];
Michael Niedermayer's avatar
Michael Niedermayer committed
93
#endif //CONFIG_ENCODERS
94

95 96
static uint8_t static_rl_table_store[NB_RL_TABLES][2][2*MAX_RUN + MAX_LEVEL + 3];

Michael Niedermayer's avatar
Michael Niedermayer committed
97 98 99
static void common_init(MpegEncContext * s)
{
    static int inited=0;
100

Michael Niedermayer's avatar
Michael Niedermayer committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    switch(s->msmpeg4_version){
    case 1:
    case 2:
        s->y_dc_scale_table=
        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
        break;
    case 3:
        if(s->workaround_bugs){
            s->y_dc_scale_table= old_ff_y_dc_scale_table;
            s->c_dc_scale_table= old_ff_c_dc_scale_table;
        } else{
            s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
            s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
        }
        break;
    case 4:
Michael Niedermayer's avatar
Michael Niedermayer committed
117
    case 5:
Michael Niedermayer's avatar
Michael Niedermayer committed
118 119 120
        s->y_dc_scale_table= wmv1_y_dc_scale_table;
        s->c_dc_scale_table= wmv1_c_dc_scale_table;
        break;
121
#if defined(CONFIG_WMV3_DECODER)||defined(CONFIG_VC1_DECODER)
anonymous's avatar
anonymous committed
122 123 124 125
    case 6:
        s->y_dc_scale_table= wmv3_dc_scale_table;
        s->c_dc_scale_table= wmv3_dc_scale_table;
        break;
126
#endif
anonymous's avatar
anonymous committed
127

Michael Niedermayer's avatar
Michael Niedermayer committed
128 129
    }

130

Michael Niedermayer's avatar
Michael Niedermayer committed
131
    if(s->msmpeg4_version>=4){
Michael Niedermayer's avatar
Michael Niedermayer committed
132 133 134 135
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , wmv1_scantable[1]);
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, wmv1_scantable[2]);
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, wmv1_scantable[3]);
        ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , wmv1_scantable[0]);
Michael Niedermayer's avatar
Michael Niedermayer committed
136
    }
137
    //Note the default tables are set in common_init in mpegvideo.c
138

Michael Niedermayer's avatar
Michael Niedermayer committed
139 140 141 142 143 144 145
    if(!inited){
        inited=1;

        init_h263_dc_for_msmpeg4();
    }
}

146 147
#ifdef CONFIG_ENCODERS

Fabrice Bellard's avatar
Fabrice Bellard committed
148 149 150 151 152
/* build the table which associate a (x,y) motion vector to a vlc */
static void init_mv_table(MVTable *tab)
{
    int i, x, y;

153
    tab->table_mv_index = av_malloc(sizeof(uint16_t) * 4096);
Fabrice Bellard's avatar
Fabrice Bellard committed
154 155 156
    /* mark all entries as not used */
    for(i=0;i<4096;i++)
        tab->table_mv_index[i] = tab->n;
157

Fabrice Bellard's avatar
Fabrice Bellard committed
158 159 160 161 162 163 164
    for(i=0;i<tab->n;i++) {
        x = tab->table_mvx[i];
        y = tab->table_mvy[i];
        tab->table_mv_index[(x << 6) | y] = i;
    }
}

165
void ff_code012(PutBitContext *pb, int n)
Fabrice Bellard's avatar
Fabrice Bellard committed
166 167 168 169 170 171 172 173 174
{
    if (n == 0) {
        put_bits(pb, 1, 0);
    } else {
        put_bits(pb, 1, 1);
        put_bits(pb, 1, (n >= 2));
    }
}

Michael Niedermayer's avatar
Michael Niedermayer committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
void ff_msmpeg4_encode_init(MpegEncContext *s)
{
    static int init_done=0;
    int i;

    common_init(s);
    if(s->msmpeg4_version>=4){
        s->min_qcoeff= -255;
        s->max_qcoeff=  255;
    }

    if (!init_done) {
        /* init various encoding tables */
        init_done = 1;
        init_mv_table(&mv_tables[0]);
        init_mv_table(&mv_tables[1]);
        for(i=0;i<NB_RL_TABLES;i++)
192
            init_rl(&rl_table[i], static_rl_table_store[i]);
193 194 195 196 197 198 199 200

        for(i=0; i<NB_RL_TABLES; i++){
            int level;
            for(level=0; level<=MAX_LEVEL; level++){
                int run;
                for(run=0; run<=MAX_RUN; run++){
                    int last;
                    for(last=0; last<2; last++){
201
                        rl_length[i][level][run][last]= get_size_of_code(s, &rl_table[  i], last, run, level, 0);
202 203 204 205
                    }
                }
            }
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
206 207 208 209 210 211 212
    }
}

static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra){
    int size=0;
    int code;
    int run_diff= intra ? 0 : 1;
213

Michael Niedermayer's avatar
Michael Niedermayer committed
214 215 216 217 218 219
    code = get_rl_index(rl, last, run, level);
    size+= rl->table_vlc[code][1];
    if (code == rl->n) {
        int level1, run1;

        level1 = level - rl->max_level[last][run];
220
        if (level1 < 1)
Michael Niedermayer's avatar
Michael Niedermayer committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
            goto esc2;
        code = get_rl_index(rl, last, run, level1);
        if (code == rl->n) {
            esc2:
            size++;
            if (level > MAX_LEVEL)
                goto esc3;
            run1 = run - rl->max_run[last][level] - run_diff;
            if (run1 < 0)
                goto esc3;
            code = get_rl_index(rl, last, run1, level);
            if (code == rl->n) {
            esc3:
                /* third escape */
                size+=1+1+6+8;
            } else {
                /* second escape */
                size+= 1+1+ rl->table_vlc[code][1];
            }
        } else {
            /* first escape */
            size+= 1+1+ rl->table_vlc[code][1];
        }
    } else {
        size++;
    }
    return size;
}

static void find_best_tables(MpegEncContext * s)
{
    int i;
    int best       =-1, best_size       =9999999;
    int chroma_best=-1, best_chroma_size=9999999;
255

Michael Niedermayer's avatar
Michael Niedermayer committed
256 257 258 259 260 261
    for(i=0; i<3; i++){
        int level;
        int chroma_size=0;
        int size=0;

        if(i>0){// ;)
262
            size++;
Michael Niedermayer's avatar
Michael Niedermayer committed
263 264 265 266 267 268
            chroma_size++;
        }
        for(level=0; level<=MAX_LEVEL; level++){
            int run;
            for(run=0; run<=MAX_RUN; run++){
                int last;
269
                const int last_size= size + chroma_size;
Michael Niedermayer's avatar
Michael Niedermayer committed
270 271 272 273
                for(last=0; last<2; last++){
                    int inter_count       = s->ac_stats[0][0][level][run][last] + s->ac_stats[0][1][level][run][last];
                    int intra_luma_count  = s->ac_stats[1][0][level][run][last];
                    int intra_chroma_count= s->ac_stats[1][1][level][run][last];
274

Michael Niedermayer's avatar
Michael Niedermayer committed
275
                    if(s->pict_type==I_TYPE){
276 277
                        size       += intra_luma_count  *rl_length[i  ][level][run][last];
                        chroma_size+= intra_chroma_count*rl_length[i+3][level][run][last];
Michael Niedermayer's avatar
Michael Niedermayer committed
278
                    }else{
279 280 281
                        size+=        intra_luma_count  *rl_length[i  ][level][run][last]
                                     +intra_chroma_count*rl_length[i+3][level][run][last]
                                     +inter_count       *rl_length[i+3][level][run][last];
282
                    }
Michael Niedermayer's avatar
Michael Niedermayer committed
283
                }
284
                if(last_size == size+chroma_size) break;
Michael Niedermayer's avatar
Michael Niedermayer committed
285 286 287 288 289 290 291 292 293 294 295
            }
        }
        if(size<best_size){
            best_size= size;
            best= i;
        }
        if(chroma_size<best_chroma_size){
            best_chroma_size= chroma_size;
            chroma_best= i;
        }
    }
296

297
//    printf("type:%d, best:%d, qp:%d, var:%d, mcvar:%d, size:%d //\n",
Michael Niedermayer's avatar
Michael Niedermayer committed
298
//           s->pict_type, best, s->qscale, s->mb_var_sum, s->mc_mb_var_sum, best_size);
299

Michael Niedermayer's avatar
Michael Niedermayer committed
300 301 302 303 304 305
    if(s->pict_type==P_TYPE) chroma_best= best;

    memset(s->ac_stats, 0, sizeof(int)*(MAX_LEVEL+1)*(MAX_RUN+1)*2*2*2);

    s->rl_table_index       =        best;
    s->rl_chroma_table_index= chroma_best;
306

Michael Niedermayer's avatar
Michael Niedermayer committed
307 308 309 310 311 312 313 314 315 316
    if(s->pict_type != s->last_non_b_pict_type){
        s->rl_table_index= 2;
        if(s->pict_type==I_TYPE)
            s->rl_chroma_table_index= 1;
        else
            s->rl_chroma_table_index= 2;
    }

}

Michael Niedermayer's avatar
Michael Niedermayer committed
317
/* write MSMPEG4 compatible frame header */
Fabrice Bellard's avatar
Fabrice Bellard committed
318 319
void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
{
Michael Niedermayer's avatar
Michael Niedermayer committed
320
    find_best_tables(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
321 322 323 324 325

    align_put_bits(&s->pb);
    put_bits(&s->pb, 2, s->pict_type - 1);

    put_bits(&s->pb, 5, s->qscale);
Michael Niedermayer's avatar
Michael Niedermayer committed
326 327 328 329
    if(s->msmpeg4_version<=2){
        s->rl_table_index = 2;
        s->rl_chroma_table_index = 2;
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
330

Fabrice Bellard's avatar
Fabrice Bellard committed
331 332 333
    s->dc_table_index = 1;
    s->mv_table_index = 1; /* only if P frame */
    s->use_skip_mb_code = 1; /* only if P frame */
Michael Niedermayer's avatar
Michael Niedermayer committed
334
    s->per_mb_rl_table = 0;
335 336
    if(s->msmpeg4_version==4)
        s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==P_TYPE);
Michael Niedermayer's avatar
Michael Niedermayer committed
337
//printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height);
Michael Niedermayer's avatar
Michael Niedermayer committed
338

Fabrice Bellard's avatar
Fabrice Bellard committed
339
    if (s->pict_type == I_TYPE) {
340 341
        s->slice_height= s->mb_height/1;
        put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height);
342

Michael Niedermayer's avatar
Michael Niedermayer committed
343 344
        if(s->msmpeg4_version==4){
            msmpeg4_encode_ext_header(s);
345
            if(s->bit_rate>MBAC_BITRATE)
346
                put_bits(&s->pb, 1, s->per_mb_rl_table);
Michael Niedermayer's avatar
Michael Niedermayer committed
347
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
348

Michael Niedermayer's avatar
Michael Niedermayer committed
349
        if(s->msmpeg4_version>2){
Michael Niedermayer's avatar
Michael Niedermayer committed
350
            if(!s->per_mb_rl_table){
351 352
                ff_code012(&s->pb, s->rl_chroma_table_index);
                ff_code012(&s->pb, s->rl_table_index);
Michael Niedermayer's avatar
Michael Niedermayer committed
353
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
354

Michael Niedermayer's avatar
Michael Niedermayer committed
355 356
            put_bits(&s->pb, 1, s->dc_table_index);
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
357 358
    } else {
        put_bits(&s->pb, 1, s->use_skip_mb_code);
359

360
        if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE)
Michael Niedermayer's avatar
Michael Niedermayer committed
361 362
            put_bits(&s->pb, 1, s->per_mb_rl_table);

Michael Niedermayer's avatar
Michael Niedermayer committed
363
        if(s->msmpeg4_version>2){
Michael Niedermayer's avatar
Michael Niedermayer committed
364
            if(!s->per_mb_rl_table)
365
                ff_code012(&s->pb, s->rl_table_index);
Fabrice Bellard's avatar
Fabrice Bellard committed
366

Michael Niedermayer's avatar
Michael Niedermayer committed
367 368 369 370
            put_bits(&s->pb, 1, s->dc_table_index);

            put_bits(&s->pb, 1, s->mv_table_index);
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
371 372
    }

Michael Niedermayer's avatar
Michael Niedermayer committed
373 374
    s->esc3_level_length= 0;
    s->esc3_run_length= 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
375 376 377

#ifdef DEBUG
    intra_count = 0;
378
    av_log(s->avctx, AV_LOG_DEBUG, "*****frame %d:\n", frame_count++);
Fabrice Bellard's avatar
Fabrice Bellard committed
379 380 381
#endif
}

Michael Niedermayer's avatar
Michael Niedermayer committed
382 383
void msmpeg4_encode_ext_header(MpegEncContext * s)
{
384
        put_bits(&s->pb, 5, s->avctx->time_base.den / s->avctx->time_base.num); //yes 29.97 -> 29
Michael Niedermayer's avatar
Michael Niedermayer committed
385

Michael Niedermayer's avatar
Michael Niedermayer committed
386
        put_bits(&s->pb, 11, FFMIN(s->bit_rate/1024, 2047));
Michael Niedermayer's avatar
Michael Niedermayer committed
387

388
        if(s->msmpeg4_version>=3)
Michael Niedermayer's avatar
Michael Niedermayer committed
389
            put_bits(&s->pb, 1, s->flipflop_rounding);
390 391
        else
            assert(s->flipflop_rounding==0);
Michael Niedermayer's avatar
Michael Niedermayer committed
392 393
}

394 395
#endif //CONFIG_ENCODERS

Fabrice Bellard's avatar
Fabrice Bellard committed
396
/* predict coded block */
397
static inline int coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
Fabrice Bellard's avatar
Fabrice Bellard committed
398
{
399
    int xy, wrap, pred, a, b, c;
Fabrice Bellard's avatar
Fabrice Bellard committed
400

401
    xy = s->block_index[n];
402
    wrap = s->b8_stride;
Fabrice Bellard's avatar
Fabrice Bellard committed
403 404

    /* B C
405
     * A X
Fabrice Bellard's avatar
Fabrice Bellard committed
406
     */
407 408 409
    a = s->coded_block[xy - 1       ];
    b = s->coded_block[xy - 1 - wrap];
    c = s->coded_block[xy     - wrap];
410

Fabrice Bellard's avatar
Fabrice Bellard committed
411 412 413 414 415
    if (b == c) {
        pred = a;
    } else {
        pred = c;
    }
416

Fabrice Bellard's avatar
Fabrice Bellard committed
417
    /* store value */
418
    *coded_block_ptr = &s->coded_block[xy];
Fabrice Bellard's avatar
Fabrice Bellard committed
419 420 421 422

    return pred;
}

423 424
#ifdef CONFIG_ENCODERS

425
static void msmpeg4_encode_motion(MpegEncContext * s,
Fabrice Bellard's avatar
Fabrice Bellard committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
                                  int mx, int my)
{
    int code;
    MVTable *mv;

    /* modulo encoding */
    /* WARNING : you cannot reach all the MVs even with the modulo
       encoding. This is a somewhat strange compromise they took !!!  */
    if (mx <= -64)
        mx += 64;
    else if (mx >= 64)
        mx -= 64;
    if (my <= -64)
        my += 64;
    else if (my >= 64)
        my -= 64;
442

Fabrice Bellard's avatar
Fabrice Bellard committed
443 444 445 446
    mx += 32;
    my += 32;
#if 0
    if ((unsigned)mx >= 64 ||
447
        (unsigned)my >= 64)
448
        av_log(s->avctx, AV_LOG_ERROR, "error mx=%d my=%d\n", mx, my);
Fabrice Bellard's avatar
Fabrice Bellard committed
449 450 451 452
#endif
    mv = &mv_tables[s->mv_table_index];

    code = mv->table_mv_index[(mx << 6) | my];
453 454
    put_bits(&s->pb,
             mv->table_mv_bits[code],
Fabrice Bellard's avatar
Fabrice Bellard committed
455 456
             mv->table_mv_code[code]);
    if (code == mv->n) {
Diego Biurrun's avatar
Diego Biurrun committed
457
        /* escape : code literally */
Fabrice Bellard's avatar
Fabrice Bellard committed
458 459 460 461 462
        put_bits(&s->pb, 6, mx);
        put_bits(&s->pb, 6, my);
    }
}

463 464 465
static inline void handle_slices(MpegEncContext *s){
    if (s->mb_x == 0) {
        if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
Michael Niedermayer's avatar
Michael Niedermayer committed
466
            if(s->msmpeg4_version < 4){
467
                ff_mpeg4_clean_buffers(s);
468 469 470
            }
            s->first_slice_line = 1;
        } else {
471
            s->first_slice_line = 0;
472 473 474 475
        }
    }
}

476
void msmpeg4_encode_mb(MpegEncContext * s,
Fabrice Bellard's avatar
Fabrice Bellard committed
477 478 479 480 481
                       DCTELEM block[6][64],
                       int motion_x, int motion_y)
{
    int cbp, coded_cbp, i;
    int pred_x, pred_y;
482
    uint8_t *coded_block;
Fabrice Bellard's avatar
Fabrice Bellard committed
483

484
    handle_slices(s);
485

Fabrice Bellard's avatar
Fabrice Bellard committed
486
    if (!s->mb_intra) {
487 488 489 490 491 492 493 494 495
        /* compute cbp */
        cbp = 0;
        for (i = 0; i < 6; i++) {
            if (s->block_last_index[i] >= 0)
                cbp |= 1 << (5 - i);
        }
        if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
            /* skip macroblock */
            put_bits(&s->pb, 1, 1);
496
            s->last_bits++;
497
            s->misc_bits++;
498
            s->skip_count++;
499

500 501
            return;
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
502
        if (s->use_skip_mb_code)
503
            put_bits(&s->pb, 1, 0);     /* mb coded */
504

Michael Niedermayer's avatar
Michael Niedermayer committed
505
        if(s->msmpeg4_version<=2){
506 507
            put_bits(&s->pb,
                     v2_mb_type[cbp&3][1],
Michael Niedermayer's avatar
Michael Niedermayer committed
508 509 510 511
                     v2_mb_type[cbp&3][0]);
            if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
            else             coded_cbp= cbp;

512 513
            put_bits(&s->pb,
                     cbpy_tab[coded_cbp>>2][1],
Michael Niedermayer's avatar
Michael Niedermayer committed
514
                     cbpy_tab[coded_cbp>>2][0]);
515 516 517

            s->misc_bits += get_bits_diff(s);

518
            h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
Michael Niedermayer's avatar
Michael Niedermayer committed
519 520 521
            msmpeg4v2_encode_motion(s, motion_x - pred_x);
            msmpeg4v2_encode_motion(s, motion_y - pred_y);
        }else{
522 523
            put_bits(&s->pb,
                     table_mb_non_intra[cbp + 64][1],
Michael Niedermayer's avatar
Michael Niedermayer committed
524 525
                     table_mb_non_intra[cbp + 64][0]);

526 527
            s->misc_bits += get_bits_diff(s);

Michael Niedermayer's avatar
Michael Niedermayer committed
528
            /* motion vector */
529
            h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
530
            msmpeg4_encode_motion(s, motion_x - pred_x,
Michael Niedermayer's avatar
Michael Niedermayer committed
531 532
                                  motion_y - pred_y);
        }
533 534 535 536 537 538 539

        s->mv_bits += get_bits_diff(s);

        for (i = 0; i < 6; i++) {
            msmpeg4_encode_block(s, block[i], i);
        }
        s->p_tex_bits += get_bits_diff(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
540
    } else {
541 542
        /* compute cbp */
        cbp = 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
543
        coded_cbp = 0;
544
        for (i = 0; i < 6; i++) {
Fabrice Bellard's avatar
Fabrice Bellard committed
545 546 547 548 549 550 551 552 553 554
            int val, pred;
            val = (s->block_last_index[i] >= 1);
            cbp |= val << (5 - i);
            if (i < 4) {
                /* predict value for close blocks only for luma */
                pred = coded_block_pred(s, i, &coded_block);
                *coded_block = val;
                val = val ^ pred;
            }
            coded_cbp |= val << (5 - i);
555
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
556 557 558 559 560
#if 0
        if (coded_cbp)
            printf("cbp=%x %x\n", cbp, coded_cbp);
#endif

Michael Niedermayer's avatar
Michael Niedermayer committed
561
        if(s->msmpeg4_version<=2){
Michael Niedermayer's avatar
Michael Niedermayer committed
562
            if (s->pict_type == I_TYPE) {
563
                put_bits(&s->pb,
Michael Niedermayer's avatar
Michael Niedermayer committed
564 565 566
                         v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]);
            } else {
                if (s->use_skip_mb_code)
567
                    put_bits(&s->pb, 1, 0);     /* mb coded */
568 569
                put_bits(&s->pb,
                         v2_mb_type[(cbp&3) + 4][1],
Michael Niedermayer's avatar
Michael Niedermayer committed
570 571
                         v2_mb_type[(cbp&3) + 4][0]);
            }
572
            put_bits(&s->pb, 1, 0);             /* no AC prediction yet */
573 574
            put_bits(&s->pb,
                     cbpy_tab[cbp>>2][1],
Michael Niedermayer's avatar
Michael Niedermayer committed
575 576 577
                     cbpy_tab[cbp>>2][0]);
        }else{
            if (s->pict_type == I_TYPE) {
578
                put_bits(&s->pb,
anonymous's avatar
anonymous committed
579
                         ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]);
Michael Niedermayer's avatar
Michael Niedermayer committed
580 581
            } else {
                if (s->use_skip_mb_code)
582
                    put_bits(&s->pb, 1, 0);     /* mb coded */
583 584
                put_bits(&s->pb,
                         table_mb_non_intra[cbp][1],
Michael Niedermayer's avatar
Michael Niedermayer committed
585 586
                         table_mb_non_intra[cbp][0]);
            }
587
            put_bits(&s->pb, 1, 0);             /* no AC prediction yet */
588 589 590 591
            if(s->inter_intra_pred){
                s->h263_aic_dir=0;
                put_bits(&s->pb, table_inter_intra[s->h263_aic_dir][1], table_inter_intra[s->h263_aic_dir][0]);
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
592
        }
593
        s->misc_bits += get_bits_diff(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
594

595 596 597 598
        for (i = 0; i < 6; i++) {
            msmpeg4_encode_block(s, block[i], i);
        }
        s->i_tex_bits += get_bits_diff(s);
599
        s->i_count++;
Fabrice Bellard's avatar
Fabrice Bellard committed
600 601 602
    }
}

603 604
#endif //CONFIG_ENCODERS

605
static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n,
606
                                    int32_t **dc_val_ptr)
Michael Niedermayer's avatar
Michael Niedermayer committed
607 608 609 610 611 612 613 614
{
    int i;

    if (n < 4) {
        i= 0;
    } else {
        i= n-3;
    }
615

Michael Niedermayer's avatar
Michael Niedermayer committed
616
    *dc_val_ptr= &s->last_dc[i];
617
    return s->last_dc[i];
Michael Niedermayer's avatar
Michael Niedermayer committed
618 619
}

620 621 622 623 624 625 626 627 628 629
static int get_dc(uint8_t *src, int stride, int scale)
{
    int y;
    int sum=0;
    for(y=0; y<8; y++){
        int x;
        for(x=0; x<8; x++){
            sum+=src[x + y*stride];
        }
    }
630
    return FASTDIV((sum + (scale>>1)), scale);
631 632
}

Fabrice Bellard's avatar
Fabrice Bellard committed
633
/* dir = 0: left, dir = 1: top prediction */
634
static inline int msmpeg4_pred_dc(MpegEncContext * s, int n,
Måns Rullgård's avatar
Måns Rullgård committed
635
                             int16_t **dc_val_ptr, int *dir_ptr)
Fabrice Bellard's avatar
Fabrice Bellard committed
636
{
637
    int a, b, c, wrap, pred, scale;
Måns Rullgård's avatar
Måns Rullgård committed
638
    int16_t *dc_val;
Fabrice Bellard's avatar
Fabrice Bellard committed
639 640 641

    /* find prediction */
    if (n < 4) {
642
        scale = s->y_dc_scale;
Fabrice Bellard's avatar
Fabrice Bellard committed
643
    } else {
644
        scale = s->c_dc_scale;
Fabrice Bellard's avatar
Fabrice Bellard committed
645
    }
646

647 648
    wrap = s->block_wrap[n];
    dc_val= s->dc_val[0] + s->block_index[n];
Fabrice Bellard's avatar
Fabrice Bellard committed
649 650

    /* B C
651
     * A X
Fabrice Bellard's avatar
Fabrice Bellard committed
652
     */
653 654 655
    a = dc_val[ - 1];
    b = dc_val[ - 1 - wrap];
    c = dc_val[ - wrap];
656

Michael Niedermayer's avatar
Michael Niedermayer committed
657
    if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
658 659
        b=c=1024;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
660 661 662 663 664

    /* XXX: the following solution consumes divisions, but it does not
       necessitate to modify mpegvideo.c. The problem comes from the
       fact they decided to store the quantized DC (which would lead
       to problems if Q could vary !) */
665
#if (defined(ARCH_X86)) && !defined PIC
Michael Niedermayer's avatar
Michael Niedermayer committed
666
    asm volatile(
667 668 669 670 671 672 673 674 675 676 677 678 679 680
        "movl %3, %%eax         \n\t"
        "shrl $1, %%eax         \n\t"
        "addl %%eax, %2         \n\t"
        "addl %%eax, %1         \n\t"
        "addl %0, %%eax         \n\t"
        "mull %4                \n\t"
        "movl %%edx, %0         \n\t"
        "movl %1, %%eax         \n\t"
        "mull %4                \n\t"
        "movl %%edx, %1         \n\t"
        "movl %2, %%eax         \n\t"
        "mull %4                \n\t"
        "movl %%edx, %2         \n\t"
        : "+b" (a), "+c" (b), "+D" (c)
Måns Rullgård's avatar
Måns Rullgård committed
681
        : "g" (scale), "S" (ff_inverse[scale])
682
        : "%eax", "%edx"
Michael Niedermayer's avatar
Michael Niedermayer committed
683
    );
684 685
#else
    /* #elif defined (ARCH_ALPHA) */
686
    /* Divisions are extremely costly on Alpha; optimize the most
687 688
       common case. But they are costly everywhere...
     */
689
    if (scale == 8) {
690 691 692
        a = (a + (8 >> 1)) / 8;
        b = (b + (8 >> 1)) / 8;
        c = (c + (8 >> 1)) / 8;
693
    } else {
694 695 696
        a = FASTDIV((a + (scale >> 1)), scale);
        b = FASTDIV((b + (scale >> 1)), scale);
        c = FASTDIV((c + (scale >> 1)), scale);
697
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
698
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
699 700
    /* XXX: WARNING: they did not choose the same test as MPEG4. This
       is very important ! */
701
    if(s->msmpeg4_version>3){
702 703 704
        if(s->inter_intra_pred){
            uint8_t *dest;
            int wrap;
705

706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
            if(n==1){
                pred=a;
                *dir_ptr = 0;
            }else if(n==2){
                pred=c;
                *dir_ptr = 1;
            }else if(n==3){
                if (abs(a - b) < abs(b - c)) {
                    pred = c;
                    *dir_ptr = 1;
                } else {
                    pred = a;
                    *dir_ptr = 0;
                }
            }else{
                if(n<4){
                    wrap= s->linesize;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
723
                    dest= s->current_picture.data[0] + (((n>>1) + 2*s->mb_y) * 8*  wrap ) + ((n&1) + 2*s->mb_x) * 8;
724
                }else{
Michael Niedermayer's avatar
Michael Niedermayer committed
725
                    wrap= s->uvlinesize;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
726
                    dest= s->current_picture.data[n-3] + (s->mb_y * 8 * wrap) + s->mb_x * 8;
727 728 729 730 731
                }
                if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
                else           a= get_dc(dest-8, wrap, scale*8);
                if(s->mb_y==0) c= (1024 + (scale>>1))/scale;
                else           c= get_dc(dest-8*wrap, wrap, scale*8);
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 761 762 763 764
                if (s->h263_aic_dir==0) {
                    pred= a;
                    *dir_ptr = 0;
                }else if (s->h263_aic_dir==1) {
                    if(n==0){
                        pred= c;
                        *dir_ptr = 1;
                    }else{
                        pred= a;
                        *dir_ptr = 0;
                    }
                }else if (s->h263_aic_dir==2) {
                    if(n==0){
                        pred= a;
                        *dir_ptr = 0;
                    }else{
                        pred= c;
                        *dir_ptr = 1;
                    }
                } else {
                    pred= c;
                    *dir_ptr = 1;
                }
            }
        }else{
            if (abs(a - b) < abs(b - c)) {
                pred = c;
                *dir_ptr = 1;
            } else {
                pred = a;
                *dir_ptr = 0;
            }
765 766 767 768 769 770 771 772 773
        }
    }else{
        if (abs(a - b) <= abs(b - c)) {
            pred = c;
            *dir_ptr = 1;
        } else {
            pred = a;
            *dir_ptr = 0;
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
774 775 776
    }

    /* update predictor */
777
    *dc_val_ptr = &dc_val[0];
Fabrice Bellard's avatar
Fabrice Bellard committed
778 779 780 781 782 783 784 785 786 787
    return pred;
}

#define DC_MAX 119

static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
{
    int sign, code;
    int pred;

Michael Niedermayer's avatar
Michael Niedermayer committed
788
    if(s->msmpeg4_version==1){
789
        int32_t *dc_val;
Michael Niedermayer's avatar
Michael Niedermayer committed
790
        pred = msmpeg4v1_pred_dc(s, n, &dc_val);
791

Michael Niedermayer's avatar
Michael Niedermayer committed
792 793 794
        /* update predictor */
        *dc_val= level;
    }else{
Måns Rullgård's avatar
Måns Rullgård committed
795
        int16_t *dc_val;
796
        pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
Fabrice Bellard's avatar
Fabrice Bellard committed
797

Michael Niedermayer's avatar
Michael Niedermayer committed
798 799 800 801 802 803
        /* update predictor */
        if (n < 4) {
            *dc_val = level * s->y_dc_scale;
        } else {
            *dc_val = level * s->c_dc_scale;
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
804 805 806 807 808
    }

    /* do the prediction */
    level -= pred;

Michael Niedermayer's avatar
Michael Niedermayer committed
809
    if(s->msmpeg4_version<=2){
Fabrice Bellard's avatar
Fabrice Bellard committed
810
        if (n < 4) {
811
            put_bits(&s->pb,
Michael Niedermayer's avatar
Michael Niedermayer committed
812 813 814
                     v2_dc_lum_table[level+256][1],
                     v2_dc_lum_table[level+256][0]);
        }else{
815
            put_bits(&s->pb,
Michael Niedermayer's avatar
Michael Niedermayer committed
816 817
                     v2_dc_chroma_table[level+256][1],
                     v2_dc_chroma_table[level+256][0]);
Fabrice Bellard's avatar
Fabrice Bellard committed
818
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
819 820 821 822 823 824 825
    }else{
        sign = 0;
        if (level < 0) {
            level = -level;
            sign = 1;
        }
        code = level;
826
        if (code > DC_MAX)
Michael Niedermayer's avatar
Michael Niedermayer committed
827 828 829 830
            code = DC_MAX;

        if (s->dc_table_index == 0) {
            if (n < 4) {
831
                put_bits(&s->pb, ff_table0_dc_lum[code][1], ff_table0_dc_lum[code][0]);
Michael Niedermayer's avatar
Michael Niedermayer committed
832
            } else {
833
                put_bits(&s->pb, ff_table0_dc_chroma[code][1], ff_table0_dc_chroma[code][0]);
Michael Niedermayer's avatar
Michael Niedermayer committed
834
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
835
        } else {
Michael Niedermayer's avatar
Michael Niedermayer committed
836
            if (n < 4) {
837
                put_bits(&s->pb, ff_table1_dc_lum[code][1], ff_table1_dc_lum[code][0]);
Michael Niedermayer's avatar
Michael Niedermayer committed
838
            } else {
839
                put_bits(&s->pb, ff_table1_dc_chroma[code][1], ff_table1_dc_chroma[code][0]);
Michael Niedermayer's avatar
Michael Niedermayer committed
840 841
            }
        }
842

Michael Niedermayer's avatar
Michael Niedermayer committed
843 844
        if (code == DC_MAX)
            put_bits(&s->pb, 8, level);
845

Michael Niedermayer's avatar
Michael Niedermayer committed
846 847
        if (level != 0) {
            put_bits(&s->pb, 1, sign);
Fabrice Bellard's avatar
Fabrice Bellard committed
848 849 850 851 852 853 854
        }
    }
}

/* Encoding of a block. Very similar to MPEG4 except for a different
   escape coding (same as H263) and more vlc tables.
 */
Michael Niedermayer's avatar
Michael Niedermayer committed
855
static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
Fabrice Bellard's avatar
Fabrice Bellard committed
856 857 858 859 860
{
    int level, run, last, i, j, last_index;
    int last_non_zero, sign, slevel;
    int code, run_diff, dc_pred_dir;
    const RLTable *rl;
861
    const uint8_t *scantable;
Fabrice Bellard's avatar
Fabrice Bellard committed
862 863 864 865 866 867 868 869 870 871

    if (s->mb_intra) {
        msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
        i = 1;
        if (n < 4) {
            rl = &rl_table[s->rl_table_index];
        } else {
            rl = &rl_table[3 + s->rl_chroma_table_index];
        }
        run_diff = 0;
872
        scantable= s->intra_scantable.permutated;
Fabrice Bellard's avatar
Fabrice Bellard committed
873 874 875
    } else {
        i = 0;
        rl = &rl_table[3 + s->rl_table_index];
Michael Niedermayer's avatar
Michael Niedermayer committed
876
        if(s->msmpeg4_version<=2)
Michael Niedermayer's avatar
Michael Niedermayer committed
877 878 879
            run_diff = 0;
        else
            run_diff = 1;
880
        scantable= s->inter_scantable.permutated;
Fabrice Bellard's avatar
Fabrice Bellard committed
881 882
    }