]> git.sesse.net Git - ffmpeg/blob - libavformat/iff.c
Merge commit 'bf2064f046af64c59a416c814474a39b0a457569'
[ffmpeg] / libavformat / iff.c
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
33 #include "libavutil/avassert.h"
34 #include "libavutil/channel_layout.h"
35 #include "libavutil/intreadwrite.h"
36 #include "libavutil/dict.h"
37 #include "libavcodec/bytestream.h"
38 #include "avformat.h"
39 #include "internal.h"
40
41 #define ID_8SVX       MKTAG('8','S','V','X')
42 #define ID_16SV       MKTAG('1','6','S','V')
43 #define ID_MAUD       MKTAG('M','A','U','D')
44 #define ID_MHDR       MKTAG('M','H','D','R')
45 #define ID_MDAT       MKTAG('M','D','A','T')
46 #define ID_VHDR       MKTAG('V','H','D','R')
47 #define ID_ATAK       MKTAG('A','T','A','K')
48 #define ID_RLSE       MKTAG('R','L','S','E')
49 #define ID_CHAN       MKTAG('C','H','A','N')
50 #define ID_PBM        MKTAG('P','B','M',' ')
51 #define ID_ILBM       MKTAG('I','L','B','M')
52 #define ID_BMHD       MKTAG('B','M','H','D')
53 #define ID_DGBL       MKTAG('D','G','B','L')
54 #define ID_CAMG       MKTAG('C','A','M','G')
55 #define ID_CMAP       MKTAG('C','M','A','P')
56 #define ID_ACBM       MKTAG('A','C','B','M')
57 #define ID_DEEP       MKTAG('D','E','E','P')
58 #define ID_RGB8       MKTAG('R','G','B','8')
59 #define ID_RGBN       MKTAG('R','G','B','N')
60
61 #define ID_FORM       MKTAG('F','O','R','M')
62 #define ID_ANNO       MKTAG('A','N','N','O')
63 #define ID_AUTH       MKTAG('A','U','T','H')
64 #define ID_CHRS       MKTAG('C','H','R','S')
65 #define ID_COPYRIGHT  MKTAG('(','c',')',' ')
66 #define ID_CSET       MKTAG('C','S','E','T')
67 #define ID_FVER       MKTAG('F','V','E','R')
68 #define ID_NAME       MKTAG('N','A','M','E')
69 #define ID_TEXT       MKTAG('T','E','X','T')
70 #define ID_ABIT       MKTAG('A','B','I','T')
71 #define ID_BODY       MKTAG('B','O','D','Y')
72 #define ID_DBOD       MKTAG('D','B','O','D')
73 #define ID_DPEL       MKTAG('D','P','E','L')
74 #define ID_DLOC       MKTAG('D','L','O','C')
75 #define ID_TVDC       MKTAG('T','V','D','C')
76
77 #define LEFT    2
78 #define RIGHT   4
79 #define STEREO  6
80
81 /**
82  * This number of bytes if added at the beginning of each AVPacket
83  * which contain additional information about video properties
84  * which has to be shared between demuxer and decoder.
85  * This number may change between frames, e.g. the demuxer might
86  * set it to smallest possible size of 2 to indicate that there's
87  * no extradata changing in this frame.
88  */
89 #define IFF_EXTRA_VIDEO_SIZE 41
90
91 typedef enum {
92     COMP_NONE,
93     COMP_FIB,
94     COMP_EXP
95 } svx8_compression_type;
96
97 typedef struct {
98     int64_t  body_pos;
99     int64_t  body_end;
100     uint32_t  body_size;
101     svx8_compression_type   svx8_compression;
102     unsigned  maud_bits;
103     unsigned  maud_compression;
104     unsigned  bitmap_compression;  ///< delta compression method used
105     unsigned  bpp;          ///< bits per plane to decode (differs from bits_per_coded_sample if HAM)
106     unsigned  ham;          ///< 0 if non-HAM or number of hold bits (6 for bpp > 6, 4 otherwise)
107     unsigned  flags;        ///< 1 for EHB, 0 is no extra half darkening
108     unsigned  transparency; ///< transparency color index in palette
109     unsigned  masking;      ///< masking method used
110     uint8_t   tvdc[32];     ///< TVDC lookup table
111 } IffDemuxContext;
112
113 /* Metadata string read */
114 static int get_metadata(AVFormatContext *s,
115                         const char *const tag,
116                         const unsigned data_size)
117 {
118     uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
119
120     if (!buf)
121         return AVERROR(ENOMEM);
122
123     if (avio_read(s->pb, buf, data_size) != data_size) {
124         av_free(buf);
125         return AVERROR(EIO);
126     }
127     buf[data_size] = 0;
128     av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL);
129     return 0;
130 }
131
132 static int iff_probe(AVProbeData *p)
133 {
134     const uint8_t *d = p->buf;
135
136     if (  AV_RL32(d)   == ID_FORM &&
137          (AV_RL32(d+8) == ID_8SVX ||
138           AV_RL32(d+8) == ID_16SV ||
139           AV_RL32(d+8) == ID_MAUD ||
140           AV_RL32(d+8) == ID_PBM  ||
141           AV_RL32(d+8) == ID_ACBM ||
142           AV_RL32(d+8) == ID_DEEP ||
143           AV_RL32(d+8) == ID_ILBM ||
144           AV_RL32(d+8) == ID_RGB8 ||
145           AV_RL32(d+8) == ID_RGBN) )
146         return AVPROBE_SCORE_MAX;
147     return 0;
148 }
149
150 static const uint8_t deep_rgb24[] = {0, 0, 0, 3, 0, 1, 0, 8, 0, 2, 0, 8, 0, 3, 0, 8};
151 static const uint8_t deep_rgba[]  = {0, 0, 0, 4, 0, 1, 0, 8, 0, 2, 0, 8, 0, 3, 0, 8};
152 static const uint8_t deep_bgra[]  = {0, 0, 0, 4, 0, 3, 0, 8, 0, 2, 0, 8, 0, 1, 0, 8};
153 static const uint8_t deep_argb[]  = {0, 0, 0, 4, 0,17, 0, 8, 0, 1, 0, 8, 0, 2, 0, 8};
154 static const uint8_t deep_abgr[]  = {0, 0, 0, 4, 0,17, 0, 8, 0, 3, 0, 8, 0, 2, 0, 8};
155
156 static int iff_read_header(AVFormatContext *s)
157 {
158     IffDemuxContext *iff = s->priv_data;
159     AVIOContext *pb = s->pb;
160     AVStream *st;
161     uint8_t *buf;
162     uint32_t chunk_id, data_size;
163     uint32_t screenmode = 0, num, den;
164     unsigned transparency = 0;
165     unsigned masking = 0; // no mask
166     uint8_t fmt[16];
167     int fmt_size;
168
169     st = avformat_new_stream(s, NULL);
170     if (!st)
171         return AVERROR(ENOMEM);
172
173     st->codec->channels = 1;
174     st->codec->channel_layout = AV_CH_LAYOUT_MONO;
175     avio_skip(pb, 8);
176     // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
177     st->codec->codec_tag = avio_rl32(pb);
178     iff->bitmap_compression = -1;
179     iff->svx8_compression = -1;
180     iff->maud_bits = -1;
181     iff->maud_compression = -1;
182
183     while(!url_feof(pb)) {
184         uint64_t orig_pos;
185         int res;
186         const char *metadata_tag = NULL;
187         chunk_id = avio_rl32(pb);
188         data_size = avio_rb32(pb);
189         orig_pos = avio_tell(pb);
190
191         switch(chunk_id) {
192         case ID_VHDR:
193             st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
194
195             if (data_size < 14)
196                 return AVERROR_INVALIDDATA;
197             avio_skip(pb, 12);
198             st->codec->sample_rate = avio_rb16(pb);
199             if (data_size >= 16) {
200                 avio_skip(pb, 1);
201                 iff->svx8_compression = avio_r8(pb);
202             }
203             break;
204
205         case ID_MHDR:
206             st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
207
208             if (data_size < 32)
209                 return AVERROR_INVALIDDATA;
210             avio_skip(pb, 4);
211             iff->maud_bits = avio_rb16(pb);
212             avio_skip(pb, 2);
213             num = avio_rb32(pb);
214             den = avio_rb16(pb);
215             if (!den)
216                 return AVERROR_INVALIDDATA;
217             avio_skip(pb, 2);
218             st->codec->sample_rate = num / den;
219             st->codec->channels = avio_rb16(pb);
220             iff->maud_compression = avio_rb16(pb);
221             if (st->codec->channels == 1)
222                 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
223             else if (st->codec->channels == 2)
224                 st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
225             break;
226
227         case ID_ABIT:
228         case ID_BODY:
229         case ID_DBOD:
230         case ID_MDAT:
231             iff->body_pos = avio_tell(pb);
232             iff->body_end = iff->body_pos + data_size;
233             iff->body_size = data_size;
234             break;
235
236         case ID_CHAN:
237             if (data_size < 4)
238                 return AVERROR_INVALIDDATA;
239             if (avio_rb32(pb) < 6) {
240                 st->codec->channels       = 1;
241                 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
242             } else {
243                 st->codec->channels       = 2;
244                 st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
245             }
246             break;
247
248         case ID_CAMG:
249             if (data_size < 4)
250                 return AVERROR_INVALIDDATA;
251             screenmode                = avio_rb32(pb);
252             break;
253
254         case ID_CMAP:
255             if (data_size < 3 || data_size > 768 || data_size % 3) {
256                  av_log(s, AV_LOG_ERROR, "Invalid CMAP chunk size %"PRIu32"\n",
257                         data_size);
258                  return AVERROR_INVALIDDATA;
259             }
260             st->codec->extradata_size = data_size + IFF_EXTRA_VIDEO_SIZE;
261             st->codec->extradata      = av_malloc(data_size + IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
262             if (!st->codec->extradata)
263                 return AVERROR(ENOMEM);
264             if (avio_read(pb, st->codec->extradata + IFF_EXTRA_VIDEO_SIZE, data_size) < 0)
265                 return AVERROR(EIO);
266             break;
267
268         case ID_BMHD:
269             st->codec->codec_type            = AVMEDIA_TYPE_VIDEO;
270             if (data_size <= 8)
271                 return AVERROR_INVALIDDATA;
272             st->codec->width                 = avio_rb16(pb);
273             st->codec->height                = avio_rb16(pb);
274             avio_skip(pb, 4); // x, y offset
275             st->codec->bits_per_coded_sample = avio_r8(pb);
276             if (data_size >= 10)
277                 masking                      = avio_r8(pb);
278             if (data_size >= 11)
279                 iff->bitmap_compression      = avio_r8(pb);
280             if (data_size >= 14) {
281                 avio_skip(pb, 1); // padding
282                 transparency                 = avio_rb16(pb);
283             }
284             if (data_size >= 16) {
285                 st->sample_aspect_ratio.num  = avio_r8(pb);
286                 st->sample_aspect_ratio.den  = avio_r8(pb);
287             }
288             break;
289
290         case ID_DPEL:
291             if (data_size < 4 || (data_size & 3))
292                 return AVERROR_INVALIDDATA;
293             if ((fmt_size = avio_read(pb, fmt, sizeof(fmt))) < 0)
294                 return fmt_size;
295             if (fmt_size == sizeof(deep_rgb24) && !memcmp(fmt, deep_rgb24, sizeof(deep_rgb24)))
296                 st->codec->pix_fmt = AV_PIX_FMT_RGB24;
297             else if (fmt_size == sizeof(deep_rgba) && !memcmp(fmt, deep_rgba, sizeof(deep_rgba)))
298                 st->codec->pix_fmt = AV_PIX_FMT_RGBA;
299             else if (fmt_size == sizeof(deep_bgra) && !memcmp(fmt, deep_bgra, sizeof(deep_bgra)))
300                 st->codec->pix_fmt = AV_PIX_FMT_BGRA;
301             else if (fmt_size == sizeof(deep_argb) && !memcmp(fmt, deep_argb, sizeof(deep_argb)))
302                 st->codec->pix_fmt = AV_PIX_FMT_ARGB;
303             else if (fmt_size == sizeof(deep_abgr) && !memcmp(fmt, deep_abgr, sizeof(deep_abgr)))
304                 st->codec->pix_fmt = AV_PIX_FMT_ABGR;
305             else {
306                 avpriv_request_sample(s, "color format %.16s", fmt);
307                 return AVERROR_PATCHWELCOME;
308             }
309             break;
310
311         case ID_DGBL:
312             st->codec->codec_type            = AVMEDIA_TYPE_VIDEO;
313             if (data_size < 8)
314                 return AVERROR_INVALIDDATA;
315             st->codec->width                 = avio_rb16(pb);
316             st->codec->height                = avio_rb16(pb);
317             iff->bitmap_compression          = avio_rb16(pb);
318             st->sample_aspect_ratio.num      = avio_r8(pb);
319             st->sample_aspect_ratio.den      = avio_r8(pb);
320             st->codec->bits_per_coded_sample = 24;
321             break;
322
323         case ID_DLOC:
324             if (data_size < 4)
325                 return AVERROR_INVALIDDATA;
326             st->codec->width  = avio_rb16(pb);
327             st->codec->height = avio_rb16(pb);
328             break;
329
330         case ID_TVDC:
331             if (data_size < sizeof(iff->tvdc))
332                 return AVERROR_INVALIDDATA;
333             res = avio_read(pb, iff->tvdc, sizeof(iff->tvdc));
334             if (res < 0)
335                 return res;
336             break;
337
338         case ID_ANNO:
339         case ID_TEXT:      metadata_tag = "comment";   break;
340         case ID_AUTH:      metadata_tag = "artist";    break;
341         case ID_COPYRIGHT: metadata_tag = "copyright"; break;
342         case ID_NAME:      metadata_tag = "title";     break;
343         }
344
345         if (metadata_tag) {
346             if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
347                 av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!\n", metadata_tag);
348                 return res;
349             }
350         }
351         avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1));
352     }
353
354     avio_seek(pb, iff->body_pos, SEEK_SET);
355
356     switch(st->codec->codec_type) {
357     case AVMEDIA_TYPE_AUDIO:
358         avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
359
360         if (st->codec->codec_tag == ID_16SV)
361             st->codec->codec_id = AV_CODEC_ID_PCM_S16BE_PLANAR;
362         else if (st->codec->codec_tag == ID_MAUD) {
363             if (iff->maud_bits == 8 && !iff->maud_compression) {
364                 st->codec->codec_id = AV_CODEC_ID_PCM_U8;
365             } else if (iff->maud_bits == 16 && !iff->maud_compression) {
366                 st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
367             } else if (iff->maud_bits ==  8 && iff->maud_compression == 2) {
368                 st->codec->codec_id = AV_CODEC_ID_PCM_ALAW;
369             } else if (iff->maud_bits ==  8 && iff->maud_compression == 3) {
370                 st->codec->codec_id = AV_CODEC_ID_PCM_MULAW;
371             } else {
372                 avpriv_request_sample(s, "compression %d and bit depth %d", iff->maud_compression, iff->maud_bits);
373                 return AVERROR_PATCHWELCOME;
374             }
375
376             st->codec->bits_per_coded_sample =
377                 av_get_bits_per_sample(st->codec->codec_id);
378
379             st->codec->block_align =
380                 st->codec->bits_per_coded_sample * st->codec->channels / 8;
381         } else {
382         switch (iff->svx8_compression) {
383         case COMP_NONE:
384             st->codec->codec_id = AV_CODEC_ID_PCM_S8_PLANAR;
385             break;
386         case COMP_FIB:
387             st->codec->codec_id = AV_CODEC_ID_8SVX_FIB;
388             break;
389         case COMP_EXP:
390             st->codec->codec_id = AV_CODEC_ID_8SVX_EXP;
391             break;
392         default:
393             av_log(s, AV_LOG_ERROR,
394                    "Unknown SVX8 compression method '%d'\n", iff->svx8_compression);
395             return -1;
396         }
397         }
398
399         st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
400         st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
401         st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
402         break;
403
404     case AVMEDIA_TYPE_VIDEO:
405         iff->bpp          = st->codec->bits_per_coded_sample;
406         if ((screenmode & 0x800 /* Hold And Modify */) && iff->bpp <= 8) {
407             iff->ham      = iff->bpp > 6 ? 6 : 4;
408             st->codec->bits_per_coded_sample = 24;
409         }
410         iff->flags        = (screenmode & 0x80 /* Extra HalfBrite */) && iff->bpp <= 8;
411         iff->masking      = masking;
412         iff->transparency = transparency;
413
414         if (!st->codec->extradata) {
415             st->codec->extradata_size = IFF_EXTRA_VIDEO_SIZE;
416             st->codec->extradata      = av_malloc(IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
417             if (!st->codec->extradata)
418                 return AVERROR(ENOMEM);
419         }
420         av_assert0(st->codec->extradata_size >= IFF_EXTRA_VIDEO_SIZE);
421         buf = st->codec->extradata;
422         bytestream_put_be16(&buf, IFF_EXTRA_VIDEO_SIZE);
423         bytestream_put_byte(&buf, iff->bitmap_compression);
424         bytestream_put_byte(&buf, iff->bpp);
425         bytestream_put_byte(&buf, iff->ham);
426         bytestream_put_byte(&buf, iff->flags);
427         bytestream_put_be16(&buf, iff->transparency);
428         bytestream_put_byte(&buf, iff->masking);
429         bytestream_put_buffer(&buf, iff->tvdc, sizeof(iff->tvdc));
430         st->codec->codec_id = AV_CODEC_ID_IFF_ILBM;
431         break;
432     default:
433         return -1;
434     }
435
436     return 0;
437 }
438
439 static int iff_read_packet(AVFormatContext *s,
440                            AVPacket *pkt)
441 {
442     IffDemuxContext *iff = s->priv_data;
443     AVIOContext *pb = s->pb;
444     AVStream *st = s->streams[0];
445     int ret;
446     int64_t pos = avio_tell(pb);
447
448     if (pos >= iff->body_end)
449         return AVERROR_EOF;
450
451     if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
452         if (st->codec->codec_tag == ID_MAUD) {
453             ret = av_get_packet(pb, pkt, FFMIN(iff->body_end - pos, 1024 * st->codec->block_align));
454         } else {
455             ret = av_get_packet(pb, pkt, iff->body_size);
456         }
457     } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
458         uint8_t *buf;
459
460         if (av_new_packet(pkt, iff->body_size + 2) < 0) {
461             return AVERROR(ENOMEM);
462         }
463
464         buf = pkt->data;
465         bytestream_put_be16(&buf, 2);
466         ret = avio_read(pb, buf, iff->body_size);
467         if (ret<0) {
468             av_free_packet(pkt);
469         } else if (ret < iff->body_size)
470             av_shrink_packet(pkt, ret + 2);
471     } else {
472         av_assert0(0);
473     }
474
475     if (pos == iff->body_pos)
476         pkt->flags |= AV_PKT_FLAG_KEY;
477     if (ret < 0)
478         return ret;
479     pkt->stream_index = 0;
480     return ret;
481 }
482
483 AVInputFormat ff_iff_demuxer = {
484     .name           = "iff",
485     .long_name      = NULL_IF_CONFIG_SMALL("IFF (Interchange File Format)"),
486     .priv_data_size = sizeof(IffDemuxContext),
487     .read_probe     = iff_probe,
488     .read_header    = iff_read_header,
489     .read_packet    = iff_read_packet,
490     .flags          = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,
491 };