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 if muxer_path_factory.is_none() {
1022 for (key, value) in output.format_options.iter() {
1023 muxer.set_option(key, value);
1024 }
1025 }
1026 muxer.write_header(&out_streams)?;
1027
1028 let mut report = TranscodeReport::default();
1029
1030 for (demuxer, ops) in demuxers.iter_mut().zip(&per_input_ops) {
1034 let discard: Vec<bool> = ops.iter().map(|op| matches!(op, StreamOp::Skip)).collect();
1035 if discard.contains(&true) {
1036 demuxer.set_discard(&discard);
1037 }
1038 }
1039
1040 for (demuxer, ops) in demuxers.iter_mut().zip(per_input_ops.iter_mut()) {
1042 loop {
1043 match phases::time(&phases::DEMUX, || demuxer.read_packet()) {
1044 Ok(packet) => process_packet(packet, ops, &mut *muxer, &mut report)?,
1045 Err(Error::Eof) => break,
1046 Err(e) => return Err(e),
1047 }
1048 }
1049 flush_streams(ops, &mut *muxer, &mut report)?;
1050 }
1051 phases::time(&phases::MUX, || muxer.write_trailer())?;
1052 if let Some(t) = t_start {
1053 phases::report(t.elapsed());
1054 }
1055
1056 Ok(report)
1057}
1058
1059fn select_streams(inputs: &[Vec<Stream>], output: &OutputSpec) -> Result<Vec<(usize, usize)>> {
1064 let mut selection = mapped_streams(inputs, output)?;
1065 selection.retain(|&(ii, i)| !output.disabled.contains(&inputs[ii][i].media_type));
1066 Ok(selection)
1067}
1068
1069fn mapped_streams(inputs: &[Vec<Stream>], output: &OutputSpec) -> Result<Vec<(usize, usize)>> {
1070 let mut selection = Vec::new();
1071 if output.maps.is_empty() {
1072 for (ii, streams) in inputs.iter().enumerate() {
1073 for (i, s) in streams.iter().enumerate() {
1074 if matches!(
1075 s.media_type,
1076 MediaType::Video | MediaType::Audio | MediaType::Subtitle
1077 ) {
1078 selection.push((ii, i));
1079 }
1080 }
1081 }
1082 } else {
1083 for map in &output.maps {
1084 let streams = inputs
1085 .get(map.input)
1086 .ok_or_else(|| Error::Option(format!("-map: no input #{}", map.input)))?;
1087 for (i, s) in streams.iter().enumerate() {
1088 let hit = match &map.selector {
1089 MapSelector::All => true,
1090 MapSelector::Kind(k) => s.media_type == *k,
1091 MapSelector::Index(idx) => i == *idx,
1092 };
1093 if hit {
1094 selection.push((map.input, i));
1095 }
1096 }
1097 }
1098 }
1099 Ok(selection)
1100}
1101
1102fn build_op(
1105 engine: &Engine,
1106 stream: &Stream,
1107 output: &OutputSpec,
1108 out_index: usize,
1109) -> Result<(StreamOp, Stream)> {
1110 let requested = match stream.media_type {
1111 MediaType::Video => output.video_codec.as_ref(),
1112 MediaType::Audio => output.audio_codec.as_ref(),
1113 _ => None,
1114 };
1115 match requested {
1116 Some(target) => {
1118 let mut decoder = engine.codecs.find_decoder(stream.codec_id)?;
1119 decoder.configure(&codec_params(stream))?;
1120 let mut encoder = engine.codecs.find_encoder(target.codec)?;
1121 encoder.configure(&target.options)?; let mut filters = if stream.media_type == MediaType::Video {
1124 FilterChain::parse(output.video_filters.as_deref().unwrap_or(""))?
1125 } else {
1126 FilterChain::default()
1127 };
1128 let mut source_range = ColorRange::Unspecified;
1135 if stream.media_type == MediaType::Video {
1136 source_range = effective_color_range(stream);
1137 if is_full_range_codec(target.codec) && source_range == ColorRange::Limited {
1138 filters.prepend_range_conversion(true);
1139 source_range = ColorRange::Full;
1140 }
1141 filters.set_source_color_range(source_range);
1142 }
1143 let (out_w, out_h) = filters.output_dims(stream.width, stream.height);
1144 let is_video = stream.media_type == MediaType::Video;
1145 let is_audio = stream.media_type == MediaType::Audio;
1146
1147 let audio_chain = if is_audio {
1149 rff_filter::AudioFilterChain::parse(output.audio_filters.as_deref().unwrap_or(""))?
1150 } else {
1151 rff_filter::AudioFilterChain::default()
1152 };
1153
1154 let target_channels = match output.audio_channels {
1156 Some(c) if is_audio => {
1157 if !(1..=2).contains(&c) {
1158 return Err(Error::unsupported(format!(
1159 "-ac {c}: only 1 or 2 channels supported"
1160 )));
1161 }
1162 c
1163 }
1164 _ => 0,
1165 };
1166
1167 let mut target_rate = 0;
1171 let mut out_rate = stream.sample_rate;
1172 if is_audio && stream.sample_rate > 0 {
1173 let desired = output
1174 .audio_rate
1175 .or(audio_chain.resample_target())
1176 .unwrap_or(stream.sample_rate);
1177 let desired = match encoder.accepted_sample_rates() {
1178 Some(rates) if !rates.contains(&desired) => nearest_rate(&rates, desired),
1179 _ => desired,
1180 };
1181 if desired != stream.sample_rate {
1182 target_rate = desired;
1183 }
1184 out_rate = desired;
1185 }
1186
1187 let trim = Trim::from_spec(output);
1189 let tb = stream.time_base;
1190 let shift_ticks = trim
1191 .map(|t| (t.start * tb.den as f64 / tb.num.max(1) as f64).round() as i64)
1192 .unwrap_or(0);
1193
1194 let fps = match output.frame_rate {
1196 Some((num, den)) if is_video => Some(FpsConv::new(num, den)),
1197 _ => None,
1198 };
1199
1200 let mut accepted_formats = encoder.accepted_pixel_formats();
1201
1202 let mut os = Stream::new(out_index, target.codec);
1203 os.media_type = stream.media_type;
1204 os.time_base = stream.time_base;
1205 os.width = out_w;
1206 os.height = out_h;
1207 os.pixel_format = stream.pixel_format;
1208 if target.codec == CodecId::RawVideo && stream.media_type == MediaType::Video {
1215 let pinned = os.pixel_format.unwrap_or(PixelFormat::Yuv420p);
1216 os.pixel_format = Some(pinned);
1217 accepted_formats = Some(vec![pinned]);
1218 }
1219 os.color_range = filters.output_color_range().unwrap_or(source_range);
1228 os.sample_rate = out_rate;
1229 os.channels = if target_channels > 0 {
1230 target_channels
1231 } else {
1232 stream.channels
1233 };
1234 if let Some((num, den)) = output.frame_rate {
1236 if is_video {
1237 os.time_base = rff_core::Rational::new(den as i32, num as i32);
1238 }
1239 }
1240 let target_sample_format = target.sample_format;
1246 os.sample_format = target_sample_format
1247 .or(stream.sample_format)
1248 .or(Some(SampleFormat::F32));
1249 let target_sample_format = target_sample_format.or(os.sample_format);
1254 if stream.media_type == MediaType::Audio && out_rate > 0 {
1257 os.time_base = rff_core::Rational::new(1, out_rate as i32);
1258 }
1259 Ok((
1260 StreamOp::Transcode(Box::new(TranscodeOp {
1261 decoder,
1262 encoder,
1263 filters,
1264 overlay: None,
1265 in_time_base: stream.time_base,
1266 is_video,
1267 trim,
1268 shift_ticks,
1269 audio_clock_samples: 0,
1270 fps,
1271 audio_chain,
1272 target_channels,
1273 target_rate,
1274 target_sample_format,
1275 resampler: None,
1276 accepted_formats,
1277 pixel_converter: None,
1278 source_range,
1279 out_index,
1280 max_video_frames: output.max_video_frames,
1281 video_frames_sent: 0,
1282 pending_pts: std::collections::VecDeque::new(),
1283 })),
1284 os,
1285 ))
1286 }
1287 None => {
1290 let mut os = stream.clone();
1291 os.index = out_index;
1292 if stream.media_type == MediaType::Subtitle {
1295 if let Some(target) = output.subtitle_codec {
1296 if target.media_type() != MediaType::Subtitle {
1297 return Err(Error::unsupported(format!(
1298 "-c:s: `{}` is not a subtitle codec",
1299 target.name()
1300 )));
1301 }
1302 os.codec_id = target;
1303 }
1304 }
1305 let trim = Trim::from_spec(output);
1306 let tb = stream.time_base;
1307 let to_ticks = |s: f64| (s * tb.den as f64 / tb.num.max(1) as f64).round() as i64;
1308 Ok((
1309 StreamOp::Copy(CopyOp {
1310 out_index,
1311 is_video: stream.media_type == MediaType::Video,
1312 start_ticks: trim.map(|t| to_ticks(t.start)).unwrap_or(0),
1313 end_ticks: trim.and_then(|t| t.end).map(to_ticks),
1314 trimming: trim.is_some(),
1315 started: false,
1316 }),
1317 os,
1318 ))
1319 }
1320 }
1321}
1322
1323fn decode_overlay_frame(
1326 engine: &Engine,
1327 demuxer: &mut dyn rff_format::Demuxer,
1328 streams: &[Stream],
1329) -> Result<VideoFrame> {
1330 let vidx = streams
1331 .iter()
1332 .position(|s| s.media_type == MediaType::Video)
1333 .ok_or_else(|| {
1334 Error::Option("filter_complex overlay: overlay input has no video".into())
1335 })?;
1336 let mut decoder = engine.codecs.find_decoder(streams[vidx].codec_id)?;
1337 decoder.configure(&codec_params(&streams[vidx]))?;
1338 let mut to_yuv = FilterChain::parse("format=yuv420p")?;
1339 let mut got_eof = false;
1340 loop {
1341 let frame = match demuxer.read_packet() {
1342 Ok(pkt) if pkt.stream_index as usize != vidx => continue,
1343 Ok(pkt) => {
1344 decoder.send_packet(&pkt)?;
1345 decoder.receive_frame()
1346 }
1347 Err(Error::Eof) if !got_eof => {
1348 got_eof = true;
1349 decoder.flush();
1350 decoder.receive_frame()
1351 }
1352 Err(e) => return Err(e),
1353 };
1354 match frame {
1355 Ok(Frame::Video(v)) => return to_yuv.apply(v),
1356 Ok(_) => continue,
1357 Err(Error::Again) if !got_eof => continue,
1358 Err(Error::Again) | Err(Error::Eof) => {
1359 return Err(Error::Option(
1360 "filter_complex overlay: no decodable frame in the overlay input".into(),
1361 ))
1362 }
1363 Err(e) => return Err(e),
1364 }
1365 }
1366}
1367
1368fn codec_params(s: &Stream) -> CodecParams {
1370 CodecParams {
1371 codec_id: s.codec_id,
1372 width: s.width,
1373 height: s.height,
1374 pixel_format: s.pixel_format,
1375 sample_rate: s.sample_rate,
1376 channels: s.channels,
1377 sample_format: s.sample_format,
1378 extradata: s.extradata.clone(),
1379 }
1380}
1381
1382fn process_packet(
1384 packet: Packet,
1385 ops: &mut [StreamOp],
1386 muxer: &mut dyn Muxer,
1387 report: &mut TranscodeReport,
1388) -> Result<()> {
1389 let Some(op) = ops.get_mut(packet.stream_index) else {
1390 return Ok(()); };
1392 match op {
1393 StreamOp::Skip => Ok(()),
1394 StreamOp::Copy(copy) => {
1395 if let Some(packet) = copy.process(packet) {
1396 muxer.write_packet(&packet)?;
1397 report.packets_written += 1;
1398 }
1399 Ok(())
1400 }
1401 StreamOp::Transcode(op) => {
1402 phases::time(&phases::DECODE, || op.decoder.send_packet(&packet))?;
1403 loop {
1404 match phases::time(&phases::DECODE, || op.decoder.receive_frame()) {
1405 Ok(frame) => op.handle_frame(frame, muxer, report)?,
1406 Err(Error::Again) | Err(Error::Eof) => break,
1407 Err(e) => return Err(e),
1408 }
1409 }
1410 Ok(())
1411 }
1412 }
1413}
1414
1415fn flush_streams(
1418 ops: &mut [StreamOp],
1419 muxer: &mut dyn Muxer,
1420 report: &mut TranscodeReport,
1421) -> Result<()> {
1422 for op in ops.iter_mut() {
1423 if let StreamOp::Transcode(op) = op {
1424 op.finish(muxer, report)?;
1425 }
1426 }
1427 Ok(())
1428}
1429
1430fn open_input(engine: &Engine, input: &InputSpec) -> Result<(String, crate::probe::Source)> {
1434 let path = input
1435 .path
1436 .to_str()
1437 .ok_or_else(|| Error::Option("input path is not valid UTF-8".into()))?;
1438 crate::probe::open_source(engine, path, input.format.as_deref())
1439}
1440
1441fn resolve_output_format(engine: &Engine, output: &OutputSpec) -> Result<String> {
1445 if let Some(forced) = &output.format {
1446 return Ok(forced.clone());
1447 }
1448 if output.path.to_str().is_some_and(rff_io::is_rtmp) {
1449 return Ok("flv".to_string());
1450 }
1451 let ext = output
1452 .path
1453 .extension()
1454 .and_then(|e| e.to_str())
1455 .unwrap_or_default();
1456 engine
1457 .formats
1458 .by_extension(ext)
1459 .map(|f| f.name.to_string())
1460 .ok_or_else(|| {
1461 Error::MuxerNotFound(format!(
1462 "{} (no format for this extension — pass -f)",
1463 output.path.display()
1464 ))
1465 })
1466}
1467
1468#[cfg(test)]
1469mod sample_format_tests {
1470 use super::*;
1471
1472 fn f32_af(samples: &[f32]) -> Frame {
1473 f32_frame(samples.to_vec(), 44_100, 1, Some(0))
1474 }
1475
1476 #[test]
1480 fn pinned_format_converts_f32_to_s16() {
1481 let src = [0.0f32, 0.5, -0.5, 1.0, -1.0];
1482 let out = conform_sample_format(Some(SampleFormat::S16), f32_af(&src)).unwrap();
1483 let Frame::Audio(af) = out else {
1484 unreachable!()
1485 };
1486 assert_eq!(af.format, SampleFormat::S16);
1487 assert_eq!(af.planes[0].len(), src.len() * 2, "s16 is 2 bytes/sample");
1488 let got: Vec<i16> = af.planes[0]
1489 .chunks_exact(2)
1490 .map(|b| i16::from_le_bytes([b[0], b[1]]))
1491 .collect();
1492 assert_eq!(got, vec![0, 16384, -16384, 32767, -32768]);
1495 }
1496
1497 #[test]
1500 fn unpinned_or_matching_format_is_a_passthrough() {
1501 let src = [0.25f32, -0.25];
1502 let Frame::Audio(a) = conform_sample_format(None, f32_af(&src)).unwrap() else {
1503 unreachable!()
1504 };
1505 assert_eq!(a.format, SampleFormat::F32);
1506 let Frame::Audio(b) = conform_sample_format(Some(SampleFormat::F32), f32_af(&src)).unwrap()
1507 else {
1508 unreachable!()
1509 };
1510 assert_eq!(b.format, SampleFormat::F32);
1511 assert_eq!(b.planes[0], a.planes[0]);
1512 }
1513
1514 #[test]
1516 fn codec_name_pins_the_sample_format() {
1517 assert_eq!(
1518 CodecId::sample_format_from_name("pcm_s16le"),
1519 Some(SampleFormat::S16)
1520 );
1521 assert_eq!(
1522 CodecId::sample_format_from_name("pcm_f32le"),
1523 Some(SampleFormat::F32)
1524 );
1525 assert_eq!(CodecId::sample_format_from_name("pcm"), None);
1527 assert_eq!(CodecId::sample_format_from_name("mp3"), None);
1528 }
1529}