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.

866 lines
24KB

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