[FFmpeg-devel] [PATCH 05/25] avformat/matroskaenc: Don't waste bytes when writing attachments

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Mon Jan 17 01:03:45 EET 2022


Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavformat/matroskaenc.c     | 20 ++++++++++++--------
 tests/ref/lavf/mkv_attachment |  4 ++--
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index 154de25f59..0927e0b873 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -2056,30 +2056,34 @@ static int mkv_write_attachments(AVFormatContext *s)
     for (i = 0; i < s->nb_streams; i++) {
         const AVStream *st = s->streams[i];
         mkv_track *track = &mkv->tracks[i];
-        ebml_master attached_file;
+        EBML_WRITER(6);
         const AVDictionaryEntry *t;
         const char *mimetype;
 
         if (st->codecpar->codec_type != AVMEDIA_TYPE_ATTACHMENT)
             continue;
 
-        attached_file = start_ebml_master(dyn_cp, MATROSKA_ID_ATTACHEDFILE, 0);
+        ebml_writer_open_master(&writer, MATROSKA_ID_ATTACHEDFILE);
 
         if (t = av_dict_get(st->metadata, "title", NULL, 0))
-            put_ebml_string(dyn_cp, MATROSKA_ID_FILEDESC, t->value);
+            ebml_writer_add_string(&writer, MATROSKA_ID_FILEDESC, t->value);
         if (!(t = av_dict_get(st->metadata, "filename", NULL, 0))) {
             av_log(s, AV_LOG_ERROR, "Attachment stream %d has no filename tag.\n", i);
             ffio_free_dyn_buf(&dyn_cp);
             return AVERROR(EINVAL);
         }
-        put_ebml_string(dyn_cp, MATROSKA_ID_FILENAME, t->value);
+        ebml_writer_add_string(&writer, MATROSKA_ID_FILENAME, t->value);
 
         mimetype = get_mimetype(st);
         av_assert0(mimetype);
-        put_ebml_string(dyn_cp, MATROSKA_ID_FILEMIMETYPE, mimetype);
-        put_ebml_binary(dyn_cp, MATROSKA_ID_FILEDATA, st->codecpar->extradata, st->codecpar->extradata_size);
-        put_ebml_uid(dyn_cp, MATROSKA_ID_FILEUID, track->uid);
-        end_ebml_master(dyn_cp, attached_file);
+        ebml_writer_add_string(&writer, MATROSKA_ID_FILEMIMETYPE, mimetype);
+        ebml_writer_add_bin(&writer, MATROSKA_ID_FILEDATA, st->codecpar->extradata, st->codecpar->extradata_size);
+        ebml_writer_add_uid(&writer, MATROSKA_ID_FILEUID, track->uid);
+        ret = ebml_writer_write(&writer, dyn_cp);
+        if (ret < 0) {
+            ffio_free_dyn_buf(&dyn_cp);
+            return ret;
+        }
     }
     return end_ebml_master_crc32(pb, &dyn_cp, mkv,
                                  MATROSKA_ID_ATTACHMENTS, 0, 0, 1);
diff --git a/tests/ref/lavf/mkv_attachment b/tests/ref/lavf/mkv_attachment
index 0149bf2010..4a2f9eb7f1 100644
--- a/tests/ref/lavf/mkv_attachment
+++ b/tests/ref/lavf/mkv_attachment
@@ -1,3 +1,3 @@
-6a77b40186840d596bd584182ac3a283 *tests/data/lavf/lavf.mkv_attachment
-472579 tests/data/lavf/lavf.mkv_attachment
+32b24b97956f1fbcfbc1a94aac8f6202 *tests/data/lavf/lavf.mkv_attachment
+472574 tests/data/lavf/lavf.mkv_attachment
 tests/data/lavf/lavf.mkv_attachment CRC=0xec6c3c68
-- 
2.32.0



More information about the ffmpeg-devel mailing list