Commit 8365287e authored by Michael Niedermayer's avatar Michael Niedermayer

Merge commit 'f1f6156b'

* commit 'f1f6156b':
  matroska: K&R formatting cosmetics

Conflicts:
	libavformat/matroskaenc.c
Merged-by: 's avatarMichael Niedermayer <michaelni@gmx.at>
parents 06e5d28e f1f6156b
...@@ -134,22 +134,21 @@ typedef struct MatroskaMuxContext { ...@@ -134,22 +134,21 @@ typedef struct MatroskaMuxContext {
#define MAX_CUETRACKPOS_SIZE 42 #define MAX_CUETRACKPOS_SIZE 42
/** per-cuepoint - 2 1-byte EBML IDs, 2 1-byte EBML sizes, 8-byte uint max */ /** 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 */ /** Seek preroll value for opus */
#define OPUS_SEEK_PREROLL 80000000 #define OPUS_SEEK_PREROLL 80000000
static int ebml_id_size(unsigned int id) 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) static void put_ebml_id(AVIOContext *pb, unsigned int id)
{ {
int i = ebml_id_size(id); int i = ebml_id_size(id);
while (i--) 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) ...@@ -170,7 +169,8 @@ static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
static int ebml_num_size(uint64_t num) static int ebml_num_size(uint64_t num)
{ {
int bytes = 1; int bytes = 1;
while ((num+1) >> bytes*7) bytes++; while ((num + 1) >> bytes * 7)
bytes++;
return bytes; return bytes;
} }
...@@ -185,7 +185,7 @@ static void put_ebml_num(AVIOContext *pb, uint64_t num, int 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); int i, needed_bytes = ebml_num_size(num);
// sizes larger than this are currently undefined in EBML // sizes larger than this are currently undefined in EBML
av_assert0(num < (1ULL<<56)-1); av_assert0(num < (1ULL << 56) - 1);
if (bytes == 0) if (bytes == 0)
// don't care how many bytes are used, so use the min // 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) ...@@ -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. // that we need to use, so write unknown size. This shouldn't happen.
av_assert0(bytes >= needed_bytes); av_assert0(bytes >= needed_bytes);
num |= 1ULL << bytes*7; num |= 1ULL << bytes * 7;
for (i = bytes - 1; i >= 0; i--) 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) static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val)
{ {
int i, bytes = 1; int i, bytes = 1;
uint64_t tmp = val; uint64_t tmp = val;
while (tmp>>=8) bytes++; while (tmp >>= 8)
bytes++;
put_ebml_id(pb, elementid); put_ebml_id(pb, elementid);
put_ebml_num(pb, bytes, 0); put_ebml_num(pb, bytes, 0);
for (i = bytes - 1; i >= 0; i--) 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) 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) ...@@ -221,7 +222,7 @@ static void put_ebml_sint(AVIOContext *pb, unsigned int elementid, int64_t val)
put_ebml_id(pb, elementid); put_ebml_id(pb, elementid);
put_ebml_num(pb, bytes, 0); put_ebml_num(pb, bytes, 0);
for (i = bytes - 1; i >= 0; i--) 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) 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, ...@@ -239,7 +240,8 @@ static void put_ebml_binary(AVIOContext *pb, unsigned int elementid,
avio_write(pb, buf, size); 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)); put_ebml_binary(pb, elementid, str, strlen(str));
} }
...@@ -261,18 +263,19 @@ static void put_ebml_void(AVIOContext *pb, uint64_t size) ...@@ -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 we need to reserve so 2 cases, we use 8 bytes to store the
// size if possible, 1 byte otherwise // size if possible, 1 byte otherwise
if (size < 10) if (size < 10)
put_ebml_num(pb, size-1, 0); put_ebml_num(pb, size - 1, 0);
else else
put_ebml_num(pb, size-9, 8); put_ebml_num(pb, size - 9, 8);
ffio_fill(pb, 0, currentpos + size - avio_tell(pb)); 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; int bytes = expectedsize ? ebml_num_size(expectedsize) : 8;
put_ebml_id(pb, elementid); put_ebml_id(pb, elementid);
put_ebml_size_unknown(pb, bytes); 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) static void end_ebml_master(AVIOContext *pb, ebml_master master)
...@@ -302,7 +305,8 @@ static void put_xiph_size(AVIOContext *pb, int size) ...@@ -302,7 +305,8 @@ static void put_xiph_size(AVIOContext *pb, int size)
* @param numelements The maximum number of elements that will be indexed * @param numelements The maximum number of elements that will be indexed
* by this seek head, 0 if unlimited. * 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)); mkv_seekhead *new_seekhead = av_mallocz(sizeof(mkv_seekhead));
if (new_seekhead == NULL) if (new_seekhead == NULL)
...@@ -394,7 +398,7 @@ fail: ...@@ -394,7 +398,7 @@ fail:
return currentpos; 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)); mkv_cues *cues = av_mallocz(sizeof(mkv_cues));
if (cues == NULL) if (cues == NULL)
...@@ -552,13 +556,15 @@ static int put_flac_codecpriv(AVFormatContext *s, ...@@ -552,13 +556,15 @@ static int put_flac_codecpriv(AVFormatContext *s,
return 0; 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; MPEG4AudioConfig mp4ac;
if (avpriv_mpeg4audio_get_config(&mp4ac, codec->extradata, if (avpriv_mpeg4audio_get_config(&mp4ac, codec->extradata,
codec->extradata_size * 8, 1) < 0) { 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; return;
} }
...@@ -566,27 +572,32 @@ static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int ...@@ -566,27 +572,32 @@ static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int
*output_sample_rate = mp4ac.ext_sample_rate; *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; AVIOContext *dyn_cp;
uint8_t *codecpriv; uint8_t *codecpriv;
int ret, codecpriv_size; int ret, codecpriv_size;
ret = avio_open_dyn_buf(&dyn_cp); ret = avio_open_dyn_buf(&dyn_cp);
if(ret < 0) if (ret < 0)
return ret; return ret;
if (native_id) { 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); ret = put_xiph_codecpriv(s, dyn_cp, codec);
else if (codec->codec_id == AV_CODEC_ID_FLAC) else if (codec->codec_id == AV_CODEC_ID_FLAC)
ret = put_flac_codecpriv(s, dyn_cp, codec); ret = put_flac_codecpriv(s, dyn_cp, codec);
else if (codec->codec_id == AV_CODEC_ID_WAVPACK) else if (codec->codec_id == AV_CODEC_ID_WAVPACK)
ret = put_wv_codecpriv(dyn_cp, codec); ret = put_wv_codecpriv(dyn_cp, codec);
else if (codec->codec_id == AV_CODEC_ID_H264) 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) 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) { else if (codec->codec_id == AV_CODEC_ID_ALAC) {
if (codec->extradata_size < 36) { if (codec->extradata_size < 36) {
av_log(s, AV_LOG_ERROR, av_log(s, AV_LOG_ERROR,
...@@ -599,21 +610,21 @@ static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecCo ...@@ -599,21 +610,21 @@ static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecCo
} else if (codec->codec_id == AV_CODEC_ID_PRORES && } else if (codec->codec_id == AV_CODEC_ID_PRORES &&
ff_codec_get_id(ff_codec_movvideo_tags, codec->codec_tag) == 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); 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); avio_write(dyn_cp, codec->extradata, codec->extradata_size);
} else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) { } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
if (qt_id) { if (qt_id) {
if (!codec->codec_tag) 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 (codec->extradata_size) {
if ( ff_codec_get_id(ff_codec_movvideo_tags, codec->codec_tag) == codec->codec_id 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; int i;
avio_wb32(dyn_cp, 0x5a + codec->extradata_size); avio_wb32(dyn_cp, 0x5a + codec->extradata_size);
avio_wl32(dyn_cp, codec->codec_tag); 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_w8(dyn_cp, 0);
} }
avio_write(dyn_cp, codec->extradata, codec->extradata_size); avio_write(dyn_cp, codec->extradata, codec->extradata_size);
...@@ -624,7 +635,8 @@ static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecCo ...@@ -624,7 +635,8 @@ static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecCo
avcodec_get_name(codec->codec_id)); avcodec_get_name(codec->codec_id));
if (!codec->codec_tag) 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) { if (!codec->codec_tag) {
av_log(s, AV_LOG_ERROR, "No bmp codec tag found for codec %s\n", av_log(s, AV_LOG_ERROR, "No bmp codec tag found for codec %s\n",
avcodec_get_name(codec->codec_id)); avcodec_get_name(codec->codec_id));
...@@ -633,7 +645,6 @@ static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecCo ...@@ -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); ff_put_bmp_header(dyn_cp, codec, ff_codec_bmp_tags, 0, 0);
} }
} else if (codec->codec_type == AVMEDIA_TYPE_AUDIO) { } else if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
unsigned int tag; unsigned int tag;
tag = ff_codec_get_tag(ff_codec_wav_tags, codec->codec_id); 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 ...@@ -650,7 +661,8 @@ static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecCo
codecpriv_size = avio_close_dyn_buf(dyn_cp, &codecpriv); codecpriv_size = avio_close_dyn_buf(dyn_cp, &codecpriv);
if (codecpriv_size) 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); av_free(codecpriv);
return ret; return ret;
} }
...@@ -663,7 +675,8 @@ static int mkv_write_tracks(AVFormatContext *s) ...@@ -663,7 +675,8 @@ static int mkv_write_tracks(AVFormatContext *s)
int i, j, ret, default_stream_exists = 0; int i, j, ret, default_stream_exists = 0;
ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_TRACKS, avio_tell(pb)); 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); tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS, 0);
for (i = 0; i < s->nb_streams; i++) { for (i = 0; i < s->nb_streams; i++) {
...@@ -786,10 +799,10 @@ static int mkv_write_tracks(AVFormatContext *s) ...@@ -786,10 +799,10 @@ static int mkv_write_tracks(AVFormatContext *s)
if (!native_id && if (!native_id &&
ff_codec_get_tag(ff_codec_movvideo_tags, codec->codec_id) && ff_codec_get_tag(ff_codec_movvideo_tags, codec->codec_id) &&
(!ff_codec_get_tag(ff_codec_bmp_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_SVQ1 ||
|| codec->codec_id == AV_CODEC_ID_SVQ3 codec->codec_id == AV_CODEC_ID_SVQ3 ||
|| codec->codec_id == AV_CODEC_ID_CINEPAK)) codec->codec_id == AV_CODEC_ID_CINEPAK))
qt_id = 1; qt_id = 1;
if (qt_id) if (qt_id)
...@@ -901,7 +914,8 @@ static int mkv_write_tracks(AVFormatContext *s) ...@@ -901,7 +914,8 @@ static int mkv_write_tracks(AVFormatContext *s)
if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT) { if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT) {
ret = mkv_write_codecprivate(s, pb, codec, native_id, qt_id); 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); end_ebml_master(pb, track);
...@@ -1153,8 +1167,10 @@ static int mkv_write_header(AVFormatContext *s) ...@@ -1153,8 +1167,10 @@ static int mkv_write_header(AVFormatContext *s)
AVDictionaryEntry *tag; AVDictionaryEntry *tag;
int ret, i, version = 2; int ret, i, version = 2;
if (!strcmp(s->oformat->name, "webm")) mkv->mode = MODE_WEBM; if (!strcmp(s->oformat->name, "webm"))
else mkv->mode = MODE_MATROSKAv2; mkv->mode = MODE_WEBM;
else
mkv->mode = MODE_MATROSKAv2;
if (s->avoid_negative_ts < 0) if (s->avoid_negative_ts < 0)
s->avoid_negative_ts = 1; s->avoid_negative_ts = 1;
...@@ -1224,7 +1240,7 @@ static int mkv_write_header(AVFormatContext *s) ...@@ -1224,7 +1240,7 @@ static int mkv_write_header(AVFormatContext *s)
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
segment_uid[i] = av_lfg_get(&lfg); 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))) if ((tag = av_dict_get(s->metadata, "encoding_tool", NULL, 0)))
put_ebml_string(pb, MATROSKA_ID_WRITINGAPP, tag->value); put_ebml_string(pb, MATROSKA_ID_WRITINGAPP, tag->value);
else else
...@@ -1251,20 +1267,24 @@ static int mkv_write_header(AVFormatContext *s) ...@@ -1251,20 +1267,24 @@ static int mkv_write_header(AVFormatContext *s)
end_ebml_master(pb, segment_info); end_ebml_master(pb, segment_info);
ret = mkv_write_tracks(s); ret = mkv_write_tracks(s);
if (ret < 0) return ret; if (ret < 0)
return ret;
for (i = 0; i < s->nb_chapters; i++) for (i = 0; i < s->nb_chapters; i++)
mkv->chapter_id_offset = FFMAX(mkv->chapter_id_offset, 1LL - s->chapters[i]->id); mkv->chapter_id_offset = FFMAX(mkv->chapter_id_offset, 1LL - s->chapters[i]->id);
if (mkv->mode != MODE_WEBM) { if (mkv->mode != MODE_WEBM) {
ret = mkv_write_chapters(s); ret = mkv_write_chapters(s);
if (ret < 0) return ret; if (ret < 0)
return ret;
ret = mkv_write_tags(s); ret = mkv_write_tags(s);
if (ret < 0) return ret; if (ret < 0)
return ret;
ret = mkv_write_attachments(s); ret = mkv_write_attachments(s);
if (ret < 0) return ret; if (ret < 0)
return ret;
} }
if (!s->pb->seekable) if (!s->pb->seekable)
...@@ -1321,8 +1341,8 @@ static int ass_get_duration(const uint8_t *p) ...@@ -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", if (sscanf(p, "%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d",
&sh, &sm, &ss, &sc, &eh, &em, &es, &ec) != 8) &sh, &sm, &ss, &sc, &eh, &em, &es, &ec) != 8)
return 0; return 0;
start = 3600000LL*sh + 60000LL*sm + 1000LL*ss + 10LL*sc; start = 3600000LL * sh + 60000LL * sm + 1000LL * ss + 10LL * sc;
end = 3600000LL*eh + 60000LL*em + 1000LL*es + 10LL*ec; end = 3600000LL * eh + 60000LL * em + 1000LL * es + 10LL * ec;
return end - start; return end - start;
} }
...@@ -1332,7 +1352,8 @@ static int ass_get_duration(const uint8_t *p) ...@@ -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 * (starting from *datap) and updates *datap so it points to the beginning of the data
* corresponding to the next block. * 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; MatroskaMuxContext *mkv = s->priv_data;
int i, layer = 0, size, line_size, data_size = pkt->size - (*datap - pkt->data); int i, layer = 0, size, line_size, data_size = pkt->size - (*datap - pkt->data);
...@@ -1342,26 +1363,29 @@ static int mkv_write_ass_block(AVFormatContext *s, AVIOContext *pb, AVPacket *pk ...@@ -1342,26 +1363,29 @@ static int mkv_write_ass_block(AVFormatContext *s, AVIOContext *pb, AVPacket *pk
int duration = ass_get_duration(data); int duration = ass_get_duration(data);
end = memchr(data, '\n', data_size); end = memchr(data, '\n', data_size);
size = line_size = end ? end-data+1 : data_size; size = line_size = end ? end - data + 1 : data_size;
size -= end ? (end[-1]=='\r')+1 : 0; size -= end ? (end[-1] == '\r') + 1 : 0;
start = data; start = data;
for (i=0; i<3; i++, start++) for (i = 0; i < 3; i++, start++)
if (!(start = memchr(start, ',', size-(start-data)))) if (!(start = memchr(start, ',', size - (start - data))))
return duration; return duration;
size -= start - data; size -= start - data;
sscanf(data, "Dialogue: %d,", &layer); 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); s->streams[pkt->stream_index]->nb_frames, layer);
size = FFMIN(i+size, sizeof(buffer)); size = FFMIN(i + size, sizeof(buffer));
memcpy(buffer+i, start, size-i); 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", "pts %" PRId64 ", duration %d\n",
avio_tell(pb), size, pkt->pts, duration); 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_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 // 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_wb16(pb, pkt->pts - mkv->cluster_pts);
avio_w8(pb, 0); avio_w8(pb, 0);
avio_write(pb, buffer, size); avio_write(pb, buffer, size);
...@@ -1490,7 +1514,7 @@ static void mkv_write_block(AVFormatContext *s, AVIOContext *pb, ...@@ -1490,7 +1514,7 @@ static void mkv_write_block(AVFormatContext *s, AVIOContext *pb,
} }
put_ebml_id(pb, blockid); 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 // this assumes stream_index is less than 126
avio_w8(pb, 0x80 | (mkv->is_dash ? mkv->dash_track_number : (pkt->stream_index + 1))); avio_w8(pb, 0x80 | (mkv->is_dash ? mkv->dash_track_number : (pkt->stream_index + 1)));
avio_wb16(pb, ts - mkv->cluster_pts); avio_wb16(pb, ts - mkv->cluster_pts);
...@@ -1522,14 +1546,19 @@ static int srt_get_duration(uint8_t **buf) ...@@ -1522,14 +1546,19 @@ static int srt_get_duration(uint8_t **buf)
{ {
int i, duration = 0; 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; 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", if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d",
&s_hour, &s_min, &s_sec, &s_hsec, &s_hour, &s_min, &s_sec, &s_hsec,
&e_hour, &e_min, &e_sec, &e_hsec) == 8) { &e_hour, &e_min, &e_sec, &e_hsec) == 8) {
s_min += 60*s_hour; e_min += 60*e_hour; s_min += 60 * s_hour;
s_sec += 60*s_min; e_sec += 60*e_min; e_min += 60 * e_hour;
s_hsec += 1000*s_sec; e_hsec += 1000*e_sec; 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; duration = e_hsec - s_hsec;
} }
*buf += ff_subtitles_next_line(*buf); *buf += ff_subtitles_next_line(*buf);
...@@ -1537,7 +1566,8 @@ static int srt_get_duration(uint8_t **buf) ...@@ -1537,7 +1566,8 @@ static int srt_get_duration(uint8_t **buf)
return duration; 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; ebml_master blockgroup;
AVPacket pkt2 = *pkt; AVPacket pkt2 = *pkt;
...@@ -1579,7 +1609,7 @@ static int mkv_write_vtt_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *p ...@@ -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)); blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(size));
put_ebml_id(pb, MATROSKA_ID_BLOCK); 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_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
avio_wb16(pb, ts - mkv->cluster_pts); avio_wb16(pb, ts - mkv->cluster_pts);
avio_w8(pb, flags); avio_w8(pb, flags);
...@@ -1665,7 +1695,8 @@ static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt, int add_ ...@@ -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) { } else if (codec->codec_id == AV_CODEC_ID_WEBVTT) {
duration = mkv_write_vtt_blocks(s, pb, pkt); duration = mkv_write_vtt_blocks(s, pb, pkt);
} else { } 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. */ /* For backward compatibility, prefer convergence_duration. */
if (pkt->convergence_duration > 0) { if (pkt->convergence_duration > 0) {
duration = pkt->convergence_duration; duration = pkt->convergence_duration;
...@@ -1675,8 +1706,10 @@ static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt, int add_ ...@@ -1675,8 +1706,10 @@ static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt, int add_
end_ebml_master(pb, blockgroup); end_ebml_master(pb, blockgroup);
} }
ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts, mkv->cluster_pos, relative_packet_pos, duration); ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts,
if (ret < 0) return ret; mkv->cluster_pos, relative_packet_pos, duration);
if (ret < 0)
return ret;
} }
mkv->duration = FFMAX(mkv->duration, ts + duration); mkv->duration = FFMAX(mkv->duration, ts + duration);
...@@ -1730,8 +1763,9 @@ static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt) ...@@ -1730,8 +1763,9 @@ static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
} }
if (mkv->cluster_pos != -1 && start_new_cluster) { if (mkv->cluster_pos != -1 && start_new_cluster) {
av_log(s, AV_LOG_DEBUG, "Starting new cluster at offset %" PRIu64 av_log(s, AV_LOG_DEBUG,
" bytes, pts %" PRIu64 "dts %" PRIu64 "\n", "Starting new cluster at offset %" PRIu64 " bytes, "
"pts %" PRIu64 "dts %" PRIu64 "\n",
avio_tell(pb), pkt->pts, pkt->dts); avio_tell(pb), pkt->pts, pkt->dts);
end_ebml_master(pb, mkv->cluster); end_ebml_master(pb, mkv->cluster);
mkv->cluster_pos = -1; mkv->cluster_pos = -1;
...@@ -1747,7 +1781,8 @@ static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt) ...@@ -1747,7 +1781,8 @@ static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
mkv->is_dash ? start_new_cluster : 0); mkv->is_dash ? start_new_cluster : 0);
av_free_packet(&mkv->cur_audio_pkt); av_free_packet(&mkv->cur_audio_pkt);
if (ret < 0) { 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; return ret;
} }
} }
...@@ -1779,8 +1814,9 @@ static int mkv_write_flush_packet(AVFormatContext *s, AVPacket *pkt) ...@@ -1779,8 +1814,9 @@ static int mkv_write_flush_packet(AVFormatContext *s, AVPacket *pkt)
pb = mkv->dyn_bc; pb = mkv->dyn_bc;
if (!pkt) { if (!pkt) {
if (mkv->cluster_pos != -1) { if (mkv->cluster_pos != -1) {
av_log(s, AV_LOG_DEBUG, "Flushing cluster at offset %" PRIu64 av_log(s, AV_LOG_DEBUG,
" bytes\n", avio_tell(pb)); "Flushing cluster at offset %" PRIu64 " bytes\n",
avio_tell(pb));
end_ebml_master(pb, mkv->cluster); end_ebml_master(pb, mkv->cluster);
mkv->cluster_pos = -1; mkv->cluster_pos = -1;
if (mkv->dyn_bc) if (mkv->dyn_bc)
...@@ -1804,7 +1840,8 @@ static int mkv_write_trailer(AVFormatContext *s) ...@@ -1804,7 +1840,8 @@ static int mkv_write_trailer(AVFormatContext *s)
ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt, 0); ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt, 0);
av_free_packet(&mkv->cur_audio_pkt); av_free_packet(&mkv->cur_audio_pkt);
if (ret < 0) { 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; return ret;
} }
} }
...@@ -1818,7 +1855,8 @@ static int mkv_write_trailer(AVFormatContext *s) ...@@ -1818,7 +1855,8 @@ static int mkv_write_trailer(AVFormatContext *s)
if (mkv->mode != MODE_WEBM) { if (mkv->mode != MODE_WEBM) {
ret = mkv_write_chapters(s); ret = mkv_write_chapters(s);
if (ret < 0) return ret; if (ret < 0)
return ret;
} }
if (pb->seekable) { if (pb->seekable) {
...@@ -1832,22 +1870,26 @@ static int mkv_write_trailer(AVFormatContext *s) ...@@ -1832,22 +1870,26 @@ static int mkv_write_trailer(AVFormatContext *s)
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); cues_end = avio_tell(pb);
if (cues_end > cuespos + mkv->reserve_cues_space) { if (cues_end > cuespos + mkv->reserve_cues_space) {
av_log(s, AV_LOG_ERROR, "Insufficient space reserved for cues: %d " av_log(s, AV_LOG_ERROR,
"(needed: %"PRId64").\n", mkv->reserve_cues_space, "Insufficient space reserved for cues: %d "
cues_end - cuespos); "(needed: %" PRId64 ").\n",
mkv->reserve_cues_space, cues_end - cuespos);
return AVERROR(EINVAL); return AVERROR(EINVAL);
} }
if (cues_end < cuespos + mkv->reserve_cues_space) 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); avio_seek(pb, currentpos, SEEK_SET);
} else { } else {
cuespos = mkv_write_cues(s, mkv->cues, mkv->tracks, s->nb_streams); cuespos = mkv_write_cues(s, mkv->cues, mkv->tracks, s->nb_streams);
} }
ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CUES, cuespos); ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CUES,
if (ret < 0) return ret; cuespos);
if (ret < 0)
return ret;
} }
mkv_write_seekhead(pb, mkv->main_seekhead); mkv_write_seekhead(pb, mkv->main_seekhead);
...@@ -1876,8 +1918,9 @@ static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance) ...@@ -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) if (ff_mkv_codec_tags[i].id == codec_id)
return 1; return 1;
if (std_compliance < FF_COMPLIANCE_NORMAL) { // mkv theoretically supports any if (std_compliance < FF_COMPLIANCE_NORMAL) {
enum AVMediaType type = avcodec_get_type(codec_id); // video/audio through VFW/ACM 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) if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)
return 1; return 1;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment