| 145 | } |
| 146 | |
| 147 | static av_cold int encode_init(AVCodecContext *avctx) |
| 148 | { |
| 149 | LibSpeexEncContext *s = avctx->priv_data; |
| 150 | int channels = avctx->ch_layout.nb_channels; |
| 151 | const SpeexMode *mode; |
| 152 | uint8_t *header_data; |
| 153 | int header_size; |
| 154 | int32_t complexity; |
| 155 | |
| 156 | /* sample rate and encoding mode */ |
| 157 | switch (avctx->sample_rate) { |
| 158 | case 8000: mode = speex_lib_get_mode(SPEEX_MODEID_NB); break; |
| 159 | case 16000: mode = speex_lib_get_mode(SPEEX_MODEID_WB); break; |
| 160 | case 32000: mode = speex_lib_get_mode(SPEEX_MODEID_UWB); break; |
| 161 | default: |
| 162 | av_log(avctx, AV_LOG_ERROR, "Sample rate of %d Hz is not supported. " |
| 163 | "Resample to 8, 16, or 32 kHz.\n", avctx->sample_rate); |
| 164 | return AVERROR(EINVAL); |
| 165 | } |
| 166 | |
| 167 | /* initialize libspeex */ |
| 168 | s->enc_state = speex_encoder_init(mode); |
| 169 | if (!s->enc_state) { |
| 170 | av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex\n"); |
| 171 | return -1; |
| 172 | } |
| 173 | speex_init_header(&s->header, avctx->sample_rate, channels, mode); |
| 174 | |
| 175 | /* rate control method and parameters */ |
| 176 | if (avctx->flags & AV_CODEC_FLAG_QSCALE) { |
| 177 | /* VBR */ |
| 178 | s->header.vbr = 1; |
| 179 | s->vad = 1; /* VAD is always implicitly activated for VBR */ |
| 180 | speex_encoder_ctl(s->enc_state, SPEEX_SET_VBR, &s->header.vbr); |
| 181 | s->vbr_quality = av_clipf(avctx->global_quality / (float)FF_QP2LAMBDA, |
| 182 | 0.0f, 10.0f); |
| 183 | speex_encoder_ctl(s->enc_state, SPEEX_SET_VBR_QUALITY, &s->vbr_quality); |
| 184 | } else { |
| 185 | s->header.bitrate = avctx->bit_rate; |
| 186 | if (avctx->bit_rate > 0) { |
| 187 | /* CBR or ABR by bitrate */ |
| 188 | if (s->abr) { |
| 189 | speex_encoder_ctl(s->enc_state, SPEEX_SET_ABR, |
| 190 | &s->header.bitrate); |
| 191 | speex_encoder_ctl(s->enc_state, SPEEX_GET_ABR, |
| 192 | &s->header.bitrate); |
| 193 | } else { |
| 194 | speex_encoder_ctl(s->enc_state, SPEEX_SET_BITRATE, |
| 195 | &s->header.bitrate); |
| 196 | speex_encoder_ctl(s->enc_state, SPEEX_GET_BITRATE, |
| 197 | &s->header.bitrate); |
| 198 | } |
| 199 | } else { |
| 200 | /* CBR by quality */ |
| 201 | speex_encoder_ctl(s->enc_state, SPEEX_SET_QUALITY, |
| 202 | &s->cbr_quality); |
| 203 | speex_encoder_ctl(s->enc_state, SPEEX_GET_BITRATE, |
| 204 | &s->header.bitrate); |
nothing calls this directly
no test coverage detected