NvJpegDecoder.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2016-2018, 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. /**
  29. * @file
  30. * <b>NVIDIA Multiemdia API: Image Decode API</b>
  31. *
  32. * @b Description: This file declares the NvJpegDecoder APIs.
  33. */
  34. #ifndef __NV_JPEG_DECODER_H__
  35. #define __NV_JPEG_DECODER_H__
  36. /**
  37. *
  38. * @defgroup l4t_mm_nvjpegdecoder_group Image Decoder
  39. * @ingroup l4t_mm_nvimage_group
  40. *
  41. * The \c %NvJPEGDecoder API provides functionality for decoding JPEG
  42. * images using libjpeg APIs.
  43. *
  44. * @{
  45. */
  46. #ifndef TEGRA_ACCELERATE
  47. /**
  48. * @c TEGRA_ACCELERATE must be defined to enable hardware-accelerated
  49. * JPEG decoding.
  50. */
  51. #define TEGRA_ACCELERATE
  52. #endif
  53. #include <stdio.h>
  54. #include "jpeglib.h"
  55. #include "NvElement.h"
  56. #include "NvBuffer.h"
  57. #ifndef MAX_CHANNELS
  58. /**
  59. * Specifies the maximum number of channels to be used with the
  60. * decoder.
  61. */
  62. #define MAX_CHANNELS 3
  63. #endif
  64. /**
  65. * @brief Helper class for decoding JPEG images using libjpeg APIs.
  66. *
  67. * @c %NvJPEGDecoder uses the @c libjpeg APIs for decoding JPEG images. It
  68. * supports two methods for decoding:
  69. *
  70. * - Decode to a @c DMABUF and return its file descriptor (FD).
  71. * - Decode and write data to a NvBuffer object, i.e., software allocated
  72. * memory (@c malloc).
  73. *
  74. * The JPEG decoder is capable of decoding YUV420, YUV422, and YUV444 JPEG images.
  75. *
  76. * @note Only the @c JCS_YCbCr (YUV420) color space is currently
  77. * supported.
  78. */
  79. class NvJPEGDecoder:public NvElement
  80. {
  81. public:
  82. /**
  83. * Creates a new JPEG Decoder named @a comp_name.
  84. *
  85. * @return Reference to the newly created decoder object, or NULL
  86. * in case of failure during initialization.
  87. */
  88. static NvJPEGDecoder *createJPEGDecoder(const char *comp_name);
  89. ~NvJPEGDecoder();
  90. /**
  91. * Decodes a JPEG image to hardware buffer memory.
  92. *
  93. * This method decodes JPEG images in hardware memory to a
  94. * specified width and size.
  95. *
  96. * @param[out] fd A reference to the file descriptor (FD) of the hardware buffer.
  97. * @param[in] in_buf A pointer to the memory containing
  98. * the JPEG image.
  99. * @param[in] in_buf_size Size of the input buffer in bytes.
  100. * @param[out] pixfmt A reference to the V4L2 pixel format
  101. * of the decoded image.
  102. * @param[out] width A reference to the width of the
  103. * decoded image in pixels.
  104. * @param[out] height A reference to the height of the
  105. * decoded image in pixels.
  106. * @return 0 for success, -1 otherwise.
  107. */
  108. int decodeToFd(int &fd,
  109. unsigned char *in_buf, unsigned long in_buf_size,
  110. uint32_t &pixfmt, uint32_t &width, uint32_t &height);
  111. /**
  112. * Decodes a JPEG image to software buffer memory.
  113. *
  114. * This method decodes JPEG images in software memory to a
  115. * specified width and size.
  116. *
  117. * @note The @c decodeToBuffer method is slower than the
  118. * NvJPEGDecoder::decodeToFd method because it involves conversion
  119. * from hardware buffer memory to software buffer memory.
  120. *
  121. * @attention The application must free the NvBuffer object.
  122. *
  123. * @param[out] buffer Indirect pointer to an @c %NvBuffer object that contains
  124. * the decoded image. The object is allocated by
  125. * the method and the buffer memory is
  126. * allocated using NvBuffer::allocateMemory.
  127. * @param[in] in_buf A pointer to the memory containing the JPEG.
  128. * @param[in] in_buf_size Size of the input buffer in bytes.
  129. * @param[out] pixfmt A pointer to the V4L2 pixel format
  130. * of the decoded image.
  131. * @param[out] width A pointer to the width of the decoded image
  132. * in pixels.
  133. * @param[out] height A pointer to the height of the decoded image
  134. * in pixels.
  135. * @return 0 for success, -1 otherwise.
  136. */
  137. int decodeToBuffer(NvBuffer ** buffer,
  138. unsigned char *in_buf, unsigned long in_buf_size,
  139. uint32_t *pixfmt, uint32_t *width, uint32_t *height);
  140. private:
  141. NvJPEGDecoder(const char *comp_name);
  142. void decodeIndirect(NvBuffer *out_buf, uint32_t pixel_format);
  143. void decodeDirect(NvBuffer *out_buf, uint32_t pixel_format);
  144. struct jpeg_decompress_struct cinfo;
  145. struct jpeg_error_mgr jerr;
  146. static const NvElementProfiler::ProfilerField valid_fields =
  147. NvElementProfiler::PROFILER_FIELD_TOTAL_UNITS |
  148. NvElementProfiler::PROFILER_FIELD_LATENCIES;
  149. };
  150. /** @} */
  151. #endif