Coverage Report

Created: 2026-12-01 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/faac/libfaac/drc.c
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
#include "drc.h"
17
#include "frame.h"
18
#include "channels.h"
19
#include "bitstream.h"
20
#include "util.h"
21
22
int DrcGetBits(const DRCInfo *drc, BitStream *bs)
23
14.9k
{
24
14.9k
    if (!drc || !drc->enabled)
25
0
        return 0;
26
27
    /*
28
     * ISO/IEC 14496-3 ยง4.5.2.1 (dynamic_range_info) syntax and gain semantics:
29
     *   - dyn_rng_sgn (1 bit): 1 = cut / compression (negative gain in dB),
30
     *                          0 = boost (positive gain in dB).
31
     *   - dyn_rng_ctl (7 bits): magnitude in 0.25 dB steps (range 0.00 to 31.75 dB).
32
     *   - prog_ref_level (7 bits): program reference level in -0.25 dB steps (0 to -31.75 dBFS).
33
     *
34
     * The analyzer stores both values on that grid and clamps the gain to
35
     * -127..127, so the wire fields are the stored magnitudes.
36
     */
37
14.9k
    int gain_code = drc->gain_code;
38
14.9k
    int sgn = gain_code < 0 ? 1 : 0;
39
14.9k
    int ctl = sgn ? -gain_code : gain_code;
40
14.9k
    int ref_code = drc->ref_code;
41
42
    /* The fixed payload is exactly 3 bytes: extension_type (4 bits),
43
     * four presence flags (4), prog_ref_level plus reserved (8), and one
44
     * gain (8). It always fits the short fill_element form. */
45
14.9k
    const int fillBytes = 3;
46
47
14.9k
    if (bs) {
48
        /* fill_element header (7), extension_type and presence flags (8). */
49
14.9k
        PutBit(bs, ((uint32_t)((ID_FIL << 4) | fillBytes) << 8)
50
14.9k
                   | (uint32_t)((EXT_DYNAMIC_RANGE << 4) | 0x1), 15);
51
        /* prog_ref_level and its reserved bit (8), band 0 sign and gain (8). */
52
14.9k
        PutBit(bs, ((uint32_t)(ref_code << 1) << 8)
53
14.9k
                   | (uint32_t)((sgn << 7) | ctl), 16);
54
14.9k
    }
55
56
14.9k
    return 7 + fillBytes * 8;
57
14.9k
}
58
59
#if FAAC_ENCODER_DRC
60
61
int DrcInit(faacEncStruct *hEncoder)
62
2.94k
{
63
2.94k
    if (hEncoder->config.drcProfile != DRC_OFF) {
64
        /*
65
         * Note: hEncoder->sampleRate holds the halved core sample rate for HE-AAC,
66
         * but ITU-R BS.1770 K-weighting loudness measurement operates on full-rate PCM.
67
         */
68
681
        uint32_t fullRate = (uint32_t)SbrContextGetFullRate(hEncoder->sbrContext, hEncoder->sampleRate);
69
        /* The access unit written on a tick carries audio that entered the FIFO
70
         * LOOKAHEAD_DEPTH ticks earlier, on the encoder timeline that
71
         * encoder_delay also uses. The analyzer's lookahead and the tick on
72
         * which the 800 ms meter first sees an onset make up the difference, so
73
         * the gain still lands one frame ahead of a loud onset. */
74
681
        hEncoder->drcAnalyzer = DrcAnalyzerInit(fullRate, hEncoder->numChannels,
75
681
                                                hEncoder->isLfeChannel, hEncoder->config.drcRefLevel,
76
681
                                                hEncoder->config.drcProfile, faacFrameSamples(hEncoder));
77
681
        if (!hEncoder->drcAnalyzer)
78
0
            return -1;
79
681
    }
80
2.94k
    return 0;
81
2.94k
}
82
83
void DrcProcessFrame(faacEncStruct *hEncoder, float * const *input_pcm, uint32_t samples_per_ch)
84
20.1k
{
85
20.1k
    if (hEncoder->drcAnalyzer)
86
4.30k
        DrcAnalyzerProcessFrame(hEncoder->drcAnalyzer, input_pcm, samples_per_ch, &hEncoder->drcInfo);
87
20.1k
}
88
89
void DrcClose(faacEncStruct *hEncoder)
90
2.95k
{
91
2.95k
    if (hEncoder->drcAnalyzer) {
92
681
        FreeMemory(hEncoder->drcAnalyzer);
93
        hEncoder->drcAnalyzer = NULL;
94
681
    }
95
2.95k
}
96
97
#endif