Mercurial > libavcodec.hg
comparison h261.c @ 2225:35fc37209ea0 libavcodec
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>)
author | michael |
---|---|
date | Mon, 13 Sep 2004 03:27:53 +0000 |
parents | 8eae8e652584 |
children | 514949de5d15 |
comparison
equal
deleted
inserted
replaced
2224:11d54cb7ac4e | 2225:35fc37209ea0 |
---|---|
34 #define H261_MTYPE_VLC_BITS 6 | 34 #define H261_MTYPE_VLC_BITS 6 |
35 #define H261_MV_VLC_BITS 7 | 35 #define H261_MV_VLC_BITS 7 |
36 #define H261_CBP_VLC_BITS 9 | 36 #define H261_CBP_VLC_BITS 9 |
37 #define TCOEFF_VLC_BITS 9 | 37 #define TCOEFF_VLC_BITS 9 |
38 | 38 |
39 #define MAX_MBA 33 | 39 #define MBA_STUFFING 33 |
40 #define MBA_STARTCODE 34 | |
40 #define IS_FIL(a) ((a)&MB_TYPE_H261_FIL) | 41 #define IS_FIL(a) ((a)&MB_TYPE_H261_FIL) |
41 | 42 |
42 /** | 43 /** |
43 * H261Context | 44 * H261Context |
44 */ | 45 */ |
49 int mba_diff; | 50 int mba_diff; |
50 int mtype; | 51 int mtype; |
51 int current_mv_x; | 52 int current_mv_x; |
52 int current_mv_y; | 53 int current_mv_y; |
53 int gob_number; | 54 int gob_number; |
54 int loop_filter; | |
55 int bits_left; //8 - nr of bits left of the following frame in the last byte in this frame | 55 int bits_left; //8 - nr of bits left of the following frame in the last byte in this frame |
56 int last_bits; //bits left of the following frame in the last byte in this frame | 56 int last_bits; //bits left of the following frame in the last byte in this frame |
57 int gob_start_code_skipped; // 1 if gob start code is already read before gob header is read | |
57 }H261Context; | 58 }H261Context; |
58 | 59 |
59 void ff_h261_loop_filter(H261Context * h){ | 60 void ff_h261_loop_filter(H261Context * h){ |
60 MpegEncContext * const s = &h->s; | 61 MpegEncContext * const s = &h->s; |
61 const int linesize = s->linesize; | 62 const int linesize = s->linesize; |
72 s->dsp.h261_loop_filter(dest_cr, uvlinesize); | 73 s->dsp.h261_loop_filter(dest_cr, uvlinesize); |
73 } | 74 } |
74 | 75 |
75 static int h261_decode_block(H261Context *h, DCTELEM *block, | 76 static int h261_decode_block(H261Context *h, DCTELEM *block, |
76 int n, int coded); | 77 int n, int coded); |
77 static int h261_decode_mb(H261Context *h, | 78 static int h261_decode_mb(H261Context *h); |
78 DCTELEM block[6][64]); | |
79 void ff_set_qscale(MpegEncContext * s, int qscale); | 79 void ff_set_qscale(MpegEncContext * s, int qscale); |
80 | 80 |
81 /***********************************************/ | 81 /***********************************************/ |
82 /* decoding */ | 82 /* decoding */ |
83 | 83 |
91 static void h261_decode_init_vlc(H261Context *h){ | 91 static void h261_decode_init_vlc(H261Context *h){ |
92 static int done = 0; | 92 static int done = 0; |
93 | 93 |
94 if(!done){ | 94 if(!done){ |
95 done = 1; | 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 h261_mba_bits, 1, 1, | 97 h261_mba_bits, 1, 1, |
98 h261_mba_code, 1, 1); | 98 h261_mba_code, 1, 1); |
99 init_vlc(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10, | 99 init_vlc(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10, |
100 h261_mtype_bits, 1, 1, | 100 h261_mtype_bits, 1, 1, |
101 h261_mtype_code, 1, 1); | 101 h261_mtype_code, 1, 1); |
130 | 130 |
131 h261_decode_init_vlc(h); | 131 h261_decode_init_vlc(h); |
132 | 132 |
133 h->bits_left = 0; | 133 h->bits_left = 0; |
134 h->last_bits = 0; | 134 h->last_bits = 0; |
135 h->gob_start_code_skipped = 0; | |
135 | 136 |
136 return 0; | 137 return 0; |
137 } | 138 } |
138 | 139 |
139 /** | 140 /** |
142 */ | 143 */ |
143 static int h261_decode_gob_header(H261Context *h){ | 144 static int h261_decode_gob_header(H261Context *h){ |
144 unsigned int val; | 145 unsigned int val; |
145 MpegEncContext * const s = &h->s; | 146 MpegEncContext * const s = &h->s; |
146 | 147 |
147 /* Check for GOB Start Code */ | 148 if ( !h->gob_start_code_skipped ){ |
148 val = show_bits(&s->gb, 15); | 149 /* Check for GOB Start Code */ |
149 if(val) | 150 val = show_bits(&s->gb, 15); |
150 return -1; | 151 if(val) |
151 | 152 return -1; |
152 /* We have a GBSC */ | 153 |
153 skip_bits(&s->gb, 16); | 154 /* We have a GBSC */ |
155 skip_bits(&s->gb, 16); | |
156 } | |
157 | |
158 h->gob_start_code_skipped = 0; | |
154 | 159 |
155 h->gob_number = get_bits(&s->gb, 4); /* GN */ | 160 h->gob_number = get_bits(&s->gb, 4); /* GN */ |
156 s->qscale = get_bits(&s->gb, 5); /* GQUANT */ | 161 s->qscale = get_bits(&s->gb, 5); /* GQUANT */ |
162 | |
163 /* Check if gob_number is valid */ | |
164 if (s->mb_height==18){ //cif | |
165 if ((h->gob_number<=0) || (h->gob_number>12)) | |
166 return -1; | |
167 } | |
168 else{ //qcif | |
169 if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5)) | |
170 return -1; | |
171 } | |
157 | 172 |
158 /* GEI */ | 173 /* GEI */ |
159 while (get_bits1(&s->gb) != 0) { | 174 while (get_bits1(&s->gb) != 0) { |
160 skip_bits(&s->gb, 8); | 175 skip_bits(&s->gb, 8); |
161 } | 176 } |
178 */ | 193 */ |
179 static int ff_h261_resync(H261Context *h){ | 194 static int ff_h261_resync(H261Context *h){ |
180 MpegEncContext * const s = &h->s; | 195 MpegEncContext * const s = &h->s; |
181 int left, ret; | 196 int left, ret; |
182 | 197 |
183 if(show_bits(&s->gb, 15)==0){ | 198 if ( h->gob_start_code_skipped ){ |
184 ret= h261_decode_gob_header(h); | 199 ret= h261_decode_gob_header(h); |
185 if(ret>=0) | 200 if(ret>=0) |
186 return 0; | 201 return 0; |
187 } | 202 } |
188 //ok, its not where its supposed to be ... | 203 else{ |
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){ | |
194 if(show_bits(&s->gb, 15)==0){ | 204 if(show_bits(&s->gb, 15)==0){ |
195 GetBitContext bak= s->gb; | |
196 | |
197 ret= h261_decode_gob_header(h); | 205 ret= h261_decode_gob_header(h); |
198 if(ret>=0) | 206 if(ret>=0) |
199 return 0; | 207 return 0; |
200 | 208 } |
201 s->gb= bak; | 209 //ok, its not where its supposed to be ... |
202 } | 210 s->gb= s->last_resync_gb; |
203 skip_bits(&s->gb, 8); | 211 align_get_bits(&s->gb); |
212 left= s->gb.size_in_bits - get_bits_count(&s->gb); | |
213 | |
214 for(;left>15+1+4+5; left-=8){ | |
215 if(show_bits(&s->gb, 15)==0){ | |
216 GetBitContext bak= s->gb; | |
217 | |
218 ret= h261_decode_gob_header(h); | |
219 if(ret>=0) | |
220 return 0; | |
221 | |
222 s->gb= bak; | |
223 } | |
224 skip_bits(&s->gb, 8); | |
225 } | |
204 } | 226 } |
205 | 227 |
206 return -1; | 228 return -1; |
207 } | 229 } |
208 | 230 |
243 return 0; | 265 return 0; |
244 } | 266 } |
245 | 267 |
246 static int decode_mv_component(GetBitContext *gb, int v){ | 268 static int decode_mv_component(GetBitContext *gb, int v){ |
247 int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2); | 269 int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2); |
270 | |
271 /* check if mv_diff is valid */ | |
272 if ( mv_diff < 0 ) | |
273 return v; | |
274 | |
248 mv_diff = mvmap[mv_diff]; | 275 mv_diff = mvmap[mv_diff]; |
249 | 276 |
250 if(mv_diff && !get_bits1(gb)) | 277 if(mv_diff && !get_bits1(gb)) |
251 mv_diff= -mv_diff; | 278 mv_diff= -mv_diff; |
252 | 279 |
255 else if(v >= 16) v-= 32; | 282 else if(v >= 16) v-= 32; |
256 | 283 |
257 return v; | 284 return v; |
258 } | 285 } |
259 | 286 |
260 static int h261_decode_mb(H261Context *h, | 287 static int h261_decode_mb(H261Context *h){ |
261 DCTELEM block[6][64]) | |
262 { | |
263 MpegEncContext * const s = &h->s; | 288 MpegEncContext * const s = &h->s; |
264 int i, cbp, xy, old_mtype; | 289 int i, cbp, xy, old_mtype; |
265 | 290 |
266 cbp = 63; | 291 cbp = 63; |
267 // Read mba | 292 // Read mba |
268 do{ | 293 do{ |
269 h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2)+1; | 294 h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2); |
270 } | 295 |
271 while( h->mba_diff == MAX_MBA + 1 ); // stuffing | 296 /* Check for slice end */ |
272 | 297 /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */ |
273 if ( h->mba_diff < 0 ) | 298 if (h->mba_diff == MBA_STARTCODE){ // start code |
274 return -1; | 299 h->gob_start_code_skipped = 1; |
275 | 300 return SLICE_END; |
301 } | |
302 } | |
303 while( h->mba_diff == MBA_STUFFING ); // stuffing | |
304 | |
305 if ( h->mba_diff < 0 ){ | |
306 if ( get_bits_count(&s->gb) + 7 >= s->gb.size_in_bits ) | |
307 return SLICE_END; | |
308 | |
309 av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y); | |
310 return SLICE_ERROR; | |
311 } | |
312 | |
313 h->mba_diff += 1; | |
276 h->current_mba += h->mba_diff; | 314 h->current_mba += h->mba_diff; |
277 | 315 |
278 if ( h->current_mba > MAX_MBA ) | 316 if ( h->current_mba > MBA_STUFFING ) |
279 return -1; | 317 return SLICE_ERROR; |
280 | 318 |
281 s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11); | 319 s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11); |
282 s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11); | 320 s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11); |
283 | |
284 xy = s->mb_x + s->mb_y * s->mb_stride; | 321 xy = s->mb_x + s->mb_y * s->mb_stride; |
285 | |
286 ff_init_block_index(s); | 322 ff_init_block_index(s); |
287 ff_update_block_index(s); | 323 ff_update_block_index(s); |
288 s->dsp.clear_blocks(s->block[0]); | 324 s->dsp.clear_blocks(s->block[0]); |
289 | 325 |
290 // Read mtype | 326 // Read mtype |
291 old_mtype = h->mtype; | 327 old_mtype = h->mtype; |
292 h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2); | 328 h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2); |
293 h->mtype = h261_mtype_map[h->mtype]; | 329 h->mtype = h261_mtype_map[h->mtype]; |
294 | |
295 if (IS_FIL (h->mtype)) | |
296 h->loop_filter = 1; | |
297 | 330 |
298 // Read mquant | 331 // Read mquant |
299 if ( IS_QUANT ( h->mtype ) ){ | 332 if ( IS_QUANT ( h->mtype ) ){ |
300 ff_set_qscale(s, get_bits(&s->gb, 5)); | 333 ff_set_qscale(s, get_bits(&s->gb, 5)); |
301 } | 334 } |
346 | 379 |
347 intra: | 380 intra: |
348 /* decode each block */ | 381 /* decode each block */ |
349 if(s->mb_intra || HAS_CBP(h->mtype)){ | 382 if(s->mb_intra || HAS_CBP(h->mtype)){ |
350 for (i = 0; i < 6; i++) { | 383 for (i = 0; i < 6; i++) { |
351 if (h261_decode_block(h, block[i], i, cbp&32) < 0){ | 384 if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){ |
352 return -1; | 385 return SLICE_ERROR; |
353 } | 386 } |
354 cbp+=cbp; | 387 cbp+=cbp; |
355 } | 388 } |
356 } | 389 } |
357 | 390 |
358 /* per-MB end of slice check */ | 391 MPV_decode_mb(s, s->block); |
359 { | 392 |
360 int v= show_bits(&s->gb, 15); | 393 if(IS_FIL (h->mtype)){ |
361 | 394 ff_h261_loop_filter(h); |
362 if(get_bits_count(&s->gb) + 15 > s->gb.size_in_bits){ | 395 } |
363 v>>= get_bits_count(&s->gb) + 15 - s->gb.size_in_bits; | 396 |
364 } | |
365 | |
366 if(v==0){ | |
367 return SLICE_END; | |
368 } | |
369 } | |
370 return SLICE_OK; | 397 return SLICE_OK; |
371 } | 398 } |
372 | 399 |
373 /** | 400 /** |
374 * decodes a macroblock | 401 * decodes a macroblock |
457 * @return <0 if no startcode found | 484 * @return <0 if no startcode found |
458 */ | 485 */ |
459 int h261_decode_picture_header(H261Context *h){ | 486 int h261_decode_picture_header(H261Context *h){ |
460 MpegEncContext * const s = &h->s; | 487 MpegEncContext * const s = &h->s; |
461 int format, i; | 488 int format, i; |
462 static int h261_framecounter = 0; | |
463 uint32_t startcode; | 489 uint32_t startcode; |
464 align_get_bits(&s->gb); | 490 align_get_bits(&s->gb); |
465 | 491 |
466 startcode = (h->last_bits << (12 - (8-h->bits_left))) | get_bits(&s->gb, 20-8 - (8- h->bits_left)); | 492 startcode = (h->last_bits << (12 - (8-h->bits_left))) | get_bits(&s->gb, 20-8 - (8- h->bits_left)); |
467 | 493 |
508 /* PEI */ | 534 /* PEI */ |
509 while (get_bits1(&s->gb) != 0){ | 535 while (get_bits1(&s->gb) != 0){ |
510 skip_bits(&s->gb, 8); | 536 skip_bits(&s->gb, 8); |
511 } | 537 } |
512 | 538 |
513 //h261 has no I-FRAMES, pass the test in MPV_frame_start in mpegvideo.c | 539 // h261 has no I-FRAMES, but if we pass I_TYPE for the first frame, the codec crashes if it does |
514 if(h261_framecounter > 1) | 540 // not contain all I-blocks (e.g. when a packet is lost) |
515 s->pict_type = P_TYPE; | 541 s->pict_type = P_TYPE; |
516 else | |
517 s->pict_type = I_TYPE; | |
518 | |
519 h261_framecounter++; | |
520 | 542 |
521 h->gob_number = 0; | 543 h->gob_number = 0; |
522 return 0; | 544 return 0; |
523 } | 545 } |
524 | 546 |
525 static int h261_decode_gob(H261Context *h){ | 547 static int h261_decode_gob(H261Context *h){ |
526 MpegEncContext * const s = &h->s; | 548 MpegEncContext * const s = &h->s; |
527 int v; | |
528 | 549 |
529 ff_set_qscale(s, s->qscale); | 550 ff_set_qscale(s, s->qscale); |
530 | 551 |
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 /* decode mb's */ | 552 /* decode mb's */ |
544 while(h->current_mba <= MAX_MBA) | 553 while(h->current_mba <= MBA_STUFFING) |
545 { | 554 { |
546 int ret; | 555 int ret; |
547 /* DCT & quantize */ | 556 /* DCT & quantize */ |
548 ret= h261_decode_mb(h, s->block); | 557 ret= h261_decode_mb(h); |
549 if(ret<0){ | 558 if(ret<0){ |
550 const int xy= s->mb_x + s->mb_y*s->mb_stride; | |
551 if(ret==SLICE_END){ | 559 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 h261_decode_mb_skipped(h, h->current_mba, 33); | 560 h261_decode_mb_skipped(h, h->current_mba, 33); |
559 return 0; | 561 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 } | 562 } |
564 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy); | 563 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride); |
565 return -1; | 564 return -1; |
566 } | 565 } |
567 MPV_decode_mb(s, s->block); | 566 |
568 if(h->loop_filter){ | |
569 ff_h261_loop_filter(h); | |
570 } | |
571 | |
572 h->loop_filter = 0; | |
573 h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1); | 567 h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1); |
574 } | 568 } |
575 | 569 |
576 return -1; | 570 return -1; |
577 } | 571 } |