Skip to content

Commit

Permalink
Fix EOF RPU case
Browse files Browse the repository at this point in the history
  • Loading branch information
quietvoid committed Nov 27, 2020
1 parent 745956f commit 4fe944a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
Binary file added assets/eof_rpu.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion src/dovi/rpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn parse_dovi_rpu(data: &[u8]) -> Result<DoviRpu, String> {
return Err(format!("Invalid RPU\n{:?}", &bytes));
}

let mut dovi_rpu = DoviRpu::read_rpu_data(bytes);
let mut dovi_rpu = DoviRpu::read_rpu_data(bytes, last_byte);
assert_eq!(received_crc32, dovi_rpu.rpu_data_crc32);

dovi_rpu.dovi_profile = dovi_rpu.header.get_dovi_profile();
Expand Down
20 changes: 17 additions & 3 deletions src/dovi/rpu/rpu_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct DoviRpu {
pub vdr_dm_data: Option<VdrDmData>,
pub remaining: BitVec<Msb0, u8>,
pub rpu_data_crc32: u32,
pub last_byte: u8,
}

impl DoviRpu {
Expand All @@ -30,8 +31,10 @@ impl DoviRpu {
}

#[inline(always)]
pub fn read_rpu_data(bytes: Vec<u8>) -> DoviRpu {
pub fn read_rpu_data(bytes: Vec<u8>, end_byte: u8) -> DoviRpu {
let mut dovi_rpu = DoviRpu::new(bytes);
dovi_rpu.last_byte = end_byte;

let reader = &mut dovi_rpu.reader;
dovi_rpu.header = RpuDataHeader::rpu_data_header(reader);

Expand All @@ -56,9 +59,16 @@ impl DoviRpu {
dovi_rpu.remaining.push(reader.get());
}

// EOF case
let final_len = if end_byte == 0 {
48
} else {
40
};

// CRC32 is at the end, apparently sometimes there is more unknown data
if reader.available() != 40 {
while reader.available() != 40 {
if reader.available() != final_len {
while reader.available() != final_len {
dovi_rpu.remaining.push(reader.get());
}
}
Expand Down Expand Up @@ -136,6 +146,10 @@ impl DoviRpu {
writer.write_n(&computed_crc32.to_be_bytes(), 32);
writer.write_n(&[0x80], 8);

if self.last_byte != 0x80 {
writer.write_n(&[self.last_byte], 8);
}

// Back to a u8 slice
let mut data_to_write = writer.as_slice().to_vec();
add_start_code_emulation_prevention_3_byte(&mut data_to_write);
Expand Down
38 changes: 24 additions & 14 deletions src/dovi/rpu/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn profile5() {
assert_eq!(dovi_rpu.dovi_profile, 5);
let parsed_data = dovi_rpu.write_rpu_data(mode);

assert_eq!(&original_data[2..], &parsed_data[2..]);
assert_eq!(&original_data, &parsed_data);
}

#[test]
Expand All @@ -33,7 +33,7 @@ fn profile8() {
assert_eq!(dovi_rpu.dovi_profile, 8);
let parsed_data = dovi_rpu.write_rpu_data(mode);

assert_eq!(&original_data[2..], &parsed_data[2..]);
assert_eq!(&original_data, &parsed_data);
}

#[test]
Expand All @@ -43,7 +43,7 @@ fn fel() {
assert_eq!(dovi_rpu.dovi_profile, 7);
let parsed_data = dovi_rpu.write_rpu_data(mode);

assert_eq!(&original_data[2..], &parsed_data[2..]);
assert_eq!(&original_data, &parsed_data);
}

#[test]
Expand All @@ -53,7 +53,7 @@ fn mel() {
assert_eq!(dovi_rpu.dovi_profile, 7);
let parsed_data = dovi_rpu.write_rpu_data(mode);

assert_eq!(&original_data[2..], &parsed_data[2..]);
assert_eq!(&original_data, &parsed_data);
}

#[test]
Expand All @@ -63,21 +63,21 @@ fn fel_conversions() {
assert_eq!(dovi_rpu.dovi_profile, 7);
let mut parsed_data = dovi_rpu.write_rpu_data(mode);

assert_eq!(&original_data[2..], &parsed_data[2..]);
assert_eq!(&original_data, &parsed_data);

// FEL to MEL
let (mel_data, mel_rpu) = parse_file(PathBuf::from("./assets/fel_to_mel.bin"));
assert_eq!(mel_rpu.dovi_profile, 7);

parsed_data = dovi_rpu.write_rpu_data(1);
assert_eq!(&mel_data[2..], &parsed_data[2..]);
assert_eq!(&mel_data, &parsed_data);

// FEL to 8.1
let (p81_data, p81_rpu) = parse_file(PathBuf::from("./assets/fel_to_81.bin"));
assert_eq!(p81_rpu.dovi_profile, 8);

parsed_data = dovi_rpu.write_rpu_data(2);
assert_eq!(&p81_data[2..], &parsed_data[2..]);
assert_eq!(&p81_data, &parsed_data);
}

#[test]
Expand All @@ -87,7 +87,7 @@ fn fel_to_mel() {
assert_eq!(dovi_rpu.dovi_profile, 7);
let parsed_data = dovi_rpu.write_rpu_data(mode);

assert_eq!(&original_data[2..], &parsed_data[2..]);
assert_eq!(&original_data, &parsed_data);
}

#[test]
Expand All @@ -97,7 +97,7 @@ fn fel_to_profile8() {
assert_eq!(dovi_rpu.dovi_profile, 8);
let parsed_data = dovi_rpu.write_rpu_data(mode);

assert_eq!(&original_data[2..], &parsed_data[2..]);
assert_eq!(&original_data, &parsed_data);
}

#[test]
Expand All @@ -107,21 +107,21 @@ fn mel_conversions() {
assert_eq!(dovi_rpu.dovi_profile, 7);
let mut parsed_data = dovi_rpu.write_rpu_data(mode);

assert_eq!(&original_data[2..], &parsed_data[2..]);
assert_eq!(&original_data, &parsed_data);

// MEL to MEL
let (mel_data, mel_rpu) = parse_file(PathBuf::from("./assets/mel_to_mel.bin"));
assert_eq!(mel_rpu.dovi_profile, 7);

parsed_data = dovi_rpu.write_rpu_data(1);
assert_eq!(&mel_data[2..], &parsed_data[2..]);
assert_eq!(&mel_data, &parsed_data);

// MEL to 8.1
let (p81_data, p81_rpu) = parse_file(PathBuf::from("./assets/mel_to_81.bin"));
assert_eq!(p81_rpu.dovi_profile, 8);

parsed_data = dovi_rpu.write_rpu_data(2);
assert_eq!(&p81_data[2..], &parsed_data[2..]);
assert_eq!(&p81_data, &parsed_data);
}

#[test]
Expand All @@ -131,7 +131,7 @@ fn data_before_crc32() {
assert_eq!(dovi_rpu.dovi_profile, 7);
let parsed_data = dovi_rpu.write_rpu_data(mode);

assert_eq!(&original_data[2..], &parsed_data[2..]);
assert_eq!(&original_data, &parsed_data);
}

#[test]
Expand All @@ -141,5 +141,15 @@ fn fix_se_write() {
assert_eq!(dovi_rpu.dovi_profile, 7);
let parsed_data = dovi_rpu.write_rpu_data(mode);

assert_eq!(&original_data[2..], &parsed_data[2..]);
assert_eq!(&original_data, &parsed_data);
}

#[test]
fn eof_rpu() {
let mode = 0;
let (original_data, mut dovi_rpu) = parse_file(PathBuf::from("./assets/eof_rpu.bin"));
assert_eq!(dovi_rpu.dovi_profile, 7);
let parsed_data = dovi_rpu.write_rpu_data(mode);

assert_eq!(&original_data, &parsed_data);
}

0 comments on commit 4fe944a

Please sign in to comment.