play.c: Check whether filter ->close() is NULL.
[paraslash.git] / opus_afh.c
index 10fe25d8e8dc00a3e085b4d6f2eae832f913b8a2..b1469b474d504e69bfe62ad95f6d1ee9043b65a5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012-2014 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2012 Andre Noll <maan@tuebingen.mpg.de>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
@@ -17,7 +17,9 @@
 #include "opus_common.h"
 #include "ogg_afh_common.h"
 
-static const char* opus_suffixes[] = {"opus", NULL};
+static const char * const opus_suffixes[] = {"opus", NULL};
+
+#define OPUS_COMMENT_HEADER "OpusTags"
 
 static bool copy_if_tag_type(const char *tag, int taglen, const char *type,
                char **p)
@@ -40,7 +42,7 @@ static int opus_get_comments(char *comments, int length,
        /* min size of a opus header is 16 bytes */
        if (length < 16)
                return -E_OPUS_COMMENT;
-       if (memcmp(p, "OpusTags", 8) != 0)
+       if (memcmp(p, OPUS_COMMENT_HEADER, strlen(OPUS_COMMENT_HEADER)) != 0)
                return -E_OPUS_COMMENT;
        p += 8;
        val = read_u32(p);
@@ -94,7 +96,8 @@ static int opus_packet_callback(ogg_packet *packet, int packet_num,
                if (ret < 0)
                        return ret;
                afhi->channels = oh->channels;
-               afhi->techinfo = make_message("header version %d, input sample rate: %dHz",
+               afhi->techinfo = make_message(
+                       "header version %d, input sample rate: %" PRIu32 "Hz",
                        oh->version, oh->input_sample_rate);
                /*
                 * The input sample rate is irrelevant for afhi->frequency as
@@ -133,6 +136,97 @@ static int opus_get_file_info(char *map, size_t numbytes, __a_unused int fd,
        return 1;
 }
 
+static size_t opus_make_meta_packet(struct taginfo *tags, char **result)
+{
+       size_t sz;
+       char *buf, *p;
+       size_t comment_len = strlen(tags->comment),
+               artist_len = strlen(tags->artist),
+               title_len = strlen(tags->title),
+               album_len = strlen(tags->album),
+               year_len = strlen(tags->year);
+       uint32_t comment_sz = comment_len,
+               artist_sz = artist_len + strlen("artist="),
+               title_sz = title_len + strlen("title="),
+               album_sz = album_len + strlen("album="),
+               year_sz = year_len + strlen("year=");
+       uint32_t num_tags;
+
+       sz = strlen(OPUS_COMMENT_HEADER)
+               + 4 /* comment length (always present) */
+               + comment_sz
+               + 4; /* number of tags */
+       num_tags = 0;
+       if (artist_len) {
+               num_tags++;
+               sz += 4 + artist_sz;
+       }
+       if (title_len) {
+               num_tags++;
+               sz += 4 + title_sz;
+       }
+       if (album_len) {
+               num_tags++;
+               sz += 4 + album_sz;
+       }
+       if (year_len) {
+               num_tags++;
+               sz += 4 + year_sz;
+       }
+       PARA_DEBUG_LOG("meta packet size: %zu bytes\n", sz);
+       /* terminating zero byte for the last sprintf() */
+       buf = p = para_malloc(sz + 1);
+       memcpy(p, OPUS_COMMENT_HEADER, strlen(OPUS_COMMENT_HEADER));
+       p += strlen(OPUS_COMMENT_HEADER);
+       write_u32(p, comment_sz);
+       p += 4;
+       strcpy(p, tags->comment);
+       p += comment_sz;
+       write_u32(p, num_tags);
+       p += 4;
+       if (artist_len) {
+               write_u32(p, artist_sz);
+               p += 4;
+               sprintf(p, "artist=%s", tags->artist);
+               p += artist_sz;
+       }
+       if (title_len) {
+               write_u32(p, title_sz);
+               p += 4;
+               sprintf(p, "title=%s", tags->title);
+               p += title_sz;
+       }
+       if (album_len) {
+               write_u32(p, album_sz);
+               p += 4;
+               sprintf(p, "album=%s", tags->album);
+               p += album_sz;
+       }
+       if (year_len) {
+               write_u32(p, year_sz);
+               p += 4;
+               sprintf(p, "year=%s", tags->year);
+               p += year_sz;
+       }
+       assert(p == buf + sz);
+       *result = buf;
+       return sz;
+}
+
+static int opus_rewrite_tags(const char *map, size_t mapsize,
+               struct taginfo *tags, int output_fd,
+               __a_unused const char *filename)
+{
+       char *meta_packet;
+       size_t meta_sz;
+       int ret;
+
+       meta_sz = opus_make_meta_packet(tags, &meta_packet);
+       ret = ogg_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.
  *
@@ -142,4 +236,5 @@ void opus_afh_init(struct audio_format_handler *afh)
 {
        afh->get_file_info = opus_get_file_info,
        afh->suffixes = opus_suffixes;
+       afh->rewrite_tags = opus_rewrite_tags;
 }