X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=opus_afh.c;h=dca6cfbad1ce3ff8cdf6d820b01bf33bf9a02110;hp=b1469b474d504e69bfe62ad95f6d1ee9043b65a5;hb=3685a9093ae12ff9ce02fc58e607eb9b63894443;hpb=3fd8d0567ae654aed7e97e832568d3e1b98e20a2 diff --git a/opus_afh.c b/opus_afh.c index b1469b47..dca6cfba 100644 --- a/opus_afh.c +++ b/opus_afh.c @@ -1,8 +1,4 @@ -/* - * Copyright (C) 2012 Andre Noll - * - * Licensed under the GPL v2. For licencing details see COPYING. - */ +/* Copyright (C) 2012 Andre Noll , see file COPYING. */ /** \file opus_afh.c Audio format handler for ogg/opus files. */ @@ -55,7 +51,7 @@ static int opus_get_comments(char *comments, int length, p += 4; if (p + ntags * 4 > end) return -E_OPUS_COMMENT; - PARA_INFO_LOG("found %d tag(s)\n", ntags); + PARA_INFO_LOG("found %u tag(s)\n", ntags); for (i = 0; i < ntags; i++, p += val) { char *tag; @@ -84,6 +80,14 @@ static int opus_get_comments(char *comments, int length, return 1; } +/* + * Ogg/Opus has two mandatory header packets: + * + * 1. ID header (identifies the stream as Opus). Dedicated "BOS" ogg page. + * 2. Comment header (metadata). May span multiple pages. + * + * See doc/draft-ietf-codec-oggopus.xml in the opus source tree for details. + */ static int opus_packet_callback(ogg_packet *packet, int packet_num, __a_unused int serial, struct afh_info *afhi, void *private_data) @@ -123,11 +127,11 @@ static int opus_get_file_info(char *map, size_t numbytes, __a_unused int fd, int ret, ms; struct opus_header oh = {.version = 0}; - struct ogg_afh_callback_info opus_callback_info = { + struct oac_callback_info opus_callback_info = { .packet_callback = opus_packet_callback, .private_data = &oh, }; - ret = ogg_get_file_info(map, numbytes, afhi, &opus_callback_info); + ret = oac_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 */ @@ -222,19 +226,80 @@ static int opus_rewrite_tags(const char *map, size_t mapsize, int ret; meta_sz = opus_make_meta_packet(tags, &meta_packet); - ret = ogg_rewrite_tags(map, mapsize, output_fd, meta_packet, meta_sz); + ret = oac_rewrite_tags(map, mapsize, output_fd, meta_packet, meta_sz); free(meta_packet); return ret; } -/** - * The init function of the ogg/opus audio format handler. - * - * \param afh Pointer to the struct to initialize. +/* + * See doc/draft-ietf-codec-oggopus.xml in the opus source tree for details + * about the format of the comment header. */ -void opus_afh_init(struct audio_format_handler *afh) +static int opus_get_header_callback(ogg_packet *packet, int packet_num, + int serial, __a_unused struct afh_info *afhi, void *private_data) +{ + struct oac_custom_header *h = private_data; + int ret; + static unsigned char dummy_tags[] = { /* a minimal comment header */ + 'O', 'p', 'u', 's', 'T', 'a', 'g', 's', + 0x06, 0x00, 0x00, 0x00, /* vendor string length */ + 'd', 'u', 'm', 'm', 'y', '\0', /* vendor string */ + 0x00, 0x00, 0x00, 0x00, /* user comment list length */ + }; + ogg_packet replacement; + + if (packet_num == 0) { + oac_custom_header_init(serial, h); + ret = oac_custom_header_append(packet, h); + if (ret < 0) + return ret; + oac_custom_header_flush(h); + return 1; + } + assert(packet_num == 1); + PARA_INFO_LOG("replacing metadata packet\n"); + replacement = *packet; + replacement.packet = dummy_tags; + replacement.bytes = sizeof(dummy_tags); + ret = oac_custom_header_append(&replacement, h); + if (ret < 0) + return ret; + oac_custom_header_flush(h); + return 0; +} + +static void opus_get_header(void *map, size_t mapsize, char **buf, + size_t *len) { - afh->get_file_info = opus_get_file_info, - afh->suffixes = opus_suffixes; - afh->rewrite_tags = opus_rewrite_tags; + int ret; + struct oac_custom_header *h = oac_custom_header_new(); + struct oac_callback_info cb = { + .packet_callback = opus_get_header_callback, + .private_data = h, + }; + + ret = oac_get_file_info(map, mapsize, NULL, &cb); + *len = oac_custom_header_get(buf, h); + if (ret < 0) { + PARA_ERROR_LOG("could not create custom header: %s\n", + para_strerror(-ret)); + free(*buf); + *buf = NULL; + *len = 0; + } else + PARA_INFO_LOG("created %zu byte ogg/opus header\n", *len); } + +/** + * The audio format handler for ogg/opus. + * + * The opus codec was standardized by the Internet Engineering Task Force and + * is described in RFC 6716 (2012). The audio format handler depends on the ogg + * and the opus libraries. + */ +const struct audio_format_handler opus_afh = { + .get_file_info = opus_get_file_info, + .get_header = opus_get_header, + .suffixes = opus_suffixes, + .rewrite_tags = opus_rewrite_tags, +};