FFmpeg
Loading...
Searching...
No Matches
vf_scale_cuda.c
Go to the documentation of this file.
1/*
2* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
3*
4* Permission is hereby granted, free of charge, to any person obtaining a
5* copy of this software and associated documentation files (the "Software"),
6* to deal in the Software without restriction, including without limitation
7* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8* and/or sell copies of the Software, and to permit persons to whom the
9* Software is furnished to do so, subject to the following conditions:
10*
11* The above copyright notice and this permission notice shall be included in
12* all copies or substantial portions of the Software.
13*
14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20* DEALINGS IN THE SOFTWARE.
21*/
22
23#include <float.h>
24#include <stdbool.h>
25#include <stdio.h>
26#include <string.h>
27
28#include "libavutil/avassert.h"
29#include "libavutil/common.h"
30#include "libavutil/csp.h"
31#include "libavutil/hwcontext.h"
34#include "libavutil/internal.h"
35#include "libavutil/mem.h"
36#include "libavutil/opt.h"
37#include "libavutil/pixdesc.h"
38#include "libavutil/refstruct.h"
39
40#include "libswscale/filters.h"
41
42#include "avfilter.h"
43#include "colorspace.h"
44#include "filters.h"
45#include "formats.h"
46#include "scale_eval.h"
47#include "video.h"
48
49#include "cuda/load_helper.h"
50#include "vf_scale_cuda.h"
51
54 char name[13];
55};
56
57static const struct format_entry supported_formats[] = {
58 {AV_PIX_FMT_YUV420P, "planar8"},
59 {AV_PIX_FMT_YUV422P, "planar8"},
60 {AV_PIX_FMT_YUV444P, "planar8"},
61 {AV_PIX_FMT_YUV420P10,"planar10"},
62 {AV_PIX_FMT_YUV422P10,"planar10"},
63 {AV_PIX_FMT_YUV444P10,"planar10"},
64 {AV_PIX_FMT_YUV444P10MSB,"planar16"},
65 {AV_PIX_FMT_YUV444P12MSB,"planar16"},
66 {AV_PIX_FMT_YUV444P16,"planar16"},
67 {AV_PIX_FMT_NV12, "semiplanar8"},
68 {AV_PIX_FMT_NV16, "semiplanar8"},
69 {AV_PIX_FMT_P010, "semiplanar10"},
70 {AV_PIX_FMT_P210, "semiplanar10"},
71 {AV_PIX_FMT_P012, "semiplanar16"},
72 {AV_PIX_FMT_P212, "semiplanar16"},
73 {AV_PIX_FMT_P016, "semiplanar16"},
74 {AV_PIX_FMT_P216, "semiplanar16"},
75 {AV_PIX_FMT_0RGB32, "bgr0"},
76 {AV_PIX_FMT_0BGR32, "rgb0"},
77 {AV_PIX_FMT_RGB32, "bgra"},
78 {AV_PIX_FMT_BGR32, "rgba"},
79};
80
81#define DIV_UP(a, b) ( ((a) + (b) - 1) / (b) )
82#define BLOCKX 32
83#define BLOCKY 16
84
85#define CHECK_CU(x) FF_CUDA_CHECK_DL(ctx, s->hwctx->internal->cuda_dl, x)
86
87enum {
89
94
96};
97
98enum {
102};
103
104enum {
108};
109
110enum {
113};
114
115typedef struct CUDAScalePassPlan {
117 /* Plane groups whose sample grids change along each axis. */
118 unsigned int x_planes;
119 unsigned int y_planes;
121
122typedef struct CUDAScaleFilter {
123 CUdeviceptr weights; ///< float[dst_size][filter_size]
124 CUdeviceptr offsets; ///< int[dst_size]
128
139
140/* Buffers a pass can read from or write to. */
141enum {
147};
148
149#define CUDA_SCALE_MAX_PASSES 3
150
151/*
152 * One kernel pass. Everything the kernels need beyond the geometry of the
153 * source and destination buffers is described here, so that a pass does
154 * not depend on its position in the chain.
155 */
156typedef struct CUDAScalePass {
157 CUfunction func; ///< kernel for the primary plane(s)
158 CUfunction func_uv; ///< kernel for the chroma plane(s)
159 CUDAScaleFilter filter; ///< generic filter weights for func
160 CUDAScaleFilter filter_uv; ///< generic filter weights for func_uv
161 unsigned int planes; ///< plane groups written, CUDA_SCALE_PLANE_*
162 int src, dst; ///< CUDA_SCALE_BUF_*
163 const AVPixFmtDescriptor *src_desc; ///< sample format read
164 const AVPixFmtDescriptor *dst_desc; ///< sample format written
166
167typedef struct CUDAScaleFilterSet {
170 CUDATex tmp[2]; ///< CUDA_SCALE_BUF_TMP, CUDA_SCALE_BUF_TMP2
171 int in_use_linear; ///< texture filtering of the input
172 int in_as_integer; ///< integer texture reads of the input
176
177typedef struct CUDAScaleContext {
178 const AVClass *class;
179
181
182 enum AVPixelFormat in_fmt, out_fmt;
187
190
193
194 /**
195 * Output sw format. AV_PIX_FMT_NONE for no conversion.
196 */
198
199 char *w_expr; ///< width expression string
200 char *h_expr; ///< height expression string
201
205
206 CUcontext cu_ctx;
207 CUmodule cu_module;
208 CUfunction cu_func_fixed;
210 CUfunction cu_func_out[2];
211 CUfunction cu_func_out_uv[2];
212 CUfunction cu_func_tmp;
213 CUfunction cu_func_tmp_uv;
214 CUfunction cu_func_tmp_v; ///< vertical float pass before RGB->YUV
215 CUfunction cu_func_conv; ///< conversion with a float RGB side
216 CUfunction cu_func_conv_uv;
217 CUstream cu_stream;
218
222
225 int use_filters_opt; /* -1 for auto */
226
227 float param;
228
231 int out_range; ///< requested output color range (AVColorRange)
232 int out_color_matrix; ///< requested output YCbCr matrix (AVColorSpace)
233 enum AVColorRange color_range; ///< resolved color range for conversion
236
237/*
238 * Compare the unrounded plane scale. AV_CEIL_RSHIFT() can hide a change
239 * in sample geometry for odd dimensions: 3px 4:4:4 -> 5px 4:2:0 has
240 * three stored chroma samples on each side, but the virtual plane size
241 * changes from 3 to 2.5 and still requires filtering.
242 */
243static int cudascale_plane_needs_scale(int in_size, int out_size,
244 int in_sub, int out_sub)
245{
246 return (int64_t)out_size * (1 << in_sub) !=
247 (int64_t)in_size * (1 << out_sub);
248}
249
250static int cudascale_plane_is_downscaled(int in_size, int out_size,
251 int in_sub, int out_sub)
252{
253 return (int64_t)out_size * (1 << in_sub) <
254 (int64_t)in_size * (1 << out_sub);
255}
256
257static double cudascale_plane_virtual_size(int src_size,
258 int in_size, int out_size,
259 int in_sub, int out_sub)
260{
261 return src_size * ((double) out_size / in_size) *
262 (1 << in_sub) / (1 << out_sub);
263}
264
266 int in_width, int in_height,
267 int out_width, int out_height,
268 int in_sub_x, int in_sub_y,
269 int out_sub_x, int out_sub_y,
270 int has_chroma)
271{
272 unsigned int x_planes = 0;
273 unsigned int y_planes = 0;
274
275 *plan = (CUDAScalePassPlan) {
277 };
278
279 if (cudascale_plane_needs_scale(in_width, out_width, 0, 0))
280 x_planes |= CUDA_SCALE_PLANE_PRIMARY;
281 if (cudascale_plane_needs_scale(in_height, out_height, 0, 0))
282 y_planes |= CUDA_SCALE_PLANE_PRIMARY;
283
284 if (has_chroma) {
285 if (cudascale_plane_needs_scale(in_width, out_width,
286 in_sub_x, out_sub_x))
287 x_planes |= CUDA_SCALE_PLANE_CHROMA;
288 if (cudascale_plane_needs_scale(in_height, out_height,
289 in_sub_y, out_sub_y))
290 y_planes |= CUDA_SCALE_PLANE_CHROMA;
291 }
292
293 plan->x_planes = x_planes;
294 plan->y_planes = y_planes;
295
296 if (x_planes && y_planes) {
299 } else if (x_planes) {
301 } else if (y_planes) {
303 }
304}
305
307{
308 CUDAScaleContext *s = ctx->priv;
309
310 s->frame = av_frame_alloc();
311 if (!s->frame)
312 return AVERROR(ENOMEM);
313
314 s->tmp_frame = av_frame_alloc();
315 if (!s->tmp_frame)
316 return AVERROR(ENOMEM);
317
318 return 0;
319}
320
321static void filter_uninit(CudaFunctions *cu, CUDAScaleFilter *filter)
322{
323 if (filter->weights)
324 cu->cuMemFree(filter->weights);
325 if (filter->offsets)
326 cu->cuMemFree(filter->offsets);
327 memset(filter, 0, sizeof(*filter));
328}
329
330static void cuda_tex_uninit(CudaFunctions *cu, CUDATex *t)
331{
332 for (int i = 0; i < FF_ARRAY_ELEMS(t->tex); i++) {
333 if (t->tex[i])
334 cu->cuTexObjectDestroy(t->tex[i]);
335 if (t->data[i] && !t->external_data)
336 cu->cuMemFree(t->data[i]);
337 }
338
339 memset(t, 0, sizeof(*t));
340}
341
342static void filter_set_uninit(CudaFunctions *cu, CUDAScaleFilterSet *set)
343{
344 for (int i = 0; i < FF_ARRAY_ELEMS(set->tmp); i++)
345 cuda_tex_uninit(cu, &set->tmp[i]);
346 for (int i = 0; i < FF_ARRAY_ELEMS(set->passes); i++) {
347 filter_uninit(cu, &set->passes[i].filter);
348 filter_uninit(cu, &set->passes[i].filter_uv);
349 }
350 memset(set, 0, sizeof(*set));
351}
352
354{
355 CUDAScaleContext *s = ctx->priv;
356
357 if (s->hwctx) {
358 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
359 CUcontext dummy;
360
361 CHECK_CU(cu->cuCtxPushCurrent(s->hwctx->cuda_ctx));
362
363 filter_set_uninit(cu, &s->filter_set);
364
365 if (s->cu_module) {
366 CHECK_CU(cu->cuModuleUnload(s->cu_module));
367 s->cu_module = NULL;
368 }
369
370 CHECK_CU(cu->cuCtxPopCurrent(&dummy));
371 }
372
373 av_frame_free(&s->frame);
374 av_buffer_unref(&s->frames_ctx);
375 av_frame_free(&s->tmp_frame);
376}
377
379{
380 AVBufferRef *out_ref = NULL;
381 AVHWFramesContext *out_ctx;
382 int ret;
383
384 out_ref = av_hwframe_ctx_alloc(device_ctx);
385 if (!out_ref)
386 return AVERROR(ENOMEM);
387 out_ctx = (AVHWFramesContext*)out_ref->data;
388
389 out_ctx->format = AV_PIX_FMT_CUDA;
390 out_ctx->sw_format = s->out_fmt;
391 out_ctx->width = FFALIGN(width, 32);
392 out_ctx->height = FFALIGN(height, 32);
393
394 ret = av_hwframe_ctx_init(out_ref);
395 if (ret < 0)
396 goto fail;
397
398 av_frame_unref(s->frame);
399 ret = av_hwframe_get_buffer(out_ref, s->frame, 0);
400 if (ret < 0)
401 goto fail;
402
403 s->frame->width = width;
404 s->frame->height = height;
405
406 av_buffer_unref(&s->frames_ctx);
407 s->frames_ctx = out_ref;
408
409 return 0;
410fail:
411 av_buffer_unref(&out_ref);
412 return ret;
413}
414
416 int width, int height,
417 int log2_chroma_w, int log2_chroma_h,
418 int nb_planes, const int depths[4],
419 const int channels[4],
420 int use_float, unsigned int planes)
421{
422 CUDAScaleContext *s = ctx->priv;
423 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
424 int ret = 0;
425
426 *tex = (CUDATex) {
427 .width = width,
428 .height = height,
429 .crop_width = width,
430 .crop_height = height,
431 .log2_chroma_w = log2_chroma_w,
432 .log2_chroma_h = log2_chroma_h,
433 };
434
435 for (int i = 0; i < nb_planes; i++) {
436 const int is_chroma = i == 1 || i == 2;
437 const unsigned int plane = is_chroma ? CUDA_SCALE_PLANE_CHROMA :
439 const int sub_x = is_chroma ? tex->log2_chroma_w : 0;
440 const int sub_y = is_chroma ? tex->log2_chroma_h : 0;
441 const int plane_w = AV_CEIL_RSHIFT(width, sub_x);
442 const int plane_h = AV_CEIL_RSHIFT(height, sub_y);
443 const int sizeof_pixel = (use_float ? sizeof(float) :
444 depths[i] <= 8 ? 1 : 2) * channels[i];
445
446 if (!(planes & plane))
447 continue;
448
449 size_t pitch;
450 ret = CHECK_CU(cu->cuMemAllocPitch(&tex->data[i], &pitch,
451 (size_t) plane_w * sizeof_pixel,
452 plane_h, 16));
453 if (ret < 0)
454 goto fail;
455 tex->linesize[i] = pitch;
456
457 CUDA_TEXTURE_DESC tex_desc = {
458 /* inter tex is always read as float */
459 .filterMode = CU_TR_FILTER_MODE_POINT,
460 };
461
462 CUDA_RESOURCE_DESC res_desc = {
463 .resType = CU_RESOURCE_TYPE_PITCH2D,
464 .res.pitch2D.format = use_float ? CU_AD_FORMAT_FLOAT :
465 depths[i] <= 8 ?
466 CU_AD_FORMAT_UNSIGNED_INT8 :
467 CU_AD_FORMAT_UNSIGNED_INT16,
468 .res.pitch2D.numChannels = channels[i],
469 .res.pitch2D.devPtr = tex->data[i],
470 .res.pitch2D.pitchInBytes = pitch,
471 .res.pitch2D.width = plane_w,
472 .res.pitch2D.height = plane_h,
473 };
474
475 ret = CHECK_CU(cu->cuTexObjectCreate(&tex->tex[i], &res_desc,
476 &tex_desc, NULL));
477 if (ret < 0)
478 goto fail;
479 }
480
481 return 0;
482
483fail:
484 cuda_tex_uninit(cu, tex);
485 return ret;
486}
487
489{
490 for (int i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
491 if (supported_formats[i].format == fmt)
492 return 1;
493 return 0;
494}
495
496static const char* get_format_name(enum AVPixelFormat fmt)
497{
498 for (int i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
499 if (supported_formats[i].format == fmt)
500 return supported_formats[i].name;
501 return NULL;
502}
503
504/* H.273 YCgCo uses the luma excursion for chroma as well. */
505static int chroma_uses_luma_span(enum AVColorSpace colorspace)
506{
507 return colorspace == AVCOL_SPC_YCGCO
508 || colorspace == AVCOL_SPC_YCGCO_RE
509 || colorspace == AVCOL_SPC_YCGCO_RO;
510}
511
512static int compute_yuv2rgb_matrix(void *log_ctx, CUDAScaleColorMatrix *mat,
513 enum AVColorSpace colorspace,
514 bool limited_range)
515{
516 const AVLumaCoefficients *coeffs = av_csp_luma_coeffs_from_avcsp(colorspace);
517 float y_scale = 1.0f;
518 float uv_scale = 1.0f;
519 double rgb2yuv[3][3], yuv2rgb[3][3];
520
521 /* Constant luminance is not the matrix returned for these coefficients. */
522 if (!coeffs || colorspace == AVCOL_SPC_BT2020_CL) {
523 av_log(log_ctx, AV_LOG_ERROR,
524 "Unsupported colorspace %d for YUV->RGB conversion\n", colorspace);
525 return AVERROR(EINVAL);
526 }
527
530
531 if (limited_range) {
532 y_scale = 255.0f / (235.0f - 16.0f);
533 uv_scale = chroma_uses_luma_span(colorspace) ? y_scale
534 : 255.0f / (240.0f - 16.0f);
535 }
536
537 for (int i = 0; i < 3; i++) {
538 mat->m[i][0] = (float)yuv2rgb[i][0] * y_scale;
539 mat->m[i][1] = (float)yuv2rgb[i][1] * uv_scale;
540 mat->m[i][2] = (float)yuv2rgb[i][2] * uv_scale;
541 }
542
543 return 0;
544}
545
546static int compute_rgb2yuv_matrix(void *log_ctx, CUDAScaleColorMatrix *mat,
547 enum AVColorSpace colorspace,
548 bool limited_range)
549{
550 const AVLumaCoefficients *coeffs = av_csp_luma_coeffs_from_avcsp(colorspace);
551 double rgb2yuv[3][3];
552 float y_scale = 1.0f;
553 float uv_scale = 1.0f;
554
555 if (!coeffs || colorspace == AVCOL_SPC_BT2020_CL) {
556 av_log(log_ctx, AV_LOG_ERROR,
557 "Unsupported colorspace %d for RGB->YUV conversion\n", colorspace);
558 return AVERROR(EINVAL);
559 }
560
562
563 if (limited_range) {
564 y_scale = (235.0f - 16.0f) / 255.0f;
565 uv_scale = chroma_uses_luma_span(colorspace) ? y_scale
566 : (240.0f - 16.0f) / 255.0f;
567 }
568
569 for (int i = 0; i < 3; i++) {
570 float scale = (i == 0) ? y_scale : uv_scale;
571 mat->m[i][0] = (float)rgb2yuv[i][0] * scale;
572 mat->m[i][1] = (float)rgb2yuv[i][1] * scale;
573 mat->m[i][2] = (float)rgb2yuv[i][2] * scale;
574 }
575
576 return 0;
577}
578
579/* Whether two specified properties differ; unspecified is compatible. */
580static int cudascale_conflicts(int in, int out, int unspecified)
581{
582 return in != out && in != unspecified && out != unspecified;
583}
584
585static av_cold void set_format_info(AVFilterContext *ctx, enum AVPixelFormat in_format, enum AVPixelFormat out_format)
586{
587 CUDAScaleContext *s = ctx->priv;
588 int i, p, d;
589
590 s->in_fmt = in_format;
591 s->out_fmt = out_format;
592
593 s->in_desc = av_pix_fmt_desc_get(s->in_fmt);
594 s->out_desc = av_pix_fmt_desc_get(s->out_fmt);
595 s->in_planes = av_pix_fmt_count_planes(s->in_fmt);
596 s->out_planes = av_pix_fmt_count_planes(s->out_fmt);
597
598 // find maximum step of each component of each plane
599 // For our subset of formats, this should accurately tell us how many channels CUDA needs
600 // i.e. 1 for Y plane, 2 for UV plane of NV12, 4 for single plane of RGB0 formats
601
602 for (i = 0; i < s->in_desc->nb_components; i++) {
603 d = (s->in_desc->comp[i].depth + 7) / 8;
604 p = s->in_desc->comp[i].plane;
605 s->in_plane_channels[p] = FFMAX(s->in_plane_channels[p], s->in_desc->comp[i].step / d);
606
607 s->in_plane_depths[p] = s->in_desc->comp[i].depth;
608 }
609}
610
611static av_cold int init_processing_chain(AVFilterContext *ctx, int in_width, int in_height,
612 int out_width, int out_height)
613{
614 CUDAScaleContext *s = ctx->priv;
615 AVFilterLink *inlink = ctx->inputs[0];
616 AVFilterLink *outlink = ctx->outputs[0];
617 FilterLink *inl = ff_filter_link(inlink);
618 FilterLink *outl = ff_filter_link(outlink);
619
620 AVHWFramesContext *in_frames_ctx;
621
622 enum AVPixelFormat in_format;
623 enum AVPixelFormat out_format;
624 int ret;
625
626 /* check that we have a hw context */
627 if (!inl->hw_frames_ctx) {
628 av_log(ctx, AV_LOG_ERROR, "No hw context provided on input\n");
629 return AVERROR(EINVAL);
630 }
631 in_frames_ctx = (AVHWFramesContext*)inl->hw_frames_ctx->data;
632 in_format = in_frames_ctx->sw_format;
633 out_format = (s->format == AV_PIX_FMT_NONE) ? in_format : s->format;
634
635 if (!format_is_supported(in_format)) {
636 av_log(ctx, AV_LOG_ERROR, "Unsupported input format: %s\n",
637 av_get_pix_fmt_name(in_format));
638 return AVERROR(ENOSYS);
639 }
640 if (!format_is_supported(out_format)) {
641 av_log(ctx, AV_LOG_ERROR, "Unsupported output format: %s\n",
642 av_get_pix_fmt_name(out_format));
643 return AVERROR(ENOSYS);
644 }
645
646 set_format_info(ctx, in_format, out_format);
647
648 s->yuv2rgb = !(s->in_desc->flags & AV_PIX_FMT_FLAG_RGB) &&
649 (s->out_desc->flags & AV_PIX_FMT_FLAG_RGB);
650 if (s->yuv2rgb) {
651
652 enum AVColorSpace cs = inlink->colorspace;
653 bool limited_range;
654
655 /* When colorspace is unspecified, default to BT.601 (SMPTE 170M)
656 * to match swscale behavior (SWS_CS_DEFAULT = ITU601).
657 */
658 if (cs == AVCOL_SPC_UNSPECIFIED)
660
661 if (cs == AVCOL_SPC_RGB) {
663 "YUV input is tagged with the RGB colorspace\n");
664 return AVERROR(EINVAL);
665 }
666
667 limited_range = (inlink->color_range != AVCOL_RANGE_JPEG);
668
669 ret = compute_yuv2rgb_matrix(ctx, &s->color_matrix, cs, limited_range);
670 if (ret < 0)
671 return ret;
672
673 s->color_range = limited_range ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
674
676 "YUV->RGB conversion enabled (%s, %s range)\n",
678 limited_range ? "limited" : "full");
679 }
680
681 s->rgb2yuv = (s->in_desc->flags & AV_PIX_FMT_FLAG_RGB) &&
682 !(s->out_desc->flags & AV_PIX_FMT_FLAG_RGB);
683 if (s->rgb2yuv) {
684 /* The matrix and range are those negotiated for the output, which
685 * the output frames are tagged with. When unspecified, use BT.601
686 * (SMPTE 170M) limited range to match swscale behavior. */
687 enum AVColorSpace cs = outlink->colorspace;
688 bool limited_range;
689
690 if (cs == AVCOL_SPC_UNSPECIFIED)
692
693 limited_range = (outlink->color_range != AVCOL_RANGE_JPEG);
694
695 ret = compute_rgb2yuv_matrix(ctx, &s->color_matrix, cs, limited_range);
696 if (ret < 0)
697 return ret;
698
699 s->color_range = limited_range ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
700
702 "RGB->YUV conversion enabled (%s, %s range)\n",
704 limited_range ? "limited" : "full");
705 }
706
707 if (s->out_desc->flags & AV_PIX_FMT_FLAG_RGB) {
708 /* Without a requested format, these are negotiated before the output
709 * is known to be RGB, and would describe the RGB output link. */
710 if (s->out_range != AVCOL_RANGE_UNSPECIFIED ||
711 s->out_color_matrix != AVCOL_SPC_UNSPECIFIED) {
713 "out_range and out_color_matrix cannot be used with RGB output\n");
714 return AVERROR(EINVAL);
715 }
716 } else if (s->rgb2yuv) {
717 if ((s->out_desc->log2_chroma_w || s->out_desc->log2_chroma_h) &&
721 "RGB->YUV conversion produces centered chroma, cannot "
722 "output %s chroma location\n",
724 return AVERROR(ENOSYS);
725 }
726 } else {
727 /* Without RGB->YUV conversion, the samples keep their encoding.
728 * Only two different specified descriptions conflict: an
729 * unspecified input may take on the negotiated description, like
730 * buffersrc promotes an unspecified range to limited, and an
731 * unspecified output leaves it to the frames. */
732 if (cudascale_conflicts(inlink->colorspace, outlink->colorspace,
737 "Cannot convert from %s %s range to %s %s range, scale_cuda "
738 "only changes YCbCr matrix and range when converting from RGB\n",
743 return AVERROR(ENOSYS);
744 }
745
746 /* Scaling with the same subsampling keeps the chroma sites. */
747 if ((s->in_desc->log2_chroma_w || s->in_desc->log2_chroma_h) &&
748 s->in_desc->log2_chroma_w == s->out_desc->log2_chroma_w &&
749 s->in_desc->log2_chroma_h == s->out_desc->log2_chroma_h &&
753 "Cannot convert chroma location from %s to %s\n",
756 return AVERROR(ENOSYS);
757 }
758 }
759
760 if (s->passthrough && in_width == out_width && in_height == out_height && in_format == out_format) {
761 s->frames_ctx = av_buffer_ref(inl->hw_frames_ctx);
762 if (!s->frames_ctx)
763 return AVERROR(ENOMEM);
764
765 s->use_filters = 0;
766 } else {
767 s->passthrough = 0;
768
769 ret = init_hwframe_ctx(s, in_frames_ctx->device_ref, out_width, out_height);
770 if (ret < 0)
771 return ret;
772
773 if (in_width == out_width && in_height == out_height &&
774 in_format == out_format && s->interp_algo == INTERP_ALGO_DEFAULT &&
775 s->use_filters_opt != 1)
776 s->interp_algo = INTERP_ALGO_NEAREST;
777
778 if (s->yuv2rgb || s->rgb2yuv) {
779 /* Conversions plan their own passes, see
780 * cudascale_conv_set_init(). */
781 if (s->use_filters_opt == 0 &&
782 (in_width != out_width || in_height != out_height))
783 av_log(ctx, AV_LOG_WARNING, "Scaling during YUV<->RGB "
784 "conversion always uses the generic filters\n");
785 s->use_filters = 0;
786 } else if (s->interp_algo == INTERP_ALGO_NEAREST) {
787 s->use_filters = 0;
788 } else if (s->use_filters_opt >= 0) {
789 s->use_filters = s->use_filters_opt;
790 } else {
791 /* Lanczos needs the generic path for its full windowed-sinc
792 * kernel. Other algorithms need it when downscaling for correct
793 * anti-aliasing. */
794 s->use_filters = s->interp_algo == INTERP_ALGO_LANCZOS ||
795 cudascale_plane_is_downscaled(in_width, out_width,
796 0, 0) ||
797 cudascale_plane_is_downscaled(in_height, out_height,
798 0, 0) ||
799 (s->in_planes > 1 && s->out_planes > 1 &&
801 in_width, out_width,
802 s->in_desc->log2_chroma_w,
803 s->out_desc->log2_chroma_w) ||
805 in_height, out_height,
806 s->in_desc->log2_chroma_h,
807 s->out_desc->log2_chroma_h)));
808 }
809 }
810
811 outl->hw_frames_ctx = av_buffer_ref(s->frames_ctx);
812 if (!outl->hw_frames_ctx)
813 return AVERROR(ENOMEM);
814
815 return 0;
816}
817
819{
820 CUDAScaleContext *s = ctx->priv;
821 CUcontext dummy, cuda_ctx = s->hwctx->cuda_ctx;
822 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
823 char buf[128];
824 int ret;
825
826 const char *in_fmt_name = get_format_name(s->in_fmt);
827 const char *out_fmt_name = get_format_name(s->out_fmt);
828
829 const char *fixed_infix;
830 int fixed_use_linear;
831 int fixed_as_integer;
832
833 extern const unsigned char ff_vf_scale_cuda_ptx_data[];
834 extern const unsigned int ff_vf_scale_cuda_ptx_len;
835
836 /* The direct conversion kernels sample 1:1; scaling during conversion
837 * uses the generic filters with interp_algo instead. */
838 switch (s->yuv2rgb || s->rgb2yuv ? INTERP_ALGO_NEAREST : s->interp_algo) {
840 fixed_infix = "Nearest";
841 fixed_use_linear = 0;
842 fixed_as_integer = 1;
843 break;
845 fixed_infix = "Bilinear";
846 fixed_use_linear = 1;
847 fixed_as_integer = 1;
848 break;
851 fixed_infix = "Bicubic";
852 fixed_use_linear = 0;
853 fixed_as_integer = 0;
854 break;
856 fixed_infix = "Lanczos";
857 fixed_use_linear = 0;
858 fixed_as_integer = 0;
859 break;
860 default:
861 av_log(ctx, AV_LOG_ERROR, "Unknown interpolation algorithm\n");
862 return AVERROR_BUG;
863 }
864
865 ret = CHECK_CU(cu->cuCtxPushCurrent(cuda_ctx));
866 if (ret < 0)
867 return ret;
868
869 ret = ff_cuda_load_module(ctx, s->hwctx, &s->cu_module,
870 ff_vf_scale_cuda_ptx_data, ff_vf_scale_cuda_ptx_len);
871 if (ret < 0)
872 goto fail;
873
874 if (s->use_filters) {
875 static const char *const infix[] = { "Generic_h", "Generic_v" };
876 const char *tmp_infix = s->interp_algo == INTERP_ALGO_LANCZOS ?
877 "Generic_float_h" : "Generic_h";
878
879 s->interp_use_linear = 0;
880 s->interp_as_integer = 0;
881
882 for (int dir = CUDA_SCALE_DIR_X; dir <= CUDA_SCALE_DIR_Y; dir++) {
883 snprintf(buf, sizeof(buf), "Subsample_%s_%s_%s",
884 infix[dir], in_fmt_name, out_fmt_name);
885 ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_out[dir],
886 s->cu_module, buf));
887 if (ret < 0)
888 goto unsupported;
889
890 snprintf(buf, sizeof(buf), "Subsample_%s_%s_%s_uv",
891 infix[dir], in_fmt_name, out_fmt_name);
892 ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_out_uv[dir],
893 s->cu_module, buf));
894 if (ret < 0)
895 goto unsupported;
896 }
897
898 snprintf(buf, sizeof(buf), "Subsample_%s_%s_%s", tmp_infix,
899 in_fmt_name, in_fmt_name);
900 ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_tmp,
901 s->cu_module, buf));
902 if (ret < 0)
903 goto unsupported;
904
905 if (s->in_planes > 1) {
906 snprintf(buf, sizeof(buf), "Subsample_%s_%s_%s_uv", tmp_infix,
907 in_fmt_name, in_fmt_name);
908 ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_tmp_uv,
909 s->cu_module, buf));
910 if (ret < 0)
911 goto unsupported;
912 }
913 } else {
914 s->interp_use_linear = fixed_use_linear;
915 s->interp_as_integer = fixed_as_integer;
916
917 snprintf(buf, sizeof(buf), "Subsample_%s_%s_%s", fixed_infix,
918 in_fmt_name, out_fmt_name);
919 ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_fixed,
920 s->cu_module, buf));
921 if (ret < 0)
922 goto unsupported;
923
924 /* YUV->RGB produces all output components in a single kernel,
925 * there is no separate chroma pass. */
926 if (!s->yuv2rgb) {
927 snprintf(buf, sizeof(buf), "Subsample_%s_%s_%s_uv", fixed_infix,
928 in_fmt_name, out_fmt_name);
929 ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_fixed_uv,
930 s->cu_module, buf));
931 if (ret < 0)
932 goto unsupported;
933 }
934 }
935
936 if (s->yuv2rgb || s->rgb2yuv) {
937 const char *rgb_name = s->yuv2rgb ? out_fmt_name : in_fmt_name;
938
939 snprintf(buf, sizeof(buf), "Conv_float_%s_%s", in_fmt_name, out_fmt_name);
940 ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_conv, s->cu_module, buf));
941 if (ret < 0)
942 goto unsupported;
943
944 snprintf(buf, sizeof(buf), "Subsample_Generic_float_h_%s_%s", rgb_name, rgb_name);
945 ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_tmp, s->cu_module, buf));
946 if (ret < 0)
947 goto unsupported;
948
949 if (s->yuv2rgb) {
950 static const char *const infix[] = { "Generic_h", "Generic_v" };
951
952 for (int dir = CUDA_SCALE_DIR_X; dir <= CUDA_SCALE_DIR_Y; dir++) {
953 snprintf(buf, sizeof(buf), "Subsample_%s_%s_%s",
954 infix[dir], rgb_name, rgb_name);
955 ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_out[dir],
956 s->cu_module, buf));
957 if (ret < 0)
958 goto unsupported;
959 }
960 } else {
961 snprintf(buf, sizeof(buf), "Conv_float_%s_%s_uv", in_fmt_name, out_fmt_name);
962 ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_conv_uv, s->cu_module, buf));
963 if (ret < 0)
964 goto unsupported;
965
966 snprintf(buf, sizeof(buf), "Subsample_Generic_float_v_%s_%s", rgb_name, rgb_name);
967 ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_tmp_v, s->cu_module, buf));
968 if (ret < 0)
969 goto unsupported;
970 }
971 }
972
973 goto fail;
974
976 av_log(ctx, AV_LOG_FATAL, "Unsupported conversion: %s -> %s\n",
977 in_fmt_name, out_fmt_name);
978 ret = AVERROR(ENOSYS);
979
980fail:
981 CHECK_CU(cu->cuCtxPopCurrent(&dummy));
982
983 return ret;
984}
985
988 int src_size, int dst_size,
989 double virtual_size, int needs_scale)
990{
991 CUDAScaleContext *s = ctx->priv;
992 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
993
995 .scaler_params = { SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT },
996 .src_size = src_size,
997 .dst_size = dst_size,
998 .virtual_size = virtual_size,
999 };
1000
1001 if (!needs_scale) {
1002 params.scaler = SWS_SCALE_POINT;
1003 params.virtual_size = 0.0;
1004 } else {
1005 switch (s->interp_algo) {
1006 case INTERP_ALGO_NEAREST: params.scaler = SWS_SCALE_POINT; break;
1007 case INTERP_ALGO_BILINEAR: params.scaler = SWS_SCALE_BILINEAR; break;
1009 params.scaler = SWS_SCALE_LANCZOS;
1010 if (s->param != SCALE_CUDA_PARAM_DEFAULT)
1011 params.scaler_params[0] = s->param;
1012 break;
1015 params.scaler = SWS_SCALE_BICUBIC;
1016 params.scaler_params[0] = params.scaler_params[1] = 0.0;
1017 if (s->param != SCALE_CUDA_PARAM_DEFAULT)
1018 params.scaler_params[1] = s->param;
1019 break;
1020 }
1021 }
1022
1025 if (ret < 0) {
1026 if (ret == AVERROR(ENOTSUP)) {
1027 av_log(ctx, AV_LOG_ERROR, "Filter size exceeds the maximum "
1028 "currently supported by the CUDA scaler (%d).\n",
1030 }
1031 return ret;
1032 }
1033
1034 float *tmp = av_malloc_array(weights->num_weights, sizeof(*tmp));
1035 if (!tmp) {
1036 ret = AVERROR(ENOMEM);
1037 goto fail;
1038 }
1039 for (size_t i = 0; i < weights->num_weights; i++)
1040 tmp[i] = weights->weights[i] / (float) SWS_FILTER_SCALE;
1041
1042 f->filter_size = weights->filter_size;
1043 f->dst_size = dst_size;
1044
1045 const size_t weights_size = weights->num_weights * sizeof(*tmp);
1046 ret = CHECK_CU(cu->cuMemAlloc(&f->weights, weights_size));
1047 if (ret < 0)
1048 goto fail;
1049 ret = CHECK_CU(cu->cuMemcpyHtoD(f->weights, tmp, weights_size));
1050 if (ret < 0)
1051 goto fail;
1052
1053 const size_t offsets_size = dst_size * sizeof(*weights->offsets);
1054 ret = CHECK_CU(cu->cuMemAlloc(&f->offsets, offsets_size));
1055 if (ret < 0)
1056 goto fail;
1057 ret = CHECK_CU(cu->cuMemcpyHtoD(f->offsets, weights->offsets, offsets_size));
1058 if (ret < 0)
1059 goto fail;
1060
1061 av_log(ctx, AV_LOG_VERBOSE, " using %d tap '%s' filter: %d -> %d\n",
1062 f->filter_size, ff_sws_scaler_name(weights->scaler), src_size, dst_size);
1063
1064 ret = 0;
1065
1066fail:
1067 av_free(tmp);
1069 return ret;
1070}
1071
1072/*
1073 * Converting together with scaling scales on the RGB side, so that every
1074 * scaling pass works on a single packed plane and reuses the generic RGB
1075 * kernels: YUV->RGB converts at the visible input size before scaling,
1076 * RGB->YUV after scaling at the output size. The RGB intermediates are
1077 * normalized floats, so the scaler rounds and clamps only once.
1078 */
1081 int in_width, int in_height)
1082{
1083 CUDAScaleContext *s = ctx->priv;
1084 AVFilterLink *outlink = ctx->outputs[0];
1085 const AVPixFmtDescriptor *rgb_desc = s->yuv2rgb ? s->out_desc : s->in_desc;
1086 static const int rgb_depths[4] = { 8 };
1087 static const int rgb_channels[4] = { 4 };
1088 const int scale_x = in_width != outlink->w;
1089 const int scale_y = in_height != outlink->h;
1091 CUDAScalePass *pass;
1092 int ret;
1093
1094 /* Scaling reads RGB input as normalized floats. */
1095 set->in_use_linear = 0;
1096 set->in_as_integer = s->yuv2rgb;
1097
1098 if (s->yuv2rgb) {
1100 in_width, in_height, 0, 0, 1,
1101 rgb_depths, rgb_channels, 1,
1103 if (ret < 0)
1104 return ret;
1105
1106 set->passes[set->nb_passes++] = (CUDAScalePass) {
1107 .func = s->cu_func_conv,
1108 .planes = CUDA_SCALE_PLANE_PRIMARY,
1109 .src = src,
1110 .dst = tmp,
1111 .src_desc = s->in_desc,
1112 .dst_desc = rgb_desc,
1113 };
1114 src = tmp++;
1115 }
1116
1117 for (int dir = CUDA_SCALE_DIR_X; dir <= CUDA_SCALE_DIR_Y; dir++) {
1118 const int last = dir == CUDA_SCALE_DIR_Y || !scale_y;
1119 const int to_out = last && s->yuv2rgb;
1120
1121 if (!(dir == CUDA_SCALE_DIR_X ? scale_x : scale_y))
1122 continue;
1123
1124 if (!to_out) {
1126 outlink->w,
1127 dir == CUDA_SCALE_DIR_Y ? outlink->h : in_height,
1128 0, 0, 1, rgb_depths, rgb_channels, 1,
1130 if (ret < 0)
1131 return ret;
1132 }
1133
1134 pass = &set->passes[set->nb_passes++];
1135 *pass = (CUDAScalePass) {
1136 .func = to_out ? s->cu_func_out[dir] :
1137 dir == CUDA_SCALE_DIR_X ? s->cu_func_tmp :
1138 s->cu_func_tmp_v,
1139 .planes = CUDA_SCALE_PLANE_PRIMARY,
1140 .src = src,
1141 .dst = to_out ? CUDA_SCALE_BUF_OUT : tmp,
1142 .src_desc = rgb_desc,
1143 .dst_desc = rgb_desc,
1144 };
1145 ret = dir == CUDA_SCALE_DIR_X ?
1146 cudascale_filter_init(ctx, &pass->filter, in_width, outlink->w, 0.0, 1) :
1147 cudascale_filter_init(ctx, &pass->filter, in_height, outlink->h, 0.0, 1);
1148 if (ret < 0)
1149 return ret;
1150 if (!to_out)
1151 src = tmp++;
1152 }
1153
1154 if (s->rgb2yuv) {
1155 set->passes[set->nb_passes++] = (CUDAScalePass) {
1156 .func = s->cu_func_conv,
1157 .func_uv = s->cu_func_conv_uv,
1159 .src = src,
1160 .dst = CUDA_SCALE_BUF_OUT,
1161 .src_desc = rgb_desc,
1162 .dst_desc = s->out_desc,
1163 };
1164 }
1165
1166 return 0;
1167}
1168
1171 int in_width, int in_height,
1172 int normalize_crop)
1173{
1174 CUDAScaleContext *s = ctx->priv;
1175 AVFilterLink *outlink = ctx->outputs[0];
1176 CUDAScalePassPlan plan;
1177 CUDAScalePass *pass_x = NULL, *pass_y = NULL, *pass;
1178 int ret;
1179
1180 const int in_sub_x = s->in_desc->log2_chroma_w;
1181 const int in_sub_y = s->in_desc->log2_chroma_h;
1182 const int out_sub_x = s->out_desc->log2_chroma_w;
1183 const int out_sub_y = s->out_desc->log2_chroma_h;
1184
1185 const int has_chroma = s->in_planes > 1 && s->out_planes > 1;
1186 const unsigned int all_planes = CUDA_SCALE_PLANE_PRIMARY |
1187 (has_chroma ? CUDA_SCALE_PLANE_CHROMA : 0);
1188 const unsigned int out_planes = CUDA_SCALE_PLANE_PRIMARY |
1189 (s->out_planes > 1 ? CUDA_SCALE_PLANE_CHROMA : 0);
1190
1191 set->in_width = in_width;
1192 set->in_height = in_height;
1193 set->normalize_crop = normalize_crop;
1194 set->in_use_linear = s->interp_use_linear;
1195 set->in_as_integer = s->interp_as_integer;
1196
1197 if ((s->yuv2rgb || s->rgb2yuv) &&
1198 (in_width != outlink->w || in_height != outlink->h))
1199 return cudascale_conv_set_init(ctx, set, in_width, in_height);
1200
1201 if (!s->use_filters) {
1202 /* The fixed function kernels scale and convert in a single pass,
1203 * as do the conversion kernels without scaling. */
1204 set->passes[set->nb_passes++] = (CUDAScalePass) {
1205 .func = s->cu_func_fixed,
1206 .func_uv = s->cu_func_fixed_uv,
1207 .planes = out_planes,
1208 .src = CUDA_SCALE_BUF_IN,
1209 .dst = CUDA_SCALE_BUF_OUT,
1210 .src_desc = s->in_desc,
1211 .dst_desc = s->out_desc,
1212 };
1213 return 0;
1214 }
1215
1217 in_width, in_height, outlink->w, outlink->h,
1218 in_sub_x, in_sub_y, out_sub_x, out_sub_y,
1219 has_chroma);
1220
1221 /* The generic path also handles non-scaling copies and format
1222 * conversions with a one-tap horizontal filter. */
1223 if (plan.dir[FILTER_OUT] == CUDA_SCALE_DIR_NONE)
1225
1226 if (plan.dir[FILTER_TMP] == CUDA_SCALE_DIR_X) {
1227 /* The intermediate keeps the input format, except for taking the
1228 * horizontal chroma subsampling of the output. A cropped input
1229 * needs all planes to be normalized. */
1230 pass_x = &set->passes[set->nb_passes++];
1231 *pass_x = (CUDAScalePass) {
1232 .func = s->cu_func_tmp,
1233 .func_uv = s->cu_func_tmp_uv,
1234 .planes = normalize_crop ? all_planes : plan.x_planes,
1235 .src = CUDA_SCALE_BUF_IN,
1236 .dst = CUDA_SCALE_BUF_TMP,
1237 .src_desc = s->in_desc,
1238 .dst_desc = s->in_desc,
1239 };
1240 }
1241
1242 pass = &set->passes[set->nb_passes++];
1243 *pass = (CUDAScalePass) {
1244 .func = s->cu_func_out[plan.dir[FILTER_OUT]],
1245 .func_uv = s->cu_func_out_uv[plan.dir[FILTER_OUT]],
1246 .planes = out_planes,
1247 .src = pass_x ? CUDA_SCALE_BUF_TMP : CUDA_SCALE_BUF_IN,
1248 .dst = CUDA_SCALE_BUF_OUT,
1249 .src_desc = s->in_desc,
1250 .dst_desc = s->out_desc,
1251 };
1252 if (plan.dir[FILTER_OUT] == CUDA_SCALE_DIR_X)
1253 pass_x = pass;
1254 else
1255 pass_y = pass;
1256
1257 if (pass_x) {
1258 const unsigned int planes = pass_x->dst == CUDA_SCALE_BUF_TMP ?
1259 pass_x->planes : all_planes;
1260
1262 ret = cudascale_filter_init(ctx, &pass_x->filter,
1263 in_width, outlink->w, 0.0,
1264 !!(plan.x_planes &
1266 if (ret < 0)
1267 return ret;
1268 }
1270 const int src_size = AV_CEIL_RSHIFT(in_width, in_sub_x);
1271 const int dst_size = AV_CEIL_RSHIFT(outlink->w, out_sub_x);
1272 const double virtual_size = cudascale_plane_virtual_size(
1273 src_size, in_width, outlink->w, in_sub_x, out_sub_x);
1275 src_size, dst_size, virtual_size,
1276 !!(plan.x_planes &
1278 if (ret < 0)
1279 return ret;
1280 }
1281 }
1282
1283 if (pass_y) {
1284 ret = cudascale_filter_init(ctx, &pass_y->filter,
1285 in_height, outlink->h, 0.0,
1286 !!(plan.y_planes &
1288 if (ret < 0)
1289 return ret;
1290 if (all_planes & CUDA_SCALE_PLANE_CHROMA) {
1291 const int src_size = AV_CEIL_RSHIFT(in_height, in_sub_y);
1292 const int dst_size = AV_CEIL_RSHIFT(outlink->h, out_sub_y);
1293 const double virtual_size = cudascale_plane_virtual_size(
1294 src_size, in_height, outlink->h, in_sub_y, out_sub_y);
1295 ret = cudascale_filter_init(ctx, &pass_y->filter_uv,
1296 src_size, dst_size, virtual_size,
1297 !!(plan.y_planes &
1299 if (ret < 0)
1300 return ret;
1301 }
1302 }
1303
1304 if (pass_x && pass_x->dst == CUDA_SCALE_BUF_TMP) {
1305 ret = inter_buf_init(ctx, &set->tmp[0],
1306 outlink->w, in_height,
1307 out_sub_x, in_sub_y, s->in_planes,
1308 s->in_plane_depths, s->in_plane_channels,
1309 s->interp_algo == INTERP_ALGO_LANCZOS,
1310 pass_x->planes);
1311 if (ret < 0)
1312 return ret;
1313 }
1314
1315 return 0;
1316}
1317
1319 int in_width, int in_height,
1320 int normalize_crop)
1321{
1322 CUDAScaleContext *s = ctx->priv;
1323 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
1324 CUDAScaleFilterSet next = { 0 };
1326 int ret;
1327
1328 /* Only the generic filters and conversions depend on the visible
1329 * input size. */
1330 if (s->filter_set.nb_passes &&
1331 ((!s->use_filters && !s->yuv2rgb && !s->rgb2yuv) ||
1332 (s->filter_set.in_width == in_width &&
1333 s->filter_set.in_height == in_height &&
1334 s->filter_set.normalize_crop == normalize_crop)))
1335 return 0;
1336
1337 ret = cudascale_filter_set_init(ctx, &next, in_width, in_height,
1338 normalize_crop);
1339 if (ret < 0)
1340 goto fail;
1341
1342 if (s->filter_set.nb_passes) {
1343 /* Queued kernels may still reference the active LUTs and texture. */
1344 ret = CHECK_CU(cu->cuStreamSynchronize(s->cu_stream));
1345 if (ret < 0)
1346 goto fail;
1347 }
1348
1349 old = s->filter_set;
1350 s->filter_set = next;
1351 memset(&next, 0, sizeof(next));
1352 filter_set_uninit(cu, &old);
1353
1355 "Prepared %d pass%s for visible input %dx%d%s\n",
1356 s->filter_set.nb_passes, s->filter_set.nb_passes > 1 ? "es" : "",
1357 in_width, in_height, normalize_crop ? " (cropped)" : "");
1358 return 0;
1359
1360fail:
1361 filter_set_uninit(cu, &next);
1362 return ret;
1363}
1364
1366 int in_width, int in_height)
1367{
1368 CUDAScaleContext *s = ctx->priv;
1369 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
1370 CUcontext dummy;
1371 int ret;
1372
1373 ret = CHECK_CU(cu->cuCtxPushCurrent(s->hwctx->cuda_ctx));
1374 if (ret < 0)
1375 return ret;
1376
1377 ret = cudascale_prepare_filter_set(ctx, in_width, in_height, 0);
1378
1379 CHECK_CU(cu->cuCtxPopCurrent(&dummy));
1380 return ret;
1381}
1382
1383/* All YCbCr matrices, preferring BT.601 (SMPTE 170M), the default of
1384 * RGB->YUV conversion. */
1386{
1388
1391 return NULL;
1392 for (int csp = 0; csp < AVCOL_SPC_NB; csp++) {
1393 if (csp == AVCOL_SPC_RGB || csp == AVCOL_SPC_RESERVED ||
1395 continue;
1396 if (ff_add_format(&ret, csp) < 0)
1397 return NULL;
1398 }
1399
1400 return ret;
1401}
1402
1403
1405 AVFilterFormatsConfig **cfg_in,
1406 AVFilterFormatsConfig **cfg_out)
1407{
1408 const CUDAScaleContext *s = ctx->priv;
1409 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE };
1410 static const int rgb_ranges[] = { AVCOL_RANGE_JPEG, AVCOL_RANGE_UNSPECIFIED, -1 };
1411 static const int yuv_ranges[] = { AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG, -1 };
1412 const AVPixFmtDescriptor *out_desc = s->format != AV_PIX_FMT_NONE ?
1413 av_pix_fmt_desc_get(s->format) : NULL;
1414 const int out_rgb = out_desc && (out_desc->flags & AV_PIX_FMT_FLAG_RGB);
1416 int ret;
1417
1418 if ((ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, pix_fmts)) < 0)
1419 return ret;
1420
1421 /* The software format of CUDA frames is unknown during negotiation, so
1422 * whether a YUV<->RGB conversion happens is only known at configuration
1423 * time. Negotiate the input and output properties independently.
1424 * Configuration rejects mismatches that the processing cannot produce.
1425 *
1426 * Unless set explicitly, the output properties are preferences rather
1427 * than constraints: a single value negotiated for the input, which
1428 * buffersrc does for ordinary YUV, is still followed by the output. YUV
1429 * output of unconstrained input, which all RGB input is, defaults to
1430 * BT.601 limited range; an unspecified range is resolved to limited.
1431 * RGB output prefers full range without forcing it, so that neither
1432 * a limited input range is followed, nor does it carry over to YUV
1433 * output converted from it again. */
1434 if ((ret = ff_formats_ref(ff_all_color_spaces(), &cfg_in[0]->color_spaces)) < 0 ||
1435 (ret = ff_formats_ref(ff_all_color_ranges(), &cfg_in[0]->color_ranges)) < 0)
1436 return ret;
1437
1438 if (out_rgb)
1440 else if (s->out_color_matrix != AVCOL_SPC_UNSPECIFIED)
1441 formats = ff_make_formats_list_singleton(s->out_color_matrix);
1442 else if (out_desc)
1443 /* YUV output, possibly converted from RGB, never carries RGB. */
1445 else
1447 if ((ret = ff_formats_ref(formats, &cfg_out[0]->color_spaces)) < 0)
1448 return ret;
1449
1450 if (out_rgb)
1451 formats = ff_make_format_list(rgb_ranges);
1452 else if (s->out_range != AVCOL_RANGE_UNSPECIFIED)
1454 else if (out_desc)
1455 formats = ff_make_format_list(yuv_ranges);
1456 else
1458 if ((ret = ff_formats_ref(formats, &cfg_out[0]->color_ranges)) < 0)
1459 return ret;
1460
1461 /* Without a requested format, the chroma sampling cannot change, so the
1462 * chroma location is shared. Otherwise, RGB output does not take on the
1463 * input location, and YUV output follows a single input location, but
1464 * prefers leaving it unspecified: RGB->YUV conversion describes its
1465 * centered chroma on the frames, while unconstrained YUV is not relabeled. */
1466 if (!out_desc)
1467 return ff_set_common_chroma_locations2(ctx, cfg_in, cfg_out,
1469
1471 &cfg_in[0]->chroma_locations)) < 0)
1472 return ret;
1475 if ((ret = ff_formats_ref(formats, &cfg_out[0]->chroma_locations)) < 0)
1476 return ret;
1477
1478 return 0;
1479}
1480
1482{
1483 AVFilterContext *ctx = outlink->src;
1484 AVFilterLink *inlink = outlink->src->inputs[0];
1485 FilterLink *inl = ff_filter_link(inlink);
1486 CUDAScaleContext *s = ctx->priv;
1487 AVHWFramesContext *frames_ctx;
1488 AVCUDADeviceContext *device_hwctx;
1489 int w, h;
1490 double w_adj = 1.0;
1491 int ret;
1492
1494 s->w_expr, s->h_expr,
1495 inlink, outlink,
1496 &w, &h)) < 0)
1497 goto fail;
1498
1499 if (s->reset_sar)
1500 w_adj = inlink->sample_aspect_ratio.num ?
1501 (double)inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
1502
1503 ret = ff_scale_adjust_dimensions(inlink, &w, &h,
1504 s->force_original_aspect_ratio,
1505 s->force_divisible_by, w_adj);
1506 if (ret < 0)
1507 goto fail;
1508
1509 if (((int64_t)h * inlink->w) > INT_MAX ||
1510 ((int64_t)w * inlink->h) > INT_MAX)
1511 av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
1512
1513 outlink->w = w;
1514 outlink->h = h;
1515
1516 ret = init_processing_chain(ctx, inlink->w, inlink->h, w, h);
1517 if (ret < 0)
1518 return ret;
1519
1520 frames_ctx = (AVHWFramesContext*)inl->hw_frames_ctx->data;
1521 device_hwctx = frames_ctx->device_ctx->hwctx;
1522
1523 s->hwctx = device_hwctx;
1524 s->cu_stream = s->hwctx->stream;
1525
1526 if (s->reset_sar)
1527 outlink->sample_aspect_ratio = (AVRational){1, 1};
1528 else if (inlink->sample_aspect_ratio.num) {
1529 outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
1530 outlink->w*inlink->h},
1531 inlink->sample_aspect_ratio);
1532 } else {
1533 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
1534 }
1535
1536 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s%s\n",
1537 inlink->w, inlink->h, av_get_pix_fmt_name(s->in_fmt),
1538 outlink->w, outlink->h, av_get_pix_fmt_name(s->out_fmt),
1539 s->passthrough ? " (passthrough)" : "");
1540
1542 if (ret < 0)
1543 return ret;
1544
1545 if (!s->passthrough) {
1546 ret = cudascale_setup_filters(ctx, inlink->w, inlink->h);
1547 if (ret < 0)
1548 return ret;
1549 }
1550
1551 return 0;
1552
1553fail:
1554 return ret;
1555}
1556
1558 int *width, int *height,
1559 int *normalize_crop)
1560{
1561 AVFilterLink *inlink = ctx->inputs[0];
1562 size_t frame_width, frame_height;
1563
1564 if (frame->width <= 0 || frame->height <= 0) {
1565 av_log(ctx, AV_LOG_ERROR, "Invalid crop rectangle\n");
1566 return AVERROR(EINVAL);
1567 }
1568
1569 frame_width = frame->width;
1570 frame_height = frame->height;
1571 if (frame->crop_left >= frame_width ||
1572 frame->crop_right >= frame_width - frame->crop_left ||
1573 frame->crop_top >= frame_height ||
1574 frame->crop_bottom >= frame_height - frame->crop_top) {
1575 av_log(ctx, AV_LOG_ERROR, "Invalid crop rectangle\n");
1576 return AVERROR(EINVAL);
1577 }
1578
1579 *width = (int)(frame_width - frame->crop_left - frame->crop_right);
1580 *height = (int)(frame_height - frame->crop_top - frame->crop_bottom);
1581 *normalize_crop = frame->crop_left || frame->crop_top ||
1582 *width != inlink->w || *height != inlink->h;
1583
1584 return 0;
1585}
1586
1587/* if depths/channels are NULL, only maps pointers without creating textures */
1589 const int depths[4], const int channels[4],
1590 CUDATex *tex, int use_linear, int as_integer)
1591{
1592 CUDAScaleContext *s = ctx->priv;
1593 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
1594
1595 const AVHWFramesContext *fctx = (const AVHWFramesContext*)frame->hw_frames_ctx->data;
1597 const int planes = av_pix_fmt_count_planes(fctx->sw_format);
1598
1599 *tex = (CUDATex) {
1600 .width = frame->width,
1601 .height = frame->height,
1602 .crop_left = frame->crop_left,
1603 .crop_top = frame->crop_top,
1604 .crop_width = (frame->width - frame->crop_right) - frame->crop_left,
1605 .crop_height = (frame->height - frame->crop_bottom) - frame->crop_top,
1606 .color_range = frame->color_range,
1607 .log2_chroma_w = desc->log2_chroma_w,
1608 .log2_chroma_h = desc->log2_chroma_h,
1609 .external_data = 1,
1610 };
1611
1612 for (int i = 0; i < planes; i++) {
1613 tex->data[i] = (CUdeviceptr)frame->data[i];
1614 tex->linesize[i] = frame->linesize[i];
1615 if (!depths || !channels)
1616 continue;
1617
1618 CUDA_TEXTURE_DESC tex_desc = {
1619 .filterMode = use_linear ?
1620 CU_TR_FILTER_MODE_LINEAR :
1621 CU_TR_FILTER_MODE_POINT,
1622 .flags = as_integer ? CU_TRSF_READ_AS_INTEGER : 0,
1623 };
1624
1625 const int is_chroma = i == 1 || i == 2;
1626 const int sub_x = is_chroma ? desc->log2_chroma_w : 0;
1627 const int sub_y = is_chroma ? desc->log2_chroma_h : 0;
1628 CUDA_RESOURCE_DESC res_desc = {
1629 .resType = CU_RESOURCE_TYPE_PITCH2D,
1630 .res.pitch2D.format = depths[i] <= 8 ?
1631 CU_AD_FORMAT_UNSIGNED_INT8 :
1632 CU_AD_FORMAT_UNSIGNED_INT16,
1633 .res.pitch2D.numChannels = channels[i],
1634 .res.pitch2D.pitchInBytes = tex->linesize[i],
1635 .res.pitch2D.devPtr = tex->data[i],
1636 .res.pitch2D.width = AV_CEIL_RSHIFT(frame->width, sub_x),
1637 .res.pitch2D.height = AV_CEIL_RSHIFT(frame->height, sub_y),
1638 };
1639
1640 int ret = CHECK_CU(cu->cuTexObjectCreate(&tex->tex[i], &res_desc, &tex_desc, NULL));
1641 if (ret < 0) {
1642 cuda_tex_uninit(cu, tex);
1643 return ret;
1644 }
1645 }
1646
1647 return 0;
1648}
1649
1651 CUfunction func, const CUDAScaleFilter *filter,
1652 const CUtexObject src_tex[4],
1653 int src_left, int src_top, int src_width, int src_height,
1654 const CUdeviceptr out_data[4],
1655 int dst_width, int dst_height, int dst_pitch, int mpeg_range)
1656{
1657 CUDAScaleContext *s = ctx->priv;
1658 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
1659 const AVPixFmtDescriptor *src_desc = pass->src_desc;
1660 const AVPixFmtDescriptor *dst_desc = pass->dst_desc;
1661
1663 .src_tex = {src_tex[0], src_tex[1], src_tex[2], src_tex[3]},
1664 .dst = {
1665 out_data[0],
1666 out_data[1],
1667 out_data[2],
1668 out_data[3]
1669 },
1670 .dst_width = dst_width,
1671 .dst_height = dst_height,
1672 .dst_pitch = dst_pitch,
1673 .src_left = src_left,
1674 .src_top = src_top,
1675 .src_width = src_width,
1676 .src_height = src_height,
1677 .param = s->param,
1678 .mpeg_range = mpeg_range,
1679 .weights = filter->weights,
1680 .offsets = filter->offsets,
1681 .filter_size = filter->filter_size,
1682 /* Supported formats use the same depth and shift for every
1683 * component. */
1684 .src_depth = src_desc->comp[0].depth,
1685 .src_storage_max = ((1U << src_desc->comp[0].depth) - 1) <<
1686 src_desc->comp[0].shift,
1687 .color_matrix = s->color_matrix,
1688 .dst_depth = dst_desc->comp[0].depth,
1689 .dst_shift = dst_desc->comp[0].shift,
1690 };
1691
1692 /* Color conversion kernels need the subsampling of the YUV side. */
1693 if (!(src_desc->flags & AV_PIX_FMT_FLAG_RGB) &&
1694 (dst_desc->flags & AV_PIX_FMT_FLAG_RGB)) {
1695 params.log2_chroma_w = src_desc->log2_chroma_w;
1696 params.log2_chroma_h = src_desc->log2_chroma_h;
1697 } else if ((src_desc->flags & AV_PIX_FMT_FLAG_RGB) &&
1698 !(dst_desc->flags & AV_PIX_FMT_FLAG_RGB)) {
1699 params.log2_chroma_w = dst_desc->log2_chroma_w;
1700 params.log2_chroma_h = dst_desc->log2_chroma_h;
1701 }
1702
1703 void *args[] = { &params };
1704
1705 return CHECK_CU(cu->cuLaunchKernel(func,
1706 DIV_UP(dst_width, BLOCKX), DIV_UP(dst_height, BLOCKY), 1,
1707 BLOCKX, BLOCKY, 1, 0, s->cu_stream, args, NULL));
1708}
1709
1711 const CUDATex *out, const CUDATex *in)
1712{
1713 CUDAScaleContext *s = ctx->priv;
1714 /* The color matrix was computed at config time, so color conversions use
1715 * the resolved color range to keep mpeg_range consistent with it. Other
1716 * passes follow the frame the source was produced from. */
1717 const int conversion = (pass->src_desc->flags ^ pass->dst_desc->flags) &
1719 const int mpeg_range = conversion ?
1720 (s->color_range != AVCOL_RANGE_JPEG) :
1722 int ret;
1723
1724 if (pass->planes & CUDA_SCALE_PLANE_PRIMARY) {
1725 // scale primary plane(s). Usually Y (and A), or single plane of RGB frames.
1726 ret = call_resize_kernel(ctx, pass, pass->func, &pass->filter,
1727 in->tex, in->crop_left, in->crop_top,
1728 in->crop_width, in->crop_height,
1729 out->data, out->width, out->height,
1730 out->linesize[0], mpeg_range);
1731 if (ret < 0)
1732 return ret;
1733 }
1734
1735 if (pass->planes & CUDA_SCALE_PLANE_CHROMA) {
1736 // scale UV plane. Scale function sets both U and V plane, or singular interleaved plane.
1737 /* Match av_frame_apply_cropping(): subsampled plane origins round
1738 * down, while visible plane extents round up. */
1739 ret = call_resize_kernel(ctx, pass, pass->func_uv, &pass->filter_uv,
1740 in->tex,
1741 in->crop_left >> in->log2_chroma_w,
1742 in->crop_top >> in->log2_chroma_h,
1745 out->data,
1746 AV_CEIL_RSHIFT(out->width, out->log2_chroma_w),
1747 AV_CEIL_RSHIFT(out->height, out->log2_chroma_h),
1748 out->linesize[1], mpeg_range);
1749 if (ret < 0)
1750 return ret;
1751 }
1752
1753 return 0;
1754}
1755
1756/* Describe an output frame consistently with the negotiated output link. */
1758{
1759 CUDAScaleContext *s = ctx->priv;
1760 AVFilterLink *outlink = ctx->outputs[0];
1761 const int out_sub = s->out_desc->log2_chroma_w || s->out_desc->log2_chroma_h;
1762
1763 if (s->yuv2rgb) {
1764 /* RGB is full range, also where the link leaves it unspecified. */
1765 frame->colorspace = AVCOL_SPC_RGB;
1766 frame->color_range = AVCOL_RANGE_JPEG;
1767 frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
1768 } else if (s->rgb2yuv) {
1769 /* The conversion matrix was built from the negotiated link
1770 * properties, and the chroma averaging centers the samples. */
1771 frame->colorspace = outlink->colorspace;
1772 frame->color_range = outlink->color_range;
1773 frame->chroma_location = out_sub ? AVCHROMA_LOC_CENTER :
1775 } else if (!(s->out_desc->flags & AV_PIX_FMT_FLAG_RGB)) {
1776 /* Plain scaling keeps the input description, but takes on the
1777 * negotiated one where the input is unspecified. */
1778 if (frame->colorspace == AVCOL_SPC_UNSPECIFIED)
1779 frame->colorspace = outlink->colorspace;
1780 if (frame->color_range == AVCOL_RANGE_UNSPECIFIED)
1781 frame->color_range = outlink->color_range;
1782 if (out_sub && frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
1783 frame->chroma_location = outlink->chroma_location;
1784 }
1785}
1786
1788 int in_width, int in_height, int normalize_crop)
1789{
1790 CUDAScaleContext *s = ctx->priv;
1791 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
1792 AVFilterLink *outlink = ctx->outputs[0];
1793 int ret = 0;
1794
1795 ret = cudascale_prepare_filter_set(ctx, in_width, in_height,
1796 normalize_crop);
1797 if (ret < 0)
1798 return ret;
1799
1800 CUDATex in_tex = {0}, out_tex = {0};
1801 ret = cuda_tex_map_frame(ctx, in, s->in_plane_depths,
1802 s->in_plane_channels, &in_tex,
1803 s->filter_set.in_use_linear,
1804 s->filter_set.in_as_integer);
1805 if (ret < 0)
1806 goto fail;
1807
1808 ret = cuda_tex_map_frame(ctx, s->frame, NULL, NULL, &out_tex, 0, 0);
1809 if (ret < 0)
1810 goto fail;
1811
1812 /* Local views of the buffers; intermediates borrow textures below. */
1813 CUDATex bufs[CUDA_SCALE_BUF_NB] = {
1814 [CUDA_SCALE_BUF_IN] = in_tex,
1815 [CUDA_SCALE_BUF_TMP] = s->filter_set.tmp[0],
1816 [CUDA_SCALE_BUF_TMP2] = s->filter_set.tmp[1],
1817 [CUDA_SCALE_BUF_OUT] = out_tex,
1818 };
1819
1820 for (int i = 0; i < s->filter_set.nb_passes; i++) {
1821 const CUDAScalePass *pass = &s->filter_set.passes[i];
1822 const CUDATex *src = &bufs[pass->src];
1823 CUDATex *dst = &bufs[pass->dst];
1824
1825 ret = cudascale_run_pass(ctx, pass, dst, src);
1826 if (ret < 0)
1827 goto fail;
1828
1829 if (pass->dst != CUDA_SCALE_BUF_OUT) {
1830 /* Later passes read the plane groups this pass did not write
1831 * from its source, and follow the range of the frame. */
1832 for (int j = 0; j < FF_ARRAY_ELEMS(dst->tex); j++) {
1833 if (!dst->tex[j])
1834 dst->tex[j] = src->tex[j];
1835 }
1836 dst->color_range = src->color_range;
1837 }
1838 }
1839
1840 ret = av_hwframe_get_buffer(s->frame->hw_frames_ctx, s->tmp_frame, 0);
1841 if (ret < 0)
1842 goto fail;
1843
1844 av_frame_move_ref(out, s->frame);
1845 av_frame_move_ref(s->frame, s->tmp_frame);
1846
1847 s->frame->width = outlink->w;
1848 s->frame->height = outlink->h;
1849
1851 if (ret < 0)
1852 goto fail;
1853
1855
1856 out->crop_top = 0;
1857 out->crop_bottom = 0;
1858 out->crop_left = 0;
1859 out->crop_right = 0;
1860
1861 if (in->crop_left || in->crop_right ||
1862 in->crop_top || in->crop_bottom ||
1863 out->width != in_width || out->height != in_height) {
1864 av_frame_side_data_remove_by_props(&out->side_data, &out->nb_side_data,
1866 }
1867
1868fail:
1869 cuda_tex_uninit(cu, &in_tex);
1870 cuda_tex_uninit(cu, &out_tex);
1871 return ret;
1872}
1873
1875{
1876 AVFilterContext *ctx = link->dst;
1877 CUDAScaleContext *s = ctx->priv;
1878 AVFilterLink *outlink = ctx->outputs[0];
1879 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
1880
1881 AVFrame *out = NULL;
1882 CUcontext dummy;
1883 int in_width, in_height, normalize_crop;
1884 int ret = 0;
1885
1886 if (s->passthrough) {
1888 return ff_filter_frame(outlink, in);
1889 }
1890
1891 ret = cudascale_frame_geometry(ctx, in, &in_width, &in_height,
1892 &normalize_crop);
1893 if (ret < 0)
1894 goto fail;
1895
1896 out = av_frame_alloc();
1897 if (!out) {
1898 ret = AVERROR(ENOMEM);
1899 goto fail;
1900 }
1901
1902 ret = CHECK_CU(cu->cuCtxPushCurrent(s->hwctx->cuda_ctx));
1903 if (ret < 0)
1904 goto fail;
1905
1906 ret = cudascale_scale(ctx, out, in, in_width, in_height,
1907 normalize_crop);
1908
1909 CHECK_CU(cu->cuCtxPopCurrent(&dummy));
1910 if (ret < 0)
1911 goto fail;
1912
1913 if (s->reset_sar) {
1914 out->sample_aspect_ratio = (AVRational){1, 1};
1915 } else {
1916 av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
1917 (int64_t)in->sample_aspect_ratio.num * outlink->h * in_width,
1918 (int64_t)in->sample_aspect_ratio.den * outlink->w * in_height,
1919 INT_MAX);
1920 }
1921
1922 av_frame_free(&in);
1923 return ff_filter_frame(outlink, out);
1924fail:
1925 av_frame_free(&in);
1927 return ret;
1928}
1929
1931{
1932 CUDAScaleContext *s = inlink->dst->priv;
1933
1934 return s->passthrough ?
1935 ff_null_get_video_buffer (inlink, w, h) :
1937}
1938
1939#define OFFSET(x) offsetof(CUDAScaleContext, x)
1940#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
1941static const AVOption options[] = {
1942 { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
1943 { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
1944 { "interp_algo", "Interpolation algorithm used for resizing", OFFSET(interp_algo), AV_OPT_TYPE_INT, { .i64 = INTERP_ALGO_DEFAULT }, 0, INTERP_ALGO_COUNT - 1, FLAGS, .unit = "interp_algo" },
1945 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, { .i64 = INTERP_ALGO_NEAREST }, 0, 0, FLAGS, .unit = "interp_algo" },
1946 { "bilinear", "bilinear", 0, AV_OPT_TYPE_CONST, { .i64 = INTERP_ALGO_BILINEAR }, 0, 0, FLAGS, .unit = "interp_algo" },
1947 { "bicubic", "bicubic", 0, AV_OPT_TYPE_CONST, { .i64 = INTERP_ALGO_BICUBIC }, 0, 0, FLAGS, .unit = "interp_algo" },
1948 { "lanczos", "lanczos", 0, AV_OPT_TYPE_CONST, { .i64 = INTERP_ALGO_LANCZOS }, 0, 0, FLAGS, .unit = "interp_algo" },
1949 { "format", "Output video pixel format", OFFSET(format), AV_OPT_TYPE_PIXEL_FMT, { .i64 = AV_PIX_FMT_NONE }, INT_MIN, INT_MAX, .flags=FLAGS },
1950 { "passthrough", "Do not process frames at all if parameters match", OFFSET(passthrough), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
1951 { "use_filters", "Use generic filters instead of fixed function kernels", OFFSET(use_filters_opt), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, FLAGS, .unit = "use_filters" },
1952 { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, FLAGS, .unit = "use_filters" },
1953 { "param", "Algorithm-Specific parameter", OFFSET(param), AV_OPT_TYPE_FLOAT, { .dbl = SCALE_CUDA_PARAM_DEFAULT }, -FLT_MAX, FLT_MAX, FLAGS },
1954 { "force_original_aspect_ratio", "decrease or increase w/h if necessary to keep the original AR", OFFSET(force_original_aspect_ratio), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, SCALE_FORCE_OAR_NB-1, FLAGS, .unit = "force_oar" },
1955 { "disable", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = SCALE_FORCE_OAR_DISABLE }, 0, 0, FLAGS, .unit = "force_oar" },
1956 { "decrease", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = SCALE_FORCE_OAR_DECREASE }, 0, 0, FLAGS, .unit = "force_oar" },
1957 { "increase", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = SCALE_FORCE_OAR_INCREASE }, 0, 0, FLAGS, .unit = "force_oar" },
1958 { "force_divisible_by", "enforce that the output resolution is divisible by a defined integer when force_original_aspect_ratio is used", OFFSET(force_divisible_by), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 256, FLAGS },
1959 { "reset_sar", "reset SAR to 1 and scale to square pixels if scaling proportionally", OFFSET(reset_sar), AV_OPT_TYPE_BOOL, { .i64 = 0}, 0, 1, FLAGS },
1960 { "out_range", "Output color range", OFFSET(out_range), AV_OPT_TYPE_INT, { .i64 = AVCOL_RANGE_UNSPECIFIED }, AVCOL_RANGE_UNSPECIFIED, AVCOL_RANGE_JPEG, FLAGS, .unit = "range" },
1961 { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_UNSPECIFIED }, 0, 0, FLAGS, .unit = "range" },
1962 { "full", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_JPEG }, 0, 0, FLAGS, .unit = "range" },
1963 { "limited", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_MPEG }, 0, 0, FLAGS, .unit = "range" },
1964 { "jpeg", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_JPEG }, 0, 0, FLAGS, .unit = "range" },
1965 { "mpeg", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_MPEG }, 0, 0, FLAGS, .unit = "range" },
1966 { "tv", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_MPEG }, 0, 0, FLAGS, .unit = "range" },
1967 { "pc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_JPEG }, 0, 0, FLAGS, .unit = "range" },
1968 { "out_color_matrix", "Output YCbCr matrix", OFFSET(out_color_matrix), AV_OPT_TYPE_INT, { .i64 = AVCOL_SPC_UNSPECIFIED }, AVCOL_SPC_BT709, AVCOL_SPC_NB - 1, FLAGS, .unit = "color" },
1969 { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_UNSPECIFIED }, 0, 0, FLAGS, .unit = "color" },
1970 { "bt601", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_SMPTE170M }, 0, 0, FLAGS, .unit = "color" },
1971 { "smpte170m", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_SMPTE170M }, 0, 0, FLAGS, .unit = "color" },
1972 { "bt470", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_BT470BG }, 0, 0, FLAGS, .unit = "color" },
1973 { "bt470bg", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_BT470BG }, 0, 0, FLAGS, .unit = "color" },
1974 { "bt709", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_BT709 }, 0, 0, FLAGS, .unit = "color" },
1975 { "fcc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_FCC }, 0, 0, FLAGS, .unit = "color" },
1976 { "smpte240m", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_SMPTE240M }, 0, 0, FLAGS, .unit = "color" },
1977 { "ycgco", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_YCGCO }, 0, 0, FLAGS, .unit = "color" },
1978 { "bt2020", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_BT2020_NCL }, 0, 0, FLAGS, .unit = "color" },
1979 { "bt2020nc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_BT2020_NCL }, 0, 0, FLAGS, .unit = "color" },
1980 { NULL },
1981};
1982
1983static const AVClass cudascale_class = {
1984 .class_name = "cudascale",
1985 .item_name = av_default_item_name,
1986 .option = options,
1987 .version = LIBAVUTIL_VERSION_INT,
1988};
1989
1991 {
1992 .name = "default",
1993 .type = AVMEDIA_TYPE_VIDEO,
1994 .filter_frame = cudascale_filter_frame,
1995 .get_buffer.video = cudascale_get_video_buffer,
1996 },
1997};
1998
2000 {
2001 .name = "default",
2002 .type = AVMEDIA_TYPE_VIDEO,
2003 .config_props = cudascale_config_props,
2004 },
2005};
2006
2008 .p.name = "scale_cuda",
2009 .p.description = NULL_IF_CONFIG_SMALL("GPU accelerated video resizer"),
2010
2011 .p.priv_class = &cudascale_class,
2012
2013 .init = cudascale_init,
2014 .uninit = cudascale_uninit,
2015
2016 .priv_size = sizeof(CUDAScaleContext),
2017
2020
2022
2023 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
2024};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
SwsAArch64OpImplParams params
Definition ops.c:52
static const char *const format[]
Definition af_aiir.c:445
const FFFilter ff_vf_scale_cuda
static FILE * out
static int out_size
static AVFormatContext * ctx
channels
Definition aptx.h:31
simple assert() macros that are a bit more flexible than ISO C assert().
@ FILTER_NB
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1112
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:596
static void fn rgb2yuv(uint8_t *_yuv[3], const ptrdiff_t yuv_stride[3], int16_t *rgb[3], ptrdiff_t s, int w, int h, const int16_t rgb2yuv_coeffs[3][3][8], const int16_t yuv_offset[8])
common internal and external API header
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
Colorspace value utility functions for libavutil.
static AVFrame * frame
int ret
Definition ffplay.c:391
static int dummy
Definition ffplay.c:4453
const char * ff_sws_scaler_name(SwsScaler scaler)
Return the friendly name of a given scaler enum, or NULL if invalid.
Definition filters.c:510
int ff_sws_filter_generate(void *log, const SwsFilterParams *params, SwsFilterWeights **out)
Generate a filter kernel for the given parameters.
Definition filters.c:203
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add ref as a new reference to formats.
Definition formats.c:776
AVFilterFormats * ff_all_color_spaces(void)
Construct an AVFilterFormats representing all possible color spaces.
Definition formats.c:706
AVFilterFormats * ff_make_formats_list_singleton(int fmt)
Equivalent to ff_make_format_list({const int[]}{ fmt, -1 })
Definition formats.c:605
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition formats.c:580
int ff_set_common_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const int *fmts)
Definition formats.c:1209
int ff_set_common_chroma_locations2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *chroma_locations)
Definition formats.c:1175
AVFilterFormats * ff_all_color_ranges(void)
Construct an AVFilterFormats representing all possible color ranges.
Definition formats.c:722
AVFilterFormats * ff_all_chroma_locations(void)
Construct an AVFilterFormats representing all possible chroma locations.
Definition formats.c:744
av_warn_unused_result AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
static void yuv2rgb(uint8_t *out, int ridx, int Y, int U, int V)
Definition g2meet.c:264
#define fail
Definition test.h:479
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_PIXEL_FMT
Underlying C type is enum AVPixelFormat.
Definition opt.h:306
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
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:140
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition buffer.c:104
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR(e)
Definition error.h:45
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:500
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition frame.c:527
void av_frame_side_data_remove_by_props(AVFrameSideData ***sd, int *nb_sd, int props)
Remove and free all side data instances that match any of the given side data properties.
Definition side_data.c:124
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:603
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:53
@ AV_SIDE_DATA_PROP_SIZE_DEPENDENT
Side data depends on the video dimensions.
Definition frame.h:361
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition log.h:204
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:247
const struct AVLumaCoefficients * av_csp_luma_coeffs_from_avcsp(enum AVColorSpace csp)
Retrieves the Luma coefficients necessary to construct a conversion matrix from an enum constant desc...
Definition csp.c:58
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition rational.c:80
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition rational.c:35
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define SWS_PARAM_DEFAULT
Definition swscale.h:456
@ SWS_SCALE_POINT
nearest neighbor (point sampling)
Definition swscale.h:100
@ SWS_SCALE_LANCZOS
3-tap sinc/sinc
Definition swscale.h:104
@ SWS_SCALE_BILINEAR
bilinear filtering
Definition swscale.h:98
@ SWS_SCALE_BICUBIC
2-tap cubic BC-spline
Definition swscale.h:99
static const int weights[]
Definition hevc_pel.c:32
static av_always_inline void scale(vec_s32 res[4], vec_s16 res_packed[2], const int shift)
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition hwcontext.c:347
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition hwcontext.c:267
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
Definition hwcontext.c:564
FFmpeg internal API for CUDA.
static int unsupported(AVCodecContext *avctx)
Definition iff.c:1461
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition jacosubdec.c:67
void ff_matrix_invert_3x3(const double in[3][3], double out[3][3])
Definition colorspace.c:27
void ff_fill_rgb2yuv_table(const AVLumaCoefficients *coeffs, double rgb2yuv[3][3])
Definition colorspace.c:127
#define FILTER_INPUTS(array)
Definition filters.h:279
#define FILTER_OUTPUTS(array)
Definition filters.h:280
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition filters.h:208
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:256
#define av_cold
Definition attributes.h:117
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:98
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
const char * desc
Definition libsvtav1.c:110
@ SWS_FILTER_SIZE_MAX
Definition filters.h:41
@ SWS_FILTER_SCALE
14-bit coefficients are picked to fit comfortably within int16_t for efficient SIMD processing (e....
Definition filters.h:40
static const struct @257111027162314367033347246032313251342043035002 planes[]
uint8_t w
Definition llvidencdsp.c:39
int ff_cuda_load_module(void *avctx, AVCUDADeviceContext *hwctx, CUmodule *cu_module, const unsigned char *data, const unsigned int length)
Loads a CUDA module and applies any decompression, if necessary.
Definition load_helper.c:34
#define FFMAX(a, b)
Definition macros.h:47
#define FFALIGN(x, a)
Definition macros.h:78
Memory handling functions.
AVOptions.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3524
const char * av_color_space_name(enum AVColorSpace space)
Definition pixdesc.c:3884
const char * av_color_range_name(enum AVColorRange range)
Definition pixdesc.c:3800
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3404
const char * av_chroma_location_name(enum AVChromaLocation location)
Definition pixdesc.c:3905
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition pixdesc.h:136
#define AV_PIX_FMT_0RGB32
Definition pixfmt.h:524
#define AV_PIX_FMT_P212
Definition pixfmt.h:629
@ AVCHROMA_LOC_CENTER
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
Definition pixfmt.h:814
@ AVCHROMA_LOC_UNSPECIFIED
Definition pixfmt.h:812
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:548
AVColorRange
Visual content value range.
Definition pixfmt.h:757
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:775
@ AVCOL_RANGE_UNSPECIFIED
Definition pixfmt.h:758
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:792
#define AV_PIX_FMT_P210
Definition pixfmt.h:627
#define AV_PIX_FMT_P012
Definition pixfmt.h:614
#define AV_PIX_FMT_P216
Definition pixfmt.h:631
#define AV_PIX_FMT_P010
Definition pixfmt.h:613
#define AV_PIX_FMT_P016
Definition pixfmt.h:615
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:549
#define AV_PIX_FMT_YUV444P12MSB
Definition pixfmt.h:564
#define AV_PIX_FMT_BGR32
Definition pixfmt.h:522
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition pixfmt.h:96
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition pixfmt.h:260
@ AV_PIX_FMT_NV16
interleaved chroma YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:198
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
#define AV_PIX_FMT_YUV444P16
Definition pixfmt.h:561
#define AV_PIX_FMT_YUV444P10MSB
Definition pixfmt.h:563
#define AV_PIX_FMT_0BGR32
Definition pixfmt.h:525
#define AV_PIX_FMT_RGB32
Definition pixfmt.h:520
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:551
AVColorSpace
YUV colorspace type.
Definition pixfmt.h:711
@ AVCOL_SPC_YCGCO_RE
YCgCo-R, even addition of bits.
Definition pixfmt.h:729
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
Definition pixfmt.h:713
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
Definition pixfmt.h:717
@ AVCOL_SPC_BT2020_CL
ITU-R BT2020 constant luminance system.
Definition pixfmt.h:723
@ AVCOL_SPC_NB
Not part of ABI.
Definition pixfmt.h:731
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
Definition pixfmt.h:712
@ AVCOL_SPC_BT2020_NCL
ITU-R BT2020 non-constant luminance system.
Definition pixfmt.h:722
@ AVCOL_SPC_UNSPECIFIED
Definition pixfmt.h:714
@ AVCOL_SPC_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above
Definition pixfmt.h:718
@ AVCOL_SPC_FCC
FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
Definition pixfmt.h:716
@ AVCOL_SPC_SMPTE240M
derived from 170M primaries and D65 white point, 170M is derived from BT470 System M's primaries
Definition pixfmt.h:719
@ AVCOL_SPC_YCGCO_RO
YCgCo-R, odd addition of bits.
Definition pixfmt.h:730
@ AVCOL_SPC_YCGCO
used by Dirac / VC-2 and H.264 FRext, see ITU-T SG16
Definition pixfmt.h:720
@ AVCOL_SPC_RESERVED
reserved for future use by ITU-T and ISO/IEC just like 15-255 are
Definition pixfmt.h:715
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition refstruct.c:121
int ff_scale_adjust_dimensions(AVFilterLink *inlink, int *ret_w, int *ret_h, int force_original_aspect_ratio, int force_divisible_by, double w_adj)
Transform evaluated width and height obtained from ff_scale_eval_dimensions into actual target width ...
Definition scale_eval.c:125
int ff_scale_eval_dimensions(void *log_ctx, const char *w_expr, const char *h_expr, AVFilterLink *inlink, AVFilterLink *outlink, int *ret_w, int *ret_h)
Parse and evaluate string expressions for width and height.
Definition scale_eval.c:73
@ SCALE_FORCE_OAR_DISABLE
Definition scale_eval.h:25
@ SCALE_FORCE_OAR_NB
Definition scale_eval.h:28
@ SCALE_FORCE_OAR_INCREASE
Definition scale_eval.h:27
@ SCALE_FORCE_OAR_DECREASE
Definition scale_eval.h:26
formats
Definition signature.h:47
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
Definition buffer.h:82
uint8_t * data
The data buffer.
Definition buffer.h:90
This struct is allocated as AVHWDeviceContext.hwctx.
Describe the class of an AVClass context structure.
Definition log.h:76
int shift
Number of least significant bits that must be shifted away to get the value.
Definition pixdesc.h:52
int depth
Number of bits in the component.
Definition pixdesc.h:57
An instance of a filter.
Definition avfilter.h:279
AVFilterLink ** inputs
array of pointers to input links
Definition avfilter.h:287
void * priv
private data for use by the filter
Definition avfilter.h:294
Lists of formats / etc.
Definition avfilter.h:120
A list of supported formats for one end of a filter link.
Definition formats.h:64
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
size_t crop_right
Definition frame.h:808
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition frame.h:576
size_t crop_top
Definition frame.h:805
size_t crop_left
Definition frame.h:807
size_t crop_bottom
Definition frame.h:806
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition hwcontext.h:88
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition hwcontext.h:200
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
Definition hwcontext.h:129
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition hwcontext.h:213
int width
The allocated dimensions of the frames in this pool.
Definition hwcontext.h:220
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Definition hwcontext.h:137
Struct containing luma coefficients to be used for RGB to YUV/YCoCg, or similar calculations.
Definition csp.h:48
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition pixdesc.h:105
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition pixdesc.h:80
uint64_t flags
Combination of AV_PIX_FMT_FLAG_... flags.
Definition pixdesc.h:94
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition pixdesc.h:89
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
CUfunction cu_func_conv
conversion with a float RGB side
int out_range
requested output color range (AVColorRange)
enum AVPixelFormat format
Output sw format.
CUfunction cu_func_tmp
CUfunction cu_func_tmp_uv
const AVPixFmtDescriptor * in_desc
enum AVPixelFormat in_fmt out_fmt
CUfunction cu_func_tmp_v
vertical float pass before RGB->YUV
int out_color_matrix
requested output YCbCr matrix (AVColorSpace)
AVCUDADeviceContext * hwctx
CUDAScaleFilterSet filter_set
char * w_expr
width expression string
enum AVColorRange color_range
resolved color range for conversion
const AVPixFmtDescriptor * out_desc
CUfunction cu_func_fixed_uv
CUfunction cu_func_conv_uv
char * h_expr
height expression string
CUfunction cu_func_out[2]
CUDAScaleColorMatrix color_matrix
CUfunction cu_func_out_uv[2]
CUfunction cu_func_fixed
AVBufferRef * frames_ctx
int in_as_integer
integer texture reads of the input
CUDATex tmp[2]
CUDA_SCALE_BUF_TMP, CUDA_SCALE_BUF_TMP2.
int in_use_linear
texture filtering of the input
CUDAScalePass passes[CUDA_SCALE_MAX_PASSES]
CUdeviceptr weights
float[dst_size][filter_size]
CUdeviceptr offsets
int[dst_size]
unsigned int y_planes
unsigned int x_planes
int dir[FILTER_NB]
const AVPixFmtDescriptor * src_desc
sample format read
CUfunction func_uv
kernel for the chroma plane(s)
CUfunction func
kernel for the primary plane(s)
CUDAScaleFilter filter
generic filter weights for func
unsigned int planes
plane groups written, CUDA_SCALE_PLANE_*
int dst
CUDA_SCALE_BUF_*.
CUDAScaleFilter filter_uv
generic filter weights for func_uv
const AVPixFmtDescriptor * dst_desc
sample format written
int external_data
CUtexObject tex[4]
CUdeviceptr data[4]
int color_range
int log2_chroma_h
int linesize[4]
int crop_height
int log2_chroma_w
Represents a computed filter kernel.
Definition filters.h:91
enum AVPixelFormat format
char name[13]
#define av_free(p)
#define av_malloc_array(a, b)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
#define src
Definition vp8dsp.c:248
#define height
Definition dsp.h:99
#define width
Definition dsp.h:99
static void set(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f, double v)
Definition swresample.c:56
static enum AVPixelFormat supported_formats[]
#define DIV_UP(a, b)
#define BLOCKX
#define BLOCKY
@ CUDA_SCALE_BUF_IN
@ CUDA_SCALE_BUF_NB
@ CUDA_SCALE_BUF_TMP2
@ CUDA_SCALE_BUF_OUT
@ CUDA_SCALE_BUF_TMP
static int cudascale_filter_set_init(AVFilterContext *ctx, CUDAScaleFilterSet *set, int in_width, int in_height, int normalize_crop)
static int inter_buf_init(AVFilterContext *ctx, CUDATex *tex, int width, int height, int log2_chroma_w, int log2_chroma_h, int nb_planes, const int depths[4], const int channels[4], int use_float, unsigned int planes)
static double cudascale_plane_virtual_size(int src_size, int in_size, int out_size, int in_sub, int out_sub)
@ CUDA_SCALE_PLANE_PRIMARY
@ CUDA_SCALE_PLANE_CHROMA
static int call_resize_kernel(AVFilterContext *ctx, const CUDAScalePass *pass, CUfunction func, const CUDAScaleFilter *filter, const CUtexObject src_tex[4], int src_left, int src_top, int src_width, int src_height, const CUdeviceptr out_data[4], int dst_width, int dst_height, int dst_pitch, int mpeg_range)
static int cudascale_plane_needs_scale(int in_size, int out_size, int in_sub, int out_sub)
static av_cold int cudascale_setup_filters(AVFilterContext *ctx, int in_width, int in_height)
static int cudascale_conflicts(int in, int out, int unspecified)
static int cudascale_filter_init(AVFilterContext *ctx, CUDAScaleFilter *f, int src_size, int dst_size, double virtual_size, int needs_scale)
static int cudascale_plane_is_downscaled(int in_size, int out_size, int in_sub, int out_sub)
static av_cold int init_hwframe_ctx(CUDAScaleContext *s, AVBufferRef *device_ctx, int width, int height)
static av_cold void cudascale_uninit(AVFilterContext *ctx)
static int cudascale_frame_geometry(AVFilterContext *ctx, const AVFrame *frame, int *width, int *height, int *normalize_crop)
static int cudascale_scale(AVFilterContext *ctx, AVFrame *out, AVFrame *in, int in_width, int in_height, int normalize_crop)
#define DIV_UP(a, b)
static av_cold int cudascale_config_props(AVFilterLink *outlink)
static av_cold int init_processing_chain(AVFilterContext *ctx, int in_width, int in_height, int out_width, int out_height)
static int compute_yuv2rgb_matrix(void *log_ctx, CUDAScaleColorMatrix *mat, enum AVColorSpace colorspace, bool limited_range)
static int cudascale_conv_set_init(AVFilterContext *ctx, CUDAScaleFilterSet *set, int in_width, int in_height)
static av_cold int cudascale_init(AVFilterContext *ctx)
static const AVClass cudascale_class
static int compute_rgb2yuv_matrix(void *log_ctx, CUDAScaleColorMatrix *mat, enum AVColorSpace colorspace, bool limited_range)
static av_cold int cudascale_load_functions(AVFilterContext *ctx)
static const char * get_format_name(enum AVPixelFormat fmt)
static int cudascale_run_pass(AVFilterContext *ctx, const CUDAScalePass *pass, const CUDATex *out, const CUDATex *in)
static void filter_set_uninit(CudaFunctions *cu, CUDAScaleFilterSet *set)
@ INTERP_ALGO_LANCZOS
@ INTERP_ALGO_BICUBIC
@ INTERP_ALGO_DEFAULT
@ INTERP_ALGO_BILINEAR
@ INTERP_ALGO_COUNT
@ INTERP_ALGO_NEAREST
static void cuda_tex_uninit(CudaFunctions *cu, CUDATex *t)
static int format_is_supported(enum AVPixelFormat fmt)
static void cudascale_describe_frame(AVFilterContext *ctx, AVFrame *frame)
#define CHECK_CU(x)
static int cudascale_query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static av_cold void set_format_info(AVFilterContext *ctx, enum AVPixelFormat in_format, enum AVPixelFormat out_format)
@ CUDA_SCALE_DIR_Y
@ CUDA_SCALE_DIR_X
@ CUDA_SCALE_DIR_NONE
static AVFrame * cudascale_get_video_buffer(AVFilterLink *inlink, int w, int h)
static int chroma_uses_luma_span(enum AVColorSpace colorspace)
#define CUDA_SCALE_MAX_PASSES
static void cudascale_plan_passes(CUDAScalePassPlan *plan, int in_width, int in_height, int out_width, int out_height, int in_sub_x, int in_sub_y, int out_sub_x, int out_sub_y, int has_chroma)
static int cudascale_prepare_filter_set(AVFilterContext *ctx, int in_width, int in_height, int normalize_crop)
static int cuda_tex_map_frame(AVFilterContext *ctx, const AVFrame *frame, const int depths[4], const int channels[4], CUDATex *tex, int use_linear, int as_integer)
#define OFFSET(x)
static void filter_uninit(CudaFunctions *cu, CUDAScaleFilter *filter)
static int cudascale_filter_frame(AVFilterLink *link, AVFrame *in)
static const AVFilterPad cudascale_outputs[]
static const AVFilterPad cudascale_inputs[]
@ FILTER_NB
@ FILTER_OUT
@ FILTER_TMP
static AVFilterFormats * cudascale_yuv_color_spaces(void)
#define SCALE_CUDA_PARAM_DEFAULT
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition video.c:44
AVFrame * ff_default_get_video_buffer(AVFilterLink *link, int w, int h)
Definition video.c:90