forked from zillevdr/vdr-plugin-softhddevice-drm
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcodec_audio.cpp
More file actions
460 lines (386 loc) · 13 KB
/
codec_audio.cpp
File metadata and controls
460 lines (386 loc) · 13 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// SPDX-License-Identifier: AGPL-3.0-or-later
/**
* @file codec_audio.cpp
* Audio Decoder
*
* This file defines cAudioDecoder, which has all the functions
* to decode audio data. It's the audio interface to ffmpeg.
*
* @copyright 2009 - 2015 by Johns. All Rights Reserved.
* @copyright 2018 by zille. All Rights Reserved.
* @copyright 2025 - 2026 by Andreas Baierl. All Rights Reserved.
*
* @license{AGPL-3.0-or-later}
*/
#include <cstdint>
#include <mutex>
#include <unistd.h>
extern "C" {
#include <libavcodec/avcodec.h>
}
#include "audio.h"
#include "codec_audio.h"
#include "logger.h"
#include "misc.h"
/**
* Create a new audio decoder for the given audio context
*
* @param audio audio context
*/
cAudioDecoder::cAudioDecoder(cSoftHdAudio *audio)
: m_pAudio(audio),
m_passthroughMask(m_pAudio->GetPassthrough() & (CODEC_AC3 | CODEC_EAC3 | CODEC_DTS))
{
if (!(m_pFrame = av_frame_alloc()))
LOGFATAL("audiocodec: %s: can't allocate audio decoder frame buffer", __FUNCTION__);
LOGDEBUG2(L_CODEC, "audiocodec: %s: Set passthrough mask %d", __FUNCTION__, m_passthroughMask);
}
cAudioDecoder::~cAudioDecoder(void)
{
Close();
av_frame_free(&m_pFrame);
}
/**
* Open and initiate the audio decoder
*
* @param codecId audio codec id
* @param par audio codec parameters
* @param timebase timebase
*
* @todo FIXME: errors shouldn't be fatal, maybe just disable audio
*/
void cAudioDecoder::Open(AVCodecID codecId, AVCodecParameters *par, AVRational timebase)
{
std::lock_guard<std::mutex> lock(m_mutex);
const AVCodec *codec;
m_codecId = codecId;
switch (codecId) {
case AV_CODEC_ID_AC3:
if (!(codec = avcodec_find_decoder_by_name("ac3_fixed")))
LOGFATAL("audiocodec: %s: codec ac3_fixed ID %#06x not found", __FUNCTION__, codecId);
break;
case AV_CODEC_ID_AAC:
if (!(codec = avcodec_find_decoder_by_name("aac_fixed")))
LOGFATAL("audiocodec: %s: codec aac_fixed ID %#06x not found", __FUNCTION__, codecId);
break;
default:
if (!(codec = avcodec_find_decoder(codecId))) {
LOGFATAL("audiocodec: %s: codec %s ID %#06x not found", __FUNCTION__,
avcodec_get_name(codecId), codecId);
}
break;
}
if (!(m_pAudioCtx = avcodec_alloc_context3(codec)))
LOGFATAL("audiocodec: %s: can't allocate audio codec context", __FUNCTION__);
m_pAudioCtx->pkt_timebase = timebase;
if (par && ((avcodec_parameters_to_context(m_pAudioCtx, par)) < 0))
LOGERROR("audiocodec: %s: insert parameters to context failed!", __FUNCTION__);
if (avcodec_open2(m_pAudioCtx, m_pAudioCtx->codec, NULL) < 0)
LOGFATAL("audiocodec: %s: can't open audio codec", __FUNCTION__);
LOGDEBUG2(L_CODEC, "audiocodec: %s: Codec %s found, passthrough mask %d", __FUNCTION__, m_pAudioCtx->codec->long_name, m_passthroughMask);
m_currentSampleRate = 0;
m_currentHwSampleRate = 0;
m_currentNumChannels = 0;
m_currentHwNumChannels = 0;
m_currentPassthroughMask = 0;
}
/**
* Close the audio decoder
*/
void cAudioDecoder::Close(void)
{
std::lock_guard<std::mutex> lock(m_mutex);
if (!m_pAudioCtx)
return;
LOGDEBUG2(L_CODEC, "audiocodec: %s", __FUNCTION__);
avcodec_free_context(&m_pAudioCtx);
m_codecId = AV_CODEC_ID_NONE;
m_lastPts = AV_NOPTS_VALUE;
}
/**
* Passthrough audio data
*
* Build spdif headers depending on the codec and send the
* data to the audio device.
* Currently supported: AC3, EAC3, DTS
*
* @param avpkt undecoded audio packet
* @param frame decoded audio frame
*
* @retval 0 codec is not supported for passthrough, use Filter to handle the data
* @retval -1 sth went wrong, data will be discarded
* @retval 1 data accepted
* if finished, spdif header was created and data was sent to passthrough device
*/
int cAudioDecoder::DecodePassthrough(const AVPacket * avpkt, AVFrame *frame)
{
m_pAudio->SetTimebase(&m_pAudioCtx->pkt_timebase);
// AC3 passthrough
if (m_currentPassthroughMask & CODEC_AC3 && m_pAudioCtx->codec_id == AV_CODEC_ID_AC3) {
uint16_t *spdif = m_spdifOutput;
int spdifSize = AC3_FRAME_SIZE * 4; // frames * channels * (samplesize / 8)
if (spdifSize < avpkt->size + 8) {
LOGERROR("audiocodec: %s: too much data for spdif buffer!", __FUNCTION__);
return -1;
}
if (avpkt->size < 6)
return -1;
// build SPDIF header and append AC3 audio data to it
int bitstreamMode = avpkt->data[5] & 0x07;
spdif[0] = htole16(IEC61937_PREAMBLE1);
spdif[1] = htole16(IEC61937_PREAMBLE2);
spdif[2] = htole16(IEC61937_AC3 | bitstreamMode << 8);
spdif[3] = htole16(avpkt->size * 8);
swab(avpkt->data, spdif + 4, avpkt->size);
memset((uint8_t *)(spdif + 4) + avpkt->size, 0, spdifSize - 8 - avpkt->size);
m_pAudio->EnqueueSpdif(spdif, spdifSize, frame);
return 1;
}
// EAC3 passthrough
if (m_currentPassthroughMask & CODEC_EAC3 && m_pAudioCtx->codec_id == AV_CODEC_ID_EAC3) {
uint16_t *spdif = m_spdifOutput;
int spdifSize = EAC3_FRAME_SIZE * 4; // frames * channels * (samplesize / 8)
int repeat = 1;
// spdifSize is smaller, if we don't have 192000
if (m_currentHwSampleRate == 48000) {
spdifSize /= 4;
}
if (spdifSize < m_spdifIndex + avpkt->size + 8) {
LOGERROR("audiocodec: %s: too much data for spdif buffer!", __FUNCTION__);
ResetSpdif();
return -1;
}
if (avpkt->size < 5) {
LOGERROR("audiocodec: %s: avpkt size too small!", __FUNCTION__);
ResetSpdif();
return -1;
}
// check if we need to pack multiple packets
int fscod = (avpkt->data[4] >> 6) & 0x3;
int numblkscod = 6;
if (fscod != 0x3) {
numblkscod = (avpkt->data[4] >> 4) & 0x3;
static const uint8_t eac3_repeat[4] = { 6, 3, 2, 1 };
repeat = eac3_repeat[numblkscod];
}
if (repeat * avpkt->size > spdifSize - 8) {
LOGERROR("audiocodec: %s: spdif size too small!", __FUNCTION__);
ResetSpdif();
return -1;
}
// pack upto repeat EAC-3 pakets into one IEC 61937 burst
swab(avpkt->data, (uint8_t *)(spdif + 4) + m_spdifIndex, avpkt->size);
m_spdifIndex += avpkt->size;
// LOGDEBUG2(L_CODEC, "audiocodec: %s: E-AC3: set repeat to %d (fscod = %d, numblkscode = %d) avpkt->size %d (spdifSize %d) (repeatCount gets %d) (m_spdifIndex = %d)",
// __FUNCTION__, repeat, fscod, numblkscod, avpkt->size, spdifSize, m_spdifRepeatCount + 1, m_spdifIndex);
if (++m_spdifRepeatCount < repeat)
return 1;
if (m_spdifRepeatCount > 6) {
ResetSpdif();
return -1;
}
// build SPDIF header and append E-AC3 audio data to it
spdif[0] = htole16(IEC61937_PREAMBLE1);
spdif[1] = htole16(IEC61937_PREAMBLE2);
spdif[2] = htole16(IEC61937_EAC3);
spdif[3] = htole16(m_spdifIndex * 8);
int pad = spdifSize - 8 - m_spdifIndex;
if (pad > 0)
memset((uint8_t *)(spdif + 4) + m_spdifIndex, 0, pad);
m_pAudio->EnqueueSpdif(spdif, spdifSize, frame);
ResetSpdif();
return 1;
}
// DTS passthrough
if (m_currentPassthroughMask & CODEC_DTS && m_pAudioCtx->codec_id == AV_CODEC_ID_DTS) {
uint16_t *spdif = m_spdifOutput;
uint8_t nbs;
int bsid;
int burstSz;
if (avpkt->size < 6) {
LOGERROR("audiocodec: %s: avpkt size too small!", __FUNCTION__);
return -1;
}
nbs = (uint8_t)((avpkt->data[4] & 0x01) << 6) |
((avpkt->data[5] >> 2) & 0x3f);
switch(nbs) {
case 0x07:
bsid = 0x0a; // MPEG-2 layer 3 is used when?
burstSz = 1024;
break;
case 0x0f:
bsid = IEC61937_DTS1;
burstSz = DTS1_FRAME_SIZE * 4; // frames * channels * (samplesize / 8)
break;
case 0x1f:
bsid = IEC61937_DTS2;
burstSz = DTS2_FRAME_SIZE * 4; // frames * channels * (samplesize / 8)
break;
case 0x3f:
bsid = IEC61937_DTS3;
burstSz = DTS3_FRAME_SIZE * 4; // frames * channels * (samplesize / 8)
break;
default:
bsid = IEC61937_NULL;
if (nbs < 5)
nbs = 127;
burstSz = (nbs + 1) * 32 * 2 + 2;
break;
}
// build SPDIF header and append DTS audio data to it
if (burstSz < avpkt->size + 8) {
LOGERROR("audiocodec: %s: too much data for spdif buffer!", __FUNCTION__);
return -1;
}
spdif[0] = htole16(IEC61937_PREAMBLE1);
spdif[1] = htole16(IEC61937_PREAMBLE2);
spdif[2] = htole16(bsid);
spdif[3] = htole16(avpkt->size * 8);
spdif[4] = htole16(DTS_PREAMBLE_16BE_1);
spdif[5] = htole16(DTS_PREAMBLE_16BE_2);
swab(avpkt->data, spdif + 6, avpkt->size);
memset((uint8_t *)(spdif + 6) + avpkt->size, 0, burstSz - 12 - avpkt->size);
m_pAudio->EnqueueSpdif(spdif, burstSz, frame);
return 1;
}
return 0;
}
/**
* Handle audio format changes
*
* Setup audio, if format changed
*
* @retval 0 if new audio was correctly set up,
* otherwise return value of cSoftHdAudio::Setup()
*/
int cAudioDecoder::UpdateFormat(void)
{
int isPassthrough = 0;
int err = 0;
m_currentSampleRate = m_pAudioCtx->sample_rate;
m_currentHwSampleRate = m_pAudioCtx->sample_rate;
m_currentNumChannels = m_pAudioCtx->ch_layout.nb_channels;
m_currentHwNumChannels = m_pAudioCtx->ch_layout.nb_channels;
m_currentPassthroughMask = m_passthroughMask;
if ((m_currentPassthroughMask & CODEC_AC3 && m_pAudioCtx->codec_id == AV_CODEC_ID_AC3) ||
(m_currentPassthroughMask & CODEC_EAC3 && m_pAudioCtx->codec_id == AV_CODEC_ID_EAC3) ||
(m_currentPassthroughMask & CODEC_DTS && m_pAudioCtx->codec_id == AV_CODEC_ID_DTS)) {
// E-AC3 over HDMI: some receivers need HBR
if (m_pAudioCtx->codec_id == AV_CODEC_ID_EAC3)
m_currentHwSampleRate *= 4;
m_currentHwNumChannels = 2;
m_spdifIndex = 0;
m_spdifRepeatCount = 0;
isPassthrough = 1;
}
if ((err = m_pAudio->Setup(m_pAudioCtx, m_currentHwSampleRate, m_currentHwNumChannels, isPassthrough)) < 0) {
// E-AC3 over HDMI: try without HBR
m_currentHwSampleRate /= 4;
if (m_pAudioCtx->codec_id != AV_CODEC_ID_EAC3 ||
((err = m_pAudio->Setup(m_pAudioCtx, m_currentHwSampleRate, m_currentHwNumChannels, isPassthrough)) < 0)) {
LOGERROR("audiocodec: %s: format change update error", __FUNCTION__);
m_currentHwSampleRate = 0;
m_currentHwNumChannels = 0;
return err;
}
}
if (!err) {
LOGDEBUG2(L_SOUND, "audiocodec: %s: format change %s %dHz *%d channels%s%s%s%s%d", __FUNCTION__,
av_get_sample_fmt_name(m_pAudioCtx->sample_fmt), m_pAudioCtx->sample_rate, m_pAudioCtx->ch_layout.nb_channels,
m_passthroughMask & CODEC_AC3 ? " AC3" : "",
m_passthroughMask & CODEC_EAC3 ? " EAC3" : "",
m_passthroughMask & CODEC_DTS ? " DTS" : "",
m_passthroughMask ? " passthrough mask " : "",
m_passthroughMask ? m_passthroughMask : 0);
}
return err;
}
/**
* Decode an audio packet
*
* This function holds the decoding loop
*
* @param avpkt audio packet to decode
*/
void cAudioDecoder::Decode(const AVPacket * avpkt)
{
std::lock_guard<std::mutex> lock(m_mutex);
int retSend, retRec;
AVFrame *frame;
// decoded frame is also needed for passthrough to set the PTS
frame = m_pFrame;
av_frame_unref(frame);
do {
retSend = avcodec_send_packet(m_pAudioCtx, avpkt);
if (retSend < 0 && retSend != AVERROR(EAGAIN))
LOGERROR("audiocodec: %s: avcodec_send_packet error: %s", __FUNCTION__, av_err2str(retSend));
do {
retRec = avcodec_receive_frame(m_pAudioCtx, frame);
if (retRec < 0) {
if (retRec != AVERROR(EAGAIN))
LOGERROR("audiocodec: %s: avcodec_receive_frame error: %s", __FUNCTION__, av_err2str(retRec));
continue;
}
if (m_lastPts == AV_NOPTS_VALUE && avpkt->pts == AV_NOPTS_VALUE) {
// the first AVPacket has no valid PTS, if its PES packet has been truncated while searching for the sync word
av_frame_unref(frame);
continue;
}
// update audio clock and remeber last PTS or guess the next PTS
if (frame->pts != AV_NOPTS_VALUE) {
m_lastPts = frame->pts;
} else if (m_lastPts != AV_NOPTS_VALUE) {
frame->pts = m_lastPts +
av_rescale_q(frame->nb_samples, (AVRational){1, frame->sample_rate}, m_pAudioCtx->pkt_timebase);
m_lastPts = frame->pts;
}
if (m_currentPassthroughMask != m_passthroughMask ||
m_currentNumChannels != m_pAudioCtx->ch_layout.nb_channels ||
m_currentSampleRate != m_pAudioCtx->sample_rate) {
UpdateFormat();
}
if (!m_currentHwNumChannels || !m_currentHwSampleRate) {
LOGERROR("audiocodec: %s: unsupported format!", __FUNCTION__);
av_frame_unref(frame);
return;
}
if (DecodePassthrough(avpkt, frame)) {
av_frame_unref(frame);
} else
m_pAudio->Filter(frame, m_pAudioCtx);
} while (retRec == 0);
} while (retSend == AVERROR(EAGAIN));
}
/**
* Flush the audio decoder buffers
*
* Also resets the last PTS and Codec ID
*/
void cAudioDecoder::FlushBuffers(void)
{
std::lock_guard<std::mutex> lock(m_mutex);
LOGDEBUG2(L_CODEC, "audiocodec: %s", __FUNCTION__);
if (m_pAudioCtx)
avcodec_flush_buffers(m_pAudioCtx);
m_lastPts = AV_NOPTS_VALUE;
m_codecId = AV_CODEC_ID_NONE;
ResetSpdif();
}
/**
* Rest the internal SPDIF burst buffer
*/
void cAudioDecoder::ResetSpdif(void)
{
m_spdifIndex = 0;
m_spdifRepeatCount = 0;
}
/**
* Set audio pass-through mask
*
* @param mask codec mask to enable (AC-3, E-AC-3, DTS)
*/
void cAudioDecoder::SetPassthroughMask(int mask)
{
LOGDEBUG2(L_CODEC, "audiocodec: %s: %d", __FUNCTION__, mask);
m_passthroughMask = mask & (CODEC_AC3 | CODEC_EAC3 | CODEC_DTS);
}