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.

855 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. return ret;
  392. ret = av_hwframe_map(frame, src_frame,
  393. ctx->internal->source_allocation_map_flags);
  394. if (ret) {
  395. av_log(ctx, AV_LOG_ERROR, "Failed to map frame into derived "
  396. "frame context: %d.\n", ret);
  397. av_frame_free(&src_frame);
  398. return ret;
  399. }
  400. // Free the source frame immediately - the mapped frame still
  401. // contains a reference to it.
  402. av_frame_free(&src_frame);
  403. return 0;
  404. }
  405. if (!ctx->internal->hw_type->frames_get_buffer)
  406. return AVERROR(ENOSYS);
  407. if (!ctx->pool)
  408. return AVERROR(EINVAL);
  409. frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
  410. if (!frame->hw_frames_ctx)
  411. return AVERROR(ENOMEM);
  412. ret = ctx->internal->hw_type->frames_get_buffer(ctx, frame);
  413. if (ret < 0) {
  414. av_buffer_unref(&frame->hw_frames_ctx);
  415. return ret;
  416. }
  417. return 0;
  418. }
  419. void *av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
  420. {
  421. AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
  422. const HWContextType *hw_type = ctx->internal->hw_type;
  423. if (hw_type->device_hwconfig_size == 0)
  424. return NULL;
  425. return av_mallocz(hw_type->device_hwconfig_size);
  426. }
  427. AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
  428. const void *hwconfig)
  429. {
  430. AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
  431. const HWContextType *hw_type = ctx->internal->hw_type;
  432. AVHWFramesConstraints *constraints;
  433. if (!hw_type->frames_get_constraints)
  434. return NULL;
  435. constraints = av_mallocz(sizeof(*constraints));
  436. if (!constraints)
  437. return NULL;
  438. constraints->min_width = constraints->min_height = 0;
  439. constraints->max_width = constraints->max_height = INT_MAX;
  440. if (hw_type->frames_get_constraints(ctx, hwconfig, constraints) >= 0) {
  441. return constraints;
  442. } else {
  443. av_hwframe_constraints_free(&constraints);
  444. return NULL;
  445. }
  446. }
  447. void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
  448. {
  449. if (*constraints) {
  450. av_freep(&(*constraints)->valid_hw_formats);
  451. av_freep(&(*constraints)->valid_sw_formats);
  452. }
  453. av_freep(constraints);
  454. }
  455. int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type,
  456. const char *device, AVDictionary *opts, int flags)
  457. {
  458. AVBufferRef *device_ref = NULL;
  459. AVHWDeviceContext *device_ctx;
  460. int ret = 0;
  461. device_ref = av_hwdevice_ctx_alloc(type);
  462. if (!device_ref) {
  463. ret = AVERROR(ENOMEM);
  464. goto fail;
  465. }
  466. device_ctx = (AVHWDeviceContext*)device_ref->data;
  467. if (!device_ctx->internal->hw_type->device_create) {
  468. ret = AVERROR(ENOSYS);
  469. goto fail;
  470. }
  471. ret = device_ctx->internal->hw_type->device_create(device_ctx, device,
  472. opts, flags);
  473. if (ret < 0)
  474. goto fail;
  475. ret = av_hwdevice_ctx_init(device_ref);
  476. if (ret < 0)
  477. goto fail;
  478. *pdevice_ref = device_ref;
  479. return 0;
  480. fail:
  481. av_buffer_unref(&device_ref);
  482. *pdevice_ref = NULL;
  483. return ret;
  484. }
  485. int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ref_ptr,
  486. enum AVHWDeviceType type,
  487. AVBufferRef *src_ref, int flags)
  488. {
  489. AVBufferRef *dst_ref = NULL, *tmp_ref;
  490. AVHWDeviceContext *dst_ctx, *tmp_ctx;
  491. int ret = 0;
  492. tmp_ref = src_ref;
  493. while (tmp_ref) {
  494. tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
  495. if (tmp_ctx->type == type) {
  496. dst_ref = av_buffer_ref(tmp_ref);
  497. if (!dst_ref) {
  498. ret = AVERROR(ENOMEM);
  499. goto fail;
  500. }
  501. goto done;
  502. }
  503. tmp_ref = tmp_ctx->internal->source_device;
  504. }
  505. dst_ref = av_hwdevice_ctx_alloc(type);
  506. if (!dst_ref) {
  507. ret = AVERROR(ENOMEM);
  508. goto fail;
  509. }
  510. dst_ctx = (AVHWDeviceContext*)dst_ref->data;
  511. tmp_ref = src_ref;
  512. while (tmp_ref) {
  513. tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
  514. if (dst_ctx->internal->hw_type->device_derive) {
  515. ret = dst_ctx->internal->hw_type->device_derive(dst_ctx,
  516. tmp_ctx,
  517. flags);
  518. if (ret == 0) {
  519. dst_ctx->internal->source_device = av_buffer_ref(src_ref);
  520. if (!dst_ctx->internal->source_device) {
  521. ret = AVERROR(ENOMEM);
  522. goto fail;
  523. }
  524. goto done;
  525. }
  526. if (ret != AVERROR(ENOSYS))
  527. goto fail;
  528. }
  529. tmp_ref = tmp_ctx->internal->source_device;
  530. }
  531. ret = AVERROR(ENOSYS);
  532. goto fail;
  533. done:
  534. *dst_ref_ptr = dst_ref;
  535. return 0;
  536. fail:
  537. av_buffer_unref(&dst_ref);
  538. *dst_ref_ptr = NULL;
  539. return ret;
  540. }
  541. static void ff_hwframe_unmap(void *opaque, uint8_t *data)
  542. {
  543. HWMapDescriptor *hwmap = (HWMapDescriptor*)data;
  544. AVHWFramesContext *ctx = opaque;
  545. if (hwmap->unmap)
  546. hwmap->unmap(ctx, hwmap);
  547. av_frame_free(&hwmap->source);
  548. av_buffer_unref(&hwmap->hw_frames_ctx);
  549. av_free(hwmap);
  550. }
  551. int ff_hwframe_map_create(AVBufferRef *hwframe_ref,
  552. AVFrame *dst, const AVFrame *src,
  553. void (*unmap)(AVHWFramesContext *ctx,
  554. HWMapDescriptor *hwmap),
  555. void *priv)
  556. {
  557. AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
  558. HWMapDescriptor *hwmap;
  559. int ret;
  560. hwmap = av_mallocz(sizeof(*hwmap));
  561. if (!hwmap) {
  562. ret = AVERROR(ENOMEM);
  563. goto fail;
  564. }
  565. hwmap->source = av_frame_alloc();
  566. if (!hwmap->source) {
  567. ret = AVERROR(ENOMEM);
  568. goto fail;
  569. }
  570. ret = av_frame_ref(hwmap->source, src);
  571. if (ret < 0)
  572. goto fail;
  573. hwmap->hw_frames_ctx = av_buffer_ref(hwframe_ref);
  574. if (!hwmap->hw_frames_ctx) {
  575. ret = AVERROR(ENOMEM);
  576. goto fail;
  577. }
  578. hwmap->unmap = unmap;
  579. hwmap->priv = priv;
  580. dst->buf[0] = av_buffer_create((uint8_t*)hwmap, sizeof(*hwmap),
  581. &ff_hwframe_unmap, ctx, 0);
  582. if (!dst->buf[0]) {
  583. ret = AVERROR(ENOMEM);
  584. goto fail;
  585. }
  586. return 0;
  587. fail:
  588. if (hwmap) {
  589. av_buffer_unref(&hwmap->hw_frames_ctx);
  590. av_frame_free(&hwmap->source);
  591. }
  592. av_free(hwmap);
  593. return ret;
  594. }
  595. int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
  596. {
  597. AVHWFramesContext *src_frames, *dst_frames;
  598. HWMapDescriptor *hwmap;
  599. int ret;
  600. if (src->hw_frames_ctx && dst->hw_frames_ctx) {
  601. src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
  602. dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
  603. if ((src_frames == dst_frames &&
  604. src->format == dst_frames->sw_format &&
  605. dst->format == dst_frames->format) ||
  606. (src_frames->internal->source_frames &&
  607. src_frames->internal->source_frames->data ==
  608. (uint8_t*)dst_frames)) {
  609. // This is an unmap operation. We don't need to directly
  610. // do anything here other than fill in the original frame,
  611. // because the real unmap will be invoked when the last
  612. // reference to the mapped frame disappears.
  613. if (!src->buf[0]) {
  614. av_log(src_frames, AV_LOG_ERROR, "Invalid mapping "
  615. "found when attempting unmap.\n");
  616. return AVERROR(EINVAL);
  617. }
  618. hwmap = (HWMapDescriptor*)src->buf[0]->data;
  619. av_frame_unref(dst);
  620. return av_frame_ref(dst, hwmap->source);
  621. }
  622. }
  623. if (src->hw_frames_ctx) {
  624. src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
  625. if (src_frames->format == src->format &&
  626. src_frames->internal->hw_type->map_from) {
  627. ret = src_frames->internal->hw_type->map_from(src_frames,
  628. dst, src, flags);
  629. if (ret != AVERROR(ENOSYS))
  630. return ret;
  631. }
  632. }
  633. if (dst->hw_frames_ctx) {
  634. dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
  635. if (dst_frames->format == dst->format &&
  636. dst_frames->internal->hw_type->map_to) {
  637. ret = dst_frames->internal->hw_type->map_to(dst_frames,
  638. dst, src, flags);
  639. if (ret != AVERROR(ENOSYS))
  640. return ret;
  641. }
  642. }
  643. return AVERROR(ENOSYS);
  644. }
  645. int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
  646. enum AVPixelFormat format,
  647. AVBufferRef *derived_device_ctx,
  648. AVBufferRef *source_frame_ctx,
  649. int flags)
  650. {
  651. AVBufferRef *dst_ref = NULL;
  652. AVHWFramesContext *dst = NULL;
  653. AVHWFramesContext *src = (AVHWFramesContext*)source_frame_ctx->data;
  654. int ret;
  655. if (src->internal->source_frames) {
  656. AVHWFramesContext *src_src =
  657. (AVHWFramesContext*)src->internal->source_frames->data;
  658. AVHWDeviceContext *dst_dev =
  659. (AVHWDeviceContext*)derived_device_ctx->data;
  660. if (src_src->device_ctx == dst_dev) {
  661. // This is actually an unmapping, so we just return a
  662. // reference to the source frame context.
  663. *derived_frame_ctx =
  664. av_buffer_ref(src->internal->source_frames);
  665. if (!*derived_frame_ctx) {
  666. ret = AVERROR(ENOMEM);
  667. goto fail;
  668. }
  669. return 0;
  670. }
  671. }
  672. dst_ref = av_hwframe_ctx_alloc(derived_device_ctx);
  673. if (!dst_ref) {
  674. ret = AVERROR(ENOMEM);
  675. goto fail;
  676. }
  677. dst = (AVHWFramesContext*)dst_ref->data;
  678. dst->format = format;
  679. dst->sw_format = src->sw_format;
  680. dst->width = src->width;
  681. dst->height = src->height;
  682. dst->internal->source_frames = av_buffer_ref(source_frame_ctx);
  683. if (!dst->internal->source_frames) {
  684. ret = AVERROR(ENOMEM);
  685. goto fail;
  686. }
  687. dst->internal->source_allocation_map_flags =
  688. flags & (AV_HWFRAME_MAP_READ |
  689. AV_HWFRAME_MAP_WRITE |
  690. AV_HWFRAME_MAP_OVERWRITE |
  691. AV_HWFRAME_MAP_DIRECT);
  692. ret = AVERROR(ENOSYS);
  693. if (src->internal->hw_type->frames_derive_from)
  694. ret = src->internal->hw_type->frames_derive_from(dst, src, flags);
  695. if (ret == AVERROR(ENOSYS) &&
  696. dst->internal->hw_type->frames_derive_to)
  697. ret = dst->internal->hw_type->frames_derive_to(dst, src, flags);
  698. if (ret == AVERROR(ENOSYS))
  699. ret = 0;
  700. if (ret)
  701. goto fail;
  702. *derived_frame_ctx = dst_ref;
  703. return 0;
  704. fail:
  705. if (dst)
  706. av_buffer_unref(&dst->internal->source_frames);
  707. av_buffer_unref(&dst_ref);
  708. return ret;
  709. }