Split the h261.c file into separate files for the encoder, decoder and common stuff.
[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 FFmpeg.
7 *
8 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 h261dec.c
25 * H.261 decoder.
26 */
27
28 #include "dsputil.h"
29 #include "avcodec.h"
30 #include "mpegvideo.h"
31 #include "h261data.h"
32 #include "h261.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, DCTELEM * block, int n, int coded);
50
51 static void h261_decode_init_vlc(H261Context *h){
52 static int done = 0;
53
54 if(!done){
55 done = 1;
56 init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
57 h261_mba_bits, 1, 1,
58 h261_mba_code, 1, 1, 1);
59 init_vlc(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
60 h261_mtype_bits, 1, 1,
61 h261_mtype_code, 1, 1, 1);
62 init_vlc(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
63 &h261_mv_tab[0][1], 2, 1,
64 &h261_mv_tab[0][0], 2, 1, 1);
65 init_vlc(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
66 &h261_cbp_tab[0][1], 2, 1,
67 &h261_cbp_tab[0][0], 2, 1, 1);
68 init_rl(&h261_rl_tcoeff, ff_h261_rl_table_store);
69 init_vlc_rl(&h261_rl_tcoeff, 1);
70 }
71 }
72
73 static int h261_decode_init(AVCodecContext *avctx){
74 H261Context *h= avctx->priv_data;
75 MpegEncContext * const s = &h->s;
76
77 // set defaults
78 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= 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 * decodes the group of blocks header or slice header.
100 * @return <0 if an error occured
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 return -1;
138
139 // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
140 // subsequent macroblocks, MBA is the difference between the absolute addresses of
141 // the macroblock and the last transmitted macroblock.
142 h->current_mba = 0;
143 h->mba_diff = 0;
144
145 return 0;
146 }
147
148 /**
149 * decodes the group of blocks / video packet header.
150 * @return <0 if no resync found
151 */
152 static int ff_h261_resync(H261Context *h){
153 MpegEncContext * const s = &h->s;
154 int left, ret;
155
156 if ( h->gob_start_code_skipped ){
157 ret= h261_decode_gob_header(h);
158 if(ret>=0)
159 return 0;
160 }
161 else{
162 if(show_bits(&s->gb, 15)==0){
163 ret= h261_decode_gob_header(h);
164 if(ret>=0)
165 return 0;
166 }
167 //ok, its not where its supposed to be ...
168 s->gb= s->last_resync_gb;
169 align_get_bits(&s->gb);
170 left= s->gb.size_in_bits - get_bits_count(&s->gb);
171
172 for(;left>15+1+4+5; left-=8){
173 if(show_bits(&s->gb, 15)==0){
174 GetBitContext bak= s->gb;
175
176 ret= h261_decode_gob_header(h);
177 if(ret>=0)
178 return 0;
179
180 s->gb= bak;
181 }
182 skip_bits(&s->gb, 8);
183 }
184 }
185
186 return -1;
187 }
188
189 /**
190 * decodes skipped macroblocks
191 * @return 0
192 */
193 static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
194 {
195 MpegEncContext * const s = &h->s;
196 int i;
197
198 s->mb_intra = 0;
199
200 for(i=mba1; i<mba2; i++){
201 int j, xy;
202
203 s->mb_x= ((h->gob_number-1) % 2) * 11 + i % 11;
204 s->mb_y= ((h->gob_number-1) / 2) * 3 + i / 11;
205 xy = s->mb_x + s->mb_y * s->mb_stride;
206 ff_init_block_index(s);
207 ff_update_block_index(s);
208
209 for(j=0;j<6;j++)
210 s->block_last_index[j] = -1;
211
212 s->mv_dir = MV_DIR_FORWARD;
213 s->mv_type = MV_TYPE_16X16;
214 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
215 s->mv[0][0][0] = 0;
216 s->mv[0][0][1] = 0;
217 s->mb_skipped = 1;
218 h->mtype &= ~MB_TYPE_H261_FIL;
219
220 MPV_decode_mb(s, s->block);
221 }
222
223 return 0;
224 }
225
226 static int decode_mv_component(GetBitContext *gb, int v){
227 int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
228
229 /* check if mv_diff is valid */
230 if ( mv_diff < 0 )
231 return v;
232
233 mv_diff = mvmap[mv_diff];
234
235 if(mv_diff && !get_bits1(gb))
236 mv_diff= -mv_diff;
237
238 v += mv_diff;
239 if (v <=-16) v+= 32;
240 else if(v >= 16) v-= 32;
241
242 return v;
243 }
244
245 static int h261_decode_mb(H261Context *h){
246 MpegEncContext * const s = &h->s;
247 int i, cbp, xy;
248
249 cbp = 63;
250 // Read mba
251 do{
252 h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
253
254 /* Check for slice end */
255 /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
256 if (h->mba_diff == MBA_STARTCODE){ // start code
257 h->gob_start_code_skipped = 1;
258 return SLICE_END;
259 }
260 }
261 while( h->mba_diff == MBA_STUFFING ); // stuffing
262
263 if ( h->mba_diff < 0 ){
264 if ( get_bits_count(&s->gb) + 7 >= s->gb.size_in_bits )
265 return SLICE_END;
266
267 av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
268 return SLICE_ERROR;
269 }
270
271 h->mba_diff += 1;
272 h->current_mba += h->mba_diff;
273
274 if ( h->current_mba > MBA_STUFFING )
275 return SLICE_ERROR;
276
277 s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
278 s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
279 xy = s->mb_x + s->mb_y * s->mb_stride;
280 ff_init_block_index(s);
281 ff_update_block_index(s);
282
283 // Read mtype
284 h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
285 h->mtype = h261_mtype_map[h->mtype];
286
287 // Read mquant
288 if ( IS_QUANT ( h->mtype ) ){
289 ff_set_qscale(s, get_bits(&s->gb, 5));
290 }
291
292 s->mb_intra = IS_INTRA4x4(h->mtype);
293
294 // Read mv
295 if ( IS_16X16 ( h->mtype ) ){
296 // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
297 // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
298 // following three situations:
299 // 1) evaluating MVD for macroblocks 1, 12 and 23;
300 // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
301 // 3) MTYPE of the previous macroblock was not MC.
302 if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
303 ( h->mba_diff != 1))
304 {
305 h->current_mv_x = 0;
306 h->current_mv_y = 0;
307 }
308
309 h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
310 h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
311 }else{
312 h->current_mv_x = 0;
313 h->current_mv_y = 0;
314 }
315
316 // Read cbp
317 if ( HAS_CBP( h->mtype ) ){
318 cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
319 }
320
321 if(s->mb_intra){
322 s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
323 goto intra;
324 }
325
326 //set motion vectors
327 s->mv_dir = MV_DIR_FORWARD;
328 s->mv_type = MV_TYPE_16X16;
329 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
330 s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
331 s->mv[0][0][1] = h->current_mv_y * 2;
332
333 intra:
334 /* decode each block */
335 if(s->mb_intra || HAS_CBP(h->mtype)){
336 s->dsp.clear_blocks(s->block[0]);
337 for (i = 0; i < 6; i++) {
338 if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
339 return SLICE_ERROR;
340 }
341 cbp+=cbp;
342 }
343 }else{
344 for (i = 0; i < 6; i++)
345 s->block_last_index[i]= -1;
346 }
347
348 MPV_decode_mb(s, s->block);
349
350 return SLICE_OK;
351 }
352
353 /**
354 * decodes a macroblock
355 * @return <0 if an error occured
356 */
357 static int h261_decode_block(H261Context * h, DCTELEM * block,
358 int n, int coded)
359 {
360 MpegEncContext * const s = &h->s;
361 int code, level, i, j, run;
362 RLTable *rl = &h261_rl_tcoeff;
363 const uint8_t *scan_table;
364
365 // For the variable length encoding there are two code tables, one being used for
366 // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
367 // for all other LEVELs except the first one in INTRA blocks which is fixed length
368 // coded with 8 bits.
369 // NOTE: the two code tables only differ in one VLC so we handle that manually.
370 scan_table = s->intra_scantable.permutated;
371 if (s->mb_intra){
372 /* DC coef */
373 level = get_bits(&s->gb, 8);
374 // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
375 if((level&0x7F) == 0){
376 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
377 return -1;
378 }
379 // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
380 if (level == 255)
381 level = 128;
382 block[0] = level;
383 i = 1;
384 }else if(coded){
385 // Run Level Code
386 // EOB Not possible for first level when cbp is available (that's why the table is different)
387 // 0 1 1s
388 // * * 0*
389 int check = show_bits(&s->gb, 2);
390 i = 0;
391 if ( check & 0x2 ){
392 skip_bits(&s->gb, 2);
393 block[0] = ( check & 0x1 ) ? -1 : 1;
394 i = 1;
395 }
396 }else{
397 i = 0;
398 }
399 if(!coded){
400 s->block_last_index[n] = i - 1;
401 return 0;
402 }
403 for(;;){
404 code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
405 if (code < 0){
406 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
407 return -1;
408 }
409 if (code == rl->n) {
410 /* escape */
411 // 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.
412 run = get_bits(&s->gb, 6);
413 level = get_sbits(&s->gb, 8);
414 }else if(code == 0){
415 break;
416 }else{
417 run = rl->table_run[code];
418 level = rl->table_level[code];
419 if (get_bits1(&s->gb))
420 level = -level;
421 }
422 i += run;
423 if (i >= 64){
424 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
425 return -1;
426 }
427 j = scan_table[i];
428 block[j] = level;
429 i++;
430 }
431 s->block_last_index[n] = i-1;
432 return 0;
433 }
434
435 /**
436 * decodes the H261 picture header.
437 * @return <0 if no startcode found
438 */
439 static int h261_decode_picture_header(H261Context *h){
440 MpegEncContext * const s = &h->s;
441 int format, i;
442 uint32_t startcode= 0;
443
444 for(i= s->gb.size_in_bits - get_bits_count(&s->gb); i>24; i-=1){
445 startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
446
447 if(startcode == 0x10)
448 break;
449 }
450
451 if (startcode != 0x10){
452 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
453 return -1;
454 }
455
456 /* temporal reference */
457 i= get_bits(&s->gb, 5); /* picture timestamp */
458 if(i < (s->picture_number&31))
459 i += 32;
460 s->picture_number = (s->picture_number&~31) + i;
461
462 s->avctx->time_base= (AVRational){1001, 30000};
463 s->current_picture.pts= s->picture_number;
464
465
466 /* PTYPE starts here */
467 skip_bits1(&s->gb); /* split screen off */
468 skip_bits1(&s->gb); /* camera off */
469 skip_bits1(&s->gb); /* freeze picture release off */
470
471 format = get_bits1(&s->gb);
472
473 //only 2 formats possible
474 if (format == 0){//QCIF
475 s->width = 176;
476 s->height = 144;
477 s->mb_width = 11;
478 s->mb_height = 9;
479 }else{//CIF
480 s->width = 352;
481 s->height = 288;
482 s->mb_width = 22;
483 s->mb_height = 18;
484 }
485
486 s->mb_num = s->mb_width * s->mb_height;
487
488 skip_bits1(&s->gb); /* still image mode off */
489 skip_bits1(&s->gb); /* Reserved */
490
491 /* PEI */
492 while (get_bits1(&s->gb) != 0){
493 skip_bits(&s->gb, 8);
494 }
495
496 // h261 has no I-FRAMES, but if we pass I_TYPE for the first frame, the codec crashes if it does
497 // not contain all I-blocks (e.g. when a packet is lost)
498 s->pict_type = P_TYPE;
499
500 h->gob_number = 0;
501 return 0;
502 }
503
504 static int h261_decode_gob(H261Context *h){
505 MpegEncContext * const s = &h->s;
506
507 ff_set_qscale(s, s->qscale);
508
509 /* decode mb's */
510 while(h->current_mba <= MBA_STUFFING)
511 {
512 int ret;
513 /* DCT & quantize */
514 ret= h261_decode_mb(h);
515 if(ret<0){
516 if(ret==SLICE_END){
517 h261_decode_mb_skipped(h, h->current_mba, 33);
518 return 0;
519 }
520 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
521 return -1;
522 }
523
524 h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
525 }
526
527 return -1;
528 }
529
530 /**
531 * returns the number of bytes consumed for building the current frame
532 */
533 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
534 int pos= get_bits_count(&s->gb)>>3;
535 if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
536 if(pos+10>buf_size) pos=buf_size; // oops ;)
537
538 return pos;
539 }
540
541 static int h261_decode_frame(AVCodecContext *avctx,
542 void *data, int *data_size,
543 uint8_t *buf, int buf_size)
544 {
545 H261Context *h= avctx->priv_data;
546 MpegEncContext *s = &h->s;
547 int ret;
548 AVFrame *pict = data;
549
550 #ifdef DEBUG
551 av_log(avctx, AV_LOG_DEBUG, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
552 av_log(avctx, AV_LOG_DEBUG, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
553 #endif
554 s->flags= avctx->flags;
555 s->flags2= avctx->flags2;
556
557 h->gob_start_code_skipped=0;
558
559 retry:
560
561 init_get_bits(&s->gb, buf, buf_size*8);
562
563 if(!s->context_initialized){
564 if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
565 return -1;
566 }
567
568 //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
569 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
570 int i= ff_find_unused_picture(s, 0);
571 s->current_picture_ptr= &s->picture[i];
572 }
573
574 ret = h261_decode_picture_header(h);
575
576 /* skip if the header was thrashed */
577 if (ret < 0){
578 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
579 return -1;
580 }
581
582 if (s->width != avctx->coded_width || s->height != avctx->coded_height){
583 ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
584 s->parse_context.buffer=0;
585 MPV_common_end(s);
586 s->parse_context= pc;
587 }
588 if (!s->context_initialized) {
589 avcodec_set_dimensions(avctx, s->width, s->height);
590
591 goto retry;
592 }
593
594 // for hurry_up==5
595 s->current_picture.pict_type= s->pict_type;
596 s->current_picture.key_frame= s->pict_type == I_TYPE;
597
598 /* skip everything if we are in a hurry>=5 */
599 if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
600 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE)
601 ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE)
602 || avctx->skip_frame >= AVDISCARD_ALL)
603 return get_consumed_bytes(s, buf_size);
604
605 if(MPV_frame_start(s, avctx) < 0)
606 return -1;
607
608 ff_er_frame_start(s);
609
610 /* decode each macroblock */
611 s->mb_x=0;
612 s->mb_y=0;
613
614 while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
615 if(ff_h261_resync(h)<0)
616 break;
617 h261_decode_gob(h);
618 }
619 MPV_frame_end(s);
620
621 assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
622 assert(s->current_picture.pict_type == s->pict_type);
623 *pict= *(AVFrame*)s->current_picture_ptr;
624 ff_print_debug_info(s, pict);
625
626 *data_size = sizeof(AVFrame);
627
628 return get_consumed_bytes(s, buf_size);
629 }
630
631 static int h261_decode_end(AVCodecContext *avctx)
632 {
633 H261Context *h= avctx->priv_data;
634 MpegEncContext *s = &h->s;
635
636 MPV_common_end(s);
637 return 0;
638 }
639
640 AVCodec h261_decoder = {
641 "h261",
642 CODEC_TYPE_VIDEO,
643 CODEC_ID_H261,
644 sizeof(H261Context),
645 h261_decode_init,
646 NULL,
647 h261_decode_end,
648 h261_decode_frame,
649 CODEC_CAP_DR1,
650 };