mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-27 16:59:56 +01:00
craft-video: reject out-of-frame Theora EncoderConfig picture bounds
This commit is contained in:
1 parent
9efbe4517a
commit
b2756f6f73
2 files changed
+41
-1
No files matched your search
@@ -104,9 +104,16 @@ fn alloc_vec_exact<T: Clone>(val: T, n: usize) -> Result<Vec<T>> {
|
||||
impl Encoder {
|
||||
/// Create a new encoder with the given configuration.
|
||||
pub fn new(config: EncoderConfig) -> Result<Self> {
|
||||
if config.width == 0 || config.height == 0 {
|
||||
if config.width == 0 || config.height == 0 || config.pic_width == 0 || config.pic_height == 0 {
|
||||
return Err(Error::DimensionMismatch("dimensions cannot be zero"));
|
||||
}
|
||||
if config.pic_x > 255
|
||||
|| config.pic_y > 255
|
||||
|| config.pic_x.checked_add(config.pic_width).is_none_or(|end| end > config.width)
|
||||
|| config.pic_y.checked_add(config.pic_height).is_none_or(|end| end > config.height)
|
||||
{
|
||||
return Err(Error::DimensionMismatch("picture region exceeds frame bounds"));
|
||||
}
|
||||
let frame_width = config.width.checked_add(15).map(|w| w & !15).ok_or(Error::DimensionMismatch("width overflows"))?;
|
||||
let frame_height = config.height.checked_add(15).map(|h| h & !15).ok_or(Error::DimensionMismatch("height overflows"))?;
|
||||
let fmbw_u64 = (frame_width / 16) as u64;
|
||||
|
||||
@@ -608,6 +608,39 @@ fn test_theora_encoder_invalid_input_rejected() {
|
||||
let mut pic = test_picture(32, 32);
|
||||
pic.y_stride = 16;
|
||||
assert!(matches!(enc.encode_frame(&pic, true), Err(Error::DimensionMismatch(_))));
|
||||
|
||||
// Out-of-frame configuration picture bounds cannot bypass active picture validation
|
||||
let cfg_out_of_frame = EncoderConfig {
|
||||
width: 32,
|
||||
height: 32,
|
||||
pic_width: 16,
|
||||
pic_height: 32,
|
||||
pic_x: 32,
|
||||
pic_y: 0,
|
||||
fps_num: 25,
|
||||
fps_den: 1,
|
||||
pixel_format: PixelFormat::Yuv420,
|
||||
color_space: ColorSpace::Undefined,
|
||||
quality: 40,
|
||||
keyframe_distance: 64,
|
||||
};
|
||||
assert!(matches!(Encoder::new(cfg_out_of_frame), Err(Error::DimensionMismatch(_))));
|
||||
|
||||
let cfg_zero_pic = EncoderConfig {
|
||||
width: 32,
|
||||
height: 32,
|
||||
pic_width: 0,
|
||||
pic_height: 32,
|
||||
pic_x: 0,
|
||||
pic_y: 0,
|
||||
fps_num: 25,
|
||||
fps_den: 1,
|
||||
pixel_format: PixelFormat::Yuv420,
|
||||
color_space: ColorSpace::Undefined,
|
||||
quality: 40,
|
||||
keyframe_distance: 64,
|
||||
};
|
||||
assert!(matches!(Encoder::new(cfg_zero_pic), Err(Error::DimensionMismatch(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user