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.

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