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
30
38
39
41 {
43 const SpeexMode *
mode;
45 int spx_mode;
46
48 header = speex_packet_to_header(avctx->
extradata,
50 if (!header)
52 }
54 int quality;
58 }
59
61 if (quality > 10) {
64 }
65
66 s->
pktsize = ((
const int[]){5,10,15,20,20,28,28,38,38,46,62})[quality];
67
68 spx_mode = 0;
69 } else if (header) {
71 avctx->
channels = header->nb_channels;
72 spx_mode = header->mode;
73 speex_header_free(header);
74 } else {
76 case 8000: spx_mode = 0; break;
77 case 16000: spx_mode = 1; break;
78 case 32000: spx_mode = 2; break;
79 default:
80 /* libspeex can handle any mode if initialized as ultra-wideband */
82 "Decoding as 32kHz ultra-wideband\n",
84 spx_mode = 2;
85 }
86 }
87
88 mode = speex_lib_get_mode(spx_mode);
89 if (!mode) {
92 }
96
98 /* libspeex can handle mono or stereo if initialized as stereo */
100 "Decoding as stereo.\n", avctx->
channels);
102 }
105
106 speex_bits_init(&s->
bits);
110 return -1;
111 }
112
115 callback.callback_id = SPEEX_INBAND_STEREO;
116 callback.func = speex_std_stereo_request_handler;
117 callback.data = &s->
stereo;
118 s->
stereo = (SpeexStereoState)SPEEX_STEREO_STATE_INIT;
119 speex_decoder_ctl(s->
dec_state, SPEEX_SET_HANDLER, &callback);
120 }
121
122 return 0;
123 }
124
126 int *got_frame_ptr,
AVPacket *avpkt)
127 {
129 int buf_size = avpkt->
size;
132 int16_t *output;
133 int ret, consumed = 0;
135
136 /* get output buffer */
140 output = (int16_t *)frame->
data[0];
141
142 /* if there is not enough data left for the smallest possible frame or the
143 next 5 bits are a terminator code, reset the libspeex buffer using the
144 current packet, otherwise ignore the current packet and keep decoding
145 frames from the libspeex buffer. */
146 if (speex_bits_remaining(&s->
bits) < 5 ||
147 speex_bits_peek_unsigned(&s->
bits, 5) == 0xF) {
148 /* check for flush packet */
149 if (!buf || !buf_size) {
150 *got_frame_ptr = 0;
151 return buf_size;
152 }
153 if (s->
pktsize && buf_size == 62)
155 /* set new buffer */
156 speex_bits_read_from(&s->
bits, buf, buf_size);
157 consumed = avpkt->
size;
158 }
159
160 /* decode a single frame */
162 if (ret <= -2) {
165 }
168
169 *got_frame_ptr = 1;
170
173 return consumed;
174 }
175
177 {
179
180 speex_bits_destroy(&s->
bits);
182
183 return 0;
184 }
185
187 {
189 speex_bits_reset(&s->
bits);
190 }
191
203 };