iff.c
Go to the documentation of this file.
1 /*
2  * IFF (.iff) file demuxer
3  * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
4  * Copyright (c) 2010 Peter Ross <pross@xvid.org>
5  * Copyright (c) 2010 Sebastian Vater <cdgs.basty@googlemail.com>
6  *
7  * This file is part of Libav.
8  *
9  * Libav is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * Libav is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with Libav; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/dict.h"
35 #include "avformat.h"
36 #include "internal.h"
37 
38 #define ID_8SVX MKTAG('8','S','V','X')
39 #define ID_VHDR MKTAG('V','H','D','R')
40 #define ID_ATAK MKTAG('A','T','A','K')
41 #define ID_RLSE MKTAG('R','L','S','E')
42 #define ID_CHAN MKTAG('C','H','A','N')
43 #define ID_PBM MKTAG('P','B','M',' ')
44 #define ID_ILBM MKTAG('I','L','B','M')
45 #define ID_BMHD MKTAG('B','M','H','D')
46 #define ID_CMAP MKTAG('C','M','A','P')
47 
48 #define ID_FORM MKTAG('F','O','R','M')
49 #define ID_ANNO MKTAG('A','N','N','O')
50 #define ID_AUTH MKTAG('A','U','T','H')
51 #define ID_CHRS MKTAG('C','H','R','S')
52 #define ID_COPYRIGHT MKTAG('(','c',')',' ')
53 #define ID_CSET MKTAG('C','S','E','T')
54 #define ID_FVER MKTAG('F','V','E','R')
55 #define ID_NAME MKTAG('N','A','M','E')
56 #define ID_TEXT MKTAG('T','E','X','T')
57 #define ID_BODY MKTAG('B','O','D','Y')
58 #define ID_ANNO MKTAG('A','N','N','O')
59 
60 #define LEFT 2
61 #define RIGHT 4
62 #define STEREO 6
63 
64 typedef enum {
69 
70 typedef enum {
74 
75 typedef struct {
76  uint64_t body_pos;
77  uint32_t body_size;
78  uint32_t sent_bytes;
80 
81 
82 /* Metadata string read */
84  const char *const tag,
85  const unsigned data_size)
86 {
87  uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
88 
89  if (!buf)
90  return AVERROR(ENOMEM);
91 
92  if (avio_read(s->pb, buf, data_size) < 0) {
93  av_free(buf);
94  return AVERROR(EIO);
95  }
96  buf[data_size] = 0;
98  return 0;
99 }
100 
101 static int iff_probe(AVProbeData *p)
102 {
103  const uint8_t *d = p->buf;
104 
105  if ( AV_RL32(d) == ID_FORM &&
106  (AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) )
107  return AVPROBE_SCORE_MAX;
108  return 0;
109 }
110 
112 {
113  IffDemuxContext *iff = s->priv_data;
114  AVIOContext *pb = s->pb;
115  AVStream *st;
116  uint32_t chunk_id, data_size;
117  int compression = -1;
118 
119  st = avformat_new_stream(s, NULL);
120  if (!st)
121  return AVERROR(ENOMEM);
122 
123  st->codec->channels = 1;
125  avio_skip(pb, 8);
126  // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
127  st->codec->codec_tag = avio_rl32(pb);
128 
129  while(!pb->eof_reached) {
130  uint64_t orig_pos;
131  int res;
132  const char *metadata_tag = NULL;
133  chunk_id = avio_rl32(pb);
134  data_size = avio_rb32(pb);
135  orig_pos = avio_tell(pb);
136 
137  switch(chunk_id) {
138  case ID_VHDR:
140 
141  if (data_size < 14)
142  return AVERROR_INVALIDDATA;
143  avio_skip(pb, 12);
144  st->codec->sample_rate = avio_rb16(pb);
145  if (data_size >= 16) {
146  avio_skip(pb, 1);
147  compression = avio_r8(pb);
148  }
149  break;
150 
151  case ID_BODY:
152  iff->body_pos = avio_tell(pb);
153  iff->body_size = data_size;
154  break;
155 
156  case ID_CHAN:
157  if (data_size < 4)
158  return AVERROR_INVALIDDATA;
159  if (avio_rb32(pb) < 6) {
160  st->codec->channels = 1;
162  } else {
163  st->codec->channels = 2;
165  }
166  break;
167 
168  case ID_CMAP:
169  if (data_size < 3 || data_size > 768 || data_size % 3) {
170  av_log(s, AV_LOG_ERROR, "Invalid CMAP chunk size %d\n",
171  data_size);
172  return AVERROR_INVALIDDATA;
173  }
174  st->codec->extradata_size = data_size;
175  st->codec->extradata = av_malloc(data_size);
176  if (!st->codec->extradata)
177  return AVERROR(ENOMEM);
178  if (avio_read(pb, st->codec->extradata, data_size) < 0)
179  return AVERROR(EIO);
180  break;
181 
182  case ID_BMHD:
184  if (data_size <= 8)
185  return AVERROR_INVALIDDATA;
186  st->codec->width = avio_rb16(pb);
187  st->codec->height = avio_rb16(pb);
188  avio_skip(pb, 4); // x, y offset
190  if (data_size >= 11) {
191  avio_skip(pb, 1); // masking
192  compression = avio_r8(pb);
193  }
194  if (data_size >= 16) {
195  avio_skip(pb, 3); // paddding, transparent
196  st->sample_aspect_ratio.num = avio_r8(pb);
197  st->sample_aspect_ratio.den = avio_r8(pb);
198  }
199  break;
200 
201  case ID_ANNO:
202  case ID_TEXT:
203  metadata_tag = "comment";
204  break;
205 
206  case ID_AUTH:
207  metadata_tag = "artist";
208  break;
209 
210  case ID_COPYRIGHT:
211  metadata_tag = "copyright";
212  break;
213 
214  case ID_NAME:
215  metadata_tag = "title";
216  break;
217  }
218 
219  if (metadata_tag) {
220  if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
221  av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!", metadata_tag);
222  return res;
223  }
224  }
225  avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1));
226  }
227 
228  avio_seek(pb, iff->body_pos, SEEK_SET);
229 
230  switch(st->codec->codec_type) {
231  case AVMEDIA_TYPE_AUDIO:
232  avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
233 
234  switch(compression) {
235  case COMP_NONE:
237  break;
238  case COMP_FIB:
240  break;
241  case COMP_EXP:
243  break;
244  default:
245  av_log(s, AV_LOG_ERROR, "unknown compression method\n");
246  return -1;
247  }
248 
249  st->codec->bits_per_coded_sample = 8;
252  break;
253 
254  case AVMEDIA_TYPE_VIDEO:
255  switch (compression) {
256  case BITMAP_RAW:
258  break;
259  case BITMAP_BYTERUN1:
261  break;
262  default:
263  av_log(s, AV_LOG_ERROR, "unknown compression method\n");
264  return AVERROR_INVALIDDATA;
265  }
266  break;
267  default:
268  return -1;
269  }
270 
271  return 0;
272 }
273 
275  AVPacket *pkt)
276 {
277  IffDemuxContext *iff = s->priv_data;
278  AVIOContext *pb = s->pb;
279  int ret;
280 
281  if(iff->sent_bytes >= iff->body_size)
282  return AVERROR_EOF;
283 
284  ret = av_get_packet(pb, pkt, iff->body_size);
285  if (ret < 0)
286  return ret;
287 
288  if(iff->sent_bytes == 0)
289  pkt->flags |= AV_PKT_FLAG_KEY;
290  iff->sent_bytes = iff->body_size;
291 
292  pkt->stream_index = 0;
293  return ret;
294 }
295 
297  .name = "iff",
298  .long_name = NULL_IF_CONFIG_SMALL("IFF (Interchange File Format)"),
299  .priv_data_size = sizeof(IffDemuxContext),
303 };
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
Bytestream IO Context.
Definition: avio.h:68
#define ID_PBM
Definition: iff.c:43
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:3283
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:697
int num
numerator
Definition: rational.h:44
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
Definition: iff.c:71
#define ID_8SVX
Definition: iff.c:38
#define AV_CH_LAYOUT_STEREO
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:562
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2141
Format I/O context.
Definition: avformat.h:828
Public dictionary API.
#define ID_ANNO
Definition: iff.c:58
uint8_t
#define ID_NAME
Definition: iff.c:55
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:577
uint32_t body_size
Definition: iff.c:77
static int iff_probe(AVProbeData *p)
Definition: iff.c:101
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
Definition: iff.c:66
uint32_t tag
Definition: movenc.c:802
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:218
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2704
#define ID_CHAN
Definition: iff.c:42
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:446
static int iff_read_header(AVFormatContext *s)
Definition: iff.c:111
#define ID_TEXT
Definition: iff.c:56
AVDictionary * metadata
Definition: avformat.h:972
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:546
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
#define ID_FORM
Definition: iff.c:48
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2736
#define ID_ILBM
Definition: iff.c:44
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:921
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2165
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:437
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:641
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:340
#define ID_VHDR
Definition: iff.c:39
int bit_rate
the average bitrate
Definition: avcodec.h:1404
audio channel layout utility functions
Definition: iff.c:67
#define ID_BODY
Definition: iff.c:57
AVInputFormat ff_iff_demuxer
Definition: iff.c:296
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:54
int width
picture width / height.
Definition: avcodec.h:1508
#define ID_AUTH
Definition: iff.c:50
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
#define AV_RL32
Definition: intreadwrite.h:146
uint32_t sent_bytes
Definition: iff.c:78
static int iff_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: iff.c:274
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:536
Stream structure.
Definition: avformat.h:622
NULL
Definition: eval.c:52
svx8_compression_type
Definition: iff.c:64
uint64_t body_pos
Definition: iff.c:76
enum AVMediaType codec_type
Definition: avcodec.h:1347
enum AVCodecID codec_id
Definition: avcodec.h:1350
int sample_rate
samples per second
Definition: avcodec.h:2104
AVIOContext * pb
I/O context.
Definition: avformat.h:861
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1365
int extradata_size
Definition: avcodec.h:1455
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:63
This structure contains the data a format has to probe a file.
Definition: avformat.h:338
Main libavformat public API header.
#define ID_CMAP
Definition: iff.c:46
int den
denominator
Definition: rational.h:45
Definition: iff.c:65
bitmap_compression_type
Definition: iff.c:70
int eof_reached
true if eof reached
Definition: avio.h:96
int channels
number of audio channels
Definition: avcodec.h:2105
void * priv_data
Format private data.
Definition: avformat.h:848
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:455
static int get_metadata(AVFormatContext *s, const char *const tag, const unsigned data_size)
Definition: iff.c:83
int stream_index
Definition: avcodec.h:917
#define ID_COPYRIGHT
Definition: iff.c:52
#define AV_CH_LAYOUT_MONO
#define ID_BMHD
Definition: iff.c:45
This structure stores compressed data.
Definition: avcodec.h:898