vf_yadif_cuda.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Copyright (C) 2018 Philip Langdale <philipl@overt.org>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/avassert.h"
  21. #include "libavutil/hwcontext_cuda_internal.h"
  22. #include "libavutil/cuda_check.h"
  23. #include "internal.h"
  24. #include "yadif.h"
  25. extern char vf_yadif_cuda_ptx[];
  26. typedef struct DeintCUDAContext {
  27. YADIFContext yadif;
  28. AVCUDADeviceContext *hwctx;
  29. AVBufferRef *device_ref;
  30. AVBufferRef *input_frames_ref;
  31. AVHWFramesContext *input_frames;
  32. CUcontext cu_ctx;
  33. CUstream stream;
  34. CUmodule cu_module;
  35. CUfunction cu_func_uchar;
  36. CUfunction cu_func_uchar2;
  37. CUfunction cu_func_ushort;
  38. CUfunction cu_func_ushort2;
  39. } DeintCUDAContext;
  40. #define DIV_UP(a, b) ( ((a) + (b) - 1) / (b) )
  41. #define ALIGN_UP(a, b) (((a) + (b) - 1) & ~((b) - 1))
  42. #define BLOCKX 32
  43. #define BLOCKY 16
  44. #define CHECK_CU(x) FF_CUDA_CHECK_DL(ctx, s->hwctx->internal->cuda_dl, x)
  45. static CUresult call_kernel(AVFilterContext *ctx, CUfunction func,
  46. CUdeviceptr prev, CUdeviceptr cur, CUdeviceptr next,
  47. CUarray_format format, int channels,
  48. int src_width, // Width is pixels per channel
  49. int src_height, // Height is pixels per channel
  50. int src_pitch, // Pitch is bytes
  51. CUdeviceptr dst,
  52. int dst_width, // Width is pixels per channel
  53. int dst_height, // Height is pixels per channel
  54. int dst_pitch, // Pitch is pixels per channel
  55. int parity, int tff)
  56. {
  57. DeintCUDAContext *s = ctx->priv;
  58. CudaFunctions *cu = s->hwctx->internal->cuda_dl;
  59. CUtexObject tex_prev = 0, tex_cur = 0, tex_next = 0;
  60. int ret;
  61. int skip_spatial_check = s->yadif.mode&2;
  62. void *args[] = { &dst, &tex_prev, &tex_cur, &tex_next,
  63. &dst_width, &dst_height, &dst_pitch,
  64. &src_width, &src_height, &parity, &tff,
  65. &skip_spatial_check };
  66. CUDA_TEXTURE_DESC tex_desc = {
  67. .filterMode = CU_TR_FILTER_MODE_POINT,
  68. .flags = CU_TRSF_READ_AS_INTEGER,
  69. };
  70. CUDA_RESOURCE_DESC res_desc = {
  71. .resType = CU_RESOURCE_TYPE_PITCH2D,
  72. .res.pitch2D.format = format,
  73. .res.pitch2D.numChannels = channels,
  74. .res.pitch2D.width = src_width,
  75. .res.pitch2D.height = src_height,
  76. .res.pitch2D.pitchInBytes = src_pitch,
  77. };
  78. res_desc.res.pitch2D.devPtr = (CUdeviceptr)prev;
  79. ret = CHECK_CU(cu->cuTexObjectCreate(&tex_prev, &res_desc, &tex_desc, NULL));
  80. if (ret < 0)
  81. goto exit;
  82. res_desc.res.pitch2D.devPtr = (CUdeviceptr)cur;
  83. ret = CHECK_CU(cu->cuTexObjectCreate(&tex_cur, &res_desc, &tex_desc, NULL));
  84. if (ret < 0)
  85. goto exit;
  86. res_desc.res.pitch2D.devPtr = (CUdeviceptr)next;
  87. ret = CHECK_CU(cu->cuTexObjectCreate(&tex_next, &res_desc, &tex_desc, NULL));
  88. if (ret < 0)
  89. goto exit;
  90. ret = CHECK_CU(cu->cuLaunchKernel(func,
  91. DIV_UP(dst_width, BLOCKX), DIV_UP(dst_height, BLOCKY), 1,
  92. BLOCKX, BLOCKY, 1,
  93. 0, s->stream, args, NULL));
  94. exit:
  95. if (tex_prev)
  96. CHECK_CU(cu->cuTexObjectDestroy(tex_prev));
  97. if (tex_cur)
  98. CHECK_CU(cu->cuTexObjectDestroy(tex_cur));
  99. if (tex_next)
  100. CHECK_CU(cu->cuTexObjectDestroy(tex_next));
  101. return ret;
  102. }
  103. static void filter(AVFilterContext *ctx, AVFrame *dst,
  104. int parity, int tff)
  105. {
  106. DeintCUDAContext *s = ctx->priv;
  107. YADIFContext *y = &s->yadif;
  108. CudaFunctions *cu = s->hwctx->internal->cuda_dl;
  109. CUcontext dummy;
  110. int i, ret;
  111. ret = CHECK_CU(cu->cuCtxPushCurrent(s->cu_ctx));
  112. if (ret < 0)
  113. return;
  114. for (i = 0; i < y->csp->nb_components; i++) {
  115. CUfunction func;
  116. CUarray_format format;
  117. int pixel_size, channels;
  118. const AVComponentDescriptor *comp = &y->csp->comp[i];
  119. if (comp->plane < i) {
  120. // We process planes as a whole, so don't reprocess
  121. // them for additional components
  122. continue;
  123. }
  124. pixel_size = (comp->depth + comp->shift) / 8;
  125. channels = comp->step / pixel_size;
  126. if (pixel_size > 2 || channels > 2) {
  127. av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n", y->csp->name);
  128. goto exit;
  129. }
  130. switch (pixel_size) {
  131. case 1:
  132. func = channels == 1 ? s->cu_func_uchar : s->cu_func_uchar2;
  133. format = CU_AD_FORMAT_UNSIGNED_INT8;
  134. break;
  135. case 2:
  136. func = channels == 1 ? s->cu_func_ushort : s->cu_func_ushort2;
  137. format = CU_AD_FORMAT_UNSIGNED_INT16;
  138. break;
  139. default:
  140. av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n", y->csp->name);
  141. goto exit;
  142. }
  143. av_log(ctx, AV_LOG_TRACE,
  144. "Deinterlacing plane %d: pixel_size: %d channels: %d\n",
  145. comp->plane, pixel_size, channels);
  146. call_kernel(ctx, func,
  147. (CUdeviceptr)y->prev->data[i],
  148. (CUdeviceptr)y->cur->data[i],
  149. (CUdeviceptr)y->next->data[i],
  150. format, channels,
  151. AV_CEIL_RSHIFT(y->cur->width, i ? y->csp->log2_chroma_w : 0),
  152. AV_CEIL_RSHIFT(y->cur->height, i ? y->csp->log2_chroma_h : 0),
  153. y->cur->linesize[i],
  154. (CUdeviceptr)dst->data[i],
  155. AV_CEIL_RSHIFT(dst->width, i ? y->csp->log2_chroma_w : 0),
  156. AV_CEIL_RSHIFT(dst->height, i ? y->csp->log2_chroma_h : 0),
  157. dst->linesize[i] / comp->step,
  158. parity, tff);
  159. }
  160. exit:
  161. CHECK_CU(cu->cuCtxPopCurrent(&dummy));
  162. return;
  163. }
  164. static av_cold void deint_cuda_uninit(AVFilterContext *ctx)
  165. {
  166. CUcontext dummy;
  167. DeintCUDAContext *s = ctx->priv;
  168. YADIFContext *y = &s->yadif;
  169. if (s->hwctx && s->cu_module) {
  170. CudaFunctions *cu = s->hwctx->internal->cuda_dl;
  171. CHECK_CU(cu->cuCtxPushCurrent(s->cu_ctx));
  172. CHECK_CU(cu->cuModuleUnload(s->cu_module));
  173. CHECK_CU(cu->cuCtxPopCurrent(&dummy));
  174. }
  175. av_frame_free(&y->prev);
  176. av_frame_free(&y->cur);
  177. av_frame_free(&y->next);
  178. av_buffer_unref(&s->device_ref);
  179. s->hwctx = NULL;
  180. av_buffer_unref(&s->input_frames_ref);
  181. s->input_frames = NULL;
  182. }
  183. static int deint_cuda_query_formats(AVFilterContext *ctx)
  184. {
  185. enum AVPixelFormat pix_fmts[] = {
  186. AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE,
  187. };
  188. int ret;
  189. if ((ret = ff_formats_ref(ff_make_format_list(pix_fmts),
  190. &ctx->inputs[0]->out_formats)) < 0)
  191. return ret;
  192. if ((ret = ff_formats_ref(ff_make_format_list(pix_fmts),
  193. &ctx->outputs[0]->in_formats)) < 0)
  194. return ret;
  195. return 0;
  196. }
  197. static int config_input(AVFilterLink *inlink)
  198. {
  199. AVFilterContext *ctx = inlink->dst;
  200. DeintCUDAContext *s = ctx->priv;
  201. if (!inlink->hw_frames_ctx) {
  202. av_log(ctx, AV_LOG_ERROR, "A hardware frames reference is "
  203. "required to associate the processing device.\n");
  204. return AVERROR(EINVAL);
  205. }
  206. s->input_frames_ref = av_buffer_ref(inlink->hw_frames_ctx);
  207. if (!s->input_frames_ref) {
  208. av_log(ctx, AV_LOG_ERROR, "A input frames reference create "
  209. "failed.\n");
  210. return AVERROR(ENOMEM);
  211. }
  212. s->input_frames = (AVHWFramesContext*)s->input_frames_ref->data;
  213. return 0;
  214. }
  215. static int config_output(AVFilterLink *link)
  216. {
  217. AVHWFramesContext *output_frames;
  218. AVFilterContext *ctx = link->src;
  219. DeintCUDAContext *s = ctx->priv;
  220. YADIFContext *y = &s->yadif;
  221. CudaFunctions *cu;
  222. int ret = 0;
  223. CUcontext dummy;
  224. av_assert0(s->input_frames);
  225. s->device_ref = av_buffer_ref(s->input_frames->device_ref);
  226. if (!s->device_ref) {
  227. av_log(ctx, AV_LOG_ERROR, "A device reference create "
  228. "failed.\n");
  229. return AVERROR(ENOMEM);
  230. }
  231. s->hwctx = ((AVHWDeviceContext*)s->device_ref->data)->hwctx;
  232. s->cu_ctx = s->hwctx->cuda_ctx;
  233. s->stream = s->hwctx->stream;
  234. cu = s->hwctx->internal->cuda_dl;
  235. link->hw_frames_ctx = av_hwframe_ctx_alloc(s->device_ref);
  236. if (!link->hw_frames_ctx) {
  237. av_log(ctx, AV_LOG_ERROR, "Failed to create HW frame context "
  238. "for output.\n");
  239. ret = AVERROR(ENOMEM);
  240. goto exit;
  241. }
  242. output_frames = (AVHWFramesContext*)link->hw_frames_ctx->data;
  243. output_frames->format = AV_PIX_FMT_CUDA;
  244. output_frames->sw_format = s->input_frames->sw_format;
  245. output_frames->width = ctx->inputs[0]->w;
  246. output_frames->height = ctx->inputs[0]->h;
  247. output_frames->initial_pool_size = 4;
  248. ret = ff_filter_init_hw_frames(ctx, link, 10);
  249. if (ret < 0)
  250. goto exit;
  251. ret = av_hwframe_ctx_init(link->hw_frames_ctx);
  252. if (ret < 0) {
  253. av_log(ctx, AV_LOG_ERROR, "Failed to initialise CUDA frame "
  254. "context for output: %d\n", ret);
  255. goto exit;
  256. }
  257. link->time_base.num = ctx->inputs[0]->time_base.num;
  258. link->time_base.den = ctx->inputs[0]->time_base.den * 2;
  259. link->w = ctx->inputs[0]->w;
  260. link->h = ctx->inputs[0]->h;
  261. if(y->mode & 1)
  262. link->frame_rate = av_mul_q(ctx->inputs[0]->frame_rate,
  263. (AVRational){2, 1});
  264. if (link->w < 3 || link->h < 3) {
  265. av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
  266. ret = AVERROR(EINVAL);
  267. goto exit;
  268. }
  269. y->csp = av_pix_fmt_desc_get(output_frames->sw_format);
  270. y->filter = filter;
  271. ret = CHECK_CU(cu->cuCtxPushCurrent(s->cu_ctx));
  272. if (ret < 0)
  273. goto exit;
  274. ret = CHECK_CU(cu->cuModuleLoadData(&s->cu_module, vf_yadif_cuda_ptx));
  275. if (ret < 0)
  276. goto exit;
  277. ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_uchar, s->cu_module, "yadif_uchar"));
  278. if (ret < 0)
  279. goto exit;
  280. ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_uchar2, s->cu_module, "yadif_uchar2"));
  281. if (ret < 0)
  282. goto exit;
  283. ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_ushort, s->cu_module, "yadif_ushort"));
  284. if (ret < 0)
  285. goto exit;
  286. ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_ushort2, s->cu_module, "yadif_ushort2"));
  287. if (ret < 0)
  288. goto exit;
  289. exit:
  290. CHECK_CU(cu->cuCtxPopCurrent(&dummy));
  291. return ret;
  292. }
  293. static const AVClass yadif_cuda_class = {
  294. .class_name = "yadif_cuda",
  295. .item_name = av_default_item_name,
  296. .option = ff_yadif_options,
  297. .version = LIBAVUTIL_VERSION_INT,
  298. .category = AV_CLASS_CATEGORY_FILTER,
  299. };
  300. static const AVFilterPad deint_cuda_inputs[] = {
  301. {
  302. .name = "default",
  303. .type = AVMEDIA_TYPE_VIDEO,
  304. .filter_frame = ff_yadif_filter_frame,
  305. .config_props = config_input,
  306. },
  307. { NULL }
  308. };
  309. static const AVFilterPad deint_cuda_outputs[] = {
  310. {
  311. .name = "default",
  312. .type = AVMEDIA_TYPE_VIDEO,
  313. .request_frame = ff_yadif_request_frame,
  314. .config_props = config_output,
  315. },
  316. { NULL }
  317. };
  318. AVFilter ff_vf_yadif_cuda = {
  319. .name = "yadif_cuda",
  320. .description = NULL_IF_CONFIG_SMALL("Deinterlace CUDA frames"),
  321. .priv_size = sizeof(DeintCUDAContext),
  322. .priv_class = &yadif_cuda_class,
  323. .uninit = deint_cuda_uninit,
  324. .query_formats = deint_cuda_query_formats,
  325. .inputs = deint_cuda_inputs,
  326. .outputs = deint_cuda_outputs,
  327. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  328. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  329. };