Repository navigation
Expand file tree
/
Copy pathcodec_audio.cpp
More file actions
484 lines (405 loc) · 14.7 KB
/
Copy pathcodec_audio.cpp
File metadata and controls
484 lines (405 loc) · 14.7 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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// 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 <string>
#include <vector>
#include <unistd.h>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}
#include "audio.h"
#include "codec_audio.h"
#include "config.h"
#include "logger.h"
#include "spdif.h"
#include "misc.h"
/**
* Build a passthrough mask logging string
*
* @param mask passthrough bitmask
*
* @return a string like " (passthrough mask: AC3 E-AC-3 DTS TrueHD DTS-HD)"
*/
static std::string GetPassthroughMaskString(int mask)
{
std::string passthrough = "";
if (mask) {
passthrough += " (passthrough mask:";
if (mask & CODEC_AC3) passthrough += " AC3";
if (mask & CODEC_EAC3) passthrough += " E-AC-3";
if (mask & CODEC_DTS) passthrough += " DTS";
if (mask & CODEC_TRUEHD) passthrough += " TrueHD";
if (mask & CODEC_DTSHD) passthrough += " DTS-HD";
passthrough += ")";
}
return passthrough;
}
/**
* Create a new audio decoder for the given audio context
*
* @param audio audio context
*/
cAudioDecoder::cAudioDecoder(cAudioStream *audio)
: m_pAudio(audio),
m_passthroughMask(m_pAudio->GetPassthroughMask() & CODEC_PASSTHROUGH_ALL)
{
if (!(m_pFrame = av_frame_alloc()))
LOGFATAL("audiocodec: %s: can't allocate audio decoder frame buffer", __FUNCTION__);
LOGDEBUG2(L_CODEC, "audiocodec: create %s%s", __FUNCTION__, GetPassthroughMaskString(m_passthroughMask).c_str());
}
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
*
* If the codec can't be opened (e.g. FFmpeg was built without its decoder),
* the audio of the stream is dropped until the next codec is opened.
*
* @retval true the decoder is open
* @retval false the codec can't be decoded
*/
bool cAudioDecoder::Open(AVCodecID codecId, AVCodecParameters *par, AVRational timebase)
{
std::lock_guard<std::mutex> lock(m_mutex);
// the mediaplayer opens the decoder for every file and audio track without closing it
CloseCodec();
const AVCodec *codec = nullptr;
// remember the codec also, if it can't be opened, so it isn't tried with every packet
m_codecId = codecId;
// the sink may have been switched on or off meanwhile (automatic passthrough)
m_passthroughMask = m_pAudio->GetPassthroughMask() & CODEC_PASSTHROUGH_ALL;
// prefer the fixed point decoders
if (codecId == AV_CODEC_ID_AC3)
codec = avcodec_find_decoder_by_name("ac3_fixed");
else if (codecId == AV_CODEC_ID_AAC)
codec = avcodec_find_decoder_by_name("aac_fixed");
if (!codec)
codec = avcodec_find_decoder(codecId);
if (!codec) {
LOGERROR("audiocodec: %s: no decoder for %s, the audio is dropped", __FUNCTION__, avcodec_get_name(codecId));
return false;
}
if (!(m_pAudioCtx = avcodec_alloc_context3(codec))) {
LOGERROR("audiocodec: %s: can't allocate the codec context for %s, the audio is dropped", __FUNCTION__, avcodec_get_name(codecId));
return false;
}
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) {
LOGERROR("audiocodec: %s: can't open the decoder for %s, the audio is dropped", __FUNCTION__, avcodec_get_name(codecId));
avcodec_free_context(&m_pAudioCtx);
return false;
}
LOGDEBUG2(L_CODEC, "audiocodec: %s: Codec %s found%s", __FUNCTION__, m_pAudioCtx->codec->long_name, GetPassthroughMaskString(m_passthroughMask).c_str());
m_currentSampleRate = 0;
m_currentHwSampleRate = 0;
m_currentNumChannels = 0;
m_currentHwNumChannels = 0;
m_currentPassthroughMask = 0;
m_currentPassthrough = false;
m_currentDtshdRate = 0;
m_passthroughFailed = false;
m_dtshdFailed = false;
return true;
}
/**
* Check, if the current codec couldn't be opened (its audio is dropped)
*/
bool cAudioDecoder::OpenFailed(void)
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_codecId != AV_CODEC_ID_NONE && !m_pAudioCtx;
}
/**
* Close the audio decoder
*/
void cAudioDecoder::Close(void)
{
std::lock_guard<std::mutex> lock(m_mutex);
CloseCodec();
}
/**
* Close the codec and the spdif muxer
*
* Must be called with the decoder mutex.
*/
void cAudioDecoder::CloseCodec(void)
{
// also a codec, which couldn't be opened, is tried again with the next Open()
m_codecId = AV_CODEC_ID_NONE;
if (!m_pAudioCtx)
return;
m_spdif.Close();
LOGDEBUG2(L_CODEC, "audiocodec: %s", __FUNCTION__);
avcodec_free_context(&m_pAudioCtx);
m_codecId = AV_CODEC_ID_NONE;
m_lastPts = AV_NOPTS_VALUE;
m_streamInfo = {};
m_pAudio->Config()->SetAudioStreamInfo({});
}
/**
* Test, if passthrough audio should be tried
*
* To enable passthrough, the current codec must be enabled in the setup
*
* Currently supported: AC3, E-AC-3, DTS, TrueHD, DTS-HD (Master Audio and High Resolution Audio)
*
* @return true, if the data should be passed through
*/
bool cAudioDecoder::ShouldTryPassthrough(void)
{
if (m_passthroughFailed)
return false;
return (m_passthroughMask & CODEC_AC3 && m_pAudioCtx->codec_id == AV_CODEC_ID_AC3) ||
(m_passthroughMask & CODEC_EAC3 && m_pAudioCtx->codec_id == AV_CODEC_ID_EAC3) ||
(m_passthroughMask & CODEC_DTS && m_pAudioCtx->codec_id == AV_CODEC_ID_DTS) ||
(m_passthroughMask & CODEC_DTSHD && m_pAudioCtx->codec_id == AV_CODEC_ID_DTS &&
IsDtsHdProfile(m_pAudioCtx->profile) && !m_dtshdFailed) ||
(m_passthroughMask & CODEC_TRUEHD && m_pAudioCtx->codec_id == AV_CODEC_ID_TRUEHD);
}
/**
* Handle audio format changes and setup audio, if format changed
*
* @retval 0 if new audio was correctly set up,
* otherwise return value of cAudioStream::Setup()
*/
int cAudioDecoder::CheckUpdateFormat(bool passthrough)
{
if (m_currentPassthrough == passthrough &&
m_currentPassthroughMask == m_passthroughMask &&
m_currentNumChannels == m_currentHwNumChannels &&
m_currentSampleRate == m_currentHwSampleRate)
return 0;
m_currentHwSampleRate = m_pAudioCtx->sample_rate;
m_currentHwNumChannels = m_pAudioCtx->ch_layout.nb_channels;
if (passthrough) {
m_spdif.Close();
// The bitstream is sent with the sample rate of the stream. It is only known
// from the codec parameters (mediaplayer), the PES data isn't decoded.
// The DTS-HD profile is also only known from the codec parameters.
bool dtshd = (m_passthroughMask & CODEC_DTSHD) && !m_dtshdFailed;
sPassthroughFormat format = GetPassthroughFormat(m_pAudioCtx->codec_id, m_pAudioCtx->sample_rate, m_pAudioCtx->profile, dtshd);
m_currentHwSampleRate = format.sampleRate;
m_currentHwNumChannels = format.channels;
m_currentDtshdRate = format.dtshdRate;
}
int err = 0;
if ((err = m_pAudio->Setup(m_pAudioCtx->pkt_timebase, m_currentHwSampleRate, m_currentHwNumChannels, passthrough)) < 0) {
LOGERROR("audiocodec: %s: format change update error", __FUNCTION__);
m_currentHwSampleRate = 0;
m_currentHwNumChannels = 0;
m_currentSampleRate = -1; // set up again with the next call
return err;
}
// remember for next update check
m_currentSampleRate = m_currentHwSampleRate;
m_currentNumChannels = m_currentHwNumChannels;
m_currentPassthroughMask = m_passthroughMask;
m_currentPassthrough = passthrough;
UpdateStreamInfo(passthrough, m_currentHwSampleRate, m_currentHwNumChannels);
// audio was newly set up (err == 0)
if (!err)
LOGDEBUG2(L_SOUND, "audiocodec: %s: format changed %s %dHz *%d channels%s", __FUNCTION__,
av_get_sample_fmt_name(m_pAudioCtx->sample_fmt), m_currentHwSampleRate, m_currentHwNumChannels,
GetPassthroughMaskString(m_passthroughMask).c_str());
return 0;
}
/**
* Publish the properties of the played stream for the statistics, if they changed
*
* @param passthrough true, if the stream is passed through
* @param outputSampleRate sample rate of the audio device
* @param outputChannels channels of the audio device
*/
void cAudioDecoder::UpdateStreamInfo(bool passthrough, int outputSampleRate, int outputChannels)
{
const char *codec = avcodec_get_name(m_pAudioCtx->codec_id);
const char *profile = avcodec_profile_name(m_pAudioCtx->codec_id, m_pAudioCtx->profile);
// called with every decoded frame, so compare without copying the strings
if (m_streamInfo.codec == codec && m_streamInfo.profile == (profile ? profile : "") &&
m_streamInfo.sampleRate == m_pAudioCtx->sample_rate &&
m_streamInfo.channels == m_pAudioCtx->ch_layout.nb_channels &&
m_streamInfo.passthrough == passthrough &&
m_streamInfo.outputSampleRate == outputSampleRate &&
m_streamInfo.outputChannels == outputChannels &&
m_streamInfo.device == m_pAudio->GetDeviceName())
return;
m_streamInfo.codec = codec;
m_streamInfo.profile = profile ? profile : "";
m_streamInfo.sampleRate = m_pAudioCtx->sample_rate;
m_streamInfo.channels = m_pAudioCtx->ch_layout.nb_channels;
m_streamInfo.passthrough = passthrough;
m_streamInfo.outputSampleRate = outputSampleRate;
m_streamInfo.outputChannels = outputChannels;
m_streamInfo.device = m_pAudio->GetDeviceName();
m_pAudio->Config()->SetAudioStreamInfo(m_streamInfo);
}
/**
* Forward an audio packet either to the decoder or passthrough
*
* @param avpkt audio packet to decode
*/
void cAudioDecoder::Decode(const AVPacket *avpkt)
{
std::lock_guard<std::mutex> lock(m_mutex);
// the codec couldn't be opened
if (!m_pAudioCtx)
return;
if (ShouldTryPassthrough())
Passthrough(avpkt);
else
DecodePCM(avpkt);
}
/**
* Passthrough audio data
*
* Build spdif headers depending on the codec and send the
* data to the audio device.
*
* @param avpkt undecoded audio packet
*
* @retval 0 codec is not supported for passthrough, sth. went wrong or we need more data to finish a spdif burst packet
* @retval 1 spdif burst was enqueued
*/
int cAudioDecoder::Passthrough(const AVPacket *avpkt)
{
int err = CheckUpdateFormat(true);
// e.g. the device doesn't offer 8 channels with 192kHz
if (err && m_currentDtshdRate && (m_passthroughMask & CODEC_DTS)) {
LOGWARNING("audiocodec: %s: passthrough of DTS-HD is not possible, pass the DTS core through", __FUNCTION__);
m_dtshdFailed = true;
err = CheckUpdateFormat(true);
}
if (err) {
// e.g. the device doesn't offer the needed sample rate (yet)
LOGWARNING("audiocodec: %s: passthrough of %s is not possible, decode it until the next codec change", __FUNCTION__, avcodec_get_name(m_pAudioCtx->codec_id));
m_passthroughFailed = true;
m_spdif.Close();
DecodePCM(avpkt);
return 0;
}
if (!m_spdif.IsOpen() && !m_spdif.Open(m_pAudioCtx->codec_id, m_currentHwSampleRate, m_currentDtshdRate)) {
LOGERROR("audiocodec: %s: failed to open spdif muxer for codec %s @ %dHz!", __FUNCTION__, avcodec_get_name(m_pAudioCtx->codec_id), m_currentHwSampleRate);
return 0;
}
m_pAudio->SetTimebase(m_pAudioCtx->pkt_timebase);
int64_t pts = AV_NOPTS_VALUE;
const auto &burst = m_spdif.Mux(avpkt, &pts);
if (burst.empty())
return 0;
m_pAudio->EnqueueSpdif(reinterpret_cast<const uint16_t *>(burst.data()), burst.size(), pts);
return 1;
}
/**
* Decode an audio packet
*
* @param avpkt audio packet to decode
*/
void cAudioDecoder::DecodePCM(const AVPacket *avpkt)
{
int retSend, retRec;
AVFrame *frame;
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 remember 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 (CheckUpdateFormat(false)) {
LOGERROR("audiocodec: %s: unsupported format!", __FUNCTION__);
av_frame_unref(frame);
return;
}
m_pAudio->Filter(frame, m_pAudioCtx);
// The audio device of a decoded stream is set up by the filter graph
// (cAudioStream::InitFilter()), CheckUpdateFormat() only sets it up after
// a passthrough. So publish the stream for the statistics here.
UpdateStreamInfo(false, m_pAudio->GetHwSampleRate(), m_pAudio->GetHwNumChannels());
} 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);
// the muxer may hold data of a burst, which isn't complete yet (e.g. TrueHD)
m_spdif.Close();
m_lastPts = AV_NOPTS_VALUE;
m_codecId = AV_CODEC_ID_NONE;
}
/**
* Try the passthrough again
*
* If the passthrough of a codec failed (e.g. the receiver was off), the codec is
* decoded until the next codec change. This allows a new try with the next packet.
* With the automatic passthrough, the codecs of the sink are read again.
*/
void cAudioDecoder::RetryPassthrough(void)
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_passthroughFailed || m_dtshdFailed)
LOGDEBUG2(L_SOUND, "audiocodec: %s: try the passthrough again", __FUNCTION__);
m_passthroughMask = m_pAudio->GetPassthroughMask() & CODEC_PASSTHROUGH_ALL;
m_passthroughFailed = false;
m_dtshdFailed = false;
}
/**
* Update the codecs, which are passed through, from the audio stream
*
* Called after the passthrough setting of the audio stream was changed.
*/
void cAudioDecoder::UpdatePassthroughMask(void)
{
m_passthroughMask = m_pAudio->GetPassthroughMask() & CODEC_PASSTHROUGH_ALL;
m_passthroughFailed = false; // try again with the new settings
m_dtshdFailed = false;
}