NvJpegDecoder.cpp 12 KB

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