/src/ffmpeg/libavcodec/mpegaudiodecheader.c
Line | Count | 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 | | #include "libavutil/macros.h" |
28 | | |
29 | | #include "mpegaudio.h" |
30 | | #include "mpegaudiodata.h" |
31 | | #include "mpegaudiodecheader.h" |
32 | | |
33 | | |
34 | | int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header) |
35 | 3.85G | { |
36 | 3.85G | int sample_rate, frame_size, mpeg25, padding; |
37 | 3.85G | int sample_rate_index, bitrate_index; |
38 | 3.85G | int ret; |
39 | | |
40 | 3.85G | ret = ff_mpa_check_header(header); |
41 | 3.85G | if (ret < 0) |
42 | 3.82G | return ret; |
43 | | |
44 | 29.2M | if (header & (1<<20)) { |
45 | 28.4M | s->lsf = (header & (1<<19)) ? 0 : 1; |
46 | 28.4M | mpeg25 = 0; |
47 | 28.4M | } else { |
48 | 890k | s->lsf = 1; |
49 | 890k | mpeg25 = 1; |
50 | 890k | } |
51 | | |
52 | 29.2M | s->layer = 4 - ((header >> 17) & 3); |
53 | | /* extract frequency */ |
54 | 29.2M | sample_rate_index = (header >> 10) & 3; |
55 | 29.2M | if (sample_rate_index >= FF_ARRAY_ELEMS(ff_mpa_freq_tab)) |
56 | 0 | sample_rate_index = 0; |
57 | 29.2M | sample_rate = ff_mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25); |
58 | 29.2M | sample_rate_index += 3 * (s->lsf + mpeg25); |
59 | 29.2M | s->sample_rate_index = sample_rate_index; |
60 | 29.2M | s->error_protection = ((header >> 16) & 1) ^ 1; |
61 | 29.2M | s->sample_rate = sample_rate; |
62 | | |
63 | 29.2M | bitrate_index = (header >> 12) & 0xf; |
64 | 29.2M | padding = (header >> 9) & 1; |
65 | | //extension = (header >> 8) & 1; |
66 | 29.2M | s->mode = (header >> 6) & 3; |
67 | 29.2M | s->mode_ext = (header >> 4) & 3; |
68 | | //copyright = (header >> 3) & 1; |
69 | | //original = (header >> 2) & 1; |
70 | | //emphasis = header & 3; |
71 | | |
72 | 29.2M | if (s->mode == MPA_MONO) |
73 | 5.81M | s->nb_channels = 1; |
74 | 23.4M | else |
75 | 23.4M | s->nb_channels = 2; |
76 | | |
77 | 29.2M | if (bitrate_index != 0) { |
78 | 21.8M | frame_size = ff_mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index]; |
79 | 21.8M | s->bit_rate = frame_size * 1000; |
80 | 21.8M | switch(s->layer) { |
81 | 12.3M | case 1: |
82 | 12.3M | frame_size = (frame_size * 12000) / sample_rate; |
83 | 12.3M | frame_size = (frame_size + padding) * 4; |
84 | 12.3M | break; |
85 | 734k | case 2: |
86 | 734k | frame_size = (frame_size * 144000) / sample_rate; |
87 | 734k | frame_size += padding; |
88 | 734k | break; |
89 | 0 | default: |
90 | 8.80M | case 3: |
91 | 8.80M | frame_size = (frame_size * 144000) / (sample_rate << s->lsf); |
92 | 8.80M | frame_size += padding; |
93 | 8.80M | break; |
94 | 21.8M | } |
95 | 21.8M | s->frame_size = frame_size; |
96 | 21.8M | } else { |
97 | | /* if no frame size computed, signal it */ |
98 | 7.40M | return 1; |
99 | 7.40M | } |
100 | | |
101 | | #if defined(DEBUG) |
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) { |
105 | | if (s->layer == 3) { |
106 | | if (s->mode_ext & MODE_EXT_MS_STEREO) |
107 | | ff_dlog(NULL, "ms-"); |
108 | | if (s->mode_ext & MODE_EXT_I_STEREO) |
109 | | ff_dlog(NULL, "i-"); |
110 | | } |
111 | | ff_dlog(NULL, "stereo"); |
112 | | } else { |
113 | | ff_dlog(NULL, "mono"); |
114 | | } |
115 | | ff_dlog(NULL, "\n"); |
116 | | #endif |
117 | 21.8M | return 0; |
118 | 29.2M | } |
119 | | |
120 | | int ff_mpa_decode_header(uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate, enum AVCodecID *codec_id) |
121 | 1.56G | { |
122 | 1.56G | MPADecodeHeader s1, *s = &s1; |
123 | | |
124 | 1.56G | if (avpriv_mpegaudio_decode_header(s, head) != 0) { |
125 | 1.55G | return -1; |
126 | 1.55G | } |
127 | | |
128 | 9.13M | switch(s->layer) { |
129 | 3.97M | case 1: |
130 | 3.97M | *codec_id = AV_CODEC_ID_MP1; |
131 | 3.97M | *frame_size = 384; |
132 | 3.97M | break; |
133 | 187k | case 2: |
134 | 187k | *codec_id = AV_CODEC_ID_MP2; |
135 | 187k | *frame_size = 1152; |
136 | 187k | break; |
137 | 0 | default: |
138 | 4.97M | case 3: |
139 | 4.97M | if (*codec_id != AV_CODEC_ID_MP3ADU) |
140 | 4.97M | *codec_id = AV_CODEC_ID_MP3; |
141 | 4.97M | if (s->lsf) |
142 | 4.89M | *frame_size = 576; |
143 | 80.0k | else |
144 | 80.0k | *frame_size = 1152; |
145 | 4.97M | break; |
146 | 9.13M | } |
147 | | |
148 | 9.13M | *sample_rate = s->sample_rate; |
149 | 9.13M | *channels = s->nb_channels; |
150 | 9.13M | *bit_rate = s->bit_rate; |
151 | 9.13M | return s->frame_size; |
152 | 9.13M | } |