Closed wangjia184 closed 6 months ago
Well, actually avcodec_fill_audio_frame
or AVAudioFifo
are both overkill here. You can do things like this:
Note: AVSamples
is almost the same as AVImage
(They are both created by av_*_fill_arrays
methods)
Copy the data
, linesize
, nb_channels
, nb_samples
and sample_fmt
into your AVFrame
instance (make AVFrame
borrows the data in AVSamples
, be aware of the lifetime though), and everything should be fine.
It would be nice if you could provide a minimal example, therefore reproducing your problem would be much simpler.
Thanks @idm0.
It works for mono channel sound.
let mut frame = AVFrame::new();
frame.set_nb_samples(bgm_samples.nb_samples);
frame.set_ch_layout(AVChannelLayout::from_nb_channels(AUDIO_CHANNEL_NUM).into_inner());
frame.set_format(bgm_samples.sample_fmt);
frame.set_sample_rate(AUDIO_SAMPLE_RATE);
frame
.alloc_buffer()
.context("Could not allocate frame buffer")?;
unsafe {
frame.deref_mut().linesize[0] = bgm_samples.linesize;
assert_ne!(
frame.deref_mut().extended_data,
std::ptr::null_mut::<*mut u8>()
);
std::ptr::copy_nonoverlapping(
bgm_samples.audio_data[0],
*frame.deref_mut().extended_data,
bgm_samples.linesize as usize,
);
}
This is not an issue of rsmpeg. I am trying to seek some assistance here.
I try to fill
AVFrame
with samples as below.The frame from above code would cause an error in encoder:
If I change the above code as below, to copy samples into an
AVAudioFifo
and then read samples fromAVAudioFifo
intoAVFrame
, it works! It means thebgm_samples
contains valid samples. But somehowavcodec_fill_audio_frame
does not copy the samples correctly.Can someone help? thanks in advance.