FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegaudiodecheader.h
Date: 2026-09-24 20:08:24
Exec Total Coverage
Lines: 26 26 100.0%
Functions: 2 2 100.0%
Branches: 14 14 100.0%

Line Branch Exec Source
1 /*
2 * MPEG Audio header decoder
3 * Copyright (c) 2001, 2002 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * MPEG Audio header decoder.
25 */
26
27 #ifndef AVCODEC_MPEGAUDIODECHEADER_H
28 #define AVCODEC_MPEGAUDIODECHEADER_H
29
30 #include <stdint.h>
31
32 #include "libavutil/attributes.h"
33
34 #include "codec_id.h"
35 #include "version_major.h"
36
37 #define MP3_MASK 0xFFFE0CCF
38
39 #define FF_MPEGAUDIO_LEGACY_DECODE_HEADER (LIBAVCODEC_VERSION_MAJOR < 64)
40
41 #if FF_MPEGAUDIO_LEGACY_DECODE_HEADER
42 #define MPA_DECODE_HEADER \
43 int frame_size; \
44 int error_protection; \
45 int layer; \
46 int sample_rate; \
47 int sample_rate_index; /* between 0 and 8 */ \
48 int bit_rate; \
49 int nb_channels; \
50 int mode; \
51 int mode_ext; \
52 int lsf;
53
54 typedef struct MPADecodeHeader {
55 MPA_DECODE_HEADER
56 } MPADecodeHeader;
57
58 /* header decoding. MUST check the header before because no
59 consistency check is done there. Return 1 if free format found and
60 that the frame size must be computed externally */
61 int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header);
62 #endif
63
64 /**
65 * Same as MPADecodeHeader, plus the header fields that carry no decoding
66 * information. Its size is not part of the libavcodec ABI: it is only ever
67 * allocated by avpriv_mpegaudio_decode_header2().
68 */
69 typedef struct MPADecodeHeader2 {
70 int frame_size;
71 int error_protection;
72 int layer;
73 int sample_rate;
74 int sample_rate_index; /* between 0 and 8 */
75 int bit_rate;
76 int nb_channels;
77 int mode;
78 int mode_ext;
79 int lsf;
80 int copyright;
81 int original;
82 int emphasis;
83 int bitrate_index;
84 int padding;
85 int private_bit;
86 } MPADecodeHeader2;
87
88 /**
89 * Decode an MPEG audio header into *phdr, which is allocated if it is NULL.
90 *
91 * The header must have been checked beforehand, as no consistency check is
92 * done here.
93 *
94 * @return 1 if free format was found and the frame size must be computed
95 * externally, 0 on success, a negative AVERROR code on failure
96 */
97 int avpriv_mpegaudio_decode_header2(MPADecodeHeader2 **phdr, uint32_t header);
98
99 /* same as avpriv_mpegaudio_decode_header2(), for callers within libavcodec,
100 which are built against this very header and may allocate it themselves */
101 int ff_mpegaudio_decode_header(MPADecodeHeader2 *s, uint32_t header);
102
103 /* useful helper to get MPEG audio stream info. Return -1 if error in
104 header, otherwise the coded frame size in bytes */
105 int ff_mpa_decode_header(uint32_t head, int *sample_rate,
106 int *channels, int *frame_size, int *bitrate, enum AVCodecID *codec_id);
107
108 /* fast header check for resync */
109 464287831 static inline int ff_mpa_check_header(uint32_t header){
110 /* header */
111
2/2
✓ Branch 0 taken 463001586 times.
✓ Branch 1 taken 1286245 times.
464287831 if ((header & 0xffe00000) != 0xffe00000)
112 463001586 return -1;
113 /* version check */
114
2/2
✓ Branch 0 taken 246332 times.
✓ Branch 1 taken 1039913 times.
1286245 if ((header & (3<<19)) == 1<<19)
115 246332 return -1;
116 /* layer check */
117
2/2
✓ Branch 0 taken 235998 times.
✓ Branch 1 taken 803915 times.
1039913 if ((header & (3<<17)) == 0)
118 235998 return -1;
119 /* bit rate */
120
2/2
✓ Branch 0 taken 541884 times.
✓ Branch 1 taken 262031 times.
803915 if ((header & (0xf<<12)) == 0xf<<12)
121 541884 return -1;
122 /* frequency */
123
2/2
✓ Branch 0 taken 35714 times.
✓ Branch 1 taken 226317 times.
262031 if ((header & (3<<10)) == 3<<10)
124 35714 return -1;
125 226317 return 0;
126 }
127
128 683 static inline uint32_t ff_mpa_encode_header(const MPADecodeHeader2 *s)
129 {
130
2/2
✓ Branch 0 taken 677 times.
✓ Branch 1 taken 6 times.
683 int version = s->sample_rate_index < 6 ? 3 - s->sample_rate_index / 3 : 0;
131
132 return 0xffeU << 20 |
133 683 version << 19 |
134 1366 (4 - s->layer) << 17 |
135
2/2
✓ Branch 0 taken 658 times.
✓ Branch 1 taken 25 times.
683 !s->error_protection << 16 |
136 683 s->bitrate_index << 12 |
137 683 s->sample_rate_index % 3 << 10 |
138 683 s->padding << 9 |
139 683 s->private_bit << 8 |
140 683 s->mode << 6 |
141 683 s->mode_ext << 4 |
142 683 s->copyright << 3 |
143 1366 s->original << 2 |
144 683 s->emphasis;
145 }
146
147 #endif /* AVCODEC_MPEGAUDIODECHEADER_H */
148