+static int mkv_write_chapters(AVFormatContext *s)
+{
+ MatroskaMuxContext *mkv = s->priv_data;
+ AVIOContext *pb = s->pb;
+ ebml_master chapters, editionentry;
+ AVRational scale = {1, 1E9};
+ int i, ret;
+
+ if (!s->nb_chapters)
+ return 0;
+
+ ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CHAPTERS, avio_tell(pb));
+ if (ret < 0) return ret;
+
+ chapters = start_ebml_master(pb, MATROSKA_ID_CHAPTERS , 0);
+ editionentry = start_ebml_master(pb, MATROSKA_ID_EDITIONENTRY, 0);
+ put_ebml_uint(pb, MATROSKA_ID_EDITIONFLAGDEFAULT, 1);
+ put_ebml_uint(pb, MATROSKA_ID_EDITIONFLAGHIDDEN , 0);
+ for (i = 0; i < s->nb_chapters; i++) {
+ ebml_master chapteratom, chapterdisplay;
+ AVChapter *c = s->chapters[i];
+ AVMetadataTag *t = NULL;
+
+ chapteratom = start_ebml_master(pb, MATROSKA_ID_CHAPTERATOM, 0);
+ put_ebml_uint(pb, MATROSKA_ID_CHAPTERUID, c->id);
+ put_ebml_uint(pb, MATROSKA_ID_CHAPTERTIMESTART,
+ av_rescale_q(c->start, c->time_base, scale));
+ put_ebml_uint(pb, MATROSKA_ID_CHAPTERTIMEEND,
+ av_rescale_q(c->end, c->time_base, scale));
+ put_ebml_uint(pb, MATROSKA_ID_CHAPTERFLAGHIDDEN , 0);
+ put_ebml_uint(pb, MATROSKA_ID_CHAPTERFLAGENABLED, 1);
+ if ((t = av_metadata_get(c->metadata, "title", NULL, 0))) {
+ chapterdisplay = start_ebml_master(pb, MATROSKA_ID_CHAPTERDISPLAY, 0);
+ put_ebml_string(pb, MATROSKA_ID_CHAPSTRING, t->value);
+ put_ebml_string(pb, MATROSKA_ID_CHAPLANG , "und");
+ end_ebml_master(pb, chapterdisplay);
+ }
+ end_ebml_master(pb, chapteratom);
+ }
+ end_ebml_master(pb, editionentry);
+ end_ebml_master(pb, chapters);
+ return 0;
+}
+
+static void mkv_write_simpletag(AVIOContext *pb, AVMetadataTag *t)
+{
+ uint8_t *key = av_strdup(t->key);
+ uint8_t *p = key;
+ const uint8_t *lang = NULL;
+ ebml_master tag;
+
+ if ((p = strrchr(p, '-')) &&
+ (lang = av_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
+ *p = 0;
+
+ p = key;
+ while (*p) {
+ if (*p == ' ')
+ *p = '_';
+ else if (*p >= 'a' && *p <= 'z')
+ *p -= 'a' - 'A';
+ p++;
+ }
+
+ tag = start_ebml_master(pb, MATROSKA_ID_SIMPLETAG, 0);
+ put_ebml_string(pb, MATROSKA_ID_TAGNAME, key);
+ if (lang)
+ put_ebml_string(pb, MATROSKA_ID_TAGLANG, lang);
+ put_ebml_string(pb, MATROSKA_ID_TAGSTRING, t->value);
+ end_ebml_master(pb, tag);
+
+ av_freep(&key);
+}
+
+static int mkv_write_tag(AVFormatContext *s, AVMetadata *m, unsigned int elementid,
+ unsigned int uid, ebml_master *tags)
+{
+ MatroskaMuxContext *mkv = s->priv_data;
+ ebml_master tag, targets;
+ AVMetadataTag *t = NULL;
+ int ret;
+
+ if (!tags->pos) {
+ ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_TAGS, avio_tell(s->pb));
+ if (ret < 0) return ret;
+
+ *tags = start_ebml_master(s->pb, MATROSKA_ID_TAGS, 0);
+ }
+
+ tag = start_ebml_master(s->pb, MATROSKA_ID_TAG, 0);
+ targets = start_ebml_master(s->pb, MATROSKA_ID_TAGTARGETS, 0);
+ if (elementid)
+ put_ebml_uint(s->pb, elementid, uid);
+ end_ebml_master(s->pb, targets);
+
+ while ((t = av_metadata_get(m, "", t, AV_METADATA_IGNORE_SUFFIX)))
+ if (strcasecmp(t->key, "title"))
+ mkv_write_simpletag(s->pb, t);
+
+ end_ebml_master(s->pb, tag);
+ return 0;
+}
+
+static int mkv_write_tags(AVFormatContext *s)
+{
+ ebml_master tags = {0};
+ int i, ret;
+
+ ff_metadata_conv_ctx(s, ff_mkv_metadata_conv, NULL);
+
+ if (av_metadata_get(s->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX)) {
+ ret = mkv_write_tag(s, s->metadata, 0, 0, &tags);
+ if (ret < 0) return ret;
+ }
+
+ for (i = 0; i < s->nb_streams; i++) {
+ AVStream *st = s->streams[i];
+
+ if (!av_metadata_get(st->metadata, "", 0, AV_METADATA_IGNORE_SUFFIX))
+ continue;
+
+ ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &tags);
+ if (ret < 0) return ret;
+ }
+
+ for (i = 0; i < s->nb_chapters; i++) {
+ AVChapter *ch = s->chapters[i];
+
+ if (!av_metadata_get(ch->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX))
+ continue;
+
+ ret = mkv_write_tag(s, ch->metadata, MATROSKA_ID_TAGTARGETS_CHAPTERUID, ch->id, &tags);
+ if (ret < 0) return ret;
+ }
+
+ if (tags.pos)
+ end_ebml_master(s->pb, tags);
+ return 0;
+}
+