FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/wavenc.c
Date: 2026-04-24 19:58:39
Exec Total Coverage
Lines: 180 297 60.6%
Functions: 11 13 84.6%
Branches: 67 134 50.0%

Line Branch Exec Source
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"
40 #include "libavutil/intreadwrite.h"
41 #include "libavutil/mathematics.h"
42 #include "libavutil/mem.h"
43 #include "libavutil/opt.h"
44 #include "libavutil/time.h"
45 #include "libavutil/time_internal.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
58 typedef enum {
59 PEAK_OFF = 0,
60 PEAK_ON,
61 PEAK_ONLY
62 } PeakType;
63
64 typedef enum {
65 PEAK_FORMAT_UINT8 = 1,
66 PEAK_FORMAT_UINT16
67 } PeakFormat;
68
69 typedef struct WAVMuxContext {
70 const AVClass *class;
71 int64_t data;
72 int64_t fact_pos;
73 int64_t ds64;
74 int64_t minpts;
75 int64_t maxpts;
76 int16_t *peak_maxpos, *peak_maxneg;
77 uint32_t peak_num_frames;
78 unsigned peak_outbuf_size;
79 uint32_t peak_outbuf_bytes;
80 unsigned size_increment;
81 uint8_t *peak_output;
82 int last_duration;
83 int write_bext;
84 int write_peak;
85 int rf64;
86 int peak_block_size;
87 int peak_format;
88 int peak_block_pos;
89 int peak_ppv;
90 int peak_bps;
91 } WAVMuxContext;
92
93 #if CONFIG_WAV_MUXER
94 static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
95 {
96 AVDictionaryEntry *tag;
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
108 static 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
148 484 static av_cold void wav_deinit(AVFormatContext *s)
149 {
150 484 WAVMuxContext *wav = s->priv_data;
151
152 484 av_freep(&wav->peak_maxpos);
153 484 av_freep(&wav->peak_maxneg);
154 484 av_freep(&wav->peak_output);
155 484 }
156
157 2 static av_cold int peak_init_writer(AVFormatContext *s)
158 {
159 2 WAVMuxContext *wav = s->priv_data;
160 2 AVCodecParameters *par = s->streams[0]->codecpar;
161
162
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (par->codec_id != AV_CODEC_ID_PCM_S8 &&
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 par->codec_id != AV_CODEC_ID_PCM_S16LE &&
164 par->codec_id != AV_CODEC_ID_PCM_U8 &&
165 par->codec_id != AV_CODEC_ID_PCM_U16LE) {
166 av_log(s, AV_LOG_ERROR, "Codec %s not supported for Peak Chunk\n",
167 avcodec_get_name(par->codec_id));
168 return -1;
169 }
170
171 2 wav->peak_bps = av_get_bits_per_sample(par->codec_id) / 8;
172
173
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (wav->peak_bps == 1 && wav->peak_format == PEAK_FORMAT_UINT16) {
174 av_log(s, AV_LOG_ERROR,
175 "Writing 16 bit peak for 8 bit audio does not make sense\n");
176 return AVERROR(EINVAL);
177 }
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (par->ch_layout.nb_channels > INT_MAX / (wav->peak_bps * wav->peak_ppv))
179 return AVERROR(ERANGE);
180 2 wav->size_increment = par->ch_layout.nb_channels * wav->peak_bps * wav->peak_ppv;
181
182 2 wav->peak_maxpos = av_calloc(par->ch_layout.nb_channels, sizeof(*wav->peak_maxpos));
183 2 wav->peak_maxneg = av_calloc(par->ch_layout.nb_channels, sizeof(*wav->peak_maxneg));
184
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (!wav->peak_maxpos || !wav->peak_maxneg)
185 goto nomem;
186
187 2 return 0;
188
189 nomem:
190 av_log(s, AV_LOG_ERROR, "Out of memory\n");
191 return AVERROR(ENOMEM);
192 }
193
194 346 static int peak_write_frame(AVFormatContext *s)
195 {
196 346 WAVMuxContext *wav = s->priv_data;
197 346 AVCodecParameters *par = s->streams[0]->codecpar;
198 346 unsigned new_size = wav->peak_outbuf_bytes + wav->size_increment;
199 uint8_t *tmp;
200 int c;
201
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 346 times.
346 if (new_size > INT_MAX) {
203 wav->write_peak = PEAK_OFF;
204 return AVERROR(ERANGE);
205 }
206 346 tmp = av_fast_realloc(wav->peak_output, &wav->peak_outbuf_size, new_size);
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 346 times.
346 if (!tmp) {
208 wav->write_peak = PEAK_OFF;
209 return AVERROR(ENOMEM);
210 }
211 346 wav->peak_output = tmp;
212
213
2/2
✓ Branch 0 taken 346 times.
✓ Branch 1 taken 346 times.
692 for (c = 0; c < par->ch_layout.nb_channels; c++) {
214 346 wav->peak_maxneg[c] = -wav->peak_maxneg[c];
215
216
2/4
✓ Branch 0 taken 346 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 346 times.
346 if (wav->peak_bps == 2 && wav->peak_format == PEAK_FORMAT_UINT8) {
217 wav->peak_maxpos[c] = wav->peak_maxpos[c] / 256;
218 wav->peak_maxneg[c] = wav->peak_maxneg[c] / 256;
219 }
220
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 346 times.
346 if (wav->peak_ppv == 1)
222 wav->peak_maxpos[c] =
223 FFMAX(wav->peak_maxpos[c], wav->peak_maxneg[c]);
224
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 346 times.
346 if (wav->peak_format == PEAK_FORMAT_UINT8) {
226 wav->peak_output[wav->peak_outbuf_bytes++] =
227 wav->peak_maxpos[c];
228 if (wav->peak_ppv == 2) {
229 wav->peak_output[wav->peak_outbuf_bytes++] =
230 wav->peak_maxneg[c];
231 }
232 } else {
233 346 AV_WL16(wav->peak_output + wav->peak_outbuf_bytes,
234 wav->peak_maxpos[c]);
235 346 wav->peak_outbuf_bytes += 2;
236
1/2
✓ Branch 0 taken 346 times.
✗ Branch 1 not taken.
346 if (wav->peak_ppv == 2) {
237 346 AV_WL16(wav->peak_output + wav->peak_outbuf_bytes,
238 wav->peak_maxneg[c]);
239 346 wav->peak_outbuf_bytes += 2;
240 }
241 }
242 346 wav->peak_maxpos[c] = 0;
243 346 wav->peak_maxneg[c] = 0;
244 }
245 346 wav->peak_num_frames++;
246
247 346 return 0;
248 }
249
250 2 static int peak_write_chunk(AVFormatContext *s)
251 {
252 2 WAVMuxContext *wav = s->priv_data;
253 2 AVIOContext *pb = s->pb;
254 2 AVCodecParameters *par = s->streams[0]->codecpar;
255 2 int64_t peak = ff_start_tag(s->pb, "levl"); // codespell:ignore
256 int64_t now0;
257 time_t now_secs;
258 char timestamp[28];
259
260 /* Peak frame of incomplete block at end */
261
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (wav->peak_block_pos) {
262 2 int ret = peak_write_frame(s);
263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
264 return ret;
265 }
266
267 2 memset(timestamp, 0, sizeof(timestamp));
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
269 struct tm tmpbuf;
270 av_log(s, AV_LOG_INFO, "Writing local time and date to Peak Envelope Chunk\n");
271 now0 = av_gettime();
272 now_secs = now0 / 1000000;
273 if (strftime(timestamp, sizeof(timestamp), "%Y:%m:%d:%H:%M:%S:", localtime_r(&now_secs, &tmpbuf))) {
274 av_strlcatf(timestamp, sizeof(timestamp), "%03d", (int)((now0 / 1000) % 1000));
275 } else {
276 av_log(s, AV_LOG_ERROR, "Failed to write timestamp\n");
277 return -1;
278 }
279 }
280
281 2 avio_wl32(pb, 1); /* version */
282 2 avio_wl32(pb, wav->peak_format); /* 8 or 16 bit */
283 2 avio_wl32(pb, wav->peak_ppv); /* positive and negative */
284 2 avio_wl32(pb, wav->peak_block_size); /* frames per value */
285 2 avio_wl32(pb, par->ch_layout.nb_channels); /* number of channels */
286 2 avio_wl32(pb, wav->peak_num_frames); /* number of peak frames */
287 2 avio_wl32(pb, -1); /* audio sample frame position (not implemented) */
288 2 avio_wl32(pb, 128); /* equal to size of header */
289 2 avio_write(pb, timestamp, 28); /* ASCII time stamp */
290 2 ffio_fill(pb, 0, 60);
291
292 2 avio_write(pb, wav->peak_output, wav->peak_outbuf_bytes);
293
294 2 ff_end_tag(pb, peak);
295
296
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (!wav->data)
297 1 wav->data = peak;
298
299 2 return 0;
300 }
301
302 484 static int wav_write_header(AVFormatContext *s)
303 {
304 484 WAVMuxContext *wav = s->priv_data;
305 484 AVIOContext *pb = s->pb;
306 int64_t fmt;
307
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 484 times.
484 if (wav->rf64 == RF64_ALWAYS) {
309 ffio_wfourcc(pb, "RF64");
310 avio_wl32(pb, -1); /* RF64 chunk size: use size in ds64 */
311 } else {
312 484 ffio_wfourcc(pb, "RIFF");
313 484 avio_wl32(pb, -1); /* file length */
314 }
315
316 484 ffio_wfourcc(pb, "WAVE");
317
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 484 times.
484 if (wav->rf64 == RF64_ALWAYS) {
319 /* write empty ds64 chunk */
320 ffio_wfourcc(pb, "ds64");
321 avio_wl32(pb, 28); /* chunk size */
322 wav->ds64 = avio_tell(pb);
323 ffio_fill(pb, 0, 28);
324 }
325
326
2/2
✓ Branch 0 taken 483 times.
✓ Branch 1 taken 1 times.
484 if (wav->write_peak != PEAK_ONLY) {
327 /* format header */
328 483 fmt = ff_start_tag(pb, "fmt ");
329
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 483 times.
483 if (ff_put_wav_header(s, pb, s->streams[0]->codecpar, 0) < 0) {
330 av_log(s, AV_LOG_ERROR, "Codec %s not supported in WAVE format\n",
331 avcodec_get_name(s->streams[0]->codecpar->codec_id));
332 return AVERROR(ENOSYS);
333 }
334 483 ff_end_tag(pb, fmt);
335
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 483 times.
483 if (wav->rf64 == RF64_AUTO) {
337 /* reserve space for ds64 */
338 ffio_wfourcc(pb, "JUNK");
339 avio_wl32(pb, 28); /* chunk size */
340 ffio_fill(pb, 0, 28);
341
342 /* in RF64_AUTO mode, fmt + JUNK will be overwritten by ds64 + fmt */
343 wav->ds64 = fmt;
344 }
345 }
346
347
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 468 times.
484 if (s->streams[0]->codecpar->codec_tag != 0x01 /* hence for all other than PCM */
348
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 && (s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
349 16 wav->fact_pos = ff_start_tag(pb, "fact");
350 16 avio_wl32(pb, 0);
351 16 ff_end_tag(pb, wav->fact_pos);
352 }
353
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 484 times.
484 if (wav->write_bext)
355 bwf_write_bext_chunk(s);
356
357
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 482 times.
484 if (wav->write_peak) {
358 int ret;
359
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = peak_init_writer(s)) < 0)
360 return ret;
361 }
362
363 484 avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codecpar->sample_rate);
364 484 wav->maxpts = wav->last_duration = 0;
365 484 wav->minpts = INT64_MAX;
366
367
2/2
✓ Branch 0 taken 483 times.
✓ Branch 1 taken 1 times.
484 if (wav->write_peak != PEAK_ONLY) {
368 /* info header */
369 483 ff_riff_write_info(s);
370
371 /* data header */
372 483 wav->data = ff_start_tag(pb, "data");
373 }
374
375 484 return 0;
376 }
377
378 74903 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
379 {
380 74903 AVIOContext *pb = s->pb;
381 74903 WAVMuxContext *wav = s->priv_data;
382
383
2/2
✓ Branch 0 taken 74892 times.
✓ Branch 1 taken 11 times.
74903 if (wav->write_peak != PEAK_ONLY)
384 74892 avio_write(pb, pkt->data, pkt->size);
385
386
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 74881 times.
74903 if (wav->write_peak) {
387 22 int c = 0;
388 int i;
389
2/2
✓ Branch 0 taken 88200 times.
✓ Branch 1 taken 22 times.
88222 for (i = 0; i < pkt->size; i += wav->peak_bps) {
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88200 times.
88200 if (wav->peak_bps == 1) {
391 wav->peak_maxpos[c] = FFMAX(wav->peak_maxpos[c], *(int8_t*)(pkt->data + i));
392 wav->peak_maxneg[c] = FFMIN(wav->peak_maxneg[c], *(int8_t*)(pkt->data + i));
393 } else {
394 88200 wav->peak_maxpos[c] = FFMAX(wav->peak_maxpos[c], (int16_t)AV_RL16(pkt->data + i));
395 88200 wav->peak_maxneg[c] = FFMIN(wav->peak_maxneg[c], (int16_t)AV_RL16(pkt->data + i));
396 }
397
1/2
✓ Branch 0 taken 88200 times.
✗ Branch 1 not taken.
88200 if (++c == s->streams[0]->codecpar->ch_layout.nb_channels) {
398 88200 c = 0;
399
2/2
✓ Branch 0 taken 344 times.
✓ Branch 1 taken 87856 times.
88200 if (++wav->peak_block_pos == wav->peak_block_size) {
400 344 int ret = peak_write_frame(s);
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 344 times.
344 if (ret < 0)
402 return ret;
403 344 wav->peak_block_pos = 0;
404 }
405 }
406 }
407 }
408
409
1/2
✓ Branch 0 taken 74903 times.
✗ Branch 1 not taken.
74903 if(pkt->pts != AV_NOPTS_VALUE) {
410 74903 wav->minpts = FFMIN(wav->minpts, pkt->pts);
411 74903 wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
412 74903 wav->last_duration = pkt->duration;
413 } else
414 av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
415 74903 return 0;
416 }
417
418 484 static int wav_write_trailer(AVFormatContext *s)
419 {
420 484 AVIOContext *pb = s->pb;
421 484 WAVMuxContext *wav = s->priv_data;
422 int64_t file_size, data_size;
423 484 int64_t number_of_samples = 0;
424 int64_t pos;
425 484 int rf64 = 0;
426 484 int ret = 0;
427
428
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 353 times.
484 if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
429
3/4
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 130 times.
✗ Branch 4 not taken.
131 if (wav->write_peak != PEAK_ONLY && avio_tell(pb) - wav->data < UINT32_MAX) {
430 130 ff_end_tag(pb, wav->data);
431 }
432
433
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 129 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
131 if (wav->write_peak && wav->peak_output) {
434 2 ret = peak_write_chunk(s);
435 }
436
437 /* update file size */
438 131 file_size = avio_tell(pb);
439 131 data_size = file_size - wav->data;
440
2/6
✓ Branch 0 taken 131 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 131 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
131 if (wav->rf64 == RF64_ALWAYS || (wav->rf64 == RF64_AUTO && file_size - 8 > UINT32_MAX)) {
441 rf64 = 1;
442
1/2
✓ Branch 0 taken 131 times.
✗ Branch 1 not taken.
131 } else if (file_size - 8 <= UINT32_MAX) {
443 131 avio_seek(pb, 4, SEEK_SET);
444 131 avio_wl32(pb, (uint32_t)(file_size - 8));
445 131 avio_seek(pb, file_size, SEEK_SET);
446 } else {
447 av_log(s, AV_LOG_ERROR,
448 "Filesize %"PRId64" invalid for wav, output file will be broken\n",
449 file_size);
450 }
451 262 number_of_samples = av_rescale_q(wav->maxpts - wav->minpts + wav->last_duration,
452 131 s->streams[0]->time_base,
453 131 av_make_q(1, s->streams[0]->codecpar->sample_rate));
454
455
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 115 times.
131 if(s->streams[0]->codecpar->codec_tag != 0x01) {
456 /* Update num_samps in fact chunk */
457 16 avio_seek(pb, wav->fact_pos, SEEK_SET);
458
2/6
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
16 if (rf64 || (wav->rf64 == RF64_AUTO && number_of_samples > UINT32_MAX)) {
459 rf64 = 1;
460 avio_wl32(pb, -1);
461 } else {
462 16 avio_wl32(pb, number_of_samples);
463 16 avio_seek(pb, file_size, SEEK_SET);
464 }
465 }
466
467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 131 times.
131 if (rf64) {
468 /* overwrite RIFF with RF64 */
469 avio_seek(pb, 0, SEEK_SET);
470 ffio_wfourcc(pb, "RF64");
471 avio_wl32(pb, -1);
472
473 /* write ds64 chunk (overwrite fmt + JUNK if rf64 == RF64_AUTO) */
474 avio_seek(pb, wav->ds64 - 8, SEEK_SET);
475 ffio_wfourcc(pb, "ds64");
476 avio_wl32(pb, 28); /* ds64 chunk size */
477 avio_wl64(pb, file_size - 8); /* RF64 chunk size */
478 avio_wl64(pb, data_size); /* data chunk size */
479 avio_wl64(pb, number_of_samples); /* fact chunk number of samples */
480 avio_wl32(pb, 0); /* number of table entries for non-'data' chunks */
481
482 /* rewrite fmt in its RF64 position after ds64 */
483 pos = ff_start_tag(pb, "fmt ");
484 ret = ff_put_wav_header(s, pb, s->streams[0]->codecpar, 0);
485 ff_end_tag(pb, pos);
486
487 /* write -1 in data chunk size */
488 avio_seek(pb, wav->data - 4, SEEK_SET);
489 avio_wl32(pb, -1);
490
491 avio_seek(pb, file_size, SEEK_SET);
492 }
493 }
494
495 484 return ret;
496 }
497
498 #define OFFSET(x) offsetof(WAVMuxContext, x)
499 #define ENC AV_OPT_FLAG_ENCODING_PARAM
500 static const AVOption options[] = {
501 { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
502 { "write_peak", "Write Peak Envelope chunk.", OFFSET(write_peak), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, ENC, .unit = "peak" },
503 { "off", "Do not write peak chunk.", 0, AV_OPT_TYPE_CONST, { .i64 = PEAK_OFF }, 0, 0, ENC, .unit = "peak" },
504 { "on", "Append peak chunk after wav data.", 0, AV_OPT_TYPE_CONST, { .i64 = PEAK_ON }, 0, 0, ENC, .unit = "peak" },
505 { "only", "Write only peak chunk, omit wav data.", 0, AV_OPT_TYPE_CONST, { .i64 = PEAK_ONLY }, 0, 0, ENC, .unit = "peak" },
506 { "rf64", "Use RF64 header rather than RIFF for large files.", OFFSET(rf64), AV_OPT_TYPE_INT, { .i64 = RF64_NEVER },-1, 1, ENC, .unit = "rf64" },
507 { "auto", "Write RF64 header if file grows large enough.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_AUTO }, 0, 0, ENC, .unit = "rf64" },
508 { "always", "Always write RF64 header regardless of file size.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_ALWAYS }, 0, 0, ENC, .unit = "rf64" },
509 { "never", "Never write RF64 header regardless of file size.", 0, AV_OPT_TYPE_CONST, { .i64 = RF64_NEVER }, 0, 0, ENC, .unit = "rf64" },
510 { "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 },
511 { "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 },
512 { "peak_ppv", "Number of peak points per peak value (1 or 2).", OFFSET(peak_ppv), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, 2, ENC },
513 { NULL },
514 };
515
516 static const AVClass wav_muxer_class = {
517 .class_name = "WAV muxer",
518 .item_name = av_default_item_name,
519 .option = options,
520 .version = LIBAVUTIL_VERSION_INT,
521 };
522
523 const FFOutputFormat ff_wav_muxer = {
524 .p.name = "wav",
525 .p.long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
526 .p.mime_type = "audio/x-wav",
527 .p.extensions = "wav",
528 .priv_data_size = sizeof(WAVMuxContext),
529 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
530 .p.video_codec = AV_CODEC_ID_NONE,
531 .p.subtitle_codec = AV_CODEC_ID_NONE,
532 .write_header = wav_write_header,
533 .write_packet = wav_write_packet,
534 .write_trailer = wav_write_trailer,
535 .deinit = wav_deinit,
536 .p.flags = AVFMT_TS_NONSTRICT,
537 .p.codec_tag = ff_wav_codec_tags_list,
538 .p.priv_class = &wav_muxer_class,
539 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH,
540 };
541 #endif /* CONFIG_WAV_MUXER */
542
543 #if CONFIG_W64_MUXER
544 #include "w64.h"
545
546 2 static void start_guid(AVIOContext *pb, const uint8_t *guid, int64_t *pos)
547 {
548 2 *pos = avio_tell(pb);
549
550 2 avio_write(pb, guid, 16);
551 2 avio_wl64(pb, INT64_MAX);
552 2 }
553
554 2 static void end_guid(AVIOContext *pb, int64_t start)
555 {
556 2 int64_t end, pos = avio_tell(pb);
557
558 2 end = FFALIGN(pos, 8);
559 2 ffio_fill(pb, 0, end - pos);
560 2 avio_seek(pb, start + 16, SEEK_SET);
561 2 avio_wl64(pb, end - start);
562 2 avio_seek(pb, end, SEEK_SET);
563 2 }
564
565 1 static int w64_write_header(AVFormatContext *s)
566 {
567 1 WAVMuxContext *wav = s->priv_data;
568 1 AVIOContext *pb = s->pb;
569 int64_t start;
570 int ret;
571
572 1 avio_write(pb, ff_w64_guid_riff, sizeof(ff_w64_guid_riff));
573 1 avio_wl64(pb, -1);
574 1 avio_write(pb, ff_w64_guid_wave, sizeof(ff_w64_guid_wave));
575 1 start_guid(pb, ff_w64_guid_fmt, &start);
576
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = ff_put_wav_header(s, pb, s->streams[0]->codecpar, 0)) < 0) {
577 av_log(s, AV_LOG_ERROR, "Codec %s not supported\n",
578 avcodec_get_name(s->streams[0]->codecpar->codec_id));
579 return ret;
580 }
581 1 end_guid(pb, start);
582
583
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (s->streams[0]->codecpar->codec_tag != 0x01 /* hence for all other than PCM */
584 && (s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
585 start_guid(pb, ff_w64_guid_fact, &wav->fact_pos);
586 avio_wl64(pb, 0);
587 end_guid(pb, wav->fact_pos);
588 }
589
590 1 start_guid(pb, ff_w64_guid_data, &wav->data);
591
592 1 return 0;
593 }
594
595 1 static int w64_write_trailer(AVFormatContext *s)
596 {
597 1 AVIOContext *pb = s->pb;
598 1 WAVMuxContext *wav = s->priv_data;
599 int64_t file_size;
600
601
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
602 1 end_guid(pb, wav->data);
603
604 1 file_size = avio_tell(pb);
605 1 avio_seek(pb, 16, SEEK_SET);
606 1 avio_wl64(pb, file_size);
607
608
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (s->streams[0]->codecpar->codec_tag != 0x01) {
609 int64_t number_of_samples;
610
611 number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
612 s->streams[0]->codecpar->sample_rate * (int64_t)s->streams[0]->time_base.num,
613 s->streams[0]->time_base.den);
614 avio_seek(pb, wav->fact_pos + 24, SEEK_SET);
615 avio_wl64(pb, number_of_samples);
616 }
617
618 1 avio_seek(pb, file_size, SEEK_SET);
619 }
620
621 1 return 0;
622 }
623
624 const FFOutputFormat ff_w64_muxer = {
625 .p.name = "w64",
626 .p.long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
627 .p.extensions = "w64",
628 .priv_data_size = sizeof(WAVMuxContext),
629 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
630 .p.video_codec = AV_CODEC_ID_NONE,
631 .p.subtitle_codec = AV_CODEC_ID_NONE,
632 .write_header = w64_write_header,
633 .write_packet = wav_write_packet,
634 .write_trailer = w64_write_trailer,
635 .p.flags = AVFMT_TS_NONSTRICT,
636 .p.codec_tag = ff_wav_codec_tags_list,
637 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH,
638 };
639 #endif /* CONFIG_W64_MUXER */
640