Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(358)

Side by Side Diff: third_party/libwebp/utils/bit_writer.c

Issue 653803003: libwebp: update to 0.4.2-rc2 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/libwebp/utils/bit_reader.c ('k') | third_party/libwebp/utils/endian_inl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 Google Inc. All Rights Reserved. 1 // Copyright 2011 Google Inc. All Rights Reserved.
2 // 2 //
3 // Use of this source code is governed by a BSD-style license 3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the COPYING file in the root of the source 4 // that can be found in the COPYING file in the root of the source
5 // tree. An additional intellectual property rights grant can be found 5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may 6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree. 7 // be found in the AUTHORS file in the root of the source tree.
8 // ----------------------------------------------------------------------------- 8 // -----------------------------------------------------------------------------
9 // 9 //
10 // Bit writing and boolean coder 10 // Bit writing and boolean coder
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 if (bw->pos_ > 0) { 45 if (bw->pos_ > 0) {
46 assert(bw->buf_ != NULL); 46 assert(bw->buf_ != NULL);
47 memcpy(new_buf, bw->buf_, bw->pos_); 47 memcpy(new_buf, bw->buf_, bw->pos_);
48 } 48 }
49 WebPSafeFree(bw->buf_); 49 WebPSafeFree(bw->buf_);
50 bw->buf_ = new_buf; 50 bw->buf_ = new_buf;
51 bw->max_pos_ = new_size; 51 bw->max_pos_ = new_size;
52 return 1; 52 return 1;
53 } 53 }
54 54
55 static void kFlush(VP8BitWriter* const bw) { 55 static void Flush(VP8BitWriter* const bw) {
56 const int s = 8 + bw->nb_bits_; 56 const int s = 8 + bw->nb_bits_;
57 const int32_t bits = bw->value_ >> s; 57 const int32_t bits = bw->value_ >> s;
58 assert(bw->nb_bits_ >= 0); 58 assert(bw->nb_bits_ >= 0);
59 bw->value_ -= bits << s; 59 bw->value_ -= bits << s;
60 bw->nb_bits_ -= 8; 60 bw->nb_bits_ -= 8;
61 if ((bits & 0xff) != 0xff) { 61 if ((bits & 0xff) != 0xff) {
62 size_t pos = bw->pos_; 62 size_t pos = bw->pos_;
63 if (!BitWriterResize(bw, bw->run_ + 1)) { 63 if (!BitWriterResize(bw, bw->run_ + 1)) {
64 return; 64 return;
65 } 65 }
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 bw->value_ += split + 1; 111 bw->value_ += split + 1;
112 bw->range_ -= split + 1; 112 bw->range_ -= split + 1;
113 } else { 113 } else {
114 bw->range_ = split; 114 bw->range_ = split;
115 } 115 }
116 if (bw->range_ < 127) { // emit 'shift' bits out and renormalize 116 if (bw->range_ < 127) { // emit 'shift' bits out and renormalize
117 const int shift = kNorm[bw->range_]; 117 const int shift = kNorm[bw->range_];
118 bw->range_ = kNewRange[bw->range_]; 118 bw->range_ = kNewRange[bw->range_];
119 bw->value_ <<= shift; 119 bw->value_ <<= shift;
120 bw->nb_bits_ += shift; 120 bw->nb_bits_ += shift;
121 if (bw->nb_bits_ > 0) kFlush(bw); 121 if (bw->nb_bits_ > 0) Flush(bw);
122 } 122 }
123 return bit; 123 return bit;
124 } 124 }
125 125
126 int VP8PutBitUniform(VP8BitWriter* const bw, int bit) { 126 int VP8PutBitUniform(VP8BitWriter* const bw, int bit) {
127 const int split = bw->range_ >> 1; 127 const int split = bw->range_ >> 1;
128 if (bit) { 128 if (bit) {
129 bw->value_ += split + 1; 129 bw->value_ += split + 1;
130 bw->range_ -= split + 1; 130 bw->range_ -= split + 1;
131 } else { 131 } else {
132 bw->range_ = split; 132 bw->range_ = split;
133 } 133 }
134 if (bw->range_ < 127) { 134 if (bw->range_ < 127) {
135 bw->range_ = kNewRange[bw->range_]; 135 bw->range_ = kNewRange[bw->range_];
136 bw->value_ <<= 1; 136 bw->value_ <<= 1;
137 bw->nb_bits_ += 1; 137 bw->nb_bits_ += 1;
138 if (bw->nb_bits_ > 0) kFlush(bw); 138 if (bw->nb_bits_ > 0) Flush(bw);
139 } 139 }
140 return bit; 140 return bit;
141 } 141 }
142 142
143 void VP8PutValue(VP8BitWriter* const bw, int value, int nb_bits) { 143 void VP8PutValue(VP8BitWriter* const bw, int value, int nb_bits) {
144 int mask; 144 int mask;
145 for (mask = 1 << (nb_bits - 1); mask; mask >>= 1) 145 for (mask = 1 << (nb_bits - 1); mask; mask >>= 1)
146 VP8PutBitUniform(bw, value & mask); 146 VP8PutBitUniform(bw, value & mask);
147 } 147 }
148 148
(...skipping 17 matching lines...) Expand all
166 bw->pos_ = 0; 166 bw->pos_ = 0;
167 bw->max_pos_ = 0; 167 bw->max_pos_ = 0;
168 bw->error_ = 0; 168 bw->error_ = 0;
169 bw->buf_ = NULL; 169 bw->buf_ = NULL;
170 return (expected_size > 0) ? BitWriterResize(bw, expected_size) : 1; 170 return (expected_size > 0) ? BitWriterResize(bw, expected_size) : 1;
171 } 171 }
172 172
173 uint8_t* VP8BitWriterFinish(VP8BitWriter* const bw) { 173 uint8_t* VP8BitWriterFinish(VP8BitWriter* const bw) {
174 VP8PutValue(bw, 0, 9 - bw->nb_bits_); 174 VP8PutValue(bw, 0, 9 - bw->nb_bits_);
175 bw->nb_bits_ = 0; // pad with zeroes 175 bw->nb_bits_ = 0; // pad with zeroes
176 kFlush(bw); 176 Flush(bw);
177 return bw->buf_; 177 return bw->buf_;
178 } 178 }
179 179
180 int VP8BitWriterAppend(VP8BitWriter* const bw, 180 int VP8BitWriterAppend(VP8BitWriter* const bw,
181 const uint8_t* data, size_t size) { 181 const uint8_t* data, size_t size) {
182 assert(data != NULL); 182 assert(data != NULL);
183 if (bw->nb_bits_ != -8) return 0; // kFlush() must have been called 183 if (bw->nb_bits_ != -8) return 0; // Flush() must have been called
184 if (!BitWriterResize(bw, size)) return 0; 184 if (!BitWriterResize(bw, size)) return 0;
185 memcpy(bw->buf_ + bw->pos_, data, size); 185 memcpy(bw->buf_ + bw->pos_, data, size);
186 bw->pos_ += size; 186 bw->pos_ += size;
187 return 1; 187 return 1;
188 } 188 }
189 189
190 void VP8BitWriterWipeOut(VP8BitWriter* const bw) { 190 void VP8BitWriterWipeOut(VP8BitWriter* const bw) {
191 if (bw != NULL) { 191 if (bw != NULL) {
192 WebPSafeFree(bw->buf_); 192 WebPSafeFree(bw->buf_);
193 memset(bw, 0, sizeof(*bw)); 193 memset(bw, 0, sizeof(*bw));
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 *bw->cur_++ = (uint8_t)bw->bits_; 298 *bw->cur_++ = (uint8_t)bw->bits_;
299 bw->bits_ >>= 8; 299 bw->bits_ >>= 8;
300 bw->used_ -= 8; 300 bw->used_ -= 8;
301 } 301 }
302 bw->used_ = 0; 302 bw->used_ = 0;
303 } 303 }
304 return bw->buf_; 304 return bw->buf_;
305 } 305 }
306 306
307 //------------------------------------------------------------------------------ 307 //------------------------------------------------------------------------------
OLDNEW
« no previous file with comments | « third_party/libwebp/utils/bit_reader.c ('k') | third_party/libwebp/utils/endian_inl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698