NvJpegEncoder.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 "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.raw_data_in = TRUE;
  76. cinfo.in_color_space = JCS_YCbCr;
  77. jpeg_set_defaults(&cinfo);
  78. jpeg_set_quality(&cinfo, quality, TRUE);
  79. jpeg_set_hardware_acceleration_parameters_enc(&cinfo, TRUE, out_buf_size, 0, 0);
  80. switch (color_space)
  81. {
  82. case JCS_YCbCr:
  83. cinfo.in_color_space = JCS_YCbCr;
  84. break;
  85. default:
  86. COMP_ERROR_MSG("Color format " << color_space << " not supported\n");
  87. return -1;
  88. }
  89. jpeg_start_compress (&cinfo, 0);
  90. if (cinfo.err->msg_code)
  91. {
  92. char err_string[256];
  93. cinfo.err->format_message((j_common_ptr) &cinfo, err_string);
  94. COMP_ERROR_MSG ("Error in jpeg_start_compress: " << err_string);
  95. return -1;
  96. }
  97. jpeg_write_raw_data (&cinfo, NULL, 0);
  98. jpeg_finish_compress(&cinfo);
  99. COMP_DEBUG_MSG("Succesfully encoded Buffer fd=" << fd);
  100. profiler.finishProcessing(buffer_id, false);
  101. return 0;
  102. }
  103. int
  104. NvJPEGEncoder::encodeFromBuffer(NvBuffer & buffer, J_COLOR_SPACE color_space,
  105. unsigned char **out_buf, unsigned long &out_buf_size,
  106. int quality)
  107. {
  108. unsigned char **line[3];
  109. uint32_t comp_height[MAX_CHANNELS];
  110. uint32_t comp_width[MAX_CHANNELS];
  111. uint32_t h_samp[MAX_CHANNELS];
  112. uint32_t v_samp[MAX_CHANNELS];
  113. uint32_t h_max_samp = 0;
  114. uint32_t v_max_samp = 0;
  115. uint32_t channels;
  116. unsigned char *base[MAX_CHANNELS], *end[MAX_CHANNELS];
  117. unsigned int stride[MAX_CHANNELS];
  118. uint32_t width;
  119. uint32_t height;
  120. uint32_t i, j, k;
  121. uint32_t buffer_id;
  122. buffer_id = profiler.startProcessing();
  123. jpeg_mem_dest(&cinfo, out_buf, &out_buf_size);
  124. width = buffer.planes[0].fmt.width;
  125. height = buffer.planes[0].fmt.height;
  126. switch (color_space)
  127. {
  128. case JCS_YCbCr:
  129. channels = 3;
  130. comp_width[0] = width;
  131. comp_height[0] = height;
  132. comp_width[1] = width / 2;
  133. comp_height[1] = height / 2;
  134. comp_width[2] = width / 2;
  135. comp_height[2] = height / 2;
  136. break;
  137. default:
  138. COMP_ERROR_MSG("Color format " << color_space <<
  139. " not supported\n");
  140. return -1;
  141. }
  142. if (channels != buffer.n_planes)
  143. {
  144. COMP_ERROR_MSG("Buffer not in proper format");
  145. return -1;
  146. }
  147. for (i = 0; i < channels; i++)
  148. {
  149. if (comp_width[i] != buffer.planes[i].fmt.width ||
  150. comp_height[i] != buffer.planes[i].fmt.height)
  151. {
  152. COMP_ERROR_MSG("Buffer not in proper format");
  153. return -1;
  154. }
  155. }
  156. h_max_samp = 0;
  157. v_max_samp = 0;
  158. for (i = 0; i < channels; ++i)
  159. {
  160. h_samp[i] = ROUND_UP_4(comp_width[0]) / comp_width[i];
  161. h_max_samp = MAX(h_max_samp, h_samp[i]);
  162. v_samp[i] = ROUND_UP_4(comp_height[0]) / comp_height[i];
  163. v_max_samp = MAX(v_max_samp, v_samp[i]);
  164. }
  165. for (i = 0; i < channels; ++i)
  166. {
  167. h_samp[i] = h_max_samp / h_samp[i];
  168. v_samp[i] = v_max_samp / v_samp[i];
  169. }
  170. cinfo.image_width = width;
  171. cinfo.image_height = height;
  172. cinfo.input_components = channels;
  173. cinfo.in_color_space = color_space;
  174. jpeg_set_defaults(&cinfo);
  175. jpeg_set_quality(&cinfo, quality, TRUE);
  176. jpeg_set_hardware_acceleration_parameters_enc(&cinfo, TRUE, out_buf_size, 0, 0);
  177. cinfo.raw_data_in = TRUE;
  178. if (cinfo.in_color_space == JCS_RGB)
  179. jpeg_set_colorspace(&cinfo, JCS_RGB);
  180. switch (color_space)
  181. {
  182. case JCS_YCbCr:
  183. cinfo.in_color_space = JCS_YCbCr;
  184. break;
  185. default:
  186. COMP_ERROR_MSG("Color format " << color_space << " not supported\n");
  187. return -1;
  188. }
  189. for (i = 0; i < channels; i++)
  190. {
  191. cinfo.comp_info[i].h_samp_factor = h_samp[i];
  192. cinfo.comp_info[i].v_samp_factor = v_samp[i];
  193. line[i] = (unsigned char **) malloc(v_max_samp * DCTSIZE *
  194. sizeof(unsigned char *));
  195. }
  196. for (i = 0; i < channels; i++)
  197. {
  198. base[i] = (unsigned char *) buffer.planes[i].data;
  199. stride[i] = buffer.planes[i].fmt.stride;
  200. end[i] = base[i] + comp_height[i] * stride[i];
  201. }
  202. jpeg_start_compress(&cinfo, TRUE);
  203. if (cinfo.err->msg_code)
  204. {
  205. char err_string[256];
  206. cinfo.err->format_message((j_common_ptr) &cinfo, err_string);
  207. COMP_ERROR_MSG ("Error in jpeg_start_compress: " << err_string);
  208. return -1;
  209. }
  210. for (i = 0; i < height; i += v_max_samp * DCTSIZE)
  211. {
  212. for (k = 0; k < channels; k++)
  213. {
  214. for (j = 0; j < v_samp[k] * DCTSIZE; j++)
  215. {
  216. line[k][j] = base[k];
  217. if (base[k] + stride[k] < end[k])
  218. base[k] += stride[k];
  219. }
  220. }
  221. jpeg_write_raw_data(&cinfo, line, v_max_samp * DCTSIZE);
  222. }
  223. jpeg_finish_compress(&cinfo);
  224. for (i = 0; i < channels; i++)
  225. {
  226. free(line[i]);
  227. }
  228. COMP_DEBUG_MSG("Succesfully encoded Buffer");
  229. profiler.finishProcessing(buffer_id, false);
  230. return 0;
  231. }
  232. void
  233. NvJPEGEncoder::setCropRect(uint32_t left, uint32_t top, uint32_t width,
  234. uint32_t height)
  235. {
  236. cinfo.crop_rect.left = left;
  237. cinfo.crop_rect.top = top;
  238. cinfo.crop_rect.width = width;
  239. cinfo.crop_rect.height = height;
  240. }
  241. void
  242. NvJPEGEncoder::setScaledEncodeParams(uint32_t scale_width, uint32_t scale_height)
  243. {
  244. cinfo.image_scale = TRUE;
  245. cinfo.scaled_image_width = scale_width;
  246. cinfo.scaled_image_height = scale_height;
  247. }