@@ -36,7 +36,7 @@ fn init_filters(
3636 audio_stream_index : usize ,
3737 filters_descr : & CStr ,
3838) -> Result < FilterState > {
39- let mut graph = AVFilterGraph :: new ( ) ;
39+ let graph = AVFilterGraph :: new ( ) ;
4040 let abuffer = AVFilter :: get_by_name ( c"abuffer" ) . context ( "Cannot find abuffer" ) ?;
4141 let abuffersink = AVFilter :: get_by_name ( c"abuffersink" ) . context ( "Cannot find abuffersink" ) ?;
4242
@@ -103,18 +103,12 @@ fn init_filters(
103103 . parse_ptr ( filters_descr, Some ( inputs) , Some ( outputs) )
104104 . context ( "avfilter_graph_parse_ptr failed" ) ?;
105105 graph. config ( ) . context ( "avfilter_graph_config failed" ) ?;
106- }
107106
108- // Print output summary like the C example (after graph is configured)
109- {
110- let sink = graph
111- . get_filter ( c"out" )
112- . ok_or_else ( || anyhow ! ( "buffersink not found" ) ) ?;
113- let out_srate = sink. get_sample_rate ( ) ;
114- let out_fmt = get_sample_fmt_name ( sink. get_format ( ) )
107+ let out_srate = buffersink_ctx. get_sample_rate ( ) ;
108+ let out_fmt = get_sample_fmt_name ( buffersink_ctx. get_format ( ) )
115109 . and_then ( |s| s. to_str ( ) . ok ( ) )
116110 . unwrap_or ( "?" ) ;
117- let ch_layout = sink . get_ch_layout ( ) ;
111+ let ch_layout = buffersink_ctx . get_ch_layout ( ) ;
118112 let ch_desc = ch_layout
119113 . describe ( )
120114 . ok ( )
@@ -142,13 +136,22 @@ fn print_and_write_frame(mut out: &File, frame: &AVFrame) -> Result<()> {
142136
143137fn decode_filter_audio ( input : & CStr , out_path : & str ) -> Result < ( ) > {
144138 let ( mut fmt, mut dec_ctx, audio_idx) = open_input_file ( input) ?;
145- let mut filt = init_filters ( & fmt, & mut dec_ctx, audio_idx, FILTER_DESCR ) ?;
139+ let filt = init_filters ( & fmt, & mut dec_ctx, audio_idx, FILTER_DESCR ) ?;
146140
147141 if let Some ( dir) = Path :: new ( out_path) . parent ( ) {
148142 std:: fs:: create_dir_all ( dir) . ok ( ) ;
149143 }
150144 let out = File :: create ( out_path) . with_context ( || format ! ( "open {}" , out_path) ) ?;
151145
146+ let mut src = filt
147+ . graph
148+ . get_filter ( c"in" )
149+ . context ( "buffersrc not found" ) ?;
150+ let mut sink = filt
151+ . graph
152+ . get_filter ( c"out" )
153+ . context ( "buffersink not found" ) ?;
154+
152155 while let Some ( packet) = fmt. read_packet ( ) ? {
153156 if packet. stream_index == audio_idx as i32 {
154157 dec_ctx
@@ -159,21 +162,13 @@ fn decode_filter_audio(input: &CStr, out_path: &str) -> Result<()> {
159162 Ok ( frame) => {
160163 // push decoded frame into graph
161164 {
162- let mut src = filt
163- . graph
164- . get_filter ( c"in" )
165- . context ( "buffersrc not found" ) ?;
166165 src. buffersrc_add_frame (
167166 Some ( frame) ,
168167 Some ( ffi:: AV_BUFFERSRC_FLAG_KEEP_REF as i32 ) ,
169168 )
170169 . context ( "Error while feeding the audio filtergraph" ) ?;
171170 }
172171 // pull all available filtered frames
173- let mut sink = filt
174- . graph
175- . get_filter ( c"out" )
176- . ok_or_else ( || anyhow ! ( "buffersink not found" ) ) ?;
177172 loop {
178173 match sink. buffersink_get_frame ( None ) {
179174 Ok ( f) => {
0 commit comments