FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_scale_npp.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /**
20  * @file
21  * scale video filter
22  */
23 
24 #include <nppi.h>
25 #include <stdio.h>
26 #include <string.h>
27 
28 #include "libavutil/avstring.h"
29 #include "libavutil/common.h"
30 #include "libavutil/eval.h"
31 #include "libavutil/hwcontext.h"
33 #include "libavutil/internal.h"
34 #include "libavutil/mathematics.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/pixdesc.h"
37 
38 #include "avfilter.h"
39 #include "formats.h"
40 #include "internal.h"
41 #include "video.h"
42 
43 static const enum AVPixelFormat supported_formats[] = {
47 };
48 
49 static const enum AVPixelFormat deinterleaved_formats[][2] = {
51 };
52 
53 static const char *const var_names[] = {
54  "PI",
55  "PHI",
56  "E",
57  "in_w", "iw",
58  "in_h", "ih",
59  "out_w", "ow",
60  "out_h", "oh",
61  "a", "dar",
62  "sar",
63  NULL
64 };
65 
66 enum var_name {
77 };
78 
79 enum ScaleStage {
84 };
85 
86 typedef struct NPPScaleStageContext {
90 
91  struct {
92  int width;
93  int height;
94  } planes_in[3], planes_out[3];
95 
99 
100 typedef struct NPPScaleContext {
101  const AVClass *class;
102 
106 
108 
109  /**
110  * New dimensions. Special values are:
111  * 0 = original width/height
112  * -1 = keep original aspect
113  */
114  int w, h;
115 
116  /**
117  * Output sw format. AV_PIX_FMT_NONE for no conversion.
118  */
120 
121  char *w_expr; ///< width expression string
122  char *h_expr; ///< height expression string
123  char *format_str;
124 
127 
129 {
130  NPPScaleContext *s = ctx->priv;
131  int i;
132 
133  if (!strcmp(s->format_str, "same")) {
134  s->format = AV_PIX_FMT_NONE;
135  } else {
137  if (s->format == AV_PIX_FMT_NONE) {
138  av_log(ctx, AV_LOG_ERROR, "Unrecognized pixel format: %s\n", s->format_str);
139  return AVERROR(EINVAL);
140  }
141  }
142 
143  for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
144  s->stages[i].frame = av_frame_alloc();
145  if (!s->stages[i].frame)
146  return AVERROR(ENOMEM);
147  }
148  s->tmp_frame = av_frame_alloc();
149  if (!s->tmp_frame)
150  return AVERROR(ENOMEM);
151 
152  return 0;
153 }
154 
156 {
157  NPPScaleContext *s = ctx->priv;
158  int i;
159 
160  for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
161  av_frame_free(&s->stages[i].frame);
163  }
165 }
166 
168 {
169  static const enum AVPixelFormat pixel_formats[] = {
171  };
172  AVFilterFormats *pix_fmts = ff_make_format_list(pixel_formats);
173 
174  ff_set_common_formats(ctx, pix_fmts);
175 
176  return 0;
177 }
178 
179 static int init_stage(NPPScaleStageContext *stage, AVBufferRef *device_ctx)
180 {
181  AVBufferRef *out_ref = NULL;
182  AVHWFramesContext *out_ctx;
183  int in_sw, in_sh, out_sw, out_sh;
184  int ret, i;
185 
186  av_pix_fmt_get_chroma_sub_sample(stage->in_fmt, &in_sw, &in_sh);
187  av_pix_fmt_get_chroma_sub_sample(stage->out_fmt, &out_sw, &out_sh);
188  if (!stage->planes_out[0].width) {
189  stage->planes_out[0].width = stage->planes_in[0].width;
190  stage->planes_out[0].height = stage->planes_in[0].height;
191  }
192 
193  for (i = 1; i < FF_ARRAY_ELEMS(stage->planes_in); i++) {
194  stage->planes_in[i].width = stage->planes_in[0].width >> in_sw;
195  stage->planes_in[i].height = stage->planes_in[0].height >> in_sh;
196  stage->planes_out[i].width = stage->planes_out[0].width >> out_sw;
197  stage->planes_out[i].height = stage->planes_out[0].height >> out_sh;
198  }
199 
200  out_ref = av_hwframe_ctx_alloc(device_ctx);
201  if (!out_ref)
202  return AVERROR(ENOMEM);
203  out_ctx = (AVHWFramesContext*)out_ref->data;
204 
205  out_ctx->format = AV_PIX_FMT_CUDA;
206  out_ctx->sw_format = stage->out_fmt;
207  out_ctx->width = FFALIGN(stage->planes_out[0].width, 32);
208  out_ctx->height = FFALIGN(stage->planes_out[0].height, 32);
209 
210  ret = av_hwframe_ctx_init(out_ref);
211  if (ret < 0)
212  goto fail;
213 
214  av_frame_unref(stage->frame);
215  ret = av_hwframe_get_buffer(out_ref, stage->frame, 0);
216  if (ret < 0)
217  goto fail;
218 
219  stage->frame->width = stage->planes_out[0].width;
220  stage->frame->height = stage->planes_out[0].height;
221 
222  av_buffer_unref(&stage->frames_ctx);
223  stage->frames_ctx = out_ref;
224 
225  return 0;
226 fail:
227  av_buffer_unref(&out_ref);
228  return ret;
229 }
230 
232 {
233  int i;
234 
235  for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
236  if (supported_formats[i] == fmt)
237  return 1;
238  return 0;
239 }
240 
242 {
244  int i, planes;
245 
246  planes = av_pix_fmt_count_planes(fmt);
247  if (planes == desc->nb_components)
248  return fmt;
249  for (i = 0; i < FF_ARRAY_ELEMS(deinterleaved_formats); i++)
250  if (deinterleaved_formats[i][0] == fmt)
251  return deinterleaved_formats[i][1];
252  return AV_PIX_FMT_NONE;
253 }
254 
255 static int init_processing_chain(AVFilterContext *ctx, int in_width, int in_height,
256  int out_width, int out_height)
257 {
258  NPPScaleContext *s = ctx->priv;
259 
260  AVHWFramesContext *in_frames_ctx;
261 
262  enum AVPixelFormat in_format;
263  enum AVPixelFormat out_format;
264  enum AVPixelFormat in_deinterleaved_format;
265  enum AVPixelFormat out_deinterleaved_format;
266 
267  int i, ret, last_stage = -1;
268 
269  /* check that we have a hw context */
270  if (!ctx->inputs[0]->hw_frames_ctx) {
271  av_log(ctx, AV_LOG_ERROR, "No hw context provided on input\n");
272  return AVERROR(EINVAL);
273  }
274  in_frames_ctx = (AVHWFramesContext*)ctx->inputs[0]->hw_frames_ctx->data;
275  in_format = in_frames_ctx->sw_format;
276  out_format = (s->format == AV_PIX_FMT_NONE) ? in_format : s->format;
277 
278  if (!format_is_supported(in_format)) {
279  av_log(ctx, AV_LOG_ERROR, "Unsupported input format: %s\n",
280  av_get_pix_fmt_name(in_format));
281  return AVERROR(ENOSYS);
282  }
283  if (!format_is_supported(out_format)) {
284  av_log(ctx, AV_LOG_ERROR, "Unsupported output format: %s\n",
285  av_get_pix_fmt_name(out_format));
286  return AVERROR(ENOSYS);
287  }
288 
289  in_deinterleaved_format = get_deinterleaved_format(in_format);
290  out_deinterleaved_format = get_deinterleaved_format(out_format);
291  if (in_deinterleaved_format == AV_PIX_FMT_NONE ||
292  out_deinterleaved_format == AV_PIX_FMT_NONE)
293  return AVERROR_BUG;
294 
295  /* figure out which stages need to be done */
296  if (in_width != out_width || in_height != out_height ||
297  in_deinterleaved_format != out_deinterleaved_format)
299 
300  if (!s->stages[STAGE_RESIZE].stage_needed && in_format == out_format)
301  s->passthrough = 1;
302 
303  if (!s->passthrough) {
304  if (in_format != in_deinterleaved_format)
306  if (out_format != out_deinterleaved_format)
308  }
309 
310  s->stages[STAGE_DEINTERLEAVE].in_fmt = in_format;
311  s->stages[STAGE_DEINTERLEAVE].out_fmt = in_deinterleaved_format;
312  s->stages[STAGE_DEINTERLEAVE].planes_in[0].width = in_width;
313  s->stages[STAGE_DEINTERLEAVE].planes_in[0].height = in_height;
314 
315  s->stages[STAGE_RESIZE].in_fmt = in_deinterleaved_format;
316  s->stages[STAGE_RESIZE].out_fmt = out_deinterleaved_format;
317  s->stages[STAGE_RESIZE].planes_in[0].width = in_width;
318  s->stages[STAGE_RESIZE].planes_in[0].height = in_height;
319  s->stages[STAGE_RESIZE].planes_out[0].width = out_width;
320  s->stages[STAGE_RESIZE].planes_out[0].height = out_height;
321 
322  s->stages[STAGE_INTERLEAVE].in_fmt = out_deinterleaved_format;
323  s->stages[STAGE_INTERLEAVE].out_fmt = out_format;
324  s->stages[STAGE_INTERLEAVE].planes_in[0].width = out_width;
325  s->stages[STAGE_INTERLEAVE].planes_in[0].height = out_height;
326 
327  /* init the hardware contexts */
328  for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
329  if (!s->stages[i].stage_needed)
330  continue;
331 
332  ret = init_stage(&s->stages[i], in_frames_ctx->device_ref);
333  if (ret < 0)
334  return ret;
335 
336  last_stage = i;
337  }
338 
339  if (last_stage < 0)
340  return AVERROR_BUG;
341  ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(s->stages[last_stage].frames_ctx);
342  if (!ctx->outputs[0]->hw_frames_ctx)
343  return AVERROR(ENOMEM);
344 
345  return 0;
346 }
347 
349 {
350  AVFilterContext *ctx = outlink->src;
351  AVFilterLink *inlink = outlink->src->inputs[0];
352  NPPScaleContext *s = ctx->priv;
353  int64_t w, h;
354  double var_values[VARS_NB], res;
355  char *expr;
356  int ret;
357 
358  var_values[VAR_PI] = M_PI;
359  var_values[VAR_PHI] = M_PHI;
360  var_values[VAR_E] = M_E;
361  var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
362  var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
363  var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
364  var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
365  var_values[VAR_A] = (double) inlink->w / inlink->h;
366  var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
367  (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
368  var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
369 
370  /* evaluate width and height */
371  av_expr_parse_and_eval(&res, (expr = s->w_expr),
372  var_names, var_values,
373  NULL, NULL, NULL, NULL, NULL, 0, ctx);
374  s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
375  if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr),
376  var_names, var_values,
377  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
378  goto fail;
379  s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
380  /* evaluate again the width, as it may depend on the output height */
381  if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
382  var_names, var_values,
383  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
384  goto fail;
385  s->w = res;
386 
387  w = s->w;
388  h = s->h;
389 
390  /* sanity check params */
391  if (w < -1 || h < -1) {
392  av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
393  return AVERROR(EINVAL);
394  }
395  if (w == -1 && h == -1)
396  s->w = s->h = 0;
397 
398  if (!(w = s->w))
399  w = inlink->w;
400  if (!(h = s->h))
401  h = inlink->h;
402  if (w == -1)
403  w = av_rescale(h, inlink->w, inlink->h);
404  if (h == -1)
405  h = av_rescale(w, inlink->h, inlink->w);
406 
407  if (w > INT_MAX || h > INT_MAX ||
408  (h * inlink->w) > INT_MAX ||
409  (w * inlink->h) > INT_MAX)
410  av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
411 
412  outlink->w = w;
413  outlink->h = h;
414 
415  ret = init_processing_chain(ctx, inlink->w, inlink->h, w, h);
416  if (ret < 0)
417  return ret;
418 
419  av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d\n",
420  inlink->w, inlink->h, outlink->w, outlink->h);
421 
422  if (inlink->sample_aspect_ratio.num)
423  outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
424  outlink->w*inlink->h},
425  inlink->sample_aspect_ratio);
426  else
427  outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
428 
429  return 0;
430 
431 fail:
433  "Error when evaluating the expression '%s'\n", expr);
434  return ret;
435 }
436 
438  AVFrame *out, AVFrame *in)
439 {
440  AVHWFramesContext *in_frames_ctx = (AVHWFramesContext*)in->hw_frames_ctx->data;
441  NppStatus err;
442 
443  switch (in_frames_ctx->sw_format) {
444  case AV_PIX_FMT_NV12:
445  err = nppiYCbCr420_8u_P2P3R(in->data[0], in->linesize[0],
446  in->data[1], in->linesize[1],
447  out->data, out->linesize,
448  (NppiSize){ in->width, in->height });
449  break;
450  default:
451  return AVERROR_BUG;
452  }
453  if (err != NPP_SUCCESS) {
454  av_log(ctx, AV_LOG_ERROR, "NPP deinterleave error: %d\n", err);
455  return AVERROR_UNKNOWN;
456  }
457 
458  return 0;
459 }
460 
462  AVFrame *out, AVFrame *in)
463 {
464  NPPScaleContext *s = ctx->priv;
465  NppStatus err;
466  int i;
467 
468  for (i = 0; i < FF_ARRAY_ELEMS(in->data) && in->data[i]; i++) {
469  int iw = stage->planes_in[i].width;
470  int ih = stage->planes_in[i].height;
471  int ow = stage->planes_out[i].width;
472  int oh = stage->planes_out[i].height;
473 
474  err = nppiResizeSqrPixel_8u_C1R(in->data[i], (NppiSize){ iw, ih },
475  in->linesize[i], (NppiRect){ 0, 0, iw, ih },
476  out->data[i], out->linesize[i],
477  (NppiRect){ 0, 0, ow, oh },
478  (double)ow / iw, (double)oh / ih,
479  0.0, 0.0, s->interp_algo);
480  if (err != NPP_SUCCESS) {
481  av_log(ctx, AV_LOG_ERROR, "NPP resize error: %d\n", err);
482  return AVERROR_UNKNOWN;
483  }
484  }
485 
486  return 0;
487 }
488 
490  AVFrame *out, AVFrame *in)
491 {
492  AVHWFramesContext *out_frames_ctx = (AVHWFramesContext*)out->hw_frames_ctx->data;
493  NppStatus err;
494 
495  switch (out_frames_ctx->sw_format) {
496  case AV_PIX_FMT_NV12:
497  err = nppiYCbCr420_8u_P3P2R((const uint8_t**)in->data,
498  in->linesize,
499  out->data[0], out->linesize[0],
500  out->data[1], out->linesize[1],
501  (NppiSize){ in->width, in->height });
502  break;
503  default:
504  return AVERROR_BUG;
505  }
506  if (err != NPP_SUCCESS) {
507  av_log(ctx, AV_LOG_ERROR, "NPP deinterleave error: %d\n", err);
508  return AVERROR_UNKNOWN;
509  }
510 
511  return 0;
512 }
513 
515  AVFrame *out, AVFrame *in) = {
519 };
520 
522 {
523  NPPScaleContext *s = ctx->priv;
524  AVFrame *src = in;
525  int i, ret, last_stage = -1;
526 
527  for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
528  if (!s->stages[i].stage_needed)
529  continue;
530 
531  ret = nppscale_process[i](ctx, &s->stages[i], s->stages[i].frame, src);
532  if (ret < 0)
533  return ret;
534 
535  src = s->stages[i].frame;
536  last_stage = i;
537  }
538 
539  if (last_stage < 0)
540  return AVERROR_BUG;
541  ret = av_hwframe_get_buffer(src->hw_frames_ctx, s->tmp_frame, 0);
542  if (ret < 0)
543  return ret;
544 
545  av_frame_move_ref(out, src);
546  av_frame_move_ref(src, s->tmp_frame);
547 
548  ret = av_frame_copy_props(out, in);
549  if (ret < 0)
550  return ret;
551 
552  return 0;
553 }
554 
556 {
557  AVFilterContext *ctx = link->dst;
558  NPPScaleContext *s = ctx->priv;
559  AVFilterLink *outlink = ctx->outputs[0];
560  AVHWFramesContext *frames_ctx = (AVHWFramesContext*)outlink->hw_frames_ctx->data;
561  AVCUDADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
562 
563  AVFrame *out = NULL;
564  CUresult err;
566  int ret = 0;
567 
568  if (s->passthrough)
569  return ff_filter_frame(outlink, in);
570 
571  out = av_frame_alloc();
572  if (!out) {
573  ret = AVERROR(ENOMEM);
574  goto fail;
575  }
576 
577  av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
578  (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
579  (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
580  INT_MAX);
581 
582  err = cuCtxPushCurrent(device_hwctx->cuda_ctx);
583  if (err != CUDA_SUCCESS) {
584  ret = AVERROR_UNKNOWN;
585  goto fail;
586  }
587 
588  ret = nppscale_scale(ctx, out, in);
589 
590  cuCtxPopCurrent(&dummy);
591  if (ret < 0)
592  goto fail;
593 
594  av_frame_free(&in);
595  return ff_filter_frame(outlink, out);
596 fail:
597  av_frame_free(&in);
598  av_frame_free(&out);
599  return ret;
600 }
601 
602 #define OFFSET(x) offsetof(NPPScaleContext, x)
603 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
604 static const AVOption options[] = {
605  { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
606  { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
607  { "format", "Output pixel format", OFFSET(format_str), AV_OPT_TYPE_STRING, { .str = "same" }, .flags = FLAGS },
608 
609  { "interp_algo", "Interpolation algorithm used for resizing", OFFSET(interp_algo), AV_OPT_TYPE_INT, { .i64 = NPPI_INTER_CUBIC }, 0, INT_MAX, FLAGS, "interp_algo" },
610  { "nn", "nearest neighbour", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_NN }, 0, 0, FLAGS, "interp_algo" },
611  { "linear", "linear", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_LINEAR }, 0, 0, FLAGS, "interp_algo" },
612  { "cubic", "cubic", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_CUBIC }, 0, 0, FLAGS, "interp_algo" },
613  { "cubic2p_bspline", "2-parameter cubic (B=1, C=0)", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_CUBIC2P_BSPLINE }, 0, 0, FLAGS, "interp_algo" },
614  { "cubic2p_catmullrom", "2-parameter cubic (B=0, C=1/2)", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_CUBIC2P_CATMULLROM }, 0, 0, FLAGS, "interp_algo" },
615  { "cubic2p_b05c03", "2-parameter cubic (B=1/2, C=3/10)", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_CUBIC2P_B05C03 }, 0, 0, FLAGS, "interp_algo" },
616  { "super", "supersampling", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_SUPER }, 0, 0, FLAGS, "interp_algo" },
617  { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_LANCZOS }, 0, 0, FLAGS, "interp_algo" },
618  { NULL },
619 };
620 
621 static const AVClass nppscale_class = {
622  .class_name = "nppscale",
623  .item_name = av_default_item_name,
624  .option = options,
625  .version = LIBAVUTIL_VERSION_INT,
626 };
627 
628 static const AVFilterPad nppscale_inputs[] = {
629  {
630  .name = "default",
631  .type = AVMEDIA_TYPE_VIDEO,
632  .filter_frame = nppscale_filter_frame,
633  },
634  { NULL }
635 };
636 
637 static const AVFilterPad nppscale_outputs[] = {
638  {
639  .name = "default",
640  .type = AVMEDIA_TYPE_VIDEO,
641  .config_props = nppscale_config_props,
642  },
643  { NULL }
644 };
645 
647  .name = "scale_npp",
648  .description = NULL_IF_CONFIG_SMALL("NVIDIA Performance Primitives video "
649  "scaling and format conversion"),
650 
651  .init = nppscale_init,
652  .uninit = nppscale_uninit,
653  .query_formats = nppscale_query_formats,
654 
655  .priv_size = sizeof(NPPScaleContext),
656  .priv_class = &nppscale_class,
657 
658  .inputs = nppscale_inputs,
659  .outputs = nppscale_outputs,
660 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:124
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2222
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
ScaleStage
Definition: vf_scale_npp.c:79
AVOption.
Definition: opt.h:245
const char * fmt
Definition: avisynth_c.h:632
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2262
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
Main libavfilter public API header.
const char * desc
Definition: nvenc.c:89
int num
numerator
Definition: rational.h:44
AVBufferRef * frames_ctx
Definition: vf_scale_npp.c:96
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:221
static int nppscale_config_props(AVFilterLink *outlink)
Definition: vf_scale_npp.c:348
enum AVPixelFormat out_fmt
Definition: vf_scale_npp.c:89
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:201
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:508
static enum AVPixelFormat get_deinterleaved_format(enum AVPixelFormat fmt)
Definition: vf_scale_npp.c:241
static int nppscale_resize(AVFilterContext *ctx, NPPScaleStageContext *stage, AVFrame *out, AVFrame *in)
Definition: vf_scale_npp.c:461
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
enum AVPixelFormat format
Output sw format.
Definition: vf_scale_npp.c:119
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame...
Definition: frame.h:434
const char * name
Pad name.
Definition: internal.h:59
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:313
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1180
char * w_expr
width expression string
Definition: vf_scale_npp.c:121
static const AVFilterPad nppscale_inputs[]
Definition: vf_scale_npp.c:628
uint8_t
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:140
enum AVPixelFormat in_fmt
Definition: vf_scale_npp.c:88
AVOptions.
static void nppscale_uninit(AVFilterContext *ctx)
Definition: vf_scale_npp.c:155
struct NPPScaleStageContext::@172 planes_in[3]
int w
New dimensions.
Definition: vf_scale_npp.c:114
AVFilter ff_vf_scale_npp
Definition: vf_scale_npp.c:646
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:84
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
static const AVOption options[]
Definition: vf_scale_npp.c:604
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
An API-specific header for AV_HWDEVICE_TYPE_CUDA.
A filter pad used for either input or output.
Definition: internal.h:53
struct NPPScaleStageContext::@172 planes_out[3]
static int nppscale_init(AVFilterContext *ctx)
Definition: vf_scale_npp.c:128
int av_expr_parse_and_eval(double *d, const char *s, const char *const *const_names, const double *const_values, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), void *opaque, int log_offset, void *log_ctx)
Parse and evaluate an expression.
Definition: eval.c:723
int width
width and height of the video frame
Definition: frame.h:236
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:568
static const char *const var_names[]
Definition: vf_scale_npp.c:53
static int nppscale_deinterleave(AVFilterContext *ctx, NPPScaleStageContext *stage, AVFrame *out, AVFrame *in)
Definition: vf_scale_npp.c:437
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:153
#define M_E
Definition: mathematics.h:31
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:2250
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
void * priv
private data for use by the filter
Definition: avfilter.h:320
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:90
static int nppscale_scale(AVFilterContext *ctx, AVFrame *out, AVFrame *in)
Definition: vf_scale_npp.c:521
static int init_stage(NPPScaleStageContext *stage, AVBufferRef *device_ctx)
Definition: vf_scale_npp.c:179
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:260
static int nppscale_interleave(AVFilterContext *ctx, NPPScaleStageContext *stage, AVFrame *out, AVFrame *in)
Definition: vf_scale_npp.c:489
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
Definition: hwcontext.c:387
#define fail()
Definition: checkasm.h:81
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
var_name
Definition: aeval.c:46
common internal API header
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:83
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Definition: hwcontext.h:141
static const AVClass nppscale_class
Definition: vf_scale_npp.c:621
AVFormatContext * ctx
Definition: movenc.c:48
static enum AVPixelFormat supported_formats[]
Definition: vf_scale_npp.c:43
CUresult
Definition: nvenc.h:41
int dummy
Definition: motion.c:64
#define OFFSET(x)
Definition: vf_scale_npp.c:602
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:386
static int format_is_supported(enum AVPixelFormat fmt)
Definition: vf_scale_npp.c:231
#define src
Definition: vp9dsp.c:530
HW acceleration through CUDA.
Definition: pixfmt.h:248
#define FF_ARRAY_ELEMS(a)
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:376
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
uint8_t * data
The data buffer.
Definition: buffer.h:89
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:263
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
This struct is allocated as AVHWDeviceContext.hwctx.
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:142
rational number numerator/denominator
Definition: rational.h:43
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:116
#define M_PHI
Definition: mathematics.h:43
AVFrame * tmp_frame
Definition: vf_scale_npp.c:104
static int init_processing_chain(AVFilterContext *ctx, int in_width, int in_height, int out_width, int out_height)
Definition: vf_scale_npp.c:255
const char * name
Filter name.
Definition: avfilter.h:146
static int(*const nppscale_process[])(AVFilterContext *ctx, NPPScaleStageContext *stage, AVFrame *out, AVFrame *in)
Definition: vf_scale_npp.c:514
static int nppscale_query_formats(AVFilterContext *ctx)
Definition: vf_scale_npp.c:167
#define FLAGS
Definition: vf_scale_npp.c:603
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:317
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:484
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
Definition: hwcontext.h:133
static const AVFilterPad nppscale_outputs[]
Definition: vf_scale_npp.c:637
A reference to a data buffer.
Definition: buffer.h:81
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
common internal and external API header
if(ret< 0)
Definition: vf_mcdeint.c:282
NPPScaleStageContext stages[STAGE_NB]
Definition: vf_scale_npp.c:103
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition: hwcontext.c:174
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
int den
denominator
Definition: rational.h:45
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
static int nppscale_filter_frame(AVFilterLink *link, AVFrame *in)
Definition: vf_scale_npp.c:555
#define NAN
Definition: math.h:28
A list of supported formats for one end of a filter link.
Definition: formats.h:64
static enum AVPixelFormat deinterleaved_formats[][2]
Definition: vf_scale_npp.c:49
An instance of a filter.
Definition: avfilter.h:305
int height
Definition: frame.h:236
FILE * out
Definition: movenc.c:54
#define M_PI
Definition: mathematics.h:46
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:2150
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:2138
void * CUcontext
Definition: nvenc.h:45
internal API functions
char * h_expr
height expression string
Definition: vf_scale_npp.c:122
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:214
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:580
simple arithmetic expression evaluator