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.

919 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(AVBufferRef **dst_ref_ptr,
  537. enum AVHWDeviceType type,
  538. AVBufferRef *src_ref, int flags)
  539. {
  540. AVBufferRef *dst_ref = NULL, *tmp_ref;
  541. AVHWDeviceContext *dst_ctx, *tmp_ctx;
  542. int ret = 0;
  543. tmp_ref = src_ref;
  544. while (tmp_ref) {
  545. tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
  546. if (tmp_ctx->type == type) {
  547. dst_ref = av_buffer_ref(tmp_ref);
  548. if (!dst_ref) {
  549. ret = AVERROR(ENOMEM);
  550. goto fail;
  551. }
  552. goto done;
  553. }
  554. tmp_ref = tmp_ctx->internal->source_device;
  555. }
  556. dst_ref = av_hwdevice_ctx_alloc(type);
  557. if (!dst_ref) {
  558. ret = AVERROR(ENOMEM);
  559. goto fail;
  560. }
  561. dst_ctx = (AVHWDeviceContext*)dst_ref->data;
  562. tmp_ref = src_ref;
  563. while (tmp_ref) {
  564. tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
  565. if (dst_ctx->internal->hw_type->device_derive) {
  566. ret = dst_ctx->internal->hw_type->device_derive(dst_ctx,
  567. tmp_ctx,
  568. flags);
  569. if (ret == 0) {
  570. dst_ctx->internal->source_device = av_buffer_ref(src_ref);
  571. if (!dst_ctx->internal->source_device) {
  572. ret = AVERROR(ENOMEM);
  573. goto fail;
  574. }
  575. ret = av_hwdevice_ctx_init(dst_ref);
  576. if (ret < 0)
  577. goto fail;
  578. goto done;
  579. }
  580. if (ret != AVERROR(ENOSYS))
  581. goto fail;
  582. }
  583. tmp_ref = tmp_ctx->internal->source_device;
  584. }
  585. ret = AVERROR(ENOSYS);
  586. goto fail;
  587. done:
  588. *dst_ref_ptr = dst_ref;
  589. return 0;
  590. fail:
  591. av_buffer_unref(&dst_ref);
  592. *dst_ref_ptr = NULL;
  593. return ret;
  594. }
  595. static void ff_hwframe_unmap(void *opaque, uint8_t *data)
  596. {
  597. HWMapDescriptor *hwmap = (HWMapDescriptor*)data;
  598. AVHWFramesContext *ctx = opaque;
  599. if (hwmap->unmap)
  600. hwmap->unmap(ctx, hwmap);
  601. av_frame_free(&hwmap->source);
  602. av_buffer_unref(&hwmap->hw_frames_ctx);
  603. av_free(hwmap);
  604. }
  605. int ff_hwframe_map_create(AVBufferRef *hwframe_ref,
  606. AVFrame *dst, const AVFrame *src,
  607. void (*unmap)(AVHWFramesContext *ctx,
  608. HWMapDescriptor *hwmap),
  609. void *priv)
  610. {
  611. AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
  612. HWMapDescriptor *hwmap;
  613. int ret;
  614. hwmap = av_mallocz(sizeof(*hwmap));
  615. if (!hwmap) {
  616. ret = AVERROR(ENOMEM);
  617. goto fail;
  618. }
  619. hwmap->source = av_frame_alloc();
  620. if (!hwmap->source) {
  621. ret = AVERROR(ENOMEM);
  622. goto fail;
  623. }
  624. ret = av_frame_ref(hwmap->source, src);
  625. if (ret < 0)
  626. goto fail;
  627. hwmap->hw_frames_ctx = av_buffer_ref(hwframe_ref);
  628. if (!hwmap->hw_frames_ctx) {
  629. ret = AVERROR(ENOMEM);
  630. goto fail;
  631. }
  632. hwmap->unmap = unmap;
  633. hwmap->priv = priv;
  634. dst->buf[0] = av_buffer_create((uint8_t*)hwmap, sizeof(*hwmap),
  635. &ff_hwframe_unmap, ctx, 0);
  636. if (!dst->buf[0]) {
  637. ret = AVERROR(ENOMEM);
  638. goto fail;
  639. }
  640. return 0;
  641. fail:
  642. if (hwmap) {
  643. av_buffer_unref(&hwmap->hw_frames_ctx);
  644. av_frame_free(&hwmap->source);
  645. }
  646. av_free(hwmap);
  647. return ret;
  648. }
  649. int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
  650. {
  651. AVHWFramesContext *src_frames, *dst_frames;
  652. HWMapDescriptor *hwmap;
  653. int ret;
  654. if (src->hw_frames_ctx && dst->hw_frames_ctx) {
  655. src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
  656. dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
  657. if ((src_frames == dst_frames &&
  658. src->format == dst_frames->sw_format &&
  659. dst->format == dst_frames->format) ||
  660. (src_frames->internal->source_frames &&
  661. src_frames->internal->source_frames->data ==
  662. (uint8_t*)dst_frames)) {
  663. // This is an unmap operation. We don't need to directly
  664. // do anything here other than fill in the original frame,
  665. // because the real unmap will be invoked when the last
  666. // reference to the mapped frame disappears.
  667. if (!src->buf[0]) {
  668. av_log(src_frames, AV_LOG_ERROR, "Invalid mapping "
  669. "found when attempting unmap.\n");
  670. return AVERROR(EINVAL);
  671. }
  672. hwmap = (HWMapDescriptor*)src->buf[0]->data;
  673. av_frame_unref(dst);
  674. return av_frame_ref(dst, hwmap->source);
  675. }
  676. }
  677. if (src->hw_frames_ctx) {
  678. src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
  679. if (src_frames->format == src->format &&
  680. src_frames->internal->hw_type->map_from) {
  681. ret = src_frames->internal->hw_type->map_from(src_frames,
  682. dst, src, flags);
  683. if (ret != AVERROR(ENOSYS))
  684. return ret;
  685. }
  686. }
  687. if (dst->hw_frames_ctx) {
  688. dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
  689. if (dst_frames->format == dst->format &&
  690. dst_frames->internal->hw_type->map_to) {
  691. ret = dst_frames->internal->hw_type->map_to(dst_frames,
  692. dst, src, flags);
  693. if (ret != AVERROR(ENOSYS))
  694. return ret;
  695. }
  696. }
  697. return AVERROR(ENOSYS);
  698. }
  699. int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
  700. enum AVPixelFormat format,
  701. AVBufferRef *derived_device_ctx,
  702. AVBufferRef *source_frame_ctx,
  703. int flags)
  704. {
  705. AVBufferRef *dst_ref = NULL;
  706. AVHWFramesContext *dst = NULL;
  707. AVHWFramesContext *src = (AVHWFramesContext*)source_frame_ctx->data;
  708. int ret;
  709. if (src->internal->source_frames) {
  710. AVHWFramesContext *src_src =
  711. (AVHWFramesContext*)src->internal->source_frames->data;
  712. AVHWDeviceContext *dst_dev =
  713. (AVHWDeviceContext*)derived_device_ctx->data;
  714. if (src_src->device_ctx == dst_dev) {
  715. // This is actually an unmapping, so we just return a
  716. // reference to the source frame context.
  717. *derived_frame_ctx =
  718. av_buffer_ref(src->internal->source_frames);
  719. if (!*derived_frame_ctx) {
  720. ret = AVERROR(ENOMEM);
  721. goto fail;
  722. }
  723. return 0;
  724. }
  725. }
  726. dst_ref = av_hwframe_ctx_alloc(derived_device_ctx);
  727. if (!dst_ref) {
  728. ret = AVERROR(ENOMEM);
  729. goto fail;
  730. }
  731. dst = (AVHWFramesContext*)dst_ref->data;
  732. dst->format = format;
  733. dst->sw_format = src->sw_format;
  734. dst->width = src->width;
  735. dst->height = src->height;
  736. dst->internal->source_frames = av_buffer_ref(source_frame_ctx);
  737. if (!dst->internal->source_frames) {
  738. ret = AVERROR(ENOMEM);
  739. goto fail;
  740. }
  741. dst->internal->source_allocation_map_flags =
  742. flags & (AV_HWFRAME_MAP_READ |
  743. AV_HWFRAME_MAP_WRITE |
  744. AV_HWFRAME_MAP_OVERWRITE |
  745. AV_HWFRAME_MAP_DIRECT);
  746. ret = AVERROR(ENOSYS);
  747. if (src->internal->hw_type->frames_derive_from)
  748. ret = src->internal->hw_type->frames_derive_from(dst, src, flags);
  749. if (ret == AVERROR(ENOSYS) &&
  750. dst->internal->hw_type->frames_derive_to)
  751. ret = dst->internal->hw_type->frames_derive_to(dst, src, flags);
  752. if (ret == AVERROR(ENOSYS))
  753. ret = 0;
  754. if (ret)
  755. goto fail;
  756. *derived_frame_ctx = dst_ref;
  757. return 0;
  758. fail:
  759. if (dst)
  760. av_buffer_unref(&dst->internal->source_frames);
  761. av_buffer_unref(&dst_ref);
  762. return ret;
  763. }
  764. int ff_hwframe_map_replace(AVFrame *dst, const AVFrame *src)
  765. {
  766. HWMapDescriptor *hwmap = (HWMapDescriptor*)dst->buf[0]->data;
  767. av_frame_unref(hwmap->source);
  768. return av_frame_ref(hwmap->source, src);
  769. }