You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

667 lines
19KB

  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 Libav.
  7. *
  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.
  12. *
  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.
  17. *
  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
  21. */
  22. /**
  23. * @file
  24. * H.261 decoder.
  25. */
  26. #include "avcodec.h"
  27. #include "mpegutils.h"
  28. #include "mpegvideo.h"
  29. #include "h263.h"
  30. #include "h261.h"
  31. #include "internal.h"
  32. #define H261_MBA_VLC_BITS 9
  33. #define H261_MTYPE_VLC_BITS 6
  34. #define H261_MV_VLC_BITS 7
  35. #define H261_CBP_VLC_BITS 9
  36. #define TCOEFF_VLC_BITS 9
  37. #define MBA_STUFFING 33
  38. #define MBA_STARTCODE 34
  39. static VLC h261_mba_vlc;
  40. static VLC h261_mtype_vlc;
  41. static VLC h261_mv_vlc;
  42. static VLC h261_cbp_vlc;
  43. static av_cold void h261_decode_init_vlc(H261Context *h)
  44. {
  45. static int done = 0;
  46. if (!done) {
  47. done = 1;
  48. INIT_VLC_STATIC(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
  49. ff_h261_mba_bits, 1, 1,
  50. ff_h261_mba_code, 1, 1, 662);
  51. INIT_VLC_STATIC(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
  52. ff_h261_mtype_bits, 1, 1,
  53. ff_h261_mtype_code, 1, 1, 80);
  54. INIT_VLC_STATIC(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
  55. &ff_h261_mv_tab[0][1], 2, 1,
  56. &ff_h261_mv_tab[0][0], 2, 1, 144);
  57. INIT_VLC_STATIC(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
  58. &ff_h261_cbp_tab[0][1], 2, 1,
  59. &ff_h261_cbp_tab[0][0], 2, 1, 512);
  60. INIT_VLC_RL(ff_h261_rl_tcoeff, 552);
  61. }
  62. }
  63. static av_cold int h261_decode_init(AVCodecContext *avctx)
  64. {
  65. H261Context *h = avctx->priv_data;
  66. MpegEncContext *const s = &h->s;
  67. // set defaults
  68. ff_MPV_decode_defaults(s);
  69. s->avctx = avctx;
  70. s->width = s->avctx->coded_width;
  71. s->height = s->avctx->coded_height;
  72. s->codec_id = s->avctx->codec->id;
  73. s->out_format = FMT_H261;
  74. s->low_delay = 1;
  75. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  76. s->codec_id = avctx->codec->id;
  77. ff_h261_common_init();
  78. h261_decode_init_vlc(h);
  79. h->gob_start_code_skipped = 0;
  80. return 0;
  81. }
  82. /**
  83. * Decode the group of blocks header or slice header.
  84. * @return <0 if an error occurred
  85. */
  86. static int h261_decode_gob_header(H261Context *h)
  87. {
  88. unsigned int val;
  89. MpegEncContext *const s = &h->s;
  90. if (!h->gob_start_code_skipped) {
  91. /* Check for GOB Start Code */
  92. val = show_bits(&s->gb, 15);
  93. if (val)
  94. return -1;
  95. /* We have a GBSC */
  96. skip_bits(&s->gb, 16);
  97. }
  98. h->gob_start_code_skipped = 0;
  99. h->gob_number = get_bits(&s->gb, 4); /* GN */
  100. s->qscale = get_bits(&s->gb, 5); /* GQUANT */
  101. /* Check if gob_number is valid */
  102. if (s->mb_height == 18) { // CIF
  103. if ((h->gob_number <= 0) || (h->gob_number > 12))
  104. return -1;
  105. } else { // QCIF
  106. if ((h->gob_number != 1) && (h->gob_number != 3) &&
  107. (h->gob_number != 5))
  108. return -1;
  109. }
  110. /* GEI */
  111. while (get_bits1(&s->gb) != 0)
  112. skip_bits(&s->gb, 8);
  113. if (s->qscale == 0) {
  114. av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\n");
  115. if (s->avctx->err_recognition & AV_EF_BITSTREAM)
  116. return -1;
  117. }
  118. /* For the first transmitted macroblock in a GOB, MBA is the absolute
  119. * address. For subsequent macroblocks, MBA is the difference between
  120. * the absolute addresses of the macroblock and the last transmitted
  121. * macroblock. */
  122. h->current_mba = 0;
  123. h->mba_diff = 0;
  124. return 0;
  125. }
  126. /**
  127. * Decode the group of blocks / video packet header.
  128. * @return <0 if no resync found
  129. */
  130. static int h261_resync(H261Context *h)
  131. {
  132. MpegEncContext *const s = &h->s;
  133. int left, ret;
  134. if (h->gob_start_code_skipped) {
  135. ret = h261_decode_gob_header(h);
  136. if (ret >= 0)
  137. return 0;
  138. } else {
  139. if (show_bits(&s->gb, 15) == 0) {
  140. ret = h261_decode_gob_header(h);
  141. if (ret >= 0)
  142. return 0;
  143. }
  144. // OK, it is not where it is supposed to be ...
  145. s->gb = s->last_resync_gb;
  146. align_get_bits(&s->gb);
  147. left = get_bits_left(&s->gb);
  148. for (; left > 15 + 1 + 4 + 5; left -= 8) {
  149. if (show_bits(&s->gb, 15) == 0) {
  150. GetBitContext bak = s->gb;
  151. ret = h261_decode_gob_header(h);
  152. if (ret >= 0)
  153. return 0;
  154. s->gb = bak;
  155. }
  156. skip_bits(&s->gb, 8);
  157. }
  158. }
  159. return -1;
  160. }
  161. /**
  162. * Decode skipped macroblocks.
  163. * @return 0
  164. */
  165. static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2)
  166. {
  167. MpegEncContext *const s = &h->s;
  168. int i;
  169. s->mb_intra = 0;
  170. for (i = mba1; i < mba2; i++) {
  171. int j, xy;
  172. s->mb_x = ((h->gob_number - 1) % 2) * 11 + i % 11;
  173. s->mb_y = ((h->gob_number - 1) / 2) * 3 + i / 11;
  174. xy = s->mb_x + s->mb_y * s->mb_stride;
  175. ff_init_block_index(s);
  176. ff_update_block_index(s);
  177. for (j = 0; j < 6; j++)
  178. s->block_last_index[j] = -1;
  179. s->mv_dir = MV_DIR_FORWARD;
  180. s->mv_type = MV_TYPE_16X16;
  181. s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
  182. s->mv[0][0][0] = 0;
  183. s->mv[0][0][1] = 0;
  184. s->mb_skipped = 1;
  185. h->mtype &= ~MB_TYPE_H261_FIL;
  186. ff_MPV_decode_mb(s, s->block);
  187. }
  188. return 0;
  189. }
  190. static const int mvmap[17] = {
  191. 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16
  192. };
  193. static int decode_mv_component(GetBitContext *gb, int v)
  194. {
  195. int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
  196. /* check if mv_diff is valid */
  197. if (mv_diff < 0)
  198. return v;
  199. mv_diff = mvmap[mv_diff];
  200. if (mv_diff && !get_bits1(gb))
  201. mv_diff = -mv_diff;
  202. v += mv_diff;
  203. if (v <= -16)
  204. v += 32;
  205. else if (v >= 16)
  206. v -= 32;
  207. return v;
  208. }
  209. /**
  210. * Decode a macroblock.
  211. * @return <0 if an error occurred
  212. */
  213. static int h261_decode_block(H261Context *h, int16_t *block, int n, int coded)
  214. {
  215. MpegEncContext *const s = &h->s;
  216. int code, level, i, j, run;
  217. RLTable *rl = &ff_h261_rl_tcoeff;
  218. const uint8_t *scan_table;
  219. /* For the variable length encoding there are two code tables, one being
  220. * used for the first transmitted LEVEL in INTER, INTER + MC and
  221. * INTER + MC + FIL blocks, the second for all other LEVELs except the
  222. * first one in INTRA blocks which is fixed length coded with 8 bits.
  223. * NOTE: The two code tables only differ in one VLC so we handle that
  224. * manually. */
  225. scan_table = s->intra_scantable.permutated;
  226. if (s->mb_intra) {
  227. /* DC coef */
  228. level = get_bits(&s->gb, 8);
  229. // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
  230. if ((level & 0x7F) == 0) {
  231. av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n",
  232. level, s->mb_x, s->mb_y);
  233. return -1;
  234. }
  235. /* The code 1000 0000 is not used, the reconstruction level of 1024
  236. * being coded as 1111 1111. */
  237. if (level == 255)
  238. level = 128;
  239. block[0] = level;
  240. i = 1;
  241. } else if (coded) {
  242. // Run Level Code
  243. // EOB Not possible for first level when cbp is available (that's why the table is different)
  244. // 0 1 1s
  245. // * * 0*
  246. int check = show_bits(&s->gb, 2);
  247. i = 0;
  248. if (check & 0x2) {
  249. skip_bits(&s->gb, 2);
  250. block[0] = (check & 0x1) ? -1 : 1;
  251. i = 1;
  252. }
  253. } else {
  254. i = 0;
  255. }
  256. if (!coded) {
  257. s->block_last_index[n] = i - 1;
  258. return 0;
  259. }
  260. for (;;) {
  261. code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
  262. if (code < 0) {
  263. av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n",
  264. s->mb_x, s->mb_y);
  265. return -1;
  266. }
  267. if (code == rl->n) {
  268. /* escape */
  269. /* The remaining combinations of (run, level) are encoded with a
  270. * 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits
  271. * level. */
  272. run = get_bits(&s->gb, 6);
  273. level = get_sbits(&s->gb, 8);
  274. } else if (code == 0) {
  275. break;
  276. } else {
  277. run = rl->table_run[code];
  278. level = rl->table_level[code];
  279. if (get_bits1(&s->gb))
  280. level = -level;
  281. }
  282. i += run;
  283. if (i >= 64) {
  284. av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n",
  285. s->mb_x, s->mb_y);
  286. return -1;
  287. }
  288. j = scan_table[i];
  289. block[j] = level;
  290. i++;
  291. }
  292. s->block_last_index[n] = i - 1;
  293. return 0;
  294. }
  295. static int h261_decode_mb(H261Context *h)
  296. {
  297. MpegEncContext *const s = &h->s;
  298. int i, cbp, xy;
  299. cbp = 63;
  300. // Read mba
  301. do {
  302. h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table,
  303. H261_MBA_VLC_BITS, 2);
  304. /* Check for slice end */
  305. /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
  306. if (h->mba_diff == MBA_STARTCODE) { // start code
  307. h->gob_start_code_skipped = 1;
  308. return SLICE_END;
  309. }
  310. } while (h->mba_diff == MBA_STUFFING); // stuffing
  311. if (h->mba_diff < 0) {
  312. if (get_bits_left(&s->gb) <= 7)
  313. return SLICE_END;
  314. av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
  315. return SLICE_ERROR;
  316. }
  317. h->mba_diff += 1;
  318. h->current_mba += h->mba_diff;
  319. if (h->current_mba > MBA_STUFFING)
  320. return SLICE_ERROR;
  321. s->mb_x = ((h->gob_number - 1) % 2) * 11 + ((h->current_mba - 1) % 11);
  322. s->mb_y = ((h->gob_number - 1) / 2) * 3 + ((h->current_mba - 1) / 11);
  323. xy = s->mb_x + s->mb_y * s->mb_stride;
  324. ff_init_block_index(s);
  325. ff_update_block_index(s);
  326. // Read mtype
  327. h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
  328. if (h->mtype < 0 || h->mtype >= FF_ARRAY_ELEMS(ff_h261_mtype_map)) {
  329. av_log(s->avctx, AV_LOG_ERROR, "Invalid mtype index %d\n",
  330. h->mtype);
  331. return SLICE_ERROR;
  332. }
  333. h->mtype = ff_h261_mtype_map[h->mtype];
  334. // Read mquant
  335. if (IS_QUANT(h->mtype))
  336. ff_set_qscale(s, get_bits(&s->gb, 5));
  337. s->mb_intra = IS_INTRA4x4(h->mtype);
  338. // Read mv
  339. if (IS_16X16(h->mtype)) {
  340. /* Motion vector data is included for all MC macroblocks. MVD is
  341. * obtained from the macroblock vector by subtracting the vector
  342. * of the preceding macroblock. For this calculation the vector
  343. * of the preceding macroblock is regarded as zero in the
  344. * following three situations:
  345. * 1) evaluating MVD for macroblocks 1, 12 and 23;
  346. * 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
  347. * 3) MTYPE of the previous macroblock was not MC. */
  348. if ((h->current_mba == 1) || (h->current_mba == 12) ||
  349. (h->current_mba == 23) || (h->mba_diff != 1)) {
  350. h->current_mv_x = 0;
  351. h->current_mv_y = 0;
  352. }
  353. h->current_mv_x = decode_mv_component(&s->gb, h->current_mv_x);
  354. h->current_mv_y = decode_mv_component(&s->gb, h->current_mv_y);
  355. } else {
  356. h->current_mv_x = 0;
  357. h->current_mv_y = 0;
  358. }
  359. // Read cbp
  360. if (HAS_CBP(h->mtype))
  361. cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
  362. if (s->mb_intra) {
  363. s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
  364. goto intra;
  365. }
  366. //set motion vectors
  367. s->mv_dir = MV_DIR_FORWARD;
  368. s->mv_type = MV_TYPE_16X16;
  369. s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
  370. s->mv[0][0][0] = h->current_mv_x * 2; // gets divided by 2 in motion compensation
  371. s->mv[0][0][1] = h->current_mv_y * 2;
  372. intra:
  373. /* decode each block */
  374. if (s->mb_intra || HAS_CBP(h->mtype)) {
  375. s->dsp.clear_blocks(s->block[0]);
  376. for (i = 0; i < 6; i++) {
  377. if (h261_decode_block(h, s->block[i], i, cbp & 32) < 0)
  378. return SLICE_ERROR;
  379. cbp += cbp;
  380. }
  381. } else {
  382. for (i = 0; i < 6; i++)
  383. s->block_last_index[i] = -1;
  384. }
  385. ff_MPV_decode_mb(s, s->block);
  386. return SLICE_OK;
  387. }
  388. /**
  389. * Decode the H.261 picture header.
  390. * @return <0 if no startcode found
  391. */
  392. static int h261_decode_picture_header(H261Context *h)
  393. {
  394. MpegEncContext *const s = &h->s;
  395. int format, i;
  396. uint32_t startcode = 0;
  397. for (i = get_bits_left(&s->gb); i > 24; i -= 1) {
  398. startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
  399. if (startcode == 0x10)
  400. break;
  401. }
  402. if (startcode != 0x10) {
  403. av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
  404. return -1;
  405. }
  406. /* temporal reference */
  407. i = get_bits(&s->gb, 5); /* picture timestamp */
  408. if (i < (s->picture_number & 31))
  409. i += 32;
  410. s->picture_number = (s->picture_number & ~31) + i;
  411. s->avctx->time_base = (AVRational) { 1001, 30000 };
  412. /* PTYPE starts here */
  413. skip_bits1(&s->gb); /* split screen off */
  414. skip_bits1(&s->gb); /* camera off */
  415. skip_bits1(&s->gb); /* freeze picture release off */
  416. format = get_bits1(&s->gb);
  417. // only 2 formats possible
  418. if (format == 0) { // QCIF
  419. s->width = 176;
  420. s->height = 144;
  421. s->mb_width = 11;
  422. s->mb_height = 9;
  423. } else { // CIF
  424. s->width = 352;
  425. s->height = 288;
  426. s->mb_width = 22;
  427. s->mb_height = 18;
  428. }
  429. s->mb_num = s->mb_width * s->mb_height;
  430. skip_bits1(&s->gb); /* still image mode off */
  431. skip_bits1(&s->gb); /* Reserved */
  432. /* PEI */
  433. while (get_bits1(&s->gb) != 0)
  434. skip_bits(&s->gb, 8);
  435. /* H.261 has no I-frames, but if we pass AV_PICTURE_TYPE_I for the first
  436. * frame, the codec crashes if it does not contain all I-blocks
  437. * (e.g. when a packet is lost). */
  438. s->pict_type = AV_PICTURE_TYPE_P;
  439. h->gob_number = 0;
  440. return 0;
  441. }
  442. static int h261_decode_gob(H261Context *h)
  443. {
  444. MpegEncContext *const s = &h->s;
  445. ff_set_qscale(s, s->qscale);
  446. /* decode mb's */
  447. while (h->current_mba <= MBA_STUFFING) {
  448. int ret;
  449. /* DCT & quantize */
  450. ret = h261_decode_mb(h);
  451. if (ret < 0) {
  452. if (ret == SLICE_END) {
  453. h261_decode_mb_skipped(h, h->current_mba, 33);
  454. return 0;
  455. }
  456. av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n",
  457. s->mb_x + s->mb_y * s->mb_stride);
  458. return -1;
  459. }
  460. h261_decode_mb_skipped(h,
  461. h->current_mba - h->mba_diff,
  462. h->current_mba - 1);
  463. }
  464. return -1;
  465. }
  466. /**
  467. * returns the number of bytes consumed for building the current frame
  468. */
  469. static int get_consumed_bytes(MpegEncContext *s, int buf_size)
  470. {
  471. int pos = get_bits_count(&s->gb) >> 3;
  472. if (pos == 0)
  473. pos = 1; // avoid infinite loops (i doubt that is needed but ...)
  474. if (pos + 10 > buf_size)
  475. pos = buf_size; // oops ;)
  476. return pos;
  477. }
  478. static int h261_decode_frame(AVCodecContext *avctx, void *data,
  479. int *got_frame, AVPacket *avpkt)
  480. {
  481. const uint8_t *buf = avpkt->data;
  482. int buf_size = avpkt->size;
  483. H261Context *h = avctx->priv_data;
  484. MpegEncContext *s = &h->s;
  485. int ret;
  486. AVFrame *pict = data;
  487. av_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
  488. av_dlog(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  489. s->flags = avctx->flags;
  490. s->flags2 = avctx->flags2;
  491. h->gob_start_code_skipped = 0;
  492. retry:
  493. init_get_bits(&s->gb, buf, buf_size * 8);
  494. if (!s->context_initialized)
  495. // we need the IDCT permutaton for reading a custom matrix
  496. if (ff_MPV_common_init(s) < 0)
  497. return -1;
  498. ret = h261_decode_picture_header(h);
  499. /* skip if the header was thrashed */
  500. if (ret < 0) {
  501. av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
  502. return -1;
  503. }
  504. if (s->width != avctx->coded_width || s->height != avctx->coded_height) {
  505. ParseContext pc = s->parse_context; // FIXME move this demuxing hack to libavformat
  506. s->parse_context.buffer = 0;
  507. ff_MPV_common_end(s);
  508. s->parse_context = pc;
  509. }
  510. if (!s->context_initialized) {
  511. ret = ff_set_dimensions(avctx, s->width, s->height);
  512. if (ret < 0)
  513. return ret;
  514. goto retry;
  515. }
  516. // for skipping the frame
  517. s->current_picture.f->pict_type = s->pict_type;
  518. s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  519. if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
  520. (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
  521. avctx->skip_frame >= AVDISCARD_ALL)
  522. return get_consumed_bytes(s, buf_size);
  523. if (ff_MPV_frame_start(s, avctx) < 0)
  524. return -1;
  525. ff_mpeg_er_frame_start(s);
  526. /* decode each macroblock */
  527. s->mb_x = 0;
  528. s->mb_y = 0;
  529. while (h->gob_number < (s->mb_height == 18 ? 12 : 5)) {
  530. if (h261_resync(h) < 0)
  531. break;
  532. h261_decode_gob(h);
  533. }
  534. ff_MPV_frame_end(s);
  535. assert(s->current_picture.f->pict_type == s->current_picture_ptr->f->pict_type);
  536. assert(s->current_picture.f->pict_type == s->pict_type);
  537. if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
  538. return ret;
  539. ff_print_debug_info(s, s->current_picture_ptr);
  540. *got_frame = 1;
  541. return get_consumed_bytes(s, buf_size);
  542. }
  543. static av_cold int h261_decode_end(AVCodecContext *avctx)
  544. {
  545. H261Context *h = avctx->priv_data;
  546. MpegEncContext *s = &h->s;
  547. ff_MPV_common_end(s);
  548. return 0;
  549. }
  550. AVCodec ff_h261_decoder = {
  551. .name = "h261",
  552. .long_name = NULL_IF_CONFIG_SMALL("H.261"),
  553. .type = AVMEDIA_TYPE_VIDEO,
  554. .id = AV_CODEC_ID_H261,
  555. .priv_data_size = sizeof(H261Context),
  556. .init = h261_decode_init,
  557. .close = h261_decode_end,
  558. .decode = h261_decode_frame,
  559. .capabilities = CODEC_CAP_DR1,
  560. };