1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* Hardware accelerated hstack, vstack and xstack filters based on VA-API
*/
#include "config_components.h"
#include "libavutil/opt.h"
#include "libavutil/common.h"
#include "libavutil/pixdesc.h"
#include "libavutil/eval.h"
#include "libavutil/hwcontext.h"
#include "libavutil/avstring.h"
#include "libavutil/avassert.h"
#include "libavutil/imgutils.h"
#include "libavutil/mathematics.h"
#include "libavutil/parseutils.h"
#include "libavutil/mem.h"
#include "filters.h"
#include "formats.h"
#include "video.h"
#include "framesync.h"
#include "vaapi_vpp.h"
#define HSTACK_NAME "hstack_vaapi"
#define VSTACK_NAME "vstack_vaapi"
#define XSTACK_NAME "xstack_vaapi"
#define HWContext VAAPIVPPContext
#define StackHWContext StackVAAPIContext
#include "stack_internal.h"
typedef struct StackVAAPIContext {
StackBaseContext base;
VARectangle *rects;
} StackVAAPIContext;
static int process_frame(FFFrameSync *fs)
{
AVFilterContext *avctx = fs->parent;
AVFilterLink *outlink = avctx->outputs[0];
StackVAAPIContext *sctx = fs->opaque;
VAAPIVPPContext *vppctx = fs->opaque;
AVFrame *oframe, *iframe;
VAProcPipelineParameterBuffer *params = NULL;
VARectangle *irect = NULL;
int ret = 0;
if (vppctx->va_context == VA_INVALID_ID)
return AVERROR(EINVAL);
oframe = ff_get_video_buffer(outlink, outlink->w, outlink->h);
if (!oframe)
return AVERROR(ENOMEM);
irect = av_calloc(avctx->nb_inputs, sizeof(*irect));
params = av_calloc(avctx->nb_inputs, sizeof(*params));
if (!irect || !params) {
ret = AVERROR(ENOMEM);
goto fail;
}
for (int i = 0; i < avctx->nb_inputs; i++) {
ret = ff_framesync_get_frame(fs, i, &iframe, 0);
if (ret)
goto fail;
if (i == 0) {
ret = av_frame_copy_props(oframe, iframe);
if (ret < 0)
goto fail;
}
ret = ff_vaapi_vpp_init_params(avctx, ¶ms[i], iframe, oframe);
if (ret)
goto fail;
av_log(avctx, AV_LOG_DEBUG, "stack input %d: %s, %ux%u (%"PRId64").\n",
i, av_get_pix_fmt_name(iframe->format),
iframe->width, iframe->height, iframe->pts);
irect[i].x = 0;
irect[i].y = 0;
irect[i].width = iframe->width;
irect[i].height = iframe->height;
params[i].surface_region = &irect[i];
params[i].surface = (VASurfaceID)(uintptr_t)iframe->data[3];
params[i].output_region = &sctx->rects[i];
if (sctx->base.fillcolor_enable)
params[i].output_background_color = (sctx->base.fillcolor[3] << 24 |
sctx->base.fillcolor[0] << 16 |
sctx->base.fillcolor[1] << 8 |
sctx->base.fillcolor[2]);
}
oframe->pts = av_rescale_q(sctx->base.fs.pts, sctx->base.fs.time_base, outlink->time_base);
oframe->sample_aspect_ratio = outlink->sample_aspect_ratio;
ret = ff_vaapi_vpp_render_pictures(avctx, params, avctx->nb_inputs, oframe);
if (ret)
goto fail;
av_freep(&irect);
av_freep(¶ms);
return ff_filter_frame(outlink, oframe);
fail:
av_freep(&irect);
av_freep(¶ms);
av_frame_free(&oframe);
return ret;
}
static int config_output(AVFilterLink *outlink)
{
AVFilterContext *avctx = outlink->src;
StackVAAPIContext *sctx = avctx->priv;
VAAPIVPPContext *vppctx = avctx->priv;
AVFilterLink *inlink0 = avctx->inputs[0];
FilterLink *inl0 = ff_filter_link(inlink0);
AVHWFramesContext *hwfc0 = NULL;
int ret;
if (inlink0->format != AV_PIX_FMT_VAAPI || !inl0->hw_frames_ctx || !inl0->hw_frames_ctx->data) {
av_log(avctx, AV_LOG_ERROR, "Software pixel format is not supported.\n");
return AVERROR(EINVAL);
}
hwfc0 = (AVHWFramesContext *)inl0->hw_frames_ctx->data;
for (int i = 1; i < sctx->base.nb_inputs; i++) {
AVFilterLink *inlink = avctx->inputs[i];
FilterLink *inl = ff_filter_link(inlink);
AVHWFramesContext *hwfc = NULL;
if (inlink->format != AV_PIX_FMT_VAAPI || !inl->hw_frames_ctx || !inl->hw_frames_ctx->data) {
av_log(avctx, AV_LOG_ERROR, "Software pixel format is not supported.\n");
return AVERROR(EINVAL);
}
hwfc = (AVHWFramesContext *)inl->hw_frames_ctx->data;
if (hwfc0->sw_format != hwfc->sw_format) {
av_log(avctx, AV_LOG_ERROR, "All inputs should have the same underlying software pixel format.\n");
return AVERROR(EINVAL);
}
if (hwfc0->device_ctx != hwfc->device_ctx) {
av_log(avctx, AV_LOG_ERROR, "All inputs should have the same underlying vaapi devices.\n");
return AVERROR(EINVAL);
}
}
ff_vaapi_vpp_config_input(inlink0);
vppctx->output_format = hwfc0->sw_format;
ret = config_comm_output(outlink);
if (ret < 0)
return ret;
for (int i = 0; i < sctx->base.nb_inputs; i++) {
sctx->rects[i].x = sctx->base.regions[i].x;
sctx->rects[i].y = sctx->base.regions[i].y;
sctx->rects[i].width = sctx->base.regions[i].width;
sctx->rects[i].height = sctx->base.regions[i].height;
}
vppctx->output_width = outlink->w;
vppctx->output_height = outlink->h;
return ff_vaapi_vpp_config_output(outlink);
}
static int vaapi_stack_init(AVFilterContext *avctx)
{
StackVAAPIContext *sctx = avctx->priv;
VAAPIVPPContext *vppctx = avctx->priv;
int ret;
ret = stack_init(avctx);
if (ret)
return ret;
/* stack region */
sctx->rects = av_calloc(sctx->base.nb_inputs, sizeof(*sctx->rects));
if (!sctx->rects)
return AVERROR(ENOMEM);
ff_vaapi_vpp_ctx_init(avctx);
vppctx->output_format = AV_PIX_FMT_NONE;
return 0;
}
static av_cold void vaapi_stack_uninit(AVFilterContext *avctx)
{
StackVAAPIContext *sctx = avctx->priv;
stack_uninit(avctx);
av_freep(&sctx->rects);
}
static const enum AVPixelFormat vaapi_stack_pix_fmts[] = {
AV_PIX_FMT_VAAPI,
AV_PIX_FMT_NONE,
};
#include "stack_internal.c"
#if CONFIG_HSTACK_VAAPI_FILTER
DEFINE_HSTACK_OPTIONS(vaapi);
DEFINE_STACK_FILTER(hstack, vaapi, "VA-API", 0);
#endif
#if CONFIG_VSTACK_VAAPI_FILTER
DEFINE_VSTACK_OPTIONS(vaapi);
DEFINE_STACK_FILTER(vstack, vaapi, "VA-API", 0);
#endif
#if CONFIG_XSTACK_VAAPI_FILTER
DEFINE_XSTACK_OPTIONS(vaapi);
DEFINE_STACK_FILTER(xstack, vaapi, "VA-API", 0);
#endif
|