PR #26372 opened by clementperon
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26372
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26372.patch

# Summary of changes

The Producer Reference Time exists as packet side data only 
(`AV_PKT_DATA_PRFT`). The RTP demuxer sets it from the RTCP sender reports, and 
the MP4 and DASH muxers write it, so it survives `-c copy`. It is lost as soon 
as the stream is decoded: with a re-encode, `-write_prft wallclock` falls back 
on the clock of the muxer.

This series carries it through a decode and an encode:

1. **avutil/frame**: add `AV_FRAME_DATA_PRFT`, with the same 
`AVProducerReferenceTime` payload.
2. **avcodec/decode**: copy the packet side data to the decoded frame, in the 
table that already does it for the other per-packet side data.
3. **avcodec/encode**: copy the frame side data to the packet, for the encoders 
without delay, in `encode_set_packet_props()`. A packet that already has the 
side data keeps it.

## Reproduction

An RTSP loopback at 30 fps, recorded with a stream copy and with a re-encode at 
the same time:

```
ffmpeg -listen_timeout 30 -rtsp_flags listen -rtsp_transport tcp -i 
rtsp://127.0.0.1:8562/test \
       -c copy     -movflags +frag_every_frame+empty_moov+default_base_moof 
-write_prft wallclock copy.mp4 \
       -c:v mjpeg  -movflags +frag_every_frame+empty_moov+default_base_moof 
-write_prft wallclock enc.mp4 &
ffmpeg -re -f lavfi -i testsrc=size=320x240:rate=30 -t 6 -c:v mpeg4 -q:v 5 \
       -f rtsp -rtsp_transport tcp rtsp://127.0.0.1:8562/test
```

Times of the 180 `prft` boxes of `enc.mp4` against those of `copy.mp4`:

| | Boxes with the same time | Difference |
|---|---|---|
| Before | 0 of 180 | -430 ms to +133 ms |
| After | 180 of 180 | 0 |

## Notes

- **Encoders with delay** are not changed: there the packet is not made of the 
frame just sent, so each encoder would have to carry the side data itself, as 
for `AV_CODEC_FLAG_COPY_OPAQUE`. libx264 with `-export_side_data prft` keeps 
writing its own time.
- **The struct stays in libavcodec** (`libavcodec/defs.h`) while the type is in 
libavutil. `AV_FRAME_DATA_PANSCAN` does the same with `AVPanScan`. I can move 
the struct to libavutil if that is preferred.
- **Filters** pass the side data with the frame. A filter that duplicates or 
drops frames duplicates or drops it with them.
- **ffprobe** does not print the new frame side data here. #26182 adds the 
packet side; the frame side can follow it.
- Independent of #26182, #26183 and #26206.

## Tests

- New `fate-api-prft`: a raw video packet with the side data is decoded and the 
frame encoded again; the test prints the time found on the frame and on the new 
packet, and the same without side data. It fails without the third commit.
- `make fate` with the sample suite passes (macOS arm64); no existing reference 
changes.

AI disclosure: the patches, the tests and this description were prepared with 
Claude Code.

🤖 Generated with [Claude Code](https://claude.com/claude-code)



From 97016d7fcb15969bcad5702ce4c2620e0db09535 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20P=C3=A9ron?= <[email protected]>
Date: Thu, 26 Nov 2026 18:24:54 +0100
Subject: [PATCH 1/3] avutil/frame: add AV_FRAME_DATA_PRFT
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The Producer Reference Time exists as packet side data only, so the
time a demuxer attaches to a packet is lost as soon as the packet is
decoded.

Add the frame side data type that holds the same AVProducerReferenceTime
struct.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Signed-off-by: ClĂ©ment PĂ©ron <[email protected]>
---
 doc/APIchanges        | 3 +++
 libavutil/frame.h     | 6 ++++++
 libavutil/side_data.c | 1 +
 libavutil/version.h   | 4 ++--
 4 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 33d895237c9..69822d3356a 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -2,6 +2,9 @@ The last version increases of all libraries were on 2026-06-23.
 
 API changes, most recent first:
 
+2026-11-xx - xxxxxxxxxx - lavu 61.22.100 - frame.h
+  Add AV_FRAME_DATA_PRFT.
+
 2026-11-xx - xxxxxxxxxx - lavu 61.21.100 - cpu.h
   Add AV_CPU_FLAG_AVXVNNI.
 
diff --git a/libavutil/frame.h b/libavutil/frame.h
index ec4e0c7ac79..6fdef68cdb0 100644
--- a/libavutil/frame.h
+++ b/libavutil/frame.h
@@ -312,6 +312,12 @@ enum AVFrameSideDataType {
      * struct defined in libavutil/gain_map.h.
      */
     AV_FRAME_DATA_GAIN_MAP_PARAMS,
+
+    /**
+     * Producer Reference Time data. The data is the AVProducerReferenceTime
+     * struct defined in libavcodec.
+     */
+    AV_FRAME_DATA_PRFT,
 };
 
 enum AVActiveFormatDescription {
diff --git a/libavutil/side_data.c b/libavutil/side_data.c
index 0d0289a9b64..4293fe87c45 100644
--- a/libavutil/side_data.c
+++ b/libavutil/side_data.c
@@ -45,6 +45,7 @@ static const AVSideDataDescriptor sd_props[] = {
     [AV_FRAME_DATA_DOVI_METADATA]               = { "Dolby Vision Metadata",   
                     AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
     [AV_FRAME_DATA_LCEVC]                       = { "LCEVC NAL data",          
                     AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
     [AV_FRAME_DATA_VIEW_ID]                     = { "View ID" },
+    [AV_FRAME_DATA_PRFT]                        = { "Producer Reference Time" 
},
     [AV_FRAME_DATA_STEREO3D]                    = { "Stereo 3D",               
                     AV_SIDE_DATA_PROP_GLOBAL },
     [AV_FRAME_DATA_REPLAYGAIN]                  = { "AVReplayGain",            
                     AV_SIDE_DATA_PROP_GLOBAL },
     [AV_FRAME_DATA_DISPLAYMATRIX]               = { "3x3 displaymatrix",       
                     AV_SIDE_DATA_PROP_GLOBAL },
diff --git a/libavutil/version.h b/libavutil/version.h
index 1e8c7802357..14e4ea583a5 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -79,8 +79,8 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR  61
-#define LIBAVUTIL_VERSION_MINOR  21
-#define LIBAVUTIL_VERSION_MICRO 101
+#define LIBAVUTIL_VERSION_MINOR  22
+#define LIBAVUTIL_VERSION_MICRO 100
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
                                                LIBAVUTIL_VERSION_MINOR, \
-- 
2.54.1


From 0be0336f9191d316bd5a9fe39378b97eb48104c1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20P=C3=A9ron?= <[email protected]>
Date: Thu, 26 Nov 2026 18:24:54 +0100
Subject: [PATCH 2/3] avcodec/decode: export the Producer Reference Time as
 frame side data
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Copy the AV_PKT_DATA_PRFT side data of a packet to the frame decoded
from it, like the other per-packet side data.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Signed-off-by: ClĂ©ment PĂ©ron <[email protected]>
---
 libavcodec/decode.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index 4f40b3677e2..6a3a98e4145 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -1580,6 +1580,7 @@ int ff_decode_frame_props_from_pkt(const AVCodecContext 
*avctx,
         { AV_PKT_DATA_IAMF_MIX_GAIN_PARAM,         
AV_FRAME_DATA_IAMF_MIX_GAIN_PARAM },
         { AV_PKT_DATA_IAMF_DEMIXING_INFO_PARAM,    
AV_FRAME_DATA_IAMF_DEMIXING_INFO_PARAM },
         { AV_PKT_DATA_IAMF_RECON_GAIN_INFO_PARAM,  
AV_FRAME_DATA_IAMF_RECON_GAIN_INFO_PARAM },
+        { AV_PKT_DATA_PRFT,                        AV_FRAME_DATA_PRFT },
         { AV_PKT_DATA_NB }
     };
 
-- 
2.54.1


From 06564272122cfdc3dc2b184f2f90fcd7fbcaa17f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20P=C3=A9ron?= <[email protected]>
Date: Thu, 26 Nov 2026 18:29:17 +0100
Subject: [PATCH 3/3] avcodec/encode: pass the Producer Reference Time to the
 packet
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Copy the AV_FRAME_DATA_PRFT side data of a frame to the packet encoded
from it, for the encoders without delay, where a packet is made of the
frame just sent. Encoders with delay are not changed.

A packet that already has this side data keeps it.

With the previous commit, the time a demuxer attached to a packet now
survives a decode and an encode, for example from an RTSP input to the
'prft' boxes written by the MP4 muxer with -write_prft wallclock.

Add a test that decodes and encodes a raw video packet.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Signed-off-by: ClĂ©ment PĂ©ron <[email protected]>
---
 libavcodec/encode.c       |   8 +++
 tests/api/Makefile        |   1 +
 tests/api/api-prft-test.c | 130 ++++++++++++++++++++++++++++++++++++++
 tests/fate/api.mak        |   4 ++
 tests/ref/fate/api-prft   |   4 ++
 5 files changed, 147 insertions(+)
 create mode 100644 tests/api/api-prft-test.c
 create mode 100644 tests/ref/fate/api-prft

diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index 27c4fd186a3..301c71872a7 100644
--- a/libavcodec/encode.c
+++ b/libavcodec/encode.c
@@ -236,6 +236,7 @@ static int encode_set_packet_props(AVCodecContext *avctx, 
AVPacket *avpkt, const
 {
     AVCodecInternal *avci = avctx->internal;
     EncodeContext     *ec = encode_ctx(avci);
+    AVFrameSideData *prft = av_frame_get_side_data(frame, AV_FRAME_DATA_PRFT);
 
     if (avpkt->pts == AV_NOPTS_VALUE) {
         avpkt->pts = frame->pts;
@@ -278,6 +279,13 @@ static int encode_set_packet_props(AVCodecContext *avctx, 
AVPacket *avpkt, const
         }
     }
 
+    if (prft && !av_packet_get_side_data(avpkt, AV_PKT_DATA_PRFT, NULL)) {
+        uint8_t *packet_sd = av_packet_new_side_data(avpkt, AV_PKT_DATA_PRFT, 
prft->size);
+        if (!packet_sd)
+            return AVERROR(ENOMEM);
+        memcpy(packet_sd, prft->data, prft->size);
+    }
+
     return 0;
 }
 
diff --git a/tests/api/Makefile b/tests/api/Makefile
index 75df91492d9..87c3f92640e 100644
--- a/tests/api/Makefile
+++ b/tests/api/Makefile
@@ -12,6 +12,7 @@ APITESTPROGS-$(call DEMDEC, H263, H263) += api-band
 APITESTPROGS-$(HAVE_THREADS) += api-threadmessage
 APITESTPROGS-$(CONFIG_AVCODEC) += api-enc-info
 APITESTPROGS-$(CONFIG_PCM_S16LE_DECODER) += api-skip-samples
+APITESTPROGS-$(call ENCDEC, RAWVIDEO, RAWVIDEO) += api-prft
 APITESTPROGS-$(call ALLYES, H261_ENCODER H261_PARSER) += api-enc-parser
 APITESTPROGS-$(CONFIG_VC1_PARSER) += api-vc1-parser
 APITESTPROGS += $(APITESTPROGS-yes)
diff --git a/tests/api/api-prft-test.c b/tests/api/api-prft-test.c
new file mode 100644
index 00000000000..fdfc8663353
--- /dev/null
+++ b/tests/api/api-prft-test.c
@@ -0,0 +1,130 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * Decode one packet carrying AV_PKT_DATA_PRFT, encode the decoded frame and
+ * print the Producer Reference Time found on the frame and on the new packet.
+ */
+
+#include <inttypes.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "libavcodec/avcodec.h"
+#include "libavutil/frame.h"
+#include "libavutil/pixfmt.h"
+
+#define WIDTH  16
+#define HEIGHT 16
+
+static void print_prft(const char *name, const uint8_t *data, size_t size)
+{
+    const AVProducerReferenceTime *prft = (const AVProducerReferenceTime 
*)data;
+
+    if (!data || size != sizeof(*prft))
+        printf("%s: no producer reference time\n", name);
+    else
+        printf("%s: wallclock %"PRId64" flags %d\n", name, prft->wallclock, 
prft->flags);
+}
+
+static AVCodecContext *open_codec(const AVCodec *codec)
+{
+    AVCodecContext *ctx = avcodec_alloc_context3(codec);
+
+    if (!ctx)
+        return NULL;
+    ctx->width     = WIDTH;
+    ctx->height    = HEIGHT;
+    ctx->pix_fmt   = AV_PIX_FMT_GRAY8;
+    ctx->time_base = (AVRational){ 1, 25 };
+    if (avcodec_open2(ctx, codec, NULL) < 0)
+        avcodec_free_context(&ctx);
+    return ctx;
+}
+
+static int run(int with_prft)
+{
+    AVCodecContext *dec = 
open_codec(avcodec_find_decoder(AV_CODEC_ID_RAWVIDEO));
+    AVCodecContext *enc = 
open_codec(avcodec_find_encoder(AV_CODEC_ID_RAWVIDEO));
+    AVPacket *pkt = av_packet_alloc();
+    AVFrame *frame = av_frame_alloc();
+    AVFrameSideData *frame_sd;
+    uint8_t *side;
+    size_t size;
+    int ret;
+
+    if (!dec || !enc || !pkt || !frame) {
+        ret = AVERROR(ENOMEM);
+        goto end;
+    }
+
+    ret = av_new_packet(pkt, WIDTH * HEIGHT);
+    if (ret < 0)
+        goto end;
+    memset(pkt->data, 0, pkt->size);
+    pkt->pts = 0;
+    if (with_prft) {
+        AVProducerReferenceTime *prft = (AVProducerReferenceTime *)
+            av_packet_new_side_data(pkt, AV_PKT_DATA_PRFT, sizeof(*prft));
+        if (!prft) {
+            ret = AVERROR(ENOMEM);
+            goto end;
+        }
+        *prft = (AVProducerReferenceTime) { .wallclock = 1700000000123456LL, 
.flags = 24 };
+    }
+
+    ret = avcodec_send_packet(dec, pkt);
+    if (ret < 0)
+        goto end;
+    ret = avcodec_receive_frame(dec, frame);
+    if (ret < 0)
+        goto end;
+    av_packet_unref(pkt);
+
+    frame_sd = av_frame_get_side_data(frame, AV_FRAME_DATA_PRFT);
+    print_prft("frame", frame_sd ? frame_sd->data : NULL, frame_sd ? 
frame_sd->size : 0);
+
+    ret = avcodec_send_frame(enc, frame);
+    if (ret < 0)
+        goto end;
+    ret = avcodec_receive_packet(enc, pkt);
+    if (ret < 0)
+        goto end;
+
+    side = av_packet_get_side_data(pkt, AV_PKT_DATA_PRFT, &size);
+    print_prft("packet", side, side ? size : 0);
+
+end:
+    av_frame_free(&frame);
+    av_packet_free(&pkt);
+    avcodec_free_context(&enc);
+    avcodec_free_context(&dec);
+    return ret;
+}
+
+int main(void)
+{
+    for (int with_prft = 1; with_prft >= 0; with_prft--) {
+        int ret = run(with_prft);
+        if (ret < 0) {
+            fprintf(stderr, "failed: %s\n", av_err2str(ret));
+            return 1;
+        }
+    }
+    return 0;
+}
diff --git a/tests/fate/api.mak b/tests/fate/api.mak
index 69df95b90d0..ad665809b03 100644
--- a/tests/fate/api.mak
+++ b/tests/fate/api.mak
@@ -24,6 +24,10 @@ FATE_API_LIBAVCODEC-$(CONFIG_PCM_S16LE_DECODER) += 
fate-api-skip-samples
 fate-api-skip-samples: $(APITESTSDIR)/api-skip-samples-test$(EXESUF)
 fate-api-skip-samples: CMD = run $(APITESTSDIR)/api-skip-samples-test$(EXESUF)
 
+FATE_API_LIBAVCODEC-$(call ENCDEC, RAWVIDEO, RAWVIDEO) += fate-api-prft
+fate-api-prft: $(APITESTSDIR)/api-prft-test$(EXESUF)
+fate-api-prft: CMD = run $(APITESTSDIR)/api-prft-test$(EXESUF)
+
 FATE_API_LIBAVCODEC-$(CONFIG_MOVTEXT_ENCODER) += fate-api-movtext
 fate-api-movtext: $(APITESTSDIR)/api-movtext-test$(EXESUF)
 fate-api-movtext: CMD = run $(APITESTSDIR)/api-movtext-test$(EXESUF)
diff --git a/tests/ref/fate/api-prft b/tests/ref/fate/api-prft
new file mode 100644
index 00000000000..8f705b8dce4
--- /dev/null
+++ b/tests/ref/fate/api-prft
@@ -0,0 +1,4 @@
+frame: wallclock 1700000000123456 flags 24
+packet: wallclock 1700000000123456 flags 24
+frame: no producer reference time
+packet: no producer reference time
-- 
2.54.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to