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