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
use crate::audio_encoder::AudioEncoder; use crate::order::frame::FrameAddress; use crate::order::*; use crate::packet::Packet; use crate::subtitle_encoder::SubtitleEncoder; use crate::tools; use crate::video_encoder::VideoEncoder; use ffmpeg_sys::*; use std::collections::{BTreeMap, HashMap}; use std::ffi::CString; use std::ptr::null_mut; use std::ffi::c_void; #[derive(Debug)] pub struct FormatContext { pub filename: String, pub format_context: *mut AVFormatContext, streams: Vec<*mut AVStream>, frames: Vec<FrameAddress>, frame_index: usize, } impl FormatContext { pub fn new(filename: &str) -> Result<FormatContext, String> { Ok(FormatContext { filename: filename.to_string(), format_context: null_mut(), streams: vec![], frames: vec![], frame_index: 0, }) } pub fn set_frames_addresses(&mut self, frames: &[FrameAddress]) { self.frames = frames.to_vec(); } pub fn open_input(&mut self) -> Result<(), String> { unsafe { self.format_context = avformat_alloc_context(); let filename = CString::new(self.filename.to_owned()).unwrap(); if avformat_open_input( &mut self.format_context, filename.as_ptr(), null_mut(), null_mut(), ) < 0 { return Err(format!("Unable to open input file {:?}", self.filename)); } avformat_find_stream_info(self.format_context, null_mut()); } Ok(()) } pub fn close_input(&mut self) { unsafe { avformat_close_input(&mut self.format_context); } } pub fn open_output( &mut self, parameters: &HashMap<String, ParameterValue>, ) -> Result<(), String> { unsafe { let filename = CString::new(self.filename.to_owned()).unwrap(); if avformat_alloc_output_context2( &mut self.format_context, null_mut(), null_mut(), filename.as_ptr(), ) < 0 { return Err(format!("Unable to open output file {:?}", self.filename)); } set_parameters(self.format_context as *mut c_void, parameters)?; } Ok(()) } pub fn add_video_stream(&mut self, encoder: &VideoEncoder) -> Result<(), String> { unsafe { let av_stream = avformat_new_stream(self.format_context, null_mut()); if av_stream.is_null() { return Err("Unable to create new stream".to_owned()); } (*av_stream).id = ((*self.format_context).nb_streams - 1) as i32; (*av_stream).time_base = (*encoder.codec_context).time_base; avcodec_parameters_from_context((*av_stream).codecpar, encoder.codec_context); self.streams.push(av_stream); } Ok(()) } pub fn add_audio_stream(&mut self, encoder: &AudioEncoder) -> Result<(), String> { unsafe { let av_stream = avformat_new_stream(self.format_context, null_mut()); if av_stream.is_null() { return Err("Unable to create new stream".to_owned()); } (*av_stream).id = ((*self.format_context).nb_streams - 1) as i32; (*av_stream).time_base = (*encoder.codec_context).time_base; avcodec_parameters_from_context((*av_stream).codecpar, encoder.codec_context); self.streams.push(av_stream); } Ok(()) } pub fn add_subtitle_stream(&mut self, encoder: &SubtitleEncoder) -> Result<(), String> { unsafe { let av_stream = avformat_new_stream(self.format_context, null_mut()); if av_stream.is_null() { return Err("Unable to create new stream".to_owned()); } (*av_stream).id = ((*self.format_context).nb_streams - 1) as i32; (*av_stream).time_base = (*encoder.codec_context).time_base; avcodec_parameters_from_context((*av_stream).codecpar, encoder.codec_context); self.streams.push(av_stream); } Ok(()) } pub fn get_stream(&self, stream_index: isize) -> *mut AVStream { unsafe { *(*self.format_context).streams.offset(stream_index) } } pub fn get_nb_streams(&self) -> u32 { if !self.frames.is_empty() { return 1; } unsafe { (*self.format_context).nb_streams } } pub fn get_format_name(&self) -> String { unsafe { tools::to_string((*(*self.format_context).iformat).name) } } pub fn get_format_long_name(&self) -> String { unsafe { tools::to_string((*(*self.format_context).iformat).long_name) } } pub fn get_program_count(&self) -> u32 { unsafe { (*self.format_context).nb_programs } } pub fn get_start_time(&self) -> Option<f32> { unsafe { if (*self.format_context).start_time == AV_NOPTS_VALUE { None } else { Some((*self.format_context).start_time as f32 / AV_TIME_BASE as f32) } } } pub fn get_duration(&self) -> Option<f64> { unsafe { if (*self.format_context).duration == AV_NOPTS_VALUE { None } else { Some((*self.format_context).duration as f64 / f64::from(AV_TIME_BASE)) } } } pub fn get_bit_rate(&self) -> Option<i64> { unsafe { if (*self.format_context).bit_rate == AV_NOPTS_VALUE { None } else { Some((*self.format_context).bit_rate) } } } pub fn get_packet_size(&self) -> u32 { unsafe { (*self.format_context).packet_size } } pub fn get_stream_type(&self, stream_index: isize) -> AVMediaType { unsafe { (*(**(*self.format_context).streams.offset(stream_index)).codecpar).codec_type } } pub fn get_stream_type_name(&self, stream_index: isize) -> String { unsafe { tools::to_string(av_get_media_type_string(self.get_stream_type(stream_index))) } } pub fn get_codec_id(&self, stream_index: isize) -> AVCodecID { unsafe { (*(**(*self.format_context).streams.offset(stream_index)).codecpar).codec_id } } pub fn get_metadata(&self) -> BTreeMap<String, String> { unsafe { let mut tag = null_mut(); let key = CString::new("").unwrap(); let mut metadata = BTreeMap::new(); loop { tag = av_dict_get( (*self.format_context).metadata, key.as_ptr(), tag, AV_DICT_IGNORE_SUFFIX, ); if tag.is_null() { break; } let k = tools::to_string((*tag).key); let v = tools::to_string((*tag).value); metadata.insert(k.to_string(), v.to_string()); } metadata } } pub fn next_packet(&mut self) -> Result<Packet, String> { if !self.frames.is_empty() { if self.frame_index >= self.frames.len() as usize { return Err("End of data stream".to_string()); } let frame = &self.frames[self.frame_index]; unsafe { let filename = CString::new(self.filename.to_owned()).unwrap(); let mut avio_context: *mut AVIOContext = null_mut(); check_result!(avio_open( &mut avio_context, filename.as_ptr(), AVIO_FLAG_READ )); if avio_seek(avio_context, frame.offset as i64, 0) < 0 { println!("ERROR !"); }; let packet = av_packet_alloc(); check_result!(av_new_packet(packet, frame.size as i32)); check_result!(avio_read(avio_context, (*packet).data, (*packet).size)); check_result!(avio_close(avio_context)); self.frame_index += 1; return Ok(Packet { name: None, packet }); } } unsafe { let mut packet = av_packet_alloc(); av_init_packet(packet); if av_read_frame(self.format_context, packet) < 0 { av_packet_free(&mut packet); return Err("Unable to read next packet".to_string()); } Ok(Packet { name: None, packet }) } } } unsafe impl Send for FormatContext {} impl From<*mut AVFormatContext> for FormatContext { fn from(format_context: *mut AVFormatContext) -> Self { FormatContext { filename: "virtual_source".to_string(), format_context, streams: vec![], frames: vec![], frame_index: 0, } } } impl Drop for FormatContext { fn drop(&mut self) { unsafe { if !self.format_context.is_null() { avformat_free_context(self.format_context); } } } }