8 #include <libavcodec/avcodec.h>
9 #include <libavformat/avformat.h>
10 #include <libavutil/avutil.h>
11 #include <libavutil/pixdesc.h>
12 #include <libavutil/channel_layout.h>
22 : _inputStream(&inputStream)
35 if(!isValid && !profile.empty())
37 const std::string msg(
"Invalid audio profile to setup decoder.");
39 throw std::runtime_error(msg);
44 LOG_INFO(
"Setup audio decoder with:\n" << profile)
56 for(ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it)
67 catch(std::exception& e)
69 LOG_WARN(
"AudioDecoder - can't set option " << (*it).first <<
" to " << (*it).second <<
": " << e.what())
104 if(!nextPacketRead && ret == 0 && got_frame == 0)
105 decodeNextFrame =
false;
107 decodeNextFrame =
true;
110 if(!nextPacketRead && !decodeNextFrame)
129 const size_t srcNbChannels = avCodecContext.channels;
130 const size_t bytePerSample = av_get_bytes_per_sample((AVSampleFormat)frameBuffer.
getAVFrame().format);
132 const int dstNbChannels = 1;
133 const int noAlignment = 0;
134 const size_t decodedSize = av_samples_get_buffer_size(NULL, dstNbChannels, frameBuffer.
getAVFrame().nb_samples,
135 avCodecContext.sample_fmt, noAlignment);
140 if(channelIndex > srcNbChannels - 1)
142 std::stringstream msg;
143 msg <<
"The channel at index ";
145 msg <<
" doesn't exist (srcNbChannels = ";
146 msg << srcNbChannels;
148 throw std::runtime_error(msg.str());
153 av_frame_set_channels(&audioBuffer.
getAVFrame(), 1);
154 av_frame_set_channel_layout(&audioBuffer.
getAVFrame(), AV_CH_LAYOUT_MONO);
158 unsigned char* src = allDataOfNextFrame.
getData()[0];
159 unsigned char* dst = audioBuffer.
getData()[0];
162 src += channelIndex * bytePerSample;
165 for(
int sample = 0; sample < allDataOfNextFrame.
getAVFrame().nb_samples; ++sample)
167 memcpy(dst, src, bytePerSample);
168 dst += bytePerSample;
169 src += bytePerSample * srcNbChannels;
void setString(const std::string &value)
void setupDecoder(const ProfileLoader::Profile &profile=ProfileLoader::Profile())
Setup the decoder.
void clear()
Clear existing data and set size to 0.
void setInt(const int value)
void flushDecoder()
Reset the internal decoder state / flush internal buffers.
std::string getDescriptionFromErrorCode(const int code)
Get the string description corresponding to the error code provided by ffmpeg/libav.
std::map< std::string, std::string > Profile
const std::string avProfileIdentificator
const std::string avProfileIdentificatorHuman
void setNbSamplesPerChannel(const size_t nbSamples)
Option & getOption(const std::string &optionName)
InputStream * _inputStream
Stream from which we read next frames (no ownership, has link)
void copyProperties(const Frame &otherFrame)
Copy all the fields that do not affect the data layout in the buffers.
Wrapper of AVOption. Get its type to know what the option is about: Int, Double, Ratio, Choice... Parse its array of options to get the potential childs (Choice and Group).
unsigned char ** getData()
Get all the data of the frame.
AudioDecoder(InputStream &inputStream)
static bool checkAudioProfile(const Profile &profileToCheck)
This class describes decoded audio data.
This class describes decoded (raw) audio or video data.
void openCodec()
Initialize the codec context.
bool decodeNextFrame(Frame &frameBuffer)
Decode next frame.
AVCodecContext & getAVCodecContext()
This class describes coded data.
const std::string avProfileThreads
size_t getNbSamplesPerChannel() const
const std::string avProfileType