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.

728 lines
20KB

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