AppEncUtils.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2017-2020 NVIDIA Corporation. All rights reserved.
  3. *
  4. * Please refer to the NVIDIA end user license agreement (EULA) associated
  5. * with this source code for terms and conditions that govern your use of
  6. * this software. Any use, reproduction, disclosure, or distribution of
  7. * this software and related documentation outside the terms of the EULA
  8. * is strictly prohibited.
  9. *
  10. */
  11. #pragma once
  12. #include <iostream>
  13. #include "NvEncoder/NvEncoder.h"
  14. #include "../Utils/NvEncoderCLIOptions.h"
  15. inline void ShowHelpAndExit_AppEncD3D(const char *szBadOption = NULL, bool bOutputInVidMem = false)
  16. {
  17. bool bThrowError = false;
  18. std::ostringstream oss;
  19. if (szBadOption)
  20. {
  21. bThrowError = true;
  22. oss << "Error parsing \"" << szBadOption << "\"" << std::endl;
  23. }
  24. oss << "Options:" << std::endl
  25. << "-i Input file (must be in BGRA format) path" << std::endl
  26. << "-o Output file path" << std::endl
  27. << "-s Input resolution in this form: WxH" << std::endl
  28. << "-gpu Ordinal of GPU to use" << std::endl
  29. << "-nv12 (No value) Convert to NV12 before encoding. Don't use it with -444" << std::endl
  30. ;
  31. if (bOutputInVidMem)
  32. {
  33. oss << "-outputInVidMem Set this to 1 to enable output in Video Memory" << std::endl;
  34. }
  35. oss << NvEncoderInitParam().GetHelpMessage();
  36. if (bThrowError)
  37. {
  38. throw std::invalid_argument(oss.str());
  39. }
  40. else
  41. {
  42. std::cout << oss.str();
  43. exit(0);
  44. }
  45. }
  46. inline void ParseCommandLine_AppEncD3D(int argc, char *argv[], char *szInputFileName, int &nWidth, int &nHeight,
  47. char *szOutputFileName, NvEncoderInitParam &initParam, int &iGpu, bool &bForceNv12, int *outputInVidMem = NULL, bool bEnableOutputInVidMem = false)
  48. {
  49. std::ostringstream oss;
  50. int i;
  51. for (i = 1; i < argc; i++) {
  52. if (!_stricmp(argv[i], "-h")) {
  53. ShowHelpAndExit_AppEncD3D(NULL, bEnableOutputInVidMem);
  54. }
  55. if (!_stricmp(argv[i], "-i")) {
  56. if (++i == argc) {
  57. ShowHelpAndExit_AppEncD3D("-i", bEnableOutputInVidMem);
  58. }
  59. sprintf(szInputFileName, "%s", argv[i]);
  60. continue;
  61. }
  62. if (!_stricmp(argv[i], "-o")) {
  63. if (++i == argc) {
  64. ShowHelpAndExit_AppEncD3D("-o", bEnableOutputInVidMem);
  65. }
  66. sprintf(szOutputFileName, "%s", argv[i]);
  67. continue;
  68. }
  69. if (!_stricmp(argv[i], "-s")) {
  70. if (++i == argc || 2 != sscanf(argv[i], "%dx%d", &nWidth, &nHeight)) {
  71. ShowHelpAndExit_AppEncD3D("-s", bEnableOutputInVidMem);
  72. }
  73. continue;
  74. }
  75. if (!_stricmp(argv[i], "-gpu")) {
  76. if (++i == argc) {
  77. ShowHelpAndExit_AppEncD3D("-gpu", bEnableOutputInVidMem);
  78. }
  79. iGpu = atoi(argv[i]);
  80. continue;
  81. }
  82. if (!_stricmp(argv[i], "-nv12")) {
  83. bForceNv12 = true;
  84. continue;
  85. }
  86. if (!_stricmp(argv[i], "-outputInVidMem"))
  87. {
  88. if (++i == argc)
  89. {
  90. ShowHelpAndExit_AppEncD3D("-outputInVidMem", bEnableOutputInVidMem);
  91. }
  92. if (outputInVidMem != NULL)
  93. {
  94. *outputInVidMem = (atoi(argv[i]) != 0) ? 1 : 0;
  95. }
  96. continue;
  97. }
  98. // Regard as encoder parameter
  99. if (argv[i][0] != '-') {
  100. ShowHelpAndExit_AppEncD3D(argv[i], bEnableOutputInVidMem);
  101. }
  102. oss << argv[i] << " ";
  103. while (i + 1 < argc && argv[i + 1][0] != '-') {
  104. oss << argv[++i] << " ";
  105. }
  106. }
  107. initParam = NvEncoderInitParam(oss.str().c_str());
  108. }