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
36
37 /**
38 * Indeo 4 frame types.
39 */
40 enum {
48 };
49
50 #define IVI4_PIC_SIZE_ESC 7
51
52
53 static const struct {
65 { NULL, NULL, 0 }, /* inverse DCT 8x8 */
66 { NULL, NULL, 0 }, /* inverse DCT 8x1 */
67 { NULL, NULL, 0 }, /* inverse DCT 1x8 */
70 { NULL, NULL, 0 }, /* no transform 4x4 */
75 { NULL, NULL, 0 }, /* inverse DCT 4x4 */
76 };
77
78 /**
79 * Decode subdivision of a plane.
80 * This is a simplified version that checks for two supported subdivisions:
81 * - 1 wavelet band per plane, size factor 1:1, code pattern: 3
82 * - 4 wavelet bands per plane, size factor 1:4, code pattern: 2,3,3,3,3
83 * Anything else is either unsupported or corrupt.
84 *
85 * @param[in,out] gb the GetBit context
86 * @return number of wavelet bands or 0 on error
87 */
89 {
90 int i;
91
93 case 3:
94 return 1;
95 case 2:
96 for (i = 0; i < 4; i++)
98 return 0;
99 return 4;
100 default:
101 return 0;
102 }
103 }
104
106 {
107 return size_factor == 15 ? def_size : (size_factor + 1) << 5;
108 }
109
110 /**
111 * Decode Indeo 4 picture header.
112 *
113 * @param[in,out] ctx pointer to the decoder context
114 * @param[in] avctx pointer to the AVCodecContext
115 * @return result code: 0 = OK, negative number = error
116 */
118 {
119 int pic_size_indx, i, p;
121
125 }
126
132 }
133
134 #if IVI4_STREAM_ANALYSER
136 ctx->has_b_frames = 1;
137 #endif
138
140 #if IVI4_STREAM_ANALYSER
142 ctx->has_transp = 1;
143 }
144 #endif
145
146 /* unknown bit: Mac decoder ignores this bit, XANIM returns error */
150 }
151
153
154 /* null frames don't contain anything else so we just return */
156 av_dlog(avctx,
"Null frame encountered!\n");
157 return 0;
158 }
159
160 /* Check key lock status. If enabled - ignore lock word. */
161 /* Usually we have to prompt the user for the password, but */
162 /* we don't do that because Indeo 4 videos can be decoded anyway */
165 av_dlog(avctx,
"Password-protected clip!\n");
166 }
167
172 } else {
175 }
176
177 /* Decode tile dimensions. */
181 #if IVI4_STREAM_ANALYSER
182 ctx->uses_tiling = 1;
183 #endif
184 } else {
187 }
188
189 /* Decode chroma subsampling. We support only 4:4 aka YVU9. */
193 }
196
197 /* decode subdivision of the planes */
204 av_log(avctx,
AV_LOG_ERROR,
"Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
207 }
208
209 /* check if picture layout was changed and reallocate buffers */
214 }
215
217
218 /* set default macroblock/block dimensions */
219 for (p = 0; p <= 2; p++) {
223 }
224 }
225
229 "Couldn't reallocate internal structures!\n");
231 }
232 }
233
235
236 /* skip decTimeEst field if present */
239
240 /* decode macroblock and block huffman codebooks */
244
246
249
251
252 /* TODO: ignore this parameter if unused */
254
256
257 /* skip picture header extension if any */
259 av_dlog(avctx,
"Pic hdr extension encountered!\n");
261 }
262
265 }
266
268
269 return 0;
270 }
271
272
273 /**
274 * Decode Indeo 4 band header.
275 *
276 * @param[in,out] ctx pointer to the decoder context
277 * @param[in,out] band pointer to the band descriptor
278 * @param[in] avctx pointer to the AVCodecContext
279 * @return result code: 0 = OK, negative number = error
280 */
283 {
284 int plane, band_num, indx, transform_id, scan_indx;
285 int i;
286 int quant_mat;
287
293 }
294
297 /* skip header size
298 * If header size is not given, header size is 4 bytes. */
301
307 }
308 #if IVI4_STREAM_ANALYSER
310 ctx->uses_fullpel = 1;
311 #endif
312
316
318 if (indx == 3) {
321 }
324
327
329
336 }
337 if ((transform_id >= 7 && transform_id <= 9) ||
338 transform_id == 17) {
341 }
342
343 if (transform_id < 10 && band->blk_size < 8) {
346 }
347 #if IVI4_STREAM_ANALYSER
348 if ((transform_id >= 0 && transform_id <= 2) || transform_id == 10)
349 ctx->uses_haar = 1;
350 #endif
351
356
358 if ((scan_indx>4 && scan_indx<10) != (band->
blk_size==4)) {
361 }
362 if (scan_indx == 15) {
365 }
368
370 if (quant_mat == 31) {
373 }
374 if (quant_mat > 21) {
377 }
379 }
384 }
388 }
392 }
393
394 /* decode block huffman codebook */
397 else
401
402 /* select appropriate rvmap table for this band */
404
405 /* decode rvmap probability corrections if any */
406 band->
num_corr = 0;
/* there is no corrections */
413 }
414
415 /* read correction pairs */
416 for (i = 0; i < band->
num_corr * 2; i++)
418 }
419 }
420
424 } else {
427 }
428
429 /* Indeo 4 doesn't use scale tables */
432
434
438 }
439
440 return 0;
441 }
442
443
444 /**
445 * Decode information (block type, cbp, quant delta, motion vector)
446 * for all macroblocks in the current tile.
447 *
448 * @param[in,out] ctx pointer to the decoder context
449 * @param[in,out] band pointer to the band descriptor
450 * @param[in,out] tile pointer to the tile descriptor
451 * @param[in] avctx pointer to the AVCodecContext
452 * @return result code: 0 = OK, negative number = error
453 */
456 {
457 int x,
y, mv_x, mv_y, mv_delta, offs, mb_offset, blks_per_mb,
458 mv_scale, mb_type_bits,
s;
461
465
468
469 /* scale factor for motion vectors */
471 mv_x = mv_y = 0;
472
475 return -1;
476 }
477
479 mb_offset = offs;
480
485
490 }
491 mb->
type = 1;
/* empty macroblocks are always INTER */
492 mb->
cbp = 0;
/* all blocks are empty */
493
499 }
500
501 mb->
mv_x = mb->
mv_y = 0;
/* no motion vector coded */
503 /* motion vector inheritance */
504 if (mv_scale) {
507 } else {
510 }
511 }
512 } else {
514 mb->
type = ref_mb->
type;
/* copy mb_type from corresponding reference mb */
517 mb->
type = 0;
/* mb_type is always INTRA for intra-frames */
518 } else {
520 }
521
523
532 }
533
535 mb->
mv_x = mb->
mv_y = 0;
/* there is no motion vector in intra-macroblocks */
536 } else {
538 /* motion vector inheritance */
539 if (mv_scale) {
542 } else {
545 }
546 } else {
547 /* decode motion vector deltas */
556 }
557 }
558 }
559
562 if ( x + (mb->
mv_x >>s) + (y+ (mb->
mv_y >>s))*band->
pitch < 0 ||
567 }
568
569 mb++;
570 if (ref_mb)
571 ref_mb++;
573 }
574
575 offs += row_offset;
576 }
577
579
580 return 0;
581 }
582
583
584 /**
585 * Rearrange decoding and reference buffers.
586 *
587 * @param[in,out] ctx pointer to the decoder context
588 */
590 {
598 break;
600 break;
601 }
602
607 /* FALLTHROUGH */
611 break;
615 break;
616 }
617 }
618
619
621 {
623 }
624
625
627 {
629
631
632 /* copy rvmap tables in our context so we can apply changes to them */
634
635 /* Force allocation of the internal buffers */
636 /* during picture header decoding. */
639
641
647
648 return 0;
649 }
650
651
662 };