Libav
wavenc.c
Go to the documentation of this file.
1 /*
2  * WAV muxer
3  * Copyright (c) 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <stdint.h>
23 #include <string.h>
24 
25 #include "libavutil/dict.h"
26 #include "libavutil/common.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/opt.h"
29 
30 #include "avformat.h"
31 #include "avio.h"
32 #include "avio_internal.h"
33 #include "internal.h"
34 #include "riff.h"
35 
36 typedef struct WAVMuxContext {
37  const AVClass *class;
38  int64_t data;
39  int64_t fact_pos;
40  int64_t minpts;
41  int64_t maxpts;
45 
46 static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
47 {
49  int len = 0;
50 
51  if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
52  len = strlen(tag->value);
53  len = FFMIN(len, maxlen);
54  avio_write(s->pb, tag->value, len);
55  }
56 
57  ffio_fill(s->pb, 0, maxlen - len);
58 }
59 
61 {
62  AVDictionaryEntry *tmp_tag;
63  uint64_t time_reference = 0;
64  int64_t bext = ff_start_tag(s->pb, "bext");
65 
66  bwf_write_bext_string(s, "description", 256);
67  bwf_write_bext_string(s, "originator", 32);
68  bwf_write_bext_string(s, "originator_reference", 32);
69  bwf_write_bext_string(s, "origination_date", 10);
70  bwf_write_bext_string(s, "origination_time", 8);
71 
72  if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0))
73  time_reference = strtoll(tmp_tag->value, NULL, 10);
74  avio_wl64(s->pb, time_reference);
75  avio_wl16(s->pb, 1); // set version to 1
76 
77  if (tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) {
78  unsigned char umidpart_str[17] = {0};
79  int i;
80  uint64_t umidpart;
81  int len = strlen(tmp_tag->value+2);
82 
83  for (i = 0; i < len/16; i++) {
84  memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16);
85  umidpart = strtoll(umidpart_str, NULL, 16);
86  avio_wb64(s->pb, umidpart);
87  }
88  ffio_fill(s->pb, 0, 64 - i*8);
89  } else
90  ffio_fill(s->pb, 0, 64); // zero UMID
91 
92  ffio_fill(s->pb, 0, 190); // Reserved
93 
94  if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
95  avio_put_str(s->pb, tmp_tag->value);
96 
97  ff_end_tag(s->pb, bext);
98 }
99 
101 {
102  WAVMuxContext *wav = s->priv_data;
103  AVIOContext *pb = s->pb;
104  int64_t fmt;
105 
106  ffio_wfourcc(pb, "RIFF");
107  avio_wl32(pb, 0); /* file length */
108  ffio_wfourcc(pb, "WAVE");
109 
110  /* format header */
111  fmt = ff_start_tag(pb, "fmt ");
112  if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
114  av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
115  desc ? desc->name : "unknown");
116  return AVERROR(ENOSYS);
117  }
118  ff_end_tag(pb, fmt);
119 
120  if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
121  && s->pb->seekable) {
122  wav->fact_pos = ff_start_tag(pb, "fact");
123  avio_wl32(pb, 0);
124  ff_end_tag(pb, wav->fact_pos);
125  }
126 
127  if (wav->write_bext)
129 
130  avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
131  wav->maxpts = wav->last_duration = 0;
132  wav->minpts = INT64_MAX;
133 
134  /* info header */
136 
137  /* data header */
138  wav->data = ff_start_tag(pb, "data");
139 
140  avio_flush(pb);
141 
142  return 0;
143 }
144 
146 {
147  AVIOContext *pb = s->pb;
148  WAVMuxContext *wav = s->priv_data;
149  avio_write(pb, pkt->data, pkt->size);
150  if(pkt->pts != AV_NOPTS_VALUE) {
151  wav->minpts = FFMIN(wav->minpts, pkt->pts);
152  wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
153  wav->last_duration = pkt->duration;
154  } else
155  av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
156  return 0;
157 }
158 
160 {
161  AVIOContext *pb = s->pb;
162  WAVMuxContext *wav = s->priv_data;
163  int64_t file_size;
164 
165  avio_flush(pb);
166 
167  if (s->pb->seekable) {
168  ff_end_tag(pb, wav->data);
169 
170  /* update file size */
171  file_size = avio_tell(pb);
172  avio_seek(pb, 4, SEEK_SET);
173  avio_wl32(pb, (uint32_t)(file_size - 8));
174  avio_seek(pb, file_size, SEEK_SET);
175 
176  avio_flush(pb);
177 
178  if(s->streams[0]->codec->codec_tag != 0x01) {
179  /* Update num_samps in fact chunk */
180  int number_of_samples;
181  number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
182  s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
183  s->streams[0]->time_base.den);
184  avio_seek(pb, wav->fact_pos, SEEK_SET);
185  avio_wl32(pb, number_of_samples);
186  avio_seek(pb, file_size, SEEK_SET);
187  avio_flush(pb);
188  }
189  }
190  return 0;
191 }
192 
193 #define OFFSET(x) offsetof(WAVMuxContext, x)
194 #define ENC AV_OPT_FLAG_ENCODING_PARAM
195 static const AVOption options[] = {
196  { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
197  { NULL },
198 };
199 
200 static const AVClass wav_muxer_class = {
201  .class_name = "WAV muxer",
202  .item_name = av_default_item_name,
203  .option = options,
204  .version = LIBAVUTIL_VERSION_INT,
205 };
206 
208  .name = "wav",
209  .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
210  .mime_type = "audio/x-wav",
211  .extensions = "wav",
212  .priv_data_size = sizeof(WAVMuxContext),
213  .audio_codec = AV_CODEC_ID_PCM_S16LE,
214  .video_codec = AV_CODEC_ID_NONE,
219  .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
220  .priv_class = &wav_muxer_class,
221 };
#define FFMAX(a, b)
Definition: common.h:55
Bytestream IO Context.
Definition: avio.h:68
AVOutputFormat ff_wav_muxer
Definition: wavenc.c:207
#define ENC
Definition: wavenc.c:194
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
AVOption.
Definition: opt.h:234
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:426
int64_t minpts
Definition: wavenc.c:40
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:2821
const char * name
Name of the codec described by this descriptor.
Definition: avcodec.h:486
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:2353
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:974
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:165
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:180
static int wav_write_trailer(AVFormatContext *s)
Definition: wavenc.c:159
int64_t fact_pos
Definition: wavenc.c:39
Format I/O context.
Definition: avformat.h:922
static int wav_write_header(AVFormatContext *s)
Definition: wavenc.c:100
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:38
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:260
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
static int flags
Definition: log.c:44
uint32_t tag
Definition: movenc.c:844
int64_t data
Definition: wavenc.c:38
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:67
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:991
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: wavenc.c:145
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void * priv_data
Format private data.
Definition: avformat.h:950
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
#define OFFSET(x)
Definition: wavenc.c:193
static void bwf_write_bext_chunk(AVFormatContext *s)
Definition: wavenc.c:60
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:353
int write_bext
Definition: wavenc.c:43
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
int64_t av_rescale(int64_t a, int64_t b, int64_t c) av_const
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:116
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:151
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
static const AVClass wav_muxer_class
Definition: wavenc.c:200
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
char * value
Definition: dict.h:76
const char * name
Definition: avformat.h:446
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:324
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
NULL
Definition: eval.c:55
Buffered I/O operations.
enum AVCodecID codec_id
Definition: avcodec.h:1067
int sample_rate
samples per second
Definition: avcodec.h:1791
int64_t maxpts
Definition: wavenc.c:41
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:330
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1082
static void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
Definition: wavenc.c:46
AVIOContext * pb
I/O context.
Definition: avformat.h:964
void ff_riff_write_info(AVFormatContext *s)
Write all recognized RIFF tags from s->metadata.
Definition: riffenc.c:276
Describe the class of an AVClass context structure.
Definition: log.h:33
uint8_t * data
Definition: avcodec.h:973
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1130
This struct describes the properties of a single codec described by an AVCodecID. ...
Definition: avcodec.h:478
static const AVOption options[]
Definition: wavenc.c:195
Main libavformat public API header.
int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc)
Definition: riffenc.c:50
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:38
int den
denominator
Definition: rational.h:45
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:276
int len
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:380
av_default_item_name
Return the context name.
Definition: dnxhdenc.c:52
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:741
This structure stores compressed data.
Definition: avcodec.h:950
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
#define FFMIN(a, b)
Definition: common.h:57
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:336
int last_duration
Definition: wavenc.c:42