FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hwcontext_qsv.c
Go to the documentation of this file.
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 #include <stdint.h>
20 #include <string.h>
21 
22 #include <mfx/mfxvideo.h>
23 
24 #include "config.h"
25 
26 #if CONFIG_VAAPI
27 #include "hwcontext_vaapi.h"
28 #endif
29 #if CONFIG_DXVA2
30 #include "hwcontext_dxva2.h"
31 #endif
32 
33 #include "buffer.h"
34 #include "common.h"
35 #include "hwcontext.h"
36 #include "hwcontext_internal.h"
37 #include "hwcontext_qsv.h"
38 #include "mem.h"
39 #include "pixfmt.h"
40 #include "pixdesc.h"
41 #include "time.h"
42 
43 typedef struct QSVDevicePriv {
46 
47 typedef struct QSVDeviceContext {
48  mfxHDL handle;
49  mfxHandleType handle_type;
50  mfxVersion ver;
51  mfxIMPL impl;
52 
56 
57 typedef struct QSVFramesContext {
58  mfxSession session_download;
59  mfxSession session_upload;
60 
62  mfxFrameSurface1 *surfaces_internal;
64 
65  // used in the frame allocator for non-opaque surfaces
66  mfxMemId *mem_ids;
67  // used in the opaque alloc request for opaque surfaces
68  mfxFrameSurface1 **surface_ptrs;
69 
70  mfxExtOpaqueSurfaceAlloc opaque_alloc;
71  mfxExtBuffer *ext_buffers[1];
73 
74 static const struct {
75  mfxHandleType handle_type;
79 #if CONFIG_VAAPI
80  { MFX_HANDLE_VA_DISPLAY, AV_HWDEVICE_TYPE_VAAPI, AV_PIX_FMT_VAAPI },
81 #endif
82 #if CONFIG_DXVA2
83  { MFX_HANDLE_D3D9_DEVICE_MANAGER, AV_HWDEVICE_TYPE_DXVA2, AV_PIX_FMT_DXVA2_VLD },
84 #endif
85  { 0 },
86 };
87 
88 static const struct {
90  uint32_t fourcc;
92  { AV_PIX_FMT_NV12, MFX_FOURCC_NV12 },
93  { AV_PIX_FMT_P010, MFX_FOURCC_P010 },
94  { AV_PIX_FMT_PAL8, MFX_FOURCC_P8 },
95 };
96 
98 {
99  int i;
100  for (i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++) {
101  if (supported_pixel_formats[i].pix_fmt == pix_fmt)
102  return supported_pixel_formats[i].fourcc;
103  }
104  return 0;
105 }
106 
108 {
109  AVQSVDeviceContext *hwctx = ctx->hwctx;
110  QSVDeviceContext *s = ctx->internal->priv;
111 
112  mfxStatus err;
113  int i;
114 
115  for (i = 0; supported_handle_types[i].handle_type; i++) {
116  err = MFXVideoCORE_GetHandle(hwctx->session, supported_handle_types[i].handle_type,
117  &s->handle);
118  if (err == MFX_ERR_NONE) {
119  s->handle_type = supported_handle_types[i].handle_type;
120  s->child_device_type = supported_handle_types[i].device_type;
121  s->child_pix_fmt = supported_handle_types[i].pix_fmt;
122  break;
123  }
124  }
125  if (!s->handle) {
126  av_log(ctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
127  "from the session\n");
128  }
129 
130  err = MFXQueryIMPL(hwctx->session, &s->impl);
131  if (err == MFX_ERR_NONE)
132  err = MFXQueryVersion(hwctx->session, &s->ver);
133  if (err != MFX_ERR_NONE) {
134  av_log(ctx, AV_LOG_ERROR, "Error querying the session attributes\n");
135  return AVERROR_UNKNOWN;
136  }
137 
138  return 0;
139 }
140 
142 {
143  QSVFramesContext *s = ctx->internal->priv;
144 
145  if (s->session_download) {
146  MFXVideoVPP_Close(s->session_download);
147  MFXClose(s->session_download);
148  }
149  s->session_download = NULL;
150 
151  if (s->session_upload) {
152  MFXVideoVPP_Close(s->session_upload);
153  MFXClose(s->session_upload);
154  }
155  s->session_upload = NULL;
156 
157  av_freep(&s->mem_ids);
158  av_freep(&s->surface_ptrs);
161 }
162 
163 static void qsv_pool_release_dummy(void *opaque, uint8_t *data)
164 {
165 }
166 
167 static AVBufferRef *qsv_pool_alloc(void *opaque, int size)
168 {
170  QSVFramesContext *s = ctx->internal->priv;
171  AVQSVFramesContext *hwctx = ctx->hwctx;
172 
173  if (s->nb_surfaces_used < hwctx->nb_surfaces) {
174  s->nb_surfaces_used++;
176  sizeof(*hwctx->surfaces), qsv_pool_release_dummy, NULL, 0);
177  }
178 
179  return NULL;
180 }
181 
183 {
184  AVQSVFramesContext *hwctx = ctx->hwctx;
185  QSVFramesContext *s = ctx->internal->priv;
186  QSVDeviceContext *device_priv = ctx->device_ctx->internal->priv;
187 
188  AVBufferRef *child_device_ref = NULL;
189  AVBufferRef *child_frames_ref = NULL;
190 
191  AVHWDeviceContext *child_device_ctx;
192  AVHWFramesContext *child_frames_ctx;
193 
194  int i, ret = 0;
195 
196  if (!device_priv->handle) {
197  av_log(ctx, AV_LOG_ERROR,
198  "Cannot create a non-opaque internal surface pool without "
199  "a hardware handle\n");
200  return AVERROR(EINVAL);
201  }
202 
203  child_device_ref = av_hwdevice_ctx_alloc(device_priv->child_device_type);
204  if (!child_device_ref)
205  return AVERROR(ENOMEM);
206  child_device_ctx = (AVHWDeviceContext*)child_device_ref->data;
207 
208 #if CONFIG_VAAPI
209  if (child_device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
210  AVVAAPIDeviceContext *child_device_hwctx = child_device_ctx->hwctx;
211  child_device_hwctx->display = (VADisplay)device_priv->handle;
212  }
213 #endif
214 #if CONFIG_DXVA2
215  if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
216  AVDXVA2DeviceContext *child_device_hwctx = child_device_ctx->hwctx;
217  child_device_hwctx->devmgr = (IDirect3DDeviceManager9*)device_priv->handle;
218  }
219 #endif
220 
221  ret = av_hwdevice_ctx_init(child_device_ref);
222  if (ret < 0) {
223  av_log(ctx, AV_LOG_ERROR, "Error initializing a child device context\n");
224  goto fail;
225  }
226 
227  child_frames_ref = av_hwframe_ctx_alloc(child_device_ref);
228  if (!child_frames_ref) {
229  ret = AVERROR(ENOMEM);
230  goto fail;
231  }
232  child_frames_ctx = (AVHWFramesContext*)child_frames_ref->data;
233 
234  child_frames_ctx->format = device_priv->child_pix_fmt;
235  child_frames_ctx->sw_format = ctx->sw_format;
236  child_frames_ctx->initial_pool_size = ctx->initial_pool_size;
237  child_frames_ctx->width = ctx->width;
238  child_frames_ctx->height = ctx->height;
239 
240 #if CONFIG_DXVA2
241  if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
242  AVDXVA2FramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
243  if (hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)
244  child_frames_hwctx->surface_type = DXVA2_VideoProcessorRenderTarget;
245  else
246  child_frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
247  }
248 #endif
249 
250  ret = av_hwframe_ctx_init(child_frames_ref);
251  if (ret < 0) {
252  av_log(ctx, AV_LOG_ERROR, "Error initializing a child frames context\n");
253  goto fail;
254  }
255 
256 #if CONFIG_VAAPI
257  if (child_device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
258  AVVAAPIFramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
259  for (i = 0; i < ctx->initial_pool_size; i++)
260  s->surfaces_internal[i].Data.MemId = child_frames_hwctx->surface_ids + i;
261  hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
262  }
263 #endif
264 #if CONFIG_DXVA2
265  if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
266  AVDXVA2FramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
267  for (i = 0; i < ctx->initial_pool_size; i++)
268  s->surfaces_internal[i].Data.MemId = (mfxMemId)child_frames_hwctx->surfaces[i];
269  if (child_frames_hwctx->surface_type == DXVA2_VideoProcessorRenderTarget)
270  hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
271  else
272  hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
273  }
274 #endif
275 
276  s->child_frames_ref = child_frames_ref;
277  child_frames_ref = NULL;
278 
279 fail:
280  av_buffer_unref(&child_device_ref);
281  av_buffer_unref(&child_frames_ref);
282  return ret;
283 }
284 
285 static int qsv_init_surface(AVHWFramesContext *ctx, mfxFrameSurface1 *surf)
286 {
287  const AVPixFmtDescriptor *desc;
288  uint32_t fourcc;
289 
290  desc = av_pix_fmt_desc_get(ctx->sw_format);
291  if (!desc)
292  return AVERROR(EINVAL);
293 
294  fourcc = qsv_fourcc_from_pix_fmt(ctx->sw_format);
295  if (!fourcc)
296  return AVERROR(EINVAL);
297 
298  surf->Info.BitDepthLuma = desc->comp[0].depth;
299  surf->Info.BitDepthChroma = desc->comp[0].depth;
300  surf->Info.Shift = desc->comp[0].depth > 8;
301 
302  if (desc->log2_chroma_w && desc->log2_chroma_h)
303  surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
304  else if (desc->log2_chroma_w)
305  surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV422;
306  else
307  surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV444;
308 
309  surf->Info.FourCC = fourcc;
310  surf->Info.Width = ctx->width;
311  surf->Info.CropW = ctx->width;
312  surf->Info.Height = ctx->height;
313  surf->Info.CropH = ctx->height;
314  surf->Info.FrameRateExtN = 25;
315  surf->Info.FrameRateExtD = 1;
316 
317  return 0;
318 }
319 
321 {
322  QSVFramesContext *s = ctx->internal->priv;
323  AVQSVFramesContext *frames_hwctx = ctx->hwctx;
324 
325  int i, ret = 0;
326 
327  if (ctx->initial_pool_size <= 0) {
328  av_log(ctx, AV_LOG_ERROR, "QSV requires a fixed frame pool size\n");
329  return AVERROR(EINVAL);
330  }
331 
333  sizeof(*s->surfaces_internal));
334  if (!s->surfaces_internal)
335  return AVERROR(ENOMEM);
336 
337  for (i = 0; i < ctx->initial_pool_size; i++) {
338  ret = qsv_init_surface(ctx, &s->surfaces_internal[i]);
339  if (ret < 0)
340  return ret;
341  }
342 
343  if (!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)) {
344  ret = qsv_init_child_ctx(ctx);
345  if (ret < 0)
346  return ret;
347  }
348 
349  ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(mfxFrameSurface1),
350  ctx, qsv_pool_alloc, NULL);
351  if (!ctx->internal->pool_internal)
352  return AVERROR(ENOMEM);
353 
354  frames_hwctx->surfaces = s->surfaces_internal;
355  frames_hwctx->nb_surfaces = ctx->initial_pool_size;
356 
357  return 0;
358 }
359 
360 static mfxStatus frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
361  mfxFrameAllocResponse *resp)
362 {
363  AVHWFramesContext *ctx = pthis;
364  QSVFramesContext *s = ctx->internal->priv;
365  AVQSVFramesContext *hwctx = ctx->hwctx;
366  mfxFrameInfo *i = &req->Info;
367  mfxFrameInfo *i1 = &hwctx->surfaces[0].Info;
368 
369  if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET) ||
370  !(req->Type & (MFX_MEMTYPE_FROM_VPPIN | MFX_MEMTYPE_FROM_VPPOUT)) ||
371  !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
372  return MFX_ERR_UNSUPPORTED;
373  if (i->Width != i1->Width || i->Height != i1->Height ||
374  i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
375  av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
376  "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
377  i->Width, i->Height, i->FourCC, i->ChromaFormat,
378  i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
379  return MFX_ERR_UNSUPPORTED;
380  }
381 
382  resp->mids = s->mem_ids;
383  resp->NumFrameActual = hwctx->nb_surfaces;
384 
385  return MFX_ERR_NONE;
386 }
387 
388 static mfxStatus frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
389 {
390  return MFX_ERR_NONE;
391 }
392 
393 static mfxStatus frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
394 {
395  return MFX_ERR_UNSUPPORTED;
396 }
397 
398 static mfxStatus frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
399 {
400  return MFX_ERR_UNSUPPORTED;
401 }
402 
403 static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
404 {
405  *hdl = mid;
406  return MFX_ERR_NONE;
407 }
408 
410  mfxSession *session, int upload)
411 {
412  QSVFramesContext *s = ctx->internal->priv;
413  AVQSVFramesContext *frames_hwctx = ctx->hwctx;
414  QSVDeviceContext *device_priv = ctx->device_ctx->internal->priv;
415  int opaque = !!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
416 
417  mfxFrameAllocator frame_allocator = {
418  .pthis = ctx,
419  .Alloc = frame_alloc,
420  .Lock = frame_lock,
421  .Unlock = frame_unlock,
422  .GetHDL = frame_get_hdl,
423  .Free = frame_free,
424  };
425 
426  mfxVideoParam par;
427  mfxStatus err;
428 
429  err = MFXInit(device_priv->impl, &device_priv->ver, session);
430  if (err != MFX_ERR_NONE) {
431  av_log(ctx, AV_LOG_ERROR, "Error initializing an internal session\n");
432  return AVERROR_UNKNOWN;
433  }
434 
435  if (device_priv->handle) {
436  err = MFXVideoCORE_SetHandle(*session, device_priv->handle_type,
437  device_priv->handle);
438  if (err != MFX_ERR_NONE)
439  return AVERROR_UNKNOWN;
440  }
441 
442  if (!opaque) {
443  err = MFXVideoCORE_SetFrameAllocator(*session, &frame_allocator);
444  if (err != MFX_ERR_NONE)
445  return AVERROR_UNKNOWN;
446  }
447 
448  memset(&par, 0, sizeof(par));
449 
450  if (opaque) {
451  par.ExtParam = s->ext_buffers;
452  par.NumExtParam = FF_ARRAY_ELEMS(s->ext_buffers);
453  par.IOPattern = upload ? MFX_IOPATTERN_OUT_OPAQUE_MEMORY :
454  MFX_IOPATTERN_IN_OPAQUE_MEMORY;
455  } else {
456  par.IOPattern = upload ? MFX_IOPATTERN_OUT_VIDEO_MEMORY :
457  MFX_IOPATTERN_IN_VIDEO_MEMORY;
458  }
459 
460  par.IOPattern |= upload ? MFX_IOPATTERN_IN_SYSTEM_MEMORY :
461  MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
462  par.AsyncDepth = 1;
463 
464  par.vpp.In = frames_hwctx->surfaces[0].Info;
465 
466  /* Apparently VPP requires the frame rate to be set to some value, otherwise
467  * init will fail (probably for the framerate conversion filter). Since we
468  * are only doing data upload/download here, we just invent an arbitrary
469  * value */
470  par.vpp.In.FrameRateExtN = 25;
471  par.vpp.In.FrameRateExtD = 1;
472  par.vpp.Out = par.vpp.In;
473 
474  err = MFXVideoVPP_Init(*session, &par);
475  if (err != MFX_ERR_NONE) {
476  av_log(ctx, AV_LOG_VERBOSE, "Error opening the internal VPP session."
477  "Surface upload/download will not be possible\n");
478  MFXClose(*session);
479  *session = NULL;
480  }
481 
482  return 0;
483 }
484 
486 {
487  QSVFramesContext *s = ctx->internal->priv;
488  AVQSVFramesContext *frames_hwctx = ctx->hwctx;
489 
490  int opaque = !!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
491 
492  uint32_t fourcc;
493  int i, ret;
494 
495  fourcc = qsv_fourcc_from_pix_fmt(ctx->sw_format);
496  if (!fourcc) {
497  av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format\n");
498  return AVERROR(ENOSYS);
499  }
500 
501  if (!ctx->pool) {
502  ret = qsv_init_pool(ctx, fourcc);
503  if (ret < 0) {
504  av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
505  return ret;
506  }
507  }
508 
509  if (opaque) {
510  s->surface_ptrs = av_mallocz_array(frames_hwctx->nb_surfaces,
511  sizeof(*s->surface_ptrs));
512  if (!s->surface_ptrs)
513  return AVERROR(ENOMEM);
514 
515  for (i = 0; i < frames_hwctx->nb_surfaces; i++)
516  s->surface_ptrs[i] = frames_hwctx->surfaces + i;
517 
518  s->opaque_alloc.In.Surfaces = s->surface_ptrs;
519  s->opaque_alloc.In.NumSurface = frames_hwctx->nb_surfaces;
520  s->opaque_alloc.In.Type = frames_hwctx->frame_type;
521 
522  s->opaque_alloc.Out = s->opaque_alloc.In;
523 
524  s->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
525  s->opaque_alloc.Header.BufferSz = sizeof(s->opaque_alloc);
526 
527  s->ext_buffers[0] = (mfxExtBuffer*)&s->opaque_alloc;
528  } else {
529  s->mem_ids = av_mallocz_array(frames_hwctx->nb_surfaces, sizeof(*s->mem_ids));
530  if (!s->mem_ids)
531  return AVERROR(ENOMEM);
532 
533  for (i = 0; i < frames_hwctx->nb_surfaces; i++)
534  s->mem_ids[i] = frames_hwctx->surfaces[i].Data.MemId;
535  }
536 
537  ret = qsv_init_internal_session(ctx, &s->session_download, 0);
538  if (ret < 0)
539  return ret;
540 
541  ret = qsv_init_internal_session(ctx, &s->session_upload, 1);
542  if (ret < 0)
543  return ret;
544 
545  return 0;
546 }
547 
549 {
550  frame->buf[0] = av_buffer_pool_get(ctx->pool);
551  if (!frame->buf[0])
552  return AVERROR(ENOMEM);
553 
554  frame->data[3] = frame->buf[0]->data;
555  frame->format = AV_PIX_FMT_QSV;
556  frame->width = ctx->width;
557  frame->height = ctx->height;
558 
559  return 0;
560 }
561 
564  enum AVPixelFormat **formats)
565 {
566  enum AVPixelFormat *fmts;
567 
568  fmts = av_malloc_array(2, sizeof(*fmts));
569  if (!fmts)
570  return AVERROR(ENOMEM);
571 
572  fmts[0] = ctx->sw_format;
573  fmts[1] = AV_PIX_FMT_NONE;
574 
575  *formats = fmts;
576 
577  return 0;
578 }
579 
581  AVHWFramesContext *src_ctx, int flags)
582 {
583  AVQSVFramesContext *src_hwctx = src_ctx->hwctx;
584  int i;
585 
586  switch (dst_ctx->device_ctx->type) {
587 #if CONFIG_VAAPI
589  {
590  AVVAAPIFramesContext *dst_hwctx = dst_ctx->hwctx;
591  dst_hwctx->surface_ids = av_mallocz_array(src_hwctx->nb_surfaces,
592  sizeof(*dst_hwctx->surface_ids));
593  if (!dst_hwctx->surface_ids)
594  return AVERROR(ENOMEM);
595  for (i = 0; i < src_hwctx->nb_surfaces; i++)
596  dst_hwctx->surface_ids[i] =
597  *(VASurfaceID*)src_hwctx->surfaces[i].Data.MemId;
598  dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
599  }
600  break;
601 #endif
602 #if CONFIG_DXVA2
604  {
605  AVDXVA2FramesContext *dst_hwctx = dst_ctx->hwctx;
606  dst_hwctx->surfaces = av_mallocz_array(src_hwctx->nb_surfaces,
607  sizeof(*dst_hwctx->surfaces));
608  if (!dst_hwctx->surfaces)
609  return AVERROR(ENOMEM);
610  for (i = 0; i < src_hwctx->nb_surfaces; i++)
611  dst_hwctx->surfaces[i] =
612  (IDirect3DSurface9*)src_hwctx->surfaces[i].Data.MemId;
613  dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
614  if (src_hwctx->frame_type == MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
615  dst_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
616  else
617  dst_hwctx->surface_type = DXVA2_VideoProcessorRenderTarget;
618  }
619  break;
620 #endif
621  default:
622  return AVERROR(ENOSYS);
623  }
624 
625  return 0;
626 }
627 
629  AVFrame *dst, const AVFrame *src, int flags)
630 {
631  QSVFramesContext *s = ctx->internal->priv;
632  mfxFrameSurface1 *surf = (mfxFrameSurface1*)src->data[3];
633  AVHWFramesContext *child_frames_ctx;
634  const AVPixFmtDescriptor *desc;
635  uint8_t *child_data;
636  AVFrame *dummy;
637  int ret = 0;
638 
639  if (!s->child_frames_ref)
640  return AVERROR(ENOSYS);
641  child_frames_ctx = (AVHWFramesContext*)s->child_frames_ref->data;
642 
643  switch (child_frames_ctx->device_ctx->type) {
644 #if CONFIG_VAAPI
646  child_data = (uint8_t*)(intptr_t)*(VASurfaceID*)surf->Data.MemId;
647  break;
648 #endif
649 #if CONFIG_DXVA2
651  child_data = surf->Data.MemId;
652  break;
653 #endif
654  default:
655  return AVERROR(ENOSYS);
656  }
657 
658  if (dst->format == child_frames_ctx->format) {
660  dst, src, NULL, NULL);
661  if (ret < 0)
662  return ret;
663 
664  dst->width = src->width;
665  dst->height = src->height;
666  dst->data[3] = child_data;
667 
668  return 0;
669  }
670 
672  if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
673  // This only supports mapping to software.
674  return AVERROR(ENOSYS);
675  }
676 
677  dummy = av_frame_alloc();
678  if (!dummy)
679  return AVERROR(ENOMEM);
680 
681  dummy->buf[0] = av_buffer_ref(src->buf[0]);
682  dummy->hw_frames_ctx = av_buffer_ref(s->child_frames_ref);
683  if (!dummy->buf[0] || !dummy->hw_frames_ctx)
684  goto fail;
685 
686  dummy->format = child_frames_ctx->format;
687  dummy->width = src->width;
688  dummy->height = src->height;
689  dummy->data[3] = child_data;
690 
691  ret = av_hwframe_map(dst, dummy, flags);
692 
693 fail:
695 
696  return ret;
697 }
698 
700  const AVFrame *src)
701 {
702  QSVFramesContext *s = ctx->internal->priv;
703  AVHWFramesContext *child_frames_ctx = (AVHWFramesContext*)s->child_frames_ref->data;
704  int download = !!src->hw_frames_ctx;
705  mfxFrameSurface1 *surf = (mfxFrameSurface1*)(download ? src->data[3] : dst->data[3]);
706 
707  AVFrame *dummy;
708  int ret;
709 
710  dummy = av_frame_alloc();
711  if (!dummy)
712  return AVERROR(ENOMEM);
713 
714  dummy->format = child_frames_ctx->format;
715  dummy->width = src->width;
716  dummy->height = src->height;
717  dummy->buf[0] = download ? src->buf[0] : dst->buf[0];
718  dummy->data[3] = surf->Data.MemId;
719  dummy->hw_frames_ctx = s->child_frames_ref;
720 
721  ret = download ? av_hwframe_transfer_data(dst, dummy, 0) :
722  av_hwframe_transfer_data(dummy, src, 0);
723 
724  dummy->buf[0] = NULL;
725  dummy->data[3] = NULL;
726  dummy->hw_frames_ctx = NULL;
727 
728  av_frame_free(&dummy);
729 
730  return ret;
731 }
732 
734  const AVFrame *src)
735 {
736  QSVFramesContext *s = ctx->internal->priv;
737  mfxFrameSurface1 out = {{ 0 }};
738  mfxFrameSurface1 *in = (mfxFrameSurface1*)src->data[3];
739 
740  mfxSyncPoint sync = NULL;
741  mfxStatus err;
742 
743  if (!s->session_download) {
744  if (s->child_frames_ref)
745  return qsv_transfer_data_child(ctx, dst, src);
746 
747  av_log(ctx, AV_LOG_ERROR, "Surface download not possible\n");
748  return AVERROR(ENOSYS);
749  }
750 
751  out.Info = in->Info;
752  out.Data.PitchLow = dst->linesize[0];
753  out.Data.Y = dst->data[0];
754  out.Data.U = dst->data[1];
755  out.Data.V = dst->data[2];
756  out.Data.A = dst->data[3];
757 
758  do {
759  err = MFXVideoVPP_RunFrameVPPAsync(s->session_download, in, &out, NULL, &sync);
760  if (err == MFX_WRN_DEVICE_BUSY)
761  av_usleep(1);
762  } while (err == MFX_WRN_DEVICE_BUSY);
763 
764  if (err < 0 || !sync) {
765  av_log(ctx, AV_LOG_ERROR, "Error downloading the surface\n");
766  return AVERROR_UNKNOWN;
767  }
768 
769  do {
770  err = MFXVideoCORE_SyncOperation(s->session_download, sync, 1000);
771  } while (err == MFX_WRN_IN_EXECUTION);
772  if (err < 0) {
773  av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation: %d\n", err);
774  return AVERROR_UNKNOWN;
775  }
776 
777  return 0;
778 }
779 
781  const AVFrame *src)
782 {
783  QSVFramesContext *s = ctx->internal->priv;
784  mfxFrameSurface1 in = {{ 0 }};
785  mfxFrameSurface1 *out = (mfxFrameSurface1*)dst->data[3];
786 
787  mfxSyncPoint sync = NULL;
788  mfxStatus err;
789 
790  if (!s->session_upload) {
791  if (s->child_frames_ref)
792  return qsv_transfer_data_child(ctx, dst, src);
793 
794  av_log(ctx, AV_LOG_ERROR, "Surface upload not possible\n");
795  return AVERROR(ENOSYS);
796  }
797 
798  in.Info = out->Info;
799  in.Data.PitchLow = src->linesize[0];
800  in.Data.Y = src->data[0];
801  in.Data.U = src->data[1];
802  in.Data.V = src->data[2];
803  in.Data.A = src->data[3];
804 
805  do {
806  err = MFXVideoVPP_RunFrameVPPAsync(s->session_upload, &in, out, NULL, &sync);
807  if (err == MFX_WRN_DEVICE_BUSY)
808  av_usleep(1);
809  } while (err == MFX_WRN_DEVICE_BUSY);
810 
811  if (err < 0 || !sync) {
812  av_log(ctx, AV_LOG_ERROR, "Error uploading the surface\n");
813  return AVERROR_UNKNOWN;
814  }
815 
816  do {
817  err = MFXVideoCORE_SyncOperation(s->session_upload, sync, 1000);
818  } while (err == MFX_WRN_IN_EXECUTION);
819  if (err < 0) {
820  av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation\n");
821  return AVERROR_UNKNOWN;
822  }
823 
824  return 0;
825 }
826 
828  AVHWFramesContext *src_ctx, int flags)
829 {
830  QSVFramesContext *s = dst_ctx->internal->priv;
831  AVQSVFramesContext *dst_hwctx = dst_ctx->hwctx;
832  int i;
833 
834  switch (src_ctx->device_ctx->type) {
835 #if CONFIG_VAAPI
837  {
838  AVVAAPIFramesContext *src_hwctx = src_ctx->hwctx;
840  sizeof(*s->surfaces_internal));
841  if (!s->surfaces_internal)
842  return AVERROR(ENOMEM);
843  for (i = 0; i < src_hwctx->nb_surfaces; i++) {
844  qsv_init_surface(dst_ctx, &s->surfaces_internal[i]);
845  s->surfaces_internal[i].Data.MemId = src_hwctx->surface_ids + i;
846  }
847  dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
848  dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
849  }
850  break;
851 #endif
852 #if CONFIG_DXVA2
854  {
855  AVDXVA2FramesContext *src_hwctx = src_ctx->hwctx;
857  sizeof(*s->surfaces_internal));
858  if (!s->surfaces_internal)
859  return AVERROR(ENOMEM);
860  for (i = 0; i < src_hwctx->nb_surfaces; i++) {
861  qsv_init_surface(dst_ctx, &s->surfaces_internal[i]);
862  s->surfaces_internal[i].Data.MemId = (mfxMemId)src_hwctx->surfaces[i];
863  }
864  dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
865  if (src_hwctx->surface_type == DXVA2_VideoProcessorRenderTarget)
866  dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
867  else
868  dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
869  }
870  break;
871 #endif
872  default:
873  return AVERROR(ENOSYS);
874  }
875 
876  dst_hwctx->surfaces = s->surfaces_internal;
877 
878  return 0;
879 }
880 
881 static int qsv_map_to(AVHWFramesContext *dst_ctx,
882  AVFrame *dst, const AVFrame *src, int flags)
883 {
884  AVQSVFramesContext *hwctx = dst_ctx->hwctx;
885  int i, err;
886 
887  for (i = 0; i < hwctx->nb_surfaces; i++) {
888 #if CONFIG_VAAPI
889  if (*(VASurfaceID*)hwctx->surfaces[i].Data.MemId ==
890  (VASurfaceID)(uintptr_t)src->data[3])
891  break;
892 #endif
893 #if CONFIG_DXVA2
894  if ((IDirect3DSurface9*)hwctx->surfaces[i].Data.MemId ==
895  (IDirect3DSurface9*)(uintptr_t)src->data[3])
896  break;
897 #endif
898  }
899  if (i >= hwctx->nb_surfaces) {
900  av_log(dst_ctx, AV_LOG_ERROR, "Trying to map from a surface which "
901  "is not in the mapped frames context.\n");
902  return AVERROR(EINVAL);
903  }
904 
906  dst, src, NULL, NULL);
907  if (err)
908  return err;
909 
910  dst->width = src->width;
911  dst->height = src->height;
912  dst->data[3] = (uint8_t*)&hwctx->surfaces[i];
913 
914  return 0;
915 }
916 
918  const void *hwconfig,
919  AVHWFramesConstraints *constraints)
920 {
921  int i;
922 
924  sizeof(*constraints->valid_sw_formats));
925  if (!constraints->valid_sw_formats)
926  return AVERROR(ENOMEM);
927 
928  for (i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++)
929  constraints->valid_sw_formats[i] = supported_pixel_formats[i].pix_fmt;
930  constraints->valid_sw_formats[FF_ARRAY_ELEMS(supported_pixel_formats)] = AV_PIX_FMT_NONE;
931 
932  constraints->valid_hw_formats = av_malloc_array(2, sizeof(*constraints->valid_hw_formats));
933  if (!constraints->valid_hw_formats)
934  return AVERROR(ENOMEM);
935 
936  constraints->valid_hw_formats[0] = AV_PIX_FMT_QSV;
937  constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
938 
939  return 0;
940 }
941 
943 {
944  AVQSVDeviceContext *hwctx = ctx->hwctx;
945  QSVDevicePriv *priv = ctx->user_opaque;
946 
947  if (hwctx->session)
948  MFXClose(hwctx->session);
949 
951  av_freep(&priv);
952 }
953 
954 static mfxIMPL choose_implementation(const char *device)
955 {
956  static const struct {
957  const char *name;
958  mfxIMPL impl;
959  } impl_map[] = {
960  { "auto", MFX_IMPL_AUTO },
961  { "sw", MFX_IMPL_SOFTWARE },
962  { "hw", MFX_IMPL_HARDWARE },
963  { "auto_any", MFX_IMPL_AUTO_ANY },
964  { "hw_any", MFX_IMPL_HARDWARE_ANY },
965  { "hw2", MFX_IMPL_HARDWARE2 },
966  { "hw3", MFX_IMPL_HARDWARE3 },
967  { "hw4", MFX_IMPL_HARDWARE4 },
968  };
969 
970  mfxIMPL impl = MFX_IMPL_AUTO_ANY;
971  int i;
972 
973  if (device) {
974  for (i = 0; i < FF_ARRAY_ELEMS(impl_map); i++)
975  if (!strcmp(device, impl_map[i].name)) {
976  impl = impl_map[i].impl;
977  break;
978  }
979  if (i == FF_ARRAY_ELEMS(impl_map))
980  impl = strtol(device, NULL, 0);
981  }
982 
983  return impl;
984 }
985 
987  mfxIMPL implementation,
988  AVHWDeviceContext *child_device_ctx,
989  int flags)
990 {
991  AVQSVDeviceContext *hwctx = ctx->hwctx;
992  QSVDeviceContext *s = ctx->internal->priv;
993 
994  mfxVersion ver = { { 3, 1 } };
995  mfxHDL handle;
996  mfxHandleType handle_type;
997  mfxStatus err;
998  int ret;
999 
1000  switch (child_device_ctx->type) {
1001 #if CONFIG_VAAPI
1003  {
1004  AVVAAPIDeviceContext *child_device_hwctx = child_device_ctx->hwctx;
1005  handle_type = MFX_HANDLE_VA_DISPLAY;
1006  handle = (mfxHDL)child_device_hwctx->display;
1007  }
1008  break;
1009 #endif
1010 #if CONFIG_DXVA2
1012  {
1013  AVDXVA2DeviceContext *child_device_hwctx = child_device_ctx->hwctx;
1014  handle_type = MFX_HANDLE_D3D9_DEVICE_MANAGER;
1015  handle = (mfxHDL)child_device_hwctx->devmgr;
1016  }
1017  break;
1018 #endif
1019  default:
1020  ret = AVERROR(ENOSYS);
1021  goto fail;
1022  }
1023 
1024  err = MFXInit(implementation, &ver, &hwctx->session);
1025  if (err != MFX_ERR_NONE) {
1026  av_log(ctx, AV_LOG_ERROR, "Error initializing an MFX session: "
1027  "%d.\n", err);
1028  ret = AVERROR_UNKNOWN;
1029  goto fail;
1030  }
1031 
1032  err = MFXVideoCORE_SetHandle(hwctx->session, handle_type, handle);
1033  if (err != MFX_ERR_NONE) {
1034  av_log(ctx, AV_LOG_ERROR, "Error setting child device handle: "
1035  "%d\n", err);
1036  ret = AVERROR_UNKNOWN;
1037  goto fail;
1038  }
1039 
1040  return 0;
1041 
1042 fail:
1043  if (hwctx->session)
1044  MFXClose(hwctx->session);
1045  return ret;
1046 }
1047 
1049  AVHWDeviceContext *child_device_ctx, int flags)
1050 {
1051  return qsv_device_derive_from_child(ctx, MFX_IMPL_HARDWARE_ANY,
1052  child_device_ctx, flags);
1053 }
1054 
1055 static int qsv_device_create(AVHWDeviceContext *ctx, const char *device,
1056  AVDictionary *opts, int flags)
1057 {
1058  QSVDevicePriv *priv;
1059  enum AVHWDeviceType child_device_type;
1060  AVHWDeviceContext *child_device;
1061  AVDictionaryEntry *e;
1062 
1063  mfxIMPL impl;
1064  int ret;
1065 
1066  priv = av_mallocz(sizeof(*priv));
1067  if (!priv)
1068  return AVERROR(ENOMEM);
1069 
1070  ctx->user_opaque = priv;
1071  ctx->free = qsv_device_free;
1072 
1073  e = av_dict_get(opts, "child_device", NULL, 0);
1074 
1075  if (CONFIG_VAAPI)
1076  child_device_type = AV_HWDEVICE_TYPE_VAAPI;
1077  else if (CONFIG_DXVA2)
1078  child_device_type = AV_HWDEVICE_TYPE_DXVA2;
1079  else {
1080  av_log(ctx, AV_LOG_ERROR, "No supported child device type is enabled\n");
1081  return AVERROR(ENOSYS);
1082  }
1083 
1084  ret = av_hwdevice_ctx_create(&priv->child_device_ctx, child_device_type,
1085  e ? e->value : NULL, NULL, 0);
1086  if (ret < 0)
1087  return ret;
1088 
1089  child_device = (AVHWDeviceContext*)priv->child_device_ctx->data;
1090 
1091  impl = choose_implementation(device);
1092 
1093  return qsv_device_derive_from_child(ctx, impl, child_device, 0);
1094 }
1095 
1098  .name = "QSV",
1099 
1100  .device_hwctx_size = sizeof(AVQSVDeviceContext),
1101  .device_priv_size = sizeof(QSVDeviceContext),
1102  .frames_hwctx_size = sizeof(AVQSVFramesContext),
1103  .frames_priv_size = sizeof(QSVFramesContext),
1104 
1105  .device_create = qsv_device_create,
1106  .device_derive = qsv_device_derive,
1107  .device_init = qsv_device_init,
1108  .frames_get_constraints = qsv_frames_get_constraints,
1109  .frames_init = qsv_frames_init,
1110  .frames_uninit = qsv_frames_uninit,
1111  .frames_get_buffer = qsv_get_buffer,
1112  .transfer_get_formats = qsv_transfer_get_formats,
1113  .transfer_data_to = qsv_transfer_data_to,
1114  .transfer_data_from = qsv_transfer_data_from,
1115  .map_to = qsv_map_to,
1116  .map_from = qsv_map_from,
1117  .frames_derive_to = qsv_frames_derive_to,
1118  .frames_derive_from = qsv_frames_derive_from,
1119 
1120  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_QSV, AV_PIX_FMT_NONE },
1121 };
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:58
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
uint32_t fourcc
Definition: hwcontext_qsv.c:90
static int qsv_init_child_ctx(AVHWFramesContext *ctx)
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:125
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2419
VAAPI-specific data associated with a frame pool.
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static int qsv_frames_get_constraints(AVHWDeviceContext *ctx, const void *hwconfig, AVHWFramesConstraints *constraints)
mfxFrameSurface1 * surfaces_internal
Definition: hwcontext_qsv.c:62
int frame_type
A combination of MFX_MEMTYPE_* describing the frame pool.
Definition: hwcontext_qsv.h:49
mfxExtBuffer * ext_buffers[1]
Definition: hwcontext_qsv.c:71
This struct is allocated as AVHWFramesContext.hwctx.
Memory handling functions.
mfxHandleType handle_type
Definition: hwcontext_qsv.c:75
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:393
This struct is allocated as AVHWFramesContext.hwctx.
Definition: hwcontext_qsv.h:42
const char * desc
Definition: nvenc.c:60
An API-specific header for AV_HWDEVICE_TYPE_DXVA2.
static const struct @246 supported_pixel_formats[]
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:226
mfxMemId * mem_ids
Definition: hwcontext_qsv.c:66
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:206
mfxHandleType handle_type
Definition: hwcontext_qsv.c:49
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:84
mfxVersion ver
Definition: hwcontext_qsv.c:50
#define src
Definition: vp8dsp.c:254
static mfxStatus frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req, mfxFrameAllocResponse *resp)
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
static int qsv_init_pool(AVHWFramesContext *ctx, uint32_t fourcc)
API-specific header for AV_HWDEVICE_TYPE_VAAPI.
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame...
Definition: frame.h:538
#define AV_PIX_FMT_P010
Definition: pixfmt.h:424
static int qsv_transfer_data_child(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src)
AVBufferPool * pool_internal
static int qsv_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src)
enum AVHWDeviceType type
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:73
DWORD surface_type
The surface type (e.g.
enum AVHWDeviceType child_device_type
Definition: hwcontext_qsv.c:53
static int qsv_map_from(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src, int flags)
static AVFrame * frame
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:89
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type, const char *device, AVDictionary *opts, int flags)
Open a device of the specified type and create an AVHWDeviceContext for it.
Definition: hwcontext.c:563
static int flags
Definition: log.c:57
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
static mfxStatus frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
ptrdiff_t size
Definition: opengl_enc.c:101
static const struct @245 supported_handle_types[]
static int qsv_frames_init(AVHWFramesContext *ctx)
#define av_log(a,...)
static int qsv_init_surface(AVHWFramesContext *ctx, mfxFrameSurface1 *surf)
This struct is allocated as AVHWDeviceContext.hwctx.
Definition: hwcontext_qsv.h:35
static int qsv_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
int width
Definition: frame.h:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
void(* free)(struct AVHWDeviceContext *ctx)
This field may be set by the caller before calling av_hwdevice_ctx_init().
Definition: hwcontext.h:101
#define AVERROR(e)
Definition: error.h:43
static int qsv_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:90
enum AVHWDeviceType device_type
Definition: hwcontext_qsv.c:76
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:140
static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
IDirect3DDeviceManager9 * devmgr
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:28
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:323
enum AVHWDeviceType type
This field identifies the underlying API used for hardware access.
Definition: hwcontext.h:76
static int qsv_frames_derive_to(AVHWFramesContext *dst_ctx, AVHWFramesContext *src_ctx, int flags)
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:229
#define fail()
Definition: checkasm.h:109
static int qsv_transfer_get_formats(AVHWFramesContext *ctx, enum AVHWFrameTransferDirection dir, enum AVPixelFormat **formats)
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
Definition: hwcontext.c:433
int initial_pool_size
Initial size of the frame pool.
Definition: hwcontext.h:196
AVDictionary * opts
Definition: movenc.c:50
static mfxStatus frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Definition: hwcontext.h:146
AVFormatContext * ctx
Definition: movenc.c:48
mfxFrameSurface1 ** surface_ptrs
Definition: hwcontext_qsv.c:68
int dummy
Definition: motion.c:64
mfxSession session_download
Definition: hwcontext_qsv.c:58
AVBufferPool * av_buffer_pool_init2(int size, void *opaque, AVBufferRef *(*alloc)(void *opaque, int size), void(*pool_free)(void *opaque))
Allocate and initialize a buffer pool with a more complex allocator.
Definition: buffer.c:218
#define FF_ARRAY_ELEMS(a)
VADisplay display
The VADisplay handle, to be filled by the user.
mfxSession session
Definition: hwcontext_qsv.h:36
static void qsv_frames_uninit(AVHWFramesContext *ctx)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:274
AVBufferRef * av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
Allocate an AVHWDeviceContext for a given hardware type.
Definition: hwcontext.c:129
static mfxStatus frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
const HWContextType ff_hwcontext_type_qsv
This struct describes the constraints on hardware frames attached to a given device with a hardware-s...
Definition: hwcontext.h:430
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:232
mfxFrameSurface1 * surfaces
Definition: hwcontext_qsv.h:43
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
uint8_t * data
The data buffer.
Definition: buffer.h:89
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
Definition: hwcontext.h:159
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
int av_hwdevice_ctx_init(AVBufferRef *ref)
Finalize the device context before use.
Definition: hwcontext.c:187
int ff_hwframe_map_create(AVBufferRef *hwframe_ref, AVFrame *dst, const AVFrame *src, void(*unmap)(AVHWFramesContext *ctx, HWMapDescriptor *hwmap), void *priv)
Definition: hwcontext.c:681
static int qsv_device_create(AVHWDeviceContext *ctx, const char *device, AVDictionary *opts, int flags)
static int qsv_init_internal_session(AVHWFramesContext *ctx, mfxSession *session, int upload)
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:121
HW acceleration through QSV, data[3] contains a pointer to the mfxFrameSurface1 structure.
Definition: pixfmt.h:236
static mfxIMPL choose_implementation(const char *device)
refcounted data buffer API
enum AVPixelFormat * valid_hw_formats
A list of possible values for format in the hw_frames_ctx, terminated by AV_PIX_FMT_NONE.
Definition: hwcontext.h:435
mfxExtOpaqueSurfaceAlloc opaque_alloc
Definition: hwcontext_qsv.c:70
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer. ...
Definition: pixfmt.h:148
AVHWFramesInternal * internal
Private data used internally by libavutil.
Definition: hwcontext.h:131
static AVBufferRef * qsv_pool_alloc(void *opaque, int size)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
void * user_opaque
Arbitrary user data, to be used e.g.
Definition: hwcontext.h:106
static int qsv_device_derive_from_child(AVHWDeviceContext *ctx, mfxIMPL implementation, AVHWDeviceContext *child_device_ctx, int flags)
A reference to a data buffer.
Definition: buffer.h:81
static void qsv_device_free(AVHWDeviceContext *ctx)
common internal and external API header
if(ret< 0)
Definition: vf_mcdeint.c:279
int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
Map a hardware frame.
Definition: hwcontext.c:733
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition: hwcontext.c:237
static int qsv_device_derive(AVHWDeviceContext *ctx, AVHWDeviceContext *child_device_ctx, int flags)
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
static int qsv_device_init(AVHWDeviceContext *ctx)
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
enum AVPixelFormat child_pix_fmt
Definition: hwcontext_qsv.c:54
IDirect3DSurface9 ** surfaces
The surface pool.
AVHWFrameTransferDirection
Definition: hwcontext.h:392
pixel format definitions
AVBufferPool * pool
A pool from which the frames are allocated by av_hwframe_get_buffer().
Definition: hwcontext.h:187
AVHWDeviceType
Definition: hwcontext.h:27
This struct is allocated as AVHWDeviceContext.hwctx.
char * value
Definition: dict.h:87
static uint32_t qsv_fourcc_from_pix_fmt(enum AVPixelFormat pix_fmt)
Definition: hwcontext_qsv.c:97
enum AVPixelFormat * valid_sw_formats
A list of possible values for sw_format in the hw_frames_ctx, terminated by AV_PIX_FMT_NONE.
Definition: hwcontext.h:442
VAAPI connection details.
static int qsv_frames_derive_from(AVHWFramesContext *dst_ctx, AVHWFramesContext *src_ctx, int flags)
int height
Definition: frame.h:259
FILE * out
Definition: movenc.c:54
#define av_freep(p)
VASurfaceID * surface_ids
The surfaces IDs of all surfaces in the pool after creation.
An API-specific header for AV_HWDEVICE_TYPE_QSV.
AVBufferRef * child_device_ctx
Definition: hwcontext_qsv.c:44
AVBufferRef * child_frames_ref
Definition: hwcontext_qsv.c:61
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
Definition: buffer.c:334
#define av_malloc_array(a, b)
formats
Definition: signature.h:48
AVHWDeviceInternal * internal
Private data used internally by libavutil.
Definition: hwcontext.h:68
int depth
Number of bits in the component.
Definition: pixdesc.h:58
static void qsv_pool_release_dummy(void *opaque, uint8_t *data)
static int qsv_map_to(AVHWFramesContext *dst_ctx, AVFrame *dst, const AVFrame *src, int flags)
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:219
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
enum AVPixelFormat pix_fmt
Definition: hwcontext_qsv.c:77
mfxSession session_upload
Definition: hwcontext_qsv.c:59
const char * name
Definition: opengl_enc.c:103