3 * Copyright (c) 2001, 2002 Fabrice Bellard
6 * Copyright (c) 2012 Paul B Mahol
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
28 #include "libavutil/dict.h"
29 #include "libavutil/common.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/opt.h"
35 #include "avio_internal.h"
39 typedef struct WAVMuxContext {
50 static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
52 AVDictionaryEntry *tag;
55 if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
56 len = strlen(tag->value);
57 len = FFMIN(len, maxlen);
58 avio_write(s->pb, tag->value, len);
61 ffio_fill(s->pb, 0, maxlen - len);
64 static void bwf_write_bext_chunk(AVFormatContext *s)
66 AVDictionaryEntry *tmp_tag;
67 uint64_t time_reference = 0;
68 int64_t bext = ff_start_tag(s->pb, "bext");
70 bwf_write_bext_string(s, "description", 256);
71 bwf_write_bext_string(s, "originator", 32);
72 bwf_write_bext_string(s, "originator_reference", 32);
73 bwf_write_bext_string(s, "origination_date", 10);
74 bwf_write_bext_string(s, "origination_time", 8);
76 if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0))
77 time_reference = strtoll(tmp_tag->value, NULL, 10);
78 avio_wl64(s->pb, time_reference);
79 avio_wl16(s->pb, 1); // set version to 1
81 if (tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) {
82 unsigned char umidpart_str[17] = {0};
85 int len = strlen(tmp_tag->value+2);
87 for (i = 0; i < len/16; i++) {
88 memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16);
89 umidpart = strtoll(umidpart_str, NULL, 16);
90 avio_wb64(s->pb, umidpart);
92 ffio_fill(s->pb, 0, 64 - i*8);
94 ffio_fill(s->pb, 0, 64); // zero UMID
96 ffio_fill(s->pb, 0, 190); // Reserved
98 if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
99 avio_put_str(s->pb, tmp_tag->value);
101 ff_end_tag(s->pb, bext);
104 static int wav_write_header(AVFormatContext *s)
106 WAVMuxContext *wav = s->priv_data;
107 AVIOContext *pb = s->pb;
110 ffio_wfourcc(pb, "RIFF");
111 avio_wl32(pb, 0); /* file length */
112 ffio_wfourcc(pb, "WAVE");
115 fmt = ff_start_tag(pb, "fmt ");
116 if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
117 av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
118 s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
123 if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
124 && s->pb->seekable) {
125 wav->fact_pos = ff_start_tag(pb, "fact");
127 ff_end_tag(pb, wav->fact_pos);
131 bwf_write_bext_chunk(s);
133 avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
134 wav->maxpts = wav->last_duration = 0;
135 wav->minpts = INT64_MAX;
138 ff_riff_write_info(s);
141 wav->data = ff_start_tag(pb, "data");
148 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
150 AVIOContext *pb = s->pb;
151 WAVMuxContext *wav = s->priv_data;
152 avio_write(pb, pkt->data, pkt->size);
153 if(pkt->pts != AV_NOPTS_VALUE) {
154 wav->minpts = FFMIN(wav->minpts, pkt->pts);
155 wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
156 wav->last_duration = pkt->duration;
158 av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
162 static int wav_write_trailer(AVFormatContext *s)
164 AVIOContext *pb = s->pb;
165 WAVMuxContext *wav = s->priv_data;
170 if (s->pb->seekable) {
171 ff_end_tag(pb, wav->data);
173 /* update file size */
174 file_size = avio_tell(pb);
175 avio_seek(pb, 4, SEEK_SET);
176 avio_wl32(pb, (uint32_t)(file_size - 8));
177 avio_seek(pb, file_size, SEEK_SET);
181 if(s->streams[0]->codec->codec_tag != 0x01) {
182 /* Update num_samps in fact chunk */
183 int number_of_samples;
184 number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
185 s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
186 s->streams[0]->time_base.den);
187 avio_seek(pb, wav->fact_pos, SEEK_SET);
188 avio_wl32(pb, number_of_samples);
189 avio_seek(pb, file_size, SEEK_SET);
196 #define OFFSET(x) offsetof(WAVMuxContext, x)
197 #define ENC AV_OPT_FLAG_ENCODING_PARAM
198 static const AVOption options[] = {
199 { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
203 static const AVClass wav_muxer_class = {
204 .class_name = "WAV muxer",
205 .item_name = av_default_item_name,
207 .version = LIBAVUTIL_VERSION_INT,
210 AVOutputFormat ff_wav_muxer = {
212 .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
213 .mime_type = "audio/x-wav",
215 .priv_data_size = sizeof(WAVMuxContext),
216 .audio_codec = AV_CODEC_ID_PCM_S16LE,
217 .video_codec = AV_CODEC_ID_NONE,
218 .write_header = wav_write_header,
219 .write_packet = wav_write_packet,
220 .write_trailer = wav_write_trailer,
221 .flags = AVFMT_TS_NONSTRICT,
222 .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
223 .priv_class = &wav_muxer_class,
225 #endif /* CONFIG_WAV_MUXER */
230 static void start_guid(AVIOContext *pb, const uint8_t *guid, int64_t *pos)
232 *pos = avio_tell(pb);
234 avio_write(pb, guid, 16);
235 avio_wl64(pb, INT64_MAX);
238 static void end_guid(AVIOContext *pb, int64_t start)
240 int64_t end, pos = avio_tell(pb);
242 end = FFALIGN(pos, 8);
243 ffio_fill(pb, 0, end - pos);
244 avio_seek(pb, start + 16, SEEK_SET);
245 avio_wl64(pb, end - start);
246 avio_seek(pb, end, SEEK_SET);
249 static int w64_write_header(AVFormatContext *s)
251 WAVMuxContext *wav = s->priv_data;
252 AVIOContext *pb = s->pb;
256 avio_write(pb, ff_w64_guid_riff, sizeof(ff_w64_guid_riff));
258 avio_write(pb, ff_w64_guid_wave, sizeof(ff_w64_guid_wave));
259 start_guid(pb, ff_w64_guid_fmt, &start);
260 if ((ret = ff_put_wav_header(pb, s->streams[0]->codec)) < 0) {
261 av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
262 s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
267 if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
268 && s->pb->seekable) {
269 start_guid(pb, ff_w64_guid_fact, &wav->fact_pos);
271 end_guid(pb, wav->fact_pos);
274 start_guid(pb, ff_w64_guid_data, &wav->data);
279 static int w64_write_trailer(AVFormatContext *s)
281 AVIOContext *pb = s->pb;
282 WAVMuxContext *wav = s->priv_data;
286 end_guid(pb, wav->data);
288 file_size = avio_tell(pb);
289 avio_seek(pb, 16, SEEK_SET);
290 avio_wl64(pb, file_size);
292 if (s->streams[0]->codec->codec_tag != 0x01) {
293 int64_t number_of_samples;
295 number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
296 s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
297 s->streams[0]->time_base.den);
298 avio_seek(pb, wav->fact_pos + 24, SEEK_SET);
299 avio_wl64(pb, number_of_samples);
302 avio_seek(pb, file_size, SEEK_SET);
309 AVOutputFormat ff_w64_muxer = {
311 .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
313 .priv_data_size = sizeof(WAVMuxContext),
314 .audio_codec = AV_CODEC_ID_PCM_S16LE,
315 .video_codec = AV_CODEC_ID_NONE,
316 .write_header = w64_write_header,
317 .write_packet = wav_write_packet,
318 .write_trailer = w64_write_trailer,
319 .flags = AVFMT_TS_NONSTRICT,
320 .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
322 #endif /* CONFIG_W64_MUXER */