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