| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | use ec_core::{Error, Result}; |
| 15 | |
| 16 | use crate::encoder::{Application, Encoder}; |
| 17 | |
| 18 | |
| 19 | #[derive(Debug)] |
| 20 | pub struct MultistreamEncoder { |
| 21 | streams: Vec<Encoder>, |
| 22 | coupled: usize, |
| 23 | mapping: Vec<u8>, |
| 24 | channels: usize, |
| 25 | |
| 26 | buf: Vec<f32>, |
| 27 | } |
| 28 | |
| 29 | impl MultistreamEncoder { |
| 30 | |
| 31 | |
| 32 | |
| 33 | pub fn look_ahead(&self, frame_size: usize) -> usize { |
| 34 | self.streams[0].look_ahead(frame_size) |
| 35 | } |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | pub fn new( |
| 42 | sample_rate: u32, |
| 43 | streams: usize, |
| 44 | coupled: usize, |
| 45 | mapping: &[u8], |
| 46 | application: Application, |
| 47 | ) -> Result<MultistreamEncoder> { |
| 48 | if streams == 0 || streams > 255 || coupled > streams { |
| 49 | return Err(Error::corrupt(format!( |
| 50 | "opus multistream: {streams} streams of which {coupled} coupled" |
| 51 | ))); |
| 52 | } |
| 53 | let coded = coupled + streams; |
| 54 | if mapping.is_empty() || mapping.len() > 255 { |
| 55 | return Err(Error::corrupt(format!( |
| 56 | "opus multistream: {} input channels", |
| 57 | mapping.len() |
| 58 | ))); |
| 59 | } |
| 60 | if let Some(&bad) = mapping.iter().find(|&&m| m != 255 && m as usize >= coded) { |
| 61 | return Err(Error::corrupt(format!( |
| 62 | "opus multistream: mapping entry {bad} names a channel of {coded}" |
| 63 | ))); |
| 64 | } |
| 65 | let mut encoders = Vec::with_capacity(streams); |
| 66 | for s in 0..streams { |
| 67 | let ch = if s < coupled { 2 } else { 1 }; |
| 68 | encoders.push(Encoder::new(sample_rate, ch, application)?); |
| 69 | } |
| 70 | Ok(MultistreamEncoder { |
| 71 | streams: encoders, |
| 72 | coupled, |
| 73 | mapping: mapping.to_vec(), |
| 74 | channels: mapping.len(), |
| 75 | buf: vec![0.0; 2 * 2880], |
| 76 | }) |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | pub fn surround( |
| 84 | sample_rate: u32, |
| 85 | channels: usize, |
| 86 | application: Application, |
| 87 | ) -> Result<MultistreamEncoder> { |
| 88 | let (_family, streams, coupled, table) = |
| 89 | crate::ogg::default_mapping(channels).ok_or_else(|| { |
| 90 | Error::unsupported( |
| 91 | format!("{channels}-channel Opus stream"), |
| 92 | "RFC 7845 maps 1 to 8 channels", |
| 93 | ) |
| 94 | })?; |
| 95 | MultistreamEncoder::new( |
| 96 | sample_rate, |
| 97 | streams as usize, |
| 98 | coupled as usize, |
| 99 | &table, |
| 100 | application, |
| 101 | ) |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | pub fn surround_5_1(sample_rate: u32) -> Result<MultistreamEncoder> { |
| 107 | MultistreamEncoder::surround(sample_rate, 6, Application::Audio) |
| 108 | } |
| 109 | |
| 110 | |
| 111 | pub fn channels(&self) -> usize { |
| 112 | self.channels |
| 113 | } |
| 114 | |
| 115 | |
| 116 | pub fn layout(&self) -> (usize, usize, &[u8]) { |
| 117 | (self.streams.len(), self.coupled, &self.mapping) |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
| 122 | pub fn set_bitrate(&mut self, total_bps: u32) { |
| 123 | let mono = self.streams.len() - self.coupled; |
| 124 | let shares = (2 * self.coupled + mono) as u32; |
| 125 | let share = total_bps / shares.max(1); |
| 126 | for (s, enc) in self.streams.iter_mut().enumerate() { |
| 127 | let w = if s < self.coupled { 2 } else { 1 }; |
| 128 | enc.set_bitrate(share * w); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | |
| 133 | pub fn set_vbr_constrained(&mut self, vbr: bool) { |
| 134 | for enc in self.streams.iter_mut() { |
| 135 | enc.set_vbr_constrained(vbr); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | |
| 140 | pub fn set_inband_fec(&mut self, on: bool) { |
| 141 | for enc in self.streams.iter_mut() { |
| 142 | enc.set_inband_fec(on); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
| 147 | pub fn set_packet_loss_perc(&mut self, perc: u8) { |
| 148 | for enc in self.streams.iter_mut() { |
| 149 | enc.set_packet_loss_perc(perc); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | |
| 154 | pub fn reset(&mut self) { |
| 155 | for enc in self.streams.iter_mut() { |
| 156 | enc.reset(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | |
| 161 | |
| 162 | pub fn final_range(&self) -> u32 { |
| 163 | self.streams.iter().fold(0, |acc, s| acc ^ s.final_range()) |
| 164 | } |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | pub fn encode_float( |
| 170 | &mut self, |
| 171 | pcm: &[f32], |
| 172 | frame_size: usize, |
| 173 | out: &mut [u8], |
| 174 | ) -> Result<usize> { |
| 175 | if pcm.len() < frame_size * self.channels { |
| 176 | return Err(Error::corrupt(format!( |
| 177 | "opus multistream encode: {} samples for {} channels of {frame_size}", |
| 178 | pcm.len(), |
| 179 | self.channels |
| 180 | ))); |
| 181 | } |
| 182 | let nb_streams = self.streams.len(); |
| 183 | let mut pos = 0usize; |
| 184 | for s in 0..nb_streams { |
| 185 | let stream_channels = if s < self.coupled { 2 } else { 1 }; |
| 186 | |
| 187 | |
| 188 | self.buf[..frame_size * stream_channels].fill(0.0); |
| 189 | for off in 0..stream_channels { |
| 190 | let coded = if s < self.coupled { |
| 191 | 2 * s + off |
| 192 | } else { |
| 193 | self.coupled + s |
| 194 | }; |
| 195 | if let Some(c) = self.mapping.iter().position(|&m| m as usize == coded) { |
| 196 | for i in 0..frame_size { |
| 197 | self.buf[i * stream_channels + off] = pcm[i * self.channels + c]; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | let last = s == nb_streams - 1; |
| 202 | let (head, buf) = (&mut out[pos..], &self.buf[..frame_size * stream_channels]); |
| 203 | |
| 204 | |
| 205 | let enc = &mut self.streams[s]; |
| 206 | let n = if last { |
| 207 | enc.encode_float(buf, frame_size, head)? |
| 208 | } else { |
| 209 | enc.encode_self_delimited(buf, frame_size, head)? |
| 210 | }; |
| 211 | pos += n; |
| 212 | } |
| 213 | Ok(pos) |
| 214 | } |
| 215 | } |