comparison h264.c @ 2310:c5cd8a064c34 libavcodec

H.264 CABAC + B-frames patch by (Loren Merritt <lorenm at u dot washington dot edu>)
author michael
date Fri, 22 Oct 2004 18:30:50 +0000
parents 6d26e105f68f
children cdbb2f30e08b
comparison
equal deleted inserted replaced
2309:550ae8914fd3 2310:c5cd8a064c34
3857 if( get_cabac( &h->cabac, &h->cabac_state[17+3] ) ) 3857 if( get_cabac( &h->cabac, &h->cabac_state[17+3] ) )
3858 mb_type += 1; 3858 mb_type += 1;
3859 3859
3860 return mb_type; 3860 return mb_type;
3861 } 3861 }
3862 } else if( h->slice_type == B_TYPE ) {
3863 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
3864 int ctx = 0;
3865 int bits;
3866
3867 if( s->mb_x > 0 && !IS_SKIP( s->current_picture.mb_type[mb_xy-1] )
3868 && !IS_DIRECT( s->current_picture.mb_type[mb_xy-1] ) )
3869 ctx++;
3870 if( s->mb_y > 0 && !IS_SKIP( s->current_picture.mb_type[mb_xy-s->mb_stride] )
3871 && !IS_DIRECT( s->current_picture.mb_type[mb_xy-s->mb_stride] ) )
3872 ctx++;
3873
3874 if( !get_cabac( &h->cabac, &h->cabac_state[27+ctx] ) )
3875 return 0; /* B_Direct_16x16 */
3876
3877 if( !get_cabac( &h->cabac, &h->cabac_state[27+3] ) ) {
3878 return 1 + get_cabac( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
3879 }
3880
3881 bits = get_cabac( &h->cabac, &h->cabac_state[27+4] ) << 3;
3882 bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 2;
3883 bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 1;
3884 bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] );
3885 if( bits < 8 )
3886 return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
3887 else if( bits == 13 ) {
3888 int mb_type;
3889 /* I-type */
3890 if( get_cabac( &h->cabac, &h->cabac_state[32] ) == 0 )
3891 return 23+0; /* I_4x4 */
3892 if( get_cabac_terminate( &h->cabac ) )
3893 return 23+25; /* I_PCM */
3894 mb_type = 23+1; /* I_16x16 */
3895 if( get_cabac( &h->cabac, &h->cabac_state[32+1] ) )
3896 mb_type += 12; /* cbp_luma != 0 */
3897
3898 if( get_cabac( &h->cabac, &h->cabac_state[32+2] ) ) {
3899 if( get_cabac( &h->cabac, &h->cabac_state[32+2] ) )
3900 mb_type += 4 * 2; /* cbp_chroma == 2 */
3901 else
3902 mb_type += 4 * 1; /* cbp_chroma == 1 */
3903 }
3904 if( get_cabac( &h->cabac, &h->cabac_state[32+3] ) )
3905 mb_type += 2;
3906 if( get_cabac( &h->cabac, &h->cabac_state[32+3] ) )
3907 mb_type += 1;
3908
3909 return mb_type;
3910 } else if( bits == 14 )
3911 return 11; /* B_L1_L0_8x16 */
3912 else if( bits == 15 )
3913 return 22; /* B_8x8 */
3914
3915 bits= ( bits<<1 ) | get_cabac( &h->cabac, &h->cabac_state[27+5] );
3916 return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
3862 } else { 3917 } else {
3863 /* TODO do others frames types */ 3918 /* TODO SI/SP frames? */
3864 return -1; 3919 return -1;
3865 } 3920 }
3866 } 3921 }
3867 3922
3868 static int decode_cabac_mb_skip( H264Context *h) { 3923 static int decode_cabac_mb_skip( H264Context *h) {
4044 if( val&0x01 ) 4099 if( val&0x01 )
4045 return (val + 1)/2; 4100 return (val + 1)/2;
4046 else 4101 else
4047 return -(val + 1)/2; 4102 return -(val + 1)/2;
4048 } 4103 }
4049 static int decode_cabac_mb_sub_type( H264Context *h ) { 4104 static int decode_cabac_p_mb_sub_type( H264Context *h ) {
4050 if( get_cabac( &h->cabac, &h->cabac_state[21] ) ) 4105 if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
4051 return 0; /* 8x8 */ 4106 return 0; /* 8x8 */
4052 if( !get_cabac( &h->cabac, &h->cabac_state[22] ) ) 4107 if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
4053 return 1; /* 8x4 */ 4108 return 1; /* 8x4 */
4054 if( get_cabac( &h->cabac, &h->cabac_state[23] ) ) 4109 if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
4055 return 2; /* 4x8 */ 4110 return 2; /* 4x8 */
4056 return 3; /* 4x4 */ 4111 return 3; /* 4x4 */
4112 }
4113 static int decode_cabac_b_mb_sub_type( H264Context *h ) {
4114 int type;
4115 if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
4116 return 0; /* B_Direct_8x8 */
4117 if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
4118 return 1 + get_cabac( &h->cabac, &h->cabac_state[38] ); /* B_L0_8x8, B_L1_8x8 */
4119 type = 3;
4120 if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
4121 if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
4122 return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
4123 type += 4;
4124 }
4125 type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
4126 type += get_cabac( &h->cabac, &h->cabac_state[39] );
4127 return type;
4057 } 4128 }
4058 4129
4059 static int decode_cabac_mb_ref( H264Context *h, int list, int n ) { 4130 static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
4060 int refa = h->ref_cache[list][scan8[n] - 1]; 4131 int refa = h->ref_cache[list][scan8[n] - 1];
4061 int refb = h->ref_cache[list][scan8[n] - 8]; 4132 int refb = h->ref_cache[list][scan8[n] - 8];
4356 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; 4427 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
4357 int mb_type, partition_count, cbp = 0; 4428 int mb_type, partition_count, cbp = 0;
4358 4429
4359 s->dsp.clear_blocks(h->mb); //FIXME avoid if allready clear (move after skip handlong?) 4430 s->dsp.clear_blocks(h->mb); //FIXME avoid if allready clear (move after skip handlong?)
4360 4431
4361 if( h->slice_type == B_TYPE ) {
4362 av_log( h->s.avctx, AV_LOG_ERROR, "B-frame not supported with CABAC\n" );
4363 return -1;
4364 }
4365 if( h->sps.mb_aff ) { 4432 if( h->sps.mb_aff ) {
4366 av_log( h->s.avctx, AV_LOG_ERROR, "Fields not supported with CABAC\n" ); 4433 av_log( h->s.avctx, AV_LOG_ERROR, "Fields not supported with CABAC\n" );
4367 return -1; 4434 return -1;
4368 } 4435 }
4369 4436
4370 if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) { 4437 if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
4371 /* read skip flags */ 4438 /* read skip flags */
4372 if( decode_cabac_mb_skip( h ) ) { 4439 if( decode_cabac_mb_skip( h ) ) {
4373 int mx, my; 4440 int mx, my;
4374 4441
4442 //FIXME b frame
4375 /* skip mb */ 4443 /* skip mb */
4376 mb_type= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP; 4444 mb_type= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
4377 4445
4378 memset(h->non_zero_count[mb_xy], 0, 16); 4446 memset(h->non_zero_count[mb_xy], 0, 16);
4379 memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui 4447 memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
4410 if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) { 4478 if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
4411 av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" ); 4479 av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
4412 return -1; 4480 return -1;
4413 } 4481 }
4414 4482
4415 if( h->slice_type == P_TYPE ) { 4483 if( h->slice_type == B_TYPE ) {
4484 if( mb_type < 23 ){
4485 partition_count= b_mb_type_info[mb_type].partition_count;
4486 mb_type= b_mb_type_info[mb_type].type;
4487 }else{
4488 mb_type -= 23;
4489 goto decode_intra_mb;
4490 }
4491 } else if( h->slice_type == P_TYPE ) {
4416 if( mb_type < 5) { 4492 if( mb_type < 5) {
4417 partition_count= p_mb_type_info[mb_type].partition_count; 4493 partition_count= p_mb_type_info[mb_type].partition_count;
4418 mb_type= p_mb_type_info[mb_type].type; 4494 mb_type= p_mb_type_info[mb_type].type;
4419 } else { 4495 } else {
4420 mb_type -= 5; 4496 mb_type -= 5;
4467 h->chroma_pred_mode= check_intra_pred_mode( h, h->chroma_pred_mode ); 4543 h->chroma_pred_mode= check_intra_pred_mode( h, h->chroma_pred_mode );
4468 if( h->chroma_pred_mode < 0 ) return -1; 4544 if( h->chroma_pred_mode < 0 ) return -1;
4469 } else if( partition_count == 4 ) { 4545 } else if( partition_count == 4 ) {
4470 int i, j, sub_partition_count[4], list, ref[2][4]; 4546 int i, j, sub_partition_count[4], list, ref[2][4];
4471 4547
4472 /* Only P-frame */ 4548 if( h->slice_type == B_TYPE ) {
4473 for( i = 0; i < 4; i++ ) { 4549 for( i = 0; i < 4; i++ ) {
4474 h->sub_mb_type[i] = decode_cabac_mb_sub_type( h ); 4550 h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
4475 sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count; 4551 sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
4476 h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type; 4552 h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
4553 }
4554 } else {
4555 for( i = 0; i < 4; i++ ) {
4556 h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
4557 sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
4558 h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
4559 }
4477 } 4560 }
4478 4561
4479 for( list = 0; list < 2; list++ ) { 4562 for( list = 0; list < 2; list++ ) {
4480 if( h->ref_count[list] > 0 ) { 4563 if( h->ref_count[list] > 0 ) {
4481 for( i = 0; i < 4; i++ ) { 4564 for( i = 0; i < 4; i++ ) {