wmadec: Set data size to 0 if nothing was decoded.
[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. For licencing details see 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 "ggo.h"
54 #include "buffer_tree.h"
55 #include "filter.h"
56 #include "error.h"
57 #include "string.h"
58 #include "spx.h"
59
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. */
65         SpeexBits bits;
66         /* Ogg pages are retrieved from this structure. */
67         ogg_sync_state oy;
68         /** Extracted from header. */
69         int speex_serialno;
70         /** Total number of ogg packets retrieved so far. */
71         int packet_count;
72         /** Needed to find out how much to skip. */
73         ogg_int64_t last_granule;
74         /** Also needed for skipping packets. */
75         int lookahead;
76         /** The state information about the current stream. */
77         ogg_stream_state os;
78         /** Whether \a os is initialized. */
79         bool stream_init;
80 };
81
82 static void spxdec_open(struct filter_node *fn)
83 {
84         struct private_spxdec_data *psd = para_calloc(sizeof(*psd));
85
86         fn->private_data = psd;
87         fn->min_iqs = 200;
88         psd->speex_serialno = -1;
89 }
90
91 static void speexdec_close(struct filter_node *fn)
92 {
93         struct private_spxdec_data *psd = fn->private_data;
94
95         if (psd->shi.state) {
96                 /* Destroy the decoder state */
97                 speex_decoder_destroy(psd->shi.state);
98                 /* Destroy the bit-stream struct */
99                 speex_bits_destroy(&psd->bits);
100         }
101         free(psd);
102         fn->private_data = NULL;
103 }
104
105 static int speexdec_execute(struct btr_node *btrn, const char *cmd,
106                 char **result)
107 {
108         struct filter_node *fn = btr_context(btrn);
109         struct private_spxdec_data *psd = fn->private_data;
110
111         return decoder_execute(cmd, psd->shi.sample_rate, psd->shi.channels,
112                 result);
113 }
114
115 static int speexdec_init(struct filter_node *fn)
116 {
117         struct private_spxdec_data *psd = fn->private_data;
118
119         PARA_INFO_LOG("init\n");
120         ogg_sync_init(&psd->oy);
121         speex_bits_init(&psd->bits);
122         return 1;
123 }
124
125 #if !defined(__LITTLE_ENDIAN__) && ( defined(WORDS_BIGENDIAN) || defined(__BIG_ENDIAN__) )
126 #define le_short(s) ((short) ((unsigned short) (s) << 8) | ((unsigned short) (s) >> 8))
127 #else
128 #define le_short(s) ((short) (s))
129 #endif
130
131 /**
132  * Size of the output buffer.
133  *
134  * Valid streams have frame sizes in the range from 160 to 640. To avoid buffer
135  * overflows, we bail out if the decoder reports a value bigger than this.
136  */
137 #define MAX_FRAME_SIZE 2000
138
139 /* Copy Ogg packet to Speex bitstream */
140 static int speexdec_write_frames(int packet_no,
141                 struct private_spxdec_data *psd, int skip_samples,
142                 int page_nb_packets, struct btr_node *btrn)
143 {
144         int i, j;
145
146         for (j = 0; j != psd->shi.nframes; j++) {
147                 short output[MAX_FRAME_SIZE], *btr_output;
148                 int skip = skip_samples + psd->lookahead, skip_idx = 0;
149                 int samples, this_frame_size,
150                         new_frame_size = psd->shi.frame_size;
151
152                 if (speex_decoder_ctl(psd->shi.state, SPEEX_GET_FRAME_SIZE,
153                                 &this_frame_size) == 0) {
154                         if (this_frame_size > MAX_FRAME_SIZE)
155                                 return -E_SPX_DECODE_OVERFLOW;
156                 };
157
158                 if (speex_decode_int(psd->shi.state, &psd->bits, output) < 0)
159                         return -E_SPX_DECODE;
160                 if (speex_bits_remaining(&psd->bits) < 0)
161                         return -E_SPX_DECODE_OVERFLOW;
162                 if (psd->shi.channels == 2)
163                         speex_decode_stereo_int(output, psd->shi.frame_size,
164                                 &psd->shi.stereo);
165                 if (packet_no == 1 && j == 0 && skip_samples > 0) {
166                         new_frame_size -= skip;
167                         skip_idx = skip * psd->shi.channels;
168                 }
169                 if (packet_no == page_nb_packets && skip_samples < 0) {
170                         new_frame_size = psd->shi.nframes * psd->shi.frame_size
171                                 + skip - j * psd->shi.frame_size;
172                         if (new_frame_size < 0)
173                                 new_frame_size = 0;
174                         if (new_frame_size > psd->shi.frame_size)
175                                 new_frame_size = psd->shi.frame_size;
176                 }
177                 if (new_frame_size <= 0)
178                         continue;
179                 samples = new_frame_size * psd->shi.channels;
180                 btr_output = para_malloc(2 * samples);
181                 for (i = 0; i < samples; i++)
182                         btr_output[i] = le_short(output[i + skip_idx]);
183                 btr_add_output((char *)btr_output, samples * 2, btrn);
184         }
185         return 1;
186 }
187
188 /* Extract all available packets */
189 static int speexdec_extract_packets(struct private_spxdec_data *psd,
190                 ogg_stream_state *os, int skip_samples, int page_nb_packets,
191                 struct btr_node *btrn)
192 {
193         int ret, packet_no;
194         bool eos = false;
195
196         for (packet_no = 0;; psd->packet_count++) {
197                 ogg_packet op;
198
199                 if (ogg_stream_packetout(os, &op) != 1)
200                         return 0;
201                 if (op.bytes >= 5 && !memcmp(op.packet, "Speex", 5))
202                         psd->speex_serialno = os->serialno;
203                 if (psd->speex_serialno == -1)
204                         return 0;
205                 if (os->serialno != psd->speex_serialno)
206                         return 0;
207                 /* If first packet, process as Speex header */
208                 if (psd->packet_count == 0) {
209                         ret = spx_process_header(op.packet, op.bytes, &psd->shi);
210                         if (ret < 0)
211                                 return ret;
212                         ret = speex_decoder_ctl(psd->shi.state, SPEEX_GET_LOOKAHEAD,
213                                 &psd->lookahead);
214                         if (ret < 0)
215                                 return ret;
216                         if (!psd->shi.nframes)
217                                 psd->shi.nframes = 1;
218                         PARA_INFO_LOG("frame size: %d\n", psd->shi.frame_size);
219                         continue;
220                 }
221                 if (psd->packet_count == 1) /* ignore comments */
222                         continue;
223                 if (psd->packet_count <= 1 + psd->shi.extra_headers)
224                         continue; /* Ignore extra headers */
225                 packet_no++;
226                 /* check end of stream condition */
227                 if (op.e_o_s && os->serialno == psd->speex_serialno)
228                         eos = true;
229                 speex_bits_read_from(&psd->bits, (char *)op.packet,
230                         op.bytes);
231                 ret = speexdec_write_frames(packet_no, psd,
232                         skip_samples, page_nb_packets, btrn);
233                 if (ret < 0)
234                         return ret;
235                 if (eos == true)
236                         return -E_SPX_EOS;
237         }
238 }
239
240 static int compute_skip_samples(ogg_page *og, struct private_spxdec_data *psd)
241 {
242         int ret, page_granule = ogg_page_granulepos(og);
243
244         if (page_granule <= 0)
245                 return 0;
246         if (psd->shi.frame_size == 0)
247                 return 0;
248         ret = ogg_page_packets(og) * psd->shi.frame_size * psd->shi.nframes
249                 - page_granule + psd->last_granule;
250         if (ogg_page_eos(og))
251                 ret = -ret;
252         return ret;
253 }
254
255 static int speexdec_post_select(__a_unused struct sched *s, void *context)
256 {
257         struct filter_node *fn = context;
258         struct private_spxdec_data *psd = fn->private_data;
259         struct btr_node *btrn = fn->btrn;
260         int ret, ns;
261         ogg_page og;
262         char *btr_buf;
263         size_t nbytes;
264
265 next_buffer:
266         ret = ns = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
267         btr_merge(btrn, fn->min_iqs);
268         if (!psd->shi.state) {
269                 if (ret <= 0)
270                         goto fail;
271                 ret = speexdec_init(fn);
272                 if (ret <= 0)
273                         goto fail;
274         }
275         nbytes = btr_next_buffer(btrn, &btr_buf);
276         nbytes = PARA_MIN(nbytes, (size_t)200);
277         if (nbytes > 0) {
278                 char *data = ogg_sync_buffer(&psd->oy, nbytes);
279                 memcpy(data, btr_buf, nbytes);
280                 btr_consume(btrn, nbytes);
281                 ogg_sync_wrote(&psd->oy, nbytes);
282         }
283         /* Loop for all complete pages we got */
284         while (ogg_sync_pageout(&psd->oy, &og) == 1) {
285                 int skip_samples;
286
287                 if (psd->stream_init == false) {
288                         ogg_stream_init(&psd->os, ogg_page_serialno(&og));
289                         psd->stream_init = true;
290                 }
291                 if (ogg_page_serialno(&og) != psd->os.serialno)
292                         ogg_stream_reset_serialno(&psd->os, ogg_page_serialno(&og));
293                 /* Add page to the bitstream */
294                 ogg_stream_pagein(&psd->os, &og);
295                 skip_samples = compute_skip_samples(&og, psd);
296                 psd->last_granule = ogg_page_granulepos(&og);
297                 ret = speexdec_extract_packets(psd, &psd->os, skip_samples,
298                         ogg_page_packets(&og), btrn);
299                 if (ret < 0)
300                         goto fail;
301         }
302         if (ns > 0)
303                 goto next_buffer;
304         ret = ns;
305 fail:
306         if (ret < 0)
307                 btr_remove_node(&fn->btrn);
308         return ret;
309 }
310
311 /**
312  * The init function of the ogg/speex decoder.
313  *
314  * \param f Its fields are filled in by the function.
315  */
316 void spxdec_filter_init(struct filter *f)
317 {
318         f->open = spxdec_open;
319         f->close = speexdec_close;
320         f->pre_select = generic_filter_pre_select;
321         f->post_select = speexdec_post_select;
322         f->execute = speexdec_execute;
323 }