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 * deinterlace video filter - QSV
24 #include <mfx/mfxvideo.h>
29 #include "libavutil/avstring.h"
30 #include "libavutil/common.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 #include "libavfilter/qsvvpp.h"
46 QSVDEINT_MORE_OUTPUT = 1,
50 typedef struct QSVFrame {
52 mfxFrameSurface1 surface;
55 struct QSVFrame *next;
58 typedef struct QSVDeintContext {
61 AVBufferRef *hw_frames_ctx;
62 /* a clone of the main session, used internally for deinterlacing */
68 mfxFrameSurface1 **surface_ptrs;
71 mfxExtOpaqueSurfaceAlloc opaque_alloc;
72 mfxExtVPPDeinterlacing deint_conf;
73 mfxExtBuffer *ext_buffers[2];
76 QSVFrame *work_frames;
82 /* option for Deinterlacing algorithm to be used */
86 static void qsvdeint_uninit(AVFilterContext *ctx)
88 QSVDeintContext *s = ctx->priv;
95 av_buffer_unref(&s->hw_frames_ctx);
99 s->work_frames = cur->next;
100 av_frame_free(&cur->frame);
102 cur = s->work_frames;
105 av_freep(&s->mem_ids);
108 av_freep(&s->surface_ptrs);
109 s->nb_surface_ptrs = 0;
112 static int qsvdeint_query_formats(AVFilterContext *ctx)
114 static const enum AVPixelFormat pixel_formats[] = {
115 AV_PIX_FMT_QSV, AV_PIX_FMT_NONE,
117 AVFilterFormats *pix_fmts = ff_make_format_list(pixel_formats);
120 if ((ret = ff_set_common_formats(ctx, pix_fmts)) < 0)
126 static mfxStatus frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
127 mfxFrameAllocResponse *resp)
129 AVFilterContext *ctx = pthis;
130 QSVDeintContext *s = ctx->priv;
132 if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET) ||
133 !(req->Type & (MFX_MEMTYPE_FROM_VPPIN | MFX_MEMTYPE_FROM_VPPOUT)) ||
134 !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
135 return MFX_ERR_UNSUPPORTED;
137 resp->mids = s->mem_ids;
138 resp->NumFrameActual = s->nb_mem_ids;
143 static mfxStatus frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
148 static mfxStatus frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
150 return MFX_ERR_UNSUPPORTED;
153 static mfxStatus frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
155 return MFX_ERR_UNSUPPORTED;
158 static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
164 static const mfxHandleType handle_types[] = {
165 MFX_HANDLE_VA_DISPLAY,
166 MFX_HANDLE_D3D9_DEVICE_MANAGER,
167 MFX_HANDLE_D3D11_DEVICE,
170 static int init_out_session(AVFilterContext *ctx)
173 QSVDeintContext *s = ctx->priv;
174 AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)s->hw_frames_ctx->data;
175 AVQSVFramesContext *hw_frames_hwctx = hw_frames_ctx->hwctx;
176 AVQSVDeviceContext *device_hwctx = hw_frames_ctx->device_ctx->hwctx;
178 int opaque = !!(hw_frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
180 mfxHDL handle = NULL;
181 mfxHandleType handle_type;
188 /* extract the properties of the "master" session given to us */
189 err = MFXQueryIMPL(device_hwctx->session, &impl);
190 if (err == MFX_ERR_NONE)
191 err = MFXQueryVersion(device_hwctx->session, &ver);
192 if (err != MFX_ERR_NONE) {
193 av_log(ctx, AV_LOG_ERROR, "Error querying the session attributes\n");
194 return AVERROR_UNKNOWN;
197 for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
198 err = MFXVideoCORE_GetHandle(device_hwctx->session, handle_types[i], &handle);
199 if (err == MFX_ERR_NONE) {
200 handle_type = handle_types[i];
205 if (err != MFX_ERR_NONE) {
206 av_log(ctx, AV_LOG_ERROR, "Error getting the session handle\n");
207 return AVERROR_UNKNOWN;
210 /* create a "slave" session with those same properties, to be used for
211 * actual deinterlacing */
212 err = MFXInit(impl, &ver, &s->session);
213 if (err != MFX_ERR_NONE) {
214 av_log(ctx, AV_LOG_ERROR, "Error initializing a session for deinterlacing\n");
215 return AVERROR_UNKNOWN;
219 err = MFXVideoCORE_SetHandle(s->session, handle_type, handle);
220 if (err != MFX_ERR_NONE)
221 return AVERROR_UNKNOWN;
224 if (QSV_RUNTIME_VERSION_ATLEAST(ver, 1, 25)) {
225 err = MFXJoinSession(device_hwctx->session, s->session);
226 if (err != MFX_ERR_NONE)
227 return AVERROR_UNKNOWN;
230 memset(&par, 0, sizeof(par));
232 s->deint_conf.Header.BufferId = MFX_EXTBUFF_VPP_DEINTERLACING;
233 s->deint_conf.Header.BufferSz = sizeof(s->deint_conf);
234 s->deint_conf.Mode = s->mode;
236 s->ext_buffers[s->num_ext_buffers++] = (mfxExtBuffer *)&s->deint_conf;
239 s->surface_ptrs = av_mallocz_array(hw_frames_hwctx->nb_surfaces,
240 sizeof(*s->surface_ptrs));
241 if (!s->surface_ptrs)
242 return AVERROR(ENOMEM);
243 for (i = 0; i < hw_frames_hwctx->nb_surfaces; i++)
244 s->surface_ptrs[i] = hw_frames_hwctx->surfaces + i;
245 s->nb_surface_ptrs = hw_frames_hwctx->nb_surfaces;
247 s->opaque_alloc.In.Surfaces = s->surface_ptrs;
248 s->opaque_alloc.In.NumSurface = s->nb_surface_ptrs;
249 s->opaque_alloc.In.Type = hw_frames_hwctx->frame_type;
251 s->opaque_alloc.Out = s->opaque_alloc.In;
253 s->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
254 s->opaque_alloc.Header.BufferSz = sizeof(s->opaque_alloc);
256 s->ext_buffers[s->num_ext_buffers++] = (mfxExtBuffer *)&s->opaque_alloc;
258 par.IOPattern = MFX_IOPATTERN_IN_OPAQUE_MEMORY | MFX_IOPATTERN_OUT_OPAQUE_MEMORY;
260 mfxFrameAllocator frame_allocator = {
262 .Alloc = frame_alloc,
264 .Unlock = frame_unlock,
265 .GetHDL = frame_get_hdl,
269 s->mem_ids = av_mallocz_array(hw_frames_hwctx->nb_surfaces,
270 sizeof(*s->mem_ids));
272 return AVERROR(ENOMEM);
273 for (i = 0; i < hw_frames_hwctx->nb_surfaces; i++)
274 s->mem_ids[i] = hw_frames_hwctx->surfaces[i].Data.MemId;
275 s->nb_mem_ids = hw_frames_hwctx->nb_surfaces;
277 err = MFXVideoCORE_SetFrameAllocator(s->session, &frame_allocator);
278 if (err != MFX_ERR_NONE)
279 return AVERROR_UNKNOWN;
281 par.IOPattern = MFX_IOPATTERN_IN_VIDEO_MEMORY | MFX_IOPATTERN_OUT_VIDEO_MEMORY;
284 par.ExtParam = s->ext_buffers;
285 par.NumExtParam = s->num_ext_buffers;
287 par.AsyncDepth = 1; // TODO async
289 par.vpp.In = hw_frames_hwctx->surfaces[0].Info;
291 par.vpp.In.CropW = ctx->inputs[0]->w;
292 par.vpp.In.CropH = ctx->inputs[0]->h;
294 if (ctx->inputs[0]->frame_rate.num) {
295 par.vpp.In.FrameRateExtN = ctx->inputs[0]->frame_rate.num;
296 par.vpp.In.FrameRateExtD = ctx->inputs[0]->frame_rate.den;
298 par.vpp.In.FrameRateExtN = ctx->inputs[0]->time_base.num;
299 par.vpp.In.FrameRateExtD = ctx->inputs[0]->time_base.den;
302 par.vpp.Out = par.vpp.In;
304 if (ctx->outputs[0]->frame_rate.num) {
305 par.vpp.Out.FrameRateExtN = ctx->outputs[0]->frame_rate.num;
306 par.vpp.Out.FrameRateExtD = ctx->outputs[0]->frame_rate.den;
308 par.vpp.Out.FrameRateExtN = ctx->outputs[0]->time_base.num;
309 par.vpp.Out.FrameRateExtD = ctx->outputs[0]->time_base.den;
312 err = MFXVideoVPP_Init(s->session, &par);
313 if (err != MFX_ERR_NONE) {
314 av_log(ctx, AV_LOG_ERROR, "Error opening the VPP for deinterlacing: %d\n", err);
315 return AVERROR_UNKNOWN;
321 static int qsvdeint_config_props(AVFilterLink *outlink)
323 AVFilterContext *ctx = outlink->src;
324 AVFilterLink *inlink = ctx->inputs[0];
325 QSVDeintContext *s = ctx->priv;
328 qsvdeint_uninit(ctx);
330 s->last_pts = AV_NOPTS_VALUE;
331 outlink->frame_rate = av_mul_q(inlink->frame_rate,
332 (AVRational){ 2, 1 });
333 outlink->time_base = av_mul_q(inlink->time_base,
334 (AVRational){ 1, 2 });
336 /* check that we have a hw context */
337 if (!inlink->hw_frames_ctx) {
338 av_log(ctx, AV_LOG_ERROR, "No hw context provided on input\n");
339 return AVERROR(EINVAL);
342 s->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
343 if (!s->hw_frames_ctx)
344 return AVERROR(ENOMEM);
346 av_buffer_unref(&outlink->hw_frames_ctx);
347 outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
348 if (!outlink->hw_frames_ctx) {
349 qsvdeint_uninit(ctx);
350 return AVERROR(ENOMEM);
353 ret = init_out_session(ctx);
361 static void clear_unused_frames(QSVDeintContext *s)
363 QSVFrame *cur = s->work_frames;
365 if (!cur->surface.Data.Locked) {
366 av_frame_free(&cur->frame);
373 static int get_free_frame(QSVDeintContext *s, QSVFrame **f)
375 QSVFrame *frame, **last;
377 clear_unused_frames(s);
379 frame = s->work_frames;
380 last = &s->work_frames;
391 frame = av_mallocz(sizeof(*frame));
393 return AVERROR(ENOMEM);
400 static int submit_frame(AVFilterContext *ctx, AVFrame *frame,
401 mfxFrameSurface1 **surface)
403 QSVDeintContext *s = ctx->priv;
407 ret = get_free_frame(s, &qf);
413 qf->surface = *(mfxFrameSurface1*)qf->frame->data[3];
415 qf->surface.Data.Locked = 0;
416 qf->surface.Info.CropW = qf->frame->width;
417 qf->surface.Info.CropH = qf->frame->height;
419 qf->surface.Info.PicStruct = !qf->frame->interlaced_frame ? MFX_PICSTRUCT_PROGRESSIVE :
420 (qf->frame->top_field_first ? MFX_PICSTRUCT_FIELD_TFF :
421 MFX_PICSTRUCT_FIELD_BFF);
422 if (qf->frame->repeat_pict == 1)
423 qf->surface.Info.PicStruct |= MFX_PICSTRUCT_FIELD_REPEATED;
424 else if (qf->frame->repeat_pict == 2)
425 qf->surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_DOUBLING;
426 else if (qf->frame->repeat_pict == 4)
427 qf->surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_TRIPLING;
429 if (ctx->inputs[0]->frame_rate.num) {
430 qf->surface.Info.FrameRateExtN = ctx->inputs[0]->frame_rate.num;
431 qf->surface.Info.FrameRateExtD = ctx->inputs[0]->frame_rate.den;
433 qf->surface.Info.FrameRateExtN = ctx->inputs[0]->time_base.num;
434 qf->surface.Info.FrameRateExtD = ctx->inputs[0]->time_base.den;
437 qf->surface.Data.TimeStamp = av_rescale_q(qf->frame->pts,
438 ctx->inputs[0]->time_base,
439 (AVRational){1, 90000});
441 *surface = &qf->surface;
447 static int process_frame(AVFilterContext *ctx, const AVFrame *in,
448 mfxFrameSurface1 *surf_in)
450 QSVDeintContext *s = ctx->priv;
451 AVFilterLink *inlink = ctx->inputs[0];
452 AVFilterLink *outlink = ctx->outputs[0];
455 mfxFrameSurface1 *surf_out;
456 mfxSyncPoint sync = NULL;
460 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
462 ret = AVERROR(ENOMEM);
466 surf_out = (mfxFrameSurface1*)out->data[3];
467 surf_out->Info.CropW = outlink->w;
468 surf_out->Info.CropH = outlink->h;
469 surf_out->Info.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
472 err = MFXVideoVPP_RunFrameVPPAsync(s->session, surf_in, surf_out,
474 if (err == MFX_WRN_DEVICE_BUSY)
476 } while (err == MFX_WRN_DEVICE_BUSY);
478 if (err == MFX_ERR_MORE_DATA) {
480 return QSVDEINT_MORE_INPUT;
483 if ((err < 0 && err != MFX_ERR_MORE_SURFACE) || !sync) {
484 av_log(ctx, AV_LOG_ERROR, "Error during deinterlacing: %d\n", err);
485 ret = AVERROR_UNKNOWN;
488 if (err == MFX_ERR_MORE_SURFACE)
492 err = MFXVideoCORE_SyncOperation(s->session, sync, 1000);
493 } while (err == MFX_WRN_IN_EXECUTION);
495 av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation: %d\n", err);
496 ret = AVERROR_UNKNOWN;
500 ret = av_frame_copy_props(out, in);
504 out->width = outlink->w;
505 out->height = outlink->h;
506 out->interlaced_frame = 0;
508 out->pts = av_rescale_q(out->pts, inlink->time_base, outlink->time_base);
509 if (out->pts == s->last_pts)
511 s->last_pts = out->pts;
513 ret = ff_filter_frame(outlink, out);
517 return again ? QSVDEINT_MORE_OUTPUT : 0;
523 static int qsvdeint_filter_frame(AVFilterLink *link, AVFrame *in)
525 AVFilterContext *ctx = link->dst;
527 mfxFrameSurface1 *surf_in;
530 ret = submit_frame(ctx, in, &surf_in);
537 ret = process_frame(ctx, in, surf_in);
540 } while (ret == QSVDEINT_MORE_OUTPUT);
545 static int qsvdeint_request_frame(AVFilterLink *outlink)
547 AVFilterContext *ctx = outlink->src;
549 return ff_request_frame(ctx->inputs[0]);
552 #define OFFSET(x) offsetof(QSVDeintContext, x)
553 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
554 static const AVOption options[] = {
555 { "mode", "set deinterlace mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = MFX_DEINTERLACING_ADVANCED}, MFX_DEINTERLACING_BOB, MFX_DEINTERLACING_ADVANCED, FLAGS, "mode"},
556 { "bob", "bob algorithm", 0, AV_OPT_TYPE_CONST, {.i64 = MFX_DEINTERLACING_BOB}, MFX_DEINTERLACING_BOB, MFX_DEINTERLACING_ADVANCED, FLAGS, "mode"},
557 { "advanced", "Motion adaptive algorithm", 0, AV_OPT_TYPE_CONST, {.i64 = MFX_DEINTERLACING_ADVANCED}, MFX_DEINTERLACING_BOB, MFX_DEINTERLACING_ADVANCED, FLAGS, "mode"},
561 static const AVClass qsvdeint_class = {
562 .class_name = "deinterlace_qsv",
563 .item_name = av_default_item_name,
565 .version = LIBAVUTIL_VERSION_INT,
568 static const AVFilterPad qsvdeint_inputs[] = {
571 .type = AVMEDIA_TYPE_VIDEO,
572 .filter_frame = qsvdeint_filter_frame,
577 static const AVFilterPad qsvdeint_outputs[] = {
580 .type = AVMEDIA_TYPE_VIDEO,
581 .config_props = qsvdeint_config_props,
582 .request_frame = qsvdeint_request_frame,
587 AVFilter ff_vf_deinterlace_qsv = {
588 .name = "deinterlace_qsv",
589 .description = NULL_IF_CONFIG_SMALL("QuickSync video deinterlacing"),
591 .uninit = qsvdeint_uninit,
592 .query_formats = qsvdeint_query_formats,
594 .priv_size = sizeof(QSVDeintContext),
595 .priv_class = &qsvdeint_class,
597 .inputs = qsvdeint_inputs,
598 .outputs = qsvdeint_outputs,
600 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,