Doxyfile: Predefine HAVE_CLOCK_GETTIME.
[paraslash.git] / opus_afh.c
1 /*
2  * Copyright (C) 2012-2013 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file opus_afh.c Audio format handler for ogg/opus files. */
8
9 #include <ogg/ogg.h>
10 #include <regex.h>
11
12 #include "para.h"
13 #include "afh.h"
14 #include "error.h"
15 #include "portable_io.h"
16 #include "string.h"
17 #include "opus_common.h"
18 #include "ogg_afh_common.h"
19
20 static const char* opus_suffixes[] = {"opus", NULL};
21
22 static bool copy_if_tag_type(const char *tag, int taglen, const char *type,
23                 char **p)
24 {
25         char *q = key_value_copy(tag, taglen, type);
26         if (!q)
27                 return false;
28         free(*p);
29         *p = q;
30         return true;
31 }
32
33 static int opus_get_comments(char *comments, int length,
34                 struct taginfo *tags)
35 {
36         char *p = comments, *end = comments + length;
37         int i;
38         uint32_t val, ntags;
39
40         /* min size of a opus header is 16 bytes */
41         if (length < 16)
42                 return -E_OPUS_COMMENT;
43         if (memcmp(p, "OpusTags", 8) != 0)
44                 return -E_OPUS_COMMENT;
45         p += 8;
46         val = read_u32(p);
47         p += 4;
48         if (p + val > end)
49                 return -E_OPUS_COMMENT;
50         tags->comment = safe_strdup(p, val);
51         p += val;
52         ntags = read_u32(p);
53         p += 4;
54         if (p + ntags * 4 > end)
55                 return -E_OPUS_COMMENT;
56         PARA_INFO_LOG("found %d tag(s)\n", ntags);
57         for (i = 0; i < ntags; i++, p += val) {
58                 char *tag;
59
60                 if (p + 4 > end)
61                         return -E_OPUS_COMMENT;
62                 val = read_u32(p);
63                 p += 4;
64                 if (p + val > end)
65                         return -E_OPUS_COMMENT;
66                 if (copy_if_tag_type(p, val, "author", &tags->artist))
67                         continue;
68                 if (copy_if_tag_type(p, val, "artist", &tags->artist))
69                         continue;
70                 if (copy_if_tag_type(p, val, "title", &tags->title))
71                         continue;
72                 if (copy_if_tag_type(p, val, "album", &tags->album))
73                         continue;
74                 if (copy_if_tag_type(p, val, "year", &tags->year))
75                         continue;
76                 if (copy_if_tag_type(p, val, "comment", &tags->comment))
77                         continue;
78                 tag = safe_strdup(p, val);
79                 PARA_NOTICE_LOG("unrecognized tag: %s\n", tag);
80                 free(tag);
81         }
82         return 1;
83 }
84
85 static int opus_packet_callback(ogg_packet *packet, int packet_num,
86                 __a_unused int serial, struct afh_info *afhi,
87                 void *private_data)
88 {
89         int ret;
90         struct opus_header *oh = private_data;
91
92         if (packet_num == 0) {
93                 ret = opus_parse_header((char *)packet->packet, packet->bytes, oh);
94                 if (ret < 0)
95                         return ret;
96                 afhi->channels = oh->channels;
97                 afhi->techinfo = make_message("header version %d, input sample rate: %dHz",
98                         oh->version, oh->input_sample_rate);
99                 /*
100                  * The input sample rate is irrelevant for afhi->frequency as
101                  * we always decode to 48kHz.
102                  */
103                 afhi->frequency = 48000;
104                 return 1;
105         }
106         if (packet_num == 1) {
107                 ret = opus_get_comments((char *)packet->packet, packet->bytes,
108                         &afhi->tags);
109                 if (ret < 0)
110                         return ret;
111                 return 0; /* header complete */
112         }
113         /* never reached */
114         assert(0);
115 }
116
117 static int opus_get_file_info(char *map, size_t numbytes, __a_unused int fd,
118                 struct afh_info *afhi)
119 {
120         int ret, ms;
121         struct opus_header oh = {.version = 0};
122
123         struct ogg_afh_callback_info opus_callback_info = {
124                 .packet_callback = opus_packet_callback,
125                 .private_data = &oh,
126         };
127         ret = ogg_get_file_info(map, numbytes, afhi, &opus_callback_info);
128         if (ret < 0)
129                 return ret;
130         ret = (afhi->chunk_table[afhi->chunks_total] - afhi->chunk_table[0]) * 8; /* bits */
131         ms = tv2ms(&afhi->chunk_tv) * afhi->chunks_total;
132         afhi->bitrate = ret / ms;
133         return 1;
134 }
135
136 /**
137  * The init function of the ogg/opus audio format handler.
138  *
139  * \param afh Pointer to the struct to initialize.
140  */
141 void opus_afh_init(struct audio_format_handler *afh)
142 {
143         afh->get_file_info = opus_get_file_info,
144         afh->suffixes = opus_suffixes;
145 }