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