use core::num::NonZeroU32;
use ::buffa::{
DecodeError, DefaultInstance, Message, SizeCache,
bytes::{Buf, BufMut},
encoding::{Tag, WireType, encode_varint, skip_field_depth, varint_len},
types::{
FIXED32_ENCODED_LEN, bytes_encoded_len, decode_bytes, decode_double, decode_float,
decode_string, decode_uint32, encode_bytes, encode_double, encode_float, encode_string,
encode_uint32, string_encoded_len, uint32_encoded_len,
},
};
use smol_str::SmolStr;
use crate::{
audio::{
BitRateMode, ChannelLayout, ContainerFormat, CoverArt, Fingerprint, Loudness, SampleFormat,
Tags,
},
capture::{Device, GeoLocation},
color::{
ChromaCoord, ChromaLocation, ContentLightLevel, DcpTargetGamut, DolbyVisionConfig,
DynamicRange, HdrStaticMetadata, Info, MasteringDisplay, Matrix, Primaries, Transfer,
},
container::Format,
disposition::TrackDisposition,
frame::{
Dimensions, FieldOrder, FrameRate, Rational, Rect, Rotation, SampleAspectRatio, StereoMode,
},
lang::Language,
pixel_format::PixelFormat,
};
const VARINT: u8 = WireType::Varint as u8;
const LEN: u8 = WireType::LengthDelimited as u8;
macro_rules! impl_enum_message {
($ty:ty, $to:expr, $from:expr) => {
impl DefaultInstance for $ty {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<$ty> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(<$ty>::default()))
}
}
impl Message for $ty {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
if *self != <$ty>::default() {
let v: u32 = $to(self);
1 + uint32_encoded_len(v) as u32
} else {
0
}
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
if *self != <$ty>::default() {
let v: u32 = $to(self);
Tag::new(1, WireType::Varint).encode(buf);
encode_uint32(v, buf);
}
}
fn merge_field(
&mut self,
tag: Tag,
buf: &mut impl Buf,
depth: u32,
) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let v = decode_uint32(buf)?;
*self = $from(v);
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = <$ty>::default();
}
}
};
}
impl_enum_message!(Matrix, |s: &Matrix| s.to_u32(), Matrix::from_u32);
impl_enum_message!(Primaries, |s: &Primaries| s.to_u32(), Primaries::from_u32);
impl_enum_message!(Transfer, |s: &Transfer| s.to_u32(), Transfer::from_u32);
impl_enum_message!(
DynamicRange,
|s: &DynamicRange| s.to_u32(),
DynamicRange::from_u32
);
impl_enum_message!(
ChromaLocation,
|s: &ChromaLocation| s.to_u32(),
ChromaLocation::from_u32
);
impl_enum_message!(
DcpTargetGamut,
|s: &DcpTargetGamut| s.to_u32(),
DcpTargetGamut::from_u32
);
impl_enum_message!(Rotation, |s: &Rotation| s.to_u32(), Rotation::from_u32);
impl_enum_message!(
FieldOrder,
|s: &FieldOrder| s.to_u32(),
FieldOrder::from_u32
);
impl_enum_message!(
StereoMode,
|s: &StereoMode| s.to_u32(),
StereoMode::from_u32
);
impl_enum_message!(
PixelFormat,
|s: &PixelFormat| s.to_u32(),
PixelFormat::from_u32
);
impl DefaultInstance for Dimensions {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<Dimensions> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(Dimensions::default()))
}
}
impl Message for Dimensions {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
let mut size = 0u32;
if self.width() != 0 {
size += 1 + uint32_encoded_len(self.width()) as u32;
}
if self.height() != 0 {
size += 1 + uint32_encoded_len(self.height()) as u32;
}
size
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
if self.width() != 0 {
Tag::new(1, WireType::Varint).encode(buf);
encode_uint32(self.width(), buf);
}
if self.height() != 0 {
Tag::new(2, WireType::Varint).encode(buf);
encode_uint32(self.height(), buf);
}
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let w = decode_uint32(buf)?;
self.set_width(w);
}
2 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 2,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let h = decode_uint32(buf)?;
self.set_height(h);
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = Dimensions::default();
}
}
impl DefaultInstance for Rect {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<Rect> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(Rect::default()))
}
}
impl Message for Rect {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
let mut size = 0u32;
if self.x() != 0 {
size += 1 + uint32_encoded_len(self.x()) as u32;
}
if self.y() != 0 {
size += 1 + uint32_encoded_len(self.y()) as u32;
}
if self.width() != 0 {
size += 1 + uint32_encoded_len(self.width()) as u32;
}
if self.height() != 0 {
size += 1 + uint32_encoded_len(self.height()) as u32;
}
size
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
if self.x() != 0 {
Tag::new(1, WireType::Varint).encode(buf);
encode_uint32(self.x(), buf);
}
if self.y() != 0 {
Tag::new(2, WireType::Varint).encode(buf);
encode_uint32(self.y(), buf);
}
if self.width() != 0 {
Tag::new(3, WireType::Varint).encode(buf);
encode_uint32(self.width(), buf);
}
if self.height() != 0 {
Tag::new(4, WireType::Varint).encode(buf);
encode_uint32(self.height(), buf);
}
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let v = decode_uint32(buf)?;
self.set_x(v);
}
2 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 2,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let v = decode_uint32(buf)?;
self.set_y(v);
}
3 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 3,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let v = decode_uint32(buf)?;
self.set_width(v);
}
4 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 4,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let v = decode_uint32(buf)?;
self.set_height(v);
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = Rect::default();
}
}
impl DefaultInstance for SampleAspectRatio {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<SampleAspectRatio> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(SampleAspectRatio::default()))
}
}
impl Message for SampleAspectRatio {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
2 + uint32_encoded_len(self.num()) as u32 + uint32_encoded_len(self.den().get()) as u32
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
Tag::new(1, WireType::Varint).encode(buf);
encode_uint32(self.num(), buf);
Tag::new(2, WireType::Varint).encode(buf);
encode_uint32(self.den().get(), buf);
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let num = decode_uint32(buf)?;
self.set_num(num);
}
2 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 2,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let den = NonZeroU32::new(decode_uint32(buf)?).unwrap_or(NonZeroU32::MIN);
self.set_den(den);
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = SampleAspectRatio::default();
}
}
impl DefaultInstance for Rational {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<Rational> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(Rational::default()))
}
}
impl Message for Rational {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
2 + uint32_encoded_len(self.num()) as u32 + uint32_encoded_len(self.den().get()) as u32
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
Tag::new(1, WireType::Varint).encode(buf);
encode_uint32(self.num(), buf);
Tag::new(2, WireType::Varint).encode(buf);
encode_uint32(self.den().get(), buf);
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let num = decode_uint32(buf)?;
self.set_num(num);
}
2 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 2,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let den = NonZeroU32::new(decode_uint32(buf)?).unwrap_or(NonZeroU32::MIN);
self.set_den(den);
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = Rational::default();
}
}
impl DefaultInstance for FrameRate {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<FrameRate> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(FrameRate::default()))
}
}
impl Message for FrameRate {
fn compute_size(&self, cache: &mut SizeCache) -> u32 {
let mut size = 0u32;
{
let slot = cache.reserve();
let inner = self.rate().compute_size(cache);
cache.set(slot, inner);
size += 1 + varint_len(inner as u64) as u32 + inner;
}
if self.is_vfr() {
size += 1 + 1; }
size
}
fn write_to(&self, cache: &mut SizeCache, buf: &mut impl BufMut) {
Tag::new(1, WireType::LengthDelimited).encode(buf);
encode_varint(cache.consume_next() as u64, buf);
self.rate().write_to(cache, buf);
if self.is_vfr() {
Tag::new(2, WireType::Varint).encode(buf);
encode_varint(1, buf);
}
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::LengthDelimited {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: LEN,
actual: tag.wire_type() as u8,
});
}
let mut rate = self.rate();
buffa::Message::merge_length_delimited(&mut rate, buf, depth)?;
self.set_rate(rate);
}
2 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 2,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
self.update_is_vfr(decode_uint32(buf)? != 0);
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = FrameRate::default();
}
}
impl DefaultInstance for DolbyVisionConfig {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<DolbyVisionConfig> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(DolbyVisionConfig::default()))
}
}
impl Message for DolbyVisionConfig {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
let mut size = 0u32;
if self.profile() != 0 {
size += 1 + uint32_encoded_len(self.profile() as u32) as u32;
}
if self.level() != 0 {
size += 1 + uint32_encoded_len(self.level() as u32) as u32;
}
if self.rpu_present() {
size += 1 + 1;
}
if self.el_present() {
size += 1 + 1;
}
if self.bl_signal_compat_id() != 0 {
size += 1 + uint32_encoded_len(self.bl_signal_compat_id() as u32) as u32;
}
size
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
if self.profile() != 0 {
Tag::new(1, WireType::Varint).encode(buf);
encode_uint32(self.profile() as u32, buf);
}
if self.level() != 0 {
Tag::new(2, WireType::Varint).encode(buf);
encode_uint32(self.level() as u32, buf);
}
if self.rpu_present() {
Tag::new(3, WireType::Varint).encode(buf);
encode_varint(1, buf);
}
if self.el_present() {
Tag::new(4, WireType::Varint).encode(buf);
encode_varint(1, buf);
}
if self.bl_signal_compat_id() != 0 {
Tag::new(5, WireType::Varint).encode(buf);
encode_uint32(self.bl_signal_compat_id() as u32, buf);
}
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
self.set_profile(decode_uint32(buf)? as u8);
}
2 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 2,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
self.set_level(decode_uint32(buf)? as u8);
}
3 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 3,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
self.update_rpu_present(decode_uint32(buf)? != 0);
}
4 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 4,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
self.update_el_present(decode_uint32(buf)? != 0);
}
5 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 5,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
self.set_bl_signal_compat_id(decode_uint32(buf)? as u8);
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = DolbyVisionConfig::default();
}
}
impl DefaultInstance for Info {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<Info> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(Info::UNSPECIFIED))
}
}
impl Message for Info {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
5 + uint32_encoded_len(self.primaries().to_u32()) as u32
+ uint32_encoded_len(self.transfer().to_u32()) as u32
+ uint32_encoded_len(self.matrix().to_u32()) as u32
+ uint32_encoded_len(self.range().to_u32()) as u32
+ uint32_encoded_len(self.chroma_location().to_u32()) as u32
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
Tag::new(1, WireType::Varint).encode(buf);
encode_uint32(self.primaries().to_u32(), buf);
Tag::new(2, WireType::Varint).encode(buf);
encode_uint32(self.transfer().to_u32(), buf);
Tag::new(3, WireType::Varint).encode(buf);
encode_uint32(self.matrix().to_u32(), buf);
Tag::new(4, WireType::Varint).encode(buf);
encode_uint32(self.range().to_u32(), buf);
Tag::new(5, WireType::Varint).encode(buf);
encode_uint32(self.chroma_location().to_u32(), buf);
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let v = decode_uint32(buf)?;
self.set_primaries(Primaries::from_u32(v));
}
2 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 2,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let v = decode_uint32(buf)?;
self.set_transfer(Transfer::from_u32(v));
}
3 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 3,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let v = decode_uint32(buf)?;
self.set_matrix(Matrix::from_u32(v));
}
4 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 4,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let v = decode_uint32(buf)?;
self.set_range(DynamicRange::from_u32(v));
}
5 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 5,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let v = decode_uint32(buf)?;
self.set_chroma_location(ChromaLocation::from_u32(v));
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = Info::UNSPECIFIED;
}
}
impl DefaultInstance for ContentLightLevel {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<ContentLightLevel> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(ContentLightLevel::default()))
}
}
impl Message for ContentLightLevel {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
let mut size = 0u32;
if self.max_cll() != 0 {
size += 1 + uint32_encoded_len(self.max_cll()) as u32;
}
if self.max_fall() != 0 {
size += 1 + uint32_encoded_len(self.max_fall()) as u32;
}
size
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
if self.max_cll() != 0 {
Tag::new(1, WireType::Varint).encode(buf);
encode_uint32(self.max_cll(), buf);
}
if self.max_fall() != 0 {
Tag::new(2, WireType::Varint).encode(buf);
encode_uint32(self.max_fall(), buf);
}
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let v = decode_uint32(buf)?;
self.set_max_cll(v);
}
2 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 2,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let v = decode_uint32(buf)?;
self.set_max_fall(v);
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = ContentLightLevel::default();
}
}
impl DefaultInstance for ChromaCoord {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<ChromaCoord> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(ChromaCoord::default()))
}
}
impl Message for ChromaCoord {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
let mut size = 0u32;
if self.x() != 0 {
size += 1 + uint32_encoded_len(self.x()) as u32;
}
if self.y() != 0 {
size += 1 + uint32_encoded_len(self.y()) as u32;
}
size
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
if self.x() != 0 {
Tag::new(1, WireType::Varint).encode(buf);
encode_uint32(self.x(), buf);
}
if self.y() != 0 {
Tag::new(2, WireType::Varint).encode(buf);
encode_uint32(self.y(), buf);
}
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
self.set_x(decode_uint32(buf)?);
}
2 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 2,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
self.set_y(decode_uint32(buf)?);
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = ChromaCoord::default();
}
}
impl DefaultInstance for MasteringDisplay {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<MasteringDisplay> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(MasteringDisplay::default()))
}
}
impl Message for MasteringDisplay {
fn compute_size(&self, cache: &mut SizeCache) -> u32 {
let mut size = 0u32;
let primaries = self.display_primaries();
for cc in &primaries {
let slot = cache.reserve();
let inner = cc.compute_size(cache);
cache.set(slot, inner);
size += 1 + varint_len(inner as u64) as u32 + inner;
}
{
let slot = cache.reserve();
let inner = self.white_point().compute_size(cache);
cache.set(slot, inner);
size += 1 + varint_len(inner as u64) as u32 + inner;
}
if self.max_luminance() != 0 {
size += 1 + uint32_encoded_len(self.max_luminance()) as u32;
}
if self.min_luminance() != 0 {
size += 1 + uint32_encoded_len(self.min_luminance()) as u32;
}
size
}
fn write_to(&self, cache: &mut SizeCache, buf: &mut impl BufMut) {
let primaries = self.display_primaries();
for (i, cc) in primaries.iter().enumerate() {
Tag::new(1 + i as u32, WireType::LengthDelimited).encode(buf);
encode_varint(cache.consume_next() as u64, buf);
cc.write_to(cache, buf);
}
Tag::new(4, WireType::LengthDelimited).encode(buf);
encode_varint(cache.consume_next() as u64, buf);
self.white_point().write_to(cache, buf);
if self.max_luminance() != 0 {
Tag::new(5, WireType::Varint).encode(buf);
encode_uint32(self.max_luminance(), buf);
}
if self.min_luminance() != 0 {
Tag::new(6, WireType::Varint).encode(buf);
encode_uint32(self.min_luminance(), buf);
}
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
n @ 1..=3 => {
if tag.wire_type() != WireType::LengthDelimited {
return Err(DecodeError::WireTypeMismatch {
field_number: n,
expected: LEN,
actual: tag.wire_type() as u8,
});
}
let mut primaries = self.display_primaries();
let mut cc = primaries[(n - 1) as usize];
buffa::Message::merge_length_delimited(&mut cc, buf, depth)?;
primaries[(n - 1) as usize] = cc;
self.set_display_primaries(primaries);
}
4 => {
if tag.wire_type() != WireType::LengthDelimited {
return Err(DecodeError::WireTypeMismatch {
field_number: 4,
expected: LEN,
actual: tag.wire_type() as u8,
});
}
let mut wp = self.white_point();
buffa::Message::merge_length_delimited(&mut wp, buf, depth)?;
self.set_white_point(wp);
}
5 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 5,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let v = decode_uint32(buf)?;
self.set_max_luminance(v);
}
6 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 6,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let v = decode_uint32(buf)?;
self.set_min_luminance(v);
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = MasteringDisplay::default();
}
}
impl DefaultInstance for HdrStaticMetadata {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<HdrStaticMetadata> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(HdrStaticMetadata::default()))
}
}
impl Message for HdrStaticMetadata {
fn compute_size(&self, cache: &mut SizeCache) -> u32 {
let mut size = 0u32;
if let Some(md) = self.mastering() {
let slot = cache.reserve();
let inner = md.compute_size(cache);
cache.set(slot, inner);
size += 1 + varint_len(inner as u64) as u32 + inner;
}
if let Some(cll) = self.content_light() {
let slot = cache.reserve();
let inner = cll.compute_size(cache);
cache.set(slot, inner);
size += 1 + varint_len(inner as u64) as u32 + inner;
}
size
}
fn write_to(&self, cache: &mut SizeCache, buf: &mut impl BufMut) {
if let Some(md) = self.mastering() {
Tag::new(1, WireType::LengthDelimited).encode(buf);
encode_varint(cache.consume_next() as u64, buf);
md.write_to(cache, buf);
}
if let Some(cll) = self.content_light() {
Tag::new(2, WireType::LengthDelimited).encode(buf);
encode_varint(cache.consume_next() as u64, buf);
cll.write_to(cache, buf);
}
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::LengthDelimited {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: LEN,
actual: tag.wire_type() as u8,
});
}
let mut md = self.mastering().unwrap_or_default();
buffa::Message::merge_length_delimited(&mut md, buf, depth)?;
self.set_mastering(Some(md));
}
2 => {
if tag.wire_type() != WireType::LengthDelimited {
return Err(DecodeError::WireTypeMismatch {
field_number: 2,
expected: LEN,
actual: tag.wire_type() as u8,
});
}
let mut cll = self.content_light().unwrap_or_default();
buffa::Message::merge_length_delimited(&mut cll, buf, depth)?;
self.set_content_light(Some(cll));
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = HdrStaticMetadata::default();
}
}
macro_rules! impl_string_enum_message {
($ty:ty, $default_expr:expr) => {
impl DefaultInstance for $ty {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<$ty> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new($default_expr))
}
}
impl Message for $ty {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
let s = self.as_str();
if !s.is_empty() {
1 + string_encoded_len(s) as u32
} else {
0
}
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
let s = self.as_str();
if !s.is_empty() {
Tag::new(1, WireType::LengthDelimited).encode(buf);
encode_string(s, buf);
}
}
fn merge_field(
&mut self,
tag: Tag,
buf: &mut impl Buf,
depth: u32,
) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::LengthDelimited {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: LEN,
actual: tag.wire_type() as u8,
});
}
let s = decode_string(buf)?;
*self = <$ty as core::str::FromStr>::from_str(&s).unwrap_or_else(|_| unreachable!());
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = $default_expr;
}
}
};
}
impl_string_enum_message!(ChannelLayout, ChannelLayout::Other(SmolStr::new_inline("")));
impl_string_enum_message!(
ContainerFormat,
ContainerFormat::Other(SmolStr::new_inline(""))
);
impl_string_enum_message!(Format, Format::Other(SmolStr::new_inline("")));
impl DefaultInstance for BitRateMode {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<BitRateMode> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(BitRateMode::default()))
}
}
impl Message for BitRateMode {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
let v = self.to_u32();
if v != 0 {
1 + uint32_encoded_len(v) as u32
} else {
0
}
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
let v = self.to_u32();
if v != 0 {
Tag::new(1, WireType::Varint).encode(buf);
encode_uint32(v, buf);
}
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
*self = BitRateMode::from_u32(decode_uint32(buf)?);
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = BitRateMode::default();
}
}
impl DefaultInstance for SampleFormat {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<SampleFormat> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(SampleFormat::default()))
}
}
impl Message for SampleFormat {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
if *self != SampleFormat::default() {
let v = self.to_u32();
1 + uint32_encoded_len(v) as u32
} else {
0
}
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
if *self != SampleFormat::default() {
let v = self.to_u32();
Tag::new(1, WireType::Varint).encode(buf);
encode_uint32(v, buf);
}
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
*self = SampleFormat::from_u32(decode_uint32(buf)?);
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = SampleFormat::default();
}
}
const FIXED32: u8 = WireType::Fixed32 as u8;
impl DefaultInstance for Loudness {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<Loudness> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(Loudness::default()))
}
}
impl Message for Loudness {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
let mut size = 0u32;
if self.integrated_lufs() != 0.0 {
size += 1 + FIXED32_ENCODED_LEN as u32;
}
if self.range_lu() != 0.0 {
size += 1 + FIXED32_ENCODED_LEN as u32;
}
if self.true_peak_dbtp() != 0.0 {
size += 1 + FIXED32_ENCODED_LEN as u32;
}
if self.sample_peak_dbfs() != 0.0 {
size += 1 + FIXED32_ENCODED_LEN as u32;
}
size
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
if self.integrated_lufs() != 0.0 {
Tag::new(1, WireType::Fixed32).encode(buf);
encode_float(self.integrated_lufs(), buf);
}
if self.range_lu() != 0.0 {
Tag::new(2, WireType::Fixed32).encode(buf);
encode_float(self.range_lu(), buf);
}
if self.true_peak_dbtp() != 0.0 {
Tag::new(3, WireType::Fixed32).encode(buf);
encode_float(self.true_peak_dbtp(), buf);
}
if self.sample_peak_dbfs() != 0.0 {
Tag::new(4, WireType::Fixed32).encode(buf);
encode_float(self.sample_peak_dbfs(), buf);
}
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
n @ 1..=4 => {
if tag.wire_type() != WireType::Fixed32 {
return Err(DecodeError::WireTypeMismatch {
field_number: n,
expected: FIXED32,
actual: tag.wire_type() as u8,
});
}
let v = decode_float(buf)?;
match n {
1 => {
self.set_integrated_lufs(v);
}
2 => {
self.set_range_lu(v);
}
3 => {
self.set_true_peak_dbtp(v);
}
4 => {
self.set_sample_peak_dbfs(v);
}
_ => unreachable!(),
}
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = Loudness::default();
}
}
fn audio_fingerprint_seed() -> Fingerprint {
Fingerprint::try_new(SmolStr::new_inline("default"), std::vec::Vec::new())
.unwrap_or_else(|_| unreachable!())
}
impl DefaultInstance for Fingerprint {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<Fingerprint> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(audio_fingerprint_seed()))
}
}
impl Message for Fingerprint {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
let mut size = 1 + string_encoded_len(self.algorithm()) as u32;
if !self.value().is_empty() {
size += 1 + bytes_encoded_len(self.value()) as u32;
}
size
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
Tag::new(1, WireType::LengthDelimited).encode(buf);
encode_string(self.algorithm(), buf);
if !self.value().is_empty() {
Tag::new(2, WireType::LengthDelimited).encode(buf);
encode_bytes(self.value(), buf);
}
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::LengthDelimited {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: LEN,
actual: tag.wire_type() as u8,
});
}
let algo = decode_string(buf)?;
let algo = if algo.is_empty() {
SmolStr::new_inline("default")
} else {
SmolStr::new(&algo)
};
let value = self.value().to_vec();
*self = Fingerprint::try_new(algo, value).unwrap_or_else(|_| audio_fingerprint_seed());
}
2 => {
if tag.wire_type() != WireType::LengthDelimited {
return Err(DecodeError::WireTypeMismatch {
field_number: 2,
expected: LEN,
actual: tag.wire_type() as u8,
});
}
let bytes = decode_bytes(buf)?;
let algo = SmolStr::from(self.algorithm());
*self = Fingerprint::try_new(algo, bytes).unwrap_or_else(|_| audio_fingerprint_seed());
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = audio_fingerprint_seed();
}
}
fn audio_cover_art_seed() -> CoverArt {
CoverArt::try_new(
SmolStr::new_static("application/octet-stream"),
std::vec![0u8],
)
.unwrap_or_else(|_| unreachable!())
}
impl DefaultInstance for CoverArt {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<CoverArt> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(audio_cover_art_seed()))
}
}
impl Message for CoverArt {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
2 + string_encoded_len(self.mime()) as u32 + bytes_encoded_len(self.data()) as u32
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
Tag::new(1, WireType::LengthDelimited).encode(buf);
encode_string(self.mime(), buf);
Tag::new(2, WireType::LengthDelimited).encode(buf);
encode_bytes(self.data(), buf);
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::LengthDelimited {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: LEN,
actual: tag.wire_type() as u8,
});
}
let mime = decode_string(buf)?;
let mime = if mime.is_empty() {
SmolStr::new_static("application/octet-stream")
} else {
SmolStr::new(&mime)
};
let data = self.data().to_vec();
let data = if data.is_empty() {
std::vec![0u8]
} else {
data
};
*self = CoverArt::try_new(mime, data).unwrap_or_else(|_| audio_cover_art_seed());
}
2 => {
if tag.wire_type() != WireType::LengthDelimited {
return Err(DecodeError::WireTypeMismatch {
field_number: 2,
expected: LEN,
actual: tag.wire_type() as u8,
});
}
let data = decode_bytes(buf)?;
let data = if data.is_empty() {
std::vec![0u8]
} else {
data
};
let mime = SmolStr::from(self.mime());
*self = CoverArt::try_new(mime, data).unwrap_or_else(|_| audio_cover_art_seed());
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = audio_cover_art_seed();
}
}
impl DefaultInstance for Tags {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<Tags> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(Tags::default()))
}
}
impl Message for Tags {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
let mut size = 0u32;
if !self.title().is_empty() {
size += 1 + string_encoded_len(self.title()) as u32;
}
if !self.artist().is_empty() {
size += 1 + string_encoded_len(self.artist()) as u32;
}
if !self.album_artist().is_empty() {
size += 1 + string_encoded_len(self.album_artist()) as u32;
}
if !self.album().is_empty() {
size += 1 + string_encoded_len(self.album()) as u32;
}
if !self.composer().is_empty() {
size += 1 + string_encoded_len(self.composer()) as u32;
}
if !self.genre().is_empty() {
size += 1 + string_encoded_len(self.genre()) as u32;
}
if !self.comment().is_empty() {
size += 1 + string_encoded_len(self.comment()) as u32;
}
if self.year() != 0 {
size += 1 + uint32_encoded_len(self.year() as u32) as u32;
}
if self.track_number() != 0 {
size += 1 + uint32_encoded_len(self.track_number() as u32) as u32;
}
if self.track_total() != 0 {
size += 1 + uint32_encoded_len(self.track_total() as u32) as u32;
}
if self.disc_number() != 0 {
size += 1 + uint32_encoded_len(self.disc_number() as u32) as u32;
}
if self.disc_total() != 0 {
size += 1 + uint32_encoded_len(self.disc_total() as u32) as u32;
}
if let Some(lang) = self.language() {
size += 1 + string_encoded_len(&lang.to_bcp47()) as u32;
}
size
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
if !self.title().is_empty() {
Tag::new(1, WireType::LengthDelimited).encode(buf);
encode_string(self.title(), buf);
}
if !self.artist().is_empty() {
Tag::new(2, WireType::LengthDelimited).encode(buf);
encode_string(self.artist(), buf);
}
if !self.album_artist().is_empty() {
Tag::new(3, WireType::LengthDelimited).encode(buf);
encode_string(self.album_artist(), buf);
}
if !self.album().is_empty() {
Tag::new(4, WireType::LengthDelimited).encode(buf);
encode_string(self.album(), buf);
}
if !self.composer().is_empty() {
Tag::new(5, WireType::LengthDelimited).encode(buf);
encode_string(self.composer(), buf);
}
if !self.genre().is_empty() {
Tag::new(6, WireType::LengthDelimited).encode(buf);
encode_string(self.genre(), buf);
}
if !self.comment().is_empty() {
Tag::new(7, WireType::LengthDelimited).encode(buf);
encode_string(self.comment(), buf);
}
if self.year() != 0 {
Tag::new(8, WireType::Varint).encode(buf);
encode_uint32(self.year() as u32, buf);
}
if self.track_number() != 0 {
Tag::new(9, WireType::Varint).encode(buf);
encode_uint32(self.track_number() as u32, buf);
}
if self.track_total() != 0 {
Tag::new(10, WireType::Varint).encode(buf);
encode_uint32(self.track_total() as u32, buf);
}
if self.disc_number() != 0 {
Tag::new(11, WireType::Varint).encode(buf);
encode_uint32(self.disc_number() as u32, buf);
}
if self.disc_total() != 0 {
Tag::new(12, WireType::Varint).encode(buf);
encode_uint32(self.disc_total() as u32, buf);
}
if let Some(lang) = self.language() {
Tag::new(13, WireType::LengthDelimited).encode(buf);
encode_string(&lang.to_bcp47(), buf);
}
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
let n = tag.field_number();
match n {
1..=7 | 13 => {
if tag.wire_type() != WireType::LengthDelimited {
return Err(DecodeError::WireTypeMismatch {
field_number: n,
expected: LEN,
actual: tag.wire_type() as u8,
});
}
let s = decode_string(buf)?;
let s = SmolStr::new(&s);
match n {
1 => {
self.set_title(s);
}
2 => {
self.set_artist(s);
}
3 => {
self.set_album_artist(s);
}
4 => {
self.set_album(s);
}
5 => {
self.set_composer(s);
}
6 => {
self.set_genre(s);
}
7 => {
self.set_comment(s);
}
13 => {
self.update_language(if s.is_empty() {
None
} else {
Some(Language::from_bcp47(&s).unwrap_or_default())
});
}
_ => unreachable!(),
}
}
8..=12 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: n,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let v = decode_uint32(buf)? as u16;
match n {
8 => {
self.set_year(v);
}
9 => {
self.set_track_number(v);
}
10 => {
self.set_track_total(v);
}
11 => {
self.set_disc_number(v);
}
12 => {
self.set_disc_total(v);
}
_ => unreachable!(),
}
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = Tags::default();
}
}
impl DefaultInstance for TrackDisposition {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<TrackDisposition> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(TrackDisposition::default()))
}
}
impl Message for TrackDisposition {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
if self.to_u32() != 0 {
1 + uint32_encoded_len(self.to_u32()) as u32
} else {
0
}
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
if self.to_u32() != 0 {
Tag::new(1, WireType::Varint).encode(buf);
encode_uint32(self.to_u32(), buf);
}
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let v = decode_uint32(buf)?;
*self = TrackDisposition::from_u32(v);
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = TrackDisposition::default();
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
mod subtitle_impls {
use super::*;
use ::buffa::types::{decode_string, encode_string, string_encoded_len};
use core::str::FromStr;
use crate::subtitle::{Format, TrackOrigin};
impl DefaultInstance for TrackOrigin {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<TrackOrigin> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(TrackOrigin::default()))
}
}
impl Message for TrackOrigin {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
if *self != TrackOrigin::default() {
let v: u32 = self.to_u32();
1 + uint32_encoded_len(v) as u32
} else {
0
}
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
if *self != TrackOrigin::default() {
let v: u32 = self.to_u32();
Tag::new(1, WireType::Varint).encode(buf);
encode_uint32(v, buf);
}
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::Varint {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: VARINT,
actual: tag.wire_type() as u8,
});
}
let v = decode_uint32(buf)?;
*self = TrackOrigin::from_u32(v);
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = TrackOrigin::default();
}
}
impl DefaultInstance for Format {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<Format> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(Format::default()))
}
}
impl Message for Format {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
let slug = self.as_str();
1 + string_encoded_len(slug) as u32
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
let slug = self.as_str();
Tag::new(1, WireType::LengthDelimited).encode(buf);
encode_string(slug, buf);
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::LengthDelimited {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: LEN,
actual: tag.wire_type() as u8,
});
}
let s = decode_string(buf)?;
let Ok(parsed) = Format::from_str(&s);
*self = parsed;
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = Format::default();
}
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
impl DefaultInstance for Device {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<Device> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(Device::default()))
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
impl Message for Device {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
let mut size = 0u32;
if !self.make().is_empty() {
size += 1 + string_encoded_len(self.make()) as u32;
}
if !self.model().is_empty() {
size += 1 + string_encoded_len(self.model()) as u32;
}
size
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
if !self.make().is_empty() {
Tag::new(1, WireType::LengthDelimited).encode(buf);
encode_string(self.make(), buf);
}
if !self.model().is_empty() {
Tag::new(2, WireType::LengthDelimited).encode(buf);
encode_string(self.model(), buf);
}
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::LengthDelimited {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: LEN,
actual: tag.wire_type() as u8,
});
}
let s = decode_string(buf)?;
self.set_make(s.as_str());
}
2 => {
if tag.wire_type() != WireType::LengthDelimited {
return Err(DecodeError::WireTypeMismatch {
field_number: 2,
expected: LEN,
actual: tag.wire_type() as u8,
});
}
let s = decode_string(buf)?;
self.set_model(s.as_str());
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = Device::default();
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
impl DefaultInstance for GeoLocation {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<GeoLocation> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| {
buffa::alloc::boxed::Box::new(GeoLocation::try_new(0.0, 0.0, None).expect("0,0 is valid"))
})
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
impl Message for GeoLocation {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
let mut size = (1 + 8) + (1 + 8);
if self.altitude().is_some() {
size += 1 + 4;
}
size
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
Tag::new(1, WireType::Fixed64).encode(buf);
encode_double(self.lat(), buf);
Tag::new(2, WireType::Fixed64).encode(buf);
encode_double(self.lon(), buf);
if let Some(alt) = self.altitude() {
Tag::new(3, WireType::Fixed32).encode(buf);
encode_float(alt, buf);
}
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::Fixed64 {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: WireType::Fixed64 as u8,
actual: tag.wire_type() as u8,
});
}
let v = decode_double(buf)?;
let prev = *self;
let lat = if v.is_finite() {
v.clamp(-90.0, 90.0)
} else {
0.0
};
*self =
GeoLocation::try_new(lat, prev.lon(), prev.altitude()).expect("clamped lat is in range");
}
2 => {
if tag.wire_type() != WireType::Fixed64 {
return Err(DecodeError::WireTypeMismatch {
field_number: 2,
expected: WireType::Fixed64 as u8,
actual: tag.wire_type() as u8,
});
}
let v = decode_double(buf)?;
let prev = *self;
let lon = if v.is_finite() {
v.clamp(-180.0, 180.0)
} else {
0.0
};
*self =
GeoLocation::try_new(prev.lat(), lon, prev.altitude()).expect("clamped lon is in range");
}
3 => {
if tag.wire_type() != WireType::Fixed32 {
return Err(DecodeError::WireTypeMismatch {
field_number: 3,
expected: WireType::Fixed32 as u8,
actual: tag.wire_type() as u8,
});
}
let v = decode_float(buf)?;
self.set_altitude(v);
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = GeoLocation::try_new(0.0, 0.0, None).expect("0,0 is valid");
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
impl DefaultInstance for Language {
fn default_instance() -> &'static Self {
static VALUE: buffa::__private::OnceBox<Language> = buffa::__private::OnceBox::new();
VALUE.get_or_init(|| buffa::alloc::boxed::Box::new(Language::default()))
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
impl Message for Language {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
let tag = self.to_bcp47();
if tag.is_empty() {
0
} else {
1 + string_encoded_len(&tag) as u32
}
}
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut) {
let tag = self.to_bcp47();
if !tag.is_empty() {
Tag::new(1, WireType::LengthDelimited).encode(buf);
encode_string(&tag, buf);
}
}
fn merge_field(&mut self, tag: Tag, buf: &mut impl Buf, depth: u32) -> Result<(), DecodeError> {
match tag.field_number() {
1 => {
if tag.wire_type() != WireType::LengthDelimited {
return Err(DecodeError::WireTypeMismatch {
field_number: 1,
expected: LEN,
actual: tag.wire_type() as u8,
});
}
let s = decode_string(buf)?;
*self = Language::from_bcp47(&s).unwrap_or_default();
}
_ => skip_field_depth(tag, buf, depth)?,
}
Ok(())
}
fn clear(&mut self) {
*self = Language::default();
}
}
#[cfg(test)]
mod tests {
use super::*;
use ::buffa::alloc::vec::Vec;
fn nz(n: u32) -> NonZeroU32 {
NonZeroU32::new(n).unwrap()
}
fn cc(x: u32, y: u32) -> ChromaCoord {
ChromaCoord::new(x, y)
}
#[test]
fn enum_default_elides_to_zero_bytes() {
assert!(Matrix::default().encode_to_vec().is_empty());
assert!(Primaries::default().encode_to_vec().is_empty());
assert!(Transfer::default().encode_to_vec().is_empty());
assert!(DynamicRange::default().encode_to_vec().is_empty());
assert!(ChromaLocation::default().encode_to_vec().is_empty());
assert!(DcpTargetGamut::default().encode_to_vec().is_empty());
assert_eq!(Matrix::decode_from_slice(&[]).unwrap(), Matrix::default());
assert_eq!(
Primaries::decode_from_slice(&[]).unwrap(),
Primaries::default()
);
assert_eq!(
Transfer::decode_from_slice(&[]).unwrap(),
Transfer::default()
);
assert_eq!(
DynamicRange::decode_from_slice(&[]).unwrap(),
DynamicRange::default()
);
assert_eq!(
ChromaLocation::decode_from_slice(&[]).unwrap(),
ChromaLocation::default()
);
assert_eq!(
DcpTargetGamut::decode_from_slice(&[]).unwrap(),
DcpTargetGamut::default()
);
}
#[test]
fn enum_non_default_code_zero_is_encoded_not_conflated() {
let b = Matrix::Rgb.encode_to_vec();
assert!(!b.is_empty(), "non-default code-0 Rgb must be encoded");
let back = Matrix::decode_from_slice(&b).unwrap();
assert_eq!(back, Matrix::Rgb);
assert!(back.is_rgb());
assert_ne!(back, Matrix::default());
}
#[test]
fn enum_unknown_round_trips_losslessly() {
macro_rules! rt_unknown {
($ty:ty) => {{
let v = <$ty>::Unknown(12_345);
let b = v.encode_to_vec();
assert_eq!(<$ty>::decode_from_slice(&b).unwrap(), v);
}};
}
rt_unknown!(Matrix);
rt_unknown!(Primaries);
rt_unknown!(Transfer);
rt_unknown!(DynamicRange);
rt_unknown!(ChromaLocation);
rt_unknown!(DcpTargetGamut);
rt_unknown!(PixelFormat);
}
#[test]
fn enum_non_default_round_trips() {
let cm = Matrix::Bt2020Ncl.encode_to_vec();
assert_eq!(Matrix::decode_from_slice(&cm).unwrap(), Matrix::Bt2020Ncl);
let cp = Primaries::Bt2020.encode_to_vec();
assert_eq!(
Primaries::decode_from_slice(&cp).unwrap(),
Primaries::Bt2020
);
let ct = Transfer::AribStdB67Hlg.encode_to_vec();
assert_eq!(
Transfer::decode_from_slice(&ct).unwrap(),
Transfer::AribStdB67Hlg
);
let dg = DcpTargetGamut::Rec2020.encode_to_vec();
assert_eq!(
DcpTargetGamut::decode_from_slice(&dg).unwrap(),
DcpTargetGamut::Rec2020
);
}
#[test]
fn dcp_target_gamut_unknown_canonicalization() {
for (misuse, named) in [
(DcpTargetGamut::Unknown(0), DcpTargetGamut::DciP3),
(DcpTargetGamut::Unknown(1), DcpTargetGamut::Rec709),
(DcpTargetGamut::Unknown(2), DcpTargetGamut::Rec2020),
] {
let b = misuse.encode_to_vec();
assert_eq!(DcpTargetGamut::decode_from_slice(&b).unwrap(), named);
}
for v in [3u32, 4242, u32::MAX] {
let u = DcpTargetGamut::Unknown(v);
let b = u.encode_to_vec();
assert_eq!(DcpTargetGamut::decode_from_slice(&b).unwrap(), u);
assert_eq!(DcpTargetGamut::from_u32(v), DcpTargetGamut::Unknown(v));
}
}
#[test]
fn color_matrix_bt601_domain_variant_round_trips() {
let b = Matrix::Bt601.encode_to_vec();
assert!(!b.is_empty(), "non-default domain Bt601 must be encoded");
let back = Matrix::decode_from_slice(&b).unwrap();
assert_eq!(back, Matrix::Bt601);
assert!(back.is_bt_601());
assert_ne!(back, Matrix::default());
assert!(Matrix::default().encode_to_vec().is_empty());
assert_eq!(Matrix::decode_from_slice(&[]).unwrap(), Matrix::default());
}
#[test]
fn color_matrix_default_instance_and_clear() {
assert_eq!(
*<Matrix as DefaultInstance>::default_instance(),
Matrix::default()
);
let mut m = Matrix::YCgCo;
Message::clear(&mut m);
assert_eq!(m, Matrix::default());
}
#[test]
fn color_range_round_trip() {
for r in [
DynamicRange::Unspecified,
DynamicRange::Limited,
DynamicRange::Full,
] {
let b = r.encode_to_vec();
assert_eq!(DynamicRange::decode_from_slice(&b).unwrap(), r);
}
}
#[test]
fn rotation_round_trip() {
for r in [
Rotation::D0,
Rotation::D90,
Rotation::D180,
Rotation::D270,
Rotation::Unknown(7),
Rotation::Unknown(4242),
] {
let b = r.encode_to_vec();
assert_eq!(Rotation::decode_from_slice(&b).unwrap(), r);
}
}
#[test]
fn enum_wrong_wire_type_errors() {
let mut buf: Vec<u8> = Vec::new();
Tag::new(1, WireType::LengthDelimited).encode(&mut buf);
encode_varint(0, &mut buf);
let err = <Matrix as Message>::decode_from_slice(&buf).unwrap_err();
assert!(
matches!(err, DecodeError::WireTypeMismatch { field_number: 1, expected, actual }
if expected == VARINT && actual == LEN),
"got {err:?}"
);
}
#[test]
fn enum_unknown_field_is_skipped() {
let mut buf = DynamicRange::Full.encode_to_vec();
Tag::new(7, WireType::Varint).encode(&mut buf); encode_varint(123, &mut buf);
assert_eq!(
<DynamicRange as Message>::decode_from_slice(&buf).unwrap(),
DynamicRange::Full
);
}
#[test]
fn enum_unknown_id_decodes_losslessly() {
let mut buf: Vec<u8> = Vec::new();
Tag::new(1, WireType::Varint).encode(&mut buf);
encode_uint32(9_999, &mut buf);
assert_eq!(
<Transfer as Message>::decode_from_slice(&buf).unwrap(),
Transfer::Unknown(9_999)
);
}
#[test]
fn pixel_format_round_trip_including_unknown() {
for p in [
PixelFormat::Yuv420p,
PixelFormat::default(), PixelFormat::Unknown(77),
] {
let b = p.encode_to_vec();
assert_eq!(PixelFormat::decode_from_slice(&b).unwrap(), p);
}
}
#[test]
fn dimensions_round_trip_and_default() {
for d in [
Dimensions::default(),
Dimensions::new(1920, 1080),
Dimensions::new(0, 720),
] {
let b = d.encode_to_vec();
assert_eq!(Dimensions::decode_from_slice(&b).unwrap(), d);
}
}
#[test]
fn dimensions_wrong_wire_type_and_unknown_skip() {
let mut buf: Vec<u8> = Vec::new();
Tag::new(2, WireType::LengthDelimited).encode(&mut buf);
encode_varint(0, &mut buf);
assert!(matches!(
<Dimensions as Message>::decode_from_slice(&buf).unwrap_err(),
DecodeError::WireTypeMismatch { field_number: 2, expected, actual }
if expected == VARINT && actual == LEN
));
let mut ok = Dimensions::new(64, 48).encode_to_vec();
Tag::new(9, WireType::Varint).encode(&mut ok);
encode_varint(5, &mut ok);
assert_eq!(
<Dimensions as Message>::decode_from_slice(&ok).unwrap(),
Dimensions::new(64, 48)
);
}
#[test]
fn rect_round_trip_and_default() {
for r in [
Rect::default(),
Rect::new(10, 20, 1280, 720),
Rect::new(0, 0, 0, 480),
] {
let b = r.encode_to_vec();
assert_eq!(Rect::decode_from_slice(&b).unwrap(), r);
}
}
#[test]
fn rect_wrong_wire_type_and_unknown_skip() {
let mut buf: Vec<u8> = Vec::new();
Tag::new(3, WireType::LengthDelimited).encode(&mut buf);
encode_varint(0, &mut buf);
assert!(matches!(
<Rect as Message>::decode_from_slice(&buf).unwrap_err(),
DecodeError::WireTypeMismatch { field_number: 3, expected, actual }
if expected == VARINT && actual == LEN
));
let mut ok = Rect::new(1, 2, 3, 4).encode_to_vec();
Tag::new(8, WireType::Varint).encode(&mut ok);
encode_varint(1, &mut ok);
assert_eq!(
<Rect as Message>::decode_from_slice(&ok).unwrap(),
Rect::new(1, 2, 3, 4)
);
}
#[test]
fn sar_round_trip_default_and_nondefault() {
for s in [
SampleAspectRatio::default(), SampleAspectRatio::new(40, nz(33)), SampleAspectRatio::new(0, nz(1)), ] {
let b = s.encode_to_vec();
assert_eq!(SampleAspectRatio::decode_from_slice(&b).unwrap(), s);
}
}
#[test]
fn sar_wire_is_byte_stable() {
let bytes = SampleAspectRatio::new(40, nz(33)).encode_to_vec();
let expected: Vec<u8> = [0x08u8, 0x28, 0x10, 0x21].into_iter().collect();
assert_eq!(bytes, expected);
assert_eq!(
SampleAspectRatio::decode_from_slice(&bytes).unwrap(),
SampleAspectRatio::new(40, nz(33))
);
}
#[test]
fn sar_field2_wrong_wire_type_errors() {
let mut buf: Vec<u8> = Vec::new();
Tag::new(1, WireType::Varint).encode(&mut buf);
encode_uint32(4, &mut buf);
Tag::new(2, WireType::LengthDelimited).encode(&mut buf);
encode_varint(0, &mut buf);
assert!(matches!(
<SampleAspectRatio as Message>::decode_from_slice(&buf).unwrap_err(),
DecodeError::WireTypeMismatch { field_number: 2, expected, actual }
if expected == VARINT && actual == LEN
));
}
#[test]
fn sar_den_zero_clamped_and_unknown_skip() {
let mut buf: Vec<u8> = Vec::new();
Tag::new(1, WireType::Varint).encode(&mut buf);
encode_uint32(16, &mut buf);
Tag::new(2, WireType::Varint).encode(&mut buf);
encode_uint32(0, &mut buf); Tag::new(6, WireType::Varint).encode(&mut buf); encode_varint(42, &mut buf);
let s = <SampleAspectRatio as Message>::decode_from_slice(&buf).unwrap();
assert_eq!(s.num(), 16);
assert_eq!(s.den().get(), 1);
}
#[test]
fn color_info_round_trip_default_and_nondefault() {
let default = Info::UNSPECIFIED;
let b = default.encode_to_vec();
assert_eq!(Info::decode_from_slice(&b).unwrap(), default);
let ci = Info::UNSPECIFIED
.with_primaries(Primaries::Bt2020)
.with_transfer(Transfer::SmpteSt2084Pq)
.with_matrix(Matrix::Bt2020Ncl)
.with_range(DynamicRange::Limited)
.with_chroma_location(ChromaLocation::Left);
let b2 = ci.encode_to_vec();
assert_eq!(Info::decode_from_slice(&b2).unwrap(), ci);
}
#[test]
fn color_info_matrix_always_encoded_round_trips_code_zero_matrix() {
let ci = Info::new(
Primaries::Unspecified,
Transfer::Unspecified,
Matrix::Rgb,
DynamicRange::Unspecified,
ChromaLocation::Unspecified,
);
let b = ci.encode_to_vec();
let back = Info::decode_from_slice(&b).unwrap();
assert_eq!(back, ci);
assert!(back.matrix().is_rgb());
}
#[test]
fn color_info_wrong_wire_type_and_unknown_skip() {
let mut buf: Vec<u8> = Vec::new();
Tag::new(3, WireType::LengthDelimited).encode(&mut buf);
encode_varint(0, &mut buf);
assert!(matches!(
<Info as Message>::decode_from_slice(&buf).unwrap_err(),
DecodeError::WireTypeMismatch { field_number: 3, expected, actual }
if expected == VARINT && actual == LEN
));
let mut ok = Info::UNSPECIFIED
.with_range(DynamicRange::Full)
.encode_to_vec();
Tag::new(9, WireType::Varint).encode(&mut ok);
encode_varint(1, &mut ok);
assert_eq!(
<Info as Message>::decode_from_slice(&ok).unwrap(),
Info::UNSPECIFIED.with_range(DynamicRange::Full)
);
}
#[test]
fn content_light_round_trip_and_default() {
for c in [
ContentLightLevel::default(),
ContentLightLevel::new(1000, 400),
ContentLightLevel::new(0, 250),
] {
let b = c.encode_to_vec();
assert_eq!(ContentLightLevel::decode_from_slice(&b).unwrap(), c);
}
}
#[test]
fn content_light_wrong_wire_type_and_unknown_skip() {
let mut buf: Vec<u8> = Vec::new();
Tag::new(1, WireType::LengthDelimited).encode(&mut buf);
encode_varint(0, &mut buf);
assert!(matches!(
<ContentLightLevel as Message>::decode_from_slice(&buf).unwrap_err(),
DecodeError::WireTypeMismatch { field_number: 1, expected, actual }
if expected == VARINT && actual == LEN
));
let mut ok = ContentLightLevel::new(4000, 1000).encode_to_vec();
Tag::new(5, WireType::Varint).encode(&mut ok);
encode_varint(9, &mut ok);
assert_eq!(
<ContentLightLevel as Message>::decode_from_slice(&ok).unwrap(),
ContentLightLevel::new(4000, 1000)
);
}
#[test]
fn chroma_coord_round_trip_and_default() {
for c in [
ChromaCoord::default(),
cc(34000, 16000),
cc(0, 3000),
cc(u16::MAX as u32, u16::MAX as u32),
cc(70_000, 100_000),
cc(u32::MAX, u32::MAX - 1),
] {
let b = c.encode_to_vec();
assert_eq!(ChromaCoord::decode_from_slice(&b).unwrap(), c);
}
}
#[test]
fn mastering_display_round_trip_default_and_nondefault() {
let default = MasteringDisplay::default();
let b = default.encode_to_vec();
assert_eq!(MasteringDisplay::decode_from_slice(&b).unwrap(), default);
let md = MasteringDisplay::new(
[cc(34000, 16000), cc(13250, 34500), cc(7500, 3000)],
cc(15635, 16450),
10_000_000,
50,
);
let b2 = md.encode_to_vec();
let back = MasteringDisplay::decode_from_slice(&b2).unwrap();
assert_eq!(back, md);
assert_eq!(back.display_primaries()[1], cc(13250, 34500));
let md2 = MasteringDisplay::new([cc(1, 2), cc(3, 4), cc(5, 6)], cc(7, 8), 0, 0);
let b3 = md2.encode_to_vec();
assert_eq!(MasteringDisplay::decode_from_slice(&b3).unwrap(), md2);
}
#[test]
fn mastering_display_wrong_wire_type_and_unknown_skip() {
let mut buf: Vec<u8> = Vec::new();
Tag::new(2, WireType::Varint).encode(&mut buf);
encode_varint(0, &mut buf);
assert!(matches!(
<MasteringDisplay as Message>::decode_from_slice(&buf).unwrap_err(),
DecodeError::WireTypeMismatch { field_number: 2, expected, actual }
if expected == LEN && actual == VARINT
));
let mut buf5: Vec<u8> = Vec::new();
Tag::new(5, WireType::LengthDelimited).encode(&mut buf5);
encode_varint(0, &mut buf5);
assert!(matches!(
<MasteringDisplay as Message>::decode_from_slice(&buf5).unwrap_err(),
DecodeError::WireTypeMismatch { field_number: 5, expected, actual }
if expected == VARINT && actual == LEN
));
let original = MasteringDisplay::new([cc(9, 9), cc(8, 8), cc(7, 7)], cc(6, 6), 123, 4);
let mut ok = original.encode_to_vec();
Tag::new(12, WireType::Varint).encode(&mut ok);
encode_varint(99, &mut ok);
assert_eq!(
<MasteringDisplay as Message>::decode_from_slice(&ok).unwrap(),
original
);
}
#[test]
fn hdr_static_metadata_round_trip_all_presence_combos() {
let cll = ContentLightLevel::new(1000, 400);
let md = MasteringDisplay::new(
[cc(34000, 16000), cc(13250, 34500), cc(7500, 3000)],
cc(15635, 16450),
10_000_000,
50,
);
for h in [
HdrStaticMetadata::default(), HdrStaticMetadata::new(Some(md), None), HdrStaticMetadata::new(None, Some(cll)), HdrStaticMetadata::new(Some(md), Some(cll)), ] {
let b = h.encode_to_vec();
assert_eq!(HdrStaticMetadata::decode_from_slice(&b).unwrap(), h);
}
}
#[test]
fn hdr_static_metadata_wrong_wire_type_and_unknown_skip() {
let mut buf: Vec<u8> = Vec::new();
Tag::new(1, WireType::Varint).encode(&mut buf);
encode_varint(0, &mut buf);
assert!(matches!(
<HdrStaticMetadata as Message>::decode_from_slice(&buf).unwrap_err(),
DecodeError::WireTypeMismatch { field_number: 1, expected, actual }
if expected == LEN && actual == VARINT
));
let original = HdrStaticMetadata::new(None, Some(ContentLightLevel::new(2000, 500)));
let mut ok = original.encode_to_vec();
Tag::new(7, WireType::Varint).encode(&mut ok);
encode_varint(3, &mut ok);
assert_eq!(
<HdrStaticMetadata as Message>::decode_from_slice(&ok).unwrap(),
original
);
}
#[test]
fn field_order_round_trip() {
for f in [
FieldOrder::Unknown(0),
FieldOrder::Progressive,
FieldOrder::Tt,
FieldOrder::Bb,
FieldOrder::Tb,
FieldOrder::Bt,
FieldOrder::Unknown(7),
FieldOrder::Unknown(4242),
] {
let b = f.encode_to_vec();
assert_eq!(FieldOrder::decode_from_slice(&b).unwrap(), f);
}
assert!(FieldOrder::default().encode_to_vec().is_empty());
assert_eq!(
FieldOrder::decode_from_slice(&[]).unwrap(),
FieldOrder::default()
);
}
#[test]
fn field_order_wrong_wire_type_errors() {
let mut buf: Vec<u8> = Vec::new();
Tag::new(1, WireType::LengthDelimited).encode(&mut buf);
encode_varint(0, &mut buf);
let err = <FieldOrder as Message>::decode_from_slice(&buf).unwrap_err();
assert!(
matches!(err, DecodeError::WireTypeMismatch { field_number: 1, expected, actual }
if expected == VARINT && actual == LEN),
"got {err:?}"
);
}
#[test]
fn stereo_mode_round_trip() {
for s in [
StereoMode::Mono,
StereoMode::SideBySide,
StereoMode::TopBottom,
StereoMode::FrameSequence,
StereoMode::Checkerboard,
StereoMode::SideBySideQuincunx,
StereoMode::Lines,
StereoMode::Columns,
StereoMode::Unknown(99),
StereoMode::Unknown(4242),
] {
let b = s.encode_to_vec();
assert_eq!(StereoMode::decode_from_slice(&b).unwrap(), s);
}
assert!(StereoMode::default().encode_to_vec().is_empty());
assert_eq!(
StereoMode::decode_from_slice(&[]).unwrap(),
StereoMode::default()
);
}
#[test]
fn stereo_mode_unknown_canonicalization() {
for (misuse, named) in [
(StereoMode::Unknown(0), StereoMode::Mono),
(StereoMode::Unknown(1), StereoMode::SideBySide),
(StereoMode::Unknown(7), StereoMode::Columns),
] {
let b = misuse.encode_to_vec();
assert_eq!(StereoMode::decode_from_slice(&b).unwrap(), named);
}
for v in [8u32, 4242, u32::MAX] {
let u = StereoMode::Unknown(v);
let b = u.encode_to_vec();
assert_eq!(StereoMode::decode_from_slice(&b).unwrap(), u);
assert_eq!(StereoMode::from_u32(v), StereoMode::Unknown(v));
}
}
#[test]
fn stereo_mode_wrong_wire_type_errors() {
let mut buf: Vec<u8> = Vec::new();
Tag::new(1, WireType::LengthDelimited).encode(&mut buf);
encode_varint(0, &mut buf);
let err = <StereoMode as Message>::decode_from_slice(&buf).unwrap_err();
assert!(
matches!(err, DecodeError::WireTypeMismatch { field_number: 1, expected, actual }
if expected == VARINT && actual == LEN),
"got {err:?}"
);
}
#[test]
fn rational_round_trip_default_and_nondefault() {
for r in [
Rational::default(), Rational::new(30000, nz(1001)), Rational::new(0, nz(1)), ] {
let b = r.encode_to_vec();
assert_eq!(Rational::decode_from_slice(&b).unwrap(), r);
}
}
#[test]
fn rational_field2_wrong_wire_type_errors() {
let mut buf: Vec<u8> = Vec::new();
Tag::new(1, WireType::Varint).encode(&mut buf);
encode_uint32(4, &mut buf);
Tag::new(2, WireType::LengthDelimited).encode(&mut buf);
encode_varint(0, &mut buf);
assert!(matches!(
<Rational as Message>::decode_from_slice(&buf).unwrap_err(),
DecodeError::WireTypeMismatch { field_number: 2, expected, actual }
if expected == VARINT && actual == LEN
));
}
#[test]
fn rational_den_zero_clamped_and_unknown_skip() {
let mut buf: Vec<u8> = Vec::new();
Tag::new(1, WireType::Varint).encode(&mut buf);
encode_uint32(24, &mut buf);
Tag::new(2, WireType::Varint).encode(&mut buf);
encode_uint32(0, &mut buf); Tag::new(6, WireType::Varint).encode(&mut buf); encode_varint(42, &mut buf);
let r = <Rational as Message>::decode_from_slice(&buf).unwrap();
assert_eq!(r.num(), 24);
assert_eq!(r.den().get(), 1);
}
#[test]
fn frame_rate_round_trip_default_and_nondefault() {
for fr in [
FrameRate::default(), FrameRate::new(Rational::new(30000, nz(1001)), false), FrameRate::new(Rational::new(60, nz(1)), true), FrameRate::new(Rational::new(0, nz(1)), true), ] {
let b = fr.encode_to_vec();
assert_eq!(FrameRate::decode_from_slice(&b).unwrap(), fr);
}
}
#[test]
fn frame_rate_wrong_wire_type_and_unknown_skip() {
let mut buf: Vec<u8> = Vec::new();
Tag::new(1, WireType::Varint).encode(&mut buf);
encode_varint(0, &mut buf);
assert!(matches!(
<FrameRate as Message>::decode_from_slice(&buf).unwrap_err(),
DecodeError::WireTypeMismatch { field_number: 1, expected, actual }
if expected == LEN && actual == VARINT
));
let original = FrameRate::new(Rational::new(25, nz(1)), true);
let mut ok = original.encode_to_vec();
Tag::new(9, WireType::Varint).encode(&mut ok);
encode_varint(7, &mut ok);
assert_eq!(
<FrameRate as Message>::decode_from_slice(&ok).unwrap(),
original
);
}
#[test]
fn dolby_vision_config_round_trip_default_and_nondefault() {
for d in [
DolbyVisionConfig::default(),
DolbyVisionConfig::new(8, 9, true, false, 1),
DolbyVisionConfig::new(5, 6, true, true, 2),
DolbyVisionConfig::new(0, 0, false, true, 0), DolbyVisionConfig::new(255, 255, true, true, 255),
] {
let b = d.encode_to_vec();
assert_eq!(DolbyVisionConfig::decode_from_slice(&b).unwrap(), d);
}
}
#[test]
fn dolby_vision_config_wrong_wire_type_and_unknown_skip() {
let mut buf: Vec<u8> = Vec::new();
Tag::new(1, WireType::LengthDelimited).encode(&mut buf);
encode_varint(0, &mut buf);
assert!(matches!(
<DolbyVisionConfig as Message>::decode_from_slice(&buf).unwrap_err(),
DecodeError::WireTypeMismatch { field_number: 1, expected, actual }
if expected == VARINT && actual == LEN
));
let original = DolbyVisionConfig::new(7, 4, true, false, 4);
let mut ok = original.encode_to_vec();
Tag::new(11, WireType::Varint).encode(&mut ok);
encode_varint(9, &mut ok);
assert_eq!(
<DolbyVisionConfig as Message>::decode_from_slice(&ok).unwrap(),
original
);
}
#[test]
fn channel_layout_round_trip_named_and_other() {
let v = ChannelLayout::Stereo;
assert_eq!(
ChannelLayout::decode_from_slice(&v.encode_to_vec()).unwrap(),
v
);
let v = ChannelLayout::N5Point1;
assert_eq!(
ChannelLayout::decode_from_slice(&v.encode_to_vec()).unwrap(),
v
);
let v = ChannelLayout::Other(SmolStr::new("22.2"));
assert_eq!(
ChannelLayout::decode_from_slice(&v.encode_to_vec()).unwrap(),
v
);
assert!(ChannelLayout::default().encode_to_vec().is_empty());
assert_eq!(
ChannelLayout::decode_from_slice(&[]).unwrap(),
ChannelLayout::default()
);
}
#[test]
fn audio_container_round_trip() {
let v = ContainerFormat::Mp3;
assert_eq!(
ContainerFormat::decode_from_slice(&v.encode_to_vec()).unwrap(),
v
);
let v = ContainerFormat::Other(SmolStr::new("snd"));
assert_eq!(
ContainerFormat::decode_from_slice(&v.encode_to_vec()).unwrap(),
v
);
}
#[test]
fn container_format_round_trip() {
let v = Format::Mp4;
assert_eq!(Format::decode_from_slice(&v.encode_to_vec()).unwrap(), v);
let v = Format::Threegp;
assert_eq!(Format::decode_from_slice(&v.encode_to_vec()).unwrap(), v);
}
#[test]
fn bit_rate_mode_round_trip() {
assert!(BitRateMode::Cbr.encode_to_vec().is_empty());
assert_eq!(
BitRateMode::decode_from_slice(&[]).unwrap(),
BitRateMode::Cbr
);
for v in [BitRateMode::Vbr, BitRateMode::Abr] {
assert_eq!(
BitRateMode::decode_from_slice(&v.encode_to_vec()).unwrap(),
v
);
}
}
#[test]
fn audio_format_round_trip_named_and_unknown() {
assert!(SampleFormat::default().encode_to_vec().is_empty());
assert_eq!(
SampleFormat::decode_from_slice(&[]).unwrap(),
SampleFormat::default()
);
let b = SampleFormat::U8.encode_to_vec();
assert!(!b.is_empty(), "non-default code-0 U8 must be encoded");
assert_eq!(
SampleFormat::decode_from_slice(&b).unwrap(),
SampleFormat::U8
);
let b = SampleFormat::Fltp.encode_to_vec();
assert_eq!(
SampleFormat::decode_from_slice(&b).unwrap(),
SampleFormat::Fltp
);
let v = SampleFormat::Unknown(12_345);
assert_eq!(
SampleFormat::decode_from_slice(&v.encode_to_vec()).unwrap(),
v
);
}
#[test]
fn loudness_round_trip_with_zero_elision() {
assert!(Loudness::default().encode_to_vec().is_empty());
assert_eq!(
Loudness::decode_from_slice(&[]).unwrap(),
Loudness::default()
);
let l = Loudness::new(-23.0, 7.5, -1.25, -3.5);
let b = l.encode_to_vec();
assert_eq!(Loudness::decode_from_slice(&b).unwrap(), l);
let l = Loudness::default().with_true_peak_dbtp(-1.0);
assert_eq!(Loudness::decode_from_slice(&l.encode_to_vec()).unwrap(), l);
}
#[test]
fn audio_fingerprint_round_trip() {
let fp = Fingerprint::try_new("chromaprint", ::buffa::alloc::vec![0xAA, 0xBB, 0xCC]).unwrap();
let b = fp.encode_to_vec();
assert_eq!(Fingerprint::decode_from_slice(&b).unwrap(), fp);
let fp = Fingerprint::try_new("acoustid", ::buffa::alloc::vec::Vec::new()).unwrap();
let b = fp.encode_to_vec();
assert_eq!(Fingerprint::decode_from_slice(&b).unwrap(), fp);
}
#[test]
fn audio_cover_art_round_trip() {
let art = CoverArt::try_new("image/jpeg", ::buffa::alloc::vec![0xFF, 0xD8, 0xFF]).unwrap();
let b = art.encode_to_vec();
assert_eq!(CoverArt::decode_from_slice(&b).unwrap(), art);
}
#[test]
fn audio_tags_round_trip() {
let t = Tags::new()
.with_title("Song")
.with_artist("Band")
.with_album("Album")
.with_year(1999)
.with_track_number(3)
.with_track_total(12)
.with_language(crate::lang::Language::from_bcp47("en-US").unwrap());
let b = t.encode_to_vec();
assert_eq!(Tags::decode_from_slice(&b).unwrap(), t);
let t0 = Tags::default();
assert!(t0.encode_to_vec().is_empty());
assert_eq!(Tags::decode_from_slice(&[]).unwrap(), t0);
let z = Tags::new().with_year(0).with_track_number(0);
assert_eq!(z, Tags::default());
assert_eq!(Tags::decode_from_slice(&z.encode_to_vec()).unwrap(), z);
}
#[cfg(any(feature = "std", feature = "alloc"))]
#[test]
fn device_round_trip_empty_and_populated() {
use crate::capture::Device;
assert!(Device::default().encode_to_vec().is_empty());
assert_eq!(Device::decode_from_slice(&[]).unwrap(), Device::default());
let d = Device::new().with_make("Apple").with_model("iPhone 15 Pro");
let b = d.encode_to_vec();
assert_eq!(Device::decode_from_slice(&b).unwrap(), d);
let m = Device::new().with_make("Sony");
assert_eq!(Device::decode_from_slice(&m.encode_to_vec()).unwrap(), m);
let n = Device::new().with_model("ILCE-7M4");
assert_eq!(Device::decode_from_slice(&n.encode_to_vec()).unwrap(), n);
}
#[cfg(any(feature = "std", feature = "alloc"))]
#[test]
fn geo_location_round_trip_null_island_and_populated() {
use crate::capture::GeoLocation;
let null = GeoLocation::default();
let b = null.encode_to_vec();
assert!(!b.is_empty());
assert_eq!(GeoLocation::decode_from_slice(&b).unwrap(), null);
let paris = GeoLocation::try_new(48.8566, 2.3522, None).unwrap();
let b = paris.encode_to_vec();
let back = GeoLocation::decode_from_slice(&b).unwrap();
assert!((back.lat() - paris.lat()).abs() < 1e-9);
assert!((back.lon() - paris.lon()).abs() < 1e-9);
assert!(back.altitude().is_none());
let sea = GeoLocation::try_new(48.8566, 2.3522, Some(0.0)).unwrap();
let b = sea.encode_to_vec();
let back = GeoLocation::decode_from_slice(&b).unwrap();
assert_eq!(back.altitude(), Some(0.0));
let sp = GeoLocation::try_new(-23.5505, -46.6333, Some(760.0)).unwrap();
let b = sp.encode_to_vec();
let back = GeoLocation::decode_from_slice(&b).unwrap();
assert!((back.lat() - sp.lat()).abs() < 1e-9);
assert!((back.lon() - sp.lon()).abs() < 1e-9);
assert_eq!(back.altitude(), Some(760.0));
}
#[cfg(any(feature = "std", feature = "alloc"))]
#[test]
fn language_round_trip_und_and_populated() {
use crate::lang::Language;
let und = Language::default();
let b = und.encode_to_vec();
assert!(!b.is_empty());
assert_eq!(Language::decode_from_slice(&b).unwrap(), und);
assert_eq!(Language::decode_from_slice(&[]).unwrap(), und);
for tag in ["en", "en-US", "zh-Hant-TW"] {
let l = Language::from_bcp47(tag).unwrap();
let b = l.encode_to_vec();
assert_eq!(Language::decode_from_slice(&b).unwrap(), l);
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
#[test]
fn language_wire_garbage_falls_back_to_und() {
use crate::lang::Language;
let mut buf: Vec<u8> = Vec::new();
Tag::new(1, WireType::LengthDelimited).encode(&mut buf);
encode_varint("xx-yy-zz-bogus".len() as u64, &mut buf);
buf.extend_from_slice("xx-yy-zz-bogus".as_bytes());
assert_eq!(
Language::decode_from_slice(&buf).unwrap(),
Language::default()
);
}
}