Libav
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 <inttypes.h>
33 
35 #include "libavutil/intreadwrite.h"
36 #include "libavutil/dict.h"
37 #include "avformat.h"
38 #include "internal.h"
39 
40 #define ID_8SVX MKTAG('8','S','V','X')
41 #define ID_VHDR MKTAG('V','H','D','R')
42 #define ID_ATAK MKTAG('A','T','A','K')
43 #define ID_RLSE MKTAG('R','L','S','E')
44 #define ID_CHAN MKTAG('C','H','A','N')
45 #define ID_PBM MKTAG('P','B','M',' ')
46 #define ID_ILBM MKTAG('I','L','B','M')
47 #define ID_BMHD MKTAG('B','M','H','D')
48 #define ID_CMAP MKTAG('C','M','A','P')
49 
50 #define ID_FORM MKTAG('F','O','R','M')
51 #define ID_ANNO MKTAG('A','N','N','O')
52 #define ID_AUTH MKTAG('A','U','T','H')
53 #define ID_CHRS MKTAG('C','H','R','S')
54 #define ID_COPYRIGHT MKTAG('(','c',')',' ')
55 #define ID_CSET MKTAG('C','S','E','T')
56 #define ID_FVER MKTAG('F','V','E','R')
57 #define ID_NAME MKTAG('N','A','M','E')
58 #define ID_TEXT MKTAG('T','E','X','T')
59 #define ID_BODY MKTAG('B','O','D','Y')
60 #define ID_ANNO MKTAG('A','N','N','O')
61 
62 #define LEFT 2
63 #define RIGHT 4
64 #define STEREO 6
65 
66 typedef enum {
71 
72 typedef enum {
76 
77 typedef struct {
78  uint64_t body_pos;
79  uint32_t body_size;
80  uint32_t sent_bytes;
82 
83 
84 /* Metadata string read */
86  const char *const tag,
87  const unsigned data_size)
88 {
89  uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
90 
91  if (!buf)
92  return AVERROR(ENOMEM);
93 
94  if (avio_read(s->pb, buf, data_size) < 0) {
95  av_free(buf);
96  return AVERROR(EIO);
97  }
98  buf[data_size] = 0;
100  return 0;
101 }
102 
103 static int iff_probe(AVProbeData *p)
104 {
105  const uint8_t *d = p->buf;
106 
107  if ( AV_RL32(d) == ID_FORM &&
108  (AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) )
109  return AVPROBE_SCORE_MAX;
110  return 0;
111 }
112 
114 {
115  IffDemuxContext *iff = s->priv_data;
116  AVIOContext *pb = s->pb;
117  AVStream *st;
118  uint32_t chunk_id, data_size;
119  int compression = -1;
120 
121  st = avformat_new_stream(s, NULL);
122  if (!st)
123  return AVERROR(ENOMEM);
124 
125  st->codec->channels = 1;
127  avio_skip(pb, 8);
128  // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
129  st->codec->codec_tag = avio_rl32(pb);
130 
131  while(!pb->eof_reached) {
132  uint64_t orig_pos;
133  int res;
134  const char *metadata_tag = NULL;
135  chunk_id = avio_rl32(pb);
136  data_size = avio_rb32(pb);
137  orig_pos = avio_tell(pb);
138 
139  switch(chunk_id) {
140  case ID_VHDR:
142 
143  if (data_size < 14)
144  return AVERROR_INVALIDDATA;
145  avio_skip(pb, 12);
146  st->codec->sample_rate = avio_rb16(pb);
147  if (data_size >= 16) {
148  avio_skip(pb, 1);
149  compression = avio_r8(pb);
150  }
151  break;
152 
153  case ID_BODY:
154  iff->body_pos = avio_tell(pb);
155  iff->body_size = data_size;
156  break;
157 
158  case ID_CHAN:
159  if (data_size < 4)
160  return AVERROR_INVALIDDATA;
161  if (avio_rb32(pb) < 6) {
162  st->codec->channels = 1;
164  } else {
165  st->codec->channels = 2;
167  }
168  break;
169 
170  case ID_CMAP:
171  if (data_size < 3 || data_size > 768 || data_size % 3) {
172  av_log(s, AV_LOG_ERROR, "Invalid CMAP chunk size %"PRIu32"\n",
173  data_size);
174  return AVERROR_INVALIDDATA;
175  }
176  st->codec->extradata_size = data_size;
177  st->codec->extradata = av_malloc(data_size);
178  if (!st->codec->extradata)
179  return AVERROR(ENOMEM);
180  if (avio_read(pb, st->codec->extradata, data_size) < 0)
181  return AVERROR(EIO);
182  break;
183 
184  case ID_BMHD:
186  if (data_size <= 8)
187  return AVERROR_INVALIDDATA;
188  st->codec->width = avio_rb16(pb);
189  st->codec->height = avio_rb16(pb);
190  avio_skip(pb, 4); // x, y offset
192  if (data_size >= 11) {
193  avio_skip(pb, 1); // masking
194  compression = avio_r8(pb);
195  }
196  if (data_size >= 16) {
197  avio_skip(pb, 3); // paddding, transparent
198  st->sample_aspect_ratio.num = avio_r8(pb);
199  st->sample_aspect_ratio.den = avio_r8(pb);
200  }
201  break;
202 
203  case ID_ANNO:
204  case ID_TEXT:
205  metadata_tag = "comment";
206  break;
207 
208  case ID_AUTH:
209  metadata_tag = "artist";
210  break;
211 
212  case ID_COPYRIGHT:
213  metadata_tag = "copyright";
214  break;
215 
216  case ID_NAME:
217  metadata_tag = "title";
218  break;
219  }
220 
221  if (metadata_tag) {
222  if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
223  av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!", metadata_tag);
224  return res;
225  }
226  }
227  avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1));
228  }
229 
230  avio_seek(pb, iff->body_pos, SEEK_SET);
231 
232  switch(st->codec->codec_type) {
233  case AVMEDIA_TYPE_AUDIO:
234  avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
235 
236  switch(compression) {
237  case COMP_NONE:
239  break;
240  case COMP_FIB:
242  break;
243  case COMP_EXP:
245  break;
246  default:
247  av_log(s, AV_LOG_ERROR, "unknown compression method\n");
248  return -1;
249  }
250 
251  st->codec->bits_per_coded_sample = 8;
254  break;
255 
256  case AVMEDIA_TYPE_VIDEO:
257  switch (compression) {
258  case BITMAP_RAW:
260  break;
261  case BITMAP_BYTERUN1:
263  break;
264  default:
265  av_log(s, AV_LOG_ERROR, "unknown compression method\n");
266  return AVERROR_INVALIDDATA;
267  }
268  break;
269  default:
270  return -1;
271  }
272 
273  return 0;
274 }
275 
277  AVPacket *pkt)
278 {
279  IffDemuxContext *iff = s->priv_data;
280  AVIOContext *pb = s->pb;
281  int ret;
282 
283  if(iff->sent_bytes >= iff->body_size)
284  return AVERROR_EOF;
285 
286  ret = av_get_packet(pb, pkt, iff->body_size);
287  if (ret < 0)
288  return ret;
289 
290  if(iff->sent_bytes == 0)
291  pkt->flags |= AV_PKT_FLAG_KEY;
292  iff->sent_bytes = iff->body_size;
293 
294  pkt->stream_index = 0;
295  return ret;
296 }
297 
299  .name = "iff",
300  .long_name = NULL_IF_CONFIG_SMALL("IFF (Interchange File Format)"),
301  .priv_data_size = sizeof(IffDemuxContext),
305 };
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:62
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:45
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:2829
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:769
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:73
#define ID_8SVX
Definition: iff.c:40
#define AV_CH_LAYOUT_STEREO
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:580
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1844
Format I/O context.
Definition: avformat.h:922
Public dictionary API.
#define ID_ANNO
Definition: iff.c:60
uint8_t
#define ID_NAME
Definition: iff.c:57
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:595
uint32_t body_size
Definition: iff.c:79
static int iff_probe(AVProbeData *p)
Definition: iff.c:103
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1164
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
Definition: iff.c:68
uint32_t tag
Definition: movenc.c:844
#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:117
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:2523
#define ID_CHAN
Definition: iff.c:44
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:452
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
static int iff_read_header(AVFormatContext *s)
Definition: iff.c:113
#define ID_TEXT
Definition: iff.c:58
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1130
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:564
#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:145
#define ID_FORM
Definition: iff.c:50
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
#define ID_ILBM
Definition: iff.c:46
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1868
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:443
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
#define ID_VHDR
Definition: iff.c:41
int bit_rate
the average bitrate
Definition: avcodec.h:1114
audio channel layout utility functions
Definition: iff.c:69
#define ID_BODY
Definition: iff.c:59
AVInputFormat ff_iff_demuxer
Definition: iff.c:298
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that&#39;s been allocated with av_malloc() and chilren.
Definition: dict.h:66
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1229
#define ID_AUTH
Definition: iff.c:52
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:80
static int iff_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: iff.c:276
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:544
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
svx8_compression_type
Definition: iff.c:66
uint64_t body_pos
Definition: iff.c:78
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
int sample_rate
samples per second
Definition: avcodec.h:1807
AVIOContext * pb
I/O context.
Definition: avformat.h:964
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
Definition: avcodec.h:1082
int extradata_size
Definition: avcodec.h:1165
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:68
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:404
Main libavformat public API header.
#define ID_CMAP
Definition: iff.c:48
int den
denominator
Definition: rational.h:45
Definition: iff.c:67
bitmap_compression_type
Definition: iff.c:72
int eof_reached
true if eof reached
Definition: avio.h:96
int channels
number of audio channels
Definition: avcodec.h:1808
void * priv_data
Format private data.
Definition: avformat.h:950
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
static int get_metadata(AVFormatContext *s, const char *const tag, const unsigned data_size)
Definition: iff.c:85
int stream_index
Definition: avcodec.h:975
#define ID_COPYRIGHT
Definition: iff.c:54
#define AV_CH_LAYOUT_MONO
#define ID_BMHD
Definition: iff.c:47
This structure stores compressed data.
Definition: avcodec.h:950