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.

857 lines
23KB

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