-/*
- * Copyright (C) 2003-2009 Andre Noll <maan@systemlinux.org>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* Copyright (C) 2003 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
/** \file mp3_afh.c para_server's mp3 audio format handler */
* Johannes Overmann <overmann@iname.com>
*/
+#include <regex.h>
+
#include "para.h"
#include "error.h"
#include "afh.h"
#include "string.h"
-#include "afs.h"
-#include "server.h"
-
-/** \cond some defines and structs which are only used in this file */
+#include "fd.h"
/*
* MIN_CONSEC_GOOD_FRAMES defines how many consecutive valid MP3 frames we need
unsigned int emphasis;
};
-/** \endcond */
static const int frequencies[3][4] = {
{22050,24000,16000,50000}, /* MPEG 2.0 */
{44100,48000,32000,50000}, /* MPEG 1.0 */
};
static const int frame_size_index[] = {24000, 72000, 72000};
-static const char *mode_text[] = {"stereo", "joint stereo", "dual channel", "mono", "invalid"};
-
-#ifdef HAVE_LIBID3TAG
+#ifdef HAVE_ID3TAG
#include <id3tag.h>
-static char *get_latin1(id3_ucs4_t const *string)
+static char *get_utf8(id3_ucs4_t const *string)
{
if (!string)
return NULL;
- return (char *)id3_ucs4_latin1duplicate(string);
+ return (char *)id3_ucs4_utf8duplicate(string);
}
static char *get_stringlist(union id3_field *field)
char *result = NULL;
for (k = 0; k < nstrings; k++) {
- char *tmp = (char *)get_latin1(id3_field_getstrings(field, k));
+ char *tmp = (char *)get_utf8(id3_field_getstrings(field, k));
if (result) {
char *tmp2 = result;
result = make_message("%s %s", tmp2, tmp);
{
id3_ucs4_t const *string = id3_field_getfullstring(field);
- return get_latin1(string);
+ return get_utf8(string);
}
#define FOR_EACH_FIELD(f, j, fr) for (j = 0; j < (fr)->nfields && \
return NULL;
}
-static char *mp3_get_id3(__a_unused unsigned char *map,
- __a_unused size_t numbytes, int fd)
+/* this only sets values which are undefined so far */
+static void parse_frames(struct id3_tag *id3_t, struct taginfo *tags)
{
int i;
- struct id3_tag *id3_t;
- char *title = NULL, *artist = NULL, *album = NULL, *year = NULL,
- *comment = NULL, *result;
- struct id3_file *id3_f = id3_file_fdopen(fd, ID3_FILE_MODE_READONLY);
- if (!id3_f)
- goto no_tag;
- id3_t = id3_file_tag(id3_f);
- if (!id3_t)
- goto no_tag;
for (i = 0; i < id3_t->nframes; i++) {
struct id3_frame *fr = id3_t->frames[i];
- if (!strcmp(fr->id, "TIT2")) {
- if (!title)
- title = get_strings(fr);
+ if (!strcmp(fr->id, ID3_FRAME_TITLE)) {
+ if (!tags->title)
+ tags->title = get_strings(fr);
continue;
}
- if (!strcmp(fr->id, "TPE1")) {
- if (!artist)
- artist = get_strings(fr);
+ if (!strcmp(fr->id, ID3_FRAME_ARTIST)) {
+ if (!tags->artist)
+ tags->artist = get_strings(fr);
continue;
}
- if (!strcmp(fr->id, "TALB")) {
- if (!album)
- album = get_strings(fr);
+ if (!strcmp(fr->id, ID3_FRAME_ALBUM)) {
+ if (!tags->album)
+ tags->album = get_strings(fr);
continue;
}
- if (!strcmp(fr->id, "TDRC")) {
- if (!year)
- year = get_strings(fr);
+ if (!strcmp(fr->id, ID3_FRAME_YEAR)) {
+ if (!tags->year)
+ tags->year = get_strings(fr);
continue;
}
- if (!strcmp(fr->id, "COMM")) {
- if (!comment)
- comment = get_strings(fr);
+ if (!strcmp(fr->id, ID3_FRAME_COMMENT)) {
+ if (!tags->comment)
+ tags->comment = get_strings(fr);
continue;
}
}
- id3_file_close(id3_f);
- result = make_taginfo(title, artist, album, year, comment);
- free(title);
- free(artist);
- free(album);
- free(year);
- free(comment);
- return result;
-no_tag:
- if (id3_f)
- id3_file_close(id3_f);
- return make_message("%s: (no id3 v1/v2 tag)\n%s:\n",
- status_item_list[SI_TAGINFO1],
- status_item_list[SI_TAGINFO2]);
}
-#else /* HAVE_LIBID3TAG */
+static int mp3_get_id3(unsigned char *map, size_t numbytes, __a_unused int fd,
+ struct taginfo *tags)
+{
+ int ret = 0;
+ struct id3_tag *id3_t;
+
+ /* id3v2 tags are usually located at the beginning. */
+ id3_t = id3_tag_parse(map, numbytes);
+ if (id3_t) {
+ parse_frames(id3_t, tags);
+ ret |= 2;
+ id3_tag_delete(id3_t);
+ }
+ /* Also look for an id3v1 tag at the end of the file. */
+ if (numbytes >= 128) {
+ id3_t = id3_tag_parse(map + numbytes - 128, 128);
+ if (id3_t) {
+ parse_frames(id3_t, tags);
+ ret |= 1;
+ id3_tag_delete(id3_t);
+ }
+ }
+ 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, %u 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;
+
+ if (!id3_t)
+ return;
+ 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 {
+ PARA_NOTICE_LOG("adding id3v2 tag\n");
+ v2_tag = id3_tag_new();
+ assert(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:
+ free_tag(v1_tag);
+ free_tag(v2_tag);
+ return ret;
+}
+
+#else /* HAVE_ID3TAG */
/*
* Remove trailing whitespace from the end of a string
return string;
}
-static char *mp3_get_id3(unsigned char *map, size_t numbytes, __a_unused int fd)
+static int mp3_get_id3(unsigned char *map, size_t numbytes, __a_unused int fd,
+ struct taginfo *tags)
{
char title[31], artist[31], album[31], year[5], comment[31];
off_t fpos;
if (numbytes < 128 || strncmp("TAG", (char *)map + numbytes - 128, 3)) {
PARA_DEBUG_LOG("no id3 v1 tag\n");
- return make_message("%s: (no id3 v1 tag)\n%s:\n",
- status_item_list[SI_TAGINFO1],
- status_item_list[SI_TAGINFO2]);
+ return 0;
}
fpos = numbytes - 125;
memcpy(title, map + fpos, 30);
unpad(album);
unpad(year);
unpad(comment);
- return make_taginfo(title, artist, album, year, comment);
+ tags->artist = para_strdup(artist);
+ tags->title = para_strdup(title);
+ tags->year = para_strdup(year);
+ tags->album = para_strdup(album);
+ tags->comment = para_strdup(comment);
+ return 1;
}
-#endif /* HAVE_LIBID3TAG */
+#endif /* HAVE_ID3TAG */
static int header_frequency(struct mp3header *h)
{
return frequencies[h->version][h->freq];
}
-static const char *header_mode(struct mp3header *h)
+static const char *header_mode(const struct mp3header *h)
{
- if (h->mode > 4)
- h->mode = 4; /* invalid */
+ const char * const mode_text[] = {"stereo", "joint stereo",
+ "dual channel", "mono"};
+
+ if (h->mode >= ARRAY_SIZE(mode_text))
+ return "invalid";
return mode_text[h->mode];
}
static int mp3_read_info(unsigned char *map, size_t numbytes, int fd,
struct afh_info *afhi)
{
- long freq_avg = 0, br_avg = 0;
- int ret, len = 0, old_br = -1, vbr = 0;
+ uint64_t freq_sum = 0, br_sum = 0;
+ int fl = 0, ret, len = 0, old_br = -1, vbr = 0;
struct timeval total_time = {0, 0};
unsigned chunk_table_size = 1000; /* gets increased on demand */
off_t fpos = 0;
struct mp3header header;
- char *taginfo;
+ const char *tag_versions[] = {"no", "id3v1", "id3v2", "id3v1+id3v2"};
afhi->chunks_total = 0;
afhi->chunk_table = para_malloc(chunk_table_size * sizeof(uint32_t));
- taginfo = mp3_get_id3(map, numbytes, fd);
while (1) {
- unsigned long freq, br, fl;
+ int freq, br;
struct timeval tmp, cct; /* current chunk time */
fpos += len;
len = find_valid_start(map, numbytes, &fpos, &header);
- if (len <= 0)
+ if (len <= 0) {
+ size_t end;
+ ret = -E_MP3_INFO;
+ if (!afhi->chunks_total)
+ goto err_out;
+ end = afhi->chunk_table[afhi->chunks_total - 1] + fl;
+ afhi->chunk_table[afhi->chunks_total]
+ = PARA_MIN(end, numbytes);
break;
+ }
ret = header_frequency(&header);
if (ret < 0)
continue;
if (ret < 0)
continue;
fl = ret;
- tmp.tv_sec = fl;
+ tmp.tv_sec = fl - header.padding;
tmp.tv_usec = 0;
tv_divide(br * 125, &tmp, &cct);
tv_add(&cct, &total_time, &tmp);
total_time = tmp;
- //PARA_DEBUG_LOG("%s: br: %d, freq: %d, fl: %d, cct: %lu\n", __func__, br, freq, fl, cct.tv_usec);
if (afhi->chunks_total >= chunk_table_size) {
chunk_table_size *= 2;
afhi->chunk_table = para_realloc(afhi->chunk_table,
chunk_table_size * sizeof(uint32_t));
}
afhi->chunk_table[afhi->chunks_total] = fpos;
-// if (afhi->chunks_total < 10 || !(afhi->chunks_total % 1000))
-// PARA_INFO_LOG("chunk #%lu: %zd\n", afhi->chunks_total,
-// afhi->chunk_table[afhi->chunks_total]);
afhi->chunks_total++;
- if (afhi->chunks_total == 1) {
- freq_avg = freq;
- br_avg = br;
- old_br = br;
- continue;
- }
- freq_avg += ((long)freq - freq_avg) / ((long)afhi->chunks_total + 1);
- br_avg += ((long)br - br_avg) / ((long)afhi->chunks_total + 1);
- if (old_br != br)
+ freq_sum += freq;
+ br_sum += br;
+ if (afhi->chunks_total != 1 && old_br != br)
vbr = 1;
old_br = br;
}
ret = -E_MP3_INFO;
- if (!afhi->chunks_total || !freq_avg || !br_avg)
+ if (!freq_sum || !br_sum)
goto err_out;
- afhi->chunk_table[afhi->chunks_total] = numbytes - 1;
- afhi->bitrate = br_avg;
- afhi->frequency = freq_avg;
+ afhi->bitrate = br_sum / afhi->chunks_total;
+ afhi->frequency = freq_sum / afhi->chunks_total;
afhi->channels = header_channels(&header);
afhi->seconds_total = (tv2ms(&total_time) + 500) / 1000;
tv_divide(afhi->chunks_total, &total_time, &afhi->chunk_tv);
- PARA_DEBUG_LOG("%lu chunks, each %lums\n", afhi->chunks_total,
+ PARA_DEBUG_LOG("%" PRIu32 " chunks, each %lums\n", afhi->chunks_total,
tv2ms(&afhi->chunk_tv));
- tv_scale(3, &afhi->chunk_tv, &afhi->eof_tv);
- PARA_DEBUG_LOG("eof timeout: %lu\n", tv2ms(&afhi->eof_tv));
- afhi->info_string = make_message("%s: %cbr, %s\n%s",
- status_item_list[SI_AUDIO_FILE_INFO], vbr? 'v' : 'c',
- header_mode(&header), taginfo);
- free(taginfo);
+ set_max_chunk_size(afhi);
+ ret = mp3_get_id3(map, numbytes, fd, &afhi->tags);
+ afhi->techinfo = make_message("%cbr, %s, %s tags", vbr? 'v' : 'c',
+ header_mode(&header), tag_versions[ret]);
return 1;
err_out:
- free(taginfo);
- PARA_ERROR_LOG("%s\n", para_strerror(-ret));
free(afhi->chunk_table);
return ret;
}
ret = mp3_read_info((unsigned char *)map, numbytes, fd, afhi);
if (ret < 0)
return ret;
- if (afhi->seconds_total < 2 || !afhi->chunks_total)
+ if (afhi->chunks_total == 0)
return -E_MP3_INFO;
return 1;
}
-static const char* mp3_suffixes[] = {"mp3", NULL};
+static const char * const mp3_suffixes[] = {"mp3", NULL};
/**
- * the init function of the mp3 audio format handler
+ * The mp3 audio format handler.
*
- * \param afh pointer to the struct to initialize
+ * It does not depend on any libraries and is hence always compiled in.
*/
-void mp3_init(struct audio_format_handler *afh)
-{
- afh->get_file_info = mp3_get_file_info;
- afh->suffixes = mp3_suffixes;
-}
+const struct audio_format_handler mp3_afh = {
+ .get_file_info = mp3_get_file_info,
+ .suffixes = mp3_suffixes,
+#ifdef HAVE_LIBID3TAG
+ .rewrite_tags = mp3_rewrite_tags,
+#endif /* HAVE_LIBID3TAG */
+};