1use std::path::PathBuf;
23
24use rff_codec::{CodecParams, Decoder, Encoder};
25use rff_core::{
26 AudioFrame, CodecId, ColorRange, Dictionary, Error, Frame, MediaType, Packet, PixelFormat,
27 Result, SampleFormat, VideoFrame,
28};
29use rff_filter::{FilterChain, FilterComplex};
30
31use rff_format::{Muxer, Stream};
32use rff_resample::Resampler;
33
34use crate::Engine;
35
36mod phases {
43 use std::sync::atomic::{AtomicU64, Ordering::Relaxed};
44 use std::sync::OnceLock;
45
46 pub static OPEN: AtomicU64 = AtomicU64::new(0);
47 pub static DEMUX: AtomicU64 = AtomicU64::new(0);
48 pub static DECODE: AtomicU64 = AtomicU64::new(0);
49 pub static CONFORM: AtomicU64 = AtomicU64::new(0);
50 pub static ENCODE: AtomicU64 = AtomicU64::new(0);
51 pub static MUX: AtomicU64 = AtomicU64::new(0);
52
53 pub fn on() -> bool {
54 static ON: OnceLock<bool> = OnceLock::new();
55 *ON.get_or_init(|| std::env::var_os("RFF_PHASES").is_some())
56 }
57
58 pub fn time<T>(acc: &AtomicU64, f: impl FnOnce() -> T) -> T {
60 if !on() {
61 return f();
62 }
63 let t = std::time::Instant::now();
64 let r = f();
65 acc.fetch_add(t.elapsed().as_nanos() as u64, Relaxed);
66 r
67 }
68
69 pub fn report(total: std::time::Duration) {
70 let total_ns = total.as_nanos().max(1) as f64;
71 eprintln!("--- rff phases (total {:.1} ms) ---", total_ns / 1e6);
72 let mut sum = 0u64;
73 for (name, acc) in [
74 ("open", &OPEN),
75 ("demux", &DEMUX),
76 ("decode", &DECODE),
77 ("conform", &CONFORM),
78 ("encode", &ENCODE),
79 ("mux", &MUX),
80 ] {
81 let ns = acc.swap(0, Relaxed);
82 sum += ns;
83 eprintln!(
84 " {name:<8} {:>9.1} ms {:>5.1}%",
85 ns as f64 / 1e6,
86 100.0 * ns as f64 / total_ns
87 );
88 }
89 let rest = total_ns - sum as f64;
90 eprintln!(
91 " {:<8} {:>9.1} ms {:>5.1}%",
92 "other",
93 rest / 1e6,
94 100.0 * rest / total_ns
95 );
96 }
97}
98
99fn is_full_range_codec(codec: CodecId) -> bool {
103 matches!(
104 codec,
105 CodecId::Jpeg | CodecId::Png | CodecId::Gif | CodecId::Webp
106 )
107}
108
109fn effective_color_range(stream: &Stream) -> ColorRange {
116 match stream.color_range {
117 ColorRange::Unspecified if is_full_range_codec(stream.codec_id) => ColorRange::Full,
118 ColorRange::Unspecified => ColorRange::Limited,
119 explicit => explicit,
120 }
121}
122
123#[derive(Debug, Clone)]
125pub struct InputSpec {
126 pub path: PathBuf,
127 pub format: Option<String>,
130}
131
132#[derive(Debug, Clone)]
134pub struct StreamCodec {
135 pub codec: CodecId,
136 pub options: Dictionary,
138 pub sample_format: Option<SampleFormat>,
142}
143
144#[derive(Debug, Clone)]
146pub enum MapSelector {
147 All,
149 Kind(MediaType),
151 Index(usize),
153}
154
155#[derive(Debug, Clone)]
157pub struct MapSpec {
158 pub input: usize,
159 pub selector: MapSelector,
160}
161
162#[derive(Debug, Clone, Default)]
164pub struct OutputSpec {
165 pub path: PathBuf,
166 pub format: Option<String>,
169 pub video_codec: Option<StreamCodec>,
171 pub audio_codec: Option<StreamCodec>,
173 pub subtitle_codec: Option<CodecId>,
177 pub video_filters: Option<String>,
180 pub filter_complex: Option<String>,
183 pub maps: Vec<MapSpec>,
186 pub disabled: Vec<MediaType>,
189 pub overwrite: bool,
191 pub max_video_frames: Option<u64>,
197 pub trim_start: Option<f64>,
201 pub trim_end: Option<f64>,
203 pub frame_rate: Option<(u32, u32)>,
206 pub audio_rate: Option<u32>,
209 pub audio_channels: Option<u16>,
211 pub audio_filters: Option<String>,
213 pub metadata: Dictionary,
215 pub format_options: Dictionary,
218}
219
220#[derive(Debug, Clone, Default)]
222pub struct TranscodeSpec {
223 pub inputs: Vec<InputSpec>,
224 pub outputs: Vec<OutputSpec>,
225}
226
227#[derive(Debug, Clone, Default)]
230pub struct TranscodeReport {
231 pub packets_written: u64,
232 pub frames_decoded: u64,
233}
234
235#[derive(Clone, Copy)]
237struct Trim {
238 start: f64,
239 end: Option<f64>,
240}
241
242impl Trim {
243 fn from_spec(o: &OutputSpec) -> Option<Trim> {
244 if o.trim_start.is_none() && o.trim_end.is_none() {
245 return None;
246 }
247 Some(Trim {
248 start: o.trim_start.unwrap_or(0.0).max(0.0),
249 end: o.trim_end,
250 })
251 }
252
253 fn keeps(&self, t: f64) -> bool {
254 t >= self.start - 1e-9 && self.end.map_or(true, |e| t < e - 1e-9)
255 }
256}
257
258struct FpsConv {
263 num: u32,
264 den: u32,
265 next_slot: i64,
266 last: Option<VideoFrame>,
267}
268
269impl FpsConv {
270 fn new(num: u32, den: u32) -> FpsConv {
271 FpsConv {
272 num: num.max(1),
273 den: den.max(1),
274 next_slot: 0,
275 last: None,
276 }
277 }
278
279 fn slot_time(&self, slot: i64) -> f64 {
280 slot as f64 * self.den as f64 / self.num as f64
281 }
282
283 fn push(&mut self, frame: VideoFrame, t: f64) -> Vec<VideoFrame> {
286 let mut out = Vec::new();
287 while let Some(last) = &self.last {
288 if self.slot_time(self.next_slot) < t - 1e-9 {
289 let mut f = last.clone();
290 f.pts = Some(self.next_slot);
291 out.push(f);
292 self.next_slot += 1;
293 } else {
294 break;
295 }
296 }
297 self.last = Some(frame);
298 out
299 }
300
301 fn finish(&mut self) -> Option<VideoFrame> {
303 self.last.take().map(|mut f| {
304 f.pts = Some(self.next_slot);
305 self.next_slot += 1;
306 f
307 })
308 }
309}
310
311enum StreamOp {
313 Skip,
315 Copy(CopyOp),
317 Transcode(Box<TranscodeOp>),
319}
320
321struct CopyOp {
325 out_index: usize,
326 is_video: bool,
327 start_ticks: i64,
329 end_ticks: Option<i64>,
330 trimming: bool,
331 started: bool,
333}
334
335impl CopyOp {
336 fn process(&mut self, mut packet: Packet) -> Option<Packet> {
338 if self.trimming {
339 let ts = packet.pts.or(packet.dts).unwrap_or(0);
340 if ts < self.start_ticks {
341 return None;
342 }
343 if let Some(end) = self.end_ticks {
344 if ts >= end {
345 return None;
346 }
347 }
348 if self.is_video && !self.started {
349 if !packet.flags.keyframe {
350 return None; }
352 self.started = true;
353 }
354 packet.pts = packet.pts.map(|p| p - self.start_ticks);
355 packet.dts = packet.dts.map(|d| d - self.start_ticks);
356 }
357 packet.stream_index = self.out_index;
358 Some(packet)
359 }
360}
361
362struct TranscodeOp {
364 decoder: Box<dyn Decoder>,
365 encoder: Box<dyn Encoder>,
366 filters: FilterChain,
367 overlay: Option<(VideoFrame, u32, u32)>,
370 in_time_base: rff_core::Rational,
372 is_video: bool,
373 trim: Option<Trim>,
375 shift_ticks: i64,
376 audio_clock_samples: u64,
378 fps: Option<FpsConv>,
380 audio_chain: rff_filter::AudioFilterChain,
382 target_channels: u16,
384 target_rate: u32,
386 target_sample_format: Option<SampleFormat>,
388 resampler: Option<Resampler>,
390 accepted_formats: Option<Vec<PixelFormat>>,
392 pixel_converter: Option<FilterChain>,
394 source_range: ColorRange,
396 out_index: usize,
397 max_video_frames: Option<u64>,
399 video_frames_sent: u64,
400 pending_pts: std::collections::VecDeque<Option<i64>>,
404}
405
406fn apply_overlay(overlay: &Option<(VideoFrame, u32, u32)>, frame: Frame) -> Result<Frame> {
409 match (overlay, frame) {
410 (Some((over, x, y)), Frame::Video(v)) => {
411 Ok(Frame::Video(rff_filter::overlay(v, over, *x, *y)?))
412 }
413 (_, frame) => Ok(frame),
414 }
415}
416
417fn apply_filters(filters: &mut FilterChain, frame: Frame) -> Result<Frame> {
420 if filters.is_empty() {
421 return Ok(frame);
422 }
423 match frame {
424 Frame::Video(v) => Ok(Frame::Video(filters.apply(v)?)),
425 other => Ok(other),
426 }
427}
428
429fn nearest_rate(rates: &[u32], target: u32) -> u32 {
431 rates
432 .iter()
433 .copied()
434 .min_by_key(|r| (*r as i64 - target as i64).abs())
435 .unwrap_or(target)
436}
437
438fn audio_to_f32(af: &AudioFrame) -> Result<Vec<f32>> {
440 match af.format {
441 SampleFormat::F32 => Ok(af.planes[0]
442 .chunks_exact(4)
443 .map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]]))
444 .collect()),
445 SampleFormat::S16 => Ok(af.planes[0]
446 .chunks_exact(2)
447 .map(|b| i16::from_le_bytes([b[0], b[1]]) as f32 / 32768.0)
448 .collect()),
449 other => Err(Error::unsupported(format!(
450 "resample: sample format `{}` (need interleaved s16/f32)",
451 other.name()
452 ))),
453 }
454}
455
456fn f32_le_bytes(samples: &[f32]) -> Vec<u8> {
460 let mut bytes = vec![0u8; samples.len() * 4];
461 for (d, s) in bytes.chunks_exact_mut(4).zip(samples) {
462 d.copy_from_slice(&s.to_le_bytes());
463 }
464 bytes
465}
466
467fn f32_to_s16(s: f32) -> i16 {
470 (s * 32768.0).round().clamp(-32768.0, 32767.0) as i16
471}
472
473fn f32_frame(samples: Vec<f32>, rate: u32, channels: u16, pts: Option<i64>) -> Frame {
475 let bytes = f32_le_bytes(&samples);
476 Frame::Audio(AudioFrame {
477 sample_rate: rate,
478 channels,
479 format: SampleFormat::F32,
480 samples: samples.len() / channels.max(1) as usize,
481 planes: vec![bytes],
482 pts,
483 })
484}
485
486fn conform_sample_format(target: Option<SampleFormat>, frame: Frame) -> Result<Frame> {
491 let Some(target) = target else {
492 return Ok(frame);
493 };
494 let Frame::Audio(af) = frame else {
495 return Ok(frame);
496 };
497 if af.format == target {
498 return Ok(Frame::Audio(af));
499 }
500 let src = &af.planes[0];
506 let plane = match (af.format, target) {
507 (SampleFormat::F32, SampleFormat::S16) => {
508 let mut out = vec![0u8; src.len() / 4 * 2];
509 for (d, b) in out.chunks_exact_mut(2).zip(src.chunks_exact(4)) {
510 let s = f32::from_le_bytes([b[0], b[1], b[2], b[3]]);
511 d.copy_from_slice(&f32_to_s16(s).to_le_bytes());
512 }
513 out
514 }
515 (SampleFormat::S16, SampleFormat::F32) => {
516 let mut out = vec![0u8; src.len() / 2 * 4];
517 for (d, b) in out.chunks_exact_mut(4).zip(src.chunks_exact(2)) {
518 let s = i16::from_le_bytes([b[0], b[1]]) as f32 / 32768.0;
519 d.copy_from_slice(&s.to_le_bytes());
520 }
521 out
522 }
523 _ => {
524 audio_to_f32(&af)?;
526 return Err(Error::unsupported(format!(
527 "sample format conversion to `{}` (only interleaved s16/f32)",
528 target.name()
529 )));
530 }
531 };
532 let planes = vec![plane];
533 Ok(Frame::Audio(AudioFrame {
534 sample_rate: af.sample_rate,
535 channels: af.channels,
536 format: target,
537 samples: af.samples,
538 planes,
539 pts: af.pts,
540 }))
541}
542
543fn conform_audio(
546 resampler: &mut Option<Resampler>,
547 target_rate: u32,
548 frame: Frame,
549) -> Result<Frame> {
550 if target_rate == 0 {
551 return Ok(frame);
552 }
553 let Frame::Audio(af) = frame else {
554 return Ok(frame);
555 };
556 if af.sample_rate == target_rate {
557 return Ok(Frame::Audio(af));
558 }
559 let rs =
560 resampler.get_or_insert_with(|| Resampler::new(af.sample_rate, target_rate, af.channels));
561 let out = rs.process(&audio_to_f32(&af)?);
562 let pts = af
563 .pts
564 .map(|p| p * target_rate as i64 / af.sample_rate.max(1) as i64);
565 Ok(f32_frame(out, target_rate, af.channels, pts))
566}
567
568fn conform_video(
576 converter: &mut Option<FilterChain>,
577 accepted: &Option<Vec<PixelFormat>>,
578 source_range: ColorRange,
579 frame: Frame,
580) -> Result<Frame> {
581 let Some(accepted) = accepted else {
582 return Ok(frame);
583 };
584 let Frame::Video(vf) = frame else {
585 return Ok(frame);
586 };
587 if accepted.is_empty() || accepted.contains(&vf.format) {
588 return Ok(Frame::Video(vf));
589 }
590 if converter.is_none() {
591 let target = accepted
593 .iter()
594 .find(|f| matches!(f, PixelFormat::Rgb24 | PixelFormat::Yuv420p))
595 .ok_or_else(|| {
596 Error::unsupported(format!(
597 "no conversion from `{}` to any format this encoder accepts",
598 vf.format.name()
599 ))
600 })?;
601 let range = match source_range {
604 ColorRange::Limited => "limited",
605 _ => "full",
606 };
607 *converter = Some(FilterChain::parse(&format!(
608 "format={}:bt601:{}",
609 target.name(),
610 range
611 ))?);
612 }
613 let chain = converter.as_mut().expect("just built");
614 Ok(Frame::Video(chain.apply(vf)?))
615}
616
617fn conform_channels(target: u16, frame: Frame) -> Result<Frame> {
620 if target == 0 {
621 return Ok(frame);
622 }
623 let Frame::Audio(af) = frame else {
624 return Ok(frame);
625 };
626 if af.channels == target {
627 return Ok(Frame::Audio(af));
628 }
629 let samples = audio_to_f32(&af)?;
630 let mixed: Vec<f32> = match (af.channels, target) {
631 (2, 1) => samples
632 .chunks_exact(2)
633 .map(|p| 0.5 * (p[0] + p[1]))
634 .collect(),
635 (1, 2) => samples.iter().flat_map(|s| [*s, *s]).collect(),
636 (from, to) => {
637 return Err(Error::unsupported(format!(
638 "-ac: only 1<->2 channel mixing supported (input {from} ch, requested {to})"
639 )))
640 }
641 };
642 Ok(f32_frame(mixed, af.sample_rate, target, af.pts))
643}
644
645impl TranscodeOp {
646 fn frame_time(&self, frame: &Frame) -> Result<f64> {
650 let tb = self.in_time_base;
651 let tick = if tb.num > 0 && tb.den > 0 {
652 tb.num as f64 / tb.den as f64
653 } else {
654 0.001
655 };
656 match frame.pts() {
657 Some(p) => Ok(p as f64 * tick),
658 None => match frame {
659 Frame::Audio(af) => {
660 Ok(self.audio_clock_samples as f64 / af.sample_rate.max(1) as f64)
661 }
662 Frame::Video(_) => Err(Error::unsupported(
663 "-ss/-t/-to/-r need timestamps and this video stream has none",
664 )),
665 },
666 }
667 }
668
669 fn handle_frame(
672 &mut self,
673 frame: Frame,
674 muxer: &mut dyn Muxer,
675 report: &mut TranscodeReport,
676 ) -> Result<()> {
677 report.frames_decoded += 1;
678 let needs_time = self.trim.is_some() || (self.is_video && self.fps.is_some());
679 let t = if needs_time {
680 Some(self.frame_time(&frame)?)
681 } else {
682 None
683 };
684 if let Frame::Audio(af) = &frame {
686 self.audio_clock_samples += af.samples as u64;
687 }
688 let mut frame = frame;
689 if let (Some(trim), Some(t)) = (self.trim, t) {
690 match frame {
691 Frame::Audio(af) => {
694 match rff_filter::trim_audio_frame(af, Some(t), Some(trim.start), trim.end)? {
695 Some(af) => frame = Frame::Audio(af),
696 None => return Ok(()),
697 }
698 }
699 Frame::Video(_) => {
700 if !trim.keeps(t) {
701 return Ok(());
702 }
703 }
704 }
705 }
706 if self.shift_ticks != 0 {
708 match &mut frame {
709 Frame::Video(v) => v.pts = v.pts.map(|p| p - self.shift_ticks),
710 Frame::Audio(a) => a.pts = a.pts.map(|p| p - self.shift_ticks),
711 }
712 }
713 match frame {
714 Frame::Video(v) if self.fps.is_some() => {
715 let t_out = t.expect("fps requires time") - self.trim.map_or(0.0, |w| w.start);
716 let mut fps = self.fps.take().expect("checked");
717 let due = fps.push(v, t_out);
718 self.fps = Some(fps);
719 for f in due {
720 self.emit(Frame::Video(f), muxer, report)?;
721 }
722 Ok(())
723 }
724 other => self.emit(other, muxer, report),
725 }
726 }
727
728 fn emit(
731 &mut self,
732 frame: Frame,
733 muxer: &mut dyn Muxer,
734 report: &mut TranscodeReport,
735 ) -> Result<()> {
736 if let Some(limit) = self.max_video_frames {
737 if matches!(frame, Frame::Video(_)) {
738 if self.video_frames_sent >= limit {
739 return Ok(());
740 }
741 self.video_frames_sent += 1;
742 }
743 }
744 let Some(frame) = phases::time(&phases::CONFORM, || self.conform(frame))? else {
745 return Ok(()); };
747 if self.is_video {
748 self.pending_pts.push_back(frame.pts());
749 }
750 phases::time(&phases::ENCODE, || self.encoder.send_frame(&frame))?;
751 self.drain(muxer, report)
752 }
753
754 fn conform(&mut self, frame: Frame) -> Result<Option<Frame>> {
757 let frame = apply_filters(&mut self.filters, frame)?;
758 let frame = apply_overlay(&self.overlay, frame)?;
759 let frame = conform_channels(self.target_channels, frame)?;
760 let frame = match frame {
761 Frame::Audio(af) if !self.audio_chain.is_empty() => {
762 let tb = self.in_time_base;
763 let t = af
764 .pts
765 .map(|p| p as f64 * tb.num as f64 / tb.den.max(1) as f64);
766 match self.audio_chain.apply(af, t)? {
767 Some(af) => Frame::Audio(af),
768 None => return Ok(None),
769 }
770 }
771 other => other,
772 };
773 let frame = conform_audio(&mut self.resampler, self.target_rate, frame)?;
774 let frame = conform_sample_format(self.target_sample_format, frame)?;
775 let frame = conform_video(
776 &mut self.pixel_converter,
777 &self.accepted_formats,
778 self.source_range,
779 frame,
780 )?;
781 Ok(Some(frame))
782 }
783
784 fn drain(&mut self, muxer: &mut dyn Muxer, report: &mut TranscodeReport) -> Result<()> {
789 loop {
790 match phases::time(&phases::ENCODE, || self.encoder.receive_packet()) {
791 Ok(mut packet) => {
792 if self.is_video {
793 let queued = self.pending_pts.pop_front().flatten();
794 if packet.pts.is_none() {
795 packet.pts = queued;
796 }
797 }
798 packet.stream_index = self.out_index;
799 phases::time(&phases::MUX, || muxer.write_packet(&packet))?;
800 report.packets_written += 1;
801 }
802 Err(Error::Again) | Err(Error::Eof) => break,
803 Err(e) => return Err(e),
804 }
805 }
806 Ok(())
807 }
808
809 fn finish(&mut self, muxer: &mut dyn Muxer, report: &mut TranscodeReport) -> Result<()> {
812 self.decoder.flush();
813 loop {
814 match phases::time(&phases::DECODE, || self.decoder.receive_frame()) {
815 Ok(frame) => self.handle_frame(frame, muxer, report)?,
816 Err(Error::Again) | Err(Error::Eof) => break,
817 Err(e) => return Err(e),
818 }
819 }
820 if let Some(mut fps) = self.fps.take() {
821 let held = fps.finish();
822 self.fps = Some(fps);
823 if let Some(f) = held {
824 self.emit(Frame::Video(f), muxer, report)?;
825 }
826 }
827 if let Some(rs) = &mut self.resampler {
828 let tail = rs.finish();
829 if !tail.is_empty() {
830 let frame = f32_frame(tail, rs.out_rate(), rs.channels(), None);
831 let frame = conform_sample_format(self.target_sample_format, frame)?;
832 self.encoder.send_frame(&frame)?;
833 self.drain(muxer, report)?;
834 }
835 }
836 phases::time(&phases::ENCODE, || self.encoder.flush());
837 self.drain(muxer, report)
838 }
839}
840
841pub fn run(engine: &Engine, spec: &TranscodeSpec) -> Result<TranscodeReport> {
847 if spec.inputs.is_empty() {
848 return Err(Error::Option("no input files specified".into()));
849 }
850 if spec.outputs.is_empty() {
851 return Err(Error::Option("no output file specified".into()));
852 }
853 if spec.outputs.len() > 1 {
854 return Err(Error::unsupported("multiple outputs are not supported yet"));
855 }
856 let output = &spec.outputs[0];
857
858 let overlay_xy = output
861 .filter_complex
862 .as_deref()
863 .map(FilterComplex::parse)
864 .transpose()?
865 .and_then(|fc| fc.overlay);
866 let overlay_input = match overlay_xy {
867 Some(_) if spec.inputs.len() >= 2 => Some(spec.inputs.len() - 1),
868 Some(_) => {
869 return Err(Error::Option(
870 "filter_complex overlay needs a second input (the overlay image/video)".into(),
871 ))
872 }
873 None => None,
874 };
875
876 let t_start = phases::on().then(std::time::Instant::now);
881 let mut demuxers: Vec<Box<dyn rff_format::Demuxer>> = Vec::new();
882 let mut input_streams: Vec<Vec<Stream>> = Vec::new();
883 for input in &spec.inputs {
884 let (demuxer, streams) = phases::time(&phases::OPEN, || -> Result<_> {
885 let (in_format, source) = open_input(engine, input)?;
886 let mut demuxer = source.open_demuxer(engine, &in_format)?;
887 let streams = demuxer.read_header()?;
888 Ok((demuxer, streams))
889 })?;
890 input_streams.push(streams);
891 demuxers.push(demuxer);
892 }
893
894 let mut selection = select_streams(&input_streams, output)?;
896 if let Some(oin) = overlay_input {
898 selection.retain(|(inp, _)| *inp != oin);
899 }
900 if selection.is_empty() {
901 return Err(Error::unsupported("no streams selected for the output"));
902 }
903
904 let mut output_owned;
910 let output = if output.video_codec.is_none() {
911 let format_name = resolve_output_format(engine, output).ok();
912 let required = match format_name.as_deref() {
915 Some("yuv4mpegpipe") => Some((CodecId::RawVideo, true)),
916 Some("webp") => Some((CodecId::Webp, false)),
917 Some("png") => Some((CodecId::Png, false)),
918 Some("jpeg") => Some((CodecId::Jpeg, false)),
919 Some("gif") => Some((CodecId::Gif, false)),
920 Some("avif") => Some((CodecId::Avif, false)),
921 Some("jpegxl") => Some((CodecId::Jxl, false)),
922 _ => None,
923 };
924 let apply = match required {
925 Some((_, true)) => true,
926 Some((codec, false)) => !selection.iter().any(|&(inp, local)| {
929 let s = &input_streams[inp][local];
930 s.media_type == MediaType::Video && s.codec_id == codec
931 }),
932 None => false,
933 };
934 if apply {
935 output_owned = output.clone();
936 output_owned.video_codec = Some(StreamCodec {
937 codec: required.expect("apply implies required").0,
938 options: Dictionary::default(),
939 sample_format: None, });
941 &output_owned
942 } else {
943 output
944 }
945 } else {
946 output
947 };
948
949 let mut per_input_ops: Vec<Vec<StreamOp>> = input_streams
951 .iter()
952 .map(|s| (0..s.len()).map(|_| StreamOp::Skip).collect())
953 .collect();
954 let mut out_streams: Vec<Stream> = Vec::new();
955 for (out_index, &(inp, local)) in selection.iter().enumerate() {
956 let (op, os) = build_op(engine, &input_streams[inp][local], output, out_index)?;
957 per_input_ops[inp][local] = op;
958 out_streams.push(os);
959 }
960
961 if let (Some((x, y)), Some(oin)) = (overlay_xy, overlay_input) {
964 let over = decode_overlay_frame(engine, &mut *demuxers[oin], &input_streams[oin])?;
965 let vidx = input_streams[0]
966 .iter()
967 .position(|s| s.media_type == MediaType::Video)
968 .ok_or_else(|| Error::Option("filter_complex overlay: input #0 has no video".into()))?;
969 match &mut per_input_ops[0][vidx] {
970 StreamOp::Transcode(op) => op.overlay = Some((over, x, y)),
971 _ => {
972 return Err(Error::unsupported(
973 "filter_complex overlay needs input #0's video re-encoded — pass -c:v",
974 ))
975 }
976 }
977 }
978
979 let out_format = resolve_output_format(engine, output)?;
983 let muxer_path_factory = engine
984 .formats
985 .by_name(&out_format)
986 .filter(|fmt| fmt.can_mux())
987 .ok_or_else(|| Error::MuxerNotFound(out_format.clone()))?
988 .muxer_path;
989
990 let out_path_str = output.path.to_str().unwrap_or_default();
993 let is_sink_stream = rff_io::is_pipe(out_path_str)
994 || rff_io::is_udp(out_path_str)
995 || rff_io::is_rtmp(out_path_str);
996 if !is_sink_stream && !output.overwrite && output.path.exists() {
997 return Err(Error::Option(format!(
998 "{} already exists (pass -y to overwrite)",
999 output.path.display()
1000 )));
1001 }
1002 let mut muxer: Box<dyn Muxer> = match muxer_path_factory {
1003 Some(factory) => factory(&output.path, &output.format_options)?,
1004 None => match rff_io::create_seekable(out_path_str)? {
1008 Some(file) => engine
1009 .formats
1010 .open_muxer_seekable(&out_format, Box::new(file))?,
1011 None => engine
1012 .formats
1013 .open_muxer(&out_format, rff_io::create(out_path_str)?)?,
1014 },
1015 };
1016 if !output.metadata.is_empty() {
1017 muxer.set_metadata(&output.metadata);
1018 }
1019 muxer.write_header(&out_streams)?;
1020
1021 let mut report = TranscodeReport::default();
1022
1023 for (demuxer, ops) in demuxers.iter_mut().zip(&per_input_ops) {
1027 let discard: Vec<bool> = ops.iter().map(|op| matches!(op, StreamOp::Skip)).collect();
1028 if discard.contains(&true) {
1029 demuxer.set_discard(&discard);
1030 }
1031 }
1032
1033 for (demuxer, ops) in demuxers.iter_mut().zip(per_input_ops.iter_mut()) {
1035 loop {
1036 match phases::time(&phases::DEMUX, || demuxer.read_packet()) {
1037 Ok(packet) => process_packet(packet, ops, &mut *muxer, &mut report)?,
1038 Err(Error::Eof) => break,
1039 Err(e) => return Err(e),
1040 }
1041 }
1042 flush_streams(ops, &mut *muxer, &mut report)?;
1043 }
1044 phases::time(&phases::MUX, || muxer.write_trailer())?;
1045 if let Some(t) = t_start {
1046 phases::report(t.elapsed());
1047 }
1048
1049 Ok(report)
1050}
1051
1052fn select_streams(inputs: &[Vec<Stream>], output: &OutputSpec) -> Result<Vec<(usize, usize)>> {
1057 let mut selection = mapped_streams(inputs, output)?;
1058 selection.retain(|&(ii, i)| !output.disabled.contains(&inputs[ii][i].media_type));
1059 Ok(selection)
1060}
1061
1062fn mapped_streams(inputs: &[Vec<Stream>], output: &OutputSpec) -> Result<Vec<(usize, usize)>> {
1063 let mut selection = Vec::new();
1064 if output.maps.is_empty() {
1065 for (ii, streams) in inputs.iter().enumerate() {
1066 for (i, s) in streams.iter().enumerate() {
1067 if matches!(
1068 s.media_type,
1069 MediaType::Video | MediaType::Audio | MediaType::Subtitle
1070 ) {
1071 selection.push((ii, i));
1072 }
1073 }
1074 }
1075 } else {
1076 for map in &output.maps {
1077 let streams = inputs
1078 .get(map.input)
1079 .ok_or_else(|| Error::Option(format!("-map: no input #{}", map.input)))?;
1080 for (i, s) in streams.iter().enumerate() {
1081 let hit = match &map.selector {
1082 MapSelector::All => true,
1083 MapSelector::Kind(k) => s.media_type == *k,
1084 MapSelector::Index(idx) => i == *idx,
1085 };
1086 if hit {
1087 selection.push((map.input, i));
1088 }
1089 }
1090 }
1091 }
1092 Ok(selection)
1093}
1094
1095fn build_op(
1098 engine: &Engine,
1099 stream: &Stream,
1100 output: &OutputSpec,
1101 out_index: usize,
1102) -> Result<(StreamOp, Stream)> {
1103 let requested = match stream.media_type {
1104 MediaType::Video => output.video_codec.as_ref(),
1105 MediaType::Audio => output.audio_codec.as_ref(),
1106 _ => None,
1107 };
1108 match requested {
1109 Some(target) => {
1111 let mut decoder = engine.codecs.find_decoder(stream.codec_id)?;
1112 decoder.configure(&codec_params(stream))?;
1113 let mut encoder = engine.codecs.find_encoder(target.codec)?;
1114 encoder.configure(&target.options)?; let mut filters = if stream.media_type == MediaType::Video {
1117 FilterChain::parse(output.video_filters.as_deref().unwrap_or(""))?
1118 } else {
1119 FilterChain::default()
1120 };
1121 let mut source_range = ColorRange::Unspecified;
1128 if stream.media_type == MediaType::Video {
1129 source_range = effective_color_range(stream);
1130 if is_full_range_codec(target.codec) && source_range == ColorRange::Limited {
1131 filters.prepend_range_conversion(true);
1132 source_range = ColorRange::Full;
1133 }
1134 filters.set_source_color_range(source_range);
1135 }
1136 let (out_w, out_h) = filters.output_dims(stream.width, stream.height);
1137 let is_video = stream.media_type == MediaType::Video;
1138 let is_audio = stream.media_type == MediaType::Audio;
1139
1140 let audio_chain = if is_audio {
1142 rff_filter::AudioFilterChain::parse(output.audio_filters.as_deref().unwrap_or(""))?
1143 } else {
1144 rff_filter::AudioFilterChain::default()
1145 };
1146
1147 let target_channels = match output.audio_channels {
1149 Some(c) if is_audio => {
1150 if !(1..=2).contains(&c) {
1151 return Err(Error::unsupported(format!(
1152 "-ac {c}: only 1 or 2 channels supported"
1153 )));
1154 }
1155 c
1156 }
1157 _ => 0,
1158 };
1159
1160 let mut target_rate = 0;
1164 let mut out_rate = stream.sample_rate;
1165 if is_audio && stream.sample_rate > 0 {
1166 let desired = output
1167 .audio_rate
1168 .or(audio_chain.resample_target())
1169 .unwrap_or(stream.sample_rate);
1170 let desired = match encoder.accepted_sample_rates() {
1171 Some(rates) if !rates.contains(&desired) => nearest_rate(&rates, desired),
1172 _ => desired,
1173 };
1174 if desired != stream.sample_rate {
1175 target_rate = desired;
1176 }
1177 out_rate = desired;
1178 }
1179
1180 let trim = Trim::from_spec(output);
1182 let tb = stream.time_base;
1183 let shift_ticks = trim
1184 .map(|t| (t.start * tb.den as f64 / tb.num.max(1) as f64).round() as i64)
1185 .unwrap_or(0);
1186
1187 let fps = match output.frame_rate {
1189 Some((num, den)) if is_video => Some(FpsConv::new(num, den)),
1190 _ => None,
1191 };
1192
1193 let mut accepted_formats = encoder.accepted_pixel_formats();
1194
1195 let mut os = Stream::new(out_index, target.codec);
1196 os.media_type = stream.media_type;
1197 os.time_base = stream.time_base;
1198 os.width = out_w;
1199 os.height = out_h;
1200 os.pixel_format = stream.pixel_format;
1201 if target.codec == CodecId::RawVideo && stream.media_type == MediaType::Video {
1208 let pinned = os.pixel_format.unwrap_or(PixelFormat::Yuv420p);
1209 os.pixel_format = Some(pinned);
1210 accepted_formats = Some(vec![pinned]);
1211 }
1212 os.color_range = filters.output_color_range().unwrap_or(source_range);
1221 os.sample_rate = out_rate;
1222 os.channels = if target_channels > 0 {
1223 target_channels
1224 } else {
1225 stream.channels
1226 };
1227 if let Some((num, den)) = output.frame_rate {
1229 if is_video {
1230 os.time_base = rff_core::Rational::new(den as i32, num as i32);
1231 }
1232 }
1233 let target_sample_format = target.sample_format;
1239 os.sample_format = target_sample_format
1240 .or(stream.sample_format)
1241 .or(Some(SampleFormat::F32));
1242 let target_sample_format = target_sample_format.or(os.sample_format);
1247 if stream.media_type == MediaType::Audio && out_rate > 0 {
1250 os.time_base = rff_core::Rational::new(1, out_rate as i32);
1251 }
1252 Ok((
1253 StreamOp::Transcode(Box::new(TranscodeOp {
1254 decoder,
1255 encoder,
1256 filters,
1257 overlay: None,
1258 in_time_base: stream.time_base,
1259 is_video,
1260 trim,
1261 shift_ticks,
1262 audio_clock_samples: 0,
1263 fps,
1264 audio_chain,
1265 target_channels,
1266 target_rate,
1267 target_sample_format,
1268 resampler: None,
1269 accepted_formats,
1270 pixel_converter: None,
1271 source_range,
1272 out_index,
1273 max_video_frames: output.max_video_frames,
1274 video_frames_sent: 0,
1275 pending_pts: std::collections::VecDeque::new(),
1276 })),
1277 os,
1278 ))
1279 }
1280 None => {
1283 let mut os = stream.clone();
1284 os.index = out_index;
1285 if stream.media_type == MediaType::Subtitle {
1288 if let Some(target) = output.subtitle_codec {
1289 if target.media_type() != MediaType::Subtitle {
1290 return Err(Error::unsupported(format!(
1291 "-c:s: `{}` is not a subtitle codec",
1292 target.name()
1293 )));
1294 }
1295 os.codec_id = target;
1296 }
1297 }
1298 let trim = Trim::from_spec(output);
1299 let tb = stream.time_base;
1300 let to_ticks = |s: f64| (s * tb.den as f64 / tb.num.max(1) as f64).round() as i64;
1301 Ok((
1302 StreamOp::Copy(CopyOp {
1303 out_index,
1304 is_video: stream.media_type == MediaType::Video,
1305 start_ticks: trim.map(|t| to_ticks(t.start)).unwrap_or(0),
1306 end_ticks: trim.and_then(|t| t.end).map(to_ticks),
1307 trimming: trim.is_some(),
1308 started: false,
1309 }),
1310 os,
1311 ))
1312 }
1313 }
1314}
1315
1316fn decode_overlay_frame(
1319 engine: &Engine,
1320 demuxer: &mut dyn rff_format::Demuxer,
1321 streams: &[Stream],
1322) -> Result<VideoFrame> {
1323 let vidx = streams
1324 .iter()
1325 .position(|s| s.media_type == MediaType::Video)
1326 .ok_or_else(|| {
1327 Error::Option("filter_complex overlay: overlay input has no video".into())
1328 })?;
1329 let mut decoder = engine.codecs.find_decoder(streams[vidx].codec_id)?;
1330 decoder.configure(&codec_params(&streams[vidx]))?;
1331 let mut to_yuv = FilterChain::parse("format=yuv420p")?;
1332 let mut got_eof = false;
1333 loop {
1334 let frame = match demuxer.read_packet() {
1335 Ok(pkt) if pkt.stream_index as usize != vidx => continue,
1336 Ok(pkt) => {
1337 decoder.send_packet(&pkt)?;
1338 decoder.receive_frame()
1339 }
1340 Err(Error::Eof) if !got_eof => {
1341 got_eof = true;
1342 decoder.flush();
1343 decoder.receive_frame()
1344 }
1345 Err(e) => return Err(e),
1346 };
1347 match frame {
1348 Ok(Frame::Video(v)) => return to_yuv.apply(v),
1349 Ok(_) => continue,
1350 Err(Error::Again) if !got_eof => continue,
1351 Err(Error::Again) | Err(Error::Eof) => {
1352 return Err(Error::Option(
1353 "filter_complex overlay: no decodable frame in the overlay input".into(),
1354 ))
1355 }
1356 Err(e) => return Err(e),
1357 }
1358 }
1359}
1360
1361fn codec_params(s: &Stream) -> CodecParams {
1363 CodecParams {
1364 codec_id: s.codec_id,
1365 width: s.width,
1366 height: s.height,
1367 pixel_format: s.pixel_format,
1368 sample_rate: s.sample_rate,
1369 channels: s.channels,
1370 sample_format: s.sample_format,
1371 extradata: s.extradata.clone(),
1372 }
1373}
1374
1375fn process_packet(
1377 packet: Packet,
1378 ops: &mut [StreamOp],
1379 muxer: &mut dyn Muxer,
1380 report: &mut TranscodeReport,
1381) -> Result<()> {
1382 let Some(op) = ops.get_mut(packet.stream_index) else {
1383 return Ok(()); };
1385 match op {
1386 StreamOp::Skip => Ok(()),
1387 StreamOp::Copy(copy) => {
1388 if let Some(packet) = copy.process(packet) {
1389 muxer.write_packet(&packet)?;
1390 report.packets_written += 1;
1391 }
1392 Ok(())
1393 }
1394 StreamOp::Transcode(op) => {
1395 phases::time(&phases::DECODE, || op.decoder.send_packet(&packet))?;
1396 loop {
1397 match phases::time(&phases::DECODE, || op.decoder.receive_frame()) {
1398 Ok(frame) => op.handle_frame(frame, muxer, report)?,
1399 Err(Error::Again) | Err(Error::Eof) => break,
1400 Err(e) => return Err(e),
1401 }
1402 }
1403 Ok(())
1404 }
1405 }
1406}
1407
1408fn flush_streams(
1411 ops: &mut [StreamOp],
1412 muxer: &mut dyn Muxer,
1413 report: &mut TranscodeReport,
1414) -> Result<()> {
1415 for op in ops.iter_mut() {
1416 if let StreamOp::Transcode(op) = op {
1417 op.finish(muxer, report)?;
1418 }
1419 }
1420 Ok(())
1421}
1422
1423fn open_input(engine: &Engine, input: &InputSpec) -> Result<(String, crate::probe::Source)> {
1427 let path = input
1428 .path
1429 .to_str()
1430 .ok_or_else(|| Error::Option("input path is not valid UTF-8".into()))?;
1431 crate::probe::open_source(engine, path, input.format.as_deref())
1432}
1433
1434fn resolve_output_format(engine: &Engine, output: &OutputSpec) -> Result<String> {
1438 if let Some(forced) = &output.format {
1439 return Ok(forced.clone());
1440 }
1441 if output.path.to_str().is_some_and(rff_io::is_rtmp) {
1442 return Ok("flv".to_string());
1443 }
1444 let ext = output
1445 .path
1446 .extension()
1447 .and_then(|e| e.to_str())
1448 .unwrap_or_default();
1449 engine
1450 .formats
1451 .by_extension(ext)
1452 .map(|f| f.name.to_string())
1453 .ok_or_else(|| {
1454 Error::MuxerNotFound(format!(
1455 "{} (no format for this extension — pass -f)",
1456 output.path.display()
1457 ))
1458 })
1459}
1460
1461#[cfg(test)]
1462mod sample_format_tests {
1463 use super::*;
1464
1465 fn f32_af(samples: &[f32]) -> Frame {
1466 f32_frame(samples.to_vec(), 44_100, 1, Some(0))
1467 }
1468
1469 #[test]
1473 fn pinned_format_converts_f32_to_s16() {
1474 let src = [0.0f32, 0.5, -0.5, 1.0, -1.0];
1475 let out = conform_sample_format(Some(SampleFormat::S16), f32_af(&src)).unwrap();
1476 let Frame::Audio(af) = out else {
1477 unreachable!()
1478 };
1479 assert_eq!(af.format, SampleFormat::S16);
1480 assert_eq!(af.planes[0].len(), src.len() * 2, "s16 is 2 bytes/sample");
1481 let got: Vec<i16> = af.planes[0]
1482 .chunks_exact(2)
1483 .map(|b| i16::from_le_bytes([b[0], b[1]]))
1484 .collect();
1485 assert_eq!(got, vec![0, 16384, -16384, 32767, -32768]);
1488 }
1489
1490 #[test]
1493 fn unpinned_or_matching_format_is_a_passthrough() {
1494 let src = [0.25f32, -0.25];
1495 let Frame::Audio(a) = conform_sample_format(None, f32_af(&src)).unwrap() else {
1496 unreachable!()
1497 };
1498 assert_eq!(a.format, SampleFormat::F32);
1499 let Frame::Audio(b) = conform_sample_format(Some(SampleFormat::F32), f32_af(&src)).unwrap()
1500 else {
1501 unreachable!()
1502 };
1503 assert_eq!(b.format, SampleFormat::F32);
1504 assert_eq!(b.planes[0], a.planes[0]);
1505 }
1506
1507 #[test]
1509 fn codec_name_pins_the_sample_format() {
1510 assert_eq!(
1511 CodecId::sample_format_from_name("pcm_s16le"),
1512 Some(SampleFormat::S16)
1513 );
1514 assert_eq!(
1515 CodecId::sample_format_from_name("pcm_f32le"),
1516 Some(SampleFormat::F32)
1517 );
1518 assert_eq!(CodecId::sample_format_from_name("pcm"), None);
1520 assert_eq!(CodecId::sample_format_from_name("mp3"), None);
1521 }
1522}