Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
  3.  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4.  *
  5.  * This file is part of FFmpeg.
  6.  *
  7.  * FFmpeg is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * FFmpeg is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with FFmpeg; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20.  */
  21.  
  22. /**
  23.  * @file
  24.  * Context Adaptive Binary Arithmetic Coder.
  25.  */
  26.  
  27. #include <string.h>
  28.  
  29. #include "libavutil/common.h"
  30. #include "libavutil/timer.h"
  31. #include "get_bits.h"
  32. #include "cabac.h"
  33. #include "cabac_functions.h"
  34.  
  35. #include "cabac_tablegen.h"
  36.  
  37. /**
  38.  *
  39.  * @param buf_size size of buf in bits
  40.  */
  41. void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size){
  42.     init_put_bits(&c->pb, buf, buf_size);
  43.  
  44.     c->low= 0;
  45.     c->range= 0x1FE;
  46.     c->outstanding_count= 0;
  47.     c->pb.bit_left++; //avoids firstBitFlag
  48. }
  49.  
  50. /**
  51.  *
  52.  * @param buf_size size of buf in bits
  53.  */
  54. int ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size){
  55.     c->bytestream_start=
  56.     c->bytestream= buf;
  57.     c->bytestream_end= buf + buf_size;
  58.  
  59. #if CABAC_BITS == 16
  60.     c->low =  (*c->bytestream++)<<18;
  61.     c->low+=  (*c->bytestream++)<<10;
  62. #else
  63.     c->low =  (*c->bytestream++)<<10;
  64. #endif
  65.     c->low+= ((*c->bytestream++)<<2) + 2;
  66.     c->range= 0x1FE;
  67.     if ((c->range<<(CABAC_BITS+1)) < c->low)
  68.         return AVERROR_INVALIDDATA;
  69.     return 0;
  70. }
  71.  
  72. void ff_init_cabac_states(void)
  73. {
  74.     static int initialized = 0;
  75.  
  76.     if (initialized)
  77.         return;
  78.  
  79.     cabac_tableinit();
  80.  
  81.     initialized = 1;
  82. }
  83.  
  84. #ifdef TEST
  85. #define SIZE 10240
  86.  
  87. #include "libavutil/lfg.h"
  88. #include "avcodec.h"
  89.  
  90. static inline void put_cabac_bit(CABACContext *c, int b){
  91.     put_bits(&c->pb, 1, b);
  92.     for(;c->outstanding_count; c->outstanding_count--){
  93.         put_bits(&c->pb, 1, 1-b);
  94.     }
  95. }
  96.  
  97. static inline void renorm_cabac_encoder(CABACContext *c){
  98.     while(c->range < 0x100){
  99.         //FIXME optimize
  100.         if(c->low<0x100){
  101.             put_cabac_bit(c, 0);
  102.         }else if(c->low<0x200){
  103.             c->outstanding_count++;
  104.             c->low -= 0x100;
  105.         }else{
  106.             put_cabac_bit(c, 1);
  107.             c->low -= 0x200;
  108.         }
  109.  
  110.         c->range+= c->range;
  111.         c->low += c->low;
  112.     }
  113. }
  114.  
  115. static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
  116.     int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
  117.  
  118.     if(bit == ((*state)&1)){
  119.         c->range -= RangeLPS;
  120.         *state    = ff_h264_mlps_state[128 + *state];
  121.     }else{
  122.         c->low += c->range - RangeLPS;
  123.         c->range = RangeLPS;
  124.         *state= ff_h264_mlps_state[127 - *state];
  125.     }
  126.  
  127.     renorm_cabac_encoder(c);
  128. }
  129.  
  130. /**
  131.  * @param bit 0 -> write zero bit, !=0 write one bit
  132.  */
  133. static void put_cabac_bypass(CABACContext *c, int bit){
  134.     c->low += c->low;
  135.  
  136.     if(bit){
  137.         c->low += c->range;
  138.     }
  139. //FIXME optimize
  140.     if(c->low<0x200){
  141.         put_cabac_bit(c, 0);
  142.     }else if(c->low<0x400){
  143.         c->outstanding_count++;
  144.         c->low -= 0x200;
  145.     }else{
  146.         put_cabac_bit(c, 1);
  147.         c->low -= 0x400;
  148.     }
  149. }
  150.  
  151. /**
  152.  *
  153.  * @return the number of bytes written
  154.  */
  155. static int put_cabac_terminate(CABACContext *c, int bit){
  156.     c->range -= 2;
  157.  
  158.     if(!bit){
  159.         renorm_cabac_encoder(c);
  160.     }else{
  161.         c->low += c->range;
  162.         c->range= 2;
  163.  
  164.         renorm_cabac_encoder(c);
  165.  
  166.         av_assert0(c->low <= 0x1FF);
  167.         put_cabac_bit(c, c->low>>9);
  168.         put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
  169.  
  170.         flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
  171.     }
  172.  
  173.     return (put_bits_count(&c->pb)+7)>>3;
  174. }
  175.  
  176. int main(void){
  177.     CABACContext c;
  178.     uint8_t b[9*SIZE];
  179.     uint8_t r[9*SIZE];
  180.     int i, ret = 0;
  181.     uint8_t state[10]= {0};
  182.     AVLFG prng;
  183.  
  184.     av_lfg_init(&prng, 1);
  185.     ff_init_cabac_encoder(&c, b, SIZE);
  186.     ff_init_cabac_states();
  187.  
  188.     for(i=0; i<SIZE; i++){
  189.         if(2*i<SIZE) r[i] = av_lfg_get(&prng) % 7;
  190.         else         r[i] = (i>>8)&1;
  191.     }
  192.  
  193.     for(i=0; i<SIZE; i++){
  194.         put_cabac_bypass(&c, r[i]&1);
  195.     }
  196.  
  197.     for(i=0; i<SIZE; i++){
  198.         put_cabac(&c, state, r[i]&1);
  199.     }
  200.  
  201.     put_cabac_terminate(&c, 1);
  202.  
  203.     ff_init_cabac_decoder(&c, b, SIZE);
  204.  
  205.     memset(state, 0, sizeof(state));
  206.  
  207.     for(i=0; i<SIZE; i++){
  208.         if( (r[i]&1) != get_cabac_bypass(&c) ) {
  209.             av_log(NULL, AV_LOG_ERROR, "CABAC bypass failure at %d\n", i);
  210.             ret = 1;
  211.         }
  212.     }
  213.  
  214.     for(i=0; i<SIZE; i++){
  215.         if( (r[i]&1) != get_cabac_noinline(&c, state) ) {
  216.             av_log(NULL, AV_LOG_ERROR, "CABAC failure at %d\n", i);
  217.             ret = 1;
  218.         }
  219.     }
  220.     if(!get_cabac_terminate(&c)) {
  221.         av_log(NULL, AV_LOG_ERROR, "where's the Terminator?\n");
  222.         ret = 1;
  223.     }
  224.  
  225.     return ret;
  226. }
  227.  
  228. #endif /* TEST */
  229.