1 /*
2 * Indeo Video Interactive v4 compatible decoder
3 * Copyright (c) 2009-2011 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 /**
23 * @file
24 * Indeo Video Interactive version 4 decoder
25 *
26 * Indeo 4 data is usually transported within .avi or .mov files.
27 * Known FOURCCs: 'IV41'
28 */
29
30 #define BITSTREAM_READER_LE
38
39 #define IVI4_PIC_SIZE_ESC 7
40
41
42 static const struct {
54 {
NULL,
NULL, 0 },
/* inverse DCT 8x8 */
55 {
NULL,
NULL, 0 },
/* inverse DCT 8x1 */
56 {
NULL,
NULL, 0 },
/* inverse DCT 1x8 */
59 {
NULL,
NULL, 0 },
/* no transform 4x4 */
64 {
NULL,
NULL, 0 },
/* inverse DCT 4x4 */
65 };
66
67 /**
68 * Decode subdivision of a plane.
69 * This is a simplified version that checks for two supported subdivisions:
70 * - 1 wavelet band per plane, size factor 1:1, code pattern: 3
71 * - 4 wavelet bands per plane, size factor 1:4, code pattern: 2,3,3,3,3
72 * Anything else is either unsupported or corrupt.
73 *
74 * @param[in,out] gb the GetBit context
75 * @return number of wavelet bands or 0 on error
76 */
78 {
80
82 case 3:
83 return 1;
84 case 2:
85 for (
i = 0;
i < 4;
i++)
87 return 0;
88 return 4;
89 default:
90 return 0;
91 }
92 }
93
95 {
96 return size_factor == 15 ? def_size : (size_factor + 1) << 5;
97 }
98
99 /**
100 * Decode Indeo 4 picture header.
101 *
102 * @param[in,out] ctx pointer to the decoder context
103 * @param[in] avctx pointer to the AVCodecContext
104 * @return result code: 0 = OK, negative number = error
105 */
107 {
108 int pic_size_indx,
i,
p;
110
114 }
115
116 ctx->prev_frame_type =
ctx->frame_type;
118 if (
ctx->frame_type == 7) {
121 }
122
124 ctx->has_b_frames = 1;
125
127
128 /* unknown bit: Mac decoder ignores this bit, XANIM returns error */
132 }
133
135
136 /* null frames don't contain anything else so we just return */
138 ff_dlog(avctx,
"Null frame encountered!\n");
139 return 0;
140 }
141
142 /* Check key lock status. If enabled - ignore lock word. */
143 /* Usually we have to prompt the user for the password, but */
144 /* we don't do that because Indeo 4 videos can be decoded anyway */
147 ff_dlog(avctx,
"Password-protected clip!\n");
148 }
149
154 } else {
157 }
158
159 /* Decode tile dimensions. */
161 if (
ctx->uses_tiling) {
164 } else {
167 }
168
169 /* Decode chroma subsampling. We support only 4:4 aka YVU9. */
173 }
176
177 /* decode subdivision of the planes */
182
187 }
188
191 av_log(avctx,
AV_LOG_ERROR,
"Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
194 }
195
196 /* check if picture layout was changed and reallocate buffers */
200 ctx->pic_conf.luma_bands = 0;
202 }
203
204 ctx->pic_conf = pic_conf;
205
206 /* set default macroblock/block dimensions */
207 for (
p = 0;
p <= 2;
p++) {
209 ctx->planes[
p].bands[
i].mb_size = !
p ? (!
ctx->is_scalable ? 16 : 8) : 4;
210 ctx->planes[
p].bands[
i].blk_size = !
p ? 8 : 4;
211 }
212 }
213
215 ctx->pic_conf.tile_height)) {
217 "Couldn't reallocate internal structures!\n");
219 }
220 }
221
223
224 /* skip decTimeEst field if present */
227
228 /* decode macroblock and block huffman codebooks */
232
234
237
239
240 /* TODO: ignore this parameter if unused */
242
244
245 /* skip picture header extension if any */
247 ff_dlog(avctx,
"Pic hdr extension encountered!\n");
251 }
252
255 }
256
258
259 return 0;
260 }
261
262
263 /**
264 * Decode Indeo 4 band header.
265 *
266 * @param[in,out] ctx pointer to the decoder context
267 * @param[in,out] band pointer to the band descriptor
268 * @param[in] avctx pointer to the AVCodecContext
269 * @return result code: 0 = OK, negative number = error
270 */
273 {
274 int plane, band_num, indx, transform_id, scan_indx;
276 int quant_mat;
278 memcpy(&temp_band, arg_band, sizeof(temp_band));
279
285 }
286
290 /* skip header size
291 * If header size is not given, header size is 4 bytes. */
294
300 }
302 ctx->uses_fullpel = 1;
303
307
309 if (indx == 3) {
312 }
315
318
320
327 }
328 if ((transform_id >= 7 && transform_id <= 9) ||
329 transform_id == 17) {
332 }
333
334 if (transform_id < 10 && band->blk_size < 8) {
337 }
338 if ((transform_id >= 0 && transform_id <= 2) || transform_id == 10)
340
344
345 if (transform_id < 10)
347 else
349
353 }
354
356 if (scan_indx == 15) {
359 }
360 if (scan_indx > 4 && scan_indx < 10) {
364 }
368 }
369
372
374 if (quant_mat == 31) {
377 }
380 quant_mat);
382 }
384 } else {
385 if (old_blk_size != band->
blk_size) {
387 "The band block size does not match the configuration "
388 "inherited\n");
390 }
391 }
396 }
400 }
404 }
405
406 /* decode block huffman codebook */
409 else
413
414 /* select appropriate rvmap table for this band */
416
417 /* decode rvmap probability corrections if any */
418 band->
num_corr = 0;
/* there is no corrections */
425 }
426
427 /* read correction pairs */
430 }
431 }
432
436 } else {
439 }
440
441 /* Indeo 4 doesn't use scale tables */
444
446
450 }
451
453 memcpy(arg_band, band, sizeof(*arg_band));
454
455 return 0;
456 }
457
458
459 /**
460 * Decode information (block type, cbp, quant delta, motion vector)
461 * for all macroblocks in the current tile.
462 *
463 * @param[in,out] ctx pointer to the decoder context
464 * @param[in,out] band pointer to the band descriptor
465 * @param[in,out] tile pointer to the tile descriptor
466 * @param[in] avctx pointer to the AVCodecContext
467 * @return result code: 0 = OK, negative number = error
468 */
471 {
472 int x, y, mv_x, mv_y, mv_delta, offs, mb_offset, blks_per_mb,
476
478 ref_mb =
tile->ref_mbs;
480
483
484 /* scale factor for motion vectors */
486 mv_x = mv_y = 0;
487
490 return -1;
491 }
492
493 for (y =
tile->ypos; y < tile->ypos +
tile->height; y += band->
mb_size) {
494 mb_offset = offs;
495
496 for (x =
tile->xpos; x < tile->xpos +
tile->width; x += band->
mb_size) {
499 mb->buf_offs = mb_offset;
502
506 }
507
512 }
513 mb->type = 1;
/* empty macroblocks are always INTER */
514 mb->cbp = 0;
/* all blocks are empty */
515
521 }
522
523 mb->mv_x =
mb->mv_y = 0;
/* no motion vector coded */
525 /* motion vector inheritance */
529 } else {
532 }
533 }
534 } else {
536 /* copy mb_type from corresponding reference mb */
537 if (!ref_mb) {
540 }
544 mb->type = 0;
/* mb_type is always INTRA for intra-frames */
545 } else {
547 }
548
550
553 if (ref_mb)
mb->q_delta = ref_mb->
q_delta;
559 }
560
562 mb->mv_x =
mb->mv_y = 0;
/* there is no motion vector in intra-macroblocks */
563 } else {
565 if (ref_mb)
566 /* motion vector inheritance */
570 } else {
573 }
574 } else {
575 /* decode motion vector deltas */
586 ctx->mb_vlc.tab->table,
590 ctx->mb_vlc.tab->table,
595 }
596 }
598 mb->b_mv_x = -
mb->mv_x;
599 mb->b_mv_y = -
mb->mv_y;
602 }
603 }
604 }
605
608 if ( x + (
mb->mv_x >>
s) + (y+ (
mb->mv_y >>
s))*band->
pitch < 0 ||
613 }
614
616 if (ref_mb)
617 ref_mb++;
619 }
620
621 offs += row_offset;
622 }
623
625
626 return 0;
627 }
628
629
630 /**
631 * Rearrange decoding and reference buffers.
632 *
633 * @param[in,out] ctx pointer to the decoder context
634 */
636 {
637 int is_prev_ref = 0, is_ref = 0;
638
639 switch (
ctx->prev_frame_type) {
643 is_prev_ref = 1;
644 break;
645 }
646
647 switch (
ctx->frame_type) {
651 is_ref = 1;
652 break;
653 }
654
655 if (is_prev_ref && is_ref) {
657 } else if (is_prev_ref) {
660 }
661 }
662
663
665 {
667 }
668
669
671 {
673
675
676 /* copy rvmap tables in our context so we can apply changes to them */
678
679 /* Force allocation of the internal buffers */
680 /* during picture header decoding. */
681 ctx->pic_conf.pic_width = 0;
682 ctx->pic_conf.pic_height = 0;
683
685
691
693 ctx->show_indeo4_info = 1;
694
697 ctx->b_ref_buf = 3;
/* buffer 2 is used for scalability mode */
701
702 return 0;
703 }
704
705
716 };