lzw.c File Reference

LZW decoding routines. More...

#include "avcodec.h"
#include "lzw.h"

Go to the source code of this file.

Data Structures

struct  LZWState

Defines

#define LZW_MAXBITS   12
#define LZW_SIZTABLE   (1<<LZW_MAXBITS)

Functions

static int lzw_get_code (struct LZWState *s)
const uint8_t * ff_lzw_cur_ptr (LZWState *p)
void ff_lzw_decode_tail (LZWState *p)
av_cold void ff_lzw_decode_open (LZWState **p)
av_cold void ff_lzw_decode_close (LZWState **p)
int ff_lzw_decode_init (LZWState *p, int csize, const uint8_t *buf, int buf_size, int mode)
int ff_lzw_decode (LZWState *p, uint8_t *buf, int len)

Variables

static const uint16_t mask [17]


Detailed Description

LZW decoding routines.

Author:
Fabrice Bellard Modified for use in TIFF by Konstantin Shishkov

Definition in file lzw.c.


Define Documentation

#define LZW_MAXBITS   12

Definition at line 33 of file lzw.c.

Referenced by ff_lzw_decode(), and ff_lzw_decode_init().

#define LZW_SIZTABLE   (1<<LZW_MAXBITS)

Definition at line 34 of file lzw.c.


Function Documentation

const uint8_t* ff_lzw_cur_ptr ( LZWState p  ) 

Definition at line 94 of file lzw.c.

Referenced by gif_read_image().

00095 {
00096     return ((struct LZWState*)p)->pbuf;
00097 }

int ff_lzw_decode ( LZWState p,
uint8_t *  buf,
int  len 
)

Decode given number of bytes NOTE: the algorithm here is inspired from the LZW GIF decoder written by Steven A. Bennett in 1987.

Parameters:
s LZW context
buf output buffer
len number of bytes to decode
Returns:
number of bytes decoded

Definition at line 169 of file lzw.c.

References c, LZWState::clear_code, LZWState::codesize, LZWState::curmask, LZWState::cursize, LZWState::end_code, LZWState::extra_slot, LZWState::fc, lzw_get_code(), LZW_MAXBITS, mask, LZWState::newcodes, LZWState::oc, LZWState::prefix, LZWState::slot, sp, LZWState::sp, LZWState::stack, LZWState::suffix, and LZWState::top_slot.

Referenced by gif_read_image().

00169                                                      {
00170     int l, c, code, oc, fc;
00171     uint8_t *sp;
00172     struct LZWState *s = (struct LZWState *)p;
00173 
00174     if (s->end_code < 0)
00175         return 0;
00176 
00177     l = len;
00178     sp = s->sp;
00179     oc = s->oc;
00180     fc = s->fc;
00181 
00182     for (;;) {
00183         while (sp > s->stack) {
00184             *buf++ = *(--sp);
00185             if ((--l) == 0)
00186                 goto the_end;
00187         }
00188         c = lzw_get_code(s);
00189         if (c == s->end_code) {
00190             break;
00191         } else if (c == s->clear_code) {
00192             s->cursize = s->codesize + 1;
00193             s->curmask = mask[s->cursize];
00194             s->slot = s->newcodes;
00195             s->top_slot = 1 << s->cursize;
00196             fc= oc= -1;
00197         } else {
00198             code = c;
00199             if (code == s->slot && fc>=0) {
00200                 *sp++ = fc;
00201                 code = oc;
00202             }else if(code >= s->slot)
00203                 break;
00204             while (code >= s->newcodes) {
00205                 *sp++ = s->suffix[code];
00206                 code = s->prefix[code];
00207             }
00208             *sp++ = code;
00209             if (s->slot < s->top_slot && oc>=0) {
00210                 s->suffix[s->slot] = code;
00211                 s->prefix[s->slot++] = oc;
00212             }
00213             fc = code;
00214             oc = c;
00215             if (s->slot >= s->top_slot - s->extra_slot) {
00216                 if (s->cursize < LZW_MAXBITS) {
00217                     s->top_slot <<= 1;
00218                     s->curmask = mask[++s->cursize];
00219                 }
00220             }
00221         }
00222     }
00223     s->end_code = -1;
00224   the_end:
00225     s->sp = sp;
00226     s->oc = oc;
00227     s->fc = fc;
00228     return len - l;
00229 }

av_cold void ff_lzw_decode_close ( LZWState **  p  ) 

Definition at line 117 of file lzw.c.

References av_freep().

Referenced by gif_decode_close(), and tiff_end().

00118 {
00119     av_freep(p);
00120 }

int ff_lzw_decode_init ( LZWState p,
int  csize,
const uint8_t *  buf,
int  buf_size,
int  mode 
)

Initialize LZW decoder

Parameters:
s LZW context
csize initial code size in bits
buf input data
buf_size input data size
mode decoder working mode - either GIF or TIFF

Definition at line 130 of file lzw.c.

References LZWState::bbits, LZWState::bbuf, LZWState::bs, LZWState::clear_code, LZWState::codesize, LZWState::curmask, LZWState::cursize, LZWState::ebuf, LZWState::end_code, LZWState::extra_slot, LZWState::fc, FF_LZW_TIFF, LZW_MAXBITS, mask, LZWState::mode, LZWState::newcodes, LZWState::oc, LZWState::pbuf, LZWState::slot, LZWState::sp, LZWState::stack, and LZWState::top_slot.

Referenced by gif_read_image(), and tiff_unpack_strip().

00131 {
00132     struct LZWState *s = (struct LZWState *)p;
00133 
00134     if(csize < 1 || csize >= LZW_MAXBITS)
00135         return -1;
00136     /* read buffer */
00137     s->pbuf = buf;
00138     s->ebuf = s->pbuf + buf_size;
00139     s->bbuf = 0;
00140     s->bbits = 0;
00141     s->bs = 0;
00142 
00143     /* decoder */
00144     s->codesize = csize;
00145     s->cursize = s->codesize + 1;
00146     s->curmask = mask[s->cursize];
00147     s->top_slot = 1 << s->cursize;
00148     s->clear_code = 1 << s->codesize;
00149     s->end_code = s->clear_code + 1;
00150     s->slot = s->newcodes = s->clear_code + 2;
00151     s->oc = s->fc = -1;
00152     s->sp = s->stack;
00153 
00154     s->mode = mode;
00155     s->extra_slot = s->mode == FF_LZW_TIFF;
00156     return 0;
00157 }

av_cold void ff_lzw_decode_open ( LZWState **  p  ) 

Definition at line 112 of file lzw.c.

References av_mallocz().

Referenced by gif_decode_init(), and tiff_init().

00113 {
00114     *p = av_mallocz(sizeof(struct LZWState));
00115 }

void ff_lzw_decode_tail ( LZWState p  ) 

Definition at line 99 of file lzw.c.

References LZWState::bs, LZWState::ebuf, FF_LZW_GIF, LZWState::mode, and LZWState::pbuf.

Referenced by gif_read_image().

00100 {
00101     struct LZWState *s = (struct LZWState *)p;
00102 
00103     if(s->mode == FF_LZW_GIF) {
00104         while(s->pbuf < s->ebuf && s->bs>0){
00105             s->pbuf += s->bs;
00106             s->bs = *s->pbuf++;
00107         }
00108     }else
00109         s->pbuf= s->ebuf;
00110 }

static int lzw_get_code ( struct LZWState s  )  [static]

Definition at line 68 of file lzw.c.

References LZWState::bbits, LZWState::bbuf, LZWState::bs, c, LZWState::curmask, LZWState::cursize, FF_LZW_GIF, LZWState::mode, and LZWState::pbuf.

Referenced by ff_lzw_decode().

00069 {
00070     int c;
00071 
00072     if(s->mode == FF_LZW_GIF) {
00073         while (s->bbits < s->cursize) {
00074             if (!s->bs) {
00075                 s->bs = *s->pbuf++;
00076             }
00077             s->bbuf |= (*s->pbuf++) << s->bbits;
00078             s->bbits += 8;
00079             s->bs--;
00080         }
00081         c = s->bbuf;
00082         s->bbuf >>= s->cursize;
00083     } else { // TIFF
00084         while (s->bbits < s->cursize) {
00085             s->bbuf = (s->bbuf << 8) | (*s->pbuf++);
00086             s->bbits += 8;
00087         }
00088         c = s->bbuf >> (s->bbits - s->cursize);
00089     }
00090     s->bbits -= s->cursize;
00091     return c & s->curmask;
00092 }


Variable Documentation

const uint16_t mask[17] [static]

Initial value:

{
    0x0000, 0x0001, 0x0003, 0x0007,
    0x000F, 0x001F, 0x003F, 0x007F,
    0x00FF, 0x01FF, 0x03FF, 0x07FF,
    0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
}

Definition at line 36 of file lzw.c.

Referenced by ac3_parametric_bit_allocation(), avg_pixels16_l2_altivec(), bit_alloc(), bit_alloc_masking(), bitcopy_n(), check_input_motion(), cinepak_decode_codebook(), cinepak_decode_vectors(), cmp(), compute_bit_allocation(), decode_13(), decode_residual(), decode_residual_block(), diff_pixels_mvi(), escape124_decode_frame(), ff_acelp_fc_pulse_per_track(), ff_er_add_slice(), ff_get_mb_score(), ff_lsb2full(), ff_lzw_decode(), ff_lzw_decode_init(), filter_channel(), get_cabac_bypass_sign(), get_field(), h264_deblock_mask(), inner_add_yblock_a_bw_16_obmc_32_altivec(), inner_add_yblock_a_bw_8_obmc_16_altivec(), ljpeg_decode_rgb_scan(), mpc8_decode_frame(), mpc8_get_mask(), mszh_decomp(), png_get_interlaced_row(), png_put_interlaced_row(), pp_get_mode_by_name_and_quality(), put_field(), put_pixels16_l2_altivec(), put_swf_line_edge(), put_swf_rect(), rematrix_channels(), rv34_decode_cbp(), rv34_pred_mv_b(), ulti_pattern(), unaligned_load(), vector_fmul_add_add_altivec(), vertClassify_altivec(), vp56_mc(), and x8_get_ac_rlf().


Generated on Thu Nov 20 07:45:33 2008 for libextractor by  doxygen 1.5.1