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 * Intel Quick Sync Video VPP base function
24 #include "libavutil/common.h"
25 #include "libavutil/mathematics.h"
26 #include "libavutil/hwcontext.h"
27 #include "libavutil/hwcontext_qsv.h"
28 #include "libavutil/time.h"
29 #include "libavutil/pixdesc.h"
35 #define IS_VIDEO_MEMORY(mode) (mode & (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET | \
36 MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET))
37 #define IS_OPAQUE_MEMORY(mode) (mode & MFX_MEMTYPE_OPAQUE_FRAME)
38 #define IS_SYSTEM_MEMORY(mode) (mode & MFX_MEMTYPE_SYSTEM_MEMORY)
40 static const mfxHandleType handle_types[] = {
41 MFX_HANDLE_VA_DISPLAY,
42 MFX_HANDLE_D3D9_DEVICE_MANAGER,
43 MFX_HANDLE_D3D11_DEVICE,
46 static const AVRational default_tb = { 1, 90000 };
51 } qsv_iopatterns[] = {
52 {MFX_IOPATTERN_IN_VIDEO_MEMORY, "input is video memory surface" },
53 {MFX_IOPATTERN_IN_SYSTEM_MEMORY, "input is system memory surface" },
54 {MFX_IOPATTERN_IN_OPAQUE_MEMORY, "input is opaque memory surface" },
55 {MFX_IOPATTERN_OUT_VIDEO_MEMORY, "output is video memory surface" },
56 {MFX_IOPATTERN_OUT_SYSTEM_MEMORY, "output is system memory surface" },
57 {MFX_IOPATTERN_OUT_OPAQUE_MEMORY, "output is opaque memory surface" },
60 int ff_qsvvpp_print_iopattern(void *log_ctx, int mfx_iopattern,
61 const char *extra_string)
63 const char *desc = NULL;
65 for (int i = 0; i < FF_ARRAY_ELEMS(qsv_iopatterns); i++) {
66 if (qsv_iopatterns[i].mfx_iopattern == mfx_iopattern) {
67 desc = qsv_iopatterns[i].desc;
71 desc = "unknown iopattern";
73 av_log(log_ctx, AV_LOG_VERBOSE, "%s: %s\n", extra_string, desc);
82 { MFX_ERR_NONE, 0, "success" },
83 { MFX_ERR_UNKNOWN, AVERROR_UNKNOWN, "unknown error" },
84 { MFX_ERR_NULL_PTR, AVERROR(EINVAL), "NULL pointer" },
85 { MFX_ERR_UNSUPPORTED, AVERROR(ENOSYS), "unsupported" },
86 { MFX_ERR_MEMORY_ALLOC, AVERROR(ENOMEM), "failed to allocate memory" },
87 { MFX_ERR_NOT_ENOUGH_BUFFER, AVERROR(ENOMEM), "insufficient input/output buffer" },
88 { MFX_ERR_INVALID_HANDLE, AVERROR(EINVAL), "invalid handle" },
89 { MFX_ERR_LOCK_MEMORY, AVERROR(EIO), "failed to lock the memory block" },
90 { MFX_ERR_NOT_INITIALIZED, AVERROR_BUG, "not initialized" },
91 { MFX_ERR_NOT_FOUND, AVERROR(ENOSYS), "specified object was not found" },
92 /* the following 3 errors should always be handled explicitly, so those "mappings"
93 * are for completeness only */
94 { MFX_ERR_MORE_DATA, AVERROR_UNKNOWN, "expect more data at input" },
95 { MFX_ERR_MORE_SURFACE, AVERROR_UNKNOWN, "expect more surface at output" },
96 { MFX_ERR_MORE_BITSTREAM, AVERROR_UNKNOWN, "expect more bitstream at output" },
97 { MFX_ERR_ABORTED, AVERROR_UNKNOWN, "operation aborted" },
98 { MFX_ERR_DEVICE_LOST, AVERROR(EIO), "device lost" },
99 { MFX_ERR_INCOMPATIBLE_VIDEO_PARAM, AVERROR(EINVAL), "incompatible video parameters" },
100 { MFX_ERR_INVALID_VIDEO_PARAM, AVERROR(EINVAL), "invalid video parameters" },
101 { MFX_ERR_UNDEFINED_BEHAVIOR, AVERROR_BUG, "undefined behavior" },
102 { MFX_ERR_DEVICE_FAILED, AVERROR(EIO), "device failed" },
103 { MFX_ERR_INCOMPATIBLE_AUDIO_PARAM, AVERROR(EINVAL), "incompatible audio parameters" },
104 { MFX_ERR_INVALID_AUDIO_PARAM, AVERROR(EINVAL), "invalid audio parameters" },
106 { MFX_WRN_IN_EXECUTION, 0, "operation in execution" },
107 { MFX_WRN_DEVICE_BUSY, 0, "device busy" },
108 { MFX_WRN_VIDEO_PARAM_CHANGED, 0, "video parameters changed" },
109 { MFX_WRN_PARTIAL_ACCELERATION, 0, "partial acceleration" },
110 { MFX_WRN_INCOMPATIBLE_VIDEO_PARAM, 0, "incompatible video parameters" },
111 { MFX_WRN_VALUE_NOT_CHANGED, 0, "value is saturated" },
112 { MFX_WRN_OUT_OF_RANGE, 0, "value out of range" },
113 { MFX_WRN_FILTER_SKIPPED, 0, "filter skipped" },
114 { MFX_WRN_INCOMPATIBLE_AUDIO_PARAM, 0, "incompatible audio parameters" },
117 static int qsv_map_error(mfxStatus mfx_err, const char **desc)
120 for (i = 0; i < FF_ARRAY_ELEMS(qsv_errors); i++) {
121 if (qsv_errors[i].mfxerr == mfx_err) {
123 *desc = qsv_errors[i].desc;
124 return qsv_errors[i].averr;
128 *desc = "unknown error";
129 return AVERROR_UNKNOWN;
132 int ff_qsvvpp_print_error(void *log_ctx, mfxStatus err,
133 const char *error_string)
137 ret = qsv_map_error(err, &desc);
138 av_log(log_ctx, AV_LOG_ERROR, "%s: %s (%d)\n", error_string, desc, err);
142 int ff_qsvvpp_print_warning(void *log_ctx, mfxStatus err,
143 const char *warning_string)
147 ret = qsv_map_error(err, &desc);
148 av_log(log_ctx, AV_LOG_WARNING, "%s: %s (%d)\n", warning_string, desc, err);
152 /* functions for frameAlloc */
153 static mfxStatus frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
154 mfxFrameAllocResponse *resp)
156 QSVVPPContext *s = pthis;
159 if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET) ||
160 !(req->Type & (MFX_MEMTYPE_FROM_VPPIN | MFX_MEMTYPE_FROM_VPPOUT)) ||
161 !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
162 return MFX_ERR_UNSUPPORTED;
164 if (req->Type & MFX_MEMTYPE_FROM_VPPIN) {
165 resp->mids = av_mallocz(s->nb_surface_ptrs_in * sizeof(*resp->mids));
167 return AVERROR(ENOMEM);
169 for (i = 0; i < s->nb_surface_ptrs_in; i++)
170 resp->mids[i] = s->surface_ptrs_in[i]->Data.MemId;
172 resp->NumFrameActual = s->nb_surface_ptrs_in;
174 resp->mids = av_mallocz(s->nb_surface_ptrs_out * sizeof(*resp->mids));
176 return AVERROR(ENOMEM);
178 for (i = 0; i < s->nb_surface_ptrs_out; i++)
179 resp->mids[i] = s->surface_ptrs_out[i]->Data.MemId;
181 resp->NumFrameActual = s->nb_surface_ptrs_out;
187 static mfxStatus frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
189 av_freep(&resp->mids);
193 static mfxStatus frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
195 return MFX_ERR_UNSUPPORTED;
198 static mfxStatus frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
200 return MFX_ERR_UNSUPPORTED;
203 static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
209 static int pix_fmt_to_mfx_fourcc(int format)
212 case AV_PIX_FMT_YUV420P:
213 return MFX_FOURCC_YV12;
214 case AV_PIX_FMT_NV12:
215 return MFX_FOURCC_NV12;
216 case AV_PIX_FMT_YUYV422:
217 return MFX_FOURCC_YUY2;
218 case AV_PIX_FMT_BGRA:
219 return MFX_FOURCC_RGB4;
222 return MFX_FOURCC_NV12;
225 static int map_frame_to_surface(AVFrame *frame, mfxFrameSurface1 *surface)
227 switch (frame->format) {
228 case AV_PIX_FMT_NV12:
229 case AV_PIX_FMT_P010:
230 surface->Data.Y = frame->data[0];
231 surface->Data.UV = frame->data[1];
233 case AV_PIX_FMT_YUV420P:
234 surface->Data.Y = frame->data[0];
235 surface->Data.U = frame->data[1];
236 surface->Data.V = frame->data[2];
238 case AV_PIX_FMT_YUYV422:
239 surface->Data.Y = frame->data[0];
240 surface->Data.U = frame->data[0] + 1;
241 surface->Data.V = frame->data[0] + 3;
243 case AV_PIX_FMT_RGB32:
244 surface->Data.B = frame->data[0];
245 surface->Data.G = frame->data[0] + 1;
246 surface->Data.R = frame->data[0] + 2;
247 surface->Data.A = frame->data[0] + 3;
250 return MFX_ERR_UNSUPPORTED;
252 surface->Data.Pitch = frame->linesize[0];
257 /* fill the surface info */
258 static int fill_frameinfo_by_link(mfxFrameInfo *frameinfo, AVFilterLink *link)
260 enum AVPixelFormat pix_fmt;
261 AVHWFramesContext *frames_ctx;
262 AVQSVFramesContext *frames_hwctx;
263 const AVPixFmtDescriptor *desc;
265 if (link->format == AV_PIX_FMT_QSV) {
266 if (!link->hw_frames_ctx)
267 return AVERROR(EINVAL);
269 frames_ctx = (AVHWFramesContext *)link->hw_frames_ctx->data;
270 frames_hwctx = frames_ctx->hwctx;
271 *frameinfo = frames_hwctx->surfaces[0].Info;
273 pix_fmt = link->format;
274 desc = av_pix_fmt_desc_get(pix_fmt);
278 frameinfo->CropX = 0;
279 frameinfo->CropY = 0;
280 frameinfo->Width = FFALIGN(link->w, 32);
281 frameinfo->Height = FFALIGN(link->h, 32);
282 frameinfo->PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
283 frameinfo->FourCC = pix_fmt_to_mfx_fourcc(pix_fmt);
284 frameinfo->BitDepthLuma = desc->comp[0].depth;
285 frameinfo->BitDepthChroma = desc->comp[0].depth;
286 frameinfo->Shift = desc->comp[0].depth > 8;
287 if (desc->log2_chroma_w && desc->log2_chroma_h)
288 frameinfo->ChromaFormat = MFX_CHROMAFORMAT_YUV420;
289 else if (desc->log2_chroma_w)
290 frameinfo->ChromaFormat = MFX_CHROMAFORMAT_YUV422;
292 frameinfo->ChromaFormat = MFX_CHROMAFORMAT_YUV444;
295 frameinfo->CropW = link->w;
296 frameinfo->CropH = link->h;
297 frameinfo->FrameRateExtN = link->frame_rate.num;
298 frameinfo->FrameRateExtD = link->frame_rate.den;
299 frameinfo->AspectRatioW = link->sample_aspect_ratio.num ? link->sample_aspect_ratio.num : 1;
300 frameinfo->AspectRatioH = link->sample_aspect_ratio.den ? link->sample_aspect_ratio.den : 1;
305 static void clear_unused_frames(QSVFrame *list)
308 /* list->queued==1 means the frame is not cached in VPP
309 * process any more, it can be released to pool. */
310 if ((list->queued == 1) && !list->surface.Data.Locked) {
311 av_frame_free(&list->frame);
318 static void clear_frame_list(QSVFrame **list)
324 *list = (*list)->next;
325 av_frame_free(&frame->frame);
330 static QSVFrame *get_free_frame(QSVFrame **list)
332 QSVFrame *out = *list;
334 for (; out; out = out->next) {
342 out = av_mallocz(sizeof(*out));
344 av_log(NULL, AV_LOG_ERROR, "Can't alloc new output frame.\n");
355 /* get the input surface */
356 static QSVFrame *submit_frame(QSVVPPContext *s, AVFilterLink *inlink, AVFrame *picref)
359 AVFilterContext *ctx = inlink->dst;
361 clear_unused_frames(s->in_frame_list);
363 qsv_frame = get_free_frame(&s->in_frame_list);
367 /* Turn AVFrame into mfxFrameSurface1.
368 * For video/opaque memory mode, pix_fmt is AV_PIX_FMT_QSV, and
369 * mfxFrameSurface1 is stored in AVFrame->data[3];
370 * for system memory mode, raw video data is stored in
371 * AVFrame, we should map it into mfxFrameSurface1.
373 if (!IS_SYSTEM_MEMORY(s->in_mem_mode)) {
374 if (picref->format != AV_PIX_FMT_QSV) {
375 av_log(ctx, AV_LOG_ERROR, "QSVVPP gets a wrong frame.\n");
378 qsv_frame->frame = av_frame_clone(picref);
379 qsv_frame->surface = *(mfxFrameSurface1 *)qsv_frame->frame->data[3];
381 /* make a copy if the input is not padded as libmfx requires */
382 if (picref->height & 31 || picref->linesize[0] & 31) {
383 qsv_frame->frame = ff_get_video_buffer(inlink,
384 FFALIGN(inlink->w, 32),
385 FFALIGN(inlink->h, 32));
386 if (!qsv_frame->frame)
389 qsv_frame->frame->width = picref->width;
390 qsv_frame->frame->height = picref->height;
392 if (av_frame_copy(qsv_frame->frame, picref) < 0) {
393 av_frame_free(&qsv_frame->frame);
397 av_frame_copy_props(qsv_frame->frame, picref);
399 qsv_frame->frame = av_frame_clone(picref);
401 if (map_frame_to_surface(qsv_frame->frame,
402 &qsv_frame->surface) < 0) {
403 av_log(ctx, AV_LOG_ERROR, "Unsupported frame.\n");
408 qsv_frame->surface.Info = s->frame_infos[FF_INLINK_IDX(inlink)];
409 qsv_frame->surface.Data.TimeStamp = av_rescale_q(qsv_frame->frame->pts,
410 inlink->time_base, default_tb);
412 qsv_frame->surface.Info.PicStruct =
413 !qsv_frame->frame->interlaced_frame ? MFX_PICSTRUCT_PROGRESSIVE :
414 (qsv_frame->frame->top_field_first ? MFX_PICSTRUCT_FIELD_TFF :
415 MFX_PICSTRUCT_FIELD_BFF);
416 if (qsv_frame->frame->repeat_pict == 1)
417 qsv_frame->surface.Info.PicStruct |= MFX_PICSTRUCT_FIELD_REPEATED;
418 else if (qsv_frame->frame->repeat_pict == 2)
419 qsv_frame->surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_DOUBLING;
420 else if (qsv_frame->frame->repeat_pict == 4)
421 qsv_frame->surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_TRIPLING;
426 /* get the output surface */
427 static QSVFrame *query_frame(QSVVPPContext *s, AVFilterLink *outlink)
429 AVFilterContext *ctx = outlink->src;
433 clear_unused_frames(s->out_frame_list);
435 out_frame = get_free_frame(&s->out_frame_list);
439 /* For video memory, get a hw frame;
440 * For system memory, get a sw frame and map it into a mfx_surface. */
441 if (!IS_SYSTEM_MEMORY(s->out_mem_mode)) {
442 out_frame->frame = av_frame_alloc();
443 if (!out_frame->frame)
446 ret = av_hwframe_get_buffer(outlink->hw_frames_ctx, out_frame->frame, 0);
448 av_log(ctx, AV_LOG_ERROR, "Can't allocate a surface.\n");
452 out_frame->surface = *(mfxFrameSurface1 *)out_frame->frame->data[3];
454 /* Get a frame with aligned dimensions.
455 * Libmfx need system memory being 128x64 aligned */
456 out_frame->frame = ff_get_video_buffer(outlink,
457 FFALIGN(outlink->w, 128),
458 FFALIGN(outlink->h, 64));
459 if (!out_frame->frame)
462 out_frame->frame->width = outlink->w;
463 out_frame->frame->height = outlink->h;
465 ret = map_frame_to_surface(out_frame->frame,
466 &out_frame->surface);
471 out_frame->surface.Info = s->vpp_param.vpp.Out;
476 /* create the QSV session */
477 static int init_vpp_session(AVFilterContext *avctx, QSVVPPContext *s)
479 AVFilterLink *inlink = avctx->inputs[0];
480 AVFilterLink *outlink = avctx->outputs[0];
481 AVQSVFramesContext *in_frames_hwctx = NULL;
482 AVQSVFramesContext *out_frames_hwctx = NULL;
484 AVBufferRef *device_ref;
485 AVHWDeviceContext *device_ctx;
486 AVQSVDeviceContext *device_hwctx;
488 mfxHandleType handle_type;
493 if (inlink->hw_frames_ctx) {
494 AVHWFramesContext *frames_ctx = (AVHWFramesContext *)inlink->hw_frames_ctx->data;
496 device_ref = frames_ctx->device_ref;
497 in_frames_hwctx = frames_ctx->hwctx;
499 s->in_mem_mode = in_frames_hwctx->frame_type;
501 s->surface_ptrs_in = av_mallocz_array(in_frames_hwctx->nb_surfaces,
502 sizeof(*s->surface_ptrs_in));
503 if (!s->surface_ptrs_in)
504 return AVERROR(ENOMEM);
506 for (i = 0; i < in_frames_hwctx->nb_surfaces; i++)
507 s->surface_ptrs_in[i] = in_frames_hwctx->surfaces + i;
509 s->nb_surface_ptrs_in = in_frames_hwctx->nb_surfaces;
510 } else if (avctx->hw_device_ctx) {
511 device_ref = avctx->hw_device_ctx;
512 s->in_mem_mode = MFX_MEMTYPE_SYSTEM_MEMORY;
514 av_log(avctx, AV_LOG_ERROR, "No hw context provided.\n");
515 return AVERROR(EINVAL);
518 device_ctx = (AVHWDeviceContext *)device_ref->data;
519 device_hwctx = device_ctx->hwctx;
521 if (outlink->format == AV_PIX_FMT_QSV) {
522 AVHWFramesContext *out_frames_ctx;
523 AVBufferRef *out_frames_ref = av_hwframe_ctx_alloc(device_ref);
525 return AVERROR(ENOMEM);
527 s->out_mem_mode = IS_OPAQUE_MEMORY(s->in_mem_mode) ?
528 MFX_MEMTYPE_OPAQUE_FRAME :
529 MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
531 out_frames_ctx = (AVHWFramesContext *)out_frames_ref->data;
532 out_frames_hwctx = out_frames_ctx->hwctx;
534 out_frames_ctx->format = AV_PIX_FMT_QSV;
535 out_frames_ctx->width = FFALIGN(outlink->w, 32);
536 out_frames_ctx->height = FFALIGN(outlink->h, 32);
537 out_frames_ctx->sw_format = s->out_sw_format;
538 out_frames_ctx->initial_pool_size = 64;
539 if (avctx->extra_hw_frames > 0)
540 out_frames_ctx->initial_pool_size += avctx->extra_hw_frames;
541 out_frames_hwctx->frame_type = s->out_mem_mode;
543 ret = av_hwframe_ctx_init(out_frames_ref);
545 av_buffer_unref(&out_frames_ref);
546 av_log(avctx, AV_LOG_ERROR, "Error creating frames_ctx for output pad.\n");
550 s->surface_ptrs_out = av_mallocz_array(out_frames_hwctx->nb_surfaces,
551 sizeof(*s->surface_ptrs_out));
552 if (!s->surface_ptrs_out) {
553 av_buffer_unref(&out_frames_ref);
554 return AVERROR(ENOMEM);
557 for (i = 0; i < out_frames_hwctx->nb_surfaces; i++)
558 s->surface_ptrs_out[i] = out_frames_hwctx->surfaces + i;
559 s->nb_surface_ptrs_out = out_frames_hwctx->nb_surfaces;
561 av_buffer_unref(&outlink->hw_frames_ctx);
562 outlink->hw_frames_ctx = out_frames_ref;
564 s->out_mem_mode = MFX_MEMTYPE_SYSTEM_MEMORY;
566 /* extract the properties of the "master" session given to us */
567 ret = MFXQueryIMPL(device_hwctx->session, &impl);
568 if (ret == MFX_ERR_NONE)
569 ret = MFXQueryVersion(device_hwctx->session, &ver);
570 if (ret != MFX_ERR_NONE) {
571 av_log(avctx, AV_LOG_ERROR, "Error querying the session attributes\n");
572 return AVERROR_UNKNOWN;
575 for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
576 ret = MFXVideoCORE_GetHandle(device_hwctx->session, handle_types[i], &handle);
577 if (ret == MFX_ERR_NONE) {
578 handle_type = handle_types[i];
584 return ff_qsvvpp_print_error(avctx, ret, "Error getting the session handle");
586 ff_qsvvpp_print_warning(avctx, ret, "Warning in getting the session handle");
587 return AVERROR_UNKNOWN;
590 /* create a "slave" session with those same properties, to be used for vpp */
591 ret = MFXInit(impl, &ver, &s->session);
593 return ff_qsvvpp_print_error(avctx, ret, "Error initializing a session");
595 ff_qsvvpp_print_warning(avctx, ret, "Warning in session initialization");
596 return AVERROR_UNKNOWN;
600 ret = MFXVideoCORE_SetHandle(s->session, handle_type, handle);
601 if (ret != MFX_ERR_NONE)
602 return AVERROR_UNKNOWN;
605 if (QSV_RUNTIME_VERSION_ATLEAST(ver, 1, 25)) {
606 ret = MFXJoinSession(device_hwctx->session, s->session);
607 if (ret != MFX_ERR_NONE)
608 return AVERROR_UNKNOWN;
611 if (IS_OPAQUE_MEMORY(s->in_mem_mode) || IS_OPAQUE_MEMORY(s->out_mem_mode)) {
612 s->opaque_alloc.In.Surfaces = s->surface_ptrs_in;
613 s->opaque_alloc.In.NumSurface = s->nb_surface_ptrs_in;
614 s->opaque_alloc.In.Type = s->in_mem_mode;
616 s->opaque_alloc.Out.Surfaces = s->surface_ptrs_out;
617 s->opaque_alloc.Out.NumSurface = s->nb_surface_ptrs_out;
618 s->opaque_alloc.Out.Type = s->out_mem_mode;
620 s->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
621 s->opaque_alloc.Header.BufferSz = sizeof(s->opaque_alloc);
622 } else if (IS_VIDEO_MEMORY(s->in_mem_mode) || IS_VIDEO_MEMORY(s->out_mem_mode)) {
623 mfxFrameAllocator frame_allocator = {
625 .Alloc = frame_alloc,
627 .Unlock = frame_unlock,
628 .GetHDL = frame_get_hdl,
632 ret = MFXVideoCORE_SetFrameAllocator(s->session, &frame_allocator);
633 if (ret != MFX_ERR_NONE)
634 return AVERROR_UNKNOWN;
640 static unsigned int qsv_fifo_item_size(void)
642 return sizeof(mfxSyncPoint) + sizeof(QSVFrame*);
645 static unsigned int qsv_fifo_size(const AVFifoBuffer* fifo)
647 return av_fifo_size(fifo)/qsv_fifo_item_size();
650 int ff_qsvvpp_create(AVFilterContext *avctx, QSVVPPContext **vpp, QSVVPPParam *param)
656 s = av_mallocz(sizeof(*s));
658 return AVERROR(ENOMEM);
660 s->filter_frame = param->filter_frame;
661 if (!s->filter_frame)
662 s->filter_frame = ff_filter_frame;
663 s->out_sw_format = param->out_sw_format;
665 /* create the vpp session */
666 ret = init_vpp_session(avctx, s);
670 s->frame_infos = av_mallocz_array(avctx->nb_inputs, sizeof(*s->frame_infos));
671 if (!s->frame_infos) {
672 ret = AVERROR(ENOMEM);
676 /* Init each input's information */
677 for (i = 0; i < avctx->nb_inputs; i++) {
678 ret = fill_frameinfo_by_link(&s->frame_infos[i], avctx->inputs[i]);
683 /* Update input's frame info according to crop */
684 for (i = 0; i < param->num_crop; i++) {
685 QSVVPPCrop *crop = param->crop + i;
686 if (crop->in_idx > avctx->nb_inputs) {
687 ret = AVERROR(EINVAL);
690 s->frame_infos[crop->in_idx].CropX = crop->x;
691 s->frame_infos[crop->in_idx].CropY = crop->y;
692 s->frame_infos[crop->in_idx].CropW = crop->w;
693 s->frame_infos[crop->in_idx].CropH = crop->h;
696 s->vpp_param.vpp.In = s->frame_infos[0];
698 ret = fill_frameinfo_by_link(&s->vpp_param.vpp.Out, avctx->outputs[0]);
700 av_log(avctx, AV_LOG_ERROR, "Fail to get frame info from link.\n");
704 if (IS_OPAQUE_MEMORY(s->in_mem_mode) || IS_OPAQUE_MEMORY(s->out_mem_mode)) {
705 s->nb_ext_buffers = param->num_ext_buf + 1;
706 s->ext_buffers = av_mallocz_array(s->nb_ext_buffers, sizeof(*s->ext_buffers));
707 if (!s->ext_buffers) {
708 ret = AVERROR(ENOMEM);
712 s->ext_buffers[0] = (mfxExtBuffer *)&s->opaque_alloc;
713 for (i = 1; i < param->num_ext_buf; i++)
714 s->ext_buffers[i] = param->ext_buf[i - 1];
715 s->vpp_param.ExtParam = s->ext_buffers;
716 s->vpp_param.NumExtParam = s->nb_ext_buffers;
718 s->vpp_param.NumExtParam = param->num_ext_buf;
719 s->vpp_param.ExtParam = param->ext_buf;
724 /** keep fifo size at least 1. Even when async_depth is 0, fifo is used. */
725 s->async_fifo = av_fifo_alloc((param->async_depth + 1) * qsv_fifo_item_size());
726 s->async_depth = param->async_depth;
727 if (!s->async_fifo) {
728 ret = AVERROR(ENOMEM);
732 s->vpp_param.AsyncDepth = param->async_depth;
734 if (IS_SYSTEM_MEMORY(s->in_mem_mode))
735 s->vpp_param.IOPattern |= MFX_IOPATTERN_IN_SYSTEM_MEMORY;
736 else if (IS_VIDEO_MEMORY(s->in_mem_mode))
737 s->vpp_param.IOPattern |= MFX_IOPATTERN_IN_VIDEO_MEMORY;
738 else if (IS_OPAQUE_MEMORY(s->in_mem_mode))
739 s->vpp_param.IOPattern |= MFX_IOPATTERN_IN_OPAQUE_MEMORY;
741 if (IS_SYSTEM_MEMORY(s->out_mem_mode))
742 s->vpp_param.IOPattern |= MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
743 else if (IS_VIDEO_MEMORY(s->out_mem_mode))
744 s->vpp_param.IOPattern |= MFX_IOPATTERN_OUT_VIDEO_MEMORY;
745 else if (IS_OPAQUE_MEMORY(s->out_mem_mode))
746 s->vpp_param.IOPattern |= MFX_IOPATTERN_OUT_OPAQUE_MEMORY;
748 /* Print input memory mode */
749 ff_qsvvpp_print_iopattern(avctx, s->vpp_param.IOPattern & 0x0F, "VPP");
750 /* Print output memory mode */
751 ff_qsvvpp_print_iopattern(avctx, s->vpp_param.IOPattern & 0xF0, "VPP");
752 ret = MFXVideoVPP_Init(s->session, &s->vpp_param);
754 ret = ff_qsvvpp_print_error(avctx, ret, "Failed to create a qsvvpp");
757 ff_qsvvpp_print_warning(avctx, ret, "Warning When creating qsvvpp");
768 int ff_qsvvpp_free(QSVVPPContext **vpp)
770 QSVVPPContext *s = *vpp;
776 MFXVideoVPP_Close(s->session);
777 MFXClose(s->session);
780 /* release all the resources */
781 clear_frame_list(&s->in_frame_list);
782 clear_frame_list(&s->out_frame_list);
783 av_freep(&s->surface_ptrs_in);
784 av_freep(&s->surface_ptrs_out);
785 av_freep(&s->ext_buffers);
786 av_freep(&s->frame_infos);
787 av_fifo_free(s->async_fifo);
793 int ff_qsvvpp_filter_frame(QSVVPPContext *s, AVFilterLink *inlink, AVFrame *picref)
795 AVFilterContext *ctx = inlink->dst;
796 AVFilterLink *outlink = ctx->outputs[0];
798 QSVFrame *in_frame, *out_frame, *tmp;
801 while (s->eof && qsv_fifo_size(s->async_fifo)) {
802 av_fifo_generic_read(s->async_fifo, &tmp, sizeof(tmp), NULL);
803 av_fifo_generic_read(s->async_fifo, &sync, sizeof(sync), NULL);
804 if (MFXVideoCORE_SyncOperation(s->session, sync, 1000) < 0)
805 av_log(ctx, AV_LOG_WARNING, "Sync failed.\n");
807 filter_ret = s->filter_frame(outlink, tmp->frame);
808 if (filter_ret < 0) {
809 av_frame_free(&tmp->frame);
821 in_frame = submit_frame(s, inlink, picref);
823 av_log(ctx, AV_LOG_ERROR, "Failed to submit frame on input[%d]\n",
824 FF_INLINK_IDX(inlink));
825 return AVERROR(ENOMEM);
829 out_frame = query_frame(s, outlink);
831 av_log(ctx, AV_LOG_ERROR, "Failed to query an output frame.\n");
832 return AVERROR(ENOMEM);
836 ret = MFXVideoVPP_RunFrameVPPAsync(s->session, &in_frame->surface,
837 &out_frame->surface, NULL, &sync);
838 if (ret == MFX_WRN_DEVICE_BUSY)
840 } while (ret == MFX_WRN_DEVICE_BUSY);
842 if (ret < 0 && ret != MFX_ERR_MORE_SURFACE) {
843 /* Ignore more_data error */
844 if (ret == MFX_ERR_MORE_DATA)
845 ret = AVERROR(EAGAIN);
848 out_frame->frame->pts = av_rescale_q(out_frame->surface.Data.TimeStamp,
849 default_tb, outlink->time_base);
852 av_fifo_generic_write(s->async_fifo, &out_frame, sizeof(out_frame), NULL);
853 av_fifo_generic_write(s->async_fifo, &sync, sizeof(sync), NULL);
856 if (qsv_fifo_size(s->async_fifo) > s->async_depth) {
857 av_fifo_generic_read(s->async_fifo, &tmp, sizeof(tmp), NULL);
858 av_fifo_generic_read(s->async_fifo, &sync, sizeof(sync), NULL);
861 ret = MFXVideoCORE_SyncOperation(s->session, sync, 1000);
862 } while (ret == MFX_WRN_IN_EXECUTION);
864 filter_ret = s->filter_frame(outlink, tmp->frame);
865 if (filter_ret < 0) {
866 av_frame_free(&tmp->frame);
875 } while(ret == MFX_ERR_MORE_SURFACE);