Mercurial > audlegacy
annotate Plugins/Input/mpg123/mpg123.c @ 761:363f8644bce7 trunk
[svn] - sanity checks here
author | nenolod |
---|---|
date | Tue, 28 Feb 2006 14:56:30 -0800 |
parents | 147e844e16cb |
children | a22aa7bfa108 |
rev | line source |
---|---|
61 | 1 #include "mpg123.h" |
354
e2775c9b8b13
[svn] very rudementary http stream detection support for mpg123-clone.
nenolod
parents:
353
diff
changeset
|
2 #include "common.h" |
61 | 3 |
4 #include <glib.h> | |
5 #include <glib/gi18n.h> | |
6 #include <gtk/gtk.h> | |
7 #include <stdlib.h> | |
8 #include <string.h> | |
354
e2775c9b8b13
[svn] very rudementary http stream detection support for mpg123-clone.
nenolod
parents:
353
diff
changeset
|
9 #include <unistd.h> |
e2775c9b8b13
[svn] very rudementary http stream detection support for mpg123-clone.
nenolod
parents:
353
diff
changeset
|
10 |
e2775c9b8b13
[svn] very rudementary http stream detection support for mpg123-clone.
nenolod
parents:
353
diff
changeset
|
11 #include <fcntl.h> |
e2775c9b8b13
[svn] very rudementary http stream detection support for mpg123-clone.
nenolod
parents:
353
diff
changeset
|
12 #include <unistd.h> |
e2775c9b8b13
[svn] very rudementary http stream detection support for mpg123-clone.
nenolod
parents:
353
diff
changeset
|
13 #include <errno.h> |
e2775c9b8b13
[svn] very rudementary http stream detection support for mpg123-clone.
nenolod
parents:
353
diff
changeset
|
14 #include <sys/types.h> |
e2775c9b8b13
[svn] very rudementary http stream detection support for mpg123-clone.
nenolod
parents:
353
diff
changeset
|
15 #include <sys/socket.h> |
e2775c9b8b13
[svn] very rudementary http stream detection support for mpg123-clone.
nenolod
parents:
353
diff
changeset
|
16 #include <sys/time.h> |
e2775c9b8b13
[svn] very rudementary http stream detection support for mpg123-clone.
nenolod
parents:
353
diff
changeset
|
17 #include <netinet/in.h> |
e2775c9b8b13
[svn] very rudementary http stream detection support for mpg123-clone.
nenolod
parents:
353
diff
changeset
|
18 #include <arpa/inet.h> |
e2775c9b8b13
[svn] very rudementary http stream detection support for mpg123-clone.
nenolod
parents:
353
diff
changeset
|
19 #include <netdb.h> |
61 | 20 |
21 #include <libaudacious/util.h> | |
22 #include <libaudacious/configdb.h> | |
23 #include <libaudacious/vfs.h> | |
24 #include <libaudacious/titlestring.h> | |
25 | |
26 #include "audacious/util.h" | |
27 | |
28 static const long outscale = 32768; | |
29 | |
354
e2775c9b8b13
[svn] very rudementary http stream detection support for mpg123-clone.
nenolod
parents:
353
diff
changeset
|
30 #define BUFSIZE_X 2048 |
e2775c9b8b13
[svn] very rudementary http stream detection support for mpg123-clone.
nenolod
parents:
353
diff
changeset
|
31 |
61 | 32 static struct frame fr, temp_fr; |
33 | |
34 PlayerInfo *mpg123_info = NULL; | |
35 static GThread *decode_thread; | |
36 | |
37 static gboolean audio_error = FALSE, output_opened = FALSE, dopause = FALSE; | |
38 gint mpg123_bitrate, mpg123_frequency, mpg123_length, mpg123_layer, | |
39 mpg123_lsf; | |
40 gchar *mpg123_title = NULL, *mpg123_filename = NULL; | |
41 static int disp_bitrate, skip_frames = 0; | |
42 static int cpu_fflags, cpu_efflags; | |
43 gboolean mpg123_stereo, mpg123_mpeg25; | |
44 int mpg123_mode; | |
45 | |
46 gchar **mpg123_id3_encoding_list = NULL; | |
47 | |
48 const char *mpg123_id3_genres[GENRE_MAX] = { | |
49 N_("Blues"), N_("Classic Rock"), N_("Country"), N_("Dance"), | |
50 N_("Disco"), N_("Funk"), N_("Grunge"), N_("Hip-Hop"), | |
51 N_("Jazz"), N_("Metal"), N_("New Age"), N_("Oldies"), | |
52 N_("Other"), N_("Pop"), N_("R&B"), N_("Rap"), N_("Reggae"), | |
53 N_("Rock"), N_("Techno"), N_("Industrial"), N_("Alternative"), | |
54 N_("Ska"), N_("Death Metal"), N_("Pranks"), N_("Soundtrack"), | |
55 N_("Euro-Techno"), N_("Ambient"), N_("Trip-Hop"), N_("Vocal"), | |
56 N_("Jazz+Funk"), N_("Fusion"), N_("Trance"), N_("Classical"), | |
57 N_("Instrumental"), N_("Acid"), N_("House"), N_("Game"), | |
58 N_("Sound Clip"), N_("Gospel"), N_("Noise"), N_("AlternRock"), | |
59 N_("Bass"), N_("Soul"), N_("Punk"), N_("Space"), | |
60 N_("Meditative"), N_("Instrumental Pop"), | |
61 N_("Instrumental Rock"), N_("Ethnic"), N_("Gothic"), | |
62 N_("Darkwave"), N_("Techno-Industrial"), N_("Electronic"), | |
63 N_("Pop-Folk"), N_("Eurodance"), N_("Dream"), | |
64 N_("Southern Rock"), N_("Comedy"), N_("Cult"), | |
65 N_("Gangsta Rap"), N_("Top 40"), N_("Christian Rap"), | |
66 N_("Pop/Funk"), N_("Jungle"), N_("Native American"), | |
67 N_("Cabaret"), N_("New Wave"), N_("Psychedelic"), N_("Rave"), | |
68 N_("Showtunes"), N_("Trailer"), N_("Lo-Fi"), N_("Tribal"), | |
69 N_("Acid Punk"), N_("Acid Jazz"), N_("Polka"), N_("Retro"), | |
70 N_("Musical"), N_("Rock & Roll"), N_("Hard Rock"), N_("Folk"), | |
71 N_("Folk/Rock"), N_("National Folk"), N_("Swing"), | |
72 N_("Fast-Fusion"), N_("Bebob"), N_("Latin"), N_("Revival"), | |
73 N_("Celtic"), N_("Bluegrass"), N_("Avantgarde"), | |
74 N_("Gothic Rock"), N_("Progressive Rock"), | |
75 N_("Psychedelic Rock"), N_("Symphonic Rock"), N_("Slow Rock"), | |
76 N_("Big Band"), N_("Chorus"), N_("Easy Listening"), | |
77 N_("Acoustic"), N_("Humour"), N_("Speech"), N_("Chanson"), | |
78 N_("Opera"), N_("Chamber Music"), N_("Sonata"), N_("Symphony"), | |
79 N_("Booty Bass"), N_("Primus"), N_("Porn Groove"), | |
80 N_("Satire"), N_("Slow Jam"), N_("Club"), N_("Tango"), | |
81 N_("Samba"), N_("Folklore"), N_("Ballad"), N_("Power Ballad"), | |
82 N_("Rhythmic Soul"), N_("Freestyle"), N_("Duet"), | |
83 N_("Punk Rock"), N_("Drum Solo"), N_("A Cappella"), | |
84 N_("Euro-House"), N_("Dance Hall"), N_("Goa"), | |
85 N_("Drum & Bass"), N_("Club-House"), N_("Hardcore"), | |
86 N_("Terror"), N_("Indie"), N_("BritPop"), N_("Negerpunk"), | |
87 N_("Polsk Punk"), N_("Beat"), N_("Christian Gangsta Rap"), | |
88 N_("Heavy Metal"), N_("Black Metal"), N_("Crossover"), | |
89 N_("Contemporary Christian"), N_("Christian Rock"), | |
90 N_("Merengue"), N_("Salsa"), N_("Thrash Metal"), | |
91 N_("Anime"), N_("JPop"), N_("Synthpop") | |
92 }; | |
93 | |
94 double | |
95 mpg123_compute_tpf(struct frame *fr) | |
96 { | |
97 const int bs[4] = { 0, 384, 1152, 1152 }; | |
98 double tpf; | |
99 | |
100 tpf = bs[fr->lay]; | |
101 tpf /= mpg123_freqs[fr->sampling_frequency] << (fr->lsf); | |
102 return tpf; | |
103 } | |
104 | |
105 static void | |
106 set_synth_functions(struct frame *fr) | |
107 { | |
108 typedef int (*func) (real *, int, unsigned char *, int *); | |
109 typedef int (*func_mono) (real *, unsigned char *, int *); | |
110 typedef void (*func_dct36) (real *, real *, real *, real *, real *); | |
111 | |
112 int ds = fr->down_sample; | |
113 int p8 = 0; | |
114 | |
115 static func funcs[][3] = { | |
116 {mpg123_synth_1to1, | |
117 mpg123_synth_2to1, | |
118 mpg123_synth_4to1}, | |
119 {mpg123_synth_1to1_8bit, | |
120 mpg123_synth_2to1_8bit, | |
121 mpg123_synth_4to1_8bit}, | |
122 }; | |
123 | |
124 static func_mono funcs_mono[2][4] = { | |
125 {mpg123_synth_1to1_mono, | |
126 mpg123_synth_2to1_mono, | |
127 mpg123_synth_4to1_mono}, | |
128 {mpg123_synth_1to1_8bit_mono, | |
129 mpg123_synth_2to1_8bit_mono, | |
130 mpg123_synth_4to1_8bit_mono} | |
131 }; | |
132 | |
133 if (mpg123_cfg.resolution == 8) | |
134 p8 = 1; | |
135 fr->synth = funcs[p8][ds]; | |
136 fr->synth_mono = funcs_mono[p8][ds]; | |
137 fr->synth_type = SYNTH_FPU; | |
138 | |
139 if (p8) { | |
140 mpg123_make_conv16to8_table(); | |
141 } | |
142 } | |
143 | |
144 static void | |
145 init(void) | |
146 { | |
147 ConfigDb *db; | |
148 | |
149 mpg123_make_decode_tables(outscale); | |
150 | |
151 mpg123_cfg.resolution = 16; | |
152 mpg123_cfg.channels = 2; | |
153 mpg123_cfg.downsample = 0; | |
154 mpg123_cfg.http_buffer_size = 128; | |
155 mpg123_cfg.http_prebuffer = 25; | |
156 mpg123_cfg.proxy_port = 8080; | |
157 mpg123_cfg.proxy_use_auth = FALSE; | |
158 mpg123_cfg.proxy_user = NULL; | |
159 mpg123_cfg.proxy_pass = NULL; | |
160 mpg123_cfg.use_udp_channel = TRUE; | |
161 mpg123_cfg.title_override = FALSE; | |
162 mpg123_cfg.disable_id3v2 = FALSE; | |
163 mpg123_cfg.default_synth = SYNTH_AUTO; | |
164 | |
165 mpg123_cfg.title_encoding_enabled = FALSE; | |
166 mpg123_cfg.title_encoding = NULL; | |
167 | |
168 db = bmp_cfg_db_open(); | |
169 | |
170 bmp_cfg_db_get_int(db, "MPG123", "resolution", &mpg123_cfg.resolution); | |
171 bmp_cfg_db_get_int(db, "MPG123", "channels", &mpg123_cfg.channels); | |
172 bmp_cfg_db_get_int(db, "MPG123", "downsample", &mpg123_cfg.downsample); | |
173 bmp_cfg_db_get_int(db, "MPG123", "http_buffer_size", | |
174 &mpg123_cfg.http_buffer_size); | |
175 bmp_cfg_db_get_int(db, "MPG123", "http_prebuffer", | |
176 &mpg123_cfg.http_prebuffer); | |
177 bmp_cfg_db_get_bool(db, "MPG123", "save_http_stream", | |
178 &mpg123_cfg.save_http_stream); | |
179 if (!bmp_cfg_db_get_string | |
180 (db, "MPG123", "save_http_path", &mpg123_cfg.save_http_path)) | |
181 mpg123_cfg.save_http_path = g_strdup(g_get_home_dir()); | |
182 | |
183 bmp_cfg_db_get_bool(db, "MPG123", "use_udp_channel", | |
184 &mpg123_cfg.use_udp_channel); | |
185 | |
186 bmp_cfg_db_get_bool(db, "MPG123", "use_proxy", &mpg123_cfg.use_proxy); | |
187 if (!bmp_cfg_db_get_string | |
188 (db, "MPG123", "proxy_host", &mpg123_cfg.proxy_host)) | |
189 mpg123_cfg.proxy_host = g_strdup("localhost"); | |
190 bmp_cfg_db_get_int(db, "MPG123", "proxy_port", &mpg123_cfg.proxy_port); | |
191 bmp_cfg_db_get_bool(db, "MPG123", "proxy_use_auth", | |
192 &mpg123_cfg.proxy_use_auth); | |
193 bmp_cfg_db_get_string(db, "MPG123", "proxy_user", &mpg123_cfg.proxy_user); | |
194 bmp_cfg_db_get_string(db, "MPG123", "proxy_pass", &mpg123_cfg.proxy_pass); | |
195 | |
196 bmp_cfg_db_get_bool(db, "MPG123", "title_override", | |
197 &mpg123_cfg.title_override); | |
198 bmp_cfg_db_get_bool(db, "MPG123", "disable_id3v2", | |
199 &mpg123_cfg.disable_id3v2); | |
200 if (!bmp_cfg_db_get_string | |
201 (db, "MPG123", "id3_format", &mpg123_cfg.id3_format)) | |
202 mpg123_cfg.id3_format = g_strdup("%p - %t"); | |
203 bmp_cfg_db_get_int(db, "MPG123", "default_synth", | |
204 &mpg123_cfg.default_synth); | |
205 | |
206 bmp_cfg_db_get_bool(db, "MPG123", "title_encoding_enabled", &mpg123_cfg.title_encoding_enabled); | |
207 bmp_cfg_db_get_string(db, "MPG123", "title_encoding", &mpg123_cfg.title_encoding); | |
208 if (mpg123_cfg.title_encoding_enabled) | |
209 mpg123_id3_encoding_list = g_strsplit_set(mpg123_cfg.title_encoding, ENCODING_SEPARATOR, 0); | |
210 | |
211 bmp_cfg_db_close(db); | |
212 | |
213 if (mpg123_cfg.resolution != 16 && mpg123_cfg.resolution != 8) | |
214 mpg123_cfg.resolution = 16; | |
215 | |
216 mpg123_cfg.channels = CLAMP(mpg123_cfg.channels, 0, 2); | |
217 mpg123_cfg.downsample = CLAMP(mpg123_cfg.downsample, 0, 2); | |
218 mpg123_getcpuflags(&cpu_fflags, &cpu_efflags); | |
219 } | |
220 | |
221 static void | |
222 cleanup(void) | |
223 { | |
224 g_strfreev(mpg123_id3_encoding_list); | |
225 } | |
226 | |
227 static guint32 | |
228 convert_to_header(guint8 * buf) | |
229 { | |
230 return (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3]; | |
231 } | |
232 | |
233 | |
234 #define DET_BUF_SIZE 1024 | |
235 | |
236 static gboolean | |
237 mpg123_detect_by_content(char *filename) | |
238 { | |
239 VFSFile *file; | |
240 guchar tmp[4]; | |
241 guint32 head; | |
242 struct frame fr; | |
243 guchar buf[DET_BUF_SIZE]; | |
244 int in_buf, i; | |
245 gboolean ret = FALSE; | |
246 | |
247 if ((file = vfs_fopen(filename, "rb")) == NULL) | |
248 return FALSE; | |
249 if (vfs_fread(tmp, 1, 4, file) != 4) | |
250 goto done; | |
251 head = convert_to_header(tmp); | |
252 while (!mpg123_head_check(head)) { | |
253 /* | |
254 * The mpeg-stream can start anywhere in the file, | |
255 * so we check the entire file | |
256 */ | |
257 /* Optimize this */ | |
258 in_buf = vfs_fread(buf, 1, DET_BUF_SIZE, file); | |
259 if (in_buf == 0) | |
260 goto done; | |
261 | |
262 for (i = 0; i < in_buf; i++) { | |
263 head <<= 8; | |
264 head |= buf[i]; | |
265 if (mpg123_head_check(head)) { | |
266 vfs_fseek(file, i + 1 - in_buf, SEEK_CUR); | |
267 break; | |
268 } | |
269 } | |
270 } | |
271 if (mpg123_decode_header(&fr, head)) { | |
272 /* | |
273 * We found something which looks like a MPEG-header. | |
274 * We check the next frame too, to be sure | |
275 */ | |
276 | |
277 if (vfs_fseek(file, fr.framesize, SEEK_CUR) != 0) | |
278 goto done; | |
279 if (vfs_fread(tmp, 1, 4, file) != 4) | |
280 goto done; | |
281 head = convert_to_header(tmp); | |
282 if (mpg123_head_check(head) && mpg123_decode_header(&fr, head)) | |
283 ret = TRUE; | |
284 } | |
285 | |
286 done: | |
287 vfs_fclose(file); | |
288 return ret; | |
289 } | |
290 | |
291 static int | |
292 is_our_file(char *filename) | |
293 { | |
653 | 294 gchar *ext = strrchr(filename, '.'); |
295 | |
761 | 296 if (!strncasecmp(filename, "http://", 7) && (ext && strncasecmp(ext, ".ogg", 4))) |
684
d991052592f6
[svn] - Roll back broken HTTP-based stream detection code.
nenolod
parents:
653
diff
changeset
|
297 return TRUE; |
761 | 298 else if (ext && strncasecmp(ext, ".mp3", 4)) /* If extension ends in .mp3, let it be --nenolod */ |
61 | 299 return (mpg123_detect_by_content(filename)); |
653 | 300 |
301 return TRUE; /* Why? The file ends in .mp3. Lalala. */ | |
61 | 302 } |
303 | |
304 static void | |
305 play_frame(struct frame *fr) | |
306 { | |
307 if (fr->error_protection) { | |
308 bsi.wordpointer += 2; | |
309 /* mpg123_getbits(16); *//* skip crc */ | |
310 } | |
311 if (!fr->do_layer(fr)) { | |
312 skip_frames = 2; | |
313 mpg123_info->output_audio = FALSE; | |
314 } | |
315 else { | |
316 if (!skip_frames) | |
317 mpg123_info->output_audio = TRUE; | |
318 else | |
319 skip_frames--; | |
320 } | |
321 } | |
322 | |
323 static const char * | |
324 get_id3_genre(unsigned char genre_code) | |
325 { | |
326 if (genre_code < GENRE_MAX) | |
327 return gettext(mpg123_id3_genres[genre_code]); | |
328 | |
329 return ""; | |
330 } | |
331 | |
332 guint | |
333 mpg123_strip_spaces(char *src, size_t n) | |
334 { | |
335 gchar *space = NULL, /* last space in src */ | |
336 *start = src; | |
337 | |
338 while (n--) | |
339 switch (*src++) { | |
340 case '\0': | |
341 n = 0; /* breaks out of while loop */ | |
342 | |
343 src--; | |
344 break; | |
345 case ' ': | |
346 if (space == NULL) | |
347 space = src - 1; | |
348 break; | |
349 default: | |
350 space = NULL; /* don't terminate intermediate spaces */ | |
351 | |
352 break; | |
353 } | |
354 if (space != NULL) { | |
355 src = space; | |
356 *src = '\0'; | |
357 } | |
358 return src - start; | |
359 } | |
360 | |
361 /* | |
362 * Function extname (filename) | |
363 * | |
364 * Return pointer within filename to its extenstion, or NULL if | |
365 * filename has no extension. | |
366 * | |
367 */ | |
368 static gchar * | |
369 extname(const char *filename) | |
370 { | |
371 gchar *ext = strrchr(filename, '.'); | |
372 | |
373 if (ext != NULL) | |
374 ++ext; | |
375 | |
376 return ext; | |
377 } | |
378 | |
379 /* | |
380 * Function id3v1_to_id3v2 (v1, v2) | |
381 * | |
382 * Convert ID3v1 tag `v1' to ID3v2 tag `v2'. | |
383 * | |
384 */ | |
385 void | |
386 mpg123_id3v1_to_id3v2(struct id3v1tag_t *v1, struct id3tag_t *v2) | |
387 { | |
388 memset(v2, 0, sizeof(struct id3tag_t)); | |
389 strncpy(v2->title, v1->title, 30); | |
390 strncpy(v2->artist, v1->artist, 30); | |
391 strncpy(v2->album, v1->album, 30); | |
392 strncpy(v2->comment, v1->u.v1_0.comment, 30); | |
393 strncpy(v2->genre, get_id3_genre(v1->genre), sizeof(v2->genre)); | |
394 g_strstrip(v2->title); | |
395 g_strstrip(v2->artist); | |
396 g_strstrip(v2->album); | |
397 g_strstrip(v2->comment); | |
398 g_strstrip(v2->genre); | |
636 | 399 { |
400 char y[5]; | |
401 memcpy(y, v1->year, 4); y[4]=0; | |
402 v2->year = atoi(y); | |
403 } | |
61 | 404 |
405 /* Check for v1.1 tags. */ | |
406 if (v1->u.v1_1.__zero == 0) | |
407 v2->track_number = v1->u.v1_1.track_number; | |
408 else | |
409 v2->track_number = 0; | |
410 } | |
411 | |
412 static char * | |
413 mpg123_getstr(char *str) | |
414 { | |
415 if (str && strlen(str) > 0) | |
416 return str; | |
417 return NULL; | |
418 } | |
419 | |
420 static gchar * | |
421 convert_id3_title(gchar * title) | |
422 { | |
423 gchar **encoding = mpg123_id3_encoding_list; | |
424 gchar *new_title = NULL; | |
425 | |
426 if (g_utf8_validate(title, -1, NULL)) | |
427 return title; | |
428 | |
429 while (*encoding && !new_title) { | |
430 new_title = g_convert(title, strlen(title), "UTF-8", *encoding++, | |
431 NULL, NULL, NULL); | |
432 } | |
433 | |
434 if (new_title) { | |
435 g_free(title); | |
436 return new_title; | |
437 } | |
438 | |
439 /* FIXME: We're relying on BMP core to provide fallback | |
440 * conversion */ | |
441 return title; | |
442 } | |
443 | |
444 /* | |
445 * Function mpg123_format_song_title (tag, filename) | |
446 * | |
447 * Create song title according to `tag' and/or `filename' and | |
448 * return it. The title must be subsequently freed using g_free(). | |
449 * | |
450 */ | |
451 gchar * | |
452 mpg123_format_song_title(struct id3tag_t * tag, gchar * filename) | |
453 { | |
454 gchar *title = NULL; | |
455 TitleInput *input; | |
456 | |
457 input = bmp_title_input_new(); | |
458 | |
459 if (tag) { | |
460 input->performer = mpg123_getstr(tag->artist); | |
461 input->album_name = mpg123_getstr(tag->album); | |
462 input->track_name = mpg123_getstr(tag->title); | |
463 input->year = tag->year; | |
464 input->track_number = tag->track_number; | |
465 input->genre = mpg123_getstr(tag->genre); | |
466 input->comment = mpg123_getstr(tag->comment); | |
467 } | |
468 | |
469 input->file_name = g_path_get_basename(filename); | |
470 input->file_path = g_path_get_dirname(filename); | |
471 input->file_ext = extname(filename); | |
472 | |
473 title = xmms_get_titlestring(mpg123_cfg.title_override ? | |
474 mpg123_cfg.id3_format : | |
475 xmms_get_gentitle_format(), input); | |
476 | |
477 if (!title) { | |
478 /* Format according to filename. */ | |
479 title = g_path_get_basename(filename); | |
480 if (extname(title)) | |
481 *(extname(title) - 1) = '\0'; /* removes period */ | |
482 } | |
483 | |
484 g_free(input->file_path); | |
485 g_free(input->file_name); | |
486 g_free(input); | |
487 | |
488 if (mpg123_cfg.title_encoding_enabled) | |
489 title = convert_id3_title(title); | |
490 | |
491 return title; | |
492 } | |
493 | |
494 /* | |
495 * Function mpg123_get_id3v2 (id3d, tag) | |
496 * | |
497 * Get desired contents from the indicated id3tag and store it in | |
498 * `tag'. | |
499 * | |
500 */ | |
501 void | |
502 mpg123_get_id3v2(struct id3_tag *id3d, struct id3tag_t *tag) | |
503 { | |
504 struct id3_frame *id3frm; | |
505 gchar *txt; | |
625
0a73d1faeb4e
[svn] GCC 4.1 warning fixes by Diego 'Flameeyes' Petteno from Gentoo.
chainsaw
parents:
575
diff
changeset
|
506 gsize tlen; |
0a73d1faeb4e
[svn] GCC 4.1 warning fixes by Diego 'Flameeyes' Petteno from Gentoo.
chainsaw
parents:
575
diff
changeset
|
507 gint num; |
61 | 508 |
509 #define ID3_SET(_tid,_fld) \ | |
510 { \ | |
511 id3frm = id3_get_frame( id3d, _tid, 1 ); \ | |
512 if (id3frm) { \ | |
513 txt = _tid == ID3_TCON ? id3_get_content(id3frm) \ | |
514 : id3_get_text(id3frm); \ | |
515 if(txt) \ | |
516 { \ | |
517 tlen = strlen(txt); \ | |
518 if ( tlen >= sizeof(tag->_fld) ) \ | |
519 tlen = sizeof(tag->_fld)-1; \ | |
520 strncpy( tag->_fld, txt, tlen ); \ | |
521 tag->_fld[tlen] = 0; \ | |
522 g_free(txt); \ | |
523 } \ | |
524 else \ | |
525 tag->_fld[0] = 0; \ | |
526 } else { \ | |
527 tag->_fld[0] = 0; \ | |
528 } \ | |
529 } | |
530 | |
531 #define ID3_SET_NUM(_tid,_fld) \ | |
532 { \ | |
533 id3frm = id3_get_frame(id3d, _tid, 1); \ | |
534 if (id3frm) { \ | |
535 num = id3_get_text_number(id3frm); \ | |
536 tag->_fld = num >= 0 ? num : 0; \ | |
537 } else \ | |
538 tag->_fld = 0; \ | |
539 } | |
540 | |
541 ID3_SET(ID3_TIT2, title); | |
542 ID3_SET(ID3_TPE1, artist); | |
543 if (strlen(tag->artist) == 0) | |
544 ID3_SET(ID3_TPE2, artist); | |
545 ID3_SET(ID3_TALB, album); | |
546 ID3_SET_NUM(ID3_TYER, year); | |
547 ID3_SET_NUM(ID3_TRCK, track_number); | |
548 ID3_SET(ID3_COMM, comment); | |
549 ID3_SET(ID3_TCON, genre); | |
550 } | |
551 | |
552 | |
553 /* | |
554 * Function get_song_title (fd, filename) | |
555 * | |
556 * Get song title of file. File position of `fd' will be | |
557 * clobbered. `fd' may be NULL, in which case `filename' is opened | |
558 * separately. The returned song title must be subsequently freed | |
559 * using g_free(). | |
560 * | |
561 */ | |
562 static gchar * | |
563 get_song_title(VFSFile * fd, char *filename) | |
564 { | |
565 VFSFile *file = fd; | |
566 char *ret = NULL; | |
567 struct id3v1tag_t id3v1tag; | |
568 struct id3tag_t id3tag; | |
569 | |
570 if (file || (file = vfs_fopen(filename, "rb")) != 0) { | |
571 struct id3_tag *id3 = NULL; | |
572 | |
573 /* | |
574 * Try reading ID3v2 tag. | |
575 */ | |
576 if (!mpg123_cfg.disable_id3v2) { | |
577 vfs_fseek(file, 0, SEEK_SET); | |
578 id3 = id3_open_fp(file, 0); | |
579 if (id3) { | |
580 mpg123_get_id3v2(id3, &id3tag); | |
581 ret = mpg123_format_song_title(&id3tag, filename); | |
582 id3_close(id3); | |
583 } | |
584 } | |
585 | |
586 /* | |
587 * Try reading ID3v1 tag. | |
588 */ | |
589 if (!id3 && (vfs_fseek(file, -1 * sizeof(id3v1tag), SEEK_END) == 0) && | |
590 (vfs_fread(&id3v1tag, 1, sizeof(id3v1tag), file) == | |
591 sizeof(id3v1tag)) && (strncmp(id3v1tag.tag, "TAG", 3) == 0)) { | |
592 mpg123_id3v1_to_id3v2(&id3v1tag, &id3tag); | |
593 ret = mpg123_format_song_title(&id3tag, filename); | |
594 } | |
595 | |
596 if (!fd) | |
597 /* | |
598 * File was opened in this function. | |
599 */ | |
600 vfs_fclose(file); | |
601 } | |
602 | |
603 if (ret == NULL) | |
604 /* | |
605 * Unable to get ID3 tag. | |
606 */ | |
607 ret = mpg123_format_song_title(NULL, filename); | |
608 | |
609 return ret; | |
610 } | |
611 | |
612 static long | |
613 get_song_length(VFSFile * file) | |
614 { | |
615 int len; | |
616 char tmp[4]; | |
617 | |
618 vfs_fseek(file, 0, SEEK_END); | |
619 len = vfs_ftell(file); | |
620 vfs_fseek(file, -128, SEEK_END); | |
621 vfs_fread(tmp, 1, 3, file); | |
622 if (!strncmp(tmp, "TAG", 3)) | |
623 len -= 128; | |
624 return len; | |
625 } | |
626 | |
627 | |
628 static guint | |
629 get_song_time(VFSFile * file) | |
630 { | |
631 guint32 head; | |
632 guchar tmp[4], *buf; | |
633 struct frame frm; | |
634 xing_header_t xing_header; | |
635 double tpf, bpf; | |
636 guint32 len; | |
637 | |
638 if (!file) | |
639 return -1; | |
640 | |
641 vfs_fseek(file, 0, SEEK_SET); | |
642 if (vfs_fread(tmp, 1, 4, file) != 4) | |
643 return 0; | |
644 head = convert_to_header(tmp); | |
645 while (!mpg123_head_check(head)) { | |
646 head <<= 8; | |
647 if (vfs_fread(tmp, 1, 1, file) != 1) | |
648 return 0; | |
649 head |= tmp[0]; | |
650 } | |
651 if (mpg123_decode_header(&frm, head)) { | |
652 buf = g_malloc(frm.framesize + 4); | |
653 vfs_fseek(file, -4, SEEK_CUR); | |
654 vfs_fread(buf, 1, frm.framesize + 4, file); | |
655 tpf = mpg123_compute_tpf(&frm); | |
656 if (mpg123_get_xing_header(&xing_header, buf)) { | |
657 g_free(buf); | |
658 if (xing_header.bytes == 0) | |
659 xing_header.bytes = get_song_length(file); | |
660 return (tpf * xing_header.frames * 1000); | |
661 } | |
662 g_free(buf); | |
663 bpf = mpg123_compute_bpf(&frm); | |
664 len = get_song_length(file); | |
665 return ((guint) (len / bpf) * tpf * 1000); | |
666 } | |
667 return 0; | |
668 } | |
669 | |
670 static void | |
671 get_song_info(char *filename, char **title_real, int *len_real) | |
672 { | |
673 VFSFile *file; | |
674 | |
675 (*len_real) = -1; | |
676 (*title_real) = NULL; | |
677 | |
678 /* | |
679 * TODO: Getting song info from http streams. | |
680 */ | |
681 if (!strncasecmp(filename, "http://", 7)) | |
682 return; | |
683 | |
684 if ((file = vfs_fopen(filename, "rb")) != NULL) { | |
685 (*len_real) = get_song_time(file); | |
686 (*title_real) = get_song_title(file, filename); | |
687 vfs_fclose(file); | |
688 } | |
689 } | |
690 | |
691 static int | |
692 open_output(void) | |
693 { | |
694 int r; | |
695 AFormat fmt = mpg123_cfg.resolution == 16 ? FMT_S16_NE : FMT_U8; | |
696 int freq = mpg123_freqs[fr.sampling_frequency] >> mpg123_cfg.downsample; | |
697 int channels = mpg123_cfg.channels == 2 ? fr.stereo : 1; | |
698 r = mpg123_ip.output->open_audio(fmt, freq, channels); | |
699 | |
700 if (r && dopause) { | |
701 mpg123_ip.output->pause(TRUE); | |
702 dopause = FALSE; | |
703 } | |
704 | |
705 return r; | |
706 } | |
707 | |
708 | |
709 static int | |
710 mpg123_seek(struct frame *fr, xing_header_t * xh, gboolean vbr, int time) | |
711 { | |
712 int jumped = -1; | |
713 | |
714 if (xh) { | |
715 int percent = ((double) time * 100.0) / | |
716 (mpg123_info->num_frames * mpg123_info->tpf); | |
717 int byte = mpg123_seek_point(xh, percent); | |
718 jumped = mpg123_stream_jump_to_byte(fr, byte); | |
719 } | |
720 else if (vbr && mpg123_length > 0) { | |
721 int byte = ((guint64) time * 1000 * mpg123_info->filesize) / | |
722 mpg123_length; | |
723 jumped = mpg123_stream_jump_to_byte(fr, byte); | |
724 } | |
725 else { | |
726 int frame = time / mpg123_info->tpf; | |
727 jumped = mpg123_stream_jump_to_frame(fr, frame); | |
728 } | |
729 | |
730 return jumped; | |
731 } | |
732 | |
733 | |
734 static void * | |
735 decode_loop(void *arg) | |
736 { | |
737 gboolean have_xing_header = FALSE, vbr = FALSE; | |
738 int disp_count = 0, temp_time; | |
739 char *filename = arg; | |
740 xing_header_t xing_header; | |
741 | |
742 /* This is used by fileinfo on http streams */ | |
743 mpg123_bitrate = 0; | |
744 | |
745 mpg123_pcm_sample = g_malloc0(32768); | |
746 mpg123_pcm_point = 0; | |
747 mpg123_filename = filename; | |
748 | |
749 mpg123_read_frame_init(); | |
750 | |
751 mpg123_open_stream(filename, -1); | |
752 | |
753 if (mpg123_info->eof || !mpg123_read_frame(&fr)) | |
754 mpg123_info->eof = TRUE; | |
755 | |
756 if (!mpg123_info->eof && mpg123_info->going) { | |
757 if (mpg123_cfg.channels == 2) | |
758 fr.single = -1; | |
759 else | |
760 fr.single = 3; | |
761 | |
762 fr.down_sample = mpg123_cfg.downsample; | |
763 fr.down_sample_sblimit = SBLIMIT >> mpg123_cfg.downsample; | |
764 set_synth_functions(&fr); | |
765 mpg123_init_layer3(fr.down_sample_sblimit); | |
766 | |
767 mpg123_info->tpf = mpg123_compute_tpf(&fr); | |
768 if (strncasecmp(filename, "http://", 7)) { | |
769 if (mpg123_stream_check_for_xing_header(&fr, &xing_header)) { | |
770 mpg123_info->num_frames = xing_header.frames; | |
771 have_xing_header = TRUE; | |
772 mpg123_read_frame(&fr); | |
773 } | |
774 } | |
775 | |
776 for (;;) { | |
777 memcpy(&temp_fr, &fr, sizeof(struct frame)); | |
778 if (!mpg123_read_frame(&temp_fr)) { | |
779 mpg123_info->eof = TRUE; | |
780 break; | |
781 } | |
782 if (fr.lay != temp_fr.lay || | |
783 fr.sampling_frequency != temp_fr.sampling_frequency || | |
784 fr.stereo != temp_fr.stereo || fr.lsf != temp_fr.lsf) | |
785 memcpy(&fr, &temp_fr, sizeof(struct frame)); | |
786 else | |
787 break; | |
788 } | |
789 | |
790 if (!have_xing_header && strncasecmp(filename, "http://", 7)) | |
791 mpg123_info->num_frames = mpg123_calc_numframes(&fr); | |
792 | |
793 memcpy(&fr, &temp_fr, sizeof(struct frame)); | |
794 mpg123_bitrate = tabsel_123[fr.lsf][fr.lay - 1][fr.bitrate_index]; | |
795 disp_bitrate = mpg123_bitrate; | |
796 mpg123_frequency = mpg123_freqs[fr.sampling_frequency]; | |
797 mpg123_stereo = fr.stereo; | |
798 mpg123_layer = fr.lay; | |
799 mpg123_lsf = fr.lsf; | |
800 mpg123_mpeg25 = fr.mpeg25; | |
801 mpg123_mode = fr.mode; | |
802 | |
803 if (strncasecmp(filename, "http://", 7)) { | |
804 mpg123_length = mpg123_info->num_frames * mpg123_info->tpf * 1000; | |
805 if (!mpg123_title) | |
806 mpg123_title = get_song_title(NULL, filename); | |
807 } | |
808 else { | |
809 if (!mpg123_title) | |
810 mpg123_title = mpg123_http_get_title(filename); | |
811 mpg123_length = -1; | |
812 } | |
813 | |
814 mpg123_ip.set_info(mpg123_title, mpg123_length, | |
815 mpg123_bitrate * 1000, | |
816 mpg123_freqs[fr.sampling_frequency], fr.stereo); | |
817 | |
818 output_opened = TRUE; | |
819 | |
820 if (!open_output()) { | |
821 audio_error = TRUE; | |
822 mpg123_info->eof = TRUE; | |
823 } | |
824 else | |
825 play_frame(&fr); | |
826 } | |
827 | |
828 mpg123_info->first_frame = FALSE; | |
829 while (mpg123_info->going) { | |
830 if (mpg123_info->jump_to_time != -1) { | |
831 void *xp = NULL; | |
832 if (have_xing_header) | |
833 xp = &xing_header; | |
834 if (mpg123_seek(&fr, xp, vbr, mpg123_info->jump_to_time) > -1) { | |
835 mpg123_ip.output->flush(mpg123_info->jump_to_time * 1000); | |
836 mpg123_info->eof = FALSE; | |
837 } | |
838 mpg123_info->jump_to_time = -1; | |
839 } | |
840 if (!mpg123_info->eof) { | |
841 if (mpg123_read_frame(&fr) != 0) { | |
842 if (fr.lay != mpg123_layer || fr.lsf != mpg123_lsf) { | |
843 memcpy(&temp_fr, &fr, sizeof(struct frame)); | |
844 if (mpg123_read_frame(&temp_fr) != 0) { | |
845 if (fr.lay == temp_fr.lay && fr.lsf == temp_fr.lsf) { | |
846 mpg123_layer = fr.lay; | |
847 mpg123_lsf = fr.lsf; | |
848 memcpy(&fr, &temp_fr, sizeof(struct frame)); | |
849 } | |
850 else { | |
851 memcpy(&fr, &temp_fr, sizeof(struct frame)); | |
852 skip_frames = 2; | |
853 mpg123_info->output_audio = FALSE; | |
854 continue; | |
855 } | |
856 | |
857 } | |
858 } | |
859 if (mpg123_freqs[fr.sampling_frequency] != mpg123_frequency | |
860 || mpg123_stereo != fr.stereo) { | |
861 memcpy(&temp_fr, &fr, sizeof(struct frame)); | |
862 if (mpg123_read_frame(&temp_fr) != 0) { | |
863 if (fr.sampling_frequency == | |
864 temp_fr.sampling_frequency | |
865 && temp_fr.stereo == fr.stereo) { | |
866 mpg123_ip.output->buffer_free(); | |
867 mpg123_ip.output->buffer_free(); | |
868 while (mpg123_ip.output->buffer_playing() | |
869 && mpg123_info->going | |
870 && mpg123_info->jump_to_time == -1) | |
871 xmms_usleep(20000); | |
872 if (!mpg123_info->going) | |
873 break; | |
874 temp_time = mpg123_ip.output->output_time(); | |
875 mpg123_ip.output->close_audio(); | |
876 mpg123_frequency = | |
877 mpg123_freqs[fr.sampling_frequency]; | |
878 mpg123_stereo = fr.stereo; | |
879 if (!mpg123_ip.output-> | |
880 open_audio(mpg123_cfg.resolution == | |
881 16 ? FMT_S16_NE : FMT_U8, | |
882 mpg123_freqs[fr.sampling_frequency] | |
883 >> mpg123_cfg.downsample, | |
884 mpg123_cfg.channels == | |
885 2 ? fr.stereo : 1)) { | |
886 audio_error = TRUE; | |
887 mpg123_info->eof = TRUE; | |
888 } | |
889 mpg123_ip.output->flush(temp_time); | |
890 mpg123_ip.set_info(mpg123_title, mpg123_length, | |
891 mpg123_bitrate * 1000, | |
892 mpg123_frequency, | |
893 mpg123_stereo); | |
894 memcpy(&fr, &temp_fr, sizeof(struct frame)); | |
895 } | |
896 else { | |
897 memcpy(&fr, &temp_fr, sizeof(struct frame)); | |
898 skip_frames = 2; | |
899 mpg123_info->output_audio = FALSE; | |
900 continue; | |
901 } | |
902 } | |
903 } | |
904 | |
905 if (tabsel_123[fr.lsf][fr.lay - 1][fr.bitrate_index] != | |
906 mpg123_bitrate) | |
907 mpg123_bitrate = | |
908 tabsel_123[fr.lsf][fr.lay - 1][fr.bitrate_index]; | |
909 | |
910 if (!disp_count) { | |
911 disp_count = 20; | |
912 if (mpg123_bitrate != disp_bitrate) { | |
913 /* FIXME networks streams */ | |
914 disp_bitrate = mpg123_bitrate; | |
915 if (!have_xing_header | |
916 && strncasecmp(filename, "http://", 7)) { | |
917 double rel = mpg123_relative_pos(); | |
918 if (rel) { | |
919 mpg123_length = | |
920 mpg123_ip.output->written_time() / rel; | |
921 vbr = TRUE; | |
922 } | |
923 | |
924 if (rel == 0 || !(mpg123_length > 0)) { | |
925 mpg123_info->num_frames = | |
926 mpg123_calc_numframes(&fr); | |
927 mpg123_info->tpf = mpg123_compute_tpf(&fr); | |
928 mpg123_length = | |
929 mpg123_info->num_frames * | |
930 mpg123_info->tpf * 1000; | |
931 } | |
932 | |
933 | |
934 } | |
935 mpg123_ip.set_info(mpg123_title, mpg123_length, | |
936 mpg123_bitrate * 1000, | |
937 mpg123_frequency, mpg123_stereo); | |
938 } | |
939 } | |
940 else | |
941 disp_count--; | |
942 play_frame(&fr); | |
943 } | |
944 else { | |
945 mpg123_ip.output->buffer_free(); | |
946 mpg123_ip.output->buffer_free(); | |
947 mpg123_info->eof = TRUE; | |
948 xmms_usleep(10000); | |
949 } | |
950 } | |
951 else { | |
952 xmms_usleep(10000); | |
953 } | |
954 } | |
955 g_free(mpg123_title); | |
956 mpg123_title = NULL; | |
957 mpg123_stream_close(); | |
958 if (output_opened && !audio_error) | |
959 mpg123_ip.output->close_audio(); | |
960 g_free(mpg123_pcm_sample); | |
961 mpg123_filename = NULL; | |
962 g_free(filename); | |
963 | |
964 return NULL; | |
965 } | |
966 | |
967 static void | |
968 play_file(char *filename) | |
969 { | |
970 memset(&fr, 0, sizeof(struct frame)); | |
971 memset(&temp_fr, 0, sizeof(struct frame)); | |
972 | |
973 mpg123_info = g_malloc0(sizeof(PlayerInfo)); | |
974 mpg123_info->going = 1; | |
975 mpg123_info->first_frame = TRUE; | |
976 mpg123_info->output_audio = TRUE; | |
977 mpg123_info->jump_to_time = -1; | |
978 skip_frames = 0; | |
979 audio_error = FALSE; | |
980 output_opened = FALSE; | |
981 dopause = FALSE; | |
982 | |
983 decode_thread = g_thread_create(decode_loop, g_strdup(filename), TRUE, | |
984 NULL); | |
985 } | |
986 | |
987 static void | |
988 stop(void) | |
989 { | |
990 if (mpg123_info && mpg123_info->going) { | |
991 mpg123_info->going = FALSE; | |
992 g_thread_join(decode_thread); | |
993 g_free(mpg123_info); | |
994 mpg123_info = NULL; | |
995 } | |
996 } | |
997 | |
998 static void | |
999 seek(int time) | |
1000 { | |
1001 mpg123_info->jump_to_time = time; | |
1002 | |
1003 while (mpg123_info->jump_to_time != -1) | |
1004 xmms_usleep(10000); | |
1005 } | |
1006 | |
1007 static void | |
1008 do_pause(short p) | |
1009 { | |
1010 if (output_opened) | |
1011 mpg123_ip.output->pause(p); | |
1012 else | |
1013 dopause = p; | |
1014 } | |
1015 | |
1016 static int | |
1017 get_time(void) | |
1018 { | |
1019 if (audio_error) | |
1020 return -2; | |
1021 if (!mpg123_info) | |
1022 return -1; | |
1023 if (!mpg123_info->going | |
1024 || (mpg123_info->eof && !mpg123_ip.output->buffer_playing())) | |
1025 return -1; | |
1026 return mpg123_ip.output->output_time(); | |
1027 } | |
1028 | |
1029 static void | |
1030 aboutbox(void) | |
1031 { | |
1032 static GtkWidget *aboutbox; | |
1033 | |
1034 if (aboutbox != NULL) | |
1035 return; | |
1036 | |
1037 aboutbox = xmms_show_message(_("About MPEG Audio Plugin"), | |
223
e7e9a86c0c01
[svn] Update credits on here reflecting audacious-decoder changes.
nenolod
parents:
177
diff
changeset
|
1038 _("Audacious decoding engine by William Pitcock <nenolod@nenolod.net>, derived from:\n" |
e7e9a86c0c01
[svn] Update credits on here reflecting audacious-decoder changes.
nenolod
parents:
177
diff
changeset
|
1039 "mpg123 decoding engine by Michael Hipp <mh@mpg123.de>\n" |
e7e9a86c0c01
[svn] Update credits on here reflecting audacious-decoder changes.
nenolod
parents:
177
diff
changeset
|
1040 "Derived partially from mpg123 0.59s.mc3 as well.\n" |
129
ce9d4aa5889a
[svn] Update the credits to note the code sync that occured.
nenolod
parents:
61
diff
changeset
|
1041 "Based on the original XMMS plugin."), |
ce9d4aa5889a
[svn] Update the credits to note the code sync that occured.
nenolod
parents:
61
diff
changeset
|
1042 _("Ok"), |
ce9d4aa5889a
[svn] Update the credits to note the code sync that occured.
nenolod
parents:
61
diff
changeset
|
1043 FALSE, NULL, NULL); |
61 | 1044 |
1045 g_signal_connect(G_OBJECT(aboutbox), "destroy", | |
1046 G_CALLBACK(gtk_widget_destroyed), &aboutbox); | |
1047 } | |
1048 | |
1049 InputPlugin mpg123_ip = { | |
1050 NULL, | |
1051 NULL, | |
1052 NULL, /* Description */ | |
1053 init, | |
1054 aboutbox, | |
1055 mpg123_configure, | |
1056 is_our_file, | |
1057 NULL, | |
1058 play_file, | |
1059 stop, | |
1060 do_pause, | |
1061 seek, | |
1062 mpg123_set_eq, | |
1063 get_time, | |
1064 NULL, NULL, | |
1065 cleanup, | |
1066 NULL, | |
1067 NULL, NULL, NULL, | |
1068 get_song_info, | |
1069 mpg123_file_info_box, /* file_info_box */ | |
1070 NULL | |
1071 }; | |
1072 | |
1073 InputPlugin * | |
1074 get_iplugin_info(void) | |
1075 { | |
1076 mpg123_ip.description = g_strdup_printf(_("MPEG Audio Plugin")); | |
1077 return &mpg123_ip; | |
1078 } |