fuzz_frontend_adts_header.c:asc_codec_parse:
  163|    758|{
  164|    758|    memset(out, 0, sizeof(*out));
  165|    758|    if (!buf || len < 2 || len > UINT32_MAX / 8) return;
  ------------------
  |  Branch (165:9): [True: 0, False: 758]
  |  Branch (165:17): [True: 0, False: 758]
  |  Branch (165:28): [True: 0, False: 758]
  ------------------
  166|       |
  167|    758|    asc_bitreader br = { buf, len * 8, 0 };
  168|       |
  169|    758|    uint32_t aot = asc_br_get(&br, 5);
  170|    758|    if (aot == 31) aot = 32 + asc_br_get(&br, 6);
  ------------------
  |  Branch (170:9): [True: 55, False: 703]
  ------------------
  171|    758|    out->object_type = (uint8_t)aot;
  172|       |
  173|    758|    out->sample_rate = asc_parse_sample_rate(&br);
  174|    758|    out->channel_config = out->num_channels = (uint8_t)asc_br_get(&br, 4);
  175|       |
  176|    758|    if (aot == 5 || aot == 29) {
  ------------------
  |  Branch (176:9): [True: 144, False: 614]
  |  Branch (176:21): [True: 85, False: 529]
  ------------------
  177|       |        /* Explicit hierarchical form: the SBR (29: SBR+PS) object wraps the
  178|       |         * core object and carries the output sampling frequency itself. */
  179|    229|        out->sbr_present = true;
  180|    229|        out->ps_present = (aot == 29);
  181|    229|        out->sbr_sample_rate = asc_parse_sample_rate(&br);
  182|    229|        uint32_t real_aot = asc_br_get(&br, 5);
  183|    229|        if (real_aot == 31) real_aot = 32 + asc_br_get(&br, 6);
  ------------------
  |  Branch (183:13): [True: 17, False: 212]
  ------------------
  184|    229|        out->object_type = (uint8_t)real_aot;
  185|    229|        aot = real_aot;
  186|    229|    }
  187|       |
  188|    758|    if (!out->sbr_present) out->sbr_sample_rate = out->sample_rate * 2;
  ------------------
  |  Branch (188:9): [True: 529, False: 229]
  ------------------
  189|       |
  190|    758|    if (aot != 2) return; /* GASpecificConfig only walked here for AAC-LC */
  ------------------
  |  Branch (190:9): [True: 128, False: 630]
  ------------------
  191|       |
  192|    630|    out->frame_length_flag = asc_br_get(&br, 1) != 0; /* frameLengthFlag */
  193|    630|    if (asc_br_get(&br, 1)) asc_skip_core_coder_delay(&br); /* coreCoderDelay */
  ------------------
  |  Branch (193:9): [True: 297, False: 333]
  ------------------
  194|    630|    bool extension_flag = asc_br_get(&br, 1) != 0;
  195|    630|    if (out->num_channels == 0) {
  ------------------
  |  Branch (195:9): [True: 431, False: 199]
  ------------------
  196|    431|        out->num_channels = asc_parse_pce(&br, &out->pce);
  197|    431|        if (!out->num_channels) return;
  ------------------
  |  Branch (197:13): [True: 183, False: 248]
  ------------------
  198|    431|    }
  199|    447|    if (extension_flag) {
  ------------------
  |  Branch (199:9): [True: 224, False: 223]
  ------------------
  200|    224|        asc_br_get(&br, 1); /* extensionFlag3 (reserved for AOT LC) */
  201|    224|    }
  202|       |
  203|    447|    if (asc_br_remaining(&br) >= 16 && asc_br_get(&br, 11) == ASC_SYNC_EXTENSION_SBR) {
  ------------------
  |  |   27|    425|#define ASC_SYNC_EXTENSION_SBR 0x2b7u
  ------------------
  |  Branch (203:9): [True: 425, False: 22]
  |  Branch (203:40): [True: 182, False: 243]
  ------------------
  204|    182|        uint32_t ext_aot = asc_br_get(&br, 5);
  205|       |        /* sbrPresentFlag is a real bit: an encoder may append the extension
  206|       |         * to state explicitly that there is no SBR, and then
  207|       |         * no sample-rate index follows. */
  208|    182|        if (ext_aot == 5 && asc_br_get(&br, 1)) {
  ------------------
  |  Branch (208:13): [True: 174, False: 8]
  |  Branch (208:29): [True: 170, False: 4]
  ------------------
  209|    170|            out->sbr_present = true;
  210|    170|            out->sbr_sample_rate = asc_parse_sample_rate(&br);
  211|    170|            if (asc_br_remaining(&br) >= 12 && asc_br_get(&br, 11) == ASC_SYNC_EXTENSION_PS) {
  ------------------
  |  |   28|    146|#define ASC_SYNC_EXTENSION_PS  0x548u
  ------------------
  |  Branch (211:17): [True: 146, False: 24]
  |  Branch (211:48): [True: 32, False: 114]
  ------------------
  212|     32|                out->ps_present = asc_br_get(&br, 1) != 0;
  213|     32|            }
  214|    170|        }
  215|    182|    }
  216|    447|}
fuzz_frontend_adts_header.c:asc_br_get:
   76|  29.3k|{
   77|  29.3k|    uint32_t val = 0;
   78|   128k|    for (; n; n--, br->pos++) {
  ------------------
  |  Branch (78:12): [True: 98.7k, False: 29.3k]
  ------------------
   79|  98.7k|        uint32_t bit = br->pos < br->len_bits ? (br->buf[br->pos >> 3] >> (7 - (br->pos & 7))) & 1 : 0;
  ------------------
  |  Branch (79:24): [True: 98.1k, False: 628]
  ------------------
   80|  98.7k|        val = (val << 1) | bit;
   81|  98.7k|    }
   82|  29.3k|    return val;
   83|  29.3k|}
fuzz_frontend_adts_header.c:asc_parse_sample_rate:
  100|  1.15k|{
  101|  1.15k|    uint32_t idx = asc_br_get(br, 4);
  102|  1.15k|    return idx == 15 ? asc_br_get(br, 24) : asc_codec_sample_rates[idx];
  ------------------
  |  Branch (102:12): [True: 419, False: 738]
  ------------------
  103|  1.15k|}
fuzz_frontend_adts_header.c:asc_skip_core_coder_delay:
   95|    297|{
   96|    297|    if (asc_br_remaining(br) >= 15) asc_br_get(br, 14);
  ------------------
  |  Branch (96:9): [True: 287, False: 10]
  ------------------
   97|    297|}
fuzz_frontend_adts_header.c:asc_parse_pce:
  108|    431|{
  109|    431|    memset(pce, 0, sizeof(*pce));
  110|    431|    uint32_t start = br->pos;
  111|    431|    if (start > UINT16_MAX || asc_br_remaining(br) < 34) return 0;
  ------------------
  |  Branch (111:9): [True: 0, False: 431]
  |  Branch (111:31): [True: 22, False: 409]
  ------------------
  112|    409|    asc_br_get(br, 4);
  113|    409|    pce->profile = (uint8_t)asc_br_get(br, 2);
  114|    409|    pce->sr_index = (uint8_t)asc_br_get(br, 4);
  115|    409|    uint32_t front = asc_br_get(br, 4);
  116|    409|    uint32_t side = asc_br_get(br, 4);
  117|    409|    uint32_t back = asc_br_get(br, 4);
  118|    409|    uint32_t lfe = asc_br_get(br, 2);
  119|    409|    uint32_t assoc = asc_br_get(br, 3);
  120|    409|    uint32_t cc = asc_br_get(br, 4);
  121|    409|    if (asc_br_get(br, 1)) asc_br_get(br, 4);
  ------------------
  |  Branch (121:9): [True: 164, False: 245]
  ------------------
  122|    409|    if (asc_br_get(br, 1)) asc_br_get(br, 4);
  ------------------
  |  Branch (122:9): [True: 166, False: 243]
  ------------------
  123|    409|    if (asc_br_get(br, 1)) asc_br_get(br, 3);
  ------------------
  |  Branch (123:9): [True: 187, False: 222]
  ------------------
  124|    409|    uint32_t channels = lfe, elements = front + side + back;
  125|    409|    if (br->pos > br->len_bits || asc_br_remaining(br) < elements * 5 + lfe * 4 + assoc * 4 + cc * 5)
  ------------------
  |  Branch (125:9): [True: 7, False: 402]
  |  Branch (125:35): [True: 25, False: 377]
  ------------------
  126|     32|        return 0;
  127|  8.33k|    for (uint32_t i = 0; i < elements; i++) {
  ------------------
  |  Branch (127:26): [True: 7.96k, False: 377]
  ------------------
  128|  7.96k|        uint32_t pair = asc_br_get(br, 1);
  129|  7.96k|        uint8_t tag = (uint8_t)asc_br_get(br, 4);
  130|  7.96k|        channels += 1 + pair;
  131|  7.96k|        uint32_t cat = i < front ? ASC_PCE_FRONT : i < front + side ? ASC_PCE_SIDE : ASC_PCE_BACK;
  ------------------
  |  Branch (131:24): [True: 2.52k, False: 5.43k]
  |  Branch (131:52): [True: 2.52k, False: 2.91k]
  ------------------
  132|  7.96k|        if (pce->n_el < 8) pce->el[pce->n_el++] = (uint8_t)(cat << 5 | pair << 4 | tag);
  ------------------
  |  Branch (132:13): [True: 2.23k, False: 5.72k]
  ------------------
  133|  7.96k|    }
  134|    966|    for (uint32_t i = 0; i < lfe; i++) {
  ------------------
  |  Branch (134:26): [True: 589, False: 377]
  ------------------
  135|    589|        uint8_t tag = (uint8_t)asc_br_get(br, 4);
  136|    589|        if (pce->n_el < 8) pce->el[pce->n_el++] = (uint8_t)(ASC_PCE_LFE << 5 | tag);
  ------------------
  |  Branch (136:13): [True: 41, False: 548]
  ------------------
  137|    589|    }
  138|    377|    br->pos += assoc * 4 + cc * 5;
  139|    377|    pce->body_bits = (uint16_t)(br->pos - start);
  140|    377|    asc_br_get(br, (8 - (br->pos & 7)) & 7);
  141|    377|    if (asc_br_remaining(br) < 8) return 0;
  ------------------
  |  Branch (141:9): [True: 46, False: 331]
  ------------------
  142|    331|    uint32_t comment_bits = asc_br_get(br, 8) * 8;
  143|    331|    if (asc_br_remaining(br) < comment_bits) return 0;
  ------------------
  |  Branch (143:9): [True: 46, False: 285]
  ------------------
  144|    285|    br->pos += comment_bits;
  145|    285|    pce->pos = (uint16_t)start;
  146|    285|    pce->comment_bytes = (uint8_t)(comment_bits / 8);
  147|    285|    pce->channels = (uint8_t)channels;
  148|    285|    return pce->channels;
  149|    331|}
fuzz_frontend_adts_header.c:asc_br_remaining:
   86|  2.45k|{
   87|  2.45k|    return br->pos < br->len_bits ? br->len_bits - br->pos : 0;
  ------------------
  |  Branch (87:12): [True: 2.35k, False: 102]
  ------------------
   88|  2.45k|}
adts.c:asc_codec_sr_idx:
   39|  1.51k|{
   40|  15.6k|    for (int i = 0; i < 13; i++) {
  ------------------
  |  Branch (40:21): [True: 14.9k, False: 700]
  ------------------
   41|  14.9k|        if (asc_codec_sample_rates[i] == rate) return i;
  ------------------
  |  Branch (41:13): [True: 816, False: 14.0k]
  ------------------
   42|  14.9k|    }
   43|    700|    return -1;
   44|  1.51k|}

adts_header_ok:
   19|    641|{
   20|    641|    return h[0] == 0xFF && (h[1] & 0xF6) == 0xF0 && ((h[2] >> 2) & 0x0F) <= 12;
  ------------------
  |  Branch (20:12): [True: 38, False: 603]
  |  Branch (20:28): [True: 8, False: 30]
  |  Branch (20:53): [True: 6, False: 2]
  ------------------
   21|    641|}
adts_frame_length:
   24|      6|{
   25|      6|    return ((uint32_t)(h[3] & 0x03) << 11) | ((uint32_t)h[4] << 3) | ((uint32_t)(h[5] & 0xE0) >> 5);
   26|      6|}
adts_header_length:
   29|      6|{
   30|      6|    return (h[1] & 0x01) ? 7 : 9; /* protection_absent == 0 adds a CRC */
  ------------------
  |  Branch (30:12): [True: 3, False: 3]
  ------------------
   31|      6|}
adts_parse_header:
   34|      6|{
   35|      6|    out->profile = (uint8_t)((h[2] & 0xC0) >> 6);
   36|      6|    out->sr_idx = (uint8_t)((h[2] & 0x3C) >> 2);
   37|      6|    out->channel_cfg = (uint8_t)(((h[2] & 0x01) << 2) | ((h[3] & 0xC0) >> 6));
   38|      6|    out->raw_blocks = h[6] & 0x03;
   39|      6|}
adts_params_from_asc:
   42|  1.51k|{
   43|  1.51k|    int sr_idx = asc_codec_sr_idx(asc->sample_rate);
   44|  1.51k|    if (sr_idx < 0)
  ------------------
  |  Branch (44:9): [True: 700, False: 816]
  ------------------
   45|    700|        return "its core sample rate has no ADTS sampling frequency index";
   46|       |
   47|    816|    if (lenient) {
  ------------------
  |  Branch (47:9): [True: 408, False: 408]
  ------------------
   48|    408|        out->profile = (asc->object_type >= 1 && asc->object_type <= 4) ? (uint8_t)(asc->object_type - 1) : 1;
  ------------------
  |  Branch (48:25): [True: 403, False: 5]
  |  Branch (48:50): [True: 354, False: 49]
  ------------------
   49|    408|        out->channel_cfg = asc->num_channels & 7;
   50|    408|    } else {
   51|       |        /* The 2-bit profile names Main, LC, SSR and LTP; any other object type has no ADTS form. */
   52|    408|        if (asc->object_type < 1 || asc->object_type > 4)
  ------------------
  |  Branch (52:13): [True: 5, False: 403]
  |  Branch (52:37): [True: 49, False: 354]
  ------------------
   53|     54|            return "its AAC object type has no ADTS profile";
   54|       |        /* Configuration 0 puts the layout in a program config element, which the caller must then
   55|       |         * write into the frames; 8 and up are reserved. */
   56|    408|        bool pce = asc->channel_config == 0 && asc->pce.channels;
  ------------------
  |  Branch (56:20): [True: 243, False: 111]
  |  Branch (56:48): [True: 134, False: 109]
  ------------------
   57|    354|        if (!pce && (asc->num_channels < 1 || asc->num_channels > 7))
  ------------------
  |  Branch (57:13): [True: 220, False: 134]
  |  Branch (57:22): [True: 109, False: 111]
  |  Branch (57:47): [True: 41, False: 70]
  ------------------
   58|    150|            return "its channel configuration cannot be written in an ADTS header";
   59|    204|        out->profile = (uint8_t)(asc->object_type - 1);
   60|    204|        out->channel_cfg = pce ? 0 : asc->num_channels;
  ------------------
  |  Branch (60:28): [True: 134, False: 70]
  ------------------
   61|    204|    }
   62|    612|    out->sr_idx = (uint8_t)sr_idx;
   63|    612|    out->raw_blocks = 0;
   64|       |    return NULL;
   65|    816|}
adts_write_header:
   74|    618|{
   75|    618|    uint32_t len = payload_bytes + ADTS_HEADER_SIZE;
  ------------------
  |  |   28|    618|#define ADTS_HEADER_SIZE 7 /* without CRC */
  ------------------
   76|    618|    hdr[0] = 0xFF;
   77|    618|    hdr[1] = 0xF1; /* MPEG-4, layer 0, no CRC */
   78|    618|    hdr[2] = (uint8_t)((p->profile << 6) | (p->sr_idx << 2) | ((p->channel_cfg >> 2) & 1));
   79|    618|    hdr[3] = (uint8_t)(((p->channel_cfg & 3) << 6) | ((len >> 11) & 0x03));
   80|    618|    hdr[4] = (uint8_t)((len >> 3) & 0xFF);
   81|    618|    hdr[5] = (uint8_t)(((len & 7) << 5) | 0x1F); /* buffer fullness: VBR */
   82|    618|    hdr[6] = (uint8_t)(0xFC | p->raw_blocks);
   83|    618|}

LLVMFuzzerTestOneInput:
   27|    759|{
   28|    759|    if (size < 2)
  ------------------
  |  Branch (28:9): [True: 1, False: 758]
  ------------------
   29|      1|        return 0;
   30|       |
   31|       |    /* 1. ADTS header parsing and validation */
   32|    758|    if (size >= ADTS_HEADER_SIZE) {
  ------------------
  |  |   28|    758|#define ADTS_HEADER_SIZE 7 /* without CRC */
  ------------------
  |  Branch (32:9): [True: 641, False: 117]
  ------------------
   33|    641|        if (adts_header_ok(data)) {
  ------------------
  |  Branch (33:13): [True: 6, False: 635]
  ------------------
   34|      6|            uint32_t flen = adts_frame_length(data);
   35|      6|            uint32_t hlen = adts_header_length(data);
   36|      6|            (void)flen;
   37|      6|            (void)hlen;
   38|       |
   39|      6|            adts_params p;
   40|      6|            adts_parse_header(data, &p);
   41|       |
   42|      6|            uint8_t out_hdr[ADTS_HEADER_SIZE];
   43|      6|            adts_write_header(&p, 100, out_hdr);
   44|      6|        }
   45|    641|    }
   46|       |
   47|       |    /* 2. AudioSpecificConfig parsing and adts_params_from_asc */
   48|    758|    AscInfo asc;
   49|    758|    asc_codec_parse(data, (uint32_t)size, &asc);
   50|       |
   51|    758|    adts_params p_strict, p_lenient;
   52|    758|    const char *err_strict = adts_params_from_asc(&asc, false, &p_strict);
   53|    758|    const char *err_lenient = adts_params_from_asc(&asc, true, &p_lenient);
   54|       |
   55|    758|    if (!err_strict) {
  ------------------
  |  Branch (55:9): [True: 204, False: 554]
  ------------------
   56|    204|        uint8_t hdr[ADTS_HEADER_SIZE];
   57|    204|        adts_write_header(&p_strict, 200, hdr);
   58|    204|    }
   59|    758|    if (!err_lenient) {
  ------------------
  |  Branch (59:9): [True: 408, False: 350]
  ------------------
   60|    408|        uint8_t hdr[ADTS_HEADER_SIZE];
   61|    408|        adts_write_header(&p_lenient, 200, hdr);
   62|    408|    }
   63|       |
   64|    758|    return 0;
   65|    759|}

