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.

831 lines
22KB

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