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
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  */
19 #include "libavutil/buffer.h"
20 #include "libavutil/hwcontext.h"
21 #include "libavutil/log.h"
22 #include "libavutil/opt.h"
24 #include "avfilter.h"
25 #include "formats.h"
26 #include "internal.h"
27 #include "video.h"
29 typedef struct CudaUploadContext {
30  const AVClass *class;
38 {
39  CudaUploadContext *s = ctx->priv;
40  char buf[64] = { 0 };
42  snprintf(buf, sizeof(buf), "%d", s->device_idx);
45 }
48 {
49  CudaUploadContext *s = ctx->priv;
53 }
56 {
57  int ret;
59  static const enum AVPixelFormat input_pix_fmts[] = {
64  };
65  static const enum AVPixelFormat output_pix_fmts[] = {
67  };
68  AVFilterFormats *in_fmts = ff_make_format_list(input_pix_fmts);
69  AVFilterFormats *out_fmts;
71  ret = ff_formats_ref(in_fmts, &ctx->inputs[0]->out_formats);
72  if (ret < 0)
73  return ret;
75  out_fmts = ff_make_format_list(output_pix_fmts);
77  ret = ff_formats_ref(out_fmts, &ctx->outputs[0]->in_formats);
78  if (ret < 0)
79  return ret;
81  return 0;
82 }
85 {
86  AVFilterContext *ctx = outlink->src;
87  AVFilterLink *inlink = ctx->inputs[0];
88  CudaUploadContext *s = ctx->priv;
90  AVHWFramesContext *hwframe_ctx;
91  int ret;
95  if (!s->hwframe)
96  return AVERROR(ENOMEM);
98  hwframe_ctx = (AVHWFramesContext*)s->hwframe->data;
99  hwframe_ctx->format = AV_PIX_FMT_CUDA;
100  hwframe_ctx->sw_format = inlink->format;
101  hwframe_ctx->width = inlink->w;
102  hwframe_ctx->height = inlink->h;
104  ret = av_hwframe_ctx_init(s->hwframe);
105  if (ret < 0)
106  return ret;
108  outlink->hw_frames_ctx = av_buffer_ref(s->hwframe);
109  if (!outlink->hw_frames_ctx)
110  return AVERROR(ENOMEM);
112  return 0;
113 }
116 {
117  AVFilterContext *ctx = link->dst;
118  AVFilterLink *outlink = ctx->outputs[0];
120  AVFrame *out = NULL;
121  int ret;
123  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
124  if (!out) {
125  ret = AVERROR(ENOMEM);
126  goto fail;
127  }
129  out->width = in->width;
130  out->height = in->height;
132  ret = av_hwframe_transfer_data(out, in, 0);
133  if (ret < 0) {
134  av_log(ctx, AV_LOG_ERROR, "Error transferring data to the GPU\n");
135  goto fail;
136  }
138  ret = av_frame_copy_props(out, in);
139  if (ret < 0)
140  goto fail;
142  av_frame_free(&in);
144  return ff_filter_frame(ctx->outputs[0], out);
145 fail:
146  av_frame_free(&in);
147  av_frame_free(&out);
148  return ret;
149 }
151 #define OFFSET(x) offsetof(CudaUploadContext, x)
153 static const AVOption cudaupload_options[] = {
154  { "device", "Number of the device to use", OFFSET(device_idx), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
155  { NULL },
156 };
158 AVFILTER_DEFINE_CLASS(cudaupload);
160 static const AVFilterPad cudaupload_inputs[] = {
161  {
162  .name = "default",
163  .type = AVMEDIA_TYPE_VIDEO,
164  .filter_frame = cudaupload_filter_frame,
165  },
166  { NULL }
167 };
169 static const AVFilterPad cudaupload_outputs[] = {
170  {
171  .name = "default",
172  .type = AVMEDIA_TYPE_VIDEO,
173  .config_props = cudaupload_config_output,
174  },
175  { NULL }
176 };
179  .name = "hwupload_cuda",
180  .description = NULL_IF_CONFIG_SMALL("Upload a system memory frame to a CUDA device."),
182  .init = cudaupload_init,
183  .uninit = cudaupload_uninit,
185  .query_formats = cudaupload_query_formats,
187  .priv_size = sizeof(CudaUploadContext),
188  .priv_class = &cudaupload_class,
190  .inputs = cudaupload_inputs,
191  .outputs = cudaupload_outputs,
193  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
194 };
#define NULL
Definition: coverity.c:32
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition: internal.h:365
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:126
AVBufferRef * hwframe
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
Definition: opt.h:246
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
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:228
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:208
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:99
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:437
const char * name
Pad name.
Definition: internal.h:60
#define AV_PIX_FMT_P010
Definition: pixfmt.h:436
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:1075
static av_cold int cudaupload_init(AVFilterContext *ctx)
#define av_cold
Definition: attributes.h:82
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:571
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:400
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:54
int width
Definition: frame.h:353
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define OFFSET(x)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
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:89
static const AVFilterPad cudaupload_inputs[]
#define AV_PIX_FMT_0BGR32
Definition: pixfmt.h:365
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:329
#define fail()
Definition: checkasm.h:122
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
Definition: hwcontext.c:439
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:439
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
HW acceleration through CUDA.
Definition: pixfmt.h:235
static int cudaupload_config_output(AVFilterLink *outlink)
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
AVFilter ff_vf_hwupload_cuda
AVBufferRef * hwdevice
static av_cold void cudaupload_uninit(AVFilterContext *ctx)
uint8_t * data
The data buffer.
Definition: buffer.h:89
void * buf
Definition: avisynth_c.h:766
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;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);return NULL;}return ac;}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;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->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);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
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:123
refcounted data buffer API
const char * name
Filter name.
Definition: avfilter.h:148
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a link
#define snprintf
Definition: snprintf.h:34
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
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:66
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition: hwcontext.c:243
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:94
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:353
FILE * out
Definition: movenc.c:54
internal API functions
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
static const AVOption cudaupload_options[]
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:221
Pixel format.
Definition: pixfmt.h:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:654
#define AV_PIX_FMT_0RGB32
Definition: pixfmt.h:364