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 "internal.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  ff_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  ff_dlog(NULL, "ms-");
104  if (s->mode_ext & MODE_EXT_I_STEREO)
105  ff_dlog(NULL, "i-");
106  }
107  ff_dlog(NULL, "stereo");
108  } else {
109  ff_dlog(NULL, "mono");
110  }
111  ff_dlog(NULL, "\n");
112 #endif
113  return 0;
114 }
115 
116 int avpriv_mpa_decode_header2(uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate, enum AVCodecID *codec_id)
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  *codec_id = AV_CODEC_ID_MP1;
130  *frame_size = 384;
131  break;
132  case 2:
133  *codec_id = AV_CODEC_ID_MP2;
134  *frame_size = 1152;
135  break;
136  default:
137  case 3:
138  if (*codec_id != AV_CODEC_ID_MP3ADU)
139  *codec_id = AV_CODEC_ID_MP3;
140  if (s->lsf)
141  *frame_size = 576;
142  else
143  *frame_size = 1152;
144  break;
145  }
146 
147  *sample_rate = s->sample_rate;
148  *channels = s->nb_channels;
149  *bit_rate = s->bit_rate;
150  return s->frame_size;
151 }
152 
153 int avpriv_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate)
154 {
155  return avpriv_mpa_decode_header2(head, sample_rate, channels, frame_size, bit_rate, &avctx->codec_id);
156 }
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
#define FF_ARRAY_ELEMS(a)
mpeg audio layer common tables.
const uint16_t avpriv_mpa_freq_tab[3]
Definition: mpegaudiodata.c:40
int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header)
static const uint8_t header[24]
Definition: sdr2.c:67
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:102
#define MODE_EXT_MS_STEREO
Definition: mpegaudiodata.h:34
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:421
static int ff_mpa_check_header(uint32_t header)
enum AVCodecID codec_id
Definition: mov_chan.c:433
Libavcodec external API header.
int avpriv_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate)
int avpriv_mpa_decode_header2(uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate, enum AVCodecID *codec_id)
sample_rate
int frame_size
Definition: mxfenc.c:1803
#define ff_dlog(ctx,...)
Definition: internal.h:54
enum AVCodecID codec_id
Definition: avcodec.h:1258
main external API structure.
Definition: avcodec.h:1241
#define MODE_EXT_I_STEREO
Definition: mpegaudiodata.h:35
#define MPA_MONO
Definition: mpegaudio.h:48
#define s1
Definition: regdef.h:38
MPEG Audio header decoder.
common internal api header.
common internal and external API header
mpeg audio declarations for both encoder and decoder.
const uint16_t avpriv_mpa_bitrate_tab[2][3][15]
Definition: mpegaudiodata.c:30