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.

835 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. src_frame = av_frame_alloc();
  381. if (!src_frame)
  382. return AVERROR(ENOMEM);
  383. ret = av_hwframe_get_buffer(ctx->internal->source_frames,
  384. src_frame, 0);
  385. if (ret < 0)
  386. return ret;
  387. ret = av_hwframe_map(frame, src_frame, 0);
  388. if (ret) {
  389. av_log(ctx, AV_LOG_ERROR, "Failed to map frame into derived "
  390. "frame context: %d.\n", ret);
  391. av_frame_free(&src_frame);
  392. return ret;
  393. }
  394. // Free the source frame immediately - the mapped frame still
  395. // contains a reference to it.
  396. av_frame_free(&src_frame);
  397. return 0;
  398. }
  399. if (!ctx->internal->hw_type->frames_get_buffer)
  400. return AVERROR(ENOSYS);
  401. if (!ctx->pool)
  402. return AVERROR(EINVAL);
  403. frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
  404. if (!frame->hw_frames_ctx)
  405. return AVERROR(ENOMEM);
  406. ret = ctx->internal->hw_type->frames_get_buffer(ctx, frame);
  407. if (ret < 0) {
  408. av_buffer_unref(&frame->hw_frames_ctx);
  409. return ret;
  410. }
  411. return 0;
  412. }
  413. void *av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
  414. {
  415. AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
  416. const HWContextType *hw_type = ctx->internal->hw_type;
  417. if (hw_type->device_hwconfig_size == 0)
  418. return NULL;
  419. return av_mallocz(hw_type->device_hwconfig_size);
  420. }
  421. AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
  422. const void *hwconfig)
  423. {
  424. AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
  425. const HWContextType *hw_type = ctx->internal->hw_type;
  426. AVHWFramesConstraints *constraints;
  427. if (!hw_type->frames_get_constraints)
  428. return NULL;
  429. constraints = av_mallocz(sizeof(*constraints));
  430. if (!constraints)
  431. return NULL;
  432. constraints->min_width = constraints->min_height = 0;
  433. constraints->max_width = constraints->max_height = INT_MAX;
  434. if (hw_type->frames_get_constraints(ctx, hwconfig, constraints) >= 0) {
  435. return constraints;
  436. } else {
  437. av_hwframe_constraints_free(&constraints);
  438. return NULL;
  439. }
  440. }
  441. void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
  442. {
  443. if (*constraints) {
  444. av_freep(&(*constraints)->valid_hw_formats);
  445. av_freep(&(*constraints)->valid_sw_formats);
  446. }
  447. av_freep(constraints);
  448. }
  449. int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type,
  450. const char *device, AVDictionary *opts, int flags)
  451. {
  452. AVBufferRef *device_ref = NULL;
  453. AVHWDeviceContext *device_ctx;
  454. int ret = 0;
  455. device_ref = av_hwdevice_ctx_alloc(type);
  456. if (!device_ref) {
  457. ret = AVERROR(ENOMEM);
  458. goto fail;
  459. }
  460. device_ctx = (AVHWDeviceContext*)device_ref->data;
  461. if (!device_ctx->internal->hw_type->device_create) {
  462. ret = AVERROR(ENOSYS);
  463. goto fail;
  464. }
  465. ret = device_ctx->internal->hw_type->device_create(device_ctx, device,
  466. opts, flags);
  467. if (ret < 0)
  468. goto fail;
  469. ret = av_hwdevice_ctx_init(device_ref);
  470. if (ret < 0)
  471. goto fail;
  472. *pdevice_ref = device_ref;
  473. return 0;
  474. fail:
  475. av_buffer_unref(&device_ref);
  476. *pdevice_ref = NULL;
  477. return ret;
  478. }
  479. int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ref_ptr,
  480. enum AVHWDeviceType type,
  481. AVBufferRef *src_ref, int flags)
  482. {
  483. AVBufferRef *dst_ref = NULL, *tmp_ref;
  484. AVHWDeviceContext *dst_ctx, *tmp_ctx;
  485. int ret = 0;
  486. tmp_ref = src_ref;
  487. while (tmp_ref) {
  488. tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
  489. if (tmp_ctx->type == type) {
  490. dst_ref = av_buffer_ref(tmp_ref);
  491. if (!dst_ref) {
  492. ret = AVERROR(ENOMEM);
  493. goto fail;
  494. }
  495. goto done;
  496. }
  497. tmp_ref = tmp_ctx->internal->source_device;
  498. }
  499. dst_ref = av_hwdevice_ctx_alloc(type);
  500. if (!dst_ref) {
  501. ret = AVERROR(ENOMEM);
  502. goto fail;
  503. }
  504. dst_ctx = (AVHWDeviceContext*)dst_ref->data;
  505. tmp_ref = src_ref;
  506. while (tmp_ref) {
  507. tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
  508. if (dst_ctx->internal->hw_type->device_derive) {
  509. ret = dst_ctx->internal->hw_type->device_derive(dst_ctx,
  510. tmp_ctx,
  511. flags);
  512. if (ret == 0) {
  513. dst_ctx->internal->source_device = av_buffer_ref(src_ref);
  514. if (!dst_ctx->internal->source_device) {
  515. ret = AVERROR(ENOMEM);
  516. goto fail;
  517. }
  518. goto done;
  519. }
  520. if (ret != AVERROR(ENOSYS))
  521. goto fail;
  522. }
  523. tmp_ref = tmp_ctx->internal->source_device;
  524. }
  525. ret = AVERROR(ENOSYS);
  526. goto fail;
  527. done:
  528. *dst_ref_ptr = dst_ref;
  529. return 0;
  530. fail:
  531. av_buffer_unref(&dst_ref);
  532. *dst_ref_ptr = NULL;
  533. return ret;
  534. }
  535. static void ff_hwframe_unmap(void *opaque, uint8_t *data)
  536. {
  537. HWMapDescriptor *hwmap = (HWMapDescriptor*)data;
  538. AVHWFramesContext *ctx = opaque;
  539. if (hwmap->unmap)
  540. hwmap->unmap(ctx, hwmap);
  541. av_frame_free(&hwmap->source);
  542. av_buffer_unref(&hwmap->hw_frames_ctx);
  543. av_free(hwmap);
  544. }
  545. int ff_hwframe_map_create(AVBufferRef *hwframe_ref,
  546. AVFrame *dst, const AVFrame *src,
  547. void (*unmap)(AVHWFramesContext *ctx,
  548. HWMapDescriptor *hwmap),
  549. void *priv)
  550. {
  551. AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
  552. HWMapDescriptor *hwmap;
  553. int ret;
  554. hwmap = av_mallocz(sizeof(*hwmap));
  555. if (!hwmap) {
  556. ret = AVERROR(ENOMEM);
  557. goto fail;
  558. }
  559. hwmap->source = av_frame_alloc();
  560. if (!hwmap->source) {
  561. ret = AVERROR(ENOMEM);
  562. goto fail;
  563. }
  564. ret = av_frame_ref(hwmap->source, src);
  565. if (ret < 0)
  566. goto fail;
  567. hwmap->hw_frames_ctx = av_buffer_ref(hwframe_ref);
  568. if (!hwmap->hw_frames_ctx) {
  569. ret = AVERROR(ENOMEM);
  570. goto fail;
  571. }
  572. hwmap->unmap = unmap;
  573. hwmap->priv = priv;
  574. dst->buf[0] = av_buffer_create((uint8_t*)hwmap, sizeof(*hwmap),
  575. &ff_hwframe_unmap, ctx, 0);
  576. if (!dst->buf[0]) {
  577. ret = AVERROR(ENOMEM);
  578. goto fail;
  579. }
  580. return 0;
  581. fail:
  582. if (hwmap) {
  583. av_buffer_unref(&hwmap->hw_frames_ctx);
  584. av_frame_free(&hwmap->source);
  585. }
  586. av_free(hwmap);
  587. return ret;
  588. }
  589. int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
  590. {
  591. AVHWFramesContext *src_frames, *dst_frames;
  592. HWMapDescriptor *hwmap;
  593. int ret;
  594. if (src->hw_frames_ctx && dst->hw_frames_ctx) {
  595. src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
  596. dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
  597. if ((src_frames == dst_frames &&
  598. src->format == dst_frames->sw_format &&
  599. dst->format == dst_frames->format) ||
  600. (src_frames->internal->source_frames &&
  601. src_frames->internal->source_frames->data ==
  602. (uint8_t*)dst_frames)) {
  603. // This is an unmap operation. We don't need to directly
  604. // do anything here other than fill in the original frame,
  605. // because the real unmap will be invoked when the last
  606. // reference to the mapped frame disappears.
  607. if (!src->buf[0]) {
  608. av_log(src_frames, AV_LOG_ERROR, "Invalid mapping "
  609. "found when attempting unmap.\n");
  610. return AVERROR(EINVAL);
  611. }
  612. hwmap = (HWMapDescriptor*)src->buf[0]->data;
  613. av_frame_unref(dst);
  614. return av_frame_ref(dst, hwmap->source);
  615. }
  616. }
  617. if (src->hw_frames_ctx) {
  618. src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
  619. if (src_frames->format == src->format &&
  620. src_frames->internal->hw_type->map_from) {
  621. ret = src_frames->internal->hw_type->map_from(src_frames,
  622. dst, src, flags);
  623. if (ret != AVERROR(ENOSYS))
  624. return ret;
  625. }
  626. }
  627. if (dst->hw_frames_ctx) {
  628. dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
  629. if (dst_frames->format == dst->format &&
  630. dst_frames->internal->hw_type->map_to) {
  631. ret = dst_frames->internal->hw_type->map_to(dst_frames,
  632. dst, src, flags);
  633. if (ret != AVERROR(ENOSYS))
  634. return ret;
  635. }
  636. }
  637. return AVERROR(ENOSYS);
  638. }
  639. int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
  640. enum AVPixelFormat format,
  641. AVBufferRef *derived_device_ctx,
  642. AVBufferRef *source_frame_ctx,
  643. int flags)
  644. {
  645. AVBufferRef *dst_ref = NULL;
  646. AVHWFramesContext *dst = NULL;
  647. AVHWFramesContext *src = (AVHWFramesContext*)source_frame_ctx->data;
  648. int ret;
  649. if (src->internal->source_frames) {
  650. AVHWFramesContext *src_src =
  651. (AVHWFramesContext*)src->internal->source_frames->data;
  652. AVHWDeviceContext *dst_dev =
  653. (AVHWDeviceContext*)derived_device_ctx->data;
  654. if (src_src->device_ctx == dst_dev) {
  655. // This is actually an unmapping, so we just return a
  656. // reference to the source frame context.
  657. *derived_frame_ctx =
  658. av_buffer_ref(src->internal->source_frames);
  659. if (!*derived_frame_ctx) {
  660. ret = AVERROR(ENOMEM);
  661. goto fail;
  662. }
  663. return 0;
  664. }
  665. }
  666. dst_ref = av_hwframe_ctx_alloc(derived_device_ctx);
  667. if (!dst_ref) {
  668. ret = AVERROR(ENOMEM);
  669. goto fail;
  670. }
  671. dst = (AVHWFramesContext*)dst_ref->data;
  672. dst->format = format;
  673. dst->sw_format = src->sw_format;
  674. dst->width = src->width;
  675. dst->height = src->height;
  676. dst->internal->source_frames = av_buffer_ref(source_frame_ctx);
  677. if (!dst->internal->source_frames) {
  678. ret = AVERROR(ENOMEM);
  679. goto fail;
  680. }
  681. ret = av_hwframe_ctx_init(dst_ref);
  682. if (ret)
  683. goto fail;
  684. *derived_frame_ctx = dst_ref;
  685. return 0;
  686. fail:
  687. if (dst)
  688. av_buffer_unref(&dst->internal->source_frames);
  689. av_buffer_unref(&dst_ref);
  690. return ret;
  691. }