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.

665 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 "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. if (pic_conf.luma_bands)
  166. pic_conf.chroma_bands = decode_plane_subdivision(&ctx->gb);
  167. ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
  168. if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
  169. av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
  170. pic_conf.luma_bands, pic_conf.chroma_bands);
  171. return AVERROR_INVALIDDATA;
  172. }
  173. /* check if picture layout was changed and reallocate buffers */
  174. if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) {
  175. if (ff_ivi_init_planes(ctx->planes, &pic_conf, 1)) {
  176. av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
  177. ctx->pic_conf.luma_bands = 0;
  178. return AVERROR(ENOMEM);
  179. }
  180. ctx->pic_conf = pic_conf;
  181. /* set default macroblock/block dimensions */
  182. for (p = 0; p <= 2; p++) {
  183. for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
  184. ctx->planes[p].bands[i].mb_size = !p ? (!ctx->is_scalable ? 16 : 8) : 4;
  185. ctx->planes[p].bands[i].blk_size = !p ? 8 : 4;
  186. }
  187. }
  188. if (ff_ivi_init_tiles(ctx->planes, ctx->pic_conf.tile_width,
  189. ctx->pic_conf.tile_height)) {
  190. av_log(avctx, AV_LOG_ERROR,
  191. "Couldn't reallocate internal structures!\n");
  192. return AVERROR(ENOMEM);
  193. }
  194. }
  195. ctx->frame_num = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 20) : 0;
  196. /* skip decTimeEst field if present */
  197. if (get_bits1(&ctx->gb))
  198. skip_bits(&ctx->gb, 8);
  199. /* decode macroblock and block huffman codebooks */
  200. if (ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_MB_HUFF, &ctx->mb_vlc, avctx) ||
  201. ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_BLK_HUFF, &ctx->blk_vlc, avctx))
  202. return AVERROR_INVALIDDATA;
  203. ctx->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8;
  204. ctx->in_imf = get_bits1(&ctx->gb);
  205. ctx->in_q = get_bits1(&ctx->gb);
  206. ctx->pic_glob_quant = get_bits(&ctx->gb, 5);
  207. /* TODO: ignore this parameter if unused */
  208. ctx->unknown1 = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 0;
  209. ctx->checksum = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 16) : 0;
  210. /* skip picture header extension if any */
  211. while (get_bits1(&ctx->gb)) {
  212. av_dlog(avctx, "Pic hdr extension encountered!\n");
  213. skip_bits(&ctx->gb, 8);
  214. }
  215. if (get_bits1(&ctx->gb)) {
  216. av_log(avctx, AV_LOG_ERROR, "Bad blocks bits encountered!\n");
  217. }
  218. align_get_bits(&ctx->gb);
  219. return 0;
  220. }
  221. /**
  222. * Decode Indeo 4 band header.
  223. *
  224. * @param[in,out] ctx pointer to the decoder context
  225. * @param[in,out] band pointer to the band descriptor
  226. * @param[in] avctx pointer to the AVCodecContext
  227. * @return result code: 0 = OK, negative number = error
  228. */
  229. static int decode_band_hdr(IVI45DecContext *ctx, IVIBandDesc *band,
  230. AVCodecContext *avctx)
  231. {
  232. int plane, band_num, indx, transform_id, scan_indx;
  233. int i;
  234. plane = get_bits(&ctx->gb, 2);
  235. band_num = get_bits(&ctx->gb, 4);
  236. if (band->plane != plane || band->band_num != band_num) {
  237. av_log(avctx, AV_LOG_ERROR, "Invalid band header sequence!\n");
  238. return AVERROR_INVALIDDATA;
  239. }
  240. band->is_empty = get_bits1(&ctx->gb);
  241. if (!band->is_empty) {
  242. int old_blk_size = band->blk_size;
  243. /* skip header size
  244. * If header size is not given, header size is 4 bytes. */
  245. if (get_bits1(&ctx->gb))
  246. skip_bits(&ctx->gb, 16);
  247. band->is_halfpel = get_bits(&ctx->gb, 2);
  248. if (band->is_halfpel >= 2) {
  249. av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported mv resolution: %d!\n",
  250. band->is_halfpel);
  251. return AVERROR_INVALIDDATA;
  252. }
  253. #if IVI4_STREAM_ANALYSER
  254. if (!band->is_halfpel)
  255. ctx->uses_fullpel = 1;
  256. #endif
  257. band->checksum_present = get_bits1(&ctx->gb);
  258. if (band->checksum_present)
  259. band->checksum = get_bits(&ctx->gb, 16);
  260. indx = get_bits(&ctx->gb, 2);
  261. if (indx == 3) {
  262. av_log(avctx, AV_LOG_ERROR, "Invalid block size!\n");
  263. return AVERROR_INVALIDDATA;
  264. }
  265. band->mb_size = 16 >> indx;
  266. band->blk_size = 8 >> (indx >> 1);
  267. band->inherit_mv = get_bits1(&ctx->gb);
  268. band->inherit_qdelta = get_bits1(&ctx->gb);
  269. band->glob_quant = get_bits(&ctx->gb, 5);
  270. if (!get_bits1(&ctx->gb) || ctx->frame_type == IVI4_FRAMETYPE_INTRA) {
  271. transform_id = get_bits(&ctx->gb, 5);
  272. if (transform_id >= FF_ARRAY_ELEMS(transforms) ||
  273. !transforms[transform_id].inv_trans) {
  274. avpriv_request_sample(avctx, "Transform %d", transform_id);
  275. return AVERROR_PATCHWELCOME;
  276. }
  277. if ((transform_id >= 7 && transform_id <= 9) ||
  278. transform_id == 17) {
  279. avpriv_request_sample(avctx, "DCT transform");
  280. return AVERROR_PATCHWELCOME;
  281. }
  282. #if IVI4_STREAM_ANALYSER
  283. if ((transform_id >= 0 && transform_id <= 2) || transform_id == 10)
  284. ctx->uses_haar = 1;
  285. #endif
  286. band->inv_transform = transforms[transform_id].inv_trans;
  287. band->dc_transform = transforms[transform_id].dc_trans;
  288. band->is_2d_trans = transforms[transform_id].is_2d_trans;
  289. if (transform_id < 10)
  290. band->transform_size = 8;
  291. else
  292. band->transform_size = 4;
  293. if (band->blk_size != band->transform_size)
  294. return AVERROR_INVALIDDATA;
  295. scan_indx = get_bits(&ctx->gb, 4);
  296. if (scan_indx == 15) {
  297. av_log(avctx, AV_LOG_ERROR, "Custom scan pattern encountered!\n");
  298. return AVERROR_INVALIDDATA;
  299. }
  300. if (scan_indx > 4 && scan_indx < 10) {
  301. if (band->blk_size != 4)
  302. return AVERROR_INVALIDDATA;
  303. } else if (band->blk_size != 8)
  304. return AVERROR_INVALIDDATA;
  305. band->scan = scan_index_to_tab[scan_indx];
  306. band->quant_mat = get_bits(&ctx->gb, 5);
  307. if (band->quant_mat >= FF_ARRAY_ELEMS(quant_index_to_tab)) {
  308. if (band->quant_mat == 31)
  309. av_log(avctx, AV_LOG_ERROR,
  310. "Custom quant matrix encountered!\n");
  311. else
  312. avpriv_request_sample(avctx, "Quantization matrix %d",
  313. band->quant_mat);
  314. band->quant_mat = -1;
  315. return AVERROR_INVALIDDATA;
  316. }
  317. } else {
  318. if (old_blk_size != band->blk_size) {
  319. av_log(avctx, AV_LOG_ERROR,
  320. "The band block size does not match the configuration "
  321. "inherited\n");
  322. return AVERROR_INVALIDDATA;
  323. }
  324. if (band->quant_mat < 0) {
  325. av_log(avctx, AV_LOG_ERROR, "Invalid quant_mat inherited\n");
  326. return AVERROR_INVALIDDATA;
  327. }
  328. }
  329. /* decode block huffman codebook */
  330. if (!get_bits1(&ctx->gb))
  331. band->blk_vlc.tab = ctx->blk_vlc.tab;
  332. else
  333. if (ff_ivi_dec_huff_desc(&ctx->gb, 1, IVI_BLK_HUFF,
  334. &band->blk_vlc, avctx))
  335. return AVERROR_INVALIDDATA;
  336. /* select appropriate rvmap table for this band */
  337. band->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8;
  338. /* decode rvmap probability corrections if any */
  339. band->num_corr = 0; /* there is no corrections */
  340. if (get_bits1(&ctx->gb)) {
  341. band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
  342. if (band->num_corr > 61) {
  343. av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
  344. band->num_corr);
  345. return AVERROR_INVALIDDATA;
  346. }
  347. /* read correction pairs */
  348. for (i = 0; i < band->num_corr * 2; i++)
  349. band->corr[i] = get_bits(&ctx->gb, 8);
  350. }
  351. }
  352. if (band->blk_size == 8) {
  353. band->intra_base = &ivi4_quant_8x8_intra[quant_index_to_tab[band->quant_mat]][0];
  354. band->inter_base = &ivi4_quant_8x8_inter[quant_index_to_tab[band->quant_mat]][0];
  355. } else {
  356. band->intra_base = &ivi4_quant_4x4_intra[quant_index_to_tab[band->quant_mat]][0];
  357. band->inter_base = &ivi4_quant_4x4_inter[quant_index_to_tab[band->quant_mat]][0];
  358. }
  359. /* Indeo 4 doesn't use scale tables */
  360. band->intra_scale = NULL;
  361. band->inter_scale = NULL;
  362. align_get_bits(&ctx->gb);
  363. return 0;
  364. }
  365. /**
  366. * Decode information (block type, cbp, quant delta, motion vector)
  367. * for all macroblocks in the current tile.
  368. *
  369. * @param[in,out] ctx pointer to the decoder context
  370. * @param[in,out] band pointer to the band descriptor
  371. * @param[in,out] tile pointer to the tile descriptor
  372. * @param[in] avctx pointer to the AVCodecContext
  373. * @return result code: 0 = OK, negative number = error
  374. */
  375. static int decode_mb_info(IVI45DecContext *ctx, IVIBandDesc *band,
  376. IVITile *tile, AVCodecContext *avctx)
  377. {
  378. int x, y, mv_x, mv_y, mv_delta, offs, mb_offset, blks_per_mb,
  379. mv_scale, mb_type_bits;
  380. IVIMbInfo *mb, *ref_mb;
  381. int row_offset = band->mb_size * band->pitch;
  382. mb = tile->mbs;
  383. ref_mb = tile->ref_mbs;
  384. offs = tile->ypos * band->pitch + tile->xpos;
  385. blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
  386. mb_type_bits = ctx->frame_type == IVI4_FRAMETYPE_BIDIR ? 2 : 1;
  387. /* scale factor for motion vectors */
  388. mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
  389. mv_x = mv_y = 0;
  390. for (y = tile->ypos; y < tile->ypos + tile->height; y += band->mb_size) {
  391. mb_offset = offs;
  392. for (x = tile->xpos; x < tile->xpos + tile->width; x += band->mb_size) {
  393. mb->xpos = x;
  394. mb->ypos = y;
  395. mb->buf_offs = mb_offset;
  396. mb->b_mv_x =
  397. mb->b_mv_y = 0;
  398. if (get_bits1(&ctx->gb)) {
  399. if (ctx->frame_type == IVI4_FRAMETYPE_INTRA) {
  400. av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
  401. return AVERROR_INVALIDDATA;
  402. }
  403. mb->type = 1; /* empty macroblocks are always INTER */
  404. mb->cbp = 0; /* all blocks are empty */
  405. mb->q_delta = 0;
  406. if (!band->plane && !band->band_num && ctx->in_q) {
  407. mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  408. IVI_VLC_BITS, 1);
  409. mb->q_delta = IVI_TOSIGNED(mb->q_delta);
  410. }
  411. mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
  412. if (band->inherit_mv && ref_mb) {
  413. /* motion vector inheritance */
  414. if (mv_scale) {
  415. mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
  416. mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
  417. } else {
  418. mb->mv_x = ref_mb->mv_x;
  419. mb->mv_y = ref_mb->mv_y;
  420. }
  421. }
  422. } else {
  423. if (band->inherit_mv) {
  424. /* copy mb_type from corresponding reference mb */
  425. if (!ref_mb)
  426. return AVERROR_INVALIDDATA;
  427. mb->type = ref_mb->type;
  428. } else if (ctx->frame_type == IVI4_FRAMETYPE_INTRA ||
  429. ctx->frame_type == IVI4_FRAMETYPE_INTRA1) {
  430. mb->type = 0; /* mb_type is always INTRA for intra-frames */
  431. } else {
  432. mb->type = get_bits(&ctx->gb, mb_type_bits);
  433. }
  434. mb->cbp = get_bits(&ctx->gb, blks_per_mb);
  435. mb->q_delta = 0;
  436. if (band->inherit_qdelta) {
  437. if (ref_mb) mb->q_delta = ref_mb->q_delta;
  438. } else if (mb->cbp || (!band->plane && !band->band_num &&
  439. ctx->in_q)) {
  440. mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  441. IVI_VLC_BITS, 1);
  442. mb->q_delta = IVI_TOSIGNED(mb->q_delta);
  443. }
  444. if (!mb->type) {
  445. mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
  446. } else {
  447. if (band->inherit_mv) {
  448. if (ref_mb)
  449. /* motion vector inheritance */
  450. if (mv_scale) {
  451. mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
  452. mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
  453. } else {
  454. mb->mv_x = ref_mb->mv_x;
  455. mb->mv_y = ref_mb->mv_y;
  456. }
  457. } else {
  458. /* decode motion vector deltas */
  459. mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  460. IVI_VLC_BITS, 1);
  461. mv_y += IVI_TOSIGNED(mv_delta);
  462. mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  463. IVI_VLC_BITS, 1);
  464. mv_x += IVI_TOSIGNED(mv_delta);
  465. mb->mv_x = mv_x;
  466. mb->mv_y = mv_y;
  467. if (mb->type == 3) {
  468. mv_delta = get_vlc2(&ctx->gb,
  469. ctx->mb_vlc.tab->table,
  470. IVI_VLC_BITS, 1);
  471. mv_y += IVI_TOSIGNED(mv_delta);
  472. mv_delta = get_vlc2(&ctx->gb,
  473. ctx->mb_vlc.tab->table,
  474. IVI_VLC_BITS, 1);
  475. mv_x += IVI_TOSIGNED(mv_delta);
  476. mb->b_mv_x = -mv_x;
  477. mb->b_mv_y = -mv_y;
  478. }
  479. }
  480. if (mb->type == 2) {
  481. mb->b_mv_x = -mb->mv_x;
  482. mb->b_mv_y = -mb->mv_y;
  483. mb->mv_x = 0;
  484. mb->mv_y = 0;
  485. }
  486. }
  487. }
  488. mb++;
  489. if (ref_mb)
  490. ref_mb++;
  491. mb_offset += band->mb_size;
  492. }
  493. offs += row_offset;
  494. }
  495. align_get_bits(&ctx->gb);
  496. return 0;
  497. }
  498. /**
  499. * Rearrange decoding and reference buffers.
  500. *
  501. * @param[in,out] ctx pointer to the decoder context
  502. */
  503. static void switch_buffers(IVI45DecContext *ctx)
  504. {
  505. int is_prev_ref = 0, is_ref = 0;
  506. switch (ctx->prev_frame_type) {
  507. case IVI4_FRAMETYPE_INTRA:
  508. case IVI4_FRAMETYPE_INTRA1:
  509. case IVI4_FRAMETYPE_INTER:
  510. is_prev_ref = 1;
  511. break;
  512. }
  513. switch (ctx->frame_type) {
  514. case IVI4_FRAMETYPE_INTRA:
  515. case IVI4_FRAMETYPE_INTRA1:
  516. case IVI4_FRAMETYPE_INTER:
  517. is_ref = 1;
  518. break;
  519. }
  520. if (is_prev_ref && is_ref) {
  521. FFSWAP(int, ctx->dst_buf, ctx->ref_buf);
  522. } else if (is_prev_ref) {
  523. FFSWAP(int, ctx->ref_buf, ctx->b_ref_buf);
  524. FFSWAP(int, ctx->dst_buf, ctx->ref_buf);
  525. }
  526. }
  527. static int is_nonnull_frame(IVI45DecContext *ctx)
  528. {
  529. return ctx->frame_type < IVI4_FRAMETYPE_NULL_FIRST;
  530. }
  531. static av_cold int decode_init(AVCodecContext *avctx)
  532. {
  533. IVI45DecContext *ctx = avctx->priv_data;
  534. ff_ivi_init_static_vlc();
  535. /* copy rvmap tables in our context so we can apply changes to them */
  536. memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
  537. /* Force allocation of the internal buffers */
  538. /* during picture header decoding. */
  539. ctx->pic_conf.pic_width = 0;
  540. ctx->pic_conf.pic_height = 0;
  541. avctx->pix_fmt = AV_PIX_FMT_YUV410P;
  542. ctx->decode_pic_hdr = decode_pic_hdr;
  543. ctx->decode_band_hdr = decode_band_hdr;
  544. ctx->decode_mb_info = decode_mb_info;
  545. ctx->switch_buffers = switch_buffers;
  546. ctx->is_nonnull_frame = is_nonnull_frame;
  547. ctx->is_indeo4 = 1;
  548. ctx->dst_buf = 0;
  549. ctx->ref_buf = 1;
  550. ctx->b_ref_buf = 3; /* buffer 2 is used for scalability mode */
  551. ctx->p_frame = av_frame_alloc();
  552. if (!ctx->p_frame)
  553. return AVERROR(ENOMEM);
  554. return 0;
  555. }
  556. AVCodec ff_indeo4_decoder = {
  557. .name = "indeo4",
  558. .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 4"),
  559. .type = AVMEDIA_TYPE_VIDEO,
  560. .id = AV_CODEC_ID_INDEO4,
  561. .priv_data_size = sizeof(IVI45DecContext),
  562. .init = decode_init,
  563. .close = ff_ivi_decode_close,
  564. .decode = ff_ivi_decode_frame,
  565. .capabilities = CODEC_CAP_DR1,
  566. };