2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * scale video filter - QSV
24 #include <mfx/mfxvideo.h>
29 #include "libavutil/avstring.h"
30 #include "libavutil/common.h"
31 #include "libavutil/eval.h"
32 #include "libavutil/hwcontext.h"
33 #include "libavutil/hwcontext_qsv.h"
34 #include "libavutil/internal.h"
35 #include "libavutil/mathematics.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/pixdesc.h"
38 #include "libavutil/time.h"
39 #include "libavfilter/qsvvpp.h"
46 static const char *const var_names[] = {
72 typedef struct QSVScaleContext {
75 /* a clone of the main session, used internally for scaling */
81 mfxMemId *mem_ids_out;
84 mfxFrameSurface1 **surface_ptrs_in;
85 int nb_surface_ptrs_in;
87 mfxFrameSurface1 **surface_ptrs_out;
88 int nb_surface_ptrs_out;
90 mfxExtOpaqueSurfaceAlloc opaque_alloc;
91 mfxExtBuffer *ext_buffers[1];
93 int shift_width, shift_height;
96 * New dimensions. Special values are:
97 * 0 = original width/height
98 * -1 = keep original aspect
103 * Output sw format. AV_PIX_FMT_NONE for no conversion.
105 enum AVPixelFormat format;
107 char *w_expr; ///< width expression string
108 char *h_expr; ///< height expression string
112 static int qsvscale_init(AVFilterContext *ctx)
114 QSVScaleContext *s = ctx->priv;
116 if (!strcmp(s->format_str, "same")) {
117 s->format = AV_PIX_FMT_NONE;
119 s->format = av_get_pix_fmt(s->format_str);
120 if (s->format == AV_PIX_FMT_NONE) {
121 av_log(ctx, AV_LOG_ERROR, "Unrecognized pixel format: %s\n", s->format_str);
122 return AVERROR(EINVAL);
129 static void qsvscale_uninit(AVFilterContext *ctx)
131 QSVScaleContext *s = ctx->priv;
134 MFXClose(s->session);
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;
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;
149 static int qsvscale_query_formats(AVFilterContext *ctx)
151 static const enum AVPixelFormat pixel_formats[] = {
152 AV_PIX_FMT_QSV, AV_PIX_FMT_NONE,
154 AVFilterFormats *pix_fmts = ff_make_format_list(pixel_formats);
157 if ((ret = ff_set_common_formats(ctx, pix_fmts)) < 0)
163 static int init_out_pool(AVFilterContext *ctx,
164 int out_width, int out_height)
166 QSVScaleContext *s = ctx->priv;
167 AVFilterLink *outlink = ctx->outputs[0];
169 AVHWFramesContext *in_frames_ctx;
170 AVHWFramesContext *out_frames_ctx;
171 AVQSVFramesContext *in_frames_hwctx;
172 AVQSVFramesContext *out_frames_hwctx;
173 enum AVPixelFormat in_format;
174 enum AVPixelFormat out_format;
177 /* check that we have a hw context */
178 if (!ctx->inputs[0]->hw_frames_ctx) {
179 av_log(ctx, AV_LOG_ERROR, "No hw context provided on input\n");
180 return AVERROR(EINVAL);
182 in_frames_ctx = (AVHWFramesContext*)ctx->inputs[0]->hw_frames_ctx->data;
183 in_frames_hwctx = in_frames_ctx->hwctx;
185 in_format = in_frames_ctx->sw_format;
186 out_format = (s->format == AV_PIX_FMT_NONE) ? in_format : s->format;
188 outlink->hw_frames_ctx = av_hwframe_ctx_alloc(in_frames_ctx->device_ref);
189 if (!outlink->hw_frames_ctx)
190 return AVERROR(ENOMEM);
191 out_frames_ctx = (AVHWFramesContext*)outlink->hw_frames_ctx->data;
192 out_frames_hwctx = out_frames_ctx->hwctx;
194 out_frames_ctx->format = AV_PIX_FMT_QSV;
195 out_frames_ctx->width = FFALIGN(out_width, 32);
196 out_frames_ctx->height = FFALIGN(out_height, 32);
197 out_frames_ctx->sw_format = out_format;
198 out_frames_ctx->initial_pool_size = 4;
200 out_frames_hwctx->frame_type = in_frames_hwctx->frame_type;
202 ret = ff_filter_init_hw_frames(ctx, outlink, 32);
206 ret = av_hwframe_ctx_init(outlink->hw_frames_ctx);
210 for (i = 0; i < out_frames_hwctx->nb_surfaces; i++) {
211 mfxFrameInfo *info = &out_frames_hwctx->surfaces[i].Info;
212 info->CropW = out_width;
213 info->CropH = out_height;
219 static mfxStatus frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
220 mfxFrameAllocResponse *resp)
222 AVFilterContext *ctx = pthis;
223 QSVScaleContext *s = ctx->priv;
225 if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET) ||
226 !(req->Type & (MFX_MEMTYPE_FROM_VPPIN | MFX_MEMTYPE_FROM_VPPOUT)) ||
227 !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
228 return MFX_ERR_UNSUPPORTED;
230 if (req->Type & MFX_MEMTYPE_FROM_VPPIN) {
231 resp->mids = s->mem_ids_in;
232 resp->NumFrameActual = s->nb_mem_ids_in;
234 resp->mids = s->mem_ids_out;
235 resp->NumFrameActual = s->nb_mem_ids_out;
241 static mfxStatus frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
246 static mfxStatus frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
248 return MFX_ERR_UNSUPPORTED;
251 static mfxStatus frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
253 return MFX_ERR_UNSUPPORTED;
256 static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
262 static const mfxHandleType handle_types[] = {
263 MFX_HANDLE_VA_DISPLAY,
264 MFX_HANDLE_D3D9_DEVICE_MANAGER,
265 MFX_HANDLE_D3D11_DEVICE,
268 static int init_out_session(AVFilterContext *ctx)
271 QSVScaleContext *s = ctx->priv;
272 AVHWFramesContext *in_frames_ctx = (AVHWFramesContext*)ctx->inputs[0]->hw_frames_ctx->data;
273 AVHWFramesContext *out_frames_ctx = (AVHWFramesContext*)ctx->outputs[0]->hw_frames_ctx->data;
274 AVQSVFramesContext *in_frames_hwctx = in_frames_ctx->hwctx;
275 AVQSVFramesContext *out_frames_hwctx = out_frames_ctx->hwctx;
276 AVQSVDeviceContext *device_hwctx = in_frames_ctx->device_ctx->hwctx;
278 int opaque = !!(in_frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
280 mfxHDL handle = NULL;
281 mfxHandleType handle_type;
288 /* extract the properties of the "master" session given to us */
289 err = MFXQueryIMPL(device_hwctx->session, &impl);
290 if (err == MFX_ERR_NONE)
291 err = MFXQueryVersion(device_hwctx->session, &ver);
292 if (err != MFX_ERR_NONE) {
293 av_log(ctx, AV_LOG_ERROR, "Error querying the session attributes\n");
294 return AVERROR_UNKNOWN;
297 for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
298 err = MFXVideoCORE_GetHandle(device_hwctx->session, handle_types[i], &handle);
299 if (err == MFX_ERR_NONE) {
300 handle_type = handle_types[i];
305 /* create a "slave" session with those same properties, to be used for
307 err = MFXInit(impl, &ver, &s->session);
308 if (err != MFX_ERR_NONE) {
309 av_log(ctx, AV_LOG_ERROR, "Error initializing a session for scaling\n");
310 return AVERROR_UNKNOWN;
314 err = MFXVideoCORE_SetHandle(s->session, handle_type, handle);
315 if (err != MFX_ERR_NONE)
316 return AVERROR_UNKNOWN;
319 if (QSV_RUNTIME_VERSION_ATLEAST(ver, 1, 25)) {
320 err = MFXJoinSession(device_hwctx->session, s->session);
321 if (err != MFX_ERR_NONE)
322 return AVERROR_UNKNOWN;
325 memset(&par, 0, sizeof(par));
328 s->surface_ptrs_in = av_mallocz_array(in_frames_hwctx->nb_surfaces,
329 sizeof(*s->surface_ptrs_in));
330 if (!s->surface_ptrs_in)
331 return AVERROR(ENOMEM);
332 for (i = 0; i < in_frames_hwctx->nb_surfaces; i++)
333 s->surface_ptrs_in[i] = in_frames_hwctx->surfaces + i;
334 s->nb_surface_ptrs_in = in_frames_hwctx->nb_surfaces;
336 s->surface_ptrs_out = av_mallocz_array(out_frames_hwctx->nb_surfaces,
337 sizeof(*s->surface_ptrs_out));
338 if (!s->surface_ptrs_out)
339 return AVERROR(ENOMEM);
340 for (i = 0; i < out_frames_hwctx->nb_surfaces; i++)
341 s->surface_ptrs_out[i] = out_frames_hwctx->surfaces + i;
342 s->nb_surface_ptrs_out = out_frames_hwctx->nb_surfaces;
344 s->opaque_alloc.In.Surfaces = s->surface_ptrs_in;
345 s->opaque_alloc.In.NumSurface = s->nb_surface_ptrs_in;
346 s->opaque_alloc.In.Type = in_frames_hwctx->frame_type;
348 s->opaque_alloc.Out.Surfaces = s->surface_ptrs_out;
349 s->opaque_alloc.Out.NumSurface = s->nb_surface_ptrs_out;
350 s->opaque_alloc.Out.Type = out_frames_hwctx->frame_type;
352 s->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
353 s->opaque_alloc.Header.BufferSz = sizeof(s->opaque_alloc);
355 s->ext_buffers[0] = (mfxExtBuffer*)&s->opaque_alloc;
357 par.ExtParam = s->ext_buffers;
358 par.NumExtParam = FF_ARRAY_ELEMS(s->ext_buffers);
360 par.IOPattern = MFX_IOPATTERN_IN_OPAQUE_MEMORY | MFX_IOPATTERN_OUT_OPAQUE_MEMORY;
362 mfxFrameAllocator frame_allocator = {
364 .Alloc = frame_alloc,
366 .Unlock = frame_unlock,
367 .GetHDL = frame_get_hdl,
371 s->mem_ids_in = av_mallocz_array(in_frames_hwctx->nb_surfaces,
372 sizeof(*s->mem_ids_in));
374 return AVERROR(ENOMEM);
375 for (i = 0; i < in_frames_hwctx->nb_surfaces; i++)
376 s->mem_ids_in[i] = in_frames_hwctx->surfaces[i].Data.MemId;
377 s->nb_mem_ids_in = in_frames_hwctx->nb_surfaces;
379 s->mem_ids_out = av_mallocz_array(out_frames_hwctx->nb_surfaces,
380 sizeof(*s->mem_ids_out));
382 return AVERROR(ENOMEM);
383 for (i = 0; i < out_frames_hwctx->nb_surfaces; i++)
384 s->mem_ids_out[i] = out_frames_hwctx->surfaces[i].Data.MemId;
385 s->nb_mem_ids_out = out_frames_hwctx->nb_surfaces;
387 err = MFXVideoCORE_SetFrameAllocator(s->session, &frame_allocator);
388 if (err != MFX_ERR_NONE)
389 return AVERROR_UNKNOWN;
391 par.IOPattern = MFX_IOPATTERN_IN_VIDEO_MEMORY | MFX_IOPATTERN_OUT_VIDEO_MEMORY;
394 par.AsyncDepth = 1; // TODO async
396 par.vpp.In = in_frames_hwctx->surfaces[0].Info;
397 par.vpp.Out = out_frames_hwctx->surfaces[0].Info;
399 /* Apparently VPP requires the frame rate to be set to some value, otherwise
400 * init will fail (probably for the framerate conversion filter). Since we
401 * are only doing scaling here, we just invent an arbitrary
403 par.vpp.In.FrameRateExtN = 25;
404 par.vpp.In.FrameRateExtD = 1;
405 par.vpp.Out.FrameRateExtN = 25;
406 par.vpp.Out.FrameRateExtD = 1;
408 err = MFXVideoVPP_Init(s->session, &par);
409 if (err != MFX_ERR_NONE) {
410 av_log(ctx, AV_LOG_ERROR, "Error opening the VPP for scaling\n");
411 return AVERROR_UNKNOWN;
417 static int init_scale_session(AVFilterContext *ctx, int in_width, int in_height,
418 int out_width, int out_height)
422 qsvscale_uninit(ctx);
424 ret = init_out_pool(ctx, out_width, out_height);
428 ret = init_out_session(ctx);
435 static int qsvscale_config_props(AVFilterLink *outlink)
437 AVFilterContext *ctx = outlink->src;
438 AVFilterLink *inlink = outlink->src->inputs[0];
439 QSVScaleContext *s = ctx->priv;
441 double var_values[VARS_NB], res;
445 var_values[VAR_PI] = M_PI;
446 var_values[VAR_PHI] = M_PHI;
447 var_values[VAR_E] = M_E;
448 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
449 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
450 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
451 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
452 var_values[VAR_A] = (double) inlink->w / inlink->h;
453 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
454 (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
455 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
457 /* evaluate width and height */
458 av_expr_parse_and_eval(&res, (expr = s->w_expr),
459 var_names, var_values,
460 NULL, NULL, NULL, NULL, NULL, 0, ctx);
461 s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
462 if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr),
463 var_names, var_values,
464 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
466 s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
467 /* evaluate again the width, as it may depend on the output height */
468 if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
469 var_names, var_values,
470 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
477 /* sanity check params */
478 if (w < -1 || h < -1) {
479 av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
480 return AVERROR(EINVAL);
482 if (w == -1 && h == -1)
490 w = av_rescale(h, inlink->w, inlink->h);
492 h = av_rescale(w, inlink->h, inlink->w);
494 if (w > INT_MAX || h > INT_MAX ||
495 (h * inlink->w) > INT_MAX ||
496 (w * inlink->h) > INT_MAX)
497 av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
502 ret = init_scale_session(ctx, inlink->w, inlink->h, w, h);
506 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d\n",
507 inlink->w, inlink->h, outlink->w, outlink->h);
509 if (inlink->sample_aspect_ratio.num)
510 outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
511 outlink->w*inlink->h},
512 inlink->sample_aspect_ratio);
514 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
519 av_log(NULL, AV_LOG_ERROR,
520 "Error when evaluating the expression '%s'\n", expr);
524 static int qsvscale_filter_frame(AVFilterLink *link, AVFrame *in)
526 AVFilterContext *ctx = link->dst;
527 QSVScaleContext *s = ctx->priv;
528 AVFilterLink *outlink = ctx->outputs[0];
530 mfxSyncPoint sync = NULL;
536 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
538 ret = AVERROR(ENOMEM);
543 err = MFXVideoVPP_RunFrameVPPAsync(s->session,
544 (mfxFrameSurface1*)in->data[3],
545 (mfxFrameSurface1*)out->data[3],
547 if (err == MFX_WRN_DEVICE_BUSY)
549 } while (err == MFX_WRN_DEVICE_BUSY);
551 if (err < 0 || !sync) {
552 av_log(ctx, AV_LOG_ERROR, "Error during scaling\n");
553 ret = AVERROR_UNKNOWN;
558 err = MFXVideoCORE_SyncOperation(s->session, sync, 1000);
559 } while (err == MFX_WRN_IN_EXECUTION);
561 av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation: %d\n", err);
562 ret = AVERROR_UNKNOWN;
566 ret = av_frame_copy_props(out, in);
570 out->width = outlink->w;
571 out->height = outlink->h;
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,
579 return ff_filter_frame(outlink, out);
586 #define OFFSET(x) offsetof(QSVScaleContext, x)
587 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_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 },
596 static const AVClass qsvscale_class = {
597 .class_name = "qsvscale",
598 .item_name = av_default_item_name,
600 .version = LIBAVUTIL_VERSION_INT,
603 static const AVFilterPad qsvscale_inputs[] = {
606 .type = AVMEDIA_TYPE_VIDEO,
607 .filter_frame = qsvscale_filter_frame,
612 static const AVFilterPad qsvscale_outputs[] = {
615 .type = AVMEDIA_TYPE_VIDEO,
616 .config_props = qsvscale_config_props,
621 AVFilter ff_vf_scale_qsv = {
623 .description = NULL_IF_CONFIG_SMALL("QuickSync video scaling and format conversion"),
625 .init = qsvscale_init,
626 .uninit = qsvscale_uninit,
627 .query_formats = qsvscale_query_formats,
629 .priv_size = sizeof(QSVScaleContext),
630 .priv_class = &qsvscale_class,
632 .inputs = qsvscale_inputs,
633 .outputs = qsvscale_outputs,
635 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,