Libav
|
00001 /* 00002 * Copyright (C) 2008 David Conrad 00003 * 00004 * This file is part of FFmpeg. 00005 * 00006 * FFmpeg is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * FFmpeg is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with FFmpeg; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 00021 #include "avcodec.h" 00022 #include <speex/speex.h> 00023 #include <speex/speex_header.h> 00024 #include <speex/speex_stereo.h> 00025 #include <speex/speex_callbacks.h> 00026 00027 typedef struct { 00028 SpeexBits bits; 00029 SpeexStereoState stereo; 00030 void *dec_state; 00031 SpeexHeader *header; 00032 int frame_size; 00033 } LibSpeexContext; 00034 00035 00036 static av_cold int libspeex_decode_init(AVCodecContext *avctx) 00037 { 00038 LibSpeexContext *s = avctx->priv_data; 00039 const SpeexMode *mode; 00040 00041 // defaults in the case of a missing header 00042 if (avctx->sample_rate <= 8000) 00043 mode = &speex_nb_mode; 00044 else if (avctx->sample_rate <= 16000) 00045 mode = &speex_wb_mode; 00046 else 00047 mode = &speex_uwb_mode; 00048 00049 if (avctx->extradata_size >= 80) 00050 s->header = speex_packet_to_header(avctx->extradata, avctx->extradata_size); 00051 00052 avctx->sample_fmt = SAMPLE_FMT_S16; 00053 if (s->header) { 00054 avctx->sample_rate = s->header->rate; 00055 avctx->channels = s->header->nb_channels; 00056 avctx->frame_size = s->frame_size = s->header->frame_size; 00057 if (s->header->frames_per_packet) 00058 avctx->frame_size *= s->header->frames_per_packet; 00059 00060 mode = speex_lib_get_mode(s->header->mode); 00061 if (!mode) { 00062 av_log(avctx, AV_LOG_ERROR, "Unknown Speex mode %d", s->header->mode); 00063 return -1; 00064 } 00065 } else 00066 av_log(avctx, AV_LOG_INFO, "Missing Speex header, assuming defaults.\n"); 00067 00068 if (avctx->channels > 2) { 00069 av_log(avctx, AV_LOG_ERROR, "Only stereo and mono are supported.\n"); 00070 return -1; 00071 } 00072 00073 speex_bits_init(&s->bits); 00074 s->dec_state = speex_decoder_init(mode); 00075 if (!s->dec_state) { 00076 av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex decoder.\n"); 00077 return -1; 00078 } 00079 00080 if (!s->header) { 00081 speex_decoder_ctl(s->dec_state, SPEEX_GET_FRAME_SIZE, &s->frame_size); 00082 } 00083 00084 if (avctx->channels == 2) { 00085 SpeexCallback callback; 00086 callback.callback_id = SPEEX_INBAND_STEREO; 00087 callback.func = speex_std_stereo_request_handler; 00088 callback.data = &s->stereo; 00089 s->stereo = (SpeexStereoState)SPEEX_STEREO_STATE_INIT; 00090 speex_decoder_ctl(s->dec_state, SPEEX_SET_HANDLER, &callback); 00091 } 00092 return 0; 00093 } 00094 00095 static int libspeex_decode_frame(AVCodecContext *avctx, 00096 void *data, int *data_size, 00097 AVPacket *avpkt) 00098 { 00099 const uint8_t *buf = avpkt->data; 00100 int buf_size = avpkt->size; 00101 LibSpeexContext *s = avctx->priv_data; 00102 int16_t *output = data, *end; 00103 int i, num_samples; 00104 00105 num_samples = s->frame_size * avctx->channels; 00106 end = output + *data_size / sizeof(*output); 00107 00108 speex_bits_read_from(&s->bits, buf, buf_size); 00109 00110 for (i = 0; speex_bits_remaining(&s->bits) && output + num_samples < end; i++) { 00111 int ret = speex_decode_int(s->dec_state, &s->bits, output); 00112 if (ret <= -2) { 00113 av_log(avctx, AV_LOG_ERROR, "Error decoding Speex frame.\n"); 00114 return -1; 00115 } else if (ret == -1) 00116 // end of stream 00117 break; 00118 00119 if (avctx->channels == 2) 00120 speex_decode_stereo_int(output, s->frame_size, &s->stereo); 00121 00122 output += num_samples; 00123 } 00124 00125 avctx->frame_size = s->frame_size * i; 00126 *data_size = avctx->channels * avctx->frame_size * sizeof(*output); 00127 return buf_size; 00128 } 00129 00130 static av_cold int libspeex_decode_close(AVCodecContext *avctx) 00131 { 00132 LibSpeexContext *s = avctx->priv_data; 00133 00134 speex_header_free(s->header); 00135 speex_bits_destroy(&s->bits); 00136 speex_decoder_destroy(s->dec_state); 00137 00138 return 0; 00139 } 00140 00141 AVCodec libspeex_decoder = { 00142 "libspeex", 00143 AVMEDIA_TYPE_AUDIO, 00144 CODEC_ID_SPEEX, 00145 sizeof(LibSpeexContext), 00146 libspeex_decode_init, 00147 NULL, 00148 libspeex_decode_close, 00149 libspeex_decode_frame, 00150 .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"), 00151 };