A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 2349 lines 81 kB view raw
1/*************************************************************************** 2* __________ __ ___. 3* Open \______ \ ____ ____ | | _\_ |__ _______ ___ 4* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 5* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 6* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 7* \/ \/ \/ \/ \/ 8* $Id$ 9* 10* JPEG image viewer 11* (This is a real mess if it has to be coded in one single C file) 12* 13* Copyright (C) 2009 Andrew Mahone fractional decode, split IDCT - 16-point 14* IDCT based on IJG jpeg-7 pre-release 15* File scrolling addition (C) 2005 Alexander Spyridakis 16* Copyright (C) 2004 Jörg Hohensohn aka [IDC]Dragon 17* Heavily borrowed from the IJG implementation (C) Thomas G. Lane 18* Small & fast downscaling IDCT (C) 2002 by Guido Vollbeding JPEGclub.org 19* 20* This program is free software; you can redistribute it and/or 21* modify it under the terms of the GNU General Public License 22* as published by the Free Software Foundation; either version 2 23* of the License, or (at your option) any later version. 24* 25* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 26* KIND, either express or implied. 27* 28****************************************************************************/ 29#include "metadata_common.h" 30#include "plugin.h" 31#include "debug.h" 32#include "jpeg_load.h" 33/*#define JPEG_BS_DEBUG*/ 34//#define ROCKBOX_DEBUG_JPEG 35/* for portability of below JPEG code */ 36#define MEMSET(p,v,c) memset(p,v,c) 37#define MEMCPY(d,s,c) memcpy(d,s,c) 38#define INLINE static inline 39#define ENDIAN_SWAP16(n) n /* only for poor little endian machines */ 40#ifdef ROCKBOX_DEBUG_JPEG 41#define JDEBUGF DEBUGF 42#else 43#define JDEBUGF(...) 44#endif 45 46#if (__GNUC__ >= 6) 47#pragma GCC diagnostic push 48#pragma GCC diagnostic ignored "-Wshift-negative-value" 49#endif 50 51/**************** begin JPEG code ********************/ 52 53#ifdef HAVE_LCD_COLOR 54typedef struct uint8_rgb jpeg_pix_t; 55#else 56typedef uint8_t jpeg_pix_t; 57#endif 58#define JPEG_IDCT_TRANSPOSE 59#define JPEG_PIX_SZ (sizeof(jpeg_pix_t)) 60#ifdef HAVE_LCD_COLOR 61#define COLOR_EXTRA_IDCT_WS 64 62#else 63#define COLOR_EXTRA_IDCT_WS 0 64#endif 65#ifdef JPEG_IDCT_TRANSPOSE 66#define V_OUT(n) ws2[8*n] 67#define V_IN_ST 1 68#define TRANSPOSE_EXTRA_IDCT_WS 64 69#else 70#define V_OUT(n) ws[8*n] 71#define V_IN_ST 8 72#define TRANSPOSE_EXTRA_IDCT_WS 0 73#endif 74#define IDCT_WS_SIZE (64 + TRANSPOSE_EXTRA_IDCT_WS + COLOR_EXTRA_IDCT_WS) 75 76/* This can't be in jpeg_load.h because plugin.h includes it, and it conflicts 77 * with the definition in jpeg_decoder.h 78 */ 79struct jpeg 80{ 81#ifdef JPEG_FROM_MEM 82 unsigned char *data; 83#else 84 int fd; 85 int buf_left; 86 int buf_index; 87 88 int (*read_buf)(struct jpeg* p_jpeg, size_t count); 89 bool (*skip_bytes_seek)(struct jpeg* p_jpeg); 90 void* custom_param; 91#endif 92 unsigned long len; 93 unsigned long int bitbuf; 94 int bitbuf_bits; 95 int marker_ind; 96 int marker_val; 97 unsigned char marker; 98 int x_size, y_size; /* size of image (can be less than block boundary) */ 99 int x_phys, y_phys; /* physical size, block aligned */ 100 int x_mbl; /* x dimension of MBL */ 101 int y_mbl; /* y dimension of MBL */ 102 int blocks; /* blocks per MB */ 103 int restart_interval; /* number of MCUs between RSTm markers */ 104 int restart; /* blocks until next restart marker */ 105 int mcu_row; /* current row relative to first row of this row of MCUs */ 106 unsigned char *out_ptr; /* pointer to current row to output */ 107 int cur_row; /* current row relative to top of image */ 108 int set_rows; 109 int store_pos[4]; /* for Y block ordering */ 110#ifdef HAVE_LCD_COLOR 111 int last_dc_val[3]; 112 int h_scale[2]; /* horizontal scalefactor = (2**N) / 8 */ 113 int v_scale[2]; /* same as above, for vertical direction */ 114 int k_need[2]; /* per component zig-zag index of last needed coefficient */ 115 int zero_need[2]; /* per compenent number of coefficients to zero */ 116#else 117 int last_dc_val; 118 int h_scale[1]; /* horizontal scalefactor = (2**N) / 8 */ 119 int v_scale[1]; /* same as above, for vertical direction */ 120 int k_need[1]; /* per component zig-zag index of last needed coefficient */ 121 int zero_need[1]; /* per compenent number of coefficients to zero */ 122#endif 123 jpeg_pix_t *img_buf; 124 125 int16_t quanttable[4][QUANT_TABLE_LENGTH];/* raw quantization tables 0-3 */ 126 127 struct huffman_table hufftable[2]; /* Huffman tables */ 128 struct derived_tbl dc_derived_tbls[2]; /* Huffman-LUTs */ 129 struct derived_tbl ac_derived_tbls[2]; 130 131 struct frame_component frameheader[3]; /* Component descriptor */ 132 struct scan_component scanheader[3]; /* currently not used */ 133 134 int mcu_membership[6]; /* info per block */ 135 int tab_membership[6]; 136 int subsample_x[3]; /* info per component */ 137 int subsample_y[3]; 138 bool resize; 139 unsigned char buf[JPEG_READ_BUF_SIZE]; 140 struct img_part part; 141}; 142 143#ifdef JPEG_FROM_MEM 144static struct jpeg jpeg; 145#endif 146 147INLINE unsigned range_limit(int value) 148{ 149#if defined(CPU_COLDFIRE) 150 /* Note: Uses knowledge that only the low byte of the result is used */ 151 asm ( 152 "cmp.l #255,%[v] \n" /* overflow? */ 153 "bls.b 1f \n" /* no: return value */ 154 /* yes: set low byte to appropriate boundary */ 155 "spl.b %[v] \n" 156 "1: \n" 157 : /* outputs */ 158 [v]"+d"(value) 159 ); 160 return value; 161#elif defined(CPU_ARM) 162 /* Note: Uses knowledge that only the low byte of the result is used */ 163 asm ( 164 "cmp %[v], #255 \n" /* out of range 0..255? */ 165 "mvnhi %[v], %[v], asr #31 \n" /* yes: set all bits to ~(sign_bit) */ 166 : /* outputs */ 167 [v]"+r"(value) 168 ); 169 return value; 170#else 171 if ((unsigned)value <= 255) 172 return value; 173 174 if (value < 0) 175 return 0; 176 177 return 255; 178#endif 179} 180 181INLINE unsigned scale_output(int value) 182{ 183#if defined(CPU_ARM) && ARM_ARCH >= 6 184 asm ( 185 "usat %[v], #8, %[v], asr #18\n" 186 : [v] "+r" (value) 187 ); 188 return value; 189#else 190 return range_limit(value >> 18); 191#endif 192} 193 194/* IDCT implementation */ 195 196 197#define CONST_BITS 13 198#define PASS1_BITS 2 199 200 201/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus 202* causing a lot of useless floating-point operations at run time. 203* To get around this we use the following pre-calculated constants. 204* If you change CONST_BITS you may want to add appropriate values. 205* (With a reasonable C compiler, you can just rely on the FIX() macro...) 206*/ 207#define FIX_0_298631336 2446 /* FIX(0.298631336) */ 208#define FIX_0_390180644 3196 /* FIX(0.390180644) */ 209#define FIX_0_541196100 4433 /* FIX(0.541196100) */ 210#define FIX_0_765366865 6270 /* FIX(0.765366865) */ 211#define FIX_0_899976223 7373 /* FIX(0.899976223) */ 212#define FIX_1_175875602 9633 /* FIX(1.175875602) */ 213#define FIX_1_501321110 12299 /* FIX(1.501321110) */ 214#define FIX_1_847759065 15137 /* FIX(1.847759065) */ 215#define FIX_1_961570560 16069 /* FIX(1.961570560) */ 216#define FIX_2_053119869 16819 /* FIX(2.053119869) */ 217#define FIX_2_562915447 20995 /* FIX(2.562915447) */ 218#define FIX_3_072711026 25172 /* FIX(3.072711026) */ 219 220 221 222/* Multiply an long variable by an long constant to yield an long result. 223* For 8-bit samples with the recommended scaling, all the variable 224* and constant values involved are no more than 16 bits wide, so a 225* 16x16->32 bit multiply can be used instead of a full 32x32 multiply. 226* For 12-bit samples, a full 32-bit multiplication will be needed. 227*/ 228#define MULTIPLY(var1, var2) ((var1) * (var2)) 229 230#if defined(CPU_COLDFIRE) || \ 231 (defined(CPU_ARM) && ARM_ARCH > 4) 232#define MULTIPLY16(var,const) (((short) (var)) * ((short) (const))) 233#else 234#define MULTIPLY16 MULTIPLY 235#endif 236 237/* 238 * Macros for handling fixed-point arithmetic; these are used by many 239 * but not all of the DCT/IDCT modules. 240 * 241 * All values are expected to be of type INT32. 242 * Fractional constants are scaled left by CONST_BITS bits. 243 * CONST_BITS is defined within each module using these macros, 244 * and may differ from one module to the next. 245 */ 246#define ONE ((long)1) 247#define CONST_SCALE (ONE << CONST_BITS) 248 249/* Convert a positive real constant to an integer scaled by CONST_SCALE. 250 * Caution: some C compilers fail to reduce "FIX(constant)" at compile time, 251 * thus causing a lot of useless floating-point operations at run time. 252 */ 253#define FIX(x) ((long) ((x) * CONST_SCALE + 0.5)) 254#define RIGHT_SHIFT(x,shft) ((x) >> (shft)) 255 256/* Descale and correctly round an int value that's scaled by N bits. 257* We assume RIGHT_SHIFT rounds towards minus infinity, so adding 258* the fudge factor is correct for either sign of X. 259*/ 260#define DESCALE(x,n) (((x) + (1l << ((n)-1))) >> (n)) 261 262#define DS_OUT ((CONST_BITS)+(PASS1_BITS)+3) 263 264/* 265 * Conversion of full 0-255 range YCrCb to RGB: 266 * |R| |1.000000 -0.000001 1.402000| |Y'| 267 * |G| = |1.000000 -0.334136 -0.714136| |Pb| 268 * |B| |1.000000 1.772000 0.000000| |Pr| 269 * Scaled (yields s15-bit output): 270 * |R| |128 0 179| |Y | 271 * |G| = |128 -43 -91| |Cb - 128| 272 * |B| |128 227 0| |Cr - 128| 273 */ 274#define YFAC 128 275#define RVFAC 179 276#define GUFAC (-43) 277#define GVFAC (-91) 278#define BUFAC 227 279#define COMPONENT_SHIFT 15 280 281#ifndef CPU_ARM 282/* horizontal-pass 1-point IDCT */ 283static void jpeg_idct1h(int16_t *ws, unsigned char *out, int16_t *end, int rowstep) 284{ 285 for (; ws < end; ws += 8) 286 { 287 *out = range_limit(128 + (int) DESCALE(*ws, 3 + PASS1_BITS)); 288 out += rowstep; 289 } 290} 291 292/* vertical-pass 2-point IDCT */ 293static void jpeg_idct2v(int16_t *ws, int16_t *end) 294{ 295 for (; ws < end; ws++) 296 { 297 int tmp1 = ws[0*8]; 298 int tmp2 = ws[1*8]; 299 ws[0*8] = tmp1 + tmp2; 300 ws[1*8] = tmp1 - tmp2; 301 } 302} 303 304/* horizontal-pass 2-point IDCT */ 305static void jpeg_idct2h(int16_t *ws, unsigned char *out, int16_t *end, int rowstep) 306{ 307 for (; ws < end; ws += 8, out += rowstep) 308 { 309 int tmp1 = ws[0] + (ONE << (PASS1_BITS + 2)) 310 + (128 << (PASS1_BITS + 3)); 311 int tmp2 = ws[1]; 312 out[JPEG_PIX_SZ*0] = range_limit((int) RIGHT_SHIFT(tmp1 + tmp2, 313 PASS1_BITS + 3)); 314 out[JPEG_PIX_SZ*1] = range_limit((int) RIGHT_SHIFT(tmp1 - tmp2, 315 PASS1_BITS + 3)); 316 } 317} 318 319/* vertical-pass 4-point IDCT */ 320static void jpeg_idct4v(int16_t *ws, int16_t *end) 321{ 322 for (; ws < end; ws++) 323 { 324 int tmp0, tmp2, tmp10, tmp12; 325 int z1, z2, z3; 326 /* Even part */ 327 328 tmp0 = ws[8*0]; 329 tmp2 = ws[8*2]; 330 331 tmp10 = (tmp0 + tmp2) << PASS1_BITS; 332 tmp12 = (tmp0 - tmp2) << PASS1_BITS; 333 334 /* Odd part */ 335 /* Same rotation as in the even part of the 8x8 LL&M IDCT */ 336 337 z2 = ws[8*1]; 338 z3 = ws[8*3]; 339 340 z1 = MULTIPLY16(z2 + z3, FIX_0_541196100) + 341 (ONE << (CONST_BITS - PASS1_BITS - 1)); 342 tmp0 = RIGHT_SHIFT(z1 + MULTIPLY16(z3, - FIX_1_847759065), 343 CONST_BITS-PASS1_BITS); 344 tmp2 = RIGHT_SHIFT(z1 + MULTIPLY16(z2, FIX_0_765366865), 345 CONST_BITS-PASS1_BITS); 346 347 /* Final output stage */ 348 ws[8*0] = (int) (tmp10 + tmp2); 349 ws[8*3] = (int) (tmp10 - tmp2); 350 ws[8*1] = (int) (tmp12 + tmp0); 351 ws[8*2] = (int) (tmp12 - tmp0); 352 } 353} 354 355/* horizontal-pass 4-point IDCT */ 356static void jpeg_idct4h(int16_t *ws, unsigned char *out, int16_t *end, int rowstep) 357{ 358 for (; ws < end; out += rowstep, ws += 8) 359 { 360 int tmp0, tmp2, tmp10, tmp12; 361 int z1, z2, z3; 362 /* Even part */ 363 364 tmp0 = (int) ws[0] + (ONE << (PASS1_BITS + 2)) 365 + (128 << (PASS1_BITS + 3)); 366 tmp2 = (int) ws[2]; 367 368 tmp10 = (tmp0 + tmp2) << CONST_BITS; 369 tmp12 = (tmp0 - tmp2) << CONST_BITS; 370 371 /* Odd part */ 372 /* Same rotation as in the even part of the 8x8 LL&M IDCT */ 373 374 z2 = (int) ws[1]; 375 z3 = (int) ws[3]; 376 377 z1 = MULTIPLY16(z2 + z3, FIX_0_541196100); 378 tmp0 = z1 - MULTIPLY16(z3, FIX_1_847759065); 379 tmp2 = z1 + MULTIPLY16(z2, FIX_0_765366865); 380 381 /* Final output stage */ 382 383 out[JPEG_PIX_SZ*0] = range_limit((int) RIGHT_SHIFT(tmp10 + tmp2, 384 DS_OUT)); 385 out[JPEG_PIX_SZ*3] = range_limit((int) RIGHT_SHIFT(tmp10 - tmp2, 386 DS_OUT)); 387 out[JPEG_PIX_SZ*1] = range_limit((int) RIGHT_SHIFT(tmp12 + tmp0, 388 DS_OUT)); 389 out[JPEG_PIX_SZ*2] = range_limit((int) RIGHT_SHIFT(tmp12 - tmp0, 390 DS_OUT)); 391 } 392} 393 394/* vertical-pass 8-point IDCT */ 395static void jpeg_idct8v(int16_t *ws, int16_t *end) 396{ 397 long tmp0, tmp1, tmp2, tmp3; 398 long tmp10, tmp11, tmp12, tmp13; 399 long z1, z2, z3, z4, z5; 400#ifdef JPEG_IDCT_TRANSPOSE 401 int16_t *ws2 = ws + 64; 402 for (; ws < end; ws += 8, ws2++) 403 { 404#else 405 for (; ws < end; ws++) 406 { 407#endif 408 /* Due to quantization, we will usually find that many of the input 409 * coefficients are zero, especially the AC terms. We can exploit this 410 * by short-circuiting the IDCT calculation for any column in which all 411 * the AC terms are zero. In that case each output is equal to the 412 * DC coefficient (with scale factor as needed). 413 * With typical images and quantization tables, half or more of the 414 * column DCT calculations can be simplified this way. 415 */ 416 if ((ws[V_IN_ST*1] | ws[V_IN_ST*2] | ws[V_IN_ST*3] 417 | ws[V_IN_ST*4] | ws[V_IN_ST*5] | ws[V_IN_ST*6] | ws[V_IN_ST*7]) == 0) 418 { 419 /* AC terms all zero */ 420 int dcval = ws[V_IN_ST*0] << PASS1_BITS; 421 422 V_OUT(0) = V_OUT(1) = V_OUT(2) = V_OUT(3) = V_OUT(4) = V_OUT(5) = 423 V_OUT(6) = V_OUT(7) = dcval; 424 continue; 425 } 426 427 /* Even part: reverse the even part of the forward DCT. */ 428 /* The rotator is sqrt(2)*c(-6). */ 429 430 z2 = ws[V_IN_ST*2]; 431 z3 = ws[V_IN_ST*6]; 432 433 z1 = MULTIPLY16(z2 + z3, FIX_0_541196100); 434 tmp2 = z1 + MULTIPLY16(z3, - FIX_1_847759065); 435 tmp3 = z1 + MULTIPLY16(z2, FIX_0_765366865); 436 437 z2 = ws[V_IN_ST*0] << CONST_BITS; 438 z2 += ONE << (CONST_BITS - PASS1_BITS - 1); 439 z3 = ws[V_IN_ST*4] << CONST_BITS; 440 441 tmp0 = (z2 + z3); 442 tmp1 = (z2 - z3); 443 444 tmp10 = tmp0 + tmp3; 445 tmp13 = tmp0 - tmp3; 446 tmp11 = tmp1 + tmp2; 447 tmp12 = tmp1 - tmp2; 448 449 /* Odd part per figure 8; the matrix is unitary and hence its 450 transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. */ 451 452 tmp0 = ws[V_IN_ST*7]; 453 tmp1 = ws[V_IN_ST*5]; 454 tmp2 = ws[V_IN_ST*3]; 455 tmp3 = ws[V_IN_ST*1]; 456 457 z1 = tmp0 + tmp3; 458 z2 = tmp1 + tmp2; 459 z3 = tmp0 + tmp2; 460 z4 = tmp1 + tmp3; 461 z5 = MULTIPLY16(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */ 462 463 tmp0 = MULTIPLY16(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */ 464 tmp1 = MULTIPLY16(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */ 465 tmp2 = MULTIPLY16(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */ 466 tmp3 = MULTIPLY16(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */ 467 z1 = MULTIPLY16(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */ 468 z2 = MULTIPLY16(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */ 469 z3 = MULTIPLY16(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */ 470 z4 = MULTIPLY16(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */ 471 472 z3 += z5; 473 z4 += z5; 474 475 tmp0 += z1 + z3; 476 tmp1 += z2 + z4; 477 tmp2 += z2 + z3; 478 tmp3 += z1 + z4; 479 480 /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ 481 482 V_OUT(0) = (int) RIGHT_SHIFT(tmp10 + tmp3, CONST_BITS-PASS1_BITS); 483 V_OUT(7) = (int) RIGHT_SHIFT(tmp10 - tmp3, CONST_BITS-PASS1_BITS); 484 V_OUT(1) = (int) RIGHT_SHIFT(tmp11 + tmp2, CONST_BITS-PASS1_BITS); 485 V_OUT(6) = (int) RIGHT_SHIFT(tmp11 - tmp2, CONST_BITS-PASS1_BITS); 486 V_OUT(2) = (int) RIGHT_SHIFT(tmp12 + tmp1, CONST_BITS-PASS1_BITS); 487 V_OUT(5) = (int) RIGHT_SHIFT(tmp12 - tmp1, CONST_BITS-PASS1_BITS); 488 V_OUT(3) = (int) RIGHT_SHIFT(tmp13 + tmp0, CONST_BITS-PASS1_BITS); 489 V_OUT(4) = (int) RIGHT_SHIFT(tmp13 - tmp0, CONST_BITS-PASS1_BITS); 490 } 491} 492 493/* horizontal-pass 8-point IDCT */ 494static void jpeg_idct8h(int16_t *ws, unsigned char *out, int16_t *end, int rowstep) 495{ 496 long tmp0, tmp1, tmp2, tmp3; 497 long tmp10, tmp11, tmp12, tmp13; 498 long z1, z2, z3, z4, z5; 499 for (; ws < end; out += rowstep, ws += 8) 500 { 501 /* Rows of zeroes can be exploited in the same way as we did with 502 * columns. However, the column calculation has created many nonzero AC 503 * terms, so the simplification applies less often (typically 5% to 10% 504 * of the time). On machines with very fast multiplication, it's 505 * possible that the test takes more time than it's worth. In that 506 * case this section may be commented out. 507 */ 508 509#ifndef NO_ZERO_ROW_TEST 510 if ((ws[1] | ws[2] | ws[3] 511 | ws[4] | ws[5] | ws[6] | ws[7]) == 0) 512 { 513 /* AC terms all zero */ 514 unsigned char dcval = range_limit(128 + (int) DESCALE((long) ws[0], 515 PASS1_BITS+3)); 516 517 out[JPEG_PIX_SZ*0] = dcval; 518 out[JPEG_PIX_SZ*1] = dcval; 519 out[JPEG_PIX_SZ*2] = dcval; 520 out[JPEG_PIX_SZ*3] = dcval; 521 out[JPEG_PIX_SZ*4] = dcval; 522 out[JPEG_PIX_SZ*5] = dcval; 523 out[JPEG_PIX_SZ*6] = dcval; 524 out[JPEG_PIX_SZ*7] = dcval; 525 continue; 526 } 527#endif 528 529 /* Even part: reverse the even part of the forward DCT. */ 530 /* The rotator is sqrt(2)*c(-6). */ 531 532 z2 = (long) ws[2]; 533 z3 = (long) ws[6]; 534 535 z1 = MULTIPLY16(z2 + z3, FIX_0_541196100); 536 tmp2 = z1 + MULTIPLY16(z3, - FIX_1_847759065); 537 tmp3 = z1 + MULTIPLY16(z2, FIX_0_765366865); 538 539 z4 = (long) ws[0] + (ONE << (PASS1_BITS + 2)) 540 + (128 << (PASS1_BITS + 3)); 541 z4 <<= CONST_BITS; 542 z5 = (long) ws[4] << CONST_BITS; 543 tmp0 = z4 + z5; 544 tmp1 = z4 - z5; 545 546 tmp10 = tmp0 + tmp3; 547 tmp13 = tmp0 - tmp3; 548 tmp11 = tmp1 + tmp2; 549 tmp12 = tmp1 - tmp2; 550 551 /* Odd part per figure 8; the matrix is unitary and hence its 552 * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. */ 553 554 tmp0 = (long) ws[7]; 555 tmp1 = (long) ws[5]; 556 tmp2 = (long) ws[3]; 557 tmp3 = (long) ws[1]; 558 559 z1 = tmp0 + tmp3; 560 z2 = tmp1 + tmp2; 561 z3 = tmp0 + tmp2; 562 z4 = tmp1 + tmp3; 563 z5 = MULTIPLY16(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */ 564 565 tmp0 = MULTIPLY16(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */ 566 tmp1 = MULTIPLY16(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */ 567 tmp2 = MULTIPLY16(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */ 568 tmp3 = MULTIPLY16(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */ 569 z1 = MULTIPLY16(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */ 570 z2 = MULTIPLY16(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */ 571 z3 = MULTIPLY16(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */ 572 z4 = MULTIPLY16(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */ 573 574 z3 += z5; 575 z4 += z5; 576 577 tmp0 += z1 + z3; 578 tmp1 += z2 + z4; 579 tmp2 += z2 + z3; 580 tmp3 += z1 + z4; 581 582 /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ 583 584 out[JPEG_PIX_SZ*0] = range_limit((int) RIGHT_SHIFT(tmp10 + tmp3, 585 DS_OUT)); 586 out[JPEG_PIX_SZ*7] = range_limit((int) RIGHT_SHIFT(tmp10 - tmp3, 587 DS_OUT)); 588 out[JPEG_PIX_SZ*1] = range_limit((int) RIGHT_SHIFT(tmp11 + tmp2, 589 DS_OUT)); 590 out[JPEG_PIX_SZ*6] = range_limit((int) RIGHT_SHIFT(tmp11 - tmp2, 591 DS_OUT)); 592 out[JPEG_PIX_SZ*2] = range_limit((int) RIGHT_SHIFT(tmp12 + tmp1, 593 DS_OUT)); 594 out[JPEG_PIX_SZ*5] = range_limit((int) RIGHT_SHIFT(tmp12 - tmp1, 595 DS_OUT)); 596 out[JPEG_PIX_SZ*3] = range_limit((int) RIGHT_SHIFT(tmp13 + tmp0, 597 DS_OUT)); 598 out[JPEG_PIX_SZ*4] = range_limit((int) RIGHT_SHIFT(tmp13 - tmp0, 599 DS_OUT)); 600 } 601} 602 603#else 604extern void jpeg_idct1h(int16_t *ws, unsigned char *out, int16_t *end, int rowstep); 605extern void jpeg_idct2v(int16_t *ws, int16_t *end); 606extern void jpeg_idct2h(int16_t *ws, unsigned char *out, int16_t *end, int rowstep); 607extern void jpeg_idct4v(int16_t *ws, int16_t *end); 608extern void jpeg_idct4h(int16_t *ws, unsigned char *out, int16_t *end, int rowstep); 609extern void jpeg_idct8v(int16_t *ws, int16_t *end); 610extern void jpeg_idct8h(int16_t *ws, unsigned char *out, int16_t *end, int rowstep); 611#endif 612 613#ifdef HAVE_LCD_COLOR 614/* vertical-pass 16-point IDCT */ 615static void jpeg_idct16v(int16_t *ws, int16_t *end) 616{ 617 long tmp0, tmp1, tmp2, tmp3, tmp10, tmp11, tmp12, tmp13; 618 long tmp20, tmp21, tmp22, tmp23, tmp24, tmp25, tmp26, tmp27; 619 long z1, z2, z3, z4; 620#ifdef JPEG_IDCT_TRANSPOSE 621 int16_t *ws2 = ws + 64; 622 for (; ws < end; ws += 8, ws2++) 623 { 624#else 625 for (; ws < end; ws++) 626 { 627#endif 628 /* Even part */ 629 630 tmp0 = ws[V_IN_ST*0] << CONST_BITS; 631 /* Add fudge factor here for final descale. */ 632 tmp0 += 1 << (CONST_BITS-PASS1_BITS-1); 633 634 z1 = ws[V_IN_ST*4]; 635 tmp1 = MULTIPLY(z1, FIX(1.306562965)); /* c4[16] = c2[8] */ 636 tmp2 = MULTIPLY(z1, FIX_0_541196100); /* c12[16] = c6[8] */ 637 638 tmp10 = tmp0 + tmp1; 639 tmp11 = tmp0 - tmp1; 640 tmp12 = tmp0 + tmp2; 641 tmp13 = tmp0 - tmp2; 642 643 z1 = ws[V_IN_ST*2]; 644 z2 = ws[V_IN_ST*6]; 645 z3 = z1 - z2; 646 z4 = MULTIPLY(z3, FIX(0.275899379)); /* c14[16] = c7[8] */ 647 z3 = MULTIPLY(z3, FIX(1.387039845)); /* c2[16] = c1[8] */ 648 649 /* (c6+c2)[16] = (c3+c1)[8] */ 650 tmp0 = z3 + MULTIPLY(z2, FIX_2_562915447); 651 /* (c6-c14)[16] = (c3-c7)[8] */ 652 tmp1 = z4 + MULTIPLY(z1, FIX_0_899976223); 653 /* (c2-c10)[16] = (c1-c5)[8] */ 654 tmp2 = z3 - MULTIPLY(z1, FIX(0.601344887)); 655 /* (c10-c14)[16] = (c5-c7)[8] */ 656 tmp3 = z4 - MULTIPLY(z2, FIX(0.509795579)); 657 658 tmp20 = tmp10 + tmp0; 659 tmp27 = tmp10 - tmp0; 660 tmp21 = tmp12 + tmp1; 661 tmp26 = tmp12 - tmp1; 662 tmp22 = tmp13 + tmp2; 663 tmp25 = tmp13 - tmp2; 664 tmp23 = tmp11 + tmp3; 665 tmp24 = tmp11 - tmp3; 666 667 /* Odd part */ 668 669 z1 = ws[V_IN_ST*1]; 670 z2 = ws[V_IN_ST*3]; 671 z3 = ws[V_IN_ST*5]; 672 z4 = ws[V_IN_ST*7]; 673 674 tmp11 = z1 + z3; 675 676 tmp1 = MULTIPLY(z1 + z2, FIX(1.353318001)); /* c3 */ 677 tmp2 = MULTIPLY(tmp11, FIX(1.247225013)); /* c5 */ 678 tmp3 = MULTIPLY(z1 + z4, FIX(1.093201867)); /* c7 */ 679 tmp10 = MULTIPLY(z1 - z4, FIX(0.897167586)); /* c9 */ 680 tmp11 = MULTIPLY(tmp11, FIX(0.666655658)); /* c11 */ 681 tmp12 = MULTIPLY(z1 - z2, FIX(0.410524528)); /* c13 */ 682 tmp0 = tmp1 + tmp2 + tmp3 - 683 MULTIPLY(z1, FIX(2.286341144)); /* c7+c5+c3-c1 */ 684 tmp13 = tmp10 + tmp11 + tmp12 - 685 MULTIPLY(z1, FIX(1.835730603)); /* c9+c11+c13-c15 */ 686 z1 = MULTIPLY(z2 + z3, FIX(0.138617169)); /* c15 */ 687 tmp1 += z1 + MULTIPLY(z2, FIX(0.071888074)); /* c9+c11-c3-c15 */ 688 tmp2 += z1 - MULTIPLY(z3, FIX(1.125726048)); /* c5+c7+c15-c3 */ 689 z1 = MULTIPLY(z3 - z2, FIX(1.407403738)); /* c1 */ 690 tmp11 += z1 - MULTIPLY(z3, FIX(0.766367282)); /* c1+c11-c9-c13 */ 691 tmp12 += z1 + MULTIPLY(z2, FIX(1.971951411)); /* c1+c5+c13-c7 */ 692 z2 += z4; 693 z1 = MULTIPLY(z2, - FIX(0.666655658)); /* -c11 */ 694 tmp1 += z1; 695 tmp3 += z1 + MULTIPLY(z4, FIX(1.065388962)); /* c3+c11+c15-c7 */ 696 z2 = MULTIPLY(z2, - FIX(1.247225013)); /* -c5 */ 697 tmp10 += z2 + MULTIPLY(z4, FIX(3.141271809)); /* c1+c5+c9-c13 */ 698 tmp12 += z2; 699 z2 = MULTIPLY(z3 + z4, - FIX(1.353318001)); /* -c3 */ 700 tmp2 += z2; 701 tmp3 += z2; 702 z2 = MULTIPLY(z4 - z3, FIX(0.410524528)); /* c13 */ 703 tmp10 += z2; 704 tmp11 += z2; 705 706 /* Final output stage */ 707 V_OUT(0) = (int) RIGHT_SHIFT(tmp20 + tmp0, CONST_BITS-PASS1_BITS); 708 V_OUT(15) = (int) RIGHT_SHIFT(tmp20 - tmp0, CONST_BITS-PASS1_BITS); 709 V_OUT(1) = (int) RIGHT_SHIFT(tmp21 + tmp1, CONST_BITS-PASS1_BITS); 710 V_OUT(14) = (int) RIGHT_SHIFT(tmp21 - tmp1, CONST_BITS-PASS1_BITS); 711 V_OUT(2) = (int) RIGHT_SHIFT(tmp22 + tmp2, CONST_BITS-PASS1_BITS); 712 V_OUT(13) = (int) RIGHT_SHIFT(tmp22 - tmp2, CONST_BITS-PASS1_BITS); 713 V_OUT(3) = (int) RIGHT_SHIFT(tmp23 + tmp3, CONST_BITS-PASS1_BITS); 714 V_OUT(12) = (int) RIGHT_SHIFT(tmp23 - tmp3, CONST_BITS-PASS1_BITS); 715 V_OUT(4) = (int) RIGHT_SHIFT(tmp24 + tmp10, CONST_BITS-PASS1_BITS); 716 V_OUT(11) = (int) RIGHT_SHIFT(tmp24 - tmp10, CONST_BITS-PASS1_BITS); 717 V_OUT(5) = (int) RIGHT_SHIFT(tmp25 + tmp11, CONST_BITS-PASS1_BITS); 718 V_OUT(10) = (int) RIGHT_SHIFT(tmp25 - tmp11, CONST_BITS-PASS1_BITS); 719 V_OUT(6) = (int) RIGHT_SHIFT(tmp26 + tmp12, CONST_BITS-PASS1_BITS); 720 V_OUT(9) = (int) RIGHT_SHIFT(tmp26 - tmp12, CONST_BITS-PASS1_BITS); 721 V_OUT(7) = (int) RIGHT_SHIFT(tmp27 + tmp13, CONST_BITS-PASS1_BITS); 722 V_OUT(8) = (int) RIGHT_SHIFT(tmp27 - tmp13, CONST_BITS-PASS1_BITS); 723 } 724} 725 726/* horizontal-pass 16-point IDCT */ 727static void jpeg_idct16h(int16_t *ws, unsigned char *out, int16_t *end, int rowstep) 728{ 729 long tmp0, tmp1, tmp2, tmp3, tmp10, tmp11, tmp12, tmp13; 730 long tmp20, tmp21, tmp22, tmp23, tmp24, tmp25, tmp26, tmp27; 731 long z1, z2, z3, z4; 732 for (; ws < end; out += rowstep, ws += 8) 733 { 734 /* Even part */ 735 736 /* Add fudge factor here for final descale. */ 737 tmp0 = (long) ws[0] + (ONE << (PASS1_BITS+2)) 738 + (128 << (PASS1_BITS + 3)); 739 tmp0 <<= CONST_BITS; 740 741 z1 = (long) ws[4]; 742 tmp1 = MULTIPLY(z1, FIX(1.306562965)); /* c4[16] = c2[8] */ 743 tmp2 = MULTIPLY(z1, FIX_0_541196100); /* c12[16] = c6[8] */ 744 745 tmp10 = tmp0 + tmp1; 746 tmp11 = tmp0 - tmp1; 747 tmp12 = tmp0 + tmp2; 748 tmp13 = tmp0 - tmp2; 749 750 z1 = (long) ws[2]; 751 z2 = (long) ws[6]; 752 z3 = z1 - z2; 753 z4 = MULTIPLY(z3, FIX(0.275899379)); /* c14[16] = c7[8] */ 754 z3 = MULTIPLY(z3, FIX(1.387039845)); /* c2[16] = c1[8] */ 755 756 /* (c6+c2)[16] = (c3+c1)[8] */ 757 tmp0 = z3 + MULTIPLY(z2, FIX_2_562915447); 758 /* (c6-c14)[16] = (c3-c7)[8] */ 759 tmp1 = z4 + MULTIPLY(z1, FIX_0_899976223); 760 /* (c2-c10)[16] = (c1-c5)[8] */ 761 tmp2 = z3 - MULTIPLY(z1, FIX(0.601344887)); 762 /* (c10-c14)[16] = (c5-c7)[8] */ 763 tmp3 = z4 - MULTIPLY(z2, FIX(0.509795579)); 764 765 tmp20 = tmp10 + tmp0; 766 tmp27 = tmp10 - tmp0; 767 tmp21 = tmp12 + tmp1; 768 tmp26 = tmp12 - tmp1; 769 tmp22 = tmp13 + tmp2; 770 tmp25 = tmp13 - tmp2; 771 tmp23 = tmp11 + tmp3; 772 tmp24 = tmp11 - tmp3; 773 774 /* Odd part */ 775 776 z1 = (long) ws[1]; 777 z2 = (long) ws[3]; 778 z3 = (long) ws[5]; 779 z4 = (long) ws[7]; 780 781 tmp11 = z1 + z3; 782 783 tmp1 = MULTIPLY(z1 + z2, FIX(1.353318001)); /* c3 */ 784 tmp2 = MULTIPLY(tmp11, FIX(1.247225013)); /* c5 */ 785 tmp3 = MULTIPLY(z1 + z4, FIX(1.093201867)); /* c7 */ 786 tmp10 = MULTIPLY(z1 - z4, FIX(0.897167586)); /* c9 */ 787 tmp11 = MULTIPLY(tmp11, FIX(0.666655658)); /* c11 */ 788 tmp12 = MULTIPLY(z1 - z2, FIX(0.410524528)); /* c13 */ 789 tmp0 = tmp1 + tmp2 + tmp3 - 790 MULTIPLY(z1, FIX(2.286341144)); /* c7+c5+c3-c1 */ 791 tmp13 = tmp10 + tmp11 + tmp12 - 792 MULTIPLY(z1, FIX(1.835730603)); /* c9+c11+c13-c15 */ 793 z1 = MULTIPLY(z2 + z3, FIX(0.138617169)); /* c15 */ 794 tmp1 += z1 + MULTIPLY(z2, FIX(0.071888074)); /* c9+c11-c3-c15 */ 795 tmp2 += z1 - MULTIPLY(z3, FIX(1.125726048)); /* c5+c7+c15-c3 */ 796 z1 = MULTIPLY(z3 - z2, FIX(1.407403738)); /* c1 */ 797 tmp11 += z1 - MULTIPLY(z3, FIX(0.766367282)); /* c1+c11-c9-c13 */ 798 tmp12 += z1 + MULTIPLY(z2, FIX(1.971951411)); /* c1+c5+c13-c7 */ 799 z2 += z4; 800 z1 = MULTIPLY(z2, - FIX(0.666655658)); /* -c11 */ 801 tmp1 += z1; 802 tmp3 += z1 + MULTIPLY(z4, FIX(1.065388962)); /* c3+c11+c15-c7 */ 803 z2 = MULTIPLY(z2, - FIX(1.247225013)); /* -c5 */ 804 tmp10 += z2 + MULTIPLY(z4, FIX(3.141271809)); /* c1+c5+c9-c13 */ 805 tmp12 += z2; 806 z2 = MULTIPLY(z3 + z4, - FIX(1.353318001)); /* -c3 */ 807 tmp2 += z2; 808 tmp3 += z2; 809 z2 = MULTIPLY(z4 - z3, FIX(0.410524528)); /* c13 */ 810 tmp10 += z2; 811 tmp11 += z2; 812 813 /* Final output stage */ 814 815 out[JPEG_PIX_SZ*0] = scale_output(tmp20 + tmp0); 816 out[JPEG_PIX_SZ*15] = scale_output(tmp20 - tmp0); 817 out[JPEG_PIX_SZ*1] = scale_output(tmp21 + tmp1); 818 out[JPEG_PIX_SZ*14] = scale_output(tmp21 - tmp1); 819 out[JPEG_PIX_SZ*2] = scale_output(tmp22 + tmp2); 820 out[JPEG_PIX_SZ*13] = scale_output(tmp22 - tmp2); 821 out[JPEG_PIX_SZ*3] = scale_output(tmp23 + tmp3); 822 out[JPEG_PIX_SZ*12] = scale_output(tmp23 - tmp3); 823 out[JPEG_PIX_SZ*4] = scale_output(tmp24 + tmp10); 824 out[JPEG_PIX_SZ*11] = scale_output(tmp24 - tmp10); 825 out[JPEG_PIX_SZ*5] = scale_output(tmp25 + tmp11); 826 out[JPEG_PIX_SZ*10] = scale_output(tmp25 - tmp11); 827 out[JPEG_PIX_SZ*6] = scale_output(tmp26 + tmp12); 828 out[JPEG_PIX_SZ*9] = scale_output(tmp26 - tmp12); 829 out[JPEG_PIX_SZ*7] = scale_output(tmp27 + tmp13); 830 out[JPEG_PIX_SZ*8] = scale_output(tmp27 - tmp13); 831 } 832} 833#endif 834 835struct idct_entry { 836 int scale; 837 void (*v_idct)(int16_t *ws, int16_t *end); 838 void (*h_idct)(int16_t *ws, unsigned char *out, int16_t *end, int rowstep); 839}; 840 841static const struct idct_entry idct_tbl[] = { 842 { PASS1_BITS, NULL, jpeg_idct1h }, 843 { PASS1_BITS, jpeg_idct2v, jpeg_idct2h }, 844 { 0, jpeg_idct4v, jpeg_idct4h }, 845 { 0, jpeg_idct8v, jpeg_idct8h }, 846#ifdef HAVE_LCD_COLOR 847 { 0, jpeg_idct16v, jpeg_idct16h }, 848#endif 849}; 850 851/* JPEG decoder implementation */ 852 853#ifdef JPEG_FROM_MEM 854INLINE unsigned char *jpeg_getc(struct jpeg* p_jpeg) 855{ 856 if (LIKELY(p_jpeg->len)) 857 { 858 p_jpeg->len--; 859 return p_jpeg->data++; 860 } else 861 return NULL; 862} 863 864INLINE bool skip_bytes(struct jpeg* p_jpeg, int count) 865{ 866 if (p_jpeg->len >= (unsigned)count) 867 { 868 p_jpeg->len -= count; 869 p_jpeg->data += count; 870 return true; 871 } else { 872 p_jpeg->data += p_jpeg->len; 873 p_jpeg->len = 0; 874 return false; 875 } 876} 877 878INLINE void jpeg_putc(struct jpeg* p_jpeg) 879{ 880 p_jpeg->len++; 881 p_jpeg->data--; 882} 883#else 884 885static int read_buf(struct jpeg* p_jpeg, size_t count) 886{ 887 return read(p_jpeg->fd, p_jpeg->buf, count); 888} 889 890INLINE void fill_buf(struct jpeg* p_jpeg) 891{ 892 p_jpeg->buf_left = p_jpeg->read_buf(p_jpeg, MIN(JPEG_READ_BUF_SIZE, p_jpeg->len)); 893 p_jpeg->buf_index = 0; 894 if (p_jpeg->buf_left > 0) 895 p_jpeg->len -= p_jpeg->buf_left; 896} 897 898#ifdef HAVE_ALBUMART 899static int read_buf_id3_unsync(struct jpeg* p_jpeg, size_t count) 900{ 901 count = read(p_jpeg->fd, p_jpeg->buf, count); 902 return id3_unsynchronize(p_jpeg->buf, count, (bool*) &p_jpeg->custom_param); 903} 904 905static int read_buf_vorbis_base64(struct jpeg* p_jpeg, size_t count) 906{ 907 struct ogg_file* ogg = p_jpeg->custom_param; 908 unsigned char* buf = p_jpeg->buf; 909 count = ogg_file_read(ogg, buf, count); 910 if (count == (size_t) -1) 911 return 0; 912 913 return base64_decode(buf, count, buf); 914} 915 916/* when pjpeg->read_buf involves additional data processing (like base64 decoding) 917 * we can't use lseek and have to call pjpeg->read_buf for proper seek */ 918static bool skip_bytes_read_buf(struct jpeg* p_jpeg) 919{ 920 do 921 { 922 int count = -p_jpeg->buf_left; 923 fill_buf(p_jpeg); 924 if (p_jpeg->buf_left < 0) 925 return false; 926 p_jpeg->buf_left -= count; 927 p_jpeg->buf_index += count; 928 } while (p_jpeg->buf_left < 0); 929 return true; 930} 931 932#endif /* HAVE_ALBUMART */ 933 934static unsigned char *jpeg_getc(struct jpeg* p_jpeg) 935{ 936 if (UNLIKELY(p_jpeg->buf_left < 1)) 937 fill_buf(p_jpeg); 938 if (UNLIKELY(p_jpeg->buf_left < 1)) 939 return NULL; 940 p_jpeg->buf_left--; 941 return (p_jpeg->buf_index++) + p_jpeg->buf; 942} 943 944static bool skip_bytes_seek(struct jpeg* p_jpeg) 945{ 946 if (UNLIKELY(lseek(p_jpeg->fd, -p_jpeg->buf_left, SEEK_CUR) < 0)) 947 return false; 948 p_jpeg->buf_left = 0; 949 return true; 950} 951 952static bool skip_bytes(struct jpeg* p_jpeg, int count) 953{ 954 p_jpeg->buf_left -= count; 955 p_jpeg->buf_index += count; 956 return p_jpeg->buf_left >= 0 || p_jpeg->skip_bytes_seek(p_jpeg); 957} 958 959static void jpeg_putc(struct jpeg* p_jpeg) 960{ 961 p_jpeg->buf_left++; 962 p_jpeg->buf_index--; 963} 964#endif 965 966#define e_skip_bytes(jpeg, count) \ 967do {\ 968 if (UNLIKELY(!skip_bytes((jpeg),(count)))) \ 969 return -1; \ 970} while (0) 971 972#define e_getc(jpeg, code) \ 973({ \ 974 unsigned char *c; \ 975 if (UNLIKELY(!(c = jpeg_getc(jpeg)))) \ 976 return (code); \ 977 *c; \ 978}) 979 980#define d_getc(jpeg, def) \ 981({ \ 982 unsigned char *cp = jpeg_getc(jpeg); \ 983 unsigned char c = LIKELY(cp) ? *cp : (def); \ 984 c; \ 985}) 986 987/* Preprocess the JPEG JFIF file */ 988static int process_markers(struct jpeg* p_jpeg) 989{ 990 unsigned char c; 991 int marker_size; /* variable length of marker segment */ 992 int i, j, n; 993 int ret = 0; /* returned flags */ 994 bool done = false; 995 996 while (!done) 997 { 998 c = e_getc(p_jpeg, -1); 999 if (c != 0xFF) /* no marker? */ 1000 { 1001 JDEBUGF("Non-marker data\n"); 1002 continue; /* discard */ 1003 } 1004 1005 c = e_getc(p_jpeg, -1); 1006 JDEBUGF("marker value %X\n",c); 1007 switch (c) 1008 { 1009 case 0xFF: /* Previous FF was fill byte */ 1010 jpeg_putc(p_jpeg); /* This FF could be start of a marker */ 1011 continue; 1012 case 0x00: /* Zero stuffed byte */ 1013 break; /* discard */ 1014 1015 case 0xC0: /* SOF Huff - Baseline DCT */ 1016 { 1017 JDEBUGF("SOF marker "); 1018 ret |= SOF0; 1019 marker_size = e_getc(p_jpeg, -1) << 8; /* Highbyte */ 1020 marker_size |= e_getc(p_jpeg, -1); /* Lowbyte */ 1021 JDEBUGF("len: %d\n", marker_size); 1022 n = e_getc(p_jpeg, -1); /* sample precision (= 8 or 12) */ 1023 if (n != 8) 1024 { 1025 return(-1); /* Unsupported sample precision */ 1026 } 1027 p_jpeg->y_size = e_getc(p_jpeg, -1) << 8; /* Highbyte */ 1028 p_jpeg->y_size |= e_getc(p_jpeg, -1); /* Lowbyte */ 1029 p_jpeg->x_size = e_getc(p_jpeg, -1) << 8; /* Highbyte */ 1030 p_jpeg->x_size |= e_getc(p_jpeg, -1); /* Lowbyte */ 1031 JDEBUGF(" dimensions: %dx%d\n", p_jpeg->x_size, 1032 p_jpeg->y_size); 1033 1034 n = (marker_size-2-6)/3; 1035 if (e_getc(p_jpeg, -1) != n || (n != 1 && n != 3)) 1036 { 1037 return(-2); /* Unsupported SOF0 component specification */ 1038 } 1039 for (i=0; i<n; i++) 1040 { 1041 /* Component info */ 1042 p_jpeg->frameheader[i].ID = e_getc(p_jpeg, -1); 1043 p_jpeg->frameheader[i].horizontal_sampling = 1044 (c = e_getc(p_jpeg, -1)) >> 4; 1045 p_jpeg->frameheader[i].vertical_sampling = c & 0x0F; 1046 p_jpeg->frameheader[i].quanttable_select = 1047 e_getc(p_jpeg, -1); 1048 if (p_jpeg->frameheader[i].horizontal_sampling > 2 1049 || p_jpeg->frameheader[i].vertical_sampling > 2) 1050 return -3; /* Unsupported SOF0 subsampling */ 1051 } 1052 p_jpeg->blocks = n; 1053 } 1054 break; 1055 1056 case 0xC1: /* SOF Huff - Extended sequential DCT*/ 1057 case 0xC2: /* SOF Huff - Progressive DCT*/ 1058 case 0xC3: /* SOF Huff - Spatial (sequential) lossless*/ 1059 case 0xC5: /* SOF Huff - Differential sequential DCT*/ 1060 case 0xC6: /* SOF Huff - Differential progressive DCT*/ 1061 case 0xC7: /* SOF Huff - Differential spatial*/ 1062 case 0xC8: /* SOF Arith - Reserved for JPEG extensions*/ 1063 case 0xC9: /* SOF Arith - Extended sequential DCT*/ 1064 case 0xCA: /* SOF Arith - Progressive DCT*/ 1065 case 0xCB: /* SOF Arith - Spatial (sequential) lossless*/ 1066 case 0xCD: /* SOF Arith - Differential sequential DCT*/ 1067 case 0xCE: /* SOF Arith - Differential progressive DCT*/ 1068 case 0xCF: /* SOF Arith - Differential spatial*/ 1069 { 1070 return (-4); /* other DCT model than baseline not implemented */ 1071 } 1072 1073 case 0xC4: /* Define Huffman Table(s) */ 1074 { 1075 ret |= DHT; 1076 marker_size = e_getc(p_jpeg, -1) << 8; /* Highbyte */ 1077 marker_size |= e_getc(p_jpeg, -1); /* Lowbyte */ 1078 marker_size -= 2; 1079 1080 while (marker_size > 17) /* another table */ 1081 { 1082 c = e_getc(p_jpeg, -1); 1083 marker_size--; 1084 int sum = 0; 1085 i = c & 0x0F; /* table index */ 1086 if (i > 1) 1087 { 1088 return (-5); /* Huffman table index out of range */ 1089 } else { 1090 if (c & 0xF0) /* AC table */ 1091 { 1092 for (j=0; j<16; j++) 1093 { 1094 p_jpeg->hufftable[i].huffmancodes_ac[j] = 1095 (c = e_getc(p_jpeg, -1)); 1096 sum += c; 1097 marker_size -= 1; 1098 } 1099 if(16 + sum > AC_LEN) 1100 return -10; /* longer than allowed */ 1101 1102 for (; j < 16 + sum; j++) 1103 { 1104 p_jpeg->hufftable[i].huffmancodes_ac[j] = 1105 e_getc(p_jpeg, -1); 1106 marker_size--; 1107 } 1108 } 1109 else /* DC table */ 1110 { 1111 for (j=0; j<16; j++) 1112 { 1113 p_jpeg->hufftable[i].huffmancodes_dc[j] = 1114 (c = e_getc(p_jpeg, -1)); 1115 sum += c; 1116 marker_size--; 1117 } 1118 if(16 + sum > DC_LEN) 1119 return -11; /* longer than allowed */ 1120 1121 for (; j < 16 + sum; j++) 1122 { 1123 p_jpeg->hufftable[i].huffmancodes_dc[j] = 1124 e_getc(p_jpeg, -1); 1125 marker_size--; 1126 } 1127 } 1128 } 1129 } /* while */ 1130 e_skip_bytes(p_jpeg, marker_size); 1131 } 1132 break; 1133 1134 case 0xCC: /* Define Arithmetic coding conditioning(s) */ 1135 return(-6); /* Arithmetic coding not supported */ 1136 1137 case 0xD8: /* Start of Image */ 1138 JDEBUGF("SOI\n"); 1139 break; 1140 case 0xD9: /* End of Image */ 1141 JDEBUGF("EOI\n"); 1142 break; 1143 case 0x01: /* for temp private use arith code */ 1144 JDEBUGF("private\n"); 1145 break; /* skip parameterless marker */ 1146 1147 1148 case 0xDA: /* Start of Scan */ 1149 { 1150 ret |= SOS; 1151 marker_size = e_getc(p_jpeg, -1) << 8; /* Highbyte */ 1152 marker_size |= e_getc(p_jpeg, -1); /* Lowbyte */ 1153 marker_size -= 2; 1154 1155 n = (marker_size-1-3)/2; 1156 if (e_getc(p_jpeg, -1) != n || (n != 1 && n != 3)) 1157 { 1158 return (-7); /* Unsupported SOS component specification */ 1159 } 1160 marker_size--; 1161 for (i=0; i<n; i++) 1162 { 1163 p_jpeg->scanheader[i].ID = e_getc(p_jpeg, -1); 1164 p_jpeg->scanheader[i].DC_select = (c = e_getc(p_jpeg, -1)) 1165 >> 4; 1166 p_jpeg->scanheader[i].AC_select = c & 0x0F; 1167 marker_size -= 2; 1168 } 1169 /* skip spectral information */ 1170 e_skip_bytes(p_jpeg, marker_size); 1171 done = true; 1172 } 1173 break; 1174 1175 case 0xDB: /* Define quantization Table(s) */ 1176 { 1177 ret |= DQT; 1178 marker_size = e_getc(p_jpeg, -1) << 8; /* Highbyte */ 1179 marker_size |= e_getc(p_jpeg, -1); /* Lowbyte */ 1180 marker_size -= 2; 1181 1182 n = (marker_size)/(QUANT_TABLE_LENGTH+1); /* # of tables */ 1183 for (i=0; i<n; i++) 1184 { 1185 int id = e_getc(p_jpeg, -1); /* ID */ 1186 marker_size--; 1187 if (id >= 4) 1188 { 1189 return (-8); /* Unsupported quantization table */ 1190 } 1191 /* Read Quantisation table: */ 1192 for (j=0; j<QUANT_TABLE_LENGTH; j++) 1193 { 1194 p_jpeg->quanttable[id][j] = e_getc(p_jpeg, -1); 1195 marker_size--; 1196 } 1197 } 1198 e_skip_bytes(p_jpeg, marker_size); 1199 } 1200 break; 1201 1202 case 0xDD: /* Define Restart Interval */ 1203 { 1204 marker_size = e_getc(p_jpeg, -1) << 8; /* Highbyte */ 1205 marker_size |= e_getc(p_jpeg, -1); /* Lowbyte */ 1206 marker_size -= 4; 1207 /* Highbyte */ 1208 p_jpeg->restart_interval = e_getc(p_jpeg, -1) << 8; 1209 p_jpeg->restart_interval |= e_getc(p_jpeg, -1); /* Lowbyte */ 1210 e_skip_bytes(p_jpeg, marker_size); /* skip segment */ 1211 } 1212 break; 1213 1214 case 0xDC: /* Define Number of Lines */ 1215 case 0xDE: /* Define Hierarchical progression */ 1216 case 0xDF: /* Expand Reference Component(s) */ 1217 case 0xE0: /* Application Field 0*/ 1218 case 0xE1: /* Application Field 1*/ 1219 case 0xE2: /* Application Field 2*/ 1220 case 0xE3: /* Application Field 3*/ 1221 case 0xE4: /* Application Field 4*/ 1222 case 0xE5: /* Application Field 5*/ 1223 case 0xE6: /* Application Field 6*/ 1224 case 0xE7: /* Application Field 7*/ 1225 case 0xE8: /* Application Field 8*/ 1226 case 0xE9: /* Application Field 9*/ 1227 case 0xEA: /* Application Field 10*/ 1228 case 0xEB: /* Application Field 11*/ 1229 case 0xEC: /* Application Field 12*/ 1230 case 0xED: /* Application Field 13*/ 1231 case 0xEE: /* Application Field 14*/ 1232 case 0xEF: /* Application Field 15*/ 1233 case 0xFE: /* Comment */ 1234 { 1235 marker_size = e_getc(p_jpeg, -1) << 8; /* Highbyte */ 1236 marker_size |= e_getc(p_jpeg, -1); /* Lowbyte */ 1237 marker_size -= 2; 1238 JDEBUGF("unhandled marker len %d\n", marker_size); 1239 e_skip_bytes(p_jpeg, marker_size); /* skip segment */ 1240 } 1241 break; 1242 1243 case 0xF0: /* Reserved for JPEG extensions */ 1244 case 0xF1: /* Reserved for JPEG extensions */ 1245 case 0xF2: /* Reserved for JPEG extensions */ 1246 case 0xF3: /* Reserved for JPEG extensions */ 1247 case 0xF4: /* Reserved for JPEG extensions */ 1248 case 0xF5: /* Reserved for JPEG extensions */ 1249 case 0xF6: /* Reserved for JPEG extensions */ 1250 case 0xF7: /* Reserved for JPEG extensions */ 1251 case 0xF8: /* Reserved for JPEG extensions */ 1252 case 0xF9: /* Reserved for JPEG extensions */ 1253 case 0xFA: /* Reserved for JPEG extensions */ 1254 case 0xFB: /* Reserved for JPEG extensions */ 1255 case 0xFC: /* Reserved for JPEG extensions */ 1256 case 0xFD: /* Reserved for JPEG extensions */ 1257 case 0x02: /* Reserved */ 1258 default: 1259 return (-9); /* Unknown marker */ 1260 } /* switch */ 1261 } /* while */ 1262 1263 return (ret); /* return flags with seen markers */ 1264} 1265 1266static const struct huffman_table luma_table = 1267{ 1268 { 1269 0x00,0x01,0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00, 1270 0x00,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B 1271 }, 1272 { 1273 0x00,0x02,0x01,0x03,0x03,0x02,0x04,0x03,0x05,0x05,0x04,0x04,0x00,0x00, 1274 0x01,0x7D,0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06, 1275 0x13,0x51,0x61,0x07,0x22,0x71,0x14,0x32,0x81,0x91,0xA1,0x08,0x23,0x42, 1276 0xB1,0xC1,0x15,0x52,0xD1,0xF0,0x24,0x33,0x62,0x72,0x82,0x09,0x0A,0x16, 1277 0x17,0x18,0x19,0x1A,0x25,0x26,0x27,0x28,0x29,0x2A,0x34,0x35,0x36,0x37, 1278 0x38,0x39,0x3A,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x53,0x54,0x55, 1279 0x56,0x57,0x58,0x59,0x5A,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x73, 1280 0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x83,0x84,0x85,0x86,0x87,0x88,0x89, 1281 0x8A,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0xA2,0xA3,0xA4,0xA5, 1282 0xA6,0xA7,0xA8,0xA9,0xAA,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA, 1283 0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xD2,0xD3,0xD4,0xD5,0xD6, 1284 0xD7,0xD8,0xD9,0xDA,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA, 1285 0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA 1286 } 1287}; 1288 1289static const struct huffman_table chroma_table = 1290{ 1291 { 1292 0x00,0x03,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00, 1293 0x00,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B 1294 }, 1295 { 1296 0x00,0x02,0x01,0x02,0x04,0x04,0x03,0x04,0x07,0x05,0x04,0x04,0x00,0x01, 1297 0x02,0x77,0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41, 1298 0x51,0x07,0x61,0x71,0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91,0xA1,0xB1, 1299 0xC1,0x09,0x23,0x33,0x52,0xF0,0x15,0x62,0x72,0xD1,0x0A,0x16,0x24,0x34, 1300 0xE1,0x25,0xF1,0x17,0x18,0x19,0x1A,0x26,0x27,0x28,0x29,0x2A,0x35,0x36, 1301 0x37,0x38,0x39,0x3A,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x53,0x54, 1302 0x55,0x56,0x57,0x58,0x59,0x5A,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A, 1303 0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x82,0x83,0x84,0x85,0x86,0x87, 1304 0x88,0x89,0x8A,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0xA2,0xA3, 1305 0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8, 1306 0xB9,0xBA,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xD2,0xD3,0xD4, 1307 0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9, 1308 0xEA,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA 1309 } 1310}; 1311 1312static void default_huff_tbl(struct jpeg* p_jpeg) 1313{ 1314 1315 MEMCPY(&p_jpeg->hufftable[0], &luma_table, sizeof(luma_table)); 1316 MEMCPY(&p_jpeg->hufftable[1], &chroma_table, sizeof(chroma_table)); 1317 1318 return; 1319} 1320 1321/* Compute the derived values for a Huffman table */ 1322static void fix_huff_tbl(int* htbl, struct derived_tbl* dtbl) 1323{ 1324 int p, i, l, si; 1325 int lookbits, ctr; 1326 char huffsize[257]; 1327 unsigned int huffcode[257]; 1328 unsigned int code; 1329 1330 dtbl->pub = htbl; /* fill in back link */ 1331 1332 /* Figure C.1: make table of Huffman code length for each symbol */ 1333 /* Note that this is in code-length order. */ 1334 1335 p = 0; 1336 for (l = 1; l <= 16; l++) 1337 { /* all possible code length */ 1338 for (i = 1; i <= (int) htbl[l-1]; i++) /* all codes per length */ 1339 huffsize[p++] = (char) l; 1340 } 1341 huffsize[p] = 0; 1342 1343 /* Figure C.2: generate the codes themselves */ 1344 /* Note that this is in code-length order. */ 1345 1346 code = 0; 1347 si = huffsize[0]; 1348 p = 0; 1349 while (huffsize[p]) 1350 { 1351 while (((int) huffsize[p]) == si) 1352 { 1353 huffcode[p++] = code; 1354 code++; 1355 } 1356 code <<= 1; 1357 si++; 1358 } 1359 1360 /* Figure F.15: generate decoding tables for bit-sequential decoding */ 1361 1362 p = 0; 1363 for (l = 1; l <= 16; l++) 1364 { 1365 if (htbl[l-1]) 1366 { 1367 /* huffval[] index of 1st symbol of code length l */ 1368 dtbl->valptr[l] = p; 1369 dtbl->mincode[l] = huffcode[p]; /* minimum code of length l */ 1370 p += htbl[l-1]; 1371 dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */ 1372 } 1373 else 1374 { 1375 dtbl->maxcode[l] = -1; /* -1 if no codes of this length */ 1376 } 1377 } 1378 dtbl->maxcode[17] = 0xFFFFFL; /* ensures huff_DECODE terminates */ 1379 1380 /* Compute lookahead tables to speed up decoding. 1381 * First we set all the table entries to 0, indicating "too long"; 1382 * then we iterate through the Huffman codes that are short enough and 1383 * fill in all the entries that correspond to bit sequences starting 1384 * with that code. 1385 */ 1386 1387 MEMSET(dtbl->look_nbits, 0, sizeof(dtbl->look_nbits)); 1388 1389 p = 0; 1390 for (l = 1; l <= HUFF_LOOKAHEAD; l++) 1391 { 1392 for (i = 1; i <= (int) htbl[l-1]; i++, p++) 1393 { 1394 /* l = current code's length, p = its index in huffcode[] & 1395 * huffval[]. Generate left-justified code followed by all possible 1396 * bit sequences 1397 */ 1398 lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l); 1399 for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) 1400 { 1401 dtbl->look_nbits[lookbits] = l; 1402 dtbl->look_sym[lookbits] = htbl[16+p]; 1403 lookbits++; 1404 } 1405 } 1406 } 1407} 1408 1409 1410/* zag[i] is the natural-order position of the i'th element of zigzag order. */ 1411static const unsigned char zag[] = 1412{ 1413#ifdef JPEG_IDCT_TRANSPOSE 1414 0, 8, 1, 2, 9, 16, 24, 17, 1415 10, 3, 4, 11, 18, 25, 32, 40, 1416 33, 26, 19, 12, 5, 6, 13, 20, 1417 27, 34, 41, 48, 56, 49, 42, 35, 1418 28, 21, 14, 7, 15, 22, 29, 36, 1419 43, 50, 57, 58, 51, 44, 37, 30, 1420 23, 31, 38, 45, 52, 59, 60, 53, 1421 46, 39, 47, 54, 61, 62, 55, 63, 1422#endif 1423 0, 1, 8, 16, 9, 2, 3, 10, 1424 17, 24, 32, 25, 18, 11, 4, 5, 1425 12, 19, 26, 33, 40, 48, 41, 34, 1426 27, 20, 13, 6, 7, 14, 21, 28, 1427 35, 42, 49, 56, 57, 50, 43, 36, 1428 29, 22, 15, 23, 30, 37, 44, 51, 1429 58, 59, 52, 45, 38, 31, 39, 46, 1430 53, 60, 61, 54, 47, 55, 62, 63, 1431}; 1432 1433/* zig[i] is the the zig-zag order position of the i'th element of natural 1434 * order, reading left-to-right then top-to-bottom. 1435 */ 1436static const unsigned char zig[] = 1437{ 1438 0, 1, 5, 6, 14, 15, 27, 28, 1439 2, 4, 7, 13, 16, 26, 29, 42, 1440 3, 8, 12, 17, 25, 30, 41, 43, 1441 9, 11, 18, 24, 31, 40, 44, 53, 1442 10, 19, 23, 32, 39, 45, 52, 54, 1443 20, 22, 33, 38, 46, 51, 55, 60, 1444 21, 34, 37, 47, 50, 56, 59, 61, 1445 35, 36, 48, 49, 57, 58, 62, 63 1446}; 1447 1448/* Reformat some image header data so that the decoder can use it properly. */ 1449INLINE void fix_headers(struct jpeg* p_jpeg) 1450{ 1451 int i; 1452 1453 for (i=0; i<4; i++) 1454 p_jpeg->store_pos[i] = i; /* default ordering */ 1455 1456 /* assignments for the decoding of blocks */ 1457 if (p_jpeg->frameheader[0].horizontal_sampling == 2 1458 && p_jpeg->frameheader[0].vertical_sampling == 1) 1459 { /* 4:2:2 */ 1460 p_jpeg->blocks = 4; 1461 p_jpeg->x_mbl = (p_jpeg->x_size+15) / 16; 1462 p_jpeg->x_phys = p_jpeg->x_mbl * 16; 1463 p_jpeg->y_mbl = (p_jpeg->y_size+7) / 8; 1464 p_jpeg->y_phys = p_jpeg->y_mbl * 8; 1465 p_jpeg->mcu_membership[0] = 0; /* Y1=Y2=0, U=1, V=2 */ 1466 p_jpeg->mcu_membership[1] = 0; 1467 p_jpeg->mcu_membership[2] = 1; 1468 p_jpeg->mcu_membership[3] = 2; 1469 p_jpeg->tab_membership[0] = 0; /* DC, DC, AC, AC */ 1470 p_jpeg->tab_membership[1] = 0; 1471 p_jpeg->tab_membership[2] = 1; 1472 p_jpeg->tab_membership[3] = 1; 1473 p_jpeg->subsample_x[0] = 1; 1474 p_jpeg->subsample_x[1] = 2; 1475 p_jpeg->subsample_x[2] = 2; 1476 p_jpeg->subsample_y[0] = 1; 1477 p_jpeg->subsample_y[1] = 1; 1478 p_jpeg->subsample_y[2] = 1; 1479 } 1480 if (p_jpeg->frameheader[0].horizontal_sampling == 1 1481 && p_jpeg->frameheader[0].vertical_sampling == 2) 1482 { /* 4:2:2 vertically subsampled */ 1483 p_jpeg->store_pos[1] = 2; /* block positions are mirrored */ 1484 p_jpeg->store_pos[2] = 1; 1485 p_jpeg->blocks = 4; 1486 p_jpeg->x_mbl = (p_jpeg->x_size+7) / 8; 1487 p_jpeg->x_phys = p_jpeg->x_mbl * 8; 1488 p_jpeg->y_mbl = (p_jpeg->y_size+15) / 16; 1489 p_jpeg->y_phys = p_jpeg->y_mbl * 16; 1490 p_jpeg->mcu_membership[0] = 0; /* Y1=Y2=0, U=1, V=2 */ 1491 p_jpeg->mcu_membership[1] = 0; 1492 p_jpeg->mcu_membership[2] = 1; 1493 p_jpeg->mcu_membership[3] = 2; 1494 p_jpeg->tab_membership[0] = 0; /* DC, DC, AC, AC */ 1495 p_jpeg->tab_membership[1] = 0; 1496 p_jpeg->tab_membership[2] = 1; 1497 p_jpeg->tab_membership[3] = 1; 1498 p_jpeg->subsample_x[0] = 1; 1499 p_jpeg->subsample_x[1] = 1; 1500 p_jpeg->subsample_x[2] = 1; 1501 p_jpeg->subsample_y[0] = 1; 1502 p_jpeg->subsample_y[1] = 2; 1503 p_jpeg->subsample_y[2] = 2; 1504 } 1505 else if (p_jpeg->frameheader[0].horizontal_sampling == 2 1506 && p_jpeg->frameheader[0].vertical_sampling == 2) 1507 { /* 4:2:0 */ 1508 p_jpeg->blocks = 6; 1509 p_jpeg->x_mbl = (p_jpeg->x_size+15) / 16; 1510 p_jpeg->x_phys = p_jpeg->x_mbl * 16; 1511 p_jpeg->y_mbl = (p_jpeg->y_size+15) / 16; 1512 p_jpeg->y_phys = p_jpeg->y_mbl * 16; 1513 p_jpeg->mcu_membership[0] = 0; 1514 p_jpeg->mcu_membership[1] = 0; 1515 p_jpeg->mcu_membership[2] = 0; 1516 p_jpeg->mcu_membership[3] = 0; 1517 p_jpeg->mcu_membership[4] = 1; 1518 p_jpeg->mcu_membership[5] = 2; 1519 p_jpeg->tab_membership[0] = 0; 1520 p_jpeg->tab_membership[1] = 0; 1521 p_jpeg->tab_membership[2] = 0; 1522 p_jpeg->tab_membership[3] = 0; 1523 p_jpeg->tab_membership[4] = 1; 1524 p_jpeg->tab_membership[5] = 1; 1525 p_jpeg->subsample_x[0] = 1; 1526 p_jpeg->subsample_x[1] = 2; 1527 p_jpeg->subsample_x[2] = 2; 1528 p_jpeg->subsample_y[0] = 1; 1529 p_jpeg->subsample_y[1] = 2; 1530 p_jpeg->subsample_y[2] = 2; 1531 } 1532 else if (p_jpeg->frameheader[0].horizontal_sampling == 1 1533 && p_jpeg->frameheader[0].vertical_sampling == 1) 1534 { /* 4:4:4 */ 1535 /* don't overwrite p_jpeg->blocks */ 1536 p_jpeg->x_mbl = (p_jpeg->x_size+7) / 8; 1537 p_jpeg->x_phys = p_jpeg->x_mbl * 8; 1538 p_jpeg->y_mbl = (p_jpeg->y_size+7) / 8; 1539 p_jpeg->y_phys = p_jpeg->y_mbl * 8; 1540 p_jpeg->mcu_membership[0] = 0; 1541 p_jpeg->mcu_membership[1] = 1; 1542 p_jpeg->mcu_membership[2] = 2; 1543 p_jpeg->tab_membership[0] = 0; 1544 p_jpeg->tab_membership[1] = 1; 1545 p_jpeg->tab_membership[2] = 1; 1546 p_jpeg->subsample_x[0] = 1; 1547 p_jpeg->subsample_x[1] = 1; 1548 p_jpeg->subsample_x[2] = 1; 1549 p_jpeg->subsample_y[0] = 1; 1550 p_jpeg->subsample_y[1] = 1; 1551 p_jpeg->subsample_y[2] = 1; 1552 } 1553 else 1554 { 1555 /* error */ 1556 } 1557 1558} 1559 1560INLINE void fix_huff_tables(struct jpeg *p_jpeg) 1561{ 1562 fix_huff_tbl(p_jpeg->hufftable[0].huffmancodes_dc, 1563 &p_jpeg->dc_derived_tbls[0]); 1564 fix_huff_tbl(p_jpeg->hufftable[0].huffmancodes_ac, 1565 &p_jpeg->ac_derived_tbls[0]); 1566 fix_huff_tbl(p_jpeg->hufftable[1].huffmancodes_dc, 1567 &p_jpeg->dc_derived_tbls[1]); 1568 fix_huff_tbl(p_jpeg->hufftable[1].huffmancodes_ac, 1569 &p_jpeg->ac_derived_tbls[1]); 1570} 1571 1572/* Because some of the IDCT routines never multiply by any constants, and 1573 * therefore do not produce shifted output, we add the shift into the 1574 * quantization table when one of these IDCT routines is used, rather than 1575 * have the IDCT shift each value it processes. 1576 */ 1577INLINE void fix_quant_tables(struct jpeg *p_jpeg) 1578{ 1579 int shift, i, j; 1580 1581#ifdef HAVE_LCD_COLOR 1582 const int k = 2; 1583#else 1584 const int k = 1; 1585#endif 1586 1587 for (i = 0; i < k; i++) 1588 { 1589 shift = idct_tbl[p_jpeg->v_scale[i]].scale; 1590 if (shift) 1591 { 1592 for (j = 0; j < 64; j++) 1593 p_jpeg->quanttable[i][j] <<= shift; 1594 } 1595 } 1596} 1597 1598/* 1599* These functions/macros provide the in-line portion of bit fetching. 1600* Use check_bit_buffer to ensure there are N bits in get_buffer 1601* before using get_bits, peek_bits, or drop_bits. 1602* check_bit_buffer(state,n,action); 1603* Ensure there are N bits in get_buffer; if suspend, take action. 1604* val = get_bits(n); 1605* Fetch next N bits. 1606* val = peek_bits(n); 1607* Fetch next N bits without removing them from the buffer. 1608* drop_bits(n); 1609* Discard next N bits. 1610* The value N should be a simple variable, not an expression, because it 1611* is evaluated multiple times. 1612*/ 1613 1614static void fill_bit_buffer(struct jpeg* p_jpeg) 1615{ 1616 unsigned char byte, marker; 1617 1618 if (p_jpeg->marker_val) 1619 p_jpeg->marker_ind += 16; 1620 byte = d_getc(p_jpeg, 0); 1621 if (UNLIKELY(byte == 0xFF)) /* legal marker can be byte stuffing or RSTm */ 1622 { /* simplification: just skip the (one-byte) marker code */ 1623 marker = d_getc(p_jpeg, 0); 1624 if ((marker & ~7) == 0xD0) 1625 { 1626 p_jpeg->marker_val = marker; 1627 p_jpeg->marker_ind = 8; 1628 } 1629 } 1630 p_jpeg->bitbuf = (p_jpeg->bitbuf << 8) | byte; 1631 1632 byte = d_getc(p_jpeg, 0); 1633 if (UNLIKELY(byte == 0xFF)) /* legal marker can be byte stuffing or RSTm */ 1634 { /* simplification: just skip the (one-byte) marker code */ 1635 marker = d_getc(p_jpeg, 0); 1636 if ((marker & ~7) == 0xD0) 1637 { 1638 p_jpeg->marker_val = marker; 1639 p_jpeg->marker_ind = 0; 1640 } 1641 } 1642 p_jpeg->bitbuf = (p_jpeg->bitbuf << 8) | byte; 1643 p_jpeg->bitbuf_bits += 16; 1644#ifdef JPEG_BS_DEBUG 1645 DEBUGF("read in: %04X\n", p_jpeg->bitbuf & 0xFFFF); 1646#endif 1647} 1648 1649INLINE void check_bit_buffer(struct jpeg *p_jpeg, int nbits) 1650{ 1651 if (nbits > p_jpeg->bitbuf_bits) 1652 fill_bit_buffer(p_jpeg); 1653} 1654 1655INLINE int get_bits(struct jpeg *p_jpeg, int nbits) 1656{ 1657#ifdef JPEG_BS_DEBUG 1658 if (nbits > p_jpeg->bitbuf_bits) 1659 DEBUGF("bitbuffer underrun\n"); 1660 int mask = BIT_N(p_jpeg->bitbuf_bits - 1); 1661 int i; 1662 DEBUGF("get %d bits: ", nbits); 1663 for (i = 0; i < nbits; i++) 1664 DEBUGF("%d",!!(p_jpeg->bitbuf & (mask >>= 1))); 1665 DEBUGF("\n"); 1666#endif 1667 return ((int) (p_jpeg->bitbuf >> (p_jpeg->bitbuf_bits -= nbits))) & 1668 (BIT_N(nbits)-1); 1669} 1670 1671INLINE int peek_bits(struct jpeg *p_jpeg, int nbits) 1672{ 1673#ifdef JPEG_BS_DEBUG 1674 int mask = BIT_N(p_jpeg->bitbuf_bits - 1); 1675 int i; 1676 DEBUGF("peek %d bits: ", nbits); 1677 for (i = 0; i < nbits; i++) 1678 DEBUGF("%d",!!(p_jpeg->bitbuf & (mask >>= 1))); 1679 DEBUGF("\n"); 1680#endif 1681 return ((int) (p_jpeg->bitbuf >> (p_jpeg->bitbuf_bits - nbits))) & 1682 (BIT_N(nbits)-1); 1683} 1684 1685INLINE void drop_bits(struct jpeg *p_jpeg, int nbits) 1686{ 1687#ifdef JPEG_BS_DEBUG 1688 int mask = BIT_N(p_jpeg->bitbuf_bits - 1); 1689 int i; 1690 DEBUGF("drop %d bits: ", nbits); 1691 for (i = 0; i < nbits; i++) 1692 DEBUGF("%d",!!(p_jpeg->bitbuf & (mask >>= 1))); 1693 DEBUGF("\n"); 1694#endif 1695 p_jpeg->bitbuf_bits -= nbits; 1696} 1697 1698/* re-synchronize to entropy data (skip restart marker) */ 1699static void search_restart(struct jpeg *p_jpeg) 1700{ 1701 if (p_jpeg->marker_val) 1702 { 1703 p_jpeg->marker_val = 0; 1704 p_jpeg->bitbuf_bits = p_jpeg->marker_ind; 1705 p_jpeg->marker_ind = 0; 1706 return; 1707 } 1708 unsigned char byte; 1709 p_jpeg->bitbuf_bits = 0; 1710 while ((byte = d_getc(p_jpeg, 0xFF))) 1711 { 1712 if (byte == 0xff) 1713 { 1714 byte = d_getc(p_jpeg, 0xD0); 1715 if ((byte & ~7) == 0xD0) 1716 { 1717 return; 1718 } 1719 else 1720 jpeg_putc(p_jpeg); 1721 } 1722 } 1723} 1724 1725/* Figure F.12: extend sign bit. */ 1726/* This saves some code and data size, benchmarks about the same on RAM */ 1727#define HUFF_EXTEND(x,s) \ 1728({ \ 1729 int x__ = x; \ 1730 int s__ = s; \ 1731 x__ & BIT_N(s__- 1) ? x__ : x__ + (-1 << s__) + 1; \ 1732}) 1733 1734/* Decode a single value */ 1735#define huff_decode_dc(p_jpeg, tbl, s, r) \ 1736{ \ 1737 int nb, look; \ 1738\ 1739 check_bit_buffer((p_jpeg), HUFF_LOOKAHEAD); \ 1740 look = peek_bits((p_jpeg), HUFF_LOOKAHEAD); \ 1741 if ((nb = (tbl)->look_nbits[look]) != 0) \ 1742 { \ 1743 drop_bits((p_jpeg), nb); \ 1744 s = (tbl)->look_sym[look]; \ 1745 check_bit_buffer((p_jpeg), s); \ 1746 r = get_bits((p_jpeg), s); \ 1747 } else { \ 1748 /* slow_DECODE(s, HUFF_LOOKAHEAD+1)) < 0); */ \ 1749 long code; \ 1750 nb=HUFF_LOOKAHEAD+1; \ 1751 check_bit_buffer((p_jpeg), nb); \ 1752 code = get_bits((p_jpeg), nb); \ 1753 while (code > (tbl)->maxcode[nb]) \ 1754 { \ 1755 code <<= 1; \ 1756 check_bit_buffer((p_jpeg), 1); \ 1757 code |= get_bits((p_jpeg), 1); \ 1758 nb++; \ 1759 } \ 1760 if (nb > 16) /* error in Huffman */ \ 1761 { \ 1762 r = 0; s = 0; /* fake a zero, this is most safe */ \ 1763 } else { \ 1764 s = (tbl)->pub[16 + (tbl)->valptr[nb] + \ 1765 ((int) (code - (tbl)->mincode[nb]))]; \ 1766 check_bit_buffer((p_jpeg), s); \ 1767 r = get_bits((p_jpeg), s); \ 1768 } \ 1769 } /* end slow decode */ \ 1770} 1771 1772#define huff_decode_ac(p_jpeg, tbl, s) \ 1773{ \ 1774 int nb, look; \ 1775\ 1776 check_bit_buffer((p_jpeg), HUFF_LOOKAHEAD); \ 1777 look = peek_bits((p_jpeg), HUFF_LOOKAHEAD); \ 1778 if ((nb = (tbl)->look_nbits[look]) != 0) \ 1779 { \ 1780 drop_bits((p_jpeg), nb); \ 1781 s = (tbl)->look_sym[look]; \ 1782 } else { \ 1783 /* slow_DECODE(s, HUFF_LOOKAHEAD+1)) < 0); */ \ 1784 long code; \ 1785 nb=HUFF_LOOKAHEAD+1; \ 1786 check_bit_buffer((p_jpeg), nb); \ 1787 code = get_bits((p_jpeg), nb); \ 1788 while (code > (tbl)->maxcode[nb]) \ 1789 { \ 1790 code <<= 1; \ 1791 check_bit_buffer((p_jpeg), 1); \ 1792 code |= get_bits((p_jpeg), 1); \ 1793 nb++; \ 1794 } \ 1795 if (nb > 16) /* error in Huffman */ \ 1796 { \ 1797 s = 0; /* fake a zero, this is most safe */ \ 1798 } else { \ 1799 s = (tbl)->pub[16 + (tbl)->valptr[nb] + \ 1800 ((int) (code - (tbl)->mincode[nb]))]; \ 1801 } \ 1802 } /* end slow decode */ \ 1803} 1804 1805static struct img_part *store_row_jpeg(void *jpeg_args) 1806{ 1807 struct jpeg *p_jpeg = (struct jpeg*) jpeg_args; 1808#ifdef HAVE_LCD_COLOR 1809 int mcu_hscale = p_jpeg->h_scale[1]; 1810 int mcu_vscale = p_jpeg->v_scale[1]; 1811#else 1812 int mcu_hscale = (p_jpeg->h_scale[0] + 1813 p_jpeg->frameheader[0].horizontal_sampling - 1); 1814 int mcu_vscale = (p_jpeg->v_scale[0] + 1815 p_jpeg->frameheader[0].vertical_sampling - 1); 1816#endif 1817 unsigned int width = p_jpeg->x_mbl << mcu_hscale; 1818 unsigned int b_width = width * JPEG_PIX_SZ; 1819 int height = BIT_N(mcu_vscale); 1820 int x; 1821 if (!p_jpeg->mcu_row) /* Need to decode a new row of MCUs */ 1822 { 1823 p_jpeg->out_ptr = (unsigned char *)p_jpeg->img_buf; 1824 int store_offs[4]; 1825#ifdef HAVE_LCD_COLOR 1826 unsigned mcu_width = BIT_N(mcu_hscale); 1827#endif 1828 int mcu_offset = JPEG_PIX_SZ << mcu_hscale; 1829 unsigned char *out = p_jpeg->out_ptr; 1830 store_offs[p_jpeg->store_pos[0]] = 0; 1831 store_offs[p_jpeg->store_pos[1]] = JPEG_PIX_SZ << p_jpeg->h_scale[0]; 1832 store_offs[p_jpeg->store_pos[2]] = b_width << p_jpeg->v_scale[0]; 1833 store_offs[p_jpeg->store_pos[3]] = store_offs[1] + store_offs[2]; 1834 /* decoded DCT coefficients */ 1835 int16_t block[IDCT_WS_SIZE] __attribute__((aligned(8))); 1836 for (x = 0; x < p_jpeg->x_mbl; x++) 1837 { 1838 int blkn; 1839 for (blkn = 0; blkn < p_jpeg->blocks; blkn++) 1840 { 1841 int ci = p_jpeg->mcu_membership[blkn]; /* component index */ 1842 int ti = p_jpeg->tab_membership[blkn]; /* table index */ 1843#ifdef JPEG_IDCT_TRANSPOSE 1844 bool transpose = p_jpeg->v_scale[!!ci] > 2; 1845#endif 1846 int k = 1; /* coefficient index */ 1847 int s, r; /* huffman values */ 1848 struct derived_tbl* dctbl = &p_jpeg->dc_derived_tbls[ti]; 1849 struct derived_tbl* actbl = &p_jpeg->ac_derived_tbls[ti]; 1850 1851 /* Section F.2.2.1: decode the DC coefficient difference */ 1852 huff_decode_dc(p_jpeg, dctbl, s, r); 1853 1854#ifndef HAVE_LCD_COLOR 1855 if (!ci) 1856#endif 1857 { 1858 s = HUFF_EXTEND(r, s); 1859#ifdef HAVE_LCD_COLOR 1860 p_jpeg->last_dc_val[ci] += s; 1861 /* output it (assumes zag[0] = 0) */ 1862 block[0] = MULTIPLY16(p_jpeg->last_dc_val[ci], 1863 p_jpeg->quanttable[!!ci][0]); 1864#else 1865 p_jpeg->last_dc_val += s; 1866 /* output it (assumes zag[0] = 0) */ 1867 block[0] = MULTIPLY16(p_jpeg->last_dc_val, 1868 p_jpeg->quanttable[0][0]); 1869#endif 1870 /* coefficient buffer must be cleared */ 1871 MEMSET(block+1, 0, p_jpeg->zero_need[!!ci] * sizeof(int)); 1872 /* Section F.2.2.2: decode the AC coefficients */ 1873 while(true) 1874 { 1875 huff_decode_ac(p_jpeg, actbl, s); 1876 r = s >> 4; 1877 s &= 15; 1878 k += r; 1879 if (s) 1880 { 1881 check_bit_buffer(p_jpeg, s); 1882 if (k >= p_jpeg->k_need[!!ci]) 1883 goto skip_rest; 1884 r = get_bits(p_jpeg, s); 1885 r = HUFF_EXTEND(r, s); 1886 r = MULTIPLY16(r, p_jpeg->quanttable[!!ci][k]); 1887#ifdef JPEG_IDCT_TRANSPOSE 1888 block[zag[transpose ? k : k + 64]] = r ; 1889#else 1890 block[zag[k]] = r ; 1891#endif 1892 } 1893 else 1894 { 1895 if (r != 15) 1896 goto block_end; 1897 } 1898 if ((++k) & 64) 1899 goto block_end; 1900 } /* for k */ 1901 } 1902 for (; k < 64; k++) 1903 { 1904 huff_decode_ac(p_jpeg, actbl, s); 1905 r = s >> 4; 1906 s &= 15; 1907 1908 if (s) 1909 { 1910 k += r; 1911 check_bit_buffer(p_jpeg, s); 1912skip_rest: 1913 drop_bits(p_jpeg, s); 1914 } 1915 else 1916 { 1917 if (r != 15) 1918 break; 1919 k += r; 1920 } 1921 } /* for k */ 1922block_end: 1923#ifndef HAVE_LCD_COLOR 1924 if (!ci) 1925#endif 1926 { 1927 int idct_cols = BIT_N(MIN(p_jpeg->h_scale[!!ci], 3)); 1928 int idct_rows = BIT_N(p_jpeg->v_scale[!!ci]); 1929 unsigned char *b_out = out + (ci ? ci : store_offs[blkn]); 1930 if (idct_tbl[p_jpeg->v_scale[!!ci]].v_idct) 1931#ifdef JPEG_IDCT_TRANSPOSE 1932 idct_tbl[p_jpeg->v_scale[!!ci]].v_idct(block, 1933 transpose ? block + 8 * idct_cols 1934 : block + idct_cols); 1935 uint16_t * h_block = transpose ? block + 64 : block; 1936 idct_tbl[p_jpeg->h_scale[!!ci]].h_idct(h_block, b_out, 1937 h_block + idct_rows * 8, b_width); 1938#else 1939 idct_tbl[p_jpeg->v_scale[!!ci]].v_idct(block, 1940 block + idct_cols); 1941 idct_tbl[p_jpeg->h_scale[!!ci]].h_idct(block, b_out, 1942 block + idct_rows * 8, b_width); 1943#endif 1944 } 1945 } /* for blkn */ 1946 /* don't starve other threads while an MCU row decodes */ 1947 yield(); 1948#ifdef HAVE_LCD_COLOR 1949 unsigned int xp; 1950 int yp; 1951 unsigned char *row = out; 1952 if (p_jpeg->blocks == 1) 1953 { 1954 for (yp = 0; yp < height; yp++, row += b_width) 1955 { 1956 unsigned char *px = row; 1957 for (xp = 0; xp < mcu_width; xp++, px += JPEG_PIX_SZ) 1958 { 1959 px[1] = px[2] = px[0]; 1960 } 1961 } 1962 } 1963#endif 1964 out += mcu_offset; 1965 if (p_jpeg->restart_interval && --p_jpeg->restart == 0) 1966 { /* if a restart marker is due: */ 1967 p_jpeg->restart = p_jpeg->restart_interval; /* count again */ 1968 search_restart(p_jpeg); /* align the bitstream */ 1969#ifdef HAVE_LCD_COLOR 1970 p_jpeg->last_dc_val[0] = p_jpeg->last_dc_val[1] = 1971 p_jpeg->last_dc_val[2] = 0; /* reset decoder */ 1972#else 1973 p_jpeg->last_dc_val = 0; 1974#endif 1975 } 1976 } 1977 } /* if !p_jpeg->mcu_row */ 1978 p_jpeg->mcu_row = (p_jpeg->mcu_row + 1) & (height - 1); 1979 p_jpeg->part.len = width; 1980 p_jpeg->part.buf = (jpeg_pix_t *)p_jpeg->out_ptr; 1981 p_jpeg->out_ptr += b_width; 1982 return &(p_jpeg->part); 1983} 1984 1985/****************************************************************************** 1986 * read_jpeg_file() 1987 * 1988 * Reads a JPEG file and puts the data in rockbox format in *bitmap. 1989 * 1990 *****************************************************************************/ 1991#ifndef JPEG_FROM_MEM 1992int clip_jpeg_file(const char* filename, 1993 int offset, 1994 unsigned long jpeg_size, 1995 struct bitmap *bm, 1996 int maxsize, 1997 int format, 1998 const struct custom_format *cformat) 1999{ 2000 int fd, ret; 2001 fd = open(filename, O_RDONLY); 2002 JDEBUGF("read_jpeg_file: filename: %s buffer len: %d cformat: %p\n", 2003 filename, maxsize, cformat); 2004 /* Exit if file opening failed */ 2005 if (fd < 0) { 2006 DEBUGF("read_jpeg_file: can't open '%s', rc: %d\n", filename, fd); 2007 return fd * 10 - 1; 2008 } 2009 lseek(fd, offset, SEEK_SET); 2010 ret = clip_jpeg_fd(fd, 0, jpeg_size, bm, maxsize, format, cformat); 2011 close(fd); 2012 return ret; 2013} 2014 2015int read_jpeg_file(const char* filename, 2016 struct bitmap *bm, 2017 int maxsize, 2018 int format, 2019 const struct custom_format *cformat) 2020{ 2021 return clip_jpeg_file(filename, 0, 0, bm, maxsize, format, cformat); 2022} 2023#endif 2024 2025static int calc_scale(int in_size, int out_size) 2026{ 2027 int scale = 0; 2028 out_size <<= 3; 2029 for (scale = 0; scale < 3; scale++) 2030 { 2031 if (out_size <= in_size) 2032 break; 2033 else 2034 in_size <<= 1; 2035 } 2036 return scale; 2037} 2038 2039#ifdef JPEG_FROM_MEM 2040int get_jpeg_dim_mem(unsigned char *data, unsigned long len, 2041 struct dim *size) 2042{ 2043 struct jpeg *p_jpeg = &jpeg; 2044 memset(p_jpeg, 0, sizeof(struct jpeg)); 2045 p_jpeg->data = data; 2046 p_jpeg->len = len; 2047 int status = process_markers(p_jpeg); 2048 if (status < 0) 2049 return status; 2050 if ((status & (DQT | SOF0)) != (DQT | SOF0)) 2051 return -(status * 16); 2052 size->width = p_jpeg->x_size; 2053 size->height = p_jpeg->y_size; 2054 return 0; 2055} 2056 2057int decode_jpeg_mem(unsigned char *data, 2058#else 2059int clip_jpeg_fd(int fd, int flags, 2060#endif 2061 unsigned long len, 2062 struct bitmap *bm, 2063 int maxsize, 2064 int format, 2065 const struct custom_format *cformat) 2066{ 2067 bool resize = false, dither = false; 2068 struct rowset rset; 2069 struct dim src_dim; 2070 int status; 2071 int bm_size; 2072#ifdef JPEG_FROM_MEM 2073 struct jpeg *p_jpeg = &jpeg; 2074#else 2075 struct jpeg *p_jpeg = (struct jpeg*)bm->data; 2076 int tmp_size = maxsize; 2077 ALIGN_BUFFER(p_jpeg, tmp_size, sizeof(long)); 2078 /* not enough memory for our struct jpeg */ 2079 if ((size_t)tmp_size < sizeof(struct jpeg)) 2080 return -1; 2081#endif 2082 memset(p_jpeg, 0, sizeof(struct jpeg)); 2083 p_jpeg->len = len; 2084#ifdef JPEG_FROM_MEM 2085 p_jpeg->data = data; 2086#else 2087 p_jpeg->fd = fd; 2088 if (p_jpeg->len == 0) 2089 p_jpeg->len = filesize(p_jpeg->fd); 2090 2091 p_jpeg->read_buf = read_buf; 2092 p_jpeg->skip_bytes_seek = skip_bytes_seek; 2093 2094#ifdef HAVE_ALBUMART 2095 if (flags & AA_FLAG_ID3_UNSYNC) 2096 { 2097 p_jpeg->read_buf = read_buf_id3_unsync; 2098 p_jpeg->custom_param = false; 2099 } 2100 else if (flags & AA_FLAG_VORBIS_BASE64) 2101 { 2102 struct ogg_file* ogg = alloca(sizeof(*ogg)); 2103 off_t pic_pos = lseek(fd, 0, SEEK_CUR); 2104 2105 // we need 92 bytes for format probing, reuse some available space 2106 unsigned char* buf_format = (unsigned char*) p_jpeg->quanttable; 2107 int type = get_ogg_format_and_move_to_comments(fd, buf_format); 2108 2109 ogg_file_init(ogg, fd, type, 0); 2110 bool packet_found; 2111 do 2112 { 2113 int seek_from_cur_pos = pic_pos - lseek(fd, 0, SEEK_CUR); 2114 packet_found = seek_from_cur_pos <= ogg->packet_remaining; 2115 if (ogg_file_read(ogg, NULL, packet_found ? seek_from_cur_pos : ogg->packet_remaining) < 0) 2116 return -1; 2117 } 2118 while (!packet_found); 2119 2120 p_jpeg->read_buf = read_buf_vorbis_base64; 2121 p_jpeg->skip_bytes_seek = skip_bytes_read_buf; 2122 p_jpeg->custom_param = ogg; 2123 } 2124#else 2125 (void)flags; 2126#endif /* HAVE_ALBUMART */ 2127 2128#endif 2129 status = process_markers(p_jpeg); 2130#ifndef JPEG_FROM_MEM 2131 JDEBUGF("position in file: %d buffer fill: %d\n", 2132 (int)lseek(p_jpeg->fd, 0, SEEK_CUR), p_jpeg->buf_left); 2133#endif 2134 if (status < 0) 2135 return status; 2136 if ((status & (DQT | SOF0)) != (DQT | SOF0)) 2137 return -(status * 16); 2138 if (!(status & DHT)) /* if no Huffman table present: */ 2139 default_huff_tbl(p_jpeg); /* use default */ 2140 fix_headers(p_jpeg); /* derive Huffman and other lookup-tables */ 2141 2142 /*the dim array in rockbox is limited to 2^15-1 pixels, so we cannot resize 2143 images larger than this without overflowing */ 2144 if(p_jpeg->x_size > 32767 || p_jpeg->y_size > 32767) 2145 { 2146 JDEBUGF("Aborting resize of image > 32767 pixels\n"); 2147 return -1; 2148 } 2149 2150 src_dim.width = p_jpeg->x_size; 2151 src_dim.height = p_jpeg->y_size; 2152 if (format & FORMAT_RESIZE) 2153 resize = true; 2154 if (format & FORMAT_DITHER) 2155 dither = true; 2156#ifdef HAVE_LCD_COLOR 2157 bm->alpha_offset = 0; /* no alpha channel */ 2158#endif 2159 if (resize) { 2160 struct dim resize_dim = { 2161 .width = bm->width, 2162 .height = bm->height, 2163 }; 2164 if (format & FORMAT_KEEP_ASPECT) 2165 recalc_dimension(&resize_dim, &src_dim); 2166 bm->width = resize_dim.width; 2167 bm->height = resize_dim.height; 2168 } else { 2169 bm->width = p_jpeg->x_size; 2170 bm->height = p_jpeg->y_size; 2171 } 2172 p_jpeg->h_scale[0] = calc_scale(p_jpeg->x_size, bm->width); 2173 p_jpeg->v_scale[0] = calc_scale(p_jpeg->y_size, bm->height); 2174 JDEBUGF("luma IDCT size: %dx%d\n", BIT_N(p_jpeg->h_scale[0]), 2175 BIT_N(p_jpeg->v_scale[0])); 2176 if ((p_jpeg->x_size << p_jpeg->h_scale[0]) >> 3 == bm->width && 2177 (p_jpeg->y_size << p_jpeg->v_scale[0]) >> 3 == bm->height) 2178 resize = false; 2179#ifdef HAVE_LCD_COLOR 2180 p_jpeg->h_scale[1] = p_jpeg->h_scale[0] + 2181 p_jpeg->frameheader[0].horizontal_sampling - 1; 2182 p_jpeg->v_scale[1] = p_jpeg->v_scale[0] + 2183 p_jpeg->frameheader[0].vertical_sampling - 1; 2184 JDEBUGF("chroma IDCT size: %dx%d\n", BIT_N(p_jpeg->h_scale[1]), 2185 BIT_N(p_jpeg->v_scale[1])); 2186#endif 2187 JDEBUGF("scaling from %dx%d -> %dx%d\n", 2188 (p_jpeg->x_size << p_jpeg->h_scale[0]) >> 3, 2189 (p_jpeg->y_size << p_jpeg->v_scale[0]) >> 3, 2190 bm->width, bm->height); 2191 fix_quant_tables(p_jpeg); 2192 int decode_w = BIT_N(p_jpeg->h_scale[0]) - 1; 2193 int decode_h = BIT_N(p_jpeg->v_scale[0]) - 1; 2194 src_dim.width = (p_jpeg->x_size << p_jpeg->h_scale[0]) >> 3; 2195 src_dim.height = (p_jpeg->y_size << p_jpeg->v_scale[0]) >> 3; 2196#ifdef JPEG_IDCT_TRANSPOSE 2197 if (p_jpeg->v_scale[0] > 2) 2198 p_jpeg->zero_need[0] = (decode_w << 3) + decode_h; 2199 else 2200#endif 2201 p_jpeg->zero_need[0] = (decode_h << 3) + decode_w; 2202 p_jpeg->k_need[0] = zig[(decode_h << 3) + decode_w]; 2203 JDEBUGF("need luma components to %d\n", p_jpeg->k_need[0]); 2204#ifdef HAVE_LCD_COLOR 2205 decode_w = BIT_N(MIN(p_jpeg->h_scale[1],3)) - 1; 2206 decode_h = BIT_N(MIN(p_jpeg->v_scale[1],3)) - 1; 2207 if (p_jpeg->v_scale[1] > 2) 2208 p_jpeg->zero_need[1] = (decode_w << 3) + decode_h; 2209 else 2210 p_jpeg->zero_need[1] = (decode_h << 3) + decode_w; 2211 p_jpeg->k_need[1] = zig[(decode_h << 3) + decode_w]; 2212 JDEBUGF("need chroma components to %d\n", p_jpeg->k_need[1]); 2213#endif 2214 if (cformat) 2215 bm_size = cformat->get_size(bm); 2216 else 2217 bm_size = BM_SIZE(bm->width,bm->height,FORMAT_NATIVE,false); 2218 2219 char *buf_start = (char *)bm->data + bm_size; 2220 char *buf_end = (char *)bm->data + maxsize; 2221 bool return_size = format & FORMAT_RETURN_SIZE; 2222#ifndef JPEG_FROM_MEM 2223 ALIGN_BUFFER(buf_start, maxsize, sizeof(long)); 2224 if (!return_size) 2225 { 2226 if (maxsize < (int)sizeof(struct jpeg)) 2227 return -1; 2228 memmove(buf_start, p_jpeg, sizeof(struct jpeg)); 2229 p_jpeg = (struct jpeg *)buf_start; 2230 } 2231 buf_start += sizeof(struct jpeg); 2232#endif 2233 maxsize = buf_end - buf_start; 2234#ifdef HAVE_LCD_COLOR 2235 int decode_buf_size = (p_jpeg->x_mbl << p_jpeg->h_scale[1]) 2236 << p_jpeg->v_scale[1]; 2237#else 2238 int decode_buf_size = (p_jpeg->x_mbl << p_jpeg->h_scale[0]) 2239 << p_jpeg->v_scale[0]; 2240 decode_buf_size <<= p_jpeg->frameheader[0].horizontal_sampling + 2241 p_jpeg->frameheader[0].vertical_sampling - 2; 2242#endif 2243 decode_buf_size *= JPEG_PIX_SZ; 2244 JDEBUGF("decode buffer size: %d\n", decode_buf_size); 2245 if (return_size) 2246 { 2247 return (buf_start - (char *) bm->data) + decode_buf_size 2248 + (resize 2249 ? 2250 /* buffer for 1 line + 2 spare lines */ 2251#ifdef HAVE_LCD_COLOR 2252 sizeof(struct uint32_argb) 2253#else 2254 sizeof(uint32_t) 2255#endif 2256 * 3 * bm->width 2257 : 0); 2258 } 2259 2260 if (buf_end - buf_start < decode_buf_size) 2261 return -1; 2262 2263 fix_huff_tables(p_jpeg); 2264 2265 p_jpeg->img_buf = (jpeg_pix_t *)buf_start; 2266 buf_start += decode_buf_size; 2267 maxsize = buf_end - buf_start; 2268 memset(p_jpeg->img_buf, 0, decode_buf_size); 2269 p_jpeg->mcu_row = 0; 2270 p_jpeg->restart = p_jpeg->restart_interval; 2271 rset.rowstart = 0; 2272 rset.rowstop = bm->height; 2273 rset.rowstep = 1; 2274 p_jpeg->resize = resize; 2275 if (resize) 2276 { 2277 if (resize_on_load(bm, dither, &src_dim, &rset, buf_start, maxsize, 2278 cformat, IF_PIX_FMT(p_jpeg->blocks == 1 ? 0 : 1,) store_row_jpeg, 2279 p_jpeg)) 2280 return bm_size; 2281 } else { 2282 int row; 2283 struct scaler_context ctx = { 2284 .bm = bm, 2285 .dither = dither, 2286 }; 2287#if LCD_DEPTH > 1 2288 void (*output_row_8)(uint32_t, void*, struct scaler_context*) = 2289 output_row_8_native; 2290#elif defined(PLUGIN) 2291 void (*output_row_8)(uint32_t, void*, struct scaler_context*) = NULL; 2292#endif 2293#if LCD_DEPTH > 1 || defined(PLUGIN) 2294 if (cformat) 2295 output_row_8 = cformat->output_row_8; 2296#endif 2297 struct img_part *part; 2298 for (row = 0; row < bm->height; row++) 2299 { 2300 part = store_row_jpeg(p_jpeg); 2301#ifdef HAVE_LCD_COLOR 2302 if (p_jpeg->blocks > 1) 2303 { 2304 struct uint8_rgb *qp = part->buf; 2305 struct uint8_rgb *end = qp + bm->width; 2306 uint8_t y, u, v; 2307 unsigned r, g, b; 2308 for (; qp < end; qp++) 2309 { 2310 y = qp->blue; 2311 u = qp->green; 2312 v = qp->red; 2313 yuv_to_rgb(y, u, v, &r, &g, &b); 2314 qp->red = r; 2315 qp->blue = b; 2316 qp->green = g; 2317 } 2318 } 2319#endif 2320 output_row_8(row, part->buf, &ctx); 2321 } 2322 return bm_size; 2323 } 2324 return 0; 2325} 2326 2327#ifndef JPEG_FROM_MEM 2328int read_jpeg_fd(int fd, 2329 struct bitmap *bm, 2330 int maxsize, 2331 int format, 2332 const struct custom_format *cformat) 2333{ 2334 return clip_jpeg_fd(fd, 0, 0, bm, maxsize, format, cformat); 2335} 2336#endif 2337 2338const size_t JPEG_DECODE_OVERHEAD = 2339 /* Reserve an arbitrary amount for the decode buffer 2340 * FIXME: Somebody who knows what they're doing should look at this */ 2341 (38 * 1024) 2342#ifndef JPEG_FROM_MEM 2343 /* Unless the struct jpeg is defined statically, we need to allocate 2344 * it in the bitmap buffer as well */ 2345 + sizeof(struct jpeg) 2346#endif 2347 ; 2348 2349/**************** end JPEG code ********************/