@@ -8,7 +8,10 @@ use ffmpeg::{
88 threading:: Config ,
99} ;
1010
11- use crate :: { AudioEncoder , audio:: buffered_resampler:: BufferedResampler } ;
11+ use crate :: {
12+ AudioEncoder ,
13+ audio:: { base:: AudioEncoderBase , buffered_resampler:: BufferedResampler } ,
14+ } ;
1215
1316#[ derive( thiserror:: Error , Debug ) ]
1417pub enum AACEncoderError {
@@ -18,14 +21,12 @@ pub enum AACEncoderError {
1821 CodecNotFound ,
1922 #[ error( "Sample rate not supported: {0}" ) ]
2023 RateNotSupported ( i32 ) ,
24+ #[ error( "Resampler: {0}" ) ]
25+ Resampler ( ffmpeg:: Error ) ,
2126}
2227
2328pub struct AACEncoder {
24- encoder : encoder:: Audio ,
25- packet : ffmpeg:: Packet ,
26- resampler : BufferedResampler ,
27- stream_index : usize ,
28- first_pts : Option < i64 > ,
29+ base : AudioEncoderBase ,
2930}
3031
3132impl AACEncoder {
@@ -71,20 +72,8 @@ impl AACEncoder {
7172 output_config. sample_format = Self :: SAMPLE_FORMAT ;
7273 output_config. sample_rate = rate as u32 ;
7374
74- let resampler = ffmpeg:: software:: resampler (
75- (
76- input_config. sample_format ,
77- input_config. channel_layout ( ) ,
78- input_config. sample_rate ,
79- ) ,
80- (
81- output_config. sample_format ,
82- output_config. channel_layout ( ) ,
83- output_config. sample_rate ,
84- ) ,
85- )
86- . unwrap ( ) ;
87- let resampler = BufferedResampler :: new ( resampler) ;
75+ let resampler = BufferedResampler :: new ( input_config, output_config)
76+ . map_err ( AACEncoderError :: Resampler ) ?;
8877
8978 encoder. set_bit_rate ( Self :: OUTPUT_BITRATE ) ;
9079 encoder. set_rate ( rate) ;
@@ -100,76 +89,30 @@ impl AACEncoder {
10089 output_stream. set_parameters ( & encoder) ;
10190
10291 Ok ( Self {
103- encoder,
104- stream_index,
105- packet : ffmpeg:: Packet :: empty ( ) ,
106- resampler,
107- first_pts : None ,
92+ base : AudioEncoderBase :: new ( encoder, resampler, stream_index) ,
10893 } )
10994 }
11095
11196 pub fn queue_frame (
11297 & mut self ,
113- mut frame : frame:: Audio ,
98+ frame : frame:: Audio ,
11499 timestamp : Duration ,
115100 output : & mut format:: context:: Output ,
116- ) {
117- if timestamp != Duration :: MAX {
118- let Some ( pts) = frame. pts ( ) else {
119- tracing:: error!( "Frame has no pts" ) ;
120- return ;
121- } ;
122-
123- let time_base = self . encoder . time_base ( ) ;
124- let rate = time_base. denominator ( ) as f64 / time_base. numerator ( ) as f64 ;
125- frame. set_pts ( Some ( ( timestamp. as_secs_f64 ( ) * rate) . round ( ) as i64 ) ) ;
126-
127- let first_pts = self . first_pts . get_or_insert ( pts) ;
128-
129- frame. set_pts ( Some ( pts - * first_pts) ) ;
130- }
131-
132- self . resampler . add_frame ( frame) ;
133-
134- let frame_size = self . encoder . frame_size ( ) as usize ;
135-
136- while let Some ( frame) = self . resampler . get_frame ( frame_size) {
137- self . encoder . send_frame ( & frame) . unwrap ( ) ;
138-
139- self . process_packets ( output) ;
140- }
141- }
142-
143- fn process_packets ( & mut self , output : & mut format:: context:: Output ) {
144- while self . encoder . receive_packet ( & mut self . packet ) . is_ok ( ) {
145- self . packet . set_stream ( self . stream_index ) ;
146- self . packet . rescale_ts (
147- self . encoder . time_base ( ) ,
148- output. stream ( self . stream_index ) . unwrap ( ) . time_base ( ) ,
149- ) ;
150- self . packet . write_interleaved ( output) . unwrap ( ) ;
151- }
101+ ) -> Result < ( ) , ffmpeg:: Error > {
102+ self . base . send_frame ( frame, timestamp, output)
152103 }
153104
154- pub fn finish ( & mut self , output : & mut format:: context:: Output ) {
155- while let Some ( frame) = self . resampler . flush ( self . encoder . frame_size ( ) as usize ) {
156- self . encoder . send_frame ( & frame) . unwrap ( ) ;
157-
158- self . process_packets ( output) ;
159- }
160-
161- self . encoder . send_eof ( ) . unwrap ( ) ;
162-
163- self . process_packets ( output) ;
105+ pub fn finish ( & mut self , output : & mut format:: context:: Output ) -> Result < ( ) , ffmpeg:: Error > {
106+ self . base . finish ( output)
164107 }
165108}
166109
167110impl AudioEncoder for AACEncoder {
168111 fn queue_frame ( & mut self , frame : frame:: Audio , output : & mut format:: context:: Output ) {
169- self . queue_frame ( frame, Duration :: MAX , output) ;
112+ let _ = self . queue_frame ( frame, Duration :: MAX , output) ;
170113 }
171114
172115 fn finish ( & mut self , output : & mut format:: context:: Output ) {
173- self . finish ( output) ;
116+ let _ = self . finish ( output) ;
174117 }
175118}
0 commit comments