Browse code

libspeexenc: add libspeex encoder

Justin Ruggles authored on 2011/10/19 04:16:27
Showing 7 changed files
... ...
@@ -53,6 +53,7 @@ easier to use. The changes are:
53 53
 - lut, lutrgb, and lutyuv filters
54 54
 - boxblur filter
55 55
 - Ut Video decoder
56
+- Speex encoding via libspeex
56 57
 
57 58
 
58 59
 version 0.7:
... ...
@@ -178,7 +178,7 @@ External library support:
178 178
   --enable-libopenjpeg     enable JPEG 2000 decoding via OpenJPEG [no]
179 179
   --enable-librtmp         enable RTMP[E] support via librtmp [no]
180 180
   --enable-libschroedinger enable Dirac support via libschroedinger [no]
181
-  --enable-libspeex        enable Speex decoding via libspeex [no]
181
+  --enable-libspeex        enable Speex support via libspeex [no]
182 182
   --enable-libtheora       enable Theora encoding via libtheora [no]
183 183
   --enable-libvo-aacenc    enable AAC encoding via libvo-aacenc [no]
184 184
   --enable-libvo-amrwbenc  enable AMR-WB encoding via libvo-amrwbenc [no]
... ...
@@ -1406,6 +1406,7 @@ libopenjpeg_decoder_deps="libopenjpeg"
1406 1406
 libschroedinger_decoder_deps="libschroedinger"
1407 1407
 libschroedinger_encoder_deps="libschroedinger"
1408 1408
 libspeex_decoder_deps="libspeex"
1409
+libspeex_encoder_deps="libspeex"
1409 1410
 libtheora_encoder_deps="libtheora"
1410 1411
 libvo_aacenc_encoder_deps="libvo_aacenc"
1411 1412
 libvo_amrwbenc_encoder_deps="libvo_amrwbenc"
... ...
@@ -682,7 +682,7 @@ following image formats are supported:
682 682
     @tab Used in Sierra VMD files.
683 683
 @item Smacker audio          @tab     @tab  X
684 684
 @item SMPTE 302M AES3 audio  @tab     @tab  X
685
-@item Speex                  @tab     @tab  E
685
+@item Speex                  @tab  E  @tab  E
686 686
     @tab supported through external library libspeex
687 687
 @item True Audio (TTA)       @tab     @tab  X
688 688
 @item TrueHD                 @tab     @tab  X
... ...
@@ -576,6 +576,7 @@ OBJS-$(CONFIG_LIBSCHROEDINGER_ENCODER)    += libschroedingerenc.o \
576 576
                                              libschroedinger.o    \
577 577
                                              libdirac_libschro.o
578 578
 OBJS-$(CONFIG_LIBSPEEX_DECODER)           += libspeexdec.o
579
+OBJS-$(CONFIG_LIBSPEEX_ENCODER)           += libspeexenc.o
579 580
 OBJS-$(CONFIG_LIBTHEORA_ENCODER)          += libtheoraenc.o
580 581
 OBJS-$(CONFIG_LIBVO_AACENC_ENCODER)       += libvo-aacenc.o mpeg4audio.o
581 582
 OBJS-$(CONFIG_LIBVO_AMRWBENC_ENCODER)     += libvo-amrwbenc.o
... ...
@@ -370,7 +370,7 @@ void avcodec_register_all(void)
370 370
     REGISTER_DECODER (LIBOPENCORE_AMRWB, libopencore_amrwb);
371 371
     REGISTER_DECODER (LIBOPENJPEG, libopenjpeg);
372 372
     REGISTER_ENCDEC  (LIBSCHROEDINGER, libschroedinger);
373
-    REGISTER_DECODER (LIBSPEEX, libspeex);
373
+    REGISTER_ENCDEC  (LIBSPEEX, libspeex);
374 374
     REGISTER_ENCODER (LIBTHEORA, libtheora);
375 375
     REGISTER_ENCODER (LIBVO_AACENC, libvo_aacenc);
376 376
     REGISTER_ENCODER (LIBVO_AMRWBENC, libvo_amrwbenc);
377 377
new file mode 100644
... ...
@@ -0,0 +1,324 @@
0
+/*
1
+ * Copyright (C) 2009 Justin Ruggles
2
+ * Copyright (c) 2009 Xuggle Incorporated
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
+/**
22
+ * @file
23
+ * libspeex Speex audio encoder
24
+ *
25
+ * Usage Guide
26
+ * This explains the values that need to be set prior to initialization in
27
+ * order to control various encoding parameters.
28
+ *
29
+ * Channels
30
+ *     Speex only supports mono or stereo, so avctx->channels must be set to
31
+ *     1 or 2.
32
+ *
33
+ * Sample Rate / Encoding Mode
34
+ *     Speex has 3 modes, each of which uses a specific sample rate.
35
+ *         narrowband     :  8 kHz
36
+ *         wideband       : 16 kHz
37
+ *         ultra-wideband : 32 kHz
38
+ *     avctx->sample_rate must be set to one of these 3 values.  This will be
39
+ *     used to set the encoding mode.
40
+ *
41
+ * Rate Control
42
+ *     VBR mode is turned on by setting CODEC_FLAG_QSCALE in avctx->flags.
43
+ *     avctx->global_quality is used to set the encoding quality.
44
+ *     For CBR mode, avctx->bit_rate can be used to set the constant bitrate.
45
+ *     Alternatively, the 'cbr_quality' option can be set from 0 to 10 to set
46
+ *     a constant bitrate based on quality.
47
+ *     For ABR mode, set avctx->bit_rate and set the 'abr' option to 1.
48
+ *     Approx. Bitrate Range:
49
+ *         narrowband     : 2400 - 25600 bps
50
+ *         wideband       : 4000 - 43200 bps
51
+ *         ultra-wideband : 4400 - 45200 bps
52
+ *
53
+ * Complexity
54
+ *     Encoding complexity is controlled by setting avctx->compression_level.
55
+ *     The valid range is 0 to 10.  A higher setting gives generally better
56
+ *     quality at the expense of encoding speed.  This does not affect the
57
+ *     bit rate.
58
+ *
59
+ * Frames-per-Packet
60
+ *     The encoder defaults to using 1 frame-per-packet.  However, it is
61
+ *     sometimes desirable to use multiple frames-per-packet to reduce the
62
+ *     amount of container overhead.  This can be done by setting the
63
+ *     'frames_per_packet' option to a value 1 to 8.
64
+ */
65
+
66
+#include <speex/speex.h>
67
+#include <speex/speex_header.h>
68
+#include <speex/speex_stereo.h>
69
+#include "libavutil/mathematics.h"
70
+#include "libavutil/opt.h"
71
+#include "avcodec.h"
72
+#include "internal.h"
73
+
74
+typedef struct {
75
+    AVClass *class;             ///< AVClass for private options
76
+    SpeexBits bits;             ///< libspeex bitwriter context
77
+    SpeexHeader header;         ///< libspeex header struct
78
+    void *enc_state;            ///< libspeex encoder state
79
+    int frames_per_packet;      ///< number of frames to encode in each packet
80
+    float vbr_quality;          ///< VBR quality 0.0 to 10.0
81
+    int cbr_quality;            ///< CBR quality 0 to 10
82
+    int abr;                    ///< flag to enable ABR
83
+    int pkt_frame_count;        ///< frame count for the current packet
84
+    int lookahead;              ///< encoder delay
85
+    int sample_count;           ///< total sample count (used for pts)
86
+} LibSpeexEncContext;
87
+
88
+static av_cold void print_enc_params(AVCodecContext *avctx,
89
+                                     LibSpeexEncContext *s)
90
+{
91
+    const char *mode_str = "unknown";
92
+
93
+    av_log(avctx, AV_LOG_DEBUG, "channels: %d\n", avctx->channels);
94
+    switch (s->header.mode) {
95
+    case SPEEX_MODEID_NB:  mode_str = "narrowband";     break;
96
+    case SPEEX_MODEID_WB:  mode_str = "wideband";       break;
97
+    case SPEEX_MODEID_UWB: mode_str = "ultra-wideband"; break;
98
+    }
99
+    av_log(avctx, AV_LOG_DEBUG, "mode: %s\n", mode_str);
100
+    if (s->header.vbr) {
101
+        av_log(avctx, AV_LOG_DEBUG, "rate control: VBR\n");
102
+        av_log(avctx, AV_LOG_DEBUG, "  quality: %f\n", s->vbr_quality);
103
+    } else if (s->abr) {
104
+        av_log(avctx, AV_LOG_DEBUG, "rate control: ABR\n");
105
+        av_log(avctx, AV_LOG_DEBUG, "  bitrate: %d bps\n", avctx->bit_rate);
106
+    } else {
107
+        av_log(avctx, AV_LOG_DEBUG, "rate control: CBR\n");
108
+        av_log(avctx, AV_LOG_DEBUG, "  bitrate: %d bps\n", avctx->bit_rate);
109
+    }
110
+    av_log(avctx, AV_LOG_DEBUG, "complexity: %d\n",
111
+           avctx->compression_level);
112
+    av_log(avctx, AV_LOG_DEBUG, "frame size: %d samples\n",
113
+           avctx->frame_size);
114
+    av_log(avctx, AV_LOG_DEBUG, "frames per packet: %d\n",
115
+           s->frames_per_packet);
116
+    av_log(avctx, AV_LOG_DEBUG, "packet size: %d\n",
117
+           avctx->frame_size * s->frames_per_packet);
118
+}
119
+
120
+static av_cold int encode_init(AVCodecContext *avctx)
121
+{
122
+    LibSpeexEncContext *s = avctx->priv_data;
123
+    const SpeexMode *mode;
124
+    uint8_t *header_data;
125
+    int header_size;
126
+    int32_t complexity;
127
+
128
+    /* channels */
129
+    if (avctx->channels < 1 || avctx->channels > 2) {
130
+        av_log(avctx, AV_LOG_ERROR, "Invalid channels (%d). Only stereo and "
131
+               "mono are supported\n", avctx->channels);
132
+        return AVERROR(EINVAL);
133
+    }
134
+
135
+    /* sample rate and encoding mode */
136
+    switch (avctx->sample_rate) {
137
+    case  8000: mode = &speex_nb_mode;  break;
138
+    case 16000: mode = &speex_wb_mode;  break;
139
+    case 32000: mode = &speex_uwb_mode; break;
140
+    default:
141
+        av_log(avctx, AV_LOG_ERROR, "Sample rate of %d Hz is not supported. "
142
+               "Resample to 8, 16, or 32 kHz.\n", avctx->sample_rate);
143
+        return AVERROR(EINVAL);
144
+    }
145
+
146
+    /* initialize libspeex */
147
+    s->enc_state = speex_encoder_init(mode);
148
+    if (!s->enc_state) {
149
+        av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex\n");
150
+        return -1;
151
+    }
152
+    speex_init_header(&s->header, avctx->sample_rate, avctx->channels, mode);
153
+
154
+    /* rate control method and parameters */
155
+    if (avctx->flags & CODEC_FLAG_QSCALE) {
156
+        /* VBR */
157
+        s->header.vbr = 1;
158
+        speex_encoder_ctl(s->enc_state, SPEEX_SET_VBR, &s->header.vbr);
159
+        s->vbr_quality = av_clipf(avctx->global_quality / (float)FF_QP2LAMBDA,
160
+                                  0.0f, 10.0f);
161
+        speex_encoder_ctl(s->enc_state, SPEEX_SET_VBR_QUALITY, &s->vbr_quality);
162
+    } else {
163
+        s->header.bitrate = avctx->bit_rate;
164
+        if (avctx->bit_rate > 0) {
165
+            /* CBR or ABR by bitrate */
166
+            if (s->abr) {
167
+                speex_encoder_ctl(s->enc_state, SPEEX_SET_ABR,
168
+                                  &s->header.bitrate);
169
+                speex_encoder_ctl(s->enc_state, SPEEX_GET_ABR,
170
+                                  &s->header.bitrate);
171
+            } else {
172
+                speex_encoder_ctl(s->enc_state, SPEEX_SET_BITRATE,
173
+                                  &s->header.bitrate);
174
+                speex_encoder_ctl(s->enc_state, SPEEX_GET_BITRATE,
175
+                                  &s->header.bitrate);
176
+            }
177
+        } else {
178
+            /* CBR by quality */
179
+            speex_encoder_ctl(s->enc_state, SPEEX_SET_QUALITY,
180
+                              &s->cbr_quality);
181
+            speex_encoder_ctl(s->enc_state, SPEEX_GET_BITRATE,
182
+                              &s->header.bitrate);
183
+        }
184
+        /* stereo side information adds about 800 bps to the base bitrate */
185
+        /* TODO: this should be calculated exactly */
186
+        avctx->bit_rate = s->header.bitrate + (avctx->channels == 2 ? 800 : 0);
187
+    }
188
+
189
+    /* set encoding complexity */
190
+    if (avctx->compression_level > FF_COMPRESSION_DEFAULT) {
191
+        complexity = av_clip(avctx->compression_level, 0, 10);
192
+        speex_encoder_ctl(s->enc_state, SPEEX_SET_COMPLEXITY, &complexity);
193
+    }
194
+    speex_encoder_ctl(s->enc_state, SPEEX_GET_COMPLEXITY, &complexity);
195
+    avctx->compression_level = complexity;
196
+
197
+    /* set packet size */
198
+    avctx->frame_size = s->header.frame_size;
199
+    s->header.frames_per_packet = s->frames_per_packet;
200
+
201
+    /* set encoding delay */
202
+    speex_encoder_ctl(s->enc_state, SPEEX_GET_LOOKAHEAD, &s->lookahead);
203
+    s->sample_count = -s->lookahead;
204
+
205
+    /* create header packet bytes from header struct */
206
+    /* note: libspeex allocates the memory for header_data, which is freed
207
+             below with speex_header_free() */
208
+    header_data = speex_header_to_packet(&s->header, &header_size);
209
+
210
+    /* allocate extradata and coded_frame */
211
+    avctx->extradata   = av_malloc(header_size + FF_INPUT_BUFFER_PADDING_SIZE);
212
+    avctx->coded_frame = avcodec_alloc_frame();
213
+    if (!avctx->extradata || !avctx->coded_frame) {
214
+        speex_header_free(header_data);
215
+        speex_encoder_destroy(s->enc_state);
216
+        av_log(avctx, AV_LOG_ERROR, "memory allocation error\n");
217
+        return AVERROR(ENOMEM);
218
+    }
219
+
220
+    /* copy header packet to extradata */
221
+    memcpy(avctx->extradata, header_data, header_size);
222
+    avctx->extradata_size = header_size;
223
+    speex_header_free(header_data);
224
+
225
+    /* init libspeex bitwriter */
226
+    speex_bits_init(&s->bits);
227
+
228
+    print_enc_params(avctx, s);
229
+    return 0;
230
+}
231
+
232
+static int encode_frame(AVCodecContext *avctx, uint8_t *frame, int buf_size,
233
+                        void *data)
234
+{
235
+    LibSpeexEncContext *s = avctx->priv_data;
236
+    int16_t *samples      = data;
237
+    int sample_count      = s->sample_count;
238
+
239
+    if (data) {
240
+        /* encode Speex frame */
241
+        if (avctx->channels == 2)
242
+            speex_encode_stereo_int(samples, s->header.frame_size, &s->bits);
243
+        speex_encode_int(s->enc_state, samples, &s->bits);
244
+        s->pkt_frame_count++;
245
+        s->sample_count += avctx->frame_size;
246
+    } else {
247
+        /* handle end-of-stream */
248
+        if (!s->pkt_frame_count)
249
+            return 0;
250
+        /* add extra terminator codes for unused frames in last packet */
251
+        while (s->pkt_frame_count < s->frames_per_packet) {
252
+            speex_bits_pack(&s->bits, 15, 5);
253
+            s->pkt_frame_count++;
254
+        }
255
+    }
256
+
257
+    /* write output if all frames for the packet have been encoded */
258
+    if (s->pkt_frame_count == s->frames_per_packet) {
259
+        s->pkt_frame_count = 0;
260
+        avctx->coded_frame->pts =
261
+            av_rescale_q(sample_count, (AVRational){ 1, avctx->sample_rate },
262
+                         avctx->time_base);
263
+        if (buf_size > speex_bits_nbytes(&s->bits)) {
264
+            int ret = speex_bits_write(&s->bits, frame, buf_size);
265
+            speex_bits_reset(&s->bits);
266
+            return ret;
267
+        } else {
268
+            av_log(avctx, AV_LOG_ERROR, "output buffer too small");
269
+            return AVERROR(EINVAL);
270
+        }
271
+    }
272
+    return 0;
273
+}
274
+
275
+static av_cold int encode_close(AVCodecContext *avctx)
276
+{
277
+    LibSpeexEncContext *s = avctx->priv_data;
278
+
279
+    speex_bits_destroy(&s->bits);
280
+    speex_encoder_destroy(s->enc_state);
281
+
282
+    av_freep(&avctx->coded_frame);
283
+    av_freep(&avctx->extradata);
284
+
285
+    return 0;
286
+}
287
+
288
+#define OFFSET(x) offsetof(LibSpeexEncContext, x)
289
+#define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
290
+static const AVOption options[] = {
291
+    { "abr",               "Use average bit rate",                      OFFSET(abr),               AV_OPT_TYPE_INT, { 0 }, 0,   1, AE },
292
+    { "cbr_quality",       "Set quality value (0 to 10) for CBR",       OFFSET(cbr_quality),       AV_OPT_TYPE_INT, { 8 }, 0,  10, AE },
293
+    { "frames_per_packet", "Number of frames to encode in each packet", OFFSET(frames_per_packet), AV_OPT_TYPE_INT, { 1 }, 1,   8, AE },
294
+    { NULL },
295
+};
296
+
297
+static const AVClass class = {
298
+    .class_name = "libspeex",
299
+    .item_name  = av_default_item_name,
300
+    .option     = options,
301
+    .version    = LIBAVUTIL_VERSION_INT,
302
+};
303
+
304
+static const AVCodecDefault defaults[] = {
305
+    { "b",                 "0" },
306
+    { "compression_level", "3" },
307
+    { NULL },
308
+};
309
+
310
+AVCodec ff_libspeex_encoder = {
311
+    .name           = "libspeex",
312
+    .type           = AVMEDIA_TYPE_AUDIO,
313
+    .id             = CODEC_ID_SPEEX,
314
+    .priv_data_size = sizeof(LibSpeexEncContext),
315
+    .init           = encode_init,
316
+    .encode         = encode_frame,
317
+    .close          = encode_close,
318
+    .capabilities   = CODEC_CAP_DELAY,
319
+    .sample_fmts    = (const enum SampleFormat[]){ AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
320
+    .long_name      = NULL_IF_CONFIG_SMALL("libspeex Speex"),
321
+    .priv_class     = &class,
322
+    .defaults       = defaults,
323
+};
... ...
@@ -21,7 +21,7 @@
21 21
 #define AVCODEC_VERSION_H
22 22
 
23 23
 #define LIBAVCODEC_VERSION_MAJOR 53
24
-#define LIBAVCODEC_VERSION_MINOR  13
24
+#define LIBAVCODEC_VERSION_MINOR  14
25 25
 #define LIBAVCODEC_VERSION_MICRO  0
26 26
 
27 27
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \