Skip to content

Commit

Permalink
Implement streaming encoder mode with constant memory usage. (libjxl#…
Browse files Browse the repository at this point in the history
…3017)

* Implement streaming encoder mode with constant memory usage.

All the internal images are now limited to the 2048x2048 DC group size,
and the maximum memory usage is around 500 MB.

Benchmark results:
```
Encoding           kPixels    Bytes          BPP  E MP/s  D MP/s     Max norm  SSIMULACRA2   PSNR        pnorm       BPP*pnorm   QABPP   Bugs
---------------------------------------------------------------------------------------------------------------------------------------------
jxl:d0.1:1          156870 125499004    6.4001333   2.883  17.712   0.33339094  93.97017505  55.04   0.11910771  0.762305231867   6.400      0
jxl:d0.1:3          156870 118196370    6.0277173   1.342  14.221   0.33339094  93.97017505  55.04   0.11910771  0.717947620036   6.028      0
jxl:d0.1:4          156870 117892031    6.0121968   1.501  14.289   0.33382890  94.00462704  55.04   0.11907577  0.715906976850   6.012      0
jxl:d0.1:5          156870 139407423    7.1094276   0.777  14.252   0.34058568  93.91823707  57.02   0.10181720  0.723862019767   7.109      0
jxl:d0.1:6          156870 140721309    7.1764325   0.568  13.979   0.34023632  93.89339858  57.11   0.10187573  0.731104323064   7.176      0
jxl:d0.1:7          156870 141506387    7.2164695   0.560  14.325   0.33378362  93.90823930  57.19   0.10142266  0.731913544176   7.216      0
jxl:d0.1:1:buf3     156870 124186341    6.3331908   4.689  16.793   0.33339094  93.97017505  55.04   0.11910771  0.754331862831   6.333      0
jxl:d0.1:3:buf3     156870 118645467    6.0506202   3.936  15.506   0.33339094  93.97017505  55.04   0.11910771  0.720675522105   6.051      0
jxl:d0.1:4:buf3     156870 119051450    6.0713243   4.091  15.353   0.33382890  94.00462704  55.04   0.11907577  0.722947623654   6.071      0
jxl:d0.1:5:buf3     156870 124960819    6.3726872   1.788  13.883   0.42522492  93.86107006  55.38   0.11737111  0.747969378745   6.373      0
jxl:d0.1:6:buf3     156870 125038021    6.3766243   1.194  14.652   0.42531900  93.88796957  55.40   0.11745910  0.748992546623   6.377      0
jxl:d0.1:7:buf3     156870 125896374    6.4203982   1.156  14.181   0.43496868  93.90811739  55.47   0.11689811  0.750532425816   6.420      0
jxl:d1:1            156870 30373676    1.5489810   3.437  24.764   1.43588506  86.14940783  43.59   0.58841531  0.911444150987   2.257      0
jxl:d1:3            156870 27841570    1.4198500   2.069  26.847   1.43588506  86.14940783  43.59   0.58841531  0.835461474297   2.068      0
jxl:d1:4            156870 28110273    1.4335532   2.860  24.650   1.43953718  86.43275684  43.61   0.58675396  0.841142996644   2.092      0
jxl:d1:5            156870 23502237    1.1985549   0.913  23.353   1.49287327  84.49628039  43.63   0.58180371  0.697323707375   1.816      0
jxl:d1:6            156870 23686906    1.2079726   0.852  24.001   1.48966351  84.58298559  43.65   0.58294516  0.704181767609   1.829      0
jxl:d1:7            156870 23680833    1.2076629   0.662  24.676   1.49379655  84.59488978  43.66   0.58316869  0.704271179346   1.829      0
jxl:d1:1:buf3       156870 30144399    1.5372885   6.648  28.425   1.43588506  86.14940783  43.59   0.58841531  0.904564075601   2.240      0
jxl:d1:3:buf3       156870 28077983    1.4319065   6.575  26.224   1.43588506  86.14940783  43.59   0.58841531  0.842555684628   2.085      0
jxl:d1:4:buf3       156870 28586708    1.4578501   6.951  28.615   1.43953718  86.43275684  43.61   0.58675396  0.855399349245   2.127      0
jxl:d1:5:buf3       156870 23664692    1.2068397   2.236  25.647   1.79721683  84.31625183  43.00   0.62510227  0.754398246796   2.203      0
jxl:d1:6:buf3       156870 23700533    1.2086675   1.586  25.146   1.79445658  84.33377882  43.02   0.62494042  0.755345188024   2.201      0
jxl:d1:7:buf3       156870 23699715    1.2086258   1.441  24.907   1.79768241  84.34806803  43.03   0.62501552  0.755409881140   2.202      0
jxl:d10:1           156870  4634629    0.2363544   6.664  10.740   6.71570493  38.19963812  35.71   2.25288348  0.532478951288   1.600      0
jxl:d10:3           156870  3819655    0.1947928   5.277  12.029   6.71570493  38.19963812  35.71   2.25288348  0.438845458543   1.320      0
jxl:d10:4           156870  4137919    0.2110235   5.224  11.493   6.71726114  40.72074578  35.94   2.19382440  0.462948398734   1.427      0
jxl:d10:5           156870  3064736    0.1562938   1.756  12.541   9.14517281  30.37032353  35.23   2.69294022  0.420889917659   1.429      0
jxl:d10:6           156870  3123745    0.1593031   1.146  13.002   9.35053938  31.50052643  35.32   2.68063191  0.427033058530   1.496      0
jxl:d10:7           156870  3129105    0.1595765   0.913  13.258   9.23112087  31.60086736  35.33   2.67094507  0.426220007652   1.486      0
jxl:d10:1:buf3      156870  4788892    0.2442214   9.607  12.413   6.71570493  38.19963812  35.71   2.25288348  0.550202441230   1.652      0
jxl:d10:3:buf3      156870  4017087    0.2048613   9.304  11.483   6.71570493  38.19963812  35.71   2.25288348  0.461528694744   1.388      0
jxl:d10:4:buf3      156870  4396053    0.2241876   8.637  11.715   6.71726114  40.72074578  35.94   2.19382440  0.491828307200   1.516      0
jxl:d10:5:buf3      156870  3738546    0.1906564   2.497  11.388   7.36731798  37.57962463  35.82   2.26285872  0.431428576133   1.419      0
jxl:d10:6:buf3      156870  3790243    0.1932929   1.851  11.352   7.39668835  38.22880038  35.86   2.25680857  0.436224963822   1.442      0
jxl:d10:7:buf3      156870  3791016    0.1933323   1.679  11.463   7.40258595  38.25278127  35.86   2.25490305  0.435945531927   1.444      0
Aggregate:          156870 23301908    1.1883387   2.288  16.536   1.59684548  66.47757179  44.19   0.54200030  0.644079906108   2.696      0
```

* Address review comments.

* Rename update_global_state to initialize_global_state
  • Loading branch information
szabadka authored Dec 13, 2023
1 parent e986e5c commit 68c57e7
Show file tree
Hide file tree
Showing 26 changed files with 932 additions and 400 deletions.
5 changes: 3 additions & 2 deletions lib/jxl/ans_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void RoundtripTestcase(int n_histograms, int alphabet_size,

BuildAndEncodeHistograms(HistogramParams(), n_histograms, input_values_vec,
&codes, &context_map, &writer, 0, nullptr);
WriteTokens(input_values_vec[0], codes, context_map, &writer, 0, nullptr);
WriteTokens(input_values_vec[0], codes, context_map, 0, &writer, 0, nullptr);

// Magic bytes + padding
BitWriter::Allotment allotment_magic2(&writer, 24);
Expand Down Expand Up @@ -211,7 +211,8 @@ void TestCheckpointing(bool ans, bool lz77) {
auto input_values_copy = input_values;
BuildAndEncodeHistograms(params, 1, input_values_copy, &codes, &context_map,
&writer, 0, nullptr);
WriteTokens(input_values_copy[0], codes, context_map, &writer, 0, nullptr);
WriteTokens(input_values_copy[0], codes, context_map, 0, &writer, 0,
nullptr);
writer.ZeroPadToByte();
}

Expand Down
14 changes: 3 additions & 11 deletions lib/jxl/dec_ans.cc
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ Status DecodeHistograms(BitReader* br, size_t num_contexts, ANSCode* code,
if (num_contexts > 1) {
JXL_RETURN_IF_ERROR(DecodeContextMap(context_map, &num_histograms, br));
}
JXL_DEBUG_V(
4, "Decoded context map of size %" PRIuS " and %" PRIuS " histograms",
num_contexts, num_histograms);
code->lz77.nonserialized_distance_context = context_map->back();
code->use_prefix_code = br->ReadFixedBits<1>();
if (code->use_prefix_code) {
Expand All @@ -354,17 +357,6 @@ Status DecodeHistograms(BitReader* br, size_t num_contexts, ANSCode* code,
const size_t max_alphabet_size = 1 << code->log_alpha_size;
JXL_RETURN_IF_ERROR(
DecodeANSCodes(num_histograms, max_alphabet_size, br, code));
// When using LZ77, flat codes might result in valid codestreams with
// histograms that potentially allow very large bit counts.
// TODO(veluca): in principle, a valid codestream might contain a histogram
// that could allow very large numbers of bits that is never used during ANS
// decoding. There's no benefit to doing that, though.
if (!code->lz77.enabled && code->max_num_bits > 32) {
// Just emit a warning as there are many opportunities for false positives.
JXL_WARNING("Histogram can represent numbers that are too large: %" PRIuS
"\n",
code->max_num_bits);
}
return true;
}

Expand Down
Loading

0 comments on commit 68c57e7

Please sign in to comment.