-/*
- * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
/*
* based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
* Ahead Software AG
*/
-/** \file aac_afh.c para_server's aac audio format handler */
+/** \file aac_afh.c para_server's aac audio format handler. */
+
+#include <regex.h>
+#include <neaacdec.h>
#include "para.h"
-#include "afh.h"
-#include "server.h"
+#include "mp4.h"
#include "error.h"
+#include "portable_io.h"
+#include "afh.h"
#include "string.h"
-#include "aac.h"
+#include "fd.h"
+
-static int aac_find_stsz(unsigned char *buf, size_t buflen, off_t *skip)
+struct aac_afh_context {
+ const void *map;
+ size_t mapsize;
+ size_t fpos;
+ struct mp4 *mp4;
+ struct mp4_callback cb;
+};
+
+static ssize_t aac_afh_read_cb(void *user_data, void *dest, size_t want)
{
- int i;
+ struct aac_afh_context *c = user_data;
+ size_t have, rv;
- for (i = 0; i + 16 < buflen; i++) {
- unsigned char *p = buf + i;
- unsigned sample_count, sample_size;
+ if (want == 0 || c->fpos >= c->mapsize)
+ return 0;
+ have = c->mapsize - c->fpos;
+ rv = PARA_MIN(have, want);
+ PARA_DEBUG_LOG("reading %zu bytes @%zu\n", rv, c->fpos);
+ memcpy(dest, c->map + c->fpos, rv);
+ c->fpos += rv;
+ return rv;
+}
- if (p[0] != 's' || p[1] != 't' || p[2] != 's' || p[3] != 'z')
- continue;
- PARA_INFO_LOG("found stsz@%d\n", i);
- i += 8;
- sample_size = aac_read_int32(buf + i);
- PARA_INFO_LOG("sample size: %d\n", sample_size);
- i += 4;
- sample_count = aac_read_int32(buf + i);
- i += 4;
- PARA_INFO_LOG("sample count: %d\n", sample_count);
- *skip = i;
- return sample_count;
- }
- return -E_STSZ;
+static uint32_t aac_afh_seek_cb(void *user_data, uint64_t pos)
+{
+ struct aac_afh_context *c = user_data;
+ c->fpos = pos;
+ return 0;
}
-static ssize_t aac_compute_chunk_table(struct audio_format_info *afi,
- unsigned char *map, size_t numbytes)
+static int aac_afh_open(const void *map, size_t mapsize, void **afh_context)
{
- int ret, i;
- size_t sum = 0;
- off_t skip;
+ int ret;
+ struct aac_afh_context *c = para_malloc(sizeof(*c));
+
+ c->map = map;
+ c->mapsize = mapsize;
+ c->fpos = 0;
+ c->cb.read = aac_afh_read_cb;
+ c->cb.seek = aac_afh_seek_cb;
+ c->cb.user_data = c;
- ret = aac_find_stsz(map, numbytes, &skip);
+ ret = mp4_open_read(&c->cb, &c->mp4);
if (ret < 0)
- return ret;
- afi->chunks_total = ret;
- PARA_INFO_LOG("sz table has %lu entries\n", afi->chunks_total);
- afi->chunk_table = para_malloc((afi->chunks_total + 1) * sizeof(size_t));
- for (i = 1; i <= afi->chunks_total; i++) {
- if (skip + 4 > numbytes)
- break;
- sum += aac_read_int32(map + skip);
- afi->chunk_table[i] = sum;
- skip += 4;
-// if (i < 10 || i + 10 > afi->chunks_total)
-// PARA_DEBUG_LOG("offset #%d: %zu\n", i, afi->chunk_table[i]);
- }
- return skip;
+ goto free_ctx;
+ *afh_context = c;
+ return 0;
+free_ctx:
+ free(c);
+ *afh_context = NULL;
+ return ret;
}
-static int aac_set_chunk_tv(struct audio_format_info *afi,
- mp4AudioSpecificConfig *mp4ASC, long unsigned *seconds)
+static void aac_afh_close(void *afh_context)
{
- float tmp = mp4ASC->sbr_present_flag == 1? 2047 : 1023;
- struct timeval total;
- long unsigned ms = 1000.0 * afi->chunks_total * tmp
- / mp4ASC->samplingFrequency;
-
- if (!mp4ASC->samplingFrequency)
- return -E_MP4ASC;
- ms = 1000.0 * afi->chunks_total * tmp / mp4ASC->samplingFrequency;
- ms2tv(ms, &total);
- tv_divide(afi->chunks_total, &total, &afi->chunk_tv);
- PARA_INFO_LOG("%luHz, %lus (%lu x %lums)\n",
- mp4ASC->samplingFrequency, ms / 1000,
- afi->chunks_total, tv2ms(&afi->chunk_tv));
- if (ms < 1000)
- return -E_MP4ASC;
- *seconds = ms / 1000;
+ struct aac_afh_context *c = afh_context;
+ mp4_close(c->mp4);
+ free(c);
+}
+
+static int aac_afh_get_chunk(uint32_t chunk_num, void *afh_context,
+ const char **buf, uint32_t *len)
+{
+ struct aac_afh_context *c = afh_context;
+ int32_t ss;
+ size_t offset;
+ int ret;
+
+ ret = mp4_set_sample_position(c->mp4, chunk_num);
+ if (ret < 0)
+ return ret;
+ offset = c->fpos;
+ ss = mp4_get_sample_size(c->mp4, chunk_num);
+ if (ss <= 0)
+ return -E_MP4_BAD_SAMPLE;
+ assert(ss + offset <= c->mapsize);
+ *buf = c->map + offset;
+ *len = ss;
return 1;
}
+static void _aac_afh_get_taginfo(const struct mp4 *mp4, struct taginfo *tags)
+{
+ tags->artist = mp4_meta_get_artist(mp4);
+ tags->title = mp4_meta_get_title(mp4);
+ tags->year = mp4_meta_get_date(mp4);
+ tags->album = mp4_meta_get_album(mp4);
+ tags->comment = mp4_meta_get_comment(mp4);
+}
+
/*
* Init m4a file and write some tech data to given pointers.
*/
-static int aac_get_file_info(char *map, size_t numbytes,
- struct audio_format_info *afi)
+static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd,
+ struct afh_info *afhi)
{
- int i;
- size_t skip;
- ssize_t ret;
- unsigned long rate = 0, decoder_len;
- unsigned char channels = 0;
- mp4AudioSpecificConfig mp4ASC;
- NeAACDecHandle handle;
- unsigned char *umap = (unsigned char *) map;
-
- ret = aac_find_esds(umap, numbytes, &skip, &decoder_len);
- if (ret < 0)
- goto out;
- handle = aac_open();
- ret = -E_AAC_AFH_INIT;
- if (NeAACDecInit(handle, umap + skip, decoder_len, &rate, &channels))
- goto out;
- if (!channels)
- goto out;
- PARA_INFO_LOG("rate: %lu, channels: %d\n", rate, channels);
- ret = -E_MP4ASC;
- if (NeAACDecAudioSpecificConfig(umap + skip, numbytes - skip, &mp4ASC))
- goto out;
- if (!mp4ASC.samplingFrequency)
- goto out;
- ret = aac_compute_chunk_table(afi, umap, numbytes);
+ int ret;
+ int32_t rv;
+ struct aac_afh_context *c;
+ uint64_t milliseconds;
+ const char *buf;
+ uint32_t n, len;
+
+ ret = aac_afh_open(map, numbytes, (void **)&c);
if (ret < 0)
- goto out;
- skip = ret;
- ret = aac_set_chunk_tv(afi, &mp4ASC, &afi->seconds_total);
+ return ret;
+
+ ret = -E_MP4_BAD_SAMPLERATE;
+ rv = mp4_get_sample_rate(c->mp4);
+ if (rv <= 0)
+ goto close;
+ afhi->frequency = rv;
+
+ ret = -E_MP4_BAD_CHANNEL_COUNT;
+ rv = mp4_get_channel_count(c->mp4);
+ if (rv <= 0)
+ goto close;
+ afhi->channels = rv;
+
+ ret = -E_MP4_BAD_SAMPLE_COUNT;
+ rv = mp4_num_samples(c->mp4);
+ if (rv <= 0)
+ goto close;
+ afhi->chunks_total = rv;
+ afhi->max_chunk_size = 0;
+ for (n = 0; n < afhi->chunks_total; n++) {
+ if (aac_afh_get_chunk(n, c, &buf, &len) < 0)
+ break;
+ afhi->max_chunk_size = PARA_MAX(afhi->max_chunk_size, len);
+ }
+ milliseconds = mp4_get_duration(c->mp4);
+ afhi->seconds_total = milliseconds / 1000;
+ ms2tv(milliseconds / afhi->chunks_total, &afhi->chunk_tv);
+ if (aac_afh_get_chunk(0, c, &buf, &len) >= 0)
+ numbytes -= buf - map;
+ afhi->bitrate = 8 * numbytes / afhi->seconds_total / 1000;
+ _aac_afh_get_taginfo(c->mp4, &afhi->tags);
+ ret = 1;
+close:
+ aac_afh_close(c);
+ return ret;
+}
+
+static ssize_t aac_afh_meta_read_cb(void *user_data, void *dest, size_t want)
+{
+ int fd = *(int *)user_data;
+ return read(fd, dest, want);
+}
+
+static uint32_t aac_afh_meta_seek_cb(void *user_data, uint64_t pos)
+{
+ int fd = *(int *)user_data;
+ return lseek(fd, pos, SEEK_SET);
+}
+
+static ssize_t aac_afh_meta_write_cb(void *user_data, void *dest, size_t count)
+{
+ int fd = *(int *)user_data;
+ return write(fd, dest, count);
+}
+
+static uint32_t aac_afh_meta_truncate_cb(void *user_data)
+{
+ int fd = *(int *)user_data;
+ off_t offset = lseek(fd, 0, SEEK_CUR);
+ return ftruncate(fd, offset);
+}
+
+static void replace_or_add_tag(const char *item, const char *value,
+ struct mp4_metadata *meta)
+{
+ uint32_t n;
+ struct mp4_tag *t;
+
+ for (n = 0; n < meta->count; n++) {
+ t = meta->tags + n;
+ if (strcasecmp(t->item, item))
+ continue;
+ free(t->value);
+ t->value = para_strdup(value);
+ return;
+ }
+ /* item not found, add new tag */
+ meta->tags = para_realloc(meta->tags, (meta->count + 1)
+ * sizeof(struct mp4_tag));
+ t = meta->tags + meta->count;
+ t->item = para_strdup(item);
+ t->value = para_strdup(value);
+ meta->count++;
+}
+
+static int aac_afh_rewrite_tags(const char *map, size_t mapsize,
+ struct taginfo *tags, int fd, __a_unused const char *filename)
+{
+ int ret;
+ struct mp4_metadata *metadata;
+ struct mp4 *mp4;
+ struct mp4_callback cb = {
+ .read = aac_afh_meta_read_cb,
+ .seek = aac_afh_meta_seek_cb,
+ .write = aac_afh_meta_write_cb,
+ .truncate = aac_afh_meta_truncate_cb,
+ .user_data = &fd
+ };
+
+ ret = write_all(fd, map, mapsize);
if (ret < 0)
- goto out;
- ret = aac_find_entry_point(umap + skip, numbytes - skip, &skip);
+ return ret;
+ lseek(fd, 0, SEEK_SET);
+
+ ret = mp4_open_meta(&cb, &mp4);
if (ret < 0)
- goto out;
- afi->chunk_table[0] = ret;
- for (i = 1; i<= afi->chunks_total; i++)
- afi->chunk_table[i] += ret;
- afi->channels = channels;
- afi->frequency = rate;
- ret = (afi->chunk_table[afi->chunks_total] - afi->chunk_table[0]) * 8; /* bits */
- ret += (channels * afi->seconds_total * 500); /* avoid rounding error */
- afi->bitrate = ret / (channels * afi->seconds_total * 1000);
- sprintf(afi->info_string, "audio_file_info1:%lu x %lums, "
- "%uHz, %d channel%s, %ukb/s\n"
- "audio_file_info2:\n"
- "audio_file_info3:\n",
- afi->chunks_total, tv2ms(&afi->chunk_tv),
- afi->frequency, channels, channels == 1? "" : "s", afi->bitrate
- );
- tv_scale(20, &afi->chunk_tv, &afi->eof_tv);
- ret = 1;
-out:
+ return ret;
+ metadata = mp4_get_meta(mp4);
+ PARA_NOTICE_LOG("%u metadata item(s) found\n", metadata->count);
+ replace_or_add_tag("artist", tags->artist, metadata);
+ replace_or_add_tag("title", tags->title, metadata);
+ replace_or_add_tag("album", tags->album, metadata);
+ replace_or_add_tag("date", tags->year, metadata);
+ replace_or_add_tag("comment", tags->comment, metadata);
+ ret = mp4_meta_update(mp4);
+ mp4_close(mp4);
return ret;
}
-static const char* aac_suffixes[] = {"m4a", "mp4", NULL};
+static const char * const aac_suffixes[] = {"m4a", "mp4", NULL};
+
/**
- * the init function of the aac audio format handler
+ * The audio format handler for the Advanced Audio Codec.
*
- * \param afh pointer to the struct to initialize
+ * This is only compiled in if the faad library is installed.
*/
-void aac_afh_init(struct audio_format_handler *afh)
-{
- afh->get_file_info = aac_get_file_info,
- afh->suffixes = aac_suffixes;
-}
+const struct audio_format_handler aac_afh = {
+ .get_file_info = aac_get_file_info,
+ .suffixes = aac_suffixes,
+ .rewrite_tags = aac_afh_rewrite_tags,
+ .open = aac_afh_open,
+ .get_chunk = aac_afh_get_chunk,
+ .close = aac_afh_close,
+};