1 /* -*- c-basic-offset: 8; -*- */
2 /* mp3.c: libshout MP3 format handler
3 * $Id$
4 *
5 * Copyright (C) 2002-2003 the Icecast team <team@icecast.org>
6 * Copyright (C) 2015-2019 Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <shout/shout.h>
28 #include "shout_private.h"
29
30 /*
31 * MP3 frame handling courtesy of Scott Manley - may he always be Manley.
32 */
33
34 #define MPEG_MODE_MONO 3
35
36 /* -- local datatypes -- */
37 typedef struct {
38 unsigned int frames;
39 /* the number of samples for the current frame */
40 int frame_samples;
41 /* the samplerate of the current frame */
42 int frame_samplerate;
43 /* how many bytes for the rest of this frame */
44 unsigned int frame_left;
45 /* is the header bridged?? */
46 int header_bridges;
47 /* put part of header here if it spans a boundary */
48 unsigned char header_bridge[3];
49 } mp3_data_t;
50
51 typedef struct {
52 int syncword;
53 int layer;
54 int version;
55 int error_protection;
56 int bitrate_index;
57 int samplerate_index;
58 int padding;
59 int extension;
60 int mode;
61 int mode_ext;
62 int copyright;
63 int original;
64 int emphasis;
65 int stereo;
66 int bitrate;
67 unsigned int samplerate;
68 unsigned int samples;
69 unsigned int framesize;
70 } mp3_header_t;
71
72 /* -- const data -- */
73 static const unsigned int bitrate[3][3][16] =
74 {
75 {
76 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0 },
77 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0 },
78 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0 }
79 }, {
80 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0 },
81 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0 },
82 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0 }
83 }, {
84 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0 },
85 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0 },
86 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0 }
87 }
88 };
89
90 static const unsigned int samplerate[3][4] =
91 {
92 { 44100, 48000, 32000, 0 },
93 { 22050, 24000, 16000, 0 },
94 { 11025, 8000, 8000, 0 }
95 };
96
97 /* -- static prototypes -- */
98 static int send_mp3(shout_t *self, const unsigned char *data, size_t len);
99 static void close_mp3(shout_t *self);
100
101 static void parse_header(mp3_header_t *mh, uint32_t header);
102 static int mp3_header(uint32_t head, mp3_header_t *mh);
103
shout_open_mp3(shout_t * self)104 int shout_open_mp3(shout_t *self)
105 {
106 mp3_data_t *mp3_data;
107
108 if (!(mp3_data = (mp3_data_t *)calloc(1, sizeof(mp3_data_t))))
109 return SHOUTERR_MALLOC;
110
111 self->format_data = mp3_data;
112 self->send = send_mp3;
113 self->close = close_mp3;
114
115 return SHOUTERR_SUCCESS;
116 }
117
send_mp3(shout_t * self,const unsigned char * buff,size_t len)118 static int send_mp3(shout_t* self, const unsigned char* buff, size_t len)
119 {
120 mp3_data_t *mp3_data = (mp3_data_t*)self->format_data;
121 unsigned long pos;
122 uint32_t head;
123 int ret, count;
124 int start, end, error, i;
125 unsigned char *bridge_buff;
126 mp3_header_t mh;
127
128 bridge_buff = NULL;
129 pos = 0;
130 start = 0;
131 error = 0;
132 end = len - 1;
133
134 memset(&mh, 0, sizeof(mh));
135
136 /* finish the previous frame */
137 if (mp3_data->frame_left > 0) {
138 /* is the rest of the frame here? */
139 if (mp3_data->frame_left <= len) {
140 self->senttime += (int64_t)((double)mp3_data->frame_samples / (double)mp3_data->frame_samplerate * 1000000);
141 mp3_data->frames++;
142 pos += mp3_data->frame_left;
143 mp3_data->frame_left = 0;
144 } else {
145 mp3_data->frame_left -= len;
146 pos = len;
147 }
148 }
149
150 /* header was over the boundary, so build a new build a new buffer */
151 if (mp3_data->header_bridges) {
152 bridge_buff = (unsigned char*)malloc(len + mp3_data->header_bridges);
153 if (bridge_buff == NULL) {
154 return self->error = SHOUTERR_MALLOC;
155 }
156
157 bridge_buff[0] = mp3_data->header_bridge[0];
158 bridge_buff[1] = mp3_data->header_bridge[1];
159 bridge_buff[2] = mp3_data->header_bridge[2];
160
161 memcpy(&bridge_buff[mp3_data->header_bridges], buff, len);
162
163 buff = bridge_buff;
164 len += mp3_data->header_bridges;
165 end = len - 1;
166
167 mp3_data->header_bridges = 0;
168 }
169
170 /* this is the main loop
171 * we handle everything but the last 4 bytes...
172 */
173 while ((pos + 4) <= len) {
174 /* find mp3 header */
175 head = (buff[pos] << 24) |
176 (buff[pos + 1] << 16) |
177 (buff[pos + 2] << 8) |
178 (buff[pos + 3]);
179
180 /* is this a valid header? */
181 if (mp3_header(head, &mh)) {
182 if (error) {
183 start = pos;
184 end = len - 1;
185 error = 0;
186 }
187
188 mp3_data->frame_samples = mh.samples;
189 mp3_data->frame_samplerate = mh.samplerate;
190
191 /* do we have a complete frame in this buffer? */
192 if (len - pos >= mh.framesize) {
193 self->senttime += (int64_t)((double)mp3_data->frame_samples / (double)mp3_data->frame_samplerate * 1000000);
194 mp3_data->frames++;
195 pos += mh.framesize;
196 } else {
197 mp3_data->frame_left = mh.framesize - (len - pos);
198 pos = len;
199 }
200 } else {
201 /* there was an error
202 ** so we send all the valid data up to this point
203 */
204 if (!error) {
205 error = 1;
206 end = pos - 1;
207 count = end - start + 1;
208 if (count > 0) {
209 ret = shout_send_raw(self, &buff[start], count);
210 } else {
211 ret = 0;
212 }
213
214 if (ret != count) {
215 if (bridge_buff != NULL)
216 free(bridge_buff);
217 return self->error = SHOUTERR_SOCKET;
218 }
219 }
220 pos++;
221 }
222 }
223
224 /* catch the tail if there is one */
225 if ((pos > (len - 4)) && (pos < len)) {
226 end = pos - 1;
227
228 i = 0;
229 while (pos < len) {
230 mp3_data->header_bridge[i] = buff[pos];
231 pos++;
232 i++;
233 }
234 mp3_data->header_bridges = i;
235 }
236
237 if (!error) {
238 /* if there's no errors, lets send the frames */
239 count = end - start + 1;
240 if (count > 0)
241 ret = shout_send_raw(self, &buff[start], count);
242 else
243 ret = 0;
244
245 if (bridge_buff != NULL)
246 free(bridge_buff);
247
248 if (ret == count) {
249 return self->error = SHOUTERR_SUCCESS;
250 } else {
251 return self->error = SHOUTERR_SOCKET;
252 }
253 }
254
255 if (bridge_buff != NULL)
256 free(bridge_buff);
257
258 return self->error = SHOUTERR_SUCCESS;
259 }
260
parse_header(mp3_header_t * mh,uint32_t header)261 static void parse_header(mp3_header_t *mh, uint32_t header)
262 {
263 mh->syncword = (header >> 20) & 0x0fff;
264 mh->version = ((header >> 19) & 0x01) ? 0 : 1;
265
266 if ((mh->syncword & 0x01) == 0)
267 mh->version = 2;
268
269 mh->layer = 3 - ((header >> 17) & 0x03);
270 mh->error_protection = ((header >> 16) & 0x01) ? 0 : 1;
271 mh->bitrate_index = (header >> 12) & 0x0F;
272 mh->samplerate_index = (header >> 10) & 0x03;
273 mh->padding = (header >> 9) & 0x01;
274 mh->extension = (header >> 8) & 0x01;
275 mh->mode = (header >> 6) & 0x03;
276 mh->mode_ext = (header >> 4) & 0x03;
277 mh->copyright = (header >> 3) & 0x01;
278 mh->original = (header >> 2) & 0x01;
279 mh->emphasis = header & 0x03;
280
281 mh->stereo = (mh->mode == MPEG_MODE_MONO) ? 1 : 2;
282 mh->bitrate = bitrate[mh->version][mh->layer][mh->bitrate_index];
283 mh->samplerate = samplerate[mh->version][mh->samplerate_index];
284
285 if (mh->version == 0) {
286 mh->samples = 1152;
287 } else {
288 mh->samples = 576;
289 }
290
291 if (mh->samplerate)
292 mh->framesize = (mh->samples * mh->bitrate * 1000 / mh->samplerate) / 8 + mh->padding;
293 }
294
295 /* mp3 frame parsing stuff */
mp3_header(uint32_t head,mp3_header_t * mh)296 static int mp3_header(uint32_t head, mp3_header_t *mh)
297 {
298 /* fill out the header struct */
299 parse_header(mh, head);
300
301 /* check for syncword */
302 if ((mh->syncword & 0x0ffe) != 0x0ffe)
303 return 0;
304
305 /* check that layer is valid */
306 if (mh->layer == 0)
307 return 0;
308
309 /* make sure bitrate is sane */
310 if (mh->bitrate == 0)
311 return 0;
312
313 /* make sure samplerate is sane */
314 if (mh->samplerate == 0)
315 return 0;
316
317 return 1;
318 }
319
close_mp3(shout_t * self)320 static void close_mp3(shout_t *self)
321 {
322 mp3_data_t *mp3_data = (mp3_data_t*)self->format_data;
323 free(mp3_data);
324 }
325