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.

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