mp3dec: replace avpriv_mpa_decode_header with avpriv_mpegaudio_decode_header
[libav.git] / libavcodec / mpegaudiodecheader.c
CommitLineData
4bd8e17c
AJ
1/*
2 * MPEG Audio header decoder
406792e7 3 * Copyright (c) 2001, 2002 Fabrice Bellard
4bd8e17c 4 *
2912e87a 5 * This file is part of Libav.
4bd8e17c 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
4bd8e17c
AJ
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
4bd8e17c
AJ
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
4bd8e17c
AJ
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
ba87f080 23 * @file
4bd8e17c
AJ
24 * MPEG Audio header decoder.
25 */
26
44127546
JR
27#include "libavutil/common.h"
28
4bd8e17c
AJ
29#include "avcodec.h"
30#include "mpegaudio.h"
31#include "mpegaudiodata.h"
c6e1599b 32#include "mpegaudiodecheader.h"
4bd8e17c
AJ
33
34
82ab61f9 35int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header)
4bd8e17c
AJ
36{
37 int sample_rate, frame_size, mpeg25, padding;
38 int sample_rate_index, bitrate_index;
955aec3c
AK
39 int ret;
40
41 ret = ff_mpa_check_header(header);
42 if (ret < 0)
43 return ret;
44
4bd8e17c
AJ
45 if (header & (1<<20)) {
46 s->lsf = (header & (1<<19)) ? 0 : 1;
47 mpeg25 = 0;
48 } else {
49 s->lsf = 1;
50 mpeg25 = 1;
51 }
52
53 s->layer = 4 - ((header >> 17) & 3);
54 /* extract frequency */
55 sample_rate_index = (header >> 10) & 3;
44127546
JR
56 if (sample_rate_index >= FF_ARRAY_ELEMS(avpriv_mpa_freq_tab))
57 sample_rate_index = 0;
82ab61f9 58 sample_rate = avpriv_mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
4bd8e17c
AJ
59 sample_rate_index += 3 * (s->lsf + mpeg25);
60 s->sample_rate_index = sample_rate_index;
61 s->error_protection = ((header >> 16) & 1) ^ 1;
62 s->sample_rate = sample_rate;
63
64 bitrate_index = (header >> 12) & 0xf;
65 padding = (header >> 9) & 1;
66 //extension = (header >> 8) & 1;
67 s->mode = (header >> 6) & 3;
68 s->mode_ext = (header >> 4) & 3;
69 //copyright = (header >> 3) & 1;
70 //original = (header >> 2) & 1;
71 //emphasis = header & 3;
72
73 if (s->mode == MPA_MONO)
74 s->nb_channels = 1;
75 else
76 s->nb_channels = 2;
77
78 if (bitrate_index != 0) {
82ab61f9 79 frame_size = avpriv_mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
4bd8e17c
AJ
80 s->bit_rate = frame_size * 1000;
81 switch(s->layer) {
82 case 1:
83 frame_size = (frame_size * 12000) / sample_rate;
84 frame_size = (frame_size + padding) * 4;
85 break;
86 case 2:
87 frame_size = (frame_size * 144000) / sample_rate;
88 frame_size += padding;
89 break;
90 default:
91 case 3:
92 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
93 frame_size += padding;
94 break;
95 }
96 s->frame_size = frame_size;
97 } else {
98 /* if no frame size computed, signal it */
99 return 1;
100 }
101
102#if defined(DEBUG)
6a85dfc8 103 ff_dlog(NULL, "layer%d, %d Hz, %d kbits/s, ",
4bd8e17c
AJ
104 s->layer, s->sample_rate, s->bit_rate);
105 if (s->nb_channels == 2) {
106 if (s->layer == 3) {
107 if (s->mode_ext & MODE_EXT_MS_STEREO)
6a85dfc8 108 ff_dlog(NULL, "ms-");
4bd8e17c 109 if (s->mode_ext & MODE_EXT_I_STEREO)
6a85dfc8 110 ff_dlog(NULL, "i-");
4bd8e17c 111 }
6a85dfc8 112 ff_dlog(NULL, "stereo");
4bd8e17c 113 } else {
6a85dfc8 114 ff_dlog(NULL, "mono");
4bd8e17c 115 }
6a85dfc8 116 ff_dlog(NULL, "\n");
4bd8e17c
AJ
117#endif
118 return 0;
119}
0199e00b 120
82ab61f9 121int avpriv_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate)
0199e00b
MR
122{
123 MPADecodeHeader s1, *s = &s1;
124
82ab61f9 125 if (avpriv_mpegaudio_decode_header(s, head) != 0) {
0199e00b
MR
126 return -1;
127 }
128
129 switch(s->layer) {
130 case 1:
36ef5369 131 avctx->codec_id = AV_CODEC_ID_MP1;
0199e00b
MR
132 *frame_size = 384;
133 break;
134 case 2:
36ef5369 135 avctx->codec_id = AV_CODEC_ID_MP2;
0199e00b
MR
136 *frame_size = 1152;
137 break;
138 default:
139 case 3:
5cbae565
GC
140 if (avctx->codec_id != AV_CODEC_ID_MP3ADU)
141 avctx->codec_id = AV_CODEC_ID_MP3;
0199e00b
MR
142 if (s->lsf)
143 *frame_size = 576;
144 else
145 *frame_size = 1152;
146 break;
147 }
148
149 *sample_rate = s->sample_rate;
150 *channels = s->nb_channels;
151 *bit_rate = s->bit_rate;
0199e00b
MR
152 return s->frame_size;
153}