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