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.

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