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.

917 lines
25KB

  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, 32);
  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. return 0;
  468. }
  469. void *av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
  470. {
  471. AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
  472. const HWContextType *hw_type = ctx->internal->hw_type;
  473. if (hw_type->device_hwconfig_size == 0)
  474. return NULL;
  475. return av_mallocz(hw_type->device_hwconfig_size);
  476. }
  477. AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
  478. const void *hwconfig)
  479. {
  480. AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
  481. const HWContextType *hw_type = ctx->internal->hw_type;
  482. AVHWFramesConstraints *constraints;
  483. if (!hw_type->frames_get_constraints)
  484. return NULL;
  485. constraints = av_mallocz(sizeof(*constraints));
  486. if (!constraints)
  487. return NULL;
  488. constraints->min_width = constraints->min_height = 0;
  489. constraints->max_width = constraints->max_height = INT_MAX;
  490. if (hw_type->frames_get_constraints(ctx, hwconfig, constraints) >= 0) {
  491. return constraints;
  492. } else {
  493. av_hwframe_constraints_free(&constraints);
  494. return NULL;
  495. }
  496. }
  497. void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
  498. {
  499. if (*constraints) {
  500. av_freep(&(*constraints)->valid_hw_formats);
  501. av_freep(&(*constraints)->valid_sw_formats);
  502. }
  503. av_freep(constraints);
  504. }
  505. int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type,
  506. const char *device, AVDictionary *opts, int flags)
  507. {
  508. AVBufferRef *device_ref = NULL;
  509. AVHWDeviceContext *device_ctx;
  510. int ret = 0;
  511. device_ref = av_hwdevice_ctx_alloc(type);
  512. if (!device_ref) {
  513. ret = AVERROR(ENOMEM);
  514. goto fail;
  515. }
  516. device_ctx = (AVHWDeviceContext*)device_ref->data;
  517. if (!device_ctx->internal->hw_type->device_create) {
  518. ret = AVERROR(ENOSYS);
  519. goto fail;
  520. }
  521. ret = device_ctx->internal->hw_type->device_create(device_ctx, device,
  522. opts, flags);
  523. if (ret < 0)
  524. goto fail;
  525. ret = av_hwdevice_ctx_init(device_ref);
  526. if (ret < 0)
  527. goto fail;
  528. *pdevice_ref = device_ref;
  529. return 0;
  530. fail:
  531. av_buffer_unref(&device_ref);
  532. *pdevice_ref = NULL;
  533. return ret;
  534. }
  535. int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ref_ptr,
  536. enum AVHWDeviceType type,
  537. AVBufferRef *src_ref, int flags)
  538. {
  539. AVBufferRef *dst_ref = NULL, *tmp_ref;
  540. AVHWDeviceContext *dst_ctx, *tmp_ctx;
  541. int ret = 0;
  542. tmp_ref = src_ref;
  543. while (tmp_ref) {
  544. tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
  545. if (tmp_ctx->type == type) {
  546. dst_ref = av_buffer_ref(tmp_ref);
  547. if (!dst_ref) {
  548. ret = AVERROR(ENOMEM);
  549. goto fail;
  550. }
  551. goto done;
  552. }
  553. tmp_ref = tmp_ctx->internal->source_device;
  554. }
  555. dst_ref = av_hwdevice_ctx_alloc(type);
  556. if (!dst_ref) {
  557. ret = AVERROR(ENOMEM);
  558. goto fail;
  559. }
  560. dst_ctx = (AVHWDeviceContext*)dst_ref->data;
  561. tmp_ref = src_ref;
  562. while (tmp_ref) {
  563. tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
  564. if (dst_ctx->internal->hw_type->device_derive) {
  565. ret = dst_ctx->internal->hw_type->device_derive(dst_ctx,
  566. tmp_ctx,
  567. flags);
  568. if (ret == 0) {
  569. dst_ctx->internal->source_device = av_buffer_ref(src_ref);
  570. if (!dst_ctx->internal->source_device) {
  571. ret = AVERROR(ENOMEM);
  572. goto fail;
  573. }
  574. ret = av_hwdevice_ctx_init(dst_ref);
  575. if (ret < 0)
  576. goto fail;
  577. goto done;
  578. }
  579. if (ret != AVERROR(ENOSYS))
  580. goto fail;
  581. }
  582. tmp_ref = tmp_ctx->internal->source_device;
  583. }
  584. ret = AVERROR(ENOSYS);
  585. goto fail;
  586. done:
  587. *dst_ref_ptr = dst_ref;
  588. return 0;
  589. fail:
  590. av_buffer_unref(&dst_ref);
  591. *dst_ref_ptr = NULL;
  592. return ret;
  593. }
  594. static void ff_hwframe_unmap(void *opaque, uint8_t *data)
  595. {
  596. HWMapDescriptor *hwmap = (HWMapDescriptor*)data;
  597. AVHWFramesContext *ctx = opaque;
  598. if (hwmap->unmap)
  599. hwmap->unmap(ctx, hwmap);
  600. av_frame_free(&hwmap->source);
  601. av_buffer_unref(&hwmap->hw_frames_ctx);
  602. av_free(hwmap);
  603. }
  604. int ff_hwframe_map_create(AVBufferRef *hwframe_ref,
  605. AVFrame *dst, const AVFrame *src,
  606. void (*unmap)(AVHWFramesContext *ctx,
  607. HWMapDescriptor *hwmap),
  608. void *priv)
  609. {
  610. AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
  611. HWMapDescriptor *hwmap;
  612. int ret;
  613. hwmap = av_mallocz(sizeof(*hwmap));
  614. if (!hwmap) {
  615. ret = AVERROR(ENOMEM);
  616. goto fail;
  617. }
  618. hwmap->source = av_frame_alloc();
  619. if (!hwmap->source) {
  620. ret = AVERROR(ENOMEM);
  621. goto fail;
  622. }
  623. ret = av_frame_ref(hwmap->source, src);
  624. if (ret < 0)
  625. goto fail;
  626. hwmap->hw_frames_ctx = av_buffer_ref(hwframe_ref);
  627. if (!hwmap->hw_frames_ctx) {
  628. ret = AVERROR(ENOMEM);
  629. goto fail;
  630. }
  631. hwmap->unmap = unmap;
  632. hwmap->priv = priv;
  633. dst->buf[0] = av_buffer_create((uint8_t*)hwmap, sizeof(*hwmap),
  634. &ff_hwframe_unmap, ctx, 0);
  635. if (!dst->buf[0]) {
  636. ret = AVERROR(ENOMEM);
  637. goto fail;
  638. }
  639. return 0;
  640. fail:
  641. if (hwmap) {
  642. av_buffer_unref(&hwmap->hw_frames_ctx);
  643. av_frame_free(&hwmap->source);
  644. }
  645. av_free(hwmap);
  646. return ret;
  647. }
  648. int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
  649. {
  650. AVHWFramesContext *src_frames, *dst_frames;
  651. HWMapDescriptor *hwmap;
  652. int ret;
  653. if (src->hw_frames_ctx && dst->hw_frames_ctx) {
  654. src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
  655. dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
  656. if ((src_frames == dst_frames &&
  657. src->format == dst_frames->sw_format &&
  658. dst->format == dst_frames->format) ||
  659. (src_frames->internal->source_frames &&
  660. src_frames->internal->source_frames->data ==
  661. (uint8_t*)dst_frames)) {
  662. // This is an unmap operation. We don't need to directly
  663. // do anything here other than fill in the original frame,
  664. // because the real unmap will be invoked when the last
  665. // reference to the mapped frame disappears.
  666. if (!src->buf[0]) {
  667. av_log(src_frames, AV_LOG_ERROR, "Invalid mapping "
  668. "found when attempting unmap.\n");
  669. return AVERROR(EINVAL);
  670. }
  671. hwmap = (HWMapDescriptor*)src->buf[0]->data;
  672. av_frame_unref(dst);
  673. return av_frame_ref(dst, hwmap->source);
  674. }
  675. }
  676. if (src->hw_frames_ctx) {
  677. src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
  678. if (src_frames->format == src->format &&
  679. src_frames->internal->hw_type->map_from) {
  680. ret = src_frames->internal->hw_type->map_from(src_frames,
  681. dst, src, flags);
  682. if (ret != AVERROR(ENOSYS))
  683. return ret;
  684. }
  685. }
  686. if (dst->hw_frames_ctx) {
  687. dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
  688. if (dst_frames->format == dst->format &&
  689. dst_frames->internal->hw_type->map_to) {
  690. ret = dst_frames->internal->hw_type->map_to(dst_frames,
  691. dst, src, flags);
  692. if (ret != AVERROR(ENOSYS))
  693. return ret;
  694. }
  695. }
  696. return AVERROR(ENOSYS);
  697. }
  698. int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
  699. enum AVPixelFormat format,
  700. AVBufferRef *derived_device_ctx,
  701. AVBufferRef *source_frame_ctx,
  702. int flags)
  703. {
  704. AVBufferRef *dst_ref = NULL;
  705. AVHWFramesContext *dst = NULL;
  706. AVHWFramesContext *src = (AVHWFramesContext*)source_frame_ctx->data;
  707. int ret;
  708. if (src->internal->source_frames) {
  709. AVHWFramesContext *src_src =
  710. (AVHWFramesContext*)src->internal->source_frames->data;
  711. AVHWDeviceContext *dst_dev =
  712. (AVHWDeviceContext*)derived_device_ctx->data;
  713. if (src_src->device_ctx == dst_dev) {
  714. // This is actually an unmapping, so we just return a
  715. // reference to the source frame context.
  716. *derived_frame_ctx =
  717. av_buffer_ref(src->internal->source_frames);
  718. if (!*derived_frame_ctx) {
  719. ret = AVERROR(ENOMEM);
  720. goto fail;
  721. }
  722. return 0;
  723. }
  724. }
  725. dst_ref = av_hwframe_ctx_alloc(derived_device_ctx);
  726. if (!dst_ref) {
  727. ret = AVERROR(ENOMEM);
  728. goto fail;
  729. }
  730. dst = (AVHWFramesContext*)dst_ref->data;
  731. dst->format = format;
  732. dst->sw_format = src->sw_format;
  733. dst->width = src->width;
  734. dst->height = src->height;
  735. dst->internal->source_frames = av_buffer_ref(source_frame_ctx);
  736. if (!dst->internal->source_frames) {
  737. ret = AVERROR(ENOMEM);
  738. goto fail;
  739. }
  740. dst->internal->source_allocation_map_flags =
  741. flags & (AV_HWFRAME_MAP_READ |
  742. AV_HWFRAME_MAP_WRITE |
  743. AV_HWFRAME_MAP_OVERWRITE |
  744. AV_HWFRAME_MAP_DIRECT);
  745. ret = AVERROR(ENOSYS);
  746. if (src->internal->hw_type->frames_derive_from)
  747. ret = src->internal->hw_type->frames_derive_from(dst, src, flags);
  748. if (ret == AVERROR(ENOSYS) &&
  749. dst->internal->hw_type->frames_derive_to)
  750. ret = dst->internal->hw_type->frames_derive_to(dst, src, flags);
  751. if (ret == AVERROR(ENOSYS))
  752. ret = 0;
  753. if (ret)
  754. goto fail;
  755. *derived_frame_ctx = dst_ref;
  756. return 0;
  757. fail:
  758. if (dst)
  759. av_buffer_unref(&dst->internal->source_frames);
  760. av_buffer_unref(&dst_ref);
  761. return ret;
  762. }
  763. int ff_hwframe_map_replace(AVFrame *dst, const AVFrame *src)
  764. {
  765. HWMapDescriptor *hwmap = (HWMapDescriptor*)dst->buf[0]->data;
  766. av_frame_unref(hwmap->source);
  767. return av_frame_ref(hwmap->source, src);
  768. }