WIP: getting closer to actually encoding a frame!!

This commit is contained in:
lif 2020-01-25 19:32:12 -08:00
parent b1bde52bb0
commit f94f5c836f
1 changed files with 25 additions and 9 deletions

View File

@ -42,13 +42,15 @@ fn video_filter(
PixelFormat::RGB565 => if cfg!(target_endian = "big") { "rgb565be" } else { "rgb565le" },
};
let pixel_aspect = av_info.geometry.aspect_ratio / (av_info.geometry.base_width as f32 / av_info.geometry.base_height as f32);
let fps = if av_info.timing.fps == 0.0 { 60.0 } else { av_info.timing.fps };
let args = format!(
"width={}:height={}:pix_fmt={}:frame_rate={}:pixel_aspect={}",
"width={}:height={}:pix_fmt={}:frame_rate={}:pixel_aspect={}:time_base=1/{}",
av_info.geometry.base_width,
av_info.geometry.base_height,
pix_fmt,
if av_info.timing.fps == 0.0 { 60.0 } else { av_info.timing.fps },
fps,
pixel_aspect,
fps,
);
eprintln!("{}", args);
vfilter.add(&filter::find("buffer").unwrap(), "in", &args)?;
@ -61,7 +63,9 @@ fn video_filter(
}
vfilter.output("in", 0)?
.input("out", 0)?;
.input("out", 0)?
.parse("null")?;
vfilter.validate()?;
Ok(vfilter)
@ -72,7 +76,9 @@ fn audio_filter(
sample_rate: f64,
) -> Result<filter::Graph, ffmpeg::Error> {
let mut afilter = filter::Graph::new();
let sample_rate = if sample_rate == 0.0 { 32040.0 } else { sample_rate };
let args = format!("sample_rate={}:sample_fmt=s16:channel_layout=stereo", sample_rate);
eprintln!("{}", args);
afilter.add(&filter::find("abuffer").unwrap(), "in", &args)?;
//aresample?
afilter.add(&filter::find("abuffersink").unwrap(), "out", "")?;
@ -85,7 +91,8 @@ fn audio_filter(
}
afilter.output("in", 0)?
.input("out", 0)?;
.input("out", 0)?
.parse("anull")?;
afilter.validate()?;
Ok(afilter)
@ -105,13 +112,16 @@ impl MyEmulator {
let sys_info = retro.get_system_info();
let av_info = retro.get_system_av_info();
video_encoder.set_time_base(Rational::new(1, av_info.timing.fps.round() as i32));
let fps_int = av_info.timing.fps.round() as i32;
let fps_int = if fps_int == 0 { 60 } else { fps_int };
video_encoder.set_time_base(Rational::new(1, fps_int));
video_encoder.set_frame_rate(av_info.timing.fps.into());
video_encoder.set_width(av_info.geometry.base_width);
video_encoder.set_height(av_info.geometry.base_height);
video_encoder.set_aspect_ratio(av_info.geometry.aspect_ratio as f64);
audio_encoder.set_rate(av_info.timing.sample_rate.round() as i32);
audio_encoder.set_rate(44100);
let pix_fmt = PixelFormat::ARGB1555; // temporary until env call is made
let video_filter = video_filter(&video_encoder, &av_info, pix_fmt).unwrap();
@ -144,7 +154,7 @@ impl MyEmulator {
pub fn run(&mut self) {
self.retro.run();
let vframe = self.video_frames.pop_front().unwrap();
let mut vframe = self.video_frames.pop_front().unwrap();
let mut aframe = frame::Audio::new(
format::Sample::I16(format::sample::Type::Packed),
self.audio_buf.len(),
@ -155,8 +165,14 @@ impl MyEmulator {
self.audio_buf.clear();
let mut out = ffmpeg::Packet::empty();
self.video_encoder.encode(&vframe, &mut out).unwrap();
self.audio_encoder.encode(&aframe, &mut out).unwrap();
self.video_filter.get("in").unwrap().source().add(&vframe).unwrap();
self.audio_filter.get("in").unwrap().source().add(&aframe).unwrap();
while let Ok(..) = self.video_filter.get("out").unwrap().sink().frame(&mut vframe) {
self.video_encoder.encode(&vframe, &mut out).unwrap();
}
while let Ok(..) = self.audio_filter.get("out").unwrap().sink().frame(&mut aframe) {
self.audio_encoder.encode(&aframe, &mut out).unwrap();
}
}
pub fn load_game(&self, rom: impl AsRef<Path>) {