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.

888 lines
30KB

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