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.

858 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->pool_internal)
  179. av_buffer_pool_uninit(&ctx->internal->pool_internal);
  180. if (ctx->internal->hw_type->frames_uninit)
  181. ctx->internal->hw_type->frames_uninit(ctx);
  182. if (ctx->free)
  183. ctx->free(ctx);
  184. av_buffer_unref(&ctx->internal->source_frames);
  185. av_buffer_unref(&ctx->device_ref);
  186. av_freep(&ctx->hwctx);
  187. av_freep(&ctx->internal->priv);
  188. av_freep(&ctx->internal);
  189. av_freep(&ctx);
  190. }
  191. AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
  192. {
  193. AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref_in->data;
  194. const HWContextType *hw_type = device_ctx->internal->hw_type;
  195. AVHWFramesContext *ctx;
  196. AVBufferRef *buf, *device_ref = NULL;;
  197. ctx = av_mallocz(sizeof(*ctx));
  198. if (!ctx)
  199. return NULL;
  200. ctx->internal = av_mallocz(sizeof(*ctx->internal));
  201. if (!ctx->internal)
  202. goto fail;
  203. if (hw_type->frames_priv_size) {
  204. ctx->internal->priv = av_mallocz(hw_type->frames_priv_size);
  205. if (!ctx->internal->priv)
  206. goto fail;
  207. }
  208. if (hw_type->frames_hwctx_size) {
  209. ctx->hwctx = av_mallocz(hw_type->frames_hwctx_size);
  210. if (!ctx->hwctx)
  211. goto fail;
  212. }
  213. device_ref = av_buffer_ref(device_ref_in);
  214. if (!device_ref)
  215. goto fail;
  216. buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
  217. hwframe_ctx_free, NULL,
  218. AV_BUFFER_FLAG_READONLY);
  219. if (!buf)
  220. goto fail;
  221. ctx->av_class = &hwframe_ctx_class;
  222. ctx->device_ref = device_ref;
  223. ctx->device_ctx = device_ctx;
  224. ctx->format = AV_PIX_FMT_NONE;
  225. ctx->sw_format = AV_PIX_FMT_NONE;
  226. ctx->internal->hw_type = hw_type;
  227. return buf;
  228. fail:
  229. if (device_ref)
  230. av_buffer_unref(&device_ref);
  231. if (ctx->internal)
  232. av_freep(&ctx->internal->priv);
  233. av_freep(&ctx->internal);
  234. av_freep(&ctx->hwctx);
  235. av_freep(&ctx);
  236. return NULL;
  237. }
  238. static int hwframe_pool_prealloc(AVBufferRef *ref)
  239. {
  240. AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
  241. AVFrame **frames;
  242. int i, ret = 0;
  243. frames = av_mallocz_array(ctx->initial_pool_size, sizeof(*frames));
  244. if (!frames)
  245. return AVERROR(ENOMEM);
  246. for (i = 0; i < ctx->initial_pool_size; i++) {
  247. frames[i] = av_frame_alloc();
  248. if (!frames[i])
  249. goto fail;
  250. ret = av_hwframe_get_buffer(ref, frames[i], 0);
  251. if (ret < 0)
  252. goto fail;
  253. }
  254. fail:
  255. for (i = 0; i < ctx->initial_pool_size; i++)
  256. av_frame_free(&frames[i]);
  257. av_freep(&frames);
  258. return ret;
  259. }
  260. int av_hwframe_ctx_init(AVBufferRef *ref)
  261. {
  262. AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
  263. const enum AVPixelFormat *pix_fmt;
  264. int ret;
  265. if (ctx->internal->source_frames) {
  266. /* A derived frame context is already initialised. */
  267. return 0;
  268. }
  269. /* validate the pixel format */
  270. for (pix_fmt = ctx->internal->hw_type->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) {
  271. if (*pix_fmt == ctx->format)
  272. break;
  273. }
  274. if (*pix_fmt == AV_PIX_FMT_NONE) {
  275. av_log(ctx, AV_LOG_ERROR,
  276. "The hardware pixel format '%s' is not supported by the device type '%s'\n",
  277. av_get_pix_fmt_name(ctx->format), ctx->internal->hw_type->name);
  278. return AVERROR(ENOSYS);
  279. }
  280. /* validate the dimensions */
  281. ret = av_image_check_size(ctx->width, ctx->height, 0, ctx);
  282. if (ret < 0)
  283. return ret;
  284. /* format-specific init */
  285. if (ctx->internal->hw_type->frames_init) {
  286. ret = ctx->internal->hw_type->frames_init(ctx);
  287. if (ret < 0)
  288. goto fail;
  289. }
  290. if (ctx->internal->pool_internal && !ctx->pool)
  291. ctx->pool = ctx->internal->pool_internal;
  292. /* preallocate the frames in the pool, if requested */
  293. if (ctx->initial_pool_size > 0) {
  294. ret = hwframe_pool_prealloc(ref);
  295. if (ret < 0)
  296. goto fail;
  297. }
  298. return 0;
  299. fail:
  300. if (ctx->internal->hw_type->frames_uninit)
  301. ctx->internal->hw_type->frames_uninit(ctx);
  302. return ret;
  303. }
  304. int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref,
  305. enum AVHWFrameTransferDirection dir,
  306. enum AVPixelFormat **formats, int flags)
  307. {
  308. AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
  309. if (!ctx->internal->hw_type->transfer_get_formats)
  310. return AVERROR(ENOSYS);
  311. return ctx->internal->hw_type->transfer_get_formats(ctx, dir, formats);
  312. }
  313. static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
  314. {
  315. AVHWFramesContext *ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
  316. AVFrame *frame_tmp;
  317. int ret = 0;
  318. frame_tmp = av_frame_alloc();
  319. if (!frame_tmp)
  320. return AVERROR(ENOMEM);
  321. /* if the format is set, use that
  322. * otherwise pick the first supported one */
  323. if (dst->format >= 0) {
  324. frame_tmp->format = dst->format;
  325. } else {
  326. enum AVPixelFormat *formats;
  327. ret = av_hwframe_transfer_get_formats(src->hw_frames_ctx,
  328. AV_HWFRAME_TRANSFER_DIRECTION_FROM,
  329. &formats, 0);
  330. if (ret < 0)
  331. goto fail;
  332. frame_tmp->format = formats[0];
  333. av_freep(&formats);
  334. }
  335. frame_tmp->width = ctx->width;
  336. frame_tmp->height = ctx->height;
  337. ret = av_frame_get_buffer(frame_tmp, 32);
  338. if (ret < 0)
  339. goto fail;
  340. ret = av_hwframe_transfer_data(frame_tmp, src, flags);
  341. if (ret < 0)
  342. goto fail;
  343. frame_tmp->width = src->width;
  344. frame_tmp->height = src->height;
  345. av_frame_move_ref(dst, frame_tmp);
  346. fail:
  347. av_frame_free(&frame_tmp);
  348. return ret;
  349. }
  350. int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
  351. {
  352. AVHWFramesContext *ctx;
  353. int ret;
  354. if (!dst->buf[0])
  355. return transfer_data_alloc(dst, src, flags);
  356. if (src->hw_frames_ctx) {
  357. ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
  358. ret = ctx->internal->hw_type->transfer_data_from(ctx, dst, src);
  359. if (ret < 0)
  360. return ret;
  361. } else if (dst->hw_frames_ctx) {
  362. ctx = (AVHWFramesContext*)dst->hw_frames_ctx->data;
  363. ret = ctx->internal->hw_type->transfer_data_to(ctx, dst, src);
  364. if (ret < 0)
  365. return ret;
  366. } else
  367. return AVERROR(ENOSYS);
  368. return 0;
  369. }
  370. int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
  371. {
  372. AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
  373. int ret;
  374. if (ctx->internal->source_frames) {
  375. // This is a derived frame context, so we allocate in the source
  376. // and map the frame immediately.
  377. AVFrame *src_frame;
  378. frame->format = ctx->format;
  379. frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
  380. if (!frame->hw_frames_ctx)
  381. return AVERROR(ENOMEM);
  382. src_frame = av_frame_alloc();
  383. if (!src_frame)
  384. return AVERROR(ENOMEM);
  385. ret = av_hwframe_get_buffer(ctx->internal->source_frames,
  386. src_frame, 0);
  387. if (ret < 0) {
  388. av_frame_free(&src_frame);
  389. return ret;
  390. }
  391. ret = av_hwframe_map(frame, src_frame,
  392. ctx->internal->source_allocation_map_flags);
  393. if (ret) {
  394. av_log(ctx, AV_LOG_ERROR, "Failed to map frame into derived "
  395. "frame context: %d.\n", ret);
  396. av_frame_free(&src_frame);
  397. return ret;
  398. }
  399. // Free the source frame immediately - the mapped frame still
  400. // contains a reference to it.
  401. av_frame_free(&src_frame);
  402. return 0;
  403. }
  404. if (!ctx->internal->hw_type->frames_get_buffer)
  405. return AVERROR(ENOSYS);
  406. if (!ctx->pool)
  407. return AVERROR(EINVAL);
  408. frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
  409. if (!frame->hw_frames_ctx)
  410. return AVERROR(ENOMEM);
  411. ret = ctx->internal->hw_type->frames_get_buffer(ctx, frame);
  412. if (ret < 0) {
  413. av_buffer_unref(&frame->hw_frames_ctx);
  414. return ret;
  415. }
  416. return 0;
  417. }
  418. void *av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
  419. {
  420. AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
  421. const HWContextType *hw_type = ctx->internal->hw_type;
  422. if (hw_type->device_hwconfig_size == 0)
  423. return NULL;
  424. return av_mallocz(hw_type->device_hwconfig_size);
  425. }
  426. AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
  427. const void *hwconfig)
  428. {
  429. AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
  430. const HWContextType *hw_type = ctx->internal->hw_type;
  431. AVHWFramesConstraints *constraints;
  432. if (!hw_type->frames_get_constraints)
  433. return NULL;
  434. constraints = av_mallocz(sizeof(*constraints));
  435. if (!constraints)
  436. return NULL;
  437. constraints->min_width = constraints->min_height = 0;
  438. constraints->max_width = constraints->max_height = INT_MAX;
  439. if (hw_type->frames_get_constraints(ctx, hwconfig, constraints) >= 0) {
  440. return constraints;
  441. } else {
  442. av_hwframe_constraints_free(&constraints);
  443. return NULL;
  444. }
  445. }
  446. void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
  447. {
  448. if (*constraints) {
  449. av_freep(&(*constraints)->valid_hw_formats);
  450. av_freep(&(*constraints)->valid_sw_formats);
  451. }
  452. av_freep(constraints);
  453. }
  454. int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type,
  455. const char *device, AVDictionary *opts, int flags)
  456. {
  457. AVBufferRef *device_ref = NULL;
  458. AVHWDeviceContext *device_ctx;
  459. int ret = 0;
  460. device_ref = av_hwdevice_ctx_alloc(type);
  461. if (!device_ref) {
  462. ret = AVERROR(ENOMEM);
  463. goto fail;
  464. }
  465. device_ctx = (AVHWDeviceContext*)device_ref->data;
  466. if (!device_ctx->internal->hw_type->device_create) {
  467. ret = AVERROR(ENOSYS);
  468. goto fail;
  469. }
  470. ret = device_ctx->internal->hw_type->device_create(device_ctx, device,
  471. opts, flags);
  472. if (ret < 0)
  473. goto fail;
  474. ret = av_hwdevice_ctx_init(device_ref);
  475. if (ret < 0)
  476. goto fail;
  477. *pdevice_ref = device_ref;
  478. return 0;
  479. fail:
  480. av_buffer_unref(&device_ref);
  481. *pdevice_ref = NULL;
  482. return ret;
  483. }
  484. int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ref_ptr,
  485. enum AVHWDeviceType type,
  486. AVBufferRef *src_ref, int flags)
  487. {
  488. AVBufferRef *dst_ref = NULL, *tmp_ref;
  489. AVHWDeviceContext *dst_ctx, *tmp_ctx;
  490. int ret = 0;
  491. tmp_ref = src_ref;
  492. while (tmp_ref) {
  493. tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
  494. if (tmp_ctx->type == type) {
  495. dst_ref = av_buffer_ref(tmp_ref);
  496. if (!dst_ref) {
  497. ret = AVERROR(ENOMEM);
  498. goto fail;
  499. }
  500. goto done;
  501. }
  502. tmp_ref = tmp_ctx->internal->source_device;
  503. }
  504. dst_ref = av_hwdevice_ctx_alloc(type);
  505. if (!dst_ref) {
  506. ret = AVERROR(ENOMEM);
  507. goto fail;
  508. }
  509. dst_ctx = (AVHWDeviceContext*)dst_ref->data;
  510. tmp_ref = src_ref;
  511. while (tmp_ref) {
  512. tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
  513. if (dst_ctx->internal->hw_type->device_derive) {
  514. ret = dst_ctx->internal->hw_type->device_derive(dst_ctx,
  515. tmp_ctx,
  516. flags);
  517. if (ret == 0) {
  518. dst_ctx->internal->source_device = av_buffer_ref(src_ref);
  519. if (!dst_ctx->internal->source_device) {
  520. ret = AVERROR(ENOMEM);
  521. goto fail;
  522. }
  523. goto done;
  524. }
  525. if (ret != AVERROR(ENOSYS))
  526. goto fail;
  527. }
  528. tmp_ref = tmp_ctx->internal->source_device;
  529. }
  530. ret = AVERROR(ENOSYS);
  531. goto fail;
  532. done:
  533. ret = av_hwdevice_ctx_init(dst_ref);
  534. if (ret < 0)
  535. goto fail;
  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. }