Skip to content

Commit 7f17e0e

Browse files
committed
lavfi: move AVFilterLink.hw_frames_ctx to FilterLink
1 parent ce24b5b commit 7f17e0e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+326
-193
lines changed

libavfilter/avfilter.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ static void link_free(AVFilterLink **link)
206206
ff_frame_pool_uninit(&li->frame_pool);
207207
av_channel_layout_uninit(&(*link)->ch_layout);
208208

209+
av_buffer_unref(&li->l.hw_frames_ctx);
210+
209211
av_freep(link);
210212
}
211213

@@ -411,13 +413,18 @@ int ff_filter_config_links(AVFilterContext *filter)
411413
link->time_base = (AVRational) {1, link->sample_rate};
412414
}
413415

414-
if (link->src->nb_inputs && link->src->inputs[0]->hw_frames_ctx &&
416+
if (link->src->nb_inputs &&
415417
!(link->src->filter->flags_internal & FF_FILTER_FLAG_HWFRAME_AWARE)) {
416-
av_assert0(!link->hw_frames_ctx &&
418+
FilterLink *l0 = ff_filter_link(link->src->inputs[0]);
419+
420+
av_assert0(!li->l.hw_frames_ctx &&
417421
"should not be set by non-hwframe-aware filter");
418-
link->hw_frames_ctx = av_buffer_ref(link->src->inputs[0]->hw_frames_ctx);
419-
if (!link->hw_frames_ctx)
420-
return AVERROR(ENOMEM);
422+
423+
if (l0->hw_frames_ctx) {
424+
li->l.hw_frames_ctx = av_buffer_ref(l0->hw_frames_ctx);
425+
if (!li->l.hw_frames_ctx)
426+
return AVERROR(ENOMEM);
427+
}
421428
}
422429

423430
if ((config_link = link->dstpad->config_props))
@@ -765,8 +772,6 @@ static void free_link(AVFilterLink *link)
765772
if (link->dst)
766773
link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
767774

768-
av_buffer_unref(&link->hw_frames_ctx);
769-
770775
ff_formats_unref(&link->incfg.formats);
771776
ff_formats_unref(&link->outcfg.formats);
772777
ff_formats_unref(&link->incfg.color_spaces);
@@ -1615,12 +1620,13 @@ const AVClass *avfilter_get_class(void)
16151620
int ff_filter_init_hw_frames(AVFilterContext *avctx, AVFilterLink *link,
16161621
int default_pool_size)
16171622
{
1623+
FilterLink *l = ff_filter_link(link);
16181624
AVHWFramesContext *frames;
16191625

16201626
// Must already be set by caller.
1621-
av_assert0(link->hw_frames_ctx);
1627+
av_assert0(l->hw_frames_ctx);
16221628

1623-
frames = (AVHWFramesContext*)link->hw_frames_ctx->data;
1629+
frames = (AVHWFramesContext*)l->hw_frames_ctx->data;
16241630

16251631
if (frames->initial_pool_size == 0) {
16261632
// Dynamic allocation is necessarily supported.

libavfilter/avfilter.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -641,12 +641,6 @@ struct AVFilterLink {
641641
* cleared when a frame is filtered.
642642
*/
643643
int frame_wanted_out;
644-
645-
/**
646-
* For hwaccel pixel formats, this should be a reference to the
647-
* AVHWFramesContext describing the frames.
648-
*/
649-
AVBufferRef *hw_frames_ctx;
650644
};
651645

652646
/**

libavfilter/buffersink.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,12 @@ MAKE_AVFILTERLINK_ACCESSOR(enum AVColorRange, color_range)
188188

189189
MAKE_AVFILTERLINK_ACCESSOR(int , sample_rate )
190190

191-
MAKE_AVFILTERLINK_ACCESSOR(AVBufferRef * , hw_frames_ctx )
191+
AVBufferRef* av_buffersink_get_hw_frames_ctx(const AVFilterContext *ctx)
192+
{
193+
FilterLink *l = ff_filter_link(ctx->inputs[0]);
194+
av_assert0(ctx->filter->activate == activate);
195+
return l->hw_frames_ctx;
196+
}
192197

193198
int av_buffersink_get_channels(const AVFilterContext *ctx)
194199
{

libavfilter/buffersrc.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ static int query_formats(AVFilterContext *ctx)
493493

494494
static int config_props(AVFilterLink *link)
495495
{
496+
FilterLink *l = ff_filter_link(link);
496497
BufferSourceContext *c = link->src->priv;
497498

498499
switch (link->type) {
@@ -502,8 +503,8 @@ static int config_props(AVFilterLink *link)
502503
link->sample_aspect_ratio = c->pixel_aspect;
503504

504505
if (c->hw_frames_ctx) {
505-
link->hw_frames_ctx = av_buffer_ref(c->hw_frames_ctx);
506-
if (!link->hw_frames_ctx)
506+
l->hw_frames_ctx = av_buffer_ref(c->hw_frames_ctx);
507+
if (!l->hw_frames_ctx)
507508
return AVERROR(ENOMEM);
508509
}
509510
break;

libavfilter/filters.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ typedef struct FilterLink {
5757
* May be set by the link destination filter in its config_props().
5858
*/
5959
int max_samples;
60+
61+
/**
62+
* For hwaccel pixel formats, this should be a reference to the
63+
* AVHWFramesContext describing the frames.
64+
*
65+
* May be set by the link source filter in its config_props().
66+
*/
67+
AVBufferRef *hw_frames_ctx;
6068
} FilterLink;
6169

6270
static inline FilterLink* ff_filter_link(AVFilterLink *link)

libavfilter/opencl.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "libavutil/mem.h"
2424
#include "libavutil/pixdesc.h"
2525

26+
#include "filters.h"
2627
#include "opencl.h"
2728

2829
static int opencl_filter_set_device(AVFilterContext *avctx,
@@ -44,12 +45,13 @@ static int opencl_filter_set_device(AVFilterContext *avctx,
4445

4546
int ff_opencl_filter_config_input(AVFilterLink *inlink)
4647
{
48+
FilterLink *l = ff_filter_link(inlink);
4749
AVFilterContext *avctx = inlink->dst;
4850
OpenCLFilterContext *ctx = avctx->priv;
4951
AVHWFramesContext *input_frames;
5052
int err;
5153

52-
if (!inlink->hw_frames_ctx) {
54+
if (!l->hw_frames_ctx) {
5355
av_log(avctx, AV_LOG_ERROR, "OpenCL filtering requires a "
5456
"hardware frames context on the input.\n");
5557
return AVERROR(EINVAL);
@@ -59,7 +61,7 @@ int ff_opencl_filter_config_input(AVFilterLink *inlink)
5961
if (avctx->inputs[0] != inlink)
6062
return 0;
6163

62-
input_frames = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
64+
input_frames = (AVHWFramesContext*)l->hw_frames_ctx->data;
6365
if (input_frames->format != AV_PIX_FMT_OPENCL)
6466
return AVERROR(EINVAL);
6567

@@ -80,13 +82,14 @@ int ff_opencl_filter_config_input(AVFilterLink *inlink)
8082

8183
int ff_opencl_filter_config_output(AVFilterLink *outlink)
8284
{
85+
FilterLink *l = ff_filter_link(outlink);
8386
AVFilterContext *avctx = outlink->src;
8487
OpenCLFilterContext *ctx = avctx->priv;
8588
AVBufferRef *output_frames_ref = NULL;
8689
AVHWFramesContext *output_frames;
8790
int err;
8891

89-
av_buffer_unref(&outlink->hw_frames_ctx);
92+
av_buffer_unref(&l->hw_frames_ctx);
9093

9194
if (!ctx->device_ref) {
9295
if (!avctx->hw_device_ctx) {
@@ -119,7 +122,7 @@ int ff_opencl_filter_config_output(AVFilterLink *outlink)
119122
goto fail;
120123
}
121124

122-
outlink->hw_frames_ctx = output_frames_ref;
125+
l->hw_frames_ctx = output_frames_ref;
123126
outlink->w = ctx->output_width;
124127
outlink->h = ctx->output_height;
125128

libavfilter/qsvvpp.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "libavutil/pixdesc.h"
2929

3030
#include "internal.h"
31+
#include "filters.h"
3132
#include "qsvvpp.h"
3233
#include "video.h"
3334

@@ -303,10 +304,11 @@ static int fill_frameinfo_by_link(mfxFrameInfo *frameinfo, AVFilterLink *link)
303304
const AVPixFmtDescriptor *desc;
304305

305306
if (link->format == AV_PIX_FMT_QSV) {
306-
if (!link->hw_frames_ctx)
307+
FilterLink *l = ff_filter_link(link);
308+
if (!l->hw_frames_ctx)
307309
return AVERROR(EINVAL);
308310

309-
frames_ctx = (AVHWFramesContext *)link->hw_frames_ctx->data;
311+
frames_ctx = (AVHWFramesContext *)l->hw_frames_ctx->data;
310312
frames_hwctx = frames_ctx->hwctx;
311313
*frameinfo = frames_hwctx->nb_surfaces ? frames_hwctx->surfaces[0].Info : *frames_hwctx->info;
312314
} else {
@@ -472,6 +474,7 @@ static QSVFrame *submit_frame(QSVVPPContext *s, AVFilterLink *inlink, AVFrame *p
472474
/* get the output surface */
473475
static QSVFrame *query_frame(QSVVPPContext *s, AVFilterLink *outlink, const AVFrame *in)
474476
{
477+
FilterLink *l = ff_filter_link(outlink);
475478
AVFilterContext *ctx = outlink->src;
476479
QSVFrame *out_frame;
477480
int ret;
@@ -489,7 +492,7 @@ static QSVFrame *query_frame(QSVVPPContext *s, AVFilterLink *outlink, const AVFr
489492
if (!out_frame->frame)
490493
return NULL;
491494

492-
ret = av_hwframe_get_buffer(outlink->hw_frames_ctx, out_frame->frame, 0);
495+
ret = av_hwframe_get_buffer(l->hw_frames_ctx, out_frame->frame, 0);
493496
if (ret < 0) {
494497
av_log(ctx, AV_LOG_ERROR, "Can't allocate a surface.\n");
495498
return NULL;
@@ -546,7 +549,9 @@ FF_ENABLE_DEPRECATION_WARNINGS
546549
static int init_vpp_session(AVFilterContext *avctx, QSVVPPContext *s)
547550
{
548551
AVFilterLink *inlink = avctx->inputs[0];
552+
FilterLink *inl = ff_filter_link(inlink);
549553
AVFilterLink *outlink = avctx->outputs[0];
554+
FilterLink *outl = ff_filter_link(outlink);
550555
AVQSVFramesContext *in_frames_hwctx = NULL;
551556
AVQSVFramesContext *out_frames_hwctx = NULL;
552557

@@ -559,8 +564,8 @@ static int init_vpp_session(AVFilterContext *avctx, QSVVPPContext *s)
559564
mfxIMPL impl;
560565
int ret, i;
561566

562-
if (inlink->hw_frames_ctx) {
563-
AVHWFramesContext *frames_ctx = (AVHWFramesContext *)inlink->hw_frames_ctx->data;
567+
if (inl->hw_frames_ctx) {
568+
AVHWFramesContext *frames_ctx = (AVHWFramesContext *)inl->hw_frames_ctx->data;
564569

565570
device_ref = frames_ctx->device_ref;
566571
in_frames_hwctx = frames_ctx->hwctx;
@@ -657,8 +662,8 @@ static int init_vpp_session(AVFilterContext *avctx, QSVVPPContext *s)
657662
s->surface_ptrs_out[i] = out_frames_hwctx->surfaces + i;
658663
s->nb_surface_ptrs_out = out_frames_hwctx->nb_surfaces;
659664

660-
av_buffer_unref(&outlink->hw_frames_ctx);
661-
outlink->hw_frames_ctx = out_frames_ref;
665+
av_buffer_unref(&outl->hw_frames_ctx);
666+
outl->hw_frames_ctx = out_frames_ref;
662667
} else
663668
s->out_mem_mode = MFX_MEMTYPE_SYSTEM_MEMORY;
664669

libavfilter/vaapi_vpp.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "libavutil/avassert.h"
2222
#include "libavutil/mem.h"
2323
#include "libavutil/pixdesc.h"
24+
25+
#include "filters.h"
2426
#include "formats.h"
2527
#include "internal.h"
2628
#include "vaapi_vpp.h"
@@ -74,19 +76,20 @@ void ff_vaapi_vpp_pipeline_uninit(AVFilterContext *avctx)
7476

7577
int ff_vaapi_vpp_config_input(AVFilterLink *inlink)
7678
{
79+
FilterLink *l = ff_filter_link(inlink);
7780
AVFilterContext *avctx = inlink->dst;
7881
VAAPIVPPContext *ctx = avctx->priv;
7982

8083
if (ctx->pipeline_uninit)
8184
ctx->pipeline_uninit(avctx);
8285

83-
if (!inlink->hw_frames_ctx) {
86+
if (!l->hw_frames_ctx) {
8487
av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is "
8588
"required to associate the processing device.\n");
8689
return AVERROR(EINVAL);
8790
}
8891

89-
ctx->input_frames_ref = av_buffer_ref(inlink->hw_frames_ctx);
92+
ctx->input_frames_ref = av_buffer_ref(l->hw_frames_ctx);
9093
if (!ctx->input_frames_ref) {
9194
av_log(avctx, AV_LOG_ERROR, "A input frames reference create "
9295
"failed.\n");
@@ -99,8 +102,10 @@ int ff_vaapi_vpp_config_input(AVFilterLink *inlink)
99102

100103
int ff_vaapi_vpp_config_output(AVFilterLink *outlink)
101104
{
105+
FilterLink *outl = ff_filter_link(outlink);
102106
AVFilterContext *avctx = outlink->src;
103107
AVFilterLink *inlink = avctx->inputs[0];
108+
FilterLink *inl = ff_filter_link(inlink);
104109
VAAPIVPPContext *ctx = avctx->priv;
105110
AVVAAPIHWConfig *hwconfig = NULL;
106111
AVHWFramesConstraints *constraints = NULL;
@@ -121,8 +126,8 @@ int ff_vaapi_vpp_config_output(AVFilterLink *outlink)
121126
outlink->h = ctx->output_height;
122127

123128
if (ctx->passthrough) {
124-
if (inlink->hw_frames_ctx)
125-
outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
129+
if (inl->hw_frames_ctx)
130+
outl->hw_frames_ctx = av_buffer_ref(inl->hw_frames_ctx);
126131
av_log(ctx, AV_LOG_VERBOSE, "Using VAAPI filter passthrough mode.\n");
127132

128133
return 0;
@@ -189,15 +194,15 @@ int ff_vaapi_vpp_config_output(AVFilterLink *outlink)
189194
goto fail;
190195
}
191196

192-
outlink->hw_frames_ctx = av_hwframe_ctx_alloc(ctx->device_ref);
193-
if (!outlink->hw_frames_ctx) {
197+
outl->hw_frames_ctx = av_hwframe_ctx_alloc(ctx->device_ref);
198+
if (!outl->hw_frames_ctx) {
194199
av_log(avctx, AV_LOG_ERROR, "Failed to create HW frame context "
195200
"for output.\n");
196201
err = AVERROR(ENOMEM);
197202
goto fail;
198203
}
199204

200-
output_frames = (AVHWFramesContext*)outlink->hw_frames_ctx->data;
205+
output_frames = (AVHWFramesContext*)outl->hw_frames_ctx->data;
201206

202207
output_frames->format = AV_PIX_FMT_VAAPI;
203208
output_frames->sw_format = ctx->output_format;
@@ -213,7 +218,7 @@ int ff_vaapi_vpp_config_output(AVFilterLink *outlink)
213218
if (err < 0)
214219
goto fail;
215220

216-
err = av_hwframe_ctx_init(outlink->hw_frames_ctx);
221+
err = av_hwframe_ctx_init(outl->hw_frames_ctx);
217222
if (err < 0) {
218223
av_log(avctx, AV_LOG_ERROR, "Failed to initialise VAAPI frame "
219224
"context for output: %d\n", err);
@@ -247,7 +252,7 @@ int ff_vaapi_vpp_config_output(AVFilterLink *outlink)
247252
return 0;
248253

249254
fail:
250-
av_buffer_unref(&outlink->hw_frames_ctx);
255+
av_buffer_unref(&outl->hw_frames_ctx);
251256
av_freep(&hwconfig);
252257
av_hwframe_constraints_free(&constraints);
253258
return err;

libavfilter/vf_bilateral_cuda.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "libavutil/pixdesc.h"
3131

3232
#include "avfilter.h"
33+
#include "filters.h"
3334
#include "internal.h"
3435

3536
#include "cuda/load_helper.h"
@@ -178,16 +179,18 @@ static av_cold void set_format_info(AVFilterContext *ctx, enum AVPixelFormat in_
178179

179180
static av_cold int init_processing_chain(AVFilterContext *ctx, int width, int height)
180181
{
182+
FilterLink *inl = ff_filter_link(ctx->inputs[0]);
183+
FilterLink *outl = ff_filter_link(ctx->outputs[0]);
181184
CUDABilateralContext *s = ctx->priv;
182185
AVHWFramesContext *in_frames_ctx;
183186
int ret;
184187

185188
/* check that we have a hw context */
186-
if (!ctx->inputs[0]->hw_frames_ctx) {
189+
if (!inl->hw_frames_ctx) {
187190
av_log(ctx, AV_LOG_ERROR, "No hw context provided on input\n");
188191
return AVERROR(EINVAL);
189192
}
190-
in_frames_ctx = (AVHWFramesContext*)ctx->inputs[0]->hw_frames_ctx->data;
193+
in_frames_ctx = (AVHWFramesContext*)inl->hw_frames_ctx->data;
191194

192195
if (!format_is_supported(in_frames_ctx->sw_format)) {
193196
av_log(ctx, AV_LOG_ERROR, "Unsupported format: %s\n", av_get_pix_fmt_name(in_frames_ctx->sw_format));
@@ -200,8 +203,8 @@ static av_cold int init_processing_chain(AVFilterContext *ctx, int width, int he
200203
if (ret < 0)
201204
return ret;
202205

203-
ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(s->frames_ctx);
204-
if (!ctx->outputs[0]->hw_frames_ctx)
206+
outl->hw_frames_ctx = av_buffer_ref(s->frames_ctx);
207+
if (!outl->hw_frames_ctx)
205208
return AVERROR(ENOMEM);
206209

207210
return 0;
@@ -248,6 +251,7 @@ static av_cold int cuda_bilateral_config_props(AVFilterLink *outlink)
248251
{
249252
AVFilterContext *ctx = outlink->src;
250253
AVFilterLink *inlink = outlink->src->inputs[0];
254+
FilterLink *inl = ff_filter_link(inlink);
251255
CUDABilateralContext *s = ctx->priv;
252256
AVHWFramesContext *frames_ctx;
253257
AVCUDADeviceContext *device_hwctx;
@@ -257,7 +261,7 @@ static av_cold int cuda_bilateral_config_props(AVFilterLink *outlink)
257261
if (ret < 0)
258262
return ret;
259263

260-
frames_ctx = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
264+
frames_ctx = (AVHWFramesContext*)inl->hw_frames_ctx->data;
261265
device_hwctx = frames_ctx->device_ctx->hwctx;
262266

263267
s->hwctx = device_hwctx;

0 commit comments

Comments
 (0)