1 /*
2 * Copyright (C) 2008 David Conrad
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <speex/speex.h>
22 #include <speex/speex_header.h>
23 #include <speex/speex_stereo.h>
24 #include <speex/speex_callbacks.h>
25
31
39
40
42 {
47
53 }
59 }
60
65 }
66
67 s->pktsize = ((
const int[]){5,10,15,20,20,28,28,38,38,46,62})[
quality];
68
69 spx_mode = 0;
75 } else {
77 case 8000: spx_mode = 0; break;
78 case 16000: spx_mode = 1; break;
79 case 32000: spx_mode = 2; break;
80 default:
81 /* libspeex can handle any mode if initialized as ultra-wideband */
83 "Decoding as 32kHz ultra-wideband\n",
85 spx_mode = 2;
86 }
87 }
88
89 mode = speex_lib_get_mode(spx_mode);
93 }
94 s->frame_size = 160 << spx_mode;
97
99 /* libspeex can handle mono or stereo if initialized as stereo */
103 }
107
108 speex_bits_init(&
s->bits);
109 s->dec_state = speex_decoder_init(
mode);
112 return -1;
113 }
114
118 callback.func = speex_std_stereo_request_handler;
120 s->stereo = (SpeexStereoState)SPEEX_STEREO_STATE_INIT;
121 speex_decoder_ctl(
s->dec_state, SPEEX_SET_HANDLER, &
callback);
122 }
123
124 return 0;
125 }
126
128 int *got_frame_ptr,
AVPacket *avpkt)
129 {
130 uint8_t *buf = avpkt->
data;
131 int buf_size = avpkt->
size;
134 int ret, consumed = 0;
136
137 /* get output buffer */
138 frame->nb_samples =
s->frame_size;
142
143 /* if there is not enough data left for the smallest possible frame or the
144 next 5 bits are a terminator code, reset the libspeex buffer using the
145 current packet, otherwise ignore the current packet and keep decoding
146 frames from the libspeex buffer. */
147 if (speex_bits_remaining(&
s->bits) < 5 ||
148 speex_bits_peek_unsigned(&
s->bits, 5) == 0xF) {
149 /* check for flush packet */
150 if (!buf || !buf_size) {
151 *got_frame_ptr = 0;
152 return buf_size;
153 }
154 if (
s->pktsize && buf_size == 62)
155 buf_size =
s->pktsize;
156 /* set new buffer */
157 speex_bits_read_from(&
s->bits, buf, buf_size);
158 consumed = avpkt->
size;
159 }
160
161 /* decode a single frame */
162 ret = speex_decode_int(
s->dec_state, &
s->bits,
output);
166 }
168 speex_decode_stereo_int(
output,
s->frame_size, &
s->stereo);
169
170 *got_frame_ptr = 1;
171
173 speex_decoder_ctl(
s->dec_state, SPEEX_GET_BITRATE, &avctx->
bit_rate);
174 return consumed;
175 }
176
178 {
180
181 speex_bits_destroy(&
s->bits);
182 speex_decoder_destroy(
s->dec_state);
183
184 return 0;
185 }
186
188 {
190 speex_bits_reset(&
s->bits);
191 }
192
194 .
p.
name =
"libspeex",
199 .p.wrapper_name = "libspeex",
206 };