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.

858 lines
23KB

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