+/*
+ * Copyright (C) 2012-2013 Andre Noll <maan@systemlinux.org>
+ *
+ * Licensed under the GPL v2. For licencing details see COPYING.
+ */
+
+/** \file opus_afh.c Audio format handler for ogg/opus files. */
+
#include <ogg/ogg.h>
#include <regex.h>
#include "string.h"
#include "opus_common.h"
#include "ogg_afh_common.h"
+
+static const char* opus_suffixes[] = {"opus", NULL};
+
+static bool copy_if_tag_type(const char *tag, int taglen, const char *type,
+ char **p)
+{
+ char *q = key_value_copy(tag, taglen, type);
+ if (!q)
+ return false;
+ free(*p);
+ *p = q;
+ return true;
+}
+
+static int opus_get_comments(char *comments, int length,
+ struct taginfo *tags)
+{
+ char *p = comments, *end = comments + length;
+ int i;
+ uint32_t val, ntags;
+
+ /* min size of a opus header is 16 bytes */
+ if (length < 16)
+ return -E_OPUS_COMMENT;
+ if (memcmp(p, "OpusTags", 8) != 0)
+ return -E_OPUS_COMMENT;
+ p += 8;
+ val = read_u32(p);
+ p += 4;
+ if (p + val > end)
+ return -E_OPUS_COMMENT;
+ tags->comment = safe_strdup(p, val);
+ p += val;
+ ntags = read_u32(p);
+ p += 4;
+ if (p + ntags * 4 > end)
+ return -E_OPUS_COMMENT;
+ PARA_INFO_LOG("found %d tag(s)\n", ntags);
+ for (i = 0; i < ntags; i++, p += val) {
+ char *tag;
+
+ if (p + 4 > end)
+ return -E_OPUS_COMMENT;
+ val = read_u32(p);
+ p += 4;
+ if (p + val > end)
+ return -E_OPUS_COMMENT;
+ if (copy_if_tag_type(p, val, "author", &tags->artist))
+ continue;
+ if (copy_if_tag_type(p, val, "artist", &tags->artist))
+ continue;
+ if (copy_if_tag_type(p, val, "title", &tags->title))
+ continue;
+ if (copy_if_tag_type(p, val, "album", &tags->album))
+ continue;
+ if (copy_if_tag_type(p, val, "year", &tags->year))
+ continue;
+ if (copy_if_tag_type(p, val, "comment", &tags->comment))
+ continue;
+ tag = safe_strdup(p, val);
+ PARA_NOTICE_LOG("unrecognized tag: %s\n", tag);
+ free(tag);
+ }
+ return 1;
+}
+
+static int opus_packet_callback(ogg_packet *packet, int packet_num,
+ __a_unused int serial, struct afh_info *afhi,
+ void *private_data)
+{
+ int ret;
+ struct opus_header *oh = private_data;
+
+ if (packet_num == 0) {
+ ret = opus_parse_header((char *)packet->packet, packet->bytes, oh);
+ if (ret < 0)
+ return ret;
+ afhi->channels = oh->channels;
+ afhi->techinfo = make_message("header version %d, input sample rate: %dHz",
+ oh->version, oh->input_sample_rate);
+ /*
+ * The input sample rate is irrelevant for afhi->frequency as
+ * we always decode to 48kHz.
+ */
+ afhi->frequency = 48000;
+ return 1;
+ }
+ if (packet_num == 1) {
+ ret = opus_get_comments((char *)packet->packet, packet->bytes,
+ &afhi->tags);
+ if (ret < 0)
+ return ret;
+ return 0; /* header complete */
+ }
+ /* never reached */
+ assert(0);
+}
+
+static int opus_get_file_info(char *map, size_t numbytes, __a_unused int fd,
+ struct afh_info *afhi)
+{
+ int ret, ms;
+ struct opus_header oh = {.version = 0};
+
+ struct ogg_afh_callback_info opus_callback_info = {
+ .packet_callback = opus_packet_callback,
+ .private_data = &oh,
+ };
+ ret = ogg_get_file_info(map, numbytes, afhi, &opus_callback_info);
+ if (ret < 0)
+ return ret;
+ ret = (afhi->chunk_table[afhi->chunks_total] - afhi->chunk_table[0]) * 8; /* bits */
+ ms = tv2ms(&afhi->chunk_tv) * afhi->chunks_total;
+ afhi->bitrate = ret / ms;
+ return 1;
+}
+
/**
* The init function of the ogg/opus audio format handler.
*
*/
void opus_afh_init(struct audio_format_handler *afh)
{
-
+ afh->get_file_info = opus_get_file_info,
+ afh->suffixes = opus_suffixes;
}