|
|
|
@ -134,22 +134,21 @@ typedef struct MatroskaMuxContext { |
|
|
|
|
#define MAX_CUETRACKPOS_SIZE 42 |
|
|
|
|
|
|
|
|
|
/** per-cuepoint - 2 1-byte EBML IDs, 2 1-byte EBML sizes, 8-byte uint max */ |
|
|
|
|
#define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE*num_tracks |
|
|
|
|
#define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE * num_tracks |
|
|
|
|
|
|
|
|
|
/** Seek preroll value for opus */ |
|
|
|
|
#define OPUS_SEEK_PREROLL 80000000 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int ebml_id_size(unsigned int id) |
|
|
|
|
{ |
|
|
|
|
return (av_log2(id+1)-1)/7+1; |
|
|
|
|
return (av_log2(id + 1) - 1) / 7 + 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void put_ebml_id(AVIOContext *pb, unsigned int id) |
|
|
|
|
{ |
|
|
|
|
int i = ebml_id_size(id); |
|
|
|
|
while (i--) |
|
|
|
|
avio_w8(pb, (uint8_t)(id >> (i*8))); |
|
|
|
|
avio_w8(pb, (uint8_t)(id >> (i * 8))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -170,7 +169,8 @@ static void put_ebml_size_unknown(AVIOContext *pb, int bytes) |
|
|
|
|
static int ebml_num_size(uint64_t num) |
|
|
|
|
{ |
|
|
|
|
int bytes = 1; |
|
|
|
|
while ((num+1) >> bytes*7) bytes++; |
|
|
|
|
while ((num + 1) >> bytes * 7) |
|
|
|
|
bytes++; |
|
|
|
|
return bytes; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -185,7 +185,7 @@ static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes) |
|
|
|
|
int i, needed_bytes = ebml_num_size(num); |
|
|
|
|
|
|
|
|
|
// sizes larger than this are currently undefined in EBML
|
|
|
|
|
av_assert0(num < (1ULL<<56)-1); |
|
|
|
|
av_assert0(num < (1ULL << 56) - 1); |
|
|
|
|
|
|
|
|
|
if (bytes == 0) |
|
|
|
|
// don't care how many bytes are used, so use the min
|
|
|
|
@ -194,21 +194,22 @@ static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes) |
|
|
|
|
// that we need to use, so write unknown size. This shouldn't happen.
|
|
|
|
|
av_assert0(bytes >= needed_bytes); |
|
|
|
|
|
|
|
|
|
num |= 1ULL << bytes*7; |
|
|
|
|
num |= 1ULL << bytes * 7; |
|
|
|
|
for (i = bytes - 1; i >= 0; i--) |
|
|
|
|
avio_w8(pb, (uint8_t)(num >> i*8)); |
|
|
|
|
avio_w8(pb, (uint8_t)(num >> i * 8)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val) |
|
|
|
|
{ |
|
|
|
|
int i, bytes = 1; |
|
|
|
|
uint64_t tmp = val; |
|
|
|
|
while (tmp>>=8) bytes++; |
|
|
|
|
while (tmp >>= 8) |
|
|
|
|
bytes++; |
|
|
|
|
|
|
|
|
|
put_ebml_id(pb, elementid); |
|
|
|
|
put_ebml_num(pb, bytes, 0); |
|
|
|
|
for (i = bytes - 1; i >= 0; i--) |
|
|
|
|
avio_w8(pb, (uint8_t)(val >> i*8)); |
|
|
|
|
avio_w8(pb, (uint8_t)(val >> i * 8)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void put_ebml_sint(AVIOContext *pb, unsigned int elementid, int64_t val) |
|
|
|
@ -221,7 +222,7 @@ static void put_ebml_sint(AVIOContext *pb, unsigned int elementid, int64_t val) |
|
|
|
|
put_ebml_id(pb, elementid); |
|
|
|
|
put_ebml_num(pb, bytes, 0); |
|
|
|
|
for (i = bytes - 1; i >= 0; i--) |
|
|
|
|
avio_w8(pb, (uint8_t)(val >> i*8)); |
|
|
|
|
avio_w8(pb, (uint8_t)(val >> i * 8)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val) |
|
|
|
@ -239,7 +240,8 @@ static void put_ebml_binary(AVIOContext *pb, unsigned int elementid, |
|
|
|
|
avio_write(pb, buf, size); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void put_ebml_string(AVIOContext *pb, unsigned int elementid, const char *str) |
|
|
|
|
static void put_ebml_string(AVIOContext *pb, unsigned int elementid, |
|
|
|
|
const char *str) |
|
|
|
|
{ |
|
|
|
|
put_ebml_binary(pb, elementid, str, strlen(str)); |
|
|
|
|
} |
|
|
|
@ -261,18 +263,19 @@ static void put_ebml_void(AVIOContext *pb, uint64_t size) |
|
|
|
|
// size we need to reserve so 2 cases, we use 8 bytes to store the
|
|
|
|
|
// size if possible, 1 byte otherwise
|
|
|
|
|
if (size < 10) |
|
|
|
|
put_ebml_num(pb, size-1, 0); |
|
|
|
|
put_ebml_num(pb, size - 1, 0); |
|
|
|
|
else |
|
|
|
|
put_ebml_num(pb, size-9, 8); |
|
|
|
|
put_ebml_num(pb, size - 9, 8); |
|
|
|
|
ffio_fill(pb, 0, currentpos + size - avio_tell(pb)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static ebml_master start_ebml_master(AVIOContext *pb, unsigned int elementid, uint64_t expectedsize) |
|
|
|
|
static ebml_master start_ebml_master(AVIOContext *pb, unsigned int elementid, |
|
|
|
|
uint64_t expectedsize) |
|
|
|
|
{ |
|
|
|
|
int bytes = expectedsize ? ebml_num_size(expectedsize) : 8; |
|
|
|
|
put_ebml_id(pb, elementid); |
|
|
|
|
put_ebml_size_unknown(pb, bytes); |
|
|
|
|
return (ebml_master){ avio_tell(pb), bytes }; |
|
|
|
|
return (ebml_master) {avio_tell(pb), bytes }; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void end_ebml_master(AVIOContext *pb, ebml_master master) |
|
|
|
@ -302,7 +305,8 @@ static void put_xiph_size(AVIOContext *pb, int size) |
|
|
|
|
* @param numelements The maximum number of elements that will be indexed |
|
|
|
|
* by this seek head, 0 if unlimited. |
|
|
|
|
*/ |
|
|
|
|
static mkv_seekhead * mkv_start_seekhead(AVIOContext *pb, int64_t segment_offset, int numelements) |
|
|
|
|
static mkv_seekhead *mkv_start_seekhead(AVIOContext *pb, int64_t segment_offset, |
|
|
|
|
int numelements) |
|
|
|
|
{ |
|
|
|
|
mkv_seekhead *new_seekhead = av_mallocz(sizeof(mkv_seekhead)); |
|
|
|
|
if (new_seekhead == NULL) |
|
|
|
@ -316,7 +320,7 @@ static mkv_seekhead * mkv_start_seekhead(AVIOContext *pb, int64_t segment_offset |
|
|
|
|
// and size, and 3 bytes to guarantee that an EBML void element
|
|
|
|
|
// will fit afterwards
|
|
|
|
|
new_seekhead->reserved_size = numelements * MAX_SEEKENTRY_SIZE + 13; |
|
|
|
|
new_seekhead->max_entries = numelements; |
|
|
|
|
new_seekhead->max_entries = numelements; |
|
|
|
|
put_ebml_void(pb, new_seekhead->reserved_size); |
|
|
|
|
} |
|
|
|
|
return new_seekhead; |
|
|
|
@ -394,7 +398,7 @@ fail: |
|
|
|
|
return currentpos; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static mkv_cues * mkv_start_cues(int64_t segment_offset) |
|
|
|
|
static mkv_cues *mkv_start_cues(int64_t segment_offset) |
|
|
|
|
{ |
|
|
|
|
mkv_cues *cues = av_mallocz(sizeof(mkv_cues)); |
|
|
|
|
if (cues == NULL) |
|
|
|
@ -552,13 +556,15 @@ static int put_flac_codecpriv(AVFormatContext *s, |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int *sample_rate, int *output_sample_rate) |
|
|
|
|
static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, |
|
|
|
|
int *sample_rate, int *output_sample_rate) |
|
|
|
|
{ |
|
|
|
|
MPEG4AudioConfig mp4ac; |
|
|
|
|
|
|
|
|
|
if (avpriv_mpeg4audio_get_config(&mp4ac, codec->extradata, |
|
|
|
|
codec->extradata_size * 8, 1) < 0) { |
|
|
|
|
av_log(s, AV_LOG_WARNING, "Error parsing AAC extradata, unable to determine samplerate.\n"); |
|
|
|
|
av_log(s, AV_LOG_WARNING, |
|
|
|
|
"Error parsing AAC extradata, unable to determine samplerate.\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -566,27 +572,32 @@ static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int |
|
|
|
|
*output_sample_rate = mp4ac.ext_sample_rate; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec, int native_id, int qt_id) |
|
|
|
|
static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, |
|
|
|
|
AVCodecContext *codec, int native_id, |
|
|
|
|
int qt_id) |
|
|
|
|
{ |
|
|
|
|
AVIOContext *dyn_cp; |
|
|
|
|
uint8_t *codecpriv; |
|
|
|
|
int ret, codecpriv_size; |
|
|
|
|
|
|
|
|
|
ret = avio_open_dyn_buf(&dyn_cp); |
|
|
|
|
if(ret < 0) |
|
|
|
|
if (ret < 0) |
|
|
|
|
return ret; |
|
|
|
|
|
|
|
|
|
if (native_id) { |
|
|
|
|
if (codec->codec_id == AV_CODEC_ID_VORBIS || codec->codec_id == AV_CODEC_ID_THEORA) |
|
|
|
|
if (codec->codec_id == AV_CODEC_ID_VORBIS || |
|
|
|
|
codec->codec_id == AV_CODEC_ID_THEORA) |
|
|
|
|
ret = put_xiph_codecpriv(s, dyn_cp, codec); |
|
|
|
|
else if (codec->codec_id == AV_CODEC_ID_FLAC) |
|
|
|
|
ret = put_flac_codecpriv(s, dyn_cp, codec); |
|
|
|
|
else if (codec->codec_id == AV_CODEC_ID_WAVPACK) |
|
|
|
|
ret = put_wv_codecpriv(dyn_cp, codec); |
|
|
|
|
else if (codec->codec_id == AV_CODEC_ID_H264) |
|
|
|
|
ret = ff_isom_write_avcc(dyn_cp, codec->extradata, codec->extradata_size); |
|
|
|
|
ret = ff_isom_write_avcc(dyn_cp, codec->extradata, |
|
|
|
|
codec->extradata_size); |
|
|
|
|
else if (codec->codec_id == AV_CODEC_ID_HEVC) |
|
|
|
|
ret = ff_isom_write_hvcc(dyn_cp, codec->extradata, codec->extradata_size, 0); |
|
|
|
|
ret = ff_isom_write_hvcc(dyn_cp, codec->extradata, |
|
|
|
|
codec->extradata_size, 0); |
|
|
|
|
else if (codec->codec_id == AV_CODEC_ID_ALAC) { |
|
|
|
|
if (codec->extradata_size < 36) { |
|
|
|
|
av_log(s, AV_LOG_ERROR, |
|
|
|
@ -595,25 +606,25 @@ static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecCo |
|
|
|
|
ret = AVERROR_INVALIDDATA; |
|
|
|
|
} else |
|
|
|
|
avio_write(dyn_cp, codec->extradata + 12, |
|
|
|
|
codec->extradata_size - 12); |
|
|
|
|
codec->extradata_size - 12); |
|
|
|
|
} else if (codec->codec_id == AV_CODEC_ID_PRORES && |
|
|
|
|
ff_codec_get_id(ff_codec_movvideo_tags, codec->codec_tag) == AV_CODEC_ID_PRORES) { |
|
|
|
|
avio_wl32(dyn_cp, codec->codec_tag); |
|
|
|
|
} |
|
|
|
|
else if (codec->extradata_size && codec->codec_id != AV_CODEC_ID_TTA) |
|
|
|
|
} else if (codec->extradata_size && codec->codec_id != AV_CODEC_ID_TTA) |
|
|
|
|
avio_write(dyn_cp, codec->extradata, codec->extradata_size); |
|
|
|
|
} else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) { |
|
|
|
|
if (qt_id) { |
|
|
|
|
if (!codec->codec_tag) |
|
|
|
|
codec->codec_tag = ff_codec_get_tag(ff_codec_movvideo_tags, codec->codec_id); |
|
|
|
|
codec->codec_tag = ff_codec_get_tag(ff_codec_movvideo_tags, |
|
|
|
|
codec->codec_id); |
|
|
|
|
if (codec->extradata_size) { |
|
|
|
|
if ( ff_codec_get_id(ff_codec_movvideo_tags, codec->codec_tag) == codec->codec_id |
|
|
|
|
&& ff_codec_get_id(ff_codec_movvideo_tags, AV_RL32(codec->extradata+4)) != codec->codec_id |
|
|
|
|
&& ff_codec_get_id(ff_codec_movvideo_tags, AV_RL32(codec->extradata + 4)) != codec->codec_id |
|
|
|
|
) { |
|
|
|
|
int i; |
|
|
|
|
avio_wb32(dyn_cp, 0x5a + codec->extradata_size); |
|
|
|
|
avio_wl32(dyn_cp, codec->codec_tag); |
|
|
|
|
for(i=0; i<0x5a-8; i++) |
|
|
|
|
for(i = 0; i < 0x5a - 8; i++) |
|
|
|
|
avio_w8(dyn_cp, 0); |
|
|
|
|
} |
|
|
|
|
avio_write(dyn_cp, codec->extradata, codec->extradata_size); |
|
|
|
@ -624,7 +635,8 @@ static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecCo |
|
|
|
|
avcodec_get_name(codec->codec_id)); |
|
|
|
|
|
|
|
|
|
if (!codec->codec_tag) |
|
|
|
|
codec->codec_tag = ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id); |
|
|
|
|
codec->codec_tag = ff_codec_get_tag(ff_codec_bmp_tags, |
|
|
|
|
codec->codec_id); |
|
|
|
|
if (!codec->codec_tag) { |
|
|
|
|
av_log(s, AV_LOG_ERROR, "No bmp codec tag found for codec %s\n", |
|
|
|
|
avcodec_get_name(codec->codec_id)); |
|
|
|
@ -633,7 +645,6 @@ static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecCo |
|
|
|
|
|
|
|
|
|
ff_put_bmp_header(dyn_cp, codec, ff_codec_bmp_tags, 0, 0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} else if (codec->codec_type == AVMEDIA_TYPE_AUDIO) { |
|
|
|
|
unsigned int tag; |
|
|
|
|
tag = ff_codec_get_tag(ff_codec_wav_tags, codec->codec_id); |
|
|
|
@ -650,7 +661,8 @@ static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecCo |
|
|
|
|
|
|
|
|
|
codecpriv_size = avio_close_dyn_buf(dyn_cp, &codecpriv); |
|
|
|
|
if (codecpriv_size) |
|
|
|
|
put_ebml_binary(pb, MATROSKA_ID_CODECPRIVATE, codecpriv, codecpriv_size); |
|
|
|
|
put_ebml_binary(pb, MATROSKA_ID_CODECPRIVATE, codecpriv, |
|
|
|
|
codecpriv_size); |
|
|
|
|
av_free(codecpriv); |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
@ -663,7 +675,8 @@ static int mkv_write_tracks(AVFormatContext *s) |
|
|
|
|
int i, j, ret, default_stream_exists = 0; |
|
|
|
|
|
|
|
|
|
ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_TRACKS, avio_tell(pb)); |
|
|
|
|
if (ret < 0) return ret; |
|
|
|
|
if (ret < 0) |
|
|
|
|
return ret; |
|
|
|
|
|
|
|
|
|
tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS, 0); |
|
|
|
|
for (i = 0; i < s->nb_streams; i++) { |
|
|
|
@ -775,133 +788,134 @@ static int mkv_write_tracks(AVFormatContext *s) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
switch (codec->codec_type) { |
|
|
|
|
case AVMEDIA_TYPE_VIDEO: |
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_VIDEO); |
|
|
|
|
|
|
|
|
|
if( st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0 |
|
|
|
|
&& 1.0/av_q2d(st->avg_frame_rate) > av_q2d(codec->time_base)) |
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, 1E9 / av_q2d(st->avg_frame_rate)); |
|
|
|
|
else |
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, av_q2d(codec->time_base)*1E9); |
|
|
|
|
|
|
|
|
|
if (!native_id && |
|
|
|
|
ff_codec_get_tag(ff_codec_movvideo_tags, codec->codec_id) && |
|
|
|
|
(!ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id) |
|
|
|
|
|| codec->codec_id == AV_CODEC_ID_SVQ1 |
|
|
|
|
|| codec->codec_id == AV_CODEC_ID_SVQ3 |
|
|
|
|
|| codec->codec_id == AV_CODEC_ID_CINEPAK)) |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
case AVMEDIA_TYPE_VIDEO: |
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_VIDEO); |
|
|
|
|
|
|
|
|
|
if( st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0 |
|
|
|
|
&& 1.0/av_q2d(st->avg_frame_rate) > av_q2d(codec->time_base)) |
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, 1E9 / av_q2d(st->avg_frame_rate)); |
|
|
|
|
else |
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, av_q2d(codec->time_base)*1E9); |
|
|
|
|
|
|
|
|
|
if (!native_id && |
|
|
|
|
ff_codec_get_tag(ff_codec_movvideo_tags, codec->codec_id) && |
|
|
|
|
(!ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id) || |
|
|
|
|
codec->codec_id == AV_CODEC_ID_SVQ1 || |
|
|
|
|
codec->codec_id == AV_CODEC_ID_SVQ3 || |
|
|
|
|
codec->codec_id == AV_CODEC_ID_CINEPAK)) |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0); |
|
|
|
|
// XXX: interlace flag?
|
|
|
|
|
put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELWIDTH , codec->width); |
|
|
|
|
put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELHEIGHT, codec->height); |
|
|
|
|
|
|
|
|
|
if ((tag = av_dict_get(st->metadata, "stereo_mode", NULL, 0)) || |
|
|
|
|
(tag = av_dict_get( s->metadata, "stereo_mode", NULL, 0))) { |
|
|
|
|
// save stereo mode flag
|
|
|
|
|
uint64_t st_mode = MATROSKA_VIDEO_STEREO_MODE_COUNT; |
|
|
|
|
|
|
|
|
|
for (j=0; j<MATROSKA_VIDEO_STEREO_MODE_COUNT; j++) |
|
|
|
|
if (!strcmp(tag->value, ff_matroska_video_stereo_mode[j])){ |
|
|
|
|
st_mode = j; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ((mkv->mode == MODE_WEBM && st_mode > 3 && st_mode != 11) |
|
|
|
|
|| st_mode >= MATROSKA_VIDEO_STEREO_MODE_COUNT) { |
|
|
|
|
av_log(s, AV_LOG_ERROR, |
|
|
|
|
"The specified stereo mode is not valid.\n"); |
|
|
|
|
return AVERROR(EINVAL); |
|
|
|
|
} else |
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_VIDEOSTEREOMODE, st_mode); |
|
|
|
|
|
|
|
|
|
switch (st_mode) { |
|
|
|
|
case 1: |
|
|
|
|
case 8: |
|
|
|
|
case 9: |
|
|
|
|
case 11: |
|
|
|
|
display_width_div = 2; |
|
|
|
|
break; |
|
|
|
|
case 2: |
|
|
|
|
case 3: |
|
|
|
|
case 6: |
|
|
|
|
case 7: |
|
|
|
|
display_height_div = 2; |
|
|
|
|
subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0); |
|
|
|
|
// XXX: interlace flag?
|
|
|
|
|
put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELWIDTH , codec->width); |
|
|
|
|
put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELHEIGHT, codec->height); |
|
|
|
|
|
|
|
|
|
if ((tag = av_dict_get(st->metadata, "stereo_mode", NULL, 0)) || |
|
|
|
|
(tag = av_dict_get( s->metadata, "stereo_mode", NULL, 0))) { |
|
|
|
|
// save stereo mode flag
|
|
|
|
|
uint64_t st_mode = MATROSKA_VIDEO_STEREO_MODE_COUNT; |
|
|
|
|
|
|
|
|
|
for (j=0; j<MATROSKA_VIDEO_STEREO_MODE_COUNT; j++) |
|
|
|
|
if (!strcmp(tag->value, ff_matroska_video_stereo_mode[j])){ |
|
|
|
|
st_mode = j; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ((tag = av_dict_get(st->metadata, "alpha_mode", NULL, 0)) || |
|
|
|
|
(tag = av_dict_get( s->metadata, "alpha_mode", NULL, 0)) || |
|
|
|
|
(codec->pix_fmt == AV_PIX_FMT_YUVA420P)) { |
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_VIDEOALPHAMODE, 1); |
|
|
|
|
if ((mkv->mode == MODE_WEBM && st_mode > 3 && st_mode != 11) |
|
|
|
|
|| st_mode >= MATROSKA_VIDEO_STEREO_MODE_COUNT) { |
|
|
|
|
av_log(s, AV_LOG_ERROR, |
|
|
|
|
"The specified stereo mode is not valid.\n"); |
|
|
|
|
return AVERROR(EINVAL); |
|
|
|
|
} else |
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_VIDEOSTEREOMODE, st_mode); |
|
|
|
|
|
|
|
|
|
switch (st_mode) { |
|
|
|
|
case 1: |
|
|
|
|
case 8: |
|
|
|
|
case 9: |
|
|
|
|
case 11: |
|
|
|
|
display_width_div = 2; |
|
|
|
|
break; |
|
|
|
|
case 2: |
|
|
|
|
case 3: |
|
|
|
|
case 6: |
|
|
|
|
case 7: |
|
|
|
|
display_height_div = 2; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (st->sample_aspect_ratio.num) { |
|
|
|
|
int64_t d_width = av_rescale(codec->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den); |
|
|
|
|
if (d_width > INT_MAX) { |
|
|
|
|
av_log(s, AV_LOG_ERROR, "Overflow in display width\n"); |
|
|
|
|
return AVERROR(EINVAL); |
|
|
|
|
} |
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , d_width / display_width_div); |
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, codec->height / display_height_div); |
|
|
|
|
} else if (display_width_div != 1 || display_height_div != 1) { |
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , codec->width / display_width_div); |
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, codec->height / display_height_div); |
|
|
|
|
} |
|
|
|
|
if ((tag = av_dict_get(st->metadata, "alpha_mode", NULL, 0)) || |
|
|
|
|
(tag = av_dict_get( s->metadata, "alpha_mode", NULL, 0)) || |
|
|
|
|
(codec->pix_fmt == AV_PIX_FMT_YUVA420P)) { |
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_VIDEOALPHAMODE, 1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (codec->codec_id == AV_CODEC_ID_RAWVIDEO) { |
|
|
|
|
uint32_t color_space = av_le2ne32(codec->codec_tag); |
|
|
|
|
put_ebml_binary(pb, MATROSKA_ID_VIDEOCOLORSPACE, &color_space, sizeof(color_space)); |
|
|
|
|
} |
|
|
|
|
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); |
|
|
|
|
put_ebml_uint (pb, MATROSKA_ID_AUDIOCHANNELS , codec->channels); |
|
|
|
|
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) { |
|
|
|
|
av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", codec->codec_id); |
|
|
|
|
return AVERROR(ENOSYS); |
|
|
|
|
if (st->sample_aspect_ratio.num) { |
|
|
|
|
int64_t d_width = av_rescale(codec->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den); |
|
|
|
|
if (d_width > INT_MAX) { |
|
|
|
|
av_log(s, AV_LOG_ERROR, "Overflow in display width\n"); |
|
|
|
|
return AVERROR(EINVAL); |
|
|
|
|
} |
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , d_width / display_width_div); |
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, codec->height / display_height_div); |
|
|
|
|
} else if (display_width_div != 1 || display_height_div != 1) { |
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , codec->width / display_width_div); |
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, codec->height / display_height_div); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT) |
|
|
|
|
native_id = MATROSKA_TRACK_TYPE_SUBTITLE; |
|
|
|
|
if (codec->codec_id == AV_CODEC_ID_RAWVIDEO) { |
|
|
|
|
uint32_t color_space = av_le2ne32(codec->codec_tag); |
|
|
|
|
put_ebml_binary(pb, MATROSKA_ID_VIDEOCOLORSPACE, &color_space, sizeof(color_space)); |
|
|
|
|
} |
|
|
|
|
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); |
|
|
|
|
put_ebml_uint (pb, MATROSKA_ID_AUDIOCHANNELS , codec->channels); |
|
|
|
|
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) { |
|
|
|
|
av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", codec->codec_id); |
|
|
|
|
return AVERROR(ENOSYS); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, native_id); |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n"); |
|
|
|
|
return AVERROR(EINVAL); |
|
|
|
|
if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT) |
|
|
|
|
native_id = MATROSKA_TRACK_TYPE_SUBTITLE; |
|
|
|
|
|
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, native_id); |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n"); |
|
|
|
|
return AVERROR(EINVAL); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT) { |
|
|
|
|
ret = mkv_write_codecprivate(s, pb, codec, native_id, qt_id); |
|
|
|
|
if (ret < 0) return ret; |
|
|
|
|
if (ret < 0) |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
end_ebml_master(pb, track); |
|
|
|
@ -1153,8 +1167,10 @@ static int mkv_write_header(AVFormatContext *s) |
|
|
|
|
AVDictionaryEntry *tag; |
|
|
|
|
int ret, i, version = 2; |
|
|
|
|
|
|
|
|
|
if (!strcmp(s->oformat->name, "webm")) mkv->mode = MODE_WEBM; |
|
|
|
|
else mkv->mode = MODE_MATROSKAv2; |
|
|
|
|
if (!strcmp(s->oformat->name, "webm")) |
|
|
|
|
mkv->mode = MODE_WEBM; |
|
|
|
|
else |
|
|
|
|
mkv->mode = MODE_MATROSKAv2; |
|
|
|
|
|
|
|
|
|
if (s->avoid_negative_ts < 0) |
|
|
|
|
s->avoid_negative_ts = 1; |
|
|
|
@ -1224,7 +1240,7 @@ static int mkv_write_header(AVFormatContext *s) |
|
|
|
|
for (i = 0; i < 4; i++) |
|
|
|
|
segment_uid[i] = av_lfg_get(&lfg); |
|
|
|
|
|
|
|
|
|
put_ebml_string(pb, MATROSKA_ID_MUXINGAPP , LIBAVFORMAT_IDENT); |
|
|
|
|
put_ebml_string(pb, MATROSKA_ID_MUXINGAPP, LIBAVFORMAT_IDENT); |
|
|
|
|
if ((tag = av_dict_get(s->metadata, "encoding_tool", NULL, 0))) |
|
|
|
|
put_ebml_string(pb, MATROSKA_ID_WRITINGAPP, tag->value); |
|
|
|
|
else |
|
|
|
@ -1251,20 +1267,24 @@ static int mkv_write_header(AVFormatContext *s) |
|
|
|
|
end_ebml_master(pb, segment_info); |
|
|
|
|
|
|
|
|
|
ret = mkv_write_tracks(s); |
|
|
|
|
if (ret < 0) return ret; |
|
|
|
|
if (ret < 0) |
|
|
|
|
return ret; |
|
|
|
|
|
|
|
|
|
for (i = 0; i < s->nb_chapters; i++) |
|
|
|
|
mkv->chapter_id_offset = FFMAX(mkv->chapter_id_offset, 1LL - s->chapters[i]->id); |
|
|
|
|
|
|
|
|
|
if (mkv->mode != MODE_WEBM) { |
|
|
|
|
ret = mkv_write_chapters(s); |
|
|
|
|
if (ret < 0) return ret; |
|
|
|
|
if (ret < 0) |
|
|
|
|
return ret; |
|
|
|
|
|
|
|
|
|
ret = mkv_write_tags(s); |
|
|
|
|
if (ret < 0) return ret; |
|
|
|
|
if (ret < 0) |
|
|
|
|
return ret; |
|
|
|
|
|
|
|
|
|
ret = mkv_write_attachments(s); |
|
|
|
|
if (ret < 0) return ret; |
|
|
|
|
if (ret < 0) |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!s->pb->seekable) |
|
|
|
@ -1321,8 +1341,8 @@ static int ass_get_duration(const uint8_t *p) |
|
|
|
|
if (sscanf(p, "%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d", |
|
|
|
|
&sh, &sm, &ss, &sc, &eh, &em, &es, &ec) != 8) |
|
|
|
|
return 0; |
|
|
|
|
start = 3600000LL*sh + 60000LL*sm + 1000LL*ss + 10LL*sc; |
|
|
|
|
end = 3600000LL*eh + 60000LL*em + 1000LL*es + 10LL*ec; |
|
|
|
|
start = 3600000LL * sh + 60000LL * sm + 1000LL * ss + 10LL * sc; |
|
|
|
|
end = 3600000LL * eh + 60000LL * em + 1000LL * es + 10LL * ec; |
|
|
|
|
return end - start; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -1332,7 +1352,8 @@ static int ass_get_duration(const uint8_t *p) |
|
|
|
|
* (starting from *datap) and updates *datap so it points to the beginning of the data |
|
|
|
|
* corresponding to the next block. |
|
|
|
|
*/ |
|
|
|
|
static int mkv_write_ass_block(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt, uint8_t **datap) |
|
|
|
|
static int mkv_write_ass_block(AVFormatContext *s, AVIOContext *pb, |
|
|
|
|
AVPacket *pkt, uint8_t **datap) |
|
|
|
|
{ |
|
|
|
|
MatroskaMuxContext *mkv = s->priv_data; |
|
|
|
|
int i, layer = 0, size, line_size, data_size = pkt->size - (*datap - pkt->data); |
|
|
|
@ -1341,27 +1362,30 @@ static int mkv_write_ass_block(AVFormatContext *s, AVIOContext *pb, AVPacket *pk |
|
|
|
|
char buffer[2048]; |
|
|
|
|
|
|
|
|
|
int duration = ass_get_duration(data); |
|
|
|
|
end = memchr(data, '\n', data_size); |
|
|
|
|
size = line_size = end ? end-data+1 : data_size; |
|
|
|
|
size -= end ? (end[-1]=='\r')+1 : 0; |
|
|
|
|
start = data; |
|
|
|
|
for (i=0; i<3; i++, start++) |
|
|
|
|
if (!(start = memchr(start, ',', size-(start-data)))) |
|
|
|
|
end = memchr(data, '\n', data_size); |
|
|
|
|
size = line_size = end ? end - data + 1 : data_size; |
|
|
|
|
size -= end ? (end[-1] == '\r') + 1 : 0; |
|
|
|
|
start = data; |
|
|
|
|
for (i = 0; i < 3; i++, start++) |
|
|
|
|
if (!(start = memchr(start, ',', size - (start - data)))) |
|
|
|
|
return duration; |
|
|
|
|
size -= start - data; |
|
|
|
|
sscanf(data, "Dialogue: %d,", &layer); |
|
|
|
|
i = snprintf(buffer, sizeof(buffer), "%"PRId64",%d,", |
|
|
|
|
i = snprintf(buffer, sizeof(buffer), "%" PRId64 ",%d,", |
|
|
|
|
s->streams[pkt->stream_index]->nb_frames, layer); |
|
|
|
|
size = FFMIN(i+size, sizeof(buffer)); |
|
|
|
|
memcpy(buffer+i, start, size-i); |
|
|
|
|
size = FFMIN(i + size, sizeof(buffer)); |
|
|
|
|
memcpy(buffer + i, start, size - i); |
|
|
|
|
|
|
|
|
|
av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, " |
|
|
|
|
av_log(s, AV_LOG_DEBUG, |
|
|
|
|
"Writing block at offset %" PRIu64 ", size %d, " |
|
|
|
|
"pts %" PRId64 ", duration %d\n", |
|
|
|
|
avio_tell(pb), size, pkt->pts, duration); |
|
|
|
|
blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(size)); |
|
|
|
|
blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, |
|
|
|
|
mkv_blockgroup_size(size)); |
|
|
|
|
put_ebml_id(pb, MATROSKA_ID_BLOCK); |
|
|
|
|
put_ebml_num(pb, size+4, 0); |
|
|
|
|
avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
|
|
|
|
|
put_ebml_num(pb, size + 4, 0); |
|
|
|
|
// this assumes stream_index is less than 126
|
|
|
|
|
avio_w8(pb, 0x80 | (pkt->stream_index + 1)); |
|
|
|
|
avio_wb16(pb, pkt->pts - mkv->cluster_pts); |
|
|
|
|
avio_w8(pb, 0); |
|
|
|
|
avio_write(pb, buffer, size); |
|
|
|
@ -1461,7 +1485,7 @@ static void mkv_write_block(AVFormatContext *s, AVIOContext *pb, |
|
|
|
|
if (codec->codec_id == AV_CODEC_ID_PRORES) { |
|
|
|
|
/* Matroska specification requires to remove the first QuickTime atom
|
|
|
|
|
*/ |
|
|
|
|
size -= 8; |
|
|
|
|
size -= 8; |
|
|
|
|
offset = 8; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -1490,7 +1514,7 @@ static void mkv_write_block(AVFormatContext *s, AVIOContext *pb, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
put_ebml_id(pb, blockid); |
|
|
|
|
put_ebml_num(pb, size+4, 0); |
|
|
|
|
put_ebml_num(pb, size + 4, 0); |
|
|
|
|
// this assumes stream_index is less than 126
|
|
|
|
|
avio_w8(pb, 0x80 | (mkv->is_dash ? mkv->dash_track_number : (pkt->stream_index + 1))); |
|
|
|
|
avio_wb16(pb, ts - mkv->cluster_pts); |
|
|
|
@ -1522,14 +1546,19 @@ static int srt_get_duration(uint8_t **buf) |
|
|
|
|
{ |
|
|
|
|
int i, duration = 0; |
|
|
|
|
|
|
|
|
|
for (i=0; i<2 && !duration; i++) { |
|
|
|
|
for (i = 0; i < 2 && !duration; i++) { |
|
|
|
|
int s_hour, s_min, s_sec, s_hsec, e_hour, e_min, e_sec, e_hsec; |
|
|
|
|
if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d", |
|
|
|
|
&s_hour, &s_min, &s_sec, &s_hsec, |
|
|
|
|
&e_hour, &e_min, &e_sec, &e_hsec) == 8) { |
|
|
|
|
s_min += 60*s_hour; e_min += 60*e_hour; |
|
|
|
|
s_sec += 60*s_min; e_sec += 60*e_min; |
|
|
|
|
s_hsec += 1000*s_sec; e_hsec += 1000*e_sec; |
|
|
|
|
s_min += 60 * s_hour; |
|
|
|
|
e_min += 60 * e_hour; |
|
|
|
|
s_sec += 60 * s_min; |
|
|
|
|
|
|
|
|
|
e_sec += 60 * e_min; |
|
|
|
|
s_hsec += 1000 * s_sec; |
|
|
|
|
e_hsec += 1000 * e_sec; |
|
|
|
|
|
|
|
|
|
duration = e_hsec - s_hsec; |
|
|
|
|
} |
|
|
|
|
*buf += ff_subtitles_next_line(*buf); |
|
|
|
@ -1537,7 +1566,8 @@ static int srt_get_duration(uint8_t **buf) |
|
|
|
|
return duration; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int mkv_write_srt_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt) |
|
|
|
|
static int mkv_write_srt_blocks(AVFormatContext *s, AVIOContext *pb, |
|
|
|
|
AVPacket *pkt) |
|
|
|
|
{ |
|
|
|
|
ebml_master blockgroup; |
|
|
|
|
AVPacket pkt2 = *pkt; |
|
|
|
@ -1579,7 +1609,7 @@ static int mkv_write_vtt_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *p |
|
|
|
|
blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(size)); |
|
|
|
|
|
|
|
|
|
put_ebml_id(pb, MATROSKA_ID_BLOCK); |
|
|
|
|
put_ebml_num(pb, size+4, 0); |
|
|
|
|
put_ebml_num(pb, size + 4, 0); |
|
|
|
|
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); |
|
|
|
@ -1609,10 +1639,10 @@ static void mkv_flush_dynbuf(AVFormatContext *s) |
|
|
|
|
static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt, int add_cue) |
|
|
|
|
{ |
|
|
|
|
MatroskaMuxContext *mkv = s->priv_data; |
|
|
|
|
AVIOContext *pb = s->pb; |
|
|
|
|
AVCodecContext *codec = s->streams[pkt->stream_index]->codec; |
|
|
|
|
int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY); |
|
|
|
|
int duration = pkt->duration; |
|
|
|
|
AVIOContext *pb = s->pb; |
|
|
|
|
AVCodecContext *codec = s->streams[pkt->stream_index]->codec; |
|
|
|
|
int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY); |
|
|
|
|
int duration = pkt->duration; |
|
|
|
|
int ret; |
|
|
|
|
int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts; |
|
|
|
|
int64_t relative_packet_pos; |
|
|
|
@ -1637,7 +1667,7 @@ static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt, int add_ |
|
|
|
|
|
|
|
|
|
if (mkv->cluster_pos == -1) { |
|
|
|
|
mkv->cluster_pos = avio_tell(s->pb); |
|
|
|
|
mkv->cluster = start_ebml_master(pb, MATROSKA_ID_CLUSTER, 0); |
|
|
|
|
mkv->cluster = start_ebml_master(pb, MATROSKA_ID_CLUSTER, 0); |
|
|
|
|
put_ebml_uint(pb, MATROSKA_ID_CLUSTERTIMECODE, FFMAX(0, ts)); |
|
|
|
|
mkv->cluster_pts = FFMAX(0, ts); |
|
|
|
|
} |
|
|
|
@ -1665,7 +1695,8 @@ static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt, int add_ |
|
|
|
|
} else if (codec->codec_id == AV_CODEC_ID_WEBVTT) { |
|
|
|
|
duration = mkv_write_vtt_blocks(s, pb, pkt); |
|
|
|
|
} else { |
|
|
|
|
ebml_master blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(pkt->size)); |
|
|
|
|
ebml_master blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, |
|
|
|
|
mkv_blockgroup_size(pkt->size)); |
|
|
|
|
/* For backward compatibility, prefer convergence_duration. */ |
|
|
|
|
if (pkt->convergence_duration > 0) { |
|
|
|
|
duration = pkt->convergence_duration; |
|
|
|
@ -1675,8 +1706,10 @@ static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt, int add_ |
|
|
|
|
end_ebml_master(pb, blockgroup); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts, mkv->cluster_pos, relative_packet_pos, duration); |
|
|
|
|
if (ret < 0) return ret; |
|
|
|
|
ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts, |
|
|
|
|
mkv->cluster_pos, relative_packet_pos, duration); |
|
|
|
|
if (ret < 0) |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
mkv->duration = FFMAX(mkv->duration, ts + duration); |
|
|
|
@ -1730,8 +1763,9 @@ static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (mkv->cluster_pos != -1 && start_new_cluster) { |
|
|
|
|
av_log(s, AV_LOG_DEBUG, "Starting new cluster at offset %" PRIu64 |
|
|
|
|
" bytes, pts %" PRIu64 "dts %" PRIu64 "\n", |
|
|
|
|
av_log(s, AV_LOG_DEBUG, |
|
|
|
|
"Starting new cluster at offset %" PRIu64 " bytes, " |
|
|
|
|
"pts %" PRIu64 "dts %" PRIu64 "\n", |
|
|
|
|
avio_tell(pb), pkt->pts, pkt->dts); |
|
|
|
|
end_ebml_master(pb, mkv->cluster); |
|
|
|
|
mkv->cluster_pos = -1; |
|
|
|
@ -1747,7 +1781,8 @@ static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt) |
|
|
|
|
mkv->is_dash ? start_new_cluster : 0); |
|
|
|
|
av_free_packet(&mkv->cur_audio_pkt); |
|
|
|
|
if (ret < 0) { |
|
|
|
|
av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret); |
|
|
|
|
av_log(s, AV_LOG_ERROR, |
|
|
|
|
"Could not write cached audio packet ret:%d\n", ret); |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -1779,8 +1814,9 @@ static int mkv_write_flush_packet(AVFormatContext *s, AVPacket *pkt) |
|
|
|
|
pb = mkv->dyn_bc; |
|
|
|
|
if (!pkt) { |
|
|
|
|
if (mkv->cluster_pos != -1) { |
|
|
|
|
av_log(s, AV_LOG_DEBUG, "Flushing cluster at offset %" PRIu64 |
|
|
|
|
" bytes\n", avio_tell(pb)); |
|
|
|
|
av_log(s, AV_LOG_DEBUG, |
|
|
|
|
"Flushing cluster at offset %" PRIu64 " bytes\n", |
|
|
|
|
avio_tell(pb)); |
|
|
|
|
end_ebml_master(pb, mkv->cluster); |
|
|
|
|
mkv->cluster_pos = -1; |
|
|
|
|
if (mkv->dyn_bc) |
|
|
|
@ -1804,7 +1840,8 @@ static int mkv_write_trailer(AVFormatContext *s) |
|
|
|
|
ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt, 0); |
|
|
|
|
av_free_packet(&mkv->cur_audio_pkt); |
|
|
|
|
if (ret < 0) { |
|
|
|
|
av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret); |
|
|
|
|
av_log(s, AV_LOG_ERROR, |
|
|
|
|
"Could not write cached audio packet ret:%d\n", ret); |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -1818,7 +1855,8 @@ static int mkv_write_trailer(AVFormatContext *s) |
|
|
|
|
|
|
|
|
|
if (mkv->mode != MODE_WEBM) { |
|
|
|
|
ret = mkv_write_chapters(s); |
|
|
|
|
if (ret < 0) return ret; |
|
|
|
|
if (ret < 0) |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (pb->seekable) { |
|
|
|
@ -1829,25 +1867,29 @@ static int mkv_write_trailer(AVFormatContext *s) |
|
|
|
|
currentpos = avio_tell(pb); |
|
|
|
|
avio_seek(pb, mkv->cues_pos, SEEK_SET); |
|
|
|
|
|
|
|
|
|
cuespos = mkv_write_cues(s, mkv->cues, mkv->tracks, s->nb_streams); |
|
|
|
|
cuespos = mkv_write_cues(s, mkv->cues, mkv->tracks, s->nb_streams); |
|
|
|
|
cues_end = avio_tell(pb); |
|
|
|
|
if (cues_end > cuespos + mkv->reserve_cues_space) { |
|
|
|
|
av_log(s, AV_LOG_ERROR, "Insufficient space reserved for cues: %d " |
|
|
|
|
"(needed: %"PRId64").\n", mkv->reserve_cues_space, |
|
|
|
|
cues_end - cuespos); |
|
|
|
|
av_log(s, AV_LOG_ERROR, |
|
|
|
|
"Insufficient space reserved for cues: %d " |
|
|
|
|
"(needed: %" PRId64 ").\n", |
|
|
|
|
mkv->reserve_cues_space, cues_end - cuespos); |
|
|
|
|
return AVERROR(EINVAL); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (cues_end < cuespos + mkv->reserve_cues_space) |
|
|
|
|
put_ebml_void(pb, mkv->reserve_cues_space - (cues_end - cuespos)); |
|
|
|
|
put_ebml_void(pb, mkv->reserve_cues_space - |
|
|
|
|
(cues_end - cuespos)); |
|
|
|
|
|
|
|
|
|
avio_seek(pb, currentpos, SEEK_SET); |
|
|
|
|
} else { |
|
|
|
|
cuespos = mkv_write_cues(s, mkv->cues, mkv->tracks, s->nb_streams); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CUES, cuespos); |
|
|
|
|
if (ret < 0) return ret; |
|
|
|
|
ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CUES, |
|
|
|
|
cuespos); |
|
|
|
|
if (ret < 0) |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
mkv_write_seekhead(pb, mkv->main_seekhead); |
|
|
|
@ -1876,8 +1918,9 @@ static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance) |
|
|
|
|
if (ff_mkv_codec_tags[i].id == codec_id) |
|
|
|
|
return 1; |
|
|
|
|
|
|
|
|
|
if (std_compliance < FF_COMPLIANCE_NORMAL) { // mkv theoretically supports any
|
|
|
|
|
enum AVMediaType type = avcodec_get_type(codec_id); // video/audio through VFW/ACM
|
|
|
|
|
if (std_compliance < FF_COMPLIANCE_NORMAL) { |
|
|
|
|
enum AVMediaType type = avcodec_get_type(codec_id); |
|
|
|
|
// mkv theoretically supports any video/audio through VFW/ACM
|
|
|
|
|
if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO) |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|