Skip to content

Commit f20b638

Browse files
authored
feat(avfilter): relax get_filter method to use immutable reference (#243)
* feat(avfilter): relax get_filter method to use immutable reference * refactor(tests): simplify filter initialization and frame processing in decode_filter_audio example
1 parent f224ea5 commit f20b638

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

src/avfilter/avfilter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,9 @@ impl AVFilterGraph {
325325
}
326326

327327
/// Get a filter instance identified by instance name from graph.
328-
pub fn get_filter(&mut self, name: &CStr) -> Option<AVFilterContextMut<'_>> {
328+
pub fn get_filter(&self, name: &CStr) -> Option<AVFilterContextMut<'_>> {
329329
unsafe {
330-
ffi::avfilter_graph_get_filter(self.as_mut_ptr(), name.as_ptr())
330+
ffi::avfilter_graph_get_filter(self.as_ptr() as *mut _, name.as_ptr())
331331
.upgrade()
332332
.map(|raw| AVFilterContextMut::from_raw(raw))
333333
}

tests/ffmpeg_examples/decode_filter_audio.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

143137
fn 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

Comments
 (0)