NvJpegEncoder.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright (c) 2016-2022, 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 "NvJpegEncoder.h"
  29. #include "NvLogging.h"
  30. #include <string.h>
  31. #include <malloc.h>
  32. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  33. #define ROUND_UP_4(num) (((num) + 3) & ~3)
  34. #define CAT_NAME "JpegEncoder"
  35. NvJPEGEncoder::NvJPEGEncoder(const char *comp_name)
  36. :NvElement(comp_name, valid_fields)
  37. {
  38. memset(&cinfo, 0, sizeof(cinfo));
  39. memset(&jerr, 0, sizeof(jerr));
  40. cinfo.err = jpeg_std_error(&jerr);
  41. jpeg_create_compress(&cinfo);
  42. jpeg_suppress_tables(&cinfo, TRUE);
  43. }
  44. NvJPEGEncoder *
  45. NvJPEGEncoder::createJPEGEncoder(const char *comp_name)
  46. {
  47. NvJPEGEncoder *jpegenc = new NvJPEGEncoder(comp_name);
  48. if (jpegenc->isInError())
  49. {
  50. delete jpegenc;
  51. return NULL;
  52. }
  53. return jpegenc;
  54. }
  55. NvJPEGEncoder::~NvJPEGEncoder()
  56. {
  57. jpeg_destroy_compress(&cinfo);
  58. CAT_DEBUG_MSG(comp_name << " (" << this << ") destroyed");
  59. }
  60. int
  61. NvJPEGEncoder::encodeFromFd(int fd, J_COLOR_SPACE color_space,
  62. unsigned char **out_buf, unsigned long &out_buf_size,
  63. int quality)
  64. {
  65. uint32_t buffer_id;
  66. if (fd == -1)
  67. {
  68. COMP_ERROR_MSG("Not encoding because fd = -1");
  69. return -1;
  70. }
  71. buffer_id = profiler.startProcessing();
  72. jpeg_mem_dest(&cinfo, out_buf, &out_buf_size);
  73. cinfo.fd = fd;
  74. cinfo.IsVendorbuf = TRUE;
  75. cinfo.is_deepstream = TRUE;
  76. cinfo.raw_data_in = TRUE;
  77. cinfo.in_color_space = JCS_YCbCr;
  78. jpeg_set_defaults(&cinfo);
  79. jpeg_set_quality(&cinfo, quality, TRUE);
  80. jpeg_set_hardware_acceleration_parameters_enc(&cinfo, TRUE, out_buf_size, 0, 0);
  81. switch (color_space)
  82. {
  83. case JCS_YCbCr:
  84. cinfo.in_color_space = JCS_YCbCr;
  85. break;
  86. default:
  87. COMP_ERROR_MSG("Color format " << color_space << " not supported\n");
  88. return -1;
  89. }
  90. jpeg_start_compress (&cinfo, 0);
  91. if (cinfo.err->msg_code)
  92. {
  93. char err_string[256];
  94. cinfo.err->format_message((j_common_ptr) &cinfo, err_string);
  95. COMP_ERROR_MSG ("Error in jpeg_start_compress: " << err_string);
  96. return -1;
  97. }
  98. jpeg_write_raw_data (&cinfo, NULL, 0);
  99. jpeg_finish_compress(&cinfo);
  100. COMP_DEBUG_MSG("Succesfully encoded Buffer fd=" << fd);
  101. profiler.finishProcessing(buffer_id, false);
  102. return 0;
  103. }
  104. int
  105. NvJPEGEncoder::encodeFromBuffer(NvBuffer & buffer, J_COLOR_SPACE color_space,
  106. unsigned char **out_buf, unsigned long &out_buf_size,
  107. int quality)
  108. {
  109. unsigned char **line[3];
  110. uint32_t comp_height[MAX_CHANNELS];
  111. uint32_t comp_width[MAX_CHANNELS];
  112. uint32_t h_samp[MAX_CHANNELS];
  113. uint32_t v_samp[MAX_CHANNELS];
  114. uint32_t h_max_samp = 0;
  115. uint32_t v_max_samp = 0;
  116. uint32_t channels;
  117. unsigned char *base[MAX_CHANNELS], *end[MAX_CHANNELS];
  118. unsigned int stride[MAX_CHANNELS];
  119. uint32_t width;
  120. uint32_t height;
  121. uint32_t i, j, k;
  122. uint32_t buffer_id;
  123. buffer_id = profiler.startProcessing();
  124. jpeg_mem_dest(&cinfo, out_buf, &out_buf_size);
  125. width = buffer.planes[0].fmt.width;
  126. height = buffer.planes[0].fmt.height;
  127. switch (color_space)
  128. {
  129. case JCS_YCbCr:
  130. channels = 3;
  131. comp_width[0] = width;
  132. comp_height[0] = height;
  133. comp_width[1] = width / 2;
  134. comp_height[1] = height / 2;
  135. comp_width[2] = width / 2;
  136. comp_height[2] = height / 2;
  137. break;
  138. default:
  139. COMP_ERROR_MSG("Color format " << color_space <<
  140. " not supported\n");
  141. return -1;
  142. }
  143. if (channels != buffer.n_planes)
  144. {
  145. COMP_ERROR_MSG("Buffer not in proper format");
  146. return -1;
  147. }
  148. for (i = 0; i < channels; i++)
  149. {
  150. if (comp_width[i] != buffer.planes[i].fmt.width ||
  151. comp_height[i] != buffer.planes[i].fmt.height)
  152. {
  153. COMP_ERROR_MSG("Buffer not in proper format");
  154. return -1;
  155. }
  156. }
  157. h_max_samp = 0;
  158. v_max_samp = 0;
  159. for (i = 0; i < channels; ++i)
  160. {
  161. h_samp[i] = ROUND_UP_4(comp_width[0]) / comp_width[i];
  162. h_max_samp = MAX(h_max_samp, h_samp[i]);
  163. v_samp[i] = ROUND_UP_4(comp_height[0]) / comp_height[i];
  164. v_max_samp = MAX(v_max_samp, v_samp[i]);
  165. }
  166. for (i = 0; i < channels; ++i)
  167. {
  168. h_samp[i] = h_max_samp / h_samp[i];
  169. v_samp[i] = v_max_samp / v_samp[i];
  170. }
  171. cinfo.image_width = width;
  172. cinfo.image_height = height;
  173. cinfo.input_components = channels;
  174. cinfo.in_color_space = color_space;
  175. jpeg_set_defaults(&cinfo);
  176. jpeg_set_quality(&cinfo, quality, TRUE);
  177. jpeg_set_hardware_acceleration_parameters_enc(&cinfo, TRUE, out_buf_size, 0, 0);
  178. cinfo.raw_data_in = TRUE;
  179. if (cinfo.in_color_space == JCS_RGB)
  180. jpeg_set_colorspace(&cinfo, JCS_RGB);
  181. switch (color_space)
  182. {
  183. case JCS_YCbCr:
  184. cinfo.in_color_space = JCS_YCbCr;
  185. break;
  186. default:
  187. COMP_ERROR_MSG("Color format " << color_space << " not supported\n");
  188. return -1;
  189. }
  190. for (i = 0; i < channels; i++)
  191. {
  192. cinfo.comp_info[i].h_samp_factor = h_samp[i];
  193. cinfo.comp_info[i].v_samp_factor = v_samp[i];
  194. line[i] = (unsigned char **) malloc(v_max_samp * DCTSIZE *
  195. sizeof(unsigned char *));
  196. }
  197. for (i = 0; i < channels; i++)
  198. {
  199. base[i] = (unsigned char *) buffer.planes[i].data;
  200. stride[i] = buffer.planes[i].fmt.stride;
  201. end[i] = base[i] + comp_height[i] * stride[i];
  202. }
  203. jpeg_start_compress(&cinfo, TRUE);
  204. if (cinfo.err->msg_code)
  205. {
  206. char err_string[256];
  207. cinfo.err->format_message((j_common_ptr) &cinfo, err_string);
  208. COMP_ERROR_MSG ("Error in jpeg_start_compress: " << err_string);
  209. return -1;
  210. }
  211. for (i = 0; i < height; i += v_max_samp * DCTSIZE)
  212. {
  213. for (k = 0; k < channels; k++)
  214. {
  215. for (j = 0; j < v_samp[k] * DCTSIZE; j++)
  216. {
  217. line[k][j] = base[k];
  218. if (base[k] + stride[k] < end[k])
  219. base[k] += stride[k];
  220. }
  221. }
  222. jpeg_write_raw_data(&cinfo, line, v_max_samp * DCTSIZE);
  223. }
  224. jpeg_finish_compress(&cinfo);
  225. for (i = 0; i < channels; i++)
  226. {
  227. free(line[i]);
  228. }
  229. COMP_DEBUG_MSG("Succesfully encoded Buffer");
  230. profiler.finishProcessing(buffer_id, false);
  231. return 0;
  232. }
  233. void
  234. NvJPEGEncoder::setCropRect(uint32_t left, uint32_t top, uint32_t width,
  235. uint32_t height)
  236. {
  237. cinfo.crop_rect.left = left;
  238. cinfo.crop_rect.top = top;
  239. cinfo.crop_rect.width = width;
  240. cinfo.crop_rect.height = height;
  241. }
  242. void
  243. NvJPEGEncoder::setScaledEncodeParams(uint32_t scale_width, uint32_t scale_height)
  244. {
  245. cinfo.image_scale = TRUE;
  246. cinfo.scaled_image_width = scale_width;
  247. cinfo.scaled_image_height = scale_height;
  248. }