FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hwcontext.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 "config.h"
20 
21 #include "buffer.h"
22 #include "common.h"
23 #include "hwcontext.h"
24 #include "hwcontext_internal.h"
25 #include "imgutils.h"
26 #include "log.h"
27 #include "mem.h"
28 #include "pixdesc.h"
29 #include "pixfmt.h"
30 
31 static const HWContextType * const hw_table[] = {
32 #if CONFIG_CUDA
34 #endif
35 #if CONFIG_D3D11VA
37 #endif
38 #if CONFIG_LIBDRM
40 #endif
41 #if CONFIG_DXVA2
43 #endif
44 #if CONFIG_QSV
46 #endif
47 #if CONFIG_VAAPI
49 #endif
50 #if CONFIG_VDPAU
52 #endif
53 #if CONFIG_VIDEOTOOLBOX
55 #endif
56  NULL,
57 };
58 
59 static const char *const hw_type_names[] = {
60  [AV_HWDEVICE_TYPE_CUDA] = "cuda",
61  [AV_HWDEVICE_TYPE_DRM] = "drm",
62  [AV_HWDEVICE_TYPE_DXVA2] = "dxva2",
63  [AV_HWDEVICE_TYPE_D3D11VA] = "d3d11va",
64  [AV_HWDEVICE_TYPE_QSV] = "qsv",
65  [AV_HWDEVICE_TYPE_VAAPI] = "vaapi",
66  [AV_HWDEVICE_TYPE_VDPAU] = "vdpau",
67  [AV_HWDEVICE_TYPE_VIDEOTOOLBOX] = "videotoolbox",
68 };
69 
71 {
72  int type;
73  for (type = 0; type < FF_ARRAY_ELEMS(hw_type_names); type++) {
74  if (hw_type_names[type] && !strcmp(hw_type_names[type], name))
75  return type;
76  }
77  return AV_HWDEVICE_TYPE_NONE;
78 }
79 
81 {
82  if (type >= 0 && type < FF_ARRAY_ELEMS(hw_type_names))
83  return hw_type_names[type];
84  else
85  return NULL;
86 }
87 
89 {
90  enum AVHWDeviceType next;
91  int i, set = 0;
92  for (i = 0; hw_table[i]; i++) {
93  if (prev != AV_HWDEVICE_TYPE_NONE && hw_table[i]->type <= prev)
94  continue;
95  if (!set || hw_table[i]->type < next) {
96  next = hw_table[i]->type;
97  set = 1;
98  }
99  }
100  return set ? next : AV_HWDEVICE_TYPE_NONE;
101 }
102 
103 static const AVClass hwdevice_ctx_class = {
104  .class_name = "AVHWDeviceContext",
105  .item_name = av_default_item_name,
106  .version = LIBAVUTIL_VERSION_INT,
107 };
108 
109 static void hwdevice_ctx_free(void *opaque, uint8_t *data)
110 {
112 
113  /* uninit might still want access the hw context and the user
114  * free() callback might destroy it, so uninit has to be called first */
115  if (ctx->internal->hw_type->device_uninit)
116  ctx->internal->hw_type->device_uninit(ctx);
117 
118  if (ctx->free)
119  ctx->free(ctx);
120 
122 
123  av_freep(&ctx->hwctx);
124  av_freep(&ctx->internal->priv);
125  av_freep(&ctx->internal);
126  av_freep(&ctx);
127 }
128 
130 {
132  AVBufferRef *buf;
133  const HWContextType *hw_type = NULL;
134  int i;
135 
136  for (i = 0; hw_table[i]; i++) {
137  if (hw_table[i]->type == type) {
138  hw_type = hw_table[i];
139  break;
140  }
141  }
142  if (!hw_type)
143  return NULL;
144 
145  ctx = av_mallocz(sizeof(*ctx));
146  if (!ctx)
147  return NULL;
148 
149  ctx->internal = av_mallocz(sizeof(*ctx->internal));
150  if (!ctx->internal)
151  goto fail;
152 
153  if (hw_type->device_priv_size) {
154  ctx->internal->priv = av_mallocz(hw_type->device_priv_size);
155  if (!ctx->internal->priv)
156  goto fail;
157  }
158 
159  if (hw_type->device_hwctx_size) {
160  ctx->hwctx = av_mallocz(hw_type->device_hwctx_size);
161  if (!ctx->hwctx)
162  goto fail;
163  }
164 
165  buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
168  if (!buf)
169  goto fail;
170 
171  ctx->type = type;
173 
174  ctx->internal->hw_type = hw_type;
175 
176  return buf;
177 
178 fail:
179  if (ctx->internal)
180  av_freep(&ctx->internal->priv);
181  av_freep(&ctx->internal);
182  av_freep(&ctx->hwctx);
183  av_freep(&ctx);
184  return NULL;
185 }
186 
188 {
190  int ret;
191 
192  if (ctx->internal->hw_type->device_init) {
193  ret = ctx->internal->hw_type->device_init(ctx);
194  if (ret < 0)
195  goto fail;
196  }
197 
198  return 0;
199 fail:
200  if (ctx->internal->hw_type->device_uninit)
201  ctx->internal->hw_type->device_uninit(ctx);
202  return ret;
203 }
204 
205 static const AVClass hwframe_ctx_class = {
206  .class_name = "AVHWFramesContext",
207  .item_name = av_default_item_name,
208  .version = LIBAVUTIL_VERSION_INT,
209 };
210 
211 static void hwframe_ctx_free(void *opaque, uint8_t *data)
212 {
214 
215  if (ctx->internal->source_frames) {
217 
218  } else {
219  if (ctx->internal->pool_internal)
221 
222  if (ctx->internal->hw_type->frames_uninit)
223  ctx->internal->hw_type->frames_uninit(ctx);
224 
225  if (ctx->free)
226  ctx->free(ctx);
227  }
228 
230 
231  av_freep(&ctx->hwctx);
232  av_freep(&ctx->internal->priv);
233  av_freep(&ctx->internal);
234  av_freep(&ctx);
235 }
236 
238 {
239  AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref_in->data;
240  const HWContextType *hw_type = device_ctx->internal->hw_type;
242  AVBufferRef *buf, *device_ref = NULL;
243 
244  ctx = av_mallocz(sizeof(*ctx));
245  if (!ctx)
246  return NULL;
247 
248  ctx->internal = av_mallocz(sizeof(*ctx->internal));
249  if (!ctx->internal)
250  goto fail;
251 
252  if (hw_type->frames_priv_size) {
253  ctx->internal->priv = av_mallocz(hw_type->frames_priv_size);
254  if (!ctx->internal->priv)
255  goto fail;
256  }
257 
258  if (hw_type->frames_hwctx_size) {
259  ctx->hwctx = av_mallocz(hw_type->frames_hwctx_size);
260  if (!ctx->hwctx)
261  goto fail;
262  }
263 
264  device_ref = av_buffer_ref(device_ref_in);
265  if (!device_ref)
266  goto fail;
267 
268  buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
271  if (!buf)
272  goto fail;
273 
275  ctx->device_ref = device_ref;
276  ctx->device_ctx = device_ctx;
277  ctx->format = AV_PIX_FMT_NONE;
278  ctx->sw_format = AV_PIX_FMT_NONE;
279 
280  ctx->internal->hw_type = hw_type;
281 
282  return buf;
283 
284 fail:
285  if (device_ref)
286  av_buffer_unref(&device_ref);
287  if (ctx->internal)
288  av_freep(&ctx->internal->priv);
289  av_freep(&ctx->internal);
290  av_freep(&ctx->hwctx);
291  av_freep(&ctx);
292  return NULL;
293 }
294 
296 {
298  AVFrame **frames;
299  int i, ret = 0;
300 
302  if (!frames)
303  return AVERROR(ENOMEM);
304 
305  for (i = 0; i < ctx->initial_pool_size; i++) {
306  frames[i] = av_frame_alloc();
307  if (!frames[i])
308  goto fail;
309 
310  ret = av_hwframe_get_buffer(ref, frames[i], 0);
311  if (ret < 0)
312  goto fail;
313  }
314 
315 fail:
316  for (i = 0; i < ctx->initial_pool_size; i++)
317  av_frame_free(&frames[i]);
318  av_freep(&frames);
319 
320  return ret;
321 }
322 
324 {
326  const enum AVPixelFormat *pix_fmt;
327  int ret;
328 
329  if (ctx->internal->source_frames) {
330  /* A derived frame context is already initialised. */
331  return 0;
332  }
333 
334  /* validate the pixel format */
336  if (*pix_fmt == ctx->format)
337  break;
338  }
339  if (*pix_fmt == AV_PIX_FMT_NONE) {
340  av_log(ctx, AV_LOG_ERROR,
341  "The hardware pixel format '%s' is not supported by the device type '%s'\n",
343  return AVERROR(ENOSYS);
344  }
345 
346  /* validate the dimensions */
347  ret = av_image_check_size(ctx->width, ctx->height, 0, ctx);
348  if (ret < 0)
349  return ret;
350 
351  /* format-specific init */
352  if (ctx->internal->hw_type->frames_init) {
353  ret = ctx->internal->hw_type->frames_init(ctx);
354  if (ret < 0)
355  goto fail;
356  }
357 
358  if (ctx->internal->pool_internal && !ctx->pool)
359  ctx->pool = ctx->internal->pool_internal;
360 
361  /* preallocate the frames in the pool, if requested */
362  if (ctx->initial_pool_size > 0) {
363  ret = hwframe_pool_prealloc(ref);
364  if (ret < 0)
365  goto fail;
366  }
367 
368  return 0;
369 fail:
370  if (ctx->internal->hw_type->frames_uninit)
371  ctx->internal->hw_type->frames_uninit(ctx);
372  return ret;
373 }
374 
377  enum AVPixelFormat **formats, int flags)
378 {
379  AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
380 
382  return AVERROR(ENOSYS);
383 
384  return ctx->internal->hw_type->transfer_get_formats(ctx, dir, formats);
385 }
386 
387 static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
388 {
390  AVFrame *frame_tmp;
391  int ret = 0;
392 
393  frame_tmp = av_frame_alloc();
394  if (!frame_tmp)
395  return AVERROR(ENOMEM);
396 
397  /* if the format is set, use that
398  * otherwise pick the first supported one */
399  if (dst->format >= 0) {
400  frame_tmp->format = dst->format;
401  } else {
402  enum AVPixelFormat *formats;
403 
406  &formats, 0);
407  if (ret < 0)
408  goto fail;
409  frame_tmp->format = formats[0];
410  av_freep(&formats);
411  }
412  frame_tmp->width = ctx->width;
413  frame_tmp->height = ctx->height;
414 
415  ret = av_frame_get_buffer(frame_tmp, 32);
416  if (ret < 0)
417  goto fail;
418 
419  ret = av_hwframe_transfer_data(frame_tmp, src, flags);
420  if (ret < 0)
421  goto fail;
422 
423  frame_tmp->width = src->width;
424  frame_tmp->height = src->height;
425 
426  av_frame_move_ref(dst, frame_tmp);
427 
428 fail:
429  av_frame_free(&frame_tmp);
430  return ret;
431 }
432 
434 {
436  int ret;
437 
438  if (!dst->buf[0])
439  return transfer_data_alloc(dst, src, flags);
440 
441  if (src->hw_frames_ctx) {
442  ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
443 
444  ret = ctx->internal->hw_type->transfer_data_from(ctx, dst, src);
445  if (ret < 0)
446  return ret;
447  } else if (dst->hw_frames_ctx) {
448  ctx = (AVHWFramesContext*)dst->hw_frames_ctx->data;
449 
450  ret = ctx->internal->hw_type->transfer_data_to(ctx, dst, src);
451  if (ret < 0)
452  return ret;
453  } else
454  return AVERROR(ENOSYS);
455 
456  return 0;
457 }
458 
460 {
461  AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
462  int ret;
463 
464  if (ctx->internal->source_frames) {
465  // This is a derived frame context, so we allocate in the source
466  // and map the frame immediately.
467  AVFrame *src_frame;
468 
469  frame->format = ctx->format;
470  frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
471  if (!frame->hw_frames_ctx)
472  return AVERROR(ENOMEM);
473 
474  src_frame = av_frame_alloc();
475  if (!src_frame)
476  return AVERROR(ENOMEM);
477 
479  src_frame, 0);
480  if (ret < 0)
481  return ret;
482 
483  ret = av_hwframe_map(frame, src_frame,
485  if (ret) {
486  av_log(ctx, AV_LOG_ERROR, "Failed to map frame into derived "
487  "frame context: %d.\n", ret);
488  av_frame_free(&src_frame);
489  return ret;
490  }
491 
492  // Free the source frame immediately - the mapped frame still
493  // contains a reference to it.
494  av_frame_free(&src_frame);
495 
496  return 0;
497  }
498 
499  if (!ctx->internal->hw_type->frames_get_buffer)
500  return AVERROR(ENOSYS);
501 
502  if (!ctx->pool)
503  return AVERROR(EINVAL);
504 
505  frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
506  if (!frame->hw_frames_ctx)
507  return AVERROR(ENOMEM);
508 
509  ret = ctx->internal->hw_type->frames_get_buffer(ctx, frame);
510  if (ret < 0) {
512  return ret;
513  }
514 
515  return 0;
516 }
517 
519 {
521  const HWContextType *hw_type = ctx->internal->hw_type;
522 
523  if (hw_type->device_hwconfig_size == 0)
524  return NULL;
525 
526  return av_mallocz(hw_type->device_hwconfig_size);
527 }
528 
530  const void *hwconfig)
531 {
533  const HWContextType *hw_type = ctx->internal->hw_type;
534  AVHWFramesConstraints *constraints;
535 
536  if (!hw_type->frames_get_constraints)
537  return NULL;
538 
539  constraints = av_mallocz(sizeof(*constraints));
540  if (!constraints)
541  return NULL;
542 
543  constraints->min_width = constraints->min_height = 0;
544  constraints->max_width = constraints->max_height = INT_MAX;
545 
546  if (hw_type->frames_get_constraints(ctx, hwconfig, constraints) >= 0) {
547  return constraints;
548  } else {
549  av_hwframe_constraints_free(&constraints);
550  return NULL;
551  }
552 }
553 
555 {
556  if (*constraints) {
557  av_freep(&(*constraints)->valid_hw_formats);
558  av_freep(&(*constraints)->valid_sw_formats);
559  }
560  av_freep(constraints);
561 }
562 
564  const char *device, AVDictionary *opts, int flags)
565 {
566  AVBufferRef *device_ref = NULL;
567  AVHWDeviceContext *device_ctx;
568  int ret = 0;
569 
570  device_ref = av_hwdevice_ctx_alloc(type);
571  if (!device_ref) {
572  ret = AVERROR(ENOMEM);
573  goto fail;
574  }
575  device_ctx = (AVHWDeviceContext*)device_ref->data;
576 
577  if (!device_ctx->internal->hw_type->device_create) {
578  ret = AVERROR(ENOSYS);
579  goto fail;
580  }
581 
582  ret = device_ctx->internal->hw_type->device_create(device_ctx, device,
583  opts, flags);
584  if (ret < 0)
585  goto fail;
586 
587  ret = av_hwdevice_ctx_init(device_ref);
588  if (ret < 0)
589  goto fail;
590 
591  *pdevice_ref = device_ref;
592  return 0;
593 fail:
594  av_buffer_unref(&device_ref);
595  *pdevice_ref = NULL;
596  return ret;
597 }
598 
600  enum AVHWDeviceType type,
601  AVBufferRef *src_ref, int flags)
602 {
603  AVBufferRef *dst_ref = NULL, *tmp_ref;
604  AVHWDeviceContext *dst_ctx, *tmp_ctx;
605  int ret = 0;
606 
607  tmp_ref = src_ref;
608  while (tmp_ref) {
609  tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
610  if (tmp_ctx->type == type) {
611  dst_ref = av_buffer_ref(tmp_ref);
612  if (!dst_ref) {
613  ret = AVERROR(ENOMEM);
614  goto fail;
615  }
616  goto done;
617  }
618  tmp_ref = tmp_ctx->internal->source_device;
619  }
620 
621  dst_ref = av_hwdevice_ctx_alloc(type);
622  if (!dst_ref) {
623  ret = AVERROR(ENOMEM);
624  goto fail;
625  }
626  dst_ctx = (AVHWDeviceContext*)dst_ref->data;
627 
628  tmp_ref = src_ref;
629  while (tmp_ref) {
630  tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
631  if (dst_ctx->internal->hw_type->device_derive) {
632  ret = dst_ctx->internal->hw_type->device_derive(dst_ctx,
633  tmp_ctx,
634  flags);
635  if (ret == 0) {
636  dst_ctx->internal->source_device = av_buffer_ref(src_ref);
637  if (!dst_ctx->internal->source_device) {
638  ret = AVERROR(ENOMEM);
639  goto fail;
640  }
641  goto done;
642  }
643  if (ret != AVERROR(ENOSYS))
644  goto fail;
645  }
646  tmp_ref = tmp_ctx->internal->source_device;
647  }
648 
649  ret = AVERROR(ENOSYS);
650  goto fail;
651 
652 done:
653  ret = av_hwdevice_ctx_init(dst_ref);
654  if (ret < 0)
655  goto fail;
656 
657  *dst_ref_ptr = dst_ref;
658  return 0;
659 
660 fail:
661  av_buffer_unref(&dst_ref);
662  *dst_ref_ptr = NULL;
663  return ret;
664 }
665 
666 static void ff_hwframe_unmap(void *opaque, uint8_t *data)
667 {
668  HWMapDescriptor *hwmap = (HWMapDescriptor*)data;
669  AVHWFramesContext *ctx = opaque;
670 
671  if (hwmap->unmap)
672  hwmap->unmap(ctx, hwmap);
673 
674  av_frame_free(&hwmap->source);
675 
677 
678  av_free(hwmap);
679 }
680 
682  AVFrame *dst, const AVFrame *src,
683  void (*unmap)(AVHWFramesContext *ctx,
684  HWMapDescriptor *hwmap),
685  void *priv)
686 {
687  AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
688  HWMapDescriptor *hwmap;
689  int ret;
690 
691  hwmap = av_mallocz(sizeof(*hwmap));
692  if (!hwmap) {
693  ret = AVERROR(ENOMEM);
694  goto fail;
695  }
696 
697  hwmap->source = av_frame_alloc();
698  if (!hwmap->source) {
699  ret = AVERROR(ENOMEM);
700  goto fail;
701  }
702  ret = av_frame_ref(hwmap->source, src);
703  if (ret < 0)
704  goto fail;
705 
706  hwmap->hw_frames_ctx = av_buffer_ref(hwframe_ref);
707  if (!hwmap->hw_frames_ctx) {
708  ret = AVERROR(ENOMEM);
709  goto fail;
710  }
711 
712  hwmap->unmap = unmap;
713  hwmap->priv = priv;
714 
715  dst->buf[0] = av_buffer_create((uint8_t*)hwmap, sizeof(*hwmap),
716  &ff_hwframe_unmap, ctx, 0);
717  if (!dst->buf[0]) {
718  ret = AVERROR(ENOMEM);
719  goto fail;
720  }
721 
722  return 0;
723 
724 fail:
725  if (hwmap) {
727  av_frame_free(&hwmap->source);
728  }
729  av_free(hwmap);
730  return ret;
731 }
732 
733 int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
734 {
735  AVHWFramesContext *src_frames, *dst_frames;
736  HWMapDescriptor *hwmap;
737  int ret;
738 
739  if (src->hw_frames_ctx && dst->hw_frames_ctx) {
740  src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
741  dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
742 
743  if ((src_frames == dst_frames &&
744  src->format == dst_frames->sw_format &&
745  dst->format == dst_frames->format) ||
746  (src_frames->internal->source_frames &&
747  src_frames->internal->source_frames->data ==
748  (uint8_t*)dst_frames)) {
749  // This is an unmap operation. We don't need to directly
750  // do anything here other than fill in the original frame,
751  // because the real unmap will be invoked when the last
752  // reference to the mapped frame disappears.
753  if (!src->buf[0]) {
754  av_log(src_frames, AV_LOG_ERROR, "Invalid mapping "
755  "found when attempting unmap.\n");
756  return AVERROR(EINVAL);
757  }
758  hwmap = (HWMapDescriptor*)src->buf[0]->data;
759  av_frame_unref(dst);
760  return av_frame_ref(dst, hwmap->source);
761  }
762  }
763 
764  if (src->hw_frames_ctx) {
765  src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
766 
767  if (src_frames->format == src->format &&
768  src_frames->internal->hw_type->map_from) {
769  ret = src_frames->internal->hw_type->map_from(src_frames,
770  dst, src, flags);
771  if (ret != AVERROR(ENOSYS))
772  return ret;
773  }
774  }
775 
776  if (dst->hw_frames_ctx) {
777  dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
778 
779  if (dst_frames->format == dst->format &&
780  dst_frames->internal->hw_type->map_to) {
781  ret = dst_frames->internal->hw_type->map_to(dst_frames,
782  dst, src, flags);
783  if (ret != AVERROR(ENOSYS))
784  return ret;
785  }
786  }
787 
788  return AVERROR(ENOSYS);
789 }
790 
792  enum AVPixelFormat format,
793  AVBufferRef *derived_device_ctx,
794  AVBufferRef *source_frame_ctx,
795  int flags)
796 {
797  AVBufferRef *dst_ref = NULL;
798  AVHWFramesContext *dst = NULL;
799  AVHWFramesContext *src = (AVHWFramesContext*)source_frame_ctx->data;
800  int ret;
801 
802  if (src->internal->source_frames) {
803  AVHWFramesContext *src_src =
805  AVHWDeviceContext *dst_dev =
806  (AVHWDeviceContext*)derived_device_ctx->data;
807 
808  if (src_src->device_ctx == dst_dev) {
809  // This is actually an unmapping, so we just return a
810  // reference to the source frame context.
811  *derived_frame_ctx =
813  if (!*derived_frame_ctx) {
814  ret = AVERROR(ENOMEM);
815  goto fail;
816  }
817  return 0;
818  }
819  }
820 
821  dst_ref = av_hwframe_ctx_alloc(derived_device_ctx);
822  if (!dst_ref) {
823  ret = AVERROR(ENOMEM);
824  goto fail;
825  }
826 
827  dst = (AVHWFramesContext*)dst_ref->data;
828 
829  dst->format = format;
830  dst->sw_format = src->sw_format;
831  dst->width = src->width;
832  dst->height = src->height;
833 
834  dst->internal->source_frames = av_buffer_ref(source_frame_ctx);
835  if (!dst->internal->source_frames) {
836  ret = AVERROR(ENOMEM);
837  goto fail;
838  }
839 
841  flags & (AV_HWFRAME_MAP_READ |
845 
846  ret = AVERROR(ENOSYS);
848  ret = src->internal->hw_type->frames_derive_from(dst, src, flags);
849  if (ret == AVERROR(ENOSYS) &&
851  ret = dst->internal->hw_type->frames_derive_to(dst, src, flags);
852  if (ret == AVERROR(ENOSYS))
853  ret = 0;
854  if (ret)
855  goto fail;
856 
857  *derived_frame_ctx = dst_ref;
858  return 0;
859 
860 fail:
861  if (dst)
863  av_buffer_unref(&dst_ref);
864  return ret;
865 }
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:58
#define NULL
Definition: coverity.c:32
static enum AVPixelFormat pix_fmt
static void hwdevice_ctx_free(void *opaque, uint8_t *data)
Definition: hwcontext.c:109
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
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
misc image utilities
void(* frames_uninit)(AVHWFramesContext *ctx)
#define LIBAVUTIL_VERSION_INT
Definition: version.h:86
Memory handling functions.
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:393
enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name)
Look up an AVHWDeviceType by name.
Definition: hwcontext.c:70
const HWContextType ff_hwcontext_type_vdpau
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1780
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:226
void * av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
Allocate a HW-specific configuration structure for a given HW device.
Definition: hwcontext.c:518
int(* frames_derive_to)(AVHWFramesContext *dst_ctx, AVHWFramesContext *src_ctx, int flags)
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:206
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:531
size_t device_priv_size
size of the private data, i.e.
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
#define src
Definition: vp8dsp.c:254
const HWContextType * hw_type
static void set(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f, double v)
Definition: swresample.c:59
int(* map_to)(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src, int flags)
static const char *const hw_type_names[]
Definition: hwcontext.c:59
The mapping must be readable.
Definition: hwcontext.h:501
void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
Free an AVHWFrameConstraints structure.
Definition: hwcontext.c:554
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame...
Definition: frame.h:538
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
int(* device_derive)(AVHWDeviceContext *dst_ctx, AVHWDeviceContext *src_ctx, int flags)
AVBufferPool * pool_internal
int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx, enum AVPixelFormat format, AVBufferRef *derived_device_ctx, AVBufferRef *source_frame_ctx, int flags)
Create and initialise an AVHWFramesContext as a mapping of another existing AVHWFramesContext on a di...
Definition: hwcontext.c:791
enum AVHWDeviceType type
uint8_t
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
static int hwframe_pool_prealloc(AVBufferRef *ref)
Definition: hwcontext.c:295
size_t device_hwctx_size
size of the public hardware-specific context, i.e.
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:395
static AVFrame * frame
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:89
const HWContextType ff_hwcontext_type_qsv
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
static void hwframe_ctx_free(void *opaque, uint8_t *data)
Definition: hwcontext.c:211
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
Definition: buffer.h:113
static const AVClass hwframe_ctx_class
Definition: hwcontext.c:205
#define av_log(a,...)
void(* free)(struct AVHWFramesContext *ctx)
This field may be set by the caller before calling av_hwframe_ctx_init().
Definition: hwcontext.h:167
int width
Definition: frame.h:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
The mapping must be direct.
Definition: hwcontext.h:517
void(* free)(struct AVHWDeviceContext *ctx)
This field may be set by the caller before calling av_hwdevice_ctx_init().
Definition: hwcontext.h:101
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
int(* map_from)(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src, int flags)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
AVBufferRef * source_frames
For a derived context, a reference to the original frames context it was derived from.
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 void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:229
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
Definition: hwcontext.c:459
#define fail()
Definition: checkasm.h:109
const HWContextType ff_hwcontext_type_videotoolbox
static const HWContextType *const hw_table[]
Definition: hwcontext.c:31
void(* device_uninit)(AVHWDeviceContext *ctx)
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
Transfer the data from the queried hw frame.
Definition: hwcontext.h:396
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:281
AVFrame * source
A reference to the original source of the mapping.
enum AVPixelFormat * pix_fmts
An array of pixel formats supported by the AVHWFramesContext instances Terminated by AV_PIX_FMT_NONE...
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Definition: hwcontext.h:146
const char * name
void(* unmap)(AVHWFramesContext *ctx, struct HWMapDescriptor *hwmap)
Unmap function.
AVFormatContext * ctx
Definition: movenc.c:48
int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ref_ptr, enum AVHWDeviceType type, AVBufferRef *src_ref, int flags)
Create a new device of the specified type from an existing device.
Definition: hwcontext.c:599
int frames
Definition: movenc.c:65
#define FF_ARRAY_ELEMS(a)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:274
const HWContextType ff_hwcontext_type_drm
AVBufferRef * av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
Allocate an AVHWDeviceContext for a given hardware type.
Definition: hwcontext.c:129
void * priv
Hardware-specific private data associated with the mapping.
This struct describes the constraints on hardware frames attached to a given device with a hardware-s...
Definition: hwcontext.h:430
int(* device_create)(AVHWDeviceContext *ctx, const char *device, AVDictionary *opts, int flags)
const HWContextType ff_hwcontext_type_cuda
AVHWFramesConstraints * av_hwdevice_get_hwframe_constraints(AVBufferRef *ref, const void *hwconfig)
Get the constraints on HW frames given a device and the HW-specific configuration to be used with tha...
Definition: hwcontext.c:529
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
const AVClass * av_class
A class for logging.
Definition: hwcontext.h:62
void * buf
Definition: avisynth_c.h:690
GLint GLenum type
Definition: opengl_enc.c:105
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 const char * format
Definition: movenc.c:47
Describe the class of an AVClass context structure.
Definition: log.h:67
const char * av_hwdevice_get_type_name(enum AVHWDeviceType type)
Get the string name of an AVHWDeviceType.
Definition: hwcontext.c:80
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
Definition: buffer.c:275
int source_allocation_map_flags
Flags to apply to the mapping from the source to the derived frame context when trying to allocate in...
int(* frames_get_buffer)(AVHWFramesContext *ctx, AVFrame *frame)
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:121
const AVClass * av_class
A class for logging and AVOptions.
Definition: avformat.h:1354
refcounted data buffer API
AVBufferRef * hw_frames_ctx
A reference to the hardware frames context in which this mapping was made.
static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
Definition: hwcontext.c:387
int(* frames_derive_from)(AVHWFramesContext *dst_ctx, AVHWFramesContext *src_ctx, int flags)
AVHWFramesInternal * internal
Private data used internally by libavutil.
Definition: hwcontext.h:131
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:283
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:505
int(* transfer_data_from)(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src)
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
Definition: hwcontext.h:138
static void ff_hwframe_unmap(void *opaque, uint8_t *data)
Definition: hwcontext.c:666
int(* transfer_data_to)(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src)
A reference to a data buffer.
Definition: buffer.h:81
const HWContextType * hw_type
common internal and external API header
if(ret< 0)
Definition: vf_mcdeint.c:279
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
Map a hardware frame.
Definition: hwcontext.c:733
The mapped frame will be overwritten completely in subsequent operations, so the current frame data n...
Definition: hwcontext.h:511
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition: hwcontext.c:237
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
AVHWFrameTransferDirection
Definition: hwcontext.h:392
enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev)
Iterate over supported device types.
Definition: hwcontext.c:88
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
#define av_free(p)
The mapping must be writeable.
Definition: hwcontext.h:505
int(* transfer_get_formats)(AVHWFramesContext *ctx, enum AVHWFrameTransferDirection dir, enum AVPixelFormat **formats)
int height
Definition: frame.h:259
#define av_freep(p)
AVBufferRef * source_device
For a derived device, a reference to the original device context it was derived from.
int(* device_init)(AVHWDeviceContext *ctx)
formats
Definition: signature.h:48
AVHWDeviceInternal * internal
Private data used internally by libavutil.
Definition: hwcontext.h:68
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2335
static const AVClass hwdevice_ctx_class
Definition: hwcontext.c:103
int(* frames_init)(AVHWFramesContext *ctx)
const HWContextType ff_hwcontext_type_d3d11va
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:219
const HWContextType ff_hwcontext_type_dxva2
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref, enum AVHWFrameTransferDirection dir, enum AVPixelFormat **formats, int flags)
Get a list of possible source or target formats usable in av_hwframe_transfer_data().
Definition: hwcontext.c:375
const char * name
Definition: opengl_enc.c:103
const HWContextType ff_hwcontext_type_vaapi