blob: ca344d15871ca37e70e3a19a8a8df012a58390b8 [file] [log] [blame]
Chih-Wei Huang0d92eda2012-02-07 16:55:49 +08001/*
2 libcamera: An implementation of the library required by Android OS 3.2 so
3 it can access V4L2 devices as cameras.
4
Chih-Wei Huangd5590eb2019-02-26 17:48:45 +08005 (C) 2011 Eduardo José Tagle <ejtagle@tutopia.com>
Chih-Wei Huang0d92eda2012-02-07 16:55:49 +08006
7 Based on several packages:
8 - luvcview: Sdl video Usb Video Class grabber
9 (C) 2005,2006,2007 Laurent Pinchart && Michel Xhaard
10
11 - spcaview
12 (C) 2003,2004,2005,2006 Michel Xhaard
13
14 - JPEG decoder from http://www.bootsplash.org/
15 (C) August 2001 by Michael Schroeder, <mls@suse.de>
16
17 - libcamera V4L for Android 2.2
18 (C) 2009 0xlab.org - http://0xlab.org/
19 (C) 2010 SpectraCore Technologies
20 Author: Venkat Raju <codredruids@spectracoretech.com>
21 Based on a code from http://code.google.com/p/android-m912/downloads/detail?name=v4l2_camera_v2.patch
22
23 - guvcview: http://guvcview.berlios.de
24 Paulo Assis <pj.assis@gmail.com>
25 Nobuhiro Iwamatsu <iwamatsu@nigauri.org>
26
27 This program is free software: you can redistribute it and/or modify
28 it under the terms of the GNU General Public License as published by
29 the Free Software Foundation, either version 3 of the License, or
30 (at your option) any later version.
31
32 This program is distributed in the hope that it will be useful,
33 but WITHOUT ANY WARRANTY; without even the implied warranty of
34 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 GNU General Public License for more details.
36
37 You should have received a copy of the GNU General Public License
38 along with this program. If not, see <http://www.gnu.org/licenses/>.
39
40 */
41
42#include "Utils.h"
43#include <malloc.h>
44#include <string.h>
45
46#define LOG_TAG "CameraHardware"
47#include <utils/Log.h>
48
49/*clip value between 0 and 255*/
50#define CLIP(value) (uint8_t)(((value)>0xFF)?0xff:(((value)<0)?0:(value)))
51
52
53/*jpeg decoding 420 planar to 422
54* args:
55* out: pointer to data output of idct (macroblocks yyyy u v)
56* pic: pointer to picture buffer (yuyv)
57* stride: picture stride
58*/
59static void yuv420pto422(int * out,uint8_t *pic,int stride)
60{
61 int j, k;
62 uint8_t *pic0, *pic1;
63 int *outy, *outu, *outv;
64 int outy1 = 0;
65 int outy2 = 8;
66
67 //yyyyuv
68 pic0 = pic;
69 pic1 = pic + stride;
70 outy = out;
71 outu = out + 64 * 4;
72 outv = out + 64 * 5;
73 for (j = 0; j < 8; j++)
74 {
75 for (k = 0; k < 8; k++)
76 {
77 if( k == 4)
78 {
79 outy1 += 56;
80 outy2 += 56;
81 }
82 *pic0++ = CLIP(outy[outy1]); //y1 line 1
83 *pic0++ = CLIP(128 + *outu); //u line 1-2
84 *pic0++ = CLIP(outy[outy1+1]); //y2 line 1
85 *pic0++ = CLIP(128 + *outv); //v line 1-2
86 *pic1++ = CLIP(outy[outy2]); //y1 line 2
87 *pic1++ = CLIP(128 + *outu); //u line 1-2
88 *pic1++ = CLIP(outy[outy2+1]); //y2 line 2
89 *pic1++ = CLIP(128 + *outv); //v line 1-2
90 outy1 +=2; outy2 += 2; outu++; outv++;
91 }
92 if(j==3)
93 {
94 outy = out + 128;
95 }
96 else
97 {
98 outy += 16;
99 }
100 outy1 = 0;
101 outy2 = 8;
102 pic0 += 2 * (stride -16);
103 pic1 += 2 * (stride -16);
104 }
105}
106
107/*jpeg decoding 422 planar to 422
108* args:
109* out: pointer to data output of idct (macroblocks yyyy u v)
110* pic: pointer to picture buffer (yuyv)
111* stride: picture stride
112*/
113static void yuv422pto422(int * out,uint8_t *pic,int stride)
114{
115 int j, k;
116 uint8_t *pic0, *pic1;
117 int *outy, *outu, *outv;
118 int outy1 = 0;
119 int outy2 = 8;
120 int outu1 = 0;
121 int outv1 = 0;
122
123 //yyyyuv
124 pic0 = pic;
125 pic1 = pic + stride;
126 outy = out;
127 outu = out + 64 * 4;
128 outv = out + 64 * 5;
129 for (j = 0; j < 4; j++)
130 {
131 for (k = 0; k < 8; k++)
132 {
133 if( k == 4)
134 {
135 outy1 += 56;
136 outy2 += 56;
137 }
138 *pic0++ = CLIP(outy[outy1]); //y1 line 1
139 *pic0++ = CLIP(128 + outu[outu1]); //u line 1
140 *pic0++ = CLIP(outy[outy1+1]); //y2 line 1
141 *pic0++ = CLIP(128 + outv[outv1]); //v line 1
142 *pic1++ = CLIP(outy[outy2]); //y1 line 2
143 *pic1++ = CLIP(128 + outu[outu1+8]);//u line 2
144 *pic1++ = CLIP(outy[outy2+1]); //y2 line 2
145 *pic1++ = CLIP(128 + outv[outv1+8]);//v line 2
146 outv1 += 1; outu1 += 1;
147 outy1 +=2; outy2 +=2;
148 }
149 outy += 16;outu +=8; outv +=8;
150 outv1 = 0; outu1=0;
151 outy1 = 0;
152 outy2 = 8;
153 pic0 += 2 * (stride -16);
154 pic1 += 2 * (stride -16);
155 }
156}
157
158/*use in utils.c for jpeg decoding 444 planar to 422
159* args:
160* out: pointer to data output of idct (macroblocks yyyy u v)
161* pic: pointer to picture buffer (yuyv)
162* stride: picture stride
163*/
164static void yuv444pto422(int * out,uint8_t *pic,int stride)
165{
166 int j, k;
167 uint8_t *pic0, *pic1;
168 int *outy, *outu, *outv;
169 int outy1 = 0;
170 int outy2 = 8;
171 int outu1 = 0;
172 int outv1 = 0;
173
174 //yyyyuv
175 pic0 = pic;
176 pic1 = pic + stride;
177 outy = out;
178 outu = out + 64 * 4; // Ooops where did i invert ??
179 outv = out + 64 * 5;
180 for (j = 0; j < 4; j++)
181 {
182 for (k = 0; k < 4; k++)
183 {
184 *pic0++ =CLIP( outy[outy1]); //y1 line 1
185 *pic0++ =CLIP( 128 + outu[outu1]); //u line 1
186 *pic0++ =CLIP( outy[outy1+1]); //y2 line 1
187 *pic0++ =CLIP( 128 + outv[outv1]); //v line 1
188 *pic1++ =CLIP( outy[outy2]); //y1 line 2
189 *pic1++ =CLIP( 128 + outu[outu1+8]);//u line 2
190 *pic1++ =CLIP( outy[outy2+1]); //y2 line 2
191 *pic1++ =CLIP( 128 + outv[outv1+8]);//v line 2
192 outv1 += 2; outu1 += 2;
193 outy1 +=2; outy2 +=2;
194 }
195 outy += 16;outu +=16; outv +=16;
196 outv1 = 0; outu1=0;
197 outy1 = 0;
198 outy2 = 8;
199 pic0 += 2 * (stride -8);
200 pic1 += 2 * (stride -8);
201 }
202}
203
204/*use in utils.c for jpeg decoding 400 planar to 422
205* args:
206* out: pointer to data output of idct (macroblocks yyyy )
207* pic: pointer to picture buffer (yuyv)
208* stride: picture stride
209*/
210static void yuv400pto422(int * out,uint8_t *pic,int stride)
211{
212 int j, k;
213 uint8_t *pic0, *pic1;
214 int *outy ;
215 int outy1 = 0;
216 int outy2 = 8;
217 pic0 = pic;
218 pic1 = pic + stride;
219 outy = out;
220
221 //yyyy
222 for (j = 0; j < 4; j++)
223 {
224 for (k = 0; k < 4; k++)
225 {
226 *pic0++ = CLIP(outy[outy1]); //y1 line 1
227 *pic0++ = 128 ; //u
228 *pic0++ = CLIP(outy[outy1+1]);//y2 line 1
229 *pic0++ = 128 ; //v
230 *pic1++ = CLIP(outy[outy2]); //y1 line 2
231 *pic1++ = 128 ; //u
232 *pic1++ = CLIP(outy[outy2+1]);//y2 line 2
233 *pic1++ = 128 ; //v
234 outy1 +=2; outy2 +=2;
235 }
236 outy += 16;
237 outy1 = 0;
238 outy2 = 8;
239 pic0 += 2 * (stride -8);
240 pic1 += 2 * (stride -8);
241 }
242}
243
244
245#define JPG_HUFFMAN_TABLE_LENGTH 0x01A0
246
247static const unsigned char JPEGHuffmanTable[JPG_HUFFMAN_TABLE_LENGTH] =
248{
249 // luminance dc - length bits
250 0x00,
251 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00,
252 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
253 // luminance dc - code
254 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
255 0x0A, 0x0B,
256 // chrominance dc - length bits
257 0x01,
258 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
259 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
260 // chrominance dc - code
261 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
262 0x0A, 0x0B,
263 // luminance ac - number of codes with # bits (ordered by code length 1-16)
264 0x10,
265 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05,
266 0x04, 0x04, 0x00, 0x00, 0x01, 0x7D,
267 // luminance ac - run size (ordered by code length)
268 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31,
269 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32,
270 0x81, 0x91, 0xA1, 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52,
271 0xD1, 0xF0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16,
272 0x17, 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A,
273 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44, 0x45,
274 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57,
275 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
276 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x83,
277 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94,
278 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5,
279 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6,
280 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
281 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8,
282 0xD9, 0xDA, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8,
283 0xE9, 0xEA, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
284 0xF9, 0xFA,
285 // chrominance ac -number of codes with # bits (ordered by code length 1-16)
286 0x11,
287 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05,
288 0x04, 0x04, 0x00, 0x01, 0x02, 0x77,
289 // chrominance ac - run size (ordered by code length)
290 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06,
291 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81,
292 0x08, 0x14, 0x42, 0x91, 0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33,
293 0x52, 0xF0, 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16, 0x24, 0x34,
294 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26, 0x27, 0x28,
295 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
296 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56,
297 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
298 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A,
299 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92,
300 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3,
301 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4,
302 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5,
303 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6,
304 0xD7, 0xD8, 0xD9, 0xDA, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
305 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
306 0xF9, 0xFA
307};
308
309/* Fixed point arithmetic */
310//#define FIXED Sint32
311//#define FIXED_BITS 16
312//#define TO_FIXED(X) (((Sint32)(X))<<(FIXED_BITS))
313//#define FROM_FIXED(X) (((Sint32)(X))>>(FIXED_BITS))
314
315#define ISHIFT 11
316#define IFIX(a) ((int)((a) * (1 << ISHIFT) + .5))
317
318/* special markers */
319#define M_BADHUFF -1
320#define M_EOF 0x80
321
322struct jpeg_decdata
323{
324 int dcts[6 * 64 + 16];
325 int out[64 * 6];
326 int dquant[3][64];
327};
328
329struct in
330{
331 uint8_t *p;
332 uint32_t bits;
333 int left;
334 int marker;
335 int (*func) (void *);
336 void *data;
337};
338
339/*********************************/
340#define DECBITS 10 /* seems to be the optimum */
341
342struct dec_hufftbl
343{
344 int maxcode[17];
345 int valptr[16];
346 uint8_t vals[256];
347 uint32_t llvals[1 << DECBITS];
348};
349
350union hufftblp
351{
352 struct dec_hufftbl *dhuff;
353};
354
355struct scan
356{
357 int dc; /* old dc value */
358
359 union hufftblp hudc;
360 union hufftblp huac;
361 int next; /* when to switch to next scan */
362
363 int cid; /* component id */
364 int hv; /* horiz/vert, copied from comp */
365 int tq; /* quant tbl, copied from comp */
366};
367
368/******** Markers *********/
369#define M_SOI 0xd8
370#define M_APP0 0xe0
371#define M_DQT 0xdb
372#define M_SOF0 0xc0
373#define M_DHT 0xc4
374#define M_DRI 0xdd
375#define M_SOS 0xda
376#define M_RST0 0xd0
377#define M_EOI 0xd9
378#define M_COM 0xfe
379
380
381/*********************************/
382
383#undef PREC
384#define PREC int
385
386
387static int huffman_init(struct ctx* ctx);
388static void decode_mcus (struct in *, int *, int, struct scan *, int *);
389static int dec_readmarker (struct in *);
390static void dec_makehuff (struct dec_hufftbl *, int *, uint8_t *);
391static void setinput (struct in *, uint8_t *);
392static void idctqtab(uint8_t *, PREC *);
393inline static void idct(int *in, int *out, int *quant, long off, int max);
394static int fillbits (struct in *, int, unsigned int);
395static int dec_rec2 (struct in *, struct dec_hufftbl *, int *, int, int);
396
397
398typedef void (*ftopict) (int * out, uint8_t *pic, int width) ;
399
400/*********************************/
401
402struct comp
403{
404 int cid;
405 int hv;
406 int tq;
407};
408
409#define MAXCOMP 4
410struct jpginfo
411{
412 int nc; /* number of components */
413 int ns; /* number of scans */
414 int dri; /* restart interval */
415 int nm; /* mcus til next marker */
416 int rm; /* next restart marker */
417};
418
419struct ctx {
420 uint8_t *datap;
421 struct jpginfo info;
422 struct comp comps[MAXCOMP];
423 struct scan dscans[MAXCOMP];
424 uint8_t quant[4][64];
425 struct dec_hufftbl dhuff[4];
426 struct in in;
427};
428
429
430static inline int getbyte(struct ctx* ctx)
431{
432 return *ctx->datap++;
433}
434
435static int getword(struct ctx* ctx)
436{
437 int c1, c2;
438 c1 = *ctx->datap++;
439 c2 = *ctx->datap++;
440 return c1 << 8 | c2;
441}
442
443#define dec_huffdc (ctx.dhuff + 0)
444#define dec_huffac (ctx.dhuff + 2)
445
446/*read jpeg tables (huffman and quantization)
447* args:
448* till: Marker (frame - SOF0 scan - SOS)
449* isDHT: flag indicating the presence of huffman tables (if 0 must use default ones - MJPG frame)
450*/
451static int readtables(struct ctx* ctx,int till, int *isDHT)
452{
453 int m, l, i, j, lq, pq, tq;
454 int tc, th, tt;
455
456 for (;;)
457 {
458 if (getbyte(ctx)!= 0xff)
459 return -1;
460 if ((m = getbyte(ctx)) == till)
461 break;
462
463 switch (m)
464 {
465 case 0xc2:
466 return 0;
467 /*read quantization tables (Lqt and Cqt)*/
468 case M_DQT:
469 lq = getword(ctx);
470 while (lq > 2)
471 {
472 pq = getbyte(ctx);
473 /*Lqt=0x00 Cqt=0x01*/
474 tq = pq & 15;
475 if (tq > 3)
476 return -1;
477 pq >>= 4;
478 if (pq != 0)
479 return -1;
480 for (i = 0; i < 64; i++)
481 ctx->quant[tq][i] = getbyte(ctx);
482 lq -= 64 + 1;
483 }
484 break;
485 /*read huffman table*/
486 case M_DHT:
487 l = getword(ctx);
488 while (l > 2)
489 {
490 int hufflen[16], k;
491 uint8_t huffvals[256];
492
493 tc = getbyte(ctx);
494 th = tc & 15;
495 tc >>= 4;
496 tt = tc * 2 + th;
497 if (tc > 1 || th > 1)
498 return -1;
499
500 for (i = 0; i < 16; i++)
501 hufflen[i] = getbyte(ctx);
502 l -= 1 + 16;
503 k = 0;
504 for (i = 0; i < 16; i++)
505 {
506 for (j = 0; j < hufflen[i]; j++)
507 huffvals[k++] = getbyte(ctx);
508 l -= hufflen[i];
509 }
510 dec_makehuff(ctx->dhuff + tt, hufflen, huffvals);
511 }
512 /* has huffman tables defined (JPEG)*/
513 *isDHT= 1;
514 break;
515 /*restart interval*/
516 case M_DRI:
517 l = getword(ctx);
518 ctx->info.dri = getword(ctx);
519 break;
520
521 default:
522 l = getword(ctx);
523 while (l-- > 2)
524 getbyte(ctx);
525 break;
526 }
527 }
528 return 0;
529}
530
531static void dec_initscans(struct ctx* ctx)
532{
533 int i;
534
535 ctx->info.nm = ctx->info.dri + 1;
536 ctx->info.rm = M_RST0;
537 for (i = 0; i < ctx->info.ns; i++)
538 ctx->dscans[i].dc = 0;
539}
540
541static int dec_checkmarker(struct ctx* ctx)
542{
543 int i;
544
545 if (dec_readmarker(&ctx->in) != ctx->info.rm)
546 return -1;
547 ctx->info.nm = ctx->info.dri;
548 ctx->info.rm = (ctx->info.rm + 1) & ~0x08;
549 for (i = 0; i < ctx->info.ns; i++)
550 ctx->dscans[i].dc = 0;
551 return 0;
552}
553
554/*jpeg decode
555* args:
556* pic: pointer to picture data ( decoded image - yuyv format)
557* buf: pointer to input data ( compressed jpeg )
558* with: picture width
559* height: picture height
560*/
561int jpeg_decode(uint8_t *pic, int stride, uint8_t *buf, int width, int height)
562{
563 struct ctx ctx;
564 struct jpeg_decdata *decdata;
565 int i=0, j=0, m=0, tac=0, tdc=0;
566 int intwidth=0, intheight=0;
567 int mcusx=0, mcusy=0, mx=0, my=0;
568 int ypitch=0 ,xpitch=0,x=0,y=0;
569 int mb=0;
570 int max[6];
571 ftopict convert;
572 int err = 0;
573 int isInitHuffman = 0;
574 decdata = (struct jpeg_decdata*) calloc(1, sizeof(struct jpeg_decdata));
575
576 for(i=0;i<6;i++)
577 max[i]=0;
578
579 if (!decdata)
580 {
581 err = -1;
582 goto error;
583 }
584 if (buf == NULL)
585 {
586 err = -1;
587 goto error;
588 }
589 ctx.datap = buf;
590 /*check SOI (0xFFD8)*/
591 if (getbyte(&ctx) != 0xff)
592 {
593 err = ERR_NO_SOI;
594 goto error;
595 }
596 if (getbyte(&ctx) != M_SOI)
597 {
598 err = ERR_NO_SOI;
599 goto error;
600 }
601 /*read tables - if exist, up to start frame marker (0xFFC0)*/
602 if (readtables(&ctx,M_SOF0, &isInitHuffman))
603 {
604 err = ERR_BAD_TABLES;
605 goto error;
606 }
607 getword(&ctx); /*header lenght*/
608 i = getbyte(&ctx); /*precision (8 bit)*/
609 if (i != 8)
610 {
611 err = ERR_NOT_8BIT;
612 goto error;
613 }
614 intheight = getword(&ctx); /*height*/
615 intwidth = getword(&ctx); /*width */
616
617 if ((intheight & 7) || (intwidth & 7)) /*must be even*/
618 {
619 err = ERR_BAD_WIDTH_OR_HEIGHT;
620 goto error;
621 }
622 ctx.info.nc = getbyte(&ctx); /*number of components*/
623 if (ctx.info.nc > MAXCOMP)
624 {
625 err = ERR_TOO_MANY_COMPPS;
626 goto error;
627 }
628 /*for each component*/
629 for (i = 0; i < ctx.info.nc; i++)
630 {
631 int h, v;
632 ctx.comps[i].cid = getbyte(&ctx); /*component id*/
633 ctx.comps[i].hv = getbyte(&ctx);
634 v = ctx.comps[i].hv & 15; /*vertical sampling */
635 h = ctx.comps[i].hv >> 4; /*horizontal sampling */
636 ctx.comps[i].tq = getbyte(&ctx); /*quantization table used*/
637 if (h > 3 || v > 3)
638 {
639 err = ERR_ILLEGAL_HV;
640 goto error;
641 }
642 if (ctx.comps[i].tq > 3)
643 {
644 err = ERR_QUANT_TABLE_SELECTOR;
645 goto error;
646 }
647 }
648 /*read tables - if exist, up to start of scan marker (0xFFDA)*/
649 if (readtables(&ctx,M_SOS,&isInitHuffman))
650 {
651 err = ERR_BAD_TABLES;
652 goto error;
653 }
654 getword(&ctx); /* header lenght */
655 ctx.info.ns = getbyte(&ctx); /* number of scans */
656 if (!ctx.info.ns)
657 {
Chih-Wei Huangdf9613f2013-02-20 15:46:19 +0800658 ALOGE("info ns %d/n",ctx.info.ns);
Chih-Wei Huang0d92eda2012-02-07 16:55:49 +0800659 err = ERR_NOT_YCBCR_221111;
660 goto error;
661 }
662 /*for each scan*/
663 for (i = 0; i < ctx.info.ns; i++)
664 {
665 ctx.dscans[i].cid = getbyte(&ctx); /*component id*/
666 tdc = getbyte(&ctx);
667 tac = tdc & 15; /*ac table*/
668 tdc >>= 4; /*dc table*/
669 if (tdc > 1 || tac > 1)
670 {
671 err = ERR_QUANT_TABLE_SELECTOR;
672 goto error;
673 }
674 for (j = 0; j < ctx.info.nc; j++)
675 if (ctx.comps[j].cid == ctx.dscans[i].cid)
676 break;
677 if (j == ctx.info.nc)
678 {
679 err = ERR_UNKNOWN_CID_IN_SCAN;
680 goto error;
681 }
682 ctx.dscans[i].hv = ctx.comps[j].hv;
683 ctx.dscans[i].tq = ctx.comps[j].tq;
684 ctx.dscans[i].hudc.dhuff = dec_huffdc + tdc;
685 ctx.dscans[i].huac.dhuff = dec_huffac + tac;
686 }
687
688 i = getbyte(&ctx); /*0 */
689 j = getbyte(&ctx); /*63*/
690 m = getbyte(&ctx); /*0 */
691
692 if (i != 0 || j != 63 || m != 0)
693 {
Chih-Wei Huangdf9613f2013-02-20 15:46:19 +0800694 ALOGE("hmm FW error,not seq DCT ??\n");
Chih-Wei Huang0d92eda2012-02-07 16:55:49 +0800695 }
696
697 /*build huffman tables*/
698 if(!isInitHuffman)
699 {
700 if(huffman_init(&ctx) < 0)
701 return -ERR_BAD_TABLES;
702 }
703 /*
704 if (ctx->dscans[0].cid != 1 || ctx->dscans[1].cid != 2 || ctx->dscans[2].cid != 3)
705 {
706 err = ERR_NOT_YCBCR_221111;
707 goto error;
708 }
709
710 if (ctx->dscans[1].hv != 0x11 || ctx->dscans[2].hv != 0x11)
711 {
712 err = ERR_NOT_YCBCR_221111;
713 goto error;
714 }
715 */
716 /* if internal width and external are not the same or heigth too
717 and pic not allocated realloc the good size and mark the change
718 need 1 macroblock line more ?? */
719 if (intwidth > width || intheight > height)
720 {
721 return -ERR_BAD_WIDTH_OR_HEIGHT;
722#if 0
723 width = intwidth;
724 height = intheight;
725 // BytesperPixel 2 yuyv , 3 rgb24
726 *pic = (uint8_t*) realloc( *pic, intwidth * (intheight + 8) * 2);
727#endif
728 }
729
730 switch (ctx.dscans[0].hv)
731 {
732 case 0x22: // 411
733 mb=6;
734 mcusx = width >> 4;
735 mcusy = height >> 4;
736
737 xpitch = 16 * 2;
738
739 ypitch = 16 * stride;
740 convert = yuv420pto422; //choose the right conversion function
741 break;
742 case 0x21: //422
743 mb=4;
744 mcusx = width >> 4;
745 mcusy = height >> 3;
746
747 xpitch = 16 * 2;
748
749 ypitch = 8 * stride;
750 convert = yuv422pto422; //choose the right conversion function
751 break;
752 case 0x11: //444
753 mcusx = width >> 3;
754 mcusy = height >> 3;
755
756 xpitch = 8 * 2;
757
758 ypitch = 8 * stride;
759 if (ctx.info.ns==1)
760 {
761 mb = 1;
762 convert = yuv400pto422; //choose the right conversion function
763 }
764 else
765 {
766 mb=3;
767 convert = yuv444pto422; //choose the right conversion function
768 }
769 break;
770 default:
771 err = ERR_NOT_YCBCR_221111;
772 goto error;
773 break;
774 }
775
776 idctqtab(ctx.quant[ctx.dscans[0].tq], decdata->dquant[0]);
777 idctqtab(ctx.quant[ctx.dscans[1].tq], decdata->dquant[1]);
778 idctqtab(ctx.quant[ctx.dscans[2].tq], decdata->dquant[2]);
779 setinput(&ctx.in, ctx.datap);
780 dec_initscans(&ctx);
781
782 ctx.dscans[0].next = 2;
783 ctx.dscans[1].next = 1;
784 ctx.dscans[2].next = 0; /* 4xx encoding */
785 for (my = 0,y=0; my < mcusy; my++,y+=ypitch)
786 {
787 for (mx = 0,x=0; mx < mcusx; mx++,x+=xpitch)
788 {
789 if (ctx.info.dri && !--ctx.info.nm)
790 if (dec_checkmarker(&ctx))
791 {
792 err = ERR_WRONG_MARKER;
793 goto error;
794 }
795 switch (mb)
796 {
797 case 6:
798 decode_mcus(&ctx.in, decdata->dcts, mb, ctx.dscans, max);
799 idct(decdata->dcts, decdata->out, decdata->dquant[0],
800 IFIX(128.5), max[0]);
801 idct(decdata->dcts + 64, decdata->out + 64,
802 decdata->dquant[0], IFIX(128.5), max[1]);
803 idct(decdata->dcts + 128, decdata->out + 128,
804 decdata->dquant[0], IFIX(128.5), max[2]);
805 idct(decdata->dcts + 192, decdata->out + 192,
806 decdata->dquant[0], IFIX(128.5), max[3]);
807 idct(decdata->dcts + 256, decdata->out + 256,
808 decdata->dquant[1], IFIX(0.5), max[4]);
809 idct(decdata->dcts + 320, decdata->out + 320,
810 decdata->dquant[2], IFIX(0.5), max[5]);
811 break;
812
813 case 4:
814 decode_mcus(&ctx.in, decdata->dcts, mb, ctx.dscans, max);
815 idct(decdata->dcts, decdata->out, decdata->dquant[0],
816 IFIX(128.5), max[0]);
817 idct(decdata->dcts + 64, decdata->out + 64,
818 decdata->dquant[0], IFIX(128.5), max[1]);
819 idct(decdata->dcts + 128, decdata->out + 256,
820 decdata->dquant[1], IFIX(0.5), max[4]);
821 idct(decdata->dcts + 192, decdata->out + 320,
822 decdata->dquant[2], IFIX(0.5), max[5]);
823 break;
824
825 case 3:
826 decode_mcus(&ctx.in, decdata->dcts, mb, ctx.dscans, max);
827 idct(decdata->dcts, decdata->out, decdata->dquant[0],
828 IFIX(128.5), max[0]);
829 idct(decdata->dcts + 64, decdata->out + 256,
830 decdata->dquant[1], IFIX(0.5), max[4]);
831 idct(decdata->dcts + 128, decdata->out + 320,
832 decdata->dquant[2], IFIX(0.5), max[5]);
833 break;
834
835 case 1:
836 decode_mcus(&ctx.in, decdata->dcts, mb, ctx.dscans, max);
837 idct(decdata->dcts, decdata->out, decdata->dquant[0],
838 IFIX(128.5), max[0]);
839 break;
840 } // switch enc411
841 convert(decdata->out,pic+y+x,stride); //convert to 422
842 }
843 }
844
845 m = dec_readmarker(&ctx.in);
846 if (m != M_EOI)
847 {
848 err = ERR_NO_EOI;
849 goto error;
850 }
851 free(decdata);
852 return 0;
853error:
854 free(decdata);
855 return err;
856}
857
858/****************************************************************/
859/************** huffman decoder ***************/
860/****************************************************************/
861static int huffman_init(struct ctx* ctx)
862{
863 int tc, th, tt;
864 uint8_t *ptr= (uint8_t *) JPEGHuffmanTable ;
865 int i, j, l;
866 l = JPG_HUFFMAN_TABLE_LENGTH ;
867 while (l > 0)
868 {
869 int hufflen[16], k;
870 uint8_t huffvals[256];
871
872 tc = *ptr++;
873 th = tc & 15;
874 tc >>= 4;
875 tt = tc * 2 + th;
876 if (tc > 1 || th > 1)
877 return -ERR_BAD_TABLES;
878 for (i = 0; i < 16; i++)
879 hufflen[i] = *ptr++;
880 l -= 1 + 16;
881 k = 0;
882 for (i = 0; i < 16; i++)
883 {
884 for (j = 0; j < hufflen[i]; j++)
885 huffvals[k++] = *ptr++;
886 l -= hufflen[i];
887 }
888 dec_makehuff(ctx->dhuff + tt, hufflen, huffvals);
889 }
890 return 0;
891}
892
893
894static void setinput(struct in *in, uint8_t *p)
895{
896 in->p = p;
897 in->left = 0;
898 in->bits = 0;
899 in->marker = 0;
900}
901
902static int fillbits(struct in *in, int le, unsigned int bi)
903{
904 int b, m;
905
906 if (in->marker)
907 {
908 if (le <= 16)
909 in->bits = bi << 16, le += 16;
910 return le;
911 }
912 while (le <= 24)
913 {
914 b = *in->p++;
915 if (b == 0xff && (m = *in->p++) != 0)
916 {
917 if (m == M_EOF)
918 {
919 if (in->func && (m = in->func(in->data)) == 0)
920 continue;
921 }
922 in->marker = m;
923 if (le <= 16)
924 bi = bi << 16, le += 16;
925 break;
926 }
927 bi = bi << 8 | b;
928 le += 8;
929 }
930 in->bits = bi; /* tmp... 2 return values needed */
931 return le;
932}
933
934static int dec_readmarker(struct in *in)
935{
936 int m;
937
938 in->left = fillbits(in, in->left, in->bits);
939 if ((m = in->marker) == 0)
940 return 0;
941 in->left = 0;
942 in->marker = 0;
943 return m;
944}
945
946#define LEBI_DCL int le, bi
947#define LEBI_GET(in) (le = in->left, bi = in->bits)
948#define LEBI_PUT(in) (in->left = le, in->bits = bi)
949
950#define GETBITS(in, n) ( \
951 (le < (n) ? le = fillbits(in, le, bi), bi = in->bits : 0), \
952 (le -= (n)), \
953 bi >> le & ((1 << (n)) - 1) \
954)
955
956#define UNGETBITS(in, n) ( \
957 le += (n) \
958)
959
960
961static int dec_rec2(struct in *in, struct dec_hufftbl *hu, int *runp, int c, int i)
962{
963 LEBI_DCL;
964
965 LEBI_GET(in);
966 if (i)
967 {
968 UNGETBITS(in, i & 127);
969 *runp = i >> 8 & 15;
970 i >>= 16;
971 }
972 else
973 {
974 for (i = DECBITS;
975 (c = ((c << 1) | GETBITS(in, 1))) >= (hu->maxcode[i]); i++);
976 if (i >= 16)
977 {
978 in->marker = M_BADHUFF;
979 return 0;
980 }
981 i = hu->vals[hu->valptr[i] + c - hu->maxcode[i - 1] * 2];
982 *runp = i >> 4;
983 i &= 15;
984 }
985 if (i == 0)
986 { /* sigh, 0xf0 is 11 bit */
987 LEBI_PUT(in);
988 return 0;
989 }
990 /* receive part */
991 c = GETBITS(in, i);
992 if (c < (1 << (i - 1)))
993 c += (-1 << i) + 1;
994 LEBI_PUT(in);
995 return c;
996}
997
998#define DEC_REC(in, hu, r, i) ( \
999 r = GETBITS(in, DECBITS), \
1000 i = hu->llvals[r], \
1001 i & 128 ? \
1002 ( \
1003 UNGETBITS(in, i & 127), \
1004 r = i >> 8 & 15, \
1005 i >> 16 \
1006 ) \
1007 : \
1008 ( \
1009 LEBI_PUT(in), \
1010 i = dec_rec2(in, hu, &r, r, i), \
1011 LEBI_GET(in), \
1012 i \
1013 ) \
1014)
1015
1016static void decode_mcus(struct in *in, int *dct, int n, struct scan *sc ,int *maxp)
1017{
1018 struct dec_hufftbl *hu;
1019 int i = 0, r = 0, t = 0;
1020 LEBI_DCL;
1021
1022 memset(dct, 0, n * 64 * sizeof(*dct));
1023 LEBI_GET(in);
1024 while (n-- > 0)
1025 {
1026 hu = sc->hudc.dhuff;
1027 *dct++ = (sc->dc += DEC_REC(in, hu, r, t));
1028
1029 hu = sc->huac.dhuff;
1030 i = 63;
1031 while (i > 0)
1032 {
1033 t = DEC_REC(in, hu, r, t);
1034 if (t == 0 && r == 0)
1035 {
1036 dct += i;
1037 break;
1038 }
1039 dct += r;
1040 *dct++ = t;
1041 i -= r + 1;
1042 }
1043 *maxp++ = 64 - i;
1044 if (n == sc->next)
1045 sc++;
1046 }
1047 LEBI_PUT(in);
1048}
1049
1050static void dec_makehuff(struct dec_hufftbl *hu, int *hufflen, uint8_t *huffvals)
1051{
1052 int code, k, i, j, d, x, c, v;
1053 for (i = 0; i < (1 << DECBITS); i++)
1054 hu->llvals[i] = 0;
1055
1056 /*
1057 * llvals layout:
1058 *
1059 * value v already known, run r, backup u bits:
1060 * vvvvvvvvvvvvvvvv 0000 rrrr 1 uuuuuuu
1061 * value unknown, size b bits, run r, backup u bits:
1062 * 000000000000bbbb 0000 rrrr 0 uuuuuuu
1063 * value and size unknown:
1064 * 0000000000000000 0000 0000 0 0000000
1065 */
1066 code = 0;
1067 k = 0;
1068 for (i = 0; i < 16; i++, code <<= 1)
1069 { /* sizes */
1070 hu->valptr[i] = k;
1071 for (j = 0; j < hufflen[i]; j++)
1072 {
1073 hu->vals[k] = *huffvals++;
1074 if (i < DECBITS)
1075 {
1076 c = code << (DECBITS - 1 - i);
1077 v = hu->vals[k] & 0x0f; /* size */
1078 for (d = 1 << (DECBITS - 1 - i); --d >= 0;)
1079 {
1080 if (v + i < DECBITS)
1081 { /* both fit in table */
1082 x = d >> (DECBITS - 1 - v - i);
1083 if (v && x < (1 << (v - 1)))
1084 x += (-1 << v) + 1;
1085 x = x << 16 | (hu->vals[k] & 0xf0) << 4 |
1086 (DECBITS - (i + 1 + v)) | 128;
1087 }
1088 else
1089 x = v << 16 | (hu->vals[k] & 0xf0) << 4 |
1090 (DECBITS - (i + 1));
1091 hu->llvals[c | d] = x;
1092 }
1093 }
1094 code++;
1095 k++;
1096 }
1097 hu->maxcode[i] = code;
1098 }
1099 hu->maxcode[16] = 0x20000; /* always terminate decode */
1100}
1101
1102/****************************************************************/
1103/************** idct ***************/
1104/****************************************************************/
1105
1106#define IMULT(a, b) (((a) * (b)) >> ISHIFT)
1107#define ITOINT(a) ((a) >> ISHIFT)
1108
1109#define S22 ((PREC)IFIX(2 * 0.382683432))
1110#define C22 ((PREC)IFIX(2 * 0.923879532))
1111#define IC4 ((PREC)IFIX(1 / 0.707106781))
1112
1113//zigzag order used by idct
1114static uint8_t zig2[64] = {
1115 0, 2, 3, 9, 10, 20, 21, 35,
1116 14, 16, 25, 31, 39, 46, 50, 57,
1117 5, 7, 12, 18, 23, 33, 37, 48,
1118 27, 29, 41, 44, 52, 55, 59, 62,
1119 15, 26, 30, 40, 45, 51, 56, 58,
1120 1, 4, 8, 11, 19, 22, 34, 36,
1121 28, 42, 43, 53, 54, 60, 61, 63,
1122 6, 13, 17, 24, 32, 38, 47, 49
1123};
1124
1125/*inverse dct for jpeg decoding
1126* args:
1127* in: pointer to input data ( mcu - after huffman decoding)
1128* out: pointer to data with output of idct (to be filled)
1129* quant: pointer to quantization data tables
1130* off: offset value (128.5 or 0.5)
1131* max: maximum input mcu index?
1132*/
1133inline static void idct(int *in, int *out, int *quant, long off, int max)
1134{
1135 long t0, t1, t2, t3, t4, t5, t6, t7; // t ;
1136 long tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
1137 long tmp[64], *tmpp;
1138 int i, j, te;
1139 uint8_t *zig2p;
1140
1141 t0 = off;
1142 if (max == 1) //single color mcu
1143 {
1144 t0 += in[0] * quant[0]; //only DC available
1145 for (i = 0; i < 64; i++) // fill mcu with DC value
1146 out[i] = ITOINT(t0);
1147 return;
1148 }
1149 zig2p = zig2;
1150 tmpp = tmp;
1151 for (i = 0; i < 8; i++) //apply quantization table in zigzag order
1152 {
1153 j = *zig2p++;
1154 t0 += in[j] * (long) quant[j];
1155 j = *zig2p++;
1156 t5 = in[j] * (long) quant[j];
1157 j = *zig2p++;
1158 t2 = in[j] * (long) quant[j];
1159 j = *zig2p++;
1160 t7 = in[j] * (long) quant[j];
1161 j = *zig2p++;
1162 t1 = in[j] * (long) quant[j];
1163 j = *zig2p++;
1164 t4 = in[j] * (long) quant[j];
1165 j = *zig2p++;
1166 t3 = in[j] * (long) quant[j];
1167 j = *zig2p++;
1168 t6 = in[j] * (long) quant[j];
1169
1170
1171 if ((t1 | t2 | t3 | t4 | t5 | t6 | t7) == 0)
1172 {
1173 tmpp[0 * 8] = t0; //DC
1174 tmpp[1 * 8] = t0;
1175 tmpp[2 * 8] = t0;
1176 tmpp[3 * 8] = t0;
1177 tmpp[4 * 8] = t0;
1178 tmpp[5 * 8] = t0;
1179 tmpp[6 * 8] = t0;
1180 tmpp[7 * 8] = t0;
1181
1182 tmpp++;
1183 t0 = 0;
1184 continue;
1185 }
1186 //IDCT;
1187 tmp0 = t0 + t1;
1188 t1 = t0 - t1;
1189 tmp2 = t2 - t3;
1190 t3 = t2 + t3;
1191 tmp2 = IMULT(tmp2, IC4) - t3;
1192 tmp3 = tmp0 + t3;
1193 t3 = tmp0 - t3;
1194 tmp1 = t1 + tmp2;
1195 tmp2 = t1 - tmp2;
1196 tmp4 = t4 - t7;
1197 t7 = t4 + t7;
1198 tmp5 = t5 + t6;
1199 t6 = t5 - t6;
1200 tmp6 = tmp5 - t7;
1201 t7 = tmp5 + t7;
1202 tmp5 = IMULT(tmp6, IC4);
1203 tmp6 = IMULT((tmp4 + t6), S22);
1204 tmp4 = IMULT(tmp4, (C22 - S22)) + tmp6;
1205 t6 = IMULT(t6, (C22 + S22)) - tmp6;
1206 t6 = t6 - t7;
1207 t5 = tmp5 - t6;
1208 t4 = tmp4 - t5;
1209
1210 tmpp[0 * 8] = tmp3 + t7; //t0;
1211 tmpp[1 * 8] = tmp1 + t6; //t1;
1212 tmpp[2 * 8] = tmp2 + t5; //t2;
1213 tmpp[3 * 8] = t3 + t4; //t3;
1214 tmpp[4 * 8] = t3 - t4; //t4;
1215 tmpp[5 * 8] = tmp2 - t5; //t5;
1216 tmpp[6 * 8] = tmp1 - t6; //t6;
1217 tmpp[7 * 8] = tmp3 - t7; //t7;
1218 tmpp++;
1219 t0 = 0;
1220 }
1221 for (i = 0, j = 0; i < 8; i++)
1222 {
1223 t0 = tmp[j + 0];
1224 t1 = tmp[j + 1];
1225 t2 = tmp[j + 2];
1226 t3 = tmp[j + 3];
1227 t4 = tmp[j + 4];
1228 t5 = tmp[j + 5];
1229 t6 = tmp[j + 6];
1230 t7 = tmp[j + 7];
1231 if ((t1 | t2 | t3 | t4 | t5 | t6 | t7) == 0)
1232 {
1233 te = ITOINT(t0);
1234 out[j + 0] = te;
1235 out[j + 1] = te;
1236 out[j + 2] = te;
1237 out[j + 3] = te;
1238 out[j + 4] = te;
1239 out[j + 5] = te;
1240 out[j + 6] = te;
1241 out[j + 7] = te;
1242 j += 8;
1243 continue;
1244 }
1245 //IDCT;
1246 tmp0 = t0 + t1;
1247 t1 = t0 - t1;
1248 tmp2 = t2 - t3;
1249 t3 = t2 + t3;
1250 tmp2 = IMULT(tmp2, IC4) - t3;
1251 tmp3 = tmp0 + t3;
1252 t3 = tmp0 - t3;
1253 tmp1 = t1 + tmp2;
1254 tmp2 = t1 - tmp2;
1255 tmp4 = t4 - t7;
1256 t7 = t4 + t7;
1257 tmp5 = t5 + t6;
1258 t6 = t5 - t6;
1259 tmp6 = tmp5 - t7;
1260 t7 = tmp5 + t7;
1261 tmp5 = IMULT(tmp6, IC4);
1262 tmp6 = IMULT((tmp4 + t6), S22);
1263 tmp4 = IMULT(tmp4, (C22 - S22)) + tmp6;
1264 t6 = IMULT(t6, (C22 + S22)) - tmp6;
1265 t6 = t6 - t7;
1266 t5 = tmp5 - t6;
1267 t4 = tmp4 - t5;
1268
1269 out[j + 0] = ITOINT(tmp3 + t7);
1270 out[j + 1] = ITOINT(tmp1 + t6);
1271 out[j + 2] = ITOINT(tmp2 + t5);
1272 out[j + 3] = ITOINT(t3 + t4);
1273 out[j + 4] = ITOINT(t3 - t4);
1274 out[j + 5] = ITOINT(tmp2 - t5);
1275 out[j + 6] = ITOINT(tmp1 - t6);
1276 out[j + 7] = ITOINT(tmp3 - t7);
1277 j += 8;
1278 }
1279}
1280
1281static uint8_t zig[64] = {
1282 0, 1, 5, 6, 14, 15, 27, 28,
1283 2, 4, 7, 13, 16, 26, 29, 42,
1284 3, 8, 12, 17, 25, 30, 41, 43,
1285 9, 11, 18, 24, 31, 40, 44, 53,
1286 10, 19, 23, 32, 39, 45, 52, 54,
1287 20, 22, 33, 38, 46, 51, 55, 60,
1288 21, 34, 37, 47, 50, 56, 59, 61,
1289 35, 36, 48, 49, 57, 58, 62, 63
1290};
1291
1292//coef used in idct
1293static PREC aaidct[8] = {
1294 IFIX(0.3535533906), IFIX(0.4903926402),
1295 IFIX(0.4619397663), IFIX(0.4157348062),
1296 IFIX(0.3535533906), IFIX(0.2777851165),
1297 IFIX(0.1913417162), IFIX(0.0975451610)
1298};
1299
1300static void idctqtab(uint8_t *qin,PREC *qout)
1301{
1302 int i, j;
1303
1304 for (i = 0; i < 8; i++)
1305 for (j = 0; j < 8; j++)
1306 qout[zig[i * 8 + j]] = qin[zig[i * 8 + j]] *
1307 IMULT(aaidct[i], aaidct[j]);
1308}