Skip to content

Commit 9a76ff0

Browse files
rom1vtdaede
authored andcommitted
Optimize tile size bytes in bitstream
The tile size may be encoded using 1, 2, 3 or 4 bytes. For simplicity, it always used 4 bytes. Instead, use the number of bytes required by the biggest tile.
1 parent ec82d80 commit 9a76ff0

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

‎src/encoder.rs‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ pub struct FrameState<T: Pixel> {
418418
pub rec: Frame<T>,
419419
pub cdfs: CDFContext,
420420
pub context_update_tile_id: usize, // tile id used for the CDFontext
421+
pub max_tile_size_bytes: u32,
421422
pub deblock: DeblockState,
422423
pub segmentation: SegmentationState,
423424
pub restoration: RestorationState,
@@ -447,6 +448,7 @@ impl<T: Pixel> FrameState<T> {
447448
rec: Frame::new(luma_width, luma_height, fi.sequence.chroma_sampling),
448449
cdfs: CDFContext::new(0),
449450
context_update_tile_id: 0,
451+
max_tile_size_bytes: 0,
450452
deblock: Default::default(),
451453
segmentation: Default::default(),
452454
restoration: rs,
@@ -2144,7 +2146,7 @@ fn encode_tile_group<T: Pixel>(fi: &FrameInvariants<T>, fs: &mut FrameState<T>)
21442146
fs.t.print_code();
21452147
}
21462148

2147-
let (idx_max, _max_len) = raw_tiles
2149+
let (idx_max, max_len) = raw_tiles
21482150
.iter()
21492151
.map(Vec::len)
21502152
.enumerate()
@@ -2156,10 +2158,14 @@ fn encode_tile_group<T: Pixel>(fi: &FrameInvariants<T>, fs: &mut FrameState<T>)
21562158
fs.cdfs = cdfs[idx_max];
21572159
fs.cdfs.reset_counts();
21582160

2159-
build_raw_tile_group(ti, &raw_tiles)
2161+
let max_tile_size_bytes = ((max_len as u32).ilog() + 7) / 8;
2162+
debug_assert!(max_tile_size_bytes > 0 && max_tile_size_bytes <= 4);
2163+
fs.max_tile_size_bytes = max_tile_size_bytes;
2164+
2165+
build_raw_tile_group(ti, &raw_tiles, max_tile_size_bytes)
21602166
}
21612167

2162-
fn build_raw_tile_group(ti: &TilingInfo, raw_tiles: &[Vec<u8>]) -> Vec<u8> {
2168+
fn build_raw_tile_group(ti: &TilingInfo, raw_tiles: &[Vec<u8>], max_tile_size_bytes: u32) -> Vec<u8> {
21632169
// <https://aomediacodec.github.io/av1-spec/#general-tile-group-obu-syntax>
21642170
let mut raw = Vec::new();
21652171
let mut bw = BitWriter::endian(&mut raw, BigEndian);
@@ -2172,7 +2178,7 @@ fn build_raw_tile_group(ti: &TilingInfo, raw_tiles: &[Vec<u8>]) -> Vec<u8> {
21722178
let last = raw_tiles.len() - 1;
21732179
if i != last {
21742180
let tile_size_minus_1 = raw_tile.len() - 1;
2175-
bw.write_le(4, tile_size_minus_1 as u64).unwrap();
2181+
bw.write_le(max_tile_size_bytes, tile_size_minus_1 as u64).unwrap();
21762182
}
21772183
bw.write_bytes(&raw_tile).unwrap();
21782184
}

‎src/header.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,7 @@ impl<W: io::Write> UncompressedHeader for BitWriter<W, BigEndian> {
645645
self.write(tiles_log2 as u32, fs.context_update_tile_id as u32)?;
646646

647647
// tile_size_bytes_minus_1
648-
// force TileSizeBytes == 4, to be optimized using actual tile sizes
649-
self.write(2, 3);
648+
self.write(2, fs.max_tile_size_bytes - 1);
650649
}
651650

652651
// quantization

0 commit comments

Comments
 (0)