3 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
4 * Copyright (c) 2004 Maarten Daniels
6 * This file is part of FFmpeg.
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.
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.
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
28 #include "libavutil/avassert.h"
30 #include "mpegvideo.h"
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
42 static VLC h261_mba_vlc;
43 static VLC h261_mtype_vlc;
44 static VLC h261_mv_vlc;
45 static VLC h261_cbp_vlc;
47 static av_cold void h261_decode_init_vlc(H261Context *h)
53 INIT_VLC_STATIC(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
54 ff_h261_mba_bits, 1, 1,
55 ff_h261_mba_code, 1, 1, 662);
56 INIT_VLC_STATIC(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
57 ff_h261_mtype_bits, 1, 1,
58 ff_h261_mtype_code, 1, 1, 80);
59 INIT_VLC_STATIC(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
60 &ff_h261_mv_tab[0][1], 2, 1,
61 &ff_h261_mv_tab[0][0], 2, 1, 144);
62 INIT_VLC_STATIC(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
63 &ff_h261_cbp_tab[0][1], 2, 1,
64 &ff_h261_cbp_tab[0][0], 2, 1, 512);
65 INIT_VLC_RL(ff_h261_rl_tcoeff, 552);
69 static av_cold int h261_decode_init(AVCodecContext *avctx)
71 H261Context *h = avctx->priv_data;
72 MpegEncContext *const s = &h->s;
75 ff_MPV_decode_defaults(s);
77 s->width = s->avctx->coded_width;
78 s->height = s->avctx->coded_height;
79 s->codec_id = s->avctx->codec->id;
80 s->out_format = FMT_H261;
82 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
83 s->codec_id = avctx->codec->id;
85 ff_h261_common_init();
86 h261_decode_init_vlc(h);
88 h->gob_start_code_skipped = 0;
94 * Decode the group of blocks header or slice header.
95 * @return <0 if an error occurred
97 static int h261_decode_gob_header(H261Context *h)
100 MpegEncContext *const s = &h->s;
102 if (!h->gob_start_code_skipped) {
103 /* Check for GOB Start Code */
104 val = show_bits(&s->gb, 15);
109 skip_bits(&s->gb, 16);
112 h->gob_start_code_skipped = 0;
114 h->gob_number = get_bits(&s->gb, 4); /* GN */
115 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
117 /* Check if gob_number is valid */
118 if (s->mb_height == 18) { // CIF
119 if ((h->gob_number <= 0) || (h->gob_number > 12))
122 if ((h->gob_number != 1) && (h->gob_number != 3) &&
123 (h->gob_number != 5))
128 while (get_bits1(&s->gb) != 0)
129 skip_bits(&s->gb, 8);
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))
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
148 * Decode the group of blocks / video packet header.
149 * @return <0 if no resync found
151 static int h261_resync(H261Context *h)
153 MpegEncContext *const s = &h->s;
156 if (h->gob_start_code_skipped) {
157 ret = h261_decode_gob_header(h);
161 if (show_bits(&s->gb, 15) == 0) {
162 ret = h261_decode_gob_header(h);
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);
171 for (; left > 15 + 1 + 4 + 5; left -= 8) {
172 if (show_bits(&s->gb, 15) == 0) {
173 GetBitContext bak = s->gb;
175 ret = h261_decode_gob_header(h);
181 skip_bits(&s->gb, 8);
189 * Decode skipped macroblocks.
192 static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2)
194 MpegEncContext *const s = &h->s;
199 for (i = mba1; i < mba2; i++) {
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);
208 for (j = 0; j < 6; j++)
209 s->block_last_index[j] = -1;
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;
217 h->mtype &= ~MB_TYPE_H261_FIL;
219 ff_MPV_decode_mb(s, s->block);
225 static const int mvmap[17] = {
226 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16
229 static int decode_mv_component(GetBitContext *gb, int v)
231 int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
233 /* check if mv_diff is valid */
237 mv_diff = mvmap[mv_diff];
239 if (mv_diff && !get_bits1(gb))
252 * Decode a macroblock.
253 * @return <0 if an error occurred
255 static int h261_decode_block(H261Context *h, int16_t *block, int n, int coded)
257 MpegEncContext *const s = &h->s;
258 int code, level, i, j, run;
259 RLTable *rl = &ff_h261_rl_tcoeff;
260 const uint8_t *scan_table;
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
268 scan_table = s->intra_scantable.permutated;
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);
278 /* The code 1000 0000 is not used, the reconstruction level of 1024
279 * being coded as 1111 1111. */
286 // EOB Not possible for first level when cbp is available (that's why the table is different)
289 int check = show_bits(&s->gb, 2);
292 skip_bits(&s->gb, 2);
293 block[0] = (check & 0x1) ? -1 : 1;
300 s->block_last_index[n] = i - 1;
304 code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
306 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n",
312 /* The remaining combinations of (run, level) are encoded with a
313 * 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits
315 run = get_bits(&s->gb, 6);
316 level = get_sbits(&s->gb, 8);
317 } else if (code == 0) {
320 run = rl->table_run[code];
321 level = rl->table_level[code];
322 if (get_bits1(&s->gb))
327 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n",
335 s->block_last_index[n] = i - 1;
339 static int h261_decode_mb(H261Context *h)
341 MpegEncContext *const s = &h->s;
347 h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table,
348 H261_MBA_VLC_BITS, 2);
350 /* Check for slice end */
351 /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
352 if (h->mba_diff == MBA_STARTCODE) { // start code
353 h->gob_start_code_skipped = 1;
356 } while (h->mba_diff == MBA_STUFFING); // stuffing
358 if (h->mba_diff < 0) {
359 if (get_bits_left(&s->gb) <= 7)
362 av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
367 h->current_mba += h->mba_diff;
369 if (h->current_mba > MBA_STUFFING)
372 s->mb_x = ((h->gob_number - 1) % 2) * 11 + ((h->current_mba - 1) % 11);
373 s->mb_y = ((h->gob_number - 1) / 2) * 3 + ((h->current_mba - 1) / 11);
374 xy = s->mb_x + s->mb_y * s->mb_stride;
375 ff_init_block_index(s);
376 ff_update_block_index(s);
379 h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
381 av_log(s->avctx, AV_LOG_ERROR, "Invalid mtype index %d\n",
385 av_assert0(h->mtype < FF_ARRAY_ELEMS(ff_h261_mtype_map));
386 h->mtype = ff_h261_mtype_map[h->mtype];
389 if (IS_QUANT(h->mtype))
390 ff_set_qscale(s, get_bits(&s->gb, 5));
392 s->mb_intra = IS_INTRA4x4(h->mtype);
395 if (IS_16X16(h->mtype)) {
396 /* Motion vector data is included for all MC macroblocks. MVD is
397 * obtained from the macroblock vector by subtracting the vector
398 * of the preceding macroblock. For this calculation the vector
399 * of the preceding macroblock is regarded as zero in the
400 * following three situations:
401 * 1) evaluating MVD for macroblocks 1, 12 and 23;
402 * 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
403 * 3) MTYPE of the previous macroblock was not MC. */
404 if ((h->current_mba == 1) || (h->current_mba == 12) ||
405 (h->current_mba == 23) || (h->mba_diff != 1)) {
410 h->current_mv_x = decode_mv_component(&s->gb, h->current_mv_x);
411 h->current_mv_y = decode_mv_component(&s->gb, h->current_mv_y);
418 if (HAS_CBP(h->mtype))
419 cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
422 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
427 s->mv_dir = MV_DIR_FORWARD;
428 s->mv_type = MV_TYPE_16X16;
429 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
430 s->mv[0][0][0] = h->current_mv_x * 2; // gets divided by 2 in motion compensation
431 s->mv[0][0][1] = h->current_mv_y * 2;
434 /* decode each block */
435 if (s->mb_intra || HAS_CBP(h->mtype)) {
436 s->dsp.clear_blocks(s->block[0]);
437 for (i = 0; i < 6; i++) {
438 if (h261_decode_block(h, s->block[i], i, cbp & 32) < 0)
443 for (i = 0; i < 6; i++)
444 s->block_last_index[i] = -1;
447 ff_MPV_decode_mb(s, s->block);
453 * Decode the H.261 picture header.
454 * @return <0 if no startcode found
456 static int h261_decode_picture_header(H261Context *h)
458 MpegEncContext *const s = &h->s;
460 uint32_t startcode = 0;
462 for (i = get_bits_left(&s->gb); i > 24; i -= 1) {
463 startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
465 if (startcode == 0x10)
469 if (startcode != 0x10) {
470 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
474 /* temporal reference */
475 i = get_bits(&s->gb, 5); /* picture timestamp */
476 if (i < (s->picture_number & 31))
478 s->picture_number = (s->picture_number & ~31) + i;
480 s->avctx->time_base = (AVRational) { 1001, 30000 };
481 s->current_picture.f.pts = s->picture_number;
483 /* PTYPE starts here */
484 skip_bits1(&s->gb); /* split screen off */
485 skip_bits1(&s->gb); /* camera off */
486 skip_bits1(&s->gb); /* freeze picture release off */
488 format = get_bits1(&s->gb);
490 // only 2 formats possible
491 if (format == 0) { // QCIF
503 s->mb_num = s->mb_width * s->mb_height;
505 skip_bits1(&s->gb); /* still image mode off */
506 skip_bits1(&s->gb); /* Reserved */
509 while (get_bits1(&s->gb) != 0)
510 skip_bits(&s->gb, 8);
512 /* H.261 has no I-frames, but if we pass AV_PICTURE_TYPE_I for the first
513 * frame, the codec crashes if it does not contain all I-blocks
514 * (e.g. when a packet is lost). */
515 s->pict_type = AV_PICTURE_TYPE_P;
521 static int h261_decode_gob(H261Context *h)
523 MpegEncContext *const s = &h->s;
525 ff_set_qscale(s, s->qscale);
528 while (h->current_mba <= MBA_STUFFING) {
531 ret = h261_decode_mb(h);
533 if (ret == SLICE_END) {
534 h261_decode_mb_skipped(h, h->current_mba, 33);
537 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n",
538 s->mb_x + s->mb_y * s->mb_stride);
542 h261_decode_mb_skipped(h,
543 h->current_mba - h->mba_diff,
551 * returns the number of bytes consumed for building the current frame
553 static int get_consumed_bytes(MpegEncContext *s, int buf_size)
555 int pos = get_bits_count(&s->gb) >> 3;
557 pos = 1; // avoid infinite loops (i doubt that is needed but ...)
558 if (pos + 10 > buf_size)
559 pos = buf_size; // oops ;)
564 static int h261_decode_frame(AVCodecContext *avctx, void *data,
565 int *got_frame, AVPacket *avpkt)
567 const uint8_t *buf = avpkt->data;
568 int buf_size = avpkt->size;
569 H261Context *h = avctx->priv_data;
570 MpegEncContext *s = &h->s;
572 AVFrame *pict = data;
574 av_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
575 av_dlog(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
576 s->flags = avctx->flags;
577 s->flags2 = avctx->flags2;
579 h->gob_start_code_skipped = 0;
582 init_get_bits(&s->gb, buf, buf_size * 8);
584 if (!s->context_initialized)
585 // we need the IDCT permutaton for reading a custom matrix
586 if (ff_MPV_common_init(s) < 0)
589 /* We need to set current_picture_ptr before reading the header,
590 * otherwise we cannot store anything in there. */
591 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
592 int i = ff_find_unused_picture(s, 0);
595 s->current_picture_ptr = &s->picture[i];
598 ret = h261_decode_picture_header(h);
600 /* skip if the header was thrashed */
602 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
606 if (s->width != avctx->coded_width || s->height != avctx->coded_height) {
607 ParseContext pc = s->parse_context; // FIXME move this demuxing hack to libavformat
608 s->parse_context.buffer = 0;
609 ff_MPV_common_end(s);
610 s->parse_context = pc;
612 if (!s->context_initialized) {
613 avcodec_set_dimensions(avctx, s->width, s->height);
618 // for skipping the frame
619 s->current_picture.f.pict_type = s->pict_type;
620 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
622 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
623 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
624 avctx->skip_frame >= AVDISCARD_ALL)
625 return get_consumed_bytes(s, buf_size);
627 if (ff_MPV_frame_start(s, avctx) < 0)
630 ff_mpeg_er_frame_start(s);
632 /* decode each macroblock */
636 while (h->gob_number < (s->mb_height == 18 ? 12 : 5)) {
637 if (h261_resync(h) < 0)
643 av_assert0(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
644 av_assert0(s->current_picture.f.pict_type == s->pict_type);
646 if ((ret = av_frame_ref(pict, &s->current_picture_ptr->f)) < 0)
648 ff_print_debug_info(s, s->current_picture_ptr, pict);
652 return get_consumed_bytes(s, buf_size);
655 static av_cold int h261_decode_end(AVCodecContext *avctx)
657 H261Context *h = avctx->priv_data;
658 MpegEncContext *s = &h->s;
660 ff_MPV_common_end(s);
664 AVCodec ff_h261_decoder = {
666 .type = AVMEDIA_TYPE_VIDEO,
667 .id = AV_CODEC_ID_H261,
668 .priv_data_size = sizeof(H261Context),
669 .init = h261_decode_init,
670 .close = h261_decode_end,
671 .decode = h261_decode_frame,
672 .capabilities = CODEC_CAP_DR1,
674 .long_name = NULL_IF_CONFIG_SMALL("H.261"),