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.

838 lines
27KB

  1. /*
  2. * Indeo Video Interactive v5 compatible decoder
  3. * Copyright (c) 2009 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 5 decoder
  24. *
  25. * Indeo5 data is usually transported within .avi or .mov files.
  26. * Known FOURCCs: 'IV50'
  27. */
  28. #define BITSTREAM_READER_LE
  29. #include "avcodec.h"
  30. #include "get_bits.h"
  31. #include "dsputil.h"
  32. #include "ivi_dsp.h"
  33. #include "ivi_common.h"
  34. #include "indeo5data.h"
  35. /**
  36. * Indeo5 frame types.
  37. */
  38. enum {
  39. FRAMETYPE_INTRA = 0,
  40. FRAMETYPE_INTER = 1, ///< non-droppable P-frame
  41. FRAMETYPE_INTER_SCAL = 2, ///< droppable P-frame used in the scalability mode
  42. FRAMETYPE_INTER_NOREF = 3, ///< droppable P-frame
  43. FRAMETYPE_NULL = 4 ///< empty frame with no data
  44. };
  45. #define IVI5_PIC_SIZE_ESC 15
  46. #define IVI5_IS_PROTECTED 0x20
  47. typedef struct {
  48. GetBitContext gb;
  49. AVFrame frame;
  50. RVMapDesc rvmap_tabs[9]; ///< local corrected copy of the static rvmap tables
  51. IVIPlaneDesc planes[3]; ///< color planes
  52. const uint8_t *frame_data; ///< input frame data pointer
  53. int buf_switch; ///< used to switch between three buffers
  54. int inter_scal; ///< signals a sequence of scalable inter frames
  55. int dst_buf; ///< buffer index for the currently decoded frame
  56. int ref_buf; ///< inter frame reference buffer index
  57. int ref2_buf; ///< temporal storage for switching buffers
  58. uint32_t frame_size; ///< frame size in bytes
  59. int frame_type;
  60. int prev_frame_type; ///< frame type of the previous frame
  61. int frame_num;
  62. uint32_t pic_hdr_size; ///< picture header size in bytes
  63. uint8_t frame_flags;
  64. uint16_t checksum; ///< frame checksum
  65. IVIHuffTab mb_vlc; ///< vlc table for decoding macroblock data
  66. uint16_t gop_hdr_size;
  67. uint8_t gop_flags;
  68. int is_scalable;
  69. uint32_t lock_word;
  70. IVIPicConfig pic_conf;
  71. } IVI5DecContext;
  72. /**
  73. * Decode Indeo5 GOP (Group of pictures) header.
  74. * This header is present in key frames only.
  75. * It defines parameters for all frames in a GOP.
  76. *
  77. * @param[in,out] ctx ptr to the decoder context
  78. * @param[in] avctx ptr to the AVCodecContext
  79. * @return result code: 0 = OK, -1 = error
  80. */
  81. static int decode_gop_header(IVI5DecContext *ctx, AVCodecContext *avctx)
  82. {
  83. int result, i, p, tile_size, pic_size_indx, mb_size, blk_size, is_scalable;
  84. int quant_mat, blk_size_changed = 0;
  85. IVIBandDesc *band, *band1, *band2;
  86. IVIPicConfig pic_conf;
  87. ctx->gop_flags = get_bits(&ctx->gb, 8);
  88. ctx->gop_hdr_size = (ctx->gop_flags & 1) ? get_bits(&ctx->gb, 16) : 0;
  89. if (ctx->gop_flags & IVI5_IS_PROTECTED)
  90. ctx->lock_word = get_bits_long(&ctx->gb, 32);
  91. tile_size = (ctx->gop_flags & 0x40) ? 64 << get_bits(&ctx->gb, 2) : 0;
  92. if (tile_size > 256) {
  93. av_log(avctx, AV_LOG_ERROR, "Invalid tile size: %d\n", tile_size);
  94. return -1;
  95. }
  96. /* decode number of wavelet bands */
  97. /* num_levels * 3 + 1 */
  98. pic_conf.luma_bands = get_bits(&ctx->gb, 2) * 3 + 1;
  99. pic_conf.chroma_bands = get_bits1(&ctx->gb) * 3 + 1;
  100. is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
  101. if (is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
  102. av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
  103. pic_conf.luma_bands, pic_conf.chroma_bands);
  104. return -1;
  105. }
  106. pic_size_indx = get_bits(&ctx->gb, 4);
  107. if (pic_size_indx == IVI5_PIC_SIZE_ESC) {
  108. pic_conf.pic_height = get_bits(&ctx->gb, 13);
  109. pic_conf.pic_width = get_bits(&ctx->gb, 13);
  110. } else {
  111. pic_conf.pic_height = ivi5_common_pic_sizes[pic_size_indx * 2 + 1] << 2;
  112. pic_conf.pic_width = ivi5_common_pic_sizes[pic_size_indx * 2 ] << 2;
  113. }
  114. if (ctx->gop_flags & 2) {
  115. av_log(avctx, AV_LOG_ERROR, "YV12 picture format not supported!\n");
  116. return -1;
  117. }
  118. pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
  119. pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2;
  120. if (!tile_size) {
  121. pic_conf.tile_height = pic_conf.pic_height;
  122. pic_conf.tile_width = pic_conf.pic_width;
  123. } else {
  124. pic_conf.tile_height = pic_conf.tile_width = tile_size;
  125. }
  126. /* check if picture layout was changed and reallocate buffers */
  127. if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) {
  128. result = ff_ivi_init_planes(ctx->planes, &pic_conf);
  129. if (result) {
  130. av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
  131. return -1;
  132. }
  133. ctx->pic_conf = pic_conf;
  134. ctx->is_scalable = is_scalable;
  135. blk_size_changed = 1; /* force reallocation of the internal structures */
  136. }
  137. for (p = 0; p <= 1; p++) {
  138. for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
  139. band = &ctx->planes[p].bands[i];
  140. band->is_halfpel = get_bits1(&ctx->gb);
  141. mb_size = get_bits1(&ctx->gb);
  142. blk_size = 8 >> get_bits1(&ctx->gb);
  143. mb_size = blk_size << !mb_size;
  144. blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size;
  145. if (blk_size_changed) {
  146. band->mb_size = mb_size;
  147. band->blk_size = blk_size;
  148. }
  149. if (get_bits1(&ctx->gb)) {
  150. av_log(avctx, AV_LOG_ERROR, "Extended transform info encountered!\n");
  151. return -1;
  152. }
  153. /* select transform function and scan pattern according to plane and band number */
  154. switch ((p << 2) + i) {
  155. case 0:
  156. band->inv_transform = ff_ivi_inverse_slant_8x8;
  157. band->dc_transform = ff_ivi_dc_slant_2d;
  158. band->scan = ff_zigzag_direct;
  159. break;
  160. case 1:
  161. band->inv_transform = ff_ivi_row_slant8;
  162. band->dc_transform = ff_ivi_dc_row_slant;
  163. band->scan = ff_ivi_vertical_scan_8x8;
  164. break;
  165. case 2:
  166. band->inv_transform = ff_ivi_col_slant8;
  167. band->dc_transform = ff_ivi_dc_col_slant;
  168. band->scan = ff_ivi_horizontal_scan_8x8;
  169. break;
  170. case 3:
  171. band->inv_transform = ff_ivi_put_pixels_8x8;
  172. band->dc_transform = ff_ivi_put_dc_pixel_8x8;
  173. band->scan = ff_ivi_horizontal_scan_8x8;
  174. break;
  175. case 4:
  176. band->inv_transform = ff_ivi_inverse_slant_4x4;
  177. band->dc_transform = ff_ivi_dc_slant_2d;
  178. band->scan = ff_ivi_direct_scan_4x4;
  179. break;
  180. }
  181. band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 ||
  182. band->inv_transform == ff_ivi_inverse_slant_4x4;
  183. /* select dequant matrix according to plane and band number */
  184. if (!p) {
  185. quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0;
  186. } else {
  187. quant_mat = 5;
  188. }
  189. if (band->blk_size == 8) {
  190. band->intra_base = &ivi5_base_quant_8x8_intra[quant_mat][0];
  191. band->inter_base = &ivi5_base_quant_8x8_inter[quant_mat][0];
  192. band->intra_scale = &ivi5_scale_quant_8x8_intra[quant_mat][0];
  193. band->inter_scale = &ivi5_scale_quant_8x8_inter[quant_mat][0];
  194. } else {
  195. band->intra_base = ivi5_base_quant_4x4_intra;
  196. band->inter_base = ivi5_base_quant_4x4_inter;
  197. band->intra_scale = ivi5_scale_quant_4x4_intra;
  198. band->inter_scale = ivi5_scale_quant_4x4_inter;
  199. }
  200. if (get_bits(&ctx->gb, 2)) {
  201. av_log(avctx, AV_LOG_ERROR, "End marker missing!\n");
  202. return -1;
  203. }
  204. }
  205. }
  206. /* copy chroma parameters into the 2nd chroma plane */
  207. for (i = 0; i < pic_conf.chroma_bands; i++) {
  208. band1 = &ctx->planes[1].bands[i];
  209. band2 = &ctx->planes[2].bands[i];
  210. band2->width = band1->width;
  211. band2->height = band1->height;
  212. band2->mb_size = band1->mb_size;
  213. band2->blk_size = band1->blk_size;
  214. band2->is_halfpel = band1->is_halfpel;
  215. band2->intra_base = band1->intra_base;
  216. band2->inter_base = band1->inter_base;
  217. band2->intra_scale = band1->intra_scale;
  218. band2->inter_scale = band1->inter_scale;
  219. band2->scan = band1->scan;
  220. band2->inv_transform = band1->inv_transform;
  221. band2->dc_transform = band1->dc_transform;
  222. band2->is_2d_trans = band1->is_2d_trans;
  223. }
  224. /* reallocate internal structures if needed */
  225. if (blk_size_changed) {
  226. result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width,
  227. pic_conf.tile_height);
  228. if (result) {
  229. av_log(avctx, AV_LOG_ERROR,
  230. "Couldn't reallocate internal structures!\n");
  231. return -1;
  232. }
  233. }
  234. if (ctx->gop_flags & 8) {
  235. if (get_bits(&ctx->gb, 3)) {
  236. av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n");
  237. return -1;
  238. }
  239. if (get_bits1(&ctx->gb))
  240. skip_bits_long(&ctx->gb, 24); /* skip transparency fill color */
  241. }
  242. align_get_bits(&ctx->gb);
  243. skip_bits(&ctx->gb, 23); /* FIXME: unknown meaning */
  244. /* skip GOP extension if any */
  245. if (get_bits1(&ctx->gb)) {
  246. do {
  247. i = get_bits(&ctx->gb, 16);
  248. } while (i & 0x8000);
  249. }
  250. align_get_bits(&ctx->gb);
  251. return 0;
  252. }
  253. /**
  254. * Skip a header extension.
  255. *
  256. * @param[in,out] gb the GetBit context
  257. */
  258. static inline void skip_hdr_extension(GetBitContext *gb)
  259. {
  260. int i, len;
  261. do {
  262. len = get_bits(gb, 8);
  263. for (i = 0; i < len; i++) skip_bits(gb, 8);
  264. } while(len);
  265. }
  266. /**
  267. * Decode Indeo5 picture header.
  268. *
  269. * @param[in,out] ctx ptr to the decoder context
  270. * @param[in] avctx ptr to the AVCodecContext
  271. * @return result code: 0 = OK, -1 = error
  272. */
  273. static int decode_pic_hdr(IVI5DecContext *ctx, AVCodecContext *avctx)
  274. {
  275. if (get_bits(&ctx->gb, 5) != 0x1F) {
  276. av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
  277. return -1;
  278. }
  279. ctx->prev_frame_type = ctx->frame_type;
  280. ctx->frame_type = get_bits(&ctx->gb, 3);
  281. if (ctx->frame_type >= 5) {
  282. av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type);
  283. return -1;
  284. }
  285. ctx->frame_num = get_bits(&ctx->gb, 8);
  286. if (ctx->frame_type == FRAMETYPE_INTRA) {
  287. if (decode_gop_header(ctx, avctx))
  288. return -1;
  289. }
  290. if (ctx->frame_type != FRAMETYPE_NULL) {
  291. ctx->frame_flags = get_bits(&ctx->gb, 8);
  292. ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits_long(&ctx->gb, 24) : 0;
  293. ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0;
  294. /* skip unknown extension if any */
  295. if (ctx->frame_flags & 0x20)
  296. skip_hdr_extension(&ctx->gb); /* XXX: untested */
  297. /* decode macroblock huffman codebook */
  298. if (ff_ivi_dec_huff_desc(&ctx->gb, ctx->frame_flags & 0x40, IVI_MB_HUFF, &ctx->mb_vlc, avctx))
  299. return -1;
  300. skip_bits(&ctx->gb, 3); /* FIXME: unknown meaning! */
  301. }
  302. align_get_bits(&ctx->gb);
  303. return 0;
  304. }
  305. /**
  306. * Decode Indeo5 band header.
  307. *
  308. * @param[in,out] ctx ptr to the decoder context
  309. * @param[in,out] band ptr to the band descriptor
  310. * @param[in] avctx ptr to the AVCodecContext
  311. * @return result code: 0 = OK, -1 = error
  312. */
  313. static int decode_band_hdr(IVI5DecContext *ctx, IVIBandDesc *band,
  314. AVCodecContext *avctx)
  315. {
  316. int i;
  317. uint8_t band_flags;
  318. band_flags = get_bits(&ctx->gb, 8);
  319. if (band_flags & 1) {
  320. band->is_empty = 1;
  321. return 0;
  322. }
  323. band->data_size = (ctx->frame_flags & 0x80) ? get_bits_long(&ctx->gb, 24) : 0;
  324. band->inherit_mv = band_flags & 2;
  325. band->inherit_qdelta = band_flags & 8;
  326. band->qdelta_present = band_flags & 4;
  327. if (!band->qdelta_present) band->inherit_qdelta = 1;
  328. /* decode rvmap probability corrections if any */
  329. band->num_corr = 0; /* there are no corrections */
  330. if (band_flags & 0x10) {
  331. band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
  332. if (band->num_corr > 61) {
  333. av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
  334. band->num_corr);
  335. return -1;
  336. }
  337. /* read correction pairs */
  338. for (i = 0; i < band->num_corr * 2; i++)
  339. band->corr[i] = get_bits(&ctx->gb, 8);
  340. }
  341. /* select appropriate rvmap table for this band */
  342. band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8;
  343. /* decode block huffman codebook */
  344. if (ff_ivi_dec_huff_desc(&ctx->gb, band_flags & 0x80, IVI_BLK_HUFF, &band->blk_vlc, avctx))
  345. return -1;
  346. band->checksum_present = get_bits1(&ctx->gb);
  347. if (band->checksum_present)
  348. band->checksum = get_bits(&ctx->gb, 16);
  349. band->glob_quant = get_bits(&ctx->gb, 5);
  350. /* skip unknown extension if any */
  351. if (band_flags & 0x20) { /* XXX: untested */
  352. align_get_bits(&ctx->gb);
  353. skip_hdr_extension(&ctx->gb);
  354. }
  355. align_get_bits(&ctx->gb);
  356. return 0;
  357. }
  358. /**
  359. * Decode info (block type, cbp, quant delta, motion vector)
  360. * for all macroblocks in the current tile.
  361. *
  362. * @param[in,out] ctx ptr to the decoder context
  363. * @param[in,out] band ptr to the band descriptor
  364. * @param[in,out] tile ptr to the tile descriptor
  365. * @param[in] avctx ptr to the AVCodecContext
  366. * @return result code: 0 = OK, -1 = error
  367. */
  368. static int decode_mb_info(IVI5DecContext *ctx, IVIBandDesc *band,
  369. IVITile *tile, AVCodecContext *avctx)
  370. {
  371. int x, y, mv_x, mv_y, mv_delta, offs, mb_offset,
  372. mv_scale, blks_per_mb;
  373. IVIMbInfo *mb, *ref_mb;
  374. int row_offset = band->mb_size * band->pitch;
  375. mb = tile->mbs;
  376. ref_mb = tile->ref_mbs;
  377. offs = tile->ypos * band->pitch + tile->xpos;
  378. if (!ref_mb &&
  379. ((band->qdelta_present && band->inherit_qdelta) || band->inherit_mv))
  380. return AVERROR_INVALIDDATA;
  381. /* scale factor for motion vectors */
  382. mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
  383. mv_x = mv_y = 0;
  384. for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) {
  385. mb_offset = offs;
  386. for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) {
  387. mb->xpos = x;
  388. mb->ypos = y;
  389. mb->buf_offs = mb_offset;
  390. if (get_bits1(&ctx->gb)) {
  391. if (ctx->frame_type == FRAMETYPE_INTRA) {
  392. av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
  393. return -1;
  394. }
  395. mb->type = 1; /* empty macroblocks are always INTER */
  396. mb->cbp = 0; /* all blocks are empty */
  397. mb->q_delta = 0;
  398. if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) {
  399. mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  400. IVI_VLC_BITS, 1);
  401. mb->q_delta = IVI_TOSIGNED(mb->q_delta);
  402. }
  403. mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
  404. if (band->inherit_mv && ref_mb){
  405. /* motion vector inheritance */
  406. if (mv_scale) {
  407. mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
  408. mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
  409. } else {
  410. mb->mv_x = ref_mb->mv_x;
  411. mb->mv_y = ref_mb->mv_y;
  412. }
  413. }
  414. } else {
  415. if (band->inherit_mv && ref_mb) {
  416. mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */
  417. } else if (ctx->frame_type == FRAMETYPE_INTRA) {
  418. mb->type = 0; /* mb_type is always INTRA for intra-frames */
  419. } else {
  420. mb->type = get_bits1(&ctx->gb);
  421. }
  422. blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
  423. mb->cbp = get_bits(&ctx->gb, blks_per_mb);
  424. mb->q_delta = 0;
  425. if (band->qdelta_present) {
  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->frame_flags & 8))) {
  430. mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  431. IVI_VLC_BITS, 1);
  432. mb->q_delta = IVI_TOSIGNED(mb->q_delta);
  433. }
  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 && 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 = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  450. IVI_VLC_BITS, 1);
  451. mv_y += IVI_TOSIGNED(mv_delta);
  452. mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  453. IVI_VLC_BITS, 1);
  454. mv_x += IVI_TOSIGNED(mv_delta);
  455. mb->mv_x = mv_x;
  456. mb->mv_y = mv_y;
  457. }
  458. }
  459. }
  460. mb++;
  461. if (ref_mb)
  462. ref_mb++;
  463. mb_offset += band->mb_size;
  464. }
  465. offs += row_offset;
  466. }
  467. align_get_bits(&ctx->gb);
  468. return 0;
  469. }
  470. /**
  471. * Decode an Indeo5 band.
  472. *
  473. * @param[in,out] ctx ptr to the decoder context
  474. * @param[in,out] band ptr to the band descriptor
  475. * @param[in] avctx ptr to the AVCodecContext
  476. * @return result code: 0 = OK, -1 = error
  477. */
  478. static int decode_band(IVI5DecContext *ctx, int plane_num,
  479. IVIBandDesc *band, AVCodecContext *avctx)
  480. {
  481. int result, i, t, idx1, idx2, pos;
  482. IVITile *tile;
  483. band->buf = band->bufs[ctx->dst_buf];
  484. band->ref_buf = band->bufs[ctx->ref_buf];
  485. band->data_ptr = ctx->frame_data + (get_bits_count(&ctx->gb) >> 3);
  486. result = decode_band_hdr(ctx, band, avctx);
  487. if (result) {
  488. av_log(avctx, AV_LOG_ERROR, "Error while decoding band header: %d\n",
  489. result);
  490. return -1;
  491. }
  492. if (band->is_empty) {
  493. av_log(avctx, AV_LOG_ERROR, "Empty band encountered!\n");
  494. return -1;
  495. }
  496. band->rv_map = &ctx->rvmap_tabs[band->rvmap_sel];
  497. /* apply corrections to the selected rvmap table if present */
  498. for (i = 0; i < band->num_corr; i++) {
  499. idx1 = band->corr[i*2];
  500. idx2 = band->corr[i*2+1];
  501. FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);
  502. FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);
  503. }
  504. pos = get_bits_count(&ctx->gb);
  505. for (t = 0; t < band->num_tiles; t++) {
  506. tile = &band->tiles[t];
  507. tile->is_empty = get_bits1(&ctx->gb);
  508. if (tile->is_empty) {
  509. ff_ivi_process_empty_tile(avctx, band, tile,
  510. (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3));
  511. } else {
  512. tile->data_size = ff_ivi_dec_tile_data_size(&ctx->gb);
  513. result = decode_mb_info(ctx, band, tile, avctx);
  514. if (result < 0)
  515. break;
  516. result = ff_ivi_decode_blocks(&ctx->gb, band, tile);
  517. if (result < 0 || (get_bits_count(&ctx->gb) - pos) >> 3 != tile->data_size) {
  518. av_log(avctx, AV_LOG_ERROR, "Corrupted tile data encountered!\n");
  519. break;
  520. }
  521. pos += tile->data_size << 3; // skip to next tile
  522. }
  523. }
  524. /* restore the selected rvmap table by applying its corrections in reverse order */
  525. for (i = band->num_corr-1; i >= 0; i--) {
  526. idx1 = band->corr[i*2];
  527. idx2 = band->corr[i*2+1];
  528. FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);
  529. FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);
  530. }
  531. #ifdef DEBUG
  532. if (band->checksum_present) {
  533. uint16_t chksum = ivi_calc_band_checksum(band);
  534. if (chksum != band->checksum) {
  535. av_log(avctx, AV_LOG_ERROR,
  536. "Band checksum mismatch! Plane %d, band %d, received: %x, calculated: %x\n",
  537. band->plane, band->band_num, band->checksum, chksum);
  538. }
  539. }
  540. #endif
  541. align_get_bits(&ctx->gb);
  542. return result;
  543. }
  544. /**
  545. * Switch buffers.
  546. *
  547. * @param[in,out] ctx ptr to the decoder context
  548. */
  549. static void switch_buffers(IVI5DecContext *ctx)
  550. {
  551. switch (ctx->prev_frame_type) {
  552. case FRAMETYPE_INTRA:
  553. case FRAMETYPE_INTER:
  554. ctx->buf_switch ^= 1;
  555. ctx->dst_buf = ctx->buf_switch;
  556. ctx->ref_buf = ctx->buf_switch ^ 1;
  557. break;
  558. case FRAMETYPE_INTER_SCAL:
  559. if (!ctx->inter_scal) {
  560. ctx->ref2_buf = 2;
  561. ctx->inter_scal = 1;
  562. }
  563. FFSWAP(int, ctx->dst_buf, ctx->ref2_buf);
  564. ctx->ref_buf = ctx->ref2_buf;
  565. break;
  566. case FRAMETYPE_INTER_NOREF:
  567. break;
  568. }
  569. switch (ctx->frame_type) {
  570. case FRAMETYPE_INTRA:
  571. ctx->buf_switch = 0;
  572. /* FALLTHROUGH */
  573. case FRAMETYPE_INTER:
  574. ctx->inter_scal = 0;
  575. ctx->dst_buf = ctx->buf_switch;
  576. ctx->ref_buf = ctx->buf_switch ^ 1;
  577. break;
  578. case FRAMETYPE_INTER_SCAL:
  579. case FRAMETYPE_INTER_NOREF:
  580. case FRAMETYPE_NULL:
  581. break;
  582. }
  583. }
  584. /**
  585. * Initialize Indeo5 decoder.
  586. */
  587. static av_cold int decode_init(AVCodecContext *avctx)
  588. {
  589. IVI5DecContext *ctx = avctx->priv_data;
  590. int result;
  591. ff_ivi_init_static_vlc();
  592. /* copy rvmap tables in our context so we can apply changes to them */
  593. memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
  594. /* set the initial picture layout according to the basic profile:
  595. there is only one band per plane (no scalability), only one tile (no local decoding)
  596. and picture format = YVU9 */
  597. ctx->pic_conf.pic_width = avctx->width;
  598. ctx->pic_conf.pic_height = avctx->height;
  599. ctx->pic_conf.chroma_width = (avctx->width + 3) >> 2;
  600. ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2;
  601. ctx->pic_conf.tile_width = avctx->width;
  602. ctx->pic_conf.tile_height = avctx->height;
  603. ctx->pic_conf.luma_bands = ctx->pic_conf.chroma_bands = 1;
  604. avcodec_get_frame_defaults(&ctx->frame);
  605. result = ff_ivi_init_planes(ctx->planes, &ctx->pic_conf);
  606. if (result) {
  607. av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n");
  608. return -1;
  609. }
  610. ctx->buf_switch = 0;
  611. ctx->inter_scal = 0;
  612. avctx->pix_fmt = PIX_FMT_YUV410P;
  613. return 0;
  614. }
  615. /**
  616. * main decoder function
  617. */
  618. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  619. AVPacket *avpkt)
  620. {
  621. IVI5DecContext *ctx = avctx->priv_data;
  622. const uint8_t *buf = avpkt->data;
  623. int buf_size = avpkt->size;
  624. int result, p, b;
  625. init_get_bits(&ctx->gb, buf, buf_size * 8);
  626. ctx->frame_data = buf;
  627. ctx->frame_size = buf_size;
  628. result = decode_pic_hdr(ctx, avctx);
  629. if (result) {
  630. av_log(avctx, AV_LOG_ERROR,
  631. "Error while decoding picture header: %d\n", result);
  632. return -1;
  633. }
  634. if (ctx->gop_flags & IVI5_IS_PROTECTED) {
  635. av_log(avctx, AV_LOG_ERROR, "Password-protected clip!\n");
  636. return -1;
  637. }
  638. switch_buffers(ctx);
  639. //{ START_TIMER;
  640. if (ctx->frame_type != FRAMETYPE_NULL) {
  641. for (p = 0; p < 3; p++) {
  642. for (b = 0; b < ctx->planes[p].num_bands; b++) {
  643. result = decode_band(ctx, p, &ctx->planes[p].bands[b], avctx);
  644. if (result) {
  645. av_log(avctx, AV_LOG_ERROR,
  646. "Error while decoding band: %d, plane: %d\n", b, p);
  647. return -1;
  648. }
  649. }
  650. }
  651. }
  652. //STOP_TIMER("decode_planes"); }
  653. if (ctx->frame.data[0])
  654. avctx->release_buffer(avctx, &ctx->frame);
  655. ctx->frame.reference = 0;
  656. if (avctx->get_buffer(avctx, &ctx->frame) < 0) {
  657. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  658. return -1;
  659. }
  660. if (ctx->is_scalable) {
  661. ff_ivi_recompose53 (&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0], 4);
  662. } else {
  663. ff_ivi_output_plane(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0]);
  664. }
  665. ff_ivi_output_plane(&ctx->planes[2], ctx->frame.data[1], ctx->frame.linesize[1]);
  666. ff_ivi_output_plane(&ctx->planes[1], ctx->frame.data[2], ctx->frame.linesize[2]);
  667. *data_size = sizeof(AVFrame);
  668. *(AVFrame*)data = ctx->frame;
  669. return buf_size;
  670. }
  671. /**
  672. * Close Indeo5 decoder and clean up its context.
  673. */
  674. static av_cold int decode_close(AVCodecContext *avctx)
  675. {
  676. IVI5DecContext *ctx = avctx->priv_data;
  677. ff_ivi_free_buffers(&ctx->planes[0]);
  678. if (ctx->mb_vlc.cust_tab.table)
  679. free_vlc(&ctx->mb_vlc.cust_tab);
  680. if (ctx->frame.data[0])
  681. avctx->release_buffer(avctx, &ctx->frame);
  682. return 0;
  683. }
  684. AVCodec ff_indeo5_decoder = {
  685. .name = "indeo5",
  686. .type = AVMEDIA_TYPE_VIDEO,
  687. .id = CODEC_ID_INDEO5,
  688. .priv_data_size = sizeof(IVI5DecContext),
  689. .init = decode_init,
  690. .close = decode_close,
  691. .decode = decode_frame,
  692. .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"),
  693. };