if(i<64) i= (i+1)>>1;
- tprintf(h->s.avctx, "pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
+ tprintf(h->avctx, "pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
return i&31;
}
* @param max_coeff number of coefficients in the block
* @return <0 if an error occurred
*/
-static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
- MpegEncContext * const s = &h->s;
+static int decode_residual(H264Context *h, GetBitContext *gb, int16_t *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
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_token, total_coeff, i, trailing_ones, run_before;
if(total_coeff==0)
return 0;
if(total_coeff > (unsigned)max_coeff) {
- av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff=%d)\n", s->mb_x, s->mb_y, total_coeff);
+ av_log(h->avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff=%d)\n", h->mb_x, h->mb_y, total_coeff);
return -1;
}
trailing_ones= coeff_token&3;
- tprintf(h->s.avctx, "trailing:%d, total:%d\n", trailing_ones, total_coeff);
+ tprintf(h->avctx, "trailing:%d, total:%d\n", trailing_ones, total_coeff);
assert(total_coeff<=16);
i = show_bits(gb, 3);
level_code= 30 + get_bits(gb, prefix-3); //part
if(prefix>=16){
if(prefix > 25+3){
- av_log(h->s.avctx, AV_LOG_ERROR, "Invalid level prefix\n");
+ av_log(h->avctx, AV_LOG_ERROR, "Invalid level prefix\n");
return -1;
}
level_code += (1<<(prefix-3))-4096;
else{
if (max_coeff <= 8) {
if (max_coeff == 4)
- zeros_left = get_vlc2(gb, (chroma_dc_total_zeros_vlc-1)[total_coeff].table,
+ zeros_left = get_vlc2(gb, chroma_dc_total_zeros_vlc[total_coeff - 1].table,
CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
else
- zeros_left = get_vlc2(gb, (chroma422_dc_total_zeros_vlc-1)[total_coeff].table,
+ zeros_left = get_vlc2(gb, chroma422_dc_total_zeros_vlc[total_coeff - 1].table,
CHROMA422_DC_TOTAL_ZEROS_VLC_BITS, 1);
} else {
- zeros_left= get_vlc2(gb, (total_zeros_vlc-1)[ total_coeff ].table, TOTAL_ZEROS_VLC_BITS, 1);
+ zeros_left= get_vlc2(gb, total_zeros_vlc[total_coeff - 1].table, TOTAL_ZEROS_VLC_BITS, 1);
}
}
((type*)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); \
+ run_before= get_vlc2(gb, run_vlc[zeros_left - 1].table, RUN_VLC_BITS, 1); \
else \
run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2); \
zeros_left -= run_before; \
((type*)block)[*scantable] = ((int)(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); \
+ run_before= get_vlc2(gb, run_vlc[zeros_left - 1].table, RUN_VLC_BITS, 1); \
else \
run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2); \
zeros_left -= run_before; \
} \
}
+ if (zeros_left < 0) {
+ av_log(h->avctx, AV_LOG_ERROR,
+ "negative number of zero coeffs at %d %d\n", h->mb_x, h->mb_y);
+ return AVERROR_INVALIDDATA;
+ }
+
if (h->pixel_shift) {
STORE_BLOCK(int32_t)
} else {
STORE_BLOCK(int16_t)
}
- if(zeros_left<0){
- av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
- return -1;
- }
-
return 0;
}
static av_always_inline int decode_luma_residual(H264Context *h, GetBitContext *gb, const uint8_t *scan, const uint8_t *scan8x8, int pixel_shift, int mb_type, int cbp, int p){
int i4x4, i8x8;
- MpegEncContext * const s = &h->s;
- int qscale = p == 0 ? s->qscale : h->chroma_qp[p-1];
+ int qscale = p == 0 ? h->qscale : h->chroma_qp[p-1];
if(IS_INTRA16x16(mb_type)){
AV_ZERO128(h->mb_luma_dc[p]+0);
AV_ZERO128(h->mb_luma_dc[p]+8);
for(i8x8=0; i8x8<4; i8x8++){
if(cbp & (1<<i8x8)){
if(IS_8x8DCT(mb_type)){
- DCTELEM *buf = &h->mb[64*i8x8+256*p << pixel_shift];
+ int16_t *buf = &h->mb[64*i8x8+256*p << pixel_shift];
uint8_t *nnz;
for(i4x4=0; i4x4<4; i4x4++){
const int index= i4x4 + 4*i8x8 + p*16;
}
int ff_h264_decode_mb_cavlc(H264Context *h){
- MpegEncContext * const s = &h->s;
int mb_xy;
int partition_count;
unsigned int mb_type, cbp;
int decode_chroma = h->sps.chroma_format_idc == 1 || h->sps.chroma_format_idc == 2;
const int pixel_shift = h->pixel_shift;
- mb_xy = h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;
+ mb_xy = h->mb_xy = h->mb_x + h->mb_y*h->mb_stride;
- tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
+ tprintf(h->avctx, "pic:%d mb:%d/%d\n", h->frame_num, h->mb_x, h->mb_y);
cbp = 0; /* avoid warning. FIXME: find a solution without slowing
down the code */
if(h->slice_type_nos != AV_PICTURE_TYPE_I){
- if(s->mb_skip_run==-1)
- s->mb_skip_run= get_ue_golomb(&s->gb);
+ if(h->mb_skip_run==-1)
+ h->mb_skip_run= get_ue_golomb(&h->gb);
- if (s->mb_skip_run--) {
- 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);
+ if (h->mb_skip_run--) {
+ if(FRAME_MBAFF && (h->mb_y&1) == 0){
+ if(h->mb_skip_run==0)
+ h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&h->gb);
}
decode_mb_skip(h);
return 0;
}
}
if(FRAME_MBAFF){
- if( (s->mb_y&1) == 0 )
- h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
+ if( (h->mb_y&1) == 0 )
+ h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&h->gb);
}
h->prev_mb_skipped= 0;
- mb_type= get_ue_golomb(&s->gb);
+ mb_type= get_ue_golomb(&h->gb);
if(h->slice_type_nos == AV_PICTURE_TYPE_B){
if(mb_type < 23){
partition_count= b_mb_type_info[mb_type].partition_count;
mb_type--;
decode_intra_mb:
if(mb_type > 25){
- av_log(h->s.avctx, AV_LOG_ERROR, "mb_type %d in %c slice too large at %d %d\n", mb_type, av_get_picture_type_char(h->slice_type), s->mb_x, s->mb_y);
+ av_log(h->avctx, AV_LOG_ERROR, "mb_type %d in %c slice too large at %d %d\n", mb_type, av_get_picture_type_char(h->slice_type), h->mb_x, h->mb_y);
return -1;
}
partition_count=0;
h->slice_table[ mb_xy ]= h->slice_num;
if(IS_INTRA_PCM(mb_type)){
- unsigned int x;
const int mb_size = ff_h264_mb_sizes[h->sps.chroma_format_idc] *
- h->sps.bit_depth_luma >> 3;
+ h->sps.bit_depth_luma;
// We assume these blocks are very rare so we do not optimize it.
- align_get_bits(&s->gb);
-
- // The pixels are stored in the same order as levels in h->mb array.
- for(x=0; x < mb_size; x++){
- ((uint8_t*)h->mb)[x]= get_bits(&s->gb, 8);
- }
+ h->intra_pcm_ptr = align_get_bits(&h->gb);
+ skip_bits_long(&h->gb, mb_size);
// In deblocking, the quantizer is 0
- s->current_picture.f.qscale_table[mb_xy] = 0;
+ h->cur_pic.qscale_table[mb_xy] = 0;
// All coeffs are present
memset(h->non_zero_count[mb_xy], 16, 48);
- s->current_picture.f.mb_type[mb_xy] = mb_type;
+ h->cur_pic.mb_type[mb_xy] = mb_type;
return 0;
}
- if(MB_MBAFF){
- h->ref_count[0] <<= 1;
- h->ref_count[1] <<= 1;
- }
-
fill_decode_neighbors(h, mb_type);
fill_decode_caches(h, mb_type);
if(IS_INTRA4x4(mb_type)){
int i;
int di = 1;
- if(dct8x8_allowed && get_bits1(&s->gb)){
+ if(dct8x8_allowed && get_bits1(&h->gb)){
mb_type |= MB_TYPE_8x8DCT;
di = 4;
}
for(i=0; i<16; i+=di){
int mode= pred_intra_mode(h, i);
- if(!get_bits1(&s->gb)){
- const int rem_mode= get_bits(&s->gb, 3);
+ if(!get_bits1(&h->gb)){
+ const int rem_mode= get_bits(&h->gb, 3);
mode = rem_mode + (rem_mode >= mode);
}
return -1;
}
if(decode_chroma){
- pred_mode= ff_h264_check_intra_pred_mode(h, get_ue_golomb_31(&s->gb), 1);
+ pred_mode= ff_h264_check_intra_pred_mode(h, get_ue_golomb_31(&h->gb), 1);
if(pred_mode < 0)
return -1;
h->chroma_pred_mode= pred_mode;
if(h->slice_type_nos == AV_PICTURE_TYPE_B){
for(i=0; i<4; i++){
- h->sub_mb_type[i]= get_ue_golomb_31(&s->gb);
+ h->sub_mb_type[i]= get_ue_golomb_31(&h->gb);
if(h->sub_mb_type[i] >=13){
- av_log(h->s.avctx, AV_LOG_ERROR, "B sub_mb_type %u out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
+ av_log(h->avctx, AV_LOG_ERROR, "B sub_mb_type %u out of range at %d %d\n", h->sub_mb_type[i], h->mb_x, h->mb_y);
return -1;
}
sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
}else{
assert(h->slice_type_nos == AV_PICTURE_TYPE_P); //FIXME SP correct ?
for(i=0; i<4; i++){
- h->sub_mb_type[i]= get_ue_golomb_31(&s->gb);
+ h->sub_mb_type[i]= get_ue_golomb_31(&h->gb);
if(h->sub_mb_type[i] >=4){
- av_log(h->s.avctx, AV_LOG_ERROR, "P sub_mb_type %u out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
+ av_log(h->avctx, AV_LOG_ERROR, "P sub_mb_type %u out of range at %d %d\n", h->sub_mb_type[i], h->mb_x, h->mb_y);
return -1;
}
sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
}
for(list=0; list<h->list_count; list++){
- int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
+ int ref_count = IS_REF0(mb_type) ? 1 : h->ref_count[list] << MB_MBAFF;
for(i=0; i<4; i++){
if(IS_DIRECT(h->sub_mb_type[i])) continue;
if(IS_DIR(h->sub_mb_type[i], 0, list)){
if(ref_count == 1){
tmp= 0;
}else if(ref_count == 2){
- tmp= get_bits1(&s->gb)^1;
+ tmp= get_bits1(&h->gb)^1;
}else{
- tmp= get_ue_golomb_31(&s->gb);
+ tmp= get_ue_golomb_31(&h->gb);
if(tmp>=ref_count){
- av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", tmp);
+ av_log(h->avctx, AV_LOG_ERROR, "ref %u overflow\n", tmp);
return -1;
}
}
const int index= 4*i + block_width*j;
int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
- mx += get_se_golomb(&s->gb);
- my += get_se_golomb(&s->gb);
- tprintf(s->avctx, "final mv:%d %d\n", mx, my);
+ mx += get_se_golomb(&h->gb);
+ my += get_se_golomb(&h->gb);
+ tprintf(h->avctx, "final mv:%d %d\n", mx, my);
if(IS_SUB_8X8(sub_mb_type)){
mv_cache[ 1 ][0]=
for(list=0; list<h->list_count; list++){
unsigned int val;
if(IS_DIR(mb_type, 0, list)){
- if(h->ref_count[list]==1){
+ int rc = h->ref_count[list] << MB_MBAFF;
+ if (rc == 1) {
val= 0;
- }else if(h->ref_count[list]==2){
- val= get_bits1(&s->gb)^1;
+ } else if (rc == 2) {
+ val= get_bits1(&h->gb)^1;
}else{
- val= get_ue_golomb_31(&s->gb);
- if(val >= h->ref_count[list]){
- av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
+ val= get_ue_golomb_31(&h->gb);
+ if (val >= rc) {
+ av_log(h->avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
return -1;
}
}
for(list=0; list<h->list_count; list++){
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);
+ mx += get_se_golomb(&h->gb);
+ my += get_se_golomb(&h->gb);
+ tprintf(h->avctx, "final mv:%d %d\n", mx, my);
fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
}
for(i=0; i<2; i++){
unsigned int val;
if(IS_DIR(mb_type, i, list)){
- if(h->ref_count[list] == 1){
+ int rc = h->ref_count[list] << MB_MBAFF;
+ if (rc == 1) {
val= 0;
- }else if(h->ref_count[list] == 2){
- val= get_bits1(&s->gb)^1;
+ } else if (rc == 2) {
+ val= get_bits1(&h->gb)^1;
}else{
- val= get_ue_golomb_31(&s->gb);
- if(val >= h->ref_count[list]){
- av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
+ val= get_ue_golomb_31(&h->gb);
+ if (val >= rc) {
+ av_log(h->avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
return -1;
}
}
unsigned int val;
if(IS_DIR(mb_type, i, list)){
pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
- mx += get_se_golomb(&s->gb);
- my += get_se_golomb(&s->gb);
- tprintf(s->avctx, "final mv:%d %d\n", mx, my);
+ mx += get_se_golomb(&h->gb);
+ my += get_se_golomb(&h->gb);
+ tprintf(h->avctx, "final mv:%d %d\n", mx, my);
val= pack16to32(mx,my);
}else
for(i=0; i<2; i++){
unsigned int val;
if(IS_DIR(mb_type, i, list)){ //FIXME optimize
- if(h->ref_count[list]==1){
+ int rc = h->ref_count[list] << MB_MBAFF;
+ if (rc == 1) {
val= 0;
- }else if(h->ref_count[list]==2){
- val= get_bits1(&s->gb)^1;
+ } else if (rc == 2) {
+ val= get_bits1(&h->gb)^1;
}else{
- val= get_ue_golomb_31(&s->gb);
- if(val >= h->ref_count[list]){
- av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
+ val= get_ue_golomb_31(&h->gb);
+ if (val >= rc) {
+ av_log(h->avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
return -1;
}
}
unsigned int val;
if(IS_DIR(mb_type, i, list)){
pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
- mx += get_se_golomb(&s->gb);
- my += get_se_golomb(&s->gb);
- tprintf(s->avctx, "final mv:%d %d\n", mx, my);
+ mx += get_se_golomb(&h->gb);
+ my += get_se_golomb(&h->gb);
+ tprintf(h->avctx, "final mv:%d %d\n", mx, my);
val= pack16to32(mx,my);
}else
write_back_motion(h, mb_type);
if(!IS_INTRA16x16(mb_type)){
- cbp= get_ue_golomb(&s->gb);
+ cbp= get_ue_golomb(&h->gb);
if(decode_chroma){
if(cbp > 47){
- av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, s->mb_x, s->mb_y);
+ av_log(h->avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, h->mb_x, h->mb_y);
return -1;
}
if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp[cbp];
else cbp= golomb_to_inter_cbp [cbp];
}else{
if(cbp > 15){
- av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, s->mb_x, s->mb_y);
+ av_log(h->avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, h->mb_x, h->mb_y);
return -1;
}
if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp_gray[cbp];
}
if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
- mb_type |= MB_TYPE_8x8DCT*get_bits1(&s->gb);
+ mb_type |= MB_TYPE_8x8DCT*get_bits1(&h->gb);
}
h->cbp=
h->cbp_table[mb_xy]= cbp;
- s->current_picture.f.mb_type[mb_xy] = mb_type;
+ h->cur_pic.mb_type[mb_xy] = mb_type;
if(cbp || IS_INTRA16x16(mb_type)){
int i4x4, i8x8, chroma_idx;
const int max_qp = 51 + 6*(h->sps.bit_depth_luma-8);
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;
+ scan8x8= h->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
+ scan= h->qscale ? h->field_scan : h->field_scan_q0;
}else{
- scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
- scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
+ scan8x8= h->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
+ scan= h->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
}
- dquant= get_se_golomb(&s->gb);
+ dquant= get_se_golomb(&h->gb);
- s->qscale += dquant;
+ h->qscale += dquant;
- if(((unsigned)s->qscale) > max_qp){
- if(s->qscale<0) s->qscale+= max_qp+1;
- else s->qscale-= max_qp+1;
- if(((unsigned)s->qscale) > max_qp){
- av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
+ if(((unsigned)h->qscale) > max_qp){
+ if(h->qscale<0) h->qscale+= max_qp+1;
+ else h->qscale-= max_qp+1;
+ if(((unsigned)h->qscale) > max_qp){
+ av_log(h->avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, h->mb_x, h->mb_y);
return -1;
}
}
- h->chroma_qp[0]= get_chroma_qp(h, 0, s->qscale);
- h->chroma_qp[1]= get_chroma_qp(h, 1, s->qscale);
+ h->chroma_qp[0]= get_chroma_qp(h, 0, h->qscale);
+ h->chroma_qp[1]= get_chroma_qp(h, 1, h->qscale);
if( (ret = decode_luma_residual(h, gb, scan, scan8x8, pixel_shift, mb_type, cbp, 0)) < 0 ){
return -1;
if(cbp&0x20){
for(chroma_idx=0; chroma_idx<2; chroma_idx++){
const uint32_t *qmul = h->dequant4_coeff[chroma_idx+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[chroma_idx]];
- DCTELEM *mb = h->mb + (16*(16 + 16*chroma_idx) << pixel_shift);
+ int16_t *mb = h->mb + (16*(16 + 16*chroma_idx) << pixel_shift);
for (i8x8 = 0; i8x8 < 2; i8x8++) {
for (i4x4 = 0; i4x4 < 4; i4x4++) {
const int index = 16 + 16*chroma_idx + 8*i8x8 + i4x4;
fill_rectangle(&h->non_zero_count_cache[scan8[16]], 4, 4, 8, 0, 1);
fill_rectangle(&h->non_zero_count_cache[scan8[32]], 4, 4, 8, 0, 1);
}
- s->current_picture.f.qscale_table[mb_xy] = s->qscale;
+ h->cur_pic.qscale_table[mb_xy] = h->qscale;
write_back_non_zero_count(h);
- if(MB_MBAFF){
- h->ref_count[0] >>= 1;
- h->ref_count[1] >>= 1;
- }
-
return 0;
}