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"
21 #include "libavutil/log.h"
22 #include "libavutil/opt.h"
23 
24 #include "avfilter.h"
25 #include "formats.h"
26 #include "internal.h"
27 #include "video.h"
28 
29 typedef struct CudaUploadContext {
30  const AVClass *class;
32 
36 
38 {
39  CudaUploadContext *s = ctx->priv;
40  char buf[64] = { 0 };
41 
42  snprintf(buf, sizeof(buf), "%d", s->device_idx);
43 
45 }
46 
48 {
49  CudaUploadContext *s = ctx->priv;
50 
53 }
54 
56 {
57  int ret;
58 
59  static const enum AVPixelFormat input_pix_fmts[] = {
63  };
64  static const enum AVPixelFormat output_pix_fmts[] = {
66  };
67  AVFilterFormats *in_fmts = ff_make_format_list(input_pix_fmts);
68  AVFilterFormats *out_fmts;
69 
70  ret = ff_formats_ref(in_fmts, &ctx->inputs[0]->out_formats);
71  if (ret < 0)
72  return ret;
73 
74  out_fmts = ff_make_format_list(output_pix_fmts);
75 
76  ret = ff_formats_ref(out_fmts, &ctx->outputs[0]->in_formats);
77  if (ret < 0)
78  return ret;
79 
80  return 0;
81 }
82 
84 {
85  AVFilterContext *ctx = outlink->src;
86  AVFilterLink *inlink = ctx->inputs[0];
87  CudaUploadContext *s = ctx->priv;
88 
89  AVHWFramesContext *hwframe_ctx;
90  int ret;
91 
94  if (!s->hwframe)
95  return AVERROR(ENOMEM);
96 
97  hwframe_ctx = (AVHWFramesContext*)s->hwframe->data;
98  hwframe_ctx->format = AV_PIX_FMT_CUDA;
99  hwframe_ctx->sw_format = inlink->format;
100  hwframe_ctx->width = inlink->w;
101  hwframe_ctx->height = inlink->h;
102 
103  ret = av_hwframe_ctx_init(s->hwframe);
104  if (ret < 0)
105  return ret;
106 
107  outlink->hw_frames_ctx = av_buffer_ref(s->hwframe);
108  if (!outlink->hw_frames_ctx)
109  return AVERROR(ENOMEM);
110 
111  return 0;
112 }
113 
115 {
116  AVFilterContext *ctx = link->dst;
117  AVFilterLink *outlink = ctx->outputs[0];
118 
119  AVFrame *out = NULL;
120  int ret;
121 
122  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
123  if (!out) {
124  ret = AVERROR(ENOMEM);
125  goto fail;
126  }
127 
128  out->width = in->width;
129  out->height = in->height;
130 
131  ret = av_hwframe_transfer_data(out, in, 0);
132  if (ret < 0) {
133  av_log(ctx, AV_LOG_ERROR, "Error transferring data to the GPU\n");
134  goto fail;
135  }
136 
137  ret = av_frame_copy_props(out, in);
138  if (ret < 0)
139  goto fail;
140 
141  av_frame_free(&in);
142 
143  return ff_filter_frame(ctx->outputs[0], out);
144 fail:
145  av_frame_free(&in);
146  av_frame_free(&out);
147  return ret;
148 }
149 
150 #define OFFSET(x) offsetof(CudaUploadContext, x)
151 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
152 static const AVOption cudaupload_options[] = {
153  { "device", "Number of the device to use", OFFSET(device_idx), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
154  { NULL },
155 };
156 
157 AVFILTER_DEFINE_CLASS(cudaupload);
158 
159 static const AVFilterPad cudaupload_inputs[] = {
160  {
161  .name = "default",
162  .type = AVMEDIA_TYPE_VIDEO,
163  .filter_frame = cudaupload_filter_frame,
164  },
165  { NULL }
166 };
167 
168 static const AVFilterPad cudaupload_outputs[] = {
169  {
170  .name = "default",
171  .type = AVMEDIA_TYPE_VIDEO,
172  .config_props = cudaupload_config_output,
173  },
174  { NULL }
175 };
176 
178  .name = "hwupload_cuda",
179  .description = NULL_IF_CONFIG_SMALL("Upload a system memory frame to a CUDA device."),
180 
181  .init = cudaupload_init,
182  .uninit = cudaupload_uninit,
183 
184  .query_formats = cudaupload_query_formats,
185 
186  .priv_size = sizeof(CudaUploadContext),
187  .priv_class = &cudaupload_class,
188 
189  .inputs = cudaupload_inputs,
190  .outputs = cudaupload_outputs,
191 
192  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
193 };
#define NULL
Definition: coverity.c:32
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition: internal.h:385
const char * s
Definition: avisynth_c.h:768
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:125
AVBufferRef * hwframe
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
AVOption.
Definition: opt.h:246
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
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:226
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:206
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:92
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
#define AV_PIX_FMT_P016
Definition: pixfmt.h:425
const char * name
Pad name.
Definition: internal.h:60
#define AV_PIX_FMT_P010
Definition: pixfmt.h:424
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1151
static av_cold int cudaupload_init(AVFilterContext *ctx)
#define av_cold
Definition: attributes.h:82
AVOptions.
int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type, const char *device, AVDictionary *opts, int flags)
Open a device of the specified type and create an AVHWDeviceContext for it.
Definition: hwcontext.c:563
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:392
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:54
int width
Definition: frame.h:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#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:163
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
void * priv
private data for use by the filter
Definition: avfilter.h:353
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:323
#define fail()
Definition: checkasm.h:109
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
Definition: hwcontext.c:433
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
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:389
HW acceleration through CUDA.
Definition: pixfmt.h:249
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:379
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
void * buf
Definition: avisynth_c.h:690
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:121
refcounted data buffer API
const char * name
Filter name.
Definition: avfilter.h:148
#define snprintf
Definition: snprintf.h:34
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
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:237
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:338
int height
Definition: frame.h:259
FILE * out
Definition: movenc.c:54
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:219
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:603