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.

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