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.

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