2012-10-16 11:23:29 +03:00
/*
* WAV muxer
* Copyright ( c ) 2001 , 2002 Fabrice Bellard
*
2012-12-28 16:40:55 +03:00
* Sony Wave64 muxer
* Copyright ( c ) 2012 Paul B Mahol
*
2013-01-23 11:30:23 +03:00
* WAV muxer RF64 support
* Copyright ( c ) 2013 Daniel Verkamp < daniel @ drv . nu >
*
2014-06-21 20:20:39 +03:00
* EBU Tech 3285 - Supplement 3 - Peak Envelope Chunk encoder
* Copyright ( c ) 2014 Georg Lippitsch < georg . lippitsch @ gmx . at >
*
2012-10-17 16:36:36 +03:00
* This file is part of FFmpeg .
2012-10-16 11:23:29 +03:00
*
2012-10-17 16:36:36 +03:00
* FFmpeg is free software ; you can redistribute it and / or
2012-10-16 11:23:29 +03:00
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2.1 of the License , or ( at your option ) any later version .
*
2012-10-17 16:36:36 +03:00
* FFmpeg is distributed in the hope that it will be useful ,
2012-10-16 11:23:29 +03:00
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
2012-10-17 16:36:36 +03:00
* License along with FFmpeg ; if not , write to the Free Software
2012-10-16 11:23:29 +03:00
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
*/
2022-02-23 14:56:49 +02:00
# include "config_components.h"
2012-10-16 11:23:29 +03:00
# include <stdint.h>
# include <string.h>
2023-09-02 12:11:59 +02:00
# include <time.h>
2012-10-16 11:23:29 +03:00
2014-06-21 20:20:39 +03:00
# include "libavutil/avstring.h"
2012-10-16 11:23:29 +03:00
# include "libavutil/dict.h"
# include "libavutil/common.h"
2014-06-21 20:20:39 +03:00
# include "libavutil/intreadwrite.h"
2012-10-16 11:23:29 +03:00
# include "libavutil/mathematics.h"
# include "libavutil/opt.h"
2014-06-21 20:20:39 +03:00
# include "libavutil/time.h"
2014-11-02 21:08:12 +02:00
# include "libavutil/time_internal.h"
2012-10-16 11:23:29 +03:00
# include "avformat.h"
# include "avio.h"
# include "avio_internal.h"
# include "internal.h"
2023-01-27 16:06:00 +02:00
# include "mux.h"
2012-10-16 11:23:29 +03:00
# include "riff.h"
2013-01-23 11:30:23 +03:00
# define RF64_AUTO (-1)
# define RF64_NEVER 0
# define RF64_ALWAYS 1
2014-06-21 20:20:39 +03:00
typedef enum {
PEAK_OFF = 0 ,
PEAK_ON ,
PEAK_ONLY
} PeakType ;
typedef enum {
PEAK_FORMAT_UINT8 = 1 ,
PEAK_FORMAT_UINT16
} PeakFormat ;
2012-10-16 11:23:29 +03:00
typedef struct WAVMuxContext {
const AVClass * class ;
int64_t data ;
2012-10-30 23:48:39 +03:00
int64_t fact_pos ;
2013-01-23 11:30:23 +03:00
int64_t ds64 ;
2012-10-16 11:23:29 +03:00
int64_t minpts ;
int64_t maxpts ;
2014-06-21 20:20:39 +03:00
int16_t * peak_maxpos , * peak_maxneg ;
uint32_t peak_num_frames ;
2021-02-22 09:31:50 +02:00
unsigned peak_outbuf_size ;
2014-06-21 20:20:39 +03:00
uint32_t peak_outbuf_bytes ;
2021-02-22 09:31:50 +02:00
unsigned size_increment ;
2014-06-21 20:20:39 +03:00
uint8_t * peak_output ;
2012-10-16 11:23:29 +03:00
int last_duration ;
int write_bext ;
2014-06-21 20:20:39 +03:00
int write_peak ;
2013-01-23 11:30:23 +03:00
int rf64 ;
2014-06-21 20:20:39 +03:00
int peak_block_size ;
2015-03-23 20:31:17 +02:00
int peak_format ;
2014-06-21 20:20:39 +03:00
int peak_block_pos ;
int peak_ppv ;
int peak_bps ;
2012-10-16 11:23:29 +03:00
} WAVMuxContext ;
2012-12-28 16:40:55 +03:00
# if CONFIG_WAV_MUXER
2012-10-16 11:23:29 +03:00
static inline void bwf_write_bext_string ( AVFormatContext * s , const char * key , int maxlen )
{
AVDictionaryEntry * tag ;
2015-05-10 19:52:30 +02:00
size_t len = 0 ;
2012-10-16 11:23:29 +03:00
if ( tag = av_dict_get ( s - > metadata , key , NULL , 0 ) ) {
len = strlen ( tag - > value ) ;
len = FFMIN ( len , maxlen ) ;
avio_write ( s - > pb , tag - > value , len ) ;
}
ffio_fill ( s - > pb , 0 , maxlen - len ) ;
}
static void bwf_write_bext_chunk ( AVFormatContext * s )
{
AVDictionaryEntry * tmp_tag ;
uint64_t time_reference = 0 ;
int64_t bext = ff_start_tag ( s - > pb , " bext " ) ;
bwf_write_bext_string ( s , " description " , 256 ) ;
bwf_write_bext_string ( s , " originator " , 32 ) ;
bwf_write_bext_string ( s , " originator_reference " , 32 ) ;
bwf_write_bext_string ( s , " origination_date " , 10 ) ;
bwf_write_bext_string ( s , " origination_time " , 8 ) ;
if ( tmp_tag = av_dict_get ( s - > metadata , " time_reference " , NULL , 0 ) )
time_reference = strtoll ( tmp_tag - > value , NULL , 10 ) ;
avio_wl64 ( s - > pb , time_reference ) ;
avio_wl16 ( s - > pb , 1 ) ; // set version to 1
2015-05-10 19:54:41 +02:00
if ( ( tmp_tag = av_dict_get ( s - > metadata , " umid " , NULL , 0 ) ) & & strlen ( tmp_tag - > value ) > 2 ) {
2012-10-16 11:23:29 +03:00
unsigned char umidpart_str [ 17 ] = { 0 } ;
2015-05-10 19:52:30 +02:00
int64_t i ;
2012-10-16 11:23:29 +03:00
uint64_t umidpart ;
2015-05-10 19:52:30 +02:00
size_t len = strlen ( tmp_tag - > value + 2 ) ;
2012-10-16 11:23:29 +03:00
for ( i = 0 ; i < len / 16 ; i + + ) {
memcpy ( umidpart_str , tmp_tag - > value + 2 + ( i * 16 ) , 16 ) ;
2024-01-29 06:56:06 +02:00
umidpart = strtoull ( umidpart_str , NULL , 16 ) ;
2012-10-16 11:23:29 +03:00
avio_wb64 ( s - > pb , umidpart ) ;
}
ffio_fill ( s - > pb , 0 , 64 - i * 8 ) ;
} else
ffio_fill ( s - > pb , 0 , 64 ) ; // zero UMID
ffio_fill ( s - > pb , 0 , 190 ) ; // Reserved
if ( tmp_tag = av_dict_get ( s - > metadata , " coding_history " , NULL , 0 ) )
avio_put_str ( s - > pb , tmp_tag - > value ) ;
ff_end_tag ( s - > pb , bext ) ;
}
2019-12-26 12:53:27 +02:00
static av_cold void wav_deinit ( AVFormatContext * s )
2014-06-21 20:20:39 +03:00
{
WAVMuxContext * wav = s - > priv_data ;
av_freep ( & wav - > peak_maxpos ) ;
av_freep ( & wav - > peak_maxneg ) ;
av_freep ( & wav - > peak_output ) ;
}
static av_cold int peak_init_writer ( AVFormatContext * s )
{
WAVMuxContext * wav = s - > priv_data ;
2016-04-10 21:58:15 +02:00
AVCodecParameters * par = s - > streams [ 0 ] - > codecpar ;
2014-06-21 20:20:39 +03:00
2016-04-10 21:58:15 +02:00
if ( par - > codec_id ! = AV_CODEC_ID_PCM_S8 & &
par - > codec_id ! = AV_CODEC_ID_PCM_S16LE & &
par - > codec_id ! = AV_CODEC_ID_PCM_U8 & &
par - > codec_id ! = AV_CODEC_ID_PCM_U16LE ) {
2021-02-25 08:37:54 +02:00
av_log ( s , AV_LOG_ERROR , " Codec %s not supported for Peak Chunk \n " ,
avcodec_get_name ( par - > codec_id ) ) ;
2014-06-21 20:20:39 +03:00
return - 1 ;
}
2016-04-10 21:58:15 +02:00
wav - > peak_bps = av_get_bits_per_sample ( par - > codec_id ) / 8 ;
2014-06-21 20:20:39 +03:00
if ( wav - > peak_bps = = 1 & & wav - > peak_format = = PEAK_FORMAT_UINT16 ) {
av_log ( s , AV_LOG_ERROR ,
" Writing 16 bit peak for 8 bit audio does not make sense \n " ) ;
2014-06-24 14:50:38 +03:00
return AVERROR ( EINVAL ) ;
2014-06-21 20:20:39 +03:00
}
2017-03-31 18:52:03 +02:00
if ( par - > ch_layout . nb_channels > INT_MAX / ( wav - > peak_bps * wav - > peak_ppv ) )
2021-02-22 09:31:50 +02:00
return AVERROR ( ERANGE ) ;
2017-03-31 18:52:03 +02:00
wav - > size_increment = par - > ch_layout . nb_channels * wav - > peak_bps * wav - > peak_ppv ;
2014-06-21 20:20:39 +03:00
2017-03-31 18:52:03 +02:00
wav - > peak_maxpos = av_calloc ( par - > ch_layout . nb_channels , sizeof ( * wav - > peak_maxpos ) ) ;
wav - > peak_maxneg = av_calloc ( par - > ch_layout . nb_channels , sizeof ( * wav - > peak_maxneg ) ) ;
2021-02-22 09:31:50 +02:00
if ( ! wav - > peak_maxpos | | ! wav - > peak_maxneg )
2014-06-21 20:20:39 +03:00
goto nomem ;
return 0 ;
nomem :
av_log ( s , AV_LOG_ERROR , " Out of memory \n " ) ;
return AVERROR ( ENOMEM ) ;
}
2021-02-22 09:31:50 +02:00
static int peak_write_frame ( AVFormatContext * s )
2014-06-21 20:20:39 +03:00
{
WAVMuxContext * wav = s - > priv_data ;
2016-04-10 21:58:15 +02:00
AVCodecParameters * par = s - > streams [ 0 ] - > codecpar ;
2021-02-22 09:31:50 +02:00
unsigned new_size = wav - > peak_outbuf_bytes + wav - > size_increment ;
uint8_t * tmp ;
2014-06-21 20:20:39 +03:00
int c ;
2021-02-22 09:31:50 +02:00
if ( new_size > INT_MAX ) {
wav - > write_peak = PEAK_OFF ;
return AVERROR ( ERANGE ) ;
}
tmp = av_fast_realloc ( wav - > peak_output , & wav - > peak_outbuf_size , new_size ) ;
if ( ! tmp ) {
wav - > write_peak = PEAK_OFF ;
return AVERROR ( ENOMEM ) ;
}
wav - > peak_output = tmp ;
2014-06-21 20:20:39 +03:00
2017-03-31 18:52:03 +02:00
for ( c = 0 ; c < par - > ch_layout . nb_channels ; c + + ) {
2014-06-21 20:20:39 +03:00
wav - > peak_maxneg [ c ] = - wav - > peak_maxneg [ c ] ;
if ( wav - > peak_bps = = 2 & & wav - > peak_format = = PEAK_FORMAT_UINT8 ) {
wav - > peak_maxpos [ c ] = wav - > peak_maxpos [ c ] / 256 ;
wav - > peak_maxneg [ c ] = wav - > peak_maxneg [ c ] / 256 ;
}
if ( wav - > peak_ppv = = 1 )
wav - > peak_maxpos [ c ] =
FFMAX ( wav - > peak_maxpos [ c ] , wav - > peak_maxneg [ c ] ) ;
if ( wav - > peak_format = = PEAK_FORMAT_UINT8 ) {
wav - > peak_output [ wav - > peak_outbuf_bytes + + ] =
wav - > peak_maxpos [ c ] ;
if ( wav - > peak_ppv = = 2 ) {
wav - > peak_output [ wav - > peak_outbuf_bytes + + ] =
wav - > peak_maxneg [ c ] ;
}
} else {
AV_WL16 ( wav - > peak_output + wav - > peak_outbuf_bytes ,
wav - > peak_maxpos [ c ] ) ;
wav - > peak_outbuf_bytes + = 2 ;
if ( wav - > peak_ppv = = 2 ) {
AV_WL16 ( wav - > peak_output + wav - > peak_outbuf_bytes ,
wav - > peak_maxneg [ c ] ) ;
wav - > peak_outbuf_bytes + = 2 ;
}
}
wav - > peak_maxpos [ c ] = 0 ;
wav - > peak_maxneg [ c ] = 0 ;
}
wav - > peak_num_frames + + ;
2021-02-22 09:31:50 +02:00
return 0 ;
2014-06-21 20:20:39 +03:00
}
2014-12-09 17:10:55 +02:00
static int peak_write_chunk ( AVFormatContext * s )
2014-06-21 20:20:39 +03:00
{
WAVMuxContext * wav = s - > priv_data ;
AVIOContext * pb = s - > pb ;
2016-04-10 21:58:15 +02:00
AVCodecParameters * par = s - > streams [ 0 ] - > codecpar ;
2014-06-21 20:20:39 +03:00
int64_t peak = ff_start_tag ( s - > pb , " levl " ) ;
int64_t now0 ;
time_t now_secs ;
char timestamp [ 28 ] ;
/* Peak frame of incomplete block at end */
2021-02-22 09:31:50 +02:00
if ( wav - > peak_block_pos ) {
int ret = peak_write_frame ( s ) ;
if ( ret < 0 )
return ret ;
}
2014-06-21 20:20:39 +03:00
2014-06-24 14:37:31 +03:00
memset ( timestamp , 0 , sizeof ( timestamp ) ) ;
2014-06-24 14:32:27 +03:00
if ( ! ( s - > flags & AVFMT_FLAG_BITEXACT ) ) {
2014-11-02 21:08:12 +02:00
struct tm tmpbuf ;
2014-06-21 20:20:39 +03:00
av_log ( s , AV_LOG_INFO , " Writing local time and date to Peak Envelope Chunk \n " ) ;
now0 = av_gettime ( ) ;
now_secs = now0 / 1000000 ;
2014-12-09 17:10:55 +02:00
if ( strftime ( timestamp , sizeof ( timestamp ) , " %Y:%m:%d:%H:%M:%S: " , localtime_r ( & now_secs , & tmpbuf ) ) ) {
av_strlcatf ( timestamp , sizeof ( timestamp ) , " %03d " , ( int ) ( ( now0 / 1000 ) % 1000 ) ) ;
} else {
av_log ( s , AV_LOG_ERROR , " Failed to write timestamp \n " ) ;
return - 1 ;
}
2014-06-21 20:20:39 +03:00
}
avio_wl32 ( pb , 1 ) ; /* version */
avio_wl32 ( pb , wav - > peak_format ) ; /* 8 or 16 bit */
avio_wl32 ( pb , wav - > peak_ppv ) ; /* positive and negative */
avio_wl32 ( pb , wav - > peak_block_size ) ; /* frames per value */
2017-03-31 18:52:03 +02:00
avio_wl32 ( pb , par - > ch_layout . nb_channels ) ; /* number of channels */
2014-06-21 20:20:39 +03:00
avio_wl32 ( pb , wav - > peak_num_frames ) ; /* number of peak frames */
2017-09-29 16:32:20 +02:00
avio_wl32 ( pb , - 1 ) ; /* audio sample frame position (not implemented) */
2014-06-21 20:20:39 +03:00
avio_wl32 ( pb , 128 ) ; /* equal to size of header */
avio_write ( pb , timestamp , 28 ) ; /* ASCII time stamp */
ffio_fill ( pb , 0 , 60 ) ;
avio_write ( pb , wav - > peak_output , wav - > peak_outbuf_bytes ) ;
ff_end_tag ( pb , peak ) ;
if ( ! wav - > data )
wav - > data = peak ;
2014-12-09 17:10:55 +02:00
return 0 ;
2014-06-21 20:20:39 +03:00
}
2012-10-16 11:23:29 +03:00
static int wav_write_header ( AVFormatContext * s )
{
WAVMuxContext * wav = s - > priv_data ;
AVIOContext * pb = s - > pb ;
2012-10-30 23:48:39 +03:00
int64_t fmt ;
2012-10-16 11:23:29 +03:00
2013-11-02 18:23:26 +03:00
if ( s - > nb_streams ! = 1 ) {
av_log ( s , AV_LOG_ERROR , " WAVE files have exactly one stream \n " ) ;
return AVERROR ( EINVAL ) ;
}
2013-01-23 11:30:23 +03:00
if ( wav - > rf64 = = RF64_ALWAYS ) {
ffio_wfourcc ( pb , " RF64 " ) ;
avio_wl32 ( pb , - 1 ) ; /* RF64 chunk size: use size in ds64 */
} else {
2013-02-12 19:36:01 +03:00
ffio_wfourcc ( pb , " RIFF " ) ;
2014-05-03 21:18:13 +03:00
avio_wl32 ( pb , - 1 ) ; /* file length */
2013-01-23 11:30:23 +03:00
}
2012-10-16 11:23:29 +03:00
ffio_wfourcc ( pb , " WAVE " ) ;
2013-01-23 11:30:23 +03:00
if ( wav - > rf64 ! = RF64_NEVER ) {
/* write empty ds64 chunk or JUNK chunk to reserve space for ds64 */
ffio_wfourcc ( pb , wav - > rf64 = = RF64_ALWAYS ? " ds64 " : " JUNK " ) ;
avio_wl32 ( pb , 28 ) ; /* chunk size */
wav - > ds64 = avio_tell ( pb ) ;
ffio_fill ( pb , 0 , 28 ) ;
}
2017-09-29 16:28:47 +02:00
if ( wav - > write_peak ! = PEAK_ONLY ) {
2014-06-21 20:20:40 +03:00
/* format header */
fmt = ff_start_tag ( pb , " fmt " ) ;
2016-04-10 21:58:15 +02:00
if ( ff_put_wav_header ( s , pb , s - > streams [ 0 ] - > codecpar , 0 ) < 0 ) {
2021-02-25 08:37:54 +02:00
av_log ( s , AV_LOG_ERROR , " Codec %s not supported in WAVE format \n " ,
avcodec_get_name ( s - > streams [ 0 ] - > codecpar - > codec_id ) ) ;
2014-06-21 20:20:40 +03:00
return AVERROR ( ENOSYS ) ;
}
ff_end_tag ( pb , fmt ) ;
2014-06-21 20:20:39 +03:00
}
2012-10-16 11:23:29 +03:00
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
if ( s - > streams [ 0 ] - > codecpar - > codec_tag ! = 0x01 /* hence for all other than PCM */
2016-09-27 16:26:37 +02:00
& & ( s - > pb - > seekable & AVIO_SEEKABLE_NORMAL ) ) {
2012-10-30 23:48:39 +03:00
wav - > fact_pos = ff_start_tag ( pb , " fact " ) ;
2012-10-16 11:23:29 +03:00
avio_wl32 ( pb , 0 ) ;
2012-10-30 23:48:39 +03:00
ff_end_tag ( pb , wav - > fact_pos ) ;
2012-10-16 11:23:29 +03:00
}
if ( wav - > write_bext )
bwf_write_bext_chunk ( s ) ;
2014-06-21 20:20:39 +03:00
if ( wav - > write_peak ) {
int ret ;
if ( ( ret = peak_init_writer ( s ) ) < 0 )
return ret ;
}
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
avpriv_set_pts_info ( s - > streams [ 0 ] , 64 , 1 , s - > streams [ 0 ] - > codecpar - > sample_rate ) ;
2012-10-16 11:23:29 +03:00
wav - > maxpts = wav - > last_duration = 0 ;
wav - > minpts = INT64_MAX ;
2017-09-29 16:28:47 +02:00
if ( wav - > write_peak ! = PEAK_ONLY ) {
2014-06-21 20:20:40 +03:00
/* info header */
ff_riff_write_info ( s ) ;
2012-10-16 11:23:29 +03:00
2014-06-21 20:20:40 +03:00
/* data header */
wav - > data = ff_start_tag ( pb , " data " ) ;
2014-06-21 20:20:39 +03:00
}
2012-10-16 11:23:29 +03:00
return 0 ;
}
static int wav_write_packet ( AVFormatContext * s , AVPacket * pkt )
{
AVIOContext * pb = s - > pb ;
WAVMuxContext * wav = s - > priv_data ;
2014-06-21 20:20:39 +03:00
2017-09-29 16:28:47 +02:00
if ( wav - > write_peak ! = PEAK_ONLY )
2014-06-21 20:20:40 +03:00
avio_write ( pb , pkt - > data , pkt - > size ) ;
2014-06-21 20:20:39 +03:00
if ( wav - > write_peak ) {
int c = 0 ;
int i ;
for ( i = 0 ; i < pkt - > size ; i + = wav - > peak_bps ) {
if ( wav - > peak_bps = = 1 ) {
wav - > peak_maxpos [ c ] = FFMAX ( wav - > peak_maxpos [ c ] , * ( int8_t * ) ( pkt - > data + i ) ) ;
wav - > peak_maxneg [ c ] = FFMIN ( wav - > peak_maxneg [ c ] , * ( int8_t * ) ( pkt - > data + i ) ) ;
} else {
wav - > peak_maxpos [ c ] = FFMAX ( wav - > peak_maxpos [ c ] , ( int16_t ) AV_RL16 ( pkt - > data + i ) ) ;
wav - > peak_maxneg [ c ] = FFMIN ( wav - > peak_maxneg [ c ] , ( int16_t ) AV_RL16 ( pkt - > data + i ) ) ;
}
2017-03-31 18:52:03 +02:00
if ( + + c = = s - > streams [ 0 ] - > codecpar - > ch_layout . nb_channels ) {
2014-06-21 20:20:39 +03:00
c = 0 ;
if ( + + wav - > peak_block_pos = = wav - > peak_block_size ) {
2021-02-22 09:31:50 +02:00
int ret = peak_write_frame ( s ) ;
if ( ret < 0 )
return ret ;
2014-06-21 20:20:39 +03:00
wav - > peak_block_pos = 0 ;
}
}
}
}
2012-10-16 11:23:29 +03:00
if ( pkt - > pts ! = AV_NOPTS_VALUE ) {
wav - > minpts = FFMIN ( wav - > minpts , pkt - > pts ) ;
wav - > maxpts = FFMAX ( wav - > maxpts , pkt - > pts ) ;
wav - > last_duration = pkt - > duration ;
} else
av_log ( s , AV_LOG_ERROR , " wav_write_packet: NOPTS \n " ) ;
return 0 ;
}
static int wav_write_trailer ( AVFormatContext * s )
{
AVIOContext * pb = s - > pb ;
WAVMuxContext * wav = s - > priv_data ;
2013-01-23 11:30:23 +03:00
int64_t file_size , data_size ;
int64_t number_of_samples = 0 ;
int rf64 = 0 ;
2014-12-09 17:10:55 +02:00
int ret = 0 ;
2012-10-16 11:23:29 +03:00
2016-09-27 16:26:37 +02:00
if ( s - > pb - > seekable & AVIO_SEEKABLE_NORMAL ) {
2017-09-29 16:28:47 +02:00
if ( wav - > write_peak ! = PEAK_ONLY & & avio_tell ( pb ) - wav - > data < UINT32_MAX ) {
2014-06-21 20:20:39 +03:00
ff_end_tag ( pb , wav - > data ) ;
}
if ( wav - > write_peak & & wav - > peak_output ) {
2014-12-09 17:10:55 +02:00
ret = peak_write_chunk ( s ) ;
2014-06-21 20:20:39 +03:00
}
2012-10-16 11:23:29 +03:00
/* update file size */
file_size = avio_tell ( pb ) ;
2013-01-23 11:30:23 +03:00
data_size = file_size - wav - > data ;
if ( wav - > rf64 = = RF64_ALWAYS | | ( wav - > rf64 = = RF64_AUTO & & file_size - 8 > UINT32_MAX ) ) {
rf64 = 1 ;
2015-05-16 23:32:56 +02:00
} else if ( file_size - 8 < = UINT32_MAX ) {
2013-02-12 19:36:01 +03:00
avio_seek ( pb , 4 , SEEK_SET ) ;
avio_wl32 ( pb , ( uint32_t ) ( file_size - 8 ) ) ;
avio_seek ( pb , file_size , SEEK_SET ) ;
2015-05-16 23:32:56 +02:00
} else {
av_log ( s , AV_LOG_ERROR ,
" Filesize % " PRId64 " invalid for wav, output file will be broken \n " ,
file_size ) ;
2013-01-23 11:30:23 +03:00
}
2020-04-28 05:49:17 +02:00
number_of_samples = av_rescale_q ( wav - > maxpts - wav - > minpts + wav - > last_duration ,
s - > streams [ 0 ] - > time_base ,
av_make_q ( 1 , s - > streams [ 0 ] - > codecpar - > sample_rate ) ) ;
2013-01-23 11:30:23 +03:00
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
if ( s - > streams [ 0 ] - > codecpar - > codec_tag ! = 0x01 ) {
2013-01-23 11:30:23 +03:00
/* Update num_samps in fact chunk */
2012-10-30 23:48:39 +03:00
avio_seek ( pb , wav - > fact_pos , SEEK_SET ) ;
2013-01-23 11:30:23 +03:00
if ( rf64 | | ( wav - > rf64 = = RF64_AUTO & & number_of_samples > UINT32_MAX ) ) {
rf64 = 1 ;
avio_wl32 ( pb , - 1 ) ;
} else {
2013-02-12 19:36:01 +03:00
avio_wl32 ( pb , number_of_samples ) ;
avio_seek ( pb , file_size , SEEK_SET ) ;
2013-01-23 11:30:23 +03:00
}
}
if ( rf64 ) {
/* overwrite RIFF with RF64 */
avio_seek ( pb , 0 , SEEK_SET ) ;
ffio_wfourcc ( pb , " RF64 " ) ;
avio_wl32 ( pb , - 1 ) ;
/* write ds64 chunk (overwrite JUNK if rf64 == RF64_AUTO) */
avio_seek ( pb , wav - > ds64 - 8 , SEEK_SET ) ;
ffio_wfourcc ( pb , " ds64 " ) ;
avio_wl32 ( pb , 28 ) ; /* ds64 chunk size */
avio_wl64 ( pb , file_size - 8 ) ; /* RF64 chunk size */
avio_wl64 ( pb , data_size ) ; /* data chunk size */
avio_wl64 ( pb , number_of_samples ) ; /* fact chunk number of samples */
avio_wl32 ( pb , 0 ) ; /* number of table entries for non-'data' chunks */
/* write -1 in data chunk size */
avio_seek ( pb , wav - > data - 4 , SEEK_SET ) ;
avio_wl32 ( pb , - 1 ) ;
avio_seek ( pb , file_size , SEEK_SET ) ;
2012-10-16 11:23:29 +03:00
}
}
2014-06-21 20:20:39 +03:00
2014-12-09 17:10:55 +02:00
return ret ;
2012-10-16 11:23:29 +03:00
}
# define OFFSET(x) offsetof(WAVMuxContext, x)
# define ENC AV_OPT_FLAG_ENCODING_PARAM
static const AVOption options [ ] = {
2015-09-11 17:34:17 +02:00
{ " write_bext " , " Write BEXT chunk. " , OFFSET ( write_bext ) , AV_OPT_TYPE_BOOL , { . i64 = 0 } , 0 , 1 , ENC } ,
2024-02-11 16:41:05 +02:00
{ " write_peak " , " Write Peak Envelope chunk. " , OFFSET ( write_peak ) , AV_OPT_TYPE_INT , { . i64 = 0 } , 0 , 2 , ENC , . unit = " peak " } ,
{ " off " , " Do not write peak chunk. " , 0 , AV_OPT_TYPE_CONST , { . i64 = PEAK_OFF } , 0 , 0 , ENC , . unit = " peak " } ,
{ " on " , " Append peak chunk after wav data. " , 0 , AV_OPT_TYPE_CONST , { . i64 = PEAK_ON } , 0 , 0 , ENC , . unit = " peak " } ,
{ " only " , " Write only peak chunk, omit wav data. " , 0 , AV_OPT_TYPE_CONST , { . i64 = PEAK_ONLY } , 0 , 0 , ENC , . unit = " peak " } ,
{ " rf64 " , " Use RF64 header rather than RIFF for large files. " , OFFSET ( rf64 ) , AV_OPT_TYPE_INT , { . i64 = RF64_NEVER } , - 1 , 1 , ENC , . unit = " rf64 " } ,
{ " auto " , " Write RF64 header if file grows large enough. " , 0 , AV_OPT_TYPE_CONST , { . i64 = RF64_AUTO } , 0 , 0 , ENC , . unit = " rf64 " } ,
{ " always " , " Always write RF64 header regardless of file size. " , 0 , AV_OPT_TYPE_CONST , { . i64 = RF64_ALWAYS } , 0 , 0 , ENC , . unit = " rf64 " } ,
{ " never " , " Never write RF64 header regardless of file size. " , 0 , AV_OPT_TYPE_CONST , { . i64 = RF64_NEVER } , 0 , 0 , ENC , . unit = " rf64 " } ,
2014-06-21 20:20:39 +03:00
{ " 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 } ,
{ " 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 } ,
{ " peak_ppv " , " Number of peak points per peak value (1 or 2). " , OFFSET ( peak_ppv ) , AV_OPT_TYPE_INT , { . i64 = 2 } , 1 , 2 , ENC } ,
2012-10-16 11:23:29 +03:00
{ NULL } ,
} ;
static const AVClass wav_muxer_class = {
. class_name = " WAV muxer " ,
2024-01-19 14:33:28 +02:00
. item_name = av_default_item_name ,
2012-10-16 11:23:29 +03:00
. option = options ,
. version = LIBAVUTIL_VERSION_INT ,
} ;
2023-01-27 16:06:00 +02:00
const FFOutputFormat ff_wav_muxer = {
. p . name = " wav " ,
. p . long_name = NULL_IF_CONFIG_SMALL ( " WAV / WAVE (Waveform Audio) " ) ,
. p . mime_type = " audio/x-wav " ,
. p . extensions = " wav " ,
2012-10-16 11:23:29 +03:00
. priv_data_size = sizeof ( WAVMuxContext ) ,
2023-01-27 16:06:00 +02:00
. p . audio_codec = AV_CODEC_ID_PCM_S16LE ,
. p . video_codec = AV_CODEC_ID_NONE ,
2012-10-16 11:23:29 +03:00
. write_header = wav_write_header ,
. write_packet = wav_write_packet ,
. write_trailer = wav_write_trailer ,
2019-12-26 12:53:27 +02:00
. deinit = wav_deinit ,
2023-01-27 16:06:00 +02:00
. p . flags = AVFMT_TS_NONSTRICT ,
. p . codec_tag = ff_wav_codec_tags_list ,
. p . priv_class = & wav_muxer_class ,
2012-10-16 11:23:29 +03:00
} ;
2012-12-28 16:40:55 +03:00
# endif /* CONFIG_WAV_MUXER */
# if CONFIG_W64_MUXER
# include "w64.h"
2022-11-25 11:13:52 +02:00
static av_cold int w64_init ( AVFormatContext * ctx )
{
if ( ctx - > nb_streams ! = 1 ) {
av_log ( ctx , AV_LOG_ERROR , " This muxer only supports a single stream. \n " ) ;
return AVERROR ( EINVAL ) ;
}
return 0 ;
}
2012-12-28 16:40:55 +03:00
static void start_guid ( AVIOContext * pb , const uint8_t * guid , int64_t * pos )
{
* pos = avio_tell ( pb ) ;
avio_write ( pb , guid , 16 ) ;
avio_wl64 ( pb , INT64_MAX ) ;
}
static void end_guid ( AVIOContext * pb , int64_t start )
{
int64_t end , pos = avio_tell ( pb ) ;
end = FFALIGN ( pos , 8 ) ;
ffio_fill ( pb , 0 , end - pos ) ;
avio_seek ( pb , start + 16 , SEEK_SET ) ;
avio_wl64 ( pb , end - start ) ;
avio_seek ( pb , end , SEEK_SET ) ;
}
static int w64_write_header ( AVFormatContext * s )
{
WAVMuxContext * wav = s - > priv_data ;
AVIOContext * pb = s - > pb ;
int64_t start ;
int ret ;
avio_write ( pb , ff_w64_guid_riff , sizeof ( ff_w64_guid_riff ) ) ;
avio_wl64 ( pb , - 1 ) ;
avio_write ( pb , ff_w64_guid_wave , sizeof ( ff_w64_guid_wave ) ) ;
start_guid ( pb , ff_w64_guid_fmt , & start ) ;
2016-04-10 21:58:15 +02:00
if ( ( ret = ff_put_wav_header ( s , pb , s - > streams [ 0 ] - > codecpar , 0 ) ) < 0 ) {
2021-02-25 08:37:54 +02:00
av_log ( s , AV_LOG_ERROR , " Codec %s not supported \n " ,
avcodec_get_name ( s - > streams [ 0 ] - > codecpar - > codec_id ) ) ;
2012-12-28 16:40:55 +03:00
return ret ;
}
end_guid ( pb , start ) ;
2013-01-05 18:02:08 +03:00
2016-04-10 21:58:15 +02:00
if ( s - > streams [ 0 ] - > codecpar - > codec_tag ! = 0x01 /* hence for all other than PCM */
2017-03-21 22:02:30 +02:00
& & ( s - > pb - > seekable & AVIO_SEEKABLE_NORMAL ) ) {
2013-01-05 18:02:08 +03:00
start_guid ( pb , ff_w64_guid_fact , & wav - > fact_pos ) ;
avio_wl64 ( pb , 0 ) ;
end_guid ( pb , wav - > fact_pos ) ;
}
2012-12-28 16:40:55 +03:00
start_guid ( pb , ff_w64_guid_data , & wav - > data ) ;
return 0 ;
}
static int w64_write_trailer ( AVFormatContext * s )
{
AVIOContext * pb = s - > pb ;
WAVMuxContext * wav = s - > priv_data ;
int64_t file_size ;
2017-03-21 22:02:30 +02:00
if ( pb - > seekable & AVIO_SEEKABLE_NORMAL ) {
2012-12-28 16:40:55 +03:00
end_guid ( pb , wav - > data ) ;
file_size = avio_tell ( pb ) ;
avio_seek ( pb , 16 , SEEK_SET ) ;
avio_wl64 ( pb , file_size ) ;
2016-04-10 21:58:15 +02:00
if ( s - > streams [ 0 ] - > codecpar - > codec_tag ! = 0x01 ) {
2013-01-05 18:02:08 +03:00
int64_t number_of_samples ;
number_of_samples = av_rescale ( wav - > maxpts - wav - > minpts + wav - > last_duration ,
2016-04-10 21:58:15 +02:00
s - > streams [ 0 ] - > codecpar - > sample_rate * ( int64_t ) s - > streams [ 0 ] - > time_base . num ,
2013-01-05 18:02:08 +03:00
s - > streams [ 0 ] - > time_base . den ) ;
avio_seek ( pb , wav - > fact_pos + 24 , SEEK_SET ) ;
avio_wl64 ( pb , number_of_samples ) ;
}
avio_seek ( pb , file_size , SEEK_SET ) ;
2012-12-28 16:40:55 +03:00
}
return 0 ;
}
2023-01-27 16:06:00 +02:00
const FFOutputFormat ff_w64_muxer = {
. p . name = " w64 " ,
. p . long_name = NULL_IF_CONFIG_SMALL ( " Sony Wave64 " ) ,
. p . extensions = " w64 " ,
2012-12-28 16:40:55 +03:00
. priv_data_size = sizeof ( WAVMuxContext ) ,
2023-01-27 16:06:00 +02:00
. p . audio_codec = AV_CODEC_ID_PCM_S16LE ,
. p . video_codec = AV_CODEC_ID_NONE ,
2022-11-25 11:13:52 +02:00
. init = w64_init ,
2012-12-28 16:40:55 +03:00
. write_header = w64_write_header ,
. write_packet = wav_write_packet ,
. write_trailer = w64_write_trailer ,
2023-01-27 16:06:00 +02:00
. p . flags = AVFMT_TS_NONSTRICT ,
. p . codec_tag = ff_wav_codec_tags_list ,
2012-12-28 16:40:55 +03:00
} ;
# endif /* CONFIG_W64_MUXER */