LCOV - code coverage report
Current view: top level - libavformat - wavenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 184 298 61.7 %
Date: 2017-12-14 08:27:08 Functions: 11 13 84.6 %

          Line data    Source code
       1             : /*
       2             :  * WAV muxer
       3             :  * Copyright (c) 2001, 2002 Fabrice Bellard
       4             :  *
       5             :  * Sony Wave64 muxer
       6             :  * Copyright (c) 2012 Paul B Mahol
       7             :  *
       8             :  * WAV muxer RF64 support
       9             :  * Copyright (c) 2013 Daniel Verkamp <daniel@drv.nu>
      10             :  *
      11             :  * EBU Tech 3285 - Supplement 3 - Peak Envelope Chunk encoder
      12             :  * Copyright (c) 2014 Georg Lippitsch <georg.lippitsch@gmx.at>
      13             :  *
      14             :  * This file is part of FFmpeg.
      15             :  *
      16             :  * FFmpeg is free software; you can redistribute it and/or
      17             :  * modify it under the terms of the GNU Lesser General Public
      18             :  * License as published by the Free Software Foundation; either
      19             :  * version 2.1 of the License, or (at your option) any later version.
      20             :  *
      21             :  * FFmpeg is distributed in the hope that it will be useful,
      22             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      23             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      24             :  * Lesser General Public License for more details.
      25             :  *
      26             :  * You should have received a copy of the GNU Lesser General Public
      27             :  * License along with FFmpeg; if not, write to the Free Software
      28             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      29             :  */
      30             : 
      31             : #include <stdint.h>
      32             : #include <string.h>
      33             : 
      34             : #include "libavutil/avstring.h"
      35             : #include "libavutil/dict.h"
      36             : #include "libavutil/common.h"
      37             : #include "libavutil/intreadwrite.h"
      38             : #include "libavutil/mathematics.h"
      39             : #include "libavutil/opt.h"
      40             : #include "libavutil/time.h"
      41             : #include "libavutil/time_internal.h"
      42             : 
      43             : #include "avformat.h"
      44             : #include "avio.h"
      45             : #include "avio_internal.h"
      46             : #include "internal.h"
      47             : #include "riff.h"
      48             : 
      49             : #define RF64_AUTO   (-1)
      50             : #define RF64_NEVER  0
      51             : #define RF64_ALWAYS 1
      52             : 
      53             : #define PEAK_BUFFER_SIZE   1024
      54             : 
      55             : typedef enum {
      56             :     PEAK_OFF = 0,
      57             :     PEAK_ON,
      58             :     PEAK_ONLY
      59             : } PeakType;
      60             : 
      61             : typedef enum {
      62             :     PEAK_FORMAT_UINT8 = 1,
      63             :     PEAK_FORMAT_UINT16
      64             : } PeakFormat;
      65             : 
      66             : typedef struct WAVMuxContext {
      67             :     const AVClass *class;
      68             :     int64_t data;
      69             :     int64_t fact_pos;
      70             :     int64_t ds64;
      71             :     int64_t minpts;
      72             :     int64_t maxpts;
      73             :     int16_t *peak_maxpos, *peak_maxneg;
      74             :     uint32_t peak_num_frames;
      75             :     uint32_t peak_outbuf_size;
      76             :     uint32_t peak_outbuf_bytes;
      77             :     uint8_t *peak_output;
      78             :     int last_duration;
      79             :     int write_bext;
      80             :     int write_peak;
      81             :     int rf64;
      82             :     int peak_block_size;
      83             :     int peak_format;
      84             :     int peak_block_pos;
      85             :     int peak_ppv;
      86             :     int peak_bps;
      87             : } WAVMuxContext;
      88             : 
      89             : #if CONFIG_WAV_MUXER
      90           0 : static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
      91             : {
      92             :     AVDictionaryEntry *tag;
      93           0 :     size_t len = 0;
      94             : 
      95           0 :     if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
      96           0 :         len = strlen(tag->value);
      97           0 :         len = FFMIN(len, maxlen);
      98           0 :         avio_write(s->pb, tag->value, len);
      99             :     }
     100             : 
     101           0 :     ffio_fill(s->pb, 0, maxlen - len);
     102           0 : }
     103             : 
     104           0 : static void bwf_write_bext_chunk(AVFormatContext *s)
     105             : {
     106             :     AVDictionaryEntry *tmp_tag;
     107           0 :     uint64_t time_reference = 0;
     108           0 :     int64_t bext = ff_start_tag(s->pb, "bext");
     109             : 
     110           0 :     bwf_write_bext_string(s, "description", 256);
     111           0 :     bwf_write_bext_string(s, "originator", 32);
     112           0 :     bwf_write_bext_string(s, "originator_reference", 32);
     113           0 :     bwf_write_bext_string(s, "origination_date", 10);
     114           0 :     bwf_write_bext_string(s, "origination_time", 8);
     115             : 
     116           0 :     if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0))
     117           0 :         time_reference = strtoll(tmp_tag->value, NULL, 10);
     118           0 :     avio_wl64(s->pb, time_reference);
     119           0 :     avio_wl16(s->pb, 1);  // set version to 1
     120             : 
     121           0 :     if ((tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) && strlen(tmp_tag->value) > 2) {
     122           0 :         unsigned char umidpart_str[17] = {0};
     123             :         int64_t i;
     124             :         uint64_t umidpart;
     125           0 :         size_t len = strlen(tmp_tag->value+2);
     126             : 
     127           0 :         for (i = 0; i < len/16; i++) {
     128           0 :             memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16);
     129           0 :             umidpart = strtoll(umidpart_str, NULL, 16);
     130           0 :             avio_wb64(s->pb, umidpart);
     131             :         }
     132           0 :         ffio_fill(s->pb, 0, 64 - i*8);
     133             :     } else
     134           0 :         ffio_fill(s->pb, 0, 64); // zero UMID
     135             : 
     136           0 :     ffio_fill(s->pb, 0, 190); // Reserved
     137             : 
     138           0 :     if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
     139           0 :         avio_put_str(s->pb, tmp_tag->value);
     140             : 
     141           0 :     ff_end_tag(s->pb, bext);
     142           0 : }
     143             : 
     144           2 : static av_cold void peak_free_buffers(AVFormatContext *s)
     145             : {
     146           2 :     WAVMuxContext *wav = s->priv_data;
     147             : 
     148           2 :     av_freep(&wav->peak_maxpos);
     149           2 :     av_freep(&wav->peak_maxneg);
     150           2 :     av_freep(&wav->peak_output);
     151           2 : }
     152             : 
     153           2 : static av_cold int peak_init_writer(AVFormatContext *s)
     154             : {
     155           2 :     WAVMuxContext *wav = s->priv_data;
     156           2 :     AVCodecParameters *par = s->streams[0]->codecpar;
     157             : 
     158           4 :     if (par->codec_id != AV_CODEC_ID_PCM_S8 &&
     159           2 :         par->codec_id != AV_CODEC_ID_PCM_S16LE &&
     160           0 :         par->codec_id != AV_CODEC_ID_PCM_U8 &&
     161           0 :         par->codec_id != AV_CODEC_ID_PCM_U16LE) {
     162           0 :         AVCodec *codec = avcodec_find_decoder(s->streams[0]->codecpar->codec_id);
     163           0 :         av_log(s, AV_LOG_ERROR, "%s codec not supported for Peak Chunk\n",
     164             :                codec ? codec->name : "NONE");
     165           0 :         return -1;
     166             :     }
     167             : 
     168           2 :     wav->peak_bps = av_get_bits_per_sample(par->codec_id) / 8;
     169             : 
     170           2 :     if (wav->peak_bps == 1 && wav->peak_format == PEAK_FORMAT_UINT16) {
     171           0 :         av_log(s, AV_LOG_ERROR,
     172             :                "Writing 16 bit peak for 8 bit audio does not make sense\n");
     173           0 :         return AVERROR(EINVAL);
     174             :     }
     175             : 
     176           2 :     wav->peak_maxpos = av_mallocz_array(par->channels, sizeof(*wav->peak_maxpos));
     177           2 :     wav->peak_maxneg = av_mallocz_array(par->channels, sizeof(*wav->peak_maxneg));
     178           2 :     wav->peak_output = av_malloc(PEAK_BUFFER_SIZE);
     179           4 :     if (!wav->peak_maxpos || !wav->peak_maxneg || !wav->peak_output)
     180             :         goto nomem;
     181             : 
     182           2 :     wav->peak_outbuf_size = PEAK_BUFFER_SIZE;
     183             : 
     184           2 :     return 0;
     185             : 
     186           0 : nomem:
     187           0 :     av_log(s, AV_LOG_ERROR, "Out of memory\n");
     188           0 :     peak_free_buffers(s);
     189           0 :     return AVERROR(ENOMEM);
     190             : }
     191             : 
     192         346 : static void peak_write_frame(AVFormatContext *s)
     193             : {
     194         346 :     WAVMuxContext *wav = s->priv_data;
     195         346 :     AVCodecParameters *par = s->streams[0]->codecpar;
     196             :     int c;
     197             : 
     198         346 :     if (!wav->peak_output)
     199           0 :         return;
     200             : 
     201         692 :     for (c = 0; c < par->channels; c++) {
     202         346 :         wav->peak_maxneg[c] = -wav->peak_maxneg[c];
     203             : 
     204         346 :         if (wav->peak_bps == 2 && wav->peak_format == PEAK_FORMAT_UINT8) {
     205           0 :             wav->peak_maxpos[c] = wav->peak_maxpos[c] / 256;
     206           0 :             wav->peak_maxneg[c] = wav->peak_maxneg[c] / 256;
     207             :         }
     208             : 
     209         346 :         if (wav->peak_ppv == 1)
     210           0 :             wav->peak_maxpos[c] =
     211           0 :                 FFMAX(wav->peak_maxpos[c], wav->peak_maxneg[c]);
     212             : 
     213         692 :         if (wav->peak_outbuf_size - wav->peak_outbuf_bytes <
     214         346 :             wav->peak_format * wav->peak_ppv) {
     215           0 :             wav->peak_outbuf_size += PEAK_BUFFER_SIZE;
     216           0 :             wav->peak_output = av_realloc(wav->peak_output,
     217           0 :                                           wav->peak_outbuf_size);
     218           0 :             if (!wav->peak_output) {
     219           0 :                 av_log(s, AV_LOG_ERROR, "No memory for peak data\n");
     220           0 :                 return;
     221             :             }
     222             :         }
     223             : 
     224         346 :         if (wav->peak_format == PEAK_FORMAT_UINT8) {
     225           0 :             wav->peak_output[wav->peak_outbuf_bytes++] =
     226           0 :                 wav->peak_maxpos[c];
     227           0 :             if (wav->peak_ppv == 2) {
     228           0 :                 wav->peak_output[wav->peak_outbuf_bytes++] =
     229           0 :                     wav->peak_maxneg[c];
     230             :             }
     231             :         } else {
     232         346 :             AV_WL16(wav->peak_output + wav->peak_outbuf_bytes,
     233             :                     wav->peak_maxpos[c]);
     234         346 :             wav->peak_outbuf_bytes += 2;
     235         346 :             if (wav->peak_ppv == 2) {
     236         346 :                 AV_WL16(wav->peak_output + wav->peak_outbuf_bytes,
     237             :                         wav->peak_maxneg[c]);
     238         346 :                 wav->peak_outbuf_bytes += 2;
     239             :             }
     240             :         }
     241         346 :         wav->peak_maxpos[c] = 0;
     242         346 :         wav->peak_maxneg[c] = 0;
     243             :     }
     244         346 :     wav->peak_num_frames++;
     245             : }
     246             : 
     247           2 : static int peak_write_chunk(AVFormatContext *s)
     248             : {
     249           2 :     WAVMuxContext *wav = s->priv_data;
     250           2 :     AVIOContext *pb = s->pb;
     251           2 :     AVCodecParameters *par = s->streams[0]->codecpar;
     252           2 :     int64_t peak = ff_start_tag(s->pb, "levl");
     253             :     int64_t now0;
     254             :     time_t now_secs;
     255             :     char timestamp[28];
     256             : 
     257             :     /* Peak frame of incomplete block at end */
     258           2 :     if (wav->peak_block_pos)
     259           2 :         peak_write_frame(s);
     260             : 
     261           2 :     memset(timestamp, 0, sizeof(timestamp));
     262           2 :     if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
     263             :         struct tm tmpbuf;
     264           0 :         av_log(s, AV_LOG_INFO, "Writing local time and date to Peak Envelope Chunk\n");
     265           0 :         now0 = av_gettime();
     266           0 :         now_secs = now0 / 1000000;
     267           0 :         if (strftime(timestamp, sizeof(timestamp), "%Y:%m:%d:%H:%M:%S:", localtime_r(&now_secs, &tmpbuf))) {
     268           0 :             av_strlcatf(timestamp, sizeof(timestamp), "%03d", (int)((now0 / 1000) % 1000));
     269             :         } else {
     270           0 :             av_log(s, AV_LOG_ERROR, "Failed to write timestamp\n");
     271           0 :             return -1;
     272             :         }
     273             :     }
     274             : 
     275           2 :     avio_wl32(pb, 1);                           /* version */
     276           2 :     avio_wl32(pb, wav->peak_format);            /* 8 or 16 bit */
     277           2 :     avio_wl32(pb, wav->peak_ppv);               /* positive and negative */
     278           2 :     avio_wl32(pb, wav->peak_block_size);        /* frames per value */
     279           2 :     avio_wl32(pb, par->channels);               /* number of channels */
     280           2 :     avio_wl32(pb, wav->peak_num_frames);        /* number of peak frames */
     281           2 :     avio_wl32(pb, -1);                          /* audio sample frame position (not implemented) */
     282           2 :     avio_wl32(pb, 128);                         /* equal to size of header */
     283           2 :     avio_write(pb, timestamp, 28);              /* ASCII time stamp */
     284           2 :     ffio_fill(pb, 0, 60);
     285             : 
     286           2 :     avio_write(pb, wav->peak_output, wav->peak_outbuf_bytes);
     287             : 
     288           2 :     ff_end_tag(pb, peak);
     289             : 
     290           2 :     if (!wav->data)
     291           1 :         wav->data = peak;
     292             : 
     293           2 :     return 0;
     294             : }
     295             : 
     296         478 : static int wav_write_header(AVFormatContext *s)
     297             : {
     298         478 :     WAVMuxContext *wav = s->priv_data;
     299         478 :     AVIOContext *pb = s->pb;
     300             :     int64_t fmt;
     301             : 
     302         478 :     if (s->nb_streams != 1) {
     303           0 :         av_log(s, AV_LOG_ERROR, "WAVE files have exactly one stream\n");
     304           0 :         return AVERROR(EINVAL);
     305             :     }
     306             : 
     307         478 :     if (wav->rf64 == RF64_ALWAYS) {
     308           0 :         ffio_wfourcc(pb, "RF64");
     309           0 :         avio_wl32(pb, -1); /* RF64 chunk size: use size in ds64 */
     310             :     } else {
     311         478 :         ffio_wfourcc(pb, "RIFF");
     312         478 :         avio_wl32(pb, -1); /* file length */
     313             :     }
     314             : 
     315         478 :     ffio_wfourcc(pb, "WAVE");
     316             : 
     317         478 :     if (wav->rf64 != RF64_NEVER) {
     318             :         /* write empty ds64 chunk or JUNK chunk to reserve space for ds64 */
     319           0 :         ffio_wfourcc(pb, wav->rf64 == RF64_ALWAYS ? "ds64" : "JUNK");
     320           0 :         avio_wl32(pb, 28); /* chunk size */
     321           0 :         wav->ds64 = avio_tell(pb);
     322           0 :         ffio_fill(pb, 0, 28);
     323             :     }
     324             : 
     325         478 :     if (wav->write_peak != PEAK_ONLY) {
     326             :         /* format header */
     327         477 :         fmt = ff_start_tag(pb, "fmt ");
     328         477 :         if (ff_put_wav_header(s, pb, s->streams[0]->codecpar, 0) < 0) {
     329           0 :             const AVCodecDescriptor *desc = avcodec_descriptor_get(s->streams[0]->codecpar->codec_id);
     330           0 :             av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
     331             :                    desc ? desc->name : "unknown");
     332           0 :             return AVERROR(ENOSYS);
     333             :         }
     334         477 :         ff_end_tag(pb, fmt);
     335             :     }
     336             : 
     337         478 :     if (s->streams[0]->codecpar->codec_tag != 0x01 /* hence for all other than PCM */
     338          15 :         && (s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
     339          15 :         wav->fact_pos = ff_start_tag(pb, "fact");
     340          15 :         avio_wl32(pb, 0);
     341          15 :         ff_end_tag(pb, wav->fact_pos);
     342             :     }
     343             : 
     344         478 :     if (wav->write_bext)
     345           0 :         bwf_write_bext_chunk(s);
     346             : 
     347         478 :     if (wav->write_peak) {
     348             :         int ret;
     349           2 :         if ((ret = peak_init_writer(s)) < 0)
     350           0 :             return ret;
     351             :     }
     352             : 
     353         478 :     avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codecpar->sample_rate);
     354         478 :     wav->maxpts = wav->last_duration = 0;
     355         478 :     wav->minpts = INT64_MAX;
     356             : 
     357         478 :     if (wav->write_peak != PEAK_ONLY) {
     358             :         /* info header */
     359         477 :         ff_riff_write_info(s);
     360             : 
     361             :         /* data header */
     362         477 :         wav->data = ff_start_tag(pb, "data");
     363             :     }
     364             : 
     365         478 :     avio_flush(pb);
     366             : 
     367         478 :     return 0;
     368             : }
     369             : 
     370       57783 : static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
     371             : {
     372       57783 :     AVIOContext *pb  = s->pb;
     373       57783 :     WAVMuxContext    *wav = s->priv_data;
     374             : 
     375       57783 :     if (wav->write_peak != PEAK_ONLY)
     376       57739 :         avio_write(pb, pkt->data, pkt->size);
     377             : 
     378       57783 :     if (wav->write_peak) {
     379          88 :         int c = 0;
     380             :         int i;
     381       88288 :         for (i = 0; i < pkt->size; i += wav->peak_bps) {
     382       88200 :             if (wav->peak_bps == 1) {
     383           0 :                 wav->peak_maxpos[c] = FFMAX(wav->peak_maxpos[c], *(int8_t*)(pkt->data + i));
     384           0 :                 wav->peak_maxneg[c] = FFMIN(wav->peak_maxneg[c], *(int8_t*)(pkt->data + i));
     385             :             } else {
     386       88200 :                 wav->peak_maxpos[c] = FFMAX(wav->peak_maxpos[c], (int16_t)AV_RL16(pkt->data + i));
     387       88200 :                 wav->peak_maxneg[c] = FFMIN(wav->peak_maxneg[c], (int16_t)AV_RL16(pkt->data + i));
     388             :             }
     389       88200 :             if (++c == s->streams[0]->codecpar->channels) {
     390       88200 :                 c = 0;
     391       88200 :                 if (++wav->peak_block_pos == wav->peak_block_size) {
     392         344 :                     peak_write_frame(s);
     393         344 :                     wav->peak_block_pos = 0;
     394             :                 }
     395             :             }
     396             :         }
     397             :     }
     398             : 
     399       57783 :     if(pkt->pts != AV_NOPTS_VALUE) {
     400       57783 :         wav->minpts        = FFMIN(wav->minpts, pkt->pts);
     401       57783 :         wav->maxpts        = FFMAX(wav->maxpts, pkt->pts);
     402       57783 :         wav->last_duration = pkt->duration;
     403             :     } else
     404           0 :         av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
     405       57783 :     return 0;
     406             : }
     407             : 
     408         478 : static int wav_write_trailer(AVFormatContext *s)
     409             : {
     410         478 :     AVIOContext *pb  = s->pb;
     411         478 :     WAVMuxContext    *wav = s->priv_data;
     412             :     int64_t file_size, data_size;
     413         478 :     int64_t number_of_samples = 0;
     414         478 :     int rf64 = 0;
     415         478 :     int ret = 0;
     416             : 
     417         478 :     avio_flush(pb);
     418             : 
     419         478 :     if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
     420         122 :         if (wav->write_peak != PEAK_ONLY && avio_tell(pb) - wav->data < UINT32_MAX) {
     421         121 :             ff_end_tag(pb, wav->data);
     422         121 :             avio_flush(pb);
     423             :         }
     424             : 
     425         122 :         if (wav->write_peak && wav->peak_output) {
     426           2 :             ret = peak_write_chunk(s);
     427           2 :             avio_flush(pb);
     428             :         }
     429             : 
     430             :         /* update file size */
     431         122 :         file_size = avio_tell(pb);
     432         122 :         data_size = file_size - wav->data;
     433         122 :         if (wav->rf64 == RF64_ALWAYS || (wav->rf64 == RF64_AUTO && file_size - 8 > UINT32_MAX)) {
     434           0 :             rf64 = 1;
     435         122 :         } else if (file_size - 8 <= UINT32_MAX) {
     436         122 :             avio_seek(pb, 4, SEEK_SET);
     437         122 :             avio_wl32(pb, (uint32_t)(file_size - 8));
     438         122 :             avio_seek(pb, file_size, SEEK_SET);
     439             : 
     440         122 :             avio_flush(pb);
     441             :         } else {
     442           0 :             av_log(s, AV_LOG_ERROR,
     443             :                    "Filesize %"PRId64" invalid for wav, output file will be broken\n",
     444             :                    file_size);
     445             :         }
     446             : 
     447         244 :         number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
     448         122 :                                        s->streams[0]->codecpar->sample_rate * (int64_t)s->streams[0]->time_base.num,
     449         122 :                                        s->streams[0]->time_base.den);
     450             : 
     451         122 :         if(s->streams[0]->codecpar->codec_tag != 0x01) {
     452             :             /* Update num_samps in fact chunk */
     453          15 :             avio_seek(pb, wav->fact_pos, SEEK_SET);
     454          15 :             if (rf64 || (wav->rf64 == RF64_AUTO && number_of_samples > UINT32_MAX)) {
     455           0 :                 rf64 = 1;
     456           0 :                 avio_wl32(pb, -1);
     457             :             } else {
     458          15 :                 avio_wl32(pb, number_of_samples);
     459          15 :                 avio_seek(pb, file_size, SEEK_SET);
     460          15 :                 avio_flush(pb);
     461             :             }
     462             :         }
     463             : 
     464         122 :         if (rf64) {
     465             :             /* overwrite RIFF with RF64 */
     466           0 :             avio_seek(pb, 0, SEEK_SET);
     467           0 :             ffio_wfourcc(pb, "RF64");
     468           0 :             avio_wl32(pb, -1);
     469             : 
     470             :             /* write ds64 chunk (overwrite JUNK if rf64 == RF64_AUTO) */
     471           0 :             avio_seek(pb, wav->ds64 - 8, SEEK_SET);
     472           0 :             ffio_wfourcc(pb, "ds64");
     473           0 :             avio_wl32(pb, 28);                  /* ds64 chunk size */
     474           0 :             avio_wl64(pb, file_size - 8);       /* RF64 chunk size */
     475           0 :             avio_wl64(pb, data_size);           /* data chunk size */
     476           0 :             avio_wl64(pb, number_of_samples);   /* fact chunk number of samples */
     477           0 :             avio_wl32(pb, 0);                   /* number of table entries for non-'data' chunks */
     478             : 
     479             :             /* write -1 in data chunk size */
     480           0 :             avio_seek(pb, wav->data - 4, SEEK_SET);
     481           0 :             avio_wl32(pb, -1);
     482             : 
     483           0 :             avio_seek(pb, file_size, SEEK_SET);
     484           0 :             avio_flush(pb);
     485             :         }
     486             :     }
     487             : 
     488         478 :     if (wav->write_peak)
     489           2 :         peak_free_buffers(s);
     490             : 
     491         478 :     return ret;
     492             : }
     493             : 
     494             : #define OFFSET(x) offsetof(WAVMuxContext, x)
     495             : #define ENC AV_OPT_FLAG_ENCODING_PARAM
     496             : static const AVOption options[] = {
     497             :     { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
     498             :     { "write_peak", "Write Peak Envelope chunk.",            OFFSET(write_peak), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, ENC, "peak" },
     499             :     { "off",        "Do not write peak chunk.",              0,                  AV_OPT_TYPE_CONST, { .i64 = PEAK_OFF  }, 0, 0, ENC, "peak" },
     500             :     { "on",         "Append peak chunk after wav data.",     0,                  AV_OPT_TYPE_CONST, { .i64 = PEAK_ON   }, 0, 0, ENC, "peak" },
     501             :     { "only",       "Write only peak chunk, omit wav data.", 0,                  AV_OPT_TYPE_CONST, { .i64 = PEAK_ONLY }, 0, 0, ENC, "peak" },
     502             :     { "rf64",       "Use RF64 header rather than RIFF for large files.",    OFFSET(rf64), AV_OPT_TYPE_INT,   { .i64 = RF64_NEVER  },-1, 1, ENC, "rf64" },
     503             :     { "auto",       "Write RF64 header if file grows large enough.",        0,            AV_OPT_TYPE_CONST, { .i64 = RF64_AUTO   }, 0, 0, ENC, "rf64" },
     504             :     { "always",     "Always write RF64 header regardless of file size.",    0,            AV_OPT_TYPE_CONST, { .i64 = RF64_ALWAYS }, 0, 0, ENC, "rf64" },
     505             :     { "never",      "Never write RF64 header regardless of file size.",     0,            AV_OPT_TYPE_CONST, { .i64 = RF64_NEVER  }, 0, 0, ENC, "rf64" },
     506             :     { "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 },
     507             :     { "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 },
     508             :     { "peak_ppv",        "Number of peak points per peak value (1 or 2).",              OFFSET(peak_ppv), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, 2, ENC },
     509             :     { NULL },
     510             : };
     511             : 
     512             : static const AVClass wav_muxer_class = {
     513             :     .class_name = "WAV muxer",
     514             :     .item_name  = av_default_item_name,
     515             :     .option     = options,
     516             :     .version    = LIBAVUTIL_VERSION_INT,
     517             : };
     518             : 
     519             : AVOutputFormat ff_wav_muxer = {
     520             :     .name              = "wav",
     521             :     .long_name         = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
     522             :     .mime_type         = "audio/x-wav",
     523             :     .extensions        = "wav",
     524             :     .priv_data_size    = sizeof(WAVMuxContext),
     525             :     .audio_codec       = AV_CODEC_ID_PCM_S16LE,
     526             :     .video_codec       = AV_CODEC_ID_NONE,
     527             :     .write_header      = wav_write_header,
     528             :     .write_packet      = wav_write_packet,
     529             :     .write_trailer     = wav_write_trailer,
     530             :     .flags             = AVFMT_TS_NONSTRICT,
     531             :     .codec_tag         = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
     532             :     .priv_class        = &wav_muxer_class,
     533             : };
     534             : #endif /* CONFIG_WAV_MUXER */
     535             : 
     536             : #if CONFIG_W64_MUXER
     537             : #include "w64.h"
     538             : 
     539           2 : static void start_guid(AVIOContext *pb, const uint8_t *guid, int64_t *pos)
     540             : {
     541           2 :     *pos = avio_tell(pb);
     542             : 
     543           2 :     avio_write(pb, guid, 16);
     544           2 :     avio_wl64(pb, INT64_MAX);
     545           2 : }
     546             : 
     547           2 : static void end_guid(AVIOContext *pb, int64_t start)
     548             : {
     549           2 :     int64_t end, pos = avio_tell(pb);
     550             : 
     551           2 :     end = FFALIGN(pos, 8);
     552           2 :     ffio_fill(pb, 0, end - pos);
     553           2 :     avio_seek(pb, start + 16, SEEK_SET);
     554           2 :     avio_wl64(pb, end - start);
     555           2 :     avio_seek(pb, end, SEEK_SET);
     556           2 : }
     557             : 
     558           1 : static int w64_write_header(AVFormatContext *s)
     559             : {
     560           1 :     WAVMuxContext *wav = s->priv_data;
     561           1 :     AVIOContext *pb = s->pb;
     562             :     int64_t start;
     563             :     int ret;
     564             : 
     565           1 :     avio_write(pb, ff_w64_guid_riff, sizeof(ff_w64_guid_riff));
     566           1 :     avio_wl64(pb, -1);
     567           1 :     avio_write(pb, ff_w64_guid_wave, sizeof(ff_w64_guid_wave));
     568           1 :     start_guid(pb, ff_w64_guid_fmt, &start);
     569           1 :     if ((ret = ff_put_wav_header(s, pb, s->streams[0]->codecpar, 0)) < 0) {
     570           0 :         AVCodec *codec = avcodec_find_decoder(s->streams[0]->codecpar->codec_id);
     571           0 :         av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
     572             :                codec ? codec->name : "NONE");
     573           0 :         return ret;
     574             :     }
     575           1 :     end_guid(pb, start);
     576             : 
     577           1 :     if (s->streams[0]->codecpar->codec_tag != 0x01 /* hence for all other than PCM */
     578           0 :         && (s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
     579           0 :         start_guid(pb, ff_w64_guid_fact, &wav->fact_pos);
     580           0 :         avio_wl64(pb, 0);
     581           0 :         end_guid(pb, wav->fact_pos);
     582             :     }
     583             : 
     584           1 :     start_guid(pb, ff_w64_guid_data, &wav->data);
     585             : 
     586           1 :     return 0;
     587             : }
     588             : 
     589           1 : static int w64_write_trailer(AVFormatContext *s)
     590             : {
     591           1 :     AVIOContext    *pb = s->pb;
     592           1 :     WAVMuxContext *wav = s->priv_data;
     593             :     int64_t file_size;
     594             : 
     595           1 :     if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
     596           1 :         end_guid(pb, wav->data);
     597             : 
     598           1 :         file_size = avio_tell(pb);
     599           1 :         avio_seek(pb, 16, SEEK_SET);
     600           1 :         avio_wl64(pb, file_size);
     601             : 
     602           1 :         if (s->streams[0]->codecpar->codec_tag != 0x01) {
     603             :             int64_t number_of_samples;
     604             : 
     605           0 :             number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
     606           0 :                                            s->streams[0]->codecpar->sample_rate * (int64_t)s->streams[0]->time_base.num,
     607           0 :                                            s->streams[0]->time_base.den);
     608           0 :             avio_seek(pb, wav->fact_pos + 24, SEEK_SET);
     609           0 :             avio_wl64(pb, number_of_samples);
     610             :         }
     611             : 
     612           1 :         avio_seek(pb, file_size, SEEK_SET);
     613           1 :         avio_flush(pb);
     614             :     }
     615             : 
     616           1 :     return 0;
     617             : }
     618             : 
     619             : AVOutputFormat ff_w64_muxer = {
     620             :     .name              = "w64",
     621             :     .long_name         = NULL_IF_CONFIG_SMALL("Sony Wave64"),
     622             :     .extensions        = "w64",
     623             :     .priv_data_size    = sizeof(WAVMuxContext),
     624             :     .audio_codec       = AV_CODEC_ID_PCM_S16LE,
     625             :     .video_codec       = AV_CODEC_ID_NONE,
     626             :     .write_header      = w64_write_header,
     627             :     .write_packet      = wav_write_packet,
     628             :     .write_trailer     = w64_write_trailer,
     629             :     .flags             = AVFMT_TS_NONSTRICT,
     630             :     .codec_tag         = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
     631             : };
     632             : #endif /* CONFIG_W64_MUXER */

Generated by: LCOV version 1.13