EncJpegInit.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*------------------------------------------------------------------------------
  2. -- --
  3. -- This software is confidential and proprietary and may be used --
  4. -- only as expressly authorized by a licensing agreement from --
  5. -- --
  6. -- Hantro Products Oy. --
  7. -- --
  8. -- (C) COPYRIGHT 2006 HANTRO PRODUCTS OY --
  9. -- ALL RIGHTS RESERVED --
  10. -- --
  11. -- The entire notice above must be reproduced --
  12. -- on all copies and should not be removed. --
  13. -- --
  14. --------------------------------------------------------------------------------
  15. --
  16. -- Abstract :
  17. --
  18. ------------------------------------------------------------------------------*/
  19. /*------------------------------------------------------------------------------
  20. Table of contents
  21. 1. Include headers
  22. 2. External compiler flags
  23. 3. Module defines
  24. 4. Local function prototypes
  25. 5. Functions
  26. ------------------------------------------------------------------------------*/
  27. /*------------------------------------------------------------------------------
  28. 1. Include headers
  29. ------------------------------------------------------------------------------*/
  30. #include "EncJpegInit.h"
  31. #include "EncJpegCommon.h"
  32. #include "ewl.h"
  33. /*------------------------------------------------------------------------------
  34. 2. External compiler flags
  35. --------------------------------------------------------------------------------
  36. --------------------------------------------------------------------------------
  37. 3. Module defines
  38. ------------------------------------------------------------------------------*/
  39. /*------------------------------------------------------------------------------
  40. 4. Local function prototypes
  41. ------------------------------------------------------------------------------*/
  42. static void SetQTable(u8 *dst, const u8 *src);
  43. /*------------------------------------------------------------------------------
  44. JpegInit
  45. ------------------------------------------------------------------------------*/
  46. JpegEncRet JpegInit(const JpegEncCfg * pEncCfg, jpegInstance_s ** instAddr)
  47. {
  48. jpegInstance_s *inst = NULL;
  49. const void *ewl = NULL;
  50. JpegEncRet ret = JPEGENC_OK;
  51. EWLInitParam_t param;
  52. ASSERT(pEncCfg);
  53. ASSERT(instAddr);
  54. *instAddr = NULL;
  55. param.clientType = EWL_CLIENT_TYPE_JPEG_ENC;
  56. /* Init EWL */
  57. if((ewl = EWLInit(&param)) == NULL)
  58. return JPEGENC_EWL_ERROR;
  59. /* Encoder instance */
  60. inst = (jpegInstance_s *) EWLcalloc(1, sizeof(jpegInstance_s));
  61. if(inst == NULL)
  62. {
  63. ret = JPEGENC_MEMORY_ERROR;
  64. goto err;
  65. }
  66. /* Default values */
  67. EncJpegInit(&inst->jpeg);
  68. /* Set parameters depending on user config */
  69. /* Choose quantization tables */
  70. inst->jpeg.qTable.pQlumi = QuantLuminance[pEncCfg->qLevel];
  71. inst->jpeg.qTable.pQchromi = QuantChrominance[pEncCfg->qLevel];
  72. /* If user specified quantization tables, use them */
  73. if (pEncCfg->qTableLuma)
  74. {
  75. SetQTable(inst->jpeg.qTableLuma, pEncCfg->qTableLuma);
  76. inst->jpeg.qTable.pQlumi = inst->jpeg.qTableLuma;
  77. }
  78. if (pEncCfg->qTableChroma)
  79. {
  80. SetQTable(inst->jpeg.qTableChroma, pEncCfg->qTableChroma);
  81. inst->jpeg.qTable.pQchromi = inst->jpeg.qTableChroma;
  82. }
  83. /* Comment header data */
  84. if(pEncCfg->comLength > 0 && pEncCfg->pCom != NULL)
  85. {
  86. inst->jpeg.com.comLen = pEncCfg->comLength;
  87. inst->jpeg.com.pComment = pEncCfg->pCom;
  88. inst->jpeg.com.comEnable = 1;
  89. }
  90. /* Units type */
  91. if(pEncCfg->unitsType == JPEGENC_NO_UNITS)
  92. {
  93. inst->jpeg.appn.units = ENC_NO_UNITS;
  94. inst->jpeg.appn.Xdensity = 1;
  95. inst->jpeg.appn.Ydensity = 1;
  96. }
  97. else
  98. {
  99. inst->jpeg.appn.units = pEncCfg->unitsType;
  100. inst->jpeg.appn.Xdensity = pEncCfg->xDensity;
  101. inst->jpeg.appn.Ydensity = pEncCfg->yDensity;
  102. }
  103. /* Marker type */
  104. if(pEncCfg->markerType == JPEGENC_SINGLE_MARKER)
  105. {
  106. inst->jpeg.markerType = ENC_SINGLE_MARKER;
  107. }
  108. else
  109. {
  110. inst->jpeg.markerType = pEncCfg->markerType;
  111. }
  112. #if 0
  113. /* Rotation type */
  114. if(pEncCfg->rotation == JPEGENC_ROTATE_90R)
  115. {
  116. inst->jpeg.rotation = ROTATE_90R;
  117. }
  118. else if(pEncCfg->rotation == JPEGENC_ROTATE_90L)
  119. {
  120. inst->jpeg.rotation = ROTATE_90L;
  121. }
  122. else
  123. {
  124. inst->jpeg.rotation = ROTATE_0;
  125. }
  126. #endif
  127. /* Copy quantization tables to ASIC internal memories */
  128. EncAsicSetQuantTable(&inst->asic, inst->jpeg.qTable.pQlumi,
  129. inst->jpeg.qTable.pQchromi);
  130. /* Initialize ASIC */
  131. inst->asic.ewl = ewl;
  132. (void) EncAsicControllerInit(&inst->asic);
  133. *instAddr = inst;
  134. return ret;
  135. err:
  136. if(inst != NULL)
  137. EWLfree(inst);
  138. if(ewl != NULL)
  139. (void) EWLRelease(ewl);
  140. return ret;
  141. }
  142. /*------------------------------------------------------------------------------
  143. JpegShutdown
  144. Function frees the encoder instance.
  145. Input instance_s * Pointer to the encoder instance to be freed.
  146. After this the pointer is no longer valid.
  147. ------------------------------------------------------------------------------*/
  148. void JpegShutdown(jpegInstance_s * data)
  149. {
  150. const void *ewl;
  151. ASSERT(data);
  152. ewl = data->asic.ewl;
  153. EncAsicMemFree_V2(&data->asic);
  154. EWLfree(data);
  155. (void) EWLRelease(ewl);
  156. }
  157. /*------------------------------------------------------------------------------
  158. SetQTable
  159. ------------------------------------------------------------------------------*/
  160. void SetQTable(u8 *dst, const u8 *src)
  161. {
  162. i32 i;
  163. for (i = 0; i < 64; i++)
  164. {
  165. u8 qp = src[i];
  166. /* Round qp to value that can be handled by ASIC */
  167. if (qp > 128)
  168. qp = qp/8*8;
  169. else if (qp > 64)
  170. qp = qp/4*4;
  171. else if (qp > 32)
  172. qp = qp/2*2;
  173. dst[i] = qp;
  174. }
  175. }