FFmpeg coverage


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