[FFmpeg-devel] [PATCH 1/3] fftools/ffmpeg: merge DemuxPktData into FrameData

Anton Khirnov anton at khirnov.net
Tue Dec 12 14:09:56 EET 2023


This way we can propagate arbitrary data from the demuxer all the way
into the muxer, using a single struct.
---
 fftools/ffmpeg.c       | 28 ++++++++++++++++++++--------
 fftools/ffmpeg.h       | 13 +++++++------
 fftools/ffmpeg_demux.c |  9 ++++-----
 fftools/ffmpeg_mux.c   |  4 ++--
 4 files changed, 33 insertions(+), 21 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 30b594fd97..e7ff9a6d6f 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -404,36 +404,48 @@ InputStream *ist_iter(InputStream *prev)
     return NULL;
 }
 
-static int frame_data_ensure(AVFrame *frame, int writable)
+static int frame_data_ensure(AVBufferRef **dst, int writable)
 {
-    if (!frame->opaque_ref) {
+    if (!*dst) {
         FrameData *fd;
 
-        frame->opaque_ref = av_buffer_allocz(sizeof(*fd));
-        if (!frame->opaque_ref)
+        *dst = av_buffer_allocz(sizeof(*fd));
+        if (!*dst)
             return AVERROR(ENOMEM);
-        fd = (FrameData*)frame->opaque_ref->data;
+        fd = (FrameData*)((*dst)->data);
 
         fd->dec.frame_num = UINT64_MAX;
         fd->dec.pts       = AV_NOPTS_VALUE;
     } else if (writable)
-        return av_buffer_make_writable(&frame->opaque_ref);
+        return av_buffer_make_writable(dst);
 
     return 0;
 }
 
 FrameData *frame_data(AVFrame *frame)
 {
-    int ret = frame_data_ensure(frame, 1);
+    int ret = frame_data_ensure(&frame->opaque_ref, 1);
     return ret < 0 ? NULL : (FrameData*)frame->opaque_ref->data;
 }
 
 const FrameData *frame_data_c(AVFrame *frame)
 {
-    int ret = frame_data_ensure(frame, 0);
+    int ret = frame_data_ensure(&frame->opaque_ref, 0);
     return ret < 0 ? NULL : (const FrameData*)frame->opaque_ref->data;
 }
 
+FrameData *packet_data(AVPacket *pkt)
+{
+    int ret = frame_data_ensure(&pkt->opaque_ref, 1);
+    return ret < 0 ? NULL : (FrameData*)pkt->opaque_ref->data;
+}
+
+const FrameData *packet_data_c(AVPacket *pkt)
+{
+    int ret = frame_data_ensure(&pkt->opaque_ref, 0);
+    return ret < 0 ? NULL : (const FrameData*)pkt->opaque_ref->data;
+}
+
 void remove_avoptions(AVDictionary **a, AVDictionary *b)
 {
     const AVDictionaryEntry *t = NULL;
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index ba82b7490d..aafb35538e 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -115,12 +115,6 @@ typedef struct {
 } AudioChannelMap;
 #endif
 
-typedef struct DemuxPktData {
-    // estimated dts in AV_TIME_BASE_Q,
-    // to be used when real dts is missing
-    int64_t dts_est;
-} DemuxPktData;
-
 typedef struct OptionsContext {
     OptionGroup *g;
 
@@ -622,6 +616,10 @@ typedef struct OutputFile {
 
 // optionally attached as opaque_ref to decoded AVFrames
 typedef struct FrameData {
+    // demuxer-estimated dts in AV_TIME_BASE_Q,
+    // to be used when real dts is missing
+    int64_t dts_est;
+
     // properties that come from the decoder
     struct {
         uint64_t   frame_num;
@@ -723,6 +721,9 @@ FrameData *frame_data(AVFrame *frame);
 
 const FrameData *frame_data_c(AVFrame *frame);
 
+FrameData       *packet_data  (AVPacket *pkt);
+const FrameData *packet_data_c(AVPacket *pkt);
+
 /**
  * Set up fallback filtering parameters from a decoder context. They will only
  * be used if no frames are ever sent on this input, otherwise the actual
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index 91cd7a1125..392b447338 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -328,14 +328,13 @@ static int ist_dts_update(DemuxStream *ds, AVPacket *pkt)
 
     av_assert0(!pkt->opaque_ref);
     if (ds->streamcopy_needed) {
-        DemuxPktData *pd;
+        FrameData *fd;
 
-        pkt->opaque_ref = av_buffer_allocz(sizeof(*pd));
-        if (!pkt->opaque_ref)
+        fd = packet_data(pkt);
+        if (!fd)
             return AVERROR(ENOMEM);
-        pd = (DemuxPktData*)pkt->opaque_ref->data;
 
-        pd->dts_est = ds->dts;
+        fd->dts_est = ds->dts;
     }
 
     return 0;
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 067dc65d4e..62925be8d0 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -381,8 +381,8 @@ static int of_streamcopy(OutputStream *ost, AVPacket *pkt)
 {
     OutputFile *of = output_files[ost->file_index];
     MuxStream  *ms = ms_from_ost(ost);
-    DemuxPktData *pd = pkt->opaque_ref ? (DemuxPktData*)pkt->opaque_ref->data : NULL;
-    int64_t      dts = pd ? pd->dts_est : AV_NOPTS_VALUE;
+    FrameData  *fd = pkt->opaque_ref ? (FrameData*)pkt->opaque_ref->data : NULL;
+    int64_t      dts = fd ? fd->dts_est : AV_NOPTS_VALUE;
     int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
     int64_t ts_offset;
 
-- 
2.42.0



More information about the ffmpeg-devel mailing list