2007-09-05 03:22:37 +03:00
/*
2007-09-05 03:25:18 +03:00
* Matroska muxer
2007-09-05 03:22:37 +03:00
* Copyright ( c ) 2007 David Conrad
*
* This file is part of FFmpeg .
*
* FFmpeg is free software ; you can redistribute it and / or
* 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 .
*
* FFmpeg is distributed in the hope that it will be useful ,
* 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
* License along with FFmpeg ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
*/
2013-11-23 23:32:55 +03:00
# include <stdint.h>
2022-02-23 14:56:49 +02:00
# include "config_components.h"
2022-01-07 12:36:19 +02:00
2018-07-23 17:33:55 +02:00
# include "av1.h"
2013-04-30 08:52:50 +03:00
# include "avc.h"
2014-03-03 17:53:44 +03:00
# include "hevc.h"
2007-09-05 03:22:37 +03:00
# include "avformat.h"
2013-07-15 23:20:57 +03:00
# include "avio_internal.h"
2013-04-30 08:52:50 +03:00
# include "avlanguage.h"
2022-01-01 18:51:50 +02:00
# include "dovi_isom.h"
2013-04-30 08:52:50 +03:00
# include "flacenc.h"
2011-11-29 21:28:15 +03:00
# include "internal.h"
2008-07-27 18:11:04 +03:00
# include "isom.h"
2007-09-05 03:22:37 +03:00
# include "matroska.h"
2022-05-06 15:01:33 +02:00
# include "mux.h"
2013-04-30 08:52:50 +03:00
# include "riff.h"
2022-02-23 14:18:39 +02:00
# include "version.h"
2014-05-26 14:53:13 +03:00
# include "vorbiscomment.h"
2013-05-28 11:09:21 +03:00
# include "wv.h"
2013-04-30 08:52:50 +03:00
# include "libavutil/avstring.h"
2014-05-26 14:53:13 +03:00
# include "libavutil/channel_layout.h"
2016-10-03 04:46:19 +02:00
# include "libavutil/crc.h"
2013-04-30 08:52:50 +03:00
# include "libavutil/dict.h"
2011-11-27 17:04:16 +03:00
# include "libavutil/intfloat.h"
2013-04-30 08:52:50 +03:00
# include "libavutil/intreadwrite.h"
# include "libavutil/lfg.h"
2016-03-08 00:42:47 +02:00
# include "libavutil/mastering_display_metadata.h"
2011-06-04 14:58:23 +03:00
# include "libavutil/mathematics.h"
2013-04-30 09:36:20 +03:00
# include "libavutil/opt.h"
2016-07-17 04:19:41 +02:00
# include "libavutil/parseutils.h"
2010-06-05 01:41:03 +03:00
# include "libavutil/random_seed.h"
2015-08-05 07:09:57 +02:00
# include "libavutil/rational.h"
2013-04-30 08:52:50 +03:00
# include "libavutil/samplefmt.h"
2014-08-05 13:16:17 +03:00
# include "libavutil/stereo3d.h"
2013-04-30 08:52:50 +03:00
2008-05-09 14:56:36 +03:00
# include "libavcodec/xiph.h"
2008-04-03 00:41:48 +03:00
# include "libavcodec/mpeg4audio.h"
2007-09-05 03:22:37 +03:00
2019-12-29 10:38:44 +02:00
/* Level 1 elements we create a SeekHead entry for:
2020-04-29 10:27:23 +02:00
* Info , Tracks , Chapters , Attachments , Tags ( potentially twice ) and Cues */
# define MAX_SEEKHEAD_ENTRIES 7
2019-12-29 10:38:44 +02:00
2022-01-14 01:33:13 +02:00
/* Largest known-length EBML length */
# define MAX_EBML_LENGTH ((1ULL << 56) - 2)
/* The dynamic buffer API we rely upon has a limit of INT_MAX;
* and so has avio_write ( ) . */
# define MAX_SUPPORTED_EBML_LENGTH FFMIN(MAX_EBML_LENGTH, INT_MAX)
2022-01-07 12:36:19 +02:00
# define MODE_MATROSKAv2 0x01
# define MODE_WEBM 0x02
# define IS_WEBM(mkv) (CONFIG_WEBM_MUXER && CONFIG_MATROSKA_MUXER ? \
( ( mkv ) - > mode = = MODE_WEBM ) : CONFIG_WEBM_MUXER )
avformat/matroskaenc: Remove inconsistencies wrt seekability handling
The Matroska muxer behaves differently in several ways when it thinks
that it is in unseekable/livestreaming mode: It does not add Cue entries
because they won't be written anyway for a livestream and it writes some
elements only preliminarily (with the intention to overwrite them with
an updated version at the end) when non-livestreaming etc.
There are two ways to set the Matroska muxer into livestreaming mode:
Setting an option or by providing an unseekable AVIOContext. Yet the
actual checks were not consistent:
If the AVIOContext was unseekable and no AAC extradata was available
when writing the header, writing the header failed; but if the AVIOContext
was seekable, it didn't, because the muxer expected to get the extradata
via packet side-data. Here the livestreaming option has not been checked,
although one can't use the updated extradata in case it is a livestream.
If the reserve_index_space option was used, space for writing Cues would
be reserved when writing the header unless the AVIOContext was
unseekable. Yet Cues were only written if the livestreaming option was
not set and the AVIOContext was seekable (when writing the trailer), so
if the AVIOContext was seekable and the livestreaming option set, the
reserved space would never be used at all.
If the AVIOContext was unseekable and the livestreaming option was not
set, it would be attempted to update the main length field at the end.
After all, it might be possible that the file is so short that it fits
into the AVIOContext's buffer in which case the seek back would work.
Yet this is dangerous: It might be that we are not dealing with a
simple output file, but that our output gets split into chunks and that
each of these chunks is actually seekable. In this case some part of the
last chunk (namely the eight bytes that have the same offset as the
length field had in the header) will be overwritten with what the muxer
wrongly believes to be the filesize.
(The livestreaming option has been added to deal with this scenario,
yet its documentation ("Write files assuming it is a live stream.")
doesn't make this clear at all. At least the segment muxer does not
set the option for live and given that the chances of successfully
seeking when the output is actually unseekable are slim, it is best to
not attempt to update the length field in the unseekable case at all.)
All these inconsistencies were fixed by treating the output as seekable
if the livestreaming option is not set and if the AVIOContext is
seekable. A macro has been used to enforce consistency and improve code
readability.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-05-01 22:31:36 +02:00
# define IS_SEEKABLE(pb, mkv) (((pb)->seekable & AVIO_SEEKABLE_NORMAL) && \
! ( mkv ) - > is_live )
2020-01-18 08:05:20 +02:00
enum {
DEFAULT_MODE_INFER ,
DEFAULT_MODE_INFER_NO_SUBS ,
DEFAULT_MODE_PASSTHROUGH ,
} ;
2007-09-05 03:24:56 +03:00
typedef struct ebml_master {
2019-04-20 01:41:10 +02:00
int64_t pos ; ///< absolute offset in the containing AVIOContext where the master's elements start
2019-04-20 01:41:08 +02:00
int sizebytes ; ///< how many bytes were reserved for the size
2007-09-05 03:24:56 +03:00
} ebml_master ;
2020-04-29 06:31:07 +02:00
typedef struct ebml_stored_master {
AVIOContext * bc ;
int64_t pos ;
} ebml_stored_master ;
2022-01-14 01:33:13 +02:00
typedef enum EbmlType {
EBML_UINT ,
EBML_SINT ,
EBML_FLOAT ,
EBML_UID ,
EBML_STR ,
EBML_UTF8 = EBML_STR ,
EBML_BIN ,
2022-01-16 15:01:28 +02:00
EBML_BLOCK , ///< pseudo-type for writing (Simple)Blocks
2022-01-14 01:33:13 +02:00
EBML_MASTER ,
} EbmlType ;
2022-01-16 15:01:28 +02:00
typedef struct BlockContext {
struct mkv_track * track ;
const AVPacket * pkt ;
int16_t rel_ts ;
uint8_t flags ;
NALUList h2645_nalu_list ;
} BlockContext ;
2022-01-14 01:33:13 +02:00
typedef struct EbmlMaster {
int nb_elements ; ///< -1 if not finished
int containing_master ; ///< -1 if no parent exists
} EbmlMaster ;
typedef struct EbmlElement {
uint32_t id ;
EbmlType type ;
unsigned length_size ;
uint64_t size ; ///< excluding id and length field
union {
uint64_t uint ;
int64_t sint ;
double f ;
const char * str ;
const uint8_t * bin ;
2022-01-16 15:01:28 +02:00
struct MatroskaMuxContext * mkv ; ///< used by EBML_BLOCK
2022-01-14 01:33:13 +02:00
EbmlMaster master ;
} priv ;
} EbmlElement ;
typedef struct EbmlWriter {
unsigned nb_elements ;
int current_master_element ;
EbmlElement * elements ;
} EbmlWriter ;
# define EBML_WRITER(max_nb_elems) \
EbmlElement elements [ max_nb_elems ] ; \
EbmlWriter writer = ( EbmlWriter ) { . elements = elements , \
. current_master_element = - 1 }
2007-09-05 03:23:19 +03:00
typedef struct mkv_seekhead_entry {
2019-04-20 01:41:04 +02:00
uint32_t elementid ;
2007-09-05 03:23:19 +03:00
uint64_t segmentpos ;
} mkv_seekhead_entry ;
typedef struct mkv_seekhead {
2008-10-03 13:16:29 +03:00
int64_t filepos ;
2019-12-29 10:38:44 +02:00
mkv_seekhead_entry entries [ MAX_SEEKHEAD_ENTRIES ] ;
2007-09-05 03:23:19 +03:00
int num_entries ;
2019-12-29 10:38:44 +02:00
int reserved_size ;
2007-09-05 03:23:19 +03:00
} mkv_seekhead ;
2014-09-22 10:19:33 +03:00
typedef struct mkv_cuepoint {
2007-09-05 03:23:27 +03:00
uint64_t pts ;
2014-05-07 19:46:40 +03:00
int stream_idx ;
2019-10-15 13:17:34 +02:00
int64_t cluster_pos ; ///< offset of the cluster containing the block relative to the segment
2013-07-23 10:52:08 +03:00
int64_t relative_pos ; ///< relative offset from the position of the cluster containing the block
2013-09-21 04:15:49 +03:00
int64_t duration ; ///< duration of the block according to time base
2007-09-05 03:23:27 +03:00
} mkv_cuepoint ;
2014-09-22 10:19:33 +03:00
typedef struct mkv_cues {
2020-04-29 07:24:02 +02:00
mkv_cuepoint * entries ;
2007-09-05 03:23:27 +03:00
int num_entries ;
} mkv_cues ;
2022-01-15 23:56:26 +02:00
struct MatroskaMuxContext ;
2014-09-22 10:19:33 +03:00
typedef struct mkv_track {
2010-03-09 06:23:28 +02:00
int write_dts ;
2013-02-10 23:32:37 +03:00
int has_cue ;
2019-12-29 02:33:36 +02:00
uint64_t uid ;
2019-11-27 06:00:04 +02:00
unsigned track_num ;
2020-04-15 04:49:46 +02:00
int track_num_size ;
2017-04-13 17:43:11 +02:00
int sample_rate ;
2022-01-16 13:12:17 +02:00
unsigned offset ;
2017-04-13 17:43:11 +02:00
int64_t sample_rate_offset ;
2019-10-16 04:00:56 +02:00
int64_t last_timestamp ;
int64_t duration ;
int64_t duration_offset ;
2022-06-17 05:49:06 +02:00
int codecpriv_offset ;
unsigned codecpriv_size ; ///< size reserved for CodecPrivate excluding header+length field
2014-04-29 13:26:38 +03:00
int64_t ts_offset ;
2022-01-15 23:56:26 +02:00
/* This callback will be called twice: First with a NULL AVIOContext
* to return the size of the ( Simple ) Block ' s data via size
* and a second time with the AVIOContext set when the data
* shall be written .
* The callback shall not return an error on the second call . */
int ( * reformat ) ( struct MatroskaMuxContext * , AVIOContext * ,
const AVPacket * , int * size ) ;
2010-03-09 06:23:28 +02:00
} mkv_track ;
2007-09-05 03:22:37 +03:00
typedef struct MatroskaMuxContext {
2020-04-29 07:24:02 +02:00
const AVClass * class ;
2022-01-16 15:01:28 +02:00
AVFormatContext * ctx ;
2020-04-29 07:24:02 +02:00
int mode ;
ebml_stored_master info ;
ebml_stored_master track ;
ebml_stored_master tags ;
int64_t segment_offset ;
AVIOContext * cluster_bc ;
int64_t cluster_pos ; ///< file offset of the current Cluster
int64_t cluster_pts ;
int64_t duration_offset ;
int64_t duration ;
mkv_track * tracks ;
mkv_seekhead seekhead ;
mkv_cues cues ;
int64_t cues_pos ;
2022-01-16 15:01:28 +02:00
BlockContext cur_block ;
2022-01-16 02:38:41 +02:00
2022-06-15 11:25:12 +02:00
/* Used as temporary buffer to use the minimal amount of bytes
* to write the length field of EBML Masters .
* Every user has to reset the buffer after using it and
* different uses may not overlap . It is currently used in
* mkv_write_tag ( ) . */
AVIOContext * tmp_bc ;
2021-01-29 15:46:43 +02:00
AVPacket * cur_audio_pkt ;
2020-04-29 07:24:02 +02:00
unsigned nb_attachments ;
int have_video ;
int wrote_chapters ;
2020-04-29 10:27:23 +02:00
int wrote_tags ;
2020-04-29 07:24:02 +02:00
int reserve_cues_space ;
int cluster_size_limit ;
int64_t cluster_time_limit ;
int write_crc ;
int is_live ;
int is_dash ;
int dash_track_number ;
int allow_raw_vfw ;
2020-07-08 14:02:25 +02:00
int flipped_raw_rgb ;
2020-04-29 07:24:02 +02:00
int default_mode ;
2022-01-09 20:45:58 +02:00
int move_cues_to_front ;
2020-04-29 07:24:02 +02:00
uint32_t segment_uid [ 4 ] ;
2007-09-05 03:22:37 +03:00
} MatroskaMuxContext ;
2007-09-05 03:25:42 +03:00
/** 2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit
* offset , 4 bytes for target EBML ID */
2007-09-05 03:24:56 +03:00
# define MAX_SEEKENTRY_SIZE 21
2019-12-30 16:48:35 +02:00
/** 4 * (1-byte EBML ID, 1-byte EBML size, 8-byte uint max) */
# define MAX_CUETRACKPOS_SIZE 40
2007-09-05 03:24:56 +03:00
2022-06-15 11:25:12 +02:00
/** 2 + 1 Simpletag header, 2 + 1 + 8 Name "DURATION", 23B for TagString */
# define DURATION_SIMPLETAG_SIZE (2 + 1 + (2 + 1 + 8) + 23)
2013-08-16 21:08:06 +03:00
/** Seek preroll value for opus */
# define OPUS_SEEK_PREROLL 80000000
2019-04-20 01:41:04 +02:00
static int ebml_id_size ( uint32_t id )
2007-09-05 03:24:34 +03:00
{
2019-12-28 11:00:37 +02:00
return ( av_log2 ( id ) + 7U ) / 8 ;
2007-09-05 03:24:34 +03:00
}
2019-04-20 01:41:04 +02:00
static void put_ebml_id ( AVIOContext * pb , uint32_t id )
2007-09-05 03:22:37 +03:00
{
2007-09-05 03:24:36 +03:00
int i = ebml_id_size ( id ) ;
while ( i - - )
2014-07-01 20:58:57 +03:00
avio_w8 ( pb , ( uint8_t ) ( id > > ( i * 8 ) ) ) ;
2007-09-05 03:22:37 +03:00
}
2007-09-05 03:24:31 +03:00
/**
2007-09-05 23:31:40 +03:00
* Write an EBML size meaning " unknown size " .
2007-09-05 03:24:31 +03:00
*
2007-09-05 23:31:40 +03:00
* @ param bytes The number of bytes the size should occupy ( maximum : 8 ) .
2007-09-05 03:24:31 +03:00
*/
2011-02-20 12:04:12 +02:00
static void put_ebml_size_unknown ( AVIOContext * pb , int bytes )
2007-09-05 03:24:31 +03:00
{
2012-07-13 18:03:57 +03:00
av_assert0 ( bytes < = 8 ) ;
2011-02-21 20:28:17 +02:00
avio_w8 ( pb , 0x1ff > > bytes ) ;
2021-04-14 20:00:12 +02:00
if ( av_builtin_constant_p ( bytes ) & & bytes = = 1 )
return ;
2013-07-15 23:20:57 +03:00
ffio_fill ( pb , 0xff , bytes - 1 ) ;
2007-09-05 03:24:31 +03:00
}
2007-09-05 03:24:42 +03:00
/**
2020-04-15 04:05:09 +02:00
* Returns how many bytes are needed to represent a number
* as EBML variable length integer .
2007-09-05 03:24:42 +03:00
*/
2020-04-15 04:05:09 +02:00
static int ebml_num_size ( uint64_t num )
2007-09-05 03:24:42 +03:00
{
2020-04-02 23:07:55 +02:00
int bytes = 0 ;
do {
2014-06-07 01:54:50 +03:00
bytes + + ;
2020-04-15 04:05:09 +02:00
} while ( num > > = 7 ) ;
2007-09-05 03:24:42 +03:00
return bytes ;
}
2020-04-15 04:05:09 +02:00
/**
* Calculate how many bytes are needed to represent the length field
* of an EBML element whose payload has a given length .
*/
static int ebml_length_size ( uint64_t length )
{
return ebml_num_size ( length + 1 ) ;
}
/**
* Write a number as EBML variable length integer on ` bytes ` bytes .
* ` bytes ` is taken literally without checking .
*/
static void put_ebml_num ( AVIOContext * pb , uint64_t num , int bytes )
{
num | = 1ULL < < bytes * 7 ;
for ( int i = bytes - 1 ; i > = 0 ; i - - )
avio_w8 ( pb , ( uint8_t ) ( num > > i * 8 ) ) ;
}
2007-09-05 03:24:50 +03:00
/**
2020-04-15 03:39:37 +02:00
* Write a length as EBML variable length integer .
2007-09-05 03:24:50 +03:00
*
2007-09-05 03:26:07 +03:00
* @ param bytes The number of bytes that need to be used to write the number .
2020-01-01 02:58:21 +02:00
* If zero , the minimal number of bytes will be used .
2007-09-05 03:24:50 +03:00
*/
2020-04-15 03:39:37 +02:00
static void put_ebml_length ( AVIOContext * pb , uint64_t length , int bytes )
2007-09-05 03:22:37 +03:00
{
2020-04-15 04:05:09 +02:00
int needed_bytes = ebml_length_size ( length ) ;
2007-09-05 03:22:37 +03:00
// sizes larger than this are currently undefined in EBML
2020-04-15 03:39:37 +02:00
av_assert0 ( length < ( 1ULL < < 56 ) - 1 ) ;
2007-09-05 03:23:06 +03:00
2007-09-05 03:24:50 +03:00
if ( bytes = = 0 )
bytes = needed_bytes ;
2020-01-01 02:58:21 +02:00
// The bytes needed to write the given size must not exceed
// the bytes that we ought to use.
2012-07-13 18:03:57 +03:00
av_assert0 ( bytes > = needed_bytes ) ;
2020-04-15 04:05:09 +02:00
put_ebml_num ( pb , length , bytes ) ;
2007-09-05 03:22:37 +03:00
}
2019-12-29 11:20:42 +02:00
/**
* Write a ( random ) UID with fixed size to make the output more deterministic
*/
static void put_ebml_uid ( AVIOContext * pb , uint32_t elementid , uint64_t uid )
{
put_ebml_id ( pb , elementid ) ;
2020-04-15 03:39:37 +02:00
put_ebml_length ( pb , 8 , 0 ) ;
2019-12-29 11:20:42 +02:00
avio_wb64 ( pb , uid ) ;
}
2019-04-20 01:41:04 +02:00
static void put_ebml_uint ( AVIOContext * pb , uint32_t elementid , uint64_t val )
2007-09-05 03:22:37 +03:00
{
2007-09-05 03:24:39 +03:00
int i , bytes = 1 ;
2008-03-01 02:26:19 +02:00
uint64_t tmp = val ;
2014-06-07 01:54:50 +03:00
while ( tmp > > = 8 )
bytes + + ;
2007-09-05 03:22:37 +03:00
put_ebml_id ( pb , elementid ) ;
2020-04-15 03:39:37 +02:00
put_ebml_length ( pb , bytes , 0 ) ;
2007-09-05 03:24:39 +03:00
for ( i = bytes - 1 ; i > = 0 ; i - - )
2014-07-01 20:58:57 +03:00
avio_w8 ( pb , ( uint8_t ) ( val > > i * 8 ) ) ;
2007-09-05 03:22:37 +03:00
}
2019-04-20 01:41:04 +02:00
static void put_ebml_float ( AVIOContext * pb , uint32_t elementid , double val )
2007-09-05 03:22:47 +03:00
{
put_ebml_id ( pb , elementid ) ;
2020-04-15 03:39:37 +02:00
put_ebml_length ( pb , 8 , 0 ) ;
2011-11-27 17:04:16 +03:00
avio_wb64 ( pb , av_double2int ( val ) ) ;
2007-09-05 03:22:47 +03:00
}
2019-04-20 01:41:04 +02:00
static void put_ebml_binary ( AVIOContext * pb , uint32_t elementid ,
2010-06-05 01:40:58 +03:00
const void * buf , int size )
2007-09-05 03:22:37 +03:00
{
put_ebml_id ( pb , elementid ) ;
2020-04-15 03:39:37 +02:00
put_ebml_length ( pb , size , 0 ) ;
2011-02-21 20:28:17 +02:00
avio_write ( pb , buf , size ) ;
2007-09-05 03:22:37 +03:00
}
2019-04-20 01:41:04 +02:00
static void put_ebml_string ( AVIOContext * pb , uint32_t elementid ,
2014-06-07 01:54:50 +03:00
const char * str )
2007-09-05 03:22:37 +03:00
{
put_ebml_binary ( pb , elementid , str , strlen ( str ) ) ;
}
2007-09-05 03:24:25 +03:00
/**
2010-06-30 18:38:06 +03:00
* Write a void element of a given size . Useful for reserving space in
2007-09-05 03:25:16 +03:00
* the file to be written to later .
2007-09-05 03:24:25 +03:00
*
2007-09-05 03:25:04 +03:00
* @ param size The number of bytes to reserve , which must be at least 2.
2007-09-05 03:24:25 +03:00
*/
2020-01-12 02:30:58 +02:00
static void put_ebml_void ( AVIOContext * pb , int size )
2007-09-05 03:23:09 +03:00
{
2012-07-13 18:03:57 +03:00
av_assert0 ( size > = 2 ) ;
2007-09-05 03:23:09 +03:00
put_ebml_id ( pb , EBML_ID_VOID ) ;
2007-09-05 03:25:16 +03:00
// we need to subtract the length needed to store the size from the
// size we need to reserve so 2 cases, we use 8 bytes to store the
// size if possible, 1 byte otherwise
2020-01-12 02:30:58 +02:00
if ( size < 10 ) {
size - = 2 ;
2020-04-15 03:39:37 +02:00
put_ebml_length ( pb , size , 0 ) ;
2020-01-12 02:30:58 +02:00
} else {
size - = 9 ;
2020-04-15 03:39:37 +02:00
put_ebml_length ( pb , size , 8 ) ;
2020-01-12 02:30:58 +02:00
}
ffio_fill ( pb , 0 , size ) ;
2007-09-05 03:23:09 +03:00
}
2019-04-20 01:41:04 +02:00
static ebml_master start_ebml_master ( AVIOContext * pb , uint32_t elementid ,
2014-06-07 01:54:50 +03:00
uint64_t expectedsize )
2007-09-05 03:22:37 +03:00
{
2020-04-15 03:39:37 +02:00
int bytes = expectedsize ? ebml_length_size ( expectedsize ) : 8 ;
2019-04-20 01:41:05 +02:00
2007-09-05 03:22:37 +03:00
put_ebml_id ( pb , elementid ) ;
2007-09-05 03:24:56 +03:00
put_ebml_size_unknown ( pb , bytes ) ;
2019-04-20 01:41:07 +02:00
return ( ebml_master ) { avio_tell ( pb ) , bytes } ;
2007-09-05 03:22:37 +03:00
}
2011-02-20 12:04:12 +02:00
static void end_ebml_master ( AVIOContext * pb , ebml_master master )
2007-09-05 03:22:37 +03:00
{
2011-03-03 21:11:45 +02:00
int64_t pos = avio_tell ( pb ) ;
2007-09-05 03:22:37 +03:00
2011-02-28 15:57:54 +02:00
if ( avio_seek ( pb , master . pos - master . sizebytes , SEEK_SET ) < 0 )
2007-09-05 03:26:00 +03:00
return ;
2020-04-15 03:39:37 +02:00
put_ebml_length ( pb , pos - master . pos , master . sizebytes ) ;
2011-02-28 15:57:54 +02:00
avio_seek ( pb , pos , SEEK_SET ) ;
2007-09-05 03:22:37 +03:00
}
2022-01-14 01:33:13 +02:00
static EbmlElement * ebml_writer_add ( EbmlWriter * writer ,
uint32_t id , EbmlType type )
{
writer - > elements [ writer - > nb_elements ] . id = id ;
writer - > elements [ writer - > nb_elements ] . type = type ;
return & writer - > elements [ writer - > nb_elements + + ] ;
}
static void ebml_writer_open_master ( EbmlWriter * writer , uint32_t id )
{
EbmlElement * const elem = ebml_writer_add ( writer , id , EBML_MASTER ) ;
EbmlMaster * const master = & elem - > priv . master ;
master - > containing_master = writer - > current_master_element ;
master - > nb_elements = - 1 ;
writer - > current_master_element = writer - > nb_elements - 1 ;
}
2022-01-14 14:56:52 +02:00
static void ebml_writer_close_master ( EbmlWriter * writer )
{
EbmlElement * elem ;
av_assert2 ( writer - > current_master_element > = 0 ) ;
av_assert2 ( writer - > current_master_element < writer - > nb_elements ) ;
elem = & writer - > elements [ writer - > current_master_element ] ;
av_assert2 ( elem - > type = = EBML_MASTER ) ;
2022-06-16 19:06:10 +02:00
av_assert2 ( elem - > priv . master . nb_elements < 0 ) ; /* means unset */
2022-01-14 14:56:52 +02:00
elem - > priv . master . nb_elements = writer - > nb_elements - writer - > current_master_element - 1 ;
2022-06-16 19:06:10 +02:00
av_assert2 ( elem - > priv . master . containing_master < 0 | |
elem - > priv . master . containing_master < writer - > current_master_element ) ;
2022-01-14 14:56:52 +02:00
writer - > current_master_element = elem - > priv . master . containing_master ;
}
static void ebml_writer_close_or_discard_master ( EbmlWriter * writer )
{
av_assert2 ( writer - > nb_elements > 0 ) ;
2022-06-16 19:06:10 +02:00
av_assert2 ( 0 < = writer - > current_master_element ) ;
av_assert2 ( writer - > current_master_element < writer - > nb_elements ) ;
2022-01-14 14:56:52 +02:00
if ( writer - > current_master_element = = writer - > nb_elements - 1 ) {
2022-06-16 19:06:10 +02:00
const EbmlElement * const elem = & writer - > elements [ writer - > nb_elements - 1 ] ;
2022-01-14 14:56:52 +02:00
/* The master element has no children. Discard it. */
2022-06-16 19:06:10 +02:00
av_assert2 ( elem - > type = = EBML_MASTER ) ;
av_assert2 ( elem - > priv . master . containing_master < 0 | |
elem - > priv . master . containing_master < writer - > current_master_element ) ;
writer - > current_master_element = elem - > priv . master . containing_master ;
2022-01-14 14:56:52 +02:00
writer - > nb_elements - - ;
return ;
}
ebml_writer_close_master ( writer ) ;
}
2022-01-14 01:33:13 +02:00
static void ebml_writer_add_string ( EbmlWriter * writer , uint32_t id ,
const char * str )
{
EbmlElement * const elem = ebml_writer_add ( writer , id , EBML_STR ) ;
elem - > priv . str = str ;
}
static void ebml_writer_add_bin ( EbmlWriter * writer , uint32_t id ,
const uint8_t * data , size_t size )
{
EbmlElement * const elem = ebml_writer_add ( writer , id , EBML_BIN ) ;
# if SIZE_MAX > UINT64_MAX
size = FFMIN ( size , UINT64_MAX ) ;
# endif
elem - > size = size ;
elem - > priv . bin = data ;
}
static void ebml_writer_add_float ( EbmlWriter * writer , uint32_t id ,
double val )
{
EbmlElement * const elem = ebml_writer_add ( writer , id , EBML_FLOAT ) ;
elem - > priv . f = val ;
}
static void ebml_writer_add_uid ( EbmlWriter * writer , uint32_t id ,
uint64_t val )
{
EbmlElement * const elem = ebml_writer_add ( writer , id , EBML_UID ) ;
elem - > priv . uint = val ;
}
2022-01-14 02:18:24 +02:00
static void ebml_writer_add_uint ( EbmlWriter * writer , uint32_t id ,
uint64_t val )
{
EbmlElement * elem = ebml_writer_add ( writer , id , EBML_UINT ) ;
elem - > priv . uint = val ;
}
2022-01-16 15:01:28 +02:00
static void ebml_writer_add_sint ( EbmlWriter * writer , uint32_t id ,
int64_t val )
{
EbmlElement * elem = ebml_writer_add ( writer , id , EBML_SINT ) ;
elem - > priv . sint = val ;
}
static void ebml_writer_add_block ( EbmlWriter * writer , MatroskaMuxContext * mkv )
{
EbmlElement * elem = ebml_writer_add ( writer , MATROSKA_ID_BLOCK , EBML_BLOCK ) ;
elem - > priv . mkv = mkv ;
}
2022-01-14 01:33:13 +02:00
static int ebml_writer_str_len ( EbmlElement * elem )
{
size_t len = strlen ( elem - > priv . str ) ;
# if SIZE_MAX > UINT64_MAX
len = FF_MIN ( len , UINT64_MAX ) ;
# endif
elem - > size = len ;
return 0 ;
}
static av_const int uint_size ( uint64_t val )
{
int bytes = 0 ;
do {
bytes + + ;
} while ( val > > = 8 ) ;
return bytes ;
}
static int ebml_writer_uint_len ( EbmlElement * elem )
{
elem - > size = uint_size ( elem - > priv . uint ) ;
return 0 ;
}
static av_const int sint_size ( int64_t val )
{
uint64_t tmp = 2 * ( uint64_t ) ( val < 0 ? val ^ - 1 : val ) ;
return uint_size ( tmp ) ;
}
static int ebml_writer_sint_len ( EbmlElement * elem )
{
elem - > size = sint_size ( elem - > priv . sint ) ;
return 0 ;
}
static int ebml_writer_elem_len ( EbmlWriter * writer , EbmlElement * elem ,
int remaining_elems ) ;
static int ebml_writer_master_len ( EbmlWriter * writer , EbmlElement * elem ,
int remaining_elems )
{
int nb_elems = elem - > priv . master . nb_elements > = 0 ? elem - > priv . master . nb_elements : remaining_elems - 1 ;
EbmlElement * const master = elem ;
uint64_t total_size = 0 ;
master - > priv . master . nb_elements = nb_elems ;
for ( ; elem + + , nb_elems > 0 ; ) {
int ret = ebml_writer_elem_len ( writer , elem , nb_elems ) ;
if ( ret < 0 )
return ret ;
av_assert2 ( ret < nb_elems ) ;
/* No overflow is possible here, as both total_size and elem->size
* are bounded by MAX_SUPPORTED_EBML_LENGTH . */
total_size + = ebml_id_size ( elem - > id ) + elem - > length_size + elem - > size ;
if ( total_size > MAX_SUPPORTED_EBML_LENGTH )
return AVERROR ( ERANGE ) ;
nb_elems - - ; /* consume elem */
elem + = ret , nb_elems - = ret ; /* and elem's children */
}
master - > size = total_size ;
return master - > priv . master . nb_elements ;
}
2022-01-16 15:01:28 +02:00
static int ebml_writer_block_len ( EbmlElement * elem )
{
MatroskaMuxContext * const mkv = elem - > priv . mkv ;
BlockContext * const block = & mkv - > cur_block ;
mkv_track * const track = block - > track ;
const AVPacket * const pkt = block - > pkt ;
int err , size ;
if ( track - > reformat ) {
err = track - > reformat ( mkv , NULL , pkt , & size ) ;
if ( err < 0 ) {
av_log ( mkv - > ctx , AV_LOG_ERROR , " Error when reformatting data of "
" a packet from stream %d. \n " , pkt - > stream_index ) ;
return err ;
}
} else {
size = pkt - > size ;
if ( track - > offset < = size )
size - = track - > offset ;
}
elem - > size = track - > track_num_size + 3U + size ;
return 0 ;
}
static void ebml_writer_write_block ( const EbmlElement * elem , AVIOContext * pb )
{
MatroskaMuxContext * const mkv = elem - > priv . mkv ;
BlockContext * const block = & mkv - > cur_block ;
mkv_track * const track = block - > track ;
const AVPacket * const pkt = block - > pkt ;
put_ebml_num ( pb , track - > track_num , track - > track_num_size ) ;
avio_wb16 ( pb , block - > rel_ts ) ;
avio_w8 ( pb , block - > flags ) ;
if ( track - > reformat ) {
int size ;
track - > reformat ( mkv , pb , pkt , & size ) ;
} else {
const uint8_t * data = pkt - > data ;
unsigned offset = track - > offset < = pkt - > size ? track - > offset : 0 ;
avio_write ( pb , data + offset , pkt - > size - offset ) ;
}
}
2022-01-14 01:33:13 +02:00
static int ebml_writer_elem_len ( EbmlWriter * writer , EbmlElement * elem ,
int remaining_elems )
{
int ret = 0 ;
switch ( elem - > type ) {
case EBML_FLOAT :
case EBML_UID :
elem - > size = 8 ;
break ;
case EBML_STR :
ret = ebml_writer_str_len ( elem ) ;
break ;
case EBML_UINT :
ret = ebml_writer_uint_len ( elem ) ;
break ;
case EBML_SINT :
ret = ebml_writer_sint_len ( elem ) ;
break ;
2022-01-16 15:01:28 +02:00
case EBML_BLOCK :
ret = ebml_writer_block_len ( elem ) ;
break ;
2022-01-14 01:33:13 +02:00
case EBML_MASTER :
ret = ebml_writer_master_len ( writer , elem , remaining_elems ) ;
break ;
}
if ( ret < 0 )
return ret ;
if ( elem - > size > MAX_SUPPORTED_EBML_LENGTH )
return AVERROR ( ERANGE ) ;
elem - > length_size = ebml_length_size ( elem - > size ) ;
return ret ; /* number of elements consumed excluding elem itself */
}
static int ebml_writer_elem_write ( const EbmlElement * elem , AVIOContext * pb )
{
put_ebml_id ( pb , elem - > id ) ;
put_ebml_num ( pb , elem - > size , elem - > length_size ) ;
switch ( elem - > type ) {
case EBML_UID :
case EBML_FLOAT : {
uint64_t val = elem - > type = = EBML_UID ? elem - > priv . uint
: av_double2int ( elem - > priv . f ) ;
avio_wb64 ( pb , val ) ;
break ;
}
case EBML_UINT :
case EBML_SINT : {
uint64_t val = elem - > type = = EBML_UINT ? elem - > priv . uint
: elem - > priv . sint ;
for ( int i = elem - > size ; - - i > = 0 ; )
avio_w8 ( pb , ( uint8_t ) ( val > > i * 8 ) ) ;
break ;
}
case EBML_STR :
case EBML_BIN : {
const uint8_t * data = elem - > type = = EBML_BIN ? elem - > priv . bin
: ( const uint8_t * ) elem - > priv . str ;
avio_write ( pb , data , elem - > size ) ;
break ;
}
2022-01-16 15:01:28 +02:00
case EBML_BLOCK :
ebml_writer_write_block ( elem , pb ) ;
break ;
2022-01-14 01:33:13 +02:00
case EBML_MASTER : {
int nb_elems = elem - > priv . master . nb_elements ;
elem + + ;
for ( int i = 0 ; i < nb_elems ; i + + )
i + = ebml_writer_elem_write ( elem + i , pb ) ;
return nb_elems ;
}
}
return 0 ;
}
static int ebml_writer_write ( EbmlWriter * writer , AVIOContext * pb )
{
int ret = ebml_writer_elem_len ( writer , writer - > elements ,
writer - > nb_elements ) ;
if ( ret < 0 )
return ret ;
ebml_writer_elem_write ( writer - > elements , pb ) ;
return 0 ;
}
2020-04-27 04:34:45 +02:00
static void mkv_add_seekhead_entry ( MatroskaMuxContext * mkv , uint32_t elementid ,
uint64_t filepos )
{
mkv_seekhead * seekhead = & mkv - > seekhead ;
av_assert1 ( seekhead - > num_entries < MAX_SEEKHEAD_ENTRIES ) ;
seekhead - > entries [ seekhead - > num_entries ] . elementid = elementid ;
seekhead - > entries [ seekhead - > num_entries + + ] . segmentpos = filepos - mkv - > segment_offset ;
}
2019-12-30 16:59:02 +02:00
static int start_ebml_master_crc32 ( AVIOContext * * dyn_cp , MatroskaMuxContext * mkv )
2016-10-03 04:46:19 +02:00
{
2019-04-20 01:41:08 +02:00
int ret ;
2016-10-03 04:46:19 +02:00
avformat/matroskaenc: Stop reallocating of Cluster buffer
The Matroska muxer uses a dynamic buffer to buffer the content of
Clusters before eventually writing them. Up until now, each time a
Cluster was written, the dynamic buffer was closed, i.e. freed; now it
is only reset, saving allocations of the AVIOContext itself, its opaque
as well as most of the reallocations of the buffer.
This is advantageous performance-wise, in particular on systems where
reallocations are slow (namely Windows). The following table shows the
decicyles for writing a frame on Linux (Ubuntu 19.10) and Windows (7)
on an x64 Haswell (to /dev/null on Linux, to stdout which is discarded
on Windows (the default values of the size and duration of clusters for
seekable output have been explicitly set in this case); in all tests,
writing CRC-32 values has been disabled in all tests; calls to the muxer's
write_packet function in write_packet() in libavformat/mux.c have been
timed; each of the following tests has been repeated 50 times):
| Windows before | Windows after | Linux before | Linux after
_________________________________________________________________
A | 979437 | 192304 | 259500 | 183320
B | 715936 | 155648 | 152786 | 130879
C | 265115 | 56034 | 78496 | 53243
D | 386224 | 80307 | 128894 | 75354
E | 21732 | 10695 | 11320 | 9801
(A is a 10.2 mb/s file with a GOP length of 2s, amounting to an average
Cluster size of about 2.5 MiB; the average Cluster size of B is 1.1 MiB;
for C it is 2.35 MiB, for D it is 0.46 MiB; for E - a file with just a
single audio track of 158kb/s resulting in a Cluster size of about 100
kB, the relative gains were the smallest, probably because of the small
Cluster size.)
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2019-12-29 02:01:52 +02:00
if ( ! * dyn_cp & & ( ret = avio_open_dyn_buf ( dyn_cp ) ) < 0 )
2016-10-03 04:46:19 +02:00
return ret ;
2019-04-20 01:41:07 +02:00
if ( mkv - > write_crc )
put_ebml_void ( * dyn_cp , 6 ) ; /* Reserve space for CRC32 so position/size calculations using avio_tell() take it into account */
2016-10-03 04:46:19 +02:00
return 0 ;
}
2020-04-29 20:55:26 +02:00
static int end_ebml_master_crc32 ( AVIOContext * pb , AVIOContext * * dyn_cp ,
2020-04-29 07:24:02 +02:00
MatroskaMuxContext * mkv , uint32_t id ,
int length_size , int keep_buffer ,
int add_seekentry )
2016-10-03 04:46:19 +02:00
{
uint8_t * buf , crc [ 4 ] ;
2020-04-29 20:55:26 +02:00
int ret , size , skip = 0 ;
size = avio_get_dyn_buf ( * dyn_cp , & buf ) ;
if ( ( ret = ( * dyn_cp ) - > error ) < 0 )
goto fail ;
2016-10-03 04:46:19 +02:00
2020-04-27 04:34:45 +02:00
if ( add_seekentry )
mkv_add_seekhead_entry ( mkv , id , avio_tell ( pb ) ) ;
2019-12-30 16:59:02 +02:00
put_ebml_id ( pb , id ) ;
2020-04-15 03:39:37 +02:00
put_ebml_length ( pb , size , length_size ) ;
2019-04-20 01:41:07 +02:00
if ( mkv - > write_crc ) {
skip = 6 ; /* Skip reserved 6-byte long void element from the dynamic buffer. */
AV_WL32 ( crc , av_crc ( av_crc_get_table ( AV_CRC_32_IEEE_LE ) , UINT32_MAX , buf + skip , size - skip ) ^ UINT32_MAX ) ;
put_ebml_binary ( pb , EBML_ID_CRC32 , crc , sizeof ( crc ) ) ;
}
avio_write ( pb , buf + skip , size - skip ) ;
2019-04-20 01:41:06 +02:00
2020-04-29 20:55:26 +02:00
fail :
avformat/matroskaenc: Stop reallocating of Cluster buffer
The Matroska muxer uses a dynamic buffer to buffer the content of
Clusters before eventually writing them. Up until now, each time a
Cluster was written, the dynamic buffer was closed, i.e. freed; now it
is only reset, saving allocations of the AVIOContext itself, its opaque
as well as most of the reallocations of the buffer.
This is advantageous performance-wise, in particular on systems where
reallocations are slow (namely Windows). The following table shows the
decicyles for writing a frame on Linux (Ubuntu 19.10) and Windows (7)
on an x64 Haswell (to /dev/null on Linux, to stdout which is discarded
on Windows (the default values of the size and duration of clusters for
seekable output have been explicitly set in this case); in all tests,
writing CRC-32 values has been disabled in all tests; calls to the muxer's
write_packet function in write_packet() in libavformat/mux.c have been
timed; each of the following tests has been repeated 50 times):
| Windows before | Windows after | Linux before | Linux after
_________________________________________________________________
A | 979437 | 192304 | 259500 | 183320
B | 715936 | 155648 | 152786 | 130879
C | 265115 | 56034 | 78496 | 53243
D | 386224 | 80307 | 128894 | 75354
E | 21732 | 10695 | 11320 | 9801
(A is a 10.2 mb/s file with a GOP length of 2s, amounting to an average
Cluster size of about 2.5 MiB; the average Cluster size of B is 1.1 MiB;
for C it is 2.35 MiB, for D it is 0.46 MiB; for E - a file with just a
single audio track of 158kb/s resulting in a Cluster size of about 100
kB, the relative gains were the smallest, probably because of the small
Cluster size.)
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2019-12-29 02:01:52 +02:00
if ( keep_buffer ) {
ffio_reset_dyn_buf ( * dyn_cp ) ;
} else {
ffio_free_dyn_buf ( dyn_cp ) ;
}
2020-04-29 20:55:26 +02:00
return ret ;
2016-10-03 04:46:19 +02:00
}
2017-01-05 02:24:03 +02:00
/**
2020-04-29 06:31:07 +02:00
* Output EBML master . Keep the buffer if seekable , allowing for later updates .
* Furthermore always add a SeekHead Entry for this element .
*/
2020-04-29 20:55:26 +02:00
static int end_ebml_master_crc32_tentatively ( AVIOContext * pb ,
2020-04-29 07:24:02 +02:00
ebml_stored_master * elem ,
MatroskaMuxContext * mkv , uint32_t id )
2017-01-05 02:24:03 +02:00
{
2020-04-29 07:24:02 +02:00
if ( IS_SEEKABLE ( pb , mkv ) ) {
uint8_t * buf ;
2020-04-29 06:31:07 +02:00
int size = avio_get_dyn_buf ( elem - > bc , & buf ) ;
2017-01-05 02:24:03 +02:00
2020-04-29 20:55:26 +02:00
if ( elem - > bc - > error < 0 )
return elem - > bc - > error ;
2020-04-29 06:31:07 +02:00
elem - > pos = avio_tell ( pb ) ;
mkv_add_seekhead_entry ( mkv , id , elem - > pos ) ;
2019-04-20 01:41:10 +02:00
2020-04-29 07:24:02 +02:00
put_ebml_id ( pb , id ) ;
put_ebml_length ( pb , size , 0 ) ;
avio_write ( pb , buf , size ) ;
2020-04-29 20:55:26 +02:00
return 0 ;
2020-04-29 06:31:07 +02:00
} else
2020-04-29 20:55:26 +02:00
return end_ebml_master_crc32 ( pb , & elem - > bc , mkv , id , 0 , 0 , 1 ) ;
2017-01-05 02:24:03 +02:00
}
2011-02-20 12:04:12 +02:00
static void put_xiph_size ( AVIOContext * pb , int size )
2007-09-05 03:24:06 +03:00
{
2013-07-15 23:20:57 +03:00
ffio_fill ( pb , 255 , size / 255 ) ;
2011-02-21 20:28:17 +02:00
avio_w8 ( pb , size % 255 ) ;
2007-09-05 03:24:06 +03:00
}
2015-08-19 10:06:56 +02:00
/**
* Free the members allocated in the mux context .
*/
2019-10-16 03:57:08 +02:00
static void mkv_deinit ( AVFormatContext * s )
{
MatroskaMuxContext * mkv = s - > priv_data ;
2019-10-16 03:57:09 +02:00
ffio_free_dyn_buf ( & mkv - > cluster_bc ) ;
2020-04-29 06:31:07 +02:00
ffio_free_dyn_buf ( & mkv - > info . bc ) ;
ffio_free_dyn_buf ( & mkv - > track . bc ) ;
ffio_free_dyn_buf ( & mkv - > tags . bc ) ;
2022-06-15 11:25:12 +02:00
ffio_free_dyn_buf ( & mkv - > tmp_bc ) ;
2019-10-16 03:57:09 +02:00
2022-01-16 15:01:28 +02:00
av_freep ( & mkv - > cur_block . h2645_nalu_list . nalus ) ;
2019-12-30 16:37:15 +02:00
av_freep ( & mkv - > cues . entries ) ;
2015-08-19 10:06:56 +02:00
av_freep ( & mkv - > tracks ) ;
}
2007-09-05 03:24:25 +03:00
/**
2019-12-29 10:38:44 +02:00
* Initialize the SeekHead element to be ready to index level 1 Matroska
* elements . Enough space to write MAX_SEEKHEAD_ENTRIES SeekHead entries
* will be reserved at the current file location .
2007-09-05 03:24:25 +03:00
*/
2019-12-29 10:38:44 +02:00
static void mkv_start_seekhead ( MatroskaMuxContext * mkv , AVIOContext * pb )
2007-09-05 03:23:19 +03:00
{
2019-12-29 10:38:44 +02:00
mkv - > seekhead . filepos = avio_tell ( pb ) ;
// 21 bytes max for a Seek entry, 6 bytes max for the SeekHead ID
// and size, 6 bytes for a CRC32 element, and 2 bytes to guarantee
// that an EBML void element will fit afterwards
mkv - > seekhead . reserved_size = MAX_SEEKHEAD_ENTRIES * MAX_SEEKENTRY_SIZE + 14 ;
put_ebml_void ( pb , mkv - > seekhead . reserved_size ) ;
2007-09-05 03:23:19 +03:00
}
2007-09-05 03:24:25 +03:00
/**
2019-12-29 10:57:44 +02:00
* Write the SeekHead to the file at the location reserved for it
2019-12-29 13:50:46 +02:00
* and seek to destpos afterwards . When error_on_seek_failure
2019-12-29 10:57:44 +02:00
* is not set , failure to seek to the position designated for the
2019-12-29 13:50:46 +02:00
* SeekHead is not considered an error and it is presumed that
* destpos is the current position ; failure to seek to destpos
* afterwards is always an error .
2007-09-05 03:24:25 +03:00
*
2019-12-29 10:57:44 +02:00
* @ return 0 on success , < 0 on error .
2007-09-05 03:24:25 +03:00
*/
2019-12-29 10:57:44 +02:00
static int mkv_write_seekhead ( AVIOContext * pb , MatroskaMuxContext * mkv ,
2019-12-29 13:50:46 +02:00
int error_on_seek_failure , int64_t destpos )
2007-09-05 03:23:19 +03:00
{
avformat/matroskaenc: Stop reallocating of Cluster buffer
The Matroska muxer uses a dynamic buffer to buffer the content of
Clusters before eventually writing them. Up until now, each time a
Cluster was written, the dynamic buffer was closed, i.e. freed; now it
is only reset, saving allocations of the AVIOContext itself, its opaque
as well as most of the reallocations of the buffer.
This is advantageous performance-wise, in particular on systems where
reallocations are slow (namely Windows). The following table shows the
decicyles for writing a frame on Linux (Ubuntu 19.10) and Windows (7)
on an x64 Haswell (to /dev/null on Linux, to stdout which is discarded
on Windows (the default values of the size and duration of clusters for
seekable output have been explicitly set in this case); in all tests,
writing CRC-32 values has been disabled in all tests; calls to the muxer's
write_packet function in write_packet() in libavformat/mux.c have been
timed; each of the following tests has been repeated 50 times):
| Windows before | Windows after | Linux before | Linux after
_________________________________________________________________
A | 979437 | 192304 | 259500 | 183320
B | 715936 | 155648 | 152786 | 130879
C | 265115 | 56034 | 78496 | 53243
D | 386224 | 80307 | 128894 | 75354
E | 21732 | 10695 | 11320 | 9801
(A is a 10.2 mb/s file with a GOP length of 2s, amounting to an average
Cluster size of about 2.5 MiB; the average Cluster size of B is 1.1 MiB;
for C it is 2.35 MiB, for D it is 0.46 MiB; for E - a file with just a
single audio track of 158kb/s resulting in a Cluster size of about 100
kB, the relative gains were the smallest, probably because of the small
Cluster size.)
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2019-12-29 02:01:52 +02:00
AVIOContext * dyn_cp = NULL ;
2019-12-29 10:38:44 +02:00
mkv_seekhead * seekhead = & mkv - > seekhead ;
2019-12-29 13:50:46 +02:00
int64_t remaining , ret64 ;
2019-12-29 10:57:44 +02:00
int i , ret ;
2007-09-05 03:23:19 +03:00
2019-12-29 10:57:44 +02:00
if ( ( ret64 = avio_seek ( pb , seekhead - > filepos , SEEK_SET ) ) < 0 )
return error_on_seek_failure ? ret64 : 0 ;
2007-09-05 03:23:19 +03:00
2019-12-30 16:59:02 +02:00
ret = start_ebml_master_crc32 ( & dyn_cp , mkv ) ;
2019-12-29 10:57:44 +02:00
if ( ret < 0 )
return ret ;
2016-10-03 23:59:42 +02:00
2007-09-05 03:23:19 +03:00
for ( i = 0 ; i < seekhead - > num_entries ; i + + ) {
mkv_seekhead_entry * entry = & seekhead - > entries [ i ] ;
2019-12-31 14:21:31 +02:00
ebml_master seekentry = start_ebml_master ( dyn_cp , MATROSKA_ID_SEEKENTRY ,
MAX_SEEKENTRY_SIZE ) ;
2007-09-05 03:23:19 +03:00
2016-10-03 23:59:42 +02:00
put_ebml_id ( dyn_cp , MATROSKA_ID_SEEKID ) ;
2020-04-15 03:39:37 +02:00
put_ebml_length ( dyn_cp , ebml_id_size ( entry - > elementid ) , 0 ) ;
2016-10-03 23:59:42 +02:00
put_ebml_id ( dyn_cp , entry - > elementid ) ;
2007-09-05 03:23:19 +03:00
2016-10-03 23:59:42 +02:00
put_ebml_uint ( dyn_cp , MATROSKA_ID_SEEKPOSITION , entry - > segmentpos ) ;
end_ebml_master ( dyn_cp , seekentry ) ;
2007-09-05 03:23:19 +03:00
}
2020-04-29 20:55:26 +02:00
ret = end_ebml_master_crc32 ( pb , & dyn_cp , mkv ,
MATROSKA_ID_SEEKHEAD , 0 , 0 , 0 ) ;
if ( ret < 0 )
return ret ;
2007-09-05 03:23:19 +03:00
2019-12-29 10:38:44 +02:00
remaining = seekhead - > filepos + seekhead - > reserved_size - avio_tell ( pb ) ;
2019-12-31 14:21:31 +02:00
put_ebml_void ( pb , remaining ) ;
2019-12-29 10:38:44 +02:00
2019-12-29 13:50:46 +02:00
if ( ( ret64 = avio_seek ( pb , destpos , SEEK_SET ) ) < 0 )
2019-12-29 10:57:44 +02:00
return ret64 ;
2007-09-05 03:23:19 +03:00
2019-12-29 10:57:44 +02:00
return 0 ;
2007-09-05 03:23:19 +03:00
}
2019-12-31 23:05:57 +02:00
static int mkv_add_cuepoint ( MatroskaMuxContext * mkv , int stream , int64_t ts ,
2014-05-07 19:46:40 +03:00
int64_t cluster_pos , int64_t relative_pos , int64_t duration )
2007-09-05 03:23:27 +03:00
{
2019-12-30 16:37:15 +02:00
mkv_cues * cues = & mkv - > cues ;
2007-09-05 03:23:27 +03:00
mkv_cuepoint * entries = cues - > entries ;
2021-11-27 15:15:45 +02:00
unsigned idx = cues - > num_entries ;
2007-09-05 03:23:27 +03:00
2012-10-09 15:54:01 +03:00
if ( ts < 0 )
return 0 ;
2013-09-11 14:34:25 +03:00
entries = av_realloc_array ( entries , cues - > num_entries + 1 , sizeof ( mkv_cuepoint ) ) ;
2014-08-15 21:33:21 +03:00
if ( ! entries )
2013-09-11 13:13:44 +03:00
return AVERROR ( ENOMEM ) ;
2013-09-12 16:12:52 +03:00
cues - > entries = entries ;
2007-09-05 03:23:27 +03:00
2021-11-27 15:15:45 +02:00
/* Make sure the cues entries are sorted by pts. */
while ( idx > 0 & & entries [ idx - 1 ] . pts > ts )
idx - - ;
memmove ( & entries [ idx + 1 ] , & entries [ idx ] ,
( cues - > num_entries - idx ) * sizeof ( entries [ 0 ] ) ) ;
entries [ idx ] . pts = ts ;
entries [ idx ] . stream_idx = stream ;
entries [ idx ] . cluster_pos = cluster_pos - mkv - > segment_offset ;
entries [ idx ] . relative_pos = relative_pos ;
entries [ idx ] . duration = duration ;
cues - > num_entries + + ;
2007-09-05 03:23:27 +03:00
return 0 ;
}
2019-12-30 17:28:39 +02:00
static int mkv_assemble_cues ( AVStream * * streams , AVIOContext * dyn_cp ,
2022-01-09 20:45:58 +02:00
const mkv_cues * cues , mkv_track * tracks , int num_tracks ,
uint64_t offset )
2007-09-05 03:23:27 +03:00
{
2019-12-30 17:28:39 +02:00
AVIOContext * cuepoint ;
2019-12-30 16:48:35 +02:00
int ret ;
2007-09-05 03:23:27 +03:00
2019-12-30 16:48:35 +02:00
ret = avio_open_dyn_buf ( & cuepoint ) ;
2019-12-30 17:28:39 +02:00
if ( ret < 0 )
2019-12-30 16:48:35 +02:00
return ret ;
2019-10-15 13:17:34 +02:00
2019-12-30 16:48:35 +02:00
for ( mkv_cuepoint * entry = cues - > entries , * end = entry + cues - > num_entries ;
entry < end ; ) {
uint64_t pts = entry - > pts ;
uint8_t * buf ;
int size ;
2014-08-09 05:49:09 +03:00
2019-12-30 16:48:35 +02:00
put_ebml_uint ( cuepoint , MATROSKA_ID_CUETIME , pts ) ;
2007-09-05 03:23:27 +03:00
// put all the entries from different tracks that have the exact same
// timestamp into the same CuePoint
2019-12-30 16:48:35 +02:00
for ( int j = 0 ; j < num_tracks ; j + + )
2013-02-10 23:32:37 +03:00
tracks [ j ] . has_cue = 0 ;
2019-12-30 16:48:35 +02:00
do {
ebml_master track_positions ;
int idx = entry - > stream_idx ;
2019-10-15 13:17:32 +02:00
2019-12-30 16:48:35 +02:00
av_assert0 ( idx > = 0 & & idx < num_tracks ) ;
2019-12-30 17:28:39 +02:00
if ( tracks [ idx ] . has_cue & & streams [ idx ] - > codecpar - > codec_type ! = AVMEDIA_TYPE_SUBTITLE )
2013-02-10 23:32:37 +03:00
continue ;
2019-10-15 13:17:34 +02:00
tracks [ idx ] . has_cue = 1 ;
2019-12-30 16:48:35 +02:00
track_positions = start_ebml_master ( cuepoint , MATROSKA_ID_CUETRACKPOSITION , MAX_CUETRACKPOS_SIZE ) ;
2019-12-31 23:05:57 +02:00
put_ebml_uint ( cuepoint , MATROSKA_ID_CUETRACK , tracks [ idx ] . track_num ) ;
2022-01-09 20:45:58 +02:00
put_ebml_uint ( cuepoint , MATROSKA_ID_CUECLUSTERPOSITION , entry - > cluster_pos + offset ) ;
2019-12-30 16:48:35 +02:00
put_ebml_uint ( cuepoint , MATROSKA_ID_CUERELATIVEPOSITION , entry - > relative_pos ) ;
2021-04-09 01:00:51 +02:00
if ( entry - > duration > 0 )
2019-12-30 16:48:35 +02:00
put_ebml_uint ( cuepoint , MATROSKA_ID_CUEDURATION , entry - > duration ) ;
end_ebml_master ( cuepoint , track_positions ) ;
} while ( + + entry < end & & entry - > pts = = pts ) ;
size = avio_get_dyn_buf ( cuepoint , & buf ) ;
2020-04-29 20:55:26 +02:00
if ( ( ret = cuepoint - > error ) < 0 )
break ;
2019-12-30 16:48:35 +02:00
put_ebml_binary ( dyn_cp , MATROSKA_ID_POINTENTRY , buf , size ) ;
ffio_reset_dyn_buf ( cuepoint ) ;
2007-09-05 03:23:27 +03:00
}
2019-12-30 16:48:35 +02:00
ffio_free_dyn_buf ( & cuepoint ) ;
2007-09-05 03:23:27 +03:00
2020-04-29 20:55:26 +02:00
return ret ;
2007-09-05 03:23:27 +03:00
}
2019-11-20 04:35:24 +02:00
static int put_xiph_codecpriv ( AVFormatContext * s , AVIOContext * pb ,
2022-06-17 05:12:47 +02:00
const AVCodecParameters * par ,
const uint8_t * extradata , int extradata_size )
2007-09-05 03:23:13 +03:00
{
2014-12-12 15:46:19 +02:00
const uint8_t * header_start [ 3 ] ;
2007-09-05 03:23:13 +03:00
int header_len [ 3 ] ;
int first_header_size ;
2020-05-17 17:30:19 +02:00
int err , j ;
2007-09-05 03:23:13 +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 ( par - > codec_id = = AV_CODEC_ID_VORBIS )
2007-09-05 03:23:13 +03:00
first_header_size = 30 ;
else
first_header_size = 42 ;
2022-06-17 05:12:47 +02:00
err = avpriv_split_xiph_headers ( extradata , extradata_size ,
2020-05-17 17:30:19 +02:00
first_header_size , header_start , header_len ) ;
if ( err < 0 ) {
2007-09-05 03:26:09 +03:00
av_log ( s , AV_LOG_ERROR , " Extradata corrupt. \n " ) ;
2020-05-17 17:30:19 +02:00
return err ;
2007-09-05 03:23:13 +03:00
}
2011-02-21 20:28:17 +02:00
avio_w8 ( pb , 2 ) ; // number packets - 1
2007-09-05 03:23:13 +03:00
for ( j = 0 ; j < 2 ; j + + ) {
2007-09-05 03:24:06 +03:00
put_xiph_size ( pb , header_len [ j ] ) ;
2007-09-05 03:23:13 +03:00
}
for ( j = 0 ; j < 3 ; j + + )
2011-02-21 20:28:17 +02:00
avio_write ( pb , header_start [ j ] , header_len [ j ] ) ;
2007-09-05 03:23:17 +03:00
return 0 ;
2007-09-05 03:23:13 +03:00
}
2007-09-05 03:22:37 +03:00
2022-01-07 12:36:19 +02:00
# if CONFIG_MATROSKA_MUXER
2022-06-17 05:12:47 +02:00
static int put_wv_codecpriv ( AVIOContext * pb , const uint8_t * extradata , int extradata_size )
2013-05-28 11:09:21 +03:00
{
2022-06-17 05:12:47 +02:00
if ( extradata & & extradata_size = = 2 )
avio_write ( pb , extradata , 2 ) ;
2013-05-28 11:09:21 +03:00
else
2019-12-14 03:47:13 +02:00
avio_wl16 ( pb , 0x410 ) ; // fallback to the most recent version
2013-05-28 11:09:21 +03:00
return 0 ;
}
2019-12-29 04:36:56 +02:00
static int put_flac_codecpriv ( AVFormatContext * s , AVIOContext * pb ,
2022-06-17 05:12:47 +02:00
const AVCodecParameters * par ,
const uint8_t * extradata , int extradata_size )
2014-05-26 14:53:13 +03:00
{
2017-03-31 16:57:00 +02:00
int write_comment = ( par - > ch_layout . order = = AV_CHANNEL_ORDER_NATIVE & &
! ( par - > ch_layout . u . mask & ~ 0x3ffffULL ) & &
! ff_flac_is_native_layout ( par - > ch_layout . u . mask ) ) ;
2022-06-17 05:12:47 +02:00
int ret = ff_flac_write_header ( pb , extradata , extradata_size ,
2014-05-26 23:14:14 +03:00
! write_comment ) ;
2014-05-26 14:53:13 +03:00
if ( ret < 0 )
return ret ;
if ( write_comment ) {
const char * vendor = ( s - > flags & AVFMT_FLAG_BITEXACT ) ?
2014-05-28 16:15:14 +03:00
" Lavf " : LIBAVFORMAT_IDENT ;
2014-05-26 14:53:13 +03:00
AVDictionary * dict = NULL ;
2020-04-28 04:54:05 +02:00
uint8_t buf [ 32 ] ;
2015-05-11 15:37:38 +02:00
int64_t len ;
2014-05-26 14:53:13 +03:00
2017-03-31 16:57:00 +02:00
snprintf ( buf , sizeof ( buf ) , " 0x% " PRIx64 , par - > ch_layout . u . mask ) ;
2014-05-26 14:53:13 +03:00
av_dict_set ( & dict , " WAVEFORMATEXTENSIBLE_CHANNEL_MASK " , buf , 0 ) ;
2018-12-16 22:50:08 +02:00
len = ff_vorbiscomment_length ( dict , vendor , NULL , 0 ) ;
2020-04-28 02:24:16 +02:00
av_assert1 ( len < ( 1 < < 24 ) - 4 ) ;
2015-05-11 15:37:38 +02:00
2020-04-28 04:54:05 +02:00
avio_w8 ( pb , 0x84 ) ;
avio_wb24 ( pb , len ) ;
2014-05-26 14:53:13 +03:00
2020-04-28 04:54:05 +02:00
ff_vorbiscomment_write ( pb , dict , vendor , NULL , 0 ) ;
2014-05-26 14:53:13 +03:00
av_dict_free ( & dict ) ;
}
return 0 ;
}
avformat/matroskaenc: Remove inconsistencies wrt seekability handling
The Matroska muxer behaves differently in several ways when it thinks
that it is in unseekable/livestreaming mode: It does not add Cue entries
because they won't be written anyway for a livestream and it writes some
elements only preliminarily (with the intention to overwrite them with
an updated version at the end) when non-livestreaming etc.
There are two ways to set the Matroska muxer into livestreaming mode:
Setting an option or by providing an unseekable AVIOContext. Yet the
actual checks were not consistent:
If the AVIOContext was unseekable and no AAC extradata was available
when writing the header, writing the header failed; but if the AVIOContext
was seekable, it didn't, because the muxer expected to get the extradata
via packet side-data. Here the livestreaming option has not been checked,
although one can't use the updated extradata in case it is a livestream.
If the reserve_index_space option was used, space for writing Cues would
be reserved when writing the header unless the AVIOContext was
unseekable. Yet Cues were only written if the livestreaming option was
not set and the AVIOContext was seekable (when writing the trailer), so
if the AVIOContext was seekable and the livestreaming option set, the
reserved space would never be used at all.
If the AVIOContext was unseekable and the livestreaming option was not
set, it would be attempted to update the main length field at the end.
After all, it might be possible that the file is so short that it fits
into the AVIOContext's buffer in which case the seek back would work.
Yet this is dangerous: It might be that we are not dealing with a
simple output file, but that our output gets split into chunks and that
each of these chunks is actually seekable. In this case some part of the
last chunk (namely the eight bytes that have the same offset as the
length field had in the header) will be overwritten with what the muxer
wrongly believes to be the filesize.
(The livestreaming option has been added to deal with this scenario,
yet its documentation ("Write files assuming it is a live stream.")
doesn't make this clear at all. At least the segment muxer does not
set the option for live and given that the chances of successfully
seeking when the output is actually unseekable are slim, it is best to
not attempt to update the length field in the unseekable case at all.)
All these inconsistencies were fixed by treating the output as seekable
if the livestreaming option is not set and if the AVIOContext is
seekable. A macro has been used to enforce consistency and improve code
readability.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-05-01 22:31:36 +02:00
static int get_aac_sample_rates ( AVFormatContext * s , MatroskaMuxContext * mkv ,
const uint8_t * extradata , int extradata_size ,
int * sample_rate , int * output_sample_rate )
2007-09-05 03:23:43 +03:00
{
2011-03-21 10:05:36 +02:00
MPEG4AudioConfig mp4ac ;
2017-04-13 17:43:11 +02:00
int ret ;
2007-09-05 03:23:43 +03:00
2019-09-22 01:06:48 +02:00
ret = avpriv_mpeg4audio_get_config2 ( & mp4ac , extradata , extradata_size , 1 , s ) ;
2017-04-13 17:43:11 +02:00
/* Don't abort if the failure is because of missing extradata. Assume in that
* case a bitstream filter will provide the muxer with the extradata in the
* first packet .
* Abort however if s - > pb is not seekable , as we would not be able to seek back
* to write the sample rate elements once the extradata shows up , anyway . */
avformat/matroskaenc: Remove inconsistencies wrt seekability handling
The Matroska muxer behaves differently in several ways when it thinks
that it is in unseekable/livestreaming mode: It does not add Cue entries
because they won't be written anyway for a livestream and it writes some
elements only preliminarily (with the intention to overwrite them with
an updated version at the end) when non-livestreaming etc.
There are two ways to set the Matroska muxer into livestreaming mode:
Setting an option or by providing an unseekable AVIOContext. Yet the
actual checks were not consistent:
If the AVIOContext was unseekable and no AAC extradata was available
when writing the header, writing the header failed; but if the AVIOContext
was seekable, it didn't, because the muxer expected to get the extradata
via packet side-data. Here the livestreaming option has not been checked,
although one can't use the updated extradata in case it is a livestream.
If the reserve_index_space option was used, space for writing Cues would
be reserved when writing the header unless the AVIOContext was
unseekable. Yet Cues were only written if the livestreaming option was
not set and the AVIOContext was seekable (when writing the trailer), so
if the AVIOContext was seekable and the livestreaming option set, the
reserved space would never be used at all.
If the AVIOContext was unseekable and the livestreaming option was not
set, it would be attempted to update the main length field at the end.
After all, it might be possible that the file is so short that it fits
into the AVIOContext's buffer in which case the seek back would work.
Yet this is dangerous: It might be that we are not dealing with a
simple output file, but that our output gets split into chunks and that
each of these chunks is actually seekable. In this case some part of the
last chunk (namely the eight bytes that have the same offset as the
length field had in the header) will be overwritten with what the muxer
wrongly believes to be the filesize.
(The livestreaming option has been added to deal with this scenario,
yet its documentation ("Write files assuming it is a live stream.")
doesn't make this clear at all. At least the segment muxer does not
set the option for live and given that the chances of successfully
seeking when the output is actually unseekable are slim, it is best to
not attempt to update the length field in the unseekable case at all.)
All these inconsistencies were fixed by treating the output as seekable
if the livestreaming option is not set and if the AVIOContext is
seekable. A macro has been used to enforce consistency and improve code
readability.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-05-01 22:31:36 +02:00
if ( ret < 0 & & ( extradata_size | | ! IS_SEEKABLE ( s - > pb , mkv ) ) ) {
2015-01-01 19:22:46 +02:00
av_log ( s , AV_LOG_ERROR ,
2014-06-07 01:54:50 +03:00
" Error parsing AAC extradata, unable to determine samplerate. \n " ) ;
2015-01-01 19:22:46 +02:00
return AVERROR ( EINVAL ) ;
2007-09-05 03:23:43 +03:00
}
2017-04-13 17:43:11 +02:00
if ( ret < 0 ) {
/* This will only happen when this function is called while writing the
* header and no extradata is available . The space for this element has
* to be reserved for when this function is called again after the
* extradata shows up in the first packet , as there ' s no way to know if
* output_sample_rate will be different than sample_rate or not . */
* output_sample_rate = * sample_rate ;
} else {
* sample_rate = mp4ac . sample_rate ;
* output_sample_rate = mp4ac . ext_sample_rate ;
}
2015-01-01 19:22:46 +02:00
return 0 ;
2007-09-05 03:23:43 +03:00
}
2022-01-07 12:36:19 +02:00
# endif
2007-09-05 03:23:43 +03:00
2022-06-16 14:42:26 +02:00
static int mkv_assemble_native_codecprivate ( AVFormatContext * s , AVIOContext * dyn_cp ,
const AVCodecParameters * par ,
2022-06-17 05:12:47 +02:00
const uint8_t * extradata ,
int extradata_size ,
2022-06-16 14:42:26 +02:00
unsigned * size_to_reserve )
2014-06-07 02:21:13 +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
switch ( par - > codec_id ) {
2014-06-07 02:21:13 +03:00
case AV_CODEC_ID_VORBIS :
case AV_CODEC_ID_THEORA :
2022-06-17 05:12:47 +02:00
return put_xiph_codecpriv ( s , dyn_cp , par , extradata , extradata_size ) ;
2022-01-07 12:36:19 +02:00
case AV_CODEC_ID_AV1 :
2022-06-17 05:12:47 +02:00
if ( extradata_size )
return ff_isom_write_av1c ( dyn_cp , extradata ,
extradata_size , 1 ) ;
2022-01-07 12:36:19 +02:00
else
2022-06-17 05:49:06 +02:00
* size_to_reserve = 4 ;
2022-01-07 12:36:19 +02:00
break ;
# if CONFIG_MATROSKA_MUXER
2014-06-07 02:21:13 +03:00
case AV_CODEC_ID_FLAC :
2022-06-17 05:12:47 +02:00
return put_flac_codecpriv ( s , dyn_cp , par , extradata , extradata_size ) ;
2014-06-07 02:21:13 +03:00
case AV_CODEC_ID_WAVPACK :
2022-06-17 05:12:47 +02:00
return put_wv_codecpriv ( dyn_cp , extradata , extradata_size ) ;
2014-06-07 02:21:13 +03:00
case AV_CODEC_ID_H264 :
2022-06-17 05:12:47 +02:00
return ff_isom_write_avcc ( dyn_cp , extradata ,
extradata_size ) ;
2014-06-07 02:21:13 +03:00
case AV_CODEC_ID_HEVC :
2022-06-17 05:12:47 +02:00
return ff_isom_write_hvcc ( dyn_cp , extradata ,
extradata_size , 0 ) ;
2014-06-07 02:21:13 +03:00
case AV_CODEC_ID_ALAC :
2022-06-17 05:12:47 +02:00
if ( extradata_size < 36 ) {
2014-06-07 02:21:13 +03:00
av_log ( s , AV_LOG_ERROR ,
" Invalid extradata found, ALAC expects a 36-byte "
" QuickTime atom. " ) ;
return AVERROR_INVALIDDATA ;
} else
2022-06-17 05:12:47 +02:00
avio_write ( dyn_cp , extradata + 12 ,
extradata_size - 12 ) ;
2014-06-07 02:21:13 +03:00
break ;
2017-04-13 17:43:11 +02:00
case AV_CODEC_ID_AAC :
2022-06-17 05:12:47 +02:00
if ( extradata_size )
avio_write ( dyn_cp , extradata , extradata_size ) ;
2017-04-13 17:43:11 +02:00
else
2022-06-17 05:49:06 +02:00
* size_to_reserve = MAX_PCE_SIZE ;
2017-04-13 17:43:11 +02:00
break ;
2022-01-07 12:36:19 +02:00
# endif
2014-06-07 02:21:13 +03:00
default :
2022-01-07 12:36:19 +02:00
if ( CONFIG_MATROSKA_MUXER & & par - > codec_id = = AV_CODEC_ID_PRORES & &
2016-04-10 21:58:15 +02:00
ff_codec_get_id ( ff_codec_movvideo_tags , par - > codec_tag ) = = AV_CODEC_ID_PRORES ) {
avio_wl32 ( dyn_cp , par - > codec_tag ) ;
2022-06-17 05:12:47 +02:00
} else if ( extradata_size & & par - > codec_id ! = AV_CODEC_ID_TTA )
avio_write ( dyn_cp , extradata , extradata_size ) ;
2014-06-07 02:21:13 +03:00
}
return 0 ;
}
2022-06-16 14:42:26 +02:00
static int mkv_assemble_codecprivate ( AVFormatContext * s , AVIOContext * dyn_cp ,
AVCodecParameters * par ,
2022-06-17 05:12:47 +02:00
const uint8_t * extradata , int extradata_size ,
2022-06-16 14:42:26 +02:00
int native_id , int qt_id ,
uint8_t * * codecpriv , int * codecpriv_size ,
2022-06-17 05:49:06 +02:00
unsigned * max_payload_size )
2007-09-05 03:25:49 +03:00
{
2022-01-07 12:36:19 +02:00
MatroskaMuxContext av_unused * const mkv = s - > priv_data ;
2022-06-17 05:49:06 +02:00
unsigned size_to_reserve = 0 ;
2022-06-16 14:42:26 +02:00
int ret ;
2007-09-05 03:25:52 +03:00
2007-09-05 03:25:49 +03:00
if ( native_id ) {
2022-06-17 05:12:47 +02:00
ret = mkv_assemble_native_codecprivate ( s , dyn_cp , par ,
extradata , extradata_size ,
2022-06-17 05:49:06 +02:00
& size_to_reserve ) ;
2022-06-16 14:42:26 +02:00
if ( ret < 0 )
return ret ;
2022-01-07 12:36:19 +02:00
# if CONFIG_MATROSKA_MUXER
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
} else if ( par - > codec_type = = AVMEDIA_TYPE_VIDEO ) {
2008-07-27 18:11:04 +03:00
if ( qt_id ) {
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 ( ! par - > codec_tag )
par - > codec_tag = ff_codec_get_tag ( ff_codec_movvideo_tags ,
2016-04-10 21:58:15 +02:00
par - > codec_id ) ;
2016-11-01 14:42:27 +02:00
if ( ff_codec_get_id ( ff_codec_movvideo_tags , par - > codec_tag ) = = par - > codec_id
2022-06-17 05:12:47 +02:00
& & ( ! extradata_size | | ff_codec_get_id ( ff_codec_movvideo_tags , AV_RL32 ( extradata + 4 ) ) ! = par - > codec_id )
2016-11-01 14:42:27 +02:00
) {
2022-06-17 05:12:47 +02:00
avio_wb32 ( dyn_cp , 0x5a + extradata_size ) ;
2016-11-01 14:42:27 +02:00
avio_wl32 ( dyn_cp , par - > codec_tag ) ;
2021-09-22 21:55:43 +02:00
ffio_fill ( dyn_cp , 0 , 0x5a - 8 ) ;
2016-11-01 14:42:27 +02:00
}
2022-06-17 05:12:47 +02:00
avio_write ( dyn_cp , extradata , extradata_size ) ;
2008-07-27 18:11:04 +03:00
} else {
2016-04-10 21:58:15 +02:00
if ( ! ff_codec_get_tag ( ff_codec_bmp_tags , par - > codec_id ) )
2014-01-05 04:04:29 +03:00
av_log ( s , AV_LOG_WARNING , " codec %s is not supported by this format \n " ,
2016-04-10 21:58:15 +02:00
avcodec_get_name ( par - > codec_id ) ) ;
2014-01-05 04:04: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 ( ! par - > codec_tag )
par - > codec_tag = ff_codec_get_tag ( ff_codec_bmp_tags ,
par - > codec_id ) ;
2016-04-10 21:58:15 +02:00
if ( ! par - > codec_tag & & par - > codec_id ! = AV_CODEC_ID_RAWVIDEO ) {
2013-02-10 20:02:59 +03:00
av_log ( s , AV_LOG_ERROR , " No bmp codec tag found for codec %s \n " ,
2016-04-10 21:58:15 +02:00
avcodec_get_name ( par - > codec_id ) ) ;
2022-06-16 14:42:26 +02:00
return AVERROR ( EINVAL ) ;
2010-03-04 10:53:11 +02:00
}
2007-09-05 03:25:49 +03:00
2022-06-17 05:12:47 +02:00
/* If vfw extradata updates are supported, this will have
* to be updated to pass extradata ( _size ) explicitly . */
2020-07-08 14:02:25 +02:00
ff_put_bmp_header ( dyn_cp , par , 0 , 0 , mkv - > flipped_raw_rgb ) ;
2008-07-27 18:11:04 +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
} else if ( par - > codec_type = = AVMEDIA_TYPE_AUDIO ) {
2010-03-25 16:34:19 +02:00
unsigned int tag ;
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
tag = ff_codec_get_tag ( ff_codec_wav_tags , par - > codec_id ) ;
2010-03-25 16:34:19 +02:00
if ( ! tag ) {
2013-02-10 20:02:59 +03:00
av_log ( s , AV_LOG_ERROR , " No wav codec tag found for codec %s \n " ,
2016-04-10 21:58:15 +02:00
avcodec_get_name ( par - > codec_id ) ) ;
2022-06-16 14:42:26 +02:00
return AVERROR ( EINVAL ) ;
2007-09-05 03:25:49 +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 ( ! par - > codec_tag )
par - > codec_tag = tag ;
2007-09-05 03:25:49 +03:00
2022-06-17 05:12:47 +02:00
/* Same comment as for ff_put_bmp_header applies here. */
2016-04-10 21:58:15 +02:00
ff_put_wav_header ( s , dyn_cp , par , FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX ) ;
2022-01-07 12:36:19 +02:00
# endif
2007-09-05 03:25:49 +03:00
}
2007-09-05 03:25:52 +03:00
2022-06-16 14:42:26 +02:00
* codecpriv_size = avio_get_dyn_buf ( dyn_cp , codecpriv ) ;
if ( dyn_cp - > error < 0 )
return dyn_cp - > error ;
2022-06-17 05:49:06 +02:00
* max_payload_size = * codecpriv_size + size_to_reserve ;
2022-06-16 14:42:26 +02:00
return 0 ;
}
2022-06-17 05:49:06 +02:00
static void mkv_put_codecprivate ( AVIOContext * pb , unsigned max_payload_size ,
const uint8_t * codecpriv , unsigned codecpriv_size )
{
unsigned total_codecpriv_size = 0 , total_size ;
av_assert1 ( codecpriv_size < = max_payload_size ) ;
if ( ! max_payload_size )
return ;
total_size = 2 + ebml_length_size ( max_payload_size ) + max_payload_size ;
if ( codecpriv_size ) {
unsigned length_size = ebml_length_size ( codecpriv_size ) ;
total_codecpriv_size = 2U + length_size + codecpriv_size ;
if ( total_codecpriv_size + 1 = = total_size ) {
/* It is impossible to add one byte of padding via an EBML Void. */
length_size + + ;
total_codecpriv_size + + ;
}
put_ebml_id ( pb , MATROSKA_ID_CODECPRIVATE ) ;
put_ebml_length ( pb , codecpriv_size , length_size ) ;
avio_write ( pb , codecpriv , codecpriv_size ) ;
}
if ( total_codecpriv_size < total_size )
put_ebml_void ( pb , total_size - total_codecpriv_size ) ;
}
static int mkv_update_codecprivate ( AVFormatContext * s , MatroskaMuxContext * mkv ,
uint8_t * side_data , int side_data_size ,
AVCodecParameters * par , AVIOContext * pb ,
mkv_track * track )
2022-06-16 14:42:26 +02:00
{
AVIOContext * const dyn_bc = mkv - > tmp_bc ;
uint8_t * codecpriv ;
2022-06-17 05:49:06 +02:00
unsigned max_payload_size ;
2022-06-16 14:42:26 +02:00
int ret , codecpriv_size ;
2022-06-17 05:49:06 +02:00
ret = mkv_assemble_codecprivate ( s , dyn_bc , par ,
side_data , side_data_size , 1 , 0 ,
& codecpriv , & codecpriv_size , & max_payload_size ) ;
2022-06-16 14:42:26 +02:00
if ( ret < 0 )
2022-06-17 05:49:06 +02:00
goto fail ;
if ( codecpriv_size > track - > codecpriv_size ) {
ret = AVERROR ( ENOSPC ) ;
goto fail ;
}
avio_seek ( pb , track - > codecpriv_offset , SEEK_SET ) ;
mkv_put_codecprivate ( pb , track - > codecpriv_size ,
codecpriv , codecpriv_size ) ;
if ( ! par - > extradata_size ) {
ret = ff_alloc_extradata ( par , side_data_size ) ;
if ( ret < 0 )
goto fail ;
memcpy ( par - > extradata , side_data , side_data_size ) ;
}
fail :
2022-06-16 14:42:26 +02:00
ffio_reset_dyn_buf ( dyn_bc ) ;
2022-06-17 05:49:06 +02:00
return ret ;
2007-09-05 03:25:49 +03:00
}
2022-01-14 14:56:52 +02:00
# define MAX_VIDEO_COLOR_ELEMS 20
static void mkv_write_video_color ( EbmlWriter * writer , const AVStream * st ,
2020-04-29 07:24:02 +02:00
const AVCodecParameters * par )
2019-11-20 04:35:24 +02:00
{
2020-05-21 23:57:19 +02:00
const void * side_data ;
2016-09-28 00:20:57 +02:00
2022-01-14 14:56:52 +02:00
ebml_writer_open_master ( writer , MATROSKA_ID_VIDEOCOLOR ) ;
2016-03-08 00:42:47 +02:00
2016-04-10 21:58:15 +02:00
if ( par - > color_trc ! = AVCOL_TRC_UNSPECIFIED & &
par - > color_trc < AVCOL_TRC_NB ) {
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( writer , MATROSKA_ID_VIDEOCOLORTRANSFERCHARACTERISTICS ,
par - > color_trc ) ;
2016-03-08 00:42:47 +02:00
}
2016-04-10 21:58:15 +02:00
if ( par - > color_space ! = AVCOL_SPC_UNSPECIFIED & &
par - > color_space < AVCOL_SPC_NB ) {
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( writer , MATROSKA_ID_VIDEOCOLORMATRIXCOEFF ,
par - > color_space ) ;
2016-03-08 00:42:47 +02:00
}
2016-04-10 21:58:15 +02:00
if ( par - > color_primaries ! = AVCOL_PRI_UNSPECIFIED & &
par - > color_primaries < AVCOL_PRI_NB ) {
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( writer , MATROSKA_ID_VIDEOCOLORPRIMARIES ,
par - > color_primaries ) ;
2016-03-08 00:42:47 +02:00
}
2016-04-10 21:58:15 +02:00
if ( par - > color_range ! = AVCOL_RANGE_UNSPECIFIED & &
par - > color_range < AVCOL_RANGE_NB ) {
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( writer , MATROSKA_ID_VIDEOCOLORRANGE , par - > color_range ) ;
2016-03-08 00:42:47 +02:00
}
2016-10-15 16:17:37 +02:00
if ( par - > chroma_location ! = AVCHROMA_LOC_UNSPECIFIED & &
par - > chroma_location < = AVCHROMA_LOC_TOP ) {
int xpos , ypos ;
avcodec_enum_to_chroma_pos ( & xpos , & ypos , par - > chroma_location ) ;
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( writer , MATROSKA_ID_VIDEOCOLORCHROMASITINGHORZ ,
( xpos > > 7 ) + 1 ) ;
ebml_writer_add_uint ( writer , MATROSKA_ID_VIDEOCOLORCHROMASITINGVERT ,
( ypos > > 7 ) + 1 ) ;
2016-10-15 16:17:37 +02:00
}
2017-04-25 20:42:51 +02:00
side_data = av_stream_get_side_data ( st , AV_PKT_DATA_CONTENT_LIGHT_LEVEL ,
2020-05-21 23:10:51 +02:00
NULL ) ;
if ( side_data ) {
2020-05-21 23:57:19 +02:00
const AVContentLightMetadata * metadata = side_data ;
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( writer , MATROSKA_ID_VIDEOCOLORMAXCLL ,
metadata - > MaxCLL ) ;
ebml_writer_add_uint ( writer , MATROSKA_ID_VIDEOCOLORMAXFALL ,
metadata - > MaxFALL ) ;
2017-04-25 20:42:51 +02:00
}
side_data = av_stream_get_side_data ( st , AV_PKT_DATA_MASTERING_DISPLAY_METADATA ,
2020-05-21 23:10:51 +02:00
NULL ) ;
if ( side_data ) {
2020-05-21 23:57:19 +02:00
const AVMasteringDisplayMetadata * metadata = side_data ;
2022-01-14 14:56:52 +02:00
ebml_writer_open_master ( writer , MATROSKA_ID_VIDEOCOLORMASTERINGMETA ) ;
2016-03-08 00:42:47 +02:00
if ( metadata - > has_primaries ) {
2022-01-14 14:56:52 +02:00
ebml_writer_add_float ( writer , MATROSKA_ID_VIDEOCOLOR_RX ,
av_q2d ( metadata - > display_primaries [ 0 ] [ 0 ] ) ) ;
ebml_writer_add_float ( writer , MATROSKA_ID_VIDEOCOLOR_RY ,
av_q2d ( metadata - > display_primaries [ 0 ] [ 1 ] ) ) ;
ebml_writer_add_float ( writer , MATROSKA_ID_VIDEOCOLOR_GX ,
av_q2d ( metadata - > display_primaries [ 1 ] [ 0 ] ) ) ;
ebml_writer_add_float ( writer , MATROSKA_ID_VIDEOCOLOR_GY ,
av_q2d ( metadata - > display_primaries [ 1 ] [ 1 ] ) ) ;
ebml_writer_add_float ( writer , MATROSKA_ID_VIDEOCOLOR_BX ,
av_q2d ( metadata - > display_primaries [ 2 ] [ 0 ] ) ) ;
ebml_writer_add_float ( writer , MATROSKA_ID_VIDEOCOLOR_BY ,
av_q2d ( metadata - > display_primaries [ 2 ] [ 1 ] ) ) ;
ebml_writer_add_float ( writer , MATROSKA_ID_VIDEOCOLOR_WHITEX ,
av_q2d ( metadata - > white_point [ 0 ] ) ) ;
ebml_writer_add_float ( writer , MATROSKA_ID_VIDEOCOLOR_WHITEY ,
av_q2d ( metadata - > white_point [ 1 ] ) ) ;
2016-03-08 00:42:47 +02:00
}
if ( metadata - > has_luminance ) {
2022-01-14 14:56:52 +02:00
ebml_writer_add_float ( writer , MATROSKA_ID_VIDEOCOLOR_LUMINANCEMAX ,
av_q2d ( metadata - > max_luminance ) ) ;
ebml_writer_add_float ( writer , MATROSKA_ID_VIDEOCOLOR_LUMINANCEMIN ,
av_q2d ( metadata - > min_luminance ) ) ;
2016-03-08 00:42:47 +02:00
}
2022-01-14 14:56:52 +02:00
ebml_writer_close_or_discard_master ( writer ) ;
2016-03-08 00:42:47 +02:00
}
2016-09-28 00:20:57 +02:00
2022-01-14 14:56:52 +02:00
ebml_writer_close_or_discard_master ( writer ) ;
2016-03-08 00:42:47 +02:00
}
2022-01-14 14:56:52 +02:00
# define MAX_VIDEO_PROJECTION_ELEMS 6
static void mkv_write_video_projection ( AVFormatContext * s , EbmlWriter * writer ,
const AVStream * st , uint8_t private [ ] )
2017-03-08 21:12:32 +02:00
{
const AVSphericalMapping * spherical =
2017-03-09 20:11:49 +02:00
( const AVSphericalMapping * ) av_stream_get_side_data ( st , AV_PKT_DATA_SPHERICAL ,
2020-05-21 23:10:51 +02:00
NULL ) ;
2017-03-08 21:12:32 +02:00
2020-05-21 23:10:51 +02:00
if ( ! spherical )
2020-05-06 14:13:58 +02:00
return ;
2017-03-09 20:11:49 +02:00
2020-01-01 02:58:19 +02:00
if ( spherical - > projection ! = AV_SPHERICAL_EQUIRECTANGULAR & &
spherical - > projection ! = AV_SPHERICAL_EQUIRECTANGULAR_TILE & &
spherical - > projection ! = AV_SPHERICAL_CUBEMAP ) {
av_log ( s , AV_LOG_WARNING , " Unknown projection type \n " ) ;
2020-05-06 14:13:58 +02:00
return ;
2020-01-01 02:58:19 +02:00
}
2022-01-14 14:56:52 +02:00
ebml_writer_open_master ( writer , MATROSKA_ID_VIDEOPROJECTION ) ;
2017-03-08 21:12:32 +02:00
2017-03-09 20:11:49 +02:00
switch ( spherical - > projection ) {
case AV_SPHERICAL_EQUIRECTANGULAR :
case AV_SPHERICAL_EQUIRECTANGULAR_TILE :
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( writer , MATROSKA_ID_VIDEOPROJECTIONTYPE ,
MATROSKA_VIDEO_PROJECTION_TYPE_EQUIRECTANGULAR ) ;
2020-01-01 02:58:20 +02:00
AV_WB32 ( private , 0 ) ; // version + flags
2022-01-20 17:33:17 +02:00
if ( spherical - > projection = = AV_SPHERICAL_EQUIRECTANGULAR ) {
AV_WB32 ( private + 4 , 0 ) ;
AV_WB32 ( private + 8 , 0 ) ;
AV_WB32 ( private + 12 , 0 ) ;
AV_WB32 ( private + 16 , 0 ) ;
} else {
AV_WB32 ( private + 4 , spherical - > bound_top ) ;
AV_WB32 ( private + 8 , spherical - > bound_bottom ) ;
AV_WB32 ( private + 12 , spherical - > bound_left ) ;
AV_WB32 ( private + 16 , spherical - > bound_right ) ;
}
2022-01-14 14:56:52 +02:00
ebml_writer_add_bin ( writer , MATROSKA_ID_VIDEOPROJECTIONPRIVATE ,
private , 20 ) ;
2017-03-09 20:11:49 +02:00
break ;
case AV_SPHERICAL_CUBEMAP :
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( writer , MATROSKA_ID_VIDEOPROJECTIONTYPE ,
MATROSKA_VIDEO_PROJECTION_TYPE_CUBEMAP ) ;
2020-01-01 02:58:20 +02:00
AV_WB32 ( private , 0 ) ; // version + flags
AV_WB32 ( private + 4 , 0 ) ; // layout
AV_WB32 ( private + 8 , spherical - > padding ) ;
2022-01-14 14:56:52 +02:00
ebml_writer_add_bin ( writer , MATROSKA_ID_VIDEOPROJECTIONPRIVATE ,
private , 12 ) ;
2017-03-09 20:11:49 +02:00
break ;
default :
2020-01-01 02:58:19 +02:00
av_assert0 ( 0 ) ;
2017-03-09 20:11:49 +02:00
}
if ( spherical - > yaw )
2022-01-14 14:56:52 +02:00
ebml_writer_add_float ( writer , MATROSKA_ID_VIDEOPROJECTIONPOSEYAW ,
( double ) spherical - > yaw / ( 1 < < 16 ) ) ;
2017-03-09 20:11:49 +02:00
if ( spherical - > pitch )
2022-01-14 14:56:52 +02:00
ebml_writer_add_float ( writer , MATROSKA_ID_VIDEOPROJECTIONPOSEPITCH ,
2017-03-09 20:11:49 +02:00
( double ) spherical - > pitch / ( 1 < < 16 ) ) ;
if ( spherical - > roll )
2022-01-14 14:56:52 +02:00
ebml_writer_add_float ( writer , MATROSKA_ID_VIDEOPROJECTIONPOSEROLL ,
2017-03-09 20:11:49 +02:00
( double ) spherical - > roll / ( 1 < < 16 ) ) ;
2017-03-08 21:12:32 +02:00
2022-01-14 14:56:52 +02:00
ebml_writer_close_master ( writer ) ;
2017-03-08 21:12:32 +02:00
}
2022-01-14 14:56:52 +02:00
# define MAX_FIELD_ORDER_ELEMS 2
static void mkv_write_field_order ( EbmlWriter * writer , int is_webm ,
2016-04-02 22:35:44 +02:00
enum AVFieldOrder field_order )
{
switch ( field_order ) {
case AV_FIELD_UNKNOWN :
break ;
case AV_FIELD_PROGRESSIVE :
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( writer , MATROSKA_ID_VIDEOFLAGINTERLACED ,
MATROSKA_VIDEO_INTERLACE_FLAG_PROGRESSIVE ) ;
2016-04-02 22:35:44 +02:00
break ;
case AV_FIELD_TT :
case AV_FIELD_BB :
case AV_FIELD_TB :
case AV_FIELD_BT :
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( writer , MATROSKA_ID_VIDEOFLAGINTERLACED ,
MATROSKA_VIDEO_INTERLACE_FLAG_INTERLACED ) ;
2022-01-07 12:36:19 +02:00
if ( ! is_webm ) {
2016-10-05 00:42:55 +02:00
switch ( field_order ) {
case AV_FIELD_TT :
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( writer , MATROSKA_ID_VIDEOFIELDORDER ,
MATROSKA_VIDEO_FIELDORDER_TT ) ;
2016-10-05 00:42:55 +02:00
break ;
case AV_FIELD_BB :
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( writer , MATROSKA_ID_VIDEOFIELDORDER ,
MATROSKA_VIDEO_FIELDORDER_BB ) ;
2016-10-05 00:42:55 +02:00
break ;
case AV_FIELD_TB :
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( writer , MATROSKA_ID_VIDEOFIELDORDER ,
MATROSKA_VIDEO_FIELDORDER_TB ) ;
2016-10-05 00:42:55 +02:00
break ;
case AV_FIELD_BT :
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( writer , MATROSKA_ID_VIDEOFIELDORDER ,
MATROSKA_VIDEO_FIELDORDER_BT ) ;
2016-10-05 00:42:55 +02:00
break ;
}
2016-04-02 22:35:44 +02:00
}
}
}
2014-08-29 01:30:47 +03:00
2022-01-14 14:56:52 +02:00
# define MAX_STEREO_MODE_ELEMS 1
static int mkv_write_stereo_mode ( AVFormatContext * s , EbmlWriter * writer ,
2022-01-07 12:36:19 +02:00
AVStream * st , int is_webm ,
int * h_width , int * h_height )
2014-06-07 02:38:00 +03:00
{
2019-11-20 04:35:24 +02:00
const AVDictionaryEntry * tag ;
2014-08-05 13:16:17 +03:00
MatroskaVideoStereoModeType format = MATROSKA_VIDEO_STEREOMODE_TYPE_NB ;
2020-05-07 14:13:07 +02:00
const AVStereo3D * stereo ;
2014-08-05 13:16:17 +03:00
2014-10-27 23:18:03 +02:00
* h_width = 1 ;
* h_height = 1 ;
2014-08-05 13:16:17 +03:00
// convert metadata into proper side data and add it to the stream
2014-08-29 01:30:47 +03:00
if ( ( tag = av_dict_get ( st - > metadata , " stereo_mode " , NULL , 0 ) ) | |
( tag = av_dict_get ( s - > metadata , " stereo_mode " , NULL , 0 ) ) ) {
2014-08-05 13:16:17 +03:00
int stereo_mode = atoi ( tag - > value ) ;
2014-08-29 01:30:47 +03:00
2020-05-07 14:19:27 +02:00
for ( int i = 0 ; i < MATROSKA_VIDEO_STEREOMODE_TYPE_NB ; i + + )
2014-08-29 01:30:47 +03:00
if ( ! strcmp ( tag - > value , ff_matroska_video_stereo_mode [ i ] ) ) {
stereo_mode = i ;
break ;
}
2014-08-05 13:16:17 +03:00
if ( stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB & &
stereo_mode ! = 10 & & stereo_mode ! = 12 ) {
int ret = ff_mkv_stereo3d_conv ( st , stereo_mode ) ;
if ( ret < 0 )
return ret ;
}
}
2014-06-07 02:38:00 +03:00
2020-05-07 14:13:07 +02:00
stereo = ( const AVStereo3D * ) av_stream_get_side_data ( st , AV_PKT_DATA_STEREO3D ,
2020-05-21 23:10:51 +02:00
NULL ) ;
if ( stereo ) {
2020-05-07 14:19:27 +02:00
switch ( stereo - > type ) {
case AV_STEREO3D_2D :
format = MATROSKA_VIDEO_STEREOMODE_TYPE_MONO ;
break ;
case AV_STEREO3D_SIDEBYSIDE :
format = ( stereo - > flags & AV_STEREO3D_FLAG_INVERT )
? MATROSKA_VIDEO_STEREOMODE_TYPE_RIGHT_LEFT
: MATROSKA_VIDEO_STEREOMODE_TYPE_LEFT_RIGHT ;
* h_width = 2 ;
break ;
case AV_STEREO3D_TOPBOTTOM :
format = MATROSKA_VIDEO_STEREOMODE_TYPE_TOP_BOTTOM ;
if ( stereo - > flags & AV_STEREO3D_FLAG_INVERT )
format - - ;
* h_height = 2 ;
break ;
case AV_STEREO3D_CHECKERBOARD :
format = MATROSKA_VIDEO_STEREOMODE_TYPE_CHECKERBOARD_LR ;
if ( stereo - > flags & AV_STEREO3D_FLAG_INVERT )
format - - ;
break ;
case AV_STEREO3D_LINES :
format = MATROSKA_VIDEO_STEREOMODE_TYPE_ROW_INTERLEAVED_LR ;
if ( stereo - > flags & AV_STEREO3D_FLAG_INVERT )
format - - ;
* h_height = 2 ;
break ;
case AV_STEREO3D_COLUMNS :
format = MATROSKA_VIDEO_STEREOMODE_TYPE_COL_INTERLEAVED_LR ;
if ( stereo - > flags & AV_STEREO3D_FLAG_INVERT )
format - - ;
* h_width = 2 ;
break ;
case AV_STEREO3D_FRAMESEQUENCE :
format = MATROSKA_VIDEO_STEREOMODE_TYPE_BOTH_EYES_BLOCK_LR ;
if ( stereo - > flags & AV_STEREO3D_FLAG_INVERT )
format + + ;
break ;
}
2014-06-07 02:38:00 +03:00
}
2014-08-29 01:30:47 +03:00
if ( format = = MATROSKA_VIDEO_STEREOMODE_TYPE_NB )
2020-05-07 14:19:27 +02:00
return 0 ;
2014-08-29 01:30:47 +03:00
2014-10-22 16:36:32 +03:00
// if webm, do not write unsupported modes
2022-01-07 12:36:19 +02:00
if ( ( is_webm & &
2014-08-29 01:30:47 +03:00
format > MATROSKA_VIDEO_STEREOMODE_TYPE_TOP_BOTTOM & &
format ! = MATROSKA_VIDEO_STEREOMODE_TYPE_RIGHT_LEFT )
2014-08-29 02:26:52 +03:00
| | format > = MATROSKA_VIDEO_STEREOMODE_TYPE_NB ) {
2014-07-01 21:44:05 +03:00
av_log ( s , AV_LOG_ERROR ,
" The specified stereo mode is not valid. \n " ) ;
return AVERROR ( EINVAL ) ;
2014-08-29 01:30:47 +03:00
}
2014-06-07 02:38:00 +03:00
2014-10-22 16:36:32 +03:00
// write StereoMode if format is valid
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( writer , MATROSKA_ID_VIDEOSTEREOMODE , format ) ;
2014-08-05 13:16:17 +03:00
2020-05-07 14:19:27 +02:00
return 0 ;
2014-06-07 02:38:00 +03:00
}
2022-01-01 18:51:50 +02:00
static void mkv_write_dovi ( AVFormatContext * s , AVIOContext * pb , AVStream * st )
{
2022-01-06 20:42:05 +02:00
# if CONFIG_MATROSKA_MUXER
2022-01-01 18:51:50 +02:00
AVDOVIDecoderConfigurationRecord * dovi = ( AVDOVIDecoderConfigurationRecord * )
av_stream_get_side_data ( st , AV_PKT_DATA_DOVI_CONF , NULL ) ;
if ( dovi & & dovi - > dv_profile < = 10 ) {
ebml_master mapping ;
uint8_t buf [ ISOM_DVCC_DVVC_SIZE ] ;
uint32_t type ;
uint64_t expected_size = ( 2 + 1 + ( sizeof ( DVCC_DVVC_BLOCK_TYPE_NAME ) - 1 ) )
+ ( 2 + 1 + 4 ) + ( 2 + 1 + ISOM_DVCC_DVVC_SIZE ) ;
if ( dovi - > dv_profile > 7 ) {
type = MKBETAG ( ' d ' , ' v ' , ' v ' , ' C ' ) ;
} else {
type = MKBETAG ( ' d ' , ' v ' , ' c ' , ' C ' ) ;
}
ff_isom_put_dvcc_dvvc ( s , buf , dovi ) ;
mapping = start_ebml_master ( pb , MATROSKA_ID_TRACKBLKADDMAPPING , expected_size ) ;
put_ebml_string ( pb , MATROSKA_ID_BLKADDIDNAME , DVCC_DVVC_BLOCK_TYPE_NAME ) ;
put_ebml_uint ( pb , MATROSKA_ID_BLKADDIDTYPE , type ) ;
put_ebml_binary ( pb , MATROSKA_ID_BLKADDIDEXTRADATA , buf , sizeof ( buf ) ) ;
end_ebml_master ( pb , mapping ) ;
}
2022-01-06 20:42:05 +02:00
# endif
2022-01-01 18:51:50 +02:00
}
2022-01-14 02:28:34 +02:00
static int mkv_write_track_video ( AVFormatContext * s , MatroskaMuxContext * mkv ,
AVStream * st , const AVCodecParameters * par ,
AVIOContext * pb )
{
const AVDictionaryEntry * tag ;
int display_width_div = 1 , display_height_div = 1 ;
2022-01-14 14:56:52 +02:00
uint8_t color_space [ 4 ] , projection_private [ 20 ] ;
EBML_WRITER ( MAX_FIELD_ORDER_ELEMS + MAX_STEREO_MODE_ELEMS +
MAX_VIDEO_COLOR_ELEMS + MAX_VIDEO_PROJECTION_ELEMS + 8 ) ;
2022-01-14 02:28:34 +02:00
int ret ;
2022-01-14 14:56:52 +02:00
ebml_writer_open_master ( & writer , MATROSKA_ID_TRACKVIDEO ) ;
2022-01-14 02:28:34 +02:00
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( & writer , MATROSKA_ID_VIDEOPIXELWIDTH , par - > width ) ;
ebml_writer_add_uint ( & writer , MATROSKA_ID_VIDEOPIXELHEIGHT , par - > height ) ;
2022-01-14 02:28:34 +02:00
2022-01-14 14:56:52 +02:00
mkv_write_field_order ( & writer , IS_WEBM ( mkv ) , par - > field_order ) ;
2022-01-14 02:28:34 +02:00
// check both side data and metadata for stereo information,
// write the result to the bitstream if any is found
2022-01-14 14:56:52 +02:00
ret = mkv_write_stereo_mode ( s , & writer , st , IS_WEBM ( mkv ) ,
2022-01-14 02:28:34 +02:00
& display_width_div ,
& display_height_div ) ;
if ( ret < 0 )
return ret ;
if ( ( ( tag = av_dict_get ( st - > metadata , " alpha_mode " , NULL , 0 ) ) & & atoi ( tag - > value ) ) | |
( ( tag = av_dict_get ( s - > metadata , " alpha_mode " , NULL , 0 ) ) & & atoi ( tag - > value ) ) | |
( par - > format = = AV_PIX_FMT_YUVA420P ) ) {
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( & writer , MATROSKA_ID_VIDEOALPHAMODE , 1 ) ;
2022-01-14 02:28:34 +02:00
}
// write DisplayWidth and DisplayHeight, they contain the size of
// a single source view and/or the display aspect ratio
if ( st - > sample_aspect_ratio . num ) {
int64_t d_width = av_rescale ( par - > width , st - > sample_aspect_ratio . num , st - > sample_aspect_ratio . den ) ;
if ( d_width > INT_MAX ) {
av_log ( s , AV_LOG_ERROR , " Overflow in display width \n " ) ;
return AVERROR ( EINVAL ) ;
}
if ( d_width ! = par - > width | | display_width_div ! = 1 | | display_height_div ! = 1 ) {
if ( IS_WEBM ( mkv ) | | display_width_div ! = 1 | | display_height_div ! = 1 ) {
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( & writer , MATROSKA_ID_VIDEODISPLAYWIDTH ,
d_width / display_width_div ) ;
ebml_writer_add_uint ( & writer , MATROSKA_ID_VIDEODISPLAYHEIGHT ,
par - > height / display_height_div ) ;
2022-01-14 02:28:34 +02:00
} else {
AVRational display_aspect_ratio ;
av_reduce ( & display_aspect_ratio . num , & display_aspect_ratio . den ,
par - > width * ( int64_t ) st - > sample_aspect_ratio . num ,
par - > height * ( int64_t ) st - > sample_aspect_ratio . den ,
1024 * 1024 ) ;
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( & writer , MATROSKA_ID_VIDEODISPLAYWIDTH ,
display_aspect_ratio . num ) ;
ebml_writer_add_uint ( & writer , MATROSKA_ID_VIDEODISPLAYHEIGHT ,
display_aspect_ratio . den ) ;
ebml_writer_add_uint ( & writer , MATROSKA_ID_VIDEODISPLAYUNIT ,
MATROSKA_VIDEO_DISPLAYUNIT_DAR ) ;
2022-01-14 02:28:34 +02:00
}
}
} else if ( display_width_div ! = 1 | | display_height_div ! = 1 ) {
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( & writer , MATROSKA_ID_VIDEODISPLAYWIDTH ,
par - > width / display_width_div ) ;
ebml_writer_add_uint ( & writer , MATROSKA_ID_VIDEODISPLAYHEIGHT ,
par - > height / display_height_div ) ;
2022-01-14 02:28:34 +02:00
} else if ( ! IS_WEBM ( mkv ) )
2022-01-14 14:56:52 +02:00
ebml_writer_add_uint ( & writer , MATROSKA_ID_VIDEODISPLAYUNIT ,
MATROSKA_VIDEO_DISPLAYUNIT_UNKNOWN ) ;
2022-01-14 02:28:34 +02:00
if ( par - > codec_id = = AV_CODEC_ID_RAWVIDEO ) {
2022-01-14 14:56:52 +02:00
AV_WL32 ( color_space , par - > codec_tag ) ;
ebml_writer_add_bin ( & writer , MATROSKA_ID_VIDEOCOLORSPACE ,
color_space , sizeof ( color_space ) ) ;
2022-01-14 02:28:34 +02:00
}
2022-01-14 14:56:52 +02:00
mkv_write_video_color ( & writer , st , par ) ;
mkv_write_video_projection ( s , & writer , st , projection_private ) ;
2022-01-14 02:28:34 +02:00
2022-01-14 14:56:52 +02:00
return ebml_writer_write ( & writer , pb ) ;
2022-01-14 02:28:34 +02:00
}
2014-06-07 02:21:13 +03:00
static int mkv_write_track ( AVFormatContext * s , MatroskaMuxContext * mkv ,
2020-01-18 10:31:01 +02:00
AVStream * st , mkv_track * track , AVIOContext * pb ,
2020-01-18 08:05:20 +02:00
int is_default )
2007-09-05 03:22:37 +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
AVCodecParameters * par = st - > codecpar ;
2020-01-18 10:31:01 +02:00
ebml_master subinfo , track_master ;
2014-06-07 02:21:13 +03:00
int native_id = 0 ;
int qt_id = 0 ;
2020-01-17 21:14:24 +02:00
int bit_depth ;
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
int sample_rate = par - > sample_rate ;
2014-06-07 02:21:13 +03:00
int output_sample_rate = 0 ;
int j , ret ;
2019-11-20 04:35:24 +02:00
const AVDictionaryEntry * tag ;
2014-06-07 02:21:13 +03:00
2019-12-29 04:36:56 +02:00
if ( par - > codec_type = = AVMEDIA_TYPE_ATTACHMENT )
2014-06-07 02:21:13 +03:00
return 0 ;
2011-07-30 14:37:36 +03:00
2020-01-18 10:31:01 +02:00
track_master = start_ebml_master ( pb , MATROSKA_ID_TRACKENTRY , 0 ) ;
put_ebml_uint ( pb , MATROSKA_ID_TRACKNUMBER , track - > track_num ) ;
put_ebml_uid ( pb , MATROSKA_ID_TRACKUID , track - > uid ) ;
2019-12-29 04:36:56 +02:00
put_ebml_uint ( pb , MATROSKA_ID_TRACKFLAGLACING , 0 ) ; // no lacing (yet)
2007-09-05 03:25:14 +03:00
2014-06-07 02:21:13 +03:00
if ( ( tag = av_dict_get ( st - > metadata , " title " , NULL , 0 ) ) )
put_ebml_string ( pb , MATROSKA_ID_TRACKNAME , tag - > value ) ;
tag = av_dict_get ( st - > metadata , " language " , NULL , 0 ) ;
2020-01-18 03:19:46 +02:00
put_ebml_string ( pb , MATROSKA_ID_TRACKLANGUAGE ,
2021-02-16 16:53:25 +02:00
tag & & tag - > value [ 0 ] ? tag - > value : " und " ) ;
2007-09-05 03:22:50 +03:00
2014-06-07 02:21:13 +03:00
// The default value for TRACKFLAGDEFAULT is 1, so add element
// if we need to clear it.
2020-01-18 08:05:20 +02:00
if ( ! is_default )
put_ebml_uint ( pb , MATROSKA_ID_TRACKFLAGDEFAULT , 0 ) ;
2007-09-05 03:22:50 +03:00
2014-07-01 21:22:51 +03:00
if ( st - > disposition & AV_DISPOSITION_FORCED )
put_ebml_uint ( pb , MATROSKA_ID_TRACKFLAGFORCED , 1 ) ;
2022-01-07 12:36:19 +02:00
if ( IS_WEBM ( mkv ) ) {
2014-07-01 21:22:51 +03:00
const char * codec_id ;
2018-09-21 21:18:15 +02:00
if ( par - > codec_type ! = AVMEDIA_TYPE_SUBTITLE ) {
for ( j = 0 ; ff_webm_codec_tags [ j ] . id ! = AV_CODEC_ID_NONE ; j + + ) {
if ( ff_webm_codec_tags [ j ] . id = = par - > codec_id ) {
codec_id = ff_webm_codec_tags [ j ] . str ;
native_id = 1 ;
break ;
}
}
} else if ( par - > codec_id = = AV_CODEC_ID_WEBVTT ) {
2018-09-21 21:18:36 +02:00
if ( st - > disposition & AV_DISPOSITION_CAPTIONS ) {
codec_id = " D_WEBVTT/CAPTIONS " ;
native_id = MATROSKA_TRACK_TYPE_SUBTITLE ;
} else if ( st - > disposition & AV_DISPOSITION_DESCRIPTIONS ) {
codec_id = " D_WEBVTT/DESCRIPTIONS " ;
native_id = MATROSKA_TRACK_TYPE_METADATA ;
} else if ( st - > disposition & AV_DISPOSITION_METADATA ) {
codec_id = " D_WEBVTT/METADATA " ;
native_id = MATROSKA_TRACK_TYPE_METADATA ;
} else {
codec_id = " D_WEBVTT/SUBTITLES " ;
native_id = MATROSKA_TRACK_TYPE_SUBTITLE ;
}
2018-09-21 21:18:15 +02:00
}
if ( ! native_id ) {
av_log ( s , AV_LOG_ERROR ,
" Only VP8 or VP9 or AV1 video and Vorbis or Opus audio and WebVTT subtitles are supported for WebM. \n " ) ;
return AVERROR ( EINVAL ) ;
}
2014-07-01 21:22:51 +03:00
put_ebml_string ( pb , MATROSKA_ID_CODECID , codec_id ) ;
} else {
2021-02-16 19:32:23 +02:00
if ( st - > disposition & AV_DISPOSITION_COMMENT )
put_ebml_uint ( pb , MATROSKA_ID_TRACKFLAGCOMMENTARY , 1 ) ;
2021-02-16 20:14:06 +02:00
if ( st - > disposition & AV_DISPOSITION_HEARING_IMPAIRED )
put_ebml_uint ( pb , MATROSKA_ID_TRACKFLAGHEARINGIMPAIRED , 1 ) ;
if ( st - > disposition & AV_DISPOSITION_VISUAL_IMPAIRED )
put_ebml_uint ( pb , MATROSKA_ID_TRACKFLAGVISUALIMPAIRED , 1 ) ;
2021-02-16 19:53:55 +02:00
if ( st - > disposition & ( AV_DISPOSITION_ORIGINAL | AV_DISPOSITION_DUB ) & &
( st - > disposition & ( AV_DISPOSITION_ORIGINAL | AV_DISPOSITION_DUB ) )
! = ( AV_DISPOSITION_ORIGINAL | AV_DISPOSITION_DUB ) )
put_ebml_uint ( pb , MATROSKA_ID_TRACKFLAGORIGINAL ,
! ! ( st - > disposition & AV_DISPOSITION_ORIGINAL ) ) ;
2021-02-16 20:14:06 +02:00
2014-07-01 21:22:51 +03:00
// look for a codec ID string specific to mkv to use,
// if none are found, use AVI codes
2017-01-14 07:06:05 +02:00
if ( par - > codec_id ! = AV_CODEC_ID_RAWVIDEO | | par - > codec_tag ) {
2017-01-14 07:07:06 +02:00
for ( j = 0 ; ff_mkv_codec_tags [ j ] . id ! = AV_CODEC_ID_NONE ; j + + ) {
2017-03-02 00:40:35 +02:00
if ( ff_mkv_codec_tags [ j ] . id = = par - > codec_id & & par - > codec_id ! = AV_CODEC_ID_FFV1 ) {
2017-01-14 07:07:06 +02:00
put_ebml_string ( pb , MATROSKA_ID_CODECID , ff_mkv_codec_tags [ j ] . str ) ;
native_id = 1 ;
break ;
}
2007-09-05 03:22:50 +03:00
}
2017-01-14 07:06:05 +02:00
} else {
2015-02-14 17:20:58 +02:00
if ( mkv - > allow_raw_vfw ) {
native_id = 0 ;
} else {
av_log ( s , AV_LOG_ERROR , " Raw RGB is not supported Natively in Matroska, you can use AVI or NUT or \n "
" If you would like to store it anyway using VFW mode, enable allow_raw_vfw (-allow_raw_vfw 1) \n " ) ;
return AVERROR ( EINVAL ) ;
}
}
2014-07-01 21:22:51 +03:00
}
2007-09-05 03:22:50 +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
switch ( par - > codec_type ) {
2014-06-07 02:21:13 +03:00
case AVMEDIA_TYPE_VIDEO :
2016-05-04 21:31:28 +02:00
mkv - > have_video = 1 ;
2014-06-07 02:21:13 +03:00
put_ebml_uint ( pb , MATROSKA_ID_TRACKTYPE , MATROSKA_TRACK_TYPE_VIDEO ) ;
2014-07-01 21:22:51 +03:00
if ( st - > avg_frame_rate . num > 0 & & st - > avg_frame_rate . den > 0
2016-04-10 21:58:15 +02:00
& & av_cmp_q ( av_inv_q ( st - > avg_frame_rate ) , st - > time_base ) > 0 )
2015-05-26 20:09:40 +02:00
put_ebml_uint ( pb , MATROSKA_ID_TRACKDEFAULTDURATION , 1000000000LL * st - > avg_frame_rate . den / st - > avg_frame_rate . num ) ;
2019-10-01 17:58:20 +02:00
else if ( st - > r_frame_rate . num > 0 & & st - > r_frame_rate . den > 0
& & av_cmp_q ( av_inv_q ( st - > r_frame_rate ) , st - > time_base ) > 0 )
put_ebml_uint ( pb , MATROSKA_ID_TRACKDEFAULTDURATION , 1000000000LL * st - > r_frame_rate . den / st - > r_frame_rate . num ) ;
2014-06-07 02:21:13 +03:00
2022-01-07 12:36:19 +02:00
if ( CONFIG_MATROSKA_MUXER & & ! native_id & &
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
ff_codec_get_tag ( ff_codec_movvideo_tags , par - > codec_id ) & &
2016-04-10 21:58:15 +02:00
( ( ! ff_codec_get_tag ( ff_codec_bmp_tags , par - > codec_id ) & & par - > codec_id ! = AV_CODEC_ID_RAWVIDEO ) | |
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
par - > codec_id = = AV_CODEC_ID_SVQ1 | |
par - > codec_id = = AV_CODEC_ID_SVQ3 | |
par - > codec_id = = AV_CODEC_ID_CINEPAK ) )
2014-06-07 02:21:13 +03:00
qt_id = 1 ;
if ( qt_id )
put_ebml_string ( pb , MATROSKA_ID_CODECID , " V_QUICKTIME " ) ;
else if ( ! native_id ) {
// if there is no mkv-specific codec ID, use VFW mode
put_ebml_string ( pb , MATROSKA_ID_CODECID , " V_MS/VFW/FOURCC " ) ;
2020-01-18 10:31:01 +02:00
track - > write_dts = 1 ;
2021-08-24 14:58:07 +02:00
ffformatcontext ( s ) - > avoid_negative_ts_use_pts = 0 ;
2010-06-05 01:40:54 +03:00
}
2022-01-14 02:28:34 +02:00
ret = mkv_write_track_video ( s , mkv , st , par , pb ) ;
2014-08-05 13:16:17 +03:00
if ( ret < 0 )
return ret ;
2022-01-07 12:36:19 +02:00
if ( ! IS_WEBM ( mkv ) )
2022-01-01 18:51:50 +02:00
mkv_write_dovi ( s , pb , st ) ;
2014-06-07 02:21:13 +03:00
break ;
case AVMEDIA_TYPE_AUDIO :
2020-01-17 21:14:24 +02:00
if ( par - > initial_padding & & par - > codec_id = = AV_CODEC_ID_OPUS ) {
2020-01-17 21:40:15 +02:00
int64_t codecdelay = av_rescale_q ( par - > initial_padding ,
( AVRational ) { 1 , 48000 } ,
( AVRational ) { 1 , 1000000000 } ) ;
if ( codecdelay < 0 ) {
av_log ( s , AV_LOG_ERROR , " Initial padding is invalid \n " ) ;
return AVERROR ( EINVAL ) ;
}
2020-01-18 10:31:01 +02:00
// track->ts_offset = av_rescale_q(par->initial_padding,
// (AVRational){ 1, par->sample_rate },
// st->time_base);
2020-01-17 21:14:24 +02:00
2020-01-17 21:40:15 +02:00
put_ebml_uint ( pb , MATROSKA_ID_CODECDELAY , codecdelay ) ;
}
if ( par - > codec_id = = AV_CODEC_ID_OPUS )
put_ebml_uint ( pb , MATROSKA_ID_SEEKPREROLL , OPUS_SEEK_PREROLL ) ;
2022-01-07 12:36:19 +02:00
# if CONFIG_MATROSKA_MUXER
2022-01-07 08:49:22 +02:00
else if ( par - > codec_id = = AV_CODEC_ID_AAC ) {
ret = get_aac_sample_rates ( s , mkv , par - > extradata , par - > extradata_size ,
& sample_rate , & output_sample_rate ) ;
if ( ret < 0 )
return ret ;
}
2022-01-07 12:36:19 +02:00
# endif
2020-01-17 21:14:24 +02:00
2014-06-07 02:21:13 +03:00
put_ebml_uint ( pb , MATROSKA_ID_TRACKTYPE , MATROSKA_TRACK_TYPE_AUDIO ) ;
if ( ! native_id )
// no mkv-specific ID, use ACM mode
put_ebml_string ( pb , MATROSKA_ID_CODECID , " A_MS/ACM " ) ;
2019-12-29 11:22:49 +02:00
subinfo = start_ebml_master ( pb , MATROSKA_ID_TRACKAUDIO , 6 + 4 * 9 ) ;
2017-03-31 16:57:00 +02:00
put_ebml_uint ( pb , MATROSKA_ID_AUDIOCHANNELS , par - > ch_layout . nb_channels ) ;
2017-04-13 17:43:11 +02:00
2020-01-18 10:31:01 +02:00
track - > sample_rate_offset = avio_tell ( pb ) ;
2014-06-07 02:21:13 +03:00
put_ebml_float ( pb , MATROSKA_ID_AUDIOSAMPLINGFREQ , sample_rate ) ;
if ( output_sample_rate )
put_ebml_float ( pb , MATROSKA_ID_AUDIOOUTSAMPLINGFREQ , output_sample_rate ) ;
2020-01-17 21:14:24 +02:00
bit_depth = av_get_bits_per_sample ( par - > codec_id ) ;
if ( ! bit_depth & & par - > codec_id ! = AV_CODEC_ID_ADPCM_G726 ) {
if ( par - > bits_per_raw_sample )
bit_depth = par - > bits_per_raw_sample ;
else
bit_depth = av_get_bytes_per_sample ( par - > format ) < < 3 ;
}
if ( ! bit_depth )
bit_depth = par - > bits_per_coded_sample ;
2014-06-07 02:21:13 +03:00
if ( bit_depth )
put_ebml_uint ( pb , MATROSKA_ID_AUDIOBITDEPTH , bit_depth ) ;
end_ebml_master ( pb , subinfo ) ;
break ;
case AVMEDIA_TYPE_SUBTITLE :
if ( ! native_id ) {
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
av_log ( s , AV_LOG_ERROR , " Subtitle codec %d is not supported. \n " , par - > codec_id ) ;
2014-06-07 02:21:13 +03:00
return AVERROR ( ENOSYS ) ;
}
2022-01-07 12:36:19 +02:00
if ( ! IS_WEBM ( mkv ) & & st - > disposition & AV_DISPOSITION_DESCRIPTIONS )
2021-02-16 20:49:16 +02:00
put_ebml_uint ( pb , MATROSKA_ID_TRACKFLAGTEXTDESCRIPTIONS , 1 ) ;
2014-07-01 21:22:51 +03:00
2022-01-07 12:36:19 +02:00
if ( ! IS_WEBM ( mkv ) | | par - > codec_id ! = AV_CODEC_ID_WEBVTT )
2014-07-01 21:22:51 +03:00
native_id = MATROSKA_TRACK_TYPE_SUBTITLE ;
put_ebml_uint ( pb , MATROSKA_ID_TRACKTYPE , native_id ) ;
2014-06-07 02:21:13 +03:00
break ;
default :
av_log ( s , AV_LOG_ERROR , " Only audio, video, and subtitles are supported for Matroska. \n " ) ;
2014-07-01 21:22:51 +03:00
return AVERROR ( EINVAL ) ;
}
2022-01-07 12:36:19 +02:00
if ( ! IS_WEBM ( mkv ) | | par - > codec_id ! = AV_CODEC_ID_WEBVTT ) {
2022-06-17 05:49:06 +02:00
uint8_t * codecpriv ;
int codecpriv_size , max_payload_size ;
2020-01-18 10:31:01 +02:00
track - > codecpriv_offset = avio_tell ( pb ) ;
2022-06-17 05:49:06 +02:00
ret = mkv_assemble_codecprivate ( s , mkv - > tmp_bc , par ,
par - > extradata , par - > extradata_size ,
native_id , qt_id ,
& codecpriv , & codecpriv_size , & max_payload_size ) ;
2014-07-01 21:22:51 +03:00
if ( ret < 0 )
2022-06-17 05:49:06 +02:00
goto fail ;
mkv_put_codecprivate ( pb , max_payload_size , codecpriv , codecpriv_size ) ;
track - > codecpriv_size = max_payload_size ;
2014-06-07 02:21:13 +03:00
}
2010-06-05 01:40:54 +03:00
2020-01-18 10:31:01 +02:00
end_ebml_master ( pb , track_master ) ;
2022-06-17 05:49:06 +02:00
fail :
ffio_reset_dyn_buf ( mkv - > tmp_bc ) ;
2007-09-05 03:25:58 +03:00
2022-06-17 05:49:06 +02:00
return ret ;
2014-06-07 02:21:13 +03:00
}
2014-06-07 01:54:50 +03:00
2014-06-07 02:21:13 +03:00
static int mkv_write_tracks ( AVFormatContext * s )
{
MatroskaMuxContext * mkv = s - > priv_data ;
2016-10-30 21:39:03 +02:00
AVIOContext * pb = s - > pb ;
2021-08-20 07:50:44 +02:00
int video_default_idx = - 1 , audio_default_idx = - 1 , subtitle_default_idx = - 1 ;
int i , ret ;
2007-09-05 03:25:49 +03:00
2019-12-29 04:26:05 +02:00
if ( mkv - > nb_attachments = = s - > nb_streams )
return 0 ;
2020-04-29 06:31:07 +02:00
ret = start_ebml_master_crc32 ( & mkv - > track . bc , mkv ) ;
2016-10-04 00:20:11 +02:00
if ( ret < 0 )
return ret ;
2020-01-18 08:05:20 +02:00
if ( mkv - > default_mode ! = DEFAULT_MODE_PASSTHROUGH ) {
2021-08-20 07:50:44 +02:00
int video_idx = - 1 , audio_idx = - 1 , subtitle_idx = - 1 ;
2020-01-18 08:05:20 +02:00
for ( i = s - > nb_streams - 1 ; i > = 0 ; i - - ) {
AVStream * st = s - > streams [ i ] ;
switch ( st - > codecpar - > codec_type ) {
# define CASE(type, variable) \
case AVMEDIA_TYPE_ # # type : \
variable # # _idx = i ; \
if ( st - > disposition & AV_DISPOSITION_DEFAULT ) \
variable # # _default_idx = i ; \
break ;
CASE ( VIDEO , video )
CASE ( AUDIO , audio )
CASE ( SUBTITLE , subtitle )
# undef CASE
}
}
video_default_idx = FFMAX ( video_default_idx , video_idx ) ;
audio_default_idx = FFMAX ( audio_default_idx , audio_idx ) ;
if ( mkv - > default_mode ! = DEFAULT_MODE_INFER_NO_SUBS )
subtitle_default_idx = FFMAX ( subtitle_default_idx , subtitle_idx ) ;
2014-07-01 21:22:51 +03:00
}
for ( i = 0 ; i < s - > nb_streams ; i + + ) {
2020-01-18 10:31:01 +02:00
AVStream * st = s - > streams [ i ] ;
2021-08-20 07:50:44 +02:00
int is_default = st - > disposition & AV_DISPOSITION_DEFAULT | |
2020-01-18 08:05:20 +02:00
i = = video_default_idx | | i = = audio_default_idx | |
i = = subtitle_default_idx ;
2020-01-18 10:31:01 +02:00
ret = mkv_write_track ( s , mkv , st , & mkv - > tracks [ i ] ,
2020-04-29 06:31:07 +02:00
mkv - > track . bc , is_default ) ;
2014-06-07 01:54:50 +03:00
if ( ret < 0 )
return ret ;
2007-09-05 03:22:37 +03:00
}
2016-10-30 21:39:03 +02:00
2020-04-29 20:55:26 +02:00
return end_ebml_master_crc32_tentatively ( pb , & mkv - > track , mkv ,
2020-04-29 07:24:02 +02:00
MATROSKA_ID_TRACKS ) ;
2007-09-05 03:23:15 +03:00
}
2019-11-20 04:35:24 +02:00
static int mkv_write_simpletag ( AVIOContext * pb , const AVDictionaryEntry * t )
2010-10-05 12:40:43 +03:00
{
2022-01-14 01:51:42 +02:00
EBML_WRITER ( 4 ) ;
2010-10-05 12:40:43 +03:00
uint8_t * key = av_strdup ( t - > key ) ;
uint8_t * p = key ;
const uint8_t * lang = NULL ;
2022-01-14 01:51:42 +02:00
int ret ;
2010-10-05 12:40:43 +03:00
2014-12-17 15:53:45 +02:00
if ( ! key )
return AVERROR ( ENOMEM ) ;
2010-10-05 12:40:43 +03:00
if ( ( p = strrchr ( p , ' - ' ) ) & &
2016-05-13 23:03:28 +02:00
( lang = ff_convert_lang_to ( p + 1 , AV_LANG_ISO639_2_BIBL ) ) )
2010-10-05 12:40:43 +03:00
* p = 0 ;
p = key ;
while ( * p ) {
if ( * p = = ' ' )
* p = ' _ ' ;
else if ( * p > = ' a ' & & * p < = ' z ' )
* p - = ' a ' - ' A ' ;
p + + ;
}
2022-01-14 01:51:42 +02:00
ebml_writer_open_master ( & writer , MATROSKA_ID_SIMPLETAG ) ;
ebml_writer_add_string ( & writer , MATROSKA_ID_TAGNAME , key ) ;
2010-10-05 12:40:43 +03:00
if ( lang )
2022-01-14 01:51:42 +02:00
ebml_writer_add_string ( & writer , MATROSKA_ID_TAGLANG , lang ) ;
ebml_writer_add_string ( & writer , MATROSKA_ID_TAGSTRING , t - > value ) ;
ret = ebml_writer_write ( & writer , pb ) ;
2010-10-05 12:40:43 +03:00
av_freep ( & key ) ;
2022-01-14 01:51:42 +02:00
return ret ;
2010-10-05 12:40:43 +03:00
}
2022-06-15 11:25:12 +02:00
static void mkv_write_tag_targets ( MatroskaMuxContext * mkv , AVIOContext * pb ,
uint32_t elementid , uint64_t uid )
2010-10-05 12:40:43 +03:00
{
2022-06-15 11:25:12 +02:00
ebml_master targets = start_ebml_master ( pb , MATROSKA_ID_TAGTARGETS ,
4 + 1 + 8 ) ;
2010-10-05 12:40:43 +03:00
if ( elementid )
2022-06-15 11:25:12 +02:00
put_ebml_uid ( pb , elementid , uid ) ;
end_ebml_master ( pb , targets ) ;
2015-08-05 07:09:57 +02:00
}
2019-04-20 01:41:04 +02:00
static int mkv_check_tag_name ( const char * name , uint32_t elementid )
2016-09-06 04:42:24 +02:00
{
return av_strcasecmp ( name , " title " ) & &
av_strcasecmp ( name , " stereo_mode " ) & &
av_strcasecmp ( name , " creation_time " ) & &
av_strcasecmp ( name , " encoding_tool " ) & &
2016-09-06 04:43:18 +02:00
av_strcasecmp ( name , " duration " ) & &
2016-09-06 04:42:24 +02:00
( elementid ! = MATROSKA_ID_TAGTARGETS_TRACKUID | |
2016-10-07 07:46:22 +02:00
av_strcasecmp ( name , " language " ) ) & &
( elementid ! = MATROSKA_ID_TAGTARGETS_ATTACHUID | |
( av_strcasecmp ( name , " filename " ) & &
av_strcasecmp ( name , " mimetype " ) ) ) ;
2016-09-06 04:42:24 +02:00
}
2020-04-29 07:52:56 +02:00
static int mkv_write_tag ( MatroskaMuxContext * mkv , const AVDictionary * m ,
2022-06-15 11:25:12 +02:00
AVIOContext * * pb , unsigned reserved_size ,
2020-04-29 07:52:56 +02:00
uint32_t elementid , uint64_t uid )
2015-08-05 07:09:57 +02:00
{
2019-11-20 04:35:24 +02:00
const AVDictionaryEntry * t = NULL ;
2022-06-15 11:25:12 +02:00
AVIOContext * const tmp_bc = mkv - > tmp_bc ;
uint8_t * buf ;
2022-06-15 11:49:48 +02:00
int ret = 0 , size , tag_written = 0 ;
2015-08-05 07:09:57 +02:00
2022-06-15 11:25:12 +02:00
mkv_write_tag_targets ( mkv , tmp_bc , elementid , uid ) ;
2010-10-05 12:40:43 +03:00
2014-12-17 15:53:45 +02:00
while ( ( t = av_dict_get ( m , " " , t , AV_DICT_IGNORE_SUFFIX ) ) ) {
2016-09-06 04:42:24 +02:00
if ( mkv_check_tag_name ( t - > key , elementid ) ) {
2022-06-15 11:25:12 +02:00
ret = mkv_write_simpletag ( tmp_bc , t ) ;
2014-12-17 15:53:45 +02:00
if ( ret < 0 )
2022-06-15 11:25:12 +02:00
goto end ;
2022-06-15 11:49:48 +02:00
tag_written = 1 ;
2014-12-17 15:53:45 +02:00
}
}
2022-06-15 11:25:12 +02:00
if ( reserved_size )
put_ebml_void ( tmp_bc , reserved_size ) ;
2022-06-15 11:49:48 +02:00
else if ( ! tag_written )
goto end ;
2010-10-05 12:40:43 +03:00
2022-06-15 11:25:12 +02:00
size = avio_get_dyn_buf ( tmp_bc , & buf ) ;
if ( tmp_bc - > error ) {
ret = tmp_bc - > error ;
goto end ;
}
if ( ! * pb ) {
ret = start_ebml_master_crc32 ( pb , mkv ) ;
if ( ret < 0 )
goto end ;
}
put_ebml_binary ( * pb , MATROSKA_ID_TAG , buf , size ) ;
2020-04-04 15:35:48 +02:00
2022-06-15 11:25:12 +02:00
end :
ffio_reset_dyn_buf ( tmp_bc ) ;
return ret ;
2010-10-05 12:40:43 +03:00
}
static int mkv_write_tags ( AVFormatContext * s )
{
2013-07-19 23:52:16 +03:00
MatroskaMuxContext * mkv = s - > priv_data ;
2022-06-15 11:25:12 +02:00
int i , ret , seekable = IS_SEEKABLE ( s - > pb , mkv ) ;
2010-10-05 12:40:43 +03:00
2020-04-29 10:27:23 +02:00
mkv - > wrote_tags = 1 ;
2010-10-15 22:04:25 +03:00
ff_metadata_conv_ctx ( s , ff_mkv_metadata_conv , NULL ) ;
2022-06-15 11:49:48 +02:00
ret = mkv_write_tag ( mkv , s - > metadata , & mkv - > tags . bc , 0 , 0 , 0 ) ;
if ( ret < 0 )
return ret ;
2010-10-05 12:40:43 +03:00
for ( i = 0 ; i < s - > nb_streams ; i + + ) {
2019-11-20 04:35:24 +02:00
const AVStream * st = s - > streams [ i ] ;
2019-12-29 02:33:36 +02:00
mkv_track * track = & mkv - > tracks [ i ] ;
2010-10-05 12:40:43 +03:00
2016-10-07 07:46:22 +02:00
if ( st - > codecpar - > codec_type = = AVMEDIA_TYPE_ATTACHMENT )
continue ;
2022-06-15 11:25:12 +02:00
ret = mkv_write_tag ( mkv , st - > metadata , & mkv - > tags . bc ,
seekable ? DURATION_SIMPLETAG_SIZE : 0 ,
2020-04-29 07:52:56 +02:00
MATROSKA_ID_TAGTARGETS_TRACKUID , track - > uid ) ;
2019-12-29 04:36:56 +02:00
if ( ret < 0 )
return ret ;
2022-06-15 11:25:12 +02:00
if ( seekable )
track - > duration_offset = avio_tell ( mkv - > tags . bc ) - DURATION_SIMPLETAG_SIZE ;
2015-08-05 07:09:57 +02:00
}
2022-01-07 12:36:19 +02:00
if ( mkv - > nb_attachments & & ! IS_WEBM ( mkv ) ) {
2019-12-29 03:28:07 +02:00
for ( i = 0 ; i < s - > nb_streams ; i + + ) {
2019-11-20 04:35:24 +02:00
const mkv_track * track = & mkv - > tracks [ i ] ;
const AVStream * st = s - > streams [ i ] ;
2019-12-29 03:28:07 +02:00
if ( st - > codecpar - > codec_type ! = AVMEDIA_TYPE_ATTACHMENT )
continue ;
2016-10-07 07:46:22 +02:00
2022-06-15 11:25:12 +02:00
ret = mkv_write_tag ( mkv , st - > metadata , & mkv - > tags . bc , 0 ,
2020-04-29 07:52:56 +02:00
MATROSKA_ID_TAGTARGETS_ATTACHUID , track - > uid ) ;
2016-10-07 07:46:22 +02:00
if ( ret < 0 )
return ret ;
}
}
2020-04-29 06:31:07 +02:00
if ( mkv - > tags . bc ) {
2020-04-29 20:55:26 +02:00
return end_ebml_master_crc32_tentatively ( s - > pb , & mkv - > tags , mkv ,
2020-04-29 07:24:02 +02:00
MATROSKA_ID_TAGS ) ;
2016-10-04 00:27:30 +02:00
}
2010-10-05 12:40:43 +03:00
return 0 ;
}
2021-03-15 22:17:25 +02:00
static int mkv_new_chapter_ids_needed ( const AVFormatContext * s )
{
for ( unsigned i = 0 ; i < s - > nb_chapters ; i + + ) {
if ( ! s - > chapters [ i ] - > id )
return 1 ;
for ( unsigned j = 0 ; j < i ; j + + )
if ( s - > chapters [ j ] - > id = = s - > chapters [ i ] - > id )
return 1 ;
}
return 0 ;
}
2020-04-29 08:48:25 +02:00
static int mkv_write_chapters ( AVFormatContext * s )
{
MatroskaMuxContext * mkv = s - > priv_data ;
2020-04-29 10:27:23 +02:00
AVIOContext * dyn_cp = NULL , * dyn_tags = NULL , * * tags , * pb = s - > pb ;
2020-04-29 08:48:25 +02:00
ebml_master editionentry ;
AVRational scale = { 1 , 1E9 } ;
2021-03-15 22:17:25 +02:00
int ret , create_new_ids ;
2020-04-29 08:48:25 +02:00
if ( ! s - > nb_chapters | | mkv - > wrote_chapters )
return 0 ;
ret = start_ebml_master_crc32 ( & dyn_cp , mkv ) ;
if ( ret < 0 )
return ret ;
editionentry = start_ebml_master ( dyn_cp , MATROSKA_ID_EDITIONENTRY , 0 ) ;
2022-01-07 12:36:19 +02:00
if ( ! IS_WEBM ( mkv ) ) {
2020-04-29 08:48:25 +02:00
put_ebml_uint ( dyn_cp , MATROSKA_ID_EDITIONFLAGDEFAULT , 1 ) ;
2020-04-29 10:27:23 +02:00
/* If mkv_write_tags() has already been called, then any tags
* corresponding to chapters will be put into a new Tags element . */
tags = mkv - > wrote_tags ? & dyn_tags : & mkv - > tags . bc ;
} else
tags = NULL ;
2020-04-29 08:48:25 +02:00
2021-03-15 22:17:25 +02:00
create_new_ids = mkv_new_chapter_ids_needed ( s ) ;
for ( unsigned i = 0 ; i < s - > nb_chapters ; i + + ) {
2022-06-15 08:27:58 +02:00
AVChapter * const c = s - > chapters [ i ] ;
2020-04-29 08:48:25 +02:00
int64_t chapterstart = av_rescale_q ( c - > start , c - > time_base , scale ) ;
int64_t chapterend = av_rescale_q ( c - > end , c - > time_base , scale ) ;
const AVDictionaryEntry * t ;
2021-03-16 09:29:59 +02:00
uint64_t uid = create_new_ids ? i + 1ULL : c - > id ;
2022-01-14 15:11:22 +02:00
EBML_WRITER ( 7 ) ;
2020-04-29 08:48:25 +02:00
if ( chapterstart < 0 | | chapterstart > chapterend | | chapterend < 0 ) {
av_log ( s , AV_LOG_ERROR ,
" Invalid chapter start (% " PRId64 " ) or end (% " PRId64 " ). \n " ,
chapterstart , chapterend ) ;
2020-04-29 10:27:23 +02:00
ret = AVERROR_INVALIDDATA ;
goto fail ;
2020-04-29 08:48:25 +02:00
}
2022-01-14 15:11:22 +02:00
ebml_writer_open_master ( & writer , MATROSKA_ID_CHAPTERATOM ) ;
ebml_writer_add_uint ( & writer , MATROSKA_ID_CHAPTERUID , uid ) ;
ebml_writer_add_uint ( & writer , MATROSKA_ID_CHAPTERTIMESTART , chapterstart ) ;
ebml_writer_add_uint ( & writer , MATROSKA_ID_CHAPTERTIMEEND , chapterend ) ;
2020-04-29 08:48:25 +02:00
if ( ( t = av_dict_get ( c - > metadata , " title " , NULL , 0 ) ) ) {
2022-01-14 15:11:22 +02:00
ebml_writer_open_master ( & writer , MATROSKA_ID_CHAPTERDISPLAY ) ;
ebml_writer_add_string ( & writer , MATROSKA_ID_CHAPSTRING , t - > value ) ;
ebml_writer_add_string ( & writer , MATROSKA_ID_CHAPLANG , " und " ) ;
2020-04-29 08:48:25 +02:00
}
2022-01-14 15:11:22 +02:00
ret = ebml_writer_write ( & writer , dyn_cp ) ;
if ( ret < 0 )
goto fail ;
2020-04-29 10:27:23 +02:00
2022-06-15 08:27:58 +02:00
if ( tags ) {
ff_metadata_conv ( & c - > metadata , ff_mkv_metadata_conv , NULL ) ;
2022-06-15 11:49:48 +02:00
ret = mkv_write_tag ( mkv , c - > metadata , tags , 0 ,
MATROSKA_ID_TAGTARGETS_CHAPTERUID , uid ) ;
if ( ret < 0 )
goto fail ;
2020-04-29 10:27:23 +02:00
}
2020-04-29 08:48:25 +02:00
}
end_ebml_master ( dyn_cp , editionentry ) ;
mkv - > wrote_chapters = 1 ;
2020-04-29 10:27:23 +02:00
ret = end_ebml_master_crc32 ( pb , & dyn_cp , mkv , MATROSKA_ID_CHAPTERS , 0 , 0 , 1 ) ;
if ( ret < 0 )
goto fail ;
if ( dyn_tags )
return end_ebml_master_crc32 ( pb , & dyn_tags , mkv ,
MATROSKA_ID_TAGS , 0 , 0 , 1 ) ;
return 0 ;
fail :
if ( tags ) {
/* tags == &mkv->tags.bc can only happen if mkv->tags.bc was
* initially NULL , so we never free older tags . */
ffio_free_dyn_buf ( tags ) ;
}
ffio_free_dyn_buf ( & dyn_cp ) ;
return ret ;
2020-04-29 08:48:25 +02:00
}
2019-11-03 14:25:08 +02:00
static const char * get_mimetype ( const AVStream * st )
{
const AVDictionaryEntry * t ;
if ( t = av_dict_get ( st - > metadata , " mimetype " , NULL , 0 ) )
return t - > value ;
if ( st - > codecpar - > codec_id ! = AV_CODEC_ID_NONE ) {
const AVCodecDescriptor * desc = avcodec_descriptor_get ( st - > codecpar - > codec_id ) ;
if ( desc & & desc - > mime_types ) {
return desc - > mime_types [ 0 ] ;
} else if ( st - > codecpar - > codec_id = = AV_CODEC_ID_TEXT )
return " text/plain " ;
}
return NULL ;
}
2011-07-30 14:37:36 +03:00
static int mkv_write_attachments ( AVFormatContext * s )
{
MatroskaMuxContext * mkv = s - > priv_data ;
avformat/matroskaenc: Stop reallocating of Cluster buffer
The Matroska muxer uses a dynamic buffer to buffer the content of
Clusters before eventually writing them. Up until now, each time a
Cluster was written, the dynamic buffer was closed, i.e. freed; now it
is only reset, saving allocations of the AVIOContext itself, its opaque
as well as most of the reallocations of the buffer.
This is advantageous performance-wise, in particular on systems where
reallocations are slow (namely Windows). The following table shows the
decicyles for writing a frame on Linux (Ubuntu 19.10) and Windows (7)
on an x64 Haswell (to /dev/null on Linux, to stdout which is discarded
on Windows (the default values of the size and duration of clusters for
seekable output have been explicitly set in this case); in all tests,
writing CRC-32 values has been disabled in all tests; calls to the muxer's
write_packet function in write_packet() in libavformat/mux.c have been
timed; each of the following tests has been repeated 50 times):
| Windows before | Windows after | Linux before | Linux after
_________________________________________________________________
A | 979437 | 192304 | 259500 | 183320
B | 715936 | 155648 | 152786 | 130879
C | 265115 | 56034 | 78496 | 53243
D | 386224 | 80307 | 128894 | 75354
E | 21732 | 10695 | 11320 | 9801
(A is a 10.2 mb/s file with a GOP length of 2s, amounting to an average
Cluster size of about 2.5 MiB; the average Cluster size of B is 1.1 MiB;
for C it is 2.35 MiB, for D it is 0.46 MiB; for E - a file with just a
single audio track of 158kb/s resulting in a Cluster size of about 100
kB, the relative gains were the smallest, probably because of the small
Cluster size.)
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2019-12-29 02:01:52 +02:00
AVIOContext * dyn_cp = NULL , * pb = s - > pb ;
2011-07-30 14:37:36 +03:00
int i , ret ;
2019-12-29 04:26:05 +02:00
if ( ! mkv - > nb_attachments )
2011-07-30 14:37:36 +03:00
return 0 ;
2019-12-30 16:59:02 +02:00
ret = start_ebml_master_crc32 ( & dyn_cp , mkv ) ;
2019-12-29 04:36:56 +02:00
if ( ret < 0 )
return ret ;
2011-07-30 14:37:36 +03:00
for ( i = 0 ; i < s - > nb_streams ; i + + ) {
2019-11-20 04:35:24 +02:00
const AVStream * st = s - > streams [ i ] ;
2019-12-29 03:28:07 +02:00
mkv_track * track = & mkv - > tracks [ i ] ;
2022-01-14 02:09:13 +02:00
EBML_WRITER ( 6 ) ;
2019-11-20 04:35:24 +02:00
const AVDictionaryEntry * t ;
2019-11-03 14:25:08 +02:00
const char * mimetype ;
2011-07-30 14:37:36 +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 ( st - > codecpar - > codec_type ! = AVMEDIA_TYPE_ATTACHMENT )
2011-07-30 14:37:36 +03:00
continue ;
2022-01-14 02:09:13 +02:00
ebml_writer_open_master ( & writer , MATROSKA_ID_ATTACHEDFILE ) ;
2011-07-30 14:37:36 +03:00
if ( t = av_dict_get ( st - > metadata , " title " , NULL , 0 ) )
2022-01-14 02:09:13 +02:00
ebml_writer_add_string ( & writer , MATROSKA_ID_FILEDESC , t - > value ) ;
2011-07-30 14:37:36 +03:00
if ( ! ( t = av_dict_get ( st - > metadata , " filename " , NULL , 0 ) ) ) {
av_log ( s , AV_LOG_ERROR , " Attachment stream %d has no filename tag. \n " , i ) ;
2021-04-10 00:11:06 +02:00
ffio_free_dyn_buf ( & dyn_cp ) ;
2011-07-30 14:37:36 +03:00
return AVERROR ( EINVAL ) ;
}
2022-01-14 02:09:13 +02:00
ebml_writer_add_string ( & writer , MATROSKA_ID_FILENAME , t - > value ) ;
2011-07-30 14:37:36 +03:00
2019-11-03 14:25:08 +02:00
mimetype = get_mimetype ( st ) ;
av_assert0 ( mimetype ) ;
2022-01-14 02:09:13 +02:00
ebml_writer_add_string ( & writer , MATROSKA_ID_FILEMIMETYPE , mimetype ) ;
ebml_writer_add_bin ( & writer , MATROSKA_ID_FILEDATA ,
st - > codecpar - > extradata , st - > codecpar - > extradata_size ) ;
ebml_writer_add_uid ( & writer , MATROSKA_ID_FILEUID , track - > uid ) ;
ret = ebml_writer_write ( & writer , dyn_cp ) ;
if ( ret < 0 ) {
ffio_free_dyn_buf ( & dyn_cp ) ;
return ret ;
}
2011-07-30 14:37:36 +03:00
}
2020-04-29 20:55:26 +02:00
return end_ebml_master_crc32 ( pb , & dyn_cp , mkv ,
MATROSKA_ID_ATTACHMENTS , 0 , 0 , 1 ) ;
2011-07-30 14:37:36 +03:00
}
2016-07-17 04:19:41 +02:00
static int64_t get_metadata_duration ( AVFormatContext * s )
{
2019-12-29 04:36:56 +02:00
const AVDictionaryEntry * duration = av_dict_get ( s - > metadata , " DURATION " ,
NULL , 0 ) ;
2016-07-17 04:19:41 +02:00
int64_t max = 0 ;
int64_t us ;
2019-12-29 04:36:56 +02:00
if ( duration & & ( av_parse_time ( & us , duration - > value , 1 ) = = 0 ) & & us > 0 ) {
2016-07-17 04:19:41 +02:00
av_log ( s , AV_LOG_DEBUG , " get_metadata_duration found duration in context metadata: % " PRId64 " \n " , us ) ;
return us ;
}
2019-12-29 04:36:56 +02:00
for ( unsigned i = 0 ; i < s - > nb_streams ; i + + ) {
2016-07-17 04:19:41 +02:00
int64_t us ;
2019-12-29 04:36:56 +02:00
duration = av_dict_get ( s - > streams [ i ] - > metadata , " DURATION " , NULL , 0 ) ;
2016-07-17 04:19:41 +02:00
if ( duration & & ( av_parse_time ( & us , duration - > value , 1 ) = = 0 ) )
max = FFMAX ( max , us ) ;
}
av_log ( s , AV_LOG_DEBUG , " get_metadata_duration returned: % " PRId64 " \n " , max ) ;
return max ;
}
2022-01-14 02:18:24 +02:00
static void ebml_write_header ( AVIOContext * pb ,
const char * doctype , int version )
{
EBML_WRITER ( 8 ) ;
ebml_writer_open_master ( & writer , EBML_ID_HEADER ) ;
ebml_writer_add_uint ( & writer , EBML_ID_EBMLVERSION , 1 ) ;
ebml_writer_add_uint ( & writer , EBML_ID_EBMLREADVERSION , 1 ) ;
ebml_writer_add_uint ( & writer , EBML_ID_EBMLMAXIDLENGTH , 4 ) ;
ebml_writer_add_uint ( & writer , EBML_ID_EBMLMAXSIZELENGTH , 8 ) ;
ebml_writer_add_string ( & writer , EBML_ID_DOCTYPE , doctype ) ;
ebml_writer_add_uint ( & writer , EBML_ID_DOCTYPEVERSION , version ) ;
ebml_writer_add_uint ( & writer , EBML_ID_DOCTYPEREADVERSION , 2 ) ;
/* The size is bounded, so no need to check this. */
ebml_writer_write ( & writer , pb ) ;
}
2022-01-14 23:33:23 +02:00
static int mkv_write_info ( AVFormatContext * s )
2007-09-05 03:23:15 +03:00
{
MatroskaMuxContext * mkv = s - > priv_data ;
2019-11-20 04:35:24 +02:00
const AVDictionaryEntry * tag ;
2016-02-06 19:01:23 +02:00
int64_t creation_time ;
2022-01-14 23:33:23 +02:00
AVIOContext * pb ;
int ret = start_ebml_master_crc32 ( & mkv - > info . bc , mkv ) ;
2016-10-04 00:29:28 +02:00
if ( ret < 0 )
return ret ;
2020-04-29 06:31:07 +02:00
pb = mkv - > info . bc ;
2016-10-04 00:29:28 +02:00
2007-09-05 03:23:15 +03:00
put_ebml_uint ( pb , MATROSKA_ID_TIMECODESCALE , 1000000 ) ;
2011-05-22 13:46:29 +03:00
if ( ( tag = av_dict_get ( s - > metadata , " title " , NULL , 0 ) ) )
2009-02-15 18:53:05 +02:00
put_ebml_string ( pb , MATROSKA_ID_TITLE , tag - > value ) ;
2014-05-01 11:43:10 +03:00
if ( ! ( s - > flags & AVFMT_FLAG_BITEXACT ) ) {
2014-06-07 01:54:50 +03:00
put_ebml_string ( pb , MATROSKA_ID_MUXINGAPP , LIBAVFORMAT_IDENT ) ;
2014-03-03 23:20:14 +03:00
if ( ( tag = av_dict_get ( s - > metadata , " encoding_tool " , NULL , 0 ) ) )
put_ebml_string ( pb , MATROSKA_ID_WRITINGAPP , tag - > value ) ;
else
put_ebml_string ( pb , MATROSKA_ID_WRITINGAPP , LIBAVFORMAT_IDENT ) ;
2016-07-26 20:22:32 +02:00
2022-01-07 12:36:19 +02:00
if ( ! IS_WEBM ( mkv ) )
2020-01-18 10:27:25 +02:00
put_ebml_binary ( pb , MATROSKA_ID_SEGMENTUID , mkv - > segment_uid , 16 ) ;
2013-09-18 06:59:34 +03:00
} else {
const char * ident = " Lavf " ;
put_ebml_string ( pb , MATROSKA_ID_MUXINGAPP , ident ) ;
put_ebml_string ( pb , MATROSKA_ID_WRITINGAPP , ident ) ;
2007-09-05 03:23:15 +03:00
}
2007-09-05 03:24:03 +03:00
2016-02-06 19:01:23 +02:00
if ( ff_parse_creation_time_metadata ( s , & creation_time , 0 ) > 0 ) {
2012-03-05 21:02:48 +03:00
// Adjust time so it's relative to 2001-01-01 and convert to nanoseconds.
2016-02-06 19:01:23 +02:00
int64_t date_utc = ( creation_time - 978307200000000LL ) * 1000 ;
2012-03-05 21:02:48 +03:00
uint8_t date_utc_buf [ 8 ] ;
AV_WB64 ( date_utc_buf , date_utc ) ;
put_ebml_binary ( pb , MATROSKA_ID_DATEUTC , date_utc_buf , 8 ) ;
}
2007-09-05 03:23:15 +03:00
// reserve space for the duration
mkv - > duration = 0 ;
2011-03-03 21:11:45 +02:00
mkv - > duration_offset = avio_tell ( pb ) ;
2015-03-30 23:46:10 +02:00
if ( ! mkv - > is_live ) {
2016-07-17 04:19:41 +02:00
int64_t metadata_duration = get_metadata_duration ( s ) ;
if ( s - > duration > 0 ) {
int64_t scaledDuration = av_rescale ( s - > duration , 1000 , AV_TIME_BASE ) ;
put_ebml_float ( pb , MATROSKA_ID_DURATION , scaledDuration ) ;
av_log ( s , AV_LOG_DEBUG , " Write early duration from recording time = % " PRIu64 " \n " , scaledDuration ) ;
} else if ( metadata_duration > 0 ) {
int64_t scaledDuration = av_rescale ( metadata_duration , 1000 , AV_TIME_BASE ) ;
put_ebml_float ( pb , MATROSKA_ID_DURATION , scaledDuration ) ;
av_log ( s , AV_LOG_DEBUG , " Write early duration from metadata = % " PRIu64 " \n " , scaledDuration ) ;
2020-05-02 17:46:09 +02:00
} else if ( s - > pb - > seekable & AVIO_SEEKABLE_NORMAL ) {
2016-07-17 04:19:41 +02:00
put_ebml_void ( pb , 11 ) ; // assumes double-precision float to be written
}
2015-03-30 23:46:10 +02:00
}
2022-01-14 23:33:23 +02:00
return end_ebml_master_crc32_tentatively ( s - > pb , & mkv - > info ,
mkv , MATROSKA_ID_INFO ) ;
}
static int mkv_write_header ( AVFormatContext * s )
{
MatroskaMuxContext * mkv = s - > priv_data ;
AVIOContext * pb = s - > pb ;
int ret , version = 2 ;
2022-06-15 11:25:12 +02:00
ret = avio_open_dyn_buf ( & mkv - > tmp_bc ) ;
if ( ret < 0 )
return ret ;
2022-01-14 23:33:23 +02:00
if ( ! IS_WEBM ( mkv ) | |
av_dict_get ( s - > metadata , " stereo_mode " , NULL , 0 ) | |
av_dict_get ( s - > metadata , " alpha_mode " , NULL , 0 ) )
version = 4 ;
for ( unsigned i = 0 ; i < s - > nb_streams ; i + + ) {
if ( s - > streams [ i ] - > codecpar - > codec_id = = AV_CODEC_ID_OPUS | |
av_dict_get ( s - > streams [ i ] - > metadata , " stereo_mode " , NULL , 0 ) | |
av_dict_get ( s - > streams [ i ] - > metadata , " alpha_mode " , NULL , 0 ) )
version = 4 ;
}
ebml_write_header ( pb , s - > oformat - > name , version ) ;
put_ebml_id ( pb , MATROSKA_ID_SEGMENT ) ;
put_ebml_size_unknown ( pb , 8 ) ;
mkv - > segment_offset = avio_tell ( pb ) ;
// We write a SeekHead at the beginning to point to all other level
// one elements (except Clusters).
mkv_start_seekhead ( mkv , pb ) ;
ret = mkv_write_info ( s ) ;
2020-04-29 20:55:26 +02:00
if ( ret < 0 )
return ret ;
2007-09-05 03:23:15 +03:00
2007-09-05 03:25:32 +03:00
ret = mkv_write_tracks ( s ) ;
2014-06-07 01:54:50 +03:00
if ( ret < 0 )
2019-10-16 03:57:08 +02:00
return ret ;
2007-09-05 03:22:37 +03:00
2017-06-02 04:42:05 +02:00
ret = mkv_write_chapters ( s ) ;
if ( ret < 0 )
2019-10-16 03:57:08 +02:00
return ret ;
2010-10-05 12:40:43 +03:00
2022-01-07 12:36:19 +02:00
if ( ! IS_WEBM ( mkv ) ) {
2016-10-07 07:46:22 +02:00
ret = mkv_write_attachments ( s ) ;
2014-06-07 01:54:50 +03:00
if ( ret < 0 )
2019-10-16 03:57:08 +02:00
return ret ;
2010-06-05 01:40:54 +03:00
}
2009-10-18 15:17:01 +03:00
2020-04-29 10:27:23 +02:00
/* Must come after mkv_write_chapters() to write chapter tags
* into the same Tags element as the other tags . */
2017-06-02 04:47:32 +02:00
ret = mkv_write_tags ( s ) ;
if ( ret < 0 )
2019-10-16 03:57:08 +02:00
return ret ;
2017-06-02 04:47:32 +02:00
2020-04-29 07:24:02 +02:00
if ( ! IS_SEEKABLE ( pb , mkv ) ) {
2019-12-29 13:50:46 +02:00
ret = mkv_write_seekhead ( pb , mkv , 0 , avio_tell ( pb ) ) ;
2019-12-29 10:57:44 +02:00
if ( ret < 0 )
return ret ;
}
2010-03-04 10:53:04 +02:00
2018-09-12 17:47:47 +02:00
if ( s - > metadata_header_padding > 0 ) {
if ( s - > metadata_header_padding = = 1 )
s - > metadata_header_padding + + ;
put_ebml_void ( pb , s - > metadata_header_padding ) ;
}
2022-01-09 20:45:58 +02:00
if ( mkv - > reserve_cues_space | | mkv - > move_cues_to_front ) {
avformat/matroskaenc: Remove inconsistencies wrt seekability handling
The Matroska muxer behaves differently in several ways when it thinks
that it is in unseekable/livestreaming mode: It does not add Cue entries
because they won't be written anyway for a livestream and it writes some
elements only preliminarily (with the intention to overwrite them with
an updated version at the end) when non-livestreaming etc.
There are two ways to set the Matroska muxer into livestreaming mode:
Setting an option or by providing an unseekable AVIOContext. Yet the
actual checks were not consistent:
If the AVIOContext was unseekable and no AAC extradata was available
when writing the header, writing the header failed; but if the AVIOContext
was seekable, it didn't, because the muxer expected to get the extradata
via packet side-data. Here the livestreaming option has not been checked,
although one can't use the updated extradata in case it is a livestream.
If the reserve_index_space option was used, space for writing Cues would
be reserved when writing the header unless the AVIOContext was
unseekable. Yet Cues were only written if the livestreaming option was
not set and the AVIOContext was seekable (when writing the trailer), so
if the AVIOContext was seekable and the livestreaming option set, the
reserved space would never be used at all.
If the AVIOContext was unseekable and the livestreaming option was not
set, it would be attempted to update the main length field at the end.
After all, it might be possible that the file is so short that it fits
into the AVIOContext's buffer in which case the seek back would work.
Yet this is dangerous: It might be that we are not dealing with a
simple output file, but that our output gets split into chunks and that
each of these chunks is actually seekable. In this case some part of the
last chunk (namely the eight bytes that have the same offset as the
length field had in the header) will be overwritten with what the muxer
wrongly believes to be the filesize.
(The livestreaming option has been added to deal with this scenario,
yet its documentation ("Write files assuming it is a live stream.")
doesn't make this clear at all. At least the segment muxer does not
set the option for live and given that the chances of successfully
seeking when the output is actually unseekable are slim, it is best to
not attempt to update the length field in the unseekable case at all.)
All these inconsistencies were fixed by treating the output as seekable
if the livestreaming option is not set and if the AVIOContext is
seekable. A macro has been used to enforce consistency and improve code
readability.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-05-01 22:31:36 +02:00
if ( IS_SEEKABLE ( pb , mkv ) ) {
2020-04-29 07:24:02 +02:00
mkv - > cues_pos = avio_tell ( pb ) ;
2022-01-09 20:45:58 +02:00
if ( mkv - > reserve_cues_space > = 1 ) {
if ( mkv - > reserve_cues_space = = 1 )
mkv - > reserve_cues_space + + ;
put_ebml_void ( pb , mkv - > reserve_cues_space ) ;
}
2020-05-02 01:47:17 +02:00
} else
mkv - > reserve_cues_space = - 1 ;
2013-04-30 09:36:20 +03:00
}
2012-03-14 23:40:29 +03:00
mkv - > cluster_pos = - 1 ;
2010-06-05 01:40:50 +03:00
2013-08-18 21:09:40 +03:00
// start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
// after 4k and on a keyframe
avformat/matroskaenc: Remove inconsistencies wrt seekability handling
The Matroska muxer behaves differently in several ways when it thinks
that it is in unseekable/livestreaming mode: It does not add Cue entries
because they won't be written anyway for a livestream and it writes some
elements only preliminarily (with the intention to overwrite them with
an updated version at the end) when non-livestreaming etc.
There are two ways to set the Matroska muxer into livestreaming mode:
Setting an option or by providing an unseekable AVIOContext. Yet the
actual checks were not consistent:
If the AVIOContext was unseekable and no AAC extradata was available
when writing the header, writing the header failed; but if the AVIOContext
was seekable, it didn't, because the muxer expected to get the extradata
via packet side-data. Here the livestreaming option has not been checked,
although one can't use the updated extradata in case it is a livestream.
If the reserve_index_space option was used, space for writing Cues would
be reserved when writing the header unless the AVIOContext was
unseekable. Yet Cues were only written if the livestreaming option was
not set and the AVIOContext was seekable (when writing the trailer), so
if the AVIOContext was seekable and the livestreaming option set, the
reserved space would never be used at all.
If the AVIOContext was unseekable and the livestreaming option was not
set, it would be attempted to update the main length field at the end.
After all, it might be possible that the file is so short that it fits
into the AVIOContext's buffer in which case the seek back would work.
Yet this is dangerous: It might be that we are not dealing with a
simple output file, but that our output gets split into chunks and that
each of these chunks is actually seekable. In this case some part of the
last chunk (namely the eight bytes that have the same offset as the
length field had in the header) will be overwritten with what the muxer
wrongly believes to be the filesize.
(The livestreaming option has been added to deal with this scenario,
yet its documentation ("Write files assuming it is a live stream.")
doesn't make this clear at all. At least the segment muxer does not
set the option for live and given that the chances of successfully
seeking when the output is actually unseekable are slim, it is best to
not attempt to update the length field in the unseekable case at all.)
All these inconsistencies were fixed by treating the output as seekable
if the livestreaming option is not set and if the AVIOContext is
seekable. A macro has been used to enforce consistency and improve code
readability.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-05-01 22:31:36 +02:00
if ( IS_SEEKABLE ( pb , mkv ) ) {
2013-08-18 21:09:40 +03:00
if ( mkv - > cluster_time_limit < 0 )
mkv - > cluster_time_limit = 5000 ;
if ( mkv - > cluster_size_limit < 0 )
mkv - > cluster_size_limit = 5 * 1024 * 1024 ;
} else {
if ( mkv - > cluster_time_limit < 0 )
mkv - > cluster_time_limit = 1000 ;
if ( mkv - > cluster_size_limit < 0 )
mkv - > cluster_size_limit = 32 * 1024 ;
}
2007-09-05 03:22:37 +03:00
return 0 ;
}
2022-01-07 12:36:19 +02:00
# if CONFIG_MATROSKA_MUXER
2022-01-16 02:38:41 +02:00
static int mkv_reformat_h2645 ( MatroskaMuxContext * mkv , AVIOContext * pb ,
const AVPacket * pkt , int * size )
{
int ret ;
if ( pb ) {
2022-01-16 15:01:28 +02:00
ff_nal_units_write_list ( & mkv - > cur_block . h2645_nalu_list , pb , pkt - > data ) ;
2022-01-16 02:38:41 +02:00
} else {
2022-01-16 15:01:28 +02:00
ret = ff_nal_units_create_list ( & mkv - > cur_block . h2645_nalu_list , pkt - > data , pkt - > size ) ;
2022-01-16 02:38:41 +02:00
if ( ret < 0 )
return ret ;
* size = ret ;
}
return 0 ;
}
2022-01-16 00:52:13 +02:00
static int mkv_reformat_wavpack ( MatroskaMuxContext * mkv , AVIOContext * pb ,
const AVPacket * pkt , int * size )
2013-05-28 11:09:21 +03:00
{
2022-01-16 00:52:13 +02:00
const uint8_t * src = pkt - > data ;
int srclen = pkt - > size ;
2013-05-28 11:09:21 +03:00
int offset = 0 ;
int ret ;
while ( srclen > = WV_HEADER_SIZE ) {
WvHeader header ;
ret = ff_wv_parse_header ( & header , src ) ;
if ( ret < 0 )
2022-01-16 00:52:13 +02:00
return ret ;
2013-05-28 11:09:21 +03:00
src + = WV_HEADER_SIZE ;
srclen - = WV_HEADER_SIZE ;
2022-01-16 00:52:13 +02:00
if ( srclen < header . blocksize )
return AVERROR_INVALIDDATA ;
2013-05-28 11:09:21 +03:00
2022-01-16 00:52:13 +02:00
offset + = 4 * ! ! header . initial + 8 + 4 * ! ( header . initial & & header . final ) ;
if ( pb ) {
if ( header . initial )
avio_wl32 ( pb , header . samples ) ;
avio_wl32 ( pb , header . flags ) ;
avio_wl32 ( pb , header . crc ) ;
2013-05-28 11:09:21 +03:00
2022-01-16 00:52:13 +02:00
if ( ! ( header . initial & & header . final ) )
avio_wl32 ( pb , header . blocksize ) ;
2013-05-28 11:09:21 +03:00
2022-01-16 00:52:13 +02:00
avio_write ( pb , src , header . blocksize ) ;
}
2013-05-28 11:09:21 +03:00
src + = header . blocksize ;
srclen - = header . blocksize ;
offset + = header . blocksize ;
}
* size = offset ;
return 0 ;
}
2022-01-07 12:36:19 +02:00
# endif
2013-05-28 11:09:21 +03:00
2022-01-16 01:12:29 +02:00
static int mkv_reformat_av1 ( MatroskaMuxContext * mkv , AVIOContext * pb ,
const AVPacket * pkt , int * size )
{
int ret = ff_av1_filter_obus ( pb , pkt - > data , pkt - > size ) ;
if ( ret < 0 )
return ret ;
* size = ret ;
return 0 ;
}
2022-01-16 17:24:42 +02:00
static int webm_reformat_vtt ( MatroskaMuxContext * mkv , AVIOContext * pb ,
const AVPacket * pkt , int * size )
{
const uint8_t * id , * settings ;
size_t id_size , settings_size ;
unsigned total = pkt - > size + 2U ;
if ( total > INT_MAX )
return AVERROR ( ERANGE ) ;
id = av_packet_get_side_data ( pkt , AV_PKT_DATA_WEBVTT_IDENTIFIER ,
& id_size ) ;
settings = av_packet_get_side_data ( pkt , AV_PKT_DATA_WEBVTT_SETTINGS ,
& settings_size ) ;
if ( id_size > INT_MAX - total | | settings_size > INT_MAX - ( total + = id_size ) )
return AVERROR ( ERANGE ) ;
* size = total + = settings_size ;
if ( pb ) {
avio_write ( pb , id , id_size ) ;
avio_w8 ( pb , ' \n ' ) ;
avio_write ( pb , settings , settings_size ) ;
avio_w8 ( pb , ' \n ' ) ;
avio_write ( pb , pkt - > data , pkt - > size ) ;
}
return 0 ;
}
2022-01-16 12:31:40 +02:00
static int mkv_write_block ( void * logctx , MatroskaMuxContext * mkv ,
AVIOContext * pb , const AVCodecParameters * par ,
mkv_track * track , const AVPacket * pkt ,
2022-01-16 17:24:42 +02:00
int keyframe , int64_t ts , uint64_t duration ,
2022-01-16 23:00:49 +02:00
int force_blockgroup , int64_t relative_packet_pos )
2007-09-05 03:23:47 +03:00
{
2022-01-16 15:01:28 +02:00
uint8_t * side_data ;
2021-04-14 14:59:32 +02:00
size_t side_data_size ;
2019-11-20 16:10:34 +02:00
uint64_t additional_id ;
2013-11-14 14:58:28 +03:00
int64_t discard_padding = 0 ;
2019-11-27 06:00:04 +02:00
unsigned track_number = track - > track_num ;
2022-01-16 15:01:28 +02:00
EBML_WRITER ( 9 ) ;
mkv - > cur_block . track = track ;
mkv - > cur_block . pkt = pkt ;
mkv - > cur_block . rel_ts = ts - mkv - > cluster_pts ;
mkv - > cur_block . flags = 0 ;
/* Open a BlockGroup with a Block now; it will later be converted
* to a SimpleBlock if possible . */
ebml_writer_open_master ( & writer , MATROSKA_ID_BLOCKGROUP ) ;
ebml_writer_add_block ( & writer , mkv ) ;
if ( duration )
ebml_writer_add_uint ( & writer , MATROSKA_ID_BLOCKDURATION , duration ) ;
2007-09-05 03:23:47 +03:00
2022-01-16 12:31:40 +02:00
av_log ( logctx , AV_LOG_DEBUG ,
2019-04-23 21:45:52 +02:00
" Writing block of size %d with pts % " PRId64 " , dts % " PRId64 " , "
" duration % " PRId64 " at relative offset % " PRId64 " in cluster "
2019-11-27 06:00:04 +02:00
" at offset % " PRId64 " . TrackNumber %u, keyframe %d \n " ,
2022-01-16 23:00:49 +02:00
pkt - > size , pkt - > pts , pkt - > dts , pkt - > duration , relative_packet_pos ,
2019-04-23 21:45:52 +02:00
mkv - > cluster_pos , track_number , keyframe ! = 0 ) ;
2020-01-23 18:08:28 +02:00
2013-08-30 21:26:37 +03:00
side_data = av_packet_get_side_data ( pkt ,
AV_PKT_DATA_SKIP_SAMPLES ,
& side_data_size ) ;
2019-12-29 04:56:34 +02:00
if ( side_data & & side_data_size > = 10 ) {
2013-08-30 21:26:37 +03:00
discard_padding = av_rescale_q ( AV_RL32 ( side_data + 4 ) ,
2016-04-10 21:58:15 +02:00
( AVRational ) { 1 , par - > sample_rate } ,
2013-08-30 21:26:37 +03:00
( AVRational ) { 1 , 1000000000 } ) ;
2022-01-16 15:01:28 +02:00
ebml_writer_add_sint ( & writer , MATROSKA_ID_DISCARDPADDING , discard_padding ) ;
2013-08-30 21:26:37 +03:00
}
2013-05-09 02:59:32 +03:00
side_data = av_packet_get_side_data ( pkt ,
AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL ,
& side_data_size ) ;
2022-01-16 15:01:28 +02:00
if ( side_data & & side_data_size > = 8 & &
2019-11-20 16:10:34 +02:00
// Only the Codec-specific BlockMore (id == 1) is currently supported.
2022-01-16 15:01:28 +02:00
( additional_id = AV_RB64 ( side_data ) ) = = 1 ) {
ebml_writer_open_master ( & writer , MATROSKA_ID_BLOCKADDITIONS ) ;
ebml_writer_open_master ( & writer , MATROSKA_ID_BLOCKMORE ) ;
2019-11-20 16:10:34 +02:00
/* Until dbc50f8a our demuxer used a wrong default value
* of BlockAddID , so we write it unconditionally . */
2022-01-16 15:01:28 +02:00
ebml_writer_add_uint ( & writer , MATROSKA_ID_BLOCKADDID , additional_id ) ;
ebml_writer_add_bin ( & writer , MATROSKA_ID_BLOCKADDITIONAL ,
side_data + 8 , side_data_size - 8 ) ;
ebml_writer_close_master ( & writer ) ;
ebml_writer_close_master ( & writer ) ;
}
2022-01-16 17:24:42 +02:00
if ( ! force_blockgroup & & writer . nb_elements = = 2 ) {
2022-01-16 15:01:28 +02:00
/* Nothing except the BlockGroup + Block. Can use a SimpleBlock. */
writer . elements + + ; // Skip the BlockGroup.
writer . nb_elements - - ;
av_assert2 ( writer . elements [ 0 ] . id = = MATROSKA_ID_BLOCK ) ;
writer . elements [ 0 ] . id = MATROSKA_ID_SIMPLEBLOCK ;
if ( keyframe )
mkv - > cur_block . flags | = 1 < < 7 ;
} else if ( ! keyframe )
ebml_writer_add_sint ( & writer , MATROSKA_ID_BLOCKREFERENCE ,
track - > last_timestamp - ts ) ;
2020-01-23 18:08:27 +02:00
2022-01-16 15:01:28 +02:00
return ebml_writer_write ( & writer , pb ) ;
2007-09-05 03:23:47 +03:00
}
2020-04-29 20:55:26 +02:00
static int mkv_end_cluster ( AVFormatContext * s )
2014-07-24 19:19:11 +03:00
{
MatroskaMuxContext * mkv = s - > priv_data ;
2020-04-29 20:55:26 +02:00
int ret ;
2014-07-24 19:19:11 +03:00
2019-12-29 04:14:53 +02:00
if ( ! mkv - > have_video ) {
for ( unsigned i = 0 ; i < s - > nb_streams ; i + + )
mkv - > tracks [ i ] . has_cue = 0 ;
}
2016-10-02 19:37:11 +02:00
mkv - > cluster_pos = - 1 ;
2020-04-29 20:55:26 +02:00
ret = end_ebml_master_crc32 ( s - > pb , & mkv - > cluster_bc , mkv ,
MATROSKA_ID_CLUSTER , 0 , 1 , 0 ) ;
if ( ret < 0 )
return ret ;
2019-12-27 14:53:00 +02:00
avio_write_marker ( s - > pb , AV_NOPTS_VALUE , AVIO_DATA_MARKER_FLUSH_POINT ) ;
2020-04-29 20:55:26 +02:00
return 0 ;
2014-07-24 19:19:11 +03:00
}
2019-11-20 04:35:24 +02:00
static int mkv_check_new_extra_data ( AVFormatContext * s , const AVPacket * pkt )
2016-11-01 01:04:46 +02:00
{
MatroskaMuxContext * mkv = s - > priv_data ;
mkv_track * track = & mkv - > tracks [ pkt - > stream_index ] ;
AVCodecParameters * par = s - > streams [ pkt - > stream_index ] - > codecpar ;
uint8_t * side_data ;
2021-04-14 14:59:32 +02:00
size_t side_data_size ;
2021-03-06 18:29:54 +02:00
int ret ;
2016-11-01 01:04:46 +02:00
side_data = av_packet_get_side_data ( pkt , AV_PKT_DATA_NEW_EXTRADATA ,
& side_data_size ) ;
switch ( par - > codec_id ) {
2022-01-07 12:36:19 +02:00
# if CONFIG_MATROSKA_MUXER
2017-04-13 17:43:11 +02:00
case AV_CODEC_ID_AAC :
2020-05-01 20:21:45 +02:00
if ( side_data_size & & mkv - > track . bc ) {
2022-06-17 05:49:06 +02:00
int output_sample_rate = 0 ;
avformat/matroskaenc: Remove inconsistencies wrt seekability handling
The Matroska muxer behaves differently in several ways when it thinks
that it is in unseekable/livestreaming mode: It does not add Cue entries
because they won't be written anyway for a livestream and it writes some
elements only preliminarily (with the intention to overwrite them with
an updated version at the end) when non-livestreaming etc.
There are two ways to set the Matroska muxer into livestreaming mode:
Setting an option or by providing an unseekable AVIOContext. Yet the
actual checks were not consistent:
If the AVIOContext was unseekable and no AAC extradata was available
when writing the header, writing the header failed; but if the AVIOContext
was seekable, it didn't, because the muxer expected to get the extradata
via packet side-data. Here the livestreaming option has not been checked,
although one can't use the updated extradata in case it is a livestream.
If the reserve_index_space option was used, space for writing Cues would
be reserved when writing the header unless the AVIOContext was
unseekable. Yet Cues were only written if the livestreaming option was
not set and the AVIOContext was seekable (when writing the trailer), so
if the AVIOContext was seekable and the livestreaming option set, the
reserved space would never be used at all.
If the AVIOContext was unseekable and the livestreaming option was not
set, it would be attempted to update the main length field at the end.
After all, it might be possible that the file is so short that it fits
into the AVIOContext's buffer in which case the seek back would work.
Yet this is dangerous: It might be that we are not dealing with a
simple output file, but that our output gets split into chunks and that
each of these chunks is actually seekable. In this case some part of the
last chunk (namely the eight bytes that have the same offset as the
length field had in the header) will be overwritten with what the muxer
wrongly believes to be the filesize.
(The livestreaming option has been added to deal with this scenario,
yet its documentation ("Write files assuming it is a live stream.")
doesn't make this clear at all. At least the segment muxer does not
set the option for live and given that the chances of successfully
seeking when the output is actually unseekable are slim, it is best to
not attempt to update the length field in the unseekable case at all.)
All these inconsistencies were fixed by treating the output as seekable
if the livestreaming option is not set and if the AVIOContext is
seekable. A macro has been used to enforce consistency and improve code
readability.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-05-01 22:31:36 +02:00
ret = get_aac_sample_rates ( s , mkv , side_data , side_data_size ,
& track - > sample_rate , & output_sample_rate ) ;
2017-04-13 17:43:11 +02:00
if ( ret < 0 )
return ret ;
if ( ! output_sample_rate )
output_sample_rate = track - > sample_rate ; // Space is already reserved, so it's this or a void element.
2022-06-17 05:49:06 +02:00
ret = mkv_update_codecprivate ( s , mkv , side_data , side_data_size ,
par , mkv - > track . bc , track ) ;
2017-04-13 17:43:11 +02:00
if ( ret < 0 )
return ret ;
2020-04-29 06:31:07 +02:00
avio_seek ( mkv - > track . bc , track - > sample_rate_offset , SEEK_SET ) ;
put_ebml_float ( mkv - > track . bc , MATROSKA_ID_AUDIOSAMPLINGFREQ , track - > sample_rate ) ;
put_ebml_float ( mkv - > track . bc , MATROSKA_ID_AUDIOOUTSAMPLINGFREQ , output_sample_rate ) ;
2017-04-13 17:43:11 +02:00
} else if ( ! par - > extradata_size & & ! track - > sample_rate ) {
// No extradata (codecpar or packet side data).
av_log ( s , AV_LOG_ERROR , " Error parsing AAC extradata, unable to determine samplerate. \n " ) ;
return AVERROR ( EINVAL ) ;
}
break ;
2016-11-01 01:04:46 +02:00
case AV_CODEC_ID_FLAC :
2020-05-01 20:21:45 +02:00
if ( side_data_size & & mkv - > track . bc ) {
2016-11-01 01:04:46 +02:00
if ( side_data_size ! = par - > extradata_size ) {
av_log ( s , AV_LOG_ERROR , " Invalid FLAC STREAMINFO metadata for output stream %d \n " ,
pkt - > stream_index ) ;
return AVERROR ( EINVAL ) ;
}
2022-06-17 05:49:06 +02:00
ret = mkv_update_codecprivate ( s , mkv , side_data , side_data_size ,
par , mkv - > track . bc , track ) ;
if ( ret < 0 )
return ret ;
2016-11-01 01:04:46 +02:00
}
break ;
2022-01-07 12:36:19 +02:00
# endif
2018-08-11 04:25:32 +02:00
// FIXME: Remove the following once libaom starts propagating extradata during init()
// See https://bugs.chromium.org/p/aomedia/issues/detail?id=2012
case AV_CODEC_ID_AV1 :
2020-05-01 20:21:45 +02:00
if ( side_data_size & & mkv - > track . bc & & ! par - > extradata_size ) {
2018-08-11 04:25:32 +02:00
AVIOContext * dyn_cp ;
uint8_t * codecpriv ;
int codecpriv_size ;
ret = avio_open_dyn_buf ( & dyn_cp ) ;
if ( ret < 0 )
return ret ;
2022-05-02 23:36:28 +02:00
ff_isom_write_av1c ( dyn_cp , side_data , side_data_size , 1 ) ;
2020-04-29 20:55:26 +02:00
codecpriv_size = avio_get_dyn_buf ( dyn_cp , & codecpriv ) ;
if ( ( ret = dyn_cp - > error ) < 0 | |
! codecpriv_size & & ( ret = AVERROR_INVALIDDATA ) ) {
ffio_free_dyn_buf ( & dyn_cp ) ;
return ret ;
2018-08-11 04:25:32 +02:00
}
2020-04-29 06:31:07 +02:00
avio_seek ( mkv - > track . bc , track - > codecpriv_offset , SEEK_SET ) ;
2018-08-11 04:25:32 +02:00
// Do not write the OBUs as we don't have space saved for them
2020-04-29 06:31:07 +02:00
put_ebml_binary ( mkv - > track . bc , MATROSKA_ID_CODECPRIVATE , codecpriv , 4 ) ;
2020-04-29 20:55:26 +02:00
ffio_free_dyn_buf ( & dyn_cp ) ;
2018-08-11 04:25:32 +02:00
ret = ff_alloc_extradata ( par , side_data_size ) ;
if ( ret < 0 )
return ret ;
memcpy ( par - > extradata , side_data , side_data_size ) ;
} else if ( ! par - > extradata_size )
return AVERROR_INVALIDDATA ;
break ;
2016-11-01 01:04:46 +02:00
default :
if ( side_data_size )
av_log ( s , AV_LOG_DEBUG , " Ignoring new extradata in a packet for stream %d. \n " , pkt - > stream_index ) ;
break ;
}
return 0 ;
}
2019-11-20 04:35:24 +02:00
static int mkv_write_packet_internal ( AVFormatContext * s , const AVPacket * pkt )
2007-09-05 03:22:37 +03:00
{
2007-09-05 03:23:00 +03:00
MatroskaMuxContext * mkv = s - > priv_data ;
2019-10-15 13:17:34 +02:00
AVIOContext * pb ;
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
AVCodecParameters * par = s - > streams [ pkt - > stream_index ] - > codecpar ;
2019-10-16 04:00:56 +02:00
mkv_track * track = & mkv - > tracks [ pkt - > stream_index ] ;
2019-12-29 04:56:34 +02:00
int is_sub = par - > codec_type = = AVMEDIA_TYPE_SUBTITLE ;
/* All subtitle blocks are considered to be keyframes. */
int keyframe = is_sub | | ! ! ( pkt - > flags & AV_PKT_FLAG_KEY ) ;
int64_t duration = FFMAX ( pkt - > duration , 0 ) ;
int64_t write_duration = is_sub ? duration : 0 ;
2007-09-05 03:25:32 +03:00
int ret ;
2019-10-15 13:17:34 +02:00
int64_t ts = track - > write_dts ? pkt - > dts : pkt - > pts ;
2013-07-23 10:52:08 +03:00
int64_t relative_packet_pos ;
2007-09-05 03:22:52 +03:00
2010-03-09 06:23:33 +02:00
if ( ts = = AV_NOPTS_VALUE ) {
av_log ( s , AV_LOG_ERROR , " Can't write packet with unknown timestamp \n " ) ;
return AVERROR ( EINVAL ) ;
}
2019-10-15 13:17:34 +02:00
ts + = track - > ts_offset ;
2010-03-09 06:23:33 +02:00
2014-07-24 19:19:50 +03:00
if ( mkv - > cluster_pos ! = - 1 ) {
2019-04-20 01:40:56 +02:00
int64_t cluster_time = ts - mkv - > cluster_pts ;
2014-07-24 19:19:50 +03:00
if ( ( int16_t ) cluster_time ! = cluster_time ) {
2020-04-29 20:55:26 +02:00
ret = mkv_end_cluster ( s ) ;
if ( ret < 0 )
return ret ;
2014-07-24 21:25:29 +03:00
av_log ( s , AV_LOG_WARNING , " Starting new cluster due to timestamp \n " ) ;
2014-07-24 19:19:50 +03:00
}
}
2012-03-14 23:40:29 +03:00
if ( mkv - > cluster_pos = = - 1 ) {
2019-12-30 16:59:02 +02:00
ret = start_ebml_master_crc32 ( & mkv - > cluster_bc , mkv ) ;
2016-10-03 04:46:19 +02:00
if ( ret < 0 )
return ret ;
2019-11-26 10:25:02 +02:00
mkv - > cluster_bc - > direct = 1 ;
2019-11-26 05:40:29 +02:00
mkv - > cluster_pos = avio_tell ( s - > pb ) ;
2019-04-20 01:41:07 +02:00
put_ebml_uint ( mkv - > cluster_bc , MATROSKA_ID_CLUSTERTIMECODE , FFMAX ( 0 , ts ) ) ;
2010-03-09 06:23:36 +02:00
mkv - > cluster_pts = FFMAX ( 0 , ts ) ;
2019-10-15 13:17:33 +02:00
av_log ( s , AV_LOG_DEBUG ,
" Starting new cluster with timestamp "
" % " PRId64 " at offset % " PRId64 " bytes \n " ,
mkv - > cluster_pts , mkv - > cluster_pos ) ;
2007-09-05 03:23:00 +03:00
}
2019-04-20 01:41:07 +02:00
pb = mkv - > cluster_bc ;
2007-09-05 03:23:00 +03:00
2016-10-25 17:12:03 +02:00
relative_packet_pos = avio_tell ( pb ) ;
2013-07-23 10:52:08 +03:00
2022-01-16 17:24:42 +02:00
/* The WebM spec requires WebVTT to be muxed in BlockGroups;
* so we force it even for packets without duration . */
2022-01-16 17:43:33 +02:00
ret = mkv_write_block ( s , mkv , pb , par , track , pkt ,
keyframe , ts , write_duration ,
2022-01-16 23:00:49 +02:00
par - > codec_id = = AV_CODEC_ID_WEBVTT ,
relative_packet_pos ) ;
2022-01-16 17:43:33 +02:00
if ( ret < 0 )
return ret ;
if ( keyframe & & IS_SEEKABLE ( s - > pb , mkv ) & &
( par - > codec_type = = AVMEDIA_TYPE_VIDEO | |
par - > codec_type = = AVMEDIA_TYPE_SUBTITLE | |
! mkv - > have_video & & ! track - > has_cue ) ) {
ret = mkv_add_cuepoint ( mkv , pkt - > stream_index , ts ,
mkv - > cluster_pos , relative_packet_pos ,
write_duration ) ;
2020-01-23 18:08:27 +02:00
if ( ret < 0 )
return ret ;
2022-01-16 17:43:33 +02:00
track - > has_cue = 1 ;
}
2007-09-05 03:23:27 +03:00
2022-01-16 15:01:28 +02:00
track - > last_timestamp = ts ;
2019-10-15 13:17:34 +02:00
mkv - > duration = FFMAX ( mkv - > duration , ts + duration ) ;
2019-10-16 04:00:56 +02:00
track - > duration = FFMAX ( track - > duration , ts + duration ) ;
2015-08-05 07:09:57 +02:00
2010-06-05 01:40:50 +03:00
return 0 ;
}
2019-11-20 04:35:24 +02:00
static int mkv_write_packet ( AVFormatContext * s , const AVPacket * pkt )
2010-06-05 01:40:50 +03:00
{
MatroskaMuxContext * mkv = s - > priv_data ;
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
int codec_type = s - > streams [ pkt - > stream_index ] - > codecpar - > codec_type ;
2013-08-18 21:09:39 +03:00
int keyframe = ! ! ( pkt - > flags & AV_PKT_FLAG_KEY ) ;
int cluster_size ;
int64_t cluster_time ;
int ret ;
2014-05-07 19:46:40 +03:00
int start_new_cluster ;
2013-08-18 21:09:39 +03:00
2016-11-01 01:04:46 +02:00
ret = mkv_check_new_extra_data ( s , pkt ) ;
if ( ret < 0 )
return ret ;
2020-01-22 21:01:30 +02:00
if ( mkv - > cluster_pos ! = - 1 ) {
2019-12-29 04:36:56 +02:00
if ( mkv - > tracks [ pkt - > stream_index ] . write_dts )
cluster_time = pkt - > dts - mkv - > cluster_pts ;
else
cluster_time = pkt - > pts - mkv - > cluster_pts ;
cluster_time + = mkv - > tracks [ pkt - > stream_index ] . ts_offset ;
cluster_size = avio_tell ( mkv - > cluster_bc ) ;
if ( mkv - > is_dash & & codec_type = = AVMEDIA_TYPE_VIDEO ) {
// WebM DASH specification states that the first block of
// every Cluster has to be a key frame. So for DASH video,
// we only create a Cluster on seeing key frames.
start_new_cluster = keyframe ;
} else if ( mkv - > is_dash & & codec_type = = AVMEDIA_TYPE_AUDIO & &
cluster_time > mkv - > cluster_time_limit ) {
// For DASH audio, we create a Cluster based on cluster_time_limit.
start_new_cluster = 1 ;
} else if ( ! mkv - > is_dash & &
( cluster_size > mkv - > cluster_size_limit | |
cluster_time > mkv - > cluster_time_limit | |
( codec_type = = AVMEDIA_TYPE_VIDEO & & keyframe & &
cluster_size > 4 * 1024 ) ) ) {
start_new_cluster = 1 ;
} else
start_new_cluster = 0 ;
2014-05-07 19:46:40 +03:00
2020-04-29 20:55:26 +02:00
if ( start_new_cluster ) {
ret = mkv_end_cluster ( s ) ;
if ( ret < 0 )
return ret ;
}
2020-01-22 21:01:30 +02:00
}
2010-03-04 10:53:08 +02:00
2016-05-04 21:31:28 +02:00
if ( ! mkv - > cluster_pos )
avio_write_marker ( s - > pb ,
av_rescale_q ( pkt - > dts , s - > streams [ pkt - > stream_index ] - > time_base , AV_TIME_BASE_Q ) ,
keyframe & & ( mkv - > have_video ? codec_type = = AVMEDIA_TYPE_VIDEO : 1 ) ? AVIO_DATA_MARKER_SYNC_POINT : AVIO_DATA_MARKER_BOUNDARY_POINT ) ;
2010-06-05 01:40:50 +03:00
// check if we have an audio packet cached
2021-01-29 15:46:43 +02:00
if ( mkv - > cur_audio_pkt - > size > 0 ) {
ret = mkv_write_packet_internal ( s , mkv - > cur_audio_pkt ) ;
av_packet_unref ( mkv - > cur_audio_pkt ) ;
2010-06-05 01:40:50 +03:00
if ( ret < 0 ) {
2014-06-07 01:54:50 +03:00
av_log ( s , AV_LOG_ERROR ,
" Could not write cached audio packet ret:%d \n " , ret ) ;
2010-06-05 01:40:50 +03:00
return ret ;
}
}
// buffer an audio packet to ensure the packet containing the video
// keyframe's timecode is contained in the same cluster for WebM
2013-08-18 21:09:39 +03:00
if ( codec_type = = AVMEDIA_TYPE_AUDIO ) {
2019-04-04 17:47:46 +02:00
if ( pkt - > size > 0 )
2021-01-29 15:46:43 +02:00
ret = av_packet_ref ( mkv - > cur_audio_pkt , pkt ) ;
2012-10-31 10:53:18 +03:00
} else
2019-12-29 04:14:53 +02:00
ret = mkv_write_packet_internal ( s , pkt ) ;
2010-06-05 01:40:50 +03:00
return ret ;
2007-09-05 03:22:37 +03:00
}
2013-08-19 10:33:14 +03:00
static int mkv_write_flush_packet ( AVFormatContext * s , AVPacket * pkt )
{
MatroskaMuxContext * mkv = s - > priv_data ;
2016-10-03 04:46:19 +02:00
2013-08-19 10:33:14 +03:00
if ( ! pkt ) {
2013-08-20 12:44:25 +03:00
if ( mkv - > cluster_pos ! = - 1 ) {
2020-04-29 20:55:26 +02:00
int ret = mkv_end_cluster ( s ) ;
if ( ret < 0 )
return ret ;
2019-04-20 01:41:07 +02:00
av_log ( s , AV_LOG_DEBUG ,
" Flushing cluster at offset % " PRIu64 " bytes \n " ,
avio_tell ( s - > pb ) ) ;
2013-08-19 10:33:14 +03:00
}
2015-06-09 10:47:11 +02:00
return 1 ;
2013-08-19 10:33:14 +03:00
}
return mkv_write_packet ( s , pkt ) ;
}
2007-09-05 03:22:37 +03:00
static int mkv_write_trailer ( AVFormatContext * s )
{
MatroskaMuxContext * mkv = s - > priv_data ;
2011-02-20 12:04:12 +02:00
AVIOContext * pb = s - > pb ;
avformat/matroskaenc: Remove inconsistencies wrt seekability handling
The Matroska muxer behaves differently in several ways when it thinks
that it is in unseekable/livestreaming mode: It does not add Cue entries
because they won't be written anyway for a livestream and it writes some
elements only preliminarily (with the intention to overwrite them with
an updated version at the end) when non-livestreaming etc.
There are two ways to set the Matroska muxer into livestreaming mode:
Setting an option or by providing an unseekable AVIOContext. Yet the
actual checks were not consistent:
If the AVIOContext was unseekable and no AAC extradata was available
when writing the header, writing the header failed; but if the AVIOContext
was seekable, it didn't, because the muxer expected to get the extradata
via packet side-data. Here the livestreaming option has not been checked,
although one can't use the updated extradata in case it is a livestream.
If the reserve_index_space option was used, space for writing Cues would
be reserved when writing the header unless the AVIOContext was
unseekable. Yet Cues were only written if the livestreaming option was
not set and the AVIOContext was seekable (when writing the trailer), so
if the AVIOContext was seekable and the livestreaming option set, the
reserved space would never be used at all.
If the AVIOContext was unseekable and the livestreaming option was not
set, it would be attempted to update the main length field at the end.
After all, it might be possible that the file is so short that it fits
into the AVIOContext's buffer in which case the seek back would work.
Yet this is dangerous: It might be that we are not dealing with a
simple output file, but that our output gets split into chunks and that
each of these chunks is actually seekable. In this case some part of the
last chunk (namely the eight bytes that have the same offset as the
length field had in the header) will be overwritten with what the muxer
wrongly believes to be the filesize.
(The livestreaming option has been added to deal with this scenario,
yet its documentation ("Write files assuming it is a live stream.")
doesn't make this clear at all. At least the segment muxer does not
set the option for live and given that the chances of successfully
seeking when the output is actually unseekable are slim, it is best to
not attempt to update the length field in the unseekable case at all.)
All these inconsistencies were fixed by treating the output as seekable
if the livestreaming option is not set and if the AVIOContext is
seekable. A macro has been used to enforce consistency and improve code
readability.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-05-01 22:31:36 +02:00
int64_t endpos , ret64 ;
2020-05-02 01:47:17 +02:00
int ret , ret2 = 0 ;
2007-09-05 03:23:11 +03:00
2010-06-05 01:40:50 +03:00
// check if we have an audio packet cached
2021-01-29 15:46:43 +02:00
if ( mkv - > cur_audio_pkt - > size > 0 ) {
ret = mkv_write_packet_internal ( s , mkv - > cur_audio_pkt ) ;
2010-06-05 01:40:50 +03:00
if ( ret < 0 ) {
2014-06-07 01:54:50 +03:00
av_log ( s , AV_LOG_ERROR ,
" Could not write cached audio packet ret:%d \n " , ret ) ;
2010-06-05 01:40:50 +03:00
return ret ;
}
}
avformat/matroskaenc: Only write Cluster if it has in fact been opened
Since commit 4aa0665f393847c35387a1c673e62346d0acfc95, the dynamic
buffer destined for the contents of the current Cluster is no longer
constantly allocated, reallocated and then freed after writing the
content; instead it is reset and reused when closing a Cluster.
Yet the code in mkv_write_trailer() still checked for whether a Cluster
is open by checking whether the pointer to the dynamic buffer is NULL or
not (instead of checking whether the position of the current Cluster is
-1 or not). If a Cluster was not open, an empty Cluster would be output.
One usually does not run into this issue, because unless there are
errors, there are only three possibilities to not have an opened Cluster
at the end of writing a packet:
The first is if one sent an audio packet to the muxer. It might trigger
closing and outputting the old Cluster, but because the muxer caches
audio packets internally, it would not be output immediately and
therefore no new Cluster would be opened.
The second is an audio packet that does not contain data (such packets
are sometimes sent for side-data only, e.g. by the FLAC encoder). The
only difference to the first scenario is that such packets are not
cached.
The third is if one explicitly flushes the muxer by sending a NULL
packet via av_write_frame().
If one also allows for errors, then there is also another possibility:
Caching the audio packet may fail in the first scenario.
If one calls av_write_trailer() after the first scenario, the cached
audio packet will be output when writing the trailer, for which
a Cluster is opened and everything is fine; because flushing the muxer
does currently not output the cached audio packet (if one is cached),
the issue also does not exist if an audio packet has been cached before
flushing. The issue only exists in one of the other scenarios.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-05-02 02:18:44 +02:00
if ( mkv - > cluster_pos ! = - 1 ) {
2020-04-29 20:55:26 +02:00
ret = end_ebml_master_crc32 ( pb , & mkv - > cluster_bc , mkv ,
2020-04-29 07:24:02 +02:00
MATROSKA_ID_CLUSTER , 0 , 0 , 0 ) ;
2020-04-29 20:55:26 +02:00
if ( ret < 0 )
return ret ;
2010-03-04 10:53:14 +02:00
}
2007-09-05 03:23:11 +03:00
2017-06-02 04:42:05 +02:00
ret = mkv_write_chapters ( s ) ;
if ( ret < 0 )
return ret ;
avformat/matroskaenc: Remove inconsistencies wrt seekability handling
The Matroska muxer behaves differently in several ways when it thinks
that it is in unseekable/livestreaming mode: It does not add Cue entries
because they won't be written anyway for a livestream and it writes some
elements only preliminarily (with the intention to overwrite them with
an updated version at the end) when non-livestreaming etc.
There are two ways to set the Matroska muxer into livestreaming mode:
Setting an option or by providing an unseekable AVIOContext. Yet the
actual checks were not consistent:
If the AVIOContext was unseekable and no AAC extradata was available
when writing the header, writing the header failed; but if the AVIOContext
was seekable, it didn't, because the muxer expected to get the extradata
via packet side-data. Here the livestreaming option has not been checked,
although one can't use the updated extradata in case it is a livestream.
If the reserve_index_space option was used, space for writing Cues would
be reserved when writing the header unless the AVIOContext was
unseekable. Yet Cues were only written if the livestreaming option was
not set and the AVIOContext was seekable (when writing the trailer), so
if the AVIOContext was seekable and the livestreaming option set, the
reserved space would never be used at all.
If the AVIOContext was unseekable and the livestreaming option was not
set, it would be attempted to update the main length field at the end.
After all, it might be possible that the file is so short that it fits
into the AVIOContext's buffer in which case the seek back would work.
Yet this is dangerous: It might be that we are not dealing with a
simple output file, but that our output gets split into chunks and that
each of these chunks is actually seekable. In this case some part of the
last chunk (namely the eight bytes that have the same offset as the
length field had in the header) will be overwritten with what the muxer
wrongly believes to be the filesize.
(The livestreaming option has been added to deal with this scenario,
yet its documentation ("Write files assuming it is a live stream.")
doesn't make this clear at all. At least the segment muxer does not
set the option for live and given that the chances of successfully
seeking when the output is actually unseekable are slim, it is best to
not attempt to update the length field in the unseekable case at all.)
All these inconsistencies were fixed by treating the output as seekable
if the livestreaming option is not set and if the AVIOContext is
seekable. A macro has been used to enforce consistency and improve code
readability.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-05-01 22:31:36 +02:00
if ( ! IS_SEEKABLE ( pb , mkv ) )
return 0 ;
2019-12-29 11:15:19 +02:00
2020-04-29 07:24:02 +02:00
endpos = avio_tell ( pb ) ;
2019-12-30 17:28:39 +02:00
2020-04-29 07:24:02 +02:00
if ( mkv - > cues . num_entries & & mkv - > reserve_cues_space > = 0 ) {
AVIOContext * cues = NULL ;
2022-01-09 20:45:58 +02:00
uint64_t size , offset = 0 ;
2020-04-29 07:24:02 +02:00
int length_size = 0 ;
2013-04-30 09:36:20 +03:00
2022-01-09 20:45:58 +02:00
redo_cues :
2020-04-29 07:24:02 +02:00
ret = start_ebml_master_crc32 ( & cues , mkv ) ;
if ( ret < 0 )
return ret ;
2013-04-30 09:36:20 +03:00
2020-04-29 07:24:02 +02:00
ret = mkv_assemble_cues ( s - > streams , cues , & mkv - > cues ,
2022-01-09 20:45:58 +02:00
mkv - > tracks , s - > nb_streams , offset ) ;
2020-04-29 07:24:02 +02:00
if ( ret < 0 ) {
ffio_free_dyn_buf ( & cues ) ;
return ret ;
}
2011-04-04 23:29:21 +03:00
2022-01-09 20:45:58 +02:00
if ( mkv - > reserve_cues_space | | mkv - > move_cues_to_front ) {
2020-04-29 07:24:02 +02:00
size = avio_tell ( cues ) ;
length_size = ebml_length_size ( size ) ;
size + = 4 + length_size ;
2022-01-09 20:45:58 +02:00
if ( offset + mkv - > reserve_cues_space < size ) {
if ( mkv - > move_cues_to_front ) {
offset = size - mkv - > reserve_cues_space ;
ffio_reset_dyn_buf ( cues ) ;
goto redo_cues ;
}
2020-04-29 07:24:02 +02:00
av_log ( s , AV_LOG_WARNING ,
" Insufficient space reserved for Cues: "
" %d < % " PRIu64 " . No Cues will be output. \n " ,
mkv - > reserve_cues_space , size ) ;
ret2 = AVERROR ( EINVAL ) ;
goto after_cues ;
} else {
2022-01-09 20:45:58 +02:00
if ( offset ) {
ret = ff_format_shift_data ( s , mkv - > cues_pos + mkv - > reserve_cues_space ,
offset ) ;
if ( ret < 0 ) {
ffio_free_dyn_buf ( & cues ) ;
return ret ;
}
endpos + = offset ;
}
2020-04-29 07:24:02 +02:00
if ( ( ret64 = avio_seek ( pb , mkv - > cues_pos , SEEK_SET ) ) < 0 ) {
2019-12-30 17:28:39 +02:00
ffio_free_dyn_buf ( & cues ) ;
2020-04-29 07:24:02 +02:00
return ret64 ;
}
if ( mkv - > reserve_cues_space = = size + 1 ) {
/* There is no way to reserve a single byte because
* the minimal size of an EBML Void element is 2
* ( 1 byte ID , 1 byte length field ) . This problem
* is solved by writing the Cues ' length field on
* one byte more than necessary . */
length_size + + ;
size + + ;
2019-12-30 17:28:39 +02:00
}
}
2011-04-04 23:29:21 +03:00
}
2020-04-29 07:24:02 +02:00
ret = end_ebml_master_crc32 ( pb , & cues , mkv , MATROSKA_ID_CUES ,
length_size , 0 , 1 ) ;
if ( ret < 0 )
return ret ;
if ( mkv - > reserve_cues_space ) {
if ( size < mkv - > reserve_cues_space )
put_ebml_void ( pb , mkv - > reserve_cues_space - size ) ;
2022-01-09 20:45:58 +02:00
} else if ( ! mkv - > move_cues_to_front )
2020-04-29 07:24:02 +02:00
endpos = avio_tell ( pb ) ;
}
2007-09-05 03:23:27 +03:00
2020-04-29 07:24:02 +02:00
after_cues :
2020-05-02 04:39:01 +02:00
/* Lengths greater than (1ULL << 56) - 1 can't be represented
* via an EBML number , so leave the unknown length field . */
if ( endpos - mkv - > segment_offset < ( 1ULL < < 56 ) - 1 ) {
2020-05-02 03:55:23 +02:00
if ( ( ret64 = avio_seek ( pb , mkv - > segment_offset - 8 , SEEK_SET ) ) < 0 )
return ret64 ;
put_ebml_length ( pb , endpos - mkv - > segment_offset , 8 ) ;
2020-05-02 04:39:01 +02:00
}
2020-05-02 03:55:23 +02:00
2020-04-29 07:24:02 +02:00
ret = mkv_write_seekhead ( pb , mkv , 1 , mkv - > info . pos ) ;
if ( ret < 0 )
return ret ;
2007-09-05 03:26:03 +03:00
2020-04-29 07:24:02 +02:00
if ( mkv - > info . bc ) {
2007-09-05 03:26:03 +03:00
// update the duration
av_log ( s , AV_LOG_DEBUG , " end duration = % " PRIu64 " \n " , mkv - > duration ) ;
2020-04-29 06:31:07 +02:00
avio_seek ( mkv - > info . bc , mkv - > duration_offset , SEEK_SET ) ;
put_ebml_float ( mkv - > info . bc , MATROSKA_ID_DURATION , mkv - > duration ) ;
2020-04-29 20:55:26 +02:00
ret = end_ebml_master_crc32 ( pb , & mkv - > info . bc , mkv ,
MATROSKA_ID_INFO , 0 , 0 , 0 ) ;
if ( ret < 0 )
return ret ;
2020-04-29 07:24:02 +02:00
}
2007-09-05 03:26:03 +03:00
2020-04-29 07:24:02 +02:00
if ( mkv - > track . bc ) {
// write Tracks master
avio_seek ( pb , mkv - > track . pos , SEEK_SET ) ;
ret = end_ebml_master_crc32 ( pb , & mkv - > track . bc , mkv ,
MATROSKA_ID_TRACKS , 0 , 0 , 0 ) ;
if ( ret < 0 )
return ret ;
}
2016-10-30 21:39:03 +02:00
2020-04-29 07:24:02 +02:00
// update stream durations
if ( mkv - > tags . bc ) {
2022-06-15 11:25:12 +02:00
AVIOContext * tags_bc = mkv - > tags . bc ;
2020-04-29 07:24:02 +02:00
int i ;
for ( i = 0 ; i < s - > nb_streams ; + + i ) {
const AVStream * st = s - > streams [ i ] ;
const mkv_track * track = & mkv - > tracks [ i ] ;
2015-08-05 07:09:57 +02:00
2020-04-29 07:24:02 +02:00
if ( track - > duration_offset > 0 ) {
double duration_sec = track - > duration * av_q2d ( st - > time_base ) ;
char duration_string [ 20 ] = " " ;
2022-06-15 11:25:12 +02:00
ebml_master simpletag ;
2016-10-07 07:32:10 +02:00
2020-04-29 07:24:02 +02:00
av_log ( s , AV_LOG_DEBUG , " stream %d end duration = % " PRIu64 " \n " , i ,
track - > duration ) ;
2015-08-05 07:09:57 +02:00
2022-06-15 11:25:12 +02:00
avio_seek ( tags_bc , track - > duration_offset , SEEK_SET ) ;
simpletag = start_ebml_master ( tags_bc , MATROSKA_ID_SIMPLETAG ,
2 + 1 + 8 + 23 ) ;
put_ebml_string ( tags_bc , MATROSKA_ID_TAGNAME , " DURATION " ) ;
2015-08-05 07:09:57 +02:00
2020-04-29 07:24:02 +02:00
snprintf ( duration_string , 20 , " %02d:%02d:%012.9f " ,
( int ) duration_sec / 3600 , ( ( int ) duration_sec / 60 ) % 60 ,
fmod ( duration_sec , 60 ) ) ;
2015-08-05 07:09:57 +02:00
2022-06-15 11:25:12 +02:00
put_ebml_binary ( tags_bc , MATROSKA_ID_TAGSTRING , duration_string , 20 ) ;
end_ebml_master ( tags_bc , simpletag ) ;
2015-08-05 07:09:57 +02:00
}
2016-10-04 00:27:30 +02:00
}
2015-08-05 07:09:57 +02:00
2020-04-29 07:24:02 +02:00
avio_seek ( pb , mkv - > tags . pos , SEEK_SET ) ;
ret = end_ebml_master_crc32 ( pb , & mkv - > tags . bc , mkv ,
MATROSKA_ID_TAGS , 0 , 0 , 0 ) ;
if ( ret < 0 )
return ret ;
}
avio_seek ( pb , endpos , SEEK_SET ) ;
2007-09-05 03:23:11 +03:00
2020-05-02 01:47:17 +02:00
return ret2 ;
2007-09-05 03:22:37 +03:00
}
2019-12-29 02:33:36 +02:00
static uint64_t mkv_get_uid ( const mkv_track * tracks , int i , AVLFG * c )
{
while ( 1 ) {
uint64_t uid ;
int k ;
uid = ( uint64_t ) av_lfg_get ( c ) < < 32 ;
uid | = av_lfg_get ( c ) ;
if ( ! uid )
continue ;
for ( k = 0 ; k < i ; k + + ) {
if ( tracks [ k ] . uid = = uid )
break ;
}
if ( k = = i )
return uid ;
}
}
2015-10-08 04:34:06 +02:00
static int mkv_init ( struct AVFormatContext * s )
{
2021-08-24 14:58:07 +02:00
FFFormatContext * const si = ffformatcontext ( s ) ;
2019-12-29 02:33:36 +02:00
MatroskaMuxContext * mkv = s - > priv_data ;
AVLFG c ;
2019-11-27 06:00:04 +02:00
unsigned nb_tracks = 0 ;
2015-10-08 04:34:06 +02:00
int i ;
2022-01-16 15:01:28 +02:00
mkv - > ctx = s ;
2017-11-27 19:11:21 +02:00
for ( i = 0 ; i < s - > nb_streams ; i + + ) {
if ( s - > streams [ i ] - > codecpar - > codec_id = = AV_CODEC_ID_ATRAC3 | |
s - > streams [ i ] - > codecpar - > codec_id = = AV_CODEC_ID_COOK | |
s - > streams [ i ] - > codecpar - > codec_id = = AV_CODEC_ID_RA_288 | |
s - > streams [ i ] - > codecpar - > codec_id = = AV_CODEC_ID_SIPR | |
s - > streams [ i ] - > codecpar - > codec_id = = AV_CODEC_ID_RV10 | |
s - > streams [ i ] - > codecpar - > codec_id = = AV_CODEC_ID_RV20 ) {
av_log ( s , AV_LOG_ERROR ,
" The Matroska muxer does not yet support muxing %s \n " ,
avcodec_get_name ( s - > streams [ i ] - > codecpar - > codec_id ) ) ;
return AVERROR_PATCHWELCOME ;
}
}
2015-10-08 04:34:06 +02:00
if ( s - > avoid_negative_ts < 0 ) {
s - > avoid_negative_ts = 1 ;
2021-08-24 14:58:07 +02:00
si - > avoid_negative_ts_use_pts = 1 ;
2015-10-08 04:34:06 +02:00
}
2022-01-07 12:36:19 +02:00
if ( ! CONFIG_MATROSKA_MUXER | |
( CONFIG_WEBM_MUXER & & ! strcmp ( s - > oformat - > name , " webm " ) ) ) {
2019-11-02 19:56:31 +02:00
mkv - > mode = MODE_WEBM ;
mkv - > write_crc = 0 ;
} else
mkv - > mode = MODE_MATROSKAv2 ;
2021-03-18 08:12:05 +02:00
mkv - > cur_audio_pkt = ffformatcontext ( s ) - > pkt ;
2021-09-14 21:31:53 +02:00
mkv - > tracks = av_calloc ( s - > nb_streams , sizeof ( * mkv - > tracks ) ) ;
2019-12-29 04:36:56 +02:00
if ( ! mkv - > tracks )
2019-12-29 02:33:36 +02:00
return AVERROR ( ENOMEM ) ;
2020-01-18 10:27:25 +02:00
if ( ! ( s - > flags & AVFMT_FLAG_BITEXACT ) ) {
2019-12-29 02:33:36 +02:00
av_lfg_init ( & c , av_get_random_seed ( ) ) ;
2020-01-18 10:27:25 +02:00
// Calculate the SegmentUID now in order not to waste our random seed.
for ( i = 0 ; i < 4 ; i + + )
mkv - > segment_uid [ i ] = av_lfg_get ( & c ) ;
}
2015-10-08 04:34:06 +02:00
for ( i = 0 ; i < s - > nb_streams ; i + + ) {
2019-11-27 06:00:04 +02:00
AVStream * st = s - > streams [ i ] ;
2022-01-16 00:52:13 +02:00
const AVCodecParameters * const par = st - > codecpar ;
2019-12-29 02:33:36 +02:00
mkv_track * track = & mkv - > tracks [ i ] ;
2022-01-16 00:52:13 +02:00
switch ( par - > codec_id ) {
# if CONFIG_MATROSKA_MUXER
case AV_CODEC_ID_WAVPACK :
track - > reformat = mkv_reformat_wavpack ;
break ;
2022-01-16 02:38:41 +02:00
case AV_CODEC_ID_H264 :
case AV_CODEC_ID_HEVC :
if ( ( par - > codec_id = = AV_CODEC_ID_H264 & & par - > extradata_size > 0 | |
par - > codec_id = = AV_CODEC_ID_HEVC & & par - > extradata_size > 6 ) & &
( AV_RB24 ( par - > extradata ) = = 1 | | AV_RB32 ( par - > extradata ) = = 1 ) )
track - > reformat = mkv_reformat_h2645 ;
break ;
2022-01-16 13:12:17 +02:00
case AV_CODEC_ID_PRORES :
/* Matroska specification requires to remove
* the first QuickTime atom . */
track - > offset = 8 ;
break ;
2022-01-16 00:52:13 +02:00
# endif
2022-01-16 01:12:29 +02:00
case AV_CODEC_ID_AV1 :
track - > reformat = mkv_reformat_av1 ;
break ;
2022-01-16 17:24:42 +02:00
case AV_CODEC_ID_WEBVTT :
track - > reformat = webm_reformat_vtt ;
break ;
2022-01-16 00:52:13 +02:00
}
2019-12-29 02:33:36 +02:00
if ( s - > flags & AVFMT_FLAG_BITEXACT ) {
track - > uid = i + 1 ;
} else {
track - > uid = mkv_get_uid ( mkv - > tracks , i , & c ) ;
}
2015-10-08 04:34:06 +02:00
// ms precision is the de-facto standard timescale for mkv files
2019-12-29 04:36:56 +02:00
avpriv_set_pts_info ( st , 64 , 1 , 1000 ) ;
2019-11-27 06:00:04 +02:00
2019-11-02 19:56:31 +02:00
if ( st - > codecpar - > codec_type = = AVMEDIA_TYPE_ATTACHMENT ) {
2022-01-07 12:36:19 +02:00
if ( IS_WEBM ( mkv ) ) {
2019-11-02 19:56:31 +02:00
av_log ( s , AV_LOG_WARNING , " Stream %d will be ignored "
" as WebM doesn't support attachments. \n " , i ) ;
2019-11-03 14:25:08 +02:00
} else if ( ! get_mimetype ( st ) ) {
av_log ( s , AV_LOG_ERROR , " Attachment stream %d has no mimetype "
" tag and it cannot be deduced from the codec id. \n " , i ) ;
return AVERROR ( EINVAL ) ;
2019-11-02 19:56:31 +02:00
}
2019-12-29 04:26:05 +02:00
mkv - > nb_attachments + + ;
2019-11-27 06:00:04 +02:00
continue ;
2019-11-02 19:56:31 +02:00
}
2019-11-27 06:00:04 +02:00
nb_tracks + + ;
track - > track_num = mkv - > is_dash ? mkv - > dash_track_number : nb_tracks ;
2020-04-15 04:49:46 +02:00
track - > track_num_size = ebml_num_size ( track - > track_num ) ;
2015-10-08 04:34:06 +02:00
}
2019-11-01 12:49:47 +02:00
if ( mkv - > is_dash & & nb_tracks ! = 1 )
return AVERROR ( EINVAL ) ;
2015-10-08 04:34:06 +02:00
return 0 ;
}
2021-11-18 22:48:49 +02:00
static int mkv_check_bitstream ( AVFormatContext * s , AVStream * st ,
const AVPacket * pkt )
2015-10-08 04:34:06 +02:00
{
int ret = 1 ;
2022-01-07 12:36:19 +02:00
if ( CONFIG_MATROSKA_MUXER & & st - > codecpar - > codec_id = = AV_CODEC_ID_AAC ) {
2015-10-08 04:34:06 +02:00
if ( pkt - > size > 2 & & ( AV_RB16 ( pkt - > data ) & 0xfff0 ) = = 0xfff0 )
ret = ff_stream_add_bitstream_filter ( st , " aac_adtstoasc " , NULL ) ;
2016-04-10 21:58:15 +02:00
} else if ( st - > codecpar - > codec_id = = AV_CODEC_ID_VP9 ) {
2016-02-29 16:43:07 +02:00
ret = ff_stream_add_bitstream_filter ( st , " vp9_superframe " , NULL ) ;
2020-05-08 16:11:27 +02:00
} else if ( CONFIG_MATROSKA_MUXER & &
st - > codecpar - > codec_id = = AV_CODEC_ID_HDMV_PGS_SUBTITLE ) {
ret = ff_stream_add_bitstream_filter ( st , " pgs_frame_merge " , NULL ) ;
2016-02-29 16:43:07 +02:00
}
2015-10-08 04:34:06 +02:00
return ret ;
}
2013-08-02 22:14:25 +03:00
static const AVCodecTag additional_audio_tags [ ] = {
2013-01-24 02:53:50 +03:00
{ AV_CODEC_ID_ALAC , 0 XFFFFFFFF } ,
{ AV_CODEC_ID_MLP , 0xFFFFFFFF } ,
{ AV_CODEC_ID_OPUS , 0xFFFFFFFF } ,
{ AV_CODEC_ID_PCM_S16BE , 0xFFFFFFFF } ,
{ AV_CODEC_ID_PCM_S24BE , 0xFFFFFFFF } ,
{ AV_CODEC_ID_PCM_S32BE , 0xFFFFFFFF } ,
2017-03-02 00:22:40 +02:00
{ AV_CODEC_ID_QDMC , 0xFFFFFFFF } ,
2013-01-24 02:53:50 +03:00
{ AV_CODEC_ID_QDM2 , 0xFFFFFFFF } ,
{ AV_CODEC_ID_RA_144 , 0xFFFFFFFF } ,
{ AV_CODEC_ID_RA_288 , 0xFFFFFFFF } ,
{ AV_CODEC_ID_COOK , 0xFFFFFFFF } ,
{ AV_CODEC_ID_TRUEHD , 0xFFFFFFFF } ,
{ AV_CODEC_ID_NONE , 0xFFFFFFFF }
} ;
2013-08-02 22:14:25 +03:00
static const AVCodecTag additional_video_tags [ ] = {
2013-01-24 02:53:50 +03:00
{ AV_CODEC_ID_RV10 , 0xFFFFFFFF } ,
{ AV_CODEC_ID_RV20 , 0xFFFFFFFF } ,
{ AV_CODEC_ID_RV30 , 0xFFFFFFFF } ,
{ AV_CODEC_ID_NONE , 0xFFFFFFFF }
} ;
2015-03-24 01:04:43 +02:00
static const AVCodecTag additional_subtitle_tags [ ] = {
{ AV_CODEC_ID_DVB_SUBTITLE , 0xFFFFFFFF } ,
2019-03-10 21:45:45 +02:00
{ AV_CODEC_ID_DVD_SUBTITLE , 0xFFFFFFFF } ,
2015-03-24 01:04:43 +02:00
{ AV_CODEC_ID_HDMV_PGS_SUBTITLE , 0xFFFFFFFF } ,
{ AV_CODEC_ID_NONE , 0xFFFFFFFF }
} ;
2013-04-30 09:36:20 +03:00
# define OFFSET(x) offsetof(MatroskaMuxContext, x)
# define FLAGS AV_OPT_FLAG_ENCODING_PARAM
static const AVOption options [ ] = {
2013-08-18 21:09:40 +03:00
{ " reserve_index_space " , " Reserve a given amount of space (in bytes) at the beginning of the file for the index (cues). " , OFFSET ( reserve_cues_space ) , AV_OPT_TYPE_INT , { . i64 = 0 } , 0 , INT_MAX , FLAGS } ,
2022-01-09 20:45:58 +02:00
{ " cues_to_front " , " Move Cues (the index) to the front by shifting data if necessary " , OFFSET ( move_cues_to_front ) , AV_OPT_TYPE_BOOL , { . i64 = 0 } , 0 , 1 , FLAGS } ,
2013-08-18 21:09:40 +03:00
{ " cluster_size_limit " , " Store at most the provided amount of bytes in a cluster. " , OFFSET ( cluster_size_limit ) , AV_OPT_TYPE_INT , { . i64 = - 1 } , - 1 , INT_MAX , FLAGS } ,
{ " cluster_time_limit " , " Store at most the provided number of milliseconds in a cluster. " , OFFSET ( cluster_time_limit ) , AV_OPT_TYPE_INT64 , { . i64 = - 1 } , - 1 , INT64_MAX , FLAGS } ,
2015-11-21 23:05:07 +02:00
{ " dash " , " Create a WebM file conforming to WebM DASH specification " , OFFSET ( is_dash ) , AV_OPT_TYPE_BOOL , { . i64 = 0 } , 0 , 1 , FLAGS } ,
2020-04-15 04:49:46 +02:00
{ " dash_track_number " , " Track number for the DASH stream " , OFFSET ( dash_track_number ) , AV_OPT_TYPE_INT , { . i64 = 1 } , 1 , INT_MAX , FLAGS } ,
2015-11-21 23:05:07 +02:00
{ " live " , " Write files assuming it is a live stream. " , OFFSET ( is_live ) , AV_OPT_TYPE_BOOL , { . i64 = 0 } , 0 , 1 , FLAGS } ,
{ " allow_raw_vfw " , " allow RAW VFW mode " , OFFSET ( allow_raw_vfw ) , AV_OPT_TYPE_BOOL , { . i64 = 0 } , 0 , 1 , FLAGS } ,
2020-07-08 14:02:25 +02:00
{ " flipped_raw_rgb " , " Raw RGB bitmaps in VFW mode are stored bottom-up " , OFFSET ( flipped_raw_rgb ) , AV_OPT_TYPE_BOOL , { . i64 = 0 } , 0 , 1 , FLAGS } ,
2016-10-06 23:17:04 +02:00
{ " write_crc32 " , " write a CRC32 element inside every Level 1 element " , OFFSET ( write_crc ) , AV_OPT_TYPE_BOOL , { . i64 = 1 } , 0 , 1 , FLAGS } ,
2021-08-20 08:10:12 +02:00
{ " default_mode " , " Controls how a track's FlagDefault is inferred " , OFFSET ( default_mode ) , AV_OPT_TYPE_INT , { . i64 = DEFAULT_MODE_PASSTHROUGH } , DEFAULT_MODE_INFER , DEFAULT_MODE_PASSTHROUGH , FLAGS , " default_mode " } ,
2021-08-20 07:50:44 +02:00
{ " infer " , " For each track type, mark each track of disposition default as default; if none exists, mark the first track as default. " , 0 , AV_OPT_TYPE_CONST , { . i64 = DEFAULT_MODE_INFER } , 0 , 0 , FLAGS , " default_mode " } ,
{ " infer_no_subs " , " For each track type, mark each track of disposition default as default; for audio and video: if none exists, mark the first track as default. " , 0 , AV_OPT_TYPE_CONST , { . i64 = DEFAULT_MODE_INFER_NO_SUBS } , 0 , 0 , FLAGS , " default_mode " } ,
2020-01-18 08:05:20 +02:00
{ " passthrough " , " Use the disposition flag as-is " , 0 , AV_OPT_TYPE_CONST , { . i64 = DEFAULT_MODE_PASSTHROUGH } , 0 , 0 , FLAGS , " default_mode " } ,
2013-04-30 09:36:20 +03:00
{ NULL } ,
} ;
2021-06-07 16:13:33 +02:00
static const AVClass matroska_webm_class = {
. class_name = " matroska/webm muxer " ,
2013-04-30 09:36:20 +03:00
. item_name = av_default_item_name ,
. option = options ,
. version = LIBAVUTIL_VERSION_INT ,
} ;
2021-06-07 16:13:33 +02:00
# if CONFIG_MATROSKA_MUXER
2021-08-20 09:39:04 +02:00
static int mkv_query_codec ( enum AVCodecID codec_id , int std_compliance )
{
for ( int i = 0 ; ff_mkv_codec_tags [ i ] . id ! = AV_CODEC_ID_NONE ; i + + )
if ( ff_mkv_codec_tags [ i ] . id = = codec_id )
return 1 ;
if ( std_compliance < FF_COMPLIANCE_NORMAL ) {
enum AVMediaType type = avcodec_get_type ( codec_id ) ;
// mkv theoretically supports any video/audio through VFW/ACM
if ( type = = AVMEDIA_TYPE_VIDEO | | type = = AVMEDIA_TYPE_AUDIO )
return 1 ;
}
return 0 ;
}
2021-04-19 19:45:24 +02:00
const AVOutputFormat ff_matroska_muxer = {
2011-07-16 23:18:12 +03:00
. name = " matroska " ,
2012-07-25 00:51:41 +03:00
. long_name = NULL_IF_CONFIG_SMALL ( " Matroska " ) ,
2011-07-16 23:18:12 +03:00
. mime_type = " video/x-matroska " ,
. extensions = " mkv " ,
. priv_data_size = sizeof ( MatroskaMuxContext ) ,
2012-07-22 02:44:26 +03:00
. audio_codec = CONFIG_LIBVORBIS_ENCODER ?
2012-08-05 12:11:04 +03:00
AV_CODEC_ID_VORBIS : AV_CODEC_ID_AC3 ,
2012-07-22 02:44:26 +03:00
. video_codec = CONFIG_LIBX264_ENCODER ?
2012-08-05 12:11:04 +03:00
AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4 ,
2015-10-08 04:34:06 +02:00
. init = mkv_init ,
2019-10-16 03:57:08 +02:00
. deinit = mkv_deinit ,
2011-07-16 23:18:12 +03:00
. write_header = mkv_write_header ,
2013-08-19 10:33:14 +03:00
. write_packet = mkv_write_flush_packet ,
2011-07-16 23:18:12 +03:00
. write_trailer = mkv_write_trailer ,
2011-05-26 21:19:04 +03:00
. flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
2013-08-19 10:33:14 +03:00
AVFMT_TS_NONSTRICT | AVFMT_ALLOW_FLUSH ,
2013-01-24 02:53:50 +03:00
. codec_tag = ( const AVCodecTag * const [ ] ) {
ff_codec_bmp_tags , ff_codec_wav_tags ,
2015-03-24 01:04:43 +02:00
additional_audio_tags , additional_video_tags , additional_subtitle_tags , 0
2013-01-24 02:53:50 +03:00
} ,
subtitles: introduce ASS codec id and use it.
Currently, we have a AV_CODEC_ID_SSA, which matches the way the ASS/SSA
markup is muxed in a standalone .ass/.ssa file. This means the AVPacket
data starts with a "Dialogue:" string, followed by a timing information
(start and end of the event as string) and a trailing CRLF after each
line. One packet can contain several lines. We'll refer to this layout
as "SSA" or "SSA lines".
In matroska, this markup is not stored as such: it has no "Dialogue:"
prefix, it contains a ReadOrder field, the timing information is not in
the payload, and it doesn't contain the trailing CRLF. See [1] for more
info. We'll refer to this layout as "ASS".
Since we have only one common codec for both formats, the matroska
demuxer is constructing an AVPacket following the "SSA lines" format.
This causes several problems, so it was decided to change this into
clean ASS packets.
Some insight about what is changed or unchanged in this commit:
CODECS
------
- the decoding process still writes "SSA lines" markup inside the ass
fields of the subtitles rectangles (sub->rects[n]->ass), which is
still the current common way of representing decoded subtitles
markup. It is meant to change later.
- new ASS codec id: AV_CODEC_ID_ASS (which is different from the
legacy AV_CODEC_ID_SSA)
- lavc/assdec: the "ass" decoder is renamed into "ssa" (instead of
"ass") for consistency with the codec id and allows to add a real
ass decoder. This ass decoder receives clean ASS lines (so it starts
with a ReadOrder, is followed by the Layer, etc). We make sure this
is decoded properly in a new ass-line rectangle of the decoded
subtitles (the ssa decoder OTOH is doing a simple straightforward
copy). Using the packet timing instead of data string makes sure the
ass-line now contains the appropriate timing.
- lavc/assenc: just like the ass decoder, the "ssa" encoder is renamed
into "ssa" (instead of "ass") for consistency with the codec id, and
allows to add a real "ass" encoder.
One important thing about this encoder is that it only supports one
ass rectangle: we could have put several dialogue events in the
AVPacket (separated by a \0 for instance) but this would have cause
trouble for the muxer which needs not only the start time, but also
the duration: typically, you have merged events with the same start
time (stored in the AVPacket->pts) but a different duration. At the
moment, only the matroska do the merge with the SSA-line codec.
We will need to make sure all the decoders in the future can't add
more than one rectangle (and only one Dialogue line in it
obviously).
FORMATS
-------
- lavf/assenc: the .ass/.ssa muxer can take both SSA and ASS packets.
In the case of ASS packets as input, it adds the timing based on the
AVPacket pts and duration, and mux it with "Dialogue:", trailing
CRLF, etc.
- lavf/assdec: unchanged; it currently still only outputs SSA-lines
packets.
- lavf/mkv: the demuxer can now output ASS packets without the need of
any "SSA-lines" reconstruction hack. It will become the default at
next libavformat bump, and the SSA support will be dropped from the
demuxer. The muxer can take ASS packets since it's muxed normally,
and still supports the old SSA packets. All the SSA support and
hacks in Matroska code will be dropped at next lavf bump.
[1]: http://www.matroska.org/technical/specs/subtitles/ssa.html
2013-01-03 05:06:43 +03:00
. subtitle_codec = AV_CODEC_ID_ASS ,
2011-08-11 21:35:46 +03:00
. query_codec = mkv_query_codec ,
2015-10-08 04:34:06 +02:00
. check_bitstream = mkv_check_bitstream ,
2021-06-07 16:13:33 +02:00
. priv_class = & matroska_webm_class ,
2007-09-05 03:22:37 +03:00
} ;
2010-06-05 01:40:54 +03:00
# endif
# if CONFIG_WEBM_MUXER
2021-08-20 09:39:04 +02:00
static int webm_query_codec ( enum AVCodecID codec_id , int std_compliance )
{
for ( int i = 0 ; ff_webm_codec_tags [ i ] . id ! = AV_CODEC_ID_NONE ; i + + )
if ( ff_webm_codec_tags [ i ] . id = = codec_id )
return 1 ;
return 0 ;
}
2021-04-19 19:45:24 +02:00
const AVOutputFormat ff_webm_muxer = {
2011-07-16 23:18:12 +03:00
. name = " webm " ,
2012-07-25 00:51:41 +03:00
. long_name = NULL_IF_CONFIG_SMALL ( " WebM " ) ,
2011-07-16 23:18:12 +03:00
. mime_type = " video/webm " ,
. extensions = " webm " ,
. priv_data_size = sizeof ( MatroskaMuxContext ) ,
2015-08-09 10:59:33 +02:00
. audio_codec = CONFIG_LIBOPUS_ENCODER ? AV_CODEC_ID_OPUS : AV_CODEC_ID_VORBIS ,
. video_codec = CONFIG_LIBVPX_VP9_ENCODER ? AV_CODEC_ID_VP9 : AV_CODEC_ID_VP8 ,
2013-07-09 02:25:30 +03:00
. subtitle_codec = AV_CODEC_ID_WEBVTT ,
2015-10-08 04:34:06 +02:00
. init = mkv_init ,
2019-10-16 03:57:08 +02:00
. deinit = mkv_deinit ,
2011-07-16 23:18:12 +03:00
. write_header = mkv_write_header ,
2013-08-19 10:33:14 +03:00
. write_packet = mkv_write_flush_packet ,
2011-07-16 23:18:12 +03:00
. write_trailer = mkv_write_trailer ,
2018-09-21 17:52:13 +02:00
. query_codec = webm_query_codec ,
2015-10-08 04:34:06 +02:00
. check_bitstream = mkv_check_bitstream ,
2011-05-26 21:19:04 +03:00
. flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
2013-08-19 10:33:14 +03:00
AVFMT_TS_NONSTRICT | AVFMT_ALLOW_FLUSH ,
2021-06-07 16:13:33 +02:00
. priv_class = & matroska_webm_class ,
2010-06-05 01:40:54 +03:00
} ;
# endif
2007-09-05 03:24:19 +03:00
2010-06-05 01:40:54 +03:00
# if CONFIG_MATROSKA_AUDIO_MUXER
2021-04-19 19:45:24 +02:00
const AVOutputFormat ff_matroska_audio_muxer = {
2011-07-16 23:18:12 +03:00
. name = " matroska " ,
2013-07-16 03:36:31 +03:00
. long_name = NULL_IF_CONFIG_SMALL ( " Matroska Audio " ) ,
2011-07-16 23:18:12 +03:00
. mime_type = " audio/x-matroska " ,
. extensions = " mka " ,
. priv_data_size = sizeof ( MatroskaMuxContext ) ,
2012-07-22 02:44:26 +03:00
. audio_codec = CONFIG_LIBVORBIS_ENCODER ?
2012-08-05 12:11:04 +03:00
AV_CODEC_ID_VORBIS : AV_CODEC_ID_AC3 ,
. video_codec = AV_CODEC_ID_NONE ,
2015-10-08 04:34:06 +02:00
. init = mkv_init ,
2019-10-16 03:57:08 +02:00
. deinit = mkv_deinit ,
2011-07-16 23:18:12 +03:00
. write_header = mkv_write_header ,
2013-08-19 10:33:14 +03:00
. write_packet = mkv_write_flush_packet ,
2011-07-16 23:18:12 +03:00
. write_trailer = mkv_write_trailer ,
2015-10-08 04:34:06 +02:00
. check_bitstream = mkv_check_bitstream ,
2013-08-19 10:33:14 +03:00
. flags = AVFMT_GLOBALHEADER | AVFMT_TS_NONSTRICT |
AVFMT_ALLOW_FLUSH ,
2013-01-24 02:53:50 +03:00
. codec_tag = ( const AVCodecTag * const [ ] ) {
ff_codec_wav_tags , additional_audio_tags , 0
} ,
2021-06-07 16:13:33 +02:00
. priv_class = & matroska_webm_class ,
2007-09-05 03:24:19 +03:00
} ;
2010-06-05 01:40:54 +03:00
# endif