FFmpeg
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 
36 #include "libavutil/avstring.h"
37 #include "libavutil/dict.h"
38 #include "libavutil/common.h"
39 #include "libavutil/intreadwrite.h"
40 #include "libavutil/mathematics.h"
41 #include "libavutil/opt.h"
42 #include "libavutil/time.h"
44 
45 #include "avformat.h"
46 #include "avio.h"
47 #include "avio_internal.h"
48 #include "internal.h"
49 #include "mux.h"
50 #include "riff.h"
51 
52 #define RF64_AUTO (-1)
53 #define RF64_NEVER 0
54 #define RF64_ALWAYS 1
55 
56 typedef enum {
57  PEAK_OFF = 0,
60 } PeakType;
61 
62 typedef enum {
65 } PeakFormat;
66 
67 typedef struct WAVMuxContext {
68  const AVClass *class;
69  int64_t data;
70  int64_t fact_pos;
71  int64_t ds64;
72  int64_t minpts;
73  int64_t maxpts;
75  uint32_t peak_num_frames;
76  unsigned peak_outbuf_size;
78  unsigned size_increment;
79  uint8_t *peak_output;
83  int rf64;
87  int peak_ppv;
88  int peak_bps;
90 
91 #if CONFIG_WAV_MUXER
92 static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
93 {
95  size_t len = 0;
96 
97  if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
98  len = strlen(tag->value);
99  len = FFMIN(len, maxlen);
100  avio_write(s->pb, tag->value, len);
101  }
102 
103  ffio_fill(s->pb, 0, maxlen - len);
104 }
105 
106 static void bwf_write_bext_chunk(AVFormatContext *s)
107 {
108  AVDictionaryEntry *tmp_tag;
109  uint64_t time_reference = 0;
110  int64_t bext = ff_start_tag(s->pb, "bext");
111 
112  bwf_write_bext_string(s, "description", 256);
113  bwf_write_bext_string(s, "originator", 32);
114  bwf_write_bext_string(s, "originator_reference", 32);
115  bwf_write_bext_string(s, "origination_date", 10);
116  bwf_write_bext_string(s, "origination_time", 8);
117 
118  if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0))
119  time_reference = strtoll(tmp_tag->value, NULL, 10);
120  avio_wl64(s->pb, time_reference);
121  avio_wl16(s->pb, 1); // set version to 1
122 
123  if ((tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) && strlen(tmp_tag->value) > 2) {
124  unsigned char umidpart_str[17] = {0};
125  int64_t i;
126  uint64_t umidpart;
127  size_t len = strlen(tmp_tag->value+2);
128 
129  for (i = 0; i < len/16; i++) {
130  memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16);
131  umidpart = strtoll(umidpart_str, NULL, 16);
132  avio_wb64(s->pb, umidpart);
133  }
134  ffio_fill(s->pb, 0, 64 - i*8);
135  } else
136  ffio_fill(s->pb, 0, 64); // zero UMID
137 
138  ffio_fill(s->pb, 0, 190); // Reserved
139 
140  if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
141  avio_put_str(s->pb, tmp_tag->value);
142 
143  ff_end_tag(s->pb, bext);
144 }
145 
146 static av_cold void wav_deinit(AVFormatContext *s)
147 {
148  WAVMuxContext *wav = s->priv_data;
149 
150  av_freep(&wav->peak_maxpos);
151  av_freep(&wav->peak_maxneg);
152  av_freep(&wav->peak_output);
153 }
154 
155 static av_cold int peak_init_writer(AVFormatContext *s)
156 {
157  WAVMuxContext *wav = s->priv_data;
158  AVCodecParameters *par = s->streams[0]->codecpar;
159 
160  if (par->codec_id != AV_CODEC_ID_PCM_S8 &&
162  par->codec_id != AV_CODEC_ID_PCM_U8 &&
164  av_log(s, AV_LOG_ERROR, "Codec %s not supported for Peak Chunk\n",
165  avcodec_get_name(par->codec_id));
166  return -1;
167  }
168 
169  wav->peak_bps = av_get_bits_per_sample(par->codec_id) / 8;
170 
171  if (wav->peak_bps == 1 && wav->peak_format == PEAK_FORMAT_UINT16) {
173  "Writing 16 bit peak for 8 bit audio does not make sense\n");
174  return AVERROR(EINVAL);
175  }
176  if (par->ch_layout.nb_channels > INT_MAX / (wav->peak_bps * wav->peak_ppv))
177  return AVERROR(ERANGE);
178  wav->size_increment = par->ch_layout.nb_channels * wav->peak_bps * wav->peak_ppv;
179 
180  wav->peak_maxpos = av_calloc(par->ch_layout.nb_channels, sizeof(*wav->peak_maxpos));
181  wav->peak_maxneg = av_calloc(par->ch_layout.nb_channels, sizeof(*wav->peak_maxneg));
182  if (!wav->peak_maxpos || !wav->peak_maxneg)
183  goto nomem;
184 
185  return 0;
186 
187 nomem:
188  av_log(s, AV_LOG_ERROR, "Out of memory\n");
189  return AVERROR(ENOMEM);
190 }
191 
192 static int peak_write_frame(AVFormatContext *s)
193 {
194  WAVMuxContext *wav = s->priv_data;
195  AVCodecParameters *par = s->streams[0]->codecpar;
196  unsigned new_size = wav->peak_outbuf_bytes + wav->size_increment;
197  uint8_t *tmp;
198  int c;
199 
200  if (new_size > INT_MAX) {
201  wav->write_peak = PEAK_OFF;
202  return AVERROR(ERANGE);
203  }
204  tmp = av_fast_realloc(wav->peak_output, &wav->peak_outbuf_size, new_size);
205  if (!tmp) {
206  wav->write_peak = PEAK_OFF;
207  return AVERROR(ENOMEM);
208  }
209  wav->peak_output = tmp;
210 
211  for (c = 0; c < par->ch_layout.nb_channels; c++) {
212  wav->peak_maxneg[c] = -wav->peak_maxneg[c];
213 
214  if (wav->peak_bps == 2 && wav->peak_format == PEAK_FORMAT_UINT8) {
215  wav->peak_maxpos[c] = wav->peak_maxpos[c] / 256;
216  wav->peak_maxneg[c] = wav->peak_maxneg[c] / 256;
217  }
218 
219  if (wav->peak_ppv == 1)
220  wav->peak_maxpos[c] =
221  FFMAX(wav->peak_maxpos[c], wav->peak_maxneg[c]);
222 
223  if (wav->peak_format == PEAK_FORMAT_UINT8) {
224  wav->peak_output[wav->peak_outbuf_bytes++] =
225  wav->peak_maxpos[c];
226  if (wav->peak_ppv == 2) {
227  wav->peak_output[wav->peak_outbuf_bytes++] =
228  wav->peak_maxneg[c];
229  }
230  } else {
232  wav->peak_maxpos[c]);
233  wav->peak_outbuf_bytes += 2;
234  if (wav->peak_ppv == 2) {
236  wav->peak_maxneg[c]);
237  wav->peak_outbuf_bytes += 2;
238  }
239  }
240  wav->peak_maxpos[c] = 0;
241  wav->peak_maxneg[c] = 0;
242  }
243  wav->peak_num_frames++;
244 
245  return 0;
246 }
247 
248 static int peak_write_chunk(AVFormatContext *s)
249 {
250  WAVMuxContext *wav = s->priv_data;
251  AVIOContext *pb = s->pb;
252  AVCodecParameters *par = s->streams[0]->codecpar;
253  int64_t peak = ff_start_tag(s->pb, "levl");
254  int64_t now0;
255  time_t now_secs;
256  char timestamp[28];
257 
258  /* Peak frame of incomplete block at end */
259  if (wav->peak_block_pos) {
260  int ret = peak_write_frame(s);
261  if (ret < 0)
262  return ret;
263  }
264 
265  memset(timestamp, 0, sizeof(timestamp));
266  if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
267  struct tm tmpbuf;
268  av_log(s, AV_LOG_INFO, "Writing local time and date to Peak Envelope Chunk\n");
269  now0 = av_gettime();
270  now_secs = now0 / 1000000;
271  if (strftime(timestamp, sizeof(timestamp), "%Y:%m:%d:%H:%M:%S:", localtime_r(&now_secs, &tmpbuf))) {
272  av_strlcatf(timestamp, sizeof(timestamp), "%03d", (int)((now0 / 1000) % 1000));
273  } else {
274  av_log(s, AV_LOG_ERROR, "Failed to write timestamp\n");
275  return -1;
276  }
277  }
278 
279  avio_wl32(pb, 1); /* version */
280  avio_wl32(pb, wav->peak_format); /* 8 or 16 bit */
281  avio_wl32(pb, wav->peak_ppv); /* positive and negative */
282  avio_wl32(pb, wav->peak_block_size); /* frames per value */
283  avio_wl32(pb, par->ch_layout.nb_channels); /* number of channels */
284  avio_wl32(pb, wav->peak_num_frames); /* number of peak frames */
285  avio_wl32(pb, -1); /* audio sample frame position (not implemented) */
286  avio_wl32(pb, 128); /* equal to size of header */
287  avio_write(pb, timestamp, 28); /* ASCII time stamp */
288  ffio_fill(pb, 0, 60);
289 
290  avio_write(pb, wav->peak_output, wav->peak_outbuf_bytes);
291 
292  ff_end_tag(pb, peak);
293 
294  if (!wav->data)
295  wav->data = peak;
296 
297  return 0;
298 }
299 
300 static int wav_write_header(AVFormatContext *s)
301 {
302  WAVMuxContext *wav = s->priv_data;
303  AVIOContext *pb = s->pb;
304  int64_t fmt;
305 
306  if (s->nb_streams != 1) {
307  av_log(s, AV_LOG_ERROR, "WAVE files have exactly one stream\n");
308  return AVERROR(EINVAL);
309  }
310 
311  if (wav->rf64 == RF64_ALWAYS) {
312  ffio_wfourcc(pb, "RF64");
313  avio_wl32(pb, -1); /* RF64 chunk size: use size in ds64 */
314  } else {
315  ffio_wfourcc(pb, "RIFF");
316  avio_wl32(pb, -1); /* file length */
317  }
318 
319  ffio_wfourcc(pb, "WAVE");
320 
321  if (wav->rf64 != RF64_NEVER) {
322  /* write empty ds64 chunk or JUNK chunk to reserve space for ds64 */
323  ffio_wfourcc(pb, wav->rf64 == RF64_ALWAYS ? "ds64" : "JUNK");
324  avio_wl32(pb, 28); /* chunk size */
325  wav->ds64 = avio_tell(pb);
326  ffio_fill(pb, 0, 28);
327  }
328 
329  if (wav->write_peak != PEAK_ONLY) {
330  /* format header */
331  fmt = ff_start_tag(pb, "fmt ");
332  if (ff_put_wav_header(s, pb, s->streams[0]->codecpar, 0) < 0) {
333  av_log(s, AV_LOG_ERROR, "Codec %s not supported in WAVE format\n",
334  avcodec_get_name(s->streams[0]->codecpar->codec_id));
335  return AVERROR(ENOSYS);
336  }
337  ff_end_tag(pb, fmt);
338  }
339 
340  if (s->streams[0]->codecpar->codec_tag != 0x01 /* hence for all other than PCM */
341  && (s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
342  wav->fact_pos = ff_start_tag(pb, "fact");
343  avio_wl32(pb, 0);
344  ff_end_tag(pb, wav->fact_pos);
345  }
346 
347  if (wav->write_bext)
348  bwf_write_bext_chunk(s);
349 
350  if (wav->write_peak) {
351  int ret;
352  if ((ret = peak_init_writer(s)) < 0)
353  return ret;
354  }
355 
356  avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codecpar->sample_rate);
357  wav->maxpts = wav->last_duration = 0;
358  wav->minpts = INT64_MAX;
359 
360  if (wav->write_peak != PEAK_ONLY) {
361  /* info header */
363 
364  /* data header */
365  wav->data = ff_start_tag(pb, "data");
366  }
367 
368  return 0;
369 }
370 
371 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
372 {
373  AVIOContext *pb = s->pb;
374  WAVMuxContext *wav = s->priv_data;
375 
376  if (wav->write_peak != PEAK_ONLY)
377  avio_write(pb, pkt->data, pkt->size);
378 
379  if (wav->write_peak) {
380  int c = 0;
381  int i;
382  for (i = 0; i < pkt->size; i += wav->peak_bps) {
383  if (wav->peak_bps == 1) {
384  wav->peak_maxpos[c] = FFMAX(wav->peak_maxpos[c], *(int8_t*)(pkt->data + i));
385  wav->peak_maxneg[c] = FFMIN(wav->peak_maxneg[c], *(int8_t*)(pkt->data + i));
386  } else {
387  wav->peak_maxpos[c] = FFMAX(wav->peak_maxpos[c], (int16_t)AV_RL16(pkt->data + i));
388  wav->peak_maxneg[c] = FFMIN(wav->peak_maxneg[c], (int16_t)AV_RL16(pkt->data + i));
389  }
390  if (++c == s->streams[0]->codecpar->ch_layout.nb_channels) {
391  c = 0;
392  if (++wav->peak_block_pos == wav->peak_block_size) {
393  int ret = peak_write_frame(s);
394  if (ret < 0)
395  return ret;
396  wav->peak_block_pos = 0;
397  }
398  }
399  }
400  }
401 
402  if(pkt->pts != AV_NOPTS_VALUE) {
403  wav->minpts = FFMIN(wav->minpts, pkt->pts);
404  wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
405  wav->last_duration = pkt->duration;
406  } else
407  av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
408  return 0;
409 }
410 
411 static int wav_write_trailer(AVFormatContext *s)
412 {
413  AVIOContext *pb = s->pb;
414  WAVMuxContext *wav = s->priv_data;
415  int64_t file_size, data_size;
416  int64_t number_of_samples = 0;
417  int rf64 = 0;
418  int ret = 0;
419 
420  if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
421  if (wav->write_peak != PEAK_ONLY && avio_tell(pb) - wav->data < UINT32_MAX) {
422  ff_end_tag(pb, wav->data);
423  }
424 
425  if (wav->write_peak && wav->peak_output) {
426  ret = peak_write_chunk(s);
427  }
428 
429  /* update file size */
430  file_size = avio_tell(pb);
431  data_size = file_size - wav->data;
432  if (wav->rf64 == RF64_ALWAYS || (wav->rf64 == RF64_AUTO && file_size - 8 > UINT32_MAX)) {
433  rf64 = 1;
434  } else if (file_size - 8 <= UINT32_MAX) {
435  avio_seek(pb, 4, SEEK_SET);
436  avio_wl32(pb, (uint32_t)(file_size - 8));
437  avio_seek(pb, file_size, SEEK_SET);
438  } else {
440  "Filesize %"PRId64" invalid for wav, output file will be broken\n",
441  file_size);
442  }
443  number_of_samples = av_rescale_q(wav->maxpts - wav->minpts + wav->last_duration,
444  s->streams[0]->time_base,
445  av_make_q(1, s->streams[0]->codecpar->sample_rate));
446 
447  if(s->streams[0]->codecpar->codec_tag != 0x01) {
448  /* Update num_samps in fact chunk */
449  avio_seek(pb, wav->fact_pos, SEEK_SET);
450  if (rf64 || (wav->rf64 == RF64_AUTO && number_of_samples > UINT32_MAX)) {
451  rf64 = 1;
452  avio_wl32(pb, -1);
453  } else {
454  avio_wl32(pb, number_of_samples);
455  avio_seek(pb, file_size, SEEK_SET);
456  }
457  }
458 
459  if (rf64) {
460  /* overwrite RIFF with RF64 */
461  avio_seek(pb, 0, SEEK_SET);
462  ffio_wfourcc(pb, "RF64");
463  avio_wl32(pb, -1);
464 
465  /* write ds64 chunk (overwrite JUNK if rf64 == RF64_AUTO) */
466  avio_seek(pb, wav->ds64 - 8, SEEK_SET);
467  ffio_wfourcc(pb, "ds64");
468  avio_wl32(pb, 28); /* ds64 chunk size */
469  avio_wl64(pb, file_size - 8); /* RF64 chunk size */
470  avio_wl64(pb, data_size); /* data chunk size */
471  avio_wl64(pb, number_of_samples); /* fact chunk number of samples */
472  avio_wl32(pb, 0); /* number of table entries for non-'data' chunks */
473 
474  /* write -1 in data chunk size */
475  avio_seek(pb, wav->data - 4, SEEK_SET);
476  avio_wl32(pb, -1);
477 
478  avio_seek(pb, file_size, SEEK_SET);
479  }
480  }
481 
482  return ret;
483 }
484 
485 #define OFFSET(x) offsetof(WAVMuxContext, x)
486 #define ENC AV_OPT_FLAG_ENCODING_PARAM
487 static const AVOption options[] = {
488  { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
489  { "write_peak", "Write Peak Envelope chunk.", OFFSET(write_peak), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, ENC, "peak" },
490  { "off", "Do not write peak chunk.", 0, AV_OPT_TYPE_CONST, { .i64 = PEAK_OFF }, 0, 0, ENC, "peak" },
491  { "on", "Append peak chunk after wav data.", 0, AV_OPT_TYPE_CONST, { .i64 = PEAK_ON }, 0, 0, ENC, "peak" },
492  { "only", "Write only peak chunk, omit wav data.", 0, AV_OPT_TYPE_CONST, { .i64 = PEAK_ONLY }, 0, 0, ENC, "peak" },
493  { "rf64", "Use RF64 header rather than RIFF for large files.", OFFSET(rf64), AV_OPT_TYPE_INT, { .i64 = RF64_NEVER },-1, 1, ENC, "rf64" },
494  { "auto", "Write RF64 header if file grows large enough.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_AUTO }, 0, 0, ENC, "rf64" },
495  { "always", "Always write RF64 header regardless of file size.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_ALWAYS }, 0, 0, ENC, "rf64" },
496  { "never", "Never write RF64 header regardless of file size.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_NEVER }, 0, 0, ENC, "rf64" },
497  { "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 },
498  { "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 },
499  { "peak_ppv", "Number of peak points per peak value (1 or 2).", OFFSET(peak_ppv), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, 2, ENC },
500  { NULL },
501 };
502 
503 static const AVClass wav_muxer_class = {
504  .class_name = "WAV muxer",
505  .item_name = av_default_item_name,
506  .option = options,
507  .version = LIBAVUTIL_VERSION_INT,
508 };
509 
511  .p.name = "wav",
512  .p.long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
513  .p.mime_type = "audio/x-wav",
514  .p.extensions = "wav",
515  .priv_data_size = sizeof(WAVMuxContext),
516  .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
517  .p.video_codec = AV_CODEC_ID_NONE,
518  .write_header = wav_write_header,
519  .write_packet = wav_write_packet,
520  .write_trailer = wav_write_trailer,
521  .deinit = wav_deinit,
522  .p.flags = AVFMT_TS_NONSTRICT,
523  .p.codec_tag = ff_wav_codec_tags_list,
524  .p.priv_class = &wav_muxer_class,
525 };
526 #endif /* CONFIG_WAV_MUXER */
527 
528 #if CONFIG_W64_MUXER
529 #include "w64.h"
530 
531 static av_cold int w64_init(AVFormatContext *ctx)
532 {
533  if (ctx->nb_streams != 1) {
534  av_log(ctx, AV_LOG_ERROR, "This muxer only supports a single stream.\n");
535  return AVERROR(EINVAL);
536  }
537 
538  return 0;
539 }
540 
541 static void start_guid(AVIOContext *pb, const uint8_t *guid, int64_t *pos)
542 {
543  *pos = avio_tell(pb);
544 
545  avio_write(pb, guid, 16);
546  avio_wl64(pb, INT64_MAX);
547 }
548 
549 static void end_guid(AVIOContext *pb, int64_t start)
550 {
551  int64_t end, pos = avio_tell(pb);
552 
553  end = FFALIGN(pos, 8);
554  ffio_fill(pb, 0, end - pos);
555  avio_seek(pb, start + 16, SEEK_SET);
556  avio_wl64(pb, end - start);
557  avio_seek(pb, end, SEEK_SET);
558 }
559 
560 static int w64_write_header(AVFormatContext *s)
561 {
562  WAVMuxContext *wav = s->priv_data;
563  AVIOContext *pb = s->pb;
564  int64_t start;
565  int ret;
566 
568  avio_wl64(pb, -1);
570  start_guid(pb, ff_w64_guid_fmt, &start);
571  if ((ret = ff_put_wav_header(s, pb, s->streams[0]->codecpar, 0)) < 0) {
572  av_log(s, AV_LOG_ERROR, "Codec %s not supported\n",
573  avcodec_get_name(s->streams[0]->codecpar->codec_id));
574  return ret;
575  }
576  end_guid(pb, start);
577 
578  if (s->streams[0]->codecpar->codec_tag != 0x01 /* hence for all other than PCM */
579  && (s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
580  start_guid(pb, ff_w64_guid_fact, &wav->fact_pos);
581  avio_wl64(pb, 0);
582  end_guid(pb, wav->fact_pos);
583  }
584 
585  start_guid(pb, ff_w64_guid_data, &wav->data);
586 
587  return 0;
588 }
589 
590 static int w64_write_trailer(AVFormatContext *s)
591 {
592  AVIOContext *pb = s->pb;
593  WAVMuxContext *wav = s->priv_data;
594  int64_t file_size;
595 
596  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
597  end_guid(pb, wav->data);
598 
599  file_size = avio_tell(pb);
600  avio_seek(pb, 16, SEEK_SET);
601  avio_wl64(pb, file_size);
602 
603  if (s->streams[0]->codecpar->codec_tag != 0x01) {
604  int64_t number_of_samples;
605 
606  number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
607  s->streams[0]->codecpar->sample_rate * (int64_t)s->streams[0]->time_base.num,
608  s->streams[0]->time_base.den);
609  avio_seek(pb, wav->fact_pos + 24, SEEK_SET);
610  avio_wl64(pb, number_of_samples);
611  }
612 
613  avio_seek(pb, file_size, SEEK_SET);
614  }
615 
616  return 0;
617 }
618 
620  .p.name = "w64",
621  .p.long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
622  .p.extensions = "w64",
623  .priv_data_size = sizeof(WAVMuxContext),
624  .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
625  .p.video_codec = AV_CODEC_ID_NONE,
626  .init = w64_init,
627  .write_header = w64_write_header,
628  .write_packet = wav_write_packet,
629  .write_trailer = w64_write_trailer,
630  .p.flags = AVFMT_TS_NONSTRICT,
631  .p.codec_tag = ff_wav_codec_tags_list,
632 };
633 #endif /* CONFIG_W64_MUXER */
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:326
WAVMuxContext::peak_output
uint8_t * peak_output
Definition: wavenc.c:79
AVOutputFormat::name
const char * name
Definition: avformat.h:508
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
WAVMuxContext::peak_maxneg
int16_t * peak_maxneg
Definition: wavenc.c:74
ffio_wfourcc
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:116
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:54
WAVMuxContext::maxpts
int64_t maxpts
Definition: wavenc.c:73
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
AVPacket::data
uint8_t * data
Definition: packet.h:374
AVOption
AVOption.
Definition: opt.h:251
PeakFormat
PeakFormat
Definition: wavenc.c:62
avio_wl64
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:446
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
mathematics.h
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:311
WAVMuxContext::peak_outbuf_bytes
uint32_t peak_outbuf_bytes
Definition: wavenc.c:77
WAVMuxContext::fact_pos
int64_t fact_pos
Definition: wavenc.c:70
av_strlcatf
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:104
ff_wav_muxer
const FFOutputFormat ff_wav_muxer
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:34
PEAK_ONLY
@ PEAK_ONLY
Definition: wavenc.c:59
avio_wl16
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:458
avpriv_set_pts_info
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:771
WAVMuxContext::peak_bps
int peak_bps
Definition: wavenc.c:88
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:500
RF64_AUTO
#define RF64_AUTO
Definition: wavenc.c:52
ENC
#define ENC
Definition: caca.c:197
PEAK_FORMAT_UINT16
@ PEAK_FORMAT_UINT16
Definition: wavenc.c:64
ff_start_tag
int64_t ff_start_tag(AVIOContext *pb, const char *tag)
Definition: riffenc.c:31
av_get_bits_per_sample
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:583
AV_CODEC_ID_PCM_S8
@ AV_CODEC_ID_PCM_S8
Definition: codec_id.h:330
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
av_dict_get
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:60
av_fast_realloc
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:495
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
RF64_NEVER
#define RF64_NEVER
Definition: wavenc.c:53
PEAK_FORMAT_UINT8
@ PEAK_FORMAT_UINT8
Definition: wavenc.c:63
ctx
AVFormatContext * ctx
Definition: movenc.c:48
PEAK_ON
@ PEAK_ON
Definition: wavenc.c:58
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
RF64_ALWAYS
#define RF64_ALWAYS
Definition: wavenc.c:54
key
const char * key
Definition: hwcontext_opencl.c:174
WAVMuxContext::data
int64_t data
Definition: wavenc.c:69
time_internal.h
WAVMuxContext::rf64
int rf64
Definition: wavenc.c:83
AVFormatContext
Format I/O context.
Definition: avformat.h:1104
internal.h
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
PEAK_OFF
@ PEAK_OFF
Definition: wavenc.c:57
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
WAVMuxContext::last_duration
int last_duration
Definition: wavenc.c:80
NULL
#define NULL
Definition: coverity.c:32
ff_put_wav_header
int ff_put_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int flags)
Write WAVEFORMAT header structure.
Definition: riffenc.c:54
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
FFOutputFormat
Definition: mux.h:30
time.h
WAVMuxContext::peak_block_pos
int peak_block_pos
Definition: wavenc.c:86
ffio_fill
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition: aviobuf.c:208
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:213
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
ff_w64_guid_fmt
const uint8_t ff_w64_guid_fmt[16]
Definition: w64.c:33
AVFormatContext::nb_streams
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1160
ff_w64_muxer
const FFOutputFormat ff_w64_muxer
options
const OptionDef options[]
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:115
WAVMuxContext::write_bext
int write_bext
Definition: wavenc.c:81
PeakType
PeakType
Definition: wavenc.c:56
AVIOContext::seekable
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:267
localtime_r
#define localtime_r
Definition: time_internal.h:46
av_make_q
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
avio.h
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
WAVMuxContext::minpts
int64_t minpts
Definition: wavenc.c:72
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:222
AV_WL16
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:378
WAVMuxContext
Definition: wavenc.c:67
ff_end_tag
void ff_end_tag(AVIOContext *pb, int64_t start)
Definition: riffenc.c:38
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
WAVMuxContext::write_peak
int write_peak
Definition: wavenc.c:82
avcodec_get_name
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:442
WAVMuxContext::peak_block_size
int peak_block_size
Definition: wavenc.c:84
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
avio_internal.h
ff_w64_guid_wave
const uint8_t ff_w64_guid_wave[16]
Definition: w64.c:28
common.h
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
WAVMuxContext::peak_maxpos
int16_t * peak_maxpos
Definition: wavenc.c:74
ff_w64_guid_fact
const uint8_t ff_w64_guid_fact[16]
Definition: w64.c:38
len
int len
Definition: vorbis_enc_data.h:426
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
AVFMT_TS_NONSTRICT
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:489
WAVMuxContext::peak_ppv
int peak_ppv
Definition: wavenc.c:87
tag
uint32_t tag
Definition: movenc.c:1641
AVFMT_FLAG_BITEXACT
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1239
ret
ret
Definition: filter_design.txt:187
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:252
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
WAVMuxContext::peak_outbuf_size
unsigned peak_outbuf_size
Definition: wavenc.c:76
pos
unsigned int pos
Definition: spdifenc.c:413
avformat.h
dict.h
OFFSET
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option keep it simple and lowercase description are in without and describe what they for example set the foo of the bar offset is the offset of the field in your see the OFFSET() macro
w64.h
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:41
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
ff_riff_write_info
void ff_riff_write_info(AVFormatContext *s)
Write all recognized RIFF tags from s->metadata.
Definition: riffenc.c:334
WAVMuxContext::peak_format
int peak_format
Definition: wavenc.c:85
WAVMuxContext::ds64
int64_t ds64
Definition: wavenc.c:71
avio_wb64
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:452
av_gettime
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
ff_w64_guid_data
const uint8_t ff_w64_guid_data[16]
Definition: w64.c:42
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:331
ff_w64_guid_riff
const uint8_t ff_w64_guid_riff[16]
Definition: w64.c:23
AVDictionaryEntry
Definition: dict.h:89
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:62
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
riff.h
AV_CODEC_ID_PCM_U16LE
@ AV_CODEC_ID_PCM_U16LE
Definition: codec_id.h:328
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVDictionaryEntry::value
char * value
Definition: dict.h:91
WAVMuxContext::size_increment
unsigned size_increment
Definition: wavenc.c:78
avstring.h
avio_put_str
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:394
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
ff_wav_codec_tags_list
const AVCodecTag *const ff_wav_codec_tags_list[]
WAVMuxContext::peak_num_frames
uint32_t peak_num_frames
Definition: wavenc.c:75
mux.h