• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2022 Beken Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include <common/bk_include.h>
15 #include "BK7256_RegList.h"
16 #include "arch_interrupt.h"
17 #include "gpio_map.h"
18 #include "gpio_driver.h"
19 #include "gpio_map.h"
20 #include <driver/int.h>
21 #include "dma_hal.h"
22 #include <driver/dma.h>
23 #include <os/mem.h>
24 #include "dma_driver.h"
25 #include "sys_driver.h"
26 #include "jpeg_dec_macro_def.h"
27 #include "jpeg_dec_ll_macro_def.h"
28 #include "jpeg_dec_hal.h"
29 #include <driver/hal/hal_jpeg_dec_types.h>
30 #include "driver/jpeg_dec_types.h"
31 #include "sys_ll_op_if.h"
32 #include <driver/media_types.h>
33 
34 
35 #define	LDB_WORD(ptr)		(uint16_t)(((uint16_t)*((uint8_t*)(ptr))<<8)|(uint16_t)*(uint8_t*)((ptr)+1))
36 #define ZIG(n)	Zig[n]
37 #define IPSF(n)	Ipsf[n]
38 static JPG_DECODER_ST jpg_dec_st;
39 static JDEC jdec;
40 uint8_t Zig[64] = {	/* Zigzag-order to raster-order conversion table */
41 	 0,  1,  8, 16,  9,  2,  3, 10, 17, 24, 32, 25, 18, 11,  4,  5,
42 	12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13,  6,  7, 14, 21, 28,
43 	35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
44 	58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
45 };
46 
47 static const uint16_t Ipsf[64] = {	/* See also aa_idct.png */
48 	(uint16_t)(1.00000*8192), (uint16_t)(1.38704*8192), (uint16_t)(1.30656*8192), (uint16_t)(1.17588*8192), (uint16_t)(1.00000*8192), (uint16_t)(0.78570*8192), (uint16_t)(0.54120*8192), (uint16_t)(0.27590*8192),
49 	(uint16_t)(1.38704*8192), (uint16_t)(1.92388*8192), (uint16_t)(1.81226*8192), (uint16_t)(1.63099*8192), (uint16_t)(1.38704*8192), (uint16_t)(1.08979*8192), (uint16_t)(0.75066*8192), (uint16_t)(0.38268*8192),
50 	(uint16_t)(1.30656*8192), (uint16_t)(1.81226*8192), (uint16_t)(1.70711*8192), (uint16_t)(1.53636*8192), (uint16_t)(1.30656*8192), (uint16_t)(1.02656*8192), (uint16_t)(0.70711*8192), (uint16_t)(0.36048*8192),
51 	(uint16_t)(1.17588*8192), (uint16_t)(1.63099*8192), (uint16_t)(1.53636*8192), (uint16_t)(1.38268*8192), (uint16_t)(1.17588*8192), (uint16_t)(0.92388*8192), (uint16_t)(0.63638*8192), (uint16_t)(0.32442*8192),
52 	(uint16_t)(1.00000*8192), (uint16_t)(1.38704*8192), (uint16_t)(1.30656*8192), (uint16_t)(1.17588*8192), (uint16_t)(1.00000*8192), (uint16_t)(0.78570*8192), (uint16_t)(0.54120*8192), (uint16_t)(0.27590*8192),
53 	(uint16_t)(0.78570*8192), (uint16_t)(1.08979*8192), (uint16_t)(1.02656*8192), (uint16_t)(0.92388*8192), (uint16_t)(0.78570*8192), (uint16_t)(0.61732*8192), (uint16_t)(0.42522*8192), (uint16_t)(0.21677*8192),
54 	(uint16_t)(0.54120*8192), (uint16_t)(0.75066*8192), (uint16_t)(0.70711*8192), (uint16_t)(0.63638*8192), (uint16_t)(0.54120*8192), (uint16_t)(0.42522*8192), (uint16_t)(0.29290*8192), (uint16_t)(0.14932*8192),
55 	(uint16_t)(0.27590*8192), (uint16_t)(0.38268*8192), (uint16_t)(0.36048*8192), (uint16_t)(0.32442*8192), (uint16_t)(0.27590*8192), (uint16_t)(0.21678*8192), (uint16_t)(0.14932*8192), (uint16_t)(0.07612*8192)
56 };
57 
58 
59 /** breaf   User defined input function
60  *  param1  Decompression object
61  *  param2  Pointer to the read buffer (0:skip)
62  *  param2  Number of bytes to read/skip
63  */
jpeg_dec_input_func(JDEC * jd,uint8_t * buff,uint16_t ndata)64 static uint16_t jpeg_dec_input_func (JDEC* jd, uint8_t* buff, uint16_t ndata)
65 {
66 	if (jpg_dec_st.rd_ptr > jpg_dec_st.jpg_file_size)
67 	{
68 		//os_printf("jpg_dec_st.jpg_file_size = %x \r\n", jpg_dec_st.jpg_file_size);
69 		//os_printf("jpg_dec_st.rd_ptr = %x \r\n", jpg_dec_st.rd_ptr);
70 		return 0xffff;
71 	}
72 	if (NULL != buff)
73 	{
74 		os_memcpy(buff, jpg_dec_st.inputbuf + jpg_dec_st.rd_ptr,ndata);
75 	}
76 	//os_printf("buff = %x \r\n", buff);
77 	//os_printf("*buff = %x \r\n", *buff);
78 	//os_printf("jpg_dec_st.rd_ptr = %x \r\n", jpg_dec_st.rd_ptr);
79 	//os_printf("jpg_dec_st.inputbuf = %x\r\n", *(jpg_dec_st.inputbuf +jpg_dec_st.rd_ptr));
80 
81 	jpg_dec_st.rd_ptr += ndata;
82 	return ndata;
83 
84 }
85 
86 /**
87  * @brief    This API is hw jpeg dec init
88  *
89  * @param   none
90  *
91  * @return
92  *     - BK_OK: succeed
93  *     - others: other errors.
94  */
jpg_decoder_init(void)95 int jpg_decoder_init(void)
96 {
97 	jpg_dec_st.scale_ratio = 0;
98 	jpg_dec_st.rd_ptr = 0;//init rd pointer
99 	jpg_dec_st.jpg_file_size = 1024;
100 
101 	jpg_dec_st.workbuf = os_malloc(WORK_AREA_SIZE);
102 	if (NULL == jpg_dec_st.workbuf)
103 	{
104 //		uart_printf("work area error!!\r\n");
105 		return -1;
106 	}
107 	return 0;
108 }
109 
110 
111 /**
112  * @brief    This API is hw jpeg dec deinit
113  *
114  * @param   none
115  *
116  * @return
117  *     - BK_OK: succeed
118  *     - others: other errors.
119  */
jpg_decoder_deinit(void)120 int jpg_decoder_deinit(void)
121 {
122 	if(NULL != jpg_dec_st.workbuf) {
123 		os_free(jpg_dec_st.workbuf);
124 		jpg_dec_st.workbuf = NULL;
125 	}
126 	os_memset(&jpg_dec_st, 0, sizeof(jpg_dec_st));
127 	return kNoErr;
128 }
129 
130 /**
131  * @brief    Pointer to allocated memory block (NULL:no memory available)
132  *
133  * @param1   Pointer to the decompressor object
134  *
135  * @param2   Number of bytes to allocate
136  * @return
137  *     - BK_OK: succeed
138  *     - others: other errors.
139  */
alloc_pool(JDEC * jd,uint16_t nd)140 static void* alloc_pool(JDEC* jd, uint16_t nd)
141 {
142 	char *rp = 0;
143 
144 
145 	nd = (nd + 3) & ~3;			/* Align block size to the word boundary */
146 
147 	if (jd->sz_pool >= nd) {
148 		jd->sz_pool -= nd;
149 		rp = (char*)jd->pool;			/* Get start of available memory pool */
150 		jd->pool = (void*)(rp + nd);	/* Allocate requierd bytes */
151 	}
152 
153 	return (void*)rp;	/* Return allocated memory block (NULL:no memory to allocate) */
154 }
155 
156 /**
157  * @brief   Create de-quantization and prescaling tables with a DQT segment
158  *
159  * @param1   jd Pointer to the decompressor object
160  * @param2   * data  Pointer to the quantizer tables
161  * @param3   ndata Size of input data
162  * @return
163  *     - JDR_OK: succeed
164  *     - others: other errors.
165  */
create_qt_tbl(JDEC * jd,const uint8_t * data,uint16_t ndata)166 static JRESULT create_qt_tbl(JDEC* jd, const uint8_t* data, uint16_t ndata)
167 {
168 	uint16_t i;
169 	uint8_t d, z;
170 	int32_t *pb;
171 
172 
173 	while (ndata) {	/* Process all tables in the segment */
174 		if (ndata < 65) return JDR_FMT1;	/* Err: table size is unaligned */
175 		ndata -= 65;
176 		d = *data++;							/* Get table property */
177 		if (d & 0xF0) return JDR_FMT1;			/* Err: not 8-bit resolution */
178 		i = d & 3;								/* Get table ID */
179 		pb = alloc_pool(jd, 64 * sizeof (int32_t));/* Allocate a memory block for the table */
180 		if (!pb) return JDR_MEM1;				/* Err: not enough memory */
181 		jd->qttbl[i] = pb;						/* Register the table */
182 		for (i = 0; i < 64; i++) {				/* Load the table */
183 			z = ZIG(i);							/* Zigzag-order to raster-order conversion */
184 			pb[z] = (int32_t)((uint32_t)*data++ * IPSF(z));	/* Apply scale factor of Arai algorithm to the de-quantizers */
185 		}
186 	}
187 
188 	return JDR_OK;
189 }
190 
191 
192 /**
193  * @brief     Create huffman code tables with a DHT segment
194  *
195  * @param1   jd Pointer to the decompressor object
196  * @param2   * data Pointer to the packed huffman tables
197  * @param3   ndata Size of input data
198  * @return
199  *     - JDR_OK: succeed
200  *     - others: other errors.
201  */
create_huffman_tbl(JDEC * jd,const uint8_t * data,uint16_t ndata)202 static JRESULT create_huffman_tbl( JDEC* jd, const uint8_t* data, uint16_t ndata)
203 {
204 	uint16_t i, j, b, np, cls, num;
205 	uint8_t d, *pb, *pd;
206 	uint16_t hc, *ph;
207 
208 	while (ndata) {	/* Process all tables in the segment */
209 		if (ndata < 17) return JDR_FMT1;	/* Err: wrong data size */
210 		ndata -= 17;
211 		d = *data++;						/* Get table number and class */
212 		if (d & 0xEE) return JDR_FMT1;		/* Err: invalid class/number */
213 		cls = d >> 4; num = d & 0x0F;		/* class = dc(0)/ac(1), table number = 0/1 */
214 		pb = alloc_pool(jd, 16);			/* Allocate a memory block for the bit distribution table */
215 		if (!pb) return JDR_MEM1;			/* Err: not enough memory */
216 		jd->huffbits[num][cls] = pb;
217 		for (np = i = 0; i < 16; i++) {		/* Load number of patterns for 1 to 16-bit code */
218 			np += (pb[i] = *data++);		/* Get sum of code words for each code */
219 		}
220 		ph = alloc_pool(jd, (uint16_t)(np * sizeof (uint16_t)));/* Allocate a memory block for the code word table */
221 		if (!ph) return JDR_MEM1;			/* Err: not enough memory */
222 		jd->huffcode[num][cls] = ph;
223 		hc = 0;
224 		for (j = i = 0; i < 16; i++) {		/* Re-build huffman code word table */
225 			b = pb[i];
226 			while (b--) ph[j++] = hc++;
227 			hc <<= 1;
228 		}
229 
230 		if (ndata < np) return JDR_FMT1;	/* Err: wrong data size */
231 		ndata -= np;
232 		pd = alloc_pool(jd, np);			/* Allocate a memory block for the decoded data */
233 		if (!pd) return JDR_MEM1;			/* Err: not enough memory */
234 		jd->huffdata[num][cls] = pd;
235 		for (i = 0; i < np; i++) {			/* Load decoded data corresponds to each code ward */
236 			d = *data++;
237 			if (!cls && d > 11) return JDR_FMT1;
238 			*pd++ = d;
239 		}
240 	}
241 
242 	return JDR_OK;
243 }
244 
245 /**
246  * @brief    Analyze the JPEG image and Initialize decompressor object
247  *
248  * @param1   Blank decompressor object
249  * @param2   JPEG strem input function
250  * @param3   Working buffer for the decompression session
251  * @param4   Size of working buffer
252  * @param5   I/O device identifier for the session
253  * @return
254  *     - JDR_OK: succeed
255  *     - others: other errors.
256  */
jd_prepare(JDEC * jd,uint16_t (* infunc)(JDEC *,uint8_t *,uint16_t),void * pool,uint16_t sz_pool,void * dev)257 JRESULT jd_prepare (JDEC* jd, uint16_t (*infunc)(JDEC*, uint8_t*, uint16_t), void* pool,
258 					uint16_t sz_pool, void* dev)
259 {
260 	uint8_t *seg, b;
261 	uint16_t marker;
262 	uint32_t ofs;
263 	uint32_t pointer;
264 	uint16_t n, i, j, len;
265 	JRESULT rc;
266 	uint8_t skip_get_mark = 0;
267 
268 	if (!pool)
269 	{
270 		//os_printf("1--return = %d \r\n", JDR_PAR);
271 		return JDR_PAR;
272 	}
273 	jd->pool = pool;		/* Work memroy */
274 	jd->sz_pool = sz_pool;	/* Size of given work memory */
275 	jd->infunc = infunc;	/* Stream input function */
276 	jd->device = dev;		/* I/O device identifier */
277 	jd->nrst = 0;			/* No restart interval (default) */
278 
279 	for (i = 0; i < 2; i++)
280 	{	/* Nulls pointers */
281 		for (j = 0; j < 2; j++)
282 		{
283 			jd->huffbits[i][j] = 0;
284 			jd->huffcode[i][j] = 0;
285 			jd->huffdata[i][j] = 0;
286 		}
287 	}
288 	for (i = 0; i < 4; jd->qttbl[i++] = 0) ;
289 	jd->inbuf = seg = alloc_pool(jd, JD_SZBUF);		/* Allocate stream input buffer */
290 	if (!seg)
291 	{
292 		//os_printf("JDR_MEM1--return = %d \r\n", JDR_MEM1);
293 		return JDR_MEM1;
294 	}
295 	if (jd->infunc(jd, seg, 2) != 2)
296 	{
297 		//os_printf("JDR_INP--return = %d \r\n", JDR_INP);
298 		return JDR_INP;/* Check SOI marker */
299 	}
300 
301 	//os_printf("jd->inbuf = %x \r\n", *(jd->inbuf));
302 	//os_printf("jd->inbuf = %x \r\n", *(jd->inbuf + 1));
303 	//os_printf("jd->inbuf = %x \r\n", *(jd->inbuf + 2));
304 
305 	if (LDB_WORD(seg) != 0xFFD8)
306 	{
307 		//os_printf("LDB_WORD(seg) = %x \r\n", LDB_WORD(seg));
308 		return JDR_FMT1;	/* Err: SOI is not detected */
309 	}
310 
311 	ofs = 2;
312 	pointer = 2;
313 
314 	for (;;)
315 	{
316 		if(0 == skip_get_mark)
317 		{
318 			/* Get a JPEG marker */
319 			if (jd->infunc(jd, seg, 4) != 4)
320 			{
321 				//os_printf("6-----JDR_INP = %x \r\n", jd->infunc(jd, seg, 4));
322 				//os_printf("6-----JDR_INP = %x \r\n", JDR_INP);
323 				return JDR_INP;
324 			}
325 			marker = LDB_WORD(seg);		/* Marker */
326 			len = LDB_WORD(seg + 2);	/* Length field */
327 
328 			if((marker& 0xFF) != 0xFF) {
329 				//os_printf("6-----marker& 0xFF = %x \r\n", marker& 0xFF);
330 				pointer += 2 + len;
331 			}
332 			if (len <= 2 || (marker >> 8) != 0xFF) {
333 				//os_printf("6-----JDR_INP = %x \r\n", JDR_INP);
334 				return JDR_FMT1;
335 			}
336 			len -= 2;		/* Content size excluding length field */
337 			ofs += 4 + len;	/* Number of bytes loaded */
338 			BASE_FFDA = pointer;
339 		}
340 
341 		skip_get_mark = 0;
342 
343 		switch (marker & 0xFF) {
344 		case 0xC0:	/* SOF0 (baseline JPEG) */
345 			/* Load segment data */
346 
347 			if (len > JD_SZBUF)
348 				return JDR_MEM2;
349 			if (jd->infunc(jd, seg, len) != len)
350 			{
351 				return JDR_INP;
352 			}
353 
354 			jd->width = LDB_WORD(seg+3);		/* Image width in unit of pixel */
355 			jd->height = LDB_WORD(seg+1);		/* Image height in unit of pixel */
356 
357 			if (seg[5] != 3)
358 			{
359 				os_printf("7--JDR_FMT3-- = %x \r\n", JDR_FMT3);
360 				return JDR_FMT3;	/* Err: Supports only Y/Cb/Cr format */
361 			}
362 
363 			/* Check three image components */
364 			for (i = 0; i < 3; i++)
365 			{
366 				b = seg[7 + 3 * i];	/* Get sampling factor */
367 				if (!i)
368 				{	/* Y component */
369 					if (b != 0x11 && b != 0x22 && b != 0x21)
370 					{	/* Check sampling factor */
371 						return JDR_FMT3;/* Err: Supports only 4:4:4, 4:2:0 or 4:2:2 */
372 					}
373 					jd->msx = b >> 4; jd->msy = b & 15;/* Size of MCU [blocks] */
374 				}
375 				else
376 				{	/* Cb/Cr component */
377 					if (b != 0x11)
378 					{
379 						return JDR_FMT3;/* Err: Sampling factor of Cr/Cb must be 1 */
380 					}
381 				}
382 				b = seg[8 + 3 * i];	/* Get dequantizer table ID for this component */
383 				if (b > 3)
384 				{
385 					return JDR_FMT3;/* Err: Invalid ID */
386 				}
387 				jd->qtid[i] = b;
388 			}
389 			break;
390 
391 		case 0xDD:	/* DRI */
392 			/* Load segment data */
393 			if (len > JD_SZBUF) return JDR_MEM2;
394 			if (jd->infunc(jd, seg, len) != len)
395 			{
396 				return JDR_INP;
397 			}
398 
399 			/* Get restart interval (MCUs) */
400 			jd->nrst = LDB_WORD(seg);
401 			break;
402 
403 		case 0xC4:	/* DHT */
404 			/* Load segment data */
405 			if (len > JD_SZBUF)
406 				return JDR_MEM2;
407 			if (jd->infunc(jd, seg, len) != len)
408 			{
409 				return JDR_INP;
410 			}
411 
412 			/* Create huffman tables */
413 			rc = create_huffman_tbl(jd, seg, len);
414 			//os_printf("0xC4-----JPEGDEC_INTEN = %d \r\n", JPEGDEC_INTEN);
415 			if (rc)
416 				return rc;
417 			break;
418 
419 		case 0xDB:	/* DQT */
420 			/* Load segment data */
421 			if (len > JD_SZBUF)
422 				return JDR_MEM2;
423 			if (jd->infunc(jd, seg, len) != len)
424 				return JDR_INP;
425 
426 			/* Create de-quantizer tables */
427 			rc = create_qt_tbl(jd, seg, len);
428 			if (rc)
429 				return rc;
430 			break;
431 
432 		case 0xDA:	/* SOS */
433 			/* Load segment data */
434 			if (len > JD_SZBUF)
435 				return JDR_MEM2;
436 			if (jd->infunc(jd, seg, len) != len)
437 				return JDR_INP;
438 
439 			if (!jd->width || !jd->height)
440 				return JDR_FMT1;	/* Err: Invalid image size */
441 
442 			if (seg[0] != 3)
443 				return JDR_FMT3;				/* Err: Supports only three color components format */
444 
445 			/* Check if all tables corresponding to each components have been loaded */
446 			for (i = 0; i < 3; i++)
447 			{
448 				b = seg[2 + 2 * i];	/* Get huffman table ID */
449 				if (b != 0x00 && b != 0x11)	return JDR_FMT3;	/* Err: Different table number for DC/AC element */
450 				b = i ? 1 : 0;
451 				if (!jd->huffbits[b][0] || !jd->huffbits[b][1])
452 				{	/* Check dc/ac huffman table for this component */
453 					return JDR_FMT1;					/* Err: Nnot loaded */
454 				}
455 				if (!jd->qttbl[jd->qtid[i]])
456 				{			/* Check dequantizer table for this component */
457 					return JDR_FMT1;					/* Err: Not loaded */
458 				}
459 			}
460 
461 			/* Allocate working buffer for MCU and RGB */
462 			n = jd->msy * jd->msx;						/* Number of Y blocks in the MCU */
463 			if (!n) return JDR_FMT1;					/* Err: SOF0 has not been loaded */
464 			len = n * 64 * 2 + 64;						/* Allocate buffer for IDCT and RGB output */
465 			if (len < 256) len = 256;					/* but at least 256 byte is required for IDCT */
466 			jd->workbuf = alloc_pool(jd, len);			/* and it may occupy a part of following MCU working buffer for RGB output */
467 			if (!jd->workbuf) return JDR_MEM1;			/* Err: not enough memory */
468 			jd->mcubuf = (uint8_t*)alloc_pool(jd, (uint16_t)((n + 2) * 64));	/* Allocate MCU working buffer */
469 			if (!jd->mcubuf) return JDR_MEM1;			/* Err: not enough memory */
470 
471 			/* Pre-load the JPEG data to extract it from the bit stream */
472 			jd->dptr = seg; jd->dctr = 0; jd->dmsk = 0;	/* Prepare to read bit stream */
473 			if (ofs %= JD_SZBUF)
474 			{
475 			/* Align read offset to JD_SZBUF */
476 //				jd->dctr = jd->infunc(jd, seg + ofs, (uint16_t)(JD_SZBUF - ofs));
477 //				jd->dptr = seg + ofs - 1;
478 			}
479 
480 			return JDR_OK;		/* Initialization succeeded. Ready to decompress the JPEG image. */
481 
482 		case 0xC1:	/* SOF1 */
483 		case 0xC2:	/* SOF2 */
484 		case 0xC3:	/* SOF3 */
485 		case 0xC5:	/* SOF5 */
486 		case 0xC6:	/* SOF6 */
487 		case 0xC7:	/* SOF7 */
488 		case 0xC9:	/* SOF9 */
489 		case 0xCA:	/* SOF10 */
490 		case 0xCB:	/* SOF11 */
491 		case 0xCD:	/* SOF13 */
492 		case 0xCE:	/* SOF14 */
493 		case 0xCF:	/* SOF15 */
494 		case 0xD9:	/* EOI */
495 			return JDR_FMT3;	/* Unsuppoted JPEG standard (may be progressive JPEG) */
496 
497 		/*skip the first maker flag(0xff),treat the following 0xff as the flag*/
498 		case 0xFF:
499 			if(0xff != seg[2])
500 			{
501 				pointer += 3;
502 				BASE_FFDA = pointer;
503 				marker = seg[2];
504 				if(jd->infunc(jd, &seg[4], 1) != 1)
505 				{
506 					return JDR_INP;
507 				}
508 				len = LDB_WORD(&seg[3]);
509 				if (len <= 2 )
510 					return JDR_FMT1;
511 			}
512 			else
513 			{
514 				if(0xff != seg[3])
515 				{
516 					pointer += 4;
517 					BASE_FFDA = pointer;
518 					marker = seg[3];
519 					if(jd->infunc(jd, seg, 2) != 2)
520 						return JDR_INP;
521 				}
522 				else
523 				{
524 					pointer += 4;
525 					while(1)
526 					{
527 						if(jd->infunc(jd, seg, 1) != 1)
528 						{
529 							return JDR_INP;
530 						}
531 						if(0xff == seg[0])
532 						{
533 							pointer += 1;
534 							BASE_FFDA = pointer;
535 							continue;
536 						}
537 						marker = seg[0];
538 						pointer += 1;
539 						BASE_FFDA = pointer;
540 						if(jd->infunc(jd, seg, 2) != 2)
541 							return JDR_INP;
542 						break;
543 					}
544 				}
545 
546 				len = LDB_WORD(seg);
547 				pointer += len;
548 				BASE_FFDA = pointer;
549 				if (len <= 2 )
550 					return JDR_FMT1;
551 			}
552 			len -= 2;
553 			skip_get_mark = 1;
554 			break;
555 
556 		default:	/* Unknown segment (comment, exif or etc..) */
557 			/* Skip segment data */
558 			if (jd->infunc(jd, 0, len) != len)
559 			{	/* Null pointer specifies to skip bytes of stream */
560 					return JDR_INP;
561 			}
562 		}
563 	}
564 }
565 
jpg_dec_config(uint16_t xpixel,uint16_t ypixel,uint32_t length,unsigned char * input_buf,unsigned char * output_buf)566 int jpg_dec_config(uint16_t xpixel, uint16_t ypixel, uint32_t length, unsigned char *input_buf, unsigned char * output_buf)
567 {
568 	jpeg_dec_ll_set_reg0x58_value((uint32_t)input_buf);
569 	jpeg_dec_ll_set_reg0x59_value((uint32_t)output_buf);
570 	jpg_dec_st.inputbuf = input_buf;
571 
572 
573 	jpeg_dec_ll_set_reg0x5b_value(xpixel*ypixel*2);
574 	jpeg_dec_ll_set_reg0xf_value(xpixel*ypixel*2/64 - 1);
575 #if(1)
576 		jpeg_dec_ll_set_reg0x5a_value(length);
577 #else
578 			switch(xpixel)
579 		{
580 			case PIXEL_320:
581 				jpeg_dec_ll_set_reg0x5a_value(HVGA_RD_LEN);
582 				break;
583 			case PIXEL_480:
584 				jpeg_dec_ll_set_reg0x5a_value(V720P_RD_LEN);
585 				break;
586 			case PIXEL_640:
587 				jpeg_dec_ll_set_reg0x5a_value(VGA_RD_LEN);
588 				break;
589 			case PIXEL_1280:
590 				jpeg_dec_ll_set_reg0x5a_value(V720P_RD_LEN);
591 				break;
592 			case JPEGDEC_X_PIXEL_1920:
593 				jpeg_dec_ll_set_reg0x5a_value(V1080P_RD_LEN);
594 				break;
595 			default:
596 				jpeg_dec_ll_set_reg0x5a_value(V720P_RD_LEN);
597 				break;
598 		}
599 #endif
600 
601 	return 0;
602 }
jpeg_dec_block_int_en(bool auto_int_en)603 void jpeg_dec_block_int_en(bool auto_int_en)
604 {
605 	jpeg_dec_ll_set_reg0x2_jpeg_dec_auto(0);
606 //	jpeg_dec_ll_set_reg0x5e_dec_frame_int(auto_int_en);
607 	jpeg_dec_ll_set_reg0x5e_dec_totalbusy_int(1);
608 }
609 
jpeg_dec_auto_frame_end_int_en(bool auto_int_en)610 void jpeg_dec_auto_frame_end_int_en(bool auto_int_en)
611 {
612 	jpeg_dec_ll_set_reg0x2_jpeg_dec_auto(auto_int_en);
613 	jpeg_dec_ll_set_reg0x5e_dec_frame_int(auto_int_en);
614 	jpeg_dec_ll_set_reg0x2_jpeg_dec_linen(0);
615 	jpeg_dec_ll_set_reg0x2_jpeg_line_num(0);
616 }
617 
jpeg_dec_auto_line_num_int_en(bool line_int_en,uint16_t line_num)618 void jpeg_dec_auto_line_num_int_en(bool line_int_en, uint16_t line_num)
619 {
620 	jpeg_dec_ll_set_reg0x2_jpeg_dec_auto(line_int_en);
621 	jpeg_dec_ll_set_reg0x2_jpeg_dec_linen(line_int_en);
622 	jpeg_dec_ll_set_reg0x2_jpeg_line_num(line_num);
623 }
624 
625 
JpegdecInit(void)626 JRESULT JpegdecInit(void)
627 {
628 	int ret;
629 	uint32_t xs;
630 	uint32_t bits_num = 0, i;
631 	volatile unsigned long * huf_pointer;
632 	volatile unsigned long * zig_pointer;
633 	volatile unsigned long * dqt_pointer;
634 
635 	jpg_dec_st.rd_ptr = 0;//init rd pointer
636 	jpg_dec_st.jpg_file_size = 1024;
637 
638 	ret = jd_prepare(&jdec, jpeg_dec_input_func, jpg_dec_st.workbuf, WORK_AREA_SIZE, NULL);
639 	if(ret != JDR_OK) {
640 		os_printf("jd prepare error return %x \r\n", ret);
641 		return ret;
642 	}
643 
644 	huf_pointer = &REG_JPEG_TMP0;
645 	for(i = 0; i < 64; i = i + 1)
646 		*(huf_pointer + i) = 0;
647 
648 	REG_JPEG_BITS_DC00 = *((jdec).huffbits[0][0] + 0);
649 	REG_JPEG_BITS_DC01 = *((jdec).huffbits[0][0] + 1);
650 	REG_JPEG_BITS_DC02 = *((jdec).huffbits[0][0] + 2);
651 	REG_JPEG_BITS_DC03 = *((jdec).huffbits[0][0] + 3);
652 	REG_JPEG_BITS_DC04 = *((jdec).huffbits[0][0] + 4);
653 	REG_JPEG_BITS_DC05 = *((jdec).huffbits[0][0] + 5);
654 	REG_JPEG_BITS_DC06 = *((jdec).huffbits[0][0] + 6);
655 	REG_JPEG_BITS_DC07 = *((jdec).huffbits[0][0] + 7);
656 	REG_JPEG_BITS_DC08 = *((jdec).huffbits[0][0] + 8);
657 	REG_JPEG_BITS_DC09 = *((jdec).huffbits[0][0] + 9);
658 	REG_JPEG_BITS_DC0A = *((jdec).huffbits[0][0] + 10);
659 	REG_JPEG_BITS_DC0B = *((jdec).huffbits[0][0] + 11);
660 	REG_JPEG_BITS_DC0C = *((jdec).huffbits[0][0] + 12);
661 	REG_JPEG_BITS_DC0D = *((jdec).huffbits[0][0] + 13);
662 	REG_JPEG_BITS_DC0E = *((jdec).huffbits[0][0] + 14);
663 	REG_JPEG_BITS_DC0F = *((jdec).huffbits[0][0] + 15);
664 
665 	for(i = 0; i < 16; i = i + 1)
666 	{
667 		bits_num = bits_num + *((jdec).huffbits[0][0] + i);
668 	}
669 	//os_printf("13-----bits_num = %d \r\n", bits_num);
670 
671 	huf_pointer = &REG_JPEG_HUF_TABLE00;
672 	for(i = 0; i < bits_num; i = i + 1)
673 		*(huf_pointer + i) = (*((jdec).huffcode[0][0] + i) << 8) + *((jdec).huffdata[0][0] + i);
674 
675 	//ind 10
676 	REG_JPEG_BITS_DC10 = *((jdec).huffbits[1][0] + 0);
677 	REG_JPEG_BITS_DC11 = *((jdec).huffbits[1][0] + 1);
678 	REG_JPEG_BITS_DC12 = *((jdec).huffbits[1][0] + 2);
679 	REG_JPEG_BITS_DC13 = *((jdec).huffbits[1][0] + 3);
680 	REG_JPEG_BITS_DC14 = *((jdec).huffbits[1][0] + 4);
681 	REG_JPEG_BITS_DC15 = *((jdec).huffbits[1][0] + 5);
682 	REG_JPEG_BITS_DC16 = *((jdec).huffbits[1][0] + 6);
683 	REG_JPEG_BITS_DC17 = *((jdec).huffbits[1][0] + 7);
684 	REG_JPEG_BITS_DC18 = *((jdec).huffbits[1][0] + 8);
685 	REG_JPEG_BITS_DC19 = *((jdec).huffbits[1][0] + 9);
686 	REG_JPEG_BITS_DC1A = *((jdec).huffbits[1][0] + 10);
687 	REG_JPEG_BITS_DC1B = *((jdec).huffbits[1][0] + 11);
688 	REG_JPEG_BITS_DC1C = *((jdec).huffbits[1][0] + 12);
689 	REG_JPEG_BITS_DC1D = *((jdec).huffbits[1][0] + 13);
690 	REG_JPEG_BITS_DC1E = *((jdec).huffbits[1][0] + 14);
691 	REG_JPEG_BITS_DC1F = *((jdec).huffbits[1][0] + 15);
692 
693 	bits_num = 0;
694 	for(i = 0; i < 16; i = i + 1)
695 	{
696 		bits_num = bits_num + *((jdec).huffbits[1][0] + i);
697 		//addJPEG_DEC_Reg0x1 = bits_num;
698 	}
699 
700 	huf_pointer = &REG_JPEG_HUF_TABLE10;
701 	for(i = 0; i < bits_num; i = i + 1)
702 		*(huf_pointer + i) = (*((jdec).huffcode[1][0] + i) << 8) + *((jdec).huffdata[1][0] + i);
703 
704 	//ind 01
705 	REG_JPEG_BITS_AC00 = *((jdec).huffbits[0][1] + 0);
706 	REG_JPEG_BITS_AC01 = *((jdec).huffbits[0][1] + 1);
707 	REG_JPEG_BITS_AC02 = *((jdec).huffbits[0][1] + 2);
708 	REG_JPEG_BITS_AC03 = *((jdec).huffbits[0][1] + 3);
709 	REG_JPEG_BITS_AC04 = *((jdec).huffbits[0][1] + 4);
710 	REG_JPEG_BITS_AC05 = *((jdec).huffbits[0][1] + 5);
711 	REG_JPEG_BITS_AC06 = *((jdec).huffbits[0][1] + 6);
712 	REG_JPEG_BITS_AC07 = *((jdec).huffbits[0][1] + 7);
713 	REG_JPEG_BITS_AC08 = *((jdec).huffbits[0][1] + 8);
714 	REG_JPEG_BITS_AC09 = *((jdec).huffbits[0][1] + 9);
715 	REG_JPEG_BITS_AC0A = *((jdec).huffbits[0][1] + 10);
716 	REG_JPEG_BITS_AC0B = *((jdec).huffbits[0][1] + 11);
717 	REG_JPEG_BITS_AC0C = *((jdec).huffbits[0][1] + 12);
718 	REG_JPEG_BITS_AC0D = *((jdec).huffbits[0][1] + 13);
719 	REG_JPEG_BITS_AC0E = *((jdec).huffbits[0][1] + 14);
720 	REG_JPEG_BITS_AC0F = *((jdec).huffbits[0][1] + 15);
721 	bits_num = 0;
722 	for(i = 0; i < 16; i = i + 1)
723 	{
724 		bits_num = bits_num + *((jdec).huffbits[0][1] + i);
725 	}
726 
727 	huf_pointer = &REG_JPEG_HUF_TABLE01;
728 	for(i = 0; i < bits_num; i = i + 1)
729 		*(huf_pointer + i) = (*((jdec).huffcode[0][1] + i) << 8) + *((jdec).huffdata[0][1] + i);
730 
731 	//ind 11
732 	REG_JPEG_BITS_AC10 = *((jdec).huffbits[1][1] + 0);
733 	REG_JPEG_BITS_AC11 = *((jdec).huffbits[1][1] + 1);
734 	REG_JPEG_BITS_AC12 = *((jdec).huffbits[1][1] + 2);
735 	REG_JPEG_BITS_AC13 = *((jdec).huffbits[1][1] + 3);
736 	REG_JPEG_BITS_AC14 = *((jdec).huffbits[1][1] + 4);
737 	REG_JPEG_BITS_AC15 = *((jdec).huffbits[1][1] + 5);
738 	REG_JPEG_BITS_AC16 = *((jdec).huffbits[1][1] + 6);
739 	REG_JPEG_BITS_AC17 = *((jdec).huffbits[1][1] + 7);
740 	REG_JPEG_BITS_AC18 = *((jdec).huffbits[1][1] + 8);
741 	REG_JPEG_BITS_AC19 = *((jdec).huffbits[1][1] + 9);
742 	REG_JPEG_BITS_AC1A = *((jdec).huffbits[1][1] + 10);
743 	REG_JPEG_BITS_AC1B = *((jdec).huffbits[1][1] + 11);
744 	REG_JPEG_BITS_AC1C = *((jdec).huffbits[1][1] + 12);
745 	REG_JPEG_BITS_AC1D = *((jdec).huffbits[1][1] + 13);
746 	REG_JPEG_BITS_AC1E = *((jdec).huffbits[1][1] + 14);
747 	REG_JPEG_BITS_AC1F = *((jdec).huffbits[1][1] + 15);
748 	bits_num = 0;
749 	for(i = 0; i < 16; i = i + 1)
750 	{
751 	  bits_num = bits_num + *((jdec).huffbits[1][1] + i);
752 	}
753 
754 	huf_pointer = &REG_JPEG_HUF_TABLE11;
755 	for(i = 0; i < bits_num; i = i + 1)
756 		*(huf_pointer + i) = (*((jdec).huffcode[1][1] + i) << 8) + *((jdec).huffdata[1][1] + i);
757 
758 	dqt_pointer	= &REG_JPEG_DQT_TABLE0;
759 	for(i = 0; i < 64; i = i + 1)
760 		*(dqt_pointer + i) = (*((jdec).qttbl[0] + i));
761 
762 	dqt_pointer	= &REG_JPEG_DQT_TABLE1;
763 	for(i = 0; i < 64; i = i + 1)
764 		*(dqt_pointer + i) = (*((jdec).qttbl[1] + i));
765 
766 	zig_pointer	= &REG_JPEG_Zig;
767 	for(i = 0; i < 64; i = i + 1)
768 		*(zig_pointer + i) = (unsigned long)Zig[i];
769 
770 	xs = (jdec).width >> jpg_dec_st.scale_ratio;
771 	//ys = (*jdec).height >> jpg_dec_st.scale_ratio;
772 	jpg_dec_st.width = (jdec).width;
773 	jpg_dec_st.heigth = (jdec).height;
774 	jpg_dec_st.line_wbyte = xs;//only output Y
775 	return JDR_OK;
776 }
777 
778 
779 /**
780  * @brief	 Start to decompress the JPEG picture
781  *
782  * @param1	 jd Initialized decompression object
783  *
784  * @param2	 size of bytes to allocate
785  * @return
786  *	   - BK_OK: succeed
787  *	   - others: other errors.
788  */
jd_decomp(void)789 JRESULT jd_decomp(void)
790 {
791 	//uint16_t	mx, my;
792 	JRESULT rc;
793 
794 	jpeg_dec_ll_set_reg0x0_jpeg_dec_en(1);
795 	jpeg_dec_ll_set_reg0x5_mcu_x(0);
796 	jpeg_dec_ll_set_reg0x6_mcu_y(0);
797 	//mx = jd->msx * 8; my = jd->msy * 8;			/* Size of the MCU (pixel) */
798 	jpeg_dec_ll_set_reg0xa_value(jdec.width);  //pixel number X
799 	jpeg_dec_ll_set_reg0x8_dec_cmd(JPEGDEC_DC_CLEAR);
800 	jdec.dcv[2] = jdec.dcv[1] = jdec.dcv[0] = 0;	/* Initialize DC values */
801 	rc = JDR_OK;
802 	jpeg_dec_ll_set_reg0x8_dec_cmd(JPEGDEC_START);
803 	return rc;
804 }
805