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 {
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->channels = header->nb_channels;
56  spx_mode = header->mode;
57  speex_header_free(header);
58  } else {
59  switch (avctx->sample_rate) {
60  case 8000: spx_mode = 0; break;
61  case 16000: spx_mode = 1; break;
62  case 32000: spx_mode = 2; break;
63  default:
64  /* libspeex can handle any mode if initialized as ultra-wideband */
65  av_log(avctx, AV_LOG_WARNING, "Invalid sample rate: %d\n"
66  "Decoding as 32kHz ultra-wideband\n",
67  avctx->sample_rate);
68  spx_mode = 2;
69  }
70  }
71 
72  mode = speex_lib_get_mode(spx_mode);
73  if (!mode) {
74  av_log(avctx, AV_LOG_ERROR, "Unknown Speex mode %d", spx_mode);
75  return AVERROR_INVALIDDATA;
76  }
77  avctx->sample_rate = 8000 << spx_mode;
78  s->frame_size = 160 << spx_mode;
79 
80  if (avctx->channels < 1 || avctx->channels > 2) {
81  /* libspeex can handle mono or stereo if initialized as stereo */
82  av_log(avctx, AV_LOG_ERROR, "Invalid channel count: %d.\n"
83  "Decoding as stereo.\n", avctx->channels);
84  avctx->channels = 2;
85  }
86  avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
88 
89  speex_bits_init(&s->bits);
90  s->dec_state = speex_decoder_init(mode);
91  if (!s->dec_state) {
92  av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex decoder.\n");
93  return -1;
94  }
95 
96  if (avctx->channels == 2) {
97  SpeexCallback callback;
98  callback.callback_id = SPEEX_INBAND_STEREO;
99  callback.func = speex_std_stereo_request_handler;
100  callback.data = &s->stereo;
101  s->stereo = (SpeexStereoState)SPEEX_STEREO_STATE_INIT;
102  speex_decoder_ctl(s->dec_state, SPEEX_SET_HANDLER, &callback);
103  }
104 
106  avctx->coded_frame = &s->frame;
107 
108  return 0;
109 }
110 
111 static int libspeex_decode_frame(AVCodecContext *avctx, void *data,
112  int *got_frame_ptr, AVPacket *avpkt)
113 {
114  uint8_t *buf = avpkt->data;
115  int buf_size = avpkt->size;
116  LibSpeexContext *s = avctx->priv_data;
117  int16_t *output;
118  int ret, consumed = 0;
119 
120  /* get output buffer */
121  s->frame.nb_samples = s->frame_size;
122  if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
123  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
124  return ret;
125  }
126  output = (int16_t *)s->frame.data[0];
127 
128  /* if there is not enough data left for the smallest possible frame or the
129  next 5 bits are a terminator code, reset the libspeex buffer using the
130  current packet, otherwise ignore the current packet and keep decoding
131  frames from the libspeex buffer. */
132  if (speex_bits_remaining(&s->bits) < 5 ||
133  speex_bits_peek_unsigned(&s->bits, 5) == 0x1F) {
134  /* check for flush packet */
135  if (!buf || !buf_size) {
136  *got_frame_ptr = 0;
137  return buf_size;
138  }
139  /* set new buffer */
140  speex_bits_read_from(&s->bits, buf, buf_size);
141  consumed = buf_size;
142  }
143 
144  /* decode a single frame */
145  ret = speex_decode_int(s->dec_state, &s->bits, output);
146  if (ret <= -2) {
147  av_log(avctx, AV_LOG_ERROR, "Error decoding Speex frame.\n");
148  return AVERROR_INVALIDDATA;
149  }
150  if (avctx->channels == 2)
151  speex_decode_stereo_int(output, s->frame_size, &s->stereo);
152 
153  *got_frame_ptr = 1;
154  *(AVFrame *)data = s->frame;
155 
156  return consumed;
157 }
158 
160 {
161  LibSpeexContext *s = avctx->priv_data;
162 
163  speex_bits_destroy(&s->bits);
164  speex_decoder_destroy(s->dec_state);
165 
166  return 0;
167 }
168 
170 {
171  LibSpeexContext *s = avctx->priv_data;
172  speex_bits_reset(&s->bits);
173 }
174 
176  .name = "libspeex",
177  .type = AVMEDIA_TYPE_AUDIO,
178  .id = AV_CODEC_ID_SPEEX,
179  .priv_data_size = sizeof(LibSpeexContext),
185  .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"),
186 };
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
AVCodec ff_libspeex_decoder
Definition: libspeexdec.c:175
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
int size
Definition: avcodec.h:916
SpeexStereoState stereo
Definition: libspeexdec.c:34
SpeexBits bits
Definition: libspeexdec.c:33
#define AV_CH_LAYOUT_STEREO
signed 16 bits
Definition: samplefmt.h:52
AVCodec.
Definition: avcodec.h:2960
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2112
uint8_t
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
static av_cold void libspeex_decode_flush(AVCodecContext *avctx)
Definition: libspeexdec.c:169
void * dec_state
Definition: libspeexdec.c:35
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2165
audio channel layout utility functions
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
static av_cold int libspeex_decode_init(AVCodecContext *avctx)
Definition: libspeexdec.c:40
NULL
Definition: eval.c:52
external API header
int sample_rate
samples per second
Definition: avcodec.h:2104
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
static av_cold int libspeex_decode_close(AVCodecContext *avctx)
Definition: libspeexdec.c:159
int extradata_size
Definition: avcodec.h:1455
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:602
static int libspeex_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: libspeexdec.c:111
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
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:1772
void * priv_data
Definition: avcodec.h:1382
int channels
number of audio channels
Definition: avcodec.h:2105
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:898
int nb_samples
number of audio samples (per channel) described by this frame
Definition: avcodec.h:1042
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)