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.

669 lines
19KB

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