FFmpeg
Loading...
Searching...
No Matches
mpegaudiodecheader.c
Go to the documentation of this file.
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#include <string.h>
28
29#include "libavutil/error.h"
30#include "libavutil/macros.h"
31#include "libavutil/mem.h"
32
33#include "mpegaudio.h"
34#include "mpegaudiodata.h"
35#include "mpegaudiodecheader.h"
36
37
39{
40 int sample_rate, frame_size, mpeg25, padding;
41 int sample_rate_index, bitrate_index;
42 int ret;
43
45 if (ret < 0)
46 return ret;
47
48 memset(s, 0, sizeof(*s));
49
50 if (header & (1<<20)) {
51 s->lsf = (header & (1<<19)) ? 0 : 1;
52 mpeg25 = 0;
53 } else {
54 s->lsf = 1;
55 mpeg25 = 1;
56 }
57
58 s->layer = 4 - ((header >> 17) & 3);
59 /* extract frequency */
60 sample_rate_index = (header >> 10) & 3;
61 if (sample_rate_index >= FF_ARRAY_ELEMS(ff_mpa_freq_tab))
62 sample_rate_index = 0;
63 sample_rate = ff_mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
64 sample_rate_index += 3 * (s->lsf + mpeg25);
65 s->sample_rate_index = sample_rate_index;
66 s->error_protection = ((header >> 16) & 1) ^ 1;
67 s->sample_rate = sample_rate;
68
69 bitrate_index = (header >> 12) & 0xf;
70 padding = (header >> 9) & 1;
71 s->bitrate_index = bitrate_index;
72 s->padding = padding;
73 s->private_bit = (header >> 8) & 1;
74 s->mode = (header >> 6) & 3;
75 s->mode_ext = (header >> 4) & 3;
76 s->copyright = (header >> 3) & 1;
77 s->original = (header >> 2) & 1;
78 s->emphasis = header & 3;
79
80 if (s->mode == MPA_MONO)
81 s->nb_channels = 1;
82 else
83 s->nb_channels = 2;
84
85 if (bitrate_index != 0) {
86 frame_size = ff_mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
87 s->bit_rate = frame_size * 1000;
88 switch(s->layer) {
89 case 1:
90 frame_size = (frame_size * 12000) / sample_rate;
91 frame_size = (frame_size + padding) * 4;
92 break;
93 case 2:
94 frame_size = (frame_size * 144000) / sample_rate;
95 frame_size += padding;
96 break;
97 default:
98 case 3:
99 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
100 frame_size += padding;
101 break;
102 }
103 s->frame_size = frame_size;
104 } else {
105 /* if no frame size computed, signal it */
106 return 1;
107 }
108
109#if defined(DEBUG)
110 ff_dlog(NULL, "layer%d, %d Hz, %d kbits/s, ",
111 s->layer, s->sample_rate, s->bit_rate);
112 if (s->nb_channels == 2) {
113 if (s->layer == 3) {
114 if (s->mode_ext & MODE_EXT_MS_STEREO)
115 ff_dlog(NULL, "ms-");
116 if (s->mode_ext & MODE_EXT_I_STEREO)
117 ff_dlog(NULL, "i-");
118 }
119 ff_dlog(NULL, "stereo");
120 } else {
121 ff_dlog(NULL, "mono");
122 }
123 ff_dlog(NULL, "\n");
124#endif
125 return 0;
126}
127
129{
130 MPADecodeHeader2 *hdr = *phdr;
131 int ret;
132
133 if (!hdr) {
135 if (ret < 0)
136 return ret;
137 hdr = av_mallocz(sizeof(*hdr));
138 if (!hdr)
139 return AVERROR(ENOMEM);
140 }
141
143 if (ret < 0)
144 return ret;
145
146 *phdr = hdr;
147 return ret;
148}
149
150#if FF_MPEGAUDIO_LEGACY_DECODE_HEADER
151int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header)
152{
153 MPADecodeHeader2 hdr = { 0 };
154 int ret = ff_mpegaudio_decode_header(&hdr, header);
155
156 if (ret < 0)
157 return ret;
158
159 /* callers in other libraries allocate the smaller structure, so only
160 its fields may be written */
161 s->frame_size = hdr.frame_size;
162 s->error_protection = hdr.error_protection;
163 s->layer = hdr.layer;
164 s->sample_rate = hdr.sample_rate;
165 s->sample_rate_index = hdr.sample_rate_index;
166 s->bit_rate = hdr.bit_rate;
167 s->nb_channels = hdr.nb_channels;
168 s->mode = hdr.mode;
169 s->mode_ext = hdr.mode_ext;
170 s->lsf = hdr.lsf;
171
172 return ret;
173}
174#endif
175
176int ff_mpa_decode_header(uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate, enum AVCodecID *codec_id)
177{
178 MPADecodeHeader2 s1, *s = &s1;
179
180 if (ff_mpegaudio_decode_header(s, head) != 0) {
181 return -1;
182 }
183
184 switch(s->layer) {
185 case 1:
187 *frame_size = 384;
188 break;
189 case 2:
191 *frame_size = 1152;
192 break;
193 default:
194 case 3:
197 if (s->lsf)
198 *frame_size = 576;
199 else
200 *frame_size = 1152;
201 break;
202 }
203
204 *sample_rate = s->sample_rate;
205 *channels = s->nb_channels;
206 *bit_rate = s->bit_rate;
207 return s->frame_size;
208}
channels
Definition aptx.h:31
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
error code definitions
static const uint8_t frame_size[4]
Definition g723_1.h:222
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_MP1
Definition codec_id.h:496
@ AV_CODEC_ID_MP2
Definition codec_id.h:454
@ AV_CODEC_ID_MP3ADU
Definition codec_id.h:467
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition codec_id.h:455
#define AVERROR(e)
Definition error.h:45
Utility Preprocessor macros.
Memory handling functions.
mpeg audio declarations for both encoder and decoder.
#define MPA_MONO
Definition mpegaudio.h:49
mpeg audio layer common tables.
#define MODE_EXT_MS_STEREO
FF_VISIBILITY_PUSH_HIDDEN const uint16_t ff_mpa_bitrate_tab[2][3][15]
#define MODE_EXT_I_STEREO
const uint16_t ff_mpa_freq_tab[3]
int ff_mpegaudio_decode_header(MPADecodeHeader2 *s, uint32_t header)
int ff_mpa_decode_header(uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate, enum AVCodecID *codec_id)
int avpriv_mpegaudio_decode_header2(MPADecodeHeader2 **phdr, uint32_t header)
Decode an MPEG audio header into *phdr, which is allocated if it is NULL.
MPEG Audio header decoder.
static int ff_mpa_check_header(uint32_t header)
static const uint8_t header[24]
Definition sdr2.c:68
#define FF_ARRAY_ELEMS(a)
Same as MPADecodeHeader, plus the header fields that carry no decoding information.
#define av_mallocz(s)
#define ff_dlog(a,...)
enum AVCodecID codec_id