jcphuff.c
Go to the documentation of this file.
00001 /*
00002  * jcphuff.c
00003  *
00004  * Copyright (C) 1995-1997, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains Huffman entropy encoding routines for progressive JPEG.
00009  *
00010  * We do not support output suspension in this module, since the library
00011  * currently does not allow multiple-scan files to be written with output
00012  * suspension.
00013  */
00014 
00015 #define JPEG_INTERNALS
00016 #include "jinclude.h"
00017 #include "jpeglib.h"
00018 #include "jchuff.h"             /* Declarations shared with jchuff.c */
00019 
00020 #ifdef C_PROGRESSIVE_SUPPORTED
00021 
00022 /* Expanded entropy encoder object for progressive Huffman encoding. */
00023 
00024 typedef struct {
00025   struct jpeg_entropy_encoder pub; /* public fields */
00026 
00027   /* Mode flag: TRUE for optimization, FALSE for actual data output */
00028   boolean gather_statistics;
00029 
00030   /* Bit-level coding status.
00031    * next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
00032    */
00033   JOCTET * next_output_byte;    /* => next byte to write in buffer */
00034   size_t free_in_buffer;        /* # of byte spaces remaining in buffer */
00035   INT32 put_buffer;             /* current bit-accumulation buffer */
00036   int put_bits;                 /* # of bits now in it */
00037   j_compress_ptr cinfo;         /* link to cinfo (needed for dump_buffer) */
00038 
00039   /* Coding status for DC components */
00040   int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
00041 
00042   /* Coding status for AC components */
00043   int ac_tbl_no;                /* the table number of the single component */
00044   unsigned int EOBRUN;          /* run length of EOBs */
00045   unsigned int BE;              /* # of buffered correction bits before MCU */
00046   char * bit_buffer;            /* buffer for correction bits (1 per char) */
00047   /* packing correction bits tightly would save some space but cost time... */
00048 
00049   unsigned int restarts_to_go;  /* MCUs left in this restart interval */
00050   int next_restart_num;         /* next restart number to write (0-7) */
00051 
00052   /* Pointers to derived tables (these workspaces have image lifespan).
00053    * Since any one scan codes only DC or only AC, we only need one set
00054    * of tables, not one for DC and one for AC.
00055    */
00056   c_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
00057 
00058   /* Statistics tables for optimization; again, one set is enough */
00059   long * count_ptrs[NUM_HUFF_TBLS];
00060 } phuff_entropy_encoder;
00061 
00062 typedef phuff_entropy_encoder * phuff_entropy_ptr;
00063 
00064 /* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
00065  * buffer can hold.  Larger sizes may slightly improve compression, but
00066  * 1000 is already well into the realm of overkill.
00067  * The minimum safe size is 64 bits.
00068  */
00069 
00070 #define MAX_CORR_BITS  1000     /* Max # of correction bits I can buffer */
00071 
00072 /* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
00073  * We assume that int right shift is unsigned if INT32 right shift is,
00074  * which should be safe.
00075  */
00076 
00077 #ifdef RIGHT_SHIFT_IS_UNSIGNED
00078 #define ISHIFT_TEMPS    int ishift_temp;
00079 #define IRIGHT_SHIFT(x,shft)  \
00080         ((ishift_temp = (x)) < 0 ? \
00081          (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
00082          (ishift_temp >> (shft)))
00083 #else
00084 #define ISHIFT_TEMPS
00085 #define IRIGHT_SHIFT(x,shft)    ((x) >> (shft))
00086 #endif
00087 
00088 /* Forward declarations */
00089 METHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo,
00090                                             JBLOCKROW *MCU_data));
00091 METHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo,
00092                                             JBLOCKROW *MCU_data));
00093 METHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo,
00094                                              JBLOCKROW *MCU_data));
00095 METHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo,
00096                                              JBLOCKROW *MCU_data));
00097 METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo));
00098 METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo));
00099 
00100 
00101 /*
00102  * Initialize for a Huffman-compressed scan using progressive JPEG.
00103  */
00104 
00105 METHODDEF(void)
00106 start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics)
00107 {  
00108   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
00109   boolean is_DC_band;
00110   int ci, tbl;
00111   jpeg_component_info * compptr;
00112 
00113   entropy->cinfo = cinfo;
00114   entropy->gather_statistics = gather_statistics;
00115 
00116   is_DC_band = (cinfo->Ss == 0);
00117 
00118   /* We assume jcmaster.c already validated the scan parameters. */
00119 
00120   /* Select execution routines */
00121   if (cinfo->Ah == 0) {
00122     if (is_DC_band)
00123       entropy->pub.encode_mcu = encode_mcu_DC_first;
00124     else
00125       entropy->pub.encode_mcu = encode_mcu_AC_first;
00126   } else {
00127     if (is_DC_band)
00128       entropy->pub.encode_mcu = encode_mcu_DC_refine;
00129     else {
00130       entropy->pub.encode_mcu = encode_mcu_AC_refine;
00131       /* AC refinement needs a correction bit buffer */
00132       if (entropy->bit_buffer == NULL)
00133         entropy->bit_buffer = (char *)
00134           (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00135                                       MAX_CORR_BITS * SIZEOF(char));
00136     }
00137   }
00138   if (gather_statistics)
00139     entropy->pub.finish_pass = finish_pass_gather_phuff;
00140   else
00141     entropy->pub.finish_pass = finish_pass_phuff;
00142 
00143   /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
00144    * for AC coefficients.
00145    */
00146   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00147     compptr = cinfo->cur_comp_info[ci];
00148     /* Initialize DC predictions to 0 */
00149     entropy->last_dc_val[ci] = 0;
00150     /* Get table index */
00151     if (is_DC_band) {
00152       if (cinfo->Ah != 0)       /* DC refinement needs no table */
00153         continue;
00154       tbl = compptr->dc_tbl_no;
00155     } else {
00156       entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
00157     }
00158     if (gather_statistics) {
00159       /* Check for invalid table index */
00160       /* (make_c_derived_tbl does this in the other path) */
00161       if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
00162         ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
00163       /* Allocate and zero the statistics tables */
00164       /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
00165       if (entropy->count_ptrs[tbl] == NULL)
00166         entropy->count_ptrs[tbl] = (long *)
00167           (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00168                                       257 * SIZEOF(long));
00169       MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long));
00170     } else {
00171       /* Compute derived values for Huffman table */
00172       /* We may do this more than once for a table, but it's not expensive */
00173       jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
00174                               & entropy->derived_tbls[tbl]);
00175     }
00176   }
00177 
00178   /* Initialize AC stuff */
00179   entropy->EOBRUN = 0;
00180   entropy->BE = 0;
00181 
00182   /* Initialize bit buffer to empty */
00183   entropy->put_buffer = 0;
00184   entropy->put_bits = 0;
00185 
00186   /* Initialize restart stuff */
00187   entropy->restarts_to_go = cinfo->restart_interval;
00188   entropy->next_restart_num = 0;
00189 }
00190 
00191 
00192 /* Outputting bytes to the file.
00193  * NB: these must be called only when actually outputting,
00194  * that is, entropy->gather_statistics == FALSE.
00195  */
00196 
00197 /* Emit a byte */
00198 #define emit_byte(entropy,val)  \
00199         { *(entropy)->next_output_byte++ = (JOCTET) (val);  \
00200           if (--(entropy)->free_in_buffer == 0)  \
00201             dump_buffer(entropy); }
00202 
00203 
00204 LOCAL(void)
00205 dump_buffer (phuff_entropy_ptr entropy)
00206 /* Empty the output buffer; we do not support suspension in this module. */
00207 {
00208   struct jpeg_destination_mgr * dest = entropy->cinfo->dest;
00209 
00210   if (! (*dest->empty_output_buffer) (entropy->cinfo))
00211     ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
00212   /* After a successful buffer dump, must reset buffer pointers */
00213   entropy->next_output_byte = dest->next_output_byte;
00214   entropy->free_in_buffer = dest->free_in_buffer;
00215 }
00216 
00217 
00218 /* Outputting bits to the file */
00219 
00220 /* Only the right 24 bits of put_buffer are used; the valid bits are
00221  * left-justified in this part.  At most 16 bits can be passed to emit_bits
00222  * in one call, and we never retain more than 7 bits in put_buffer
00223  * between calls, so 24 bits are sufficient.
00224  */
00225 
00226 INLINE
00227 LOCAL(void)
00228 emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size)
00229 /* Emit some bits, unless we are in gather mode */
00230 {
00231   /* This routine is heavily used, so it's worth coding tightly. */
00232   register INT32 put_buffer = (INT32) code;
00233   register int put_bits = entropy->put_bits;
00234 
00235   /* if size is 0, caller used an invalid Huffman table entry */
00236   if (size == 0)
00237     ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
00238 
00239   if (entropy->gather_statistics)
00240     return;                     /* do nothing if we're only getting stats */
00241 
00242   put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
00243   
00244   put_bits += size;             /* new number of bits in buffer */
00245   
00246   put_buffer <<= 24 - put_bits; /* align incoming bits */
00247 
00248   put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */
00249 
00250   while (put_bits >= 8) {
00251     int c = (int) ((put_buffer >> 16) & 0xFF);
00252     
00253     emit_byte(entropy, c);
00254     if (c == 0xFF) {            /* need to stuff a zero byte? */
00255       emit_byte(entropy, 0);
00256     }
00257     put_buffer <<= 8;
00258     put_bits -= 8;
00259   }
00260 
00261   entropy->put_buffer = put_buffer; /* update variables */
00262   entropy->put_bits = put_bits;
00263 }
00264 
00265 
00266 LOCAL(void)
00267 flush_bits (phuff_entropy_ptr entropy)
00268 {
00269   emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */
00270   entropy->put_buffer = 0;     /* and reset bit-buffer to empty */
00271   entropy->put_bits = 0;
00272 }
00273 
00274 
00275 /*
00276  * Emit (or just count) a Huffman symbol.
00277  */
00278 
00279 INLINE
00280 LOCAL(void)
00281 emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol)
00282 {
00283   if (entropy->gather_statistics)
00284     entropy->count_ptrs[tbl_no][symbol]++;
00285   else {
00286     c_derived_tbl * tbl = entropy->derived_tbls[tbl_no];
00287     emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
00288   }
00289 }
00290 
00291 
00292 /*
00293  * Emit bits from a correction bit buffer.
00294  */
00295 
00296 LOCAL(void)
00297 emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart,
00298                     unsigned int nbits)
00299 {
00300   if (entropy->gather_statistics)
00301     return;                     /* no real work */
00302 
00303   while (nbits > 0) {
00304     emit_bits(entropy, (unsigned int) (*bufstart), 1);
00305     bufstart++;
00306     nbits--;
00307   }
00308 }
00309 
00310 
00311 /*
00312  * Emit any pending EOBRUN symbol.
00313  */
00314 
00315 LOCAL(void)
00316 emit_eobrun (phuff_entropy_ptr entropy)
00317 {
00318   register int temp, nbits;
00319 
00320   if (entropy->EOBRUN > 0) {    /* if there is any pending EOBRUN */
00321     temp = entropy->EOBRUN;
00322     nbits = 0;
00323     while ((temp >>= 1))
00324       nbits++;
00325     /* safety check: shouldn't happen given limited correction-bit buffer */
00326     if (nbits > 14)
00327       ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
00328 
00329     emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
00330     if (nbits)
00331       emit_bits(entropy, entropy->EOBRUN, nbits);
00332 
00333     entropy->EOBRUN = 0;
00334 
00335     /* Emit any buffered correction bits */
00336     emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
00337     entropy->BE = 0;
00338   }
00339 }
00340 
00341 
00342 /*
00343  * Emit a restart marker & resynchronize predictions.
00344  */
00345 
00346 LOCAL(void)
00347 emit_restart (phuff_entropy_ptr entropy, int restart_num)
00348 {
00349   int ci;
00350 
00351   emit_eobrun(entropy);
00352 
00353   if (! entropy->gather_statistics) {
00354     flush_bits(entropy);
00355     emit_byte(entropy, 0xFF);
00356     emit_byte(entropy, JPEG_RST0 + restart_num);
00357   }
00358 
00359   if (entropy->cinfo->Ss == 0) {
00360     /* Re-initialize DC predictions to 0 */
00361     for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
00362       entropy->last_dc_val[ci] = 0;
00363   } else {
00364     /* Re-initialize all AC-related fields to 0 */
00365     entropy->EOBRUN = 0;
00366     entropy->BE = 0;
00367   }
00368 }
00369 
00370 
00371 /*
00372  * MCU encoding for DC initial scan (either spectral selection,
00373  * or first pass of successive approximation).
00374  */
00375 
00376 METHODDEF(boolean)
00377 encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
00378 {
00379   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
00380   register int temp, temp2;
00381   register int nbits;
00382   int blkn, ci;
00383   int Al = cinfo->Al;
00384   JBLOCKROW block;
00385   jpeg_component_info * compptr;
00386   ISHIFT_TEMPS
00387 
00388   entropy->next_output_byte = cinfo->dest->next_output_byte;
00389   entropy->free_in_buffer = cinfo->dest->free_in_buffer;
00390 
00391   /* Emit restart marker if needed */
00392   if (cinfo->restart_interval)
00393     if (entropy->restarts_to_go == 0)
00394       emit_restart(entropy, entropy->next_restart_num);
00395 
00396   /* Encode the MCU data blocks */
00397   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
00398     block = MCU_data[blkn];
00399     ci = cinfo->MCU_membership[blkn];
00400     compptr = cinfo->cur_comp_info[ci];
00401 
00402     /* Compute the DC value after the required point transform by Al.
00403      * This is simply an arithmetic right shift.
00404      */
00405     temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);
00406 
00407     /* DC differences are figured on the point-transformed values. */
00408     temp = temp2 - entropy->last_dc_val[ci];
00409     entropy->last_dc_val[ci] = temp2;
00410 
00411     /* Encode the DC coefficient difference per section G.1.2.1 */
00412     temp2 = temp;
00413     if (temp < 0) {
00414       temp = -temp;             /* temp is abs value of input */
00415       /* For a negative input, want temp2 = bitwise complement of abs(input) */
00416       /* This code assumes we are on a two's complement machine */
00417       temp2--;
00418     }
00419     
00420     /* Find the number of bits needed for the magnitude of the coefficient */
00421     nbits = 0;
00422     while (temp) {
00423       nbits++;
00424       temp >>= 1;
00425     }
00426     /* Check for out-of-range coefficient values.
00427      * Since we're encoding a difference, the range limit is twice as much.
00428      */
00429     if (nbits > MAX_COEF_BITS+1)
00430       ERREXIT(cinfo, JERR_BAD_DCT_COEF);
00431     
00432     /* Count/emit the Huffman-coded symbol for the number of bits */
00433     emit_symbol(entropy, compptr->dc_tbl_no, nbits);
00434     
00435     /* Emit that number of bits of the value, if positive, */
00436     /* or the complement of its magnitude, if negative. */
00437     if (nbits)                  /* emit_bits rejects calls with size 0 */
00438       emit_bits(entropy, (unsigned int) temp2, nbits);
00439   }
00440 
00441   cinfo->dest->next_output_byte = entropy->next_output_byte;
00442   cinfo->dest->free_in_buffer = entropy->free_in_buffer;
00443 
00444   /* Update restart-interval state too */
00445   if (cinfo->restart_interval) {
00446     if (entropy->restarts_to_go == 0) {
00447       entropy->restarts_to_go = cinfo->restart_interval;
00448       entropy->next_restart_num++;
00449       entropy->next_restart_num &= 7;
00450     }
00451     entropy->restarts_to_go--;
00452   }
00453 
00454   return TRUE;
00455 }
00456 
00457 
00458 /*
00459  * MCU encoding for AC initial scan (either spectral selection,
00460  * or first pass of successive approximation).
00461  */
00462 
00463 METHODDEF(boolean)
00464 encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
00465 {
00466   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
00467   register int temp, temp2;
00468   register int nbits;
00469   register int r, k;
00470   int Se = cinfo->Se;
00471   int Al = cinfo->Al;
00472   JBLOCKROW block;
00473 
00474   entropy->next_output_byte = cinfo->dest->next_output_byte;
00475   entropy->free_in_buffer = cinfo->dest->free_in_buffer;
00476 
00477   /* Emit restart marker if needed */
00478   if (cinfo->restart_interval)
00479     if (entropy->restarts_to_go == 0)
00480       emit_restart(entropy, entropy->next_restart_num);
00481 
00482   /* Encode the MCU data block */
00483   block = MCU_data[0];
00484 
00485   /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
00486   
00487   r = 0;                        /* r = run length of zeros */
00488    
00489   for (k = cinfo->Ss; k <= Se; k++) {
00490     if ((temp = (*block)[jpeg_natural_order[k]]) == 0) {
00491       r++;
00492       continue;
00493     }
00494     /* We must apply the point transform by Al.  For AC coefficients this
00495      * is an integer division with rounding towards 0.  To do this portably
00496      * in C, we shift after obtaining the absolute value; so the code is
00497      * interwoven with finding the abs value (temp) and output bits (temp2).
00498      */
00499     if (temp < 0) {
00500       temp = -temp;             /* temp is abs value of input */
00501       temp >>= Al;              /* apply the point transform */
00502       /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
00503       temp2 = ~temp;
00504     } else {
00505       temp >>= Al;              /* apply the point transform */
00506       temp2 = temp;
00507     }
00508     /* Watch out for case that nonzero coef is zero after point transform */
00509     if (temp == 0) {
00510       r++;
00511       continue;
00512     }
00513 
00514     /* Emit any pending EOBRUN */
00515     if (entropy->EOBRUN > 0)
00516       emit_eobrun(entropy);
00517     /* if run length > 15, must emit special run-length-16 codes (0xF0) */
00518     while (r > 15) {
00519       emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
00520       r -= 16;
00521     }
00522 
00523     /* Find the number of bits needed for the magnitude of the coefficient */
00524     nbits = 1;                  /* there must be at least one 1 bit */
00525     while ((temp >>= 1))
00526       nbits++;
00527     /* Check for out-of-range coefficient values */
00528     if (nbits > MAX_COEF_BITS)
00529       ERREXIT(cinfo, JERR_BAD_DCT_COEF);
00530 
00531     /* Count/emit Huffman symbol for run length / number of bits */
00532     emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);
00533 
00534     /* Emit that number of bits of the value, if positive, */
00535     /* or the complement of its magnitude, if negative. */
00536     emit_bits(entropy, (unsigned int) temp2, nbits);
00537 
00538     r = 0;                      /* reset zero run length */
00539   }
00540 
00541   if (r > 0) {                  /* If there are trailing zeroes, */
00542     entropy->EOBRUN++;          /* count an EOB */
00543     if (entropy->EOBRUN == 0x7FFF)
00544       emit_eobrun(entropy);     /* force it out to avoid overflow */
00545   }
00546 
00547   cinfo->dest->next_output_byte = entropy->next_output_byte;
00548   cinfo->dest->free_in_buffer = entropy->free_in_buffer;
00549 
00550   /* Update restart-interval state too */
00551   if (cinfo->restart_interval) {
00552     if (entropy->restarts_to_go == 0) {
00553       entropy->restarts_to_go = cinfo->restart_interval;
00554       entropy->next_restart_num++;
00555       entropy->next_restart_num &= 7;
00556     }
00557     entropy->restarts_to_go--;
00558   }
00559 
00560   return TRUE;
00561 }
00562 
00563 
00564 /*
00565  * MCU encoding for DC successive approximation refinement scan.
00566  * Note: we assume such scans can be multi-component, although the spec
00567  * is not very clear on the point.
00568  */
00569 
00570 METHODDEF(boolean)
00571 encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
00572 {
00573   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
00574   register int temp;
00575   int blkn;
00576   int Al = cinfo->Al;
00577   JBLOCKROW block;
00578 
00579   entropy->next_output_byte = cinfo->dest->next_output_byte;
00580   entropy->free_in_buffer = cinfo->dest->free_in_buffer;
00581 
00582   /* Emit restart marker if needed */
00583   if (cinfo->restart_interval)
00584     if (entropy->restarts_to_go == 0)
00585       emit_restart(entropy, entropy->next_restart_num);
00586 
00587   /* Encode the MCU data blocks */
00588   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
00589     block = MCU_data[blkn];
00590 
00591     /* We simply emit the Al'th bit of the DC coefficient value. */
00592     temp = (*block)[0];
00593     emit_bits(entropy, (unsigned int) (temp >> Al), 1);
00594   }
00595 
00596   cinfo->dest->next_output_byte = entropy->next_output_byte;
00597   cinfo->dest->free_in_buffer = entropy->free_in_buffer;
00598 
00599   /* Update restart-interval state too */
00600   if (cinfo->restart_interval) {
00601     if (entropy->restarts_to_go == 0) {
00602       entropy->restarts_to_go = cinfo->restart_interval;
00603       entropy->next_restart_num++;
00604       entropy->next_restart_num &= 7;
00605     }
00606     entropy->restarts_to_go--;
00607   }
00608 
00609   return TRUE;
00610 }
00611 
00612 
00613 /*
00614  * MCU encoding for AC successive approximation refinement scan.
00615  */
00616 
00617 METHODDEF(boolean)
00618 encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
00619 {
00620   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
00621   register int temp;
00622   register int r, k;
00623   int EOB;
00624   char *BR_buffer;
00625   unsigned int BR;
00626   int Se = cinfo->Se;
00627   int Al = cinfo->Al;
00628   JBLOCKROW block;
00629   int absvalues[DCTSIZE2];
00630 
00631   entropy->next_output_byte = cinfo->dest->next_output_byte;
00632   entropy->free_in_buffer = cinfo->dest->free_in_buffer;
00633 
00634   /* Emit restart marker if needed */
00635   if (cinfo->restart_interval)
00636     if (entropy->restarts_to_go == 0)
00637       emit_restart(entropy, entropy->next_restart_num);
00638 
00639   /* Encode the MCU data block */
00640   block = MCU_data[0];
00641 
00642   /* It is convenient to make a pre-pass to determine the transformed
00643    * coefficients' absolute values and the EOB position.
00644    */
00645   EOB = 0;
00646   for (k = cinfo->Ss; k <= Se; k++) {
00647     temp = (*block)[jpeg_natural_order[k]];
00648     /* We must apply the point transform by Al.  For AC coefficients this
00649      * is an integer division with rounding towards 0.  To do this portably
00650      * in C, we shift after obtaining the absolute value.
00651      */
00652     if (temp < 0)
00653       temp = -temp;             /* temp is abs value of input */
00654     temp >>= Al;                /* apply the point transform */
00655     absvalues[k] = temp;        /* save abs value for main pass */
00656     if (temp == 1)
00657       EOB = k;                  /* EOB = index of last newly-nonzero coef */
00658   }
00659 
00660   /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
00661   
00662   r = 0;                        /* r = run length of zeros */
00663   BR = 0;                       /* BR = count of buffered bits added now */
00664   BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
00665 
00666   for (k = cinfo->Ss; k <= Se; k++) {
00667     if ((temp = absvalues[k]) == 0) {
00668       r++;
00669       continue;
00670     }
00671 
00672     /* Emit any required ZRLs, but not if they can be folded into EOB */
00673     while (r > 15 && k <= EOB) {
00674       /* emit any pending EOBRUN and the BE correction bits */
00675       emit_eobrun(entropy);
00676       /* Emit ZRL */
00677       emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
00678       r -= 16;
00679       /* Emit buffered correction bits that must be associated with ZRL */
00680       emit_buffered_bits(entropy, BR_buffer, BR);
00681       BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
00682       BR = 0;
00683     }
00684 
00685     /* If the coef was previously nonzero, it only needs a correction bit.
00686      * NOTE: a straight translation of the spec's figure G.7 would suggest
00687      * that we also need to test r > 15.  But if r > 15, we can only get here
00688      * if k > EOB, which implies that this coefficient is not 1.
00689      */
00690     if (temp > 1) {
00691       /* The correction bit is the next bit of the absolute value. */
00692       BR_buffer[BR++] = (char) (temp & 1);
00693       continue;
00694     }
00695 
00696     /* Emit any pending EOBRUN and the BE correction bits */
00697     emit_eobrun(entropy);
00698 
00699     /* Count/emit Huffman symbol for run length / number of bits */
00700     emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);
00701 
00702     /* Emit output bit for newly-nonzero coef */
00703     temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1;
00704     emit_bits(entropy, (unsigned int) temp, 1);
00705 
00706     /* Emit buffered correction bits that must be associated with this code */
00707     emit_buffered_bits(entropy, BR_buffer, BR);
00708     BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
00709     BR = 0;
00710     r = 0;                      /* reset zero run length */
00711   }
00712 
00713   if (r > 0 || BR > 0) {        /* If there are trailing zeroes, */
00714     entropy->EOBRUN++;          /* count an EOB */
00715     entropy->BE += BR;          /* concat my correction bits to older ones */
00716     /* We force out the EOB if we risk either:
00717      * 1. overflow of the EOB counter;
00718      * 2. overflow of the correction bit buffer during the next MCU.
00719      */
00720     if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
00721       emit_eobrun(entropy);
00722   }
00723 
00724   cinfo->dest->next_output_byte = entropy->next_output_byte;
00725   cinfo->dest->free_in_buffer = entropy->free_in_buffer;
00726 
00727   /* Update restart-interval state too */
00728   if (cinfo->restart_interval) {
00729     if (entropy->restarts_to_go == 0) {
00730       entropy->restarts_to_go = cinfo->restart_interval;
00731       entropy->next_restart_num++;
00732       entropy->next_restart_num &= 7;
00733     }
00734     entropy->restarts_to_go--;
00735   }
00736 
00737   return TRUE;
00738 }
00739 
00740 
00741 /*
00742  * Finish up at the end of a Huffman-compressed progressive scan.
00743  */
00744 
00745 METHODDEF(void)
00746 finish_pass_phuff (j_compress_ptr cinfo)
00747 {   
00748   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
00749 
00750   entropy->next_output_byte = cinfo->dest->next_output_byte;
00751   entropy->free_in_buffer = cinfo->dest->free_in_buffer;
00752 
00753   /* Flush out any buffered data */
00754   emit_eobrun(entropy);
00755   flush_bits(entropy);
00756 
00757   cinfo->dest->next_output_byte = entropy->next_output_byte;
00758   cinfo->dest->free_in_buffer = entropy->free_in_buffer;
00759 }
00760 
00761 
00762 /*
00763  * Finish up a statistics-gathering pass and create the new Huffman tables.
00764  */
00765 
00766 METHODDEF(void)
00767 finish_pass_gather_phuff (j_compress_ptr cinfo)
00768 {
00769   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
00770   boolean is_DC_band;
00771   int ci, tbl;
00772   jpeg_component_info * compptr;
00773   JHUFF_TBL **htblptr;
00774   boolean did[NUM_HUFF_TBLS];
00775 
00776   /* Flush out buffered data (all we care about is counting the EOB symbol) */
00777   emit_eobrun(entropy);
00778 
00779   is_DC_band = (cinfo->Ss == 0);
00780 
00781   /* It's important not to apply jpeg_gen_optimal_table more than once
00782    * per table, because it clobbers the input frequency counts!
00783    */
00784   MEMZERO(did, SIZEOF(did));
00785 
00786   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00787     compptr = cinfo->cur_comp_info[ci];
00788     if (is_DC_band) {
00789       if (cinfo->Ah != 0)       /* DC refinement needs no table */
00790         continue;
00791       tbl = compptr->dc_tbl_no;
00792     } else {
00793       tbl = compptr->ac_tbl_no;
00794     }
00795     if (! did[tbl]) {
00796       if (is_DC_band)
00797         htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
00798       else
00799         htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
00800       if (*htblptr == NULL)
00801         *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
00802       jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);
00803       did[tbl] = TRUE;
00804     }
00805   }
00806 }
00807 
00808 
00809 /*
00810  * Module initialization routine for progressive Huffman entropy encoding.
00811  */
00812 
00813 GLOBAL(void)
00814 jinit_phuff_encoder (j_compress_ptr cinfo)
00815 {
00816   phuff_entropy_ptr entropy;
00817   int i;
00818 
00819   entropy = (phuff_entropy_ptr)
00820     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00821                                 SIZEOF(phuff_entropy_encoder));
00822   cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
00823   entropy->pub.start_pass = start_pass_phuff;
00824 
00825   /* Mark tables unallocated */
00826   for (i = 0; i < NUM_HUFF_TBLS; i++) {
00827     entropy->derived_tbls[i] = NULL;
00828     entropy->count_ptrs[i] = NULL;
00829   }
00830   entropy->bit_buffer = NULL;   /* needed only in AC refinement scan */
00831 }
00832 
00833 #endif /* C_PROGRESSIVE_SUPPORTED */


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:55