WIP more - now gets meaningful ffmpeg errors, still need to make a filter pipeline to do resampling and pixel format conv
This commit is contained in:
parent
dafe029b01
commit
ffc53708d1
|
@ -2,21 +2,24 @@ extern crate ferretro;
|
|||
extern crate ffmpeg4 as ffmpeg;
|
||||
|
||||
use std::collections::VecDeque;
|
||||
use std::env;
|
||||
use std::io::Read;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::pin::Pin;
|
||||
|
||||
use ferretro::retro;
|
||||
use ferretro::retro::ffi::{PixelFormat, GameGeometry, SystemAvInfo};
|
||||
use ferretro::retro::wrapper::LibretroWrapper;
|
||||
use structopt::StructOpt;
|
||||
|
||||
use ffmpeg::{codec, filter, format, frame, media, ChannelLayout};
|
||||
use ffmpeg::{rescale, Rescale};
|
||||
use ferretro::retro;
|
||||
use ferretro::retro::ffi::{PixelFormat, GameGeometry, SystemAvInfo, SystemInfo};
|
||||
use ferretro::retro::wrapper::{LibretroWrapper, Handler};
|
||||
|
||||
use ffmpeg::{format, frame, media, ChannelLayout};
|
||||
use ffmpeg::util::rational::Rational;
|
||||
|
||||
struct MyEmulator {
|
||||
retro: retro::wrapper::LibretroWrapper,
|
||||
sys_info: SystemInfo,
|
||||
audio_buf: Vec<(i16, i16)>,
|
||||
video_pixfmt: format::Pixel,
|
||||
video_pixel_format: format::Pixel,
|
||||
video_frames: VecDeque<frame::Video>,
|
||||
video_encoder: ffmpeg::encoder::Video,
|
||||
audio_encoder: ffmpeg::encoder::Audio,
|
||||
|
@ -24,27 +27,49 @@ struct MyEmulator {
|
|||
}
|
||||
|
||||
impl MyEmulator {
|
||||
pub fn new(core_path: impl AsRef<Path>, sys_path: &Option<impl AsRef<Path>>, ffstream: format::stream::StreamMut) -> Pin<Box<Self>> {
|
||||
let lib = libloading::Library::new(core_path).unwrap();
|
||||
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();
|
||||
let raw_retro = retro::loading::LibretroApi::from_library(lib).unwrap();
|
||||
let retro = retro::wrapper::LibretroWrapper::from(raw_retro);
|
||||
|
||||
let mut av_info = retro.get_system_av_info();
|
||||
let video_pixfmt = format::Pixel::RGB555;
|
||||
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));
|
||||
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_format(format::Pixel::YUV420P);
|
||||
video_encoder.set_aspect_ratio(av_info.geometry.aspect_ratio as f64);
|
||||
|
||||
audio_encoder.set_rate(44100);
|
||||
audio_encoder.set_format(audio_encoder.codec().unwrap().audio().unwrap().formats().unwrap().nth(0).unwrap());
|
||||
audio_encoder.set_channels(2);
|
||||
audio_encoder.set_channel_layout(ChannelLayout::STEREO);
|
||||
|
||||
let video_encoder_2 = video_encoder.open().unwrap();
|
||||
let audio_encoder_2 = audio_encoder.open().unwrap();
|
||||
|
||||
let emu = MyEmulator {
|
||||
retro,
|
||||
sys_info,
|
||||
audio_buf: Default::default(),
|
||||
video_pixfmt,
|
||||
video_pixel_format: format::Pixel::RGB555,
|
||||
video_frames: Default::default(),
|
||||
video_encoder,
|
||||
audio_encoder,
|
||||
sys_path: sys_path.map(|x| x.as_ref().to_path_buf()),
|
||||
video_encoder: video_encoder_2,
|
||||
audio_encoder: audio_encoder_2,
|
||||
sys_path: sys_path.as_ref().map(|x| x.as_ref().to_path_buf()),
|
||||
};
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -52,6 +77,7 @@ impl MyEmulator {
|
|||
self.retro.run();
|
||||
|
||||
let vframe = self.video_frames.pop_front().unwrap();
|
||||
eprintln!("video {}x{}, {} audio samples", vframe.width(), vframe.height(), self.audio_buf.len());
|
||||
let mut aframe = frame::Audio::new(
|
||||
format::Sample::I16(format::sample::Type::Packed),
|
||||
self.audio_buf.len(),
|
||||
|
@ -62,8 +88,24 @@ impl MyEmulator {
|
|||
self.audio_buf.clear();
|
||||
|
||||
let mut out = ffmpeg::Packet::empty();
|
||||
self.video_encoder.encode(&vframe, &mut out);
|
||||
self.audio_encoder.encode(&aframe, &mut out);
|
||||
self.video_encoder.encode(&vframe, &mut out).unwrap();
|
||||
self.audio_encoder.encode(&aframe, &mut out).unwrap();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -73,7 +115,7 @@ impl retro::wrapper::Handler for MyEmulator {
|
|||
}
|
||||
|
||||
fn video_refresh(&mut self, data: &[u8], width: u32, height: u32, pitch: u32) {
|
||||
let mut vframe = frame::Video::new(self.video_pixfmt, width, height);
|
||||
let mut vframe = frame::Video::new(self.video_pixel_format, width, height);
|
||||
|
||||
let stride = vframe.stride(0);
|
||||
let pitch = pitch as usize;
|
||||
|
@ -86,7 +128,7 @@ impl retro::wrapper::Handler for MyEmulator {
|
|||
let ffbegin = y * stride;
|
||||
let lrbegin = y * pitch;
|
||||
let min = usize::min(stride, pitch);
|
||||
let x = vplane[ffbegin..(ffbegin + min)].copy_from_slice(
|
||||
vplane[ffbegin..(ffbegin + min)].copy_from_slice(
|
||||
&data[lrbegin..(lrbegin + min)]
|
||||
);
|
||||
}
|
||||
|
@ -106,8 +148,14 @@ impl retro::wrapper::Handler for MyEmulator {
|
|||
stereo_pcm.len()
|
||||
}
|
||||
|
||||
fn get_can_dupe(&mut self) -> Option<bool> { Some(false) }
|
||||
|
||||
fn get_system_directory(&mut self) -> Option<PathBuf> {
|
||||
self.sys_path.clone()
|
||||
}
|
||||
|
||||
fn set_pixel_format(&mut self, format: PixelFormat) -> bool {
|
||||
self.video_pixfmt = match format {
|
||||
self.video_pixel_format = match format {
|
||||
PixelFormat::ARGB1555 => format::Pixel::RGB555,
|
||||
PixelFormat::ARGB8888 => format::Pixel::RGB32,
|
||||
PixelFormat::RGB565 => format::Pixel::RGB565,
|
||||
|
@ -130,221 +178,49 @@ impl retro::wrapper::Handler for MyEmulator {
|
|||
}
|
||||
}
|
||||
|
||||
fn filter(
|
||||
spec: &str,
|
||||
decoder: &codec::decoder::Audio,
|
||||
encoder: &codec::encoder::Audio,
|
||||
) -> Result<filter::Graph, ffmpeg::Error> {
|
||||
let mut filter = filter::Graph::new();
|
||||
|
||||
let args = format!(
|
||||
"time_base={}:sample_rate={}:sample_fmt={}:channel_layout=0x{:x}",
|
||||
decoder.time_base(),
|
||||
decoder.rate(),
|
||||
decoder.format().name(),
|
||||
decoder.channel_layout().bits()
|
||||
);
|
||||
|
||||
filter.add(&filter::find("abuffer").unwrap(), "in", &args)?;
|
||||
filter.add(&filter::find("abuffersink").unwrap(), "out", "")?;
|
||||
|
||||
{
|
||||
let mut out = filter.get("out").unwrap();
|
||||
|
||||
out.set_sample_format(encoder.format());
|
||||
out.set_channel_layout(encoder.channel_layout());
|
||||
out.set_sample_rate(encoder.rate());
|
||||
}
|
||||
|
||||
filter.output("in", 0)?.input("out", 0)?.parse(spec)?;
|
||||
filter.validate()?;
|
||||
|
||||
println!("{}", filter.dump());
|
||||
|
||||
if let Some(codec) = encoder.codec() {
|
||||
if !codec
|
||||
.capabilities()
|
||||
.contains(ffmpeg::codec::capabilities::Capabilities::VARIABLE_FRAME_SIZE)
|
||||
{
|
||||
filter
|
||||
.get("out")
|
||||
.unwrap()
|
||||
.sink()
|
||||
.set_frame_size(encoder.frame_size());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(filter)
|
||||
#[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>,
|
||||
}
|
||||
|
||||
struct Transcoder {
|
||||
stream: usize,
|
||||
filter: filter::Graph,
|
||||
decoder: codec::decoder::Audio,
|
||||
encoder: codec::encoder::Audio,
|
||||
}
|
||||
|
||||
fn transcoder<P: AsRef<Path>>(
|
||||
ictx: &mut format::context::Input,
|
||||
octx: &mut format::context::Output,
|
||||
path: &P,
|
||||
filter_spec: &str,
|
||||
) -> Result<Transcoder, ffmpeg::Error> {
|
||||
let input = ictx
|
||||
.streams()
|
||||
.best(media::Type::Audio)
|
||||
.expect("could not find best audio stream");
|
||||
let mut decoder = input.codec().decoder().audio()?;
|
||||
let codec = ffmpeg::encoder::find(octx.format().codec(path, media::Type::Audio))
|
||||
.expect("failed to find encoder")
|
||||
.audio()?;
|
||||
let global = octx
|
||||
.format()
|
||||
.flags()
|
||||
.contains(ffmpeg::format::flag::Flags::GLOBAL_HEADER);
|
||||
|
||||
decoder.set_parameters(input.parameters())?;
|
||||
|
||||
let mut output = octx.add_stream(codec)?;
|
||||
let mut encoder = output.codec().encoder().audio()?;
|
||||
|
||||
let channel_layout = codec
|
||||
.channel_layouts()
|
||||
.map(|cls| cls.best(decoder.channel_layout().channels()))
|
||||
.unwrap_or(ffmpeg::channel_layout::ChannelLayout::STEREO);
|
||||
|
||||
if global {
|
||||
encoder.set_flags(ffmpeg::codec::flag::Flags::GLOBAL_HEADER);
|
||||
}
|
||||
|
||||
encoder.set_rate(decoder.rate() as i32);
|
||||
encoder.set_channel_layout(channel_layout);
|
||||
encoder.set_channels(channel_layout.channels());
|
||||
encoder.set_format(
|
||||
codec
|
||||
.formats()
|
||||
.expect("unknown supported formats")
|
||||
.next()
|
||||
.unwrap(),
|
||||
);
|
||||
encoder.set_bit_rate(decoder.bit_rate());
|
||||
encoder.set_max_bit_rate(decoder.max_bit_rate());
|
||||
|
||||
encoder.set_time_base((1, decoder.rate() as i32));
|
||||
output.set_time_base((1, decoder.rate() as i32));
|
||||
|
||||
let encoder = encoder.open_as(codec)?;
|
||||
output.set_parameters(&encoder);
|
||||
|
||||
let filter = filter(filter_spec, &decoder, &encoder)?;
|
||||
|
||||
Ok(Transcoder {
|
||||
stream: input.index(),
|
||||
filter: filter,
|
||||
decoder: decoder,
|
||||
encoder: encoder,
|
||||
})
|
||||
}
|
||||
|
||||
// Transcode the `best` audio stream of the input file into a the output file while applying a
|
||||
// given filter. If no filter was specified the stream gets copied (`anull` filter).
|
||||
//
|
||||
// Example 1: Transcode *.mp3 file to *.wmv while speeding it up
|
||||
// transcode-audio in.mp3 out.wmv "atempo=1.2"
|
||||
//
|
||||
// Example 2: Overlay an audio file
|
||||
// transcode-audio in.mp3 out.mp3 "amovie=overlay.mp3 [ov]; [in][ov] amerge [out]"
|
||||
//
|
||||
// Example 3: Seek to a specified position (in seconds)
|
||||
// transcode-audio in.mp3 out.mp3 anull 30
|
||||
fn main() {
|
||||
let opt: Opt = Opt::from_args();
|
||||
ffmpeg::init().unwrap();
|
||||
|
||||
let input = env::args().nth(1).expect("missing input");
|
||||
let output = env::args().nth(2).expect("missing output");
|
||||
let filter = env::args().nth(3).unwrap_or_else(|| "anull".to_owned());
|
||||
let seek = env::args().nth(4).and_then(|s| s.parse::<i64>().ok());
|
||||
|
||||
let mut ictx = format::input(&input).unwrap();
|
||||
let mut octx = format::output(&output).unwrap();
|
||||
let mut transcoder = transcoder(&mut ictx, &mut octx, &output, &filter).unwrap();
|
||||
|
||||
if let Some(position) = seek {
|
||||
// If the position was given in seconds, rescale it to ffmpegs base timebase.
|
||||
let position = position.rescale((1, 1), rescale::TIME_BASE);
|
||||
// If this seek was embedded in the transcoding loop, a call of `flush()`
|
||||
// for every opened buffer after the successful seek would be advisable.
|
||||
ictx.seek(position, ..position).unwrap();
|
||||
}
|
||||
|
||||
octx.set_metadata(ictx.metadata().to_owned());
|
||||
octx.write_header().unwrap();
|
||||
|
||||
let in_time_base = transcoder.decoder.time_base();
|
||||
let out_time_base = octx.stream(0).unwrap().time_base();
|
||||
|
||||
let mut decoded = frame::Audio::empty();
|
||||
let mut encoded = ffmpeg::Packet::empty();
|
||||
|
||||
for (stream, mut packet) in ictx.packets() {
|
||||
if stream.index() == transcoder.stream {
|
||||
packet.rescale_ts(stream.time_base(), in_time_base);
|
||||
|
||||
if let Ok(true) = transcoder.decoder.decode(&packet, &mut decoded) {
|
||||
let timestamp = decoded.timestamp();
|
||||
decoded.set_pts(timestamp);
|
||||
|
||||
transcoder
|
||||
.filter
|
||||
.get("in")
|
||||
.unwrap()
|
||||
.source()
|
||||
.add(&decoded)
|
||||
.unwrap();
|
||||
|
||||
while let Ok(..) = transcoder
|
||||
.filter
|
||||
.get("out")
|
||||
.unwrap()
|
||||
.sink()
|
||||
.frame(&mut decoded)
|
||||
{
|
||||
if let Ok(true) = transcoder.encoder.encode(&decoded, &mut encoded) {
|
||||
encoded.set_stream(0);
|
||||
encoded.rescale_ts(in_time_base, out_time_base);
|
||||
encoded.write_interleaved(&mut octx).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
transcoder
|
||||
.filter
|
||||
.get("in")
|
||||
let mut octx = format::output(&opt.video).unwrap();
|
||||
let vcodec = ffmpeg::encoder::find(octx.format().codec(&opt.video, media::Type::Video))
|
||||
.unwrap()
|
||||
.source()
|
||||
.flush()
|
||||
.video()
|
||||
.unwrap();
|
||||
let acodec = ffmpeg::encoder::find(octx.format().codec(&opt.video, media::Type::Audio))
|
||||
.unwrap()
|
||||
.audio()
|
||||
.unwrap();
|
||||
|
||||
while let Ok(..) = transcoder
|
||||
.filter
|
||||
.get("out")
|
||||
.unwrap()
|
||||
.sink()
|
||||
.frame(&mut decoded)
|
||||
{
|
||||
if let Ok(true) = transcoder.encoder.encode(&decoded, &mut encoded) {
|
||||
encoded.set_stream(0);
|
||||
encoded.rescale_ts(in_time_base, out_time_base);
|
||||
encoded.write_interleaved(&mut octx).unwrap();
|
||||
}
|
||||
}
|
||||
let mut video_encoder = octx.add_stream(vcodec).unwrap().codec().encoder().video().unwrap();
|
||||
let mut audio_encoder = octx.add_stream(acodec).unwrap().codec().encoder().audio().unwrap();
|
||||
|
||||
if let Ok(true) = transcoder.encoder.flush(&mut encoded) {
|
||||
encoded.set_stream(0);
|
||||
encoded.rescale_ts(in_time_base, out_time_base);
|
||||
encoded.write_interleaved(&mut octx).unwrap();
|
||||
video_encoder.set_bit_rate(64000);
|
||||
audio_encoder.set_bit_rate(64000);
|
||||
|
||||
let mut emu = MyEmulator::new(opt.core, &opt.system, video_encoder, audio_encoder);
|
||||
emu.load_game(opt.rom);
|
||||
|
||||
octx.write_header().unwrap();
|
||||
|
||||
for _ in 0..60*60 {
|
||||
emu.run();
|
||||
}
|
||||
|
||||
octx.write_trailer().unwrap();
|
||||
|
|
Loading…
Reference in New Issue