Mercurial > audlegacy
annotate Plugins/Input/mpg123/mpg123.c @ 801:ec9ba0ad38a8 trunk
[svn] - use OBJECTIVE_DATA instead of a kludge in the posthook here
author | nenolod |
---|---|
date | Sat, 04 Mar 2006 14:34:04 -0800 |
parents | 69d3bb31aa49 |
children | c8cf439179b8 |
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 | |
777 | 234 #if 0 |
61 | 235 #define DET_BUF_SIZE 1024 |
236 | |
237 static gboolean | |
238 mpg123_detect_by_content(char *filename) | |
239 { | |
240 VFSFile *file; | |
241 guchar tmp[4]; | |
242 guint32 head; | |
243 struct frame fr; | |
244 guchar buf[DET_BUF_SIZE]; | |
245 int in_buf, i; | |
246 gboolean ret = FALSE; | |
775 | 247 guint cyc = 0; |
61 | 248 |
249 if ((file = vfs_fopen(filename, "rb")) == NULL) | |
250 return FALSE; | |
251 if (vfs_fread(tmp, 1, 4, file) != 4) | |
252 goto done; | |
253 head = convert_to_header(tmp); | |
773 | 254 while (!mpg123_head_check(head)) { |
61 | 255 /* |
256 * The mpeg-stream can start anywhere in the file, | |
257 * so we check the entire file | |
762
a22aa7bfa108
[svn] - try to avoid a buffer overflow when detecting an MPEG stream
nenolod
parents:
761
diff
changeset
|
258 * |
771 | 259 * Incorrect! We give up past ten iterations of this |
762
a22aa7bfa108
[svn] - try to avoid a buffer overflow when detecting an MPEG stream
nenolod
parents:
761
diff
changeset
|
260 * code for safety's sake. Buffer overflows suck. --nenolod |
61 | 261 */ |
262 /* Optimize this */ | |
263 in_buf = vfs_fread(buf, 1, DET_BUF_SIZE, file); | |
264 if (in_buf == 0) | |
265 goto done; | |
266 | |
267 for (i = 0; i < in_buf; i++) { | |
268 head <<= 8; | |
269 head |= buf[i]; | |
270 if (mpg123_head_check(head)) { | |
271 vfs_fseek(file, i + 1 - in_buf, SEEK_CUR); | |
272 break; | |
273 } | |
274 } | |
775 | 275 |
276 if (++cyc > 20) | |
277 goto done; | |
61 | 278 } |
279 if (mpg123_decode_header(&fr, head)) { | |
280 /* | |
281 * We found something which looks like a MPEG-header. | |
282 * We check the next frame too, to be sure | |
283 */ | |
284 | |
285 if (vfs_fseek(file, fr.framesize, SEEK_CUR) != 0) | |
286 goto done; | |
287 if (vfs_fread(tmp, 1, 4, file) != 4) | |
288 goto done; | |
289 head = convert_to_header(tmp); | |
290 if (mpg123_head_check(head) && mpg123_decode_header(&fr, head)) | |
291 ret = TRUE; | |
292 } | |
293 | |
294 done: | |
295 vfs_fclose(file); | |
296 return ret; | |
297 } | |
777 | 298 #endif |
61 | 299 |
300 static int | |
301 is_our_file(char *filename) | |
302 { | |
653 | 303 gchar *ext = strrchr(filename, '.'); |
304 | |
761 | 305 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
|
306 return TRUE; |
777 | 307 else if (ext && (!strncasecmp(ext, ".mp3", 4) |
308 || !strncasecmp(ext, ".mp2", 4) | |
309 || !strncasecmp(ext, ".mpg", 4))) | |
310 return TRUE; | |
653 | 311 |
778 | 312 return FALSE; |
61 | 313 } |
314 | |
315 static void | |
316 play_frame(struct frame *fr) | |
317 { | |
318 if (fr->error_protection) { | |
319 bsi.wordpointer += 2; | |
320 /* mpg123_getbits(16); *//* skip crc */ | |
321 } | |
322 if (!fr->do_layer(fr)) { | |
323 skip_frames = 2; | |
324 mpg123_info->output_audio = FALSE; | |
325 } | |
326 else { | |
327 if (!skip_frames) | |
328 mpg123_info->output_audio = TRUE; | |
329 else | |
330 skip_frames--; | |
331 } | |
332 } | |
333 | |
334 static const char * | |
335 get_id3_genre(unsigned char genre_code) | |
336 { | |
337 if (genre_code < GENRE_MAX) | |
338 return gettext(mpg123_id3_genres[genre_code]); | |
339 | |
340 return ""; | |
341 } | |
342 | |
343 guint | |
344 mpg123_strip_spaces(char *src, size_t n) | |
345 { | |
346 gchar *space = NULL, /* last space in src */ | |
347 *start = src; | |
348 | |
349 while (n--) | |
350 switch (*src++) { | |
351 case '\0': | |
352 n = 0; /* breaks out of while loop */ | |
353 | |
354 src--; | |
355 break; | |
356 case ' ': | |
357 if (space == NULL) | |
358 space = src - 1; | |
359 break; | |
360 default: | |
361 space = NULL; /* don't terminate intermediate spaces */ | |
362 | |
363 break; | |
364 } | |
365 if (space != NULL) { | |
366 src = space; | |
367 *src = '\0'; | |
368 } | |
369 return src - start; | |
370 } | |
371 | |
372 /* | |
373 * Function extname (filename) | |
374 * | |
375 * Return pointer within filename to its extenstion, or NULL if | |
376 * filename has no extension. | |
377 * | |
378 */ | |
379 static gchar * | |
380 extname(const char *filename) | |
381 { | |
382 gchar *ext = strrchr(filename, '.'); | |
383 | |
384 if (ext != NULL) | |
385 ++ext; | |
386 | |
387 return ext; | |
388 } | |
389 | |
390 /* | |
391 * Function id3v1_to_id3v2 (v1, v2) | |
392 * | |
393 * Convert ID3v1 tag `v1' to ID3v2 tag `v2'. | |
394 * | |
395 */ | |
396 void | |
397 mpg123_id3v1_to_id3v2(struct id3v1tag_t *v1, struct id3tag_t *v2) | |
398 { | |
399 memset(v2, 0, sizeof(struct id3tag_t)); | |
400 strncpy(v2->title, v1->title, 30); | |
401 strncpy(v2->artist, v1->artist, 30); | |
402 strncpy(v2->album, v1->album, 30); | |
403 strncpy(v2->comment, v1->u.v1_0.comment, 30); | |
404 strncpy(v2->genre, get_id3_genre(v1->genre), sizeof(v2->genre)); | |
405 g_strstrip(v2->title); | |
406 g_strstrip(v2->artist); | |
407 g_strstrip(v2->album); | |
408 g_strstrip(v2->comment); | |
409 g_strstrip(v2->genre); | |
636 | 410 { |
411 char y[5]; | |
412 memcpy(y, v1->year, 4); y[4]=0; | |
413 v2->year = atoi(y); | |
414 } | |
61 | 415 |
416 /* Check for v1.1 tags. */ | |
417 if (v1->u.v1_1.__zero == 0) | |
418 v2->track_number = v1->u.v1_1.track_number; | |
419 else | |
420 v2->track_number = 0; | |
421 } | |
422 | |
423 static char * | |
424 mpg123_getstr(char *str) | |
425 { | |
426 if (str && strlen(str) > 0) | |
427 return str; | |
428 return NULL; | |
429 } | |
430 | |
431 static gchar * | |
432 convert_id3_title(gchar * title) | |
433 { | |
434 gchar **encoding = mpg123_id3_encoding_list; | |
435 gchar *new_title = NULL; | |
436 | |
437 if (g_utf8_validate(title, -1, NULL)) | |
438 return title; | |
439 | |
440 while (*encoding && !new_title) { | |
441 new_title = g_convert(title, strlen(title), "UTF-8", *encoding++, | |
442 NULL, NULL, NULL); | |
443 } | |
444 | |
445 if (new_title) { | |
446 g_free(title); | |
447 return new_title; | |
448 } | |
449 | |
450 /* FIXME: We're relying on BMP core to provide fallback | |
451 * conversion */ | |
452 return title; | |
453 } | |
454 | |
455 /* | |
456 * Function mpg123_format_song_title (tag, filename) | |
457 * | |
458 * Create song title according to `tag' and/or `filename' and | |
459 * return it. The title must be subsequently freed using g_free(). | |
460 * | |
461 */ | |
462 gchar * | |
463 mpg123_format_song_title(struct id3tag_t * tag, gchar * filename) | |
464 { | |
465 gchar *title = NULL; | |
466 TitleInput *input; | |
467 | |
468 input = bmp_title_input_new(); | |
469 | |
470 if (tag) { | |
471 input->performer = mpg123_getstr(tag->artist); | |
472 input->album_name = mpg123_getstr(tag->album); | |
473 input->track_name = mpg123_getstr(tag->title); | |
474 input->year = tag->year; | |
475 input->track_number = tag->track_number; | |
476 input->genre = mpg123_getstr(tag->genre); | |
477 input->comment = mpg123_getstr(tag->comment); | |
478 } | |
479 | |
480 input->file_name = g_path_get_basename(filename); | |
481 input->file_path = g_path_get_dirname(filename); | |
482 input->file_ext = extname(filename); | |
483 | |
484 title = xmms_get_titlestring(mpg123_cfg.title_override ? | |
485 mpg123_cfg.id3_format : | |
486 xmms_get_gentitle_format(), input); | |
487 | |
488 if (!title) { | |
489 /* Format according to filename. */ | |
490 title = g_path_get_basename(filename); | |
491 if (extname(title)) | |
492 *(extname(title) - 1) = '\0'; /* removes period */ | |
493 } | |
494 | |
495 g_free(input->file_path); | |
496 g_free(input->file_name); | |
497 g_free(input); | |
498 | |
499 if (mpg123_cfg.title_encoding_enabled) | |
500 title = convert_id3_title(title); | |
501 | |
502 return title; | |
503 } | |
504 | |
505 /* | |
506 * Function mpg123_get_id3v2 (id3d, tag) | |
507 * | |
508 * Get desired contents from the indicated id3tag and store it in | |
509 * `tag'. | |
510 * | |
511 */ | |
512 void | |
513 mpg123_get_id3v2(struct id3_tag *id3d, struct id3tag_t *tag) | |
514 { | |
515 struct id3_frame *id3frm; | |
516 gchar *txt; | |
625
0a73d1faeb4e
[svn] GCC 4.1 warning fixes by Diego 'Flameeyes' Petteno from Gentoo.
chainsaw
parents:
575
diff
changeset
|
517 gsize tlen; |
0a73d1faeb4e
[svn] GCC 4.1 warning fixes by Diego 'Flameeyes' Petteno from Gentoo.
chainsaw
parents:
575
diff
changeset
|
518 gint num; |
61 | 519 |
520 #define ID3_SET(_tid,_fld) \ | |
521 { \ | |
522 id3frm = id3_get_frame( id3d, _tid, 1 ); \ | |
523 if (id3frm) { \ | |
524 txt = _tid == ID3_TCON ? id3_get_content(id3frm) \ | |
525 : id3_get_text(id3frm); \ | |
526 if(txt) \ | |
527 { \ | |
528 tlen = strlen(txt); \ | |
529 if ( tlen >= sizeof(tag->_fld) ) \ | |
530 tlen = sizeof(tag->_fld)-1; \ | |
531 strncpy( tag->_fld, txt, tlen ); \ | |
532 tag->_fld[tlen] = 0; \ | |
533 g_free(txt); \ | |
534 } \ | |
535 else \ | |
536 tag->_fld[0] = 0; \ | |
537 } else { \ | |
538 tag->_fld[0] = 0; \ | |
539 } \ | |
540 } | |
541 | |
542 #define ID3_SET_NUM(_tid,_fld) \ | |
543 { \ | |
544 id3frm = id3_get_frame(id3d, _tid, 1); \ | |
545 if (id3frm) { \ | |
546 num = id3_get_text_number(id3frm); \ | |
547 tag->_fld = num >= 0 ? num : 0; \ | |
548 } else \ | |
549 tag->_fld = 0; \ | |
550 } | |
551 | |
552 ID3_SET(ID3_TIT2, title); | |
553 ID3_SET(ID3_TPE1, artist); | |
554 if (strlen(tag->artist) == 0) | |
555 ID3_SET(ID3_TPE2, artist); | |
556 ID3_SET(ID3_TALB, album); | |
557 ID3_SET_NUM(ID3_TYER, year); | |
558 ID3_SET_NUM(ID3_TRCK, track_number); | |
559 ID3_SET(ID3_COMM, comment); | |
560 ID3_SET(ID3_TCON, genre); | |
561 } | |
562 | |
563 | |
564 /* | |
565 * Function get_song_title (fd, filename) | |
566 * | |
567 * Get song title of file. File position of `fd' will be | |
568 * clobbered. `fd' may be NULL, in which case `filename' is opened | |
569 * separately. The returned song title must be subsequently freed | |
570 * using g_free(). | |
571 * | |
572 */ | |
573 static gchar * | |
574 get_song_title(VFSFile * fd, char *filename) | |
575 { | |
576 VFSFile *file = fd; | |
577 char *ret = NULL; | |
578 struct id3v1tag_t id3v1tag; | |
579 struct id3tag_t id3tag; | |
580 | |
581 if (file || (file = vfs_fopen(filename, "rb")) != 0) { | |
582 struct id3_tag *id3 = NULL; | |
583 | |
584 /* | |
585 * Try reading ID3v2 tag. | |
586 */ | |
587 if (!mpg123_cfg.disable_id3v2) { | |
588 vfs_fseek(file, 0, SEEK_SET); | |
589 id3 = id3_open_fp(file, 0); | |
590 if (id3) { | |
591 mpg123_get_id3v2(id3, &id3tag); | |
592 ret = mpg123_format_song_title(&id3tag, filename); | |
593 id3_close(id3); | |
594 } | |
595 } | |
596 | |
597 /* | |
598 * Try reading ID3v1 tag. | |
599 */ | |
600 if (!id3 && (vfs_fseek(file, -1 * sizeof(id3v1tag), SEEK_END) == 0) && | |
601 (vfs_fread(&id3v1tag, 1, sizeof(id3v1tag), file) == | |
602 sizeof(id3v1tag)) && (strncmp(id3v1tag.tag, "TAG", 3) == 0)) { | |
603 mpg123_id3v1_to_id3v2(&id3v1tag, &id3tag); | |
604 ret = mpg123_format_song_title(&id3tag, filename); | |
605 } | |
606 | |
607 if (!fd) | |
608 /* | |
609 * File was opened in this function. | |
610 */ | |
611 vfs_fclose(file); | |
612 } | |
613 | |
614 if (ret == NULL) | |
615 /* | |
616 * Unable to get ID3 tag. | |
617 */ | |
618 ret = mpg123_format_song_title(NULL, filename); | |
619 | |
620 return ret; | |
621 } | |
622 | |
623 static long | |
624 get_song_length(VFSFile * file) | |
625 { | |
626 int len; | |
627 char tmp[4]; | |
628 | |
629 vfs_fseek(file, 0, SEEK_END); | |
630 len = vfs_ftell(file); | |
631 vfs_fseek(file, -128, SEEK_END); | |
632 vfs_fread(tmp, 1, 3, file); | |
633 if (!strncmp(tmp, "TAG", 3)) | |
634 len -= 128; | |
635 return len; | |
636 } | |
637 | |
638 | |
639 static guint | |
640 get_song_time(VFSFile * file) | |
641 { | |
642 guint32 head; | |
643 guchar tmp[4], *buf; | |
644 struct frame frm; | |
645 xing_header_t xing_header; | |
646 double tpf, bpf; | |
647 guint32 len; | |
648 | |
649 if (!file) | |
650 return -1; | |
651 | |
652 vfs_fseek(file, 0, SEEK_SET); | |
653 if (vfs_fread(tmp, 1, 4, file) != 4) | |
654 return 0; | |
655 head = convert_to_header(tmp); | |
656 while (!mpg123_head_check(head)) { | |
657 head <<= 8; | |
658 if (vfs_fread(tmp, 1, 1, file) != 1) | |
659 return 0; | |
660 head |= tmp[0]; | |
661 } | |
662 if (mpg123_decode_header(&frm, head)) { | |
663 buf = g_malloc(frm.framesize + 4); | |
664 vfs_fseek(file, -4, SEEK_CUR); | |
665 vfs_fread(buf, 1, frm.framesize + 4, file); | |
666 tpf = mpg123_compute_tpf(&frm); | |
667 if (mpg123_get_xing_header(&xing_header, buf)) { | |
668 g_free(buf); | |
669 if (xing_header.bytes == 0) | |
670 xing_header.bytes = get_song_length(file); | |
671 return (tpf * xing_header.frames * 1000); | |
672 } | |
673 g_free(buf); | |
674 bpf = mpg123_compute_bpf(&frm); | |
675 len = get_song_length(file); | |
676 return ((guint) (len / bpf) * tpf * 1000); | |
677 } | |
678 return 0; | |
679 } | |
680 | |
681 static void | |
682 get_song_info(char *filename, char **title_real, int *len_real) | |
683 { | |
684 VFSFile *file; | |
685 | |
686 (*len_real) = -1; | |
687 (*title_real) = NULL; | |
688 | |
689 /* | |
690 * TODO: Getting song info from http streams. | |
691 */ | |
692 if (!strncasecmp(filename, "http://", 7)) | |
693 return; | |
694 | |
695 if ((file = vfs_fopen(filename, "rb")) != NULL) { | |
696 (*len_real) = get_song_time(file); | |
697 (*title_real) = get_song_title(file, filename); | |
698 vfs_fclose(file); | |
699 } | |
700 } | |
701 | |
702 static int | |
703 open_output(void) | |
704 { | |
705 int r; | |
706 AFormat fmt = mpg123_cfg.resolution == 16 ? FMT_S16_NE : FMT_U8; | |
707 int freq = mpg123_freqs[fr.sampling_frequency] >> mpg123_cfg.downsample; | |
708 int channels = mpg123_cfg.channels == 2 ? fr.stereo : 1; | |
709 r = mpg123_ip.output->open_audio(fmt, freq, channels); | |
710 | |
711 if (r && dopause) { | |
712 mpg123_ip.output->pause(TRUE); | |
713 dopause = FALSE; | |
714 } | |
715 | |
716 return r; | |
717 } | |
718 | |
719 | |
720 static int | |
721 mpg123_seek(struct frame *fr, xing_header_t * xh, gboolean vbr, int time) | |
722 { | |
723 int jumped = -1; | |
724 | |
725 if (xh) { | |
726 int percent = ((double) time * 100.0) / | |
727 (mpg123_info->num_frames * mpg123_info->tpf); | |
728 int byte = mpg123_seek_point(xh, percent); | |
729 jumped = mpg123_stream_jump_to_byte(fr, byte); | |
730 } | |
731 else if (vbr && mpg123_length > 0) { | |
732 int byte = ((guint64) time * 1000 * mpg123_info->filesize) / | |
733 mpg123_length; | |
734 jumped = mpg123_stream_jump_to_byte(fr, byte); | |
735 } | |
736 else { | |
737 int frame = time / mpg123_info->tpf; | |
738 jumped = mpg123_stream_jump_to_frame(fr, frame); | |
739 } | |
740 | |
741 return jumped; | |
742 } | |
743 | |
744 | |
745 static void * | |
746 decode_loop(void *arg) | |
747 { | |
748 gboolean have_xing_header = FALSE, vbr = FALSE; | |
749 int disp_count = 0, temp_time; | |
750 char *filename = arg; | |
751 xing_header_t xing_header; | |
752 | |
753 /* This is used by fileinfo on http streams */ | |
754 mpg123_bitrate = 0; | |
755 | |
756 mpg123_pcm_sample = g_malloc0(32768); | |
757 mpg123_pcm_point = 0; | |
758 mpg123_filename = filename; | |
759 | |
760 mpg123_read_frame_init(); | |
761 | |
762 mpg123_open_stream(filename, -1); | |
763 | |
764 if (mpg123_info->eof || !mpg123_read_frame(&fr)) | |
765 mpg123_info->eof = TRUE; | |
766 | |
767 if (!mpg123_info->eof && mpg123_info->going) { | |
768 if (mpg123_cfg.channels == 2) | |
769 fr.single = -1; | |
770 else | |
771 fr.single = 3; | |
772 | |
773 fr.down_sample = mpg123_cfg.downsample; | |
774 fr.down_sample_sblimit = SBLIMIT >> mpg123_cfg.downsample; | |
775 set_synth_functions(&fr); | |
776 mpg123_init_layer3(fr.down_sample_sblimit); | |
777 | |
778 mpg123_info->tpf = mpg123_compute_tpf(&fr); | |
779 if (strncasecmp(filename, "http://", 7)) { | |
780 if (mpg123_stream_check_for_xing_header(&fr, &xing_header)) { | |
781 mpg123_info->num_frames = xing_header.frames; | |
782 have_xing_header = TRUE; | |
783 mpg123_read_frame(&fr); | |
784 } | |
785 } | |
786 | |
787 for (;;) { | |
788 memcpy(&temp_fr, &fr, sizeof(struct frame)); | |
789 if (!mpg123_read_frame(&temp_fr)) { | |
790 mpg123_info->eof = TRUE; | |
791 break; | |
792 } | |
793 if (fr.lay != temp_fr.lay || | |
794 fr.sampling_frequency != temp_fr.sampling_frequency || | |
795 fr.stereo != temp_fr.stereo || fr.lsf != temp_fr.lsf) | |
796 memcpy(&fr, &temp_fr, sizeof(struct frame)); | |
797 else | |
798 break; | |
799 } | |
800 | |
801 if (!have_xing_header && strncasecmp(filename, "http://", 7)) | |
802 mpg123_info->num_frames = mpg123_calc_numframes(&fr); | |
803 | |
804 memcpy(&fr, &temp_fr, sizeof(struct frame)); | |
805 mpg123_bitrate = tabsel_123[fr.lsf][fr.lay - 1][fr.bitrate_index]; | |
806 disp_bitrate = mpg123_bitrate; | |
807 mpg123_frequency = mpg123_freqs[fr.sampling_frequency]; | |
808 mpg123_stereo = fr.stereo; | |
809 mpg123_layer = fr.lay; | |
810 mpg123_lsf = fr.lsf; | |
811 mpg123_mpeg25 = fr.mpeg25; | |
812 mpg123_mode = fr.mode; | |
813 | |
814 if (strncasecmp(filename, "http://", 7)) { | |
815 mpg123_length = mpg123_info->num_frames * mpg123_info->tpf * 1000; | |
816 if (!mpg123_title) | |
817 mpg123_title = get_song_title(NULL, filename); | |
818 } | |
819 else { | |
820 if (!mpg123_title) | |
821 mpg123_title = mpg123_http_get_title(filename); | |
822 mpg123_length = -1; | |
823 } | |
824 | |
825 mpg123_ip.set_info(mpg123_title, mpg123_length, | |
826 mpg123_bitrate * 1000, | |
827 mpg123_freqs[fr.sampling_frequency], fr.stereo); | |
828 | |
829 output_opened = TRUE; | |
830 | |
831 if (!open_output()) { | |
832 audio_error = TRUE; | |
833 mpg123_info->eof = TRUE; | |
834 } | |
835 else | |
836 play_frame(&fr); | |
837 } | |
838 | |
839 mpg123_info->first_frame = FALSE; | |
840 while (mpg123_info->going) { | |
841 if (mpg123_info->jump_to_time != -1) { | |
842 void *xp = NULL; | |
843 if (have_xing_header) | |
844 xp = &xing_header; | |
845 if (mpg123_seek(&fr, xp, vbr, mpg123_info->jump_to_time) > -1) { | |
846 mpg123_ip.output->flush(mpg123_info->jump_to_time * 1000); | |
847 mpg123_info->eof = FALSE; | |
848 } | |
849 mpg123_info->jump_to_time = -1; | |
850 } | |
851 if (!mpg123_info->eof) { | |
852 if (mpg123_read_frame(&fr) != 0) { | |
853 if (fr.lay != mpg123_layer || fr.lsf != mpg123_lsf) { | |
854 memcpy(&temp_fr, &fr, sizeof(struct frame)); | |
855 if (mpg123_read_frame(&temp_fr) != 0) { | |
856 if (fr.lay == temp_fr.lay && fr.lsf == temp_fr.lsf) { | |
857 mpg123_layer = fr.lay; | |
858 mpg123_lsf = fr.lsf; | |
859 memcpy(&fr, &temp_fr, sizeof(struct frame)); | |
860 } | |
861 else { | |
862 memcpy(&fr, &temp_fr, sizeof(struct frame)); | |
863 skip_frames = 2; | |
864 mpg123_info->output_audio = FALSE; | |
865 continue; | |
866 } | |
867 | |
868 } | |
869 } | |
870 if (mpg123_freqs[fr.sampling_frequency] != mpg123_frequency | |
871 || mpg123_stereo != fr.stereo) { | |
872 memcpy(&temp_fr, &fr, sizeof(struct frame)); | |
873 if (mpg123_read_frame(&temp_fr) != 0) { | |
874 if (fr.sampling_frequency == | |
875 temp_fr.sampling_frequency | |
876 && temp_fr.stereo == fr.stereo) { | |
877 mpg123_ip.output->buffer_free(); | |
878 mpg123_ip.output->buffer_free(); | |
879 while (mpg123_ip.output->buffer_playing() | |
880 && mpg123_info->going | |
881 && mpg123_info->jump_to_time == -1) | |
882 xmms_usleep(20000); | |
883 if (!mpg123_info->going) | |
884 break; | |
885 temp_time = mpg123_ip.output->output_time(); | |
886 mpg123_ip.output->close_audio(); | |
887 mpg123_frequency = | |
888 mpg123_freqs[fr.sampling_frequency]; | |
889 mpg123_stereo = fr.stereo; | |
890 if (!mpg123_ip.output-> | |
891 open_audio(mpg123_cfg.resolution == | |
892 16 ? FMT_S16_NE : FMT_U8, | |
893 mpg123_freqs[fr.sampling_frequency] | |
894 >> mpg123_cfg.downsample, | |
895 mpg123_cfg.channels == | |
896 2 ? fr.stereo : 1)) { | |
897 audio_error = TRUE; | |
898 mpg123_info->eof = TRUE; | |
899 } | |
900 mpg123_ip.output->flush(temp_time); | |
901 mpg123_ip.set_info(mpg123_title, mpg123_length, | |
902 mpg123_bitrate * 1000, | |
903 mpg123_frequency, | |
904 mpg123_stereo); | |
905 memcpy(&fr, &temp_fr, sizeof(struct frame)); | |
906 } | |
907 else { | |
908 memcpy(&fr, &temp_fr, sizeof(struct frame)); | |
909 skip_frames = 2; | |
910 mpg123_info->output_audio = FALSE; | |
911 continue; | |
912 } | |
913 } | |
914 } | |
915 | |
916 if (tabsel_123[fr.lsf][fr.lay - 1][fr.bitrate_index] != | |
917 mpg123_bitrate) | |
918 mpg123_bitrate = | |
919 tabsel_123[fr.lsf][fr.lay - 1][fr.bitrate_index]; | |
920 | |
921 if (!disp_count) { | |
922 disp_count = 20; | |
923 if (mpg123_bitrate != disp_bitrate) { | |
924 /* FIXME networks streams */ | |
925 disp_bitrate = mpg123_bitrate; | |
926 if (!have_xing_header | |
927 && strncasecmp(filename, "http://", 7)) { | |
928 double rel = mpg123_relative_pos(); | |
929 if (rel) { | |
930 mpg123_length = | |
931 mpg123_ip.output->written_time() / rel; | |
932 vbr = TRUE; | |
933 } | |
934 | |
935 if (rel == 0 || !(mpg123_length > 0)) { | |
936 mpg123_info->num_frames = | |
937 mpg123_calc_numframes(&fr); | |
938 mpg123_info->tpf = mpg123_compute_tpf(&fr); | |
939 mpg123_length = | |
940 mpg123_info->num_frames * | |
941 mpg123_info->tpf * 1000; | |
942 } | |
943 | |
944 | |
945 } | |
946 mpg123_ip.set_info(mpg123_title, mpg123_length, | |
947 mpg123_bitrate * 1000, | |
948 mpg123_frequency, mpg123_stereo); | |
949 } | |
950 } | |
951 else | |
952 disp_count--; | |
953 play_frame(&fr); | |
954 } | |
955 else { | |
956 mpg123_ip.output->buffer_free(); | |
957 mpg123_ip.output->buffer_free(); | |
958 mpg123_info->eof = TRUE; | |
959 xmms_usleep(10000); | |
960 } | |
961 } | |
962 else { | |
963 xmms_usleep(10000); | |
964 } | |
965 } | |
966 g_free(mpg123_title); | |
967 mpg123_title = NULL; | |
968 mpg123_stream_close(); | |
969 if (output_opened && !audio_error) | |
970 mpg123_ip.output->close_audio(); | |
971 g_free(mpg123_pcm_sample); | |
972 mpg123_filename = NULL; | |
973 g_free(filename); | |
974 | |
975 return NULL; | |
976 } | |
977 | |
978 static void | |
979 play_file(char *filename) | |
980 { | |
981 memset(&fr, 0, sizeof(struct frame)); | |
982 memset(&temp_fr, 0, sizeof(struct frame)); | |
983 | |
984 mpg123_info = g_malloc0(sizeof(PlayerInfo)); | |
985 mpg123_info->going = 1; | |
986 mpg123_info->first_frame = TRUE; | |
987 mpg123_info->output_audio = TRUE; | |
988 mpg123_info->jump_to_time = -1; | |
989 skip_frames = 0; | |
990 audio_error = FALSE; | |
991 output_opened = FALSE; | |
992 dopause = FALSE; | |
993 | |
994 decode_thread = g_thread_create(decode_loop, g_strdup(filename), TRUE, | |
995 NULL); | |
996 } | |
997 | |
998 static void | |
999 stop(void) | |
1000 { | |
1001 if (mpg123_info && mpg123_info->going) { | |
1002 mpg123_info->going = FALSE; | |
1003 g_thread_join(decode_thread); | |
1004 g_free(mpg123_info); | |
1005 mpg123_info = NULL; | |
1006 } | |
1007 } | |
1008 | |
1009 static void | |
1010 seek(int time) | |
1011 { | |
1012 mpg123_info->jump_to_time = time; | |
1013 | |
1014 while (mpg123_info->jump_to_time != -1) | |
1015 xmms_usleep(10000); | |
1016 } | |
1017 | |
1018 static void | |
1019 do_pause(short p) | |
1020 { | |
1021 if (output_opened) | |
1022 mpg123_ip.output->pause(p); | |
1023 else | |
1024 dopause = p; | |
1025 } | |
1026 | |
1027 static int | |
1028 get_time(void) | |
1029 { | |
1030 if (audio_error) | |
1031 return -2; | |
1032 if (!mpg123_info) | |
1033 return -1; | |
1034 if (!mpg123_info->going | |
1035 || (mpg123_info->eof && !mpg123_ip.output->buffer_playing())) | |
1036 return -1; | |
1037 return mpg123_ip.output->output_time(); | |
1038 } | |
1039 | |
1040 static void | |
1041 aboutbox(void) | |
1042 { | |
1043 static GtkWidget *aboutbox; | |
1044 | |
1045 if (aboutbox != NULL) | |
1046 return; | |
1047 | |
1048 aboutbox = xmms_show_message(_("About MPEG Audio Plugin"), | |
223
e7e9a86c0c01
[svn] Update credits on here reflecting audacious-decoder changes.
nenolod
parents:
177
diff
changeset
|
1049 _("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
|
1050 "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
|
1051 "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
|
1052 "Based on the original XMMS plugin."), |
ce9d4aa5889a
[svn] Update the credits to note the code sync that occured.
nenolod
parents:
61
diff
changeset
|
1053 _("Ok"), |
ce9d4aa5889a
[svn] Update the credits to note the code sync that occured.
nenolod
parents:
61
diff
changeset
|
1054 FALSE, NULL, NULL); |
61 | 1055 |
1056 g_signal_connect(G_OBJECT(aboutbox), "destroy", | |
1057 G_CALLBACK(gtk_widget_destroyed), &aboutbox); | |
1058 } | |
1059 | |
1060 InputPlugin mpg123_ip = { | |
1061 NULL, | |
1062 NULL, | |
1063 NULL, /* Description */ | |
1064 init, | |
1065 aboutbox, | |
1066 mpg123_configure, | |
1067 is_our_file, | |
1068 NULL, | |
1069 play_file, | |
1070 stop, | |
1071 do_pause, | |
1072 seek, | |
1073 mpg123_set_eq, | |
1074 get_time, | |
1075 NULL, NULL, | |
1076 cleanup, | |
1077 NULL, | |
1078 NULL, NULL, NULL, | |
1079 get_song_info, | |
1080 mpg123_file_info_box, /* file_info_box */ | |
1081 NULL | |
1082 }; | |
1083 | |
1084 InputPlugin * | |
1085 get_iplugin_info(void) | |
1086 { | |
1087 mpg123_ip.description = g_strdup_printf(_("MPEG Audio Plugin")); | |
1088 return &mpg123_ip; | |
1089 } |