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.

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