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