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.

661 lines
23KB

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