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.

698 lines
25KB

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