FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 <stdint.h>
32 #include <string.h>
33 
34 #include "libavutil/avstring.h"
35 #include "libavutil/dict.h"
36 #include "libavutil/common.h"
37 #include "libavutil/intreadwrite.h"
38 #include "libavutil/mathematics.h"
39 #include "libavutil/opt.h"
40 #include "libavutil/time.h"
42 
43 #include "avformat.h"
44 #include "avio.h"
45 #include "avio_internal.h"
46 #include "internal.h"
47 #include "riff.h"
48 
49 #define RF64_AUTO (-1)
50 #define RF64_NEVER 0
51 #define RF64_ALWAYS 1
52 
53 #define PEAK_BUFFER_SIZE 1024
54 
55 typedef enum {
56  PEAK_OFF = 0,
59 } PeakType;
60 
61 typedef enum {
64 } PeakFormat;
65 
66 typedef struct WAVMuxContext {
67  const AVClass *class;
68  int64_t data;
69  int64_t fact_pos;
70  int64_t ds64;
71  int64_t minpts;
72  int64_t maxpts;
74  uint32_t peak_num_frames;
75  uint32_t peak_outbuf_size;
77  uint32_t peak_pos_pop;
78  uint16_t peak_pop;
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 peak_free_buffers(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 &&
165  av_log(s, AV_LOG_ERROR, "%s codec not supported for Peak Chunk\n",
166  codec ? codec->name : "NONE");
167  return -1;
168  }
169 
170  wav->peak_bps = av_get_bits_per_sample(par->codec_id) / 8;
171 
172  if (wav->peak_bps == 1 && wav->peak_format == PEAK_FORMAT_UINT16) {
173  av_log(s, AV_LOG_ERROR,
174  "Writing 16 bit peak for 8 bit audio does not make sense\n");
175  return AVERROR(EINVAL);
176  }
177 
178  wav->peak_maxpos = av_mallocz_array(par->channels, sizeof(*wav->peak_maxpos));
179  wav->peak_maxneg = av_mallocz_array(par->channels, sizeof(*wav->peak_maxneg));
181  if (!wav->peak_maxpos || !wav->peak_maxneg || !wav->peak_output)
182  goto nomem;
183 
185 
186  return 0;
187 
188 nomem:
189  av_log(s, AV_LOG_ERROR, "Out of memory\n");
190  peak_free_buffers(s);
191  return AVERROR(ENOMEM);
192 }
193 
194 static void peak_write_frame(AVFormatContext *s)
195 {
196  WAVMuxContext *wav = s->priv_data;
197  AVCodecParameters *par = s->streams[0]->codecpar;
198  int peak_of_peaks;
199  int c;
200 
201  if (!wav->peak_output)
202  return;
203 
204  for (c = 0; c < par->channels; c++) {
205  wav->peak_maxneg[c] = -wav->peak_maxneg[c];
206 
207  if (wav->peak_bps == 2 && wav->peak_format == PEAK_FORMAT_UINT8) {
208  wav->peak_maxpos[c] = wav->peak_maxpos[c] / 256;
209  wav->peak_maxneg[c] = wav->peak_maxneg[c] / 256;
210  }
211 
212  if (wav->peak_ppv == 1)
213  wav->peak_maxpos[c] =
214  FFMAX(wav->peak_maxpos[c], wav->peak_maxneg[c]);
215 
216  peak_of_peaks = FFMAX3(wav->peak_maxpos[c], wav->peak_maxneg[c],
217  wav->peak_pop);
218  if (peak_of_peaks > wav->peak_pop)
219  wav->peak_pos_pop = wav->peak_num_frames;
220  wav->peak_pop = peak_of_peaks;
221 
222  if (wav->peak_outbuf_size - wav->peak_outbuf_bytes <
223  wav->peak_format * wav->peak_ppv) {
225  wav->peak_output = av_realloc(wav->peak_output,
226  wav->peak_outbuf_size);
227  if (!wav->peak_output) {
228  av_log(s, AV_LOG_ERROR, "No memory for peak data\n");
229  return;
230  }
231  }
232 
233  if (wav->peak_format == PEAK_FORMAT_UINT8) {
234  wav->peak_output[wav->peak_outbuf_bytes++] =
235  wav->peak_maxpos[c];
236  if (wav->peak_ppv == 2) {
237  wav->peak_output[wav->peak_outbuf_bytes++] =
238  wav->peak_maxneg[c];
239  }
240  } else {
242  wav->peak_maxpos[c]);
243  wav->peak_outbuf_bytes += 2;
244  if (wav->peak_ppv == 2) {
246  wav->peak_maxneg[c]);
247  wav->peak_outbuf_bytes += 2;
248  }
249  }
250  wav->peak_maxpos[c] = 0;
251  wav->peak_maxneg[c] = 0;
252  }
253  wav->peak_num_frames++;
254 }
255 
256 static int peak_write_chunk(AVFormatContext *s)
257 {
258  WAVMuxContext *wav = s->priv_data;
259  AVIOContext *pb = s->pb;
260  AVCodecParameters *par = s->streams[0]->codecpar;
261  int64_t peak = ff_start_tag(s->pb, "levl");
262  int64_t now0;
263  time_t now_secs;
264  char timestamp[28];
265 
266  /* Peak frame of incomplete block at end */
267  if (wav->peak_block_pos)
268  peak_write_frame(s);
269 
270  memset(timestamp, 0, sizeof(timestamp));
271  if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
272  struct tm tmpbuf;
273  av_log(s, AV_LOG_INFO, "Writing local time and date to Peak Envelope Chunk\n");
274  now0 = av_gettime();
275  now_secs = now0 / 1000000;
276  if (strftime(timestamp, sizeof(timestamp), "%Y:%m:%d:%H:%M:%S:", localtime_r(&now_secs, &tmpbuf))) {
277  av_strlcatf(timestamp, sizeof(timestamp), "%03d", (int)((now0 / 1000) % 1000));
278  } else {
279  av_log(s, AV_LOG_ERROR, "Failed to write timestamp\n");
280  return -1;
281  }
282  }
283 
284  avio_wl32(pb, 1); /* version */
285  avio_wl32(pb, wav->peak_format); /* 8 or 16 bit */
286  avio_wl32(pb, wav->peak_ppv); /* positive and negative */
287  avio_wl32(pb, wav->peak_block_size); /* frames per value */
288  avio_wl32(pb, par->channels); /* number of channels */
289  avio_wl32(pb, wav->peak_num_frames); /* number of peak frames */
290  avio_wl32(pb, wav->peak_pos_pop); /* audio sample frame index */
291  avio_wl32(pb, 128); /* equal to size of header */
292  avio_write(pb, timestamp, 28); /* ASCII time stamp */
293  ffio_fill(pb, 0, 60);
294 
295  avio_write(pb, wav->peak_output, wav->peak_outbuf_bytes);
296 
297  ff_end_tag(pb, peak);
298 
299  if (!wav->data)
300  wav->data = peak;
301 
302  return 0;
303 }
304 
305 static int wav_write_header(AVFormatContext *s)
306 {
307  WAVMuxContext *wav = s->priv_data;
308  AVIOContext *pb = s->pb;
309  int64_t fmt;
310 
311  if (s->nb_streams != 1) {
312  av_log(s, AV_LOG_ERROR, "WAVE files have exactly one stream\n");
313  return AVERROR(EINVAL);
314  }
315 
316  if (wav->rf64 == RF64_ALWAYS) {
317  ffio_wfourcc(pb, "RF64");
318  avio_wl32(pb, -1); /* RF64 chunk size: use size in ds64 */
319  } else {
320  ffio_wfourcc(pb, "RIFF");
321  avio_wl32(pb, -1); /* file length */
322  }
323 
324  ffio_wfourcc(pb, "WAVE");
325 
326  if (wav->rf64 != RF64_NEVER) {
327  /* write empty ds64 chunk or JUNK chunk to reserve space for ds64 */
328  ffio_wfourcc(pb, wav->rf64 == RF64_ALWAYS ? "ds64" : "JUNK");
329  avio_wl32(pb, 28); /* chunk size */
330  wav->ds64 = avio_tell(pb);
331  ffio_fill(pb, 0, 28);
332  }
333 
334  if (wav->write_peak != 2) {
335  /* format header */
336  fmt = ff_start_tag(pb, "fmt ");
337  if (ff_put_wav_header(s, pb, s->streams[0]->codecpar, 0) < 0) {
339  av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
340  desc ? desc->name : "unknown");
341  return AVERROR(ENOSYS);
342  }
343  ff_end_tag(pb, fmt);
344  }
345 
346  if (s->streams[0]->codecpar->codec_tag != 0x01 /* hence for all other than PCM */
347  && (s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
348  wav->fact_pos = ff_start_tag(pb, "fact");
349  avio_wl32(pb, 0);
350  ff_end_tag(pb, wav->fact_pos);
351  }
352 
353  if (wav->write_bext)
354  bwf_write_bext_chunk(s);
355 
356  if (wav->write_peak) {
357  int ret;
358  if ((ret = peak_init_writer(s)) < 0)
359  return ret;
360  }
361 
362  avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codecpar->sample_rate);
363  wav->maxpts = wav->last_duration = 0;
364  wav->minpts = INT64_MAX;
365 
366  if (wav->write_peak != 2) {
367  /* info header */
369 
370  /* data header */
371  wav->data = ff_start_tag(pb, "data");
372  }
373 
374  avio_flush(pb);
375 
376  return 0;
377 }
378 
379 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
380 {
381  AVIOContext *pb = s->pb;
382  WAVMuxContext *wav = s->priv_data;
383 
384  if (wav->write_peak != 2)
385  avio_write(pb, pkt->data, pkt->size);
386 
387  if (wav->write_peak) {
388  int c = 0;
389  int i;
390  for (i = 0; i < pkt->size; i += wav->peak_bps) {
391  if (wav->peak_bps == 1) {
392  wav->peak_maxpos[c] = FFMAX(wav->peak_maxpos[c], *(int8_t*)(pkt->data + i));
393  wav->peak_maxneg[c] = FFMIN(wav->peak_maxneg[c], *(int8_t*)(pkt->data + i));
394  } else {
395  wav->peak_maxpos[c] = FFMAX(wav->peak_maxpos[c], (int16_t)AV_RL16(pkt->data + i));
396  wav->peak_maxneg[c] = FFMIN(wav->peak_maxneg[c], (int16_t)AV_RL16(pkt->data + i));
397  }
398  if (++c == s->streams[0]->codecpar->channels) {
399  c = 0;
400  if (++wav->peak_block_pos == wav->peak_block_size) {
401  peak_write_frame(s);
402  wav->peak_block_pos = 0;
403  }
404  }
405  }
406  }
407 
408  if(pkt->pts != AV_NOPTS_VALUE) {
409  wav->minpts = FFMIN(wav->minpts, pkt->pts);
410  wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
411  wav->last_duration = pkt->duration;
412  } else
413  av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
414  return 0;
415 }
416 
417 static int wav_write_trailer(AVFormatContext *s)
418 {
419  AVIOContext *pb = s->pb;
420  WAVMuxContext *wav = s->priv_data;
421  int64_t file_size, data_size;
422  int64_t number_of_samples = 0;
423  int rf64 = 0;
424  int ret = 0;
425 
426  avio_flush(pb);
427 
428  if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
429  if (wav->write_peak != 2 && avio_tell(pb) - wav->data < UINT32_MAX) {
430  ff_end_tag(pb, wav->data);
431  avio_flush(pb);
432  }
433 
434  if (wav->write_peak && wav->peak_output) {
435  ret = peak_write_chunk(s);
436  avio_flush(pb);
437  }
438 
439  /* update file size */
440  file_size = avio_tell(pb);
441  data_size = file_size - wav->data;
442  if (wav->rf64 == RF64_ALWAYS || (wav->rf64 == RF64_AUTO && file_size - 8 > UINT32_MAX)) {
443  rf64 = 1;
444  } else if (file_size - 8 <= UINT32_MAX) {
445  avio_seek(pb, 4, SEEK_SET);
446  avio_wl32(pb, (uint32_t)(file_size - 8));
447  avio_seek(pb, file_size, SEEK_SET);
448 
449  avio_flush(pb);
450  } else {
451  av_log(s, AV_LOG_ERROR,
452  "Filesize %"PRId64" invalid for wav, output file will be broken\n",
453  file_size);
454  }
455 
456  number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
457  s->streams[0]->codecpar->sample_rate * (int64_t)s->streams[0]->time_base.num,
458  s->streams[0]->time_base.den);
459 
460  if(s->streams[0]->codecpar->codec_tag != 0x01) {
461  /* Update num_samps in fact chunk */
462  avio_seek(pb, wav->fact_pos, SEEK_SET);
463  if (rf64 || (wav->rf64 == RF64_AUTO && number_of_samples > UINT32_MAX)) {
464  rf64 = 1;
465  avio_wl32(pb, -1);
466  } else {
467  avio_wl32(pb, number_of_samples);
468  avio_seek(pb, file_size, SEEK_SET);
469  avio_flush(pb);
470  }
471  }
472 
473  if (rf64) {
474  /* overwrite RIFF with RF64 */
475  avio_seek(pb, 0, SEEK_SET);
476  ffio_wfourcc(pb, "RF64");
477  avio_wl32(pb, -1);
478 
479  /* write ds64 chunk (overwrite JUNK if rf64 == RF64_AUTO) */
480  avio_seek(pb, wav->ds64 - 8, SEEK_SET);
481  ffio_wfourcc(pb, "ds64");
482  avio_wl32(pb, 28); /* ds64 chunk size */
483  avio_wl64(pb, file_size - 8); /* RF64 chunk size */
484  avio_wl64(pb, data_size); /* data chunk size */
485  avio_wl64(pb, number_of_samples); /* fact chunk number of samples */
486  avio_wl32(pb, 0); /* number of table entries for non-'data' chunks */
487 
488  /* write -1 in data chunk size */
489  avio_seek(pb, wav->data - 4, SEEK_SET);
490  avio_wl32(pb, -1);
491 
492  avio_seek(pb, file_size, SEEK_SET);
493  avio_flush(pb);
494  }
495  }
496 
497  if (wav->write_peak)
498  peak_free_buffers(s);
499 
500  return ret;
501 }
502 
503 #define OFFSET(x) offsetof(WAVMuxContext, x)
504 #define ENC AV_OPT_FLAG_ENCODING_PARAM
505 static const AVOption options[] = {
506  { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
507  { "write_peak", "Write Peak Envelope chunk.", OFFSET(write_peak), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, ENC, "peak" },
508  { "off", "Do not write peak chunk.", 0, AV_OPT_TYPE_CONST, { .i64 = PEAK_OFF }, 0, 0, ENC, "peak" },
509  { "on", "Append peak chunk after wav data.", 0, AV_OPT_TYPE_CONST, { .i64 = PEAK_ON }, 0, 0, ENC, "peak" },
510  { "only", "Write only peak chunk, omit wav data.", 0, AV_OPT_TYPE_CONST, { .i64 = PEAK_ONLY }, 0, 0, ENC, "peak" },
511  { "rf64", "Use RF64 header rather than RIFF for large files.", OFFSET(rf64), AV_OPT_TYPE_INT, { .i64 = RF64_NEVER },-1, 1, ENC, "rf64" },
512  { "auto", "Write RF64 header if file grows large enough.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_AUTO }, 0, 0, ENC, "rf64" },
513  { "always", "Always write RF64 header regardless of file size.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_ALWAYS }, 0, 0, ENC, "rf64" },
514  { "never", "Never write RF64 header regardless of file size.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_NEVER }, 0, 0, ENC, "rf64" },
515  { "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 },
516  { "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 },
517  { "peak_ppv", "Number of peak points per peak value (1 or 2).", OFFSET(peak_ppv), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, 2, ENC },
518  { NULL },
519 };
520 
521 static const AVClass wav_muxer_class = {
522  .class_name = "WAV muxer",
523  .item_name = av_default_item_name,
524  .option = options,
525  .version = LIBAVUTIL_VERSION_INT,
526 };
527 
528 AVOutputFormat ff_wav_muxer = {
529  .name = "wav",
530  .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
531  .mime_type = "audio/x-wav",
532  .extensions = "wav",
533  .priv_data_size = sizeof(WAVMuxContext),
534  .audio_codec = AV_CODEC_ID_PCM_S16LE,
535  .video_codec = AV_CODEC_ID_NONE,
536  .write_header = wav_write_header,
537  .write_packet = wav_write_packet,
538  .write_trailer = wav_write_trailer,
540  .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
541  .priv_class = &wav_muxer_class,
542 };
543 #endif /* CONFIG_WAV_MUXER */
544 
545 #if CONFIG_W64_MUXER
546 #include "w64.h"
547 
548 static void start_guid(AVIOContext *pb, const uint8_t *guid, int64_t *pos)
549 {
550  *pos = avio_tell(pb);
551 
552  avio_write(pb, guid, 16);
553  avio_wl64(pb, INT64_MAX);
554 }
555 
556 static void end_guid(AVIOContext *pb, int64_t start)
557 {
558  int64_t end, pos = avio_tell(pb);
559 
560  end = FFALIGN(pos, 8);
561  ffio_fill(pb, 0, end - pos);
562  avio_seek(pb, start + 16, SEEK_SET);
563  avio_wl64(pb, end - start);
564  avio_seek(pb, end, SEEK_SET);
565 }
566 
567 static int w64_write_header(AVFormatContext *s)
568 {
569  WAVMuxContext *wav = s->priv_data;
570  AVIOContext *pb = s->pb;
571  int64_t start;
572  int ret;
573 
575  avio_wl64(pb, -1);
577  start_guid(pb, ff_w64_guid_fmt, &start);
578  if ((ret = ff_put_wav_header(s, pb, s->streams[0]->codecpar, 0)) < 0) {
580  av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
581  codec ? codec->name : "NONE");
582  return ret;
583  }
584  end_guid(pb, start);
585 
586  if (s->streams[0]->codecpar->codec_tag != 0x01 /* hence for all other than PCM */
587  && (s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
588  start_guid(pb, ff_w64_guid_fact, &wav->fact_pos);
589  avio_wl64(pb, 0);
590  end_guid(pb, wav->fact_pos);
591  }
592 
593  start_guid(pb, ff_w64_guid_data, &wav->data);
594 
595  return 0;
596 }
597 
598 static int w64_write_trailer(AVFormatContext *s)
599 {
600  AVIOContext *pb = s->pb;
601  WAVMuxContext *wav = s->priv_data;
602  int64_t file_size;
603 
604  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
605  end_guid(pb, wav->data);
606 
607  file_size = avio_tell(pb);
608  avio_seek(pb, 16, SEEK_SET);
609  avio_wl64(pb, file_size);
610 
611  if (s->streams[0]->codecpar->codec_tag != 0x01) {
612  int64_t number_of_samples;
613 
614  number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
615  s->streams[0]->codecpar->sample_rate * (int64_t)s->streams[0]->time_base.num,
616  s->streams[0]->time_base.den);
617  avio_seek(pb, wav->fact_pos + 24, SEEK_SET);
618  avio_wl64(pb, number_of_samples);
619  }
620 
621  avio_seek(pb, file_size, SEEK_SET);
622  avio_flush(pb);
623  }
624 
625  return 0;
626 }
627 
628 AVOutputFormat ff_w64_muxer = {
629  .name = "w64",
630  .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
631  .extensions = "w64",
632  .priv_data_size = sizeof(WAVMuxContext),
633  .audio_codec = AV_CODEC_ID_PCM_S16LE,
634  .video_codec = AV_CODEC_ID_NONE,
635  .write_header = w64_write_header,
636  .write_packet = wav_write_packet,
637  .write_trailer = w64_write_trailer,
639  .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
640 };
641 #endif /* CONFIG_W64_MUXER */
Definition: wavenc.c:57
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:672
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:452
#define NULL
Definition: coverity.c:32
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:458
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:155
const uint8_t ff_w64_guid_wave[16]
Definition: w64.c:28
Buffered I/O operations.
int16_t * peak_maxpos
Definition: wavenc.c:73
void ff_end_tag(AVIOContext *pb, int64_t start)
Definition: riffenc.c:38
int64_t ff_start_tag(AVIOContext *pb, const char *tag)
Definition: riffenc.c:31
#define RF64_ALWAYS
Definition: wavenc.c:51
AVOption.
Definition: opt.h:246
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:135
int ff_put_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int flags)
Write WAVEFORMAT header structure.
Definition: riffenc.c:54
int64_t minpts
Definition: wavenc.c:71
const uint8_t ff_w64_guid_fact[16]
Definition: w64.c:38
const char * fmt
Definition: avisynth_c.h:769
int peak_format
Definition: wavenc.c:85
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4601
const char * desc
Definition: nvenc.c:60
int write_peak
Definition: wavenc.c:82
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4066
int num
Numerator.
Definition: rational.h:59
int size
Definition: avcodec.h:1658
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
static AVPacket pkt
int64_t ds64
Definition: wavenc.c:70
AVCodec.
Definition: avcodec.h:3681
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:496
This struct describes the properties of an encoded stream.
Definition: avcodec.h:4058
int64_t fact_pos
Definition: wavenc.c:69
int peak_block_pos
Definition: wavenc.c:86
Format I/O context.
Definition: avformat.h:1349
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:72
Public dictionary API.
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:358
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
AVOptions.
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1675
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1417
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:40
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1460
uint8_t * data
Definition: avcodec.h:1657
static int flags
Definition: log.c:57
uint32_t tag
Definition: movenc.c:1413
PeakFormat
Definition: wavenc.c:61
int64_t data
Definition: wavenc.c:68
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:525
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:205
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:66
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1477
const OptionDef options[]
Definition: ffserver.c:3948
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:446
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:3575
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1567
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
PeakType
Definition: wavenc.c:55
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
const char * name
Name of the codec implementation.
Definition: avcodec.h:3688
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:458
#define FFMAX(a, b)
Definition: common.h:94
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:229
int16_t * peak_maxneg
Definition: wavenc.c:73
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:3063
int write_bext
Definition: wavenc.c:81
int peak_block_size
Definition: wavenc.c:84
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1405
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:251
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:225
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
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:191
#define FFMIN(a, b)
Definition: common.h:96
static struct tm * localtime_r(const time_t *clock, struct tm *result)
Definition: time_internal.h:37
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
const uint8_t ff_w64_guid_data[16]
Definition: w64.c:42
const char * name
Definition: avformat.h:524
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
uint32_t peak_pos_pop
Definition: wavenc.c:77
const uint8_t ff_w64_guid_riff[16]
Definition: w64.c:23
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:374
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
#define RF64_AUTO
Definition: wavenc.c:49
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: utils.c:3168
int64_t maxpts
Definition: wavenc.c:72
#define ENC
Definition: caca.c:203
uint32_t peak_num_frames
Definition: wavenc.c:74
void ff_riff_write_info(AVFormatContext *s)
Write all recognized RIFF tags from s->metadata.
Definition: riffenc.c:327
Describe the class of an AVClass context structure.
Definition: log.h:67
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:101
#define RF64_NEVER
Definition: wavenc.c:50
const char * name
Name of the codec described by this descriptor.
Definition: avcodec.h:703
This struct describes the properties of a single codec described by an AVCodecID. ...
Definition: avcodec.h:695
int rf64
Definition: wavenc.c:83
const uint8_t ff_w64_guid_fmt[16]
Definition: w64.c:33
#define OFFSET(x)
Definition: ffmpeg_opt.c:3291
int sample_rate
Audio only.
Definition: avcodec.h:4176
Main libavformat public API header.
common internal and external API header
uint32_t peak_outbuf_size
Definition: wavenc.c:75
static double c[64]
uint32_t peak_outbuf_bytes
Definition: wavenc.c:76
#define AV_WL16(p, v)
Definition: intreadwrite.h:417
uint8_t * peak_output
Definition: wavenc.c:79
int den
Denominator.
Definition: rational.h:60
char * value
Definition: dict.h:87
int len
int peak_bps
Definition: wavenc.c:88
uint16_t peak_pop
Definition: wavenc.c:78
void * priv_data
Format private data.
Definition: avformat.h:1377
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:337
#define PEAK_BUFFER_SIZE
Definition: wavenc.c:53
int channels
Audio only.
Definition: avcodec.h:4172
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:690
AVCodecParameters * codecpar
Definition: avformat.h:1252
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:4070
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:926
This structure stores compressed data.
Definition: avcodec.h:1634
int peak_ppv
Definition: wavenc.c:87
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1650
#define FFMAX3(a, b, c)
Definition: common.h:95
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
int last_duration
Definition: wavenc.c:80