FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_hwupload_cuda.c
Go to the documentation of this file.
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 
19 #include "libavutil/buffer.h"
20 #include "libavutil/hwcontext.h"
22 #include "libavutil/log.h"
23 #include "libavutil/opt.h"
24 
25 #include "avfilter.h"
26 #include "formats.h"
27 #include "internal.h"
28 #include "video.h"
29 
30 typedef struct CudaUploadContext {
31  const AVClass *class;
33 
37 
39 {
40  AVCUDADeviceContext *hwctx = ctx->hwctx;
41  cuCtxDestroy(hwctx->cuda_ctx);
42 }
43 
45 {
46  CudaUploadContext *s = ctx->priv;
47 
48  AVHWDeviceContext *device_ctx;
49  AVCUDADeviceContext *device_hwctx;
50  CUdevice device;
51  CUcontext cuda_ctx = NULL, dummy;
52  CUresult err;
53  int ret;
54 
55  err = cuInit(0);
56  if (err != CUDA_SUCCESS) {
57  av_log(ctx, AV_LOG_ERROR, "Could not initialize the CUDA driver API\n");
58  return AVERROR_UNKNOWN;
59  }
60 
61  err = cuDeviceGet(&device, s->device_idx);
62  if (err != CUDA_SUCCESS) {
63  av_log(ctx, AV_LOG_ERROR, "Could not get the device number %d\n", s->device_idx);
64  return AVERROR_UNKNOWN;
65  }
66 
67  err = cuCtxCreate(&cuda_ctx, 0, device);
68  if (err != CUDA_SUCCESS) {
69  av_log(ctx, AV_LOG_ERROR, "Error creating a CUDA context\n");
70  return AVERROR_UNKNOWN;
71  }
72 
73  cuCtxPopCurrent(&dummy);
74 
76  if (!s->hwdevice) {
77  cuCtxDestroy(cuda_ctx);
78  return AVERROR(ENOMEM);
79  }
80 
81  device_ctx = (AVHWDeviceContext*)s->hwdevice->data;
82  device_ctx->free = cudaupload_ctx_free;
83 
84  device_hwctx = device_ctx->hwctx;
85  device_hwctx->cuda_ctx = cuda_ctx;
86 
88  if (ret < 0)
89  return ret;
90 
91  return 0;
92 }
93 
95 {
96  CudaUploadContext *s = ctx->priv;
97 
100 }
101 
103 {
104  static const enum AVPixelFormat input_pix_fmts[] = {
107  };
108  static const enum AVPixelFormat output_pix_fmts[] = {
110  };
111  AVFilterFormats *in_fmts = ff_make_format_list(input_pix_fmts);
112  AVFilterFormats *out_fmts = ff_make_format_list(output_pix_fmts);
113 
114  ff_formats_ref(in_fmts, &ctx->inputs[0]->out_formats);
115  ff_formats_ref(out_fmts, &ctx->outputs[0]->in_formats);
116 
117  return 0;
118 }
119 
121 {
122  AVFilterContext *ctx = outlink->src;
123  AVFilterLink *inlink = ctx->inputs[0];
124  CudaUploadContext *s = ctx->priv;
125 
126  AVHWFramesContext *hwframe_ctx;
127  int ret;
128 
131  if (!s->hwframe)
132  return AVERROR(ENOMEM);
133 
134  hwframe_ctx = (AVHWFramesContext*)s->hwframe->data;
135  hwframe_ctx->format = AV_PIX_FMT_CUDA;
136  hwframe_ctx->sw_format = inlink->format;
137  hwframe_ctx->width = FFALIGN(inlink->w, 16);
138  hwframe_ctx->height = FFALIGN(inlink->h, 16);
139 
140  ret = av_hwframe_ctx_init(s->hwframe);
141  if (ret < 0)
142  return ret;
143 
144  outlink->hw_frames_ctx = av_buffer_ref(s->hwframe);
145  if (!outlink->hw_frames_ctx)
146  return AVERROR(ENOMEM);
147 
148  return 0;
149 }
150 
152 {
153  AVFilterContext *ctx = link->dst;
154  CudaUploadContext *s = ctx->priv;
155 
156  AVFrame *out = NULL;
157  int ret;
158 
159  out = av_frame_alloc();
160  if (!out) {
161  ret = AVERROR(ENOMEM);
162  goto fail;
163  }
164 
165  ret = av_hwframe_get_buffer(s->hwframe, out, 0);
166  if (ret < 0)
167  goto fail;
168 
169  out->width = in->width;
170  out->height = in->height;
171 
172  ret = av_hwframe_transfer_data(out, in, 0);
173  if (ret < 0) {
174  av_log(ctx, AV_LOG_ERROR, "Error transferring data to the GPU\n");
175  goto fail;
176  }
177 
178  ret = av_frame_copy_props(out, in);
179  if (ret < 0)
180  goto fail;
181 
182  av_frame_free(&in);
183 
184  return ff_filter_frame(ctx->outputs[0], out);
185 fail:
186  av_frame_free(&in);
187  av_frame_free(&out);
188  return ret;
189 }
190 
191 #define OFFSET(x) offsetof(CudaUploadContext, x)
192 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
193 static const AVOption cudaupload_options[] = {
194  { "device", "Number of the device to use", OFFSET(device_idx), AV_OPT_TYPE_INT, { .i64 = 0 }, .flags = FLAGS },
195  { NULL },
196 };
197 
198 AVFILTER_DEFINE_CLASS(cudaupload);
199 
200 static const AVFilterPad cudaupload_inputs[] = {
201  {
202  .name = "default",
203  .type = AVMEDIA_TYPE_VIDEO,
204  .filter_frame = cudaupload_filter_frame,
205  },
206  { NULL }
207 };
208 
209 static const AVFilterPad cudaupload_outputs[] = {
210  {
211  .name = "default",
212  .type = AVMEDIA_TYPE_VIDEO,
213  .config_props = cudaupload_config_output,
214  },
215  { NULL }
216 };
217 
219  .name = "hwupload_cuda",
220  .description = NULL_IF_CONFIG_SMALL("Upload a system memory frame to a CUDA device."),
221 
222  .init = cudaupload_init,
223  .uninit = cudaupload_uninit,
224 
225  .query_formats = cudaupload_query_formats,
226 
227  .priv_size = sizeof(CudaUploadContext),
228  .priv_class = &cudaupload_class,
229 
230  .inputs = cudaupload_inputs,
231  .outputs = cudaupload_outputs,
232 };
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:53
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:124
AVBufferRef * hwframe
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
AVOption.
Definition: opt.h:245
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
Main libavfilter public API header.
#define FLAGS
int CUdevice
Definition: nvenc.h:44
static int cudaupload_query_formats(AVFilterContext *ctx)
static const AVFilterPad cudaupload_outputs[]
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:221
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:201
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
const char * name
Pad name.
Definition: internal.h:59
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:313
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1180
static av_cold int cudaupload_init(AVFilterContext *ctx)
#define av_cold
Definition: attributes.h:82
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:140
AVOptions.
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:84
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
An API-specific header for AV_HWDEVICE_TYPE_CUDA.
A filter pad used for either input or output.
Definition: internal.h:53
int width
width and height of the video frame
Definition: frame.h:236
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void(* free)(struct AVHWDeviceContext *ctx)
This field may be set by the caller before calling av_hwdevice_ctx_init().
Definition: hwcontext.h:96
#define OFFSET(x)
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:153
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
static void cudaupload_ctx_free(AVHWDeviceContext *ctx)
void * priv
private data for use by the filter
Definition: avfilter.h:320
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:90
static const AVFilterPad cudaupload_inputs[]
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:260
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
Definition: hwcontext.c:387
#define fail()
Definition: checkasm.h:81
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
Definition: hwcontext.c:361
static int cudaupload_filter_frame(AVFilterLink *link, AVFrame *in)
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:440
AVFormatContext * ctx
Definition: movenc.c:48
CUresult
Definition: nvenc.h:41
int dummy
Definition: motion.c:64
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:386
HW acceleration through CUDA.
Definition: pixfmt.h:248
static int cudaupload_config_output(AVFilterLink *outlink)
AVFilter ff_vf_hwupload_cuda
AVBufferRef * hwdevice
static av_cold void cudaupload_uninit(AVFilterContext *ctx)
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:376
AVBufferRef * av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
Allocate an AVHWDeviceContext for a given pixel format.
Definition: hwcontext.c:71
AVFILTER_DEFINE_CLASS(cudaupload)
uint8_t * data
The data buffer.
Definition: buffer.h:89
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
int av_hwdevice_ctx_init(AVBufferRef *ref)
Finalize the device context before use.
Definition: hwcontext.c:129
This struct is allocated as AVHWDeviceContext.hwctx.
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:142
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:116
refcounted data buffer API
const char * name
Filter name.
Definition: avfilter.h:146
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:317
A reference to a data buffer.
Definition: buffer.h:81
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition: hwcontext.c:174
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:305
int height
Definition: frame.h:236
FILE * out
Definition: movenc.c:54
void * CUcontext
Definition: nvenc.h:45
internal API functions
static const AVOption cudaupload_options[]
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:214
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:580