Commit 467548b8da for ffmpeg

commit 467548b8daf26ae90519e14d5baa05ef6b165023
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date:   Thu Nov 12 12:40:23 2026 -0600

    avcodec/opus/tab: add ff_opus_packet_samples()

    Read the duration of an Opus packet from its TOC. It replaces a static
    function of the Matroska muxer.

diff --git a/libavcodec/opus/tab.h b/libavcodec/opus/tab.h
index c1d38f9e69..a6fcf62535 100644
--- a/libavcodec/opus/tab.h
+++ b/libavcodec/opus/tab.h
@@ -26,10 +26,40 @@
 #include <stdint.h>

 #include "libavutil/attributes_internal.h"
+#include "libavutil/error.h"

 FF_VISIBILITY_PUSH_HIDDEN
 extern const uint16_t ff_opus_frame_duration[32];

+/**
+ * @return the duration of an Opus packet in samples at 48 kHz, read from
+ *         its TOC, or AVERROR_INVALIDDATA if the packet is too short or
+ *         longer than 120 ms
+ */
+static inline int ff_opus_packet_samples(const uint8_t *buf, int buf_size)
+{
+    int frame_count, samples;
+
+    if (buf_size < 1)
+        return AVERROR_INVALIDDATA;
+    switch (buf[0] & 3) {
+    case 0:
+        frame_count = 1;
+        break;
+    case 3:
+        if (buf_size < 2)
+            return AVERROR_INVALIDDATA;
+        frame_count = buf[1] & 0x3F;
+        break;
+    default:
+        frame_count = 2;
+        break;
+    }
+    samples = frame_count * ff_opus_frame_duration[buf[0] >> 3];
+    // A packet holds at most 120 ms.
+    return samples > 120 * 48 ? AVERROR_INVALIDDATA : samples;
+}
+
 extern const uint8_t  ff_celt_band_end[];

 extern const uint8_t  ff_opus_default_coupled_streams[];
diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index f08fee0c9a..25526d079d 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -283,34 +283,6 @@ typedef struct MatroskaMuxContext {
 /** Seek preroll value for opus */
 #define OPUS_SEEK_PREROLL 80000000

-/**
- * Returns the duration of an Opus packet in samples.
- */
-static int parse_opus_packet_duration(const uint8_t *buf, int buf_size)
-{
-    int code   = buf[0] & 0x3;
-    int config = buf[0] >> 3;
-    int frame_count;
-
-    switch (code) {
-    default:
-        av_unreachable("code is in 0..3");
-    case 0:
-        frame_count = 1;
-        break;
-    case 1:
-    case 2:
-        frame_count = 2;
-        break;
-    case 3:
-        if (buf_size <= 1)
-            return AVERROR_INVALIDDATA;
-        frame_count = buf[1] & 0x3F;
-        break;
-    }
-    return frame_count * ff_opus_frame_duration[config];
-}
-
 static int ebml_id_size(uint32_t id)
 {
     return (av_log2(id) + 7U) / 8;
@@ -2900,7 +2872,7 @@ static int mkv_write_block(void *logctx, MatroskaMuxContext *mkv,
         duration != track->default_duration_low))
         ebml_writer_add_uint(&writer, MATROSKA_ID_BLOCKDURATION, duration);
     else if (par->codec_id == AV_CODEC_ID_OPUS) {
-        ret = parse_opus_packet_duration(pkt->data, pkt->size);
+        ret = ff_opus_packet_samples(pkt->data, pkt->size);
         if (ret >= 0) {
             /* If the packet's duration is inconsistent with the coded duration,
              * add an explicit duration element. */