7 #include <libavcodec/avcodec.h>
8 #include <libavutil/opt.h>
10 #ifdef AVTRANSCODER_LIBAV_DEPENDENCY
11 #include <libavresample/avresample.h>
12 #define AllocResampleContext avresample_alloc_context
13 #define FreeResampleContext avresample_free
14 #define InitResampleContext avresample_open
15 #define SetSampleFormat av_opt_set_int
17 #include <libswresample/swresample.h>
18 #define AllocResampleContext swr_alloc
19 #define FreeResampleContext swr_free
20 #define InitResampleContext swr_init
21 #define SetSampleFormat av_opt_set_sample_fmt
24 #if LIBAVCODEC_VERSION_MAJOR > 54
25 #include <libavutil/frame.h>
36 : _audioConvertContext(NULL)
52 throw std::runtime_error(
"unable to create audio convert context");
68 std::stringstream msg;
69 msg <<
"Unable to open audio convert context:" << std::endl;
70 msg <<
"in_channel_layout " << av_get_default_channel_layout(src.
getNbChannels()) << std::endl;
71 msg <<
"out_channel_layout " << av_get_default_channel_layout(dst.
getNbChannels()) << std::endl;
73 msg <<
"out_sample_rate " << dst.
getSampleRate() << std::endl;
76 throw std::runtime_error(msg.str());
79 std::stringstream msg;
82 msg <<
"Source, number of channels = " << src.
getNbChannels() << std::endl;
83 msg <<
"Source, sample rate = " << src.
getSampleRate() << std::endl;
84 msg <<
"Destination, number of channels = " << dst.
getNbChannels() << std::endl;
85 msg <<
"Destination, sample rate = " << dst.
getSampleRate() << std::endl;
97 const size_t nbInputSamplesPerChannel = srcFrame.
getAVFrame().nb_samples;
99 const unsigned char** srcData = srcFrame.
getData();
100 unsigned char** dstData = dstFrame.
getData();
102 int nbOutputSamplesPerChannel;
103 #ifdef AVTRANSCODER_LIBAV_DEPENDENCY
104 nbOutputSamplesPerChannel =
105 avresample_convert(
_audioConvertContext, dstData, 0, nbInputSamplesPerChannel, srcData, 0, nbInputSamplesPerChannel);
107 nbOutputSamplesPerChannel =
108 swr_convert(
_audioConvertContext, dstData, nbInputSamplesPerChannel, srcData, nbInputSamplesPerChannel);
111 if(nbOutputSamplesPerChannel < 0)
113 throw std::runtime_error(
"unable to convert audio samples");
117 dstFrame.
getAVFrame().nb_samples = nbOutputSamplesPerChannel;
AVSampleFormat getSampleFormat() const
unsigned char ** getData()
Get all the data of the frame.
size_t getSampleRate() const
size_t getNbChannels() const
This class describes decoded audio data.
This class describes decoded (raw) audio or video data.
std::string getSampleFormatName(const AVSampleFormat sampleFormat)