vf_sr_amf.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * @file
  20. * Super resolution video filter with AMF hardware acceleration
  21. */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/imgutils.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "libavutil/time.h"
  29. #include "libavutil/hwcontext.h"
  30. #include "libavutil/hwcontext_amf.h"
  31. #include "libavutil/hwcontext_amf_internal.h"
  32. #include "AMF/components/HQScaler.h"
  33. #include "AMF/components/ColorSpace.h"
  34. #include "vf_amf_common.h"
  35. #include "avfilter.h"
  36. #include "avfilter_internal.h"
  37. #include "formats.h"
  38. #include "video.h"
  39. #if CONFIG_DXVA2
  40. #include <d3d9.h>
  41. #endif
  42. #if CONFIG_D3D11VA
  43. #include <d3d11.h>
  44. #endif
  45. static int amf_filter_query_formats(AVFilterContext *avctx)
  46. {
  47. const enum AVPixelFormat *output_pix_fmts;
  48. static const enum AVPixelFormat input_pix_fmts[] = {
  49. AV_PIX_FMT_NV12,
  50. AV_PIX_FMT_P010,
  51. AV_PIX_FMT_BGRA,
  52. AV_PIX_FMT_RGBA,
  53. AV_PIX_FMT_AMF_SURFACE,
  54. AV_PIX_FMT_RGBAF16,
  55. AV_PIX_FMT_NONE,
  56. };
  57. static const enum AVPixelFormat output_pix_fmts_default[] = {
  58. AV_PIX_FMT_NV12,
  59. AV_PIX_FMT_P010,
  60. AV_PIX_FMT_BGRA,
  61. AV_PIX_FMT_RGBA,
  62. AV_PIX_FMT_AMF_SURFACE,
  63. AV_PIX_FMT_D3D11,
  64. AV_PIX_FMT_DXVA2_VLD,
  65. AV_PIX_FMT_RGBAF16,
  66. AV_PIX_FMT_NONE,
  67. };
  68. output_pix_fmts = output_pix_fmts_default;
  69. return amf_setup_input_output_formats(avctx, input_pix_fmts, output_pix_fmts);
  70. }
  71. static int amf_filter_config_output(AVFilterLink *outlink)
  72. {
  73. AVFilterContext *avctx = outlink->src;
  74. AVFilterLink *inlink = avctx->inputs[0];
  75. AMFFilterContext *ctx = avctx->priv;
  76. AMFSize out_size;
  77. int err;
  78. AMF_RESULT res;
  79. enum AVPixelFormat in_format;
  80. err = amf_init_filter_config(outlink, &in_format);
  81. if (err < 0)
  82. return err;
  83. // HQ scaler should be used for upscaling only
  84. if (inlink->w > outlink->w || inlink->h > outlink->h) {
  85. av_log(avctx, AV_LOG_ERROR, "AMF HQ scaler should be used for upscaling only.\n");
  86. return AVERROR_UNKNOWN;
  87. }
  88. // FIXME: add checks whether we have HW context
  89. res = ctx->amf_device_ctx->factory->pVtbl->CreateComponent(ctx->amf_device_ctx->factory, ctx->amf_device_ctx->context, AMFHQScaler, &ctx->component);
  90. AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_FILTER_NOT_FOUND, "CreateComponent(%ls) failed with error %d\n", AMFHQScaler, res);
  91. out_size.width = outlink->w;
  92. out_size.height = outlink->h;
  93. AMF_ASSIGN_PROPERTY_SIZE(res, ctx->component, AMF_HQ_SCALER_OUTPUT_SIZE, out_size);
  94. AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN, "AMFHQScaler-SetProperty() failed with error %d\n", res);
  95. if (ctx->algorithm != -1) {
  96. AMF_ASSIGN_PROPERTY_INT64(res, ctx->component, AMF_HQ_SCALER_ALGORITHM, ctx->algorithm);
  97. }
  98. if (ctx->sharpness != -1) {
  99. AMF_ASSIGN_PROPERTY_DOUBLE(res, ctx->component, AMF_HQ_SCALER_SHARPNESS, ctx->sharpness);
  100. }
  101. AMF_ASSIGN_PROPERTY_BOOL(res, ctx->component, AMF_HQ_SCALER_FILL, ctx->fill);
  102. AMF_ASSIGN_PROPERTY_BOOL(res, ctx->component, AMF_HQ_SCALER_KEEP_ASPECT_RATIO, ctx->keep_ratio);
  103. // Setup default options to skip color conversion
  104. ctx->color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_UNKNOWN;
  105. ctx->color_range = AMF_COLOR_RANGE_UNDEFINED;
  106. ctx->primaries = AMF_COLOR_PRIMARIES_UNDEFINED;
  107. ctx->trc = AMF_COLOR_TRANSFER_CHARACTERISTIC_UNDEFINED;
  108. res = ctx->component->pVtbl->Init(ctx->component, av_av_to_amf_format(in_format), inlink->w, inlink->h);
  109. AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN, "AMFHQScaler-Init() failed with error %d\n", res);
  110. return 0;
  111. }
  112. #define OFFSET(x) offsetof(AMFFilterContext, x)
  113. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  114. static const AVOption sr_amf_options[] = {
  115. { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
  116. { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
  117. { "format", "Output pixel format", OFFSET(format_str), AV_OPT_TYPE_STRING, { .str = "same" }, .flags = FLAGS },
  118. { "sharpness", "Sharpness", OFFSET(sharpness), AV_OPT_TYPE_FLOAT, { .dbl = -1 }, -1, 2., FLAGS, "sharpness" },
  119. { "keep-ratio", "Keep aspect ratio", OFFSET(keep_ratio), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS, "keep_ration" },
  120. { "fill", "Fill", OFFSET(fill), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS, "fill" },
  121. { "algorithm", "Scaling algorithm", OFFSET(algorithm), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, AMF_HQ_SCALER_ALGORITHM_VIDEOSR1_1, FLAGS, "algorithm" },
  122. { "bilinear", "Bilinear", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_HQ_SCALER_ALGORITHM_BILINEAR }, 0, 0, FLAGS, "algorithm" },
  123. { "bicubic", "Bicubic", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_HQ_SCALER_ALGORITHM_BICUBIC }, 0, 0, FLAGS, "algorithm" },
  124. { "sr1-0", "Video SR1.0", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_HQ_SCALER_ALGORITHM_VIDEOSR1_0 }, 0, 0, FLAGS, "algorithm" },
  125. { "point", "Point", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_HQ_SCALER_ALGORITHM_POINT }, 0, 0, FLAGS, "algorithm" },
  126. { "sr1-1", "Video SR1.1", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_HQ_SCALER_ALGORITHM_VIDEOSR1_1 }, 0, 0, FLAGS, "algorithm" },
  127. { NULL },
  128. };
  129. AVFILTER_DEFINE_CLASS(sr_amf);
  130. static const AVFilterPad amf_filter_inputs[] = {
  131. {
  132. .name = "default",
  133. .type = AVMEDIA_TYPE_VIDEO,
  134. .filter_frame = amf_filter_filter_frame,
  135. }
  136. };
  137. static const AVFilterPad amf_filter_outputs[] = {
  138. {
  139. .name = "default",
  140. .type = AVMEDIA_TYPE_VIDEO,
  141. .config_props = amf_filter_config_output,
  142. }
  143. };
  144. FFFilter ff_vf_sr_amf = {
  145. .p.name = "sr_amf",
  146. .p.description = NULL_IF_CONFIG_SMALL("AMF HQ video upscaling"),
  147. .p.priv_class = &sr_amf_class,
  148. .p.flags = AVFILTER_FLAG_HWDEVICE,
  149. .priv_size = sizeof(AMFFilterContext),
  150. .init = amf_filter_init,
  151. .uninit = amf_filter_uninit,
  152. FILTER_INPUTS(amf_filter_inputs),
  153. FILTER_OUTPUTS(amf_filter_outputs),
  154. FILTER_QUERY_FUNC(&amf_filter_query_formats),
  155. FILTER_SINGLE_PIXFMT(AV_PIX_FMT_AMF_SURFACE),
  156. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  157. };