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.

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