/*
 * Copyright (c) 1997-2005 Alexandros Eleftheriadis and Danny Hong.
 * 
 * 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>
 *
 */


/* 
 * The binary arithmetic coder described in 
 * @MANUAL{
 * TITLE        = {Advanced video coding for generic audiovisual services},
 * ORGANIZATION = {ITU-T Recommendation (H.264)},
 * YEAR         = {2003},
 *
 */


const int rTblM[64][4]={
    { 128, 176, 208, 240},
    { 128, 167, 197, 227},  
    { 128, 158, 187, 216},
    { 123, 150, 178, 205},
    { 116, 142, 169, 195},
    { 111, 135, 160, 185},
    { 105, 128, 152, 175},
    { 100, 122, 144, 166},
    {  95, 116, 137, 158},
    {  90, 110, 130, 150},
    {  85, 104, 123, 142},
    {  81,  99, 117, 135},
    {  77,  94, 111, 128},
    {  73,  89, 105, 122},
    {  69,  85, 100, 116},
    {  66,  80,  95, 110},
    {  62,  76,  90, 104},
    {  59,  72,  86,  99},
    {  56,  69,  81,  94},
    {  53,  65,  77,  89},
    {  51,  62,  73,  85},
    {  48,  59,  69,  80},
    {  46,  56,  66,  76},
    {  43,  53,  63,  72},
    {  41,  50,  59,  69},
    {  39,  48,  56,  65},
    {  37,  45,  54,  62},
    {  35,  43,  51,  59},
    {  33,  41,  48,  56},
    {  32,  39,  46,  53},
    {  30,  37,  43,  50},
    {  29,  35,  41,  48},
    {  27,  33,  39,  45},
    {  26,  31,  37,  43},
    {  24,  30,  35,  41},
    {  23,  28,  33,  39},
    {  22,  27,  32,  37},
    {  21,  26,  30,  35},
    {  20,  24,  29,  33},
    {  19,  23,  27,  31},
    {  18,  22,  26,  30},
    {  17,  21,  25,  28},
    {  16,  20,  23,  27},
    {  15,  19,  22,  25},
    {  14,  18,  21,  24},
    {  14,  17,  20,  23},
    {  13,  16,  19,  22},
    {  12,  15,  18,  21},
    {  12,  14,  17,  20},
    {  11,  14,  16,  19},
    {  11,  13,  15,  18},
    {  10,  12,  15,  17},
    {  10,  12,  14,  16},
    {   9,  11,  13,  15},
    {   9,  11,  12,  14},
    {   8,  10,  12,  14},
    {   8,   9,  11,  13},
    {   7,   9,  11,  12},
    {   7,   9,  10,  12},
    {   7,   8,  10,  11},
    {   6,   8,   9,  11},
    {   6,   7,   9,  10},
    {   6,   7,   8,   9},
    {   2,   2,   2,   2}
};
const int iTblM[64][2]={ 
     {0, 1},  {0, 2},  {1, 3},  {2, 4},  {2, 5},  {4, 6},  {4, 7},  {5, 8},  {6, 9},  {7,10}, 
     {8,11},  {9,12},  {9,13}, {11,14}, {11,15}, {12,16}, {13,17}, {13,18}, {15,19}, {15,20},  
    {16,21}, {16,22}, {18,23}, {18,24}, {19,25}, {19,26}, {21,27}, {21,28}, {22,29}, {22,30}, 
    {23,31}, {24,32}, {24,33}, {25,34}, {26,35}, {26,36}, {27,37}, {27,38}, {28,39}, {29,40}, 
    {29,41}, {30,42}, {30,43}, {30,44}, {31,45}, {32,46}, {32,47}, {33,48}, {33,49}, {33,50}, 
    {34,51}, {34,52}, {35,53}, {35,54}, {35,55}, {36,56}, {36,57}, {36,58}, {37,59}, {37,60},  
    {37,61}, {38,62}, {38,62}, {63,63} 
};

bac MCoder { 
    "prec",     10,             // 10-bit precision	 
    "ooc",      2,              // No multiplicative operation --> fast BAC
    "init",     {(1<<9)-2,9},   // Init: R=(1<<9)-2; read 9 code stream bits for decoding 
    "end",      {3,1},          // End: Output 3 bits; byte align with 0 bits
    "rtable",   {64,4,"rTblM"}, // The rTbl table contains possible R values for the LPS
    "nexti",    "iTblM"         // The iTbl table contains transition rules for the LPS
}