Skip to content

Commit

Permalink
Simplify floor_zero_decode (RustAudio#55)
Browse files Browse the repository at this point in the history
Since `else` branch is so much shorter it makes sense to return early on `else` condition
  • Loading branch information
AnthonyMikh authored and est31 committed Jul 12, 2019
1 parent 45c3a82 commit 0a5d07d
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,48 +116,48 @@ fn floor_zero_decode(rdr :&mut BitpackCursor, codebooks :&[Codebook],
// TODO this needs to become 128 bits wide, not just 64,
// as floor0_amplitude_bits can be up to 127.
let amplitude = try!(rdr.read_dyn_u64(fl.floor0_amplitude_bits));
if amplitude > 0 {
let booknumber = try!(rdr.read_dyn_u32(
::ilog(fl.floor0_number_of_books as u64)));
match fl.floor0_book_list.get(booknumber as usize) {
// Undecodable per spec
None => try!(Err(FloorSpecialCase::PacketUndecodable)),
Some(codebook_idx) => {
let mut coefficients = Vec::with_capacity(fl.floor0_order as usize);
let mut last = 0.0;
let codebook = &codebooks[*codebook_idx as usize];
loop {
let mut last_new = last;
let temp_vector = try!(rdr.read_huffman_vq(codebook));
if temp_vector.len() + coefficients.len() < fl.floor0_order as usize {
// Little optimisation: we don't have to care about the >= case here
for &e in temp_vector {
coefficients.push((last + e as f32).cos());
last_new = e as f32;
}
} else {
for &e in temp_vector {
coefficients.push((last + e as f32).cos());
last_new = e as f32;
// This rule makes sure that coefficients doesn't get
// larger than floor0_order and saves an allocation
// in this case
if coefficients.len() == fl.floor0_order as usize {
return Ok((coefficients, amplitude));
}
}
if amplitude <= 0 {
// This channel is unused in this frame,
// its all zeros.
return Err(FloorSpecialCase::Unused);
}

let booknumber = try!(rdr.read_dyn_u32(
::ilog(fl.floor0_number_of_books as u64)));
match fl.floor0_book_list.get(booknumber as usize) {
// Undecodable per spec
None => try!(Err(FloorSpecialCase::PacketUndecodable)),
Some(codebook_idx) => {
let mut coefficients = Vec::with_capacity(fl.floor0_order as usize);
let mut last = 0.0;
let codebook = &codebooks[*codebook_idx as usize];
loop {
let mut last_new = last;
let temp_vector = try!(rdr.read_huffman_vq(codebook));
if temp_vector.len() + coefficients.len() < fl.floor0_order as usize {
// Little optimisation: we don't have to care about the >= case here
for &e in temp_vector {
coefficients.push((last + e as f32).cos());
last_new = e as f32;
}
last += last_new;
if coefficients.len() >= fl.floor0_order as usize {
return Ok((coefficients, amplitude));
} else {
for &e in temp_vector {
coefficients.push((last + e as f32).cos());
last_new = e as f32;
// This rule makes sure that coefficients doesn't get
// larger than floor0_order and saves an allocation
// in this case
if coefficients.len() == fl.floor0_order as usize {
return Ok((coefficients, amplitude));
}
}
}
},
}
} else {
// This channel is unused in this frame,
// its all zeros.
try!(Err(FloorSpecialCase::Unused));
last += last_new;
if coefficients.len() >= fl.floor0_order as usize {
return Ok((coefficients, amplitude));
}
}
},
}
unreachable!();
}
Expand Down

0 comments on commit 0a5d07d

Please sign in to comment.