Add bcmedia serialisation and remove redundent data from the bcmedia model

This commit is contained in:
QuantumEntangledAndy committed 2021-08-03 17:07:08 +07:00
1 parent 4ecb2a1ae4
commit 692b7429c7
4 files changed
+240 -51

No files matched your search

+27 -14
View File
@@ -2,7 +2,7 @@ use super::model::BcMediaIframe;
use super::model::*;
use err_derive::Error;
use nom::IResult;
use nom::{combinator::*, number::streaming::*, take, take_str};
use nom::{combinator::*, named, number::streaming::*, take, take_str};
use std::io::Read;
// PAD_SIZE: Media packets use 8 byte padding
@@ -113,7 +113,7 @@ fn bcmedia(buf: &[u8]) -> IResult<&[u8], BcMedia> {
}
fn bcmedia_info_v1(buf: &[u8]) -> IResult<&[u8], BcMediaInfoV1> {
let (buf, header_size) = verify(le_u32, |x| *x == 32)(buf)?;
let (buf, _header_size) = verify(le_u32, |x| *x == 32)(buf)?;
let (buf, video_width) = le_u32(buf)?;
let (buf, video_height) = le_u32(buf)?;
let (buf, _unknown) = le_u8(buf)?;
@@ -135,7 +135,7 @@ fn bcmedia_info_v1(buf: &[u8]) -> IResult<&[u8], BcMediaInfoV1> {
Ok((
buf,
BcMediaInfoV1 {
header_size,
// header_size,
video_width,
video_height,
fps,
@@ -156,7 +156,7 @@ fn bcmedia_info_v1(buf: &[u8]) -> IResult<&[u8], BcMediaInfoV1> {
}
fn bcmedia_info_v2(buf: &[u8]) -> IResult<&[u8], BcMediaInfoV2> {
let (buf, header_size) = verify(le_u32, |x| *x == 32)(buf)?;
let (buf, _header_size) = verify(le_u32, |x| *x == 32)(buf)?;
let (buf, video_width) = le_u32(buf)?;
let (buf, video_height) = le_u32(buf)?;
let (buf, _unknown) = le_u8(buf)?;
@@ -178,7 +178,7 @@ fn bcmedia_info_v2(buf: &[u8]) -> IResult<&[u8], BcMediaInfoV2> {
Ok((
buf,
BcMediaInfoV2 {
header_size,
// header_size,
video_width,
video_height,
fps,
@@ -199,7 +199,8 @@ fn bcmedia_info_v2(buf: &[u8]) -> IResult<&[u8], BcMediaInfoV2> {
}
fn bcmedia_iframe(buf: &[u8]) -> IResult<&[u8], BcMediaIframe> {
let (buf, video_type_str) = take_str!(buf, 4)?;
named!(take4str( &[u8] ) -> &str, take_str!( 4 ) );
let (buf, video_type_str) = verify(take4str, |x| matches!(x, "H264" | "H265"))(buf)?;
let (buf, payload_size) = le_u32(buf)?;
let (buf, _unknown_a) = le_u32(buf)?;
let (buf, microseconds) = le_u32(buf)?;
@@ -213,11 +214,16 @@ fn bcmedia_iframe(buf: &[u8]) -> IResult<&[u8], BcMediaIframe> {
};
let (buf, _padding) = take!(buf, pad_size)?;
let video_type = match video_type_str {
"H264" => VideoType::H264,
"H265" => VideoType::H265,
_ => unreachable!(),
};
Ok((
buf,
BcMediaIframe {
video_type: video_type_str.to_string(),
payload_size,
video_type,
// payload_size,
microseconds,
time,
data: data_slice.to_vec(),
@@ -226,7 +232,8 @@ fn bcmedia_iframe(buf: &[u8]) -> IResult<&[u8], BcMediaIframe> {
}
fn bcmedia_pframe(buf: &[u8]) -> IResult<&[u8], BcMediaPframe> {
let (buf, video_type_str) = take_str!(buf, 4)?;
named!(take4str( &[u8] ) -> &str, take_str!( 4 ) );
let (buf, video_type_str) = verify(take4str, |x| matches!(x, "H264" | "H265"))(buf)?;
let (buf, payload_size) = le_u32(buf)?;
let (buf, _unknown_a) = le_u32(buf)?;
let (buf, microseconds) = le_u32(buf)?;
@@ -238,11 +245,17 @@ fn bcmedia_pframe(buf: &[u8]) -> IResult<&[u8], BcMediaPframe> {
};
let (buf, _padding) = take!(buf, pad_size)?;
let video_type = match video_type_str {
"H264" => VideoType::H264,
"H265" => VideoType::H265,
_ => unreachable!(),
};
Ok((
buf,
BcMediaPframe {
video_type: video_type_str.to_string(),
payload_size,
video_type,
// payload_size,
microseconds,
data: data_slice.to_vec(),
},
@@ -262,7 +275,7 @@ fn bcmedia_aac(buf: &[u8]) -> IResult<&[u8], BcMediaAac> {
Ok((
buf,
BcMediaAac {
payload_size,
// payload_size,
data: data_slice.to_vec(),
},
))
@@ -283,8 +296,8 @@ fn bcmedia_adpcm(buf: &[u8]) -> IResult<&[u8], BcMediaAdpcm> {
Ok((
buf,
BcMediaAdpcm {
payload_size,
block_size,
// payload_size,
// block_size,
data: data_slice.to_vec(),
},
))
+39 -26
View File
@@ -19,10 +19,10 @@ pub(super) const MAGIC_HEADER_BCMEDIA_INFO_V1: u32 = 0x31303031;
/// The start of a BcMedia stream contains this message
/// which describes the data to follow
pub struct BcMediaInfoV1 {
/// This is the size of the header so it's actually a fixed value
/// The other messages have body size here so maybe that's why
/// it's included
pub header_size: u32,
// This is the size of the header so it's actually a fixed value
// The other messages have body size here so maybe that's why
// it's included
// pub header_size: u32,
/// Width of the video
pub video_width: u32,
/// Height of the video
@@ -62,10 +62,10 @@ pub(super) const MAGIC_HEADER_BCMEDIA_INFO_V2: u32 = 0x32303031;
/// The start of a BcMedia stream contains this message
/// which describes the data to follow
pub struct BcMediaInfoV2 {
/// This is the size of the header so it's actually a fixed value
/// The other messages have body size here so maybe that's why
/// it's included
pub header_size: u32,
// This is the size of the header so it's actually a fixed value
// The other messages have body size here so maybe that's why
// it's included
// pub header_size: u32,
/// Width of the video
pub video_width: u32,
/// Height of the video
@@ -102,12 +102,20 @@ pub struct BcMediaInfoV2 {
pub(super) const MAGIC_HEADER_BCMEDIA_IFRAME: u32 = 0x63643030;
/// Video Types for I/PFrame
pub enum VideoType {
/// H264 video data
H264,
/// H265 video data
H265,
}
/// This is a BcMedia video IFrame.
pub struct BcMediaIframe {
/// ASCII of either "H264", or "H265"
pub video_type: String,
/// Size of payload after header in bytes
pub payload_size: u32,
/// "H264", or "H265"
pub video_type: VideoType,
// Size of payload after header in bytes
// pub payload_size: u32,
// unknown: u32, // NVR channel count? Known values 1-00/08 2-00 3-00 4-00
/// Timestamp in microseconds
pub microseconds: u32,
@@ -123,10 +131,10 @@ pub(super) const MAGIC_HEADER_BCMEDIA_PFRAME: u32 = 0x63643130;
/// This is a BcMedia video PFrame.
pub struct BcMediaPframe {
/// ASCII of either "H264", or "H265"
pub video_type: String,
/// Size of payload after header in bytes
pub payload_size: u32,
/// "H264", or "H265"
pub video_type: VideoType,
// Size of payload after header in bytes
// pub payload_size: u32,
// unknown: u32, // NVR channel count? Known values 1-00/08 2-00 3-00 4-00
/// Timestamp in microseconds
pub microseconds: u32,
@@ -139,8 +147,8 @@ pub(super) const MAGIC_HEADER_BCMEDIA_AAC: u32 = 0x62773530;
/// This contains BcMedia audio data in AAC format
pub struct BcMediaAac {
/// Size of payload after header in bytes
pub payload_size: u16,
// Size of payload after header in bytes
// pub payload_size: u16,
// Size of payload after header in bytes exactly the same as before
// pub payload_size_b: u16,
/// Raw AAC data
@@ -153,17 +161,22 @@ pub(super) const MAGIC_HEADER_BCMEDIA_ADPCM_DATA: u16 = 0x0100;
/// This contains BcMedia audio data in ADPCM format
pub struct BcMediaAdpcm {
/// Size of payload after header in bytes
pub payload_size: u16,
// Size of payload after header in bytes
// pub payload_size: u16,
// 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 block size in bytes (there are twice this in samples)
//
// 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,
/// The raw adpcm data in DVI-4 layout.
///
/// 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,
/// The raw adpcm data in DVI-4 layout. Size should be [block_size] + 4
/// One `data` should contain 4 bytes of the adpcm predictor state then one block
/// of adpcm samples
///
/// To calculate the block-align size simply remove 4 from the `len()`
pub data: Vec<u8>,
}
+167 -2
View File
@@ -2,11 +2,14 @@ use super::model::*;
use cookie_factory::bytes::*;
use cookie_factory::sequence::tuple;
use cookie_factory::{combinator::*, gen};
use cookie_factory::{GenError, SerializeFn, WriteContext};
use cookie_factory::{GenError, SerializeFn};
use err_derive::Error;
use log::error;
use std::io::Write;
// PAD_SIZE: Media packets use 8 byte padding
const PAD_SIZE: u32 = 8;
/// The error types used during serialisation
#[derive(Debug, Error)]
pub enum Error {
@@ -16,9 +19,171 @@ pub enum Error {
}
impl BcMedia {
#[allow(dead_code)]
pub(crate) fn serialize<W: Write>(&self, buf: W) -> Result<W, Error> {
// match &self {}
let (buf, _) = match &self {
BcMedia::InfoV1(payload) => gen(bcmedia_info_v1(payload), buf)?,
BcMedia::InfoV2(payload) => gen(bcmedia_info_v2(payload), buf)?,
BcMedia::Iframe(payload) => {
let pad_size = match payload.data.len() as u32 % PAD_SIZE {
0 => 0,
n => PAD_SIZE - n,
};
gen(
tuple((
bcmedia_iframe(payload),
slice(&payload.data),
slice(&vec![0; pad_size as usize]),
)),
buf,
)?
}
BcMedia::Pframe(payload) => {
let pad_size = match payload.data.len() as u32 % PAD_SIZE {
0 => 0,
n => PAD_SIZE - n,
};
gen(
tuple((
bcmedia_pframe(payload),
slice(&payload.data),
slice(&vec![0; pad_size as usize]),
)),
buf,
)?
}
BcMedia::Aac(payload) => {
let pad_size = match payload.data.len() as u32 % PAD_SIZE {
0 => 0,
n => PAD_SIZE - n,
};
gen(
tuple((
bcmedia_aac(payload),
slice(&payload.data),
slice(&vec![0; pad_size as usize]),
)),
buf,
)?
}
BcMedia::Adpcm(payload) => {
let pad_size = match payload.data.len() as u32 % PAD_SIZE {
0 => 0,
n => PAD_SIZE - n,
};
gen(
tuple((
bcmedia_adpcm(payload),
slice(&payload.data),
slice(&vec![0; pad_size as usize]),
)),
buf,
)?
}
};
Ok(buf)
}
}
fn bcmedia_info_v1<W: Write>(payload: &BcMediaInfoV1) -> impl SerializeFn<W> {
tuple((
le_u32(MAGIC_HEADER_BCMEDIA_INFO_V1),
le_u32(32),
le_u32(payload.video_width),
le_u32(payload.video_height),
le_u8(0), // unknown. Known values 00/01
le_u8(payload.fps),
le_u8(payload.start_year),
le_u8(payload.start_month),
le_u8(payload.start_day),
le_u8(payload.start_hour),
le_u8(payload.start_min),
le_u8(payload.start_seconds),
le_u8(payload.end_year),
le_u8(payload.end_month),
le_u8(payload.end_day),
le_u8(payload.end_hour),
le_u8(payload.end_min),
le_u8(payload.end_seconds),
le_u8(0),
le_u8(0),
))
}
fn bcmedia_info_v2<W: Write>(payload: &BcMediaInfoV2) -> impl SerializeFn<W> {
tuple((
le_u32(MAGIC_HEADER_BCMEDIA_INFO_V2),
le_u32(32),
le_u32(payload.video_width),
le_u32(payload.video_height),
le_u8(0), // unknown. Known values 00/01
le_u8(payload.fps),
le_u8(payload.start_year),
le_u8(payload.start_month),
le_u8(payload.start_day),
le_u8(payload.start_hour),
le_u8(payload.start_min),
le_u8(payload.start_seconds),
le_u8(payload.end_year),
le_u8(payload.end_month),
le_u8(payload.end_day),
le_u8(payload.end_hour),
le_u8(payload.end_min),
le_u8(payload.end_seconds),
le_u8(0),
le_u8(0),
))
}
fn bcmedia_iframe<W: Write>(payload: &BcMediaIframe) -> impl SerializeFn<W> {
// Cookie String needs a static lifetime
let vid_string = match payload.video_type {
VideoType::H264 => "H264",
VideoType::H265 => "H265",
};
tuple((
le_u32(MAGIC_HEADER_BCMEDIA_IFRAME),
string(vid_string),
le_u32(payload.data.len() as u32),
le_u32(0), // unknown. NVR channel count? Known values 1-00/08 2-00 3-00 4-00
le_u32(payload.microseconds),
le_u32(0), // unknown. Known values 1-00/23/5A 2-00 3-00 4-00
le_u32(payload.time),
le_u32(0), // unknown. Known values 1-00/06/29 2-00/01 3-00/C3 4-00
))
}
fn bcmedia_pframe<W: Write>(payload: &BcMediaPframe) -> impl SerializeFn<W> {
// Cookie String needs a static lifetime
let vid_string = match payload.video_type {
VideoType::H264 => "H264",
VideoType::H265 => "H265",
};
tuple((
le_u32(MAGIC_HEADER_BCMEDIA_PFRAME),
string(vid_string),
le_u32(payload.data.len() as u32),
le_u32(0), // unknown. NVR channel count? Known values 1-00/08 2-00 3-00 4-00
le_u32(payload.microseconds),
le_u32(0), // unknown. Known values 1-00/23/5A 2-00 3-00 4-00
))
}
fn bcmedia_aac<W: Write>(payload: &BcMediaAac) -> impl SerializeFn<W> {
tuple((
le_u32(MAGIC_HEADER_BCMEDIA_AAC),
le_u16(payload.data.len() as u16),
le_u16(payload.data.len() as u16),
))
}
fn bcmedia_adpcm<W: Write>(payload: &BcMediaAdpcm) -> impl SerializeFn<W> {
tuple((
le_u32(MAGIC_HEADER_BCMEDIA_ADPCM),
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
le_u16((payload.data.len()) as u16), // block size + predictor state size (4bytes)
))
}
+7 -9
View File
@@ -59,19 +59,17 @@ impl StreamOutput for GstOutputs {
fn write(&mut self, media: BcMedia) -> StreamOutputError {
match media {
BcMedia::Iframe(payload) => {
let video_type = match payload.video_type.as_str() {
"H264" | "h264" => StreamFormat::H264,
"H265" | "h265" => StreamFormat::H265,
_ => unreachable!(),
let video_type = match payload.video_type {
VideoType::H264 => StreamFormat::H264,
VideoType::H265 => StreamFormat::H265,
};
self.set_format(Some(video_type));
self.vidsrc.write_all(&payload.data)?;
}
BcMedia::Pframe(payload) => {
let video_type = match payload.video_type.as_str() {
"H264" | "h264" => StreamFormat::H264,
"H265" | "h265" => StreamFormat::H265,
_ => unreachable!(),
let video_type = match payload.video_type {
VideoType::H264 => StreamFormat::H264,
VideoType::H265 => StreamFormat::H265,
};
self.set_format(Some(video_type));
self.vidsrc.write_all(&payload.data)?;
@@ -81,7 +79,7 @@ impl StreamOutput for GstOutputs {
self.audsrc.write_all(&payload.data)?;
}
BcMedia::Adpcm(payload) => {
self.set_format(Some(StreamFormat::Adpcm(payload.block_size)));
self.set_format(Some(StreamFormat::Adpcm((payload.data.len() - 4) as u16)));
let pcm = adpcm_to_pcm(&payload.data).map_err(|e| {
if let Error::AdpcmDecoding(msg) = e {
neolink_core::Error::OtherString(format!("ADPCM decoding error: {}", msg))