2 * Copyright (C) 2002-2006 Jean-Marc Valin
3 * Copyright (C) 2010 Andre Noll <maan@tuebingen.mpg.de>
5 * Licensed under the GPL v2, see file COPYING.
8 /** \file spxdec_filter.c Paraslash's ogg/speex decoder. */
10 /* This file is based on speexdec.c, by Jean-Marc Valin, see below. */
12 /* Copyright (C) 2002-2006 Jean-Marc Valin
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions
19 - Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
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.
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.
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.
45 #include <speex/speex.h>
46 #include <speex/speex_header.h>
47 #include <speex/speex_stereo.h>
48 #include <speex/speex_callbacks.h>
51 #include "portable_io.h"
54 #include "buffer_tree.h"
60 /** Data specific to the speexdec filter. */
61 struct private_spxdec_data {
62 /** Header information obtained from the first ogg packet. */
63 struct spx_header_info shi;
64 /** Read from and written to by the Speex routines. */
66 /* Ogg pages are retrieved from this structure. */
68 /** Extracted from header. */
70 /** Total number of ogg packets retrieved so far. */
72 /** Needed to find out how much to skip. */
73 ogg_int64_t last_granule;
74 /** Also needed for skipping packets. */
76 /** The state information about the current stream. */
78 /** Whether \a os is initialized. */
82 static void spxdec_open(struct filter_node *fn)
84 struct private_spxdec_data *psd = zalloc(sizeof(*psd));
86 fn->private_data = psd;
88 psd->speex_serialno = -1;
91 static void speexdec_close(struct filter_node *fn)
93 struct private_spxdec_data *psd = fn->private_data;
96 /* Destroy the decoder state */
97 speex_decoder_destroy(psd->shi.state);
98 /* Destroy the bit-stream struct */
99 speex_bits_destroy(&psd->bits);
102 fn->private_data = NULL;
105 static int speexdec_execute(const struct btr_node *btrn, const char *cmd,
108 struct filter_node *fn = btr_context(btrn);
109 struct private_spxdec_data *psd = fn->private_data;
111 return decoder_execute(cmd, psd->shi.sample_rate, psd->shi.channels,
115 static int speexdec_init(struct filter_node *fn)
117 struct private_spxdec_data *psd = fn->private_data;
119 PARA_INFO_LOG("init\n");
120 ogg_sync_init(&psd->oy);
121 speex_bits_init(&psd->bits);
126 * Size of the output buffer.
128 * Valid streams have frame sizes in the range from 160 to 640. To avoid buffer
129 * overflows, we bail out if the decoder reports a value bigger than this.
131 #define MAX_FRAME_SIZE 2000
133 /* Copy Ogg packet to Speex bitstream */
134 static int speexdec_write_frames(int packet_no,
135 struct private_spxdec_data *psd, int skip_samples,
136 int page_nb_packets, struct btr_node *btrn)
140 for (j = 0; j != psd->shi.nframes; j++) {
141 short output[MAX_FRAME_SIZE], *btr_output;
142 int skip = skip_samples + psd->lookahead, skip_idx = 0;
143 int samples, this_frame_size,
144 new_frame_size = psd->shi.frame_size;
146 if (speex_decoder_ctl(psd->shi.state, SPEEX_GET_FRAME_SIZE,
147 &this_frame_size) == 0) {
148 if (this_frame_size > MAX_FRAME_SIZE)
149 return -E_SPX_DECODE_OVERFLOW;
152 if (speex_decode_int(psd->shi.state, &psd->bits, output) < 0)
153 return -E_SPX_DECODE;
154 if (speex_bits_remaining(&psd->bits) < 0)
155 return -E_SPX_DECODE_OVERFLOW;
156 if (psd->shi.channels == 2)
157 speex_decode_stereo_int(output, psd->shi.frame_size,
159 if (packet_no == 1 && j == 0 && skip_samples > 0) {
160 new_frame_size -= skip;
161 skip_idx = skip * psd->shi.channels;
163 if (packet_no == page_nb_packets && skip_samples < 0) {
164 new_frame_size = psd->shi.nframes * psd->shi.frame_size
165 + skip - j * psd->shi.frame_size;
166 if (new_frame_size < 0)
168 if (new_frame_size > psd->shi.frame_size)
169 new_frame_size = psd->shi.frame_size;
171 if (new_frame_size <= 0)
173 samples = new_frame_size * psd->shi.channels;
174 btr_output = arr_alloc(samples, 2);
175 for (i = 0; i < samples; i++)
176 btr_output[i] = read_u16(output + i + skip_idx);
177 btr_add_output((char *)btr_output, samples * 2, btrn);
182 /* Extract all available packets */
183 static int speexdec_extract_packets(struct private_spxdec_data *psd,
184 ogg_stream_state *os, int skip_samples, int page_nb_packets,
185 struct btr_node *btrn)
190 for (packet_no = 0;; psd->packet_count++) {
193 if (ogg_stream_packetout(os, &op) != 1)
195 if (op.bytes >= 5 && !memcmp(op.packet, "Speex", 5))
196 psd->speex_serialno = os->serialno;
197 if (psd->speex_serialno == -1)
199 if (os->serialno != psd->speex_serialno)
201 /* If first packet, process as Speex header */
202 if (psd->packet_count == 0) {
203 ret = spx_process_header(op.packet, op.bytes, &psd->shi);
206 ret = speex_decoder_ctl(psd->shi.state, SPEEX_GET_LOOKAHEAD,
210 if (!psd->shi.nframes)
211 psd->shi.nframes = 1;
212 PARA_INFO_LOG("frame size: %d\n", psd->shi.frame_size);
215 if (psd->packet_count == 1) /* ignore comments */
217 if (psd->packet_count <= 1 + psd->shi.extra_headers)
218 continue; /* Ignore extra headers */
220 /* check end of stream condition */
221 if (op.e_o_s && os->serialno == psd->speex_serialno)
223 speex_bits_read_from(&psd->bits, (char *)op.packet,
225 ret = speexdec_write_frames(packet_no, psd,
226 skip_samples, page_nb_packets, btrn);
234 static int compute_skip_samples(ogg_page *og, struct private_spxdec_data *psd)
236 int ret, page_granule = ogg_page_granulepos(og);
238 if (page_granule <= 0)
240 if (psd->shi.frame_size == 0)
242 ret = ogg_page_packets(og) * psd->shi.frame_size * psd->shi.nframes
243 - page_granule + psd->last_granule;
244 if (ogg_page_eos(og))
249 static int speexdec_post_monitor(__a_unused struct sched *s, void *context)
251 struct filter_node *fn = context;
252 struct private_spxdec_data *psd = fn->private_data;
253 struct btr_node *btrn = fn->btrn;
260 ret = ns = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
261 btr_merge(btrn, fn->min_iqs);
262 if (!psd->shi.state) {
265 ret = speexdec_init(fn);
269 nbytes = btr_next_buffer(btrn, &btr_buf);
270 nbytes = PARA_MIN(nbytes, (size_t)200);
272 char *data = ogg_sync_buffer(&psd->oy, nbytes);
273 memcpy(data, btr_buf, nbytes);
274 btr_consume(btrn, nbytes);
275 ogg_sync_wrote(&psd->oy, nbytes);
277 /* Loop for all complete pages we got */
278 while (ogg_sync_pageout(&psd->oy, &og) == 1) {
281 if (psd->stream_init == false) {
282 ogg_stream_init(&psd->os, ogg_page_serialno(&og));
283 psd->stream_init = true;
285 if (ogg_page_serialno(&og) != psd->os.serialno)
286 ogg_stream_reset_serialno(&psd->os, ogg_page_serialno(&og));
287 /* Add page to the bitstream */
288 ogg_stream_pagein(&psd->os, &og);
289 skip_samples = compute_skip_samples(&og, psd);
290 psd->last_granule = ogg_page_granulepos(&og);
291 ret = speexdec_extract_packets(psd, &psd->os, skip_samples,
292 ogg_page_packets(&og), btrn);
301 btr_remove_node(&fn->btrn);
305 const struct filter lsg_filter_cmd_com_spxdec_user_data = {
307 .close = speexdec_close,
308 .pre_monitor = generic_filter_pre_monitor,
309 .post_monitor = speexdec_post_monitor,
310 .execute = speexdec_execute,