[FFmpeg-cvslog] fftools/ffmpeg: make sure FrameData is writable when we modify it

Anton Khirnov git at videolan.org
Wed Dec 6 11:43:47 EET 2023


ffmpeg | branch: master | Anton Khirnov <anton at khirnov.net> | Mon Nov 13 09:35:44 2023 +0100| [99d2fa38ad62d27ea754489e1b13c487970f47d6] | committer: Anton Khirnov

fftools/ffmpeg: make sure FrameData is writable when we modify it

Also, add a function that returns const FrameData* for cases that only
read from it.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=99d2fa38ad62d27ea754489e1b13c487970f47d6
---

 fftools/ffmpeg.c        | 21 +++++++++++++++++----
 fftools/ffmpeg.h        |  2 ++
 fftools/ffmpeg_filter.c |  4 ++--
 3 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index cdb16ef90b..61fcda2526 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -427,21 +427,34 @@ InputStream *ist_iter(InputStream *prev)
     return NULL;
 }
 
-FrameData *frame_data(AVFrame *frame)
+static int frame_data_ensure(AVFrame *frame, int writable)
 {
     if (!frame->opaque_ref) {
         FrameData *fd;
 
         frame->opaque_ref = av_buffer_allocz(sizeof(*fd));
         if (!frame->opaque_ref)
-            return NULL;
+            return AVERROR(ENOMEM);
         fd = (FrameData*)frame->opaque_ref->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 0;
+}
 
-    return (FrameData*)frame->opaque_ref->data;
+FrameData *frame_data(AVFrame *frame)
+{
+    int ret = frame_data_ensure(frame, 1);
+    return ret < 0 ? NULL : (FrameData*)frame->opaque_ref->data;
+}
+
+const FrameData *frame_data_c(AVFrame *frame)
+{
+    int ret = frame_data_ensure(frame, 0);
+    return ret < 0 ? NULL : (const FrameData*)frame->opaque_ref->data;
 }
 
 void remove_avoptions(AVDictionary **a, AVDictionary *b)
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 41935d39d5..1f11a2f002 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -726,6 +726,8 @@ int subtitle_wrap_frame(AVFrame *frame, AVSubtitle *subtitle, int copy);
  */
 FrameData *frame_data(AVFrame *frame);
 
+const FrameData *frame_data_c(AVFrame *frame);
+
 int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame, int keep_reference);
 int ifilter_send_eof(InputFilter *ifilter, int64_t pts, AVRational tb);
 int ifilter_sub2video(InputFilter *ifilter, const AVFrame *frame);
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 0f7c076d59..3172ae25ec 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -1859,9 +1859,9 @@ static int choose_out_timebase(OutputFilterPriv *ofp, AVFrame *frame)
     FPSConvContext   *fps = &ofp->fps;
     AVRational        tb = (AVRational){ 0, 0 };
     AVRational fr;
-    FrameData *fd;
+    const FrameData *fd;
 
-    fd = frame_data(frame);
+    fd = frame_data_c(frame);
 
     // apply -enc_time_base
     if (ofp->enc_timebase.num == ENC_TIME_BASE_DEMUX &&



More information about the ffmpeg-cvslog mailing list