ffmpeg / libavformat / wav.c @ e9b78eeb
History | View | Annotate | Download (5.99 KB)
1 |
/*
|
---|---|
2 |
* WAV muxer and demuxer
|
3 |
* Copyright (c) 2001, 2002 Fabrice Bellard.
|
4 |
*
|
5 |
* This file is part of FFmpeg.
|
6 |
*
|
7 |
* FFmpeg is free software; you can redistribute it and/or
|
8 |
* modify it under the terms of the GNU Lesser General Public
|
9 |
* License as published by the Free Software Foundation; either
|
10 |
* version 2.1 of the License, or (at your option) any later version.
|
11 |
*
|
12 |
* FFmpeg is distributed in the hope that it will be useful,
|
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
|
18 |
* License along with FFmpeg; if not, write to the Free Software
|
19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
20 |
*/
|
21 |
#include "avformat.h" |
22 |
#include "allformats.h" |
23 |
#include "riff.h" |
24 |
|
25 |
typedef struct { |
26 |
offset_t data; |
27 |
offset_t data_end; |
28 |
} WAVContext; |
29 |
|
30 |
#ifdef CONFIG_MUXERS
|
31 |
static int wav_write_header(AVFormatContext *s) |
32 |
{ |
33 |
WAVContext *wav = s->priv_data; |
34 |
ByteIOContext *pb = &s->pb; |
35 |
offset_t fmt; |
36 |
|
37 |
put_tag(pb, "RIFF");
|
38 |
put_le32(pb, 0); /* file length */ |
39 |
put_tag(pb, "WAVE");
|
40 |
|
41 |
/* format header */
|
42 |
fmt = start_tag(pb, "fmt ");
|
43 |
if (put_wav_header(pb, s->streams[0]->codec) < 0) { |
44 |
av_free(wav); |
45 |
return -1; |
46 |
} |
47 |
end_tag(pb, fmt); |
48 |
|
49 |
av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate); |
50 |
|
51 |
/* data header */
|
52 |
wav->data = start_tag(pb, "data");
|
53 |
|
54 |
put_flush_packet(pb); |
55 |
|
56 |
return 0; |
57 |
} |
58 |
|
59 |
static int wav_write_packet(AVFormatContext *s, AVPacket *pkt) |
60 |
{ |
61 |
ByteIOContext *pb = &s->pb; |
62 |
put_buffer(pb, pkt->data, pkt->size); |
63 |
return 0; |
64 |
} |
65 |
|
66 |
static int wav_write_trailer(AVFormatContext *s) |
67 |
{ |
68 |
ByteIOContext *pb = &s->pb; |
69 |
WAVContext *wav = s->priv_data; |
70 |
offset_t file_size; |
71 |
|
72 |
if (!url_is_streamed(&s->pb)) {
|
73 |
end_tag(pb, wav->data); |
74 |
|
75 |
/* update file size */
|
76 |
file_size = url_ftell(pb); |
77 |
url_fseek(pb, 4, SEEK_SET);
|
78 |
put_le32(pb, (uint32_t)(file_size - 8));
|
79 |
url_fseek(pb, file_size, SEEK_SET); |
80 |
|
81 |
put_flush_packet(pb); |
82 |
} |
83 |
return 0; |
84 |
} |
85 |
#endif //CONFIG_MUXERS |
86 |
|
87 |
/* return the size of the found tag */
|
88 |
/* XXX: > 2GB ? */
|
89 |
static int find_tag(ByteIOContext *pb, uint32_t tag1) |
90 |
{ |
91 |
unsigned int tag; |
92 |
int size;
|
93 |
|
94 |
for(;;) {
|
95 |
if (url_feof(pb))
|
96 |
return -1; |
97 |
tag = get_le32(pb); |
98 |
size = get_le32(pb); |
99 |
if (tag == tag1)
|
100 |
break;
|
101 |
url_fseek(pb, size, SEEK_CUR); |
102 |
} |
103 |
if (size < 0) |
104 |
size = 0x7fffffff;
|
105 |
return size;
|
106 |
} |
107 |
|
108 |
static int wav_probe(AVProbeData *p) |
109 |
{ |
110 |
/* check file header */
|
111 |
if (p->buf_size <= 32) |
112 |
return 0; |
113 |
if (p->buf[0] == 'R' && p->buf[1] == 'I' && |
114 |
p->buf[2] == 'F' && p->buf[3] == 'F' && |
115 |
p->buf[8] == 'W' && p->buf[9] == 'A' && |
116 |
p->buf[10] == 'V' && p->buf[11] == 'E') |
117 |
return AVPROBE_SCORE_MAX;
|
118 |
else
|
119 |
return 0; |
120 |
} |
121 |
|
122 |
/* wav input */
|
123 |
static int wav_read_header(AVFormatContext *s, |
124 |
AVFormatParameters *ap) |
125 |
{ |
126 |
int size;
|
127 |
unsigned int tag; |
128 |
ByteIOContext *pb = &s->pb; |
129 |
AVStream *st; |
130 |
WAVContext *wav = s->priv_data; |
131 |
|
132 |
/* check RIFF header */
|
133 |
tag = get_le32(pb); |
134 |
|
135 |
if (tag != MKTAG('R', 'I', 'F', 'F')) |
136 |
return -1; |
137 |
get_le32(pb); /* file size */
|
138 |
tag = get_le32(pb); |
139 |
if (tag != MKTAG('W', 'A', 'V', 'E')) |
140 |
return -1; |
141 |
|
142 |
/* parse fmt header */
|
143 |
size = find_tag(pb, MKTAG('f', 'm', 't', ' ')); |
144 |
if (size < 0) |
145 |
return -1; |
146 |
st = av_new_stream(s, 0);
|
147 |
if (!st)
|
148 |
return AVERROR_NOMEM;
|
149 |
|
150 |
get_wav_header(pb, st->codec, size); |
151 |
st->need_parsing = 1;
|
152 |
|
153 |
av_set_pts_info(st, 64, 1, st->codec->sample_rate); |
154 |
|
155 |
size = find_tag(pb, MKTAG('d', 'a', 't', 'a')); |
156 |
if (size < 0) |
157 |
return -1; |
158 |
wav->data_end= url_ftell(pb) + size; |
159 |
return 0; |
160 |
} |
161 |
|
162 |
#define MAX_SIZE 4096 |
163 |
|
164 |
static int wav_read_packet(AVFormatContext *s, |
165 |
AVPacket *pkt) |
166 |
{ |
167 |
int ret, size, left;
|
168 |
AVStream *st; |
169 |
WAVContext *wav = s->priv_data; |
170 |
|
171 |
if (url_feof(&s->pb))
|
172 |
return AVERROR_IO;
|
173 |
st = s->streams[0];
|
174 |
|
175 |
left= wav->data_end - url_ftell(&s->pb); |
176 |
if(left <= 0){ |
177 |
left = find_tag(&(s->pb), MKTAG('d', 'a', 't', 'a')); |
178 |
if (left < 0) { |
179 |
return AVERROR_IO;
|
180 |
} |
181 |
wav->data_end= url_ftell(&s->pb) + left; |
182 |
} |
183 |
|
184 |
size = MAX_SIZE; |
185 |
if (st->codec->block_align > 1) { |
186 |
if (size < st->codec->block_align)
|
187 |
size = st->codec->block_align; |
188 |
size = (size / st->codec->block_align) * st->codec->block_align; |
189 |
} |
190 |
size= FFMIN(size, left); |
191 |
ret= av_get_packet(&s->pb, pkt, size); |
192 |
if (ret <= 0) |
193 |
return AVERROR_IO;
|
194 |
pkt->stream_index = 0;
|
195 |
|
196 |
/* note: we need to modify the packet size here to handle the last
|
197 |
packet */
|
198 |
pkt->size = ret; |
199 |
return ret;
|
200 |
} |
201 |
|
202 |
static int wav_read_close(AVFormatContext *s) |
203 |
{ |
204 |
return 0; |
205 |
} |
206 |
|
207 |
static int wav_read_seek(AVFormatContext *s, |
208 |
int stream_index, int64_t timestamp, int flags) |
209 |
{ |
210 |
AVStream *st; |
211 |
|
212 |
st = s->streams[0];
|
213 |
switch(st->codec->codec_id) {
|
214 |
case CODEC_ID_MP2:
|
215 |
case CODEC_ID_MP3:
|
216 |
case CODEC_ID_AC3:
|
217 |
case CODEC_ID_DTS:
|
218 |
/* use generic seeking with dynamically generated indexes */
|
219 |
return -1; |
220 |
default:
|
221 |
break;
|
222 |
} |
223 |
return pcm_read_seek(s, stream_index, timestamp, flags);
|
224 |
} |
225 |
|
226 |
#ifdef CONFIG_WAV_DEMUXER
|
227 |
AVInputFormat wav_demuxer = { |
228 |
"wav",
|
229 |
"wav format",
|
230 |
sizeof(WAVContext),
|
231 |
wav_probe, |
232 |
wav_read_header, |
233 |
wav_read_packet, |
234 |
wav_read_close, |
235 |
wav_read_seek, |
236 |
.flags= AVFMT_GENERIC_INDEX, |
237 |
.codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0}, |
238 |
}; |
239 |
#endif
|
240 |
#ifdef CONFIG_WAV_MUXER
|
241 |
AVOutputFormat wav_muxer = { |
242 |
"wav",
|
243 |
"wav format",
|
244 |
"audio/x-wav",
|
245 |
"wav",
|
246 |
sizeof(WAVContext),
|
247 |
CODEC_ID_PCM_S16LE, |
248 |
CODEC_ID_NONE, |
249 |
wav_write_header, |
250 |
wav_write_packet, |
251 |
wav_write_trailer, |
252 |
.codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0}, |
253 |
}; |
254 |
#endif
|