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