1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
use std::ffi::{CStr, CString}; use std::ptr; use std::str::from_utf8_unchecked; use super::{Context, Filter}; use ffi::*; use libc::c_int; use Error; pub struct Graph { ptr: *mut AVFilterGraph, } unsafe impl Send for Graph {} unsafe impl Sync for Graph {} impl Graph { pub unsafe fn wrap(ptr: *mut AVFilterGraph) -> Self { Graph { ptr } } pub unsafe fn as_ptr(&self) -> *const AVFilterGraph { self.ptr as *const _ } pub unsafe fn as_mut_ptr(&mut self) -> *mut AVFilterGraph { self.ptr } } impl Graph { pub fn new() -> Self { unsafe { let ptr = avfilter_graph_alloc(); if ptr.is_null() { panic!("out of memory"); } Graph::wrap(ptr) } } pub fn validate(&mut self) -> Result<(), Error> { unsafe { match avfilter_graph_config(self.as_mut_ptr(), ptr::null_mut()) { 0 => Ok(()), e => Err(Error::from(e)), } } } pub fn add<'a, 'b>( &'a mut self, filter: &Filter, name: &str, args: &str, ) -> Result<Context<'b>, Error> where 'a: 'b, { unsafe { let name = CString::new(name).unwrap(); let args = CString::new(args).unwrap(); let mut context = ptr::null_mut(); match avfilter_graph_create_filter( &mut context as *mut *mut AVFilterContext, filter.as_ptr(), name.as_ptr(), args.as_ptr(), ptr::null_mut(), self.as_mut_ptr(), ) { n if n >= 0 => Ok(Context::wrap(context)), e => Err(Error::from(e)), } } } pub fn get<'a, 'b>(&'b mut self, name: &str) -> Option<Context<'b>> where 'a: 'b, { unsafe { let name = CString::new(name).unwrap(); let ptr = avfilter_graph_get_filter(self.as_mut_ptr(), name.as_ptr()); if ptr.is_null() { None } else { Some(Context::wrap(ptr)) } } } pub fn dump(&self) -> String { unsafe { let ptr = avfilter_graph_dump(self.as_ptr() as *mut _, ptr::null()); let cstr = from_utf8_unchecked(CStr::from_ptr(ptr).to_bytes()); let string = cstr.to_owned(); av_free(ptr as *mut _); string } } pub fn input(&mut self, name: &str, pad: usize) -> Result<Parser, Error> { Parser::new(self).input(name, pad) } pub fn output(&mut self, name: &str, pad: usize) -> Result<Parser, Error> { Parser::new(self).output(name, pad) } pub fn parse(&mut self, spec: &str) -> Result<(), Error> { Parser::new(self).parse(spec) } } impl Drop for Graph { fn drop(&mut self) { unsafe { avfilter_graph_free(&mut self.as_mut_ptr()); } } } pub struct Parser<'a> { graph: &'a mut Graph, inputs: *mut AVFilterInOut, outputs: *mut AVFilterInOut, } impl<'a> Parser<'a> { pub fn new(graph: &mut Graph) -> Parser { Parser { graph, inputs: ptr::null_mut(), outputs: ptr::null_mut(), } } pub fn input(mut self, name: &str, pad: usize) -> Result<Self, Error> { unsafe { let mut context = self.graph.get(name).ok_or(Error::InvalidData)?; let input = avfilter_inout_alloc(); if input.is_null() { panic!("out of memory"); } let name = CString::new(name).unwrap(); (*input).name = av_strdup(name.as_ptr()); (*input).filter_ctx = context.as_mut_ptr(); (*input).pad_idx = pad as c_int; (*input).next = ptr::null_mut(); if self.inputs.is_null() { self.inputs = input; } else { (*self.inputs).next = input; } } Ok(self) } pub fn output(mut self, name: &str, pad: usize) -> Result<Self, Error> { unsafe { let mut context = self.graph.get(name).ok_or(Error::InvalidData)?; let output = avfilter_inout_alloc(); if output.is_null() { panic!("out of memory"); } let name = CString::new(name).unwrap(); (*output).name = av_strdup(name.as_ptr()); (*output).filter_ctx = context.as_mut_ptr(); (*output).pad_idx = pad as c_int; (*output).next = ptr::null_mut(); if self.outputs.is_null() { self.outputs = output; } else { (*self.outputs).next = output; } } Ok(self) } pub fn parse(mut self, spec: &str) -> Result<(), Error> { unsafe { let spec = CString::new(spec).unwrap(); let result = avfilter_graph_parse_ptr( self.graph.as_mut_ptr(), spec.as_ptr(), &mut self.inputs, &mut self.outputs, ptr::null_mut(), ); avfilter_inout_free(&mut self.inputs); avfilter_inout_free(&mut self.outputs); match result { n if n >= 0 => Ok(()), e => Err(Error::from(e)), } } } } impl Default for Graph { fn default() -> Self { Self::new() } }