Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add option to force compressed data to be byte aligned #49

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions c/arg.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ size_t set_options(BrotliEncoderParameter *out_encoder_param_keys,
out_encoder_param_values[ret] = 1;
ret += 1;
}
if (strstr(argv[i], "-appendable") == argv[i]) {
out_encoder_param_keys[ret] = BROTLI_PARAM_APPENDABLE;
out_encoder_param_values[ret] = 1;
ret += 1;
}
if (strstr(argv[i], "-bytealign") == argv[i]) {
out_encoder_param_keys[ret] = BROTLI_PARAM_BYTE_ALIGN;
out_encoder_param_values[ret] = 1;
ret += 1;
}
if (strstr(argv[i], "-bare") == argv[i]) {
out_encoder_param_keys[ret] = BROTLI_PARAM_BARE_STREAM;
out_encoder_param_values[ret] = 1;
ret += 1;
}
}
return ret;
}
6 changes: 5 additions & 1 deletion c/brotli/encode.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,11 @@ typedef enum BrotliEncoderParameter {
BROTLI_PARAM_AVOID_DISTANCE_PREFIX_SEARCH = 166,
BROTLI_PARAM_CATABLE = 167,
BROTLI_PARAM_APPENDABLE = 168,
BROTLI_PARAM_MAGIC_NUMBER = 169
BROTLI_PARAM_MAGIC_NUMBER = 169,
BROTLI_PARAM_NO_DICTIONARY = 170,
BROTLI_PARAM_FAVOR_EFFICIENCY = 171,
BROTLI_PARAM_BYTE_ALIGN = 172,
BROTLI_PARAM_BARE_STREAM = 173
} BrotliEncoderParameter;

/**
Expand Down
10 changes: 10 additions & 0 deletions c/go/brotli/brotli.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type CompressionOptions struct {
Catable bool
Appendable bool
Magic bool
ByteAlign bool
BareStream bool
Mode int
LgWin byte
LgBlock byte
Expand Down Expand Up @@ -157,6 +159,14 @@ func makeCompressionOptionsStreams(options CompressionOptions,
qualityParams = append(qualityParams, C.BROTLI_PARAM_MAGIC_NUMBER)
values = append(values, 1)
}
if options.ByteAlign {
qualityParams = append(qualityParams, C.BROTLI_PARAM_BYTE_ALIGN)
values = append(values, 1)
}
if options.BareStream {
qualityParams = append(qualityParams, C.BROTLI_PARAM_BARE_STREAM)
values = append(values, 1)
}
if options.Mode != 0 {
qualityParams = append(qualityParams, C.BROTLI_PARAM_MODE)
values = append(values, C.uint32_t(options.Mode))
Expand Down
6 changes: 5 additions & 1 deletion c/go/brotli/brotli/encode.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,11 @@ typedef enum BrotliEncoderParameter {
BROTLI_PARAM_AVOID_DISTANCE_PREFIX_SEARCH = 166,
BROTLI_PARAM_CATABLE = 167,
BROTLI_PARAM_APPENDABLE = 168,
BROTLI_PARAM_MAGIC_NUMBER = 169
BROTLI_PARAM_MAGIC_NUMBER = 169,
BROTLI_PARAM_NO_DICTIONARY = 170,
BROTLI_PARAM_FAVOR_EFFICIENCY = 171,
BROTLI_PARAM_BYTE_ALIGN = 172,
BROTLI_PARAM_BARE_STREAM = 173,
} BrotliEncoderParameter;

/**
Expand Down
4 changes: 4 additions & 0 deletions c/py/brotli.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ def BrotliParseHeader(raw_data):
BROTLI_PARAM_CATABLE = 167
BROTLI_PARAM_APPENDABLE = 168
BROTLI_PARAM_MAGIC_NUMBER = 169
BROTLI_PARAM_NO_DICTIONARY = 170
BROTLI_PARAM_FAVOR_EFFICIENCY = 171
BROTLI_PARAM_BYTE_ALIGN = 172
BROTLI_PARAM_BARE_STREAM = 173

#simple test binary
def main(args):
Expand Down
18 changes: 17 additions & 1 deletion src/bin/brotli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,15 @@ fn main() {
use_work_pool = false;
continue;
}

if (argument == "-bytealign" || argument == "--bytealign") && !double_dash {
params.byte_align = true;
continue;
}
if (argument == "-bare" || argument == "--bare") && !double_dash {
params.bare_stream = true;
params.byte_align = true;
continue;
}
if (argument == "-appendable" || argument == "--appendable") && !double_dash {
params.appendable = true;
continue;
Expand Down Expand Up @@ -746,6 +754,14 @@ fn main() {
}
panic!("Unknown Argument {:}", argument);
}
if params.bare_stream && !params.appendable {
println_stderr!("bare streams only supported when catable or appendable!");
return;
}
if params.byte_align && !params.appendable {
println_stderr!("byte aligned streams only supported when catable or appendable!");
return;
}
if filenames[0] != "" {
let mut input = match File::open(&Path::new(&filenames[0])) {
Err(why) => panic!("couldn't open {:}\n{:}", filenames[0], why),
Expand Down
5 changes: 5 additions & 0 deletions src/enc/backward_references/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ pub struct BrotliEncoderParams {
pub large_window: bool,
// avoid search for the best ndirect vs npostfix parameters for distance
pub avoid_distance_prefix_search: bool,
// inserts an extra empty metadata block before the final empty metablock in
// catable/appendable mode so concatination tools can just remove the last byte
pub byte_align: bool,
// do not emit a stream header or empty last block at end of data
pub bare_stream: bool,
// construct brotli in such a way that it may be concatenated with another brotli file using appropriate bit ops
pub catable: bool,
// can use the dictionary (default yes unless catable is set)
Expand Down
37 changes: 35 additions & 2 deletions src/enc/brotli_bit_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2765,18 +2765,51 @@ pub fn BrotliStoreUncompressedMetaBlock<Cb, Alloc: BrotliAlloc>
}
}


pub fn BrotliStoreSyncMetaBlock(storage_ix: &mut usize, storage: &mut [u8]) {
BrotliWriteBits(6, 6, storage_ix, storage);
BrotliWriteBits(6u8, 6u64, storage_ix, storage);
JumpToByteBoundary(storage_ix, storage);
}

pub fn BrotliWritePaddingMetaBlock(storage_ix: &mut usize, storage: &mut [u8]) {
if *storage_ix & 7 != 0 {
BrotliWriteBits(6u8, 6u64, storage_ix, storage);
JumpToByteBoundary(storage_ix, storage);
}
}

pub fn BrotliWriteEmptyLastMetaBlock(storage_ix: &mut usize, storage: &mut [u8]) {
BrotliWriteBits(1u8, 1u64, storage_ix, storage);
BrotliWriteBits(1u8, 1u64, storage_ix, storage);
JumpToByteBoundary(storage_ix, storage);
}

pub fn BrotliWriteRawMetadataMetaBlock(storage_ix: &mut usize, storage: &mut [u8], len: usize, data: &mut [u8]) {
BrotliWriteBits(1u8, 0u64, storage_ix, storage); // not last
BrotliWriteBits(2u8, 3u64, storage_ix, storage); // MNIBBLES = 0 (pattern 1,1)
BrotliWriteBits(1u8, 0u64, storage_ix, storage); // reserved
if len > 16777215 {
panic!("metadata too large");
} else if len > 65535 {
BrotliWriteBits(2u8, 3u64, storage_ix, storage);
BrotliWriteBits(8u8, ((len >> 16) & 255) as u64, storage_ix, storage);
BrotliWriteBits(8u8, ((len >> 8) & 255) as u64, storage_ix, storage);
BrotliWriteBits(8u8, (len & 255) as u64, storage_ix, storage);
} else if len > 255 {
BrotliWriteBits(2u8, 2u64, storage_ix, storage);
BrotliWriteBits(8u8, ((len >> 8) & 255) as u64, storage_ix, storage);
BrotliWriteBits(8u8, (len & 255) as u64, storage_ix, storage);
} else if len == 0 {
BrotliWriteBits(2u8, 0u64, storage_ix, storage);
} else {
BrotliWriteBits(2u8, 1u64, storage_ix, storage);
BrotliWriteBits(8u8, (len & 255) as u64, storage_ix, storage);
}
JumpToByteBoundary(storage_ix, storage);
for index in 0..len {
BrotliWriteBits(8u8, data[index] as u64, storage_ix, storage);
}
}

const MAX_SIZE_ENCODING:usize = 10;

fn encode_base_128(mut value: u64)-> (usize, [u8;MAX_SIZE_ENCODING]) {
Expand Down
49 changes: 41 additions & 8 deletions src/enc/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use super::block_split::BlockSplit;
use super::brotli_bit_stream::{BrotliBuildAndStoreHuffmanTreeFast, BrotliStoreHuffmanTree,
BrotliStoreMetaBlock, BrotliStoreMetaBlockFast,
BrotliStoreMetaBlockTrivial, BrotliStoreUncompressedMetaBlock,
BrotliWriteEmptyLastMetaBlock, BrotliWriteMetadataMetaBlock,
BrotliWriteEmptyLastMetaBlock,
BrotliWritePaddingMetaBlock, BrotliWriteMetadataMetaBlock,
MetaBlockSplit, RecoderState, JumpToByteBoundary};

use enc::input_pair::InputReferenceMut;
Expand Down Expand Up @@ -340,6 +341,17 @@ value: u32) -> i32 {
params.favor_cpu_efficiency = value != 0;
return 1i32;
}
if p as (i32) == BrotliEncoderParameter::BROTLI_PARAM_BYTE_ALIGN as (i32) {
Copy link
Contributor

@philippeitis philippeitis Dec 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if p == BrotliEncoderParameter::BROTLI_PARAM_BYTE_ALIGN should compile on rust 1.12? (I know that the rest of the code in this function uses the other convention, but from what I understand, this is all machine generated, and not how you'd naturally write the code).

params.byte_align = value != 0;
return 1i32;
}
if p as (i32) == BrotliEncoderParameter::BROTLI_PARAM_BARE_STREAM as (i32) {
params.bare_stream = value != 0;
if !params.byte_align {
params.byte_align = value != 0;
}
return 1i32;
}
0i32
}

Expand Down Expand Up @@ -397,6 +409,8 @@ pub fn BrotliEncoderInitParams() -> BrotliEncoderParams {
cdf_adaptation_detection: 0,
prior_bitmask_detection: 0,
literal_adaptation: [(0,0);4],
byte_align: false,
bare_stream: false,
catable: false,
use_dictionary: true,
appendable: false,
Expand Down Expand Up @@ -621,7 +635,12 @@ pub fn SanitizeParams(params: &mut BrotliEncoderParams) {
}
}
if params.catable {
params.appendable = true;
params.appendable = true;
}
if params.bare_stream {
params.byte_align = true;
} else if !params.appendable {
params.byte_align = false;
}
}

Expand Down Expand Up @@ -721,7 +740,9 @@ fn EnsureInitialized<Alloc: BrotliAlloc>
if (*s).params.quality == 0i32 || (*s).params.quality == 1i32 {
lgwin = brotli_max_int(lgwin, 18i32);
}
EncodeWindowBits(lgwin, s.params.large_window, &mut (*s).last_bytes_, &mut (*s).last_bytes_bits_);
if !((*s).params.catable && (*s).params.bare_stream) {
EncodeWindowBits(lgwin, s.params.large_window, &mut (*s).last_bytes_, &mut (*s).last_bytes_bits_);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to do &mut (*s).field - &mut s.field works identically and looks nicer (again, machine generated code which isn't very natural).

}
}
if (*s).params.quality == 0i32 {
InitCommandPrefixCodes(&mut (*s).cmd_depths_[..],
Expand Down Expand Up @@ -1948,6 +1969,15 @@ fn DecideOverLiteralContextModeling(input: &[u8],
literal_context_map);
}
}
fn WriteEmptyLastBlocksInternal(params: &BrotliEncoderParams, storage_ix: &mut usize, storage: &mut [u8]) {
// insert empty block for byte alignment if required
if params.byte_align {
BrotliWritePaddingMetaBlock(storage_ix, storage);
}
if !params.bare_stream {
BrotliWriteEmptyLastMetaBlock(storage_ix, storage)
}
}
fn WriteMetaBlockInternal<Alloc: BrotliAlloc,
Cb>
(alloc: &mut Alloc,
Expand Down Expand Up @@ -1975,7 +2005,7 @@ fn WriteMetaBlockInternal<Alloc: BrotliAlloc,
&mut [interface::StaticCommand],
interface::InputPair, &mut Alloc) {
let actual_is_last = is_last;
if params.appendable {
if params.appendable || params.byte_align {
is_last = 0;
} else {
assert_eq!(params.catable, false); // Sanitize Params senforces this constraint
Expand All @@ -1986,8 +2016,7 @@ fn WriteMetaBlockInternal<Alloc: BrotliAlloc,
let literal_context_lut = BROTLI_CONTEXT_LUT(literal_context_mode);
let mut block_params = params.clone();
if bytes == 0usize {
BrotliWriteBits(2usize, 3, storage_ix, storage);
*storage_ix = (*storage_ix).wrapping_add(7u32 as (usize)) & !7u32 as (usize);
WriteEmptyLastBlocksInternal(params, storage_ix, storage);
return;
}
if ShouldCompress(data,
Expand All @@ -2010,7 +2039,7 @@ fn WriteMetaBlockInternal<Alloc: BrotliAlloc,
false,
cb);
if actual_is_last != is_last {
BrotliWriteEmptyLastMetaBlock(storage_ix, storage)
WriteEmptyLastBlocksInternal(params, storage_ix, storage);
}
return;
}
Expand Down Expand Up @@ -2150,7 +2179,7 @@ fn WriteMetaBlockInternal<Alloc: BrotliAlloc,
cb);
}
if actual_is_last != is_last {
BrotliWriteEmptyLastMetaBlock(storage_ix, storage)
WriteEmptyLastBlocksInternal(params, storage_ix, storage);
}
}

Expand Down Expand Up @@ -2247,6 +2276,10 @@ fn EncodeData<Alloc: BrotliAlloc,
*out_size = catable_header_size;
s.is_first_mb = IsFirst::HeaderWritten;
}
// fixup for empty stream - note: catable is always appendable
if bytes == 0 && s.params.byte_align && s.params.appendable && !s.params.catable {
BrotliWritePaddingMetaBlock(&mut storage_ix, (*s).storage_.slice_mut());
}
}
if let IsFirst::BothCatableBytesWritten = s.is_first_mb {
// nothing to do here, move along
Expand Down
4 changes: 2 additions & 2 deletions src/enc/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub enum BrotliEncoderParameter {
BROTLI_PARAM_MAGIC_NUMBER = 169,
BROTLI_PARAM_NO_DICTIONARY = 170,
BROTLI_PARAM_FAVOR_EFFICIENCY = 171,
BROTLI_PARAM_BYTE_ALIGN = 172,
BROTLI_PARAM_BARE_STREAM = 173,
UNUSED7=7,
UNUSED8=8,
UNUSED9=9,
Expand Down Expand Up @@ -172,8 +174,6 @@ pub enum BrotliEncoderParameter {
UNUSED147=147,
UNUSED148=148,
UNUSED149=149,
UNUSED172=172,
UNUSED173=173,
UNUSED174=174,
UNUSED175=175,
UNUSED176=176,
Expand Down