FFmpeg
Loading...
Searching...
No Matches
wavenc.c
Go to the documentation of this file.
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 * EBU Tech 3285 - Supplement 3 - Peak Envelope Chunk encoder
12 * Copyright (c) 2014 Georg Lippitsch <georg.lippitsch@gmx.at>
13 *
14 * This file is part of FFmpeg.
15 *
16 * FFmpeg is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
20 *
21 * FFmpeg is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with FFmpeg; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 */
30
31#include "config_components.h"
32
33#include <stdint.h>
34#include <string.h>
35#include <time.h>
36
37#include "libavutil/avstring.h"
38#include "libavutil/dict.h"
39#include "libavutil/common.h"
42#include "libavutil/mem.h"
43#include "libavutil/opt.h"
44#include "libavutil/time.h"
46
47#include "avformat.h"
48#include "avio.h"
49#include "avio_internal.h"
50#include "internal.h"
51#include "mux.h"
52#include "riff.h"
53
54#define RF64_AUTO (-1)
55#define RF64_NEVER 0
56#define RF64_ALWAYS 1
57
58typedef enum {
62} PeakType;
63
68
92
93#if CONFIG_WAV_MUXER
94static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
95{
97 size_t len = 0;
98
99 if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
100 len = strlen(tag->value);
101 len = FFMIN(len, maxlen);
102 avio_write(s->pb, tag->value, len);
103 }
104
105 ffio_fill(s->pb, 0, maxlen - len);
106}
107
108static void bwf_write_bext_chunk(AVFormatContext *s)
109{
110 AVDictionaryEntry *tmp_tag;
111 uint64_t time_reference = 0;
112 int64_t bext = ff_start_tag(s->pb, "bext");
113
114 bwf_write_bext_string(s, "description", 256);
115 bwf_write_bext_string(s, "originator", 32);
116 bwf_write_bext_string(s, "originator_reference", 32);
117 bwf_write_bext_string(s, "origination_date", 10);
118 bwf_write_bext_string(s, "origination_time", 8);
119
120 if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0))
121 time_reference = strtoll(tmp_tag->value, NULL, 10);
122 avio_wl64(s->pb, time_reference);
123 avio_wl16(s->pb, 1); // set version to 1
124
125 if ((tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) && strlen(tmp_tag->value) > 2) {
126 unsigned char umidpart_str[17] = {0};
127 int64_t i;
128 uint64_t umidpart;
129 size_t len = strlen(tmp_tag->value+2);
130
131 for (i = 0; i < len/16; i++) {
132 memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16);
133 umidpart = strtoull(umidpart_str, NULL, 16);
134 avio_wb64(s->pb, umidpart);
135 }
136 ffio_fill(s->pb, 0, 64 - i*8);
137 } else
138 ffio_fill(s->pb, 0, 64); // zero UMID
139
140 ffio_fill(s->pb, 0, 190); // Reserved
141
142 if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
143 avio_put_str(s->pb, tmp_tag->value);
144
145 ff_end_tag(s->pb, bext);
146}
147
148static av_cold int wav_init(AVFormatContext *s)
149{
150 WAVMuxContext *wav = s->priv_data;
151
152 if (wav->write_peak == PEAK_ONLY) {
153 if (wav->rf64 == RF64_ALWAYS) {
154 av_log(s, AV_LOG_ERROR, "write_peak=only is not compatible with RF64\n");
155 return AVERROR(EINVAL);
156 }
157 if (wav->rf64 == RF64_AUTO)
158 wav->rf64 = RF64_NEVER;
159 }
160
161 if (wav->rf64 == RF64_AUTO && !(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
162 wav->rf64 = RF64_NEVER;
163
164 return 0;
165}
166
167static av_cold void wav_deinit(AVFormatContext *s)
168{
169 WAVMuxContext *wav = s->priv_data;
170
171 av_freep(&wav->peak_maxpos);
172 av_freep(&wav->peak_maxneg);
173 av_freep(&wav->peak_output);
174}
175
176static av_cold int peak_init_writer(AVFormatContext *s)
177{
178 WAVMuxContext *wav = s->priv_data;
179 AVCodecParameters *par = s->streams[0]->codecpar;
180
181 if (par->codec_id != AV_CODEC_ID_PCM_S8 &&
185 av_log(s, AV_LOG_ERROR, "Codec %s not supported for Peak Chunk\n",
187 return -1;
188 }
189
191
192 if (wav->peak_bps == 1 && wav->peak_format == PEAK_FORMAT_UINT16) {
194 "Writing 16 bit peak for 8 bit audio does not make sense\n");
195 return AVERROR(EINVAL);
196 }
197 if (par->ch_layout.nb_channels <= 0 ||
198 par->ch_layout.nb_channels > INT_MAX / (wav->peak_bps * wav->peak_ppv))
199 return AVERROR(ERANGE);
200 wav->size_increment = par->ch_layout.nb_channels * wav->peak_bps * wav->peak_ppv;
201
202 wav->peak_maxpos = av_calloc(par->ch_layout.nb_channels, sizeof(*wav->peak_maxpos));
203 wav->peak_maxneg = av_calloc(par->ch_layout.nb_channels, sizeof(*wav->peak_maxneg));
204 if (!wav->peak_maxpos || !wav->peak_maxneg)
205 goto nomem;
206
207 return 0;
208
209nomem:
210 av_log(s, AV_LOG_ERROR, "Out of memory\n");
211 return AVERROR(ENOMEM);
212}
213
214static int peak_write_frame(AVFormatContext *s)
215{
216 WAVMuxContext *wav = s->priv_data;
217 AVCodecParameters *par = s->streams[0]->codecpar;
218 unsigned new_size = wav->peak_outbuf_bytes + wav->size_increment;
219 uint8_t *tmp;
220 int c;
221
222 if (new_size > INT_MAX) {
223 wav->write_peak = PEAK_OFF;
224 return AVERROR(ERANGE);
225 }
226 tmp = av_fast_realloc(wav->peak_output, &wav->peak_outbuf_size, new_size);
227 if (!tmp) {
228 wav->write_peak = PEAK_OFF;
229 return AVERROR(ENOMEM);
230 }
231 wav->peak_output = tmp;
232
233 for (c = 0; c < par->ch_layout.nb_channels; c++) {
234 wav->peak_maxneg[c] = -wav->peak_maxneg[c];
235
236 if (wav->peak_bps == 2 && wav->peak_format == PEAK_FORMAT_UINT8) {
237 wav->peak_maxpos[c] = wav->peak_maxpos[c] / 256;
238 wav->peak_maxneg[c] = wav->peak_maxneg[c] / 256;
239 }
240
241 if (wav->peak_ppv == 1)
242 wav->peak_maxpos[c] =
243 FFMAX(wav->peak_maxpos[c], wav->peak_maxneg[c]);
244
245 if (wav->peak_format == PEAK_FORMAT_UINT8) {
246 wav->peak_output[wav->peak_outbuf_bytes++] =
247 wav->peak_maxpos[c];
248 if (wav->peak_ppv == 2) {
249 wav->peak_output[wav->peak_outbuf_bytes++] =
250 wav->peak_maxneg[c];
251 }
252 } else {
254 wav->peak_maxpos[c]);
255 wav->peak_outbuf_bytes += 2;
256 if (wav->peak_ppv == 2) {
258 wav->peak_maxneg[c]);
259 wav->peak_outbuf_bytes += 2;
260 }
261 }
262 wav->peak_maxpos[c] = 0;
263 wav->peak_maxneg[c] = 0;
264 }
265 wav->peak_num_frames++;
266
267 return 0;
268}
269
270static int peak_write_chunk(AVFormatContext *s)
271{
272 WAVMuxContext *wav = s->priv_data;
273 AVIOContext *pb = s->pb;
274 AVCodecParameters *par = s->streams[0]->codecpar;
275 int64_t peak = ff_start_tag(s->pb, "levl"); // codespell:ignore
276 int64_t now0;
277 time_t now_secs;
278 char timestamp[28];
279
280 /* Peak frame of incomplete block at end */
281 if (wav->peak_block_pos) {
282 int ret = peak_write_frame(s);
283 if (ret < 0)
284 return ret;
285 }
286
287 memset(timestamp, 0, sizeof(timestamp));
288 if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
289 struct tm tmpbuf;
290 av_log(s, AV_LOG_INFO, "Writing local time and date to Peak Envelope Chunk\n");
291 now0 = av_gettime();
292 now_secs = now0 / 1000000;
293 if (strftime(timestamp, sizeof(timestamp), "%Y:%m:%d:%H:%M:%S:", localtime_r(&now_secs, &tmpbuf))) {
294 av_strlcatf(timestamp, sizeof(timestamp), "%03d", (int)((now0 / 1000) % 1000));
295 } else {
296 av_log(s, AV_LOG_ERROR, "Failed to write timestamp\n");
297 return -1;
298 }
299 }
300
301 avio_wl32(pb, 1); /* version */
302 avio_wl32(pb, wav->peak_format); /* 8 or 16 bit */
303 avio_wl32(pb, wav->peak_ppv); /* positive and negative */
304 avio_wl32(pb, wav->peak_block_size); /* frames per value */
305 avio_wl32(pb, par->ch_layout.nb_channels); /* number of channels */
306 avio_wl32(pb, wav->peak_num_frames); /* number of peak frames */
307 avio_wl32(pb, -1); /* audio sample frame position (not implemented) */
308 avio_wl32(pb, 128); /* equal to size of header */
309 avio_write(pb, timestamp, 28); /* ASCII time stamp */
310 ffio_fill(pb, 0, 60);
311
313
314 ff_end_tag(pb, peak);
315
316 if (!wav->data)
317 wav->data = peak;
318
319 return 0;
320}
321
322static int wav_write_header(AVFormatContext *s)
323{
324 WAVMuxContext *wav = s->priv_data;
325 AVIOContext *pb = s->pb;
326 int64_t fmt;
327
328 if (wav->rf64 == RF64_ALWAYS) {
329 ffio_wfourcc(pb, "RF64");
330 avio_wl32(pb, -1); /* RF64 chunk size: use size in ds64 */
331 } else {
332 ffio_wfourcc(pb, "RIFF");
333 avio_wl32(pb, -1); /* file length */
334 }
335
336 ffio_wfourcc(pb, "WAVE");
337
338 if (wav->rf64 == RF64_ALWAYS) {
339 /* write empty ds64 chunk */
340 ffio_wfourcc(pb, "ds64");
341 avio_wl32(pb, 28); /* chunk size */
342 wav->ds64 = avio_tell(pb);
343 ffio_fill(pb, 0, 28);
344 }
345
346 if (wav->write_peak != PEAK_ONLY) {
347 /* format header */
348 fmt = ff_start_tag(pb, "fmt ");
349 if (ff_put_wav_header(s, pb, s->streams[0]->codecpar, 0) < 0) {
350 av_log(s, AV_LOG_ERROR, "Codec %s not supported in WAVE format\n",
351 avcodec_get_name(s->streams[0]->codecpar->codec_id));
352 return AVERROR(ENOSYS);
353 }
354 ff_end_tag(pb, fmt);
355
356 if (wav->rf64 == RF64_AUTO) {
357 /* reserve space for ds64 */
358 ffio_wfourcc(pb, "JUNK");
359 avio_wl32(pb, 28); /* chunk size */
360 ffio_fill(pb, 0, 28);
361
362 /* in RF64_AUTO mode, fmt + JUNK will be overwritten by ds64 + fmt */
363 wav->ds64 = fmt;
364 }
365 }
366
367 if (s->streams[0]->codecpar->codec_tag != 0x01 /* hence for all other than PCM */
368 && (s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
369 wav->fact_pos = ff_start_tag(pb, "fact");
370 avio_wl32(pb, 0);
371 ff_end_tag(pb, wav->fact_pos);
372 }
373
374 if (wav->write_bext)
375 bwf_write_bext_chunk(s);
376
377 if (wav->write_peak) {
378 int ret;
379 if ((ret = peak_init_writer(s)) < 0)
380 return ret;
381 }
382
383 avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codecpar->sample_rate);
384 wav->maxpts = wav->last_duration = 0;
385 wav->minpts = INT64_MAX;
386
387 if (wav->write_peak != PEAK_ONLY) {
388 /* info header */
390
391 /* data header */
392 wav->data = ff_start_tag(pb, "data");
393 }
394
395 return 0;
396}
397
398static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
399{
400 AVIOContext *pb = s->pb;
401 WAVMuxContext *wav = s->priv_data;
402
403 if (wav->write_peak != PEAK_ONLY)
404 avio_write(pb, pkt->data, pkt->size);
405
406 if (wav->write_peak) {
407 int c = 0;
408 int i;
409 for (i = 0; i < pkt->size; i += wav->peak_bps) {
410 if (wav->peak_bps == 1) {
411 wav->peak_maxpos[c] = FFMAX(wav->peak_maxpos[c], *(int8_t*)(pkt->data + i));
412 wav->peak_maxneg[c] = FFMIN(wav->peak_maxneg[c], *(int8_t*)(pkt->data + i));
413 } else {
414 wav->peak_maxpos[c] = FFMAX(wav->peak_maxpos[c], (int16_t)AV_RL16(pkt->data + i));
415 wav->peak_maxneg[c] = FFMIN(wav->peak_maxneg[c], (int16_t)AV_RL16(pkt->data + i));
416 }
417 if (++c == s->streams[0]->codecpar->ch_layout.nb_channels) {
418 c = 0;
419 if (++wav->peak_block_pos == wav->peak_block_size) {
420 int ret = peak_write_frame(s);
421 if (ret < 0)
422 return ret;
423 wav->peak_block_pos = 0;
424 }
425 }
426 }
427 }
428
429 if(pkt->pts != AV_NOPTS_VALUE) {
430 wav->minpts = FFMIN(wav->minpts, pkt->pts);
431 wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
432 wav->last_duration = pkt->duration;
433 } else
434 av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
435 return 0;
436}
437
438static int wav_write_trailer(AVFormatContext *s)
439{
440 AVIOContext *pb = s->pb;
441 WAVMuxContext *wav = s->priv_data;
442 int64_t file_size, data_size;
443 int64_t number_of_samples = 0;
444 int64_t pos;
445 int rf64 = 0;
446 int ret = 0;
447
448 if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
449 data_size = avio_tell(pb) - wav->data;
450 if (wav->write_peak != PEAK_ONLY && data_size < UINT32_MAX) {
451 ff_end_tag(pb, wav->data);
452 }
453
454 if (wav->write_peak && wav->peak_output) {
455 ret = peak_write_chunk(s);
456 }
457
458 /* update file size */
459 file_size = avio_tell(pb);
460 if (wav->rf64 == RF64_ALWAYS || (wav->rf64 == RF64_AUTO && file_size - 8 > UINT32_MAX)) {
461 rf64 = 1;
462 } else if (file_size - 8 <= UINT32_MAX) {
463 avio_seek(pb, 4, SEEK_SET);
464 avio_wl32(pb, (uint32_t)(file_size - 8));
465 avio_seek(pb, file_size, SEEK_SET);
466 } else {
468 "Filesize %"PRId64" invalid for wav, output file will be broken\n",
469 file_size);
470 }
471 number_of_samples = av_rescale_q(wav->maxpts - wav->minpts + wav->last_duration,
472 s->streams[0]->time_base,
473 av_make_q(1, s->streams[0]->codecpar->sample_rate));
474
475 if(s->streams[0]->codecpar->codec_tag != 0x01) {
476 /* Update num_samps in fact chunk */
477 avio_seek(pb, wav->fact_pos, SEEK_SET);
478 if (rf64 || (wav->rf64 == RF64_AUTO && number_of_samples > UINT32_MAX)) {
479 rf64 = 1;
480 avio_wl32(pb, -1);
481 } else {
482 avio_wl32(pb, number_of_samples);
483 avio_seek(pb, file_size, SEEK_SET);
484 }
485 }
486
487 if (rf64) {
488 /* overwrite RIFF with RF64 */
489 avio_seek(pb, 0, SEEK_SET);
490 ffio_wfourcc(pb, "RF64");
491 avio_wl32(pb, -1);
492
493 /* write ds64 chunk (overwrite fmt + JUNK if rf64 == RF64_AUTO) */
494 avio_seek(pb, wav->ds64 - 8, SEEK_SET);
495 ffio_wfourcc(pb, "ds64");
496 avio_wl32(pb, 28); /* ds64 chunk size */
497 avio_wl64(pb, file_size - 8); /* RF64 chunk size */
498 avio_wl64(pb, data_size); /* data chunk size */
499 avio_wl64(pb, number_of_samples); /* fact chunk number of samples */
500 avio_wl32(pb, 0); /* number of table entries for non-'data' chunks */
501
502 /* rewrite fmt in its RF64 position after ds64 */
503 pos = ff_start_tag(pb, "fmt ");
504 ret = ff_put_wav_header(s, pb, s->streams[0]->codecpar, 0);
505 ff_end_tag(pb, pos);
506
507 /* write -1 in data chunk size */
508 avio_seek(pb, wav->data - 4, SEEK_SET);
509 avio_wl32(pb, -1);
510
511 avio_seek(pb, file_size, SEEK_SET);
512 }
513 }
514
515 return ret;
516}
517
518#define OFFSET(x) offsetof(WAVMuxContext, x)
519#define ENC AV_OPT_FLAG_ENCODING_PARAM
520static const AVOption options[] = {
521 { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
522 { "write_peak", "Write Peak Envelope chunk.", OFFSET(write_peak), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, ENC, .unit = "peak" },
523 { "off", "Do not write peak chunk.", 0, AV_OPT_TYPE_CONST, { .i64 = PEAK_OFF }, 0, 0, ENC, .unit = "peak" },
524 { "on", "Append peak chunk after wav data.", 0, AV_OPT_TYPE_CONST, { .i64 = PEAK_ON }, 0, 0, ENC, .unit = "peak" },
525 { "only", "Write only peak chunk, omit wav data.", 0, AV_OPT_TYPE_CONST, { .i64 = PEAK_ONLY }, 0, 0, ENC, .unit = "peak" },
526 { "rf64", "Use RF64 header rather than RIFF for large files.", OFFSET(rf64), AV_OPT_TYPE_INT, { .i64 = RF64_NEVER },-1, 1, ENC, .unit = "rf64" },
527 { "auto", "Write RF64 header if file grows large enough.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_AUTO }, 0, 0, ENC, .unit = "rf64" },
528 { "always", "Always write RF64 header regardless of file size.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_ALWAYS }, 0, 0, ENC, .unit = "rf64" },
529 { "never", "Never write RF64 header regardless of file size.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_NEVER }, 0, 0, ENC, .unit = "rf64" },
530 { "peak_block_size", "Number of audio samples used to generate each peak frame.", OFFSET(peak_block_size), AV_OPT_TYPE_INT, { .i64 = 256 }, 0, 65536, ENC },
531 { "peak_format", "The format of the peak envelope data (1: uint8, 2: uint16).", OFFSET(peak_format), AV_OPT_TYPE_INT, { .i64 = PEAK_FORMAT_UINT16 }, PEAK_FORMAT_UINT8, PEAK_FORMAT_UINT16, ENC },
532 { "peak_ppv", "Number of peak points per peak value (1 or 2).", OFFSET(peak_ppv), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, 2, ENC },
533 { NULL },
534};
535
536static const AVClass wav_muxer_class = {
537 .class_name = "WAV muxer",
538 .item_name = av_default_item_name,
539 .option = options,
540 .version = LIBAVUTIL_VERSION_INT,
541};
542
544 .p.name = "wav",
545 .p.long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
546 .p.mime_type = "audio/x-wav",
547 .p.extensions = "wav",
548 .priv_data_size = sizeof(WAVMuxContext),
549 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
550 .p.video_codec = AV_CODEC_ID_NONE,
551 .p.subtitle_codec = AV_CODEC_ID_NONE,
552 .init = wav_init,
553 .write_header = wav_write_header,
554 .write_packet = wav_write_packet,
555 .write_trailer = wav_write_trailer,
556 .deinit = wav_deinit,
557 .p.flags = AVFMT_TS_NONSTRICT,
558 .p.codec_tag = ff_wav_codec_tags_list,
559 .p.priv_class = &wav_muxer_class,
560 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH,
561};
562#endif /* CONFIG_WAV_MUXER */
563
564#if CONFIG_W64_MUXER
565#include "w64.h"
566
567static void start_guid(AVIOContext *pb, const uint8_t *guid, int64_t *pos)
568{
569 *pos = avio_tell(pb);
570
571 avio_write(pb, guid, 16);
572 avio_wl64(pb, INT64_MAX);
573}
574
575static void end_guid(AVIOContext *pb, int64_t start)
576{
577 int64_t end, pos = avio_tell(pb);
578
579 end = FFALIGN(pos, 8);
580 ffio_fill(pb, 0, end - pos);
581 avio_seek(pb, start + 16, SEEK_SET);
582 avio_wl64(pb, end - start);
583 avio_seek(pb, end, SEEK_SET);
584}
585
586static int w64_write_header(AVFormatContext *s)
587{
588 WAVMuxContext *wav = s->priv_data;
589 AVIOContext *pb = s->pb;
590 int64_t start;
591 int ret;
592
594 avio_wl64(pb, -1);
596 start_guid(pb, ff_w64_guid_fmt, &start);
597 if ((ret = ff_put_wav_header(s, pb, s->streams[0]->codecpar, 0)) < 0) {
598 av_log(s, AV_LOG_ERROR, "Codec %s not supported\n",
599 avcodec_get_name(s->streams[0]->codecpar->codec_id));
600 return ret;
601 }
602 end_guid(pb, start);
603
604 if (s->streams[0]->codecpar->codec_tag != 0x01 /* hence for all other than PCM */
605 && (s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
606 start_guid(pb, ff_w64_guid_fact, &wav->fact_pos);
607 avio_wl64(pb, 0);
608 end_guid(pb, wav->fact_pos);
609 }
610
611 start_guid(pb, ff_w64_guid_data, &wav->data);
612
613 return 0;
614}
615
616static int w64_write_trailer(AVFormatContext *s)
617{
618 AVIOContext *pb = s->pb;
619 WAVMuxContext *wav = s->priv_data;
620 int64_t file_size;
621
622 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
623 end_guid(pb, wav->data);
624
625 file_size = avio_tell(pb);
626 avio_seek(pb, 16, SEEK_SET);
627 avio_wl64(pb, file_size);
628
629 if (s->streams[0]->codecpar->codec_tag != 0x01) {
630 int64_t number_of_samples;
631
632 number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
633 s->streams[0]->codecpar->sample_rate * (int64_t)s->streams[0]->time_base.num,
634 s->streams[0]->time_base.den);
635 avio_seek(pb, wav->fact_pos + 24, SEEK_SET);
636 avio_wl64(pb, number_of_samples);
637 }
638
639 avio_seek(pb, file_size, SEEK_SET);
640 }
641
642 return 0;
643}
644
646 .p.name = "w64",
647 .p.long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
648 .p.extensions = "w64",
649 .priv_data_size = sizeof(WAVMuxContext),
650 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
651 .p.video_codec = AV_CODEC_ID_NONE,
652 .p.subtitle_codec = AV_CODEC_ID_NONE,
653 .write_header = w64_write_header,
654 .write_packet = wav_write_packet,
655 .write_trailer = w64_write_trailer,
656 .p.flags = AVFMT_TS_NONSTRICT,
657 .p.codec_tag = ff_wav_codec_tags_list,
658 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH,
659};
660#endif /* CONFIG_W64_MUXER */
const FFOutputFormat ff_w64_muxer
const FFOutputFormat ff_wav_muxer
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:856
Main libavformat public API header.
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition avformat.h:1569
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition avformat.h:509
Buffered I/O operations.
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
void avio_wl64(AVIOContext *s, uint64_t val)
Definition aviobuf.c:428
void avio_wl32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:360
void avio_wl16(AVIOContext *s, unsigned int val)
Definition aviobuf.c:440
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition aviobuf.c:376
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
void avio_wb64(AVIOContext *s, uint64_t val)
Definition aviobuf.c:434
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition aviobuf.c:192
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition avstring.c:103
#define ENC
Definition caca.c:198
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
common internal and external API header
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
Public dictionary API.
const char * key
#define OFFSET(x)
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition utils.c:557
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition utils.c:421
@ AV_CODEC_ID_PCM_U8
Definition codec_id.h:337
@ AV_CODEC_ID_PCM_S16LE
Definition codec_id.h:332
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_PCM_S8
Definition codec_id.h:336
@ AV_CODEC_ID_PCM_U16LE
Definition codec_id.h:334
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition rational.h:71
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition mem.c:601
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define AV_RL16(p)
#define AV_WL16(p, v)
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define FFALIGN(x, a)
Definition macros.h:78
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
uint32_t tag
Definition movenc.c:2128
#define FF_OFMT_FLAG_MAX_ONE_OF_EACH
If this flag is set, it indicates that for each codec type whose corresponding default codec (i....
Definition mux.h:50
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
AVOptions.
internal header for RIFF based (de)muxers do NOT include this in end user applications
void ff_end_tag(AVIOContext *pb, int64_t start)
Definition riffenc.c:38
void ff_riff_write_info(AVFormatContext *s)
Write all recognized RIFF tags from s->metadata.
Definition riffenc.c:357
int64_t ff_start_tag(AVIOContext *pb, const char *tag)
Definition riffenc.c:31
int ff_put_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int flags)
Write WAVEFORMAT header structure.
Definition riffenc.c:54
const AVCodecTag *const ff_wav_codec_tags_list[]
unsigned int pos
Definition spdifenc.c:431
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
char * value
Definition dict.h:92
Format I/O context.
Definition avformat.h:1400
Bytestream IO Context.
Definition avio.h:160
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition avio.h:261
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:587
int64_t minpts
Definition wavenc.c:74
int16_t * peak_maxpos
Definition wavenc.c:76
uint32_t peak_num_frames
Definition wavenc.c:77
unsigned size_increment
Definition wavenc.c:80
uint32_t peak_outbuf_bytes
Definition wavenc.c:79
uint8_t * peak_output
Definition wavenc.c:81
int write_bext
Definition wavenc.c:83
int write_peak
Definition wavenc.c:84
int64_t ds64
Definition wavenc.c:73
int16_t * peak_maxneg
Definition wavenc.c:76
int64_t maxpts
Definition wavenc.c:75
int peak_ppv
Definition wavenc.c:89
int peak_format
Definition wavenc.c:87
int peak_block_pos
Definition wavenc.c:88
int64_t data
Definition wavenc.c:71
int peak_bps
Definition wavenc.c:90
int64_t fact_pos
Definition wavenc.c:72
unsigned peak_outbuf_size
Definition wavenc.c:78
int last_duration
Definition wavenc.c:82
int peak_block_size
Definition wavenc.c:86
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
int64_t av_gettime(void)
Get the current time in microseconds.
Definition time.c:40
#define localtime_r
int len
static double c[64]
const uint8_t ff_w64_guid_wave[16]
Definition w64.c:28
const uint8_t ff_w64_guid_riff[16]
Definition w64.c:23
const uint8_t ff_w64_guid_data[16]
Definition w64.c:42
const uint8_t ff_w64_guid_fact[16]
Definition w64.c:38
const uint8_t ff_w64_guid_fmt[16]
Definition w64.c:33
#define RF64_AUTO
Definition wavenc.c:54
PeakFormat
Definition wavenc.c:64
@ PEAK_FORMAT_UINT8
Definition wavenc.c:65
@ PEAK_FORMAT_UINT16
Definition wavenc.c:66
#define RF64_NEVER
Definition wavenc.c:55
PeakType
Definition wavenc.c:58
@ PEAK_ON
Definition wavenc.c:60
@ PEAK_OFF
Definition wavenc.c:59
@ PEAK_ONLY
Definition wavenc.c:61
#define RF64_ALWAYS
Definition wavenc.c:56