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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
use crate::tools;
use ffmpeg_sys::AVOptionType::*;
use ffmpeg_sys::*;
use std::ffi::CString;
use std::fmt;
use std::ptr::null_mut;

#[derive(Debug, PartialEq)]
pub struct Filter {
  pub context: *mut AVFilterContext,
}

impl Filter {
  /// # Safety
  pub unsafe fn new(filter_graph: *mut AVFilterGraph, plugin_name: &str) -> Result<Self, String> {
    Filter::new_with_label(filter_graph, plugin_name, "")
  }

  /// # Safety
  pub unsafe fn new_with_label(
    filter_graph: *mut AVFilterGraph,
    plugin_name: &str,
    instance_name: &str,
  ) -> Result<Self, String> {
    let label = CString::new(plugin_name).unwrap();
    let filter = avfilter_get_by_name(label.as_ptr());
    if filter.is_null() {
      return Err(format!(
        "Could not find the {} filter",
        label.into_string().unwrap()
      ));
    }

    let context = if instance_name == "" {
      avfilter_graph_alloc_filter(filter_graph, filter, null_mut())
    } else {
      let i_name = CString::new(instance_name).unwrap();
      avfilter_graph_alloc_filter(filter_graph, filter, i_name.as_ptr())
    };

    if context.is_null() {
      return Err(format!(
        "Could not allocate the {} instance",
        label.into_string().unwrap()
      ));
    }

    Ok(Filter { context })
  }

  pub fn get_label(&self) -> String {
    unsafe {
      if (*self.context).name.is_null() {
        "".to_string()
      } else {
        tools::to_string((*self.context).name)
      }
    }
  }

  pub fn init(&self) -> Result<(), String> {
    unsafe {
      check_result!(avfilter_init_str(self.context, null_mut()));
    }
    Ok(())
  }
}

fn dump_option(
  filter: *mut AVFilterContext,
  class: *const AVClass,
  prev_opt: Option<*const AVOption>,
  f: &mut fmt::Formatter,
) -> Option<*const AVOption> {
  unsafe {
    let option = if let Some(po) = prev_opt {
      av_opt_next(class as *mut _, po)
    } else {
      (*class).option
    };

    if option.is_null() {
      return None;
    }

    let option_name = tools::to_string((*option).name);

    let option_help = "".to_owned();
    /*  if (*option).help.is_null() {
      "".to_owned()
    }
    else {
      "\n\t".to_owned() + tools::to_string((*option).help)
    };*/

    let option_unit = if (*option).unit.is_null()
      || (*option).type_ == AV_OPT_TYPE_CONST
      || (*option).type_ == AV_OPT_TYPE_FLAGS
    {
      "".to_owned()
    } else {
      " ".to_owned() + &tools::to_string((*option).unit)
    };

    let option_value = match (*option).type_ {
      AV_OPT_TYPE_BOOL => {
        let value = 0i64;
        let value_ptr: *const i64 = &value;
        av_opt_get_int(
          filter as *mut _,
          (*option).name,
          AV_OPT_SEARCH_CHILDREN,
          value_ptr as *mut _,
        );
        if value == 0 {
          "false".to_owned()
        } else {
          "true".to_owned()
        }
      }
      AV_OPT_TYPE_INT => {
        let value = 0i64;
        let value_ptr: *const i64 = &value;
        av_opt_get_int(
          filter as *mut _,
          (*option).name,
          AV_OPT_SEARCH_CHILDREN,
          value_ptr as *mut _,
        );
        format!("{}", value)
      }
      AV_OPT_TYPE_INT64 => {
        let value = 0i64;
        let value_ptr: *const i64 = &value;
        av_opt_get_int(
          filter as *mut _,
          (*option).name,
          AV_OPT_SEARCH_CHILDREN,
          value_ptr as *mut _,
        );
        format!("{}", value)
      }
      AV_OPT_TYPE_DOUBLE => {
        let value = 0f64;
        let value_ptr: *const f64 = &value;
        av_opt_get_double(
          filter as *mut _,
          (*option).name,
          AV_OPT_SEARCH_CHILDREN,
          value_ptr as *mut _,
        );
        format!("{}", value)
      }
      AV_OPT_TYPE_RATIONAL => {
        let rational = AVRational { num: 0, den: 0 };
        let value_ptr: *const _ = &rational;
        av_opt_get_q(
          filter as *mut _,
          (*option).name,
          AV_OPT_SEARCH_CHILDREN,
          value_ptr as *mut _,
        );
        format!("{}/{}", rational.num, rational.den)
      }
      // AV_OPT_TYPE_BINARY |
      AV_OPT_TYPE_STRING => {
        let data = av_malloc(512);
        let value_ptr: *const _ = &data;
        av_opt_get(
          filter as *mut _,
          (*option).name,
          AV_OPT_SEARCH_CHILDREN,
          value_ptr as *mut *mut _,
        );

        tools::to_string(data as *const i8)
      }
      AV_OPT_TYPE_SAMPLE_FMT => {
        let format = AVSampleFormat::AV_SAMPLE_FMT_NONE;
        let value_ptr: *const _ = &format;
        av_opt_get_sample_fmt(
          filter as *mut _,
          (*option).name,
          AV_OPT_SEARCH_CHILDREN,
          value_ptr as *mut _,
        );

        let sample_fmt = av_get_sample_fmt_name(format);
        tools::to_string(sample_fmt)
      }
      AV_OPT_TYPE_IMAGE_SIZE => {
        let width = 0i32;
        let height = 0i32;
        let width_ptr: *const _ = &width;
        let height_ptr: *const _ = &height;
        av_opt_get_image_size(
          filter as *mut _,
          (*option).name,
          AV_OPT_SEARCH_CHILDREN,
          width_ptr as *mut _,
          height_ptr as *mut _,
        );

        format!("{}x{}", width, height)
      }
      _ => {
        // println!("{:?} {:?}", option_name, (*option).type_ );
        "".to_owned()
      }
    };

    let min_max = if (*option).type_ == AV_OPT_TYPE_INT || (*option).type_ == AV_OPT_TYPE_INT64 {
      format!("(min: {}, max: {}) ", (*option).min, (*option).max)
    } else {
      "".to_owned()
    };

    let ret = writeln!(
      f,
      " | {} = {}{} {} {}",
      option_name, option_value, option_unit, min_max, option_help
    );

    if ret.is_err() {
      return None;
    }

    Some(option)
  }
}

fn dump_options(filter: *mut AVFilterContext, class: *const AVClass, f: &mut fmt::Formatter) {
  let mut next = dump_option(filter, class, None, f);
  if next.is_none() {
    return;
  }

  loop {
    next = dump_option(filter, class, next, f);
    if next == None {
      break;
    }
  }
}

fn dump_link(
  pad_name: *const i8,
  link: *mut AVFilterLink,
  mode: &str,
  is_input: bool,
  f: &mut fmt::Formatter,
) -> fmt::Result {
  unsafe {
    let input_name = tools::to_string(pad_name);
    if link.is_null() {
      writeln!(f, "{} {}: not connected", mode, input_name)?;

      return Ok(());
    }

    let context = if is_input { (*link).src } else { (*link).dst };

    let node_label = if (*context).name.is_null() {
      "".to_string()
    } else {
      tools::to_string((*context).name)
    };

    let node_name = tools::to_string((*(*context).filter).name);

    write!(
      f,
      "{} {}: {} ({}) ",
      mode, input_name, node_name, &node_label
    )?;
    let pad = if is_input {
      avfilter_pad_get_name((*link).srcpad, 0)
    } else {
      avfilter_pad_get_name((*link).dstpad, 0)
    };
    let pad_type = avfilter_pad_get_type((*link).srcpad, 0);

    let str_pad = tools::to_string(pad);
    writeln!(f, "[{} | {:?}]", str_pad, pad_type)?;
  }
  Ok(())
}

impl fmt::Display for Filter {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    unsafe {
      let filter = self.context;
      let filter_label = self.get_label();

      let filter_name = tools::to_string((*(*filter).filter).name);
      writeln!(f, "{} ({})", filter_name, filter_label)?;

      dump_options(filter, (*filter).av_class, f);
      if !(*(*filter).filter).priv_class.is_null() {
        dump_options(filter, (*(*filter).filter).priv_class, f);
      }

      let input_links = tools::from_buf_raw((*filter).inputs, (*filter).nb_inputs as usize);
      for (index, input_link) in input_links.iter().enumerate() {
        let name = avfilter_pad_get_name((*filter).input_pads, index as i32);
        dump_link(name, *input_link, ">-", true, f)?;
      }

      let output_links = tools::from_buf_raw((*filter).outputs, (*filter).nb_outputs as usize);
      for (index, output_link) in output_links.iter().enumerate() {
        let name = avfilter_pad_get_name((*filter).output_pads, index as i32);
        dump_link(name, *output_link, "->", false, f)?;
      }
    }
    Ok(())
  }
}