FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 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/common.h"
28 
29 #include "avcodec.h"
30 #include "mpegaudio.h"
31 #include "mpegaudiodata.h"
32 #include "mpegaudiodecheader.h"
33 
34 
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;
50  if (sample_rate_index >= FF_ARRAY_ELEMS(avpriv_mpa_freq_tab))
51  sample_rate_index = 0;
52  sample_rate = avpriv_mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
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) {
73  frame_size = avpriv_mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
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)
97  av_dlog(NULL, "layer%d, %d Hz, %d kbits/s, ",
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)
102  av_dlog(NULL, "ms-");
103  if (s->mode_ext & MODE_EXT_I_STEREO)
104  av_dlog(NULL, "i-");
105  }
106  av_dlog(NULL, "stereo");
107  } else {
108  av_dlog(NULL, "mono");
109  }
110  av_dlog(NULL, "\n");
111 #endif
112  return 0;
113 }
114 
115 int avpriv_mpa_decode_header2(uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate, enum AVCodecID *codec_id)
116 {
117  MPADecodeHeader s1, *s = &s1;
118 
119  if (ff_mpa_check_header(head) != 0)
120  return -1;
121 
122  if (avpriv_mpegaudio_decode_header(s, head) != 0) {
123  return -1;
124  }
125 
126  switch(s->layer) {
127  case 1:
128  *codec_id = AV_CODEC_ID_MP1;
129  *frame_size = 384;
130  break;
131  case 2:
132  *codec_id = AV_CODEC_ID_MP2;
133  *frame_size = 1152;
134  break;
135  default:
136  case 3:
137  *codec_id = AV_CODEC_ID_MP3;
138  if (s->lsf)
139  *frame_size = 576;
140  else
141  *frame_size = 1152;
142  break;
143  }
144 
145  *sample_rate = s->sample_rate;
146  *channels = s->nb_channels;
147  *bit_rate = s->bit_rate;
148  return s->frame_size;
149 }
150 
151 int avpriv_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate)
152 {
153  return avpriv_mpa_decode_header2(head, sample_rate, channels, frame_size, bit_rate, &avctx->codec_id);
154 }