You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

734 lines
24KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  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.
  8. *
  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.
  13. *
  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
  17. */
  18. /**
  19. * @file
  20. * Intel Quick Sync Video VPP base function
  21. */
  22. #include "libavutil/common.h"
  23. #include "libavutil/mathematics.h"
  24. #include "libavutil/hwcontext.h"
  25. #include "libavutil/hwcontext_qsv.h"
  26. #include "libavutil/time.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "internal.h"
  29. #include "qsvvpp.h"
  30. #include "video.h"
  31. #define IS_VIDEO_MEMORY(mode) (mode & (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET | \
  32. MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET))
  33. #define IS_OPAQUE_MEMORY(mode) (mode & MFX_MEMTYPE_OPAQUE_FRAME)
  34. #define IS_SYSTEM_MEMORY(mode) (mode & MFX_MEMTYPE_SYSTEM_MEMORY)
  35. typedef struct QSVFrame {
  36. AVFrame *frame;
  37. mfxFrameSurface1 *surface;
  38. mfxFrameSurface1 surface_internal; /* for system memory */
  39. struct QSVFrame *next;
  40. } QSVFrame;
  41. /* abstract struct for all QSV filters */
  42. struct QSVVPPContext {
  43. mfxSession session;
  44. int (*filter_frame) (AVFilterLink *outlink, AVFrame *frame);/* callback */
  45. enum AVPixelFormat out_sw_format; /* Real output format */
  46. mfxVideoParam vpp_param;
  47. mfxFrameInfo *frame_infos; /* frame info for each input */
  48. /* members related to the input/output surface */
  49. int in_mem_mode;
  50. int out_mem_mode;
  51. QSVFrame *in_frame_list;
  52. QSVFrame *out_frame_list;
  53. int nb_surface_ptrs_in;
  54. int nb_surface_ptrs_out;
  55. mfxFrameSurface1 **surface_ptrs_in;
  56. mfxFrameSurface1 **surface_ptrs_out;
  57. /* MFXVPP extern parameters */
  58. mfxExtOpaqueSurfaceAlloc opaque_alloc;
  59. mfxExtBuffer **ext_buffers;
  60. int nb_ext_buffers;
  61. };
  62. static const mfxHandleType handle_types[] = {
  63. MFX_HANDLE_VA_DISPLAY,
  64. MFX_HANDLE_D3D9_DEVICE_MANAGER,
  65. MFX_HANDLE_D3D11_DEVICE,
  66. };
  67. static const AVRational default_tb = { 1, 90000 };
  68. /* functions for frameAlloc */
  69. static mfxStatus frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
  70. mfxFrameAllocResponse *resp)
  71. {
  72. QSVVPPContext *s = pthis;
  73. int i;
  74. if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET) ||
  75. !(req->Type & (MFX_MEMTYPE_FROM_VPPIN | MFX_MEMTYPE_FROM_VPPOUT)) ||
  76. !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
  77. return MFX_ERR_UNSUPPORTED;
  78. if (req->Type & MFX_MEMTYPE_FROM_VPPIN) {
  79. resp->mids = av_mallocz(s->nb_surface_ptrs_in * sizeof(*resp->mids));
  80. if (!resp->mids)
  81. return AVERROR(ENOMEM);
  82. for (i = 0; i < s->nb_surface_ptrs_in; i++)
  83. resp->mids[i] = s->surface_ptrs_in[i]->Data.MemId;
  84. resp->NumFrameActual = s->nb_surface_ptrs_in;
  85. } else {
  86. resp->mids = av_mallocz(s->nb_surface_ptrs_out * sizeof(*resp->mids));
  87. if (!resp->mids)
  88. return AVERROR(ENOMEM);
  89. for (i = 0; i < s->nb_surface_ptrs_out; i++)
  90. resp->mids[i] = s->surface_ptrs_out[i]->Data.MemId;
  91. resp->NumFrameActual = s->nb_surface_ptrs_out;
  92. }
  93. return MFX_ERR_NONE;
  94. }
  95. static mfxStatus frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
  96. {
  97. av_freep(&resp->mids);
  98. return MFX_ERR_NONE;
  99. }
  100. static mfxStatus frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
  101. {
  102. return MFX_ERR_UNSUPPORTED;
  103. }
  104. static mfxStatus frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
  105. {
  106. return MFX_ERR_UNSUPPORTED;
  107. }
  108. static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
  109. {
  110. *hdl = mid;
  111. return MFX_ERR_NONE;
  112. }
  113. static int pix_fmt_to_mfx_fourcc(int format)
  114. {
  115. switch (format) {
  116. case AV_PIX_FMT_YUV420P:
  117. return MFX_FOURCC_YV12;
  118. case AV_PIX_FMT_NV12:
  119. return MFX_FOURCC_NV12;
  120. case AV_PIX_FMT_YUYV422:
  121. return MFX_FOURCC_YUY2;
  122. case AV_PIX_FMT_RGB32:
  123. return MFX_FOURCC_RGB4;
  124. }
  125. return MFX_FOURCC_NV12;
  126. }
  127. static int map_frame_to_surface(AVFrame *frame, mfxFrameSurface1 *surface)
  128. {
  129. switch (frame->format) {
  130. case AV_PIX_FMT_NV12:
  131. surface->Data.Y = frame->data[0];
  132. surface->Data.UV = frame->data[1];
  133. break;
  134. case AV_PIX_FMT_YUV420P:
  135. surface->Data.Y = frame->data[0];
  136. surface->Data.U = frame->data[1];
  137. surface->Data.V = frame->data[2];
  138. break;
  139. case AV_PIX_FMT_YUYV422:
  140. surface->Data.Y = frame->data[0];
  141. surface->Data.U = frame->data[0] + 1;
  142. surface->Data.V = frame->data[0] + 3;
  143. break;
  144. case AV_PIX_FMT_RGB32:
  145. surface->Data.B = frame->data[0];
  146. surface->Data.G = frame->data[0] + 1;
  147. surface->Data.R = frame->data[0] + 2;
  148. surface->Data.A = frame->data[0] + 3;
  149. break;
  150. default:
  151. return MFX_ERR_UNSUPPORTED;
  152. }
  153. surface->Data.Pitch = frame->linesize[0];
  154. return 0;
  155. }
  156. /* fill the surface info */
  157. static int fill_frameinfo_by_link(mfxFrameInfo *frameinfo, AVFilterLink *link)
  158. {
  159. enum AVPixelFormat pix_fmt;
  160. AVHWFramesContext *frames_ctx;
  161. AVQSVFramesContext *frames_hwctx;
  162. const AVPixFmtDescriptor *desc;
  163. if (link->format == AV_PIX_FMT_QSV) {
  164. if (!link->hw_frames_ctx)
  165. return AVERROR(EINVAL);
  166. frames_ctx = (AVHWFramesContext *)link->hw_frames_ctx->data;
  167. frames_hwctx = frames_ctx->hwctx;
  168. *frameinfo = frames_hwctx->surfaces[0].Info;
  169. } else {
  170. pix_fmt = link->format;
  171. desc = av_pix_fmt_desc_get(pix_fmt);
  172. if (!desc)
  173. return AVERROR_BUG;
  174. frameinfo->CropX = 0;
  175. frameinfo->CropY = 0;
  176. frameinfo->Width = FFALIGN(link->w, 32);
  177. frameinfo->Height = FFALIGN(link->h, 32);
  178. frameinfo->PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
  179. frameinfo->FourCC = pix_fmt_to_mfx_fourcc(pix_fmt);
  180. frameinfo->BitDepthLuma = desc->comp[0].depth;
  181. frameinfo->BitDepthChroma = desc->comp[0].depth;
  182. frameinfo->Shift = desc->comp[0].depth > 8;
  183. if (desc->log2_chroma_w && desc->log2_chroma_h)
  184. frameinfo->ChromaFormat = MFX_CHROMAFORMAT_YUV420;
  185. else if (desc->log2_chroma_w)
  186. frameinfo->ChromaFormat = MFX_CHROMAFORMAT_YUV422;
  187. else
  188. frameinfo->ChromaFormat = MFX_CHROMAFORMAT_YUV444;
  189. }
  190. frameinfo->CropW = link->w;
  191. frameinfo->CropH = link->h;
  192. frameinfo->FrameRateExtN = link->frame_rate.num;
  193. frameinfo->FrameRateExtD = link->frame_rate.den;
  194. frameinfo->AspectRatioW = link->sample_aspect_ratio.num ? link->sample_aspect_ratio.num : 1;
  195. frameinfo->AspectRatioH = link->sample_aspect_ratio.den ? link->sample_aspect_ratio.den : 1;
  196. return 0;
  197. }
  198. static void clear_unused_frames(QSVFrame *list)
  199. {
  200. while (list) {
  201. if (list->surface && !list->surface->Data.Locked) {
  202. list->surface = NULL;
  203. av_frame_free(&list->frame);
  204. }
  205. list = list->next;
  206. }
  207. }
  208. static void clear_frame_list(QSVFrame **list)
  209. {
  210. while (*list) {
  211. QSVFrame *frame;
  212. frame = *list;
  213. *list = (*list)->next;
  214. av_frame_free(&frame->frame);
  215. av_freep(&frame);
  216. }
  217. }
  218. static QSVFrame *get_free_frame(QSVFrame **list)
  219. {
  220. QSVFrame *out = *list;
  221. for (; out; out = out->next) {
  222. if (!out->surface)
  223. break;
  224. }
  225. if (!out) {
  226. out = av_mallocz(sizeof(*out));
  227. if (!out) {
  228. av_log(NULL, AV_LOG_ERROR, "Can't alloc new output frame.\n");
  229. return NULL;
  230. }
  231. out->next = *list;
  232. *list = out;
  233. }
  234. return out;
  235. }
  236. /* get the input surface */
  237. static QSVFrame *submit_frame(QSVVPPContext *s, AVFilterLink *inlink, AVFrame *picref)
  238. {
  239. QSVFrame *qsv_frame;
  240. AVFilterContext *ctx = inlink->dst;
  241. clear_unused_frames(s->in_frame_list);
  242. qsv_frame = get_free_frame(&s->in_frame_list);
  243. if (!qsv_frame)
  244. return NULL;
  245. /* Turn AVFrame into mfxFrameSurface1.
  246. * For video/opaque memory mode, pix_fmt is AV_PIX_FMT_QSV, and
  247. * mfxFrameSurface1 is stored in AVFrame->data[3];
  248. * for system memory mode, raw video data is stored in
  249. * AVFrame, we should map it into mfxFrameSurface1.
  250. */
  251. if (!IS_SYSTEM_MEMORY(s->in_mem_mode)) {
  252. if (picref->format != AV_PIX_FMT_QSV) {
  253. av_log(ctx, AV_LOG_ERROR, "QSVVPP gets a wrong frame.\n");
  254. return NULL;
  255. }
  256. qsv_frame->frame = picref;
  257. qsv_frame->surface = (mfxFrameSurface1 *)qsv_frame->frame->data[3];
  258. } else {
  259. /* make a copy if the input is not padded as libmfx requires */
  260. if (picref->height & 31 || picref->linesize[0] & 31) {
  261. qsv_frame->frame = ff_get_video_buffer(inlink,
  262. FFALIGN(inlink->w, 32),
  263. FFALIGN(inlink->h, 32));
  264. if (!qsv_frame->frame)
  265. return NULL;
  266. qsv_frame->frame->width = picref->width;
  267. qsv_frame->frame->height = picref->height;
  268. if (av_frame_copy(qsv_frame->frame, picref) < 0) {
  269. av_frame_free(&qsv_frame->frame);
  270. return NULL;
  271. }
  272. av_frame_copy_props(qsv_frame->frame, picref);
  273. av_frame_free(&picref);
  274. } else
  275. qsv_frame->frame = picref;
  276. if (map_frame_to_surface(qsv_frame->frame,
  277. &qsv_frame->surface_internal) < 0) {
  278. av_log(ctx, AV_LOG_ERROR, "Unsupported frame.\n");
  279. return NULL;
  280. }
  281. qsv_frame->surface = &qsv_frame->surface_internal;
  282. }
  283. qsv_frame->surface->Info = s->frame_infos[FF_INLINK_IDX(inlink)];
  284. qsv_frame->surface->Data.TimeStamp = av_rescale_q(qsv_frame->frame->pts,
  285. inlink->time_base, default_tb);
  286. qsv_frame->surface->Info.PicStruct =
  287. !qsv_frame->frame->interlaced_frame ? MFX_PICSTRUCT_PROGRESSIVE :
  288. (qsv_frame->frame->top_field_first ? MFX_PICSTRUCT_FIELD_TFF :
  289. MFX_PICSTRUCT_FIELD_BFF);
  290. if (qsv_frame->frame->repeat_pict == 1)
  291. qsv_frame->surface->Info.PicStruct |= MFX_PICSTRUCT_FIELD_REPEATED;
  292. else if (qsv_frame->frame->repeat_pict == 2)
  293. qsv_frame->surface->Info.PicStruct |= MFX_PICSTRUCT_FRAME_DOUBLING;
  294. else if (qsv_frame->frame->repeat_pict == 4)
  295. qsv_frame->surface->Info.PicStruct |= MFX_PICSTRUCT_FRAME_TRIPLING;
  296. return qsv_frame;
  297. }
  298. /* get the output surface */
  299. static QSVFrame *query_frame(QSVVPPContext *s, AVFilterLink *outlink)
  300. {
  301. AVFilterContext *ctx = outlink->src;
  302. QSVFrame *out_frame;
  303. int ret;
  304. clear_unused_frames(s->out_frame_list);
  305. out_frame = get_free_frame(&s->out_frame_list);
  306. if (!out_frame)
  307. return NULL;
  308. /* For video memory, get a hw frame;
  309. * For system memory, get a sw frame and map it into a mfx_surface. */
  310. if (!IS_SYSTEM_MEMORY(s->out_mem_mode)) {
  311. out_frame->frame = av_frame_alloc();
  312. if (!out_frame->frame)
  313. return NULL;
  314. ret = av_hwframe_get_buffer(outlink->hw_frames_ctx, out_frame->frame, 0);
  315. if (ret < 0) {
  316. av_log(ctx, AV_LOG_ERROR, "Can't allocate a surface.\n");
  317. return NULL;
  318. }
  319. out_frame->surface = (mfxFrameSurface1 *)out_frame->frame->data[3];
  320. } else {
  321. /* Get a frame with aligned dimensions.
  322. * Libmfx need system memory being 128x64 aligned */
  323. out_frame->frame = ff_get_video_buffer(outlink,
  324. FFALIGN(outlink->w, 128),
  325. FFALIGN(outlink->h, 64));
  326. if (!out_frame->frame)
  327. return NULL;
  328. out_frame->frame->width = outlink->w;
  329. out_frame->frame->height = outlink->h;
  330. ret = map_frame_to_surface(out_frame->frame,
  331. &out_frame->surface_internal);
  332. if (ret < 0)
  333. return NULL;
  334. out_frame->surface = &out_frame->surface_internal;
  335. }
  336. out_frame->surface->Info = s->vpp_param.vpp.Out;
  337. return out_frame;
  338. }
  339. /* create the QSV session */
  340. static int init_vpp_session(AVFilterContext *avctx, QSVVPPContext *s)
  341. {
  342. AVFilterLink *inlink = avctx->inputs[0];
  343. AVFilterLink *outlink = avctx->outputs[0];
  344. AVQSVFramesContext *in_frames_hwctx = NULL;
  345. AVQSVFramesContext *out_frames_hwctx = NULL;
  346. AVBufferRef *device_ref;
  347. AVHWDeviceContext *device_ctx;
  348. AVQSVDeviceContext *device_hwctx;
  349. mfxHDL handle;
  350. mfxHandleType handle_type;
  351. mfxVersion ver;
  352. mfxIMPL impl;
  353. int ret, i;
  354. if (inlink->hw_frames_ctx) {
  355. AVHWFramesContext *frames_ctx = (AVHWFramesContext *)inlink->hw_frames_ctx->data;
  356. device_ref = frames_ctx->device_ref;
  357. in_frames_hwctx = frames_ctx->hwctx;
  358. s->in_mem_mode = in_frames_hwctx->frame_type;
  359. s->surface_ptrs_in = av_mallocz_array(in_frames_hwctx->nb_surfaces,
  360. sizeof(*s->surface_ptrs_in));
  361. if (!s->surface_ptrs_in)
  362. return AVERROR(ENOMEM);
  363. for (i = 0; i < in_frames_hwctx->nb_surfaces; i++)
  364. s->surface_ptrs_in[i] = in_frames_hwctx->surfaces + i;
  365. s->nb_surface_ptrs_in = in_frames_hwctx->nb_surfaces;
  366. } else if (avctx->hw_device_ctx) {
  367. device_ref = avctx->hw_device_ctx;
  368. s->in_mem_mode = MFX_MEMTYPE_SYSTEM_MEMORY;
  369. } else {
  370. av_log(avctx, AV_LOG_ERROR, "No hw context provided.\n");
  371. return AVERROR(EINVAL);
  372. }
  373. device_ctx = (AVHWDeviceContext *)device_ref->data;
  374. device_hwctx = device_ctx->hwctx;
  375. if (outlink->format == AV_PIX_FMT_QSV) {
  376. AVHWFramesContext *out_frames_ctx;
  377. AVBufferRef *out_frames_ref = av_hwframe_ctx_alloc(device_ref);
  378. if (!out_frames_ref)
  379. return AVERROR(ENOMEM);
  380. s->out_mem_mode = IS_OPAQUE_MEMORY(s->in_mem_mode) ?
  381. MFX_MEMTYPE_OPAQUE_FRAME :
  382. MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
  383. out_frames_ctx = (AVHWFramesContext *)out_frames_ref->data;
  384. out_frames_hwctx = out_frames_ctx->hwctx;
  385. out_frames_ctx->format = AV_PIX_FMT_QSV;
  386. out_frames_ctx->width = FFALIGN(outlink->w, 32);
  387. out_frames_ctx->height = FFALIGN(outlink->h, 32);
  388. out_frames_ctx->sw_format = s->out_sw_format;
  389. out_frames_ctx->initial_pool_size = 64;
  390. out_frames_hwctx->frame_type = s->out_mem_mode;
  391. ret = av_hwframe_ctx_init(out_frames_ref);
  392. if (ret < 0) {
  393. av_buffer_unref(&out_frames_ref);
  394. av_log(avctx, AV_LOG_ERROR, "Error creating frames_ctx for output pad.\n");
  395. return ret;
  396. }
  397. s->surface_ptrs_out = av_mallocz_array(out_frames_hwctx->nb_surfaces,
  398. sizeof(*s->surface_ptrs_out));
  399. if (!s->surface_ptrs_out) {
  400. av_buffer_unref(&out_frames_ref);
  401. return AVERROR(ENOMEM);
  402. }
  403. for (i = 0; i < out_frames_hwctx->nb_surfaces; i++)
  404. s->surface_ptrs_out[i] = out_frames_hwctx->surfaces + i;
  405. s->nb_surface_ptrs_out = out_frames_hwctx->nb_surfaces;
  406. av_buffer_unref(&outlink->hw_frames_ctx);
  407. outlink->hw_frames_ctx = out_frames_ref;
  408. } else
  409. s->out_mem_mode = MFX_MEMTYPE_SYSTEM_MEMORY;
  410. /* extract the properties of the "master" session given to us */
  411. ret = MFXQueryIMPL(device_hwctx->session, &impl);
  412. if (ret == MFX_ERR_NONE)
  413. ret = MFXQueryVersion(device_hwctx->session, &ver);
  414. if (ret != MFX_ERR_NONE) {
  415. av_log(avctx, AV_LOG_ERROR, "Error querying the session attributes\n");
  416. return AVERROR_UNKNOWN;
  417. }
  418. for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
  419. ret = MFXVideoCORE_GetHandle(device_hwctx->session, handle_types[i], &handle);
  420. if (ret == MFX_ERR_NONE) {
  421. handle_type = handle_types[i];
  422. break;
  423. }
  424. }
  425. /* create a "slave" session with those same properties, to be used for vpp */
  426. ret = MFXInit(impl, &ver, &s->session);
  427. if (ret != MFX_ERR_NONE) {
  428. av_log(avctx, AV_LOG_ERROR, "Error initializing a session for scaling\n");
  429. return AVERROR_UNKNOWN;
  430. }
  431. if (handle) {
  432. ret = MFXVideoCORE_SetHandle(s->session, handle_type, handle);
  433. if (ret != MFX_ERR_NONE)
  434. return AVERROR_UNKNOWN;
  435. }
  436. if (QSV_RUNTIME_VERSION_ATLEAST(ver, 1, 25)) {
  437. ret = MFXJoinSession(device_hwctx->session, s->session);
  438. if (ret != MFX_ERR_NONE)
  439. return AVERROR_UNKNOWN;
  440. }
  441. if (IS_OPAQUE_MEMORY(s->in_mem_mode) || IS_OPAQUE_MEMORY(s->out_mem_mode)) {
  442. s->opaque_alloc.In.Surfaces = s->surface_ptrs_in;
  443. s->opaque_alloc.In.NumSurface = s->nb_surface_ptrs_in;
  444. s->opaque_alloc.In.Type = s->in_mem_mode;
  445. s->opaque_alloc.Out.Surfaces = s->surface_ptrs_out;
  446. s->opaque_alloc.Out.NumSurface = s->nb_surface_ptrs_out;
  447. s->opaque_alloc.Out.Type = s->out_mem_mode;
  448. s->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
  449. s->opaque_alloc.Header.BufferSz = sizeof(s->opaque_alloc);
  450. } else if (IS_VIDEO_MEMORY(s->in_mem_mode) || IS_VIDEO_MEMORY(s->out_mem_mode)) {
  451. mfxFrameAllocator frame_allocator = {
  452. .pthis = s,
  453. .Alloc = frame_alloc,
  454. .Lock = frame_lock,
  455. .Unlock = frame_unlock,
  456. .GetHDL = frame_get_hdl,
  457. .Free = frame_free,
  458. };
  459. ret = MFXVideoCORE_SetFrameAllocator(s->session, &frame_allocator);
  460. if (ret != MFX_ERR_NONE)
  461. return AVERROR_UNKNOWN;
  462. }
  463. return 0;
  464. }
  465. int ff_qsvvpp_create(AVFilterContext *avctx, QSVVPPContext **vpp, QSVVPPParam *param)
  466. {
  467. int i;
  468. int ret;
  469. QSVVPPContext *s;
  470. s = av_mallocz(sizeof(*s));
  471. if (!s)
  472. return AVERROR(ENOMEM);
  473. s->filter_frame = param->filter_frame;
  474. if (!s->filter_frame)
  475. s->filter_frame = ff_filter_frame;
  476. s->out_sw_format = param->out_sw_format;
  477. /* create the vpp session */
  478. ret = init_vpp_session(avctx, s);
  479. if (ret < 0)
  480. goto failed;
  481. s->frame_infos = av_mallocz_array(avctx->nb_inputs, sizeof(*s->frame_infos));
  482. if (!s->frame_infos) {
  483. ret = AVERROR(ENOMEM);
  484. goto failed;
  485. }
  486. /* Init each input's information */
  487. for (i = 0; i < avctx->nb_inputs; i++) {
  488. ret = fill_frameinfo_by_link(&s->frame_infos[i], avctx->inputs[i]);
  489. if (ret < 0)
  490. goto failed;
  491. }
  492. /* Update input's frame info according to crop */
  493. for (i = 0; i < param->num_crop; i++) {
  494. QSVVPPCrop *crop = param->crop + i;
  495. if (crop->in_idx > avctx->nb_inputs) {
  496. ret = AVERROR(EINVAL);
  497. goto failed;
  498. }
  499. s->frame_infos[crop->in_idx].CropX = crop->x;
  500. s->frame_infos[crop->in_idx].CropY = crop->y;
  501. s->frame_infos[crop->in_idx].CropW = crop->w;
  502. s->frame_infos[crop->in_idx].CropH = crop->h;
  503. }
  504. s->vpp_param.vpp.In = s->frame_infos[0];
  505. ret = fill_frameinfo_by_link(&s->vpp_param.vpp.Out, avctx->outputs[0]);
  506. if (ret < 0) {
  507. av_log(avctx, AV_LOG_ERROR, "Fail to get frame info from link.\n");
  508. goto failed;
  509. }
  510. if (IS_OPAQUE_MEMORY(s->in_mem_mode) || IS_OPAQUE_MEMORY(s->out_mem_mode)) {
  511. s->nb_ext_buffers = param->num_ext_buf + 1;
  512. s->ext_buffers = av_mallocz_array(s->nb_ext_buffers, sizeof(*s->ext_buffers));
  513. if (!s->ext_buffers) {
  514. ret = AVERROR(ENOMEM);
  515. goto failed;
  516. }
  517. s->ext_buffers[0] = (mfxExtBuffer *)&s->opaque_alloc;
  518. for (i = 1; i < param->num_ext_buf; i++)
  519. s->ext_buffers[i] = param->ext_buf[i - 1];
  520. s->vpp_param.ExtParam = s->ext_buffers;
  521. s->vpp_param.NumExtParam = s->nb_ext_buffers;
  522. } else {
  523. s->vpp_param.NumExtParam = param->num_ext_buf;
  524. s->vpp_param.ExtParam = param->ext_buf;
  525. }
  526. s->vpp_param.AsyncDepth = 1;
  527. if (IS_SYSTEM_MEMORY(s->in_mem_mode))
  528. s->vpp_param.IOPattern |= MFX_IOPATTERN_IN_SYSTEM_MEMORY;
  529. else if (IS_VIDEO_MEMORY(s->in_mem_mode))
  530. s->vpp_param.IOPattern |= MFX_IOPATTERN_IN_VIDEO_MEMORY;
  531. else if (IS_OPAQUE_MEMORY(s->in_mem_mode))
  532. s->vpp_param.IOPattern |= MFX_IOPATTERN_IN_OPAQUE_MEMORY;
  533. if (IS_SYSTEM_MEMORY(s->out_mem_mode))
  534. s->vpp_param.IOPattern |= MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
  535. else if (IS_VIDEO_MEMORY(s->out_mem_mode))
  536. s->vpp_param.IOPattern |= MFX_IOPATTERN_OUT_VIDEO_MEMORY;
  537. else if (IS_OPAQUE_MEMORY(s->out_mem_mode))
  538. s->vpp_param.IOPattern |= MFX_IOPATTERN_OUT_OPAQUE_MEMORY;
  539. ret = MFXVideoVPP_Init(s->session, &s->vpp_param);
  540. if (ret < 0) {
  541. av_log(avctx, AV_LOG_ERROR, "Failed to create a qsvvpp, ret = %d.\n", ret);
  542. goto failed;
  543. }
  544. *vpp = s;
  545. return 0;
  546. failed:
  547. ff_qsvvpp_free(&s);
  548. return ret;
  549. }
  550. int ff_qsvvpp_free(QSVVPPContext **vpp)
  551. {
  552. QSVVPPContext *s = *vpp;
  553. if (!s)
  554. return 0;
  555. if (s->session) {
  556. MFXVideoVPP_Close(s->session);
  557. MFXClose(s->session);
  558. }
  559. /* release all the resources */
  560. clear_frame_list(&s->in_frame_list);
  561. clear_frame_list(&s->out_frame_list);
  562. av_freep(&s->surface_ptrs_in);
  563. av_freep(&s->surface_ptrs_out);
  564. av_freep(&s->ext_buffers);
  565. av_freep(&s->frame_infos);
  566. av_freep(vpp);
  567. return 0;
  568. }
  569. int ff_qsvvpp_filter_frame(QSVVPPContext *s, AVFilterLink *inlink, AVFrame *picref)
  570. {
  571. AVFilterContext *ctx = inlink->dst;
  572. AVFilterLink *outlink = ctx->outputs[0];
  573. mfxSyncPoint sync;
  574. QSVFrame *in_frame, *out_frame;
  575. int ret, filter_ret;
  576. in_frame = submit_frame(s, inlink, picref);
  577. if (!in_frame) {
  578. av_log(ctx, AV_LOG_ERROR, "Failed to submit frame on input[%d]\n",
  579. FF_INLINK_IDX(inlink));
  580. return AVERROR(ENOMEM);
  581. }
  582. do {
  583. out_frame = query_frame(s, outlink);
  584. if (!out_frame) {
  585. av_log(ctx, AV_LOG_ERROR, "Failed to query an output frame.\n");
  586. return AVERROR(ENOMEM);
  587. }
  588. do {
  589. ret = MFXVideoVPP_RunFrameVPPAsync(s->session, in_frame->surface,
  590. out_frame->surface, NULL, &sync);
  591. if (ret == MFX_WRN_DEVICE_BUSY)
  592. av_usleep(500);
  593. } while (ret == MFX_WRN_DEVICE_BUSY);
  594. if (ret < 0 && ret != MFX_ERR_MORE_SURFACE) {
  595. /* Ignore more_data error */
  596. if (ret == MFX_ERR_MORE_DATA)
  597. ret = AVERROR(EAGAIN);
  598. break;
  599. }
  600. if (MFXVideoCORE_SyncOperation(s->session, sync, 1000) < 0)
  601. av_log(ctx, AV_LOG_WARNING, "Sync failed.\n");
  602. out_frame->frame->pts = av_rescale_q(out_frame->surface->Data.TimeStamp,
  603. default_tb, outlink->time_base);
  604. filter_ret = s->filter_frame(outlink, out_frame->frame);
  605. if (filter_ret < 0) {
  606. av_frame_free(&out_frame->frame);
  607. ret = filter_ret;
  608. break;
  609. }
  610. out_frame->frame = NULL;
  611. } while(ret == MFX_ERR_MORE_SURFACE);
  612. return ret;
  613. }