Commit bf4ea51e authored by michael's avatar michael

H.261 decoder improvements

- GOB's can exist of only MBA_STUFFING codes: it crashed before, but it is fixed now
- Clearer code
- Some extra checks so the decoder is more resilient against errors
patch by (Maarten Daniels <maarten dot daniels at student dot luc dot ac dot be>)


git-svn-id: file:///var/local/repositories/ffmpeg/trunk@3457 9553f0bf-9b14-0410-a0b8-cfaf0461ba5b
parent ec5ad3b0
...@@ -36,7 +36,8 @@ ...@@ -36,7 +36,8 @@
#define H261_CBP_VLC_BITS 9 #define H261_CBP_VLC_BITS 9
#define TCOEFF_VLC_BITS 9 #define TCOEFF_VLC_BITS 9
#define MAX_MBA 33 #define MBA_STUFFING 33
#define MBA_STARTCODE 34
#define IS_FIL(a) ((a)&MB_TYPE_H261_FIL) #define IS_FIL(a) ((a)&MB_TYPE_H261_FIL)
/** /**
...@@ -51,9 +52,9 @@ typedef struct H261Context{ ...@@ -51,9 +52,9 @@ typedef struct H261Context{
int current_mv_x; int current_mv_x;
int current_mv_y; int current_mv_y;
int gob_number; int gob_number;
int loop_filter;
int bits_left; //8 - nr of bits left of the following frame in the last byte in this frame int bits_left; //8 - nr of bits left of the following frame in the last byte in this frame
int last_bits; //bits left of the following frame in the last byte in this frame int last_bits; //bits left of the following frame in the last byte in this frame
int gob_start_code_skipped; // 1 if gob start code is already read before gob header is read
}H261Context; }H261Context;
void ff_h261_loop_filter(H261Context * h){ void ff_h261_loop_filter(H261Context * h){
...@@ -74,8 +75,7 @@ void ff_h261_loop_filter(H261Context * h){ ...@@ -74,8 +75,7 @@ void ff_h261_loop_filter(H261Context * h){
static int h261_decode_block(H261Context *h, DCTELEM *block, static int h261_decode_block(H261Context *h, DCTELEM *block,
int n, int coded); int n, int coded);
static int h261_decode_mb(H261Context *h, static int h261_decode_mb(H261Context *h);
DCTELEM block[6][64]);
void ff_set_qscale(MpegEncContext * s, int qscale); void ff_set_qscale(MpegEncContext * s, int qscale);
/***********************************************/ /***********************************************/
...@@ -93,7 +93,7 @@ static void h261_decode_init_vlc(H261Context *h){ ...@@ -93,7 +93,7 @@ static void h261_decode_init_vlc(H261Context *h){
if(!done){ if(!done){
done = 1; done = 1;
init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 34, init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
h261_mba_bits, 1, 1, h261_mba_bits, 1, 1,
h261_mba_code, 1, 1); h261_mba_code, 1, 1);
init_vlc(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10, init_vlc(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
...@@ -132,6 +132,7 @@ static int h261_decode_init(AVCodecContext *avctx){ ...@@ -132,6 +132,7 @@ static int h261_decode_init(AVCodecContext *avctx){
h->bits_left = 0; h->bits_left = 0;
h->last_bits = 0; h->last_bits = 0;
h->gob_start_code_skipped = 0;
return 0; return 0;
} }
...@@ -144,6 +145,7 @@ static int h261_decode_gob_header(H261Context *h){ ...@@ -144,6 +145,7 @@ static int h261_decode_gob_header(H261Context *h){
unsigned int val; unsigned int val;
MpegEncContext * const s = &h->s; MpegEncContext * const s = &h->s;
if ( !h->gob_start_code_skipped ){
/* Check for GOB Start Code */ /* Check for GOB Start Code */
val = show_bits(&s->gb, 15); val = show_bits(&s->gb, 15);
if(val) if(val)
...@@ -151,10 +153,23 @@ static int h261_decode_gob_header(H261Context *h){ ...@@ -151,10 +153,23 @@ static int h261_decode_gob_header(H261Context *h){
/* We have a GBSC */ /* We have a GBSC */
skip_bits(&s->gb, 16); skip_bits(&s->gb, 16);
}
h->gob_start_code_skipped = 0;
h->gob_number = get_bits(&s->gb, 4); /* GN */ h->gob_number = get_bits(&s->gb, 4); /* GN */
s->qscale = get_bits(&s->gb, 5); /* GQUANT */ s->qscale = get_bits(&s->gb, 5); /* GQUANT */
/* Check if gob_number is valid */
if (s->mb_height==18){ //cif
if ((h->gob_number<=0) || (h->gob_number>12))
return -1;
}
else{ //qcif
if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5))
return -1;
}
/* GEI */ /* GEI */
while (get_bits1(&s->gb) != 0) { while (get_bits1(&s->gb) != 0) {
skip_bits(&s->gb, 8); skip_bits(&s->gb, 8);
...@@ -180,6 +195,12 @@ static int ff_h261_resync(H261Context *h){ ...@@ -180,6 +195,12 @@ static int ff_h261_resync(H261Context *h){
MpegEncContext * const s = &h->s; MpegEncContext * const s = &h->s;
int left, ret; int left, ret;
if ( h->gob_start_code_skipped ){
ret= h261_decode_gob_header(h);
if(ret>=0)
return 0;
}
else{
if(show_bits(&s->gb, 15)==0){ if(show_bits(&s->gb, 15)==0){
ret= h261_decode_gob_header(h); ret= h261_decode_gob_header(h);
if(ret>=0) if(ret>=0)
...@@ -202,6 +223,7 @@ static int ff_h261_resync(H261Context *h){ ...@@ -202,6 +223,7 @@ static int ff_h261_resync(H261Context *h){
} }
skip_bits(&s->gb, 8); skip_bits(&s->gb, 8);
} }
}
return -1; return -1;
} }
...@@ -245,6 +267,11 @@ static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 ) ...@@ -245,6 +267,11 @@ static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
static int decode_mv_component(GetBitContext *gb, int v){ static int decode_mv_component(GetBitContext *gb, int v){
int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2); int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
/* check if mv_diff is valid */
if ( mv_diff < 0 )
return v;
mv_diff = mvmap[mv_diff]; mv_diff = mvmap[mv_diff];
if(mv_diff && !get_bits1(gb)) if(mv_diff && !get_bits1(gb))
...@@ -257,32 +284,41 @@ static int decode_mv_component(GetBitContext *gb, int v){ ...@@ -257,32 +284,41 @@ static int decode_mv_component(GetBitContext *gb, int v){
return v; return v;
} }
static int h261_decode_mb(H261Context *h, static int h261_decode_mb(H261Context *h){
DCTELEM block[6][64])
{
MpegEncContext * const s = &h->s; MpegEncContext * const s = &h->s;
int i, cbp, xy, old_mtype; int i, cbp, xy, old_mtype;
cbp = 63; cbp = 63;
// Read mba // Read mba
do{ do{
h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2)+1; h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
/* Check for slice end */
/* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
if (h->mba_diff == MBA_STARTCODE){ // start code
h->gob_start_code_skipped = 1;
return SLICE_END;
}
} }
while( h->mba_diff == MAX_MBA + 1 ); // stuffing while( h->mba_diff == MBA_STUFFING ); // stuffing
if ( h->mba_diff < 0 ) if ( h->mba_diff < 0 ){
return -1; if ( get_bits_count(&s->gb) + 7 >= s->gb.size_in_bits )
return SLICE_END;
av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
return SLICE_ERROR;
}
h->mba_diff += 1;
h->current_mba += h->mba_diff; h->current_mba += h->mba_diff;
if ( h->current_mba > MAX_MBA ) if ( h->current_mba > MBA_STUFFING )
return -1; return SLICE_ERROR;
s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11); s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11); s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
xy = s->mb_x + s->mb_y * s->mb_stride; xy = s->mb_x + s->mb_y * s->mb_stride;
ff_init_block_index(s); ff_init_block_index(s);
ff_update_block_index(s); ff_update_block_index(s);
s->dsp.clear_blocks(s->block[0]); s->dsp.clear_blocks(s->block[0]);
...@@ -292,9 +328,6 @@ static int h261_decode_mb(H261Context *h, ...@@ -292,9 +328,6 @@ static int h261_decode_mb(H261Context *h,
h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2); h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
h->mtype = h261_mtype_map[h->mtype]; h->mtype = h261_mtype_map[h->mtype];
if (IS_FIL (h->mtype))
h->loop_filter = 1;
// Read mquant // Read mquant
if ( IS_QUANT ( h->mtype ) ){ if ( IS_QUANT ( h->mtype ) ){
ff_set_qscale(s, get_bits(&s->gb, 5)); ff_set_qscale(s, get_bits(&s->gb, 5));
...@@ -348,25 +381,19 @@ intra: ...@@ -348,25 +381,19 @@ intra:
/* decode each block */ /* decode each block */
if(s->mb_intra || HAS_CBP(h->mtype)){ if(s->mb_intra || HAS_CBP(h->mtype)){
for (i = 0; i < 6; i++) { for (i = 0; i < 6; i++) {
if (h261_decode_block(h, block[i], i, cbp&32) < 0){ if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
return -1; return SLICE_ERROR;
} }
cbp+=cbp; cbp+=cbp;
} }
} }
/* per-MB end of slice check */ MPV_decode_mb(s, s->block);
{
int v= show_bits(&s->gb, 15);
if(get_bits_count(&s->gb) + 15 > s->gb.size_in_bits){ if(IS_FIL (h->mtype)){
v>>= get_bits_count(&s->gb) + 15 - s->gb.size_in_bits; ff_h261_loop_filter(h);
} }
if(v==0){
return SLICE_END;
}
}
return SLICE_OK; return SLICE_OK;
} }
...@@ -459,7 +486,6 @@ static int h261_decode_block(H261Context * h, DCTELEM * block, ...@@ -459,7 +486,6 @@ static int h261_decode_block(H261Context * h, DCTELEM * block,
int h261_decode_picture_header(H261Context *h){ int h261_decode_picture_header(H261Context *h){
MpegEncContext * const s = &h->s; MpegEncContext * const s = &h->s;
int format, i; int format, i;
static int h261_framecounter = 0;
uint32_t startcode; uint32_t startcode;
align_get_bits(&s->gb); align_get_bits(&s->gb);
...@@ -510,13 +536,9 @@ int h261_decode_picture_header(H261Context *h){ ...@@ -510,13 +536,9 @@ int h261_decode_picture_header(H261Context *h){
skip_bits(&s->gb, 8); skip_bits(&s->gb, 8);
} }
//h261 has no I-FRAMES, pass the test in MPV_frame_start in mpegvideo.c // h261 has no I-FRAMES, but if we pass I_TYPE for the first frame, the codec crashes if it does
if(h261_framecounter > 1) // not contain all I-blocks (e.g. when a packet is lost)
s->pict_type = P_TYPE; s->pict_type = P_TYPE;
else
s->pict_type = I_TYPE;
h261_framecounter++;
h->gob_number = 0; h->gob_number = 0;
return 0; return 0;
...@@ -524,52 +546,24 @@ int h261_decode_picture_header(H261Context *h){ ...@@ -524,52 +546,24 @@ int h261_decode_picture_header(H261Context *h){
static int h261_decode_gob(H261Context *h){ static int h261_decode_gob(H261Context *h){
MpegEncContext * const s = &h->s; MpegEncContext * const s = &h->s;
int v;
ff_set_qscale(s, s->qscale); ff_set_qscale(s, s->qscale);
/* check for empty gob */
v= show_bits(&s->gb, 15);
if(get_bits_count(&s->gb) + 15 > s->gb.size_in_bits){
v>>= get_bits_count(&s->gb) + 15 - s->gb.size_in_bits;
}
if(v==0){
h261_decode_mb_skipped(h, 0, 33);
return 0;
}
/* decode mb's */ /* decode mb's */
while(h->current_mba <= MAX_MBA) while(h->current_mba <= MBA_STUFFING)
{ {
int ret; int ret;
/* DCT & quantize */ /* DCT & quantize */
ret= h261_decode_mb(h, s->block); ret= h261_decode_mb(h);
if(ret<0){ if(ret<0){
const int xy= s->mb_x + s->mb_y*s->mb_stride;
if(ret==SLICE_END){ if(ret==SLICE_END){
MPV_decode_mb(s, s->block);
if(h->loop_filter){
ff_h261_loop_filter(h);
}
h->loop_filter = 0;
h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
h261_decode_mb_skipped(h, h->current_mba, 33); h261_decode_mb_skipped(h, h->current_mba, 33);
return 0; return 0;
}else if(ret==SLICE_NOEND){
av_log(s->avctx, AV_LOG_ERROR, "Slice mismatch at MB: %d\n", xy);
return -1;
} }
av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy); av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
return -1; return -1;
} }
MPV_decode_mb(s, s->block);
if(h->loop_filter){
ff_h261_loop_filter(h);
}
h->loop_filter = 0;
h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1); h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
} }
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#define MB_TYPE_H261_FIL 0x800000 #define MB_TYPE_H261_FIL 0x800000
// H.261 VLC table for macroblock addressing // H.261 VLC table for macroblock addressing
const uint8_t h261_mba_code[34] = { const uint8_t h261_mba_code[35] = {
1, 3, 2, 3, 1, 3, 2, 3,
2, 3, 2, 7, 2, 3, 2, 7,
6, 11, 10, 9, 6, 11, 10, 9,
...@@ -15,10 +15,11 @@ const uint8_t h261_mba_code[34] = { ...@@ -15,10 +15,11 @@ const uint8_t h261_mba_code[34] = {
32, 31, 30, 29, 32, 31, 30, 29,
28, 27, 26, 25, 28, 27, 26, 25,
24, 24,
15 //(MBA stuffing) 15, //(MBA stuffing)
1 //(start code)
}; };
const uint8_t h261_mba_bits[34] = { const uint8_t h261_mba_bits[35] = {
1, 3, 3, 4, 1, 3, 3, 4,
4, 5, 5, 7, 4, 5, 5, 7,
7, 8, 8, 8, 7, 8, 8, 8,
...@@ -28,7 +29,8 @@ const uint8_t h261_mba_bits[34] = { ...@@ -28,7 +29,8 @@ const uint8_t h261_mba_bits[34] = {
11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11,
11, 11,
11 //(MBA stuffing) 11, //(MBA stuffing)
16 //(start code)
}; };
//H.261 VLC table for macroblock type //H.261 VLC table for macroblock type
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment