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 :     boolhuff.h
     15 *
     16 *   Description  :     Bool Coder header file.
     17 *
     18 ****************************************************************************/
     19 #ifndef VP8_ENCODER_BOOLHUFF_H_
     20 #define VP8_ENCODER_BOOLHUFF_H_
     21 
     22 #include "vpx_ports/mem.h"
     23 #include "vpx/internal/vpx_codec_internal.h"
     24 
     25 #ifdef __cplusplus
     26 extern "C" {
     27 #endif
     28 
     29 typedef struct
     30 {
     31     unsigned int lowvalue;
     32     unsigned int range;
     33     int count;
     34     unsigned int pos;
     35     unsigned char *buffer;
     36     unsigned char *buffer_end;
     37     struct vpx_internal_error_info *error;
     38 
     39     /* Variables used to track bit costs without outputing to the bitstream */
     40     unsigned int  measure_cost;
     41     unsigned long bit_counter;
     42 } BOOL_CODER;
     43 
     44 extern void vp8_start_encode(BOOL_CODER *bc, unsigned char *buffer, unsigned char *buffer_end);
     45 
     46 extern void vp8_encode_value(BOOL_CODER *br, int data, int bits);
     47 extern void vp8_stop_encode(BOOL_CODER *bc);
     48 extern const unsigned int vp8_prob_cost[256];
     49 
     50 
     51 DECLARE_ALIGNED(16, extern const unsigned char, vp8_norm[256]);
     52 
     53 static int validate_buffer(const unsigned char *start,
     54                            size_t               len,
     55                            const unsigned char *end,
     56                            struct vpx_internal_error_info *error)
     57 {
     58     if (start + len > start && start + len < end)
     59         return 1;
     60     else
     61         vpx_internal_error(error, VPX_CODEC_CORRUPT_FRAME,
     62             "Truncated packet or corrupt partition ");
     63 
     64     return 0;
     65 }
     66 static void vp8_encode_bool(BOOL_CODER *br, int bit, int probability)
     67 {
     68     unsigned int split;
     69     int count = br->count;
     70     unsigned int range = br->range;
     71     unsigned int lowvalue = br->lowvalue;
     72     register unsigned int shift;
     73 
     74 #ifdef VP8_ENTROPY_STATS
     75 #if defined(SECTIONBITS_OUTPUT)
     76 
     77     if (bit)
     78         Sectionbits[active_section] += vp8_prob_cost[255-probability];
     79     else
     80         Sectionbits[active_section] += vp8_prob_cost[probability];
     81 
     82 #endif
     83 #endif
     84 
     85     split = 1 + (((range - 1) * probability) >> 8);
     86 
     87     range = split;
     88 
     89     if (bit)
     90     {
     91         lowvalue += split;
     92         range = br->range - split;
     93     }
     94 
     95     shift = vp8_norm[range];
     96 
     97     range <<= shift;
     98     count += shift;
     99 
    100     if (count >= 0)
    101     {
    102         int offset = shift - count;
    103 
    104         if ((lowvalue << (offset - 1)) & 0x80000000)
    105         {
    106             int x = br->pos - 1;
    107 
    108             while (x >= 0 && br->buffer[x] == 0xff)
    109             {
    110                 br->buffer[x] = (unsigned char)0;
    111                 x--;
    112             }
    113 
    114             br->buffer[x] += 1;
    115         }
    116 
    117         validate_buffer(br->buffer + br->pos, 1, br->buffer_end, br->error);
    118         br->buffer[br->pos++] = (lowvalue >> (24 - offset));
    119 
    120         lowvalue <<= offset;
    121         shift = count;
    122         lowvalue &= 0xffffff;
    123         count -= 8 ;
    124     }
    125 
    126     lowvalue <<= shift;
    127     br->count = count;
    128     br->lowvalue = lowvalue;
    129     br->range = range;
    130 }
    131 
    132 #ifdef __cplusplus
    133 }  // extern "C"
    134 #endif
    135 
    136 #endif  // VP8_ENCODER_BOOLHUFF_H_
    137