|
| 1 | +use anyhow::{bail, Result}; |
| 2 | +use std::io::Read; |
| 3 | + |
| 4 | +use super::{HevcParser, IoFormat, IoProcessor}; |
| 5 | + |
| 6 | +/// Base HEVC stream processor |
| 7 | +pub struct HevcProcessor { |
| 8 | + opts: HevcProcessorOpts, |
| 9 | + |
| 10 | + format: IoFormat, |
| 11 | + parser: HevcParser, |
| 12 | + |
| 13 | + chunk_size: usize, |
| 14 | + |
| 15 | + main_buf: Vec<u8>, |
| 16 | + sec_buf: Vec<u8>, |
| 17 | + consumed: usize, |
| 18 | + |
| 19 | + chunk: Vec<u8>, |
| 20 | + end: Vec<u8>, |
| 21 | + offsets: Vec<usize>, |
| 22 | + |
| 23 | + last_buffered_frame: u64, |
| 24 | +} |
| 25 | + |
| 26 | +/// Options for the processor |
| 27 | +#[derive(Default)] |
| 28 | +pub struct HevcProcessorOpts { |
| 29 | + /// Buffer a frame when using `parse_nalus`. |
| 30 | + /// This stops the stream reading as soon as a full frame has been parsed. |
| 31 | + pub buffer_frame: bool, |
| 32 | +} |
| 33 | + |
| 34 | +impl HevcProcessor { |
| 35 | + /// Initialize a HEVC stream processor |
| 36 | + pub fn new(format: IoFormat, opts: HevcProcessorOpts, chunk_size: usize) -> Self { |
| 37 | + let sec_buf = if format == IoFormat::RawStdin { |
| 38 | + vec![0; 50_000] |
| 39 | + } else { |
| 40 | + Vec::new() |
| 41 | + }; |
| 42 | + |
| 43 | + Self { |
| 44 | + opts, |
| 45 | + format, |
| 46 | + parser: HevcParser::default(), |
| 47 | + |
| 48 | + chunk_size, |
| 49 | + main_buf: vec![0; chunk_size], |
| 50 | + sec_buf, |
| 51 | + consumed: 0, |
| 52 | + |
| 53 | + chunk: Vec::with_capacity(chunk_size), |
| 54 | + end: Vec::with_capacity(chunk_size), |
| 55 | + offsets: Vec::with_capacity(2048), |
| 56 | + |
| 57 | + last_buffered_frame: 0, |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// Fully parse the input stream |
| 62 | + pub fn process_io( |
| 63 | + &mut self, |
| 64 | + reader: &mut dyn Read, |
| 65 | + processor: &mut dyn IoProcessor, |
| 66 | + ) -> Result<()> { |
| 67 | + self.parse_nalus(reader, processor)?; |
| 68 | + |
| 69 | + self.parser.finish(); |
| 70 | + |
| 71 | + processor.finalize(&self.parser)?; |
| 72 | + |
| 73 | + Ok(()) |
| 74 | + } |
| 75 | + |
| 76 | + /// Parse NALUs from the stream |
| 77 | + /// Depending on the options, this either: |
| 78 | + /// - Loops the entire stream until EOF |
| 79 | + /// - Loops until a complete frame has been parsed |
| 80 | + /// In both cases, the processor callback is called when a NALU payload is ready. |
| 81 | + pub fn parse_nalus( |
| 82 | + &mut self, |
| 83 | + reader: &mut dyn Read, |
| 84 | + processor: &mut dyn IoProcessor, |
| 85 | + ) -> Result<()> { |
| 86 | + while let Ok(n) = reader.read(&mut self.main_buf) { |
| 87 | + let mut read_bytes = n; |
| 88 | + if read_bytes == 0 && self.end.is_empty() && self.chunk.is_empty() { |
| 89 | + break; |
| 90 | + } |
| 91 | + |
| 92 | + if self.format == IoFormat::RawStdin { |
| 93 | + self.chunk.extend_from_slice(&self.main_buf[..read_bytes]); |
| 94 | + |
| 95 | + loop { |
| 96 | + match reader.read(&mut self.sec_buf) { |
| 97 | + Ok(num) => { |
| 98 | + if num > 0 { |
| 99 | + read_bytes += num; |
| 100 | + |
| 101 | + self.chunk.extend_from_slice(&self.sec_buf[..num]); |
| 102 | + |
| 103 | + if read_bytes >= self.chunk_size { |
| 104 | + break; |
| 105 | + } |
| 106 | + } else { |
| 107 | + break; |
| 108 | + } |
| 109 | + } |
| 110 | + Err(e) => bail!("{:?}", e), |
| 111 | + } |
| 112 | + } |
| 113 | + } else if read_bytes < self.chunk_size { |
| 114 | + self.chunk.extend_from_slice(&self.main_buf[..read_bytes]); |
| 115 | + } else { |
| 116 | + self.chunk.extend_from_slice(&self.main_buf); |
| 117 | + } |
| 118 | + |
| 119 | + self.parser.get_offsets(&self.chunk, &mut self.offsets); |
| 120 | + |
| 121 | + if self.offsets.is_empty() { |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + let last = if read_bytes < self.chunk_size { |
| 126 | + *self.offsets.last().unwrap() |
| 127 | + } else { |
| 128 | + let last = self.offsets.pop().unwrap(); |
| 129 | + |
| 130 | + self.end.clear(); |
| 131 | + self.end.extend_from_slice(&self.chunk[last..]); |
| 132 | + |
| 133 | + last |
| 134 | + }; |
| 135 | + |
| 136 | + let nals = self |
| 137 | + .parser |
| 138 | + .split_nals(&self.chunk, &self.offsets, last, true)?; |
| 139 | + |
| 140 | + // Process NALUs |
| 141 | + processor.process_nals(&self.parser, &nals, &self.chunk)?; |
| 142 | + |
| 143 | + self.chunk.clear(); |
| 144 | + |
| 145 | + if !self.end.is_empty() { |
| 146 | + self.chunk.extend_from_slice(&self.end); |
| 147 | + self.end.clear() |
| 148 | + } |
| 149 | + |
| 150 | + self.consumed += read_bytes; |
| 151 | + |
| 152 | + if self.consumed >= 100_000_000 { |
| 153 | + processor.update_progress(1); |
| 154 | + self.consumed = 0; |
| 155 | + } |
| 156 | + |
| 157 | + if self.opts.buffer_frame { |
| 158 | + let next_frame = nals.iter().map(|nal| nal.decoded_frame_index).max(); |
| 159 | + |
| 160 | + if let Some(number) = next_frame { |
| 161 | + if number > self.last_buffered_frame { |
| 162 | + self.last_buffered_frame = number; |
| 163 | + |
| 164 | + // Stop reading |
| 165 | + break; |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + Ok(()) |
| 172 | + } |
| 173 | +} |
0 commit comments