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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 //#define DEBUG
28 #include "libavutil/common.h"
29 
30 #include "avcodec.h"
31 #include "mpegaudio.h"
32 #include "mpegaudiodata.h"
33 #include "mpegaudiodecheader.h"
34 
35 
37 {
38  int sample_rate, frame_size, mpeg25, padding;
39  int sample_rate_index, bitrate_index;
40  if (header & (1<<20)) {
41  s->lsf = (header & (1<<19)) ? 0 : 1;
42  mpeg25 = 0;
43  } else {
44  s->lsf = 1;
45  mpeg25 = 1;
46  }
47 
48  s->layer = 4 - ((header >> 17) & 3);
49  /* extract frequency */
50  sample_rate_index = (header >> 10) & 3;
51  if (sample_rate_index >= FF_ARRAY_ELEMS(avpriv_mpa_freq_tab))
52  sample_rate_index = 0;
53  sample_rate = avpriv_mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
54  sample_rate_index += 3 * (s->lsf + mpeg25);
55  s->sample_rate_index = sample_rate_index;
56  s->error_protection = ((header >> 16) & 1) ^ 1;
57  s->sample_rate = sample_rate;
58 
59  bitrate_index = (header >> 12) & 0xf;
60  padding = (header >> 9) & 1;
61  //extension = (header >> 8) & 1;
62  s->mode = (header >> 6) & 3;
63  s->mode_ext = (header >> 4) & 3;
64  //copyright = (header >> 3) & 1;
65  //original = (header >> 2) & 1;
66  //emphasis = header & 3;
67 
68  if (s->mode == MPA_MONO)
69  s->nb_channels = 1;
70  else
71  s->nb_channels = 2;
72 
73  if (bitrate_index != 0) {
74  frame_size = avpriv_mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
75  s->bit_rate = frame_size * 1000;
76  switch(s->layer) {
77  case 1:
78  frame_size = (frame_size * 12000) / sample_rate;
79  frame_size = (frame_size + padding) * 4;
80  break;
81  case 2:
82  frame_size = (frame_size * 144000) / sample_rate;
83  frame_size += padding;
84  break;
85  default:
86  case 3:
87  frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
88  frame_size += padding;
89  break;
90  }
91  s->frame_size = frame_size;
92  } else {
93  /* if no frame size computed, signal it */
94  return 1;
95  }
96 
97 #if defined(DEBUG)
98  av_dlog(NULL, "layer%d, %d Hz, %d kbits/s, ",
99  s->layer, s->sample_rate, s->bit_rate);
100  if (s->nb_channels == 2) {
101  if (s->layer == 3) {
102  if (s->mode_ext & MODE_EXT_MS_STEREO)
103  av_dlog(NULL, "ms-");
104  if (s->mode_ext & MODE_EXT_I_STEREO)
105  av_dlog(NULL, "i-");
106  }
107  av_dlog(NULL, "stereo");
108  } else {
109  av_dlog(NULL, "mono");
110  }
111  av_dlog(NULL, "\n");
112 #endif
113  return 0;
114 }
115 
116 int avpriv_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate)
117 {
118  MPADecodeHeader s1, *s = &s1;
119 
120  if (ff_mpa_check_header(head) != 0)
121  return -1;
122 
123  if (avpriv_mpegaudio_decode_header(s, head) != 0) {
124  return -1;
125  }
126 
127  switch(s->layer) {
128  case 1:
129  avctx->codec_id = AV_CODEC_ID_MP1;
130  *frame_size = 384;
131  break;
132  case 2:
133  avctx->codec_id = AV_CODEC_ID_MP2;
134  *frame_size = 1152;
135  break;
136  default:
137  case 3:
138  avctx->codec_id = AV_CODEC_ID_MP3;
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;
149  return s->frame_size;
150 }