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.

688 lines
23KB

  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. /**
  47. * Decode Indeo5 GOP (Group of pictures) header.
  48. * This header is present in key frames only.
  49. * It defines parameters for all frames in a GOP.
  50. *
  51. * @param[in,out] ctx ptr to the decoder context
  52. * @param[in] avctx ptr to the AVCodecContext
  53. * @return result code: 0 = OK, -1 = error
  54. */
  55. static int decode_gop_header(IVI45DecContext *ctx, AVCodecContext *avctx)
  56. {
  57. int result, i, p, tile_size, pic_size_indx, mb_size, blk_size, is_scalable;
  58. int quant_mat, blk_size_changed = 0;
  59. IVIBandDesc *band, *band1, *band2;
  60. IVIPicConfig pic_conf;
  61. ctx->gop_flags = get_bits(&ctx->gb, 8);
  62. ctx->gop_hdr_size = (ctx->gop_flags & 1) ? get_bits(&ctx->gb, 16) : 0;
  63. if (ctx->gop_flags & IVI5_IS_PROTECTED)
  64. ctx->lock_word = get_bits_long(&ctx->gb, 32);
  65. tile_size = (ctx->gop_flags & 0x40) ? 64 << get_bits(&ctx->gb, 2) : 0;
  66. if (tile_size > 256) {
  67. av_log(avctx, AV_LOG_ERROR, "Invalid tile size: %d\n", tile_size);
  68. return AVERROR_INVALIDDATA;
  69. }
  70. /* decode number of wavelet bands */
  71. /* num_levels * 3 + 1 */
  72. pic_conf.luma_bands = get_bits(&ctx->gb, 2) * 3 + 1;
  73. pic_conf.chroma_bands = get_bits1(&ctx->gb) * 3 + 1;
  74. is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
  75. if (is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
  76. av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
  77. pic_conf.luma_bands, pic_conf.chroma_bands);
  78. return AVERROR_INVALIDDATA;
  79. }
  80. pic_size_indx = get_bits(&ctx->gb, 4);
  81. if (pic_size_indx == IVI5_PIC_SIZE_ESC) {
  82. pic_conf.pic_height = get_bits(&ctx->gb, 13);
  83. pic_conf.pic_width = get_bits(&ctx->gb, 13);
  84. } else {
  85. pic_conf.pic_height = ivi5_common_pic_sizes[pic_size_indx * 2 + 1] << 2;
  86. pic_conf.pic_width = ivi5_common_pic_sizes[pic_size_indx * 2 ] << 2;
  87. }
  88. if (ctx->gop_flags & 2) {
  89. avpriv_report_missing_feature(avctx, "YV12 picture format");
  90. return AVERROR_PATCHWELCOME;
  91. }
  92. pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
  93. pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2;
  94. if (!tile_size) {
  95. pic_conf.tile_height = pic_conf.pic_height;
  96. pic_conf.tile_width = pic_conf.pic_width;
  97. } else {
  98. pic_conf.tile_height = pic_conf.tile_width = tile_size;
  99. }
  100. /* check if picture layout was changed and reallocate buffers */
  101. if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf) || ctx->gop_invalid) {
  102. result = ff_ivi_init_planes(ctx->planes, &pic_conf);
  103. if (result < 0) {
  104. av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
  105. return result;
  106. }
  107. ctx->pic_conf = pic_conf;
  108. ctx->is_scalable = is_scalable;
  109. blk_size_changed = 1; /* force reallocation of the internal structures */
  110. }
  111. for (p = 0; p <= 1; p++) {
  112. for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
  113. band = &ctx->planes[p].bands[i];
  114. band->is_halfpel = get_bits1(&ctx->gb);
  115. mb_size = get_bits1(&ctx->gb);
  116. blk_size = 8 >> get_bits1(&ctx->gb);
  117. mb_size = blk_size << !mb_size;
  118. if (p==0 && blk_size==4) {
  119. av_log(avctx, AV_LOG_ERROR, "4x4 luma blocks are unsupported!\n");
  120. return AVERROR_PATCHWELCOME;
  121. }
  122. blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size;
  123. if (blk_size_changed) {
  124. band->mb_size = mb_size;
  125. band->blk_size = blk_size;
  126. }
  127. if (get_bits1(&ctx->gb)) {
  128. avpriv_report_missing_feature(avctx, "Extended transform info");
  129. return AVERROR_PATCHWELCOME;
  130. }
  131. /* select transform function and scan pattern according to plane and band number */
  132. switch ((p << 2) + i) {
  133. case 0:
  134. band->inv_transform = ff_ivi_inverse_slant_8x8;
  135. band->dc_transform = ff_ivi_dc_slant_2d;
  136. band->scan = ff_zigzag_direct;
  137. band->transform_size = 8;
  138. break;
  139. case 1:
  140. band->inv_transform = ff_ivi_row_slant8;
  141. band->dc_transform = ff_ivi_dc_row_slant;
  142. band->scan = ff_ivi_vertical_scan_8x8;
  143. band->transform_size = 8;
  144. break;
  145. case 2:
  146. band->inv_transform = ff_ivi_col_slant8;
  147. band->dc_transform = ff_ivi_dc_col_slant;
  148. band->scan = ff_ivi_horizontal_scan_8x8;
  149. band->transform_size = 8;
  150. break;
  151. case 3:
  152. band->inv_transform = ff_ivi_put_pixels_8x8;
  153. band->dc_transform = ff_ivi_put_dc_pixel_8x8;
  154. band->scan = ff_ivi_horizontal_scan_8x8;
  155. band->transform_size = 8;
  156. break;
  157. case 4:
  158. band->inv_transform = ff_ivi_inverse_slant_4x4;
  159. band->dc_transform = ff_ivi_dc_slant_2d;
  160. band->scan = ff_ivi_direct_scan_4x4;
  161. band->transform_size = 4;
  162. break;
  163. }
  164. band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 ||
  165. band->inv_transform == ff_ivi_inverse_slant_4x4;
  166. if (band->transform_size != band->blk_size) {
  167. av_log(avctx, AV_LOG_ERROR, "transform and block size mismatch (%d != %d)\n", band->transform_size, band->blk_size);
  168. return AVERROR_INVALIDDATA;
  169. }
  170. /* select dequant matrix according to plane and band number */
  171. if (!p) {
  172. quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0;
  173. } else {
  174. quant_mat = 5;
  175. }
  176. if (band->blk_size == 8) {
  177. if(quant_mat >= 5){
  178. av_log(avctx, AV_LOG_ERROR, "quant_mat %d too large!\n", quant_mat);
  179. return -1;
  180. }
  181. band->intra_base = &ivi5_base_quant_8x8_intra[quant_mat][0];
  182. band->inter_base = &ivi5_base_quant_8x8_inter[quant_mat][0];
  183. band->intra_scale = &ivi5_scale_quant_8x8_intra[quant_mat][0];
  184. band->inter_scale = &ivi5_scale_quant_8x8_inter[quant_mat][0];
  185. } else {
  186. band->intra_base = ivi5_base_quant_4x4_intra;
  187. band->inter_base = ivi5_base_quant_4x4_inter;
  188. band->intra_scale = ivi5_scale_quant_4x4_intra;
  189. band->inter_scale = ivi5_scale_quant_4x4_inter;
  190. }
  191. if (get_bits(&ctx->gb, 2)) {
  192. av_log(avctx, AV_LOG_ERROR, "End marker missing!\n");
  193. return AVERROR_INVALIDDATA;
  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->intra_base = band1->intra_base;
  207. band2->inter_base = band1->inter_base;
  208. band2->intra_scale = band1->intra_scale;
  209. band2->inter_scale = band1->inter_scale;
  210. band2->scan = band1->scan;
  211. band2->inv_transform = band1->inv_transform;
  212. band2->dc_transform = band1->dc_transform;
  213. band2->is_2d_trans = band1->is_2d_trans;
  214. band2->transform_size= band1->transform_size;
  215. }
  216. /* reallocate internal structures if needed */
  217. if (blk_size_changed) {
  218. result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width,
  219. pic_conf.tile_height);
  220. if (result < 0) {
  221. av_log(avctx, AV_LOG_ERROR,
  222. "Couldn't reallocate internal structures!\n");
  223. return result;
  224. }
  225. }
  226. if (ctx->gop_flags & 8) {
  227. if (get_bits(&ctx->gb, 3)) {
  228. av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n");
  229. return AVERROR_INVALIDDATA;
  230. }
  231. if (get_bits1(&ctx->gb))
  232. skip_bits_long(&ctx->gb, 24); /* skip transparency fill color */
  233. }
  234. align_get_bits(&ctx->gb);
  235. skip_bits(&ctx->gb, 23); /* FIXME: unknown meaning */
  236. /* skip GOP extension if any */
  237. if (get_bits1(&ctx->gb)) {
  238. do {
  239. i = get_bits(&ctx->gb, 16);
  240. } while (i & 0x8000);
  241. }
  242. align_get_bits(&ctx->gb);
  243. return 0;
  244. }
  245. /**
  246. * Skip a header extension.
  247. *
  248. * @param[in,out] gb the GetBit context
  249. */
  250. static inline void skip_hdr_extension(GetBitContext *gb)
  251. {
  252. int i, len;
  253. do {
  254. len = get_bits(gb, 8);
  255. for (i = 0; i < len; i++) skip_bits(gb, 8);
  256. } while(len);
  257. }
  258. /**
  259. * Decode Indeo5 picture header.
  260. *
  261. * @param[in,out] ctx ptr to the decoder context
  262. * @param[in] avctx ptr to the AVCodecContext
  263. * @return result code: 0 = OK, -1 = error
  264. */
  265. static int decode_pic_hdr(IVI45DecContext *ctx, AVCodecContext *avctx)
  266. {
  267. int ret;
  268. if (get_bits(&ctx->gb, 5) != 0x1F) {
  269. av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
  270. return AVERROR_INVALIDDATA;
  271. }
  272. ctx->prev_frame_type = ctx->frame_type;
  273. ctx->frame_type = get_bits(&ctx->gb, 3);
  274. if (ctx->frame_type >= 5) {
  275. av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type);
  276. return AVERROR_INVALIDDATA;
  277. }
  278. ctx->frame_num = get_bits(&ctx->gb, 8);
  279. if (ctx->frame_type == FRAMETYPE_INTRA) {
  280. if ((ret = decode_gop_header(ctx, avctx)) < 0) {
  281. av_log(avctx, AV_LOG_ERROR, "Invalid GOP header, skipping frames.\n");
  282. ctx->gop_invalid = 1;
  283. return ret;
  284. }
  285. ctx->gop_invalid = 0;
  286. }
  287. if (ctx->frame_type == FRAMETYPE_INTER_SCAL && !ctx->is_scalable) {
  288. av_log(avctx, AV_LOG_ERROR, "Scalable inter frame in non scaleable stream\n");
  289. ctx->frame_type = FRAMETYPE_INTER;
  290. return AVERROR_INVALIDDATA;
  291. }
  292. if (ctx->frame_type != FRAMETYPE_NULL) {
  293. ctx->frame_flags = get_bits(&ctx->gb, 8);
  294. ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits_long(&ctx->gb, 24) : 0;
  295. ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0;
  296. /* skip unknown extension if any */
  297. if (ctx->frame_flags & 0x20)
  298. skip_hdr_extension(&ctx->gb); /* XXX: untested */
  299. /* decode macroblock huffman codebook */
  300. ret = ff_ivi_dec_huff_desc(&ctx->gb, ctx->frame_flags & 0x40,
  301. IVI_MB_HUFF, &ctx->mb_vlc, avctx);
  302. if (ret < 0)
  303. return ret;
  304. skip_bits(&ctx->gb, 3); /* FIXME: unknown meaning! */
  305. }
  306. align_get_bits(&ctx->gb);
  307. return 0;
  308. }
  309. /**
  310. * Decode Indeo5 band header.
  311. *
  312. * @param[in,out] ctx ptr to the decoder context
  313. * @param[in,out] band ptr to the band descriptor
  314. * @param[in] avctx ptr to the AVCodecContext
  315. * @return result code: 0 = OK, -1 = error
  316. */
  317. static int decode_band_hdr(IVI45DecContext *ctx, IVIBandDesc *band,
  318. AVCodecContext *avctx)
  319. {
  320. int i, ret;
  321. uint8_t band_flags;
  322. band_flags = get_bits(&ctx->gb, 8);
  323. if (band_flags & 1) {
  324. band->is_empty = 1;
  325. return 0;
  326. }
  327. band->data_size = (ctx->frame_flags & 0x80) ? get_bits_long(&ctx->gb, 24) : 0;
  328. band->inherit_mv = band_flags & 2;
  329. band->inherit_qdelta = band_flags & 8;
  330. band->qdelta_present = band_flags & 4;
  331. if (!band->qdelta_present) band->inherit_qdelta = 1;
  332. /* decode rvmap probability corrections if any */
  333. band->num_corr = 0; /* there are no corrections */
  334. if (band_flags & 0x10) {
  335. band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
  336. if (band->num_corr > 61) {
  337. av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
  338. band->num_corr);
  339. return AVERROR_INVALIDDATA;
  340. }
  341. /* read correction pairs */
  342. for (i = 0; i < band->num_corr * 2; i++)
  343. band->corr[i] = get_bits(&ctx->gb, 8);
  344. }
  345. /* select appropriate rvmap table for this band */
  346. band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8;
  347. /* decode block huffman codebook */
  348. ret = ff_ivi_dec_huff_desc(&ctx->gb, band_flags & 0x80, IVI_BLK_HUFF,
  349. &band->blk_vlc, avctx);
  350. if (ret < 0)
  351. return ret;
  352. band->checksum_present = get_bits1(&ctx->gb);
  353. if (band->checksum_present)
  354. band->checksum = get_bits(&ctx->gb, 16);
  355. band->glob_quant = get_bits(&ctx->gb, 5);
  356. /* skip unknown extension if any */
  357. if (band_flags & 0x20) { /* XXX: untested */
  358. align_get_bits(&ctx->gb);
  359. skip_hdr_extension(&ctx->gb);
  360. }
  361. align_get_bits(&ctx->gb);
  362. return 0;
  363. }
  364. /**
  365. * Decode info (block type, cbp, quant delta, motion vector)
  366. * for all macroblocks in the current tile.
  367. *
  368. * @param[in,out] ctx ptr to the decoder context
  369. * @param[in,out] band ptr to the band descriptor
  370. * @param[in,out] tile ptr to the tile descriptor
  371. * @param[in] avctx ptr to the AVCodecContext
  372. * @return result code: 0 = OK, -1 = error
  373. */
  374. static int decode_mb_info(IVI45DecContext *ctx, IVIBandDesc *band,
  375. IVITile *tile, AVCodecContext *avctx)
  376. {
  377. int x, y, mv_x, mv_y, mv_delta, offs, mb_offset,
  378. mv_scale, blks_per_mb, s;
  379. IVIMbInfo *mb, *ref_mb;
  380. int row_offset = band->mb_size * band->pitch;
  381. mb = tile->mbs;
  382. ref_mb = tile->ref_mbs;
  383. offs = tile->ypos * band->pitch + tile->xpos;
  384. if (!ref_mb &&
  385. ((band->qdelta_present && band->inherit_qdelta) || band->inherit_mv))
  386. return AVERROR_INVALIDDATA;
  387. if (tile->num_MBs != IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size)) {
  388. av_log(avctx, AV_LOG_ERROR, "Allocated tile size %d mismatches parameters %d\n",
  389. tile->num_MBs, IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size));
  390. return AVERROR_INVALIDDATA;
  391. }
  392. /* scale factor for motion vectors */
  393. mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
  394. mv_x = mv_y = 0;
  395. for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) {
  396. mb_offset = offs;
  397. for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) {
  398. mb->xpos = x;
  399. mb->ypos = y;
  400. mb->buf_offs = mb_offset;
  401. if (get_bits1(&ctx->gb)) {
  402. if (ctx->frame_type == FRAMETYPE_INTRA) {
  403. av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
  404. return AVERROR_INVALIDDATA;
  405. }
  406. mb->type = 1; /* empty macroblocks are always INTER */
  407. mb->cbp = 0; /* all blocks are empty */
  408. mb->q_delta = 0;
  409. if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) {
  410. mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  411. IVI_VLC_BITS, 1);
  412. mb->q_delta = IVI_TOSIGNED(mb->q_delta);
  413. }
  414. mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
  415. if (band->inherit_mv && ref_mb){
  416. /* motion vector inheritance */
  417. if (mv_scale) {
  418. mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
  419. mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
  420. } else {
  421. mb->mv_x = ref_mb->mv_x;
  422. mb->mv_y = ref_mb->mv_y;
  423. }
  424. }
  425. } else {
  426. if (band->inherit_mv && ref_mb) {
  427. mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */
  428. } else if (ctx->frame_type == FRAMETYPE_INTRA) {
  429. mb->type = 0; /* mb_type is always INTRA for intra-frames */
  430. } else {
  431. mb->type = get_bits1(&ctx->gb);
  432. }
  433. blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
  434. mb->cbp = get_bits(&ctx->gb, blks_per_mb);
  435. mb->q_delta = 0;
  436. if (band->qdelta_present) {
  437. if (band->inherit_qdelta) {
  438. if (ref_mb) mb->q_delta = ref_mb->q_delta;
  439. } else if (mb->cbp || (!band->plane && !band->band_num &&
  440. (ctx->frame_flags & 8))) {
  441. mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  442. IVI_VLC_BITS, 1);
  443. mb->q_delta = IVI_TOSIGNED(mb->q_delta);
  444. }
  445. }
  446. if (!mb->type) {
  447. mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
  448. } else {
  449. if (band->inherit_mv && ref_mb){
  450. /* motion vector inheritance */
  451. if (mv_scale) {
  452. mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
  453. mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
  454. } else {
  455. mb->mv_x = ref_mb->mv_x;
  456. mb->mv_y = ref_mb->mv_y;
  457. }
  458. } else {
  459. /* decode motion vector deltas */
  460. mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  461. IVI_VLC_BITS, 1);
  462. mv_y += IVI_TOSIGNED(mv_delta);
  463. mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
  464. IVI_VLC_BITS, 1);
  465. mv_x += IVI_TOSIGNED(mv_delta);
  466. mb->mv_x = mv_x;
  467. mb->mv_y = mv_y;
  468. }
  469. }
  470. }
  471. s= band->is_halfpel;
  472. if (mb->type)
  473. if ( x + (mb->mv_x >>s) + (y+ (mb->mv_y >>s))*band->pitch < 0 ||
  474. x + ((mb->mv_x+s)>>s) + band->mb_size - 1
  475. + (y+band->mb_size - 1 +((mb->mv_y+s)>>s))*band->pitch > band->bufsize - 1) {
  476. av_log(avctx, AV_LOG_ERROR, "motion vector %d %d outside reference\n", x*s + mb->mv_x, y*s + mb->mv_y);
  477. return AVERROR_INVALIDDATA;
  478. }
  479. mb++;
  480. if (ref_mb)
  481. ref_mb++;
  482. mb_offset += band->mb_size;
  483. }
  484. offs += row_offset;
  485. }
  486. align_get_bits(&ctx->gb);
  487. return 0;
  488. }
  489. /**
  490. * Switch buffers.
  491. *
  492. * @param[in,out] ctx ptr to the decoder context
  493. */
  494. static void switch_buffers(IVI45DecContext *ctx)
  495. {
  496. switch (ctx->prev_frame_type) {
  497. case FRAMETYPE_INTRA:
  498. case FRAMETYPE_INTER:
  499. ctx->buf_switch ^= 1;
  500. ctx->dst_buf = ctx->buf_switch;
  501. ctx->ref_buf = ctx->buf_switch ^ 1;
  502. break;
  503. case FRAMETYPE_INTER_SCAL:
  504. if (!ctx->inter_scal) {
  505. ctx->ref2_buf = 2;
  506. ctx->inter_scal = 1;
  507. }
  508. FFSWAP(int, ctx->dst_buf, ctx->ref2_buf);
  509. ctx->ref_buf = ctx->ref2_buf;
  510. break;
  511. case FRAMETYPE_INTER_NOREF:
  512. break;
  513. }
  514. switch (ctx->frame_type) {
  515. case FRAMETYPE_INTRA:
  516. ctx->buf_switch = 0;
  517. /* FALLTHROUGH */
  518. case FRAMETYPE_INTER:
  519. ctx->inter_scal = 0;
  520. ctx->dst_buf = ctx->buf_switch;
  521. ctx->ref_buf = ctx->buf_switch ^ 1;
  522. break;
  523. case FRAMETYPE_INTER_SCAL:
  524. case FRAMETYPE_INTER_NOREF:
  525. case FRAMETYPE_NULL:
  526. break;
  527. }
  528. }
  529. static int is_nonnull_frame(IVI45DecContext *ctx)
  530. {
  531. return ctx->frame_type != FRAMETYPE_NULL;
  532. }
  533. /**
  534. * Initialize Indeo5 decoder.
  535. */
  536. static av_cold int decode_init(AVCodecContext *avctx)
  537. {
  538. IVI45DecContext *ctx = avctx->priv_data;
  539. int result;
  540. ff_ivi_init_static_vlc();
  541. /* copy rvmap tables in our context so we can apply changes to them */
  542. memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
  543. /* set the initial picture layout according to the basic profile:
  544. there is only one band per plane (no scalability), only one tile (no local decoding)
  545. and picture format = YVU9 */
  546. ctx->pic_conf.pic_width = avctx->width;
  547. ctx->pic_conf.pic_height = avctx->height;
  548. ctx->pic_conf.chroma_width = (avctx->width + 3) >> 2;
  549. ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2;
  550. ctx->pic_conf.tile_width = avctx->width;
  551. ctx->pic_conf.tile_height = avctx->height;
  552. ctx->pic_conf.luma_bands = ctx->pic_conf.chroma_bands = 1;
  553. result = ff_ivi_init_planes(ctx->planes, &ctx->pic_conf);
  554. if (result) {
  555. av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n");
  556. return AVERROR_INVALIDDATA;
  557. }
  558. ctx->buf_switch = 0;
  559. ctx->inter_scal = 0;
  560. ctx->decode_pic_hdr = decode_pic_hdr;
  561. ctx->decode_band_hdr = decode_band_hdr;
  562. ctx->decode_mb_info = decode_mb_info;
  563. ctx->switch_buffers = switch_buffers;
  564. ctx->is_nonnull_frame = is_nonnull_frame;
  565. avctx->pix_fmt = AV_PIX_FMT_YUV410P;
  566. return 0;
  567. }
  568. AVCodec ff_indeo5_decoder = {
  569. .name = "indeo5",
  570. .type = AVMEDIA_TYPE_VIDEO,
  571. .id = AV_CODEC_ID_INDEO5,
  572. .priv_data_size = sizeof(IVI45DecContext),
  573. .init = decode_init,
  574. .close = ff_ivi_decode_close,
  575. .decode = ff_ivi_decode_frame,
  576. .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"),
  577. .capabilities = CODEC_CAP_DR1,
  578. };