1/*
2Copyright (c) 2012, Broadcom Europe Ltd
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the copyright holder nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27#define MPGA_HEADER_SIZE 6
28
29#define MPGA_MODE_STEREO 0
30#define MPGA_MODE_JSTEREO 1
31#define MPGA_MODE_DUAL 2
32#define MPGA_MODE_MONO 3
33
34/*****************************************************************************/
35static VC_CONTAINER_STATUS_T mpga_read_header( uint8_t frame_header[MPGA_HEADER_SIZE],
36 uint32_t *p_frame_size, unsigned int *p_frame_bitrate, unsigned int *p_version,
37 unsigned int *p_layer, unsigned int *p_sample_rate, unsigned int *p_channels,
38 unsigned int *p_frame_size_samples, unsigned int *p_offset )
39{
40 static const uint16_t mpga_bitrate[2][3][15] =
41 {{{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448}, /* MPEG1, Layer 1 */
42 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384}, /* MPEG1, Layer 2 */
43 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320}},/* MPEG1, Layer 3 */
44 {{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256}, /* MPEG2 and MPEG2.5, Layer 1 */
45 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}, /* MPEG2 and MPEG2.5, Layer 2 */
46 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}} /* MPEG2 and MPEG2.5, Layer 3 */};
47 static const uint16_t mpga_sample_rate[] = {44100, 48000, 32000};
48 static const uint16_t mpga_frame_size[] = {384, 1152, 576};
49
50 unsigned int version, layer, br_id, sr_id, emphasis;
51 unsigned int bitrate, sample_rate, padding, mode;
52
53 /* Check frame sync, 11 bits as we want to allow for MPEG2.5 */
54 if (frame_header[0] != 0xff || (frame_header[1] & 0xe0) != 0xe0)
55 return VC_CONTAINER_ERROR_FORMAT_INVALID;
56
57 version = 4 - ((frame_header[1] >> 3) & 3);
58 layer = 4 - ((frame_header[1] >> 1) & 3 );
59 br_id = (frame_header[2] >> 4) & 0xf;
60 sr_id = (frame_header[2] >> 2) & 3;
61 padding = (frame_header[2] >> 1) & 1;
62 mode = (frame_header[3] >> 6) & 3;
63 emphasis = (frame_header[3]) & 3;
64
65 /* Check for invalid values */
66 if (version == 3 || layer == 4 || br_id == 15 || sr_id == 3 || emphasis == 2)
67 return VC_CONTAINER_ERROR_FORMAT_INVALID;
68
69 if (version == 4) version = 3;
70
71 bitrate = mpga_bitrate[version == 1 ? 0 : 1][layer-1][br_id];
72 bitrate *= 1000;
73
74 sample_rate = mpga_sample_rate[sr_id];
75 sample_rate >>= (version - 1);
76
77 if (p_version) *p_version = version;
78 if (p_layer) *p_layer = layer;
79 if (p_sample_rate) *p_sample_rate = sample_rate;
80 if (p_channels) *p_channels = mode == MPGA_MODE_MONO ? 1 : 2;
81 if (p_frame_bitrate) *p_frame_bitrate = bitrate;
82 if (p_offset) *p_offset = 0;
83
84 if (p_frame_size_samples)
85 {
86 *p_frame_size_samples = mpga_frame_size[layer - 1];
87 if (version == 1 && layer == 3) *p_frame_size_samples <<= 1;
88 }
89
90 if (!p_frame_size)
91 return VC_CONTAINER_SUCCESS;
92
93 if (!bitrate)
94 *p_frame_size = 0;
95 else if (layer == 1)
96 *p_frame_size = (padding + bitrate * 12 / sample_rate) * 4;
97 else if (layer == 2)
98 *p_frame_size = padding + bitrate * 144 / sample_rate;
99 else
100 *p_frame_size = padding + bitrate * (version == 1 ? 144 : 72) / sample_rate;
101
102 return VC_CONTAINER_SUCCESS;
103}
104
105/*****************************************************************************/
106static VC_CONTAINER_STATUS_T adts_read_header( uint8_t frame_header[MPGA_HEADER_SIZE],
107 uint32_t *p_frame_size, unsigned int *p_frame_bitrate, unsigned int *p_version,
108 unsigned int *p_layer, unsigned int *p_sample_rate, unsigned int *p_channels,
109 unsigned int *p_frame_size_samples, unsigned int *p_offset )
110{
111 static const unsigned int adts_sample_rate[16] =
112 {96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350};
113 unsigned int profile, sr_id, bitrate, sample_rate, frame_size, channels, crc;
114 unsigned int frame_size_samples = 1024;
115
116 /* Check frame sync (12 bits) */
117 if (frame_header[0] != 0xff || (frame_header[1] & 0xf0) != 0xf0)
118 return VC_CONTAINER_ERROR_FORMAT_INVALID;
119
120 /* Layer must be 0 */
121 if ((frame_header[1] >> 1) & 3)
122 return VC_CONTAINER_ERROR_FORMAT_INVALID;
123
124 crc = !(frame_header[1] & 0x1);
125 profile = (frame_header[2] >> 6) + 1; /* MPEG-4 Audio Object Type */
126
127 sr_id = (frame_header[2] >> 2) & 0xf;
128 sample_rate = adts_sample_rate[sr_id];
129 channels = ((frame_header[2] & 0x1) << 2) | ((frame_header[3] >> 6) & 0x3);
130 frame_size = ((frame_header[3] & 0x03) << 11) | (frame_header[4] << 3) | (frame_header[5] >> 5);
131
132 if (!sample_rate || !channels || !frame_size)
133 return VC_CONTAINER_ERROR_FORMAT_INVALID;
134
135 bitrate = frame_size * 8 * sample_rate / frame_size_samples;
136
137 if (p_version) *p_version = profile;
138 if (p_layer) *p_layer = 0;
139 if (p_sample_rate) *p_sample_rate = sample_rate;
140 if (p_channels) *p_channels = channels;
141 if (p_frame_bitrate) *p_frame_bitrate = bitrate;
142 if (p_frame_size) *p_frame_size = frame_size;
143 if (p_frame_size_samples) *p_frame_size_samples = frame_size_samples;
144 if (p_offset) *p_offset = crc ? 9 : 7;
145
146 return VC_CONTAINER_SUCCESS;
147}
148