Debian Codegraph @

.cargo/vendor/av-scenechange/src/ffmpeg.rs

raw
1 extern crate ffmpeg_the_third as ffmpeg;
2
3 use std::path::Path;
4
5 use anyhow::bail;
6 use ffmpeg::{
7 codec::{decoder, packet},
8 format,
9 format::context,
10 frame,
11 media::Type,
12 };
13 use ffmpeg_the_third::threading;
14 use num_rational::Rational32;
15 use v_frame::{
16 frame::Frame,
17 pixel::{ChromaSampling, Pixel},
18 };
19
20 use crate::decoder::VideoDetails;
21
22 /// An interface that is used for decoding a video stream using ffmpeg
23 ///
24 /// There have been desync issue reported with this decoder
25 /// on some video files. Use at your own risk!
26 pub struct FfmpegDecoder {
27 input_ctx: context::Input,
28 decoder: decoder::Video,
29 pub video_details: VideoDetails,
30 frameno: usize,
31 stream_index: usize,
32 end_of_stream: bool,
33 eof_sent: bool,
34 }
35
36 impl FfmpegDecoder {
37 /// Initialize a new ffmpeg decoder for a given input file
38 ///
39 /// # Errors
40 ///
41 /// - If ffmpeg is not available or not working on the system
42 /// - If the source contains no video
43 /// - If the source has an unsupported pixel format
44 #[inline]
45 pub fn new<P: AsRef<Path>>(input: P) -> anyhow::Result<Self> {
46 ffmpeg::init()?;
47
48 let input_ctx = format::input(&input)?;
49 let input = input_ctx
50 .streams()
51 .best(Type::Video)
52 .ok_or_else(|| anyhow::anyhow!("Could not find video stream"))?;
53 let stream_index = input.index();
54 let mut context = ffmpeg::codec::context::Context::from_parameters(input.parameters())?;
55 context.set_threading(threading::Config::kind(threading::Type::Frame));
56 let mut decoder = context.decoder().video()?;
57 decoder.set_parameters(input.parameters())?;
58
59 let frame_rate = input.avg_frame_rate();
60 Ok(Self {
61 video_details: VideoDetails {
62 width: decoder.width() as usize,
63 height: decoder.height() as usize,
64 bit_depth: match decoder.format() {
65 format::pixel::Pixel::YUV420P
66 | format::pixel::Pixel::YUV422P
67 | format::pixel::Pixel::YUV444P
68 | format::pixel::Pixel::YUVJ420P
69 | format::pixel::Pixel::YUVJ422P
70 | format::pixel::Pixel::YUVJ444P => 8,
71 format::pixel::Pixel::YUV420P10LE
72 | format::pixel::Pixel::YUV422P10LE
73 | format::pixel::Pixel::YUV444P10LE => 10,
74 format::pixel::Pixel::YUV420P12LE
75 | format::pixel::Pixel::YUV422P12LE
76 | format::pixel::Pixel::YUV444P12LE => 12,
77 _ => {
78 bail!("Unsupported pixel format {:?}", decoder.format());
79 }
80 },
81 chroma_sampling: match decoder.format() {
82 format::pixel::Pixel::YUV420P
83 | format::pixel::Pixel::YUVJ420P
84 | format::pixel::Pixel::YUV420P10LE
85 | format::pixel::Pixel::YUV420P12LE => ChromaSampling::Cs420,
86 format::pixel::Pixel::YUV422P
87 | format::pixel::Pixel::YUVJ422P
88 | format::pixel::Pixel::YUV422P10LE
89 | format::pixel::Pixel::YUV422P12LE => ChromaSampling::Cs422,
90 format::pixel::Pixel::YUV444P
91 | format::pixel::Pixel::YUVJ444P
92 | format::pixel::Pixel::YUV444P10LE
93 | format::pixel::Pixel::YUV444P12LE => ChromaSampling::Cs444,
94 _ => {
95 bail!("Unsupported pixel format {:?}", decoder.format());
96 }
97 },
98 time_base: Rational32::new(frame_rate.denominator(), frame_rate.numerator()),
99 },
100 decoder,
101 input_ctx,
102 frameno: 0,
103 stream_index,
104 end_of_stream: false,
105 eof_sent: false,
106 })
107 }
108
109 fn decode_frame<T: Pixel>(&self, decoded: &frame::Video) -> Frame<T> {
110 const SB_SIZE_LOG2: usize = 6;
111 const SB_SIZE: usize = 1 << SB_SIZE_LOG2;
112 const SUBPEL_FILTER_SIZE: usize = 8;
113 const FRAME_MARGIN: usize = 16 + SUBPEL_FILTER_SIZE;
114 const LUMA_PADDING: usize = SB_SIZE + FRAME_MARGIN;
115
116 let mut f: Frame<T> = Frame::new_with_padding(
117 self.video_details.width,
118 self.video_details.height,
119 self.video_details.chroma_sampling,
120 LUMA_PADDING,
121 );
122 let width = self.video_details.width;
123 let height = self.video_details.height;
124 let bit_depth = self.video_details.bit_depth;
125 let bytes = if bit_depth > 8 { 2 } else { 1 };
126 let (chroma_width, _) = self
127 .video_details
128 .chroma_sampling
129 .get_chroma_dimensions(width, height);
130 f.planes[0].copy_from_raw_u8(decoded.data(0), width * bytes, bytes);
131 f.planes[1].copy_from_raw_u8(decoded.data(1), chroma_width * bytes, bytes);
132 f.planes[2].copy_from_raw_u8(decoded.data(2), chroma_width * bytes, bytes);
133 f
134 }
135
136 /// # Errors
137 ///
138 /// - If there are no frames remaining
139 #[inline]
140 pub fn read_video_frame<T: Pixel>(&mut self) -> anyhow::Result<Frame<T>> {
141 // For some reason there's a crap ton of work needed to get ffmpeg to do
142 // something simple, because each codec has it's own stupid way of doing
143 // things and they don't all decode the same way.
144 //
145 // Maybe ffmpeg could have made a simple, singular interface that does this for
146 // us, but noooooo.
147 //
148 // Reference: https://ffmpeg.org/doxygen/trunk/api-h264-test_8c_source.html#l00110
149 loop {
150 // This iterator is actually really stupid... it doesn't reset itself after each
151 // `new`. But that solves our lifetime hell issues, ironically.
152 let packet = self
153 .input_ctx
154 .packets()
155 .next()
156 .and_then(Result::ok)
157 .map(|(_, packet)| packet);
158
159 let mut packet = if let Some(packet) = packet {
160 packet
161 } else {
162 self.end_of_stream = true;
163 packet::Packet::empty()
164 };
165
166 if self.end_of_stream && !self.eof_sent {
167 let _ = self.decoder.send_eof();
168 self.eof_sent = true;
169 }
170
171 if self.end_of_stream || packet.stream() == self.stream_index {
172 let mut decoded = frame::Video::new(
173 self.decoder.format(),
174 self.video_details.width as u32,
175 self.video_details.height as u32,
176 );
177 packet.set_pts(Some(self.frameno as i64));
178 packet.set_dts(Some(self.frameno as i64));
179
180 if !self.end_of_stream {
181 let _ = self.decoder.send_packet(&packet);
182 }
183
184 if self.decoder.receive_frame(&mut decoded).is_ok() {
185 let f = self.decode_frame(&decoded);
186 self.frameno += 1;
187 return Ok(f);
188 } else if self.end_of_stream {
189 bail!("No frames left");
190 }
191 }
192 }
193 }
194 }