2 * MPEG Audio header decoder
3 * Copyright (c) 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
24 * MPEG Audio header decoder.
27 #include "libavutil/macros.h"
29 #include "mpegaudio.h"
30 #include "mpegaudiodata.h"
31 #include "mpegaudiodecheader.h"
34 int avpriv_mpegaudio_decode_header(MPADecodeHeader
*s
, uint32_t header
)
36 int sample_rate
, frame_size
, mpeg25
, padding
;
37 int sample_rate_index
, bitrate_index
;
40 ret
= ff_mpa_check_header(header
);
44 if (header
& (1<<20)) {
45 s
->lsf
= (header
& (1<<19)) ? 0 : 1;
52 s
->layer
= 4 - ((header
>> 17) & 3);
53 /* extract frequency */
54 sample_rate_index
= (header
>> 10) & 3;
55 if (sample_rate_index
>= FF_ARRAY_ELEMS(ff_mpa_freq_tab
))
56 sample_rate_index
= 0;
57 sample_rate
= ff_mpa_freq_tab
[sample_rate_index
] >> (s
->lsf
+ mpeg25
);
58 sample_rate_index
+= 3 * (s
->lsf
+ mpeg25
);
59 s
->sample_rate_index
= sample_rate_index
;
60 s
->error_protection
= ((header
>> 16) & 1) ^ 1;
61 s
->sample_rate
= sample_rate
;
63 bitrate_index
= (header
>> 12) & 0xf;
64 padding
= (header
>> 9) & 1;
65 //extension = (header >> 8) & 1;
66 s
->mode
= (header
>> 6) & 3;
67 s
->mode_ext
= (header
>> 4) & 3;
68 //copyright = (header >> 3) & 1;
69 //original = (header >> 2) & 1;
70 //emphasis = header & 3;
72 if (s
->mode
== MPA_MONO
)
77 if (bitrate_index
!= 0) {
78 frame_size
= ff_mpa_bitrate_tab
[s
->lsf
][s
->layer
- 1][bitrate_index
];
79 s
->bit_rate
= frame_size
* 1000;
82 frame_size
= (frame_size
* 12000) / sample_rate
;
83 frame_size
= (frame_size
+ padding
) * 4;
86 frame_size
= (frame_size
* 144000) / sample_rate
;
87 frame_size
+= padding
;
91 frame_size
= (frame_size
* 144000) / (sample_rate
<< s
->lsf
);
92 frame_size
+= padding
;
95 s
->frame_size
= frame_size
;
97 /* if no frame size computed, signal it */
102 ff_dlog(NULL
, "layer%d, %d Hz, %d kbits/s, ",
103 s
->layer
, s
->sample_rate
, s
->bit_rate
);
104 if (s
->nb_channels
== 2) {
106 if (s
->mode_ext
& MODE_EXT_MS_STEREO
)
107 ff_dlog(NULL
, "ms-");
108 if (s
->mode_ext
& MODE_EXT_I_STEREO
)
111 ff_dlog(NULL
, "stereo");
113 ff_dlog(NULL
, "mono");
120 int ff_mpa_decode_header(uint32_t head
, int *sample_rate
, int *channels
, int *frame_size
, int *bit_rate
, enum AVCodecID
*codec_id
)
122 MPADecodeHeader s1
, *s
= &s1
;
124 if (avpriv_mpegaudio_decode_header(s
, head
) != 0) {
130 *codec_id
= AV_CODEC_ID_MP1
;
134 *codec_id
= AV_CODEC_ID_MP2
;
139 if (*codec_id
!= AV_CODEC_ID_MP3ADU
)
140 *codec_id
= AV_CODEC_ID_MP3
;
148 *sample_rate
= s
->sample_rate
;
149 *channels
= s
->nb_channels
;
150 *bit_rate
= s
->bit_rate
;
151 return s
->frame_size
;