00001
00007 #include <vorbis/vorbisenc.h>
00008
00009 #include "avcodec.h"
00010
00011 #undef NDEBUG
00012 #include <assert.h>
00013
00014 #define OGGVORBIS_FRAME_SIZE 64
00015
00016 #define BUFFER_SIZE (1024*64)
00017
00018 typedef struct OggVorbisContext {
00019 vorbis_info vi ;
00020 vorbis_dsp_state vd ;
00021 vorbis_block vb ;
00022 uint8_t buffer[BUFFER_SIZE];
00023 int buffer_index;
00024
00025
00026 vorbis_comment vc ;
00027 ogg_packet op;
00028 } OggVorbisContext ;
00029
00030
00031 static int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) {
00032
00033 #ifdef OGGVORBIS_VBR_BY_ESTIMATE
00034
00035
00036 return (vorbis_encode_setup_managed(vi, avccontext->channels,
00037 avccontext->sample_rate, -1, avccontext->bit_rate, -1) ||
00038 vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE_AVG, NULL) ||
00039 vorbis_encode_setup_init(vi)) ;
00040 #else
00041
00042
00043 return vorbis_encode_init(vi, avccontext->channels,
00044 avccontext->sample_rate, -1, avccontext->bit_rate, -1) ;
00045 #endif
00046 }
00047
00048 static int oggvorbis_encode_init(AVCodecContext *avccontext) {
00049 OggVorbisContext *context = avccontext->priv_data ;
00050 ogg_packet header, header_comm, header_code;
00051 uint8_t *p;
00052 unsigned int offset, len;
00053
00054 vorbis_info_init(&context->vi) ;
00055 if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) {
00056 av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed") ;
00057 return -1 ;
00058 }
00059 vorbis_analysis_init(&context->vd, &context->vi) ;
00060 vorbis_block_init(&context->vd, &context->vb) ;
00061
00062 vorbis_comment_init(&context->vc);
00063 vorbis_comment_add_tag(&context->vc, "encoder", LIBAVCODEC_IDENT) ;
00064
00065 vorbis_analysis_headerout(&context->vd, &context->vc, &header,
00066 &header_comm, &header_code);
00067
00068 len = header.bytes + header_comm.bytes + header_code.bytes;
00069 avccontext->extradata_size= 64 + len + len/255;
00070 p = avccontext->extradata= av_mallocz(avccontext->extradata_size);
00071 p[0] = 2;
00072 offset = 1;
00073 offset += av_xiphlacing(&p[offset], header.bytes);
00074 offset += av_xiphlacing(&p[offset], header_comm.bytes);
00075 memcpy(&p[offset], header.packet, header.bytes);
00076 offset += header.bytes;
00077 memcpy(&p[offset], header_comm.packet, header_comm.bytes);
00078 offset += header_comm.bytes;
00079 memcpy(&p[offset], header_code.packet, header_code.bytes);
00080 offset += header_code.bytes;
00081 avccontext->extradata_size = offset;
00082 avccontext->extradata= av_realloc(avccontext->extradata, avccontext->extradata_size);
00083
00084
00085
00086
00087 vorbis_comment_clear(&context->vc);
00088
00089 avccontext->frame_size = OGGVORBIS_FRAME_SIZE ;
00090
00091 avccontext->coded_frame= avcodec_alloc_frame();
00092 avccontext->coded_frame->key_frame= 1;
00093
00094 return 0 ;
00095 }
00096
00097
00098 static int oggvorbis_encode_frame(AVCodecContext *avccontext,
00099 unsigned char *packets,
00100 int buf_size, void *data)
00101 {
00102 OggVorbisContext *context = avccontext->priv_data ;
00103 float **buffer ;
00104 ogg_packet op ;
00105 signed short *audio = data ;
00106 int l, samples = data ? OGGVORBIS_FRAME_SIZE : 0;
00107
00108 buffer = vorbis_analysis_buffer(&context->vd, samples) ;
00109
00110 if(context->vi.channels == 1) {
00111 for(l = 0 ; l < samples ; l++)
00112 buffer[0][l]=audio[l]/32768.f;
00113 } else {
00114 for(l = 0 ; l < samples ; l++){
00115 buffer[0][l]=audio[l*2]/32768.f;
00116 buffer[1][l]=audio[l*2+1]/32768.f;
00117 }
00118 }
00119
00120 vorbis_analysis_wrote(&context->vd, samples) ;
00121
00122 while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
00123 vorbis_analysis(&context->vb, NULL);
00124 vorbis_bitrate_addblock(&context->vb) ;
00125
00126 while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
00127 if(op.bytes==1)
00128 continue;
00129 memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet));
00130 context->buffer_index += sizeof(ogg_packet);
00131 memcpy(context->buffer + context->buffer_index, op.packet, op.bytes);
00132 context->buffer_index += op.bytes;
00133
00134 }
00135 }
00136
00137 l=0;
00138 if(context->buffer_index){
00139 ogg_packet *op2= (ogg_packet*)context->buffer;
00140 op2->packet = context->buffer + sizeof(ogg_packet);
00141
00142 l= op2->bytes;
00143 avccontext->coded_frame->pts= op2->granulepos;
00144
00145 memcpy(packets, op2->packet, l);
00146 context->buffer_index -= l + sizeof(ogg_packet);
00147 memcpy(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index);
00148
00149 }
00150
00151 return l;
00152 }
00153
00154
00155 static int oggvorbis_encode_close(AVCodecContext *avccontext) {
00156 OggVorbisContext *context = avccontext->priv_data ;
00157
00158
00159 vorbis_analysis_wrote(&context->vd, 0) ;
00160
00161 vorbis_block_clear(&context->vb);
00162 vorbis_dsp_clear(&context->vd);
00163 vorbis_info_clear(&context->vi);
00164
00165 av_freep(&avccontext->coded_frame);
00166 av_freep(&avccontext->extradata);
00167
00168 return 0 ;
00169 }
00170
00171
00172 AVCodec oggvorbis_encoder = {
00173 "vorbis",
00174 CODEC_TYPE_AUDIO,
00175 CODEC_ID_VORBIS,
00176 sizeof(OggVorbisContext),
00177 oggvorbis_encode_init,
00178 oggvorbis_encode_frame,
00179 oggvorbis_encode_close,
00180 .capabilities= CODEC_CAP_DELAY,
00181 } ;
00182
00183 static int oggvorbis_decode_init(AVCodecContext *avccontext) {
00184 OggVorbisContext *context = avccontext->priv_data ;
00185 uint8_t *p= avccontext->extradata;
00186 int i, hsizes[3];
00187 unsigned char *headers[3], *extradata = avccontext->extradata;
00188
00189 vorbis_info_init(&context->vi) ;
00190 vorbis_comment_init(&context->vc) ;
00191
00192 if(! avccontext->extradata_size || ! p) {
00193 av_log(avccontext, AV_LOG_ERROR, "vorbis extradata absent\n");
00194 return -1;
00195 }
00196
00197 if(p[0] == 0 && p[1] == 30) {
00198 for(i = 0; i < 3; i++){
00199 hsizes[i] = *p++ << 8;
00200 hsizes[i] += *p++;
00201 headers[i] = p;
00202 p += hsizes[i];
00203 }
00204 } else if(*p == 2) {
00205 unsigned int offset = 1;
00206 p++;
00207 for(i=0; i<2; i++) {
00208 hsizes[i] = 0;
00209 while((*p == 0xFF) && (offset < avccontext->extradata_size)) {
00210 hsizes[i] += 0xFF;
00211 offset++;
00212 p++;
00213 }
00214 if(offset >= avccontext->extradata_size - 1) {
00215 av_log(avccontext, AV_LOG_ERROR,
00216 "vorbis header sizes damaged\n");
00217 return -1;
00218 }
00219 hsizes[i] += *p;
00220 offset++;
00221 p++;
00222 }
00223 hsizes[2] = avccontext->extradata_size - hsizes[0]-hsizes[1]-offset;
00224 #if 0
00225 av_log(avccontext, AV_LOG_DEBUG,
00226 "vorbis header sizes: %d, %d, %d, / extradata_len is %d \n",
00227 hsizes[0], hsizes[1], hsizes[2], avccontext->extradata_size);
00228 #endif
00229 headers[0] = extradata + offset;
00230 headers[1] = extradata + offset + hsizes[0];
00231 headers[2] = extradata + offset + hsizes[0] + hsizes[1];
00232 } else {
00233 av_log(avccontext, AV_LOG_ERROR,
00234 "vorbis initial header len is wrong: %d\n", *p);
00235 return -1;
00236 }
00237
00238 for(i=0; i<3; i++){
00239 context->op.b_o_s= i==0;
00240 context->op.bytes = hsizes[i];
00241 context->op.packet = headers[i];
00242 if(vorbis_synthesis_headerin(&context->vi, &context->vc, &context->op)<0){
00243 av_log(avccontext, AV_LOG_ERROR, "%d. vorbis header damaged\n", i+1);
00244 return -1;
00245 }
00246 }
00247
00248 avccontext->channels = context->vi.channels;
00249 avccontext->sample_rate = context->vi.rate;
00250 avccontext->time_base= (AVRational){1, avccontext->sample_rate};
00251
00252 vorbis_synthesis_init(&context->vd, &context->vi);
00253 vorbis_block_init(&context->vd, &context->vb);
00254
00255 return 0 ;
00256 }
00257
00258
00259 static inline int conv(int samples, float **pcm, char *buf, int channels) {
00260 int i, j, val ;
00261 ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
00262 float *mono ;
00263
00264 for(i = 0 ; i < channels ; i++){
00265 ptr = &data[i];
00266 mono = pcm[i] ;
00267
00268 for(j = 0 ; j < samples ; j++) {
00269
00270 val = mono[j] * 32767.f;
00271
00272 if(val > 32767) val = 32767 ;
00273 if(val < -32768) val = -32768 ;
00274
00275 *ptr = val ;
00276 ptr += channels;
00277 }
00278 }
00279
00280 return 0 ;
00281 }
00282
00283
00284 static int oggvorbis_decode_frame(AVCodecContext *avccontext,
00285 void *data, int *data_size,
00286 uint8_t *buf, int buf_size)
00287 {
00288 OggVorbisContext *context = avccontext->priv_data ;
00289 float **pcm ;
00290 ogg_packet *op= &context->op;
00291 int samples, total_samples, total_bytes,i;
00292
00293 if(!buf_size){
00294
00295 return 0;
00296 }
00297
00298 op->packet = buf;
00299 op->bytes = buf_size;
00300
00301
00302
00303
00304
00305
00306
00307 if(vorbis_synthesis(&context->vb, op) == 0)
00308 vorbis_synthesis_blockin(&context->vd, &context->vb) ;
00309
00310 total_samples = 0 ;
00311 total_bytes = 0 ;
00312
00313 while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
00314 conv(samples, pcm, (char*)data + total_bytes, context->vi.channels) ;
00315 total_bytes += samples * 2 * context->vi.channels ;
00316 total_samples += samples ;
00317 vorbis_synthesis_read(&context->vd, samples) ;
00318 }
00319
00320 *data_size = total_bytes ;
00321 return buf_size ;
00322 }
00323
00324
00325 static int oggvorbis_decode_close(AVCodecContext *avccontext) {
00326 OggVorbisContext *context = avccontext->priv_data ;
00327
00328 vorbis_info_clear(&context->vi) ;
00329 vorbis_comment_clear(&context->vc) ;
00330
00331 return 0 ;
00332 }
00333
00334
00335 AVCodec oggvorbis_decoder = {
00336 "vorbis",
00337 CODEC_TYPE_AUDIO,
00338 CODEC_ID_VORBIS,
00339 sizeof(OggVorbisContext),
00340 oggvorbis_decode_init,
00341 NULL,
00342 oggvorbis_decode_close,
00343 oggvorbis_decode_frame,
00344 .capabilities= CODEC_CAP_DELAY,
00345 } ;