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.

835 lines
27KB

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