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>
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"
# 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"
2013-04-30 08:52:50 +03:00
# include "riff.h"
2013-09-08 19:02:45 +03:00
# include "subtitles.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"
2013-05-03 14:09:23 +03:00
# include "libavutil/sha.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"
2013-08-30 21:26:37 +03:00
# include "libavcodec/internal.h"
2007-09-05 03:22:37 +03:00
2007-09-05 03:24:56 +03:00
typedef struct ebml_master {
2008-10-03 13:16:29 +03:00
int64_t pos ; ///< absolute offset in the file where the master's elements start
2007-09-05 03:24:56 +03:00
int sizebytes ; ///< how many bytes were reserved for the size
} ebml_master ;
2007-09-05 03:23:19 +03:00
typedef struct mkv_seekhead_entry {
unsigned int elementid ;
uint64_t segmentpos ;
} mkv_seekhead_entry ;
typedef struct mkv_seekhead {
2008-10-03 13:16:29 +03:00
int64_t filepos ;
int64_t segment_offset ; ///< the file offset to the beginning of the segment
2007-09-05 03:23:26 +03:00
int reserved_size ; ///< -1 if appending to file
2007-09-05 03:23:19 +03:00
int max_entries ;
mkv_seekhead_entry * entries ;
int num_entries ;
} 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 ;
2007-09-05 03:23:27 +03:00
int tracknum ;
2008-10-03 13:16:29 +03:00
int64_t cluster_pos ; ///< file offset of the cluster containing the block
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 {
2008-10-03 13:16:29 +03:00
int64_t segment_offset ;
2007-09-05 03:23:27 +03:00
mkv_cuepoint * entries ;
int num_entries ;
} mkv_cues ;
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 ;
2017-04-13 17:43:11 +02:00
int sample_rate ;
int64_t sample_rate_offset ;
2016-11-01 01:04:46 +02:00
int64_t codecpriv_offset ;
2014-04-29 13:26:38 +03:00
int64_t ts_offset ;
2010-03-09 06:23:28 +02:00
} mkv_track ;
2016-10-07 07:46:22 +02:00
typedef struct mkv_attachment {
int stream_idx ;
uint32_t fileuid ;
} mkv_attachment ;
typedef struct mkv_attachments {
mkv_attachment * entries ;
int num_entries ;
} mkv_attachments ;
2010-06-05 01:40:54 +03:00
# define MODE_MATROSKAv2 0x01
# define MODE_WEBM 0x02
2015-06-05 01:29:49 +02:00
/** Maximum number of tracks allowed in a Matroska file (with track numbers in
* range 1 to 126 ( inclusive ) */
# define MAX_TRACKS 126
2007-09-05 03:22:37 +03:00
typedef struct MatroskaMuxContext {
2013-04-30 09:36:20 +03:00
const AVClass * class ;
2010-06-05 01:40:54 +03:00
int mode ;
2011-02-20 12:04:12 +02:00
AVIOContext * dyn_bc ;
2016-10-04 00:27:30 +02:00
AVIOContext * tags_bc ;
ebml_master tags ;
2016-10-04 00:29:28 +02:00
AVIOContext * info_bc ;
ebml_master info ;
2016-10-30 21:39:03 +02:00
AVIOContext * tracks_bc ;
ebml_master tracks_master ;
2007-09-05 03:24:56 +03:00
ebml_master segment ;
2008-10-03 13:16:29 +03:00
int64_t segment_offset ;
2007-09-05 03:24:56 +03:00
ebml_master cluster ;
2008-10-03 13:16:29 +03:00
int64_t cluster_pos ; ///< file offset of the current cluster
2010-03-09 06:23:36 +02:00
int64_t cluster_pts ;
2008-10-03 13:16:29 +03:00
int64_t duration_offset ;
2010-03-09 06:23:36 +02:00
int64_t duration ;
2007-09-05 03:23:19 +03:00
mkv_seekhead * main_seekhead ;
2007-09-05 03:23:27 +03:00
mkv_cues * cues ;
2010-03-09 06:23:28 +02:00
mkv_track * tracks ;
2016-10-07 07:46:22 +02:00
mkv_attachments * attachments ;
2007-09-05 03:24:29 +03:00
2010-06-05 01:40:50 +03:00
AVPacket cur_audio_pkt ;
2011-07-30 14:37:36 +03:00
int have_attachments ;
2016-05-04 21:31:28 +02:00
int have_video ;
2013-04-30 09:36:20 +03:00
int reserve_cues_space ;
2013-08-18 21:09:40 +03:00
int cluster_size_limit ;
2013-04-30 09:36:20 +03:00
int64_t cues_pos ;
2013-08-18 21:09:40 +03:00
int64_t cluster_time_limit ;
2014-05-07 19:46:40 +03:00
int is_dash ;
int dash_track_number ;
2015-03-30 23:46:10 +02:00
int is_live ;
2016-10-06 23:17:04 +02:00
int write_crc ;
2013-07-19 23:52:16 +03:00
uint32_t chapter_id_offset ;
2013-09-03 20:53:34 +03:00
int wrote_chapters ;
2015-02-14 17:20:58 +02:00
2015-06-05 01:29:49 +02:00
int64_t last_track_timestamp [ MAX_TRACKS ] ;
2015-08-05 07:09:57 +02:00
int64_t * stream_durations ;
int64_t * stream_duration_offsets ;
2015-02-14 17:20:58 +02:00
int allow_raw_vfw ;
2007-09-05 03:22:37 +03:00
} MatroskaMuxContext ;
2007-09-05 03:24:56 +03:00
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
2013-09-21 04:15:49 +03:00
/** per-cuepoint-track - 5 1-byte EBML IDs, 5 1-byte EBML sizes, 4
2007-09-05 03:25:42 +03:00
* 8 - byte uint max */
2013-09-21 04:15:49 +03:00
# define MAX_CUETRACKPOS_SIZE 42
2007-09-05 03:24:56 +03:00
2007-09-05 03:25:42 +03:00
/** per-cuepoint - 2 1-byte EBML IDs, 2 1-byte EBML sizes, 8-byte uint max */
2014-06-07 01:54:50 +03:00
# define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE * num_tracks
2007-09-05 03:24:56 +03:00
2013-08-16 21:08:06 +03:00
/** Seek preroll value for opus */
# define OPUS_SEEK_PREROLL 80000000
2007-09-05 03:24:34 +03:00
static int ebml_id_size ( unsigned int id )
{
2014-06-07 01:54:50 +03:00
return ( av_log2 ( id + 1 ) - 1 ) / 7 + 1 ;
2007-09-05 03:24:34 +03:00
}
2011-02-20 12:04:12 +02:00
static void put_ebml_id ( AVIOContext * pb , unsigned int 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 ) ;
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
/**
2007-09-05 03:26:07 +03:00
* Calculate how many bytes are needed to represent a given number in EBML .
2007-09-05 03:24:42 +03:00
*/
2007-09-05 03:26:07 +03:00
static int ebml_num_size ( uint64_t num )
2007-09-05 03:24:42 +03:00
{
int bytes = 1 ;
2014-06-07 01:54:50 +03:00
while ( ( num + 1 ) > > bytes * 7 )
bytes + + ;
2007-09-05 03:24:42 +03:00
return bytes ;
}
2007-09-05 03:24:50 +03:00
/**
2007-09-05 03:26:07 +03:00
* Write a number in EBML variable length format .
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 .
2007-09-05 03:24:50 +03:00
* If zero , any number of bytes can be used .
*/
2011-02-20 12:04:12 +02:00
static void put_ebml_num ( AVIOContext * pb , uint64_t num , int bytes )
2007-09-05 03:22:37 +03:00
{
2007-09-05 03:26:07 +03:00
int i , needed_bytes = ebml_num_size ( num ) ;
2007-09-05 03:22:37 +03:00
// sizes larger than this are currently undefined in EBML
2014-07-01 20:58:57 +03:00
av_assert0 ( num < ( 1ULL < < 56 ) - 1 ) ;
2007-09-05 03:23:06 +03:00
2007-09-05 03:24:50 +03:00
if ( bytes = = 0 )
// don't care how many bytes are used, so use the min
bytes = needed_bytes ;
2007-09-05 03:25:24 +03:00
// the bytes needed to write the given size would exceed the bytes
// that we need to use, so write unknown size. This shouldn't happen.
2012-07-13 18:03:57 +03:00
av_assert0 ( bytes > = needed_bytes ) ;
2007-09-05 03:24:50 +03:00
2014-06-07 01:54:50 +03:00
num | = 1ULL < < bytes * 7 ;
2007-09-05 03:24:44 +03:00
for ( i = bytes - 1 ; i > = 0 ; i - - )
2014-07-01 20:58:57 +03:00
avio_w8 ( pb , ( uint8_t ) ( num > > i * 8 ) ) ;
2007-09-05 03:22:37 +03:00
}
2011-02-20 12:04:12 +02:00
static void put_ebml_uint ( AVIOContext * pb , unsigned int 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 ) ;
2007-09-05 03:26:07 +03:00
put_ebml_num ( 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
}
2013-11-15 21:00:37 +03:00
static void put_ebml_sint ( AVIOContext * pb , unsigned int elementid , int64_t val )
{
int i , bytes = 1 ;
2013-11-15 23:30:30 +03:00
uint64_t tmp = 2 * ( val < 0 ? val ^ - 1 : val ) ;
while ( tmp > > = 8 ) bytes + + ;
2013-11-15 21:00:37 +03:00
put_ebml_id ( pb , elementid ) ;
put_ebml_num ( pb , bytes , 0 ) ;
for ( i = bytes - 1 ; i > = 0 ; i - - )
2014-07-01 20:58:57 +03:00
avio_w8 ( pb , ( uint8_t ) ( val > > i * 8 ) ) ;
2013-11-15 21:00:37 +03:00
}
2011-02-20 12:04:12 +02:00
static void put_ebml_float ( AVIOContext * pb , unsigned int elementid , double val )
2007-09-05 03:22:47 +03:00
{
put_ebml_id ( pb , elementid ) ;
2007-09-05 03:26:07 +03:00
put_ebml_num ( 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
}
2011-02-20 12:04:12 +02:00
static void put_ebml_binary ( AVIOContext * pb , unsigned int 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 ) ;
2007-09-05 03:26:07 +03:00
put_ebml_num ( pb , size , 0 ) ;
2011-02-21 20:28:17 +02:00
avio_write ( pb , buf , size ) ;
2007-09-05 03:22:37 +03:00
}
2014-06-07 01:54:50 +03:00
static void put_ebml_string ( AVIOContext * pb , unsigned int elementid ,
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
*/
2011-02-20 12:04:12 +02:00
static void put_ebml_void ( AVIOContext * pb , uint64_t size )
2007-09-05 03:23:09 +03:00
{
2011-03-03 21:11:45 +02:00
int64_t currentpos = avio_tell ( pb ) ;
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
2007-09-05 03:23:09 +03:00
if ( size < 10 )
2016-07-26 19:18:43 +02:00
put_ebml_num ( pb , size - 2 , 0 ) ;
2007-09-05 03:23:09 +03:00
else
2014-06-07 01:54:50 +03:00
put_ebml_num ( pb , size - 9 , 8 ) ;
2013-07-15 23:20:57 +03:00
ffio_fill ( pb , 0 , currentpos + size - avio_tell ( pb ) ) ;
2007-09-05 03:23:09 +03:00
}
2014-06-07 01:54:50 +03:00
static ebml_master start_ebml_master ( AVIOContext * pb , unsigned int elementid ,
uint64_t expectedsize )
2007-09-05 03:22:37 +03:00
{
2007-09-05 03:26:07 +03:00
int bytes = expectedsize ? ebml_num_size ( expectedsize ) : 8 ;
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 ) ;
2014-06-07 01:54:50 +03: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 ;
2007-09-05 03:26:07 +03:00
put_ebml_num ( 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
}
2016-10-25 17:12:03 +02:00
static int start_ebml_master_crc32 ( AVIOContext * pb , AVIOContext * * dyn_cp , MatroskaMuxContext * mkv ,
ebml_master * master , unsigned int elementid , uint64_t expectedsize )
2016-10-03 04:46:19 +02:00
{
int ret ;
2016-10-07 14:01:20 +02:00
if ( ( ret = avio_open_dyn_buf ( dyn_cp ) ) < 0 )
2016-10-03 04:46:19 +02:00
return ret ;
2017-03-21 22:02:30 +02:00
if ( pb - > seekable & AVIO_SEEKABLE_NORMAL ) {
2016-10-03 04:46:19 +02:00
* master = start_ebml_master ( pb , elementid , expectedsize ) ;
2016-10-25 17:12:03 +02:00
if ( mkv - > write_crc & & mkv - > mode ! = MODE_WEBM )
put_ebml_void ( * dyn_cp , 6 ) ; /* Reserve space for CRC32 so position/size calculations using avio_tell() take it into account */
} else
2016-10-03 04:46:19 +02:00
* master = start_ebml_master ( * dyn_cp , elementid , expectedsize ) ;
return 0 ;
}
static void end_ebml_master_crc32 ( AVIOContext * pb , AVIOContext * * dyn_cp , MatroskaMuxContext * mkv ,
ebml_master master )
{
uint8_t * buf , crc [ 4 ] ;
2016-10-25 17:12:03 +02:00
int size , skip = 0 ;
2016-10-03 04:46:19 +02:00
2017-03-21 22:02:30 +02:00
if ( pb - > seekable & AVIO_SEEKABLE_NORMAL ) {
2016-10-03 04:46:19 +02:00
size = avio_close_dyn_buf ( * dyn_cp , & buf ) ;
2016-10-06 23:17:04 +02:00
if ( mkv - > write_crc & & mkv - > mode ! = MODE_WEBM ) {
2016-10-25 17:12:03 +02:00
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 ) ;
2016-10-03 04:46:19 +02:00
put_ebml_binary ( pb , EBML_ID_CRC32 , crc , sizeof ( crc ) ) ;
}
2016-10-25 17:12:03 +02:00
avio_write ( pb , buf + skip , size - skip ) ;
2016-10-03 04:46:19 +02:00
end_ebml_master ( pb , master ) ;
} else {
end_ebml_master ( * dyn_cp , master ) ;
size = avio_close_dyn_buf ( * dyn_cp , & buf ) ;
avio_write ( pb , buf , size ) ;
}
av_free ( buf ) ;
* dyn_cp = NULL ;
}
2017-01-05 02:24:03 +02:00
/**
* Complete ebml master whithout destroying the buffer , allowing for later updates
*/
static void end_ebml_master_crc32_preliminary ( AVIOContext * pb , AVIOContext * * dyn_cp , MatroskaMuxContext * mkv ,
ebml_master master )
{
2017-03-21 22:02:30 +02:00
if ( pb - > seekable & AVIO_SEEKABLE_NORMAL ) {
2017-01-05 02:24:03 +02:00
uint8_t * buf ;
int size = avio_get_dyn_buf ( * dyn_cp , & buf ) ;
avio_write ( pb , buf , size ) ;
end_ebml_master ( pb , master ) ;
}
}
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 .
*/
static void mkv_free ( MatroskaMuxContext * mkv ) {
2017-01-26 21:26:46 +02:00
uint8_t * buf ;
if ( mkv - > dyn_bc ) {
avio_close_dyn_buf ( mkv - > dyn_bc , & buf ) ;
av_free ( buf ) ;
}
if ( mkv - > info_bc ) {
avio_close_dyn_buf ( mkv - > info_bc , & buf ) ;
av_free ( buf ) ;
}
if ( mkv - > tracks_bc ) {
avio_close_dyn_buf ( mkv - > tracks_bc , & buf ) ;
av_free ( buf ) ;
}
if ( mkv - > tags_bc ) {
avio_close_dyn_buf ( mkv - > tags_bc , & buf ) ;
av_free ( buf ) ;
}
2015-08-19 10:06:56 +02:00
if ( mkv - > main_seekhead ) {
av_freep ( & mkv - > main_seekhead - > entries ) ;
av_freep ( & mkv - > main_seekhead ) ;
}
if ( mkv - > cues ) {
av_freep ( & mkv - > cues - > entries ) ;
av_freep ( & mkv - > cues ) ;
}
2016-10-07 07:46:22 +02:00
if ( mkv - > attachments ) {
av_freep ( & mkv - > attachments - > entries ) ;
av_freep ( & mkv - > attachments ) ;
}
2015-08-19 10:06:56 +02:00
av_freep ( & mkv - > tracks ) ;
av_freep ( & mkv - > stream_durations ) ;
av_freep ( & mkv - > stream_duration_offsets ) ;
}
2007-09-05 03:24:25 +03:00
/**
2007-09-05 03:25:16 +03:00
* Initialize a mkv_seekhead element to be ready to index level 1 Matroska
* elements . If a maximum number of elements is specified , enough space
* will be reserved at the current file location to write a seek head of
* that size .
2007-09-05 03:24:25 +03:00
*
2007-09-05 03:25:16 +03:00
* @ param segment_offset The absolute offset to the position in the file
2007-09-05 03:26:11 +03:00
* where the segment begins .
* @ param numelements The maximum number of elements that will be indexed
2007-09-05 03:25:16 +03:00
* by this seek head , 0 if unlimited .
2007-09-05 03:24:25 +03:00
*/
2014-06-07 01:54:50 +03:00
static mkv_seekhead * mkv_start_seekhead ( AVIOContext * pb , int64_t segment_offset ,
int numelements )
2007-09-05 03:23:19 +03:00
{
mkv_seekhead * new_seekhead = av_mallocz ( sizeof ( mkv_seekhead ) ) ;
2014-08-14 23:31:24 +03:00
if ( ! new_seekhead )
2007-09-05 03:23:19 +03:00
return NULL ;
new_seekhead - > segment_offset = segment_offset ;
if ( numelements > 0 ) {
2011-03-03 21:11:45 +02:00
new_seekhead - > filepos = avio_tell ( pb ) ;
2007-09-05 03:25:16 +03:00
// 21 bytes max for a seek entry, 10 bytes max for the SeekHead ID
2016-10-03 23:59:42 +02:00
// and size, 6 bytes for a CRC32 element, and 3 bytes to guarantee
// that an EBML void element will fit afterwards
new_seekhead - > reserved_size = numelements * MAX_SEEKENTRY_SIZE + 19 ;
2014-06-07 01:54:50 +03:00
new_seekhead - > max_entries = numelements ;
2007-09-05 03:23:19 +03:00
put_ebml_void ( pb , new_seekhead - > reserved_size ) ;
}
return new_seekhead ;
}
static int mkv_add_seekhead_entry ( mkv_seekhead * seekhead , unsigned int elementid , uint64_t filepos )
{
mkv_seekhead_entry * entries = seekhead - > entries ;
// don't store more elements than we reserved space for
if ( seekhead - > max_entries > 0 & & seekhead - > max_entries < = seekhead - > num_entries )
return - 1 ;
2013-09-11 14:34:25 +03:00
entries = av_realloc_array ( entries , seekhead - > num_entries + 1 , sizeof ( mkv_seekhead_entry ) ) ;
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
seekhead - > entries = entries ;
2007-09-05 03:23:19 +03:00
2013-09-12 10:49:38 +03:00
seekhead - > entries [ seekhead - > num_entries ] . elementid = elementid ;
seekhead - > entries [ seekhead - > num_entries + + ] . segmentpos = filepos - seekhead - > segment_offset ;
2007-09-05 03:23:19 +03:00
return 0 ;
}
2007-09-05 03:24:25 +03:00
/**
2007-09-05 03:25:16 +03:00
* Write the seek head to the file and free it . If a maximum number of
* elements was specified to mkv_start_seekhead ( ) , the seek head will
* be written at the location reserved for it . Otherwise , it is written
* at the current location in the file .
2007-09-05 03:24:25 +03:00
*
2010-03-04 10:53:04 +02:00
* @ return The file offset where the seekhead was written ,
* - 1 if an error occurred .
2007-09-05 03:24:25 +03:00
*/
2015-08-19 10:06:56 +02:00
static int64_t mkv_write_seekhead ( AVIOContext * pb , MatroskaMuxContext * mkv )
2007-09-05 03:23:19 +03:00
{
2016-10-03 23:59:42 +02:00
AVIOContext * dyn_cp ;
2015-08-19 10:06:56 +02:00
mkv_seekhead * seekhead = mkv - > main_seekhead ;
2007-09-05 03:24:56 +03:00
ebml_master metaseek , seekentry ;
2008-10-03 13:16:29 +03:00
int64_t currentpos ;
2007-09-05 03:23:19 +03:00
int i ;
2011-03-03 21:11:45 +02:00
currentpos = avio_tell ( pb ) ;
2007-09-05 03:23:19 +03:00
2011-11-07 22:06:01 +03:00
if ( seekhead - > reserved_size > 0 ) {
if ( avio_seek ( pb , seekhead - > filepos , SEEK_SET ) < 0 ) {
currentpos = - 1 ;
goto fail ;
}
}
2007-09-05 03:23:19 +03:00
2016-10-25 17:12:03 +02:00
if ( start_ebml_master_crc32 ( pb , & dyn_cp , mkv , & metaseek , MATROSKA_ID_SEEKHEAD ,
2016-10-03 23:59:42 +02:00
seekhead - > reserved_size ) < 0 ) {
currentpos = - 1 ;
goto fail ;
}
2007-09-05 03:23:19 +03:00
for ( i = 0 ; i < seekhead - > num_entries ; i + + ) {
mkv_seekhead_entry * entry = & seekhead - > entries [ i ] ;
2016-10-03 23:59:42 +02:00
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 ) ;
put_ebml_num ( dyn_cp , ebml_id_size ( entry - > elementid ) , 0 ) ;
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
}
2016-10-03 23:59:42 +02:00
end_ebml_master_crc32 ( pb , & dyn_cp , mkv , metaseek ) ;
2007-09-05 03:23:19 +03:00
if ( seekhead - > reserved_size > 0 ) {
2011-03-03 21:11:45 +02:00
uint64_t remaining = seekhead - > filepos + seekhead - > reserved_size - avio_tell ( pb ) ;
2007-09-05 03:23:19 +03:00
put_ebml_void ( pb , remaining ) ;
2011-02-28 15:57:54 +02:00
avio_seek ( pb , currentpos , SEEK_SET ) ;
2007-09-05 03:23:19 +03:00
currentpos = seekhead - > filepos ;
}
2011-11-07 22:06:01 +03:00
fail :
2015-08-19 10:06:56 +02:00
av_freep ( & mkv - > main_seekhead - > entries ) ;
av_freep ( & mkv - > main_seekhead ) ;
2007-09-05 03:23:19 +03:00
return currentpos ;
}
2014-06-07 01:54:50 +03:00
static mkv_cues * mkv_start_cues ( int64_t segment_offset )
2007-09-05 03:23:27 +03:00
{
mkv_cues * cues = av_mallocz ( sizeof ( mkv_cues ) ) ;
2014-08-14 23:31:24 +03:00
if ( ! cues )
2007-09-05 03:23:27 +03:00
return NULL ;
cues - > segment_offset = segment_offset ;
return cues ;
}
2014-05-07 19:46:40 +03:00
static int mkv_add_cuepoint ( mkv_cues * cues , int stream , int tracknum , int64_t ts ,
int64_t cluster_pos , int64_t relative_pos , int64_t duration )
2007-09-05 03:23:27 +03:00
{
mkv_cuepoint * entries = cues - > entries ;
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
2013-09-12 10:49:38 +03:00
cues - > entries [ cues - > num_entries ] . pts = ts ;
2014-05-07 19:46:40 +03:00
cues - > entries [ cues - > num_entries ] . stream_idx = stream ;
cues - > entries [ cues - > num_entries ] . tracknum = tracknum ;
2013-09-12 16:12:52 +03:00
cues - > entries [ cues - > num_entries ] . cluster_pos = cluster_pos - cues - > segment_offset ;
2013-09-21 04:15:49 +03:00
cues - > entries [ cues - > num_entries ] . relative_pos = relative_pos ;
cues - > entries [ cues - > num_entries + + ] . duration = duration ;
2007-09-05 03:23:27 +03:00
return 0 ;
}
2014-05-22 10:22:15 +03:00
static int64_t mkv_write_cues ( AVFormatContext * s , mkv_cues * cues , mkv_track * tracks , int num_tracks )
2007-09-05 03:23:27 +03:00
{
2016-10-04 00:15:24 +02:00
MatroskaMuxContext * mkv = s - > priv_data ;
AVIOContext * dyn_cp , * pb = s - > pb ;
2007-09-05 03:24:56 +03:00
ebml_master cues_element ;
2008-10-03 13:16:29 +03:00
int64_t currentpos ;
2016-10-04 00:15:24 +02:00
int i , j , ret ;
2007-09-05 03:23:27 +03:00
2011-03-03 21:11:45 +02:00
currentpos = avio_tell ( pb ) ;
2016-10-25 17:12:03 +02:00
ret = start_ebml_master_crc32 ( pb , & dyn_cp , mkv , & cues_element , MATROSKA_ID_CUES , 0 ) ;
2016-10-04 00:15:24 +02:00
if ( ret < 0 )
return ret ;
2007-09-05 03:23:27 +03:00
for ( i = 0 ; i < cues - > num_entries ; i + + ) {
2007-09-05 03:24:56 +03:00
ebml_master cuepoint , track_positions ;
2007-09-05 03:23:27 +03:00
mkv_cuepoint * entry = & cues - > entries [ i ] ;
uint64_t pts = entry - > pts ;
2014-08-09 05:49:09 +03:00
int ctp_nb = 0 ;
2007-09-05 03:23:27 +03:00
2014-08-09 05:49:09 +03:00
// Calculate the number of entries, so we know the element size
for ( j = 0 ; j < num_tracks ; j + + )
tracks [ j ] . has_cue = 0 ;
for ( j = 0 ; j < cues - > num_entries - i & & entry [ j ] . pts = = pts ; j + + ) {
int tracknum = entry [ j ] . stream_idx ;
av_assert0 ( tracknum > = 0 & & tracknum < num_tracks ) ;
2016-04-10 21:58:15 +02:00
if ( tracks [ tracknum ] . has_cue & & s - > streams [ tracknum ] - > codecpar - > codec_type ! = AVMEDIA_TYPE_SUBTITLE )
2014-08-09 05:49:09 +03:00
continue ;
tracks [ tracknum ] . has_cue = 1 ;
ctp_nb + + ;
}
2016-10-04 00:15:24 +02:00
cuepoint = start_ebml_master ( dyn_cp , MATROSKA_ID_POINTENTRY , MAX_CUEPOINT_SIZE ( ctp_nb ) ) ;
put_ebml_uint ( dyn_cp , 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
2013-02-10 23:32:37 +03:00
for ( j = 0 ; j < num_tracks ; j + + )
tracks [ j ] . has_cue = 0 ;
2007-09-05 03:23:27 +03:00
for ( j = 0 ; j < cues - > num_entries - i & & entry [ j ] . pts = = pts ; j + + ) {
2014-05-07 19:46:40 +03:00
int tracknum = entry [ j ] . stream_idx ;
2013-02-24 01:05:44 +03:00
av_assert0 ( tracknum > = 0 & & tracknum < num_tracks ) ;
2016-04-10 21:58:15 +02:00
if ( tracks [ tracknum ] . has_cue & & s - > streams [ tracknum ] - > codecpar - > codec_type ! = AVMEDIA_TYPE_SUBTITLE )
2013-02-10 23:32:37 +03:00
continue ;
2013-02-24 01:05:44 +03:00
tracks [ tracknum ] . has_cue = 1 ;
2016-10-04 00:15:24 +02:00
track_positions = start_ebml_master ( dyn_cp , MATROSKA_ID_CUETRACKPOSITION , MAX_CUETRACKPOS_SIZE ) ;
put_ebml_uint ( dyn_cp , MATROSKA_ID_CUETRACK , entry [ j ] . tracknum ) ;
put_ebml_uint ( dyn_cp , MATROSKA_ID_CUECLUSTERPOSITION , entry [ j ] . cluster_pos ) ;
put_ebml_uint ( dyn_cp , MATROSKA_ID_CUERELATIVEPOSITION , entry [ j ] . relative_pos ) ;
2013-09-21 04:15:49 +03:00
if ( entry [ j ] . duration ! = - 1 )
2016-10-04 00:15:24 +02:00
put_ebml_uint ( dyn_cp , MATROSKA_ID_CUEDURATION , entry [ j ] . duration ) ;
end_ebml_master ( dyn_cp , track_positions ) ;
2007-09-05 03:23:27 +03:00
}
i + = j - 1 ;
2016-10-04 00:15:24 +02:00
end_ebml_master ( dyn_cp , cuepoint ) ;
2007-09-05 03:23:27 +03:00
}
2016-10-04 00:15:24 +02:00
end_ebml_master_crc32 ( pb , & dyn_cp , mkv , cues_element ) ;
2007-09-05 03:23:27 +03:00
return currentpos ;
}
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
static int put_xiph_codecpriv ( AVFormatContext * s , AVIOContext * pb , AVCodecParameters * par )
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 ;
2007-09-05 03:24:06 +03:00
int 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 ;
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 ( avpriv_split_xiph_headers ( par - > extradata , par - > extradata_size ,
2007-09-05 03:23:13 +03:00
first_header_size , header_start , header_len ) < 0 ) {
2007-09-05 03:26:09 +03:00
av_log ( s , AV_LOG_ERROR , " Extradata corrupt. \n " ) ;
2007-09-05 03:23:13 +03:00
return - 1 ;
}
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
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
static int put_wv_codecpriv ( AVIOContext * pb , AVCodecParameters * par )
2013-05-28 11:09:21 +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 - > extradata & & par - > extradata_size = = 2 )
avio_write ( pb , par - > extradata , 2 ) ;
2013-05-28 11:09:21 +03:00
else
avio_wl16 ( pb , 0x403 ) ; // fallback to the version mentioned in matroska specs
return 0 ;
}
2014-05-26 14:53:13 +03:00
static int put_flac_codecpriv ( AVFormatContext * s ,
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
AVIOContext * pb , AVCodecParameters * par )
2014-05-26 14:53: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
int write_comment = ( par - > channel_layout & &
! ( par - > channel_layout & ~ 0x3ffffULL ) & &
! ff_flac_is_native_layout ( par - > channel_layout ) ) ;
int ret = ff_flac_write_header ( pb , par - > extradata , par - > 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 ;
uint8_t buf [ 32 ] , * data , * p ;
2015-05-11 15:37:38 +02:00
int64_t len ;
2014-05-26 14:53: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
snprintf ( buf , sizeof ( buf ) , " 0x% " PRIx64 , par - > channel_layout ) ;
2014-05-26 14:53:13 +03:00
av_dict_set ( & dict , " WAVEFORMATEXTENSIBLE_CHANNEL_MASK " , buf , 0 ) ;
len = ff_vorbiscomment_length ( dict , vendor ) ;
2015-05-11 15:37:38 +02:00
if ( len > = ( ( 1 < < 24 ) - 4 ) )
return AVERROR ( EINVAL ) ;
2014-05-26 14:53:13 +03:00
data = av_malloc ( len + 4 ) ;
if ( ! data ) {
av_dict_free ( & dict ) ;
return AVERROR ( ENOMEM ) ;
}
data [ 0 ] = 0x84 ;
AV_WB24 ( data + 1 , len ) ;
p = data + 4 ;
ff_vorbiscomment_write ( & p , & dict , vendor ) ;
avio_write ( pb , data , len + 4 ) ;
av_freep ( & data ) ;
av_dict_free ( & dict ) ;
}
return 0 ;
}
2017-04-13 17:43:11 +02:00
static int get_aac_sample_rates ( AVFormatContext * s , uint8_t * extradata , int extradata_size ,
2015-01-01 19:22:46 +02:00
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
2017-04-13 17:43:11 +02:00
ret = avpriv_mpeg4audio_get_config ( & mp4ac , extradata ,
extradata_size * 8 , 1 ) ;
/* 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 . */
if ( ret < 0 & & ( extradata_size | | ! ( s - > pb - > seekable & AVIO_SEEKABLE_NORMAL ) ) ) {
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
}
2017-04-13 17:43:11 +02:00
static int mkv_write_native_codecprivate ( AVFormatContext * s , 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 ,
2014-06-07 02:21:13 +03:00
AVIOContext * dyn_cp )
{
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 :
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
return put_xiph_codecpriv ( s , dyn_cp , par ) ;
2014-06-07 02:21:13 +03:00
case AV_CODEC_ID_FLAC :
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
return put_flac_codecpriv ( s , dyn_cp , par ) ;
2014-06-07 02:21:13 +03:00
case AV_CODEC_ID_WAVPACK :
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
return put_wv_codecpriv ( dyn_cp , par ) ;
2014-06-07 02:21:13 +03:00
case AV_CODEC_ID_H264 :
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
return ff_isom_write_avcc ( dyn_cp , par - > extradata ,
par - > extradata_size ) ;
2014-06-07 02:21:13 +03:00
case AV_CODEC_ID_HEVC :
2016-04-10 21:58:15 +02:00
ff_isom_write_hvcc ( dyn_cp , par - > extradata ,
par - > extradata_size , 0 ) ;
2015-03-18 21:35:26 +02:00
return 0 ;
2018-07-23 17:33:55 +02:00
case AV_CODEC_ID_AV1 :
2018-08-11 04:25:32 +02:00
if ( par - > extradata_size )
return ff_isom_write_av1c ( dyn_cp , par - > extradata ,
par - > extradata_size ) ;
else
put_ebml_void ( pb , 4 + 3 ) ;
break ;
2014-06-07 02:21:13 +03:00
case AV_CODEC_ID_ALAC :
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 - > 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
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
avio_write ( dyn_cp , par - > extradata + 12 ,
par - > 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 :
if ( par - > extradata_size )
avio_write ( dyn_cp , par - > extradata , par - > extradata_size ) ;
else
put_ebml_void ( pb , MAX_PCE_SIZE + 2 + 4 ) ;
break ;
2014-06-07 02:21:13 +03:00
default :
2016-04-10 21:58:15 +02:00
if ( par - > codec_id = = AV_CODEC_ID_PRORES & &
ff_codec_get_id ( ff_codec_movvideo_tags , par - > codec_tag ) = = AV_CODEC_ID_PRORES ) {
avio_wl32 ( dyn_cp , par - > codec_tag ) ;
} else if ( par - > extradata_size & & par - > codec_id ! = AV_CODEC_ID_TTA )
avio_write ( dyn_cp , par - > extradata , par - > extradata_size ) ;
2014-06-07 02:21:13 +03:00
}
return 0 ;
}
2014-06-07 01:54:50 +03:00
static int mkv_write_codecprivate ( AVFormatContext * s , 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 ,
int native_id , int qt_id )
2007-09-05 03:25:49 +03:00
{
2011-02-20 12:04:12 +02:00
AVIOContext * dyn_cp ;
2007-09-05 03:25:52 +03:00
uint8_t * codecpriv ;
2007-11-21 09:41:00 +02:00
int ret , codecpriv_size ;
2007-09-05 03:25:52 +03:00
2011-03-17 09:13:34 +02:00
ret = avio_open_dyn_buf ( & dyn_cp ) ;
2014-06-07 01:54:50 +03:00
if ( ret < 0 )
2007-11-21 09:41:00 +02:00
return ret ;
2007-09-05 03:25:49 +03:00
if ( native_id ) {
2017-04-13 17:43:11 +02:00
ret = mkv_write_native_codecprivate ( s , pb , par , dyn_cp ) ;
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
& & ( ! par - > extradata_size | | ff_codec_get_id ( ff_codec_movvideo_tags , AV_RL32 ( par - > extradata + 4 ) ) ! = par - > codec_id )
) {
int i ;
avio_wb32 ( dyn_cp , 0x5a + par - > extradata_size ) ;
avio_wl32 ( dyn_cp , par - > codec_tag ) ;
for ( i = 0 ; i < 0x5a - 8 ; i + + )
avio_w8 ( dyn_cp , 0 ) ;
}
avio_write ( dyn_cp , par - > extradata , par - > 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 ) ) ;
2013-02-10 20:02:59 +03:00
ret = AVERROR ( EINVAL ) ;
2010-03-04 10:53:11 +02:00
}
2007-09-05 03:25:49 +03:00
2017-07-14 13:03:20 +02:00
ff_put_bmp_header ( dyn_cp , par , 0 , 0 ) ;
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 ) ) ;
2013-02-10 20:02:59 +03:00
ret = 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
2016-04-10 21:58:15 +02:00
ff_put_wav_header ( s , dyn_cp , par , FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX ) ;
2007-09-05 03:25:49 +03:00
}
2007-09-05 03:25:52 +03:00
2011-03-17 09:16:07 +02:00
codecpriv_size = avio_close_dyn_buf ( dyn_cp , & codecpriv ) ;
2007-09-05 03:25:52 +03:00
if ( codecpriv_size )
2014-06-07 01:54:50 +03:00
put_ebml_binary ( pb , MATROSKA_ID_CODECPRIVATE , codecpriv ,
codecpriv_size ) ;
2007-09-05 03:25:52 +03:00
av_free ( codecpriv ) ;
return ret ;
2007-09-05 03:25:49 +03:00
}
2016-04-10 21:58:15 +02:00
static int mkv_write_video_color ( AVIOContext * pb , AVCodecParameters * par , AVStream * st ) {
2016-09-28 00:20:57 +02:00
AVIOContext * dyn_cp ;
uint8_t * colorinfo_ptr ;
2016-03-08 00:42:47 +02:00
int side_data_size = 0 ;
2016-09-28 00:20:57 +02:00
int ret , colorinfo_size ;
2017-04-25 20:42:51 +02:00
const uint8_t * side_data ;
2016-09-28 00:20:57 +02:00
ret = avio_open_dyn_buf ( & dyn_cp ) ;
if ( ret < 0 )
return ret ;
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 ) {
2016-09-28 00:20:57 +02:00
put_ebml_uint ( dyn_cp , MATROSKA_ID_VIDEOCOLORTRANSFERCHARACTERISTICS ,
2016-04-10 21:58:15 +02:00
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 ) {
2016-09-28 00:20:57 +02:00
put_ebml_uint ( dyn_cp , 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 ) {
2016-09-28 00:20:57 +02:00
put_ebml_uint ( dyn_cp , 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 ) {
2016-09-28 00:20:57 +02:00
put_ebml_uint ( dyn_cp , 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 ) ;
put_ebml_uint ( dyn_cp , MATROSKA_ID_VIDEOCOLORCHROMASITINGHORZ , ( xpos > > 7 ) + 1 ) ;
put_ebml_uint ( dyn_cp , MATROSKA_ID_VIDEOCOLORCHROMASITINGVERT , ( ypos > > 7 ) + 1 ) ;
}
2017-04-25 20:42:51 +02:00
side_data = av_stream_get_side_data ( st , AV_PKT_DATA_CONTENT_LIGHT_LEVEL ,
& side_data_size ) ;
if ( side_data_size ) {
const AVContentLightMetadata * metadata =
( const AVContentLightMetadata * ) side_data ;
put_ebml_uint ( dyn_cp , MATROSKA_ID_VIDEOCOLORMAXCLL , metadata - > MaxCLL ) ;
put_ebml_uint ( dyn_cp , MATROSKA_ID_VIDEOCOLORMAXFALL , metadata - > MaxFALL ) ;
}
side_data = av_stream_get_side_data ( st , AV_PKT_DATA_MASTERING_DISPLAY_METADATA ,
& side_data_size ) ;
2016-03-08 00:42:47 +02:00
if ( side_data_size = = sizeof ( AVMasteringDisplayMetadata ) ) {
ebml_master meta_element = start_ebml_master (
2016-09-28 00:20:57 +02:00
dyn_cp , MATROSKA_ID_VIDEOCOLORMASTERINGMETA , 0 ) ;
2016-03-08 00:42:47 +02:00
const AVMasteringDisplayMetadata * metadata =
( const AVMasteringDisplayMetadata * ) side_data ;
if ( metadata - > has_primaries ) {
2016-09-28 00:20:57 +02:00
put_ebml_float ( dyn_cp , MATROSKA_ID_VIDEOCOLOR_RX ,
2016-03-08 00:42:47 +02:00
av_q2d ( metadata - > display_primaries [ 0 ] [ 0 ] ) ) ;
2016-09-28 00:20:57 +02:00
put_ebml_float ( dyn_cp , MATROSKA_ID_VIDEOCOLOR_RY ,
2016-03-08 00:42:47 +02:00
av_q2d ( metadata - > display_primaries [ 0 ] [ 1 ] ) ) ;
2016-09-28 00:20:57 +02:00
put_ebml_float ( dyn_cp , MATROSKA_ID_VIDEOCOLOR_GX ,
2016-03-08 00:42:47 +02:00
av_q2d ( metadata - > display_primaries [ 1 ] [ 0 ] ) ) ;
2016-09-28 00:20:57 +02:00
put_ebml_float ( dyn_cp , MATROSKA_ID_VIDEOCOLOR_GY ,
2016-03-08 00:42:47 +02:00
av_q2d ( metadata - > display_primaries [ 1 ] [ 1 ] ) ) ;
2016-09-28 00:20:57 +02:00
put_ebml_float ( dyn_cp , MATROSKA_ID_VIDEOCOLOR_BX ,
2016-03-08 00:42:47 +02:00
av_q2d ( metadata - > display_primaries [ 2 ] [ 0 ] ) ) ;
2016-09-28 00:20:57 +02:00
put_ebml_float ( dyn_cp , MATROSKA_ID_VIDEOCOLOR_BY ,
2016-03-08 00:42:47 +02:00
av_q2d ( metadata - > display_primaries [ 2 ] [ 1 ] ) ) ;
2016-09-28 00:20:57 +02:00
put_ebml_float ( dyn_cp , MATROSKA_ID_VIDEOCOLOR_WHITEX ,
2016-03-08 00:42:47 +02:00
av_q2d ( metadata - > white_point [ 0 ] ) ) ;
2016-09-28 00:20:57 +02:00
put_ebml_float ( dyn_cp , MATROSKA_ID_VIDEOCOLOR_WHITEY ,
2016-03-08 00:42:47 +02:00
av_q2d ( metadata - > white_point [ 1 ] ) ) ;
}
if ( metadata - > has_luminance ) {
2016-09-28 00:20:57 +02:00
put_ebml_float ( dyn_cp , MATROSKA_ID_VIDEOCOLOR_LUMINANCEMAX ,
2016-03-08 00:42:47 +02:00
av_q2d ( metadata - > max_luminance ) ) ;
2016-09-28 00:20:57 +02:00
put_ebml_float ( dyn_cp , MATROSKA_ID_VIDEOCOLOR_LUMINANCEMIN ,
2016-03-08 00:42:47 +02:00
av_q2d ( metadata - > min_luminance ) ) ;
}
2016-09-28 00:20:57 +02:00
end_ebml_master ( dyn_cp , meta_element ) ;
2016-03-08 00:42:47 +02:00
}
2016-09-28 00:20:57 +02:00
colorinfo_size = avio_close_dyn_buf ( dyn_cp , & colorinfo_ptr ) ;
if ( colorinfo_size ) {
2017-01-28 18:44:59 +02:00
ebml_master colorinfo = start_ebml_master ( pb , MATROSKA_ID_VIDEOCOLOR , colorinfo_size ) ;
2016-09-28 00:20:57 +02:00
avio_write ( pb , colorinfo_ptr , colorinfo_size ) ;
end_ebml_master ( pb , colorinfo ) ;
}
av_free ( colorinfo_ptr ) ;
2016-03-08 00:42:47 +02:00
return 0 ;
}
2017-03-09 20:11:49 +02:00
static int mkv_write_video_projection ( AVFormatContext * s , AVIOContext * pb ,
AVStream * st )
2017-03-08 21:12:32 +02:00
{
2017-03-09 20:11:49 +02:00
AVIOContext b ;
AVIOContext * dyn_cp ;
2017-03-08 21:12:32 +02:00
int side_data_size = 0 ;
2017-03-09 20:11:49 +02:00
int ret , projection_size ;
uint8_t * projection_ptr ;
uint8_t private [ 20 ] ;
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 ,
2017-03-08 21:12:32 +02:00
& side_data_size ) ;
2017-10-21 17:56:33 +02:00
if ( ! side_data_size )
2017-03-09 20:11:49 +02:00
return 0 ;
ret = avio_open_dyn_buf ( & dyn_cp ) ;
if ( ret < 0 )
return ret ;
2017-03-08 21:12:32 +02:00
2017-03-09 20:11:49 +02:00
switch ( spherical - > projection ) {
case AV_SPHERICAL_EQUIRECTANGULAR :
put_ebml_uint ( dyn_cp , MATROSKA_ID_VIDEOPROJECTIONTYPE ,
MATROSKA_VIDEO_PROJECTION_TYPE_EQUIRECTANGULAR ) ;
break ;
case AV_SPHERICAL_EQUIRECTANGULAR_TILE :
ffio_init_context ( & b , private , 20 , 1 , NULL , NULL , NULL , NULL ) ;
put_ebml_uint ( dyn_cp , MATROSKA_ID_VIDEOPROJECTIONTYPE ,
MATROSKA_VIDEO_PROJECTION_TYPE_EQUIRECTANGULAR ) ;
avio_wb32 ( & b , 0 ) ; // version + flags
avio_wb32 ( & b , spherical - > bound_top ) ;
avio_wb32 ( & b , spherical - > bound_bottom ) ;
avio_wb32 ( & b , spherical - > bound_left ) ;
avio_wb32 ( & b , spherical - > bound_right ) ;
put_ebml_binary ( dyn_cp , MATROSKA_ID_VIDEOPROJECTIONPRIVATE ,
2017-10-21 17:56:33 +02:00
private , avio_tell ( & b ) ) ;
2017-03-09 20:11:49 +02:00
break ;
case AV_SPHERICAL_CUBEMAP :
ffio_init_context ( & b , private , 12 , 1 , NULL , NULL , NULL , NULL ) ;
put_ebml_uint ( dyn_cp , MATROSKA_ID_VIDEOPROJECTIONTYPE ,
MATROSKA_VIDEO_PROJECTION_TYPE_CUBEMAP ) ;
avio_wb32 ( & b , 0 ) ; // version + flags
avio_wb32 ( & b , 0 ) ; // layout
avio_wb32 ( & b , spherical - > padding ) ;
put_ebml_binary ( dyn_cp , MATROSKA_ID_VIDEOPROJECTIONPRIVATE ,
2017-10-21 17:56:33 +02:00
private , avio_tell ( & b ) ) ;
2017-03-09 20:11:49 +02:00
break ;
default :
av_log ( s , AV_LOG_WARNING , " Unknown projection type \n " ) ;
goto end ;
}
if ( spherical - > yaw )
put_ebml_float ( dyn_cp , MATROSKA_ID_VIDEOPROJECTIONPOSEYAW ,
( double ) spherical - > yaw / ( 1 < < 16 ) ) ;
if ( spherical - > pitch )
put_ebml_float ( dyn_cp , MATROSKA_ID_VIDEOPROJECTIONPOSEPITCH ,
( double ) spherical - > pitch / ( 1 < < 16 ) ) ;
if ( spherical - > roll )
put_ebml_float ( dyn_cp , MATROSKA_ID_VIDEOPROJECTIONPOSEROLL ,
( double ) spherical - > roll / ( 1 < < 16 ) ) ;
2017-03-08 21:12:32 +02:00
end :
2017-03-09 20:11:49 +02:00
projection_size = avio_close_dyn_buf ( dyn_cp , & projection_ptr ) ;
if ( projection_size ) {
ebml_master projection = start_ebml_master ( pb ,
MATROSKA_ID_VIDEOPROJECTION ,
projection_size ) ;
avio_write ( pb , projection_ptr , projection_size ) ;
end_ebml_master ( pb , projection ) ;
2017-03-08 21:12:32 +02:00
}
2017-03-09 20:11:49 +02:00
av_freep ( & projection_ptr ) ;
2017-03-08 21:12:32 +02:00
return 0 ;
}
2016-10-05 00:42:55 +02:00
static void mkv_write_field_order ( AVIOContext * pb , int mode ,
2016-04-02 22:35:44 +02:00
enum AVFieldOrder field_order )
{
switch ( field_order ) {
case AV_FIELD_UNKNOWN :
break ;
case AV_FIELD_PROGRESSIVE :
put_ebml_uint ( pb , MATROSKA_ID_VIDEOFLAGINTERLACED ,
MATROSKA_VIDEO_INTERLACE_FLAG_PROGRESSIVE ) ;
break ;
case AV_FIELD_TT :
case AV_FIELD_BB :
case AV_FIELD_TB :
case AV_FIELD_BT :
put_ebml_uint ( pb , MATROSKA_ID_VIDEOFLAGINTERLACED ,
MATROSKA_VIDEO_INTERLACE_FLAG_INTERLACED ) ;
2016-10-05 00:42:55 +02:00
if ( mode ! = MODE_WEBM ) {
switch ( field_order ) {
case AV_FIELD_TT :
put_ebml_uint ( pb , MATROSKA_ID_VIDEOFIELDORDER ,
MATROSKA_VIDEO_FIELDORDER_TT ) ;
break ;
case AV_FIELD_BB :
put_ebml_uint ( pb , MATROSKA_ID_VIDEOFIELDORDER ,
MATROSKA_VIDEO_FIELDORDER_BB ) ;
break ;
case AV_FIELD_TB :
put_ebml_uint ( pb , MATROSKA_ID_VIDEOFIELDORDER ,
MATROSKA_VIDEO_FIELDORDER_TB ) ;
break ;
case AV_FIELD_BT :
put_ebml_uint ( pb , MATROSKA_ID_VIDEOFIELDORDER ,
MATROSKA_VIDEO_FIELDORDER_BT ) ;
break ;
}
2016-04-02 22:35:44 +02:00
}
}
}
2014-08-29 01:30:47 +03:00
2014-08-05 13:16:17 +03:00
static int mkv_write_stereo_mode ( AVFormatContext * s , AVIOContext * pb ,
2014-10-27 23:18:03 +02:00
AVStream * st , int mode , int * h_width , int * h_height )
2014-06-07 02:38:00 +03:00
{
2014-08-05 13:16:17 +03:00
int i ;
2014-08-29 01:30:47 +03:00
int ret = 0 ;
2014-08-05 13:16:17 +03:00
AVDictionaryEntry * tag ;
MatroskaVideoStereoModeType format = MATROSKA_VIDEO_STEREOMODE_TYPE_NB ;
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
2014-08-29 02:26:52 +03:00
for ( 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
2014-10-22 16:36:32 +03:00
// iterate to find the stereo3d side data
2014-08-05 13:16:17 +03:00
for ( i = 0 ; i < st - > nb_side_data ; i + + ) {
AVPacketSideData sd = st - > side_data [ i ] ;
if ( sd . type = = AV_PKT_DATA_STEREO3D ) {
AVStereo3D * stereo = ( AVStereo3D * ) sd . data ;
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 ;
2014-10-27 23:18:03 +02:00
* h_width = 2 ;
2014-08-05 13:16:17 +03:00
break ;
case AV_STEREO3D_TOPBOTTOM :
format = MATROSKA_VIDEO_STEREOMODE_TYPE_TOP_BOTTOM ;
if ( stereo - > flags & AV_STEREO3D_FLAG_INVERT )
format - - ;
2014-10-27 23:18:03 +02:00
* h_height = 2 ;
2014-08-05 13:16:17 +03:00
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 - - ;
2014-10-27 23:18:03 +02:00
* h_height = 2 ;
2014-08-05 13:16:17 +03:00
break ;
case AV_STEREO3D_COLUMNS :
format = MATROSKA_VIDEO_STEREOMODE_TYPE_COL_INTERLEAVED_LR ;
if ( stereo - > flags & AV_STEREO3D_FLAG_INVERT )
format - - ;
2014-10-27 23:18:03 +02:00
* h_width = 2 ;
2014-08-05 13:16:17 +03:00
break ;
case AV_STEREO3D_FRAMESEQUENCE :
format = MATROSKA_VIDEO_STEREOMODE_TYPE_BOTH_EYES_BLOCK_LR ;
if ( stereo - > flags & AV_STEREO3D_FLAG_INVERT )
format + + ;
break ;
}
break ;
}
2014-06-07 02:38:00 +03:00
}
2014-08-29 01:30:47 +03:00
if ( format = = MATROSKA_VIDEO_STEREOMODE_TYPE_NB )
return ret ;
2014-10-22 16:36:32 +03:00
// if webm, do not write unsupported modes
2014-08-29 01:30:47 +03:00
if ( ( mode = = MODE_WEBM & &
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 " ) ;
2014-08-05 13:16:17 +03:00
format = MATROSKA_VIDEO_STEREOMODE_TYPE_NB ;
2014-07-01 21:44:05 +03:00
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
2014-08-29 01:30:47 +03:00
put_ebml_uint ( pb , MATROSKA_ID_VIDEOSTEREOMODE , format ) ;
2014-08-05 13:16:17 +03:00
2014-08-29 01:30:47 +03:00
return ret ;
2014-06-07 02:38:00 +03:00
}
2014-06-07 02:21:13 +03:00
static int mkv_write_track ( AVFormatContext * s , MatroskaMuxContext * mkv ,
2014-07-01 21:22:51 +03:00
int i , AVIOContext * pb , int default_stream_exists )
2007-09-05 03:22:37 +03:00
{
2014-06-07 02:21:13 +03:00
AVStream * st = s - > streams [ i ] ;
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 ;
2014-06-07 02:21:13 +03:00
ebml_master subinfo , track ;
int native_id = 0 ;
int qt_id = 0 ;
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 bit_depth = av_get_bits_per_sample ( par - > codec_id ) ;
int sample_rate = par - > sample_rate ;
2014-06-07 02:21:13 +03:00
int output_sample_rate = 0 ;
2014-07-01 21:22:51 +03:00
int display_width_div = 1 ;
int display_height_div = 1 ;
2014-06-07 02:21:13 +03:00
int j , ret ;
AVDictionaryEntry * 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
if ( par - > codec_type = = AVMEDIA_TYPE_ATTACHMENT ) {
2014-06-07 02:21:13 +03:00
mkv - > have_attachments = 1 ;
return 0 ;
2013-01-30 12:28:53 +03:00
}
2011-07-30 14:37:36 +03:00
2016-05-22 03:20:24 +02:00
if ( par - > codec_type = = AVMEDIA_TYPE_AUDIO ) {
2016-05-22 03:22:16 +02:00
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 ;
2016-05-22 03:20:24 +02:00
}
2007-09-05 03:23:19 +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_AAC ) {
2017-04-13 17:43:11 +02:00
ret = get_aac_sample_rates ( s , par - > extradata , par - > extradata_size , & sample_rate ,
& output_sample_rate ) ;
2015-01-01 19:22:46 +02:00
if ( ret < 0 )
return ret ;
}
2011-07-30 14:37:36 +03:00
2014-06-07 02:21:13 +03:00
track = start_ebml_master ( pb , MATROSKA_ID_TRACKENTRY , 0 ) ;
2014-07-01 21:22:51 +03:00
put_ebml_uint ( pb , MATROSKA_ID_TRACKNUMBER ,
mkv - > is_dash ? mkv - > dash_track_number : i + 1 ) ;
put_ebml_uint ( pb , MATROSKA_ID_TRACKUID ,
mkv - > is_dash ? mkv - > dash_track_number : i + 1 ) ;
2014-06-07 02:21:13 +03: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 ) ;
2016-04-10 21:58:15 +02:00
if ( mkv - > mode ! = MODE_WEBM | | par - > codec_id ! = AV_CODEC_ID_WEBVTT ) {
2014-07-20 11:36:51 +03:00
put_ebml_string ( pb , MATROSKA_ID_TRACKLANGUAGE , tag & & tag - > value ? tag - > value : " und " ) ;
2014-07-01 21:22:51 +03:00
} else if ( tag & & tag - > value ) {
put_ebml_string ( pb , MATROSKA_ID_TRACKLANGUAGE , tag - > value ) ;
}
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.
2014-07-01 21:22:51 +03:00
if ( default_stream_exists & & ! ( st - > disposition & AV_DISPOSITION_DEFAULT ) )
2014-06-07 02:21:13 +03:00
put_ebml_uint ( pb , MATROSKA_ID_TRACKFLAGDEFAULT , ! ! ( st - > disposition & AV_DISPOSITION_DEFAULT ) ) ;
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 ) ;
2018-09-21 21:18:15 +02:00
if ( mkv - > mode = = MODE_WEBM ) {
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 ) {
2014-07-01 21:22:51 +03: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 ;
2013-07-09 02:25:30 +03:00
} else {
2014-07-01 21:22:51 +03:00
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 {
// 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
2016-04-10 21:58:15 +02:00
if ( par - > codec_type = = AVMEDIA_TYPE_AUDIO & & par - > initial_padding & & par - > codec_id = = AV_CODEC_ID_OPUS ) {
int64_t codecdelay = av_rescale_q ( par - > initial_padding ,
2016-01-05 22:34:35 +02:00
( AVRational ) { 1 , 48000 } ,
2015-12-09 17:16:46 +02:00
( AVRational ) { 1 , 1000000000 } ) ;
if ( codecdelay < 0 ) {
av_log ( s , AV_LOG_ERROR , " Initial padding is invalid \n " ) ;
return AVERROR ( EINVAL ) ;
}
2016-04-10 21:58:15 +02:00
// mkv->tracks[i].ts_offset = av_rescale_q(par->initial_padding,
// (AVRational){ 1, par->sample_rate },
2014-07-01 21:22:51 +03:00
// st->time_base);
2014-04-29 13:26:38 +03:00
2015-12-09 17:16:46 +02:00
put_ebml_uint ( pb , MATROSKA_ID_CODECDELAY , codecdelay ) ;
2014-06-07 02:21:13 +03:00
}
2016-04-10 21:58:15 +02:00
if ( par - > codec_id = = AV_CODEC_ID_OPUS ) {
2014-07-01 21:22:51 +03:00
put_ebml_uint ( pb , MATROSKA_ID_SEEKPREROLL , OPUS_SEEK_PREROLL ) ;
2014-06-07 02:21:13 +03:00
}
2013-08-16 21:08:06 +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 ) ;
2014-06-07 02:21:13 +03:00
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
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 " ) ;
mkv - > tracks [ i ] . write_dts = 1 ;
2015-05-05 12:57:24 +02:00
s - > internal - > avoid_negative_ts_use_pts = 0 ;
2010-06-05 01:40:54 +03:00
}
2014-06-07 02:21:13 +03:00
subinfo = start_ebml_master ( pb , MATROSKA_ID_TRACKVIDEO , 0 ) ;
2016-04-02 22:35:44 +02: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
put_ebml_uint ( pb , MATROSKA_ID_VIDEOPIXELWIDTH , par - > width ) ;
put_ebml_uint ( pb , MATROSKA_ID_VIDEOPIXELHEIGHT , par - > height ) ;
2007-09-05 03:25:58 +03:00
2016-10-05 00:42:55 +02:00
mkv_write_field_order ( pb , mkv - > mode , par - > field_order ) ;
2016-04-02 22:35:44 +02:00
2014-08-05 13:16:17 +03:00
// check both side data and metadata for stereo information,
// write the result to the bitstream if any is found
2014-10-27 23:18:03 +02:00
ret = mkv_write_stereo_mode ( s , pb , st , mkv - > mode ,
& display_width_div ,
& display_height_div ) ;
2014-08-05 13:16:17 +03:00
if ( ret < 0 )
return ret ;
2014-08-20 19:40:20 +03:00
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 ) ) | |
2016-04-10 21:58:15 +02:00
( par - > format = = AV_PIX_FMT_YUVA420P ) ) {
2014-07-01 21:22:51 +03:00
put_ebml_uint ( pb , MATROSKA_ID_VIDEOALPHAMODE , 1 ) ;
2014-06-07 02:21:13 +03:00
}
2013-07-09 02:25:30 +03:00
2014-10-27 23:18:03 +02:00
// write DisplayWidth and DisplayHeight, they contain the size of
// a single source view and/or the display aspect ratio
2014-06-07 02:21:13 +03:00
if ( st - > sample_aspect_ratio . num ) {
2016-04-10 21:58:15 +02:00
int64_t d_width = av_rescale ( par - > width , st - > sample_aspect_ratio . num , st - > sample_aspect_ratio . den ) ;
2014-07-01 21:22:51 +03:00
if ( d_width > INT_MAX ) {
av_log ( s , AV_LOG_ERROR , " Overflow in display width \n " ) ;
return AVERROR ( EINVAL ) ;
2014-06-07 01:54:50 +03:00
}
2016-10-17 19:17:35 +02:00
if ( d_width ! = par - > width | | display_width_div ! = 1 | | display_height_div ! = 1 ) {
2016-10-22 20:56:28 +02:00
if ( mkv - > mode = = MODE_WEBM | | display_width_div ! = 1 | | display_height_div ! = 1 ) {
put_ebml_uint ( pb , MATROSKA_ID_VIDEODISPLAYWIDTH , d_width / display_width_div ) ;
put_ebml_uint ( pb , MATROSKA_ID_VIDEODISPLAYHEIGHT , par - > height / display_height_div ) ;
} 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 ) ;
put_ebml_uint ( pb , MATROSKA_ID_VIDEODISPLAYWIDTH , display_aspect_ratio . num ) ;
put_ebml_uint ( pb , MATROSKA_ID_VIDEODISPLAYHEIGHT , display_aspect_ratio . den ) ;
put_ebml_uint ( pb , MATROSKA_ID_VIDEODISPLAYUNIT , MATROSKA_VIDEO_DISPLAYUNIT_DAR ) ;
}
2016-10-17 19:17:35 +02:00
}
2014-07-01 21:22:51 +03:00
} else if ( display_width_div ! = 1 | | display_height_div ! = 1 ) {
2016-04-10 21:58:15 +02:00
put_ebml_uint ( pb , MATROSKA_ID_VIDEODISPLAYWIDTH , par - > width / display_width_div ) ;
put_ebml_uint ( pb , MATROSKA_ID_VIDEODISPLAYHEIGHT , par - > height / display_height_div ) ;
2017-02-22 02:14:17 +02:00
} else if ( mkv - > mode ! = MODE_WEBM )
2016-10-15 20:02:26 +02:00
put_ebml_uint ( pb , MATROSKA_ID_VIDEODISPLAYUNIT , MATROSKA_VIDEO_DISPLAYUNIT_UNKNOWN ) ;
2013-07-09 02:25:30 +03:00
2016-04-10 21:58:15 +02:00
if ( par - > codec_id = = AV_CODEC_ID_RAWVIDEO ) {
uint32_t color_space = av_le2ne32 ( par - > codec_tag ) ;
2014-07-01 21:22:51 +03:00
put_ebml_binary ( pb , MATROSKA_ID_VIDEOCOLORSPACE , & color_space , sizeof ( color_space ) ) ;
2013-07-09 02:25:30 +03:00
}
2017-01-15 15:56:53 +02:00
ret = mkv_write_video_color ( pb , par , st ) ;
2017-03-08 21:12:32 +02:00
if ( ret < 0 )
return ret ;
ret = mkv_write_video_projection ( s , pb , st ) ;
2017-01-15 15:56:53 +02:00
if ( ret < 0 )
return ret ;
2014-06-07 02:21:13 +03:00
end_ebml_master ( pb , subinfo ) ;
break ;
case AVMEDIA_TYPE_AUDIO :
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 " ) ;
subinfo = start_ebml_master ( pb , MATROSKA_ID_TRACKAUDIO , 0 ) ;
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
put_ebml_uint ( pb , MATROSKA_ID_AUDIOCHANNELS , par - > channels ) ;
2017-04-13 17:43:11 +02:00
mkv - > tracks [ i ] . 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 ) ;
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 ) ;
}
2014-07-01 21:22:51 +03:00
2016-04-10 21:58:15 +02:00
if ( mkv - > mode ! = MODE_WEBM | | 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 ) ;
}
2018-09-21 23:22:05 +02:00
if ( mkv - > mode ! = MODE_WEBM | | par - > codec_id ! = AV_CODEC_ID_WEBVTT ) {
2016-11-01 01:04:46 +02:00
mkv - > tracks [ i ] . codecpriv_offset = avio_tell ( pb ) ;
2016-04-10 21:58:15 +02:00
ret = mkv_write_codecprivate ( s , pb , par , native_id , qt_id ) ;
2014-07-01 21:22:51 +03:00
if ( ret < 0 )
return ret ;
2014-06-07 02:21:13 +03:00
}
2010-06-05 01:40:54 +03:00
2014-06-07 02:21:13 +03:00
end_ebml_master ( pb , track ) ;
2007-09-05 03:25:58 +03:00
2014-06-07 02:21:13 +03:00
return 0 ;
}
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 ;
2014-07-01 21:22:51 +03:00
int i , ret , default_stream_exists = 0 ;
2007-09-05 03:25:49 +03:00
2014-06-07 02:21:13 +03:00
ret = mkv_add_seekhead_entry ( mkv - > main_seekhead , MATROSKA_ID_TRACKS , avio_tell ( pb ) ) ;
if ( ret < 0 )
return ret ;
2016-10-30 21:39:03 +02:00
ret = start_ebml_master_crc32 ( pb , & mkv - > tracks_bc , mkv , & mkv - > tracks_master , MATROSKA_ID_TRACKS , 0 ) ;
2016-10-04 00:20:11 +02:00
if ( ret < 0 )
return ret ;
2014-06-07 02:21:13 +03:00
for ( i = 0 ; i < s - > nb_streams ; i + + ) {
2014-07-01 21:22:51 +03:00
AVStream * st = s - > streams [ i ] ;
default_stream_exists | = st - > disposition & AV_DISPOSITION_DEFAULT ;
}
for ( i = 0 ; i < s - > nb_streams ; i + + ) {
2016-10-30 21:39:03 +02:00
ret = mkv_write_track ( s , mkv , i , mkv - > tracks_bc , default_stream_exists ) ;
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
2017-03-21 22:02:30 +02:00
if ( ( pb - > seekable & AVIO_SEEKABLE_NORMAL ) & & ! mkv - > is_live )
2017-01-05 02:24:03 +02:00
end_ebml_master_crc32_preliminary ( pb , & mkv - > tracks_bc , mkv , mkv - > tracks_master ) ;
2016-10-30 21:39:03 +02:00
else
end_ebml_master_crc32 ( pb , & mkv - > tracks_bc , mkv , mkv - > tracks_master ) ;
2007-09-05 03:23:17 +03:00
return 0 ;
2007-09-05 03:23:15 +03:00
}
2009-10-18 15:17:01 +03:00
static int mkv_write_chapters ( AVFormatContext * s )
{
MatroskaMuxContext * mkv = s - > priv_data ;
2016-10-04 00:20:53 +02:00
AVIOContext * dyn_cp , * pb = s - > pb ;
2009-10-18 15:17:01 +03:00
ebml_master chapters , editionentry ;
AVRational scale = { 1 , 1E9 } ;
int i , ret ;
2013-09-03 20:53:34 +03:00
if ( ! s - > nb_chapters | | mkv - > wrote_chapters )
2009-10-18 15:17:01 +03:00
return 0 ;
2011-03-03 21:11:45 +02:00
ret = mkv_add_seekhead_entry ( mkv - > main_seekhead , MATROSKA_ID_CHAPTERS , avio_tell ( pb ) ) ;
2009-10-18 15:17:01 +03:00
if ( ret < 0 ) return ret ;
2016-10-25 17:12:03 +02:00
ret = start_ebml_master_crc32 ( pb , & dyn_cp , mkv , & chapters , MATROSKA_ID_CHAPTERS , 0 ) ;
2016-10-04 00:20:53 +02:00
if ( ret < 0 ) return ret ;
editionentry = start_ebml_master ( dyn_cp , MATROSKA_ID_EDITIONENTRY , 0 ) ;
2017-06-02 04:42:05 +02:00
if ( mkv - > mode ! = MODE_WEBM ) {
put_ebml_uint ( dyn_cp , MATROSKA_ID_EDITIONFLAGDEFAULT , 1 ) ;
put_ebml_uint ( dyn_cp , MATROSKA_ID_EDITIONFLAGHIDDEN , 0 ) ;
}
2009-10-18 15:17:01 +03:00
for ( i = 0 ; i < s - > nb_chapters ; i + + ) {
ebml_master chapteratom , chapterdisplay ;
AVChapter * c = s - > chapters [ i ] ;
2015-02-17 21:40:00 +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 ) ;
2011-05-22 13:46:29 +03:00
AVDictionaryEntry * t = NULL ;
2015-03-09 02:05:30 +02:00
if ( chapterstart < 0 | | chapterstart > chapterend | | chapterend < 0 ) {
2015-03-11 16:56:52 +02:00
av_log ( s , AV_LOG_ERROR ,
" Invalid chapter start (% " PRId64 " ) or end (% " PRId64 " ). \n " ,
2015-03-09 02:05:30 +02:00
chapterstart , chapterend ) ;
2015-02-03 17:40:34 +02:00
return AVERROR_INVALIDDATA ;
2015-03-09 02:05:30 +02:00
}
2009-10-18 15:17:01 +03:00
2016-10-04 00:20:53 +02:00
chapteratom = start_ebml_master ( dyn_cp , MATROSKA_ID_CHAPTERATOM , 0 ) ;
put_ebml_uint ( dyn_cp , MATROSKA_ID_CHAPTERUID , c - > id + mkv - > chapter_id_offset ) ;
put_ebml_uint ( dyn_cp , MATROSKA_ID_CHAPTERTIMESTART , chapterstart ) ;
put_ebml_uint ( dyn_cp , MATROSKA_ID_CHAPTERTIMEEND , chapterend ) ;
2017-06-02 04:42:05 +02:00
if ( mkv - > mode ! = MODE_WEBM ) {
put_ebml_uint ( dyn_cp , MATROSKA_ID_CHAPTERFLAGHIDDEN , 0 ) ;
put_ebml_uint ( dyn_cp , MATROSKA_ID_CHAPTERFLAGENABLED , 1 ) ;
}
2011-05-22 13:46:29 +03:00
if ( ( t = av_dict_get ( c - > metadata , " title " , NULL , 0 ) ) ) {
2016-10-04 00:20:53 +02:00
chapterdisplay = start_ebml_master ( dyn_cp , MATROSKA_ID_CHAPTERDISPLAY , 0 ) ;
put_ebml_string ( dyn_cp , MATROSKA_ID_CHAPSTRING , t - > value ) ;
put_ebml_string ( dyn_cp , MATROSKA_ID_CHAPLANG , " und " ) ;
end_ebml_master ( dyn_cp , chapterdisplay ) ;
2009-10-18 15:17:01 +03:00
}
2016-10-04 00:20:53 +02:00
end_ebml_master ( dyn_cp , chapteratom ) ;
2009-10-18 15:17:01 +03:00
}
2016-10-04 00:20:53 +02:00
end_ebml_master ( dyn_cp , editionentry ) ;
end_ebml_master_crc32 ( pb , & dyn_cp , mkv , chapters ) ;
2013-09-03 20:53:34 +03:00
mkv - > wrote_chapters = 1 ;
2009-10-18 15:17:01 +03:00
return 0 ;
}
2014-12-17 15:53:45 +02:00
static int mkv_write_simpletag ( AVIOContext * pb , AVDictionaryEntry * t )
2010-10-05 12:40:43 +03:00
{
uint8_t * key = av_strdup ( t - > key ) ;
uint8_t * p = key ;
const uint8_t * lang = NULL ;
ebml_master tag ;
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 + + ;
}
tag = start_ebml_master ( pb , MATROSKA_ID_SIMPLETAG , 0 ) ;
put_ebml_string ( pb , MATROSKA_ID_TAGNAME , key ) ;
if ( lang )
put_ebml_string ( pb , MATROSKA_ID_TAGLANG , lang ) ;
put_ebml_string ( pb , MATROSKA_ID_TAGSTRING , t - > value ) ;
end_ebml_master ( pb , tag ) ;
av_freep ( & key ) ;
2014-12-17 15:53:45 +02:00
return 0 ;
2010-10-05 12:40:43 +03:00
}
2015-08-05 07:09:57 +02:00
static int mkv_write_tag_targets ( AVFormatContext * s ,
unsigned int elementid , unsigned int uid ,
ebml_master * tags , ebml_master * tag )
2010-10-05 12:40:43 +03:00
{
2016-10-04 00:27:30 +02:00
AVIOContext * pb ;
2010-10-05 12:40:43 +03:00
MatroskaMuxContext * mkv = s - > priv_data ;
2015-08-05 07:09:57 +02:00
ebml_master targets ;
2010-10-05 12:40:43 +03:00
int ret ;
if ( ! tags - > pos ) {
2011-03-03 21:11:45 +02:00
ret = mkv_add_seekhead_entry ( mkv - > main_seekhead , MATROSKA_ID_TAGS , avio_tell ( s - > pb ) ) ;
2010-10-05 12:40:43 +03:00
if ( ret < 0 ) return ret ;
2016-10-25 17:12:03 +02:00
start_ebml_master_crc32 ( s - > pb , & mkv - > tags_bc , mkv , tags , MATROSKA_ID_TAGS , 0 ) ;
2010-10-05 12:40:43 +03:00
}
2016-10-04 00:27:30 +02:00
pb = mkv - > tags_bc ;
2010-10-05 12:40:43 +03:00
2016-10-04 00:27:30 +02:00
* tag = start_ebml_master ( pb , MATROSKA_ID_TAG , 0 ) ;
targets = start_ebml_master ( pb , MATROSKA_ID_TAGTARGETS , 0 ) ;
2010-10-05 12:40:43 +03:00
if ( elementid )
2016-10-04 00:27:30 +02:00
put_ebml_uint ( pb , elementid , uid ) ;
end_ebml_master ( pb , targets ) ;
2015-08-05 07:09:57 +02:00
return 0 ;
}
2016-09-06 04:42:24 +02:00
static int mkv_check_tag_name ( const char * name , unsigned int elementid )
{
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
}
2015-08-05 07:09:57 +02:00
static int mkv_write_tag ( AVFormatContext * s , AVDictionary * m , unsigned int elementid ,
unsigned int uid , ebml_master * tags )
{
2016-10-04 00:27:30 +02:00
MatroskaMuxContext * mkv = s - > priv_data ;
2015-08-05 07:09:57 +02:00
ebml_master tag ;
int ret ;
AVDictionaryEntry * t = NULL ;
ret = mkv_write_tag_targets ( s , elementid , uid , tags , & tag ) ;
if ( ret < 0 )
return ret ;
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 ) ) {
2016-10-04 00:27:30 +02:00
ret = mkv_write_simpletag ( mkv - > tags_bc , t ) ;
2014-12-17 15:53:45 +02:00
if ( ret < 0 )
return ret ;
}
}
2010-10-05 12:40:43 +03:00
2016-10-04 00:27:30 +02:00
end_ebml_master ( mkv - > tags_bc , tag ) ;
2010-10-05 12:40:43 +03:00
return 0 ;
}
2016-09-06 05:26:16 +02:00
static int mkv_check_tag ( AVDictionary * m , unsigned int elementid )
2013-07-17 09:10:16 +03:00
{
AVDictionaryEntry * t = NULL ;
while ( ( t = av_dict_get ( m , " " , t , AV_DICT_IGNORE_SUFFIX ) ) )
2016-09-06 05:26:16 +02:00
if ( mkv_check_tag_name ( t - > key , elementid ) )
2013-07-17 14:30:36 +03:00
return 1 ;
2013-07-17 09:10:16 +03:00
2013-07-17 14:30:36 +03:00
return 0 ;
2013-07-17 09:10:16 +03:00
}
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 ;
2010-10-05 12:40:43 +03:00
int i , ret ;
2010-10-15 22:04:25 +03:00
ff_metadata_conv_ctx ( s , ff_mkv_metadata_conv , NULL ) ;
2016-09-06 05:26:16 +02:00
if ( mkv_check_tag ( s - > metadata , 0 ) ) {
2016-10-04 00:27:30 +02:00
ret = mkv_write_tag ( s , s - > metadata , 0 , 0 , & mkv - > tags ) ;
2010-10-05 12:40:43 +03:00
if ( ret < 0 ) return ret ;
}
for ( i = 0 ; i < s - > nb_streams ; i + + ) {
AVStream * st = s - > streams [ i ] ;
2016-10-07 07:46:22 +02:00
if ( st - > codecpar - > codec_type = = AVMEDIA_TYPE_ATTACHMENT )
continue ;
2016-09-06 05:26:16 +02:00
if ( ! mkv_check_tag ( st - > metadata , MATROSKA_ID_TAGTARGETS_TRACKUID ) )
2010-10-05 12:40:43 +03:00
continue ;
2016-10-04 00:27:30 +02:00
ret = mkv_write_tag ( s , st - > metadata , MATROSKA_ID_TAGTARGETS_TRACKUID , i + 1 , & mkv - > tags ) ;
2010-10-05 12:40:43 +03:00
if ( ret < 0 ) return ret ;
}
2017-03-21 22:02:30 +02:00
if ( ( s - > pb - > seekable & AVIO_SEEKABLE_NORMAL ) & & ! mkv - > is_live ) {
2015-08-05 07:09:57 +02:00
for ( i = 0 ; i < s - > nb_streams ; i + + ) {
2016-10-04 00:27:30 +02:00
AVIOContext * pb ;
2016-10-07 07:46:22 +02:00
AVStream * st = s - > streams [ i ] ;
2015-08-05 07:09:57 +02:00
ebml_master tag_target ;
ebml_master tag ;
2016-10-07 07:46:22 +02:00
if ( st - > codecpar - > codec_type = = AVMEDIA_TYPE_ATTACHMENT )
continue ;
2016-10-04 00:27:30 +02:00
mkv_write_tag_targets ( s , MATROSKA_ID_TAGTARGETS_TRACKUID , i + 1 , & mkv - > tags , & tag_target ) ;
pb = mkv - > tags_bc ;
2015-08-05 07:09:57 +02:00
2016-10-04 00:27:30 +02:00
tag = start_ebml_master ( pb , MATROSKA_ID_SIMPLETAG , 0 ) ;
put_ebml_string ( pb , MATROSKA_ID_TAGNAME , " DURATION " ) ;
mkv - > stream_duration_offsets [ i ] = avio_tell ( pb ) ;
2015-08-05 07:09:57 +02:00
// Reserve space to write duration as a 20-byte string.
// 2 (ebml id) + 1 (data size) + 20 (data)
2016-10-04 00:27:30 +02:00
put_ebml_void ( pb , 23 ) ;
end_ebml_master ( pb , tag ) ;
end_ebml_master ( pb , tag_target ) ;
2015-08-05 07:09:57 +02:00
}
}
2017-06-02 04:47:32 +02:00
if ( mkv - > mode ! = MODE_WEBM ) {
for ( i = 0 ; i < s - > nb_chapters ; i + + ) {
AVChapter * ch = s - > chapters [ i ] ;
2010-10-05 12:40:43 +03:00
2017-06-02 04:47:32 +02:00
if ( ! mkv_check_tag ( ch - > metadata , MATROSKA_ID_TAGTARGETS_CHAPTERUID ) )
continue ;
2010-10-05 12:40:43 +03:00
2017-06-02 04:47:32 +02:00
ret = mkv_write_tag ( s , ch - > metadata , MATROSKA_ID_TAGTARGETS_CHAPTERUID , ch - > id + mkv - > chapter_id_offset , & mkv - > tags ) ;
if ( ret < 0 )
return ret ;
}
2010-10-05 12:40:43 +03:00
}
2017-06-02 04:47:32 +02:00
if ( mkv - > have_attachments & & mkv - > mode ! = MODE_WEBM ) {
2016-10-07 07:46:22 +02:00
for ( i = 0 ; i < mkv - > attachments - > num_entries ; i + + ) {
mkv_attachment * attachment = & mkv - > attachments - > entries [ i ] ;
AVStream * st = s - > streams [ attachment - > stream_idx ] ;
if ( ! mkv_check_tag ( st - > metadata , MATROSKA_ID_TAGTARGETS_ATTACHUID ) )
continue ;
ret = mkv_write_tag ( s , st - > metadata , MATROSKA_ID_TAGTARGETS_ATTACHUID , attachment - > fileuid , & mkv - > tags ) ;
if ( ret < 0 )
return ret ;
}
}
2016-10-04 00:27:30 +02:00
if ( mkv - > tags . pos ) {
2017-03-21 22:02:30 +02:00
if ( ( s - > pb - > seekable & AVIO_SEEKABLE_NORMAL ) & & ! mkv - > is_live )
2017-01-05 02:24:03 +02:00
end_ebml_master_crc32_preliminary ( s - > pb , & mkv - > tags_bc , mkv , mkv - > tags ) ;
2016-10-04 00:27:30 +02:00
else
end_ebml_master_crc32 ( s - > pb , & mkv - > tags_bc , mkv , mkv - > tags ) ;
}
2010-10-05 12:40:43 +03:00
return 0 ;
}
2011-07-30 14:37:36 +03:00
static int mkv_write_attachments ( AVFormatContext * s )
{
MatroskaMuxContext * mkv = s - > priv_data ;
2016-10-04 00:23:55 +02:00
AVIOContext * dyn_cp , * pb = s - > pb ;
2011-07-30 14:37:36 +03:00
ebml_master attachments ;
AVLFG c ;
int i , ret ;
if ( ! mkv - > have_attachments )
return 0 ;
2016-10-07 07:46:22 +02:00
mkv - > attachments = av_mallocz ( sizeof ( * mkv - > attachments ) ) ;
if ( ! mkv - > attachments )
2016-10-27 08:09:23 +02:00
return AVERROR ( ENOMEM ) ;
2016-10-07 07:46:22 +02:00
2011-07-30 14:37:36 +03:00
av_lfg_init ( & c , av_get_random_seed ( ) ) ;
ret = mkv_add_seekhead_entry ( mkv - > main_seekhead , MATROSKA_ID_ATTACHMENTS , avio_tell ( pb ) ) ;
if ( ret < 0 ) return ret ;
2016-10-25 17:12:03 +02:00
ret = start_ebml_master_crc32 ( pb , & dyn_cp , mkv , & attachments , MATROSKA_ID_ATTACHMENTS , 0 ) ;
2016-10-04 00:23:55 +02:00
if ( ret < 0 ) return ret ;
2011-07-30 14:37:36 +03:00
for ( i = 0 ; i < s - > nb_streams ; i + + ) {
AVStream * st = s - > streams [ i ] ;
ebml_master attached_file ;
2016-10-07 07:46:22 +02:00
mkv_attachment * attachment = mkv - > attachments - > entries ;
2011-07-30 14:37:36 +03:00
AVDictionaryEntry * t ;
const char * mimetype = NULL ;
2016-10-07 07:46:22 +02:00
uint32_t fileuid ;
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 ;
2016-10-07 07:46:22 +02:00
attachment = av_realloc_array ( attachment , mkv - > attachments - > num_entries + 1 , sizeof ( mkv_attachment ) ) ;
if ( ! attachment )
return AVERROR ( ENOMEM ) ;
mkv - > attachments - > entries = attachment ;
2016-10-04 00:23:55 +02:00
attached_file = start_ebml_master ( dyn_cp , MATROSKA_ID_ATTACHEDFILE , 0 ) ;
2011-07-30 14:37:36 +03:00
if ( t = av_dict_get ( st - > metadata , " title " , NULL , 0 ) )
2016-10-04 00:23:55 +02:00
put_ebml_string ( dyn_cp , 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 ) ;
return AVERROR ( EINVAL ) ;
}
2016-10-04 00:23:55 +02:00
put_ebml_string ( dyn_cp , MATROSKA_ID_FILENAME , t - > value ) ;
2011-07-30 14:37:36 +03:00
if ( t = av_dict_get ( st - > metadata , " mimetype " , NULL , 0 ) )
mimetype = t - > value ;
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 ( st - > codecpar - > codec_id ! = AV_CODEC_ID_NONE ) {
2011-07-30 14:37:36 +03:00
int i ;
2012-08-05 12:11:04 +03:00
for ( i = 0 ; ff_mkv_mime_tags [ i ] . id ! = AV_CODEC_ID_NONE ; i + + )
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 ( ff_mkv_mime_tags [ i ] . id = = st - > codecpar - > codec_id ) {
2011-07-30 14:37:36 +03:00
mimetype = ff_mkv_mime_tags [ i ] . str ;
break ;
}
2015-04-03 16:11:53 +02:00
for ( i = 0 ; ff_mkv_image_mime_tags [ i ] . id ! = AV_CODEC_ID_NONE ; i + + )
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 ( ff_mkv_image_mime_tags [ i ] . id = = st - > codecpar - > codec_id ) {
2015-04-03 16:11:53 +02:00
mimetype = ff_mkv_image_mime_tags [ i ] . str ;
break ;
}
2011-07-30 14:37:36 +03:00
}
if ( ! mimetype ) {
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 ) ;
}
2014-05-18 05:02:44 +03:00
if ( s - > flags & AVFMT_FLAG_BITEXACT ) {
2013-01-01 16:14:43 +03:00
struct AVSHA * sha = av_sha_alloc ( ) ;
uint8_t digest [ 20 ] ;
if ( ! sha )
return AVERROR ( ENOMEM ) ;
av_sha_init ( sha , 160 ) ;
2016-04-10 21:58:15 +02:00
av_sha_update ( sha , st - > codecpar - > extradata , st - > codecpar - > extradata_size ) ;
2013-01-01 16:14:43 +03:00
av_sha_final ( sha , digest ) ;
av_free ( sha ) ;
2016-10-07 07:46:22 +02:00
fileuid = AV_RL32 ( digest ) ;
2013-01-01 16:14:43 +03:00
} else {
fileuid = av_lfg_get ( & c ) ;
}
2016-10-07 07:46:22 +02:00
av_log ( s , AV_LOG_VERBOSE , " Using %.8 " PRIx32 " for attachment %d \n " ,
fileuid , mkv - > attachments - > num_entries ) ;
2013-01-01 16:14:43 +03:00
2016-10-04 00:23:55 +02:00
put_ebml_string ( dyn_cp , MATROSKA_ID_FILEMIMETYPE , mimetype ) ;
put_ebml_binary ( dyn_cp , MATROSKA_ID_FILEDATA , st - > codecpar - > extradata , st - > codecpar - > extradata_size ) ;
put_ebml_uint ( dyn_cp , MATROSKA_ID_FILEUID , fileuid ) ;
end_ebml_master ( dyn_cp , attached_file ) ;
2016-10-07 07:46:22 +02:00
mkv - > attachments - > entries [ mkv - > attachments - > num_entries ] . stream_idx = i ;
mkv - > attachments - > entries [ mkv - > attachments - > num_entries + + ] . fileuid = fileuid ;
2011-07-30 14:37:36 +03:00
}
2016-10-04 00:23:55 +02:00
end_ebml_master_crc32 ( pb , & dyn_cp , mkv , attachments ) ;
2011-07-30 14:37:36 +03:00
return 0 ;
}
2016-07-17 04:19:41 +02:00
static int64_t get_metadata_duration ( AVFormatContext * s )
{
int i = 0 ;
int64_t max = 0 ;
int64_t us ;
AVDictionaryEntry * explicitDuration = av_dict_get ( s - > metadata , " DURATION " , NULL , 0 ) ;
if ( explicitDuration & & ( av_parse_time ( & us , explicitDuration - > value , 1 ) = = 0 ) & & us > 0 ) {
av_log ( s , AV_LOG_DEBUG , " get_metadata_duration found duration in context metadata: % " PRId64 " \n " , us ) ;
return us ;
}
for ( i = 0 ; i < s - > nb_streams ; i + + ) {
int64_t us ;
AVDictionaryEntry * duration = av_dict_get ( s - > streams [ i ] - > metadata , " DURATION " , NULL , 0 ) ;
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 ;
}
2007-09-05 03:23:15 +03:00
static int mkv_write_header ( AVFormatContext * s )
{
MatroskaMuxContext * mkv = s - > priv_data ;
2011-02-20 12:04:12 +02:00
AVIOContext * pb = s - > pb ;
2016-10-04 00:29:28 +02:00
ebml_master ebml_header ;
2011-05-22 13:46:29 +03:00
AVDictionaryEntry * tag ;
2014-05-12 19:12:28 +03:00
int ret , i , version = 2 ;
2016-02-06 19:01:23 +02:00
int64_t creation_time ;
2007-09-05 03:23:15 +03:00
2014-06-07 01:54:50 +03:00
if ( ! strcmp ( s - > oformat - > name , " webm " ) )
mkv - > mode = MODE_WEBM ;
else
mkv - > mode = MODE_MATROSKAv2 ;
2010-06-05 01:40:54 +03:00
2014-05-12 19:12:28 +03:00
if ( mkv - > mode ! = MODE_WEBM | |
av_dict_get ( s - > metadata , " stereo_mode " , NULL , 0 ) | |
av_dict_get ( s - > metadata , " alpha_mode " , NULL , 0 ) )
version = 4 ;
for ( i = 0 ; i < s - > nb_streams ; i + + ) {
2016-04-10 21:58:15 +02:00
if ( s - > streams [ i ] - > codecpar - > codec_id = = AV_CODEC_ID_OPUS | |
2014-05-12 19:12:28 +03:00
av_dict_get ( s - > streams [ i ] - > metadata , " stereo_mode " , NULL , 0 ) | |
av_dict_get ( s - > streams [ i ] - > metadata , " alpha_mode " , NULL , 0 ) )
version = 4 ;
}
2013-02-10 12:51:12 +03:00
2014-05-15 02:56:08 +03:00
mkv - > tracks = av_mallocz_array ( s - > nb_streams , sizeof ( * mkv - > tracks ) ) ;
2015-08-19 10:06:56 +02:00
if ( ! mkv - > tracks ) {
ret = AVERROR ( ENOMEM ) ;
goto fail ;
}
2007-09-05 03:24:56 +03:00
ebml_header = start_ebml_master ( pb , EBML_ID_HEADER , 0 ) ;
2007-09-05 03:23:15 +03:00
put_ebml_uint ( pb , EBML_ID_EBMLVERSION , 1 ) ;
put_ebml_uint ( pb , EBML_ID_EBMLREADVERSION , 1 ) ;
put_ebml_uint ( pb , EBML_ID_EBMLMAXIDLENGTH , 4 ) ;
put_ebml_uint ( pb , EBML_ID_EBMLMAXSIZELENGTH , 8 ) ;
2010-06-05 01:40:54 +03:00
put_ebml_string ( pb , EBML_ID_DOCTYPE , s - > oformat - > name ) ;
2014-05-12 19:12:28 +03:00
put_ebml_uint ( pb , EBML_ID_DOCTYPEVERSION , version ) ;
2007-09-05 03:23:15 +03:00
put_ebml_uint ( pb , EBML_ID_DOCTYPEREADVERSION , 2 ) ;
end_ebml_master ( pb , ebml_header ) ;
2007-09-05 03:24:56 +03:00
mkv - > segment = start_ebml_master ( pb , MATROSKA_ID_SEGMENT , 0 ) ;
2011-03-03 21:11:45 +02:00
mkv - > segment_offset = avio_tell ( pb ) ;
2007-09-05 03:23:19 +03:00
2007-09-05 03:25:16 +03:00
// we write 2 seek heads - one at the end of the file to point to each
// cluster, and one at the beginning to point to all other level one
// elements (including the seek head at the end of the file), which
// isn't more than 10 elements if we only write one of each other
// currently defined level 1 element
2007-09-05 03:23:19 +03:00
mkv - > main_seekhead = mkv_start_seekhead ( pb , mkv - > segment_offset , 10 ) ;
2015-08-19 10:06:56 +02:00
if ( ! mkv - > main_seekhead ) {
ret = AVERROR ( ENOMEM ) ;
goto fail ;
}
2007-09-05 03:23:19 +03:00
2011-03-03 21:11:45 +02:00
ret = mkv_add_seekhead_entry ( mkv - > main_seekhead , MATROSKA_ID_INFO , avio_tell ( pb ) ) ;
2015-08-19 10:06:56 +02:00
if ( ret < 0 ) goto fail ;
2007-09-05 03:23:15 +03:00
2016-10-25 17:12:03 +02:00
ret = start_ebml_master_crc32 ( pb , & mkv - > info_bc , mkv , & mkv - > info , MATROSKA_ID_INFO , 0 ) ;
2016-10-04 00:29:28 +02:00
if ( ret < 0 )
return ret ;
pb = mkv - > info_bc ;
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
if ( mkv - > mode ! = MODE_WEBM ) {
uint32_t segment_uid [ 4 ] ;
AVLFG lfg ;
av_lfg_init ( & lfg , av_get_random_seed ( ) ) ;
for ( i = 0 ; i < 4 ; i + + )
segment_uid [ i ] = av_lfg_get ( & lfg ) ;
put_ebml_binary ( pb , MATROSKA_ID_SEGMENTUID , 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 ) ;
} else {
put_ebml_void ( pb , 11 ) ; // assumes double-precision float to be written
}
2015-03-30 23:46:10 +02:00
}
2017-03-21 22:02:30 +02:00
if ( ( s - > pb - > seekable & AVIO_SEEKABLE_NORMAL ) & & ! mkv - > is_live )
2017-01-05 02:24:03 +02:00
end_ebml_master_crc32_preliminary ( s - > pb , & mkv - > info_bc , mkv , mkv - > info ) ;
2016-10-04 00:29:28 +02:00
else
end_ebml_master_crc32 ( s - > pb , & mkv - > info_bc , mkv , mkv - > info ) ;
pb = s - > pb ;
2007-09-05 03:23:15 +03:00
2015-08-05 07:09:57 +02:00
// initialize stream_duration fields
mkv - > stream_durations = av_mallocz ( s - > nb_streams * sizeof ( int64_t ) ) ;
mkv - > stream_duration_offsets = av_mallocz ( s - > nb_streams * sizeof ( int64_t ) ) ;
2017-11-28 06:23:23 +02:00
if ( ! mkv - > stream_durations | | ! mkv - > stream_duration_offsets ) {
ret = AVERROR ( ENOMEM ) ;
goto fail ;
}
2015-08-05 07:09:57 +02: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 )
2015-08-19 10:06:56 +02:00
goto fail ;
2007-09-05 03:22:37 +03:00
2013-07-19 23:52:16 +03:00
for ( i = 0 ; i < s - > nb_chapters ; i + + )
mkv - > chapter_id_offset = FFMAX ( mkv - > chapter_id_offset , 1LL - s - > chapters [ i ] - > id ) ;
2017-06-02 04:42:05 +02:00
ret = mkv_write_chapters ( s ) ;
if ( ret < 0 )
goto fail ;
2010-10-05 12:40:43 +03:00
2017-06-02 04:42:05 +02:00
if ( mkv - > mode ! = MODE_WEBM ) {
2016-10-07 07:46:22 +02:00
ret = mkv_write_attachments ( s ) ;
2014-06-07 01:54:50 +03:00
if ( ret < 0 )
2015-08-19 10:06:56 +02:00
goto fail ;
2010-06-05 01:40:54 +03:00
}
2009-10-18 15:17:01 +03:00
2017-06-02 04:47:32 +02:00
ret = mkv_write_tags ( s ) ;
if ( ret < 0 )
goto fail ;
2017-03-21 22:02:30 +02:00
if ( ! ( s - > pb - > seekable & AVIO_SEEKABLE_NORMAL ) & & ! mkv - > is_live )
2015-08-19 10:06:56 +02:00
mkv_write_seekhead ( pb , mkv ) ;
2010-03-04 10:53:04 +02:00
2007-09-05 03:23:27 +03:00
mkv - > cues = mkv_start_cues ( mkv - > segment_offset ) ;
2015-08-19 10:06:56 +02:00
if ( ! mkv - > cues ) {
ret = AVERROR ( ENOMEM ) ;
goto fail ;
}
2016-09-27 16:26:37 +02:00
if ( ( pb - > seekable & AVIO_SEEKABLE_NORMAL ) & & mkv - > reserve_cues_space ) {
2013-04-30 09:36:20 +03:00
mkv - > cues_pos = avio_tell ( pb ) ;
2018-02-11 21:41:32 +02:00
if ( mkv - > reserve_cues_space = = 1 )
mkv - > reserve_cues_space + + ;
2013-04-30 09:36:20 +03:00
put_ebml_void ( pb , mkv - > reserve_cues_space ) ;
}
2010-06-05 01:40:50 +03:00
av_init_packet ( & mkv - > cur_audio_pkt ) ;
mkv - > cur_audio_pkt . size = 0 ;
2012-03-14 23:40:29 +03:00
mkv - > cluster_pos = - 1 ;
2010-06-05 01:40:50 +03:00
2011-03-14 21:39:06 +02:00
avio_flush ( pb ) ;
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
2016-09-27 16:26:37 +02:00
if ( pb - > seekable & AVIO_SEEKABLE_NORMAL ) {
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 ;
2015-08-19 10:06:56 +02:00
fail :
mkv_free ( mkv ) ;
return ret ;
2007-09-05 03:22:37 +03:00
}
2008-10-05 01:44:50 +03:00
static int mkv_blockgroup_size ( int pkt_size )
2007-09-05 03:24:56 +03:00
{
2008-10-05 01:44:50 +03:00
int size = pkt_size + 4 ;
2007-09-05 03:26:07 +03:00
size + = ebml_num_size ( size ) ;
2007-09-05 03:24:56 +03:00
size + = 2 ; // EBML ID for block and block duration
size + = 8 ; // max size of block duration
2007-09-05 03:26:07 +03:00
size + = ebml_num_size ( size ) ;
2007-09-05 03:24:56 +03:00
size + = 1 ; // blockgroup EBML ID
return size ;
}
2013-05-28 11:09:21 +03:00
static int mkv_strip_wavpack ( const uint8_t * src , uint8_t * * pdst , int * size )
{
uint8_t * dst ;
int srclen = * size ;
int offset = 0 ;
int ret ;
dst = av_malloc ( srclen ) ;
if ( ! dst )
return AVERROR ( ENOMEM ) ;
while ( srclen > = WV_HEADER_SIZE ) {
WvHeader header ;
ret = ff_wv_parse_header ( & header , src ) ;
if ( ret < 0 )
goto fail ;
src + = WV_HEADER_SIZE ;
srclen - = WV_HEADER_SIZE ;
if ( srclen < header . blocksize ) {
ret = AVERROR_INVALIDDATA ;
goto fail ;
}
if ( header . initial ) {
AV_WL32 ( dst + offset , header . samples ) ;
offset + = 4 ;
}
AV_WL32 ( dst + offset , header . flags ) ;
AV_WL32 ( dst + offset + 4 , header . crc ) ;
offset + = 8 ;
if ( ! ( header . initial & & header . final ) ) {
AV_WL32 ( dst + offset , header . blocksize ) ;
offset + = 4 ;
}
memcpy ( dst + offset , src , header . blocksize ) ;
src + = header . blocksize ;
srclen - = header . blocksize ;
offset + = header . blocksize ;
}
* pdst = dst ;
* size = offset ;
return 0 ;
fail :
av_freep ( & dst ) ;
return ret ;
}
2011-02-20 12:04:12 +02:00
static void mkv_write_block ( AVFormatContext * s , AVIOContext * pb ,
2015-06-05 01:29:49 +02:00
unsigned int blockid , AVPacket * pkt , int keyframe )
2007-09-05 03:23:47 +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
AVCodecParameters * par = s - > streams [ pkt - > stream_index ] - > codecpar ;
2013-05-09 02:59:32 +03:00
uint8_t * data = NULL , * side_data = NULL ;
int offset = 0 , size = pkt - > size , side_data_size = 0 ;
2010-03-09 06:23:28 +02:00
int64_t ts = mkv - > tracks [ pkt - > stream_index ] . write_dts ? pkt - > dts : pkt - > pts ;
2013-11-14 14:58:28 +03:00
uint64_t additional_id = 0 ;
int64_t discard_padding = 0 ;
2015-06-05 01:29:49 +02:00
uint8_t track_number = ( mkv - > is_dash ? mkv - > dash_track_number : ( pkt - > stream_index + 1 ) ) ;
2013-05-17 14:58:07 +03:00
ebml_master block_group , block_additions , block_more ;
2007-09-05 03:23:47 +03:00
2017-02-16 00:22:40 +02:00
ts + = mkv - > tracks [ pkt - > stream_index ] . ts_offset ;
2007-09-05 03:23:47 +03:00
2007-09-05 03:25:38 +03:00
av_log ( s , AV_LOG_DEBUG , " Writing block at offset % " PRIu64 " , size %d, "
2015-09-29 15:14:59 +02:00
" pts % " PRId64 " , dts % " PRId64 " , duration % " PRId64 " , keyframe %d \n " ,
2015-06-05 01:29:49 +02:00
avio_tell ( pb ) , pkt - > size , pkt - > pts , pkt - > dts , pkt - > duration ,
keyframe ! = 0 ) ;
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_H264 & & par - > extradata_size > 0 & &
( AV_RB24 ( par - > extradata ) = = 1 | | AV_RB32 ( par - > extradata ) = = 1 ) )
2009-01-20 02:52:12 +02:00
ff_avc_parse_nal_units_buf ( pkt - > data , & data , & size ) ;
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_id = = AV_CODEC_ID_HEVC & & par - > extradata_size > 6 & &
( AV_RB24 ( par - > extradata ) = = 1 | | AV_RB32 ( par - > extradata ) = = 1 ) )
2014-03-03 17:53:45 +03:00
/* extradata is Annex B, assume the bitstream is too and convert it */
ff_hevc_annexb2mp4_buf ( pkt - > data , & data , & size , 0 , NULL ) ;
2018-07-23 17:33:55 +02:00
else if ( par - > codec_id = = AV_CODEC_ID_AV1 )
ff_av1_filter_obus_buf ( pkt - > data , & data , & size ) ;
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_id = = AV_CODEC_ID_WAVPACK ) {
2013-05-28 11:09:21 +03:00
int ret = mkv_strip_wavpack ( pkt - > data , & data , & size ) ;
if ( ret < 0 ) {
av_log ( s , AV_LOG_ERROR , " Error stripping a WavPack packet. \n " ) ;
return ;
}
} else
2009-01-20 02:52:12 +02:00
data = pkt - > data ;
2012-09-15 01:59:05 +03:00
2016-04-10 21:58:15 +02:00
if ( par - > codec_id = = AV_CODEC_ID_PRORES & & size > = 8 ) {
2012-09-15 01:59:05 +03:00
/* Matroska specification requires to remove the first QuickTime atom
*/
2014-06-07 01:54:50 +03:00
size - = 8 ;
2012-09-15 01:59:05 +03:00
offset = 8 ;
}
2013-08-30 21:26:37 +03:00
side_data = av_packet_get_side_data ( pkt ,
AV_PKT_DATA_SKIP_SAMPLES ,
& side_data_size ) ;
if ( side_data & & side_data_size > = 10 ) {
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 } ) ;
}
2013-05-09 02:59:32 +03:00
side_data = av_packet_get_side_data ( pkt ,
AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL ,
& side_data_size ) ;
if ( side_data ) {
additional_id = AV_RB64 ( side_data ) ;
side_data + = 8 ;
side_data_size - = 8 ;
}
2013-08-30 21:26:37 +03:00
if ( ( side_data_size & & additional_id = = 1 ) | | discard_padding ) {
2013-05-09 02:59:32 +03:00
block_group = start_ebml_master ( pb , MATROSKA_ID_BLOCKGROUP , 0 ) ;
2013-05-17 14:58:07 +03:00
blockid = MATROSKA_ID_BLOCK ;
}
put_ebml_id ( pb , blockid ) ;
2014-06-07 01:54:50 +03:00
put_ebml_num ( pb , size + 4 , 0 ) ;
2014-05-07 19:46:40 +03:00
// this assumes stream_index is less than 126
2015-06-05 01:29:49 +02:00
avio_w8 ( pb , 0x80 | track_number ) ;
2013-05-17 14:58:07 +03:00
avio_wb16 ( pb , ts - mkv - > cluster_pts ) ;
2015-06-05 01:29:49 +02:00
avio_w8 ( pb , ( blockid = = MATROSKA_ID_SIMPLEBLOCK & & keyframe ) ? ( 1 < < 7 ) : 0 ) ;
2013-05-17 14:58:07 +03:00
avio_write ( pb , data + offset , size ) ;
if ( data ! = pkt - > data )
av_free ( data ) ;
2015-06-05 01:29:49 +02:00
if ( blockid = = MATROSKA_ID_BLOCK & & ! keyframe ) {
put_ebml_sint ( pb , MATROSKA_ID_BLOCKREFERENCE ,
mkv - > last_track_timestamp [ track_number - 1 ] ) ;
}
mkv - > last_track_timestamp [ track_number - 1 ] = ts - mkv - > cluster_pts ;
2013-08-30 21:26:37 +03:00
if ( discard_padding ) {
2013-11-14 14:58:28 +03:00
put_ebml_sint ( pb , MATROSKA_ID_DISCARDPADDING , discard_padding ) ;
2013-08-30 21:26:37 +03:00
}
2013-05-17 14:58:07 +03:00
if ( side_data_size & & additional_id = = 1 ) {
2013-05-09 02:59:32 +03:00
block_additions = start_ebml_master ( pb , MATROSKA_ID_BLOCKADDITIONS , 0 ) ;
block_more = start_ebml_master ( pb , MATROSKA_ID_BLOCKMORE , 0 ) ;
put_ebml_uint ( pb , MATROSKA_ID_BLOCKADDID , 1 ) ;
put_ebml_id ( pb , MATROSKA_ID_BLOCKADDITIONAL ) ;
put_ebml_num ( pb , side_data_size , 0 ) ;
avio_write ( pb , side_data , side_data_size ) ;
end_ebml_master ( pb , block_more ) ;
end_ebml_master ( pb , block_additions ) ;
2013-08-30 21:26:37 +03:00
}
if ( ( side_data_size & & additional_id = = 1 ) | | discard_padding ) {
2013-05-09 02:59:32 +03:00
end_ebml_master ( pb , block_group ) ;
}
2007-09-05 03:23:47 +03:00
}
2013-07-09 02:25:30 +03:00
static int mkv_write_vtt_blocks ( AVFormatContext * s , AVIOContext * pb , AVPacket * pkt )
{
MatroskaMuxContext * mkv = s - > priv_data ;
ebml_master blockgroup ;
int id_size , settings_size , size ;
uint8_t * id , * settings ;
int64_t ts = mkv - > tracks [ pkt - > stream_index ] . write_dts ? pkt - > dts : pkt - > pts ;
const int flags = 0 ;
id_size = 0 ;
id = av_packet_get_side_data ( pkt , AV_PKT_DATA_WEBVTT_IDENTIFIER ,
& id_size ) ;
settings_size = 0 ;
settings = av_packet_get_side_data ( pkt , AV_PKT_DATA_WEBVTT_SETTINGS ,
& settings_size ) ;
size = id_size + 1 + settings_size + 1 + pkt - > size ;
av_log ( s , AV_LOG_DEBUG , " Writing block at offset % " PRIu64 " , size %d, "
2015-09-29 15:14:59 +02:00
" pts % " PRId64 " , dts % " PRId64 " , duration % " PRId64 " , flags %d \n " ,
2013-07-09 02:25:30 +03:00
avio_tell ( pb ) , size , pkt - > pts , pkt - > dts , pkt - > duration , flags ) ;
blockgroup = start_ebml_master ( pb , MATROSKA_ID_BLOCKGROUP , mkv_blockgroup_size ( size ) ) ;
put_ebml_id ( pb , MATROSKA_ID_BLOCK ) ;
2014-07-01 20:58:57 +03:00
put_ebml_num ( pb , size + 4 , 0 ) ;
2013-07-09 02:25:30 +03:00
avio_w8 ( pb , 0x80 | ( pkt - > stream_index + 1 ) ) ; // this assumes stream_index is less than 126
avio_wb16 ( pb , ts - mkv - > cluster_pts ) ;
avio_w8 ( pb , flags ) ;
avio_printf ( pb , " %.*s \n %.*s \n %.*s " , id_size , id , settings_size , settings , pkt - > size , pkt - > data ) ;
put_ebml_uint ( pb , MATROSKA_ID_BLOCKDURATION , pkt - > duration ) ;
end_ebml_master ( pb , blockgroup ) ;
return pkt - > duration ;
}
2014-07-24 19:19:11 +03:00
static void mkv_start_new_cluster ( AVFormatContext * s , AVPacket * pkt )
{
MatroskaMuxContext * mkv = s - > priv_data ;
2016-10-03 04:46:19 +02:00
end_ebml_master_crc32 ( s - > pb , & mkv - > dyn_bc , mkv , mkv - > cluster ) ;
2016-10-02 19:37:11 +02:00
mkv - > cluster_pos = - 1 ;
2017-03-21 22:02:30 +02:00
if ( s - > pb - > seekable & AVIO_SEEKABLE_NORMAL )
2016-10-02 19:14:51 +02:00
av_log ( s , AV_LOG_DEBUG ,
" Starting new cluster at offset % " PRIu64 " bytes, "
" pts % " PRIu64 " dts % " PRIu64 " \n " ,
avio_tell ( s - > pb ) , pkt - > pts , pkt - > dts ) ;
else
av_log ( s , AV_LOG_DEBUG , " Starting new cluster, "
" pts % " PRIu64 " dts % " PRIu64 " \n " ,
pkt - > pts , pkt - > dts ) ;
2014-07-24 19:19:11 +03:00
avio_flush ( s - > pb ) ;
}
2016-11-01 01:04:46 +02:00
static int mkv_check_new_extra_data ( AVFormatContext * s , AVPacket * pkt )
{
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 ;
int side_data_size = 0 , ret ;
side_data = av_packet_get_side_data ( pkt , AV_PKT_DATA_NEW_EXTRADATA ,
& side_data_size ) ;
switch ( par - > codec_id ) {
2017-04-13 17:43:11 +02:00
case AV_CODEC_ID_AAC :
if ( side_data_size & & ( s - > pb - > seekable & AVIO_SEEKABLE_NORMAL ) & & ! mkv - > is_live ) {
int filler , output_sample_rate = 0 ;
int64_t curpos ;
ret = get_aac_sample_rates ( s , side_data , side_data_size , & track - > sample_rate ,
& output_sample_rate ) ;
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.
av_freep ( & par - > extradata ) ;
ret = ff_alloc_extradata ( par , side_data_size ) ;
if ( ret < 0 )
return ret ;
memcpy ( par - > extradata , side_data , side_data_size ) ;
curpos = avio_tell ( mkv - > tracks_bc ) ;
avio_seek ( mkv - > tracks_bc , track - > codecpriv_offset , SEEK_SET ) ;
mkv_write_codecprivate ( s , mkv - > tracks_bc , par , 1 , 0 ) ;
filler = MAX_PCE_SIZE + 2 + 4 - ( avio_tell ( mkv - > tracks_bc ) - track - > codecpriv_offset ) ;
if ( filler )
put_ebml_void ( mkv - > tracks_bc , filler ) ;
avio_seek ( mkv - > tracks_bc , track - > sample_rate_offset , SEEK_SET ) ;
put_ebml_float ( mkv - > tracks_bc , MATROSKA_ID_AUDIOSAMPLINGFREQ , track - > sample_rate ) ;
put_ebml_float ( mkv - > tracks_bc , MATROSKA_ID_AUDIOOUTSAMPLINGFREQ , output_sample_rate ) ;
avio_seek ( mkv - > tracks_bc , curpos , SEEK_SET ) ;
} 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 :
2017-04-08 20:44:53 +02:00
if ( side_data_size & & ( s - > pb - > seekable & AVIO_SEEKABLE_NORMAL ) & & ! mkv - > is_live ) {
2016-11-01 01:04:46 +02:00
AVCodecParameters * codecpriv_par ;
int64_t curpos ;
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 ) ;
}
codecpriv_par = avcodec_parameters_alloc ( ) ;
if ( ! codecpriv_par )
return AVERROR ( ENOMEM ) ;
ret = avcodec_parameters_copy ( codecpriv_par , par ) ;
if ( ret < 0 ) {
avcodec_parameters_free ( & codecpriv_par ) ;
return ret ;
}
memcpy ( codecpriv_par - > extradata , side_data , side_data_size ) ;
curpos = avio_tell ( mkv - > tracks_bc ) ;
avio_seek ( mkv - > tracks_bc , track - > codecpriv_offset , SEEK_SET ) ;
mkv_write_codecprivate ( s , mkv - > tracks_bc , codecpriv_par , 1 , 0 ) ;
avio_seek ( mkv - > tracks_bc , curpos , SEEK_SET ) ;
avcodec_parameters_free ( & codecpriv_par ) ;
}
break ;
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 :
if ( side_data_size & & ( s - > pb - > seekable & AVIO_SEEKABLE_NORMAL ) & & ! mkv - > is_live & &
! par - > extradata_size ) {
AVIOContext * dyn_cp ;
uint8_t * codecpriv ;
int codecpriv_size ;
int64_t curpos ;
ret = avio_open_dyn_buf ( & dyn_cp ) ;
if ( ret < 0 )
return ret ;
ff_isom_write_av1c ( dyn_cp , side_data , side_data_size ) ;
codecpriv_size = avio_close_dyn_buf ( dyn_cp , & codecpriv ) ;
if ( ! codecpriv_size ) {
av_free ( codecpriv ) ;
return AVERROR_INVALIDDATA ;
}
curpos = avio_tell ( mkv - > tracks_bc ) ;
avio_seek ( mkv - > tracks_bc , track - > codecpriv_offset , SEEK_SET ) ;
// Do not write the OBUs as we don't have space saved for them
put_ebml_binary ( mkv - > tracks_bc , MATROSKA_ID_CODECPRIVATE , codecpriv , 4 ) ;
av_free ( codecpriv ) ;
avio_seek ( mkv - > tracks_bc , curpos , SEEK_SET ) ;
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 ;
}
2014-05-07 19:46:40 +03:00
static int mkv_write_packet_internal ( AVFormatContext * s , AVPacket * pkt , int add_cue )
2007-09-05 03:22:37 +03:00
{
2007-09-05 03:23:00 +03:00
MatroskaMuxContext * mkv = s - > priv_data ;
2014-06-07 01:54:50 +03:00
AVIOContext * pb = s - > 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 ;
2014-06-07 01:54:50 +03:00
int keyframe = ! ! ( pkt - > flags & AV_PKT_FLAG_KEY ) ;
int duration = pkt - > duration ;
2007-09-05 03:25:32 +03:00
int ret ;
2010-03-09 06:23:28 +02:00
int64_t ts = mkv - > tracks [ pkt - > stream_index ] . write_dts ? pkt - > dts : pkt - > pts ;
2013-07-23 10:52:08 +03:00
int64_t relative_packet_pos ;
2014-05-22 10:22:15 +03:00
int dash_tracknum = mkv - > is_dash ? mkv - > dash_track_number : pkt - > stream_index + 1 ;
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 ) ;
}
2014-04-29 13:26:38 +03:00
ts + = mkv - > tracks [ pkt - > stream_index ] . ts_offset ;
2010-03-09 06:23:33 +02:00
2014-07-24 19:19:50 +03:00
if ( mkv - > cluster_pos ! = - 1 ) {
int64_t cluster_time = ts - mkv - > cluster_pts + mkv - > tracks [ pkt - > stream_index ] . ts_offset ;
if ( ( int16_t ) cluster_time ! = cluster_time ) {
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
mkv_start_new_cluster ( s , pkt ) ;
}
}
2012-03-14 23:40:29 +03:00
if ( mkv - > cluster_pos = = - 1 ) {
2011-03-03 21:11:45 +02:00
mkv - > cluster_pos = avio_tell ( s - > pb ) ;
2016-10-25 17:12:03 +02:00
ret = start_ebml_master_crc32 ( s - > pb , & mkv - > dyn_bc , mkv , & mkv - > cluster , MATROSKA_ID_CLUSTER , 0 ) ;
2016-10-03 04:46:19 +02:00
if ( ret < 0 )
return ret ;
put_ebml_uint ( mkv - > dyn_bc , MATROSKA_ID_CLUSTERTIMECODE , FFMAX ( 0 , ts ) ) ;
2010-03-09 06:23:36 +02:00
mkv - > cluster_pts = FFMAX ( 0 , ts ) ;
2007-09-05 03:23:00 +03:00
}
2016-10-03 04:46:19 +02:00
pb = mkv - > dyn_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
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_type ! = AVMEDIA_TYPE_SUBTITLE ) {
2015-06-05 01:29:49 +02:00
mkv_write_block ( s , pb , MATROSKA_ID_SIMPLEBLOCK , pkt , keyframe ) ;
2017-03-21 22:02:30 +02:00
if ( ( s - > pb - > seekable & AVIO_SEEKABLE_NORMAL ) & & ( par - > codec_type = = AVMEDIA_TYPE_VIDEO & & keyframe | | add_cue ) ) {
2014-05-22 10:22:15 +03:00
ret = mkv_add_cuepoint ( mkv - > cues , pkt - > stream_index , dash_tracknum , ts , mkv - > cluster_pos , relative_packet_pos , - 1 ) ;
if ( ret < 0 ) return ret ;
}
} else {
2016-04-10 21:58:15 +02:00
if ( par - > codec_id = = AV_CODEC_ID_WEBVTT ) {
2015-08-26 01:03:23 +02:00
duration = mkv_write_vtt_blocks ( s , pb , pkt ) ;
} else {
ebml_master blockgroup = start_ebml_master ( pb , MATROSKA_ID_BLOCKGROUP ,
mkv_blockgroup_size ( pkt - > size ) ) ;
2015-09-29 15:14:59 +02:00
2015-09-26 18:13:55 +02:00
# if FF_API_CONVERGENCE_DURATION
FF_DISABLE_DEPRECATION_WARNINGS
2015-08-26 01:03:23 +02:00
/* For backward compatibility, prefer convergence_duration. */
if ( pkt - > convergence_duration > 0 ) {
duration = pkt - > convergence_duration ;
}
2015-09-26 18:13:55 +02:00
FF_ENABLE_DEPRECATION_WARNINGS
# endif
2015-08-26 01:03:23 +02:00
/* All subtitle blocks are considered to be keyframes. */
mkv_write_block ( s , pb , MATROSKA_ID_BLOCK , pkt , 1 ) ;
put_ebml_uint ( pb , MATROSKA_ID_BLOCKDURATION , duration ) ;
end_ebml_master ( pb , blockgroup ) ;
matroskadec, matroskadec, srtenc: Read/Write duration for subtitles.
After much discussion and back-and-forth, we reached the conclusion
that matroska uses convergence_duration for subtitle duration because
a 32bit value isn't large enough to store the duration if sub-micro-second
timebases are used. Matroska may not be the only one that supports these
timebases, but it's certainly the only one that ffmpeg attempts to support
in this way.
The long term solution that we seemed to reach was that if we encounter
a matroska file with a sub-micro-second timebase, we should internally
scale it up to at least micro-second, and then duration can be used
normally. This suggests that on the encode side, we should not allow
generation of files with sub-micro-second timebases, but that's a separate
issue.
That being a non-trivial change, and the subtitle interoperability breakage
being very real, I'm re-submitting this small change for consideration.
In this diff, we make sure that duration is populated by the matroska
demuxer, and that convergence_duration is respected in matroskaenc and
srtenc, but that duration is used otherwise. This ends up being a strict
improvement - pipelines that use convergence duration are unchanged, and
ones that are currently broken due to the duration mismatch will start
working - except for the ones with the extreme timebases, but those were
already broken.
Signed-off-by: Philip Langdale <philipl@overt.org>
2012-05-23 07:50:02 +03:00
}
2007-09-05 03:23:11 +03:00
2017-03-21 22:02:30 +02:00
if ( s - > pb - > seekable & AVIO_SEEKABLE_NORMAL ) {
2015-08-26 01:03:23 +02:00
ret = mkv_add_cuepoint ( mkv - > cues , pkt - > stream_index , dash_tracknum , ts ,
mkv - > cluster_pos , relative_packet_pos , duration ) ;
if ( ret < 0 )
return ret ;
2015-07-09 19:52:06 +02:00
}
2015-07-09 19:49:37 +02:00
}
2007-09-05 03:23:27 +03:00
2010-06-05 01:40:50 +03:00
mkv - > duration = FFMAX ( mkv - > duration , ts + duration ) ;
2015-08-05 07:09:57 +02:00
if ( mkv - > stream_durations )
mkv - > stream_durations [ pkt - > stream_index ] =
FFMAX ( mkv - > stream_durations [ pkt - > stream_index ] , ts + duration ) ;
2010-06-05 01:40:50 +03:00
return 0 ;
}
static int mkv_write_packet ( AVFormatContext * s , AVPacket * pkt )
{
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 ;
2013-08-18 21:09:39 +03:00
if ( mkv - > tracks [ pkt - > stream_index ] . write_dts )
cluster_time = pkt - > dts - mkv - > cluster_pts ;
else
cluster_time = pkt - > pts - mkv - > cluster_pts ;
2014-04-29 13:26:38 +03:00
cluster_time + = mkv - > tracks [ pkt - > stream_index ] . ts_offset ;
2010-06-05 01:40:50 +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
2016-10-02 19:37:11 +02:00
cluster_size = avio_tell ( mkv - > dyn_bc ) ;
2013-08-18 21:09:39 +03:00
2014-05-07 19:46:40 +03:00
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 & &
2014-10-09 18:28:00 +03:00
( mkv - > cluster_pos = = - 1 | |
cluster_time > mkv - > cluster_time_limit ) ) {
2014-05-07 19:46:40 +03:00
// 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 ;
}
if ( mkv - > cluster_pos ! = - 1 & & start_new_cluster ) {
2014-07-24 19:19:11 +03:00
mkv_start_new_cluster ( s , pkt ) ;
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
if ( mkv - > cur_audio_pkt . size > 0 ) {
2014-05-07 19:46:40 +03:00
// for DASH audio, a CuePoint has to be added when there is a new cluster.
ret = mkv_write_packet_internal ( s , & mkv - > cur_audio_pkt ,
mkv - > is_dash ? start_new_cluster : 0 ) ;
2015-10-23 11:11:31 +02:00
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 ) {
2015-10-23 11:11:33 +02:00
ret = av_packet_ref ( & mkv - > cur_audio_pkt , pkt ) ;
2012-10-31 10:53:18 +03:00
} else
2014-05-07 19:46:40 +03:00
ret = mkv_write_packet_internal ( s , pkt , 0 ) ;
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 ) {
2016-10-03 04:46:19 +02:00
end_ebml_master_crc32 ( s - > pb , & mkv - > dyn_bc , mkv , mkv - > cluster ) ;
2016-10-02 19:37:11 +02:00
mkv - > cluster_pos = - 1 ;
2017-03-21 22:02:30 +02:00
if ( s - > pb - > seekable & AVIO_SEEKABLE_NORMAL )
2016-10-02 19:14:51 +02:00
av_log ( s , AV_LOG_DEBUG ,
" Flushing cluster at offset % " PRIu64 " bytes \n " ,
avio_tell ( s - > pb ) ) ;
else
av_log ( s , AV_LOG_DEBUG , " Flushing cluster \n " ) ;
2013-08-19 10:33:14 +03:00
avio_flush ( s - > pb ) ;
}
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 ;
2010-06-05 01:41:11 +03:00
int64_t currentpos , cuespos ;
2007-09-05 03:25:34 +03:00
int ret ;
2007-09-05 03:23:11 +03:00
2010-06-05 01:40:50 +03:00
// check if we have an audio packet cached
if ( mkv - > cur_audio_pkt . size > 0 ) {
2014-05-07 19:46:40 +03:00
ret = mkv_write_packet_internal ( s , & mkv - > cur_audio_pkt , 0 ) ;
2015-10-23 11:11:31 +02:00
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 ;
}
}
2010-03-04 10:53:14 +02:00
if ( mkv - > dyn_bc ) {
2016-10-03 04:46:19 +02:00
end_ebml_master_crc32 ( pb , & mkv - > dyn_bc , mkv , mkv - > cluster ) ;
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 ;
2013-09-03 20:53:34 +03:00
2017-03-21 22:02:30 +02:00
if ( ( pb - > seekable & AVIO_SEEKABLE_NORMAL ) & & ! mkv - > is_live ) {
2011-04-04 23:29:21 +03:00
if ( mkv - > cues - > num_entries ) {
2013-04-30 09:36:20 +03:00
if ( mkv - > reserve_cues_space ) {
int64_t cues_end ;
currentpos = avio_tell ( pb ) ;
avio_seek ( pb , mkv - > cues_pos , SEEK_SET ) ;
2014-07-01 20:58:57 +03:00
cuespos = mkv_write_cues ( s , mkv - > cues , mkv - > tracks , s - > nb_streams ) ;
2013-04-30 09:36:20 +03:00
cues_end = avio_tell ( pb ) ;
if ( cues_end > cuespos + mkv - > reserve_cues_space ) {
2014-06-07 01:54:50 +03:00
av_log ( s , AV_LOG_ERROR ,
" Insufficient space reserved for cues: %d "
" (needed: % " PRId64 " ). \n " ,
mkv - > reserve_cues_space , cues_end - cuespos ) ;
2013-04-30 09:36:20 +03:00
return AVERROR ( EINVAL ) ;
}
if ( cues_end < cuespos + mkv - > reserve_cues_space )
2014-06-07 01:54:50 +03:00
put_ebml_void ( pb , mkv - > reserve_cues_space -
( cues_end - cuespos ) ) ;
2013-04-30 09:36:20 +03:00
avio_seek ( pb , currentpos , SEEK_SET ) ;
} else {
2014-05-22 10:22:15 +03:00
cuespos = mkv_write_cues ( s , mkv - > cues , mkv - > tracks , s - > nb_streams ) ;
2013-04-30 09:36:20 +03:00
}
2011-04-04 23:29:21 +03:00
2014-06-07 01:54:50 +03:00
ret = mkv_add_seekhead_entry ( mkv - > main_seekhead , MATROSKA_ID_CUES ,
cuespos ) ;
if ( ret < 0 )
return ret ;
2011-04-04 23:29:21 +03:00
}
2007-09-05 03:23:27 +03:00
2015-08-19 10:06:56 +02:00
mkv_write_seekhead ( pb , mkv ) ;
2007-09-05 03:26:03 +03:00
// update the duration
av_log ( s , AV_LOG_DEBUG , " end duration = % " PRIu64 " \n " , mkv - > duration ) ;
2011-03-03 21:11:45 +02:00
currentpos = avio_tell ( pb ) ;
2016-10-04 00:29:28 +02:00
avio_seek ( mkv - > info_bc , mkv - > duration_offset , SEEK_SET ) ;
put_ebml_float ( mkv - > info_bc , MATROSKA_ID_DURATION , mkv - > duration ) ;
avio_seek ( pb , mkv - > info . pos , SEEK_SET ) ;
end_ebml_master_crc32 ( pb , & mkv - > info_bc , mkv , mkv - > info ) ;
2007-09-05 03:26:03 +03:00
2016-10-30 21:39:03 +02:00
// write tracks master
avio_seek ( pb , mkv - > tracks_master . pos , SEEK_SET ) ;
end_ebml_master_crc32 ( pb , & mkv - > tracks_bc , mkv , mkv - > tracks_master ) ;
2015-08-05 07:09:57 +02:00
// update stream durations
2016-10-07 07:32:10 +02:00
if ( ! mkv - > is_live & & mkv - > stream_durations ) {
2015-08-06 01:15:08 +02:00
int i ;
2016-10-07 07:32:10 +02:00
int64_t curr = avio_tell ( mkv - > tags_bc ) ;
2015-08-06 01:15:08 +02:00
for ( i = 0 ; i < s - > nb_streams ; + + i ) {
2015-08-05 07:09:57 +02:00
AVStream * st = s - > streams [ i ] ;
2016-10-07 07:32:10 +02:00
if ( mkv - > stream_duration_offsets [ i ] > 0 ) {
double duration_sec = mkv - > stream_durations [ i ] * av_q2d ( st - > time_base ) ;
char duration_string [ 20 ] = " " ;
av_log ( s , AV_LOG_DEBUG , " stream %d end duration = % " PRIu64 " \n " , i ,
mkv - > stream_durations [ i ] ) ;
2015-08-05 07:09:57 +02:00
2016-10-04 00:27:30 +02:00
avio_seek ( mkv - > tags_bc , mkv - > stream_duration_offsets [ i ] , SEEK_SET ) ;
2015-08-05 07:09:57 +02:00
snprintf ( duration_string , 20 , " %02d:%02d:%012.9f " ,
( int ) duration_sec / 3600 , ( ( int ) duration_sec / 60 ) % 60 ,
fmod ( duration_sec , 60 ) ) ;
2016-10-04 00:27:30 +02:00
put_ebml_binary ( mkv - > tags_bc , MATROSKA_ID_TAGSTRING , duration_string , 20 ) ;
2015-08-05 07:09:57 +02:00
}
}
2016-10-07 07:32:10 +02:00
avio_seek ( mkv - > tags_bc , curr , SEEK_SET ) ;
2015-08-05 07:09:57 +02:00
}
2016-10-04 00:27:30 +02:00
if ( mkv - > tags . pos & & ! mkv - > is_live ) {
avio_seek ( pb , mkv - > tags . pos , SEEK_SET ) ;
end_ebml_master_crc32 ( pb , & mkv - > tags_bc , mkv , mkv - > tags ) ;
}
2015-08-05 07:09:57 +02:00
2011-02-28 15:57:54 +02:00
avio_seek ( pb , currentpos , SEEK_SET ) ;
2007-09-05 03:26:00 +03:00
}
2007-09-05 03:23:11 +03:00
2015-03-30 23:46:10 +02:00
if ( ! mkv - > is_live ) {
end_ebml_master ( pb , mkv - > segment ) ;
}
2012-09-09 22:35:23 +03:00
2015-08-19 10:06:56 +02:00
mkv_free ( mkv ) ;
2007-09-05 03:22:37 +03:00
return 0 ;
}
2012-08-05 12:11:04 +03:00
static int mkv_query_codec ( enum AVCodecID codec_id , int std_compliance )
2011-08-11 21:35:46 +03:00
{
int i ;
2012-08-05 12:11:04 +03:00
for ( i = 0 ; ff_mkv_codec_tags [ i ] . id ! = AV_CODEC_ID_NONE ; i + + )
2011-08-11 21:35:46 +03:00
if ( ff_mkv_codec_tags [ i ] . id = = codec_id )
return 1 ;
2014-06-07 01:54:50 +03:00
if ( std_compliance < FF_COMPLIANCE_NORMAL ) {
enum AVMediaType type = avcodec_get_type ( codec_id ) ;
// mkv theoretically supports any video/audio through VFW/ACM
2011-08-11 21:35:46 +03:00
if ( type = = AVMEDIA_TYPE_VIDEO | | type = = AVMEDIA_TYPE_AUDIO )
return 1 ;
}
return 0 ;
}
2018-09-21 17:52:13 +02:00
static int webm_query_codec ( enum AVCodecID codec_id , int std_compliance )
{
int i ;
for ( 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 ;
}
2015-10-08 04:34:06 +02:00
static int mkv_init ( struct AVFormatContext * s )
{
int i ;
2017-11-27 19:11:21 +02:00
if ( s - > nb_streams > MAX_TRACKS ) {
av_log ( s , AV_LOG_ERROR ,
" At most %d streams are supported for muxing in Matroska \n " ,
MAX_TRACKS ) ;
return AVERROR ( EINVAL ) ;
}
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 ;
s - > internal - > avoid_negative_ts_use_pts = 1 ;
}
for ( i = 0 ; i < s - > nb_streams ; i + + ) {
// ms precision is the de-facto standard timescale for mkv files
avpriv_set_pts_info ( s - > streams [ i ] , 64 , 1 , 1000 ) ;
}
return 0 ;
}
static int mkv_check_bitstream ( struct AVFormatContext * s , const AVPacket * pkt )
{
int ret = 1 ;
AVStream * st = s - > streams [ pkt - > stream_index ] ;
2016-04-10 21:58:15 +02:00
if ( 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 ) ;
}
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 } ,
{ 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 } ,
{ " 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 } ,
2014-05-07 19:46:40 +03:00
{ " dash_track_number " , " Track number for the DASH stream " , OFFSET ( dash_track_number ) , AV_OPT_TYPE_INT , { . i64 = 1 } , 0 , 127 , 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 } ,
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 } ,
2013-04-30 09:36:20 +03:00
{ NULL } ,
} ;
2010-06-05 01:40:54 +03:00
# if CONFIG_MATROSKA_MUXER
2013-04-30 09:36:20 +03:00
static const AVClass matroska_class = {
. class_name = " matroska muxer " ,
. item_name = av_default_item_name ,
. option = options ,
. version = LIBAVUTIL_VERSION_INT ,
} ;
2011-01-26 00:03:28 +02:00
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 ,
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 ,
2013-04-30 09:36:20 +03:00
. priv_class = & matroska_class ,
2007-09-05 03:22:37 +03:00
} ;
2010-06-05 01:40:54 +03:00
# endif
# if CONFIG_WEBM_MUXER
2013-04-30 09:36:20 +03:00
static const AVClass webm_class = {
. class_name = " webm muxer " ,
. item_name = av_default_item_name ,
. option = options ,
. version = LIBAVUTIL_VERSION_INT ,
} ;
2011-01-26 00:03:28 +02:00
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 ,
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 ,
2013-04-30 09:36:20 +03:00
. priv_class = & 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
2013-04-30 09:36:20 +03:00
static const AVClass mka_class = {
. class_name = " matroska audio muxer " ,
. item_name = av_default_item_name ,
. option = options ,
. version = LIBAVUTIL_VERSION_INT ,
} ;
2011-01-26 00:03:28 +02:00
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 ,
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
} ,
2013-04-30 09:36:20 +03:00
. priv_class = & mka_class ,
2007-09-05 03:24:19 +03:00
} ;
2010-06-05 01:40:54 +03:00
# endif