Home | History | Annotate | Download | only in encoder
      1 /*
      2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 
     12 /****************************************************************************
     13 *
     14 *   Module Title :     vp9_boolhuff.h
     15 *
     16 *   Description  :     Bool Coder header file.
     17 *
     18 ****************************************************************************/
     19 #ifndef VP9_ENCODER_VP9_BOOLHUFF_H_
     20 #define VP9_ENCODER_VP9_BOOLHUFF_H_
     21 
     22 #include "vpx_ports/mem.h"
     23 
     24 typedef struct {
     25   unsigned int lowvalue;
     26   unsigned int range;
     27   unsigned int value;
     28   int count;
     29   unsigned int pos;
     30   uint8_t *buffer;
     31 
     32   // Variables used to track bit costs without outputing to the bitstream
     33   unsigned int  measure_cost;
     34   unsigned long bit_counter;
     35 } vp9_writer;
     36 
     37 extern const unsigned int vp9_prob_cost[256];
     38 
     39 void vp9_start_encode(vp9_writer *bc, uint8_t *buffer);
     40 void vp9_stop_encode(vp9_writer *bc);
     41 
     42 DECLARE_ALIGNED(16, extern const unsigned char, vp9_norm[256]);
     43 
     44 static void vp9_write(vp9_writer *br, int bit, int probability) {
     45   unsigned int split;
     46   int count = br->count;
     47   unsigned int range = br->range;
     48   unsigned int lowvalue = br->lowvalue;
     49   register unsigned int shift;
     50 
     51 #ifdef ENTROPY_STATS
     52 #if defined(SECTIONBITS_OUTPUT)
     53 
     54   if (bit)
     55     Sectionbits[active_section] += vp9_prob_cost[255 - probability];
     56   else
     57     Sectionbits[active_section] += vp9_prob_cost[probability];
     58 
     59 #endif
     60 #endif
     61 
     62   split = 1 + (((range - 1) * probability) >> 8);
     63 
     64   range = split;
     65 
     66   if (bit) {
     67     lowvalue += split;
     68     range = br->range - split;
     69   }
     70 
     71   shift = vp9_norm[range];
     72 
     73   range <<= shift;
     74   count += shift;
     75 
     76   if (count >= 0) {
     77     int offset = shift - count;
     78 
     79     if ((lowvalue << (offset - 1)) & 0x80000000) {
     80       int x = br->pos - 1;
     81 
     82       while (x >= 0 && br->buffer[x] == 0xff) {
     83         br->buffer[x] = 0;
     84         x--;
     85       }
     86 
     87       br->buffer[x] += 1;
     88     }
     89 
     90     br->buffer[br->pos++] = (lowvalue >> (24 - offset));
     91     lowvalue <<= offset;
     92     shift = count;
     93     lowvalue &= 0xffffff;
     94     count -= 8;
     95   }
     96 
     97   lowvalue <<= shift;
     98   br->count = count;
     99   br->lowvalue = lowvalue;
    100   br->range = range;
    101 }
    102 
    103 static void vp9_write_bit(vp9_writer *w, int bit) {
    104   vp9_write(w, bit, 128);  // vp9_prob_half
    105 }
    106 
    107 static void vp9_write_literal(vp9_writer *w, int data, int bits) {
    108   int bit;
    109 
    110   for (bit = bits - 1; bit >= 0; bit--)
    111     vp9_write_bit(w, 1 & (data >> bit));
    112 }
    113 
    114 
    115 #endif  // VP9_ENCODER_VP9_BOOLHUFF_H_
    116