LCOV - code coverage report
Current view: directory - media/libjpeg - jdphuff.c (source / functions) Found Hit Coverage
Test: app.info Lines: 231 0 0.0 %
Date: 2012-06-02 Functions: 7 0 0.0 %

       1                 : /*
       2                 :  * jdphuff.c
       3                 :  *
       4                 :  * Copyright (C) 1995-1997, 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 decoding routines for progressive JPEG.
       9                 :  *
      10                 :  * Much of the complexity here has to do with supporting input suspension.
      11                 :  * If the data source module demands suspension, we want to be able to back
      12                 :  * up to the start of the current MCU.  To do this, we copy state variables
      13                 :  * into local working storage, and update them back to the permanent
      14                 :  * storage only upon successful completion of an MCU.
      15                 :  */
      16                 : 
      17                 : #define JPEG_INTERNALS
      18                 : #include "jinclude.h"
      19                 : #include "jpeglib.h"
      20                 : #include "jdhuff.h"           /* Declarations shared with jdhuff.c */
      21                 : 
      22                 : 
      23                 : #ifdef D_PROGRESSIVE_SUPPORTED
      24                 : 
      25                 : /*
      26                 :  * Expanded entropy decoder object for progressive Huffman decoding.
      27                 :  *
      28                 :  * The savable_state subrecord contains fields that change within an MCU,
      29                 :  * but must not be updated permanently until we complete the MCU.
      30                 :  */
      31                 : 
      32                 : typedef struct {
      33                 :   unsigned int EOBRUN;                  /* remaining EOBs in EOBRUN */
      34                 :   int last_dc_val[MAX_COMPS_IN_SCAN];   /* last DC coef for each component */
      35                 : } savable_state;
      36                 : 
      37                 : /* This macro is to work around compilers with missing or broken
      38                 :  * structure assignment.  You'll need to fix this code if you have
      39                 :  * such a compiler and you change MAX_COMPS_IN_SCAN.
      40                 :  */
      41                 : 
      42                 : #ifndef NO_STRUCT_ASSIGN
      43                 : #define ASSIGN_STATE(dest,src)  ((dest) = (src))
      44                 : #else
      45                 : #if MAX_COMPS_IN_SCAN == 4
      46                 : #define ASSIGN_STATE(dest,src)  \
      47                 :         ((dest).EOBRUN = (src).EOBRUN, \
      48                 :          (dest).last_dc_val[0] = (src).last_dc_val[0], \
      49                 :          (dest).last_dc_val[1] = (src).last_dc_val[1], \
      50                 :          (dest).last_dc_val[2] = (src).last_dc_val[2], \
      51                 :          (dest).last_dc_val[3] = (src).last_dc_val[3])
      52                 : #endif
      53                 : #endif
      54                 : 
      55                 : 
      56                 : typedef struct {
      57                 :   struct jpeg_entropy_decoder pub; /* public fields */
      58                 : 
      59                 :   /* These fields are loaded into local variables at start of each MCU.
      60                 :    * In case of suspension, we exit WITHOUT updating them.
      61                 :    */
      62                 :   bitread_perm_state bitstate;  /* Bit buffer at start of MCU */
      63                 :   savable_state saved;          /* Other state at start of MCU */
      64                 : 
      65                 :   /* These fields are NOT loaded into local working state. */
      66                 :   unsigned int restarts_to_go;  /* MCUs left in this restart interval */
      67                 : 
      68                 :   /* Pointers to derived tables (these workspaces have image lifespan) */
      69                 :   d_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
      70                 : 
      71                 :   d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */
      72                 : } phuff_entropy_decoder;
      73                 : 
      74                 : typedef phuff_entropy_decoder * phuff_entropy_ptr;
      75                 : 
      76                 : /* Forward declarations */
      77                 : METHODDEF(boolean) decode_mcu_DC_first JPP((j_decompress_ptr cinfo,
      78                 :                                             JBLOCKROW *MCU_data));
      79                 : METHODDEF(boolean) decode_mcu_AC_first JPP((j_decompress_ptr cinfo,
      80                 :                                             JBLOCKROW *MCU_data));
      81                 : METHODDEF(boolean) decode_mcu_DC_refine JPP((j_decompress_ptr cinfo,
      82                 :                                              JBLOCKROW *MCU_data));
      83                 : METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo,
      84                 :                                              JBLOCKROW *MCU_data));
      85                 : 
      86                 : 
      87                 : /*
      88                 :  * Initialize for a Huffman-compressed scan.
      89                 :  */
      90                 : 
      91                 : METHODDEF(void)
      92               0 : start_pass_phuff_decoder (j_decompress_ptr cinfo)
      93                 : {
      94               0 :   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
      95                 :   boolean is_DC_band, bad;
      96                 :   int ci, coefi, tbl;
      97                 :   int *coef_bit_ptr;
      98                 :   jpeg_component_info * compptr;
      99                 : 
     100               0 :   is_DC_band = (cinfo->Ss == 0);
     101                 : 
     102                 :   /* Validate scan parameters */
     103               0 :   bad = FALSE;
     104               0 :   if (is_DC_band) {
     105               0 :     if (cinfo->Se != 0)
     106               0 :       bad = TRUE;
     107                 :   } else {
     108                 :     /* need not check Ss/Se < 0 since they came from unsigned bytes */
     109               0 :     if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
     110               0 :       bad = TRUE;
     111                 :     /* AC scans may have only one component */
     112               0 :     if (cinfo->comps_in_scan != 1)
     113               0 :       bad = TRUE;
     114                 :   }
     115               0 :   if (cinfo->Ah != 0) {
     116                 :     /* Successive approximation refinement scan: must have Al = Ah-1. */
     117               0 :     if (cinfo->Al != cinfo->Ah-1)
     118               0 :       bad = TRUE;
     119                 :   }
     120               0 :   if (cinfo->Al > 13)             /* need not check for < 0 */
     121               0 :     bad = TRUE;
     122                 :   /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
     123                 :    * but the spec doesn't say so, and we try to be liberal about what we
     124                 :    * accept.  Note: large Al values could result in out-of-range DC
     125                 :    * coefficients during early scans, leading to bizarre displays due to
     126                 :    * overflows in the IDCT math.  But we won't crash.
     127                 :    */
     128               0 :   if (bad)
     129               0 :     ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
     130                 :              cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
     131                 :   /* Update progression status, and verify that scan order is legal.
     132                 :    * Note that inter-scan inconsistencies are treated as warnings
     133                 :    * not fatal errors ... not clear if this is right way to behave.
     134                 :    */
     135               0 :   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
     136               0 :     int cindex = cinfo->cur_comp_info[ci]->component_index;
     137               0 :     coef_bit_ptr = & cinfo->coef_bits[cindex][0];
     138               0 :     if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
     139               0 :       WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
     140               0 :     for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
     141               0 :       int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
     142               0 :       if (cinfo->Ah != expected)
     143               0 :         WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
     144               0 :       coef_bit_ptr[coefi] = cinfo->Al;
     145                 :     }
     146                 :   }
     147                 : 
     148                 :   /* Select MCU decoding routine */
     149               0 :   if (cinfo->Ah == 0) {
     150               0 :     if (is_DC_band)
     151               0 :       entropy->pub.decode_mcu = decode_mcu_DC_first;
     152                 :     else
     153               0 :       entropy->pub.decode_mcu = decode_mcu_AC_first;
     154                 :   } else {
     155               0 :     if (is_DC_band)
     156               0 :       entropy->pub.decode_mcu = decode_mcu_DC_refine;
     157                 :     else
     158               0 :       entropy->pub.decode_mcu = decode_mcu_AC_refine;
     159                 :   }
     160                 : 
     161               0 :   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
     162               0 :     compptr = cinfo->cur_comp_info[ci];
     163                 :     /* Make sure requested tables are present, and compute derived tables.
     164                 :      * We may build same derived table more than once, but it's not expensive.
     165                 :      */
     166               0 :     if (is_DC_band) {
     167               0 :       if (cinfo->Ah == 0) {  /* DC refinement needs no table */
     168               0 :         tbl = compptr->dc_tbl_no;
     169               0 :         jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
     170               0 :                                 & entropy->derived_tbls[tbl]);
     171                 :       }
     172                 :     } else {
     173               0 :       tbl = compptr->ac_tbl_no;
     174               0 :       jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
     175               0 :                               & entropy->derived_tbls[tbl]);
     176                 :       /* remember the single active table */
     177               0 :       entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
     178                 :     }
     179                 :     /* Initialize DC predictions to 0 */
     180               0 :     entropy->saved.last_dc_val[ci] = 0;
     181                 :   }
     182                 : 
     183                 :   /* Initialize bitread state variables */
     184               0 :   entropy->bitstate.bits_left = 0;
     185               0 :   entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
     186               0 :   entropy->pub.insufficient_data = FALSE;
     187                 : 
     188                 :   /* Initialize private state variables */
     189               0 :   entropy->saved.EOBRUN = 0;
     190                 : 
     191                 :   /* Initialize restart counter */
     192               0 :   entropy->restarts_to_go = cinfo->restart_interval;
     193               0 : }
     194                 : 
     195                 : 
     196                 : /*
     197                 :  * Figure F.12: extend sign bit.
     198                 :  * On some machines, a shift and add will be faster than a table lookup.
     199                 :  */
     200                 : 
     201                 : #ifdef AVOID_TABLES
     202                 : 
     203                 : #define HUFF_EXTEND(x,s)  ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
     204                 : 
     205                 : #else
     206                 : 
     207                 : #define HUFF_EXTEND(x,s)  ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
     208                 : 
     209                 : static const int extend_test[16] =   /* entry n is 2**(n-1) */
     210                 :   { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
     211                 :     0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
     212                 : 
     213                 : static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
     214                 :   { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
     215                 :     ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
     216                 :     ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
     217                 :     ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
     218                 : 
     219                 : #endif /* AVOID_TABLES */
     220                 : 
     221                 : 
     222                 : /*
     223                 :  * Check for a restart marker & resynchronize decoder.
     224                 :  * Returns FALSE if must suspend.
     225                 :  */
     226                 : 
     227                 : LOCAL(boolean)
     228               0 : process_restart (j_decompress_ptr cinfo)
     229                 : {
     230               0 :   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
     231                 :   int ci;
     232                 : 
     233                 :   /* Throw away any unused bits remaining in bit buffer; */
     234                 :   /* include any full bytes in next_marker's count of discarded bytes */
     235               0 :   cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
     236               0 :   entropy->bitstate.bits_left = 0;
     237                 : 
     238                 :   /* Advance past the RSTn marker */
     239               0 :   if (! (*cinfo->marker->read_restart_marker) (cinfo))
     240               0 :     return FALSE;
     241                 : 
     242                 :   /* Re-initialize DC predictions to 0 */
     243               0 :   for (ci = 0; ci < cinfo->comps_in_scan; ci++)
     244               0 :     entropy->saved.last_dc_val[ci] = 0;
     245                 :   /* Re-init EOB run count, too */
     246               0 :   entropy->saved.EOBRUN = 0;
     247                 : 
     248                 :   /* Reset restart counter */
     249               0 :   entropy->restarts_to_go = cinfo->restart_interval;
     250                 : 
     251                 :   /* Reset out-of-data flag, unless read_restart_marker left us smack up
     252                 :    * against a marker.  In that case we will end up treating the next data
     253                 :    * segment as empty, and we can avoid producing bogus output pixels by
     254                 :    * leaving the flag set.
     255                 :    */
     256               0 :   if (cinfo->unread_marker == 0)
     257               0 :     entropy->pub.insufficient_data = FALSE;
     258                 : 
     259               0 :   return TRUE;
     260                 : }
     261                 : 
     262                 : 
     263                 : /*
     264                 :  * Huffman MCU decoding.
     265                 :  * Each of these routines decodes and returns one MCU's worth of
     266                 :  * Huffman-compressed coefficients. 
     267                 :  * The coefficients are reordered from zigzag order into natural array order,
     268                 :  * but are not dequantized.
     269                 :  *
     270                 :  * The i'th block of the MCU is stored into the block pointed to by
     271                 :  * MCU_data[i].  WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
     272                 :  *
     273                 :  * We return FALSE if data source requested suspension.  In that case no
     274                 :  * changes have been made to permanent state.  (Exception: some output
     275                 :  * coefficients may already have been assigned.  This is harmless for
     276                 :  * spectral selection, since we'll just re-assign them on the next call.
     277                 :  * Successive approximation AC refinement has to be more careful, however.)
     278                 :  */
     279                 : 
     280                 : /*
     281                 :  * MCU decoding for DC initial scan (either spectral selection,
     282                 :  * or first pass of successive approximation).
     283                 :  */
     284                 : 
     285                 : METHODDEF(boolean)
     286               0 : decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
     287                 : {   
     288               0 :   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
     289               0 :   int Al = cinfo->Al;
     290                 :   register int s, r;
     291                 :   int blkn, ci;
     292                 :   JBLOCKROW block;
     293                 :   BITREAD_STATE_VARS;
     294                 :   savable_state state;
     295                 :   d_derived_tbl * tbl;
     296                 :   jpeg_component_info * compptr;
     297                 : 
     298                 :   /* Process restart marker if needed; may have to suspend */
     299               0 :   if (cinfo->restart_interval) {
     300               0 :     if (entropy->restarts_to_go == 0)
     301               0 :       if (! process_restart(cinfo))
     302               0 :         return FALSE;
     303                 :   }
     304                 : 
     305                 :   /* If we've run out of data, just leave the MCU set to zeroes.
     306                 :    * This way, we return uniform gray for the remainder of the segment.
     307                 :    */
     308               0 :   if (! entropy->pub.insufficient_data) {
     309                 : 
     310                 :     /* Load up working state */
     311               0 :     BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
     312               0 :     ASSIGN_STATE(state, entropy->saved);
     313                 : 
     314                 :     /* Outer loop handles each block in the MCU */
     315                 : 
     316               0 :     for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
     317               0 :       block = MCU_data[blkn];
     318               0 :       ci = cinfo->MCU_membership[blkn];
     319               0 :       compptr = cinfo->cur_comp_info[ci];
     320               0 :       tbl = entropy->derived_tbls[compptr->dc_tbl_no];
     321                 : 
     322                 :       /* Decode a single block's worth of coefficients */
     323                 : 
     324                 :       /* Section F.2.2.1: decode the DC coefficient difference */
     325               0 :       HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
     326               0 :       if (s) {
     327               0 :         CHECK_BIT_BUFFER(br_state, s, return FALSE);
     328               0 :         r = GET_BITS(s);
     329               0 :         s = HUFF_EXTEND(r, s);
     330                 :       }
     331                 : 
     332                 :       /* Convert DC difference to actual value, update last_dc_val */
     333               0 :       s += state.last_dc_val[ci];
     334               0 :       state.last_dc_val[ci] = s;
     335                 :       /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
     336               0 :       (*block)[0] = (JCOEF) (s << Al);
     337                 :     }
     338                 : 
     339                 :     /* Completed MCU, so update state */
     340               0 :     BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
     341               0 :     ASSIGN_STATE(entropy->saved, state);
     342                 :   }
     343                 : 
     344                 :   /* Account for restart interval (no-op if not using restarts) */
     345               0 :   entropy->restarts_to_go--;
     346                 : 
     347               0 :   return TRUE;
     348                 : }
     349                 : 
     350                 : 
     351                 : /*
     352                 :  * MCU decoding for AC initial scan (either spectral selection,
     353                 :  * or first pass of successive approximation).
     354                 :  */
     355                 : 
     356                 : METHODDEF(boolean)
     357               0 : decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
     358                 : {   
     359               0 :   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
     360               0 :   int Se = cinfo->Se;
     361               0 :   int Al = cinfo->Al;
     362                 :   register int s, k, r;
     363                 :   unsigned int EOBRUN;
     364                 :   JBLOCKROW block;
     365                 :   BITREAD_STATE_VARS;
     366                 :   d_derived_tbl * tbl;
     367                 : 
     368                 :   /* Process restart marker if needed; may have to suspend */
     369               0 :   if (cinfo->restart_interval) {
     370               0 :     if (entropy->restarts_to_go == 0)
     371               0 :       if (! process_restart(cinfo))
     372               0 :         return FALSE;
     373                 :   }
     374                 : 
     375                 :   /* If we've run out of data, just leave the MCU set to zeroes.
     376                 :    * This way, we return uniform gray for the remainder of the segment.
     377                 :    */
     378               0 :   if (! entropy->pub.insufficient_data) {
     379                 : 
     380                 :     /* Load up working state.
     381                 :      * We can avoid loading/saving bitread state if in an EOB run.
     382                 :      */
     383               0 :     EOBRUN = entropy->saved.EOBRUN;  /* only part of saved state we need */
     384                 : 
     385                 :     /* There is always only one block per MCU */
     386                 : 
     387               0 :     if (EOBRUN > 0)          /* if it's a band of zeroes... */
     388               0 :       EOBRUN--;                 /* ...process it now (we do nothing) */
     389                 :     else {
     390               0 :       BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
     391               0 :       block = MCU_data[0];
     392               0 :       tbl = entropy->ac_derived_tbl;
     393                 : 
     394               0 :       for (k = cinfo->Ss; k <= Se; k++) {
     395               0 :         HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
     396               0 :         r = s >> 4;
     397               0 :         s &= 15;
     398               0 :         if (s) {
     399               0 :           k += r;
     400               0 :           CHECK_BIT_BUFFER(br_state, s, return FALSE);
     401               0 :           r = GET_BITS(s);
     402               0 :           s = HUFF_EXTEND(r, s);
     403                 :           /* Scale and output coefficient in natural (dezigzagged) order */
     404               0 :           (*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al);
     405                 :         } else {
     406               0 :           if (r == 15) {        /* ZRL */
     407               0 :             k += 15;            /* skip 15 zeroes in band */
     408                 :           } else {              /* EOBr, run length is 2^r + appended bits */
     409               0 :             EOBRUN = 1 << r;
     410               0 :             if (r) {            /* EOBr, r > 0 */
     411               0 :               CHECK_BIT_BUFFER(br_state, r, return FALSE);
     412               0 :               r = GET_BITS(r);
     413               0 :               EOBRUN += r;
     414                 :             }
     415               0 :             EOBRUN--;           /* this band is processed at this moment */
     416               0 :             break;              /* force end-of-band */
     417                 :           }
     418                 :         }
     419                 :       }
     420                 : 
     421               0 :       BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
     422                 :     }
     423                 : 
     424                 :     /* Completed MCU, so update state */
     425               0 :     entropy->saved.EOBRUN = EOBRUN;  /* only part of saved state we need */
     426                 :   }
     427                 : 
     428                 :   /* Account for restart interval (no-op if not using restarts) */
     429               0 :   entropy->restarts_to_go--;
     430                 : 
     431               0 :   return TRUE;
     432                 : }
     433                 : 
     434                 : 
     435                 : /*
     436                 :  * MCU decoding for DC successive approximation refinement scan.
     437                 :  * Note: we assume such scans can be multi-component, although the spec
     438                 :  * is not very clear on the point.
     439                 :  */
     440                 : 
     441                 : METHODDEF(boolean)
     442               0 : decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
     443                 : {   
     444               0 :   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
     445               0 :   int p1 = 1 << cinfo->Al;     /* 1 in the bit position being coded */
     446                 :   int blkn;
     447                 :   JBLOCKROW block;
     448                 :   BITREAD_STATE_VARS;
     449                 : 
     450                 :   /* Process restart marker if needed; may have to suspend */
     451               0 :   if (cinfo->restart_interval) {
     452               0 :     if (entropy->restarts_to_go == 0)
     453               0 :       if (! process_restart(cinfo))
     454               0 :         return FALSE;
     455                 :   }
     456                 : 
     457                 :   /* Not worth the cycles to check insufficient_data here,
     458                 :    * since we will not change the data anyway if we read zeroes.
     459                 :    */
     460                 : 
     461                 :   /* Load up working state */
     462               0 :   BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
     463                 : 
     464                 :   /* Outer loop handles each block in the MCU */
     465                 : 
     466               0 :   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
     467               0 :     block = MCU_data[blkn];
     468                 : 
     469                 :     /* Encoded data is simply the next bit of the two's-complement DC value */
     470               0 :     CHECK_BIT_BUFFER(br_state, 1, return FALSE);
     471               0 :     if (GET_BITS(1))
     472               0 :       (*block)[0] |= p1;
     473                 :     /* Note: since we use |=, repeating the assignment later is safe */
     474                 :   }
     475                 : 
     476                 :   /* Completed MCU, so update state */
     477               0 :   BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
     478                 : 
     479                 :   /* Account for restart interval (no-op if not using restarts) */
     480               0 :   entropy->restarts_to_go--;
     481                 : 
     482               0 :   return TRUE;
     483                 : }
     484                 : 
     485                 : 
     486                 : /*
     487                 :  * MCU decoding for AC successive approximation refinement scan.
     488                 :  */
     489                 : 
     490                 : METHODDEF(boolean)
     491               0 : decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
     492                 : {   
     493               0 :   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
     494               0 :   int Se = cinfo->Se;
     495               0 :   int p1 = 1 << cinfo->Al;     /* 1 in the bit position being coded */
     496               0 :   int m1 = (-1) << cinfo->Al;  /* -1 in the bit position being coded */
     497                 :   register int s, k, r;
     498                 :   unsigned int EOBRUN;
     499                 :   JBLOCKROW block;
     500                 :   JCOEFPTR thiscoef;
     501                 :   BITREAD_STATE_VARS;
     502                 :   d_derived_tbl * tbl;
     503                 :   int num_newnz;
     504                 :   int newnz_pos[DCTSIZE2];
     505                 : 
     506                 :   /* Process restart marker if needed; may have to suspend */
     507               0 :   if (cinfo->restart_interval) {
     508               0 :     if (entropy->restarts_to_go == 0)
     509               0 :       if (! process_restart(cinfo))
     510               0 :         return FALSE;
     511                 :   }
     512                 : 
     513                 :   /* If we've run out of data, don't modify the MCU.
     514                 :    */
     515               0 :   if (! entropy->pub.insufficient_data) {
     516                 : 
     517                 :     /* Load up working state */
     518               0 :     BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
     519               0 :     EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
     520                 : 
     521                 :     /* There is always only one block per MCU */
     522               0 :     block = MCU_data[0];
     523               0 :     tbl = entropy->ac_derived_tbl;
     524                 : 
     525                 :     /* If we are forced to suspend, we must undo the assignments to any newly
     526                 :      * nonzero coefficients in the block, because otherwise we'd get confused
     527                 :      * next time about which coefficients were already nonzero.
     528                 :      * But we need not undo addition of bits to already-nonzero coefficients;
     529                 :      * instead, we can test the current bit to see if we already did it.
     530                 :      */
     531               0 :     num_newnz = 0;
     532                 : 
     533                 :     /* initialize coefficient loop counter to start of band */
     534               0 :     k = cinfo->Ss;
     535                 : 
     536               0 :     if (EOBRUN == 0) {
     537               0 :       for (; k <= Se; k++) {
     538               0 :         HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
     539               0 :         r = s >> 4;
     540               0 :         s &= 15;
     541               0 :         if (s) {
     542               0 :           if (s != 1)           /* size of new coef should always be 1 */
     543               0 :             WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
     544               0 :           CHECK_BIT_BUFFER(br_state, 1, goto undoit);
     545               0 :           if (GET_BITS(1))
     546               0 :             s = p1;             /* newly nonzero coef is positive */
     547                 :           else
     548               0 :             s = m1;             /* newly nonzero coef is negative */
     549                 :         } else {
     550               0 :           if (r != 15) {
     551               0 :             EOBRUN = 1 << r;      /* EOBr, run length is 2^r + appended bits */
     552               0 :             if (r) {
     553               0 :               CHECK_BIT_BUFFER(br_state, r, goto undoit);
     554               0 :               r = GET_BITS(r);
     555               0 :               EOBRUN += r;
     556                 :             }
     557               0 :             break;              /* rest of block is handled by EOB logic */
     558                 :           }
     559                 :           /* note s = 0 for processing ZRL */
     560                 :         }
     561                 :         /* Advance over already-nonzero coefs and r still-zero coefs,
     562                 :          * appending correction bits to the nonzeroes.  A correction bit is 1
     563                 :          * if the absolute value of the coefficient must be increased.
     564                 :          */
     565                 :         do {
     566               0 :           thiscoef = *block + jpeg_natural_order[k];
     567               0 :           if (*thiscoef != 0) {
     568               0 :             CHECK_BIT_BUFFER(br_state, 1, goto undoit);
     569               0 :             if (GET_BITS(1)) {
     570               0 :               if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
     571               0 :                 if (*thiscoef >= 0)
     572               0 :                   *thiscoef += p1;
     573                 :                 else
     574               0 :                   *thiscoef += m1;
     575                 :               }
     576                 :             }
     577                 :           } else {
     578               0 :             if (--r < 0)
     579               0 :               break;            /* reached target zero coefficient */
     580                 :           }
     581               0 :           k++;
     582               0 :         } while (k <= Se);
     583               0 :         if (s) {
     584               0 :           int pos = jpeg_natural_order[k];
     585                 :           /* Output newly nonzero coefficient */
     586               0 :           (*block)[pos] = (JCOEF) s;
     587                 :           /* Remember its position in case we have to suspend */
     588               0 :           newnz_pos[num_newnz++] = pos;
     589                 :         }
     590                 :       }
     591                 :     }
     592                 : 
     593               0 :     if (EOBRUN > 0) {
     594                 :       /* Scan any remaining coefficient positions after the end-of-band
     595                 :        * (the last newly nonzero coefficient, if any).  Append a correction
     596                 :        * bit to each already-nonzero coefficient.  A correction bit is 1
     597                 :        * if the absolute value of the coefficient must be increased.
     598                 :        */
     599               0 :       for (; k <= Se; k++) {
     600               0 :         thiscoef = *block + jpeg_natural_order[k];
     601               0 :         if (*thiscoef != 0) {
     602               0 :           CHECK_BIT_BUFFER(br_state, 1, goto undoit);
     603               0 :           if (GET_BITS(1)) {
     604               0 :             if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
     605               0 :               if (*thiscoef >= 0)
     606               0 :                 *thiscoef += p1;
     607                 :               else
     608               0 :                 *thiscoef += m1;
     609                 :             }
     610                 :           }
     611                 :         }
     612                 :       }
     613                 :       /* Count one block completed in EOB run */
     614               0 :       EOBRUN--;
     615                 :     }
     616                 : 
     617                 :     /* Completed MCU, so update state */
     618               0 :     BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
     619               0 :     entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
     620                 :   }
     621                 : 
     622                 :   /* Account for restart interval (no-op if not using restarts) */
     623               0 :   entropy->restarts_to_go--;
     624                 : 
     625               0 :   return TRUE;
     626                 : 
     627                 : undoit:
     628                 :   /* Re-zero any output coefficients that we made newly nonzero */
     629               0 :   while (num_newnz > 0)
     630               0 :     (*block)[newnz_pos[--num_newnz]] = 0;
     631                 : 
     632               0 :   return FALSE;
     633                 : }
     634                 : 
     635                 : 
     636                 : /*
     637                 :  * Module initialization routine for progressive Huffman entropy decoding.
     638                 :  */
     639                 : 
     640                 : GLOBAL(void)
     641               0 : jinit_phuff_decoder (j_decompress_ptr cinfo)
     642                 : {
     643                 :   phuff_entropy_ptr entropy;
     644                 :   int *coef_bit_ptr;
     645                 :   int ci, i;
     646                 : 
     647               0 :   entropy = (phuff_entropy_ptr)
     648               0 :     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
     649                 :                                 SIZEOF(phuff_entropy_decoder));
     650               0 :   cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
     651               0 :   entropy->pub.start_pass = start_pass_phuff_decoder;
     652                 : 
     653                 :   /* Mark derived tables unallocated */
     654               0 :   for (i = 0; i < NUM_HUFF_TBLS; i++) {
     655               0 :     entropy->derived_tbls[i] = NULL;
     656                 :   }
     657                 : 
     658                 :   /* Create progression status table */
     659               0 :   cinfo->coef_bits = (int (*)[DCTSIZE2])
     660               0 :     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
     661               0 :                                 cinfo->num_components*DCTSIZE2*SIZEOF(int));
     662               0 :   coef_bit_ptr = & cinfo->coef_bits[0][0];
     663               0 :   for (ci = 0; ci < cinfo->num_components; ci++) 
     664               0 :     for (i = 0; i < DCTSIZE2; i++)
     665               0 :       *coef_bit_ptr++ = -1;
     666               0 : }
     667                 : 
     668                 : #endif /* D_PROGRESSIVE_SUPPORTED */

Generated by: LCOV version 1.7