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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "VideoPlayerCodec.h"
#include "ServiceBroker.h"
#include "URL.h"
#include "cores/AudioEngine/AEResampleFactory.h"
#include "cores/AudioEngine/Interfaces/AE.h"
#include "cores/AudioEngine/Utils/AEUtil.h"
#include "cores/VideoPlayer/DVDCodecs/DVDFactoryCodec.h"
#include "cores/VideoPlayer/DVDDemuxers/DVDDemuxUtils.h"
#include "cores/VideoPlayer/DVDDemuxers/DVDFactoryDemuxer.h"
#include "cores/VideoPlayer/DVDInputStreams/DVDFactoryInputStream.h"
#include "cores/VideoPlayer/DVDStreamInfo.h"
#include "music/tags/TagLoaderTagLib.h"
#include "utils/StringUtils.h"
#include "utils/log.h"
VideoPlayerCodec::VideoPlayerCodec() : m_processInfo(CProcessInfo::CreateInstance())
{
m_CodecName = "VideoPlayer";
}
VideoPlayerCodec::~VideoPlayerCodec()
{
DeInit();
}
AEAudioFormat VideoPlayerCodec::GetFormat()
{
AEAudioFormat format;
if (m_pAudioCodec)
{
format = m_pAudioCodec->GetFormat();
}
return format;
}
void VideoPlayerCodec::SetContentType(const std::string &strContent)
{
m_strContentType = strContent;
StringUtils::ToLower(m_strContentType);
}
void VideoPlayerCodec::SetPassthroughStreamType(CAEStreamInfo::DataType streamType)
{
m_srcFormat.m_streamInfo.m_type = streamType;
}
bool VideoPlayerCodec::Init(const CFileItem &file, unsigned int filecache)
{
// take precaution if Init()ialized earlier
if (m_bInited)
{
// keep things as is if Init() was done with known strFile
if (m_strFileName == file.GetDynPath())
return true;
// got differing filename, so cleanup before starting over
DeInit();
}
m_nDecodedLen = 0;
CFileItem fileitem(file);
fileitem.SetMimeType(m_strContentType);
fileitem.SetMimeTypeForInternetFile();
m_pInputStream = CDVDFactoryInputStream::CreateInputStream(NULL, fileitem);
if (!m_pInputStream)
{
CLog::Log(LOGERROR, "{}: Error creating input stream for {}", __FUNCTION__, file.GetDynPath());
return false;
}
//! @todo
//! convey CFileItem::ContentLookup() into Open()
if (!m_pInputStream->Open())
{
CLog::Log(LOGERROR, "{}: Error opening file {}", __FUNCTION__, file.GetDynPath());
if (m_pInputStream.use_count() > 1)
throw std::runtime_error("m_pInputStream reference count is greater than 1");
m_pInputStream.reset();
return false;
}
m_pDemuxer = NULL;
try
{
m_pDemuxer = CDVDFactoryDemuxer::CreateDemuxer(m_pInputStream);
if (!m_pDemuxer)
{
if (m_pInputStream.use_count() > 1)
throw std::runtime_error("m_pInputStream reference count is greater than 1");
m_pInputStream.reset();
CLog::Log(LOGERROR, "{}: Error creating demuxer", __FUNCTION__);
return false;
}
}
catch(...)
{
CLog::Log(LOGERROR, "{}: Exception thrown when opening demuxer", __FUNCTION__);
if (m_pDemuxer)
{
delete m_pDemuxer;
m_pDemuxer = NULL;
}
return false;
}
CDemuxStream* pStream = NULL;
m_nAudioStream = -1;
int64_t demuxerId = -1;
for (auto stream : m_pDemuxer->GetStreams())
{
if (stream && stream->type == STREAM_AUDIO)
{
m_nAudioStream = stream->uniqueId;
demuxerId = stream->demuxerId;
pStream = stream;
break;
}
}
if (m_nAudioStream == -1)
{
CLog::Log(LOGERROR, "{}: Could not find audio stream", __FUNCTION__);
delete m_pDemuxer;
m_pDemuxer = NULL;
if (m_pInputStream.use_count() > 1)
throw std::runtime_error("m_pInputStream reference count is greater than 1");
m_pInputStream.reset();
return false;
}
CDVDStreamInfo hint(*pStream, true);
CAEStreamInfo::DataType ptStreamTye =
GetPassthroughStreamType(hint.codec, hint.samplerate, hint.profile);
m_pAudioCodec = CDVDFactoryCodec::CreateAudioCodec(hint, *m_processInfo, true, true, ptStreamTye);
if (!m_pAudioCodec)
{
CLog::Log(LOGERROR, "{}: Could not create audio codec", __FUNCTION__);
delete m_pDemuxer;
m_pDemuxer = NULL;
if (m_pInputStream.use_count() > 1)
throw std::runtime_error("m_pInputStream reference count is greater than 1");
m_pInputStream.reset();
return false;
}
// Extract ReplayGain info
// tagLoaderTagLib.Load will try to determine tag type by file extension, so set fallback by contentType
std::string strFallbackFileExtension = "";
if (m_strContentType == "audio/aacp" ||
m_strContentType == "audio/aac")
strFallbackFileExtension = "m4a";
else if (m_strContentType == "audio/x-ms-wma")
strFallbackFileExtension = "wma";
else if (m_strContentType == "audio/x-ape" ||
m_strContentType == "audio/ape")
strFallbackFileExtension = "ape";
CTagLoaderTagLib tagLoaderTagLib;
tagLoaderTagLib.Load(file.GetDynPath(), m_tag, strFallbackFileExtension);
// we have to decode initial data in order to get channels/samplerate
// for sanity - we read no more than 10 packets
int nErrors = 0;
for (int nPacket = 0;
nPacket < 10 && (m_channels == 0 || m_format.m_sampleRate == 0 || m_format.m_frameSize == 0);
nPacket++)
{
uint8_t dummy[256];
size_t nSize = 256;
if (ReadPCM(dummy, nSize, &nSize) == READ_ERROR)
++nErrors;
m_srcFormat = m_pAudioCodec->GetFormat();
m_format = m_srcFormat;
m_channels = m_srcFormat.m_channelLayout.Count();
m_bitsPerSample = CAEUtil::DataFormatToBits(m_srcFormat.m_dataFormat);
m_bitsPerCodedSample = static_cast<CDemuxStreamAudio*>(pStream)->iBitsPerSample;
}
if (nErrors >= 10)
{
CLog::Log(LOGDEBUG, "{}: Could not decode data", __FUNCTION__);
return false;
}
// test if seeking is supported
m_bCanSeek = false;
if (m_pInputStream->Seek(0, SEEK_POSSIBLE))
{
if (Seek(1))
{
// rewind stream to beginning
Seek(0);
m_bCanSeek = true;
}
else
{
m_pInputStream->Seek(0, SEEK_SET);
if (!m_pDemuxer->Reset())
return false;
}
}
if (m_channels == 0) // no data - just guess and hope for the best
{
m_srcFormat.m_channelLayout = CAEChannelInfo(AE_CH_LAYOUT_2_0);
m_channels = m_srcFormat.m_channelLayout.Count();
}
if (m_srcFormat.m_sampleRate == 0)
m_srcFormat.m_sampleRate = 44100;
m_TotalTime = m_pDemuxer->GetStreamLength();
m_bitRate = m_pAudioCodec->GetBitRate();
if (!m_bitRate && m_TotalTime)
{
m_bitRate = (int)(((m_pInputStream->GetLength()*1000) / m_TotalTime) * 8);
}
m_CodecName = m_pDemuxer->GetStreamCodecName(demuxerId, m_nAudioStream);
m_needConvert = false;
if (NeedConvert(m_srcFormat.m_dataFormat))
{
m_needConvert = true;
// if we don't know the framesize yet, we will fail when converting
if (m_srcFormat.m_frameSize == 0)
return false;
m_pResampler = ActiveAE::CAEResampleFactory::Create();
SampleConfig dstConfig, srcConfig;
dstConfig.channel_layout = CAEUtil::GetAVChannelLayout(m_srcFormat.m_channelLayout);
dstConfig.channels = m_channels;
dstConfig.sample_rate = m_srcFormat.m_sampleRate;
dstConfig.fmt = CAEUtil::GetAVSampleFormat(AE_FMT_FLOAT);
dstConfig.bits_per_sample = CAEUtil::DataFormatToUsedBits(AE_FMT_FLOAT);
dstConfig.dither_bits = CAEUtil::DataFormatToDitherBits(AE_FMT_FLOAT);
srcConfig.channel_layout = CAEUtil::GetAVChannelLayout(m_srcFormat.m_channelLayout);
srcConfig.channels = m_channels;
srcConfig.sample_rate = m_srcFormat.m_sampleRate;
srcConfig.fmt = CAEUtil::GetAVSampleFormat(m_srcFormat.m_dataFormat);
srcConfig.bits_per_sample = CAEUtil::DataFormatToUsedBits(m_srcFormat.m_dataFormat);
srcConfig.dither_bits = CAEUtil::DataFormatToDitherBits(m_srcFormat.m_dataFormat);
m_pResampler->Init(dstConfig, srcConfig,
false,
false,
M_SQRT1_2,
NULL,
AE_QUALITY_UNKNOWN,
false);
m_planes = AE_IS_PLANAR(m_srcFormat.m_dataFormat) ? m_channels : 1;
m_format = m_srcFormat;
m_format.m_dataFormat = AE_FMT_FLOAT;
m_bitsPerSample = CAEUtil::DataFormatToBits(m_format.m_dataFormat);
}
m_strFileName = file.GetDynPath();
m_bInited = true;
return true;
}
void VideoPlayerCodec::DeInit()
{
if (m_pDemuxer != NULL)
{
delete m_pDemuxer;
m_pDemuxer = NULL;
}
if (m_pInputStream.use_count() > 1)
throw std::runtime_error("m_pInputStream reference count is greater than 1");
m_pInputStream.reset();
m_pAudioCodec.reset();
delete m_pResampler;
m_pResampler = NULL;
// cleanup format information
m_TotalTime = 0;
m_bitsPerSample = 0;
m_bitRate = 0;
m_channels = 0;
m_format.m_dataFormat = AE_FMT_INVALID;
m_nDecodedLen = 0;
m_strFileName = "";
m_bInited = false;
}
bool VideoPlayerCodec::Seek(int64_t iSeekTime)
{
// default to announce backwards seek if !m_pPacket to not make FFmpeg
// skip mpeg audio frames at playback start
bool seekback = true;
bool ret = m_pDemuxer->SeekTime((int)iSeekTime, seekback);
m_pAudioCodec->Reset();
m_nDecodedLen = 0;
return ret;
}
int VideoPlayerCodec::ReadPCM(uint8_t* pBuffer, size_t size, size_t* actualsize)
{
if (m_nDecodedLen > 0)
{
size_t nLen = (size < m_nDecodedLen) ? size : m_nDecodedLen;
*actualsize = nLen;
if (m_needConvert)
{
int samples = *actualsize / (m_bitsPerSample>>3);
int frames = samples / m_channels;
m_pResampler->Resample(&pBuffer, frames, m_audioFrame.data, frames, 1.0);
for (int i=0; i<m_planes; i++)
{
m_audioFrame.data[i] += frames*m_srcFormat.m_frameSize/m_planes;
}
}
else
{
memcpy(pBuffer, m_audioFrame.data[0], *actualsize);
m_audioFrame.data[0] += (*actualsize);
}
m_nDecodedLen -= nLen;
return READ_SUCCESS;
}
m_nDecodedLen = 0;
m_pAudioCodec->GetData(m_audioFrame);
int bytes = m_audioFrame.nb_frames * m_audioFrame.framesize;
if (!bytes)
{
DemuxPacket* pPacket = nullptr;
do
{
if (pPacket)
CDVDDemuxUtils::FreeDemuxPacket(pPacket);
pPacket = m_pDemuxer->Read();
} while (pPacket && pPacket->iStreamId != m_nAudioStream);
if (!pPacket)
{
return READ_EOF;
}
pPacket->pts = DVD_NOPTS_VALUE;
pPacket->dts = DVD_NOPTS_VALUE;
int ret = m_pAudioCodec->AddData(*pPacket);
CDVDDemuxUtils::FreeDemuxPacket(pPacket);
if (ret < 0)
{
return READ_ERROR;
}
m_pAudioCodec->GetData(m_audioFrame);
bytes = m_audioFrame.nb_frames * m_audioFrame.framesize;
}
m_nDecodedLen = bytes;
// scale decoded bytes to destination format
if (m_needConvert)
m_nDecodedLen *= (m_bitsPerSample>>3) / (m_srcFormat.m_frameSize / m_channels);
*actualsize = (m_nDecodedLen <= size) ? m_nDecodedLen : size;
if (*actualsize > 0)
{
if (m_needConvert)
{
int samples = *actualsize / (m_bitsPerSample>>3);
int frames = samples / m_channels;
m_pResampler->Resample(&pBuffer, frames, m_audioFrame.data, frames, 1.0);
for (int i=0; i<m_planes; i++)
{
m_audioFrame.data[i] += frames*m_srcFormat.m_frameSize/m_planes;
}
}
else
{
memcpy(pBuffer, m_audioFrame.data[0], *actualsize);
m_audioFrame.data[0] += *actualsize;
}
m_nDecodedLen -= *actualsize;
}
return READ_SUCCESS;
}
int VideoPlayerCodec::ReadRaw(uint8_t **pBuffer, int *bufferSize)
{
DemuxPacket* pPacket;
m_nDecodedLen = 0;
DVDAudioFrame audioframe;
m_pAudioCodec->GetData(audioframe);
if (audioframe.nb_frames)
{
return READ_SUCCESS;
}
do
{
pPacket = m_pDemuxer->Read();
} while (pPacket && pPacket->iStreamId != m_nAudioStream);
if (!pPacket)
{
return READ_EOF;
}
pPacket->pts = DVD_NOPTS_VALUE;
pPacket->dts = DVD_NOPTS_VALUE;
int ret = m_pAudioCodec->AddData(*pPacket);
CDVDDemuxUtils::FreeDemuxPacket(pPacket);
if (ret < 0)
{
return READ_ERROR;
}
m_pAudioCodec->GetData(audioframe);
if (audioframe.nb_frames)
{
*bufferSize = audioframe.nb_frames;
*pBuffer = audioframe.data[0];
}
else
{
*bufferSize = 0;
}
return READ_SUCCESS;
}
bool VideoPlayerCodec::CanInit()
{
return true;
}
bool VideoPlayerCodec::CanSeek()
{
return m_bCanSeek;
}
bool VideoPlayerCodec::NeedConvert(AEDataFormat fmt)
{
if (fmt == AE_FMT_RAW)
return false;
switch(fmt)
{
case AE_FMT_U8:
case AE_FMT_S16NE:
case AE_FMT_S32NE:
case AE_FMT_FLOAT:
case AE_FMT_DOUBLE:
return false;
default:
return true;
}
}
CAEStreamInfo::DataType VideoPlayerCodec::GetPassthroughStreamType(AVCodecID codecId,
int samplerate,
int profile)
{
AEAudioFormat format;
format.m_dataFormat = AE_FMT_RAW;
format.m_sampleRate = samplerate;
format.m_streamInfo.m_type = CAEStreamInfo::DataType::STREAM_TYPE_NULL;
switch (codecId)
{
case AV_CODEC_ID_AC3:
format.m_streamInfo.m_type = CAEStreamInfo::STREAM_TYPE_AC3;
format.m_streamInfo.m_sampleRate = samplerate;
break;
case AV_CODEC_ID_EAC3:
format.m_streamInfo.m_type = CAEStreamInfo::STREAM_TYPE_EAC3;
format.m_streamInfo.m_sampleRate = samplerate;
break;
case AV_CODEC_ID_DTS:
if (profile == FF_PROFILE_DTS_HD_HRA)
format.m_streamInfo.m_type = CAEStreamInfo::STREAM_TYPE_DTSHD;
else if (profile == FF_PROFILE_DTS_HD_MA)
format.m_streamInfo.m_type = CAEStreamInfo::STREAM_TYPE_DTSHD_MA;
else
format.m_streamInfo.m_type = CAEStreamInfo::STREAM_TYPE_DTSHD_CORE;
format.m_streamInfo.m_sampleRate = samplerate;
break;
case AV_CODEC_ID_TRUEHD:
format.m_streamInfo.m_type = CAEStreamInfo::STREAM_TYPE_TRUEHD;
format.m_streamInfo.m_sampleRate = samplerate;
break;
default:
format.m_streamInfo.m_type = CAEStreamInfo::STREAM_TYPE_NULL;
}
bool supports = CServiceBroker::GetActiveAE()->SupportsRaw(format);
if (!supports && codecId == AV_CODEC_ID_DTS &&
format.m_streamInfo.m_type != CAEStreamInfo::STREAM_TYPE_DTSHD_CORE &&
CServiceBroker::GetActiveAE()->UsesDtsCoreFallback())
{
format.m_streamInfo.m_type = CAEStreamInfo::STREAM_TYPE_DTSHD_CORE;
supports = CServiceBroker::GetActiveAE()->SupportsRaw(format);
}
if (supports)
return format.m_streamInfo.m_type;
else
return CAEStreamInfo::DataType::STREAM_TYPE_NULL;
}
|