OLD | NEW |
1 // Copyright 2011 Google Inc. | 1 // Copyright 2011 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // This code is licensed under the same terms as WebM: | 3 // This code is licensed under the same terms as WebM: |
4 // Software License Agreement: http://www.webmproject.org/license/software/ | 4 // Software License Agreement: http://www.webmproject.org/license/software/ |
5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ | 5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ |
6 // ----------------------------------------------------------------------------- | 6 // ----------------------------------------------------------------------------- |
7 // | 7 // |
8 // Header syntax writing | 8 // Header syntax writing |
9 // | 9 // |
10 // Author: Skal (pascal.massimino@gmail.com) | 10 // Author: Skal (pascal.massimino@gmail.com) |
11 | 11 |
12 #include <assert.h> | 12 #include <assert.h> |
13 #include <math.h> | |
14 | 13 |
15 #include "vp8enci.h" | 14 #include "../webp/format_constants.h" |
| 15 #include "./vp8enci.h" |
16 | 16 |
17 #if defined(__cplusplus) || defined(c_plusplus) | 17 #if defined(__cplusplus) || defined(c_plusplus) |
18 extern "C" { | 18 extern "C" { |
19 #endif | 19 #endif |
20 | 20 |
21 #define KSIGNATURE 0x9d012a | 21 //------------------------------------------------------------------------------ |
22 #define KHEADER_SIZE 10 | 22 // Helper functions |
23 #define KRIFF_SIZE 20 | |
24 #define KSIZE_OFFSET (KRIFF_SIZE - 8) | |
25 | 23 |
26 #define MAX_PARTITION0_SIZE (1 << 19) // max size of mode partition | 24 // TODO(later): Move to webp/format_constants.h? |
27 #define MAX_PARTITION_SIZE (1 << 24) // max size for token partition | 25 static void PutLE24(uint8_t* const data, uint32_t val) { |
| 26 data[0] = (val >> 0) & 0xff; |
| 27 data[1] = (val >> 8) & 0xff; |
| 28 data[2] = (val >> 16) & 0xff; |
| 29 } |
| 30 |
| 31 static void PutLE32(uint8_t* const data, uint32_t val) { |
| 32 PutLE24(data, val); |
| 33 data[3] = (val >> 24) & 0xff; |
| 34 } |
| 35 |
| 36 static int IsVP8XNeeded(const VP8Encoder* const enc) { |
| 37 return !!enc->has_alpha_; // Currently the only case when VP8X is needed. |
| 38 // This could change in the future. |
| 39 } |
| 40 |
| 41 static int PutPaddingByte(const WebPPicture* const pic) { |
| 42 |
| 43 const uint8_t pad_byte[1] = { 0 }; |
| 44 return !!pic->writer(pad_byte, 1, pic); |
| 45 } |
28 | 46 |
29 //------------------------------------------------------------------------------ | 47 //------------------------------------------------------------------------------ |
30 // Writers for header's various pieces (in order of appearance) | 48 // Writers for header's various pieces (in order of appearance) |
31 | 49 |
32 // Main keyframe header | 50 static WebPEncodingError PutRIFFHeader(const VP8Encoder* const enc, |
33 | 51 size_t riff_size) { |
34 static void PutLE32(uint8_t* const data, uint32_t val) { | 52 const WebPPicture* const pic = enc->pic_; |
35 data[0] = (val >> 0) & 0xff; | 53 uint8_t riff[RIFF_HEADER_SIZE] = { |
36 data[1] = (val >> 8) & 0xff; | 54 'R', 'I', 'F', 'F', 0, 0, 0, 0, 'W', 'E', 'B', 'P' |
37 data[2] = (val >> 16) & 0xff; | 55 }; |
38 data[3] = (val >> 24) & 0xff; | 56 assert(riff_size == (uint32_t)riff_size); |
| 57 PutLE32(riff + TAG_SIZE, (uint32_t)riff_size); |
| 58 if (!pic->writer(riff, sizeof(riff), pic)) { |
| 59 return VP8_ENC_ERROR_BAD_WRITE; |
| 60 } |
| 61 return VP8_ENC_OK; |
39 } | 62 } |
40 | 63 |
41 static int PutHeader(int profile, size_t size0, size_t total_size, | 64 static WebPEncodingError PutVP8XHeader(const VP8Encoder* const enc) { |
42 WebPPicture* const pic) { | 65 const WebPPicture* const pic = enc->pic_; |
43 uint8_t buf[KHEADER_SIZE]; | 66 uint8_t vp8x[CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE] = { |
44 uint8_t RIFF[KRIFF_SIZE] = { | 67 'V', 'P', '8', 'X' |
45 'R', 'I', 'F', 'F', 0, 0, 0, 0, 'W', 'E', 'B', 'P', 'V', 'P', '8', ' ' | |
46 }; | 68 }; |
| 69 uint32_t flags = 0; |
| 70 |
| 71 assert(IsVP8XNeeded(enc)); |
| 72 assert(pic->width >= 1 && pic->height >= 1); |
| 73 assert(pic->width <= MAX_CANVAS_SIZE && pic->height <= MAX_CANVAS_SIZE); |
| 74 |
| 75 if (enc->has_alpha_) { |
| 76 flags |= ALPHA_FLAG_BIT; |
| 77 } |
| 78 |
| 79 PutLE32(vp8x + TAG_SIZE, VP8X_CHUNK_SIZE); |
| 80 PutLE32(vp8x + CHUNK_HEADER_SIZE, flags); |
| 81 PutLE24(vp8x + CHUNK_HEADER_SIZE + 4, pic->width - 1); |
| 82 PutLE24(vp8x + CHUNK_HEADER_SIZE + 7, pic->height - 1); |
| 83 if(!pic->writer(vp8x, sizeof(vp8x), pic)) { |
| 84 return VP8_ENC_ERROR_BAD_WRITE; |
| 85 } |
| 86 return VP8_ENC_OK; |
| 87 } |
| 88 |
| 89 static WebPEncodingError PutAlphaChunk(const VP8Encoder* const enc) { |
| 90 const WebPPicture* const pic = enc->pic_; |
| 91 uint8_t alpha_chunk_hdr[CHUNK_HEADER_SIZE] = { |
| 92 'A', 'L', 'P', 'H' |
| 93 }; |
| 94 |
| 95 assert(enc->has_alpha_); |
| 96 |
| 97 // Alpha chunk header. |
| 98 PutLE32(alpha_chunk_hdr + TAG_SIZE, enc->alpha_data_size_); |
| 99 if (!pic->writer(alpha_chunk_hdr, sizeof(alpha_chunk_hdr), pic)) { |
| 100 return VP8_ENC_ERROR_BAD_WRITE; |
| 101 } |
| 102 |
| 103 // Alpha chunk data. |
| 104 if (!pic->writer(enc->alpha_data_, enc->alpha_data_size_, pic)) { |
| 105 return VP8_ENC_ERROR_BAD_WRITE; |
| 106 } |
| 107 |
| 108 // Padding. |
| 109 if ((enc->alpha_data_size_ & 1) && !PutPaddingByte(pic)) { |
| 110 return VP8_ENC_ERROR_BAD_WRITE; |
| 111 } |
| 112 return VP8_ENC_OK; |
| 113 } |
| 114 |
| 115 static WebPEncodingError PutVP8Header(const WebPPicture* const pic, |
| 116 size_t vp8_size) { |
| 117 uint8_t vp8_chunk_hdr[CHUNK_HEADER_SIZE] = { |
| 118 'V', 'P', '8', ' ' |
| 119 }; |
| 120 assert(vp8_size == (uint32_t)vp8_size); |
| 121 PutLE32(vp8_chunk_hdr + TAG_SIZE, (uint32_t)vp8_size); |
| 122 if (!pic->writer(vp8_chunk_hdr, sizeof(vp8_chunk_hdr), pic)) { |
| 123 return VP8_ENC_ERROR_BAD_WRITE; |
| 124 } |
| 125 return VP8_ENC_OK; |
| 126 } |
| 127 |
| 128 static WebPEncodingError PutVP8FrameHeader(const WebPPicture* const pic, |
| 129 int profile, size_t size0) { |
| 130 uint8_t vp8_frm_hdr[VP8_FRAME_HEADER_SIZE]; |
47 uint32_t bits; | 131 uint32_t bits; |
48 | 132 |
49 if (size0 >= MAX_PARTITION0_SIZE) { // partition #0 is too big to fit | 133 if (size0 >= VP8_MAX_PARTITION0_SIZE) { // partition #0 is too big to fit |
50 return WebPEncodingSetError(pic, VP8_ENC_ERROR_PARTITION0_OVERFLOW); | 134 return VP8_ENC_ERROR_PARTITION0_OVERFLOW; |
51 } | 135 } |
52 | 136 |
53 if (total_size > 0xfffffffeU - KRIFF_SIZE) { | 137 // Paragraph 9.1. |
54 return WebPEncodingSetError(pic, VP8_ENC_ERROR_FILE_TOO_BIG); | |
55 } | |
56 | |
57 PutLE32(RIFF + 4, (uint32_t)(total_size + KSIZE_OFFSET)); | |
58 PutLE32(RIFF + 16, (uint32_t)total_size); | |
59 if (!pic->writer(RIFF, sizeof(RIFF), pic)) { | |
60 return WebPEncodingSetError(pic, VP8_ENC_ERROR_BAD_WRITE); | |
61 } | |
62 | |
63 bits = 0 // keyframe (1b) | 138 bits = 0 // keyframe (1b) |
64 | (profile << 1) // profile (3b) | 139 | (profile << 1) // profile (3b) |
65 | (1 << 4) // visible (1b) | 140 | (1 << 4) // visible (1b) |
66 | ((uint32_t)size0 << 5); // partition length (19b) | 141 | ((uint32_t)size0 << 5); // partition length (19b) |
67 buf[0] = bits & 0xff; | 142 vp8_frm_hdr[0] = (bits >> 0) & 0xff; |
68 buf[1] = (bits >> 8) & 0xff; | 143 vp8_frm_hdr[1] = (bits >> 8) & 0xff; |
69 buf[2] = (bits >> 16) & 0xff; | 144 vp8_frm_hdr[2] = (bits >> 16) & 0xff; |
70 // signature | 145 // signature |
71 buf[3] = (KSIGNATURE >> 16) & 0xff; | 146 vp8_frm_hdr[3] = (VP8_SIGNATURE >> 16) & 0xff; |
72 buf[4] = (KSIGNATURE >> 8) & 0xff; | 147 vp8_frm_hdr[4] = (VP8_SIGNATURE >> 8) & 0xff; |
73 buf[5] = (KSIGNATURE >> 0) & 0xff; | 148 vp8_frm_hdr[5] = (VP8_SIGNATURE >> 0) & 0xff; |
74 // dimensions | 149 // dimensions |
75 buf[6] = pic->width & 0xff; | 150 vp8_frm_hdr[6] = pic->width & 0xff; |
76 buf[7] = pic->width >> 8; | 151 vp8_frm_hdr[7] = pic->width >> 8; |
77 buf[8] = pic->height & 0xff; | 152 vp8_frm_hdr[8] = pic->height & 0xff; |
78 buf[9] = pic->height >> 8; | 153 vp8_frm_hdr[9] = pic->height >> 8; |
79 | 154 |
80 return pic->writer(buf, sizeof(buf), pic); | 155 if (!pic->writer(vp8_frm_hdr, sizeof(vp8_frm_hdr), pic)) { |
| 156 return VP8_ENC_ERROR_BAD_WRITE; |
| 157 } |
| 158 return VP8_ENC_OK; |
| 159 } |
| 160 |
| 161 // WebP Headers. |
| 162 static int PutWebPHeaders(const VP8Encoder* const enc, size_t size0, |
| 163 size_t vp8_size, size_t riff_size) { |
| 164 WebPPicture* const pic = enc->pic_; |
| 165 WebPEncodingError err = VP8_ENC_OK; |
| 166 |
| 167 // RIFF header. |
| 168 err = PutRIFFHeader(enc, riff_size); |
| 169 if (err != VP8_ENC_OK) goto Error; |
| 170 |
| 171 // VP8X. |
| 172 if (IsVP8XNeeded(enc)) { |
| 173 err = PutVP8XHeader(enc); |
| 174 if (err != VP8_ENC_OK) goto Error; |
| 175 } |
| 176 |
| 177 // Alpha. |
| 178 if (enc->has_alpha_) { |
| 179 err = PutAlphaChunk(enc); |
| 180 if (err != VP8_ENC_OK) goto Error; |
| 181 } |
| 182 |
| 183 // VP8 header. |
| 184 err = PutVP8Header(pic, vp8_size); |
| 185 if (err != VP8_ENC_OK) goto Error; |
| 186 |
| 187 // VP8 frame header. |
| 188 err = PutVP8FrameHeader(pic, enc->profile_, size0); |
| 189 if (err != VP8_ENC_OK) goto Error; |
| 190 |
| 191 // All OK. |
| 192 return 1; |
| 193 |
| 194 // Error. |
| 195 Error: |
| 196 return WebPEncodingSetError(pic, err); |
81 } | 197 } |
82 | 198 |
83 // Segmentation header | 199 // Segmentation header |
84 static void PutSegmentHeader(VP8BitWriter* const bw, | 200 static void PutSegmentHeader(VP8BitWriter* const bw, |
85 const VP8Encoder* const enc) { | 201 const VP8Encoder* const enc) { |
86 const VP8SegmentHeader* const hdr = &enc->segment_hdr_; | 202 const VP8SegmentHeader* const hdr = &enc->segment_hdr_; |
87 const VP8Proba* const proba = &enc->proba_; | 203 const VP8Proba* const proba = &enc->proba_; |
88 if (VP8PutBitUniform(bw, (hdr->num_segments_ > 1))) { | 204 if (VP8PutBitUniform(bw, (hdr->num_segments_ > 1))) { |
89 // We always 'update' the quant and filter strength values | 205 // We always 'update' the quant and filter strength values |
90 const int update_data = 1; | 206 const int update_data = 1; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 VP8PutSignedValue(bw, enc->dq_uv_ac_, 4); | 257 VP8PutSignedValue(bw, enc->dq_uv_ac_, 4); |
142 } | 258 } |
143 | 259 |
144 // Partition sizes | 260 // Partition sizes |
145 static int EmitPartitionsSize(const VP8Encoder* const enc, | 261 static int EmitPartitionsSize(const VP8Encoder* const enc, |
146 WebPPicture* const pic) { | 262 WebPPicture* const pic) { |
147 uint8_t buf[3 * (MAX_NUM_PARTITIONS - 1)]; | 263 uint8_t buf[3 * (MAX_NUM_PARTITIONS - 1)]; |
148 int p; | 264 int p; |
149 for (p = 0; p < enc->num_parts_ - 1; ++p) { | 265 for (p = 0; p < enc->num_parts_ - 1; ++p) { |
150 const size_t part_size = VP8BitWriterSize(enc->parts_ + p); | 266 const size_t part_size = VP8BitWriterSize(enc->parts_ + p); |
151 if (part_size >= MAX_PARTITION_SIZE) { | 267 if (part_size >= VP8_MAX_PARTITION_SIZE) { |
152 return WebPEncodingSetError(pic, VP8_ENC_ERROR_PARTITION_OVERFLOW); | 268 return WebPEncodingSetError(pic, VP8_ENC_ERROR_PARTITION_OVERFLOW); |
153 } | 269 } |
154 buf[3 * p + 0] = (part_size >> 0) & 0xff; | 270 buf[3 * p + 0] = (part_size >> 0) & 0xff; |
155 buf[3 * p + 1] = (part_size >> 8) & 0xff; | 271 buf[3 * p + 1] = (part_size >> 8) & 0xff; |
156 buf[3 * p + 2] = (part_size >> 16) & 0xff; | 272 buf[3 * p + 2] = (part_size >> 16) & 0xff; |
157 } | 273 } |
158 return p ? pic->writer(buf, 3 * p, pic) : 1; | 274 return p ? pic->writer(buf, 3 * p, pic) : 1; |
159 } | 275 } |
160 | 276 |
161 //------------------------------------------------------------------------------ | 277 //------------------------------------------------------------------------------ |
162 | 278 |
163 #ifdef WEBP_EXPERIMENTAL_FEATURES | 279 #ifdef WEBP_EXPERIMENTAL_FEATURES |
164 | 280 |
165 #define KTRAILER_SIZE 8 | 281 #define KTRAILER_SIZE 8 |
166 | 282 |
167 static void PutLE24(uint8_t* buf, size_t value) { | |
168 buf[0] = (value >> 0) & 0xff; | |
169 buf[1] = (value >> 8) & 0xff; | |
170 buf[2] = (value >> 16) & 0xff; | |
171 } | |
172 | |
173 static int WriteExtensions(VP8Encoder* const enc) { | 283 static int WriteExtensions(VP8Encoder* const enc) { |
174 uint8_t buffer[KTRAILER_SIZE]; | 284 uint8_t buffer[KTRAILER_SIZE]; |
175 VP8BitWriter* const bw = &enc->bw_; | 285 VP8BitWriter* const bw = &enc->bw_; |
176 WebPPicture* const pic = enc->pic_; | 286 WebPPicture* const pic = enc->pic_; |
177 | 287 |
178 // Layer (bytes 0..3) | 288 // Layer (bytes 0..3) |
179 PutLE24(buffer + 0, enc->layer_data_size_); | 289 PutLE24(buffer + 0, enc->layer_data_size_); |
180 buffer[3] = enc->pic_->colorspace & WEBP_CSP_UV_MASK; | 290 buffer[3] = enc->pic_->colorspace & WEBP_CSP_UV_MASK; |
181 if (enc->layer_data_size_ > 0) { | 291 if (enc->layer_data_size_ > 0) { |
182 assert(enc->use_layer_); | 292 assert(enc->use_layer_); |
183 // append layer data to last partition | 293 // append layer data to last partition |
184 if (!VP8BitWriterAppend(&enc->parts_[enc->num_parts_ - 1], | 294 if (!VP8BitWriterAppend(&enc->parts_[enc->num_parts_ - 1], |
185 enc->layer_data_, enc->layer_data_size_)) { | 295 enc->layer_data_, enc->layer_data_size_)) { |
186 return WebPEncodingSetError(pic, VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY); | 296 return WebPEncodingSetError(pic, VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY); |
187 } | 297 } |
188 } | 298 } |
189 // Alpha (bytes 4..6) | |
190 PutLE24(buffer + 4, enc->alpha_data_size_); | |
191 if (enc->alpha_data_size_ > 0) { | |
192 assert(enc->has_alpha_); | |
193 if (!VP8BitWriterAppend(bw, enc->alpha_data_, enc->alpha_data_size_)) { | |
194 return WebPEncodingSetError(pic, VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY); | |
195 } | |
196 } | |
197 | 299 |
198 buffer[KTRAILER_SIZE - 1] = 0x01; // marker | 300 buffer[KTRAILER_SIZE - 1] = 0x01; // marker |
199 if (!VP8BitWriterAppend(bw, buffer, KTRAILER_SIZE)) { | 301 if (!VP8BitWriterAppend(bw, buffer, KTRAILER_SIZE)) { |
200 return WebPEncodingSetError(pic, VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY); | 302 return WebPEncodingSetError(pic, VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY); |
201 } | 303 } |
202 return 1; | 304 return 1; |
203 } | 305 } |
204 | 306 |
205 #endif /* WEBP_EXPERIMENTAL_FEATURES */ | 307 #endif /* WEBP_EXPERIMENTAL_FEATURES */ |
206 | 308 |
207 //------------------------------------------------------------------------------ | 309 //------------------------------------------------------------------------------ |
208 | 310 |
209 static size_t GeneratePartition0(VP8Encoder* const enc) { | 311 static size_t GeneratePartition0(VP8Encoder* const enc) { |
210 VP8BitWriter* const bw = &enc->bw_; | 312 VP8BitWriter* const bw = &enc->bw_; |
211 const int mb_size = enc->mb_w_ * enc->mb_h_; | 313 const int mb_size = enc->mb_w_ * enc->mb_h_; |
212 uint64_t pos1, pos2, pos3; | 314 uint64_t pos1, pos2, pos3; |
213 #ifdef WEBP_EXPERIMENTAL_FEATURES | 315 #ifdef WEBP_EXPERIMENTAL_FEATURES |
214 const int need_extensions = enc->has_alpha_ || enc->use_layer_; | 316 const int need_extensions = enc->use_layer_; |
215 #endif | 317 #endif |
216 | 318 |
217 pos1 = VP8BitWriterPos(bw); | 319 pos1 = VP8BitWriterPos(bw); |
218 VP8BitWriterInit(bw, mb_size * 7 / 8); // ~7 bits per macroblock | 320 VP8BitWriterInit(bw, mb_size * 7 / 8); // ~7 bits per macroblock |
219 #ifdef WEBP_EXPERIMENTAL_FEATURES | 321 #ifdef WEBP_EXPERIMENTAL_FEATURES |
220 VP8PutBitUniform(bw, need_extensions); // extensions | 322 VP8PutBitUniform(bw, need_extensions); // extensions |
221 #else | 323 #else |
222 VP8PutBitUniform(bw, 0); // colorspace | 324 VP8PutBitUniform(bw, 0); // colorspace |
223 #endif | 325 #endif |
224 VP8PutBitUniform(bw, 0); // clamp type | 326 VP8PutBitUniform(bw, 0); // clamp type |
(...skipping 18 matching lines...) Expand all Loading... |
243 | 345 |
244 if (enc->pic_->stats) { | 346 if (enc->pic_->stats) { |
245 enc->pic_->stats->header_bytes[0] = (int)((pos2 - pos1 + 7) >> 3); | 347 enc->pic_->stats->header_bytes[0] = (int)((pos2 - pos1 + 7) >> 3); |
246 enc->pic_->stats->header_bytes[1] = (int)((pos3 - pos2 + 7) >> 3); | 348 enc->pic_->stats->header_bytes[1] = (int)((pos3 - pos2 + 7) >> 3); |
247 enc->pic_->stats->alpha_data_size = (int)enc->alpha_data_size_; | 349 enc->pic_->stats->alpha_data_size = (int)enc->alpha_data_size_; |
248 enc->pic_->stats->layer_data_size = (int)enc->layer_data_size_; | 350 enc->pic_->stats->layer_data_size = (int)enc->layer_data_size_; |
249 } | 351 } |
250 return !bw->error_; | 352 return !bw->error_; |
251 } | 353 } |
252 | 354 |
| 355 void VP8EncFreeBitWriters(VP8Encoder* const enc) { |
| 356 int p; |
| 357 VP8BitWriterWipeOut(&enc->bw_); |
| 358 for (p = 0; p < enc->num_parts_; ++p) { |
| 359 VP8BitWriterWipeOut(enc->parts_ + p); |
| 360 } |
| 361 } |
| 362 |
253 int VP8EncWrite(VP8Encoder* const enc) { | 363 int VP8EncWrite(VP8Encoder* const enc) { |
254 WebPPicture* const pic = enc->pic_; | 364 WebPPicture* const pic = enc->pic_; |
255 VP8BitWriter* const bw = &enc->bw_; | 365 VP8BitWriter* const bw = &enc->bw_; |
| 366 const int task_percent = 19; |
| 367 const int percent_per_part = task_percent / enc->num_parts_; |
| 368 const int final_percent = enc->percent_ + task_percent; |
256 int ok = 0; | 369 int ok = 0; |
257 size_t coded_size, pad; | 370 size_t vp8_size, pad, riff_size; |
258 int p; | 371 int p; |
259 | 372 |
260 // Partition #0 with header and partition sizes | 373 // Partition #0 with header and partition sizes |
261 ok = !!GeneratePartition0(enc); | 374 ok = !!GeneratePartition0(enc); |
262 | 375 |
263 // Compute total size (for the RIFF header) | 376 // Compute VP8 size |
264 coded_size = KHEADER_SIZE + VP8BitWriterSize(bw) + 3 * (enc->num_parts_ - 1); | 377 vp8_size = VP8_FRAME_HEADER_SIZE + |
| 378 VP8BitWriterSize(bw) + |
| 379 3 * (enc->num_parts_ - 1); |
265 for (p = 0; p < enc->num_parts_; ++p) { | 380 for (p = 0; p < enc->num_parts_; ++p) { |
266 coded_size += VP8BitWriterSize(enc->parts_ + p); | 381 vp8_size += VP8BitWriterSize(enc->parts_ + p); |
267 } | 382 } |
268 pad = coded_size & 1; | 383 pad = vp8_size & 1; |
269 coded_size += pad; | 384 vp8_size += pad; |
| 385 |
| 386 // Compute RIFF size |
| 387 // At the minimum it is: "WEBPVP8 nnnn" + VP8 data size. |
| 388 riff_size = TAG_SIZE + CHUNK_HEADER_SIZE + vp8_size; |
| 389 if (IsVP8XNeeded(enc)) { // Add size for: VP8X header + data. |
| 390 riff_size += CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE; |
| 391 } |
| 392 if (enc->has_alpha_) { // Add size for: ALPH header + data. |
| 393 const uint32_t padded_alpha_size = enc->alpha_data_size_ + |
| 394 (enc->alpha_data_size_ & 1); |
| 395 riff_size += CHUNK_HEADER_SIZE + padded_alpha_size; |
| 396 } |
| 397 // Sanity check. |
| 398 if (riff_size > 0xfffffffeU) { |
| 399 return WebPEncodingSetError(pic, VP8_ENC_ERROR_FILE_TOO_BIG); |
| 400 } |
270 | 401 |
271 // Emit headers and partition #0 | 402 // Emit headers and partition #0 |
272 { | 403 { |
273 const uint8_t* const part0 = VP8BitWriterBuf(bw); | 404 const uint8_t* const part0 = VP8BitWriterBuf(bw); |
274 const size_t size0 = VP8BitWriterSize(bw); | 405 const size_t size0 = VP8BitWriterSize(bw); |
275 ok = ok && PutHeader(enc->profile_, size0, coded_size, pic) | 406 ok = ok && PutWebPHeaders(enc, size0, vp8_size, riff_size) |
276 && pic->writer(part0, size0, pic) | 407 && pic->writer(part0, size0, pic) |
277 && EmitPartitionsSize(enc, pic); | 408 && EmitPartitionsSize(enc, pic); |
278 free((void*)part0); | 409 VP8BitWriterWipeOut(bw); // will free the internal buffer. |
279 } | 410 } |
280 | 411 |
281 // Token partitions | 412 // Token partitions |
282 for (p = 0; p < enc->num_parts_; ++p) { | 413 for (p = 0; p < enc->num_parts_; ++p) { |
283 const uint8_t* const buf = VP8BitWriterBuf(enc->parts_ + p); | 414 const uint8_t* const buf = VP8BitWriterBuf(enc->parts_ + p); |
284 const size_t size = VP8BitWriterSize(enc->parts_ + p); | 415 const size_t size = VP8BitWriterSize(enc->parts_ + p); |
285 if (size) | 416 if (size) |
286 ok = ok && pic->writer(buf, size, pic); | 417 ok = ok && pic->writer(buf, size, pic); |
287 free((void*)buf); | 418 VP8BitWriterWipeOut(enc->parts_ + p); // will free the internal buffer. |
| 419 ok = ok && WebPReportProgress(pic, enc->percent_ + percent_per_part, |
| 420 &enc->percent_); |
288 } | 421 } |
289 | 422 |
290 // Padding byte | 423 // Padding byte |
291 if (ok && pad) { | 424 if (ok && pad) { |
292 const uint8_t pad_byte[1] = { 0 }; | 425 ok = PutPaddingByte(pic); |
293 ok = pic->writer(pad_byte, 1, pic); | |
294 } | 426 } |
295 | 427 |
296 enc->coded_size_ = (int)coded_size + KRIFF_SIZE; | 428 enc->coded_size_ = (int)(CHUNK_HEADER_SIZE + riff_size); |
| 429 ok = ok && WebPReportProgress(pic, final_percent, &enc->percent_); |
297 return ok; | 430 return ok; |
298 } | 431 } |
299 | 432 |
300 //------------------------------------------------------------------------------ | 433 //------------------------------------------------------------------------------ |
301 | 434 |
302 #if defined(__cplusplus) || defined(c_plusplus) | 435 #if defined(__cplusplus) || defined(c_plusplus) |
303 } // extern "C" | 436 } // extern "C" |
304 #endif | 437 #endif |
OLD | NEW |