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 
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/dict.h"
34 #include "avformat.h"
35 #include "internal.h"
36 
37 #define ID_8SVX MKTAG('8','S','V','X')
38 #define ID_VHDR MKTAG('V','H','D','R')
39 #define ID_ATAK MKTAG('A','T','A','K')
40 #define ID_RLSE MKTAG('R','L','S','E')
41 #define ID_CHAN MKTAG('C','H','A','N')
42 #define ID_PBM MKTAG('P','B','M',' ')
43 #define ID_ILBM MKTAG('I','L','B','M')
44 #define ID_BMHD MKTAG('B','M','H','D')
45 #define ID_CMAP MKTAG('C','M','A','P')
46 
47 #define ID_FORM MKTAG('F','O','R','M')
48 #define ID_ANNO MKTAG('A','N','N','O')
49 #define ID_AUTH MKTAG('A','U','T','H')
50 #define ID_CHRS MKTAG('C','H','R','S')
51 #define ID_COPYRIGHT MKTAG('(','c',')',' ')
52 #define ID_CSET MKTAG('C','S','E','T')
53 #define ID_FVER MKTAG('F','V','E','R')
54 #define ID_NAME MKTAG('N','A','M','E')
55 #define ID_TEXT MKTAG('T','E','X','T')
56 #define ID_BODY MKTAG('B','O','D','Y')
57 #define ID_ANNO MKTAG('A','N','N','O')
58 
59 #define LEFT 2
60 #define RIGHT 4
61 #define STEREO 6
62 
63 typedef enum {
68 
69 typedef enum {
73 
74 typedef struct {
75  uint64_t body_pos;
76  uint32_t body_size;
77  uint32_t sent_bytes;
79 
80 
81 /* Metadata string read */
83  const char *const tag,
84  const unsigned data_size)
85 {
86  uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
87 
88  if (!buf)
89  return AVERROR(ENOMEM);
90 
91  if (avio_read(s->pb, buf, data_size) < 0) {
92  av_free(buf);
93  return AVERROR(EIO);
94  }
95  buf[data_size] = 0;
97  return 0;
98 }
99 
100 static int iff_probe(AVProbeData *p)
101 {
102  const uint8_t *d = p->buf;
103 
104  if ( AV_RL32(d) == ID_FORM &&
105  (AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) )
106  return AVPROBE_SCORE_MAX;
107  return 0;
108 }
109 
111  AVFormatParameters *ap)
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;
124  avio_skip(pb, 8);
125  // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
126  st->codec->codec_tag = avio_rl32(pb);
127 
128  while(!pb->eof_reached) {
129  uint64_t orig_pos;
130  int res;
131  const char *metadata_tag = NULL;
132  chunk_id = avio_rl32(pb);
133  data_size = avio_rb32(pb);
134  orig_pos = avio_tell(pb);
135 
136  switch(chunk_id) {
137  case ID_VHDR:
139 
140  if (data_size < 14)
141  return AVERROR_INVALIDDATA;
142  avio_skip(pb, 12);
143  st->codec->sample_rate = avio_rb16(pb);
144  if (data_size >= 16) {
145  avio_skip(pb, 1);
146  compression = avio_r8(pb);
147  }
148  break;
149 
150  case ID_BODY:
151  iff->body_pos = avio_tell(pb);
152  iff->body_size = data_size;
153  break;
154 
155  case ID_CHAN:
156  if (data_size < 4)
157  return AVERROR_INVALIDDATA;
158  st->codec->channels = (avio_rb32(pb) < 6) ? 1 : 2;
159  break;
160 
161  case ID_CMAP:
162  if (data_size < 3 || data_size > 768 || data_size % 3) {
163  av_log(s, AV_LOG_ERROR, "Invalid CMAP chunk size %d\n",
164  data_size);
165  return AVERROR_INVALIDDATA;
166  }
167  st->codec->extradata_size = data_size;
168  st->codec->extradata = av_malloc(data_size);
169  if (!st->codec->extradata)
170  return AVERROR(ENOMEM);
171  if (avio_read(pb, st->codec->extradata, data_size) < 0)
172  return AVERROR(EIO);
173  break;
174 
175  case ID_BMHD:
177  if (data_size <= 8)
178  return AVERROR_INVALIDDATA;
179  st->codec->width = avio_rb16(pb);
180  st->codec->height = avio_rb16(pb);
181  avio_skip(pb, 4); // x, y offset
183  if (data_size >= 11) {
184  avio_skip(pb, 1); // masking
185  compression = avio_r8(pb);
186  }
187  if (data_size >= 16) {
188  avio_skip(pb, 3); // paddding, transparent
189  st->sample_aspect_ratio.num = avio_r8(pb);
190  st->sample_aspect_ratio.den = avio_r8(pb);
191  }
192  break;
193 
194  case ID_ANNO:
195  case ID_TEXT:
196  metadata_tag = "comment";
197  break;
198 
199  case ID_AUTH:
200  metadata_tag = "artist";
201  break;
202 
203  case ID_COPYRIGHT:
204  metadata_tag = "copyright";
205  break;
206 
207  case ID_NAME:
208  metadata_tag = "title";
209  break;
210  }
211 
212  if (metadata_tag) {
213  if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
214  av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!", metadata_tag);
215  return res;
216  }
217  }
218  avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1));
219  }
220 
221  avio_seek(pb, iff->body_pos, SEEK_SET);
222 
223  switch(st->codec->codec_type) {
224  case AVMEDIA_TYPE_AUDIO:
225  avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
226 
227  switch(compression) {
228  case COMP_NONE:
230  break;
231  case COMP_FIB:
232  st->codec->codec_id = CODEC_ID_8SVX_FIB;
233  break;
234  case COMP_EXP:
235  st->codec->codec_id = CODEC_ID_8SVX_EXP;
236  break;
237  default:
238  av_log(s, AV_LOG_ERROR, "unknown compression method\n");
239  return -1;
240  }
241 
242  st->codec->bits_per_coded_sample = 8;
245  break;
246 
247  case AVMEDIA_TYPE_VIDEO:
248  switch (compression) {
249  case BITMAP_RAW:
251  break;
252  case BITMAP_BYTERUN1:
254  break;
255  default:
256  av_log(s, AV_LOG_ERROR, "unknown compression method\n");
257  return AVERROR_INVALIDDATA;
258  }
259  break;
260  default:
261  return -1;
262  }
263 
264  return 0;
265 }
266 
268  AVPacket *pkt)
269 {
270  IffDemuxContext *iff = s->priv_data;
271  AVIOContext *pb = s->pb;
272  int ret;
273 
274  if(iff->sent_bytes >= iff->body_size)
275  return AVERROR_EOF;
276 
277  ret = av_get_packet(pb, pkt, iff->body_size);
278  if (ret < 0)
279  return ret;
280 
281  if(iff->sent_bytes == 0)
282  pkt->flags |= AV_PKT_FLAG_KEY;
283  iff->sent_bytes = iff->body_size;
284 
285  pkt->stream_index = 0;
286  return ret;
287 }
288 
290  .name = "IFF",
291  .long_name = NULL_IF_CONFIG_SMALL("IFF format"),
292  .priv_data_size = sizeof(IffDemuxContext),
296 };
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
#define ID_PBM
Definition: iff.c:42
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the pts for a given stream.
Definition: utils.c:3828
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:716
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:211
Definition: iff.c:70
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:954
#define ID_8SVX
Definition: iff.c:37
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:754
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1751
Format I/O context.
Definition: avformat.h:863
Public dictionary API.
#define ID_ANNO
Definition: iff.c:57
#define ID_NAME
Definition: iff.c:54
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:769
uint32_t body_size
Definition: iff.c:76
static int iff_probe(AVProbeData *p)
Definition: iff.c:100
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1387
Definition: iff.c:65
static int read_header(FFV1Context *f)
Definition: ffv1.c:1513
uint32_t tag
Definition: movenc.c:670
#define AVERROR_EOF
End of file.
Definition: error.h:51
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:269
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:492
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1974
#define ID_CHAN
Definition: iff.c:41
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:652
#define ID_TEXT
Definition: iff.c:55
AVDictionary * metadata
Definition: avformat.h:1085
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:137
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:738
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
AV_RL32
Definition: bytestream.h:89
#define ID_FORM
Definition: iff.c:47
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2776
#define ID_ILBM
Definition: iff.c:43
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:914
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:632
AVCodecContext * codec
codec context
Definition: avformat.h:623
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:341
#define ID_VHDR
Definition: iff.c:38
int bit_rate
the average bitrate
Definition: avcodec.h:1340
Definition: iff.c:66
#define ID_BODY
Definition: iff.c:56
AVInputFormat ff_iff_demuxer
Definition: iff.c:289
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() and chilren.
Definition: dict.h:66
int width
picture width / height.
Definition: avcodec.h:1408
#define ID_AUTH
Definition: iff.c:49
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:483
static int read_probe(AVProbeData *p)
Definition: img2.c:185
uint32_t sent_bytes
Definition: iff.c:77
static int iff_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: iff.c:267
Stream structure.
Definition: avformat.h:620
NULL
Definition: eval.c:50
svx8_compression_type
Definition: iff.c:63
uint64_t body_pos
Definition: iff.c:75
enum AVMediaType codec_type
Definition: avcodec.h:1574
int sample_rate
samples per second
Definition: avcodec.h:1456
AVIOContext * pb
Definition: avformat.h:896
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1590
static int iff_read_header(AVFormatContext *s, AVFormatParameters *ap)
Definition: iff.c:110
int extradata_size
Definition: avcodec.h:1388
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:109
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:50
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:64
This structure contains the data a format has to probe a file.
Definition: avformat.h:339
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:345
Main libavformat public API header.
#define ID_CMAP
Definition: iff.c:45
int den
denominator
Definition: rational.h:45
Definition: iff.c:64
bitmap_compression_type
Definition: iff.c:69
int eof_reached
true if eof reached
Definition: avio.h:98
int channels
number of audio channels
Definition: avcodec.h:1457
void * priv_data
Format private data.
Definition: avformat.h:883
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:460
static int get_metadata(AVFormatContext *s, const char *const tag, const unsigned data_size)
Definition: iff.c:82
int stream_index
Definition: avcodec.h:910
#define ID_COPYRIGHT
Definition: iff.c:51
#define ID_BMHD
Definition: iff.c:44
enum CodecID codec_id
Definition: avcodec.h:1575