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.

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