[Libav-user] Extracting samples from audio file

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

[Libav-user] Extracting samples from audio file

Philippe Gorley
Hi,

I need to read an audio file into an array of samples and then
deinterleave/resample it. Right now, I'm decoding the file using the
avcodec_send_packet/avcodec_receive_frame API. But how do I get the
samples from an AVFrame into an int16_t?

Cheers,

--
Philippe Gorley
Free Software Consultant | Montréal, Qc
Savoir-faire Linux

Confidentiality Message: This communication (including any files
transmitted with it) is intended solely for the person or entity to
whom it is addressed, and may contain confidential or privileged
information.
The disclosure, distribution or copying of this message is strictly
forbidden. Should you have received this communication in error,
kindly contact the sender promptly, destroy any copies and delete
this message from your computer system.
_______________________________________________
Libav-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/libav-user
Reply | Threaded
Open this post in threaded view
|

Re: Extracting samples from audio file

salsaman
Hi Philippe,
I would suggest you look into libswresample, particularly swr_convert().

regards,
Gabriel.


On Wed, Aug 9, 2017 at 4:33 PM, Philippe Gorley <[hidden email]> wrote:
Hi,

I need to read an audio file into an array of samples and then deinterleave/resample it. Right now, I'm decoding the file using the avcodec_send_packet/avcodec_receive_frame API. But how do I get the samples from an AVFrame into an int16_t?

Cheers,

--
Philippe Gorley
Free Software Consultant | Montréal, Qc
Savoir-faire Linux

Confidentiality Message: This communication (including any files
transmitted with it) is intended solely for the person or entity to
whom it is addressed, and may contain confidential or privileged
information.
The disclosure, distribution or copying of this message is strictly
forbidden. Should you have received this communication in error,
kindly contact the sender promptly, destroy any copies and delete
this message from your computer system.
_______________________________________________
Libav-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/libav-user


_______________________________________________
Libav-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/libav-user
Reply | Threaded
Open this post in threaded view
|

Re: Extracting samples from audio file

Philippe Gorley
On 2017-08-09 07:57 PM, salsaman wrote:
> Hi Philippe,
> I would suggest you look into libswresample, particularly swr_convert().

Didn't know about swr_convert, thanks.

The in parameter comes from a decoded frame (AVFrame->data)?

> regards,
> Gabriel.
>
>
> http://lives-video.com
> https://www.openhub.net/accounts/salsaman
>
> _______________________________________________
> Libav-user mailing list
> [hidden email]
> http://ffmpeg.org/mailman/listinfo/libav-user
>

Cheers,

--
Philippe Gorley
Free Software Consultant | Montréal, Qc
Savoir-faire Linux

Confidentiality Message: This communication (including any files
transmitted with it) is intended solely for the person or entity to
whom it is addressed, and may contain confidential or privileged
information.
The disclosure, distribution or copying of this message is strictly
forbidden. Should you have received this communication in error,
kindly contact the sender promptly, destroy any copies and delete
this message from your computer system.
_______________________________________________
Libav-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/libav-user
Reply | Threaded
Open this post in threaded view
|

Re: Extracting samples from audio file

salsaman


On Thu, Aug 10, 2017 at 1:30 PM, Philippe Gorley <[hidden email]> wrote:
On 2017-08-09 07:57 PM, salsaman wrote:
Hi Philippe,
I would suggest you look into libswresample, particularly swr_convert().

Didn't know about swr_convert, thanks.

The in parameter comes from a decoded frame (AVFrame->data)?


Correct, you would first create the swr_context then use it to convert the data.




 
regards,
Gabriel.


http://lives-video.com
https://www.openhub.net/accounts/salsaman

_______________________________________________
Libav-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/libav-user


Cheers,

--
Philippe Gorley
Free Software Consultant | Montréal, Qc
Savoir-faire Linux

Confidentiality Message: This communication (including any files
transmitted with it) is intended solely for the person or entity to
whom it is addressed, and may contain confidential or privileged
information.
The disclosure, distribution or copying of this message is strictly
forbidden. Should you have received this communication in error,
kindly contact the sender promptly, destroy any copies and delete
this message from your computer system.
_______________________________________________
Libav-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/libav-user


_______________________________________________
Libav-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/libav-user
Reply | Threaded
Open this post in threaded view
|

Re: Extracting samples from audio file

Philippe Gorley
On 2017-08-10 01:17 PM, salsaman wrote:
> Correct, you would first create the swr_context then use it to convert
> the data.
>
> _______________________________________________
> Libav-user mailing list
> [hidden email]
> http://ffmpeg.org/mailman/listinfo/libav-user
>

I'm still struggling with getting this to work. My code is here:
https://pastebin.com/arzUw2za

The commented out code at the bottom is the old Sndfile code, which I'd
like to replace using FFmpeg. What it does is read all the audio samples
into an array of int16_t. NB: Sndfile calls them frames, while FFmpeg
calls them samples.

Right now, my code gives me low volume static.

I'm setting up the decoding pipeline, it (seems to, at least) works. I'm
getting the same number of samples as Sndfile does. I'm guessing the
problem lies with the resampling code.

I'm calling swr_config_frame and swr_convert_frame for each decoded
frame, loop through the output frame's extended_data[0] and append those
samples to an std::vector.

Notes:
AudioSample is an alias for int16_t.
AudioBuffer is a container with an std::vector<std::vector<AudioSample>>
(one for each channel).
AudioFormat is a POD struct with the sample rate and number of channels.

Can anyone look at this and tell me what I'm doing wrong?

Thanks,

--
Philippe Gorley
Free Software Consultant | Montréal, Qc
Savoir-faire Linux

Confidentiality Message: This communication (including any files
transmitted with it) is intended solely for the person or entity to
whom it is addressed, and may contain confidential or privileged
information.
The disclosure, distribution or copying of this message is strictly
forbidden. Should you have received this communication in error,
kindly contact the sender promptly, destroy any copies and delete
this message from your computer system.
_______________________________________________
Libav-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/libav-user
Reply | Threaded
Open this post in threaded view
|

Re: Extracting samples from audio file

Paul B Mahol
On 8/15/17, Philippe Gorley <[hidden email]> wrote:

> On 2017-08-10 01:17 PM, salsaman wrote:
>> Correct, you would first create the swr_context then use it to convert
>> the data.
>>
>> _______________________________________________
>> Libav-user mailing list
>> [hidden email]
>> http://ffmpeg.org/mailman/listinfo/libav-user
>>
>
> I'm still struggling with getting this to work. My code is here:
> https://pastebin.com/arzUw2za
>
> The commented out code at the bottom is the old Sndfile code, which I'd
> like to replace using FFmpeg. What it does is read all the audio samples
> into an array of int16_t. NB: Sndfile calls them frames, while FFmpeg
> calls them samples.
>
> Right now, my code gives me low volume static.
>
> I'm setting up the decoding pipeline, it (seems to, at least) works. I'm
> getting the same number of samples as Sndfile does. I'm guessing the
> problem lies with the resampling code.
>
> I'm calling swr_config_frame and swr_convert_frame for each decoded
> frame, loop through the output frame's extended_data[0] and append those
> samples to an std::vector.
>
> Notes:
> AudioSample is an alias for int16_t.
> AudioBuffer is a container with an std::vector<std::vector<AudioSample>>
> (one for each channel).
> AudioFormat is a POD struct with the sample rate and number of channels.
>
> Can anyone look at this and tell me what I'm doing wrong?

needResamping is triggered only when same sample rate is both ways.
Are you sure that swr resample code works that way, you can not
guarantee it will
give output frame for each input frame. There are nice swr examples in
repo, Have you looked at them?

>
> Thanks,
_______________________________________________
Libav-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/libav-user
Reply | Threaded
Open this post in threaded view
|

Re: Extracting samples from audio file

Philippe Gorley
On 2017-08-15 04:40 PM, Paul B Mahol wrote:

> On 8/15/17, Philippe Gorley <[hidden email]> wrote:
>> On 2017-08-10 01:17 PM, salsaman wrote:
>>> Correct, you would first create the swr_context then use it to convert
>>> the data.
>>>
>>> _______________________________________________
>>> Libav-user mailing list
>>> [hidden email]
>>> http://ffmpeg.org/mailman/listinfo/libav-user
>>>
>>
>> I'm still struggling with getting this to work. My code is here:
>> https://pastebin.com/arzUw2za
>>
>> The commented out code at the bottom is the old Sndfile code, which I'd
>> like to replace using FFmpeg. What it does is read all the audio samples
>> into an array of int16_t. NB: Sndfile calls them frames, while FFmpeg
>> calls them samples.
>>
>> Right now, my code gives me low volume static.
>>
>> I'm setting up the decoding pipeline, it (seems to, at least) works. I'm
>> getting the same number of samples as Sndfile does. I'm guessing the
>> problem lies with the resampling code.
>>
>> I'm calling swr_config_frame and swr_convert_frame for each decoded
>> frame, loop through the output frame's extended_data[0] and append those
>> samples to an std::vector.
>>
>> Notes:
>> AudioSample is an alias for int16_t.
>> AudioBuffer is a container with an std::vector<std::vector<AudioSample>>
>> (one for each channel).
>> AudioFormat is a POD struct with the sample rate and number of channels.
>>
>> Can anyone look at this and tell me what I'm doing wrong?
>
> needResamping is triggered only when same sample rate is both ways.
> Are you sure that swr resample code works that way, you can not
> guarantee it will
> give output frame for each input frame. There are nice swr examples in
> repo, Have you looked at them?

Even when commenting out the else block and forcing the resampling, I
get the problem. But yes, that might be a future problem. Thanks for
flagging it.

I have looked at docs/examples/resampling_audio.c (but does not use
AVFrame), the code in libswresample, and have read the docs (at least,
whatever I could find).

Maybe I should use swr_convert instead of swr_convert_frame and directly
append the out samples to my vector?

>
>>
>> Thanks,
> _______________________________________________
> Libav-user mailing list
> [hidden email]
> http://ffmpeg.org/mailman/listinfo/libav-user
>

Thanks,

--
Philippe Gorley
Free Software Consultant | Montréal, Qc
Savoir-faire Linux

Confidentiality Message: This communication (including any files
transmitted with it) is intended solely for the person or entity to
whom it is addressed, and may contain confidential or privileged
information.
The disclosure, distribution or copying of this message is strictly
forbidden. Should you have received this communication in error,
kindly contact the sender promptly, destroy any copies and delete
this message from your computer system.
_______________________________________________
Libav-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/libav-user
Reply | Threaded
Open this post in threaded view
|

Re: Extracting samples from audio file

Tuukka Pasanen
Hello,

I have written example how to extract PCM from data with FFMpeg. It's
not a bible how to make it but and example..

https://github.com/illuusio/ffmpeg-example

Tuukka


Philippe Gorley kirjoitti 16.08.2017 klo 00:06:

> On 2017-08-15 04:40 PM, Paul B Mahol wrote:
>> On 8/15/17, Philippe Gorley <[hidden email]>
>> wrote:
>>> On 2017-08-10 01:17 PM, salsaman wrote:
>>>> Correct, you would first create the swr_context then use it to convert
>>>> the data.
>>>>
>>>> _______________________________________________
>>>> Libav-user mailing list
>>>> [hidden email]
>>>> http://ffmpeg.org/mailman/listinfo/libav-user
>>>>
>>>
>>> I'm still struggling with getting this to work. My code is here:
>>> https://pastebin.com/arzUw2za
>>>
>>> The commented out code at the bottom is the old Sndfile code, which I'd
>>> like to replace using FFmpeg. What it does is read all the audio
>>> samples
>>> into an array of int16_t. NB: Sndfile calls them frames, while FFmpeg
>>> calls them samples.
>>>
>>> Right now, my code gives me low volume static.
>>>
>>> I'm setting up the decoding pipeline, it (seems to, at least) works.
>>> I'm
>>> getting the same number of samples as Sndfile does. I'm guessing the
>>> problem lies with the resampling code.
>>>
>>> I'm calling swr_config_frame and swr_convert_frame for each decoded
>>> frame, loop through the output frame's extended_data[0] and append
>>> those
>>> samples to an std::vector.
>>>
>>> Notes:
>>> AudioSample is an alias for int16_t.
>>> AudioBuffer is a container with an
>>> std::vector<std::vector<AudioSample>>
>>> (one for each channel).
>>> AudioFormat is a POD struct with the sample rate and number of
>>> channels.
>>>
>>> Can anyone look at this and tell me what I'm doing wrong?
>>
>> needResamping is triggered only when same sample rate is both ways.
>> Are you sure that swr resample code works that way, you can not
>> guarantee it will
>> give output frame for each input frame. There are nice swr examples in
>> repo, Have you looked at them?
>
> Even when commenting out the else block and forcing the resampling, I
> get the problem. But yes, that might be a future problem. Thanks for
> flagging it.
>
> I have looked at docs/examples/resampling_audio.c (but does not use
> AVFrame), the code in libswresample, and have read the docs (at least,
> whatever I could find).
>
> Maybe I should use swr_convert instead of swr_convert_frame and
> directly append the out samples to my vector?
>
>>
>>>
>>> Thanks,
>> _______________________________________________
>> Libav-user mailing list
>> [hidden email]
>> http://ffmpeg.org/mailman/listinfo/libav-user
>>
>
> Thanks,
>

_______________________________________________
Libav-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/libav-user