lzw: switch to bytestream2

Found-by: Mateusz "j00ru" Jurczyk and Gynvael Coldwind
CC:libav-stable@libav.org
pull/53/head
Anton Khirnov 11 years ago
parent 1b5d065ca7
commit e89aa4bf56
  1. 27
      libavcodec/lzw.c

@ -28,6 +28,7 @@
*/ */
#include "avcodec.h" #include "avcodec.h"
#include "bytestream.h"
#include "lzw.h" #include "lzw.h"
#include "libavutil/mem.h" #include "libavutil/mem.h"
@ -43,7 +44,7 @@ static const uint16_t mask[17] =
}; };
struct LZWState { struct LZWState {
const uint8_t *buf_start, *pbuf, *ebuf; GetByteContext gb;
int bbits; int bbits;
unsigned int bbuf; unsigned int bbuf;
@ -73,9 +74,9 @@ static int lzw_get_code(struct LZWState * s)
if(s->mode == FF_LZW_GIF) { if(s->mode == FF_LZW_GIF) {
while (s->bbits < s->cursize) { while (s->bbits < s->cursize) {
if (!s->bs) { if (!s->bs) {
s->bs = *s->pbuf++; s->bs = bytestream2_get_byte(&s->gb);
} }
s->bbuf |= (*s->pbuf++) << s->bbits; s->bbuf |= bytestream2_get_byte(&s->gb) << s->bbits;
s->bbits += 8; s->bbits += 8;
s->bs--; s->bs--;
} }
@ -83,7 +84,7 @@ static int lzw_get_code(struct LZWState * s)
s->bbuf >>= s->cursize; s->bbuf >>= s->cursize;
} else { // TIFF } else { // TIFF
while (s->bbits < s->cursize) { while (s->bbits < s->cursize) {
s->bbuf = (s->bbuf << 8) | (*s->pbuf++); s->bbuf = (s->bbuf << 8) | bytestream2_get_byte(&s->gb);
s->bbits += 8; s->bbits += 8;
} }
c = s->bbuf >> (s->bbits - s->cursize); c = s->bbuf >> (s->bbits - s->cursize);
@ -95,7 +96,7 @@ static int lzw_get_code(struct LZWState * s)
int ff_lzw_size_read(LZWState *p) int ff_lzw_size_read(LZWState *p)
{ {
struct LZWState *s = p; struct LZWState *s = p;
return s->pbuf - s->buf_start; return bytestream2_tell(&s->gb);
} }
void ff_lzw_decode_tail(LZWState *p) void ff_lzw_decode_tail(LZWState *p)
@ -103,17 +104,12 @@ void ff_lzw_decode_tail(LZWState *p)
struct LZWState *s = (struct LZWState *)p; struct LZWState *s = (struct LZWState *)p;
if(s->mode == FF_LZW_GIF) { if(s->mode == FF_LZW_GIF) {
while (s->bs > 0) { while (s->bs > 0 && bytestream2_get_bytes_left(&s->gb)) {
if (s->bs >= s->ebuf - s->pbuf) { bytestream2_skip(&s->gb, s->bs);
s->pbuf = s->ebuf; s->bs = bytestream2_get_byte(&s->gb);
break;
} else {
s->pbuf += s->bs;
s->bs = *s->pbuf++;
}
} }
}else }else
s->pbuf= s->ebuf; bytestream2_skip(&s->gb, bytestream2_get_bytes_left(&s->gb));
} }
av_cold void ff_lzw_decode_open(LZWState **p) av_cold void ff_lzw_decode_open(LZWState **p)
@ -141,8 +137,7 @@ int ff_lzw_decode_init(LZWState *p, int csize, const uint8_t *buf, int buf_size,
if(csize < 1 || csize >= LZW_MAXBITS) if(csize < 1 || csize >= LZW_MAXBITS)
return -1; return -1;
/* read buffer */ /* read buffer */
s->pbuf = buf; bytestream2_init(&s->gb, buf, buf_size);
s->ebuf = s->pbuf + buf_size;
s->bbuf = 0; s->bbuf = 0;
s->bbits = 0; s->bbits = 0;
s->bs = 0; s->bs = 0;

Loading…
Cancel
Save