2007-04-07 18:10:22 +03:00
|
|
|
/*
|
|
|
|
* LZW encoder
|
|
|
|
* Copyright (c) 2007 Bartlomiej Wolowiec
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2011-07-14 04:46:19 +03:00
|
|
|
* LZW encoder
|
2007-04-07 18:10:22 +03:00
|
|
|
* @author Bartlomiej Wolowiec
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
|
|
|
#include "lzw.h"
|
2014-02-26 04:26:29 +03:00
|
|
|
#include "mathops.h"
|
|
|
|
#include "put_bits.h"
|
2007-04-07 18:10:22 +03:00
|
|
|
|
|
|
|
#define LZW_MAXBITS 12
|
|
|
|
#define LZW_SIZTABLE (1<<LZW_MAXBITS)
|
|
|
|
#define LZW_HASH_SIZE 16411
|
|
|
|
#define LZW_HASH_SHIFT 6
|
|
|
|
|
|
|
|
#define LZW_PREFIX_EMPTY -1
|
|
|
|
#define LZW_PREFIX_FREE -2
|
|
|
|
|
|
|
|
/** One code in hash table */
|
|
|
|
typedef struct Code{
|
|
|
|
/// Hash code of prefix, LZW_PREFIX_EMPTY if empty prefix, or LZW_PREFIX_FREE if no code
|
|
|
|
int hash_prefix;
|
|
|
|
int code; ///< LZW code
|
|
|
|
uint8_t suffix; ///< Last character in code block
|
|
|
|
}Code;
|
|
|
|
|
|
|
|
/** LZW encode state */
|
|
|
|
typedef struct LZWEncodeState {
|
|
|
|
int clear_code; ///< Value of clear code
|
|
|
|
int end_code; ///< Value of end code
|
|
|
|
Code tab[LZW_HASH_SIZE]; ///< Hash table
|
|
|
|
int tabsize; ///< Number of values in hash table
|
|
|
|
int bits; ///< Actual bits code
|
|
|
|
int bufsize; ///< Size of output buffer
|
|
|
|
PutBitContext pb; ///< Put bit context for output
|
|
|
|
int maxbits; ///< Max bits code
|
|
|
|
int maxcode; ///< Max value of code
|
|
|
|
int output_bytes; ///< Number of written bytes
|
|
|
|
int last_code; ///< Value of last output code or LZW_PREFIX_EMPTY
|
2009-12-03 21:17:39 +02:00
|
|
|
enum FF_LZW_MODES mode; ///< TIFF or GIF
|
2020-07-19 20:29:29 +02:00
|
|
|
int little_endian; ///< GIF is LE while TIFF is BE
|
2007-04-07 18:10:22 +03:00
|
|
|
}LZWEncodeState;
|
|
|
|
|
|
|
|
|
|
|
|
const int ff_lzw_encode_state_size = sizeof(LZWEncodeState);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hash function adding character
|
|
|
|
* @param head LZW code for prefix
|
|
|
|
* @param add Character to add
|
|
|
|
* @return New hash value
|
|
|
|
*/
|
|
|
|
static inline int hash(int head, const int add)
|
|
|
|
{
|
|
|
|
head ^= (add << LZW_HASH_SHIFT);
|
|
|
|
if (head >= LZW_HASH_SIZE)
|
|
|
|
head -= LZW_HASH_SIZE;
|
2012-10-18 22:42:04 +03:00
|
|
|
av_assert2(head >= 0 && head < LZW_HASH_SIZE);
|
2007-04-07 18:10:22 +03:00
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hash function calculates next hash value
|
|
|
|
* @param head Actual hash code
|
|
|
|
* @param offset Offset calculated by hashOffset
|
|
|
|
* @return New hash value
|
|
|
|
*/
|
|
|
|
static inline int hashNext(int head, const int offset)
|
|
|
|
{
|
|
|
|
head -= offset;
|
|
|
|
if(head < 0)
|
|
|
|
head += LZW_HASH_SIZE;
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hash function calculates hash offset
|
|
|
|
* @param head Actual hash code
|
|
|
|
* @return Hash offset
|
|
|
|
*/
|
|
|
|
static inline int hashOffset(const int head)
|
|
|
|
{
|
|
|
|
return head ? LZW_HASH_SIZE - head : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write one code to stream
|
|
|
|
* @param s LZW state
|
|
|
|
* @param c code to write
|
|
|
|
*/
|
|
|
|
static inline void writeCode(LZWEncodeState * s, int c)
|
|
|
|
{
|
2013-08-14 23:56:56 +03:00
|
|
|
av_assert2(0 <= c && c < 1 << s->bits);
|
2020-07-19 20:29:29 +02:00
|
|
|
if (s->little_endian)
|
|
|
|
put_bits_le(&s->pb, s->bits, c);
|
|
|
|
else
|
|
|
|
put_bits(&s->pb, s->bits, c);
|
2007-04-07 18:10:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find LZW code for block
|
|
|
|
* @param s LZW state
|
|
|
|
* @param c Last character in block
|
|
|
|
* @param hash_prefix LZW code for prefix
|
|
|
|
* @return LZW code for block or -1 if not found in table
|
|
|
|
*/
|
|
|
|
static inline int findCode(LZWEncodeState * s, uint8_t c, int hash_prefix)
|
|
|
|
{
|
|
|
|
int h = hash(FFMAX(hash_prefix, 0), c);
|
|
|
|
int hash_offset = hashOffset(h);
|
|
|
|
|
|
|
|
while (s->tab[h].hash_prefix != LZW_PREFIX_FREE) {
|
|
|
|
if ((s->tab[h].suffix == c)
|
|
|
|
&& (s->tab[h].hash_prefix == hash_prefix))
|
|
|
|
return h;
|
|
|
|
h = hashNext(h, hash_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add block to LZW code table
|
|
|
|
* @param s LZW state
|
|
|
|
* @param c Last character in block
|
|
|
|
* @param hash_prefix LZW code for prefix
|
|
|
|
* @param hash_code LZW code for bytes block
|
|
|
|
*/
|
|
|
|
static inline void addCode(LZWEncodeState * s, uint8_t c, int hash_prefix, int hash_code)
|
|
|
|
{
|
|
|
|
s->tab[hash_code].code = s->tabsize;
|
|
|
|
s->tab[hash_code].suffix = c;
|
|
|
|
s->tab[hash_code].hash_prefix = hash_prefix;
|
|
|
|
|
|
|
|
s->tabsize++;
|
|
|
|
|
2009-12-03 21:17:39 +02:00
|
|
|
if (s->tabsize >= (1 << s->bits) + (s->mode == FF_LZW_GIF))
|
2007-04-07 18:10:22 +03:00
|
|
|
s->bits++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear LZW code table
|
|
|
|
* @param s LZW state
|
|
|
|
*/
|
|
|
|
static void clearTable(LZWEncodeState * s)
|
|
|
|
{
|
|
|
|
int i, h;
|
|
|
|
|
|
|
|
writeCode(s, s->clear_code);
|
|
|
|
s->bits = 9;
|
|
|
|
for (i = 0; i < LZW_HASH_SIZE; i++) {
|
|
|
|
s->tab[i].hash_prefix = LZW_PREFIX_FREE;
|
|
|
|
}
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
h = hash(0, i);
|
|
|
|
s->tab[h].code = i;
|
|
|
|
s->tab[h].suffix = i;
|
|
|
|
s->tab[h].hash_prefix = LZW_PREFIX_EMPTY;
|
|
|
|
}
|
|
|
|
s->tabsize = 258;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate number of bytes written
|
|
|
|
* @param s LZW encode state
|
|
|
|
* @return Number of bytes written
|
|
|
|
*/
|
|
|
|
static int writtenBytes(LZWEncodeState *s){
|
2021-03-25 13:52:56 +02:00
|
|
|
int ret = put_bytes_count(&s->pb, 0);
|
2007-04-07 18:10:22 +03:00
|
|
|
ret -= s->output_bytes;
|
|
|
|
s->output_bytes += ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize LZW encoder. Please set s->clear_code, s->end_code and s->maxbits before run.
|
|
|
|
* @param s LZW state
|
|
|
|
* @param outbuf Output buffer
|
|
|
|
* @param outsize Size of output buffer
|
|
|
|
* @param maxbits Maximum length of code
|
|
|
|
*/
|
2009-12-03 21:17:39 +02:00
|
|
|
void ff_lzw_encode_init(LZWEncodeState *s, uint8_t *outbuf, int outsize,
|
2020-07-19 20:29:29 +02:00
|
|
|
int maxbits, enum FF_LZW_MODES mode, int little_endian)
|
2007-04-07 18:10:22 +03:00
|
|
|
{
|
|
|
|
s->clear_code = 256;
|
|
|
|
s->end_code = 257;
|
|
|
|
s->maxbits = maxbits;
|
|
|
|
init_put_bits(&s->pb, outbuf, outsize);
|
|
|
|
s->bufsize = outsize;
|
2013-08-14 23:56:56 +03:00
|
|
|
av_assert0(s->maxbits >= 9 && s->maxbits <= LZW_MAXBITS);
|
2007-04-07 18:10:22 +03:00
|
|
|
s->maxcode = 1 << s->maxbits;
|
|
|
|
s->output_bytes = 0;
|
|
|
|
s->last_code = LZW_PREFIX_EMPTY;
|
|
|
|
s->bits = 9;
|
2009-12-03 21:17:39 +02:00
|
|
|
s->mode = mode;
|
2020-07-19 20:29:29 +02:00
|
|
|
s->little_endian = little_endian;
|
2007-04-07 18:10:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LZW main compress function
|
|
|
|
* @param s LZW state
|
|
|
|
* @param inbuf Input buffer
|
|
|
|
* @param insize Size of input buffer
|
|
|
|
* @return Number of bytes written or -1 on error
|
|
|
|
*/
|
|
|
|
int ff_lzw_encode(LZWEncodeState * s, const uint8_t * inbuf, int insize)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if(insize * 3 > (s->bufsize - s->output_bytes) * 2){
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-04-10 01:21:49 +03:00
|
|
|
if (s->last_code == LZW_PREFIX_EMPTY)
|
2007-04-07 18:10:22 +03:00
|
|
|
clearTable(s);
|
|
|
|
|
|
|
|
for (i = 0; i < insize; i++) {
|
|
|
|
uint8_t c = *inbuf++;
|
2007-04-10 01:21:49 +03:00
|
|
|
int code = findCode(s, c, s->last_code);
|
2007-04-10 01:18:09 +03:00
|
|
|
if (s->tab[code].hash_prefix == LZW_PREFIX_FREE) {
|
2007-04-10 01:21:49 +03:00
|
|
|
writeCode(s, s->last_code);
|
|
|
|
addCode(s, c, s->last_code, code);
|
2007-04-10 01:18:09 +03:00
|
|
|
code= hash(0, c);
|
2007-04-07 18:10:22 +03:00
|
|
|
}
|
2007-04-10 01:21:49 +03:00
|
|
|
s->last_code = s->tab[code].code;
|
2007-04-07 18:10:22 +03:00
|
|
|
if (s->tabsize >= s->maxcode - 1) {
|
|
|
|
clearTable(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return writtenBytes(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write end code and flush bitstream
|
|
|
|
* @param s LZW state
|
|
|
|
* @return Number of bytes written or -1 on error
|
|
|
|
*/
|
2020-07-19 20:29:29 +02:00
|
|
|
int ff_lzw_encode_flush(LZWEncodeState *s)
|
2007-04-07 18:10:22 +03:00
|
|
|
{
|
|
|
|
if (s->last_code != -1)
|
|
|
|
writeCode(s, s->last_code);
|
|
|
|
writeCode(s, s->end_code);
|
2020-07-19 20:29:29 +02:00
|
|
|
if (s->little_endian) {
|
|
|
|
if (s->mode == FF_LZW_GIF)
|
|
|
|
put_bits_le(&s->pb, 1, 0);
|
2013-08-14 23:38:55 +03:00
|
|
|
|
2020-07-19 20:29:29 +02:00
|
|
|
flush_put_bits_le(&s->pb);
|
|
|
|
} else {
|
|
|
|
if (s->mode == FF_LZW_GIF)
|
|
|
|
put_bits(&s->pb, 1, 0);
|
|
|
|
|
|
|
|
flush_put_bits(&s->pb);
|
|
|
|
}
|
2007-04-07 18:10:22 +03:00
|
|
|
s->last_code = -1;
|
|
|
|
|
|
|
|
return writtenBytes(s);
|
|
|
|
}
|