forked from iizukanao/node-rtsp-rtmp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aac.coffee
634 lines (544 loc) · 21.5 KB
/
aac.coffee
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
# AAC parser
fs = require 'fs'
Bits = require './bits'
logger = require './logger'
audioBuf = null
MPEG_IDENTIFIER_MPEG2 = 1
MPEG_IDENTIFIER_MPEG4 = 0
eventListeners = {}
api =
SYN_ID_SCE: 0x0 # single_channel_element
SYN_ID_CPE: 0x1 # channel_pair_element
SYN_ID_CCE: 0x2 # coupling_channel_element
SYN_ID_LFE: 0x3 # lfe_channel_element
SYN_ID_DSE: 0x4 # data_stream_elemen
SYN_ID_PCE: 0x5 # program_config_element
SYN_ID_FIL: 0x6 # fill_element
SYN_ID_END: 0x7 # TERM
open: (file) ->
audioBuf = fs.readFileSync file # up to 1GB
close: ->
audioBuf = null
emit: (name, data...) ->
if eventListeners[name]?
for listener in eventListeners[name]
listener data...
return
on: (name, listener) ->
if eventListeners[name]?
eventListeners[name].push listener
else
eventListeners[name] = [ listener ]
end: ->
@emit 'end'
parseADTSHeader: (buf) ->
info = {}
bits = new Bits buf
# adts_fixed_header()
info.syncword = bits.read_bits 12
info.ID = bits.read_bit()
info.layer = bits.read_bits 2
info.protection_absent = bits.read_bit()
info.profile_ObjectType = bits.read_bits 2
info.sampling_frequency_index = bits.read_bits 4
info.private_bit = bits.read_bit()
info.channel_configuration = bits.read_bits 3
info.original_copy = bits.read_bit()
info.home = bits.read_bit()
# adts_variable_header()
info.copyright_identification_bit = bits.read_bit()
info.copyright_identification_start = bits.read_bit()
info.aac_frame_length = bits.read_bits 13
info.adts_buffer_fullness = bits.read_bits 11
info.number_of_raw_data_blocks_in_frame = bits.read_bits 2
return info
# For ascInfo argument, pass a return value of readAudioSpecificConfig()
createADTSHeader: (ascInfo, aac_frame_length) ->
bits = new Bits
bits.create_buf()
# adts_fixed_header()
bits.add_bits 12, 0xfff # syncword
bits.add_bit 0 # ID (1=MPEG-2 AAC; 0=MPEG-4)
bits.add_bits 2, 0 # layer
bits.add_bit 1 # protection_absent
if ascInfo.audioObjectType - 1 > 0b11
throw new Error "invalid audioObjectType: #{ascInfo.audioObjectType} (must be <= 4)"
bits.add_bits 2, ascInfo.audioObjectType - 1 # profile_ObjectType
bits.add_bits 4, ascInfo.samplingFrequencyIndex # sampling_frequency_index
bits.add_bit 0 # private_bit
if ascInfo.channelConfiguration > 0b111
throw new Error "invalid channelConfiguration: #{ascInfo.channelConfiguration} (must be <= 7)"
bits.add_bits 3, ascInfo.channelConfiguration # channel_configuration
bits.add_bit 0 # original_copy
bits.add_bit 0 # home
# adts_variable_header()
bits.add_bit 0 # copyright_identification_bit
bits.add_bit 0 # copyright_identification_start
if aac_frame_length > 8192 - 7 # 7 == length of ADTS header
throw new Error "invalid aac_frame_length: #{aac_frame_length} (must be <= 8192)"
bits.add_bits 13, aac_frame_length + 7 # aac_frame_length (7 == ADTS header length)
bits.add_bits 11, 0x7ff # adts_buffer_fullness (0x7ff = VBR)
bits.add_bits 2, 0 # number_of_raw_data_blocks_in_frame (actual - 1)
return bits.get_created_buf()
getNextPossibleSyncwordPosition: (buffer) ->
syncwordPos = Bits.searchBitsInArray buffer, [0xff, 0xf0], 1
# The maximum distance between two syncwords is 8192 bytes.
if syncwordPos > 8192
throw new Error "the next syncword is too far: #{syncwordPos} bytes"
return syncwordPos
skipToNextPossibleSyncword: ->
syncwordPos = Bits.searchBitsInArray audioBuf, [0xff, 0xf0], 1
if syncwordPos > 0
# The maximum distance between two syncwords is 8192 bytes.
if syncwordPos > 8192
throw new Error "the next syncword is too far: #{syncwordPos} bytes"
logger.debug "skipped #{syncwordPos} bytes until syncword"
audioBuf = audioBuf[syncwordPos..]
return
splitIntoADTSFrames: (buffer) ->
adtsFrames = []
loop
if buffer.length < 7
# not enough ADTS header
break
if (buffer[0] isnt 0xff) or (buffer[1] & 0xf0 isnt 0xf0)
console.log "aac: syncword is not at current position"
syncwordPos = @getNextPossibleSyncwordPosition()
buffer = buffer[syncwordPos..]
continue
aac_frame_length = Bits.parse_bits_uint buffer, 30, 13
if buffer.length < aac_frame_length
# not enough buffer
break
if buffer.length >= aac_frame_length + 2
# check next syncword
if (buffer[aac_frame_length] isnt 0xff) or
(buffer[aac_frame_length+1] & 0xf0 isnt 0xf0) # false syncword
console.log "aac:splitIntoADTSFrames(): syncword was false positive (emulated syncword)"
syncwordPos = @getNextPossibleSyncwordPosition()
buffer = buffer[syncwordPos..]
continue
adtsFrame = buffer[0...aac_frame_length]
# Truncate audio buffer
buffer = buffer[aac_frame_length..]
adtsFrames.push adtsFrame
return adtsFrames
feedPESPacket: (pesPacket) ->
if audioBuf?
audioBuf = Buffer.concat [audioBuf, pesPacket.pes.data]
else
audioBuf = pesPacket.pes.data
pts = pesPacket.pes.PTS
dts = pesPacket.pes.DTS
adtsFrames = []
loop
if audioBuf.length < 7
# not enough ADTS header
break
if (audioBuf[0] isnt 0xff) or (audioBuf[1] & 0xf0 isnt 0xf0)
console.log "aac: syncword is not at current position"
@skipToNextPossibleSyncword()
continue
aac_frame_length = Bits.parse_bits_uint audioBuf, 30, 13
if audioBuf.length < aac_frame_length
# not enough buffer
break
if audioBuf.length >= aac_frame_length + 2
# check next syncword
if (audioBuf[aac_frame_length] isnt 0xff) or
(audioBuf[aac_frame_length+1] & 0xf0 isnt 0xf0) # false syncword
console.log "aac:feedPESPacket(): syncword was false positive (emulated syncword)"
@skipToNextPossibleSyncword()
continue
adtsFrame = audioBuf[0...aac_frame_length]
# Truncate audio buffer
audioBuf = audioBuf[aac_frame_length..]
adtsFrames.push adtsFrame
@emit 'dts_adts_frame', pts, dts, adtsFrame
if adtsFrames.length > 0
@emit 'dts_adts_frames', pts, dts, adtsFrames
feed: (data) ->
if audioBuf?
audioBuf = Buffer.concat [audioBuf, data]
else
audioBuf = data
adtsFrames = []
loop
if audioBuf.length < 7
# not enough ADTS header
break
if (audioBuf[0] isnt 0xff) or (audioBuf[1] & 0xf0 isnt 0xf0)
console.log "aac: syncword is not at current position"
@skipToNextPossibleSyncword()
continue
aac_frame_length = Bits.parse_bits_uint audioBuf, 30, 13
if audioBuf.length < aac_frame_length
# not enough buffer
break
if audioBuf.length >= aac_frame_length + 2
# check next syncword
if (audioBuf[aac_frame_length] isnt 0xff) or
(audioBuf[aac_frame_length+1] & 0xf0 isnt 0xf0) # false syncword
console.log "aac:feed(): syncword was false positive (emulated syncword)"
@skipToNextPossibleSyncword()
continue
adtsFrame = audioBuf[0...aac_frame_length]
# Truncate audio buffer
audioBuf = audioBuf[aac_frame_length..]
adtsFrames.push adtsFrame
@emit 'adts_frame', adtsFrame
if adtsFrames.length > 0
@emit 'adts_frames', adtsFrames
hasMoreData: ->
return audioBuf? and (audioBuf.length > 0)
getSampleRateFromFreqIndex: (freqIndex) ->
switch freqIndex
when 0x0 then 96000
when 0x1 then 88200
when 0x2 then 64000
when 0x3 then 48000
when 0x4 then 44100
when 0x5 then 32000
when 0x6 then 24000
when 0x7 then 22050
when 0x8 then 16000
when 0x9 then 12000
when 0xa then 11025
when 0xb then 8000
when 0xc then 7350
else null # escape value
# ISO 14496-3 - Table 1.16
getSamplingFreqIndex: (sampleRate) ->
switch sampleRate
when 96000 then 0x0
when 88200 then 0x1
when 64000 then 0x2
when 48000 then 0x3
when 44100 then 0x4
when 32000 then 0x5
when 24000 then 0x6
when 22050 then 0x7
when 16000 then 0x8
when 12000 then 0x9
when 11025 then 0xa
when 8000 then 0xb
when 7350 then 0xc
else 0xf # escape value
getChannelConfiguration: (channels) ->
switch channels
when 1 then 1
when 2 then 2
when 3 then 3
when 4 then 4
when 5 then 5
when 6 then 6
when 8 then 7
else
throw new Error "#{channels} channels audio is not supported"
getChannelsByChannelConfiguration: (channelConfiguration) ->
switch channelConfiguration
when 1 then 1
when 2 then 2
when 3 then 3
when 4 then 4
when 5 then 5
when 6 then 6
when 7 then 8
else
throw new Error "Channel configuration #{channelConfiguration} is not supported"
# @param opts: {
# frameLength (int): 1024 or 960
# dependsOnCoreCoder (boolean) (optional): true if core coder is used
# coreCoderDelay (number) (optional): delay in samples. mandatory if
# dependsOnCoreCoder is true.
# }
addGASpecificConfig: (bits, opts) ->
# frameLengthFlag (1 bit)
if opts.frameLengthFlag?
bits.add_bit opts.frameLengthFlag
else
if opts.frameLength is 1024
bits.add_bit 0
else if opts.frameLength is 960
bits.add_bit 1
else
throw new Error "Invalid frameLength: #{opts.frameLength} (must be 1024 or 960)"
# dependsOnCoreCoder (1 bit)
if opts.dependsOnCoreCoder
bits.add_bit 1
bits.add_bits 14, opts.coreCoderDelay
else
bits.add_bit 0
if opts.extensionFlag?
bits.add_bit opts.extensionFlag
else
# extensionFlag (1 bit)
if opts.audioObjectType in [1, 2, 3, 4, 6, 7]
bits.add_bit 0
else
throw new Error "audio object type #{opts.audioObjectType} is not implemented"
# ISO 14496-3 GetAudioObjectType()
readGetAudioObjectType: (bits) ->
audioObjectType = bits.read_bits 5
if audioObjectType is 31
audioObjectType = 32 + bits.read_bits 6
return audioObjectType
# @param opts: {
# samplingFrequencyIndex: number
# channelConfiguration: number
# audioObjectType: number
# }
readGASpecificConfig: (bits, opts) ->
info = {}
info.frameLengthFlag = bits.read_bit()
info.dependsOnCoreCoder = bits.read_bit()
if info.dependsOnCoreCoder is 1
info.coreCoderDelay = bits.read_bits 14
info.extensionFlag = bits.read_bit()
if opts.channelConfiguration is 0
info.program_config_element = api.read_program_config_element bits
if opts.audioObjectType in [6, 20]
info.layerNr = bits.read_bits 3
if info.extensionFlag
if opts.audioObjectType is 22
info.numOfSubFrame = bits.read_bits 5
info.layer_length = bits.read_bits 11
if opts.audioObjectType in [17, 19, 20, 23]
info.aacSectionDataResilienceFlag = bits.read_bit()
info.aacScalefactorDataResilienceFlag = bits.read_bit()
info.aacSpectralDataResilienceFlag = bits.read_bit()
info.extensionFlag3 = bits.read_bit()
# ISO 14496-3 says: tbd in version 3
return info
# ISO 14496-3 1.6.2.1 AudioSpecificConfig
parseAudioSpecificConfig: (buf) ->
bits = new Bits buf
asc = api.readAudioSpecificConfig bits
return asc
# ISO 14496-3 1.6.2.1 AudioSpecificConfig
readAudioSpecificConfig: (bits) ->
info = {}
info.audioObjectType = api.readGetAudioObjectType bits
info.samplingFrequencyIndex = bits.read_bits 4
if info.samplingFrequencyIndex is 0xf
info.samplingFrequency = bits.read_bits 24
else
info.samplingFrequency = api.getSampleRateFromFreqIndex info.samplingFrequencyIndex
info.channelConfiguration = bits.read_bits 4
info.sbrPresentFlag = -1
info.psPresentFlag = -1
info.mpsPresentFlag = -1
if (info.audioObjectType is 5) or (info.audioObjectType is 29)
# Explicit hierarchical signaling of SBR
# 1.6.5.2 2.A in ISO 14496-3
info.explicitHierarchicalSBR = true
info.extensionAudioObjectType = 5
info.sbrPresentFlag = 1
if info.audioObjectType is 29
info.psPresentFlag = 1
extensionSamplingFrequencyIndex = bits.read_bits 4
if extensionSamplingFrequencyIndex is 0xf
info.extensionSamplingFrequency = bits.read_bits 24
else
info.extensionSamplingFrequency = api.getSampleRateFromFreqIndex extensionSamplingFrequencyIndex
info.audioObjectType = api.readGetAudioObjectType bits
if info.audioObjectType is 22
info.extensionChannelConfiguration = bits.read_bits 4
else
info.extensionAudioObjectType = 0
switch info.audioObjectType
when 1, 2, 3, 4, 6, 7, 17, 19, 20, 21, 22, 23
info.gaSpecificConfig = api.readGASpecificConfig bits, info
else
throw new Error "audio object type #{info.audioObjectType} is not implemented"
switch info.audioObjectType
when 17, 19, 20, 21, 22, 23, 24, 25, 26, 27, 39
throw new Error "audio object type #{info.audioObjectType} is not implemented"
extensionIdentifier = -1
if bits.get_remaining_bits() >= 11
extensionIdentifier = bits.read_bits 11
if extensionIdentifier is 0x2b7
extensionIdentifier = -1
if (info.extensionAudioObjectType isnt 5) and (bits.get_remaining_bits() >= 5)
# Explicit backward compatible signaling of SBR
# 1.6.5.2 2.B in ISO 14496-3
info.explicitBackwardCompatibleSBR = true
info.extensionAudioObjectType = api.readGetAudioObjectType bits
if info.extensionAudioObjectType is 5
info.sbrPresentFlag = bits.read_bit()
if info.sbrPresentFlag is 1
extensionSamplingFrequencyIndex = bits.read_bits 4
if extensionSamplingFrequencyIndex is 0xf
info.extensionSamplingFrequency = bits.read_bits 24
else
info.extensionSamplingFrequency = api.getSampleRateFromFreqIndex extensionSamplingFrequencyIndex
if bits.get_remaining_bits() >= 12
extensionIdentifier = bits.read_bits 11
if extensionIdentifier is 0x548
extensionIdentifier = -1
info.psPresentFlag = bits.read_bit()
if info.extensionAudioObjectType is 22
info.sbrPresentFlag = bits.read_bit()
if info.sbrPresentFlag is 1
extensionSamplingFrequencyIndex = bits.read_bits 4
if extensionSamplingFrequencyIndex is 0xf
info.extensionSamplingFrequency = bits.read_bits 24
else
info.extensionSamplingFrequency = api.getSampleRateFromFreqIndex extensionSamplingFrequencyIndex
info.extensionChannelConfiguration = bits.read_bits 4
if (extensionIdentifier is -1) and (bits.get_remaining_bits() >= 11)
extensionIdentifier = bits.read_bits 11
if extensionIdentifier is 0x76a
logger.warn "aac: this audio config may not be supported (extensionIdentifier == 0x76a)"
if (info.audioObjectType isnt 30) and (bits.get_remaining_bits() >= 1)
info.mpsPresentFlag = bits.read_bit()
if info.mpsPresentFlag is 1
info.sacPayloadEmbedding = 1
info.sscLen = bits.read_bits 8
if info.sscLen is 0xff
sscLenExt = bits.read_bits 16
info.sscLen += sscLenExt
info.spatialSpecificConfig = api.readSpatialSpecificConfig bits
return info
readSpatialSpecificConfig: (bits) ->
throw new Error "SpatialSpecificConfig is not implemented"
# Inverse of GetAudioObjectType() in ISO 14496-3 Table 1.14
addAudioObjectType: (bits, audioObjectType) ->
if audioObjectType >= 32
bits.add_bits 5, 31 # 0b11111
bits.add_bits 6, audioObjectType - 32
else
bits.add_bits 5, audioObjectType
# @param opts: A return value of parseAudioSpecificConfig(), or an object: {
# audioObjectType (int): audio object type
# samplingFrequency (int): sample rate in Hz
# extensionSamplingFrequency (int) (optional): sample rate in Hz for extension
# channels (int): number of channels
# extensionChannels (int): number of channels for extension
# frameLength (int): 1024 or 960
# }
createAudioSpecificConfig: (opts, explicitHierarchicalSBR=false) ->
bits = new Bits
bits.create_buf()
# Table 1.13 - AudioSpecificConfig()
if (opts.sbrPresentFlag is 1) and explicitHierarchicalSBR
if opts.psPresentFlag is 1
audioObjectType = 29 # HE-AAC v2
else
audioObjectType = 5 # HE-AAC v1
else
audioObjectType = opts.audioObjectType
api.addAudioObjectType bits, audioObjectType
samplingFreqIndex = api.getSamplingFreqIndex opts.samplingFrequency
bits.add_bits 4, samplingFreqIndex
if samplingFreqIndex is 0xf
bits.add_bits 24, opts.samplingFrequency
if opts.channelConfiguration?
bits.add_bits 4, opts.channelConfiguration
else
channelConfiguration = api.getChannelConfiguration opts.channels
bits.add_bits 4, channelConfiguration
if (opts.sbrPresentFlag is 1) and explicitHierarchicalSBR
# extensionSamplingFrequencyIndex
samplingFreqIndex = api.getSamplingFreqIndex opts.extensionSamplingFrequency
bits.add_bits 4, samplingFreqIndex
if samplingFreqIndex is 0xf
# extensionSamplingFrequency
bits.add_bits 24, opts.extensionSamplingFrequency
api.addAudioObjectType bits, opts.audioObjectType
if opts.audioObjectType is 22
if opts.channelConfiguration?
bits.add_bits 4, opts.channelConfiguration
else
channelConfiguration = api.getChannelConfiguration opts.extensionChannels
bits.add_bits 4, channelConfiguration
switch opts.audioObjectType
when 1, 2, 3, 4, 6, 7, 17, 19, 20, 21, 22, 23
if opts.gaSpecificConfig?
api.addGASpecificConfig bits, opts.gaSpecificConfig
else
api.addGASpecificConfig bits, opts
else
throw new Error "audio object type #{opts.audioObjectType} is not implemented"
switch opts.audioObjectType
when 17, 19, 20, 21, 22, 23, 24, 25, 26, 27, 39
throw new Error "audio object type #{opts.audioObjectType} is not implemented"
if (opts.sbrPresentFlag is 1) and (not explicitHierarchicalSBR)
# extensionIdentifier
bits.add_bits 11, 0x2b7
if opts.audioObjectType isnt 22
# extensionAudioObjectType
api.addAudioObjectType bits, 5
# sbrPresentFlag
bits.add_bit 1
samplingFreqIndex = api.getSamplingFreqIndex opts.extensionSamplingFrequency
# extensionSamplingFrequencyIndex
bits.add_bits 4, samplingFreqIndex
if samplingFreqIndex is 0xf
# extensionSamplingFrequency
bits.add_bits 24, opts.extensionSamplingFrequency
if opts.psPresentFlag is 1
# extensionIdentifier
bits.add_bits 11, 0x548
# psPresentFlag
bits.add_bit 1
else # opts.audioObjectType is 22
# extensionAudioObjectType
api.addAudioObjectType bits, 22
# sbrPresentFlag
bits.add_bit 1
samplingFreqIndex = api.getSamplingFreqIndex opts.extensionSamplingFrequency
# extensionSamplingFrequencyIndex
bits.add_bits 4, samplingFreqIndex
if samplingFreqIndex is 0xf
# extensionSamplingFrequency
bits.add_bits 24, opts.extensionSamplingFrequency
# extensionChannelConfiguration
if opts.extensionChannelConfiguration?
bits.add_bits 4, opts.extensionChannelConfiguration
else
channelConfiguration = api.getChannelConfiguration opts.extensionChannels
bits.add_bits 4, channelConfiguration
return bits.get_created_buf()
parseADTSFrame: (adtsFrame) ->
info = {}
if (adtsFrame[0] isnt 0xff) or (adtsFrame[1] & 0xf0 isnt 0xf0)
throw new Error "malformed audio: data doesn't start with a syncword (0xfff)"
info.mpegIdentifier = Bits.parse_bits_uint adtsFrame, 12, 1
profile_ObjectType = Bits.parse_bits_uint adtsFrame, 16, 2
if info.mpegIdentifier is MPEG_IDENTIFIER_MPEG2
info.audioObjectType = profile_ObjectType
else
info.audioObjectType = profile_ObjectType + 1
freq = Bits.parse_bits_uint adtsFrame, 18, 4
info.sampleRate = api.getSampleRateFromFreqIndex freq
info.channels = Bits.parse_bits_uint adtsFrame, 23, 3
# # raw_data_block starts from byte index 7
# id_syn_ele = Bits.parse_bits_uint adtsFrame, 56, 3
return info
getNextADTSFrame: ->
if not audioBuf?
throw new Error "aac error: file is not opened yet"
loop
if not api.hasMoreData()
return null
if (audioBuf[0] isnt 0xff) or (audioBuf[1] & 0xf0 isnt 0xf0)
console.log "aac: syncword is not at current position"
@skipToNextPossibleSyncword()
continue
aac_frame_length = Bits.parse_bits_uint audioBuf, 30, 13
if audioBuf.length < aac_frame_length
# not enough buffer
return null
if audioBuf.length >= aac_frame_length + 2
# check next syncword
if (audioBuf[aac_frame_length] isnt 0xff) or
(audioBuf[aac_frame_length+1] & 0xf0 isnt 0xf0) # false syncword
console.log "aac:getNextADTSFrame(): syncword was false positive (emulated syncword)"
@skipToNextPossibleSyncword()
continue
adtsFrame = audioBuf[0...aac_frame_length]
# Truncate audio buffer
audioBuf = audioBuf[aac_frame_length..]
return adtsFrame
module.exports = api