Originally committed as revision 3457 to svn://svn.ffmpeg.org/ffmpeg/trunk
| ... | ... |
@@ -36,7 +36,8 @@ |
| 36 | 36 |
#define H261_CBP_VLC_BITS 9 |
| 37 | 37 |
#define TCOEFF_VLC_BITS 9 |
| 38 | 38 |
|
| 39 |
-#define MAX_MBA 33 |
|
| 39 |
+#define MBA_STUFFING 33 |
|
| 40 |
+#define MBA_STARTCODE 34 |
|
| 40 | 41 |
#define IS_FIL(a) ((a)&MB_TYPE_H261_FIL) |
| 41 | 42 |
|
| 42 | 43 |
/** |
| ... | ... |
@@ -51,9 +52,9 @@ typedef struct H261Context{
|
| 51 | 51 |
int current_mv_x; |
| 52 | 52 |
int current_mv_y; |
| 53 | 53 |
int gob_number; |
| 54 |
- int loop_filter; |
|
| 55 | 54 |
int bits_left; //8 - nr of bits left of the following frame in the last byte in this frame |
| 56 | 55 |
int last_bits; //bits left of the following frame in the last byte in this frame |
| 56 |
+ int gob_start_code_skipped; // 1 if gob start code is already read before gob header is read |
|
| 57 | 57 |
}H261Context; |
| 58 | 58 |
|
| 59 | 59 |
void ff_h261_loop_filter(H261Context * h){
|
| ... | ... |
@@ -74,8 +75,7 @@ void ff_h261_loop_filter(H261Context * h){
|
| 74 | 74 |
|
| 75 | 75 |
static int h261_decode_block(H261Context *h, DCTELEM *block, |
| 76 | 76 |
int n, int coded); |
| 77 |
-static int h261_decode_mb(H261Context *h, |
|
| 78 |
- DCTELEM block[6][64]); |
|
| 77 |
+static int h261_decode_mb(H261Context *h); |
|
| 79 | 78 |
void ff_set_qscale(MpegEncContext * s, int qscale); |
| 80 | 79 |
|
| 81 | 80 |
/***********************************************/ |
| ... | ... |
@@ -93,7 +93,7 @@ static void h261_decode_init_vlc(H261Context *h){
|
| 93 | 93 |
|
| 94 | 94 |
if(!done){
|
| 95 | 95 |
done = 1; |
| 96 |
- init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 34, |
|
| 96 |
+ init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 35, |
|
| 97 | 97 |
h261_mba_bits, 1, 1, |
| 98 | 98 |
h261_mba_code, 1, 1); |
| 99 | 99 |
init_vlc(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10, |
| ... | ... |
@@ -132,6 +132,7 @@ static int h261_decode_init(AVCodecContext *avctx){
|
| 132 | 132 |
|
| 133 | 133 |
h->bits_left = 0; |
| 134 | 134 |
h->last_bits = 0; |
| 135 |
+ h->gob_start_code_skipped = 0; |
|
| 135 | 136 |
|
| 136 | 137 |
return 0; |
| 137 | 138 |
} |
| ... | ... |
@@ -144,17 +145,31 @@ static int h261_decode_gob_header(H261Context *h){
|
| 144 | 144 |
unsigned int val; |
| 145 | 145 |
MpegEncContext * const s = &h->s; |
| 146 | 146 |
|
| 147 |
- /* Check for GOB Start Code */ |
|
| 148 |
- val = show_bits(&s->gb, 15); |
|
| 149 |
- if(val) |
|
| 150 |
- return -1; |
|
| 147 |
+ if ( !h->gob_start_code_skipped ){
|
|
| 148 |
+ /* Check for GOB Start Code */ |
|
| 149 |
+ val = show_bits(&s->gb, 15); |
|
| 150 |
+ if(val) |
|
| 151 |
+ return -1; |
|
| 152 |
+ |
|
| 153 |
+ /* We have a GBSC */ |
|
| 154 |
+ skip_bits(&s->gb, 16); |
|
| 155 |
+ } |
|
| 151 | 156 |
|
| 152 |
- /* We have a GBSC */ |
|
| 153 |
- skip_bits(&s->gb, 16); |
|
| 157 |
+ h->gob_start_code_skipped = 0; |
|
| 154 | 158 |
|
| 155 | 159 |
h->gob_number = get_bits(&s->gb, 4); /* GN */ |
| 156 | 160 |
s->qscale = get_bits(&s->gb, 5); /* GQUANT */ |
| 157 | 161 |
|
| 162 |
+ /* Check if gob_number is valid */ |
|
| 163 |
+ if (s->mb_height==18){ //cif
|
|
| 164 |
+ if ((h->gob_number<=0) || (h->gob_number>12)) |
|
| 165 |
+ return -1; |
|
| 166 |
+ } |
|
| 167 |
+ else{ //qcif
|
|
| 168 |
+ if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5)) |
|
| 169 |
+ return -1; |
|
| 170 |
+ } |
|
| 171 |
+ |
|
| 158 | 172 |
/* GEI */ |
| 159 | 173 |
while (get_bits1(&s->gb) != 0) {
|
| 160 | 174 |
skip_bits(&s->gb, 8); |
| ... | ... |
@@ -180,27 +195,34 @@ static int ff_h261_resync(H261Context *h){
|
| 180 | 180 |
MpegEncContext * const s = &h->s; |
| 181 | 181 |
int left, ret; |
| 182 | 182 |
|
| 183 |
- if(show_bits(&s->gb, 15)==0){
|
|
| 183 |
+ if ( h->gob_start_code_skipped ){
|
|
| 184 | 184 |
ret= h261_decode_gob_header(h); |
| 185 | 185 |
if(ret>=0) |
| 186 | 186 |
return 0; |
| 187 | 187 |
} |
| 188 |
- //ok, its not where its supposed to be ... |
|
| 189 |
- s->gb= s->last_resync_gb; |
|
| 190 |
- align_get_bits(&s->gb); |
|
| 191 |
- left= s->gb.size_in_bits - get_bits_count(&s->gb); |
|
| 192 |
- |
|
| 193 |
- for(;left>15+1+4+5; left-=8){
|
|
| 188 |
+ else{
|
|
| 194 | 189 |
if(show_bits(&s->gb, 15)==0){
|
| 195 |
- GetBitContext bak= s->gb; |
|
| 196 |
- |
|
| 197 | 190 |
ret= h261_decode_gob_header(h); |
| 198 | 191 |
if(ret>=0) |
| 199 | 192 |
return 0; |
| 193 |
+ } |
|
| 194 |
+ //ok, its not where its supposed to be ... |
|
| 195 |
+ s->gb= s->last_resync_gb; |
|
| 196 |
+ align_get_bits(&s->gb); |
|
| 197 |
+ left= s->gb.size_in_bits - get_bits_count(&s->gb); |
|
| 198 |
+ |
|
| 199 |
+ for(;left>15+1+4+5; left-=8){
|
|
| 200 |
+ if(show_bits(&s->gb, 15)==0){
|
|
| 201 |
+ GetBitContext bak= s->gb; |
|
| 202 |
+ |
|
| 203 |
+ ret= h261_decode_gob_header(h); |
|
| 204 |
+ if(ret>=0) |
|
| 205 |
+ return 0; |
|
| 200 | 206 |
|
| 201 |
- s->gb= bak; |
|
| 207 |
+ s->gb= bak; |
|
| 208 |
+ } |
|
| 209 |
+ skip_bits(&s->gb, 8); |
|
| 202 | 210 |
} |
| 203 |
- skip_bits(&s->gb, 8); |
|
| 204 | 211 |
} |
| 205 | 212 |
|
| 206 | 213 |
return -1; |
| ... | ... |
@@ -245,6 +267,11 @@ static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 ) |
| 245 | 245 |
|
| 246 | 246 |
static int decode_mv_component(GetBitContext *gb, int v){
|
| 247 | 247 |
int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2); |
| 248 |
+ |
|
| 249 |
+ /* check if mv_diff is valid */ |
|
| 250 |
+ if ( mv_diff < 0 ) |
|
| 251 |
+ return v; |
|
| 252 |
+ |
|
| 248 | 253 |
mv_diff = mvmap[mv_diff]; |
| 249 | 254 |
|
| 250 | 255 |
if(mv_diff && !get_bits1(gb)) |
| ... | ... |
@@ -257,32 +284,41 @@ static int decode_mv_component(GetBitContext *gb, int v){
|
| 257 | 257 |
return v; |
| 258 | 258 |
} |
| 259 | 259 |
|
| 260 |
-static int h261_decode_mb(H261Context *h, |
|
| 261 |
- DCTELEM block[6][64]) |
|
| 262 |
-{
|
|
| 260 |
+static int h261_decode_mb(H261Context *h){
|
|
| 263 | 261 |
MpegEncContext * const s = &h->s; |
| 264 | 262 |
int i, cbp, xy, old_mtype; |
| 265 | 263 |
|
| 266 | 264 |
cbp = 63; |
| 267 | 265 |
// Read mba |
| 268 | 266 |
do{
|
| 269 |
- h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2)+1; |
|
| 267 |
+ h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2); |
|
| 268 |
+ |
|
| 269 |
+ /* Check for slice end */ |
|
| 270 |
+ /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */ |
|
| 271 |
+ if (h->mba_diff == MBA_STARTCODE){ // start code
|
|
| 272 |
+ h->gob_start_code_skipped = 1; |
|
| 273 |
+ return SLICE_END; |
|
| 274 |
+ } |
|
| 270 | 275 |
} |
| 271 |
- while( h->mba_diff == MAX_MBA + 1 ); // stuffing |
|
| 276 |
+ while( h->mba_diff == MBA_STUFFING ); // stuffing |
|
| 272 | 277 |
|
| 273 |
- if ( h->mba_diff < 0 ) |
|
| 274 |
- return -1; |
|
| 278 |
+ if ( h->mba_diff < 0 ){
|
|
| 279 |
+ if ( get_bits_count(&s->gb) + 7 >= s->gb.size_in_bits ) |
|
| 280 |
+ return SLICE_END; |
|
| 281 |
+ |
|
| 282 |
+ av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y); |
|
| 283 |
+ return SLICE_ERROR; |
|
| 284 |
+ } |
|
| 275 | 285 |
|
| 286 |
+ h->mba_diff += 1; |
|
| 276 | 287 |
h->current_mba += h->mba_diff; |
| 277 | 288 |
|
| 278 |
- if ( h->current_mba > MAX_MBA ) |
|
| 279 |
- return -1; |
|
| 289 |
+ if ( h->current_mba > MBA_STUFFING ) |
|
| 290 |
+ return SLICE_ERROR; |
|
| 280 | 291 |
|
| 281 | 292 |
s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11); |
| 282 | 293 |
s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11); |
| 283 |
- |
|
| 284 | 294 |
xy = s->mb_x + s->mb_y * s->mb_stride; |
| 285 |
- |
|
| 286 | 295 |
ff_init_block_index(s); |
| 287 | 296 |
ff_update_block_index(s); |
| 288 | 297 |
s->dsp.clear_blocks(s->block[0]); |
| ... | ... |
@@ -292,9 +328,6 @@ static int h261_decode_mb(H261Context *h, |
| 292 | 292 |
h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2); |
| 293 | 293 |
h->mtype = h261_mtype_map[h->mtype]; |
| 294 | 294 |
|
| 295 |
- if (IS_FIL (h->mtype)) |
|
| 296 |
- h->loop_filter = 1; |
|
| 297 |
- |
|
| 298 | 295 |
// Read mquant |
| 299 | 296 |
if ( IS_QUANT ( h->mtype ) ){
|
| 300 | 297 |
ff_set_qscale(s, get_bits(&s->gb, 5)); |
| ... | ... |
@@ -348,25 +381,19 @@ intra: |
| 348 | 348 |
/* decode each block */ |
| 349 | 349 |
if(s->mb_intra || HAS_CBP(h->mtype)){
|
| 350 | 350 |
for (i = 0; i < 6; i++) {
|
| 351 |
- if (h261_decode_block(h, block[i], i, cbp&32) < 0){
|
|
| 352 |
- return -1; |
|
| 351 |
+ if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
|
|
| 352 |
+ return SLICE_ERROR; |
|
| 353 | 353 |
} |
| 354 | 354 |
cbp+=cbp; |
| 355 | 355 |
} |
| 356 | 356 |
} |
| 357 | 357 |
|
| 358 |
- /* per-MB end of slice check */ |
|
| 359 |
- {
|
|
| 360 |
- int v= show_bits(&s->gb, 15); |
|
| 361 |
- |
|
| 362 |
- if(get_bits_count(&s->gb) + 15 > s->gb.size_in_bits){
|
|
| 363 |
- v>>= get_bits_count(&s->gb) + 15 - s->gb.size_in_bits; |
|
| 364 |
- } |
|
| 358 |
+ MPV_decode_mb(s, s->block); |
|
| 365 | 359 |
|
| 366 |
- if(v==0){
|
|
| 367 |
- return SLICE_END; |
|
| 368 |
- } |
|
| 360 |
+ if(IS_FIL (h->mtype)){
|
|
| 361 |
+ ff_h261_loop_filter(h); |
|
| 369 | 362 |
} |
| 363 |
+ |
|
| 370 | 364 |
return SLICE_OK; |
| 371 | 365 |
} |
| 372 | 366 |
|
| ... | ... |
@@ -459,7 +486,6 @@ static int h261_decode_block(H261Context * h, DCTELEM * block, |
| 459 | 459 |
int h261_decode_picture_header(H261Context *h){
|
| 460 | 460 |
MpegEncContext * const s = &h->s; |
| 461 | 461 |
int format, i; |
| 462 |
- static int h261_framecounter = 0; |
|
| 463 | 462 |
uint32_t startcode; |
| 464 | 463 |
align_get_bits(&s->gb); |
| 465 | 464 |
|
| ... | ... |
@@ -510,13 +536,9 @@ int h261_decode_picture_header(H261Context *h){
|
| 510 | 510 |
skip_bits(&s->gb, 8); |
| 511 | 511 |
} |
| 512 | 512 |
|
| 513 |
- //h261 has no I-FRAMES, pass the test in MPV_frame_start in mpegvideo.c |
|
| 514 |
- if(h261_framecounter > 1) |
|
| 515 |
- s->pict_type = P_TYPE; |
|
| 516 |
- else |
|
| 517 |
- s->pict_type = I_TYPE; |
|
| 518 |
- |
|
| 519 |
- h261_framecounter++; |
|
| 513 |
+ // h261 has no I-FRAMES, but if we pass I_TYPE for the first frame, the codec crashes if it does |
|
| 514 |
+ // not contain all I-blocks (e.g. when a packet is lost) |
|
| 515 |
+ s->pict_type = P_TYPE; |
|
| 520 | 516 |
|
| 521 | 517 |
h->gob_number = 0; |
| 522 | 518 |
return 0; |
| ... | ... |
@@ -524,52 +546,24 @@ int h261_decode_picture_header(H261Context *h){
|
| 524 | 524 |
|
| 525 | 525 |
static int h261_decode_gob(H261Context *h){
|
| 526 | 526 |
MpegEncContext * const s = &h->s; |
| 527 |
- int v; |
|
| 528 | 527 |
|
| 529 | 528 |
ff_set_qscale(s, s->qscale); |
| 530 | 529 |
|
| 531 |
- /* check for empty gob */ |
|
| 532 |
- v= show_bits(&s->gb, 15); |
|
| 533 |
- |
|
| 534 |
- if(get_bits_count(&s->gb) + 15 > s->gb.size_in_bits){
|
|
| 535 |
- v>>= get_bits_count(&s->gb) + 15 - s->gb.size_in_bits; |
|
| 536 |
- } |
|
| 537 |
- |
|
| 538 |
- if(v==0){
|
|
| 539 |
- h261_decode_mb_skipped(h, 0, 33); |
|
| 540 |
- return 0; |
|
| 541 |
- } |
|
| 542 |
- |
|
| 543 | 530 |
/* decode mb's */ |
| 544 |
- while(h->current_mba <= MAX_MBA) |
|
| 531 |
+ while(h->current_mba <= MBA_STUFFING) |
|
| 545 | 532 |
{
|
| 546 | 533 |
int ret; |
| 547 | 534 |
/* DCT & quantize */ |
| 548 |
- ret= h261_decode_mb(h, s->block); |
|
| 535 |
+ ret= h261_decode_mb(h); |
|
| 549 | 536 |
if(ret<0){
|
| 550 |
- const int xy= s->mb_x + s->mb_y*s->mb_stride; |
|
| 551 | 537 |
if(ret==SLICE_END){
|
| 552 |
- MPV_decode_mb(s, s->block); |
|
| 553 |
- if(h->loop_filter){
|
|
| 554 |
- ff_h261_loop_filter(h); |
|
| 555 |
- } |
|
| 556 |
- h->loop_filter = 0; |
|
| 557 |
- h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1); |
|
| 558 | 538 |
h261_decode_mb_skipped(h, h->current_mba, 33); |
| 559 | 539 |
return 0; |
| 560 |
- }else if(ret==SLICE_NOEND){
|
|
| 561 |
- av_log(s->avctx, AV_LOG_ERROR, "Slice mismatch at MB: %d\n", xy); |
|
| 562 |
- return -1; |
|
| 563 | 540 |
} |
| 564 |
- av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy); |
|
| 541 |
+ av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride); |
|
| 565 | 542 |
return -1; |
| 566 | 543 |
} |
| 567 |
- MPV_decode_mb(s, s->block); |
|
| 568 |
- if(h->loop_filter){
|
|
| 569 |
- ff_h261_loop_filter(h); |
|
| 570 |
- } |
|
| 571 |
- |
|
| 572 |
- h->loop_filter = 0; |
|
| 544 |
+ |
|
| 573 | 545 |
h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1); |
| 574 | 546 |
} |
| 575 | 547 |
|
| ... | ... |
@@ -5,7 +5,7 @@ |
| 5 | 5 |
#define MB_TYPE_H261_FIL 0x800000 |
| 6 | 6 |
|
| 7 | 7 |
// H.261 VLC table for macroblock addressing |
| 8 |
-const uint8_t h261_mba_code[34] = {
|
|
| 8 |
+const uint8_t h261_mba_code[35] = {
|
|
| 9 | 9 |
1, 3, 2, 3, |
| 10 | 10 |
2, 3, 2, 7, |
| 11 | 11 |
6, 11, 10, 9, |
| ... | ... |
@@ -15,10 +15,11 @@ const uint8_t h261_mba_code[34] = {
|
| 15 | 15 |
32, 31, 30, 29, |
| 16 | 16 |
28, 27, 26, 25, |
| 17 | 17 |
24, |
| 18 |
- 15 //(MBA stuffing) |
|
| 18 |
+ 15, //(MBA stuffing) |
|
| 19 |
+ 1 //(start code) |
|
| 19 | 20 |
}; |
| 20 | 21 |
|
| 21 |
-const uint8_t h261_mba_bits[34] = {
|
|
| 22 |
+const uint8_t h261_mba_bits[35] = {
|
|
| 22 | 23 |
1, 3, 3, 4, |
| 23 | 24 |
4, 5, 5, 7, |
| 24 | 25 |
7, 8, 8, 8, |
| ... | ... |
@@ -28,7 +29,8 @@ const uint8_t h261_mba_bits[34] = {
|
| 28 | 28 |
11, 11, 11, 11, |
| 29 | 29 |
11, 11, 11, 11, |
| 30 | 30 |
11, |
| 31 |
- 11 //(MBA stuffing) |
|
| 31 |
+ 11, //(MBA stuffing) |
|
| 32 |
+ 16 //(start code) |
|
| 32 | 33 |
}; |
| 33 | 34 |
|
| 34 | 35 |
//H.261 VLC table for macroblock type |