4
4
* Copyright (c) 2010 Peter Ross <pross@xvid.org>
5
5
* Copyright (c) 2010 Sebastian Vater <cdgs.basty@googlemail.com>
7
* This file is part of FFmpeg.
7
* This file is part of Libav.
9
* FFmpeg is free software; you can redistribute it and/or
9
* Libav is free software; you can redistribute it and/or
10
10
* modify it under the terms of the GNU Lesser General Public
11
11
* License as published by the Free Software Foundation; either
12
12
* version 2.1 of the License, or (at your option) any later version.
14
* FFmpeg is distributed in the hope that it will be useful,
14
* Libav is distributed in the hope that it will be useful,
15
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
17
* Lesser General Public License for more details.
19
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
20
* License along with Libav; if not, write to the Free Software
21
21
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
94
/* Metadata string read */
95
static int get_metadata(AVFormatContext *s,
96
const char *const tag,
97
const unsigned data_size)
99
uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
102
return AVERROR(ENOMEM);
104
if (avio_read(s->pb, buf, data_size) < 0) {
109
av_metadata_set2(&s->metadata, tag, buf, AV_METADATA_DONT_STRDUP_VAL);
95
113
static int iff_probe(AVProbeData *p)
97
115
const uint8_t *d = p->buf;
106
124
AVFormatParameters *ap)
108
126
IffDemuxContext *iff = s->priv_data;
109
ByteIOContext *pb = s->pb;
127
AVIOContext *pb = s->pb;
111
129
uint32_t chunk_id, data_size;
112
130
int compression = -1;
115
132
st = av_new_stream(s, 0);
117
134
return AVERROR(ENOMEM);
119
136
st->codec->channels = 1;
121
138
// codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
122
st->codec->codec_tag = get_le32(pb);
139
st->codec->codec_tag = avio_rl32(pb);
124
while(!url_feof(pb)) {
141
while(!pb->eof_reached) {
125
142
uint64_t orig_pos;
126
chunk_id = get_le32(pb);
127
data_size = get_be32(pb);
128
orig_pos = url_ftell(pb);
144
const char *metadata_tag = NULL;
145
chunk_id = avio_rl32(pb);
146
data_size = avio_rb32(pb);
147
orig_pos = avio_tell(pb);
130
149
switch(chunk_id) {
134
153
if (data_size < 14)
135
154
return AVERROR_INVALIDDATA;
137
st->codec->sample_rate = get_be16(pb);
156
st->codec->sample_rate = avio_rb16(pb);
138
157
if (data_size >= 16) {
140
compression = get_byte(pb);
159
compression = avio_r8(pb);
145
iff->body_pos = url_ftell(pb);
164
iff->body_pos = avio_tell(pb);
146
165
iff->body_size = data_size;
150
169
if (data_size < 4)
151
170
return AVERROR_INVALIDDATA;
152
st->codec->channels = (get_be32(pb) < 6) ? 1 : 2;
171
st->codec->channels = (avio_rb32(pb) < 6) ? 1 : 2;
157
176
st->codec->extradata = av_malloc(data_size);
158
177
if (!st->codec->extradata)
159
178
return AVERROR(ENOMEM);
160
if (get_buffer(pb, st->codec->extradata, data_size) < 0)
179
if (avio_read(pb, st->codec->extradata, data_size) < 0)
161
180
return AVERROR(EIO);
165
184
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
166
185
if (data_size <= 8)
167
186
return AVERROR_INVALIDDATA;
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);
187
st->codec->width = avio_rb16(pb);
188
st->codec->height = avio_rb16(pb);
189
avio_skip(pb, 4); // x, y offset
190
st->codec->bits_per_coded_sample = avio_r8(pb);
172
191
if (data_size >= 11) {
173
url_fskip(pb, 1); // masking
174
compression = get_byte(pb);
192
avio_skip(pb, 1); // masking
193
compression = avio_r8(pb);
176
195
if (data_size >= 16) {
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);
196
avio_skip(pb, 3); // paddding, transparent
197
st->sample_aspect_ratio.num = avio_r8(pb);
198
st->sample_aspect_ratio.den = avio_r8(pb);
184
buf = av_malloc(data_size + 1);
187
get_buffer(pb, buf, data_size);
189
av_metadata_set2(&s->metadata, "comment", buf, AV_METADATA_DONT_STRDUP_VAL);
193
url_fskip(pb, data_size - (url_ftell(pb) - orig_pos) + (data_size & 1));
204
metadata_tag = "comment";
208
metadata_tag = "artist";
212
metadata_tag = "copyright";
216
metadata_tag = "title";
221
if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
222
av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!", metadata_tag);
226
avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1));
196
url_fseek(pb, iff->body_pos, SEEK_SET);
229
avio_seek(pb, iff->body_pos, SEEK_SET);
198
231
switch(st->codec->codec_type) {
199
232
case AVMEDIA_TYPE_AUDIO:
222
255
case AVMEDIA_TYPE_VIDEO:
223
256
switch (compression) {
225
if (st->codec->codec_tag == ID_ILBM) {
226
st->codec->codec_id = CODEC_ID_IFF_ILBM;
228
st->codec->codec_id = CODEC_ID_RAWVIDEO;
229
st->codec->pix_fmt = PIX_FMT_PAL8;
230
st->codec->codec_tag = 0;
258
st->codec->codec_id = CODEC_ID_IFF_ILBM;
233
260
case BITMAP_BYTERUN1:
234
261
st->codec->codec_id = CODEC_ID_IFF_BYTERUN1;
251
278
IffDemuxContext *iff = s->priv_data;
252
ByteIOContext *pb = s->pb;
279
AVIOContext *pb = s->pb;
253
280
AVStream *st = s->streams[0];
256
283
if(iff->sent_bytes >= iff->body_size)
257
284
return AVERROR(EIO);
259
if(s->streams[0]->codec->channels == 2) {
286
if(st->codec->channels == 2) {
260
287
uint8_t sample_buffer[PACKET_SIZE];
262
ret = get_buffer(pb, sample_buffer, PACKET_SIZE);
289
ret = avio_read(pb, sample_buffer, PACKET_SIZE);
263
290
if(av_new_packet(pkt, PACKET_SIZE) < 0) {
264
av_log(s, AV_LOG_ERROR, "iff: cannot allocate packet \n");
291
av_log(s, AV_LOG_ERROR, "cannot allocate packet\n");
265
292
return AVERROR(ENOMEM);
267
294
interleave_stereo(sample_buffer, pkt->data, PACKET_SIZE);
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);
273
ret = ff_cmap_read_palette(st->codec, (uint32_t*)(pkt->data + iff->body_size));
276
av_freep(&st->codec->extradata);
277
st->codec->extradata_size = 0;
279
ret = get_buffer(pb, pkt->data, iff->body_size);
280
} else if (s->streams[0]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
295
} else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
281
296
ret = av_get_packet(pb, pkt, iff->body_size);
283
298
ret = av_get_packet(pb, pkt, PACKET_SIZE);
286
301
if(iff->sent_bytes == 0)
287
302
pkt->flags |= AV_PKT_FLAG_KEY;
289
if(s->streams[0]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
304
if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
290
305
iff->sent_bytes += PACKET_SIZE;
292
307
iff->sent_bytes = iff->body_size;
294
309
pkt->stream_index = 0;
295
if(s->streams[0]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
310
if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
296
311
pkt->pts = iff->audio_frame_count;
297
iff->audio_frame_count += ret / s->streams[0]->codec->channels;
312
iff->audio_frame_count += ret / st->codec->channels;
302
AVInputFormat iff_demuxer = {
317
AVInputFormat ff_iff_demuxer = {
304
319
NULL_IF_CONFIG_SMALL("IFF format"),
305
320
sizeof(IffDemuxContext),