lavc decoders: work with refcounted frames.
[libav.git] / libavcodec / h261dec.c
1 /*
2 * H261 decoder
3 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
4 * Copyright (c) 2004 Maarten Daniels
5 *
6 * This file is part of Libav.
7 *
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * H.261 decoder.
26 */
27
28 #include "avcodec.h"
29 #include "mpegvideo.h"
30 #include "h263.h"
31 #include "h261.h"
32 #include "h261data.h"
33
34 #define H261_MBA_VLC_BITS 9
35 #define H261_MTYPE_VLC_BITS 6
36 #define H261_MV_VLC_BITS 7
37 #define H261_CBP_VLC_BITS 9
38 #define TCOEFF_VLC_BITS 9
39 #define MBA_STUFFING 33
40 #define MBA_STARTCODE 34
41
42 extern uint8_t ff_h261_rl_table_store[2][2*MAX_RUN + MAX_LEVEL + 3];
43
44 static VLC h261_mba_vlc;
45 static VLC h261_mtype_vlc;
46 static VLC h261_mv_vlc;
47 static VLC h261_cbp_vlc;
48
49 static int h261_decode_block(H261Context * h, int16_t * block, int n, int coded);
50
51 static av_cold void h261_decode_init_vlc(H261Context *h){
52 static int done = 0;
53
54 if(!done){
55 done = 1;
56 INIT_VLC_STATIC(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
57 h261_mba_bits, 1, 1,
58 h261_mba_code, 1, 1, 662);
59 INIT_VLC_STATIC(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
60 h261_mtype_bits, 1, 1,
61 h261_mtype_code, 1, 1, 80);
62 INIT_VLC_STATIC(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
63 &h261_mv_tab[0][1], 2, 1,
64 &h261_mv_tab[0][0], 2, 1, 144);
65 INIT_VLC_STATIC(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
66 &h261_cbp_tab[0][1], 2, 1,
67 &h261_cbp_tab[0][0], 2, 1, 512);
68 ff_init_rl(&h261_rl_tcoeff, ff_h261_rl_table_store);
69 INIT_VLC_RL(h261_rl_tcoeff, 552);
70 }
71 }
72
73 static av_cold int h261_decode_init(AVCodecContext *avctx){
74 H261Context *h= avctx->priv_data;
75 MpegEncContext * const s = &h->s;
76
77 // set defaults
78 ff_MPV_decode_defaults(s);
79 s->avctx = avctx;
80
81 s->width = s->avctx->coded_width;
82 s->height = s->avctx->coded_height;
83 s->codec_id = s->avctx->codec->id;
84
85 s->out_format = FMT_H261;
86 s->low_delay= 1;
87 avctx->pix_fmt= AV_PIX_FMT_YUV420P;
88
89 s->codec_id= avctx->codec->id;
90
91 h261_decode_init_vlc(h);
92
93 h->gob_start_code_skipped = 0;
94
95 return 0;
96 }
97
98 /**
99 * Decode the group of blocks header or slice header.
100 * @return <0 if an error occurred
101 */
102 static int h261_decode_gob_header(H261Context *h){
103 unsigned int val;
104 MpegEncContext * const s = &h->s;
105
106 if ( !h->gob_start_code_skipped ){
107 /* Check for GOB Start Code */
108 val = show_bits(&s->gb, 15);
109 if(val)
110 return -1;
111
112 /* We have a GBSC */
113 skip_bits(&s->gb, 16);
114 }
115
116 h->gob_start_code_skipped = 0;
117
118 h->gob_number = get_bits(&s->gb, 4); /* GN */
119 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
120
121 /* Check if gob_number is valid */
122 if (s->mb_height==18){ //cif
123 if ((h->gob_number<=0) || (h->gob_number>12))
124 return -1;
125 }
126 else{ //qcif
127 if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5))
128 return -1;
129 }
130
131 /* GEI */
132 while (get_bits1(&s->gb) != 0) {
133 skip_bits(&s->gb, 8);
134 }
135
136 if(s->qscale==0) {
137 av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\n");
138 if (s->avctx->err_recognition & AV_EF_BITSTREAM)
139 return -1;
140 }
141
142 // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
143 // subsequent macroblocks, MBA is the difference between the absolute addresses of
144 // the macroblock and the last transmitted macroblock.
145 h->current_mba = 0;
146 h->mba_diff = 0;
147
148 return 0;
149 }
150
151 /**
152 * Decode the group of blocks / video packet header.
153 * @return <0 if no resync found
154 */
155 static int ff_h261_resync(H261Context *h){
156 MpegEncContext * const s = &h->s;
157 int left, ret;
158
159 if ( h->gob_start_code_skipped ){
160 ret= h261_decode_gob_header(h);
161 if(ret>=0)
162 return 0;
163 }
164 else{
165 if(show_bits(&s->gb, 15)==0){
166 ret= h261_decode_gob_header(h);
167 if(ret>=0)
168 return 0;
169 }
170 //OK, it is not where it is supposed to be ...
171 s->gb= s->last_resync_gb;
172 align_get_bits(&s->gb);
173 left= get_bits_left(&s->gb);
174
175 for(;left>15+1+4+5; left-=8){
176 if(show_bits(&s->gb, 15)==0){
177 GetBitContext bak= s->gb;
178
179 ret= h261_decode_gob_header(h);
180 if(ret>=0)
181 return 0;
182
183 s->gb= bak;
184 }
185 skip_bits(&s->gb, 8);
186 }
187 }
188
189 return -1;
190 }
191
192 /**
193 * Decode skipped macroblocks.
194 * @return 0
195 */
196 static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
197 {
198 MpegEncContext * const s = &h->s;
199 int i;
200
201 s->mb_intra = 0;
202
203 for(i=mba1; i<mba2; i++){
204 int j, xy;
205
206 s->mb_x= ((h->gob_number-1) % 2) * 11 + i % 11;
207 s->mb_y= ((h->gob_number-1) / 2) * 3 + i / 11;
208 xy = s->mb_x + s->mb_y * s->mb_stride;
209 ff_init_block_index(s);
210 ff_update_block_index(s);
211
212 for(j=0;j<6;j++)
213 s->block_last_index[j] = -1;
214
215 s->mv_dir = MV_DIR_FORWARD;
216 s->mv_type = MV_TYPE_16X16;
217 s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
218 s->mv[0][0][0] = 0;
219 s->mv[0][0][1] = 0;
220 s->mb_skipped = 1;
221 h->mtype &= ~MB_TYPE_H261_FIL;
222
223 ff_MPV_decode_mb(s, s->block);
224 }
225
226 return 0;
227 }
228
229 static int decode_mv_component(GetBitContext *gb, int v){
230 int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
231
232 /* check if mv_diff is valid */
233 if ( mv_diff < 0 )
234 return v;
235
236 mv_diff = mvmap[mv_diff];
237
238 if(mv_diff && !get_bits1(gb))
239 mv_diff= -mv_diff;
240
241 v += mv_diff;
242 if (v <=-16) v+= 32;
243 else if(v >= 16) v-= 32;
244
245 return v;
246 }
247
248 static int h261_decode_mb(H261Context *h){
249 MpegEncContext * const s = &h->s;
250 int i, cbp, xy;
251
252 cbp = 63;
253 // Read mba
254 do{
255 h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
256
257 /* Check for slice end */
258 /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
259 if (h->mba_diff == MBA_STARTCODE){ // start code
260 h->gob_start_code_skipped = 1;
261 return SLICE_END;
262 }
263 }
264 while( h->mba_diff == MBA_STUFFING ); // stuffing
265
266 if ( h->mba_diff < 0 ){
267 if (get_bits_left(&s->gb) <= 7)
268 return SLICE_END;
269
270 av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
271 return SLICE_ERROR;
272 }
273
274 h->mba_diff += 1;
275 h->current_mba += h->mba_diff;
276
277 if ( h->current_mba > MBA_STUFFING )
278 return SLICE_ERROR;
279
280 s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
281 s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
282 xy = s->mb_x + s->mb_y * s->mb_stride;
283 ff_init_block_index(s);
284 ff_update_block_index(s);
285
286 // Read mtype
287 h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
288 h->mtype = h261_mtype_map[h->mtype];
289
290 // Read mquant
291 if ( IS_QUANT ( h->mtype ) ){
292 ff_set_qscale(s, get_bits(&s->gb, 5));
293 }
294
295 s->mb_intra = IS_INTRA4x4(h->mtype);
296
297 // Read mv
298 if ( IS_16X16 ( h->mtype ) ){
299 // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
300 // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
301 // following three situations:
302 // 1) evaluating MVD for macroblocks 1, 12 and 23;
303 // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
304 // 3) MTYPE of the previous macroblock was not MC.
305 if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
306 ( h->mba_diff != 1))
307 {
308 h->current_mv_x = 0;
309 h->current_mv_y = 0;
310 }
311
312 h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
313 h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
314 }else{
315 h->current_mv_x = 0;
316 h->current_mv_y = 0;
317 }
318
319 // Read cbp
320 if ( HAS_CBP( h->mtype ) ){
321 cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
322 }
323
324 if(s->mb_intra){
325 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
326 goto intra;
327 }
328
329 //set motion vectors
330 s->mv_dir = MV_DIR_FORWARD;
331 s->mv_type = MV_TYPE_16X16;
332 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
333 s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
334 s->mv[0][0][1] = h->current_mv_y * 2;
335
336 intra:
337 /* decode each block */
338 if(s->mb_intra || HAS_CBP(h->mtype)){
339 s->dsp.clear_blocks(s->block[0]);
340 for (i = 0; i < 6; i++) {
341 if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
342 return SLICE_ERROR;
343 }
344 cbp+=cbp;
345 }
346 }else{
347 for (i = 0; i < 6; i++)
348 s->block_last_index[i]= -1;
349 }
350
351 ff_MPV_decode_mb(s, s->block);
352
353 return SLICE_OK;
354 }
355
356 /**
357 * Decode a macroblock.
358 * @return <0 if an error occurred
359 */
360 static int h261_decode_block(H261Context * h, int16_t * block,
361 int n, int coded)
362 {
363 MpegEncContext * const s = &h->s;
364 int code, level, i, j, run;
365 RLTable *rl = &h261_rl_tcoeff;
366 const uint8_t *scan_table;
367
368 // For the variable length encoding there are two code tables, one being used for
369 // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
370 // for all other LEVELs except the first one in INTRA blocks which is fixed length
371 // coded with 8 bits.
372 // NOTE: the two code tables only differ in one VLC so we handle that manually.
373 scan_table = s->intra_scantable.permutated;
374 if (s->mb_intra){
375 /* DC coef */
376 level = get_bits(&s->gb, 8);
377 // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
378 if((level&0x7F) == 0){
379 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
380 return -1;
381 }
382 // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
383 if (level == 255)
384 level = 128;
385 block[0] = level;
386 i = 1;
387 }else if(coded){
388 // Run Level Code
389 // EOB Not possible for first level when cbp is available (that's why the table is different)
390 // 0 1 1s
391 // * * 0*
392 int check = show_bits(&s->gb, 2);
393 i = 0;
394 if ( check & 0x2 ){
395 skip_bits(&s->gb, 2);
396 block[0] = ( check & 0x1 ) ? -1 : 1;
397 i = 1;
398 }
399 }else{
400 i = 0;
401 }
402 if(!coded){
403 s->block_last_index[n] = i - 1;
404 return 0;
405 }
406 for(;;){
407 code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
408 if (code < 0){
409 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
410 return -1;
411 }
412 if (code == rl->n) {
413 /* escape */
414 // The remaining combinations of (run, level) are encoded with a 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits level.
415 run = get_bits(&s->gb, 6);
416 level = get_sbits(&s->gb, 8);
417 }else if(code == 0){
418 break;
419 }else{
420 run = rl->table_run[code];
421 level = rl->table_level[code];
422 if (get_bits1(&s->gb))
423 level = -level;
424 }
425 i += run;
426 if (i >= 64){
427 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
428 return -1;
429 }
430 j = scan_table[i];
431 block[j] = level;
432 i++;
433 }
434 s->block_last_index[n] = i-1;
435 return 0;
436 }
437
438 /**
439 * Decode the H.261 picture header.
440 * @return <0 if no startcode found
441 */
442 static int h261_decode_picture_header(H261Context *h){
443 MpegEncContext * const s = &h->s;
444 int format, i;
445 uint32_t startcode= 0;
446
447 for(i= get_bits_left(&s->gb); i>24; i-=1){
448 startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
449
450 if(startcode == 0x10)
451 break;
452 }
453
454 if (startcode != 0x10){
455 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
456 return -1;
457 }
458
459 /* temporal reference */
460 i= get_bits(&s->gb, 5); /* picture timestamp */
461 if(i < (s->picture_number&31))
462 i += 32;
463 s->picture_number = (s->picture_number&~31) + i;
464
465 s->avctx->time_base= (AVRational){1001, 30000};
466 s->current_picture.f.pts = s->picture_number;
467
468
469 /* PTYPE starts here */
470 skip_bits1(&s->gb); /* split screen off */
471 skip_bits1(&s->gb); /* camera off */
472 skip_bits1(&s->gb); /* freeze picture release off */
473
474 format = get_bits1(&s->gb);
475
476 //only 2 formats possible
477 if (format == 0){//QCIF
478 s->width = 176;
479 s->height = 144;
480 s->mb_width = 11;
481 s->mb_height = 9;
482 }else{//CIF
483 s->width = 352;
484 s->height = 288;
485 s->mb_width = 22;
486 s->mb_height = 18;
487 }
488
489 s->mb_num = s->mb_width * s->mb_height;
490
491 skip_bits1(&s->gb); /* still image mode off */
492 skip_bits1(&s->gb); /* Reserved */
493
494 /* PEI */
495 while (get_bits1(&s->gb) != 0){
496 skip_bits(&s->gb, 8);
497 }
498
499 // h261 has no I-FRAMES, but if we pass AV_PICTURE_TYPE_I for the first frame, the codec crashes if it does
500 // not contain all I-blocks (e.g. when a packet is lost)
501 s->pict_type = AV_PICTURE_TYPE_P;
502
503 h->gob_number = 0;
504 return 0;
505 }
506
507 static int h261_decode_gob(H261Context *h){
508 MpegEncContext * const s = &h->s;
509
510 ff_set_qscale(s, s->qscale);
511
512 /* decode mb's */
513 while(h->current_mba <= MBA_STUFFING)
514 {
515 int ret;
516 /* DCT & quantize */
517 ret= h261_decode_mb(h);
518 if(ret<0){
519 if(ret==SLICE_END){
520 h261_decode_mb_skipped(h, h->current_mba, 33);
521 return 0;
522 }
523 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
524 return -1;
525 }
526
527 h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
528 }
529
530 return -1;
531 }
532
533 /**
534 * returns the number of bytes consumed for building the current frame
535 */
536 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
537 int pos= get_bits_count(&s->gb)>>3;
538 if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
539 if(pos+10>buf_size) pos=buf_size; // oops ;)
540
541 return pos;
542 }
543
544 static int h261_decode_frame(AVCodecContext *avctx,
545 void *data, int *got_frame,
546 AVPacket *avpkt)
547 {
548 const uint8_t *buf = avpkt->data;
549 int buf_size = avpkt->size;
550 H261Context *h= avctx->priv_data;
551 MpegEncContext *s = &h->s;
552 int ret;
553 AVFrame *pict = data;
554
555 av_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
556 av_dlog(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
557 s->flags= avctx->flags;
558 s->flags2= avctx->flags2;
559
560 h->gob_start_code_skipped=0;
561
562 retry:
563
564 init_get_bits(&s->gb, buf, buf_size*8);
565
566 if(!s->context_initialized){
567 if (ff_MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
568 return -1;
569 }
570
571 //we need to set current_picture_ptr before reading the header, otherwise we cannot store anyting im there
572 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
573 int i= ff_find_unused_picture(s, 0);
574 if (i < 0)
575 return i;
576 s->current_picture_ptr= &s->picture[i];
577 }
578
579 ret = h261_decode_picture_header(h);
580
581 /* skip if the header was thrashed */
582 if (ret < 0){
583 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
584 return -1;
585 }
586
587 if (s->width != avctx->coded_width || s->height != avctx->coded_height){
588 ParseContext pc= s->parse_context; //FIXME move this demuxing hack to libavformat
589 s->parse_context.buffer=0;
590 ff_MPV_common_end(s);
591 s->parse_context= pc;
592 }
593 if (!s->context_initialized) {
594 avcodec_set_dimensions(avctx, s->width, s->height);
595
596 goto retry;
597 }
598
599 // for skipping the frame
600 s->current_picture.f.pict_type = s->pict_type;
601 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
602
603 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==AV_PICTURE_TYPE_B)
604 ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=AV_PICTURE_TYPE_I)
605 || avctx->skip_frame >= AVDISCARD_ALL)
606 return get_consumed_bytes(s, buf_size);
607
608 if(ff_MPV_frame_start(s, avctx) < 0)
609 return -1;
610
611 ff_mpeg_er_frame_start(s);
612
613 /* decode each macroblock */
614 s->mb_x=0;
615 s->mb_y=0;
616
617 while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
618 if(ff_h261_resync(h)<0)
619 break;
620 h261_decode_gob(h);
621 }
622 ff_MPV_frame_end(s);
623
624 assert(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
625 assert(s->current_picture.f.pict_type == s->pict_type);
626
627 if ((ret = av_frame_ref(pict, &s->current_picture_ptr->f)) < 0)
628 return ret;
629 ff_print_debug_info(s, s->current_picture_ptr);
630
631 *got_frame = 1;
632
633 return get_consumed_bytes(s, buf_size);
634 }
635
636 static av_cold int h261_decode_end(AVCodecContext *avctx)
637 {
638 H261Context *h= avctx->priv_data;
639 MpegEncContext *s = &h->s;
640
641 ff_MPV_common_end(s);
642 return 0;
643 }
644
645 AVCodec ff_h261_decoder = {
646 .name = "h261",
647 .type = AVMEDIA_TYPE_VIDEO,
648 .id = AV_CODEC_ID_H261,
649 .priv_data_size = sizeof(H261Context),
650 .init = h261_decode_init,
651 .close = h261_decode_end,
652 .decode = h261_decode_frame,
653 .capabilities = CODEC_CAP_DR1,
654 .long_name = NULL_IF_CONFIG_SMALL("H.261"),
655 };