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.

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