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.

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