code:
/********************************************************************
 *                                                                  *
 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
 *                                                                  *
 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007             *
 * by the Xiph.Org Foundation http://www.xiph.org/                  *
 *                                                                  *
 ********************************************************************

 function: static codebooks autogenerated by huff/huffbuld
 last modified: $Id: floor_books.h 16939 2010-03-01 08:38:14Z xiphmont $

 ********************************************************************/

#include "codebook.h"

static const long _huff_lengthlist_line_256x7_0sub1[] = {
         0, 2, 3, 3, 3, 3, 4, 3, 4,
};

static const static_codebook _huff_book_line_256x7_0sub1 = {
        1, 9,
        (long *)_huff_lengthlist_line_256x7_0sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_256x7_0sub2[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 3, 4, 3, 5, 3,
         6, 3, 6, 4, 6, 4, 7, 5, 7,
};

static const static_codebook _huff_book_line_256x7_0sub2 = {
        1, 25,
        (long *)_huff_lengthlist_line_256x7_0sub2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_256x7_0sub3[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 2, 5, 3, 5, 3,
         6, 3, 6, 4, 7, 6, 7, 8, 7, 9, 8, 9, 9, 9,10, 9,
        11,13,11,13,10,10,13,13,13,13,13,13,12,12,12,12,
};

static const static_codebook _huff_book_line_256x7_0sub3 = {
        1, 64,
        (long *)_huff_lengthlist_line_256x7_0sub3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_256x7_1sub1[] = {
         0, 3, 3, 3, 3, 2, 4, 3, 4,
};

static const static_codebook _huff_book_line_256x7_1sub1 = {
        1, 9,
        (long *)_huff_lengthlist_line_256x7_1sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_256x7_1sub2[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 3, 4, 3, 4, 4,
         5, 4, 6, 5, 6, 7, 6, 8, 8,
};

static const static_codebook _huff_book_line_256x7_1sub2 = {
        1, 25,
        (long *)_huff_lengthlist_line_256x7_1sub2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_256x7_1sub3[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 4, 3, 6, 3, 7,
         3, 8, 5, 8, 6, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
         8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 7,
};

static const static_codebook _huff_book_line_256x7_1sub3 = {
        1, 64,
        (long *)_huff_lengthlist_line_256x7_1sub3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_256x7_class0[] = {
         7, 5, 5, 9, 9, 6, 6, 9,12, 8, 7, 8,11, 8, 9,15,
         6, 3, 3, 7, 7, 4, 3, 6, 9, 6, 5, 6, 8, 6, 8,15,
         8, 5, 5, 9, 8, 5, 4, 6,10, 7, 5, 5,11, 8, 7,15,
        14,15,13,13,13,13, 8,11,15,10, 7, 6,11, 9,10,15,
};

static const static_codebook _huff_book_line_256x7_class0 = {
        1, 64,
        (long *)_huff_lengthlist_line_256x7_class0,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_256x7_class1[] = {
         5, 6, 8,15, 6, 9,10,15,10,11,12,15,15,15,15,15,
         4, 6, 7,15, 6, 7, 8,15, 9, 8, 9,15,15,15,15,15,
         6, 8, 9,15, 7, 7, 8,15,10, 9,10,15,15,15,15,15,
        15,13,15,15,15,10,11,15,15,13,13,15,15,15,15,15,
         4, 6, 7,15, 6, 8, 9,15,10,10,12,15,15,15,15,15,
         2, 5, 6,15, 5, 6, 7,15, 8, 6, 7,15,15,15,15,15,
         5, 6, 8,15, 5, 6, 7,15, 9, 6, 7,15,15,15,15,15,
        14,12,13,15,12,10,11,15,15,15,15,15,15,15,15,15,
         7, 8, 9,15, 9,10,10,15,15,14,14,15,15,15,15,15,
         5, 6, 7,15, 7, 8, 9,15,12, 9,10,15,15,15,15,15,
         7, 7, 9,15, 7, 7, 8,15,12, 8, 9,15,15,15,15,15,
        13,13,14,15,12,11,12,15,15,15,15,15,15,15,15,15,
        15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
        13,13,13,15,15,15,15,15,15,15,15,15,15,15,15,15,
        15,12,13,15,15,12,13,15,15,14,15,15,15,15,15,15,
        15,15,15,15,15,15,13,15,15,15,15,15,15,15,15,15,
};

static const static_codebook _huff_book_line_256x7_class1 = {
        1, 256,
        (long *)_huff_lengthlist_line_256x7_class1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_512x17_0sub0[] = {
         4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
         5, 6, 5, 6, 6, 6, 6, 5, 6, 6, 7, 6, 7, 6, 7, 6,
         7, 6, 8, 7, 8, 7, 8, 7, 8, 7, 8, 7, 9, 7, 9, 7,
         9, 7, 9, 8, 9, 8,10, 8,10, 8,10, 7,10, 6,10, 8,
        10, 8,11, 7,10, 7,11, 8,11,11,12,12,11,11,12,11,
        13,11,13,11,13,12,15,12,13,13,14,14,14,14,14,15,
        15,15,16,14,17,19,19,18,18,18,18,18,18,18,18,18,
        18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,
};

static const static_codebook _huff_book_line_512x17_0sub0 = {
        1, 128,
        (long *)_huff_lengthlist_line_512x17_0sub0,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_512x17_1sub0[] = {
         2, 4, 5, 4, 5, 4, 5, 4, 5, 5, 5, 5, 5, 5, 6, 5,
         6, 5, 6, 6, 7, 6, 7, 6, 8, 7, 8, 7, 8, 7, 8, 7,
};

static const static_codebook _huff_book_line_512x17_1sub0 = {
        1, 32,
        (long *)_huff_lengthlist_line_512x17_1sub0,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_512x17_1sub1[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         4, 3, 5, 3, 5, 4, 5, 4, 5, 4, 5, 5, 5, 5, 6, 5,
         6, 5, 7, 5, 8, 6, 8, 6, 8, 6, 8, 6, 8, 7, 9, 7,
         9, 7,11, 9,11,11,12,11,14,12,14,16,14,16,13,16,
        14,16,12,15,13,16,14,16,13,14,12,15,13,15,13,13,
        13,15,12,14,14,15,13,15,12,15,15,15,15,15,15,15,
        15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
};

static const static_codebook _huff_book_line_512x17_1sub1 = {
        1, 128,
        (long *)_huff_lengthlist_line_512x17_1sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_512x17_2sub1[] = {
         0, 4, 5, 4, 4, 4, 5, 4, 4, 4, 5, 4, 5, 4, 5, 3,
         5, 3,
};

static const static_codebook _huff_book_line_512x17_2sub1 = {
        1, 18,
        (long *)_huff_lengthlist_line_512x17_2sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_512x17_2sub2[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 4, 3, 4, 3, 4, 4, 5, 4, 5, 4, 6, 4, 6, 5,
         6, 5, 7, 5, 7, 6, 8, 6, 8, 6, 8, 7, 8, 7, 9, 7,
         9, 8,
};

static const static_codebook _huff_book_line_512x17_2sub2 = {
        1, 50,
        (long *)_huff_lengthlist_line_512x17_2sub2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_512x17_2sub3[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 3, 3, 3, 3, 4, 3, 4, 4, 5, 5, 6, 6, 7, 7,
         7, 8, 8,11, 8, 9, 9, 9,10,11,11,11, 9,10,10,11,
        11,11,11,10,10,10,10,10,10,10,10,10,10,10,10,10,
        10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
        10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
};

static const static_codebook _huff_book_line_512x17_2sub3 = {
        1, 128,
        (long *)_huff_lengthlist_line_512x17_2sub3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_512x17_3sub1[] = {
         0, 4, 4, 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 5, 4, 5,
         5, 5,
};

static const static_codebook _huff_book_line_512x17_3sub1 = {
        1, 18,
        (long *)_huff_lengthlist_line_512x17_3sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_512x17_3sub2[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 2, 3, 3, 4, 3, 5, 4, 6, 4, 6, 5, 7, 6, 7,
         6, 8, 6, 8, 7, 9, 8,10, 8,12, 9,13,10,15,10,15,
        11,14,
};

static const static_codebook _huff_book_line_512x17_3sub2 = {
        1, 50,
        (long *)_huff_lengthlist_line_512x17_3sub2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_512x17_3sub3[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 4, 8, 4, 8, 4, 8, 4, 8, 5, 8, 5, 8, 6, 8,
         4, 8, 4, 8, 5, 8, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7,
         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
};

static const static_codebook _huff_book_line_512x17_3sub3 = {
        1, 128,
        (long *)_huff_lengthlist_line_512x17_3sub3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_512x17_class1[] = {
         1, 2, 3, 6, 5, 4, 7, 7,
};

static const static_codebook _huff_book_line_512x17_class1 = {
        1, 8,
        (long *)_huff_lengthlist_line_512x17_class1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_512x17_class2[] = {
         3, 3, 3,14, 5, 4, 4,11, 8, 6, 6,10,17,12,11,17,
         6, 5, 5,15, 5, 3, 4,11, 8, 5, 5, 8,16, 9,10,14,
        10, 8, 9,17, 8, 6, 6,13,10, 7, 7,10,16,11,13,14,
        17,17,17,17,17,16,16,16,16,15,16,16,16,16,16,16,
};

static const static_codebook _huff_book_line_512x17_class2 = {
        1, 64,
        (long *)_huff_lengthlist_line_512x17_class2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_512x17_class3[] = {
         2, 4, 6,17, 4, 5, 7,17, 8, 7,10,17,17,17,17,17,
         3, 4, 6,15, 3, 3, 6,15, 7, 6, 9,17,17,17,17,17,
         6, 8,10,17, 6, 6, 8,16, 9, 8,10,17,17,15,16,17,
        17,17,17,17,12,15,15,16,12,15,15,16,16,16,16,16,
};

static const static_codebook _huff_book_line_512x17_class3 = {
        1, 64,
        (long *)_huff_lengthlist_line_512x17_class3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x4_class0[] = {
         7, 7, 7,11, 6, 6, 7,11, 7, 6, 6,10,12,10,10,13,
         7, 7, 8,11, 7, 7, 7,11, 7, 6, 7,10,11,10,10,13,
        10,10, 9,12, 9, 9, 9,11, 8, 8, 8,11,13,11,10,14,
        15,15,14,15,15,14,13,14,15,12,12,17,17,17,17,17,
         7, 7, 6, 9, 6, 6, 6, 9, 7, 6, 6, 8,11,11,10,12,
         7, 7, 7, 9, 7, 6, 6, 9, 7, 6, 6, 9,13,10,10,11,
        10, 9, 8,10, 9, 8, 8,10, 8, 8, 7, 9,13,12,10,11,
        17,14,14,13,15,14,12,13,17,13,12,15,17,17,14,17,
         7, 6, 6, 7, 6, 6, 5, 7, 6, 6, 6, 6,11, 9, 9, 9,
         7, 7, 6, 7, 7, 6, 6, 7, 6, 6, 6, 6,10, 9, 8, 9,
        10, 9, 8, 8, 9, 8, 7, 8, 8, 7, 6, 8,11,10, 9,10,
        17,17,12,15,15,15,12,14,14,14,10,12,15,13,12,13,
        11,10, 8,10,11,10, 8, 8,10, 9, 7, 7,10, 9, 9,11,
        11,11, 9,10,11,10, 8, 9,10, 8, 6, 8,10, 9, 9,11,
        14,13,10,12,12,11,10,10, 8, 7, 8,10,10,11,11,12,
        17,17,15,17,17,17,17,17,17,13,12,17,17,17,14,17,
};

static const static_codebook _huff_book_line_128x4_class0 = {
        1, 256,
        (long *)_huff_lengthlist_line_128x4_class0,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x4_0sub0[] = {
         2, 2, 2, 2,
};

static const static_codebook _huff_book_line_128x4_0sub0 = {
        1, 4,
        (long *)_huff_lengthlist_line_128x4_0sub0,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x4_0sub1[] = {
         0, 0, 0, 0, 3, 2, 3, 2, 3, 3,
};

static const static_codebook _huff_book_line_128x4_0sub1 = {
        1, 10,
        (long *)_huff_lengthlist_line_128x4_0sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x4_0sub2[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 4, 3, 4, 3,
         4, 4, 5, 4, 5, 4, 6, 5, 6,
};

static const static_codebook _huff_book_line_128x4_0sub2 = {
        1, 25,
        (long *)_huff_lengthlist_line_128x4_0sub2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x4_0sub3[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 3, 5, 3, 5, 3,
         5, 4, 6, 5, 6, 5, 7, 6, 6, 7, 7, 9, 9,11,11,16,
        11,14,10,11,11,13,16,15,15,15,15,15,15,15,15,15,
};

static const static_codebook _huff_book_line_128x4_0sub3 = {
        1, 64,
        (long *)_huff_lengthlist_line_128x4_0sub3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_256x4_class0[] = {
         6, 7, 7,12, 6, 6, 7,12, 7, 6, 6,10,15,12,11,13,
         7, 7, 8,13, 7, 7, 8,12, 7, 7, 7,11,12,12,11,13,
        10, 9, 9,11, 9, 9, 9,10,10, 8, 8,12,14,12,12,14,
        11,11,12,14,11,12,11,15,15,12,13,15,15,15,15,15,
         6, 6, 7,10, 6, 6, 6,11, 7, 6, 6, 9,14,12,11,13,
         7, 7, 7,10, 6, 6, 7, 9, 7, 7, 6,10,13,12,10,12,
         9, 9, 9,11, 9, 9, 8, 9, 9, 8, 8,10,13,12,10,12,
        12,12,11,13,12,12,11,12,15,13,12,15,15,15,14,14,
         6, 6, 6, 8, 6, 6, 5, 6, 7, 7, 6, 5,11,10, 9, 8,
         7, 6, 6, 7, 6, 6, 5, 6, 7, 7, 6, 6,11,10, 9, 8,
         8, 8, 8, 9, 8, 8, 7, 8, 8, 8, 6, 7,11,10, 9, 9,
        14,11,10,14,14,11,10,15,13,11, 9,11,15,12,12,11,
        11, 9, 8, 8,10, 9, 8, 9,11,10, 9, 8,12,11,12,11,
        13,10, 8, 9,11,10, 8, 9,10, 9, 8, 9,10, 8,12,12,
        15,11,10,10,13,11,10,10, 8, 8, 7,12,10, 9,11,12,
        15,12,11,15,13,11,11,15,12,14,11,13,15,15,13,13,
};

static const static_codebook _huff_book_line_256x4_class0 = {
        1, 256,
        (long *)_huff_lengthlist_line_256x4_class0,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_256x4_0sub0[] = {
         2, 2, 2, 2,
};

static const static_codebook _huff_book_line_256x4_0sub0 = {
        1, 4,
        (long *)_huff_lengthlist_line_256x4_0sub0,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_256x4_0sub1[] = {
         0, 0, 0, 0, 2, 2, 3, 3, 3, 3,
};

static const static_codebook _huff_book_line_256x4_0sub1 = {
        1, 10,
        (long *)_huff_lengthlist_line_256x4_0sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_256x4_0sub2[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 3, 4, 3, 4, 3,
         5, 3, 5, 4, 5, 4, 6, 4, 6,
};

static const static_codebook _huff_book_line_256x4_0sub2 = {
        1, 25,
        (long *)_huff_lengthlist_line_256x4_0sub2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_256x4_0sub3[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 3, 5, 3, 5, 3,
         6, 4, 7, 4, 7, 5, 7, 6, 7, 6, 7, 8,10,13,13,13,
        13,13,13,13,13,13,13,13,13,13,13,12,12,12,12,12,
};

static const static_codebook _huff_book_line_256x4_0sub3 = {
        1, 64,
        (long *)_huff_lengthlist_line_256x4_0sub3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x7_class0[] = {
        10, 7, 8,13, 9, 6, 7,11,10, 8, 8,12,17,17,17,17,
         7, 5, 5, 9, 6, 4, 4, 8, 8, 5, 5, 8,16,14,13,16,
         7, 5, 5, 7, 6, 3, 3, 5, 8, 5, 4, 7,14,12,12,15,
        10, 7, 8, 9, 7, 5, 5, 6, 9, 6, 5, 5,15,12, 9,10,
};

static const static_codebook _huff_book_line_128x7_class0 = {
        1, 64,
        (long *)_huff_lengthlist_line_128x7_class0,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x7_class1[] = {
         8,13,17,17, 8,11,17,17,11,13,17,17,17,17,17,17,
         6,10,16,17, 6,10,15,17, 8,10,16,17,17,17,17,17,
         9,13,15,17, 8,11,17,17,10,12,17,17,17,17,17,17,
        17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,
         6,11,15,17, 7,10,15,17, 8,10,17,17,17,15,17,17,
         4, 8,13,17, 4, 7,13,17, 6, 8,15,17,16,15,17,17,
         6,11,15,17, 6, 9,13,17, 8,10,17,17,15,17,17,17,
        16,17,17,17,12,14,15,17,13,14,15,17,17,17,17,17,
         5,10,14,17, 5, 9,14,17, 7, 9,15,17,15,15,17,17,
         3, 7,12,17, 3, 6,11,17, 5, 7,13,17,12,12,17,17,
         5, 9,14,17, 3, 7,11,17, 5, 8,13,17,13,11,16,17,
        12,17,17,17, 9,14,15,17,10,11,14,17,16,14,17,17,
         8,12,17,17, 8,12,17,17,10,12,17,17,17,17,17,17,
         5,10,17,17, 5, 9,15,17, 7, 9,17,17,13,13,17,17,
         7,11,17,17, 6,10,15,17, 7, 9,15,17,12,11,17,17,
        12,15,17,17,11,14,17,17,11,10,15,17,17,16,17,17,
};

static const static_codebook _huff_book_line_128x7_class1 = {
        1, 256,
        (long *)_huff_lengthlist_line_128x7_class1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x7_0sub1[] = {
         0, 3, 3, 3, 3, 3, 3, 3, 3,
};

static const static_codebook _huff_book_line_128x7_0sub1 = {
        1, 9,
        (long *)_huff_lengthlist_line_128x7_0sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x7_0sub2[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 4, 4, 4, 4,
         5, 4, 5, 4, 5, 4, 6, 4, 6,
};

static const static_codebook _huff_book_line_128x7_0sub2 = {
        1, 25,
        (long *)_huff_lengthlist_line_128x7_0sub2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x7_0sub3[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 3, 5, 3, 5, 4,
         5, 4, 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5,
         7, 8, 9,11,13,13,13,13,13,13,13,13,13,13,13,13,
};

static const static_codebook _huff_book_line_128x7_0sub3 = {
        1, 64,
        (long *)_huff_lengthlist_line_128x7_0sub3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x7_1sub1[] = {
         0, 3, 3, 2, 3, 3, 4, 3, 4,
};

static const static_codebook _huff_book_line_128x7_1sub1 = {
        1, 9,
        (long *)_huff_lengthlist_line_128x7_1sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x7_1sub2[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 3, 6, 3, 6, 3,
         6, 3, 7, 3, 8, 4, 9, 4, 9,
};

static const static_codebook _huff_book_line_128x7_1sub2 = {
        1, 25,
        (long *)_huff_lengthlist_line_128x7_1sub2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x7_1sub3[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 7, 2, 7, 3, 8, 4,
         9, 5, 9, 8,10,11,11,12,14,14,14,14,14,14,14,14,
        14,14,14,14,14,14,14,14,14,14,14,14,13,13,13,13,
};

static const static_codebook _huff_book_line_128x7_1sub3 = {
        1, 64,
        (long *)_huff_lengthlist_line_128x7_1sub3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x11_class1[] = {
         1, 6, 3, 7, 2, 4, 5, 7,
};

static const static_codebook _huff_book_line_128x11_class1 = {
        1, 8,
        (long *)_huff_lengthlist_line_128x11_class1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x11_class2[] = {
         1, 6,12,16, 4,12,15,16, 9,15,16,16,16,16,16,16,
         2, 5,11,16, 5,11,13,16, 9,13,16,16,16,16,16,16,
         4, 8,12,16, 5, 9,12,16, 9,13,15,16,16,16,16,16,
        15,16,16,16,11,14,13,16,12,15,16,16,16,16,16,15,
};

static const static_codebook _huff_book_line_128x11_class2 = {
        1, 64,
        (long *)_huff_lengthlist_line_128x11_class2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x11_class3[] = {
         7, 6, 9,17, 7, 6, 8,17,12, 9,11,16,16,16,16,16,
         5, 4, 7,16, 5, 3, 6,14, 9, 6, 8,15,16,16,16,16,
         5, 4, 6,13, 3, 2, 4,11, 7, 4, 6,13,16,11,10,14,
        12,12,12,16, 9, 7,10,15,12, 9,11,16,16,15,15,16,
};

static const static_codebook _huff_book_line_128x11_class3 = {
        1, 64,
        (long *)_huff_lengthlist_line_128x11_class3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x11_0sub0[] = {
         5, 5, 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5,
         6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 6, 6, 6, 7, 6,
         7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 8, 6, 8, 6, 8, 7,
         8, 7, 8, 7, 8, 7, 9, 7, 9, 8, 9, 8, 9, 8,10, 8,
        10, 9,10, 9,10, 9,11, 9,11, 9,10,10,11,10,11,10,
        11,11,11,11,11,11,12,13,14,14,14,15,15,16,16,16,
        17,15,16,15,16,16,17,17,16,17,17,17,17,17,17,17,
        17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,
};

static const static_codebook _huff_book_line_128x11_0sub0 = {
        1, 128,
        (long *)_huff_lengthlist_line_128x11_0sub0,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x11_1sub0[] = {
         2, 5, 5, 5, 5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5,
         6, 5, 6, 5, 6, 5, 7, 6, 7, 6, 7, 6, 8, 6, 8, 6,
};

static const static_codebook _huff_book_line_128x11_1sub0 = {
        1, 32,
        (long *)_huff_lengthlist_line_128x11_1sub0,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x11_1sub1[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         5, 3, 5, 3, 6, 4, 6, 4, 7, 4, 7, 4, 7, 4, 8, 4,
         8, 4, 9, 5, 9, 5, 9, 5, 9, 6,10, 6,10, 6,11, 7,
        10, 7,10, 8,11, 9,11, 9,11,10,11,11,12,11,11,12,
        15,15,12,14,11,14,12,14,11,14,13,14,12,14,11,14,
        11,14,12,14,11,14,11,14,13,13,14,14,14,14,14,14,
        14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
};

static const static_codebook _huff_book_line_128x11_1sub1 = {
        1, 128,
        (long *)_huff_lengthlist_line_128x11_1sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x11_2sub1[] = {
         0, 4, 5, 4, 5, 4, 5, 3, 5, 3, 5, 3, 5, 4, 4, 4,
         5, 5,
};

static const static_codebook _huff_book_line_128x11_2sub1 = {
        1, 18,
        (long *)_huff_lengthlist_line_128x11_2sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x11_2sub2[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 3, 3, 3, 4, 4, 4, 4, 5, 4, 5, 4, 6, 5, 7,
         5, 7, 6, 8, 6, 8, 6, 9, 7, 9, 7,10, 7, 9, 8,11,
         8,11,
};

static const static_codebook _huff_book_line_128x11_2sub2 = {
        1, 50,
        (long *)_huff_lengthlist_line_128x11_2sub2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x11_2sub3[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 4, 8, 3, 8, 4, 8, 4, 8, 6, 8, 5, 8, 4, 8,
         4, 8, 6, 8, 7, 8, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
};

static const static_codebook _huff_book_line_128x11_2sub3 = {
        1, 128,
        (long *)_huff_lengthlist_line_128x11_2sub3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x11_3sub1[] = {
         0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4,
         5, 4,
};

static const static_codebook _huff_book_line_128x11_3sub1 = {
        1, 18,
        (long *)_huff_lengthlist_line_128x11_3sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x11_3sub2[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 5, 3, 5, 4, 6, 4, 6, 4, 7, 4, 7, 4, 8, 4,
         8, 4, 9, 4, 9, 4,10, 4,10, 5,10, 5,11, 5,12, 6,
        12, 6,
};

static const static_codebook _huff_book_line_128x11_3sub2 = {
        1, 50,
        (long *)_huff_lengthlist_line_128x11_3sub2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x11_3sub3[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 7, 1, 6, 3, 7, 3, 8, 4, 8, 5, 8, 8, 8, 9,
         7, 8, 8, 7, 7, 7, 8, 9,10, 9, 9,10,10,10,10,10,
        10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
        10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
        10,10,10,10,10,10,10,10,10,10,10,10,10,10, 9, 9,
};

static const static_codebook _huff_book_line_128x11_3sub3 = {
        1, 128,
        (long *)_huff_lengthlist_line_128x11_3sub3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x17_class1[] = {
         1, 3, 4, 7, 2, 5, 6, 7,
};

static const static_codebook _huff_book_line_128x17_class1 = {
        1, 8,
        (long *)_huff_lengthlist_line_128x17_class1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x17_class2[] = {
         1, 4,10,19, 3, 8,13,19, 7,12,19,19,19,19,19,19,
         2, 6,11,19, 8,13,19,19, 9,11,19,19,19,19,19,19,
         6, 7,13,19, 9,13,19,19,10,13,18,18,18,18,18,18,
        18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,
};

static const static_codebook _huff_book_line_128x17_class2 = {
        1, 64,
        (long *)_huff_lengthlist_line_128x17_class2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x17_class3[] = {
         3, 6,10,17, 4, 8,11,20, 8,10,11,20,20,20,20,20,
         2, 4, 8,18, 4, 6, 8,17, 7, 8,10,20,20,17,20,20,
         3, 5, 8,17, 3, 4, 6,17, 8, 8,10,17,17,12,16,20,
        13,13,15,20,10,10,12,20,15,14,15,20,20,20,19,19,
};

static const static_codebook _huff_book_line_128x17_class3 = {
        1, 64,
        (long *)_huff_lengthlist_line_128x17_class3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x17_0sub0[] = {
         5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5,
         7, 5, 7, 5, 7, 5, 7, 5, 7, 5, 7, 5, 8, 5, 8, 5,
         8, 5, 8, 5, 8, 6, 8, 6, 8, 6, 9, 6, 9, 6, 9, 6,
         9, 6, 9, 7, 9, 7, 9, 7, 9, 7,10, 7,10, 8,10, 8,
        10, 8,10, 8,10, 8,11, 8,11, 8,11, 8,11, 8,11, 9,
        12, 9,12, 9,12, 9,12, 9,12,10,12,10,13,11,13,11,
        14,12,14,13,15,14,16,14,17,15,18,16,20,20,20,20,
        20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
};

static const static_codebook _huff_book_line_128x17_0sub0 = {
        1, 128,
        (long *)_huff_lengthlist_line_128x17_0sub0,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x17_1sub0[] = {
         2, 5, 5, 4, 5, 4, 5, 4, 5, 5, 5, 5, 5, 5, 6, 5,
         6, 5, 6, 5, 7, 6, 7, 6, 7, 6, 8, 6, 9, 7, 9, 7,
};

static const static_codebook _huff_book_line_128x17_1sub0 = {
        1, 32,
        (long *)_huff_lengthlist_line_128x17_1sub0,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x17_1sub1[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         4, 3, 5, 3, 5, 3, 6, 3, 6, 4, 6, 4, 7, 4, 7, 5,
         8, 5, 8, 6, 9, 7, 9, 7, 9, 8,10, 9,10, 9,11,10,
        11,11,11,11,11,11,12,12,12,13,12,13,12,14,12,15,
        12,14,12,16,13,17,13,17,14,17,14,16,13,17,14,17,
        14,17,15,17,15,15,16,17,17,17,17,17,17,17,17,17,
        17,17,17,17,17,17,16,16,16,16,16,16,16,16,16,16,
};

static const static_codebook _huff_book_line_128x17_1sub1 = {
        1, 128,
        (long *)_huff_lengthlist_line_128x17_1sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x17_2sub1[] = {
         0, 4, 5, 4, 6, 4, 8, 3, 9, 3, 9, 2, 9, 3, 8, 4,
         9, 4,
};

static const static_codebook _huff_book_line_128x17_2sub1 = {
        1, 18,
        (long *)_huff_lengthlist_line_128x17_2sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x17_2sub2[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 5, 1, 5, 3, 5, 3, 5, 4, 7, 5,10, 7,10, 7,
        12,10,14,10,14, 9,14,11,14,14,14,13,13,13,13,13,
        13,13,
};

static const static_codebook _huff_book_line_128x17_2sub2 = {
        1, 50,
        (long *)_huff_lengthlist_line_128x17_2sub2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x17_2sub3[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 6, 6,
         6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
         6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
         6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
};

static const static_codebook _huff_book_line_128x17_2sub3 = {
        1, 128,
        (long *)_huff_lengthlist_line_128x17_2sub3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x17_3sub1[] = {
         0, 4, 4, 4, 4, 4, 4, 4, 5, 3, 5, 3, 5, 4, 6, 4,
         6, 4,
};

static const static_codebook _huff_book_line_128x17_3sub1 = {
        1, 18,
        (long *)_huff_lengthlist_line_128x17_3sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x17_3sub2[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 5, 3, 6, 3, 6, 4, 7, 4, 7, 4, 7, 4, 8, 4,
         8, 4, 8, 4, 8, 4, 9, 4, 9, 5,10, 5,10, 7,10, 8,
        10, 8,
};

static const static_codebook _huff_book_line_128x17_3sub2 = {
        1, 50,
        (long *)_huff_lengthlist_line_128x17_3sub2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_128x17_3sub3[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 3, 2, 4, 3, 4, 4, 4, 5, 4, 7, 5, 8, 5,11,
         6,10, 6,12, 7,12, 7,12, 8,12, 8,12,10,12,12,12,
        12,12,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
        11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
        11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
};

static const static_codebook _huff_book_line_128x17_3sub3 = {
        1, 128,
        (long *)_huff_lengthlist_line_128x17_3sub3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_1024x27_class1[] = {
         2,10, 8,14, 7,12,11,14, 1, 5, 3, 7, 4, 9, 7,13,
};

static const static_codebook _huff_book_line_1024x27_class1 = {
        1, 16,
        (long *)_huff_lengthlist_line_1024x27_class1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_1024x27_class2[] = {
         1, 4, 2, 6, 3, 7, 5, 7,
};

static const static_codebook _huff_book_line_1024x27_class2 = {
        1, 8,
        (long *)_huff_lengthlist_line_1024x27_class2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_1024x27_class3[] = {
         1, 5, 7,21, 5, 8, 9,21,10, 9,12,20,20,16,20,20,
         4, 8, 9,20, 6, 8, 9,20,11,11,13,20,20,15,17,20,
         9,11,14,20, 8,10,15,20,11,13,15,20,20,20,20,20,
        20,20,20,20,13,20,20,20,18,18,20,20,20,20,20,20,
         3, 6, 8,20, 6, 7, 9,20,10, 9,12,20,20,20,20,20,
         5, 7, 9,20, 6, 6, 9,20,10, 9,12,20,20,20,20,20,
         8,10,13,20, 8, 9,12,20,11,10,12,20,20,20,20,20,
        18,20,20,20,15,17,18,20,18,17,18,20,20,20,20,20,
         7,10,12,20, 8, 9,11,20,14,13,14,20,20,20,20,20,
         6, 9,12,20, 7, 8,11,20,12,11,13,20,20,20,20,20,
         9,11,15,20, 8,10,14,20,12,11,14,20,20,20,20,20,
        20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
        11,16,18,20,15,15,17,20,20,17,20,20,20,20,20,20,
         9,14,16,20,12,12,15,20,17,15,18,20,20,20,20,20,
        16,19,18,20,15,16,20,20,17,17,20,20,20,20,20,20,
        20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
};

static const static_codebook _huff_book_line_1024x27_class3 = {
        1, 256,
        (long *)_huff_lengthlist_line_1024x27_class3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_1024x27_class4[] = {
         2, 3, 7,13, 4, 4, 7,15, 8, 6, 9,17,21,16,15,21,
         2, 5, 7,11, 5, 5, 7,14, 9, 7,10,16,17,15,16,21,
         4, 7,10,17, 7, 7, 9,15,11, 9,11,16,21,18,15,21,
        18,21,21,21,15,17,17,19,21,19,18,20,21,21,21,20,
};

static const static_codebook _huff_book_line_1024x27_class4 = {
        1, 64,
        (long *)_huff_lengthlist_line_1024x27_class4,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_1024x27_0sub0[] = {
         5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5,
         6, 5, 6, 5, 6, 5, 6, 5, 7, 5, 7, 5, 7, 5, 7, 5,
         8, 6, 8, 6, 8, 6, 9, 6, 9, 6,10, 6,10, 6,11, 6,
        11, 7,11, 7,12, 7,12, 7,12, 7,12, 7,12, 7,12, 7,
        12, 7,12, 8,13, 8,12, 8,12, 8,13, 8,13, 9,13, 9,
        13, 9,13, 9,12,10,12,10,13,10,14,11,14,12,14,13,
        14,13,14,14,15,16,15,15,15,14,15,17,21,22,22,21,
        22,22,22,22,22,22,21,21,21,21,21,21,21,21,21,21,
};

static const static_codebook _huff_book_line_1024x27_0sub0 = {
        1, 128,
        (long *)_huff_lengthlist_line_1024x27_0sub0,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_1024x27_1sub0[] = {
         2, 5, 5, 4, 5, 4, 5, 4, 5, 4, 6, 5, 6, 5, 6, 5,
         6, 5, 7, 5, 7, 6, 8, 6, 8, 6, 8, 6, 9, 6, 9, 6,
};

static const static_codebook _huff_book_line_1024x27_1sub0 = {
        1, 32,
        (long *)_huff_lengthlist_line_1024x27_1sub0,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_1024x27_1sub1[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         8, 5, 8, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4,
         9, 4, 9, 4, 9, 4, 8, 4, 8, 4, 9, 5, 9, 5, 9, 5,
         9, 5, 9, 6,10, 6,10, 7,10, 8,11, 9,11,11,12,13,
        12,14,13,15,13,15,14,16,14,17,15,17,15,15,16,16,
        15,16,16,16,15,18,16,15,17,17,19,19,19,19,19,19,
        19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,
};

static const static_codebook _huff_book_line_1024x27_1sub1 = {
        1, 128,
        (long *)_huff_lengthlist_line_1024x27_1sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_1024x27_2sub0[] = {
         1, 5, 5, 5, 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5,
         6, 6, 7, 7, 7, 7, 8, 7, 8, 8, 9, 8,10, 9,10, 9,
};

static const static_codebook _huff_book_line_1024x27_2sub0 = {
        1, 32,
        (long *)_huff_lengthlist_line_1024x27_2sub0,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_1024x27_2sub1[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         4, 3, 4, 3, 4, 4, 5, 4, 5, 4, 5, 5, 6, 5, 6, 5,
         7, 5, 7, 6, 7, 6, 8, 7, 8, 7, 8, 7, 9, 8, 9, 9,
         9, 9,10,10,10,11, 9,12, 9,12, 9,15,10,14, 9,13,
        10,13,10,12,10,12,10,13,10,12,11,13,11,14,12,13,
        13,14,14,13,14,15,14,16,13,13,14,16,16,16,16,16,
        16,16,16,16,16,16,16,16,16,16,16,16,16,16,15,15,
};

static const static_codebook _huff_book_line_1024x27_2sub1 = {
        1, 128,
        (long *)_huff_lengthlist_line_1024x27_2sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_1024x27_3sub1[] = {
         0, 4, 5, 4, 5, 3, 5, 3, 5, 3, 5, 4, 4, 4, 4, 5,
         5, 5,
};

static const static_codebook _huff_book_line_1024x27_3sub1 = {
        1, 18,
        (long *)_huff_lengthlist_line_1024x27_3sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_1024x27_3sub2[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 3, 3, 4, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6,
         5, 7, 5, 8, 6, 8, 6, 9, 7,10, 7,10, 8,10, 8,11,
         9,11,
};

static const static_codebook _huff_book_line_1024x27_3sub2 = {
        1, 50,
        (long *)_huff_lengthlist_line_1024x27_3sub2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_1024x27_3sub3[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 3, 7, 3, 8, 3,10, 3, 8, 3, 9, 3, 8, 4, 9,
         4, 9, 5, 9, 6,10, 6, 9, 7,11, 7,12, 9,13,10,13,
        12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
        12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
        12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
};

static const static_codebook _huff_book_line_1024x27_3sub3 = {
        1, 128,
        (long *)_huff_lengthlist_line_1024x27_3sub3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_1024x27_4sub1[] = {
         0, 4, 5, 4, 5, 4, 5, 4, 5, 3, 5, 3, 5, 3, 5, 4,
         5, 4,
};

static const static_codebook _huff_book_line_1024x27_4sub1 = {
        1, 18,
        (long *)_huff_lengthlist_line_1024x27_4sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_1024x27_4sub2[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 4, 2, 4, 2, 5, 3, 5, 4, 6, 6, 6, 7, 7, 8,
         7, 8, 7, 8, 7, 9, 8, 9, 8, 9, 8,10, 8,11, 9,12,
         9,12,
};

static const static_codebook _huff_book_line_1024x27_4sub2 = {
        1, 50,
        (long *)_huff_lengthlist_line_1024x27_4sub2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_1024x27_4sub3[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 2, 5, 2, 6, 3, 6, 4, 7, 4, 7, 5, 9, 5,11,
         6,11, 6,11, 7,11, 6,11, 6,11, 9,11, 8,11,11,11,
        11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
        11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
        11,11,11,11,11,11,11,11,11,11,10,10,10,10,10,10,
};

static const static_codebook _huff_book_line_1024x27_4sub3 = {
        1, 128,
        (long *)_huff_lengthlist_line_1024x27_4sub3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_2048x27_class1[] = {
         2, 6, 8, 9, 7,11,13,13, 1, 3, 5, 5, 6, 6,12,10,
};

static const static_codebook _huff_book_line_2048x27_class1 = {
        1, 16,
        (long *)_huff_lengthlist_line_2048x27_class1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_2048x27_class2[] = {
         1, 2, 3, 6, 4, 7, 5, 7,
};

static const static_codebook _huff_book_line_2048x27_class2 = {
        1, 8,
        (long *)_huff_lengthlist_line_2048x27_class2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_2048x27_class3[] = {
         3, 3, 6,16, 5, 5, 7,16, 9, 8,11,16,16,16,16,16,
         5, 5, 8,16, 5, 5, 7,16, 8, 7, 9,16,16,16,16,16,
         9, 9,12,16, 6, 8,11,16, 9,10,11,16,16,16,16,16,
        16,16,16,16,13,16,16,16,15,16,16,16,16,16,16,16,
         5, 4, 7,16, 6, 5, 8,16, 9, 8,10,16,16,16,16,16,
         5, 5, 7,15, 5, 4, 6,15, 7, 6, 8,16,16,16,16,16,
         9, 9,11,15, 7, 7, 9,16, 8, 8, 9,16,16,16,16,16,
        16,16,16,16,15,15,15,16,15,15,14,16,16,16,16,16,
         8, 8,11,16, 8, 9,10,16,11,10,14,16,16,16,16,16,
         6, 8,10,16, 6, 7,10,16, 8, 8,11,16,14,16,16,16,
        10,11,14,16, 9, 9,11,16,10,10,11,16,16,16,16,16,
        16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
        16,16,16,16,15,16,16,16,16,16,16,16,16,16,16,16,
        12,16,15,16,12,14,16,16,16,16,16,16,16,16,16,16,
        16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
        16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
};

static const static_codebook _huff_book_line_2048x27_class3 = {
        1, 256,
        (long *)_huff_lengthlist_line_2048x27_class3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_2048x27_class4[] = {
         2, 4, 7,13, 4, 5, 7,15, 8, 7,10,16,16,14,16,16,
         2, 4, 7,16, 3, 4, 7,14, 8, 8,10,16,16,16,15,16,
         6, 8,11,16, 7, 7, 9,16,11, 9,13,16,16,16,15,16,
        16,16,16,16,14,16,16,16,16,16,16,16,16,16,16,16,
};

static const static_codebook _huff_book_line_2048x27_class4 = {
        1, 64,
        (long *)_huff_lengthlist_line_2048x27_class4,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_2048x27_0sub0[] = {
         5, 5, 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5,
         6, 5, 7, 5, 7, 5, 7, 5, 8, 5, 8, 5, 8, 5, 9, 5,
         9, 6,10, 6,10, 6,11, 6,11, 6,11, 6,11, 6,11, 6,
        11, 6,11, 6,12, 7,11, 7,11, 7,11, 7,11, 7,10, 7,
        11, 7,11, 7,12, 7,11, 8,11, 8,11, 8,11, 8,13, 8,
        12, 9,11, 9,11, 9,11,10,12,10,12, 9,12,10,12,11,
        14,12,16,12,12,11,14,16,17,17,17,17,17,17,17,17,
        17,17,17,17,17,17,17,17,17,17,17,17,16,16,16,16,
};

static const static_codebook _huff_book_line_2048x27_0sub0 = {
        1, 128,
        (long *)_huff_lengthlist_line_2048x27_0sub0,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_2048x27_1sub0[] = {
         4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
         5, 5, 6, 6, 6, 6, 6, 6, 7, 6, 7, 6, 7, 6, 7, 6,
};

static const static_codebook _huff_book_line_2048x27_1sub0 = {
        1, 32,
        (long *)_huff_lengthlist_line_2048x27_1sub0,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_2048x27_1sub1[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         6, 5, 7, 5, 7, 4, 7, 4, 8, 4, 8, 4, 8, 4, 8, 3,
         8, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 5, 9, 5, 9, 6,
         9, 7, 9, 8, 9, 9, 9,10, 9,11, 9,14, 9,15,10,15,
        10,15,10,15,10,15,11,15,10,14,12,14,11,14,13,14,
        13,15,15,15,12,15,15,15,13,15,13,15,13,15,15,15,
        15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,14,
};

static const static_codebook _huff_book_line_2048x27_1sub1 = {
        1, 128,
        (long *)_huff_lengthlist_line_2048x27_1sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_2048x27_2sub0[] = {
         2, 4, 5, 4, 5, 4, 5, 4, 5, 5, 5, 5, 5, 5, 6, 5,
         6, 5, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
};

static const static_codebook _huff_book_line_2048x27_2sub0 = {
        1, 32,
        (long *)_huff_lengthlist_line_2048x27_2sub0,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_2048x27_2sub1[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         3, 4, 3, 4, 3, 4, 4, 5, 4, 5, 5, 5, 6, 6, 6, 7,
         6, 8, 6, 8, 6, 9, 7,10, 7,10, 7,10, 7,12, 7,12,
         7,12, 9,12,11,12,10,12,10,12,11,12,12,12,10,12,
        10,12,10,12, 9,12,11,12,12,12,12,12,11,12,11,12,
        12,12,12,12,12,12,12,12,10,10,12,12,12,12,12,10,
        12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
};

static const static_codebook _huff_book_line_2048x27_2sub1 = {
        1, 128,
        (long *)_huff_lengthlist_line_2048x27_2sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_2048x27_3sub1[] = {
         0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
         5, 5,
};

static const static_codebook _huff_book_line_2048x27_3sub1 = {
        1, 18,
        (long *)_huff_lengthlist_line_2048x27_3sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_2048x27_3sub2[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6,
         6, 7, 6, 7, 6, 8, 6, 9, 7, 9, 7, 9, 9,11, 9,12,
        10,12,
};

static const static_codebook _huff_book_line_2048x27_3sub2 = {
        1, 50,
        (long *)_huff_lengthlist_line_2048x27_3sub2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_2048x27_3sub3[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 3, 6, 3, 7, 3, 7, 5, 7, 7, 7, 7, 7, 6, 7,
         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
};

static const static_codebook _huff_book_line_2048x27_3sub3 = {
        1, 128,
        (long *)_huff_lengthlist_line_2048x27_3sub3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_2048x27_4sub1[] = {
         0, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 5, 4, 5, 4,
         4, 5,
};

static const static_codebook _huff_book_line_2048x27_4sub1 = {
        1, 18,
        (long *)_huff_lengthlist_line_2048x27_4sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_2048x27_4sub2[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 3, 2, 4, 3, 4, 4, 4, 5, 5, 6, 5, 6, 5, 7,
         6, 6, 6, 7, 7, 7, 8, 9, 9, 9,12,10,11,10,10,12,
        10,10,
};

static const static_codebook _huff_book_line_2048x27_4sub2 = {
        1, 50,
        (long *)_huff_lengthlist_line_2048x27_4sub2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_2048x27_4sub3[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 3, 6, 5, 7, 5, 7, 7, 7, 7, 7, 5, 7, 5, 7,
         5, 7, 5, 7, 7, 7, 7, 7, 4, 7, 7, 7, 7, 7, 7, 7,
         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
         7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6,
};

static const static_codebook _huff_book_line_2048x27_4sub3 = {
        1, 128,
        (long *)_huff_lengthlist_line_2048x27_4sub3,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_256x4low_class0[] = {
         4, 5, 6,11, 5, 5, 6,10, 7, 7, 6, 6,14,13, 9, 9,
         6, 6, 6,10, 6, 6, 6, 9, 8, 7, 7, 9,14,12, 8,11,
         8, 7, 7,11, 8, 8, 7,11, 9, 9, 7, 9,13,11, 9,13,
        19,19,18,19,15,16,16,19,11,11,10,13,10,10, 9,15,
         5, 5, 6,13, 6, 6, 6,11, 8, 7, 6, 7,14,11,10,11,
         6, 6, 6,12, 7, 6, 6,11, 8, 7, 7,11,13,11, 9,11,
         9, 7, 6,12, 8, 7, 6,12, 9, 8, 8,11,13,10, 7,13,
        19,19,17,19,17,14,14,19,12,10, 8,12,13,10, 9,16,
         7, 8, 7,12, 7, 7, 7,11, 8, 7, 7, 8,12,12,11,11,
         8, 8, 7,12, 8, 7, 6,11, 8, 7, 7,10,10,11,10,11,
         9, 8, 8,13, 9, 8, 7,12,10, 9, 7,11, 9, 8, 7,11,
        18,18,15,18,18,16,17,18,15,11,10,18,11, 9, 9,18,
        16,16,13,16,12,11,10,16,12,11, 9, 6,15,12,11,13,
        16,16,14,14,13,11,12,16,12, 9, 9,13,13,10,10,12,
        17,18,17,17,14,15,14,16,14,12,14,15,12,10,11,12,
        18,18,18,18,18,18,18,18,18,12,13,18,16,11, 9,18,
};

static const static_codebook _huff_book_line_256x4low_class0 = {
        1, 256,
        (long *)_huff_lengthlist_line_256x4low_class0,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_256x4low_0sub0[] = {
         1, 3, 2, 3,
};

static const static_codebook _huff_book_line_256x4low_0sub0 = {
        1, 4,
        (long *)_huff_lengthlist_line_256x4low_0sub0,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_256x4low_0sub1[] = {
         0, 0, 0, 0, 2, 3, 2, 3, 3, 3,
};

static const static_codebook _huff_book_line_256x4low_0sub1 = {
        1, 10,
        (long *)_huff_lengthlist_line_256x4low_0sub1,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_256x4low_0sub2[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 4, 3, 4,
         4, 4, 4, 4, 5, 5, 5, 6, 6,
};

static const static_codebook _huff_book_line_256x4low_0sub2 = {
        1, 25,
        (long *)_huff_lengthlist_line_256x4low_0sub2,
        0, 0, 0, 0, 0,
        NULL,
        0
};

static const long _huff_lengthlist_line_256x4low_0sub3[] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 2, 4, 3, 5, 4,
         5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 8, 6, 9,
         7,12,11,16,13,16,12,15,13,15,12,14,12,15,15,15,
};

static const static_codebook _huff_book_line_256x4low_0sub3 = {
        1, 64,
        (long *)_huff_lengthlist_line_256x4low_0sub3,
        0, 0, 0, 0, 0,
        NULL,
        0
};


code:
/********************************************************************
 *                                                                  *
 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
 *                                                                  *
 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009             *
 * by the Xiph.Org Foundation http://www.xiph.org/                  *
 *                                                                  *
 ********************************************************************

 function: basic codebook pack/unpack/code/decode operations
 last mod: $Id: codebook.c 17553 2010-10-21 17:54:26Z tterribe $

 ********************************************************************/

#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ogg/ogg.h>
#include "vorbis/codec.h"
#include "codebook.h"
#include "scales.h"
#include "misc.h"
#include "os.h"

/* packs the given codebook into the bitstream **************************/

int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *opb){
  long i,j;
  int ordered=0;

  /* first the basic parameters */
  oggpack_write(opb,0x564342,24);
  oggpack_write(opb,c->dim,16);
  oggpack_write(opb,c->entries,24);

  /* pack the codewords.  There are two packings; length ordered and
     length random.  Decide between the two now. */

  for(i=1;i<c->entries;i++)
    if(c->lengthlist[i-1]==0 || c->lengthlist[i]<c->lengthlist[i-1])break;
  if(i==c->entries)ordered=1;

  if(ordered){
    /* length ordered.  We only need to say how many codewords of
       each length.  The actual codewords are generated
       deterministically */

    long count=0;
    oggpack_write(opb,1,1);  /* ordered */
    oggpack_write(opb,c->lengthlist[0]-1,5); /* 1 to 32 */

    for(i=1;i<c->entries;i++){
      long this=c->lengthlist[i];
      long last=c->lengthlist[i-1];
      if(this>last){
        for(j=last;j<this;j++){
          oggpack_write(opb,i-count,_ilog(c->entries-count));
          count=i;
        }
      }
    }
    oggpack_write(opb,i-count,_ilog(c->entries-count));

  }else{
    /* length random.  Again, we don't code the codeword itself, just
       the length.  This time, though, we have to encode each length */
    oggpack_write(opb,0,1);   /* unordered */

    /* algortihmic mapping has use for 'unused entries', which we tag
       here.  The algorithmic mapping happens as usual, but the unused
       entry has no codeword. */
    for(i=0;i<c->entries;i++)
      if(c->lengthlist[i]==0)break;

    if(i==c->entries){
      oggpack_write(opb,0,1); /* no unused entries */
      for(i=0;i<c->entries;i++)
        oggpack_write(opb,c->lengthlist[i]-1,5);
    }else{
      oggpack_write(opb,1,1); /* we have unused entries; thus we tag */
      for(i=0;i<c->entries;i++){
        if(c->lengthlist[i]==0){
          oggpack_write(opb,0,1);
        }else{
          oggpack_write(opb,1,1);
          oggpack_write(opb,c->lengthlist[i]-1,5);
        }
      }
    }
  }

  /* is the entry number the desired return value, or do we have a
     mapping? If we have a mapping, what type? */
  oggpack_write(opb,c->maptype,4);
  switch(c->maptype){
  case 0:
    /* no mapping */
    break;
  case 1:case 2:
    /* implicitly populated value mapping */
    /* explicitly populated value mapping */

    if(!c->quantlist){
      /* no quantlist?  error */
      return(-1);
    }

    /* values that define the dequantization */
    oggpack_write(opb,c->q_min,32);
    oggpack_write(opb,c->q_delta,32);
    oggpack_write(opb,c->q_quant-1,4);
    oggpack_write(opb,c->q_sequencep,1);

    {
      int quantvals;
      switch(c->maptype){
      case 1:
        /* a single column of (c->entries/c->dim) quantized values for
           building a full value list algorithmically (square lattice) */
        quantvals=_book_maptype1_quantvals(c);
        break;
      case 2:
        /* every value (c->entries*c->dim total) specified explicitly */
        quantvals=c->entries*c->dim;
        break;
      default: /* NOT_REACHABLE */
        quantvals=-1;
      }

      /* quantized values */
      for(i=0;i<quantvals;i++)
        oggpack_write(opb,labs(c->quantlist[i]),c->q_quant);

    }
    break;
  default:
    /* error case; we don't have any other map types now */
    return(-1);
  }

  return(0);
}

/* unpacks a codebook from the packet buffer into the codebook struct,
   readies the codebook auxiliary structures for decode *************/
static_codebook *vorbis_staticbook_unpack(oggpack_buffer *opb){
  long i,j;
  static_codebook *s=_ogg_calloc(1,sizeof(*s));
  s->allocedp=1;

  /* make sure alignment is correct */
  if(oggpack_read(opb,24)!=0x564342)goto _eofout;

  /* first the basic parameters */
  s->dim=oggpack_read(opb,16);
  s->entries=oggpack_read(opb,24);
  if(s->entries==-1)goto _eofout;

  if(_ilog(s->dim)+_ilog(s->entries)>24)goto _eofout;

  /* codeword ordering.... length ordered or unordered? */
  switch((int)oggpack_read(opb,1)){
  case 0:{
    long unused;
    /* allocated but unused entries? */
    unused=oggpack_read(opb,1);
    if((s->entries*(unused?1:5)+7)>>3>opb->storage-oggpack_bytes(opb))
      goto _eofout;
    /* unordered */
    s->lengthlist=_ogg_malloc(sizeof(*s->lengthlist)*s->entries);

    /* allocated but unused entries? */
    if(unused){
      /* yes, unused entries */

      for(i=0;i<s->entries;i++){
        if(oggpack_read(opb,1)){
          long num=oggpack_read(opb,5);
          if(num==-1)goto _eofout;
          s->lengthlist[i]=num+1;
        }else
          s->lengthlist[i]=0;
      }
    }else{
      /* all entries used; no tagging */
      for(i=0;i<s->entries;i++){
        long num=oggpack_read(opb,5);
        if(num==-1)goto _eofout;
        s->lengthlist[i]=num+1;
      }
    }

    break;
  }
  case 1:
    /* ordered */
    {
      long length=oggpack_read(opb,5)+1;
      if(length==0)goto _eofout;
      s->lengthlist=_ogg_malloc(sizeof(*s->lengthlist)*s->entries);

      for(i=0;i<s->entries;){
        long num=oggpack_read(opb,_ilog(s->entries-i));
        if(num==-1)goto _eofout;
        if(length>32 || num>s->entries-i ||
           (num>0 && (num-1)>>(length-1)>1)){
          goto _errout;
        }
        if(length>32)goto _errout;
        for(j=0;j<num;j++,i++)
          s->lengthlist[i]=length;
        length++;
      }
    }
    break;
  default:
    /* EOF */
    goto _eofout;
  }

  /* Do we have a mapping to unpack? */
  switch((s->maptype=oggpack_read(opb,4))){
  case 0:
    /* no mapping */
    break;
  case 1: case 2:
    /* implicitly populated value mapping */
    /* explicitly populated value mapping */

    s->q_min=oggpack_read(opb,32);
    s->q_delta=oggpack_read(opb,32);
    s->q_quant=oggpack_read(opb,4)+1;
    s->q_sequencep=oggpack_read(opb,1);
    if(s->q_sequencep==-1)goto _eofout;

    {
      int quantvals=0;
      switch(s->maptype){
      case 1:
        quantvals=(s->dim==0?0:_book_maptype1_quantvals(s));
        break;
      case 2:
        quantvals=s->entries*s->dim;
        break;
      }

      /* quantized values */
      if((quantvals*s->q_quant+7>>3)>opb->storage-oggpack_bytes(opb))
        goto _eofout;
      s->quantlist=_ogg_malloc(sizeof(*s->quantlist)*quantvals);
      for(i=0;i<quantvals;i++)
        s->quantlist[i]=oggpack_read(opb,s->q_quant);

      if(quantvals&&s->quantlist[quantvals-1]==-1)goto _eofout;
    }
    break;
  default:
    goto _errout;
  }

  /* all set */
  return(s);

 _errout:
 _eofout:
  vorbis_staticbook_destroy(s);
  return(NULL);
}

/* returns the number of bits ************************************************/
int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b){
  if(a<0 || a>=book->c->entries)return(0);
  oggpack_write(b,book->codelist[a],book->c->lengthlist[a]);
  return(book->c->lengthlist[a]);
}

/* the 'eliminate the decode tree' optimization actually requires the
   codewords to be MSb first, not LSb.  This is an annoying inelegancy
   (and one of the first places where carefully thought out design
   turned out to be wrong; Vorbis II and future Ogg codecs should go
   to an MSb bitpacker), but not actually the huge hit it appears to
   be.  The first-stage decode table catches most words so that
   bitreverse is not in the main execution path. */

static ogg_uint32_t bitreverse(ogg_uint32_t x){
  x=    ((x>>16)&0x0000ffff) | ((x<<16)&0xffff0000);
  x=    ((x>> 8)&0x00ff00ff) | ((x<< 8)&0xff00ff00);
  x=    ((x>> 4)&0x0f0f0f0f) | ((x<< 4)&0xf0f0f0f0);
  x=    ((x>> 2)&0x33333333) | ((x<< 2)&0xcccccccc);
  return((x>> 1)&0x55555555) | ((x<< 1)&0xaaaaaaaa);
}

STIN long decode_packed_entry_number(codebook *book, oggpack_buffer *b){
  int  read=book->dec_maxlength;
  long lo,hi;
  long lok = oggpack_look(b,book->dec_firsttablen);

  if (lok >= 0) {
    long entry = book->dec_firsttable[lok];
    if(entry&0x80000000UL){
      lo=(entry>>15)&0x7fff;
      hi=book->used_entries-(entry&0x7fff);
    }else{
      oggpack_adv(b, book->dec_codelengths[entry-1]);
      return(entry-1);
    }
  }else{
    lo=0;
    hi=book->used_entries;
  }

  lok = oggpack_look(b, read);

  while(lok<0 && read>1)
    lok = oggpack_look(b, --read);
  if(lok<0)return -1;

  /* bisect search for the codeword in the ordered list */
  {
    ogg_uint32_t testword=bitreverse((ogg_uint32_t)lok);

    while(hi-lo>1){
      long p=(hi-lo)>>1;
      long test=book->codelist[lo+p]>testword;
      lo+=p&(test-1);
      hi-=p&(-test);
      }

    if(book->dec_codelengths[lo]<=read){
      oggpack_adv(b, book->dec_codelengths[lo]);
      return(lo);
    }
  }

  oggpack_adv(b, read);

  return(-1);
}

/* Decode side is specced and easier, because we don't need to find
   matches using different criteria; we simply read and map.  There are
   two things we need to do 'depending':

   We may need to support interleave.  We don't really, but it's
   convenient to do it here rather than rebuild the vector later.

   Cascades may be additive or multiplicitive; this is not inherent in
   the codebook, but set in the code using the codebook.  Like
   interleaving, it's easiest to do it here.
   addmul==0 -> declarative (set the value)
   addmul==1 -> additive
   addmul==2 -> multiplicitive */

/* returns the [original, not compacted] entry number or -1 on eof *********/
long vorbis_book_decode(codebook *book, oggpack_buffer *b){
  if(book->used_entries>0){
    long packed_entry=decode_packed_entry_number(book,b);
    if(packed_entry>=0)
      return(book->dec_index[packed_entry]);
  }

  /* if there's no dec_index, the codebook unpacking isn't collapsed */
  return(-1);
}

/* returns 0 on OK or -1 on eof *************************************/
long vorbis_book_decodevs_add(codebook *book,float *a,oggpack_buffer *b,int n){
  if(book->used_entries>0){
    int step=n/book->dim;
    long *entry = alloca(sizeof(*entry)*step);
    float **t = alloca(sizeof(*t)*step);
    int i,j,o;

    for (i = 0; i < step; i++) {
      entry[i]=decode_packed_entry_number(book,b);
      if(entry[i]==-1)return(-1);
      t[i] = book->valuelist+entry[i]*book->dim;
    }
    for(i=0,o=0;i<book->dim;i++,o+=step)
      for (j=0;j<step;j++)
        a[o+j]+=t[j][i];
  }
  return(0);
}

long vorbis_book_decodev_add(codebook *book,float *a,oggpack_buffer *b,int n){
  if(book->used_entries>0){
    int i,j,entry;
    float *t;

    if(book->dim>8){
      for(i=0;i<n;){
        entry = decode_packed_entry_number(book,b);
        if(entry==-1)return(-1);
        t     = book->valuelist+entry*book->dim;
        for (j=0;j<book->dim;)
          a[i++]+=t[j++];
      }
    }else{
      for(i=0;i<n;){
        entry = decode_packed_entry_number(book,b);
        if(entry==-1)return(-1);
        t     = book->valuelist+entry*book->dim;
        j=0;
        switch((int)book->dim){
        case 8:
          a[i++]+=t[j++];
        case 7:
          a[i++]+=t[j++];
        case 6:
          a[i++]+=t[j++];
        case 5:
          a[i++]+=t[j++];
        case 4:
          a[i++]+=t[j++];
        case 3:
          a[i++]+=t[j++];
        case 2:
          a[i++]+=t[j++];
        case 1:
          a[i++]+=t[j++];
        case 0:
          break;
        }
      }
    }
  }
  return(0);
}

long vorbis_book_decodev_set(codebook *book,float *a,oggpack_buffer *b,int n){
  if(book->used_entries>0){
    int i,j,entry;
    float *t;

    for(i=0;i<n;){
      entry = decode_packed_entry_number(book,b);
      if(entry==-1)return(-1);
      t     = book->valuelist+entry*book->dim;
      for (j=0;j<book->dim;)
        a[i++]=t[j++];
    }
  }else{
    int i,j;

    for(i=0;i<n;){
      for (j=0;j<book->dim;)
        a[i++]=0.f;
    }
  }
  return(0);
}

long vorbis_book_decodevv_add(codebook *book,float **a,long offset,int ch,
                              oggpack_buffer *b,int n){

  long i,j,entry;
  int chptr=0;
  if(book->used_entries>0){
    for(i=offset/ch;i<(offset+n)/ch;){
      entry = decode_packed_entry_number(book,b);
      if(entry==-1)return(-1);
      {
        const float *t = book->valuelist+entry*book->dim;
        for (j=0;j<book->dim;j++){
          a[chptr++][i]+=t[j];
          if(chptr==ch){
            chptr=0;
            i++;
          }
        }
      }
    }
  }
  return(0);
}

license: mit


code:
/********************************************************************
 *                                                                  *
 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
 *                                                                  *
 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009             *
 * by the Xiph.Org Foundation http://www.xiph.org/                  *
 *                                                                  *
 ********************************************************************

 function: basic shared codebook operations
 last mod: $Id: codebook.h 17030 2010-03-25 06:52:55Z xiphmont $

 ********************************************************************/

#ifndef _V_CODEBOOK_H_
#define _V_CODEBOOK_H_

#include <ogg/ogg.h>

/* This structure encapsulates huffman and VQ style encoding books; it
   doesn't do anything specific to either.

   valuelist/quantlist are nonNULL (and q_* significant) only if
   there's entry->value mapping to be done.

   If encode-side mapping must be done (and thus the entry needs to be
   hunted), the auxiliary encode pointer will point to a decision
   tree.  This is true of both VQ and huffman, but is mostly useful
   with VQ.

*/

typedef struct static_codebook{
  long   dim;            /* codebook dimensions (elements per vector) */
  long   entries;        /* codebook entries */
  long  *lengthlist;     /* codeword lengths in bits */

  /* mapping ***************************************************************/
  int    maptype;        /* 0=none
                            1=implicitly populated values from map column
                            2=listed arbitrary values */

  /* The below does a linear, single monotonic sequence mapping. */
  long     q_min;       /* packed 32 bit float; quant value 0 maps to minval */
  long     q_delta;     /* packed 32 bit float; val 1 - val 0 == delta */
  int      q_quant;     /* bits: 0 < quant <= 16 */
  int      q_sequencep; /* bitflag */

  long     *quantlist;  /* map == 1: (int)(entries^(1/dim)) element column map
                           map == 2: list of dim*entries quantized entry vals
                        */
  int allocedp;
} static_codebook;

typedef struct codebook{
  long dim;           /* codebook dimensions (elements per vector) */
  long entries;       /* codebook entries */
  long used_entries;  /* populated codebook entries */
  const static_codebook *c;

  /* for encode, the below are entry-ordered, fully populated */
  /* for decode, the below are ordered by bitreversed codeword and only
     used entries are populated */
  float        *valuelist;  /* list of dim*entries actual entry values */
  ogg_uint32_t *codelist;   /* list of bitstream codewords for each entry */

  int          *dec_index;  /* only used if sparseness collapsed */
  char         *dec_codelengths;
  ogg_uint32_t *dec_firsttable;
  int           dec_firsttablen;
  int           dec_maxlength;

  /* The current encoder uses only centered, integer-only lattice books. */
  int           quantvals;
  int           minval;
  int           delta;
} codebook;

extern void vorbis_staticbook_destroy(static_codebook *b);
extern int vorbis_book_init_encode(codebook *dest,const static_codebook *source);
extern int vorbis_book_init_decode(codebook *dest,const static_codebook *source);
extern void vorbis_book_clear(codebook *b);

extern float *_book_unquantize(const static_codebook *b,int n,int *map);
extern float *_book_logdist(const static_codebook *b,float *vals);
extern float _float32_unpack(long val);
extern long   _float32_pack(float val);
extern int  _best(codebook *book, float *a, int step);
extern int _ilog(unsigned int v);
extern long _book_maptype1_quantvals(const static_codebook *b);

extern int vorbis_book_besterror(codebook *book,float *a,int step,int addmul);
extern long vorbis_book_codeword(codebook *book,int entry);
extern long vorbis_book_codelen(codebook *book,int entry);

extern int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *b);
extern static_codebook *vorbis_staticbook_unpack(oggpack_buffer *b);

extern int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b);

extern long vorbis_book_decode(codebook *book, oggpack_buffer *b);
extern long vorbis_book_decodevs_add(codebook *book, float *a,
                                     oggpack_buffer *b,int n);
extern long vorbis_book_decodev_set(codebook *book, float *a,
                                    oggpack_buffer *b,int n);
extern long vorbis_book_decodev_add(codebook *book, float *a,
                                    oggpack_buffer *b,int n);
extern long vorbis_book_decodevv_add(codebook *book, float **a,
                                     long off,int ch,
                                    oggpack_buffer *b,int n);

#endif


code:
/********************************************************************
 *                                                                  *
 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
 *                                                                  *
 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
 * by the Xiph.Org Foundation http://www.xiph.org/                  *

 ********************************************************************

 function: libvorbis codec headers
 last mod: $Id: codec.h 17021 2010-03-24 09:29:41Z xiphmont $

 ********************************************************************/

#ifndef _vorbis_codec_h_
#define _vorbis_codec_h_

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */

#include <ogg/ogg.h>

typedef struct vorbis_info{
  int version;
  int channels;
  long rate;

  /* The below bitrate declarations are *hints*.
     Combinations of the three values carry the following implications:

     all three set to the same value:
       implies a fixed rate bitstream
     only nominal set:
       implies a VBR stream that averages the nominal bitrate.  No hard
       upper/lower limit
     upper and or lower set:
       implies a VBR bitstream that obeys the bitrate limits. nominal
       may also be set to give a nominal rate.
     none set:
       the coder does not care to speculate.
  */

  long bitrate_upper;
  long bitrate_nominal;
  long bitrate_lower;
  long bitrate_window;

  void *codec_setup;
} vorbis_info;

/* vorbis_dsp_state buffers the current vorbis audio
   analysis/synthesis state.  The DSP state belongs to a specific
   logical bitstream ****************************************************/
typedef struct vorbis_dsp_state{
  int analysisp;
  vorbis_info *vi;

  float **pcm;
  float **pcmret;
  int      pcm_storage;
  int      pcm_current;
  int      pcm_returned;

  int  preextrapolate;
  int  eofflag;

  long lW;
  long W;
  long nW;
  long centerW;

  ogg_int64_t granulepos;
  ogg_int64_t sequence;

  ogg_int64_t glue_bits;
  ogg_int64_t time_bits;
  ogg_int64_t floor_bits;
  ogg_int64_t res_bits;

  void       *backend_state;
} vorbis_dsp_state;

typedef struct vorbis_block{
  /* necessary stream state for linking to the framing abstraction */
  float  **pcm;       /* this is a pointer into local storage */
  oggpack_buffer opb;

  long  lW;
  long  W;
  long  nW;
  int   pcmend;
  int   mode;

  int         eofflag;
  ogg_int64_t granulepos;
  ogg_int64_t sequence;
  vorbis_dsp_state *vd; /* For read-only access of configuration */

  /* local storage to avoid remallocing; it's up to the mapping to
     structure it */
  void               *localstore;
  long                localtop;
  long                localalloc;
  long                totaluse;
  struct alloc_chain *reap;

  /* bitmetrics for the frame */
  long glue_bits;
  long time_bits;
  long floor_bits;
  long res_bits;

  void *internal;

} vorbis_block;

/* vorbis_block is a single block of data to be processed as part of
the analysis/synthesis stream; it belongs to a specific logical
bitstream, but is independent from other vorbis_blocks belonging to
that logical bitstream. *************************************************/

struct alloc_chain{
  void *ptr;
  struct alloc_chain *next;
};

/* vorbis_info contains all the setup information specific to the
   specific compression/decompression mode in progress (eg,
   psychoacoustic settings, channel setup, options, codebook
   etc). vorbis_info and substructures are in backends.h.
*********************************************************************/

/* the comments are not part of vorbis_info so that vorbis_info can be
   static storage */
typedef struct vorbis_comment{
  /* unlimited user comment fields.  libvorbis writes 'libvorbis'
     whatever vendor is set to in encode */
  char **user_comments;
  int   *comment_lengths;
  int    comments;
  char  *vendor;

} vorbis_comment;

/* libvorbis encodes in two abstraction layers; first we perform DSP
   and produce a packet (see docs/analysis.txt).  The packet is then
   coded into a framed OggSquish bitstream by the second layer (see
   docs/framing.txt).  Decode is the reverse process; we sync/frame
   the bitstream and extract individual packets, then decode the
   packet back into PCM audio.

   The extra framing/packetizing is used in streaming formats, such as
   files.  Over the net (such as with UDP), the framing and
   packetization aren't necessary as they're provided by the transport
   and the streaming layer is not used */

/* Vorbis PRIMITIVES: general ***************************************/

extern void     vorbis_info_init(vorbis_info *vi);
extern void     vorbis_info_clear(vorbis_info *vi);
extern int      vorbis_info_blocksize(vorbis_info *vi,int zo);
extern void     vorbis_comment_init(vorbis_comment *vc);
extern void     vorbis_comment_add(vorbis_comment *vc, const char *comment);
extern void     vorbis_comment_add_tag(vorbis_comment *vc,
                                       const char *tag, const char *contents);
extern char    *vorbis_comment_query(vorbis_comment *vc, const char *tag, int count);
extern int      vorbis_comment_query_count(vorbis_comment *vc, const char *tag);
extern void     vorbis_comment_clear(vorbis_comment *vc);

extern int      vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb);
extern int      vorbis_block_clear(vorbis_block *vb);
extern void     vorbis_dsp_clear(vorbis_dsp_state *v);
extern double   vorbis_granule_time(vorbis_dsp_state *v,
                                    ogg_int64_t granulepos);

extern const char *vorbis_version_string(void);

/* Vorbis PRIMITIVES: analysis/DSP layer ****************************/

extern int      vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi);
extern int      vorbis_commentheader_out(vorbis_comment *vc, ogg_packet *op);
extern int      vorbis_analysis_headerout(vorbis_dsp_state *v,
                                          vorbis_comment *vc,
                                          ogg_packet *op,
                                          ogg_packet *op_comm,
                                          ogg_packet *op_code);
extern float  **vorbis_analysis_buffer(vorbis_dsp_state *v,int vals);
extern int      vorbis_analysis_wrote(vorbis_dsp_state *v,int vals);
extern int      vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb);
extern int      vorbis_analysis(vorbis_block *vb,ogg_packet *op);

extern int      vorbis_bitrate_addblock(vorbis_block *vb);
extern int      vorbis_bitrate_flushpacket(vorbis_dsp_state *vd,
                                           ogg_packet *op);

/* Vorbis PRIMITIVES: synthesis layer *******************************/
extern int      vorbis_synthesis_idheader(ogg_packet *op);
extern int      vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,
                                          ogg_packet *op);

extern int      vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi);
extern int      vorbis_synthesis_restart(vorbis_dsp_state *v);
extern int      vorbis_synthesis(vorbis_block *vb,ogg_packet *op);
extern int      vorbis_synthesis_trackonly(vorbis_block *vb,ogg_packet *op);
extern int      vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb);
extern int      vorbis_synthesis_pcmout(vorbis_dsp_state *v,float ***pcm);
extern int      vorbis_synthesis_lapout(vorbis_dsp_state *v,float ***pcm);
extern int      vorbis_synthesis_read(vorbis_dsp_state *v,int samples);
extern long     vorbis_packet_blocksize(vorbis_info *vi,ogg_packet *op);

extern int      vorbis_synthesis_halfrate(vorbis_info *v,int flag);
extern int      vorbis_synthesis_halfrate_p(vorbis_info *v);

/* Vorbis ERRORS and return codes ***********************************/

#define OV_FALSE      -1
#define OV_EOF        -2
#define OV_HOLE       -3

#define OV_EREAD      -128
#define OV_EFAULT     -129
#define OV_EIMPL      -130
#define OV_EINVAL     -131
#define OV_ENOTVORBIS -132
#define OV_EBADHEADER -133
#define OV_EVERSION   -134
#define OV_ENOTAUDIO  -135
#define OV_EBADPACKET -136
#define OV_EBADLINK   -137
#define OV_ENOSEEK    -138

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif


code:
/********************************************************************
 *                                                                  *
 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
 *                                                                  *
 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009             *
 * by the Xiph.Org Foundation http://www.xiph.org/                  *
 *                                                                  *
 ********************************************************************

 function: libvorbis codec headers
 last mod: $Id: codec_internal.h 16227 2009-07-08 06:58:46Z xiphmont $

 ********************************************************************/

#ifndef _V_CODECI_H_
#define _V_CODECI_H_

#include "envelope.h"
#include "codebook.h"

#define BLOCKTYPE_IMPULSE    0
#define BLOCKTYPE_PADDING    1
#define BLOCKTYPE_TRANSITION 0
#define BLOCKTYPE_LONG       1

#define PACKETBLOBS 15

typedef struct vorbis_block_internal{
  float  **pcmdelay;  /* this is a pointer into local storage */
  float  ampmax;
  int    blocktype;

  oggpack_buffer *packetblob[PACKETBLOBS]; /* initialized, must be freed;
                                              blob [PACKETBLOBS/2] points to
                                              the oggpack_buffer in the
                                              main vorbis_block */
} vorbis_block_internal;

typedef void vorbis_look_floor;
typedef void vorbis_look_residue;
typedef void vorbis_look_transform;

/* mode ************************************************************/
typedef struct {
  int blockflag;
  int windowtype;
  int transformtype;
  int mapping;
} vorbis_info_mode;

typedef void vorbis_info_floor;
typedef void vorbis_info_residue;
typedef void vorbis_info_mapping;

#include "psy.h"
#include "bitrate.h"

typedef struct private_state {
  /* local lookup storage */
  envelope_lookup        *ve; /* envelope lookup */
  int                     window[2];
  vorbis_look_transform **transform[2];    /* block, type */
  drft_lookup             fft_look[2];

  int                     modebits;
  vorbis_look_floor     **flr;
  vorbis_look_residue   **residue;
  vorbis_look_psy        *psy;
  vorbis_look_psy_global *psy_g_look;

  /* local storage, only used on the encoding side.  This way the
     application does not need to worry about freeing some packets'
     memory and not others'; packet storage is always tracked.
     Cleared next call to a _dsp_ function */
  unsigned char *header;
  unsigned char *header1;
  unsigned char *header2;

  bitrate_manager_state bms;

  ogg_int64_t sample_count;
} private_state;

/* codec_setup_info contains all the setup information specific to the
   specific compression/decompression mode in progress (eg,
   psychoacoustic settings, channel setup, options, codebook
   etc).
*********************************************************************/

#include "highlevel.h"
typedef struct codec_setup_info {

  /* Vorbis supports only short and long blocks, but allows the
     encoder to choose the sizes */

  long blocksizes[2];

  /* modes are the primary means of supporting on-the-fly different
     blocksizes, different channel mappings (LR or M/A),
     different residue backends, etc.  Each mode consists of a
     blocksize flag and a mapping (along with the mapping setup */

  int        modes;
  int        maps;
  int        floors;
  int        residues;
  int        books;
  int        psys;     /* encode only */

  vorbis_info_mode       *mode_param[64];
  int                     map_type[64];
  vorbis_info_mapping    *map_param[64];
  int                     floor_type[64];
  vorbis_info_floor      *floor_param[64];
  int                     residue_type[64];
  vorbis_info_residue    *residue_param[64];
  static_codebook        *book_param[256];
  codebook               *fullbooks;

  vorbis_info_psy        *psy_param[4]; /* encode only */
  vorbis_info_psy_global psy_g_param;

  bitrate_manager_info   bi;
  highlevel_encode_setup hi; /* used only by vorbisenc.c.  It's a
                                highly redundant structure, but
                                improves clarity of program flow. */
  int         halfrate_flag; /* painless downsample for decode */
} codec_setup_info;

extern vorbis_look_psy_global *_vp_global_look(vorbis_info *vi);
extern void _vp_global_free(vorbis_look_psy_global *look);

typedef struct {
  int sorted_index[VIF_POSIT+2];
  int forward_index[VIF_POSIT+2];
  int reverse_index[VIF_POSIT+2];

  int hineighbor[VIF_POSIT];
  int loneighbor[VIF_POSIT];
  int posts;

  int n;
  int quant_q;
  vorbis_info_floor1 *vi;

  long phrasebits;
  long postbits;
  long frames;
} vorbis_look_floor1;

extern int *floor1_fit(vorbis_block *vb,vorbis_look_floor1 *look,
                          const float *logmdct,   /* in */
                          const float *logmask);
extern int *floor1_interpolate_fit(vorbis_block *vb,vorbis_look_floor1 *look,
                          int *A,int *B,
                          int del);
extern int floor1_encode(oggpack_buffer *opb,vorbis_block *vb,
                  vorbis_look_floor1 *look,
                  int *post,int *ilogmask);
#endif


code:
/********************************************************************
 *                                                                  *
 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
 *                                                                  *
 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009             *
 * by the Xiph.Org Foundation http://www.xiph.org/                  *
 *                                                                  *
 ********************************************************************

 function: PCM data envelope analysis
 last mod: $Id: envelope.c 16227 2009-07-08 06:58:46Z xiphmont $

 ********************************************************************/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <ogg/ogg.h>
#include "vorbis/codec.h"
#include "codec_internal.h"

#include "os.h"
#include "scales.h"
#include "envelope.h"
#include "mdct.h"
#include "misc.h"

void _ve_envelope_init(envelope_lookup *e,vorbis_info *vi){
  codec_setup_info *ci=vi->codec_setup;
  vorbis_info_psy_global *gi=&ci->psy_g_param;
  int ch=vi->channels;
  int i,j;
  int n=e->winlength=128;
  e->searchstep=64; /* not random */

  e->minenergy=gi->preecho_minenergy;
  e->ch=ch;
  e->storage=128;
  e->cursor=ci->blocksizes[1]/2;
  e->mdct_win=_ogg_calloc(n,sizeof(*e->mdct_win));
  mdct_init(&e->mdct,n);

  for(i=0;i<n;i++){
    e->mdct_win[i]=sin(i/(n-1.)*M_PI);
    e->mdct_win[i]*=e->mdct_win[i];
  }

  /* magic follows */
  e->band[0].begin=2;  e->band[0].end=4;
  e->band[1].begin=4;  e->band[1].end=5;
  e->band[2].begin=6;  e->band[2].end=6;
  e->band[3].begin=9;  e->band[3].end=8;
  e->band[4].begin=13;  e->band[4].end=8;
  e->band[5].begin=17;  e->band[5].end=8;
  e->band[6].begin=22;  e->band[6].end=8;

  for(j=0;j<VE_BANDS;j++){
    n=e->band[j].end;
    e->band[j].window=_ogg_malloc(n*sizeof(*e->band[0].window));
    for(i=0;i<n;i++){
      e->band[j].window[i]=sin((i+.5)/n*M_PI);
      e->band[j].total+=e->band[j].window[i];
    }
    e->band[j].total=1./e->band[j].total;
  }

  e->filter=_ogg_calloc(VE_BANDS*ch,sizeof(*e->filter));
  e->mark=_ogg_calloc(e->storage,sizeof(*e->mark));

}

void _ve_envelope_clear(envelope_lookup *e){
  int i;
  mdct_clear(&e->mdct);
  for(i=0;i<VE_BANDS;i++)
    _ogg_free(e->band[i].window);
  _ogg_free(e->mdct_win);
  _ogg_free(e->filter);
  _ogg_free(e->mark);
  memset(e,0,sizeof(*e));
}

/* fairly straight threshhold-by-band based until we find something
   that works better and isn't patented. */

static int _ve_amp(envelope_lookup *ve,
                   vorbis_info_psy_global *gi,
                   float *data,
                   envelope_band *bands,
                   envelope_filter_state *filters){
  long n=ve->winlength;
  int ret=0;
  long i,j;
  float decay;

  /* we want to have a 'minimum bar' for energy, else we're just
     basing blocks on quantization noise that outweighs the signal
     itself (for low power signals) */

  float minV=ve->minenergy;
  float *vec=alloca(n*sizeof(*vec));

  /* stretch is used to gradually lengthen the number of windows
     considered prevoius-to-potential-trigger */
  int stretch=max(VE_MINSTRETCH,ve->stretch/2);
  float penalty=gi->stretch_penalty-(ve->stretch/2-VE_MINSTRETCH);
  if(penalty<0.f)penalty=0.f;
  if(penalty>gi->stretch_penalty)penalty=gi->stretch_penalty;

  /*_analysis_output_always("lpcm",seq2,data,n,0,0,
    totalshift+pos*ve->searchstep);*/

 /* window and transform */
  for(i=0;i<n;i++)
    vec[i]=data[i]*ve->mdct_win[i];
  mdct_forward(&ve->mdct,vec,vec);

  /*_analysis_output_always("mdct",seq2,vec,n/2,0,1,0); */

  /* near-DC spreading function; this has nothing to do with
     psychoacoustics, just sidelobe leakage and window size */
  {
    float temp=vec[0]*vec[0]+.7*vec[1]*vec[1]+.2*vec[2]*vec[2];
    int ptr=filters->nearptr;

    /* the accumulation is regularly refreshed from scratch to avoid
       floating point creep */
    if(ptr==0){
      decay=filters->nearDC_acc=filters->nearDC_partialacc+temp;
      filters->nearDC_partialacc=temp;
    }else{
      decay=filters->nearDC_acc+=temp;
      filters->nearDC_partialacc+=temp;
    }
    filters->nearDC_acc-=filters->nearDC[ptr];
    filters->nearDC[ptr]=temp;

    decay*=(1./(VE_NEARDC+1));
    filters->nearptr++;
    if(filters->nearptr>=VE_NEARDC)filters->nearptr=0;
    decay=todB(&decay)*.5-15.f;
  }

  /* perform spreading and limiting, also smooth the spectrum.  yes,
     the MDCT results in all real coefficients, but it still *behaves*
     like real/imaginary pairs */
  for(i=0;i<n/2;i+=2){
    float val=vec[i]*vec[i]+vec[i+1]*vec[i+1];
    val=todB(&val)*.5f;
    if(val<decay)val=decay;
    if(val<minV)val=minV;
    vec[i>>1]=val;
    decay-=8.;
  }

  /*_analysis_output_always("spread",seq2++,vec,n/4,0,0,0);*/

  /* perform preecho/postecho triggering by band */
  for(j=0;j<VE_BANDS;j++){
    float acc=0.;
    float valmax,valmin;

    /* accumulate amplitude */
    for(i=0;i<bands[j].end;i++)
      acc+=vec[i+bands[j].begin]*bands[j].window[i];

    acc*=bands[j].total;

    /* convert amplitude to delta */
    {
      int p,this=filters[j].ampptr;
      float postmax,postmin,premax=-99999.f,premin=99999.f;

      p=this;
      p--;
      if(p<0)p+=VE_AMP;
      postmax=max(acc,filters[j].ampbuf[p]);
      postmin=min(acc,filters[j].ampbuf[p]);

      for(i=0;i<stretch;i++){
        p--;
        if(p<0)p+=VE_AMP;
        premax=max(premax,filters[j].ampbuf[p]);
        premin=min(premin,filters[j].ampbuf[p]);
      }

      valmin=postmin-premin;
      valmax=postmax-premax;

      /*filters[j].markers[pos]=valmax;*/
      filters[j].ampbuf[this]=acc;
      filters[j].ampptr++;
      if(filters[j].ampptr>=VE_AMP)filters[j].ampptr=0;
    }

    /* look at min/max, decide trigger */
    if(valmax>gi->preecho_thresh[j]+penalty){
      ret|=1;
      ret|=4;
    }
    if(valmin<gi->postecho_thresh[j]-penalty)ret|=2;
  }

  return(ret);
}

#if 0
static int seq=0;
static ogg_int64_t totalshift=-1024;
#endif

long _ve_envelope_search(vorbis_dsp_state *v){
  vorbis_info *vi=v->vi;
  codec_setup_info *ci=vi->codec_setup;
  vorbis_info_psy_global *gi=&ci->psy_g_param;
  envelope_lookup *ve=((private_state *)(v->backend_state))->ve;
  long i,j;

  int first=ve->current/ve->searchstep;
  int last=v->pcm_current/ve->searchstep-VE_WIN;
  if(first<0)first=0;

  /* make sure we have enough storage to match the PCM */
  if(last+VE_WIN+VE_POST>ve->storage){
    ve->storage=last+VE_WIN+VE_POST; /* be sure */
    ve->mark=_ogg_realloc(ve->mark,ve->storage*sizeof(*ve->mark));
  }

  for(j=first;j<last;j++){
    int ret=0;

    ve->stretch++;
    if(ve->stretch>VE_MAXSTRETCH*2)
      ve->stretch=VE_MAXSTRETCH*2;

    for(i=0;i<ve->ch;i++){
      float *pcm=v->pcm[i]+ve->searchstep*(j);
      ret|=_ve_amp(ve,gi,pcm,ve->band,ve->filter+i*VE_BANDS);
    }

    ve->mark[j+VE_POST]=0;
    if(ret&1){
      ve->mark[j]=1;
      ve->mark[j+1]=1;
    }

    if(ret&2){
      ve->mark[j]=1;
      if(j>0)ve->mark[j-1]=1;
    }

    if(ret&4)ve->stretch=-1;
  }

  ve->current=last*ve->searchstep;

  {
    long centerW=v->centerW;
    long testW=
      centerW+
      ci->blocksizes[v->W]/4+
      ci->blocksizes[1]/2+
      ci->blocksizes[0]/4;

    j=ve->cursor;

    while(j<ve->current-(ve->searchstep)){/* account for postecho
                                             working back one window */
      if(j>=testW)return(1);

      ve->cursor=j;

      if(ve->mark[j/ve->searchstep]){
        if(j>centerW){

#if 0
          if(j>ve->curmark){
            float *marker=alloca(v->pcm_current*sizeof(*marker));
            int l,m;
            memset(marker,0,sizeof(*marker)*v->pcm_current);
            fprintf(stderr,"mark! seq=%d, cursor:%fs time:%fs\n",
                    seq,
                    (totalshift+ve->cursor)/44100.,
                    (totalshift+j)/44100.);
            _analysis_output_always("pcmL",seq,v->pcm[0],v->pcm_current,0,0,totalshift);
            _analysis_output_always("pcmR",seq,v->pcm[1],v->pcm_current,0,0,totalshift);

            _analysis_output_always("markL",seq,v->pcm[0],j,0,0,totalshift);
            _analysis_output_always("markR",seq,v->pcm[1],j,0,0,totalshift);

            for(m=0;m<VE_BANDS;m++){
              char buf[80];
              sprintf(buf,"delL%d",m);
              for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[m].markers[l]*.1;
              _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift);
            }

            for(m=0;m<VE_BANDS;m++){
              char buf[80];
              sprintf(buf,"delR%d",m);
              for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[m+VE_BANDS].markers[l]*.1;
              _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift);
            }

            for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->mark[l]*.4;
            _analysis_output_always("mark",seq,marker,v->pcm_current,0,0,totalshift);

            seq++;

          }
#endif

          ve->curmark=j;
          if(j>=testW)return(1);
          return(0);
        }
      }
      j+=ve->searchstep;
    }
  }

  return(-1);
}

int _ve_envelope_mark(vorbis_dsp_state *v){
  envelope_lookup *ve=((private_state *)(v->backend_state))->ve;
  vorbis_info *vi=v->vi;
  codec_setup_info *ci=vi->codec_setup;
  long centerW=v->centerW;
  long beginW=centerW-ci->blocksizes[v->W]/4;
  long endW=centerW+ci->blocksizes[v->W]/4;
  if(v->W){
    beginW-=ci->blocksizes[v->lW]/4;
    endW+=ci->blocksizes[v->nW]/4;
  }else{
    beginW-=ci->blocksizes[0]/4;
    endW+=ci->blocksizes[0]/4;
  }

  if(ve->curmark>=beginW && ve->curmark<endW)return(1);
  {
    long first=beginW/ve->searchstep;
    long last=endW/ve->searchstep;
    long i;
    for(i=first;i<last;i++)
      if(ve->mark[i])return(1);
  }
  return(0);
}

void _ve_envelope_shift(envelope_lookup *e,long shift){
  int smallsize=e->current/e->searchstep+VE_POST; /* adjust for placing marks
                                                     ahead of ve->current */
  int smallshift=shift/e->searchstep;

  memmove(e->mark,e->mark+smallshift,(smallsize-smallshift)*sizeof(*e->mark));

#if 0
  for(i=0;i<VE_BANDS*e->ch;i++)
    memmove(e->filter[i].markers,
            e->filter[i].markers+smallshift,
            (1024-smallshift)*sizeof(*(*e->filter).markers));
  totalshift+=shift;
#endif

  e->current-=shift;
  if(e->curmark>=0)
    e->curmark-=shift;
  e->cursor-=shift;
}


code:
/********************************************************************
 *                                                                  *
 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
 *                                                                  *
 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009             *
 * by the Xiph.Org Foundation http://www.xiph.org/                  *
 *                                                                  *
 ********************************************************************

 function: PCM data envelope analysis and manipulation
 last mod: $Id: envelope.h 16227 2009-07-08 06:58:46Z xiphmont $

 ********************************************************************/

#ifndef _V_ENVELOPE_
#define _V_ENVELOPE_

#include "mdct.h"

#define VE_PRE    16
#define VE_WIN    4
#define VE_POST   2
#define VE_AMP    (VE_PRE+VE_POST-1)

#define VE_BANDS  7
#define VE_NEARDC 15

#define VE_MINSTRETCH 2   /* a bit less than short block */
#define VE_MAXSTRETCH 12  /* one-third full block */

typedef struct {
  float ampbuf[VE_AMP];
  int   ampptr;

  float nearDC[VE_NEARDC];
  float nearDC_acc;
  float nearDC_partialacc;
  int   nearptr;

} envelope_filter_state;

typedef struct {
  int begin;
  int end;
  float *window;
  float total;
} envelope_band;

typedef struct {
  int ch;
  int winlength;
  int searchstep;
  float minenergy;

  mdct_lookup  mdct;
  float       *mdct_win;

  envelope_band          band[VE_BANDS];
  envelope_filter_state *filter;
  int   stretch;

  int                   *mark;

  long storage;
  long current;
  long curmark;
  long cursor;
} envelope_lookup;

extern void _ve_envelope_init(envelope_lookup *e,vorbis_info *vi);
extern void _ve_envelope_clear(envelope_lookup *e);
extern long _ve_envelope_search(vorbis_dsp_state *v);
extern void _ve_envelope_shift(envelope_lookup *e,long shift);
extern int  _ve_envelope_mark(vorbis_dsp_state *v);

#endif