| 1 |
// Copyright (c) 2001-2016, Alliance for Open Media. All rights reserved |
| 2 |
// Copyright (c) 2017-2022, The rav1e contributors. All rights reserved |
| 3 |
// |
| 4 |
// This source code is subject to the terms of the BSD 2 Clause License and |
| 5 |
// the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 6 |
// was not distributed with this source code in the LICENSE file, you can |
| 7 |
// obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 8 |
// Media Patent License 1.0 was not distributed with this source code in the |
| 9 |
// PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
| 10 |
|
| 11 |
use std::io::Read; |
| 12 |
|
| 13 |
use crate::color::ChromaSampling::Cs400; |
| 14 |
use crate::decoder::{DecodeError, Decoder, FrameBuilder, VideoDetails}; |
| 15 |
use rav1e::prelude::*; |
| 16 |
|
| 17 |
impl Decoder for y4m::Decoder<Box<dyn Read + Send>> { |
| 18 |
fn get_video_details(&self) -> VideoDetails { |
| 19 |
let width = self.get_width(); |
| 20 |
let height = self.get_height(); |
| 21 |
let aspect_ratio = self.get_pixel_aspect(); |
| 22 |
let color_space = self.get_colorspace(); |
| 23 |
let bit_depth = color_space.get_bit_depth(); |
| 24 |
let (chroma_sampling, chroma_sample_position) = |
| 25 |
map_y4m_color_space(color_space); |
| 26 |
let framerate = self.get_framerate(); |
| 27 |
let time_base = Rational::new(framerate.den as u64, framerate.num as u64); |
| 28 |
|
| 29 |
VideoDetails { |
| 30 |
width, |
| 31 |
height, |
| 32 |
sample_aspect_ratio: if aspect_ratio.num == 0 && aspect_ratio.den == 0 { |
| 33 |
Rational::new(1, 1) |
| 34 |
} else { |
| 35 |
Rational::new(aspect_ratio.num as u64, aspect_ratio.den as u64) |
| 36 |
}, |
| 37 |
bit_depth, |
| 38 |
chroma_sampling, |
| 39 |
chroma_sample_position, |
| 40 |
time_base, |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
fn read_frame<T: Pixel, F: FrameBuilder<T>>( |
| 45 |
&mut self, ctx: &F, cfg: &VideoDetails, |
| 46 |
) -> Result<Frame<T>, DecodeError> { |
| 47 |
let bytes = self.get_bytes_per_sample(); |
| 48 |
self |
| 49 |
.read_frame() |
| 50 |
.map(|frame| { |
| 51 |
let mut f = ctx.new_frame(); |
| 52 |
|
| 53 |
let (chroma_width, _) = |
| 54 |
cfg.chroma_sampling.get_chroma_dimensions(cfg.width, cfg.height); |
| 55 |
|
| 56 |
f.planes[0].copy_from_raw_u8( |
| 57 |
frame.get_y_plane(), |
| 58 |
cfg.width * bytes, |
| 59 |
bytes, |
| 60 |
); |
| 61 |
if cfg.chroma_sampling != Cs400 { |
| 62 |
f.planes[1].copy_from_raw_u8( |
| 63 |
frame.get_u_plane(), |
| 64 |
chroma_width * bytes, |
| 65 |
bytes, |
| 66 |
); |
| 67 |
f.planes[2].copy_from_raw_u8( |
| 68 |
frame.get_v_plane(), |
| 69 |
chroma_width * bytes, |
| 70 |
bytes, |
| 71 |
); |
| 72 |
} |
| 73 |
f |
| 74 |
}) |
| 75 |
.map_err(Into::into) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
impl From<y4m::Error> for DecodeError { |
| 80 |
fn from(e: y4m::Error) -> DecodeError { |
| 81 |
match e { |
| 82 |
y4m::Error::EOF => DecodeError::EOF, |
| 83 |
y4m::Error::BadInput => DecodeError::BadInput, |
| 84 |
y4m::Error::UnknownColorspace => DecodeError::UnknownColorspace, |
| 85 |
y4m::Error::ParseError(_) => DecodeError::ParseError, |
| 86 |
y4m::Error::IoError(_) => DecodeError::IoError, |
| 87 |
// Note that this error code has nothing to do with the system running out of memory, |
| 88 |
// it means the y4m decoder has exceeded its memory allocation limit. |
| 89 |
y4m::Error::OutOfMemory => DecodeError::MemoryLimitExceeded, |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
pub const fn map_y4m_color_space( |
| 95 |
color_space: y4m::Colorspace, |
| 96 |
) -> (ChromaSampling, ChromaSamplePosition) { |
| 97 |
use crate::ChromaSamplePosition::*; |
| 98 |
use crate::ChromaSampling::*; |
| 99 |
use y4m::Colorspace::*; |
| 100 |
match color_space { |
| 101 |
Cmono | Cmono12 => (Cs400, Unknown), |
| 102 |
C420jpeg | C420paldv => (Cs420, Unknown), |
| 103 |
C420mpeg2 => (Cs420, Vertical), |
| 104 |
C420 | C420p10 | C420p12 => (Cs420, Colocated), |
| 105 |
C422 | C422p10 | C422p12 => (Cs422, Colocated), |
| 106 |
C444 | C444p10 | C444p12 => (Cs444, Colocated), |
| 107 |
_ => unimplemented!(), |
| 108 |
} |
| 109 |
} |