forked from andybalholm/brotli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
1737 lines (1558 loc) · 42.7 KB
/
encode.go
1
2
3
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package brotli
import (
"io"
"math"
)
/* Copyright 2016 Google Inc. All Rights Reserved.
Distributed under MIT license.
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/
/** Minimal value for ::BROTLI_PARAM_LGWIN parameter. */
const minWindowBits = 10
/**
* Maximal value for ::BROTLI_PARAM_LGWIN parameter.
*
* @note equal to @c BROTLI_MAX_DISTANCE_BITS constant.
*/
const maxWindowBits = 24
/**
* Maximal value for ::BROTLI_PARAM_LGWIN parameter
* in "Large Window Brotli" (32-bit).
*/
const largeMaxWindowBits = 30
/** Minimal value for ::BROTLI_PARAM_LGBLOCK parameter. */
const minInputBlockBits = 16
/** Maximal value for ::BROTLI_PARAM_LGBLOCK parameter. */
const maxInputBlockBits = 24
/** Minimal value for ::BROTLI_PARAM_QUALITY parameter. */
const minQuality = 0
/** Maximal value for ::BROTLI_PARAM_QUALITY parameter. */
const maxQuality = 11
/** Options for ::BROTLI_PARAM_MODE parameter. */
const (
modeGeneric = 0
modeText = 1
modeFont = 2
)
/** Default value for ::BROTLI_PARAM_QUALITY parameter. */
const defaultQuality = 11
/** Default value for ::BROTLI_PARAM_LGWIN parameter. */
const defaultWindow = 22
/** Default value for ::BROTLI_PARAM_MODE parameter. */
const defaultMode = modeGeneric
/** Operations that can be performed by streaming encoder. */
const (
operationProcess = 0
operationFlush = 1
operationFinish = 2
operationEmitMetadata = 3
)
const (
streamProcessing = 0
streamFlushRequested = 1
streamFinished = 2
streamMetadataHead = 3
streamMetadataBody = 4
)
type Writer struct {
dst io.Writer
params encoderParams
hasher_ hasherHandle
input_pos_ uint64
ringbuffer_ ringBuffer
cmd_alloc_size_ uint
commands_ []command
num_commands_ uint
num_literals_ uint
last_insert_len_ uint
last_flush_pos_ uint64
last_processed_pos_ uint64
dist_cache_ [numDistanceShortCodes]int
saved_dist_cache_ [4]int
last_bytes_ uint16
last_bytes_bits_ byte
prev_byte_ byte
prev_byte2_ byte
storage_size_ uint
storage_ []byte
small_table_ [1 << 10]int
large_table_ []int
large_table_size_ uint
cmd_depths_ [128]byte
cmd_bits_ [128]uint16
cmd_code_ [512]byte
cmd_code_numbits_ uint
command_buf_ []uint32
literal_buf_ []byte
next_out_ []byte
available_out_ uint
total_out_ uint
tiny_buf_ struct {
u64 [2]uint64
u8 [16]byte
}
remaining_metadata_bytes_ uint32
stream_state_ int
is_last_block_emitted_ bool
is_initialized_ bool
}
func inputBlockSize(s *Writer) uint {
return uint(1) << uint(s.params.lgblock)
}
func unprocessedInputSize(s *Writer) uint64 {
return s.input_pos_ - s.last_processed_pos_
}
func remainingInputBlockSize(s *Writer) uint {
var delta uint64 = unprocessedInputSize(s)
var block_size uint = inputBlockSize(s)
if delta >= uint64(block_size) {
return 0
}
return block_size - uint(delta)
}
/* Wraps 64-bit input position to 32-bit ring-buffer position preserving
"not-a-first-lap" feature. */
func wrapPosition(position uint64) uint32 {
var result uint32 = uint32(position)
var gb uint64 = position >> 30
if gb > 2 {
/* Wrap every 2GiB; The first 3GB are continuous. */
result = result&((1<<30)-1) | (uint32((gb-1)&1)+1)<<30
}
return result
}
func getBrotliStorage(s *Writer, size uint) []byte {
if s.storage_size_ < size {
s.storage_ = nil
s.storage_ = make([]byte, size)
s.storage_size_ = size
}
return s.storage_
}
func hashTableSize(max_table_size uint, input_size uint) uint {
var htsize uint = 256
for htsize < max_table_size && htsize < input_size {
htsize <<= 1
}
return htsize
}
func getHashTable(s *Writer, quality int, input_size uint, table_size *uint) []int {
var max_table_size uint = maxHashTableSize(quality)
var htsize uint = hashTableSize(max_table_size, input_size)
/* Use smaller hash table when input.size() is smaller, since we
fill the table, incurring O(hash table size) overhead for
compression, and if the input is short, we won't need that
many hash table entries anyway. */
var table []int
assert(max_table_size >= 256)
if quality == fastOnePassCompressionQuality {
/* Only odd shifts are supported by fast-one-pass. */
if htsize&0xAAAAA == 0 {
htsize <<= 1
}
}
if htsize <= uint(len(s.small_table_)) {
table = s.small_table_[:]
} else {
if htsize > s.large_table_size_ {
s.large_table_size_ = htsize
s.large_table_ = nil
s.large_table_ = make([]int, htsize)
}
table = s.large_table_
}
*table_size = htsize
for i := 0; i < int(htsize); i++ {
table[i] = 0
}
return table
}
func encodeWindowBits(lgwin int, large_window bool, last_bytes *uint16, last_bytes_bits *byte) {
if large_window {
*last_bytes = uint16((lgwin&0x3F)<<8 | 0x11)
*last_bytes_bits = 14
} else {
if lgwin == 16 {
*last_bytes = 0
*last_bytes_bits = 1
} else if lgwin == 17 {
*last_bytes = 1
*last_bytes_bits = 7
} else if lgwin > 17 {
*last_bytes = uint16((lgwin-17)<<1 | 0x01)
*last_bytes_bits = 4
} else {
*last_bytes = uint16((lgwin-8)<<4 | 0x01)
*last_bytes_bits = 7
}
}
}
/* Initializes the command and distance prefix codes for the first block. */
var initCommandPrefixCodes_kDefaultCommandDepths = [128]byte{
0,
4,
4,
5,
6,
6,
7,
7,
7,
7,
7,
8,
8,
8,
8,
8,
0,
0,
0,
4,
4,
4,
4,
4,
5,
5,
6,
6,
6,
6,
7,
7,
7,
7,
10,
10,
10,
10,
10,
10,
0,
4,
4,
5,
5,
5,
6,
6,
7,
8,
8,
9,
10,
10,
10,
10,
10,
10,
10,
10,
10,
10,
10,
10,
5,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
6,
6,
6,
6,
6,
6,
5,
5,
5,
5,
5,
5,
4,
4,
4,
4,
4,
4,
4,
5,
5,
5,
5,
5,
5,
6,
6,
7,
7,
7,
8,
10,
12,
12,
12,
12,
12,
12,
12,
12,
12,
12,
12,
12,
}
var initCommandPrefixCodes_kDefaultCommandBits = [128]uint16{
0,
0,
8,
9,
3,
35,
7,
71,
39,
103,
23,
47,
175,
111,
239,
31,
0,
0,
0,
4,
12,
2,
10,
6,
13,
29,
11,
43,
27,
59,
87,
55,
15,
79,
319,
831,
191,
703,
447,
959,
0,
14,
1,
25,
5,
21,
19,
51,
119,
159,
95,
223,
479,
991,
63,
575,
127,
639,
383,
895,
255,
767,
511,
1023,
14,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
27,
59,
7,
39,
23,
55,
30,
1,
17,
9,
25,
5,
0,
8,
4,
12,
2,
10,
6,
21,
13,
29,
3,
19,
11,
15,
47,
31,
95,
63,
127,
255,
767,
2815,
1791,
3839,
511,
2559,
1535,
3583,
1023,
3071,
2047,
4095,
}
var initCommandPrefixCodes_kDefaultCommandCode = []byte{
0xff,
0x77,
0xd5,
0xbf,
0xe7,
0xde,
0xea,
0x9e,
0x51,
0x5d,
0xde,
0xc6,
0x70,
0x57,
0xbc,
0x58,
0x58,
0x58,
0xd8,
0xd8,
0x58,
0xd5,
0xcb,
0x8c,
0xea,
0xe0,
0xc3,
0x87,
0x1f,
0x83,
0xc1,
0x60,
0x1c,
0x67,
0xb2,
0xaa,
0x06,
0x83,
0xc1,
0x60,
0x30,
0x18,
0xcc,
0xa1,
0xce,
0x88,
0x54,
0x94,
0x46,
0xe1,
0xb0,
0xd0,
0x4e,
0xb2,
0xf7,
0x04,
0x00,
}
var initCommandPrefixCodes_kDefaultCommandCodeNumBits uint = 448
func initCommandPrefixCodes(cmd_depths []byte, cmd_bits []uint16, cmd_code []byte, cmd_code_numbits *uint) {
copy(cmd_depths, initCommandPrefixCodes_kDefaultCommandDepths[:])
copy(cmd_bits, initCommandPrefixCodes_kDefaultCommandBits[:])
/* Initialize the pre-compressed form of the command and distance prefix
codes. */
copy(cmd_code, initCommandPrefixCodes_kDefaultCommandCode)
*cmd_code_numbits = initCommandPrefixCodes_kDefaultCommandCodeNumBits
}
/* Decide about the context map based on the ability of the prediction
ability of the previous byte UTF8-prefix on the next byte. The
prediction ability is calculated as Shannon entropy. Here we need
Shannon entropy instead of 'BitsEntropy' since the prefix will be
encoded with the remaining 6 bits of the following byte, and
BitsEntropy will assume that symbol to be stored alone using Huffman
coding. */
var kStaticContextMapContinuation = [64]uint32{
1,
1,
2,
2,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
}
var kStaticContextMapSimpleUTF8 = [64]uint32{
0,
0,
1,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
}
func chooseContextMap(quality int, bigram_histo []uint32, num_literal_contexts *uint, literal_context_map *[]uint32) {
var monogram_histo = [3]uint32{0}
var two_prefix_histo = [6]uint32{0}
var total uint
var i uint
var dummy uint
var entropy [4]float64
for i = 0; i < 9; i++ {
monogram_histo[i%3] += bigram_histo[i]
two_prefix_histo[i%6] += bigram_histo[i]
}
entropy[1] = shannonEntropy(monogram_histo[:], 3, &dummy)
entropy[2] = (shannonEntropy(two_prefix_histo[:], 3, &dummy) + shannonEntropy(two_prefix_histo[3:], 3, &dummy))
entropy[3] = 0
for i = 0; i < 3; i++ {
entropy[3] += shannonEntropy(bigram_histo[3*i:], 3, &dummy)
}
total = uint(monogram_histo[0] + monogram_histo[1] + monogram_histo[2])
assert(total != 0)
entropy[0] = 1.0 / float64(total)
entropy[1] *= entropy[0]
entropy[2] *= entropy[0]
entropy[3] *= entropy[0]
if quality < minQualityForHqContextModeling {
/* 3 context models is a bit slower, don't use it at lower qualities. */
entropy[3] = entropy[1] * 10
}
/* If expected savings by symbol are less than 0.2 bits, skip the
context modeling -- in exchange for faster decoding speed. */
if entropy[1]-entropy[2] < 0.2 && entropy[1]-entropy[3] < 0.2 {
*num_literal_contexts = 1
} else if entropy[2]-entropy[3] < 0.02 {
*num_literal_contexts = 2
*literal_context_map = kStaticContextMapSimpleUTF8[:]
} else {
*num_literal_contexts = 3
*literal_context_map = kStaticContextMapContinuation[:]
}
}
/* Decide if we want to use a more complex static context map containing 13
context values, based on the entropy reduction of histograms over the
first 5 bits of literals. */
var kStaticContextMapComplexUTF8 = [64]uint32{
11,
11,
12,
12,
0,
0,
0,
0,
1,
1,
9,
9,
2,
2,
2,
2,
1,
1,
1,
1,
8,
3,
3,
3,
1,
1,
1,
1,
2,
2,
2,
2,
8,
4,
4,
4,
8,
7,
4,
4,
8,
0,
0,
0,
3,
3,
3,
3,
5,
5,
10,
5,
5,
5,
10,
5,
6,
6,
6,
6,
6,
6,
6,
6,
}
func shouldUseComplexStaticContextMap(input []byte, start_pos uint, length uint, mask uint, quality int, size_hint uint, num_literal_contexts *uint, literal_context_map *[]uint32) bool {
/* Try the more complex static context map only for long data. */
if size_hint < 1<<20 {
return false
} else {
var end_pos uint = start_pos + length
var combined_histo = [32]uint32{0}
var context_histo = [13][32]uint32{[32]uint32{0}}
var total uint32 = 0
var entropy [3]float64
var dummy uint
var i uint
var utf8_lut contextLUT = getContextLUT(contextUTF8)
/* To make entropy calculations faster and to fit on the stack, we collect
histograms over the 5 most significant bits of literals. One histogram
without context and 13 additional histograms for each context value. */
for ; start_pos+64 <= end_pos; start_pos += 4096 {
var stride_end_pos uint = start_pos + 64
var prev2 byte = input[start_pos&mask]
var prev1 byte = input[(start_pos+1)&mask]
var pos uint
/* To make the analysis of the data faster we only examine 64 byte long
strides at every 4kB intervals. */
for pos = start_pos + 2; pos < stride_end_pos; pos++ {
var literal byte = input[pos&mask]
var context byte = byte(kStaticContextMapComplexUTF8[getContext(prev1, prev2, utf8_lut)])
total++
combined_histo[literal>>3]++
context_histo[context][literal>>3]++
prev2 = prev1
prev1 = literal
}
}
entropy[1] = shannonEntropy(combined_histo[:], 32, &dummy)
entropy[2] = 0
for i = 0; i < 13; i++ {
entropy[2] += shannonEntropy(context_histo[i][0:], 32, &dummy)
}
entropy[0] = 1.0 / float64(total)
entropy[1] *= entropy[0]
entropy[2] *= entropy[0]
/* The triggering heuristics below were tuned by compressing the individual
files of the silesia corpus. If we skip this kind of context modeling
for not very well compressible input (i.e. entropy using context modeling
is 60% of maximal entropy) or if expected savings by symbol are less
than 0.2 bits, then in every case when it triggers, the final compression
ratio is improved. Note however that this heuristics might be too strict
for some cases and could be tuned further. */
if entropy[2] > 3.0 || entropy[1]-entropy[2] < 0.2 {
return false
} else {
*num_literal_contexts = 13
*literal_context_map = kStaticContextMapComplexUTF8[:]
return true
}
}
}
func decideOverLiteralContextModeling(input []byte, start_pos uint, length uint, mask uint, quality int, size_hint uint, num_literal_contexts *uint, literal_context_map *[]uint32) {
if quality < minQualityForContextModeling || length < 64 {
return
} else if shouldUseComplexStaticContextMap(input, start_pos, length, mask, quality, size_hint, num_literal_contexts, literal_context_map) {
} else /* Context map was already set, nothing else to do. */
{
var end_pos uint = start_pos + length
/* Gather bi-gram data of the UTF8 byte prefixes. To make the analysis of
UTF8 data faster we only examine 64 byte long strides at every 4kB
intervals. */
var bigram_prefix_histo = [9]uint32{0}
for ; start_pos+64 <= end_pos; start_pos += 4096 {
var lut = [4]int{0, 0, 1, 2}
var stride_end_pos uint = start_pos + 64
var prev int = lut[input[start_pos&mask]>>6] * 3
var pos uint
for pos = start_pos + 1; pos < stride_end_pos; pos++ {
var literal byte = input[pos&mask]
bigram_prefix_histo[prev+lut[literal>>6]]++
prev = lut[literal>>6] * 3
}
}
chooseContextMap(quality, bigram_prefix_histo[0:], num_literal_contexts, literal_context_map)
}
}
func shouldCompress_encode(data []byte, mask uint, last_flush_pos uint64, bytes uint, num_literals uint, num_commands uint) bool {
/* TODO: find more precise minimal block overhead. */
if bytes <= 2 {
return false
}
if num_commands < (bytes>>8)+2 {
if float64(num_literals) > 0.99*float64(bytes) {
var literal_histo = [256]uint32{0}
const kSampleRate uint32 = 13
const kMinEntropy float64 = 7.92
var bit_cost_threshold float64 = float64(bytes) * kMinEntropy / float64(kSampleRate)
var t uint = uint((uint32(bytes) + kSampleRate - 1) / kSampleRate)
var pos uint32 = uint32(last_flush_pos)
var i uint
for i = 0; i < t; i++ {
literal_histo[data[pos&uint32(mask)]]++
pos += kSampleRate
}
if bitsEntropy(literal_histo[:], 256) > bit_cost_threshold {
return false
}
}
}
return true
}
/* Chooses the literal context mode for a metablock */
func chooseContextMode(params *encoderParams, data []byte, pos uint, mask uint, length uint) int {
/* We only do the computation for the option of something else than
CONTEXT_UTF8 for the highest qualities */
if params.quality >= minQualityForHqBlockSplitting && !isMostlyUTF8(data, pos, mask, length, kMinUTF8Ratio) {
return contextSigned
}
return contextUTF8
}
func writeMetaBlockInternal(data []byte, mask uint, last_flush_pos uint64, bytes uint, is_last bool, literal_context_mode int, params *encoderParams, prev_byte byte, prev_byte2 byte, num_literals uint, num_commands uint, commands []command, saved_dist_cache []int, dist_cache []int, storage_ix *uint, storage []byte) {
var wrapped_last_flush_pos uint32 = wrapPosition(last_flush_pos)
var last_bytes uint16
var last_bytes_bits byte
var literal_context_lut contextLUT = getContextLUT(literal_context_mode)
var block_params encoderParams = *params
if bytes == 0 {
/* Write the ISLAST and ISEMPTY bits. */
writeBits(2, 3, storage_ix, storage)
*storage_ix = (*storage_ix + 7) &^ 7
return
}
if !shouldCompress_encode(data, mask, last_flush_pos, bytes, num_literals, num_commands) {
/* Restore the distance cache, as its last update by
CreateBackwardReferences is now unused. */
copy(dist_cache, saved_dist_cache[:4])
storeUncompressedMetaBlock(is_last, data, uint(wrapped_last_flush_pos), mask, bytes, storage_ix, storage)
return
}
assert(*storage_ix <= 14)
last_bytes = uint16(storage[1])<<8 | uint16(storage[0])
last_bytes_bits = byte(*storage_ix)
if params.quality <= maxQualityForStaticEntropyCodes {
storeMetaBlockFast(data, uint(wrapped_last_flush_pos), bytes, mask, is_last, params, commands, num_commands, storage_ix, storage)
} else if params.quality < minQualityForBlockSplit {
storeMetaBlockTrivial(data, uint(wrapped_last_flush_pos), bytes, mask, is_last, params, commands, num_commands, storage_ix, storage)
} else {
var mb metaBlockSplit
initMetaBlockSplit(&mb)
if params.quality < minQualityForHqBlockSplitting {
var num_literal_contexts uint = 1
var literal_context_map []uint32 = nil
if !params.disable_literal_context_modeling {
decideOverLiteralContextModeling(data, uint(wrapped_last_flush_pos), bytes, mask, params.quality, params.size_hint, &num_literal_contexts, &literal_context_map)
}
buildMetaBlockGreedy(data, uint(wrapped_last_flush_pos), mask, prev_byte, prev_byte2, literal_context_lut, num_literal_contexts, literal_context_map, commands, num_commands, &mb)
} else {
buildMetaBlock(data, uint(wrapped_last_flush_pos), mask, &block_params, prev_byte, prev_byte2, commands, num_commands, literal_context_mode, &mb)
}
if params.quality >= minQualityForOptimizeHistograms {
/* The number of distance symbols effectively used for distance
histograms. It might be less than distance alphabet size
for "Large Window Brotli" (32-bit). */
var num_effective_dist_codes uint32 = block_params.dist.alphabet_size
if num_effective_dist_codes > numHistogramDistanceSymbols {
num_effective_dist_codes = numHistogramDistanceSymbols
}
optimizeHistograms(num_effective_dist_codes, &mb)
}
storeMetaBlock(data, uint(wrapped_last_flush_pos), bytes, mask, prev_byte, prev_byte2, is_last, &block_params, literal_context_mode, commands, num_commands, &mb, storage_ix, storage)
destroyMetaBlockSplit(&mb)
}
if bytes+4 < *storage_ix>>3 {
/* Restore the distance cache and last byte. */
copy(dist_cache, saved_dist_cache[:4])