FFmpeg
 All Data Structures Namespaces 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 LibSpeexContext {
32  SpeexBits bits;
33  SpeexStereoState stereo;
34  void *dec_state;
36  int pktsize;
38 
39 
41 {
42  LibSpeexContext *s = avctx->priv_data;
43  const SpeexMode *mode;
44  SpeexHeader *header = NULL;
45  int spx_mode;
46 
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 (avctx->codec_tag == MKTAG('S', 'P', 'X', 'N')) {
54  int quality;
55  if (!avctx->extradata || avctx->extradata && avctx->extradata_size < 47) {
56  av_log(avctx, AV_LOG_ERROR, "Missing or invalid extradata.\n");
57  return AVERROR_INVALIDDATA;
58  }
59 
60  quality = avctx->extradata[37];
61  if (quality > 10) {
62  av_log(avctx, AV_LOG_ERROR, "Unsupported quality mode %d.\n", quality);
63  return AVERROR_PATCHWELCOME;
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) {
70  avctx->sample_rate = header->rate;
71  avctx->channels = header->nb_channels;
72  spx_mode = header->mode;
73  speex_header_free(header);
74  } else {
75  switch (avctx->sample_rate) {
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 */
81  av_log(avctx, AV_LOG_WARNING, "Invalid sample rate: %d\n"
82  "Decoding as 32kHz ultra-wideband\n",
83  avctx->sample_rate);
84  spx_mode = 2;
85  }
86  }
87 
88  mode = speex_lib_get_mode(spx_mode);
89  if (!mode) {
90  av_log(avctx, AV_LOG_ERROR, "Unknown Speex mode %d", spx_mode);
91  return AVERROR_INVALIDDATA;
92  }
93  s->frame_size = 160 << spx_mode;
94  if (!avctx->sample_rate)
95  avctx->sample_rate = 8000 << spx_mode;
96 
97  if (avctx->channels < 1 || avctx->channels > 2) {
98  /* libspeex can handle mono or stereo if initialized as stereo */
99  av_log(avctx, AV_LOG_ERROR, "Invalid channel count: %d.\n"
100  "Decoding as stereo.\n", avctx->channels);
101  avctx->channels = 2;
102  }
103  avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
105 
106  speex_bits_init(&s->bits);
107  s->dec_state = speex_decoder_init(mode);
108  if (!s->dec_state) {
109  av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex decoder.\n");
110  return -1;
111  }
112 
113  if (avctx->channels == 2) {
114  SpeexCallback callback;
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 
125 static int libspeex_decode_frame(AVCodecContext *avctx, void *data,
126  int *got_frame_ptr, AVPacket *avpkt)
127 {
128  uint8_t *buf = avpkt->data;
129  int buf_size = avpkt->size;
130  LibSpeexContext *s = avctx->priv_data;
131  AVFrame *frame = data;
132  int16_t *output;
133  int ret, consumed = 0;
134  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
135 
136  /* get output buffer */
137  frame->nb_samples = s->frame_size;
138  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
139  return ret;
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)
154  buf_size = s->pktsize;
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 */
161  ret = speex_decode_int(s->dec_state, &s->bits, output);
162  if (ret <= -2) {
163  av_log(avctx, AV_LOG_ERROR, "Error decoding Speex frame.\n");
164  return AVERROR_INVALIDDATA;
165  }
166  if (avctx->channels == 2)
167  speex_decode_stereo_int(output, s->frame_size, &s->stereo);
168 
169  *got_frame_ptr = 1;
170 
171  if (!avctx->bit_rate)
172  speex_decoder_ctl(s->dec_state, SPEEX_GET_BITRATE, &avctx->bit_rate);
173  return consumed;
174 }
175 
177 {
178  LibSpeexContext *s = avctx->priv_data;
179 
180  speex_bits_destroy(&s->bits);
181  speex_decoder_destroy(s->dec_state);
182 
183  return 0;
184 }
185 
187 {
188  LibSpeexContext *s = avctx->priv_data;
189  speex_bits_reset(&s->bits);
190 }
191 
193  .name = "libspeex",
194  .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"),
195  .type = AVMEDIA_TYPE_AUDIO,
196  .id = AV_CODEC_ID_SPEEX,
197  .priv_data_size = sizeof(LibSpeexContext),
199  .close = libspeex_decode_close,
203 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
AVCodec ff_libspeex_decoder
Definition: libspeexdec.c:192
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static void flush(AVCodecContext *avctx)
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1597
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1468
SpeexStereoState stereo
Definition: libspeexdec.c:33
SpeexBits bits
Definition: libspeexdec.c:32
#define AV_CH_LAYOUT_STEREO
AVCodec.
Definition: avcodec.h:3392
#define AV_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:881
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2295
uint8_t
#define av_cold
Definition: attributes.h:82
mode
Definition: f_perms.c:27
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1647
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1467
static const uint8_t header[24]
Definition: sdr2.c:67
#define av_log(a,...)
static av_cold void libspeex_decode_flush(AVCodecContext *avctx)
Definition: libspeexdec.c:186
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void * dec_state
Definition: libspeexdec.c:34
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
const char * name
Name of the codec implementation.
Definition: avcodec.h:3399
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2338
static void callback(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time, enum dshowDeviceType devtype)
Definition: dshow.c:161
audio channel layout utility functions
static av_cold int libspeex_decode_init(AVCodecContext *avctx)
Definition: libspeexdec.c:40
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
Libavcodec external API header.
int sample_rate
samples per second
Definition: avcodec.h:2287
main external API structure.
Definition: avcodec.h:1532
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:894
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1564
static av_cold int libspeex_decode_close(AVCodecContext *avctx)
Definition: libspeexdec.c:176
void * buf
Definition: avisynth_c.h:553
int extradata_size
Definition: avcodec.h:1648
#define AV_CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time...
Definition: avcodec.h:906
static int libspeex_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: libspeexdec.c:125
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:192
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:572
common internal api header.
common internal and external API header
if(ret< 0)
Definition: vf_mcdeint.c:282
signed 16 bits
Definition: samplefmt.h:62
void * priv_data
Definition: avcodec.h:1574
int channels
number of audio channels
Definition: avcodec.h:2288
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:342
This structure stores compressed data.
Definition: avcodec.h:1444
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:235
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:856