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.

793 lines
21KB

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