libspeexenc.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 Justin Ruggles
3  * Copyright (c) 2009 Xuggle Incorporated
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
67 #include <speex/speex.h>
68 #include <speex/speex_header.h>
69 #include <speex/speex_stereo.h>
70 #include "libavutil/mathematics.h"
71 #include "libavutil/opt.h"
72 #include "avcodec.h"
73 #include "internal.h"
74 
75 typedef struct {
76  AVClass *class;
77  SpeexBits bits;
78  SpeexHeader header;
79  void *enc_state;
81  float vbr_quality;
83  int abr;
85  int lookahead;
86  int64_t next_pts;
89 
92 {
93  const char *mode_str = "unknown";
94 
95  av_log(avctx, AV_LOG_DEBUG, "channels: %d\n", avctx->channels);
96  switch (s->header.mode) {
97  case SPEEX_MODEID_NB: mode_str = "narrowband"; break;
98  case SPEEX_MODEID_WB: mode_str = "wideband"; break;
99  case SPEEX_MODEID_UWB: mode_str = "ultra-wideband"; break;
100  }
101  av_log(avctx, AV_LOG_DEBUG, "mode: %s\n", mode_str);
102  if (s->header.vbr) {
103  av_log(avctx, AV_LOG_DEBUG, "rate control: VBR\n");
104  av_log(avctx, AV_LOG_DEBUG, " quality: %f\n", s->vbr_quality);
105  } else if (s->abr) {
106  av_log(avctx, AV_LOG_DEBUG, "rate control: ABR\n");
107  av_log(avctx, AV_LOG_DEBUG, " bitrate: %d bps\n", avctx->bit_rate);
108  } else {
109  av_log(avctx, AV_LOG_DEBUG, "rate control: CBR\n");
110  av_log(avctx, AV_LOG_DEBUG, " bitrate: %d bps\n", avctx->bit_rate);
111  }
112  av_log(avctx, AV_LOG_DEBUG, "complexity: %d\n",
113  avctx->compression_level);
114  av_log(avctx, AV_LOG_DEBUG, "frame size: %d samples\n",
115  avctx->frame_size);
116  av_log(avctx, AV_LOG_DEBUG, "frames per packet: %d\n",
117  s->frames_per_packet);
118  av_log(avctx, AV_LOG_DEBUG, "packet size: %d\n",
119  avctx->frame_size * s->frames_per_packet);
120 }
121 
123 {
124  LibSpeexEncContext *s = avctx->priv_data;
125  const SpeexMode *mode;
126  uint8_t *header_data;
127  int header_size;
128  int32_t complexity;
129 
130  /* channels */
131  if (avctx->channels < 1 || avctx->channels > 2) {
132  av_log(avctx, AV_LOG_ERROR, "Invalid channels (%d). Only stereo and "
133  "mono are supported\n", avctx->channels);
134  return AVERROR(EINVAL);
135  }
136 
137  /* sample rate and encoding mode */
138  switch (avctx->sample_rate) {
139  case 8000: mode = &speex_nb_mode; break;
140  case 16000: mode = &speex_wb_mode; break;
141  case 32000: mode = &speex_uwb_mode; break;
142  default:
143  av_log(avctx, AV_LOG_ERROR, "Sample rate of %d Hz is not supported. "
144  "Resample to 8, 16, or 32 kHz.\n", avctx->sample_rate);
145  return AVERROR(EINVAL);
146  }
147 
148  /* initialize libspeex */
149  s->enc_state = speex_encoder_init(mode);
150  if (!s->enc_state) {
151  av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex\n");
152  return -1;
153  }
154  speex_init_header(&s->header, avctx->sample_rate, avctx->channels, mode);
155 
156  /* rate control method and parameters */
157  if (avctx->flags & CODEC_FLAG_QSCALE) {
158  /* VBR */
159  s->header.vbr = 1;
160  speex_encoder_ctl(s->enc_state, SPEEX_SET_VBR, &s->header.vbr);
161  s->vbr_quality = av_clipf(avctx->global_quality / (float)FF_QP2LAMBDA,
162  0.0f, 10.0f);
163  speex_encoder_ctl(s->enc_state, SPEEX_SET_VBR_QUALITY, &s->vbr_quality);
164  } else {
165  s->header.bitrate = avctx->bit_rate;
166  if (avctx->bit_rate > 0) {
167  /* CBR or ABR by bitrate */
168  if (s->abr) {
169  speex_encoder_ctl(s->enc_state, SPEEX_SET_ABR,
170  &s->header.bitrate);
171  speex_encoder_ctl(s->enc_state, SPEEX_GET_ABR,
172  &s->header.bitrate);
173  } else {
174  speex_encoder_ctl(s->enc_state, SPEEX_SET_BITRATE,
175  &s->header.bitrate);
176  speex_encoder_ctl(s->enc_state, SPEEX_GET_BITRATE,
177  &s->header.bitrate);
178  }
179  } else {
180  /* CBR by quality */
181  speex_encoder_ctl(s->enc_state, SPEEX_SET_QUALITY,
182  &s->cbr_quality);
183  speex_encoder_ctl(s->enc_state, SPEEX_GET_BITRATE,
184  &s->header.bitrate);
185  }
186  /* stereo side information adds about 800 bps to the base bitrate */
187  /* TODO: this should be calculated exactly */
188  avctx->bit_rate = s->header.bitrate + (avctx->channels == 2 ? 800 : 0);
189  }
190 
191  /* set encoding complexity */
193  complexity = av_clip(avctx->compression_level, 0, 10);
194  speex_encoder_ctl(s->enc_state, SPEEX_SET_COMPLEXITY, &complexity);
195  }
196  speex_encoder_ctl(s->enc_state, SPEEX_GET_COMPLEXITY, &complexity);
197  avctx->compression_level = complexity;
198 
199  /* set packet size */
200  avctx->frame_size = s->header.frame_size;
201  s->header.frames_per_packet = s->frames_per_packet;
202 
203  /* set encoding delay */
204  speex_encoder_ctl(s->enc_state, SPEEX_GET_LOOKAHEAD, &s->lookahead);
205  s->next_pts = -s->lookahead;
206 
207  /* create header packet bytes from header struct */
208  /* note: libspeex allocates the memory for header_data, which is freed
209  below with speex_header_free() */
210  header_data = speex_header_to_packet(&s->header, &header_size);
211 
212  /* allocate extradata and coded_frame */
213  avctx->extradata = av_malloc(header_size + FF_INPUT_BUFFER_PADDING_SIZE);
214  avctx->coded_frame = avcodec_alloc_frame();
215  if (!avctx->extradata || !avctx->coded_frame) {
216  speex_header_free(header_data);
217  speex_encoder_destroy(s->enc_state);
218  av_log(avctx, AV_LOG_ERROR, "memory allocation error\n");
219  return AVERROR(ENOMEM);
220  }
221 
222  /* copy header packet to extradata */
223  memcpy(avctx->extradata, header_data, header_size);
224  avctx->extradata_size = header_size;
225  speex_header_free(header_data);
226 
227  /* init libspeex bitwriter */
228  speex_bits_init(&s->bits);
229 
230  print_enc_params(avctx, s);
231  return 0;
232 }
233 
234 static int encode_frame(AVCodecContext *avctx, uint8_t *frame, int buf_size,
235  void *data)
236 {
237  LibSpeexEncContext *s = avctx->priv_data;
238  int16_t *samples = data;
239 
240  if (data) {
241  /* encode Speex frame */
242  if (avctx->channels == 2)
243  speex_encode_stereo_int(samples, s->header.frame_size, &s->bits);
244  speex_encode_int(s->enc_state, samples, &s->bits);
245  s->pkt_frame_count++;
246  s->pkt_sample_count += avctx->frame_size;
247  } else {
248  /* handle end-of-stream */
249  if (!s->pkt_frame_count)
250  return 0;
251  /* add extra terminator codes for unused frames in last packet */
252  while (s->pkt_frame_count < s->frames_per_packet) {
253  speex_bits_pack(&s->bits, 15, 5);
254  s->pkt_frame_count++;
255  }
256  }
257 
258  /* write output if all frames for the packet have been encoded */
259  if (s->pkt_frame_count == s->frames_per_packet) {
260  s->pkt_frame_count = 0;
261  avctx->coded_frame->pts =
262  av_rescale_q(s->next_pts, (AVRational){ 1, avctx->sample_rate },
263  avctx->time_base);
264  s->next_pts += s->pkt_sample_count;
265  s->pkt_sample_count = 0;
266  if (buf_size > speex_bits_nbytes(&s->bits)) {
267  int ret = speex_bits_write(&s->bits, frame, buf_size);
268  speex_bits_reset(&s->bits);
269  return ret;
270  } else {
271  av_log(avctx, AV_LOG_ERROR, "output buffer too small");
272  return AVERROR(EINVAL);
273  }
274  }
275  return 0;
276 }
277 
279 {
280  LibSpeexEncContext *s = avctx->priv_data;
281 
282  speex_bits_destroy(&s->bits);
283  speex_encoder_destroy(s->enc_state);
284 
285  av_freep(&avctx->coded_frame);
286  av_freep(&avctx->extradata);
287 
288  return 0;
289 }
290 
291 #define OFFSET(x) offsetof(LibSpeexEncContext, x)
292 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
293 static const AVOption options[] = {
294  { "abr", "Use average bit rate", OFFSET(abr), AV_OPT_TYPE_INT, { 0 }, 0, 1, AE },
295  { "cbr_quality", "Set quality value (0 to 10) for CBR", OFFSET(cbr_quality), AV_OPT_TYPE_INT, { 8 }, 0, 10, AE },
296  { "frames_per_packet", "Number of frames to encode in each packet", OFFSET(frames_per_packet), AV_OPT_TYPE_INT, { 1 }, 1, 8, AE },
297  { NULL },
298 };
299 
300 static const AVClass class = {
301  .class_name = "libspeex",
302  .item_name = av_default_item_name,
303  .option = options,
305 };
306 
307 static const AVCodecDefault defaults[] = {
308  { "b", "0" },
309  { "compression_level", "3" },
310  { NULL },
311 };
312 
314  .name = "libspeex",
315  .type = AVMEDIA_TYPE_AUDIO,
316  .id = CODEC_ID_SPEEX,
317  .priv_data_size = sizeof(LibSpeexEncContext),
318  .init = encode_init,
319  .encode = encode_frame,
320  .close = encode_close,
321  .capabilities = CODEC_CAP_DELAY,
322  .sample_fmts = (const enum SampleFormat[]){ AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
323  .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"),
324  .priv_class = &class,
325  .defaults = defaults,
326 };
int pkt_frame_count
frame count for the current packet
Definition: libspeexenc.c:84
#define FF_COMPRESSION_DEFAULT
Definition: avcodec.h:2770
static av_cold int encode_init(AVCodecContext *avctx)
Definition: libspeexenc.c:122
static short * samples
Definition: ffmpeg.c:233
AVOption.
Definition: opt.h:244
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2000
static int encode_frame(AVCodecContext *avctx, uint8_t *frame, int buf_size, void *data)
Definition: libspeexenc.c:234
AVOptions.
int cbr_quality
CBR quality 0 to 10.
Definition: libspeexenc.c:82
SpeexBits bits
libspeex bitwriter context
Definition: libspeexenc.c:77
static av_cold int encode_close(AVCodecContext *avctx)
Definition: libspeexenc.c:278
signed 16 bits
Definition: samplefmt.h:30
AVCodec.
Definition: avcodec.h:3189
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1398
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:147
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
#define av_cold
Definition: attributes.h:71
int lookahead
encoder delay
Definition: libspeexenc.c:85
int64_t pts
presentation timestamp in time_base units (time when frame should be shown to user) If AV_NOPTS_VALUE...
Definition: avcodec.h:1037
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1387
AVCodec ff_libspeex_encoder
Definition: libspeexenc.c:313
const char data[16]
Definition: mxf.c:60
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:132
int frames_per_packet
number of frames to encode in each packet
Definition: libspeexenc.c:80
static const AVCodecDefault defaults[]
Definition: libspeexenc.c:307
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1355
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
#define AE
Definition: libspeexenc.c:292
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
Definition: utils.c:618
int bit_rate
the average bitrate
Definition: avcodec.h:1340
int pkt_sample_count
sample count in the current packet
Definition: libspeexenc.c:87
LIBAVUTIL_VERSION_INT
Definition: eval.c:50
int frame_size
Samples per packet, initialized when calling 'init'.
Definition: avcodec.h:1470
NULL
Definition: eval.c:50
void * enc_state
libspeex encoder state
Definition: libspeexenc.c:79
external API header
#define OFFSET(x)
Definition: libspeexenc.c:291
int compression_level
Definition: avcodec.h:2769
int sample_rate
samples per second
Definition: avcodec.h:1456
av_default_item_name
Definition: dnxhdenc.c:43
main external API structure.
Definition: avcodec.h:1329
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:327
#define CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:627
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
int extradata_size
Definition: avcodec.h:1388
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:64
Describe the class of an AVClass context structure.
Definition: log.h:33
rational number numerator/denominator
Definition: rational.h:43
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:497
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:2208
common internal api header.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:125
static av_cold void print_enc_params(AVCodecContext *avctx, LibSpeexEncContext *s)
Definition: libspeexenc.c:90
SpeexHeader header
libspeex header struct
Definition: libspeexenc.c:78
static const AVOption options[]
Definition: libspeexenc.c:293
void * priv_data
Definition: avcodec.h:1531
int channels
number of audio channels
Definition: avcodec.h:1457
int abr
flag to enable ABR
Definition: libspeexenc.c:83
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:250
int64_t next_pts
next pts, in sample_rate time base
Definition: libspeexenc.c:86
#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:750
float vbr_quality
VBR quality 0.0 to 10.0.
Definition: libspeexenc.c:81