| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * LZW decoder | ||
| 3 | * Copyright (c) 2003 Fabrice Bellard | ||
| 4 | * Copyright (c) 2006 Konstantin Shishkov | ||
| 5 | * | ||
| 6 | * This file is part of FFmpeg. | ||
| 7 | * | ||
| 8 | * FFmpeg is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with FFmpeg; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | /** | ||
| 24 | * @file | ||
| 25 | * @brief LZW decoding routines | ||
| 26 | * @author Fabrice Bellard | ||
| 27 | * @author modified for use in TIFF by Konstantin Shishkov | ||
| 28 | */ | ||
| 29 | |||
| 30 | #include "libavutil/attributes.h" | ||
| 31 | #include "bytestream.h" | ||
| 32 | #include "lzw.h" | ||
| 33 | #include "libavutil/mem.h" | ||
| 34 | |||
| 35 | #define LZW_MAXBITS 12 | ||
| 36 | #define LZW_SIZTABLE (1<<LZW_MAXBITS) | ||
| 37 | |||
| 38 | static const uint16_t mask[17] = | ||
| 39 | { | ||
| 40 | 0x0000, 0x0001, 0x0003, 0x0007, | ||
| 41 | 0x000F, 0x001F, 0x003F, 0x007F, | ||
| 42 | 0x00FF, 0x01FF, 0x03FF, 0x07FF, | ||
| 43 | 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF | ||
| 44 | }; | ||
| 45 | |||
| 46 | struct LZWState { | ||
| 47 | GetByteContext gb; | ||
| 48 | int bbits; | ||
| 49 | unsigned int bbuf; | ||
| 50 | |||
| 51 | int mode; ///< Decoder mode | ||
| 52 | int cursize; ///< The current code size | ||
| 53 | int curmask; | ||
| 54 | int codesize; | ||
| 55 | int clear_code; | ||
| 56 | int end_code; | ||
| 57 | int newcodes; ///< First available code | ||
| 58 | int top_slot; ///< Highest code for current size | ||
| 59 | int extra_slot; | ||
| 60 | int slot; ///< Last read code | ||
| 61 | int fc, oc; | ||
| 62 | uint8_t *sp; | ||
| 63 | uint8_t stack[LZW_SIZTABLE]; | ||
| 64 | uint8_t suffix[LZW_SIZTABLE]; | ||
| 65 | uint16_t prefix[LZW_SIZTABLE]; | ||
| 66 | int bs; ///< current buffer size for GIF | ||
| 67 | }; | ||
| 68 | |||
| 69 | /* get one code from stream */ | ||
| 70 | 2165926 | static int lzw_get_code(struct LZWState * s) | |
| 71 | { | ||
| 72 | int c; | ||
| 73 | |||
| 74 |
3/4✓ Branch 0 taken 2165567 times.
✓ Branch 1 taken 359 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2165567 times.
|
2165926 | if (s->bbits < s->cursize && bytestream2_get_bytes_left(&s->gb) <= 0) |
| 75 | ✗ | return s->end_code; | |
| 76 | |||
| 77 |
2/2✓ Branch 0 taken 2165543 times.
✓ Branch 1 taken 383 times.
|
2165926 | if(s->mode == FF_LZW_GIF) { |
| 78 |
2/2✓ Branch 0 taken 2943050 times.
✓ Branch 1 taken 2165543 times.
|
5108593 | while (s->bbits < s->cursize) { |
| 79 |
2/2✓ Branch 0 taken 12583 times.
✓ Branch 1 taken 2930467 times.
|
2943050 | if (!s->bs) { |
| 80 | 12583 | s->bs = bytestream2_get_byte(&s->gb); | |
| 81 | } | ||
| 82 | 2943050 | s->bbuf |= bytestream2_get_byte(&s->gb) << s->bbits; | |
| 83 | 2943050 | s->bbits += 8; | |
| 84 | 2943050 | s->bs--; | |
| 85 | } | ||
| 86 | 2165543 | c = s->bbuf; | |
| 87 | 2165543 | s->bbuf >>= s->cursize; | |
| 88 | } else { // TIFF | ||
| 89 |
2/2✓ Branch 0 taken 432 times.
✓ Branch 1 taken 383 times.
|
815 | while (s->bbits < s->cursize) { |
| 90 | 432 | s->bbuf = (s->bbuf << 8) | bytestream2_get_byte(&s->gb); | |
| 91 | 432 | s->bbits += 8; | |
| 92 | } | ||
| 93 | 383 | c = s->bbuf >> (s->bbits - s->cursize); | |
| 94 | } | ||
| 95 | 2165926 | s->bbits -= s->cursize; | |
| 96 | 2165926 | return c & s->curmask; | |
| 97 | } | ||
| 98 | |||
| 99 | 1294 | int ff_lzw_decode_tail(LZWState *p) | |
| 100 | { | ||
| 101 | 1294 | struct LZWState *s = (struct LZWState *)p; | |
| 102 | |||
| 103 |
1/2✓ Branch 0 taken 1294 times.
✗ Branch 1 not taken.
|
1294 | if(s->mode == FF_LZW_GIF) { |
| 104 |
3/4✓ Branch 0 taken 1294 times.
✓ Branch 1 taken 1294 times.
✓ Branch 3 taken 1294 times.
✗ Branch 4 not taken.
|
2588 | while (s->bs > 0 && bytestream2_get_bytes_left(&s->gb)) { |
| 105 | 1294 | bytestream2_skip(&s->gb, s->bs); | |
| 106 | 1294 | s->bs = bytestream2_get_byte(&s->gb); | |
| 107 | } | ||
| 108 | }else | ||
| 109 | ✗ | bytestream2_skip(&s->gb, bytestream2_get_bytes_left(&s->gb)); | |
| 110 | 1294 | return bytestream2_tell(&s->gb); | |
| 111 | } | ||
| 112 | |||
| 113 | 57 | av_cold void ff_lzw_decode_open(LZWState **p) | |
| 114 | { | ||
| 115 | 57 | *p = av_mallocz(sizeof(struct LZWState)); | |
| 116 | 57 | } | |
| 117 | |||
| 118 | 57 | av_cold void ff_lzw_decode_close(LZWState **p) | |
| 119 | { | ||
| 120 | 57 | av_freep(p); | |
| 121 | 57 | } | |
| 122 | |||
| 123 | /** | ||
| 124 | * Initialize LZW decoder | ||
| 125 | * @param p LZW context | ||
| 126 | * @param csize initial code size in bits | ||
| 127 | * @param buf input data | ||
| 128 | * @param buf_size input data size | ||
| 129 | * @param mode decoder working mode - either GIF or TIFF | ||
| 130 | */ | ||
| 131 | 1296 | int ff_lzw_decode_init(LZWState *p, int csize, const uint8_t *buf, int buf_size, int mode) | |
| 132 | { | ||
| 133 | 1296 | struct LZWState *s = (struct LZWState *)p; | |
| 134 | |||
| 135 |
2/4✓ Branch 0 taken 1296 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1296 times.
|
1296 | if(csize < 1 || csize >= LZW_MAXBITS) |
| 136 | ✗ | return -1; | |
| 137 | /* read buffer */ | ||
| 138 | 1296 | bytestream2_init(&s->gb, buf, buf_size); | |
| 139 | 1296 | s->bbuf = 0; | |
| 140 | 1296 | s->bbits = 0; | |
| 141 | 1296 | s->bs = 0; | |
| 142 | |||
| 143 | /* decoder */ | ||
| 144 | 1296 | s->codesize = csize; | |
| 145 | 1296 | s->cursize = s->codesize + 1; | |
| 146 | 1296 | s->curmask = mask[s->cursize]; | |
| 147 | 1296 | s->top_slot = 1 << s->cursize; | |
| 148 | 1296 | s->clear_code = 1 << s->codesize; | |
| 149 | 1296 | s->end_code = s->clear_code + 1; | |
| 150 | 1296 | s->slot = s->newcodes = s->clear_code + 2; | |
| 151 | 1296 | s->oc = s->fc = -1; | |
| 152 | 1296 | s->sp = s->stack; | |
| 153 | |||
| 154 | 1296 | s->mode = mode; | |
| 155 | 1296 | s->extra_slot = s->mode == FF_LZW_TIFF; | |
| 156 | 1296 | return 0; | |
| 157 | } | ||
| 158 | |||
| 159 | /** | ||
| 160 | * Decode given number of bytes | ||
| 161 | * NOTE: the algorithm here is inspired from the LZW GIF decoder | ||
| 162 | * written by Steven A. Bennett in 1987. | ||
| 163 | * | ||
| 164 | * @param p LZW context | ||
| 165 | * @param buf output buffer | ||
| 166 | * @param len number of bytes to decode | ||
| 167 | * @return number of bytes decoded | ||
| 168 | */ | ||
| 169 | 37989 | int ff_lzw_decode(LZWState *p, uint8_t *buf, int len){ | |
| 170 | int l, c, code, oc, fc; | ||
| 171 | uint8_t *sp; | ||
| 172 | 37989 | struct LZWState *s = (struct LZWState *)p; | |
| 173 | |||
| 174 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37989 times.
|
37989 | if (s->end_code < 0) |
| 175 | ✗ | return 0; | |
| 176 | |||
| 177 | 37989 | l = len; | |
| 178 | 37989 | sp = s->sp; | |
| 179 | 37989 | oc = s->oc; | |
| 180 | 37989 | fc = s->fc; | |
| 181 | |||
| 182 | for (;;) { | ||
| 183 |
2/2✓ Branch 0 taken 5330391 times.
✓ Branch 1 taken 2165926 times.
|
7496317 | while (sp > s->stack) { |
| 184 | 5330391 | *buf++ = *(--sp); | |
| 185 |
2/2✓ Branch 0 taken 37989 times.
✓ Branch 1 taken 5292402 times.
|
5330391 | if ((--l) == 0) |
| 186 | 37989 | goto the_end; | |
| 187 | } | ||
| 188 | 2165926 | c = lzw_get_code(s); | |
| 189 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2165926 times.
|
2165926 | if (c == s->end_code) { |
| 190 | ✗ | break; | |
| 191 |
2/2✓ Branch 0 taken 3062 times.
✓ Branch 1 taken 2162864 times.
|
2165926 | } else if (c == s->clear_code) { |
| 192 | 3062 | s->cursize = s->codesize + 1; | |
| 193 | 3062 | s->curmask = mask[s->cursize]; | |
| 194 | 3062 | s->slot = s->newcodes; | |
| 195 | 3062 | s->top_slot = 1 << s->cursize; | |
| 196 | 3062 | fc= oc= -1; | |
| 197 | } else { | ||
| 198 | 2162864 | code = c; | |
| 199 |
3/4✓ Branch 0 taken 30234 times.
✓ Branch 1 taken 2132630 times.
✓ Branch 2 taken 30234 times.
✗ Branch 3 not taken.
|
2162864 | if (code == s->slot && fc>=0) { |
| 200 | 30234 | *sp++ = fc; | |
| 201 | 30234 | code = oc; | |
| 202 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2132630 times.
|
2132630 | }else if(code >= s->slot) |
| 203 | ✗ | break; | |
| 204 |
2/2✓ Branch 0 taken 3137293 times.
✓ Branch 1 taken 2162864 times.
|
5300157 | while (code >= s->newcodes) { |
| 205 | 3137293 | *sp++ = s->suffix[code]; | |
| 206 | 3137293 | code = s->prefix[code]; | |
| 207 | } | ||
| 208 | 2162864 | *sp++ = code; | |
| 209 |
3/4✓ Branch 0 taken 2162864 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2159802 times.
✓ Branch 3 taken 3062 times.
|
2162864 | if (s->slot < s->top_slot && oc>=0) { |
| 210 | 2159802 | s->suffix[s->slot] = code; | |
| 211 | 2159802 | s->prefix[s->slot++] = oc; | |
| 212 | } | ||
| 213 | 2162864 | fc = code; | |
| 214 | 2162864 | oc = c; | |
| 215 |
2/2✓ Branch 0 taken 1563 times.
✓ Branch 1 taken 2161301 times.
|
2162864 | if (s->slot >= s->top_slot - s->extra_slot) { |
| 216 |
1/2✓ Branch 0 taken 1563 times.
✗ Branch 1 not taken.
|
1563 | if (s->cursize < LZW_MAXBITS) { |
| 217 | 1563 | s->top_slot <<= 1; | |
| 218 | 1563 | s->curmask = mask[++s->cursize]; | |
| 219 | } | ||
| 220 | } | ||
| 221 | } | ||
| 222 | } | ||
| 223 | ✗ | s->end_code = -1; | |
| 224 | 37989 | the_end: | |
| 225 | 37989 | s->sp = sp; | |
| 226 | 37989 | s->oc = oc; | |
| 227 | 37989 | s->fc = fc; | |
| 228 | 37989 | return len - l; | |
| 229 | } | ||
| 230 |