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