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.

699 lines
25KB

  1. /*
  2. * Indeo Video Interactive v4 compatible decoder
  3. * Copyright (c) 2009-2011 Maxim Poliakovski
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "indeo4data.h"
  32. #include "internal.h"
  33. #include "ivi.h"
  34. #include "ivi_dsp.h"
  35. #define IVI4_PIC_SIZE_ESC 7
  36. static const struct {
  37. InvTransformPtr *inv_trans;
  38. DCTransformPtr *dc_trans;
  39. int is_2d_trans;
  40. } transforms[18] = {
  41. { ff_ivi_inverse_haar_8x8, ff_ivi_dc_haar_2d, 1 },
  42. { ff_ivi_row_haar8, ff_ivi_dc_haar_2d, 0 },
  43. { ff_ivi_col_haar8, ff_ivi_dc_haar_2d, 0 },
  44. { ff_ivi_put_pixels_8x8, ff_ivi_put_dc_pixel_8x8, 1 },
  45. { ff_ivi_inverse_slant_8x8, ff_ivi_dc_slant_2d, 1 },
  46. { ff_ivi_row_slant8, ff_ivi_dc_row_slant, 1 },
  47. { ff_ivi_col_slant8, ff_ivi_dc_col_slant, 1 },
  48. { NULL, NULL, 0 }, /* inverse DCT 8x8 */
  49. { NULL, NULL, 0 }, /* inverse DCT 8x1 */
  50. { NULL, NULL, 0 }, /* inverse DCT 1x8 */
  51. { ff_ivi_inverse_haar_4x4, ff_ivi_dc_haar_2d, 1 },
  52. { ff_ivi_inverse_slant_4x4, ff_ivi_dc_slant_2d, 1 },
  53. { NULL, NULL, 0 }, /* no transform 4x4 */
  54. { ff_ivi_row_haar4, ff_ivi_dc_haar_2d, 0 },
  55. { ff_ivi_col_haar4, ff_ivi_dc_haar_2d, 0 },
  56. { ff_ivi_row_slant4, ff_ivi_dc_row_slant, 0 },
  57. { ff_ivi_col_slant4, ff_ivi_dc_col_slant, 0 },
  58. { NULL, NULL, 0 }, /* inverse DCT 4x4 */
  59. };
  60. /**
  61. * Decode subdivision of a plane.
  62. * This is a simplified version that checks for two supported subdivisions:
  63. * - 1 wavelet band per plane, size factor 1:1, code pattern: 3
  64. * - 4 wavelet bands per plane, size factor 1:4, code pattern: 2,3,3,3,3
  65. * Anything else is either unsupported or corrupt.
  66. *
  67. * @param[in,out] gb the GetBit context
  68. * @return number of wavelet bands or 0 on error
  69. */
  70. static int decode_plane_subdivision(GetBitContext *gb)
  71. {
  72. int i;
  73. switch (get_bits(gb, 2)) {
  74. case 3:
  75. return 1;
  76. case 2:
  77. for (i = 0; i < 4; i++)
  78. if (get_bits(gb, 2) != 3)
  79. return 0;
  80. return 4;
  81. default:
  82. return 0;
  83. }
  84. }
  85. static inline int scale_tile_size(int def_size, int size_factor)
  86. {
  87. return size_factor == 15 ? def_size : (size_factor + 1) << 5;
  88. }
  89. /**
  90. * Decode Indeo 4 picture header.
  91. *
  92. * @param[in,out] ctx pointer to the decoder context
  93. * @param[in] avctx pointer to the AVCodecContext
  94. * @return result code: 0 = OK, negative number = error
  95. */
  96. static int decode_pic_hdr(IVI45DecContext *ctx, AVCodecContext *avctx)
  97. {
  98. int pic_size_indx, i, p;
  99. IVIPicConfig pic_conf;
  100. if (get_bits(&ctx->gb, 18) != 0x3FFF8) {
  101. av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
  102. return AVERROR_INVALIDDATA;
  103. }
  104. ctx->prev_frame_type = ctx->frame_type;
  105. ctx->frame_type = get_bits(&ctx->gb, 3);
  106. if (ctx->frame_type == 7) {
  107. av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d\n", ctx->frame_type);
  108. return AVERROR_INVALIDDATA;
  109. }
  110. if (ctx->frame_type == IVI4_FRAMETYPE_BIDIR)
  111. ctx->has_b_frames = 1;
  112. ctx->has_transp = get_bits1(&ctx->gb);
  113. /* unknown bit: Mac decoder ignores this bit, XANIM returns error */
  114. if (get_bits1(&ctx->gb)) {
  115. av_log(avctx, AV_LOG_ERROR, "Sync bit is set!\n");
  116. return AVERROR_INVALIDDATA;
  117. }
  118. ctx->data_size = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 24) : 0;
  119. /* null frames don't contain anything else so we just return */
  120. if (ctx->frame_type >= IVI4_FRAMETYPE_NULL_FIRST) {
  121. ff_dlog(avctx, "Null frame encountered!\n");
  122. return 0;
  123. }
  124. /* Check key lock status. If enabled - ignore lock word. */
  125. /* Usually we have to prompt the user for the password, but */
  126. /* we don't do that because Indeo 4 videos can be decoded anyway */
  127. if (get_bits1(&ctx->gb)) {
  128. skip_bits_long(&ctx->gb, 32);
  129. ff_dlog(avctx, "Password-protected clip!\n");
  130. }
  131. pic_size_indx = get_bits(&ctx->gb, 3);
  132. if (pic_size_indx == IVI4_PIC_SIZE_ESC) {
  133. pic_conf.pic_height = get_bits(&ctx->gb, 16);
  134. pic_conf.pic_width = get_bits(&ctx->gb, 16);
  135. } else {
  136. pic_conf.pic_height = ivi4_common_pic_sizes[pic_size_indx * 2 + 1];
  137. pic_conf.pic_width = ivi4_common_pic_sizes[pic_size_indx * 2 ];
  138. }
  139. /* Decode tile dimensions. */
  140. ctx->uses_tiling = get_bits1(&ctx->gb);
  141. if (ctx->uses_tiling) {
  142. pic_conf.tile_height = scale_tile_size(pic_conf.pic_height, get_bits(&ctx->gb, 4));
  143. pic_conf.tile_width = scale_tile_size(pic_conf.pic_width, get_bits(&ctx->gb, 4));
  144. } else {
  145. pic_conf.tile_height = pic_conf.pic_height;
  146. pic_conf.tile_width = pic_conf.pic_width;
  147. }
  148. /* Decode chroma subsampling. We support only 4:4 aka YVU9. */
  149. if (get_bits(&ctx->gb, 2)) {
  150. av_log(avctx, AV_LOG_ERROR, "Only YVU9 picture format is supported!\n");
  151. return AVERROR_INVALIDDATA;
  152. }
  153. pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
  154. pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2;
  155. /* decode subdivision of the planes */
  156. pic_conf.luma_bands = decode_plane_subdivision(&ctx->gb);
  157. pic_conf.chroma_bands = 0;
  158. if (pic_conf.luma_bands)
  159. pic_conf.chroma_bands = decode_plane_subdivision(&ctx->gb);
  160. ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
  161. if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
  162. av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
  163. pic_conf.luma_bands, pic_conf.chroma_bands);
  164. return AVERROR_INVALIDDATA;
  165. }
  166. /* check if picture layout was changed and reallocate buffers */
  167. if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) {
  168. if (ff_ivi_init_planes(ctx->planes, &pic_conf, 1)) {
  169. av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
  170. ctx->pic_conf.luma_bands = 0;
  171. return AVERROR(ENOMEM);
  172. }
  173. ctx->pic_conf = pic_conf;
  174. /* set default macroblock/block dimensions */
  175. for (p = 0; p <= 2; p++) {
  176. for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
  177. ctx->planes[p].bands[i].mb_size = !p ? (!ctx->is_scalable ? 16 : 8) : 4;
  178. ctx->planes[p].bands[i].blk_size = !p ? 8 : 4;
  179. }
  180. }
  181. if (ff_ivi_init_tiles(ctx->planes, ctx->pic_conf.tile_width,
  182. ctx->pic_conf.tile_height)) {
  183. av_log(avctx, AV_LOG_ERROR,
  184. "Couldn't reallocate internal structures!\n");
  185. return AVERROR(ENOMEM);
  186. }
  187. }
  188. ctx->frame_num = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 20) : 0;
  189. /* skip decTimeEst field if present */
  190. if (get_bits1(&ctx->gb))
  191. skip_bits(&ctx->gb, 8);
  192. /* decode macroblock and block huffman codebooks */
  193. if (ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_MB_HUFF, &ctx->mb_vlc, avctx) ||
  194. ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_BLK_HUFF, &ctx->blk_vlc, avctx))
  195. return AVERROR_INVALIDDATA;
  196. ctx->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8;
  197. ctx->in_imf = get_bits1(&ctx->gb);
  198. ctx->in_q = get_bits1(&ctx->gb);
  199. ctx->pic_glob_quant = get_bits(&ctx->gb, 5);
  200. /* TODO: ignore this parameter if unused */
  201. ctx->unknown1 = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 0;
  202. ctx->checksum = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 16) : 0;
  203. /* skip picture header extension if any */
  204. while (get_bits1(&ctx->gb)) {
  205. ff_dlog(avctx, "Pic hdr extension encountered!\n");
  206. if (get_bits_left(&ctx->gb) < 10)
  207. return AVERROR_INVALIDDATA;
  208. skip_bits(&ctx->gb, 8);
  209. }
  210. if (get_bits1(&ctx->gb)) {
  211. av_log(avctx, AV_LOG_ERROR, "Bad blocks bits encountered!\n");
  212. }
  213. align_get_bits(&ctx->gb);
  214. return 0;
  215. }
  216. /**
  217. * Decode Indeo 4 band header.
  218. *
  219. * @param[in,out] ctx pointer to the decoder context
  220. * @param[in,out] band pointer to the band descriptor
  221. * @param[in] avctx pointer to the AVCodecContext
  222. * @return result code: 0 = OK, negative number = error
  223. */
  224. static int decode_band_hdr(IVI45DecContext *ctx, IVIBandDesc *band,
  225. AVCodecContext *avctx)
  226. {
  227. int plane, band_num, indx, transform_id, scan_indx;
  228. int i;
  229. int quant_mat;
  230. plane = get_bits(&ctx->gb, 2);
  231. band_num = get_bits(&ctx->gb, 4);
  232. if (band->plane != plane || band->band_num != band_num) {
  233. av_log(avctx, AV_LOG_ERROR, "Invalid band header sequence!\n");
  234. return AVERROR_INVALIDDATA;
  235. }
  236. band->is_empty = get_bits1(&ctx->gb);
  237. if (!band->is_empty) {
  238. int old_blk_size = band->blk_size;
  239. /* skip header size
  240. * If header size is not given, header size is 4 bytes. */
  241. if (get_bits1(&ctx->gb))
  242. skip_bits(&ctx->gb, 16);
  243. band->is_halfpel = get_bits(&ctx->gb, 2);
  244. if (band->is_halfpel >= 2) {
  245. av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported mv resolution: %d!\n",
  246. band->is_halfpel);
  247. return AVERROR_INVALIDDATA;
  248. }
  249. if (!band->is_halfpel)
  250. ctx->uses_fullpel = 1;
  251. band->checksum_present = get_bits1(&ctx->gb);
  252. if (band->checksum_present)
  253. band->checksum = get_bits(&ctx->gb, 16);
  254. indx = get_bits(&ctx->gb, 2);
  255. if (indx == 3) {
  256. av_log(avctx, AV_LOG_ERROR, "Invalid block size!\n");
  257. return AVERROR_INVALIDDATA;
  258. }
  259. band->mb_size = 16 >> indx;
  260. band->blk_size = 8 >> (indx >> 1);
  261. band->inherit_mv = get_bits1(&ctx->gb);
  262. band->inherit_qdelta = get_bits1(&ctx->gb);
  263. band->glob_quant = get_bits(&ctx->gb, 5);
  264. if (!get_bits1(&ctx->gb) || ctx->frame_type == IVI4_FRAMETYPE_INTRA) {
  265. transform_id = get_bits(&ctx->gb, 5);
  266. if (transform_id >= FF_ARRAY_ELEMS(transforms) ||
  267. !transforms[transform_id].inv_trans) {
  268. avpriv_request_sample(avctx, "Transform %d", transform_id);
  269. return AVERROR_PATCHWELCOME;
  270. }
  271. if ((transform_id >= 7 && transform_id <= 9) ||
  272. transform_id == 17) {
  273. avpriv_request_sample(avctx, "DCT transform");
  274. return AVERROR_PATCHWELCOME;
  275. }
  276. if (transform_id < 10 && band->blk_size < 8) {
  277. av_log(avctx, AV_LOG_ERROR, "wrong transform size!\n");
  278. return AVERROR_INVALIDDATA;
  279. }
  280. if ((transform_id >= 0 && transform_id <= 2) || transform_id == 10)
  281. ctx->uses_haar = 1;
  282. band->inv_transform = transforms[transform_id].inv_trans;
  283. band->dc_transform = transforms[transform_id].dc_trans;
  284. band->is_2d_trans = transforms[transform_id].is_2d_trans;
  285. if (transform_id < 10)
  286. band->transform_size = 8;
  287. else
  288. band->transform_size = 4;
  289. if (band->blk_size != band->transform_size) {
  290. av_log(avctx, AV_LOG_ERROR, "transform and block size mismatch (%d != %d)\n", band->transform_size, band->blk_size);
  291. return AVERROR_INVALIDDATA;
  292. }
  293. scan_indx = get_bits(&ctx->gb, 4);
  294. if (scan_indx == 15) {
  295. av_log(avctx, AV_LOG_ERROR, "Custom scan pattern encountered!\n");
  296. return AVERROR_INVALIDDATA;
  297. }
  298. if (scan_indx > 4 && scan_indx < 10) {
  299. if (band->blk_size != 4) {
  300. av_log(avctx, AV_LOG_ERROR, "mismatching scan table!\n");
  301. return AVERROR_INVALIDDATA;
  302. }
  303. } else if (band->blk_size != 8) {
  304. av_log(avctx, AV_LOG_ERROR, "mismatching scan table!\n");
  305. return AVERROR_INVALIDDATA;
  306. }
  307. band->scan = scan_index_to_tab[scan_indx];
  308. band->scan_size = band->blk_size;
  309. quant_mat = get_bits(&ctx->gb, 5);
  310. if (quant_mat == 31) {
  311. av_log(avctx, AV_LOG_ERROR, "Custom quant matrix encountered!\n");
  312. return AVERROR_INVALIDDATA;
  313. }
  314. if (quant_mat >= FF_ARRAY_ELEMS(quant_index_to_tab)) {
  315. avpriv_request_sample(avctx, "Quantization matrix %d",
  316. quant_mat);
  317. return AVERROR_INVALIDDATA;
  318. }
  319. band->quant_mat = quant_mat;
  320. } else {
  321. if (old_blk_size != band->blk_size) {
  322. av_log(avctx, AV_LOG_ERROR,
  323. "The band block size does not match the configuration "
  324. "inherited\n");
  325. return AVERROR_INVALIDDATA;
  326. }
  327. }
  328. if (quant_index_to_tab[band->quant_mat] > 4 && band->blk_size == 4) {
  329. av_log(avctx, AV_LOG_ERROR, "Invalid quant matrix for 4x4 block encountered!\n");
  330. band->quant_mat = 0;
  331. return AVERROR_INVALIDDATA;
  332. }
  333. if (band->scan_size != band->blk_size) {
  334. av_log(avctx, AV_LOG_ERROR, "mismatching scan table!\n");
  335. return AVERROR_INVALIDDATA;
  336. }
  337. if (band->transform_size == 8 && band->blk_size < 8) {
  338. av_log(avctx, AV_LOG_ERROR, "mismatching transform_size!\n");
  339. return AVERROR_INVALIDDATA;
  340. }
  341. /* decode block huffman codebook */
  342. if (!get_bits1(&ctx->gb))
  343. band->blk_vlc.tab = ctx->blk_vlc.tab;
  344. else
  345. if (ff_ivi_dec_huff_desc(&ctx->gb, 1, IVI_BLK_HUFF,
  346. &band->blk_vlc, avctx))
  347. return AVERROR_INVALIDDATA;
  348. /* select appropriate rvmap table for this band */
  349. band->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8;
  350. /* decode rvmap probability corrections if any */
  351. band->num_corr = 0; /* there is no corrections */
  352. if (get_bits1(&ctx->gb)) {
  353. band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
  354. if (band->num_corr > 61) {
  355. av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
  356. band->num_corr);
  357. return AVERROR_INVALIDDATA;
  358. }
  359. /* read correction pairs */
  360. for (i = 0; i < band->num_corr * 2; i++)
  361. band->corr[i] = get_bits(&ctx->gb, 8);
  362. }
  363. }
  364. if (band->blk_size == 8) {
  365. band->intra_base = &ivi4_quant_8x8_intra[quant_index_to_tab[band->quant_mat]][0];
  366. band->inter_base = &ivi4_quant_8x8_inter[quant_index_to_tab[band->quant_mat]][0];
  367. } else {
  368. band->intra_base = &ivi4_quant_4x4_intra[quant_index_to_tab[band->quant_mat]][0];
  369. band->inter_base = &ivi4_quant_4x4_inter[quant_index_to_tab[band->quant_mat]][0];
  370. }
  371. /* Indeo 4 doesn't use scale tables */
  372. band->intra_scale = NULL;
  373. band->inter_scale = NULL;
  374. align_get_bits(&ctx->gb);
  375. if (!band->scan) {
  376. av_log(avctx, AV_LOG_ERROR, "band->scan not set\n");
  377. return AVERROR_INVALIDDATA;
  378. }
  379. return 0;
  380. }
  381. /**
  382. * Decode information (block type, cbp, quant delta, motion vector)
  383. * for all macroblocks in the current tile.
  384. *
  385. * @param[in,out] ctx pointer to the decoder context
  386. * @param[in,out] band pointer to the band descriptor
  387. * @param[in,out] tile pointer to the tile descriptor
  388. * @param[in] avctx pointer to the AVCodecContext
  389. * @return result code: 0 = OK, negative number = error
  390. */
  391. static int decode_mb_info(IVI45DecContext *ctx, IVIBandDesc *band,
  392. IVITile *tile, AVCodecContext *avctx)
  393. {
  394. int x, y, mv_x, mv_y, mv_delta, offs, mb_offset, blks_per_mb,
  395. mv_scale, mb_type_bits, s;
  396. IVIMbInfo *mb, *ref_mb;
  397. int row_offset = band->mb_size * band->pitch;
  398. mb = tile->mbs;
  399. ref_mb = tile->ref_mbs;
  400. offs = tile->ypos * band->pitch + tile->xpos;
  401. blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
  402. mb_type_bits = ctx->frame_type == IVI4_FRAMETYPE_BIDIR ? 2 : 1;
  403. /* scale factor for motion vectors */
  404. mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
  405. mv_x = mv_y = 0;
  406. if (((tile->width + band->mb_size-1)/band->mb_size) * ((tile->height + band->mb_size-1)/band->mb_size) != tile->num_MBs) {
  407. av_log(avctx, AV_LOG_ERROR, "num_MBs mismatch %d %d %d %d\n", tile->width, tile->height, band->mb_size, tile->num_MBs);
  408. return -1;
  409. }
  410. for (y = tile->ypos; y < tile->ypos + tile->height; y += band->mb_size) {
  411. mb_offset = offs;
  412. for (x = tile->xpos; x < tile->xpos + tile->width; x += band->mb_size) {
  413. mb->xpos = x;
  414. mb->ypos = y;
  415. mb->buf_offs = mb_offset;
  416. mb->b_mv_x =
  417. mb->b_mv_y = 0;
  418. if (get_bits1(&ctx->gb)) {
  419. if (ctx->frame_type == IVI4_FRAMETYPE_INTRA) {
  420. av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
  421. return AVERROR_INVALIDDATA;
  422. }
  423. mb->type = 1; /* empty macroblocks are always INTER */
  424. mb->cbp = 0; /* all blocks are empty */
  425. mb->q_delta = 0;
  426. if (!band->plane && !band->band_num && ctx->in_q) {
  427. mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  428. IVI_VLC_BITS, 1);
  429. mb->q_delta = IVI_TOSIGNED(mb->q_delta);
  430. }
  431. mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
  432. if (band->inherit_mv && ref_mb) {
  433. /* motion vector inheritance */
  434. if (mv_scale) {
  435. mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
  436. mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
  437. } else {
  438. mb->mv_x = ref_mb->mv_x;
  439. mb->mv_y = ref_mb->mv_y;
  440. }
  441. }
  442. } else {
  443. if (band->inherit_mv) {
  444. /* copy mb_type from corresponding reference mb */
  445. if (!ref_mb) {
  446. av_log(avctx, AV_LOG_ERROR, "ref_mb unavailable\n");
  447. return AVERROR_INVALIDDATA;
  448. }
  449. mb->type = ref_mb->type;
  450. } else if (ctx->frame_type == IVI4_FRAMETYPE_INTRA ||
  451. ctx->frame_type == IVI4_FRAMETYPE_INTRA1) {
  452. mb->type = 0; /* mb_type is always INTRA for intra-frames */
  453. } else {
  454. mb->type = get_bits(&ctx->gb, mb_type_bits);
  455. }
  456. mb->cbp = get_bits(&ctx->gb, blks_per_mb);
  457. mb->q_delta = 0;
  458. if (band->inherit_qdelta) {
  459. if (ref_mb) mb->q_delta = ref_mb->q_delta;
  460. } else if (mb->cbp || (!band->plane && !band->band_num &&
  461. ctx->in_q)) {
  462. mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  463. IVI_VLC_BITS, 1);
  464. mb->q_delta = IVI_TOSIGNED(mb->q_delta);
  465. }
  466. if (!mb->type) {
  467. mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
  468. } else {
  469. if (band->inherit_mv) {
  470. if (ref_mb)
  471. /* motion vector inheritance */
  472. if (mv_scale) {
  473. mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
  474. mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
  475. } else {
  476. mb->mv_x = ref_mb->mv_x;
  477. mb->mv_y = ref_mb->mv_y;
  478. }
  479. } else {
  480. /* decode motion vector deltas */
  481. mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  482. IVI_VLC_BITS, 1);
  483. mv_y += IVI_TOSIGNED(mv_delta);
  484. mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  485. IVI_VLC_BITS, 1);
  486. mv_x += IVI_TOSIGNED(mv_delta);
  487. mb->mv_x = mv_x;
  488. mb->mv_y = mv_y;
  489. if (mb->type == 3) {
  490. mv_delta = get_vlc2(&ctx->gb,
  491. ctx->mb_vlc.tab->table,
  492. IVI_VLC_BITS, 1);
  493. mv_y += IVI_TOSIGNED(mv_delta);
  494. mv_delta = get_vlc2(&ctx->gb,
  495. ctx->mb_vlc.tab->table,
  496. IVI_VLC_BITS, 1);
  497. mv_x += IVI_TOSIGNED(mv_delta);
  498. mb->b_mv_x = -mv_x;
  499. mb->b_mv_y = -mv_y;
  500. }
  501. }
  502. if (mb->type == 2) {
  503. mb->b_mv_x = -mb->mv_x;
  504. mb->b_mv_y = -mb->mv_y;
  505. mb->mv_x = 0;
  506. mb->mv_y = 0;
  507. }
  508. }
  509. }
  510. s= band->is_halfpel;
  511. if (mb->type)
  512. if ( x + (mb->mv_x >>s) + (y+ (mb->mv_y >>s))*band->pitch < 0 ||
  513. x + ((mb->mv_x+s)>>s) + band->mb_size - 1
  514. + (y+band->mb_size - 1 +((mb->mv_y+s)>>s))*band->pitch > band->bufsize -1) {
  515. av_log(avctx, AV_LOG_ERROR, "motion vector %d %d outside reference\n", x*s + mb->mv_x, y*s + mb->mv_y);
  516. return AVERROR_INVALIDDATA;
  517. }
  518. mb++;
  519. if (ref_mb)
  520. ref_mb++;
  521. mb_offset += band->mb_size;
  522. }
  523. offs += row_offset;
  524. }
  525. align_get_bits(&ctx->gb);
  526. return 0;
  527. }
  528. /**
  529. * Rearrange decoding and reference buffers.
  530. *
  531. * @param[in,out] ctx pointer to the decoder context
  532. */
  533. static void switch_buffers(IVI45DecContext *ctx)
  534. {
  535. int is_prev_ref = 0, is_ref = 0;
  536. switch (ctx->prev_frame_type) {
  537. case IVI4_FRAMETYPE_INTRA:
  538. case IVI4_FRAMETYPE_INTRA1:
  539. case IVI4_FRAMETYPE_INTER:
  540. is_prev_ref = 1;
  541. break;
  542. }
  543. switch (ctx->frame_type) {
  544. case IVI4_FRAMETYPE_INTRA:
  545. case IVI4_FRAMETYPE_INTRA1:
  546. case IVI4_FRAMETYPE_INTER:
  547. is_ref = 1;
  548. break;
  549. }
  550. if (is_prev_ref && is_ref) {
  551. FFSWAP(int, ctx->dst_buf, ctx->ref_buf);
  552. } else if (is_prev_ref) {
  553. FFSWAP(int, ctx->ref_buf, ctx->b_ref_buf);
  554. FFSWAP(int, ctx->dst_buf, ctx->ref_buf);
  555. }
  556. }
  557. static int is_nonnull_frame(IVI45DecContext *ctx)
  558. {
  559. return ctx->frame_type < IVI4_FRAMETYPE_NULL_FIRST;
  560. }
  561. static av_cold int decode_init(AVCodecContext *avctx)
  562. {
  563. IVI45DecContext *ctx = avctx->priv_data;
  564. ff_ivi_init_static_vlc();
  565. /* copy rvmap tables in our context so we can apply changes to them */
  566. memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
  567. /* Force allocation of the internal buffers */
  568. /* during picture header decoding. */
  569. ctx->pic_conf.pic_width = 0;
  570. ctx->pic_conf.pic_height = 0;
  571. avctx->pix_fmt = AV_PIX_FMT_YUV410P;
  572. ctx->decode_pic_hdr = decode_pic_hdr;
  573. ctx->decode_band_hdr = decode_band_hdr;
  574. ctx->decode_mb_info = decode_mb_info;
  575. ctx->switch_buffers = switch_buffers;
  576. ctx->is_nonnull_frame = is_nonnull_frame;
  577. ctx->is_indeo4 = 1;
  578. ctx->show_indeo4_info = 1;
  579. ctx->dst_buf = 0;
  580. ctx->ref_buf = 1;
  581. ctx->b_ref_buf = 3; /* buffer 2 is used for scalability mode */
  582. ctx->p_frame = av_frame_alloc();
  583. if (!ctx->p_frame)
  584. return AVERROR(ENOMEM);
  585. return 0;
  586. }
  587. AVCodec ff_indeo4_decoder = {
  588. .name = "indeo4",
  589. .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 4"),
  590. .type = AVMEDIA_TYPE_VIDEO,
  591. .id = AV_CODEC_ID_INDEO4,
  592. .priv_data_size = sizeof(IVI45DecContext),
  593. .init = decode_init,
  594. .close = ff_ivi_decode_close,
  595. .decode = ff_ivi_decode_frame,
  596. .capabilities = AV_CODEC_CAP_DR1,
  597. };