Use correct adpcm block size
This commit is contained in:
6 files changed
+23
-18
No files matched your search
@@ -243,7 +243,7 @@ impl BcCamera {
|
||||
let full_block_size = block_size + 4; // Block size + predictor state
|
||||
let sub = connection.subscribe(MSG_ID_TALK)?;
|
||||
|
||||
const BLOCK_PER_PAYLOAD: usize = 4;
|
||||
const BLOCK_PER_PAYLOAD: usize = 1;
|
||||
const BLOCK_HEADER_SIZE: usize = 4;
|
||||
const SAMPLES_PER_BYTE: usize = 2;
|
||||
|
||||
@@ -255,7 +255,7 @@ impl BcCamera {
|
||||
let mut end_of_stream = false;
|
||||
while !end_of_stream {
|
||||
while payload_bytes.len() < target_chunks {
|
||||
let mut buffer = vec![0; target_chunks - payload_bytes.len()];
|
||||
let mut buffer = vec![255; target_chunks - payload_bytes.len()];
|
||||
if let Ok(read) = buffered_recv.read(&mut buffer) {
|
||||
payload_bytes.extend(&buffer[..read]);
|
||||
} else {
|
||||
@@ -269,15 +269,13 @@ impl BcCamera {
|
||||
}
|
||||
|
||||
let mut payload = vec![];
|
||||
for bytes in payload_bytes.chunks(full_block_size as usize) {
|
||||
let bcmedia_adpcm = BcMedia::Adpcm(BcMediaAdpcm {
|
||||
data: bytes.to_vec(),
|
||||
});
|
||||
for block_bytes in payload_bytes.chunks(full_block_size as usize) {
|
||||
let bytes: Vec<u8> = block_bytes.to_vec();
|
||||
let bcmedia_adpcm = BcMedia::Adpcm(BcMediaAdpcm { data: bytes });
|
||||
payload = bcmedia_adpcm.serialize(payload)?;
|
||||
}
|
||||
|
||||
let adpcm_len = payload_bytes.len();
|
||||
payload_bytes = vec![];
|
||||
|
||||
// There are two samples per byte
|
||||
//
|
||||
@@ -285,9 +283,15 @@ impl BcCamera {
|
||||
//
|
||||
// There is 1 initial sample stored in the block header so we add that in the end
|
||||
//
|
||||
let samples_sent = (adpcm_len - BLOCK_HEADER_SIZE * BLOCK_PER_PAYLOAD)
|
||||
* SAMPLES_PER_BYTE
|
||||
+ BLOCK_PER_PAYLOAD;
|
||||
let samples_sent = if adpcm_len >= BLOCK_HEADER_SIZE * BLOCK_PER_PAYLOAD {
|
||||
(adpcm_len - BLOCK_HEADER_SIZE * BLOCK_PER_PAYLOAD) * SAMPLES_PER_BYTE
|
||||
+ BLOCK_PER_PAYLOAD
|
||||
} else {
|
||||
// Zero samples in this block
|
||||
break;
|
||||
};
|
||||
|
||||
payload_bytes = vec![];
|
||||
|
||||
// Time to play the sample in seconds
|
||||
let play_length = samples_sent as f32 / sample_rate as f32;
|
||||
|
||||
@@ -287,9 +287,10 @@ fn bcmedia_adpcm(buf: &[u8]) -> IResult<&[u8], BcMediaAdpcm> {
|
||||
let (buf, payload_size) = le_u16(buf)?;
|
||||
let (buf, _payload_size_b) = le_u16(buf)?;
|
||||
let (buf, _magic) = verify(le_u16, |x| *x == MAGIC_HEADER_BCMEDIA_ADPCM_DATA)(buf)?;
|
||||
// This `block_size` is bigendian for some reason. It is block size
|
||||
let (buf, block_size) = be_u16(buf)?;
|
||||
let (buf, data_slice) = take!(buf, block_size + BLOCK_HEADER_SIZE)?;
|
||||
// This `sample_block_size` is bigendian for some reason. It is block size - BLOCK_HEADER_SIZE
|
||||
let (buf, sample_block_size) = be_u16(buf)?;
|
||||
let block_size = sample_block_size + BLOCK_HEADER_SIZE;
|
||||
let (buf, data_slice) = take!(buf, block_size)?;
|
||||
let pad_size = match payload_size as u32 % PAD_SIZE {
|
||||
0 => 0,
|
||||
n => PAD_SIZE - n,
|
||||
|
||||
@@ -166,12 +166,12 @@ pub struct BcMediaAdpcm {
|
||||
// Size of payload after header in bytes exactly the same as before
|
||||
// pub payload_size_b: u16,
|
||||
// more_magic: MAGIC_HEADER_BCMEDIA_ADPCM_DATA
|
||||
// Adpcm block size in bytes (there are twice this in samples)
|
||||
// Adpcm sample_block_size in bytes
|
||||
//
|
||||
// These bytes (and the MAGIC_HEADER_BCMEDIA_ADPCM_DATA) are included as
|
||||
// part of the payload_size. It may be more prudent to sealise them to
|
||||
// another structure.
|
||||
// pub block_size: u16,
|
||||
// pub sample_block_size: u16,
|
||||
/// The raw adpcm data in DVI-4 layout.
|
||||
///
|
||||
/// One `data` should contain 4 bytes of the adpcm predictor state then one block
|
||||
|
||||
@@ -183,6 +183,6 @@ fn bcmedia_adpcm<W: Write>(payload: &BcMediaAdpcm) -> impl SerializeFn<W> {
|
||||
le_u16((payload.data.len() + 4) as u16), // Payload + 2 byte magic + 2byte block size
|
||||
le_u16((payload.data.len() + 4) as u16), // Payload + 2 byte magic + 2byte block size
|
||||
le_u16(MAGIC_HEADER_BCMEDIA_ADPCM_DATA), // magic
|
||||
be_u16((payload.data.len() - 4) as u16), // block size = len - predictor state size (4bytes)
|
||||
be_u16((payload.data.len() - 4) as u16), // sample_block_size = len - predictor state size (4bytes)
|
||||
))
|
||||
}
|
||||
+1
-1
@@ -80,7 +80,7 @@ impl StreamOutput for GstOutputs {
|
||||
self.audsrc.write_all(&payload.data)?;
|
||||
}
|
||||
BcMedia::Adpcm(payload) => {
|
||||
self.set_format(Some(StreamFormat::Adpcm((payload.data.len() - 4) as u16)));
|
||||
self.set_format(Some(StreamFormat::Adpcm(payload.data.len() as u16)));
|
||||
self.audsrc.write_all(&payload.data)?;
|
||||
}
|
||||
_ => {
|
||||
|
||||
+1
-1
@@ -72,7 +72,7 @@ pub fn main(opt: Opt) -> Result<(), Error> {
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let block_size = talk_config.audio_config.length_per_encoder / 2;
|
||||
let block_size = (talk_config.audio_config.length_per_encoder / 2) + 4;
|
||||
let sample_rate = talk_config.audio_config.sample_rate;
|
||||
if block_size == 0 || sample_rate == 0 {
|
||||
return Err(Error::TalkUnsupported);
|
||||
|
||||
Reference in new issue
Block a user