00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #include "avcodec.h"
00031 #include "dsputil.h"
00032 #include "mpegvideo.h"
00033 #include "msmpeg4.h"
00034 #include "libavutil/x86_cpu.h"
00035 #include "h263.h"
00036 #include "mpeg4video.h"
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #define DC_VLC_BITS 9
00048 #define V2_INTRA_CBPC_VLC_BITS 3
00049 #define V2_MB_TYPE_VLC_BITS 7
00050 #define MV_VLC_BITS 9
00051 #define V2_MV_VLC_BITS 9
00052 #define TEX_VLC_BITS 9
00053
00054 #define II_BITRATE 128*1024
00055 #define MBAC_BITRATE 50*1024
00056
00057 #define DEFAULT_INTER_INDEX 3
00058
00059 static uint32_t v2_dc_lum_table[512][2];
00060 static uint32_t v2_dc_chroma_table[512][2];
00061
00062
00063 extern const uint8_t wmv3_dc_scale_table[32];
00064
00065 #include "msmpeg4data.h"
00066
00067 #if CONFIG_ENCODERS //strangely gcc includes this even if it is not referenced
00068 static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2];
00069 #endif //CONFIG_ENCODERS
00070
00071 static uint8_t static_rl_table_store[NB_RL_TABLES][2][2*MAX_RUN + MAX_LEVEL + 3];
00072
00073
00074
00075 static av_cold void init_h263_dc_for_msmpeg4(void)
00076 {
00077 int level, uni_code, uni_len;
00078
00079 for(level=-256; level<256; level++){
00080 int size, v, l;
00081
00082 size = 0;
00083 v = abs(level);
00084 while (v) {
00085 v >>= 1;
00086 size++;
00087 }
00088
00089 if (level < 0)
00090 l= (-level) ^ ((1 << size) - 1);
00091 else
00092 l= level;
00093
00094
00095 uni_code= ff_mpeg4_DCtab_lum[size][0];
00096 uni_len = ff_mpeg4_DCtab_lum[size][1];
00097 uni_code ^= (1<<uni_len)-1;
00098
00099 if (size > 0) {
00100 uni_code<<=size; uni_code|=l;
00101 uni_len+=size;
00102 if (size > 8){
00103 uni_code<<=1; uni_code|=1;
00104 uni_len++;
00105 }
00106 }
00107 v2_dc_lum_table[level+256][0]= uni_code;
00108 v2_dc_lum_table[level+256][1]= uni_len;
00109
00110
00111 uni_code= ff_mpeg4_DCtab_chrom[size][0];
00112 uni_len = ff_mpeg4_DCtab_chrom[size][1];
00113 uni_code ^= (1<<uni_len)-1;
00114
00115 if (size > 0) {
00116 uni_code<<=size; uni_code|=l;
00117 uni_len+=size;
00118 if (size > 8){
00119 uni_code<<=1; uni_code|=1;
00120 uni_len++;
00121 }
00122 }
00123 v2_dc_chroma_table[level+256][0]= uni_code;
00124 v2_dc_chroma_table[level+256][1]= uni_len;
00125
00126 }
00127 }
00128
00129 static av_cold void common_init(MpegEncContext * s)
00130 {
00131 static int initialized=0;
00132
00133 switch(s->msmpeg4_version){
00134 case 1:
00135 case 2:
00136 s->y_dc_scale_table=
00137 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
00138 break;
00139 case 3:
00140 if(s->workaround_bugs){
00141 s->y_dc_scale_table= old_ff_y_dc_scale_table;
00142 s->c_dc_scale_table= wmv1_c_dc_scale_table;
00143 } else{
00144 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
00145 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
00146 }
00147 break;
00148 case 4:
00149 case 5:
00150 s->y_dc_scale_table= wmv1_y_dc_scale_table;
00151 s->c_dc_scale_table= wmv1_c_dc_scale_table;
00152 break;
00153 #if CONFIG_VC1_DECODER
00154 case 6:
00155 s->y_dc_scale_table= wmv3_dc_scale_table;
00156 s->c_dc_scale_table= wmv3_dc_scale_table;
00157 break;
00158 #endif
00159
00160 }
00161
00162
00163 if(s->msmpeg4_version>=4){
00164 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , wmv1_scantable[1]);
00165 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, wmv1_scantable[2]);
00166 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, wmv1_scantable[3]);
00167 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , wmv1_scantable[0]);
00168 }
00169
00170
00171 if(!initialized){
00172 initialized=1;
00173
00174 init_h263_dc_for_msmpeg4();
00175 }
00176 }
00177
00178 #if CONFIG_ENCODERS
00179
00180
00181 static void init_mv_table(MVTable *tab)
00182 {
00183 int i, x, y;
00184
00185 tab->table_mv_index = av_malloc(sizeof(uint16_t) * 4096);
00186
00187 for(i=0;i<4096;i++)
00188 tab->table_mv_index[i] = tab->n;
00189
00190 for(i=0;i<tab->n;i++) {
00191 x = tab->table_mvx[i];
00192 y = tab->table_mvy[i];
00193 tab->table_mv_index[(x << 6) | y] = i;
00194 }
00195 }
00196
00197 void ff_msmpeg4_code012(PutBitContext *pb, int n)
00198 {
00199 if (n == 0) {
00200 put_bits(pb, 1, 0);
00201 } else {
00202 put_bits(pb, 1, 1);
00203 put_bits(pb, 1, (n >= 2));
00204 }
00205 }
00206
00207 static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra){
00208 int size=0;
00209 int code;
00210 int run_diff= intra ? 0 : 1;
00211
00212 code = get_rl_index(rl, last, run, level);
00213 size+= rl->table_vlc[code][1];
00214 if (code == rl->n) {
00215 int level1, run1;
00216
00217 level1 = level - rl->max_level[last][run];
00218 if (level1 < 1)
00219 goto esc2;
00220 code = get_rl_index(rl, last, run, level1);
00221 if (code == rl->n) {
00222 esc2:
00223 size++;
00224 if (level > MAX_LEVEL)
00225 goto esc3;
00226 run1 = run - rl->max_run[last][level] - run_diff;
00227 if (run1 < 0)
00228 goto esc3;
00229 code = get_rl_index(rl, last, run1, level);
00230 if (code == rl->n) {
00231 esc3:
00232
00233 size+=1+1+6+8;
00234 } else {
00235
00236 size+= 1+1+ rl->table_vlc[code][1];
00237 }
00238 } else {
00239
00240 size+= 1+1+ rl->table_vlc[code][1];
00241 }
00242 } else {
00243 size++;
00244 }
00245 return size;
00246 }
00247
00248 av_cold void ff_msmpeg4_encode_init(MpegEncContext *s)
00249 {
00250 static int init_done=0;
00251 int i;
00252
00253 common_init(s);
00254 if(s->msmpeg4_version>=4){
00255 s->min_qcoeff= -255;
00256 s->max_qcoeff= 255;
00257 }
00258
00259 if (!init_done) {
00260
00261 init_done = 1;
00262 init_mv_table(&mv_tables[0]);
00263 init_mv_table(&mv_tables[1]);
00264 for(i=0;i<NB_RL_TABLES;i++)
00265 init_rl(&rl_table[i], static_rl_table_store[i]);
00266
00267 for(i=0; i<NB_RL_TABLES; i++){
00268 int level;
00269 for (level = 1; level <= MAX_LEVEL; level++) {
00270 int run;
00271 for(run=0; run<=MAX_RUN; run++){
00272 int last;
00273 for(last=0; last<2; last++){
00274 rl_length[i][level][run][last]= get_size_of_code(s, &rl_table[ i], last, run, level, 0);
00275 }
00276 }
00277 }
00278 }
00279 }
00280 }
00281
00282 static void find_best_tables(MpegEncContext * s)
00283 {
00284 int i;
00285 int best =-1, best_size =9999999;
00286 int chroma_best=-1, best_chroma_size=9999999;
00287
00288 for(i=0; i<3; i++){
00289 int level;
00290 int chroma_size=0;
00291 int size=0;
00292
00293 if(i>0){
00294 size++;
00295 chroma_size++;
00296 }
00297 for(level=0; level<=MAX_LEVEL; level++){
00298 int run;
00299 for(run=0; run<=MAX_RUN; run++){
00300 int last;
00301 const int last_size= size + chroma_size;
00302 for(last=0; last<2; last++){
00303 int inter_count = s->ac_stats[0][0][level][run][last] + s->ac_stats[0][1][level][run][last];
00304 int intra_luma_count = s->ac_stats[1][0][level][run][last];
00305 int intra_chroma_count= s->ac_stats[1][1][level][run][last];
00306
00307 if(s->pict_type==AV_PICTURE_TYPE_I){
00308 size += intra_luma_count *rl_length[i ][level][run][last];
00309 chroma_size+= intra_chroma_count*rl_length[i+3][level][run][last];
00310 }else{
00311 size+= intra_luma_count *rl_length[i ][level][run][last]
00312 +intra_chroma_count*rl_length[i+3][level][run][last]
00313 +inter_count *rl_length[i+3][level][run][last];
00314 }
00315 }
00316 if(last_size == size+chroma_size) break;
00317 }
00318 }
00319 if(size<best_size){
00320 best_size= size;
00321 best= i;
00322 }
00323 if(chroma_size<best_chroma_size){
00324 best_chroma_size= chroma_size;
00325 chroma_best= i;
00326 }
00327 }
00328
00329
00330
00331
00332 if(s->pict_type==AV_PICTURE_TYPE_P) chroma_best= best;
00333
00334 memset(s->ac_stats, 0, sizeof(int)*(MAX_LEVEL+1)*(MAX_RUN+1)*2*2*2);
00335
00336 s->rl_table_index = best;
00337 s->rl_chroma_table_index= chroma_best;
00338
00339 if(s->pict_type != s->last_non_b_pict_type){
00340 s->rl_table_index= 2;
00341 if(s->pict_type==AV_PICTURE_TYPE_I)
00342 s->rl_chroma_table_index= 1;
00343 else
00344 s->rl_chroma_table_index= 2;
00345 }
00346
00347 }
00348
00349
00350 void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
00351 {
00352 find_best_tables(s);
00353
00354 avpriv_align_put_bits(&s->pb);
00355 put_bits(&s->pb, 2, s->pict_type - 1);
00356
00357 put_bits(&s->pb, 5, s->qscale);
00358 if(s->msmpeg4_version<=2){
00359 s->rl_table_index = 2;
00360 s->rl_chroma_table_index = 2;
00361 }
00362
00363 s->dc_table_index = 1;
00364 s->mv_table_index = 1;
00365 s->use_skip_mb_code = 1;
00366 s->per_mb_rl_table = 0;
00367 if(s->msmpeg4_version==4)
00368 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==AV_PICTURE_TYPE_P);
00369
00370
00371 if (s->pict_type == AV_PICTURE_TYPE_I) {
00372 s->slice_height= s->mb_height/1;
00373 put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height);
00374
00375 if(s->msmpeg4_version==4){
00376 msmpeg4_encode_ext_header(s);
00377 if(s->bit_rate>MBAC_BITRATE)
00378 put_bits(&s->pb, 1, s->per_mb_rl_table);
00379 }
00380
00381 if(s->msmpeg4_version>2){
00382 if(!s->per_mb_rl_table){
00383 ff_msmpeg4_code012(&s->pb, s->rl_chroma_table_index);
00384 ff_msmpeg4_code012(&s->pb, s->rl_table_index);
00385 }
00386
00387 put_bits(&s->pb, 1, s->dc_table_index);
00388 }
00389 } else {
00390 put_bits(&s->pb, 1, s->use_skip_mb_code);
00391
00392 if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE)
00393 put_bits(&s->pb, 1, s->per_mb_rl_table);
00394
00395 if(s->msmpeg4_version>2){
00396 if(!s->per_mb_rl_table)
00397 ff_msmpeg4_code012(&s->pb, s->rl_table_index);
00398
00399 put_bits(&s->pb, 1, s->dc_table_index);
00400
00401 put_bits(&s->pb, 1, s->mv_table_index);
00402 }
00403 }
00404
00405 s->esc3_level_length= 0;
00406 s->esc3_run_length= 0;
00407 }
00408
00409 void msmpeg4_encode_ext_header(MpegEncContext * s)
00410 {
00411 put_bits(&s->pb, 5, s->avctx->time_base.den / s->avctx->time_base.num);
00412
00413 put_bits(&s->pb, 11, FFMIN(s->bit_rate/1024, 2047));
00414
00415 if(s->msmpeg4_version>=3)
00416 put_bits(&s->pb, 1, s->flipflop_rounding);
00417 else
00418 assert(s->flipflop_rounding==0);
00419 }
00420
00421 #endif //CONFIG_ENCODERS
00422
00423
00424 int ff_msmpeg4_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
00425 {
00426 int xy, wrap, pred, a, b, c;
00427
00428 xy = s->block_index[n];
00429 wrap = s->b8_stride;
00430
00431
00432
00433
00434 a = s->coded_block[xy - 1 ];
00435 b = s->coded_block[xy - 1 - wrap];
00436 c = s->coded_block[xy - wrap];
00437
00438 if (b == c) {
00439 pred = a;
00440 } else {
00441 pred = c;
00442 }
00443
00444
00445 *coded_block_ptr = &s->coded_block[xy];
00446
00447 return pred;
00448 }
00449
00450 #if CONFIG_ENCODERS
00451
00452 void ff_msmpeg4_encode_motion(MpegEncContext * s,
00453 int mx, int my)
00454 {
00455 int code;
00456 MVTable *mv;
00457
00458
00459
00460
00461 if (mx <= -64)
00462 mx += 64;
00463 else if (mx >= 64)
00464 mx -= 64;
00465 if (my <= -64)
00466 my += 64;
00467 else if (my >= 64)
00468 my -= 64;
00469
00470 mx += 32;
00471 my += 32;
00472 #if 0
00473 if ((unsigned)mx >= 64 ||
00474 (unsigned)my >= 64)
00475 av_log(s->avctx, AV_LOG_ERROR, "error mx=%d my=%d\n", mx, my);
00476 #endif
00477 mv = &mv_tables[s->mv_table_index];
00478
00479 code = mv->table_mv_index[(mx << 6) | my];
00480 put_bits(&s->pb,
00481 mv->table_mv_bits[code],
00482 mv->table_mv_code[code]);
00483 if (code == mv->n) {
00484
00485 put_bits(&s->pb, 6, mx);
00486 put_bits(&s->pb, 6, my);
00487 }
00488 }
00489
00490 void ff_msmpeg4_handle_slices(MpegEncContext *s){
00491 if (s->mb_x == 0) {
00492 if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
00493 if(s->msmpeg4_version < 4){
00494 ff_mpeg4_clean_buffers(s);
00495 }
00496 s->first_slice_line = 1;
00497 } else {
00498 s->first_slice_line = 0;
00499 }
00500 }
00501 }
00502
00503 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
00504 {
00505 int range, bit_size, sign, code, bits;
00506
00507 if (val == 0) {
00508
00509 code = 0;
00510 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
00511 } else {
00512 bit_size = s->f_code - 1;
00513 range = 1 << bit_size;
00514 if (val <= -64)
00515 val += 64;
00516 else if (val >= 64)
00517 val -= 64;
00518
00519 if (val >= 0) {
00520 sign = 0;
00521 } else {
00522 val = -val;
00523 sign = 1;
00524 }
00525 val--;
00526 code = (val >> bit_size) + 1;
00527 bits = val & (range - 1);
00528
00529 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
00530 if (bit_size > 0) {
00531 put_bits(&s->pb, bit_size, bits);
00532 }
00533 }
00534 }
00535
00536 void msmpeg4_encode_mb(MpegEncContext * s,
00537 DCTELEM block[6][64],
00538 int motion_x, int motion_y)
00539 {
00540 int cbp, coded_cbp, i;
00541 int pred_x, pred_y;
00542 uint8_t *coded_block;
00543
00544 ff_msmpeg4_handle_slices(s);
00545
00546 if (!s->mb_intra) {
00547
00548 cbp = 0;
00549 for (i = 0; i < 6; i++) {
00550 if (s->block_last_index[i] >= 0)
00551 cbp |= 1 << (5 - i);
00552 }
00553 if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
00554
00555 put_bits(&s->pb, 1, 1);
00556 s->last_bits++;
00557 s->misc_bits++;
00558 s->skip_count++;
00559
00560 return;
00561 }
00562 if (s->use_skip_mb_code)
00563 put_bits(&s->pb, 1, 0);
00564
00565 if(s->msmpeg4_version<=2){
00566 put_bits(&s->pb,
00567 v2_mb_type[cbp&3][1],
00568 v2_mb_type[cbp&3][0]);
00569 if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
00570 else coded_cbp= cbp;
00571
00572 put_bits(&s->pb,
00573 ff_h263_cbpy_tab[coded_cbp>>2][1],
00574 ff_h263_cbpy_tab[coded_cbp>>2][0]);
00575
00576 s->misc_bits += get_bits_diff(s);
00577
00578 h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
00579 msmpeg4v2_encode_motion(s, motion_x - pred_x);
00580 msmpeg4v2_encode_motion(s, motion_y - pred_y);
00581 }else{
00582 put_bits(&s->pb,
00583 table_mb_non_intra[cbp + 64][1],
00584 table_mb_non_intra[cbp + 64][0]);
00585
00586 s->misc_bits += get_bits_diff(s);
00587
00588
00589 h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
00590 ff_msmpeg4_encode_motion(s, motion_x - pred_x,
00591 motion_y - pred_y);
00592 }
00593
00594 s->mv_bits += get_bits_diff(s);
00595
00596 for (i = 0; i < 6; i++) {
00597 ff_msmpeg4_encode_block(s, block[i], i);
00598 }
00599 s->p_tex_bits += get_bits_diff(s);
00600 } else {
00601
00602 cbp = 0;
00603 coded_cbp = 0;
00604 for (i = 0; i < 6; i++) {
00605 int val, pred;
00606 val = (s->block_last_index[i] >= 1);
00607 cbp |= val << (5 - i);
00608 if (i < 4) {
00609
00610 pred = ff_msmpeg4_coded_block_pred(s, i, &coded_block);
00611 *coded_block = val;
00612 val = val ^ pred;
00613 }
00614 coded_cbp |= val << (5 - i);
00615 }
00616
00617 if(s->msmpeg4_version<=2){
00618 if (s->pict_type == AV_PICTURE_TYPE_I) {
00619 put_bits(&s->pb,
00620 v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]);
00621 } else {
00622 if (s->use_skip_mb_code)
00623 put_bits(&s->pb, 1, 0);
00624 put_bits(&s->pb,
00625 v2_mb_type[(cbp&3) + 4][1],
00626 v2_mb_type[(cbp&3) + 4][0]);
00627 }
00628 put_bits(&s->pb, 1, 0);
00629 put_bits(&s->pb,
00630 ff_h263_cbpy_tab[cbp>>2][1],
00631 ff_h263_cbpy_tab[cbp>>2][0]);
00632 }else{
00633 if (s->pict_type == AV_PICTURE_TYPE_I) {
00634 put_bits(&s->pb,
00635 ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]);
00636 } else {
00637 if (s->use_skip_mb_code)
00638 put_bits(&s->pb, 1, 0);
00639 put_bits(&s->pb,
00640 table_mb_non_intra[cbp][1],
00641 table_mb_non_intra[cbp][0]);
00642 }
00643 put_bits(&s->pb, 1, 0);
00644 if(s->inter_intra_pred){
00645 s->h263_aic_dir=0;
00646 put_bits(&s->pb, table_inter_intra[s->h263_aic_dir][1], table_inter_intra[s->h263_aic_dir][0]);
00647 }
00648 }
00649 s->misc_bits += get_bits_diff(s);
00650
00651 for (i = 0; i < 6; i++) {
00652 ff_msmpeg4_encode_block(s, block[i], i);
00653 }
00654 s->i_tex_bits += get_bits_diff(s);
00655 s->i_count++;
00656 }
00657 }
00658
00659 #endif //CONFIG_ENCODERS
00660
00661 static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n,
00662 int32_t **dc_val_ptr)
00663 {
00664 int i;
00665
00666 if (n < 4) {
00667 i= 0;
00668 } else {
00669 i= n-3;
00670 }
00671
00672 *dc_val_ptr= &s->last_dc[i];
00673 return s->last_dc[i];
00674 }
00675
00676 static int get_dc(uint8_t *src, int stride, int scale)
00677 {
00678 int y;
00679 int sum=0;
00680 for(y=0; y<8; y++){
00681 int x;
00682 for(x=0; x<8; x++){
00683 sum+=src[x + y*stride];
00684 }
00685 }
00686 return FASTDIV((sum + (scale>>1)), scale);
00687 }
00688
00689
00690 static inline int msmpeg4_pred_dc(MpegEncContext * s, int n,
00691 int16_t **dc_val_ptr, int *dir_ptr)
00692 {
00693 int a, b, c, wrap, pred, scale;
00694 int16_t *dc_val;
00695
00696
00697 if (n < 4) {
00698 scale = s->y_dc_scale;
00699 } else {
00700 scale = s->c_dc_scale;
00701 }
00702
00703 wrap = s->block_wrap[n];
00704 dc_val= s->dc_val[0] + s->block_index[n];
00705
00706
00707
00708
00709 a = dc_val[ - 1];
00710 b = dc_val[ - 1 - wrap];
00711 c = dc_val[ - wrap];
00712
00713 if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
00714 b=c=1024;
00715 }
00716
00717
00718
00719
00720
00721 #if ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE
00722 __asm__ volatile(
00723 "movl %3, %%eax \n\t"
00724 "shrl $1, %%eax \n\t"
00725 "addl %%eax, %2 \n\t"
00726 "addl %%eax, %1 \n\t"
00727 "addl %0, %%eax \n\t"
00728 "mull %4 \n\t"
00729 "movl %%edx, %0 \n\t"
00730 "movl %1, %%eax \n\t"
00731 "mull %4 \n\t"
00732 "movl %%edx, %1 \n\t"
00733 "movl %2, %%eax \n\t"
00734 "mull %4 \n\t"
00735 "movl %%edx, %2 \n\t"
00736 : "+b" (a), "+c" (b), "+D" (c)
00737 : "g" (scale), "S" (ff_inverse[scale])
00738 : "%eax", "%edx"
00739 );
00740 #else
00741
00742
00743
00744
00745 if (scale == 8) {
00746 a = (a + (8 >> 1)) / 8;
00747 b = (b + (8 >> 1)) / 8;
00748 c = (c + (8 >> 1)) / 8;
00749 } else {
00750 a = FASTDIV((a + (scale >> 1)), scale);
00751 b = FASTDIV((b + (scale >> 1)), scale);
00752 c = FASTDIV((c + (scale >> 1)), scale);
00753 }
00754 #endif
00755
00756
00757 if(s->msmpeg4_version>3){
00758 if(s->inter_intra_pred){
00759 uint8_t *dest;
00760 int wrap;
00761
00762 if(n==1){
00763 pred=a;
00764 *dir_ptr = 0;
00765 }else if(n==2){
00766 pred=c;
00767 *dir_ptr = 1;
00768 }else if(n==3){
00769 if (abs(a - b) < abs(b - c)) {
00770 pred = c;
00771 *dir_ptr = 1;
00772 } else {
00773 pred = a;
00774 *dir_ptr = 0;
00775 }
00776 }else{
00777 if(n<4){
00778 wrap= s->linesize;
00779 dest= s->current_picture.f.data[0] + (((n >> 1) + 2*s->mb_y) * 8* wrap ) + ((n & 1) + 2*s->mb_x) * 8;
00780 }else{
00781 wrap= s->uvlinesize;
00782 dest= s->current_picture.f.data[n - 3] + (s->mb_y * 8 * wrap) + s->mb_x * 8;
00783 }
00784 if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
00785 else a= get_dc(dest-8, wrap, scale*8);
00786 if(s->mb_y==0) c= (1024 + (scale>>1))/scale;
00787 else c= get_dc(dest-8*wrap, wrap, scale*8);
00788
00789 if (s->h263_aic_dir==0) {
00790 pred= a;
00791 *dir_ptr = 0;
00792 }else if (s->h263_aic_dir==1) {
00793 if(n==0){
00794 pred= c;
00795 *dir_ptr = 1;
00796 }else{
00797 pred= a;
00798 *dir_ptr = 0;
00799 }
00800 }else if (s->h263_aic_dir==2) {
00801 if(n==0){
00802 pred= a;
00803 *dir_ptr = 0;
00804 }else{
00805 pred= c;
00806 *dir_ptr = 1;
00807 }
00808 } else {
00809 pred= c;
00810 *dir_ptr = 1;
00811 }
00812 }
00813 }else{
00814 if (abs(a - b) < abs(b - c)) {
00815 pred = c;
00816 *dir_ptr = 1;
00817 } else {
00818 pred = a;
00819 *dir_ptr = 0;
00820 }
00821 }
00822 }else{
00823 if (abs(a - b) <= abs(b - c)) {
00824 pred = c;
00825 *dir_ptr = 1;
00826 } else {
00827 pred = a;
00828 *dir_ptr = 0;
00829 }
00830 }
00831
00832
00833 *dc_val_ptr = &dc_val[0];
00834 return pred;
00835 }
00836
00837 #define DC_MAX 119
00838
00839 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
00840 {
00841 int sign, code;
00842 int pred, extquant;
00843 int extrabits = 0;
00844
00845 int16_t *dc_val;
00846 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
00847
00848
00849 if (n < 4) {
00850 *dc_val = level * s->y_dc_scale;
00851 } else {
00852 *dc_val = level * s->c_dc_scale;
00853 }
00854
00855
00856 level -= pred;
00857
00858 if(s->msmpeg4_version<=2){
00859 if (n < 4) {
00860 put_bits(&s->pb,
00861 v2_dc_lum_table[level+256][1],
00862 v2_dc_lum_table[level+256][0]);
00863 }else{
00864 put_bits(&s->pb,
00865 v2_dc_chroma_table[level+256][1],
00866 v2_dc_chroma_table[level+256][0]);
00867 }
00868 }else{
00869 sign = 0;
00870 if (level < 0) {
00871 level = -level;
00872 sign = 1;
00873 }
00874 code = level;
00875 if (code > DC_MAX)
00876 code = DC_MAX;
00877 else if( s->msmpeg4_version>=6 ) {
00878 if( s->qscale == 1 ) {
00879 extquant = (level + 3) & 0x3;
00880 code = ((level+3)>>2);
00881 } else if( s->qscale == 2 ) {
00882 extquant = (level + 1) & 0x1;
00883 code = ((level+1)>>1);
00884 }
00885 }
00886
00887 if (s->dc_table_index == 0) {
00888 if (n < 4) {
00889 put_bits(&s->pb, ff_table0_dc_lum[code][1], ff_table0_dc_lum[code][0]);
00890 } else {
00891 put_bits(&s->pb, ff_table0_dc_chroma[code][1], ff_table0_dc_chroma[code][0]);
00892 }
00893 } else {
00894 if (n < 4) {
00895 put_bits(&s->pb, ff_table1_dc_lum[code][1], ff_table1_dc_lum[code][0]);
00896 } else {
00897 put_bits(&s->pb, ff_table1_dc_chroma[code][1], ff_table1_dc_chroma[code][0]);
00898 }
00899 }
00900
00901 if(s->msmpeg4_version>=6 && s->qscale<=2)
00902 extrabits = 3 - s->qscale;
00903
00904 if (code == DC_MAX)
00905 put_bits(&s->pb, 8 + extrabits, level);
00906 else if(extrabits > 0)
00907 put_bits(&s->pb, extrabits, extquant);
00908
00909 if (level != 0) {
00910 put_bits(&s->pb, 1, sign);
00911 }
00912 }
00913 }
00914
00915
00916
00917
00918 void ff_msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
00919 {
00920 int level, run, last, i, j, last_index;
00921 int last_non_zero, sign, slevel;
00922 int code, run_diff, dc_pred_dir;
00923 const RLTable *rl;
00924 const uint8_t *scantable;
00925
00926 if (s->mb_intra) {
00927 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
00928 i = 1;
00929 if (n < 4) {
00930 rl = &rl_table[s->rl_table_index];
00931 } else {
00932 rl = &rl_table[3 + s->rl_chroma_table_index];
00933 }
00934 run_diff = s->msmpeg4_version>=4;
00935 scantable= s->intra_scantable.permutated;
00936 } else {
00937 i = 0;
00938 rl = &rl_table[3 + s->rl_table_index];
00939 if(s->msmpeg4_version<=2)
00940 run_diff = 0;
00941 else
00942 run_diff = 1;
00943 scantable= s->inter_scantable.permutated;
00944 }
00945
00946
00947 if(s->msmpeg4_version>=4 && s->msmpeg4_version<6 && s->block_last_index[n]>0){
00948 for(last_index=63; last_index>=0; last_index--){
00949 if(block[scantable[last_index]]) break;
00950 }
00951 s->block_last_index[n]= last_index;
00952 }else
00953 last_index = s->block_last_index[n];
00954
00955 last_non_zero = i - 1;
00956 for (; i <= last_index; i++) {
00957 j = scantable[i];
00958 level = block[j];
00959 if (level) {
00960 run = i - last_non_zero - 1;
00961 last = (i == last_index);
00962 sign = 0;
00963 slevel = level;
00964 if (level < 0) {
00965 sign = 1;
00966 level = -level;
00967 }
00968
00969 if(level<=MAX_LEVEL && run<=MAX_RUN){
00970 s->ac_stats[s->mb_intra][n>3][level][run][last]++;
00971 }
00972
00973 s->ac_stats[s->mb_intra][n > 3][40][63][0]++;
00974
00975 code = get_rl_index(rl, last, run, level);
00976 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
00977 if (code == rl->n) {
00978 int level1, run1;
00979
00980 level1 = level - rl->max_level[last][run];
00981 if (level1 < 1)
00982 goto esc2;
00983 code = get_rl_index(rl, last, run, level1);
00984 if (code == rl->n) {
00985 esc2:
00986 put_bits(&s->pb, 1, 0);
00987 if (level > MAX_LEVEL)
00988 goto esc3;
00989 run1 = run - rl->max_run[last][level] - run_diff;
00990 if (run1 < 0)
00991 goto esc3;
00992 code = get_rl_index(rl, last, run1+1, level);
00993 if (s->msmpeg4_version == 4 && code == rl->n)
00994 goto esc3;
00995 code = get_rl_index(rl, last, run1, level);
00996 if (code == rl->n) {
00997 esc3:
00998
00999 put_bits(&s->pb, 1, 0);
01000 put_bits(&s->pb, 1, last);
01001 if(s->msmpeg4_version>=4){
01002 if(s->esc3_level_length==0){
01003 s->esc3_level_length=8;
01004 s->esc3_run_length= 6;
01005
01006 if(s->qscale<8)
01007 put_bits(&s->pb, 6 + (s->msmpeg4_version>=6), 3);
01008 else
01009 put_bits(&s->pb, 8, 3);
01010 }
01011 put_bits(&s->pb, s->esc3_run_length, run);
01012 put_bits(&s->pb, 1, sign);
01013 put_bits(&s->pb, s->esc3_level_length, level);
01014 }else{
01015 put_bits(&s->pb, 6, run);
01016 put_sbits(&s->pb, 8, slevel);
01017 }
01018 } else {
01019
01020 put_bits(&s->pb, 1, 1);
01021 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
01022 put_bits(&s->pb, 1, sign);
01023 }
01024 } else {
01025
01026 put_bits(&s->pb, 1, 1);
01027 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
01028 put_bits(&s->pb, 1, sign);
01029 }
01030 } else {
01031 put_bits(&s->pb, 1, sign);
01032 }
01033 last_non_zero = i;
01034 }
01035 }
01036 }
01037
01038
01039
01040
01041 VLC ff_mb_non_intra_vlc[4];
01042 static VLC v2_dc_lum_vlc;
01043 static VLC v2_dc_chroma_vlc;
01044 static VLC v2_intra_cbpc_vlc;
01045 static VLC v2_mb_type_vlc;
01046 static VLC v2_mv_vlc;
01047 VLC ff_inter_intra_vlc;
01048
01049
01050 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
01051 {
01052 int code, val, sign, shift;
01053
01054 code = get_vlc2(&s->gb, v2_mv_vlc.table, V2_MV_VLC_BITS, 2);
01055
01056 if (code < 0)
01057 return 0xffff;
01058
01059 if (code == 0)
01060 return pred;
01061 sign = get_bits1(&s->gb);
01062 shift = f_code - 1;
01063 val = code;
01064 if (shift) {
01065 val = (val - 1) << shift;
01066 val |= get_bits(&s->gb, shift);
01067 val++;
01068 }
01069 if (sign)
01070 val = -val;
01071
01072 val += pred;
01073 if (val <= -64)
01074 val += 64;
01075 else if (val >= 64)
01076 val -= 64;
01077
01078 return val;
01079 }
01080
01081 static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
01082 {
01083 int cbp, code, i;
01084
01085 if (s->pict_type == AV_PICTURE_TYPE_P) {
01086 if (s->use_skip_mb_code) {
01087 if (get_bits1(&s->gb)) {
01088
01089 s->mb_intra = 0;
01090 for(i=0;i<6;i++)
01091 s->block_last_index[i] = -1;
01092 s->mv_dir = MV_DIR_FORWARD;
01093 s->mv_type = MV_TYPE_16X16;
01094 s->mv[0][0][0] = 0;
01095 s->mv[0][0][1] = 0;
01096 s->mb_skipped = 1;
01097 return 0;
01098 }
01099 }
01100
01101 if(s->msmpeg4_version==2)
01102 code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
01103 else
01104 code = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
01105 if(code<0 || code>7){
01106 av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
01107 return -1;
01108 }
01109
01110 s->mb_intra = code >>2;
01111
01112 cbp = code & 0x3;
01113 } else {
01114 s->mb_intra = 1;
01115 if(s->msmpeg4_version==2)
01116 cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
01117 else
01118 cbp= get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1);
01119 if(cbp<0 || cbp>3){
01120 av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
01121 return -1;
01122 }
01123 }
01124
01125 if (!s->mb_intra) {
01126 int mx, my, cbpy;
01127
01128 cbpy= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
01129 if(cbpy<0){
01130 av_log(s->avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
01131 return -1;
01132 }
01133
01134 cbp|= cbpy<<2;
01135 if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
01136
01137 h263_pred_motion(s, 0, 0, &mx, &my);
01138 mx= msmpeg4v2_decode_motion(s, mx, 1);
01139 my= msmpeg4v2_decode_motion(s, my, 1);
01140
01141 s->mv_dir = MV_DIR_FORWARD;
01142 s->mv_type = MV_TYPE_16X16;
01143 s->mv[0][0][0] = mx;
01144 s->mv[0][0][1] = my;
01145 } else {
01146 if(s->msmpeg4_version==2){
01147 s->ac_pred = get_bits1(&s->gb);
01148 cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2;
01149 } else{
01150 s->ac_pred = 0;
01151 cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2;
01152 if(s->pict_type==AV_PICTURE_TYPE_P) cbp^=0x3C;
01153 }
01154 }
01155
01156 s->dsp.clear_blocks(s->block[0]);
01157 for (i = 0; i < 6; i++) {
01158 if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
01159 {
01160 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
01161 return -1;
01162 }
01163 }
01164 return 0;
01165 }
01166
01167 static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
01168 {
01169 int cbp, code, i;
01170 uint8_t *coded_val;
01171 uint32_t * const mb_type_ptr = &s->current_picture.f.mb_type[s->mb_x + s->mb_y*s->mb_stride];
01172
01173 if (s->pict_type == AV_PICTURE_TYPE_P) {
01174 if (s->use_skip_mb_code) {
01175 if (get_bits1(&s->gb)) {
01176
01177 s->mb_intra = 0;
01178 for(i=0;i<6;i++)
01179 s->block_last_index[i] = -1;
01180 s->mv_dir = MV_DIR_FORWARD;
01181 s->mv_type = MV_TYPE_16X16;
01182 s->mv[0][0][0] = 0;
01183 s->mv[0][0][1] = 0;
01184 s->mb_skipped = 1;
01185 *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
01186
01187 return 0;
01188 }
01189 }
01190
01191 code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
01192 if (code < 0)
01193 return -1;
01194
01195 s->mb_intra = (~code & 0x40) >> 6;
01196
01197 cbp = code & 0x3f;
01198 } else {
01199 s->mb_intra = 1;
01200 code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
01201 if (code < 0)
01202 return -1;
01203
01204 cbp = 0;
01205 for(i=0;i<6;i++) {
01206 int val = ((code >> (5 - i)) & 1);
01207 if (i < 4) {
01208 int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
01209 val = val ^ pred;
01210 *coded_val = val;
01211 }
01212 cbp |= val << (5 - i);
01213 }
01214 }
01215
01216 if (!s->mb_intra) {
01217 int mx, my;
01218
01219 if(s->per_mb_rl_table && cbp){
01220 s->rl_table_index = decode012(&s->gb);
01221 s->rl_chroma_table_index = s->rl_table_index;
01222 }
01223 h263_pred_motion(s, 0, 0, &mx, &my);
01224 if (ff_msmpeg4_decode_motion(s, &mx, &my) < 0)
01225 return -1;
01226 s->mv_dir = MV_DIR_FORWARD;
01227 s->mv_type = MV_TYPE_16X16;
01228 s->mv[0][0][0] = mx;
01229 s->mv[0][0][1] = my;
01230 *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
01231 } else {
01232
01233 s->ac_pred = get_bits1(&s->gb);
01234 *mb_type_ptr = MB_TYPE_INTRA;
01235 if(s->inter_intra_pred){
01236 s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
01237
01238 }
01239 if(s->per_mb_rl_table && cbp){
01240 s->rl_table_index = decode012(&s->gb);
01241 s->rl_chroma_table_index = s->rl_table_index;
01242 }
01243 }
01244
01245 s->dsp.clear_blocks(s->block[0]);
01246 for (i = 0; i < 6; i++) {
01247 if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
01248 {
01249 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
01250 return -1;
01251 }
01252 }
01253
01254 return 0;
01255 }
01256
01257
01258 av_cold int ff_msmpeg4_decode_init(AVCodecContext *avctx)
01259 {
01260 MpegEncContext *s = avctx->priv_data;
01261 static int done = 0;
01262 int i;
01263 MVTable *mv;
01264
01265 if (ff_h263_decode_init(avctx) < 0)
01266 return -1;
01267
01268 common_init(s);
01269
01270 if (!done) {
01271 done = 1;
01272
01273 for(i=0;i<NB_RL_TABLES;i++) {
01274 init_rl(&rl_table[i], static_rl_table_store[i]);
01275 }
01276 INIT_VLC_RL(rl_table[0], 642);
01277 INIT_VLC_RL(rl_table[1], 1104);
01278 INIT_VLC_RL(rl_table[2], 554);
01279 INIT_VLC_RL(rl_table[3], 940);
01280 INIT_VLC_RL(rl_table[4], 962);
01281 INIT_VLC_RL(rl_table[5], 554);
01282
01283 mv = &mv_tables[0];
01284 INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
01285 mv->table_mv_bits, 1, 1,
01286 mv->table_mv_code, 2, 2, 3714);
01287 mv = &mv_tables[1];
01288 INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
01289 mv->table_mv_bits, 1, 1,
01290 mv->table_mv_code, 2, 2, 2694);
01291
01292 INIT_VLC_STATIC(&ff_msmp4_dc_luma_vlc[0], DC_VLC_BITS, 120,
01293 &ff_table0_dc_lum[0][1], 8, 4,
01294 &ff_table0_dc_lum[0][0], 8, 4, 1158);
01295 INIT_VLC_STATIC(&ff_msmp4_dc_chroma_vlc[0], DC_VLC_BITS, 120,
01296 &ff_table0_dc_chroma[0][1], 8, 4,
01297 &ff_table0_dc_chroma[0][0], 8, 4, 1118);
01298 INIT_VLC_STATIC(&ff_msmp4_dc_luma_vlc[1], DC_VLC_BITS, 120,
01299 &ff_table1_dc_lum[0][1], 8, 4,
01300 &ff_table1_dc_lum[0][0], 8, 4, 1476);
01301 INIT_VLC_STATIC(&ff_msmp4_dc_chroma_vlc[1], DC_VLC_BITS, 120,
01302 &ff_table1_dc_chroma[0][1], 8, 4,
01303 &ff_table1_dc_chroma[0][0], 8, 4, 1216);
01304
01305 INIT_VLC_STATIC(&v2_dc_lum_vlc, DC_VLC_BITS, 512,
01306 &v2_dc_lum_table[0][1], 8, 4,
01307 &v2_dc_lum_table[0][0], 8, 4, 1472);
01308 INIT_VLC_STATIC(&v2_dc_chroma_vlc, DC_VLC_BITS, 512,
01309 &v2_dc_chroma_table[0][1], 8, 4,
01310 &v2_dc_chroma_table[0][0], 8, 4, 1506);
01311
01312 INIT_VLC_STATIC(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
01313 &v2_intra_cbpc[0][1], 2, 1,
01314 &v2_intra_cbpc[0][0], 2, 1, 8);
01315 INIT_VLC_STATIC(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
01316 &v2_mb_type[0][1], 2, 1,
01317 &v2_mb_type[0][0], 2, 1, 128);
01318 INIT_VLC_STATIC(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
01319 &mvtab[0][1], 2, 1,
01320 &mvtab[0][0], 2, 1, 538);
01321
01322 INIT_VLC_STATIC(&ff_mb_non_intra_vlc[0], MB_NON_INTRA_VLC_BITS, 128,
01323 &wmv2_inter_table[0][0][1], 8, 4,
01324 &wmv2_inter_table[0][0][0], 8, 4, 1636);
01325 INIT_VLC_STATIC(&ff_mb_non_intra_vlc[1], MB_NON_INTRA_VLC_BITS, 128,
01326 &wmv2_inter_table[1][0][1], 8, 4,
01327 &wmv2_inter_table[1][0][0], 8, 4, 2648);
01328 INIT_VLC_STATIC(&ff_mb_non_intra_vlc[2], MB_NON_INTRA_VLC_BITS, 128,
01329 &wmv2_inter_table[2][0][1], 8, 4,
01330 &wmv2_inter_table[2][0][0], 8, 4, 1532);
01331 INIT_VLC_STATIC(&ff_mb_non_intra_vlc[3], MB_NON_INTRA_VLC_BITS, 128,
01332 &wmv2_inter_table[3][0][1], 8, 4,
01333 &wmv2_inter_table[3][0][0], 8, 4, 2488);
01334
01335 INIT_VLC_STATIC(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
01336 &ff_msmp4_mb_i_table[0][1], 4, 2,
01337 &ff_msmp4_mb_i_table[0][0], 4, 2, 536);
01338
01339 INIT_VLC_STATIC(&ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
01340 &table_inter_intra[0][1], 2, 1,
01341 &table_inter_intra[0][0], 2, 1, 8);
01342 }
01343
01344 switch(s->msmpeg4_version){
01345 case 1:
01346 case 2:
01347 s->decode_mb= msmpeg4v12_decode_mb;
01348 break;
01349 case 3:
01350 case 4:
01351 s->decode_mb= msmpeg4v34_decode_mb;
01352 break;
01353 case 5:
01354 if (CONFIG_WMV2_DECODER)
01355 s->decode_mb= ff_wmv2_decode_mb;
01356 case 6:
01357
01358 break;
01359 }
01360
01361 s->slice_height= s->mb_height;
01362
01363 return 0;
01364 }
01365
01366 int msmpeg4_decode_picture_header(MpegEncContext * s)
01367 {
01368 int code;
01369
01370 if(s->msmpeg4_version==1){
01371 int start_code = get_bits_long(&s->gb, 32);
01372 if(start_code!=0x00000100){
01373 av_log(s->avctx, AV_LOG_ERROR, "invalid startcode\n");
01374 return -1;
01375 }
01376
01377 skip_bits(&s->gb, 5);
01378 }
01379
01380 s->pict_type = get_bits(&s->gb, 2) + 1;
01381 if (s->pict_type != AV_PICTURE_TYPE_I &&
01382 s->pict_type != AV_PICTURE_TYPE_P){
01383 av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n");
01384 return -1;
01385 }
01386 #if 0
01387 {
01388 static int had_i=0;
01389 if(s->pict_type == AV_PICTURE_TYPE_I) had_i=1;
01390 if(!had_i) return -1;
01391 }
01392 #endif
01393 s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
01394 if(s->qscale==0){
01395 av_log(s->avctx, AV_LOG_ERROR, "invalid qscale\n");
01396 return -1;
01397 }
01398
01399 if (s->pict_type == AV_PICTURE_TYPE_I) {
01400 code = get_bits(&s->gb, 5);
01401 if(s->msmpeg4_version==1){
01402 if(code==0 || code>s->mb_height){
01403 av_log(s->avctx, AV_LOG_ERROR, "invalid slice height %d\n", code);
01404 return -1;
01405 }
01406
01407 s->slice_height = code;
01408 }else{
01409
01410 if (code < 0x17){
01411 av_log(s->avctx, AV_LOG_ERROR, "error, slice code was %X\n", code);
01412 return -1;
01413 }
01414
01415 s->slice_height = s->mb_height / (code - 0x16);
01416 }
01417
01418 switch(s->msmpeg4_version){
01419 case 1:
01420 case 2:
01421 s->rl_chroma_table_index = 2;
01422 s->rl_table_index = 2;
01423
01424 s->dc_table_index = 0;
01425 break;
01426 case 3:
01427 s->rl_chroma_table_index = decode012(&s->gb);
01428 s->rl_table_index = decode012(&s->gb);
01429
01430 s->dc_table_index = get_bits1(&s->gb);
01431 break;
01432 case 4:
01433 msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
01434
01435 if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
01436 else s->per_mb_rl_table= 0;
01437
01438 if(!s->per_mb_rl_table){
01439 s->rl_chroma_table_index = decode012(&s->gb);
01440 s->rl_table_index = decode012(&s->gb);
01441 }
01442
01443 s->dc_table_index = get_bits1(&s->gb);
01444 s->inter_intra_pred= 0;
01445 break;
01446 }
01447 s->no_rounding = 1;
01448 if(s->avctx->debug&FF_DEBUG_PICT_INFO)
01449 av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d \n",
01450 s->qscale,
01451 s->rl_chroma_table_index,
01452 s->rl_table_index,
01453 s->dc_table_index,
01454 s->per_mb_rl_table,
01455 s->slice_height);
01456 } else {
01457 switch(s->msmpeg4_version){
01458 case 1:
01459 case 2:
01460 if(s->msmpeg4_version==1)
01461 s->use_skip_mb_code = 1;
01462 else
01463 s->use_skip_mb_code = get_bits1(&s->gb);
01464 s->rl_table_index = 2;
01465 s->rl_chroma_table_index = s->rl_table_index;
01466 s->dc_table_index = 0;
01467 s->mv_table_index = 0;
01468 break;
01469 case 3:
01470 s->use_skip_mb_code = get_bits1(&s->gb);
01471 s->rl_table_index = decode012(&s->gb);
01472 s->rl_chroma_table_index = s->rl_table_index;
01473
01474 s->dc_table_index = get_bits1(&s->gb);
01475
01476 s->mv_table_index = get_bits1(&s->gb);
01477 break;
01478 case 4:
01479 s->use_skip_mb_code = get_bits1(&s->gb);
01480
01481 if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
01482 else s->per_mb_rl_table= 0;
01483
01484 if(!s->per_mb_rl_table){
01485 s->rl_table_index = decode012(&s->gb);
01486 s->rl_chroma_table_index = s->rl_table_index;
01487 }
01488
01489 s->dc_table_index = get_bits1(&s->gb);
01490
01491 s->mv_table_index = get_bits1(&s->gb);
01492 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
01493 break;
01494 }
01495
01496 if(s->avctx->debug&FF_DEBUG_PICT_INFO)
01497 av_log(s->avctx, AV_LOG_DEBUG, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n",
01498 s->use_skip_mb_code,
01499 s->rl_table_index,
01500 s->rl_chroma_table_index,
01501 s->dc_table_index,
01502 s->mv_table_index,
01503 s->per_mb_rl_table,
01504 s->qscale);
01505
01506 if(s->flipflop_rounding){
01507 s->no_rounding ^= 1;
01508 }else{
01509 s->no_rounding = 0;
01510 }
01511 }
01512
01513
01514 s->esc3_level_length= 0;
01515 s->esc3_run_length= 0;
01516
01517 return 0;
01518 }
01519
01520 int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
01521 {
01522 int left= buf_size*8 - get_bits_count(&s->gb);
01523 int length= s->msmpeg4_version>=3 ? 17 : 16;
01524
01525 if(left>=length && left<length+8)
01526 {
01527 skip_bits(&s->gb, 5);
01528 s->bit_rate= get_bits(&s->gb, 11)*1024;
01529 if(s->msmpeg4_version>=3)
01530 s->flipflop_rounding= get_bits1(&s->gb);
01531 else
01532 s->flipflop_rounding= 0;
01533
01534
01535 }
01536 else if(left<length+8)
01537 {
01538 s->flipflop_rounding= 0;
01539 if(s->msmpeg4_version != 2)
01540 av_log(s->avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left);
01541 }
01542 else
01543 {
01544 av_log(s->avctx, AV_LOG_ERROR, "I frame too long, ignoring ext header\n");
01545 }
01546
01547 return 0;
01548 }
01549
01550 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
01551 {
01552 int level, pred;
01553
01554 if(s->msmpeg4_version<=2){
01555 if (n < 4) {
01556 level = get_vlc2(&s->gb, v2_dc_lum_vlc.table, DC_VLC_BITS, 3);
01557 } else {
01558 level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, DC_VLC_BITS, 3);
01559 }
01560 if (level < 0)
01561 return -1;
01562 level-=256;
01563 }else{
01564 if (n < 4) {
01565 level = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
01566 } else {
01567 level = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
01568 }
01569 if (level < 0){
01570 av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
01571 return -1;
01572 }
01573
01574 if (level == DC_MAX) {
01575 level = get_bits(&s->gb, 8);
01576 if (get_bits1(&s->gb))
01577 level = -level;
01578 } else if (level != 0) {
01579 if (get_bits1(&s->gb))
01580 level = -level;
01581 }
01582 }
01583
01584 if(s->msmpeg4_version==1){
01585 int32_t *dc_val;
01586 pred = msmpeg4v1_pred_dc(s, n, &dc_val);
01587 level += pred;
01588
01589
01590 *dc_val= level;
01591 }else{
01592 int16_t *dc_val;
01593 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
01594 level += pred;
01595
01596
01597 if (n < 4) {
01598 *dc_val = level * s->y_dc_scale;
01599 } else {
01600 *dc_val = level * s->c_dc_scale;
01601 }
01602 }
01603
01604 return level;
01605 }
01606
01607
01608 int ff_msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
01609 int n, int coded, const uint8_t *scan_table)
01610 {
01611 int level, i, last, run, run_diff;
01612 int av_uninit(dc_pred_dir);
01613 RLTable *rl;
01614 RL_VLC_ELEM *rl_vlc;
01615 int qmul, qadd;
01616
01617 if (s->mb_intra) {
01618 qmul=1;
01619 qadd=0;
01620
01621
01622 level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
01623
01624 if (level < 0){
01625 av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
01626 if(s->inter_intra_pred) level=0;
01627 else return -1;
01628 }
01629 if (n < 4) {
01630 rl = &rl_table[s->rl_table_index];
01631 if(level > 256*s->y_dc_scale){
01632 av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", s->qscale);
01633 if(!s->inter_intra_pred) return -1;
01634 }
01635 } else {
01636 rl = &rl_table[3 + s->rl_chroma_table_index];
01637 if(level > 256*s->c_dc_scale){
01638 av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", s->qscale);
01639 if(!s->inter_intra_pred) return -1;
01640 }
01641 }
01642 block[0] = level;
01643
01644 run_diff = s->msmpeg4_version >= 4;
01645 i = 0;
01646 if (!coded) {
01647 goto not_coded;
01648 }
01649 if (s->ac_pred) {
01650 if (dc_pred_dir == 0)
01651 scan_table = s->intra_v_scantable.permutated;
01652 else
01653 scan_table = s->intra_h_scantable.permutated;
01654 } else {
01655 scan_table = s->intra_scantable.permutated;
01656 }
01657 rl_vlc= rl->rl_vlc[0];
01658 } else {
01659 qmul = s->qscale << 1;
01660 qadd = (s->qscale - 1) | 1;
01661 i = -1;
01662 rl = &rl_table[3 + s->rl_table_index];
01663
01664 if(s->msmpeg4_version==2)
01665 run_diff = 0;
01666 else
01667 run_diff = 1;
01668
01669 if (!coded) {
01670 s->block_last_index[n] = i;
01671 return 0;
01672 }
01673 if(!scan_table)
01674 scan_table = s->inter_scantable.permutated;
01675 rl_vlc= rl->rl_vlc[s->qscale];
01676 }
01677 {
01678 OPEN_READER(re, &s->gb);
01679 for(;;) {
01680 UPDATE_CACHE(re, &s->gb);
01681 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
01682 if (level==0) {
01683 int cache;
01684 cache= GET_CACHE(re, &s->gb);
01685
01686 if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
01687 if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
01688
01689 if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);
01690 UPDATE_CACHE(re, &s->gb);
01691 if(s->msmpeg4_version<=3){
01692 last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
01693 run= SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6);
01694 level= SHOW_SBITS(re, &s->gb, 8); LAST_SKIP_CACHE(re, &s->gb, 8);
01695 SKIP_COUNTER(re, &s->gb, 1+6+8);
01696 }else{
01697 int sign;
01698 last= SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
01699 if(!s->esc3_level_length){
01700 int ll;
01701
01702 if(s->qscale<8){
01703 ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
01704 if(ll==0){
01705 ll= 8+SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
01706 }
01707 }else{
01708 ll=2;
01709 while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
01710 ll++;
01711 SKIP_BITS(re, &s->gb, 1);
01712 }
01713 if(ll<8) SKIP_BITS(re, &s->gb, 1);
01714 }
01715
01716 s->esc3_level_length= ll;
01717 s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);
01718
01719 UPDATE_CACHE(re, &s->gb);
01720 }
01721 run= SHOW_UBITS(re, &s->gb, s->esc3_run_length);
01722 SKIP_BITS(re, &s->gb, s->esc3_run_length);
01723
01724 sign= SHOW_UBITS(re, &s->gb, 1);
01725 SKIP_BITS(re, &s->gb, 1);
01726
01727 level= SHOW_UBITS(re, &s->gb, s->esc3_level_length);
01728 SKIP_BITS(re, &s->gb, s->esc3_level_length);
01729 if(sign) level= -level;
01730 }
01731
01732 #if 0 // waste of time / this will detect very few errors
01733 {
01734 const int abs_level= FFABS(level);
01735 const int run1= run - rl->max_run[last][abs_level] - run_diff;
01736 if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
01737 if(abs_level <= rl->max_level[last][run]){
01738 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
01739 return DECODING_AC_LOST;
01740 }
01741 if(abs_level <= rl->max_level[last][run]*2){
01742 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
01743 return DECODING_AC_LOST;
01744 }
01745 if(run1>=0 && abs_level <= rl->max_level[last][run1]){
01746 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
01747 return DECODING_AC_LOST;
01748 }
01749 }
01750 }
01751 #endif
01752
01753 if (level>0) level= level * qmul + qadd;
01754 else level= level * qmul - qadd;
01755 #if 0 // waste of time too :(
01756 if(level>2048 || level<-2048){
01757 av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc\n");
01758 return DECODING_AC_LOST;
01759 }
01760 #endif
01761 i+= run + 1;
01762 if(last) i+=192;
01763 #ifdef ERROR_DETAILS
01764 if(run==66)
01765 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC3 level=%d\n", level);
01766 else if((i>62 && i<192) || i>192+63)
01767 av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level);
01768 #endif
01769 } else {
01770
01771 SKIP_BITS(re, &s->gb, 2);
01772 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
01773 i+= run + rl->max_run[run>>7][level/qmul] + run_diff;
01774 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
01775 LAST_SKIP_BITS(re, &s->gb, 1);
01776 #ifdef ERROR_DETAILS
01777 if(run==66)
01778 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC2 level=%d\n", level);
01779 else if((i>62 && i<192) || i>192+63)
01780 av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level);
01781 #endif
01782 }
01783 } else {
01784
01785 SKIP_BITS(re, &s->gb, 1);
01786 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
01787 i+= run;
01788 level = level + rl->max_level[run>>7][(run-1)&63] * qmul;
01789 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
01790 LAST_SKIP_BITS(re, &s->gb, 1);
01791 #ifdef ERROR_DETAILS
01792 if(run==66)
01793 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC1 level=%d\n", level);
01794 else if((i>62 && i<192) || i>192+63)
01795 av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level);
01796 #endif
01797 }
01798 } else {
01799 i+= run;
01800 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
01801 LAST_SKIP_BITS(re, &s->gb, 1);
01802 #ifdef ERROR_DETAILS
01803 if(run==66)
01804 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code level=%d\n", level);
01805 else if((i>62 && i<192) || i>192+63)
01806 av_log(s->avctx, AV_LOG_ERROR, "run overflow i=%d run=%d level=%d\n", i, run, level);
01807 #endif
01808 }
01809 if (i > 62){
01810 i-= 192;
01811 if(i&(~63)){
01812 const int left= get_bits_left(&s->gb);
01813 if(((i+192 == 64 && level/qmul==-1) || s->error_recognition<=1) && left>=0){
01814 av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
01815 break;
01816 }else{
01817 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
01818 return -1;
01819 }
01820 }
01821
01822 block[scan_table[i]] = level;
01823 break;
01824 }
01825
01826 block[scan_table[i]] = level;
01827 }
01828 CLOSE_READER(re, &s->gb);
01829 }
01830 not_coded:
01831 if (s->mb_intra) {
01832 mpeg4_pred_ac(s, block, n, dc_pred_dir);
01833 if (s->ac_pred) {
01834 i = 63;
01835 }
01836 }
01837 if(s->msmpeg4_version>=4 && i>0) i=63;
01838 s->block_last_index[n] = i;
01839
01840 return 0;
01841 }
01842
01843 int ff_msmpeg4_decode_motion(MpegEncContext * s,
01844 int *mx_ptr, int *my_ptr)
01845 {
01846 MVTable *mv;
01847 int code, mx, my;
01848
01849 mv = &mv_tables[s->mv_table_index];
01850
01851 code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
01852 if (code < 0){
01853 av_log(s->avctx, AV_LOG_ERROR, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
01854 return -1;
01855 }
01856 if (code == mv->n) {
01857
01858 mx = get_bits(&s->gb, 6);
01859 my = get_bits(&s->gb, 6);
01860 } else {
01861 mx = mv->table_mvx[code];
01862 my = mv->table_mvy[code];
01863 }
01864
01865 mx += *mx_ptr - 32;
01866 my += *my_ptr - 32;
01867
01868 if (mx <= -64)
01869 mx += 64;
01870 else if (mx >= 64)
01871 mx -= 64;
01872
01873 if (my <= -64)
01874 my += 64;
01875 else if (my >= 64)
01876 my -= 64;
01877 *mx_ptr = mx;
01878 *my_ptr = my;
01879 return 0;
01880 }
01881
01882 AVCodec ff_msmpeg4v1_decoder = {
01883 .name = "msmpeg4v1",
01884 .type = AVMEDIA_TYPE_VIDEO,
01885 .id = CODEC_ID_MSMPEG4V1,
01886 .priv_data_size = sizeof(MpegEncContext),
01887 .init = ff_msmpeg4_decode_init,
01888 .close = ff_h263_decode_end,
01889 .decode = ff_h263_decode_frame,
01890 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
01891 .max_lowres= 3,
01892 .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 1"),
01893 .pix_fmts= ff_pixfmt_list_420,
01894 };
01895
01896 AVCodec ff_msmpeg4v2_decoder = {
01897 .name = "msmpeg4v2",
01898 .type = AVMEDIA_TYPE_VIDEO,
01899 .id = CODEC_ID_MSMPEG4V2,
01900 .priv_data_size = sizeof(MpegEncContext),
01901 .init = ff_msmpeg4_decode_init,
01902 .close = ff_h263_decode_end,
01903 .decode = ff_h263_decode_frame,
01904 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
01905 .max_lowres= 3,
01906 .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
01907 .pix_fmts= ff_pixfmt_list_420,
01908 };
01909
01910 AVCodec ff_msmpeg4v3_decoder = {
01911 .name = "msmpeg4",
01912 .type = AVMEDIA_TYPE_VIDEO,
01913 .id = CODEC_ID_MSMPEG4V3,
01914 .priv_data_size = sizeof(MpegEncContext),
01915 .init = ff_msmpeg4_decode_init,
01916 .close = ff_h263_decode_end,
01917 .decode = ff_h263_decode_frame,
01918 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
01919 .max_lowres= 3,
01920 .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
01921 .pix_fmts= ff_pixfmt_list_420,
01922 };
01923
01924 AVCodec ff_wmv1_decoder = {
01925 .name = "wmv1",
01926 .type = AVMEDIA_TYPE_VIDEO,
01927 .id = CODEC_ID_WMV1,
01928 .priv_data_size = sizeof(MpegEncContext),
01929 .init = ff_msmpeg4_decode_init,
01930 .close = ff_h263_decode_end,
01931 .decode = ff_h263_decode_frame,
01932 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
01933 .max_lowres= 3,
01934 .long_name= NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
01935 .pix_fmts= ff_pixfmt_list_420,
01936 };