libavfilter/vf_hwupload_cuda.c
21f7cd4a
 /*
ef9915a0
  * This file is part of FFmpeg.
21f7cd4a
  *
ef9915a0
  * FFmpeg is free software; you can redistribute it and/or
21f7cd4a
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
ef9915a0
  * FFmpeg is distributed in the hope that it will be useful,
21f7cd4a
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
ef9915a0
  * License along with FFmpeg; if not, write to the Free Software
21f7cd4a
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #include "libavutil/buffer.h"
 #include "libavutil/hwcontext.h"
 #include "libavutil/log.h"
 #include "libavutil/opt.h"
 
 #include "avfilter.h"
 #include "formats.h"
 #include "internal.h"
 #include "video.h"
 
 typedef struct CudaUploadContext {
     const AVClass *class;
     int device_idx;
 
     AVBufferRef *hwdevice;
     AVBufferRef *hwframe;
 } CudaUploadContext;
 
 static av_cold int cudaupload_init(AVFilterContext *ctx)
 {
     CudaUploadContext *s = ctx->priv;
b0ca90d7
     char buf[64] = { 0 };
21f7cd4a
 
b0ca90d7
     snprintf(buf, sizeof(buf), "%d", s->device_idx);
21f7cd4a
 
b0ca90d7
     return av_hwdevice_ctx_create(&s->hwdevice, AV_HWDEVICE_TYPE_CUDA, buf, NULL, 0);
21f7cd4a
 }
 
 static av_cold void cudaupload_uninit(AVFilterContext *ctx)
 {
     CudaUploadContext *s = ctx->priv;
 
     av_buffer_unref(&s->hwframe);
     av_buffer_unref(&s->hwdevice);
 }
 
 static int cudaupload_query_formats(AVFilterContext *ctx)
 {
0faf3c3a
     int ret;
 
21f7cd4a
     static const enum AVPixelFormat input_pix_fmts[] = {
         AV_PIX_FMT_NV12, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV444P,
2e867937
         AV_PIX_FMT_P010, AV_PIX_FMT_P016, AV_PIX_FMT_YUV444P16,
21f7cd4a
         AV_PIX_FMT_NONE,
     };
     static const enum AVPixelFormat output_pix_fmts[] = {
         AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE,
     };
     AVFilterFormats *in_fmts  = ff_make_format_list(input_pix_fmts);
7dafb3a2
     AVFilterFormats *out_fmts;
21f7cd4a
 
0faf3c3a
     ret = ff_formats_ref(in_fmts, &ctx->inputs[0]->out_formats);
     if (ret < 0)
         return ret;
 
7dafb3a2
     out_fmts = ff_make_format_list(output_pix_fmts);
 
0faf3c3a
     ret = ff_formats_ref(out_fmts, &ctx->outputs[0]->in_formats);
     if (ret < 0)
         return ret;
21f7cd4a
 
     return 0;
 }
 
 static int cudaupload_config_output(AVFilterLink *outlink)
 {
     AVFilterContext *ctx = outlink->src;
     AVFilterLink *inlink = ctx->inputs[0];
     CudaUploadContext *s = ctx->priv;
 
     AVHWFramesContext *hwframe_ctx;
     int ret;
 
     av_buffer_unref(&s->hwframe);
     s->hwframe = av_hwframe_ctx_alloc(s->hwdevice);
     if (!s->hwframe)
         return AVERROR(ENOMEM);
 
     hwframe_ctx            = (AVHWFramesContext*)s->hwframe->data;
     hwframe_ctx->format    = AV_PIX_FMT_CUDA;
     hwframe_ctx->sw_format = inlink->format;
b0ca90d7
     hwframe_ctx->width     = inlink->w;
     hwframe_ctx->height    = inlink->h;
21f7cd4a
 
     ret = av_hwframe_ctx_init(s->hwframe);
     if (ret < 0)
         return ret;
 
     outlink->hw_frames_ctx = av_buffer_ref(s->hwframe);
     if (!outlink->hw_frames_ctx)
         return AVERROR(ENOMEM);
 
     return 0;
 }
 
 static int cudaupload_filter_frame(AVFilterLink *link, AVFrame *in)
 {
     AVFilterContext   *ctx = link->dst;
78e871eb
     AVFilterLink  *outlink = ctx->outputs[0];
21f7cd4a
 
     AVFrame *out = NULL;
     int ret;
 
7e2561fa
     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
21f7cd4a
     if (!out) {
         ret = AVERROR(ENOMEM);
         goto fail;
     }
 
     out->width  = in->width;
     out->height = in->height;
 
     ret = av_hwframe_transfer_data(out, in, 0);
     if (ret < 0) {
         av_log(ctx, AV_LOG_ERROR, "Error transferring data to the GPU\n");
         goto fail;
     }
 
     ret = av_frame_copy_props(out, in);
     if (ret < 0)
         goto fail;
 
     av_frame_free(&in);
 
     return ff_filter_frame(ctx->outputs[0], out);
 fail:
     av_frame_free(&in);
     av_frame_free(&out);
     return ret;
 }
 
 #define OFFSET(x) offsetof(CudaUploadContext, x)
ef9915a0
 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
 static const AVOption cudaupload_options[] = {
4df5a6a2
     { "device", "Number of the device to use", OFFSET(device_idx), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
21f7cd4a
     { NULL },
 };
 
7ecef5ed
 AVFILTER_DEFINE_CLASS(cudaupload);
21f7cd4a
 
 static const AVFilterPad cudaupload_inputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
         .filter_frame = cudaupload_filter_frame,
     },
     { NULL }
 };
 
 static const AVFilterPad cudaupload_outputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
         .config_props = cudaupload_config_output,
     },
     { NULL }
 };
 
 AVFilter ff_vf_hwupload_cuda = {
     .name        = "hwupload_cuda",
ef9915a0
     .description = NULL_IF_CONFIG_SMALL("Upload a system memory frame to a CUDA device."),
21f7cd4a
 
     .init      = cudaupload_init,
     .uninit    = cudaupload_uninit,
 
     .query_formats = cudaupload_query_formats,
 
     .priv_size  = sizeof(CudaUploadContext),
     .priv_class = &cudaupload_class,
 
     .inputs    = cudaupload_inputs,
     .outputs   = cudaupload_outputs,
e3fb74f7
 
     .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
21f7cd4a
 };