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.

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