Coverage Report

Created: 2026-12-01 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/faac/common/asc_codec.h
Line
Count
Source
1
/*
2
 * FAAC - Freeware Advanced Audio Coder
3
 * Copyright (C) 2026 Nils Schimmelmann
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 */
15
16
/* AudioSpecificConfig parser and builder for core AAC-LC with an implicit
17
 * SBR/PS sync extension. Header-only with internal linkage, so each library
18
 * and frontend compiles its own copy. */
19
20
#ifndef FAAC_ASC_CODEC_H
21
#define FAAC_ASC_CODEC_H
22
23
#include <stdint.h>
24
#include <stdbool.h>
25
#include <string.h>
26
27
0
#define ASC_SYNC_EXTENSION_SBR 0x2b7u
28
0
#define ASC_SYNC_EXTENSION_PS  0x548u
29
30
/* ISO/IEC 14496-3 Table 1.16 sampling_frequency_index. */
31
static const uint32_t asc_codec_sample_rates[16] = {
32
    96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
33
    16000, 12000, 11025, 8000, 7350, 0, 0, 0
34
};
35
36
/* Table 1.16 index of a rate, or -1 when the table has none (an ASC can
37
 * carry any rate explicitly; an index-only field such as ADTS cannot). */
38
static inline int asc_codec_sr_idx(uint32_t rate)
39
0
{
40
0
    for (int i = 0; i < 13; i++) {
41
0
        if (asc_codec_sample_rates[i] == rate) return i;
42
0
    }
43
0
    return -1;
44
0
}
Unexecuted instantiation: decoder.c:asc_codec_sr_idx
Unexecuted instantiation: bits.c:asc_codec_sr_idx
Unexecuted instantiation: asc.c:asc_codec_sr_idx
Unexecuted instantiation: huffman.c:asc_codec_sr_idx
Unexecuted instantiation: syntax.c:asc_codec_sr_idx
Unexecuted instantiation: dequant.c:asc_codec_sr_idx
Unexecuted instantiation: stereo.c:asc_codec_sr_idx
Unexecuted instantiation: tns.c:asc_codec_sr_idx
Unexecuted instantiation: imdct.c:asc_codec_sr_idx
Unexecuted instantiation: sbr.c:asc_codec_sr_idx
Unexecuted instantiation: ps.c:asc_codec_sr_idx
Unexecuted instantiation: coupling.c:asc_codec_sr_idx
Unexecuted instantiation: sbr_dec_tables.c:asc_codec_sr_idx
45
46
/* The audio elements of a PCE in list order (front, side, back, LFE), each
47
 * as category << 5 | is_cpe << 4 | tag. Only the first eight are kept. */
48
enum asc_pce_category { ASC_PCE_FRONT, ASC_PCE_SIDE, ASC_PCE_BACK, ASC_PCE_LFE };
49
typedef struct {
50
    uint8_t channels;
51
    uint8_t profile, sr_index;
52
    uint8_t n_el;
53
    uint8_t el[8];
54
    /* Where the fields sit in the parsed buffer, so the element can be
55
     * rewritten into another one: start bit, length before the byte
56
     * alignment, and comment bytes. */
57
    uint16_t pos, body_bits;
58
    uint8_t comment_bytes;
59
} AscPce;
60
61
typedef struct {
62
    uint8_t  object_type;     /* core AOT (2 = AAC-LC) */
63
    uint32_t sample_rate;     /* core sample rate, Hz */
64
    uint8_t  num_channels;
65
    uint8_t  channel_config;
66
    AscPce   pce;
67
    bool     sbr_present;
68
    uint32_t sbr_sample_rate; /* post-SBR (extension) rate, Hz; == 2*sample_rate if not explicitly signaled */
69
    bool     ps_present;
70
    bool     frame_length_flag; /* 0 = 1024 (standard), 1 = 960 (DRM / short frame) */
71
} AscInfo;
72
73
typedef struct { const uint8_t *buf; uint32_t len_bits; uint32_t pos; } asc_bitreader;
74
75
static inline uint32_t asc_br_get(asc_bitreader *br, uint32_t n)
76
279k
{
77
279k
    uint32_t val = 0;
78
1.10M
    for (; n; n--, br->pos++) {
79
824k
        uint32_t bit = br->pos < br->len_bits ? (br->buf[br->pos >> 3] >> (7 - (br->pos & 7))) & 1 : 0;
80
824k
        val = (val << 1) | bit;
81
824k
    }
82
279k
    return val;
83
279k
}
Unexecuted instantiation: decoder.c:asc_br_get
Unexecuted instantiation: bits.c:asc_br_get
asc.c:asc_br_get
Line
Count
Source
76
14.0k
{
77
14.0k
    uint32_t val = 0;
78
51.5k
    for (; n; n--, br->pos++) {
79
37.4k
        uint32_t bit = br->pos < br->len_bits ? (br->buf[br->pos >> 3] >> (7 - (br->pos & 7))) & 1 : 0;
80
37.4k
        val = (val << 1) | bit;
81
37.4k
    }
82
14.0k
    return val;
83
14.0k
}
Unexecuted instantiation: huffman.c:asc_br_get
syntax.c:asc_br_get
Line
Count
Source
76
265k
{
77
265k
    uint32_t val = 0;
78
1.05M
    for (; n; n--, br->pos++) {
79
786k
        uint32_t bit = br->pos < br->len_bits ? (br->buf[br->pos >> 3] >> (7 - (br->pos & 7))) & 1 : 0;
80
786k
        val = (val << 1) | bit;
81
786k
    }
82
265k
    return val;
83
265k
}
Unexecuted instantiation: dequant.c:asc_br_get
Unexecuted instantiation: stereo.c:asc_br_get
Unexecuted instantiation: tns.c:asc_br_get
Unexecuted instantiation: imdct.c:asc_br_get
Unexecuted instantiation: sbr.c:asc_br_get
Unexecuted instantiation: ps.c:asc_br_get
Unexecuted instantiation: coupling.c:asc_br_get
Unexecuted instantiation: sbr_dec_tables.c:asc_br_get
84
85
static inline uint32_t asc_br_remaining(const asc_bitreader *br)
86
43.2k
{
87
43.2k
    return br->pos < br->len_bits ? br->len_bits - br->pos : 0;
88
43.2k
}
Unexecuted instantiation: decoder.c:asc_br_remaining
Unexecuted instantiation: bits.c:asc_br_remaining
asc.c:asc_br_remaining
Line
Count
Source
86
2.34k
{
87
2.34k
    return br->pos < br->len_bits ? br->len_bits - br->pos : 0;
88
2.34k
}
Unexecuted instantiation: huffman.c:asc_br_remaining
syntax.c:asc_br_remaining
Line
Count
Source
86
40.9k
{
87
40.9k
    return br->pos < br->len_bits ? br->len_bits - br->pos : 0;
88
40.9k
}
Unexecuted instantiation: dequant.c:asc_br_remaining
Unexecuted instantiation: stereo.c:asc_br_remaining
Unexecuted instantiation: tns.c:asc_br_remaining
Unexecuted instantiation: imdct.c:asc_br_remaining
Unexecuted instantiation: sbr.c:asc_br_remaining
Unexecuted instantiation: ps.c:asc_br_remaining
Unexecuted instantiation: coupling.c:asc_br_remaining
Unexecuted instantiation: sbr_dec_tables.c:asc_br_remaining
89
90
/* Some muxers (Audible's among them) set dependsOnCoreCoder on a plain AAC-LC
91
 * config yet leave out the 14-bit coreCoderDelay, ending the ASC right after
92
 * extensionFlag. A conforming ASC always has room for the delay plus
93
 * extensionFlag, so the delay is read only when those 15 bits are present. */
94
static inline void asc_skip_core_coder_delay(asc_bitreader *br)
95
0
{
96
0
    if (asc_br_remaining(br) >= 15) asc_br_get(br, 14);
97
0
}
Unexecuted instantiation: decoder.c:asc_skip_core_coder_delay
Unexecuted instantiation: bits.c:asc_skip_core_coder_delay
Unexecuted instantiation: asc.c:asc_skip_core_coder_delay
Unexecuted instantiation: huffman.c:asc_skip_core_coder_delay
Unexecuted instantiation: syntax.c:asc_skip_core_coder_delay
Unexecuted instantiation: dequant.c:asc_skip_core_coder_delay
Unexecuted instantiation: stereo.c:asc_skip_core_coder_delay
Unexecuted instantiation: tns.c:asc_skip_core_coder_delay
Unexecuted instantiation: imdct.c:asc_skip_core_coder_delay
Unexecuted instantiation: sbr.c:asc_skip_core_coder_delay
Unexecuted instantiation: ps.c:asc_skip_core_coder_delay
Unexecuted instantiation: coupling.c:asc_skip_core_coder_delay
Unexecuted instantiation: sbr_dec_tables.c:asc_skip_core_coder_delay
98
99
static inline uint32_t asc_parse_sample_rate(asc_bitreader *br)
100
2.34k
{
101
2.34k
    uint32_t idx = asc_br_get(br, 4);
102
2.34k
    return idx == 15 ? asc_br_get(br, 24) : asc_codec_sample_rates[idx];
103
2.34k
}
Unexecuted instantiation: decoder.c:asc_parse_sample_rate
Unexecuted instantiation: bits.c:asc_parse_sample_rate
asc.c:asc_parse_sample_rate
Line
Count
Source
100
2.34k
{
101
2.34k
    uint32_t idx = asc_br_get(br, 4);
102
2.34k
    return idx == 15 ? asc_br_get(br, 24) : asc_codec_sample_rates[idx];
103
2.34k
}
Unexecuted instantiation: huffman.c:asc_parse_sample_rate
Unexecuted instantiation: syntax.c:asc_parse_sample_rate
Unexecuted instantiation: dequant.c:asc_parse_sample_rate
Unexecuted instantiation: stereo.c:asc_parse_sample_rate
Unexecuted instantiation: tns.c:asc_parse_sample_rate
Unexecuted instantiation: imdct.c:asc_parse_sample_rate
Unexecuted instantiation: sbr.c:asc_parse_sample_rate
Unexecuted instantiation: ps.c:asc_parse_sample_rate
Unexecuted instantiation: coupling.c:asc_parse_sample_rate
Unexecuted instantiation: sbr_dec_tables.c:asc_parse_sample_rate
104
105
/* PCE lists carry element tags rather than PCM positions. Count only audio
106
 * elements, but skip every optional field so SBR starts at the correct bit. */
107
static inline uint8_t asc_parse_pce(asc_bitreader *br, AscPce *pce)
108
11.1k
{
109
11.1k
    memset(pce, 0, sizeof(*pce));
110
11.1k
    uint32_t start = br->pos;
111
11.1k
    if (start > UINT16_MAX || asc_br_remaining(br) < 34) return 0;
112
10.7k
    asc_br_get(br, 4);
113
10.7k
    pce->profile = (uint8_t)asc_br_get(br, 2);
114
10.7k
    pce->sr_index = (uint8_t)asc_br_get(br, 4);
115
10.7k
    uint32_t front = asc_br_get(br, 4);
116
10.7k
    uint32_t side = asc_br_get(br, 4);
117
10.7k
    uint32_t back = asc_br_get(br, 4);
118
10.7k
    uint32_t lfe = asc_br_get(br, 2);
119
10.7k
    uint32_t assoc = asc_br_get(br, 3);
120
10.7k
    uint32_t cc = asc_br_get(br, 4);
121
10.7k
    if (asc_br_get(br, 1)) asc_br_get(br, 4);
122
10.7k
    if (asc_br_get(br, 1)) asc_br_get(br, 4);
123
10.7k
    if (asc_br_get(br, 1)) asc_br_get(br, 3);
124
10.7k
    uint32_t channels = lfe, elements = front + side + back;
125
10.7k
    if (br->pos > br->len_bits || asc_br_remaining(br) < elements * 5 + lfe * 4 + assoc * 4 + cc * 5)
126
979
        return 0;
127
61.3k
    for (uint32_t i = 0; i < elements; i++) {
128
51.4k
        uint32_t pair = asc_br_get(br, 1);
129
51.4k
        uint8_t tag = (uint8_t)asc_br_get(br, 4);
130
51.4k
        channels += 1 + pair;
131
51.4k
        uint32_t cat = i < front ? ASC_PCE_FRONT : i < front + side ? ASC_PCE_SIDE : ASC_PCE_BACK;
132
51.4k
        if (pce->n_el < 8) pce->el[pce->n_el++] = (uint8_t)(cat << 5 | pair << 4 | tag);
133
51.4k
    }
134
19.0k
    for (uint32_t i = 0; i < lfe; i++) {
135
9.24k
        uint8_t tag = (uint8_t)asc_br_get(br, 4);
136
9.24k
        if (pce->n_el < 8) pce->el[pce->n_el++] = (uint8_t)(ASC_PCE_LFE << 5 | tag);
137
9.24k
    }
138
9.80k
    br->pos += assoc * 4 + cc * 5;
139
9.80k
    pce->body_bits = (uint16_t)(br->pos - start);
140
9.80k
    asc_br_get(br, (8 - (br->pos & 7)) & 7);
141
9.80k
    if (asc_br_remaining(br) < 8) return 0;
142
9.50k
    uint32_t comment_bits = asc_br_get(br, 8) * 8;
143
9.50k
    if (asc_br_remaining(br) < comment_bits) return 0;
144
8.41k
    br->pos += comment_bits;
145
8.41k
    pce->pos = (uint16_t)start;
146
8.41k
    pce->comment_bytes = (uint8_t)(comment_bits / 8);
147
8.41k
    pce->channels = (uint8_t)channels;
148
8.41k
    return pce->channels;
149
9.50k
}
Unexecuted instantiation: decoder.c:asc_parse_pce
Unexecuted instantiation: bits.c:asc_parse_pce
Unexecuted instantiation: asc.c:asc_parse_pce
Unexecuted instantiation: huffman.c:asc_parse_pce
syntax.c:asc_parse_pce
Line
Count
Source
108
11.1k
{
109
11.1k
    memset(pce, 0, sizeof(*pce));
110
11.1k
    uint32_t start = br->pos;
111
11.1k
    if (start > UINT16_MAX || asc_br_remaining(br) < 34) return 0;
112
10.7k
    asc_br_get(br, 4);
113
10.7k
    pce->profile = (uint8_t)asc_br_get(br, 2);
114
10.7k
    pce->sr_index = (uint8_t)asc_br_get(br, 4);
115
10.7k
    uint32_t front = asc_br_get(br, 4);
116
10.7k
    uint32_t side = asc_br_get(br, 4);
117
10.7k
    uint32_t back = asc_br_get(br, 4);
118
10.7k
    uint32_t lfe = asc_br_get(br, 2);
119
10.7k
    uint32_t assoc = asc_br_get(br, 3);
120
10.7k
    uint32_t cc = asc_br_get(br, 4);
121
10.7k
    if (asc_br_get(br, 1)) asc_br_get(br, 4);
122
10.7k
    if (asc_br_get(br, 1)) asc_br_get(br, 4);
123
10.7k
    if (asc_br_get(br, 1)) asc_br_get(br, 3);
124
10.7k
    uint32_t channels = lfe, elements = front + side + back;
125
10.7k
    if (br->pos > br->len_bits || asc_br_remaining(br) < elements * 5 + lfe * 4 + assoc * 4 + cc * 5)
126
979
        return 0;
127
61.3k
    for (uint32_t i = 0; i < elements; i++) {
128
51.4k
        uint32_t pair = asc_br_get(br, 1);
129
51.4k
        uint8_t tag = (uint8_t)asc_br_get(br, 4);
130
51.4k
        channels += 1 + pair;
131
51.4k
        uint32_t cat = i < front ? ASC_PCE_FRONT : i < front + side ? ASC_PCE_SIDE : ASC_PCE_BACK;
132
51.4k
        if (pce->n_el < 8) pce->el[pce->n_el++] = (uint8_t)(cat << 5 | pair << 4 | tag);
133
51.4k
    }
134
19.0k
    for (uint32_t i = 0; i < lfe; i++) {
135
9.24k
        uint8_t tag = (uint8_t)asc_br_get(br, 4);
136
9.24k
        if (pce->n_el < 8) pce->el[pce->n_el++] = (uint8_t)(ASC_PCE_LFE << 5 | tag);
137
9.24k
    }
138
9.80k
    br->pos += assoc * 4 + cc * 5;
139
9.80k
    pce->body_bits = (uint16_t)(br->pos - start);
140
9.80k
    asc_br_get(br, (8 - (br->pos & 7)) & 7);
141
9.80k
    if (asc_br_remaining(br) < 8) return 0;
142
9.50k
    uint32_t comment_bits = asc_br_get(br, 8) * 8;
143
9.50k
    if (asc_br_remaining(br) < comment_bits) return 0;
144
8.41k
    br->pos += comment_bits;
145
8.41k
    pce->pos = (uint16_t)start;
146
8.41k
    pce->comment_bytes = (uint8_t)(comment_bits / 8);
147
8.41k
    pce->channels = (uint8_t)channels;
148
8.41k
    return pce->channels;
149
9.50k
}
Unexecuted instantiation: dequant.c:asc_parse_pce
Unexecuted instantiation: stereo.c:asc_parse_pce
Unexecuted instantiation: tns.c:asc_parse_pce
Unexecuted instantiation: imdct.c:asc_parse_pce
Unexecuted instantiation: sbr.c:asc_parse_pce
Unexecuted instantiation: ps.c:asc_parse_pce
Unexecuted instantiation: coupling.c:asc_parse_pce
Unexecuted instantiation: sbr_dec_tables.c:asc_parse_pce
150
151
/*
152
 * Parses aot/sample_rate/channels, then either the "explicit" nested-AOT
153
 * HE-AAC form (top-level AOT==5) or, for a plain-LC ASC, walks
154
 * GASpecificConfig (frameLengthFlag/dependsOnCoreCoder[/coreCoderDelay]/
155
 * extensionFlag[/extensionFlag3]) before scanning for the "implicit"
156
 * trailing 0x2b7/0x548 sync-extension -- the form real MP4/M4A muxers
157
 * (including this project's own) actually write. asc_len is in bytes.
158
 *
159
 * PCE audio lists supply the channel count when channelConfiguration is zero;
160
 * optional fields and comments are skipped before the sync-extension scan.
161
 */
162
static inline void asc_codec_parse(const uint8_t *buf, uint32_t len, AscInfo *out)
163
0
{
164
0
    memset(out, 0, sizeof(*out));
165
0
    if (!buf || len < 2 || len > UINT32_MAX / 8) return;
166
0
167
0
    asc_bitreader br = { buf, len * 8, 0 };
168
0
169
0
    uint32_t aot = asc_br_get(&br, 5);
170
0
    if (aot == 31) aot = 32 + asc_br_get(&br, 6);
171
0
    out->object_type = (uint8_t)aot;
172
0
173
0
    out->sample_rate = asc_parse_sample_rate(&br);
174
0
    out->channel_config = out->num_channels = (uint8_t)asc_br_get(&br, 4);
175
0
176
0
    if (aot == 5 || aot == 29) {
177
0
        /* Explicit hierarchical form: the SBR (29: SBR+PS) object wraps the
178
0
         * core object and carries the output sampling frequency itself. */
179
0
        out->sbr_present = true;
180
0
        out->ps_present = (aot == 29);
181
0
        out->sbr_sample_rate = asc_parse_sample_rate(&br);
182
0
        uint32_t real_aot = asc_br_get(&br, 5);
183
0
        if (real_aot == 31) real_aot = 32 + asc_br_get(&br, 6);
184
0
        out->object_type = (uint8_t)real_aot;
185
0
        aot = real_aot;
186
0
    }
187
0
188
0
    if (!out->sbr_present) out->sbr_sample_rate = out->sample_rate * 2;
189
0
190
0
    if (aot != 2) return; /* GASpecificConfig only walked here for AAC-LC */
191
0
192
0
    out->frame_length_flag = asc_br_get(&br, 1) != 0; /* frameLengthFlag */
193
0
    if (asc_br_get(&br, 1)) asc_skip_core_coder_delay(&br); /* coreCoderDelay */
194
0
    bool extension_flag = asc_br_get(&br, 1) != 0;
195
0
    if (out->num_channels == 0) {
196
0
        out->num_channels = asc_parse_pce(&br, &out->pce);
197
0
        if (!out->num_channels) return;
198
0
    }
199
0
    if (extension_flag) {
200
0
        asc_br_get(&br, 1); /* extensionFlag3 (reserved for AOT LC) */
201
0
    }
202
0
203
0
    if (asc_br_remaining(&br) >= 16 && asc_br_get(&br, 11) == ASC_SYNC_EXTENSION_SBR) {
204
0
        uint32_t ext_aot = asc_br_get(&br, 5);
205
0
        /* sbrPresentFlag is a real bit: an encoder may append the extension
206
0
         * to state explicitly that there is no SBR, and then
207
0
         * no sample-rate index follows. */
208
0
        if (ext_aot == 5 && asc_br_get(&br, 1)) {
209
0
            out->sbr_present = true;
210
0
            out->sbr_sample_rate = asc_parse_sample_rate(&br);
211
0
            if (asc_br_remaining(&br) >= 12 && asc_br_get(&br, 11) == ASC_SYNC_EXTENSION_PS) {
212
0
                out->ps_present = asc_br_get(&br, 1) != 0;
213
0
            }
214
0
        }
215
0
    }
216
0
}
Unexecuted instantiation: decoder.c:asc_codec_parse
Unexecuted instantiation: bits.c:asc_codec_parse
Unexecuted instantiation: asc.c:asc_codec_parse
Unexecuted instantiation: huffman.c:asc_codec_parse
Unexecuted instantiation: syntax.c:asc_codec_parse
Unexecuted instantiation: dequant.c:asc_codec_parse
Unexecuted instantiation: stereo.c:asc_codec_parse
Unexecuted instantiation: tns.c:asc_codec_parse
Unexecuted instantiation: imdct.c:asc_codec_parse
Unexecuted instantiation: sbr.c:asc_codec_parse
Unexecuted instantiation: ps.c:asc_codec_parse
Unexecuted instantiation: coupling.c:asc_codec_parse
Unexecuted instantiation: sbr_dec_tables.c:asc_codec_parse
217
218
typedef struct { uint8_t *buf; uint32_t cap_bits; uint32_t pos; } asc_bitwriter;
219
220
static inline void asc_bw_put(asc_bitwriter *bw, uint32_t val, uint32_t n)
221
0
{
222
0
    /* Whole byte-sized chunks rather than bit-at-a-time: the constant widths
223
0
     * at each call site made the compiler unroll a per-bit loop into
224
0
     * roughly 3 KB of straight-line code. */
225
0
    while (n > 0) {
226
0
        uint32_t byte_idx = bw->pos >> 3;
227
0
        uint32_t room = 8 - (bw->pos & 7);
228
0
        uint32_t take = n < room ? n : room;
229
0
        if ((byte_idx << 3) >= bw->cap_bits) {
230
0
            bw->pos += n;
231
0
            return;
232
0
        }
233
0
        uint32_t chunk = (val >> (n - take)) & ((1u << take) - 1);
234
0
        bw->buf[byte_idx] |= (uint8_t)(chunk << (room - take));
235
0
        bw->pos += take;
236
0
        n -= take;
237
0
    }
238
0
}
Unexecuted instantiation: decoder.c:asc_bw_put
Unexecuted instantiation: bits.c:asc_bw_put
Unexecuted instantiation: asc.c:asc_bw_put
Unexecuted instantiation: huffman.c:asc_bw_put
Unexecuted instantiation: syntax.c:asc_bw_put
Unexecuted instantiation: dequant.c:asc_bw_put
Unexecuted instantiation: stereo.c:asc_bw_put
Unexecuted instantiation: tns.c:asc_bw_put
Unexecuted instantiation: imdct.c:asc_bw_put
Unexecuted instantiation: sbr.c:asc_bw_put
Unexecuted instantiation: ps.c:asc_bw_put
Unexecuted instantiation: coupling.c:asc_bw_put
Unexecuted instantiation: sbr_dec_tables.c:asc_bw_put
239
240
/* Copies a parsed PCE's fields and comment from src, the buffer it was parsed
241
 * from. The byte alignment inside the element is relative to the start of the
242
 * ASC or of the raw data block, which differ, so it is rewritten for bw. */
243
static inline void asc_pce_write(asc_bitwriter *bw, const uint8_t *src, const AscPce *pce)
244
0
{
245
0
    asc_bitreader br = { src, UINT32_MAX, pce->pos };
246
0
    for (uint32_t i = 0; i < pce->body_bits; i++)
247
0
        asc_bw_put(bw, asc_br_get(&br, 1), 1);
248
0
    asc_bw_put(bw, 0, (8 - (bw->pos & 7)) & 7);
249
0
    asc_bw_put(bw, pce->comment_bytes, 8);
250
0
    br.pos = ((pce->pos + pce->body_bits + 7) & ~7u) + 8;
251
0
    for (uint32_t i = 0; i < pce->comment_bytes; i++)
252
0
        asc_bw_put(bw, asc_br_get(&br, 8), 8);
253
0
}
Unexecuted instantiation: decoder.c:asc_pce_write
Unexecuted instantiation: bits.c:asc_pce_write
Unexecuted instantiation: asc.c:asc_pce_write
Unexecuted instantiation: huffman.c:asc_pce_write
Unexecuted instantiation: syntax.c:asc_pce_write
Unexecuted instantiation: dequant.c:asc_pce_write
Unexecuted instantiation: stereo.c:asc_pce_write
Unexecuted instantiation: tns.c:asc_pce_write
Unexecuted instantiation: imdct.c:asc_pce_write
Unexecuted instantiation: sbr.c:asc_pce_write
Unexecuted instantiation: ps.c:asc_pce_write
Unexecuted instantiation: coupling.c:asc_pce_write
Unexecuted instantiation: sbr_dec_tables.c:asc_pce_write
254
255
/* The PCE of an ASC as the element a raw data block opens with: element id,
256
 * fields, alignment and comment. Returns its bytes, or 0 if out_cap is short. */
257
static inline uint32_t asc_pce_element(const uint8_t *asc, const AscPce *pce, uint8_t *out, uint32_t out_cap)
258
0
{
259
0
    uint32_t bytes = ((3 + pce->body_bits + 7) >> 3) + 1 + pce->comment_bytes;
260
0
    if (!pce->channels || out_cap < bytes) return 0;
261
0
    memset(out, 0, bytes);
262
0
    asc_bitwriter bw = { out, bytes * 8, 0 };
263
0
    asc_bw_put(&bw, 5, 3); /* ID_PCE */
264
0
    asc_pce_write(&bw, asc, pce);
265
0
    return bytes;
266
0
}
Unexecuted instantiation: decoder.c:asc_pce_element
Unexecuted instantiation: bits.c:asc_pce_element
Unexecuted instantiation: asc.c:asc_pce_element
Unexecuted instantiation: huffman.c:asc_pce_element
Unexecuted instantiation: syntax.c:asc_pce_element
Unexecuted instantiation: dequant.c:asc_pce_element
Unexecuted instantiation: stereo.c:asc_pce_element
Unexecuted instantiation: tns.c:asc_pce_element
Unexecuted instantiation: imdct.c:asc_pce_element
Unexecuted instantiation: sbr.c:asc_pce_element
Unexecuted instantiation: ps.c:asc_pce_element
Unexecuted instantiation: coupling.c:asc_pce_element
Unexecuted instantiation: sbr_dec_tables.c:asc_pce_element
267
268
typedef struct {
269
    uint8_t object_type; /* core AOT, e.g. 2 = AAC-LC */
270
    uint8_t sr_idx;      /* Table 1.16 index for the core rate */
271
    uint8_t channels;
272
    bool    sbr_present;
273
    uint8_t sbr_sr_idx;  /* Table 1.16 index for the post-SBR (output) rate; ignored if !sbr_present */
274
    bool    ps_signaled; /* emit the PS sync-extension block at all (only meaningful if sbr_present) */
275
    bool    ps_present;  /* psPresentFlag value inside that block, if ps_signaled */
276
    bool    hierarchical; /* explicit hierarchical form (AOT 5/29 wrapping the core) instead of the sync-extension */
277
    const AscPce *pce;    /* with channels == 0: the layout, parsed from pce_src */
278
    const uint8_t *pce_src;
279
} AscBuildInfo;
280
281
/*
282
 * Builds a core-LC ASC, with an implicit SBR sync-extension (and, if
283
 * ps_signaled, a nested PS sync-extension) appended when sbr_present is
284
 * set. Returns the number of bytes written, or 0 if out_cap is too small.
285
 */
286
static inline uint32_t asc_codec_build(const AscBuildInfo *info, uint8_t *out, uint32_t out_cap)
287
0
{
288
0
    if (!info || !out || out_cap < 2) return 0;
289
0
290
0
    memset(out, 0, out_cap);
291
0
    asc_bitwriter bw = { out, out_cap * 8, 0 };
292
0
293
0
    if (info->sbr_present && info->hierarchical) {
294
0
        if (out_cap < 4) return 0;
295
0
        asc_bw_put(&bw, info->ps_signaled && info->ps_present ? 29 : 5, 5);
296
0
        asc_bw_put(&bw, info->sr_idx, 4);
297
0
        asc_bw_put(&bw, info->channels & 0x0F, 4);
298
0
        asc_bw_put(&bw, info->sbr_sr_idx, 4);
299
0
    }
300
0
301
0
    uint8_t obj = info->object_type;
302
0
    if (obj >= 32) {
303
0
        /* AOT-escape form, mirroring asc_codec_parse()'s aot==31 read above. */
304
0
        asc_bw_put(&bw, 31, 5);
305
0
        asc_bw_put(&bw, obj - 32, 6);
306
0
    } else {
307
0
        asc_bw_put(&bw, obj, 5);
308
0
    }
309
0
    if (!(info->sbr_present && info->hierarchical)) {
310
0
        asc_bw_put(&bw, info->sr_idx, 4);
311
0
        asc_bw_put(&bw, info->channels & 0x0F, 4);
312
0
    }
313
0
    asc_bw_put(&bw, 0, 3); /* frameLengthFlag, dependsOnCoreCoder, extensionFlag */
314
0
    if (info->pce) asc_pce_write(&bw, info->pce_src, info->pce);
315
0
316
0
    if (info->sbr_present && !info->hierarchical) {
317
0
        asc_bw_put(&bw, ASC_SYNC_EXTENSION_SBR, 11);
318
0
        asc_bw_put(&bw, 5, 5); /* extensionAudioObjectType = SBR */
319
0
        asc_bw_put(&bw, 1, 1); /* sbrPresentFlag */
320
0
        asc_bw_put(&bw, info->sbr_sr_idx, 4);
321
0
        if (info->ps_signaled) {
322
0
            asc_bw_put(&bw, ASC_SYNC_EXTENSION_PS, 11);
323
0
            asc_bw_put(&bw, info->ps_present ? 1 : 0, 1);
324
0
        }
325
0
    }
326
0
327
0
    return (bw.pos + 7) / 8;
328
0
}
Unexecuted instantiation: decoder.c:asc_codec_build
Unexecuted instantiation: bits.c:asc_codec_build
Unexecuted instantiation: asc.c:asc_codec_build
Unexecuted instantiation: huffman.c:asc_codec_build
Unexecuted instantiation: syntax.c:asc_codec_build
Unexecuted instantiation: dequant.c:asc_codec_build
Unexecuted instantiation: stereo.c:asc_codec_build
Unexecuted instantiation: tns.c:asc_codec_build
Unexecuted instantiation: imdct.c:asc_codec_build
Unexecuted instantiation: sbr.c:asc_codec_build
Unexecuted instantiation: ps.c:asc_codec_build
Unexecuted instantiation: coupling.c:asc_codec_build
Unexecuted instantiation: sbr_dec_tables.c:asc_codec_build
329
330
#endif /* FAAC_ASC_CODEC_H */