]> git.sesse.net Git - ffmpeg/blob - libavcodec/libspeexenc.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / libspeexenc.c
1 /*
2  * Copyright (c) 2009 by Xuggle Incorporated.  All rights reserved.
3  * This file is part of FFmpeg.
4  *
5  * FFmpeg is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * FFmpeg is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with FFmpeg; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 #include <libavcodec/avcodec.h>
20 #include <speex/speex.h>
21 #include <speex/speex_header.h>
22 #include <speex/speex_stereo.h>
23
24 typedef struct {
25     SpeexBits bits;
26     void *enc_state;
27     SpeexHeader header;
28 } LibSpeexEncContext;
29
30
31 static av_cold int libspeex_encode_init(AVCodecContext *avctx)
32 {
33     LibSpeexEncContext *s = (LibSpeexEncContext*)avctx->priv_data;
34     const SpeexMode *mode;
35
36     if ((avctx->sample_fmt != SAMPLE_FMT_S16 && avctx->sample_fmt != SAMPLE_FMT_FLT) ||
37             avctx->sample_rate <= 0 ||
38             avctx->channels <= 0 ||
39             avctx->channels > 2)
40     {
41         av_log(avctx, AV_LOG_ERROR, "Unsupported sample format, rate, or channels for speex");
42         return -1;
43     }
44
45     if (avctx->sample_rate <= 8000)
46         mode = &speex_nb_mode;
47     else if (avctx->sample_rate <= 16000)
48         mode = &speex_wb_mode;
49     else
50         mode = &speex_uwb_mode;
51
52     speex_bits_init(&s->bits);
53     s->enc_state = speex_encoder_init(mode);
54     if (!s->enc_state)
55     {
56         av_log(avctx, AV_LOG_ERROR, "could not initialize speex encoder");
57         return -1;
58     }
59
60     // initialize the header
61     speex_init_header(&s->header, avctx->sample_rate,
62             avctx->channels, mode);
63
64     // TODO: It'd be nice to support VBR here, but
65     // I'm uncertain what AVCodecContext options to use
66     // to signal whether to turn it on.
67     if (avctx->flags & CODEC_FLAG_QSCALE) {
68         spx_int32_t quality = 0;
69         // Map global_quality's mpeg 1/2/4 scale into Speex's 0-10 scale
70         if (avctx->global_quality > FF_LAMBDA_MAX)
71             quality = 0; // lowest possible quality
72         else
73             quality = (spx_int32_t)((FF_LAMBDA_MAX-avctx->global_quality)*10.0/FF_LAMBDA_MAX);
74         speex_encoder_ctl(s->enc_state, SPEEX_SET_QUALITY, &quality);
75     } else {
76         // default to CBR
77         if (avctx->bit_rate > 0)
78             speex_encoder_ctl(s->enc_state, SPEEX_SET_BITRATE, &avctx->bit_rate);
79         // otherwise just take the default quality setting
80     }
81     // reset the bit-rate to the actual bit rate speex will use
82     speex_encoder_ctl(s->enc_state, SPEEX_GET_BITRATE, &s->header.bitrate);
83     avctx->bit_rate = s->header.bitrate;
84
85     // get the actual sample rate
86     speex_encoder_ctl(s->enc_state, SPEEX_GET_SAMPLING_RATE, &s->header.rate);
87     avctx->sample_rate = s->header.rate;
88
89     // get the frame-size.  To align with FLV, we're going to put 2 frames
90     // per packet.  If someone can tell me how to make this configurable
91     // from the avcodec contents, I'll mod this so it's not hard-coded.
92     // but without this, FLV files with speex data won't play correctly
93     // in flash player 10.
94     speex_encoder_ctl(s->enc_state, SPEEX_GET_FRAME_SIZE, &s->header.frame_size);
95     s->header.frames_per_packet = 2; // Need for FLV container support
96     avctx->frame_size = s->header.frame_size*s->header.frames_per_packet;
97
98     // and we'll put a speex header packet into extradata so that muxers
99     // can use it.
100     avctx->extradata = speex_header_to_packet(&s->header, &avctx->extradata_size);
101     return 0;
102 }
103
104 static av_cold int libspeex_encode_frame(
105         AVCodecContext *avctx, uint8_t *frame,
106         int buf_size, void *data)
107 {
108     LibSpeexEncContext *s = (LibSpeexEncContext*)avctx->priv_data;
109     int i = 0;
110
111     if (!data)
112         // nothing to flush
113         return 0;
114
115     speex_bits_reset(&s->bits);
116     for(i = 0; i < s->header.frames_per_packet; i++)
117     {
118         if (avctx->sample_fmt == SAMPLE_FMT_FLT)
119         {
120             if (avctx->channels == 2) {
121                 speex_encode_stereo(
122                         (float*)data+i*s->header.frame_size,
123                         s->header.frame_size,
124                         &s->bits);
125             }
126             speex_encode(s->enc_state,
127                     (float*)data+i*s->header.frame_size, &s->bits);
128         } else {
129             if (avctx->channels == 2) {
130                 speex_encode_stereo_int(
131                         (spx_int16_t*)data+i*s->header.frame_size,
132                         s->header.frame_size,
133                         &s->bits);
134             }
135             speex_encode_int(s->enc_state,
136                     (spx_int16_t*)data+i*s->header.frame_size, &s->bits);
137         }
138     }
139     // put in a terminator so this will fit in a OGG or FLV packet
140     speex_bits_insert_terminator(&s->bits);
141
142     if (buf_size >= speex_bits_nbytes(&s->bits)) {
143         return speex_bits_write(&s->bits, frame, buf_size);
144     } else {
145         av_log(avctx, AV_LOG_ERROR, "output buffer too small");
146         return -1;
147     }
148 }
149
150 static av_cold int libspeex_encode_close(AVCodecContext *avctx)
151 {
152     LibSpeexEncContext *s = (LibSpeexEncContext*)avctx->priv_data;
153
154     speex_bits_destroy(&s->bits);
155     speex_encoder_destroy(s->enc_state);
156     s->enc_state = 0;
157     if (avctx->extradata)
158         speex_header_free(avctx->extradata);
159     avctx->extradata = 0;
160     avctx->extradata_size = 0;
161
162     return 0;
163 }
164
165 AVCodec ff_libspeex_encoder = {
166     "libspeex",
167     AVMEDIA_TYPE_AUDIO,
168     CODEC_ID_SPEEX,
169     sizeof(LibSpeexEncContext),
170     libspeex_encode_init,
171     libspeex_encode_frame,
172     libspeex_encode_close,
173     0,
174     .capabilities = CODEC_CAP_DELAY,
175     .supported_samplerates = (const int[]){8000, 16000, 32000, 0},
176     .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_FLT,SAMPLE_FMT_NONE},
177     .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex Encoder"),
178 };