diff --git a/codecs/wp2/enc/wp2_enc.cpp b/codecs/wp2/enc/wp2_enc.cpp index d98bda33..41a89d46 100644 --- a/codecs/wp2/enc/wp2_enc.cpp +++ b/codecs/wp2/enc/wp2_enc.cpp @@ -5,168 +5,8 @@ #include "src/wp2/encode.h" using namespace emscripten; -// enum PartitionMethod { -// // For each block size starting from biggest, take all blocks matching -// some: THRESH_VARIANCE_PARTITIONING, // uniform variance score -// // For each block size starting from biggest, find the best pos depending -// on: BIG_BLOCK_VARIANCE_RNG_PARTITIONING, // uniform variance score -// BIG_BLOCK_VARIANCE_MAX_PARTITIONING, // low variance score -// BIG_BLOCK_TRANS_AMPL_PARTITIONING, // DCT coeffs amplitude -// BIG_BLOCK_TRANS_ZEROS_PARTITIONING, // number of near zeros in DCT coeffs -// BIG_BLOCK_LUMA_RNG_PARTITIONING, // luma range score -// BIG_BLOCK_RECONS_PARTITIONING, // reconstruction score -// BIG_BLOCK_PRED_PARTITIONING, // prediction score -// BIG_BLOCK_QUANT_PARTITIONING, // pred+recons+quant score -// // For each pos starting from top left, take the best block size depending -// on: TOP_LEFT_RECONS_PARTITIONING, // reconstruction score -// TOP_LEFT_PRED_PARTITIONING, // prediction score -// TOP_LEFT_BLOCK_ENCODE_PARTITIONING, // block encoding score -// TOP_LEFT_TILE_ENCODE_PARTITIONING, // tile encoding score (slow) -// TOP_LEFT_TILE_ENCODE_DECODE_PARTITIONING, // tile enc+dec score (slow) -// // Combine several metrics in successive block size passes: -// MULTIPASS_PARTITIONING, -// // For each possible block layout, take the best one (extremely slow): -// EXHAUSTIVE_PARTITIONING, // based on tile enc+dec score -// // Split variants of methods above: -// SPLIT_BIG_BLOCK_VARIANCE_MAX_PARTITIONING, -// SPLIT_BIG_BLOCK_PRED_PARTITIONING, -// SPLIT_BIG_BLOCK_QUANT_PARTITIONING, -// SPLIT_TOP_LEFT_BLOCK_ENCODE_PARTITIONING, -// // Fixed block size (except on edges). Also depends on the partition set. -// ALL_4X4_PARTITIONING, -// ALL_8X8_PARTITIONING, -// ALL_16X16_PARTITIONING, -// ALL_32X32_PARTITIONING, -// NUM_PARTITION_METHODS -// }; - -// enum PartitionSet { // The smallest block size is 4x4. -// SMALL_SQUARES, // Up to 8x8 -// SMALL_RECTS, // Up to 16x16 -// ALL_RECTS, // Up to 32x32, ratio at most 4:1 -// THICK_RECTS, // Up to 32x32, ratio at most 2:1 -// MEDIUM_SQUARES, // Up to 16x16 -// ALL_SQUARES, // Up to 32x32 -// SOME_RECTS, // Up to 32x32, subset of frequently used rects -// NUM_PARTITION_SETS -// }; - -// enum class PredictorSet { -// CUSTOM_PREDICTORS = 0, // this predictor should stay first -// DC_PREDICTORS, -// ANGLE_PREDICTORS, -// VP8_PREDICTORS, -// SMOOTH_PREDICTORS, -// PAETH_PREDICTOR, -// AV1_FILTER, -// FUSE_PREDICTORS, -// ALL, // includes all of the above starting at 1 -// LARGE_PREDICTORS, // fixed set for some Y blocks -// UV_PREDICTORS, // fixed set for U/V -// ALPHA_PREDICTORS, // fixed set for alpha -// LAST_PREDICTORS, // end-of-list marker -// }; - -// enum class Csp { kYCoCg, kYCbCr, kCustom, kYIQ }; - -// typedef enum { -// UVModeAdapt = 0, -// UVMode420, -// UVModeSharp, -// UVMode444, -// NumUVMode // End-of-list marker -// } UVMode; - -struct WP2Options { - float quality = 75.0f; // Range: [0 = smallest file .. 100 = lossless] - // Quality in [95-100) range will use near-lossless. - // Quality 100 is strictly lossless. - // int target_size = 0; // If non-zero, set the desired target size in - // bytes. Takes precedence over the 'quality' parameter. - // float target_psnr = 0.f; // If non-zero, specifies the minimal distortion - // to try to achieve. Precedence over 'target_size'. - - float alpha_quality = 100.f; // Range: [0 = smallest size .. 100 = lossless] - int speed = 5; // Quality/speed trade-off. Range: [0=fast .. 9=slower-better] - - // Side parameters: - // Set whether the image will be rotated during decoding. - // Orientation decoding_orientation = Orientation::kOriginal; - // Add a heavily compressed preview to be decoded and displayed before final - // pixels (small size overhead up to kMaxPreviewSize). - // bool create_preview = false; - - // TransferFunction transfer_function = WP2_TF_ITU_R_BT2020_10BIT; - - // Parameters related to lossy compression only: - int pass = 1; // Number of entropy-analysis passes. Range: [1..10] - - // Spatial noise shaping strength in [0(=off), 100] - // Affects how we spread noise between 'risky' areas (where noise is easily - // visible) and easier areas (where it's less visible). A high SNS - // value leads to skewing noise more towards areas where it should be less - // visible. In general this improves SSIM but worsens PSNR. - float sns = 50.f; - int error_diffusion = 0; // error diffusion strength [0=off, 100=max] - - int segments = 4; // Max number of segments. Range: [1..kMaxNumSegments] - int segment_threshold = 0; // Segmentation threshold. Range: [0..100] - // selector for explicit or implicit segment-id - // typedef enum { - // SEGMENT_ID_AUTO, // use ID_EXPLICIT above a quality threshold - // SEGMENT_ID_EXPLICIT, - // SEGMENT_ID_IMPLICIT - // } SegmentIdMode; - // SegmentIdMode segment_id_mode = SEGMENT_ID_AUTO; - - // Size of tiles (width/height) in pixels. Tiles are always square. Each - // tile is compressed independently, possibly in parallel. - // Valid values: 64, 128, 256, 512 (see format_constants.h) - // 0 means size is chosen automatically. - int tile_size = 0; - - // Algorithm for dividing the image into blocks. - // PartitionMethod partition_method = MULTIPASS_PARTITIONING; - // The set of allowed block sizes for image partitioning. - // PartitionSet partition_set = SOME_RECTS; - // If true, use binary space partitioning instead of floating partition. - bool partition_snapping = true; - - // The set of predictors that can be used for reconstruction. - // PredictorSet predictor_set = PredictorSet::VP8_PREDICTORS; - - WP2::Csp csp_type = WP2::Csp::kYCoCg; // Colorspace. - - WP2::EncoderConfig::UVMode uv_mode = - WP2::EncoderConfig::UVMode::UVMode420; // Default sub-sampling mode for - // U/V planes. - - // int preprocessing = 0; // Preprocessing filter. - // int preprocessing_strength = 0; // Range: [0 .. 100] - - // bool use_random_matrix = false; // Experimental. - // bool store_grain = false; // Experimental: store grain info - - // // Parameters related to lossless compression only: - // bool use_delta_palette = false; // Reserved for future lossless feature. - - // // Performance parameters (no impact on encoded bytes): - // int thread_level = 0; // If non-zero, try and use multi-threaded - // encoding. bool low_memory = false; // Memory usage reduction (but CPU use - // increase). - - // // Neural compression: - // int use_neural_compression = 0; // Neural network compression. - // const char* graphdef_path = nullptr; // Directory holding encoder / - // decoder - // // graphdefs structure: - // // base/qq/[en|de]coder.pbbin - - // EncoderInfo* info = nullptr; // If not -}; - val encode(std::string image_in, int image_width, int image_height, - WP2Options options) { + WP2::EncoderConfig config) { WP2Status status; uint8_t *image_buffer = (uint8_t *)image_in.c_str(); WP2::ArgbBuffer src = WP2::ArgbBuffer(); @@ -174,19 +14,13 @@ val encode(std::string image_in, int image_width, int image_height, // premultiplied alpha image_width, image_height, image_buffer, 4 * image_width); if (status != WP2_STATUS_OK) { - return val(1); + return val::null(); } WP2::MemoryWriter memory_writer; - WP2::EncoderConfig config; - config.quality = options.quality; - config.alpha_quality = options.alpha_quality; - config.speed = options.speed; - config.pass = options.pass; - config.sns = options.sns; status = WP2::Encode(src, &memory_writer, config); if (status != WP2_STATUS_OK) { - return val(2); + return val::null(); } return val(typed_memory_view(memory_writer.size_, memory_writer.mem_)); @@ -194,12 +28,12 @@ val encode(std::string image_in, int image_width, int image_height, } EMSCRIPTEN_BINDINGS(my_module) { - value_object("WP2Options") - .field("quality", &WP2Options::quality) - .field("alpha_quality", &WP2Options::alpha_quality) - .field("speed", &WP2Options::speed) - .field("pass", &WP2Options::pass) - .field("sns", &WP2Options::sns); + value_object("WP2EncoderConfig") + .field("quality", &WP2::EncoderConfig::quality) + .field("alpha_quality", &WP2::EncoderConfig::alpha_quality) + .field("speed", &WP2::EncoderConfig::speed) + .field("pass", &WP2::EncoderConfig::pass) + .field("sns", &WP2::EncoderConfig::sns); function("encode", &encode); } diff --git a/codecs/wp2/enc/wp2_enc.js b/codecs/wp2/enc/wp2_enc.js index b7274013..e59f9e53 100644 --- a/codecs/wp2/enc/wp2_enc.js +++ b/codecs/wp2/enc/wp2_enc.js @@ -17,7 +17,7 @@ function ma(a,b,d){var e=I;if(0>1;for(var e=d+b/2;!(d>=e)&&J[d];)++d;d<<=1;if(32>1];if(0==f||d==b/2)return e;++d;e+=String.fromCharCode(f)}}function pa(a,b,d){void 0===d&&(d=2147483647);if(2>d)return 0;d-=2;var e=b;d=d<2*a.length?d/2:a.length;for(var f=0;f>1]=a.charCodeAt(f),b+=2;K[b>>1]=0;return b-e}function qa(a){return 2*a.length} function ra(a,b){for(var d=0,e="";!(d>=b/4);){var f=L[a+4*d>>2];if(0==f)break;++d;65536<=f?(f-=65536,e+=String.fromCharCode(55296|f>>10,56320|f&1023)):e+=String.fromCharCode(f)}return e}function sa(a,b,d){void 0===d&&(d=2147483647);if(4>d)return 0;var e=b;d=e+d-4;for(var f=0;f=g){var l=a.charCodeAt(++f);g=65536+((g&1023)<<10)|l&1023}L[b>>2]=g;b+=4;if(b+4>d)break}L[b>>2]=0;return b-e} function ta(a){for(var b=0,d=0;d=e&&++d;b+=4}return b}var M,ua,I,K,J,L,N,va,wa;function xa(a){M=a;c.HEAP8=ua=new Int8Array(a);c.HEAP16=K=new Int16Array(a);c.HEAP32=L=new Int32Array(a);c.HEAPU8=I=new Uint8Array(a);c.HEAPU16=J=new Uint16Array(a);c.HEAPU32=N=new Uint32Array(a);c.HEAPF32=va=new Float32Array(a);c.HEAPF64=wa=new Float64Array(a)}var ya=c.INITIAL_MEMORY||16777216;c.wasmMemory?F=c.wasmMemory:F=new WebAssembly.Memory({initial:ya/65536,maximum:32768}); -F&&(M=F.buffer);ya=M.byteLength;xa(M);L[34960]=5382880;function za(a){for(;0>2])}var R={},S={},Sa={};function Ta(a){if(void 0===a)return"_unknown";a=a.replace(/[^a-zA-Z0-9_]/g,"$");var b=a.charCodeAt(0);return 48<=b&&57>=b?"_"+a:a}function Ua(a,b){a=Ta(a);return(new Function("body","return function "+a+'() {\n "use strict"; return body.apply(this, arguments);\n};\n'))(b)} @@ -32,21 +32,21 @@ function hb(a,b,d){c.hasOwnProperty(a)?((void 0===d||void 0!==c[a].fa&&void 0!== function Y(a,b){a=U(a);var d=c["dynCall_"+a];for(var e=[],f=1;f>1]}:function(e){return J[e>>1]};case 2:return d?function(e){return L[e>>2]}:function(e){return N[e>>2]};default:throw new TypeError("Unknown integer type: "+a);}}var ob=[null,[],[]];Wa=c.InternalError=Va("InternalError"); for(var pb=Array(256),qb=0;256>qb;++qb)pb[qb]=String.fromCharCode(qb);Za=pb;$a=c.BindingError=Va("BindingError");c.count_emval_handles=function(){for(var a=0,b=5;b>g])},ga:null})},y:function(a,b){b=U(b);T(a,{name:b,fromWireType:function(d){var e=X[d].value;bb(d);return e},toWireType:function(d,e){return cb(e)},argPackAdvance:8,readValueFromPointer:Ra,ga:null})},k:function(a,b,d){d=Ya(d);b=U(b);T(a, -{name:b,fromWireType:function(e){return e},toWireType:function(e,f){if("number"!==typeof f&&"boolean"!==typeof f)throw new TypeError('Cannot convert "'+db(f)+'" to '+this.name);return f},argPackAdvance:8,readValueFromPointer:eb(b,d),ga:null})},r:function(a,b,d,e,f,g){var l=ib(b,d);a=U(a);f=Y(e,f);hb(a,function(){mb("Cannot call "+a+" due to unbound types",l)},b-1);Xa([],l,function(k){var h=[k[0],null].concat(k.slice(1)),m=k=a,n=f,q=h.length;2>q&&V("argTypes array size mismatch! Must at least get return value and 'this' types!"); +var sb={w:function(a,b,d,e){C("Assertion failed: "+(a?H(I,a,void 0):"")+", at: "+[b?b?H(I,b,void 0):"":"unknown filename",d,e?e?H(I,e,void 0):"":"unknown function"])},B:function(a){return rb(a)},A:function(a){"uncaught_exception"in Oa?Oa.ja++:Oa.ja=1;throw a;},t:function(a){var b=Pa[a];delete Pa[a];var d=b.sa,e=b.ta,f=b.ka,g=f.map(function(l){return l.qa}).concat(f.map(function(l){return l.va}));Xa([a],g,function(l){var k={};f.forEach(function(h,m){var n=l[m],q=h.oa,x=h.pa,z=l[m+f.length],p=h.ua, +ia=h.wa;k[h.ma]={read:function(A){return n.fromWireType(q(x,A))},write:function(A,G){var W=[];p(ia,A,z.toWireType(W,G));Qa(W)}}});return[{name:b.name,fromWireType:function(h){var m={},n;for(n in k)m[n]=k[n].read(h);e(h);return m},toWireType:function(h,m){for(var n in k)if(!(n in m))throw new TypeError('Missing field: "'+n+'"');var q=d();for(n in k)k[n].write(q,m[n]);null!==h&&h.push(e,q);return q},argPackAdvance:8,readValueFromPointer:Ra,ga:e}]})},y:function(a,b,d,e,f){var g=Ya(d);b=U(b);T(a,{name:b, +fromWireType:function(l){return!!l},toWireType:function(l,k){return k?e:f},argPackAdvance:8,readValueFromPointer:function(l){if(1===d)var k=ua;else if(2===d)k=K;else if(4===d)k=L;else throw new TypeError("Unknown boolean type size: "+b);return this.fromWireType(k[l>>g])},ga:null})},x:function(a,b){b=U(b);T(a,{name:b,fromWireType:function(d){var e=X[d].value;bb(d);return e},toWireType:function(d,e){return cb(e)},argPackAdvance:8,readValueFromPointer:Ra,ga:null})},j:function(a,b,d){d=Ya(d);b=U(b);T(a, +{name:b,fromWireType:function(e){return e},toWireType:function(e,f){if("number"!==typeof f&&"boolean"!==typeof f)throw new TypeError('Cannot convert "'+db(f)+'" to '+this.name);return f},argPackAdvance:8,readValueFromPointer:eb(b,d),ga:null})},q:function(a,b,d,e,f,g){var l=ib(b,d);a=U(a);f=Y(e,f);hb(a,function(){mb("Cannot call "+a+" due to unbound types",l)},b-1);Xa([],l,function(k){var h=[k[0],null].concat(k.slice(1)),m=k=a,n=f,q=h.length;2>q&&V("argTypes array size mismatch! Must at least get return value and 'this' types!"); for(var x=null!==h[1]&&!1,z=!1,p=1;p>>k}}var h=-1!=b.indexOf("unsigned");T(a,{name:b,fromWireType:g,toWireType:function(m, n){if("number"!==typeof n&&"boolean"!==typeof n)throw new TypeError('Cannot convert "'+db(n)+'" to '+this.name);if(nf)throw new TypeError('Passing a number "'+db(n)+'" from JS side to C/C++ side to an argument of type "'+b+'", which is outside the valid range ['+e+", "+f+"]!");return h?n>>>0:n|0},argPackAdvance:8,readValueFromPointer:nb(b,l,0!==e),ga:null})},a:function(a,b,d){function e(g){g>>=2;var l=N;return new f(M,l[g+1],l[g])}var f=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array, -Uint32Array,Float32Array,Float64Array][b];d=U(d);T(a,{name:d,fromWireType:e,argPackAdvance:8,readValueFromPointer:e},{ra:!0})},l:function(a,b){b=U(b);var d="std::string"===b;T(a,{name:b,fromWireType:function(e){var f=N[e>>2];if(d)for(var g=e+4,l=0;l<=f;++l){var k=e+4+l;if(l==f||0==I[k]){g=g?H(I,g,k-g):"";if(void 0===h)var h=g;else h+=String.fromCharCode(0),h+=g;g=k+1}}else{h=Array(f);for(l=0;l>2];if(d)for(var g=e+4,l=0;l<=f;++l){var k=e+4+l;if(l==f||0==I[k]){g=g?H(I,g,k-g):"";if(void 0===h)var h=g;else h+=String.fromCharCode(0),h+=g;g=k+1}}else{h=Array(f);for(l=0;l=q&&(q=65536+((q&1023)<<10)|f.charCodeAt(++n)&1023);127>=q?++m:m=2047>=q?m+2:65535>=q?m+3:m+4}return m}:function(){return f.length})(),k=rb(4+l+1);N[k>>2]=l;if(d&&g)ma(f,k+4,l+1);else if(g)for(g=0;g>2],n=l(),q,x=h+4,z=0;z<=m;++z){var p=h+4+z*b;if(z==m||0==n[p>>k])x=e(x,p-x),void 0=== -q?q=x:(q+=String.fromCharCode(0),q+=x),x=p+b}Z(h);return q},toWireType:function(h,m){"string"!==typeof m&&V("Cannot pass non-string to C++ string type "+d);var n=g(m),q=rb(4+n+b);N[q>>2]=n>>k;f(m,q+4,n+b);null!==h&&h.push(Z,q);return q},argPackAdvance:8,readValueFromPointer:Ra,ga:function(h){Z(h)}})},x:function(a,b,d,e,f,g){Pa[a]={name:U(b),sa:Y(d,e),ta:Y(f,g),ka:[]}},e:function(a,b,d,e,f,g,l,k,h,m){Pa[a].ka.push({ma:U(b),qa:d,oa:Y(e,f),pa:g,va:l,ua:Y(k,h),wa:m})},A:function(a,b){b=U(b);T(a,{xa:!0, -name:b,argPackAdvance:0,fromWireType:function(){},toWireType:function(){}})},p:bb,q:function(a){4>>=0;var b=I.length;if(2147483648=d;d*=2){var e=b*(1+.2/d);e=Math.min(e,a+100663296);e=Math.max(16777216,a,e);0>>16);xa(F.buffer);var f=1;break a}catch(g){}f=void 0}if(f)return!0}return!1},w:function(){return 0},s:function(){},j:function(a,b,d,e){for(var f=0,g=0;g>2],k=L[b+(8*g+4)>>2],h=0;h>2]=f;return 0},memory:F,m:function(){return 0},o:function(){return 0},n:function(){},E:function(){return 6},D:function(){},h:function(a){a=+a;return 0<=a?+Ga(a+.5):+Fa(a-.5)},c:function(a){a= -+a;return 0<=a?+Ga(a+.5):+Fa(a-.5)},t:function(){},table:ja}; +q?q=x:(q+=String.fromCharCode(0),q+=x),x=p+b}Z(h);return q},toWireType:function(h,m){"string"!==typeof m&&V("Cannot pass non-string to C++ string type "+d);var n=g(m),q=rb(4+n+b);N[q>>2]=n>>k;f(m,q+4,n+b);null!==h&&h.push(Z,q);return q},argPackAdvance:8,readValueFromPointer:Ra,ga:function(h){Z(h)}})},E:function(a,b,d,e,f,g){Pa[a]={name:U(b),sa:Y(d,e),ta:Y(f,g),ka:[]}},e:function(a,b,d,e,f,g,l,k,h,m){Pa[a].ka.push({ma:U(b),qa:d,oa:Y(e,f),pa:g,va:l,ua:Y(k,h),wa:m})},z:function(a,b){b=U(b);T(a,{xa:!0, +name:b,argPackAdvance:0,fromWireType:function(){},toWireType:function(){}})},o:bb,p:function(a){4>>=0;var b=I.length;if(2147483648=d;d*=2){var e=b*(1+.2/d);e=Math.min(e,a+100663296);e=Math.max(16777216,a,e);0>>16);xa(F.buffer);var f=1;break a}catch(g){}f=void 0}if(f)return!0}return!1},v:function(){return 0},r:function(){},i:function(a,b,d,e){for(var f=0,g=0;g>2],k=L[b+(8*g+4)>>2],h=0;h>2]=f;return 0},memory:F,l:function(){return 0},n:function(){return 0},m:function(){},D:function(){return 6},C:function(){},g:function(a){a=+a;return 0<=a?+Ga(a+.5):+Fa(a-.5)},c:function(a){a= ++a;return 0<=a?+Ga(a+.5):+Fa(a-.5)},s:function(){},table:ja}; (function(){function a(f){c.asm=f.exports;O--;c.monitorRunDependencies&&c.monitorRunDependencies(O);0==O&&(null!==Ha&&(clearInterval(Ha),Ha=null),P&&(f=P,P=null,f()))}function b(f){a(f.instance)}function d(f){return Ma().then(function(g){return WebAssembly.instantiate(g,e)}).then(f,function(g){D("failed to asynchronously prepare wasm: "+g);C(g)})}var e={a:sb};O++;c.monitorRunDependencies&&c.monitorRunDependencies(O);if(c.instantiateWasm)try{return c.instantiateWasm(e,a)}catch(f){return D("Module.instantiateWasm callback failed with error: "+ f),!1}(function(){if(E||"function"!==typeof WebAssembly.instantiateStreaming||Ja()||Ia("file://")||"function"!==typeof fetch)return d(b);fetch(Q,{credentials:"same-origin"}).then(function(f){return WebAssembly.instantiateStreaming(f,e).then(b,function(g){D("wasm streaming compile failed: "+g);D("falling back to ArrayBuffer instantiation");return d(b)})})})();return{}})(); var Na=c.___wasm_call_ctors=function(){return(Na=c.___wasm_call_ctors=c.asm.G).apply(null,arguments)},rb=c._malloc=function(){return(rb=c._malloc=c.asm.H).apply(null,arguments)},Z=c._free=function(){return(Z=c._free=c.asm.I).apply(null,arguments)},lb=c.___getTypeName=function(){return(lb=c.___getTypeName=c.asm.J).apply(null,arguments)};c.___embind_register_native_and_builtin_types=function(){return(c.___embind_register_native_and_builtin_types=c.asm.K).apply(null,arguments)}; diff --git a/codecs/wp2/enc/wp2_enc.wasm b/codecs/wp2/enc/wp2_enc.wasm index 4db9b14e..ffa99ef4 100644 Binary files a/codecs/wp2/enc/wp2_enc.wasm and b/codecs/wp2/enc/wp2_enc.wasm differ