/*
 * Copyright (c) 1997-2004 Alexandros Eleftheriadis, Danny Hong and
 * Yuntai Kyong.
 * 
 * This file is part of Flavor, developed at Columbia University
 * (www.ee.columbia.edu/flavor).
 *
 * Flavor is free software; you can redistribute it and/or modify
 * it under the terms of the Flavor Artistic License as described in
 * the file COPYING.txt. 
 *
 */

/* 
 * Authors:
 * Danny Hong <danny@ee.columbia.edu>
 * Chun Hao(Tony) Wang <tony@ee.columbia.edu>
 * Haidi Gu
 *
 */

/*
 * MPEG-I Audio Layer I and II predefined constants and tables
 *
 */

// Layers
const unsigned int LAYER1    = 0b11;
const unsigned int LAYER2    = 0b10;
const unsigned int LAYER3    = 0b01;

// Modes
const int SINGLE_CHANNEL	= 0b11;
const int DUAL_CHANNEL      = 0b10;
const int JOINT_STEREO      = 0b01;
const int STEREO            = 0b00;

const int BITRATE[16][3] = {
	{-1, -1, -1},
	{32, 32, 32},
	{64, 48, 40},
	{96, 56, 48},
	{128, 64, 56},
	{160, 80, 64},
	{192, 96, 80},
	{224, 112, 96},
	{256, 128, 112},
	{288, 160, 128},
	{320, 192, 160},
	{352, 224, 192},
	{384, 256, 224},
	{416, 320, 256},
	{448, 384, 320},
	{-1, -1, -1}
};

const int SAMPLING_FREQ[4] = {44100, 48000, 32000, -1};

// Bound look up table for joint stereo (2.4.2.3)
const int BOUND[4] = {4,8,12,16};

// Look up table for Layer I allocation to bits per sample (2.4.2.5)
const int BPS[16] = {0,2,3,4,5,6,7,8,9,10,11,12,13,14,15,-1};

const int CHANNELRATE[11] = {1,0,2,3,4,5,6,7,8,9,10};

// Look up table to determine which table (Table B.2a-d) to use for bit allocation in Layer II 
const int TABLEINDEX[3][11] = {
    {0,2,2,0,0,0,0,0,0,0,0},
    {1,2,2,0,0,0,1,1,1,1,1},
    {1,3,3,0,0,0,1,1,1,1,1}
};

// Table B.2a-d, given the table number, lookup sblimit
const int TSBLIMIT[4] = {27,30,8,12};

// Table B.2a-d, given the table number and sb, lookup nbal	
const int NBAL[4][32] = {
    {4,4,4,4,4,4,4,4,4,4,4,3,3,3,3,3,3,3,3,3,3,3,3,2,2,2,2,0,0,0,0,0},
    {4,4,4,4,4,4,4,4,4,4,4,3,3,3,3,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,0,0},
    {4,4,3,3,3,3,3,3,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,4,3,3,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};

// Table B.2a-d
const int QUANTIZATION[4][32][16] = {
  { {-1,0,2,4,5,6,7,8,9,10,11,12,13,14,15,16},
    {-1,0,2,4,5,6,7,8,9,10,11,12,13,14,15,16},
    {-1,0,2,4,5,6,7,8,9,10,11,12,13,14,15,16},
    {-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,16},
    {-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,16},
    {-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,16},
    {-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,16},
    {-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,16},
    {-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,16},
    {-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,16},
    {-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,16},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} },

  { {-1,0,2,4,5,6,7,8,9,10,11,12,13,14,15,16},
    {-1,0,2,4,5,6,7,8,9,10,11,12,13,14,15,16},
    {-1,0,2,4,5,6,7,8,9,10,11,12,13,14,15,16},
    {-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,16},
    {-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,16},
    {-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,16},
    {-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,16},
    {-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,16},
    {-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,16},
    {-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,16},
    {-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,16},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,2,3,4,5,16,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,0,1,16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} },

  { {-1,0,1,3,4,5,6,7,8,9,10,11,12,13,14,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const int GROUPING[17] = {1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0};
const int CODEBITS[17] = {5,7,3,10,4,5,6,7,8,9,10,11,12,13,14,15,16};
	
// (2.4.2.7)
const int SLEN1[16] = {0,0,0,0,3,1,1,1,2,2,2,3,3,3,4,4};
const int SLEN2[16] = {0,1,2,3,0,1,2,3,1,2,3,1,2,3,2,3};

const unsigned int SF_BANDS_L[3][22] = {
    {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,549},
    {0,4,8,12,16,20,24,30,36,44,52,62,74,90,110,134,162,196,238,288,342,417},
    {0,4,8,12,16,20,24,30,36,42,50,60,72,88,106,128,156,190,230,276,330,383}
};
const unsigned int SF_BANDS_S[3][13] = {
    {0,4,8,12,16,22,30,42,58,78,104,138,179},
    {0,4,8,12,16,22,30,40,52,66,84,106,135},
    {0,4,8,12,16,22,28,38,50,64,80,100,125}
};