-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathADTSReader.cpp
More file actions
491 lines (404 loc) · 12.6 KB
/
ADTSReader.cpp
File metadata and controls
491 lines (404 loc) · 12.6 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
485
486
487
488
489
490
491
/*
* Copyright (C) 2017 peak3d (http://www.peak3d.de)
* 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 "ADTSReader.h"
#include "parser/CodecParser.h"
#include "utils/log.h"
#include "utils/Utils.h"
#include <stdlib.h>
#include <bento4/Ap4ByteStream.h>
using namespace adaptive;
using namespace UTILS;
uint64_t ID3TAG::getSize(const uint8_t* data, unsigned int len, unsigned int shift)
{
uint64_t size(0);
const uint8_t* dataE(data + len);
for (; data < dataE; ++data)
size = size << shift | *data;
return size;
};
ID3TAG::PARSECODE ID3TAG::parse(AP4_ByteStream* stream)
{
uint8_t buffer[64];
unsigned int retCount(0);
while (!AP4_SUCCEEDED(stream->Read(buffer, HEADER_SIZE)))
{
if (retCount++)
return PARSE_FAIL;
}
// ID3v2 header
// 3 byte "ID3" + 1 byte ver + 1 byte revision + 1 byte flags + 4 byte size
if (memcmp(buffer, "ID3", 3) != 0)
{
AP4_Position pos;
stream->Tell(pos);
stream->Seek(pos - HEADER_SIZE);
return PARSE_NO_ID3;
}
m_majorVer = buffer[3];
m_revisionVer = buffer[4];
m_flags = buffer[5];
uint32_t size = static_cast<uint32_t>(getSize(buffer + 6, 4, 7));
//iterate through frames and search timestamp
while (size > HEADER_SIZE)
{
if (!AP4_SUCCEEDED(stream->Read(buffer, HEADER_SIZE)))
return PARSE_FAIL;
uint32_t frameSize = static_cast<uint32_t>(getSize(buffer + 4, 4, 8));
if (memcmp(buffer, "PRIV", 4) == 0 && frameSize == 53)
{
if (!AP4_SUCCEEDED(stream->Read(buffer, frameSize)))
return PARSE_FAIL;
// HLS audio packet timestamp: https://datatracker.ietf.org/doc/html/rfc8216
if (strncmp(reinterpret_cast<const char*>(buffer), "com.apple.streaming.transportStreamTimestamp", 44) == 0 && buffer[44] == 0)
{
m_timestamp = getSize(buffer + 45, 8, 8);
}
}
else
{
AP4_Position curPos;
stream->Tell(curPos);
stream->Seek(curPos + frameSize);
}
size -= (HEADER_SIZE + frameSize);
}
return PARSE_SUCCESS;
}
void ID3TAG::SkipID3Data(AP4_ByteStream* stream)
{
uint8_t buffer[64];
if (!AP4_SUCCEEDED(stream->Read(buffer, HEADER_SIZE)))
{
// Try again
if (!AP4_SUCCEEDED(stream->Read(buffer, HEADER_SIZE)))
return;
}
if (std::memcmp(buffer, "ID3", 3) != 0) // No ID3 header
{
AP4_Position currentPos;
stream->Tell(currentPos);
stream->Seek(currentPos - HEADER_SIZE);
return;
}
// Get ID3v2 data size
uint32_t headerSize = static_cast<uint32_t>(getSize(buffer + 6, 4, 7));
AP4_Position currentPos;
stream->Tell(currentPos);
stream->Seek(currentPos + headerSize);
}
/**********************************************************************************************************************************/
void ADTSFrame::AdjustStreamForPadding(AP4_ByteStream* stream)
{
AP4_Position currentPos;
AP4_Position newPos;
stream->Tell(currentPos);
stream->Seek(currentPos + 16);
stream->Tell(newPos);
if (newPos - currentPos == 16)
stream->Seek(currentPos);
}
ADTSFrame::ADTSFrameInfo ADTSFrame::GetFrameInfo(AP4_ByteStream* stream)
{
ADTSFrameInfo frameInfo;
frameInfo.m_codecType = CAdaptiveAdtsHeaderParser::GetAdtsType(stream);
switch (frameInfo.m_codecType)
{
case AdtsType::AAC:
ParseAacHeader(stream, frameInfo);
break;
case AdtsType::AC3:
ParseAc3Header(stream, frameInfo);
break;
case AdtsType::AC4:
ParseAc4Header(stream, frameInfo);
break;
case AdtsType::EAC3:
ParseEc3Header(stream, frameInfo);
break;
default:
break;
}
return frameInfo;
}
bool ADTSFrame::parse(AP4_ByteStream* stream)
{
m_frameInfo.m_codecType = CAdaptiveAdtsHeaderParser::GetAdtsType(stream);
switch (m_frameInfo.m_codecType)
{
case AdtsType::AAC:
return ParseAac(stream);
case AdtsType::AC3:
return ParseAc3(stream);
case AdtsType::AC4:
return ParseAc4(stream);
case AdtsType::EAC3:
return ParseEc3(stream);
default:
return false;
}
}
bool ADTSFrame::ParseAac(AP4_ByteStream* stream)
{
if (!ParseAacHeader(stream, m_frameInfo))
return false;
m_summedFrameCount += m_frameInfo.m_frameCount;
// rewind stream to beginning of syncframe
AP4_Position currentPos;
stream->Tell(currentPos);
stream->Seek(currentPos - (AP4_ADTS_HEADER_SIZE));
m_dataBuffer.SetDataSize(m_frameInfo.m_frameSize);
if (!AP4_SUCCEEDED(stream->Read(m_dataBuffer.UseData(), m_dataBuffer.GetDataSize())))
return false;
AdjustStreamForPadding(stream);
return true;
}
bool ADTSFrame::ParseAacHeader(AP4_ByteStream* stream, ADTSFrameInfo& frameInfo)
{
AP4_DataBuffer buffer;
buffer.SetDataSize(16);
if (!AP4_SUCCEEDED(stream->Read(buffer.UseData(), AP4_ADTS_HEADER_SIZE)))
return false;
CAdaptiveAdtsParser parser;
AP4_Size sz = buffer.GetDataSize();
parser.Feed(buffer.GetData(), &sz);
AP4_AacFrame frame;
AP4_Result result = parser.FindFrameHeader(frame);
if (!AP4_SUCCEEDED(result))
return false;
frameInfo.m_codecProfile = frame.m_Info.m_Profile;
frameInfo.m_frameSize = frame.m_Info.m_FrameLength + AP4_ADTS_HEADER_SIZE;
frameInfo.m_frameCount = 1024;
frameInfo.m_sampleRate = frame.m_Info.m_SamplingFrequency;
frameInfo.m_channels = frame.m_Info.m_ChannelConfiguration;
return true;
}
bool ADTSFrame::ParseAc3(AP4_ByteStream* stream)
{
if (!ParseAc3Header(stream, m_frameInfo))
return false;
m_summedFrameCount += m_frameInfo.m_frameCount;
// rewind stream to beginning of syncframe
AP4_Position currentPos;
stream->Tell(currentPos);
stream->Seek(currentPos - (AP4_AC3_HEADER_SIZE));
m_dataBuffer.SetDataSize(m_frameInfo.m_frameSize);
if (!AP4_SUCCEEDED(stream->Read(m_dataBuffer.UseData(), m_dataBuffer.GetDataSize())))
return false;
AdjustStreamForPadding(stream);
return true;
}
bool ADTSFrame::ParseAc3Header(AP4_ByteStream* stream, ADTSFrameInfo& frameInfo)
{
AP4_DataBuffer buffer;
buffer.SetDataSize(AP4_AC3_HEADER_SIZE);
if (!AP4_SUCCEEDED(stream->Read(buffer.UseData(), AP4_AC3_HEADER_SIZE)))
return false;
CAdaptiveAc3Parser parser;
AP4_Size sz = buffer.GetDataSize();
parser.Feed(buffer.GetData(), &sz);
AP4_Ac3Frame frame;
AP4_Result result = parser.FindFrameHeader(frame);
if (!AP4_SUCCEEDED(result))
return false;
frameInfo.m_frameSize = frame.m_Info.m_FrameSize;
frameInfo.m_frameCount = 256u * frame.m_Info.m_ChannelCount;
frameInfo.m_sampleRate = frame.m_Info.m_SampleRate;
frameInfo.m_channels = frame.m_Info.m_ChannelCount;
return true;
}
bool ADTSFrame::ParseAc4(AP4_ByteStream* stream)
{
if (!ParseAc4Header(stream, m_frameInfo))
return false;
m_summedFrameCount += m_frameInfo.m_frameCount;
// rewind stream to beginning of syncframe
AP4_Position currentPos;
stream->Tell(currentPos);
stream->Seek(currentPos - (AP4_AC4_HEADER_SIZE));
m_dataBuffer.SetDataSize(m_frameInfo.m_frameSize);
if (!AP4_SUCCEEDED(stream->Read(m_dataBuffer.UseData(), m_dataBuffer.GetDataSize())))
return false;
AdjustStreamForPadding(stream);
return true;
}
bool ADTSFrame::ParseAc4Header(AP4_ByteStream* stream, ADTSFrameInfo& frameInfo)
{
AP4_DataBuffer buffer;
buffer.SetDataSize(AP4_AC3_HEADER_SIZE);
if (!AP4_SUCCEEDED(stream->Read(buffer.UseData(), AP4_AC3_HEADER_SIZE)))
return false;
CAdaptiveAc3Parser parser;
AP4_Size sz = buffer.GetDataSize();
parser.Feed(buffer.GetData(), &sz);
AP4_Ac3Frame frame;
AP4_Result result = parser.FindFrameHeader(frame);
if (!AP4_SUCCEEDED(result))
return false;
frameInfo.m_frameSize = frame.m_Info.m_FrameSize;
frameInfo.m_frameCount = 256u * frame.m_Info.m_ChannelCount;
frameInfo.m_sampleRate = frame.m_Info.m_SampleRate;
frameInfo.m_channels = frame.m_Info.m_ChannelCount;
return true;
}
bool ADTSFrame::ParseEc3(AP4_ByteStream* stream)
{
if (!ParseEc3Header(stream, m_frameInfo))
return false;
m_summedFrameCount += m_frameInfo.m_frameCount;
// rewind stream to beginning of syncframe
AP4_Position currentPos;
stream->Tell(currentPos);
stream->Seek(currentPos - (AP4_EAC3_HEADER_SIZE));
m_dataBuffer.SetDataSize(m_frameInfo.m_frameSize);
if (!AP4_SUCCEEDED(stream->Read(m_dataBuffer.UseData(), m_dataBuffer.GetDataSize())))
return false;
AdjustStreamForPadding(stream);
return true;
}
bool ADTSFrame::ParseEc3Header(AP4_ByteStream* stream, ADTSFrameInfo& frameInfo)
{
AP4_DataBuffer buffer;
buffer.SetDataSize(AP4_EAC3_HEADER_SIZE);
if (!AP4_SUCCEEDED(stream->Read(buffer.UseData(), AP4_EAC3_HEADER_SIZE)))
return false;
CAdaptiveEac3Parser parser;
AP4_Size sz = buffer.GetDataSize();
parser.Feed(buffer.GetData(), &sz);
AP4_Eac3Frame frame;
AP4_Result result = parser.FindFrameHeader(frame);
if (!AP4_SUCCEEDED(result))
return false;
frameInfo.m_frameSize = frame.m_Info.m_FrameSize;
frameInfo.m_frameCount = 256u * frame.m_Info.m_ChannelCount;
frameInfo.m_sampleRate = frame.m_Info.m_SampleRate;
if (frame.m_Info.complexity_index_type_a > 0)
{
// The channels value should match the value of the complexity_index_type_a field
frameInfo.m_channels = frame.m_Info.complexity_index_type_a;
frameInfo.m_codecFlags |= CODEC_FLAG_ATMOS;
}
else
{
frameInfo.m_channels = frame.m_Info.m_ChannelCount;
frameInfo.m_codecFlags &= ~CODEC_FLAG_ATMOS;
}
return true;
}
void ADTSFrame::reset()
{
m_summedFrameCount = 0;
m_frameInfo.m_frameCount = 0;
m_dataBuffer.SetDataSize(0);
}
uint64_t ADTSFrame::getPtsOffset() const
{
if (m_frameInfo.m_sampleRate > 0)
return (m_summedFrameCount * 90000) / m_frameInfo.m_sampleRate;
return 0;
}
uint64_t ADTSFrame::getDuration() const
{
if (m_frameInfo.m_sampleRate > 0)
return (static_cast<uint64_t>(m_frameInfo.m_frameCount) * 90000) / m_frameInfo.m_sampleRate;
return 0;
}
/**********************************************************************************************************************************/
ADTSReader::ADTSReader(AP4_ByteStream *stream)
: m_stream(stream)
{
}
ADTSReader::~ADTSReader()
{
}
void ADTSReader::Reset()
{
m_pts = ADTS_PTS_UNSET;
m_frameParser.reset();
}
bool ADTSReader::GetInformation(kodi::addon::InputstreamInfo& info)
{
m_id3TagParser.SkipID3Data(m_stream);
ADTSFrame::ADTSFrameInfo frameInfo = m_frameParser.GetFrameInfo(m_stream);
m_stream->Seek(0); // Seek back because data has been consumed
if (frameInfo.m_codecType == AdtsType::NONE)
return false;
std::string codecName;
STREAMCODEC_PROFILE codecProfile{CodecProfileUnknown};
if (frameInfo.m_codecType == AdtsType::AAC)
{
codecName = CODEC::NAME_AAC;
if (frameInfo.m_codecProfile == AP4_AAC_PROFILE_MAIN)
codecProfile = AACCodecProfileMAIN;
else if (frameInfo.m_codecProfile == AP4_AAC_PROFILE_LC)
codecProfile = AACCodecProfileLOW;
else if (frameInfo.m_codecProfile == AP4_AAC_PROFILE_SSR)
codecProfile = AACCodecProfileSSR;
else if (frameInfo.m_codecProfile == AP4_AAC_PROFILE_LTP)
codecProfile = AACCodecProfileLTP;
}
else if (frameInfo.m_codecType == AdtsType::AC3)
{
codecName = CODEC::NAME_AC3;
}
else if (frameInfo.m_codecType == AdtsType::AC4)
{
codecName = CODEC::NAME_AC4;
}
else if (frameInfo.m_codecType == AdtsType::EAC3)
{
codecName = CODEC::NAME_EAC3;
if ((frameInfo.m_codecFlags & ADTSFrame::CODEC_FLAG_ATMOS) == ADTSFrame::CODEC_FLAG_ATMOS)
codecProfile = DDPlusCodecProfileAtmos;
}
bool isChanged{false};
if (!codecName.empty() && info.GetCodecName() != codecName)
{
info.SetCodecName(codecName);
isChanged = true;
}
if (codecProfile != CodecProfileUnknown && info.GetCodecProfile() != codecProfile)
{
info.SetCodecProfile(codecProfile);
isChanged = true;
}
if (info.GetChannels() != frameInfo.m_channels)
{
info.SetChannels(frameInfo.m_channels);
isChanged = true;
}
if (info.GetSampleRate() != frameInfo.m_sampleRate)
{
info.SetSampleRate(frameInfo.m_sampleRate);
isChanged = true;
}
return isChanged;
}
// We assume that m_startpos is the current I-Frame position
bool ADTSReader::SeekTime(uint64_t timeInTs, bool preceeding)
{
while (m_pts < timeInTs)
if (!ReadPacket())
return false;
return true;
}
bool ADTSReader::ReadPacket()
{
ID3TAG::PARSECODE id3Ret;
while (true)
{
if ((id3Ret = m_id3TagParser.parse(m_stream)) == ID3TAG::PARSE_SUCCESS)
continue;
else if (id3Ret == ID3TAG::PARSE_FAIL)
return false;
if (m_id3TagParser.getPts(m_basePts))
m_frameParser.resetFrameCount();
m_pts = m_basePts + m_frameParser.getPtsOffset();
return m_frameParser.parse(m_stream);
}
return true;
}