Coverage Report

Created: 2026-12-01 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/external/faac/libfaad/asc.c
Line
Count
Source
1
/*
2
 * FAAD - Freeware Advanced Audio Decoder
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
#include "faad_internal.h"
17
#include "asc_codec.h"
18
19
/* SFB tables cover only the first 12 rates; any other rate (7350 Hz
20
 * included) has no layout to decode with, so it is -1 and never guessed. */
21
int get_sr_index(uint32_t sample_rate)
22
39.0k
{
23
194k
    for (int i = 0; i < 12; i++) {
24
194k
        if (asc_codec_sample_rates[i] == sample_rate) {
25
38.9k
            return i;
26
38.9k
        }
27
194k
    }
28
121
    return -1;
29
39.0k
}
30
31
/* The shared ASC helper accepts zero-filled truncated syntax for container
32
 * probing; decoder initialization must not, so it is parsed here instead. */
33
static faad_status parse_asc(const uint8_t *buf, uint32_t len, AudioSpecificConfig *asc, bool *frame_length)
34
6.71k
{
35
6.71k
    if (!buf || len < 2 || len > UINT32_MAX / 8) return FAAD_ERR_INVALID_ARGUMENT;
36
6.71k
    memset(asc, 0, sizeof(*asc));
37
6.71k
    asc_bitreader br = { buf, len * 8, 0 };
38
6.71k
    uint32_t aot = asc_br_get(&br, 5);
39
6.71k
    if (aot == 31) aot = 32 + asc_br_get(&br, 6);
40
6.71k
    uint32_t rate = asc_parse_sample_rate(&br);
41
6.71k
    uint32_t channels = asc_br_get(&br, 4);
42
6.71k
    uint32_t output_rate = rate * 2;
43
6.71k
    bool explicit_sbr = aot == 5 || aot == 29;
44
6.71k
    if (explicit_sbr) {
45
463
        output_rate = asc_parse_sample_rate(&br);
46
463
        asc->is_sbr = true;
47
463
        asc->is_ps = aot == 29;
48
463
        aot = asc_br_get(&br, 5);
49
463
        if (aot == 31) aot = 32 + asc_br_get(&br, 6);
50
463
    }
51
6.71k
    if (br.pos > br.len_bits || !rate || !output_rate || (channels > 7 && channels != 11 && channels != 12))
52
150
        return FAAD_ERR_INVALID_ARGUMENT;
53
6.56k
    if (aot != 2) return FAAD_ERR_UNSUPPORTED;
54
6.43k
    if (explicit_sbr && output_rate != 2 * rate) return FAAD_ERR_UNSUPPORTED;
55
6.35k
    *frame_length = asc_br_get(&br, 1) != 0;
56
6.35k
    if (asc_br_get(&br, 1)) asc_skip_core_coder_delay(&br);
57
6.35k
    bool extension = asc_br_get(&br, 1) != 0;
58
6.35k
    if (!channels) {
59
        /* Validate the ASC's PCE profile and sampling index; channel elements
60
         * are mapped from its tags during packet decoding. */
61
1.14k
        if (!asc_parse_pce(&br, &asc->pce)) return FAAD_ERR_INVALID_ARGUMENT;
62
843
        if (asc->pce.profile != 1 || asc->pce.sr_index >= 12) return FAAD_ERR_UNSUPPORTED;
63
        /* Per-channel state is sized for MAX_CHANNELS. */
64
578
        if (asc->pce.channels > MAX_CHANNELS) return FAAD_ERR_UNSUPPORTED;
65
578
    }
66
5.74k
    if (extension) asc_br_get(&br, 1);
67
5.74k
    if (br.pos > br.len_bits) return FAAD_ERR_INVALID_ARGUMENT;
68
5.73k
    if (asc_br_remaining(&br) >= 11 && asc_br_get(&br, 11) == ASC_SYNC_EXTENSION_SBR) {
69
446
        uint32_t ext_aot = asc_br_get(&br, 5);
70
446
        if (ext_aot == 5 && asc_br_get(&br, 1)) {
71
411
            uint32_t ext_rate = asc_parse_sample_rate(&br);
72
411
            if (!ext_rate || br.pos > br.len_bits) return FAAD_ERR_INVALID_ARGUMENT;
73
368
            if (ext_rate != rate * 2) return FAAD_ERR_UNSUPPORTED;
74
205
            asc->is_sbr = true;
75
205
            output_rate = ext_rate;
76
205
            if (asc_br_remaining(&br) >= 11 && asc_br_get(&br, 11) == ASC_SYNC_EXTENSION_PS)
77
49
                asc->is_ps = asc_br_get(&br, 1) != 0;
78
205
        }
79
446
    }
80
5.53k
    if (br.pos > br.len_bits) return FAAD_ERR_INVALID_ARGUMENT;
81
5.52k
    asc->object_type = FAAD_OBJ_LC;
82
5.52k
    asc->sample_rate = rate;
83
5.52k
    asc->num_channels = channels;
84
5.52k
    asc->sbr_sample_rate = output_rate;
85
5.52k
    return FAAD_OK;
86
5.53k
}
87
88
faad_status asc_decode(BitReader *bs, AudioSpecificConfig *asc)
89
6.71k
{
90
    /* asc_decode() is always called on a BitReader freshly bits_init()'d over
91
     * exactly the ASC bytes (see faad_decoder_open()). */
92
6.71k
    bool frame_length;
93
6.71k
    faad_status status = parse_asc(bs->buffer, bs->len, asc, &frame_length);
94
6.71k
    if (status != FAAD_OK) return status;
95
96
5.52k
    if (frame_length) {
97
3.07k
#ifndef FAAD_DISABLE_DAB
98
3.07k
        asc->frame_length_flag = true;
99
#else
100
        return FAAD_ERR_UNSUPPORTED;
101
#endif
102
3.07k
    }
103
104
5.52k
    return FAAD_OK;
105
6.71k
}
106
107
faad_status adts_decode_header(BitReader *bs, AudioSpecificConfig *asc, uint32_t *frame_length)
108
530
{
109
530
    uint32_t sync = bits_get(bs, 12);
110
530
    if (sync != 0xFFF) {
111
0
        return FAAD_ERR_SYNC_LOST;
112
0
    }
113
530
    bits_skip(bs, 1);
114
530
    bits_skip(bs, 2);
115
530
    uint32_t protection_absent = bits_get(bs, 1);
116
117
530
    uint32_t profile = bits_get(bs, 2);
118
530
    uint32_t sr_idx = bits_get(bs, 4);
119
530
    bits_skip(bs, 1);
120
530
    uint32_t channel_config = bits_get(bs, 3);
121
530
    bits_skip(bs, 4);
122
123
530
    uint32_t flen = bits_get(bs, 13);
124
530
    bits_skip(bs, 11);
125
530
    uint32_t raw_blocks = bits_get(bs, 2);
126
127
530
    if (protection_absent == 0) {
128
113
        bits_skip(bs, 16);
129
113
    }
130
131
530
    uint32_t min_hdr = (protection_absent == 0) ? 9 : 7;
132
    /* A malformed header is reported before an unsupported profile: only a
133
     * well-formed header can be trusted to describe a frame worth skipping. */
134
530
    if (sr_idx >= 12 || asc_codec_sample_rates[sr_idx] == 0 || flen < min_hdr) {
135
21
        return FAAD_ERR_DECODE_FAILED;
136
21
    }
137
138
509
    if (profile != 1 || raw_blocks != 0) {
139
33
        return FAAD_ERR_UNSUPPORTED;
140
33
    }
141
142
476
    if (asc) {
143
476
        memset(asc, 0, sizeof(*asc));
144
476
        asc->object_type = (enum faad_object_type)(profile + 1);
145
476
        asc->sample_rate = asc_codec_sample_rates[sr_idx];
146
476
        asc->num_channels = channel_config;
147
476
    }
148
149
476
    if (frame_length) {
150
476
        *frame_length = flen;
151
476
    }
152
153
476
    return FAAD_OK;
154
509
}