Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license |
michael@0 | 5 | * that can be found in the LICENSE file in the root of the source |
michael@0 | 6 | * tree. An additional intellectual property rights grant can be found |
michael@0 | 7 | * in the file PATENTS. All contributing project authors may |
michael@0 | 8 | * be found in the AUTHORS file in the root of the source tree. |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | |
michael@0 | 12 | /**************************************************************************** |
michael@0 | 13 | * |
michael@0 | 14 | * Module Title : boolhuff.h |
michael@0 | 15 | * |
michael@0 | 16 | * Description : Bool Coder header file. |
michael@0 | 17 | * |
michael@0 | 18 | ****************************************************************************/ |
michael@0 | 19 | #ifndef __INC_BOOLHUFF_H |
michael@0 | 20 | #define __INC_BOOLHUFF_H |
michael@0 | 21 | |
michael@0 | 22 | #include "vpx_ports/mem.h" |
michael@0 | 23 | #include "vpx/internal/vpx_codec_internal.h" |
michael@0 | 24 | |
michael@0 | 25 | typedef struct |
michael@0 | 26 | { |
michael@0 | 27 | unsigned int lowvalue; |
michael@0 | 28 | unsigned int range; |
michael@0 | 29 | int count; |
michael@0 | 30 | unsigned int pos; |
michael@0 | 31 | unsigned char *buffer; |
michael@0 | 32 | unsigned char *buffer_end; |
michael@0 | 33 | struct vpx_internal_error_info *error; |
michael@0 | 34 | |
michael@0 | 35 | /* Variables used to track bit costs without outputing to the bitstream */ |
michael@0 | 36 | unsigned int measure_cost; |
michael@0 | 37 | unsigned long bit_counter; |
michael@0 | 38 | } BOOL_CODER; |
michael@0 | 39 | |
michael@0 | 40 | extern void vp8_start_encode(BOOL_CODER *bc, unsigned char *buffer, unsigned char *buffer_end); |
michael@0 | 41 | |
michael@0 | 42 | extern void vp8_encode_value(BOOL_CODER *br, int data, int bits); |
michael@0 | 43 | extern void vp8_stop_encode(BOOL_CODER *bc); |
michael@0 | 44 | extern const unsigned int vp8_prob_cost[256]; |
michael@0 | 45 | |
michael@0 | 46 | |
michael@0 | 47 | DECLARE_ALIGNED(16, extern const unsigned char, vp8_norm[256]); |
michael@0 | 48 | |
michael@0 | 49 | static int validate_buffer(const unsigned char *start, |
michael@0 | 50 | size_t len, |
michael@0 | 51 | const unsigned char *end, |
michael@0 | 52 | struct vpx_internal_error_info *error) |
michael@0 | 53 | { |
michael@0 | 54 | if (start + len > start && start + len < end) |
michael@0 | 55 | return 1; |
michael@0 | 56 | else |
michael@0 | 57 | vpx_internal_error(error, VPX_CODEC_CORRUPT_FRAME, |
michael@0 | 58 | "Truncated packet or corrupt partition "); |
michael@0 | 59 | |
michael@0 | 60 | return 0; |
michael@0 | 61 | } |
michael@0 | 62 | static void vp8_encode_bool(BOOL_CODER *br, int bit, int probability) |
michael@0 | 63 | { |
michael@0 | 64 | unsigned int split; |
michael@0 | 65 | int count = br->count; |
michael@0 | 66 | unsigned int range = br->range; |
michael@0 | 67 | unsigned int lowvalue = br->lowvalue; |
michael@0 | 68 | register unsigned int shift; |
michael@0 | 69 | |
michael@0 | 70 | #ifdef VP8_ENTROPY_STATS |
michael@0 | 71 | #if defined(SECTIONBITS_OUTPUT) |
michael@0 | 72 | |
michael@0 | 73 | if (bit) |
michael@0 | 74 | Sectionbits[active_section] += vp8_prob_cost[255-probability]; |
michael@0 | 75 | else |
michael@0 | 76 | Sectionbits[active_section] += vp8_prob_cost[probability]; |
michael@0 | 77 | |
michael@0 | 78 | #endif |
michael@0 | 79 | #endif |
michael@0 | 80 | |
michael@0 | 81 | split = 1 + (((range - 1) * probability) >> 8); |
michael@0 | 82 | |
michael@0 | 83 | range = split; |
michael@0 | 84 | |
michael@0 | 85 | if (bit) |
michael@0 | 86 | { |
michael@0 | 87 | lowvalue += split; |
michael@0 | 88 | range = br->range - split; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | shift = vp8_norm[range]; |
michael@0 | 92 | |
michael@0 | 93 | range <<= shift; |
michael@0 | 94 | count += shift; |
michael@0 | 95 | |
michael@0 | 96 | if (count >= 0) |
michael@0 | 97 | { |
michael@0 | 98 | int offset = shift - count; |
michael@0 | 99 | |
michael@0 | 100 | if ((lowvalue << (offset - 1)) & 0x80000000) |
michael@0 | 101 | { |
michael@0 | 102 | int x = br->pos - 1; |
michael@0 | 103 | |
michael@0 | 104 | while (x >= 0 && br->buffer[x] == 0xff) |
michael@0 | 105 | { |
michael@0 | 106 | br->buffer[x] = (unsigned char)0; |
michael@0 | 107 | x--; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | br->buffer[x] += 1; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | validate_buffer(br->buffer + br->pos, 1, br->buffer_end, br->error); |
michael@0 | 114 | br->buffer[br->pos++] = (lowvalue >> (24 - offset)); |
michael@0 | 115 | |
michael@0 | 116 | lowvalue <<= offset; |
michael@0 | 117 | shift = count; |
michael@0 | 118 | lowvalue &= 0xffffff; |
michael@0 | 119 | count -= 8 ; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | lowvalue <<= shift; |
michael@0 | 123 | br->count = count; |
michael@0 | 124 | br->lowvalue = lowvalue; |
michael@0 | 125 | br->range = range; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | #endif |