/
audiooutputdigitalencoder.cpp
305 lines (267 loc) · 8 KB
/
audiooutputdigitalencoder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Std C headers
#include <cstdio>
#include <unistd.h>
#include <string.h>
#include "mythcorecontext.h"
#include "config.h"
// libav headers
extern "C" {
#include "libavutil/mem.h" // for av_free
#include "libavcodec/avcodec.h"
}
// MythTV headers
#include "audiooutputdigitalencoder.h"
#include "audiooutpututil.h"
#include "compat.h"
#include "mythlogging.h"
#define LOC QString("DEnc: ")
AudioOutputDigitalEncoder::AudioOutputDigitalEncoder(void) :
av_context(NULL),
out(NULL), out_size(0),
in(NULL), inp(NULL), in_size(0),
outlen(0), inlen(0),
samples_per_frame(0),
m_spdifenc(NULL)
{
out = (outbuf_t *)av_mallocz(OUTBUFSIZE);
if (out)
{
out_size = OUTBUFSIZE;
}
in = (inbuf_t *)av_mallocz(INBUFSIZE);
if (in)
{
in_size = INBUFSIZE;
}
inp = (inbuf_t *)av_mallocz(INBUFSIZE);
}
AudioOutputDigitalEncoder::~AudioOutputDigitalEncoder()
{
Reset();
if (out)
{
av_freep(&out);
out_size = 0;
}
if (in)
{
av_freep(&in);
in_size = 0;
}
if (inp)
{
av_freep(&inp);
}
}
void AudioOutputDigitalEncoder::Reset(void)
{
if (av_context)
{
avcodec_close(av_context);
av_freep(&av_context);
}
delete m_spdifenc;
m_spdifenc = NULL;
clear();
}
void *AudioOutputDigitalEncoder::realloc(void *ptr,
size_t old_size, size_t new_size)
{
if (!ptr)
return ptr;
// av_realloc doesn't maintain 16 bytes alignment
void *new_ptr = av_malloc(new_size);
if (!new_ptr)
{
av_free(ptr);
return new_ptr;
}
memcpy(new_ptr, ptr, old_size);
av_free(ptr);
return new_ptr;
}
bool AudioOutputDigitalEncoder::Init(
CodecID codec_id, int bitrate, int samplerate, int channels)
{
AVCodec *codec;
int ret;
LOG(VB_AUDIO, LOG_INFO, LOC +
QString("Init codecid=%1, br=%2, sr=%3, ch=%4")
.arg(ff_codec_id_string(codec_id)) .arg(bitrate)
.arg(samplerate) .arg(channels));
if (!(in || inp || out))
{
LOG(VB_GENERAL, LOG_ERR, LOC + "Memory allocation failed");
return false;
}
// Clear digital encoder from all existing content
Reset();
// We need to do this when called from mythmusic
avcodeclock->lock();
avcodec_register_all();
avcodeclock->unlock();
codec = avcodec_find_encoder_by_name("ac3_fixed");
if (!codec)
{
LOG(VB_GENERAL, LOG_ERR, LOC + "Could not find codec");
return false;
}
av_context = avcodec_alloc_context3(codec);
avcodec_get_context_defaults3(av_context, codec);
av_context->bit_rate = bitrate;
av_context->sample_rate = samplerate;
av_context->channels = channels;
av_context->channel_layout = av_get_default_channel_layout(channels);
av_context->sample_fmt = AV_SAMPLE_FMT_S16P;
// open it
ret = avcodec_open2(av_context, codec, NULL);
if (ret < 0)
{
LOG(VB_GENERAL, LOG_ERR, LOC +
"Could not open codec, invalid bitrate or samplerate");
return false;
}
m_spdifenc = new SPDIFEncoder("spdif", AV_CODEC_ID_AC3);
if (!m_spdifenc->Succeeded())
{
LOG(VB_GENERAL, LOG_ERR, LOC + "Could not create spdif muxer");
return false;
}
samples_per_frame = av_context->frame_size * av_context->channels;
LOG(VB_AUDIO, LOG_INFO, QString("DigitalEncoder::Init fs=%1, spf=%2")
.arg(av_context->frame_size) .arg(samples_per_frame));
return true;
}
size_t AudioOutputDigitalEncoder::Encode(void *buf, int len, AudioFormat format)
{
int sampleSize = AudioOutputSettings::SampleSize(format);
if (sampleSize <= 0)
{
LOG(VB_AUDIO, LOG_ERR, LOC + "AC-3 encode error, sample size is zero");
return 0;
}
// Check if there is enough space in incoming buffer
int required_len = inlen +
len * AudioOutputSettings::SampleSize(FORMAT_S16) / sampleSize;
if (required_len > (int)in_size)
{
required_len = ((required_len / INBUFSIZE) + 1) * INBUFSIZE;
LOG(VB_AUDIO, LOG_INFO, LOC +
QString("low mem, reallocating in buffer from %1 to %2")
.arg(in_size) .arg(required_len));
inbuf_t *tmp = reinterpret_cast<inbuf_t*>
(realloc(in, in_size, required_len));
if (!tmp)
{
in = NULL;
in_size = 0;
LOG(VB_AUDIO, LOG_ERR, LOC +
"AC-3 encode error, insufficient memory");
return outlen;
}
in = tmp;
in_size = required_len;
}
if (format != FORMAT_S16)
{
inlen += AudioOutputUtil::fromFloat(FORMAT_S16, (char *)in + inlen,
buf, len);
}
else
{
memcpy((char *)in + inlen, buf, len);
inlen += len;
}
int frames = inlen / sizeof(inbuf_t) / samples_per_frame;
int i = 0;
int channels = av_context->channels;
AVFrame *frame = avcodec_alloc_frame();
int size_channel = av_context->frame_size *
AudioOutputSettings::SampleSize(FORMAT_S16);
frame->extended_data = frame->data;
frame->nb_samples = av_context->frame_size;
frame->pts = AV_NOPTS_VALUE;
if (frames > 0)
{
// init AVFrame for planar data (input is interleaved)
for (int j = 0, jj = 0; j < channels; j++, jj += av_context->frame_size)
{
frame->data[j] = (uint8_t*)(inp + jj);
}
}
while (i < frames)
{
AVPacket pkt;
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
int got_packet = 0;
AudioOutputUtil::DeinterleaveSamples(
FORMAT_S16, channels,
(uint8_t*)inp,
(uint8_t*)(in + i * samples_per_frame),
size_channel * channels);
int ret = avcodec_encode_audio2(av_context, &pkt, frame,
&got_packet);
if (ret < 0)
{
LOG(VB_AUDIO, LOG_ERR, LOC + "AC-3 encode error");
avcodec_free_frame(&frame);
return ret;
}
i++;
if (!got_packet)
continue;
if (!m_spdifenc)
{
m_spdifenc = new SPDIFEncoder("spdif", AV_CODEC_ID_AC3);
}
m_spdifenc->WriteFrame((uint8_t *)pkt.data, pkt.size);
av_free_packet(&pkt);
// Check if output buffer is big enough
required_len = outlen + m_spdifenc->GetProcessedSize();
if (required_len > (int)out_size)
{
required_len = ((required_len / OUTBUFSIZE) + 1) * OUTBUFSIZE;
LOG(VB_AUDIO, LOG_WARNING, LOC +
QString("low mem, reallocating out buffer from %1 to %2")
.arg(out_size) .arg(required_len));
outbuf_t *tmp = reinterpret_cast<outbuf_t*>
(realloc(out, out_size, required_len));
if (!tmp)
{
avcodec_free_frame(&frame);
out = NULL;
out_size = 0;
LOG(VB_AUDIO, LOG_ERR, LOC +
"AC-3 encode error, insufficient memory");
return outlen;
}
out = tmp;
out_size = required_len;
}
int data_size = 0;
m_spdifenc->GetData((uint8_t *)out + outlen, data_size);
outlen += data_size;
inlen -= samples_per_frame * sizeof(inbuf_t);
}
avcodec_free_frame(&frame);
memmove(in, in + i * samples_per_frame, inlen);
return outlen;
}
size_t AudioOutputDigitalEncoder::GetFrames(void *ptr, int maxlen)
{
int len = std::min(maxlen, outlen);
if (len != maxlen)
{
LOG(VB_AUDIO, LOG_INFO, LOC + "GetFrames: getting less than requested");
}
memcpy(ptr, out, len);
outlen -= len;
memmove(out, (char *)out + len, outlen);
return len;
}
void AudioOutputDigitalEncoder::clear()
{
inlen = outlen = 0;
}