hwcontext.c 24 KB

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