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

929 lines
26KB

  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. #include "config.h"
  19. #include "buffer.h"
  20. #include "common.h"
  21. #include "hwcontext.h"
  22. #include "hwcontext_internal.h"
  23. #include "imgutils.h"
  24. #include "log.h"
  25. #include "mem.h"
  26. #include "pixdesc.h"
  27. #include "pixfmt.h"
  28. static const HWContextType * const hw_table[] = {
  29. #if CONFIG_CUDA
  30. &ff_hwcontext_type_cuda,
  31. #endif
  32. #if CONFIG_D3D11VA
  33. &ff_hwcontext_type_d3d11va,
  34. #endif
  35. #if CONFIG_LIBDRM
  36. &ff_hwcontext_type_drm,
  37. #endif
  38. #if CONFIG_DXVA2
  39. &ff_hwcontext_type_dxva2,
  40. #endif
  41. #if CONFIG_OPENCL
  42. &ff_hwcontext_type_opencl,
  43. #endif
  44. #if CONFIG_QSV
  45. &ff_hwcontext_type_qsv,
  46. #endif
  47. #if CONFIG_VAAPI
  48. &ff_hwcontext_type_vaapi,
  49. #endif
  50. #if CONFIG_VDPAU
  51. &ff_hwcontext_type_vdpau,
  52. #endif
  53. #if CONFIG_VIDEOTOOLBOX
  54. &ff_hwcontext_type_videotoolbox,
  55. #endif
  56. #if CONFIG_MEDIACODEC
  57. &ff_hwcontext_type_mediacodec,
  58. #endif
  59. #if CONFIG_VULKAN
  60. &ff_hwcontext_type_vulkan,
  61. #endif
  62. NULL,
  63. };
  64. static const char *const hw_type_names[] = {
  65. [AV_HWDEVICE_TYPE_CUDA] = "cuda",
  66. [AV_HWDEVICE_TYPE_DRM] = "drm",
  67. [AV_HWDEVICE_TYPE_DXVA2] = "dxva2",
  68. [AV_HWDEVICE_TYPE_D3D11VA] = "d3d11va",
  69. [AV_HWDEVICE_TYPE_OPENCL] = "opencl",
  70. [AV_HWDEVICE_TYPE_QSV] = "qsv",
  71. [AV_HWDEVICE_TYPE_VAAPI] = "vaapi",
  72. [AV_HWDEVICE_TYPE_VDPAU] = "vdpau",
  73. [AV_HWDEVICE_TYPE_VIDEOTOOLBOX] = "videotoolbox",
  74. [AV_HWDEVICE_TYPE_MEDIACODEC] = "mediacodec",
  75. [AV_HWDEVICE_TYPE_VULKAN] = "vulkan",
  76. };
  77. enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name)
  78. {
  79. int type;
  80. for (type = 0; type < FF_ARRAY_ELEMS(hw_type_names); type++) {
  81. if (hw_type_names[type] && !strcmp(hw_type_names[type], name))
  82. return type;
  83. }
  84. return AV_HWDEVICE_TYPE_NONE;
  85. }
  86. const char *av_hwdevice_get_type_name(enum AVHWDeviceType type)
  87. {
  88. if (type > AV_HWDEVICE_TYPE_NONE &&
  89. type < FF_ARRAY_ELEMS(hw_type_names))
  90. return hw_type_names[type];
  91. else
  92. return NULL;
  93. }
  94. enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev)
  95. {
  96. enum AVHWDeviceType next;
  97. int i, set = 0;
  98. for (i = 0; hw_table[i]; i++) {
  99. if (prev != AV_HWDEVICE_TYPE_NONE && hw_table[i]->type <= prev)
  100. continue;
  101. if (!set || hw_table[i]->type < next) {
  102. next = hw_table[i]->type;
  103. set = 1;
  104. }
  105. }
  106. return set ? next : AV_HWDEVICE_TYPE_NONE;
  107. }
  108. static const AVClass hwdevice_ctx_class = {
  109. .class_name = "AVHWDeviceContext",
  110. .item_name = av_default_item_name,
  111. .version = LIBAVUTIL_VERSION_INT,
  112. };
  113. static void hwdevice_ctx_free(void *opaque, uint8_t *data)
  114. {
  115. AVHWDeviceContext *ctx = (AVHWDeviceContext*)data;
  116. /* uninit might still want access the hw context and the user
  117. * free() callback might destroy it, so uninit has to be called first */
  118. if (ctx->internal->hw_type->device_uninit)
  119. ctx->internal->hw_type->device_uninit(ctx);
  120. if (ctx->free)
  121. ctx->free(ctx);
  122. av_buffer_unref(&ctx->internal->source_device);
  123. av_freep(&ctx->hwctx);
  124. av_freep(&ctx->internal->priv);
  125. av_freep(&ctx->internal);
  126. av_freep(&ctx);
  127. }
  128. AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
  129. {
  130. AVHWDeviceContext *ctx;
  131. AVBufferRef *buf;
  132. const HWContextType *hw_type = NULL;
  133. int i;
  134. for (i = 0; hw_table[i]; i++) {
  135. if (hw_table[i]->type == type) {
  136. hw_type = hw_table[i];
  137. break;
  138. }
  139. }
  140. if (!hw_type)
  141. return NULL;
  142. ctx = av_mallocz(sizeof(*ctx));
  143. if (!ctx)
  144. return NULL;
  145. ctx->internal = av_mallocz(sizeof(*ctx->internal));
  146. if (!ctx->internal)
  147. goto fail;
  148. if (hw_type->device_priv_size) {
  149. ctx->internal->priv = av_mallocz(hw_type->device_priv_size);
  150. if (!ctx->internal->priv)
  151. goto fail;
  152. }
  153. if (hw_type->device_hwctx_size) {
  154. ctx->hwctx = av_mallocz(hw_type->device_hwctx_size);
  155. if (!ctx->hwctx)
  156. goto fail;
  157. }
  158. buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
  159. hwdevice_ctx_free, NULL,
  160. AV_BUFFER_FLAG_READONLY);
  161. if (!buf)
  162. goto fail;
  163. ctx->type = type;
  164. ctx->av_class = &hwdevice_ctx_class;
  165. ctx->internal->hw_type = hw_type;
  166. return buf;
  167. fail:
  168. if (ctx->internal)
  169. av_freep(&ctx->internal->priv);
  170. av_freep(&ctx->internal);
  171. av_freep(&ctx->hwctx);
  172. av_freep(&ctx);
  173. return NULL;
  174. }
  175. int av_hwdevice_ctx_init(AVBufferRef *ref)
  176. {
  177. AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
  178. int ret;
  179. if (ctx->internal->hw_type->device_init) {
  180. ret = ctx->internal->hw_type->device_init(ctx);
  181. if (ret < 0)
  182. goto fail;
  183. }
  184. return 0;
  185. fail:
  186. if (ctx->internal->hw_type->device_uninit)
  187. ctx->internal->hw_type->device_uninit(ctx);
  188. return ret;
  189. }
  190. static const AVClass hwframe_ctx_class = {
  191. .class_name = "AVHWFramesContext",
  192. .item_name = av_default_item_name,
  193. .version = LIBAVUTIL_VERSION_INT,
  194. };
  195. static void hwframe_ctx_free(void *opaque, uint8_t *data)
  196. {
  197. AVHWFramesContext *ctx = (AVHWFramesContext*)data;
  198. if (ctx->internal->pool_internal)
  199. av_buffer_pool_uninit(&ctx->internal->pool_internal);
  200. if (ctx->internal->hw_type->frames_uninit)
  201. ctx->internal->hw_type->frames_uninit(ctx);
  202. if (ctx->free)
  203. ctx->free(ctx);
  204. av_buffer_unref(&ctx->internal->source_frames);
  205. av_buffer_unref(&ctx->device_ref);
  206. av_freep(&ctx->hwctx);
  207. av_freep(&ctx->internal->priv);
  208. av_freep(&ctx->internal);
  209. av_freep(&ctx);
  210. }
  211. AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
  212. {
  213. AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref_in->data;
  214. const HWContextType *hw_type = device_ctx->internal->hw_type;
  215. AVHWFramesContext *ctx;
  216. AVBufferRef *buf, *device_ref = NULL;
  217. ctx = av_mallocz(sizeof(*ctx));
  218. if (!ctx)
  219. return NULL;
  220. ctx->internal = av_mallocz(sizeof(*ctx->internal));
  221. if (!ctx->internal)
  222. goto fail;
  223. if (hw_type->frames_priv_size) {
  224. ctx->internal->priv = av_mallocz(hw_type->frames_priv_size);
  225. if (!ctx->internal->priv)
  226. goto fail;
  227. }
  228. if (hw_type->frames_hwctx_size) {
  229. ctx->hwctx = av_mallocz(hw_type->frames_hwctx_size);
  230. if (!ctx->hwctx)
  231. goto fail;
  232. }
  233. device_ref = av_buffer_ref(device_ref_in);
  234. if (!device_ref)
  235. goto fail;
  236. buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
  237. hwframe_ctx_free, NULL,
  238. AV_BUFFER_FLAG_READONLY);
  239. if (!buf)
  240. goto fail;
  241. ctx->av_class = &hwframe_ctx_class;
  242. ctx->device_ref = device_ref;
  243. ctx->device_ctx = device_ctx;
  244. ctx->format = AV_PIX_FMT_NONE;
  245. ctx->sw_format = AV_PIX_FMT_NONE;
  246. ctx->internal->hw_type = hw_type;
  247. return buf;
  248. fail:
  249. if (device_ref)
  250. av_buffer_unref(&device_ref);
  251. if (ctx->internal)
  252. av_freep(&ctx->internal->priv);
  253. av_freep(&ctx->internal);
  254. av_freep(&ctx->hwctx);
  255. av_freep(&ctx);
  256. return NULL;
  257. }
  258. static int hwframe_pool_prealloc(AVBufferRef *ref)
  259. {
  260. AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
  261. AVFrame **frames;
  262. int i, ret = 0;
  263. frames = av_mallocz_array(ctx->initial_pool_size, sizeof(*frames));
  264. if (!frames)
  265. return AVERROR(ENOMEM);
  266. for (i = 0; i < ctx->initial_pool_size; i++) {
  267. frames[i] = av_frame_alloc();
  268. if (!frames[i])
  269. goto fail;
  270. ret = av_hwframe_get_buffer(ref, frames[i], 0);
  271. if (ret < 0)
  272. goto fail;
  273. }
  274. fail:
  275. for (i = 0; i < ctx->initial_pool_size; i++)
  276. av_frame_free(&frames[i]);
  277. av_freep(&frames);
  278. return ret;
  279. }
  280. int av_hwframe_ctx_init(AVBufferRef *ref)
  281. {
  282. AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
  283. const enum AVPixelFormat *pix_fmt;
  284. int ret;
  285. if (ctx->internal->source_frames) {
  286. /* A derived frame context is already initialised. */
  287. return 0;
  288. }
  289. /* validate the pixel format */
  290. for (pix_fmt = ctx->internal->hw_type->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) {
  291. if (*pix_fmt == ctx->format)
  292. break;
  293. }
  294. if (*pix_fmt == AV_PIX_FMT_NONE) {
  295. av_log(ctx, AV_LOG_ERROR,
  296. "The hardware pixel format '%s' is not supported by the device type '%s'\n",
  297. av_get_pix_fmt_name(ctx->format), ctx->internal->hw_type->name);
  298. return AVERROR(ENOSYS);
  299. }
  300. /* validate the dimensions */
  301. ret = av_image_check_size(ctx->width, ctx->height, 0, ctx);
  302. if (ret < 0)
  303. return ret;
  304. /* format-specific init */
  305. if (ctx->internal->hw_type->frames_init) {
  306. ret = ctx->internal->hw_type->frames_init(ctx);
  307. if (ret < 0)
  308. goto fail;
  309. }
  310. if (ctx->internal->pool_internal && !ctx->pool)
  311. ctx->pool = ctx->internal->pool_internal;
  312. /* preallocate the frames in the pool, if requested */
  313. if (ctx->initial_pool_size > 0) {
  314. ret = hwframe_pool_prealloc(ref);
  315. if (ret < 0)
  316. goto fail;
  317. }
  318. return 0;
  319. fail:
  320. if (ctx->internal->hw_type->frames_uninit)
  321. ctx->internal->hw_type->frames_uninit(ctx);
  322. return ret;
  323. }
  324. int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref,
  325. enum AVHWFrameTransferDirection dir,
  326. enum AVPixelFormat **formats, int flags)
  327. {
  328. AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
  329. if (!ctx->internal->hw_type->transfer_get_formats)
  330. return AVERROR(ENOSYS);
  331. return ctx->internal->hw_type->transfer_get_formats(ctx, dir, formats);
  332. }
  333. static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
  334. {
  335. AVHWFramesContext *ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
  336. AVFrame *frame_tmp;
  337. int ret = 0;
  338. frame_tmp = av_frame_alloc();
  339. if (!frame_tmp)
  340. return AVERROR(ENOMEM);
  341. /* if the format is set, use that
  342. * otherwise pick the first supported one */
  343. if (dst->format >= 0) {
  344. frame_tmp->format = dst->format;
  345. } else {
  346. enum AVPixelFormat *formats;
  347. ret = av_hwframe_transfer_get_formats(src->hw_frames_ctx,
  348. AV_HWFRAME_TRANSFER_DIRECTION_FROM,
  349. &formats, 0);
  350. if (ret < 0)
  351. goto fail;
  352. frame_tmp->format = formats[0];
  353. av_freep(&formats);
  354. }
  355. frame_tmp->width = ctx->width;
  356. frame_tmp->height = ctx->height;
  357. ret = av_frame_get_buffer(frame_tmp, 0);
  358. if (ret < 0)
  359. goto fail;
  360. ret = av_hwframe_transfer_data(frame_tmp, src, flags);
  361. if (ret < 0)
  362. goto fail;
  363. frame_tmp->width = src->width;
  364. frame_tmp->height = src->height;
  365. av_frame_move_ref(dst, frame_tmp);
  366. fail:
  367. av_frame_free(&frame_tmp);
  368. return ret;
  369. }
  370. int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
  371. {
  372. AVHWFramesContext *ctx;
  373. int ret;
  374. if (!dst->buf[0])
  375. return transfer_data_alloc(dst, src, flags);
  376. /*
  377. * Hardware -> Hardware Transfer.
  378. * Unlike Software -> Hardware or Hardware -> Software, the transfer
  379. * function could be provided by either the src or dst, depending on
  380. * the specific combination of hardware.
  381. */
  382. if (src->hw_frames_ctx && dst->hw_frames_ctx) {
  383. AVHWFramesContext *src_ctx =
  384. (AVHWFramesContext*)src->hw_frames_ctx->data;
  385. AVHWFramesContext *dst_ctx =
  386. (AVHWFramesContext*)dst->hw_frames_ctx->data;
  387. if (src_ctx->internal->source_frames) {
  388. av_log(src_ctx, AV_LOG_ERROR,
  389. "A device with a derived frame context cannot be used as "
  390. "the source of a HW -> HW transfer.");
  391. return AVERROR(ENOSYS);
  392. }
  393. if (dst_ctx->internal->source_frames) {
  394. av_log(src_ctx, AV_LOG_ERROR,
  395. "A device with a derived frame context cannot be used as "
  396. "the destination of a HW -> HW transfer.");
  397. return AVERROR(ENOSYS);
  398. }
  399. ret = src_ctx->internal->hw_type->transfer_data_from(src_ctx, dst, src);
  400. if (ret == AVERROR(ENOSYS))
  401. ret = dst_ctx->internal->hw_type->transfer_data_to(dst_ctx, dst, src);
  402. if (ret < 0)
  403. return ret;
  404. } else {
  405. if (src->hw_frames_ctx) {
  406. ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
  407. ret = ctx->internal->hw_type->transfer_data_from(ctx, dst, src);
  408. if (ret < 0)
  409. return ret;
  410. } else if (dst->hw_frames_ctx) {
  411. ctx = (AVHWFramesContext*)dst->hw_frames_ctx->data;
  412. ret = ctx->internal->hw_type->transfer_data_to(ctx, dst, src);
  413. if (ret < 0)
  414. return ret;
  415. } else {
  416. return AVERROR(ENOSYS);
  417. }
  418. }
  419. return 0;
  420. }
  421. int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
  422. {
  423. AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
  424. int ret;
  425. if (ctx->internal->source_frames) {
  426. // This is a derived frame context, so we allocate in the source
  427. // and map the frame immediately.
  428. AVFrame *src_frame;
  429. frame->format = ctx->format;
  430. frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
  431. if (!frame->hw_frames_ctx)
  432. return AVERROR(ENOMEM);
  433. src_frame = av_frame_alloc();
  434. if (!src_frame)
  435. return AVERROR(ENOMEM);
  436. ret = av_hwframe_get_buffer(ctx->internal->source_frames,
  437. src_frame, 0);
  438. if (ret < 0) {
  439. av_frame_free(&src_frame);
  440. return ret;
  441. }
  442. ret = av_hwframe_map(frame, src_frame,
  443. ctx->internal->source_allocation_map_flags);
  444. if (ret) {
  445. av_log(ctx, AV_LOG_ERROR, "Failed to map frame into derived "
  446. "frame context: %d.\n", ret);
  447. av_frame_free(&src_frame);
  448. return ret;
  449. }
  450. // Free the source frame immediately - the mapped frame still
  451. // contains a reference to it.
  452. av_frame_free(&src_frame);
  453. return 0;
  454. }
  455. if (!ctx->internal->hw_type->frames_get_buffer)
  456. return AVERROR(ENOSYS);
  457. if (!ctx->pool)
  458. return AVERROR(EINVAL);
  459. frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
  460. if (!frame->hw_frames_ctx)
  461. return AVERROR(ENOMEM);
  462. ret = ctx->internal->hw_type->frames_get_buffer(ctx, frame);
  463. if (ret < 0) {
  464. av_buffer_unref(&frame->hw_frames_ctx);
  465. return ret;
  466. }
  467. frame->extended_data = frame->data;
  468. return 0;
  469. }
  470. void *av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
  471. {
  472. AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
  473. const HWContextType *hw_type = ctx->internal->hw_type;
  474. if (hw_type->device_hwconfig_size == 0)
  475. return NULL;
  476. return av_mallocz(hw_type->device_hwconfig_size);
  477. }
  478. AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
  479. const void *hwconfig)
  480. {
  481. AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
  482. const HWContextType *hw_type = ctx->internal->hw_type;
  483. AVHWFramesConstraints *constraints;
  484. if (!hw_type->frames_get_constraints)
  485. return NULL;
  486. constraints = av_mallocz(sizeof(*constraints));
  487. if (!constraints)
  488. return NULL;
  489. constraints->min_width = constraints->min_height = 0;
  490. constraints->max_width = constraints->max_height = INT_MAX;
  491. if (hw_type->frames_get_constraints(ctx, hwconfig, constraints) >= 0) {
  492. return constraints;
  493. } else {
  494. av_hwframe_constraints_free(&constraints);
  495. return NULL;
  496. }
  497. }
  498. void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
  499. {
  500. if (*constraints) {
  501. av_freep(&(*constraints)->valid_hw_formats);
  502. av_freep(&(*constraints)->valid_sw_formats);
  503. }
  504. av_freep(constraints);
  505. }
  506. int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type,
  507. const char *device, AVDictionary *opts, int flags)
  508. {
  509. AVBufferRef *device_ref = NULL;
  510. AVHWDeviceContext *device_ctx;
  511. int ret = 0;
  512. device_ref = av_hwdevice_ctx_alloc(type);
  513. if (!device_ref) {
  514. ret = AVERROR(ENOMEM);
  515. goto fail;
  516. }
  517. device_ctx = (AVHWDeviceContext*)device_ref->data;
  518. if (!device_ctx->internal->hw_type->device_create) {
  519. ret = AVERROR(ENOSYS);
  520. goto fail;
  521. }
  522. ret = device_ctx->internal->hw_type->device_create(device_ctx, device,
  523. opts, flags);
  524. if (ret < 0)
  525. goto fail;
  526. ret = av_hwdevice_ctx_init(device_ref);
  527. if (ret < 0)
  528. goto fail;
  529. *pdevice_ref = device_ref;
  530. return 0;
  531. fail:
  532. av_buffer_unref(&device_ref);
  533. *pdevice_ref = NULL;
  534. return ret;
  535. }
  536. int av_hwdevice_ctx_create_derived_opts(AVBufferRef **dst_ref_ptr,
  537. enum AVHWDeviceType type,
  538. AVBufferRef *src_ref,
  539. AVDictionary *options, int flags)
  540. {
  541. AVBufferRef *dst_ref = NULL, *tmp_ref;
  542. AVHWDeviceContext *dst_ctx, *tmp_ctx;
  543. int ret = 0;
  544. tmp_ref = src_ref;
  545. while (tmp_ref) {
  546. tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
  547. if (tmp_ctx->type == type) {
  548. dst_ref = av_buffer_ref(tmp_ref);
  549. if (!dst_ref) {
  550. ret = AVERROR(ENOMEM);
  551. goto fail;
  552. }
  553. goto done;
  554. }
  555. tmp_ref = tmp_ctx->internal->source_device;
  556. }
  557. dst_ref = av_hwdevice_ctx_alloc(type);
  558. if (!dst_ref) {
  559. ret = AVERROR(ENOMEM);
  560. goto fail;
  561. }
  562. dst_ctx = (AVHWDeviceContext*)dst_ref->data;
  563. tmp_ref = src_ref;
  564. while (tmp_ref) {
  565. tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
  566. if (dst_ctx->internal->hw_type->device_derive) {
  567. ret = dst_ctx->internal->hw_type->device_derive(dst_ctx,
  568. tmp_ctx,
  569. options,
  570. flags);
  571. if (ret == 0) {
  572. dst_ctx->internal->source_device = av_buffer_ref(src_ref);
  573. if (!dst_ctx->internal->source_device) {
  574. ret = AVERROR(ENOMEM);
  575. goto fail;
  576. }
  577. ret = av_hwdevice_ctx_init(dst_ref);
  578. if (ret < 0)
  579. goto fail;
  580. goto done;
  581. }
  582. if (ret != AVERROR(ENOSYS))
  583. goto fail;
  584. }
  585. tmp_ref = tmp_ctx->internal->source_device;
  586. }
  587. ret = AVERROR(ENOSYS);
  588. goto fail;
  589. done:
  590. *dst_ref_ptr = dst_ref;
  591. return 0;
  592. fail:
  593. av_buffer_unref(&dst_ref);
  594. *dst_ref_ptr = NULL;
  595. return ret;
  596. }
  597. int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ref_ptr,
  598. enum AVHWDeviceType type,
  599. AVBufferRef *src_ref, int flags)
  600. {
  601. return av_hwdevice_ctx_create_derived_opts(dst_ref_ptr, type, src_ref,
  602. NULL, flags);
  603. }
  604. static void ff_hwframe_unmap(void *opaque, uint8_t *data)
  605. {
  606. HWMapDescriptor *hwmap = (HWMapDescriptor*)data;
  607. AVHWFramesContext *ctx = opaque;
  608. if (hwmap->unmap)
  609. hwmap->unmap(ctx, hwmap);
  610. av_frame_free(&hwmap->source);
  611. av_buffer_unref(&hwmap->hw_frames_ctx);
  612. av_free(hwmap);
  613. }
  614. int ff_hwframe_map_create(AVBufferRef *hwframe_ref,
  615. AVFrame *dst, const AVFrame *src,
  616. void (*unmap)(AVHWFramesContext *ctx,
  617. HWMapDescriptor *hwmap),
  618. void *priv)
  619. {
  620. AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
  621. HWMapDescriptor *hwmap;
  622. int ret;
  623. hwmap = av_mallocz(sizeof(*hwmap));
  624. if (!hwmap) {
  625. ret = AVERROR(ENOMEM);
  626. goto fail;
  627. }
  628. hwmap->source = av_frame_alloc();
  629. if (!hwmap->source) {
  630. ret = AVERROR(ENOMEM);
  631. goto fail;
  632. }
  633. ret = av_frame_ref(hwmap->source, src);
  634. if (ret < 0)
  635. goto fail;
  636. hwmap->hw_frames_ctx = av_buffer_ref(hwframe_ref);
  637. if (!hwmap->hw_frames_ctx) {
  638. ret = AVERROR(ENOMEM);
  639. goto fail;
  640. }
  641. hwmap->unmap = unmap;
  642. hwmap->priv = priv;
  643. dst->buf[0] = av_buffer_create((uint8_t*)hwmap, sizeof(*hwmap),
  644. &ff_hwframe_unmap, ctx, 0);
  645. if (!dst->buf[0]) {
  646. ret = AVERROR(ENOMEM);
  647. goto fail;
  648. }
  649. return 0;
  650. fail:
  651. if (hwmap) {
  652. av_buffer_unref(&hwmap->hw_frames_ctx);
  653. av_frame_free(&hwmap->source);
  654. }
  655. av_free(hwmap);
  656. return ret;
  657. }
  658. int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
  659. {
  660. AVHWFramesContext *src_frames, *dst_frames;
  661. HWMapDescriptor *hwmap;
  662. int ret;
  663. if (src->hw_frames_ctx && dst->hw_frames_ctx) {
  664. src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
  665. dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
  666. if ((src_frames == dst_frames &&
  667. src->format == dst_frames->sw_format &&
  668. dst->format == dst_frames->format) ||
  669. (src_frames->internal->source_frames &&
  670. src_frames->internal->source_frames->data ==
  671. (uint8_t*)dst_frames)) {
  672. // This is an unmap operation. We don't need to directly
  673. // do anything here other than fill in the original frame,
  674. // because the real unmap will be invoked when the last
  675. // reference to the mapped frame disappears.
  676. if (!src->buf[0]) {
  677. av_log(src_frames, AV_LOG_ERROR, "Invalid mapping "
  678. "found when attempting unmap.\n");
  679. return AVERROR(EINVAL);
  680. }
  681. hwmap = (HWMapDescriptor*)src->buf[0]->data;
  682. av_frame_unref(dst);
  683. return av_frame_ref(dst, hwmap->source);
  684. }
  685. }
  686. if (src->hw_frames_ctx) {
  687. src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
  688. if (src_frames->format == src->format &&
  689. src_frames->internal->hw_type->map_from) {
  690. ret = src_frames->internal->hw_type->map_from(src_frames,
  691. dst, src, flags);
  692. if (ret != AVERROR(ENOSYS))
  693. return ret;
  694. }
  695. }
  696. if (dst->hw_frames_ctx) {
  697. dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
  698. if (dst_frames->format == dst->format &&
  699. dst_frames->internal->hw_type->map_to) {
  700. ret = dst_frames->internal->hw_type->map_to(dst_frames,
  701. dst, src, flags);
  702. if (ret != AVERROR(ENOSYS))
  703. return ret;
  704. }
  705. }
  706. return AVERROR(ENOSYS);
  707. }
  708. int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
  709. enum AVPixelFormat format,
  710. AVBufferRef *derived_device_ctx,
  711. AVBufferRef *source_frame_ctx,
  712. int flags)
  713. {
  714. AVBufferRef *dst_ref = NULL;
  715. AVHWFramesContext *dst = NULL;
  716. AVHWFramesContext *src = (AVHWFramesContext*)source_frame_ctx->data;
  717. int ret;
  718. if (src->internal->source_frames) {
  719. AVHWFramesContext *src_src =
  720. (AVHWFramesContext*)src->internal->source_frames->data;
  721. AVHWDeviceContext *dst_dev =
  722. (AVHWDeviceContext*)derived_device_ctx->data;
  723. if (src_src->device_ctx == dst_dev) {
  724. // This is actually an unmapping, so we just return a
  725. // reference to the source frame context.
  726. *derived_frame_ctx =
  727. av_buffer_ref(src->internal->source_frames);
  728. if (!*derived_frame_ctx) {
  729. ret = AVERROR(ENOMEM);
  730. goto fail;
  731. }
  732. return 0;
  733. }
  734. }
  735. dst_ref = av_hwframe_ctx_alloc(derived_device_ctx);
  736. if (!dst_ref) {
  737. ret = AVERROR(ENOMEM);
  738. goto fail;
  739. }
  740. dst = (AVHWFramesContext*)dst_ref->data;
  741. dst->format = format;
  742. dst->sw_format = src->sw_format;
  743. dst->width = src->width;
  744. dst->height = src->height;
  745. dst->internal->source_frames = av_buffer_ref(source_frame_ctx);
  746. if (!dst->internal->source_frames) {
  747. ret = AVERROR(ENOMEM);
  748. goto fail;
  749. }
  750. dst->internal->source_allocation_map_flags =
  751. flags & (AV_HWFRAME_MAP_READ |
  752. AV_HWFRAME_MAP_WRITE |
  753. AV_HWFRAME_MAP_OVERWRITE |
  754. AV_HWFRAME_MAP_DIRECT);
  755. ret = AVERROR(ENOSYS);
  756. if (src->internal->hw_type->frames_derive_from)
  757. ret = src->internal->hw_type->frames_derive_from(dst, src, flags);
  758. if (ret == AVERROR(ENOSYS) &&
  759. dst->internal->hw_type->frames_derive_to)
  760. ret = dst->internal->hw_type->frames_derive_to(dst, src, flags);
  761. if (ret == AVERROR(ENOSYS))
  762. ret = 0;
  763. if (ret)
  764. goto fail;
  765. *derived_frame_ctx = dst_ref;
  766. return 0;
  767. fail:
  768. if (dst)
  769. av_buffer_unref(&dst->internal->source_frames);
  770. av_buffer_unref(&dst_ref);
  771. return ret;
  772. }
  773. int ff_hwframe_map_replace(AVFrame *dst, const AVFrame *src)
  774. {
  775. HWMapDescriptor *hwmap = (HWMapDescriptor*)dst->buf[0]->data;
  776. av_frame_unref(hwmap->source);
  777. return av_frame_ref(hwmap->source, src);
  778. }