| 1 |
use std::{path::Path, ptr::addr_of_mut}; |
| 2 |
|
| 3 |
use anyhow::{bail, ensure, Result}; |
| 4 |
use arrayvec::ArrayVec; |
| 5 |
use ffmpeg_next::{ |
| 6 |
codec::context::Context, |
| 7 |
decoder, |
| 8 |
format::{context::Input, input, Pixel}, |
| 9 |
media::Type as MediaType, |
| 10 |
Error::StreamNotFound, |
| 11 |
}; |
| 12 |
use num_rational::Rational64; |
| 13 |
use v_frame::{frame::Frame, plane::Plane}; |
| 14 |
|
| 15 |
use super::{ColorFormat, PixelFormat, VideoSource}; |
| 16 |
|
| 17 |
pub(super) struct FfmpegSource { |
| 18 |
input: Input, |
| 19 |
decoder: decoder::Video, |
| 20 |
next_frameno: usize, |
| 21 |
} |
| 22 |
|
| 23 |
impl FfmpegSource { |
| 24 |
pub(super) fn open(source: &Path) -> Result<Self> { |
| 25 |
let input = input(&source)?; |
| 26 |
let video_stream = input |
| 27 |
.streams() |
| 28 |
.best(MediaType::Video) |
| 29 |
.ok_or(StreamNotFound)?; |
| 30 |
let decoder = Context::from_parameters(video_stream.parameters())? |
| 31 |
.decoder() |
| 32 |
.video()?; |
| 33 |
|
| 34 |
Ok(FfmpegSource { |
| 35 |
input, |
| 36 |
decoder, |
| 37 |
next_frameno: 0, |
| 38 |
}) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
impl VideoSource for FfmpegSource { |
| 43 |
fn read_frame(&mut self, frameno: usize) -> Result<Option<Frame<u8>>> { |
| 44 |
ensure!( |
| 45 |
frameno == self.next_frameno, |
| 46 |
"Frame number mismatch in read_frame, ffmpeg decoder is desynced from av1-grain" |
| 47 |
); |
| 48 |
|
| 49 |
loop { |
| 50 |
// SAFETY: This is a really bad Rust interface from ffmpeg_next. |
| 51 |
// We don't let the frame escape unless it's initialized successfully. |
| 52 |
unsafe { |
| 53 |
let mut frame = ffmpeg_next::util::frame::Frame::empty(); |
| 54 |
if self.decoder.receive_frame(&mut frame).is_ok() { |
| 55 |
self.next_frameno += 1; |
| 56 |
return Ok(Some(ffmpeg_frame_to_v_frame(&mut frame))); |
| 57 |
} |
| 58 |
}; |
| 59 |
|
| 60 |
let video_stream = self |
| 61 |
.input |
| 62 |
.streams() |
| 63 |
.best(MediaType::Video) |
| 64 |
.ok_or(StreamNotFound)?; |
| 65 |
let video_stream_index = video_stream.index(); |
| 66 |
|
| 67 |
let packet = self |
| 68 |
.input |
| 69 |
.packets() |
| 70 |
.find(|&(ref stream, _)| stream.index() == video_stream_index); |
| 71 |
if packet.is_none() { |
| 72 |
return Ok(None); |
| 73 |
} |
| 74 |
|
| 75 |
let (_, packet) = packet.unwrap(); |
| 76 |
self.decoder.send_packet(&packet)?; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
fn get_frame_count(&mut self) -> Result<usize> { |
| 81 |
let video_stream = self |
| 82 |
.input |
| 83 |
.streams() |
| 84 |
.best(MediaType::Video) |
| 85 |
.ok_or(StreamNotFound)?; |
| 86 |
let video_stream_index = video_stream.index(); |
| 87 |
|
| 88 |
let num_frames = self |
| 89 |
.input |
| 90 |
.packets() |
| 91 |
.filter(|&(ref stream, _)| stream.index() == video_stream_index) |
| 92 |
.count(); |
| 93 |
self.input.seek(0, 0..1)?; |
| 94 |
|
| 95 |
ensure!(num_frames > 0, "ffmpeg reported 0 frames"); |
| 96 |
|
| 97 |
Ok(num_frames) |
| 98 |
} |
| 99 |
|
| 100 |
fn get_frame_rate(&mut self) -> Result<Rational64> { |
| 101 |
let video_stream = self |
| 102 |
.input |
| 103 |
.streams() |
| 104 |
.best(MediaType::Video) |
| 105 |
.ok_or(StreamNotFound)?; |
| 106 |
let rate = video_stream.avg_frame_rate(); |
| 107 |
Ok(Rational64::new(i64::from(rate.0), i64::from(rate.1))) |
| 108 |
} |
| 109 |
|
| 110 |
fn get_resolution(&mut self) -> Result<(u32, u32)> { |
| 111 |
let video_stream = self |
| 112 |
.input |
| 113 |
.streams() |
| 114 |
.best(MediaType::Video) |
| 115 |
.ok_or(StreamNotFound)?; |
| 116 |
|
| 117 |
let decoder = Context::from_parameters(video_stream.parameters())? |
| 118 |
.decoder() |
| 119 |
.video()?; |
| 120 |
|
| 121 |
Ok((decoder.width(), decoder.height())) |
| 122 |
} |
| 123 |
|
| 124 |
fn get_pixel_format(&mut self) -> Result<ColorFormat> { |
| 125 |
let video_stream = self |
| 126 |
.input |
| 127 |
.streams() |
| 128 |
.best(MediaType::Video) |
| 129 |
.ok_or(StreamNotFound)?; |
| 130 |
|
| 131 |
let decoder = Context::from_parameters(video_stream.parameters())? |
| 132 |
.decoder() |
| 133 |
.video()?; |
| 134 |
|
| 135 |
ColorFormat::try_from(decoder.format()) |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
impl TryFrom<Pixel> for ColorFormat { |
| 140 |
type Error = anyhow::Error; |
| 141 |
|
| 142 |
fn try_from(format: Pixel) -> Result<Self> { |
| 143 |
Ok(match format { |
| 144 |
Pixel::YUV420P | Pixel::YUVJ420P => ColorFormat { |
| 145 |
pixel_format: PixelFormat::YUV420, |
| 146 |
bit_depth: 8, |
| 147 |
}, |
| 148 |
Pixel::YUV422P | Pixel::YUVJ422P => ColorFormat { |
| 149 |
pixel_format: PixelFormat::YUV422, |
| 150 |
bit_depth: 8, |
| 151 |
}, |
| 152 |
Pixel::YUV444P | Pixel::YUVJ444P => ColorFormat { |
| 153 |
pixel_format: PixelFormat::YUV444, |
| 154 |
bit_depth: 8, |
| 155 |
}, |
| 156 |
Pixel::YUV420P9 | Pixel::YUV420P9BE | Pixel::YUV420P9LE => ColorFormat { |
| 157 |
pixel_format: PixelFormat::YUV420, |
| 158 |
bit_depth: 9, |
| 159 |
}, |
| 160 |
Pixel::YUV422P9 | Pixel::YUV422P9BE | Pixel::YUV422P9LE => ColorFormat { |
| 161 |
pixel_format: PixelFormat::YUV422, |
| 162 |
bit_depth: 9, |
| 163 |
}, |
| 164 |
Pixel::YUV444P9 | Pixel::YUV444P9BE | Pixel::YUV444P9LE => ColorFormat { |
| 165 |
pixel_format: PixelFormat::YUV444, |
| 166 |
bit_depth: 9, |
| 167 |
}, |
| 168 |
Pixel::YUV420P10 | Pixel::YUV420P10BE | Pixel::YUV420P10LE => ColorFormat { |
| 169 |
pixel_format: PixelFormat::YUV420, |
| 170 |
bit_depth: 10, |
| 171 |
}, |
| 172 |
Pixel::YUV422P10 | Pixel::YUV422P10BE | Pixel::YUV422P10LE => ColorFormat { |
| 173 |
pixel_format: PixelFormat::YUV422, |
| 174 |
bit_depth: 10, |
| 175 |
}, |
| 176 |
Pixel::YUV444P10 | Pixel::YUV444P10BE | Pixel::YUV444P10LE => ColorFormat { |
| 177 |
pixel_format: PixelFormat::YUV444, |
| 178 |
bit_depth: 10, |
| 179 |
}, |
| 180 |
Pixel::YUV420P12 | Pixel::YUV420P12BE | Pixel::YUV420P12LE => ColorFormat { |
| 181 |
pixel_format: PixelFormat::YUV420, |
| 182 |
bit_depth: 12, |
| 183 |
}, |
| 184 |
Pixel::YUV422P12 | Pixel::YUV422P12BE | Pixel::YUV422P12LE => ColorFormat { |
| 185 |
pixel_format: PixelFormat::YUV422, |
| 186 |
bit_depth: 12, |
| 187 |
}, |
| 188 |
Pixel::YUV444P12 | Pixel::YUV444P12BE | Pixel::YUV444P12LE => ColorFormat { |
| 189 |
pixel_format: PixelFormat::YUV444, |
| 190 |
bit_depth: 12, |
| 191 |
}, |
| 192 |
Pixel::YUV420P14 | Pixel::YUV420P14BE | Pixel::YUV420P14LE => ColorFormat { |
| 193 |
pixel_format: PixelFormat::YUV420, |
| 194 |
bit_depth: 14, |
| 195 |
}, |
| 196 |
Pixel::YUV422P14 | Pixel::YUV422P14BE | Pixel::YUV422P14LE => ColorFormat { |
| 197 |
pixel_format: PixelFormat::YUV422, |
| 198 |
bit_depth: 14, |
| 199 |
}, |
| 200 |
Pixel::YUV444P14 | Pixel::YUV444P14BE | Pixel::YUV444P14LE => ColorFormat { |
| 201 |
pixel_format: PixelFormat::YUV444, |
| 202 |
bit_depth: 14, |
| 203 |
}, |
| 204 |
Pixel::YUV420P16 | Pixel::YUV420P16LE | Pixel::YUV420P16BE => ColorFormat { |
| 205 |
pixel_format: PixelFormat::YUV420, |
| 206 |
bit_depth: 16, |
| 207 |
}, |
| 208 |
Pixel::YUV422P16 | Pixel::YUV422P16LE | Pixel::YUV422P16BE => ColorFormat { |
| 209 |
pixel_format: PixelFormat::YUV422, |
| 210 |
bit_depth: 16, |
| 211 |
}, |
| 212 |
Pixel::YUV444P16 | Pixel::YUV444P16LE | Pixel::YUV444P16BE => ColorFormat { |
| 213 |
pixel_format: PixelFormat::YUV444, |
| 214 |
bit_depth: 16, |
| 215 |
}, |
| 216 |
Pixel::GRAY8 => ColorFormat { |
| 217 |
pixel_format: PixelFormat::YUV400, |
| 218 |
bit_depth: 8, |
| 219 |
}, |
| 220 |
Pixel::GRAY16BE | Pixel::GRAY16LE | Pixel::GRAY16 => ColorFormat { |
| 221 |
pixel_format: PixelFormat::YUV400, |
| 222 |
bit_depth: 16, |
| 223 |
}, |
| 224 |
_ => bail!("Only YUV clips are supported"), |
| 225 |
}) |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
fn ffmpeg_frame_to_v_frame(ff_frame: &mut ffmpeg_next::util::frame::Frame) -> Frame<u8> { |
| 230 |
// SAFETY: We know this pointer is initialized |
| 231 |
let in_frame = unsafe { ffmpeg_next::util::frame::video::Video::wrap(ff_frame.as_mut_ptr()) }; |
| 232 |
let mut planes = [ |
| 233 |
Plane::new(0, 0, 0, 0, 0, 0), |
| 234 |
Plane::new(0, 0, 0, 0, 0, 0), |
| 235 |
Plane::new(0, 0, 0, 0, 0, 0), |
| 236 |
]; |
| 237 |
let format = |
| 238 |
ColorFormat::try_from(in_frame.format()).expect("Color format has already been checked"); |
| 239 |
for p in 0..in_frame.planes() { |
| 240 |
let mut plane: Plane<u8> = Plane::new( |
| 241 |
in_frame.plane_width(p) as usize, |
| 242 |
in_frame.plane_height(p) as usize, |
| 243 |
if p > 0 { |
| 244 |
format.pixel_format.subsampling().0 |
| 245 |
} else { |
| 246 |
0 |
| 247 |
}, |
| 248 |
if p > 0 { |
| 249 |
format.pixel_format.subsampling().1 |
| 250 |
} else { |
| 251 |
0 |
| 252 |
}, |
| 253 |
0usize, |
| 254 |
0usize, |
| 255 |
); |
| 256 |
|
| 257 |
let in_data = in_frame.data(p); |
| 258 |
|
| 259 |
if format.bit_depth == 8 { |
| 260 |
assert!(plane.data.len() == in_data.len()); |
| 261 |
plane.data_origin_mut().copy_from_slice(in_data); |
| 262 |
} else { |
| 263 |
assert!(plane.data.len() * 2 == in_data.len()); |
| 264 |
in_data |
| 265 |
.chunks_exact(2) |
| 266 |
.zip(plane.data_origin_mut().iter_mut()) |
| 267 |
.for_each(|(i, o)| { |
| 268 |
let i = u16::from_le_bytes([i[0], i[1]]); |
| 269 |
*o = (i >> (format.bit_depth - 8)) as u8; |
| 270 |
}); |
| 271 |
} |
| 272 |
|
| 273 |
planes[p] = plane; |
| 274 |
} |
| 275 |
Frame { planes } |
| 276 |
} |