2019-11-24 10:14:22 +01:00
|
|
|
extern crate ferretro;
|
2020-11-29 11:02:32 +01:00
|
|
|
extern crate ffmpeg_next as ffmpeg;
|
2019-11-24 10:14:22 +01:00
|
|
|
|
|
|
|
use std::collections::VecDeque;
|
2019-11-25 07:55:30 +01:00
|
|
|
use std::io::Read;
|
2019-11-24 10:14:22 +01:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::pin::Pin;
|
|
|
|
|
2019-11-25 07:55:30 +01:00
|
|
|
use structopt::StructOpt;
|
2019-12-09 09:01:27 +01:00
|
|
|
use failure::Fallible;
|
2019-11-25 07:55:30 +01:00
|
|
|
|
2019-11-24 10:14:22 +01:00
|
|
|
use ferretro::retro;
|
2019-11-25 07:55:30 +01:00
|
|
|
use ferretro::retro::ffi::{PixelFormat, GameGeometry, SystemAvInfo, SystemInfo};
|
|
|
|
use ferretro::retro::wrapper::{LibretroWrapper, Handler};
|
2019-11-24 10:14:22 +01:00
|
|
|
|
2019-12-09 09:01:27 +01:00
|
|
|
use ffmpeg::{format, filter, frame, media, ChannelLayout};
|
2019-11-25 07:55:30 +01:00
|
|
|
use ffmpeg::util::rational::Rational;
|
2019-11-24 10:14:22 +01:00
|
|
|
|
|
|
|
struct MyEmulator {
|
|
|
|
retro: retro::wrapper::LibretroWrapper,
|
2019-11-25 07:55:30 +01:00
|
|
|
sys_info: SystemInfo,
|
2020-01-25 08:58:04 +01:00
|
|
|
av_info: SystemAvInfo,
|
2019-11-24 10:14:22 +01:00
|
|
|
audio_buf: Vec<(i16, i16)>,
|
2019-11-25 07:55:30 +01:00
|
|
|
video_pixel_format: format::Pixel,
|
2019-11-24 10:14:22 +01:00
|
|
|
video_frames: VecDeque<frame::Video>,
|
|
|
|
video_encoder: ffmpeg::encoder::Video,
|
|
|
|
audio_encoder: ffmpeg::encoder::Audio,
|
2019-12-09 09:01:27 +01:00
|
|
|
video_filter: filter::Graph,
|
|
|
|
audio_filter: filter::Graph,
|
2019-11-24 10:14:22 +01:00
|
|
|
sys_path: Option<PathBuf>,
|
|
|
|
}
|
|
|
|
|
2019-12-09 09:01:27 +01:00
|
|
|
fn video_filter(
|
|
|
|
video_encoder: &ffmpeg::encoder::video::Video,
|
|
|
|
av_info: &SystemAvInfo,
|
|
|
|
pix_fmt: PixelFormat,
|
|
|
|
) -> Result<filter::Graph, ffmpeg::Error> {
|
|
|
|
let mut vfilter = filter::Graph::new();
|
|
|
|
let pix_fmt = match pix_fmt {
|
|
|
|
PixelFormat::ARGB1555 => if cfg!(target_endian = "big") { "rgb555be" } else { "rgb555le" },
|
|
|
|
PixelFormat::ARGB8888 => "argb",
|
|
|
|
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);
|
2020-01-26 04:32:12 +01:00
|
|
|
let fps = if av_info.timing.fps == 0.0 { 60.0 } else { av_info.timing.fps };
|
2019-12-09 09:01:27 +01:00
|
|
|
let args = format!(
|
2020-01-26 04:32:12 +01:00
|
|
|
"width={}:height={}:pix_fmt={}:frame_rate={}:pixel_aspect={}:time_base=1/{}",
|
2019-12-09 09:01:27 +01:00
|
|
|
av_info.geometry.base_width,
|
|
|
|
av_info.geometry.base_height,
|
|
|
|
pix_fmt,
|
2020-01-26 04:32:12 +01:00
|
|
|
fps,
|
2019-12-09 09:01:27 +01:00
|
|
|
pixel_aspect,
|
2020-01-26 04:32:12 +01:00
|
|
|
fps,
|
2019-12-09 09:01:27 +01:00
|
|
|
);
|
2020-01-25 21:15:59 +01:00
|
|
|
eprintln!("{}", args);
|
|
|
|
vfilter.add(&filter::find("buffer").unwrap(), "in", &args)?;
|
2019-12-09 09:01:27 +01:00
|
|
|
//scale?
|
2020-01-25 21:15:59 +01:00
|
|
|
vfilter.add(&filter::find("buffersink").unwrap(), "out", "")?;
|
2019-12-09 09:01:27 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut out = vfilter.get("out").unwrap();
|
|
|
|
out.set_pixel_format(video_encoder.format());
|
|
|
|
}
|
|
|
|
|
|
|
|
vfilter.output("in", 0)?
|
2020-01-26 04:32:12 +01:00
|
|
|
.input("out", 0)?
|
|
|
|
.parse("null")?;
|
|
|
|
|
2019-12-09 09:01:27 +01:00
|
|
|
vfilter.validate()?;
|
|
|
|
|
|
|
|
Ok(vfilter)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn audio_filter(
|
|
|
|
audio_encoder: &ffmpeg::encoder::audio::Audio,
|
|
|
|
sample_rate: f64,
|
|
|
|
) -> Result<filter::Graph, ffmpeg::Error> {
|
|
|
|
let mut afilter = filter::Graph::new();
|
2020-01-26 04:32:12 +01:00
|
|
|
let sample_rate = if sample_rate == 0.0 { 32040.0 } else { sample_rate };
|
2019-12-09 09:01:27 +01:00
|
|
|
let args = format!("sample_rate={}:sample_fmt=s16:channel_layout=stereo", sample_rate);
|
2020-01-26 04:32:12 +01:00
|
|
|
eprintln!("{}", args);
|
2020-01-25 21:15:59 +01:00
|
|
|
afilter.add(&filter::find("abuffer").unwrap(), "in", &args)?;
|
2019-12-09 09:01:27 +01:00
|
|
|
//aresample?
|
2020-01-25 21:15:59 +01:00
|
|
|
afilter.add(&filter::find("abuffersink").unwrap(), "out", "")?;
|
2019-12-09 09:01:27 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut out = afilter.get("out").unwrap();
|
|
|
|
out.set_sample_format(audio_encoder.format());
|
|
|
|
out.set_channel_layout(audio_encoder.channel_layout());
|
|
|
|
out.set_sample_rate(audio_encoder.rate());
|
|
|
|
}
|
|
|
|
|
|
|
|
afilter.output("in", 0)?
|
2020-01-26 04:32:12 +01:00
|
|
|
.input("out", 0)?
|
|
|
|
.parse("anull")?;
|
2019-12-09 09:01:27 +01:00
|
|
|
afilter.validate()?;
|
|
|
|
|
|
|
|
Ok(afilter)
|
|
|
|
}
|
|
|
|
|
2019-11-24 10:14:22 +01:00
|
|
|
impl MyEmulator {
|
2019-11-25 07:55:30 +01:00
|
|
|
pub fn new(
|
|
|
|
core_path: impl AsRef<Path>,
|
|
|
|
sys_path: &Option<impl AsRef<Path>>,
|
|
|
|
mut video_encoder: ffmpeg::encoder::video::Video,
|
|
|
|
mut audio_encoder: ffmpeg::encoder::audio::Audio,
|
|
|
|
) -> Pin<Box<Self>> {
|
|
|
|
let lib = libloading::Library::new(core_path.as_ref()).unwrap();
|
2019-11-24 10:14:22 +01:00
|
|
|
let raw_retro = retro::loading::LibretroApi::from_library(lib).unwrap();
|
|
|
|
let retro = retro::wrapper::LibretroWrapper::from(raw_retro);
|
|
|
|
|
2019-11-25 07:55:30 +01:00
|
|
|
let sys_info = retro.get_system_info();
|
2020-11-29 11:01:07 +01:00
|
|
|
let mut av_info = retro.get_system_av_info();
|
2019-11-25 07:55:30 +01:00
|
|
|
|
2020-01-26 04:32:12 +01:00
|
|
|
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));
|
2019-11-25 07:55:30 +01:00
|
|
|
video_encoder.set_frame_rate(av_info.timing.fps.into());
|
2020-11-29 11:01:07 +01:00
|
|
|
if av_info.geometry.base_height == 0 && av_info.geometry.base_width == 0 {
|
|
|
|
av_info.geometry.base_width = 640;
|
|
|
|
av_info.geometry.base_height = 480;
|
|
|
|
av_info.geometry.aspect_ratio = 4.33;
|
|
|
|
}
|
2019-11-25 07:55:30 +01:00
|
|
|
video_encoder.set_width(av_info.geometry.base_width);
|
|
|
|
video_encoder.set_height(av_info.geometry.base_height);
|
2020-11-29 11:01:07 +01:00
|
|
|
//video_encoder.set_aspect_ratio(av_info.geometry.aspect_ratio as f64);
|
2019-11-25 07:55:30 +01:00
|
|
|
|
2020-01-26 04:32:12 +01:00
|
|
|
audio_encoder.set_rate(44100);
|
2019-12-09 09:01:27 +01:00
|
|
|
|
2020-01-25 08:58:04 +01:00
|
|
|
let pix_fmt = PixelFormat::ARGB1555; // temporary until env call is made
|
|
|
|
let video_filter = video_filter(&video_encoder, &av_info, pix_fmt).unwrap();
|
2019-12-09 09:01:27 +01:00
|
|
|
let audio_filter = audio_filter(&audio_encoder, av_info.timing.sample_rate).unwrap();
|
2019-11-25 07:55:30 +01:00
|
|
|
|
2019-12-09 09:01:27 +01:00
|
|
|
let video_encoder = video_encoder.open().unwrap();
|
|
|
|
let audio_encoder = audio_encoder.open().unwrap();
|
2019-11-24 10:14:22 +01:00
|
|
|
|
|
|
|
let emu = MyEmulator {
|
|
|
|
retro,
|
2019-11-25 07:55:30 +01:00
|
|
|
sys_info,
|
2020-01-25 08:58:04 +01:00
|
|
|
av_info: av_info.clone(),
|
2019-11-24 10:14:22 +01:00
|
|
|
audio_buf: Default::default(),
|
2019-11-25 07:55:30 +01:00
|
|
|
video_pixel_format: format::Pixel::RGB555,
|
2019-11-24 10:14:22 +01:00
|
|
|
video_frames: Default::default(),
|
2019-12-09 09:01:27 +01:00
|
|
|
video_encoder,
|
|
|
|
audio_encoder,
|
|
|
|
video_filter,
|
|
|
|
audio_filter,
|
2019-11-25 07:55:30 +01:00
|
|
|
sys_path: sys_path.as_ref().map(|x| x.as_ref().to_path_buf()),
|
2019-11-24 10:14:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut pin_emu = Box::pin(emu);
|
|
|
|
retro::wrapper::set_handler(pin_emu.as_mut());
|
|
|
|
pin_emu.retro.init();
|
2019-11-25 07:55:30 +01:00
|
|
|
pin_emu.set_system_av_info(av_info);
|
2019-11-24 10:14:22 +01:00
|
|
|
pin_emu
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run(&mut self) {
|
|
|
|
self.retro.run();
|
|
|
|
|
2020-01-26 04:32:12 +01:00
|
|
|
let mut vframe = self.video_frames.pop_front().unwrap();
|
2019-11-24 10:14:22 +01:00
|
|
|
let mut aframe = frame::Audio::new(
|
|
|
|
format::Sample::I16(format::sample::Type::Packed),
|
|
|
|
self.audio_buf.len(),
|
|
|
|
ChannelLayout::STEREO
|
|
|
|
);
|
|
|
|
let aplane: &mut [(i16, i16)] = aframe.plane_mut(0);
|
|
|
|
aplane.copy_from_slice(self.audio_buf.as_ref());
|
|
|
|
self.audio_buf.clear();
|
|
|
|
|
|
|
|
let mut out = ffmpeg::Packet::empty();
|
2020-01-26 04:32:12 +01:00
|
|
|
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();
|
|
|
|
}
|
2019-11-25 07:55:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_game(&self, rom: impl AsRef<Path>) {
|
|
|
|
let path = rom.as_ref();
|
|
|
|
let mut data = None;
|
|
|
|
let mut v = Vec::new();
|
|
|
|
if !self.sys_info.need_fullpath {
|
|
|
|
if let Ok(mut f) = std::fs::File::open(path) {
|
|
|
|
if f.read_to_end(&mut v).is_ok() {
|
|
|
|
data = Some(v.as_ref());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.retro
|
|
|
|
.load_game(Some(path), data, None)
|
|
|
|
.unwrap();
|
2019-11-24 10:14:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl retro::wrapper::Handler for MyEmulator {
|
|
|
|
fn libretro_core(&mut self) -> &mut LibretroWrapper {
|
|
|
|
&mut self.retro
|
|
|
|
}
|
|
|
|
|
|
|
|
fn video_refresh(&mut self, data: &[u8], width: u32, height: u32, pitch: u32) {
|
2019-11-25 07:55:30 +01:00
|
|
|
let mut vframe = frame::Video::new(self.video_pixel_format, width, height);
|
2019-11-24 10:14:22 +01:00
|
|
|
|
|
|
|
let stride = vframe.stride(0);
|
|
|
|
let pitch = pitch as usize;
|
|
|
|
|
|
|
|
let vplane = vframe.data_mut(0);
|
|
|
|
if data.len() == vplane.len() && pitch == stride {
|
|
|
|
vplane.copy_from_slice(&data);
|
|
|
|
} else {
|
|
|
|
for y in 0..(height as usize) {
|
|
|
|
let ffbegin = y * stride;
|
|
|
|
let lrbegin = y * pitch;
|
|
|
|
let min = usize::min(stride, pitch);
|
2019-11-25 07:55:30 +01:00
|
|
|
vplane[ffbegin..(ffbegin + min)].copy_from_slice(
|
2019-11-24 10:14:22 +01:00
|
|
|
&data[lrbegin..(lrbegin + min)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.video_frames.push_back(vframe);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn audio_sample(&mut self, left: i16, right: i16) {
|
|
|
|
self.audio_buf.push((left, right));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn audio_sample_batch(&mut self, stereo_pcm: &[i16]) -> usize {
|
|
|
|
let left_iter = stereo_pcm.iter().step_by(2).cloned();
|
|
|
|
let right_iter = stereo_pcm.iter().skip(1).step_by(2).cloned();
|
|
|
|
self.audio_buf.extend(Iterator::zip(left_iter, right_iter));
|
|
|
|
stereo_pcm.len()
|
|
|
|
}
|
|
|
|
|
2019-11-25 07:55:30 +01:00
|
|
|
fn get_can_dupe(&mut self) -> Option<bool> { Some(false) }
|
|
|
|
|
|
|
|
fn get_system_directory(&mut self) -> Option<PathBuf> {
|
|
|
|
self.sys_path.clone()
|
|
|
|
}
|
|
|
|
|
2019-11-24 10:14:22 +01:00
|
|
|
fn set_pixel_format(&mut self, format: PixelFormat) -> bool {
|
2019-11-25 07:55:30 +01:00
|
|
|
self.video_pixel_format = match format {
|
2019-11-24 10:14:22 +01:00
|
|
|
PixelFormat::ARGB1555 => format::Pixel::RGB555,
|
|
|
|
PixelFormat::ARGB8888 => format::Pixel::RGB32,
|
|
|
|
PixelFormat::RGB565 => format::Pixel::RGB565,
|
|
|
|
};
|
2020-01-25 08:58:04 +01:00
|
|
|
self.video_filter = video_filter(&self.video_encoder, &self.av_info, format).unwrap();
|
2019-11-24 10:14:22 +01:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_system_av_info(&mut self, system_av_info: SystemAvInfo) -> bool {
|
|
|
|
self.video_encoder.set_frame_rate(system_av_info.timing.fps.into());
|
2020-11-29 11:01:07 +01:00
|
|
|
if system_av_info.timing.sample_rate.round() as i32 > 0 {
|
2019-11-24 10:14:22 +01:00
|
|
|
self.audio_encoder.set_rate(system_av_info.timing.sample_rate.round() as i32);
|
2020-11-29 11:01:07 +01:00
|
|
|
}
|
2020-01-25 08:58:04 +01:00
|
|
|
self.av_info.timing = system_av_info.timing;
|
2020-01-25 21:15:59 +01:00
|
|
|
self.set_geometry(system_av_info.geometry);
|
2019-11-24 10:14:22 +01:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_geometry(&mut self, geometry: GameGeometry) -> bool {
|
|
|
|
self.video_encoder.set_width(geometry.base_width);
|
|
|
|
self.video_encoder.set_height(geometry.base_height);
|
|
|
|
self.video_encoder.set_aspect_ratio(geometry.aspect_ratio as f64);
|
2020-01-25 08:58:04 +01:00
|
|
|
self.av_info.geometry = geometry;
|
2020-01-25 21:15:59 +01:00
|
|
|
let pixel_format = match self.video_pixel_format {
|
|
|
|
format::Pixel::RGB555 => PixelFormat::ARGB1555,
|
|
|
|
format::Pixel::RGB32 => PixelFormat::ARGB8888,
|
|
|
|
format::Pixel::RGB565 => PixelFormat::RGB565,
|
|
|
|
_ => unimplemented!(),
|
|
|
|
};
|
|
|
|
self.video_filter = video_filter(&self.video_encoder, &self.av_info, pixel_format).unwrap();
|
2019-11-24 10:14:22 +01:00
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 07:55:30 +01:00
|
|
|
#[derive(StructOpt)]
|
|
|
|
struct Opt {
|
|
|
|
/// Core module to use.
|
|
|
|
#[structopt(short, long, parse(from_os_str))]
|
|
|
|
core: PathBuf,
|
|
|
|
/// ROM to load using the core.
|
|
|
|
#[structopt(short, long, parse(from_os_str))]
|
|
|
|
rom: PathBuf,
|
|
|
|
/// Recorded video to write.
|
|
|
|
#[structopt(short, long, parse(from_os_str))]
|
|
|
|
video: PathBuf,
|
|
|
|
/// System directory, often containing BIOS files
|
|
|
|
#[structopt(short, long, parse(from_os_str))]
|
|
|
|
system: Option<PathBuf>,
|
2019-11-24 10:14:22 +01:00
|
|
|
}
|
|
|
|
|
2019-12-09 09:01:27 +01:00
|
|
|
fn main() -> Fallible<()> {
|
2019-11-25 07:55:30 +01:00
|
|
|
let opt: Opt = Opt::from_args();
|
2020-11-29 11:01:07 +01:00
|
|
|
ffmpeg::log::set_level(ffmpeg::log::Level::Trace);
|
2019-11-24 10:14:22 +01:00
|
|
|
ffmpeg::init().unwrap();
|
|
|
|
|
2019-12-09 09:01:27 +01:00
|
|
|
let mut octx = format::output(&opt.video)?;
|
2019-11-24 10:14:22 +01:00
|
|
|
|
2019-12-09 09:01:27 +01:00
|
|
|
let detected_vcodec = octx.format().codec(&opt.video, media::Type::Video);
|
|
|
|
let detected_acodec = octx.format().codec(&opt.video, media::Type::Audio);
|
|
|
|
|
|
|
|
let vcodec = ffmpeg::encoder::find(detected_vcodec).unwrap().video()?;
|
|
|
|
let acodec = ffmpeg::encoder::find(detected_acodec).unwrap().audio()?;
|
|
|
|
|
|
|
|
let mut video_encoder = octx.add_stream(vcodec)?.codec().encoder().video()?;
|
|
|
|
let mut audio_encoder = octx.add_stream(acodec)?.codec().encoder().audio()?;
|
2019-11-24 10:14:22 +01:00
|
|
|
|
2019-11-25 07:55:30 +01:00
|
|
|
video_encoder.set_bit_rate(64000);
|
2019-12-09 09:01:27 +01:00
|
|
|
video_encoder.set_format(video_encoder.codec().unwrap().video()?.formats().unwrap().nth(0).unwrap());
|
|
|
|
|
2019-11-25 07:55:30 +01:00
|
|
|
audio_encoder.set_bit_rate(64000);
|
2019-12-09 09:01:27 +01:00
|
|
|
audio_encoder.set_rate(44100);
|
|
|
|
audio_encoder.set_format(audio_encoder.codec().unwrap().audio()?.formats().unwrap().nth(0).unwrap());
|
|
|
|
audio_encoder.set_channels(2);
|
|
|
|
audio_encoder.set_channel_layout(ChannelLayout::STEREO);
|
2019-11-24 10:14:22 +01:00
|
|
|
|
2019-11-25 07:55:30 +01:00
|
|
|
let mut emu = MyEmulator::new(opt.core, &opt.system, video_encoder, audio_encoder);
|
|
|
|
emu.load_game(opt.rom);
|
2019-11-24 10:14:22 +01:00
|
|
|
|
2019-11-25 07:55:30 +01:00
|
|
|
octx.write_header().unwrap();
|
2019-11-24 10:14:22 +01:00
|
|
|
|
2019-11-25 07:55:30 +01:00
|
|
|
for _ in 0..60*60 {
|
|
|
|
emu.run();
|
2019-11-24 10:14:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
octx.write_trailer().unwrap();
|
2019-12-09 09:01:27 +01:00
|
|
|
Ok(())
|
2019-11-24 10:14:22 +01:00
|
|
|
}
|