This merges libav commit ac7bfd69678f3966e38debdb27f4bde94dc0345c,
which was previously skipped.
(cherry picked from commit ac7bfd69678f3966e38debdb27f4bde94dc0345c)
Signed-off-by: Mark Thompson <sw@jkqxz.net>
... | ... |
@@ -3127,6 +3127,7 @@ rubberband_filter_deps="librubberband" |
3127 | 3127 |
sab_filter_deps="gpl swscale" |
3128 | 3128 |
scale2ref_filter_deps="swscale" |
3129 | 3129 |
scale_filter_deps="swscale" |
3130 |
+scale_qsv_filter_deps="libmfx" |
|
3130 | 3131 |
select_filter_select="pixelutils" |
3131 | 3132 |
showcqt_filter_deps="avcodec avformat swscale" |
3132 | 3133 |
showcqt_filter_select="fft" |
... | ... |
@@ -94,7 +94,6 @@ Stuff that didn't reach the codebase: |
94 | 94 |
- a853388d2 hevc: change the stride of the MC buffer to be in bytes instead of elements |
95 | 95 |
- 0cef06df0 checkasm: add HEVC MC tests |
96 | 96 |
- e7078e842 hevcdsp: add x86 SIMD for MC |
97 |
-- QSV scaling filter (62c58c5) |
|
98 | 97 |
|
99 | 98 |
Collateral damage that needs work locally: |
100 | 99 |
------------------------------------------ |
... | ... |
@@ -261,6 +261,7 @@ OBJS-$(CONFIG_ROTATE_FILTER) += vf_rotate.o |
261 | 261 |
OBJS-$(CONFIG_SAB_FILTER) += vf_sab.o |
262 | 262 |
OBJS-$(CONFIG_SCALE_FILTER) += vf_scale.o scale.o |
263 | 263 |
OBJS-$(CONFIG_SCALE_NPP_FILTER) += vf_scale_npp.o scale.o |
264 |
+OBJS-$(CONFIG_SCALE_QSV_FILTER) += vf_scale_qsv.o |
|
264 | 265 |
OBJS-$(CONFIG_SCALE_VAAPI_FILTER) += vf_scale_vaapi.o scale.o |
265 | 266 |
OBJS-$(CONFIG_SCALE2REF_FILTER) += vf_scale.o scale.o |
266 | 267 |
OBJS-$(CONFIG_SELECT_FILTER) += f_select.o |
... | ... |
@@ -271,6 +271,7 @@ static void register_all(void) |
271 | 271 |
REGISTER_FILTER(SAB, sab, vf); |
272 | 272 |
REGISTER_FILTER(SCALE, scale, vf); |
273 | 273 |
REGISTER_FILTER(SCALE_NPP, scale_npp, vf); |
274 |
+ REGISTER_FILTER(SCALE_QSV, scale_qsv, vf); |
|
274 | 275 |
REGISTER_FILTER(SCALE_VAAPI, scale_vaapi, vf); |
275 | 276 |
REGISTER_FILTER(SCALE2REF, scale2ref, vf); |
276 | 277 |
REGISTER_FILTER(SELECT, select, vf); |
... | ... |
@@ -30,7 +30,7 @@ |
30 | 30 |
#include "libavutil/version.h" |
31 | 31 |
|
32 | 32 |
#define LIBAVFILTER_VERSION_MAJOR 6 |
33 |
-#define LIBAVFILTER_VERSION_MINOR 74 |
|
33 |
+#define LIBAVFILTER_VERSION_MINOR 75 |
|
34 | 34 |
#define LIBAVFILTER_VERSION_MICRO 100 |
35 | 35 |
|
36 | 36 |
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ |
37 | 37 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,635 @@ |
0 |
+/* |
|
1 |
+ * This file is part of FFmpeg. |
|
2 |
+ * |
|
3 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
4 |
+ * modify it under the terms of the GNU Lesser General Public |
|
5 |
+ * License as published by the Free Software Foundation; either |
|
6 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
7 |
+ * |
|
8 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
9 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
11 |
+ * Lesser General Public License for more details. |
|
12 |
+ * |
|
13 |
+ * You should have received a copy of the GNU Lesser General Public |
|
14 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
15 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 |
+ */ |
|
17 |
+ |
|
18 |
+/** |
|
19 |
+ * @file |
|
20 |
+ * scale video filter - QSV |
|
21 |
+ */ |
|
22 |
+ |
|
23 |
+#include <mfx/mfxvideo.h> |
|
24 |
+ |
|
25 |
+#include <stdio.h> |
|
26 |
+#include <string.h> |
|
27 |
+ |
|
28 |
+#include "libavutil/avstring.h" |
|
29 |
+#include "libavutil/common.h" |
|
30 |
+#include "libavutil/eval.h" |
|
31 |
+#include "libavutil/hwcontext.h" |
|
32 |
+#include "libavutil/hwcontext_qsv.h" |
|
33 |
+#include "libavutil/internal.h" |
|
34 |
+#include "libavutil/mathematics.h" |
|
35 |
+#include "libavutil/opt.h" |
|
36 |
+#include "libavutil/pixdesc.h" |
|
37 |
+#include "libavutil/time.h" |
|
38 |
+ |
|
39 |
+#include "avfilter.h" |
|
40 |
+#include "formats.h" |
|
41 |
+#include "internal.h" |
|
42 |
+#include "video.h" |
|
43 |
+ |
|
44 |
+static const char *const var_names[] = { |
|
45 |
+ "PI", |
|
46 |
+ "PHI", |
|
47 |
+ "E", |
|
48 |
+ "in_w", "iw", |
|
49 |
+ "in_h", "ih", |
|
50 |
+ "out_w", "ow", |
|
51 |
+ "out_h", "oh", |
|
52 |
+ "a", "dar", |
|
53 |
+ "sar", |
|
54 |
+ NULL |
|
55 |
+}; |
|
56 |
+ |
|
57 |
+enum var_name { |
|
58 |
+ VAR_PI, |
|
59 |
+ VAR_PHI, |
|
60 |
+ VAR_E, |
|
61 |
+ VAR_IN_W, VAR_IW, |
|
62 |
+ VAR_IN_H, VAR_IH, |
|
63 |
+ VAR_OUT_W, VAR_OW, |
|
64 |
+ VAR_OUT_H, VAR_OH, |
|
65 |
+ VAR_A, VAR_DAR, |
|
66 |
+ VAR_SAR, |
|
67 |
+ VARS_NB |
|
68 |
+}; |
|
69 |
+ |
|
70 |
+typedef struct QSVScaleContext { |
|
71 |
+ const AVClass *class; |
|
72 |
+ |
|
73 |
+ AVBufferRef *out_frames_ref; |
|
74 |
+ /* a clone of the main session, used internally for scaling */ |
|
75 |
+ mfxSession session; |
|
76 |
+ |
|
77 |
+ mfxMemId *mem_ids_in; |
|
78 |
+ int nb_mem_ids_in; |
|
79 |
+ |
|
80 |
+ mfxMemId *mem_ids_out; |
|
81 |
+ int nb_mem_ids_out; |
|
82 |
+ |
|
83 |
+ mfxFrameSurface1 **surface_ptrs_in; |
|
84 |
+ int nb_surface_ptrs_in; |
|
85 |
+ |
|
86 |
+ mfxFrameSurface1 **surface_ptrs_out; |
|
87 |
+ int nb_surface_ptrs_out; |
|
88 |
+ |
|
89 |
+ mfxExtOpaqueSurfaceAlloc opaque_alloc; |
|
90 |
+ mfxExtBuffer *ext_buffers[1]; |
|
91 |
+ |
|
92 |
+ int shift_width, shift_height; |
|
93 |
+ |
|
94 |
+ /** |
|
95 |
+ * New dimensions. Special values are: |
|
96 |
+ * 0 = original width/height |
|
97 |
+ * -1 = keep original aspect |
|
98 |
+ */ |
|
99 |
+ int w, h; |
|
100 |
+ |
|
101 |
+ /** |
|
102 |
+ * Output sw format. AV_PIX_FMT_NONE for no conversion. |
|
103 |
+ */ |
|
104 |
+ enum AVPixelFormat format; |
|
105 |
+ |
|
106 |
+ char *w_expr; ///< width expression string |
|
107 |
+ char *h_expr; ///< height expression string |
|
108 |
+ char *format_str; |
|
109 |
+} QSVScaleContext; |
|
110 |
+ |
|
111 |
+static int qsvscale_init(AVFilterContext *ctx) |
|
112 |
+{ |
|
113 |
+ QSVScaleContext *s = ctx->priv; |
|
114 |
+ |
|
115 |
+ if (!strcmp(s->format_str, "same")) { |
|
116 |
+ s->format = AV_PIX_FMT_NONE; |
|
117 |
+ } else { |
|
118 |
+ s->format = av_get_pix_fmt(s->format_str); |
|
119 |
+ if (s->format == AV_PIX_FMT_NONE) { |
|
120 |
+ av_log(ctx, AV_LOG_ERROR, "Unrecognized pixel format: %s\n", s->format_str); |
|
121 |
+ return AVERROR(EINVAL); |
|
122 |
+ } |
|
123 |
+ } |
|
124 |
+ |
|
125 |
+ return 0; |
|
126 |
+} |
|
127 |
+ |
|
128 |
+static void qsvscale_uninit(AVFilterContext *ctx) |
|
129 |
+{ |
|
130 |
+ QSVScaleContext *s = ctx->priv; |
|
131 |
+ |
|
132 |
+ if (s->session) { |
|
133 |
+ MFXClose(s->session); |
|
134 |
+ s->session = NULL; |
|
135 |
+ } |
|
136 |
+ av_buffer_unref(&s->out_frames_ref); |
|
137 |
+ |
|
138 |
+ av_freep(&s->mem_ids_in); |
|
139 |
+ av_freep(&s->mem_ids_out); |
|
140 |
+ s->nb_mem_ids_in = 0; |
|
141 |
+ s->nb_mem_ids_out = 0; |
|
142 |
+ |
|
143 |
+ av_freep(&s->surface_ptrs_in); |
|
144 |
+ av_freep(&s->surface_ptrs_out); |
|
145 |
+ s->nb_surface_ptrs_in = 0; |
|
146 |
+ s->nb_surface_ptrs_out = 0; |
|
147 |
+} |
|
148 |
+ |
|
149 |
+static int qsvscale_query_formats(AVFilterContext *ctx) |
|
150 |
+{ |
|
151 |
+ static const enum AVPixelFormat pixel_formats[] = { |
|
152 |
+ AV_PIX_FMT_QSV, AV_PIX_FMT_NONE, |
|
153 |
+ }; |
|
154 |
+ AVFilterFormats *pix_fmts = ff_make_format_list(pixel_formats); |
|
155 |
+ int ret; |
|
156 |
+ |
|
157 |
+ if ((ret = ff_set_common_formats(ctx, pix_fmts)) < 0) |
|
158 |
+ return ret; |
|
159 |
+ |
|
160 |
+ return 0; |
|
161 |
+} |
|
162 |
+ |
|
163 |
+static int init_out_pool(AVFilterContext *ctx, |
|
164 |
+ int out_width, int out_height) |
|
165 |
+{ |
|
166 |
+ QSVScaleContext *s = ctx->priv; |
|
167 |
+ |
|
168 |
+ AVHWFramesContext *in_frames_ctx; |
|
169 |
+ AVHWFramesContext *out_frames_ctx; |
|
170 |
+ AVQSVFramesContext *in_frames_hwctx; |
|
171 |
+ AVQSVFramesContext *out_frames_hwctx; |
|
172 |
+ enum AVPixelFormat in_format; |
|
173 |
+ enum AVPixelFormat out_format; |
|
174 |
+ int i, ret; |
|
175 |
+ |
|
176 |
+ /* check that we have a hw context */ |
|
177 |
+ if (!ctx->inputs[0]->hw_frames_ctx) { |
|
178 |
+ av_log(ctx, AV_LOG_ERROR, "No hw context provided on input\n"); |
|
179 |
+ return AVERROR(EINVAL); |
|
180 |
+ } |
|
181 |
+ in_frames_ctx = (AVHWFramesContext*)ctx->inputs[0]->hw_frames_ctx->data; |
|
182 |
+ in_frames_hwctx = in_frames_ctx->hwctx; |
|
183 |
+ |
|
184 |
+ in_format = in_frames_ctx->sw_format; |
|
185 |
+ out_format = (s->format == AV_PIX_FMT_NONE) ? in_format : s->format; |
|
186 |
+ |
|
187 |
+ s->out_frames_ref = av_hwframe_ctx_alloc(in_frames_ctx->device_ref); |
|
188 |
+ if (!s->out_frames_ref) |
|
189 |
+ return AVERROR(ENOMEM); |
|
190 |
+ out_frames_ctx = (AVHWFramesContext*)s->out_frames_ref->data; |
|
191 |
+ out_frames_hwctx = out_frames_ctx->hwctx; |
|
192 |
+ |
|
193 |
+ out_frames_ctx->format = AV_PIX_FMT_QSV; |
|
194 |
+ out_frames_ctx->width = FFALIGN(out_width, 32); |
|
195 |
+ out_frames_ctx->height = FFALIGN(out_height, 32); |
|
196 |
+ out_frames_ctx->sw_format = out_format; |
|
197 |
+ out_frames_ctx->initial_pool_size = 32; |
|
198 |
+ |
|
199 |
+ out_frames_hwctx->frame_type = in_frames_hwctx->frame_type; |
|
200 |
+ |
|
201 |
+ ret = av_hwframe_ctx_init(s->out_frames_ref); |
|
202 |
+ if (ret < 0) |
|
203 |
+ return ret; |
|
204 |
+ |
|
205 |
+ for (i = 0; i < out_frames_hwctx->nb_surfaces; i++) { |
|
206 |
+ mfxFrameInfo *info = &out_frames_hwctx->surfaces[i].Info; |
|
207 |
+ info->CropW = out_width; |
|
208 |
+ info->CropH = out_height; |
|
209 |
+ } |
|
210 |
+ |
|
211 |
+ return 0; |
|
212 |
+} |
|
213 |
+ |
|
214 |
+static mfxStatus frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req, |
|
215 |
+ mfxFrameAllocResponse *resp) |
|
216 |
+{ |
|
217 |
+ AVFilterContext *ctx = pthis; |
|
218 |
+ QSVScaleContext *s = ctx->priv; |
|
219 |
+ |
|
220 |
+ if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET) || |
|
221 |
+ !(req->Type & (MFX_MEMTYPE_FROM_VPPIN | MFX_MEMTYPE_FROM_VPPOUT)) || |
|
222 |
+ !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME)) |
|
223 |
+ return MFX_ERR_UNSUPPORTED; |
|
224 |
+ |
|
225 |
+ if (req->Type & MFX_MEMTYPE_FROM_VPPIN) { |
|
226 |
+ resp->mids = s->mem_ids_in; |
|
227 |
+ resp->NumFrameActual = s->nb_mem_ids_in; |
|
228 |
+ } else { |
|
229 |
+ resp->mids = s->mem_ids_out; |
|
230 |
+ resp->NumFrameActual = s->nb_mem_ids_out; |
|
231 |
+ } |
|
232 |
+ |
|
233 |
+ return MFX_ERR_NONE; |
|
234 |
+} |
|
235 |
+ |
|
236 |
+static mfxStatus frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp) |
|
237 |
+{ |
|
238 |
+ return MFX_ERR_NONE; |
|
239 |
+} |
|
240 |
+ |
|
241 |
+static mfxStatus frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr) |
|
242 |
+{ |
|
243 |
+ return MFX_ERR_UNSUPPORTED; |
|
244 |
+} |
|
245 |
+ |
|
246 |
+static mfxStatus frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr) |
|
247 |
+{ |
|
248 |
+ return MFX_ERR_UNSUPPORTED; |
|
249 |
+} |
|
250 |
+ |
|
251 |
+static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl) |
|
252 |
+{ |
|
253 |
+ *hdl = mid; |
|
254 |
+ return MFX_ERR_NONE; |
|
255 |
+} |
|
256 |
+ |
|
257 |
+static const mfxHandleType handle_types[] = { |
|
258 |
+ MFX_HANDLE_VA_DISPLAY, |
|
259 |
+ MFX_HANDLE_D3D9_DEVICE_MANAGER, |
|
260 |
+ MFX_HANDLE_D3D11_DEVICE, |
|
261 |
+}; |
|
262 |
+ |
|
263 |
+static int init_out_session(AVFilterContext *ctx) |
|
264 |
+{ |
|
265 |
+ |
|
266 |
+ QSVScaleContext *s = ctx->priv; |
|
267 |
+ AVHWFramesContext *in_frames_ctx = (AVHWFramesContext*)ctx->inputs[0]->hw_frames_ctx->data; |
|
268 |
+ AVHWFramesContext *out_frames_ctx = (AVHWFramesContext*)s->out_frames_ref->data; |
|
269 |
+ AVQSVFramesContext *in_frames_hwctx = in_frames_ctx->hwctx; |
|
270 |
+ AVQSVFramesContext *out_frames_hwctx = out_frames_ctx->hwctx; |
|
271 |
+ AVQSVDeviceContext *device_hwctx = in_frames_ctx->device_ctx->hwctx; |
|
272 |
+ |
|
273 |
+ int opaque = !!(in_frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME); |
|
274 |
+ |
|
275 |
+ mfxHDL handle = NULL; |
|
276 |
+ mfxHandleType handle_type; |
|
277 |
+ mfxVersion ver; |
|
278 |
+ mfxIMPL impl; |
|
279 |
+ mfxVideoParam par; |
|
280 |
+ mfxStatus err; |
|
281 |
+ int i; |
|
282 |
+ |
|
283 |
+ /* extract the properties of the "master" session given to us */ |
|
284 |
+ err = MFXQueryIMPL(device_hwctx->session, &impl); |
|
285 |
+ if (err == MFX_ERR_NONE) |
|
286 |
+ err = MFXQueryVersion(device_hwctx->session, &ver); |
|
287 |
+ if (err != MFX_ERR_NONE) { |
|
288 |
+ av_log(ctx, AV_LOG_ERROR, "Error querying the session attributes\n"); |
|
289 |
+ return AVERROR_UNKNOWN; |
|
290 |
+ } |
|
291 |
+ |
|
292 |
+ for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) { |
|
293 |
+ err = MFXVideoCORE_GetHandle(device_hwctx->session, handle_types[i], &handle); |
|
294 |
+ if (err == MFX_ERR_NONE) { |
|
295 |
+ handle_type = handle_types[i]; |
|
296 |
+ break; |
|
297 |
+ } |
|
298 |
+ } |
|
299 |
+ |
|
300 |
+ /* create a "slave" session with those same properties, to be used for |
|
301 |
+ * actual scaling */ |
|
302 |
+ err = MFXInit(impl, &ver, &s->session); |
|
303 |
+ if (err != MFX_ERR_NONE) { |
|
304 |
+ av_log(ctx, AV_LOG_ERROR, "Error initializing a session for scaling\n"); |
|
305 |
+ return AVERROR_UNKNOWN; |
|
306 |
+ } |
|
307 |
+ |
|
308 |
+ if (handle) { |
|
309 |
+ err = MFXVideoCORE_SetHandle(s->session, handle_type, handle); |
|
310 |
+ if (err != MFX_ERR_NONE) |
|
311 |
+ return AVERROR_UNKNOWN; |
|
312 |
+ } |
|
313 |
+ |
|
314 |
+ memset(&par, 0, sizeof(par)); |
|
315 |
+ |
|
316 |
+ if (opaque) { |
|
317 |
+ s->surface_ptrs_in = av_mallocz_array(in_frames_hwctx->nb_surfaces, |
|
318 |
+ sizeof(*s->surface_ptrs_in)); |
|
319 |
+ if (!s->surface_ptrs_in) |
|
320 |
+ return AVERROR(ENOMEM); |
|
321 |
+ for (i = 0; i < in_frames_hwctx->nb_surfaces; i++) |
|
322 |
+ s->surface_ptrs_in[i] = in_frames_hwctx->surfaces + i; |
|
323 |
+ s->nb_surface_ptrs_in = in_frames_hwctx->nb_surfaces; |
|
324 |
+ |
|
325 |
+ s->surface_ptrs_out = av_mallocz_array(out_frames_hwctx->nb_surfaces, |
|
326 |
+ sizeof(*s->surface_ptrs_out)); |
|
327 |
+ if (!s->surface_ptrs_out) |
|
328 |
+ return AVERROR(ENOMEM); |
|
329 |
+ for (i = 0; i < out_frames_hwctx->nb_surfaces; i++) |
|
330 |
+ s->surface_ptrs_out[i] = out_frames_hwctx->surfaces + i; |
|
331 |
+ s->nb_surface_ptrs_out = out_frames_hwctx->nb_surfaces; |
|
332 |
+ |
|
333 |
+ s->opaque_alloc.In.Surfaces = s->surface_ptrs_in; |
|
334 |
+ s->opaque_alloc.In.NumSurface = s->nb_surface_ptrs_in; |
|
335 |
+ s->opaque_alloc.In.Type = in_frames_hwctx->frame_type; |
|
336 |
+ |
|
337 |
+ s->opaque_alloc.Out.Surfaces = s->surface_ptrs_out; |
|
338 |
+ s->opaque_alloc.Out.NumSurface = s->nb_surface_ptrs_out; |
|
339 |
+ s->opaque_alloc.Out.Type = out_frames_hwctx->frame_type; |
|
340 |
+ |
|
341 |
+ s->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION; |
|
342 |
+ s->opaque_alloc.Header.BufferSz = sizeof(s->opaque_alloc); |
|
343 |
+ |
|
344 |
+ s->ext_buffers[0] = (mfxExtBuffer*)&s->opaque_alloc; |
|
345 |
+ |
|
346 |
+ par.ExtParam = s->ext_buffers; |
|
347 |
+ par.NumExtParam = FF_ARRAY_ELEMS(s->ext_buffers); |
|
348 |
+ |
|
349 |
+ par.IOPattern = MFX_IOPATTERN_IN_OPAQUE_MEMORY | MFX_IOPATTERN_OUT_OPAQUE_MEMORY; |
|
350 |
+ } else { |
|
351 |
+ mfxFrameAllocator frame_allocator = { |
|
352 |
+ .pthis = ctx, |
|
353 |
+ .Alloc = frame_alloc, |
|
354 |
+ .Lock = frame_lock, |
|
355 |
+ .Unlock = frame_unlock, |
|
356 |
+ .GetHDL = frame_get_hdl, |
|
357 |
+ .Free = frame_free, |
|
358 |
+ }; |
|
359 |
+ |
|
360 |
+ s->mem_ids_in = av_mallocz_array(in_frames_hwctx->nb_surfaces, |
|
361 |
+ sizeof(*s->mem_ids_in)); |
|
362 |
+ if (!s->mem_ids_in) |
|
363 |
+ return AVERROR(ENOMEM); |
|
364 |
+ for (i = 0; i < in_frames_hwctx->nb_surfaces; i++) |
|
365 |
+ s->mem_ids_in[i] = in_frames_hwctx->surfaces[i].Data.MemId; |
|
366 |
+ s->nb_mem_ids_in = in_frames_hwctx->nb_surfaces; |
|
367 |
+ |
|
368 |
+ s->mem_ids_out = av_mallocz_array(out_frames_hwctx->nb_surfaces, |
|
369 |
+ sizeof(*s->mem_ids_out)); |
|
370 |
+ if (!s->mem_ids_out) |
|
371 |
+ return AVERROR(ENOMEM); |
|
372 |
+ for (i = 0; i < out_frames_hwctx->nb_surfaces; i++) |
|
373 |
+ s->mem_ids_out[i] = out_frames_hwctx->surfaces[i].Data.MemId; |
|
374 |
+ s->nb_mem_ids_out = out_frames_hwctx->nb_surfaces; |
|
375 |
+ |
|
376 |
+ err = MFXVideoCORE_SetFrameAllocator(s->session, &frame_allocator); |
|
377 |
+ if (err != MFX_ERR_NONE) |
|
378 |
+ return AVERROR_UNKNOWN; |
|
379 |
+ |
|
380 |
+ par.IOPattern = MFX_IOPATTERN_IN_VIDEO_MEMORY | MFX_IOPATTERN_OUT_VIDEO_MEMORY; |
|
381 |
+ } |
|
382 |
+ |
|
383 |
+ par.AsyncDepth = 1; // TODO async |
|
384 |
+ |
|
385 |
+ par.vpp.In = in_frames_hwctx->surfaces[0].Info; |
|
386 |
+ par.vpp.Out = out_frames_hwctx->surfaces[0].Info; |
|
387 |
+ |
|
388 |
+ /* Apparently VPP requires the frame rate to be set to some value, otherwise |
|
389 |
+ * init will fail (probably for the framerate conversion filter). Since we |
|
390 |
+ * are only doing scaling here, we just invent an arbitrary |
|
391 |
+ * value */ |
|
392 |
+ par.vpp.In.FrameRateExtN = 25; |
|
393 |
+ par.vpp.In.FrameRateExtD = 1; |
|
394 |
+ par.vpp.Out.FrameRateExtN = 25; |
|
395 |
+ par.vpp.Out.FrameRateExtD = 1; |
|
396 |
+ |
|
397 |
+ err = MFXVideoVPP_Init(s->session, &par); |
|
398 |
+ if (err != MFX_ERR_NONE) { |
|
399 |
+ av_log(ctx, AV_LOG_ERROR, "Error opening the VPP for scaling\n"); |
|
400 |
+ return AVERROR_UNKNOWN; |
|
401 |
+ } |
|
402 |
+ |
|
403 |
+ return 0; |
|
404 |
+} |
|
405 |
+ |
|
406 |
+static int init_scale_session(AVFilterContext *ctx, int in_width, int in_height, |
|
407 |
+ int out_width, int out_height) |
|
408 |
+{ |
|
409 |
+ QSVScaleContext *s = ctx->priv; |
|
410 |
+ |
|
411 |
+ int ret; |
|
412 |
+ |
|
413 |
+ qsvscale_uninit(ctx); |
|
414 |
+ |
|
415 |
+ ret = init_out_pool(ctx, out_width, out_height); |
|
416 |
+ if (ret < 0) |
|
417 |
+ return ret; |
|
418 |
+ |
|
419 |
+ ret = init_out_session(ctx); |
|
420 |
+ if (ret < 0) |
|
421 |
+ return ret; |
|
422 |
+ |
|
423 |
+ av_buffer_unref(&ctx->outputs[0]->hw_frames_ctx); |
|
424 |
+ ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(s->out_frames_ref); |
|
425 |
+ if (!ctx->outputs[0]->hw_frames_ctx) |
|
426 |
+ return AVERROR(ENOMEM); |
|
427 |
+ |
|
428 |
+ return 0; |
|
429 |
+} |
|
430 |
+ |
|
431 |
+static int qsvscale_config_props(AVFilterLink *outlink) |
|
432 |
+{ |
|
433 |
+ AVFilterContext *ctx = outlink->src; |
|
434 |
+ AVFilterLink *inlink = outlink->src->inputs[0]; |
|
435 |
+ QSVScaleContext *s = ctx->priv; |
|
436 |
+ int64_t w, h; |
|
437 |
+ double var_values[VARS_NB], res; |
|
438 |
+ char *expr; |
|
439 |
+ int ret; |
|
440 |
+ |
|
441 |
+ var_values[VAR_PI] = M_PI; |
|
442 |
+ var_values[VAR_PHI] = M_PHI; |
|
443 |
+ var_values[VAR_E] = M_E; |
|
444 |
+ var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w; |
|
445 |
+ var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h; |
|
446 |
+ var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN; |
|
447 |
+ var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN; |
|
448 |
+ var_values[VAR_A] = (double) inlink->w / inlink->h; |
|
449 |
+ var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ? |
|
450 |
+ (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1; |
|
451 |
+ var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR]; |
|
452 |
+ |
|
453 |
+ /* evaluate width and height */ |
|
454 |
+ av_expr_parse_and_eval(&res, (expr = s->w_expr), |
|
455 |
+ var_names, var_values, |
|
456 |
+ NULL, NULL, NULL, NULL, NULL, 0, ctx); |
|
457 |
+ s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res; |
|
458 |
+ if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr), |
|
459 |
+ var_names, var_values, |
|
460 |
+ NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) |
|
461 |
+ goto fail; |
|
462 |
+ s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res; |
|
463 |
+ /* evaluate again the width, as it may depend on the output height */ |
|
464 |
+ if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr), |
|
465 |
+ var_names, var_values, |
|
466 |
+ NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) |
|
467 |
+ goto fail; |
|
468 |
+ s->w = res; |
|
469 |
+ |
|
470 |
+ w = s->w; |
|
471 |
+ h = s->h; |
|
472 |
+ |
|
473 |
+ /* sanity check params */ |
|
474 |
+ if (w < -1 || h < -1) { |
|
475 |
+ av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n"); |
|
476 |
+ return AVERROR(EINVAL); |
|
477 |
+ } |
|
478 |
+ if (w == -1 && h == -1) |
|
479 |
+ s->w = s->h = 0; |
|
480 |
+ |
|
481 |
+ if (!(w = s->w)) |
|
482 |
+ w = inlink->w; |
|
483 |
+ if (!(h = s->h)) |
|
484 |
+ h = inlink->h; |
|
485 |
+ if (w == -1) |
|
486 |
+ w = av_rescale(h, inlink->w, inlink->h); |
|
487 |
+ if (h == -1) |
|
488 |
+ h = av_rescale(w, inlink->h, inlink->w); |
|
489 |
+ |
|
490 |
+ if (w > INT_MAX || h > INT_MAX || |
|
491 |
+ (h * inlink->w) > INT_MAX || |
|
492 |
+ (w * inlink->h) > INT_MAX) |
|
493 |
+ av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n"); |
|
494 |
+ |
|
495 |
+ outlink->w = w; |
|
496 |
+ outlink->h = h; |
|
497 |
+ |
|
498 |
+ ret = init_scale_session(ctx, inlink->w, inlink->h, w, h); |
|
499 |
+ if (ret < 0) |
|
500 |
+ return ret; |
|
501 |
+ |
|
502 |
+ av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d\n", |
|
503 |
+ inlink->w, inlink->h, outlink->w, outlink->h); |
|
504 |
+ |
|
505 |
+ if (inlink->sample_aspect_ratio.num) |
|
506 |
+ outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w, |
|
507 |
+ outlink->w*inlink->h}, |
|
508 |
+ inlink->sample_aspect_ratio); |
|
509 |
+ else |
|
510 |
+ outlink->sample_aspect_ratio = inlink->sample_aspect_ratio; |
|
511 |
+ |
|
512 |
+ return 0; |
|
513 |
+ |
|
514 |
+fail: |
|
515 |
+ av_log(NULL, AV_LOG_ERROR, |
|
516 |
+ "Error when evaluating the expression '%s'\n", expr); |
|
517 |
+ return ret; |
|
518 |
+} |
|
519 |
+ |
|
520 |
+static int qsvscale_filter_frame(AVFilterLink *link, AVFrame *in) |
|
521 |
+{ |
|
522 |
+ AVFilterContext *ctx = link->dst; |
|
523 |
+ QSVScaleContext *s = ctx->priv; |
|
524 |
+ AVFilterLink *outlink = ctx->outputs[0]; |
|
525 |
+ |
|
526 |
+ mfxSyncPoint sync = NULL; |
|
527 |
+ mfxStatus err; |
|
528 |
+ |
|
529 |
+ AVFrame *out = NULL; |
|
530 |
+ int ret = 0; |
|
531 |
+ |
|
532 |
+ out = av_frame_alloc(); |
|
533 |
+ if (!out) { |
|
534 |
+ ret = AVERROR(ENOMEM); |
|
535 |
+ goto fail; |
|
536 |
+ } |
|
537 |
+ |
|
538 |
+ ret = av_hwframe_get_buffer(s->out_frames_ref, out, 0); |
|
539 |
+ if (ret < 0) |
|
540 |
+ goto fail; |
|
541 |
+ |
|
542 |
+ do { |
|
543 |
+ err = MFXVideoVPP_RunFrameVPPAsync(s->session, |
|
544 |
+ (mfxFrameSurface1*)in->data[3], |
|
545 |
+ (mfxFrameSurface1*)out->data[3], |
|
546 |
+ NULL, &sync); |
|
547 |
+ if (err == MFX_WRN_DEVICE_BUSY) |
|
548 |
+ av_usleep(1); |
|
549 |
+ } while (err == MFX_WRN_DEVICE_BUSY); |
|
550 |
+ |
|
551 |
+ if (err < 0 || !sync) { |
|
552 |
+ av_log(ctx, AV_LOG_ERROR, "Error during scaling\n"); |
|
553 |
+ ret = AVERROR_UNKNOWN; |
|
554 |
+ goto fail; |
|
555 |
+ } |
|
556 |
+ |
|
557 |
+ do { |
|
558 |
+ err = MFXVideoCORE_SyncOperation(s->session, sync, 1000); |
|
559 |
+ } while (err == MFX_WRN_IN_EXECUTION); |
|
560 |
+ if (err < 0) { |
|
561 |
+ av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation: %d\n", err); |
|
562 |
+ ret = AVERROR_UNKNOWN; |
|
563 |
+ goto fail; |
|
564 |
+ } |
|
565 |
+ |
|
566 |
+ ret = av_frame_copy_props(out, in); |
|
567 |
+ if (ret < 0) |
|
568 |
+ goto fail; |
|
569 |
+ |
|
570 |
+ out->width = outlink->w; |
|
571 |
+ out->height = outlink->h; |
|
572 |
+ |
|
573 |
+ av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den, |
|
574 |
+ (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w, |
|
575 |
+ (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h, |
|
576 |
+ INT_MAX); |
|
577 |
+ |
|
578 |
+ av_frame_free(&in); |
|
579 |
+ return ff_filter_frame(outlink, out); |
|
580 |
+fail: |
|
581 |
+ av_frame_free(&in); |
|
582 |
+ av_frame_free(&out); |
|
583 |
+ return ret; |
|
584 |
+} |
|
585 |
+ |
|
586 |
+#define OFFSET(x) offsetof(QSVScaleContext, x) |
|
587 |
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM |
|
588 |
+static const AVOption options[] = { |
|
589 |
+ { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS }, |
|
590 |
+ { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS }, |
|
591 |
+ { "format", "Output pixel format", OFFSET(format_str), AV_OPT_TYPE_STRING, { .str = "same" }, .flags = FLAGS }, |
|
592 |
+ |
|
593 |
+ { NULL }, |
|
594 |
+}; |
|
595 |
+ |
|
596 |
+static const AVClass qsvscale_class = { |
|
597 |
+ .class_name = "qsvscale", |
|
598 |
+ .item_name = av_default_item_name, |
|
599 |
+ .option = options, |
|
600 |
+ .version = LIBAVUTIL_VERSION_INT, |
|
601 |
+}; |
|
602 |
+ |
|
603 |
+static const AVFilterPad qsvscale_inputs[] = { |
|
604 |
+ { |
|
605 |
+ .name = "default", |
|
606 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
607 |
+ .filter_frame = qsvscale_filter_frame, |
|
608 |
+ }, |
|
609 |
+ { NULL } |
|
610 |
+}; |
|
611 |
+ |
|
612 |
+static const AVFilterPad qsvscale_outputs[] = { |
|
613 |
+ { |
|
614 |
+ .name = "default", |
|
615 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
616 |
+ .config_props = qsvscale_config_props, |
|
617 |
+ }, |
|
618 |
+ { NULL } |
|
619 |
+}; |
|
620 |
+ |
|
621 |
+AVFilter ff_vf_scale_qsv = { |
|
622 |
+ .name = "scale_qsv", |
|
623 |
+ .description = NULL_IF_CONFIG_SMALL("QuickSync video scaling and format conversion"), |
|
624 |
+ |
|
625 |
+ .init = qsvscale_init, |
|
626 |
+ .uninit = qsvscale_uninit, |
|
627 |
+ .query_formats = qsvscale_query_formats, |
|
628 |
+ |
|
629 |
+ .priv_size = sizeof(QSVScaleContext), |
|
630 |
+ .priv_class = &qsvscale_class, |
|
631 |
+ |
|
632 |
+ .inputs = qsvscale_inputs, |
|
633 |
+ .outputs = qsvscale_outputs, |
|
634 |
+}; |