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.

666 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.h"
  32. #include "ivi_dsp.h"
  33. #include "indeo4data.h"
  34. #include "internal.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 IVI4_STREAM_ANALYSER
  111. if (ctx->frame_type == IVI4_FRAMETYPE_BIDIR)
  112. ctx->has_b_frames = 1;
  113. #endif
  114. ctx->transp_status = get_bits1(&ctx->gb);
  115. #if IVI4_STREAM_ANALYSER
  116. if (ctx->transp_status) {
  117. ctx->has_transp = 1;
  118. }
  119. #endif
  120. /* unknown bit: Mac decoder ignores this bit, XANIM returns error */
  121. if (get_bits1(&ctx->gb)) {
  122. av_log(avctx, AV_LOG_ERROR, "Sync bit is set!\n");
  123. return AVERROR_INVALIDDATA;
  124. }
  125. ctx->data_size = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 24) : 0;
  126. /* null frames don't contain anything else so we just return */
  127. if (ctx->frame_type >= IVI4_FRAMETYPE_NULL_FIRST) {
  128. ff_dlog(avctx, "Null frame encountered!\n");
  129. return 0;
  130. }
  131. /* Check key lock status. If enabled - ignore lock word. */
  132. /* Usually we have to prompt the user for the password, but */
  133. /* we don't do that because Indeo 4 videos can be decoded anyway */
  134. if (get_bits1(&ctx->gb)) {
  135. skip_bits_long(&ctx->gb, 32);
  136. ff_dlog(avctx, "Password-protected clip!\n");
  137. }
  138. pic_size_indx = get_bits(&ctx->gb, 3);
  139. if (pic_size_indx == IVI4_PIC_SIZE_ESC) {
  140. pic_conf.pic_height = get_bits(&ctx->gb, 16);
  141. pic_conf.pic_width = get_bits(&ctx->gb, 16);
  142. } else {
  143. pic_conf.pic_height = ivi4_common_pic_sizes[pic_size_indx * 2 + 1];
  144. pic_conf.pic_width = ivi4_common_pic_sizes[pic_size_indx * 2 ];
  145. }
  146. /* Decode tile dimensions. */
  147. if (get_bits1(&ctx->gb)) {
  148. pic_conf.tile_height = scale_tile_size(pic_conf.pic_height, get_bits(&ctx->gb, 4));
  149. pic_conf.tile_width = scale_tile_size(pic_conf.pic_width, get_bits(&ctx->gb, 4));
  150. #if IVI4_STREAM_ANALYSER
  151. ctx->uses_tiling = 1;
  152. #endif
  153. } else {
  154. pic_conf.tile_height = pic_conf.pic_height;
  155. pic_conf.tile_width = pic_conf.pic_width;
  156. }
  157. /* Decode chroma subsampling. We support only 4:4 aka YVU9. */
  158. if (get_bits(&ctx->gb, 2)) {
  159. av_log(avctx, AV_LOG_ERROR, "Only YVU9 picture format is supported!\n");
  160. return AVERROR_INVALIDDATA;
  161. }
  162. pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
  163. pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2;
  164. /* decode subdivision of the planes */
  165. pic_conf.luma_bands = decode_plane_subdivision(&ctx->gb);
  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. ff_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. plane = get_bits(&ctx->gb, 2);
  236. band_num = get_bits(&ctx->gb, 4);
  237. if (band->plane != plane || band->band_num != band_num) {
  238. av_log(avctx, AV_LOG_ERROR, "Invalid band header sequence!\n");
  239. return AVERROR_INVALIDDATA;
  240. }
  241. band->is_empty = get_bits1(&ctx->gb);
  242. if (!band->is_empty) {
  243. int old_blk_size = band->blk_size;
  244. /* skip header size
  245. * If header size is not given, header size is 4 bytes. */
  246. if (get_bits1(&ctx->gb))
  247. skip_bits(&ctx->gb, 16);
  248. band->is_halfpel = get_bits(&ctx->gb, 2);
  249. if (band->is_halfpel >= 2) {
  250. av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported mv resolution: %d!\n",
  251. band->is_halfpel);
  252. return AVERROR_INVALIDDATA;
  253. }
  254. #if IVI4_STREAM_ANALYSER
  255. if (!band->is_halfpel)
  256. ctx->uses_fullpel = 1;
  257. #endif
  258. band->checksum_present = get_bits1(&ctx->gb);
  259. if (band->checksum_present)
  260. band->checksum = get_bits(&ctx->gb, 16);
  261. indx = get_bits(&ctx->gb, 2);
  262. if (indx == 3) {
  263. av_log(avctx, AV_LOG_ERROR, "Invalid block size!\n");
  264. return AVERROR_INVALIDDATA;
  265. }
  266. band->mb_size = 16 >> indx;
  267. band->blk_size = 8 >> (indx >> 1);
  268. band->inherit_mv = get_bits1(&ctx->gb);
  269. band->inherit_qdelta = get_bits1(&ctx->gb);
  270. band->glob_quant = get_bits(&ctx->gb, 5);
  271. if (!get_bits1(&ctx->gb) || ctx->frame_type == IVI4_FRAMETYPE_INTRA) {
  272. transform_id = get_bits(&ctx->gb, 5);
  273. if (transform_id >= FF_ARRAY_ELEMS(transforms) ||
  274. !transforms[transform_id].inv_trans) {
  275. avpriv_request_sample(avctx, "Transform %d", transform_id);
  276. return AVERROR_PATCHWELCOME;
  277. }
  278. if ((transform_id >= 7 && transform_id <= 9) ||
  279. transform_id == 17) {
  280. avpriv_request_sample(avctx, "DCT transform");
  281. return AVERROR_PATCHWELCOME;
  282. }
  283. #if IVI4_STREAM_ANALYSER
  284. if ((transform_id >= 0 && transform_id <= 2) || transform_id == 10)
  285. ctx->uses_haar = 1;
  286. #endif
  287. band->inv_transform = transforms[transform_id].inv_trans;
  288. band->dc_transform = transforms[transform_id].dc_trans;
  289. band->is_2d_trans = transforms[transform_id].is_2d_trans;
  290. if (transform_id < 10)
  291. band->transform_size = 8;
  292. else
  293. band->transform_size = 4;
  294. if (band->blk_size != band->transform_size)
  295. return AVERROR_INVALIDDATA;
  296. scan_indx = get_bits(&ctx->gb, 4);
  297. if (scan_indx == 15) {
  298. av_log(avctx, AV_LOG_ERROR, "Custom scan pattern encountered!\n");
  299. return AVERROR_INVALIDDATA;
  300. }
  301. if (scan_indx > 4 && scan_indx < 10) {
  302. if (band->blk_size != 4)
  303. return AVERROR_INVALIDDATA;
  304. } else if (band->blk_size != 8)
  305. return AVERROR_INVALIDDATA;
  306. band->scan = scan_index_to_tab[scan_indx];
  307. band->quant_mat = get_bits(&ctx->gb, 5);
  308. if (band->quant_mat >= FF_ARRAY_ELEMS(quant_index_to_tab)) {
  309. if (band->quant_mat == 31)
  310. av_log(avctx, AV_LOG_ERROR,
  311. "Custom quant matrix encountered!\n");
  312. else
  313. avpriv_request_sample(avctx, "Quantization matrix %d",
  314. band->quant_mat);
  315. band->quant_mat = -1;
  316. return AVERROR_INVALIDDATA;
  317. }
  318. } else {
  319. if (old_blk_size != band->blk_size) {
  320. av_log(avctx, AV_LOG_ERROR,
  321. "The band block size does not match the configuration "
  322. "inherited\n");
  323. return AVERROR_INVALIDDATA;
  324. }
  325. if (band->quant_mat < 0) {
  326. av_log(avctx, AV_LOG_ERROR, "Invalid quant_mat inherited\n");
  327. return AVERROR_INVALIDDATA;
  328. }
  329. }
  330. /* decode block huffman codebook */
  331. if (!get_bits1(&ctx->gb))
  332. band->blk_vlc.tab = ctx->blk_vlc.tab;
  333. else
  334. if (ff_ivi_dec_huff_desc(&ctx->gb, 1, IVI_BLK_HUFF,
  335. &band->blk_vlc, avctx))
  336. return AVERROR_INVALIDDATA;
  337. /* select appropriate rvmap table for this band */
  338. band->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8;
  339. /* decode rvmap probability corrections if any */
  340. band->num_corr = 0; /* there is no corrections */
  341. if (get_bits1(&ctx->gb)) {
  342. band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
  343. if (band->num_corr > 61) {
  344. av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
  345. band->num_corr);
  346. return AVERROR_INVALIDDATA;
  347. }
  348. /* read correction pairs */
  349. for (i = 0; i < band->num_corr * 2; i++)
  350. band->corr[i] = get_bits(&ctx->gb, 8);
  351. }
  352. }
  353. if (band->blk_size == 8) {
  354. band->intra_base = &ivi4_quant_8x8_intra[quant_index_to_tab[band->quant_mat]][0];
  355. band->inter_base = &ivi4_quant_8x8_inter[quant_index_to_tab[band->quant_mat]][0];
  356. } else {
  357. band->intra_base = &ivi4_quant_4x4_intra[quant_index_to_tab[band->quant_mat]][0];
  358. band->inter_base = &ivi4_quant_4x4_inter[quant_index_to_tab[band->quant_mat]][0];
  359. }
  360. /* Indeo 4 doesn't use scale tables */
  361. band->intra_scale = NULL;
  362. band->inter_scale = NULL;
  363. align_get_bits(&ctx->gb);
  364. return 0;
  365. }
  366. /**
  367. * Decode information (block type, cbp, quant delta, motion vector)
  368. * for all macroblocks in the current tile.
  369. *
  370. * @param[in,out] ctx pointer to the decoder context
  371. * @param[in,out] band pointer to the band descriptor
  372. * @param[in,out] tile pointer to the tile descriptor
  373. * @param[in] avctx pointer to the AVCodecContext
  374. * @return result code: 0 = OK, negative number = error
  375. */
  376. static int decode_mb_info(IVI45DecContext *ctx, IVIBandDesc *band,
  377. IVITile *tile, AVCodecContext *avctx)
  378. {
  379. int x, y, mv_x, mv_y, mv_delta, offs, mb_offset, blks_per_mb,
  380. mv_scale, mb_type_bits;
  381. IVIMbInfo *mb, *ref_mb;
  382. int row_offset = band->mb_size * band->pitch;
  383. mb = tile->mbs;
  384. ref_mb = tile->ref_mbs;
  385. offs = tile->ypos * band->pitch + tile->xpos;
  386. blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
  387. mb_type_bits = ctx->frame_type == IVI4_FRAMETYPE_BIDIR ? 2 : 1;
  388. /* scale factor for motion vectors */
  389. mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
  390. mv_x = mv_y = 0;
  391. for (y = tile->ypos; y < tile->ypos + tile->height; y += band->mb_size) {
  392. mb_offset = offs;
  393. for (x = tile->xpos; x < tile->xpos + tile->width; x += band->mb_size) {
  394. mb->xpos = x;
  395. mb->ypos = y;
  396. mb->buf_offs = mb_offset;
  397. mb->b_mv_x =
  398. mb->b_mv_y = 0;
  399. if (get_bits1(&ctx->gb)) {
  400. if (ctx->frame_type == IVI4_FRAMETYPE_INTRA) {
  401. av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
  402. return AVERROR_INVALIDDATA;
  403. }
  404. mb->type = 1; /* empty macroblocks are always INTER */
  405. mb->cbp = 0; /* all blocks are empty */
  406. mb->q_delta = 0;
  407. if (!band->plane && !band->band_num && ctx->in_q) {
  408. mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  409. IVI_VLC_BITS, 1);
  410. mb->q_delta = IVI_TOSIGNED(mb->q_delta);
  411. }
  412. mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
  413. if (band->inherit_mv && ref_mb) {
  414. /* motion vector inheritance */
  415. if (mv_scale) {
  416. mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
  417. mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
  418. } else {
  419. mb->mv_x = ref_mb->mv_x;
  420. mb->mv_y = ref_mb->mv_y;
  421. }
  422. }
  423. } else {
  424. if (band->inherit_mv) {
  425. /* copy mb_type from corresponding reference mb */
  426. if (!ref_mb)
  427. return AVERROR_INVALIDDATA;
  428. mb->type = ref_mb->type;
  429. } else if (ctx->frame_type == IVI4_FRAMETYPE_INTRA ||
  430. ctx->frame_type == IVI4_FRAMETYPE_INTRA1) {
  431. mb->type = 0; /* mb_type is always INTRA for intra-frames */
  432. } else {
  433. mb->type = get_bits(&ctx->gb, mb_type_bits);
  434. }
  435. mb->cbp = get_bits(&ctx->gb, blks_per_mb);
  436. mb->q_delta = 0;
  437. if (band->inherit_qdelta) {
  438. if (ref_mb) mb->q_delta = ref_mb->q_delta;
  439. } else if (mb->cbp || (!band->plane && !band->band_num &&
  440. ctx->in_q)) {
  441. mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  442. IVI_VLC_BITS, 1);
  443. mb->q_delta = IVI_TOSIGNED(mb->q_delta);
  444. }
  445. if (!mb->type) {
  446. mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
  447. } else {
  448. if (band->inherit_mv) {
  449. if (ref_mb)
  450. /* motion vector inheritance */
  451. if (mv_scale) {
  452. mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
  453. mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
  454. } else {
  455. mb->mv_x = ref_mb->mv_x;
  456. mb->mv_y = ref_mb->mv_y;
  457. }
  458. } else {
  459. /* decode motion vector deltas */
  460. mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  461. IVI_VLC_BITS, 1);
  462. mv_y += IVI_TOSIGNED(mv_delta);
  463. mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  464. IVI_VLC_BITS, 1);
  465. mv_x += IVI_TOSIGNED(mv_delta);
  466. mb->mv_x = mv_x;
  467. mb->mv_y = mv_y;
  468. if (mb->type == 3) {
  469. mv_delta = get_vlc2(&ctx->gb,
  470. ctx->mb_vlc.tab->table,
  471. IVI_VLC_BITS, 1);
  472. mv_y += IVI_TOSIGNED(mv_delta);
  473. mv_delta = get_vlc2(&ctx->gb,
  474. ctx->mb_vlc.tab->table,
  475. IVI_VLC_BITS, 1);
  476. mv_x += IVI_TOSIGNED(mv_delta);
  477. mb->b_mv_x = -mv_x;
  478. mb->b_mv_y = -mv_y;
  479. }
  480. }
  481. if (mb->type == 2) {
  482. mb->b_mv_x = -mb->mv_x;
  483. mb->b_mv_y = -mb->mv_y;
  484. mb->mv_x = 0;
  485. mb->mv_y = 0;
  486. }
  487. }
  488. }
  489. mb++;
  490. if (ref_mb)
  491. ref_mb++;
  492. mb_offset += band->mb_size;
  493. }
  494. offs += row_offset;
  495. }
  496. align_get_bits(&ctx->gb);
  497. return 0;
  498. }
  499. /**
  500. * Rearrange decoding and reference buffers.
  501. *
  502. * @param[in,out] ctx pointer to the decoder context
  503. */
  504. static void switch_buffers(IVI45DecContext *ctx)
  505. {
  506. int is_prev_ref = 0, is_ref = 0;
  507. switch (ctx->prev_frame_type) {
  508. case IVI4_FRAMETYPE_INTRA:
  509. case IVI4_FRAMETYPE_INTRA1:
  510. case IVI4_FRAMETYPE_INTER:
  511. is_prev_ref = 1;
  512. break;
  513. }
  514. switch (ctx->frame_type) {
  515. case IVI4_FRAMETYPE_INTRA:
  516. case IVI4_FRAMETYPE_INTRA1:
  517. case IVI4_FRAMETYPE_INTER:
  518. is_ref = 1;
  519. break;
  520. }
  521. if (is_prev_ref && is_ref) {
  522. FFSWAP(int, ctx->dst_buf, ctx->ref_buf);
  523. } else if (is_prev_ref) {
  524. FFSWAP(int, ctx->ref_buf, ctx->b_ref_buf);
  525. FFSWAP(int, ctx->dst_buf, ctx->ref_buf);
  526. }
  527. }
  528. static int is_nonnull_frame(IVI45DecContext *ctx)
  529. {
  530. return ctx->frame_type < IVI4_FRAMETYPE_NULL_FIRST;
  531. }
  532. static av_cold int decode_init(AVCodecContext *avctx)
  533. {
  534. IVI45DecContext *ctx = avctx->priv_data;
  535. ff_ivi_init_static_vlc();
  536. /* copy rvmap tables in our context so we can apply changes to them */
  537. memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
  538. /* Force allocation of the internal buffers */
  539. /* during picture header decoding. */
  540. ctx->pic_conf.pic_width = 0;
  541. ctx->pic_conf.pic_height = 0;
  542. avctx->pix_fmt = AV_PIX_FMT_YUV410P;
  543. ctx->decode_pic_hdr = decode_pic_hdr;
  544. ctx->decode_band_hdr = decode_band_hdr;
  545. ctx->decode_mb_info = decode_mb_info;
  546. ctx->switch_buffers = switch_buffers;
  547. ctx->is_nonnull_frame = is_nonnull_frame;
  548. ctx->is_indeo4 = 1;
  549. ctx->dst_buf = 0;
  550. ctx->ref_buf = 1;
  551. ctx->b_ref_buf = 3; /* buffer 2 is used for scalability mode */
  552. ctx->p_frame = av_frame_alloc();
  553. if (!ctx->p_frame)
  554. return AVERROR(ENOMEM);
  555. return 0;
  556. }
  557. AVCodec ff_indeo4_decoder = {
  558. .name = "indeo4",
  559. .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 4"),
  560. .type = AVMEDIA_TYPE_VIDEO,
  561. .id = AV_CODEC_ID_INDEO4,
  562. .priv_data_size = sizeof(IVI45DecContext),
  563. .init = decode_init,
  564. .close = ff_ivi_decode_close,
  565. .decode = ff_ivi_decode_frame,
  566. .capabilities = AV_CODEC_CAP_DR1,
  567. };