Reindent after rr22977.
[libav.git] / libavformat / iff.c
CommitLineData
3a027f5e 1/*
4477dedc 2 * IFF (.iff) file demuxer
3a027f5e 3 * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
02c4f626 4 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
46dcabf8 5 * Copyright (c) 2010 Sebastian Vater <cdgs.basty@googlemail.com>
3a027f5e
JM
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24/**
ba87f080 25 * @file
4477dedc 26 * IFF file demuxer
3a027f5e
JM
27 * by Jaikrishnan Menon
28 * for more information on the .iff file format, visit:
29 * http://wiki.multimedia.cx/index.php?title=IFF
30 */
31
6a5d31ac 32#include "libavutil/intreadwrite.h"
d9747e29 33#include "libavcodec/iff.h"
3a027f5e
JM
34#include "avformat.h"
35
36#define ID_8SVX MKTAG('8','S','V','X')
37#define ID_VHDR MKTAG('V','H','D','R')
38#define ID_ATAK MKTAG('A','T','A','K')
39#define ID_RLSE MKTAG('R','L','S','E')
40#define ID_CHAN MKTAG('C','H','A','N')
02c4f626
PR
41#define ID_PBM MKTAG('P','B','M',' ')
42#define ID_ILBM MKTAG('I','L','B','M')
43#define ID_BMHD MKTAG('B','M','H','D')
44#define ID_CMAP MKTAG('C','M','A','P')
3a027f5e
JM
45
46#define ID_FORM MKTAG('F','O','R','M')
47#define ID_ANNO MKTAG('A','N','N','O')
48#define ID_AUTH MKTAG('A','U','T','H')
49#define ID_CHRS MKTAG('C','H','R','S')
50#define ID_COPYRIGHT MKTAG('(','c',')',' ')
51#define ID_CSET MKTAG('C','S','E','T')
52#define ID_FVER MKTAG('F','V','E','R')
53#define ID_NAME MKTAG('N','A','M','E')
54#define ID_TEXT MKTAG('T','E','X','T')
55#define ID_BODY MKTAG('B','O','D','Y')
899605f1 56#define ID_ANNO MKTAG('A','N','N','O')
3a027f5e
JM
57
58#define LEFT 2
59#define RIGHT 4
60#define STEREO 6
61
62#define PACKET_SIZE 1024
63
e5953464
SV
64typedef enum {
65 COMP_NONE,
66 COMP_FIB,
67 COMP_EXP
68} svx8_compression_type;
69
70typedef enum {
71 BITMAP_RAW,
72 BITMAP_BYTERUN1
73} bitmap_compression_type;
3a027f5e
JM
74
75typedef struct {
46dcabf8 76 uint64_t body_pos;
3a027f5e
JM
77 uint32_t body_size;
78 uint32_t sent_bytes;
79 uint32_t audio_frame_count;
80} IffDemuxContext;
81
86b2d47f
JM
82
83static void interleave_stereo(const uint8_t *src, uint8_t *dest, int size)
84{
85 uint8_t *end = dest + size;
86 size = size>>1;
87
88 while(dest < end) {
89 *dest++ = *src;
90 *dest++ = *(src+size);
91 src++;
92 }
93}
94
3a027f5e
JM
95static int iff_probe(AVProbeData *p)
96{
97 const uint8_t *d = p->buf;
98
99 if ( AV_RL32(d) == ID_FORM &&
02c4f626 100 (AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) )
3a027f5e
JM
101 return AVPROBE_SCORE_MAX;
102 return 0;
103}
104
105static int iff_read_header(AVFormatContext *s,
106 AVFormatParameters *ap)
107{
108 IffDemuxContext *iff = s->priv_data;
109 ByteIOContext *pb = s->pb;
110 AVStream *st;
111 uint32_t chunk_id, data_size;
02c4f626 112 int compression = -1;
899605f1 113 char *buf;
3a027f5e
JM
114
115 st = av_new_stream(s, 0);
116 if (!st)
4f5e6ef1 117 return AVERROR(ENOMEM);
3a027f5e
JM
118
119 st->codec->channels = 1;
02c4f626
PR
120 url_fskip(pb, 8);
121 // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
122 st->codec->codec_tag = get_le32(pb);
3a027f5e 123
46dcabf8
SV
124 while(!url_feof(pb)) {
125 uint64_t orig_pos;
3a027f5e
JM
126 chunk_id = get_le32(pb);
127 data_size = get_be32(pb);
46dcabf8 128 orig_pos = url_ftell(pb);
3a027f5e
JM
129
130 switch(chunk_id) {
131 case ID_VHDR:
72415b2a 132 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
46dcabf8
SV
133
134 if (data_size < 14)
135 return AVERROR_INVALIDDATA;
3a027f5e
JM
136 url_fskip(pb, 12);
137 st->codec->sample_rate = get_be16(pb);
46dcabf8 138 if (data_size >= 16) {
4f5e6ef1
SV
139 url_fskip(pb, 1);
140 compression = get_byte(pb);
46dcabf8 141 }
3a027f5e
JM
142 break;
143
144 case ID_BODY:
46dcabf8 145 iff->body_pos = url_ftell(pb);
3a027f5e 146 iff->body_size = data_size;
3a027f5e
JM
147 break;
148
149 case ID_CHAN:
46dcabf8
SV
150 if (data_size < 4)
151 return AVERROR_INVALIDDATA;
3a027f5e
JM
152 st->codec->channels = (get_be32(pb) < 6) ? 1 : 2;
153 break;
154
02c4f626
PR
155 case ID_CMAP:
156 st->codec->extradata_size = data_size;
157 st->codec->extradata = av_malloc(data_size);
158 if (!st->codec->extradata)
159 return AVERROR(ENOMEM);
160 if (get_buffer(pb, st->codec->extradata, data_size) < 0)
161 return AVERROR(EIO);
162 break;
163
164 case ID_BMHD:
72415b2a 165 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
46dcabf8
SV
166 if (data_size <= 8)
167 return AVERROR_INVALIDDATA;
02c4f626
PR
168 st->codec->width = get_be16(pb);
169 st->codec->height = get_be16(pb);
170 url_fskip(pb, 4); // x, y offset
171 st->codec->bits_per_coded_sample = get_byte(pb);
46dcabf8 172 if (data_size >= 11) {
4f5e6ef1
SV
173 url_fskip(pb, 1); // masking
174 compression = get_byte(pb);
46dcabf8
SV
175 }
176 if (data_size >= 16) {
4f5e6ef1
SV
177 url_fskip(pb, 3); // paddding, transparent
178 st->sample_aspect_ratio.num = get_byte(pb);
179 st->sample_aspect_ratio.den = get_byte(pb);
46dcabf8 180 }
02c4f626
PR
181 break;
182
899605f1
PR
183 case ID_ANNO:
184 buf = av_malloc(data_size + 1);
185 if (!buf)
186 break;
187 get_buffer(pb, buf, data_size);
188 buf[data_size] = 0;
189 av_metadata_set2(&s->metadata, "comment", buf, AV_METADATA_DONT_STRDUP_VAL);
190 break;
3a027f5e 191 }
46dcabf8
SV
192
193 url_fskip(pb, data_size - (url_ftell(pb) - orig_pos) + (data_size & 1));
3a027f5e
JM
194 }
195
46dcabf8
SV
196 url_fseek(pb, iff->body_pos, SEEK_SET);
197
02c4f626 198 switch(st->codec->codec_type) {
72415b2a 199 case AVMEDIA_TYPE_AUDIO:
e5953464 200 av_set_pts_info(st, 32, 1, st->codec->sample_rate);
3a027f5e 201
e5953464
SV
202 switch(compression) {
203 case COMP_NONE:
204 st->codec->codec_id = CODEC_ID_PCM_S8;
205 break;
206 case COMP_FIB:
207 st->codec->codec_id = CODEC_ID_8SVX_FIB;
208 break;
209 case COMP_EXP:
210 st->codec->codec_id = CODEC_ID_8SVX_EXP;
211 break;
212 default:
213 av_log(s, AV_LOG_ERROR, "iff: unknown compression method\n");
214 return -1;
215 }
3a027f5e 216
e5953464
SV
217 st->codec->bits_per_coded_sample = 8;
218 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
219 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
220 break;
02c4f626 221
72415b2a 222 case AVMEDIA_TYPE_VIDEO:
02c4f626
PR
223 switch (compression) {
224 case BITMAP_RAW:
225 if (st->codec->codec_tag == ID_ILBM) {
226 st->codec->codec_id = CODEC_ID_IFF_ILBM;
227 } else {
228 st->codec->codec_id = CODEC_ID_RAWVIDEO;
229 st->codec->pix_fmt = PIX_FMT_PAL8;
230 st->codec->codec_tag = 0;
231 }
232 break;
233 case BITMAP_BYTERUN1:
234 st->codec->codec_id = CODEC_ID_IFF_BYTERUN1;
235 break;
236 default:
237 av_log(s, AV_LOG_ERROR, "unknown compression method\n");
238 return AVERROR_INVALIDDATA;
239 }
240 break;
241 default:
242 return -1;
243 }
3a027f5e
JM
244
245 return 0;
246}
247
248static int iff_read_packet(AVFormatContext *s,
249 AVPacket *pkt)
250{
251 IffDemuxContext *iff = s->priv_data;
252 ByteIOContext *pb = s->pb;
02c4f626 253 AVStream *st = s->streams[0];
3a027f5e
JM
254 int ret;
255
02c4f626 256 if(iff->sent_bytes >= iff->body_size)
3a027f5e 257 return AVERROR(EIO);
86b2d47f
JM
258
259 if(s->streams[0]->codec->channels == 2) {
260 uint8_t sample_buffer[PACKET_SIZE];
261
262 ret = get_buffer(pb, sample_buffer, PACKET_SIZE);
263 if(av_new_packet(pkt, PACKET_SIZE) < 0) {
264 av_log(s, AV_LOG_ERROR, "iff: cannot allocate packet \n");
265 return AVERROR(ENOMEM);
266 }
267 interleave_stereo(sample_buffer, pkt->data, PACKET_SIZE);
02c4f626
PR
268 } else if (s->streams[0]->codec->codec_id == CODEC_ID_RAWVIDEO) {
269 if(av_new_packet(pkt, iff->body_size + AVPALETTE_SIZE) < 0) {
270 return AVERROR(ENOMEM);
271 }
272
273 ret = ff_cmap_read_palette(st->codec, (uint32_t*)(pkt->data + iff->body_size));
274 if (ret < 0)
275 return ret;
276 av_freep(&st->codec->extradata);
277 st->codec->extradata_size = 0;
278
279 ret = get_buffer(pb, pkt->data, iff->body_size);
72415b2a 280 } else if (s->streams[0]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
02c4f626
PR
281 ret = av_get_packet(pb, pkt, iff->body_size);
282 } else {
86b2d47f
JM
283 ret = av_get_packet(pb, pkt, PACKET_SIZE);
284 }
3a027f5e
JM
285
286 if(iff->sent_bytes == 0)
cc947f04 287 pkt->flags |= AV_PKT_FLAG_KEY;
3a027f5e 288
72415b2a 289 if(s->streams[0]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
451b353d 290 iff->sent_bytes += PACKET_SIZE;
02c4f626
PR
291 } else {
292 iff->sent_bytes = iff->body_size;
293 }
3a027f5e 294 pkt->stream_index = 0;
72415b2a 295 if(s->streams[0]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
451b353d
PR
296 pkt->pts = iff->audio_frame_count;
297 iff->audio_frame_count += ret / s->streams[0]->codec->channels;
02c4f626 298 }
3a027f5e
JM
299 return ret;
300}
301
302AVInputFormat iff_demuxer = {
303 "IFF",
bde15e74 304 NULL_IF_CONFIG_SMALL("IFF format"),
3a027f5e
JM
305 sizeof(IffDemuxContext),
306 iff_probe,
307 iff_read_header,
308 iff_read_packet,
309};