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.

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