]> git.sesse.net Git - ffmpeg/blob - libavformat/wavenc.c
Merge remote-tracking branch 'qatar/master'
[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  * This file is part of FFmpeg.
9  *
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.
14  *
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.
19  *
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
23  */
24
25 #include <stdint.h>
26 #include <string.h>
27
28 #include "libavutil/dict.h"
29 #include "libavutil/common.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/opt.h"
32
33 #include "avformat.h"
34 #include "avio.h"
35 #include "avio_internal.h"
36 #include "internal.h"
37 #include "riff.h"
38
39 typedef struct WAVMuxContext {
40     const AVClass *class;
41     int64_t data;
42     int64_t fact_pos;
43     int64_t minpts;
44     int64_t maxpts;
45     int last_duration;
46     int write_bext;
47 } WAVMuxContext;
48
49 #if CONFIG_WAV_MUXER
50 static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
51 {
52     AVDictionaryEntry *tag;
53     int len = 0;
54
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);
59     }
60
61     ffio_fill(s->pb, 0, maxlen - len);
62 }
63
64 static void bwf_write_bext_chunk(AVFormatContext *s)
65 {
66     AVDictionaryEntry *tmp_tag;
67     uint64_t time_reference = 0;
68     int64_t bext = ff_start_tag(s->pb, "bext");
69
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);
75
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
80
81     if (tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) {
82         unsigned char umidpart_str[17] = {0};
83         int i;
84         uint64_t umidpart;
85         int len = strlen(tmp_tag->value+2);
86
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);
91         }
92         ffio_fill(s->pb, 0, 64 - i*8);
93     } else
94         ffio_fill(s->pb, 0, 64); // zero UMID
95
96     ffio_fill(s->pb, 0, 190); // Reserved
97
98     if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
99         avio_put_str(s->pb, tmp_tag->value);
100
101     ff_end_tag(s->pb, bext);
102 }
103
104 static int wav_write_header(AVFormatContext *s)
105 {
106     WAVMuxContext *wav = s->priv_data;
107     AVIOContext *pb = s->pb;
108     int64_t fmt;
109
110     ffio_wfourcc(pb, "RIFF");
111     avio_wl32(pb, 0); /* file length */
112     ffio_wfourcc(pb, "WAVE");
113
114     /* format header */
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");
119         return -1;
120     }
121     ff_end_tag(pb, fmt);
122
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");
126         avio_wl32(pb, 0);
127         ff_end_tag(pb, wav->fact_pos);
128     }
129
130     if (wav->write_bext)
131         bwf_write_bext_chunk(s);
132
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;
136
137     /* info header */
138     ff_riff_write_info(s);
139
140     /* data header */
141     wav->data = ff_start_tag(pb, "data");
142
143     avio_flush(pb);
144
145     return 0;
146 }
147
148 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
149 {
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;
157     } else
158         av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
159     return 0;
160 }
161
162 static int wav_write_trailer(AVFormatContext *s)
163 {
164     AVIOContext *pb  = s->pb;
165     WAVMuxContext    *wav = s->priv_data;
166     int64_t file_size;
167
168     avio_flush(pb);
169
170     if (s->pb->seekable) {
171         ff_end_tag(pb, wav->data);
172
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);
178
179         avio_flush(pb);
180
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);
190             avio_flush(pb);
191         }
192     }
193     return 0;
194 }
195
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 },
200     { NULL },
201 };
202
203 static const AVClass wav_muxer_class = {
204     .class_name = "WAV muxer",
205     .item_name  = av_default_item_name,
206     .option     = options,
207     .version    = LIBAVUTIL_VERSION_INT,
208 };
209
210 AVOutputFormat ff_wav_muxer = {
211     .name              = "wav",
212     .long_name         = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
213     .mime_type         = "audio/x-wav",
214     .extensions        = "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,
224 };
225 #endif /* CONFIG_WAV_MUXER */
226
227 #if CONFIG_W64_MUXER
228 #include "w64.h"
229
230 static void start_guid(AVIOContext *pb, const uint8_t *guid, int64_t *pos)
231 {
232     *pos = avio_tell(pb);
233
234     avio_write(pb, guid, 16);
235     avio_wl64(pb, INT64_MAX);
236 }
237
238 static void end_guid(AVIOContext *pb, int64_t start)
239 {
240     int64_t end, pos = avio_tell(pb);
241
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);
247 }
248
249 static int w64_write_header(AVFormatContext *s)
250 {
251     WAVMuxContext *wav = s->priv_data;
252     AVIOContext *pb = s->pb;
253     int64_t start;
254     int ret;
255
256     avio_write(pb, ff_w64_guid_riff, sizeof(ff_w64_guid_riff));
257     avio_wl64(pb, -1);
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");
263         return ret;
264     }
265     end_guid(pb, start);
266
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);
270         avio_wl64(pb, 0);
271         end_guid(pb, wav->fact_pos);
272     }
273
274     start_guid(pb, ff_w64_guid_data, &wav->data);
275
276     return 0;
277 }
278
279 static int w64_write_trailer(AVFormatContext *s)
280 {
281     AVIOContext    *pb = s->pb;
282     WAVMuxContext *wav = s->priv_data;
283     int64_t file_size;
284
285     if (pb->seekable) {
286         end_guid(pb, wav->data);
287
288         file_size = avio_tell(pb);
289         avio_seek(pb, 16, SEEK_SET);
290         avio_wl64(pb, file_size);
291
292         if (s->streams[0]->codec->codec_tag != 0x01) {
293             int64_t number_of_samples;
294
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);
300         }
301
302         avio_seek(pb, file_size, SEEK_SET);
303         avio_flush(pb);
304     }
305
306     return 0;
307 }
308
309 AVOutputFormat ff_w64_muxer = {
310     .name              = "w64",
311     .long_name         = NULL_IF_CONFIG_SMALL("Sony Wave64"),
312     .extensions        = "w64",
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 },
321 };
322 #endif /* CONFIG_W64_MUXER */