From: Andre Noll Date: Thu, 26 Dec 2013 20:22:12 +0000 (+0000) Subject: The mp3 tagger. X-Git-Tag: v0.5.6~100^2~2 X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=commitdiff_plain;h=30e8f0c610f0f3279475197e70e3be62af5cfcc9 The mp3 tagger. This commit adds support for adding and modifying the ID3 tags of mp3 files. Both ID3 version 1 and version 2 are supported. If no tags were found, a version 2 tag is added, otherwise only the existing tags are modified. The documentation is updated to mention that libid3tag is required for the new feature. --- diff --git a/error.h b/error.h index 697b545a..90a2fc9e 100644 --- a/error.h +++ b/error.h @@ -408,7 +408,10 @@ extern const char **para_errlist[]; PARA_ERROR(MP3_INFO, "could not read mp3 info"), \ PARA_ERROR(HEADER_FREQ, "invalid header frequency"), \ PARA_ERROR(HEADER_BITRATE, "invalid header bitrate"), \ - + PARA_ERROR(ID3_DETACH, "could not detach id3 frame"), \ + PARA_ERROR(ID3_ATTACH, "could not atttach id3 frame"), \ + PARA_ERROR(ID3_SETENCODING, "could not set id3 text encoding field"), \ + PARA_ERROR(ID3_SETSTRING, "could not set id3 string field"), \ #define AAC_AFH_ERRORS \ PARA_ERROR(STSZ, "did not find stcz atom"), \ diff --git a/mp3_afh.c b/mp3_afh.c index ccd28dad..484172ab 100644 --- a/mp3_afh.c +++ b/mp3_afh.c @@ -22,6 +22,7 @@ #include "error.h" #include "afh.h" #include "string.h" +#include "fd.h" /* * MIN_CONSEC_GOOD_FRAMES defines how many consecutive valid MP3 frames we need @@ -183,6 +184,201 @@ static int mp3_get_id3(unsigned char *map, size_t numbytes, __a_unused int fd, return ret; } +/* These helpers are not mentioned in the libid3tag header file. */ +void id3_field_init(union id3_field *field, enum id3_field_type type); +void id3_field_finish(union id3_field *field); + +/* + * Frames of type ID3_FRAME_COMMENT contain three fields of type language, + * string, and fullstring. The third field contains the actual comment. + */ +static int set_comment_fields(struct id3_frame *fr, id3_ucs4_t *ucs4_val) +{ + int ret; + union id3_field *field; + + field = id3_frame_field(fr, 1); + id3_field_init(field, ID3_FIELD_TYPE_LANGUAGE); + + field = id3_frame_field(fr, 2); + id3_field_init(field, ID3_FIELD_TYPE_STRING); + + field = id3_frame_field(fr, 3); + id3_field_init(field, ID3_FIELD_TYPE_STRINGFULL); + ret = id3_field_setfullstring(field, ucs4_val); + if (ret < 0) + return -E_ID3_SETSTRING; + return 1; +} + +/* + * UCS-4 stands for Universal Character Set where each character uses exactly + * 32 bits. It is also known as UTF-32, see ISO 10646. + * + * We have to use UCS-4 as an intermediate representation because the functions + * of libid3tag which set the tag content expect an array of UCS-4 characters. + * Fortunately, libid3tag contains conversion functions from and to UTF-8. + */ +static int replace_tag(char const *id, const char *val, struct id3_tag *id3_t, + int options) +{ + int ret; + struct id3_frame *fr; + union id3_field *field; + id3_ucs4_t *ucs4_val; + + /* First, detach all frames that match the given id. */ + while ((fr = id3_tag_findframe(id3_t, id, 0))) { + PARA_INFO_LOG("deleting %s frame\n", id); + ret = id3_tag_detachframe(id3_t, fr); + if (ret < 0) + return -E_ID3_DETACH; + id3_frame_delete(fr); + } + if (!val || !*val) + return 0; + fr = id3_frame_new(id); + PARA_DEBUG_LOG("frame desc: %s, %d fields\n", fr->description, fr->nfields); + + /* Frame 0 contains the encoding. We always use UTF-8. */ + field = id3_frame_field(fr, 0); + id3_field_init(field, ID3_FIELD_TYPE_TEXTENCODING); + ret = id3_field_settextencoding(field, ID3_FIELD_TEXTENCODING_UTF_8); + if (ret < 0) + return -E_ID3_SETENCODING; + /* create UCS-4 representation */ + ucs4_val = id3_utf8_ucs4duplicate((const id3_utf8_t *)val); + + if (strcmp(id, ID3_FRAME_COMMENT) == 0) + ret = set_comment_fields(fr, ucs4_val); + else { + /* Non-comment frames contain the value in field 1. */ + field = id3_frame_field(fr, 1); + if (options & ID3_TAG_OPTION_ID3V1) { + id3_ucs4_t *ptr[] = {ucs4_val, NULL}; + id3_field_init(field, ID3_FIELD_TYPE_STRINGLIST); + ret = id3_field_setstrings(field, 1, ptr); + } else { + id3_field_init(field, ID3_FIELD_TYPE_STRINGFULL); + ret = id3_field_setfullstring(field, ucs4_val); + } + } + free(ucs4_val); + if (ret < 0) + return -E_ID3_SETSTRING; + + PARA_INFO_LOG("attaching %s frame, value: %s\n", id, val); + ret = id3_tag_attachframe(id3_t, fr); + if (ret < 0) + return -E_ID3_ATTACH; + return 1; +} + +static int replace_tags(struct id3_tag *id3_t, struct taginfo *tags) +{ + int ret, options = id3_tag_options(id3_t, 0, 0); + + ret = replace_tag(ID3_FRAME_ARTIST, tags->artist, id3_t, options); + if (ret < 0) + return ret; + ret = replace_tag(ID3_FRAME_TITLE, tags->title, id3_t, options); + if (ret < 0) + return ret; + ret = replace_tag(ID3_FRAME_ALBUM, tags->album, id3_t, options); + if (ret < 0) + return ret; + ret = replace_tag(ID3_FRAME_YEAR, tags->year, id3_t, options); + if (ret < 0) + return ret; + ret = replace_tag(ID3_FRAME_COMMENT, tags->comment, id3_t, options); + if (ret < 0) + return ret; + return 1; +} + +/* id3_tag_delete() does not seem to work due to refcount issues. */ +static void free_tag(struct id3_tag *id3_t) +{ + int i, j; + for (i = 0; i < id3_t->nframes; i++) { + struct id3_frame *fr = id3_t->frames[i]; + for (j = 0; j < fr->nfields; j++) { + union id3_field *field = &fr->fields[j]; + id3_field_finish(field); + } + free(fr); + } + free(id3_t->frames); + free(id3_t); +} + +static int mp3_rewrite_tags(const char *map, size_t mapsize, + struct taginfo *tags, int fd, __a_unused const char *filename) +{ + int ret; + id3_length_t old_v2size, new_v2size; + id3_byte_t v1_buffer[128], *v2_buffer; + size_t data_sz; + struct id3_tag *v1_tag = NULL, *v2_tag = NULL; + + if (mapsize >= 128) { + v1_tag = id3_tag_parse((const id3_byte_t *)map + mapsize - 128, + 128); + if (v1_tag) { + PARA_NOTICE_LOG("replacing id3v1 tag\n"); + ret = replace_tags(v1_tag, tags); + if (ret < 0) + goto out; + id3_tag_render(v1_tag, v1_buffer); + } + } + old_v2size = 0; + v2_tag = id3_tag_parse((const id3_byte_t *)map, mapsize); + if (v2_tag) { + PARA_NOTICE_LOG("replacing id3v2 tag\n"); + old_v2size = v2_tag->paddedsize; + } else if (!v1_tag) { + PARA_NOTICE_LOG("no id3 tags found, adding id3v2 tag\n"); + v2_tag = id3_tag_new(); + assert(v2_tag); + } + if (v2_tag) { + /* + * Turn off all options to avoid creating an extended header. + * id321 does not understand it. + */ + id3_tag_options(v2_tag, ~0U, 0); + ret = replace_tags(v2_tag, tags); + if (ret < 0) + goto out; + new_v2size = id3_tag_render(v2_tag, NULL); + v2_buffer = para_malloc(new_v2size); + id3_tag_render(v2_tag, v2_buffer); + PARA_INFO_LOG("writing v2 tag (%lu bytes)\n", new_v2size); + ret = write_all(fd, (char *)v2_buffer, new_v2size); + free(v2_buffer); + if (ret < 0) + goto out; + } + data_sz = mapsize - old_v2size; + if (v1_tag && data_sz >= 128) + data_sz -= 128; + PARA_INFO_LOG("writing data part (%zu bytes)\n", data_sz); + ret = write_all(fd, map + old_v2size, data_sz); + if (ret < 0) + goto out; + if (v1_tag) { + PARA_INFO_LOG("writing v1 tag\n"); + ret = write_all(fd, (char *)v1_buffer, 128); + } +out: + if (v1_tag) + free_tag(v1_tag); + if (v2_tag) + free_tag(v2_tag); + return ret; +} + #else /* HAVE_ID3TAG */ /* @@ -499,4 +695,7 @@ void mp3_init(struct audio_format_handler *afh) { afh->get_file_info = mp3_get_file_info; afh->suffixes = mp3_suffixes; +#ifdef HAVE_LIBID3TAG + afh->rewrite_tags = mp3_rewrite_tags; +#endif /* HAVE_LIBID3TAG */ } diff --git a/web/manual.m4 b/web/manual.m4 index b7e05978..e5ff1ab1 100644 --- a/web/manual.m4 +++ b/web/manual.m4 @@ -259,7 +259,8 @@ Optional: - XREFERENCE(http://www.underbit.com/products/mad/, libid3tag). For version-2 ID3 tag support, you'll need the libid3tag development package libid3tag0-dev. Without - libid3tag, only version one tags are recognized. + libid3tag, only version-1 tags are recognized. The mp3 tagger + also needs this library for modifying (id3v1 and id3v2) tags. - XREFERENCE(http://www.xiph.org/downloads/, ogg vorbis). For ogg vorbis streams you'll need libogg, libvorbis,