qsvvpp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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_BGRA:
  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. case AV_PIX_FMT_P010:
  132. surface->Data.Y = frame->data[0];
  133. surface->Data.UV = frame->data[1];
  134. break;
  135. case AV_PIX_FMT_YUV420P:
  136. surface->Data.Y = frame->data[0];
  137. surface->Data.U = frame->data[1];
  138. surface->Data.V = frame->data[2];
  139. break;
  140. case AV_PIX_FMT_YUYV422:
  141. surface->Data.Y = frame->data[0];
  142. surface->Data.U = frame->data[0] + 1;
  143. surface->Data.V = frame->data[0] + 3;
  144. break;
  145. case AV_PIX_FMT_RGB32:
  146. surface->Data.B = frame->data[0];
  147. surface->Data.G = frame->data[0] + 1;
  148. surface->Data.R = frame->data[0] + 2;
  149. surface->Data.A = frame->data[0] + 3;
  150. break;
  151. default:
  152. return MFX_ERR_UNSUPPORTED;
  153. }
  154. surface->Data.Pitch = frame->linesize[0];
  155. return 0;
  156. }
  157. /* fill the surface info */
  158. static int fill_frameinfo_by_link(mfxFrameInfo *frameinfo, AVFilterLink *link)
  159. {
  160. enum AVPixelFormat pix_fmt;
  161. AVHWFramesContext *frames_ctx;
  162. AVQSVFramesContext *frames_hwctx;
  163. const AVPixFmtDescriptor *desc;
  164. if (link->format == AV_PIX_FMT_QSV) {
  165. if (!link->hw_frames_ctx)
  166. return AVERROR(EINVAL);
  167. frames_ctx = (AVHWFramesContext *)link->hw_frames_ctx->data;
  168. frames_hwctx = frames_ctx->hwctx;
  169. *frameinfo = frames_hwctx->surfaces[0].Info;
  170. } else {
  171. pix_fmt = link->format;
  172. desc = av_pix_fmt_desc_get(pix_fmt);
  173. if (!desc)
  174. return AVERROR_BUG;
  175. frameinfo->CropX = 0;
  176. frameinfo->CropY = 0;
  177. frameinfo->Width = FFALIGN(link->w, 32);
  178. frameinfo->Height = FFALIGN(link->h, 32);
  179. frameinfo->PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
  180. frameinfo->FourCC = pix_fmt_to_mfx_fourcc(pix_fmt);
  181. frameinfo->BitDepthLuma = desc->comp[0].depth;
  182. frameinfo->BitDepthChroma = desc->comp[0].depth;
  183. frameinfo->Shift = desc->comp[0].depth > 8;
  184. if (desc->log2_chroma_w && desc->log2_chroma_h)
  185. frameinfo->ChromaFormat = MFX_CHROMAFORMAT_YUV420;
  186. else if (desc->log2_chroma_w)
  187. frameinfo->ChromaFormat = MFX_CHROMAFORMAT_YUV422;
  188. else
  189. frameinfo->ChromaFormat = MFX_CHROMAFORMAT_YUV444;
  190. }
  191. frameinfo->CropW = link->w;
  192. frameinfo->CropH = link->h;
  193. frameinfo->FrameRateExtN = link->frame_rate.num;
  194. frameinfo->FrameRateExtD = link->frame_rate.den;
  195. frameinfo->AspectRatioW = link->sample_aspect_ratio.num ? link->sample_aspect_ratio.num : 1;
  196. frameinfo->AspectRatioH = link->sample_aspect_ratio.den ? link->sample_aspect_ratio.den : 1;
  197. return 0;
  198. }
  199. static void clear_unused_frames(QSVFrame *list)
  200. {
  201. while (list) {
  202. if (list->surface && !list->surface->Data.Locked) {
  203. list->surface = NULL;
  204. av_frame_free(&list->frame);
  205. }
  206. list = list->next;
  207. }
  208. }
  209. static void clear_frame_list(QSVFrame **list)
  210. {
  211. while (*list) {
  212. QSVFrame *frame;
  213. frame = *list;
  214. *list = (*list)->next;
  215. av_frame_free(&frame->frame);
  216. av_freep(&frame);
  217. }
  218. }
  219. static QSVFrame *get_free_frame(QSVFrame **list)
  220. {
  221. QSVFrame *out = *list;
  222. for (; out; out = out->next) {
  223. if (!out->surface)
  224. break;
  225. }
  226. if (!out) {
  227. out = av_mallocz(sizeof(*out));
  228. if (!out) {
  229. av_log(NULL, AV_LOG_ERROR, "Can't alloc new output frame.\n");
  230. return NULL;
  231. }
  232. out->next = *list;
  233. *list = out;
  234. }
  235. return out;
  236. }
  237. /* get the input surface */
  238. static QSVFrame *submit_frame(QSVVPPContext *s, AVFilterLink *inlink, AVFrame *picref)
  239. {
  240. QSVFrame *qsv_frame;
  241. AVFilterContext *ctx = inlink->dst;
  242. clear_unused_frames(s->in_frame_list);
  243. qsv_frame = get_free_frame(&s->in_frame_list);
  244. if (!qsv_frame)
  245. return NULL;
  246. /* Turn AVFrame into mfxFrameSurface1.
  247. * For video/opaque memory mode, pix_fmt is AV_PIX_FMT_QSV, and
  248. * mfxFrameSurface1 is stored in AVFrame->data[3];
  249. * for system memory mode, raw video data is stored in
  250. * AVFrame, we should map it into mfxFrameSurface1.
  251. */
  252. if (!IS_SYSTEM_MEMORY(s->in_mem_mode)) {
  253. if (picref->format != AV_PIX_FMT_QSV) {
  254. av_log(ctx, AV_LOG_ERROR, "QSVVPP gets a wrong frame.\n");
  255. return NULL;
  256. }
  257. qsv_frame->frame = av_frame_clone(picref);
  258. qsv_frame->surface = (mfxFrameSurface1 *)qsv_frame->frame->data[3];
  259. } else {
  260. /* make a copy if the input is not padded as libmfx requires */
  261. if (picref->height & 31 || picref->linesize[0] & 31) {
  262. qsv_frame->frame = ff_get_video_buffer(inlink,
  263. FFALIGN(inlink->w, 32),
  264. FFALIGN(inlink->h, 32));
  265. if (!qsv_frame->frame)
  266. return NULL;
  267. qsv_frame->frame->width = picref->width;
  268. qsv_frame->frame->height = picref->height;
  269. if (av_frame_copy(qsv_frame->frame, picref) < 0) {
  270. av_frame_free(&qsv_frame->frame);
  271. return NULL;
  272. }
  273. av_frame_copy_props(qsv_frame->frame, picref);
  274. } else
  275. qsv_frame->frame = av_frame_clone(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. if (avctx->extra_hw_frames > 0)
  391. out_frames_ctx->initial_pool_size += avctx->extra_hw_frames;
  392. out_frames_hwctx->frame_type = s->out_mem_mode;
  393. ret = av_hwframe_ctx_init(out_frames_ref);
  394. if (ret < 0) {
  395. av_buffer_unref(&out_frames_ref);
  396. av_log(avctx, AV_LOG_ERROR, "Error creating frames_ctx for output pad.\n");
  397. return ret;
  398. }
  399. s->surface_ptrs_out = av_mallocz_array(out_frames_hwctx->nb_surfaces,
  400. sizeof(*s->surface_ptrs_out));
  401. if (!s->surface_ptrs_out) {
  402. av_buffer_unref(&out_frames_ref);
  403. return AVERROR(ENOMEM);
  404. }
  405. for (i = 0; i < out_frames_hwctx->nb_surfaces; i++)
  406. s->surface_ptrs_out[i] = out_frames_hwctx->surfaces + i;
  407. s->nb_surface_ptrs_out = out_frames_hwctx->nb_surfaces;
  408. av_buffer_unref(&outlink->hw_frames_ctx);
  409. outlink->hw_frames_ctx = out_frames_ref;
  410. } else
  411. s->out_mem_mode = MFX_MEMTYPE_SYSTEM_MEMORY;
  412. /* extract the properties of the "master" session given to us */
  413. ret = MFXQueryIMPL(device_hwctx->session, &impl);
  414. if (ret == MFX_ERR_NONE)
  415. ret = MFXQueryVersion(device_hwctx->session, &ver);
  416. if (ret != MFX_ERR_NONE) {
  417. av_log(avctx, AV_LOG_ERROR, "Error querying the session attributes\n");
  418. return AVERROR_UNKNOWN;
  419. }
  420. for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
  421. ret = MFXVideoCORE_GetHandle(device_hwctx->session, handle_types[i], &handle);
  422. if (ret == MFX_ERR_NONE) {
  423. handle_type = handle_types[i];
  424. break;
  425. }
  426. }
  427. if (ret != MFX_ERR_NONE) {
  428. av_log(avctx, AV_LOG_ERROR, "Error getting the session handle\n");
  429. return AVERROR_UNKNOWN;
  430. }
  431. /* create a "slave" session with those same properties, to be used for vpp */
  432. ret = MFXInit(impl, &ver, &s->session);
  433. if (ret != MFX_ERR_NONE) {
  434. av_log(avctx, AV_LOG_ERROR, "Error initializing a session for scaling\n");
  435. return AVERROR_UNKNOWN;
  436. }
  437. if (handle) {
  438. ret = MFXVideoCORE_SetHandle(s->session, handle_type, handle);
  439. if (ret != MFX_ERR_NONE)
  440. return AVERROR_UNKNOWN;
  441. }
  442. if (QSV_RUNTIME_VERSION_ATLEAST(ver, 1, 25)) {
  443. ret = MFXJoinSession(device_hwctx->session, s->session);
  444. if (ret != MFX_ERR_NONE)
  445. return AVERROR_UNKNOWN;
  446. }
  447. if (IS_OPAQUE_MEMORY(s->in_mem_mode) || IS_OPAQUE_MEMORY(s->out_mem_mode)) {
  448. s->opaque_alloc.In.Surfaces = s->surface_ptrs_in;
  449. s->opaque_alloc.In.NumSurface = s->nb_surface_ptrs_in;
  450. s->opaque_alloc.In.Type = s->in_mem_mode;
  451. s->opaque_alloc.Out.Surfaces = s->surface_ptrs_out;
  452. s->opaque_alloc.Out.NumSurface = s->nb_surface_ptrs_out;
  453. s->opaque_alloc.Out.Type = s->out_mem_mode;
  454. s->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
  455. s->opaque_alloc.Header.BufferSz = sizeof(s->opaque_alloc);
  456. } else if (IS_VIDEO_MEMORY(s->in_mem_mode) || IS_VIDEO_MEMORY(s->out_mem_mode)) {
  457. mfxFrameAllocator frame_allocator = {
  458. .pthis = s,
  459. .Alloc = frame_alloc,
  460. .Lock = frame_lock,
  461. .Unlock = frame_unlock,
  462. .GetHDL = frame_get_hdl,
  463. .Free = frame_free,
  464. };
  465. ret = MFXVideoCORE_SetFrameAllocator(s->session, &frame_allocator);
  466. if (ret != MFX_ERR_NONE)
  467. return AVERROR_UNKNOWN;
  468. }
  469. return 0;
  470. }
  471. int ff_qsvvpp_create(AVFilterContext *avctx, QSVVPPContext **vpp, QSVVPPParam *param)
  472. {
  473. int i;
  474. int ret;
  475. QSVVPPContext *s;
  476. s = av_mallocz(sizeof(*s));
  477. if (!s)
  478. return AVERROR(ENOMEM);
  479. s->filter_frame = param->filter_frame;
  480. if (!s->filter_frame)
  481. s->filter_frame = ff_filter_frame;
  482. s->out_sw_format = param->out_sw_format;
  483. /* create the vpp session */
  484. ret = init_vpp_session(avctx, s);
  485. if (ret < 0)
  486. goto failed;
  487. s->frame_infos = av_mallocz_array(avctx->nb_inputs, sizeof(*s->frame_infos));
  488. if (!s->frame_infos) {
  489. ret = AVERROR(ENOMEM);
  490. goto failed;
  491. }
  492. /* Init each input's information */
  493. for (i = 0; i < avctx->nb_inputs; i++) {
  494. ret = fill_frameinfo_by_link(&s->frame_infos[i], avctx->inputs[i]);
  495. if (ret < 0)
  496. goto failed;
  497. }
  498. /* Update input's frame info according to crop */
  499. for (i = 0; i < param->num_crop; i++) {
  500. QSVVPPCrop *crop = param->crop + i;
  501. if (crop->in_idx > avctx->nb_inputs) {
  502. ret = AVERROR(EINVAL);
  503. goto failed;
  504. }
  505. s->frame_infos[crop->in_idx].CropX = crop->x;
  506. s->frame_infos[crop->in_idx].CropY = crop->y;
  507. s->frame_infos[crop->in_idx].CropW = crop->w;
  508. s->frame_infos[crop->in_idx].CropH = crop->h;
  509. }
  510. s->vpp_param.vpp.In = s->frame_infos[0];
  511. ret = fill_frameinfo_by_link(&s->vpp_param.vpp.Out, avctx->outputs[0]);
  512. if (ret < 0) {
  513. av_log(avctx, AV_LOG_ERROR, "Fail to get frame info from link.\n");
  514. goto failed;
  515. }
  516. if (IS_OPAQUE_MEMORY(s->in_mem_mode) || IS_OPAQUE_MEMORY(s->out_mem_mode)) {
  517. s->nb_ext_buffers = param->num_ext_buf + 1;
  518. s->ext_buffers = av_mallocz_array(s->nb_ext_buffers, sizeof(*s->ext_buffers));
  519. if (!s->ext_buffers) {
  520. ret = AVERROR(ENOMEM);
  521. goto failed;
  522. }
  523. s->ext_buffers[0] = (mfxExtBuffer *)&s->opaque_alloc;
  524. for (i = 1; i < param->num_ext_buf; i++)
  525. s->ext_buffers[i] = param->ext_buf[i - 1];
  526. s->vpp_param.ExtParam = s->ext_buffers;
  527. s->vpp_param.NumExtParam = s->nb_ext_buffers;
  528. } else {
  529. s->vpp_param.NumExtParam = param->num_ext_buf;
  530. s->vpp_param.ExtParam = param->ext_buf;
  531. }
  532. s->vpp_param.AsyncDepth = 1;
  533. if (IS_SYSTEM_MEMORY(s->in_mem_mode))
  534. s->vpp_param.IOPattern |= MFX_IOPATTERN_IN_SYSTEM_MEMORY;
  535. else if (IS_VIDEO_MEMORY(s->in_mem_mode))
  536. s->vpp_param.IOPattern |= MFX_IOPATTERN_IN_VIDEO_MEMORY;
  537. else if (IS_OPAQUE_MEMORY(s->in_mem_mode))
  538. s->vpp_param.IOPattern |= MFX_IOPATTERN_IN_OPAQUE_MEMORY;
  539. if (IS_SYSTEM_MEMORY(s->out_mem_mode))
  540. s->vpp_param.IOPattern |= MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
  541. else if (IS_VIDEO_MEMORY(s->out_mem_mode))
  542. s->vpp_param.IOPattern |= MFX_IOPATTERN_OUT_VIDEO_MEMORY;
  543. else if (IS_OPAQUE_MEMORY(s->out_mem_mode))
  544. s->vpp_param.IOPattern |= MFX_IOPATTERN_OUT_OPAQUE_MEMORY;
  545. ret = MFXVideoVPP_Init(s->session, &s->vpp_param);
  546. if (ret < 0) {
  547. av_log(avctx, AV_LOG_ERROR, "Failed to create a qsvvpp, ret = %d.\n", ret);
  548. goto failed;
  549. }
  550. *vpp = s;
  551. return 0;
  552. failed:
  553. ff_qsvvpp_free(&s);
  554. return ret;
  555. }
  556. int ff_qsvvpp_free(QSVVPPContext **vpp)
  557. {
  558. QSVVPPContext *s = *vpp;
  559. if (!s)
  560. return 0;
  561. if (s->session) {
  562. MFXVideoVPP_Close(s->session);
  563. MFXClose(s->session);
  564. }
  565. /* release all the resources */
  566. clear_frame_list(&s->in_frame_list);
  567. clear_frame_list(&s->out_frame_list);
  568. av_freep(&s->surface_ptrs_in);
  569. av_freep(&s->surface_ptrs_out);
  570. av_freep(&s->ext_buffers);
  571. av_freep(&s->frame_infos);
  572. av_freep(vpp);
  573. return 0;
  574. }
  575. int ff_qsvvpp_filter_frame(QSVVPPContext *s, AVFilterLink *inlink, AVFrame *picref)
  576. {
  577. AVFilterContext *ctx = inlink->dst;
  578. AVFilterLink *outlink = ctx->outputs[0];
  579. mfxSyncPoint sync;
  580. QSVFrame *in_frame, *out_frame;
  581. int ret, filter_ret;
  582. in_frame = submit_frame(s, inlink, picref);
  583. if (!in_frame) {
  584. av_log(ctx, AV_LOG_ERROR, "Failed to submit frame on input[%d]\n",
  585. FF_INLINK_IDX(inlink));
  586. return AVERROR(ENOMEM);
  587. }
  588. do {
  589. out_frame = query_frame(s, outlink);
  590. if (!out_frame) {
  591. av_log(ctx, AV_LOG_ERROR, "Failed to query an output frame.\n");
  592. return AVERROR(ENOMEM);
  593. }
  594. do {
  595. ret = MFXVideoVPP_RunFrameVPPAsync(s->session, in_frame->surface,
  596. out_frame->surface, NULL, &sync);
  597. if (ret == MFX_WRN_DEVICE_BUSY)
  598. av_usleep(500);
  599. } while (ret == MFX_WRN_DEVICE_BUSY);
  600. if (ret < 0 && ret != MFX_ERR_MORE_SURFACE) {
  601. /* Ignore more_data error */
  602. if (ret == MFX_ERR_MORE_DATA)
  603. ret = AVERROR(EAGAIN);
  604. break;
  605. }
  606. if (MFXVideoCORE_SyncOperation(s->session, sync, 1000) < 0)
  607. av_log(ctx, AV_LOG_WARNING, "Sync failed.\n");
  608. out_frame->frame->pts = av_rescale_q(out_frame->surface->Data.TimeStamp,
  609. default_tb, outlink->time_base);
  610. filter_ret = s->filter_frame(outlink, out_frame->frame);
  611. if (filter_ret < 0) {
  612. av_frame_free(&out_frame->frame);
  613. ret = filter_ret;
  614. break;
  615. }
  616. out_frame->frame = NULL;
  617. } while(ret == MFX_ERR_MORE_SURFACE);
  618. return ret;
  619. }