]> git.sesse.net Git - ffmpeg/blob - libavformat/wavenc.c
Merge commit '054013a0fc6f2b52c60cee3e051be8cc7f82cef3'
[ffmpeg] / libavformat / wavenc.c
1 /*
2  * WAV muxer
3  * Copyright (c) 2001, 2002 Fabrice Bellard
4  *
5  * Sony Wave64 muxer
6  * Copyright (c) 2012 Paul B Mahol
7  *
8  * WAV muxer RF64 support
9  * Copyright (c) 2013 Daniel Verkamp <daniel@drv.nu>
10  *
11  * This file is part of FFmpeg.
12  *
13  * FFmpeg is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public
15  * License as published by the Free Software Foundation; either
16  * version 2.1 of the License, or (at your option) any later version.
17  *
18  * FFmpeg is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with FFmpeg; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26  */
27
28 #include <stdint.h>
29 #include <string.h>
30
31 #include "libavutil/dict.h"
32 #include "libavutil/common.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/opt.h"
35
36 #include "avformat.h"
37 #include "avio.h"
38 #include "avio_internal.h"
39 #include "internal.h"
40 #include "riff.h"
41
42 #define RF64_AUTO   (-1)
43 #define RF64_NEVER  0
44 #define RF64_ALWAYS 1
45
46 typedef struct WAVMuxContext {
47     const AVClass *class;
48     int64_t data;
49     int64_t fact_pos;
50     int64_t ds64;
51     int64_t minpts;
52     int64_t maxpts;
53     int last_duration;
54     int write_bext;
55     int rf64;
56 } WAVMuxContext;
57
58 #if CONFIG_WAV_MUXER
59 static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
60 {
61     AVDictionaryEntry *tag;
62     int len = 0;
63
64     if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
65         len = strlen(tag->value);
66         len = FFMIN(len, maxlen);
67         avio_write(s->pb, tag->value, len);
68     }
69
70     ffio_fill(s->pb, 0, maxlen - len);
71 }
72
73 static void bwf_write_bext_chunk(AVFormatContext *s)
74 {
75     AVDictionaryEntry *tmp_tag;
76     uint64_t time_reference = 0;
77     int64_t bext = ff_start_tag(s->pb, "bext");
78
79     bwf_write_bext_string(s, "description", 256);
80     bwf_write_bext_string(s, "originator", 32);
81     bwf_write_bext_string(s, "originator_reference", 32);
82     bwf_write_bext_string(s, "origination_date", 10);
83     bwf_write_bext_string(s, "origination_time", 8);
84
85     if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0))
86         time_reference = strtoll(tmp_tag->value, NULL, 10);
87     avio_wl64(s->pb, time_reference);
88     avio_wl16(s->pb, 1);  // set version to 1
89
90     if (tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) {
91         unsigned char umidpart_str[17] = {0};
92         int i;
93         uint64_t umidpart;
94         int len = strlen(tmp_tag->value+2);
95
96         for (i = 0; i < len/16; i++) {
97             memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16);
98             umidpart = strtoll(umidpart_str, NULL, 16);
99             avio_wb64(s->pb, umidpart);
100         }
101         ffio_fill(s->pb, 0, 64 - i*8);
102     } else
103         ffio_fill(s->pb, 0, 64); // zero UMID
104
105     ffio_fill(s->pb, 0, 190); // Reserved
106
107     if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
108         avio_put_str(s->pb, tmp_tag->value);
109
110     ff_end_tag(s->pb, bext);
111 }
112
113 static int wav_write_header(AVFormatContext *s)
114 {
115     WAVMuxContext *wav = s->priv_data;
116     AVIOContext *pb = s->pb;
117     int64_t fmt;
118
119     if (s->nb_streams != 1) {
120         av_log(s, AV_LOG_ERROR, "WAVE files have exactly one stream\n");
121         return AVERROR(EINVAL);
122     }
123
124     if (wav->rf64 == RF64_ALWAYS) {
125         ffio_wfourcc(pb, "RF64");
126         avio_wl32(pb, -1); /* RF64 chunk size: use size in ds64 */
127     } else {
128         ffio_wfourcc(pb, "RIFF");
129         avio_wl32(pb, -1); /* file length */
130     }
131
132     ffio_wfourcc(pb, "WAVE");
133
134     if (wav->rf64 != RF64_NEVER) {
135         /* write empty ds64 chunk or JUNK chunk to reserve space for ds64 */
136         ffio_wfourcc(pb, wav->rf64 == RF64_ALWAYS ? "ds64" : "JUNK");
137         avio_wl32(pb, 28); /* chunk size */
138         wav->ds64 = avio_tell(pb);
139         ffio_fill(pb, 0, 28);
140     }
141
142     /* format header */
143     fmt = ff_start_tag(pb, "fmt ");
144     if (ff_put_wav_header(pb, s->streams[0]->codec, 0) < 0) {
145         const AVCodecDescriptor *desc = avcodec_descriptor_get(s->streams[0]->codec->codec_id);
146         av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
147                desc ? desc->name : "unknown");
148         return AVERROR(ENOSYS);
149     }
150     ff_end_tag(pb, fmt);
151
152     if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
153         && s->pb->seekable) {
154         wav->fact_pos = ff_start_tag(pb, "fact");
155         avio_wl32(pb, 0);
156         ff_end_tag(pb, wav->fact_pos);
157     }
158
159     if (wav->write_bext)
160         bwf_write_bext_chunk(s);
161
162     avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
163     wav->maxpts = wav->last_duration = 0;
164     wav->minpts = INT64_MAX;
165
166     /* info header */
167     ff_riff_write_info(s);
168
169     /* data header */
170     wav->data = ff_start_tag(pb, "data");
171
172     avio_flush(pb);
173
174     return 0;
175 }
176
177 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
178 {
179     AVIOContext *pb  = s->pb;
180     WAVMuxContext    *wav = s->priv_data;
181     avio_write(pb, pkt->data, pkt->size);
182     if(pkt->pts != AV_NOPTS_VALUE) {
183         wav->minpts        = FFMIN(wav->minpts, pkt->pts);
184         wav->maxpts        = FFMAX(wav->maxpts, pkt->pts);
185         wav->last_duration = pkt->duration;
186     } else
187         av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
188     return 0;
189 }
190
191 static int wav_write_trailer(AVFormatContext *s)
192 {
193     AVIOContext *pb  = s->pb;
194     WAVMuxContext    *wav = s->priv_data;
195     int64_t file_size, data_size;
196     int64_t number_of_samples = 0;
197     int rf64 = 0;
198
199     avio_flush(pb);
200
201     if (s->pb->seekable) {
202         /* update file size */
203         file_size = avio_tell(pb);
204         data_size = file_size - wav->data;
205         if (wav->rf64 == RF64_ALWAYS || (wav->rf64 == RF64_AUTO && file_size - 8 > UINT32_MAX)) {
206             rf64 = 1;
207         } else {
208             avio_seek(pb, 4, SEEK_SET);
209             avio_wl32(pb, (uint32_t)(file_size - 8));
210             avio_seek(pb, file_size, SEEK_SET);
211
212             ff_end_tag(pb, wav->data);
213             avio_flush(pb);
214         }
215
216         number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
217                                        s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
218                                        s->streams[0]->time_base.den);
219
220         if(s->streams[0]->codec->codec_tag != 0x01) {
221             /* Update num_samps in fact chunk */
222             avio_seek(pb, wav->fact_pos, SEEK_SET);
223             if (rf64 || (wav->rf64 == RF64_AUTO && number_of_samples > UINT32_MAX)) {
224                 rf64 = 1;
225                 avio_wl32(pb, -1);
226             } else {
227                 avio_wl32(pb, number_of_samples);
228                 avio_seek(pb, file_size, SEEK_SET);
229                 avio_flush(pb);
230             }
231         }
232
233         if (rf64) {
234             /* overwrite RIFF with RF64 */
235             avio_seek(pb, 0, SEEK_SET);
236             ffio_wfourcc(pb, "RF64");
237             avio_wl32(pb, -1);
238
239             /* write ds64 chunk (overwrite JUNK if rf64 == RF64_AUTO) */
240             avio_seek(pb, wav->ds64 - 8, SEEK_SET);
241             ffio_wfourcc(pb, "ds64");
242             avio_wl32(pb, 28);                  /* ds64 chunk size */
243             avio_wl64(pb, file_size - 8);       /* RF64 chunk size */
244             avio_wl64(pb, data_size);           /* data chunk size */
245             avio_wl64(pb, number_of_samples);   /* fact chunk number of samples */
246             avio_wl32(pb, 0);                   /* number of table entries for non-'data' chunks */
247
248             /* write -1 in data chunk size */
249             avio_seek(pb, wav->data - 4, SEEK_SET);
250             avio_wl32(pb, -1);
251
252             avio_seek(pb, file_size, SEEK_SET);
253             avio_flush(pb);
254         }
255     }
256     return 0;
257 }
258
259 #define OFFSET(x) offsetof(WAVMuxContext, x)
260 #define ENC AV_OPT_FLAG_ENCODING_PARAM
261 static const AVOption options[] = {
262     { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
263     { "rf64",       "Use RF64 header rather than RIFF for large files.",    OFFSET(rf64), AV_OPT_TYPE_INT,   { .i64 = RF64_NEVER  },-1, 1, ENC, "rf64" },
264     { "auto",       "Write RF64 header if file grows large enough.",        0,            AV_OPT_TYPE_CONST, { .i64 = RF64_AUTO   }, 0, 0, ENC, "rf64" },
265     { "always",     "Always write RF64 header regardless of file size.",    0,            AV_OPT_TYPE_CONST, { .i64 = RF64_ALWAYS }, 0, 0, ENC, "rf64" },
266     { "never",      "Never write RF64 header regardless of file size.",     0,            AV_OPT_TYPE_CONST, { .i64 = RF64_NEVER  }, 0, 0, ENC, "rf64" },
267     { NULL },
268 };
269
270 static const AVClass wav_muxer_class = {
271     .class_name = "WAV muxer",
272     .item_name  = av_default_item_name,
273     .option     = options,
274     .version    = LIBAVUTIL_VERSION_INT,
275 };
276
277 AVOutputFormat ff_wav_muxer = {
278     .name              = "wav",
279     .long_name         = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
280     .mime_type         = "audio/x-wav",
281     .extensions        = "wav",
282     .priv_data_size    = sizeof(WAVMuxContext),
283     .audio_codec       = AV_CODEC_ID_PCM_S16LE,
284     .video_codec       = AV_CODEC_ID_NONE,
285     .write_header      = wav_write_header,
286     .write_packet      = wav_write_packet,
287     .write_trailer     = wav_write_trailer,
288     .flags             = AVFMT_TS_NONSTRICT,
289     .codec_tag         = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
290     .priv_class        = &wav_muxer_class,
291 };
292 #endif /* CONFIG_WAV_MUXER */
293
294 #if CONFIG_W64_MUXER
295 #include "w64.h"
296
297 static void start_guid(AVIOContext *pb, const uint8_t *guid, int64_t *pos)
298 {
299     *pos = avio_tell(pb);
300
301     avio_write(pb, guid, 16);
302     avio_wl64(pb, INT64_MAX);
303 }
304
305 static void end_guid(AVIOContext *pb, int64_t start)
306 {
307     int64_t end, pos = avio_tell(pb);
308
309     end = FFALIGN(pos, 8);
310     ffio_fill(pb, 0, end - pos);
311     avio_seek(pb, start + 16, SEEK_SET);
312     avio_wl64(pb, end - start);
313     avio_seek(pb, end, SEEK_SET);
314 }
315
316 static int w64_write_header(AVFormatContext *s)
317 {
318     WAVMuxContext *wav = s->priv_data;
319     AVIOContext *pb = s->pb;
320     int64_t start;
321     int ret;
322
323     avio_write(pb, ff_w64_guid_riff, sizeof(ff_w64_guid_riff));
324     avio_wl64(pb, -1);
325     avio_write(pb, ff_w64_guid_wave, sizeof(ff_w64_guid_wave));
326     start_guid(pb, ff_w64_guid_fmt, &start);
327     if ((ret = ff_put_wav_header(pb, s->streams[0]->codec, 0)) < 0) {
328         av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
329                s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
330         return ret;
331     }
332     end_guid(pb, start);
333
334     if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
335         && s->pb->seekable) {
336         start_guid(pb, ff_w64_guid_fact, &wav->fact_pos);
337         avio_wl64(pb, 0);
338         end_guid(pb, wav->fact_pos);
339     }
340
341     start_guid(pb, ff_w64_guid_data, &wav->data);
342
343     return 0;
344 }
345
346 static int w64_write_trailer(AVFormatContext *s)
347 {
348     AVIOContext    *pb = s->pb;
349     WAVMuxContext *wav = s->priv_data;
350     int64_t file_size;
351
352     if (pb->seekable) {
353         end_guid(pb, wav->data);
354
355         file_size = avio_tell(pb);
356         avio_seek(pb, 16, SEEK_SET);
357         avio_wl64(pb, file_size);
358
359         if (s->streams[0]->codec->codec_tag != 0x01) {
360             int64_t number_of_samples;
361
362             number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
363                                            s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
364                                            s->streams[0]->time_base.den);
365             avio_seek(pb, wav->fact_pos + 24, SEEK_SET);
366             avio_wl64(pb, number_of_samples);
367         }
368
369         avio_seek(pb, file_size, SEEK_SET);
370         avio_flush(pb);
371     }
372
373     return 0;
374 }
375
376 AVOutputFormat ff_w64_muxer = {
377     .name              = "w64",
378     .long_name         = NULL_IF_CONFIG_SMALL("Sony Wave64"),
379     .extensions        = "w64",
380     .priv_data_size    = sizeof(WAVMuxContext),
381     .audio_codec       = AV_CODEC_ID_PCM_S16LE,
382     .video_codec       = AV_CODEC_ID_NONE,
383     .write_header      = w64_write_header,
384     .write_packet      = wav_write_packet,
385     .write_trailer     = w64_write_trailer,
386     .flags             = AVFMT_TS_NONSTRICT,
387     .codec_tag         = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
388 };
389 #endif /* CONFIG_W64_MUXER */