3 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
4 * Copyright (c) 2004 Maarten Daniels
6 * This file is part of Libav.
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.
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.
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
29 #include "bitstream.h"
31 #include "mpegutils.h"
32 #include "mpegvideo.h"
38 #define H261_MBA_VLC_BITS 9
39 #define H261_MTYPE_VLC_BITS 6
40 #define H261_MV_VLC_BITS 7
41 #define H261_CBP_VLC_BITS 9
42 #define TCOEFF_VLC_BITS 9
43 #define MBA_STUFFING 33
44 #define MBA_STARTCODE 34
46 static VLC h261_mba_vlc;
47 static VLC h261_mtype_vlc;
48 static VLC h261_mv_vlc;
49 static VLC h261_cbp_vlc;
51 static av_cold void h261_decode_init_vlc(H261Context *h)
57 INIT_VLC_STATIC(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
58 ff_h261_mba_bits, 1, 1,
59 ff_h261_mba_code, 1, 1, 662);
60 INIT_VLC_STATIC(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
61 ff_h261_mtype_bits, 1, 1,
62 ff_h261_mtype_code, 1, 1, 80);
63 INIT_VLC_STATIC(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
64 &ff_h261_mv_tab[0][1], 2, 1,
65 &ff_h261_mv_tab[0][0], 2, 1, 144);
66 INIT_VLC_STATIC(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
67 &ff_h261_cbp_tab[0][1], 2, 1,
68 &ff_h261_cbp_tab[0][0], 2, 1, 512);
69 INIT_VLC_RL(ff_h261_rl_tcoeff, 552);
73 static av_cold int h261_decode_init(AVCodecContext *avctx)
75 H261Context *h = avctx->priv_data;
76 MpegEncContext *const s = &h->s;
79 ff_mpv_decode_defaults(s);
81 s->width = s->avctx->coded_width;
82 s->height = s->avctx->coded_height;
83 s->codec_id = s->avctx->codec->id;
84 s->out_format = FMT_H261;
86 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
87 s->codec_id = avctx->codec->id;
89 ff_h261_common_init();
90 h261_decode_init_vlc(h);
92 h->gob_start_code_skipped = 0;
98 * Decode the group of blocks header or slice header.
99 * @return <0 if an error occurred
101 static int h261_decode_gob_header(H261Context *h)
104 MpegEncContext *const s = &h->s;
106 if (!h->gob_start_code_skipped) {
107 /* Check for GOB Start Code */
108 val = bitstream_peek(&s->bc, 15);
113 bitstream_skip(&s->bc, 16);
116 h->gob_start_code_skipped = 0;
118 h->gob_number = bitstream_read(&s->bc, 4); /* GN */
119 s->qscale = bitstream_read(&s->bc, 5); /* GQUANT */
121 /* Check if gob_number is valid */
122 if (s->mb_height == 18) { // CIF
123 if ((h->gob_number <= 0) || (h->gob_number > 12))
126 if ((h->gob_number != 1) && (h->gob_number != 3) &&
127 (h->gob_number != 5))
132 while (bitstream_read_bit(&s->bc) != 0)
133 bitstream_skip(&s->bc, 8);
135 if (s->qscale == 0) {
136 av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\n");
137 if (s->avctx->err_recognition & AV_EF_BITSTREAM)
141 /* For the first transmitted macroblock in a GOB, MBA is the absolute
142 * address. For subsequent macroblocks, MBA is the difference between
143 * the absolute addresses of the macroblock and the last transmitted
152 * Decode the group of blocks / video packet header.
153 * @return <0 if no resync found
155 static int h261_resync(H261Context *h)
157 MpegEncContext *const s = &h->s;
160 if (h->gob_start_code_skipped) {
161 ret = h261_decode_gob_header(h);
165 if (bitstream_peek(&s->bc, 15) == 0) {
166 ret = h261_decode_gob_header(h);
170 // OK, it is not where it is supposed to be ...
171 s->bc = s->last_resync_bc;
172 bitstream_align(&s->bc);
173 left = bitstream_bits_left(&s->bc);
175 for (; left > 15 + 1 + 4 + 5; left -= 8) {
176 if (bitstream_peek(&s->bc, 15) == 0) {
177 BitstreamContext bak = s->bc;
179 ret = h261_decode_gob_header(h);
185 bitstream_skip(&s->bc, 8);
193 * Decode skipped macroblocks.
196 static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2)
198 MpegEncContext *const s = &h->s;
203 for (i = mba1; i < mba2; i++) {
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);
212 for (j = 0; j < 6; j++)
213 s->block_last_index[j] = -1;
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;
221 h->mtype &= ~MB_TYPE_H261_FIL;
223 ff_mpv_decode_mb(s, s->block);
229 static const int mvmap[17] = {
230 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16
233 static int decode_mv_component(BitstreamContext *bc, int v)
235 int mv_diff = bitstream_read_vlc(bc, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
237 /* check if mv_diff is valid */
241 mv_diff = mvmap[mv_diff];
243 if (mv_diff && !bitstream_read_bit(bc))
256 * Decode a macroblock.
257 * @return <0 if an error occurred
259 static int h261_decode_block(H261Context *h, int16_t *block, int n, int coded)
261 MpegEncContext *const s = &h->s;
262 int code, level, i, j, run;
263 RLTable *rl = &ff_h261_rl_tcoeff;
264 const uint8_t *scan_table;
266 /* For the variable length encoding there are two code tables, one being
267 * used for the first transmitted LEVEL in INTER, INTER + MC and
268 * INTER + MC + FIL blocks, the second for all other LEVELs except the
269 * first one in INTRA blocks which is fixed length coded with 8 bits.
270 * NOTE: The two code tables only differ in one VLC so we handle that
272 scan_table = s->intra_scantable.permutated;
275 level = bitstream_read(&s->bc, 8);
276 // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
277 if ((level & 0x7F) == 0) {
278 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n",
279 level, s->mb_x, s->mb_y);
282 /* The code 1000 0000 is not used, the reconstruction level of 1024
283 * being coded as 1111 1111. */
290 // EOB Not possible for first level when cbp is available (that's why the table is different)
293 int check = bitstream_peek(&s->bc, 2);
296 bitstream_skip(&s->bc, 2);
297 block[0] = (check & 0x1) ? -1 : 1;
304 s->block_last_index[n] = i - 1;
308 code = bitstream_read_vlc(&s->bc, rl->vlc.table, TCOEFF_VLC_BITS, 2);
310 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n",
316 /* The remaining combinations of (run, level) are encoded with a
317 * 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits
319 run = bitstream_read(&s->bc, 6);
320 level = bitstream_read_signed(&s->bc, 8);
321 } else if (code == 0) {
324 run = rl->table_run[code];
325 level = rl->table_level[code];
326 if (bitstream_read_bit(&s->bc))
331 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n",
339 s->block_last_index[n] = i - 1;
343 static int h261_decode_mb(H261Context *h)
345 MpegEncContext *const s = &h->s;
351 h->mba_diff = bitstream_read_vlc(&s->bc, h261_mba_vlc.table,
352 H261_MBA_VLC_BITS, 2);
354 /* Check for slice end */
355 /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
356 if (h->mba_diff == MBA_STARTCODE) { // start code
357 h->gob_start_code_skipped = 1;
360 } while (h->mba_diff == MBA_STUFFING); // stuffing
362 if (h->mba_diff < 0) {
363 if (bitstream_bits_left(&s->bc) <= 7)
366 av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
371 h->current_mba += h->mba_diff;
373 if (h->current_mba > MBA_STUFFING)
376 s->mb_x = ((h->gob_number - 1) % 2) * 11 + ((h->current_mba - 1) % 11);
377 s->mb_y = ((h->gob_number - 1) / 2) * 3 + ((h->current_mba - 1) / 11);
378 xy = s->mb_x + s->mb_y * s->mb_stride;
379 ff_init_block_index(s);
380 ff_update_block_index(s);
383 h->mtype = bitstream_read_vlc(&s->bc, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
384 if (h->mtype < 0 || h->mtype >= FF_ARRAY_ELEMS(ff_h261_mtype_map)) {
385 av_log(s->avctx, AV_LOG_ERROR, "Invalid mtype index %d\n",
389 h->mtype = ff_h261_mtype_map[h->mtype];
392 if (IS_QUANT(h->mtype))
393 ff_set_qscale(s, bitstream_read(&s->bc, 5));
395 s->mb_intra = IS_INTRA4x4(h->mtype);
398 if (IS_16X16(h->mtype)) {
399 /* Motion vector data is included for all MC macroblocks. MVD is
400 * obtained from the macroblock vector by subtracting the vector
401 * of the preceding macroblock. For this calculation the vector
402 * of the preceding macroblock is regarded as zero in the
403 * following three situations:
404 * 1) evaluating MVD for macroblocks 1, 12 and 23;
405 * 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
406 * 3) MTYPE of the previous macroblock was not MC. */
407 if ((h->current_mba == 1) || (h->current_mba == 12) ||
408 (h->current_mba == 23) || (h->mba_diff != 1)) {
413 h->current_mv_x = decode_mv_component(&s->bc, h->current_mv_x);
414 h->current_mv_y = decode_mv_component(&s->bc, h->current_mv_y);
421 if (HAS_CBP(h->mtype))
422 cbp = bitstream_read_vlc(&s->bc, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
425 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
430 s->mv_dir = MV_DIR_FORWARD;
431 s->mv_type = MV_TYPE_16X16;
432 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
433 s->mv[0][0][0] = h->current_mv_x * 2; // gets divided by 2 in motion compensation
434 s->mv[0][0][1] = h->current_mv_y * 2;
437 /* decode each block */
438 if (s->mb_intra || HAS_CBP(h->mtype)) {
439 s->bdsp.clear_blocks(s->block[0]);
440 for (i = 0; i < 6; i++) {
441 if (h261_decode_block(h, s->block[i], i, cbp & 32) < 0)
446 for (i = 0; i < 6; i++)
447 s->block_last_index[i] = -1;
450 ff_mpv_decode_mb(s, s->block);
456 * Decode the H.261 picture header.
457 * @return <0 if no startcode found
459 static int h261_decode_picture_header(H261Context *h)
461 MpegEncContext *const s = &h->s;
463 uint32_t startcode = 0;
465 for (i = bitstream_bits_left(&s->bc); i > 24; i -= 1) {
466 startcode = ((startcode << 1) | bitstream_read(&s->bc, 1)) & 0x000FFFFF;
468 if (startcode == 0x10)
472 if (startcode != 0x10) {
473 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
477 /* temporal reference */
478 i = bitstream_read(&s->bc, 5); /* picture timestamp */
479 if (i < (s->picture_number & 31))
481 s->picture_number = (s->picture_number & ~31) + i;
483 s->avctx->framerate = (AVRational) { 30000, 1001 };
485 /* PTYPE starts here */
486 bitstream_skip(&s->bc, 1); /* split screen off */
487 bitstream_skip(&s->bc, 1); /* camera off */
488 bitstream_skip(&s->bc, 1); /* freeze picture release off */
490 format = bitstream_read_bit(&s->bc);
492 // only 2 formats possible
493 if (format == 0) { // QCIF
505 s->mb_num = s->mb_width * s->mb_height;
507 bitstream_skip(&s->bc, 1); /* still image mode off */
508 bitstream_skip(&s->bc, 1); /* Reserved */
511 while (bitstream_read_bit(&s->bc) != 0)
512 bitstream_skip(&s->bc, 8);
514 /* H.261 has no I-frames, but if we pass AV_PICTURE_TYPE_I for the first
515 * frame, the codec crashes if it does not contain all I-blocks
516 * (e.g. when a packet is lost). */
517 s->pict_type = AV_PICTURE_TYPE_P;
523 static int h261_decode_gob(H261Context *h)
525 MpegEncContext *const s = &h->s;
527 ff_set_qscale(s, s->qscale);
530 while (h->current_mba <= MBA_STUFFING) {
533 ret = h261_decode_mb(h);
535 if (ret == SLICE_END) {
536 h261_decode_mb_skipped(h, h->current_mba, 33);
539 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n",
540 s->mb_x + s->mb_y * s->mb_stride);
544 h261_decode_mb_skipped(h,
545 h->current_mba - h->mba_diff,
553 * returns the number of bytes consumed for building the current frame
555 static int get_consumed_bytes(MpegEncContext *s, int buf_size)
557 int pos = bitstream_tell(&s->bc) >> 3;
559 pos = 1; // avoid infinite loops (i doubt that is needed but ...)
560 if (pos + 10 > buf_size)
561 pos = buf_size; // oops ;)
566 static int h261_decode_frame(AVCodecContext *avctx, void *data,
567 int *got_frame, AVPacket *avpkt)
569 const uint8_t *buf = avpkt->data;
570 int buf_size = avpkt->size;
571 H261Context *h = avctx->priv_data;
572 MpegEncContext *s = &h->s;
574 AVFrame *pict = data;
576 ff_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
577 ff_dlog(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
579 h->gob_start_code_skipped = 0;
582 bitstream_init8(&s->bc, buf, buf_size);
584 if (!s->context_initialized)
585 // we need the IDCT permutation for reading a custom matrix
588 ret = h261_decode_picture_header(h);
590 /* skip if the header was thrashed */
592 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
596 if (s->width != avctx->coded_width || s->height != avctx->coded_height) {
597 ParseContext pc = s->parse_context; // FIXME move this demuxing hack to libavformat
598 s->parse_context.buffer = 0;
599 ff_mpv_common_end(s);
600 s->parse_context = pc;
603 if (!s->context_initialized) {
604 if ((ret = ff_mpv_common_init(s)) < 0)
607 ret = ff_set_dimensions(avctx, s->width, s->height);
614 // for skipping the frame
615 s->current_picture.f->pict_type = s->pict_type;
616 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
618 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
619 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
620 avctx->skip_frame >= AVDISCARD_ALL)
621 return get_consumed_bytes(s, buf_size);
623 if (ff_mpv_frame_start(s, avctx) < 0)
626 ff_mpeg_er_frame_start(s);
628 /* decode each macroblock */
632 while (h->gob_number < (s->mb_height == 18 ? 12 : 5)) {
633 if (h261_resync(h) < 0)
639 assert(s->current_picture.f->pict_type == s->current_picture_ptr->f->pict_type);
640 assert(s->current_picture.f->pict_type == s->pict_type);
642 if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
644 ff_print_debug_info(s, s->current_picture_ptr);
648 return get_consumed_bytes(s, buf_size);
651 static av_cold int h261_decode_end(AVCodecContext *avctx)
653 H261Context *h = avctx->priv_data;
654 MpegEncContext *s = &h->s;
656 ff_mpv_common_end(s);
660 AVCodec ff_h261_decoder = {
662 .long_name = NULL_IF_CONFIG_SMALL("H.261"),
663 .type = AVMEDIA_TYPE_VIDEO,
664 .id = AV_CODEC_ID_H261,
665 .priv_data_size = sizeof(H261Context),
666 .init = h261_decode_init,
667 .close = h261_decode_end,
668 .decode = h261_decode_frame,
669 .capabilities = AV_CODEC_CAP_DR1,