From 2f5f428c8a51381e9a5d31b41090215a69dcbab3 Mon Sep 17 00:00:00 2001
From: Maxime Gervais <gervais.maxime@gmail.com>
Date: Sun, 21 Jun 2026 16:26:38 +0200
Subject: [PATCH] QTAVPlayer patches

Signed-off-by: Maxime Gervais <gervais.maxime@gmail.com>
---
 .../src/QtAVPlayer/qavcodec.cpp               |  3 ++
 .../src/QtAVPlayer/qavdemuxer.cpp             | 30 ++++++++++++++++++-
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/qctools-QtAVPlayer/src/QtAVPlayer/qavcodec.cpp b/qctools-QtAVPlayer/src/QtAVPlayer/qavcodec.cpp
index 707ced7..d7a8688 100644
--- a/qctools-QtAVPlayer/src/QtAVPlayer/qavcodec.cpp
+++ b/qctools-QtAVPlayer/src/QtAVPlayer/qavcodec.cpp
@@ -65,6 +65,9 @@ bool QAVCodec::open(AVStream *stream, AVDictionary** opts)
     }
 
     d->avctx->codec_id = d->codec->id;
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59, 0, 0)
+    d->avctx->flags |= AV_CODEC_FLAG_COPY_OPAQUE;
+#endif
     ret = avcodec_open2(d->avctx, d->codec, opts);
     if (ret < 0) {
         qWarning() << "Could not open the codec:" << d->codec->name << d->codec->id << ret;
diff --git a/qctools-QtAVPlayer/src/QtAVPlayer/qavdemuxer.cpp b/qctools-QtAVPlayer/src/QtAVPlayer/qavdemuxer.cpp
index 2861a7e..439ccca 100644
--- a/qctools-QtAVPlayer/src/QtAVPlayer/qavdemuxer.cpp
+++ b/qctools-QtAVPlayer/src/QtAVPlayer/qavdemuxer.cpp
@@ -52,6 +52,7 @@ extern "C" {
 #include <libavformat/avformat.h>
 #include <libavdevice/avdevice.h>
 #include <libavcodec/avcodec.h>
+#include <libavutil/buffer.h>
 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59, 0, 0)
 #include <libavcodec/bsf.h>
 #endif
@@ -61,6 +62,29 @@ QT_BEGIN_NAMESPACE
 
 namespace {
 
+struct PacketInfo
+{
+    int64_t pkt_pos = 0;
+    int pkt_size = 0;
+};
+
+static void attachPacketInfo(AVPacket *pkt)
+{
+    if (!pkt)
+        return;
+
+    AVBufferRef *opaque = av_buffer_alloc(sizeof(PacketInfo));
+    if (!opaque)
+        return;
+
+    auto *info = reinterpret_cast<PacketInfo *>(opaque->data);
+    info->pkt_pos = pkt->pos;
+    info->pkt_size = pkt->size;
+
+    av_buffer_unref(&pkt->opaque_ref);
+    pkt->opaque_ref = opaque;
+}
+
 struct QAVDictionaryHolder
 {
     AVDictionary *dict = nullptr;
@@ -702,6 +726,8 @@ int QAVDemuxer::read(QAVPacket &pkt)
         } else {
             qDebug() << "av_read_frame: unexpected result:" << ret;
         }
+    } else {
+        attachPacketInfo(pkt.packet());
     }
     {
         QMutexLocker locker(&d->mutex);
@@ -723,8 +749,10 @@ int QAVDemuxer::read(QAVPacket &pkt)
         if ((ret >= 0 || eof) && d->bsf_ctx) {
             int bsf_ret = av_bsf_send_packet(d->bsf_ctx, d->eof ? NULL : pkt.packet());
             if (bsf_ret >= 0) {
-                while ((bsf_ret = av_bsf_receive_packet(d->bsf_ctx, pkt.packet())) >= 0)
+                while ((bsf_ret = av_bsf_receive_packet(d->bsf_ctx, pkt.packet())) >= 0) {
+                    attachPacketInfo(pkt.packet());
                     d->packets.append(pkt);
+                }
             }
             if (bsf_ret < 0 && bsf_ret != AVERROR_EOF && bsf_ret != AVERROR(EAGAIN)) {
                 qWarning() << "Error applying bitstream filters to an output:" << bsf_ret;
-- 
2.54.0

