/*
 * 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        = {Information technology 
                   -- Coded representation of picture and audio information 
                   -- Progressive bi-level image compression},
 * ORGANIZATION = {SO/IEC 11544 International Standard (JBIG)},
 * YEAR         = {1993},
 *
 */


const int rTblQM[113]={
  0x5A1D, 0x2586, 0x1114, 0x080B, 0x03D8, 0x01dA, 0x00E5, 0x006F, 0x0036, 0x001A, 
  0x000D, 0x0006, 0x0003, 0x0001, 0x5A7F, 0x3F25, 0x2CF2, 0x207C, 0x17B9, 0x1182, 
  0x0CEF, 0x09A1, 0x072F, 0x055C, 0x0406, 0x0303, 0x0240, 0x01B1, 0x0144, 0x00F5,
  0x00B7, 0x008A, 0x0068, 0x004E, 0x003B, 0x002C, 0x5AE1, 0x484C, 0x3A0D, 0x2EF1,
  0x261F, 0x1F33, 0x19A8, 0x1518, 0x1177, 0x0E74, 0x0BFB, 0x09F8, 0x0861, 0x0706, 
  0x05CD, 0x04DE, 0x040F, 0x0363, 0x02D4, 0x025C, 0x01F8, 0x01A4, 0x0160, 0x0125, 
  0x00F6, 0x00CB, 0x00AB, 0x008F, 0x5B12, 0x4D04, 0x412C, 0x37D8, 0x2FE8, 0x293C, 
  0x2379, 0x1EDF, 0x1AA9, 0x174E, 0x1424, 0x119C, 0x0F6B, 0x0D51, 0x0BB6, 0x0A40,
  0x5832, 0x4D1C, 0x438E, 0x3BDD, 0x34EE, 0x2EAE, 0x299A, 0x2516, 0x5570, 0x4CA9, 
  0x44D9, 0x3E22, 0x3824, 0x32B4, 0x2E17, 0x56A8, 0x4F46, 0x47E5, 0x41CF, 0x3C3D, 
  0x375E, 0x5231, 0x4C0F, 0x4639, 0x415E, 0x5627, 0x50E7, 0x4B85, 0x5597, 0x504F, 
  0x5A10, 0x5522, 0x59EB
};
const int iTblQM[113][2]={
   {1,  1},  {14,  2},  {16,  3},  {18,  4},  {20, 5},  {23,  6},  {25,  7},  {28,  8},  {30,  9},  {33, 10},  
  {35, 11},   {9, 12},  {10, 13},  {12, 14},  {15,15},  {36, 16},  {38, 17},  {39, 18},  {40, 19},  {42, 20},
  {43, 21},  {45, 22},  {46, 23},  {48, 24},  {49,25},  {51, 26},  {52, 27},  {54, 28},  {56, 29},  {57, 30},
  {59, 31},  {60, 32},  {62, 33},  {63, 34},  {32,35},  {33,  9},  {37, 37},  {64, 38},  {65, 39},  {67, 40},  
  {68, 41},  {69, 42},  {70, 43},  {72, 44},  {73,45},  {74, 46},  {75, 47},  {77, 48},  {78, 49},  {79, 50},  
  {48, 51},  {50, 52},  {50, 53},  {51, 54},  {52,55},  {53, 56},  {54, 57},  {55, 58},  {56, 59},  {57, 60},
  {58, 61},  {59, 62},  {61, 63},  {61, 32},  {65,65},  {80, 66},  {81, 67},  {82, 68},  {83, 69},  {84, 70},  
  {86, 71},  {87, 72},  {87, 73},  {72, 74},  {72,75},  {74, 76},  {74, 77},  {75, 78},  {77, 79},  {77, 48},  
  {80, 81},  {88, 82},  {89, 83},  {90, 84},  {91,85},  {92, 86},  {93, 87},  {86, 71},  {88, 89},  {95, 90},
  {96, 91},  {97, 92},  {99, 93},  {99, 94},  {93,86},  {95, 96}, {101, 97}, {102, 98}, {103, 99}, {104,100},  
  {99, 93}, {105,102}, {106,103}, {107,104}, {103,99}, {105,106}, {108,107}, {109,103}, {110,109}, {111,107}, 
 {110,111}, {112,109}, {112,111}
};

bac QMCoder { 
    "ooc",      2,                  // No multiplicative operation --> fast BAC
    "norm",     0x8000,             // Renormalize whenever R < 0x8000 
    "init",     0x10000,            // Init: R=0x10000
    "end",      {0,1},              // End: Output minimal bits; byte align with 0 bits
    "rtable",   {113,1,"rTblQM"},   // The rTblQM table contains possible R values for the LPS
    "nexti",    "iTblQM",           // The iTblQM table contains probability transition rules
    "trans",    1,                  // Apply transition after renormalization
    "exch",     1                   // Apply conditional exchange 
}