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