Go to the documentation of this file.
8 #include "libavutil/mem.h"
9 #include "libavcodec/avcodec.h"
20 #define LOC QString("DEnc: ")
23 : m_outbuf(static_cast<uint8_t*>(av_mallocz(
OUTBUFSIZE)))
42 av_freep(
reinterpret_cast<void*
>(&
m_outbuf));
47 av_freep(
reinterpret_cast<void*
>(&
m_inbuf));
52 av_freep(
reinterpret_cast<void*
>(&
m_framebuf));
70 size_t old_size,
size_t new_size)
76 void *new_ptr = av_malloc(new_size);
82 memcpy(new_ptr, ptr, old_size);
90 #define AC3_FIXED 0 // NOLINT(cppcoreguidelines-macro-usage)
92 static constexpr
const char*
CODECNAME {
"ac3_fixed" };
99 #define MYTH_USE_FLOAT 1 // NOLINT(cppcoreguidelines-macro-usage)
103 AVCodecID codec_id,
int bitrate,
int samplerate,
int channels)
105 LOG(VB_AUDIO, LOG_INFO,
LOC +
106 QString(
"Init codecid=%1, br=%2, sr=%3, ch=%4")
107 .arg(avcodec_get_name(codec_id)) .arg(bitrate)
108 .arg(samplerate) .arg(channels));
112 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Memory allocation failed");
119 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"Using codec %1 to encode audio").arg(
CODECNAME));
120 const AVCodec *codec = avcodec_find_encoder_by_name(
CODECNAME);
123 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Could not find codec");
131 av_channel_layout_default(&(
m_avContext->ch_layout), channels);
135 int ret = avcodec_open2(
m_avContext, codec,
nullptr);
138 LOG(VB_GENERAL, LOG_ERR,
LOC +
139 "Could not open codec, invalid bitrate or samplerate");
147 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Could not create spdif muxer");
153 LOG(VB_AUDIO, LOG_INFO, QString(
"DigitalEncoder::Init fs=%1, spf=%2")
171 LOG(VB_AUDIO, LOG_ERR,
LOC +
"AC-3 encode error, sample size is zero");
176 ssize_t required_len =
m_inlen +
182 LOG(VB_AUDIO, LOG_INFO,
LOC +
183 QString(
"low mem, reallocating in buffer from %1 to %2")
190 LOG(VB_AUDIO, LOG_ERR,
LOC +
191 "AC-3 encode error, insufficient memory");
214 LOG(VB_AUDIO, LOG_ERR,
LOC +
215 QString(
"AC-3 encode error, cannot handle input format %1")
232 LOG(VB_AUDIO, LOG_ERR,
LOC +
233 "AC-3 encode error, insufficient memory");
250 for (
int j = 0, jj = 0; j < channels; j++, jj += size_channel)
258 AVPacket *pkt = av_packet_alloc();
261 LOG(VB_RECORD, LOG_ERR,
"packet allocation failed");
262 return AVERROR(ENOMEM);
264 bool got_packet =
false;
269 m_inbuf + (
static_cast<ptrdiff_t
>(i) * size_channel * channels),
270 size_channel * channels);
276 int ret = avcodec_receive_packet(
m_avContext, pkt);
279 if (ret == AVERROR(EAGAIN))
290 LOG(VB_GENERAL, LOG_ERR,
LOC +
291 QString(
"audio encode error: %1 (%2)")
294 av_packet_free(&pkt);
309 av_packet_unref(pkt);
310 av_packet_free(&pkt);
317 LOG(VB_AUDIO, LOG_WARNING,
LOC +
318 QString(
"low mem, reallocating out buffer from %1 to %2")
325 LOG(VB_AUDIO, LOG_ERR,
LOC +
326 "AC-3 encode error, insufficient memory");
332 size_t data_size = 0;
344 int len = std::min(maxlen,
m_outlen);
347 LOG(VB_AUDIO, LOG_INFO,
LOC +
"GetFrames: getting less than requested");
static void error(const char *str,...)
~AudioOutputDigitalEncoder()
bool Init(AVCodecID codec_id, int bitrate, int samplerate, int channels)
int GetFrames(void *ptr, int maxlen)
static void * realloc(void *ptr, size_t old_size, size_t new_size)
static constexpr ssize_t OUTBUFSIZE
static int SampleSize(AudioFormat format)
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
static constexpr AVSampleFormat FFMPEG_SAMPLE_FORMAT
SPDIFEncoder * m_spdifEnc
static constexpr const char * CODECNAME
int Encode(void *input, int len, AudioFormat format)
static constexpr ssize_t INBUFSIZE
AVCodecContext * m_avContext
void WriteFrame(unsigned char *data, int size)
Encode data through created muxer unsigned char data: pointer to data to encode int size: size of dat...
int GetData(unsigned char *buffer, size_t &dest_size)
Retrieve encoded data and copy it in the provided buffer.
AudioOutputDigitalEncoder(void)
static constexpr AudioFormat MYTH_SAMPLE_FORMAT
static void DeinterleaveSamples(AudioFormat format, int channels, uint8_t *output, const uint8_t *input, int data_size)
Deinterleave input samples Deinterleave audio samples and compact them.
static int fromFloat(AudioFormat format, void *out, const void *in, int bytes)
Convert float samples to integers.
char * av_make_error_stdstring(std::string &errbuf, int errnum)