NvJpegEncoder.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 Multimedia API: Image Encode API</b>
  31. *
  32. * @b This file declares the NvJpegEncoder APIs.
  33. */
  34. #ifndef __NV_JPEG_ENCODER_H__
  35. #define __NV_JPEG_ENCODER_H__
  36. /**
  37. *
  38. * @defgroup l4t_mm_nvjpegencoder_group Image Encoder
  39. * @ingroup l4t_mm_nvimage_group
  40. *
  41. * The \c %NvJPEGEncoder API provides functionality for encoding JPEG
  42. * images using libjpeg APIs.
  43. *
  44. * @{
  45. */
  46. #ifndef TEGRA_ACCELERATE
  47. /**
  48. * @c TEGRA_ACCELERATE must be defined to enable hardware acceleration of
  49. * JPEG encoding.
  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. * encoder.
  61. */
  62. #define MAX_CHANNELS 3
  63. #endif
  64. /**
  65. *
  66. * \c NvJpegEncoder uses the @c libjpeg APIs for decoding JPEG images. It
  67. * supports two methods for encoding:
  68. *
  69. * - Encode using a file descriptor (FD) of the MMAP buffer created by
  70. * a V4L2 element (supports YUV420 and NV12 color formats).
  71. * - Encode using data pointers in NvBuffer object, i.e., software allocated
  72. * memory (@c malloc); supports YUV420 color format.
  73. *
  74. * @note Only the JCS_YCbCr (YUV420) color space is currently
  75. * supported.
  76. */
  77. class NvJPEGEncoder:public NvElement
  78. {
  79. public:
  80. /**
  81. * Creates a new JPEG encoder named @a comp_name.
  82. *
  83. * @return Reference to the newly created decoder object, or NULL
  84. * in case of failure during initialization.
  85. */
  86. static NvJPEGEncoder *createJPEGEncoder(const char *comp_name);
  87. ~NvJPEGEncoder();
  88. /**
  89. *
  90. * Encodes a JPEG image from a file descriptor (FD) of hardware
  91. * buffer memory.
  92. *
  93. * The application may allocate the memory for storing the JPEG
  94. * image. If the allocation is less than what is required, @c
  95. * libjpeg allocates more memory. The @a out_buf pointer and @a
  96. * out_buf_size are updated accordingly.
  97. *
  98. * Supports YUV420 and NV12 formats.
  99. *
  100. * @attention The application must free the @a out_buf memory.
  101. *
  102. * @param[out] fd Indicates the file descriptor (FD) of the hardware buffer.
  103. * @param[out] color_space Indicates the color_space to use for encoding.
  104. * @param[in] out_buf Specifies a pointer to the memory for the JPEG image.
  105. * @param[in] out_buf_size Specifies the size of the output buffer in bytes.
  106. * @param[in] quality Sets the image quality.
  107. * @return 0 for success, -1 otherwise.
  108. */
  109. int encodeFromFd(int fd, J_COLOR_SPACE color_space,
  110. unsigned char **out_buf, unsigned long &out_buf_size,
  111. int quality = 75);
  112. /**
  113. * Encodes a JPEG image from software buffer memory.
  114. *
  115. * The application may allocate the memory for storing the JPEG
  116. * image. If the allocation is less than what is required, @c
  117. * libjpeg allocates more memory. The @a out_buf pointer and @a
  118. * out_buf_size are updated accordingly.
  119. *
  120. * The @c encodeFromBuffer method is slower than
  121. * NvJPEGEncoder::encodeFromFd because @c encodeFromBuffer
  122. * involves conversion from software buffer memory to hardware
  123. * buffer memory.
  124. *
  125. * Supports the YUV420 format only.
  126. *
  127. * @attention The application must free the @a out_buf memory.
  128. *
  129. * @param[out] buffer Indicates the NvBuffer object to contain the encoded
  130. image.
  131. * @param[out] color_space Indicates the color_space to use for encoding.
  132. * @param[in] out_buf Specifies a pointer to the memory for the JPEG image.
  133. * @param[in] out_buf_size Specifies the size of the output buffer in bytes.
  134. * @param[in] quality Sets the image quality.
  135. * @return 0 for success, -1 otherwise.
  136. */
  137. int encodeFromBuffer(NvBuffer & buffer, J_COLOR_SPACE color_space,
  138. unsigned char **out_buf, unsigned long &out_buf_size,
  139. int quality = 75);
  140. /**
  141. * Sets the cropping rectangle used by the JPEG encoder. This method
  142. * must be called before #encodeFromFd or #encodeFromBuffer for the
  143. * cropping to take effect.
  144. *
  145. * @attention The JPEG encoder resets the crop paramaters after each call
  146. * to jpeg_finish_compress. Therefore, this method must be called before every
  147. * call to %encodeFromFd or %encodeFromBuffer.
  148. *
  149. * @param[in] left Horizontal offset of the cropping rectangle, in pixels.
  150. * @param[in] top Vertical offset of the cropping rectangle, in pixels.
  151. * @param[in] width Width of the cropping rectangle, in pixels.
  152. * @param[in] height Height of the cropping rectangle, in pixels.
  153. */
  154. void setCropRect(uint32_t left, uint32_t top, uint32_t width,
  155. uint32_t height);
  156. /**
  157. * Sets scaling parameters by which image needs to be scaled and encoded.
  158. * This method must be called before #encodeFromFd or #encodeFromBuffer for
  159. * scaled encoding to take effect.
  160. *
  161. * @param[in] scale_width Specifies width of the scaled image.
  162. * @param[in] scale_height Specifies height of the scaled image.
  163. */
  164. void setScaledEncodeParams(uint32_t scale_width, uint32_t scale_height);
  165. private:
  166. NvJPEGEncoder(const char *comp_name);
  167. struct jpeg_compress_struct cinfo;
  168. struct jpeg_error_mgr jerr;
  169. static const NvElementProfiler::ProfilerField valid_fields =
  170. NvElementProfiler::PROFILER_FIELD_TOTAL_UNITS |
  171. NvElementProfiler::PROFILER_FIELD_LATENCIES;
  172. };
  173. #endif
  174. /** @} */