Libav
libspeexdec.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 David Conrad
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; 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 {
32  SpeexBits bits;
33  SpeexStereoState stereo;
34  void *dec_state;
37 
38 
40 {
41  LibSpeexContext *s = avctx->priv_data;
42  const SpeexMode *mode;
43  SpeexHeader *header = NULL;
44  int spx_mode;
45 
47  if (avctx->extradata && avctx->extradata_size >= 80) {
48  header = speex_packet_to_header(avctx->extradata,
49  avctx->extradata_size);
50  if (!header)
51  av_log(avctx, AV_LOG_WARNING, "Invalid Speex header\n");
52  }
53  if (header) {
54  avctx->channels = header->nb_channels;
55  spx_mode = header->mode;
56  speex_header_free(header);
57  } else {
58  switch (avctx->sample_rate) {
59  case 8000: spx_mode = 0; break;
60  case 16000: spx_mode = 1; break;
61  case 32000: spx_mode = 2; break;
62  default:
63  /* libspeex can handle any mode if initialized as ultra-wideband */
64  av_log(avctx, AV_LOG_WARNING, "Invalid sample rate: %d\n"
65  "Decoding as 32kHz ultra-wideband\n",
66  avctx->sample_rate);
67  spx_mode = 2;
68  }
69  }
70 
71  mode = speex_lib_get_mode(spx_mode);
72  if (!mode) {
73  av_log(avctx, AV_LOG_ERROR, "Unknown Speex mode %d", spx_mode);
74  return AVERROR_INVALIDDATA;
75  }
76  avctx->sample_rate = 8000 << spx_mode;
77  s->frame_size = 160 << spx_mode;
78 
79  if (avctx->channels < 1 || avctx->channels > 2) {
80  /* libspeex can handle mono or stereo if initialized as stereo */
81  av_log(avctx, AV_LOG_ERROR, "Invalid channel count: %d.\n"
82  "Decoding as stereo.\n", avctx->channels);
83  avctx->channels = 2;
84  }
85  avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
87 
88  speex_bits_init(&s->bits);
89  s->dec_state = speex_decoder_init(mode);
90  if (!s->dec_state) {
91  av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex decoder.\n");
92  return -1;
93  }
94 
95  if (avctx->channels == 2) {
96  SpeexCallback callback;
97  callback.callback_id = SPEEX_INBAND_STEREO;
98  callback.func = speex_std_stereo_request_handler;
99  callback.data = &s->stereo;
100  s->stereo = (SpeexStereoState)SPEEX_STEREO_STATE_INIT;
101  speex_decoder_ctl(s->dec_state, SPEEX_SET_HANDLER, &callback);
102  }
103 
104  return 0;
105 }
106 
107 static int libspeex_decode_frame(AVCodecContext *avctx, void *data,
108  int *got_frame_ptr, AVPacket *avpkt)
109 {
110  uint8_t *buf = avpkt->data;
111  int buf_size = avpkt->size;
112  LibSpeexContext *s = avctx->priv_data;
113  AVFrame *frame = data;
114  int16_t *output;
115  int ret, consumed = 0;
116 
117  /* get output buffer */
118  frame->nb_samples = s->frame_size;
119  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
120  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
121  return ret;
122  }
123  output = (int16_t *)frame->data[0];
124 
125  /* if there is not enough data left for the smallest possible frame or the
126  next 5 bits are a terminator code, reset the libspeex buffer using the
127  current packet, otherwise ignore the current packet and keep decoding
128  frames from the libspeex buffer. */
129  if (speex_bits_remaining(&s->bits) < 5 ||
130  speex_bits_peek_unsigned(&s->bits, 5) == 0xF) {
131  /* check for flush packet */
132  if (!buf || !buf_size) {
133  *got_frame_ptr = 0;
134  return buf_size;
135  }
136  /* set new buffer */
137  speex_bits_read_from(&s->bits, buf, buf_size);
138  consumed = buf_size;
139  }
140 
141  /* decode a single frame */
142  ret = speex_decode_int(s->dec_state, &s->bits, output);
143  if (ret <= -2) {
144  av_log(avctx, AV_LOG_ERROR, "Error decoding Speex frame.\n");
145  return AVERROR_INVALIDDATA;
146  }
147  if (avctx->channels == 2)
148  speex_decode_stereo_int(output, s->frame_size, &s->stereo);
149 
150  *got_frame_ptr = 1;
151 
152  return consumed;
153 }
154 
156 {
157  LibSpeexContext *s = avctx->priv_data;
158 
159  speex_bits_destroy(&s->bits);
160  speex_decoder_destroy(s->dec_state);
161 
162  return 0;
163 }
164 
166 {
167  LibSpeexContext *s = avctx->priv_data;
168  speex_bits_reset(&s->bits);
169 }
170 
172  .name = "libspeex",
173  .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"),
174  .type = AVMEDIA_TYPE_AUDIO,
175  .id = AV_CODEC_ID_SPEEX,
176  .priv_data_size = sizeof(LibSpeexContext),
182 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:107
AVCodec ff_libspeex_decoder
Definition: libspeexdec.c:171
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
int size
Definition: avcodec.h:974
SpeexStereoState stereo
Definition: libspeexdec.c:33
SpeexBits bits
Definition: libspeexdec.c:32
#define AV_CH_LAYOUT_STEREO
signed 16 bits
Definition: samplefmt.h:52
AVCodec.
Definition: avcodec.h:2755
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:269
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1787
uint8_t
#define av_cold
Definition: attributes.h:66
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1162
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:711
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:973
static av_cold void libspeex_decode_flush(AVCodecContext *avctx)
Definition: libspeexdec.c:165
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void * dec_state
Definition: libspeexdec.c:34
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:740
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
const char * name
Name of the codec implementation.
Definition: avcodec.h:2762
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1840
audio channel layout utility functions
static av_cold int libspeex_decode_init(AVCodecContext *avctx)
Definition: libspeexdec.c:39
if(ac->has_optimized_func)
NULL
Definition: eval.c:55
Libavcodec external API header.
int sample_rate
samples per second
Definition: avcodec.h:1779
main external API structure.
Definition: avcodec.h:1054
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:489
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:575
static av_cold int libspeex_decode_close(AVCodecContext *avctx)
Definition: libspeexdec.c:155
int extradata_size
Definition: avcodec.h:1163
static int libspeex_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: libspeexdec.c:107
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:113
#define CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time...
Definition: avcodec.h:763
common internal api header.
common internal and external API header
static av_cold void flush(AVCodecContext *avctx)
Flush (reset) the frame ID after seeking.
Definition: alsdec.c:1792
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:498
void * priv_data
Definition: avcodec.h:1090
int channels
number of audio channels
Definition: avcodec.h:1780
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:950
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:151