Coverage Report

Created: 2026-07-12 08:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/packetizer/mpegaudio.h
Line
Count
Source
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
162M
{
49
162M
    static const uint16_t ppi_bitrate[2][3][16] =
50
162M
    {
51
162M
        {
52
            /* v1 l1 */
53
162M
            { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
54
162M
              416, 448, 0},
55
            /* v1 l2 */
56
162M
            { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256,
57
162M
              320, 384, 0},
58
            /* v1 l3 */
59
162M
            { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224,
60
162M
              256, 320, 0}
61
162M
        },
62
63
162M
        {
64
            /* v2 l1 */
65
162M
            { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192,
66
162M
              224, 256, 0},
67
            /* v2 l2 */
68
162M
            { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
69
162M
              144, 160, 0},
70
            /* v2 l3 */
71
162M
            { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
72
162M
              144, 160, 0}
73
162M
        }
74
162M
    };
75
76
162M
    static const uint16_t ppi_samplerate[2][4] = /* version 1 then 2 */
77
162M
    {
78
162M
        { 44100, 48000, 32000, 0 },
79
162M
        { 22050, 24000, 16000, 0 }
80
162M
    };
81
82
162M
    bool b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
83
162M
    unsigned i_version_index = 1 - ((i_header & 0x80000) >> 19);
84
162M
    h->i_layer   = 4 - ((i_header & 0x60000) >> 17);
85
    //bool b_crc = !((i_header >> 16) & 0x01);
86
162M
    unsigned i_bitrate_index = (i_header & 0xf000) >> 12;
87
162M
    unsigned i_samplerate_index = (i_header & 0xc00) >> 10;
88
162M
    bool b_padding = (i_header & 0x200) >> 9;
89
    /* Extension */
90
162M
    uint8_t i_mode = (i_header & 0xc0) >> 6;
91
    /* Modeext, copyright & original */
92
162M
    uint8_t i_emphasis = i_header & 0x3;
93
94
162M
    h->i_chan_mode = 0;
95
96
162M
    if (h->i_layer == 4
97
160M
     || i_bitrate_index == 0x0f
98
54.3M
     || i_samplerate_index == 0x03
99
51.3M
     || i_emphasis == 0x02)
100
112M
        return -1;
101
102
50.2M
    switch (i_mode)
103
50.2M
    {
104
935k
        case 2: /* dual-mono */
105
935k
            h->i_chan_mode = AOUT_CHANMODE_DUALMONO;
106
            /* fall through */
107
17.9M
        case 0: /* stereo */
108
25.7M
        case 1: /* joint stereo */
109
25.7M
            h->i_channels = 2;
110
25.7M
            h->i_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
111
25.7M
            break;
112
24.5M
        case 3: /* mono */
113
24.5M
            h->i_channels = 1;
114
24.5M
            h->i_channels_conf = AOUT_CHAN_CENTER;
115
24.5M
            break;
116
50.2M
    }
117
118
50.2M
    uint16_t i_max_bit_rate = ppi_bitrate[i_version_index][h->i_layer-1][14];
119
50.2M
    h->i_bit_rate = ppi_bitrate[i_version_index][h->i_layer-1][i_bitrate_index];
120
50.2M
    h->i_sample_rate = ppi_samplerate[i_version_index][i_samplerate_index];
121
122
50.2M
    if (b_mpeg_2_5)
123
11.0M
        h->i_sample_rate /= 2;
124
125
50.2M
    switch (h->i_layer)
126
50.2M
    {
127
22.5M
        case 1:
128
22.5M
            h->i_frame_size = (12000 * h->i_bit_rate / h->i_sample_rate +
129
22.5M
                            b_padding) * 4;
130
22.5M
            h->i_max_frame_size = (12000 * i_max_bit_rate /
131
22.5M
                                  h->i_sample_rate + 1) * 4;
132
22.5M
            h->i_samples_per_frame = 384;
133
22.5M
            break;
134
135
9.96M
        case 2:
136
9.96M
            h->i_frame_size = 144000 * h->i_bit_rate / h->i_sample_rate + b_padding;
137
9.96M
            h->i_max_frame_size = 144000 * i_max_bit_rate / h->i_sample_rate + 1;
138
9.96M
            h->i_samples_per_frame = 1152;
139
9.96M
            break;
140
141
17.7M
        case 3:
142
17.7M
            h->i_frame_size = (i_version_index ? 72000 : 144000) *
143
17.7M
                            h->i_bit_rate / h->i_sample_rate + b_padding;
144
17.7M
            h->i_max_frame_size = (i_version_index ? 72000 : 144000) *
145
17.7M
                                  i_max_bit_rate / h->i_sample_rate + 1;
146
17.7M
            h->i_samples_per_frame = i_version_index ? 576 : 1152;
147
17.7M
            break;
148
149
0
        default:
150
50.2M
            vlc_assert_unreachable();
151
50.2M
    }
152
153
    /* Free bitrate mode can support higher bitrates */
154
50.2M
    if (h->i_bit_rate == 0)
155
34.7M
        h->i_max_frame_size *= 2;
156
157
50.2M
    return 0;
158
50.2M
}
es.c:mpga_decode_frameheader
Line
Count
Source
48
13.2k
{
49
13.2k
    static const uint16_t ppi_bitrate[2][3][16] =
50
13.2k
    {
51
13.2k
        {
52
            /* v1 l1 */
53
13.2k
            { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
54
13.2k
              416, 448, 0},
55
            /* v1 l2 */
56
13.2k
            { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256,
57
13.2k
              320, 384, 0},
58
            /* v1 l3 */
59
13.2k
            { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224,
60
13.2k
              256, 320, 0}
61
13.2k
        },
62
63
13.2k
        {
64
            /* v2 l1 */
65
13.2k
            { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192,
66
13.2k
              224, 256, 0},
67
            /* v2 l2 */
68
13.2k
            { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
69
13.2k
              144, 160, 0},
70
            /* v2 l3 */
71
13.2k
            { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
72
13.2k
              144, 160, 0}
73
13.2k
        }
74
13.2k
    };
75
76
13.2k
    static const uint16_t ppi_samplerate[2][4] = /* version 1 then 2 */
77
13.2k
    {
78
13.2k
        { 44100, 48000, 32000, 0 },
79
13.2k
        { 22050, 24000, 16000, 0 }
80
13.2k
    };
81
82
13.2k
    bool b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
83
13.2k
    unsigned i_version_index = 1 - ((i_header & 0x80000) >> 19);
84
13.2k
    h->i_layer   = 4 - ((i_header & 0x60000) >> 17);
85
    //bool b_crc = !((i_header >> 16) & 0x01);
86
13.2k
    unsigned i_bitrate_index = (i_header & 0xf000) >> 12;
87
13.2k
    unsigned i_samplerate_index = (i_header & 0xc00) >> 10;
88
13.2k
    bool b_padding = (i_header & 0x200) >> 9;
89
    /* Extension */
90
13.2k
    uint8_t i_mode = (i_header & 0xc0) >> 6;
91
    /* Modeext, copyright & original */
92
13.2k
    uint8_t i_emphasis = i_header & 0x3;
93
94
13.2k
    h->i_chan_mode = 0;
95
96
13.2k
    if (h->i_layer == 4
97
13.2k
     || i_bitrate_index == 0x0f
98
13.2k
     || i_samplerate_index == 0x03
99
13.2k
     || i_emphasis == 0x02)
100
0
        return -1;
101
102
13.2k
    switch (i_mode)
103
13.2k
    {
104
278
        case 2: /* dual-mono */
105
278
            h->i_chan_mode = AOUT_CHANMODE_DUALMONO;
106
            /* fall through */
107
5.17k
        case 0: /* stereo */
108
9.61k
        case 1: /* joint stereo */
109
9.61k
            h->i_channels = 2;
110
9.61k
            h->i_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
111
9.61k
            break;
112
3.68k
        case 3: /* mono */
113
3.68k
            h->i_channels = 1;
114
3.68k
            h->i_channels_conf = AOUT_CHAN_CENTER;
115
3.68k
            break;
116
13.2k
    }
117
118
13.2k
    uint16_t i_max_bit_rate = ppi_bitrate[i_version_index][h->i_layer-1][14];
119
13.2k
    h->i_bit_rate = ppi_bitrate[i_version_index][h->i_layer-1][i_bitrate_index];
120
13.2k
    h->i_sample_rate = ppi_samplerate[i_version_index][i_samplerate_index];
121
122
13.2k
    if (b_mpeg_2_5)
123
5.07k
        h->i_sample_rate /= 2;
124
125
13.2k
    switch (h->i_layer)
126
13.2k
    {
127
5.65k
        case 1:
128
5.65k
            h->i_frame_size = (12000 * h->i_bit_rate / h->i_sample_rate +
129
5.65k
                            b_padding) * 4;
130
5.65k
            h->i_max_frame_size = (12000 * i_max_bit_rate /
131
5.65k
                                  h->i_sample_rate + 1) * 4;
132
5.65k
            h->i_samples_per_frame = 384;
133
5.65k
            break;
134
135
1.75k
        case 2:
136
1.75k
            h->i_frame_size = 144000 * h->i_bit_rate / h->i_sample_rate + b_padding;
137
1.75k
            h->i_max_frame_size = 144000 * i_max_bit_rate / h->i_sample_rate + 1;
138
1.75k
            h->i_samples_per_frame = 1152;
139
1.75k
            break;
140
141
5.88k
        case 3:
142
5.88k
            h->i_frame_size = (i_version_index ? 72000 : 144000) *
143
5.88k
                            h->i_bit_rate / h->i_sample_rate + b_padding;
144
5.88k
            h->i_max_frame_size = (i_version_index ? 72000 : 144000) *
145
5.88k
                                  i_max_bit_rate / h->i_sample_rate + 1;
146
5.88k
            h->i_samples_per_frame = i_version_index ? 576 : 1152;
147
5.88k
            break;
148
149
0
        default:
150
13.2k
            vlc_assert_unreachable();
151
13.2k
    }
152
153
    /* Free bitrate mode can support higher bitrates */
154
13.2k
    if (h->i_bit_rate == 0)
155
8.52k
        h->i_max_frame_size *= 2;
156
157
13.2k
    return 0;
158
13.2k
}
mpegaudio.c:mpga_decode_frameheader
Line
Count
Source
48
162M
{
49
162M
    static const uint16_t ppi_bitrate[2][3][16] =
50
162M
    {
51
162M
        {
52
            /* v1 l1 */
53
162M
            { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
54
162M
              416, 448, 0},
55
            /* v1 l2 */
56
162M
            { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256,
57
162M
              320, 384, 0},
58
            /* v1 l3 */
59
162M
            { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224,
60
162M
              256, 320, 0}
61
162M
        },
62
63
162M
        {
64
            /* v2 l1 */
65
162M
            { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192,
66
162M
              224, 256, 0},
67
            /* v2 l2 */
68
162M
            { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
69
162M
              144, 160, 0},
70
            /* v2 l3 */
71
162M
            { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
72
162M
              144, 160, 0}
73
162M
        }
74
162M
    };
75
76
162M
    static const uint16_t ppi_samplerate[2][4] = /* version 1 then 2 */
77
162M
    {
78
162M
        { 44100, 48000, 32000, 0 },
79
162M
        { 22050, 24000, 16000, 0 }
80
162M
    };
81
82
162M
    bool b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
83
162M
    unsigned i_version_index = 1 - ((i_header & 0x80000) >> 19);
84
162M
    h->i_layer   = 4 - ((i_header & 0x60000) >> 17);
85
    //bool b_crc = !((i_header >> 16) & 0x01);
86
162M
    unsigned i_bitrate_index = (i_header & 0xf000) >> 12;
87
162M
    unsigned i_samplerate_index = (i_header & 0xc00) >> 10;
88
162M
    bool b_padding = (i_header & 0x200) >> 9;
89
    /* Extension */
90
162M
    uint8_t i_mode = (i_header & 0xc0) >> 6;
91
    /* Modeext, copyright & original */
92
162M
    uint8_t i_emphasis = i_header & 0x3;
93
94
162M
    h->i_chan_mode = 0;
95
96
162M
    if (h->i_layer == 4
97
160M
     || i_bitrate_index == 0x0f
98
54.3M
     || i_samplerate_index == 0x03
99
51.3M
     || i_emphasis == 0x02)
100
112M
        return -1;
101
102
50.2M
    switch (i_mode)
103
50.2M
    {
104
934k
        case 2: /* dual-mono */
105
934k
            h->i_chan_mode = AOUT_CHANMODE_DUALMONO;
106
            /* fall through */
107
17.9M
        case 0: /* stereo */
108
25.7M
        case 1: /* joint stereo */
109
25.7M
            h->i_channels = 2;
110
25.7M
            h->i_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
111
25.7M
            break;
112
24.4M
        case 3: /* mono */
113
24.4M
            h->i_channels = 1;
114
24.4M
            h->i_channels_conf = AOUT_CHAN_CENTER;
115
24.4M
            break;
116
50.2M
    }
117
118
50.2M
    uint16_t i_max_bit_rate = ppi_bitrate[i_version_index][h->i_layer-1][14];
119
50.2M
    h->i_bit_rate = ppi_bitrate[i_version_index][h->i_layer-1][i_bitrate_index];
120
50.2M
    h->i_sample_rate = ppi_samplerate[i_version_index][i_samplerate_index];
121
122
50.2M
    if (b_mpeg_2_5)
123
11.0M
        h->i_sample_rate /= 2;
124
125
50.2M
    switch (h->i_layer)
126
50.2M
    {
127
22.5M
        case 1:
128
22.5M
            h->i_frame_size = (12000 * h->i_bit_rate / h->i_sample_rate +
129
22.5M
                            b_padding) * 4;
130
22.5M
            h->i_max_frame_size = (12000 * i_max_bit_rate /
131
22.5M
                                  h->i_sample_rate + 1) * 4;
132
22.5M
            h->i_samples_per_frame = 384;
133
22.5M
            break;
134
135
9.96M
        case 2:
136
9.96M
            h->i_frame_size = 144000 * h->i_bit_rate / h->i_sample_rate + b_padding;
137
9.96M
            h->i_max_frame_size = 144000 * i_max_bit_rate / h->i_sample_rate + 1;
138
9.96M
            h->i_samples_per_frame = 1152;
139
9.96M
            break;
140
141
17.7M
        case 3:
142
17.7M
            h->i_frame_size = (i_version_index ? 72000 : 144000) *
143
17.7M
                            h->i_bit_rate / h->i_sample_rate + b_padding;
144
17.7M
            h->i_max_frame_size = (i_version_index ? 72000 : 144000) *
145
17.7M
                                  i_max_bit_rate / h->i_sample_rate + 1;
146
17.7M
            h->i_samples_per_frame = i_version_index ? 576 : 1152;
147
17.7M
            break;
148
149
0
        default:
150
50.2M
            vlc_assert_unreachable();
151
50.2M
    }
152
153
    /* Free bitrate mode can support higher bitrates */
154
50.2M
    if (h->i_bit_rate == 0)
155
34.7M
        h->i_max_frame_size *= 2;
156
157
50.2M
    return 0;
158
50.2M
}