2 * WAV muxer and demuxer
3 * Copyright (c) 2001, 2002 Fabrice Bellard
7 * Copyright (c) 2009 Daniel Verkamp
9 * This file is part of FFmpeg.
11 * FFmpeg is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * FFmpeg is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
39 static int wav_write_header(AVFormatContext
*s
)
41 WAVContext
*wav
= s
->priv_data
;
42 ByteIOContext
*pb
= s
->pb
;
46 put_le32(pb
, 0); /* file length */
50 fmt
= ff_start_tag(pb
, "fmt ");
51 if (ff_put_wav_header(pb
, s
->streams
[0]->codec
) < 0) {
52 av_log(s
, AV_LOG_ERROR
, "%s codec not supported in WAVE format\n",
53 s
->streams
[0]->codec
->codec
? s
->streams
[0]->codec
->codec
->name
: "NONE");
59 if (s
->streams
[0]->codec
->codec_tag
!= 0x01 /* hence for all other than PCM */
60 && !url_is_streamed(s
->pb
)) {
61 fact
= ff_start_tag(pb
, "fact");
66 av_set_pts_info(s
->streams
[0], 64, 1, s
->streams
[0]->codec
->sample_rate
);
67 wav
->maxpts
= wav
->last_duration
= 0;
68 wav
->minpts
= INT64_MAX
;
71 wav
->data
= ff_start_tag(pb
, "data");
78 static int wav_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
80 ByteIOContext
*pb
= s
->pb
;
81 WAVContext
*wav
= s
->priv_data
;
82 put_buffer(pb
, pkt
->data
, pkt
->size
);
83 if(pkt
->pts
!= AV_NOPTS_VALUE
) {
84 wav
->minpts
= FFMIN(wav
->minpts
, pkt
->pts
);
85 wav
->maxpts
= FFMAX(wav
->maxpts
, pkt
->pts
);
86 wav
->last_duration
= pkt
->duration
;
88 av_log(s
, AV_LOG_ERROR
, "wav_write_packet: NOPTS\n");
92 static int wav_write_trailer(AVFormatContext
*s
)
94 ByteIOContext
*pb
= s
->pb
;
95 WAVContext
*wav
= s
->priv_data
;
98 if (!url_is_streamed(s
->pb
)) {
99 ff_end_tag(pb
, wav
->data
);
101 /* update file size */
102 file_size
= url_ftell(pb
);
103 url_fseek(pb
, 4, SEEK_SET
);
104 put_le32(pb
, (uint32_t)(file_size
- 8));
105 url_fseek(pb
, file_size
, SEEK_SET
);
107 put_flush_packet(pb
);
109 if(s
->streams
[0]->codec
->codec_tag
!= 0x01) {
110 /* Update num_samps in fact chunk */
111 int number_of_samples
;
112 number_of_samples
= av_rescale(wav
->maxpts
- wav
->minpts
+ wav
->last_duration
,
113 s
->streams
[0]->codec
->sample_rate
* (int64_t)s
->streams
[0]->time_base
.num
,
114 s
->streams
[0]->time_base
.den
);
115 url_fseek(pb
, wav
->data
-12, SEEK_SET
);
116 put_le32(pb
, number_of_samples
);
117 url_fseek(pb
, file_size
, SEEK_SET
);
118 put_flush_packet(pb
);
124 AVOutputFormat wav_muxer
= {
126 NULL_IF_CONFIG_SMALL("WAV format"),
135 .codec_tag
= (const AVCodecTag
* const []){ff_codec_wav_tags
, 0},
137 #endif /* CONFIG_WAV_MUXER */
140 #if CONFIG_WAV_DEMUXER
141 /* return the size of the found tag */
142 static int64_t find_tag(ByteIOContext
*pb
, uint32_t tag1
)
154 url_fseek(pb
, size
, SEEK_CUR
);
159 static int wav_probe(AVProbeData
*p
)
161 /* check file header */
162 if (p
->buf_size
<= 32)
164 if (!memcmp(p
->buf
+ 8, "WAVE", 4)) {
165 if (!memcmp(p
->buf
, "RIFF", 4))
167 Since ACT demuxer has standard WAV header at top of it's own,
168 returning score is decreased to avoid probe conflict
171 return AVPROBE_SCORE_MAX
- 1;
172 else if (!memcmp(p
->buf
, "RF64", 4) &&
173 !memcmp(p
->buf
+ 12, "ds64", 4))
174 return AVPROBE_SCORE_MAX
;
180 static int wav_read_header(AVFormatContext
*s
,
181 AVFormatParameters
*ap
)
183 int64_t size
, av_uninit(data_size
);
186 ByteIOContext
*pb
= s
->pb
;
188 WAVContext
*wav
= s
->priv_data
;
190 /* check RIFF header */
193 rf64
= tag
== MKTAG('R', 'F', '6', '4');
194 if (!rf64
&& tag
!= MKTAG('R', 'I', 'F', 'F'))
196 get_le32(pb
); /* file size */
198 if (tag
!= MKTAG('W', 'A', 'V', 'E'))
202 if (get_le32(pb
) != MKTAG('d', 's', '6', '4'))
207 get_le64(pb
); /* RIFF size */
208 data_size
= get_le64(pb
);
209 url_fskip(pb
, size
- 16); /* skip rest of ds64 chunk */
212 /* parse fmt header */
213 size
= find_tag(pb
, MKTAG('f', 'm', 't', ' '));
216 st
= av_new_stream(s
, 0);
218 return AVERROR(ENOMEM
);
220 ff_get_wav_header(pb
, st
->codec
, size
);
221 st
->need_parsing
= AVSTREAM_PARSE_FULL
;
223 av_set_pts_info(st
, 64, 1, st
->codec
->sample_rate
);
225 size
= find_tag(pb
, MKTAG('d', 'a', 't', 'a'));
230 wav
->data_end
= url_ftell(pb
) + size
;
234 /** Find chunk with w64 GUID by skipping over other chunks
235 * @return the size of the found chunk
237 static int64_t find_guid(ByteIOContext
*pb
, const uint8_t guid1
[16])
242 while (!url_feof(pb
)) {
243 get_buffer(pb
, guid
, 16);
247 if (!memcmp(guid
, guid1
, 16))
249 url_fskip(pb
, FFALIGN(size
, INT64_C(8)) - 24);
254 static const uint8_t guid_data
[16] = { 'd', 'a', 't', 'a',
255 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
257 #define MAX_SIZE 4096
259 static int wav_read_packet(AVFormatContext
*s
,
265 WAVContext
*wav
= s
->priv_data
;
269 left
= wav
->data_end
- url_ftell(s
->pb
);
271 if (CONFIG_W64_DEMUXER
&& wav
->w64
)
272 left
= find_guid(s
->pb
, guid_data
) - 24;
274 left
= find_tag(s
->pb
, MKTAG('d', 'a', 't', 'a'));
277 wav
->data_end
= url_ftell(s
->pb
) + left
;
281 if (st
->codec
->block_align
> 1) {
282 if (size
< st
->codec
->block_align
)
283 size
= st
->codec
->block_align
;
284 size
= (size
/ st
->codec
->block_align
) * st
->codec
->block_align
;
286 size
= FFMIN(size
, left
);
287 ret
= av_get_packet(s
->pb
, pkt
, size
);
290 pkt
->stream_index
= 0;
295 static int wav_read_seek(AVFormatContext
*s
,
296 int stream_index
, int64_t timestamp
, int flags
)
301 switch (st
->codec
->codec_id
) {
306 /* use generic seeking with dynamically generated indexes */
311 return pcm_read_seek(s
, stream_index
, timestamp
, flags
);
314 AVInputFormat wav_demuxer
= {
316 NULL_IF_CONFIG_SMALL("WAV format"),
323 .flags
= AVFMT_GENERIC_INDEX
,
324 .codec_tag
= (const AVCodecTag
* const []){ff_codec_wav_tags
, 0},
326 #endif /* CONFIG_WAV_DEMUXER */
329 #if CONFIG_W64_DEMUXER
330 static const uint8_t guid_riff
[16] = { 'r', 'i', 'f', 'f',
331 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
333 static const uint8_t guid_wave
[16] = { 'w', 'a', 'v', 'e',
334 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
336 static const uint8_t guid_fmt
[16] = { 'f', 'm', 't', ' ',
337 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
339 static int w64_probe(AVProbeData
*p
)
341 if (p
->buf_size
<= 40)
343 if (!memcmp(p
->buf
, guid_riff
, 16) &&
344 !memcmp(p
->buf
+ 24, guid_wave
, 16))
345 return AVPROBE_SCORE_MAX
;
350 static int w64_read_header(AVFormatContext
*s
, AVFormatParameters
*ap
)
353 ByteIOContext
*pb
= s
->pb
;
354 WAVContext
*wav
= s
->priv_data
;
358 get_buffer(pb
, guid
, 16);
359 if (memcmp(guid
, guid_riff
, 16))
362 if (get_le64(pb
) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
365 get_buffer(pb
, guid
, 16);
366 if (memcmp(guid
, guid_wave
, 16)) {
367 av_log(s
, AV_LOG_ERROR
, "could not find wave guid\n");
371 size
= find_guid(pb
, guid_fmt
);
373 av_log(s
, AV_LOG_ERROR
, "could not find fmt guid\n");
377 st
= av_new_stream(s
, 0);
379 return AVERROR(ENOMEM
);
381 /* subtract chunk header size - normal wav file doesn't count it */
382 ff_get_wav_header(pb
, st
->codec
, size
- 24);
383 url_fskip(pb
, FFALIGN(size
, INT64_C(8)) - size
);
385 st
->need_parsing
= AVSTREAM_PARSE_FULL
;
387 av_set_pts_info(st
, 64, 1, st
->codec
->sample_rate
);
389 size
= find_guid(pb
, guid_data
);
391 av_log(s
, AV_LOG_ERROR
, "could not find data guid\n");
394 wav
->data_end
= url_ftell(pb
) + size
- 24;
400 AVInputFormat w64_demuxer
= {
402 NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
409 .flags
= AVFMT_GENERIC_INDEX
,
410 .codec_tag
= (const AVCodecTag
* const []){ff_codec_wav_tags
, 0},
412 #endif /* CONFIG_W64_DEMUXER */