*/
/**
- * @file libavcodec/h264_cavlc.c
+ * @file
* H.264 / AVC / MPEG4 part10 cavlc bitstream decoding.
* @author Michael Niedermayer <michaelni@gmx.at>
*/
+#define CABAC 0
+
#include "internal.h"
#include "avcodec.h"
#include "mpegvideo.h"
#include "h264_mvpred.h"
#include "golomb.h"
-#if ARCH_X86
-#include "x86/h264_i386.h"
-#endif
-
//#undef NDEBUG
#include <assert.h>
MpegEncContext * const s = &h->s;
static const int coeff_token_table_index[17]= {0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3};
int level[16];
- int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
+ int zeros_left, coeff_token, total_coeff, i, trailing_ones, run_before;
//FIXME put trailing_onex into the context
if(trailing_ones<total_coeff) {
int mask, prefix;
- int suffix_length = total_coeff > 10 && trailing_ones < 3;
+ int suffix_length = total_coeff > 10 & trailing_ones < 3;
int bitsi= show_bits(gb, LEVEL_TAB_BITS);
int level_code= cavlc_level_tab[suffix_length][bitsi][0];
level_code= prefix + get_bits(gb, 4); //part
}else{
level_code= 30 + get_bits(gb, prefix-3); //part
- if(prefix>=16)
+ if(prefix>=16){
+ if(prefix > 25+3){
+ av_log(h->s.avctx, AV_LOG_ERROR, "Invalid level prefix\n");
+ return -1;
+ }
level_code += (1<<(prefix-3))-4096;
+ }
}
if(trailing_ones < 3) level_code += 2;
mask= -(level_code&1);
level[trailing_ones]= (((2+level_code)>>1) ^ mask) - mask;
}else{
- if(trailing_ones < 3) level_code += (level_code>>31)|1;
+ level_code += ((level_code>>31)|1) & -(trailing_ones < 3);
- suffix_length = 1;
- if(level_code + 3U > 6U)
- suffix_length++;
+ suffix_length = 1 + (level_code + 3U > 6U);
level[trailing_ones]= level_code;
}
level_code= (((2+level_code)>>1) ^ mask) - mask;
}
level[i]= level_code;
-
- if(suffix_limit[suffix_length] + level_code > 2U*suffix_limit[suffix_length])
- suffix_length++;
+ suffix_length+= suffix_limit[suffix_length] + level_code > 2U*suffix_limit[suffix_length];
}
}
zeros_left=0;
else{
if(n == CHROMA_DC_BLOCK_INDEX)
- zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
+ zeros_left= get_vlc2(gb, (chroma_dc_total_zeros_vlc-1)[ total_coeff ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
else
- zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
+ zeros_left= get_vlc2(gb, (total_zeros_vlc-1)[ total_coeff ].table, TOTAL_ZEROS_VLC_BITS, 1);
}
- coeff_num = zeros_left + total_coeff - 1;
- j = scantable[coeff_num];
+ scantable += zeros_left + total_coeff - 1;
if(n > 24){
- block[j] = level[0];
- for(i=1;i<total_coeff;i++) {
- if(zeros_left <= 0)
- run_before = 0;
- else if(zeros_left < 7){
- run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
- }else{
+ block[*scantable] = level[0];
+ for(i=1;i<total_coeff && zeros_left > 0;i++) {
+ if(zeros_left < 7)
+ run_before= get_vlc2(gb, (run_vlc-1)[zeros_left].table, RUN_VLC_BITS, 1);
+ else
run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
- }
zeros_left -= run_before;
- coeff_num -= 1 + run_before;
- j= scantable[ coeff_num ];
-
- block[j]= level[i];
+ scantable -= 1 + run_before;
+ block[*scantable]= level[i];
+ }
+ for(;i<total_coeff;i++) {
+ scantable--;
+ block[*scantable]= level[i];
}
}else{
- block[j] = (level[0] * qmul[j] + 32)>>6;
- for(i=1;i<total_coeff;i++) {
- if(zeros_left <= 0)
- run_before = 0;
- else if(zeros_left < 7){
- run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
- }else{
+ block[*scantable] = (level[0] * qmul[*scantable] + 32)>>6;
+ for(i=1;i<total_coeff && zeros_left > 0;i++) {
+ if(zeros_left < 7)
+ run_before= get_vlc2(gb, (run_vlc-1)[zeros_left].table, RUN_VLC_BITS, 1);
+ else
run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
- }
zeros_left -= run_before;
- coeff_num -= 1 + run_before;
- j= scantable[ coeff_num ];
-
- block[j]= (level[i] * qmul[j] + 32)>>6;
+ scantable -= 1 + run_before;
+ block[*scantable]= (level[i] * qmul[*scantable] + 32)>>6;
+ }
+ for(;i<total_coeff;i++) {
+ scantable--;
+ block[*scantable]= (level[i] * qmul[*scantable] + 32)>>6;
}
}
if(FRAME_MBAFF && (s->mb_y&1) == 0){
if(s->mb_skip_run==0)
h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
- else
- predict_field_decoding_flag(h);
}
decode_mb_skip(h);
return 0;
// In deblocking, the quantizer is 0
s->current_picture.qscale_table[mb_xy]= 0;
// All coeffs are present
- memset(h->non_zero_count[mb_xy], 16, 16);
+ memset(h->non_zero_count[mb_xy], 16, 32);
s->current_picture.mb_type[mb_xy]= mb_type;
return 0;
h->ref_count[1] <<= 1;
}
- fill_caches(h, mb_type, 0);
+ fill_decode_neighbors(h, mb_type);
+ fill_decode_caches(h, mb_type);
//mb_pred
if(IS_INTRA(mb_type)){
sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
}
- if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
- || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
+ if( IS_DIRECT(h->sub_mb_type[0]|h->sub_mb_type[1]|h->sub_mb_type[2]|h->sub_mb_type[3])) {
ff_h264_pred_direct_motion(h, &mb_type);
h->ref_cache[0][scan8[4]] =
h->ref_cache[1][scan8[4]] =
return -1;
}
}
- }else
- val= LIST_NOT_USED&0xFF;
fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
+ }
}
for(list=0; list<h->list_count; list++){
- unsigned int val;
if(IS_DIR(mb_type, 0, list)){
pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
mx += get_se_golomb(&s->gb);
my += get_se_golomb(&s->gb);
tprintf(s->avctx, "final mv:%d %d\n", mx, my);
- val= pack16to32(mx,my);
- }else
- val=0;
- fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, val, 4);
+ fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
+ }
}
}
else if(IS_16X8(mb_type)){
else cbp= golomb_to_inter_cbp_gray[cbp];
}
}
- h->cbp = cbp;
if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
- if(get_bits1(&s->gb)){
- mb_type |= MB_TYPE_8x8DCT;
- h->cbp_table[mb_xy]= cbp;
- }
+ mb_type |= MB_TYPE_8x8DCT*get_bits1(&s->gb);
}
+ h->cbp=
+ h->cbp_table[mb_xy]= cbp;
s->current_picture.mb_type[mb_xy]= mb_type;
if(cbp || IS_INTRA16x16(mb_type)){
GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
const uint8_t *scan, *scan8x8, *dc_scan;
-// fill_non_zero_count_cache(h);
-
if(IS_INTERLACED(mb_type)){
scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
scan= s->qscale ? h->field_scan : h->field_scan_q0;
dquant= get_se_golomb(&s->gb);
- if( dquant > 25 || dquant < -26 ){
- av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
- return -1;
- }
-
s->qscale += dquant;
+
if(((unsigned)s->qscale) > 51){
if(s->qscale<0) s->qscale+= 52;
else s->qscale-= 52;
+ if(((unsigned)s->qscale) > 51){
+ av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
+ return -1;
+ }
}
h->chroma_qp[0]= get_chroma_qp(h, 0, s->qscale);