xref: /xiu/library/container/flv/src/mpeg4_aac.rs (revision 13bac29a)
1 use {
2     super::errors::{MpegAacError, MpegErrorValue},
3     bytes::BytesMut,
4     bytesio::{
5         bits_reader::BitsReader, bits_writer::BitsWriter, bytes_reader::BytesReader,
6         bytes_writer::BytesWriter,
7     },
8 };
9 
10 const AAC_FREQUENCE_SIZE: usize = 13;
11 const AAC_FREQUENCE: [u32; AAC_FREQUENCE_SIZE] = [
12     96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350,
13 ];
14 
15 #[derive(Debug, Clone)]
16 pub struct Mpeg4Aac {
17     pub profile: u8,
18     pub sampling_frequency_index: u8,
19     pub channel_configuration: u8,
20 
21     pub sampling_frequency: u32,
22     pub channels: u8,
23     pub sbr: usize,
24     pub ps: usize,
25     pub pce: BytesMut,
26     pub npce: usize,
27 }
28 
29 impl Default for Mpeg4Aac {
default() -> Self30     fn default() -> Self {
31         Self::new()
32     }
33 }
34 
35 impl Mpeg4Aac {
new() -> Self36     pub fn new() -> Self {
37         Self {
38             profile: 0,
39             sampling_frequency_index: 0,
40             channel_configuration: 0,
41             sampling_frequency: 0,
42             channels: 0,
43             sbr: 0,
44             ps: 0,
45 
46             pce: BytesMut::new(),
47             npce: 0,
48         }
49     }
50 }
51 
52 pub struct Mpeg4AacProcessor {
53     pub bytes_reader: BytesReader,
54     pub bytes_writer: BytesWriter,
55     pub bits_reader: BitsReader,
56     pub mpeg4_aac: Mpeg4Aac,
57 }
58 
59 impl Default for Mpeg4AacProcessor {
default() -> Self60     fn default() -> Self {
61         Self::new()
62     }
63 }
64 //https://blog.csdn.net/coloriy/article/details/90511746
65 impl Mpeg4AacProcessor {
new() -> Self66     pub fn new() -> Self {
67         Self {
68             bytes_reader: BytesReader::new(BytesMut::new()),
69             bytes_writer: BytesWriter::new(),
70             bits_reader: BitsReader::new(BytesReader::new(BytesMut::new())),
71             mpeg4_aac: Mpeg4Aac::new(),
72         }
73     }
74 
extend_data(&mut self, data: BytesMut) -> &mut Self75     pub fn extend_data(&mut self, data: BytesMut) -> &mut Self {
76         self.bytes_reader.extend_from_slice(&data[..]);
77         self
78     }
79 
audio_specific_config_load(&mut self) -> Result<&mut Self, MpegAacError>80     pub fn audio_specific_config_load(&mut self) -> Result<&mut Self, MpegAacError> {
81         //11 88 56 E5
82         let byte_0 = self.bytes_reader.read_u8()?;
83         self.mpeg4_aac.profile = (byte_0 >> 3) & 0x1F;
84 
85         let byte_1 = self.bytes_reader.read_u8()?;
86         self.mpeg4_aac.sampling_frequency_index = ((byte_0 & 0x07) << 1) | ((byte_1 >> 7) & 0x01);
87         self.mpeg4_aac.channel_configuration = (byte_1 >> 3) & 0x0F;
88         self.mpeg4_aac.channels = self.mpeg4_aac.channel_configuration;
89         self.mpeg4_aac.sampling_frequency =
90             AAC_FREQUENCE[self.mpeg4_aac.sampling_frequency_index as usize];
91 
92         // log::info!("aac info: {:?}", self.mpeg4_aac);
93 
94         // if self.bytes_reader.len() > 2 {
95         //return self.audio_specific_config_load2();
96         // }
97 
98         // self.bytes_reader.read_u8()?;
99         // self.bytes_reader.read_u8()?;
100 
101         self.bytes_reader.extract_remaining_bytes();
102 
103         Ok(self)
104     }
105 
audio_specific_config_load2(&mut self) -> Result<(), MpegAacError>106     pub fn audio_specific_config_load2(&mut self) -> Result<(), MpegAacError> {
107         let remain_bytes = self.bytes_reader.extract_remaining_bytes();
108         // self.bits_reader.extend_from_bytesmut(remain_bytes);
109         self.bits_reader.extend_data(remain_bytes);
110 
111         self.mpeg4_aac.profile = self.get_audio_object_type()?;
112         self.mpeg4_aac.sampling_frequency_index = self.get_sampling_frequency()?;
113         self.mpeg4_aac.channel_configuration = self.bits_reader.read_n_bits(4)? as u8;
114 
115         let mut extension_audio_object_type: u8;
116         let mut extension_sampling_frequency_index: u8 = 0;
117         let mut extension_channel_configuration: u8 = 0;
118 
119         if self.mpeg4_aac.profile == 5 || self.mpeg4_aac.profile == 29 {
120             extension_audio_object_type = 5;
121             self.mpeg4_aac.sbr = 1;
122             {
123                 if self.mpeg4_aac.profile == 29 {
124                     self.mpeg4_aac.ps = 1;
125                 }
126                 extension_sampling_frequency_index = self.get_sampling_frequency()?;
127                 self.mpeg4_aac.profile = self.get_audio_object_type()?;
128 
129                 if self.mpeg4_aac.profile == 22 {
130                     extension_channel_configuration = self.bits_reader.read_n_bits(4)? as u8;
131                 }
132             }
133         } else {
134             extension_audio_object_type = 0;
135         }
136 
137         let ep_config: u64;
138 
139         match self.mpeg4_aac.profile {
140             1 | 2 | 3 | 4 | 5 | 6 | 7 | 17 | 19 | 20 | 21 | 22 | 23 => {
141                 self.ga_specific_config_load()?;
142             }
143             8 => {
144                 self.celp_specific_config_load()?;
145             }
146             _ => {}
147         }
148 
149         match self.mpeg4_aac.profile {
150             17 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 39 => {
151                 ep_config = self.bits_reader.read_n_bits(2)?;
152 
153                 match ep_config {
154                     2 | 3 => {
155                         return Err(MpegAacError {
156                             value: MpegErrorValue::ShouldNotComeHere,
157                         });
158                     }
159 
160                     _ => {}
161                 }
162             }
163             _ => {}
164         }
165 
166         let mut sync_extension_type: u64;
167 
168         if 5 != extension_audio_object_type && self.bits_reader.len() >= 16 {
169             sync_extension_type = self.bits_reader.read_n_bits(11)?;
170 
171             if 0x2B7 == sync_extension_type {
172                 extension_audio_object_type = self.get_audio_object_type()?;
173 
174                 match extension_audio_object_type {
175                     5 => {
176                         self.mpeg4_aac.sbr = self.bits_reader.read_n_bits(1)? as usize;
177                         if self.mpeg4_aac.sbr > 0 {
178                             extension_sampling_frequency_index = self.get_sampling_frequency()?;
179                             if self.bits_reader.len() >= 12 {
180                                 sync_extension_type = self.bits_reader.read_n_bits(11)?;
181                                 if 0x548 == sync_extension_type {
182                                     self.mpeg4_aac.ps = self.bits_reader.read_n_bits(1)? as usize;
183                                 }
184                             }
185                         }
186                     }
187                     22 => {
188                         self.mpeg4_aac.sbr = self.bits_reader.read_n_bits(1)? as usize;
189 
190                         if self.mpeg4_aac.sbr > 0 {
191                             extension_sampling_frequency_index = self.get_sampling_frequency()?;
192                         }
193 
194                         extension_channel_configuration = self.bits_reader.read_n_bits(4)? as u8;
195                     }
196 
197                     _ => {}
198                 }
199             }
200         }
201 
202         self.bits_reader.bits_aligment_8();
203 
204         log::trace!(
205             "remove warnings: {} {} {}",
206             extension_audio_object_type,
207             extension_sampling_frequency_index,
208             extension_channel_configuration
209         );
210 
211         Ok(())
212     }
213 
celp_specific_config_load(&mut self) -> Result<(), MpegAacError>214     pub fn celp_specific_config_load(&mut self) -> Result<(), MpegAacError> {
215         let excitation_mode: u64;
216 
217         if self.bits_reader.read_n_bits(1)? > 0 {
218             excitation_mode = self.bits_reader.read_n_bits(1)?;
219             self.bits_reader.read_n_bits(1)?;
220             self.bits_reader.read_n_bits(1)?;
221 
222             if excitation_mode == 1 {
223                 self.bits_reader.read_n_bits(3)?;
224             } else if excitation_mode == 0 {
225                 self.bits_reader.read_n_bits(5)?;
226                 self.bits_reader.read_n_bits(2)?;
227                 self.bits_reader.read_n_bits(1)?;
228             }
229         } else {
230             self.bits_reader.read_n_bits(1)?;
231             self.bits_reader.read_n_bits(2)?;
232         }
233 
234         // if self.bits_reader.read_n_bits(1)? > 0 {
235         //     self.bits_reader.read_n_bits(2)?;
236         // } else {
237         //     self.bits_reader.read_n_bits(2)?;
238         // }
239 
240         Ok(())
241     }
ga_specific_config_load(&mut self) -> Result<(), MpegAacError>242     pub fn ga_specific_config_load(&mut self) -> Result<(), MpegAacError> {
243         self.bits_reader.read_n_bits(1)?;
244 
245         if self.bits_reader.read_n_bits(1)? > 0 {
246             self.bits_reader.read_n_bits(14)?;
247         }
248         let extension_flag: u64 = self.bits_reader.read_n_bits(1)?;
249 
250         if 0 == self.mpeg4_aac.channel_configuration {
251             self.pce_load()?;
252         }
253 
254         if self.mpeg4_aac.profile == 6 || self.mpeg4_aac.profile == 20 {
255             self.bits_reader.read_n_bits(3)?;
256         }
257 
258         if extension_flag > 0 {
259             match self.mpeg4_aac.profile {
260                 22 => {
261                     self.bits_reader.read_n_bits(5)?;
262                     self.bits_reader.read_n_bits(11)?;
263                 }
264                 17 | 19 | 20 | 23 => {
265                     self.bits_reader.read_n_bits(1)?;
266                     self.bits_reader.read_n_bits(1)?;
267                     self.bits_reader.read_n_bits(1)?;
268                 }
269                 _ => {}
270             }
271 
272             self.bits_reader.read_n_bits(1)?;
273         }
274 
275         Ok(())
276     }
277 
mpeg4_bits_copy( &mut self, writer: &mut BitsWriter, read_len: usize, ) -> Result<u64, MpegAacError>278     fn mpeg4_bits_copy(
279         &mut self,
280         writer: &mut BitsWriter,
281         read_len: usize,
282     ) -> Result<u64, MpegAacError> {
283         let data = self.bits_reader.read_n_bits(read_len)?;
284         writer.write_n_bits(data, read_len)?;
285         Ok(data)
286     }
287 
pce_load(&mut self) -> Result<u8, MpegAacError>288     pub fn pce_load(&mut self) -> Result<u8, MpegAacError> {
289         let mut cpe: u64 = 0;
290         let mut tag: u64 = 0;
291 
292         let mut pce_bits_vec = BitsWriter::new(BytesWriter::new());
293         pce_bits_vec.write_bytes(self.mpeg4_aac.pce.clone())?;
294 
295         self.mpeg4_aac.channels = 0;
296 
297         let element_instance_tag: u64 = self.mpeg4_bits_copy(&mut pce_bits_vec, 4)?;
298         let object_type: u64 = self.mpeg4_bits_copy(&mut pce_bits_vec, 2)?;
299         let sampling_frequency_index: u64 = self.mpeg4_bits_copy(&mut pce_bits_vec, 4)?;
300         let num_front_channel_elements: u64 = self.mpeg4_bits_copy(&mut pce_bits_vec, 4)?;
301         let num_side_channel_elements: u64 = self.mpeg4_bits_copy(&mut pce_bits_vec, 4)?;
302         let num_back_channel_elements: u64 = self.mpeg4_bits_copy(&mut pce_bits_vec, 4)?;
303         let num_lfe_channel_elements: u64 = self.mpeg4_bits_copy(&mut pce_bits_vec, 2)?;
304         let num_assoc_data_elements: u64 = self.mpeg4_bits_copy(&mut pce_bits_vec, 3)?;
305         let num_valid_cc_elements: u64 = self.mpeg4_bits_copy(&mut pce_bits_vec, 4)?;
306 
307         for _ in 0..3 {
308             if self.mpeg4_bits_copy(&mut pce_bits_vec, 1)? > 0 {
309                 self.mpeg4_bits_copy(&mut pce_bits_vec, 4)?;
310             }
311         }
312 
313         for _ in 0..num_front_channel_elements {
314             cpe = self.mpeg4_bits_copy(&mut pce_bits_vec, 1)?;
315             tag = self.mpeg4_bits_copy(&mut pce_bits_vec, 4)?;
316 
317             if cpe > 0 || self.mpeg4_aac.ps > 0 {
318                 self.mpeg4_aac.channels += 2;
319             } else {
320                 self.mpeg4_aac.channels += 1;
321             }
322         }
323 
324         for _ in 0..num_side_channel_elements {
325             cpe = self.mpeg4_bits_copy(&mut pce_bits_vec, 1)?;
326             tag = self.mpeg4_bits_copy(&mut pce_bits_vec, 4)?;
327 
328             if cpe > 0 || self.mpeg4_aac.ps > 0 {
329                 self.mpeg4_aac.channels += 2;
330             } else {
331                 self.mpeg4_aac.channels += 1;
332             }
333         }
334 
335         for _ in 0..num_back_channel_elements {
336             cpe = self.mpeg4_bits_copy(&mut pce_bits_vec, 1)?;
337             tag = self.mpeg4_bits_copy(&mut pce_bits_vec, 4)?;
338 
339             if cpe > 0 || self.mpeg4_aac.ps > 0 {
340                 self.mpeg4_aac.channels += 2;
341             } else {
342                 self.mpeg4_aac.channels += 1;
343             }
344         }
345 
346         for _ in 0..num_lfe_channel_elements {
347             tag = self.mpeg4_bits_copy(&mut pce_bits_vec, 4)?;
348             self.mpeg4_aac.channels += 1;
349         }
350 
351         for _ in 0..num_assoc_data_elements {
352             tag = self.mpeg4_bits_copy(&mut pce_bits_vec, 4)?;
353         }
354 
355         for _ in 0..num_valid_cc_elements {
356             cpe = self.mpeg4_bits_copy(&mut pce_bits_vec, 1)?;
357             tag = self.mpeg4_bits_copy(&mut pce_bits_vec, 4)?;
358         }
359 
360         pce_bits_vec.bits_aligment_8()?;
361         self.bits_reader.bits_aligment_8();
362 
363         let comment_field_bytes: u64 = self.mpeg4_bits_copy(&mut pce_bits_vec, 8)?;
364 
365         for _ in 0..comment_field_bytes {
366             self.mpeg4_bits_copy(&mut pce_bits_vec, 8)?;
367         }
368 
369         let rv = (pce_bits_vec.len() + 7) / 8;
370 
371         log::trace!(
372             "remove warnings: {} {} {} {} {}",
373             tag,
374             element_instance_tag,
375             object_type,
376             sampling_frequency_index,
377             cpe
378         );
379 
380         Ok(rv as u8)
381     }
382 
get_audio_object_type(&mut self) -> Result<u8, MpegAacError>383     pub fn get_audio_object_type(&mut self) -> Result<u8, MpegAacError> {
384         let mut audio_object_type: u64;
385 
386         audio_object_type = self.bits_reader.read_n_bits(5)?;
387         if 31 == audio_object_type {
388             audio_object_type = 32 + self.bits_reader.read_n_bits(6)?;
389         }
390 
391         Ok(audio_object_type as u8)
392     }
393 
get_sampling_frequency(&mut self) -> Result<u8, MpegAacError>394     pub fn get_sampling_frequency(&mut self) -> Result<u8, MpegAacError> {
395         let mut sampling_frequency_index: u64;
396 
397         sampling_frequency_index = self.bits_reader.read_n_bits(4)?;
398         if sampling_frequency_index == 0x0F {
399             sampling_frequency_index = self.bits_reader.read_n_bits(24)?;
400         }
401 
402         Ok(sampling_frequency_index as u8)
403     }
404 
adts_save(&mut self) -> Result<(), MpegAacError>405     pub fn adts_save(&mut self) -> Result<(), MpegAacError> {
406         let id = 0; // 0-MPEG4/1-MPEG2
407         let len = (self.bytes_reader.len() + 7) as u32;
408         self.bytes_writer.write_u8(0xFF)?; //0
409         self.bytes_writer.write_u8(
410             0xF0 /* 12-syncword */ | (id << 3)/*1-ID*/| 0x01, /*1-protection_absent*/
411         )?; //1
412 
413         let profile = self.mpeg4_aac.profile;
414         let sampling_frequency_index = self.mpeg4_aac.sampling_frequency_index;
415         let channel_configuration = self.mpeg4_aac.channel_configuration;
416         self.bytes_writer.write_u8(
417             ((profile - 1) << 6)
418                 | ((sampling_frequency_index & 0x0F) << 2)
419                 | ((channel_configuration >> 2) & 0x01),
420         )?; //2
421 
422         self.bytes_writer
423             .write_u8(((channel_configuration & 0x03) << 6) | ((len >> 11) as u8 & 0x03))?; //3
424         self.bytes_writer.write_u8((len >> 3) as u8)?; //4
425         self.bytes_writer
426             .write_u8((((len & 0x07) as u8) << 5) | 0x1F)?; //5
427         self.bytes_writer.write_u8(0xFC)?; //6
428 
429         self.bytes_writer
430             .write(&self.bytes_reader.extract_remaining_bytes()[..])?;
431 
432         Ok(())
433     }
434 }
435