OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Small jpeg decoder library |
| 3 * |
| 4 * Copyright (c) 2006, Luc Saillard <luc@saillard.org> |
| 5 * Copyright (c) 2012 Intel Corporation. |
| 6 * All rights reserved. |
| 7 * Redistribution and use in source and binary forms, with or without |
| 8 * modification, are permitted provided that the following conditions are met: |
| 9 * |
| 10 * - Redistributions of source code must retain the above copyright notice, |
| 11 * this list of conditions and the following disclaimer. |
| 12 * |
| 13 * - Redistributions in binary form must reproduce the above copyright notice, |
| 14 * this list of conditions and the following disclaimer in the documentation |
| 15 * and/or other materials provided with the distribution. |
| 16 * |
| 17 * - Neither the name of the author nor the names of its contributors may be |
| 18 * used to endorse or promote products derived from this software without |
| 19 * specific prior written permission. |
| 20 * |
| 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 * POSSIBILITY OF SUCH DAMAGE. |
| 32 * |
| 33 */ |
| 34 |
| 35 #include <stdio.h> |
| 36 #include <stdlib.h> |
| 37 #include <string.h> |
| 38 #include <stdint.h> |
| 39 #include <errno.h> |
| 40 |
| 41 #include "tinyjpeg.h" |
| 42 #include "tinyjpeg-internal.h" |
| 43 |
| 44 // for libva |
| 45 #include <unistd.h> |
| 46 #include <sys/types.h> |
| 47 #include <sys/stat.h> |
| 48 #include <fcntl.h> |
| 49 #include <assert.h> |
| 50 #include <va/va.h> |
| 51 #include <va/va_x11.h> |
| 52 #include <X11/Xlib.h> |
| 53 |
| 54 |
| 55 #define cY 0 |
| 56 #define cCb 1 |
| 57 #define cCr 2 |
| 58 |
| 59 #define BLACK_Y 0 |
| 60 #define BLACK_U 127 |
| 61 #define BLACK_V 127 |
| 62 |
| 63 #if DEBUG |
| 64 #define trace(fmt, args...) do { \ |
| 65 fprintf(stderr, fmt, ## args); \ |
| 66 fflush(stderr); \ |
| 67 } while(0) |
| 68 #else |
| 69 #define trace(fmt, args...) do { } while (0) |
| 70 #endif |
| 71 #define error(fmt, args...) do { \ |
| 72 snprintf(error_string, sizeof(error_string), fmt, ## args); \ |
| 73 return -1; \ |
| 74 } while(0) |
| 75 |
| 76 /* Global variable to return the last error found while deconding */ |
| 77 static char error_string[256]; |
| 78 static VAHuffmanTableBufferJPEG default_huffman_table_param={ |
| 79 huffman_table: |
| 80 { |
| 81 // lumiance component |
| 82 { |
| 83 dc_bits:{0,1,5,1,1,1,1,1,1,0,0,0}, // 12 bits is ok for baseline pro
file |
| 84 dc_huffval:{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0
x0b}, |
| 85 ac_bits:{0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,125}, |
| 86 ac_huffval:{ |
| 87 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, |
| 88 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, |
| 89 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, |
| 90 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, |
| 91 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, |
| 92 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, |
| 93 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, |
| 94 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, |
| 95 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, |
| 96 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, |
| 97 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, |
| 98 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, |
| 99 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, |
| 100 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, |
| 101 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, |
| 102 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, |
| 103 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, |
| 104 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, |
| 105 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, |
| 106 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, |
| 107 0xf9, 0xfa |
| 108 },/*,0xonly,0xthe,0xfirst,0x162,0xbytes,0xare,0xavailable,0x*/ |
| 109 }, |
| 110 // chrom component |
| 111 { |
| 112 dc_bits:{0,3,1,1,1,1,1,1,1,1,1,0}, // 12 bits is ok for baseline pro
file |
| 113 dc_huffval:{0,1,2,3,4,5,6,7,8,9,0xa,0xb}, |
| 114 ac_bits:{0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,119}, |
| 115 ac_huffval:{ |
| 116 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, |
| 117 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, |
| 118 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, |
| 119 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, |
| 120 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, |
| 121 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, |
| 122 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, |
| 123 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, |
| 124 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, |
| 125 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, |
| 126 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, |
| 127 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, |
| 128 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, |
| 129 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, |
| 130 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, |
| 131 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, |
| 132 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, |
| 133 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, |
| 134 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, |
| 135 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, |
| 136 0xf9, 0xfa |
| 137 },/*,0xonly,0xthe,0xfirst,0x162,0xbytes,0xare,0xavailable,0x*/ |
| 138 }, |
| 139 } |
| 140 }; |
| 141 |
| 142 #define be16_to_cpu(x) (((x)[0]<<8)|(x)[1]) |
| 143 |
| 144 |
| 145 static int build_default_huffman_tables(struct jdec_private *priv) |
| 146 { |
| 147 int i = 0; |
| 148 if (priv->default_huffman_table_initialized) |
| 149 return 0; |
| 150 |
| 151 for (i = 0; i < 4; i++) { |
| 152 memcpy(priv->HTDC[i].bits, default_huffman_table_param.huffman_table[i].
dc_bits, 16); |
| 153 memcpy(priv->HTDC[i].values, default_huffman_table_param.huffman_table[i
].dc_huffval, 16); |
| 154 memcpy(priv->HTAC[i].bits, default_huffman_table_param.huffman_table[i].
ac_bits, 16); |
| 155 memcpy(priv->HTAC[i].values, default_huffman_table_param.huffman_table[i
].ac_huffval, 256); |
| 156 } |
| 157 priv->default_huffman_table_initialized = 1; |
| 158 return 0; |
| 159 } |
| 160 |
| 161 |
| 162 static void print_SOF(const unsigned char *stream) |
| 163 { |
| 164 int width, height, nr_components, precision; |
| 165 #if DEBUG |
| 166 const char *nr_components_to_string[] = { |
| 167 "????", |
| 168 "Grayscale", |
| 169 "????", |
| 170 "YCbCr", |
| 171 "CYMK" |
| 172 }; |
| 173 #endif |
| 174 |
| 175 precision = stream[2]; |
| 176 height = be16_to_cpu(stream+3); |
| 177 width = be16_to_cpu(stream+5); |
| 178 nr_components = stream[7]; |
| 179 |
| 180 trace("> SOF marker\n"); |
| 181 trace("Size:%dx%d nr_components:%d (%s) precision:%d\n", |
| 182 width, height, |
| 183 nr_components, nr_components_to_string[nr_components], |
| 184 precision); |
| 185 } |
| 186 |
| 187 static int parse_DQT(struct jdec_private *priv, const unsigned char *stream) |
| 188 { |
| 189 int qi; |
| 190 const unsigned char *dqt_block_end; |
| 191 |
| 192 trace("> DQT marker\n"); |
| 193 dqt_block_end = stream + be16_to_cpu(stream); |
| 194 stream += 2; /* Skip length */ |
| 195 |
| 196 while (stream < dqt_block_end) |
| 197 { |
| 198 qi = *stream++; |
| 199 #if SANITY_CHECK |
| 200 if (qi>>4) |
| 201 error("16 bits quantization table is not supported\n"); |
| 202 if (qi>4) |
| 203 error("No more 4 quantization table is supported (got %d)\n", qi); |
| 204 #endif |
| 205 memcpy(priv->Q_tables[qi&0x0F], stream, 64); |
| 206 stream += 64; |
| 207 } |
| 208 trace("< DQT marker\n"); |
| 209 return 0; |
| 210 } |
| 211 |
| 212 static int parse_SOF(struct jdec_private *priv, const unsigned char *stream) |
| 213 { |
| 214 int i, width, height, nr_components, cid, sampling_factor; |
| 215 unsigned char Q_table; |
| 216 struct component *c; |
| 217 |
| 218 trace("> SOF marker\n"); |
| 219 print_SOF(stream); |
| 220 |
| 221 height = be16_to_cpu(stream+3); |
| 222 width = be16_to_cpu(stream+5); |
| 223 nr_components = stream[7]; |
| 224 priv->nf_components = nr_components; |
| 225 #if SANITY_CHECK |
| 226 if (stream[2] != 8) |
| 227 error("Precision other than 8 is not supported\n"); |
| 228 if (width>JPEG_MAX_WIDTH || height>JPEG_MAX_HEIGHT) |
| 229 printf("WARNING:Width and Height (%dx%d) seems suspicious\n", width, height)
; |
| 230 if (nr_components != 3) |
| 231 printf("ERROR:We only support YUV images\n"); |
| 232 if (height%16) |
| 233 printf("WARNING:Height need to be a multiple of 16 (current height is %d)\n"
, height); |
| 234 if (width%16) |
| 235 printf("WARNING:Width need to be a multiple of 16 (current Width is %d)\n",
width); |
| 236 #endif |
| 237 stream += 8; |
| 238 for (i=0; i<nr_components; i++) { |
| 239 cid = *stream++; |
| 240 sampling_factor = *stream++; |
| 241 Q_table = *stream++; |
| 242 c = &priv->component_infos[i]; |
| 243 c->cid = cid; |
| 244 if (Q_table >= COMPONENTS) |
| 245 error("Bad Quantization table index (got %d, max allowed %d)\n", Q_table,
COMPONENTS-1); |
| 246 c->Vfactor = sampling_factor&0xf; |
| 247 c->Hfactor = sampling_factor>>4; |
| 248 c->quant_table_index = Q_table; |
| 249 trace("Component:%d factor:%dx%d Quantization table:%d\n", |
| 250 cid, c->Hfactor, c->Vfactor, Q_table ); |
| 251 |
| 252 } |
| 253 priv->width = width; |
| 254 priv->height = height; |
| 255 |
| 256 trace("< SOF marker\n"); |
| 257 |
| 258 return 0; |
| 259 } |
| 260 |
| 261 static int parse_SOS(struct jdec_private *priv, const unsigned char *stream) |
| 262 { |
| 263 unsigned int i, cid, table; |
| 264 unsigned int nr_components = stream[2]; |
| 265 |
| 266 trace("> SOS marker\n"); |
| 267 |
| 268 priv->cur_sos.nr_components= nr_components; |
| 269 |
| 270 stream += 3; |
| 271 for (i=0;i<nr_components;i++) { |
| 272 cid = *stream++; |
| 273 table = *stream++; |
| 274 priv->cur_sos.components[i].component_id = cid; |
| 275 priv->cur_sos.components[i].dc_selector = ((table>>4)&0x0F); |
| 276 priv->cur_sos.components[i].ac_selector = (table&0x0F); |
| 277 #if SANITY_CHECK |
| 278 if ((table&0xf)>=4) |
| 279 error("We do not support more than 2 AC Huffman table\n"); |
| 280 if ((table>>4)>=4) |
| 281 error("We do not support more than 2 DC Huffman table\n"); |
| 282 if (cid != priv->component_infos[i].cid) |
| 283 error("SOS cid order (%d:%d) isn't compatible with the SOF marker (%d:%d
)\n", |
| 284 i, cid, i, priv->component_infos[i].cid); |
| 285 trace("ComponentId:%d tableAC:%d tableDC:%d\n", cid, table&0xf, table>>4); |
| 286 #endif |
| 287 } |
| 288 priv->stream = stream+3; |
| 289 trace("< SOS marker\n"); |
| 290 return 0; |
| 291 } |
| 292 |
| 293 int tinyjpeg_parse_SOS(struct jdec_private *priv, const unsigned char *stream) |
| 294 { |
| 295 return parse_SOS(priv, stream); |
| 296 } |
| 297 |
| 298 |
| 299 static int parse_DHT(struct jdec_private *priv, const unsigned char *stream) |
| 300 { |
| 301 unsigned int count, i; |
| 302 int length, index; |
| 303 unsigned char Tc, Th; |
| 304 |
| 305 length = be16_to_cpu(stream) - 2; |
| 306 stream += 2; /* Skip length */ |
| 307 |
| 308 trace("> DHT marker (length=%d)\n", length); |
| 309 |
| 310 while (length>0) { |
| 311 index = *stream++; |
| 312 |
| 313 Tc = index & 0xf0; // it is not important to <<4 |
| 314 Th = index & 0x0f; |
| 315 if (Tc) { |
| 316 memcpy(priv->HTAC[index & 0xf].bits, stream, 16); |
| 317 } |
| 318 else { |
| 319 memcpy(priv->HTDC[index & 0xf].bits, stream, 12); |
| 320 } |
| 321 |
| 322 count = 0; |
| 323 for (i=0; i<16; i++) { |
| 324 count += *stream++; |
| 325 } |
| 326 |
| 327 #if SANITY_CHECK |
| 328 if (count >= HUFFMAN_BITS_SIZE) |
| 329 error("No more than %d bytes is allowed to describe a huffman table", HUF
FMAN_BITS_SIZE); |
| 330 if ( (index &0xf) >= HUFFMAN_TABLES) |
| 331 error("No more than %d Huffman tables is supported (got %d)\n", HUFFMAN_T
ABLES, index&0xf); |
| 332 trace("Huffman table %s[%d] length=%d\n", (index&0xf0)?"AC":"DC", index&0xf
, count); |
| 333 #endif |
| 334 |
| 335 if (Tc) { |
| 336 memcpy(priv->HTAC[index & 0xf].values, stream, count); |
| 337 } |
| 338 else { |
| 339 memcpy(priv->HTDC[index & 0xf].values, stream, count); |
| 340 } |
| 341 |
| 342 length -= 1; |
| 343 length -= 16; |
| 344 length -= count; |
| 345 stream += count; |
| 346 } |
| 347 trace("< DHT marker\n"); |
| 348 return 0; |
| 349 } |
| 350 static int parse_DRI(struct jdec_private *priv, const unsigned char *stream) |
| 351 { |
| 352 unsigned int length; |
| 353 |
| 354 trace("> DRI marker\n"); |
| 355 |
| 356 length = be16_to_cpu(stream); |
| 357 |
| 358 #if SANITY_CHECK |
| 359 if (length != 4) |
| 360 error("Length of DRI marker need to be 4\n"); |
| 361 #endif |
| 362 |
| 363 priv->restart_interval = be16_to_cpu(stream+2); |
| 364 |
| 365 #if DEBUG |
| 366 trace("Restart interval = %d\n", priv->restart_interval); |
| 367 #endif |
| 368 |
| 369 trace("< DRI marker\n"); |
| 370 |
| 371 return 0; |
| 372 } |
| 373 |
| 374 |
| 375 static int parse_JFIF(struct jdec_private *priv, const unsigned char *stream) |
| 376 { |
| 377 int chuck_len; |
| 378 int marker; |
| 379 int sos_marker_found = 0; |
| 380 int dht_marker_found = 0; |
| 381 int dqt_marker_found = 0; |
| 382 const unsigned char *next_chunck; |
| 383 |
| 384 /* Parse marker */ |
| 385 while (!sos_marker_found) |
| 386 { |
| 387 if (*stream++ != 0xff) |
| 388 goto bogus_jpeg_format; |
| 389 /* Skip any padding ff byte (this is normal) */ |
| 390 while (*stream == 0xff) |
| 391 stream++; |
| 392 |
| 393 marker = *stream++; |
| 394 chuck_len = be16_to_cpu(stream); |
| 395 next_chunck = stream + chuck_len; |
| 396 switch (marker) |
| 397 { |
| 398 case SOF: |
| 399 if (parse_SOF(priv, stream) < 0) |
| 400 return -1; |
| 401 break; |
| 402 case DQT: |
| 403 if (parse_DQT(priv, stream) < 0) |
| 404 return -1; |
| 405 dqt_marker_found = 1; |
| 406 break; |
| 407 case SOS: |
| 408 if (parse_SOS(priv, stream) < 0) |
| 409 return -1; |
| 410 sos_marker_found = 1; |
| 411 break; |
| 412 case DHT: |
| 413 if (parse_DHT(priv, stream) < 0) |
| 414 return -1; |
| 415 dht_marker_found = 1; |
| 416 break; |
| 417 case DRI: |
| 418 if (parse_DRI(priv, stream) < 0) |
| 419 return -1; |
| 420 break; |
| 421 default: |
| 422 trace("> Unknown marker %2.2x\n", marker); |
| 423 break; |
| 424 } |
| 425 |
| 426 stream = next_chunck; |
| 427 } |
| 428 |
| 429 if (!dht_marker_found) { |
| 430 trace("No Huffman table loaded, using the default one\n"); |
| 431 build_default_huffman_tables(priv); |
| 432 } |
| 433 if (!dqt_marker_found) { |
| 434 error("ERROR:No Quantization table loaded, using the default one\n"); |
| 435 } |
| 436 |
| 437 #ifdef SANITY_CHECK |
| 438 if ( (priv->component_infos[cY].Hfactor < priv->component_infos[cCb].Hfactor
) |
| 439 || (priv->component_infos[cY].Hfactor < priv->component_infos[cCr].Hfactor
)) |
| 440 error("Horizontal sampling factor for Y should be greater than horitontal sa
mpling factor for Cb or Cr\n"); |
| 441 if ( (priv->component_infos[cY].Vfactor < priv->component_infos[cCb].Vfactor
) |
| 442 || (priv->component_infos[cY].Vfactor < priv->component_infos[cCr].Vfactor
)) |
| 443 error("Vertical sampling factor for Y should be greater than vertical sampli
ng factor for Cb or Cr\n"); |
| 444 if ( (priv->component_infos[cCb].Hfactor!=1) |
| 445 || (priv->component_infos[cCr].Hfactor!=1) |
| 446 || (priv->component_infos[cCb].Vfactor!=1) |
| 447 || (priv->component_infos[cCr].Vfactor!=1)) |
| 448 printf("ERROR:Sampling other than 1x1 for Cr and Cb is not supported"); |
| 449 #endif |
| 450 |
| 451 return 0; |
| 452 bogus_jpeg_format: |
| 453 trace("Bogus jpeg format\n"); |
| 454 return -1; |
| 455 } |
| 456 |
| 457 /******************************************************************************* |
| 458 * |
| 459 * Functions exported of the library. |
| 460 * |
| 461 * Note: Some applications can access directly to internal pointer of the |
| 462 * structure. It's is not recommended, but if you have many images to |
| 463 * uncompress with the same parameters, some functions can be called to speedup |
| 464 * the decoding. |
| 465 * |
| 466 ******************************************************************************/ |
| 467 |
| 468 /** |
| 469 * Allocate a new tinyjpeg decoder object. |
| 470 * |
| 471 * Before calling any other functions, an object need to be called. |
| 472 */ |
| 473 struct jdec_private *tinyjpeg_init(void) |
| 474 { |
| 475 struct jdec_private *priv; |
| 476 |
| 477 priv = (struct jdec_private *)calloc(1, sizeof(struct jdec_private)); |
| 478 if (priv == NULL) |
| 479 return NULL; |
| 480 return priv; |
| 481 } |
| 482 |
| 483 /** |
| 484 * Free a tinyjpeg object. |
| 485 * |
| 486 * No others function can be called after this one. |
| 487 */ |
| 488 void tinyjpeg_free(struct jdec_private *priv) |
| 489 { |
| 490 free(priv); |
| 491 } |
| 492 |
| 493 /** |
| 494 * Initialize the tinyjpeg object and prepare the decoding of the stream. |
| 495 * |
| 496 * Check if the jpeg can be decoded with this jpeg decoder. |
| 497 * Fill some table used for preprocessing. |
| 498 */ |
| 499 int tinyjpeg_parse_header(struct jdec_private *priv, const unsigned char *buf, u
nsigned int size) |
| 500 { |
| 501 int ret; |
| 502 |
| 503 /* Identify the file */ |
| 504 if ((buf[0] != 0xFF) || (buf[1] != SOI)) |
| 505 error("Not a JPG file ?\n"); |
| 506 |
| 507 priv->stream_begin = buf+2; |
| 508 priv->stream_length = size-2; |
| 509 priv->stream_end = priv->stream_begin + priv->stream_length; |
| 510 |
| 511 ret = parse_JFIF(priv, priv->stream_begin); |
| 512 |
| 513 return ret; |
| 514 } |
| 515 |
| 516 |
| 517 int tinyjpeg_decode(struct jdec_private *priv) |
| 518 { |
| 519 #define CHECK_VASTATUS(va_status,func) \ |
| 520 if (va_status != VA_STATUS_SUCCESS) { \ |
| 521 fprintf(stderr,"%s:%s (%d) failed,exit\n", __func__, func, __LINE__); \ |
| 522 exit(1); \ |
| 523 } |
| 524 |
| 525 VAEntrypoint entrypoints[5]; |
| 526 int num_entrypoints,vld_entrypoint; |
| 527 VAConfigAttrib attrib; |
| 528 VAConfigID config_id; |
| 529 VASurfaceID surface_id; |
| 530 VAContextID context_id; |
| 531 VABufferID pic_param_buf,iqmatrix_buf,huffmantable_buf,slice_param_buf,slice
_data_buf; |
| 532 int major_ver, minor_ver; |
| 533 Display *x11_display; |
| 534 VADisplay va_dpy; |
| 535 VAStatus va_status; |
| 536 int max_h_factor, max_v_factor; |
| 537 int putsurface=1; |
| 538 unsigned int i; |
| 539 |
| 540 x11_display = XOpenDisplay(":0.0"); |
| 541 |
| 542 if (x11_display == NULL) { |
| 543 fprintf(stderr, "Can't connect X server!\n"); |
| 544 exit(-1); |
| 545 } |
| 546 |
| 547 assert(x11_display); |
| 548 |
| 549 va_dpy = vaGetDisplay(x11_display); |
| 550 va_status = vaInitialize(va_dpy, &major_ver, &minor_ver); |
| 551 assert(va_status == VA_STATUS_SUCCESS); |
| 552 |
| 553 va_status = vaQueryConfigEntrypoints(va_dpy, VAProfileJPEGBaseline, entrypoi
nts, |
| 554 &num_entrypoints); |
| 555 CHECK_VASTATUS(va_status, "vaQueryConfigEntrypoints"); |
| 556 |
| 557 for (vld_entrypoint = 0; vld_entrypoint < num_entrypoints; vld_entrypoint++)
{ |
| 558 if (entrypoints[vld_entrypoint] == VAEntrypointVLD) |
| 559 break; |
| 560 } |
| 561 if (vld_entrypoint == num_entrypoints) { |
| 562 /* not find VLD entry point */ |
| 563 assert(0); |
| 564 } |
| 565 |
| 566 /* Assuming finding VLD, find out the format for the render target */ |
| 567 attrib.type = VAConfigAttribRTFormat; |
| 568 vaGetConfigAttributes(va_dpy, VAProfileJPEGBaseline, VAEntrypointVLD, |
| 569 &attrib, 1); |
| 570 if ((attrib.value & VA_RT_FORMAT_YUV420) == 0) { |
| 571 /* not find desired YUV420 RT format */ |
| 572 assert(0); |
| 573 } |
| 574 |
| 575 va_status = vaCreateConfig(va_dpy, VAProfileJPEGBaseline, VAEntrypointVLD, |
| 576 &attrib, 1,&config_id); |
| 577 CHECK_VASTATUS(va_status, "vaQueryConfigEntrypoints"); |
| 578 |
| 579 va_status = vaCreateSurfaces(va_dpy,priv->width,priv->height, //alignment? |
| 580 VA_RT_FORMAT_YUV420, 1, &surface_id); |
| 581 CHECK_VASTATUS(va_status, "vaCreateSurfaces"); |
| 582 |
| 583 /* Create a context for this decode pipe */ |
| 584 va_status = vaCreateContext(va_dpy, config_id, |
| 585 priv->width, priv->height, // alignment? |
| 586 VA_PROGRESSIVE, |
| 587 &surface_id, |
| 588 1, |
| 589 &context_id); |
| 590 CHECK_VASTATUS(va_status, "vaCreateContext"); |
| 591 |
| 592 |
| 593 VAPictureParameterBufferJPEG pic_param; |
| 594 memset(&pic_param, 0, sizeof(pic_param)); |
| 595 pic_param.type = VA_JPEG_SOF0; // tinyjpeg support baseline profile only, do
es it match va capability? |
| 596 pic_param.sample_precision = 8; // tinyjpeg support baseline profile only, d
oes it match va capability? |
| 597 pic_param.image_width = priv->width; |
| 598 pic_param.image_height = priv->height; |
| 599 pic_param.num_components = priv->nf_components; |
| 600 |
| 601 for (i=0; i<pic_param.num_components; i++) { // tinyjpeg support 3 component
s only, does it match va? |
| 602 pic_param.components[i].component_id = priv->component_infos[i].cid; |
| 603 pic_param.components[i].h_sampling_factor = priv->component_infos[i].Hfa
ctor; |
| 604 pic_param.components[i].v_sampling_factor = priv->component_infos[i].Vfa
ctor; |
| 605 pic_param.components[i].quantiser_table_selector = priv->component_infos
[i].quant_table_index; |
| 606 } |
| 607 |
| 608 pic_param.roi.enabled = 0; |
| 609 pic_param.roi.start_x = 0; |
| 610 pic_param.roi.start_y = 0; |
| 611 pic_param.roi.end_x = 0; |
| 612 pic_param.roi.end_y = 0; |
| 613 pic_param.rotation = 0; |
| 614 |
| 615 va_status = vaCreateBuffer(va_dpy, context_id, |
| 616 VAPictureParameterBufferType, // VAPictureParamete
rBufferJPEG? |
| 617 sizeof(VAPictureParameterBufferJPEG), |
| 618 1, &pic_param, |
| 619 &pic_param_buf); |
| 620 CHECK_VASTATUS(va_status, "vaCreateBuffer"); |
| 621 |
| 622 VAIQMatrixBufferJPEG iq_matrix; |
| 623 // todo, only mask it if non-default quant matrix is used. do we need build
default quant matrix? |
| 624 memset(&iq_matrix, 0, sizeof(VAIQMatrixBufferJPEG)); |
| 625 for (i = 0; i < COMPONENTS; i++) { |
| 626 iq_matrix.precision[i] = 0; |
| 627 memcpy(iq_matrix.quantiser_matrix[i], priv->Q_tables[i], 64); |
| 628 } |
| 629 va_status = vaCreateBuffer(va_dpy, context_id, |
| 630 VAIQMatrixBufferType, // VAIQMatrixBufferJPEG? |
| 631 sizeof(VAIQMatrixBufferJPEG), |
| 632 1, &iq_matrix, |
| 633 &iqmatrix_buf ); |
| 634 CHECK_VASTATUS(va_status, "vaCreateBuffer"); |
| 635 |
| 636 VAHuffmanTableBufferJPEG huffman_table; |
| 637 memset(&huffman_table, 0, sizeof(VAHuffmanTableBufferJPEG)); |
| 638 for (i = 0; i < COMPONENTS; i++) { |
| 639 memcpy(huffman_table.huffman_table[i].dc_bits, priv->HTDC[i].bits, 16); |
| 640 memcpy(huffman_table.huffman_table[i].dc_huffval, priv->HTDC[i].values,
16); |
| 641 memcpy(huffman_table.huffman_table[i].ac_bits, priv->HTAC[i].bits, 16); |
| 642 memcpy(huffman_table.huffman_table[i].ac_huffval, priv->HTAC[i].values,
256); |
| 643 } |
| 644 |
| 645 va_status = vaCreateBuffer(va_dpy, context_id, |
| 646 VAHuffmanTableBufferType, // VAHuffmanTableBufferJ
PEG? |
| 647 sizeof(VAHuffmanTableBufferJPEG), |
| 648 1, &huffman_table, |
| 649 &huffmantable_buf ); |
| 650 CHECK_VASTATUS(va_status, "vaCreateBuffer"); |
| 651 |
| 652 // one slice for whole image? |
| 653 max_h_factor = priv->component_infos[0].Hfactor; |
| 654 max_v_factor = priv->component_infos[0].Vfactor; |
| 655 static VASliceParameterBufferJPEG slice_param; |
| 656 slice_param.slice_data_size = priv->stream_end - priv->stream; |
| 657 slice_param.slice_data_offset = 0; |
| 658 slice_param.slice_data_flag = VA_SLICE_DATA_FLAG_ALL; |
| 659 slice_param.slice_horizontal_position = 0; |
| 660 slice_param.slice_vertical_position = 0; |
| 661 slice_param.num_components = priv->cur_sos.nr_components; |
| 662 for (i = 0; i < slice_param.num_components; i++) { |
| 663 slice_param.components[i].component_id = priv->cur_sos.components[i].com
ponent_id; /* FIXME: set to values specified in SOS */ |
| 664 slice_param.components[i].dc_selector = priv->cur_sos.components[i].dc_s
elector; /* FIXME: set to values specified in SOS */ |
| 665 slice_param.components[i].ac_selector = priv->cur_sos.components[i].ac_s
elector; /* FIXME: set to values specified in SOS */ |
| 666 } |
| 667 slice_param.restart_interval = priv->restart_interval; |
| 668 slice_param.num_mcus = ((priv->width+max_h_factor*8-1)/(max_h_factor*8))* |
| 669 ((priv->height+max_v_factor*8-1)/(max_v_factor*8)); //
?? 720/16? |
| 670 |
| 671 va_status = vaCreateBuffer(va_dpy, context_id, |
| 672 VASliceParameterBufferType, // VASliceParameterBuf
ferJPEG? |
| 673 sizeof(VASliceParameterBufferJPEG), |
| 674 1, |
| 675 &slice_param, &slice_param_buf); |
| 676 CHECK_VASTATUS(va_status, "vaCreateBuffer"); |
| 677 |
| 678 va_status = vaCreateBuffer(va_dpy, context_id, |
| 679 VASliceDataBufferType, |
| 680 priv->stream_end - priv->stream, |
| 681 1, |
| 682 (void*)priv->stream, // jpeg_clip, |
| 683 &slice_data_buf); |
| 684 CHECK_VASTATUS(va_status, "vaCreateBuffer"); |
| 685 |
| 686 va_status = vaBeginPicture(va_dpy, context_id, surface_id); |
| 687 CHECK_VASTATUS(va_status, "vaBeginPicture"); |
| 688 |
| 689 va_status = vaRenderPicture(va_dpy,context_id, &pic_param_buf, 1); |
| 690 CHECK_VASTATUS(va_status, "vaRenderPicture"); |
| 691 |
| 692 va_status = vaRenderPicture(va_dpy,context_id, &iqmatrix_buf, 1); |
| 693 CHECK_VASTATUS(va_status, "vaRenderPicture"); |
| 694 |
| 695 va_status = vaRenderPicture(va_dpy,context_id, &huffmantable_buf, 1); |
| 696 CHECK_VASTATUS(va_status, "vaRenderPicture"); |
| 697 |
| 698 va_status = vaRenderPicture(va_dpy,context_id, &slice_param_buf, 1); |
| 699 CHECK_VASTATUS(va_status, "vaRenderPicture"); |
| 700 |
| 701 va_status = vaRenderPicture(va_dpy,context_id, &slice_data_buf, 1); |
| 702 CHECK_VASTATUS(va_status, "vaRenderPicture"); |
| 703 |
| 704 va_status = vaEndPicture(va_dpy,context_id); |
| 705 CHECK_VASTATUS(va_status, "vaEndPicture"); |
| 706 |
| 707 va_status = vaSyncSurface(va_dpy, surface_id); |
| 708 CHECK_VASTATUS(va_status, "vaSyncSurface"); |
| 709 |
| 710 if (putsurface) { |
| 711 Window win; |
| 712 win = XCreateSimpleWindow(x11_display, RootWindow(x11_display, 0), 0, 0, |
| 713 priv->width,priv->height, 0, 0, WhitePixel(x11_display, 0)); |
| 714 XMapWindow(x11_display, win); |
| 715 XSync(x11_display, False); |
| 716 va_status = vaPutSurface(va_dpy, surface_id, win, |
| 717 0,0,priv->width,priv->height, |
| 718 0,0,priv->width,priv->height, |
| 719 NULL,0,0); |
| 720 CHECK_VASTATUS(va_status, "vaPutSurface"); |
| 721 } |
| 722 printf("press any key to exit\n"); |
| 723 getchar(); |
| 724 |
| 725 vaDestroySurfaces(va_dpy,&surface_id,1); |
| 726 vaDestroyConfig(va_dpy,config_id); |
| 727 vaDestroyContext(va_dpy,context_id); |
| 728 |
| 729 vaTerminate(va_dpy); |
| 730 XCloseDisplay(x11_display); |
| 731 |
| 732 return 0; |
| 733 } |
| 734 const char *tinyjpeg_get_errorstring(struct jdec_private *priv) |
| 735 { |
| 736 /* FIXME: the error string must be store in the context */ |
| 737 priv = priv; |
| 738 return error_string; |
| 739 } |
| 740 void tinyjpeg_get_size(struct jdec_private *priv, unsigned int *width, unsigned
int *height) |
| 741 { |
| 742 *width = priv->width; |
| 743 *height = priv->height; |
| 744 } |
| 745 |
| 746 |
OLD | NEW |