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;
84 ff_thread_once(&init_static_once, h261_decode_init_static);
90 * Decode the group of blocks header or slice header.
91 * @return <0 if an error occurred
93 static int h261_decode_gob_header(H261Context *h)
96 MpegEncContext *const s = &h->s;
98 if (!h->gob_start_code_skipped) {
99 /* Check for GOB Start Code */
100 val = show_bits(&s->gb, 15);
105 skip_bits(&s->gb, 16);
108 h->gob_start_code_skipped = 0;
110 h->gob_number = get_bits(&s->gb, 4); /* GN */
111 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
113 /* Check if gob_number is valid */
114 if (s->mb_height == 18) { // CIF
115 if ((h->gob_number <= 0) || (h->gob_number > 12))
118 if ((h->gob_number != 1) && (h->gob_number != 3) &&
119 (h->gob_number != 5))
124 if (skip_1stop_8data_bits(&s->gb) < 0)
125 return AVERROR_INVALIDDATA;
127 if (s->qscale == 0) {
128 av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\n");
129 if (s->avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
133 /* For the first transmitted macroblock in a GOB, MBA is the absolute
134 * address. For subsequent macroblocks, MBA is the difference between
135 * the absolute addresses of the macroblock and the last transmitted
144 * Decode the group of blocks / video packet header.
145 * @return <0 if no resync found
147 static int h261_resync(H261Context *h)
149 MpegEncContext *const s = &h->s;
152 if (h->gob_start_code_skipped) {
153 ret = h261_decode_gob_header(h);
157 if (show_bits(&s->gb, 15) == 0) {
158 ret = h261_decode_gob_header(h);
162 // OK, it is not where it is supposed to be ...
163 s->gb = s->last_resync_gb;
164 align_get_bits(&s->gb);
165 left = get_bits_left(&s->gb);
167 for (; left > 15 + 1 + 4 + 5; left -= 8) {
168 if (show_bits(&s->gb, 15) == 0) {
169 GetBitContext bak = s->gb;
171 ret = h261_decode_gob_header(h);
177 skip_bits(&s->gb, 8);
185 * Decode skipped macroblocks.
188 static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2)
190 MpegEncContext *const s = &h->s;
195 for (i = mba1; i < mba2; i++) {
198 s->mb_x = ((h->gob_number - 1) % 2) * 11 + i % 11;
199 s->mb_y = ((h->gob_number - 1) / 2) * 3 + i / 11;
200 xy = s->mb_x + s->mb_y * s->mb_stride;
201 ff_init_block_index(s);
202 ff_update_block_index(s);
204 for (j = 0; j < 6; j++)
205 s->block_last_index[j] = -1;
207 s->mv_dir = MV_DIR_FORWARD;
208 s->mv_type = MV_TYPE_16X16;
209 s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
213 h->mtype &= ~MB_TYPE_H261_FIL;
215 if (s->current_picture.motion_val[0]) {
216 int b_stride = 2*s->mb_width + 1;
217 int b_xy = 2 * s->mb_x + (2 * s->mb_y) * b_stride;
218 s->current_picture.motion_val[0][b_xy][0] = s->mv[0][0][0];
219 s->current_picture.motion_val[0][b_xy][1] = s->mv[0][0][1];
222 ff_mpv_reconstruct_mb(s, s->block);
228 static const int mvmap[17] = {
229 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16
232 static int decode_mv_component(GetBitContext *gb, int v)
234 int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
236 /* check if mv_diff is valid */
240 mv_diff = mvmap[mv_diff];
242 if (mv_diff && !get_bits1(gb))
255 * Decode a macroblock.
256 * @return <0 if an error occurred
258 static int h261_decode_block(H261Context *h, int16_t *block, int n, int coded)
260 MpegEncContext *const s = &h->s;
261 int level, i, j, run;
262 RLTable *rl = &ff_h261_rl_tcoeff;
263 const uint8_t *scan_table;
265 /* For the variable length encoding there are two code tables, one being
266 * used for the first transmitted LEVEL in INTER, INTER + MC and
267 * INTER + MC + FIL blocks, the second for all other LEVELs except the
268 * first one in INTRA blocks which is fixed length coded with 8 bits.
269 * NOTE: The two code tables only differ in one VLC so we handle that
271 scan_table = s->intra_scantable.permutated;
274 level = get_bits(&s->gb, 8);
275 // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
276 if ((level & 0x7F) == 0) {
277 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n",
278 level, s->mb_x, s->mb_y);
281 /* The code 1000 0000 is not used, the reconstruction level of 1024
282 * being coded as 1111 1111. */
289 // EOB Not possible for first level when cbp is available (that's why the table is different)
292 int check = show_bits(&s->gb, 2);
295 skip_bits(&s->gb, 2);
296 block[0] = (check & 0x1) ? -1 : 1;
303 s->block_last_index[n] = i - 1;
307 OPEN_READER(re, &s->gb);
308 i--; // offset by -1 to allow direct indexing of scan_table
310 UPDATE_CACHE(re, &s->gb);
311 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TCOEFF_VLC_BITS, 2, 0);
314 CLOSE_READER(re, &s->gb);
315 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n",
320 /* The remaining combinations of (run, level) are encoded with a
321 * 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits
323 run = SHOW_UBITS(re, &s->gb, 6) + 1;
324 SKIP_CACHE(re, &s->gb, 6);
325 level = SHOW_SBITS(re, &s->gb, 8);
326 SKIP_COUNTER(re, &s->gb, 6 + 8);
327 } else if (level == 0) {
330 if (SHOW_UBITS(re, &s->gb, 1))
332 SKIP_COUNTER(re, &s->gb, 1);
336 CLOSE_READER(re, &s->gb);
337 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n",
344 CLOSE_READER(re, &s->gb);
346 s->block_last_index[n] = i;
350 static int h261_decode_mb(H261Context *h)
352 MpegEncContext *const s = &h->s;
358 h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table,
359 H261_MBA_VLC_BITS, 2);
361 /* Check for slice end */
362 /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
363 if (h->mba_diff == MBA_STARTCODE) { // start code
364 h->gob_start_code_skipped = 1;
367 } while (h->mba_diff == MBA_STUFFING); // stuffing
369 if (h->mba_diff < 0) {
370 if (get_bits_left(&s->gb) <= 7)
373 av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
378 h->current_mba += h->mba_diff;
380 if (h->current_mba > MBA_STUFFING)
383 s->mb_x = ((h->gob_number - 1) % 2) * 11 + ((h->current_mba - 1) % 11);
384 s->mb_y = ((h->gob_number - 1) / 2) * 3 + ((h->current_mba - 1) / 11);
385 xy = s->mb_x + s->mb_y * s->mb_stride;
386 ff_init_block_index(s);
387 ff_update_block_index(s);
390 h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
392 av_log(s->avctx, AV_LOG_ERROR, "Invalid mtype index %d\n",
396 av_assert0(h->mtype < FF_ARRAY_ELEMS(ff_h261_mtype_map));
397 h->mtype = ff_h261_mtype_map[h->mtype];
400 if (IS_QUANT(h->mtype))
401 ff_set_qscale(s, get_bits(&s->gb, 5));
403 s->mb_intra = IS_INTRA4x4(h->mtype);
406 if (IS_16X16(h->mtype)) {
407 /* Motion vector data is included for all MC macroblocks. MVD is
408 * obtained from the macroblock vector by subtracting the vector
409 * of the preceding macroblock. For this calculation the vector
410 * of the preceding macroblock is regarded as zero in the
411 * following three situations:
412 * 1) evaluating MVD for macroblocks 1, 12 and 23;
413 * 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
414 * 3) MTYPE of the previous macroblock was not MC. */
415 if ((h->current_mba == 1) || (h->current_mba == 12) ||
416 (h->current_mba == 23) || (h->mba_diff != 1)) {
421 h->current_mv_x = decode_mv_component(&s->gb, h->current_mv_x);
422 h->current_mv_y = decode_mv_component(&s->gb, h->current_mv_y);
429 if (HAS_CBP(h->mtype))
430 cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 1) + 1;
433 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
438 s->mv_dir = MV_DIR_FORWARD;
439 s->mv_type = MV_TYPE_16X16;
440 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
441 s->mv[0][0][0] = h->current_mv_x * 2; // gets divided by 2 in motion compensation
442 s->mv[0][0][1] = h->current_mv_y * 2;
444 if (s->current_picture.motion_val[0]) {
445 int b_stride = 2*s->mb_width + 1;
446 int b_xy = 2 * s->mb_x + (2 * s->mb_y) * b_stride;
447 s->current_picture.motion_val[0][b_xy][0] = s->mv[0][0][0];
448 s->current_picture.motion_val[0][b_xy][1] = s->mv[0][0][1];
452 /* decode each block */
453 if (s->mb_intra || HAS_CBP(h->mtype)) {
454 s->bdsp.clear_blocks(s->block[0]);
455 for (i = 0; i < 6; i++) {
456 if (h261_decode_block(h, s->block[i], i, cbp & 32) < 0)
461 for (i = 0; i < 6; i++)
462 s->block_last_index[i] = -1;
465 ff_mpv_reconstruct_mb(s, s->block);
471 * Decode the H.261 picture header.
472 * @return <0 if no startcode found
474 static int h261_decode_picture_header(H261Context *h)
476 MpegEncContext *const s = &h->s;
478 uint32_t startcode = 0;
480 for (i = get_bits_left(&s->gb); i > 24; i -= 1) {
481 startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
483 if (startcode == 0x10)
487 if (startcode != 0x10) {
488 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
492 /* temporal reference */
493 i = get_bits(&s->gb, 5); /* picture timestamp */
494 if (i < (s->picture_number & 31))
496 s->picture_number = (s->picture_number & ~31) + i;
498 s->avctx->framerate = (AVRational) { 30000, 1001 };
500 /* PTYPE starts here */
501 skip_bits1(&s->gb); /* split screen off */
502 skip_bits1(&s->gb); /* camera off */
503 skip_bits1(&s->gb); /* freeze picture release off */
505 format = get_bits1(&s->gb);
507 // only 2 formats possible
508 if (format == 0) { // QCIF
520 s->mb_num = s->mb_width * s->mb_height;
522 skip_bits1(&s->gb); /* still image mode off */
523 skip_bits1(&s->gb); /* Reserved */
526 if (skip_1stop_8data_bits(&s->gb) < 0)
527 return AVERROR_INVALIDDATA;
529 /* H.261 has no I-frames, but if we pass AV_PICTURE_TYPE_I for the first
530 * frame, the codec crashes if it does not contain all I-blocks
531 * (e.g. when a packet is lost). */
532 s->pict_type = AV_PICTURE_TYPE_P;
538 static int h261_decode_gob(H261Context *h)
540 MpegEncContext *const s = &h->s;
542 ff_set_qscale(s, s->qscale);
545 while (h->current_mba <= MBA_STUFFING) {
548 ret = h261_decode_mb(h);
550 if (ret == SLICE_END) {
551 h261_decode_mb_skipped(h, h->current_mba, 33);
554 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n",
555 s->mb_x + s->mb_y * s->mb_stride);
559 h261_decode_mb_skipped(h,
560 h->current_mba - h->mba_diff,
568 * returns the number of bytes consumed for building the current frame
570 static int get_consumed_bytes(MpegEncContext *s, int buf_size)
572 int pos = get_bits_count(&s->gb) >> 3;
574 pos = 1; // avoid infinite loops (i doubt that is needed but ...)
575 if (pos + 10 > buf_size)
576 pos = buf_size; // oops ;)
581 static int h261_decode_frame(AVCodecContext *avctx, void *data,
582 int *got_frame, AVPacket *avpkt)
584 const uint8_t *buf = avpkt->data;
585 int buf_size = avpkt->size;
586 H261Context *h = avctx->priv_data;
587 MpegEncContext *s = &h->s;
589 AVFrame *pict = data;
591 ff_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
592 ff_dlog(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
594 h->gob_start_code_skipped = 0;
597 init_get_bits(&s->gb, buf, buf_size * 8);
599 ret = h261_decode_picture_header(h);
601 /* skip if the header was thrashed */
603 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
607 if (s->width != avctx->coded_width || s->height != avctx->coded_height) {
608 ff_mpv_common_end(s);
611 if (!s->context_initialized) {
612 if ((ret = ff_mpv_common_init(s)) < 0)
615 ret = ff_set_dimensions(avctx, s->width, s->height);
622 // for skipping the frame
623 s->current_picture.f->pict_type = s->pict_type;
624 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
626 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
627 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
628 avctx->skip_frame >= AVDISCARD_ALL)
629 return get_consumed_bytes(s, buf_size);
631 if (ff_mpv_frame_start(s, avctx) < 0)
634 ff_mpeg_er_frame_start(s);
636 /* decode each macroblock */
640 while (h->gob_number < (s->mb_height == 18 ? 12 : 5)) {
641 if (h261_resync(h) < 0)
647 av_assert0(s->current_picture.f->pict_type == s->current_picture_ptr->f->pict_type);
648 av_assert0(s->current_picture.f->pict_type == s->pict_type);
650 if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
652 ff_print_debug_info(s, s->current_picture_ptr, pict);
656 return get_consumed_bytes(s, buf_size);
659 static av_cold int h261_decode_end(AVCodecContext *avctx)
661 H261Context *h = avctx->priv_data;
662 MpegEncContext *s = &h->s;
664 ff_mpv_common_end(s);
668 const AVCodec ff_h261_decoder = {
670 .long_name = NULL_IF_CONFIG_SMALL("H.261"),
671 .type = AVMEDIA_TYPE_VIDEO,
672 .id = AV_CODEC_ID_H261,
673 .priv_data_size = sizeof(H261Context),
674 .init = h261_decode_init,
675 .close = h261_decode_end,
676 .decode = h261_decode_frame,
677 .capabilities = AV_CODEC_CAP_DR1,
678 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,