Commit | Line | Data |
---|---|---|
115329f1 | 1 | /* |
7fbde343 | 2 | * WAV muxer and demuxer |
406792e7 | 3 | * Copyright (c) 2001, 2002 Fabrice Bellard |
de6d9b64 | 4 | * |
740e89cf DV |
5 | * Sony Wave64 demuxer |
6 | * Copyright (c) 2009 Daniel Verkamp | |
7 | * | |
b78e7197 DB |
8 | * This file is part of FFmpeg. |
9 | * | |
10 | * FFmpeg is free software; you can redistribute it and/or | |
19720f15 FB |
11 | * modify it under the terms of the GNU Lesser General Public |
12 | * License as published by the Free Software Foundation; either | |
b78e7197 | 13 | * version 2.1 of the License, or (at your option) any later version. |
de6d9b64 | 14 | * |
b78e7197 | 15 | * FFmpeg is distributed in the hope that it will be useful, |
de6d9b64 | 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19720f15 FB |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 | * Lesser General Public License for more details. | |
de6d9b64 | 19 | * |
19720f15 | 20 | * You should have received a copy of the GNU Lesser General Public |
b78e7197 | 21 | * License along with FFmpeg; if not, write to the Free Software |
5509bffa | 22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
de6d9b64 | 23 | */ |
de6d9b64 | 24 | #include "avformat.h" |
7241a6bb | 25 | #include "raw.h" |
9d9f4119 | 26 | #include "riff.h" |
46a3d068 | 27 | |
de6d9b64 | 28 | typedef struct { |
bc5c918e DB |
29 | int64_t data; |
30 | int64_t data_end; | |
d794d388 MB |
31 | int64_t minpts; |
32 | int64_t maxpts; | |
33 | int last_duration; | |
740e89cf | 34 | int w64; |
de6d9b64 FB |
35 | } WAVContext; |
36 | ||
b250f9c6 | 37 | #if CONFIG_WAV_MUXER |
de6d9b64 FB |
38 | static int wav_write_header(AVFormatContext *s) |
39 | { | |
c9a65ca8 | 40 | WAVContext *wav = s->priv_data; |
899681cd | 41 | ByteIOContext *pb = s->pb; |
bc5c918e | 42 | int64_t fmt, fact; |
de6d9b64 | 43 | |
de6d9b64 FB |
44 | put_tag(pb, "RIFF"); |
45 | put_le32(pb, 0); /* file length */ | |
46 | put_tag(pb, "WAVE"); | |
47 | ||
48 | /* format header */ | |
1a40491e DV |
49 | fmt = ff_start_tag(pb, "fmt "); |
50 | if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) { | |
5165651c | 51 | av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n", |
f2d65a6c | 52 | s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE"); |
1ea4f593 | 53 | av_free(wav); |
46a3d068 FB |
54 | return -1; |
55 | } | |
1a40491e | 56 | ff_end_tag(pb, fmt); |
de6d9b64 | 57 | |
c5e86106 DB |
58 | if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */ |
59 | && !url_is_streamed(s->pb)) { | |
1a40491e | 60 | fact = ff_start_tag(pb, "fact"); |
d794d388 | 61 | put_le32(pb, 0); |
1a40491e | 62 | ff_end_tag(pb, fact); |
d794d388 MB |
63 | } |
64 | ||
01f4895c | 65 | av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate); |
d794d388 MB |
66 | wav->maxpts = wav->last_duration = 0; |
67 | wav->minpts = INT64_MAX; | |
a1e568bd | 68 | |
de6d9b64 | 69 | /* data header */ |
1a40491e | 70 | wav->data = ff_start_tag(pb, "data"); |
115329f1 | 71 | |
de6d9b64 FB |
72 | put_flush_packet(pb); |
73 | ||
74 | return 0; | |
75 | } | |
76 | ||
e928649b | 77 | static int wav_write_packet(AVFormatContext *s, AVPacket *pkt) |
de6d9b64 | 78 | { |
c5e86106 DB |
79 | ByteIOContext *pb = s->pb; |
80 | WAVContext *wav = s->priv_data; | |
e928649b | 81 | put_buffer(pb, pkt->data, pkt->size); |
d794d388 | 82 | if(pkt->pts != AV_NOPTS_VALUE) { |
c5e86106 DB |
83 | wav->minpts = FFMIN(wav->minpts, pkt->pts); |
84 | wav->maxpts = FFMAX(wav->maxpts, pkt->pts); | |
d794d388 MB |
85 | wav->last_duration = pkt->duration; |
86 | } else | |
87 | av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n"); | |
de6d9b64 FB |
88 | return 0; |
89 | } | |
90 | ||
91 | static int wav_write_trailer(AVFormatContext *s) | |
92 | { | |
c5e86106 DB |
93 | ByteIOContext *pb = s->pb; |
94 | WAVContext *wav = s->priv_data; | |
bc5c918e | 95 | int64_t file_size; |
de6d9b64 | 96 | |
899681cd | 97 | if (!url_is_streamed(s->pb)) { |
1a40491e | 98 | ff_end_tag(pb, wav->data); |
de6d9b64 FB |
99 | |
100 | /* update file size */ | |
101 | file_size = url_ftell(pb); | |
102 | url_fseek(pb, 4, SEEK_SET); | |
0c1a9eda | 103 | put_le32(pb, (uint32_t)(file_size - 8)); |
de6d9b64 FB |
104 | url_fseek(pb, file_size, SEEK_SET); |
105 | ||
106 | put_flush_packet(pb); | |
d794d388 MB |
107 | |
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); | |
118 | } | |
de6d9b64 | 119 | } |
de6d9b64 FB |
120 | return 0; |
121 | } | |
8212568a | 122 | #endif /* CONFIG_WAV_MUXER */ |
de6d9b64 FB |
123 | |
124 | /* return the size of the found tag */ | |
502d6c0a | 125 | static int64_t find_tag(ByteIOContext *pb, uint32_t tag1) |
de6d9b64 FB |
126 | { |
127 | unsigned int tag; | |
502d6c0a | 128 | int64_t size; |
de6d9b64 | 129 | |
c5e86106 | 130 | for (;;) { |
de6d9b64 FB |
131 | if (url_feof(pb)) |
132 | return -1; | |
c5e86106 | 133 | tag = get_le32(pb); |
de6d9b64 FB |
134 | size = get_le32(pb); |
135 | if (tag == tag1) | |
136 | break; | |
137 | url_fseek(pb, size, SEEK_CUR); | |
138 | } | |
de6d9b64 FB |
139 | return size; |
140 | } | |
141 | ||
c9a65ca8 FB |
142 | static int wav_probe(AVProbeData *p) |
143 | { | |
144 | /* check file header */ | |
145 | if (p->buf_size <= 32) | |
146 | return 0; | |
c5e86106 DB |
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' && | |
c9a65ca8 | 150 | p->buf[10] == 'V' && p->buf[11] == 'E') |
c6cd525a VV |
151 | /* |
152 | Since ACT demuxer has standard WAV header at top of it's own, | |
153 | returning score is decreased to avoid probe conflict | |
154 | between ACT and WAV. | |
155 | */ | |
156 | return AVPROBE_SCORE_MAX - 1; | |
c9a65ca8 FB |
157 | else |
158 | return 0; | |
159 | } | |
160 | ||
de6d9b64 FB |
161 | /* wav input */ |
162 | static int wav_read_header(AVFormatContext *s, | |
163 | AVFormatParameters *ap) | |
164 | { | |
502d6c0a | 165 | int64_t size; |
de6d9b64 | 166 | unsigned int tag; |
899681cd | 167 | ByteIOContext *pb = s->pb; |
de6d9b64 | 168 | AVStream *st; |
b582d7ba | 169 | WAVContext *wav = s->priv_data; |
de6d9b64 FB |
170 | |
171 | /* check RIFF header */ | |
172 | tag = get_le32(pb); | |
173 | ||
174 | if (tag != MKTAG('R', 'I', 'F', 'F')) | |
175 | return -1; | |
176 | get_le32(pb); /* file size */ | |
177 | tag = get_le32(pb); | |
178 | if (tag != MKTAG('W', 'A', 'V', 'E')) | |
179 | return -1; | |
115329f1 | 180 | |
de6d9b64 FB |
181 | /* parse fmt header */ |
182 | size = find_tag(pb, MKTAG('f', 'm', 't', ' ')); | |
183 | if (size < 0) | |
184 | return -1; | |
c9a65ca8 | 185 | st = av_new_stream(s, 0); |
de6d9b64 | 186 | if (!st) |
769e10f0 | 187 | return AVERROR(ENOMEM); |
de6d9b64 | 188 | |
1a40491e | 189 | ff_get_wav_header(pb, st->codec, size); |
57004ff1 | 190 | st->need_parsing = AVSTREAM_PARSE_FULL; |
595bf4ef | 191 | |
01f4895c | 192 | av_set_pts_info(st, 64, 1, st->codec->sample_rate); |
595bf4ef | 193 | |
e095026a FB |
194 | size = find_tag(pb, MKTAG('d', 'a', 't', 'a')); |
195 | if (size < 0) | |
196 | return -1; | |
b582d7ba | 197 | wav->data_end= url_ftell(pb) + size; |
de6d9b64 FB |
198 | return 0; |
199 | } | |
200 | ||
740e89cf DV |
201 | #if CONFIG_W64_DEMUXER |
202 | ||
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 }; | |
205 | ||
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 }; | |
208 | ||
209 | static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ', | |
210 | 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A }; | |
211 | ||
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 }; | |
214 | ||
215 | static int w64_probe(AVProbeData *p) | |
216 | { | |
217 | if (p->buf_size <= 40) | |
218 | return 0; | |
219 | if (!memcmp(p->buf, guid_riff, 16) && | |
c5e86106 | 220 | !memcmp(p->buf + 24, guid_wave, 16)) |
740e89cf | 221 | return AVPROBE_SCORE_MAX; |
c5e86106 | 222 | else |
740e89cf DV |
223 | return 0; |
224 | } | |
225 | ||
226 | /** Find chunk with w64 GUID by skipping over other chunks | |
227 | * @return the size of the found chunk | |
228 | */ | |
229 | static int64_t find_guid(ByteIOContext *pb, const uint8_t guid1[16]) | |
230 | { | |
231 | uint8_t guid[16]; | |
232 | int64_t size; | |
233 | ||
234 | while (!url_feof(pb)) { | |
235 | get_buffer(pb, guid, 16); | |
236 | size = get_le64(pb); | |
237 | if (size <= 24) | |
238 | return -1; | |
239 | if (!memcmp(guid, guid1, 16)) | |
240 | return size; | |
241 | url_fskip(pb, FFALIGN(size, INT64_C(8)) - 24); | |
242 | } | |
243 | return -1; | |
244 | } | |
245 | ||
246 | static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap) | |
247 | { | |
248 | int64_t size; | |
c5e86106 DB |
249 | ByteIOContext *pb = s->pb; |
250 | WAVContext *wav = s->priv_data; | |
740e89cf DV |
251 | AVStream *st; |
252 | uint8_t guid[16]; | |
253 | ||
254 | get_buffer(pb, guid, 16); | |
255 | if (memcmp(guid, guid_riff, 16)) | |
256 | return -1; | |
257 | ||
258 | if (get_le64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */ | |
259 | return -1; | |
260 | ||
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"); | |
264 | return -1; | |
265 | } | |
266 | ||
267 | size = find_guid(pb, guid_fmt); | |
268 | if (size < 0) { | |
269 | av_log(s, AV_LOG_ERROR, "could not find fmt guid\n"); | |
270 | return -1; | |
271 | } | |
272 | ||
273 | st = av_new_stream(s, 0); | |
274 | if (!st) | |
275 | return AVERROR(ENOMEM); | |
276 | ||
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); | |
280 | ||
281 | st->need_parsing = AVSTREAM_PARSE_FULL; | |
282 | ||
283 | av_set_pts_info(st, 64, 1, st->codec->sample_rate); | |
284 | ||
285 | size = find_guid(pb, guid_data); | |
286 | if (size < 0) { | |
287 | av_log(s, AV_LOG_ERROR, "could not find data guid\n"); | |
288 | return -1; | |
289 | } | |
290 | wav->data_end = url_ftell(pb) + size - 24; | |
c5e86106 | 291 | wav->w64 = 1; |
740e89cf DV |
292 | |
293 | return 0; | |
294 | } | |
295 | #endif /* CONFIG_W64_DEMUXER */ | |
296 | ||
de6d9b64 FB |
297 | #define MAX_SIZE 4096 |
298 | ||
299 | static int wav_read_packet(AVFormatContext *s, | |
300 | AVPacket *pkt) | |
301 | { | |
740e89cf DV |
302 | int ret, size; |
303 | int64_t left; | |
f9b5459e | 304 | AVStream *st; |
b582d7ba | 305 | WAVContext *wav = s->priv_data; |
de6d9b64 | 306 | |
899681cd | 307 | if (url_feof(s->pb)) |
6f3e0b21 | 308 | return AVERROR(EIO); |
f9b5459e FB |
309 | st = s->streams[0]; |
310 | ||
c5e86106 DB |
311 | left = wav->data_end - url_ftell(s->pb); |
312 | if (left <= 0){ | |
313 | if (CONFIG_W64_DEMUXER && wav->w64) | |
740e89cf | 314 | left = find_guid(s->pb, guid_data) - 24; |
c5e86106 DB |
315 | else |
316 | left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a')); | |
317 | if (left < 0) | |
6f3e0b21 | 318 | return AVERROR(EIO); |
899681cd | 319 | wav->data_end= url_ftell(s->pb) + left; |
b582d7ba MN |
320 | } |
321 | ||
f9b5459e | 322 | size = MAX_SIZE; |
01f4895c MN |
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; | |
f9b5459e | 327 | } |
c5e86106 DB |
328 | size = FFMIN(size, left); |
329 | ret = av_get_packet(s->pb, pkt, size); | |
e9b78eeb | 330 | if (ret <= 0) |
6f3e0b21 | 331 | return AVERROR(EIO); |
de6d9b64 FB |
332 | pkt->stream_index = 0; |
333 | ||
5ed8fafc FB |
334 | /* note: we need to modify the packet size here to handle the last |
335 | packet */ | |
336 | pkt->size = ret; | |
de6d9b64 FB |
337 | return ret; |
338 | } | |
339 | ||
115329f1 | 340 | static int wav_read_seek(AVFormatContext *s, |
7b3c1382 | 341 | int stream_index, int64_t timestamp, int flags) |
f9b5459e FB |
342 | { |
343 | AVStream *st; | |
344 | ||
345 | st = s->streams[0]; | |
c5e86106 | 346 | switch (st->codec->codec_id) { |
f9b5459e FB |
347 | case CODEC_ID_MP2: |
348 | case CODEC_ID_MP3: | |
349 | case CODEC_ID_AC3: | |
23c99253 | 350 | case CODEC_ID_DTS: |
f9b5459e FB |
351 | /* use generic seeking with dynamically generated indexes */ |
352 | return -1; | |
353 | default: | |
354 | break; | |
355 | } | |
7b3c1382 | 356 | return pcm_read_seek(s, stream_index, timestamp, flags); |
f9b5459e FB |
357 | } |
358 | ||
b250f9c6 | 359 | #if CONFIG_WAV_DEMUXER |
ff70e601 | 360 | AVInputFormat wav_demuxer = { |
c9a65ca8 | 361 | "wav", |
bde15e74 | 362 | NULL_IF_CONFIG_SMALL("WAV format"), |
b582d7ba | 363 | sizeof(WAVContext), |
c9a65ca8 FB |
364 | wav_probe, |
365 | wav_read_header, | |
366 | wav_read_packet, | |
9b64a036 | 367 | NULL, |
f9b5459e | 368 | wav_read_seek, |
e9b78eeb | 369 | .flags= AVFMT_GENERIC_INDEX, |
1a40491e | 370 | .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0}, |
c9a65ca8 | 371 | }; |
ff70e601 | 372 | #endif |
c5e86106 | 373 | |
b250f9c6 | 374 | #if CONFIG_WAV_MUXER |
ff70e601 | 375 | AVOutputFormat wav_muxer = { |
de6d9b64 | 376 | "wav", |
bde15e74 | 377 | NULL_IF_CONFIG_SMALL("WAV format"), |
de6d9b64 FB |
378 | "audio/x-wav", |
379 | "wav", | |
c9a65ca8 | 380 | sizeof(WAVContext), |
5ed8fafc | 381 | CODEC_ID_PCM_S16LE, |
de6d9b64 FB |
382 | CODEC_ID_NONE, |
383 | wav_write_header, | |
384 | wav_write_packet, | |
385 | wav_write_trailer, | |
1a40491e | 386 | .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0}, |
de6d9b64 | 387 | }; |
ff70e601 | 388 | #endif |
c5e86106 | 389 | |
740e89cf DV |
390 | #if CONFIG_W64_DEMUXER |
391 | AVInputFormat w64_demuxer = { | |
392 | "w64", | |
393 | NULL_IF_CONFIG_SMALL("Sony Wave64 format"), | |
394 | sizeof(WAVContext), | |
395 | w64_probe, | |
396 | w64_read_header, | |
397 | wav_read_packet, | |
398 | NULL, | |
399 | wav_read_seek, | |
400 | .flags = AVFMT_GENERIC_INDEX, | |
401 | .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0}, | |
402 | }; | |
403 | #endif |