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