1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
//! Unsafe FFmpeg calls for audio stream operations (replacement, extraction, addition).
#![allow(unsafe_code)]
#![allow(unsafe_op_in_unsafe_fn)]
use std::path::Path;
use crate::error::EncodeError;
/// Replace the audio stream of `video_input` with the audio from `audio_input`,
/// writing the combined result to `output`.
///
/// # Safety
///
/// All FFmpeg pointer invariants are maintained internally. The public
/// `AudioReplacement::run` wraps this function safely.
pub(crate) fn run_audio_replacement(
video_input: &Path,
audio_input: &Path,
output: &Path,
) -> Result<(), EncodeError> {
// SAFETY: All pointers are validated (null-checked) before use; resources
// are freed on every exit path.
unsafe { run_audio_replacement_unsafe(video_input, audio_input, output) }
}
unsafe fn run_audio_replacement_unsafe(
video_input: &Path,
audio_input: &Path,
output: &Path,
) -> Result<(), EncodeError> {
// ── Step 1: open video input ──────────────────────────────────────────────
// SAFETY: video_input is a caller-supplied path; open_input returns Err on failure.
let vid_ctx =
ff_sys::avformat::open_input(video_input).map_err(EncodeError::from_ffmpeg_error)?;
// ── Step 2: find stream info for video input ──────────────────────────────
// SAFETY: vid_ctx is non-null (open_input succeeded).
if let Err(e) = ff_sys::avformat::find_stream_info(vid_ctx) {
let mut p = vid_ctx;
ff_sys::avformat::close_input(&mut p);
return Err(EncodeError::from_ffmpeg_error(e));
}
// ── Step 3: locate the first video stream ─────────────────────────────────
// SAFETY: nb_streams is the valid count; streams is a valid array of that length.
let nb_vid_streams = (*vid_ctx).nb_streams as usize;
let mut video_stream_idx: Option<usize> = None;
for i in 0..nb_vid_streams {
let stream = *(*vid_ctx).streams.add(i);
if (*(*stream).codecpar).codec_type == ff_sys::AVMediaType_AVMEDIA_TYPE_VIDEO {
video_stream_idx = Some(i);
break;
}
}
let Some(video_stream_idx) = video_stream_idx else {
let mut p = vid_ctx;
ff_sys::avformat::close_input(&mut p);
return Err(EncodeError::MediaOperationFailed {
reason: format!(
"no video stream found in video input path={}",
video_input.display()
),
});
};
// ── Step 4: open audio input ──────────────────────────────────────────────
// SAFETY: audio_input is a caller-supplied path; open_input returns Err on failure.
let aud_ctx = match ff_sys::avformat::open_input(audio_input) {
Ok(ctx) => ctx,
Err(e) => {
let mut p = vid_ctx;
ff_sys::avformat::close_input(&mut p);
return Err(EncodeError::from_ffmpeg_error(e));
}
};
// ── Step 5: find stream info for audio input ──────────────────────────────
// SAFETY: aud_ctx is non-null (open_input succeeded).
if let Err(e) = ff_sys::avformat::find_stream_info(aud_ctx) {
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
return Err(EncodeError::from_ffmpeg_error(e));
}
// ── Step 6: locate the first audio stream ─────────────────────────────────
// SAFETY: nb_streams is the valid count; streams is a valid array of that length.
let nb_aud_streams = (*aud_ctx).nb_streams as usize;
let mut audio_stream_idx: Option<usize> = None;
for i in 0..nb_aud_streams {
let stream = *(*aud_ctx).streams.add(i);
if (*(*stream).codecpar).codec_type == ff_sys::AVMediaType_AVMEDIA_TYPE_AUDIO {
audio_stream_idx = Some(i);
break;
}
}
let Some(audio_stream_idx) = audio_stream_idx else {
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
return Err(EncodeError::MediaOperationFailed {
reason: format!(
"no audio stream found in audio input path={}",
audio_input.display()
),
});
};
// ── Step 7: allocate output context ──────────────────────────────────────
let Some(output_str) = output.to_str() else {
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
return Err(EncodeError::Ffmpeg {
code: 0,
message: "output path is not valid UTF-8".to_string(),
});
};
let Ok(c_output) = std::ffi::CString::new(output_str) else {
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
return Err(EncodeError::Ffmpeg {
code: 0,
message: "output path contains null bytes".to_string(),
});
};
let mut out_ctx: *mut ff_sys::AVFormatContext = std::ptr::null_mut();
// SAFETY: c_output is a valid null-terminated C string.
let ret = ff_sys::avformat_alloc_output_context2(
&mut out_ctx,
std::ptr::null_mut(),
std::ptr::null(),
c_output.as_ptr(),
);
if ret < 0 || out_ctx.is_null() {
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
return Err(EncodeError::from_ffmpeg_error(ret));
}
// ── Step 8: copy video stream parameters to output ────────────────────────
// SAFETY: video_stream_idx < nb_vid_streams; streams is a valid array.
let vid_in_stream = *(*vid_ctx).streams.add(video_stream_idx);
// SAFETY: out_ctx is non-null (avformat_alloc_output_context2 succeeded).
let vid_out_stream = ff_sys::avformat_new_stream(out_ctx, std::ptr::null());
if vid_out_stream.is_null() {
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
ff_sys::avformat_free_context(out_ctx);
return Err(EncodeError::Ffmpeg {
code: 0,
message: "avformat_new_stream failed for video".to_string(),
});
}
// SAFETY: both codecpar pointers are non-null (created by FFmpeg).
let ret =
ff_sys::avcodec_parameters_copy((*vid_out_stream).codecpar, (*vid_in_stream).codecpar);
if ret < 0 {
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
ff_sys::avformat_free_context(out_ctx);
return Err(EncodeError::from_ffmpeg_error(ret));
}
// Clear codec_tag so the muxer assigns the correct value for the container.
(*(*vid_out_stream).codecpar).codec_tag = 0;
// ── Step 9: copy audio stream parameters to output ────────────────────────
// SAFETY: audio_stream_idx < nb_aud_streams; streams is a valid array.
let aud_in_stream = *(*aud_ctx).streams.add(audio_stream_idx);
// SAFETY: out_ctx is non-null (avformat_alloc_output_context2 succeeded).
let aud_out_stream = ff_sys::avformat_new_stream(out_ctx, std::ptr::null());
if aud_out_stream.is_null() {
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
ff_sys::avformat_free_context(out_ctx);
return Err(EncodeError::Ffmpeg {
code: 0,
message: "avformat_new_stream failed for audio".to_string(),
});
}
// SAFETY: both codecpar pointers are non-null (created by FFmpeg).
let ret =
ff_sys::avcodec_parameters_copy((*aud_out_stream).codecpar, (*aud_in_stream).codecpar);
if ret < 0 {
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
ff_sys::avformat_free_context(out_ctx);
return Err(EncodeError::from_ffmpeg_error(ret));
}
// Clear codec_tag so the muxer assigns the correct value for the container.
(*(*aud_out_stream).codecpar).codec_tag = 0;
// ── Step 10: open output file ─────────────────────────────────────────────
// SAFETY: output is a valid path; WRITE opens the file for writing.
let pb = match ff_sys::avformat::open_output(output, ff_sys::avformat::avio_flags::WRITE) {
Ok(pb) => pb,
Err(e) => {
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
ff_sys::avformat_free_context(out_ctx);
return Err(EncodeError::from_ffmpeg_error(e));
}
};
// SAFETY: out_ctx is non-null; pb is a valid AVIOContext.
(*out_ctx).pb = pb;
// ── Step 11: write header ─────────────────────────────────────────────────
// SAFETY: out_ctx is fully configured with streams and pb set.
let ret = ff_sys::avformat_write_header(out_ctx, std::ptr::null_mut());
if ret < 0 {
// SAFETY: (*out_ctx).pb was set above and is non-null.
ff_sys::avformat::close_output(std::ptr::addr_of_mut!((*out_ctx).pb));
ff_sys::avformat_free_context(out_ctx);
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
return Err(EncodeError::from_ffmpeg_error(ret));
}
// Read time bases after avformat_write_header — the muxer may adjust them.
// SAFETY: stream pointers remain valid for the lifetime of their parent contexts.
let vid_in_tb = (*vid_in_stream).time_base;
let aud_in_tb = (*aud_in_stream).time_base;
let vid_out_tb = (*vid_out_stream).time_base;
let aud_out_tb = (*aud_out_stream).time_base;
log::debug!(
"audio replacement header written \
video_stream_idx={video_stream_idx} audio_stream_idx={audio_stream_idx}"
);
// ── Step 12: allocate packet ──────────────────────────────────────────────
// SAFETY: av_packet_alloc never returns null in practice (aborts on OOM).
let pkt = ff_sys::av_packet_alloc();
if pkt.is_null() {
ff_sys::av_write_trailer(out_ctx);
ff_sys::avformat::close_output(std::ptr::addr_of_mut!((*out_ctx).pb));
ff_sys::avformat_free_context(out_ctx);
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
return Err(EncodeError::Ffmpeg {
code: 0,
message: "av_packet_alloc failed".to_string(),
});
}
// ── Step 13: interleaved packet copy loop ─────────────────────────────────
// Alternate between video and audio inputs; use av_interleaved_write_frame
// so the muxer buffers and flushes packets in the correct timestamp order.
let mut loop_err: Option<EncodeError> = None;
let mut vid_eof = false;
let mut aud_eof = false;
'copy: loop {
// Read one packet from the video input, forwarding only the target stream.
if !vid_eof {
// SAFETY: vid_ctx and pkt are valid non-null pointers.
match ff_sys::avformat::read_frame(vid_ctx, pkt) {
Err(e) if e == ff_sys::error_codes::EOF => {
vid_eof = true;
}
Err(e) => {
loop_err = Some(EncodeError::from_ffmpeg_error(e));
break 'copy;
}
Ok(()) => {
if (*pkt).stream_index as usize == video_stream_idx {
// SAFETY: pkt, vid_in_tb, vid_out_tb are valid plain-data values.
ff_sys::av_packet_rescale_ts(pkt, vid_in_tb, vid_out_tb);
(*pkt).stream_index = 0;
// SAFETY: out_ctx and pkt are valid.
let ret = ff_sys::av_interleaved_write_frame(out_ctx, pkt);
// av_interleaved_write_frame takes the packet's buf reference;
// unref to clear any remaining fields.
ff_sys::av_packet_unref(pkt);
if ret < 0 {
loop_err = Some(EncodeError::from_ffmpeg_error(ret));
break 'copy;
}
} else {
ff_sys::av_packet_unref(pkt);
}
}
}
}
// Read one packet from the audio input, forwarding only the target stream.
if !aud_eof {
// SAFETY: aud_ctx and pkt are valid non-null pointers.
match ff_sys::avformat::read_frame(aud_ctx, pkt) {
Err(e) if e == ff_sys::error_codes::EOF => {
aud_eof = true;
}
Err(e) => {
loop_err = Some(EncodeError::from_ffmpeg_error(e));
break 'copy;
}
Ok(()) => {
if (*pkt).stream_index as usize == audio_stream_idx {
// SAFETY: pkt, aud_in_tb, aud_out_tb are valid plain-data values.
ff_sys::av_packet_rescale_ts(pkt, aud_in_tb, aud_out_tb);
(*pkt).stream_index = 1;
// SAFETY: out_ctx and pkt are valid.
let ret = ff_sys::av_interleaved_write_frame(out_ctx, pkt);
ff_sys::av_packet_unref(pkt);
if ret < 0 {
loop_err = Some(EncodeError::from_ffmpeg_error(ret));
break 'copy;
}
} else {
ff_sys::av_packet_unref(pkt);
}
}
}
}
if vid_eof && aud_eof {
break 'copy;
}
}
// SAFETY: pkt was allocated by av_packet_alloc above and is still valid.
let mut pkt_ptr = pkt;
ff_sys::av_packet_free(&mut pkt_ptr);
// ── Step 14: write trailer ────────────────────────────────────────────────
// SAFETY: out_ctx is valid; write_header was called successfully.
ff_sys::av_write_trailer(out_ctx);
// ── Step 15: cleanup ──────────────────────────────────────────────────────
// SAFETY: (*out_ctx).pb is non-null (opened above; still set after write_header).
ff_sys::avformat::close_output(std::ptr::addr_of_mut!((*out_ctx).pb));
// SAFETY: out_ctx is non-null and was allocated by avformat_alloc_output_context2.
ff_sys::avformat_free_context(out_ctx);
// SAFETY: vid_ctx and aud_ctx are non-null (open_input succeeded).
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
log::info!("audio replaced output={}", output.display());
match loop_err {
Some(e) => Err(e),
None => Ok(()),
}
}
// ── Audio extraction ──────────────────────────────────────────────────────────
/// Demux the audio track at `stream_index` (or the first audio stream when
/// `stream_index` is `None`) from `input` and write it to `output`.
///
/// The audio bitstream is stream-copied (no decode/encode cycle).
///
/// # Safety
///
/// All FFmpeg pointer invariants are maintained internally. The public
/// `AudioExtractor::run` wraps this function safely.
pub(crate) fn run_audio_extraction(
input: &Path,
output: &Path,
stream_index: Option<usize>,
) -> Result<(), EncodeError> {
// SAFETY: All pointers are validated (null-checked) before use; resources
// are freed on every exit path.
unsafe { run_audio_extraction_unsafe(input, output, stream_index) }
}
unsafe fn run_audio_extraction_unsafe(
input: &Path,
output: &Path,
requested_idx: Option<usize>,
) -> Result<(), EncodeError> {
// ── Step 1: open input ────────────────────────────────────────────────────
// SAFETY: input is a caller-supplied path; open_input returns Err on failure.
let in_ctx = ff_sys::avformat::open_input(input).map_err(EncodeError::from_ffmpeg_error)?;
// ── Step 2: find stream info ──────────────────────────────────────────────
// SAFETY: in_ctx is non-null (open_input succeeded).
if let Err(e) = ff_sys::avformat::find_stream_info(in_ctx) {
let mut p = in_ctx;
ff_sys::avformat::close_input(&mut p);
return Err(EncodeError::from_ffmpeg_error(e));
}
// ── Step 3: locate the audio stream ──────────────────────────────────────
// SAFETY: nb_streams is the valid count; streams is a valid array of that length.
let nb_streams = (*in_ctx).nb_streams as usize;
let audio_stream_idx = if let Some(idx) = requested_idx {
// Validate that the requested index is actually an audio stream.
if idx >= nb_streams {
let mut p = in_ctx;
ff_sys::avformat::close_input(&mut p);
return Err(EncodeError::MediaOperationFailed {
reason: format!("stream index {idx} out of range (input has {nb_streams} streams)"),
});
}
let stream = *(*in_ctx).streams.add(idx);
if (*(*stream).codecpar).codec_type != ff_sys::AVMediaType_AVMEDIA_TYPE_AUDIO {
let mut p = in_ctx;
ff_sys::avformat::close_input(&mut p);
return Err(EncodeError::MediaOperationFailed {
reason: format!("stream index {idx} is not an audio stream"),
});
}
idx
} else {
// Find the first audio stream.
let mut found: Option<usize> = None;
for i in 0..nb_streams {
let stream = *(*in_ctx).streams.add(i);
if (*(*stream).codecpar).codec_type == ff_sys::AVMediaType_AVMEDIA_TYPE_AUDIO {
found = Some(i);
break;
}
}
match found {
Some(idx) => idx,
None => {
let mut p = in_ctx;
ff_sys::avformat::close_input(&mut p);
return Err(EncodeError::MediaOperationFailed {
reason: format!("no audio stream found in input path={}", input.display()),
});
}
}
};
// ── Step 4: allocate output context ──────────────────────────────────────
let Some(output_str) = output.to_str() else {
let mut p = in_ctx;
ff_sys::avformat::close_input(&mut p);
return Err(EncodeError::Ffmpeg {
code: 0,
message: "output path is not valid UTF-8".to_string(),
});
};
let Ok(c_output) = std::ffi::CString::new(output_str) else {
let mut p = in_ctx;
ff_sys::avformat::close_input(&mut p);
return Err(EncodeError::Ffmpeg {
code: 0,
message: "output path contains null bytes".to_string(),
});
};
let mut out_ctx: *mut ff_sys::AVFormatContext = std::ptr::null_mut();
// SAFETY: c_output is a valid null-terminated C string; format is auto-detected from ext.
let ret = ff_sys::avformat_alloc_output_context2(
&mut out_ctx,
std::ptr::null_mut(),
std::ptr::null(),
c_output.as_ptr(),
);
if ret < 0 || out_ctx.is_null() {
let mut p = in_ctx;
ff_sys::avformat::close_input(&mut p);
return Err(EncodeError::from_ffmpeg_error(ret));
}
// ── Step 5: copy audio stream parameters to output ────────────────────────
// SAFETY: audio_stream_idx < nb_streams; streams is a valid array.
let in_stream = *(*in_ctx).streams.add(audio_stream_idx);
// SAFETY: out_ctx is non-null (avformat_alloc_output_context2 succeeded).
let out_stream = ff_sys::avformat_new_stream(out_ctx, std::ptr::null());
if out_stream.is_null() {
let mut p = in_ctx;
ff_sys::avformat::close_input(&mut p);
ff_sys::avformat_free_context(out_ctx);
return Err(EncodeError::Ffmpeg {
code: 0,
message: "avformat_new_stream failed".to_string(),
});
}
// SAFETY: both codecpar pointers are non-null (created by FFmpeg).
let ret = ff_sys::avcodec_parameters_copy((*out_stream).codecpar, (*in_stream).codecpar);
if ret < 0 {
let mut p = in_ctx;
ff_sys::avformat::close_input(&mut p);
ff_sys::avformat_free_context(out_ctx);
return Err(EncodeError::from_ffmpeg_error(ret));
}
// Clear codec_tag so the muxer assigns the correct value for the container.
(*(*out_stream).codecpar).codec_tag = 0;
// ── Step 6: open output file ──────────────────────────────────────────────
// SAFETY: output is a valid path; WRITE opens the file for writing.
let pb = match ff_sys::avformat::open_output(output, ff_sys::avformat::avio_flags::WRITE) {
Ok(pb) => pb,
Err(e) => {
let mut p = in_ctx;
ff_sys::avformat::close_input(&mut p);
ff_sys::avformat_free_context(out_ctx);
return Err(EncodeError::from_ffmpeg_error(e));
}
};
// SAFETY: out_ctx is non-null; pb is a valid AVIOContext.
(*out_ctx).pb = pb;
// ── Step 7: write header ──────────────────────────────────────────────────
// A non-zero return here usually means the codec is incompatible with the
// chosen output container. Wrap it as MediaOperationFailed with a clear
// message so callers know what went wrong.
// SAFETY: out_ctx is fully configured with the stream and pb set.
let ret = ff_sys::avformat_write_header(out_ctx, std::ptr::null_mut());
if ret < 0 {
// SAFETY: (*out_ctx).pb was set above and is non-null.
ff_sys::avformat::close_output(std::ptr::addr_of_mut!((*out_ctx).pb));
ff_sys::avformat_free_context(out_ctx);
let mut p = in_ctx;
ff_sys::avformat::close_input(&mut p);
return Err(EncodeError::MediaOperationFailed {
reason: format!(
"codec incompatible with output container: {}",
ff_sys::av_error_string(ret)
),
});
}
// Read time bases after avformat_write_header — the muxer may adjust them.
// SAFETY: stream pointers remain valid for the lifetime of their parent contexts.
let in_tb = (*in_stream).time_base;
let out_tb = (*out_stream).time_base;
log::debug!(
"audio extraction header written audio_stream_idx={audio_stream_idx} \
output={}",
output.display()
);
// ── Step 8: allocate packet ───────────────────────────────────────────────
// SAFETY: av_packet_alloc never returns null in practice (aborts on OOM).
let pkt = ff_sys::av_packet_alloc();
if pkt.is_null() {
ff_sys::av_write_trailer(out_ctx);
ff_sys::avformat::close_output(std::ptr::addr_of_mut!((*out_ctx).pb));
ff_sys::avformat_free_context(out_ctx);
let mut p = in_ctx;
ff_sys::avformat::close_input(&mut p);
return Err(EncodeError::Ffmpeg {
code: 0,
message: "av_packet_alloc failed".to_string(),
});
}
// ── Step 9: packet copy loop (audio stream only) ──────────────────────────
let mut loop_err: Option<EncodeError> = None;
'read: loop {
// SAFETY: in_ctx and pkt are valid non-null pointers.
match ff_sys::avformat::read_frame(in_ctx, pkt) {
Err(e) if e == ff_sys::error_codes::EOF => break 'read,
Err(e) => {
loop_err = Some(EncodeError::from_ffmpeg_error(e));
break 'read;
}
Ok(()) => {}
}
if (*pkt).stream_index as usize != audio_stream_idx {
// Skip non-audio packets.
ff_sys::av_packet_unref(pkt);
continue 'read;
}
// Rescale timestamps to the output stream's time base and remap index.
// SAFETY: pkt, in_tb, out_tb are valid plain-data values.
ff_sys::av_packet_rescale_ts(pkt, in_tb, out_tb);
(*pkt).stream_index = 0;
// SAFETY: out_ctx and pkt are valid.
let ret = ff_sys::av_interleaved_write_frame(out_ctx, pkt);
// av_interleaved_write_frame takes the packet's buf reference; unref to clear.
ff_sys::av_packet_unref(pkt);
if ret < 0 {
loop_err = Some(EncodeError::from_ffmpeg_error(ret));
break 'read;
}
}
// SAFETY: pkt was allocated by av_packet_alloc above and is still valid.
let mut pkt_ptr = pkt;
ff_sys::av_packet_free(&mut pkt_ptr);
// ── Step 10: write trailer ────────────────────────────────────────────────
// SAFETY: out_ctx is valid; write_header was called successfully.
ff_sys::av_write_trailer(out_ctx);
// ── Step 11: cleanup ──────────────────────────────────────────────────────
// SAFETY: (*out_ctx).pb is non-null (opened above; still set after write_header).
ff_sys::avformat::close_output(std::ptr::addr_of_mut!((*out_ctx).pb));
// SAFETY: out_ctx is non-null and was allocated by avformat_alloc_output_context2.
ff_sys::avformat_free_context(out_ctx);
// SAFETY: in_ctx is non-null (open_input succeeded).
let mut p = in_ctx;
ff_sys::avformat::close_input(&mut p);
log::info!(
"audio extracted output={} stream_index={audio_stream_idx}",
output.display()
);
match loop_err {
Some(e) => Err(e),
None => Ok(()),
}
}
// ── Audio addition ────────────────────────────────────────────────────────────
/// Mux `audio_input` into `video_input`, writing both streams to `output`.
///
/// The video bitstream is stream-copied (no decode/encode cycle). When
/// `loop_audio` is true and the audio is shorter than the video, the audio
/// track is looped by re-seeking to the start and advancing the PTS offset.
///
/// # Safety
///
/// All FFmpeg pointer invariants are maintained internally. The public
/// `AudioAdder::run` wraps this function safely.
pub(crate) fn run_audio_addition(
video_input: &Path,
audio_input: &Path,
output: &Path,
loop_audio: bool,
) -> Result<(), EncodeError> {
// SAFETY: All pointers are validated (null-checked) before use; resources
// are freed on every exit path.
unsafe { run_audio_addition_unsafe(video_input, audio_input, output, loop_audio) }
}
unsafe fn run_audio_addition_unsafe(
video_input: &Path,
audio_input: &Path,
output: &Path,
loop_audio: bool,
) -> Result<(), EncodeError> {
// ── Step 1: open video input ──────────────────────────────────────────────
// SAFETY: video_input is a caller-supplied path; open_input returns Err on failure.
let vid_ctx =
ff_sys::avformat::open_input(video_input).map_err(EncodeError::from_ffmpeg_error)?;
// ── Step 2: find stream info for video input ──────────────────────────────
// SAFETY: vid_ctx is non-null (open_input succeeded).
if let Err(e) = ff_sys::avformat::find_stream_info(vid_ctx) {
let mut p = vid_ctx;
ff_sys::avformat::close_input(&mut p);
return Err(EncodeError::from_ffmpeg_error(e));
}
// ── Step 3: locate the first video stream ─────────────────────────────────
// SAFETY: nb_streams is the valid count; streams is a valid array of that length.
let nb_vid_streams = (*vid_ctx).nb_streams as usize;
let mut video_stream_idx: Option<usize> = None;
for i in 0..nb_vid_streams {
let stream = *(*vid_ctx).streams.add(i);
if (*(*stream).codecpar).codec_type == ff_sys::AVMediaType_AVMEDIA_TYPE_VIDEO {
video_stream_idx = Some(i);
break;
}
}
let Some(video_stream_idx) = video_stream_idx else {
let mut p = vid_ctx;
ff_sys::avformat::close_input(&mut p);
return Err(EncodeError::MediaOperationFailed {
reason: format!(
"no video stream found in video input path={}",
video_input.display()
),
});
};
// ── Step 4: open audio input ──────────────────────────────────────────────
// SAFETY: audio_input is a caller-supplied path; open_input returns Err on failure.
let aud_ctx = match ff_sys::avformat::open_input(audio_input) {
Ok(ctx) => ctx,
Err(e) => {
let mut p = vid_ctx;
ff_sys::avformat::close_input(&mut p);
return Err(EncodeError::from_ffmpeg_error(e));
}
};
// ── Step 5: find stream info for audio input ──────────────────────────────
// SAFETY: aud_ctx is non-null (open_input succeeded).
if let Err(e) = ff_sys::avformat::find_stream_info(aud_ctx) {
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
return Err(EncodeError::from_ffmpeg_error(e));
}
// ── Step 6: locate the first audio stream ─────────────────────────────────
// SAFETY: nb_streams is the valid count; streams is a valid array of that length.
let nb_aud_streams = (*aud_ctx).nb_streams as usize;
let mut audio_stream_idx: Option<usize> = None;
for i in 0..nb_aud_streams {
let stream = *(*aud_ctx).streams.add(i);
if (*(*stream).codecpar).codec_type == ff_sys::AVMediaType_AVMEDIA_TYPE_AUDIO {
audio_stream_idx = Some(i);
break;
}
}
let Some(audio_stream_idx) = audio_stream_idx else {
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
return Err(EncodeError::MediaOperationFailed {
reason: format!(
"no audio stream found in audio input path={}",
audio_input.display()
),
});
};
// ── Step 7: decide whether to loop the audio ──────────────────────────────
// Loop only when requested AND audio duration < video duration.
// Durations are in AV_TIME_BASE (microseconds); a value ≤ 0 means unknown.
let vid_duration_us = (*vid_ctx).duration;
let aud_duration_us = (*aud_ctx).duration;
let should_loop = loop_audio
&& vid_duration_us > 0
&& aud_duration_us > 0
&& aud_duration_us < vid_duration_us;
// ── Step 8: allocate output context ──────────────────────────────────────
let Some(output_str) = output.to_str() else {
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
return Err(EncodeError::Ffmpeg {
code: 0,
message: "output path is not valid UTF-8".to_string(),
});
};
let Ok(c_output) = std::ffi::CString::new(output_str) else {
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
return Err(EncodeError::Ffmpeg {
code: 0,
message: "output path contains null bytes".to_string(),
});
};
let mut out_ctx: *mut ff_sys::AVFormatContext = std::ptr::null_mut();
// SAFETY: c_output is a valid null-terminated C string.
let ret = ff_sys::avformat_alloc_output_context2(
&mut out_ctx,
std::ptr::null_mut(),
std::ptr::null(),
c_output.as_ptr(),
);
if ret < 0 || out_ctx.is_null() {
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
return Err(EncodeError::from_ffmpeg_error(ret));
}
// ── Step 9: copy video stream parameters ─────────────────────────────────
// SAFETY: video_stream_idx < nb_vid_streams; streams is a valid array.
let vid_in_stream = *(*vid_ctx).streams.add(video_stream_idx);
// SAFETY: out_ctx is non-null (avformat_alloc_output_context2 succeeded).
let vid_out_stream = ff_sys::avformat_new_stream(out_ctx, std::ptr::null());
if vid_out_stream.is_null() {
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
ff_sys::avformat_free_context(out_ctx);
return Err(EncodeError::Ffmpeg {
code: 0,
message: "avformat_new_stream failed for video".to_string(),
});
}
// SAFETY: both codecpar pointers are non-null (created by FFmpeg).
let ret =
ff_sys::avcodec_parameters_copy((*vid_out_stream).codecpar, (*vid_in_stream).codecpar);
if ret < 0 {
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
ff_sys::avformat_free_context(out_ctx);
return Err(EncodeError::from_ffmpeg_error(ret));
}
// Clear codec_tag so the muxer assigns the correct value for the container.
(*(*vid_out_stream).codecpar).codec_tag = 0;
// ── Step 10: copy audio stream parameters ────────────────────────────────
// SAFETY: audio_stream_idx < nb_aud_streams; streams is a valid array.
let aud_in_stream = *(*aud_ctx).streams.add(audio_stream_idx);
// SAFETY: out_ctx is non-null (avformat_alloc_output_context2 succeeded).
let aud_out_stream = ff_sys::avformat_new_stream(out_ctx, std::ptr::null());
if aud_out_stream.is_null() {
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
ff_sys::avformat_free_context(out_ctx);
return Err(EncodeError::Ffmpeg {
code: 0,
message: "avformat_new_stream failed for audio".to_string(),
});
}
// SAFETY: both codecpar pointers are non-null (created by FFmpeg).
let ret =
ff_sys::avcodec_parameters_copy((*aud_out_stream).codecpar, (*aud_in_stream).codecpar);
if ret < 0 {
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
ff_sys::avformat_free_context(out_ctx);
return Err(EncodeError::from_ffmpeg_error(ret));
}
// Clear codec_tag so the muxer assigns the correct value for the container.
(*(*aud_out_stream).codecpar).codec_tag = 0;
// ── Step 11: open output file ─────────────────────────────────────────────
// SAFETY: output is a valid path; WRITE opens the file for writing.
let pb = match ff_sys::avformat::open_output(output, ff_sys::avformat::avio_flags::WRITE) {
Ok(pb) => pb,
Err(e) => {
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
ff_sys::avformat_free_context(out_ctx);
return Err(EncodeError::from_ffmpeg_error(e));
}
};
// SAFETY: out_ctx is non-null; pb is a valid AVIOContext.
(*out_ctx).pb = pb;
// ── Step 12: write header ─────────────────────────────────────────────────
// SAFETY: out_ctx is fully configured with streams and pb set.
let ret = ff_sys::avformat_write_header(out_ctx, std::ptr::null_mut());
if ret < 0 {
// SAFETY: (*out_ctx).pb was set above and is non-null.
ff_sys::avformat::close_output(std::ptr::addr_of_mut!((*out_ctx).pb));
ff_sys::avformat_free_context(out_ctx);
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
return Err(EncodeError::from_ffmpeg_error(ret));
}
// Read time bases after avformat_write_header — the muxer may adjust them.
// SAFETY: stream pointers remain valid for the lifetime of their parent contexts.
let vid_in_tb = (*vid_in_stream).time_base;
let aud_in_tb = (*aud_in_stream).time_base;
let vid_out_tb = (*vid_out_stream).time_base;
let aud_out_tb = (*aud_out_stream).time_base;
// Duration of the audio stream in its INPUT timebase — used to compute the
// PTS offset when the audio is looped. Fall back to 0 when unknown.
let aud_loop_duration_in_tb: i64 = if (*aud_in_stream).duration > 0 {
(*aud_in_stream).duration
} else {
0
};
log::debug!(
"audio addition header written should_loop={should_loop} \
video_stream_idx={video_stream_idx} audio_stream_idx={audio_stream_idx}"
);
// ── Step 13: allocate packet ──────────────────────────────────────────────
// SAFETY: av_packet_alloc never returns null in practice (aborts on OOM).
let pkt = ff_sys::av_packet_alloc();
if pkt.is_null() {
ff_sys::av_write_trailer(out_ctx);
ff_sys::avformat::close_output(std::ptr::addr_of_mut!((*out_ctx).pb));
ff_sys::avformat_free_context(out_ctx);
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
return Err(EncodeError::Ffmpeg {
code: 0,
message: "av_packet_alloc failed".to_string(),
});
}
// ── Step 14: interleaved packet copy loop ─────────────────────────────────
// Terminate when video is exhausted. Audio terminates naturally (non-loop)
// or is re-seeked with an advancing PTS offset (loop).
let mut add_loop_err: Option<EncodeError> = None;
let mut vid_eof = false;
let mut aud_eof = false;
// Cumulative PTS offset applied to looped audio packets (in audio IN timebase).
let mut aud_pts_offset_in_tb: i64 = 0;
'copy: loop {
// ── video packet ──────────────────────────────────────────────────
if !vid_eof {
// SAFETY: vid_ctx and pkt are valid non-null pointers.
match ff_sys::avformat::read_frame(vid_ctx, pkt) {
Err(e) if e == ff_sys::error_codes::EOF => {
vid_eof = true;
}
Err(e) => {
add_loop_err = Some(EncodeError::from_ffmpeg_error(e));
break 'copy;
}
Ok(()) => {
if (*pkt).stream_index as usize == video_stream_idx {
// SAFETY: pkt, vid_in_tb, vid_out_tb are valid plain-data values.
ff_sys::av_packet_rescale_ts(pkt, vid_in_tb, vid_out_tb);
(*pkt).stream_index = 0;
// SAFETY: out_ctx and pkt are valid.
let ret = ff_sys::av_interleaved_write_frame(out_ctx, pkt);
ff_sys::av_packet_unref(pkt);
if ret < 0 {
add_loop_err = Some(EncodeError::from_ffmpeg_error(ret));
break 'copy;
}
} else {
ff_sys::av_packet_unref(pkt);
}
}
}
}
// Stop as soon as video is done — no point reading more audio.
if vid_eof {
break 'copy;
}
// ── audio packet ──────────────────────────────────────────────────
if !aud_eof {
// SAFETY: aud_ctx and pkt are valid non-null pointers.
match ff_sys::avformat::read_frame(aud_ctx, pkt) {
Err(e) if e == ff_sys::error_codes::EOF => {
if should_loop {
// Re-seek audio to the start and advance the PTS offset
// so that looped packets continue from where the last
// packet ended.
// SAFETY: aud_ctx is non-null; seeking to timestamp 0.
let _ = ff_sys::avformat::seek_frame(
aud_ctx,
audio_stream_idx as i32,
0,
ff_sys::avformat::seek_flags::BACKWARD,
);
aud_pts_offset_in_tb += aud_loop_duration_in_tb;
// pkt was not filled on EOF; nothing to unref.
} else {
aud_eof = true;
}
}
Err(e) => {
add_loop_err = Some(EncodeError::from_ffmpeg_error(e));
break 'copy;
}
Ok(()) => {
if (*pkt).stream_index as usize == audio_stream_idx {
// Apply the cumulative loop offset before rescaling so
// that PTS values are monotonically increasing across loops.
if (*pkt).pts != ff_sys::AV_NOPTS_VALUE {
(*pkt).pts += aud_pts_offset_in_tb;
}
if (*pkt).dts != ff_sys::AV_NOPTS_VALUE {
(*pkt).dts += aud_pts_offset_in_tb;
}
// SAFETY: pkt, aud_in_tb, aud_out_tb are valid plain-data values.
ff_sys::av_packet_rescale_ts(pkt, aud_in_tb, aud_out_tb);
(*pkt).stream_index = 1;
// SAFETY: out_ctx and pkt are valid.
let ret = ff_sys::av_interleaved_write_frame(out_ctx, pkt);
ff_sys::av_packet_unref(pkt);
if ret < 0 {
add_loop_err = Some(EncodeError::from_ffmpeg_error(ret));
break 'copy;
}
} else {
ff_sys::av_packet_unref(pkt);
}
}
}
}
}
// SAFETY: pkt was allocated by av_packet_alloc above and is still valid.
let mut pkt_ptr = pkt;
ff_sys::av_packet_free(&mut pkt_ptr);
// ── Step 15: write trailer ────────────────────────────────────────────────
// SAFETY: out_ctx is valid; write_header was called successfully.
ff_sys::av_write_trailer(out_ctx);
// ── Step 16: cleanup ──────────────────────────────────────────────────────
// SAFETY: (*out_ctx).pb is non-null (opened above; still set after write_header).
ff_sys::avformat::close_output(std::ptr::addr_of_mut!((*out_ctx).pb));
// SAFETY: out_ctx is non-null and was allocated by avformat_alloc_output_context2.
ff_sys::avformat_free_context(out_ctx);
// SAFETY: vid_ctx and aud_ctx are non-null (open_input succeeded).
let mut pv = vid_ctx;
ff_sys::avformat::close_input(&mut pv);
let mut pa = aud_ctx;
ff_sys::avformat::close_input(&mut pa);
log::info!(
"audio added output={} loop_audio={loop_audio}",
output.display()
);
match add_loop_err {
Some(e) => Err(e),
None => Ok(()),
}
}