| OLD | NEW |
| 1 /* | 1 /* |
| 2 * jchuff.c | 2 * jchuff.c |
| 3 * | 3 * |
| 4 * Copyright (C) 1991-1997, Thomas G. Lane. | 4 * Copyright (C) 1991-1997, Thomas G. Lane. |
| 5 * Copyright (C) 2009-2011, D. R. Commander. |
| 5 * This file is part of the Independent JPEG Group's software. | 6 * This file is part of the Independent JPEG Group's software. |
| 6 * For conditions of distribution and use, see the accompanying README file. | 7 * For conditions of distribution and use, see the accompanying README file. |
| 7 * | 8 * |
| 8 * This file contains Huffman entropy encoding routines. | 9 * This file contains Huffman entropy encoding routines. |
| 9 * | 10 * |
| 10 * Much of the complexity here has to do with supporting output suspension. | 11 * Much of the complexity here has to do with supporting output suspension. |
| 11 * If the data destination module demands suspension, we want to be able to | 12 * If the data destination module demands suspension, we want to be able to |
| 12 * back up to the start of the current MCU. To do this, we copy state | 13 * back up to the start of the current MCU. To do this, we copy state |
| 13 * variables into local working storage, and update them back to the | 14 * variables into local working storage, and update them back to the |
| 14 * permanent JPEG objects only upon successful completion of an MCU. | 15 * permanent JPEG objects only upon successful completion of an MCU. |
| 15 */ | 16 */ |
| 16 | 17 |
| 17 /* Modifications: | |
| 18 * Copyright (C)2007 Sun Microsystems, Inc. | |
| 19 * Copyright (C)2009 D. R. Commander | |
| 20 * | |
| 21 * This library is free software and may be redistributed and/or modified under | |
| 22 * the terms of the wxWindows Library License, Version 3.1 or (at your option) | |
| 23 * any later version. The full license is in the LICENSE.txt file included | |
| 24 * with this distribution. | |
| 25 * | |
| 26 * This library is distributed in the hope that it will be useful, | |
| 27 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 29 * wxWindows Library License for more details. | |
| 30 */ | |
| 31 | |
| 32 #define JPEG_INTERNALS | 18 #define JPEG_INTERNALS |
| 33 #include "jinclude.h" | 19 #include "jinclude.h" |
| 34 #include "jpeglib.h" | 20 #include "jpeglib.h" |
| 35 #include "jchuff.h" /* Declarations shared with jcphuff.c */ | 21 #include "jchuff.h" /* Declarations shared with jcphuff.c */ |
| 36 #include <limits.h> | 22 #include <limits.h> |
| 37 | 23 |
| 38 static unsigned char jpeg_first_bit_table[65536]; | 24 static unsigned char jpeg_nbits_table[65536]; |
| 39 int jpeg_first_bit_table_init=0; | 25 static int jpeg_nbits_table_init = 0; |
| 40 | 26 |
| 41 #ifndef min | 27 #ifndef min |
| 42 #define min(a,b) ((a)<(b)?(a):(b)) | 28 #define min(a,b) ((a)<(b)?(a):(b)) |
| 43 #endif | 29 #endif |
| 44 | 30 |
| 31 |
| 45 /* Expanded entropy encoder object for Huffman encoding. | 32 /* Expanded entropy encoder object for Huffman encoding. |
| 46 * | 33 * |
| 47 * The savable_state subrecord contains fields that change within an MCU, | 34 * The savable_state subrecord contains fields that change within an MCU, |
| 48 * but must not be updated permanently until we complete the MCU. | 35 * but must not be updated permanently until we complete the MCU. |
| 49 */ | 36 */ |
| 50 | 37 |
| 51 typedef struct { | 38 typedef struct { |
| 52 size_t put_buffer; /* current bit-accumulation buffer */ | 39 size_t put_buffer; /* current bit-accumulation buffer */ |
| 53 int put_bits; /* # of bits now in it */ | 40 int put_bits; /* # of bits now in it */ |
| 54 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ | 41 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl, | 161 jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl, |
| 175 & entropy->dc_derived_tbls[dctbl]); | 162 & entropy->dc_derived_tbls[dctbl]); |
| 176 jpeg_make_c_derived_tbl(cinfo, FALSE, actbl, | 163 jpeg_make_c_derived_tbl(cinfo, FALSE, actbl, |
| 177 & entropy->ac_derived_tbls[actbl]); | 164 & entropy->ac_derived_tbls[actbl]); |
| 178 } | 165 } |
| 179 /* Initialize DC predictions to 0 */ | 166 /* Initialize DC predictions to 0 */ |
| 180 entropy->saved.last_dc_val[ci] = 0; | 167 entropy->saved.last_dc_val[ci] = 0; |
| 181 } | 168 } |
| 182 | 169 |
| 183 /* Initialize bit buffer to empty */ | 170 /* Initialize bit buffer to empty */ |
| 184 | |
| 185 entropy->saved.put_buffer = 0; | 171 entropy->saved.put_buffer = 0; |
| 186 entropy->saved.put_bits = 0; | 172 entropy->saved.put_bits = 0; |
| 187 | 173 |
| 188 /* Initialize restart stuff */ | 174 /* Initialize restart stuff */ |
| 189 entropy->restarts_to_go = cinfo->restart_interval; | 175 entropy->restarts_to_go = cinfo->restart_interval; |
| 190 entropy->next_restart_num = 0; | 176 entropy->next_restart_num = 0; |
| 191 } | 177 } |
| 192 | 178 |
| 193 | 179 |
| 194 /* | 180 /* |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 maxsymbol = isDC ? 15 : 255; | 264 maxsymbol = isDC ? 15 : 255; |
| 279 | 265 |
| 280 for (p = 0; p < lastp; p++) { | 266 for (p = 0; p < lastp; p++) { |
| 281 i = htbl->huffval[p]; | 267 i = htbl->huffval[p]; |
| 282 if (i < 0 || i > maxsymbol || dtbl->ehufsi[i]) | 268 if (i < 0 || i > maxsymbol || dtbl->ehufsi[i]) |
| 283 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); | 269 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); |
| 284 dtbl->ehufco[i] = huffcode[p]; | 270 dtbl->ehufco[i] = huffcode[p]; |
| 285 dtbl->ehufsi[i] = huffsize[p]; | 271 dtbl->ehufsi[i] = huffsize[p]; |
| 286 } | 272 } |
| 287 | 273 |
| 288 if(!jpeg_first_bit_table_init) { | 274 if(!jpeg_nbits_table_init) { |
| 289 for(i = 0; i < 65536; i++) { | 275 for(i = 0; i < 65536; i++) { |
| 290 int bit = 0, val = i; | 276 int nbits = 0, temp = i; |
| 291 while (val) {val >>= 1; bit++;} | 277 while (temp) {temp >>= 1; nbits++;} |
| 292 jpeg_first_bit_table[i] = bit; | 278 jpeg_nbits_table[i] = nbits; |
| 293 } | 279 } |
| 294 jpeg_first_bit_table_init = 1; | 280 jpeg_nbits_table_init = 1; |
| 295 } | 281 } |
| 296 } | 282 } |
| 297 | 283 |
| 298 | 284 |
| 299 /* Outputting bytes to the file */ | 285 /* Outputting bytes to the file */ |
| 300 | 286 |
| 301 /* Emit a byte, taking 'action' if must suspend. */ | 287 /* Emit a byte, taking 'action' if must suspend. */ |
| 302 #define emit_byte(state,val,action) \ | 288 #define emit_byte(state,val,action) \ |
| 303 { *(state)->next_output_byte++ = (JOCTET) (val); \ | 289 { *(state)->next_output_byte++ = (JOCTET) (val); \ |
| 304 if (--(state)->free_in_buffer == 0) \ | 290 if (--(state)->free_in_buffer == 0) \ |
| (...skipping 13 matching lines...) Expand all Loading... |
| 318 return FALSE; | 304 return FALSE; |
| 319 /* After a successful buffer dump, must reset buffer pointers */ | 305 /* After a successful buffer dump, must reset buffer pointers */ |
| 320 state->next_output_byte = dest->next_output_byte; | 306 state->next_output_byte = dest->next_output_byte; |
| 321 state->free_in_buffer = dest->free_in_buffer; | 307 state->free_in_buffer = dest->free_in_buffer; |
| 322 return TRUE; | 308 return TRUE; |
| 323 } | 309 } |
| 324 | 310 |
| 325 | 311 |
| 326 /* Outputting bits to the file */ | 312 /* Outputting bits to the file */ |
| 327 | 313 |
| 328 /* Only the right 24 bits of put_buffer are used; the valid bits are | 314 /* These macros perform the same task as the emit_bits() function in the |
| 329 * left-justified in this part. At most 16 bits can be passed to emit_bits | 315 * original libjpeg code. In addition to reducing overhead by explicitly |
| 330 * in one call, and we never retain more than 7 bits in put_buffer | 316 * inlining the code, additional performance is achieved by taking into |
| 331 * between calls, so 24 bits are sufficient. | 317 * account the size of the bit buffer and waiting until it is almost full |
| 318 * before emptying it. This mostly benefits 64-bit platforms, since 6 |
| 319 * bytes can be stored in a 64-bit bit buffer before it has to be emptied. |
| 332 */ | 320 */ |
| 333 | 321 |
| 334 /***************************************************************/ | 322 #define EMIT_BYTE() { \ |
| 335 | 323 JOCTET c; \ |
| 336 #define EMIT_BYTE() { \ | 324 put_bits -= 8; \ |
| 337 if (0xFF == (*buffer++ = (unsigned char)(put_buffer >> (put_bits -= 8)))) \ | 325 c = (JOCTET)GETJOCTET(put_buffer >> put_bits); \ |
| 338 *buffer++ = 0; \ | 326 *buffer++ = c; \ |
| 327 if (c == 0xFF) /* need to stuff a zero byte? */ \ |
| 328 *buffer++ = 0; \ |
| 339 } | 329 } |
| 340 | 330 |
| 341 /***************************************************************/ | 331 #define PUT_BITS(code, size) { \ |
| 342 | 332 put_bits += size; \ |
| 343 #define DUMP_BITS_(code, size) { \ | 333 put_buffer = (put_buffer << size) | code; \ |
| 344 put_bits += size; \ | |
| 345 put_buffer = (put_buffer << size) | code; \ | |
| 346 if (put_bits > 7) \ | |
| 347 while(put_bits > 7) \ | |
| 348 EMIT_BYTE() \ | |
| 349 } | |
| 350 | |
| 351 /***************************************************************/ | |
| 352 | |
| 353 #define CHECKBUF15() { \ | |
| 354 if (put_bits > 15) { \ | |
| 355 EMIT_BYTE() \ | |
| 356 EMIT_BYTE() \ | |
| 357 } \ | |
| 358 } | 334 } |
| 359 | 335 |
| 360 #define CHECKBUF47() { \ | 336 #define CHECKBUF15() { \ |
| 361 if (put_bits > 47) { \ | 337 if (put_bits > 15) { \ |
| 362 EMIT_BYTE() \ | 338 EMIT_BYTE() \ |
| 363 EMIT_BYTE() \ | 339 EMIT_BYTE() \ |
| 364 EMIT_BYTE() \ | 340 } \ |
| 365 EMIT_BYTE() \ | |
| 366 EMIT_BYTE() \ | |
| 367 EMIT_BYTE() \ | |
| 368 } \ | |
| 369 } | 341 } |
| 370 | 342 |
| 371 #define CHECKBUF31() { \ | 343 #define CHECKBUF31() { \ |
| 372 if (put_bits > 31) { \ | 344 if (put_bits > 31) { \ |
| 373 EMIT_BYTE() \ | 345 EMIT_BYTE() \ |
| 374 EMIT_BYTE() \ | 346 EMIT_BYTE() \ |
| 375 EMIT_BYTE() \ | 347 EMIT_BYTE() \ |
| 376 EMIT_BYTE() \ | 348 EMIT_BYTE() \ |
| 377 } \ | 349 } \ |
| 378 } | 350 } |
| 379 | 351 |
| 380 /***************************************************************/ | 352 #define CHECKBUF47() { \ |
| 381 | 353 if (put_bits > 47) { \ |
| 382 #define DUMP_BITS_NOCHECK(code, size) { \ | 354 EMIT_BYTE() \ |
| 383 put_bits += size; \ | 355 EMIT_BYTE() \ |
| 384 put_buffer = (put_buffer << size) | code; \ | 356 EMIT_BYTE() \ |
| 385 } | 357 EMIT_BYTE() \ |
| 358 EMIT_BYTE() \ |
| 359 EMIT_BYTE() \ |
| 360 } \ |
| 361 } |
| 386 | 362 |
| 387 #if __WORDSIZE==64 || defined(_WIN64) | 363 #if __WORDSIZE==64 || defined(_WIN64) |
| 388 | 364 |
| 389 #define DUMP_BITS(code, size) { \ | 365 #define EMIT_BITS(code, size) { \ |
| 390 CHECKBUF47() \ | 366 CHECKBUF47() \ |
| 391 put_bits += size; \ | 367 PUT_BITS(code, size) \ |
| 392 put_buffer = (put_buffer << size) | code; \ | 368 } |
| 369 |
| 370 #define EMIT_CODE(code, size) { \ |
| 371 temp2 &= (((INT32) 1)<<nbits) - 1; \ |
| 372 CHECKBUF31() \ |
| 373 PUT_BITS(code, size) \ |
| 374 PUT_BITS(temp2, nbits) \ |
| 393 } | 375 } |
| 394 | 376 |
| 395 #else | 377 #else |
| 396 | 378 |
| 397 #define DUMP_BITS(code, size) { \ | 379 #define EMIT_BITS(code, size) { \ |
| 398 put_bits += size; \ | 380 PUT_BITS(code, size) \ |
| 399 put_buffer = (put_buffer << size) | code; \ | 381 CHECKBUF15() \ |
| 400 CHECKBUF15() \ | 382 } |
| 383 |
| 384 #define EMIT_CODE(code, size) { \ |
| 385 temp2 &= (((INT32) 1)<<nbits) - 1; \ |
| 386 PUT_BITS(code, size) \ |
| 387 CHECKBUF15() \ |
| 388 PUT_BITS(temp2, nbits) \ |
| 389 CHECKBUF15() \ |
| 401 } | 390 } |
| 402 | 391 |
| 403 #endif | 392 #endif |
| 404 | 393 |
| 405 /***************************************************************/ | |
| 406 | |
| 407 #define DUMP_SINGLE_VALUE(ht, codevalue) { \ | |
| 408 size = ht->ehufsi[codevalue]; \ | |
| 409 code = ht->ehufco[codevalue]; \ | |
| 410 \ | |
| 411 DUMP_BITS(code, size) \ | |
| 412 } | |
| 413 | |
| 414 /***************************************************************/ | |
| 415 | |
| 416 #define DUMP_VALUE_SLOW(ht, codevalue, t, nbits) { \ | |
| 417 size = ht->ehufsi[codevalue]; \ | |
| 418 code = ht->ehufco[codevalue]; \ | |
| 419 t &= ~(-1 << nbits); \ | |
| 420 DUMP_BITS_NOCHECK(code, size) \ | |
| 421 CHECKBUF15() \ | |
| 422 DUMP_BITS_NOCHECK(t, nbits) \ | |
| 423 CHECKBUF15() \ | |
| 424 } | |
| 425 | |
| 426 int _max=0; | |
| 427 | |
| 428 #if __WORDSIZE==64 || defined(_WIN64) | |
| 429 | |
| 430 #define DUMP_VALUE(ht, codevalue, t, nbits) { \ | |
| 431 size = ht->ehufsi[codevalue]; \ | |
| 432 code = ht->ehufco[codevalue]; \ | |
| 433 t &= ~(-1 << nbits); \ | |
| 434 CHECKBUF31() \ | |
| 435 DUMP_BITS_NOCHECK(code, size) \ | |
| 436 DUMP_BITS_NOCHECK(t, nbits) \ | |
| 437 } | |
| 438 | |
| 439 #else | |
| 440 | |
| 441 #define DUMP_VALUE(ht, codevalue, t, nbits) { \ | |
| 442 size = ht->ehufsi[codevalue]; \ | |
| 443 code = ht->ehufco[codevalue]; \ | |
| 444 t &= ~(-1 << nbits); \ | |
| 445 DUMP_BITS_NOCHECK(code, size) \ | |
| 446 CHECKBUF15() \ | |
| 447 DUMP_BITS_NOCHECK(t, nbits) \ | |
| 448 CHECKBUF15() \ | |
| 449 } | |
| 450 | |
| 451 #endif | |
| 452 | |
| 453 /***************************************************************/ | |
| 454 | 394 |
| 455 #define BUFSIZE (DCTSIZE2 * 2) | 395 #define BUFSIZE (DCTSIZE2 * 2) |
| 456 | 396 |
| 457 #define LOAD_BUFFER() { \ | 397 #define LOAD_BUFFER() { \ |
| 458 if (state->free_in_buffer < BUFSIZE) { \ | 398 if (state->free_in_buffer < BUFSIZE) { \ |
| 459 localbuf = 1; \ | 399 localbuf = 1; \ |
| 460 buffer = _buffer; \ | 400 buffer = _buffer; \ |
| 461 } \ | 401 } \ |
| 462 else buffer = state->next_output_byte; \ | 402 else buffer = state->next_output_byte; \ |
| 463 } | 403 } |
| 464 | 404 |
| 465 #define STORE_BUFFER() { \ | 405 #define STORE_BUFFER() { \ |
| 466 if (localbuf) { \ | 406 if (localbuf) { \ |
| 467 bytes = buffer - _buffer; \ | 407 bytes = buffer - _buffer; \ |
| 468 buffer = _buffer; \ | 408 buffer = _buffer; \ |
| 469 while (bytes > 0) { \ | 409 while (bytes > 0) { \ |
| 470 bytestocopy = min(bytes, state->free_in_buffer); \ | 410 bytestocopy = min(bytes, state->free_in_buffer); \ |
| 471 MEMCOPY(state->next_output_byte, buffer, bytestocopy); \ | 411 MEMCOPY(state->next_output_byte, buffer, bytestocopy); \ |
| 472 state->next_output_byte += bytestocopy; \ | 412 state->next_output_byte += bytestocopy; \ |
| 473 buffer += bytestocopy; \ | 413 buffer += bytestocopy; \ |
| 474 state->free_in_buffer -= bytestocopy; \ | 414 state->free_in_buffer -= bytestocopy; \ |
| 475 if (state->free_in_buffer == 0) \ | 415 if (state->free_in_buffer == 0) \ |
| 476 if (! dump_buffer(state)) return FALSE; \ | 416 if (! dump_buffer(state)) return FALSE; \ |
| 477 bytes -= bytestocopy; \ | 417 bytes -= bytestocopy; \ |
| 478 } \ | 418 } \ |
| 479 } \ | 419 } \ |
| 480 else { \ | 420 else { \ |
| 481 state->free_in_buffer -= (buffer - state->next_output_byte); \ | 421 state->free_in_buffer -= (buffer - state->next_output_byte); \ |
| 482 state->next_output_byte = buffer; \ | 422 state->next_output_byte = buffer; \ |
| 483 } \ | 423 } \ |
| 484 } | 424 } |
| 485 | 425 |
| 486 /***************************************************************/ | |
| 487 | 426 |
| 488 LOCAL(boolean) | 427 LOCAL(boolean) |
| 489 flush_bits (working_state * state) | 428 flush_bits (working_state * state) |
| 490 { | 429 { |
| 491 unsigned char _buffer[BUFSIZE], *buffer; | 430 JOCTET _buffer[BUFSIZE], *buffer; |
| 492 size_t put_buffer; int put_bits; | 431 size_t put_buffer; int put_bits; |
| 493 size_t bytes, bytestocopy; int localbuf = 0; | 432 size_t bytes, bytestocopy; int localbuf = 0; |
| 494 | 433 |
| 495 put_buffer = state->cur.put_buffer; | 434 put_buffer = state->cur.put_buffer; |
| 496 put_bits = state->cur.put_bits; | 435 put_bits = state->cur.put_bits; |
| 497 LOAD_BUFFER() | 436 LOAD_BUFFER() |
| 498 | 437 |
| 499 DUMP_BITS_(0x7F, 7) | 438 /* fill any partial byte with ones */ |
| 439 PUT_BITS(0x7F, 7) |
| 440 while (put_bits >= 8) EMIT_BYTE() |
| 500 | 441 |
| 501 state->cur.put_buffer = 0; /* and reset bit-buffer to empty */ | 442 state->cur.put_buffer = 0; /* and reset bit-buffer to empty */ |
| 502 state->cur.put_bits = 0; | 443 state->cur.put_bits = 0; |
| 503 STORE_BUFFER() | 444 STORE_BUFFER() |
| 504 | 445 |
| 505 return TRUE; | 446 return TRUE; |
| 506 } | 447 } |
| 507 | 448 |
| 449 |
| 508 /* Encode a single block's worth of coefficients */ | 450 /* Encode a single block's worth of coefficients */ |
| 509 | 451 |
| 510 LOCAL(boolean) | 452 LOCAL(boolean) |
| 511 encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val, | 453 encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val, |
| 512 c_derived_tbl *dctbl, c_derived_tbl *actbl) | 454 c_derived_tbl *dctbl, c_derived_tbl *actbl) |
| 513 { | 455 { |
| 514 int temp, temp2; | 456 int temp, temp2, temp3; |
| 515 int nbits; | 457 int nbits; |
| 516 int r, sflag, size, code; | 458 int r, code, size; |
| 517 unsigned char _buffer[BUFSIZE], *buffer; | 459 JOCTET _buffer[BUFSIZE], *buffer; |
| 518 size_t put_buffer; int put_bits; | 460 size_t put_buffer; int put_bits; |
| 519 int code_0xf0 = actbl->ehufco[0xf0], size_0xf0 = actbl->ehufsi[0xf0]; | 461 int code_0xf0 = actbl->ehufco[0xf0], size_0xf0 = actbl->ehufsi[0xf0]; |
| 520 size_t bytes, bytestocopy; int localbuf = 0; | 462 size_t bytes, bytestocopy; int localbuf = 0; |
| 521 | 463 |
| 522 put_buffer = state->cur.put_buffer; | 464 put_buffer = state->cur.put_buffer; |
| 523 put_bits = state->cur.put_bits; | 465 put_bits = state->cur.put_bits; |
| 524 LOAD_BUFFER() | 466 LOAD_BUFFER() |
| 525 | 467 |
| 526 /* Encode the DC coefficient difference per section F.1.2.1 */ | 468 /* Encode the DC coefficient difference per section F.1.2.1 */ |
| 527 | 469 |
| 528 temp = temp2 = block[0] - last_dc_val; | 470 temp = temp2 = block[0] - last_dc_val; |
| 529 | 471 |
| 530 sflag = temp >> 31; | 472 /* This is a well-known technique for obtaining the absolute value without a |
| 531 temp -= ((temp + temp) & sflag); | 473 * branch. It is derived from an assembly language technique presented in |
| 532 temp2 += sflag; | 474 * "How to Optimize for the Pentium Processors", Copyright (c) 1996, 1997 by |
| 533 nbits = jpeg_first_bit_table[temp]; | 475 * Agner Fog. |
| 534 DUMP_VALUE_SLOW(dctbl, nbits, temp2, nbits) | 476 */ |
| 477 temp3 = temp >> (CHAR_BIT * sizeof(int) - 1); |
| 478 temp ^= temp3; |
| 479 temp -= temp3; |
| 480 |
| 481 /* For a negative input, want temp2 = bitwise complement of abs(input) */ |
| 482 /* This code assumes we are on a two's complement machine */ |
| 483 temp2 += temp3; |
| 484 |
| 485 /* Find the number of bits needed for the magnitude of the coefficient */ |
| 486 nbits = jpeg_nbits_table[temp]; |
| 487 |
| 488 /* Emit the Huffman-coded symbol for the number of bits */ |
| 489 code = dctbl->ehufco[nbits]; |
| 490 size = dctbl->ehufsi[nbits]; |
| 491 PUT_BITS(code, size) |
| 492 CHECKBUF15() |
| 493 |
| 494 /* Mask off any extra bits in code */ |
| 495 temp2 &= (((INT32) 1)<<nbits) - 1; |
| 496 |
| 497 /* Emit that number of bits of the value, if positive, */ |
| 498 /* or the complement of its magnitude, if negative. */ |
| 499 PUT_BITS(temp2, nbits) |
| 500 CHECKBUF15() |
| 535 | 501 |
| 536 /* Encode the AC coefficients per section F.1.2.2 */ | 502 /* Encode the AC coefficients per section F.1.2.2 */ |
| 537 | 503 |
| 538 r = 0; /* r = run length of zeros */ | 504 r = 0; /* r = run length of zeros */ |
| 539 | 505 |
| 540 #define innerloop(order) { \ | 506 /* Manually unroll the k loop to eliminate the counter variable. This |
| 541 temp2 = *(JCOEF*)((unsigned char*)block + order); \ | 507 * improves performance greatly on systems with a limited number of |
| 542 if(temp2 == 0) r++; \ | 508 * registers (such as x86.) |
| 543 else { \ | 509 */ |
| 544 temp = (JCOEF)temp2; \ | 510 #define kloop(jpeg_natural_order_of_k) { \ |
| 545 sflag = temp >> 31; \ | 511 if ((temp = block[jpeg_natural_order_of_k]) == 0) { \ |
| 546 temp = (temp ^ sflag) - sflag; \ | 512 r++; \ |
| 547 temp2 += sflag; \ | 513 } else { \ |
| 548 nbits = jpeg_first_bit_table[temp]; \ | 514 temp2 = temp; \ |
| 549 for(; r > 15; r -= 16) DUMP_BITS(code_0xf0, size_0xf0) \ | 515 /* Branch-less absolute value, bitwise complement, etc., same as above */ \ |
| 550 sflag = (r << 4) + nbits; \ | 516 temp3 = temp >> (CHAR_BIT * sizeof(int) - 1); \ |
| 551 DUMP_VALUE(actbl, sflag, temp2, nbits) \ | 517 temp ^= temp3; \ |
| 518 temp -= temp3; \ |
| 519 temp2 += temp3; \ |
| 520 nbits = jpeg_nbits_table[temp]; \ |
| 521 /* if run length > 15, must emit special run-length-16 codes (0xF0) */ \ |
| 522 while (r > 15) { \ |
| 523 EMIT_BITS(code_0xf0, size_0xf0) \ |
| 524 r -= 16; \ |
| 525 } \ |
| 526 /* Emit Huffman symbol for run length / number of bits */ \ |
| 527 temp3 = (r << 4) + nbits; \ |
| 528 code = actbl->ehufco[temp3]; \ |
| 529 size = actbl->ehufsi[temp3]; \ |
| 530 EMIT_CODE(code, size) \ |
| 552 r = 0; \ | 531 r = 0; \ |
| 553 }} | 532 } \ |
| 533 } |
| 554 | 534 |
| 555 innerloop(2*1); innerloop(2*8); innerloop(2*16); innerloop(2*9); | 535 /* One iteration for each value in jpeg_natural_order[] */ |
| 556 innerloop(2*2); innerloop(2*3); innerloop(2*10); innerloop(2*17); | 536 kloop(1); kloop(8); kloop(16); kloop(9); kloop(2); kloop(3); |
| 557 innerloop(2*24); innerloop(2*32); innerloop(2*25); innerloop(2*18); | 537 kloop(10); kloop(17); kloop(24); kloop(32); kloop(25); kloop(18); |
| 558 innerloop(2*11); innerloop(2*4); innerloop(2*5); innerloop(2*12); | 538 kloop(11); kloop(4); kloop(5); kloop(12); kloop(19); kloop(26); |
| 559 innerloop(2*19); innerloop(2*26); innerloop(2*33); innerloop(2*40); | 539 kloop(33); kloop(40); kloop(48); kloop(41); kloop(34); kloop(27); |
| 560 innerloop(2*48); innerloop(2*41); innerloop(2*34); innerloop(2*27); | 540 kloop(20); kloop(13); kloop(6); kloop(7); kloop(14); kloop(21); |
| 561 innerloop(2*20); innerloop(2*13); innerloop(2*6); innerloop(2*7); | 541 kloop(28); kloop(35); kloop(42); kloop(49); kloop(56); kloop(57); |
| 562 innerloop(2*14); innerloop(2*21); innerloop(2*28); innerloop(2*35); | 542 kloop(50); kloop(43); kloop(36); kloop(29); kloop(22); kloop(15); |
| 563 innerloop(2*42); innerloop(2*49); innerloop(2*56); innerloop(2*57); | 543 kloop(23); kloop(30); kloop(37); kloop(44); kloop(51); kloop(58); |
| 564 innerloop(2*50); innerloop(2*43); innerloop(2*36); innerloop(2*29); | 544 kloop(59); kloop(52); kloop(45); kloop(38); kloop(31); kloop(39); |
| 565 innerloop(2*22); innerloop(2*15); innerloop(2*23); innerloop(2*30); | 545 kloop(46); kloop(53); kloop(60); kloop(61); kloop(54); kloop(47); |
| 566 innerloop(2*37); innerloop(2*44); innerloop(2*51); innerloop(2*58); | 546 kloop(55); kloop(62); kloop(63); |
| 567 innerloop(2*59); innerloop(2*52); innerloop(2*45); innerloop(2*38); | |
| 568 innerloop(2*31); innerloop(2*39); innerloop(2*46); innerloop(2*53); | |
| 569 innerloop(2*60); innerloop(2*61); innerloop(2*54); innerloop(2*47); | |
| 570 innerloop(2*55); innerloop(2*62); innerloop(2*63); | |
| 571 | 547 |
| 572 /* If the last coef(s) were zero, emit an end-of-block code */ | 548 /* If the last coef(s) were zero, emit an end-of-block code */ |
| 573 if (r > 0) DUMP_SINGLE_VALUE(actbl, 0x0) | 549 if (r > 0) { |
| 550 code = actbl->ehufco[0]; |
| 551 size = actbl->ehufsi[0]; |
| 552 EMIT_BITS(code, size) |
| 553 } |
| 574 | 554 |
| 575 state->cur.put_buffer = put_buffer; | 555 state->cur.put_buffer = put_buffer; |
| 576 state->cur.put_bits = put_bits; | 556 state->cur.put_bits = put_bits; |
| 577 STORE_BUFFER() | 557 STORE_BUFFER() |
| 578 | 558 |
| 579 return TRUE; | 559 return TRUE; |
| 580 } | 560 } |
| 581 | 561 |
| 582 | 562 |
| 583 /* | 563 /* |
| (...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1037 entropy->pub.start_pass = start_pass_huff; | 1017 entropy->pub.start_pass = start_pass_huff; |
| 1038 | 1018 |
| 1039 /* Mark tables unallocated */ | 1019 /* Mark tables unallocated */ |
| 1040 for (i = 0; i < NUM_HUFF_TBLS; i++) { | 1020 for (i = 0; i < NUM_HUFF_TBLS; i++) { |
| 1041 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; | 1021 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; |
| 1042 #ifdef ENTROPY_OPT_SUPPORTED | 1022 #ifdef ENTROPY_OPT_SUPPORTED |
| 1043 entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL; | 1023 entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL; |
| 1044 #endif | 1024 #endif |
| 1045 } | 1025 } |
| 1046 } | 1026 } |
| OLD | NEW |