DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

jchuff.cpp (28629B)


      1 /*
      2  * jchuff.c
      3  *
      4  * Copyright (C) 1991-1995, Thomas G. Lane.
      5  * This file is part of the Independent JPEG Group's software.
      6  * For conditions of distribution and use, see the accompanying README file.
      7  *
      8  * This file contains Huffman entropy encoding routines.
      9  *
     10  * 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  * 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  * permanent JPEG objects only upon successful completion of an MCU.
     15  */
     16 
     17 #define JPEG_INTERNALS
     18 #include "jinclude.h"
     19 #include "jpeglib.h"
     20 #include "jchuff.h"      /* Declarations shared with jcphuff.c */
     21 
     22 
     23 /* Expanded entropy encoder object for Huffman encoding.
     24  *
     25  * The savable_state subrecord contains fields that change within an MCU,
     26  * but must not be updated permanently until we complete the MCU.
     27  */
     28 
     29 typedef struct {
     30     INT32 put_buffer;   /* current bit-accumulation buffer */
     31     int   put_bits;     /* # of bits now in it */
     32     int   last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
     33 } savable_state;
     34 
     35 /* This macro is to work around compilers with missing or broken
     36  * structure assignment.  You'll need to fix this code if you have
     37  * such a compiler and you change MAX_COMPS_IN_SCAN.
     38  */
     39 
     40 #ifndef NO_STRUCT_ASSIGN
     41 #define ASSIGN_STATE( dest, src )  ( ( dest ) = ( src ) )
     42 #else
     43 #if MAX_COMPS_IN_SCAN == 4
     44 #define ASSIGN_STATE( dest, src )  \
     45     ( ( dest ).put_buffer = ( src ).put_buffer, \
     46      ( dest ).put_bits = ( src ).put_bits, \
     47      ( dest ).last_dc_val[0] = ( src ).last_dc_val[0], \
     48      ( dest ).last_dc_val[1] = ( src ).last_dc_val[1], \
     49      ( dest ).last_dc_val[2] = ( src ).last_dc_val[2], \
     50      ( dest ).last_dc_val[3] = ( src ).last_dc_val[3] )
     51 #endif
     52 #endif
     53 
     54 
     55 typedef struct {
     56     struct jpeg_entropy_encoder pub;/* public fields */
     57 
     58     savable_state saved;    /* Bit buffer & DC state at start of MCU */
     59 
     60     /* These fields are NOT loaded into local working state. */
     61     unsigned int restarts_to_go;/* MCUs left in this restart interval */
     62     int          next_restart_num; /* next restart number to write (0-7) */
     63 
     64     /* Pointers to derived tables (these workspaces have image lifespan) */
     65     c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
     66     c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
     67 
     68 #ifdef ENTROPY_OPT_SUPPORTED    /* Statistics tables for optimization */
     69     long * dc_count_ptrs[NUM_HUFF_TBLS];
     70     long * ac_count_ptrs[NUM_HUFF_TBLS];
     71 #endif
     72 } huff_entropy_encoder;
     73 
     74 typedef huff_entropy_encoder * huff_entropy_ptr;
     75 
     76 /* Working state while writing an MCU.
     77  * This struct contains all the fields that are needed by subroutines.
     78  */
     79 
     80 typedef struct {
     81     JOCTET *       next_output_byte; /* => next byte to write in buffer */
     82     size_t         free_in_buffer; /* # of byte spaces remaining in buffer */
     83     savable_state  cur;     /* Current bit buffer & DC state */
     84     j_compress_ptr cinfo;   /* dump_buffer needs access to this */
     85 } working_state;
     86 
     87 
     88 /* Forward declarations */
     89 METHODDEF boolean encode_mcu_huff JPP( ( j_compress_ptr cinfo,
     90                                          JBLOCKROW * MCU_data ) );
     91 METHODDEF void finish_pass_huff JPP( (j_compress_ptr cinfo) );
     92 #ifdef ENTROPY_OPT_SUPPORTED
     93 METHODDEF boolean encode_mcu_gather JPP( ( j_compress_ptr cinfo,
     94                                            JBLOCKROW * MCU_data ) );
     95 METHODDEF void finish_pass_gather JPP( (j_compress_ptr cinfo) );
     96 #endif
     97 
     98 
     99 /*
    100  * Initialize for a Huffman-compressed scan.
    101  * If gather_statistics is TRUE, we do not output anything during the scan,
    102  * just count the Huffman symbols used and generate Huffman code tables.
    103  */
    104 
    105 METHODDEF void
    106 start_pass_huff( j_compress_ptr cinfo, boolean gather_statistics ) {
    107     huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
    108     int ci, dctbl, actbl;
    109     jpeg_component_info * compptr;
    110 
    111     if ( gather_statistics ) {
    112 #ifdef ENTROPY_OPT_SUPPORTED
    113         entropy->pub.encode_mcu = encode_mcu_gather;
    114         entropy->pub.finish_pass = finish_pass_gather;
    115 #else
    116         ERREXIT( cinfo, JERR_NOT_COMPILED );
    117 #endif
    118     } else {
    119         entropy->pub.encode_mcu = encode_mcu_huff;
    120         entropy->pub.finish_pass = finish_pass_huff;
    121     }
    122 
    123     for ( ci = 0; ci < cinfo->comps_in_scan; ci++ ) {
    124         compptr = cinfo->cur_comp_info[ci];
    125         dctbl = compptr->dc_tbl_no;
    126         actbl = compptr->ac_tbl_no;
    127         /* Make sure requested tables are present */
    128         /* (In gather mode, tables need not be allocated yet) */
    129         if ( ( dctbl < 0 ) || ( dctbl >= NUM_HUFF_TBLS ) ||
    130             ( ( cinfo->dc_huff_tbl_ptrs[dctbl] == NULL ) && ( !gather_statistics ) ) ) {
    131             ERREXIT1( cinfo, JERR_NO_HUFF_TABLE, dctbl );
    132         }
    133         if ( ( actbl < 0 ) || ( actbl >= NUM_HUFF_TBLS ) ||
    134             ( ( cinfo->ac_huff_tbl_ptrs[actbl] == NULL ) && ( !gather_statistics ) ) ) {
    135             ERREXIT1( cinfo, JERR_NO_HUFF_TABLE, actbl );
    136         }
    137         if ( gather_statistics ) {
    138 #ifdef ENTROPY_OPT_SUPPORTED
    139             /* Allocate and zero the statistics tables */
    140             /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
    141             if ( entropy->dc_count_ptrs[dctbl] == NULL ) {
    142                 entropy->dc_count_ptrs[dctbl] = (long *)
    143                                                 ( *cinfo->mem->alloc_small )( (j_common_ptr) cinfo, JPOOL_IMAGE,
    144                                                                              257 * SIZEOF( long ) );
    145             }
    146             MEMZERO( entropy->dc_count_ptrs[dctbl], 257 * SIZEOF( long ) );
    147             if ( entropy->ac_count_ptrs[actbl] == NULL ) {
    148                 entropy->ac_count_ptrs[actbl] = (long *)
    149                                                 ( *cinfo->mem->alloc_small )( (j_common_ptr) cinfo, JPOOL_IMAGE,
    150                                                                              257 * SIZEOF( long ) );
    151             }
    152             MEMZERO( entropy->ac_count_ptrs[actbl], 257 * SIZEOF( long ) );
    153 #endif
    154         } else {
    155             /* Compute derived values for Huffman tables */
    156             /* We may do this more than once for a table, but it's not expensive */
    157             jpeg_make_c_derived_tbl( cinfo, cinfo->dc_huff_tbl_ptrs[dctbl],
    158                                      &entropy->dc_derived_tbls[dctbl] );
    159             jpeg_make_c_derived_tbl( cinfo, cinfo->ac_huff_tbl_ptrs[actbl],
    160                                      &entropy->ac_derived_tbls[actbl] );
    161         }
    162         /* Initialize DC predictions to 0 */
    163         entropy->saved.last_dc_val[ci] = 0;
    164     }
    165 
    166     /* Initialize bit buffer to empty */
    167     entropy->saved.put_buffer = 0;
    168     entropy->saved.put_bits = 0;
    169 
    170     /* Initialize restart stuff */
    171     entropy->restarts_to_go = cinfo->restart_interval;
    172     entropy->next_restart_num = 0;
    173 }
    174 
    175 
    176 /*
    177  * Compute the derived values for a Huffman table.
    178  * Note this is also used by jcphuff.c.
    179  */
    180 
    181 GLOBAL void
    182 jpeg_make_c_derived_tbl( j_compress_ptr cinfo, JHUFF_TBL * htbl,
    183                          c_derived_tbl ** pdtbl ) {
    184     c_derived_tbl * dtbl;
    185     int p, i, l, lastp, si;
    186     char huffsize[257];
    187     unsigned int huffcode[257];
    188     unsigned int code;
    189 
    190     /* Allocate a workspace if we haven't already done so. */
    191     if ( *pdtbl == NULL ) {
    192         *pdtbl = (c_derived_tbl *)
    193                  ( *cinfo->mem->alloc_small )( (j_common_ptr) cinfo, JPOOL_IMAGE,
    194                                               SIZEOF( c_derived_tbl ) );
    195     }
    196     dtbl = *pdtbl;
    197 
    198     /* Figure C.1: make table of Huffman code length for each symbol */
    199     /* Note that this is in code-length order. */
    200 
    201     p = 0;
    202     for ( l = 1; l <= 16; l++ ) {
    203         for ( i = 1; i <= (int) htbl->bits[l]; i++ ) {
    204             huffsize[p++] = (char) l;
    205         }
    206     }
    207     huffsize[p] = 0;
    208     lastp = p;
    209 
    210     /* Figure C.2: generate the codes themselves */
    211     /* Note that this is in code-length order. */
    212 
    213     code = 0;
    214     si = huffsize[0];
    215     p = 0;
    216     while ( huffsize[p] ) {
    217         while ( ( (int) huffsize[p] ) == si ) {
    218             huffcode[p++] = code;
    219             code++;
    220         }
    221         code <<= 1;
    222         si++;
    223     }
    224 
    225     /* Figure C.3: generate encoding tables */
    226     /* These are code and size indexed by symbol value */
    227 
    228     /* Set any codeless symbols to have code length 0;
    229      * this allows emit_bits to detect any attempt to emit such symbols.
    230      */
    231     MEMZERO( dtbl->ehufsi, SIZEOF( dtbl->ehufsi ) );
    232 
    233     for ( p = 0; p < lastp; p++ ) {
    234         dtbl->ehufco[htbl->huffval[p]] = huffcode[p];
    235         dtbl->ehufsi[htbl->huffval[p]] = huffsize[p];
    236     }
    237 }
    238 
    239 
    240 /* Outputting bytes to the file */
    241 
    242 /* Emit a byte, taking 'action' if must suspend. */
    243 #define emit_byte( state, val, action )  \
    244     { *( state )->next_output_byte++ = (JOCTET) ( val );  \
    245       if ( -- ( state )->free_in_buffer == 0 ) { \
    246           if ( !dump_buffer( state ) )  \
    247           { action; } } }
    248 
    249 
    250 LOCAL boolean
    251 dump_buffer( working_state * state ) {
    252 /* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
    253     struct jpeg_destination_mgr * dest = state->cinfo->dest;
    254 
    255     if ( !( *dest->empty_output_buffer )( state->cinfo ) ) {
    256         return FALSE;
    257     }
    258     /* After a successful buffer dump, must reset buffer pointers */
    259     state->next_output_byte = dest->next_output_byte;
    260     state->free_in_buffer = dest->free_in_buffer;
    261     return TRUE;
    262 }
    263 
    264 
    265 /* Outputting bits to the file */
    266 
    267 /* Only the right 24 bits of put_buffer are used; the valid bits are
    268  * left-justified in this part.  At most 16 bits can be passed to emit_bits
    269  * in one call, and we never retain more than 7 bits in put_buffer
    270  * between calls, so 24 bits are sufficient.
    271  */
    272 
    273 INLINE
    274 LOCAL boolean
    275 emit_bits( working_state * state, unsigned int code, int size ) {
    276 /* Emit some bits; return TRUE if successful, FALSE if must suspend */
    277 /* This routine is heavily used, so it's worth coding tightly. */
    278     register INT32 put_buffer = (INT32) code;
    279     register int put_bits = state->cur.put_bits;
    280 
    281     /* if size is 0, caller used an invalid Huffman table entry */
    282     if ( size == 0 ) {
    283         ERREXIT( state->cinfo, JERR_HUFF_MISSING_CODE );
    284     }
    285 
    286     put_buffer &= ( ( (INT32) 1 ) << size ) - 1;/* mask off any extra bits in code */
    287 
    288     put_bits += size;   /* new number of bits in buffer */
    289 
    290     put_buffer <<= 24 - put_bits;/* align incoming bits */
    291 
    292     put_buffer |= state->cur.put_buffer;/* and merge with old buffer contents */
    293 
    294     while ( put_bits >= 8 ) {
    295         int c = (int) ( ( put_buffer >> 16 ) & 0xFF );
    296 
    297         emit_byte( state, c, return FALSE );
    298         if ( c == 0xFF ) {  /* need to stuff a zero byte? */
    299             emit_byte( state, 0, return FALSE );
    300         }
    301         put_buffer <<= 8;
    302         put_bits -= 8;
    303     }
    304 
    305     state->cur.put_buffer = put_buffer;/* update state variables */
    306     state->cur.put_bits = put_bits;
    307 
    308     return TRUE;
    309 }
    310 
    311 
    312 LOCAL boolean
    313 flush_bits( working_state * state ) {
    314     if ( !emit_bits( state, 0x7F, 7 ) ) {/* fill any partial byte with ones */
    315         return FALSE;
    316     }
    317     state->cur.put_buffer = 0;  /* and reset bit-buffer to empty */
    318     state->cur.put_bits = 0;
    319     return TRUE;
    320 }
    321 
    322 
    323 /* Encode a single block's worth of coefficients */
    324 
    325 LOCAL boolean
    326 encode_one_block( working_state * state, JCOEFPTR block, int last_dc_val,
    327                   c_derived_tbl * dctbl, c_derived_tbl * actbl ) {
    328     register int temp, temp2;
    329     register int nbits;
    330     register int k, r, i;
    331 
    332     /* Encode the DC coefficient difference per section F.1.2.1 */
    333 
    334     temp = temp2 = block[0] - last_dc_val;
    335 
    336     if ( temp < 0 ) {
    337         temp = -temp;   /* temp is abs value of input */
    338         /* For a negative input, want temp2 = bitwise complement of abs(input) */
    339         /* This code assumes we are on a two's complement machine */
    340         temp2--;
    341     }
    342 
    343     /* Find the number of bits needed for the magnitude of the coefficient */
    344     nbits = 0;
    345     while ( temp ) {
    346         nbits++;
    347         temp >>= 1;
    348     }
    349 
    350     /* Emit the Huffman-coded symbol for the number of bits */
    351     if ( !emit_bits( state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits] ) ) {
    352         return FALSE;
    353     }
    354 
    355     /* Emit that number of bits of the value, if positive, */
    356     /* or the complement of its magnitude, if negative. */
    357     if ( nbits ) {      /* emit_bits rejects calls with size 0 */
    358         if ( !emit_bits( state, (unsigned int) temp2, nbits ) ) {
    359             return FALSE;
    360         }
    361     }
    362 
    363     /* Encode the AC coefficients per section F.1.2.2 */
    364 
    365     r = 0;          /* r = run length of zeros */
    366 
    367     for ( k = 1; k < DCTSIZE2; k++ ) {
    368         if ( ( temp = block[jpeg_natural_order[k]] ) == 0 ) {
    369             r++;
    370         } else {
    371             /* if run length > 15, must emit special run-length-16 codes (0xF0) */
    372             while ( r > 15 ) {
    373                 if ( !emit_bits( state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0] ) ) {
    374                     return FALSE;
    375                 }
    376                 r -= 16;
    377             }
    378 
    379             temp2 = temp;
    380             if ( temp < 0 ) {
    381                 temp = -temp;/* temp is abs value of input */
    382                 /* This code assumes we are on a two's complement machine */
    383                 temp2--;
    384             }
    385 
    386             /* Find the number of bits needed for the magnitude of the coefficient */
    387             nbits = 1;  /* there must be at least one 1 bit */
    388             while ( ( temp >>= 1 ) ) {
    389                 nbits++;
    390             }
    391 
    392             /* Emit Huffman symbol for run length / number of bits */
    393             i = ( r << 4 ) + nbits;
    394             if ( !emit_bits( state, actbl->ehufco[i], actbl->ehufsi[i] ) ) {
    395                 return FALSE;
    396             }
    397 
    398             /* Emit that number of bits of the value, if positive, */
    399             /* or the complement of its magnitude, if negative. */
    400             if ( !emit_bits( state, (unsigned int) temp2, nbits ) ) {
    401                 return FALSE;
    402             }
    403 
    404             r = 0;
    405         }
    406     }
    407 
    408     /* If the last coef(s) were zero, emit an end-of-block code */
    409     if ( r > 0 ) {
    410         if ( !emit_bits( state, actbl->ehufco[0], actbl->ehufsi[0] ) ) {
    411             return FALSE;
    412         }
    413     }
    414 
    415     return TRUE;
    416 }
    417 
    418 
    419 /*
    420  * Emit a restart marker & resynchronize predictions.
    421  */
    422 
    423 LOCAL boolean
    424 emit_restart( working_state * state, int restart_num ) {
    425     int ci;
    426 
    427     if ( !flush_bits( state ) ) {
    428         return FALSE;
    429     }
    430 
    431     emit_byte( state, 0xFF, return FALSE );
    432     emit_byte( state, JPEG_RST0 + restart_num, return FALSE );
    433 
    434     /* Re-initialize DC predictions to 0 */
    435     for ( ci = 0; ci < state->cinfo->comps_in_scan; ci++ ) {
    436         state->cur.last_dc_val[ci] = 0;
    437     }
    438 
    439     /* The restart counter is not updated until we successfully write the MCU. */
    440 
    441     return TRUE;
    442 }
    443 
    444 
    445 /*
    446  * Encode and output one MCU's worth of Huffman-compressed coefficients.
    447  */
    448 
    449 METHODDEF boolean
    450 encode_mcu_huff( j_compress_ptr cinfo, JBLOCKROW * MCU_data ) {
    451     huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
    452     working_state state;
    453     int blkn, ci;
    454     jpeg_component_info * compptr;
    455 
    456     /* Load up working state */
    457     state.next_output_byte = cinfo->dest->next_output_byte;
    458     state.free_in_buffer = cinfo->dest->free_in_buffer;
    459     ASSIGN_STATE( state.cur, entropy->saved );
    460     state.cinfo = cinfo;
    461 
    462     /* Emit restart marker if needed */
    463     if ( cinfo->restart_interval ) {
    464         if ( entropy->restarts_to_go == 0 ) {
    465             if ( !emit_restart( &state, entropy->next_restart_num ) ) {
    466                 return FALSE;
    467             }
    468         }
    469     }
    470 
    471     /* Encode the MCU data blocks */
    472     for ( blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++ ) {
    473         ci = cinfo->MCU_membership[blkn];
    474         compptr = cinfo->cur_comp_info[ci];
    475         if ( !encode_one_block( &state,
    476                                 MCU_data[blkn][0], state.cur.last_dc_val[ci],
    477                                 entropy->dc_derived_tbls[compptr->dc_tbl_no],
    478                                 entropy->ac_derived_tbls[compptr->ac_tbl_no] ) ) {
    479             return FALSE;
    480         }
    481         /* Update last_dc_val */
    482         state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
    483     }
    484 
    485     /* Completed MCU, so update state */
    486     cinfo->dest->next_output_byte = state.next_output_byte;
    487     cinfo->dest->free_in_buffer = state.free_in_buffer;
    488     ASSIGN_STATE( entropy->saved, state.cur );
    489 
    490     /* Update restart-interval state too */
    491     if ( cinfo->restart_interval ) {
    492         if ( entropy->restarts_to_go == 0 ) {
    493             entropy->restarts_to_go = cinfo->restart_interval;
    494             entropy->next_restart_num++;
    495             entropy->next_restart_num &= 7;
    496         }
    497         entropy->restarts_to_go--;
    498     }
    499 
    500     return TRUE;
    501 }
    502 
    503 
    504 /*
    505  * Finish up at the end of a Huffman-compressed scan.
    506  */
    507 
    508 METHODDEF void
    509 finish_pass_huff( j_compress_ptr cinfo ) {
    510     huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
    511     working_state state;
    512 
    513     /* Load up working state ... flush_bits needs it */
    514     state.next_output_byte = cinfo->dest->next_output_byte;
    515     state.free_in_buffer = cinfo->dest->free_in_buffer;
    516     ASSIGN_STATE( state.cur, entropy->saved );
    517     state.cinfo = cinfo;
    518 
    519     /* Flush out the last data */
    520     if ( !flush_bits( &state ) ) {
    521         ERREXIT( cinfo, JERR_CANT_SUSPEND );
    522     }
    523 
    524     /* Update state */
    525     cinfo->dest->next_output_byte = state.next_output_byte;
    526     cinfo->dest->free_in_buffer = state.free_in_buffer;
    527     ASSIGN_STATE( entropy->saved, state.cur );
    528 }
    529 
    530 
    531 /*
    532  * Huffman coding optimization.
    533  *
    534  * This actually is optimization, in the sense that we find the best possible
    535  * Huffman table(s) for the given data.  We first scan the supplied data and
    536  * count the number of uses of each symbol that is to be Huffman-coded.
    537  * (This process must agree with the code above.)  Then we build an
    538  * optimal Huffman coding tree for the observed counts.
    539  *
    540  * The JPEG standard requires Huffman codes to be no more than 16 bits long.
    541  * If some symbols have a very small but nonzero probability, the Huffman tree
    542  * must be adjusted to meet the code length restriction.  We currently use
    543  * the adjustment method suggested in the JPEG spec.  This method is *not*
    544  * optimal; it may not choose the best possible limited-length code.  But
    545  * since the symbols involved are infrequently used, it's not clear that
    546  * going to extra trouble is worthwhile.
    547  */
    548 
    549 #ifdef ENTROPY_OPT_SUPPORTED
    550 
    551 
    552 /* Process a single block's worth of coefficients */
    553 
    554 LOCAL void
    555 htest_one_block( JCOEFPTR block, int last_dc_val,
    556                  long dc_counts[], long ac_counts[] ) {
    557     register int temp;
    558     register int nbits;
    559     register int k, r;
    560 
    561     /* Encode the DC coefficient difference per section F.1.2.1 */
    562 
    563     temp = block[0] - last_dc_val;
    564     if ( temp < 0 ) {
    565         temp = -temp;
    566     }
    567 
    568     /* Find the number of bits needed for the magnitude of the coefficient */
    569     nbits = 0;
    570     while ( temp ) {
    571         nbits++;
    572         temp >>= 1;
    573     }
    574 
    575     /* Count the Huffman symbol for the number of bits */
    576     dc_counts[nbits]++;
    577 
    578     /* Encode the AC coefficients per section F.1.2.2 */
    579 
    580     r = 0;          /* r = run length of zeros */
    581 
    582     for ( k = 1; k < DCTSIZE2; k++ ) {
    583         if ( ( temp = block[jpeg_natural_order[k]] ) == 0 ) {
    584             r++;
    585         } else {
    586             /* if run length > 15, must emit special run-length-16 codes (0xF0) */
    587             while ( r > 15 ) {
    588                 ac_counts[0xF0]++;
    589                 r -= 16;
    590             }
    591 
    592             /* Find the number of bits needed for the magnitude of the coefficient */
    593             if ( temp < 0 ) {
    594                 temp = -temp;
    595             }
    596 
    597             /* Find the number of bits needed for the magnitude of the coefficient */
    598             nbits = 1;  /* there must be at least one 1 bit */
    599             while ( ( temp >>= 1 ) ) {
    600                 nbits++;
    601             }
    602 
    603             /* Count Huffman symbol for run length / number of bits */
    604             ac_counts[( r << 4 ) + nbits]++;
    605 
    606             r = 0;
    607         }
    608     }
    609 
    610     /* If the last coef(s) were zero, emit an end-of-block code */
    611     if ( r > 0 ) {
    612         ac_counts[0]++;
    613     }
    614 }
    615 
    616 
    617 /*
    618  * Trial-encode one MCU's worth of Huffman-compressed coefficients.
    619  * No data is actually output, so no suspension return is possible.
    620  */
    621 
    622 METHODDEF boolean
    623 encode_mcu_gather( j_compress_ptr cinfo, JBLOCKROW * MCU_data ) {
    624     huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
    625     int blkn, ci;
    626     jpeg_component_info * compptr;
    627 
    628     /* Take care of restart intervals if needed */
    629     if ( cinfo->restart_interval ) {
    630         if ( entropy->restarts_to_go == 0 ) {
    631             /* Re-initialize DC predictions to 0 */
    632             for ( ci = 0; ci < cinfo->comps_in_scan; ci++ ) {
    633                 entropy->saved.last_dc_val[ci] = 0;
    634             }
    635             /* Update restart state */
    636             entropy->restarts_to_go = cinfo->restart_interval;
    637         }
    638         entropy->restarts_to_go--;
    639     }
    640 
    641     for ( blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++ ) {
    642         ci = cinfo->MCU_membership[blkn];
    643         compptr = cinfo->cur_comp_info[ci];
    644         htest_one_block( MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
    645                          entropy->dc_count_ptrs[compptr->dc_tbl_no],
    646                          entropy->ac_count_ptrs[compptr->ac_tbl_no] );
    647         entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
    648     }
    649 
    650     return TRUE;
    651 }
    652 
    653 
    654 /*
    655  * Generate the optimal coding for the given counts, fill htbl.
    656  * Note this is also used by jcphuff.c.
    657  */
    658 
    659 GLOBAL void
    660 jpeg_gen_optimal_table( j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[] ) {
    661 #define MAX_CLEN 32     /* assumed maximum initial code length */
    662     UINT8 bits[MAX_CLEN + 1];/* bits[k] = # of symbols with code length k */
    663     int codesize[257];      /* codesize[k] = code length of symbol k */
    664     int others[257];    /* next symbol in current branch of tree */
    665     int c1, c2;
    666     int p, i, j;
    667     long v;
    668 
    669     /* This algorithm is explained in section K.2 of the JPEG standard */
    670 
    671     MEMZERO( bits, SIZEOF( bits ) );
    672     MEMZERO( codesize, SIZEOF( codesize ) );
    673     for ( i = 0; i < 257; i++ ) {
    674         others[i] = -1;
    675     }                   /* init links to empty */
    676 
    677     freq[256] = 1;      /* make sure there is a nonzero count */
    678     /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
    679      * that no real symbol is given code-value of all ones, because 256
    680      * will be placed in the largest codeword category.
    681      */
    682 
    683     /* Huffman's basic algorithm to assign optimal code lengths to symbols */
    684 
    685     for (;; ) {
    686         /* Find the smallest nonzero frequency, set c1 = its symbol */
    687         /* In case of ties, take the larger symbol number */
    688         c1 = -1;
    689         v = 1000000000L;
    690         for ( i = 0; i <= 256; i++ ) {
    691             if ( ( freq[i] ) && ( freq[i] <= v ) ) {
    692                 v = freq[i];
    693                 c1 = i;
    694             }
    695         }
    696 
    697         /* Find the next smallest nonzero frequency, set c2 = its symbol */
    698         /* In case of ties, take the larger symbol number */
    699         c2 = -1;
    700         v = 1000000000L;
    701         for ( i = 0; i <= 256; i++ ) {
    702             if ( ( freq[i] ) && ( freq[i] <= v ) && ( i != c1 ) ) {
    703                 v = freq[i];
    704                 c2 = i;
    705             }
    706         }
    707 
    708         /* Done if we've merged everything into one frequency */
    709         if ( c2 < 0 ) {
    710             break;
    711         }
    712 
    713         /* Else merge the two counts/trees */
    714         freq[c1] += freq[c2];
    715         freq[c2] = 0;
    716 
    717         /* Increment the codesize of everything in c1's tree branch */
    718         codesize[c1]++;
    719         while ( others[c1] >= 0 ) {
    720             c1 = others[c1];
    721             codesize[c1]++;
    722         }
    723 
    724         others[c1] = c2;    /* chain c2 onto c1's tree branch */
    725 
    726         /* Increment the codesize of everything in c2's tree branch */
    727         codesize[c2]++;
    728         while ( others[c2] >= 0 ) {
    729             c2 = others[c2];
    730             codesize[c2]++;
    731         }
    732     }
    733 
    734     /* Now count the number of symbols of each code length */
    735     for ( i = 0; i <= 256; i++ ) {
    736         if ( codesize[i] ) {
    737             /* The JPEG standard seems to think that this can't happen, */
    738             /* but I'm paranoid... */
    739             if ( codesize[i] > MAX_CLEN ) {
    740                 ERREXIT( cinfo, JERR_HUFF_CLEN_OVERFLOW );
    741             }
    742 
    743             bits[codesize[i]]++;
    744         }
    745     }
    746 
    747     /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
    748      * Huffman procedure assigned any such lengths, we must adjust the coding.
    749      * Here is what the JPEG spec says about how this next bit works:
    750      * Since symbols are paired for the longest Huffman code, the symbols are
    751      * removed from this length category two at a time.  The prefix for the pair
    752      * (which is one bit shorter) is allocated to one of the pair; then,
    753      * skipping the BITS entry for that prefix length, a code word from the next
    754      * shortest nonzero BITS entry is converted into a prefix for two code words
    755      * one bit longer.
    756      */
    757 
    758     for ( i = MAX_CLEN; i > 16; i-- ) {
    759         while ( bits[i] > 0 ) {
    760             j = i - 2;  /* find length of new prefix to be used */
    761             while ( bits[j] == 0 ) {
    762                 j--;
    763             }
    764 
    765             bits[i] -= 2;/* remove two symbols */
    766             bits[i - 1]++;/* one goes in this length */
    767             bits[j + 1] += 2;/* two new symbols in this length */
    768             bits[j]--;  /* symbol of this length is now a prefix */
    769         }
    770     }
    771 
    772     /* Remove the count for the pseudo-symbol 256 from the largest codelength */
    773     while ( bits[i] == 0 ) {/* find largest codelength still in use */
    774         i--;
    775     }
    776     bits[i]--;
    777 
    778     /* Return final symbol counts (only for lengths 0..16) */
    779     MEMCOPY( htbl->bits, bits, SIZEOF( htbl->bits ) );
    780 
    781     /* Return a list of the symbols sorted by code length */
    782     /* It's not real clear to me why we don't need to consider the codelength
    783      * changes made above, but the JPEG spec seems to think this works.
    784      */
    785     p = 0;
    786     for ( i = 1; i <= MAX_CLEN; i++ ) {
    787         for ( j = 0; j <= 255; j++ ) {
    788             if ( codesize[j] == i ) {
    789                 htbl->huffval[p] = (UINT8) j;
    790                 p++;
    791             }
    792         }
    793     }
    794 
    795     /* Set sent_table FALSE so updated table will be written to JPEG file. */
    796     htbl->sent_table = FALSE;
    797 }
    798 
    799 
    800 /*
    801  * Finish up a statistics-gathering pass and create the new Huffman tables.
    802  */
    803 
    804 METHODDEF void
    805 finish_pass_gather( j_compress_ptr cinfo ) {
    806     huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
    807     int ci, dctbl, actbl;
    808     jpeg_component_info * compptr;
    809     JHUFF_TBL ** htblptr;
    810     boolean did_dc[NUM_HUFF_TBLS];
    811     boolean did_ac[NUM_HUFF_TBLS];
    812 
    813     /* It's important not to apply jpeg_gen_optimal_table more than once
    814      * per table, because it clobbers the input frequency counts!
    815      */
    816     MEMZERO( did_dc, SIZEOF( did_dc ) );
    817     MEMZERO( did_ac, SIZEOF( did_ac ) );
    818 
    819     for ( ci = 0; ci < cinfo->comps_in_scan; ci++ ) {
    820         compptr = cinfo->cur_comp_info[ci];
    821         dctbl = compptr->dc_tbl_no;
    822         actbl = compptr->ac_tbl_no;
    823         if ( !did_dc[dctbl] ) {
    824             htblptr = &cinfo->dc_huff_tbl_ptrs[dctbl];
    825             if ( *htblptr == NULL ) {
    826                 *htblptr = jpeg_alloc_huff_table( (j_common_ptr) cinfo );
    827             }
    828             jpeg_gen_optimal_table( cinfo, *htblptr, entropy->dc_count_ptrs[dctbl] );
    829             did_dc[dctbl] = TRUE;
    830         }
    831         if ( !did_ac[actbl] ) {
    832             htblptr = &cinfo->ac_huff_tbl_ptrs[actbl];
    833             if ( *htblptr == NULL ) {
    834                 *htblptr = jpeg_alloc_huff_table( (j_common_ptr) cinfo );
    835             }
    836             jpeg_gen_optimal_table( cinfo, *htblptr, entropy->ac_count_ptrs[actbl] );
    837             did_ac[actbl] = TRUE;
    838         }
    839     }
    840 }
    841 
    842 
    843 #endif /* ENTROPY_OPT_SUPPORTED */
    844 
    845 
    846 /*
    847  * Module initialization routine for Huffman entropy encoding.
    848  */
    849 
    850 GLOBAL void
    851 jinit_huff_encoder( j_compress_ptr cinfo ) {
    852     huff_entropy_ptr entropy;
    853     int i;
    854 
    855     entropy = (huff_entropy_ptr)
    856               ( *cinfo->mem->alloc_small )( (j_common_ptr) cinfo, JPOOL_IMAGE,
    857                                            SIZEOF( huff_entropy_encoder ) );
    858     cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
    859     entropy->pub.start_pass = start_pass_huff;
    860 
    861     /* Mark tables unallocated */
    862     for ( i = 0; i < NUM_HUFF_TBLS; i++ ) {
    863         entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
    864 #ifdef ENTROPY_OPT_SUPPORTED
    865         entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
    866 #endif
    867     }
    868 }