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.

659 lines
24KB

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