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.

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