2 * WAV muxer and demuxer
3 * Copyright (c) 2001, 2002 Fabrice Bellard
6 * Copyright (c) 2009 Daniel Verkamp
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
38 static int wav_write_header(AVFormatContext
*s
)
40 WAVContext
*wav
= s
->priv_data
;
41 ByteIOContext
*pb
= s
->pb
;
45 put_le32(pb
, 0); /* file length */
49 fmt
= ff_start_tag(pb
, "fmt ");
50 if (ff_put_wav_header(pb
, s
->streams
[0]->codec
) < 0) {
51 av_log(s
, AV_LOG_ERROR
, "%s codec not supported in WAVE format\n",
52 s
->streams
[0]->codec
->codec
? s
->streams
[0]->codec
->codec
->name
: "NONE");
58 if (s
->streams
[0]->codec
->codec_tag
!= 0x01 /* hence for all other than PCM */
59 && !url_is_streamed(s
->pb
)) {
60 fact
= ff_start_tag(pb
, "fact");
65 av_set_pts_info(s
->streams
[0], 64, 1, s
->streams
[0]->codec
->sample_rate
);
66 wav
->maxpts
= wav
->last_duration
= 0;
67 wav
->minpts
= INT64_MAX
;
70 wav
->data
= ff_start_tag(pb
, "data");
77 static int wav_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
79 ByteIOContext
*pb
= s
->pb
;
80 WAVContext
*wav
= s
->priv_data
;
81 put_buffer(pb
, pkt
->data
, pkt
->size
);
82 if(pkt
->pts
!= AV_NOPTS_VALUE
) {
83 wav
->minpts
= FFMIN(wav
->minpts
, pkt
->pts
);
84 wav
->maxpts
= FFMAX(wav
->maxpts
, pkt
->pts
);
85 wav
->last_duration
= pkt
->duration
;
87 av_log(s
, AV_LOG_ERROR
, "wav_write_packet: NOPTS\n");
91 static int wav_write_trailer(AVFormatContext
*s
)
93 ByteIOContext
*pb
= s
->pb
;
94 WAVContext
*wav
= s
->priv_data
;
97 if (!url_is_streamed(s
->pb
)) {
98 ff_end_tag(pb
, wav
->data
);
100 /* update file size */
101 file_size
= url_ftell(pb
);
102 url_fseek(pb
, 4, SEEK_SET
);
103 put_le32(pb
, (uint32_t)(file_size
- 8));
104 url_fseek(pb
, file_size
, SEEK_SET
);
106 put_flush_packet(pb
);
108 if(s
->streams
[0]->codec
->codec_tag
!= 0x01) {
109 /* Update num_samps in fact chunk */
110 int number_of_samples
;
111 number_of_samples
= av_rescale(wav
->maxpts
- wav
->minpts
+ wav
->last_duration
,
112 s
->streams
[0]->codec
->sample_rate
* (int64_t)s
->streams
[0]->time_base
.num
,
113 s
->streams
[0]->time_base
.den
);
114 url_fseek(pb
, wav
->data
-12, SEEK_SET
);
115 put_le32(pb
, number_of_samples
);
116 url_fseek(pb
, file_size
, SEEK_SET
);
117 put_flush_packet(pb
);
122 #endif /* CONFIG_WAV_MUXER */
124 /* return the size of the found tag */
125 static int64_t find_tag(ByteIOContext
*pb
, uint32_t tag1
)
137 url_fseek(pb
, size
, SEEK_CUR
);
142 static int wav_probe(AVProbeData
*p
)
144 /* check file header */
145 if (p
->buf_size
<= 32)
147 if (p
->buf
[ 0] == 'R' && p
->buf
[ 1] == 'I' &&
148 p
->buf
[ 2] == 'F' && p
->buf
[ 3] == 'F' &&
149 p
->buf
[ 8] == 'W' && p
->buf
[ 9] == 'A' &&
150 p
->buf
[10] == 'V' && p
->buf
[11] == 'E')
152 Since ACT demuxer has standard WAV header at top of it's own,
153 returning score is decreased to avoid probe conflict
156 return AVPROBE_SCORE_MAX
- 1;
162 static int wav_read_header(AVFormatContext
*s
,
163 AVFormatParameters
*ap
)
167 ByteIOContext
*pb
= s
->pb
;
169 WAVContext
*wav
= s
->priv_data
;
171 /* check RIFF header */
174 if (tag
!= MKTAG('R', 'I', 'F', 'F'))
176 get_le32(pb
); /* file size */
178 if (tag
!= MKTAG('W', 'A', 'V', 'E'))
181 /* parse fmt header */
182 size
= find_tag(pb
, MKTAG('f', 'm', 't', ' '));
185 st
= av_new_stream(s
, 0);
187 return AVERROR(ENOMEM
);
189 ff_get_wav_header(pb
, st
->codec
, size
);
190 st
->need_parsing
= AVSTREAM_PARSE_FULL
;
192 av_set_pts_info(st
, 64, 1, st
->codec
->sample_rate
);
194 size
= find_tag(pb
, MKTAG('d', 'a', 't', 'a'));
197 wav
->data_end
= url_ftell(pb
) + size
;
201 #if CONFIG_W64_DEMUXER
203 static const uint8_t guid_riff
[16] = { 'r', 'i', 'f', 'f',
204 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
206 static const uint8_t guid_wave
[16] = { 'w', 'a', 'v', 'e',
207 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
209 static const uint8_t guid_fmt
[16] = { 'f', 'm', 't', ' ',
210 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
212 static const uint8_t guid_data
[16] = { 'd', 'a', 't', 'a',
213 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
215 static int w64_probe(AVProbeData
*p
)
217 if (p
->buf_size
<= 40)
219 if (!memcmp(p
->buf
, guid_riff
, 16) &&
220 !memcmp(p
->buf
+ 24, guid_wave
, 16))
221 return AVPROBE_SCORE_MAX
;
226 /** Find chunk with w64 GUID by skipping over other chunks
227 * @return the size of the found chunk
229 static int64_t find_guid(ByteIOContext
*pb
, const uint8_t guid1
[16])
234 while (!url_feof(pb
)) {
235 get_buffer(pb
, guid
, 16);
239 if (!memcmp(guid
, guid1
, 16))
241 url_fskip(pb
, FFALIGN(size
, INT64_C(8)) - 24);
246 static int w64_read_header(AVFormatContext
*s
, AVFormatParameters
*ap
)
249 ByteIOContext
*pb
= s
->pb
;
250 WAVContext
*wav
= s
->priv_data
;
254 get_buffer(pb
, guid
, 16);
255 if (memcmp(guid
, guid_riff
, 16))
258 if (get_le64(pb
) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
261 get_buffer(pb
, guid
, 16);
262 if (memcmp(guid
, guid_wave
, 16)) {
263 av_log(s
, AV_LOG_ERROR
, "could not find wave guid\n");
267 size
= find_guid(pb
, guid_fmt
);
269 av_log(s
, AV_LOG_ERROR
, "could not find fmt guid\n");
273 st
= av_new_stream(s
, 0);
275 return AVERROR(ENOMEM
);
277 /* subtract chunk header size - normal wav file doesn't count it */
278 ff_get_wav_header(pb
, st
->codec
, size
- 24);
279 url_fskip(pb
, FFALIGN(size
, INT64_C(8)) - size
);
281 st
->need_parsing
= AVSTREAM_PARSE_FULL
;
283 av_set_pts_info(st
, 64, 1, st
->codec
->sample_rate
);
285 size
= find_guid(pb
, guid_data
);
287 av_log(s
, AV_LOG_ERROR
, "could not find data guid\n");
290 wav
->data_end
= url_ftell(pb
) + size
- 24;
295 #endif /* CONFIG_W64_DEMUXER */
297 #define MAX_SIZE 4096
299 static int wav_read_packet(AVFormatContext
*s
,
305 WAVContext
*wav
= s
->priv_data
;
311 left
= wav
->data_end
- url_ftell(s
->pb
);
313 if (CONFIG_W64_DEMUXER
&& wav
->w64
)
314 left
= find_guid(s
->pb
, guid_data
) - 24;
316 left
= find_tag(s
->pb
, MKTAG('d', 'a', 't', 'a'));
319 wav
->data_end
= url_ftell(s
->pb
) + left
;
323 if (st
->codec
->block_align
> 1) {
324 if (size
< st
->codec
->block_align
)
325 size
= st
->codec
->block_align
;
326 size
= (size
/ st
->codec
->block_align
) * st
->codec
->block_align
;
328 size
= FFMIN(size
, left
);
329 ret
= av_get_packet(s
->pb
, pkt
, size
);
332 pkt
->stream_index
= 0;
334 /* note: we need to modify the packet size here to handle the last
340 static int wav_read_seek(AVFormatContext
*s
,
341 int stream_index
, int64_t timestamp
, int flags
)
346 switch (st
->codec
->codec_id
) {
351 /* use generic seeking with dynamically generated indexes */
356 return pcm_read_seek(s
, stream_index
, timestamp
, flags
);
359 #if CONFIG_WAV_DEMUXER
360 AVInputFormat wav_demuxer
= {
362 NULL_IF_CONFIG_SMALL("WAV format"),
369 .flags
= AVFMT_GENERIC_INDEX
,
370 .codec_tag
= (const AVCodecTag
* const []){ff_codec_wav_tags
, 0},
375 AVOutputFormat wav_muxer
= {
377 NULL_IF_CONFIG_SMALL("WAV format"),
386 .codec_tag
= (const AVCodecTag
* const []){ff_codec_wav_tags
, 0},
390 #if CONFIG_W64_DEMUXER
391 AVInputFormat w64_demuxer
= {
393 NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
400 .flags
= AVFMT_GENERIC_INDEX
,
401 .codec_tag
= (const AVCodecTag
* const []){ff_codec_wav_tags
, 0},