Mercurial > libavformat.hg
annotate wav.c @ 2277:456e19bc6cf7 libavformat
Replace ENOSYS by ENOTSUP as in this case the problem is not really a function
which is not available, but a media type which is not supported.
author | takis |
---|---|
date | Fri, 20 Jul 2007 08:05:27 +0000 |
parents | b21c2af60bc9 |
children | 213268d7594e |
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 |
0 | 3 * Copyright (c) 2001, 2002 Fabrice Bellard. |
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" | |
1142
e3a585883bbd
Move initialisations and internal symbols in allformats.h,
gpoirier
parents:
1136
diff
changeset
|
22 #include "allformats.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 { | |
26 offset_t data; | |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
27 offset_t data_end; |
1781 | 28 int64_t minpts; |
29 int64_t maxpts; | |
30 int last_duration; | |
0 | 31 } WAVContext; |
32 | |
1149 | 33 #ifdef CONFIG_MUXERS |
0 | 34 static int wav_write_header(AVFormatContext *s) |
35 { | |
36 WAVContext *wav = s->priv_data; | |
37 ByteIOContext *pb = &s->pb; | |
1781 | 38 offset_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 */ | |
45 fmt = start_tag(pb, "fmt "); | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
817
diff
changeset
|
46 if (put_wav_header(pb, s->streams[0]->codec) < 0) { |
0 | 47 av_free(wav); |
48 return -1; | |
49 } | |
50 end_tag(pb, fmt); | |
51 | |
1781 | 52 if(s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */ |
53 && !url_is_streamed(&s->pb)) { | |
54 fact = start_tag(pb, "fact"); | |
55 put_le32(pb, 0); | |
56 end_tag(pb, fact); | |
57 } | |
58 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
817
diff
changeset
|
59 av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate); |
1781 | 60 wav->maxpts = wav->last_duration = 0; |
61 wav->minpts = INT64_MAX; | |
645
9fc2d2cc4608
wav timestamp truncation fix by (Wolfram Gloger <wmglo dent.med.uni-muenchen de>)
michael
parents:
587
diff
changeset
|
62 |
0 | 63 /* data header */ |
64 wav->data = start_tag(pb, "data"); | |
885 | 65 |
0 | 66 put_flush_packet(pb); |
67 | |
68 return 0; | |
69 } | |
70 | |
468 | 71 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt) |
0 | 72 { |
73 ByteIOContext *pb = &s->pb; | |
1781 | 74 WAVContext *wav = s->priv_data; |
468 | 75 put_buffer(pb, pkt->data, pkt->size); |
1781 | 76 if(pkt->pts != AV_NOPTS_VALUE) { |
77 wav->minpts = FFMIN(wav->minpts, pkt->pts); | |
78 wav->maxpts = FFMAX(wav->maxpts, pkt->pts); | |
79 wav->last_duration = pkt->duration; | |
80 } else | |
81 av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n"); | |
0 | 82 return 0; |
83 } | |
84 | |
85 static int wav_write_trailer(AVFormatContext *s) | |
86 { | |
87 ByteIOContext *pb = &s->pb; | |
88 WAVContext *wav = s->priv_data; | |
89 offset_t file_size; | |
90 | |
91 if (!url_is_streamed(&s->pb)) { | |
92 end_tag(pb, wav->data); | |
93 | |
94 /* update file size */ | |
95 file_size = url_ftell(pb); | |
96 url_fseek(pb, 4, SEEK_SET); | |
65 | 97 put_le32(pb, (uint32_t)(file_size - 8)); |
0 | 98 url_fseek(pb, file_size, SEEK_SET); |
99 | |
100 put_flush_packet(pb); | |
1781 | 101 |
102 if(s->streams[0]->codec->codec_tag != 0x01) { | |
103 /* Update num_samps in fact chunk */ | |
104 int number_of_samples; | |
105 number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration, | |
106 s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num, | |
107 s->streams[0]->time_base.den); | |
108 url_fseek(pb, wav->data-12, SEEK_SET); | |
109 put_le32(pb, number_of_samples); | |
110 url_fseek(pb, file_size, SEEK_SET); | |
111 put_flush_packet(pb); | |
112 } | |
0 | 113 } |
114 return 0; | |
115 } | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
846
diff
changeset
|
116 #endif //CONFIG_MUXERS |
0 | 117 |
118 /* return the size of the found tag */ | |
119 /* XXX: > 2GB ? */ | |
65 | 120 static int find_tag(ByteIOContext *pb, uint32_t tag1) |
0 | 121 { |
122 unsigned int tag; | |
123 int size; | |
124 | |
125 for(;;) { | |
126 if (url_feof(pb)) | |
127 return -1; | |
128 tag = get_le32(pb); | |
129 size = get_le32(pb); | |
130 if (tag == tag1) | |
131 break; | |
132 url_fseek(pb, size, SEEK_CUR); | |
133 } | |
134 if (size < 0) | |
135 size = 0x7fffffff; | |
136 return size; | |
137 } | |
138 | |
139 static int wav_probe(AVProbeData *p) | |
140 { | |
141 /* check file header */ | |
142 if (p->buf_size <= 32) | |
143 return 0; | |
144 if (p->buf[0] == 'R' && p->buf[1] == 'I' && | |
145 p->buf[2] == 'F' && p->buf[3] == 'F' && | |
146 p->buf[8] == 'W' && p->buf[9] == 'A' && | |
147 p->buf[10] == 'V' && p->buf[11] == 'E') | |
148 return AVPROBE_SCORE_MAX; | |
149 else | |
150 return 0; | |
151 } | |
152 | |
153 /* wav input */ | |
154 static int wav_read_header(AVFormatContext *s, | |
155 AVFormatParameters *ap) | |
156 { | |
157 int size; | |
158 unsigned int tag; | |
159 ByteIOContext *pb = &s->pb; | |
160 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
|
161 WAVContext *wav = s->priv_data; |
0 | 162 |
163 /* check RIFF header */ | |
164 tag = get_le32(pb); | |
165 | |
166 if (tag != MKTAG('R', 'I', 'F', 'F')) | |
167 return -1; | |
168 get_le32(pb); /* file size */ | |
169 tag = get_le32(pb); | |
170 if (tag != MKTAG('W', 'A', 'V', 'E')) | |
171 return -1; | |
885 | 172 |
0 | 173 /* parse fmt header */ |
174 size = find_tag(pb, MKTAG('f', 'm', 't', ' ')); | |
175 if (size < 0) | |
176 return -1; | |
177 st = av_new_stream(s, 0); | |
178 if (!st) | |
2273
7eb456c4ed8a
Replace all occurrences of AVERROR_NOMEM with AVERROR(ENOMEM).
takis
parents:
2023
diff
changeset
|
179 return AVERROR(ENOMEM); |
0 | 180 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
817
diff
changeset
|
181 get_wav_header(pb, st->codec, size); |
2023 | 182 st->need_parsing = AVSTREAM_PARSE_FULL; |
567 | 183 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
817
diff
changeset
|
184 av_set_pts_info(st, 64, 1, st->codec->sample_rate); |
567 | 185 |
0 | 186 size = find_tag(pb, MKTAG('d', 'a', 't', 'a')); |
187 if (size < 0) | |
188 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
|
189 wav->data_end= url_ftell(pb) + size; |
0 | 190 return 0; |
191 } | |
192 | |
193 #define MAX_SIZE 4096 | |
194 | |
195 static int wav_read_packet(AVFormatContext *s, | |
196 AVPacket *pkt) | |
197 { | |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
198 int ret, size, left; |
309 | 199 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
|
200 WAVContext *wav = s->priv_data; |
0 | 201 |
202 if (url_feof(&s->pb)) | |
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2273
diff
changeset
|
203 return AVERROR(EIO); |
309 | 204 st = s->streams[0]; |
205 | |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
206 left= wav->data_end - url_ftell(&s->pb); |
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
207 if(left <= 0){ |
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
208 left = find_tag(&(s->pb), MKTAG('d', 'a', 't', 'a')); |
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
209 if (left < 0) { |
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2273
diff
changeset
|
210 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
|
211 } |
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
212 wav->data_end= url_ftell(&s->pb) + left; |
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
213 } |
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
214 |
309 | 215 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
|
216 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
|
217 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
|
218 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
|
219 size = (size / st->codec->block_align) * st->codec->block_align; |
309 | 220 } |
1136
d65cd7c3573e
dont read over the end of a data chunk and at the end search for the next
michael
parents:
1122
diff
changeset
|
221 size= FFMIN(size, left); |
1756 | 222 ret= av_get_packet(&s->pb, pkt, size); |
223 if (ret <= 0) | |
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2273
diff
changeset
|
224 return AVERROR(EIO); |
0 | 225 pkt->stream_index = 0; |
226 | |
227 /* note: we need to modify the packet size here to handle the last | |
228 packet */ | |
229 pkt->size = ret; | |
230 return ret; | |
231 } | |
232 | |
233 static int wav_read_close(AVFormatContext *s) | |
234 { | |
235 return 0; | |
236 } | |
237 | |
885 | 238 static int wav_read_seek(AVFormatContext *s, |
558 | 239 int stream_index, int64_t timestamp, int flags) |
309 | 240 { |
241 AVStream *st; | |
242 | |
243 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
|
244 switch(st->codec->codec_id) { |
309 | 245 case CODEC_ID_MP2: |
246 case CODEC_ID_MP3: | |
247 case CODEC_ID_AC3: | |
496
112057e05179
libdts support by (Benjamin Zores <ben at geexbox dot org>)
michael
parents:
482
diff
changeset
|
248 case CODEC_ID_DTS: |
309 | 249 /* use generic seeking with dynamically generated indexes */ |
250 return -1; | |
251 default: | |
252 break; | |
253 } | |
558 | 254 return pcm_read_seek(s, stream_index, timestamp, flags); |
309 | 255 } |
256 | |
1169 | 257 #ifdef CONFIG_WAV_DEMUXER |
258 AVInputFormat wav_demuxer = { | |
0 | 259 "wav", |
260 "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
|
261 sizeof(WAVContext), |
0 | 262 wav_probe, |
263 wav_read_header, | |
264 wav_read_packet, | |
265 wav_read_close, | |
309 | 266 wav_read_seek, |
1756 | 267 .flags= AVFMT_GENERIC_INDEX, |
1679 | 268 .codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0}, |
0 | 269 }; |
1169 | 270 #endif |
271 #ifdef CONFIG_WAV_MUXER | |
272 AVOutputFormat wav_muxer = { | |
0 | 273 "wav", |
274 "wav format", | |
275 "audio/x-wav", | |
276 "wav", | |
277 sizeof(WAVContext), | |
278 CODEC_ID_PCM_S16LE, | |
279 CODEC_ID_NONE, | |
280 wav_write_header, | |
281 wav_write_packet, | |
282 wav_write_trailer, | |
1679 | 283 .codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0}, |
0 | 284 }; |
1169 | 285 #endif |