/src/libwebp/src/enc/syntax_enc.c
Line | Count | Source |
1 | | // Copyright 2011 Google Inc. All Rights Reserved. |
2 | | // |
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 |
5 | | // tree. An additional intellectual property rights grant can be found |
6 | | // in the file PATENTS. All contributing project authors may |
7 | | // be found in the AUTHORS file in the root of the source tree. |
8 | | // ----------------------------------------------------------------------------- |
9 | | // |
10 | | // Header syntax writing |
11 | | // |
12 | | // Author: Skal (pascal.massimino@gmail.com) |
13 | | |
14 | | #include <assert.h> |
15 | | #include <stddef.h> |
16 | | |
17 | | #include "src/dec/common_dec.h" |
18 | | #include "src/enc/vp8i_enc.h" |
19 | | #include "src/utils/bit_writer_utils.h" |
20 | | #include "src/utils/utils.h" |
21 | | #include "src/webp/encode.h" |
22 | | #include "src/webp/format_constants.h" // RIFF constants |
23 | | #include "src/webp/mux_types.h" // ALPHA_FLAG |
24 | | #include "src/webp/types.h" |
25 | | |
26 | | //------------------------------------------------------------------------------ |
27 | | // Helper functions |
28 | | |
29 | 183k | static int IsVP8XNeeded(const VP8Encoder* const enc) { |
30 | 183k | return !!enc->has_alpha; // Currently the only case when VP8X is needed. |
31 | | // This could change in the future. |
32 | 183k | } |
33 | | |
34 | 74.5k | static int PutPaddingByte(const WebPPicture* const pic) { |
35 | 74.5k | const uint8_t pad_byte[1] = {0}; |
36 | 74.5k | return !!pic->writer(pad_byte, 1, pic); |
37 | 74.5k | } |
38 | | |
39 | | //------------------------------------------------------------------------------ |
40 | | // Writers for header's various pieces (in order of appearance) |
41 | | |
42 | | static WebPEncodingError PutRIFFHeader(const VP8Encoder* const enc, |
43 | 91.7k | size_t riff_size) { |
44 | 91.7k | const WebPPicture* const pic = enc->pic; |
45 | 91.7k | uint8_t riff[RIFF_HEADER_SIZE] = {'R', 'I', 'F', 'F', 0, 0, |
46 | 91.7k | 0, 0, 'W', 'E', 'B', 'P'}; |
47 | 91.7k | assert(riff_size == (uint32_t)riff_size); |
48 | 91.7k | PutLE32(riff + TAG_SIZE, (uint32_t)riff_size); |
49 | 91.7k | if (!pic->writer(riff, sizeof(riff), pic)) { |
50 | 0 | return VP8_ENC_ERROR_BAD_WRITE; |
51 | 0 | } |
52 | 91.7k | return VP8_ENC_OK; |
53 | 91.7k | } |
54 | | |
55 | 50.7k | static WebPEncodingError PutVP8XHeader(const VP8Encoder* const enc) { |
56 | 50.7k | const WebPPicture* const pic = enc->pic; |
57 | 50.7k | uint8_t vp8x[CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE] = {'V', 'P', '8', 'X'}; |
58 | 50.7k | uint32_t flags = 0; |
59 | | |
60 | 50.7k | assert(IsVP8XNeeded(enc)); |
61 | 50.7k | assert(pic->width >= 1 && pic->height >= 1); |
62 | 50.7k | assert(pic->width <= MAX_CANVAS_SIZE && pic->height <= MAX_CANVAS_SIZE); |
63 | | |
64 | 50.7k | if (enc->has_alpha) { |
65 | 50.7k | flags |= ALPHA_FLAG; |
66 | 50.7k | } |
67 | | |
68 | 50.7k | PutLE32(vp8x + TAG_SIZE, VP8X_CHUNK_SIZE); |
69 | 50.7k | PutLE32(vp8x + CHUNK_HEADER_SIZE, flags); |
70 | 50.7k | PutLE24(vp8x + CHUNK_HEADER_SIZE + 4, pic->width - 1); |
71 | 50.7k | PutLE24(vp8x + CHUNK_HEADER_SIZE + 7, pic->height - 1); |
72 | 50.7k | if (!pic->writer(vp8x, sizeof(vp8x), pic)) { |
73 | 0 | return VP8_ENC_ERROR_BAD_WRITE; |
74 | 0 | } |
75 | 50.7k | return VP8_ENC_OK; |
76 | 50.7k | } |
77 | | |
78 | 50.7k | static WebPEncodingError PutAlphaChunk(const VP8Encoder* const enc) { |
79 | 50.7k | const WebPPicture* const pic = enc->pic; |
80 | 50.7k | uint8_t alpha_chunk_hdr[CHUNK_HEADER_SIZE] = {'A', 'L', 'P', 'H'}; |
81 | | |
82 | 50.7k | assert(enc->has_alpha); |
83 | | |
84 | | // Alpha chunk header. |
85 | 50.7k | PutLE32(alpha_chunk_hdr + TAG_SIZE, enc->alpha_data_size); |
86 | 50.7k | if (!pic->writer(alpha_chunk_hdr, sizeof(alpha_chunk_hdr), pic)) { |
87 | 0 | return VP8_ENC_ERROR_BAD_WRITE; |
88 | 0 | } |
89 | | |
90 | | // Alpha chunk data. |
91 | 50.7k | if (!pic->writer(enc->alpha_data, enc->alpha_data_size, pic)) { |
92 | 0 | return VP8_ENC_ERROR_BAD_WRITE; |
93 | 0 | } |
94 | | |
95 | | // Padding. |
96 | 50.7k | if ((enc->alpha_data_size & 1) && !PutPaddingByte(pic)) { |
97 | 0 | return VP8_ENC_ERROR_BAD_WRITE; |
98 | 0 | } |
99 | 50.7k | return VP8_ENC_OK; |
100 | 50.7k | } |
101 | | |
102 | | static WebPEncodingError PutVP8Header(const WebPPicture* const pic, |
103 | 91.7k | size_t vp8_size) { |
104 | 91.7k | uint8_t vp8_chunk_hdr[CHUNK_HEADER_SIZE] = {'V', 'P', '8', ' '}; |
105 | 91.7k | assert(vp8_size == (uint32_t)vp8_size); |
106 | 91.7k | PutLE32(vp8_chunk_hdr + TAG_SIZE, (uint32_t)vp8_size); |
107 | 91.7k | if (!pic->writer(vp8_chunk_hdr, sizeof(vp8_chunk_hdr), pic)) { |
108 | 0 | return VP8_ENC_ERROR_BAD_WRITE; |
109 | 0 | } |
110 | 91.7k | return VP8_ENC_OK; |
111 | 91.7k | } |
112 | | |
113 | | static WebPEncodingError PutVP8FrameHeader(const WebPPicture* const pic, |
114 | 91.7k | int profile, size_t size0) { |
115 | 91.7k | uint8_t vp8_frm_hdr[VP8_FRAME_HEADER_SIZE]; |
116 | 91.7k | uint32_t bits; |
117 | | |
118 | 91.7k | if (size0 >= VP8_MAX_PARTITION0_SIZE) { // partition #0 is too big to fit |
119 | 0 | return VP8_ENC_ERROR_PARTITION0_OVERFLOW; |
120 | 0 | } |
121 | | |
122 | | // Paragraph 9.1. |
123 | 91.7k | bits = 0 // keyframe (1b) |
124 | 91.7k | | (profile << 1) // profile (3b) |
125 | 91.7k | | (1 << 4) // visible (1b) |
126 | 91.7k | | ((uint32_t)size0 << 5); // partition length (19b) |
127 | 91.7k | vp8_frm_hdr[0] = (bits >> 0) & 0xff; |
128 | 91.7k | vp8_frm_hdr[1] = (bits >> 8) & 0xff; |
129 | 91.7k | vp8_frm_hdr[2] = (bits >> 16) & 0xff; |
130 | | // signature |
131 | 91.7k | vp8_frm_hdr[3] = (VP8_SIGNATURE >> 16) & 0xff; |
132 | 91.7k | vp8_frm_hdr[4] = (VP8_SIGNATURE >> 8) & 0xff; |
133 | 91.7k | vp8_frm_hdr[5] = (VP8_SIGNATURE >> 0) & 0xff; |
134 | | // dimensions |
135 | 91.7k | vp8_frm_hdr[6] = pic->width & 0xff; |
136 | 91.7k | vp8_frm_hdr[7] = pic->width >> 8; |
137 | 91.7k | vp8_frm_hdr[8] = pic->height & 0xff; |
138 | 91.7k | vp8_frm_hdr[9] = pic->height >> 8; |
139 | | |
140 | 91.7k | if (!pic->writer(vp8_frm_hdr, sizeof(vp8_frm_hdr), pic)) { |
141 | 0 | return VP8_ENC_ERROR_BAD_WRITE; |
142 | 0 | } |
143 | 91.7k | return VP8_ENC_OK; |
144 | 91.7k | } |
145 | | |
146 | | // WebP Headers. |
147 | | static int PutWebPHeaders(const VP8Encoder* const enc, size_t size0, |
148 | 91.7k | size_t vp8_size, size_t riff_size) { |
149 | 91.7k | WebPPicture* const pic = enc->pic; |
150 | 91.7k | WebPEncodingError err = VP8_ENC_OK; |
151 | | |
152 | | // RIFF header. |
153 | 91.7k | err = PutRIFFHeader(enc, riff_size); |
154 | 91.7k | if (err != VP8_ENC_OK) goto Error; |
155 | | |
156 | | // VP8X. |
157 | 91.7k | if (IsVP8XNeeded(enc)) { |
158 | 50.7k | err = PutVP8XHeader(enc); |
159 | 50.7k | if (err != VP8_ENC_OK) goto Error; |
160 | 50.7k | } |
161 | | |
162 | | // Alpha. |
163 | 91.7k | if (enc->has_alpha) { |
164 | 50.7k | err = PutAlphaChunk(enc); |
165 | 50.7k | if (err != VP8_ENC_OK) goto Error; |
166 | 50.7k | } |
167 | | |
168 | | // VP8 header. |
169 | 91.7k | err = PutVP8Header(pic, vp8_size); |
170 | 91.7k | if (err != VP8_ENC_OK) goto Error; |
171 | | |
172 | | // VP8 frame header. |
173 | 91.7k | err = PutVP8FrameHeader(pic, enc->profile, size0); |
174 | 91.7k | if (err != VP8_ENC_OK) goto Error; |
175 | | |
176 | | // All OK. |
177 | 91.7k | return 1; |
178 | | |
179 | | // Error. |
180 | 0 | Error: |
181 | 0 | return WebPEncodingSetError(pic, err); |
182 | 91.7k | } |
183 | | |
184 | | // Segmentation header |
185 | | static void PutSegmentHeader(VP8BitWriter* const bw, |
186 | 91.7k | const VP8Encoder* const enc) { |
187 | 91.7k | const VP8EncSegmentHeader* const hdr = &enc->segment_hdr; |
188 | 91.7k | const VP8EncProba* const proba = &enc->proba; |
189 | 91.7k | if (VP8PutBitUniform(bw, (hdr->num_segments > 1))) { |
190 | | // We always 'update' the quant and filter strength values |
191 | 31.1k | const int update_data = 1; |
192 | 31.1k | int s; |
193 | 31.1k | VP8PutBitUniform(bw, hdr->update_map); |
194 | 31.1k | if (VP8PutBitUniform(bw, update_data)) { |
195 | | // we always use absolute values, not relative ones |
196 | 31.1k | VP8PutBitUniform(bw, 1); // (segment_feature_mode = 1. Paragraph 9.3.) |
197 | 155k | for (s = 0; s < NUM_MB_SEGMENTS; ++s) { |
198 | 124k | VP8PutSignedBits(bw, enc->dqm[s].quant, 7); |
199 | 124k | } |
200 | 155k | for (s = 0; s < NUM_MB_SEGMENTS; ++s) { |
201 | 124k | VP8PutSignedBits(bw, enc->dqm[s].fstrength, 6); |
202 | 124k | } |
203 | 31.1k | } |
204 | 31.1k | if (hdr->update_map) { |
205 | 120k | for (s = 0; s < 3; ++s) { |
206 | 90.4k | if (VP8PutBitUniform(bw, (proba->segments[s] != 255u))) { |
207 | 45.4k | VP8PutBits(bw, proba->segments[s], 8); |
208 | 45.4k | } |
209 | 90.4k | } |
210 | 30.1k | } |
211 | 31.1k | } |
212 | 91.7k | } |
213 | | |
214 | | // Filtering parameters header |
215 | | static void PutFilterHeader(VP8BitWriter* const bw, |
216 | 91.7k | const VP8EncFilterHeader* const hdr) { |
217 | 91.7k | const int use_lf_delta = (hdr->i4x4_lf_delta != 0); |
218 | 91.7k | VP8PutBitUniform(bw, hdr->simple); |
219 | 91.7k | VP8PutBits(bw, hdr->level, 6); |
220 | 91.7k | VP8PutBits(bw, hdr->sharpness, 3); |
221 | 91.7k | if (VP8PutBitUniform(bw, use_lf_delta)) { |
222 | | // '0' is the default value for i4x4_lf_delta at frame #0. |
223 | 0 | const int need_update = (hdr->i4x4_lf_delta != 0); |
224 | 0 | if (VP8PutBitUniform(bw, need_update)) { |
225 | | // we don't use ref_lf_delta => emit four 0 bits |
226 | 0 | VP8PutBits(bw, 0, 4); |
227 | | // we use mode_lf_delta for i4x4 |
228 | 0 | VP8PutSignedBits(bw, hdr->i4x4_lf_delta, 6); |
229 | 0 | VP8PutBits(bw, 0, 3); // all others unused |
230 | 0 | } |
231 | 0 | } |
232 | 91.7k | } |
233 | | |
234 | | // Nominal quantization parameters |
235 | 91.7k | static void PutQuant(VP8BitWriter* const bw, const VP8Encoder* const enc) { |
236 | 91.7k | VP8PutBits(bw, enc->base_quant, 7); |
237 | 91.7k | VP8PutSignedBits(bw, enc->dq_y1_dc, 4); |
238 | 91.7k | VP8PutSignedBits(bw, enc->dq_y2_dc, 4); |
239 | 91.7k | VP8PutSignedBits(bw, enc->dq_y2_ac, 4); |
240 | 91.7k | VP8PutSignedBits(bw, enc->dq_uv_dc, 4); |
241 | 91.7k | VP8PutSignedBits(bw, enc->dq_uv_ac, 4); |
242 | 91.7k | } |
243 | | |
244 | | // Partition sizes |
245 | | static int EmitPartitionsSize(const VP8Encoder* const enc, |
246 | 91.7k | WebPPicture* const pic) { |
247 | 91.7k | uint8_t buf[3 * (MAX_NUM_PARTITIONS - 1)]; |
248 | 91.7k | int p; |
249 | 282k | for (p = 0; p < enc->num_parts - 1; ++p) { |
250 | 190k | const size_t part_size = VP8BitWriterSize(enc->parts + p); |
251 | 190k | if (part_size >= VP8_MAX_PARTITION_SIZE) { |
252 | 0 | return WebPEncodingSetError(pic, VP8_ENC_ERROR_PARTITION_OVERFLOW); |
253 | 0 | } |
254 | 190k | buf[3 * p + 0] = (part_size >> 0) & 0xff; |
255 | 190k | buf[3 * p + 1] = (part_size >> 8) & 0xff; |
256 | 190k | buf[3 * p + 2] = (part_size >> 16) & 0xff; |
257 | 190k | } |
258 | 91.7k | if (p && !pic->writer(buf, 3 * p, pic)) { |
259 | 0 | return WebPEncodingSetError(pic, VP8_ENC_ERROR_BAD_WRITE); |
260 | 0 | } |
261 | 91.7k | return 1; |
262 | 91.7k | } |
263 | | |
264 | | //------------------------------------------------------------------------------ |
265 | | |
266 | 91.7k | static int GeneratePartition0(VP8Encoder* const enc) { |
267 | 91.7k | VP8BitWriter* const bw = &enc->bw; |
268 | 91.7k | const int mb_size = enc->mb_w * enc->mb_h; |
269 | 91.7k | uint64_t pos1, pos2, pos3; |
270 | | |
271 | 91.7k | pos1 = VP8BitWriterPos(bw); |
272 | 91.7k | if (!VP8BitWriterInit(bw, mb_size * 7 / 8)) { // ~7 bits per macroblock |
273 | 0 | return WebPEncodingSetError(enc->pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
274 | 0 | } |
275 | 91.7k | VP8PutBitUniform(bw, 0); // colorspace |
276 | 91.7k | VP8PutBitUniform(bw, 0); // clamp type |
277 | | |
278 | 91.7k | PutSegmentHeader(bw, enc); |
279 | 91.7k | PutFilterHeader(bw, &enc->filter_hdr); |
280 | 91.7k | VP8PutBits(bw, |
281 | 91.7k | enc->num_parts == 8 ? 3 |
282 | 91.7k | : enc->num_parts == 4 ? 2 |
283 | 71.6k | : enc->num_parts == 2 ? 1 |
284 | 59.0k | : 0, |
285 | 91.7k | 2); |
286 | 91.7k | PutQuant(bw, enc); |
287 | 91.7k | VP8PutBitUniform(bw, 0); // no proba update |
288 | 91.7k | VP8WriteProbas(bw, &enc->proba); |
289 | 91.7k | pos2 = VP8BitWriterPos(bw); |
290 | 91.7k | VP8CodeIntraModes(enc); |
291 | 91.7k | VP8BitWriterFinish(bw); |
292 | | |
293 | 91.7k | pos3 = VP8BitWriterPos(bw); |
294 | | |
295 | 91.7k | #if !defined(WEBP_DISABLE_STATS) |
296 | 91.7k | if (enc->pic->stats) { |
297 | 0 | enc->pic->stats->header_bytes[0] = (int)((pos2 - pos1 + 7) >> 3); |
298 | 0 | enc->pic->stats->header_bytes[1] = (int)((pos3 - pos2 + 7) >> 3); |
299 | 0 | enc->pic->stats->alpha_data_size = (int)enc->alpha_data_size; |
300 | 0 | } |
301 | | #else |
302 | | (void)pos1; |
303 | | (void)pos2; |
304 | | (void)pos3; |
305 | | #endif |
306 | 91.7k | if (bw->error) { |
307 | 0 | return WebPEncodingSetError(enc->pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
308 | 0 | } |
309 | 91.7k | return 1; |
310 | 91.7k | } |
311 | | |
312 | 0 | void VP8EncFreeBitWriters(VP8Encoder* const enc) { |
313 | 0 | int p; |
314 | 0 | VP8BitWriterWipeOut(&enc->bw); |
315 | 0 | for (p = 0; p < enc->num_parts; ++p) { |
316 | 0 | VP8BitWriterWipeOut(enc->parts + p); |
317 | 0 | } |
318 | 0 | } |
319 | | |
320 | 91.7k | int VP8EncWrite(VP8Encoder* const enc) { |
321 | 91.7k | WebPPicture* const pic = enc->pic; |
322 | 91.7k | VP8BitWriter* const bw = &enc->bw; |
323 | 91.7k | const int task_percent = 19; |
324 | 91.7k | const int percent_per_part = task_percent / enc->num_parts; |
325 | 91.7k | const int final_percent = enc->percent + task_percent; |
326 | 91.7k | int ok = 0; |
327 | 91.7k | size_t vp8_size, pad, riff_size; |
328 | 91.7k | int p; |
329 | | |
330 | | // Partition #0 with header and partition sizes |
331 | 91.7k | ok = GeneratePartition0(enc); |
332 | 91.7k | if (!ok) return 0; |
333 | | |
334 | | // Compute VP8 size |
335 | 91.7k | vp8_size = |
336 | 91.7k | VP8_FRAME_HEADER_SIZE + VP8BitWriterSize(bw) + 3 * (enc->num_parts - 1); |
337 | 374k | for (p = 0; p < enc->num_parts; ++p) { |
338 | 282k | vp8_size += VP8BitWriterSize(enc->parts + p); |
339 | 282k | } |
340 | 91.7k | pad = vp8_size & 1; |
341 | 91.7k | vp8_size += pad; |
342 | | |
343 | | // Compute RIFF size |
344 | | // At the minimum it is: "WEBPVP8 nnnn" + VP8 data size. |
345 | 91.7k | riff_size = TAG_SIZE + CHUNK_HEADER_SIZE + vp8_size; |
346 | 91.7k | if (IsVP8XNeeded(enc)) { // Add size for: VP8X header + data. |
347 | 50.7k | riff_size += CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE; |
348 | 50.7k | } |
349 | 91.7k | if (enc->has_alpha) { // Add size for: ALPH header + data. |
350 | 50.7k | const uint32_t padded_alpha_size = |
351 | 50.7k | enc->alpha_data_size + (enc->alpha_data_size & 1); |
352 | 50.7k | riff_size += CHUNK_HEADER_SIZE + padded_alpha_size; |
353 | 50.7k | } |
354 | | // RIFF size should fit in 32-bits. |
355 | 91.7k | if (riff_size > 0xfffffffeU) { |
356 | 0 | return WebPEncodingSetError(pic, VP8_ENC_ERROR_FILE_TOO_BIG); |
357 | 0 | } |
358 | | |
359 | | // Emit headers and partition #0 |
360 | 91.7k | { |
361 | 91.7k | const uint8_t* const part0 = VP8BitWriterBuf(bw); |
362 | 91.7k | const size_t size0 = VP8BitWriterSize(bw); |
363 | 91.7k | ok = ok && PutWebPHeaders(enc, size0, vp8_size, riff_size) && |
364 | 91.7k | pic->writer(part0, size0, pic) && EmitPartitionsSize(enc, pic); |
365 | 91.7k | VP8BitWriterWipeOut(bw); // will free the internal buffer. |
366 | 91.7k | } |
367 | | |
368 | | // Token partitions |
369 | 374k | for (p = 0; p < enc->num_parts; ++p) { |
370 | 282k | const uint8_t* const buf = VP8BitWriterBuf(enc->parts + p); |
371 | 282k | const size_t size = VP8BitWriterSize(enc->parts + p); |
372 | 282k | if (size) ok = ok && pic->writer(buf, size, pic); |
373 | 282k | VP8BitWriterWipeOut(enc->parts + p); // will free the internal buffer. |
374 | 282k | ok = ok && WebPReportProgress(pic, enc->percent + percent_per_part, |
375 | 282k | &enc->percent); |
376 | 282k | } |
377 | | |
378 | | // Padding byte |
379 | 91.7k | if (ok && pad) { |
380 | 46.5k | ok = PutPaddingByte(pic); |
381 | 46.5k | } |
382 | | |
383 | 91.7k | enc->coded_size = (int)(CHUNK_HEADER_SIZE + riff_size); |
384 | 91.7k | ok = ok && WebPReportProgress(pic, final_percent, &enc->percent); |
385 | 91.7k | if (!ok) WebPEncodingSetError(pic, VP8_ENC_ERROR_BAD_WRITE); |
386 | 91.7k | return ok; |
387 | 91.7k | } |
388 | | |
389 | | //------------------------------------------------------------------------------ |