command.c: Fix typo in comment.
[paraslash.git] / spxdec_filter.c
1 /*
2 * Copyright (C) 2002-2006 Jean-Marc Valin
3 * Copyright (C) 2010 Andre Noll <maan@tuebingen.mpg.de>
4 *
5 * Licensed under the GPL v2, see file COPYING.
6 */
7
8 /** \file spxdec_filter.c Paraslash's ogg/speex decoder. */
9
10 /* This file is based on speexdec.c, by Jean-Marc Valin, see below. */
11
12 /* Copyright (C) 2002-2006 Jean-Marc Valin
13 File: speexdec.c
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions
17 are met:
18
19 - Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
21
22 - Redistributions in binary form must reproduce the above copyright
23 notice, this list of conditions and the following disclaimer in the
24 documentation and/or other materials provided with the distribution.
25
26 - Neither the name of the Xiph.org Foundation nor the names of its
27 contributors may be used to endorse or promote products derived from
28 this software without specific prior written permission.
29
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
34 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
37 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
38 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
39 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 */
42
43 #include <regex.h>
44 #include <ogg/ogg.h>
45 #include <speex/speex.h>
46 #include <speex/speex_header.h>
47 #include <speex/speex_stereo.h>
48 #include <speex/speex_callbacks.h>
49
50 #include "para.h"
51 #include "list.h"
52 #include "sched.h"
53 #include "buffer_tree.h"
54 #include "filter.h"
55 #include "error.h"
56 #include "string.h"
57 #include "spx.h"
58
59 /** Data specific to the speexdec filter. */
60 struct private_spxdec_data {
61 /** Header information obtained from the first ogg packet. */
62 struct spx_header_info shi;
63 /** Read from and written to by the Speex routines. */
64 SpeexBits bits;
65 /* Ogg pages are retrieved from this structure. */
66 ogg_sync_state oy;
67 /** Extracted from header. */
68 int speex_serialno;
69 /** Total number of ogg packets retrieved so far. */
70 int packet_count;
71 /** Needed to find out how much to skip. */
72 ogg_int64_t last_granule;
73 /** Also needed for skipping packets. */
74 int lookahead;
75 /** The state information about the current stream. */
76 ogg_stream_state os;
77 /** Whether \a os is initialized. */
78 bool stream_init;
79 };
80
81 static void spxdec_open(struct filter_node *fn)
82 {
83 struct private_spxdec_data *psd = para_calloc(sizeof(*psd));
84
85 fn->private_data = psd;
86 fn->min_iqs = 200;
87 psd->speex_serialno = -1;
88 }
89
90 static void speexdec_close(struct filter_node *fn)
91 {
92 struct private_spxdec_data *psd = fn->private_data;
93
94 if (psd->shi.state) {
95 /* Destroy the decoder state */
96 speex_decoder_destroy(psd->shi.state);
97 /* Destroy the bit-stream struct */
98 speex_bits_destroy(&psd->bits);
99 }
100 free(psd);
101 fn->private_data = NULL;
102 }
103
104 static int speexdec_execute(struct btr_node *btrn, const char *cmd,
105 char **result)
106 {
107 struct filter_node *fn = btr_context(btrn);
108 struct private_spxdec_data *psd = fn->private_data;
109
110 return decoder_execute(cmd, psd->shi.sample_rate, psd->shi.channels,
111 result);
112 }
113
114 static int speexdec_init(struct filter_node *fn)
115 {
116 struct private_spxdec_data *psd = fn->private_data;
117
118 PARA_INFO_LOG("init\n");
119 ogg_sync_init(&psd->oy);
120 speex_bits_init(&psd->bits);
121 return 1;
122 }
123
124 #if !defined(__LITTLE_ENDIAN__) && ( defined(WORDS_BIGENDIAN) || defined(__BIG_ENDIAN__) )
125 #define le_short(s) ((short) ((unsigned short) (s) << 8) | ((unsigned short) (s) >> 8))
126 #else
127 #define le_short(s) ((short) (s))
128 #endif
129
130 /**
131 * Size of the output buffer.
132 *
133 * Valid streams have frame sizes in the range from 160 to 640. To avoid buffer
134 * overflows, we bail out if the decoder reports a value bigger than this.
135 */
136 #define MAX_FRAME_SIZE 2000
137
138 /* Copy Ogg packet to Speex bitstream */
139 static int speexdec_write_frames(int packet_no,
140 struct private_spxdec_data *psd, int skip_samples,
141 int page_nb_packets, struct btr_node *btrn)
142 {
143 int i, j;
144
145 for (j = 0; j != psd->shi.nframes; j++) {
146 short output[MAX_FRAME_SIZE], *btr_output;
147 int skip = skip_samples + psd->lookahead, skip_idx = 0;
148 int samples, this_frame_size,
149 new_frame_size = psd->shi.frame_size;
150
151 if (speex_decoder_ctl(psd->shi.state, SPEEX_GET_FRAME_SIZE,
152 &this_frame_size) == 0) {
153 if (this_frame_size > MAX_FRAME_SIZE)
154 return -E_SPX_DECODE_OVERFLOW;
155 };
156
157 if (speex_decode_int(psd->shi.state, &psd->bits, output) < 0)
158 return -E_SPX_DECODE;
159 if (speex_bits_remaining(&psd->bits) < 0)
160 return -E_SPX_DECODE_OVERFLOW;
161 if (psd->shi.channels == 2)
162 speex_decode_stereo_int(output, psd->shi.frame_size,
163 &psd->shi.stereo);
164 if (packet_no == 1 && j == 0 && skip_samples > 0) {
165 new_frame_size -= skip;
166 skip_idx = skip * psd->shi.channels;
167 }
168 if (packet_no == page_nb_packets && skip_samples < 0) {
169 new_frame_size = psd->shi.nframes * psd->shi.frame_size
170 + skip - j * psd->shi.frame_size;
171 if (new_frame_size < 0)
172 new_frame_size = 0;
173 if (new_frame_size > psd->shi.frame_size)
174 new_frame_size = psd->shi.frame_size;
175 }
176 if (new_frame_size <= 0)
177 continue;
178 samples = new_frame_size * psd->shi.channels;
179 btr_output = para_malloc(2 * samples);
180 for (i = 0; i < samples; i++)
181 btr_output[i] = le_short(output[i + skip_idx]);
182 btr_add_output((char *)btr_output, samples * 2, btrn);
183 }
184 return 1;
185 }
186
187 /* Extract all available packets */
188 static int speexdec_extract_packets(struct private_spxdec_data *psd,
189 ogg_stream_state *os, int skip_samples, int page_nb_packets,
190 struct btr_node *btrn)
191 {
192 int ret, packet_no;
193 bool eos = false;
194
195 for (packet_no = 0;; psd->packet_count++) {
196 ogg_packet op;
197
198 if (ogg_stream_packetout(os, &op) != 1)
199 return 0;
200 if (op.bytes >= 5 && !memcmp(op.packet, "Speex", 5))
201 psd->speex_serialno = os->serialno;
202 if (psd->speex_serialno == -1)
203 return 0;
204 if (os->serialno != psd->speex_serialno)
205 return 0;
206 /* If first packet, process as Speex header */
207 if (psd->packet_count == 0) {
208 ret = spx_process_header(op.packet, op.bytes, &psd->shi);
209 if (ret < 0)
210 return ret;
211 ret = speex_decoder_ctl(psd->shi.state, SPEEX_GET_LOOKAHEAD,
212 &psd->lookahead);
213 if (ret < 0)
214 return ret;
215 if (!psd->shi.nframes)
216 psd->shi.nframes = 1;
217 PARA_INFO_LOG("frame size: %d\n", psd->shi.frame_size);
218 continue;
219 }
220 if (psd->packet_count == 1) /* ignore comments */
221 continue;
222 if (psd->packet_count <= 1 + psd->shi.extra_headers)
223 continue; /* Ignore extra headers */
224 packet_no++;
225 /* check end of stream condition */
226 if (op.e_o_s && os->serialno == psd->speex_serialno)
227 eos = true;
228 speex_bits_read_from(&psd->bits, (char *)op.packet,
229 op.bytes);
230 ret = speexdec_write_frames(packet_no, psd,
231 skip_samples, page_nb_packets, btrn);
232 if (ret < 0)
233 return ret;
234 if (eos == true)
235 return -E_SPX_EOS;
236 }
237 }
238
239 static int compute_skip_samples(ogg_page *og, struct private_spxdec_data *psd)
240 {
241 int ret, page_granule = ogg_page_granulepos(og);
242
243 if (page_granule <= 0)
244 return 0;
245 if (psd->shi.frame_size == 0)
246 return 0;
247 ret = ogg_page_packets(og) * psd->shi.frame_size * psd->shi.nframes
248 - page_granule + psd->last_granule;
249 if (ogg_page_eos(og))
250 ret = -ret;
251 return ret;
252 }
253
254 static int speexdec_post_select(__a_unused struct sched *s, void *context)
255 {
256 struct filter_node *fn = context;
257 struct private_spxdec_data *psd = fn->private_data;
258 struct btr_node *btrn = fn->btrn;
259 int ret, ns;
260 ogg_page og;
261 char *btr_buf;
262 size_t nbytes;
263
264 next_buffer:
265 ret = ns = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
266 btr_merge(btrn, fn->min_iqs);
267 if (!psd->shi.state) {
268 if (ret <= 0)
269 goto fail;
270 ret = speexdec_init(fn);
271 if (ret <= 0)
272 goto fail;
273 }
274 nbytes = btr_next_buffer(btrn, &btr_buf);
275 nbytes = PARA_MIN(nbytes, (size_t)200);
276 if (nbytes > 0) {
277 char *data = ogg_sync_buffer(&psd->oy, nbytes);
278 memcpy(data, btr_buf, nbytes);
279 btr_consume(btrn, nbytes);
280 ogg_sync_wrote(&psd->oy, nbytes);
281 }
282 /* Loop for all complete pages we got */
283 while (ogg_sync_pageout(&psd->oy, &og) == 1) {
284 int skip_samples;
285
286 if (psd->stream_init == false) {
287 ogg_stream_init(&psd->os, ogg_page_serialno(&og));
288 psd->stream_init = true;
289 }
290 if (ogg_page_serialno(&og) != psd->os.serialno)
291 ogg_stream_reset_serialno(&psd->os, ogg_page_serialno(&og));
292 /* Add page to the bitstream */
293 ogg_stream_pagein(&psd->os, &og);
294 skip_samples = compute_skip_samples(&og, psd);
295 psd->last_granule = ogg_page_granulepos(&og);
296 ret = speexdec_extract_packets(psd, &psd->os, skip_samples,
297 ogg_page_packets(&og), btrn);
298 if (ret < 0)
299 goto fail;
300 }
301 if (ns > 0)
302 goto next_buffer;
303 ret = ns;
304 fail:
305 if (ret < 0)
306 btr_remove_node(&fn->btrn);
307 return ret;
308 }
309
310 const struct filter lsg_filter_cmd_com_spxdec_user_data = {
311 .open = spxdec_open,
312 .close = speexdec_close,
313 .pre_select = generic_filter_pre_select,
314 .post_select = speexdec_post_select,
315 .execute = speexdec_execute,
316 };