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>
19 * @author Gustavo Sverzut Barbieri <gustavo.barbieri@openbossa.org>
33 #define _XOPEN_SOURCE 600
34 #include <lightmediascanner_plugin.h>
35 #include <lightmediascanner_db.h>
36 #include <lightmediascanner_charset_conv.h>
37 #include <sys/types.h>
46 #define ID3V2_HEADER_SIZE 10
47 #define ID3V2_FOOTER_SIZE 10
50 ID3_ENCODING_LATIN1 = 0,
57 #define ID3_NUM_ENCODINGS ID3_ENCODING_LAST
60 #include "id3v1_genres.c"
63 struct lms_string_size title;
64 struct lms_string_size artist;
65 struct lms_string_size album;
66 struct lms_string_size genre;
67 unsigned char trackno;
68 int cur_artist_priority;
71 struct id3v2_frame_header {
73 unsigned int frame_size;
75 int data_length_indicator;
85 } __attribute__((packed));
88 struct lms_plugin plugin;
89 lms_db_audio_t *audio_db;
90 lms_charset_conv_t *cs_convs[ID3_NUM_ENCODINGS];
93 static const char _name[] = "id3";
94 static const struct lms_string_size _exts[] = {
95 LMS_STATIC_STRING_SIZE(".mp3"),
96 LMS_STATIC_STRING_SIZE(".aac")
100 _to_uint(const char *data, int data_size)
102 unsigned int sum = 0;
103 unsigned int last, i;
105 last = data_size > 4 ? 3 : data_size - 1;
107 for (i = 0; i <= last; i++)
108 sum |= ((unsigned char) data[i]) << ((last - i) * 8);
114 _is_id3v2_second_synch_byte(unsigned char byte)
118 if ((byte & 0xE0) == 0xE0)
126 static const char pattern[3] = "ID3";
128 unsigned int prev_part_match, prev_part_match_sync = 0;
131 if (read(fd, buffer, sizeof(buffer)) != sizeof(buffer))
134 if (memcmp(buffer, pattern, sizeof(pattern)) == 0)
137 /* This loop is the crux of the find method. There are three cases that we
138 * want to account for:
139 * (1) The previously searched buffer contained a partial match of the
140 * search pattern and we want to see if the next one starts with the
141 * remainder of that pattern.
143 * (2) The search pattern is wholly contained within the current buffer.
145 * (3) The current buffer ends with a partial match of the pattern. We will
146 * note this for use in the next iteration, where we will check for the rest
150 prev_part_match_sync = 0;
153 const char *p, *p_end;
155 /* (1) previous partial match */
156 if (prev_part_match_sync) {
157 if (_is_id3v2_second_synch_byte(buffer[0]))
159 prev_part_match_sync = 0;
162 if (prev_part_match) {
163 const int size = sizeof(buffer) - prev_part_match;
164 const char *part_pattern = pattern + prev_part_match;
166 if (memcmp(buffer, part_pattern, size) == 0)
167 return buffer_offset - prev_part_match;
172 p_end = buffer + sizeof(buffer);
173 for (p = buffer; p < p_end; p++) {
174 if (*p == pattern[0]) {
175 /* Try to match pattern, possible partial contents */
181 if (todo == 0 || memcmp(q, pattern + 1, todo) == 0) {
183 if (todo == sizeof(buffer))
184 /* (2) pattern contained in current buffer */
185 return buffer_offset;
187 /* (3) partial match */
188 prev_part_match = todo;
191 } else if ((unsigned char)*p == 0xff) {
192 /* Try to match synch pattern, possible partial contents */
197 if (_is_id3v2_second_synch_byte(*q))
198 /* (2) synch pattern contained in current buffer */
201 /* (3) partial match */
202 prev_part_match_sync = 1;
206 if (read(fd, buffer, sizeof(buffer)) != sizeof(buffer))
208 buffer_offset += sizeof(buffer);
215 _get_id3v2_frame_header_size(unsigned int version)
230 _parse_id3v2_frame_header(char *data, unsigned int version, struct id3v2_frame_header *fh)
236 memcpy(fh->frame_id, data, 3);
238 fh->frame_size = _to_uint(data + 3, 3);
240 fh->data_length_indicator = 0;
243 memcpy(fh->frame_id, data, 4);
244 fh->frame_size = _to_uint(data + 4, 4);
245 fh->compression = data[9] & 0x40;
246 fh->data_length_indicator = 0;
250 memcpy(fh->frame_id, data, 4);
251 fh->frame_size = _to_uint(data + 4, 4);
252 fh->compression = data[9] & 0x4;
253 fh->data_length_indicator = data[9] & 0x1;
259 _get_id3v2_frame_info(const char *frame_data, unsigned int frame_size, struct lms_string_size *s, lms_charset_conv_t *cs_conv, int strip)
261 if (frame_size > s->len)
262 s->str = realloc(s->str, sizeof(char) * (frame_size + 1));
263 memcpy(s->str, frame_data, frame_size);
264 s->str[frame_size] = '\0';
267 lms_charset_conv(cs_conv, &s->str, &s->len);
269 lms_string_size_strip_and_free(s);
273 _get_id3v2_artist(unsigned int index, const char *frame_data, unsigned int frame_size, struct id3_info *info, lms_charset_conv_t *cs_conv)
275 static const unsigned char artist_priorities[] = {3, 4, 2, 1};
276 const unsigned int index_max = sizeof(artist_priorities) / sizeof(*artist_priorities);
278 if (index >= index_max)
281 if (artist_priorities[index] > info->cur_artist_priority) {
282 struct lms_string_size artist = {0};
284 _get_id3v2_frame_info(frame_data, frame_size, &artist, cs_conv, 1);
286 if (info->artist.str)
287 free(info->artist.str);
288 info->artist = artist;
289 info->cur_artist_priority = artist_priorities[index];
296 _get_id3v1_genre(unsigned int genre, struct lms_string_size *out)
298 if (genre < ID3V1_NUM_GENRES) {
299 unsigned int size, base, len;
301 base = id3v1_genres_offsets[genre];
302 size = id3v1_genres_offsets[genre + 1] - base;
305 if (len > out->len) {
306 char *p = realloc(out->str, size);
313 memcpy(out->str, id3v1_genres_mem + base, size);
321 _parse_id3v1_genre(const char *str_genre, struct lms_string_size *out)
323 return _get_id3v1_genre(atoi(str_genre), out);
327 _get_id3v2_genre(const char *frame_data, unsigned int frame_size, struct lms_string_size *out, lms_charset_conv_t *cs_conv)
330 struct lms_string_size genre = {0};
332 _get_id3v2_frame_info(frame_data, frame_size, &genre, cs_conv, 1);
337 is_number = (genre.len != 0 && genre.str[0] != '(');
339 for (i = 0; i < genre.len; ++i) {
340 if (!isdigit(genre.str[i])) {
347 if (is_number && _parse_id3v1_genre(genre.str, out) == 0) {
348 /* id3v1 genre found */
353 /* ID3v2.3 "content type" can contain a ID3v1 genre number in parenthesis at
354 * the beginning of the field. If this is all that the field contains, do a
355 * translation from that number to the name and return that. If there is a
356 * string folloing the ID3v1 genre number, that is considered to be
357 * authoritative and we return that instead. Or finally, the field may
358 * simply be free text, in which case we just return the value. */
360 if (genre.len > 1 && genre.str[0] == '(') {
361 char *closing = NULL;
363 if (genre.str[genre.len - 1] == ')') {
364 closing = strchr(genre.str, ')');
365 if (closing == genre.str + genre.len - 1) {
366 /* ) is the last character and only appears once in the
367 * string get the id3v1 genre enclosed by parentheses
369 if (_parse_id3v1_genre(genre.str + 1, out) == 0) {
376 /* get the string followed by the id3v1 genre */
378 closing = strchr(genre.str, ')');
382 out->len = genre.len - (closing - genre.str);
383 out->str = genre.str;
384 memmove(out->str, closing, out->len + 1); /* includes '\0' */
385 lms_string_size_strip_and_free(out);
395 _get_id3v2_trackno(const char *frame_data, unsigned int frame_size, struct id3_info *info, lms_charset_conv_t *cs_conv)
397 struct lms_string_size trackno = {0};
399 _get_id3v2_frame_info(frame_data, frame_size, &trackno, cs_conv, 0);
400 info->trackno = atoi(trackno.str);
405 _parse_id3v2_frame(struct id3v2_frame_header *fh, const char *frame_data, struct id3_info *info, lms_charset_conv_t **cs_convs)
407 lms_charset_conv_t *cs_conv;
408 unsigned int text_encoding, frame_size;
412 fprintf(stderr, "frame id = %.4s frame size = %d text encoding = %d\n",
413 fh->frame_id, fh->frame_size, frame_data[0]);
416 /* All used frames start with 'T' */
427 text_encoding = frame_data[0];
429 /* skip first byte - text encoding */
431 frame_size = fh->frame_size - 1;
433 if (text_encoding < ID3_NUM_ENCODINGS) {
434 if (text_encoding == ID3_ENCODING_UTF16) {
435 if (memcmp(frame_data, "\xfe\xff", 2) == 0)
436 text_encoding = ID3_ENCODING_UTF16BE;
438 text_encoding = ID3_ENCODING_UTF16LE;
442 cs_conv = cs_convs[text_encoding];
446 /* ID3v2.2 used 3 bytes for the frame id, so let's check it */
447 if ((fid[1] == 'T' && fid[2] == '2') ||
448 (fid[1] == 'I' && fid[2] == 'T' && fid[3] == '2'))
449 _get_id3v2_frame_info(frame_data, frame_size, &info->title, cs_conv, 1);
450 else if (fid[1] == 'P') {
452 _get_id3v2_artist(fid[3] - '1', frame_data, frame_size,
454 else if (fid[2] >= '1' && fid[2] <= '4')
455 _get_id3v2_artist(fid[2] - '1', frame_data, frame_size,
459 else if (fid[1] == 'A' && fid[2] == 'L')
460 _get_id3v2_frame_info(frame_data, frame_size, &info->album, cs_conv, 1);
462 else if (fid[1] == 'C' && fid[2] == 'O')
463 _get_id3v2_genre(frame_data, frame_size, &info->genre, cs_conv);
464 else if (fid[1] == 'R' && (fid[2] == 'K' ||
465 (fid[2] == 'C' && fid[3] == 'K')))
466 _get_id3v2_trackno(frame_data, frame_size, info, cs_conv);
470 _parse_id3v2(int fd, long id3v2_offset, struct id3_info *info, lms_charset_conv_t **cs_convs)
472 char header_data[10], frame_header_data[10];
473 unsigned int tag_size, major_version, frame_data_pos, frame_data_length, frame_header_size;
474 int extended_header, footer_present;
475 struct id3v2_frame_header fh;
478 lseek(fd, id3v2_offset, SEEK_SET);
481 if (read(fd, header_data, ID3V2_HEADER_SIZE) != ID3V2_HEADER_SIZE)
484 tag_size = _to_uint(header_data + 6, 4);
489 major_version = header_data[3];
492 frame_data_length = tag_size;
494 /* check for extended header */
495 extended_header = header_data[5] & 0x20; /* bit 6 */
496 if (extended_header) {
497 /* skip extended header */
498 unsigned int extended_header_size;
499 char extended_header_data[4];
501 if (read(fd, extended_header_data, 4) != 4)
503 extended_header_size = _to_uint(extended_header_data, 4);
504 lseek(fd, extended_header_size - 4, SEEK_CUR);
505 frame_data_pos += extended_header_size;
506 frame_data_length -= extended_header_size;
509 footer_present = header_data[5] & 0x8; /* bit 4 */
510 if (footer_present && frame_data_length > ID3V2_FOOTER_SIZE)
511 frame_data_length -= ID3V2_FOOTER_SIZE;
513 frame_header_size = _get_id3v2_frame_header_size(major_version);
514 while (frame_data_pos < frame_data_length - frame_header_size) {
515 nread = read(fd, frame_header_data, frame_header_size);
519 if (nread != frame_header_size)
522 if (frame_header_data[0] == 0)
525 _parse_id3v2_frame_header(frame_header_data, major_version, &fh);
529 if (!fh.compression &&
530 fh.frame_id[0] == 'T' &&
531 memcmp(fh.frame_id, "TXXX", 4) != 0) {
534 if (fh.data_length_indicator)
535 lseek(fd, 4, SEEK_CUR);
537 frame_data = malloc(sizeof(char) * fh.frame_size);
538 if (read(fd, frame_data, fh.frame_size) != fh.frame_size) {
543 _parse_id3v2_frame(&fh, frame_data, info, cs_convs);
547 if (fh.data_length_indicator)
548 lseek(fd, fh.frame_size + 4, SEEK_CUR);
550 lseek(fd, fh.frame_size, SEEK_CUR);
553 frame_data_pos += fh.frame_size + frame_header_size;
560 _parse_id3v1(int fd, struct id3_info *info, lms_charset_conv_t *cs_conv)
562 struct id3v1_tag tag;
563 if (read(fd, &tag, sizeof(struct id3v1_tag)) == -1)
566 info->title.str = strndup(tag.title, 30);
567 info->title.len = strlen(info->title.str);
568 lms_charset_conv(cs_conv, &info->title.str, &info->title.len);
569 info->artist.str = strndup(tag.artist, 30);
570 info->artist.len = strlen(info->artist.str);
571 lms_charset_conv(cs_conv, &info->artist.str, &info->artist.len);
572 info->album.str = strndup(tag.album, 30);
573 info->album.len = strlen(info->album.str);
574 lms_charset_conv(cs_conv, &info->album.str, &info->album.len);
575 _get_id3v1_genre(tag.genre, &info->genre);
576 if (tag.comments[28] == 0 && tag.comments[29] != 0)
577 info->trackno = (unsigned char) tag.comments[29];
583 _match(struct plugin *p, const char *path, int len, int base)
587 i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
591 return (void*)(i + 1);
595 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
597 struct id3_info info = {{0}, {0}, {0}, {0}, 0, -1};
598 struct lms_audio_info audio_info = {0, {0}, {0}, {0}, {0}, 0, 0, 0};
602 fd = open(finfo->path, O_RDONLY);
608 id3v2_offset = _find_id3v2(fd);
609 if (id3v2_offset >= 0) {
611 fprintf(stderr, "id3v2 tag found in file %s with offset %ld\n",
612 finfo->path, id3v2_offset);
614 if (_parse_id3v2(fd, id3v2_offset, &info, plugin->cs_convs) != 0) {
622 fprintf(stderr, "id3v2 tag not found in file %s. trying id3v1\n",
625 /* check for id3v1 tag */
626 if (lseek(fd, -128, SEEK_END) == -1) {
631 if (read(fd, &tag, 3) == -1) {
636 if (memcmp(tag, "TAG", 3) == 0) {
638 fprintf(stderr, "id3v1 tag found in file %s\n", finfo->path);
640 if (_parse_id3v1(fd, &info, ctxt->cs_conv) != 0) {
647 if (!info.title.str) {
649 ext_idx = ((int)match) - 1;
650 info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
651 info.title.str = malloc((info.title.len + 1) * sizeof(char));
652 memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
653 info.title.str[info.title.len] = '\0';
654 lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
658 fprintf(stderr, "file %s info\n", finfo->path);
659 fprintf(stderr, "\ttitle='%s'\n", info.title.str);
660 fprintf(stderr, "\tartist='%s'\n", info.artist.str);
661 fprintf(stderr, "\talbum='%s'\n", info.album.str);
662 fprintf(stderr, "\tgenre='%s'\n", info.genre.str);
663 fprintf(stderr, "\ttrack number='%d'\n", info.trackno);
666 audio_info.id = finfo->id;
667 audio_info.title = info.title;
668 audio_info.artist = info.artist;
669 audio_info.album = info.album;
670 audio_info.genre = info.genre;
671 audio_info.trackno = info.trackno;
672 r = lms_db_audio_add(plugin->audio_db, &audio_info);
675 posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
679 free(info.title.str);
681 free(info.artist.str);
683 free(info.album.str);
685 free(info.genre.str);
691 _setup(struct plugin *plugin, struct lms_context *ctxt)
694 const char *id3_encodings[ID3_NUM_ENCODINGS] = {
702 plugin->audio_db = lms_db_audio_new(ctxt->db);
703 if (!plugin->audio_db)
706 for (i = 0; i < ID3_NUM_ENCODINGS; ++i) {
707 /* do not create charset conv for UTF-8 encoding */
708 if (!id3_encodings[i]) {
709 plugin->cs_convs[i] = NULL;
712 plugin->cs_convs[i] = lms_charset_conv_new_full(0, 0);
713 if (!plugin->cs_convs[i])
715 lms_charset_conv_add(plugin->cs_convs[i], id3_encodings[i]);
722 _start(struct plugin *plugin, struct lms_context *ctxt)
724 return lms_db_audio_start(plugin->audio_db);
728 _finish(struct plugin *plugin, struct lms_context *ctxt)
732 if (plugin->audio_db)
733 lms_db_audio_free(plugin->audio_db);
735 for (i = 0; i < ID3_NUM_ENCODINGS; ++i) {
736 if (plugin->cs_convs[i])
737 lms_charset_conv_free(plugin->cs_convs[i]);
744 _close(struct plugin *plugin)
750 API struct lms_plugin *
751 lms_plugin_open(void)
753 struct plugin *plugin;
755 plugin = (struct plugin *)malloc(sizeof(*plugin));
756 plugin->plugin.name = _name;
757 plugin->plugin.match = (lms_plugin_match_fn_t)_match;
758 plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
759 plugin->plugin.close = (lms_plugin_close_fn_t)_close;
760 plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
761 plugin->plugin.start = (lms_plugin_start_fn_t)_start;
762 plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
764 return (struct lms_plugin *)plugin;