lavc: get the profile name through the codec descriptor in avcodec_string()
[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;
39 if (header & (1<<20)) {
40 s->lsf = (header & (1<<19)) ? 0 : 1;
41 mpeg25 = 0;
42 } else {
43 s->lsf = 1;
44 mpeg25 = 1;
45 }
46
47 s->layer = 4 - ((header >> 17) & 3);
48 /* extract frequency */
49 sample_rate_index = (header >> 10) & 3;
44127546
JR
50 if (sample_rate_index >= FF_ARRAY_ELEMS(avpriv_mpa_freq_tab))
51 sample_rate_index = 0;
82ab61f9 52 sample_rate = avpriv_mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
4bd8e17c
AJ
53 sample_rate_index += 3 * (s->lsf + mpeg25);
54 s->sample_rate_index = sample_rate_index;
55 s->error_protection = ((header >> 16) & 1) ^ 1;
56 s->sample_rate = sample_rate;
57
58 bitrate_index = (header >> 12) & 0xf;
59 padding = (header >> 9) & 1;
60 //extension = (header >> 8) & 1;
61 s->mode = (header >> 6) & 3;
62 s->mode_ext = (header >> 4) & 3;
63 //copyright = (header >> 3) & 1;
64 //original = (header >> 2) & 1;
65 //emphasis = header & 3;
66
67 if (s->mode == MPA_MONO)
68 s->nb_channels = 1;
69 else
70 s->nb_channels = 2;
71
72 if (bitrate_index != 0) {
82ab61f9 73 frame_size = avpriv_mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
4bd8e17c
AJ
74 s->bit_rate = frame_size * 1000;
75 switch(s->layer) {
76 case 1:
77 frame_size = (frame_size * 12000) / sample_rate;
78 frame_size = (frame_size + padding) * 4;
79 break;
80 case 2:
81 frame_size = (frame_size * 144000) / sample_rate;
82 frame_size += padding;
83 break;
84 default:
85 case 3:
86 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
87 frame_size += padding;
88 break;
89 }
90 s->frame_size = frame_size;
91 } else {
92 /* if no frame size computed, signal it */
93 return 1;
94 }
95
96#if defined(DEBUG)
6a85dfc8 97 ff_dlog(NULL, "layer%d, %d Hz, %d kbits/s, ",
4bd8e17c
AJ
98 s->layer, s->sample_rate, s->bit_rate);
99 if (s->nb_channels == 2) {
100 if (s->layer == 3) {
101 if (s->mode_ext & MODE_EXT_MS_STEREO)
6a85dfc8 102 ff_dlog(NULL, "ms-");
4bd8e17c 103 if (s->mode_ext & MODE_EXT_I_STEREO)
6a85dfc8 104 ff_dlog(NULL, "i-");
4bd8e17c 105 }
6a85dfc8 106 ff_dlog(NULL, "stereo");
4bd8e17c 107 } else {
6a85dfc8 108 ff_dlog(NULL, "mono");
4bd8e17c 109 }
6a85dfc8 110 ff_dlog(NULL, "\n");
4bd8e17c
AJ
111#endif
112 return 0;
113}
0199e00b 114
82ab61f9 115int avpriv_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate)
0199e00b
MR
116{
117 MPADecodeHeader s1, *s = &s1;
118
119 if (ff_mpa_check_header(head) != 0)
120 return -1;
121
82ab61f9 122 if (avpriv_mpegaudio_decode_header(s, head) != 0) {
0199e00b
MR
123 return -1;
124 }
125
126 switch(s->layer) {
127 case 1:
36ef5369 128 avctx->codec_id = AV_CODEC_ID_MP1;
0199e00b
MR
129 *frame_size = 384;
130 break;
131 case 2:
36ef5369 132 avctx->codec_id = AV_CODEC_ID_MP2;
0199e00b
MR
133 *frame_size = 1152;
134 break;
135 default:
136 case 3:
5cbae565
GC
137 if (avctx->codec_id != AV_CODEC_ID_MP3ADU)
138 avctx->codec_id = AV_CODEC_ID_MP3;
0199e00b
MR
139 if (s->lsf)
140 *frame_size = 576;
141 else
142 *frame_size = 1152;
143 break;
144 }
145
146 *sample_rate = s->sample_rate;
147 *channels = s->nb_channels;
148 *bit_rate = s->bit_rate;
0199e00b
MR
149 return s->frame_size;
150}