diff mbox series

[FFmpeg-devel] avfilter: use ff_inlink_make_frame_writable()

Message ID CAPYw7P4=BNy44p6TpTD4mdeOwFQRvVN44QENQbL_Fn19XugOfg@mail.gmail.com
State New
Headers show
Series [FFmpeg-devel] avfilter: use ff_inlink_make_frame_writable() | expand

Checks

Context Check Description
yinshiyou/configure_loongarch64 warning Failed to apply patch
andriy/make_x86 success Make finished
andriy/make_fate_x86 fail Make fate failed

Commit Message

Paul B Mahol Feb. 6, 2023, 2:12 p.m. UTC
Patch attached.

Comments

Nicolas George Feb. 6, 2023, 2:26 p.m. UTC | #1
Paul B Mahol (12023-02-06):
> Patch attached.

> From 8d2d4d35a54d0a3ddf300ef7196489898c3efc4e Mon Sep 17 00:00:00 2001
> From: Paul B Mahol <onemda@gmail.com>
> Date: Mon, 6 Feb 2023 14:57:50 +0100
> Subject: [PATCH] avfilter: use ff_inlink_make_frame_writable()
> 
> Signed-off-by: Paul B Mahol <onemda@gmail.com>
> ---
>  libavfilter/avf_abitscope.c       | 11 +++++++++--
>  libavfilter/avf_ahistogram.c      |  9 +++++++--
>  libavfilter/avf_aphasemeter.c     |  9 +++++++--
>  libavfilter/avf_avectorscope.c    |  7 ++++++-
>  libavfilter/avf_showspectrum.c    |  5 ++++-
>  libavfilter/avf_showvolume.c      |  6 ++++--
>  libavfilter/f_ebur128.c           | 10 ++++++++--
>  libavfilter/f_perms.c             |  3 ++-
>  libavfilter/framesync.c           |  2 +-
>  libavfilter/vf_cover_rect.c       |  7 +++++--
>  libavfilter/vf_dedot.c            |  2 +-
>  libavfilter/vf_floodfill.c        |  3 ++-
>  libavfilter/vf_lensfun.c          |  6 +++++-
>  libavfilter/vf_overlay_cuda.c     |  3 ++-
>  libavfilter/vf_paletteuse.c       |  2 +-
>  libavfilter/vf_photosensitivity.c |  3 ++-
>  libavfilter/vf_repeatfields.c     | 13 ++++++++++---
>  libavfilter/vf_signalstats.c      | 13 +++++++++----
>  libavfilter/vf_telecine.c         | 13 +++++++++++--
>  libavfilter/vf_vidstabdetect.c    | 10 +++++++---
>  20 files changed, 103 insertions(+), 34 deletions(-)
> 
> diff --git a/libavfilter/avf_abitscope.c b/libavfilter/avf_abitscope.c
> index 4fc3c06ecb..782d57e03a 100644
> --- a/libavfilter/avf_abitscope.c
> +++ b/libavfilter/avf_abitscope.c
> @@ -213,6 +213,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
>      AVFilterLink *outlink = ctx->outputs[0];
>      AudioBitScopeContext *s = ctx->priv;
>      AVFrame *outpicref;
> +    int ret;
>  
>      if (s->mode == 0 || !s->outpicref) {
>          outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
> @@ -228,10 +229,16 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
>      }
>  
>      if (s->mode == 1) {
> -        av_frame_make_writable(s->outpicref);
> +        ret = ff_inlink_make_frame_writable(outlink, &s->outpicref);
> +        if (ret < 0) {
> +            av_frame_free(&insamples);
> +            return ret;
> +        }
>          outpicref = av_frame_clone(s->outpicref);
> -        if (!outpicref)
> +        if (!outpicref) {
> +            av_frame_free(&insamples);
>              return AVERROR(ENOMEM);
> +        }
>      }
>  
>      outpicref->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base);
> diff --git a/libavfilter/avf_ahistogram.c b/libavfilter/avf_ahistogram.c
> index c45493730d..06490192a5 100644
> --- a/libavfilter/avf_ahistogram.c
> +++ b/libavfilter/avf_ahistogram.c
> @@ -209,7 +209,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>      AudioHistogramContext *s = ctx->priv;
>      const int H = s->histogram_h;
>      const int w = s->w;
> -    int c, y, n, p, bin;
> +    int c, y, n, p, bin, ret;
>      uint64_t acmax = 1;
>      AVFrame *clone;
>  
> @@ -229,7 +229,12 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>          }
>      }
>  
> -    av_frame_make_writable(s->out);
> +    ret = ff_inlink_make_frame_writable(outlink, &s->out);
> +    if (ret < 0) {
> +        av_frame_free(&in);
> +        return ret;
> +    }
> +
>      if (s->dmode == SEPARATE) {
>          for (y = 0; y < w; y++) {
>              s->combine_buffer[3 * y    ] = 0;
> diff --git a/libavfilter/avf_aphasemeter.c b/libavfilter/avf_aphasemeter.c
> index 0f7692982c..bf9f922639 100644
> --- a/libavfilter/avf_aphasemeter.c
> +++ b/libavfilter/avf_aphasemeter.c
> @@ -29,6 +29,7 @@
>  #include "libavutil/parseutils.h"
>  #include "libavutil/timestamp.h"
>  #include "avfilter.h"
> +#include "filters.h"
>  #include "formats.h"
>  #include "audio.h"
>  #include "video.h"
> @@ -246,7 +247,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>      float fphase = 0;
>      AVFrame *out;
>      uint8_t *dst;
> -    int i;
> +    int i, ret;
>      int mono_measurement;
>      int out_phase_measurement;
>      float tolerance = 1.0f - s->tolerance;
> @@ -265,8 +266,12 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>          for (i = 0; i < outlink->h; i++)
>              memset(out->data[0] + i * out->linesize[0], 0, outlink->w * 4);
>      } else if (s->do_video) {
> +        ret = ff_inlink_make_frame_writable(outlink, &s->out);
> +        if (ret < 0) {
> +            av_frame_free(&in);
> +            return ret;
> +        }
>          out = s->out;
> -        av_frame_make_writable(s->out);
>          for (i = outlink->h - 1; i >= 10; i--)
>              memmove(out->data[0] + (i  ) * out->linesize[0],
>                      out->data[0] + (i-1) * out->linesize[0],
> diff --git a/libavfilter/avf_avectorscope.c b/libavfilter/avf_avectorscope.c
> index 3927d80b42..6e45fd9575 100644
> --- a/libavfilter/avf_avectorscope.c
> +++ b/libavfilter/avf_avectorscope.c
> @@ -297,6 +297,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
>      unsigned x, y;
>      unsigned prev_x = s->prev_x, prev_y = s->prev_y;
>      double zoom = s->zoom;
> +    int ret;
>  
>      if (!s->outpicref || s->outpicref->width  != outlink->w ||
>                           s->outpicref->height != outlink->h) {
> @@ -314,7 +315,11 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
>      s->outpicref->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base);
>      s->outpicref->duration = 1;
>  
> -    av_frame_make_writable(s->outpicref);
> +    ret = ff_inlink_make_frame_writable(outlink, &s->outpicref);
> +    if (ret < 0) {
> +        av_frame_free(&insamples);
> +        return ret;
> +    }
>      ff_filter_execute(ctx, fade, NULL, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
>  
>      if (zoom < 1) {
> diff --git a/libavfilter/avf_showspectrum.c b/libavfilter/avf_showspectrum.c
> index 24a424a34a..4ce964706f 100644
> --- a/libavfilter/avf_showspectrum.c
> +++ b/libavfilter/avf_showspectrum.c
> @@ -1441,7 +1441,10 @@ static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples)
>          }
>      }
>  
> -    av_frame_make_writable(s->outpicref);
> +    ret = ff_inlink_make_frame_writable(outlink, &s->outpicref);
> +    if (ret < 0)
> +        return ret;
> +    outpicref = s->outpicref;
>      /* copy to output */
>      if (s->orientation == VERTICAL) {
>          if (s->sliding == SCROLL) {
> diff --git a/libavfilter/avf_showvolume.c b/libavfilter/avf_showvolume.c
> index 24d42d030d..645fc68cb0 100644
> --- a/libavfilter/avf_showvolume.c
> +++ b/libavfilter/avf_showvolume.c
> @@ -324,7 +324,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
>      AVFilterLink *outlink = ctx->outputs[0];
>      ShowVolumeContext *s = ctx->priv;
>      const int step = s->step;
> -    int c, j, k, max_draw;
> +    int c, j, k, max_draw, ret;
>      char channel_name[64];
>      AVFrame *out;
>  
> @@ -434,7 +434,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)

>      out = av_frame_clone(s->out);
>      if (!out)
>          return AVERROR(ENOMEM);
> -    av_frame_make_writable(out);
> +    ret = ff_inlink_make_frame_writable(outlink, &out);
> +    if (ret < 0)
> +        return ret;

Possible leak?

>  
>      /* draw volume level */
>      for (c = 0; c < inlink->ch_layout.nb_channels && s->h >= 8 && s->draw_volume; c++) {
> diff --git a/libavfilter/f_ebur128.c b/libavfilter/f_ebur128.c
> index 8afab37fdb..ef0fb4a52a 100644
> --- a/libavfilter/f_ebur128.c
> +++ b/libavfilter/f_ebur128.c
> @@ -618,7 +618,7 @@ static int gate_update(struct integrator *integ, double power,
>  
>  static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
>  {
> -    int i, ch, idx_insample;
> +    int i, ch, idx_insample, ret;
>      AVFilterContext *ctx = inlink->dst;
>      EBUR128Context *ebur128 = ctx->priv;
>      const int nb_channels = ebur128->nb_channels;
> @@ -821,7 +821,13 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
>                  y_loudness_lu_graph = lu_to_y(ebur128, loudness_3000 - ebur128->target);
>                  y_loudness_lu_gauge = lu_to_y(ebur128, gauge_value);
>  
> -                av_frame_make_writable(pic);
> +                ret = ff_inlink_make_frame_writable(outlink, &ebur128->outpicref);
> +                if (ret < 0) {
> +                    av_frame_free(&insamples);
> +                    ebur128->insamples = NULL;
> +                    return ret;
> +                }
> +                pic = ebur128->outpicref;
>                  /* draw the graph using the short-term loudness */
>                  p = pic->data[0] + ebur128->graph.y*pic->linesize[0] + ebur128->graph.x*3;
>                  for (y = 0; y < ebur128->graph.h; y++) {
> diff --git a/libavfilter/f_perms.c b/libavfilter/f_perms.c
> index e26a15fd06..95fb97f201 100644
> --- a/libavfilter/f_perms.c
> +++ b/libavfilter/f_perms.c
> @@ -24,6 +24,7 @@
>  #include "libavutil/opt.h"
>  #include "libavutil/random_seed.h"
>  #include "audio.h"
> +#include "filters.h"
>  #include "video.h"
>  
>  enum mode {
> @@ -96,7 +97,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
>             in_perm == out_perm ? " (no-op)" : "");
>  
>      if (in_perm == RO && out_perm == RW) {
> -        if ((ret = av_frame_make_writable(frame)) < 0)
> +        if ((ret = ff_inlink_make_frame_writable(inlink, &frame)) < 0)
>              return ret;
>      } else if (in_perm == RW && out_perm == RO) {
>          out = av_frame_clone(frame);
> diff --git a/libavfilter/framesync.c b/libavfilter/framesync.c
> index ee91e4cf68..422f4f7ad1 100644
> --- a/libavfilter/framesync.c
> +++ b/libavfilter/framesync.c
> @@ -288,7 +288,7 @@ int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe,
>          if (need_copy) {
>              if (!(frame = av_frame_clone(frame)))
>                  return AVERROR(ENOMEM);
> -            if ((ret = av_frame_make_writable(frame)) < 0) {
> +            if ((ret = ff_inlink_make_frame_writable(fs->parent->inputs[0], &frame) < 0)) {
>                  av_frame_free(&frame);
>                  return ret;
>              }
> diff --git a/libavfilter/vf_cover_rect.c b/libavfilter/vf_cover_rect.c
> index 01c9f2abbb..2fc5fc9693 100644
> --- a/libavfilter/vf_cover_rect.c
> +++ b/libavfilter/vf_cover_rect.c
> @@ -24,6 +24,7 @@
>  
>  #include "libavutil/imgutils.h"
>  #include "libavutil/opt.h"
> +#include "filters.h"
>  #include "internal.h"
>  
>  #include "lavfutils.h"
> @@ -125,7 +126,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>      AVFilterContext *ctx = inlink->dst;
>      CoverContext *cover = ctx->priv;
>      AVDictionaryEntry *ex, *ey, *ew, *eh;
> -    int x = -1, y = -1, w = -1, h = -1;
> +    int ret, x = -1, y = -1, w = -1, h = -1;
>      char *xendptr = NULL, *yendptr = NULL, *wendptr = NULL, *hendptr = NULL;
>  
>      ex = av_dict_get(in->metadata, "lavfi.rect.x", NULL, AV_DICT_MATCH_CASE);
> @@ -170,7 +171,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>      x = av_clip(x, 0, in->width  - w);
>      y = av_clip(y, 0, in->height - h);
>  

> -    av_frame_make_writable(in);
> +    ret = ff_inlink_make_frame_writable(inlink, &in);
> +    if (ret < 0)
> +        return ret;

Ditto?

>  
>      if (cover->mode == MODE_BLUR) {
>          blur (cover, in, x, y);
> diff --git a/libavfilter/vf_dedot.c b/libavfilter/vf_dedot.c
> index a0638f45b4..6ca47c262a 100644
> --- a/libavfilter/vf_dedot.c
> +++ b/libavfilter/vf_dedot.c
> @@ -289,7 +289,7 @@ static int activate(AVFilterContext *ctx)
>              s->frames[4]) {
>              out = av_frame_clone(s->frames[2]);
>              if (out && !ctx->is_disabled) {
> -                ret = av_frame_make_writable(out);
> +                ret = ff_inlink_make_frame_writable(inlink, &out);
>                  if (ret >= 0) {
>                      if (s->m & 1)
>                          ff_filter_execute(ctx, s->dedotcrawl, out, NULL,
> diff --git a/libavfilter/vf_floodfill.c b/libavfilter/vf_floodfill.c
> index da747c9f9f..952f1d15f1 100644
> --- a/libavfilter/vf_floodfill.c
> +++ b/libavfilter/vf_floodfill.c
> @@ -22,6 +22,7 @@
>  #include "libavutil/imgutils.h"
>  #include "libavutil/intreadwrite.h"
>  #include "avfilter.h"
> +#include "filters.h"
>  #include "formats.h"
>  #include "internal.h"
>  #include "video.h"
> @@ -315,7 +316,7 @@ static int filter_frame(AVFilterLink *link, AVFrame *frame)
>              s->front++;
>          }
>  
> -        if (ret = av_frame_make_writable(frame))
> +        if (ret = ff_inlink_make_frame_writable(link, &frame))
>              return ret;
>  
>          while (s->front > s->back) {
> diff --git a/libavfilter/vf_lensfun.c b/libavfilter/vf_lensfun.c
> index 35c522a723..52652e0c9b 100644
> --- a/libavfilter/vf_lensfun.c
> +++ b/libavfilter/vf_lensfun.c
> @@ -32,6 +32,7 @@
>  #include "libavutil/opt.h"
>  #include "libswscale/swscale.h"
>  #include "avfilter.h"
> +#include "filters.h"
>  #include "formats.h"
>  #include "internal.h"
>  #include "video.h"
> @@ -443,9 +444,12 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>      AVFrame *out;
>      VignettingThreadData vignetting_thread_data;
>      DistortionCorrectionThreadData distortion_correction_thread_data;
> +    int ret;
>  
>      if (lensfun->mode & VIGNETTING) {
> -        av_frame_make_writable(in);
> +        ret = ff_inlink_make_frame_writable(inlink, &in);
> +        if (ret < 0)
> +            return ret;

Ditto?

>  
>          vignetting_thread_data = (VignettingThreadData) {
>              .width = inlink->w,
> diff --git a/libavfilter/vf_overlay_cuda.c b/libavfilter/vf_overlay_cuda.c
> index 68c00405fb..b2cbb9c625 100644
> --- a/libavfilter/vf_overlay_cuda.c
> +++ b/libavfilter/vf_overlay_cuda.c
> @@ -32,6 +32,7 @@
>  #include "libavutil/eval.h"
>  
>  #include "avfilter.h"
> +#include "filters.h"
>  #include "framesync.h"
>  #include "internal.h"
>  
> @@ -252,7 +253,7 @@ static int overlay_cuda_blend(FFFrameSync *fs)
>      if (!input_overlay)
>          return ff_filter_frame(outlink, input_main);
>  
> -    ret = av_frame_make_writable(input_main);
> +    ret = ff_inlink_make_frame_writable(inlink, &input_main);
>      if (ret < 0) {
>          av_frame_free(&input_main);
>          return ret;
> diff --git a/libavfilter/vf_paletteuse.c b/libavfilter/vf_paletteuse.c
> index 944ff5c74d..5fa7a605ce 100644
> --- a/libavfilter/vf_paletteuse.c
> +++ b/libavfilter/vf_paletteuse.c
> @@ -783,7 +783,7 @@ static int apply_palette(AVFilterLink *inlink, AVFrame *in, AVFrame **outf)
>      av_frame_unref(s->last_out);
>      if ((ret = av_frame_ref(s->last_in, in))       < 0 ||
>          (ret = av_frame_ref(s->last_out, out))     < 0 ||
> -        (ret = av_frame_make_writable(s->last_in)) < 0) {
> +        (ret = ff_inlink_make_frame_writable(inlink, &s->last_in)) < 0) {
>          av_frame_free(&out);
>          *outf = NULL;
>          return ret;
> diff --git a/libavfilter/vf_photosensitivity.c b/libavfilter/vf_photosensitivity.c
> index 1bb984cc93..e05d4d0262 100644
> --- a/libavfilter/vf_photosensitivity.c
> +++ b/libavfilter/vf_photosensitivity.c
> @@ -25,6 +25,7 @@
>  #include "libavutil/pixdesc.h"
>  #include "avfilter.h"
>  
> +#include "filters.h"
>  #include "formats.h"
>  #include "internal.h"
>  #include "video.h"
> @@ -243,7 +244,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>              /* just duplicate the frame */
>              s->history[s->history_pos] = 0; /* frame was duplicated, thus, delta is zero */
>          } else {
> -            res = av_frame_make_writable(s->last_frame_av);
> +            res = ff_inlink_make_frame_writable(inlink, &s->last_frame_av);
>              if (res) {
>                  av_frame_free(&in);
>                  return res;
> diff --git a/libavfilter/vf_repeatfields.c b/libavfilter/vf_repeatfields.c
> index 9c02c61631..4ace5a18f7 100644
> --- a/libavfilter/vf_repeatfields.c
> +++ b/libavfilter/vf_repeatfields.c
> @@ -20,6 +20,7 @@
>  
>  #include "libavutil/imgutils.h"
>  #include "avfilter.h"
> +#include "filters.h"
>  #include "internal.h"
>  
>  typedef struct RepeatFieldsContext {
> @@ -110,7 +111,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) {
>          ret = ff_filter_frame(outlink, new);
>  
>          if (in->repeat_pict) {
> -            av_frame_make_writable(out);
> +            ret = ff_inlink_make_frame_writable(inlink, &out);
> +            if (ret < 0)
> +                return ret;

Ditto?

>              update_pts(outlink, out, in->pts, 2);
>              for (i = 0; i < s->nb_planes; i++) {
>                  av_image_copy_plane(out->data[i], out->linesize[i] * 2,
> @@ -121,7 +124,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) {
>          }
>      } else {
>          for (i = 0; i < s->nb_planes; i++) {
> -            av_frame_make_writable(out);
> +            ret = ff_inlink_make_frame_writable(inlink, &out);
> +            if (ret < 0)
> +                return ret;

Ditto?

>              av_image_copy_plane(out->data[i] + out->linesize[i], out->linesize[i] * 2,
>                                  in->data[i] + in->linesize[i], in->linesize[i] * 2,
>                                  s->linesize[i], s->planeheight[i] / 2);
> @@ -139,7 +144,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) {
>              ret = ff_filter_frame(outlink, new);
>              state = 0;
>          } else {
> -            av_frame_make_writable(out);
> +            ret = ff_inlink_make_frame_writable(inlink, &out);
> +            if (ret < 0)
> +                return ret;

Ditto?

>              update_pts(outlink, out, in->pts, 1);
>              for (i = 0; i < s->nb_planes; i++) {
>                  av_image_copy_plane(out->data[i], out->linesize[i] * 2,
> diff --git a/libavfilter/vf_signalstats.c b/libavfilter/vf_signalstats.c
> index e6f84be9ba..93ff378b31 100644
> --- a/libavfilter/vf_signalstats.c
> +++ b/libavfilter/vf_signalstats.c
> @@ -23,6 +23,7 @@
>  #include "libavutil/intreadwrite.h"
>  #include "libavutil/opt.h"
>  #include "libavutil/pixdesc.h"
> +#include "filters.h"
>  #include "internal.h"
>  
>  enum FilterMode {
> @@ -565,7 +566,7 @@ static int filter_frame8(AVFilterLink *link, AVFrame *in)
>      int tothue = 0;
>      int dify = 0, difu = 0, difv = 0;
>      uint16_t masky = 0, masku = 0, maskv = 0;
> -
> +    int ret;
>      int filtot[FILT_NUMB] = {0};
>      AVFrame *prev;
>  
> @@ -588,7 +589,9 @@ static int filter_frame8(AVFilterLink *link, AVFrame *in)
>  
>      if (s->outfilter != FILTER_NONE) {
>          out = av_frame_clone(in);
> -        av_frame_make_writable(out);
> +        ret = ff_inlink_make_frame_writable(link, &out);
> +        if (ret < 0)
> +            return ret;

Ditto?

>      }
>  
>      ff_filter_execute(ctx, compute_sat_hue_metrics8, &td_huesat,
> @@ -790,7 +793,7 @@ static int filter_frame16(AVFilterLink *link, AVFrame *in)
>  
>      int filtot[FILT_NUMB] = {0};
>      AVFrame *prev;
> -
> +    int ret;
>      AVFrame *sat = s->frame_sat;
>      AVFrame *hue = s->frame_hue;
>      const uint16_t *p_sat = (uint16_t *)sat->data[0];
> @@ -810,7 +813,9 @@ static int filter_frame16(AVFilterLink *link, AVFrame *in)
>  
>      if (s->outfilter != FILTER_NONE) {
>          out = av_frame_clone(in);
> -        av_frame_make_writable(out);
> +        ret = ff_inlink_make_frame_writable(link, &out);
> +        if (ret < 0)
> +            return ret;

Ditto?

>      }
>  
>      ff_filter_execute(ctx, compute_sat_hue_metrics16, &td_huesat,
> diff --git a/libavfilter/vf_telecine.c b/libavfilter/vf_telecine.c
> index e8de63bbcf..227de6f733 100644
> --- a/libavfilter/vf_telecine.c
> +++ b/libavfilter/vf_telecine.c
> @@ -29,6 +29,7 @@
>  #include "libavutil/opt.h"
>  #include "libavutil/pixdesc.h"
>  #include "avfilter.h"
> +#include "filters.h"
>  #include "formats.h"
>  #include "internal.h"
>  #include "video.h"
> @@ -182,7 +183,11 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
>      }
>  
>      if (s->occupied) {
> -        av_frame_make_writable(s->frame[nout]);
> +        ret = ff_inlink_make_frame_writable(inlink, &s->frame[nout]);
> +        if (ret < 0) {
> +            av_frame_free(&inpicref);
> +            return ret;
> +        }
>          for (i = 0; i < s->nb_planes; i++) {
>              // fill in the EARLIER field from the buffered pic
>              av_image_copy_plane(s->frame[nout]->data[i] + s->frame[nout]->linesize[i] * s->first_field,
> @@ -208,7 +213,11 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
>  
>      while (len >= 2) {
>          // output THIS image as-is
> -        av_frame_make_writable(s->frame[nout]);
> +        ret = ff_inlink_make_frame_writable(inlink, &s->frame[nout]);
> +        if (ret < 0) {
> +            av_frame_free(&inpicref);
> +            return ret;
> +        }
>          for (i = 0; i < s->nb_planes; i++)
>              av_image_copy_plane(s->frame[nout]->data[i], s->frame[nout]->linesize[i],
>                                  inpicref->data[i], inpicref->linesize[i],
> diff --git a/libavfilter/vf_vidstabdetect.c b/libavfilter/vf_vidstabdetect.c
> index 62b998e171..3eb1abab46 100644
> --- a/libavfilter/vf_vidstabdetect.c
> +++ b/libavfilter/vf_vidstabdetect.c
> @@ -27,6 +27,7 @@
>  #include "libavutil/opt.h"
>  #include "libavutil/pixdesc.h"
>  #include "avfilter.h"
> +#include "filters.h"
>  #include "internal.h"
>  
>  #include "vidstabutils.h"
> @@ -149,10 +150,13 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>  
>      AVFilterLink *outlink = inlink->dst->outputs[0];
>      VSFrame frame;
> -    int plane;
> +    int plane, ret;
>  
> -    if (s->conf.show > 0 && !av_frame_is_writable(in))
> -        av_frame_make_writable(in);
> +    if (s->conf.show > 0 && !av_frame_is_writable(in)) {
> +        ret = ff_inlink_make_frame_writable(inlink, &in);
> +        if (ret < 0)
> +            return ret;
> +    }
>  
>      for (plane = 0; plane < md->fi.planes; plane++) {
>          frame.data[plane] = in->data[plane];

No objections except for the possible leaks.
Paul B Mahol Feb. 7, 2023, 5:26 p.m. UTC | #2
On 2/6/23, Paul B Mahol <onemda@gmail.com> wrote:
> Patch attached.
>

Better patch attached.
Nicolas George Feb. 8, 2023, 9:34 a.m. UTC | #3
Paul B Mahol (12023-02-07):
> On 2/6/23, Paul B Mahol <onemda@gmail.com> wrote:
> > Patch attached.
> >
> 
> Better patch attached.

> From 15f004ccb196e39c6c429e2c93041444c1a1e419 Mon Sep 17 00:00:00 2001
> From: Paul B Mahol <onemda@gmail.com>
> Date: Mon, 6 Feb 2023 14:57:50 +0100
> Subject: [PATCH] avfilter: use ff_inlink_make_frame_writable()
> 
> Signed-off-by: Paul B Mahol <onemda@gmail.com>
> ---
>  libavfilter/avf_abitscope.c       | 11 ++++++++--
>  libavfilter/avf_ahistogram.c      |  9 ++++++--
>  libavfilter/avf_aphasemeter.c     |  9 ++++++--
>  libavfilter/avf_avectorscope.c    |  7 ++++++-
>  libavfilter/avf_showspectrum.c    |  5 ++++-
>  libavfilter/avf_showvolume.c      |  8 ++++++--
>  libavfilter/f_ebur128.c           | 10 +++++++--
>  libavfilter/f_perms.c             |  3 ++-
>  libavfilter/framesync.c           |  2 +-
>  libavfilter/vf_cover_rect.c       |  9 ++++++--
>  libavfilter/vf_dedot.c            |  2 +-
>  libavfilter/vf_floodfill.c        |  5 ++++-
>  libavfilter/vf_lensfun.c          |  8 +++++++-
>  libavfilter/vf_overlay_cuda.c     |  3 ++-
>  libavfilter/vf_paletteuse.c       |  2 +-
>  libavfilter/vf_photosensitivity.c |  3 ++-
>  libavfilter/vf_repeatfields.c     | 34 ++++++++++++++++++++++++-------
>  libavfilter/vf_signalstats.c      | 17 ++++++++++++----
>  libavfilter/vf_telecine.c         | 13 ++++++++++--
>  libavfilter/vf_vidstabdetect.c    | 12 ++++++++---
>  20 files changed, 134 insertions(+), 38 deletions(-)
> 
> diff --git a/libavfilter/avf_abitscope.c b/libavfilter/avf_abitscope.c
> index 4fc3c06ecb..782d57e03a 100644
> --- a/libavfilter/avf_abitscope.c
> +++ b/libavfilter/avf_abitscope.c
> @@ -213,6 +213,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
>      AVFilterLink *outlink = ctx->outputs[0];
>      AudioBitScopeContext *s = ctx->priv;
>      AVFrame *outpicref;
> +    int ret;
>  
>      if (s->mode == 0 || !s->outpicref) {
>          outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
> @@ -228,10 +229,16 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
>      }
>  
>      if (s->mode == 1) {
> -        av_frame_make_writable(s->outpicref);
> +        ret = ff_inlink_make_frame_writable(outlink, &s->outpicref);
> +        if (ret < 0) {
> +            av_frame_free(&insamples);
> +            return ret;
> +        }
>          outpicref = av_frame_clone(s->outpicref);
> -        if (!outpicref)
> +        if (!outpicref) {
> +            av_frame_free(&insamples);
>              return AVERROR(ENOMEM);
> +        }
>      }
>  
>      outpicref->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base);
> diff --git a/libavfilter/avf_ahistogram.c b/libavfilter/avf_ahistogram.c
> index c45493730d..06490192a5 100644
> --- a/libavfilter/avf_ahistogram.c
> +++ b/libavfilter/avf_ahistogram.c
> @@ -209,7 +209,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>      AudioHistogramContext *s = ctx->priv;
>      const int H = s->histogram_h;
>      const int w = s->w;
> -    int c, y, n, p, bin;
> +    int c, y, n, p, bin, ret;
>      uint64_t acmax = 1;
>      AVFrame *clone;
>  
> @@ -229,7 +229,12 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>          }
>      }
>  
> -    av_frame_make_writable(s->out);
> +    ret = ff_inlink_make_frame_writable(outlink, &s->out);
> +    if (ret < 0) {
> +        av_frame_free(&in);
> +        return ret;
> +    }
> +
>      if (s->dmode == SEPARATE) {
>          for (y = 0; y < w; y++) {
>              s->combine_buffer[3 * y    ] = 0;
> diff --git a/libavfilter/avf_aphasemeter.c b/libavfilter/avf_aphasemeter.c
> index 0f7692982c..bf9f922639 100644
> --- a/libavfilter/avf_aphasemeter.c
> +++ b/libavfilter/avf_aphasemeter.c
> @@ -29,6 +29,7 @@
>  #include "libavutil/parseutils.h"
>  #include "libavutil/timestamp.h"
>  #include "avfilter.h"
> +#include "filters.h"
>  #include "formats.h"
>  #include "audio.h"
>  #include "video.h"
> @@ -246,7 +247,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>      float fphase = 0;
>      AVFrame *out;
>      uint8_t *dst;
> -    int i;
> +    int i, ret;
>      int mono_measurement;
>      int out_phase_measurement;
>      float tolerance = 1.0f - s->tolerance;
> @@ -265,8 +266,12 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>          for (i = 0; i < outlink->h; i++)
>              memset(out->data[0] + i * out->linesize[0], 0, outlink->w * 4);
>      } else if (s->do_video) {
> +        ret = ff_inlink_make_frame_writable(outlink, &s->out);
> +        if (ret < 0) {
> +            av_frame_free(&in);
> +            return ret;
> +        }
>          out = s->out;
> -        av_frame_make_writable(s->out);
>          for (i = outlink->h - 1; i >= 10; i--)
>              memmove(out->data[0] + (i  ) * out->linesize[0],
>                      out->data[0] + (i-1) * out->linesize[0],
> diff --git a/libavfilter/avf_avectorscope.c b/libavfilter/avf_avectorscope.c
> index 3927d80b42..6e45fd9575 100644
> --- a/libavfilter/avf_avectorscope.c
> +++ b/libavfilter/avf_avectorscope.c
> @@ -297,6 +297,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
>      unsigned x, y;
>      unsigned prev_x = s->prev_x, prev_y = s->prev_y;
>      double zoom = s->zoom;
> +    int ret;
>  
>      if (!s->outpicref || s->outpicref->width  != outlink->w ||
>                           s->outpicref->height != outlink->h) {
> @@ -314,7 +315,11 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
>      s->outpicref->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base);
>      s->outpicref->duration = 1;
>  
> -    av_frame_make_writable(s->outpicref);
> +    ret = ff_inlink_make_frame_writable(outlink, &s->outpicref);
> +    if (ret < 0) {
> +        av_frame_free(&insamples);
> +        return ret;
> +    }
>      ff_filter_execute(ctx, fade, NULL, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
>  
>      if (zoom < 1) {
> diff --git a/libavfilter/avf_showspectrum.c b/libavfilter/avf_showspectrum.c
> index 24a424a34a..4ce964706f 100644
> --- a/libavfilter/avf_showspectrum.c
> +++ b/libavfilter/avf_showspectrum.c
> @@ -1441,7 +1441,10 @@ static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples)
>          }
>      }
>  
> -    av_frame_make_writable(s->outpicref);
> +    ret = ff_inlink_make_frame_writable(outlink, &s->outpicref);
> +    if (ret < 0)
> +        return ret;
> +    outpicref = s->outpicref;
>      /* copy to output */
>      if (s->orientation == VERTICAL) {
>          if (s->sliding == SCROLL) {
> diff --git a/libavfilter/avf_showvolume.c b/libavfilter/avf_showvolume.c
> index 24d42d030d..fa64d5237a 100644
> --- a/libavfilter/avf_showvolume.c
> +++ b/libavfilter/avf_showvolume.c
> @@ -324,7 +324,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
>      AVFilterLink *outlink = ctx->outputs[0];
>      ShowVolumeContext *s = ctx->priv;
>      const int step = s->step;
> -    int c, j, k, max_draw;
> +    int c, j, k, max_draw, ret;
>      char channel_name[64];
>      AVFrame *out;
>  
> @@ -434,7 +434,11 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
>      out = av_frame_clone(s->out);
>      if (!out)
>          return AVERROR(ENOMEM);
> -    av_frame_make_writable(out);
> +    ret = ff_inlink_make_frame_writable(outlink, &out);
> +    if (ret < 0) {
> +        av_frame_free(&out);
> +        return ret;
> +    }
>  
>      /* draw volume level */
>      for (c = 0; c < inlink->ch_layout.nb_channels && s->h >= 8 && s->draw_volume; c++) {
> diff --git a/libavfilter/f_ebur128.c b/libavfilter/f_ebur128.c
> index 8afab37fdb..ef0fb4a52a 100644
> --- a/libavfilter/f_ebur128.c
> +++ b/libavfilter/f_ebur128.c
> @@ -618,7 +618,7 @@ static int gate_update(struct integrator *integ, double power,
>  
>  static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
>  {

> -    int i, ch, idx_insample;
> +    int i, ch, idx_insample, ret;
>      AVFilterContext *ctx = inlink->dst;
>      EBUR128Context *ebur128 = ctx->priv;
>      const int nb_channels = ebur128->nb_channels;
> @@ -821,7 +821,13 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
>                  y_loudness_lu_graph = lu_to_y(ebur128, loudness_3000 - ebur128->target);
>                  y_loudness_lu_gauge = lu_to_y(ebur128, gauge_value);
>  
> -                av_frame_make_writable(pic);
> +                ret = ff_inlink_make_frame_writable(outlink, &ebur128->outpicref);
> +                if (ret < 0) {
> +                    av_frame_free(&insamples);
> +                    ebur128->insamples = NULL;
> +                    return ret;
> +                }
> +                pic = ebur128->outpicref;

You should remove the “pic = ebur128->outpicref” that happens when it is
declared, since it is now a dead assignment.

>                  /* draw the graph using the short-term loudness */
>                  p = pic->data[0] + ebur128->graph.y*pic->linesize[0] + ebur128->graph.x*3;
>                  for (y = 0; y < ebur128->graph.h; y++) {
> diff --git a/libavfilter/f_perms.c b/libavfilter/f_perms.c
> index e26a15fd06..95fb97f201 100644
> --- a/libavfilter/f_perms.c
> +++ b/libavfilter/f_perms.c
> @@ -24,6 +24,7 @@
>  #include "libavutil/opt.h"
>  #include "libavutil/random_seed.h"
>  #include "audio.h"
> +#include "filters.h"
>  #include "video.h"
>  
>  enum mode {
> @@ -96,7 +97,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
>             in_perm == out_perm ? " (no-op)" : "");
>  
>      if (in_perm == RO && out_perm == RW) {
> -        if ((ret = av_frame_make_writable(frame)) < 0)
> +        if ((ret = ff_inlink_make_frame_writable(inlink, &frame)) < 0)
>              return ret;
>      } else if (in_perm == RW && out_perm == RO) {
>          out = av_frame_clone(frame);
> diff --git a/libavfilter/framesync.c b/libavfilter/framesync.c
> index ee91e4cf68..422f4f7ad1 100644
> --- a/libavfilter/framesync.c
> +++ b/libavfilter/framesync.c
> @@ -288,7 +288,7 @@ int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe,
>          if (need_copy) {
>              if (!(frame = av_frame_clone(frame)))
>                  return AVERROR(ENOMEM);
> -            if ((ret = av_frame_make_writable(frame)) < 0) {
> +            if ((ret = ff_inlink_make_frame_writable(fs->parent->inputs[0], &frame) < 0)) {
>                  av_frame_free(&frame);
>                  return ret;
>              }
> diff --git a/libavfilter/vf_cover_rect.c b/libavfilter/vf_cover_rect.c
> index 01c9f2abbb..642747a351 100644
> --- a/libavfilter/vf_cover_rect.c
> +++ b/libavfilter/vf_cover_rect.c
> @@ -24,6 +24,7 @@
>  
>  #include "libavutil/imgutils.h"
>  #include "libavutil/opt.h"
> +#include "filters.h"
>  #include "internal.h"
>  
>  #include "lavfutils.h"
> @@ -125,7 +126,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>      AVFilterContext *ctx = inlink->dst;
>      CoverContext *cover = ctx->priv;
>      AVDictionaryEntry *ex, *ey, *ew, *eh;
> -    int x = -1, y = -1, w = -1, h = -1;
> +    int ret, x = -1, y = -1, w = -1, h = -1;
>      char *xendptr = NULL, *yendptr = NULL, *wendptr = NULL, *hendptr = NULL;
>  
>      ex = av_dict_get(in->metadata, "lavfi.rect.x", NULL, AV_DICT_MATCH_CASE);
> @@ -170,7 +171,11 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>      x = av_clip(x, 0, in->width  - w);
>      y = av_clip(y, 0, in->height - h);
>  
> -    av_frame_make_writable(in);
> +    ret = ff_inlink_make_frame_writable(inlink, &in);
> +    if (ret < 0) {
> +        av_frame_free(&in);
> +        return ret;
> +    }
>  
>      if (cover->mode == MODE_BLUR) {
>          blur (cover, in, x, y);
> diff --git a/libavfilter/vf_dedot.c b/libavfilter/vf_dedot.c
> index a0638f45b4..6ca47c262a 100644
> --- a/libavfilter/vf_dedot.c
> +++ b/libavfilter/vf_dedot.c
> @@ -289,7 +289,7 @@ static int activate(AVFilterContext *ctx)
>              s->frames[4]) {
>              out = av_frame_clone(s->frames[2]);
>              if (out && !ctx->is_disabled) {
> -                ret = av_frame_make_writable(out);
> +                ret = ff_inlink_make_frame_writable(inlink, &out);
>                  if (ret >= 0) {
>                      if (s->m & 1)
>                          ff_filter_execute(ctx, s->dedotcrawl, out, NULL,
> diff --git a/libavfilter/vf_floodfill.c b/libavfilter/vf_floodfill.c
> index da747c9f9f..212255a784 100644
> --- a/libavfilter/vf_floodfill.c
> +++ b/libavfilter/vf_floodfill.c
> @@ -22,6 +22,7 @@
>  #include "libavutil/imgutils.h"
>  #include "libavutil/intreadwrite.h"
>  #include "avfilter.h"
> +#include "filters.h"
>  #include "formats.h"
>  #include "internal.h"
>  #include "video.h"
> @@ -315,8 +316,10 @@ static int filter_frame(AVFilterLink *link, AVFrame *frame)
>              s->front++;
>          }
>  
> -        if (ret = av_frame_make_writable(frame))
> +        if (ret = ff_inlink_make_frame_writable(link, &frame)) {
> +            av_frame_free(&frame);
>              return ret;
> +        }
>  
>          while (s->front > s->back) {
>              int x, y;
> diff --git a/libavfilter/vf_lensfun.c b/libavfilter/vf_lensfun.c
> index 35c522a723..f544af773e 100644
> --- a/libavfilter/vf_lensfun.c
> +++ b/libavfilter/vf_lensfun.c
> @@ -32,6 +32,7 @@
>  #include "libavutil/opt.h"
>  #include "libswscale/swscale.h"
>  #include "avfilter.h"
> +#include "filters.h"
>  #include "formats.h"
>  #include "internal.h"
>  #include "video.h"
> @@ -443,9 +444,14 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>      AVFrame *out;
>      VignettingThreadData vignetting_thread_data;
>      DistortionCorrectionThreadData distortion_correction_thread_data;
> +    int ret;
>  
>      if (lensfun->mode & VIGNETTING) {
> -        av_frame_make_writable(in);
> +        ret = ff_inlink_make_frame_writable(inlink, &in);
> +        if (ret < 0) {
> +            av_frame_free(&in);
> +            return ret;
> +        }
>  
>          vignetting_thread_data = (VignettingThreadData) {
>              .width = inlink->w,
> diff --git a/libavfilter/vf_overlay_cuda.c b/libavfilter/vf_overlay_cuda.c
> index 68c00405fb..b2cbb9c625 100644
> --- a/libavfilter/vf_overlay_cuda.c
> +++ b/libavfilter/vf_overlay_cuda.c
> @@ -32,6 +32,7 @@
>  #include "libavutil/eval.h"
>  
>  #include "avfilter.h"
> +#include "filters.h"
>  #include "framesync.h"
>  #include "internal.h"
>  
> @@ -252,7 +253,7 @@ static int overlay_cuda_blend(FFFrameSync *fs)
>      if (!input_overlay)
>          return ff_filter_frame(outlink, input_main);
>  
> -    ret = av_frame_make_writable(input_main);
> +    ret = ff_inlink_make_frame_writable(inlink, &input_main);
>      if (ret < 0) {
>          av_frame_free(&input_main);
>          return ret;
> diff --git a/libavfilter/vf_paletteuse.c b/libavfilter/vf_paletteuse.c
> index 944ff5c74d..5fa7a605ce 100644
> --- a/libavfilter/vf_paletteuse.c
> +++ b/libavfilter/vf_paletteuse.c
> @@ -783,7 +783,7 @@ static int apply_palette(AVFilterLink *inlink, AVFrame *in, AVFrame **outf)
>      av_frame_unref(s->last_out);
>      if ((ret = av_frame_ref(s->last_in, in))       < 0 ||
>          (ret = av_frame_ref(s->last_out, out))     < 0 ||
> -        (ret = av_frame_make_writable(s->last_in)) < 0) {
> +        (ret = ff_inlink_make_frame_writable(inlink, &s->last_in)) < 0) {
>          av_frame_free(&out);
>          *outf = NULL;
>          return ret;
> diff --git a/libavfilter/vf_photosensitivity.c b/libavfilter/vf_photosensitivity.c
> index 1bb984cc93..e05d4d0262 100644
> --- a/libavfilter/vf_photosensitivity.c
> +++ b/libavfilter/vf_photosensitivity.c
> @@ -25,6 +25,7 @@
>  #include "libavutil/pixdesc.h"
>  #include "avfilter.h"
>  
> +#include "filters.h"
>  #include "formats.h"
>  #include "internal.h"
>  #include "video.h"
> @@ -243,7 +244,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>              /* just duplicate the frame */
>              s->history[s->history_pos] = 0; /* frame was duplicated, thus, delta is zero */
>          } else {
> -            res = av_frame_make_writable(s->last_frame_av);
> +            res = ff_inlink_make_frame_writable(inlink, &s->last_frame_av);
>              if (res) {
>                  av_frame_free(&in);
>                  return res;
> diff --git a/libavfilter/vf_repeatfields.c b/libavfilter/vf_repeatfields.c
> index 9c02c61631..4dbe3199e8 100644
> --- a/libavfilter/vf_repeatfields.c
> +++ b/libavfilter/vf_repeatfields.c
> @@ -20,6 +20,7 @@
>  
>  #include "libavutil/imgutils.h"
>  #include "avfilter.h"
> +#include "filters.h"
>  #include "internal.h"
>  
>  typedef struct RepeatFieldsContext {
> @@ -75,7 +76,8 @@ static void update_pts(AVFilterLink *link, AVFrame *f, int64_t pts, int fields)
>          f->pts = AV_NOPTS_VALUE;
>  }
>  
> -static int filter_frame(AVFilterLink *inlink, AVFrame *in) {
> +static int filter_frame(AVFilterLink *inlink, AVFrame *in)
> +{
>      AVFilterContext *ctx = inlink->dst;
>      AVFilterLink *outlink = inlink->dst->outputs[0];
>      RepeatFieldsContext *s = ctx->priv;
> @@ -85,8 +87,10 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) {
>  
>      if (!s->frame) {
>          s->frame = av_frame_clone(in);
> -        if (!s->frame)
> +        if (!s->frame) {
> +            av_frame_free(&in);
>              return AVERROR(ENOMEM);
> +        }
>          s->frame->pts = AV_NOPTS_VALUE;
>      }
>  
> @@ -104,13 +108,19 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) {
>          AVFrame *new;
>  
>          new = av_frame_clone(in);
> -        if (!new)
> +        if (!new) {
> +            av_frame_free(&in);
>              return AVERROR(ENOMEM);
> +        }
>  
>          ret = ff_filter_frame(outlink, new);
>  
>          if (in->repeat_pict) {
> -            av_frame_make_writable(out);
> +            ret = ff_inlink_make_frame_writable(inlink, &out);
> +            if (ret < 0) {
> +                av_frame_free(&in);
> +                return ret;
> +            }
>              update_pts(outlink, out, in->pts, 2);
>              for (i = 0; i < s->nb_planes; i++) {
>                  av_image_copy_plane(out->data[i], out->linesize[i] * 2,
> @@ -121,7 +131,11 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) {
>          }
>      } else {
>          for (i = 0; i < s->nb_planes; i++) {
> -            av_frame_make_writable(out);
> +            ret = ff_inlink_make_frame_writable(inlink, &out);
> +            if (ret < 0) {
> +                av_frame_free(&in);
> +                return ret;
> +            }
>              av_image_copy_plane(out->data[i] + out->linesize[i], out->linesize[i] * 2,
>                                  in->data[i] + in->linesize[i], in->linesize[i] * 2,
>                                  s->linesize[i], s->planeheight[i] / 2);
> @@ -133,13 +147,19 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) {
>              AVFrame *new;
>  
>              new = av_frame_clone(in);
> -            if (!new)
> +            if (!new) {
> +                av_frame_free(&in);
>                  return AVERROR(ENOMEM);
> +            }
>  
>              ret = ff_filter_frame(outlink, new);
>              state = 0;
>          } else {
> -            av_frame_make_writable(out);
> +            ret = ff_inlink_make_frame_writable(inlink, &out);
> +            if (ret < 0) {
> +                av_frame_free(&in);
> +                return ret;
> +            }
>              update_pts(outlink, out, in->pts, 1);
>              for (i = 0; i < s->nb_planes; i++) {
>                  av_image_copy_plane(out->data[i], out->linesize[i] * 2,
> diff --git a/libavfilter/vf_signalstats.c b/libavfilter/vf_signalstats.c
> index e6f84be9ba..09a93010da 100644
> --- a/libavfilter/vf_signalstats.c
> +++ b/libavfilter/vf_signalstats.c
> @@ -23,6 +23,7 @@
>  #include "libavutil/intreadwrite.h"
>  #include "libavutil/opt.h"
>  #include "libavutil/pixdesc.h"
> +#include "filters.h"
>  #include "internal.h"
>  
>  enum FilterMode {
> @@ -565,7 +566,7 @@ static int filter_frame8(AVFilterLink *link, AVFrame *in)
>      int tothue = 0;
>      int dify = 0, difu = 0, difv = 0;
>      uint16_t masky = 0, masku = 0, maskv = 0;
> -
> +    int ret;
>      int filtot[FILT_NUMB] = {0};
>      AVFrame *prev;
>  
> @@ -588,7 +589,11 @@ static int filter_frame8(AVFilterLink *link, AVFrame *in)
>  
>      if (s->outfilter != FILTER_NONE) {
>          out = av_frame_clone(in);
> -        av_frame_make_writable(out);
> +        ret = ff_inlink_make_frame_writable(link, &out);
> +        if (ret < 0) {
> +            av_frame_free(&in);
> +            return ret;
> +        }
>      }
>  
>      ff_filter_execute(ctx, compute_sat_hue_metrics8, &td_huesat,
> @@ -790,7 +795,7 @@ static int filter_frame16(AVFilterLink *link, AVFrame *in)
>  
>      int filtot[FILT_NUMB] = {0};
>      AVFrame *prev;
> -
> +    int ret;
>      AVFrame *sat = s->frame_sat;
>      AVFrame *hue = s->frame_hue;
>      const uint16_t *p_sat = (uint16_t *)sat->data[0];
> @@ -810,7 +815,11 @@ static int filter_frame16(AVFilterLink *link, AVFrame *in)
>  
>      if (s->outfilter != FILTER_NONE) {
>          out = av_frame_clone(in);
> -        av_frame_make_writable(out);
> +        ret = ff_inlink_make_frame_writable(link, &out);
> +        if (ret < 0) {
> +            av_frame_free(&in);
> +            return ret;
> +        }
>      }
>  
>      ff_filter_execute(ctx, compute_sat_hue_metrics16, &td_huesat,
> diff --git a/libavfilter/vf_telecine.c b/libavfilter/vf_telecine.c
> index e8de63bbcf..227de6f733 100644
> --- a/libavfilter/vf_telecine.c
> +++ b/libavfilter/vf_telecine.c
> @@ -29,6 +29,7 @@
>  #include "libavutil/opt.h"
>  #include "libavutil/pixdesc.h"
>  #include "avfilter.h"
> +#include "filters.h"
>  #include "formats.h"
>  #include "internal.h"
>  #include "video.h"
> @@ -182,7 +183,11 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
>      }
>  
>      if (s->occupied) {
> -        av_frame_make_writable(s->frame[nout]);
> +        ret = ff_inlink_make_frame_writable(inlink, &s->frame[nout]);
> +        if (ret < 0) {
> +            av_frame_free(&inpicref);
> +            return ret;
> +        }
>          for (i = 0; i < s->nb_planes; i++) {
>              // fill in the EARLIER field from the buffered pic
>              av_image_copy_plane(s->frame[nout]->data[i] + s->frame[nout]->linesize[i] * s->first_field,
> @@ -208,7 +213,11 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
>  
>      while (len >= 2) {
>          // output THIS image as-is
> -        av_frame_make_writable(s->frame[nout]);
> +        ret = ff_inlink_make_frame_writable(inlink, &s->frame[nout]);
> +        if (ret < 0) {
> +            av_frame_free(&inpicref);
> +            return ret;
> +        }
>          for (i = 0; i < s->nb_planes; i++)
>              av_image_copy_plane(s->frame[nout]->data[i], s->frame[nout]->linesize[i],
>                                  inpicref->data[i], inpicref->linesize[i],
> diff --git a/libavfilter/vf_vidstabdetect.c b/libavfilter/vf_vidstabdetect.c
> index 62b998e171..b27b1e40a6 100644
> --- a/libavfilter/vf_vidstabdetect.c
> +++ b/libavfilter/vf_vidstabdetect.c
> @@ -27,6 +27,7 @@
>  #include "libavutil/opt.h"
>  #include "libavutil/pixdesc.h"
>  #include "avfilter.h"
> +#include "filters.h"
>  #include "internal.h"
>  
>  #include "vidstabutils.h"
> @@ -149,10 +150,15 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>  
>      AVFilterLink *outlink = inlink->dst->outputs[0];
>      VSFrame frame;
> -    int plane;
> +    int plane, ret;
>  
> -    if (s->conf.show > 0 && !av_frame_is_writable(in))
> -        av_frame_make_writable(in);
> +    if (s->conf.show > 0 && !av_frame_is_writable(in)) {
> +        ret = ff_inlink_make_frame_writable(inlink, &in);
> +        if (ret < 0) {
> +            av_frame_free(&in);
> +            return ret;
> +        }
> +    }
>  
>      for (plane = 0; plane < md->fi.planes; plane++) {
>          frame.data[plane] = in->data[plane];

No other remark from me, thanks.

Regards,
Michael Niedermayer Feb. 8, 2023, 11:10 p.m. UTC | #4
On Tue, Feb 07, 2023 at 06:26:17PM +0100, Paul B Mahol wrote:
> On 2/6/23, Paul B Mahol <onemda@gmail.com> wrote:
> > Patch attached.
> >
> 
> Better patch attached.

>  avf_abitscope.c       |   11 +++++++++--
>  avf_ahistogram.c      |    9 +++++++--
>  avf_aphasemeter.c     |    9 +++++++--
>  avf_avectorscope.c    |    7 ++++++-
>  avf_showspectrum.c    |    5 ++++-
>  avf_showvolume.c      |    8 ++++++--
>  f_ebur128.c           |   10 ++++++++--
>  f_perms.c             |    3 ++-
>  framesync.c           |    2 +-
>  vf_cover_rect.c       |    9 +++++++--
>  vf_dedot.c            |    2 +-
>  vf_floodfill.c        |    5 ++++-
>  vf_lensfun.c          |    8 +++++++-
>  vf_overlay_cuda.c     |    3 ++-
>  vf_paletteuse.c       |    2 +-
>  vf_photosensitivity.c |    3 ++-
>  vf_repeatfields.c     |   34 +++++++++++++++++++++++++++-------
>  vf_signalstats.c      |   17 +++++++++++++----
>  vf_telecine.c         |   13 +++++++++++--
>  vf_vidstabdetect.c    |   12 +++++++++---
>  20 files changed, 134 insertions(+), 38 deletions(-)
> 8aabbe97177a141f13eb044580349e7ae1779b7f  0001-avfilter-use-ff_inlink_make_frame_writable.patch
> From 15f004ccb196e39c6c429e2c93041444c1a1e419 Mon Sep 17 00:00:00 2001
> From: Paul B Mahol <onemda@gmail.com>
> Date: Mon, 6 Feb 2023 14:57:50 +0100
> Subject: [PATCH] avfilter: use ff_inlink_make_frame_writable()
> 
> Signed-off-by: Paul B Mahol <onemda@gmail.com>
> ---

Seems to segfault here in fate unless i missed some patch 

==14233== Command: ./ffmpeg_g -nostdin -nostats -noauto_conversion_filters -cpuflags all -auto_conversion_filters -idct simple -hwaccel none -threads 1 -thread_type frame+slice -i fate-suite//smjpeg/scenwin.mjpg -vf trim=duration=0.5,perms=random,owdenoise=10:20:20:enable=not(between(t\\,0.2\\,1.2)) -an -f rawvideo -y /dev/null
==14233== 
ffmpeg version N-109788-g09fec9674b Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 7 (Ubuntu 7.5.0-3ubuntu1~18.04)
  configuration: --enable-libspeex --enable-libvpx --enable-gpl --cc='ccache gcc' --enable-libopenjpeg --enable-pthreads --enable-debug=3 --cpu=core2 --enable-libvorbis --samples=fate-suite/ --enable-libx264 --enable-debug=3 --enable-libdc1394 --enable-libxvid --enable-libfreetype --enable-nonfree --enable-libmodplug --enable-libpulse --enable-libmp3lame --enable-version3 --enable-libbluray --enable-libxml2 --assert-level=2 --enable-libtheora --enable-openssl --enable-libcaca --enable-libopus --enable-libcdio --enable-openal --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-libgsm --enable-libass --enable-frei0r --enable-libsoxr --enable-libfdk-aac --disable-optimizations --disable-stripping --extra-cflags='-Dav_always_inline=inline'
  libavutil      57. 44.100 / 57. 44.100
  libavcodec     59. 63.100 / 59. 63.100
  libavformat    59. 38.100 / 59. 38.100
  libavdevice    59.  8.101 / 59.  8.101
  libavfilter     8. 56.100 /  8. 56.100
  libswscale      6.  8.112 /  6.  8.112
  libswresample   4.  9.100 /  4.  9.100
  libpostproc    56.  7.100 / 56.  7.100
[mjpeg @ 0x16a0cf00] EOI missing, emulating
Guessed Channel Layout for Input Stream #0.0 : mono
Input #0, smjpeg, from 'fate-suite//smjpeg/scenwin.mjpg':
  Duration: 00:00:08.11, start: 0.000000, bitrate: 557 kb/s
  Stream #0:0: Audio: adpcm_ima_smjpeg (APCM / 0x4D435041), 22050 Hz, 1 channels, s16
  Stream #0:1: Video: mjpeg (Baseline) (JFIF / 0x4649464A), yuvj420p(pc, bt470bg/unknown/unknown), 320x240 [SAR 1:1 DAR 4:3], 9 fps, 9 tbr, 1k tbn
Stream mapping:
  Stream #0:1 -> #0:0 (mjpeg (native) -> rawvideo (native))
[Parsed_perms_1 @ 0x16b61380] random seed: 0x4e2ee912
[swscaler @ 0x16b9df40] deprecated pixel format used, make sure you did set range correctly
==14233== Invalid read of size 4
==14233==    at 0x2C1C4E: ff_filter_frame (avfilter.c:1011)
==14233==    by 0x2D4842: filter_frame (f_perms.c:108)
==14233==    by 0x2C1A24: ff_filter_frame_framed (avfilter.c:969)
==14233==    by 0x2C20AD: ff_filter_frame_to_filter (avfilter.c:1113)
==14233==    by 0x2C22BD: ff_filter_activate_default (avfilter.c:1162)
==14233==    by 0x2C241D: ff_filter_activate (avfilter.c:1320)
==14233==    by 0x2C70AF: ff_filter_graph_run_once (avfiltergraph.c:1352)
==14233==    by 0x2C861F: push_frame (buffersrc.c:169)
==14233==    by 0x2C8C8C: av_buffersrc_add_frame_flags (buffersrc.c:258)
==14233==    by 0x26F938: ifilter_send_frame (ffmpeg.c:2038)
==14233==    by 0x26FCDC: send_frame_to_filters (ffmpeg.c:2124)
==14233==    by 0x2709D1: decode_video (ffmpeg.c:2310)
==14233==    by 0x27190B: process_input_packet (ffmpeg.c:2599)
==14233==    by 0x276B57: process_input (ffmpeg.c:3859)
==14233==    by 0x277012: transcode_step (ffmpeg.c:3994)
==14233==    by 0x277156: transcode (ffmpeg.c:4041)
==14233==    by 0x27768C: main (ffmpeg.c:4179)
==14233==  Address 0x16dc6770 is 112 bytes inside a block of size 480 free'd
==14233==    at 0x4C32D3B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14233==    by 0x12B1201: av_free (mem.c:251)
==14233==    by 0x12B1249: av_freep (mem.c:261)
==14233==    by 0x12A1723: av_frame_free (frame.c:122)
==14233==    by 0x2C2999: ff_inlink_make_frame_writable (avfilter.c:1452)
==14233==    by 0x2D47F0: filter_frame (f_perms.c:100)
==14233==    by 0x2C1A24: ff_filter_frame_framed (avfilter.c:969)
==14233==    by 0x2C20AD: ff_filter_frame_to_filter (avfilter.c:1113)
==14233==    by 0x2C22BD: ff_filter_activate_default (avfilter.c:1162)
==14233==    by 0x2C241D: ff_filter_activate (avfilter.c:1320)
==14233==    by 0x2C70AF: ff_filter_graph_run_once (avfiltergraph.c:1352)
==14233==    by 0x2C861F: push_frame (buffersrc.c:169)
==14233==    by 0x2C8C8C: av_buffersrc_add_frame_flags (buffersrc.c:258)
==14233==    by 0x26F938: ifilter_send_frame (ffmpeg.c:2038)
==14233==    by 0x26FCDC: send_frame_to_filters (ffmpeg.c:2124)
==14233==    by 0x2709D1: decode_video (ffmpeg.c:2310)
==14233==    by 0x27190B: process_input_packet (ffmpeg.c:2599)
==14233==    by 0x276B57: process_input (ffmpeg.c:3859)
==14233==    by 0x277012: transcode_step (ffmpeg.c:3994)
==14233==    by 0x277156: transcode (ffmpeg.c:4041)
==14233==  Block was alloc'd at
==14233==    at 0x4C33E76: memalign (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14233==    by 0x4C33F91: posix_memalign (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14233==    by 0x12B0E25: av_malloc (mem.c:105)
==14233==    by 0x12A16C5: av_frame_alloc (frame.c:106)
==14233==    by 0x2C8B55: av_buffersrc_add_frame_flags (buffersrc.c:233)
==14233==    by 0x26F938: ifilter_send_frame (ffmpeg.c:2038)
==14233==    by 0x26FCDC: send_frame_to_filters (ffmpeg.c:2124)
==14233==    by 0x2709D1: decode_video (ffmpeg.c:2310)
==14233==    by 0x27190B: process_input_packet (ffmpeg.c:2599)
==14233==    by 0x276B57: process_input (ffmpeg.c:3859)
==14233==    by 0x277012: transcode_step (ffmpeg.c:3994)
==14233==    by 0x277156: transcode (ffmpeg.c:4041)
==14233==    by 0x27768C: main (ffmpeg.c:4179)


[...]
Paul B Mahol Feb. 9, 2023, 7:14 a.m. UTC | #5
New patch attached.
Michael Niedermayer Feb. 11, 2023, 12:19 a.m. UTC | #6
On Thu, Feb 09, 2023 at 08:14:16AM +0100, Paul B Mahol wrote:
> New patch attached.

>  avf_abitscope.c       |   11 +++++++++--
>  avf_ahistogram.c      |    9 +++++++--
>  avf_aphasemeter.c     |    9 +++++++--
>  avf_avectorscope.c    |    7 ++++++-
>  avf_showspectrum.c    |    5 ++++-
>  avf_showvolume.c      |    8 ++++++--
>  f_ebur128.c           |   12 +++++++++---
>  f_perms.c             |    4 +++-
>  framesync.c           |    2 +-
>  vf_cover_rect.c       |    9 +++++++--
>  vf_dedot.c            |    2 +-
>  vf_floodfill.c        |    5 ++++-
>  vf_lensfun.c          |    8 +++++++-
>  vf_overlay_cuda.c     |    3 ++-
>  vf_paletteuse.c       |    2 +-
>  vf_photosensitivity.c |    3 ++-
>  vf_repeatfields.c     |   34 +++++++++++++++++++++++++++-------
>  vf_signalstats.c      |   27 +++++++++++++++++++++++----
>  vf_telecine.c         |   13 +++++++++++--
>  vf_vidstabdetect.c    |   12 +++++++++---
>  20 files changed, 146 insertions(+), 39 deletions(-)
> d8b08205d8901abb290c603b9ea5d71264ba1b2d  0001-avfilter-use-ff_inlink_make_frame_writable.patch
> From 86203516a4d38f312ea5319315aa79841a39a45b Mon Sep 17 00:00:00 2001
> From: Paul B Mahol <onemda@gmail.com>
> Date: Mon, 6 Feb 2023 14:57:50 +0100
> Subject: [PATCH] avfilter: use ff_inlink_make_frame_writable()
> 
> Signed-off-by: Paul B Mahol <onemda@gmail.com>

new failure:
-i interlaced_flag_switch.mpeg  -vf repeatfields  -t 2 -vcodec huffyuv -bitexact -y /tmp/file-file-repeatfields.avi

[huffyuv @ 0x386098c0] using huffyuv 2.2.0 or newer interlacing flag
[huffyuv @ 0x38612800] using huffyuv 2.2.0 or newer interlacing flag
[huffyuv @ 0x3a9ed9c0] using huffyuv 2.2.0 or newer interlacing flag
[huffyuv @ 0x3a9f4cc0] using huffyuv 2.2.0 or newer interlacing flag
[huffyuv @ 0x3a9fdc00] using huffyuv 2.2.0 or newer interlacing flag
[huffyuv @ 0x3aa06b40] using huffyuv 2.2.0 or newer interlacing flag
[huffyuv @ 0x3aa0fa80] using huffyuv 2.2.0 or newer interlacing flag
[huffyuv @ 0x16e86bc0] using huffyuv 2.2.0 or newer interlacing flag
Output #0, avi, to '/tmp/file-file-repeatfields.avi':
  Stream #0:0: Video: huffyuv (HFYU / 0x55594648), yuv422p(tv, progressive), 720x480 [SAR 8:9 DAR 4:3], q=2-31, 200 kb/s, 29.97 fps, 29.97 tbn
    Metadata:
      encoder         : Lavc huffyuv
frame=    0 fps=0.0 q=0.0 size=       0kB time=-577014:32:22.77 bitrate=  -0.0kbits/s speed=N/A    ^M==15866== Invalid read of size 8
==15866==    at 0x12A48B9: av_frame_is_writable (frame.c:525)
==15866==    by 0x2C2787: ff_inlink_make_frame_writable (avfilter.c:1402)
==15866==    by 0x441284: filter_frame (vf_repeatfields.c:134)
==15866==    by 0x2C1910: ff_filter_frame_framed (avfilter.c:948)
==15866==    by 0x2C1F99: ff_filter_frame_to_filter (avfilter.c:1092)
==15866==    by 0x2C21A9: ff_filter_activate_default (avfilter.c:1141)
==15866==    by 0x2C2309: ff_filter_activate (avfilter.c:1299)
==15866==    by 0x2C6F9B: ff_filter_graph_run_once (avfiltergraph.c:1352)
==15866==    by 0x2C84A9: push_frame (buffersrc.c:166)
==15866==    by 0x2C8B16: av_buffersrc_add_frame_flags (buffersrc.c:255)
==15866==    by 0x26FE7D: ifilter_send_frame (ffmpeg.c:2047)
==15866==    by 0x270222: send_frame_to_filters (ffmpeg.c:2133)
==15866==    by 0x270F1A: decode_video (ffmpeg.c:2319)
==15866==    by 0x271E54: process_input_packet (ffmpeg.c:2608)
==15866==    by 0x276C31: process_input (ffmpeg.c:3857)
==15866==    by 0x2770EC: transcode_step (ffmpeg.c:3992)
==15866==    by 0x277230: transcode (ffmpeg.c:4039)
==15866==    by 0x277766: main (ffmpeg.c:4177)
==15866==  Address 0x2eb09d60 is 224 bytes inside a block of size 480 free'd
==15866==    at 0x4C32D3B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15866==    by 0x12B310B: av_free (mem.c:241)
==15866==    by 0x12B3153: av_freep (mem.c:251)
==15866==    by 0x12A3690: av_frame_free (frame.c:105)
==15866==    by 0x2C2885: ff_inlink_make_frame_writable (avfilter.c:1431)
==15866==    by 0x44117F: filter_frame (vf_repeatfields.c:119)
==15866==    by 0x2C1910: ff_filter_frame_framed (avfilter.c:948)
==15866==    by 0x2C1F99: ff_filter_frame_to_filter (avfilter.c:1092)
==15866==    by 0x2C21A9: ff_filter_activate_default (avfilter.c:1141)
==15866==    by 0x2C2309: ff_filter_activate (avfilter.c:1299)
==15866==    by 0x2C6F9B: ff_filter_graph_run_once (avfiltergraph.c:1352)
==15866==    by 0x2C84A9: push_frame (buffersrc.c:166)
==15866==    by 0x2C8B16: av_buffersrc_add_frame_flags (buffersrc.c:255)
==15866==    by 0x26FE7D: ifilter_send_frame (ffmpeg.c:2047)
==15866==    by 0x270222: send_frame_to_filters (ffmpeg.c:2133)
==15866==    by 0x270F1A: decode_video (ffmpeg.c:2319)
==15866==    by 0x271E54: process_input_packet (ffmpeg.c:2608)
==15866==    by 0x276C31: process_input (ffmpeg.c:3857)
==15866==    by 0x2770EC: transcode_step (ffmpeg.c:3992)
==15866==    by 0x277230: transcode (ffmpeg.c:4039)
==15866==  Block was alloc'd at
==15866==    at 0x4C33E76: memalign (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15866==    by 0x4C33F91: posix_memalign (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15866==    by 0x12B2D92: av_malloc (mem.c:105)
==15866==    by 0x12A3632: av_frame_alloc (frame.c:89)
==15866==    by 0x12A46C7: av_frame_clone (frame.c:463)
==15866==    by 0x441052: filter_frame (vf_repeatfields.c:89)
==15866==    by 0x2C1910: ff_filter_frame_framed (avfilter.c:948)
==15866==    by 0x2C1F99: ff_filter_frame_to_filter (avfilter.c:1092)
==15866==    by 0x2C21A9: ff_filter_activate_default (avfilter.c:1141)
==15866==    by 0x2C2309: ff_filter_activate (avfilter.c:1299)
==15866==    by 0x2C6F9B: ff_filter_graph_run_once (avfiltergraph.c:1352)
==15866==    by 0x2C84A9: push_frame (buffersrc.c:166)
==15866==    by 0x2C8B16: av_buffersrc_add_frame_flags (buffersrc.c:255)
==15866==    by 0x26FE7D: ifilter_send_frame (ffmpeg.c:2047)
==15866==    by 0x270222: send_frame_to_filters (ffmpeg.c:2133)
==15866==    by 0x270F1A: decode_video (ffmpeg.c:2319)
==15866==    by 0x271E54: process_input_packet (ffmpeg.c:2608)
==15866==    by 0x276C31: process_input (ffmpeg.c:3857)
==15866==    by 0x2770EC: transcode_step (ffmpeg.c:3992)
==15866==    by 0x277230: transcode (ffmpeg.c:4039)



[...]
Paul B Mahol Feb. 11, 2023, 10:09 a.m. UTC | #7
FIxed, will apply soon.
diff mbox series

Patch

From 8d2d4d35a54d0a3ddf300ef7196489898c3efc4e Mon Sep 17 00:00:00 2001
From: Paul B Mahol <onemda@gmail.com>
Date: Mon, 6 Feb 2023 14:57:50 +0100
Subject: [PATCH] avfilter: use ff_inlink_make_frame_writable()

Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 libavfilter/avf_abitscope.c       | 11 +++++++++--
 libavfilter/avf_ahistogram.c      |  9 +++++++--
 libavfilter/avf_aphasemeter.c     |  9 +++++++--
 libavfilter/avf_avectorscope.c    |  7 ++++++-
 libavfilter/avf_showspectrum.c    |  5 ++++-
 libavfilter/avf_showvolume.c      |  6 ++++--
 libavfilter/f_ebur128.c           | 10 ++++++++--
 libavfilter/f_perms.c             |  3 ++-
 libavfilter/framesync.c           |  2 +-
 libavfilter/vf_cover_rect.c       |  7 +++++--
 libavfilter/vf_dedot.c            |  2 +-
 libavfilter/vf_floodfill.c        |  3 ++-
 libavfilter/vf_lensfun.c          |  6 +++++-
 libavfilter/vf_overlay_cuda.c     |  3 ++-
 libavfilter/vf_paletteuse.c       |  2 +-
 libavfilter/vf_photosensitivity.c |  3 ++-
 libavfilter/vf_repeatfields.c     | 13 ++++++++++---
 libavfilter/vf_signalstats.c      | 13 +++++++++----
 libavfilter/vf_telecine.c         | 13 +++++++++++--
 libavfilter/vf_vidstabdetect.c    | 10 +++++++---
 20 files changed, 103 insertions(+), 34 deletions(-)

diff --git a/libavfilter/avf_abitscope.c b/libavfilter/avf_abitscope.c
index 4fc3c06ecb..782d57e03a 100644
--- a/libavfilter/avf_abitscope.c
+++ b/libavfilter/avf_abitscope.c
@@ -213,6 +213,7 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
     AVFilterLink *outlink = ctx->outputs[0];
     AudioBitScopeContext *s = ctx->priv;
     AVFrame *outpicref;
+    int ret;
 
     if (s->mode == 0 || !s->outpicref) {
         outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
@@ -228,10 +229,16 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
     }
 
     if (s->mode == 1) {
-        av_frame_make_writable(s->outpicref);
+        ret = ff_inlink_make_frame_writable(outlink, &s->outpicref);
+        if (ret < 0) {
+            av_frame_free(&insamples);
+            return ret;
+        }
         outpicref = av_frame_clone(s->outpicref);
-        if (!outpicref)
+        if (!outpicref) {
+            av_frame_free(&insamples);
             return AVERROR(ENOMEM);
+        }
     }
 
     outpicref->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base);
diff --git a/libavfilter/avf_ahistogram.c b/libavfilter/avf_ahistogram.c
index c45493730d..06490192a5 100644
--- a/libavfilter/avf_ahistogram.c
+++ b/libavfilter/avf_ahistogram.c
@@ -209,7 +209,7 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     AudioHistogramContext *s = ctx->priv;
     const int H = s->histogram_h;
     const int w = s->w;
-    int c, y, n, p, bin;
+    int c, y, n, p, bin, ret;
     uint64_t acmax = 1;
     AVFrame *clone;
 
@@ -229,7 +229,12 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *in)
         }
     }
 
-    av_frame_make_writable(s->out);
+    ret = ff_inlink_make_frame_writable(outlink, &s->out);
+    if (ret < 0) {
+        av_frame_free(&in);
+        return ret;
+    }
+
     if (s->dmode == SEPARATE) {
         for (y = 0; y < w; y++) {
             s->combine_buffer[3 * y    ] = 0;
diff --git a/libavfilter/avf_aphasemeter.c b/libavfilter/avf_aphasemeter.c
index 0f7692982c..bf9f922639 100644
--- a/libavfilter/avf_aphasemeter.c
+++ b/libavfilter/avf_aphasemeter.c
@@ -29,6 +29,7 @@ 
 #include "libavutil/parseutils.h"
 #include "libavutil/timestamp.h"
 #include "avfilter.h"
+#include "filters.h"
 #include "formats.h"
 #include "audio.h"
 #include "video.h"
@@ -246,7 +247,7 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     float fphase = 0;
     AVFrame *out;
     uint8_t *dst;
-    int i;
+    int i, ret;
     int mono_measurement;
     int out_phase_measurement;
     float tolerance = 1.0f - s->tolerance;
@@ -265,8 +266,12 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *in)
         for (i = 0; i < outlink->h; i++)
             memset(out->data[0] + i * out->linesize[0], 0, outlink->w * 4);
     } else if (s->do_video) {
+        ret = ff_inlink_make_frame_writable(outlink, &s->out);
+        if (ret < 0) {
+            av_frame_free(&in);
+            return ret;
+        }
         out = s->out;
-        av_frame_make_writable(s->out);
         for (i = outlink->h - 1; i >= 10; i--)
             memmove(out->data[0] + (i  ) * out->linesize[0],
                     out->data[0] + (i-1) * out->linesize[0],
diff --git a/libavfilter/avf_avectorscope.c b/libavfilter/avf_avectorscope.c
index 3927d80b42..6e45fd9575 100644
--- a/libavfilter/avf_avectorscope.c
+++ b/libavfilter/avf_avectorscope.c
@@ -297,6 +297,7 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
     unsigned x, y;
     unsigned prev_x = s->prev_x, prev_y = s->prev_y;
     double zoom = s->zoom;
+    int ret;
 
     if (!s->outpicref || s->outpicref->width  != outlink->w ||
                          s->outpicref->height != outlink->h) {
@@ -314,7 +315,11 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
     s->outpicref->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base);
     s->outpicref->duration = 1;
 
-    av_frame_make_writable(s->outpicref);
+    ret = ff_inlink_make_frame_writable(outlink, &s->outpicref);
+    if (ret < 0) {
+        av_frame_free(&insamples);
+        return ret;
+    }
     ff_filter_execute(ctx, fade, NULL, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
 
     if (zoom < 1) {
diff --git a/libavfilter/avf_showspectrum.c b/libavfilter/avf_showspectrum.c
index 24a424a34a..4ce964706f 100644
--- a/libavfilter/avf_showspectrum.c
+++ b/libavfilter/avf_showspectrum.c
@@ -1441,7 +1441,10 @@  static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples)
         }
     }
 
-    av_frame_make_writable(s->outpicref);
+    ret = ff_inlink_make_frame_writable(outlink, &s->outpicref);
+    if (ret < 0)
+        return ret;
+    outpicref = s->outpicref;
     /* copy to output */
     if (s->orientation == VERTICAL) {
         if (s->sliding == SCROLL) {
diff --git a/libavfilter/avf_showvolume.c b/libavfilter/avf_showvolume.c
index 24d42d030d..645fc68cb0 100644
--- a/libavfilter/avf_showvolume.c
+++ b/libavfilter/avf_showvolume.c
@@ -324,7 +324,7 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
     AVFilterLink *outlink = ctx->outputs[0];
     ShowVolumeContext *s = ctx->priv;
     const int step = s->step;
-    int c, j, k, max_draw;
+    int c, j, k, max_draw, ret;
     char channel_name[64];
     AVFrame *out;
 
@@ -434,7 +434,9 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
     out = av_frame_clone(s->out);
     if (!out)
         return AVERROR(ENOMEM);
-    av_frame_make_writable(out);
+    ret = ff_inlink_make_frame_writable(outlink, &out);
+    if (ret < 0)
+        return ret;
 
     /* draw volume level */
     for (c = 0; c < inlink->ch_layout.nb_channels && s->h >= 8 && s->draw_volume; c++) {
diff --git a/libavfilter/f_ebur128.c b/libavfilter/f_ebur128.c
index 8afab37fdb..ef0fb4a52a 100644
--- a/libavfilter/f_ebur128.c
+++ b/libavfilter/f_ebur128.c
@@ -618,7 +618,7 @@  static int gate_update(struct integrator *integ, double power,
 
 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
 {
-    int i, ch, idx_insample;
+    int i, ch, idx_insample, ret;
     AVFilterContext *ctx = inlink->dst;
     EBUR128Context *ebur128 = ctx->priv;
     const int nb_channels = ebur128->nb_channels;
@@ -821,7 +821,13 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
                 y_loudness_lu_graph = lu_to_y(ebur128, loudness_3000 - ebur128->target);
                 y_loudness_lu_gauge = lu_to_y(ebur128, gauge_value);
 
-                av_frame_make_writable(pic);
+                ret = ff_inlink_make_frame_writable(outlink, &ebur128->outpicref);
+                if (ret < 0) {
+                    av_frame_free(&insamples);
+                    ebur128->insamples = NULL;
+                    return ret;
+                }
+                pic = ebur128->outpicref;
                 /* draw the graph using the short-term loudness */
                 p = pic->data[0] + ebur128->graph.y*pic->linesize[0] + ebur128->graph.x*3;
                 for (y = 0; y < ebur128->graph.h; y++) {
diff --git a/libavfilter/f_perms.c b/libavfilter/f_perms.c
index e26a15fd06..95fb97f201 100644
--- a/libavfilter/f_perms.c
+++ b/libavfilter/f_perms.c
@@ -24,6 +24,7 @@ 
 #include "libavutil/opt.h"
 #include "libavutil/random_seed.h"
 #include "audio.h"
+#include "filters.h"
 #include "video.h"
 
 enum mode {
@@ -96,7 +97,7 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
            in_perm == out_perm ? " (no-op)" : "");
 
     if (in_perm == RO && out_perm == RW) {
-        if ((ret = av_frame_make_writable(frame)) < 0)
+        if ((ret = ff_inlink_make_frame_writable(inlink, &frame)) < 0)
             return ret;
     } else if (in_perm == RW && out_perm == RO) {
         out = av_frame_clone(frame);
diff --git a/libavfilter/framesync.c b/libavfilter/framesync.c
index ee91e4cf68..422f4f7ad1 100644
--- a/libavfilter/framesync.c
+++ b/libavfilter/framesync.c
@@ -288,7 +288,7 @@  int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe,
         if (need_copy) {
             if (!(frame = av_frame_clone(frame)))
                 return AVERROR(ENOMEM);
-            if ((ret = av_frame_make_writable(frame)) < 0) {
+            if ((ret = ff_inlink_make_frame_writable(fs->parent->inputs[0], &frame) < 0)) {
                 av_frame_free(&frame);
                 return ret;
             }
diff --git a/libavfilter/vf_cover_rect.c b/libavfilter/vf_cover_rect.c
index 01c9f2abbb..2fc5fc9693 100644
--- a/libavfilter/vf_cover_rect.c
+++ b/libavfilter/vf_cover_rect.c
@@ -24,6 +24,7 @@ 
 
 #include "libavutil/imgutils.h"
 #include "libavutil/opt.h"
+#include "filters.h"
 #include "internal.h"
 
 #include "lavfutils.h"
@@ -125,7 +126,7 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     AVFilterContext *ctx = inlink->dst;
     CoverContext *cover = ctx->priv;
     AVDictionaryEntry *ex, *ey, *ew, *eh;
-    int x = -1, y = -1, w = -1, h = -1;
+    int ret, x = -1, y = -1, w = -1, h = -1;
     char *xendptr = NULL, *yendptr = NULL, *wendptr = NULL, *hendptr = NULL;
 
     ex = av_dict_get(in->metadata, "lavfi.rect.x", NULL, AV_DICT_MATCH_CASE);
@@ -170,7 +171,9 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     x = av_clip(x, 0, in->width  - w);
     y = av_clip(y, 0, in->height - h);
 
-    av_frame_make_writable(in);
+    ret = ff_inlink_make_frame_writable(inlink, &in);
+    if (ret < 0)
+        return ret;
 
     if (cover->mode == MODE_BLUR) {
         blur (cover, in, x, y);
diff --git a/libavfilter/vf_dedot.c b/libavfilter/vf_dedot.c
index a0638f45b4..6ca47c262a 100644
--- a/libavfilter/vf_dedot.c
+++ b/libavfilter/vf_dedot.c
@@ -289,7 +289,7 @@  static int activate(AVFilterContext *ctx)
             s->frames[4]) {
             out = av_frame_clone(s->frames[2]);
             if (out && !ctx->is_disabled) {
-                ret = av_frame_make_writable(out);
+                ret = ff_inlink_make_frame_writable(inlink, &out);
                 if (ret >= 0) {
                     if (s->m & 1)
                         ff_filter_execute(ctx, s->dedotcrawl, out, NULL,
diff --git a/libavfilter/vf_floodfill.c b/libavfilter/vf_floodfill.c
index da747c9f9f..952f1d15f1 100644
--- a/libavfilter/vf_floodfill.c
+++ b/libavfilter/vf_floodfill.c
@@ -22,6 +22,7 @@ 
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
 #include "avfilter.h"
+#include "filters.h"
 #include "formats.h"
 #include "internal.h"
 #include "video.h"
@@ -315,7 +316,7 @@  static int filter_frame(AVFilterLink *link, AVFrame *frame)
             s->front++;
         }
 
-        if (ret = av_frame_make_writable(frame))
+        if (ret = ff_inlink_make_frame_writable(link, &frame))
             return ret;
 
         while (s->front > s->back) {
diff --git a/libavfilter/vf_lensfun.c b/libavfilter/vf_lensfun.c
index 35c522a723..52652e0c9b 100644
--- a/libavfilter/vf_lensfun.c
+++ b/libavfilter/vf_lensfun.c
@@ -32,6 +32,7 @@ 
 #include "libavutil/opt.h"
 #include "libswscale/swscale.h"
 #include "avfilter.h"
+#include "filters.h"
 #include "formats.h"
 #include "internal.h"
 #include "video.h"
@@ -443,9 +444,12 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     AVFrame *out;
     VignettingThreadData vignetting_thread_data;
     DistortionCorrectionThreadData distortion_correction_thread_data;
+    int ret;
 
     if (lensfun->mode & VIGNETTING) {
-        av_frame_make_writable(in);
+        ret = ff_inlink_make_frame_writable(inlink, &in);
+        if (ret < 0)
+            return ret;
 
         vignetting_thread_data = (VignettingThreadData) {
             .width = inlink->w,
diff --git a/libavfilter/vf_overlay_cuda.c b/libavfilter/vf_overlay_cuda.c
index 68c00405fb..b2cbb9c625 100644
--- a/libavfilter/vf_overlay_cuda.c
+++ b/libavfilter/vf_overlay_cuda.c
@@ -32,6 +32,7 @@ 
 #include "libavutil/eval.h"
 
 #include "avfilter.h"
+#include "filters.h"
 #include "framesync.h"
 #include "internal.h"
 
@@ -252,7 +253,7 @@  static int overlay_cuda_blend(FFFrameSync *fs)
     if (!input_overlay)
         return ff_filter_frame(outlink, input_main);
 
-    ret = av_frame_make_writable(input_main);
+    ret = ff_inlink_make_frame_writable(inlink, &input_main);
     if (ret < 0) {
         av_frame_free(&input_main);
         return ret;
diff --git a/libavfilter/vf_paletteuse.c b/libavfilter/vf_paletteuse.c
index 944ff5c74d..5fa7a605ce 100644
--- a/libavfilter/vf_paletteuse.c
+++ b/libavfilter/vf_paletteuse.c
@@ -783,7 +783,7 @@  static int apply_palette(AVFilterLink *inlink, AVFrame *in, AVFrame **outf)
     av_frame_unref(s->last_out);
     if ((ret = av_frame_ref(s->last_in, in))       < 0 ||
         (ret = av_frame_ref(s->last_out, out))     < 0 ||
-        (ret = av_frame_make_writable(s->last_in)) < 0) {
+        (ret = ff_inlink_make_frame_writable(inlink, &s->last_in)) < 0) {
         av_frame_free(&out);
         *outf = NULL;
         return ret;
diff --git a/libavfilter/vf_photosensitivity.c b/libavfilter/vf_photosensitivity.c
index 1bb984cc93..e05d4d0262 100644
--- a/libavfilter/vf_photosensitivity.c
+++ b/libavfilter/vf_photosensitivity.c
@@ -25,6 +25,7 @@ 
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
 
+#include "filters.h"
 #include "formats.h"
 #include "internal.h"
 #include "video.h"
@@ -243,7 +244,7 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *in)
             /* just duplicate the frame */
             s->history[s->history_pos] = 0; /* frame was duplicated, thus, delta is zero */
         } else {
-            res = av_frame_make_writable(s->last_frame_av);
+            res = ff_inlink_make_frame_writable(inlink, &s->last_frame_av);
             if (res) {
                 av_frame_free(&in);
                 return res;
diff --git a/libavfilter/vf_repeatfields.c b/libavfilter/vf_repeatfields.c
index 9c02c61631..4ace5a18f7 100644
--- a/libavfilter/vf_repeatfields.c
+++ b/libavfilter/vf_repeatfields.c
@@ -20,6 +20,7 @@ 
 
 #include "libavutil/imgutils.h"
 #include "avfilter.h"
+#include "filters.h"
 #include "internal.h"
 
 typedef struct RepeatFieldsContext {
@@ -110,7 +111,9 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *in) {
         ret = ff_filter_frame(outlink, new);
 
         if (in->repeat_pict) {
-            av_frame_make_writable(out);
+            ret = ff_inlink_make_frame_writable(inlink, &out);
+            if (ret < 0)
+                return ret;
             update_pts(outlink, out, in->pts, 2);
             for (i = 0; i < s->nb_planes; i++) {
                 av_image_copy_plane(out->data[i], out->linesize[i] * 2,
@@ -121,7 +124,9 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *in) {
         }
     } else {
         for (i = 0; i < s->nb_planes; i++) {
-            av_frame_make_writable(out);
+            ret = ff_inlink_make_frame_writable(inlink, &out);
+            if (ret < 0)
+                return ret;
             av_image_copy_plane(out->data[i] + out->linesize[i], out->linesize[i] * 2,
                                 in->data[i] + in->linesize[i], in->linesize[i] * 2,
                                 s->linesize[i], s->planeheight[i] / 2);
@@ -139,7 +144,9 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *in) {
             ret = ff_filter_frame(outlink, new);
             state = 0;
         } else {
-            av_frame_make_writable(out);
+            ret = ff_inlink_make_frame_writable(inlink, &out);
+            if (ret < 0)
+                return ret;
             update_pts(outlink, out, in->pts, 1);
             for (i = 0; i < s->nb_planes; i++) {
                 av_image_copy_plane(out->data[i], out->linesize[i] * 2,
diff --git a/libavfilter/vf_signalstats.c b/libavfilter/vf_signalstats.c
index e6f84be9ba..93ff378b31 100644
--- a/libavfilter/vf_signalstats.c
+++ b/libavfilter/vf_signalstats.c
@@ -23,6 +23,7 @@ 
 #include "libavutil/intreadwrite.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
+#include "filters.h"
 #include "internal.h"
 
 enum FilterMode {
@@ -565,7 +566,7 @@  static int filter_frame8(AVFilterLink *link, AVFrame *in)
     int tothue = 0;
     int dify = 0, difu = 0, difv = 0;
     uint16_t masky = 0, masku = 0, maskv = 0;
-
+    int ret;
     int filtot[FILT_NUMB] = {0};
     AVFrame *prev;
 
@@ -588,7 +589,9 @@  static int filter_frame8(AVFilterLink *link, AVFrame *in)
 
     if (s->outfilter != FILTER_NONE) {
         out = av_frame_clone(in);
-        av_frame_make_writable(out);
+        ret = ff_inlink_make_frame_writable(link, &out);
+        if (ret < 0)
+            return ret;
     }
 
     ff_filter_execute(ctx, compute_sat_hue_metrics8, &td_huesat,
@@ -790,7 +793,7 @@  static int filter_frame16(AVFilterLink *link, AVFrame *in)
 
     int filtot[FILT_NUMB] = {0};
     AVFrame *prev;
-
+    int ret;
     AVFrame *sat = s->frame_sat;
     AVFrame *hue = s->frame_hue;
     const uint16_t *p_sat = (uint16_t *)sat->data[0];
@@ -810,7 +813,9 @@  static int filter_frame16(AVFilterLink *link, AVFrame *in)
 
     if (s->outfilter != FILTER_NONE) {
         out = av_frame_clone(in);
-        av_frame_make_writable(out);
+        ret = ff_inlink_make_frame_writable(link, &out);
+        if (ret < 0)
+            return ret;
     }
 
     ff_filter_execute(ctx, compute_sat_hue_metrics16, &td_huesat,
diff --git a/libavfilter/vf_telecine.c b/libavfilter/vf_telecine.c
index e8de63bbcf..227de6f733 100644
--- a/libavfilter/vf_telecine.c
+++ b/libavfilter/vf_telecine.c
@@ -29,6 +29,7 @@ 
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
+#include "filters.h"
 #include "formats.h"
 #include "internal.h"
 #include "video.h"
@@ -182,7 +183,11 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
     }
 
     if (s->occupied) {
-        av_frame_make_writable(s->frame[nout]);
+        ret = ff_inlink_make_frame_writable(inlink, &s->frame[nout]);
+        if (ret < 0) {
+            av_frame_free(&inpicref);
+            return ret;
+        }
         for (i = 0; i < s->nb_planes; i++) {
             // fill in the EARLIER field from the buffered pic
             av_image_copy_plane(s->frame[nout]->data[i] + s->frame[nout]->linesize[i] * s->first_field,
@@ -208,7 +213,11 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
 
     while (len >= 2) {
         // output THIS image as-is
-        av_frame_make_writable(s->frame[nout]);
+        ret = ff_inlink_make_frame_writable(inlink, &s->frame[nout]);
+        if (ret < 0) {
+            av_frame_free(&inpicref);
+            return ret;
+        }
         for (i = 0; i < s->nb_planes; i++)
             av_image_copy_plane(s->frame[nout]->data[i], s->frame[nout]->linesize[i],
                                 inpicref->data[i], inpicref->linesize[i],
diff --git a/libavfilter/vf_vidstabdetect.c b/libavfilter/vf_vidstabdetect.c
index 62b998e171..3eb1abab46 100644
--- a/libavfilter/vf_vidstabdetect.c
+++ b/libavfilter/vf_vidstabdetect.c
@@ -27,6 +27,7 @@ 
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
+#include "filters.h"
 #include "internal.h"
 
 #include "vidstabutils.h"
@@ -149,10 +150,13 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 
     AVFilterLink *outlink = inlink->dst->outputs[0];
     VSFrame frame;
-    int plane;
+    int plane, ret;
 
-    if (s->conf.show > 0 && !av_frame_is_writable(in))
-        av_frame_make_writable(in);
+    if (s->conf.show > 0 && !av_frame_is_writable(in)) {
+        ret = ff_inlink_make_frame_writable(inlink, &in);
+        if (ret < 0)
+            return ret;
+    }
 
     for (plane = 0; plane < md->fi.planes; plane++) {
         frame.data[plane] = in->data[plane];
-- 
2.39.1