comparison h264.c @ 2594:5357b214eda0 libavcodec

CABAC support for MBAFF I frames patch by (Lo«Ác Le Loarer | lll+ffmpeg m4x org)
author michael
date Tue, 05 Apr 2005 01:35:22 +0000
parents 61a08e2b9822
children b5b09255f7c3
comparison
equal deleted inserted replaced
2593:786ccf72ccd5 2594:5357b214eda0
506 left_block[7]= 10; 506 left_block[7]= 10;
507 } 507 }
508 } 508 }
509 } 509 }
510 510
511 if(for_deblock){ 511 h->top_mb_xy = top_xy;
512 h->top_mb_xy = top_xy; 512 h->left_mb_xy[0] = left_xy[0];
513 h->left_mb_xy[0] = left_xy[0]; 513 h->left_mb_xy[1] = left_xy[1];
514 h->left_mb_xy[1] = left_xy[1];
515 }
516 if(for_deblock){ 514 if(for_deblock){
517 topleft_type = h->slice_table[topleft_xy ] < 255 ? s->current_picture.mb_type[topleft_xy] : 0; 515 topleft_type = h->slice_table[topleft_xy ] < 255 ? s->current_picture.mb_type[topleft_xy] : 0;
518 top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0; 516 top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
519 topright_type= h->slice_table[topright_xy] < 255 ? s->current_picture.mb_type[topright_xy]: 0; 517 topright_type= h->slice_table[topright_xy] < 255 ? s->current_picture.mb_type[topright_xy]: 0;
520 left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0; 518 left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
607 h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8]; 605 h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
608 606
609 h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12]; 607 h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
610 h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11]; 608 h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
611 609
612 h->top_cbp= h->cbp_table[top_xy];
613 }else{ 610 }else{
614 h->non_zero_count_cache[4+8*0]= 611 h->non_zero_count_cache[4+8*0]=
615 h->non_zero_count_cache[5+8*0]= 612 h->non_zero_count_cache[5+8*0]=
616 h->non_zero_count_cache[6+8*0]= 613 h->non_zero_count_cache[6+8*0]=
617 h->non_zero_count_cache[7+8*0]= 614 h->non_zero_count_cache[7+8*0]=
620 h->non_zero_count_cache[2+8*0]= 617 h->non_zero_count_cache[2+8*0]=
621 618
622 h->non_zero_count_cache[1+8*3]= 619 h->non_zero_count_cache[1+8*3]=
623 h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64; 620 h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
624 621
625 if(IS_INTRA(mb_type)) h->top_cbp= 0x1C0; 622 }
626 else h->top_cbp= 0; 623
627 }
628
629 for (i=0; i<2; i++) { 624 for (i=0; i<2; i++) {
630 if(left_type[i]){ 625 if(left_type[i]){
631 h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]]; 626 h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
632 h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]]; 627 h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
633 h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]]; 628 h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
634 h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]]; 629 h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
635 h->left_cbp= h->cbp_table[left_xy[i]]; //FIXME interlacing
636 }else{ 630 }else{
637 h->non_zero_count_cache[3+8*1 + 2*8*i]= 631 h->non_zero_count_cache[3+8*1 + 2*8*i]=
638 h->non_zero_count_cache[3+8*2 + 2*8*i]= 632 h->non_zero_count_cache[3+8*2 + 2*8*i]=
639 h->non_zero_count_cache[0+8*1 + 8*i]= 633 h->non_zero_count_cache[0+8*1 + 8*i]=
640 h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64; 634 h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
641 635 }
642 if(IS_INTRA(mb_type)) h->left_cbp= 0x1C0;//FIXME interlacing 636 }
643 else h->left_cbp= 0; 637
638 if( h->pps.cabac ) {
639 // top_cbp
640 if(top_type) {
641 h->top_cbp = h->cbp_table[top_xy];
642 } else if(IS_INTRA(mb_type)) {
643 h->top_cbp = 0x1C0;
644 } else {
645 h->top_cbp = 0;
646 }
647 // left_cbp
648 if (left_type[0]) {
649 h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
650 } else if(IS_INTRA(mb_type)) {
651 h->left_cbp = 0x1C0;
652 } else {
653 h->left_cbp = 0;
654 }
655 if (left_type[0]) {
656 h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
657 }
658 if (left_type[1]) {
659 h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
644 } 660 }
645 } 661 }
646 662
647 #if 1 663 #if 1
648 //FIXME direct mb can skip much of this 664 //FIXME direct mb can skip much of this
4357 h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type; 4373 h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
4358 } 4374 }
4359 } 4375 }
4360 4376
4361 for(list=0; list<2; list++){ 4377 for(list=0; list<2; list++){
4362 const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list]; 4378 int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
4363 if(ref_count == 0) continue; 4379 if(ref_count == 0) continue;
4380 if (h->mb_aff_frame && h->mb_field_decoding_flag) {
4381 ref_count <<= 1;
4382 }
4364 for(i=0; i<4; i++){ 4383 for(i=0; i<4; i++){
4365 if(IS_DIRECT(h->sub_mb_type[i])) continue; 4384 if(IS_DIRECT(h->sub_mb_type[i])) continue;
4366 if(IS_DIR(h->sub_mb_type[i], 0, list)){ 4385 if(IS_DIR(h->sub_mb_type[i], 0, list)){
4367 ref[list][i] = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip? 4386 ref[list][i] = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
4368 }else{ 4387 }else{
4612 write_back_non_zero_count(h); 4631 write_back_non_zero_count(h);
4613 4632
4614 return 0; 4633 return 0;
4615 } 4634 }
4616 4635
4636 static int decode_cabac_field_decoding_flag(H264Context *h) {
4637 MpegEncContext * const s = &h->s;
4638 const int mb_x = s->mb_x;
4639 const int mb_y = s->mb_y & ~1;
4640 const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
4641 const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
4642
4643 unsigned int ctx = 0;
4644
4645 if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
4646 ctx += 1;
4647 }
4648 if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
4649 ctx += 1;
4650 }
4651
4652 return get_cabac( &h->cabac, &h->cabac_state[70 + ctx] );
4653 }
4654
4617 static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) { 4655 static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
4618 uint8_t *state= &h->cabac_state[ctx_base]; 4656 uint8_t *state= &h->cabac_state[ctx_base];
4619 int mb_type; 4657 int mb_type;
4620 4658
4621 if(intra_slice){ 4659 if(intra_slice){
4755 else 4793 else
4756 return mode; 4794 return mode;
4757 } 4795 }
4758 4796
4759 static int decode_cabac_mb_chroma_pre_mode( H264Context *h) { 4797 static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
4760 MpegEncContext * const s = &h->s; 4798 const int mba_xy = h->left_mb_xy[0];
4761 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride; 4799 const int mbb_xy = h->top_mb_xy;
4762 const int mba_xy = mb_xy - 1;
4763 const int mbb_xy = mb_xy - s->mb_stride;
4764 4800
4765 int ctx = 0; 4801 int ctx = 0;
4766 4802
4767 /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */ 4803 /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
4768 if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 ) 4804 if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
4795 { 5, 7, 13, 15} 4831 { 5, 7, 13, 15}
4796 }; 4832 };
4797 4833
4798 static int decode_cabac_mb_cbp_luma( H264Context *h) { 4834 static int decode_cabac_mb_cbp_luma( H264Context *h) {
4799 MpegEncContext * const s = &h->s; 4835 MpegEncContext * const s = &h->s;
4800 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
4801 4836
4802 int cbp = 0; 4837 int cbp = 0;
4803 int i8x8; 4838 int i8x8;
4804 4839
4805 h->cbp_table[mb_xy] = 0; /* FIXME aaahahahah beurk */
4806
4807 for( i8x8 = 0; i8x8 < 4; i8x8++ ) { 4840 for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
4808 int mba_xy = -1; 4841 int cbp_a = -1;
4809 int mbb_xy = -1; 4842 int cbp_b = -1;
4810 int x, y; 4843 int x, y;
4811 int ctx = 0; 4844 int ctx = 0;
4812 4845
4813 x = block_idx_x[4*i8x8]; 4846 x = block_idx_x[4*i8x8];
4814 y = block_idx_y[4*i8x8]; 4847 y = block_idx_y[4*i8x8];
4815 4848
4816 if( x > 0 ) 4849 if( x > 0 )
4817 mba_xy = mb_xy; 4850 cbp_a = cbp;
4818 else if( s->mb_x > 0 ) { 4851 else if( s->mb_x > 0 && (h->slice_table[h->left_mb_xy[0]] == h->slice_num)) {
4819 mba_xy = mb_xy - 1; 4852 cbp_a = h->left_cbp;
4820 if (h->slice_table[mba_xy] != h->slice_num) { 4853 tprintf("cbp_a = left_cbp = %x\n", cbp_a);
4821 mba_xy = -1;
4822 }
4823 } 4854 }
4824 4855
4825 if( y > 0 ) 4856 if( y > 0 )
4826 mbb_xy = mb_xy; 4857 cbp_b = cbp;
4827 else if( s->mb_y > 0 ) { 4858 else if( s->mb_y > 0 && (h->slice_table[h->top_mb_xy] == h->slice_num)) {
4828 mbb_xy = mb_xy - s->mb_stride; 4859 cbp_b = h->top_cbp;
4829 if (h->slice_table[mbb_xy] != h->slice_num) { 4860 tprintf("cbp_b = top_cbp = %x\n", cbp_b);
4830 mbb_xy = -1;
4831 }
4832 } 4861 }
4833 4862
4834 /* No need to test for skip as we put 0 for skip block */ 4863 /* No need to test for skip as we put 0 for skip block */
4835 /* No need to test for IPCM as we put 1 for IPCM block */ 4864 /* No need to test for IPCM as we put 1 for IPCM block */
4836 if( mba_xy >= 0 ) { 4865 if( cbp_a >= 0 ) {
4837 int i8x8a = block_idx_xy[(x-1)&0x03][y]/4; 4866 int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
4838 if( ((h->cbp_table[mba_xy] >> i8x8a)&0x01) == 0 ) 4867 if( ((cbp_a >> i8x8a)&0x01) == 0 )
4839 ctx++; 4868 ctx++;
4840 } 4869 }
4841 4870
4842 if( mbb_xy >= 0 ) { 4871 if( cbp_b >= 0 ) {
4843 int i8x8b = block_idx_xy[x][(y-1)&0x03]/4; 4872 int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
4844 if( ((h->cbp_table[mbb_xy] >> i8x8b)&0x01) == 0 ) 4873 if( ((cbp_b >> i8x8b)&0x01) == 0 )
4845 ctx += 2; 4874 ctx += 2;
4846 } 4875 }
4847 4876
4848 if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) { 4877 if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
4849 cbp |= 1 << i8x8; 4878 cbp |= 1 << i8x8;
4850 h->cbp_table[mb_xy] = cbp; /* FIXME aaahahahah beurk */
4851 } 4879 }
4852 } 4880 }
4853 return cbp; 4881 return cbp;
4854 } 4882 }
4855 static int decode_cabac_mb_cbp_chroma( H264Context *h) { 4883 static int decode_cabac_mb_cbp_chroma( H264Context *h) {
5019 } 5047 }
5020 5048
5021 static int inline decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, int qp, int max_coeff) { 5049 static int inline decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, int qp, int max_coeff) {
5022 const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride; 5050 const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
5023 const uint16_t *qmul= dequant_coeff[qp]; 5051 const uint16_t *qmul= dequant_coeff[qp];
5052 static const int significant_coeff_flag_field_offset[2] = { 105, 277 };
5053 static const int last_significant_coeff_flag_field_offset[2] = { 166, 338 };
5024 static const int significant_coeff_flag_offset[5] = { 0, 15, 29, 44, 47 }; 5054 static const int significant_coeff_flag_offset[5] = { 0, 15, 29, 44, 47 };
5025 static const int coeff_abs_level_m1_offset[5] = {227+ 0, 227+10, 227+20, 227+30, 227+39 }; 5055 static const int coeff_abs_level_m1_offset[5] = {227+ 0, 227+10, 227+20, 227+30, 227+39 };
5026 5056
5027 int index[16]; 5057 int index[16];
5028 5058
5048 5078
5049 return 0; 5079 return 0;
5050 } 5080 }
5051 5081
5052 for(last= 0; last < max_coeff - 1; last++) { 5082 for(last= 0; last < max_coeff - 1; last++) {
5053 if( get_cabac( &h->cabac, &h->cabac_state[105+significant_coeff_flag_offset[cat]+last] )) { 5083 if( get_cabac( &h->cabac, &h->cabac_state[significant_coeff_flag_field_offset[h->mb_field_decoding_flag]+significant_coeff_flag_offset[cat]+last] )) {
5054 index[coeff_count++] = last; 5084 index[coeff_count++] = last;
5055 if( get_cabac( &h->cabac, &h->cabac_state[166+significant_coeff_flag_offset[cat]+last] ) ) { 5085 if( get_cabac( &h->cabac, &h->cabac_state[last_significant_coeff_flag_field_offset[h->mb_field_decoding_flag]+significant_coeff_flag_offset[cat]+last] ) ) {
5056 last= max_coeff; 5086 last= max_coeff;
5057 break; 5087 break;
5058 } 5088 }
5059 } 5089 }
5060 } 5090 }
5131 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; 5161 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
5132 int mb_type, partition_count, cbp = 0; 5162 int mb_type, partition_count, cbp = 0;
5133 5163
5134 s->dsp.clear_blocks(h->mb); //FIXME avoid if allready clear (move after skip handlong?) 5164 s->dsp.clear_blocks(h->mb); //FIXME avoid if allready clear (move after skip handlong?)
5135 5165
5136 if( h->sps.mb_aff ) {
5137 av_log( h->s.avctx, AV_LOG_ERROR, "Fields not supported with CABAC\n" );
5138 return -1;
5139 }
5140
5141 tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y); 5166 tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
5142 if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) { 5167 if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
5143 /* read skip flags */ 5168 /* read skip flags */
5144 if( decode_cabac_mb_skip( h ) ) { 5169 if( decode_cabac_mb_skip( h ) ) {
5145 decode_mb_skip(h); 5170 decode_mb_skip(h);
5150 5175
5151 return 0; 5176 return 0;
5152 5177
5153 } 5178 }
5154 } 5179 }
5180 if(h->mb_aff_frame){
5181 if ( ((s->mb_y&1) == 0) || h->prev_mb_skiped)
5182 h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
5183 }else
5184 h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
5185
5155 h->prev_mb_skiped = 0; 5186 h->prev_mb_skiped = 0;
5156 5187
5157 if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) { 5188 if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
5158 av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" ); 5189 av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
5159 return -1; 5190 return -1;
5181 partition_count = 0; 5212 partition_count = 0;
5182 cbp= i_mb_type_info[mb_type].cbp; 5213 cbp= i_mb_type_info[mb_type].cbp;
5183 h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode; 5214 h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
5184 mb_type= i_mb_type_info[mb_type].type; 5215 mb_type= i_mb_type_info[mb_type].type;
5185 } 5216 }
5186 #if 0
5187 if(h->mb_field_decoding_flag) 5217 if(h->mb_field_decoding_flag)
5188 mb_type |= MB_TYPE_INTERLACED; 5218 mb_type |= MB_TYPE_INTERLACED;
5189 #endif
5190 5219
5191 s->current_picture.mb_type[mb_xy]= mb_type; 5220 s->current_picture.mb_type[mb_xy]= mb_type;
5192 h->slice_table[ mb_xy ]= h->slice_num; 5221 h->slice_table[ mb_xy ]= h->slice_num;
5193 5222
5194 if(IS_INTRA_PCM(mb_type)) { 5223 if(IS_INTRA_PCM(mb_type)) {