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.

872 lines
29KB

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