FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Macros | Functions | Variables
matroskaenc.c File Reference
#include <stdint.h>
#include "avc.h"
#include "hevc.h"
#include "avformat.h"
#include "avio_internal.h"
#include "avlanguage.h"
#include "flacenc.h"
#include "internal.h"
#include "isom.h"
#include "matroska.h"
#include "riff.h"
#include "subtitles.h"
#include "vorbiscomment.h"
#include "wv.h"
#include "libavutil/avstring.h"
#include "libavutil/channel_layout.h"
#include "libavutil/dict.h"
#include "libavutil/intfloat.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/lfg.h"
#include "libavutil/mathematics.h"
#include "libavutil/opt.h"
#include "libavutil/random_seed.h"
#include "libavutil/samplefmt.h"
#include "libavutil/sha.h"
#include "libavutil/stereo3d.h"
#include "libavcodec/xiph.h"
#include "libavcodec/mpeg4audio.h"
#include "libavcodec/internal.h"

Go to the source code of this file.

Data Structures

struct  ebml_master
 
struct  mkv_seekhead_entry
 
struct  mkv_seekhead
 
struct  mkv_cuepoint
 
struct  mkv_cues
 
struct  mkv_track
 
struct  MatroskaMuxContext
 

Macros

#define MODE_MATROSKAv2   0x01
 
#define MODE_WEBM   0x02
 
#define MAX_SEEKENTRY_SIZE   21
 2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit offset, 4 bytes for target EBML ID
 
#define MAX_CUETRACKPOS_SIZE   42
 per-cuepoint-track - 5 1-byte EBML IDs, 5 1-byte EBML sizes, 4 8-byte uint max
 
#define MAX_CUEPOINT_SIZE(num_tracks)   12 + MAX_CUETRACKPOS_SIZE * num_tracks
 per-cuepoint - 2 1-byte EBML IDs, 2 1-byte EBML sizes, 8-byte uint max
 
#define OPUS_SEEK_PREROLL   80000000
 Seek preroll value for opus.
 
#define OFFSET(x)   offsetof(MatroskaMuxContext, x)
 
#define FLAGS   AV_OPT_FLAG_ENCODING_PARAM
 

Functions

static int ebml_id_size (unsigned int id)
 
static void put_ebml_id (AVIOContext *pb, unsigned int id)
 
static void put_ebml_size_unknown (AVIOContext *pb, int bytes)
 Write an EBML size meaning "unknown size".
 
static int ebml_num_size (uint64_t num)
 Calculate how many bytes are needed to represent a given number in EBML.
 
static void put_ebml_num (AVIOContext *pb, uint64_t num, int bytes)
 Write a number in EBML variable length format.
 
static void put_ebml_uint (AVIOContext *pb, unsigned int elementid, uint64_t val)
 
static void put_ebml_sint (AVIOContext *pb, unsigned int elementid, int64_t val)
 
static void put_ebml_float (AVIOContext *pb, unsigned int elementid, double val)
 
static void put_ebml_binary (AVIOContext *pb, unsigned int elementid, const void *buf, int size)
 
static void put_ebml_string (AVIOContext *pb, unsigned int elementid, const char *str)
 
static void put_ebml_void (AVIOContext *pb, uint64_t size)
 Write a void element of a given size.
 
static ebml_master start_ebml_master (AVIOContext *pb, unsigned int elementid, uint64_t expectedsize)
 
static void end_ebml_master (AVIOContext *pb, ebml_master master)
 
static void put_xiph_size (AVIOContext *pb, int size)
 
static mkv_seekheadmkv_start_seekhead (AVIOContext *pb, int64_t segment_offset, int numelements)
 Initialize a mkv_seekhead element to be ready to index level 1 Matroska elements.
 
static int mkv_add_seekhead_entry (mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos)
 
static int64_t mkv_write_seekhead (AVIOContext *pb, mkv_seekhead *seekhead)
 Write the seek head to the file and free it.
 
static mkv_cuesmkv_start_cues (int64_t segment_offset)
 
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)
 
static int64_t mkv_write_cues (AVFormatContext *s, mkv_cues *cues, mkv_track *tracks, int num_tracks)
 
static int put_xiph_codecpriv (AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec)
 
static int put_wv_codecpriv (AVIOContext *pb, AVCodecContext *codec)
 
static int put_flac_codecpriv (AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec)
 
static void get_aac_sample_rates (AVFormatContext *s, AVCodecContext *codec, int *sample_rate, int *output_sample_rate)
 
static int mkv_write_native_codecprivate (AVFormatContext *s, AVCodecContext *codec, AVIOContext *dyn_cp)
 
static int mkv_write_codecprivate (AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec, int native_id, int qt_id)
 
static int mkv_write_stereo_mode (AVFormatContext *s, AVIOContext *pb, AVStream *st, int mode)
 
static int mkv_write_track (AVFormatContext *s, MatroskaMuxContext *mkv, int i, AVIOContext *pb, int default_stream_exists)
 
static int mkv_write_tracks (AVFormatContext *s)
 
static int mkv_write_chapters (AVFormatContext *s)
 
static void mkv_write_simpletag (AVIOContext *pb, AVDictionaryEntry *t)
 
static int mkv_write_tag (AVFormatContext *s, AVDictionary *m, unsigned int elementid, unsigned int uid, ebml_master *tags)
 
static int mkv_check_tag (AVDictionary *m)
 
static int mkv_write_tags (AVFormatContext *s)
 
static int mkv_write_attachments (AVFormatContext *s)
 
static int mkv_write_header (AVFormatContext *s)
 
static int mkv_blockgroup_size (int pkt_size)
 
static int mkv_strip_wavpack (const uint8_t *src, uint8_t **pdst, int *size)
 
static void mkv_write_block (AVFormatContext *s, AVIOContext *pb, unsigned int blockid, AVPacket *pkt, int flags)
 
static int srt_get_duration (uint8_t **buf)
 
static int mkv_write_srt_blocks (AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
 
static int mkv_write_vtt_blocks (AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
 
static void mkv_flush_dynbuf (AVFormatContext *s)
 
static void mkv_start_new_cluster (AVFormatContext *s, AVPacket *pkt)
 
static int mkv_write_packet_internal (AVFormatContext *s, AVPacket *pkt, int add_cue)
 
static int mkv_write_packet (AVFormatContext *s, AVPacket *pkt)
 
static int mkv_write_flush_packet (AVFormatContext *s, AVPacket *pkt)
 
static int mkv_write_trailer (AVFormatContext *s)
 
static int mkv_query_codec (enum AVCodecID codec_id, int std_compliance)
 

Variables

static const AVCodecTag additional_audio_tags []
 
static const AVCodecTag additional_video_tags []
 
static const AVOption options []
 

Macro Definition Documentation

#define MODE_MATROSKAv2   0x01

Definition at line 95 of file matroskaenc.c.

Referenced by mkv_write_header().

#define MODE_WEBM   0x02
#define MAX_SEEKENTRY_SIZE   21

2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit offset, 4 bytes for target EBML ID

Definition at line 131 of file matroskaenc.c.

Referenced by mkv_start_seekhead(), and mkv_write_seekhead().

#define MAX_CUETRACKPOS_SIZE   42

per-cuepoint-track - 5 1-byte EBML IDs, 5 1-byte EBML sizes, 4 8-byte uint max

Definition at line 135 of file matroskaenc.c.

Referenced by mkv_write_cues().

#define MAX_CUEPOINT_SIZE (   num_tracks)    12 + MAX_CUETRACKPOS_SIZE * num_tracks

per-cuepoint - 2 1-byte EBML IDs, 2 1-byte EBML sizes, 8-byte uint max

Definition at line 138 of file matroskaenc.c.

Referenced by mkv_write_cues().

#define OPUS_SEEK_PREROLL   80000000

Seek preroll value for opus.

Definition at line 141 of file matroskaenc.c.

#define OFFSET (   x)    offsetof(MatroskaMuxContext, x)

Definition at line 2012 of file matroskaenc.c.

#define FLAGS   AV_OPT_FLAG_ENCODING_PARAM

Definition at line 2013 of file matroskaenc.c.

Function Documentation

static int ebml_id_size ( unsigned int  id)
static

Definition at line 143 of file matroskaenc.c.

Referenced by mkv_write_seekhead(), and put_ebml_id().

static void put_ebml_id ( AVIOContext pb,
unsigned int  id 
)
static
static void put_ebml_size_unknown ( AVIOContext pb,
int  bytes 
)
static

Write an EBML size meaning "unknown size".

Parameters
bytesThe number of bytes the size should occupy (maximum: 8).

Definition at line 160 of file matroskaenc.c.

Referenced by start_ebml_master().

static int ebml_num_size ( uint64_t  num)
static

Calculate how many bytes are needed to represent a given number in EBML.

Definition at line 170 of file matroskaenc.c.

Referenced by mkv_blockgroup_size(), put_ebml_num(), and start_ebml_master().

static void put_ebml_num ( AVIOContext pb,
uint64_t  num,
int  bytes 
)
static

Write a number in EBML variable length format.

Parameters
bytesThe number of bytes that need to be used to write the number. If zero, any number of bytes can be used.

Definition at line 184 of file matroskaenc.c.

Referenced by end_ebml_master(), mkv_write_block(), mkv_write_seekhead(), mkv_write_vtt_blocks(), put_ebml_binary(), put_ebml_float(), put_ebml_sint(), put_ebml_uint(), and put_ebml_void().

static void put_ebml_uint ( AVIOContext pb,
unsigned int  elementid,
uint64_t  val 
)
static
static void put_ebml_sint ( AVIOContext pb,
unsigned int  elementid,
int64_t  val 
)
static

Definition at line 216 of file matroskaenc.c.

Referenced by mkv_write_block().

static void put_ebml_float ( AVIOContext pb,
unsigned int  elementid,
double  val 
)
static

Definition at line 229 of file matroskaenc.c.

Referenced by mkv_write_trailer().

static void put_ebml_binary ( AVIOContext pb,
unsigned int  elementid,
const void buf,
int  size 
)
static
static void put_ebml_string ( AVIOContext pb,
unsigned int  elementid,
const char *  str 
)
static
static void put_ebml_void ( AVIOContext pb,
uint64_t  size 
)
static

Write a void element of a given size.

Useful for reserving space in the file to be written to later.

Parameters
sizeThe number of bytes to reserve, which must be at least 2.

Definition at line 256 of file matroskaenc.c.

Referenced by mkv_start_seekhead(), mkv_write_header(), mkv_write_seekhead(), and mkv_write_trailer().

static ebml_master start_ebml_master ( AVIOContext pb,
unsigned int  elementid,
uint64_t  expectedsize 
)
static
static void end_ebml_master ( AVIOContext pb,
ebml_master  master 
)
static
static void put_xiph_size ( AVIOContext pb,
int  size 
)
static

Definition at line 292 of file matroskaenc.c.

Referenced by put_xiph_codecpriv().

static mkv_seekhead* mkv_start_seekhead ( AVIOContext pb,
int64_t  segment_offset,
int  numelements 
)
static

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.

Parameters
segment_offsetThe absolute offset to the position in the file where the segment begins.
numelementsThe maximum number of elements that will be indexed by this seek head, 0 if unlimited.

Definition at line 309 of file matroskaenc.c.

Referenced by mkv_write_header().

static int mkv_add_seekhead_entry ( mkv_seekhead seekhead,
unsigned int  elementid,
uint64_t  filepos 
)
static
static int64_t mkv_write_seekhead ( AVIOContext pb,
mkv_seekhead seekhead 
)
static

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.

Returns
The file offset where the seekhead was written, -1 if an error occurred.

Definition at line 358 of file matroskaenc.c.

Referenced by mkv_write_header(), and mkv_write_trailer().

static mkv_cues* mkv_start_cues ( int64_t  segment_offset)
static

Definition at line 402 of file matroskaenc.c.

Referenced by mkv_write_header().

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 
)
static

Definition at line 412 of file matroskaenc.c.

Referenced by mkv_write_packet_internal().

static int64_t mkv_write_cues ( AVFormatContext s,
mkv_cues cues,
mkv_track tracks,
int  num_tracks 
)
static

Definition at line 435 of file matroskaenc.c.

Referenced by mkv_write_trailer().

static int put_xiph_codecpriv ( AVFormatContext s,
AVIOContext pb,
AVCodecContext codec 
)
static

Definition at line 492 of file matroskaenc.c.

Referenced by mkv_write_native_codecprivate().

static int put_wv_codecpriv ( AVIOContext pb,
AVCodecContext codec 
)
static

Definition at line 520 of file matroskaenc.c.

Referenced by mkv_write_native_codecprivate().

static int put_flac_codecpriv ( AVFormatContext s,
AVIOContext pb,
AVCodecContext codec 
)
static

Definition at line 529 of file matroskaenc.c.

Referenced by mkv_write_native_codecprivate().

static void get_aac_sample_rates ( AVFormatContext s,
AVCodecContext codec,
int *  sample_rate,
int *  output_sample_rate 
)
static

Definition at line 573 of file matroskaenc.c.

Referenced by mkv_write_track().

static int mkv_write_native_codecprivate ( AVFormatContext s,
AVCodecContext codec,
AVIOContext dyn_cp 
)
static

Definition at line 589 of file matroskaenc.c.

Referenced by mkv_write_codecprivate().

static int mkv_write_codecprivate ( AVFormatContext s,
AVIOContext pb,
AVCodecContext codec,
int  native_id,
int  qt_id 
)
static

Definition at line 628 of file matroskaenc.c.

static int mkv_write_stereo_mode ( AVFormatContext s,
AVIOContext pb,
AVStream st,
int  mode 
)
static

Definition at line 698 of file matroskaenc.c.

static int mkv_write_track ( AVFormatContext s,
MatroskaMuxContext mkv,
int  i,
AVIOContext pb,
int  default_stream_exists 
)
static

Definition at line 789 of file matroskaenc.c.

Referenced by mkv_write_tracks().

static int mkv_write_tracks ( AVFormatContext s)
static

Definition at line 1014 of file matroskaenc.c.

Referenced by mkv_write_header().

static int mkv_write_chapters ( AVFormatContext s)
static

Definition at line 1039 of file matroskaenc.c.

Referenced by mkv_write_header(), and mkv_write_trailer().

static void mkv_write_simpletag ( AVIOContext pb,
AVDictionaryEntry t 
)
static

Definition at line 1085 of file matroskaenc.c.

Referenced by mkv_write_tag().

static int mkv_write_tag ( AVFormatContext s,
AVDictionary m,
unsigned int  elementid,
unsigned int  uid,
ebml_master tags 
)
static

Definition at line 1115 of file matroskaenc.c.

Referenced by mkv_write_tags().

static int mkv_check_tag ( AVDictionary m)
static

Definition at line 1146 of file matroskaenc.c.

Referenced by mkv_write_tags().

static int mkv_write_tags ( AVFormatContext s)
static

Definition at line 1157 of file matroskaenc.c.

Referenced by mkv_write_header().

static int mkv_write_attachments ( AVFormatContext s)
static

Definition at line 1195 of file matroskaenc.c.

Referenced by mkv_write_header().

static int mkv_write_header ( AVFormatContext s)
static

Definition at line 1274 of file matroskaenc.c.

static int mkv_blockgroup_size ( int  pkt_size)
static
static int mkv_strip_wavpack ( const uint8_t src,
uint8_t **  pdst,
int *  size 
)
static

Definition at line 1448 of file matroskaenc.c.

Referenced by mkv_write_block().

static void mkv_write_block ( AVFormatContext s,
AVIOContext pb,
unsigned int  blockid,
AVPacket pkt,
int  flags 
)
static

Definition at line 1501 of file matroskaenc.c.

Referenced by mkv_write_packet_internal(), and mkv_write_srt_blocks().

static int srt_get_duration ( uint8_t **  buf)
static

Definition at line 1592 of file matroskaenc.c.

Referenced by mkv_write_srt_blocks().

static int mkv_write_srt_blocks ( AVFormatContext s,
AVIOContext pb,
AVPacket pkt 
)
static

Definition at line 1616 of file matroskaenc.c.

Referenced by mkv_write_packet_internal().

static int mkv_write_vtt_blocks ( AVFormatContext s,
AVIOContext pb,
AVPacket pkt 
)
static

Definition at line 1633 of file matroskaenc.c.

Referenced by mkv_write_packet_internal().

static void mkv_flush_dynbuf ( AVFormatContext s)
static
static void mkv_start_new_cluster ( AVFormatContext s,
AVPacket pkt 
)
static

Definition at line 1686 of file matroskaenc.c.

Referenced by mkv_write_packet(), and mkv_write_packet_internal().

static int mkv_write_packet_internal ( AVFormatContext s,
AVPacket pkt,
int  add_cue 
)
static

Definition at line 1708 of file matroskaenc.c.

Referenced by mkv_write_packet(), and mkv_write_trailer().

static int mkv_write_packet ( AVFormatContext s,
AVPacket pkt 
)
static

Definition at line 1786 of file matroskaenc.c.

Referenced by mkv_write_flush_packet().

static int mkv_write_flush_packet ( AVFormatContext s,
AVPacket pkt 
)
static

Definition at line 1863 of file matroskaenc.c.

static int mkv_write_trailer ( AVFormatContext s)
static

Definition at line 1887 of file matroskaenc.c.

static int mkv_query_codec ( enum AVCodecID  codec_id,
int  std_compliance 
)
static

Definition at line 1970 of file matroskaenc.c.

Variable Documentation

const AVCodecTag additional_audio_tags[]
static
Initial value:
= {
{ AV_CODEC_ID_ALAC, 0XFFFFFFFF },
{ AV_CODEC_ID_EAC3, 0XFFFFFFFF },
{ 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 },
{ 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 }
}

Definition at line 1987 of file matroskaenc.c.

const AVCodecTag additional_video_tags[]
static
Initial value:
= {
{ AV_CODEC_ID_RV10, 0xFFFFFFFF },
{ AV_CODEC_ID_RV20, 0xFFFFFFFF },
{ AV_CODEC_ID_RV30, 0xFFFFFFFF },
{ AV_CODEC_ID_RV40, 0xFFFFFFFF },
{ AV_CODEC_ID_VP9, 0xFFFFFFFF },
{ AV_CODEC_ID_NONE, 0xFFFFFFFF }
}

Definition at line 2003 of file matroskaenc.c.

const AVOption options[]
static
Initial value:
= {
{ "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 },
{ "dash", "Create a WebM file conforming to WebM DASH specification", OFFSET(is_dash), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
{ "dash_track_number", "Track number for the DASH stream", OFFSET(dash_track_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 127, FLAGS },
{ NULL },
}

Definition at line 2014 of file matroskaenc.c.