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"
45 QSVDEINT_MORE_OUTPUT = 1,
49 typedef struct QSVFrame {
51 mfxFrameSurface1 surface;
54 struct QSVFrame *next;
57 typedef struct QSVDeintContext {
60 AVBufferRef *hw_frames_ctx;
61 /* a clone of the main session, used internally for deinterlacing */
67 mfxFrameSurface1 **surface_ptrs;
70 mfxExtOpaqueSurfaceAlloc opaque_alloc;
71 mfxExtBuffer *ext_buffers[1];
73 QSVFrame *work_frames;
80 static void qsvdeint_uninit(AVFilterContext *ctx)
82 QSVDeintContext *s = ctx->priv;
89 av_buffer_unref(&s->hw_frames_ctx);
93 s->work_frames = cur->next;
94 av_frame_free(&cur->frame);
99 av_freep(&s->mem_ids);
102 av_freep(&s->surface_ptrs);
103 s->nb_surface_ptrs = 0;
106 static int qsvdeint_query_formats(AVFilterContext *ctx)
108 static const enum AVPixelFormat pixel_formats[] = {
109 AV_PIX_FMT_QSV, AV_PIX_FMT_NONE,
111 AVFilterFormats *pix_fmts = ff_make_format_list(pixel_formats);
114 if ((ret = ff_set_common_formats(ctx, pix_fmts)) < 0)
120 static mfxStatus frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
121 mfxFrameAllocResponse *resp)
123 AVFilterContext *ctx = pthis;
124 QSVDeintContext *s = ctx->priv;
126 if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET) ||
127 !(req->Type & (MFX_MEMTYPE_FROM_VPPIN | MFX_MEMTYPE_FROM_VPPOUT)) ||
128 !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
129 return MFX_ERR_UNSUPPORTED;
131 resp->mids = s->mem_ids;
132 resp->NumFrameActual = s->nb_mem_ids;
137 static mfxStatus frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
142 static mfxStatus frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
144 return MFX_ERR_UNSUPPORTED;
147 static mfxStatus frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
149 return MFX_ERR_UNSUPPORTED;
152 static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
158 static const mfxHandleType handle_types[] = {
159 MFX_HANDLE_VA_DISPLAY,
160 MFX_HANDLE_D3D9_DEVICE_MANAGER,
161 MFX_HANDLE_D3D11_DEVICE,
164 static int init_out_session(AVFilterContext *ctx)
167 QSVDeintContext *s = ctx->priv;
168 AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)s->hw_frames_ctx->data;
169 AVQSVFramesContext *hw_frames_hwctx = hw_frames_ctx->hwctx;
170 AVQSVDeviceContext *device_hwctx = hw_frames_ctx->device_ctx->hwctx;
172 int opaque = !!(hw_frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
174 mfxHDL handle = NULL;
175 mfxHandleType handle_type;
182 /* extract the properties of the "master" session given to us */
183 err = MFXQueryIMPL(device_hwctx->session, &impl);
184 if (err == MFX_ERR_NONE)
185 err = MFXQueryVersion(device_hwctx->session, &ver);
186 if (err != MFX_ERR_NONE) {
187 av_log(ctx, AV_LOG_ERROR, "Error querying the session attributes\n");
188 return AVERROR_UNKNOWN;
191 for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
192 err = MFXVideoCORE_GetHandle(device_hwctx->session, handle_types[i], &handle);
193 if (err == MFX_ERR_NONE) {
194 handle_type = handle_types[i];
199 /* create a "slave" session with those same properties, to be used for
200 * actual deinterlacing */
201 err = MFXInit(impl, &ver, &s->session);
202 if (err != MFX_ERR_NONE) {
203 av_log(ctx, AV_LOG_ERROR, "Error initializing a session for deinterlacing\n");
204 return AVERROR_UNKNOWN;
208 err = MFXVideoCORE_SetHandle(s->session, handle_type, handle);
209 if (err != MFX_ERR_NONE)
210 return AVERROR_UNKNOWN;
213 memset(&par, 0, sizeof(par));
216 s->surface_ptrs = av_mallocz_array(hw_frames_hwctx->nb_surfaces,
217 sizeof(*s->surface_ptrs));
218 if (!s->surface_ptrs)
219 return AVERROR(ENOMEM);
220 for (i = 0; i < hw_frames_hwctx->nb_surfaces; i++)
221 s->surface_ptrs[i] = hw_frames_hwctx->surfaces + i;
222 s->nb_surface_ptrs = hw_frames_hwctx->nb_surfaces;
224 s->opaque_alloc.In.Surfaces = s->surface_ptrs;
225 s->opaque_alloc.In.NumSurface = s->nb_surface_ptrs;
226 s->opaque_alloc.In.Type = hw_frames_hwctx->frame_type;
228 s->opaque_alloc.Out = s->opaque_alloc.In;
230 s->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
231 s->opaque_alloc.Header.BufferSz = sizeof(s->opaque_alloc);
233 s->ext_buffers[0] = (mfxExtBuffer*)&s->opaque_alloc;
235 par.ExtParam = s->ext_buffers;
236 par.NumExtParam = FF_ARRAY_ELEMS(s->ext_buffers);
238 par.IOPattern = MFX_IOPATTERN_IN_OPAQUE_MEMORY | MFX_IOPATTERN_OUT_OPAQUE_MEMORY;
240 mfxFrameAllocator frame_allocator = {
242 .Alloc = frame_alloc,
244 .Unlock = frame_unlock,
245 .GetHDL = frame_get_hdl,
249 s->mem_ids = av_mallocz_array(hw_frames_hwctx->nb_surfaces,
250 sizeof(*s->mem_ids));
252 return AVERROR(ENOMEM);
253 for (i = 0; i < hw_frames_hwctx->nb_surfaces; i++)
254 s->mem_ids[i] = hw_frames_hwctx->surfaces[i].Data.MemId;
255 s->nb_mem_ids = hw_frames_hwctx->nb_surfaces;
257 err = MFXVideoCORE_SetFrameAllocator(s->session, &frame_allocator);
258 if (err != MFX_ERR_NONE)
259 return AVERROR_UNKNOWN;
261 par.IOPattern = MFX_IOPATTERN_IN_VIDEO_MEMORY | MFX_IOPATTERN_OUT_VIDEO_MEMORY;
264 par.AsyncDepth = 1; // TODO async
266 par.vpp.In = hw_frames_hwctx->surfaces[0].Info;
268 par.vpp.In.CropW = ctx->inputs[0]->w;
269 par.vpp.In.CropH = ctx->inputs[0]->h;
271 if (ctx->inputs[0]->frame_rate.num) {
272 par.vpp.In.FrameRateExtN = ctx->inputs[0]->frame_rate.num;
273 par.vpp.In.FrameRateExtD = ctx->inputs[0]->frame_rate.den;
275 par.vpp.In.FrameRateExtN = ctx->inputs[0]->time_base.num;
276 par.vpp.In.FrameRateExtD = ctx->inputs[0]->time_base.den;
279 par.vpp.Out = par.vpp.In;
281 if (ctx->outputs[0]->frame_rate.num) {
282 par.vpp.Out.FrameRateExtN = ctx->outputs[0]->frame_rate.num;
283 par.vpp.Out.FrameRateExtD = ctx->outputs[0]->frame_rate.den;
285 par.vpp.Out.FrameRateExtN = ctx->outputs[0]->time_base.num;
286 par.vpp.Out.FrameRateExtD = ctx->outputs[0]->time_base.den;
289 err = MFXVideoVPP_Init(s->session, &par);
290 if (err != MFX_ERR_NONE) {
291 av_log(ctx, AV_LOG_ERROR, "Error opening the VPP for deinterlacing: %d\n", err);
292 return AVERROR_UNKNOWN;
298 static int qsvdeint_config_props(AVFilterLink *outlink)
300 AVFilterContext *ctx = outlink->src;
301 AVFilterLink *inlink = ctx->inputs[0];
302 QSVDeintContext *s = ctx->priv;
305 qsvdeint_uninit(ctx);
307 s->last_pts = AV_NOPTS_VALUE;
308 outlink->frame_rate = av_mul_q(inlink->frame_rate,
309 (AVRational){ 2, 1 });
310 outlink->time_base = av_mul_q(inlink->time_base,
311 (AVRational){ 1, 2 });
313 /* check that we have a hw context */
314 if (!inlink->hw_frames_ctx) {
315 av_log(ctx, AV_LOG_ERROR, "No hw context provided on input\n");
316 return AVERROR(EINVAL);
319 s->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
320 if (!s->hw_frames_ctx)
321 return AVERROR(ENOMEM);
323 av_buffer_unref(&outlink->hw_frames_ctx);
324 outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
325 if (!outlink->hw_frames_ctx) {
326 qsvdeint_uninit(ctx);
327 return AVERROR(ENOMEM);
330 ret = init_out_session(ctx);
338 static void clear_unused_frames(QSVDeintContext *s)
340 QSVFrame *cur = s->work_frames;
342 if (!cur->surface.Data.Locked) {
343 av_frame_free(&cur->frame);
350 static int get_free_frame(QSVDeintContext *s, QSVFrame **f)
352 QSVFrame *frame, **last;
354 clear_unused_frames(s);
356 frame = s->work_frames;
357 last = &s->work_frames;
368 frame = av_mallocz(sizeof(*frame));
370 return AVERROR(ENOMEM);
377 static int submit_frame(AVFilterContext *ctx, AVFrame *frame,
378 mfxFrameSurface1 **surface)
380 QSVDeintContext *s = ctx->priv;
384 ret = get_free_frame(s, &qf);
390 qf->surface = *(mfxFrameSurface1*)qf->frame->data[3];
392 qf->surface.Data.Locked = 0;
393 qf->surface.Info.CropW = qf->frame->width;
394 qf->surface.Info.CropH = qf->frame->height;
396 qf->surface.Info.PicStruct = !qf->frame->interlaced_frame ? MFX_PICSTRUCT_PROGRESSIVE :
397 (qf->frame->top_field_first ? MFX_PICSTRUCT_FIELD_TFF :
398 MFX_PICSTRUCT_FIELD_BFF);
399 if (qf->frame->repeat_pict == 1)
400 qf->surface.Info.PicStruct |= MFX_PICSTRUCT_FIELD_REPEATED;
401 else if (qf->frame->repeat_pict == 2)
402 qf->surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_DOUBLING;
403 else if (qf->frame->repeat_pict == 4)
404 qf->surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_TRIPLING;
406 if (ctx->inputs[0]->frame_rate.num) {
407 qf->surface.Info.FrameRateExtN = ctx->inputs[0]->frame_rate.num;
408 qf->surface.Info.FrameRateExtD = ctx->inputs[0]->frame_rate.den;
410 qf->surface.Info.FrameRateExtN = ctx->inputs[0]->time_base.num;
411 qf->surface.Info.FrameRateExtD = ctx->inputs[0]->time_base.den;
414 qf->surface.Data.TimeStamp = av_rescale_q(qf->frame->pts,
415 ctx->inputs[0]->time_base,
416 (AVRational){1, 90000});
418 *surface = &qf->surface;
424 static int process_frame(AVFilterContext *ctx, const AVFrame *in,
425 mfxFrameSurface1 *surf_in)
427 QSVDeintContext *s = ctx->priv;
428 AVFilterLink *inlink = ctx->inputs[0];
429 AVFilterLink *outlink = ctx->outputs[0];
432 mfxFrameSurface1 *surf_out;
433 mfxSyncPoint sync = NULL;
437 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
439 ret = AVERROR(ENOMEM);
443 surf_out = (mfxFrameSurface1*)out->data[3];
444 surf_out->Info.CropW = outlink->w;
445 surf_out->Info.CropH = outlink->h;
446 surf_out->Info.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
449 err = MFXVideoVPP_RunFrameVPPAsync(s->session, surf_in, surf_out,
451 if (err == MFX_WRN_DEVICE_BUSY)
453 } while (err == MFX_WRN_DEVICE_BUSY);
455 if (err == MFX_ERR_MORE_DATA) {
457 return QSVDEINT_MORE_INPUT;
460 if ((err < 0 && err != MFX_ERR_MORE_SURFACE) || !sync) {
461 av_log(ctx, AV_LOG_ERROR, "Error during deinterlacing: %d\n", err);
462 ret = AVERROR_UNKNOWN;
465 if (err == MFX_ERR_MORE_SURFACE)
469 err = MFXVideoCORE_SyncOperation(s->session, sync, 1000);
470 } while (err == MFX_WRN_IN_EXECUTION);
472 av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation: %d\n", err);
473 ret = AVERROR_UNKNOWN;
477 ret = av_frame_copy_props(out, in);
481 out->width = outlink->w;
482 out->height = outlink->h;
483 out->interlaced_frame = 0;
485 out->pts = av_rescale_q(out->pts, inlink->time_base, outlink->time_base);
486 if (out->pts == s->last_pts)
488 s->last_pts = out->pts;
490 ret = ff_filter_frame(outlink, out);
494 return again ? QSVDEINT_MORE_OUTPUT : 0;
500 static int qsvdeint_filter_frame(AVFilterLink *link, AVFrame *in)
502 AVFilterContext *ctx = link->dst;
504 mfxFrameSurface1 *surf_in;
507 ret = submit_frame(ctx, in, &surf_in);
514 ret = process_frame(ctx, in, surf_in);
517 } while (ret == QSVDEINT_MORE_OUTPUT);
522 static int qsvdeint_request_frame(AVFilterLink *outlink)
524 AVFilterContext *ctx = outlink->src;
526 return ff_request_frame(ctx->inputs[0]);
529 #define OFFSET(x) offsetof(QSVDeintContext, x)
530 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
531 static const AVOption options[] = {
535 static const AVClass qsvdeint_class = {
536 .class_name = "deinterlace_qsv",
537 .item_name = av_default_item_name,
539 .version = LIBAVUTIL_VERSION_INT,
542 static const AVFilterPad qsvdeint_inputs[] = {
545 .type = AVMEDIA_TYPE_VIDEO,
546 .filter_frame = qsvdeint_filter_frame,
551 static const AVFilterPad qsvdeint_outputs[] = {
554 .type = AVMEDIA_TYPE_VIDEO,
555 .config_props = qsvdeint_config_props,
556 .request_frame = qsvdeint_request_frame,
561 AVFilter ff_vf_deinterlace_qsv = {
562 .name = "deinterlace_qsv",
563 .description = NULL_IF_CONFIG_SMALL("QuickSync video deinterlacing"),
565 .uninit = qsvdeint_uninit,
566 .query_formats = qsvdeint_query_formats,
568 .priv_size = sizeof(QSVDeintContext),
569 .priv_class = &qsvdeint_class,
571 .inputs = qsvdeint_inputs,
572 .outputs = qsvdeint_outputs,
574 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,