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"
29 #include "libavutil/thread.h"
32 #include "mpegutils.h"
33 #include "mpegvideo.h"
38 #define H261_MBA_VLC_BITS 8
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_static(void)
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, 540);
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_FIRST_VLC_RL(ff_h261_rl_tcoeff, 552);
68 static av_cold int h261_decode_init(AVCodecContext *avctx)
70 static AVOnce init_static_once = AV_ONCE_INIT;
71 H261Context *h = avctx->priv_data;
72 MpegEncContext *const s = &h->s;
75 ff_mpv_decode_init(s, avctx);
77 s->out_format = FMT_H261;
79 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
81 h->gob_start_code_skipped = 0;
83 ff_thread_once(&init_static_once, h261_decode_init_static);
89 * Decode the group of blocks header or slice header.
90 * @return <0 if an error occurred
92 static int h261_decode_gob_header(H261Context *h)
95 MpegEncContext *const s = &h->s;
97 if (!h->gob_start_code_skipped) {
98 /* Check for GOB Start Code */
99 val = show_bits(&s->gb, 15);
104 skip_bits(&s->gb, 16);
107 h->gob_start_code_skipped = 0;
109 h->gob_number = get_bits(&s->gb, 4); /* GN */
110 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
112 /* Check if gob_number is valid */
113 if (s->mb_height == 18) { // CIF
114 if ((h->gob_number <= 0) || (h->gob_number > 12))
117 if ((h->gob_number != 1) && (h->gob_number != 3) &&
118 (h->gob_number != 5))
123 if (skip_1stop_8data_bits(&s->gb) < 0)
124 return AVERROR_INVALIDDATA;
126 if (s->qscale == 0) {
127 av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\n");
128 if (s->avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
132 /* For the first transmitted macroblock in a GOB, MBA is the absolute
133 * address. For subsequent macroblocks, MBA is the difference between
134 * the absolute addresses of the macroblock and the last transmitted
143 * Decode the group of blocks / video packet header.
144 * @return <0 if no resync found
146 static int h261_resync(H261Context *h)
148 MpegEncContext *const s = &h->s;
151 if (h->gob_start_code_skipped) {
152 ret = h261_decode_gob_header(h);
156 if (show_bits(&s->gb, 15) == 0) {
157 ret = h261_decode_gob_header(h);
161 // OK, it is not where it is supposed to be ...
162 s->gb = s->last_resync_gb;
163 align_get_bits(&s->gb);
164 left = get_bits_left(&s->gb);
166 for (; left > 15 + 1 + 4 + 5; left -= 8) {
167 if (show_bits(&s->gb, 15) == 0) {
168 GetBitContext bak = s->gb;
170 ret = h261_decode_gob_header(h);
176 skip_bits(&s->gb, 8);
184 * Decode skipped macroblocks.
187 static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2)
189 MpegEncContext *const s = &h->s;
194 for (i = mba1; i < mba2; i++) {
197 s->mb_x = ((h->gob_number - 1) % 2) * 11 + i % 11;
198 s->mb_y = ((h->gob_number - 1) / 2) * 3 + i / 11;
199 xy = s->mb_x + s->mb_y * s->mb_stride;
200 ff_init_block_index(s);
201 ff_update_block_index(s);
203 for (j = 0; j < 6; j++)
204 s->block_last_index[j] = -1;
206 s->mv_dir = MV_DIR_FORWARD;
207 s->mv_type = MV_TYPE_16X16;
208 s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
212 h->mtype &= ~MB_TYPE_H261_FIL;
214 if (s->current_picture.motion_val[0]) {
215 int b_stride = 2*s->mb_width + 1;
216 int b_xy = 2 * s->mb_x + (2 * s->mb_y) * b_stride;
217 s->current_picture.motion_val[0][b_xy][0] = s->mv[0][0][0];
218 s->current_picture.motion_val[0][b_xy][1] = s->mv[0][0][1];
221 ff_mpv_reconstruct_mb(s, s->block);
227 static const int mvmap[17] = {
228 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16
231 static int decode_mv_component(GetBitContext *gb, int v)
233 int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
235 /* check if mv_diff is valid */
239 mv_diff = mvmap[mv_diff];
241 if (mv_diff && !get_bits1(gb))
254 * Decode a macroblock.
255 * @return <0 if an error occurred
257 static int h261_decode_block(H261Context *h, int16_t *block, int n, int coded)
259 MpegEncContext *const s = &h->s;
260 int level, i, j, run;
261 RLTable *rl = &ff_h261_rl_tcoeff;
262 const uint8_t *scan_table;
264 /* For the variable length encoding there are two code tables, one being
265 * used for the first transmitted LEVEL in INTER, INTER + MC and
266 * INTER + MC + FIL blocks, the second for all other LEVELs except the
267 * first one in INTRA blocks which is fixed length coded with 8 bits.
268 * NOTE: The two code tables only differ in one VLC so we handle that
270 scan_table = s->intra_scantable.permutated;
273 level = get_bits(&s->gb, 8);
274 // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
275 if ((level & 0x7F) == 0) {
276 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n",
277 level, s->mb_x, s->mb_y);
280 /* The code 1000 0000 is not used, the reconstruction level of 1024
281 * being coded as 1111 1111. */
288 // EOB Not possible for first level when cbp is available (that's why the table is different)
291 int check = show_bits(&s->gb, 2);
294 skip_bits(&s->gb, 2);
295 block[0] = (check & 0x1) ? -1 : 1;
302 s->block_last_index[n] = i - 1;
306 OPEN_READER(re, &s->gb);
307 i--; // offset by -1 to allow direct indexing of scan_table
309 UPDATE_CACHE(re, &s->gb);
310 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TCOEFF_VLC_BITS, 2, 0);
313 CLOSE_READER(re, &s->gb);
314 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n",
319 /* The remaining combinations of (run, level) are encoded with a
320 * 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits
322 run = SHOW_UBITS(re, &s->gb, 6) + 1;
323 SKIP_CACHE(re, &s->gb, 6);
324 level = SHOW_SBITS(re, &s->gb, 8);
325 SKIP_COUNTER(re, &s->gb, 6 + 8);
326 } else if (level == 0) {
329 if (SHOW_UBITS(re, &s->gb, 1))
331 SKIP_COUNTER(re, &s->gb, 1);
335 CLOSE_READER(re, &s->gb);
336 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n",
343 CLOSE_READER(re, &s->gb);
345 s->block_last_index[n] = i;
349 static int h261_decode_mb(H261Context *h)
351 MpegEncContext *const s = &h->s;
357 h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table,
358 H261_MBA_VLC_BITS, 2);
360 /* Check for slice end */
361 /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
362 if (h->mba_diff == MBA_STARTCODE) { // start code
363 h->gob_start_code_skipped = 1;
366 } while (h->mba_diff == MBA_STUFFING); // stuffing
368 if (h->mba_diff < 0) {
369 if (get_bits_left(&s->gb) <= 7)
372 av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
377 h->current_mba += h->mba_diff;
379 if (h->current_mba > MBA_STUFFING)
382 s->mb_x = ((h->gob_number - 1) % 2) * 11 + ((h->current_mba - 1) % 11);
383 s->mb_y = ((h->gob_number - 1) / 2) * 3 + ((h->current_mba - 1) / 11);
384 xy = s->mb_x + s->mb_y * s->mb_stride;
385 ff_init_block_index(s);
386 ff_update_block_index(s);
389 h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
391 av_log(s->avctx, AV_LOG_ERROR, "Invalid mtype index %d\n",
395 av_assert0(h->mtype < FF_ARRAY_ELEMS(ff_h261_mtype_map));
396 h->mtype = ff_h261_mtype_map[h->mtype];
399 if (IS_QUANT(h->mtype))
400 ff_set_qscale(s, get_bits(&s->gb, 5));
402 s->mb_intra = IS_INTRA4x4(h->mtype);
405 if (IS_16X16(h->mtype)) {
406 /* Motion vector data is included for all MC macroblocks. MVD is
407 * obtained from the macroblock vector by subtracting the vector
408 * of the preceding macroblock. For this calculation the vector
409 * of the preceding macroblock is regarded as zero in the
410 * following three situations:
411 * 1) evaluating MVD for macroblocks 1, 12 and 23;
412 * 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
413 * 3) MTYPE of the previous macroblock was not MC. */
414 if ((h->current_mba == 1) || (h->current_mba == 12) ||
415 (h->current_mba == 23) || (h->mba_diff != 1)) {
420 h->current_mv_x = decode_mv_component(&s->gb, h->current_mv_x);
421 h->current_mv_y = decode_mv_component(&s->gb, h->current_mv_y);
428 if (HAS_CBP(h->mtype))
429 cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 1) + 1;
432 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
437 s->mv_dir = MV_DIR_FORWARD;
438 s->mv_type = MV_TYPE_16X16;
439 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
440 s->mv[0][0][0] = h->current_mv_x * 2; // gets divided by 2 in motion compensation
441 s->mv[0][0][1] = h->current_mv_y * 2;
443 if (s->current_picture.motion_val[0]) {
444 int b_stride = 2*s->mb_width + 1;
445 int b_xy = 2 * s->mb_x + (2 * s->mb_y) * b_stride;
446 s->current_picture.motion_val[0][b_xy][0] = s->mv[0][0][0];
447 s->current_picture.motion_val[0][b_xy][1] = s->mv[0][0][1];
451 /* decode each block */
452 if (s->mb_intra || HAS_CBP(h->mtype)) {
453 s->bdsp.clear_blocks(s->block[0]);
454 for (i = 0; i < 6; i++) {
455 if (h261_decode_block(h, s->block[i], i, cbp & 32) < 0)
460 for (i = 0; i < 6; i++)
461 s->block_last_index[i] = -1;
464 ff_mpv_reconstruct_mb(s, s->block);
470 * Decode the H.261 picture header.
471 * @return <0 if no startcode found
473 static int h261_decode_picture_header(H261Context *h)
475 MpegEncContext *const s = &h->s;
477 uint32_t startcode = 0;
479 for (i = get_bits_left(&s->gb); i > 24; i -= 1) {
480 startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
482 if (startcode == 0x10)
486 if (startcode != 0x10) {
487 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
491 /* temporal reference */
492 i = get_bits(&s->gb, 5); /* picture timestamp */
493 if (i < (s->picture_number & 31))
495 s->picture_number = (s->picture_number & ~31) + i;
497 s->avctx->framerate = (AVRational) { 30000, 1001 };
499 /* PTYPE starts here */
500 skip_bits1(&s->gb); /* split screen off */
501 skip_bits1(&s->gb); /* camera off */
502 skip_bits1(&s->gb); /* freeze picture release off */
504 format = get_bits1(&s->gb);
506 // only 2 formats possible
507 if (format == 0) { // QCIF
519 s->mb_num = s->mb_width * s->mb_height;
521 skip_bits1(&s->gb); /* still image mode off */
522 skip_bits1(&s->gb); /* Reserved */
525 if (skip_1stop_8data_bits(&s->gb) < 0)
526 return AVERROR_INVALIDDATA;
528 /* H.261 has no I-frames, but if we pass AV_PICTURE_TYPE_I for the first
529 * frame, the codec crashes if it does not contain all I-blocks
530 * (e.g. when a packet is lost). */
531 s->pict_type = AV_PICTURE_TYPE_P;
537 static int h261_decode_gob(H261Context *h)
539 MpegEncContext *const s = &h->s;
541 ff_set_qscale(s, s->qscale);
544 while (h->current_mba <= MBA_STUFFING) {
547 ret = h261_decode_mb(h);
549 if (ret == SLICE_END) {
550 h261_decode_mb_skipped(h, h->current_mba, 33);
553 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n",
554 s->mb_x + s->mb_y * s->mb_stride);
558 h261_decode_mb_skipped(h,
559 h->current_mba - h->mba_diff,
567 * returns the number of bytes consumed for building the current frame
569 static int get_consumed_bytes(MpegEncContext *s, int buf_size)
571 int pos = get_bits_count(&s->gb) >> 3;
573 pos = 1; // avoid infinite loops (i doubt that is needed but ...)
574 if (pos + 10 > buf_size)
575 pos = buf_size; // oops ;)
580 static int h261_decode_frame(AVCodecContext *avctx, void *data,
581 int *got_frame, AVPacket *avpkt)
583 const uint8_t *buf = avpkt->data;
584 int buf_size = avpkt->size;
585 H261Context *h = avctx->priv_data;
586 MpegEncContext *s = &h->s;
588 AVFrame *pict = data;
590 ff_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
591 ff_dlog(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
593 h->gob_start_code_skipped = 0;
596 init_get_bits(&s->gb, buf, buf_size * 8);
598 if (!s->context_initialized)
599 // we need the IDCT permutation for reading a custom matrix
602 ret = h261_decode_picture_header(h);
604 /* skip if the header was thrashed */
606 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
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;
617 if (!s->context_initialized) {
618 if ((ret = ff_mpv_common_init(s)) < 0)
621 ret = ff_set_dimensions(avctx, s->width, s->height);
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;
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);
637 if (ff_mpv_frame_start(s, avctx) < 0)
640 ff_mpeg_er_frame_start(s);
642 /* decode each macroblock */
646 while (h->gob_number < (s->mb_height == 18 ? 12 : 5)) {
647 if (h261_resync(h) < 0)
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);
656 if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
658 ff_print_debug_info(s, s->current_picture_ptr, pict);
662 return get_consumed_bytes(s, buf_size);
665 static av_cold int h261_decode_end(AVCodecContext *avctx)
667 H261Context *h = avctx->priv_data;
668 MpegEncContext *s = &h->s;
670 ff_mpv_common_end(s);
674 AVCodec ff_h261_decoder = {
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 = AV_CODEC_CAP_DR1,
684 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,