Merge branch 't/decl_after_statement'
[paraslash.git] / spx_afh.c
1 /*
2  * Copyright (C) 2010-2011 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /* This file is based on speexdec.c, by Jean-Marc Valin, see below. */
8
9 /* Copyright (C) 2002-2006 Jean-Marc Valin
10    File: speexdec.c
11
12    Redistribution and use in source and binary forms, with or without
13    modification, are permitted provided that the following conditions
14    are met:
15    
16    - Redistributions of source code must retain the above copyright
17    notice, this list of conditions and the following disclaimer.
18    
19    - Redistributions in binary form must reproduce the above copyright
20    notice, this list of conditions and the following disclaimer in the
21    documentation and/or other materials provided with the distribution.
22    
23    - Neither the name of the Xiph.org Foundation nor the names of its
24    contributors may be used to endorse or promote products derived from
25    this software without specific prior written permission.
26    
27    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
31    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39 /** \file spx_afh.c Audio format handler for ogg/speex files. */
40
41 #include <stdbool.h>
42 #include <ogg/ogg.h>
43 #include <regex.h>
44 #include <speex/speex.h>
45 #include <speex/speex_header.h>
46 #include <speex/speex_stereo.h>
47
48 #include "para.h"
49 #include "afh.h"
50 #include "error.h"
51 #include "portable_io.h"
52 #include "string.h"
53 #include "spx.h"
54 #include "ogg_afh_common.h"
55
56 struct private_spx_data {
57         struct spx_header_info shi;
58 };
59
60
61 static char *copy_comment(const char *src, int len)
62 {
63         char *p = para_malloc(len + 1);
64
65         if (len > 0)
66                 memcpy(p, src, len);
67         p[len] = '\0';
68         PARA_DEBUG_LOG("%s\n", p);
69         return p;
70 }
71
72 static bool copy_if_tag_type(const char *tag, int taglen, const char *type,
73                 char **p)
74 {
75         int len = strlen(type);
76
77         if (taglen <= len)
78                 return false;
79         if (strncasecmp(tag, type, len))
80                 return false;
81         if (tag[len] != '=')
82                 return false;
83         free(*p);
84         *p = copy_comment(tag + len + 1, taglen - len - 1);
85         return true;
86 }
87
88 static int spx_get_comments(unsigned char *comments, int length,
89                 struct taginfo *tags)
90 {
91         char *c = (char *)comments;
92         uint32_t len, nb_fields;
93         int i;
94         char *end;
95
96         if (length < 8)
97                 return -E_SPX_COMMENT;
98         end = c + length;
99         len = read_u32(c);
100         c += 4;
101         if (c + len > end)
102                 return -E_SPX_COMMENT;
103         tags->comment = copy_comment(c, len);
104
105         c += len;
106         if (c + 4 > end)
107                 return -E_SPX_COMMENT;
108         nb_fields = read_u32(c);
109         PARA_DEBUG_LOG("%d comment(s)\n", nb_fields);
110         c += 4;
111         for (i = 0; i < nb_fields; i++, c += len) {
112                 char *tag;
113
114                 if (c + 4 > end)
115                         return -E_SPX_COMMENT;
116                 len = read_u32(c);
117                 c += 4;
118                 if (c + len > end)
119                         return -E_SPX_COMMENT;
120                 if (copy_if_tag_type(c, len, "author", &tags->artist))
121                         continue;
122                 if (copy_if_tag_type(c, len, "artist", &tags->artist))
123                         continue;
124                 if (copy_if_tag_type(c, len, "title", &tags->title))
125                         continue;
126                 if (copy_if_tag_type(c, len, "album", &tags->album))
127                         continue;
128                 if (copy_if_tag_type(c, len, "year", &tags->year))
129                         continue;
130                 if (copy_if_tag_type(c, len, "comment", &tags->comment))
131                         continue;
132                 tag = copy_comment(c, len);
133                 PARA_NOTICE_LOG("unrecognized comment: %s\n", tag);
134                 free(tag);
135         }
136         return 1;
137 }
138
139 static const char* speex_suffixes[] = {"spx", "speex", NULL};
140
141 static int spx_packet_callback(ogg_packet *packet, int packet_num,
142                 __a_unused int serial, struct afh_info *afhi,
143                 void *private_data)
144 {
145         struct private_spx_data *psd = private_data;
146         int ret;
147
148         if (packet_num == 0) {
149                 ret = spx_process_header(packet->packet, packet->bytes,
150                         &psd->shi);
151                 if (ret < 0)
152                         return ret;
153                 afhi->channels = psd->shi.channels;
154                 afhi->frequency = psd->shi.sample_rate;
155                 afhi->bitrate = psd->shi.bitrate / 1000;
156                 afhi->techinfo = make_message("%s, v%d", psd->shi.mode->modeName,
157                         psd->shi.mode->bitstream_version);
158                 return 1;
159         }
160         if (packet_num == 1) {
161                 ret = spx_get_comments(packet->packet, packet->bytes,
162                         &afhi->tags);
163                 if (ret < 0)
164                         return ret;
165                 return 0; /* header complete */
166         }
167         /* never reached */
168         return 0;
169 }
170
171 static int spx_get_file_info(char *map, size_t numbytes, __a_unused int fd,
172                 struct afh_info *afhi)
173 {
174         struct private_spx_data psd;
175         struct ogg_afh_callback_info spx_callback_info = {
176                 .packet_callback = spx_packet_callback,
177                 .private_data = &psd,
178         };
179
180         memset(&psd, 0, sizeof(psd));
181         return ogg_get_file_info(map, numbytes, afhi, &spx_callback_info);
182 }
183
184 /**
185  * The init function of the ogg/speex audio format handler.
186  *
187  * \param afh Pointer to the struct to initialize.
188  */
189 void spx_afh_init(struct audio_format_handler *afh)
190 {
191         afh->get_file_info = spx_get_file_info,
192         afh->suffixes = speex_suffixes;
193 }