From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
To: ffmpeg-devel@ffmpeg.org
Cc: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Subject: [FFmpeg-devel] [PATCH 3/4] avformat/matroskaenc: Don't check twice whether to write tags
Date: Wed, 15 Jun 2022 14:01:15 +0200
Message-ID: <DB6PR0101MB2214198D307541505E7FB8518FAD9@DB6PR0101MB2214.eurprd01.prod.exchangelabs.com> (raw)
In-Reply-To: <DB6PR0101MB2214A75346424BB0B8D268408FAD9@DB6PR0101MB2214.eurprd01.prod.exchangelabs.com>
Because not all metadata is written as tags, the Matroska muxer
filters out the tags that are not written as tags.
Therefore the code first checks whether a Tag master element
needs to be opened for a given stream/chapter/attachment/global
metadata. If the answer turns out to be yes, it is checked again
whether a given AVDictionaryEntry is written as a tag.
This commit changes this: The Tag element is opened unconditionally
and in case it turns out that it was unneeded, it is discarded again.
This is possible because the Tag element is written into its own
dynamic buffer.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavformat/matroskaenc.c | 40 +++++++++++----------------------------
 1 file changed, 11 insertions(+), 29 deletions(-)
diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index 404fbdf579..f1385c6b21 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -1953,7 +1953,7 @@ static int mkv_write_tag(MatroskaMuxContext *mkv, const AVDictionary *m,
     const AVDictionaryEntry *t = NULL;
     AVIOContext *const tmp_bc = mkv->tmp_bc;
     uint8_t *buf;
-    int ret, size;
+    int ret = 0, size, tag_written = 0;
 
     mkv_write_tag_targets(mkv, tmp_bc, elementid, uid);
 
@@ -1962,10 +1962,13 @@ static int mkv_write_tag(MatroskaMuxContext *mkv, const AVDictionary *m,
             ret = mkv_write_simpletag(tmp_bc, t);
             if (ret < 0)
                 goto end;
+            tag_written = 1;
         }
     }
     if (reserved_size)
         put_ebml_void(tmp_bc, reserved_size);
+    else if (!tag_written)
+        goto end;
 
     size = avio_get_dyn_buf(tmp_bc, &buf);
     if (tmp_bc->error) {
@@ -1984,17 +1987,6 @@ end:
     return ret;
 }
 
-static int mkv_check_tag(const AVDictionary *m, uint32_t elementid)
-{
-    const AVDictionaryEntry *t = NULL;
-
-    while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
-        if (mkv_check_tag_name(t->key, elementid))
-            return 1;
-
-    return 0;
-}
-
 static int mkv_write_tags(AVFormatContext *s)
 {
     MatroskaMuxContext *mkv = s->priv_data;
@@ -2004,11 +1996,9 @@ static int mkv_write_tags(AVFormatContext *s)
 
     ff_metadata_conv_ctx(s, ff_mkv_metadata_conv, NULL);
 
-    if (mkv_check_tag(s->metadata, 0)) {
-        ret = mkv_write_tag(mkv, s->metadata, &mkv->tags.bc, 0, 0, 0);
-        if (ret < 0)
-            return ret;
-    }
+    ret = mkv_write_tag(mkv, s->metadata, &mkv->tags.bc, 0, 0, 0);
+    if (ret < 0)
+        return ret;
 
     for (i = 0; i < s->nb_streams; i++) {
         const AVStream *st = s->streams[i];
@@ -2017,9 +2007,6 @@ static int mkv_write_tags(AVFormatContext *s)
         if (st->codecpar->codec_type == AVMEDIA_TYPE_ATTACHMENT)
             continue;
 
-        if (!seekable && !mkv_check_tag(st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID))
-            continue;
-
         ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc,
                             seekable ? DURATION_SIMPLETAG_SIZE : 0,
                             MATROSKA_ID_TAGTARGETS_TRACKUID, track->uid);
@@ -2037,9 +2024,6 @@ static int mkv_write_tags(AVFormatContext *s)
             if (st->codecpar->codec_type != AVMEDIA_TYPE_ATTACHMENT)
                 continue;
 
-            if (!mkv_check_tag(st->metadata, MATROSKA_ID_TAGTARGETS_ATTACHUID))
-                continue;
-
             ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc, 0,
                                 MATROSKA_ID_TAGTARGETS_ATTACHUID, track->uid);
             if (ret < 0)
@@ -2124,12 +2108,10 @@ static int mkv_write_chapters(AVFormatContext *s)
         if (tags) {
             ff_metadata_conv(&c->metadata, ff_mkv_metadata_conv, NULL);
 
-            if (mkv_check_tag(c->metadata, MATROSKA_ID_TAGTARGETS_CHAPTERUID)) {
-                ret = mkv_write_tag(mkv, c->metadata, tags, 0,
-                                    MATROSKA_ID_TAGTARGETS_CHAPTERUID, uid);
-                if (ret < 0)
-                    goto fail;
-            }
+            ret = mkv_write_tag(mkv, c->metadata, tags, 0,
+                                MATROSKA_ID_TAGTARGETS_CHAPTERUID, uid);
+            if (ret < 0)
+                goto fail;
         }
     }
     end_ebml_master(dyn_cp, editionentry);
-- 
2.34.1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next prev parent reply	other threads:[~2022-06-15 12:02 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-15  6:47 [FFmpeg-devel] [PATCH] avformat/matroskaenc: Convert chapter metadata Andreas Rheinhardt
2022-06-15 12:01 ` [FFmpeg-devel] [PATCH 2/4] avformat/matroskaenc: Don't waste bytes to Write Tag length fields Andreas Rheinhardt
2022-06-15 12:01 ` Andreas Rheinhardt [this message]
2022-06-15 12:01 ` [FFmpeg-devel] [PATCH 4/4] avformat/matroskaenc: Reuse dynamic buffer Andreas Rheinhardt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox
  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):
  git send-email \
    --in-reply-to=DB6PR0101MB2214198D307541505E7FB8518FAD9@DB6PR0101MB2214.eurprd01.prod.exchangelabs.com \
    --to=andreas.rheinhardt@outlook.com \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY
  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git