Commit 8ceb2b21a3 for ffmpeg

commit 8ceb2b21a3a35412e9bfd2e4475c16d2992d23c3
Author: James Almer <jamrial@gmail.com>
Date:   Wed Dec 2 15:52:36 2026 -0300

    avformat/matroskadec: support multiple Edition entries

    And use the one marked as default, or the first in the lack of thereof, as documented.

    Signed-off-by: James Almer <jamrial@gmail.com>

diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c
index 248ee21055..5cb6f8d05d 100644
--- a/libavformat/matroskadec.c
+++ b/libavformat/matroskadec.c
@@ -335,6 +335,11 @@ typedef struct MatroskaChapter {
     AVChapter *chapter;
 } MatroskaChapter;

+typedef struct MatroskaEdition {
+    uint64_t flag_default;
+    EbmlList chapters;
+} MatroskaEdition;
+
 typedef struct MatroskaIndexPos {
     uint64_t track;
     uint64_t pos;
@@ -420,7 +425,7 @@ typedef struct MatroskaDemuxContext {
     EbmlBin  date_utc;
     EbmlList tracks;
     EbmlList attachments;
-    EbmlList chapters;
+    EbmlList editions;
     EbmlList index;
     EbmlList tags;
     EbmlList seekhead;
@@ -471,7 +476,7 @@ typedef struct MatroskaDemuxContext {
 static EbmlSyntax ebml_syntax[3], matroska_segment[9], matroska_track_video_color[15], matroska_track_video[19],
                   matroska_track[33], matroska_track_encoding[6], matroska_track_encodings[2],
                   matroska_track_combine_planes[2], matroska_track_operation[2], matroska_block_addition_mapping[5], matroska_tracks[2],
-                  matroska_attachments[2], matroska_chapter_entry[9], matroska_chapter[6], matroska_chapters[2],
+                  matroska_attachments[2], matroska_chapter[9], matroska_edition[6], matroska_chapters[2],
                   matroska_index_entry[3], matroska_index[2], matroska_tag[3], matroska_tags[2], matroska_seekhead[2],
                   matroska_blockadditions[2], matroska_blockgroup[8], matroska_cluster_parsing[8];

@@ -687,10 +692,10 @@ static EbmlSyntax matroska_chapter_display[] = {
     { MATROSKA_ID_CHAPSTRING,  EBML_UTF8, 0, 0, offsetof(MatroskaChapter, title) },
     { MATROSKA_ID_CHAPLANG,    EBML_NONE },
     { MATROSKA_ID_CHAPCOUNTRY, EBML_NONE },
-    CHILD_OF(matroska_chapter_entry)
+    CHILD_OF(matroska_chapter)
 };

-static EbmlSyntax matroska_chapter_entry[] = {
+static EbmlSyntax matroska_chapter[] = {
     { MATROSKA_ID_CHAPTERTIMESTART,   EBML_UINT, 0, 0, offsetof(MatroskaChapter, start), { .u = AV_NOPTS_VALUE } },
     { MATROSKA_ID_CHAPTERTIMEEND,     EBML_UINT, 0, 0, offsetof(MatroskaChapter, end),   { .u = AV_NOPTS_VALUE } },
     { MATROSKA_ID_CHAPTERUID,         EBML_UINT, 0, 0, offsetof(MatroskaChapter, uid) },
@@ -699,20 +704,20 @@ static EbmlSyntax matroska_chapter_entry[] = {
     { MATROSKA_ID_CHAPTERFLAGENABLED, EBML_NONE },
     { MATROSKA_ID_CHAPTERPHYSEQUIV,   EBML_NONE },
     { MATROSKA_ID_CHAPTERATOM,        EBML_NONE },
-    CHILD_OF(matroska_chapter)
+    CHILD_OF(matroska_edition)
 };

-static EbmlSyntax matroska_chapter[] = {
-    { MATROSKA_ID_CHAPTERATOM,        EBML_NEST, 0, sizeof(MatroskaChapter), offsetof(MatroskaDemuxContext, chapters), { .n = matroska_chapter_entry } },
+static EbmlSyntax matroska_edition[] = {
+    { MATROSKA_ID_CHAPTERATOM,        EBML_NEST, 0, sizeof(MatroskaChapter), offsetof(MatroskaEdition, chapters), { .n = matroska_chapter } },
     { MATROSKA_ID_EDITIONUID,         EBML_NONE },
     { MATROSKA_ID_EDITIONFLAGHIDDEN,  EBML_NONE },
-    { MATROSKA_ID_EDITIONFLAGDEFAULT, EBML_NONE },
+    { MATROSKA_ID_EDITIONFLAGDEFAULT, EBML_UINT, 0, 0, offsetof(MatroskaEdition, flag_default), { .u = 0 } },
     { MATROSKA_ID_EDITIONFLAGORDERED, EBML_NONE },
     CHILD_OF(matroska_chapters)
 };

 static EbmlSyntax matroska_chapters[] = {
-    { MATROSKA_ID_EDITIONENTRY, EBML_NEST, 0, 0, 0, { .n = matroska_chapter } },
+    { MATROSKA_ID_EDITIONENTRY, EBML_NEST, 0, sizeof(MatroskaEdition), offsetof(MatroskaDemuxContext, editions), { .n = matroska_edition } },
     CHILD_OF(matroska_segment)
 };

@@ -1911,14 +1916,17 @@ static void matroska_convert_tags(AVFormatContext *s)
                        i, tags[i].target.attachuid);
             }
         } else if (tags[i].target.chapteruid) {
-            MatroskaChapter *chapter = matroska->chapters.elem;
+            MatroskaEdition *edition = matroska->editions.elem;
             int found = 0;
-            for (j = 0; j < matroska->chapters.nb_elem; j++) {
-                if (chapter[j].uid == tags[i].target.chapteruid &&
-                    chapter[j].chapter) {
-                    matroska_convert_tag(s, &tags[i].tag,
-                                         &chapter[j].chapter->metadata, NULL);
-                    found = 1;
+            for (int k = 0; k < matroska->editions.nb_elem; k++) {
+                MatroskaChapter *chapter = edition[k].chapters.elem;
+                for (j = 0; j < edition[k].chapters.nb_elem; j++) {
+                    if (chapter[j].uid == tags[i].target.chapteruid &&
+                        chapter[j].chapter) {
+                        matroska_convert_tag(s, &tags[i].tag,
+                                             &chapter[j].chapter->metadata, NULL);
+                        found = 1;
+                    }
                 }
             }
             if (!found) {
@@ -3589,12 +3597,14 @@ static int matroska_read_header(AVFormatContext *s)
     FFFormatContext *const si = ffformatcontext(s);
     MatroskaDemuxContext *matroska = s->priv_data;
     EbmlList *attachments_list = &matroska->attachments;
-    EbmlList *chapters_list    = &matroska->chapters;
+    EbmlList *editions_list    = &matroska->editions;
     MatroskaAttachment *attachments;
+    MatroskaEdition *editions;
     MatroskaChapter *chapters;
     uint64_t max_start = 0;
     int64_t pos;
     Ebml ebml = { 0 };
+    int default_edition = -1;
     int i, j, res;

     matroska->ctx = s;
@@ -3718,17 +3728,26 @@ static int matroska_read_header(AVFormatContext *s)
         }
     }

-    chapters = chapters_list->elem;
-    for (i = 0; i < chapters_list->nb_elem; i++)
-        if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid &&
-            (max_start == 0 || chapters[i].start > max_start)) {
-            chapters[i].chapter =
-                avpriv_new_chapter(s, chapters[i].uid,
-                                   (AVRational) { 1, 1000000000 },
-                                   chapters[i].start, chapters[i].end,
-                                   chapters[i].title);
-            max_start = chapters[i].start;
-        }
+    editions = editions_list->elem;
+    for (i = 0; i < editions_list->nb_elem; i++) {
+        if (!editions[i].flag_default)
+            continue;
+        default_edition = i;
+        break;
+    }
+    if (default_edition >= 0) {
+        chapters = editions[default_edition].chapters.elem;
+        for (i = 0; i < editions[default_edition].chapters.nb_elem; i++)
+            if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid &&
+                (max_start == 0 || chapters[i].start > max_start)) {
+                chapters[i].chapter =
+                    avpriv_new_chapter(s, chapters[i].uid,
+                                       (AVRational) { 1, 1000000000 },
+                                       chapters[i].start, chapters[i].end,
+                                       chapters[i].title);
+                max_start = chapters[i].start;
+            }
+    }

     matroska_add_index_entries(matroska);