From 3f943fe6815b493741ff65f6c25ca856c38cdafc Mon Sep 17 00:00:00 2001 From: Michael Niedermayer Date: Wed, 22 Aug 2012 03:57:45 +0200 Subject: huffman/huffyuv: move lorens huffman table generation code to huffman.c/h Reviewed-by: Derek Buitenhuis Signed-off-by: Michael Niedermayer --- libavcodec/huffman.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'libavcodec/huffman.c') diff --git a/libavcodec/huffman.c b/libavcodec/huffman.c index cd1d0d754e..8b336dc7ff 100644 --- a/libavcodec/huffman.c +++ b/libavcodec/huffman.c @@ -1,5 +1,6 @@ /* * Copyright (c) 2006 Konstantin Shishkov + * Copyright (c) 2007 Loren Merritt * * This file is part of FFmpeg. * @@ -111,3 +112,60 @@ int ff_huff_build_tree(AVCodecContext *avctx, VLC *vlc, int nb_codes, } return 0; } + +typedef struct { + uint64_t val; + int name; +} HeapElem; + +static void heap_sift(HeapElem *h, int root, int size) +{ + while(root*2+1 < size) { + int child = root*2+1; + if(child < size-1 && h[child].val > h[child+1].val) + child++; + if(h[root].val > h[child].val) { + FFSWAP(HeapElem, h[root], h[child]); + root = child; + } else + break; + } +} + +void ff_generate_len_table(uint8_t *dst, const uint64_t *stats){ + HeapElem h[256]; + int up[2*256]; + int len[2*256]; + int offset, i, next; + int size = 256; + + for(offset=1; ; offset<<=1){ + for(i=0; i=0; i--) + heap_sift(h, i, size); + + for(next=size; next=size; i--) + len[i] = len[up[i]] + 1; + for(i=0; i= 32) break; + } + if(i==size) break; + } +} -- cgit v1.2.3