Mercurial > libavformat.hg
annotate wav.c @ 5076:e07fb352a12c libavformat
id3v2.4.0 uses syncsafe integers for sizes of frames.
Fixes issue1234
Fixes regression introduced in r19275
author | michael |
---|---|
date | Mon, 29 Jun 2009 18:36:49 +0000 |
parents | 33a244b7ca65 |
children | 789e13163b3b |
rev | line source |
---|---|
885 | 1 /* |
1415
3b00fb8ef8e4
replace coder/decoder file description in libavformat by muxer/demuxer
aurel
parents:
1358
diff
changeset
|
2 * WAV muxer and demuxer |
4251
77e0c7511d41
cosmetics: Remove pointless period after copyright statement non-sentences.
diego
parents:
4206
diff
changeset
|
3 * Copyright (c) 2001, 2002 Fabrice Bellard |
0 | 4 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1172
diff
changeset
|
5 * This file is part of FFmpeg. |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1172
diff
changeset
|
6 * |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1172
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
0 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1172
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
0 | 11 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1172
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
0 | 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1172
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
893
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 20 */ |
21 #include "avformat.h" | |
2545
213268d7594e
move unrelated functions declarations out of allformats.h
aurel
parents:
2274
diff
changeset
|
22 #include "raw.h" |
1172
6a5e58d2114b
move common stuff from avienc.c and wav.c to new file riff.c
mru
parents:
1169
diff
changeset
|
23 #include "riff.h" |
0 | 24 |
25 typedef struct { | |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3934
diff
changeset
|
26 int64_t data; |
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3934
diff
changeset
|
27 int64_t data_end; |
1781 | 28 int64_t minpts; |
29 int64_t maxpts; | |
30 int last_duration; | |
0 | 31 } WAVContext; |
32 | |
4206 | 33 #if CONFIG_WAV_MUXER |
0 | 34 static int wav_write_header(AVFormatContext *s) |
35 { | |
36 WAVContext *wav = s->priv_data; | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2545
diff
changeset
|
37 ByteIOContext *pb = s->pb; |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3934
diff
changeset
|
38 int64_t fmt, fact; |
0 | 39 |
40 put_tag(pb, "RIFF"); | |
41 put_le32(pb, 0); /* file length */ | |
42 put_tag(pb, "WAVE"); | |
43 | |
44 /* format header */ | |
5058
33a244b7ca65
Add ff_ prefixes to exported symbols in libavformat/riff.h.
diego
parents:
5001
diff
changeset
|
45 fmt = ff_start_tag(pb, "fmt "); |
33a244b7ca65
Add ff_ prefixes to exported symbols in libavformat/riff.h.
diego
parents:
5001
diff
changeset
|
46 if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) { |
3925 | 47 av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n", |
3934 | 48 s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE"); |
0 | 49 av_free(wav); |
50 return -1; | |
51 } | |
5058
33a244b7ca65
Add ff_ prefixes to exported symbols in libavformat/riff.h.
diego
parents:
5001
diff
changeset
|
52 ff_end_tag(pb, fmt); |
0 | 53 |
1781 | 54 if(s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */ |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2545
diff
changeset
|
55 && !url_is_streamed(s->pb)) { |
5058
33a244b7ca65
Add ff_ prefixes to exported symbols in libavformat/riff.h.
diego
parents:
5001
diff
changeset
|
56 fact = ff_start_tag(pb, "fact"); |
1781 | 57 put_le32(pb, 0); |
5058
33a244b7ca65
Add ff_ prefixes to exported symbols in libavformat/riff.h.
diego
parents:
5001
diff
changeset
|
58 ff_end_tag(pb, fact); |
1781 | 59 } |
60 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
817
diff
changeset
|
61 av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate); |
1781 | 62 wav->maxpts = wav->last_duration = 0; |
63 wav->minpts = INT64_MAX; | |
645
9fc2d2cc4608
wav timestamp truncation fix by (Wolfram Gloger <wmglo dent.med.uni-muenchen de>)
michael
parents:
587
diff
changeset
|
64 |
0 | 65 /* data header */ |
5058
33a244b7ca65
Add ff_ prefixes to exported symbols in libavformat/riff.h.
diego
parents:
5001
diff
changeset
|
66 wav->data = ff_start_tag(pb, "data"); |
885 | 67 |
0 | 68 put_flush_packet(pb); |
69 | |
70 return 0; | |
71 } | |
72 | |
468 | 73 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt) |
0 | 74 { |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2545
diff
changeset
|
75 ByteIOContext *pb = s->pb; |
1781 | 76 WAVContext *wav = s->priv_data; |
468 | 77 put_buffer(pb, pkt->data, pkt->size); |
1781 | 78 if(pkt->pts != AV_NOPTS_VALUE) { |
79 wav->minpts = FFMIN(wav->minpts, pkt->pts); | |
80 wav->maxpts = FFMAX(wav->maxpts, pkt->pts); | |
81 wav->last_duration = pkt->duration; | |
82 } else | |
83 av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n"); | |
0 | 84 return 0; |
85 } | |
86 | |
87 static int wav_write_trailer(AVFormatContext *s) | |
88 { | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2545
diff
changeset
|
89 ByteIOContext *pb = s->pb; |
0 | 90 WAVContext *wav = s->priv_data; |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3934
diff
changeset
|
91 int64_t file_size; |
0 | 92 |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2545
diff
changeset
|
93 if (!url_is_streamed(s->pb)) { |
5058
33a244b7ca65
Add ff_ prefixes to exported symbols in libavformat/riff.h.
diego
parents:
5001
diff
changeset
|
94 ff_end_tag(pb, wav->data); |
0 | 95 |
96 /* update file size */ | |
97 file_size = url_ftell(pb); | |
98 url_fseek(pb, 4, SEEK_SET); | |
65 | 99 put_le32(pb, (uint32_t)(file_size - 8)); |
0 | 100 url_fseek(pb, file_size, SEEK_SET); |
101 | |
102 put_flush_packet(pb); | |
1781 | 103 |
104 if(s->streams[0]->codec->codec_tag != 0x01) { | |
105 /* Update num_samps in fact chunk */ | |
106 int number_of_samples; | |
107 number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration, | |
108 s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num, | |
109 s->streams[0]->time_base.den); | |
110 url_fseek(pb, wav->data-12, SEEK_SET); | |
111 put_le32(pb, number_of_samples); | |
112 url_fseek(pb, file_size, SEEK_SET); | |
113 put_flush_packet(pb); | |
114 } | |
0 | 115 } |
116 return 0; | |
117 } | |
3871
e6aeb2733e34
Replace generic CONFIG_MUXERS preprocessor conditionals by more specific
diego
parents:
3766
diff
changeset
|
118 #endif /* CONFIG_WAV_MUXER */ |
0 | 119 |
120 /* return the size of the found tag */ | |
5001
4da68099d180
Change find_tag return type to int64_t, fix a bug
bcoudurier
parents:
4251
diff
changeset
|
121 static int64_t find_tag(ByteIOContext *pb, uint32_t tag1) |
0 | 122 { |
123 unsigned int tag; | |
5001
4da68099d180
Change find_tag return type to int64_t, fix a bug
bcoudurier
parents:
4251
diff
changeset
|
124 int64_t size; |
0 | 125 |
126 for(;;) { | |
127 if (url_feof(pb)) | |
128 return -1; | |
129 tag = get_le32(pb); | |
130 size = get_le32(pb); | |
131 if (tag == tag1) | |
132 break; | |
133 url_fseek(pb, size, SEEK_CUR); | |
134 } | |
135 return size; | |
136 } | |
137 | |
138 static int wav_probe(AVProbeData *p) | |
139 { | |
140 /* check file header */ | |
141 if (p->buf_size <= 32) | |
142 return 0; | |
143 if (p->buf[0] == 'R' && p->buf[1] == 'I' && | |
144 p->buf[2] == 'F' && p->buf[3] == 'F' && | |
145 p->buf[8] == 'W' && p->buf[9] == 'A' && | |
146 p->buf[10] == 'V' && p->buf[11] == 'E') | |
3233
e2bdb989f7da
Decrease returning probe score for WAV demuxer to avoid
voroshil
parents:
2771
diff
changeset
|
147 /* |
e2bdb989f7da
Decrease returning probe score for WAV demuxer to avoid
voroshil
parents:
2771
diff
changeset
|
148 Since ACT demuxer has standard WAV header at top of it's own, |
e2bdb989f7da
Decrease returning probe score for WAV demuxer to avoid
voroshil
parents:
2771
diff
changeset
|
149 returning score is decreased to avoid probe conflict |
e2bdb989f7da
Decrease returning probe score for WAV demuxer to avoid
voroshil
parents:
2771
diff
changeset
|
150 between ACT and WAV. |
e2bdb989f7da
Decrease returning probe score for WAV demuxer to avoid
voroshil
parents:
2771
diff
changeset
|
151 */ |
e2bdb989f7da
Decrease returning probe score for WAV demuxer to avoid
voroshil
parents:
2771
diff
changeset
|
152 return AVPROBE_SCORE_MAX - 1; |
0 | 153 else |
154 return 0; | |
155 } | |
156 | |
157 /* wav input */ | |
158 static int wav_read_header(AVFormatContext *s, | |
159 AVFormatParameters *ap) | |
160 { | |
5001
4da68099d180
Change find_tag return type to int64_t, fix a bug
bcoudurier
parents:
4251
diff
changeset
|
161 int64_t size; |
0 | 162 unsigned int tag; |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2545
diff
changeset
|
163 ByteIOContext *pb = s->pb; |
0 | 164 AVStream *st; |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
165 WAVContext *wav = s->priv_data; |
0 | 166 |
167 /* check RIFF header */ | |
168 tag = get_le32(pb); | |
169 | |
170 if (tag != MKTAG('R', 'I', 'F', 'F')) | |
171 return -1; | |
172 get_le32(pb); /* file size */ | |
173 tag = get_le32(pb); | |
174 if (tag != MKTAG('W', 'A', 'V', 'E')) | |
175 return -1; | |
885 | 176 |
0 | 177 /* parse fmt header */ |
178 size = find_tag(pb, MKTAG('f', 'm', 't', ' ')); | |
179 if (size < 0) | |
180 return -1; | |
181 st = av_new_stream(s, 0); | |
182 if (!st) | |
2273
7eb456c4ed8a
Replace all occurrences of AVERROR_NOMEM with AVERROR(ENOMEM).
takis
parents:
2023
diff
changeset
|
183 return AVERROR(ENOMEM); |
0 | 184 |
5058
33a244b7ca65
Add ff_ prefixes to exported symbols in libavformat/riff.h.
diego
parents:
5001
diff
changeset
|
185 ff_get_wav_header(pb, st->codec, size); |
2023 | 186 st->need_parsing = AVSTREAM_PARSE_FULL; |
567 | 187 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
817
diff
changeset
|
188 av_set_pts_info(st, 64, 1, st->codec->sample_rate); |
567 | 189 |
0 | 190 size = find_tag(pb, MKTAG('d', 'a', 't', 'a')); |
191 if (size < 0) | |
192 return -1; | |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
193 wav->data_end= url_ftell(pb) + size; |
0 | 194 return 0; |
195 } | |
196 | |
197 #define MAX_SIZE 4096 | |
198 | |
199 static int wav_read_packet(AVFormatContext *s, | |
200 AVPacket *pkt) | |
201 { | |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
202 int ret, size, left; |
309 | 203 AVStream *st; |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
204 WAVContext *wav = s->priv_data; |
0 | 205 |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2545
diff
changeset
|
206 if (url_feof(s->pb)) |
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2273
diff
changeset
|
207 return AVERROR(EIO); |
309 | 208 st = s->streams[0]; |
209 | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2545
diff
changeset
|
210 left= wav->data_end - url_ftell(s->pb); |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
211 if(left <= 0){ |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2545
diff
changeset
|
212 left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a')); |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
213 if (left < 0) { |
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2273
diff
changeset
|
214 return AVERROR(EIO); |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
215 } |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2545
diff
changeset
|
216 wav->data_end= url_ftell(s->pb) + left; |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
217 } |
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
218 |
309 | 219 size = MAX_SIZE; |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
817
diff
changeset
|
220 if (st->codec->block_align > 1) { |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
817
diff
changeset
|
221 if (size < st->codec->block_align) |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
817
diff
changeset
|
222 size = st->codec->block_align; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
817
diff
changeset
|
223 size = (size / st->codec->block_align) * st->codec->block_align; |
309 | 224 } |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
225 size= FFMIN(size, left); |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2545
diff
changeset
|
226 ret= av_get_packet(s->pb, pkt, size); |
1756 | 227 if (ret <= 0) |
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2273
diff
changeset
|
228 return AVERROR(EIO); |
0 | 229 pkt->stream_index = 0; |
230 | |
231 /* note: we need to modify the packet size here to handle the last | |
232 packet */ | |
233 pkt->size = ret; | |
234 return ret; | |
235 } | |
236 | |
885 | 237 static int wav_read_seek(AVFormatContext *s, |
558 | 238 int stream_index, int64_t timestamp, int flags) |
309 | 239 { |
240 AVStream *st; | |
241 | |
242 st = s->streams[0]; | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
817
diff
changeset
|
243 switch(st->codec->codec_id) { |
309 | 244 case CODEC_ID_MP2: |
245 case CODEC_ID_MP3: | |
246 case CODEC_ID_AC3: | |
496
112057e05179
libdts support by (Benjamin Zores <ben at geexbox dot org>)
michael
parents:
482
diff
changeset
|
247 case CODEC_ID_DTS: |
309 | 248 /* use generic seeking with dynamically generated indexes */ |
249 return -1; | |
250 default: | |
251 break; | |
252 } | |
558 | 253 return pcm_read_seek(s, stream_index, timestamp, flags); |
309 | 254 } |
255 | |
4206 | 256 #if CONFIG_WAV_DEMUXER |
1169 | 257 AVInputFormat wav_demuxer = { |
0 | 258 "wav", |
3424
7a0230981402
Make long_names in lavf/lavdev optional depending on CONFIG_SMALL.
diego
parents:
3233
diff
changeset
|
259 NULL_IF_CONFIG_SMALL("WAV format"), |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
260 sizeof(WAVContext), |
0 | 261 wav_probe, |
262 wav_read_header, | |
263 wav_read_packet, | |
3484 | 264 NULL, |
309 | 265 wav_read_seek, |
1756 | 266 .flags= AVFMT_GENERIC_INDEX, |
5058
33a244b7ca65
Add ff_ prefixes to exported symbols in libavformat/riff.h.
diego
parents:
5001
diff
changeset
|
267 .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0}, |
0 | 268 }; |
1169 | 269 #endif |
4206 | 270 #if CONFIG_WAV_MUXER |
1169 | 271 AVOutputFormat wav_muxer = { |
0 | 272 "wav", |
3424
7a0230981402
Make long_names in lavf/lavdev optional depending on CONFIG_SMALL.
diego
parents:
3233
diff
changeset
|
273 NULL_IF_CONFIG_SMALL("WAV format"), |
0 | 274 "audio/x-wav", |
275 "wav", | |
276 sizeof(WAVContext), | |
277 CODEC_ID_PCM_S16LE, | |
278 CODEC_ID_NONE, | |
279 wav_write_header, | |
280 wav_write_packet, | |
281 wav_write_trailer, | |
5058
33a244b7ca65
Add ff_ prefixes to exported symbols in libavformat/riff.h.
diego
parents:
5001
diff
changeset
|
282 .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0}, |
0 | 283 }; |
1169 | 284 #endif |