atranscoder-rpc/src/transcoder.rs

219 lines
7.0 KiB
Rust
Raw Normal View History

2024-05-25 20:32:54 +03:00
extern crate ffmpeg_next as ffmpeg;
use std::error::Error;
use std::sync::Arc;
use ffmpeg::{codec, filter, format, frame, media};
2024-05-25 23:58:12 +03:00
use ffmpeg_next::error::EAGAIN;
2024-05-25 20:32:54 +03:00
pub struct Transcoder {
pub(crate) stream: usize,
filter: filter::Graph,
decoder: codec::decoder::Audio,
encoder: codec::encoder::Audio,
pub(crate) in_time_base: ffmpeg::Rational,
out_time_base: ffmpeg::Rational,
}
pub struct TranscoderParams {
pub codec: String,
pub bit_rate: usize,
pub max_bit_rate: usize,
pub sample_rate: i32,
pub channel_layout: ffmpeg::channel_layout::ChannelLayout,
}
impl Transcoder {
pub fn new(
ictx: &mut format::context::Input,
octx: &mut format::context::Output,
params: TranscoderParams,
2024-05-25 21:29:53 +03:00
) -> Result<Transcoder, Box<dyn Error>> {
2024-05-25 20:32:54 +03:00
let input = ictx
.streams()
.best(media::Type::Audio)
2024-05-25 23:17:54 +03:00
.ok_or("could not find best audio stream")?;
2024-05-25 20:32:54 +03:00
let context = codec::context::Context::from_parameters(input.parameters())?;
2024-05-25 23:17:54 +03:00
let mut decoder = context.decoder().audio().map_err(|err| {
format!("couldn't find decoder for input file: {}", err)
})?;
let codec = ffmpeg::encoder::find_by_name(&*params.codec)
.ok_or_else(|| format!("couldn't find codec with name: {}", params.codec))?
.audio()?;
let global = octx.format().flags().contains(format::flag::Flags::GLOBAL_HEADER);
2024-05-25 20:32:54 +03:00
decoder.set_parameters(input.parameters())?;
let mut output = octx.add_stream(codec)?;
let context = codec::context::Context::from_parameters(output.parameters())?;
let mut encoder = context.encoder().audio()?;
if global {
encoder.set_flags(codec::flag::Flags::GLOBAL_HEADER);
}
let sample_rate = if params.sample_rate > 0 {
params.sample_rate
} else {
decoder.rate() as i32
};
encoder.set_rate(sample_rate);
encoder.set_channel_layout(params.channel_layout);
2024-05-25 23:17:54 +03:00
2024-05-25 20:32:54 +03:00
#[cfg(not(feature = "ffmpeg_7_0"))]
2024-05-25 23:17:54 +03:00
encoder.set_channels(params.channel_layout.channels());
2024-05-25 20:32:54 +03:00
encoder.set_format(
codec
.formats()
2024-05-25 23:17:54 +03:00
.ok_or_else(|| format!("failed to get supported formats for codec: {}", codec.name()))?
2024-05-25 20:32:54 +03:00
.next()
2024-05-25 23:17:54 +03:00
.ok_or("no supported formats found for codec")?,
2024-05-25 20:32:54 +03:00
);
2024-05-25 23:17:54 +03:00
encoder.set_bit_rate(if params.bit_rate > 0 { params.bit_rate } else { decoder.bit_rate() });
encoder.set_max_bit_rate(if params.max_bit_rate > 0 { params.max_bit_rate } else { decoder.max_bit_rate() });
2024-05-25 20:32:54 +03:00
encoder.set_time_base((1, sample_rate));
output.set_time_base((1, sample_rate));
2024-05-25 23:17:54 +03:00
let in_time_base = decoder.time_base();
2024-05-25 20:32:54 +03:00
let encoder = encoder.open_as(codec)?;
output.set_parameters(&encoder);
2024-05-25 23:17:54 +03:00
let filter = filter_graph("anull", &decoder, &encoder)?;
2024-05-25 20:32:54 +03:00
Ok(Transcoder {
stream: input.index(),
filter,
decoder,
encoder,
in_time_base,
2024-05-25 23:17:54 +03:00
out_time_base: output.time_base(),
2024-05-25 20:32:54 +03:00
})
}
fn send_frame_to_encoder(&mut self, frame: &ffmpeg::Frame) -> Result<(), ffmpeg::Error> {
self.encoder.send_frame(frame)
}
2024-05-25 23:58:12 +03:00
pub(crate) fn send_eof_to_encoder(&mut self) -> Result<(), ffmpeg::Error> {
self.encoder.send_eof()
2024-05-25 20:32:54 +03:00
}
2024-05-25 21:29:53 +03:00
pub(crate) fn receive_and_process_encoded_packets(
&mut self,
octx: &mut format::context::Output,
) -> Result<(), Box<dyn Error>> {
2024-05-25 20:32:54 +03:00
let mut encoded = ffmpeg::Packet::empty();
while self.encoder.receive_packet(&mut encoded).is_ok() {
encoded.set_stream(0);
encoded.rescale_ts(self.in_time_base, self.out_time_base);
2024-05-25 21:29:53 +03:00
2024-05-25 23:17:54 +03:00
if let Err(err) = encoded.write_interleaved(octx) {
return Err(err.into());
2024-05-25 21:29:53 +03:00
}
2024-05-25 20:32:54 +03:00
}
2024-05-25 21:29:53 +03:00
Ok(())
2024-05-25 20:32:54 +03:00
}
2024-05-25 23:58:12 +03:00
fn add_frame_to_filter(&mut self, frame: &ffmpeg::Frame) -> Result<(), ffmpeg::Error> {
self.filter.get("in").unwrap().source().add(frame)
2024-05-25 20:32:54 +03:00
}
2024-05-25 23:58:12 +03:00
pub(crate) fn flush_filter(&mut self) -> Result<(), ffmpeg::Error> {
self.filter.get("in").unwrap().source().flush()
2024-05-25 20:32:54 +03:00
}
2024-05-25 21:29:53 +03:00
pub(crate) fn get_and_process_filtered_frames(
&mut self,
octx: &mut format::context::Output,
) -> Result<(), Box<dyn Error>> {
2024-05-25 20:32:54 +03:00
let mut filtered = frame::Audio::empty();
2024-05-25 21:29:53 +03:00
loop {
2024-05-25 23:17:54 +03:00
let mut ctx = self.filter.get("out").ok_or("cannot get context from filter")?;
2024-05-25 21:29:53 +03:00
2024-05-25 23:58:12 +03:00
if let Err(err) = ctx.sink().frame(&mut filtered) {
if err != ffmpeg::Error::Eof {
return Err(err.into());
}
return Ok(());
2024-05-25 21:29:53 +03:00
}
2024-05-25 23:17:54 +03:00
self.send_frame_to_encoder(&filtered)?;
self.receive_and_process_encoded_packets(octx)?;
2024-05-25 20:32:54 +03:00
}
}
2024-05-25 21:29:53 +03:00
pub(crate) fn send_packet_to_decoder(
&mut self,
packet: &ffmpeg::Packet,
) -> Result<(), Box<dyn Error>> {
2024-05-25 23:17:54 +03:00
self.decoder.send_packet(packet).map_err(|err| err.into())
2024-05-25 20:32:54 +03:00
}
2024-05-25 21:29:53 +03:00
pub(crate) fn send_eof_to_decoder(&mut self) -> Result<(), Box<dyn Error>> {
2024-05-25 23:17:54 +03:00
self.decoder.send_eof().map_err(|err| err.into())
2024-05-25 20:32:54 +03:00
}
2024-05-25 21:29:53 +03:00
pub(crate) fn receive_and_process_decoded_frames(
&mut self,
octx: &mut format::context::Output,
) -> Result<(), Box<dyn Error>> {
2024-05-25 20:32:54 +03:00
let mut decoded = frame::Audio::empty();
while self.decoder.receive_frame(&mut decoded).is_ok() {
let timestamp = decoded.timestamp();
decoded.set_pts(timestamp);
2024-05-25 23:58:12 +03:00
self.add_frame_to_filter(&decoded)?;
if let Err(mut err) = self.get_and_process_filtered_frames(octx) {
let expected = ffmpeg::Error::Other { errno: EAGAIN };
if err.downcast_mut::<ffmpeg::error::Error>().ok_or(ffmpeg::Error::Bug) == Err(expected) {
continue
}
}
2024-05-25 20:32:54 +03:00
}
2024-05-25 21:29:53 +03:00
Ok(())
2024-05-25 20:32:54 +03:00
}
}
2024-05-25 23:17:54 +03:00
fn filter_graph(
2024-05-25 20:32:54 +03:00
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", "")?;
2024-05-25 23:17:54 +03:00
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());
2024-05-25 20:32:54 +03:00
filter.output("in", 0)?.input("out", 0)?.parse(spec)?;
filter.validate()?;
println!("{}", filter.dump());
if let Some(codec) = encoder.codec() {
2024-05-25 23:17:54 +03:00
if !codec.capabilities().contains(codec::capabilities::Capabilities::VARIABLE_FRAME_SIZE) {
filter.get("out").unwrap().sink().set_frame_size(encoder.frame_size());
2024-05-25 20:32:54 +03:00
}
}
Ok(filter)
}