6#include "libavformat/avformat.h"
7#include "libavutil/opt.h"
14#define LOC QString("SPDIFEncoder: ")
27 QByteArray dev_ba = muxer.toLatin1();
29 const AVOutputFormat *fmt = av_guess_format(dev_ba.constData(),
nullptr,
nullptr);
32 LOG(VB_AUDIO, LOG_ERR,
LOC +
"av_guess_format");
36 m_oc = avformat_alloc_context();
39 LOG(VB_AUDIO, LOG_ERR,
LOC +
"avformat_alloc_context");
46 this,
nullptr,
funcIO,
nullptr);
49 LOG(VB_AUDIO, LOG_ERR,
LOC +
"avio_alloc_context");
54 m_oc->pb->seekable = 0;
55 m_oc->flags |= AVFMT_NOFILE | AVFMT_FLAG_IGNIDX;
57 const AVCodec *codec = avcodec_find_decoder(codec_id);
60 LOG(VB_AUDIO, LOG_ERR,
LOC +
"avcodec_find_decoder");
65 AVStream *stream = avformat_new_stream(
m_oc,
nullptr);
68 LOG(VB_AUDIO, LOG_ERR,
LOC +
"avformat_new_stream");
74 stream->codecpar->codec_id = codec->id;
75 stream->codecpar->codec_type = codec->type;
76 stream->codecpar->sample_rate = 48000;
78 if (avformat_write_header(
m_oc,
nullptr) < 0)
80 LOG(VB_AUDIO, LOG_ERR,
LOC +
"avformat_write_header");
85 LOG(VB_AUDIO, LOG_INFO,
LOC + QString(
"Creating %1 encoder (for %2)")
86 .arg(muxer, avcodec_get_name(codec_id)));
103 AVPacket *packet = av_packet_alloc();
104 if (packet ==
nullptr)
106 LOG(VB_GENERAL, LOG_ERR,
"packet allocation failed");
110 static int s_pts = 1;
111 packet->pts = s_pts++;
115 if (av_write_frame(
m_oc, packet) < 0)
117 LOG(VB_AUDIO, LOG_ERR,
LOC +
"av_write_frame");
120 av_packet_free(&packet);
131 if ((
m_oc ==
nullptr) || (
m_oc->pb ==
nullptr))
133 LOG(VB_AUDIO, LOG_ERR,
LOC +
"GetData");
149 if ((
m_oc ==
nullptr) || (
m_oc->pb ==
nullptr))
156 if ((
m_oc ==
nullptr) || (
m_oc->pb ==
nullptr))
158 return m_oc->pb->buffer;
181 av_opt_set_int(
m_oc->priv_data,
"dtshd_rate", rate, 0);
192 if ((enc->m_oc ==
nullptr) || (enc->m_oc->pb ==
nullptr))
194 LOG(VB_AUDIO, LOG_ERR,
LOC +
"funcIO");
198 memcpy(enc->m_oc->pb->buffer + enc->m_size, buf, size);
212 av_write_trailer(
m_oc);
219 av_free(
m_oc->pb->buffer);
220 av_freep(
reinterpret_cast<void*
>(&
m_oc->pb));
222 avformat_free_context(
m_oc);
static const int kMaxSizeBuffer
kMaxSizeBuffer is the maximum size of a buffer to be used with DecodeAudio
SPDIFEncoder(const QString &muxer, AVCodecID codec_id)
SPDIFEncoder constructor Args: QString muxer : name of the muxer.
static int funcIO(void *opaque, const uint8_t *buf, int size)
funcIO: Internal callback function that will receive encoded frames
void Destroy()
Destroy and free all allocated memory.
unsigned char * GetProcessedBuffer()
void Reset()
Reset the internal encoder buffer.
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...
bool SetMaxHDRate(int rate)
Set the maximum HD rate.
int GetData(unsigned char *buffer, size_t &dest_size)
Retrieve encoded data and copy it in the provided buffer.
#define LOG(_MASK_, _LEVEL_, _QSTRING_)