1 /*
2 * Indeo Video Interactive v4 compatible decoder
3 * Copyright (c) 2009-2011 Maxim Poliakovski
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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 * Libav 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 Libav; 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
37
38 /**
39 * Indeo 4 frame types.
40 */
41 enum {
49 };
50
51 #define IVI4_PIC_SIZE_ESC 7
52
53
54 static const struct {
60 {
NULL,
NULL, 0 },
/* inverse Haar 8x1 */
61 {
NULL,
NULL, 0 },
/* inverse Haar 1x8 */
66 {
NULL,
NULL, 0 },
/* inverse DCT 8x8 */
67 {
NULL,
NULL, 0 },
/* inverse DCT 8x1 */
68 {
NULL,
NULL, 0 },
/* inverse DCT 1x8 */
69 {
NULL,
NULL, 0 },
/* inverse Haar 4x4 */
71 {
NULL,
NULL, 0 },
/* no transform 4x4 */
72 {
NULL,
NULL, 0 },
/* inverse Haar 1x4 */
73 {
NULL,
NULL, 0 },
/* inverse Haar 4x1 */
74 {
NULL,
NULL, 0 },
/* inverse slant 1x4 */
75 {
NULL,
NULL, 0 },
/* inverse slant 4x1 */
76 {
NULL,
NULL, 0 },
/* inverse DCT 4x4 */
77 };
78
79 /**
80 * Decode subdivision of a plane.
81 * This is a simplified version that checks for two supported subdivisions:
82 * - 1 wavelet band per plane, size factor 1:1, code pattern: 3
83 * - 4 wavelet bands per plane, size factor 1:4, code pattern: 2,3,3,3,3
84 * Anything else is either unsupported or corrupt.
85 *
86 * @param[in,out] gb the GetBit context
87 * @return number of wavelet bands or 0 on error
88 */
90 {
91 int i;
92
94 case 3:
95 return 1;
96 case 2:
97 for (i = 0; i < 4; i++)
99 return 0;
100 return 4;
101 default:
102 return 0;
103 }
104 }
105
107 {
108 return size_factor == 15 ? def_size : (size_factor + 1) << 5;
109 }
110
111 /**
112 * Decode Indeo 4 picture header.
113 *
114 * @param[in,out] ctx pointer to the decoder context
115 * @param[in] avctx pointer to the AVCodecContext
116 * @return result code: 0 = OK, negative number = error
117 */
119 {
120 int pic_size_indx, i, p;
122
126 }
127
133 }
134
135 #if IVI4_STREAM_ANALYSER
137 ctx->has_b_frames = 1;
138 #endif
139
141 #if IVI4_STREAM_ANALYSER
143 ctx->has_transp = 1;
144 }
145 #endif
146
147 /* unknown bit: Mac decoder ignores this bit, XANIM returns error */
151 }
152
154
155 /* null frames don't contain anything else so we just return */
157 av_dlog(avctx,
"Null frame encountered!\n");
158 return 0;
159 }
160
161 /* Check key lock status. If enabled - ignore lock word. */
162 /* Usually we have to prompt the user for the password, but */
163 /* we don't do that because Indeo 4 videos can be decoded anyway */
166 av_dlog(avctx,
"Password-protected clip!\n");
167 }
168
173 } else {
176 }
177
178 /* Decode tile dimensions. */
182 #if IVI4_STREAM_ANALYSER
183 ctx->uses_tiling = 1;
184 #endif
185 } else {
188 }
189
190 /* Decode chroma subsampling. We support only 4:4 aka YVU9. */
194 }
197
198 /* decode subdivision of the planes */
205 av_log(avctx,
AV_LOG_ERROR,
"Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
208 }
209
210 /* check if picture layout was changed and reallocate buffers */
215 }
216
218
219 /* set default macroblock/block dimensions */
220 for (p = 0; p <= 2; p++) {
224 }
225 }
226
230 "Couldn't reallocate internal structures!\n");
232 }
233 }
234
236
237 /* skip decTimeEst field if present */
240
241 /* decode macroblock and block huffman codebooks */
245
247
250
252
253 /* TODO: ignore this parameter if unused */
255
257
258 /* skip picture header extension if any */
260 av_dlog(avctx,
"Pic hdr extension encountered!\n");
262 }
263
266 }
267
269
270 return 0;
271 }
272
273
274 /**
275 * Decode Indeo 4 band header.
276 *
277 * @param[in,out] ctx pointer to the decoder context
278 * @param[in,out] band pointer to the band descriptor
279 * @param[in] avctx pointer to the AVCodecContext
280 * @return result code: 0 = OK, negative number = error
281 */
284 {
285 int plane, band_num, indx, transform_id, scan_indx;
286 int i;
287 int quant_mat;
288
294 }
295
298 /* skip header size
299 * If header size is not given, header size is 4 bytes. */
302
308 }
309 #if IVI4_STREAM_ANALYSER
311 ctx->uses_fullpel = 1;
312 #endif
313
317
319 if (indx == 3) {
322 }
325
328
330
337 }
338 if ((transform_id >= 7 && transform_id <= 9) ||
339 transform_id == 17) {
342 }
343
344 if (transform_id < 10 && band->blk_size < 8) {
347 }
348 #if IVI4_STREAM_ANALYSER
349 if ((transform_id >= 0 && transform_id <= 2) || transform_id == 10)
350 ctx->uses_haar = 1;
351 #endif
352
357
359 if ((scan_indx>4 && scan_indx<10) != (band->
blk_size==4)) {
362 }
363 if (scan_indx == 15) {
366 }
369
371 if (quant_mat == 31) {
374 }
375 if (quant_mat > 21) {
378 }
380 }
385 }
389 }
390
391 /* decode block huffman codebook */
395
396 /* select appropriate rvmap table for this band */
398
399 /* decode rvmap probability corrections if any */
400 band->
num_corr = 0;
/* there is no corrections */
407 }
408
409 /* read correction pairs */
410 for (i = 0; i < band->
num_corr * 2; i++)
412 }
413 }
414
418 } else {
421 }
422
423 /* Indeo 4 doesn't use scale tables */
426
428
432 }
433
434 return 0;
435 }
436
437
438 /**
439 * Decode information (block type, cbp, quant delta, motion vector)
440 * for all macroblocks in the current tile.
441 *
442 * @param[in,out] ctx pointer to the decoder context
443 * @param[in,out] band pointer to the band descriptor
444 * @param[in,out] tile pointer to the tile descriptor
445 * @param[in] avctx pointer to the AVCodecContext
446 * @return result code: 0 = OK, negative number = error
447 */
450 {
451 int x, y, mv_x, mv_y, mv_delta, offs, mb_offset, blks_per_mb,
452 mv_scale, mb_type_bits, s;
455
459
462
463 /* scale factor for motion vectors */
465 mv_x = mv_y = 0;
466
469 return -1;
470 }
471
473 mb_offset = offs;
474
479
484 }
485 mb->
type = 1;
/* empty macroblocks are always INTER */
486 mb->
cbp = 0;
/* all blocks are empty */
487
493 }
494
495 mb->
mv_x = mb->
mv_y = 0;
/* no motion vector coded */
497 /* motion vector inheritance */
498 if (mv_scale) {
501 } else {
504 }
505 }
506 } else {
508 mb->
type = ref_mb->
type;
/* copy mb_type from corresponding reference mb */
511 mb->
type = 0;
/* mb_type is always INTRA for intra-frames */
512 } else {
514 }
515
517
526 }
527
529 mb->
mv_x = mb->
mv_y = 0;
/* there is no motion vector in intra-macroblocks */
530 } else {
532 /* motion vector inheritance */
533 if (mv_scale) {
536 } else {
539 }
540 } else {
541 /* decode motion vector deltas */
550 }
551 }
552 }
553
556 if ( x + (mb->
mv_x >>s) + (y+ (mb->
mv_y >>s))*band->
pitch < 0 ||
561 }
562
563 mb++;
564 if (ref_mb)
565 ref_mb++;
567 }
568
569 offs += row_offset;
570 }
571
573
574 return 0;
575 }
576
577
578 /**
579 * Rearrange decoding and reference buffers.
580 *
581 * @param[in,out] ctx pointer to the decoder context
582 */
584 {
592 break;
594 break;
595 }
596
601 /* FALLTHROUGH */
605 break;
609 break;
610 }
611 }
612
613
615 {
617 }
618
619
621 {
623
625
626 /* copy rvmap tables in our context so we can apply changes to them */
628
629 /* Force allocation of the internal buffers */
630 /* during picture header decoding. */
633
635
641
642 return 0;
643 }
644
645
656 };