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.

880 lines
31KB

  1. /*
  2. * Indeo Video Interactive v4 compatible decoder
  3. * Copyright (c) 2009-2011 Maxim Poliakovski
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Indeo Video Interactive version 4 decoder
  24. *
  25. * Indeo 4 data is usually transported within .avi or .mov files.
  26. * Known FOURCCs: 'IV41'
  27. */
  28. #define BITSTREAM_READER_LE
  29. #include "avcodec.h"
  30. #include "get_bits.h"
  31. #include "dsputil.h"
  32. #include "ivi_dsp.h"
  33. #include "ivi_common.h"
  34. #include "indeo4data.h"
  35. #define IVI4_STREAM_ANALYSER 0
  36. #define IVI4_DEBUG_CHECKSUM 0
  37. /**
  38. * Indeo 4 frame types.
  39. */
  40. enum {
  41. FRAMETYPE_INTRA = 0,
  42. FRAMETYPE_BIDIR1 = 1, ///< bidirectional frame
  43. FRAMETYPE_INTER = 2, ///< non-droppable P-frame
  44. FRAMETYPE_BIDIR = 3, ///< bidirectional frame
  45. FRAMETYPE_INTER_NOREF = 4, ///< droppable P-frame
  46. FRAMETYPE_NULL_FIRST = 5, ///< empty frame with no data
  47. FRAMETYPE_NULL_LAST = 6 ///< empty frame with no data
  48. };
  49. #define IVI4_PIC_SIZE_ESC 7
  50. typedef struct {
  51. GetBitContext gb;
  52. AVFrame frame;
  53. RVMapDesc rvmap_tabs[9]; ///< local corrected copy of the static rvmap tables
  54. uint32_t frame_num;
  55. int frame_type;
  56. int prev_frame_type; ///< frame type of the previous frame
  57. uint32_t data_size; ///< size of the frame data in bytes from picture header
  58. int is_scalable;
  59. int transp_status; ///< transparency mode status: 1 - enabled
  60. IVIPicConfig pic_conf;
  61. IVIPlaneDesc planes[3]; ///< color planes
  62. int buf_switch; ///< used to switch between three buffers
  63. int dst_buf; ///< buffer index for the currently decoded frame
  64. int ref_buf; ///< inter frame reference buffer index
  65. IVIHuffTab mb_vlc; ///< current macroblock table descriptor
  66. IVIHuffTab blk_vlc; ///< current block table descriptor
  67. uint16_t checksum; ///< frame checksum
  68. uint8_t rvmap_sel;
  69. uint8_t in_imf;
  70. uint8_t in_q; ///< flag for explicitly stored quantiser delta
  71. uint8_t pic_glob_quant;
  72. uint8_t unknown1;
  73. #if IVI4_STREAM_ANALYSER
  74. uint8_t has_b_frames;
  75. uint8_t has_transp;
  76. uint8_t uses_tiling;
  77. uint8_t uses_haar;
  78. uint8_t uses_fullpel;
  79. #endif
  80. } IVI4DecContext;
  81. static const struct {
  82. InvTransformPtr *inv_trans;
  83. DCTransformPtr *dc_trans;
  84. int is_2d_trans;
  85. } transforms[18] = {
  86. { ff_ivi_inverse_haar_8x8, ff_ivi_dc_haar_2d, 1 },
  87. { NULL, NULL, 0 }, /* inverse Haar 8x1 */
  88. { NULL, NULL, 0 }, /* inverse Haar 1x8 */
  89. { ff_ivi_put_pixels_8x8, ff_ivi_put_dc_pixel_8x8, 1 },
  90. { ff_ivi_inverse_slant_8x8, ff_ivi_dc_slant_2d, 1 },
  91. { ff_ivi_row_slant8, ff_ivi_dc_row_slant, 1 },
  92. { ff_ivi_col_slant8, ff_ivi_dc_col_slant, 1 },
  93. { NULL, NULL, 0 }, /* inverse DCT 8x8 */
  94. { NULL, NULL, 0 }, /* inverse DCT 8x1 */
  95. { NULL, NULL, 0 }, /* inverse DCT 1x8 */
  96. { NULL, NULL, 0 }, /* inverse Haar 4x4 */
  97. { ff_ivi_inverse_slant_4x4, ff_ivi_dc_slant_2d, 1 },
  98. { NULL, NULL, 0 }, /* no transform 4x4 */
  99. { NULL, NULL, 0 }, /* inverse Haar 1x4 */
  100. { NULL, NULL, 0 }, /* inverse Haar 4x1 */
  101. { NULL, NULL, 0 }, /* inverse slant 1x4 */
  102. { NULL, NULL, 0 }, /* inverse slant 4x1 */
  103. { NULL, NULL, 0 }, /* inverse DCT 4x4 */
  104. };
  105. /**
  106. * Decode subdivision of a plane.
  107. * This is a simplified version that checks for two supported subdivisions:
  108. * - 1 wavelet band per plane, size factor 1:1, code pattern: 3
  109. * - 4 wavelet bands per plane, size factor 1:4, code pattern: 2,3,3,3,3
  110. * Anything else is either unsupported or corrupt.
  111. *
  112. * @param[in,out] gb the GetBit context
  113. * @return number of wavelet bands or 0 on error
  114. */
  115. static int decode_plane_subdivision(GetBitContext *gb)
  116. {
  117. int i;
  118. switch (get_bits(gb, 2)) {
  119. case 3:
  120. return 1;
  121. case 2:
  122. for (i = 0; i < 4; i++)
  123. if (get_bits(gb, 2) != 3)
  124. return 0;
  125. return 4;
  126. default:
  127. return 0;
  128. }
  129. }
  130. static inline int scale_tile_size(int def_size, int size_factor)
  131. {
  132. return size_factor == 15 ? def_size : (size_factor + 1) << 5;
  133. }
  134. /**
  135. * Decode Indeo 4 picture header.
  136. *
  137. * @param[in,out] ctx pointer to the decoder context
  138. * @param[in] avctx pointer to the AVCodecContext
  139. * @return result code: 0 = OK, negative number = error
  140. */
  141. static int decode_pic_hdr(IVI4DecContext *ctx, AVCodecContext *avctx)
  142. {
  143. int pic_size_indx, i, p;
  144. IVIPicConfig pic_conf;
  145. if (get_bits(&ctx->gb, 18) != 0x3FFF8) {
  146. av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
  147. return AVERROR_INVALIDDATA;
  148. }
  149. ctx->prev_frame_type = ctx->frame_type;
  150. ctx->frame_type = get_bits(&ctx->gb, 3);
  151. if (ctx->frame_type == 7) {
  152. av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d\n", ctx->frame_type);
  153. return AVERROR_INVALIDDATA;
  154. }
  155. #if IVI4_STREAM_ANALYSER
  156. if ( ctx->frame_type == FRAMETYPE_BIDIR1
  157. || ctx->frame_type == FRAMETYPE_BIDIR)
  158. ctx->has_b_frames = 1;
  159. #endif
  160. ctx->transp_status = get_bits1(&ctx->gb);
  161. #if IVI4_STREAM_ANALYSER
  162. if (ctx->transp_status) {
  163. ctx->has_transp = 1;
  164. }
  165. #endif
  166. /* unknown bit: Mac decoder ignores this bit, XANIM returns error */
  167. if (get_bits1(&ctx->gb)) {
  168. av_log(avctx, AV_LOG_ERROR, "Sync bit is set!\n");
  169. return AVERROR_INVALIDDATA;
  170. }
  171. ctx->data_size = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 24) : 0;
  172. /* null frames don't contain anything else so we just return */
  173. if (ctx->frame_type >= FRAMETYPE_NULL_FIRST) {
  174. av_dlog(avctx, "Null frame encountered!\n");
  175. return 0;
  176. }
  177. /* Check key lock status. If enabled - ignore lock word. */
  178. /* Usually we have to prompt the user for the password, but */
  179. /* we don't do that because Indeo 4 videos can be decoded anyway */
  180. if (get_bits1(&ctx->gb)) {
  181. skip_bits_long(&ctx->gb, 32);
  182. av_dlog(avctx, "Password-protected clip!\n");
  183. }
  184. pic_size_indx = get_bits(&ctx->gb, 3);
  185. if (pic_size_indx == IVI4_PIC_SIZE_ESC) {
  186. pic_conf.pic_height = get_bits(&ctx->gb, 16);
  187. pic_conf.pic_width = get_bits(&ctx->gb, 16);
  188. } else {
  189. pic_conf.pic_height = ivi4_common_pic_sizes[pic_size_indx * 2 + 1];
  190. pic_conf.pic_width = ivi4_common_pic_sizes[pic_size_indx * 2 ];
  191. }
  192. /* Decode tile dimensions. */
  193. if (get_bits1(&ctx->gb)) {
  194. pic_conf.tile_height = scale_tile_size(pic_conf.pic_height, get_bits(&ctx->gb, 4));
  195. pic_conf.tile_width = scale_tile_size(pic_conf.pic_width, get_bits(&ctx->gb, 4));
  196. #if IVI4_STREAM_ANALYSER
  197. ctx->uses_tiling = 1;
  198. #endif
  199. } else {
  200. pic_conf.tile_height = pic_conf.pic_height;
  201. pic_conf.tile_width = pic_conf.pic_width;
  202. }
  203. /* Decode chroma subsampling. We support only 4:4 aka YVU9. */
  204. if (get_bits(&ctx->gb, 2)) {
  205. av_log(avctx, AV_LOG_ERROR, "Only YVU9 picture format is supported!\n");
  206. return AVERROR_INVALIDDATA;
  207. }
  208. pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
  209. pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2;
  210. /* decode subdivision of the planes */
  211. pic_conf.luma_bands = decode_plane_subdivision(&ctx->gb);
  212. if (pic_conf.luma_bands)
  213. pic_conf.chroma_bands = decode_plane_subdivision(&ctx->gb);
  214. ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
  215. if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
  216. av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
  217. pic_conf.luma_bands, pic_conf.chroma_bands);
  218. return AVERROR_INVALIDDATA;
  219. }
  220. /* check if picture layout was changed and reallocate buffers */
  221. if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) {
  222. if (ff_ivi_init_planes(ctx->planes, &pic_conf)) {
  223. av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
  224. return AVERROR(ENOMEM);
  225. }
  226. ctx->pic_conf = pic_conf;
  227. /* set default macroblock/block dimensions */
  228. for (p = 0; p <= 2; p++) {
  229. for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
  230. ctx->planes[p].bands[i].mb_size = !p ? (!ctx->is_scalable ? 16 : 8) : 4;
  231. ctx->planes[p].bands[i].blk_size = !p ? 8 : 4;
  232. }
  233. }
  234. if (ff_ivi_init_tiles(ctx->planes, ctx->pic_conf.tile_width,
  235. ctx->pic_conf.tile_height)) {
  236. av_log(avctx, AV_LOG_ERROR,
  237. "Couldn't reallocate internal structures!\n");
  238. return AVERROR(ENOMEM);
  239. }
  240. }
  241. ctx->frame_num = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 20) : 0;
  242. /* skip decTimeEst field if present */
  243. if (get_bits1(&ctx->gb))
  244. skip_bits(&ctx->gb, 8);
  245. /* decode macroblock and block huffman codebooks */
  246. if (ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_MB_HUFF, &ctx->mb_vlc, avctx) ||
  247. ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_BLK_HUFF, &ctx->blk_vlc, avctx))
  248. return AVERROR_INVALIDDATA;
  249. ctx->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8;
  250. ctx->in_imf = get_bits1(&ctx->gb);
  251. ctx->in_q = get_bits1(&ctx->gb);
  252. ctx->pic_glob_quant = get_bits(&ctx->gb, 5);
  253. /* TODO: ignore this parameter if unused */
  254. ctx->unknown1 = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 0;
  255. ctx->checksum = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 16) : 0;
  256. /* skip picture header extension if any */
  257. while (get_bits1(&ctx->gb)) {
  258. av_dlog(avctx, "Pic hdr extension encountered!\n");
  259. skip_bits(&ctx->gb, 8);
  260. }
  261. if (get_bits1(&ctx->gb)) {
  262. av_log(avctx, AV_LOG_ERROR, "Bad blocks bits encountered!\n");
  263. }
  264. align_get_bits(&ctx->gb);
  265. return 0;
  266. }
  267. /**
  268. * Decode Indeo 4 band header.
  269. *
  270. * @param[in,out] ctx pointer to the decoder context
  271. * @param[in,out] band pointer to the band descriptor
  272. * @param[in] avctx pointer to the AVCodecContext
  273. * @return result code: 0 = OK, negative number = error
  274. */
  275. static int decode_band_hdr(IVI4DecContext *ctx, IVIBandDesc *band,
  276. AVCodecContext *avctx)
  277. {
  278. int plane, band_num, indx, transform_id, scan_indx;
  279. int i;
  280. int quant_mat;
  281. plane = get_bits(&ctx->gb, 2);
  282. band_num = get_bits(&ctx->gb, 4);
  283. if (band->plane != plane || band->band_num != band_num) {
  284. av_log(avctx, AV_LOG_ERROR, "Invalid band header sequence!\n");
  285. return AVERROR_INVALIDDATA;
  286. }
  287. band->is_empty = get_bits1(&ctx->gb);
  288. if (!band->is_empty) {
  289. /* skip header size
  290. * If header size is not given, header size is 4 bytes. */
  291. if (get_bits1(&ctx->gb))
  292. skip_bits(&ctx->gb, 16);
  293. band->is_halfpel = get_bits(&ctx->gb, 2);
  294. if (band->is_halfpel >= 2) {
  295. av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported mv resolution: %d!\n",
  296. band->is_halfpel);
  297. return AVERROR_INVALIDDATA;
  298. }
  299. #if IVI4_STREAM_ANALYSER
  300. if (!band->is_halfpel)
  301. ctx->uses_fullpel = 1;
  302. #endif
  303. band->checksum_present = get_bits1(&ctx->gb);
  304. if (band->checksum_present)
  305. band->checksum = get_bits(&ctx->gb, 16);
  306. indx = get_bits(&ctx->gb, 2);
  307. if (indx == 3) {
  308. av_log(avctx, AV_LOG_ERROR, "Invalid block size!\n");
  309. return AVERROR_INVALIDDATA;
  310. }
  311. band->mb_size = 16 >> indx;
  312. band->blk_size = 8 >> (indx >> 1);
  313. band->inherit_mv = get_bits1(&ctx->gb);
  314. band->inherit_qdelta = get_bits1(&ctx->gb);
  315. band->glob_quant = get_bits(&ctx->gb, 5);
  316. if (!get_bits1(&ctx->gb) || ctx->frame_type == FRAMETYPE_INTRA) {
  317. transform_id = get_bits(&ctx->gb, 5);
  318. if (transform_id >= FF_ARRAY_ELEMS(transforms) ||
  319. !transforms[transform_id].inv_trans) {
  320. av_log_ask_for_sample(avctx, "Unimplemented transform: %d!\n", transform_id);
  321. return AVERROR_PATCHWELCOME;
  322. }
  323. if ((transform_id >= 7 && transform_id <= 9) ||
  324. transform_id == 17) {
  325. av_log_ask_for_sample(avctx, "DCT transform not supported yet!\n");
  326. return AVERROR_PATCHWELCOME;
  327. }
  328. if (transform_id < 10 && band->blk_size < 8) {
  329. av_log(avctx, AV_LOG_ERROR, "wrong transform size!\n");
  330. return AVERROR_INVALIDDATA;
  331. }
  332. #if IVI4_STREAM_ANALYSER
  333. if ((transform_id >= 0 && transform_id <= 2) || transform_id == 10)
  334. ctx->uses_haar = 1;
  335. #endif
  336. band->inv_transform = transforms[transform_id].inv_trans;
  337. band->dc_transform = transforms[transform_id].dc_trans;
  338. band->is_2d_trans = transforms[transform_id].is_2d_trans;
  339. band->transform_size= (transform_id < 10) ? 8 : 4;
  340. scan_indx = get_bits(&ctx->gb, 4);
  341. if ((scan_indx>4 && scan_indx<10) != (band->blk_size==4)) {
  342. av_log(avctx, AV_LOG_ERROR, "mismatching scan table!\n");
  343. return AVERROR_INVALIDDATA;
  344. }
  345. if (scan_indx == 15) {
  346. av_log(avctx, AV_LOG_ERROR, "Custom scan pattern encountered!\n");
  347. return AVERROR_INVALIDDATA;
  348. }
  349. band->scan = scan_index_to_tab[scan_indx];
  350. quant_mat = get_bits(&ctx->gb, 5);
  351. if (quant_mat == 31) {
  352. av_log(avctx, AV_LOG_ERROR, "Custom quant matrix encountered!\n");
  353. return AVERROR_INVALIDDATA;
  354. }
  355. if (quant_mat > 21) {
  356. av_log(avctx, AV_LOG_ERROR, "Invalid quant matrix encountered!\n");
  357. return AVERROR_INVALIDDATA;
  358. }
  359. band->quant_mat = quant_mat;
  360. }
  361. if (quant_index_to_tab[band->quant_mat] > 4 && band->blk_size == 4) {
  362. av_log(avctx, AV_LOG_ERROR, "Invalid quant matrix for 4x4 block encountered!\n");
  363. band->quant_mat = 0;
  364. return AVERROR_INVALIDDATA;
  365. }
  366. /* decode block huffman codebook */
  367. if (ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_BLK_HUFF,
  368. &band->blk_vlc, avctx))
  369. return AVERROR_INVALIDDATA;
  370. /* select appropriate rvmap table for this band */
  371. band->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8;
  372. /* decode rvmap probability corrections if any */
  373. band->num_corr = 0; /* there is no corrections */
  374. if (get_bits1(&ctx->gb)) {
  375. band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
  376. if (band->num_corr > 61) {
  377. av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
  378. band->num_corr);
  379. return AVERROR_INVALIDDATA;
  380. }
  381. /* read correction pairs */
  382. for (i = 0; i < band->num_corr * 2; i++)
  383. band->corr[i] = get_bits(&ctx->gb, 8);
  384. }
  385. }
  386. if (band->blk_size == 8) {
  387. band->intra_base = &ivi4_quant_8x8_intra[quant_index_to_tab[band->quant_mat]][0];
  388. band->inter_base = &ivi4_quant_8x8_inter[quant_index_to_tab[band->quant_mat]][0];
  389. } else {
  390. band->intra_base = &ivi4_quant_4x4_intra[quant_index_to_tab[band->quant_mat]][0];
  391. band->inter_base = &ivi4_quant_4x4_inter[quant_index_to_tab[band->quant_mat]][0];
  392. }
  393. /* Indeo 4 doesn't use scale tables */
  394. band->intra_scale = NULL;
  395. band->inter_scale = NULL;
  396. align_get_bits(&ctx->gb);
  397. if (!band->scan) {
  398. av_log(avctx, AV_LOG_ERROR, "band->scan not set\n");
  399. return AVERROR_INVALIDDATA;
  400. }
  401. return 0;
  402. }
  403. /**
  404. * Decode information (block type, cbp, quant delta, motion vector)
  405. * for all macroblocks in the current tile.
  406. *
  407. * @param[in,out] ctx pointer to the decoder context
  408. * @param[in,out] band pointer to the band descriptor
  409. * @param[in,out] tile pointer to the tile descriptor
  410. * @param[in] avctx pointer to the AVCodecContext
  411. * @return result code: 0 = OK, negative number = error
  412. */
  413. static int decode_mb_info(IVI4DecContext *ctx, IVIBandDesc *band,
  414. IVITile *tile, AVCodecContext *avctx)
  415. {
  416. int x, y, mv_x, mv_y, mv_delta, offs, mb_offset, blks_per_mb,
  417. mv_scale, mb_type_bits, s;
  418. IVIMbInfo *mb, *ref_mb;
  419. int row_offset = band->mb_size * band->pitch;
  420. mb = tile->mbs;
  421. ref_mb = tile->ref_mbs;
  422. offs = tile->ypos * band->pitch + tile->xpos;
  423. blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
  424. mb_type_bits = ctx->frame_type == FRAMETYPE_BIDIR ? 2 : 1;
  425. /* scale factor for motion vectors */
  426. mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
  427. mv_x = mv_y = 0;
  428. if (((tile->width + band->mb_size-1)/band->mb_size) * ((tile->height + band->mb_size-1)/band->mb_size) != tile->num_MBs) {
  429. av_log(avctx, AV_LOG_ERROR, "num_MBs mismatch %d %d %d %d\n", tile->width, tile->height, band->mb_size, tile->num_MBs);
  430. return -1;
  431. }
  432. for (y = tile->ypos; y < tile->ypos + tile->height; y += band->mb_size) {
  433. mb_offset = offs;
  434. for (x = tile->xpos; x < tile->xpos + tile->width; x += band->mb_size) {
  435. mb->xpos = x;
  436. mb->ypos = y;
  437. mb->buf_offs = mb_offset;
  438. if (get_bits1(&ctx->gb)) {
  439. if (ctx->frame_type == FRAMETYPE_INTRA) {
  440. av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
  441. return AVERROR_INVALIDDATA;
  442. }
  443. mb->type = 1; /* empty macroblocks are always INTER */
  444. mb->cbp = 0; /* all blocks are empty */
  445. mb->q_delta = 0;
  446. if (!band->plane && !band->band_num && ctx->in_q) {
  447. mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  448. IVI_VLC_BITS, 1);
  449. mb->q_delta = IVI_TOSIGNED(mb->q_delta);
  450. }
  451. mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
  452. if (band->inherit_mv && ref_mb) {
  453. /* motion vector inheritance */
  454. if (mv_scale) {
  455. mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
  456. mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
  457. } else {
  458. mb->mv_x = ref_mb->mv_x;
  459. mb->mv_y = ref_mb->mv_y;
  460. }
  461. }
  462. } else {
  463. if (band->inherit_mv && ref_mb) {
  464. mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */
  465. } else if (ctx->frame_type == FRAMETYPE_INTRA) {
  466. mb->type = 0; /* mb_type is always INTRA for intra-frames */
  467. } else {
  468. mb->type = get_bits(&ctx->gb, mb_type_bits);
  469. }
  470. mb->cbp = get_bits(&ctx->gb, blks_per_mb);
  471. mb->q_delta = 0;
  472. if (band->inherit_qdelta) {
  473. if (ref_mb) mb->q_delta = ref_mb->q_delta;
  474. } else if (mb->cbp || (!band->plane && !band->band_num &&
  475. ctx->in_q)) {
  476. mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  477. IVI_VLC_BITS, 1);
  478. mb->q_delta = IVI_TOSIGNED(mb->q_delta);
  479. }
  480. if (!mb->type) {
  481. mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
  482. } else {
  483. if (band->inherit_mv && ref_mb) {
  484. /* motion vector inheritance */
  485. if (mv_scale) {
  486. mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
  487. mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
  488. } else {
  489. mb->mv_x = ref_mb->mv_x;
  490. mb->mv_y = ref_mb->mv_y;
  491. }
  492. } else {
  493. /* decode motion vector deltas */
  494. mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  495. IVI_VLC_BITS, 1);
  496. mv_y += IVI_TOSIGNED(mv_delta);
  497. mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  498. IVI_VLC_BITS, 1);
  499. mv_x += IVI_TOSIGNED(mv_delta);
  500. mb->mv_x = mv_x;
  501. mb->mv_y = mv_y;
  502. }
  503. }
  504. }
  505. s= band->is_halfpel;
  506. if (mb->type)
  507. if ( x + (mb->mv_x >>s) + (y+ (mb->mv_y >>s))*band->pitch < 0 ||
  508. x + ((mb->mv_x+s)>>s) + band->mb_size - 1
  509. + (y+band->mb_size - 1 +((mb->mv_y+s)>>s))*band->pitch > band->bufsize -1) {
  510. av_log(avctx, AV_LOG_ERROR, "motion vector %d %d outside reference\n", x*s + mb->mv_x, y*s + mb->mv_y);
  511. return AVERROR_INVALIDDATA;
  512. }
  513. mb++;
  514. if (ref_mb)
  515. ref_mb++;
  516. mb_offset += band->mb_size;
  517. }
  518. offs += row_offset;
  519. }
  520. align_get_bits(&ctx->gb);
  521. return 0;
  522. }
  523. /**
  524. * Decode an Indeo 4 band.
  525. *
  526. * @param[in,out] ctx pointer to the decoder context
  527. * @param[in,out] band pointer to the band descriptor
  528. * @param[in] avctx pointer to the AVCodecContext
  529. * @return result code: 0 = OK, negative number = error
  530. */
  531. static int decode_band(IVI4DecContext *ctx, int plane_num,
  532. IVIBandDesc *band, AVCodecContext *avctx)
  533. {
  534. int result, i, t, pos, idx1, idx2;
  535. IVITile *tile;
  536. int ret = 0;
  537. band->buf = band->bufs[ctx->dst_buf];
  538. band->ref_buf = band->bufs[ctx->ref_buf];
  539. result = decode_band_hdr(ctx, band, avctx);
  540. if (result) {
  541. av_log(avctx, AV_LOG_ERROR, "Error decoding band header\n");
  542. return result;
  543. }
  544. if (band->is_empty) {
  545. av_log(avctx, AV_LOG_ERROR, "Empty band encountered!\n");
  546. return AVERROR_INVALIDDATA;
  547. }
  548. band->rv_map = &ctx->rvmap_tabs[band->rvmap_sel];
  549. /* apply corrections to the selected rvmap table if present */
  550. for (i = 0; i < band->num_corr; i++) {
  551. idx1 = band->corr[i * 2];
  552. idx2 = band->corr[i * 2 + 1];
  553. FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);
  554. FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);
  555. if (idx1 == band->rv_map->eob_sym || idx2 == band->rv_map->eob_sym)
  556. band->rv_map->eob_sym ^= idx1 ^ idx2;
  557. if (idx1 == band->rv_map->esc_sym || idx2 == band->rv_map->esc_sym)
  558. band->rv_map->esc_sym ^= idx1 ^ idx2;
  559. }
  560. pos = get_bits_count(&ctx->gb);
  561. for (t = 0; t < band->num_tiles; t++) {
  562. tile = &band->tiles[t];
  563. tile->is_empty = get_bits1(&ctx->gb);
  564. if (tile->is_empty) {
  565. ff_ivi_process_empty_tile(avctx, band, tile,
  566. (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3));
  567. av_dlog(avctx, "Empty tile encountered!\n");
  568. } else {
  569. tile->data_size = ff_ivi_dec_tile_data_size(&ctx->gb);
  570. if (!tile->data_size) {
  571. av_log(avctx, AV_LOG_ERROR, "Tile data size is zero!\n");
  572. ret = AVERROR_INVALIDDATA;
  573. break;
  574. }
  575. result = decode_mb_info(ctx, band, tile, avctx);
  576. if (result < 0)
  577. break;
  578. result = ff_ivi_decode_blocks(&ctx->gb, band, tile);
  579. if (result < 0 || ((get_bits_count(&ctx->gb) - pos) >> 3) != tile->data_size) {
  580. av_log(avctx, AV_LOG_ERROR, "Corrupted tile data encountered!\n");
  581. break;
  582. }
  583. pos += tile->data_size << 3; // skip to next tile
  584. }
  585. }
  586. /* restore the selected rvmap table by applying its corrections in reverse order */
  587. for (i = band->num_corr - 1; i >= 0; i--) {
  588. idx1 = band->corr[i * 2];
  589. idx2 = band->corr[i * 2 + 1];
  590. FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);
  591. FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);
  592. if (idx1 == band->rv_map->eob_sym || idx2 == band->rv_map->eob_sym)
  593. band->rv_map->eob_sym ^= idx1 ^ idx2;
  594. if (idx1 == band->rv_map->esc_sym || idx2 == band->rv_map->esc_sym)
  595. band->rv_map->esc_sym ^= idx1 ^ idx2;
  596. }
  597. #if defined(DEBUG) && IVI4_DEBUG_CHECKSUM
  598. if (band->checksum_present) {
  599. uint16_t chksum = ivi_calc_band_checksum(band);
  600. if (chksum != band->checksum) {
  601. av_log(avctx, AV_LOG_ERROR,
  602. "Band checksum mismatch! Plane %d, band %d, received: %x, calculated: %x\n",
  603. band->plane, band->band_num, band->checksum, chksum);
  604. }
  605. }
  606. #endif
  607. align_get_bits(&ctx->gb);
  608. return ret;
  609. }
  610. static av_cold int decode_init(AVCodecContext *avctx)
  611. {
  612. IVI4DecContext *ctx = avctx->priv_data;
  613. ff_ivi_init_static_vlc();
  614. /* copy rvmap tables in our context so we can apply changes to them */
  615. memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
  616. /* Force allocation of the internal buffers */
  617. /* during picture header decoding. */
  618. ctx->pic_conf.pic_width = 0;
  619. ctx->pic_conf.pic_height = 0;
  620. avctx->pix_fmt = PIX_FMT_YUV410P;
  621. return 0;
  622. }
  623. /**
  624. * Rearrange decoding and reference buffers.
  625. *
  626. * @param[in,out] ctx pointer to the decoder context
  627. */
  628. static void switch_buffers(IVI4DecContext *ctx)
  629. {
  630. switch (ctx->prev_frame_type) {
  631. case FRAMETYPE_INTRA:
  632. case FRAMETYPE_INTER:
  633. ctx->buf_switch ^= 1;
  634. ctx->dst_buf = ctx->buf_switch;
  635. ctx->ref_buf = ctx->buf_switch ^ 1;
  636. break;
  637. case FRAMETYPE_INTER_NOREF:
  638. break;
  639. }
  640. switch (ctx->frame_type) {
  641. case FRAMETYPE_INTRA:
  642. ctx->buf_switch = 0;
  643. /* FALLTHROUGH */
  644. case FRAMETYPE_INTER:
  645. ctx->dst_buf = ctx->buf_switch;
  646. ctx->ref_buf = ctx->buf_switch ^ 1;
  647. break;
  648. case FRAMETYPE_INTER_NOREF:
  649. case FRAMETYPE_NULL_FIRST:
  650. case FRAMETYPE_NULL_LAST:
  651. break;
  652. }
  653. }
  654. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  655. AVPacket *avpkt)
  656. {
  657. IVI4DecContext *ctx = avctx->priv_data;
  658. const uint8_t *buf = avpkt->data;
  659. int buf_size = avpkt->size;
  660. int result, p, b;
  661. init_get_bits(&ctx->gb, buf, buf_size * 8);
  662. result = decode_pic_hdr(ctx, avctx);
  663. if (result) {
  664. av_log(avctx, AV_LOG_ERROR, "Error decoding picture header\n");
  665. return result;
  666. }
  667. switch_buffers(ctx);
  668. if (ctx->frame_type < FRAMETYPE_NULL_FIRST) {
  669. for (p = 0; p < 3; p++) {
  670. for (b = 0; b < ctx->planes[p].num_bands; b++) {
  671. result = decode_band(ctx, p, &ctx->planes[p].bands[b], avctx);
  672. if (result) {
  673. av_log(avctx, AV_LOG_ERROR,
  674. "Error decoding band: %d, plane: %d\n", b, p);
  675. return result;
  676. }
  677. }
  678. }
  679. }
  680. /* If the bidirectional mode is enabled, next I and the following P frame will */
  681. /* be sent together. Unfortunately the approach below seems to be the only way */
  682. /* to handle the B-frames mode. That's exactly the same Intel decoders do. */
  683. if (ctx->frame_type == FRAMETYPE_INTRA) {
  684. while (get_bits(&ctx->gb, 8)); // skip version string
  685. skip_bits_long(&ctx->gb, 64); // skip padding, TODO: implement correct 8-bytes alignment
  686. if (get_bits_left(&ctx->gb) > 18 && show_bits(&ctx->gb, 18) == 0x3FFF8)
  687. av_log(avctx, AV_LOG_ERROR, "Buffer contains IP frames!\n");
  688. }
  689. if (ctx->frame_type >= FRAMETYPE_NULL_FIRST)
  690. return buf_size;
  691. if (ctx->frame.data[0])
  692. avctx->release_buffer(avctx, &ctx->frame);
  693. avcodec_set_dimensions(avctx, ctx->planes[0].width, ctx->planes[0].height);
  694. ctx->frame.reference = 0;
  695. if ((result = avctx->get_buffer(avctx, &ctx->frame)) < 0) {
  696. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  697. return result;
  698. }
  699. if (ctx->is_scalable) {
  700. ff_ivi_recompose_haar(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0], 4);
  701. } else {
  702. ff_ivi_output_plane(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0]);
  703. }
  704. ff_ivi_output_plane(&ctx->planes[2], ctx->frame.data[1], ctx->frame.linesize[1]);
  705. ff_ivi_output_plane(&ctx->planes[1], ctx->frame.data[2], ctx->frame.linesize[2]);
  706. *data_size = sizeof(AVFrame);
  707. *(AVFrame*)data = ctx->frame;
  708. return buf_size;
  709. }
  710. static av_cold int decode_close(AVCodecContext *avctx)
  711. {
  712. IVI4DecContext *ctx = avctx->priv_data;
  713. ff_ivi_free_buffers(&ctx->planes[0]);
  714. if (ctx->frame.data[0])
  715. avctx->release_buffer(avctx, &ctx->frame);
  716. #if IVI4_STREAM_ANALYSER
  717. if (ctx->is_scalable)
  718. av_log(avctx, AV_LOG_ERROR, "This video uses scalability mode!\n");
  719. if (ctx->uses_tiling)
  720. av_log(avctx, AV_LOG_ERROR, "This video uses local decoding!\n");
  721. if (ctx->has_b_frames)
  722. av_log(avctx, AV_LOG_ERROR, "This video contains B-frames!\n");
  723. if (ctx->has_transp)
  724. av_log(avctx, AV_LOG_ERROR, "Transparency mode is enabled!\n");
  725. if (ctx->uses_haar)
  726. av_log(avctx, AV_LOG_ERROR, "This video uses Haar transform!\n");
  727. if (ctx->uses_fullpel)
  728. av_log(avctx, AV_LOG_ERROR, "This video uses fullpel motion vectors!\n");
  729. #endif
  730. return 0;
  731. }
  732. AVCodec ff_indeo4_decoder = {
  733. .name = "indeo4",
  734. .type = AVMEDIA_TYPE_VIDEO,
  735. .id = CODEC_ID_INDEO4,
  736. .priv_data_size = sizeof(IVI4DecContext),
  737. .init = decode_init,
  738. .close = decode_close,
  739. .decode = decode_frame,
  740. .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 4"),
  741. };