jpegdecscan.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. -- Description : jpeg scan decode source
  17. --
  18. ------------------------------------------------------------------------------
  19. --
  20. -- Version control information, please leave untouched.
  21. --
  22. -- $RCSfile: jpegdecscan.c,v $
  23. -- $Revision: 1.12 $
  24. -- $Date: 2008/10/14 12:00:55 $
  25. --
  26. ------------------------------------------------------------------------------*/
  27. /*------------------------------------------------------------------------------
  28. Table of contents
  29. 1. Include headers
  30. 2. External compiler flags
  31. 3. Module defines
  32. 4. Local function prototypes
  33. 5. Functions
  34. - JpegDecDecodeScan
  35. - JpegDecDecodeScanHeader
  36. - JpegDecDecodeBaselineScan
  37. - JpegDecDecodeProgressiveScan
  38. - JpegDecDecodeBaselineBlock
  39. ------------------------------------------------------------------------------*/
  40. /*------------------------------------------------------------------------------
  41. 1. Include headers
  42. ------------------------------------------------------------------------------*/
  43. #include <linux/module.h>
  44. #include <linux/platform_device.h>
  45. #include "jpegdecutils.h"
  46. #include "jpegdecinternal.h"
  47. #include "jpegdecmarkers.h"
  48. #include "jpegdecscan.h"
  49. #include "dwl.h"
  50. /*------------------------------------------------------------------------------
  51. 2. External compiler flags
  52. --------------------------------------------------------------------------------
  53. _DTRACE: define this flag to print trace information to stdout
  54. --------------------------------------------------------------------------------
  55. 3. Module defines
  56. ------------------------------------------------------------------------------*/
  57. /*------------------------------------------------------------------------------
  58. 4. Local function prototypes
  59. ------------------------------------------------------------------------------*/
  60. JpegDecRet JpegDecDecodeScanHeader(JpegDecContainer *);
  61. /*------------------------------------------------------------------------------
  62. 5. Functions
  63. ------------------------------------------------------------------------------*/
  64. /*------------------------------------------------------------------------------
  65. Function name: JpegDecDecodeScan
  66. Functional description:
  67. Handles the top level control of scan decoding
  68. Inputs:
  69. JpegDecContainer *pDecData Pointer to JpegDecContainer structure
  70. Outputs:
  71. OK (0)
  72. NOK (-1)
  73. ------------------------------------------------------------------------------*/
  74. JpegDecRet JpegDecDecodeScan(JpegDecContainer * pDecData)
  75. {
  76. JpegDecRet retCode; /* Returned code container */
  77. retCode = JPEGDEC_ERROR;
  78. retCode = JpegDecDecodeScanHeader(pDecData);
  79. if(retCode != JPEGDEC_OK)
  80. {
  81. return (retCode);
  82. }
  83. JPEGDEC_TRACE_INTERNAL(("SCAN: Allocate residual buffer\n"));
  84. /* Allocate buffers if not done already */
  85. if(!pDecData->info.allocated)
  86. {
  87. retCode = JpegDecAllocateResidual(pDecData);
  88. if(retCode != JPEGDEC_OK)
  89. {
  90. JPEGDEC_TRACE_INTERNAL(("SCAN: ALLOCATE ERROR\n"));
  91. return (retCode);
  92. }
  93. /* update */
  94. pDecData->info.allocated = 1;
  95. }
  96. JPEGDEC_TRACE_INTERNAL(("SCAN: Scan rlc data\n"));
  97. JPEGDEC_TRACE_INTERNAL(("SCAN: MODE: %d\n", pDecData->frame.codingType));
  98. return (JPEGDEC_OK);
  99. }
  100. /*------------------------------------------------------------------------------
  101. Function name: JpegDecDecodeScanHeader
  102. Functional description:
  103. Decodes scan headers
  104. Inputs:
  105. JpegDecContainer *pDecData Pointer to JpegDecContainer structure
  106. Outputs:
  107. OK (0)
  108. NOK (-1)
  109. ------------------------------------------------------------------------------*/
  110. JpegDecRet JpegDecDecodeScanHeader(JpegDecContainer * pDecData)
  111. {
  112. u32 i;
  113. u32 tmp;
  114. StreamStorage *pStream = &(pDecData->stream);
  115. pDecData->scan.Ls = JpegDecGet2Bytes(pStream);
  116. /* check if there is enough data */
  117. if(((pStream->readBits / 8) + pDecData->scan.Ls) > pStream->streamLength)
  118. return (JPEGDEC_STRM_ERROR);
  119. pDecData->scan.Ns = JpegDecGetByte(pStream);
  120. pDecData->info.fillX = pDecData->info.fillY = 0;
  121. if(pDecData->scan.Ns == 1)
  122. {
  123. /* Reset to non-interleaved baseline operation type */
  124. if(pDecData->info.operationType == JPEGDEC_BASELINE &&
  125. pDecData->info.yCbCrMode != JPEGDEC_YUV400)
  126. pDecData->info.operationType = JPEGDEC_NONINTERLEAVED;
  127. tmp = JpegDecGetByte(&(pDecData->stream));
  128. pDecData->frame.cIndex = tmp - 1;
  129. pDecData->info.componentId = pDecData->frame.cIndex;
  130. pDecData->scan.Cs[pDecData->frame.cIndex] = tmp;
  131. tmp = JpegDecGetByte(pStream);
  132. pDecData->scan.Td[pDecData->frame.cIndex] = tmp >> 4;
  133. pDecData->scan.Ta[pDecData->frame.cIndex] = tmp & 0x0F;
  134. /* check/update component info */
  135. if(pDecData->frame.Nf == 3)
  136. {
  137. pDecData->info.fillRight = 0;
  138. pDecData->info.fillBottom = 0;
  139. pDecData->info.
  140. pfNeeded[pDecData->scan.Cs[pDecData->frame.cIndex] - 1] = 0;
  141. if(pDecData->scan.Cs[pDecData->frame.cIndex] == 2 ||
  142. pDecData->scan.Cs[pDecData->frame.cIndex] == 3)
  143. {
  144. if(pDecData->info.operationType == JPEGDEC_PROGRESSIVE ||
  145. pDecData->info.operationType == JPEGDEC_NONINTERLEAVED ||
  146. pDecData->info.nonInterleavedScanReady)
  147. {
  148. if(pDecData->info.yCbCrModeOrig == JPEGDEC_YUV420)
  149. {
  150. pDecData->info.X = pDecData->frame.hwX >> 1;
  151. pDecData->info.Y = pDecData->frame.hwY >> 1;
  152. }
  153. else if(pDecData->info.yCbCrModeOrig == JPEGDEC_YUV422)
  154. {
  155. pDecData->info.X = pDecData->frame.hwX >> 1;
  156. }
  157. else if(pDecData->info.yCbCrModeOrig == JPEGDEC_YUV440)
  158. {
  159. pDecData->info.Y = pDecData->frame.hwY >> 1;
  160. }
  161. else
  162. {
  163. pDecData->info.yCbCrMode = 0;
  164. return (JPEGDEC_UNSUPPORTED);
  165. }
  166. }
  167. pDecData->info.yCbCrMode = 0;
  168. }
  169. else if(pDecData->scan.Cs[pDecData->frame.cIndex] == 1) /* YCbCr 4:2:0 */
  170. {
  171. pDecData->info.X = pDecData->frame.hwX;
  172. pDecData->info.Y = pDecData->frame.hwY;
  173. if(pDecData->info.yCbCrMode == JPEGDEC_YUV420)
  174. {
  175. pDecData->info.yCbCrMode = 1;
  176. }
  177. else if(pDecData->info.yCbCrMode == JPEGDEC_YUV422)
  178. {
  179. pDecData->info.yCbCrMode = 0;
  180. if(pDecData->frame.cIndex == 0)
  181. {
  182. JPEGDEC_TRACE_INTERNAL(("SCAN: #YUV422 FLAG\n"));
  183. pDecData->info.yCbCr422 = 1;
  184. }
  185. }
  186. else if(pDecData->info.yCbCrMode == JPEGDEC_YUV444)
  187. {
  188. pDecData->info.yCbCrMode = 0;
  189. return (JPEGDEC_UNSUPPORTED);
  190. }
  191. }
  192. else
  193. {
  194. pDecData->info.yCbCrMode = 0;
  195. return (JPEGDEC_UNSUPPORTED);
  196. }
  197. if(pDecData->info.X & 0xF)
  198. {
  199. pDecData->info.X += 8;
  200. pDecData->info.fillX = 1;
  201. }
  202. else if((pDecData->scan.Cs[pDecData->frame.cIndex] == 1 ||
  203. pDecData->info.yCbCrModeOrig == JPEGDEC_YUV440) &&
  204. (pDecData->frame.X & 0xF) && (pDecData->frame.X & 0xF) <= 8)
  205. {
  206. pDecData->info.fillRight = 1;
  207. }
  208. if(pDecData->info.Y & 0xF)
  209. {
  210. pDecData->info.Y += 8;
  211. pDecData->info.fillY = 1;
  212. }
  213. else if((pDecData->scan.Cs[pDecData->frame.cIndex] == 1 ||
  214. pDecData->info.yCbCrModeOrig == JPEGDEC_YUV422) &&
  215. (pDecData->frame.Y & 0xF) && (pDecData->frame.Y & 0xF) <= 8)
  216. {
  217. pDecData->info.fillBottom = 1;
  218. }
  219. }
  220. else if(pDecData->frame.Nf == 1)
  221. {
  222. JPEGDEC_TRACE_INTERNAL(("SCAN: #YUV422 FLAG\n"));
  223. pDecData->info.yCbCr422 = 0;
  224. }
  225. /* decoding info */
  226. if(pDecData->info.operationType == JPEGDEC_PROGRESSIVE ||
  227. pDecData->info.operationType == JPEGDEC_NONINTERLEAVED)
  228. {
  229. pDecData->info.yCbCrMode = 0;
  230. }
  231. }
  232. else
  233. {
  234. for(i = 0; i < pDecData->scan.Ns; i++)
  235. {
  236. pDecData->scan.Cs[i] = JpegDecGetByte(&(pDecData->stream));
  237. tmp = JpegDecGetByte(pStream);
  238. pDecData->scan.Td[i] = tmp >> 4; /* which DC table */
  239. pDecData->scan.Ta[i] = tmp & 0x0F; /* which AC table */
  240. pDecData->info.pfNeeded[i] = 1;
  241. }
  242. pDecData->info.X = pDecData->frame.hwX;
  243. pDecData->info.Y = pDecData->frame.hwY;
  244. pDecData->frame.cIndex = 0;
  245. pDecData->info.yCbCrMode = pDecData->info.yCbCrModeOrig;
  246. }
  247. pDecData->scan.Ss = JpegDecGetByte(pStream);
  248. pDecData->scan.Se = JpegDecGetByte(pStream);
  249. tmp = JpegDecGetByte(pStream);
  250. pDecData->scan.Ah = tmp >> 4;
  251. pDecData->scan.Al = tmp & 0x0F;
  252. if(pDecData->frame.codingType == SOF0)
  253. {
  254. /* baseline */
  255. if(pDecData->scan.Ss != 0)
  256. return (JPEGDEC_UNSUPPORTED);
  257. if(pDecData->scan.Se != 63)
  258. return (JPEGDEC_UNSUPPORTED);
  259. if(pDecData->scan.Ah != 0)
  260. return (JPEGDEC_UNSUPPORTED);
  261. if(pDecData->scan.Al != 0)
  262. return (JPEGDEC_UNSUPPORTED);
  263. /* update scan decoding parameters */
  264. /* interleaved/non-interleaved */
  265. if(pDecData->info.operationType == JPEGDEC_BASELINE)
  266. pDecData->info.nonInterleaved = 0;
  267. else
  268. pDecData->info.nonInterleaved = 1;
  269. /* decoding info */
  270. if((pDecData->frame.Nf == 3 && pDecData->scan.Ns == 1) ||
  271. (pDecData->frame.Nf == 1 && pDecData->scan.Ns == 1))
  272. pDecData->info.amountOfQTables = 1;
  273. else
  274. pDecData->info.amountOfQTables = 3;
  275. }
  276. if(pDecData->frame.codingType == SOF2)
  277. {
  278. /* progressive */
  279. if(pDecData->scan.Ss == 0 && pDecData->scan.Se != 0)
  280. return (JPEGDEC_UNSUPPORTED);
  281. if(pDecData->scan.Ah < 0 || pDecData->scan.Ah > 13)
  282. return (JPEGDEC_UNSUPPORTED);
  283. if(pDecData->scan.Al < 0 || pDecData->scan.Al > 13)
  284. return (JPEGDEC_UNSUPPORTED);
  285. /* update scan decoding parameters */
  286. /* TODO! What if 2 components, possible??? */
  287. /* interleaved/non-interleaved */
  288. if(pDecData->scan.Ns == 1)
  289. {
  290. pDecData->info.nonInterleaved = 1;
  291. /* component ID */
  292. pDecData->info.componentId = pDecData->frame.cIndex;
  293. pDecData->info.amountOfQTables = 1;
  294. }
  295. else
  296. {
  297. pDecData->info.nonInterleaved = 0;
  298. /* component ID ==> set to luma ==> interleaved */
  299. pDecData->info.componentId = 0;
  300. pDecData->info.amountOfQTables = 3;
  301. }
  302. }
  303. return (JPEGDEC_OK);
  304. }