FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
libspeexdec.c
Go to the documentation of this file.
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 
27 #include "libavutil/common.h"
28 #include "avcodec.h"
29 #include "internal.h"
30 
31 typedef struct {
33  SpeexBits bits;
34  SpeexStereoState stereo;
35  void *dec_state;
38 
39 
41 {
42  LibSpeexContext *s = avctx->priv_data;
43  const SpeexMode *mode;
44  SpeexHeader *header = NULL;
45  int spx_mode;
46 
48  if (avctx->extradata && avctx->extradata_size >= 80) {
49  header = speex_packet_to_header(avctx->extradata,
50  avctx->extradata_size);
51  if (!header)
52  av_log(avctx, AV_LOG_WARNING, "Invalid Speex header\n");
53  }
54  if (header) {
55  avctx->sample_rate = header->rate;
56  avctx->channels = header->nb_channels;
57  spx_mode = header->mode;
58  speex_header_free(header);
59  } else {
60  switch (avctx->sample_rate) {
61  case 8000: spx_mode = 0; break;
62  case 16000: spx_mode = 1; break;
63  case 32000: spx_mode = 2; break;
64  default:
65  /* libspeex can handle any mode if initialized as ultra-wideband */
66  av_log(avctx, AV_LOG_WARNING, "Invalid sample rate: %d\n"
67  "Decoding as 32kHz ultra-wideband\n",
68  avctx->sample_rate);
69  spx_mode = 2;
70  }
71  }
72 
73  mode = speex_lib_get_mode(spx_mode);
74  if (!mode) {
75  av_log(avctx, AV_LOG_ERROR, "Unknown Speex mode %d", spx_mode);
76  return AVERROR_INVALIDDATA;
77  }
78  s->frame_size = 160 << spx_mode;
79  if (!avctx->sample_rate)
80  avctx->sample_rate = 8000 << spx_mode;
81 
82  if (avctx->channels < 1 || avctx->channels > 2) {
83  /* libspeex can handle mono or stereo if initialized as stereo */
84  av_log(avctx, AV_LOG_ERROR, "Invalid channel count: %d.\n"
85  "Decoding as stereo.\n", avctx->channels);
86  avctx->channels = 2;
87  }
88  avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
90 
91  speex_bits_init(&s->bits);
92  s->dec_state = speex_decoder_init(mode);
93  if (!s->dec_state) {
94  av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex decoder.\n");
95  return -1;
96  }
97 
98  if (avctx->channels == 2) {
99  SpeexCallback callback;
100  callback.callback_id = SPEEX_INBAND_STEREO;
101  callback.func = speex_std_stereo_request_handler;
102  callback.data = &s->stereo;
103  s->stereo = (SpeexStereoState)SPEEX_STEREO_STATE_INIT;
104  speex_decoder_ctl(s->dec_state, SPEEX_SET_HANDLER, &callback);
105  }
106 
108  avctx->coded_frame = &s->frame;
109 
110  return 0;
111 }
112 
113 static int libspeex_decode_frame(AVCodecContext *avctx, void *data,
114  int *got_frame_ptr, AVPacket *avpkt)
115 {
116  uint8_t *buf = avpkt->data;
117  int buf_size = avpkt->size;
118  LibSpeexContext *s = avctx->priv_data;
119  int16_t *output;
120  int ret, consumed = 0;
121 
122  /* get output buffer */
123  s->frame.nb_samples = s->frame_size;
124  if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
125  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
126  return ret;
127  }
128  output = (int16_t *)s->frame.data[0];
129 
130  /* if there is not enough data left for the smallest possible frame or the
131  next 5 bits are a terminator code, reset the libspeex buffer using the
132  current packet, otherwise ignore the current packet and keep decoding
133  frames from the libspeex buffer. */
134  if (speex_bits_remaining(&s->bits) < 5 ||
135  speex_bits_peek_unsigned(&s->bits, 5) == 0xF) {
136  /* check for flush packet */
137  if (!buf || !buf_size) {
138  *got_frame_ptr = 0;
139  return buf_size;
140  }
141  /* set new buffer */
142  speex_bits_read_from(&s->bits, buf, buf_size);
143  consumed = buf_size;
144  }
145 
146  /* decode a single frame */
147  ret = speex_decode_int(s->dec_state, &s->bits, output);
148  if (ret <= -2) {
149  av_log(avctx, AV_LOG_ERROR, "Error decoding Speex frame.\n");
150  return AVERROR_INVALIDDATA;
151  }
152  if (avctx->channels == 2)
153  speex_decode_stereo_int(output, s->frame_size, &s->stereo);
154 
155  *got_frame_ptr = 1;
156  *(AVFrame *)data = s->frame;
157 
158  return consumed;
159 }
160 
162 {
163  LibSpeexContext *s = avctx->priv_data;
164 
165  speex_bits_destroy(&s->bits);
166  speex_decoder_destroy(s->dec_state);
167 
168  return 0;
169 }
170 
172 {
173  LibSpeexContext *s = avctx->priv_data;
174  speex_bits_reset(&s->bits);
175 }
176 
178  .name = "libspeex",
179  .type = AVMEDIA_TYPE_AUDIO,
180  .id = AV_CODEC_ID_SPEEX,
181  .priv_data_size = sizeof(LibSpeexContext),
187  .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"),
188 };