8 #include "libavformat/avformat.h"
9 #include "libavutil/opt.h"
12 #define LOC QString("SPDIFEncoder: ")
25 QByteArray dev_ba = muxer.toLatin1();
27 const AVOutputFormat *fmt = av_guess_format(dev_ba.constData(),
nullptr,
nullptr);
30 LOG(VB_AUDIO, LOG_ERR,
LOC +
"av_guess_format");
34 m_oc = avformat_alloc_context();
37 LOG(VB_AUDIO, LOG_ERR,
LOC +
"avformat_alloc_context");
44 this,
nullptr,
funcIO,
nullptr);
47 LOG(VB_AUDIO, LOG_ERR,
LOC +
"avio_alloc_context");
52 m_oc->pb->seekable = 0;
53 m_oc->flags |= AVFMT_NOFILE | AVFMT_FLAG_IGNIDX;
55 const AVCodec *codec = avcodec_find_decoder(codec_id);
58 LOG(VB_AUDIO, LOG_ERR,
LOC +
"avcodec_find_decoder");
63 AVStream *stream = avformat_new_stream(
m_oc,
nullptr);
66 LOG(VB_AUDIO, LOG_ERR,
LOC +
"avformat_new_stream");
72 stream->codecpar->codec_id = codec->id;
73 stream->codecpar->codec_type = codec->type;
74 stream->codecpar->sample_rate = 48000;
76 if (avformat_write_header(
m_oc,
nullptr) < 0)
78 LOG(VB_AUDIO, LOG_ERR,
LOC +
"avformat_write_header");
83 LOG(VB_AUDIO, LOG_INFO,
LOC + QString(
"Creating %1 encoder (for %2)")
84 .arg(muxer, avcodec_get_name(codec_id)));
101 AVPacket *packet = av_packet_alloc();
102 if (packet ==
nullptr)
104 LOG(VB_GENERAL, LOG_ERR,
"packet allocation failed");
108 static int s_pts = 1;
109 packet->pts = s_pts++;
113 if (av_write_frame(
m_oc, packet) < 0)
115 LOG(VB_AUDIO, LOG_ERR,
LOC +
"av_write_frame");
118 av_packet_free(&packet);
129 if ((
m_oc ==
nullptr) || (
m_oc->pb ==
nullptr))
131 LOG(VB_AUDIO, LOG_ERR,
LOC +
"GetData");
147 if ((
m_oc ==
nullptr) || (
m_oc->pb ==
nullptr))
154 if ((
m_oc ==
nullptr) || (
m_oc->pb ==
nullptr))
156 return m_oc->pb->buffer;
179 av_opt_set_int(
m_oc->priv_data,
"dtshd_rate", rate, 0);
190 if ((enc->m_oc ==
nullptr) || (enc->m_oc->pb ==
nullptr))
192 LOG(VB_AUDIO, LOG_ERR,
LOC +
"funcIO");
196 memcpy(enc->m_oc->pb->buffer + enc->m_size, buf, size);
210 av_write_trailer(
m_oc);
217 av_free(
m_oc->pb->buffer);
218 av_freep(
reinterpret_cast<void*
>(&
m_oc->pb));
220 avformat_free_context(
m_oc);