NvJpegDecoder.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 "NvJpegDecoder.h"
  29. #include "NvLogging.h"
  30. #include <string.h>
  31. #include <malloc.h>
  32. #include "unistd.h"
  33. #include "stdlib.h"
  34. #include "nvbufsurface.h"
  35. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  36. #define ROUND_UP_4(num) (((num) + 3) & ~3)
  37. #define CAT_NAME "JpegDecoder"
  38. NvJPEGDecoder::NvJPEGDecoder(const char *comp_name)
  39. :NvElement(comp_name, valid_fields)
  40. {
  41. memset(&cinfo, 0, sizeof(cinfo));
  42. memset(&jerr, 0, sizeof(jerr));
  43. cinfo.err = jpeg_std_error(&jerr);
  44. jpeg_create_decompress(&cinfo);
  45. }
  46. NvJPEGDecoder *
  47. NvJPEGDecoder::createJPEGDecoder(const char *comp_name)
  48. {
  49. NvJPEGDecoder *jpegdec = new NvJPEGDecoder(comp_name);
  50. if (jpegdec->isInError())
  51. {
  52. delete jpegdec;
  53. return NULL;
  54. }
  55. return jpegdec;
  56. }
  57. NvJPEGDecoder::~NvJPEGDecoder()
  58. {
  59. jpeg_destroy_decompress(&cinfo);
  60. CAT_DEBUG_MSG(comp_name << " (" << this << ") destroyed");
  61. }
  62. int
  63. NvJPEGDecoder::decodeToFd(int &fd, unsigned char * in_buf,
  64. unsigned long in_buf_size, uint32_t &pixfmt, uint32_t &width,
  65. uint32_t &height)
  66. {
  67. uint32_t pixel_format = 0;
  68. uint32_t buffer_id;
  69. NvBufSurface surface;
  70. if (in_buf == NULL || in_buf_size == 0)
  71. {
  72. COMP_ERROR_MSG("Not decoding because input buffer = NULL or size = 0");
  73. return -1;
  74. }
  75. buffer_id = profiler.startProcessing();
  76. cinfo.out_color_space = JCS_YCbCr;
  77. jpeg_mem_src(&cinfo, in_buf, in_buf_size);
  78. cinfo.out_color_space = JCS_YCbCr;
  79. /* Read file header, set default decompression parameters */
  80. (void) jpeg_read_header(&cinfo, TRUE);
  81. cinfo.out_color_space = JCS_YCbCr;
  82. cinfo.IsVendorbuf = TRUE;
  83. cinfo.is_deepstream = TRUE;
  84. cinfo.pVendor_buf = (unsigned char*)&surface;
  85. if (cinfo.comp_info[0].h_samp_factor == 2)
  86. {
  87. if (cinfo.comp_info[0].v_samp_factor == 2)
  88. {
  89. pixel_format = V4L2_PIX_FMT_YUV420M;
  90. }
  91. else
  92. {
  93. pixel_format = V4L2_PIX_FMT_YUV422M;
  94. }
  95. }
  96. else
  97. {
  98. if (cinfo.comp_info[0].v_samp_factor == 1)
  99. {
  100. pixel_format = V4L2_PIX_FMT_YUV444M;
  101. }
  102. else
  103. {
  104. pixel_format = V4L2_PIX_FMT_YUV422RM;
  105. }
  106. }
  107. jpeg_start_decompress (&cinfo);
  108. if ((cinfo.output_width % (cinfo.max_h_samp_factor * DCTSIZE))
  109. && pixel_format == V4L2_PIX_FMT_YUV420M)
  110. {
  111. COMP_ERROR_MSG("decodeToFd() failed, please run decodeToBuffer()");
  112. jpeg_finish_decompress(&cinfo);
  113. profiler.finishProcessing(buffer_id, false);
  114. return -1;
  115. }
  116. else
  117. {
  118. jpeg_read_raw_data (&cinfo, NULL, cinfo.comp_info[0].v_samp_factor * DCTSIZE);
  119. }
  120. jpeg_finish_decompress(&cinfo);
  121. width = cinfo.image_width;
  122. height = cinfo.image_height;
  123. pixfmt = pixel_format;
  124. fd = cinfo.fd;
  125. COMP_DEBUG_MSG("Succesfully decoded Buffer fd=" << fd);
  126. profiler.finishProcessing(buffer_id, false);
  127. return 0;
  128. }
  129. int
  130. NvJPEGDecoder::decodeToBuffer(NvBuffer ** buffer, unsigned char * in_buf,
  131. unsigned long in_buf_size, uint32_t *pixfmt, uint32_t * width,
  132. uint32_t * height)
  133. {
  134. NvBuffer *out_buf = NULL;
  135. uint32_t pixel_format = 0;
  136. uint32_t buffer_id;
  137. if (buffer == NULL)
  138. {
  139. COMP_ERROR_MSG("Not decoding because buffer = NULL");
  140. return -1;
  141. }
  142. if (in_buf == NULL || in_buf_size == 0)
  143. {
  144. COMP_ERROR_MSG("Not decoding because input buffer = NULL or size = 0");
  145. return -1;
  146. }
  147. buffer_id = profiler.startProcessing();
  148. cinfo.out_color_space = JCS_YCbCr;
  149. jpeg_mem_src(&cinfo, in_buf, in_buf_size);
  150. cinfo.out_color_space = JCS_YCbCr;
  151. (void) jpeg_read_header(&cinfo, TRUE);
  152. cinfo.out_color_space = JCS_YCbCr;
  153. if (cinfo.comp_info[0].h_samp_factor == 2)
  154. {
  155. if (cinfo.comp_info[0].v_samp_factor == 2)
  156. {
  157. pixel_format = V4L2_PIX_FMT_YUV420M;
  158. }
  159. else
  160. {
  161. pixel_format = V4L2_PIX_FMT_YUV422M;
  162. }
  163. }
  164. else
  165. {
  166. if (cinfo.comp_info[0].v_samp_factor == 1)
  167. {
  168. pixel_format = V4L2_PIX_FMT_YUV444M;
  169. }
  170. else
  171. {
  172. pixel_format = V4L2_PIX_FMT_YUV422RM;
  173. }
  174. }
  175. out_buf = new NvBuffer(pixel_format, cinfo.image_width,
  176. cinfo.image_height, 0);
  177. out_buf->allocateMemory();
  178. cinfo.do_fancy_upsampling = FALSE;
  179. cinfo.do_block_smoothing = FALSE;
  180. cinfo.out_color_space = cinfo.jpeg_color_space;
  181. cinfo.dct_method = JDCT_FASTEST;
  182. cinfo.bMeasure_ImageProcessTime = FALSE;
  183. cinfo.raw_data_out = TRUE;
  184. jpeg_start_decompress (&cinfo);
  185. /* For some widths jpeglib requires more horizontal padding than I420
  186. * provides. In those cases we need to decode into separate buffers and then
  187. * copy over the data into our final picture buffer, otherwise jpeglib might
  188. * write over the end of a line into the beginning of the next line,
  189. * resulting in blocky artifacts on the left side of the picture. */
  190. if ((cinfo.output_width % (cinfo.max_h_samp_factor * DCTSIZE)) != 0
  191. || cinfo.comp_info[0].h_samp_factor != 2
  192. || cinfo.comp_info[1].h_samp_factor != 1
  193. || cinfo.comp_info[2].h_samp_factor != 1
  194. || cinfo.comp_info[0].v_samp_factor != 2
  195. || cinfo.comp_info[1].v_samp_factor != 1
  196. || cinfo.comp_info[2].v_samp_factor != 1)
  197. {
  198. COMP_DEBUG_MSG("indirect decoding using extra buffer copy");
  199. decodeIndirect(out_buf, pixel_format);
  200. }
  201. else
  202. {
  203. decodeDirect(out_buf, pixel_format);
  204. }
  205. jpeg_finish_decompress(&cinfo);
  206. if (width)
  207. {
  208. *width= cinfo.image_width;
  209. }
  210. if (height)
  211. {
  212. *height= cinfo.image_height;
  213. }
  214. if (pixfmt)
  215. {
  216. *pixfmt = pixel_format;
  217. }
  218. *buffer = out_buf;
  219. COMP_DEBUG_MSG("Succesfully decoded Buffer " << buffer);
  220. profiler.finishProcessing(buffer_id, false);
  221. return 0;
  222. }
  223. void
  224. NvJPEGDecoder::decodeIndirect(NvBuffer *out_buf, uint32_t pixel_format)
  225. {
  226. unsigned char *y_rows[16] = { NULL, };
  227. unsigned char *u_rows[16] = { NULL, };
  228. unsigned char *v_rows[16] = { NULL, };
  229. unsigned char **scanarray[3] = { y_rows, u_rows, v_rows };
  230. int i, j, k;
  231. int lines;
  232. unsigned char *base[3] = { NULL, };
  233. unsigned char *last[3] = { NULL, };
  234. int stride[3];
  235. int width, height;
  236. int r_v, r_h, width_32, read_rows;
  237. r_v = cinfo.comp_info[0].v_samp_factor;
  238. r_h = cinfo.comp_info[0].h_samp_factor;
  239. width = cinfo.image_width;
  240. height = cinfo.image_height;
  241. read_rows = r_v * DCTSIZE;
  242. for (i = 0; i < 3; i++)
  243. {
  244. stride[i] = out_buf->planes[i].fmt.stride;
  245. base[i] = out_buf->planes[i].data;
  246. last[i] = base[i] + (stride[i] * (out_buf->planes[i].fmt.height - 1));
  247. }
  248. width_32 = (width + 31) & 0xFFFFFFE0;
  249. for (i = 0; i < read_rows; i++) {
  250. y_rows[i] = new unsigned char [width_32];
  251. u_rows[i] = new unsigned char [width_32];
  252. v_rows[i] = new unsigned char [width_32];
  253. }
  254. for (i = 0; i < height; i += read_rows)
  255. {
  256. lines = jpeg_read_raw_data (&cinfo, scanarray, read_rows);
  257. if (lines > 0)
  258. {
  259. for (j = 0, k = 0; j < read_rows; j += r_v, k++)
  260. {
  261. if (base[0] <= last[0])
  262. {
  263. memcpy ((void*)base[0], (void*)y_rows[j],
  264. stride[0]*sizeof(unsigned char));
  265. base[0] += stride[0];
  266. }
  267. if (r_v == 2)
  268. {
  269. if (base[0] <= last[0])
  270. {
  271. memcpy ((void*)base[0], (void*)y_rows[j + 1],
  272. stride[0]*sizeof(unsigned char));
  273. base[0] += stride[0];
  274. }
  275. }
  276. if (base[1] <= last[1] && base[2] <= last[2])
  277. {
  278. if (r_h == 2
  279. || pixel_format == V4L2_PIX_FMT_YUV444M
  280. || pixel_format == V4L2_PIX_FMT_YUV422RM)
  281. {
  282. memcpy ((void*)base[1], (void*)u_rows[k],
  283. stride[1]*sizeof(unsigned char));
  284. memcpy ((void*)base[2], (void*)v_rows[k],
  285. stride[2]*sizeof(unsigned char));
  286. }
  287. }
  288. if (r_v == 2 || (k & 1) != 0 ||
  289. pixel_format == V4L2_PIX_FMT_YUV444M)
  290. {
  291. base[1] += stride[1];
  292. base[2] += stride[2];
  293. }
  294. }
  295. }
  296. else
  297. {
  298. COMP_ERROR_MSG("jpeg_read_raw_data() returned 0");
  299. }
  300. }
  301. for (i = 0; i < read_rows; i++)
  302. {
  303. delete[] y_rows[i];
  304. delete[] u_rows[i];
  305. delete[] v_rows[i];
  306. }
  307. }
  308. void
  309. NvJPEGDecoder::decodeDirect(NvBuffer *out_buf, uint32_t pixel_format)
  310. {
  311. unsigned char **line[3];
  312. unsigned char *y[4 * DCTSIZE] = { NULL, };
  313. unsigned char *u[4 * DCTSIZE] = { NULL, };
  314. unsigned char *v[4 * DCTSIZE] = { NULL, };
  315. int i, j;
  316. int lines, v_samp[3];
  317. unsigned char *base[3], *last[3];
  318. int stride[3];
  319. line[0] = y;
  320. line[1] = u;
  321. line[2] = v;
  322. for (i = 0; i < 3; i++)
  323. {
  324. v_samp[i] = cinfo.comp_info[i].v_samp_factor;
  325. stride[i] = out_buf->planes[i].fmt.width;
  326. base[i] = out_buf->planes[i].data;
  327. last[i] = base[i] + (stride[i] * (out_buf->planes[i].fmt.height - 1));
  328. }
  329. for (i = 0; i < (int) cinfo.image_height; i += v_samp[0] * DCTSIZE)
  330. {
  331. for (j = 0; j < (v_samp[0] * DCTSIZE); ++j)
  332. {
  333. /* Y */
  334. line[0][j] = base[0] + (i + j) * stride[0];
  335. /* U,V */
  336. if (pixel_format == V4L2_PIX_FMT_YUV420M)
  337. {
  338. /* Y */
  339. line[0][j] = base[0] + (i + j) * stride[0];
  340. if ((line[0][j] > last[0]))
  341. line[0][j] = last[0];
  342. /* U */
  343. if (v_samp[1] == v_samp[0]) {
  344. line[1][j] = base[1] + ((i + j) / 2) * stride[1];
  345. } else if (j < (v_samp[1] * DCTSIZE)) {
  346. line[1][j] = base[1] + ((i / 2) + j) * stride[1];
  347. }
  348. if ((line[1][j] > last[1]))
  349. line[1][j] = last[1];
  350. /* V */
  351. if (v_samp[2] == v_samp[0]) {
  352. line[2][j] = base[2] + ((i + j) / 2) * stride[2];
  353. } else if (j < (v_samp[2] * DCTSIZE)) {
  354. line[2][j] = base[2] + ((i / 2) + j) * stride[2];
  355. }
  356. if ((line[2][j] > last[2]))
  357. line[2][j] = last[2];
  358. }
  359. else
  360. {
  361. line[1][j] = base[1] + (i + j) * stride[1];
  362. line[2][j] = base[2] + (i + j) * stride[2];
  363. }
  364. }
  365. lines = jpeg_read_raw_data (&cinfo, line, v_samp[0] * DCTSIZE);
  366. if ((!lines))
  367. {
  368. COMP_DEBUG_MSG( "jpeg_read_raw_data() returned 0\n");
  369. }
  370. }
  371. }