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
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
//! Internal audio encoder implementation.
//!
//! This module contains the internal implementation details of the audio encoder,
//! including FFmpeg context management and encoding operations.
// Rust 2024: Allow unsafe operations in unsafe functions for FFmpeg C API
#![allow(unsafe_code)]
#![allow(unsafe_op_in_unsafe_fn)]
// FFmpeg C API frequently requires raw pointer casting
#![allow(clippy::ptr_as_ptr)]
#![allow(clippy::cast_possible_wrap)]
use crate::audio::codec_options::{AudioCodecOptions, Mp3Quality};
use crate::{AudioCodec, EncodeError};
use ff_format::AudioFrame;
use ff_sys::{
AVAudioFifo, AVChannelLayout, AVCodecContext, AVCodecID, AVCodecID_AV_CODEC_ID_AAC,
AVCodecID_AV_CODEC_ID_AC3, AVCodecID_AV_CODEC_ID_ALAC, AVCodecID_AV_CODEC_ID_DTS,
AVCodecID_AV_CODEC_ID_EAC3, AVCodecID_AV_CODEC_ID_FLAC, AVCodecID_AV_CODEC_ID_MP3,
AVCodecID_AV_CODEC_ID_NONE, AVCodecID_AV_CODEC_ID_OPUS, AVCodecID_AV_CODEC_ID_PCM_S16LE,
AVCodecID_AV_CODEC_ID_PCM_S24LE, AVCodecID_AV_CODEC_ID_VORBIS, AVFormatContext, AVFrame,
SwrContext, av_frame_alloc, av_frame_free, av_interleaved_write_frame, av_packet_alloc,
av_packet_free, av_packet_unref, av_write_trailer, avcodec, avformat_alloc_output_context2,
avformat_free_context, avformat_new_stream, avformat_write_header, swresample,
};
use std::ffi::{CString, c_void};
use std::ptr;
/// Internal encoder state with FFmpeg contexts.
pub(super) struct AudioEncoderInner {
/// Output format context
pub(super) format_ctx: *mut AVFormatContext,
/// Audio codec context
pub(super) codec_ctx: Option<*mut AVCodecContext>,
/// Audio stream index
pub(super) stream_index: i32,
/// Resampling context for audio format conversion
pub(super) swr_ctx: Option<*mut SwrContext>,
/// Sample counter
pub(super) sample_count: u64,
/// Bytes written
pub(super) bytes_written: u64,
/// Actual audio codec name being used
pub(super) actual_codec: String,
/// FFmpeg format-aware sample FIFO. Non-null when the encoder requires a
/// fixed number of samples per frame (e.g. AAC: 1024, FLAC: 4096).
fifo: *mut AVAudioFifo,
/// Required samples per frame; 0 when the encoder accepts variable sizes.
frame_size: usize,
}
/// AudioEncoder configuration (stored from builder).
#[derive(Debug, Clone)]
pub(super) struct AudioEncoderConfig {
pub(super) path: std::path::PathBuf,
pub(super) sample_rate: u32,
pub(super) channels: u32,
pub(super) codec: AudioCodec,
pub(super) bitrate: Option<u64>,
pub(super) codec_options: Option<AudioCodecOptions>,
pub(super) _progress_callback: bool,
}
impl AudioEncoderInner {
/// Create a new encoder with the given configuration.
pub(super) fn new(config: &AudioEncoderConfig) -> Result<Self, EncodeError> {
unsafe {
ff_sys::ensure_initialized();
// Allocate output format context
let c_path = CString::new(config.path.to_str().ok_or_else(|| {
EncodeError::CannotCreateFile {
path: config.path.clone(),
}
})?)
.map_err(|_| EncodeError::CannotCreateFile {
path: config.path.clone(),
})?;
let mut format_ctx: *mut AVFormatContext = ptr::null_mut();
let ret = avformat_alloc_output_context2(
&mut format_ctx,
ptr::null_mut(),
ptr::null(),
c_path.as_ptr(),
);
if ret < 0 || format_ctx.is_null() {
return Err(EncodeError::Ffmpeg {
code: ret,
message: format!(
"Cannot create output context: {}",
ff_sys::av_error_string(ret)
),
});
}
let mut encoder = Self {
format_ctx,
codec_ctx: None,
stream_index: -1,
swr_ctx: None,
sample_count: 0,
bytes_written: 0,
actual_codec: String::new(),
fifo: ptr::null_mut(),
frame_size: 0,
};
// Initialize audio encoder
encoder.init_audio_encoder(config)?;
// Open output file
match ff_sys::avformat::open_output(&config.path, ff_sys::avformat::avio_flags::WRITE) {
Ok(pb) => (*format_ctx).pb = pb,
Err(_) => {
encoder.cleanup();
return Err(EncodeError::CannotCreateFile {
path: config.path.clone(),
});
}
}
// Write file header
let ret = avformat_write_header(format_ctx, ptr::null_mut());
if ret < 0 {
encoder.cleanup();
return Err(EncodeError::Ffmpeg {
code: ret,
message: format!("Cannot write header: {}", ff_sys::av_error_string(ret)),
});
}
Ok(encoder)
}
}
/// Initialize audio encoder.
unsafe fn init_audio_encoder(
&mut self,
config: &AudioEncoderConfig,
) -> Result<(), EncodeError> {
// Select encoder based on codec and availability
let encoder_name = self.select_audio_encoder(config.codec)?;
self.actual_codec = encoder_name.clone();
let c_encoder_name =
CString::new(encoder_name.as_str()).map_err(|_| EncodeError::Ffmpeg {
code: 0,
message: "Invalid encoder name".to_string(),
})?;
let codec_ptr =
avcodec::find_encoder_by_name(c_encoder_name.as_ptr()).ok_or_else(|| {
EncodeError::NoSuitableEncoder {
codec: format!("{:?}", config.codec),
tried: vec![encoder_name.clone()],
}
})?;
// Allocate codec context
let mut codec_ctx =
avcodec::alloc_context3(codec_ptr).map_err(EncodeError::from_ffmpeg_error)?;
// Configure codec context
(*codec_ctx).codec_id = codec_to_id(config.codec);
(*codec_ctx).sample_rate = config.sample_rate as i32;
// Set channel layout using FFmpeg 7.x API
swresample::channel_layout::set_default(
&mut (*codec_ctx).ch_layout,
config.channels as i32,
);
// Select the first sample format the codec actually supports; fall back to FLTP.
// Reading sample_fmts before avcodec_open2 is required — the encoder uses
// this value to decide its internal pipeline.
let target_fmt = {
let fmts = (*codec_ptr).sample_fmts;
if !fmts.is_null() && *fmts != ff_sys::swresample::sample_format::NONE {
// SAFETY: sample_fmts is a null-terminated array owned by the codec descriptor
*fmts
} else {
ff_sys::swresample::sample_format::FLTP
}
};
(*codec_ctx).sample_fmt = target_fmt;
// Set bitrate
if let Some(br) = config.bitrate {
(*codec_ctx).bit_rate = br as i64;
} else {
// Default bitrate based on codec
(*codec_ctx).bit_rate = match config.codec {
AudioCodec::Aac => 192_000,
AudioCodec::Opus => 128_000,
AudioCodec::Mp3 => 192_000,
AudioCodec::Flac => 0, // Lossless
AudioCodec::Pcm => 0, // Uncompressed
AudioCodec::Pcm16 => 0, // Uncompressed
AudioCodec::Pcm24 => 0, // Uncompressed
AudioCodec::Vorbis => 192_000,
AudioCodec::Ac3 => 192_000,
AudioCodec::Eac3 => 192_000,
AudioCodec::Dts => 0, // Lossless/variable
AudioCodec::Alac => 0, // Lossless
_ => 192_000,
};
}
// Set time base
(*codec_ctx).time_base.num = 1;
(*codec_ctx).time_base.den = config.sample_rate as i32;
// Apply per-codec options before opening the codec context.
if let Some(opts) = &config.codec_options {
// SAFETY: codec_ctx is valid and allocated; priv_data is set by
// avcodec_alloc_context3. Options are applied before avcodec_open2
// so they take effect during codec initialisation.
Self::apply_codec_options(codec_ctx, opts, &encoder_name);
}
// Open codec
avcodec::open2(codec_ctx, codec_ptr, ptr::null_mut())
.map_err(EncodeError::from_ffmpeg_error)?;
// After open2, frame_size is populated. Allocate an AVAudioFifo when
// the encoder requires a fixed number of samples and does not advertise
// VARIABLE_FRAME_SIZE. AVAudioFifo handles both planar and packed
// layouts internally and is backed by a ring-buffer (O(1) read/write).
let required = (*codec_ctx).frame_size as usize;
let caps = (*codec_ptr).capabilities as u32;
if required > 0 && caps & ff_sys::avcodec::codec_caps::VARIABLE_FRAME_SIZE == 0 {
self.fifo =
swresample::audio_fifo::alloc(target_fmt, config.channels as i32, required as i32)
.map_err(|e| EncodeError::Ffmpeg {
code: e,
message: format!(
"Cannot allocate audio FIFO: {}",
ff_sys::av_error_string(e)
),
})?;
self.frame_size = required;
}
// Create stream
let stream = avformat_new_stream(self.format_ctx, codec_ptr);
if stream.is_null() {
avcodec::free_context(&mut codec_ctx as *mut *mut _);
return Err(EncodeError::Ffmpeg {
code: 0,
message: "Cannot create stream".to_string(),
});
}
(*stream).time_base = (*codec_ctx).time_base;
// Copy ALL codec parameters (including extradata) from the open codec
// context to the stream. avcodec_parameters_from_context must be
// called after avcodec_open2 because some codecs (e.g. FLAC, AAC)
// populate extradata only after the codec is opened. Manual field
// copies would miss extradata, causing avformat_write_header to fail.
avcodec::parameters_from_context((*stream).codecpar, codec_ctx)
.map_err(EncodeError::from_ffmpeg_error)?;
self.stream_index = ((*self.format_ctx).nb_streams - 1) as i32;
self.codec_ctx = Some(codec_ctx);
Ok(())
}
/// Apply per-codec options via `av_opt_set` before `avcodec_open2`.
///
/// All `av_opt_set` return values are checked; a negative value is logged
/// as a warning and the option is skipped (never returns an error).
unsafe fn apply_codec_options(
codec_ctx: *mut AVCodecContext,
opts: &AudioCodecOptions,
encoder_name: &str,
) {
match opts {
AudioCodecOptions::Opus(opus) => {
// application
if let Ok(s) = CString::new(opus.application.as_str()) {
// SAFETY: codec_ctx and priv_data are non-null; string is NUL-terminated.
let ret = ff_sys::av_opt_set(
(*codec_ctx).priv_data,
b"application\0".as_ptr() as *const i8,
s.as_ptr(),
0,
);
if ret < 0 {
log::warn!(
"av_opt_set failed option=application value={} encoder={encoder_name}",
opus.application.as_str()
);
}
}
// frame_duration (libopus expects microseconds)
if let Some(dur_ms) = opus.frame_duration_ms {
let dur_us_str = (i64::from(dur_ms) * 1000).to_string();
if let Ok(s) = CString::new(dur_us_str.as_str()) {
// SAFETY: codec_ctx and priv_data are non-null; string is NUL-terminated.
let ret = ff_sys::av_opt_set(
(*codec_ctx).priv_data,
b"frame_duration\0".as_ptr() as *const i8,
s.as_ptr(),
0,
);
if ret < 0 {
log::warn!(
"av_opt_set failed option=frame_duration value={dur_us_str} \
encoder={encoder_name}"
);
}
}
}
}
AudioCodecOptions::Aac(aac) => {
// profile (aac_low / aac_he / aac_he_v2)
let profile_str = aac.profile.as_str();
if let Ok(s) = CString::new(profile_str) {
// SAFETY: codec_ctx and priv_data are non-null; string is NUL-terminated.
let ret = ff_sys::av_opt_set(
(*codec_ctx).priv_data,
b"profile\0".as_ptr() as *const i8,
s.as_ptr(),
0,
);
if ret < 0 {
log::warn!(
"AAC profile={profile_str} not supported by encoder \
ret={ret} encoder={encoder_name}"
);
}
}
// vbr (libfdk_aac VBR quality 1–5)
if let Some(q) = aac.vbr_quality {
let q_str = q.to_string();
if let Ok(s) = CString::new(q_str.as_str()) {
// SAFETY: codec_ctx and priv_data are non-null; string is NUL-terminated.
let ret = ff_sys::av_opt_set(
(*codec_ctx).priv_data,
b"vbr\0".as_ptr() as *const i8,
s.as_ptr(),
0,
);
if ret < 0 {
log::warn!(
"av_opt_set failed option=vbr value={q} \
encoder={encoder_name}"
);
}
}
}
}
AudioCodecOptions::Mp3(mp3) => {
match mp3.quality {
Mp3Quality::Vbr(q) => {
// VBR mode: override bitrate to 0 and set the libmp3lame q scale.
// SAFETY: codec_ctx is non-null; direct field write is safe.
(*codec_ctx).bit_rate = 0;
let q_str = q.to_string();
if let Ok(s) = CString::new(q_str.as_str()) {
// SAFETY: codec_ctx and priv_data are non-null; string is NUL-terminated.
let ret = ff_sys::av_opt_set(
(*codec_ctx).priv_data,
b"q\0".as_ptr() as *const i8,
s.as_ptr(),
0,
);
if ret < 0 {
log::warn!(
"av_opt_set failed option=q value={q} \
encoder={encoder_name}"
);
}
}
}
Mp3Quality::Cbr(bitrate) => {
// CBR mode: set the fixed bitrate directly on the codec context.
// SAFETY: codec_ctx is non-null; direct field write is safe.
(*codec_ctx).bit_rate = i64::from(bitrate);
}
}
}
AudioCodecOptions::Flac(flac) => {
// compression_level
let level_str = flac.compression_level.to_string();
if let Ok(s) = CString::new(level_str.as_str()) {
// SAFETY: codec_ctx and priv_data are non-null; string is NUL-terminated.
let ret = ff_sys::av_opt_set(
(*codec_ctx).priv_data,
b"compression_level\0".as_ptr() as *const i8,
s.as_ptr(),
0,
);
if ret < 0 {
log::warn!(
"av_opt_set failed option=compression_level value={} \
encoder={encoder_name}",
flac.compression_level
);
}
}
}
}
}
/// Select best available audio encoder for the given codec.
fn select_audio_encoder(&self, codec: AudioCodec) -> Result<String, EncodeError> {
let candidates: Vec<&str> = match codec {
AudioCodec::Aac => vec!["aac", "libfdk_aac"],
AudioCodec::Opus => vec!["libopus"],
AudioCodec::Mp3 => vec!["libmp3lame", "mp3"],
AudioCodec::Flac => vec!["flac"],
AudioCodec::Pcm => vec!["pcm_s16le"],
AudioCodec::Pcm16 => vec!["pcm_s16le"],
AudioCodec::Pcm24 => vec!["pcm_s24le"],
AudioCodec::Vorbis => vec!["libvorbis", "vorbis"],
AudioCodec::Ac3 => vec!["ac3"],
AudioCodec::Eac3 => vec!["eac3"],
AudioCodec::Dts => vec![],
AudioCodec::Alac => vec!["alac"],
_ => vec![],
};
// Try each candidate
for &name in &candidates {
unsafe {
let c_name = CString::new(name).map_err(|_| EncodeError::Ffmpeg {
code: 0,
message: "Invalid encoder name".to_string(),
})?;
if avcodec::find_encoder_by_name(c_name.as_ptr()).is_some() {
return Ok(name.to_string());
}
}
}
Err(EncodeError::NoSuitableEncoder {
codec: format!("{:?}", codec),
tried: candidates.iter().map(|s| (*s).to_string()).collect(),
})
}
/// Push an audio frame for encoding.
pub(super) fn push_frame(&mut self, frame: &AudioFrame) -> Result<(), EncodeError> {
// SAFETY: self is properly initialised; all raw FFmpeg pointers are valid and exclusively owned.
unsafe {
let codec_ctx = self.codec_ctx.ok_or_else(|| EncodeError::InvalidConfig {
reason: "Audio codec not initialized".to_string(),
})?;
if !self.fifo.is_null() {
// Fixed-frame-size path: convert → write into AVAudioFifo → drain
// complete frames. AVAudioFifo manages the ring buffer internally.
let mut av_frame = av_frame_alloc();
if av_frame.is_null() {
return Err(EncodeError::Ffmpeg {
code: 0,
message: "Cannot allocate frame".to_string(),
});
}
let convert_result = self.convert_audio_frame(frame, av_frame);
if let Err(e) = convert_result {
av_frame_free(&mut av_frame as *mut *mut _);
return Err(e);
}
let nb_samples = (*av_frame).nb_samples;
// Write converted samples into AVAudioFifo.
// SAFETY: av_frame data buffers were allocated by convert_audio_frame
let write_result = swresample::audio_fifo::write(
self.fifo,
(*av_frame).data.as_ptr().cast::<*mut c_void>(),
nb_samples,
);
av_frame_free(&mut av_frame as *mut *mut _);
write_result.map_err(|e| EncodeError::Ffmpeg {
code: e,
message: format!(
"Failed to write to audio FIFO: {}",
ff_sys::av_error_string(e)
),
})?;
// Drain all complete frames from the FIFO
let frame_size = self.frame_size as i32;
while swresample::audio_fifo::size(self.fifo) >= frame_size {
self.drain_fifo_frame(codec_ctx, frame_size, false)?;
}
} else {
// Direct path: send frame straight to the encoder.
let mut av_frame = av_frame_alloc();
if av_frame.is_null() {
return Err(EncodeError::Ffmpeg {
code: 0,
message: "Cannot allocate frame".to_string(),
});
}
let convert_result = self.convert_audio_frame(frame, av_frame);
if let Err(e) = convert_result {
av_frame_free(&mut av_frame as *mut *mut _);
return Err(e);
}
(*av_frame).pts = self.sample_count as i64;
let send_result = avcodec::send_frame(codec_ctx, av_frame);
if let Err(e) = send_result {
av_frame_free(&mut av_frame as *mut *mut _);
return Err(EncodeError::Ffmpeg {
code: e,
message: format!(
"Failed to send audio frame: {}",
ff_sys::av_error_string(e)
),
});
}
let receive_result = self.receive_packets();
av_frame_free(&mut av_frame as *mut *mut _);
receive_result?;
self.sample_count += frame.samples() as u64;
}
Ok(())
} // unsafe
}
/// Read `frame_size` samples from the FIFO into a new AVFrame and encode it.
///
/// When `zero_pad` is `true` the frame buffer is zeroed before reading so
/// that a partial tail (fewer samples than `frame_size`) is silence-padded
/// to the required length. `zero_pad` should be `false` in the normal drain
/// loop (FIFO always contains a full frame's worth of samples) and `true`
/// only in the EOF flush called from [`Self::finish`].
unsafe fn drain_fifo_frame(
&mut self,
codec_ctx: *mut AVCodecContext,
frame_size: i32,
zero_pad: bool,
) -> Result<(), EncodeError> {
let mut av_frame = av_frame_alloc();
if av_frame.is_null() {
return Err(EncodeError::Ffmpeg {
code: 0,
message: "Cannot allocate frame".to_string(),
});
}
(*av_frame).format = (*codec_ctx).sample_fmt;
(*av_frame).sample_rate = (*codec_ctx).sample_rate;
(*av_frame).nb_samples = frame_size;
(*av_frame).pts = self.sample_count as i64;
if let Err(e) =
swresample::channel_layout::copy(&mut (*av_frame).ch_layout, &(*codec_ctx).ch_layout)
{
av_frame_free(&mut av_frame as *mut *mut _);
return Err(EncodeError::from_ffmpeg_error(e));
}
let ret = ff_sys::av_frame_get_buffer(av_frame, 0);
if ret < 0 {
av_frame_free(&mut av_frame as *mut *mut _);
return Err(EncodeError::Ffmpeg {
code: ret,
message: format!(
"Cannot allocate audio frame buffer: {}",
ff_sys::av_error_string(ret)
),
});
}
if zero_pad {
// Zero all plane buffers so the unread tail is silence, not garbage.
// linesize[i] gives the allocated size of each plane in bytes.
// SAFETY: av_frame_get_buffer guarantees data[i] and linesize[i] are consistent
for i in 0..8 {
if !(*av_frame).data[i].is_null() {
let sz = (*av_frame).linesize[i].max(0) as usize;
ptr::write_bytes((*av_frame).data[i], 0, sz);
}
}
}
// Read from AVAudioFifo into the frame's plane buffers.
// For zero_pad=false: FIFO has >= frame_size samples → returns exactly frame_size.
// For zero_pad=true: FIFO has < frame_size samples → returns < frame_size;
// the zeroed tail provides silence padding.
// SAFETY: av_frame_get_buffer allocated the plane buffers; they are large enough
if let Err(e) = swresample::audio_fifo::read(
self.fifo,
(*av_frame).data.as_ptr().cast::<*mut c_void>(),
frame_size,
) {
av_frame_free(&mut av_frame as *mut *mut _);
return Err(EncodeError::Ffmpeg {
code: e,
message: format!(
"Failed to read from audio FIFO: {}",
ff_sys::av_error_string(e)
),
});
}
// nb_samples stays as frame_size: the encoder always receives a full frame.
let send_result = avcodec::send_frame(codec_ctx, av_frame);
av_frame_free(&mut av_frame as *mut *mut _);
send_result.map_err(|e| EncodeError::Ffmpeg {
code: e,
message: format!("Failed to send audio frame: {}", ff_sys::av_error_string(e)),
})?;
self.receive_packets()?;
self.sample_count += frame_size as u64;
Ok(())
}
/// Convert AudioFrame to AVFrame with resampling if needed.
unsafe fn convert_audio_frame(
&mut self,
src: &AudioFrame,
dst: *mut AVFrame,
) -> Result<(), EncodeError> {
let codec_ctx = self.codec_ctx.ok_or_else(|| EncodeError::InvalidConfig {
reason: "Audio codec not initialized".to_string(),
})?;
let target_sample_rate = (*codec_ctx).sample_rate;
let target_format = (*codec_ctx).sample_fmt;
let target_ch_layout = &(*codec_ctx).ch_layout;
// Check if we need to resample
let src_sample_rate = src.sample_rate() as i32;
let src_format = sample_format_to_av(src.format());
let src_ch_layout = {
let mut layout = AVChannelLayout::default();
swresample::channel_layout::set_default(&mut layout, src.channels() as i32);
layout
};
let needs_resampling = src_sample_rate != target_sample_rate
|| src_format != target_format
|| !swresample::channel_layout::is_equal(&src_ch_layout, target_ch_layout);
if needs_resampling {
// Initialize resampler if needed
if self.swr_ctx.is_none() {
let swr_ctx = swresample::alloc_set_opts2(
target_ch_layout,
target_format,
target_sample_rate,
&src_ch_layout,
src_format,
src_sample_rate,
)
.map_err(EncodeError::from_ffmpeg_error)?;
swresample::init(swr_ctx).map_err(EncodeError::from_ffmpeg_error)?;
self.swr_ctx = Some(swr_ctx);
}
let swr_ctx = self.swr_ctx.ok_or_else(|| EncodeError::Ffmpeg {
code: 0,
message: "Resampling context not initialized".to_string(),
})?;
// Estimate output sample count
let out_samples = swresample::estimate_output_samples(
target_sample_rate,
src_sample_rate,
src.samples() as i32,
);
// Set frame properties
(*dst).format = target_format;
(*dst).sample_rate = target_sample_rate;
(*dst).nb_samples = out_samples;
// Copy target channel layout
swresample::channel_layout::copy(&mut (*dst).ch_layout, target_ch_layout)
.map_err(EncodeError::from_ffmpeg_error)?;
// Allocate frame buffer
let ret = ff_sys::av_frame_get_buffer(dst, 0);
if ret < 0 {
return Err(EncodeError::Ffmpeg {
code: ret,
message: format!(
"Cannot allocate audio frame buffer: {}",
ff_sys::av_error_string(ret)
),
});
}
// Prepare input pointers
let in_ptrs: Vec<*const u8> = if src.format().is_planar() {
// Planar: one pointer per channel
src.planes().iter().map(|p| p.as_ptr()).collect()
} else {
// Packed: single pointer
vec![src.planes()[0].as_ptr()]
};
// Convert
let samples_out = swresample::convert(
swr_ctx,
(*dst).data.as_mut_ptr().cast(),
out_samples,
in_ptrs.as_ptr(),
src.samples() as i32,
)
.map_err(EncodeError::from_ffmpeg_error)?;
(*dst).nb_samples = samples_out;
} else {
// No resampling needed, direct copy
(*dst).format = src_format;
(*dst).sample_rate = src_sample_rate;
(*dst).nb_samples = src.samples() as i32;
// Copy channel layout
swresample::channel_layout::copy(&mut (*dst).ch_layout, &src_ch_layout)
.map_err(EncodeError::from_ffmpeg_error)?;
// Allocate frame buffer
let ret = ff_sys::av_frame_get_buffer(dst, 0);
if ret < 0 {
return Err(EncodeError::Ffmpeg {
code: ret,
message: format!(
"Cannot allocate audio frame buffer: {}",
ff_sys::av_error_string(ret)
),
});
}
// Copy audio data
if src.format().is_planar() {
// Copy each plane
for (i, plane) in src.planes().iter().enumerate() {
if i < (*dst).data.len() && !(*dst).data[i].is_null() {
let size = plane.len();
ptr::copy_nonoverlapping(plane.as_ptr(), (*dst).data[i], size);
}
}
} else {
// Copy single packed buffer
if !(*dst).data[0].is_null() {
let size = src.planes()[0].len();
ptr::copy_nonoverlapping(src.planes()[0].as_ptr(), (*dst).data[0], size);
}
}
}
Ok(())
}
/// Receive encoded packets from the encoder.
unsafe fn receive_packets(&mut self) -> Result<(), EncodeError> {
let codec_ctx = self.codec_ctx.ok_or_else(|| EncodeError::InvalidConfig {
reason: "Audio codec not initialized".to_string(),
})?;
let mut packet = av_packet_alloc();
if packet.is_null() {
return Err(EncodeError::Ffmpeg {
code: 0,
message: "Cannot allocate packet".to_string(),
});
}
loop {
match avcodec::receive_packet(codec_ctx, packet) {
Ok(()) => {
// Packet received successfully
}
Err(e) if e == ff_sys::error_codes::EAGAIN || e == ff_sys::error_codes::EOF => {
// No more packets available
break;
}
Err(e) => {
av_packet_free(&mut packet as *mut *mut _);
return Err(EncodeError::Ffmpeg {
code: e,
message: format!(
"Error receiving audio packet: {}",
ff_sys::av_error_string(e)
),
});
}
}
// Set stream index
(*packet).stream_index = self.stream_index;
// Write packet
let write_ret = av_interleaved_write_frame(self.format_ctx, packet);
if write_ret < 0 {
av_packet_unref(packet);
av_packet_free(&mut packet as *mut *mut _);
return Err(EncodeError::MuxingFailed {
reason: ff_sys::av_error_string(write_ret),
});
}
self.bytes_written += (*packet).size as u64;
av_packet_unref(packet);
}
av_packet_free(&mut packet as *mut *mut _);
Ok(())
}
/// Finish encoding and write trailer.
pub(super) fn finish(&mut self) -> Result<(), EncodeError> {
// SAFETY: self is properly initialised; all raw FFmpeg pointers are valid and exclusively owned.
unsafe {
// Flush any remaining samples from the AVAudioFifo (silence-padded to a
// full frame so the encoder always receives its required frame_size).
if !self.fifo.is_null() && swresample::audio_fifo::size(self.fifo) > 0 {
let codec_ctx = self.codec_ctx.ok_or_else(|| EncodeError::InvalidConfig {
reason: "Audio codec not initialized".to_string(),
})?;
self.drain_fifo_frame(codec_ctx, self.frame_size as i32, true)?;
}
// Flush audio encoder
if let Some(codec_ctx) = self.codec_ctx {
// Send NULL frame to flush
avcodec::send_frame(codec_ctx, ptr::null())
.map_err(EncodeError::from_ffmpeg_error)?;
self.receive_packets()?;
}
// Write trailer
let ret = av_write_trailer(self.format_ctx);
if ret < 0 {
return Err(EncodeError::Ffmpeg {
code: ret,
message: format!("Cannot write trailer: {}", ff_sys::av_error_string(ret)),
});
}
Ok(())
} // unsafe
}
/// Cleanup FFmpeg resources.
unsafe fn cleanup(&mut self) {
// Free AVAudioFifo
if !self.fifo.is_null() {
swresample::audio_fifo::free(self.fifo);
self.fifo = ptr::null_mut();
}
// Free audio codec context
if let Some(mut ctx) = self.codec_ctx.take() {
avcodec::free_context(&mut ctx as *mut *mut _);
}
// Free resampling context
if let Some(mut ctx) = self.swr_ctx.take() {
swresample::free(&mut ctx as *mut *mut _);
}
// Close output file
if !self.format_ctx.is_null() {
if !(*self.format_ctx).pb.is_null() {
ff_sys::avformat::close_output(&mut (*self.format_ctx).pb);
}
avformat_free_context(self.format_ctx);
self.format_ctx = ptr::null_mut();
}
}
}
impl Drop for AudioEncoderInner {
fn drop(&mut self) {
// SAFETY: We own all the FFmpeg resources and need to free them
unsafe {
self.cleanup();
}
}
}
// Helper functions
/// Convert AudioCodec to FFmpeg AVCodecID.
fn codec_to_id(codec: AudioCodec) -> AVCodecID {
match codec {
AudioCodec::Aac => AVCodecID_AV_CODEC_ID_AAC,
AudioCodec::Opus => AVCodecID_AV_CODEC_ID_OPUS,
AudioCodec::Mp3 => AVCodecID_AV_CODEC_ID_MP3,
AudioCodec::Flac => AVCodecID_AV_CODEC_ID_FLAC,
AudioCodec::Pcm => AVCodecID_AV_CODEC_ID_PCM_S16LE,
AudioCodec::Pcm16 => AVCodecID_AV_CODEC_ID_PCM_S16LE,
AudioCodec::Pcm24 => AVCodecID_AV_CODEC_ID_PCM_S24LE,
AudioCodec::Vorbis => AVCodecID_AV_CODEC_ID_VORBIS,
AudioCodec::Ac3 => AVCodecID_AV_CODEC_ID_AC3,
AudioCodec::Eac3 => AVCodecID_AV_CODEC_ID_EAC3,
AudioCodec::Dts => AVCodecID_AV_CODEC_ID_DTS,
AudioCodec::Alac => AVCodecID_AV_CODEC_ID_ALAC,
_ => AVCodecID_AV_CODEC_ID_NONE,
}
}
/// Convert ff-format SampleFormat to FFmpeg AVSampleFormat.
fn sample_format_to_av(format: ff_format::SampleFormat) -> ff_sys::AVSampleFormat {
use ff_format::SampleFormat;
use ff_sys::swresample::sample_format;
match format {
SampleFormat::U8 => sample_format::U8,
SampleFormat::I16 => sample_format::S16,
SampleFormat::I32 => sample_format::S32,
SampleFormat::F32 => sample_format::FLT,
SampleFormat::F64 => sample_format::DBL,
SampleFormat::U8p => sample_format::U8P,
SampleFormat::I16p => sample_format::S16P,
SampleFormat::I32p => sample_format::S32P,
SampleFormat::F32p => sample_format::FLTP,
SampleFormat::F64p => sample_format::DBLP,
_ => {
log::warn!(
"sample_format has no AV mapping, falling back to FLTP \
format={format:?} fallback=FLTP"
);
sample_format::FLTP
}
}
}
// SAFETY: AudioEncoderInner owns all FFmpeg contexts exclusively.
// These contexts are not accessed from multiple threads simultaneously;
// all access is serialized by whichever thread holds the AudioEncoder.
// Ownership transfer between threads is safe because FFmpeg contexts
// are created and destroyed on the same thread (via std::thread::spawn).
unsafe impl Send for AudioEncoderInner {}
#[cfg(test)]
mod tests {
use ff_format::SampleFormat;
use ff_sys::swresample::sample_format;
use ff_sys::{
AVCodecID_AV_CODEC_ID_AAC, AVCodecID_AV_CODEC_ID_FLAC, AVCodecID_AV_CODEC_ID_MP3,
AVCodecID_AV_CODEC_ID_OPUS, AVCodecID_AV_CODEC_ID_PCM_S16LE, AVCodecID_AV_CODEC_ID_VORBIS,
};
use crate::AudioCodec;
use super::{codec_to_id, sample_format_to_av};
// -------------------------------------------------------------------------
// codec_to_id
// -------------------------------------------------------------------------
#[test]
fn codec_to_id_aac() {
assert_eq!(codec_to_id(AudioCodec::Aac), AVCodecID_AV_CODEC_ID_AAC);
}
#[test]
fn codec_to_id_opus() {
assert_eq!(codec_to_id(AudioCodec::Opus), AVCodecID_AV_CODEC_ID_OPUS);
}
#[test]
fn codec_to_id_mp3() {
assert_eq!(codec_to_id(AudioCodec::Mp3), AVCodecID_AV_CODEC_ID_MP3);
}
#[test]
fn codec_to_id_flac() {
assert_eq!(codec_to_id(AudioCodec::Flac), AVCodecID_AV_CODEC_ID_FLAC);
}
#[test]
fn codec_to_id_pcm() {
assert_eq!(
codec_to_id(AudioCodec::Pcm),
AVCodecID_AV_CODEC_ID_PCM_S16LE
);
}
#[test]
fn codec_to_id_vorbis() {
assert_eq!(
codec_to_id(AudioCodec::Vorbis),
AVCodecID_AV_CODEC_ID_VORBIS
);
}
// -------------------------------------------------------------------------
// sample_format_to_av
// -------------------------------------------------------------------------
#[test]
fn sample_format_u8() {
assert_eq!(sample_format_to_av(SampleFormat::U8), sample_format::U8);
}
#[test]
fn sample_format_i16() {
assert_eq!(sample_format_to_av(SampleFormat::I16), sample_format::S16);
}
#[test]
fn sample_format_i32() {
assert_eq!(sample_format_to_av(SampleFormat::I32), sample_format::S32);
}
#[test]
fn sample_format_f32() {
assert_eq!(sample_format_to_av(SampleFormat::F32), sample_format::FLT);
}
#[test]
fn sample_format_f64() {
assert_eq!(sample_format_to_av(SampleFormat::F64), sample_format::DBL);
}
#[test]
fn sample_format_u8p() {
assert_eq!(sample_format_to_av(SampleFormat::U8p), sample_format::U8P);
}
#[test]
fn sample_format_i16p() {
assert_eq!(sample_format_to_av(SampleFormat::I16p), sample_format::S16P);
}
#[test]
fn sample_format_i32p() {
assert_eq!(sample_format_to_av(SampleFormat::I32p), sample_format::S32P);
}
#[test]
fn sample_format_f32p() {
assert_eq!(sample_format_to_av(SampleFormat::F32p), sample_format::FLTP);
}
#[test]
fn sample_format_f64p() {
assert_eq!(sample_format_to_av(SampleFormat::F64p), sample_format::DBLP);
}
#[test]
fn sample_format_unknown_falls_back_to_fltp() {
assert_eq!(
sample_format_to_av(SampleFormat::Other(99)),
sample_format::FLTP
);
}
}