- // reset the bit-rate to the actual bit rate speex will use
- speex_encoder_ctl(s->enc_state, SPEEX_GET_BITRATE, &s->header.bitrate);
- avctx->bit_rate = s->header.bitrate;
-
- // get the actual sample rate
- speex_encoder_ctl(s->enc_state, SPEEX_GET_SAMPLING_RATE, &s->header.rate);
- avctx->sample_rate = s->header.rate;
-
- // get the frame-size. To align with FLV, we're going to put 2 frames
- // per packet. If someone can tell me how to make this configurable
- // from the avcodec contents, I'll mod this so it's not hard-coded.
- // but without this, FLV files with speex data won't play correctly
- // in flash player 10.
- speex_encoder_ctl(s->enc_state, SPEEX_GET_FRAME_SIZE, &s->header.frame_size);
- s->header.frames_per_packet = 2; // Need for FLV container support
- avctx->frame_size = s->header.frame_size*s->header.frames_per_packet;
-
- // and we'll put a speex header packet into extradata so that muxers
- // can use it.
- avctx->extradata = speex_header_to_packet(&s->header, &avctx->extradata_size);
+
+ /* set encoding complexity */
+ if (avctx->compression_level > FF_COMPRESSION_DEFAULT) {
+ complexity = av_clip(avctx->compression_level, 0, 10);
+ speex_encoder_ctl(s->enc_state, SPEEX_SET_COMPLEXITY, &complexity);
+ }
+ speex_encoder_ctl(s->enc_state, SPEEX_GET_COMPLEXITY, &complexity);
+ avctx->compression_level = complexity;
+
+ /* set packet size */
+ avctx->frame_size = s->header.frame_size;
+ s->header.frames_per_packet = s->frames_per_packet;
+
+ /* set encoding delay */
+ speex_encoder_ctl(s->enc_state, SPEEX_GET_LOOKAHEAD, &s->lookahead);
+ s->next_pts = -s->lookahead;
+
+ /* create header packet bytes from header struct */
+ /* note: libspeex allocates the memory for header_data, which is freed
+ below with speex_header_free() */
+ header_data = speex_header_to_packet(&s->header, &header_size);
+
+ /* allocate extradata and coded_frame */
+ avctx->extradata = av_malloc(header_size + FF_INPUT_BUFFER_PADDING_SIZE);
+ avctx->coded_frame = avcodec_alloc_frame();
+ if (!avctx->extradata || !avctx->coded_frame) {
+ speex_header_free(header_data);
+ speex_encoder_destroy(s->enc_state);
+ av_log(avctx, AV_LOG_ERROR, "memory allocation error\n");
+ return AVERROR(ENOMEM);
+ }
+
+ /* copy header packet to extradata */
+ memcpy(avctx->extradata, header_data, header_size);
+ avctx->extradata_size = header_size;
+ speex_header_free(header_data);
+
+ /* init libspeex bitwriter */
+ speex_bits_init(&s->bits);
+
+ print_enc_params(avctx, s);