Coverage Report

Created: 2025-07-23 06:11

/src/vlc/modules/packetizer/mpegaudio.h
Line
Count
Source (jump to first uncovered line)
1
/*****************************************************************************
2
 * mpegaudio.h:
3
 *****************************************************************************
4
 * Copyright (C) 2001-2016 VLC authors and VideoLAN
5
 *               2022 VideoLabs
6
 *
7
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8
 *          Eric Petit <titer@videolan.org>
9
 *          Christophe Massiot <massiot@via.ecp.fr>
10
 *          Gildas Bazin <gbazin@videolan.org>
11
 *
12
 * This program is free software; you can redistribute it and/or modify it
13
 * under the terms of the GNU Lesser General Public License as published by
14
 * the Free Software Foundation; either version 2.1 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Lesser General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Lesser General Public License
23
 * along with this program; if not, write to the Free Software Foundation,
24
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25
 *****************************************************************************/
26
27
#include <stdbool.h>
28
#include <stdint.h>
29
30
struct mpga_frameheader_s
31
{
32
    uint8_t i_layer;
33
    uint8_t i_channels;
34
    uint16_t i_channels_conf;
35
    uint16_t i_chan_mode;
36
    uint16_t i_bit_rate;
37
    uint16_t i_sample_rate;
38
    uint16_t i_samples_per_frame;
39
    unsigned int i_frame_size;
40
    unsigned int i_max_frame_size;
41
};
42
43
/*****************************************************************************
44
 * mpgaDecodeFrameHeader: parse MPEG audio sync info
45
 * returns 0 on success, -1 on failure
46
 *****************************************************************************/
47
static int mpga_decode_frameheader(uint32_t i_header, struct mpga_frameheader_s *h)
48
0
{
49
0
    static const uint16_t ppi_bitrate[2][3][16] =
50
0
    {
51
0
        {
52
            /* v1 l1 */
53
0
            { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
54
0
              416, 448, 0},
55
            /* v1 l2 */
56
0
            { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256,
57
0
              320, 384, 0},
58
            /* v1 l3 */
59
0
            { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224,
60
0
              256, 320, 0}
61
0
        },
62
63
0
        {
64
            /* v2 l1 */
65
0
            { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192,
66
0
              224, 256, 0},
67
            /* v2 l2 */
68
0
            { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
69
0
              144, 160, 0},
70
            /* v2 l3 */
71
0
            { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
72
0
              144, 160, 0}
73
0
        }
74
0
    };
75
76
0
    static const uint16_t ppi_samplerate[2][4] = /* version 1 then 2 */
77
0
    {
78
0
        { 44100, 48000, 32000, 0 },
79
0
        { 22050, 24000, 16000, 0 }
80
0
    };
81
82
0
    bool b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
83
0
    unsigned i_version_index = 1 - ((i_header & 0x80000) >> 19);
84
0
    h->i_layer   = 4 - ((i_header & 0x60000) >> 17);
85
    //bool b_crc = !((i_header >> 16) & 0x01);
86
0
    unsigned i_bitrate_index = (i_header & 0xf000) >> 12;
87
0
    unsigned i_samplerate_index = (i_header & 0xc00) >> 10;
88
0
    bool b_padding = (i_header & 0x200) >> 9;
89
    /* Extension */
90
0
    uint8_t i_mode = (i_header & 0xc0) >> 6;
91
    /* Modeext, copyright & original */
92
0
    uint8_t i_emphasis = i_header & 0x3;
93
94
0
    h->i_chan_mode = 0;
95
96
0
    if (h->i_layer == 4
97
0
     || i_bitrate_index == 0x0f
98
0
     || i_samplerate_index == 0x03
99
0
     || i_emphasis == 0x02)
100
0
        return -1;
101
102
0
    switch (i_mode)
103
0
    {
104
0
        case 2: /* dual-mono */
105
0
            h->i_chan_mode = AOUT_CHANMODE_DUALMONO;
106
            /* fall through */
107
0
        case 0: /* stereo */
108
0
        case 1: /* joint stereo */
109
0
            h->i_channels = 2;
110
0
            h->i_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
111
0
            break;
112
0
        case 3: /* mono */
113
0
            h->i_channels = 1;
114
0
            h->i_channels_conf = AOUT_CHAN_CENTER;
115
0
            break;
116
0
    }
117
118
0
    uint16_t i_max_bit_rate = ppi_bitrate[i_version_index][h->i_layer-1][14];
119
0
    h->i_bit_rate = ppi_bitrate[i_version_index][h->i_layer-1][i_bitrate_index];
120
0
    h->i_sample_rate = ppi_samplerate[i_version_index][i_samplerate_index];
121
122
0
    if (b_mpeg_2_5)
123
0
        h->i_sample_rate /= 2;
124
125
0
    switch (h->i_layer)
126
0
    {
127
0
        case 1:
128
0
            h->i_frame_size = (12000 * h->i_bit_rate / h->i_sample_rate +
129
0
                            b_padding) * 4;
130
0
            h->i_max_frame_size = (12000 * i_max_bit_rate /
131
0
                                  h->i_sample_rate + 1) * 4;
132
0
            h->i_samples_per_frame = 384;
133
0
            break;
134
135
0
        case 2:
136
0
            h->i_frame_size = 144000 * h->i_bit_rate / h->i_sample_rate + b_padding;
137
0
            h->i_max_frame_size = 144000 * i_max_bit_rate / h->i_sample_rate + 1;
138
0
            h->i_samples_per_frame = 1152;
139
0
            break;
140
141
0
        case 3:
142
0
            h->i_frame_size = (i_version_index ? 72000 : 144000) *
143
0
                            h->i_bit_rate / h->i_sample_rate + b_padding;
144
0
            h->i_max_frame_size = (i_version_index ? 72000 : 144000) *
145
0
                                  i_max_bit_rate / h->i_sample_rate + 1;
146
0
            h->i_samples_per_frame = i_version_index ? 576 : 1152;
147
0
            break;
148
149
0
        default:
150
0
            vlc_assert_unreachable();
151
0
    }
152
153
    /* Free bitrate mode can support higher bitrates */
154
0
    if (h->i_bit_rate == 0)
155
0
        h->i_max_frame_size *= 2;
156
157
0
    return 0;
158
0
}
Unexecuted instantiation: es.c:mpga_decode_frameheader
Unexecuted instantiation: mpegaudio.c:mpga_decode_frameheader