00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <vorbis/vorbisenc.h>
00022
00023 #include "avcodec.h"
00024 #include "bytestream.h"
00025
00026 typedef struct OggVorbisDecContext {
00027 AVFrame frame;
00028 vorbis_info vi;
00029 vorbis_dsp_state vd;
00030 vorbis_block vb;
00031 vorbis_comment vc;
00032 ogg_packet op;
00033 } OggVorbisDecContext;
00034
00035 static int oggvorbis_decode_init(AVCodecContext *avccontext) {
00036 OggVorbisDecContext *context = avccontext->priv_data ;
00037 uint8_t *p= avccontext->extradata;
00038 int i, hsizes[3];
00039 unsigned char *headers[3], *extradata = avccontext->extradata;
00040
00041 vorbis_info_init(&context->vi) ;
00042 vorbis_comment_init(&context->vc) ;
00043
00044 if(! avccontext->extradata_size || ! p) {
00045 av_log(avccontext, AV_LOG_ERROR, "vorbis extradata absent\n");
00046 return -1;
00047 }
00048
00049 if(p[0] == 0 && p[1] == 30) {
00050 for(i = 0; i < 3; i++){
00051 hsizes[i] = bytestream_get_be16((const uint8_t **)&p);
00052 headers[i] = p;
00053 p += hsizes[i];
00054 }
00055 } else if(*p == 2) {
00056 unsigned int offset = 1;
00057 p++;
00058 for(i=0; i<2; i++) {
00059 hsizes[i] = 0;
00060 while((*p == 0xFF) && (offset < avccontext->extradata_size)) {
00061 hsizes[i] += 0xFF;
00062 offset++;
00063 p++;
00064 }
00065 if(offset >= avccontext->extradata_size - 1) {
00066 av_log(avccontext, AV_LOG_ERROR,
00067 "vorbis header sizes damaged\n");
00068 return -1;
00069 }
00070 hsizes[i] += *p;
00071 offset++;
00072 p++;
00073 }
00074 hsizes[2] = avccontext->extradata_size - hsizes[0]-hsizes[1]-offset;
00075 #if 0
00076 av_log(avccontext, AV_LOG_DEBUG,
00077 "vorbis header sizes: %d, %d, %d, / extradata_len is %d \n",
00078 hsizes[0], hsizes[1], hsizes[2], avccontext->extradata_size);
00079 #endif
00080 headers[0] = extradata + offset;
00081 headers[1] = extradata + offset + hsizes[0];
00082 headers[2] = extradata + offset + hsizes[0] + hsizes[1];
00083 } else {
00084 av_log(avccontext, AV_LOG_ERROR,
00085 "vorbis initial header len is wrong: %d\n", *p);
00086 return -1;
00087 }
00088
00089 for(i=0; i<3; i++){
00090 context->op.b_o_s= i==0;
00091 context->op.bytes = hsizes[i];
00092 context->op.packet = headers[i];
00093 if(vorbis_synthesis_headerin(&context->vi, &context->vc, &context->op)<0){
00094 av_log(avccontext, AV_LOG_ERROR, "%d. vorbis header damaged\n", i+1);
00095 return -1;
00096 }
00097 }
00098
00099 avccontext->channels = context->vi.channels;
00100 avccontext->sample_rate = context->vi.rate;
00101 avccontext->time_base= (AVRational){1, avccontext->sample_rate};
00102
00103 vorbis_synthesis_init(&context->vd, &context->vi);
00104 vorbis_block_init(&context->vd, &context->vb);
00105
00106 return 0 ;
00107 }
00108
00109
00110 static inline int conv(int samples, float **pcm, char *buf, int channels) {
00111 int i, j;
00112 ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
00113 float *mono ;
00114
00115 for(i = 0 ; i < channels ; i++){
00116 ptr = &data[i];
00117 mono = pcm[i] ;
00118
00119 for(j = 0 ; j < samples ; j++) {
00120 *ptr = av_clip_int16(mono[j] * 32767.f);
00121 ptr += channels;
00122 }
00123 }
00124
00125 return 0 ;
00126 }
00127
00128 static int oggvorbis_decode_frame(AVCodecContext *avccontext, void *data,
00129 int *got_frame_ptr, AVPacket *avpkt)
00130 {
00131 OggVorbisDecContext *context = avccontext->priv_data ;
00132 float **pcm ;
00133 ogg_packet *op= &context->op;
00134 int samples, total_samples, total_bytes;
00135 int ret;
00136 int16_t *output;
00137
00138 if(!avpkt->size){
00139
00140 return 0;
00141 }
00142
00143 context->frame.nb_samples = 8192*4;
00144 if ((ret = avccontext->get_buffer(avccontext, &context->frame)) < 0) {
00145 av_log(avccontext, AV_LOG_ERROR, "get_buffer() failed\n");
00146 return ret;
00147 }
00148 output = (int16_t *)context->frame.data[0];
00149
00150
00151 op->packet = avpkt->data;
00152 op->bytes = avpkt->size;
00153
00154
00155
00156
00157
00158
00159
00160 if(vorbis_synthesis(&context->vb, op) == 0)
00161 vorbis_synthesis_blockin(&context->vd, &context->vb) ;
00162
00163 total_samples = 0 ;
00164 total_bytes = 0 ;
00165
00166 while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
00167 conv(samples, pcm, (char*)output + total_bytes, context->vi.channels) ;
00168 total_bytes += samples * 2 * context->vi.channels ;
00169 total_samples += samples ;
00170 vorbis_synthesis_read(&context->vd, samples) ;
00171 }
00172
00173 context->frame.nb_samples = total_samples;
00174 *got_frame_ptr = 1;
00175 *(AVFrame *)data = context->frame;
00176 return avpkt->size;
00177 }
00178
00179
00180 static int oggvorbis_decode_close(AVCodecContext *avccontext) {
00181 OggVorbisDecContext *context = avccontext->priv_data ;
00182
00183 vorbis_info_clear(&context->vi) ;
00184 vorbis_comment_clear(&context->vc) ;
00185
00186 return 0 ;
00187 }
00188
00189
00190 AVCodec ff_libvorbis_decoder = {
00191 .name = "libvorbis",
00192 .type = AVMEDIA_TYPE_AUDIO,
00193 .id = AV_CODEC_ID_VORBIS,
00194 .priv_data_size = sizeof(OggVorbisDecContext),
00195 .init = oggvorbis_decode_init,
00196 .decode = oggvorbis_decode_frame,
00197 .close = oggvorbis_decode_close,
00198 .capabilities = CODEC_CAP_DELAY,
00199 .long_name = NULL_IF_CONFIG_SMALL("libvorbis"),
00200 };