]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - aac_afh.c
Rename mood_switch(), mood_close(), playlist_{open/close}.
[paraslash.git] / aac_afh.c
index 92b70915621ce3fd4472d9595adb5b9fe5485636..c4301a2f178257b19d56b3cf297da3e1eaff337c 100644 (file)
--- a/aac_afh.c
+++ b/aac_afh.c
-/*
- * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
- *
- *     This program is free software; you can redistribute it and/or modify
- *     it under the terms of the GNU General Public License as published by
- *     the Free Software Foundation; either version 2 of the License, or
- *     (at your option) any later version.
- *
- *     This program is distributed in the hope that it will be useful,
- *     but WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *     GNU General Public License for more details.
- *
- *     You should have received a copy of the GNU General Public License
- *     along with this program; if not, write to the Free Software
- *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
- */
+/* 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 "server.cmdline.h"
-#include "server.h"
-#include "afs.h"
+#include "para.h"
+#include "mp4.h"
 #include "error.h"
+#include "portable_io.h"
+#include "afh.h"
 #include "string.h"
-#include "aac.h"
+#include "fd.h"
 
-/* must be big enough to hold header */
-#define DEFAULT_INBUF_SIZE 65536
 
-static FILE *infile;
-static int inbuf_size;
-static unsigned char *inbuf;
-static unsigned inbuf_len;
-struct audio_format *af;
+struct aac_afh_context {
+       const void *map;
+       size_t mapsize;
+       size_t fpos;
+       struct mp4 *mp4;
+       struct mp4_callback cb;
+};
 
-static unsigned num_chunks, entry;
-
-static size_t *chunk_table;
-NeAACDecHandle handle;
+static ssize_t aac_afh_read_cb(void *user_data, void *dest, size_t want)
+{
+       struct aac_afh_context *c = user_data;
+       size_t have, rv;
 
+       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;
+}
 
-static void aac_close_audio_file(void)
+static off_t aac_afh_seek_cb(void *user_data, off_t offset, int whence)
 {
+       struct aac_afh_context *c = user_data;
+
+       if (whence == SEEK_SET)
+               c->fpos = offset;
+       else if (whence == SEEK_CUR)
+               c->fpos += offset;
+       else if (whence == SEEK_END)
+               c->fpos = c->mapsize + offset;
+       else
+               assert(false);
+       return c->fpos;
 }
 
-static int aac_find_stsz(unsigned char *buf, unsigned buflen, unsigned *skip)
+static int aac_afh_open(const void *map, size_t mapsize, void **afh_context)
 {
-       int i;
+       int ret;
+       struct aac_afh_context *c = alloc(sizeof(*c));
 
-       for (i = 0; i + 16 < buflen; i++) {
-               unsigned char *p = buf + i;
-               unsigned sample_count, sample_size;
+       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;
 
-               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;
-       }
-       PARA_WARNING_LOG("stsz not found, buflen: %d\n", buflen);
-       return -E_STSZ;
+       ret = mp4_open(&c->cb, &c->mp4);
+       if (ret < 0)
+               goto free_ctx;
+       *afh_context = c;
+       return 0;
+free_ctx:
+       free(c);
+       *afh_context = NULL;
+       return ret;
 }
 
-static int read_chunk_table(unsigned skip)
+static void aac_afh_close(void *afh_context)
 {
-       int ret, i;
-       long unsigned sum = 0;
-
-       for (;;) {
-               ret = aac_find_stsz(inbuf, inbuf_len, &skip);
-               if (ret >= 0)
-                       break;
-               ret = read(fileno(infile), inbuf, inbuf_size);
-               if (ret <= 0)
-                       return -E_AAC_READ;
-               PARA_INFO_LOG("next buffer: %d bytes\n", ret);
-       }
-       num_chunks = ret;
-       PARA_INFO_LOG("sz table has %d entries\n", num_chunks);
-       free(chunk_table);
-       chunk_table = para_malloc(num_chunks * sizeof(size_t));
-       for (i = 0; i < num_chunks; i++) {
-               if (skip + 4 > inbuf_len) {
-                       skip = inbuf_len - skip;
-                       memmove(inbuf, inbuf + inbuf_len - skip, skip);
-                       ret = read(fileno(infile), inbuf + skip, inbuf_size - skip);
-                       if (ret <= 0)
-                               return -E_AAC_READ;
-                       inbuf_len = ret + skip;
-                       skip = 0;
-                       PARA_INFO_LOG("next buffer: %d bytes\n", inbuf_len);
-               }
-               sum += aac_read_int32(inbuf + skip);
-               chunk_table[i] = sum;
-               skip += 4;
-               if (i < 10 || i > num_chunks - 10)
-                       PARA_DEBUG_LOG("offset #%d: %d\n", i, chunk_table[i]);
-       }
+       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;
+       uint32_t ss;
+       size_t offset;
+       int ret;
+
+       ret = mp4_set_sample_position(c->mp4, chunk_num);
+       if (ret < 0)
+               return ret;
+       offset = c->fpos;
+       ret = mp4_get_sample_size(c->mp4, chunk_num, &ss);
+       if (ret < 0)
+               return ret;
+       if (ss + offset > c->mapsize) /* file got truncated?! */
+               return -E_MP4_CORRUPT;
+       *buf = c->map + offset;
+       *len = ss;
        return 1;
+}
 
+static void aac_afh_get_taginfo(const struct mp4 *mp4, struct taginfo *tags)
+{
+       tags->artist = mp4_get_tag_value(mp4, "artist");
+       tags->title = mp4_get_tag_value(mp4, "title");
+       tags->year = mp4_get_tag_value(mp4, "date");
+       tags->album = mp4_get_tag_value(mp4, "album");
+       tags->comment = mp4_get_tag_value(mp4, "comment");
 }
 
 /*
  * Init m4a file and write some tech data to given pointers.
  */
-static int aac_get_file_info(FILE *file, char *info_str, long unsigned *frames,
-       int *seconds)
+static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd,
+               struct afh_info *afhi)
 {
-       int ret, skip, decoder_len;
-       unsigned long rate = 0;
-       unsigned char channels = 0;
-       mp4AudioSpecificConfig mp4ASC;
-
-       free(inbuf);
-       inbuf_size = DEFAULT_INBUF_SIZE;
-       inbuf = para_malloc(inbuf_size);
-       infile = file;
-
-       PARA_INFO_LOG("file: %p\n", file);
-       ret = read(fileno(infile), inbuf, inbuf_size);
-       PARA_INFO_LOG("read %d bytes\n", ret);
-       if (ret <= 0)
-               return -E_AAC_READ;
-       PARA_INFO_LOG("checking aac %d bytes\n", ret);
-       inbuf_len = ret;
-       ret = aac_find_esds(inbuf, inbuf_len, &skip);
-       if (ret < 0)
-               return ret;
-       decoder_len = ret;
-       handle = aac_open();
-       ret = NeAACDecInit(handle, inbuf + skip,
-               decoder_len, &rate, &channels);
-       if (ret < 0)
-               return -E_AACDEC_INIT;
-       skip += ret;
-       PARA_INFO_LOG("rate: %lu, channels: %d\n", rate, channels);
-       ret = NeAACDecAudioSpecificConfig(inbuf + skip, inbuf_len - skip, &mp4ASC);
-       if (ret >= 0) {
-               PARA_DEBUG_LOG("mp4ASC.samplingFrequency: %lu\n",
-                       mp4ASC.samplingFrequency);
-       } else
-               PARA_WARNING_LOG("no mp4ASC %s\n", "");
-
-       ret = read_chunk_table(skip);
+       int ret;
+       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)
                return ret;
-       *frames = num_chunks;
-       for (;;) {
-               ret = aac_find_entry_point(inbuf, inbuf_len, &skip);
-               if (ret >= 0)
-                       break;
-               ret = read(fileno(infile), inbuf, inbuf_size);
-               if (ret <= 0)
-                       return -E_AAC_READ;
-               PARA_INFO_LOG("next buffer: %d bytes\n", ret);
-       }
-       entry = ret;
-       PARA_INFO_LOG("offset table has %d entries\, entry: %zd\n", num_chunks,
-               entry);
-#if 1
-       sprintf(info_str, "audio_file_info1:%d x %lums\n"
-               "audio_file_info2:\n"
-               "audio_file_info3:\n",
-               num_chunks,
-               tv2ms(&af->chunk_tv));
-#endif
-#if 1
-       {
-       struct timeval total_tv;
-       tv_scale(num_chunks, &af->chunk_tv, &total_tv);
-       *seconds = tv2ms(&total_tv) / 1000;
-       PARA_INFO_LOG("%d seconds, %d chunks\n", *seconds, num_chunks);
+
+       afhi->frequency = mp4_get_sample_rate(c->mp4);
+       assert(afhi->frequency > 0);
+       afhi->channels = mp4_get_channel_count(c->mp4);
+       assert(afhi->channels > 0);
+       afhi->chunks_total = mp4_num_samples(c->mp4);
+       assert(afhi->chunks_total > 0);
+
+       afhi->max_chunk_size = 0;
+       for (n = 0; n < afhi->chunks_total; n++) {
+               ret = aac_afh_get_chunk(n, c, &buf, &len);
+               if (ret < 0)
+                       goto out;
+               afhi->max_chunk_size = PARA_MAX(afhi->max_chunk_size, len);
        }
-#endif
-       return 1;
+       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)
+               goto out;
+       numbytes -= buf - map;
+       afhi->bitrate = 8 * numbytes / afhi->seconds_total / 1000;
+       aac_afh_get_taginfo(c->mp4, &afhi->tags);
+       ret = 1;
+out:
+       aac_afh_close(c);
+       return ret;
 }
 
-/*
- * Simple stream reposition routine
- */
-static int aac_reposition_stream(long unsigned request)
+static ssize_t aac_afh_meta_read_cb(void *user_data, void *dest, size_t want)
 {
-       return 1;
-//     return -E_AAC_REPOS;
+       int fd = *(int *)user_data;
+       return read(fd, dest, want);
 }
 
-static __must_check int para_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
+static off_t aac_afh_meta_seek_cb(void *user_data, off_t offset, int whence)
 {
-       size_t res = fread(ptr, size, nmemb, stream);
-       if (res == nmemb)
-               return size * nmemb;
-       if (feof(stream))
-               return 0;
-       return -E_FREAD;
+       int fd = *(int *)user_data;
+       off_t ret = lseek(fd, offset, whence);
+
+       assert(ret != (off_t)-1);
+       return ret;
 }
 
-static char *aac_read_chunk(long unsigned current_chunk, ssize_t *len)
+static ssize_t aac_afh_meta_write_cb(void *user_data, void *dest, size_t count)
 {
-       int ret;
-       size_t pos;
-
-       *len = 0;
-       if (current_chunk >= num_chunks)
-               return NULL;
-       if (!current_chunk) {
-               *len = chunk_table[0];
-               pos = entry;
-       } else {
-               *len = chunk_table[current_chunk] - chunk_table[current_chunk - 1];
-               pos = entry + chunk_table[current_chunk - 1];
-       }
-       if (inbuf_size < *len) {
-               inbuf = para_realloc(inbuf, *len);
-               inbuf_size = *len;
+       int fd = *(int *)user_data;
+       return write(fd, dest, count);
+}
+
+static int 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;
        }
-//     PARA_DEBUG_LOG("reading chunk #%lu@%zd (%zd bytes)\n", current_chunk,
-//             pos, *len);
-       ret = fseek(infile, pos, SEEK_SET);
-       if (ret < 0)
-               return NULL;
-       ret = para_fread(inbuf, *len, 1, infile);
-       if (ret != *len)
-               return NULL;
-//     PARA_DEBUG_LOG("ret: %d, inbuf[0]: %lx - %lx\n", ret, (long unsigned) inbuf[0],
-//             (long unsigned) inbuf[4]);
-       return (char *)inbuf;
+       /* 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++;
 }
 
-void aac_afh_init(void *p)
+static int aac_afh_rewrite_tags(const char *map, size_t mapsize,
+               struct taginfo *tags, int fd, __a_unused const char *filename)
 {
-       af = p;
-       af->reposition_stream = aac_reposition_stream;
-       af->get_file_info = aac_get_file_info,
-       af->read_chunk = aac_read_chunk;
-       af->close_audio_file = aac_close_audio_file;
-       af->get_header_info = NULL;
-       af->chunk_tv.tv_sec = 0;
-       af->chunk_tv.tv_usec = 23120;
-       tv_scale(3, &af->chunk_tv, &af->eof_tv);
+       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)
+               return ret;
+       lseek(fd, 0, SEEK_SET);
+
+       ret = mp4_open_meta(&cb, &mp4);
+       if (ret < 0)
+               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_update_meta(mp4);
+       mp4_close(mp4);
+       return ret;
 }
+
+static const char * const aac_suffixes[] = {"m4a", "mp4", NULL};
+
+/**
+ * The audio format handler for the Advanced Audio Codec.
+ *
+ * This is only compiled in if the faad library is installed.
+ */
+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,
+};