Coverage Report

Created: 2025-12-14 06:40

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