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