This is possible by using a dynamic buffer to write them; said dynamic buffer is (re)used and reset as appropriate.
Showing the changes to libavformat (1 of 26 files). Show all
Unified · Split
| @@ -216,6 +216,13 @@ typedef struct MatroskaMuxContext { · 215 unmodified lines | ||
| 216 | 216 | |
| 217 | 217 | BlockContext cur_block; |
| 218 | 218 | |
| 219 | + /* Used as temporary buffer to use the minimal amount of bytes | |
| 220 | + * to write the length field of EBML Masters. | |
| 221 | + * Every user has to reset the buffer after using it and | |
| 222 | + * different uses may not overlap. It is currently used in | |
| 223 | + * mkv_write_tag(). */ | |
| 224 | + AVIOContext *tmp_bc; | |
| 225 | + | |
| 219 | 226 | AVPacket *cur_audio_pkt; |
| 220 | 227 | |
| 221 | 228 | unsigned nb_attachments; |
| @@ -247,6 +254,9 @@ typedef struct MatroskaMuxContext { · 25 unmodified lines | ||
| 247 | 254 | /** 4 * (1-byte EBML ID, 1-byte EBML size, 8-byte uint max) */ |
| 248 | 255 | #define MAX_CUETRACKPOS_SIZE 40 |
| 249 | 256 | |
| 257 | +/** 2 + 1 Simpletag header, 2 + 1 + 8 Name "DURATION", 23B for TagString */ | |
| 258 | +#define DURATION_SIMPLETAG_SIZE (2 + 1 + (2 + 1 + 8) + 23) | |
| 259 | + | |
| 250 | 260 | /** Seek preroll value for opus */ |
| 251 | 261 | #define OPUS_SEEK_PREROLL 80000000 |
| 252 | 262 | |
| @@ -824,6 +834,7 @@ static void mkv_deinit(AVFormatContext *s) · 571 unmodified lines | ||
| 824 | 834 | ffio_free_dyn_buf(&mkv->info.bc); |
| 825 | 835 | ffio_free_dyn_buf(&mkv->track.bc); |
| 826 | 836 | ffio_free_dyn_buf(&mkv->tags.bc); |
| 837 | + ffio_free_dyn_buf(&mkv->tmp_bc); | |
| 827 | 838 | |
| 828 | 839 | av_freep(&mkv->cur_block.h2645_nalu_list.nalus); |
| 829 | 840 | av_freep(&mkv->cues.entries); |
| @@ -1921,24 +1932,14 @@ static int mkv_write_simpletag(AVIOContext *pb, const AVDictionaryEntry *t) · 1091 unmodified lines | ||
| 1921 | 1932 | return ret; |
| 1922 | 1933 | } |
| 1923 | 1934 | |
| 1924 | −static int mkv_write_tag_targets(MatroskaMuxContext *mkv, AVIOContext **pb, | |
| 1925 | − ebml_master *tag, uint32_t elementid, uint64_t uid) | |
| 1935 | +static void mkv_write_tag_targets(MatroskaMuxContext *mkv, AVIOContext *pb, | |
| 1936 | + uint32_t elementid, uint64_t uid) | |
| 1926 | 1937 | { |
| 1927 | − ebml_master targets; | |
| 1928 | − int ret; | |
| 1929 | − | |
| 1930 | − if (!*pb) { | |
| 1931 | − ret = start_ebml_master_crc32(pb, mkv); | |
| 1932 | − if (ret < 0) | |
| 1933 | − return ret; | |
| 1934 | − } | |
| 1935 | − | |
| 1936 | − *tag = start_ebml_master(*pb, MATROSKA_ID_TAG, 0); | |
| 1937 | − targets = start_ebml_master(*pb, MATROSKA_ID_TAGTARGETS, 4 + 1 + 8); | |
| 1938 | + ebml_master targets = start_ebml_master(pb, MATROSKA_ID_TAGTARGETS, | |
| 1939 | + 4 + 1 + 8); | |
| 1938 | 1940 | if (elementid) |
| 1939 | − put_ebml_uid(*pb, elementid, uid); | |
| 1940 | − end_ebml_master(*pb, targets); | |
| 1941 | − return 0; | |
| 1941 | + put_ebml_uid(pb, elementid, uid); | |
| 1942 | + end_ebml_master(pb, targets); | |
| 1942 | 1943 | } |
| 1943 | 1944 | |
| 1944 | 1945 | static int mkv_check_tag_name(const char *name, uint32_t elementid) |
| @@ -1956,29 +1957,41 @@ static int mkv_check_tag_name(const char *name, uint32_t elementid) · 11 unmodified lines | ||
| 1956 | 1957 | } |
| 1957 | 1958 | |
| 1958 | 1959 | static int mkv_write_tag(MatroskaMuxContext *mkv, const AVDictionary *m, |
| 1959 | − AVIOContext **pb, ebml_master *tag, | |
| 1960 | + AVIOContext **pb, unsigned reserved_size, | |
| 1960 | 1961 | uint32_t elementid, uint64_t uid) |
| 1961 | 1962 | { |
| 1962 | 1963 | const AVDictionaryEntry *t = NULL; |
| 1963 | − ebml_master tag2; | |
| 1964 | − int ret; | |
| 1964 | + AVIOContext *const tmp_bc = mkv->tmp_bc; | |
| 1965 | + uint8_t *buf; | |
| 1966 | + int ret, size; | |
| 1965 | 1967 | |
| 1966 | − ret = mkv_write_tag_targets(mkv, pb, tag ? tag : &tag2, elementid, uid); | |
| 1967 | − if (ret < 0) | |
| 1968 | − return ret; | |
| 1968 | + mkv_write_tag_targets(mkv, tmp_bc, elementid, uid); | |
| 1969 | 1969 | |
| 1970 | 1970 | while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) { |
| 1971 | 1971 | if (mkv_check_tag_name(t->key, elementid)) { |
| 1972 | − ret = mkv_write_simpletag(*pb, t); | |
| 1972 | + ret = mkv_write_simpletag(tmp_bc, t); | |
| 1973 | 1973 | if (ret < 0) |
| 1974 | − return ret; | |
| 1974 | + goto end; | |
| 1975 | 1975 | } |
| 1976 | 1976 | } |
| 1977 | + if (reserved_size) | |
| 1978 | + put_ebml_void(tmp_bc, reserved_size); | |
| 1977 | 1979 | |
| 1978 | − if (!tag) | |
| 1979 | − end_ebml_master(*pb, tag2); | |
| 1980 | + size = avio_get_dyn_buf(tmp_bc, &buf); | |
| 1981 | + if (tmp_bc->error) { | |
| 1982 | + ret = tmp_bc->error; | |
| 1983 | + goto end; | |
| 1984 | + } | |
| 1985 | + if (!*pb) { | |
| 1986 | + ret = start_ebml_master_crc32(pb, mkv); | |
| 1987 | + if (ret < 0) | |
| 1988 | + goto end; | |
| 1989 | + } | |
| 1990 | + put_ebml_binary(*pb, MATROSKA_ID_TAG, buf, size); | |
| 1980 | 1991 | |
| 1981 | − return 0; | |
| 1992 | +end: | |
| 1993 | + ffio_reset_dyn_buf(tmp_bc); | |
| 1994 | + return ret; | |
| 1982 | 1995 | } |
| 1983 | 1996 | |
| 1984 | 1997 | static int mkv_check_tag(const AVDictionary *m, uint32_t elementid) |
| @@ -1995,15 +2008,14 @@ static int mkv_check_tag(const AVDictionary *m, uint32_t elementid) · 10 unmodified lines | ||
| 1995 | 2008 | static int mkv_write_tags(AVFormatContext *s) |
| 1996 | 2009 | { |
| 1997 | 2010 | MatroskaMuxContext *mkv = s->priv_data; |
| 1998 | − ebml_master tag, *tagp = IS_SEEKABLE(s->pb, mkv) ? &tag : NULL; | |
| 1999 | − int i, ret; | |
| 2011 | + int i, ret, seekable = IS_SEEKABLE(s->pb, mkv); | |
| 2000 | 2012 | |
| 2001 | 2013 | mkv->wrote_tags = 1; |
| 2002 | 2014 | |
| 2003 | 2015 | ff_metadata_conv_ctx(s, ff_mkv_metadata_conv, NULL); |
| 2004 | 2016 | |
| 2005 | 2017 | if (mkv_check_tag(s->metadata, 0)) { |
| 2006 | − ret = mkv_write_tag(mkv, s->metadata, &mkv->tags.bc, NULL, 0, 0); | |
| 2018 | + ret = mkv_write_tag(mkv, s->metadata, &mkv->tags.bc, 0, 0, 0); | |
| 2007 | 2019 | if (ret < 0) |
| 2008 | 2020 | return ret; |
| 2009 | 2021 | } |
| @@ -2015,29 +2027,16 @@ static int mkv_write_tags(AVFormatContext *s) · 5 unmodified lines | ||
| 2015 | 2027 | if (st->codecpar->codec_type == AVMEDIA_TYPE_ATTACHMENT) |
| 2016 | 2028 | continue; |
| 2017 | 2029 | |
| 2018 | − if (!tagp && !mkv_check_tag(st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID)) | |
| 2030 | + if (!seekable && !mkv_check_tag(st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID)) | |
| 2019 | 2031 | continue; |
| 2020 | 2032 | |
| 2021 | − ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc, tagp, | |
| 2033 | + ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc, | |
| 2034 | + seekable ? DURATION_SIMPLETAG_SIZE : 0, | |
| 2022 | 2035 | MATROSKA_ID_TAGTARGETS_TRACKUID, track->uid); |
| 2023 | 2036 | if (ret < 0) |
| 2024 | 2037 | return ret; |
| 2025 | − | |
| 2026 | − if (tagp) { | |
| 2027 | − AVIOContext *pb = mkv->tags.bc; | |
| 2028 | − ebml_master simpletag; | |
| 2029 | − | |
| 2030 | − simpletag = start_ebml_master(pb, MATROSKA_ID_SIMPLETAG, | |
| 2031 | − 2 + 1 + 8 + 23); | |
| 2032 | − put_ebml_string(pb, MATROSKA_ID_TAGNAME, "DURATION"); | |
| 2033 | − track->duration_offset = avio_tell(pb); | |
| 2034 | − | |
| 2035 | − // Reserve space to write duration as a 20-byte string. | |
| 2036 | − // 2 (ebml id) + 1 (data size) + 20 (data) | |
| 2037 | − put_ebml_void(pb, 23); | |
| 2038 | − end_ebml_master(pb, simpletag); | |
| 2039 | − end_ebml_master(pb, tag); | |
| 2040 | − } | |
| 2038 | + if (seekable) | |
| 2039 | + track->duration_offset = avio_tell(mkv->tags.bc) - DURATION_SIMPLETAG_SIZE; | |
| 2041 | 2040 | } |
| 2042 | 2041 | |
| 2043 | 2042 | if (mkv->nb_attachments && !IS_WEBM(mkv)) { |
| @@ -2051,7 +2050,7 @@ static int mkv_write_tags(AVFormatContext *s) · 7 unmodified lines | ||
| 2051 | 2050 | if (!mkv_check_tag(st->metadata, MATROSKA_ID_TAGTARGETS_ATTACHUID)) |
| 2052 | 2051 | continue; |
| 2053 | 2052 | |
| 2054 | − ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc, NULL, | |
| 2053 | + ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc, 0, | |
| 2055 | 2054 | MATROSKA_ID_TAGTARGETS_ATTACHUID, track->uid); |
| 2056 | 2055 | if (ret < 0) |
| 2057 | 2056 | return ret; |
| @@ -2136,7 +2135,7 @@ static int mkv_write_chapters(AVFormatContext *s) · 78 unmodified lines | ||
| 2136 | 2135 | ff_metadata_conv(&c->metadata, ff_mkv_metadata_conv, NULL); |
| 2137 | 2136 | |
| 2138 | 2137 | if (mkv_check_tag(c->metadata, MATROSKA_ID_TAGTARGETS_CHAPTERUID)) { |
| 2139 | − ret = mkv_write_tag(mkv, c->metadata, tags, NULL, | |
| 2138 | + ret = mkv_write_tag(mkv, c->metadata, tags, 0, | |
| 2140 | 2139 | MATROSKA_ID_TAGTARGETS_CHAPTERUID, uid); |
| 2141 | 2140 | if (ret < 0) |
| 2142 | 2141 | goto fail; |
| @@ -2336,6 +2335,10 @@ static int mkv_write_header(AVFormatContext *s) · 193 unmodified lines | ||
| 2336 | 2335 | AVIOContext *pb = s->pb; |
| 2337 | 2336 | int ret, version = 2; |
| 2338 | 2337 | |
| 2338 | + ret = avio_open_dyn_buf(&mkv->tmp_bc); | |
| 2339 | + if (ret < 0) | |
| 2340 | + return ret; | |
| 2341 | + | |
| 2339 | 2342 | if (!IS_WEBM(mkv) || |
| 2340 | 2343 | av_dict_get(s->metadata, "stereo_mode", NULL, 0) || |
| 2341 | 2344 | av_dict_get(s->metadata, "alpha_mode", NULL, 0)) |
| @@ -3010,6 +3013,7 @@ after_cues: · 668 unmodified lines | ||
| 3010 | 3013 | |
| 3011 | 3014 | // update stream durations |
| 3012 | 3015 | if (mkv->tags.bc) { |
| 3016 | + AVIOContext *tags_bc = mkv->tags.bc; | |
| 3013 | 3017 | int i; |
| 3014 | 3018 | for (i = 0; i < s->nb_streams; ++i) { |
| 3015 | 3019 | const AVStream *st = s->streams[i]; |
| @@ -3018,17 +3022,22 @@ after_cues: · 2 unmodified lines | ||
| 3018 | 3022 | if (track->duration_offset > 0) { |
| 3019 | 3023 | double duration_sec = track->duration * av_q2d(st->time_base); |
| 3020 | 3024 | char duration_string[20] = ""; |
| 3025 | + ebml_master simpletag; | |
| 3021 | 3026 | |
| 3022 | 3027 | av_log(s, AV_LOG_DEBUG, "stream %d end duration = %" PRIu64 "\n", i, |
| 3023 | 3028 | track->duration); |
| 3024 | 3029 | |
| 3025 | − avio_seek(mkv->tags.bc, track->duration_offset, SEEK_SET); | |
| 3030 | + avio_seek(tags_bc, track->duration_offset, SEEK_SET); | |
| 3031 | + simpletag = start_ebml_master(tags_bc, MATROSKA_ID_SIMPLETAG, | |
| 3032 | + 2 + 1 + 8 + 23); | |
| 3033 | + put_ebml_string(tags_bc, MATROSKA_ID_TAGNAME, "DURATION"); | |
| 3026 | 3034 | |
| 3027 | 3035 | snprintf(duration_string, 20, "%02d:%02d:%012.9f", |
| 3028 | 3036 | (int) duration_sec / 3600, ((int) duration_sec / 60) % 60, |
| 3029 | 3037 | fmod(duration_sec, 60)); |
| 3030 | 3038 | |
| 3031 | − put_ebml_binary(mkv->tags.bc, MATROSKA_ID_TAGSTRING, duration_string, 20); | |
| 3039 | + put_ebml_binary(tags_bc, MATROSKA_ID_TAGSTRING, duration_string, 20); | |
| 3040 | + end_ebml_master(tags_bc, simpletag); | |
| 3032 | 3041 | } |
| 3033 | 3042 | } |
| 3034 | 3043 | |