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.

868 lines
24KB

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