comparison msmpeg4.c @ 578:32c142b10263 libavcodec

fixing bitrate vs. kbitrate in header fixing wmv1 decoder (was broken during some optimizations ...) inter-intra prediction encoding
author michaelni
date Mon, 22 Jul 2002 08:15:27 +0000
parents 61442627f857
children 5b7e0db3e886
comparison
equal deleted inserted replaced
577:babaca0899f1 578:32c142b10263
43 #define V2_MV_VLC_BITS 9 43 #define V2_MV_VLC_BITS 9
44 #define TEX_VLC_BITS 9 44 #define TEX_VLC_BITS 9
45 #define MB_NON_INTRA_VLC_BITS 9 45 #define MB_NON_INTRA_VLC_BITS 9
46 #define MB_INTRA_VLC_BITS 9 46 #define MB_INTRA_VLC_BITS 9
47 47
48 #define II_BITRATE 128*1024
49 #define MBAC_BITRATE 50*1024
50
48 static UINT32 v2_dc_lum_table[512][2]; 51 static UINT32 v2_dc_lum_table[512][2];
49 static UINT32 v2_dc_chroma_table[512][2]; 52 static UINT32 v2_dc_chroma_table[512][2];
50 53
51 static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n); 54 static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
52 static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, 55 static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
358 361
359 s->dc_table_index = 1; 362 s->dc_table_index = 1;
360 s->mv_table_index = 1; /* only if P frame */ 363 s->mv_table_index = 1; /* only if P frame */
361 s->use_skip_mb_code = 1; /* only if P frame */ 364 s->use_skip_mb_code = 1; /* only if P frame */
362 s->per_mb_rl_table = 0; 365 s->per_mb_rl_table = 0;
363 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=128 && s->pict_type==P_TYPE); 366 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==P_TYPE);
364 367
365 if (s->pict_type == I_TYPE) { 368 if (s->pict_type == I_TYPE) {
366 s->no_rounding = 1; 369 s->no_rounding = 1;
367 s->slice_height= s->mb_height/1; 370 s->slice_height= s->mb_height/1;
368 put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height); 371 put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height);
369 372
370 if(s->msmpeg4_version==4){ 373 if(s->msmpeg4_version==4){
371 msmpeg4_encode_ext_header(s); 374 msmpeg4_encode_ext_header(s);
372 if(s->bit_rate>50) 375 if(s->bit_rate>MBAC_BITRATE)
373 put_bits(&s->pb, 1, s->per_mb_rl_table); 376 put_bits(&s->pb, 1, s->per_mb_rl_table);
374 } 377 }
375 378
376 if(s->msmpeg4_version>2){ 379 if(s->msmpeg4_version>2){
377 if(!s->per_mb_rl_table){ 380 if(!s->per_mb_rl_table){
382 put_bits(&s->pb, 1, s->dc_table_index); 385 put_bits(&s->pb, 1, s->dc_table_index);
383 } 386 }
384 } else { 387 } else {
385 put_bits(&s->pb, 1, s->use_skip_mb_code); 388 put_bits(&s->pb, 1, s->use_skip_mb_code);
386 389
387 if(s->msmpeg4_version==4 && s->bit_rate>50) 390 if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE)
388 put_bits(&s->pb, 1, s->per_mb_rl_table); 391 put_bits(&s->pb, 1, s->per_mb_rl_table);
389 392
390 if(s->msmpeg4_version>2){ 393 if(s->msmpeg4_version>2){
391 if(!s->per_mb_rl_table) 394 if(!s->per_mb_rl_table)
392 code012(&s->pb, s->rl_table_index); 395 code012(&s->pb, s->rl_table_index);
414 417
415 void msmpeg4_encode_ext_header(MpegEncContext * s) 418 void msmpeg4_encode_ext_header(MpegEncContext * s)
416 { 419 {
417 put_bits(&s->pb, 5, s->frame_rate / FRAME_RATE_BASE); //yes 29.97 -> 29 420 put_bits(&s->pb, 5, s->frame_rate / FRAME_RATE_BASE); //yes 29.97 -> 29
418 421
419 put_bits(&s->pb, 11, MIN(s->bit_rate, 2047)); 422 put_bits(&s->pb, 11, MIN(s->bit_rate/1024, 2047));
420 423
421 if(s->msmpeg4_version<3) 424 if(s->msmpeg4_version<3)
422 s->flipflop_rounding=0; 425 s->flipflop_rounding=0;
423 else{ 426 else{
424 s->flipflop_rounding=1; 427 s->flipflop_rounding=1;
622 table_mb_non_intra[cbp][1], 625 table_mb_non_intra[cbp][1],
623 table_mb_non_intra[cbp][0]); 626 table_mb_non_intra[cbp][0]);
624 } 627 }
625 set_stat(ST_INTRA_MB); 628 set_stat(ST_INTRA_MB);
626 put_bits(&s->pb, 1, 0); /* no AC prediction yet */ 629 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
630 if(s->inter_intra_pred){
631 s->h263_aic_dir=0;
632 put_bits(&s->pb, table_inter_intra[s->h263_aic_dir][1], table_inter_intra[s->h263_aic_dir][0]);
633 }
627 } 634 }
628 } 635 }
629 636
630 for (i = 0; i < 6; i++) { 637 for (i = 0; i < 6; i++) {
631 msmpeg4_encode_block(s, block[i], i); 638 msmpeg4_encode_block(s, block[i], i);
1245 s->dc_table_index = get_bits1(&s->gb); 1252 s->dc_table_index = get_bits1(&s->gb);
1246 break; 1253 break;
1247 case 4: 1254 case 4:
1248 msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8); 1255 msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
1249 1256
1250 if(s->bit_rate > 50) s->per_mb_rl_table= get_bits1(&s->gb); 1257 if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
1251 else s->per_mb_rl_table= 0; 1258 else s->per_mb_rl_table= 0;
1252 1259
1253 if(!s->per_mb_rl_table){ 1260 if(!s->per_mb_rl_table){
1254 s->rl_chroma_table_index = decode012(&s->gb); 1261 s->rl_chroma_table_index = decode012(&s->gb);
1255 s->rl_table_index = decode012(&s->gb); 1262 s->rl_table_index = decode012(&s->gb);
1256 } 1263 }
1290 s->mv_table_index = get_bits1(&s->gb); 1297 s->mv_table_index = get_bits1(&s->gb);
1291 break; 1298 break;
1292 case 4: 1299 case 4:
1293 s->use_skip_mb_code = get_bits1(&s->gb); 1300 s->use_skip_mb_code = get_bits1(&s->gb);
1294 1301
1295 if(s->bit_rate > 50) s->per_mb_rl_table= get_bits1(&s->gb); 1302 if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
1296 else s->per_mb_rl_table= 0; 1303 else s->per_mb_rl_table= 0;
1297 1304
1298 if(!s->per_mb_rl_table){ 1305 if(!s->per_mb_rl_table){
1299 s->rl_table_index = decode012(&s->gb); 1306 s->rl_table_index = decode012(&s->gb);
1300 s->rl_chroma_table_index = s->rl_table_index; 1307 s->rl_chroma_table_index = s->rl_table_index;
1301 } 1308 }
1302 1309
1303 s->dc_table_index = get_bits1(&s->gb); 1310 s->dc_table_index = get_bits1(&s->gb);
1304 1311
1305 s->mv_table_index = get_bits1(&s->gb); 1312 s->mv_table_index = get_bits1(&s->gb);
1306 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=128); 1313 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
1307 break; 1314 break;
1308 } 1315 }
1309 /* printf("skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n", 1316 /* printf("skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n",
1310 s->use_skip_mb_code, 1317 s->use_skip_mb_code,
1311 s->rl_table_index, 1318 s->rl_table_index,
1338 if(left>=length && left<length+8) 1345 if(left>=length && left<length+8)
1339 { 1346 {
1340 int fps; 1347 int fps;
1341 1348
1342 fps= get_bits(&s->gb, 5); 1349 fps= get_bits(&s->gb, 5);
1343 s->bit_rate= get_bits(&s->gb, 11); 1350 s->bit_rate= get_bits(&s->gb, 11)*1024;
1344 if(s->msmpeg4_version>=3) 1351 if(s->msmpeg4_version>=3)
1345 s->flipflop_rounding= get_bits1(&s->gb); 1352 s->flipflop_rounding= get_bits1(&s->gb);
1346 else 1353 else
1347 s->flipflop_rounding= 0; 1354 s->flipflop_rounding= 0;
1348 1355
1349 // printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate, s->flipflop_rounding); 1356 // printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate/1024, s->flipflop_rounding);
1350 } 1357 }
1351 else if(left<length+8) 1358 else if(left<length+8)
1352 { 1359 {
1353 s->flipflop_rounding= 0; 1360 s->flipflop_rounding= 0;
1354 printf("ext header missing, %d left\n", left); 1361 printf("ext header missing, %d left\n", left);
1741 ll=2; 1748 ll=2;
1742 while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){ 1749 while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
1743 ll++; 1750 ll++;
1744 SKIP_BITS(re, &s->gb, 1); 1751 SKIP_BITS(re, &s->gb, 1);
1745 } 1752 }
1746 SKIP_BITS(re, &s->gb, 1); 1753 if(ll<8) SKIP_BITS(re, &s->gb, 1);
1747 } 1754 }
1748 1755
1749 s->esc3_level_length= ll; 1756 s->esc3_level_length= ll;
1750 s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2); 1757 s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);
1751 //printf("level length:%d, run length: %d\n", ll, s->esc3_run_length); 1758 //printf("level length:%d, run length: %d\n", ll, s->esc3_run_length);
1759 UPDATE_CACHE(re, &s->gb);
1752 } 1760 }
1753 run= SHOW_UBITS(re, &s->gb, s->esc3_run_length); 1761 run= SHOW_UBITS(re, &s->gb, s->esc3_run_length);
1754 SKIP_BITS(re, &s->gb, s->esc3_run_length); 1762 SKIP_BITS(re, &s->gb, s->esc3_run_length);
1755 1763
1756 sign= SHOW_UBITS(re, &s->gb, 1); 1764 sign= SHOW_UBITS(re, &s->gb, 1);