2020-06-08 03:29:51 +03:00
|
|
|
package mpeg
|
|
|
|
|
|
|
|
// one AAC frame or "raw data block"
|
|
|
|
|
|
|
|
// ISO/IEC 13818-7 Part 7: Advanced Audio Coding (AAC)
|
|
|
|
// ISO/IEC 14496-3
|
|
|
|
|
|
|
|
import (
|
2021-08-17 13:06:32 +03:00
|
|
|
"github.com/wader/fq/format"
|
|
|
|
"github.com/wader/fq/format/registry"
|
|
|
|
"github.com/wader/fq/pkg/decode"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
registry.MustRegister(&decode.Format{
|
|
|
|
Name: format.AAC_FRAME,
|
|
|
|
Description: "Advanced Audio Coding frame",
|
|
|
|
DecodeFn: aacDecode,
|
2021-11-03 19:19:33 +03:00
|
|
|
RootArray: true,
|
2021-09-16 17:26:31 +03:00
|
|
|
RootName: "elements",
|
2020-06-08 03:29:51 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
SCE = 0b000
|
|
|
|
CPE = 0b001
|
|
|
|
CCE = 0b010
|
|
|
|
LFE = 0b011
|
|
|
|
DSE = 0b100
|
|
|
|
PCE = 0b101
|
|
|
|
FIL = 0b110
|
|
|
|
TERM = 0b111
|
|
|
|
)
|
|
|
|
|
2021-11-05 17:04:26 +03:00
|
|
|
var syntaxElementNames = decode.UToStr{
|
2020-06-08 03:29:51 +03:00
|
|
|
SCE: "SCE",
|
|
|
|
CPE: "CPE",
|
|
|
|
CCE: "CCE",
|
|
|
|
LFE: "LFE",
|
|
|
|
DSE: "DSE",
|
|
|
|
PCE: "PCE",
|
|
|
|
FIL: "FIL",
|
|
|
|
TERM: "TERM",
|
|
|
|
}
|
|
|
|
|
2021-09-27 12:01:14 +03:00
|
|
|
//nolint:revive
|
2020-06-08 03:29:51 +03:00
|
|
|
const (
|
|
|
|
EXT_FILL = 0x0
|
|
|
|
EXT_FILL_DATA = 0x1
|
|
|
|
EXT_DATA_ELEMENT = 0x2
|
|
|
|
EXT_DYNAMIC_RANGE = 0xb
|
|
|
|
EXT_SBR_DATA = 0xd
|
|
|
|
EXT_SBR_DATA_CRC = 0xe
|
|
|
|
)
|
|
|
|
|
2021-11-05 17:04:26 +03:00
|
|
|
var extensionPayloadIDNames = decode.UToStr{
|
2020-06-08 03:29:51 +03:00
|
|
|
EXT_FILL: "EXT_FILL",
|
|
|
|
EXT_FILL_DATA: "EXT_FILL_DATA",
|
|
|
|
EXT_DATA_ELEMENT: "EXT_DATA_ELEMENT",
|
|
|
|
EXT_DYNAMIC_RANGE: "EXT_DYNAMIC_RANGE",
|
|
|
|
EXT_SBR_DATA: "EXT_SBR_DATA",
|
|
|
|
EXT_SBR_DATA_CRC: "EXT_SBR_DATA_CRC",
|
|
|
|
}
|
|
|
|
|
2021-09-27 12:01:14 +03:00
|
|
|
//nolint:revive
|
2020-06-08 03:29:51 +03:00
|
|
|
const (
|
|
|
|
ONLY_LONG_SEQUENCE = 0x0
|
|
|
|
LONG_START_SEQUENCE = 0x1
|
|
|
|
EIGHT_SHORT_SEQUENCE = 0x2
|
|
|
|
LONG_STOP_SEQUENCE = 0x3
|
|
|
|
)
|
|
|
|
|
2021-11-05 17:04:26 +03:00
|
|
|
var windowSequenceNames = decode.UToStr{
|
2020-06-08 03:29:51 +03:00
|
|
|
ONLY_LONG_SEQUENCE: "ONLY_LONG_SEQUENCE",
|
|
|
|
LONG_START_SEQUENCE: "LONG_START_SEQUENCE",
|
|
|
|
EIGHT_SHORT_SEQUENCE: "EIGHT_SHORT_SEQUENCE",
|
|
|
|
LONG_STOP_SEQUENCE: "LONG_STOP_SEQUENCE",
|
|
|
|
}
|
|
|
|
|
|
|
|
var windowSequenceNumWindows = map[int]int{
|
|
|
|
ONLY_LONG_SEQUENCE: 1,
|
|
|
|
LONG_START_SEQUENCE: 1,
|
|
|
|
EIGHT_SHORT_SEQUENCE: 8,
|
|
|
|
LONG_STOP_SEQUENCE: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
func aacLTPData(d *decode.D, objectType int, windowSequence int) {
|
|
|
|
switch objectType {
|
|
|
|
case format.MPEGAudioObjectTypeER_AAC_LD:
|
|
|
|
// TODO:
|
|
|
|
default:
|
|
|
|
d.FieldU11("ltp_lag")
|
|
|
|
d.FieldU3("ltp_coef")
|
|
|
|
|
|
|
|
_ = windowSequenceNumWindows[windowSequence]
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func aacICSInfo(d *decode.D, objectType int) {
|
|
|
|
d.FieldU1("ics_reserved_bit")
|
2021-11-17 18:13:10 +03:00
|
|
|
windowSequence := d.FieldU2("window_sequence", d.MapUToStrSym(windowSequenceNames))
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldU1("window_shape")
|
|
|
|
switch windowSequence {
|
|
|
|
case EIGHT_SHORT_SEQUENCE:
|
|
|
|
d.FieldU4("max_sfb")
|
|
|
|
d.FieldU7("scale_factor_grouping")
|
|
|
|
default:
|
|
|
|
maxSFB := d.FieldU6("max_sfb")
|
|
|
|
predictorDataPresent := d.FieldBool("predictor_data_present")
|
|
|
|
if predictorDataPresent {
|
|
|
|
switch objectType {
|
|
|
|
case format.MPEGAudioObjectTypeMain: // 1
|
|
|
|
predictorReset := d.FieldBool("predictor_reset")
|
|
|
|
if predictorReset {
|
|
|
|
d.FieldU5("predictor_reset_group_number")
|
|
|
|
}
|
|
|
|
d.FieldU5("predictor_reset_group_number")
|
|
|
|
// TODO: min(max_sfb, PRED_SFB_MAX)
|
|
|
|
// TODO: array?
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldRawLen("prediction_used", int64(maxSFB))
|
2020-06-08 03:29:51 +03:00
|
|
|
default:
|
|
|
|
ltpDataPresent := d.FieldBool("ltp_data_present")
|
|
|
|
if ltpDataPresent {
|
|
|
|
aacLTPData(d, objectType, int(windowSequence))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ;
|
|
|
|
// if (window_sequence == EIGHT_SHORT_SEQUENCE) {
|
|
|
|
// max_sfb; scale_factor_grouping;
|
|
|
|
// } }
|
|
|
|
// else {
|
|
|
|
// ltp_data_present;
|
|
|
|
// if (ltp_data_present) {
|
|
|
|
// ltp_data(); }
|
|
|
|
// if (common_window) {
|
|
|
|
// ltp_data_present;
|
|
|
|
// LICENSED TO MECON Limited. - RANCHI/BANGALORE,
|
|
|
|
// FOR INTERNAL USE AT THIS LOCATION ONLY, SUPPLIED BY BOOK SUPPLY BUREAU.
|
|
|
|
// if (ltp_data_present) {
|
|
|
|
// ltp_data(); }
|
|
|
|
// } }
|
|
|
|
// } }
|
|
|
|
// }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func aacIndividualChannelStream(d *decode.D, objectType int, commonWindow bool, scaleFlag bool) {
|
|
|
|
d.FieldU8("global_gain")
|
|
|
|
if !commonWindow && !scaleFlag {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("ics_info", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
aacICSInfo(d, objectType)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func aacSingleChannelElement(d *decode.D, objectType int) {
|
|
|
|
d.FieldU4("element_instance_tag")
|
|
|
|
aacIndividualChannelStream(d, objectType, false, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func aacProgramConfigElement(d *decode.D, ascStartPos int64) {
|
|
|
|
d.FieldU4("element_instance_tag")
|
|
|
|
d.FieldU2("object_type")
|
|
|
|
d.FieldU4("sampling_frequency_index")
|
|
|
|
numFrontChannelElements := d.FieldU4("num_front_channel_elements")
|
|
|
|
numSideChannelElements := d.FieldU4("num_side_channel_elements")
|
|
|
|
numBackChannelElements := d.FieldU4("num_back_channel_elements")
|
|
|
|
numLfeChannelElements := d.FieldU2("num_lfe_channel_elements")
|
|
|
|
numAssocDataElements := d.FieldU3("num_assoc_data_elements")
|
|
|
|
numValidCcElements := d.FieldU4("num_valid_cc_elements")
|
|
|
|
monoMixdownPresent := d.FieldBool("mono_mixdown_present")
|
|
|
|
if monoMixdownPresent {
|
|
|
|
d.FieldU4("mono_mixdown_element_number")
|
|
|
|
}
|
|
|
|
stereoMixdownPresent := d.FieldBool("stereo_mixdown_present")
|
|
|
|
if stereoMixdownPresent {
|
|
|
|
d.FieldU4("stereo_mixdown_element_number")
|
|
|
|
}
|
|
|
|
matrixMixdownIdxPresent := d.FieldBool("matrix_mixdown_idx_present")
|
|
|
|
if matrixMixdownIdxPresent {
|
|
|
|
d.FieldU2("matrix_mixdown_idx")
|
|
|
|
d.FieldBool("pseudo_surround_enable")
|
|
|
|
}
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("front_channel_elements", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
for i := uint64(0); i < numFrontChannelElements; i++ {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("front_channel_element", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldBool("is_cpe")
|
|
|
|
d.FieldU4("tag_select")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("side_channel_elements", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
for i := uint64(0); i < numSideChannelElements; i++ {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("side_channel_element", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldBool("is_cpe")
|
|
|
|
d.FieldU4("tag_select")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("back_channel_elements", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
for i := uint64(0); i < numBackChannelElements; i++ {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("back_channel_element", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldBool("is_cpe")
|
|
|
|
d.FieldU4("tag_select")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("lfe_channel_elements", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
for i := uint64(0); i < numLfeChannelElements; i++ {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("lfe_channel_element", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldU4("tag_select")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("assoc_data_elements", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
for i := uint64(0); i < numAssocDataElements; i++ {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("assoc_data_element", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldU4("tag_select")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("valid_cc_elements", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
for i := uint64(0); i < numValidCcElements; i++ {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("valid_cc_element", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldU1("cc_element_is_ind_sw")
|
|
|
|
d.FieldU4("valid_cc_element_tag_select")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
byteAlignBits := (8 - ((d.Pos() + ascStartPos) & 0x7)) & 0x7
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldRawLen("byte_alignment", byteAlignBits)
|
2020-06-08 03:29:51 +03:00
|
|
|
commentFieldBytes := d.FieldU8("comment_field_bytes")
|
|
|
|
d.FieldUTF8("comment_field", int(commentFieldBytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
func aacFillElement(d *decode.D) {
|
|
|
|
var cnt uint64
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("cnt", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
count := d.FieldU4("count")
|
|
|
|
cnt = count
|
|
|
|
if cnt == 15 {
|
|
|
|
escCount := d.FieldU8("esc_count")
|
|
|
|
cnt += escCount - 1
|
|
|
|
}
|
|
|
|
})
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldValueU("payload_length", cnt)
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("extension_payload", func(d *decode.D) {
|
|
|
|
d.LenFn(int64(cnt)*8, func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-11-17 18:13:10 +03:00
|
|
|
extensionType := d.FieldU4("extension_type", d.MapUToStrSym(extensionPayloadIDNames))
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
// d.FieldU("align4", 2)
|
|
|
|
|
|
|
|
switch extensionType {
|
|
|
|
case EXT_FILL:
|
|
|
|
d.FieldU4("fill_nibble")
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldRawLen("fill_byte", 8*(int64(cnt)-1))
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func aacDecode(d *decode.D, in interface{}) interface{} {
|
|
|
|
var objectType int
|
|
|
|
if afi, ok := in.(format.AACFrameIn); ok {
|
|
|
|
objectType = afi.ObjectType
|
|
|
|
}
|
|
|
|
// objectType = 2
|
|
|
|
|
|
|
|
// TODO: seems tricky to know length of blocks
|
|
|
|
// TODO: currently break when length is unknown
|
2021-09-16 17:26:31 +03:00
|
|
|
seenTerm := false
|
|
|
|
for !seenTerm {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("element", func(d *decode.D) {
|
2021-11-17 18:13:10 +03:00
|
|
|
se := d.FieldU3("syntax_element", d.MapUToStrSym(syntaxElementNames))
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-09-16 17:26:31 +03:00
|
|
|
switch se {
|
|
|
|
case FIL:
|
|
|
|
aacFillElement(d)
|
|
|
|
|
|
|
|
case SCE:
|
|
|
|
aacSingleChannelElement(d, objectType)
|
|
|
|
seenTerm = true
|
|
|
|
|
|
|
|
case PCE:
|
|
|
|
aacProgramConfigElement(d, 0)
|
|
|
|
seenTerm = true
|
|
|
|
|
|
|
|
default:
|
|
|
|
fallthrough
|
|
|
|
case TERM:
|
|
|
|
seenTerm = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.ByteAlignBits() > 0 {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldRawLen("byte_align", int64(d.ByteAlignBits()))
|
2021-09-16 17:26:31 +03:00
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldRawLen("data", d.BitsLeft())
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|