ws-webp: codecs/Vp8L decoder (libwebp port) and codecs/Vp8 key frame decoder + libwebp YUV->RGB (fancy / point upsampling); bit-exact on first samples

This commit is contained in:
4DBug committed 2026-10-31 08:39:26 -05:00
1 parent ccb06775c5
commit 4ffb8cd6bd
8 files changed
+2749

No files matched your search

+55
View File
@@ -0,0 +1,55 @@
Copyright (c) 2010, Google Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Google nor the names of its contributors may
be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Additional IP Rights Grant (Patents):
Additional IP Rights Grant (Patents)
------------------------------------
"These implementations" means the copyrightable works that implement the WebM
codecs distributed by Google as part of the WebM Project.
Google hereby grants to you a perpetual, worldwide, non-exclusive, no-charge,
royalty-free, irrevocable (except as stated in this section) patent license to
make, have made, use, offer to sell, sell, import, transfer, and otherwise
run, modify and propagate the contents of these implementations of WebM, where
such license applies only to those patent claims, both currently owned by
Google and acquired in the future, licensable by Google that are necessarily
infringed by these implementations of WebM. This grant does not include claims
that would be infringed only as a consequence of further modification of these
implementations. If you or your agent or exclusive licensee institute or order
or agree to the institution of patent litigation or any other patent
enforcement activity against any entity (including a cross-claim or
counterclaim in a lawsuit) alleging that any of these implementations of WebM
or any code incorporated within any of these implementations of WebM
constitute direct or contributory patent infringement, or inducement of
patent infringement, then any patent rights granted to you under this License
for these implementations of WebM shall terminate as of the date such
litigation is filed.
File diff suppressed because it is too large. Load diff
+180
View File
@@ -0,0 +1,180 @@
--!strict
-- codecs/Vp8/Tables: the constant VP8 tables of RFC 6386 (sections 13.5, 13.4, 11.5, 14.1), copied from libwebp 1.6
-- src/dec/tree_dec.c and src/dec/quant_dec.c (BSD-3-Clause, licenses/libwebp-LICENSE.txt). Flat 1-based arrays.
local Tables = {}
-- default token probabilities [type 4][band 8][ctx 3][proba 11]: index ((t * 8 + b) * 3 + c) * 11 + p + 1
Tables.COEFF_PROBA0 = {
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 253, 136, 254, 255, 228, 219, 128, 128, 128, 128, 128,
189, 129, 242, 255, 227, 213, 255, 219, 128, 128, 128, 106, 126, 227, 252, 214, 209, 255, 255, 128, 128, 128,
1, 98, 248, 255, 236, 226, 255, 255, 128, 128, 128, 181, 133, 238, 254, 221, 234, 255, 154, 128, 128, 128,
78, 134, 202, 247, 198, 180, 255, 219, 128, 128, 128, 1, 185, 249, 255, 243, 255, 128, 128, 128, 128, 128,
184, 150, 247, 255, 236, 224, 128, 128, 128, 128, 128, 77, 110, 216, 255, 236, 230, 128, 128, 128, 128, 128,
1, 101, 251, 255, 241, 255, 128, 128, 128, 128, 128, 170, 139, 241, 252, 236, 209, 255, 255, 128, 128, 128,
37, 116, 196, 243, 228, 255, 255, 255, 128, 128, 128, 1, 204, 254, 255, 245, 255, 128, 128, 128, 128, 128,
207, 160, 250, 255, 238, 128, 128, 128, 128, 128, 128, 102, 103, 231, 255, 211, 171, 128, 128, 128, 128, 128,
1, 152, 252, 255, 240, 255, 128, 128, 128, 128, 128, 177, 135, 243, 255, 234, 225, 128, 128, 128, 128, 128,
80, 129, 211, 255, 194, 224, 128, 128, 128, 128, 128, 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128,
246, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128, 255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
198, 35, 237, 223, 193, 187, 162, 160, 145, 155, 62, 131, 45, 198, 221, 172, 176, 220, 157, 252, 221, 1,
68, 47, 146, 208, 149, 167, 221, 162, 255, 223, 128, 1, 149, 241, 255, 221, 224, 255, 255, 128, 128, 128,
184, 141, 234, 253, 222, 220, 255, 199, 128, 128, 128, 81, 99, 181, 242, 176, 190, 249, 202, 255, 255, 128,
1, 129, 232, 253, 214, 197, 242, 196, 255, 255, 128, 99, 121, 210, 250, 201, 198, 255, 202, 128, 128, 128,
23, 91, 163, 242, 170, 187, 247, 210, 255, 255, 128, 1, 200, 246, 255, 234, 255, 128, 128, 128, 128, 128,
109, 178, 241, 255, 231, 245, 255, 255, 128, 128, 128, 44, 130, 201, 253, 205, 192, 255, 255, 128, 128, 128,
1, 132, 239, 251, 219, 209, 255, 165, 128, 128, 128, 94, 136, 225, 251, 218, 190, 255, 255, 128, 128, 128,
22, 100, 174, 245, 186, 161, 255, 199, 128, 128, 128, 1, 182, 249, 255, 232, 235, 128, 128, 128, 128, 128,
124, 143, 241, 255, 227, 234, 128, 128, 128, 128, 128, 35, 77, 181, 251, 193, 211, 255, 205, 128, 128, 128,
1, 157, 247, 255, 236, 231, 255, 255, 128, 128, 128, 121, 141, 235, 255, 225, 227, 255, 255, 128, 128, 128,
45, 99, 188, 251, 195, 217, 255, 224, 128, 128, 128, 1, 1, 251, 255, 213, 255, 128, 128, 128, 128, 128,
203, 1, 248, 255, 255, 128, 128, 128, 128, 128, 128, 137, 1, 177, 255, 224, 255, 128, 128, 128, 128, 128,
253, 9, 248, 251, 207, 208, 255, 192, 128, 128, 128, 175, 13, 224, 243, 193, 185, 249, 198, 255, 255, 128,
73, 17, 171, 221, 161, 179, 236, 167, 255, 234, 128, 1, 95, 247, 253, 212, 183, 255, 255, 128, 128, 128,
239, 90, 244, 250, 211, 209, 255, 255, 128, 128, 128, 155, 77, 195, 248, 188, 195, 255, 255, 128, 128, 128,
1, 24, 239, 251, 218, 219, 255, 205, 128, 128, 128, 201, 51, 219, 255, 196, 186, 128, 128, 128, 128, 128,
69, 46, 190, 239, 201, 218, 255, 228, 128, 128, 128, 1, 191, 251, 255, 255, 128, 128, 128, 128, 128, 128,
223, 165, 249, 255, 213, 255, 128, 128, 128, 128, 128, 141, 124, 248, 255, 255, 128, 128, 128, 128, 128, 128,
1, 16, 248, 255, 255, 128, 128, 128, 128, 128, 128, 190, 36, 230, 255, 236, 255, 128, 128, 128, 128, 128,
149, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128, 1, 226, 255, 128, 128, 128, 128, 128, 128, 128, 128,
247, 192, 255, 128, 128, 128, 128, 128, 128, 128, 128, 240, 128, 255, 128, 128, 128, 128, 128, 128, 128, 128,
1, 134, 252, 255, 255, 128, 128, 128, 128, 128, 128, 213, 62, 250, 255, 255, 128, 128, 128, 128, 128, 128,
55, 93, 255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
202, 24, 213, 235, 186, 191, 220, 160, 240, 175, 255, 126, 38, 182, 232, 169, 184, 228, 174, 255, 187, 128,
61, 46, 138, 219, 151, 178, 240, 170, 255, 216, 128, 1, 112, 230, 250, 199, 191, 247, 159, 255, 255, 128,
166, 109, 228, 252, 211, 215, 255, 174, 128, 128, 128, 39, 77, 162, 232, 172, 180, 245, 178, 255, 255, 128,
1, 52, 220, 246, 198, 199, 249, 220, 255, 255, 128, 124, 74, 191, 243, 183, 193, 250, 221, 255, 255, 128,
24, 71, 130, 219, 154, 170, 243, 182, 255, 255, 128, 1, 182, 225, 249, 219, 240, 255, 224, 128, 128, 128,
149, 150, 226, 252, 216, 205, 255, 171, 128, 128, 128, 28, 108, 170, 242, 183, 194, 254, 223, 255, 255, 128,
1, 81, 230, 252, 204, 203, 255, 192, 128, 128, 128, 123, 102, 209, 247, 188, 196, 255, 233, 128, 128, 128,
20, 95, 153, 243, 164, 173, 255, 203, 128, 128, 128, 1, 222, 248, 255, 216, 213, 128, 128, 128, 128, 128,
168, 175, 246, 252, 235, 205, 255, 255, 128, 128, 128, 47, 116, 215, 255, 211, 212, 255, 255, 128, 128, 128,
1, 121, 236, 253, 212, 214, 255, 255, 128, 128, 128, 141, 84, 213, 252, 201, 202, 255, 219, 128, 128, 128,
42, 80, 160, 240, 162, 185, 255, 205, 128, 128, 128, 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128,
244, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128, 238, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128,
}
-- token probability update probabilities, same layout
Tables.COEFF_UPDATE_PROBA = {
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 176, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255,
223, 241, 252, 255, 255, 255, 255, 255, 255, 255, 255, 249, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255,
255, 244, 252, 255, 255, 255, 255, 255, 255, 255, 255, 234, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255,
253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 246, 254, 255, 255, 255, 255, 255, 255, 255, 255,
239, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255,
255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255, 251, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255,
251, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255,
255, 254, 253, 255, 254, 255, 255, 255, 255, 255, 255, 250, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255,
254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
217, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 225, 252, 241, 253, 255, 255, 254, 255, 255, 255, 255,
234, 250, 241, 250, 253, 255, 253, 254, 255, 255, 255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255,
223, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 238, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255,
255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255, 249, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255,
247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255,
253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255, 250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
186, 251, 250, 255, 255, 255, 255, 255, 255, 255, 255, 234, 251, 244, 254, 255, 255, 255, 255, 255, 255, 255,
251, 251, 243, 253, 254, 255, 254, 255, 255, 255, 255, 255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255,
236, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, 251, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255,
255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255,
254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 250, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255,
248, 254, 249, 253, 255, 255, 255, 255, 255, 255, 255, 255, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255,
246, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255, 252, 254, 251, 254, 254, 255, 255, 255, 255, 255, 255,
255, 254, 252, 255, 255, 255, 255, 255, 255, 255, 255, 248, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255,
253, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255,
245, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255, 253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255,
255, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255, 252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255,
255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255,
249, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
}
-- 4x4 intra mode probabilities [top 10][left 10][9]: index (top * 10 + left) * 9 + i + 1
Tables.BMODES_PROBA = {
231, 120, 48, 89, 115, 113, 120, 152, 112, 152, 179, 64, 126, 170, 118, 46, 70, 95, 175, 69, 143, 80,
85, 82, 72, 155, 103, 56, 58, 10, 171, 218, 189, 17, 13, 152, 114, 26, 17, 163, 44, 195, 21, 10,
173, 121, 24, 80, 195, 26, 62, 44, 64, 85, 144, 71, 10, 38, 171, 213, 144, 34, 26, 170, 46, 55,
19, 136, 160, 33, 206, 71, 63, 20, 8, 114, 114, 208, 12, 9, 226, 81, 40, 11, 96, 182, 84, 29,
16, 36, 134, 183, 89, 137, 98, 101, 106, 165, 148, 72, 187, 100, 130, 157, 111, 32, 75, 80, 66, 102,
167, 99, 74, 62, 40, 234, 128, 41, 53, 9, 178, 241, 141, 26, 8, 107, 74, 43, 26, 146, 73, 166,
49, 23, 157, 65, 38, 105, 160, 51, 52, 31, 115, 128, 104, 79, 12, 27, 217, 255, 87, 17, 7, 87,
68, 71, 44, 114, 51, 15, 186, 23, 47, 41, 14, 110, 182, 183, 21, 17, 194, 66, 45, 25, 102, 197,
189, 23, 18, 22, 88, 88, 147, 150, 42, 46, 45, 196, 205, 43, 97, 183, 117, 85, 38, 35, 179, 61,
39, 53, 200, 87, 26, 21, 43, 232, 171, 56, 34, 51, 104, 114, 102, 29, 93, 77, 39, 28, 85, 171,
58, 165, 90, 98, 64, 34, 22, 116, 206, 23, 34, 43, 166, 73, 107, 54, 32, 26, 51, 1, 81, 43,
31, 68, 25, 106, 22, 64, 171, 36, 225, 114, 34, 19, 21, 102, 132, 188, 16, 76, 124, 62, 18, 78,
95, 85, 57, 50, 48, 51, 193, 101, 35, 159, 215, 111, 89, 46, 111, 60, 148, 31, 172, 219, 228, 21,
18, 111, 112, 113, 77, 85, 179, 255, 38, 120, 114, 40, 42, 1, 196, 245, 209, 10, 25, 109, 88, 43,
29, 140, 166, 213, 37, 43, 154, 61, 63, 30, 155, 67, 45, 68, 1, 209, 100, 80, 8, 43, 154, 1,
51, 26, 71, 142, 78, 78, 16, 255, 128, 34, 197, 171, 41, 40, 5, 102, 211, 183, 4, 1, 221, 51,
50, 17, 168, 209, 192, 23, 25, 82, 138, 31, 36, 171, 27, 166, 38, 44, 229, 67, 87, 58, 169, 82,
115, 26, 59, 179, 63, 59, 90, 180, 59, 166, 93, 73, 154, 40, 40, 21, 116, 143, 209, 34, 39, 175,
47, 15, 16, 183, 34, 223, 49, 45, 183, 46, 17, 33, 183, 6, 98, 15, 32, 183, 57, 46, 22, 24,
128, 1, 54, 17, 37, 65, 32, 73, 115, 28, 128, 23, 128, 205, 40, 3, 9, 115, 51, 192, 18, 6,
223, 87, 37, 9, 115, 59, 77, 64, 21, 47, 104, 55, 44, 218, 9, 54, 53, 130, 226, 64, 90, 70,
205, 40, 41, 23, 26, 57, 54, 57, 112, 184, 5, 41, 38, 166, 213, 30, 34, 26, 133, 152, 116, 10,
32, 134, 39, 19, 53, 221, 26, 114, 32, 73, 255, 31, 9, 65, 234, 2, 15, 1, 118, 73, 75, 32,
12, 51, 192, 255, 160, 43, 51, 88, 31, 35, 67, 102, 85, 55, 186, 85, 56, 21, 23, 111, 59, 205,
45, 37, 192, 55, 38, 70, 124, 73, 102, 1, 34, 98, 125, 98, 42, 88, 104, 85, 117, 175, 82, 95,
84, 53, 89, 128, 100, 113, 101, 45, 75, 79, 123, 47, 51, 128, 81, 171, 1, 57, 17, 5, 71, 102,
57, 53, 41, 49, 38, 33, 13, 121, 57, 73, 26, 1, 85, 41, 10, 67, 138, 77, 110, 90, 47, 114,
115, 21, 2, 10, 102, 255, 166, 23, 6, 101, 29, 16, 10, 85, 128, 101, 196, 26, 57, 18, 10, 102,
102, 213, 34, 20, 43, 117, 20, 15, 36, 163, 128, 68, 1, 26, 102, 61, 71, 37, 34, 53, 31, 243,
192, 69, 60, 71, 38, 73, 119, 28, 222, 37, 68, 45, 128, 34, 1, 47, 11, 245, 171, 62, 17, 19,
70, 146, 85, 55, 62, 70, 37, 43, 37, 154, 100, 163, 85, 160, 1, 63, 9, 92, 136, 28, 64, 32,
201, 85, 75, 15, 9, 9, 64, 255, 184, 119, 16, 86, 6, 28, 5, 64, 255, 25, 248, 1, 56, 8,
17, 132, 137, 255, 55, 116, 128, 58, 15, 20, 82, 135, 57, 26, 121, 40, 164, 50, 31, 137, 154, 133,
25, 35, 218, 51, 103, 44, 131, 131, 123, 31, 6, 158, 86, 40, 64, 135, 148, 224, 45, 183, 128, 22,
26, 17, 131, 240, 154, 14, 1, 209, 45, 16, 21, 91, 64, 222, 7, 1, 197, 56, 21, 39, 155, 60,
138, 23, 102, 213, 83, 12, 13, 54, 192, 255, 68, 47, 28, 85, 26, 85, 85, 128, 128, 32, 146, 171,
18, 11, 7, 63, 144, 171, 4, 4, 246, 35, 27, 10, 146, 174, 171, 12, 26, 128, 190, 80, 35, 99,
180, 80, 126, 54, 45, 85, 126, 47, 87, 176, 51, 41, 20, 32, 101, 75, 128, 139, 118, 146, 116, 128,
85, 56, 41, 15, 176, 236, 85, 37, 9, 62, 71, 30, 17, 119, 118, 255, 17, 18, 138, 101, 38, 60,
138, 55, 70, 43, 26, 142, 146, 36, 19, 30, 171, 255, 97, 27, 20, 138, 45, 61, 62, 219, 1, 81,
188, 64, 32, 41, 20, 117, 151, 142, 20, 21, 163, 112, 19, 12, 61, 195, 128, 48, 4, 24,
}
-- DC dequantization factors, index q + 1 (q 0..127)
Tables.DC_TABLE = {
4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13, 14, 15, 16, 17, 17, 18, 19, 20, 20, 21, 21,
22, 22, 23, 23, 24, 25, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 76, 77, 78, 79, 80, 81,
82, 83, 84, 85, 86, 87, 88, 89, 91, 93, 95, 96, 98, 100, 101, 102, 104, 106, 108, 110, 112, 114,
116, 118, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 143, 145, 148, 151, 154, 157,
}
-- AC dequantization factors, index q + 1
Tables.AC_TABLE = {
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80,
82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 119, 122, 125, 128,
131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 161, 164, 167, 170, 173, 177, 181, 185, 189, 193, 197, 201,
205, 209, 213, 217, 221, 225, 229, 234, 239, 245, 249, 254, 259, 264, 269, 274, 279, 284,
}
-- coefficient band of each position (with a sentinel for position 16)
Tables.BANDS = { 0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 0 }
-- zigzag scan: coefficient n goes to position ZIGZAG[n + 1]
Tables.ZIGZAG = { 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15 }
-- extra bits probabilities for the DCT_CAT3..6 tokens
Tables.CAT3 = { 173, 148, 140 }
Tables.CAT4 = { 176, 155, 140, 135 }
Tables.CAT5 = { 180, 157, 141, 134, 130 }
Tables.CAT6 = { 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129 }
return Tables
+123
View File
@@ -0,0 +1,123 @@
--!strict
--!native
--!optimize 2
-- codecs/Vp8/Yuv: 4:2:0 Y'CbCr (BT.601, limited range) to rgba8 the way libwebp's decoder does it, bit-exact: the
-- 14-bit fixed-point VP8YuvToRgb of src/dsp/yuv.h, with either the "fancy" upsampler (src/dsp/upsampling.c: a 9-3-3-1
-- bilinear filter on the chroma, libwebp's default) or point sampling (dwebp -nofancy). A port of libwebp 1.6
-- (BSD-3-Clause, licenses/libwebp-LICENSE.txt).
local rshift = bit32.rshift
local readu8, writeu8, writeu32 = buffer.readu8, buffer.writeu8, buffer.writeu32
local bcreate = buffer.create
local Yuv = {}
local function clip(v: number): number
-- VP8Clip8: v in 14-bit fixed point
if v >= 0 and v < 16384 then
return rshift(v, 6)
end
return if v < 0 then 0 else 255
end
-- (v * coeff) >> 8 for non-negative v
local function mulHi(v: number, c: number): number
return rshift(v * c, 8)
end
-- rgba(y, u, v) -> the pixel as a little-endian u32 (bytes R G B 255)
local function rgba(y: number, u: number, v: number): number
local yy = mulHi(y, 19077)
local r = clip(yy + mulHi(v, 26149) - 14234)
local g = clip(yy - mulHi(u, 6419) - mulHi(v, 13320) + 8708)
local b = clip(yy + mulHi(u, 33050) - 17685)
return r + g * 256 + b * 65536 + 0xff000000
end
Yuv.rgba = rgba
-- upsamplePair: libwebp UPSAMPLE_FUNC for one or two output rows. ty / by: offsets of the top / bottom luma rows
-- (by < 0: no bottom row), tu / cu: offsets of the top / current chroma rows; dt / db: output row offsets.
local function upsamplePair(Y: buffer, U: buffer, V: buffer, ty: number, by: number, tu: number, cu: number, out: buffer, dt: number, db: number, len: number)
local last = rshift(len - 1, 1)
local tlu, tlv = readu8(U, tu), readu8(V, tu)
local lu, lv = readu8(U, cu), readu8(V, cu)
do
local u0 = rshift(3 * tlu + lu + 2, 2)
local v0 = rshift(3 * tlv + lv + 2, 2)
writeu32(out, dt, rgba(readu8(Y, ty), u0, v0))
end
if by >= 0 then
local u0 = rshift(3 * lu + tlu + 2, 2)
local v0 = rshift(3 * lv + tlv + 2, 2)
writeu32(out, db, rgba(readu8(Y, by), u0, v0))
end
for x = 1, last do
local tu1, tv1 = readu8(U, tu + x), readu8(V, tu + x)
local u1, v1 = readu8(U, cu + x), readu8(V, cu + x)
local avgU = tlu + tu1 + lu + u1 + 8
local avgV = tlv + tv1 + lv + v1 + 8
local d12u = rshift(avgU + 2 * (tu1 + lu), 3)
local d03u = rshift(avgU + 2 * (tlu + u1), 3)
local d12v = rshift(avgV + 2 * (tv1 + lv), 3)
local d03v = rshift(avgV + 2 * (tlv + v1), 3)
local px = 2 * x
writeu32(out, dt + (px - 1) * 4, rgba(readu8(Y, ty + px - 1), rshift(d12u + tlu, 1), rshift(d12v + tlv, 1)))
writeu32(out, dt + px * 4, rgba(readu8(Y, ty + px), rshift(d03u + tu1, 1), rshift(d03v + tv1, 1)))
if by >= 0 then
writeu32(out, db + (px - 1) * 4, rgba(readu8(Y, by + px - 1), rshift(d03u + lu, 1), rshift(d03v + lv, 1)))
writeu32(out, db + px * 4, rgba(readu8(Y, by + px), rshift(d12u + u1, 1), rshift(d12v + v1, 1)))
end
tlu, tlv, lu, lv = tu1, tv1, u1, v1
end
if len % 2 == 0 then
local u0 = rshift(3 * tlu + lu + 2, 2)
local v0 = rshift(3 * tlv + lv + 2, 2)
writeu32(out, dt + (len - 1) * 4, rgba(readu8(Y, ty + len - 1), u0, v0))
if by >= 0 then
u0 = rshift(3 * lu + tlu + 2, 2)
v0 = rshift(3 * lv + tlv + 2, 2)
writeu32(out, db + (len - 1) * 4, rgba(readu8(Y, by + len - 1), u0, v0))
end
end
end
-- toRgba(y, u, v, w, h, fancy, out?) -> rgba8 (alpha 255). Planes are tightly packed (chroma ceil(w / 2) wide).
function Yuv.toRgba(Y: buffer, U: buffer, V: buffer, w: number, h: number, fancy: boolean, out: buffer?): buffer
local o = out or bcreate(w * h * 4)
local cw = (w + 1) // 2
local stride = w * 4
if fancy then
-- libwebp EmitFancyRGB over the whole picture: row 0 alone (chroma mirrored), then pairs of rows between two
-- chroma rows, then the last row of an even height alone
upsamplePair(Y, U, V, 0, -1, 0, 0, o, 0, -1, w)
local y = 1
local cu = 0
while y + 1 < h do
upsamplePair(Y, U, V, y * w, (y + 1) * w, cu, cu + cw, o, y * stride, (y + 1) * stride, w)
cu += cw
y += 2
end
if h % 2 == 0 and h > 1 then
upsamplePair(Y, U, V, (h - 1) * w, -1, cu, cu, o, (h - 1) * stride, -1, w)
end
else
for y = 0, h - 1 do
local cu = (y // 2) * cw
local yo = y * w
local oo = y * stride
for x = 0, w - 1 do
local c = cu + x // 2
writeu32(o, oo + x * 4, rgba(readu8(Y, yo + x), readu8(U, c), readu8(V, c)))
end
end
end
return o
end
-- setAlpha(rgba, a8, n): copy an alpha plane into the A bytes
function Yuv.setAlpha(o: buffer, a: buffer, n: number)
for i = 0, n - 1 do
writeu8(o, i * 4 + 3, readu8(a, i))
end
end
return Yuv
+142
View File
@@ -0,0 +1,142 @@
--!strict
-- codecs/Vp8: the VP8 video codec (RFC 6386), decode only. A codec (layer 1): image/webp uses it for lossy frames, the
-- WebM / Matroska and IVF video tracks can use it too. A port of libwebp 1.6 (BSD-3-Clause,
-- licenses/libwebp-LICENSE.txt), bit-exact with libwebp: the yuv420p planes equal `dwebp -yuv`, and toRgba equals
-- `dwebp -pam` (fancy upsampling) or `dwebp -nofancy -pam`.
-- Supports: key frames (intra 16x16 / 4x4 / chroma prediction, segments, both loop filters, token partitions,
-- probability updates, skip). Not supported yet: inter frames (decodeFrame returns UNSUPPORTED; the decoder object
-- keeps the cross-frame state so they can be added), the horizontal / vertical scale bits (ignored, as libwebp does).
--
-- local Vp8 = require("@formats/codecs/Vp8")
-- local frame, err = Vp8.decodeFrame(buf, off, len) -- a yuv420p Types.Image (+ frame.meta.source.vp8 header)
-- local rgba = Vp8.toRgba(frame) -- rgba8 Image (libwebp's fancy upsampling)
-- local dec = Vp8.newDecoder(); dec:decodeFrame(...) -- a stream of frames (WebM)
--
-- Never throws: bad data returns nil, Err (core/Err codes TRUNCATED / CORRUPT / UNSUPPORTED / LIMIT / ARG).
local Err = require("@formats/core/Err")
local Types = require("@formats/Types")
local Decode = require("@formats/codecs/Vp8/Decode")
local Yuv = require("@formats/codecs/Vp8/Yuv")
local Vp8 = {}
local DEFAULT_MAX_PIXELS = 2 ^ 28
export type Options = {
maxPixels: number?, -- default 2^28
}
local function fail(msg: string?, offset: number?): Err.Err
local m = msg or "bad data"
if string.sub(m, 1, 11) == "truncated: " then
return Err.new(Err.TRUNCATED, string.sub(m, 12), offset)
elseif string.sub(m, 1, 7) == "limit: " then
return Err.new(Err.LIMIT, string.sub(m, 8), offset)
elseif string.sub(m, 1, 13) == "unsupported: " then
return Err.new(Err.UNSUPPORTED, string.sub(m, 14), offset)
end
return Err.new(Err.CORRUPT, m, offset)
end
local function range(buf: buffer, off: number?, len: number?): (number, number)
local n = buffer.len(buf)
local o = math.clamp(off or 0, 0, n)
return o, math.clamp(len or (n - o), 0, n - o)
end
-- info(buf, off?, len?) -> { keyFrame, profile, show, partitionLength, width, height, xscale, yscale } | nil, Err
function Vp8.info(buf: buffer, off: number?, len: number?): (any, Err.Err?)
if type(buf) ~= "buffer" then
return nil, Err.new(Err.ARG, "info: expected a buffer")
end
local o, l = range(buf, off, len)
local h, err = Decode.header(buf, o, l)
if not h then
return nil, fail(err, o)
end
return h, nil
end
local DecoderMeta = {}
DecoderMeta.__index = DecoderMeta
export type Decoder = typeof(setmetatable({} :: { state: Decode.State }, DecoderMeta))
-- newDecoder() -> Decoder: decodes successive frames of one stream
function Vp8.newDecoder(): Decoder
return setmetatable({ state = Decode.new() }, DecoderMeta)
end
-- decoder:decodeFrame(buf, off?, len?, opts?) -> a yuv420p Image | nil, Err
function DecoderMeta.decodeFrame(self: Decoder, buf: buffer, off: number?, len: number?, opts: Options?): (Types.Image?, Err.Err?)
if type(buf) ~= "buffer" then
return nil, Err.new(Err.ARG, "decodeFrame: expected a buffer")
end
local o, l = range(buf, off, len)
local f, err = Decode.decodeFrame(self.state, buf, o, l, opts and opts.maxPixels or DEFAULT_MAX_PIXELS)
if not f then
return nil, fail(err, o)
end
local img = Types.Image.new(f.width, f.height, "yuv420p")
local ny = f.width * f.height
local nc = buffer.len(f.u)
buffer.copy(img.pixels, 0, f.y, 0, ny)
buffer.copy(img.pixels, ny, f.u, 0, nc)
buffer.copy(img.pixels, ny + nc, f.v, 0, nc)
img.meta.source = { format = "vp8", keyFrame = f.header.keyFrame, profile = f.header.profile }
return img, nil
end
-- decodeFrame(buf, off?, len?, opts?) -> a yuv420p Image | nil, Err (one key frame)
function Vp8.decodeFrame(buf: buffer, off: number?, len: number?, opts: Options?): (Types.Image?, Err.Err?)
return Vp8.newDecoder():decodeFrame(buf, off, len, opts)
end
-- decodePlanes(buf, off?, len?, opts?) -> y, u, v, width, height | nil, Err: one key frame as separate planes
function Vp8.decodePlanes(buf: buffer, off: number?, len: number?, opts: Options?): (buffer?, any, buffer?, number?, number?)
if type(buf) ~= "buffer" then
return nil, Err.new(Err.ARG, "decodePlanes: expected a buffer")
end
local o, l = range(buf, off, len)
local f, err = Decode.decodeFrame(Decode.new(), buf, o, l, opts and opts.maxPixels or DEFAULT_MAX_PIXELS)
if not f then
return nil, fail(err, o)
end
return f.y, f.u, f.v, f.width, f.height
end
-- planes(img) -> y, u, v offsets in img.pixels and the chroma width / height
function Vp8.planes(img: Types.Image): (number, number, number, number, number)
local cw, ch = (img.width + 1) // 2, (img.height + 1) // 2
local ny = img.width * img.height
return 0, ny, ny + cw * ch, cw, ch
end
-- toRgba(yuvImage, fancy?) -> rgba8 Image. fancy (default true): libwebp's bilinear chroma upsampling; false: point
-- sampling (dwebp -nofancy)
function Vp8.toRgba(img: Types.Image, fancy: boolean?): (Types.Image?, Err.Err?)
if type(img) ~= "table" or img.format ~= "yuv420p" then
return nil, Err.new(Err.ARG, "toRgba: needs a yuv420p Image")
end
local w, h = img.width, img.height
local _, uo, vo, cw, ch = Vp8.planes(img)
local y = buffer.create(w * h)
local u = buffer.create(cw * ch)
local v = buffer.create(cw * ch)
buffer.copy(y, 0, img.pixels, 0, w * h)
buffer.copy(u, 0, img.pixels, uo, cw * ch)
buffer.copy(v, 0, img.pixels, vo, cw * ch)
local out = Types.Image.new(w, h, "rgba8")
Yuv.toRgba(y, u, v, w, h, fancy ~= false, out.pixels)
out.meta.source = img.meta.source
return out, nil
end
-- yuvToRgba(y, u, v, w, h, fancy?) -> rgba8 pixels from separate planes (alpha 255)
function Vp8.yuvToRgba(y: buffer, u: buffer, v: buffer, w: number, h: number, fancy: boolean?): buffer
return Yuv.toRgba(y, u, v, w, h, fancy ~= false)
end
Vp8.setAlpha = Yuv.setAlpha
return Vp8
+873
View File
@@ -0,0 +1,873 @@
--!strict
--!native
--!optimize 2
-- codecs/Vp8L/Decode: the WebP lossless (VP8L) bitstream decoder: prefix codes (two-level tables), meta prefix codes,
-- LZ77 backward references with the 2-D distance map, the colour cache, and the four inverse transforms (predictor,
-- cross colour, subtract green, colour indexing with pixel bundling). A port of libwebp 1.6 src/dec/vp8l_dec.c,
-- src/utils/huffman_utils.c and src/dsp/lossless.c (BSD-3-Clause, licenses/libwebp-LICENSE.txt); bit-exact with it.
--
-- Pixels are ARGB u32 values (0xAARRGGBB) in a buffer, 4 bytes each, rows top to bottom. Never throws: every
-- function returns nil, "message" (prefixed "truncated: " when the data ends early) on bad data.
local band, bor, bxor, lshift, rshift = bit32.band, bit32.bor, bit32.bxor, bit32.lshift, bit32.rshift
local readu8, readu32, writeu32, writeu8 = buffer.readu8, buffer.readu32, buffer.writeu32, buffer.writeu8
local bcreate, bcopy, blen = buffer.create, buffer.copy, buffer.len
local Decode = {}
local ROOT_BITS = 8
local MAX_LEN = 15
local NUM_LITERAL = 256
local NUM_LENGTH = 24
local NUM_DISTANCE = 40
local MAX_CACHE_BITS = 11
local ARGB_BLACK = 0xff000000
local CODE_LENGTH_ORDER = { 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }
local CODE_TO_PLANE = {
0x18, 0x07, 0x17, 0x19, 0x28, 0x06, 0x27, 0x29, 0x16, 0x1a, 0x26, 0x2a, 0x38, 0x05, 0x37, 0x39, 0x15, 0x1b, 0x36,
0x3a, 0x25, 0x2b, 0x48, 0x04, 0x47, 0x49, 0x14, 0x1c, 0x35, 0x3b, 0x46, 0x4a, 0x24, 0x2c, 0x58, 0x45, 0x4b, 0x34,
0x3c, 0x03, 0x57, 0x59, 0x13, 0x1d, 0x56, 0x5a, 0x23, 0x2d, 0x44, 0x4c, 0x55, 0x5b, 0x33, 0x3d, 0x68, 0x02, 0x67,
0x69, 0x12, 0x1e, 0x66, 0x6a, 0x22, 0x2e, 0x54, 0x5c, 0x43, 0x4d, 0x65, 0x6b, 0x32, 0x3e, 0x78, 0x01, 0x77, 0x79,
0x53, 0x5d, 0x11, 0x1f, 0x64, 0x6c, 0x42, 0x4e, 0x76, 0x7a, 0x21, 0x2f, 0x75, 0x7b, 0x31, 0x3f, 0x63, 0x6d, 0x52,
0x5e, 0x00, 0x74, 0x7c, 0x41, 0x4f, 0x10, 0x20, 0x62, 0x6e, 0x30, 0x73, 0x7d, 0x51, 0x5f, 0x40, 0x72, 0x7e, 0x61,
0x6f, 0x50, 0x71, 0x7f, 0x60, 0x70,
}
-- distance map (x offset 8 - (v & 15), y offset v >> 4) as a plain distance for a row width: done per image
Decode.CODE_TO_PLANE = CODE_TO_PLANE
-- ------------------------------------------------------------------------------------------------ bit reader
-- LSB-first; bytes past the end read as zero and are counted in `over`: the stream is overrun when fewer bits are
-- left in the buffer than phantom bits were added (bitcnt < over * 8).
export type State = {
buf: buffer,
pos: number,
limit: number,
bitbuf: number,
bitcnt: number,
over: number,
-- prefix code tables (u32 entries value * 32 + bits), shared by every group of every level
tab: buffer,
tabUsed: number,
maxPixels: number,
}
local function fill(d: State)
local bitbuf, bitcnt = d.bitbuf, d.bitcnt
while bitcnt <= 24 do
local p = d.pos
local b = 0
if p < d.limit then
b = readu8(d.buf, p)
d.pos = p + 1
else
d.over += 1
end
bitbuf += lshift(b, bitcnt)
bitcnt += 8
end
d.bitbuf, d.bitcnt = bitbuf, bitcnt
end
-- readBits(d, n) n <= 24
local function readBits(d: State, n: number): number
if d.bitcnt < n then
fill(d)
end
local v = band(d.bitbuf, lshift(1, n) - 1)
d.bitbuf = rshift(d.bitbuf, n)
d.bitcnt -= n
return v
end
local function eos(d: State): boolean
return d.bitcnt < d.over * 8
end
-- ------------------------------------------------------------------------------------------------ prefix codes
local count = table.create(MAX_LEN + 1, 0)
local offset = table.create(MAX_LEN + 2, 0)
local function ensure(d: State, n: number)
local cap = blen(d.tab) // 4
if d.tabUsed + n > cap then
local nb = bcreate(math.max(cap * 2, d.tabUsed + n) * 4)
bcopy(nb, 0, d.tab, 0, d.tabUsed * 4)
d.tab = nb
end
end
local function replicate(tab: buffer, at: number, step: number, size: number, entry: number)
local e = size
repeat
e -= step
writeu32(tab, (at + e) * 4, entry)
until e <= 0
end
local function nextKey(key: number, len: number): number
local step = lshift(1, len - 1)
while band(key, step) ~= 0 do
step = rshift(step, 1)
end
return if step ~= 0 then band(key, step - 1) + step else key
end
-- build(d, lens (0-based Lua array), n) -> table offset in d.tab, or -1 for an invalid code set (libwebp
-- BuildHuffmanTable: complete codes only, except a single used symbol, which reads 0 bits)
local sorted = table.create(2400, 0)
local function build(d: State, lens: { number }, n: number): number
for l = 0, MAX_LEN do
count[l + 1] = 0
end
for s = 0, n - 1 do
local l = lens[s + 1]
if l > MAX_LEN then
return -1
end
count[l + 1] += 1
end
if count[1] == n then
return -1
end
offset[2] = 0
for len = 1, MAX_LEN - 1 do
if count[len + 1] > lshift(1, len) then
return -1
end
offset[len + 2] = offset[len + 1] + count[len + 1]
end
for s = 0, n - 1 do
local l = lens[s + 1]
if l > 0 then
sorted[offset[l + 1] + 1] = s
offset[l + 1] += 1
end
end
local long = 0
for len = ROOT_BITS + 1, MAX_LEN do
long += count[len + 1]
end
ensure(d, lshift(1, ROOT_BITS) + long * lshift(1, MAX_LEN - ROOT_BITS))
local tab = d.tab
local root = d.tabUsed
local totalSize = lshift(1, ROOT_BITS)
if offset[MAX_LEN + 1] == 1 then
replicate(tab, root, 1, totalSize, sorted[1] * 32)
d.tabUsed = root + totalSize
return root
end
local key = 0
local numNodes, numOpen = 1, 1
local tableBits = ROOT_BITS
local tableSize = lshift(1, tableBits)
local tableAt = root
local low = -1
local mask = totalSize - 1
local symbol = 1
local step = 2
for len = 1, ROOT_BITS do
numOpen *= 2
numNodes += numOpen
numOpen -= count[len + 1]
if numOpen < 0 then
return -1
end
while count[len + 1] > 0 do
replicate(tab, root + key, step, tableSize, sorted[symbol] * 32 + len)
symbol += 1
key = nextKey(key, len)
count[len + 1] -= 1
end
step *= 2
end
step = 2
for len = ROOT_BITS + 1, MAX_LEN do
numOpen *= 2
numNodes += numOpen
numOpen -= count[len + 1]
if numOpen < 0 then
return -1
end
while count[len + 1] > 0 do
if band(key, mask) ~= low then
tableAt += tableSize
-- NextTableBitSize
local l2 = len
local left = lshift(1, l2 - ROOT_BITS)
while l2 < MAX_LEN do
left -= count[l2 + 1]
if left <= 0 then
break
end
l2 += 1
left *= 2
end
tableBits = l2 - ROOT_BITS
tableSize = lshift(1, tableBits)
totalSize += tableSize
low = band(key, mask)
writeu32(tab, (root + low) * 4, (tableAt - root - low) * 32 + tableBits + ROOT_BITS)
end
replicate(tab, tableAt + rshift(key, ROOT_BITS), step, tableSize, sorted[symbol] * 32 + len - ROOT_BITS)
symbol += 1
key = nextKey(key, len)
count[len + 1] -= 1
end
step *= 2
end
if numNodes ~= 2 * offset[MAX_LEN + 1] - 1 then
return -1
end
d.tabUsed = root + totalSize
return root
end
-- readSymbol(d, at) -> symbol (the reader is refilled to >= 15 bits first)
local function readSymbol(d: State, at: number): number
if d.bitcnt < MAX_LEN then
fill(d)
end
local tab = d.tab
local bitbuf = d.bitbuf
local idx = at + band(bitbuf, 255)
local e = readu32(tab, idx * 4)
local nb = band(e, 31)
if nb > ROOT_BITS then
bitbuf = rshift(bitbuf, ROOT_BITS)
d.bitcnt -= ROOT_BITS
e = readu32(tab, (idx + rshift(e, 5) + band(bitbuf, lshift(1, nb - ROOT_BITS) - 1)) * 4)
nb = band(e, 31)
end
d.bitbuf = rshift(bitbuf, nb)
d.bitcnt -= nb
return rshift(e, 5)
end
local codeLengths = table.create(2400, 0)
local clLens = table.create(19, 0)
-- readCode(d, alphabetSize) -> table offset, or -1
local function readCode(d: State, size: number): number
for i = 1, size do
codeLengths[i] = 0
end
if readBits(d, 1) == 1 then
local numSymbols = readBits(d, 1) + 1
local first = readBits(d, 1)
local s = readBits(d, if first == 0 then 1 else 8)
if s < size then
codeLengths[s + 1] = 1
end
if numSymbols == 2 then
s = readBits(d, 8)
if s < size then
codeLengths[s + 1] = 1
end
end
else
for i = 1, 19 do
clLens[i] = 0
end
local numCodes = readBits(d, 4) + 4
for i = 1, numCodes do
clLens[CODE_LENGTH_ORDER[i] + 1] = readBits(d, 3)
end
local mark = d.tabUsed
local clt = build(d, clLens, 19)
if clt < 0 then
return -1
end
local maxSymbol = size
if readBits(d, 1) == 1 then
local nbits = 2 + 2 * readBits(d, 3)
maxSymbol = 2 + readBits(d, nbits)
if maxSymbol > size then
return -1
end
end
local prev = 8
local s = 0
while s < size do
if maxSymbol == 0 then
break
end
maxSymbol -= 1
local cl = readSymbol(d, clt)
if cl < 16 then
codeLengths[s + 1] = cl
s += 1
if cl ~= 0 then
prev = cl
end
else
local rep, len
if cl == 16 then
rep, len = readBits(d, 2) + 3, prev
elseif cl == 17 then
rep, len = readBits(d, 3) + 3, 0
else
rep, len = readBits(d, 7) + 11, 0
end
if s + rep > size then
return -1
end
for k = s + 1, s + rep do
codeLengths[k] = len
end
s += rep
end
end
d.tabUsed = mark -- the code length table is temporary
end
if eos(d) then
return -1
end
return build(d, codeLengths, size)
end
-- ------------------------------------------------------------------------------------------------ pixel data
local function subSample(size: number, bits: number): number
return rshift(size + lshift(1, bits) - 1, bits)
end
Decode.subSample = subSample
local function cacheKey(argb: number, shift: number): number
-- (0x1e35a7bd * argb) mod 2^32 >> shift, exact in doubles
local lo = argb % 65536
local hi = (argb - lo) / 65536
local m = (lo * 0xa7bd + ((lo * 0x1e35 + hi * 0xa7bd) % 65536) * 65536) % 4294967296
return rshift(m, shift)
end
Decode.cacheKey = cacheKey
-- decodeData: the entropy-coded pixels of one level into `data` (w * h u32)
local function decodeData(d: State, data: buffer, w: number, h: number, cacheBits: number, groups: { number }, himg: buffer?, hbits: number, hxs: number): (boolean, string?)
local total = w * h
local cache: buffer? = if cacheBits > 0 then bcreate(lshift(1, cacheBits) * 4) else nil
local cshift = 32 - cacheBits
local lenLimit = NUM_LITERAL + NUM_LENGTH
local cacheLimit = lenLimit + (if cacheBits > 0 then lshift(1, cacheBits) else 0)
local hmask = if himg then lshift(1, hbits) - 1 else -1
local i, col, row = 0, 0, 0
local gG, gR, gB, gA, gD = groups[1], groups[2], groups[3], groups[4], groups[5]
local function setGroup()
if himg then
local g = readu32(himg, (hxs * rshift(row, hbits) + rshift(col, hbits)) * 4) * 5
gG, gR, gB, gA, gD = groups[g + 1], groups[g + 2], groups[g + 3], groups[g + 4], groups[g + 5]
end
end
while i < total do
if band(col, hmask) == 0 then
setGroup()
end
local code = readSymbol(d, gG)
if code < NUM_LITERAL then
local r = readSymbol(d, gR)
local b = readSymbol(d, gB)
local a = readSymbol(d, gA)
if d.bitcnt < d.over * 8 then
break
end
local argb = a * 16777216 + r * 65536 + code * 256 + b
writeu32(data, i * 4, argb)
if cache then
writeu32(cache, cacheKey(argb, cshift) * 4, argb)
end
i += 1
col += 1
if col >= w then
col = 0
row += 1
end
elseif code < lenLimit then
local ls = code - NUM_LITERAL
local length
if ls < 4 then
length = ls + 1
else
local eb = rshift(ls - 2, 1)
length = lshift(2 + band(ls, 1), eb) + readBits(d, eb) + 1
end
local ds = readSymbol(d, gD)
local dc
if ds < 4 then
dc = ds + 1
else
local eb = rshift(ds - 2, 1)
dc = lshift(2 + band(ds, 1), eb) + readBits(d, eb) + 1
end
local dist
if dc > 120 then
dist = dc - 120
else
local v = CODE_TO_PLANE[dc]
dist = rshift(v, 4) * w + 8 - band(v, 15)
if dist < 1 then
dist = 1
end
end
if d.bitcnt < d.over * 8 then
break
end
if i < dist or total - i < length then
return false, "bad backward reference"
end
if dist >= length then
bcopy(data, i * 4, data, (i - dist) * 4, length * 4)
else
for k = i, i + length - 1 do
writeu32(data, k * 4, readu32(data, (k - dist) * 4))
end
end
if cache then
for k = i, i + length - 1 do
local argb = readu32(data, k * 4)
writeu32(cache, cacheKey(argb, cshift) * 4, argb)
end
end
i += length
col += length
while col >= w do
col -= w
row += 1
end
if band(col, hmask) ~= 0 then
setGroup()
end
elseif code < cacheLimit and cache then
if d.bitcnt < d.over * 8 then
break
end
local argb = readu32(cache, (code - lenLimit) * 4)
writeu32(data, i * 4, argb)
writeu32(cache, cacheKey(argb, cshift) * 4, argb)
i += 1
col += 1
if col >= w then
col = 0
row += 1
end
else
return false, "bad symbol"
end
end
if eos(d) then
return false, "truncated: pixel data"
end
return true, nil
end
-- ------------------------------------------------------------------------------------------------ transforms
local function addPixels(a: number, b: number): number
return band(band(a, 0xff00ff00) + band(b, 0xff00ff00), 0xff00ff00) + band(band(a, 0x00ff00ff) + band(b, 0x00ff00ff), 0x00ff00ff)
end
Decode.addPixels = addPixels
local function average2(a: number, b: number): number
return rshift(band(bxor(a, b), 0xfefefefe), 1) + band(a, b)
end
Decode.average2 = average2
local function clip255(v: number): number
return if v < 0 then 0 elseif v > 255 then 255 else v
end
local function sub3(a: number, b: number, c: number): number
local pb, pa = b - c, a - c
return (if pb < 0 then -pb else pb) - (if pa < 0 then -pa else pa)
end
-- select(T, L, TL)
local function select3(a: number, b: number, c: number): number
local s = sub3(rshift(a, 24), rshift(b, 24), rshift(c, 24))
+ sub3(band(rshift(a, 16), 255), band(rshift(b, 16), 255), band(rshift(c, 16), 255))
+ sub3(band(rshift(a, 8), 255), band(rshift(b, 8), 255), band(rshift(c, 8), 255))
+ sub3(band(a, 255), band(b, 255), band(c, 255))
return if s <= 0 then a else b
end
local function clampedFull(c0: number, c1: number, c2: number): number
local a = clip255(rshift(c0, 24) + rshift(c1, 24) - rshift(c2, 24))
local r = clip255(band(rshift(c0, 16), 255) + band(rshift(c1, 16), 255) - band(rshift(c2, 16), 255))
local g = clip255(band(rshift(c0, 8), 255) + band(rshift(c1, 8), 255) - band(rshift(c2, 8), 255))
local b = clip255(band(c0, 255) + band(c1, 255) - band(c2, 255))
return a * 16777216 + r * 65536 + g * 256 + b
end
local function half(a: number, b: number): number
local dd = a - b
-- C division truncates toward zero
local q = if dd >= 0 then rshift(dd, 1) else -rshift(-dd, 1)
return clip255(a + q)
end
local function clampedHalf(c0: number, c1: number, c2: number): number
local ave = average2(c0, c1)
local a = half(rshift(ave, 24), rshift(c2, 24))
local r = half(band(rshift(ave, 16), 255), band(rshift(c2, 16), 255))
local g = half(band(rshift(ave, 8), 255), band(rshift(c2, 8), 255))
local b = half(band(ave, 255), band(c2, 255))
return a * 16777216 + r * 65536 + g * 256 + b
end
-- predict(mode, L, T, TR, TL) -> the predicted pixel (libwebp VP8LPredictors; 14 and 15 are black)
local function predict(mode: number, L: number, T: number, TR: number, TL: number): number
if mode == 0 then
return ARGB_BLACK
elseif mode == 1 then
return L
elseif mode == 2 then
return T
elseif mode == 3 then
return TR
elseif mode == 4 then
return TL
elseif mode == 5 then
return average2(average2(L, TR), T)
elseif mode == 6 then
return average2(L, TL)
elseif mode == 7 then
return average2(L, T)
elseif mode == 8 then
return average2(TL, T)
elseif mode == 9 then
return average2(T, TR)
elseif mode == 10 then
return average2(average2(L, TL), average2(T, TR))
elseif mode == 11 then
return select3(T, L, TL)
elseif mode == 12 then
return clampedFull(L, T, TL)
elseif mode == 13 then
return clampedHalf(L, T, TL)
end
return ARGB_BLACK
end
Decode.predict = predict
local function inversePredictor(data: buffer, w: number, h: number, bits: number, sub: buffer)
local tw = subSample(w, bits)
-- first row: black, then L
writeu32(data, 0, addPixels(readu32(data, 0), ARGB_BLACK))
for x = 1, w - 1 do
writeu32(data, x * 4, addPixels(readu32(data, x * 4), readu32(data, (x - 1) * 4)))
end
for y = 1, h - 1 do
local row = y * w
local up = row - w
writeu32(data, row * 4, addPixels(readu32(data, row * 4), readu32(data, up * 4)))
local trow = rshift(y, bits) * tw
local x = 1
while x < w do
local mode = band(rshift(readu32(sub, (trow + rshift(x, bits)) * 4), 8), 15)
local xe = math.min(w, lshift(rshift(x, bits) + 1, bits))
local L = readu32(data, (row + x - 1) * 4)
if mode == 1 then
for k = x, xe - 1 do
L = addPixels(readu32(data, (row + k) * 4), L)
writeu32(data, (row + k) * 4, L)
end
elseif mode == 2 then
for k = x, xe - 1 do
writeu32(data, (row + k) * 4, addPixels(readu32(data, (row + k) * 4), readu32(data, (up + k) * 4)))
end
else
for k = x, xe - 1 do
local p = predict(mode, L, readu32(data, (up + k) * 4), readu32(data, (up + k + 1) * 4), readu32(data, (up + k - 1) * 4))
L = addPixels(readu32(data, (row + k) * 4), p)
writeu32(data, (row + k) * 4, L)
end
end
x = xe
end
end
end
local function sint8(v: number): number
return if v >= 128 then v - 256 else v
end
local function inverseCrossColor(data: buffer, w: number, h: number, bits: number, sub: buffer)
local tw = subSample(w, bits)
for y = 0, h - 1 do
local trow = rshift(y, bits) * tw
local x = 0
while x < w do
local m = readu32(sub, (trow + rshift(x, bits)) * 4)
local g2r = sint8(band(m, 255))
local g2b = sint8(band(rshift(m, 8), 255))
local r2b = sint8(band(rshift(m, 16), 255))
local xe = math.min(w, lshift(rshift(x, bits) + 1, bits))
for k = y * w + x, y * w + xe - 1 do
local argb = readu32(data, k * 4)
local g = sint8(band(rshift(argb, 8), 255))
local r = band(band(rshift(argb, 16), 255) + ((g2r * g) // 32), 255)
local b = band(band(argb, 255) + ((g2b * g) // 32) + ((r2b * sint8(r)) // 32), 255)
writeu32(data, k * 4, band(argb, 0xff00ff00) + r * 65536 + b)
end
x = xe
end
end
end
local function inverseSubtractGreen(data: buffer, n: number)
for k = 0, n - 1 do
local argb = readu32(data, k * 4)
local g = band(rshift(argb, 8), 255)
writeu32(data, k * 4, band(argb, 0xff00ff00) + band(band(argb, 0x00ff00ff) + g * 65537, 0x00ff00ff))
end
end
-- inverseColorIndexing(src (sw x h), w, h, bits, map (2^(8 >> bits) u32)) -> w x h
local function inverseColorIndexing(src: buffer, sw: number, w: number, h: number, bits: number, map: buffer): buffer
local out = bcreate(w * h * 4)
if bits == 0 then
for k = 0, w * h - 1 do
writeu32(out, k * 4, readu32(map, band(rshift(readu32(src, k * 4), 8), 255) * 4))
end
return out
end
local bpp = rshift(8, bits)
local cmask = lshift(1, bits) - 1
local vmask = lshift(1, bpp) - 1
for y = 0, h - 1 do
local packed = 0
local s = y * sw
local o = y * w
for x = 0, w - 1 do
if band(x, cmask) == 0 then
packed = band(rshift(readu32(src, s * 4), 8), 255)
s += 1
end
writeu32(out, (o + x) * 4, readu32(map, band(packed, vmask) * 4))
packed = rshift(packed, bpp)
end
end
return out
end
-- ------------------------------------------------------------------------------------------------ image stream
type Transform = { kind: number, xsize: number, bits: number, data: buffer? }
local decodeStream: (d: State, xs: number, ys: number, level0: boolean) -> (buffer?, string?)
-- readCodes(d, xs, ys, cacheBits, level0) -> groups (5 table offsets each), huffman image, bits, image width
local function readCodes(d: State, xs: number, ys: number, cacheBits: number, level0: boolean): ({ number }?, buffer?, number, number, string?)
local himg: buffer? = nil
local hbits, hxs = 0, 0
local numGroups = 1
local mapping: { number }? = nil
local numMax = 1
if level0 and readBits(d, 1) == 1 then
hbits = readBits(d, 3) + 2
hxs = subSample(xs, hbits)
local hys = subSample(ys, hbits)
local img, err = decodeStream(d, hxs, hys, false)
if not img then
return nil, nil, 0, 0, err
end
local n = hxs * hys
for k = 0, n - 1 do
local g = band(rshift(readu32(img, k * 4), 8), 0xffff)
writeu32(img, k * 4, g)
if g >= numMax then
numMax = g + 1
end
end
if numMax > 1000 or numMax > xs * ys then
-- remap the used groups to [0, numGroups); unused ones are read and checked but not kept
local map: { number } = table.create(numMax, -1)
numGroups = 0
for k = 0, n - 1 do
local g = readu32(img, k * 4)
if map[g + 1] == -1 then
map[g + 1] = numGroups
numGroups += 1
end
writeu32(img, k * 4, map[g + 1])
end
mapping = map
else
numGroups = numMax
end
himg = img
end
if eos(d) then
return nil, nil, 0, 0, "truncated: meta codes"
end
local groups: { number } = table.create(numGroups * 5, 0)
local cacheSize = if cacheBits > 0 then lshift(1, cacheBits) else 0
for i = 0, numMax - 1 do
local slot = if mapping then mapping[i + 1] else i
local mark = d.tabUsed
for j = 0, 4 do
local size = if j == 0 then NUM_LITERAL + NUM_LENGTH + cacheSize elseif j == 4 then NUM_DISTANCE else NUM_LITERAL
local at = readCode(d, size)
if at < 0 then
return nil, nil, 0, 0, if eos(d) then "truncated: prefix code" else "bad prefix code"
end
if slot >= 0 then
groups[slot * 5 + j + 1] = at
end
end
if slot < 0 then
d.tabUsed = mark
end
end
return groups, himg, hbits, hxs, nil
end
decodeStream = function(d: State, xs: number, ys: number, level0: boolean): (buffer?, string?)
local transforms: { Transform } = {}
local seen = 0
local txs = xs
if level0 then
while readBits(d, 1) == 1 do
local kind = readBits(d, 2)
if band(seen, lshift(1, kind)) ~= 0 then
return nil, "repeated transform"
end
seen = bor(seen, lshift(1, kind))
local t: Transform = { kind = kind, xsize = txs, bits = 0, data = nil }
if kind == 0 or kind == 1 then
t.bits = readBits(d, 3) + 2
local sub, err = decodeStream(d, subSample(txs, t.bits), subSample(ys, t.bits), false)
if not sub then
return nil, err
end
t.data = sub
elseif kind == 3 then
local num = readBits(d, 8) + 1
local bits = if num > 16 then 0 elseif num > 4 then 1 elseif num > 2 then 2 else 3
t.bits = bits
local pal, err = decodeStream(d, num, 1, false)
if not pal then
return nil, err
end
local final = lshift(1, rshift(8, bits))
local map = bcreate(final * 4)
local prev = 0
for k = 0, num - 1 do
prev = addPixels(readu32(pal, k * 4), prev)
writeu32(map, k * 4, prev)
end
t.data = map
txs = subSample(txs, bits)
end
table.insert(transforms, t)
if eos(d) then
return nil, "truncated: transforms"
end
end
end
local cacheBits = 0
if readBits(d, 1) == 1 then
cacheBits = readBits(d, 4)
if cacheBits < 1 or cacheBits > MAX_CACHE_BITS then
return nil, "bad colour cache size"
end
end
local mark = d.tabUsed
local groups, himg, hbits, hxs, err = readCodes(d, txs, ys, cacheBits, level0)
if not groups then
return nil, err
end
local data = bcreate(txs * ys * 4)
local ok, derr = decodeData(d, data, txs, ys, cacheBits, groups, himg, hbits, hxs)
if not ok then
return nil, derr
end
d.tabUsed = mark
-- inverse transforms, last read first
for k = #transforms, 1, -1 do
local t = transforms[k]
if t.kind == 0 then
inversePredictor(data, t.xsize, ys, t.bits, t.data :: buffer)
elseif t.kind == 1 then
inverseCrossColor(data, t.xsize, ys, t.bits, t.data :: buffer)
elseif t.kind == 2 then
inverseSubtractGreen(data, t.xsize * ys)
else
data = inverseColorIndexing(data, subSample(t.xsize, t.bits), t.xsize, ys, t.bits, t.data :: buffer)
end
end
return data, nil
end
local function newState(buf: buffer, off: number, len: number, maxPixels: number): State
return {
buf = buf,
pos = off,
limit = off + len,
bitbuf = 0,
bitcnt = 0,
over = 0,
tab = bcreate(4096 * 4),
tabUsed = 0,
maxPixels = maxPixels,
}
end
-- header(buf, off, len) -> width, height, alphaHint | nil, message
function Decode.header(buf: buffer, off: number, len: number): (number?, number, boolean, string?)
if len < 5 then
return nil, 0, false, "truncated: header"
end
if readu8(buf, off) ~= 0x2f then
return nil, 0, false, "bad VP8L signature"
end
local v = readu32(buf, off + 1)
local w = band(v, 0x3fff) + 1
local h = band(rshift(v, 14), 0x3fff) + 1
local alpha = band(rshift(v, 28), 1) == 1
if rshift(v, 29) ~= 0 then
return nil, 0, false, "bad VP8L version"
end
return w, h, alpha, nil
end
-- argb(buf, off, len, maxPixels) -> ARGB pixels, width, height, alphaHint | nil, message
function Decode.argb(buf: buffer, off: number, len: number, maxPixels: number): (buffer?, number, number, boolean, string?)
local w, h, alpha, err = Decode.header(buf, off, len)
if not w then
return nil, 0, 0, false, err
end
if w * h > maxPixels then
return nil, w, h, alpha, "limit: " .. w .. " x " .. h .. " is over maxPixels"
end
local d = newState(buf, off + 5, len - 5, maxPixels)
local data, derr = decodeStream(d, w, h, true)
if not data then
return nil, w, h, alpha, derr
end
return data, w, h, alpha, nil
end
-- stream(buf, off, len, w, h) -> ARGB pixels | nil, message: a headerless image stream (the ALPH chunk's payload)
function Decode.stream(buf: buffer, off: number, len: number, w: number, h: number): (buffer?, string?)
local d = newState(buf, off, len, w * h)
return decodeStream(d, w, h, true)
end
-- toRgba(argb, n, out?, outOff?) -> rgba8 bytes
function Decode.toRgba(argb: buffer, n: number, out: buffer?, outOff: number?): buffer
local o = out or bcreate(n * 4)
local base = outOff or 0
for k = 0, n - 1 do
local v = readu32(argb, k * 4)
-- 0xAARRGGBB (bytes B G R A) -> bytes R G B A
writeu32(o, base + k * 4, band(v, 0xff00ff00) + band(rshift(v, 16), 255) + band(v, 255) * 65536)
end
return o
end
-- green(argb, n) -> the green channel as bytes (alpha planes)
function Decode.green(argb: buffer, n: number): buffer
local o = bcreate(n)
for k = 0, n - 1 do
writeu8(o, k, band(rshift(readu32(argb, k * 4), 8), 255))
end
return o
end
return Decode
+8
View File
@@ -0,0 +1,8 @@
--!strict
--!native
-- codecs/Vp8L/Encode: stub
local Encode = {}
function Encode.encode(rgba: buffer, w: number, h: number, effort: number, exact: boolean, alphaHint: boolean?, headerless: boolean): buffer
return buffer.create(0)
end
return Encode
+120
View File
@@ -0,0 +1,120 @@
--!strict
-- codecs/Vp8L: WebP lossless (VP8L, "WebP Lossless Bitstream Specification", RFC 9649 section 3). A codec (layer 1):
-- image/webp uses it for VP8L frames and for compressed ALPH planes; other containers may carry it too.
-- Decoder: a port of libwebp 1.6 (BSD-3-Clause, licenses/libwebp-LICENSE.txt), bit-exact with it. Encoder: new
-- (subtract green, predictor and cross-colour transforms with per-tile mode search, colour indexing with pixel
-- bundling for <= 256 colours, LZ77 with hash chains and the 2-D distance codes, a colour cache chosen by entropy,
-- one prefix code group per image or meta codes for big images).
--
-- local Vp8L = require("@formats/codecs/Vp8L")
-- local rgba, w, h, alpha = Vp8L.decode(buf, off, len) -- a VP8L bitstream (0x2f signature) -> rgba8
-- local a8 = Vp8L.decodeAlpha(buf, off, len, w, h) -- a headerless stream's green channel (ALPH)
-- local bits = Vp8L.encode(rgba, w, h, { effort = 4 }) -- rgba8 -> a VP8L bitstream
--
-- Never throws: bad data returns nil, Err (core/Err codes TRUNCATED / CORRUPT / LIMIT / ARG).
local Err = require("@formats/core/Err")
local Decode = require("@formats/codecs/Vp8L/Decode")
local Encode = require("@formats/codecs/Vp8L/Encode")
local Vp8L = {}
local DEFAULT_MAX_PIXELS = 2 ^ 28
local function fail(msg: string?, offset: number?): Err.Err
local m = msg or "bad data"
if string.sub(m, 1, 11) == "truncated: " then
return Err.new(Err.TRUNCATED, string.sub(m, 12), offset)
elseif string.sub(m, 1, 7) == "limit: " then
return Err.new(Err.LIMIT, string.sub(m, 8), offset)
end
return Err.new(Err.CORRUPT, m, offset)
end
Vp8L.fail = fail
local function range(buf: buffer, off: number?, len: number?): (number, number)
local n = buffer.len(buf)
local o = math.clamp(off or 0, 0, n)
return o, math.clamp(len or (n - o), 0, n - o)
end
-- info(buf, off?, len?) -> { width, height, alpha } | nil, Err: the 5-byte header
function Vp8L.info(buf: buffer, off: number?, len: number?): ({ width: number, height: number, alpha: boolean }?, Err.Err?)
local o, l = range(buf, off, len)
local w, h, alpha, err = Decode.header(buf, o, l)
if not w then
return nil, fail(err, o)
end
return { width = w, height = h, alpha = alpha }, nil
end
-- decodeArgb(buf, off?, len?, maxPixels?) -> ARGB u32 pixels (0xAARRGGBB), width, height, alphaHint | nil, Err
function Vp8L.decodeArgb(buf: buffer, off: number?, len: number?, maxPixels: number?): (buffer?, any, number?, boolean?)
local o, l = range(buf, off, len)
local px, w, h, alpha, err = Decode.argb(buf, o, l, maxPixels or DEFAULT_MAX_PIXELS)
if not px then
return nil, fail(err, o)
end
return px, w, h, alpha
end
-- decode(buf, off?, len?, maxPixels?) -> rgba8 pixels, width, height, alphaHint | nil, Err
function Vp8L.decode(buf: buffer, off: number?, len: number?, maxPixels: number?): (buffer?, any, number?, boolean?)
local px, w, h, alpha = Vp8L.decodeArgb(buf, off, len, maxPixels)
if not px then
return nil, w
end
return Decode.toRgba(px, w * (h :: number)), w, h, alpha
end
-- decodeAlpha(buf, off, len, width, height) -> width * height bytes (the green channel) | nil, Err
function Vp8L.decodeAlpha(buf: buffer, off: number?, len: number?, width: number, height: number): (buffer?, Err.Err?)
local o, l = range(buf, off, len)
local px, err = Decode.stream(buf, o, l, width, height)
if not px then
return nil, fail(err, o)
end
return Decode.green(px, width * height), nil
end
-- toRgba(argb, n) -> rgba8 bytes of n ARGB pixels
Vp8L.toRgba = Decode.toRgba
export type EncodeOptions = {
effort: number?, -- 0..9, default 5: more = smaller and slower (0: no transforms, no LZ77 search)
exact: boolean?, -- default false: keep the RGB of fully transparent pixels (otherwise they may be zeroed)
alphaHint: boolean?, -- the header's "alpha is used" bit; default: any alpha < 255
}
-- encode(rgba, width, height, opts?) -> a VP8L bitstream (signature + header + image stream) | nil, Err
function Vp8L.encode(rgba: buffer, width: number, height: number, opts: EncodeOptions?): (buffer?, Err.Err?)
if type(rgba) ~= "buffer" or type(width) ~= "number" or type(height) ~= "number" then
return nil, Err.new(Err.ARG, "encode: needs rgba8 pixels, width, height")
end
if width < 1 or height < 1 or width > 16384 or height > 16384 or width % 1 ~= 0 or height % 1 ~= 0 then
return nil, Err.new(Err.ARG, "encode: size must be 1..16384")
end
if buffer.len(rgba) < width * height * 4 then
return nil, Err.new(Err.ARG, "encode: pixel buffer too small")
end
local o = opts or {}
local effort = o.effort or 5
if type(effort) ~= "number" or effort < 0 or effort > 9 then
return nil, Err.new(Err.ARG, "encode: effort must be 0..9")
end
return Encode.encode(rgba, width, height, effort, o.exact == true, o.alphaHint, false), nil
end
-- encodeAlpha(a8, width, height, opts?) -> a headerless stream of the plane in green (an ALPH payload)
function Vp8L.encodeAlpha(a8: buffer, width: number, height: number, opts: EncodeOptions?): (buffer?, Err.Err?)
if type(a8) ~= "buffer" or buffer.len(a8) < width * height then
return nil, Err.new(Err.ARG, "encodeAlpha: plane too small")
end
local rgba = buffer.create(width * height * 4)
for i = 0, width * height - 1 do
buffer.writeu32(rgba, i * 4, 0xff000000 + buffer.readu8(a8, i) * 256)
end
local effort = opts and opts.effort or 5
return Encode.encode(rgba, width, height, effort, true, false, true), nil
end
return Vp8L