Mercurial > libavformat.hg
annotate iff.c @ 5628:ee642c4e5b5c libavformat
Dont try generic seek if seek request before first index entry and backward.
Fixes issue1275
author | michael |
---|---|
date | Wed, 03 Feb 2010 23:59:48 +0000 |
parents | b3d4ef7e53ad |
children | 1701ad9b6064 |
rev | line source |
---|---|
3189 | 1 /* |
3198 | 2 * IFF (.iff) file demuxer |
3189 | 3 * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net> |
5625 | 4 * Copyright (c) 2010 Peter Ross <pross@xvid.org> |
3189 | 5 * |
6 * This file is part of FFmpeg. | |
7 * | |
8 * FFmpeg is free software; you can redistribute it and/or | |
9 * modify it under the terms of the GNU Lesser General Public | |
10 * License as published by the Free Software Foundation; either | |
11 * version 2.1 of the License, or (at your option) any later version. | |
12 * | |
13 * FFmpeg is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Lesser General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Lesser General Public | |
19 * License along with FFmpeg; if not, write to the Free Software | |
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 */ | |
22 | |
23 /** | |
4331
49c1d3b27727
Use full internal pathname in doxygen @file directives.
diego
parents:
4201
diff
changeset
|
24 * @file libavformat/iff.c |
3198 | 25 * IFF file demuxer |
3189 | 26 * by Jaikrishnan Menon |
27 * for more information on the .iff file format, visit: | |
28 * http://wiki.multimedia.cx/index.php?title=IFF | |
29 */ | |
30 | |
4201
7d2f3f1b68d8
Fix build: Add intreadwrite.h and bswap.h #includes where necessary.
diego
parents:
4090
diff
changeset
|
31 #include "libavutil/intreadwrite.h" |
3189 | 32 #include "avformat.h" |
33 | |
34 #define ID_8SVX MKTAG('8','S','V','X') | |
35 #define ID_VHDR MKTAG('V','H','D','R') | |
36 #define ID_ATAK MKTAG('A','T','A','K') | |
37 #define ID_RLSE MKTAG('R','L','S','E') | |
38 #define ID_CHAN MKTAG('C','H','A','N') | |
5625 | 39 #define ID_PBM MKTAG('P','B','M',' ') |
40 #define ID_ILBM MKTAG('I','L','B','M') | |
41 #define ID_BMHD MKTAG('B','M','H','D') | |
42 #define ID_CMAP MKTAG('C','M','A','P') | |
3189 | 43 |
44 #define ID_FORM MKTAG('F','O','R','M') | |
45 #define ID_ANNO MKTAG('A','N','N','O') | |
46 #define ID_AUTH MKTAG('A','U','T','H') | |
47 #define ID_CHRS MKTAG('C','H','R','S') | |
48 #define ID_COPYRIGHT MKTAG('(','c',')',' ') | |
49 #define ID_CSET MKTAG('C','S','E','T') | |
50 #define ID_FVER MKTAG('F','V','E','R') | |
51 #define ID_NAME MKTAG('N','A','M','E') | |
52 #define ID_TEXT MKTAG('T','E','X','T') | |
53 #define ID_BODY MKTAG('B','O','D','Y') | |
54 | |
55 #define LEFT 2 | |
56 #define RIGHT 4 | |
57 #define STEREO 6 | |
58 | |
59 #define PACKET_SIZE 1024 | |
60 | |
4090
a209d26d63c2
Avoid _t in identifier names, _t is reserved by POSIX.
diego
parents:
3908
diff
changeset
|
61 typedef enum {COMP_NONE, COMP_FIB, COMP_EXP} svx8_compression_type; |
5625 | 62 typedef enum {BITMAP_RAW, BITMAP_BYTERUN1} bitmap_compression_type; |
3189 | 63 |
64 typedef struct { | |
65 uint32_t body_size; | |
66 uint32_t sent_bytes; | |
67 uint32_t audio_frame_count; | |
68 } IffDemuxContext; | |
69 | |
3285 | 70 |
71 static void interleave_stereo(const uint8_t *src, uint8_t *dest, int size) | |
72 { | |
73 uint8_t *end = dest + size; | |
74 size = size>>1; | |
75 | |
76 while(dest < end) { | |
77 *dest++ = *src; | |
78 *dest++ = *(src+size); | |
79 src++; | |
80 } | |
81 } | |
82 | |
3189 | 83 static int iff_probe(AVProbeData *p) |
84 { | |
85 const uint8_t *d = p->buf; | |
86 | |
87 if ( AV_RL32(d) == ID_FORM && | |
5625 | 88 (AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) ) |
3189 | 89 return AVPROBE_SCORE_MAX; |
90 return 0; | |
91 } | |
92 | |
93 static int iff_read_header(AVFormatContext *s, | |
94 AVFormatParameters *ap) | |
95 { | |
96 IffDemuxContext *iff = s->priv_data; | |
97 ByteIOContext *pb = s->pb; | |
98 AVStream *st; | |
99 uint32_t chunk_id, data_size; | |
3200 | 100 int padding, done = 0; |
5625 | 101 int compression = -1; |
3189 | 102 |
103 st = av_new_stream(s, 0); | |
104 if (!st) | |
105 return AVERROR(ENOMEM); | |
106 | |
107 st->codec->channels = 1; | |
5625 | 108 url_fskip(pb, 8); |
109 // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content | |
110 st->codec->codec_tag = get_le32(pb); | |
3189 | 111 |
112 while(!done && !url_feof(pb)) { | |
113 chunk_id = get_le32(pb); | |
114 data_size = get_be32(pb); | |
115 padding = data_size & 1; | |
116 | |
117 switch(chunk_id) { | |
118 case ID_VHDR: | |
5625 | 119 st->codec->codec_type = CODEC_TYPE_AUDIO; |
3189 | 120 url_fskip(pb, 12); |
121 st->codec->sample_rate = get_be16(pb); | |
122 url_fskip(pb, 1); | |
5625 | 123 compression = get_byte(pb); |
3189 | 124 url_fskip(pb, 4); |
125 break; | |
126 | |
127 case ID_BODY: | |
128 iff->body_size = data_size; | |
129 done = 1; | |
130 break; | |
131 | |
132 case ID_CHAN: | |
133 st->codec->channels = (get_be32(pb) < 6) ? 1 : 2; | |
134 break; | |
135 | |
5625 | 136 case ID_CMAP: |
137 st->codec->extradata_size = data_size; | |
138 st->codec->extradata = av_malloc(data_size); | |
139 if (!st->codec->extradata) | |
140 return AVERROR(ENOMEM); | |
141 if (get_buffer(pb, st->codec->extradata, data_size) < 0) | |
142 return AVERROR(EIO); | |
143 break; | |
144 | |
145 case ID_BMHD: | |
146 st->codec->codec_type = CODEC_TYPE_VIDEO; | |
147 st->codec->width = get_be16(pb); | |
148 st->codec->height = get_be16(pb); | |
149 url_fskip(pb, 4); // x, y offset | |
150 st->codec->bits_per_coded_sample = get_byte(pb); | |
151 url_fskip(pb, 1); // masking | |
152 compression = get_byte(pb); | |
153 url_fskip(pb, 3); // paddding, transparent | |
154 st->sample_aspect_ratio.num = get_byte(pb); | |
155 st->sample_aspect_ratio.den = get_byte(pb); | |
156 url_fskip(pb, 4); // source page width, height | |
157 break; | |
158 | |
3189 | 159 default: |
160 url_fseek(pb, data_size + padding, SEEK_CUR); | |
161 break; | |
162 } | |
163 } | |
164 | |
5625 | 165 switch(st->codec->codec_type) { |
166 case CODEC_TYPE_AUDIO: | |
3189 | 167 av_set_pts_info(st, 32, 1, st->codec->sample_rate); |
168 | |
5625 | 169 switch(compression) { |
3189 | 170 case COMP_NONE: |
171 st->codec->codec_id = CODEC_ID_PCM_S8; | |
172 break; | |
173 case COMP_FIB: | |
174 st->codec->codec_id = CODEC_ID_8SVX_FIB; | |
175 break; | |
176 case COMP_EXP: | |
177 st->codec->codec_id = CODEC_ID_8SVX_EXP; | |
178 break; | |
179 default: | |
180 av_log(s, AV_LOG_ERROR, "iff: unknown compression method\n"); | |
181 return -1; | |
182 } | |
183 | |
3908
1d3d17de20ba
Bump Major version, this commit is almost just renaming bits_per_sample to
michael
parents:
3424
diff
changeset
|
184 st->codec->bits_per_coded_sample = 8; |
1d3d17de20ba
Bump Major version, this commit is almost just renaming bits_per_sample to
michael
parents:
3424
diff
changeset
|
185 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample; |
1d3d17de20ba
Bump Major version, this commit is almost just renaming bits_per_sample to
michael
parents:
3424
diff
changeset
|
186 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample; |
5625 | 187 break; |
188 | |
189 case CODEC_TYPE_VIDEO: | |
190 switch (compression) { | |
191 case BITMAP_RAW: | |
192 if (st->codec->codec_tag == ID_ILBM) { | |
193 st->codec->codec_id = CODEC_ID_IFF_ILBM; | |
194 } else { | |
195 st->codec->codec_id = CODEC_ID_RAWVIDEO; | |
196 st->codec->pix_fmt = PIX_FMT_PAL8; | |
197 st->codec->codec_tag = 0; | |
198 } | |
199 break; | |
200 case BITMAP_BYTERUN1: | |
201 st->codec->codec_id = CODEC_ID_IFF_BYTERUN1; | |
202 break; | |
203 default: | |
204 av_log(s, AV_LOG_ERROR, "unknown compression method\n"); | |
205 return AVERROR_INVALIDDATA; | |
206 } | |
207 break; | |
208 default: | |
209 return -1; | |
210 } | |
3189 | 211 |
212 return 0; | |
213 } | |
214 | |
5625 | 215 int ff_cmap_read_palette(AVCodecContext *avctx, uint32_t *pal); |
216 | |
3189 | 217 static int iff_read_packet(AVFormatContext *s, |
218 AVPacket *pkt) | |
219 { | |
220 IffDemuxContext *iff = s->priv_data; | |
221 ByteIOContext *pb = s->pb; | |
5625 | 222 AVStream *st = s->streams[0]; |
3189 | 223 int ret; |
224 | |
5625 | 225 if(iff->sent_bytes >= iff->body_size) |
3189 | 226 return AVERROR(EIO); |
3285 | 227 |
228 if(s->streams[0]->codec->channels == 2) { | |
229 uint8_t sample_buffer[PACKET_SIZE]; | |
230 | |
231 ret = get_buffer(pb, sample_buffer, PACKET_SIZE); | |
232 if(av_new_packet(pkt, PACKET_SIZE) < 0) { | |
233 av_log(s, AV_LOG_ERROR, "iff: cannot allocate packet \n"); | |
234 return AVERROR(ENOMEM); | |
235 } | |
236 interleave_stereo(sample_buffer, pkt->data, PACKET_SIZE); | |
5625 | 237 } else if (s->streams[0]->codec->codec_id == CODEC_ID_RAWVIDEO) { |
238 if(av_new_packet(pkt, iff->body_size + AVPALETTE_SIZE) < 0) { | |
239 return AVERROR(ENOMEM); | |
240 } | |
241 | |
242 ret = ff_cmap_read_palette(st->codec, (uint32_t*)(pkt->data + iff->body_size)); | |
243 if (ret < 0) | |
244 return ret; | |
245 av_freep(&st->codec->extradata); | |
246 st->codec->extradata_size = 0; | |
247 | |
248 ret = get_buffer(pb, pkt->data, iff->body_size); | |
249 } else if (s->streams[0]->codec->codec_type == CODEC_TYPE_VIDEO) { | |
250 ret = av_get_packet(pb, pkt, iff->body_size); | |
251 } else { | |
3285 | 252 ret = av_get_packet(pb, pkt, PACKET_SIZE); |
253 } | |
3189 | 254 |
255 if(iff->sent_bytes == 0) | |
256 pkt->flags |= PKT_FLAG_KEY; | |
257 | |
5625 | 258 if(s->streams[0]->codec->codec_type == CODEC_TYPE_AUDIO) { |
5626 | 259 iff->sent_bytes += PACKET_SIZE; |
5625 | 260 } else { |
261 iff->sent_bytes = iff->body_size; | |
262 } | |
3189 | 263 pkt->stream_index = 0; |
5625 | 264 if(s->streams[0]->codec->codec_type == CODEC_TYPE_AUDIO) { |
5626 | 265 pkt->pts = iff->audio_frame_count; |
266 iff->audio_frame_count += ret / s->streams[0]->codec->channels; | |
5625 | 267 } |
3189 | 268 return ret; |
269 } | |
270 | |
271 AVInputFormat iff_demuxer = { | |
272 "IFF", | |
3424
7a0230981402
Make long_names in lavf/lavdev optional depending on CONFIG_SMALL.
diego
parents:
3285
diff
changeset
|
273 NULL_IF_CONFIG_SMALL("IFF format"), |
3189 | 274 sizeof(IffDemuxContext), |
275 iff_probe, | |
276 iff_read_header, | |
277 iff_read_packet, | |
278 }; |