WIP: messing with mutability for a better tomorrow
This commit is contained in:
parent
7b06ed61d1
commit
9dcbc94847
|
@ -205,7 +205,7 @@ static bool ffmpeg_init_config(struct ff_config_param *params,
|
||||||
audio_encoder.set_time_base(Rational::new(1, 60));
|
audio_encoder.set_time_base(Rational::new(1, 60));
|
||||||
audio_output.set_time_base(Rational::new(1, 60));
|
audio_output.set_time_base(Rational::new(1, 60));
|
||||||
|
|
||||||
let mut audio_encoder = audio_encoder.open_as(acodec).unwrap();
|
let audio_encoder = audio_encoder.open_as(acodec).unwrap();
|
||||||
//audio_output.set_parameters(&audio_encoder);
|
//audio_output.set_parameters(&audio_encoder);
|
||||||
|
|
||||||
let audio_filter = audio_filter(&audio_encoder, av_info.timing.sample_rate).unwrap();
|
let audio_filter = audio_filter(&audio_encoder, av_info.timing.sample_rate).unwrap();
|
||||||
|
@ -359,7 +359,7 @@ static bool ffmpeg_init_config(struct ff_config_param *params,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_game(&self, rom: impl AsRef<Path>) {
|
pub fn load_game(&mut self, rom: impl AsRef<Path>) {
|
||||||
let path = rom.as_ref();
|
let path = rom.as_ref();
|
||||||
let mut data = None;
|
let mut data = None;
|
||||||
let mut v = Vec::new();
|
let mut v = Vec::new();
|
||||||
|
|
|
@ -167,7 +167,7 @@ impl MyEmulator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_game(&self, rom: impl AsRef<Path>) {
|
pub fn load_game(&mut self, rom: impl AsRef<Path>) {
|
||||||
let path = rom.as_ref();
|
let path = rom.as_ref();
|
||||||
let mut data = None;
|
let mut data = None;
|
||||||
let mut v = Vec::new();
|
let mut v = Vec::new();
|
||||||
|
|
|
@ -0,0 +1,519 @@
|
||||||
|
extern crate ffmpeg_next as ffmpeg;
|
||||||
|
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
use std::io::Read;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::pin::Pin;
|
||||||
|
|
||||||
|
use structopt::StructOpt;
|
||||||
|
use failure::Fallible;
|
||||||
|
|
||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
use ffmpeg::{ChannelLayout, Packet, codec, filter, format, frame, media};
|
||||||
|
use ffmpeg::util::rational::Rational;
|
||||||
|
|
||||||
|
struct MyEmulator {
|
||||||
|
retro: LibretroWrapper,
|
||||||
|
sys_info: SystemInfo,
|
||||||
|
av_info: SystemAvInfo,
|
||||||
|
audio_buf: Vec<(i16, i16)>,
|
||||||
|
video_pixel_format: format::Pixel,
|
||||||
|
prev_video_frame: Option<frame::Video>,
|
||||||
|
video_frames: VecDeque<frame::Video>,
|
||||||
|
video_encoder: ffmpeg::encoder::Video,
|
||||||
|
audio_encoder: ffmpeg::encoder::Audio,
|
||||||
|
video_filter: filter::Graph,
|
||||||
|
audio_filter: filter::Graph,
|
||||||
|
sys_path: Option<PathBuf>,
|
||||||
|
frame_properties_locked: bool,
|
||||||
|
octx: ffmpeg::format::context::Output,
|
||||||
|
frame: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
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={}:time_base=1/{}",
|
||||||
|
av_info.geometry.base_width,
|
||||||
|
av_info.geometry.base_height,
|
||||||
|
pix_fmt,
|
||||||
|
fps,
|
||||||
|
pixel_aspect,
|
||||||
|
fps,
|
||||||
|
);
|
||||||
|
eprintln!("🎥 filter args: {}", args);
|
||||||
|
vfilter.add(&filter::find("buffer").unwrap(), "in", &args)?;
|
||||||
|
//scale?
|
||||||
|
vfilter.add(&filter::find("buffersink").unwrap(), "out", "")?;
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut out = vfilter.get("out").unwrap();
|
||||||
|
out.set_pixel_format(video_encoder.format());
|
||||||
|
}
|
||||||
|
|
||||||
|
vfilter.output("in", 0)?
|
||||||
|
.input("out", 0)?
|
||||||
|
.parse("null")?; // passthrough filter for video
|
||||||
|
|
||||||
|
vfilter.validate()?;
|
||||||
|
// human-readable filter graph
|
||||||
|
eprintln!("{}", vfilter.dump());
|
||||||
|
|
||||||
|
Ok(vfilter)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn audio_filter(
|
||||||
|
audio_encoder: &ffmpeg::codec::encoder::Audio,
|
||||||
|
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:time_base=1/60", sample_rate);
|
||||||
|
eprintln!("🔊 filter args: {}", args);
|
||||||
|
afilter.add(&filter::find("abuffer").unwrap(), "in", &args)?;
|
||||||
|
//aresample?
|
||||||
|
afilter.add(&filter::find("abuffersink").unwrap(), "out", "")?;
|
||||||
|
|
||||||
|
{
|
||||||
|
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)?
|
||||||
|
.input("out", 0)?
|
||||||
|
.parse("anull")?;
|
||||||
|
afilter.validate()?;
|
||||||
|
// human-readable filter graph
|
||||||
|
eprintln!("{}", afilter.dump());
|
||||||
|
|
||||||
|
if let Some(codec) = audio_encoder.codec() {
|
||||||
|
if !codec
|
||||||
|
.capabilities()
|
||||||
|
.contains(ffmpeg::codec::capabilities::Capabilities::VARIABLE_FRAME_SIZE)
|
||||||
|
{
|
||||||
|
eprintln!("setting constant frame size {}", audio_encoder.frame_size());
|
||||||
|
afilter
|
||||||
|
.get("out")
|
||||||
|
.unwrap()
|
||||||
|
.sink()
|
||||||
|
.set_frame_size(audio_encoder.frame_size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(afilter)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MyEmulator {
|
||||||
|
pub fn new(
|
||||||
|
core_path: impl AsRef<Path>,
|
||||||
|
sys_path: &Option<impl AsRef<Path>>,
|
||||||
|
video_path: impl AsRef<Path>,
|
||||||
|
mut octx: ffmpeg::format::context::Output,
|
||||||
|
) -> Pin<Box<Self>> {
|
||||||
|
let lib = libloading::Library::new(core_path.as_ref()).unwrap();
|
||||||
|
let raw_retro = retro::loading::LibretroApi::from_library(lib).unwrap();
|
||||||
|
let retro = retro::wrapper::LibretroWrapper::from(raw_retro);
|
||||||
|
|
||||||
|
let sys_info = retro.get_system_info();
|
||||||
|
let mut av_info = retro.get_system_av_info();
|
||||||
|
|
||||||
|
let fps_int = av_info.timing.fps.round() as i32;
|
||||||
|
let fps_int = if fps_int == 0 { 60 } else { fps_int };
|
||||||
|
|
||||||
|
let detected_vcodec = octx.format().codec(&video_path, media::Type::Video);
|
||||||
|
//let detected_acodec = octx.format().codec(&video_path, media::Type::Audio);
|
||||||
|
let wavname = Path::new("out.wav");
|
||||||
|
let detected_acodec = octx.format().codec(&wavname, media::Type::Audio);
|
||||||
|
|
||||||
|
let vcodec = ffmpeg::encoder::find(detected_vcodec).unwrap().video().unwrap();
|
||||||
|
let acodec = ffmpeg::encoder::find(detected_acodec).unwrap().audio().unwrap();
|
||||||
|
|
||||||
|
let mut video_output = octx.add_stream(vcodec).unwrap();
|
||||||
|
video_output.set_time_base(Rational::new(1, 60));
|
||||||
|
let mut video_encoder = video_output.codec().encoder().video().unwrap();
|
||||||
|
|
||||||
|
video_encoder.set_bit_rate(2560000);
|
||||||
|
video_encoder.set_format(video_encoder.codec().unwrap().video().unwrap().formats().unwrap().nth(0).unwrap());
|
||||||
|
|
||||||
|
video_encoder.set_time_base(Rational::new(1, 60));
|
||||||
|
video_encoder.set_frame_rate(Some(Rational::new(fps_int, 1)));
|
||||||
|
|
||||||
|
//video_encoder.set_frame_rate(av_info.timing.fps.into());
|
||||||
|
|
||||||
|
if av_info.geometry.base_height == 0 && av_info.geometry.base_width == 0 {
|
||||||
|
av_info.geometry.base_width = 320;
|
||||||
|
av_info.geometry.base_height = 224;
|
||||||
|
av_info.geometry.aspect_ratio = 4.33;
|
||||||
|
}
|
||||||
|
if av_info.timing.sample_rate == 0.0 {
|
||||||
|
av_info.timing.sample_rate = 44100.0;
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
|
||||||
|
let pix_fmt = PixelFormat::ARGB1555; // temporary until env call is made
|
||||||
|
let video_filter = video_filter(&video_encoder, &av_info, pix_fmt).unwrap();
|
||||||
|
|
||||||
|
let video_encoder = video_encoder.open_as(vcodec).unwrap();
|
||||||
|
//video_output.set_parameters(&video_encoder);
|
||||||
|
|
||||||
|
let mut audio_output = octx.add_stream(acodec).unwrap();
|
||||||
|
let mut audio_encoder = audio_output.codec().encoder().audio().unwrap();
|
||||||
|
|
||||||
|
//let mut video_encoder = octx.add_stream(vcodec).unwrap().codec().encoder().video().unwrap();
|
||||||
|
/*
|
||||||
|
let mut audio_output = octx.add_stream(acodec).unwrap();
|
||||||
|
let mut audio_encoder = audio_output.codec().encoder().audio().unwrap();
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
retroarch inits
|
||||||
|
static bool ffmpeg_init_config(struct ff_config_param *params,
|
||||||
|
if (!ffmpeg_init_muxer_pre(handle))
|
||||||
|
if (!ffmpeg_init_video(handle))
|
||||||
|
av_frame_alloc
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
audio_encoder.set_bit_rate(640000);
|
||||||
|
audio_encoder.set_max_bit_rate(990000);
|
||||||
|
|
||||||
|
//audio_encoder.set_rate(44100);
|
||||||
|
audio_encoder.set_rate(av_info.timing.sample_rate.round() as i32);
|
||||||
|
audio_encoder.set_channels(2);
|
||||||
|
audio_encoder.set_channel_layout(ChannelLayout::STEREO);
|
||||||
|
audio_encoder.set_format(audio_encoder.codec().unwrap().audio().unwrap().formats().unwrap().nth(0).unwrap());
|
||||||
|
audio_encoder.set_time_base(Rational::new(1, 60));
|
||||||
|
audio_output.set_time_base(Rational::new(1, 60));
|
||||||
|
|
||||||
|
let mut audio_encoder = audio_encoder.open_as(acodec).unwrap();
|
||||||
|
//audio_output.set_parameters(&audio_encoder);
|
||||||
|
|
||||||
|
let audio_filter = audio_filter(&audio_encoder, av_info.timing.sample_rate).unwrap();
|
||||||
|
|
||||||
|
//audio_encoder.set_rate(av_info.timing.sample_rate.round() as i32);
|
||||||
|
|
||||||
|
octx.write_header().unwrap();
|
||||||
|
ffmpeg::format::context::output::dump(&octx, 0, None);
|
||||||
|
|
||||||
|
let emu = MyEmulator {
|
||||||
|
retro,
|
||||||
|
sys_info,
|
||||||
|
av_info: av_info.clone(),
|
||||||
|
audio_buf: Default::default(),
|
||||||
|
video_pixel_format: format::Pixel::RGB555,
|
||||||
|
prev_video_frame: None,
|
||||||
|
video_frames: Default::default(),
|
||||||
|
video_encoder,
|
||||||
|
audio_encoder,
|
||||||
|
video_filter,
|
||||||
|
audio_filter,
|
||||||
|
sys_path: sys_path.as_ref().map(|x| x.as_ref().to_path_buf()),
|
||||||
|
frame_properties_locked: false,
|
||||||
|
octx,
|
||||||
|
frame: 0
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut pin_emu = Box::pin(emu);
|
||||||
|
retro::wrapper::set_handler(pin_emu.as_mut());
|
||||||
|
pin_emu.retro.init();
|
||||||
|
pin_emu.set_system_av_info(&av_info);
|
||||||
|
pin_emu
|
||||||
|
}
|
||||||
|
|
||||||
|
fn receive_and_write_packets(&mut self, encoder: EncoderToWriteFrom)
|
||||||
|
{
|
||||||
|
let stream_index = match encoder {
|
||||||
|
EncoderToWriteFrom::Video => 0,
|
||||||
|
EncoderToWriteFrom::Audio => 1,
|
||||||
|
};
|
||||||
|
let mut encoded_packet = ffmpeg::Packet::empty();
|
||||||
|
loop
|
||||||
|
{
|
||||||
|
match match encoder {
|
||||||
|
EncoderToWriteFrom::Video => self.video_encoder.receive_packet(&mut encoded_packet),
|
||||||
|
EncoderToWriteFrom::Audio => self.audio_encoder.receive_packet(&mut encoded_packet),
|
||||||
|
} {
|
||||||
|
Ok(..) => {
|
||||||
|
//if encoded_packet.size() > 0 {
|
||||||
|
encoded_packet.set_stream(stream_index);
|
||||||
|
eprintln!("📦 Writing packet, pts {:?} dts {:?} size {}", encoded_packet.pts(), encoded_packet.dts(), encoded_packet.size());
|
||||||
|
if stream_index == 0 {
|
||||||
|
encoded_packet.rescale_ts(Rational(1, 60), self.octx.stream(stream_index).unwrap().time_base());
|
||||||
|
}
|
||||||
|
eprintln!("📦 rescaled , pts {:?} dts {:?} size {}", encoded_packet.pts(), encoded_packet.dts(), encoded_packet.size());
|
||||||
|
|
||||||
|
match encoded_packet.write_interleaved(&mut self.octx) {
|
||||||
|
Ok(..) => eprintln!("Write OK"),
|
||||||
|
Err(e) => eprintln!("Error writing: {}", e),
|
||||||
|
}
|
||||||
|
//encoded_packet.write_interleaved(&mut self.octx).unwrap(); // AAA
|
||||||
|
//}
|
||||||
|
//else {
|
||||||
|
//eprintln!("Did not try to write 0-length packet");
|
||||||
|
//}
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Error writing packet: {:?}", e);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(&mut self, frame: i64) {
|
||||||
|
self.frame += 1;
|
||||||
|
self.retro.run();
|
||||||
|
|
||||||
|
match self.video_frames.pop_front() {
|
||||||
|
Some(mut vframe) => {
|
||||||
|
vframe.set_pts(Some(frame));
|
||||||
|
eprintln!("🎞 queue frame pts {:?}", vframe.pts());
|
||||||
|
self.video_filter.get("in").unwrap().source().add(&vframe).unwrap();
|
||||||
|
let mut filtered_vframe = frame::Video::empty();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match self.video_filter.get("out").unwrap().sink().frame(&mut filtered_vframe) {
|
||||||
|
Ok(..) => {
|
||||||
|
eprintln!("🎥 Got filtered video frame {}x{} pts {:?}", filtered_vframe.width(), filtered_vframe.height(), filtered_vframe.pts());
|
||||||
|
if self.video_filter.get("in").unwrap().source().failed_requests() > 0 {
|
||||||
|
println!("🎥 failed to put filter input frame");
|
||||||
|
}
|
||||||
|
//filtered_vframe.set_pts(Some(frame));
|
||||||
|
self.video_encoder.send_frame(&filtered_vframe).unwrap();
|
||||||
|
|
||||||
|
self.receive_and_write_packets(EncoderToWriteFrom::Video);
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Error getting filtered video frame: {:?}", e);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut aframe = frame::Audio::new(
|
||||||
|
format::Sample::I16(format::sample::Type::Packed),
|
||||||
|
self.audio_buf.len(),
|
||||||
|
ChannelLayout::STEREO
|
||||||
|
);
|
||||||
|
if aframe.planes() > 0 {
|
||||||
|
aframe.set_channels(2);
|
||||||
|
aframe.set_rate(44100);
|
||||||
|
aframe.set_pts(Some(frame));
|
||||||
|
let aplane: &mut [(i16, i16)] = aframe.plane_mut(0);
|
||||||
|
eprintln!("Audio buffer length {} -> {}", self.audio_buf.len(), aplane.len());
|
||||||
|
aplane.copy_from_slice(self.audio_buf.as_ref());
|
||||||
|
//eprintln!("src: {:?}, dest: {:?}", self.audio_buf, aplane);
|
||||||
|
self.audio_buf.clear();
|
||||||
|
|
||||||
|
eprintln!("frame audio: {:?}", aframe);
|
||||||
|
|
||||||
|
eprintln!("🎞 queue frame pts {:?}", aframe.pts());
|
||||||
|
self.audio_filter.get("in").unwrap().source().add(&aframe).unwrap();
|
||||||
|
|
||||||
|
let mut filtered_aframe = frame::Audio::empty();
|
||||||
|
loop {
|
||||||
|
match self.audio_filter.get("out").unwrap().sink().frame(&mut filtered_aframe) {
|
||||||
|
Ok(..) => {
|
||||||
|
eprintln!("🔊 Got filtered audio frame {:?} pts {:?}", filtered_aframe, filtered_aframe.pts());
|
||||||
|
if self.audio_filter.get("in").unwrap().source().failed_requests() > 0 {
|
||||||
|
println!("🎥 failed to put filter input frame");
|
||||||
|
}
|
||||||
|
//let faplane: &[f32] = filtered_aframe.plane(0);
|
||||||
|
//filtered_aframe.set_pts(Some(frame));
|
||||||
|
|
||||||
|
self.audio_encoder.send_frame(&filtered_aframe).unwrap();
|
||||||
|
self.receive_and_write_packets(EncoderToWriteFrom::Audio);
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Error getting filtered audio frame: {:?}", e);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => println!("Video not ready during frame {}", self.frame)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn end(&mut self) {
|
||||||
|
self.video_encoder.send_eof();
|
||||||
|
self.receive_and_write_packets(EncoderToWriteFrom::Video);
|
||||||
|
self.audio_encoder.send_eof();
|
||||||
|
self.receive_and_write_packets(EncoderToWriteFrom::Audio);
|
||||||
|
self.octx.write_trailer().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn unserialize(&mut self, state: impl AsRef<Path>) -> Fallible<()> {
|
||||||
|
let path = state.as_ref();
|
||||||
|
let mut v = Vec::new();
|
||||||
|
if let Ok(mut f) = std::fs::File::open(path) {
|
||||||
|
if f.read_to_end(&mut v).is_ok(){
|
||||||
|
return self.retro.unserialize(v.as_ref());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(failure::err_msg("Couldn't read file to unserialize"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl retro::wrapper::LibretroWrapperAccess for MyEmulator {
|
||||||
|
fn libretro_core(&mut self) -> &mut LibretroWrapper {
|
||||||
|
&mut self.retro
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl retro::wrapper::RetroCallbacks for MyEmulator {
|
||||||
|
fn video_refresh(&mut self, data: &[u8], width: u32, height: u32, pitch: u32) {
|
||||||
|
let mut vframe = frame::Video::new(self.video_pixel_format, width, height);
|
||||||
|
|
||||||
|
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);
|
||||||
|
vplane[ffbegin..(ffbegin + min)].copy_from_slice(
|
||||||
|
&data[lrbegin..(lrbegin + min)]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//vframe.set_pts(Some(self.frame as i64));
|
||||||
|
|
||||||
|
self.prev_video_frame.replace(vframe.clone());
|
||||||
|
self.video_frames.push_back(vframe);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn video_refresh_dupe(&mut self, width: u32, height: u32, _pitch: u32) {
|
||||||
|
if let Some(frame) = &self.prev_video_frame {
|
||||||
|
self.video_frames.push_back(frame.clone());
|
||||||
|
} else {
|
||||||
|
let vframe = frame::Video::new(self.video_pixel_format, width, height);
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_system_directory(&mut self) -> Option<PathBuf> {
|
||||||
|
self.sys_path.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_pixel_format(&mut self, format: PixelFormat) -> bool {
|
||||||
|
if self.frame_properties_locked {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.video_pixel_format = match format {
|
||||||
|
PixelFormat::ARGB1555 => format::Pixel::RGB555,
|
||||||
|
PixelFormat::ARGB8888 => format::Pixel::RGB32,
|
||||||
|
PixelFormat::RGB565 => format::Pixel::RGB565,
|
||||||
|
};
|
||||||
|
self.video_filter = video_filter(&self.video_encoder, &self.av_info, format).unwrap();
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_system_av_info(&mut self, system_av_info: &SystemAvInfo) -> bool {
|
||||||
|
if self.frame_properties_locked {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//self.video_encoder.set_frame_rate(system_av_info.timing.fps.into());
|
||||||
|
//self.video_encoder.set_time_base(Rational::new(1, 60));
|
||||||
|
//self.video_encoder.set_frame_rate(Some(Rational::new(1, 60)));
|
||||||
|
if system_av_info.timing.sample_rate.round() as i32 > 0 {
|
||||||
|
self.audio_encoder.set_rate(system_av_info.timing.sample_rate.round() as i32);
|
||||||
|
}
|
||||||
|
self.av_info.timing = system_av_info.timing.clone();
|
||||||
|
self.set_geometry(&system_av_info.geometry);
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_geometry(&mut self, geometry: &GameGeometry) -> bool {
|
||||||
|
if self.frame_properties_locked {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
self.av_info.geometry = geometry.clone();
|
||||||
|
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();
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn get_variable(&mut self, key: &str) -> Option<String> {
|
||||||
|
match key {
|
||||||
|
"beetle_saturn_analog_stick_deadzone" => Some("15%".to_string()),
|
||||||
|
"parallel-n64-gfxplugin" => Some("angrylion".to_string()),
|
||||||
|
"parallel-n64-astick-deadzone" => Some("15%".to_string()),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_variables(&mut self, variables: &Vec<Variable2>) -> bool {
|
||||||
|
for v in variables {
|
||||||
|
eprintln!("{:?}", v);
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn log_print(&mut self, level: retro::ffi::LogLevel, msg: &str) {
|
||||||
|
eprint!("🕹️ [{:?}] {}", level, msg);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,5 @@
|
||||||
|
//pub mod ffmpeg;
|
||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::retro::ffi::*;
|
use crate::retro::ffi::*;
|
||||||
use std::os::raw::c_uint;
|
use std::os::raw::c_uint;
|
||||||
|
|
|
@ -8,4 +8,5 @@ pub mod prelude {
|
||||||
pub use crate::retro::constants::*;
|
pub use crate::retro::constants::*;
|
||||||
pub use crate::retro::wrapped_types::*;
|
pub use crate::retro::wrapped_types::*;
|
||||||
pub use crate::retro::wrapper::{RetroCallbacks, LibretroWrapper, LibretroWrapperAccess};
|
pub use crate::retro::wrapper::{RetroCallbacks, LibretroWrapper, LibretroWrapperAccess};
|
||||||
|
pub use crate::retro::ffi::{PixelFormat, GameGeometry, SystemAvInfo, SystemInfo};
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,31 +46,31 @@ impl LibretroApi {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// set_environment() must be called before init().
|
/// set_environment() must be called before init().
|
||||||
pub fn set_environment(&self, cb: EnvironmentFn) {
|
pub fn set_environment(&mut self, cb: EnvironmentFn) {
|
||||||
unsafe { (&self.core_api.retro_set_environment)(cb) }
|
unsafe { (&self.core_api.retro_set_environment)(cb) }
|
||||||
}
|
}
|
||||||
/// set_video_refresh() must be called before run().
|
/// set_video_refresh() must be called before run().
|
||||||
pub fn set_video_refresh(&self, cb: VideoRefreshFn) {
|
pub fn set_video_refresh(&mut self, cb: VideoRefreshFn) {
|
||||||
unsafe { (&self.core_api.retro_set_video_refresh)(cb) }
|
unsafe { (&self.core_api.retro_set_video_refresh)(cb) }
|
||||||
}
|
}
|
||||||
pub fn set_audio_sample(&self, cb: AudioSampleFn) {
|
pub fn set_audio_sample(&mut self, cb: AudioSampleFn) {
|
||||||
unsafe { (&self.core_api.retro_set_audio_sample)(cb) }
|
unsafe { (&self.core_api.retro_set_audio_sample)(cb) }
|
||||||
}
|
}
|
||||||
pub fn set_audio_sample_batch(&self, cb: AudioSampleBatchFn) {
|
pub fn set_audio_sample_batch(&mut self, cb: AudioSampleBatchFn) {
|
||||||
unsafe { (&self.core_api.retro_set_audio_sample_batch)(cb) }
|
unsafe { (&self.core_api.retro_set_audio_sample_batch)(cb) }
|
||||||
}
|
}
|
||||||
pub fn set_input_poll(&self, cb: InputPollFn) {
|
pub fn set_input_poll(&mut self, cb: InputPollFn) {
|
||||||
unsafe { (&self.core_api.retro_set_input_poll)(cb) }
|
unsafe { (&self.core_api.retro_set_input_poll)(cb) }
|
||||||
}
|
}
|
||||||
pub fn set_input_state(&self, cb: InputStateFn) {
|
pub fn set_input_state(&mut self, cb: InputStateFn) {
|
||||||
unsafe { (&self.core_api.retro_set_input_state)(cb) }
|
unsafe { (&self.core_api.retro_set_input_state)(cb) }
|
||||||
}
|
}
|
||||||
/// set_environment() must be called before init().
|
/// set_environment() must be called before init().
|
||||||
pub fn init(&self) {
|
pub fn init(&mut self) {
|
||||||
// TODO assert!(called retro_set_environment);
|
// TODO assert!(called retro_set_environment);
|
||||||
unsafe { (&self.core_api.retro_init)() }
|
unsafe { (&self.core_api.retro_init)() }
|
||||||
}
|
}
|
||||||
pub fn deinit(&self) {
|
pub fn deinit(&mut self) {
|
||||||
unsafe { (&self.core_api.retro_deinit)() }
|
unsafe { (&self.core_api.retro_deinit)() }
|
||||||
}
|
}
|
||||||
/// Must return RETRO_API_VERSION. Used to validate ABI compatibility when the API is revised.
|
/// Must return RETRO_API_VERSION. Used to validate ABI compatibility when the API is revised.
|
||||||
|
@ -110,11 +110,11 @@ impl LibretroApi {
|
||||||
/// the core should call RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS to notify the
|
/// the core should call RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS to notify the
|
||||||
/// frontend if the descriptions for any controls have changed as a
|
/// frontend if the descriptions for any controls have changed as a
|
||||||
/// result of changing the device type.
|
/// result of changing the device type.
|
||||||
pub fn set_controller_port_device(&self, port: u32, device: u32) {
|
pub fn set_controller_port_device(&mut self, port: u32, device: u32) {
|
||||||
unsafe { (&self.core_api.retro_set_controller_port_device)(port, device) }
|
unsafe { (&self.core_api.retro_set_controller_port_device)(port, device) }
|
||||||
}
|
}
|
||||||
/// Resets the current game.
|
/// Resets the current game.
|
||||||
pub fn reset(&self) {
|
pub fn reset(&mut self) {
|
||||||
unsafe { (&self.core_api.retro_reset)() }
|
unsafe { (&self.core_api.retro_reset)() }
|
||||||
}
|
}
|
||||||
/// Runs the game for one video frame.
|
/// Runs the game for one video frame.
|
||||||
|
@ -124,11 +124,11 @@ impl LibretroApi {
|
||||||
/// this still counts as a frame, and retro_run() should explicitly dupe
|
/// this still counts as a frame, and retro_run() should explicitly dupe
|
||||||
/// a frame if GET_CAN_DUPE returns true.
|
/// a frame if GET_CAN_DUPE returns true.
|
||||||
/// In this case, the video callback can take a NULL argument for data.
|
/// In this case, the video callback can take a NULL argument for data.
|
||||||
pub fn run(&self) {
|
pub fn run(&mut self) {
|
||||||
unsafe { (&self.core_api.retro_run)() }
|
unsafe { (&self.core_api.retro_run)() }
|
||||||
}
|
}
|
||||||
/// Serializes internal state.
|
/// Serializes internal state.
|
||||||
pub fn serialize(&self) -> Fallible<Vec<u8>> {
|
pub fn serialize(&mut self) -> Fallible<Vec<u8>> {
|
||||||
let size: usize = unsafe { (&self.core_api.retro_serialize_size)() };
|
let size: usize = unsafe { (&self.core_api.retro_serialize_size)() };
|
||||||
let mut vec = Vec::with_capacity(size);
|
let mut vec = Vec::with_capacity(size);
|
||||||
vec.resize(size, 0);
|
vec.resize(size, 0);
|
||||||
|
@ -139,7 +139,7 @@ impl LibretroApi {
|
||||||
Err(failure::err_msg("Serialize failed"))
|
Err(failure::err_msg("Serialize failed"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn unserialize(&self, data: &[u8]) -> Fallible<()> {
|
pub fn unserialize(&mut self, data: &[u8]) -> Fallible<()> {
|
||||||
// validate size of the data
|
// validate size of the data
|
||||||
let size: usize = unsafe { (&self.core_api.retro_serialize_size)() };
|
let size: usize = unsafe { (&self.core_api.retro_serialize_size)() };
|
||||||
if data.len() != size {
|
if data.len() != size {
|
||||||
|
@ -152,15 +152,15 @@ impl LibretroApi {
|
||||||
Err(failure::err_msg("Unserialize failed"))
|
Err(failure::err_msg("Unserialize failed"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn cheat_reset(&self) {
|
pub fn cheat_reset(&mut self) {
|
||||||
unsafe { (&self.core_api.retro_cheat_reset)() }
|
unsafe { (&self.core_api.retro_cheat_reset)() }
|
||||||
}
|
}
|
||||||
pub fn cheat_set(&self, index: u32, enabled: bool, code: &str) {
|
pub fn cheat_set(&mut self, index: u32, enabled: bool, code: &str) {
|
||||||
unsafe { (&self.core_api.retro_cheat_set)(index, enabled, code.as_bytes().as_ptr() as *const c_char) }
|
unsafe { (&self.core_api.retro_cheat_set)(index, enabled, code.as_bytes().as_ptr() as *const c_char) }
|
||||||
}
|
}
|
||||||
/// Loads a game.
|
/// Loads a game.
|
||||||
pub fn load_game(
|
pub fn load_game(
|
||||||
&self,
|
&mut self,
|
||||||
path: Option<&Path>,
|
path: Option<&Path>,
|
||||||
data: Option<&[u8]>,
|
data: Option<&[u8]>,
|
||||||
meta: Option<&str>,
|
meta: Option<&str>,
|
||||||
|
@ -194,7 +194,7 @@ impl LibretroApi {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Unloads the currently loaded game. Called before deinit().
|
/// Unloads the currently loaded game. Called before deinit().
|
||||||
pub fn unload_game(&self) {
|
pub fn unload_game(&mut self) {
|
||||||
unsafe { (&self.core_api.retro_unload_game)() }
|
unsafe { (&self.core_api.retro_unload_game)() }
|
||||||
}
|
}
|
||||||
pub fn get_region(&self) -> Region {
|
pub fn get_region(&self) -> Region {
|
||||||
|
|
|
@ -4,7 +4,7 @@ use core::slice::from_raw_parts;
|
||||||
|
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::ffi::{CStr, CString};
|
use std::ffi::{CStr, CString};
|
||||||
use std::ops::Deref;
|
use std::ops::{Deref, DerefMut};
|
||||||
use std::os::raw::{c_char, c_uint};
|
use std::os::raw::{c_char, c_uint};
|
||||||
use std::os::unix::ffi::OsStrExt;
|
use std::os::unix::ffi::OsStrExt;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
@ -29,14 +29,10 @@ extern "C" {
|
||||||
fn c_ext_set_log_print_cb(cb: WrappedLogPrintFn);
|
fn c_ext_set_log_print_cb(cb: WrappedLogPrintFn);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait LibretroWrapperAccess {
|
|
||||||
fn libretro_core(&mut self) -> &mut LibretroWrapper;
|
|
||||||
}
|
|
||||||
|
|
||||||
// method docs largely copied/lightly-adapted-to-rust straight from libretro.h.
|
// method docs largely copied/lightly-adapted-to-rust straight from libretro.h.
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub trait RetroCallbacks: LibretroWrapperAccess + Unpin + 'static {
|
pub trait RetroCallbacks: Unpin + 'static {
|
||||||
// -- main callbacks --
|
// -- main callbacks --
|
||||||
/// Render a frame. Pixel format is 15-bit 0RGB1555 native endian
|
/// Render a frame. Pixel format is 15-bit 0RGB1555 native endian
|
||||||
/// unless changed (see [Self::set_pixel_format]).
|
/// unless changed (see [Self::set_pixel_format]).
|
||||||
|
@ -370,9 +366,16 @@ pub trait RetroCallbacks: LibretroWrapperAccess + Unpin + 'static {
|
||||||
fn get_sensor_input(&mut self, port: c_uint, id: c_uint) -> f32 { 0.0 }
|
fn get_sensor_input(&mut self, port: c_uint, id: c_uint) -> f32 { 0.0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait LibretroWrapperAccess {
|
||||||
|
fn libretro_core(&mut self) -> &mut LibretroWrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait RootRetroCallbacks : RetroCallbacks + LibretroWrapperAccess {}
|
||||||
|
impl<T: RetroCallbacks + LibretroWrapperAccess> RootRetroCallbacks for T {}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct StaticCallbacks {
|
struct StaticCallbacks {
|
||||||
handler: Option<Pin<&'static mut dyn RetroCallbacks>>,
|
handler: Option<Pin<&'static mut dyn RootRetroCallbacks>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Sync for StaticCallbacks {}
|
unsafe impl Sync for StaticCallbacks {}
|
||||||
|
@ -854,11 +857,17 @@ impl Deref for LibretroWrapper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl DerefMut for LibretroWrapper {
|
||||||
|
fn deref_mut(&mut self) -> &mut LibretroApi {
|
||||||
|
&mut self.api
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// a note on lifetimes: we explicitly lie about them here because as long as they live as long as
|
// a note on lifetimes: we explicitly lie about them here because as long as they live as long as
|
||||||
// the library wrapper itself we're good (we wipe our 'static references on drop() too)
|
// the library wrapper itself we're good (we wipe our 'static references on drop() too)
|
||||||
pub fn set_handler(handler: Pin<&'_ mut (dyn RetroCallbacks + '_)>) {
|
pub fn set_handler(handler: Pin<&'_ mut (dyn RootRetroCallbacks + '_)>) {
|
||||||
unsafe {
|
unsafe {
|
||||||
let ptr = handler.get_unchecked_mut() as *mut dyn RetroCallbacks;
|
let ptr = handler.get_unchecked_mut() as *mut dyn RootRetroCallbacks;
|
||||||
CB_SINGLETON
|
CB_SINGLETON
|
||||||
.handler
|
.handler
|
||||||
.replace(Pin::new_unchecked(ptr.as_mut().unwrap()));
|
.replace(Pin::new_unchecked(ptr.as_mut().unwrap()));
|
||||||
|
|
Loading…
Reference in New Issue