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