2 * Copyright (C) 2008 by INdT
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * @author Andre Moreira Magalhaes <andre.magalhaes@openbossa.org>
32 #define _XOPEN_SOURCE 600
33 #include <lightmediascanner_plugin.h>
34 #include <lightmediascanner_db.h>
35 #include <lightmediascanner_charset_conv.h>
36 #include <sys/types.h>
45 #define ID3V2_HEADER_SIZE 10
46 #define ID3V2_FOOTER_SIZE 10
47 #define ID3V2_FRAME_HEADER_SIZE 10
49 #define ID3V1_NUM_GENRES 148
51 #define ID3_NUM_ENCODINGS 5
54 ID3_ENCODING_LATIN1 = 0,
61 static const char *id3v1_genres[ID3V1_NUM_GENRES] = {
198 "Christian Gangsta Rap",
202 "Contemporary Christian",
212 struct id3v2_frame_header {
214 unsigned int frame_size;
216 int data_length_indicator;
226 } __attribute__((packed));
229 struct lms_plugin plugin;
230 lms_db_audio_t *audio_db;
231 lms_charset_conv_t *cs_convs[ID3_NUM_ENCODINGS];
234 static const char _name[] = "id3";
235 static const struct lms_string_size _exts[] = {
236 LMS_STATIC_STRING_SIZE(".mp3"),
237 LMS_STATIC_STRING_SIZE(".aac")
241 _to_uint(const char *data, int data_size)
243 unsigned int sum = 0;
244 unsigned int last, i;
246 last = data_size > 4 ? 3 : data_size - 1;
248 for (i = 0; i <= last; i++)
249 sum |= ((unsigned char) data[i]) << ((last - i) * 8);
255 _is_id3v2_second_synch_byte(unsigned char byte)
259 if ((byte & 0xE0) == 0xE0)
267 long buffer_offset = 0;
269 int buffer_size = sizeof(buffer);
270 const char pattern[] = "ID3";
273 /* These variables are used to keep track of a partial match that happens at
274 * the end of a buffer. */
275 int previous_partial_match = -1;
276 int previous_partial_synch_match = 0;
277 int first_synch_byte;
279 /* Start the search at the beginning of the file. */
280 lseek(fd, 0, SEEK_SET);
282 if ((nread = read(fd, &buffer, buffer_size)) != buffer_size)
285 /* check if pattern is in the beggining of the file */
286 if (memcmp(buffer, pattern, 3) == 0)
289 /* This loop is the crux of the find method. There are three cases that we
290 * want to account for:
291 * (1) The previously searched buffer contained a partial match of the search
292 * pattern and we want to see if the next one starts with the remainder of
295 * (2) The search pattern is wholly contained within the current buffer.
297 * (3) The current buffer ends with a partial match of the pattern. We will
298 * note this for use in the next iteration, where we will check for the rest
301 /* (1) previous partial match */
302 if (previous_partial_synch_match && _is_id3v2_second_synch_byte(buffer[0]))
305 if (previous_partial_match >= 0 && previous_partial_match < buffer_size) {
306 const int pattern_offset = buffer_size - previous_partial_match;
308 if (memcmp(buffer, pattern + pattern_offset, 3 - pattern_offset) == 0)
309 return buffer_offset - buffer_size + previous_partial_match;
312 /* (2) pattern contained in current buffer */
314 while ((p = memchr(p, 'I', buffer_size))) {
315 if (memcmp(p, pattern, 3) == 0)
316 return buffer_offset + (p - buffer);
320 p = memchr(buffer, 255, buffer_size);
322 first_synch_byte = p - buffer;
324 first_synch_byte = -1;
326 /* Here we have to loop because there could be several of the first
327 * (11111111) byte, and we want to check all such instances until we find
328 * a full match (11111111 111) or hit the end of the buffer. */
329 while (first_synch_byte >= 0) {
330 /* if this *is not* at the end of the buffer */
331 if (first_synch_byte < buffer_size - 1) {
332 if(_is_id3v2_second_synch_byte(buffer[first_synch_byte + 1]))
333 /* We've found the frame synch pattern. */
336 /* We found 11111111 at the end of the current buffer indicating a
337 * partial match of the synch pattern. The find() below should
338 * return -1 and break out of the loop. */
339 previous_partial_synch_match = 1;
342 /* Check in the rest of the buffer. */
343 p = memchr(p + 1, 255, buffer_size);
345 first_synch_byte = p - buffer;
347 first_synch_byte = -1;
350 /* (3) partial match */
351 if (buffer[nread - 1] == pattern[1])
352 previous_partial_match = nread - 1;
353 else if (memcmp(&buffer[nread - 2], pattern, 2) == 0)
354 previous_partial_match = nread - 2;
355 buffer_offset += buffer_size;
357 if ((nread = read(fd, &buffer, sizeof(buffer))) == -1)
365 _get_id3v2_frame_header_size(unsigned int version)
380 _parse_id3v2_frame_header(char *data, unsigned int version, struct id3v2_frame_header *fh)
386 memcpy(fh->frame_id, data, 3);
388 fh->frame_size = _to_uint(data + 3, 3);
390 fh->data_length_indicator = 0;
393 memcpy(fh->frame_id, data, 4);
394 fh->frame_size = _to_uint(data + 4, 4);
395 fh->compression = data[9] & 0x40;
396 fh->data_length_indicator = 0;
400 memcpy(fh->frame_id, data, 4);
401 fh->frame_size = _to_uint(data + 4, 4);
402 fh->compression = data[9] & 0x4;
403 fh->data_length_indicator = data[9] & 0x1;
409 _get_id3v2_frame_info(const char *frame_data, unsigned int frame_size, struct lms_string_size *s, lms_charset_conv_t *cs_conv)
411 s->str = malloc(sizeof(char) * (frame_size + 1));
412 memcpy(s->str, frame_data, frame_size);
413 s->str[frame_size] = '\0';
416 lms_charset_conv(cs_conv, &s->str, &s->len);
420 _parse_id3v2_frame(struct id3v2_frame_header *fh, const char *frame_data, struct lms_audio_info *info, lms_charset_conv_t **cs_convs)
422 lms_charset_conv_t *cs_conv = NULL;
423 unsigned int text_encoding, frame_size;
426 fprintf(stderr, "text encoding = %d\n", frame_data[0]);
435 text_encoding = frame_data[0];
436 if (text_encoding >= 0 && text_encoding < ID3_NUM_ENCODINGS)
437 cs_conv = cs_convs[text_encoding];
439 /* skip first byte - text encoding */
441 frame_size = fh->frame_size - 1;
443 if (memcmp(fh->frame_id, "TIT2", 4) == 0)
444 _get_id3v2_frame_info(frame_data, frame_size, &info->title, cs_conv);
445 else if (memcmp(fh->frame_id, "TPE1", 4) == 0)
446 _get_id3v2_frame_info(frame_data, frame_size, &info->artist, cs_conv);
447 else if (memcmp(fh->frame_id, "TALB", 4) == 0)
448 _get_id3v2_frame_info(frame_data, frame_size, &info->album, cs_conv);
449 else if (memcmp(fh->frame_id, "TCON", 4) == 0) {
450 /* TODO handle multiple genres */
453 _get_id3v2_frame_info(frame_data, frame_size, &info->genre, cs_conv);
456 for (i = 0; i < info->genre.len; ++i) {
457 if (!isdigit(info->genre.str[i]))
463 (info->genre.len > 0)) {
464 int genre = atoi(info->genre.str);
465 if (genre >= 0 && genre < ID3V1_NUM_GENRES) {
466 free(info->genre.str);
467 info->genre.str = strdup(id3v1_genres[(int) genre]);
468 info->genre.len = strlen(info->genre.str);
471 /* ignore other genres */
472 free(info->genre.str);
473 info->genre.str = NULL;
478 else if (memcmp(fh->frame_id, "TRCK", 4) == 0) {
479 struct lms_string_size trackno;
480 _get_id3v2_frame_info(frame_data, frame_size, &trackno, cs_conv);
481 info->trackno = atoi(trackno.str);
487 _parse_id3v2(int fd, long id3v2_offset, struct lms_audio_info *info, lms_charset_conv_t **cs_convs)
489 char header_data[10], frame_header_data[10];
490 unsigned int tag_size, major_version, frame_data_pos, frame_data_length, frame_header_size;
491 int extended_header, footer_present;
492 struct id3v2_frame_header fh;
494 lseek(fd, id3v2_offset, SEEK_SET);
497 if (read(fd, header_data, ID3V2_HEADER_SIZE) != ID3V2_HEADER_SIZE)
500 tag_size = _to_uint(header_data + 6, 4);
505 major_version = header_data[3];
508 frame_data_length = tag_size;
510 /* check for extended header */
511 extended_header = header_data[5] & 0x20; /* bit 6 */
512 if (extended_header) {
513 /* skip extended header */
514 unsigned int extended_header_size;
515 char extended_header_data[4];
517 if (read(fd, extended_header_data, 4) != 4)
519 extended_header_size = _to_uint(extended_header_data, 4);
520 lseek(fd, extended_header_size - 4, SEEK_CUR);
521 frame_data_pos += extended_header_size;
522 frame_data_length -= extended_header_size;
525 footer_present = header_data[5] & 0x8; /* bit 4 */
526 if (footer_present && frame_data_length > ID3V2_FOOTER_SIZE)
527 frame_data_length -= ID3V2_FOOTER_SIZE;
529 frame_header_size = _get_id3v2_frame_header_size(major_version);
530 while (frame_data_pos < frame_data_length - frame_header_size) {
531 if (read(fd, frame_header_data, ID3V2_FRAME_HEADER_SIZE) != ID3V2_FRAME_HEADER_SIZE)
534 if (frame_header_data[0] == 0)
537 _parse_id3v2_frame_header(frame_header_data, major_version, &fh);
539 if (!fh.compression &&
540 fh.frame_id[0] == 'T' &&
541 memcmp(fh.frame_id, "TXXX", 4) != 0) {
544 if (fh.data_length_indicator)
545 lseek(fd, 4, SEEK_CUR);
547 frame_data = malloc(sizeof(char) * fh.frame_size);
548 if (read(fd, frame_data, fh.frame_size) != fh.frame_size) {
553 _parse_id3v2_frame(&fh, frame_data, info, cs_convs);
557 if (fh.data_length_indicator)
558 lseek(fd, fh.frame_size + 4, SEEK_CUR);
560 lseek(fd, fh.frame_size, SEEK_CUR);
563 frame_data_pos += fh.frame_size + frame_header_size;
570 _parse_id3v1(int fd, struct lms_audio_info *info, lms_charset_conv_t **cs_convs)
572 lms_charset_conv_t *cs_conv;
573 struct id3v1_tag tag;
574 if (read(fd, &tag, sizeof(struct id3v1_tag)) == -1)
577 cs_conv = cs_convs[ID3_ENCODING_LATIN1];
579 info->title.str = strndup(tag.title, 30);
580 info->title.len = strlen(info->title.str);
581 lms_charset_conv(cs_conv, &info->title.str, &info->title.len);
582 info->artist.str = strndup(tag.artist, 30);
583 info->artist.len = strlen(info->artist.str);
584 lms_charset_conv(cs_conv, &info->artist.str, &info->artist.len);
585 info->album.str = strndup(tag.album, 30);
586 info->album.len = strlen(info->album.str);
587 lms_charset_conv(cs_conv, &info->album.str, &info->album.len);
588 if ((unsigned char) tag.genre < ID3V1_NUM_GENRES) {
589 info->genre.str = strdup(id3v1_genres[(unsigned char) tag.genre]);
590 info->genre.len = strlen(info->genre.str);
592 if (tag.comments[28] == 0 && tag.comments[29] != 0)
593 info->trackno = (unsigned char) tag.comments[29];
599 _match(struct plugin *p, const char *path, int len, int base)
603 i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
607 return (void*)(i + 1);
611 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
613 struct lms_audio_info info = {0, {0}, {0}, {0}, {0}, 0, 0, 0};
617 fd = open(finfo->path, O_RDONLY);
623 id3v2_offset = _find_id3v2(fd);
624 if (id3v2_offset >= 0) {
626 fprintf(stderr, "id3v2 tag found in file %s with offset %ld\n",
627 finfo->path, id3v2_offset);
629 if (_parse_id3v2(fd, id3v2_offset, &info, plugin->cs_convs) != 0) {
637 fprintf(stderr, "id3v2 tag not found in file %s. trying id3v1\n", finfo->path);
639 /* check for id3v1 tag */
640 if (lseek(fd, -128, SEEK_END) == -1) {
645 if (read(fd, &tag, 3) == -1) {
650 if (memcmp(tag, "TAG", 3) == 0) {
652 fprintf(stderr, "id3v1 tag found in file %s\n", finfo->path);
654 if (_parse_id3v1(fd, &info, plugin->cs_convs) != 0) {
661 lms_string_size_strip_and_free(&info.title);
662 lms_string_size_strip_and_free(&info.artist);
663 lms_string_size_strip_and_free(&info.album);
664 lms_string_size_strip_and_free(&info.genre);
666 if (!info.title.str) {
668 ext_idx = ((int)match) - 1;
669 info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
670 info.title.str = malloc((info.title.len + 1) * sizeof(char));
671 memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
672 info.title.str[info.title.len] = '\0';
674 lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
677 lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
679 lms_charset_conv(ctxt->cs_conv, &info.album.str, &info.album.len);
681 lms_charset_conv(ctxt->cs_conv, &info.genre.str, &info.genre.len);
684 fprintf(stderr, "file %s info\n", finfo->path);
685 fprintf(stderr, "\ttitle='%s'\n", info.title.str);
686 fprintf(stderr, "\tartist='%s'\n", info.artist.str);
687 fprintf(stderr, "\talbum='%s'\n", info.album.str);
688 fprintf(stderr, "\tgenre='%s'\n", info.genre.str);
689 fprintf(stderr, "\ttrack number='%d'\n", info.trackno);
693 r = lms_db_audio_add(plugin->audio_db, &info);
699 free(info.title.str);
701 free(info.artist.str);
703 free(info.album.str);
705 free(info.genre.str);
711 _setup(struct plugin *plugin, struct lms_context *ctxt)
714 const char *id3v2_encodings[ID3_NUM_ENCODINGS] = {
722 plugin->audio_db = lms_db_audio_new(ctxt->db);
723 if (!plugin->audio_db)
726 for (i = 0; i < ID3_NUM_ENCODINGS; ++i) {
727 /* do not create charset conv for UTF-8 encoding */
728 if (i == ID3_ENCODING_UTF8) {
729 plugin->cs_convs[i] = NULL;
732 plugin->cs_convs[i] = lms_charset_conv_new_full(0, 0);
733 if (!plugin->cs_convs[i])
735 lms_charset_conv_add(plugin->cs_convs[i], id3v2_encodings[i]);
742 _start(struct plugin *plugin, struct lms_context *ctxt)
744 return lms_db_audio_start(plugin->audio_db);
748 _finish(struct plugin *plugin, struct lms_context *ctxt)
752 if (plugin->audio_db)
753 lms_db_audio_free(plugin->audio_db);
755 for (i = 0; i < ID3_NUM_ENCODINGS; ++i) {
756 if (plugin->cs_convs[i])
757 lms_charset_conv_free(plugin->cs_convs[i]);
764 _close(struct plugin *plugin)
770 API struct lms_plugin *
771 lms_plugin_open(void)
773 struct plugin *plugin;
775 plugin = (struct plugin *)malloc(sizeof(*plugin));
776 plugin->plugin.name = _name;
777 plugin->plugin.match = (lms_plugin_match_fn_t)_match;
778 plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
779 plugin->plugin.close = (lms_plugin_close_fn_t)_close;
780 plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
781 plugin->plugin.start = (lms_plugin_start_fn_t)_start;
782 plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
784 return (struct lms_plugin *)plugin;