1 /*
2 * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
3 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
4 * Copyright (c) 2010 Sebastian Vater <cdgs.basty@googlemail.com>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * IFF file demuxer
26 * by Jaikrishnan Menon
27 * for more information on the .iff file format, visit:
28 * http://wiki.multimedia.cx/index.php?title=IFF
29 */
30
31 #include <inttypes.h>
32
41
42 #define ID_8SVX MKTAG('8','S','V','X')
43 #define ID_16SV MKTAG('1','6','S','V')
44 #define ID_MAUD MKTAG('M','A','U','D')
45 #define ID_MHDR MKTAG('M','H','D','R')
46 #define ID_MDAT MKTAG('M','D','A','T')
47 #define ID_VHDR MKTAG('V','H','D','R')
48 #define ID_ATAK MKTAG('A','T','A','K')
49 #define ID_RLSE MKTAG('R','L','S','E')
50 #define ID_CHAN MKTAG('C','H','A','N')
51 #define ID_PBM MKTAG('P','B','M',' ')
52 #define ID_ILBM MKTAG('I','L','B','M')
53 #define ID_BMHD MKTAG('B','M','H','D')
54 #define ID_DGBL MKTAG('D','G','B','L')
55 #define ID_CAMG MKTAG('C','A','M','G')
56 #define ID_CMAP MKTAG('C','M','A','P')
57 #define ID_ACBM MKTAG('A','C','B','M')
58 #define ID_DEEP MKTAG('D','E','E','P')
59 #define ID_RGB8 MKTAG('R','G','B','8')
60 #define ID_RGBN MKTAG('R','G','B','N')
61 #define ID_DSD MKTAG('D','S','D',' ')
62 #define ID_DST MKTAG('D','S','T',' ')
63 #define ID_DSTC MKTAG('D','S','T','C')
64 #define ID_DSTF MKTAG('D','S','T','F')
65 #define ID_FRTE MKTAG('F','R','T','E')
66 #define ID_ANIM MKTAG('A','N','I','M')
67 #define ID_ANHD MKTAG('A','N','H','D')
68 #define ID_DLTA MKTAG('D','L','T','A')
69 #define ID_DPAN MKTAG('D','P','A','N')
70
71 #define ID_FORM MKTAG('F','O','R','M')
72 #define ID_FRM8 MKTAG('F','R','M','8')
73 #define ID_ANNO MKTAG('A','N','N','O')
74 #define ID_AUTH MKTAG('A','U','T','H')
75 #define ID_CHRS MKTAG('C','H','R','S')
76 #define ID_COPYRIGHT MKTAG('(','c',')',' ')
77 #define ID_CSET MKTAG('C','S','E','T')
78 #define ID_FVER MKTAG('F','V','E','R')
79 #define ID_NAME MKTAG('N','A','M','E')
80 #define ID_TEXT MKTAG('T','E','X','T')
81 #define ID_ABIT MKTAG('A','B','I','T')
82 #define ID_BODY MKTAG('B','O','D','Y')
83 #define ID_DBOD MKTAG('D','B','O','D')
84 #define ID_DPEL MKTAG('D','P','E','L')
85 #define ID_DLOC MKTAG('D','L','O','C')
86 #define ID_TVDC MKTAG('T','V','D','C')
87
91
92 /**
93 * This number of bytes if added at the beginning of each AVPacket
94 * which contain additional information about video properties
95 * which has to be shared between demuxer and decoder.
96 * This number may change between frames, e.g. the demuxer might
97 * set it to smallest possible size of 2 to indicate that there's
98 * no extradata changing in this frame.
99 */
100 #define IFF_EXTRA_VIDEO_SIZE 41
101
107
117 unsigned bpp;
///< bits per plane to decode (differs from bits_per_coded_sample if HAM)
118 unsigned ham;
///< 0 if non-HAM or number of hold bits (6 for bpp > 6, 4 otherwise)
119 unsigned flags;
///< 1 for EHB, 0 is no extra half darkening
122 uint8_t
tvdc[32];
///< TVDC lookup table
125
126 /* Metadata string read */
128 const char *
const tag,
129 const unsigned data_size)
130 {
131 uint8_t *buf = ((data_size + 1) == 0) ?
NULL :
av_malloc(data_size + 1);
132
133 if (!buf)
135
136 if (
avio_read(
s->pb, buf, data_size) != data_size) {
139 }
140 buf[data_size] = 0;
142 return 0;
143 }
144
146 {
147 const uint8_t *
d = p->
buf;
148
162 return 0;
163 }
164
169 };
170
171
172 #define DSD_SLFT MKTAG('S','L','F','T')
173 #define DSD_SRGT MKTAG('S','R','G','T')
174 #define DSD_MLFT MKTAG('M','L','F','T')
175 #define DSD_MRGT MKTAG('M','R','G','T')
176 #define DSD_C MKTAG('C',' ',' ',' ')
177 #define DSD_LS MKTAG('L','S',' ',' ')
178 #define DSD_RS MKTAG('R','S',' ',' ')
179 #define DSD_LFE MKTAG('L','F','E',' ')
180
184
189
194 };
195
198 { 0 }, { 0 },
200 };
201
203 "dsd_source_comment",
204 "analogue_source_comment",
205 "pcm_source_comment",
206 };
207
209 "general_remark",
210 "operator_name",
211 "creating_machine",
212 "timezone",
213 "file_revision"
214 };
215
217 {
219
224 const char * metadata_tag =
NULL;
225
226 if (
size >= INT64_MAX)
228
230 case MKTAG(
'D',
'I',
'A',
'R'): metadata_tag =
"artist";
break;
231 case MKTAG(
'D',
'I',
'T',
'I'): metadata_tag =
"title";
break;
232 }
233
234 if (metadata_tag &&
size > 4) {
240 }
241 }
242
244 }
245
246 return 0;
247 }
248
250 {
252 char abss[24];
254 int dsd_layout[6];
256
261
262 if (
size >= INT64_MAX)
264
266 case MKTAG(
'A',
'B',
'S',
'S'):
274 break;
275
276 case MKTAG(
'C',
'H',
'N',
'L'):
281 if (size < 2 + st->codecpar->ch_layout.nb_channels * 4)
285 break;
286 }
292 !memcmp(
d->dsd_layout, dsd_layout,
d->layout.nb_channels *
sizeof(uint32_t))) {
294 break;
295 }
296 }
297 break;
298
299 case MKTAG(
'C',
'M',
'P',
'R'):
308 }
309 break;
310
311 case MKTAG(
'F',
'S',
' ',
' '):
315 break;
316
317 case MKTAG(
'I',
'D',
'3',
' '):
319 if (id3v2_extra_meta) {
324 }
326 }
327
331 }
332 break;
333
334 case MKTAG(
'L',
'S',
'C',
'O'):
343 }
344 break;
345 }
346
348 }
349
350 return 0;
351 }
352
354 {
357 uint32_t chunk_id;
358 uint64_t chunk_pos, data_pos, data_size;
360
365
369
370 if (data_size < 1 || data_size >= INT64_MAX)
372
373 switch (chunk_id) {
378 return 0;
379 }
383 if (data_size & 1)
387 pkt->
duration =
s->streams[0]->codecpar->sample_rate / 75;
389
392 return 0;
393
395 return 0;
396
398 if (data_size < 4)
400 s->streams[0]->duration =
avio_rb32(pb) * (uint64_t)
s->streams[0]->codecpar->sample_rate / 75;
401
402 break;
403 }
404
406 }
407
409 }
410
411 static const uint8_t
deep_rgb24[] = {0, 0, 0, 3, 0, 1, 0, 8, 0, 2, 0, 8, 0, 3, 0, 8};
412 static const uint8_t
deep_rgba[] = {0, 0, 0, 4, 0, 1, 0, 8, 0, 2, 0, 8, 0, 3, 0, 8};
413 static const uint8_t
deep_bgra[] = {0, 0, 0, 4, 0, 3, 0, 8, 0, 2, 0, 8, 0, 1, 0, 8};
414 static const uint8_t
deep_argb[] = {0, 0, 0, 4, 0,17, 0, 8, 0, 1, 0, 8, 0, 2, 0, 8};
415 static const uint8_t
deep_abgr[] = {0, 0, 0, 4, 0,17, 0, 8, 0, 3, 0, 8, 0, 2, 0, 8};
416
418 {
422 uint8_t *buf;
423 uint32_t chunk_id;
424 uint64_t data_size;
425 uint32_t screenmode = 0, num, den;
426 unsigned transparency = 0;
427 unsigned masking = 0; // no mask
428 uint8_t fmt[16];
429 int fmt_size;
430
432 if (!st)
434
438 // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
442 }
447
449 uint64_t orig_pos;
450 int res;
451 const char *metadata_tag =
NULL;
456
457 if (data_size >= INT64_MAX)
459
460 switch(chunk_id) {
463
464 if (data_size < 14)
468 if (data_size >= 16) {
471 }
472 break;
473
476
477 if (data_size < 32)
484 if (!den)
495 break;
496
510 }
511 break;
512
514 if (data_size < 4)
518 } else {
520 }
521 break;
522
524 if (data_size < 4)
527 break;
528
530 if (data_size < 3 || data_size > 768 || data_size % 3) {
532 data_size);
534 }
537 if (res < 0)
538 return res;
543 }
544 break;
545
548 if (data_size <= 8)
554 if (data_size >= 10)
556 if (data_size >= 11)
558 if (data_size >= 14) {
561 }
562 if (data_size >= 16) {
565 }
566 break;
567
569 break;
570
574 break;
575
577 if (data_size < 4 || (data_size & 3))
579 if ((fmt_size =
avio_read(pb, fmt,
sizeof(fmt))) < 0)
580 return fmt_size;
591 else {
594 }
595 break;
596
599 if (data_size < 8)
607 break;
608
610 if (data_size < 4)
614 break;
615
617 if (data_size <
sizeof(iff->
tvdc))
620 if (res < 0)
621 return res;
622 break;
623
625 case ID_TEXT: metadata_tag =
"comment";
break;
626 case ID_AUTH: metadata_tag =
"artist";
break;
628 case ID_NAME: metadata_tag =
"title";
break;
629
630 /* DSD tags */
631
632 case MKTAG(
'F',
'V',
'E',
'R'):
633 if (data_size < 4)
638 break;
639
640 case MKTAG(
'D',
'I',
'I',
'N'):
642 if (res < 0)
643 return res;
644 break;
645
646 case MKTAG(
'P',
'R',
'O',
'P'):
647 if (data_size < 4)
651 break;
652 }
654 if (res < 0)
655 return res;
656 break;
657
658 case MKTAG(
'C',
'O',
'M',
'T'):
659 if (data_size < 2)
662 for (
i = 0;
i < nb_comments;
i++) {
666 int metadata_size;
667
673 snprintf(
tmp,
sizeof(
tmp),
"%04d-%02d-%02d %02d:%02d", year, mon, day, hour,
min);
675
679 case 1:
681 tag =
"channel_comment";
682 else {
685 }
686 break;
687 case 2:
689 break;
690 case 3:
692 break;
693 default:
695 }
696
700 return res;
701 }
702
703 if (metadata_size & 1)
705 }
706 break;
707 }
708
709 if (metadata_tag) {
712 return res;
713 }
714 }
716 }
717
720 else
722
726
738 } else {
741 }
747 break;
750 break;
753 break;
754 default:
757 return -1;
758 }
759 }
760
769 break;
770
775 if ((screenmode & 0x800
/* Hold And Modify */) && iff->
bpp <= 8) {
776 iff->
ham = iff->
bpp > 6 ? 6 : 4;
778 }
779 iff->
flags = (screenmode & 0x80
/* Extra HalfBrite */) && iff->
bpp <= 8;
782
787 }
792 bytestream_put_byte(&buf, iff->
bpp);
793 bytestream_put_byte(&buf, iff->
ham);
794 bytestream_put_byte(&buf, iff->
flags);
796 bytestream_put_byte(&buf, iff->
masking);
799 break;
800 default:
801 return -1;
802 }
803
804 return 0;
805 }
806
808 {
810
814 unsigned chunk = bytestream2_get_le32(&gb);
815 unsigned size = bytestream2_get_be32(&gb);
816
819 break;
821 return bytestream2_get_be32(&gb);
822 } else {
824 }
825 }
826 return 10;
827 }
828
831 {
837
842
848 } else {
852 }
855 uint64_t data_size, orig_pos;
856 uint32_t chunk_id, chunk_id2;
857
861
866
870 break;
871 }
else if (chunk_id ==
ID_FORM &&
873 continue;
874 } else {
876 }
877 }
891 } else {
893 }
894
899 }
900
909 };