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