Update to V9.1.0 (2010-03)
[obnox/wireshark/wip.git] / wsutil / mpeg-audio.c
1 /* mpeg-audio.c
2  *
3  * MPEG Audio header dissection
4  * Written by Shaun Jackman <sjackman@gmail.com>
5  * Copyright 2007 Shaun Jackman
6  *
7  * $Id$
8  *
9  * Wiretap Library
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #include "mpeg-audio.h"
26
27 static const int mpa_versions[4] = { 2, -1, 1, 0 };
28 static const int mpa_layers[4] = { -1, 2, 1, 0 };
29
30 static const unsigned mpa_samples_data[3][3] = {
31         { 384, 1152, 1152 },
32         { 384, 1152, 576 },
33         { 384, 1152, 576 },
34 };
35
36 static const unsigned mpa_bitrates[3][3][16] = { /* kb/s */
37   {
38         { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448 },
39         { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 384 },
40         { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320 },
41   },
42   {
43         { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192, 224, 256 },
44         { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160 },
45         { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160 },
46   },
47   {
48         { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192, 224, 256 },
49         { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160 },
50         { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160 },
51   },
52 };
53
54 static const unsigned mpa_frequencies[3][4] = {
55         { 44100, 48000, 32000 },
56         { 22050, 24000, 16000 },
57         { 11025, 12000, 8000 },
58 };
59
60 static const unsigned mpa_padding_data[3] = { 4, 1, 1 };
61
62 int
63 mpa_version(const struct mpa *mpa)
64 {
65         return mpa_versions[mpa->version];
66 }
67
68 int
69 mpa_layer(const struct mpa *mpa)
70 {
71         return mpa_layers[mpa->layer];
72 }
73
74 unsigned
75 mpa_samples(const struct mpa *mpa)
76 {
77         return mpa_samples_data[mpa_versions[mpa->version]][mpa_layer(mpa)];
78 }
79
80 unsigned
81 mpa_bitrate(const struct mpa *mpa)
82 {
83         return (1000 * (mpa_bitrates[mpa_versions[mpa->version]][mpa_layers[mpa->layer]][mpa->bitrate]));
84 }
85
86 unsigned
87 mpa_frequency(const struct mpa *mpa)
88 {
89         return(mpa_frequencies[mpa_versions[mpa->version]][mpa->frequency]);
90 }
91
92 unsigned
93 mpa_padding(const struct mpa *mpa)
94 {
95         return(mpa->padding ? mpa_padding_data[mpa_layers[mpa->layer]] : 0);
96 }