build: Remove arch_cppflags.
[paraslash.git] / opus_afh.c
1 /*
2  * Copyright (C) 2012 Andre Noll <maan@tuebingen.mpg.de>
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 #define OPUS_COMMENT_HEADER "OpusTags"
23
24 static bool copy_if_tag_type(const char *tag, int taglen, const char *type,
25                 char **p)
26 {
27         char *q = key_value_copy(tag, taglen, type);
28         if (!q)
29                 return false;
30         free(*p);
31         *p = q;
32         return true;
33 }
34
35 static int opus_get_comments(char *comments, int length,
36                 struct taginfo *tags)
37 {
38         char *p = comments, *end = comments + length;
39         int i;
40         uint32_t val, ntags;
41
42         /* min size of a opus header is 16 bytes */
43         if (length < 16)
44                 return -E_OPUS_COMMENT;
45         if (memcmp(p, OPUS_COMMENT_HEADER, strlen(OPUS_COMMENT_HEADER)) != 0)
46                 return -E_OPUS_COMMENT;
47         p += 8;
48         val = read_u32(p);
49         p += 4;
50         if (p + val > end)
51                 return -E_OPUS_COMMENT;
52         tags->comment = safe_strdup(p, val);
53         p += val;
54         ntags = read_u32(p);
55         p += 4;
56         if (p + ntags * 4 > end)
57                 return -E_OPUS_COMMENT;
58         PARA_INFO_LOG("found %d tag(s)\n", ntags);
59         for (i = 0; i < ntags; i++, p += val) {
60                 char *tag;
61
62                 if (p + 4 > end)
63                         return -E_OPUS_COMMENT;
64                 val = read_u32(p);
65                 p += 4;
66                 if (p + val > end)
67                         return -E_OPUS_COMMENT;
68                 if (copy_if_tag_type(p, val, "author", &tags->artist))
69                         continue;
70                 if (copy_if_tag_type(p, val, "artist", &tags->artist))
71                         continue;
72                 if (copy_if_tag_type(p, val, "title", &tags->title))
73                         continue;
74                 if (copy_if_tag_type(p, val, "album", &tags->album))
75                         continue;
76                 if (copy_if_tag_type(p, val, "year", &tags->year))
77                         continue;
78                 if (copy_if_tag_type(p, val, "comment", &tags->comment))
79                         continue;
80                 tag = safe_strdup(p, val);
81                 PARA_NOTICE_LOG("unrecognized tag: %s\n", tag);
82                 free(tag);
83         }
84         return 1;
85 }
86
87 static int opus_packet_callback(ogg_packet *packet, int packet_num,
88                 __a_unused int serial, struct afh_info *afhi,
89                 void *private_data)
90 {
91         int ret;
92         struct opus_header *oh = private_data;
93
94         if (packet_num == 0) {
95                 ret = opus_parse_header((char *)packet->packet, packet->bytes, oh);
96                 if (ret < 0)
97                         return ret;
98                 afhi->channels = oh->channels;
99                 afhi->techinfo = make_message("header version %d, input sample rate: %dHz",
100                         oh->version, oh->input_sample_rate);
101                 /*
102                  * The input sample rate is irrelevant for afhi->frequency as
103                  * we always decode to 48kHz.
104                  */
105                 afhi->frequency = 48000;
106                 return 1;
107         }
108         if (packet_num == 1) {
109                 ret = opus_get_comments((char *)packet->packet, packet->bytes,
110                         &afhi->tags);
111                 if (ret < 0)
112                         return ret;
113                 return 0; /* header complete */
114         }
115         /* never reached */
116         assert(0);
117 }
118
119 static int opus_get_file_info(char *map, size_t numbytes, __a_unused int fd,
120                 struct afh_info *afhi)
121 {
122         int ret, ms;
123         struct opus_header oh = {.version = 0};
124
125         struct ogg_afh_callback_info opus_callback_info = {
126                 .packet_callback = opus_packet_callback,
127                 .private_data = &oh,
128         };
129         ret = ogg_get_file_info(map, numbytes, afhi, &opus_callback_info);
130         if (ret < 0)
131                 return ret;
132         ret = (afhi->chunk_table[afhi->chunks_total] - afhi->chunk_table[0]) * 8; /* bits */
133         ms = tv2ms(&afhi->chunk_tv) * afhi->chunks_total;
134         afhi->bitrate = ret / ms;
135         return 1;
136 }
137
138 static size_t opus_make_meta_packet(struct taginfo *tags, char **result)
139 {
140         size_t sz;
141         char *buf, *p;
142         size_t comment_len = strlen(tags->comment),
143                 artist_len = strlen(tags->artist),
144                 title_len = strlen(tags->title),
145                 album_len = strlen(tags->album),
146                 year_len = strlen(tags->year);
147         uint32_t comment_sz = comment_len,
148                 artist_sz = artist_len + strlen("artist="),
149                 title_sz = title_len + strlen("title="),
150                 album_sz = album_len + strlen("album="),
151                 year_sz = year_len + strlen("year=");
152         uint32_t num_tags;
153
154         sz = strlen(OPUS_COMMENT_HEADER)
155                 + 4 /* comment length (always present) */
156                 + comment_sz
157                 + 4; /* number of tags */
158         num_tags = 0;
159         if (artist_len) {
160                 num_tags++;
161                 sz += 4 + artist_sz;
162         }
163         if (title_len) {
164                 num_tags++;
165                 sz += 4 + title_sz;
166         }
167         if (album_len) {
168                 num_tags++;
169                 sz += 4 + album_sz;
170         }
171         if (year_len) {
172                 num_tags++;
173                 sz += 4 + year_sz;
174         }
175         PARA_DEBUG_LOG("meta packet size: %zu bytes\n", sz);
176         /* terminating zero byte for the last sprintf() */
177         buf = p = para_malloc(sz + 1);
178         memcpy(p, OPUS_COMMENT_HEADER, strlen(OPUS_COMMENT_HEADER));
179         p += strlen(OPUS_COMMENT_HEADER);
180         write_u32(p, comment_sz);
181         p += 4;
182         strcpy(p, tags->comment);
183         p += comment_sz;
184         write_u32(p, num_tags);
185         p += 4;
186         if (artist_len) {
187                 write_u32(p, artist_sz);
188                 p += 4;
189                 sprintf(p, "artist=%s", tags->artist);
190                 p += artist_sz;
191         }
192         if (title_len) {
193                 write_u32(p, title_sz);
194                 p += 4;
195                 sprintf(p, "title=%s", tags->title);
196                 p += title_sz;
197         }
198         if (album_len) {
199                 write_u32(p, album_sz);
200                 p += 4;
201                 sprintf(p, "album=%s", tags->album);
202                 p += album_sz;
203         }
204         if (year_len) {
205                 write_u32(p, year_sz);
206                 p += 4;
207                 sprintf(p, "year=%s", tags->year);
208                 p += year_sz;
209         }
210         assert(p == buf + sz);
211         *result = buf;
212         return sz;
213 }
214
215 static int opus_rewrite_tags(const char *map, size_t mapsize,
216                 struct taginfo *tags, int output_fd,
217                 __a_unused const char *filename)
218 {
219         char *meta_packet;
220         size_t meta_sz;
221         int ret;
222
223         meta_sz = opus_make_meta_packet(tags, &meta_packet);
224         ret = ogg_rewrite_tags(map, mapsize, output_fd, meta_packet, meta_sz);
225         free(meta_packet);
226         return ret;
227 }
228
229 /**
230  * The init function of the ogg/opus audio format handler.
231  *
232  * \param afh Pointer to the struct to initialize.
233  */
234 void opus_afh_init(struct audio_format_handler *afh)
235 {
236         afh->get_file_info = opus_get_file_info,
237         afh->suffixes = opus_suffixes;
238         afh->rewrite_tags = opus_rewrite_tags;
239 }