-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathCodecParser.cpp
More file actions
399 lines (332 loc) · 12 KB
/
CodecParser.cpp
File metadata and controls
399 lines (332 loc) · 12 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
/*
* Copyright (C) 2023 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 "CodecParser.h"
#include <bento4/Ap4Utils.h>
using namespace adaptive;
AdtsType CAdaptiveAdtsHeaderParser::GetAdtsType(AP4_ByteStream* stream)
{
AP4_DataBuffer buffer;
buffer.SetDataSize(AP4_EAC3_HEADER_SIZE); // max header size is 64 (E-AC3)
AdtsType adtsType = AdtsType::NONE;
if (!AP4_SUCCEEDED(stream->Read(buffer.UseData(), AP4_EAC3_HEADER_SIZE)))
return adtsType;
AP4_BitReader bits(buffer.GetData(), AP4_EAC3_HEADER_SIZE);
AP4_UI32 syncWord = bits.ReadBits(16);
if ((syncWord & AP4_ADTS_SYNC_MASK) == AP4_ADTS_SYNC_PATTERN)
{
adtsType = AdtsType::AAC;
}
else if ((syncWord & AP4_AC4_SYNC_MASK) == AP4_AC4_SYNC_PATTERN)
{
adtsType = AdtsType::AC4;
}
else if (syncWord == AP4_AC3_SYNC_PATTERN)
{
bits.SkipBits(24);
AP4_UI32 bitStreamID = bits.ReadBits(5);
if ((bitStreamID > 10) && (bitStreamID <= 16))
{
adtsType = AdtsType::EAC3;
}
else if (bitStreamID <= 10)
{
adtsType = AdtsType::AC3;
}
}
AP4_Position currentPos;
stream->Tell(currentPos);
stream->Seek(currentPos - (AP4_EAC3_HEADER_SIZE));
return adtsType;
}
AP4_Result CAdaptiveAdtsParser::FindFrameHeader(AP4_AacFrame& frame)
{
unsigned char raw_header[AP4_ADTS_HEADER_SIZE];
AP4_Result result;
/* align to the start of the next byte */
m_Bits.ByteAlign();
/* find a frame header */
result = FindHeader(raw_header);
if (AP4_FAILED(result))
return result;
/* parse the header */
AP4_AdtsHeader adts_header(raw_header);
/* check the header */
result = adts_header.Check();
if (AP4_FAILED(result))
return AP4_ERROR_CORRUPTED_BITSTREAM;
m_Bits.SkipBytes(AP4_ADTS_HEADER_SIZE);
/* fill in the frame info */
frame.m_Info.m_Standard =
(adts_header.m_Id == 1 ? AP4_AAC_STANDARD_MPEG2 : AP4_AAC_STANDARD_MPEG4);
switch (adts_header.m_ProfileObjectType)
{
case 0:
frame.m_Info.m_Profile = AP4_AAC_PROFILE_MAIN;
break;
case 1:
frame.m_Info.m_Profile = AP4_AAC_PROFILE_LC;
break;
case 2:
frame.m_Info.m_Profile = AP4_AAC_PROFILE_SSR;
break;
case 3:
frame.m_Info.m_Profile = AP4_AAC_PROFILE_LTP;
}
frame.m_Info.m_FrameLength = adts_header.m_FrameLength - AP4_ADTS_HEADER_SIZE;
frame.m_Info.m_ChannelConfiguration = adts_header.m_ChannelConfiguration;
frame.m_Info.m_SamplingFrequencyIndex = adts_header.m_SamplingFrequencyIndex;
frame.m_Info.m_SamplingFrequency =
AP4_AdtsSamplingFrequencyTable[adts_header.m_SamplingFrequencyIndex];
/* skip crc if present */
if (adts_header.m_ProtectionAbsent == 0)
{
m_Bits.SkipBits(16);
}
/* set the frame source */
frame.m_Source = &m_Bits;
return AP4_SUCCESS;
}
AP4_Result CAdaptiveAc3Parser::FindFrameHeader(AP4_Ac3Frame& frame)
{
unsigned char raw_header[AP4_AC3_HEADER_SIZE];
AP4_Result result;
/* align to the start of the next byte */
m_Bits.ByteAlign();
/* find a frame header */
result = FindHeader(raw_header);
if (AP4_FAILED(result))
return result;
if (m_LittleEndian)
{
AP4_ByteSwap16(raw_header, AP4_AC3_HEADER_SIZE);
}
/* parse the header */
AP4_Ac3Header ac3_header(raw_header);
/* check the header */
result = ac3_header.Check();
if (AP4_FAILED(result))
{
m_Bits.SkipBytes(2);
return AP4_ERROR_CORRUPTED_BITSTREAM;
}
frame.m_Info.m_ChannelCount = ac3_header.m_ChannelCount;
frame.m_Info.m_SampleRate = FSCOD_AC3[ac3_header.m_Fscod];
frame.m_Info.m_FrameSize = ac3_header.m_FrameSize;
frame.m_Info.m_Ac3StreamInfo.fscod = ac3_header.m_Fscod;
frame.m_Info.m_Ac3StreamInfo.bsid = ac3_header.m_Bsid;
frame.m_Info.m_Ac3StreamInfo.bsmod = ac3_header.m_Bsmod;
frame.m_Info.m_Ac3StreamInfo.acmod = ac3_header.m_Acmod;
frame.m_Info.m_Ac3StreamInfo.lfeon = ac3_header.m_Lfeon;
frame.m_Info.m_Ac3StreamInfo.bit_rate_code = ac3_header.m_Frmsizecod / 2;
frame.m_LittleEndian = m_LittleEndian;
/* set the frame source */
frame.m_Source = &m_Bits;
return AP4_SUCCESS;
}
AP4_Result CAdaptiveAc4Parser::FindFrameHeader(AP4_Ac4Frame& frame)
{
unsigned int available;
unsigned char raw_header[AP4_AC4_HEADER_SIZE];
AP4_Result result;
/* align to the start of the next byte */
m_Bits.ByteAlign();
/* find a frame header */
result = FindHeader(raw_header);
if (AP4_FAILED(result))
return result;
// duplicated work, just to get the frame size
AP4_BitReader tmp_bits(raw_header, AP4_AC4_HEADER_SIZE);
unsigned int sync_frame_size = GetSyncFrameSize(tmp_bits);
if (sync_frame_size > (AP4_BITSTREAM_BUFFER_SIZE - 1))
{
return AP4_ERROR_NOT_ENOUGH_DATA;
}
/*
* Error handling to skip the 'fake' sync word.
* - the maximum sync frame size is about (AP4_BITSTREAM_BUFFER_SIZE - 1) bytes.
*/
if (m_Bits.GetBytesAvailable() < sync_frame_size)
{
if (m_Bits.GetBytesAvailable() == (AP4_BITSTREAM_BUFFER_SIZE - 1))
{
// skip the sync word, assume it's 'fake' sync word
m_Bits.SkipBytes(2);
}
return AP4_ERROR_NOT_ENOUGH_DATA;
}
unsigned char* rawframe = new unsigned char[sync_frame_size];
// copy the whole frame becasue toc size is unknown
m_Bits.PeekBytes(rawframe, sync_frame_size);
/* parse the header */
AP4_Ac4Header ac4_header(rawframe, sync_frame_size);
delete[] rawframe;
// Place before goto statement to resolve Xcode compiler issue
unsigned int bit_rate_mode = 0;
/* check the header */
result = ac4_header.Check();
if (AP4_FAILED(result))
{
m_Bits.SkipBytes(sync_frame_size);
goto fail;
}
/* check if we have enough data to peek at the next header */
available = m_Bits.GetBytesAvailable();
// TODO: find the proper AP4_AC4_MAX_TOC_SIZE or just parse what this step need ?
if (available >= ac4_header.m_FrameSize + ac4_header.m_HeaderSize + ac4_header.m_CrcSize +
AP4_AC4_HEADER_SIZE + AP4_AC4_MAX_TOC_SIZE)
{
// enough to peek at the header of the next frame
m_Bits.SkipBytes(ac4_header.m_FrameSize + ac4_header.m_HeaderSize + ac4_header.m_CrcSize);
m_Bits.PeekBytes(raw_header, AP4_AC4_HEADER_SIZE);
// duplicated work, just to get the frame size
AP4_BitReader peak_tmp_bits(raw_header, AP4_AC4_HEADER_SIZE);
unsigned int next_sync_frame_size = GetSyncFrameSize(peak_tmp_bits);
unsigned char* next_rawframe = new unsigned char[next_sync_frame_size];
// copy the whole frame becasue toc size is unknown
if (m_Bits.GetBytesAvailable() < (next_sync_frame_size))
{
next_sync_frame_size = m_Bits.GetBytesAvailable();
}
m_Bits.PeekBytes(next_rawframe, next_sync_frame_size);
m_Bits.SkipBytes(
-((int)(ac4_header.m_FrameSize + ac4_header.m_HeaderSize + ac4_header.m_CrcSize)));
/* check the header */
AP4_Ac4Header peek_ac4_header(next_rawframe, next_sync_frame_size);
delete[] next_rawframe;
result = peek_ac4_header.Check();
if (AP4_FAILED(result))
{
// TODO: need to reserve current sync frame ?
m_Bits.SkipBytes(sync_frame_size + next_sync_frame_size);
goto fail;
}
/* check that the fixed part of this header is the same as the */
/* fixed part of the previous header */
if (!AP4_Ac4Header::MatchFixed(ac4_header, peek_ac4_header))
{
// TODO: need to reserve current sync frame ?
m_Bits.SkipBytes(sync_frame_size + next_sync_frame_size);
goto fail;
}
}
else if (available < (ac4_header.m_FrameSize + ac4_header.m_HeaderSize + ac4_header.m_CrcSize) ||
(m_Bits.m_Flags & AP4_BITSTREAM_FLAG_EOS) == 0)
{
// not enough for a frame, or not at the end (in which case we'll want to peek at the next header)
return AP4_ERROR_NOT_ENOUGH_DATA;
}
m_Bits.SkipBytes(ac4_header.m_HeaderSize);
/* fill in the frame info */
frame.m_Info.m_HeaderSize = ac4_header.m_HeaderSize;
frame.m_Info.m_FrameSize = ac4_header.m_FrameSize;
frame.m_Info.m_CRCSize = ac4_header.m_CrcSize;
frame.m_Info.m_ChannelCount = ac4_header.m_ChannelCount;
frame.m_Info.m_SampleDuration =
(ac4_header.m_FsIndex == 0) ? 2048 : AP4_Ac4SampleDeltaTable[ac4_header.m_FrameRateIndex];
frame.m_Info.m_MediaTimeScale =
(ac4_header.m_FsIndex == 0) ? 44100 : AP4_Ac4MediaTimeScaleTable[ac4_header.m_FrameRateIndex];
frame.m_Info.m_Iframe = ac4_header.m_BIframeGlobal;
/* fill the AC4 DSI info */
frame.m_Info.m_Ac4Dsi.ac4_dsi_version = 1;
frame.m_Info.m_Ac4Dsi.d.v1.bitstream_version = ac4_header.m_BitstreamVersion;
frame.m_Info.m_Ac4Dsi.d.v1.fs_index = ac4_header.m_FsIndex;
frame.m_Info.m_Ac4Dsi.d.v1.fs =
AP4_Ac4SamplingFrequencyTable[frame.m_Info.m_Ac4Dsi.d.v1.fs_index];
frame.m_Info.m_Ac4Dsi.d.v1.frame_rate_index = ac4_header.m_FrameRateIndex;
frame.m_Info.m_Ac4Dsi.d.v1.b_program_id = ac4_header.m_BProgramId;
frame.m_Info.m_Ac4Dsi.d.v1.short_program_id = ac4_header.m_ShortProgramId;
frame.m_Info.m_Ac4Dsi.d.v1.b_uuid = ac4_header.m_BProgramUuidPresent;
AP4_CopyMemory(frame.m_Info.m_Ac4Dsi.d.v1.program_uuid, ac4_header.m_ProgramUuid, 16);
// Calcuate the bit rate mode according to ETSI TS 103 190-2 V1.2.1 Annex B
if (ac4_header.m_WaitFrames == 0)
{
bit_rate_mode = 1;
}
else if (ac4_header.m_WaitFrames >= 1 && ac4_header.m_WaitFrames <= 6)
{
bit_rate_mode = 2;
}
else if (ac4_header.m_WaitFrames > 6)
{
bit_rate_mode = 3;
}
frame.m_Info.m_Ac4Dsi.d.v1.ac4_bitrate_dsi.bit_rate_mode = bit_rate_mode;
frame.m_Info.m_Ac4Dsi.d.v1.ac4_bitrate_dsi.bit_rate = 0; // unknown, fixed value now
frame.m_Info.m_Ac4Dsi.d.v1.ac4_bitrate_dsi.bit_rate_precision =
0xffffffff; // unknown, fixed value now
frame.m_Info.m_Ac4Dsi.d.v1.n_presentations = ac4_header.m_NPresentations;
frame.m_Info.m_Ac4Dsi.d.v1.presentations = ac4_header.m_PresentationV1;
/* set the frame source */
frame.m_Source = &m_Bits;
return AP4_SUCCESS;
fail:
/* skip the header and return (only skip the first byte in */
/* case this was a false header that hides one just after) */
return AP4_ERROR_CORRUPTED_BITSTREAM;
}
AP4_Result CAdaptiveEac3Parser::FindFrameHeader(AP4_Eac3Frame& frame)
{
bool dependent_stream_exist = false;
unsigned int dependent_stream_chan_loc = 0;
unsigned int dependent_stream_length = 0;
unsigned int skip_size = 0;
unsigned char raw_header[AP4_EAC3_HEADER_SIZE];
AP4_Result result;
/* align to the start of the next byte */
m_Bits.ByteAlign();
/* find a frame header */
result = FindHeader(raw_header, skip_size);
if (AP4_FAILED(result))
return result;
if (m_LittleEndian)
{
AP4_ByteSwap16(raw_header, AP4_EAC3_HEADER_SIZE);
}
/* parse the header */
AP4_Eac3Header eac3_header(raw_header);
/* check the header */
result = eac3_header.Check();
if (AP4_FAILED(result))
return AP4_ERROR_CORRUPTED_BITSTREAM;
/* fill in the frame info */
frame.m_Info.m_ChannelCount = eac3_header.m_ChannelCount;
if (dependent_stream_exist)
{
frame.m_Info.m_FrameSize = eac3_header.m_FrameSize + dependent_stream_length;
}
else
{
frame.m_Info.m_FrameSize = eac3_header.m_FrameSize;
}
frame.m_Info.m_SampleRate = EAC3_SAMPLE_RATE_ARY[eac3_header.m_Fscod];
frame.m_Info.m_Eac3SubStream.fscod = eac3_header.m_Fscod;
frame.m_Info.m_Eac3SubStream.bsid = eac3_header.m_Bsid;
frame.m_Info.m_Eac3SubStream.bsmod = eac3_header.m_Bsmod;
frame.m_Info.m_Eac3SubStream.acmod = eac3_header.m_Acmod;
frame.m_Info.m_Eac3SubStream.lfeon = eac3_header.m_Lfeon;
if (dependent_stream_exist)
{
frame.m_Info.m_Eac3SubStream.num_dep_sub = 1;
frame.m_Info.m_Eac3SubStream.chan_loc = dependent_stream_chan_loc;
}
else
{
frame.m_Info.m_Eac3SubStream.num_dep_sub = 0;
frame.m_Info.m_Eac3SubStream.chan_loc = 0;
}
frame.m_Info.complexity_index_type_a = 0;
if (eac3_header.m_Addbsie && (eac3_header.m_Addbsil == 1) && (eac3_header.m_Addbsi[0] == 0x1))
{
frame.m_Info.complexity_index_type_a = eac3_header.m_Addbsi[1];
}
/* set the little endian flag */
frame.m_LittleEndian = m_LittleEndian;
/* set the frame source */
frame.m_Source = &m_Bits;
return AP4_SUCCESS;
}