video_encode.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2016-2023, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of NVIDIA CORPORATION nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  20. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  24. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <fstream>
  29. #include "NvVideoEncoder.h"
  30. #include <sstream>
  31. #include <stdint.h>
  32. #include <semaphore.h>
  33. #include "NvBufSurface.h"
  34. #define CRC32_POLYNOMIAL 0xEDB88320L
  35. #define MAX_OUT_BUFFERS 32
  36. typedef struct RPS_List
  37. {
  38. uint32_t nFrameId;
  39. bool bLTRefFrame;
  40. } RPS_List;
  41. typedef struct RPS_param
  42. {
  43. sem_t sema;
  44. uint32_t m_numTemperalLayers;
  45. uint32_t nActiveRefFrames;
  46. RPS_List rps_list[V4L2_MAX_REF_FRAMES];
  47. } RPS_param;
  48. typedef struct CrcRec
  49. {
  50. unsigned int CRCTable[256];
  51. unsigned int CrcValue;
  52. }Crc;
  53. typedef struct
  54. {
  55. NvVideoEncoder *enc;
  56. uint32_t encoder_pixfmt;
  57. uint32_t raw_pixfmt;
  58. char *in_file_path;
  59. std::ifstream *in_file;
  60. uint32_t width;
  61. uint32_t height;
  62. char *out_file_path;
  63. std::ofstream *out_file;
  64. char *ROI_Param_file_path;
  65. char *Recon_Ref_file_path;
  66. char *RPS_Param_file_path;
  67. char *hints_Param_file_path;
  68. char *GDR_Param_file_path;
  69. char *GDR_out_file_path;
  70. std::ifstream *roi_Param_file;
  71. std::ifstream *recon_Ref_file;
  72. std::ifstream *rps_Param_file;
  73. std::ifstream *hints_Param_file;
  74. std::ifstream *gdr_Param_file;
  75. std::ofstream *gdr_out_file;
  76. uint32_t bitrate;
  77. uint32_t peak_bitrate;
  78. uint32_t profile;
  79. enum v4l2_mpeg_video_bitrate_mode ratecontrol;
  80. uint32_t iframe_interval;
  81. uint32_t idr_interval;
  82. uint32_t level;
  83. uint32_t fps_n;
  84. uint32_t fps_d;
  85. uint32_t gdr_start_frame_number; /* Frame number where GDR has to be started */
  86. uint32_t gdr_num_frames; /* Number of frames where GDR to be applied */
  87. uint32_t gdr_out_frame_number; /* Frames number from where encoded buffers are to be dumped */
  88. enum v4l2_enc_temporal_tradeoff_level_type temporal_tradeoff_level;
  89. enum v4l2_enc_hw_preset_type hw_preset_type;
  90. v4l2_enc_slice_length_type slice_length_type;
  91. uint32_t slice_length;
  92. uint32_t virtual_buffer_size;
  93. uint32_t num_reference_frames;
  94. uint32_t slice_intrarefresh_interval;
  95. uint32_t num_b_frames;
  96. uint32_t nMinQpI; /* Minimum QP value to use for index frames */
  97. uint32_t nMaxQpI; /* Maximum QP value to use for index frames */
  98. uint32_t nMinQpP; /* Minimum QP value to use for P frames */
  99. uint32_t nMaxQpP; /* Maximum QP value to use for P frames */
  100. uint32_t nMinQpB; /* Minimum QP value to use for B frames */
  101. uint32_t nMaxQpB; /* Maximum QP value to use for B frames */
  102. uint32_t sMaxQp; /* Session Maximum QP value */
  103. uint32_t sar_width;
  104. uint32_t sar_height;
  105. uint32_t IinitQP;
  106. uint32_t PinitQP;
  107. uint32_t BinitQP;
  108. uint32_t log2_num_av1rows;
  109. uint32_t log2_num_av1cols;
  110. uint8_t bit_depth;
  111. uint8_t enable_av1ssimrdo;
  112. uint8_t disable_av1cdfupdate;
  113. uint8_t chroma_format_idc;
  114. int output_plane_fd[32];
  115. int capture_plane_fd[32];
  116. bool insert_sps_pps_at_idr;
  117. bool enable_slice_level_encode;
  118. bool disable_cabac;
  119. bool insert_vui;
  120. bool enable_extended_colorformat;
  121. bool insert_aud;
  122. bool alliframes;
  123. bool is_semiplanar;
  124. bool enable_initQP;
  125. bool enable_ratecontrol;
  126. bool enable_av1tile;
  127. enum v4l2_memory output_memory_type;
  128. enum v4l2_memory capture_memory_type;
  129. enum v4l2_colorspace cs;
  130. bool report_metadata;
  131. bool input_metadata;
  132. bool copy_timestamp;
  133. uint32_t start_ts;
  134. bool dump_mv;
  135. bool enableGDR;
  136. bool bGapsInFrameNumAllowed;
  137. bool bnoIframe;
  138. uint32_t nH264FrameNumBits;
  139. uint32_t nH265PocLsbBits;
  140. bool externalRCHints;
  141. bool enableROI;
  142. bool b_use_enc_cmd;
  143. bool enableLossless;
  144. bool got_eos;
  145. bool externalRPS;
  146. bool RPS_threeLayerSvc;
  147. RPS_param rps_par;
  148. bool use_gold_crc;
  149. char gold_crc[20];
  150. Crc *pBitStreamCrc;
  151. bool bReconCrc;
  152. uint32_t rl; /* Reconstructed surface Left cordinate */
  153. uint32_t rt; /* Reconstructed surface Top cordinate */
  154. uint32_t rw; /* Reconstructed surface width */
  155. uint32_t rh; /* Reconstructed surface height */
  156. uint64_t timestamp;
  157. uint64_t timestampincr;
  158. bool stats;
  159. std::stringstream *runtime_params_str;
  160. uint32_t next_param_change_frame;
  161. bool got_error;
  162. int stress_test;
  163. uint32_t endofstream_capture;
  164. uint32_t endofstream_output;
  165. uint32_t input_frames_queued_count;
  166. uint32_t startf;
  167. uint32_t endf;
  168. uint32_t num_output_buffers;
  169. int32_t num_frames_to_encode;
  170. uint32_t poc_type;
  171. v4l2_enc_ppe_init_params ppe_init_params; // Configuration params for preprocessing enhancements module
  172. int max_perf;
  173. int blocking_mode; //Set if running in blocking mode
  174. sem_t pollthread_sema; // Polling thread waits on this to be signalled to issue Poll
  175. sem_t encoderthread_sema; // Encoder thread waits on this to be signalled to continue q/dq loop
  176. pthread_t enc_pollthread; // Polling thread, created if running in non-blocking mode.
  177. pthread_t enc_capture_loop; // Encoder capture thread
  178. } context_t;
  179. int parse_csv_args(context_t * ctx, int argc, char *argv[]);