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.

452 lines
12KB

  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_VAAPI
  33. &ff_hwcontext_type_vaapi,
  34. #endif
  35. #if CONFIG_VDPAU
  36. &ff_hwcontext_type_vdpau,
  37. #endif
  38. NULL,
  39. };
  40. static const AVClass hwdevice_ctx_class = {
  41. .class_name = "AVHWDeviceContext",
  42. .item_name = av_default_item_name,
  43. .version = LIBAVUTIL_VERSION_INT,
  44. };
  45. static void hwdevice_ctx_free(void *opaque, uint8_t *data)
  46. {
  47. AVHWDeviceContext *ctx = (AVHWDeviceContext*)data;
  48. /* uninit might still want access the hw context and the user
  49. * free() callback might destroy it, so uninit has to be called first */
  50. if (ctx->internal->hw_type->device_uninit)
  51. ctx->internal->hw_type->device_uninit(ctx);
  52. if (ctx->free)
  53. ctx->free(ctx);
  54. av_freep(&ctx->hwctx);
  55. av_freep(&ctx->internal->priv);
  56. av_freep(&ctx->internal);
  57. av_freep(&ctx);
  58. }
  59. AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
  60. {
  61. AVHWDeviceContext *ctx;
  62. AVBufferRef *buf;
  63. const HWContextType *hw_type = NULL;
  64. int i;
  65. for (i = 0; hw_table[i]; i++) {
  66. if (hw_table[i]->type == type) {
  67. hw_type = hw_table[i];
  68. break;
  69. }
  70. }
  71. if (!hw_type)
  72. return NULL;
  73. ctx = av_mallocz(sizeof(*ctx));
  74. if (!ctx)
  75. return NULL;
  76. ctx->internal = av_mallocz(sizeof(*ctx->internal));
  77. if (!ctx->internal)
  78. goto fail;
  79. if (hw_type->device_priv_size) {
  80. ctx->internal->priv = av_mallocz(hw_type->device_priv_size);
  81. if (!ctx->internal->priv)
  82. goto fail;
  83. }
  84. if (hw_type->device_hwctx_size) {
  85. ctx->hwctx = av_mallocz(hw_type->device_hwctx_size);
  86. if (!ctx->hwctx)
  87. goto fail;
  88. }
  89. buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
  90. hwdevice_ctx_free, NULL,
  91. AV_BUFFER_FLAG_READONLY);
  92. if (!buf)
  93. goto fail;
  94. ctx->type = type;
  95. ctx->av_class = &hwdevice_ctx_class;
  96. ctx->internal->hw_type = hw_type;
  97. return buf;
  98. fail:
  99. if (ctx->internal)
  100. av_freep(&ctx->internal->priv);
  101. av_freep(&ctx->internal);
  102. av_freep(&ctx->hwctx);
  103. av_freep(&ctx);
  104. return NULL;
  105. }
  106. int av_hwdevice_ctx_init(AVBufferRef *ref)
  107. {
  108. AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
  109. int ret;
  110. if (ctx->internal->hw_type->device_init) {
  111. ret = ctx->internal->hw_type->device_init(ctx);
  112. if (ret < 0)
  113. goto fail;
  114. }
  115. return 0;
  116. fail:
  117. if (ctx->internal->hw_type->device_uninit)
  118. ctx->internal->hw_type->device_uninit(ctx);
  119. return ret;
  120. }
  121. static const AVClass hwframe_ctx_class = {
  122. .class_name = "AVHWFramesContext",
  123. .item_name = av_default_item_name,
  124. .version = LIBAVUTIL_VERSION_INT,
  125. };
  126. static void hwframe_ctx_free(void *opaque, uint8_t *data)
  127. {
  128. AVHWFramesContext *ctx = (AVHWFramesContext*)data;
  129. if (ctx->internal->pool_internal)
  130. av_buffer_pool_uninit(&ctx->internal->pool_internal);
  131. if (ctx->internal->hw_type->frames_uninit)
  132. ctx->internal->hw_type->frames_uninit(ctx);
  133. if (ctx->free)
  134. ctx->free(ctx);
  135. av_buffer_unref(&ctx->device_ref);
  136. av_freep(&ctx->hwctx);
  137. av_freep(&ctx->internal->priv);
  138. av_freep(&ctx->internal);
  139. av_freep(&ctx);
  140. }
  141. AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
  142. {
  143. AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref_in->data;
  144. const HWContextType *hw_type = device_ctx->internal->hw_type;
  145. AVHWFramesContext *ctx;
  146. AVBufferRef *buf, *device_ref = NULL;
  147. ctx = av_mallocz(sizeof(*ctx));
  148. if (!ctx)
  149. return NULL;
  150. ctx->internal = av_mallocz(sizeof(*ctx->internal));
  151. if (!ctx->internal)
  152. goto fail;
  153. if (hw_type->frames_priv_size) {
  154. ctx->internal->priv = av_mallocz(hw_type->frames_priv_size);
  155. if (!ctx->internal->priv)
  156. goto fail;
  157. }
  158. if (hw_type->frames_hwctx_size) {
  159. ctx->hwctx = av_mallocz(hw_type->frames_hwctx_size);
  160. if (!ctx->hwctx)
  161. goto fail;
  162. }
  163. device_ref = av_buffer_ref(device_ref_in);
  164. if (!device_ref)
  165. goto fail;
  166. buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
  167. hwframe_ctx_free, NULL,
  168. AV_BUFFER_FLAG_READONLY);
  169. if (!buf)
  170. goto fail;
  171. ctx->av_class = &hwframe_ctx_class;
  172. ctx->device_ref = device_ref;
  173. ctx->device_ctx = device_ctx;
  174. ctx->format = AV_PIX_FMT_NONE;
  175. ctx->sw_format = AV_PIX_FMT_NONE;
  176. ctx->internal->hw_type = hw_type;
  177. return buf;
  178. fail:
  179. if (device_ref)
  180. av_buffer_unref(&device_ref);
  181. if (ctx->internal)
  182. av_freep(&ctx->internal->priv);
  183. av_freep(&ctx->internal);
  184. av_freep(&ctx->hwctx);
  185. av_freep(&ctx);
  186. return NULL;
  187. }
  188. static int hwframe_pool_prealloc(AVBufferRef *ref)
  189. {
  190. AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
  191. AVFrame **frames;
  192. int i, ret = 0;
  193. frames = av_mallocz_array(ctx->initial_pool_size, sizeof(*frames));
  194. if (!frames)
  195. return AVERROR(ENOMEM);
  196. for (i = 0; i < ctx->initial_pool_size; i++) {
  197. frames[i] = av_frame_alloc();
  198. if (!frames[i])
  199. goto fail;
  200. ret = av_hwframe_get_buffer(ref, frames[i], 0);
  201. if (ret < 0)
  202. goto fail;
  203. }
  204. fail:
  205. for (i = 0; i < ctx->initial_pool_size; i++)
  206. av_frame_free(&frames[i]);
  207. av_freep(&frames);
  208. return ret;
  209. }
  210. int av_hwframe_ctx_init(AVBufferRef *ref)
  211. {
  212. AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
  213. const enum AVPixelFormat *pix_fmt;
  214. int ret;
  215. /* validate the pixel format */
  216. for (pix_fmt = ctx->internal->hw_type->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) {
  217. if (*pix_fmt == ctx->format)
  218. break;
  219. }
  220. if (*pix_fmt == AV_PIX_FMT_NONE) {
  221. av_log(ctx, AV_LOG_ERROR,
  222. "The hardware pixel format '%s' is not supported by the device type '%s'\n",
  223. av_get_pix_fmt_name(ctx->format), ctx->internal->hw_type->name);
  224. return AVERROR(ENOSYS);
  225. }
  226. /* validate the dimensions */
  227. ret = av_image_check_size(ctx->width, ctx->height, 0, ctx);
  228. if (ret < 0)
  229. return ret;
  230. /* format-specific init */
  231. if (ctx->internal->hw_type->frames_init) {
  232. ret = ctx->internal->hw_type->frames_init(ctx);
  233. if (ret < 0)
  234. goto fail;
  235. }
  236. if (ctx->internal->pool_internal && !ctx->pool)
  237. ctx->pool = ctx->internal->pool_internal;
  238. /* preallocate the frames in the pool, if requested */
  239. if (ctx->initial_pool_size > 0) {
  240. ret = hwframe_pool_prealloc(ref);
  241. if (ret < 0)
  242. goto fail;
  243. }
  244. return 0;
  245. fail:
  246. if (ctx->internal->hw_type->frames_uninit)
  247. ctx->internal->hw_type->frames_uninit(ctx);
  248. return ret;
  249. }
  250. int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref,
  251. enum AVHWFrameTransferDirection dir,
  252. enum AVPixelFormat **formats, int flags)
  253. {
  254. AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
  255. if (!ctx->internal->hw_type->transfer_get_formats)
  256. return AVERROR(ENOSYS);
  257. return ctx->internal->hw_type->transfer_get_formats(ctx, dir, formats);
  258. }
  259. static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
  260. {
  261. AVFrame *frame_tmp;
  262. int ret = 0;
  263. frame_tmp = av_frame_alloc();
  264. if (!frame_tmp)
  265. return AVERROR(ENOMEM);
  266. /* if the format is set, use that
  267. * otherwise pick the first supported one */
  268. if (dst->format >= 0) {
  269. frame_tmp->format = dst->format;
  270. } else {
  271. enum AVPixelFormat *formats;
  272. ret = av_hwframe_transfer_get_formats(src->hw_frames_ctx,
  273. AV_HWFRAME_TRANSFER_DIRECTION_FROM,
  274. &formats, 0);
  275. if (ret < 0)
  276. goto fail;
  277. frame_tmp->format = formats[0];
  278. av_freep(&formats);
  279. }
  280. frame_tmp->width = src->width;
  281. frame_tmp->height = src->height;
  282. ret = av_frame_get_buffer(frame_tmp, 32);
  283. if (ret < 0)
  284. goto fail;
  285. ret = av_hwframe_transfer_data(frame_tmp, src, flags);
  286. if (ret < 0)
  287. goto fail;
  288. av_frame_move_ref(dst, frame_tmp);
  289. fail:
  290. av_frame_free(&frame_tmp);
  291. return ret;
  292. }
  293. int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
  294. {
  295. AVHWFramesContext *ctx;
  296. int ret;
  297. if (!dst->buf[0])
  298. return transfer_data_alloc(dst, src, flags);
  299. if (src->hw_frames_ctx) {
  300. ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
  301. ret = ctx->internal->hw_type->transfer_data_from(ctx, dst, src);
  302. if (ret < 0)
  303. return ret;
  304. } else if (dst->hw_frames_ctx) {
  305. ctx = (AVHWFramesContext*)dst->hw_frames_ctx->data;
  306. ret = ctx->internal->hw_type->transfer_data_to(ctx, dst, src);
  307. if (ret < 0)
  308. return ret;
  309. } else
  310. return AVERROR(ENOSYS);
  311. return 0;
  312. }
  313. int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
  314. {
  315. AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
  316. int ret;
  317. if (!ctx->internal->hw_type->frames_get_buffer)
  318. return AVERROR(ENOSYS);
  319. if (!ctx->pool)
  320. return AVERROR(EINVAL);
  321. frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
  322. if (!frame->hw_frames_ctx)
  323. return AVERROR(ENOMEM);
  324. ret = ctx->internal->hw_type->frames_get_buffer(ctx, frame);
  325. if (ret < 0) {
  326. av_buffer_unref(&frame->hw_frames_ctx);
  327. return ret;
  328. }
  329. return 0;
  330. }
  331. void *av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
  332. {
  333. AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
  334. const HWContextType *hw_type = ctx->internal->hw_type;
  335. if (hw_type->device_hwconfig_size == 0)
  336. return NULL;
  337. return av_mallocz(hw_type->device_hwconfig_size);
  338. }
  339. AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
  340. const void *hwconfig)
  341. {
  342. AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
  343. const HWContextType *hw_type = ctx->internal->hw_type;
  344. AVHWFramesConstraints *constraints;
  345. if (!hw_type->frames_get_constraints)
  346. return NULL;
  347. constraints = av_mallocz(sizeof(*constraints));
  348. if (!constraints)
  349. return NULL;
  350. constraints->min_width = constraints->min_height = 0;
  351. constraints->max_width = constraints->max_height = INT_MAX;
  352. if (hw_type->frames_get_constraints(ctx, hwconfig, constraints) >= 0) {
  353. return constraints;
  354. } else {
  355. av_hwframe_constraints_free(&constraints);
  356. return NULL;
  357. }
  358. }
  359. void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
  360. {
  361. if (*constraints) {
  362. av_freep(&(*constraints)->valid_hw_formats);
  363. av_freep(&(*constraints)->valid_sw_formats);
  364. }
  365. av_freep(constraints);
  366. }