00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00027 #include <vorbis/vorbisenc.h>
00028
00029 #include "libavutil/opt.h"
00030 #include "avcodec.h"
00031 #include "bytestream.h"
00032 #include "vorbis.h"
00033
00034 #undef NDEBUG
00035 #include <assert.h>
00036
00037 #define OGGVORBIS_FRAME_SIZE 64
00038
00039 #define BUFFER_SIZE (1024*64)
00040
00041 typedef struct OggVorbisContext {
00042 AVClass *av_class;
00043 vorbis_info vi ;
00044 vorbis_dsp_state vd ;
00045 vorbis_block vb ;
00046 uint8_t buffer[BUFFER_SIZE];
00047 int buffer_index;
00048 int eof;
00049
00050
00051 vorbis_comment vc ;
00052 ogg_packet op;
00053
00054 double iblock;
00055 } OggVorbisContext ;
00056
00057 static const AVOption options[]={
00058 {"iblock", "Sets the impulse block bias", offsetof(OggVorbisContext, iblock), FF_OPT_TYPE_DOUBLE, 0, -15, 0, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_ENCODING_PARAM},
00059 {NULL}
00060 };
00061 static const AVClass class = { "libvorbis", av_default_item_name, options, LIBAVUTIL_VERSION_INT };
00062
00063 static av_cold int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) {
00064 OggVorbisContext *context = avccontext->priv_data ;
00065 double cfreq;
00066
00067 if(avccontext->flags & CODEC_FLAG_QSCALE) {
00068
00069 if(vorbis_encode_setup_vbr(vi, avccontext->channels,
00070 avccontext->sample_rate,
00071 avccontext->global_quality / (float)FF_QP2LAMBDA / 10.0))
00072 return -1;
00073 } else {
00074 int minrate = avccontext->rc_min_rate > 0 ? avccontext->rc_min_rate : -1;
00075 int maxrate = avccontext->rc_min_rate > 0 ? avccontext->rc_max_rate : -1;
00076
00077
00078 if(vorbis_encode_setup_managed(vi, avccontext->channels,
00079 avccontext->sample_rate, minrate, avccontext->bit_rate, maxrate))
00080 return -1;
00081
00082
00083 if(minrate == -1 && maxrate == -1)
00084 if(vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL))
00085 return -1;
00086 }
00087
00088
00089 if(avccontext->cutoff > 0) {
00090 cfreq = avccontext->cutoff / 1000.0;
00091 if(vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq))
00092 return -1;
00093 }
00094
00095 if(context->iblock){
00096 vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &context->iblock);
00097 }
00098
00099 return vorbis_encode_setup_init(vi);
00100 }
00101
00102
00103 static int xiph_len(int l) { return (1 + l / 255 + l); }
00104
00105 static av_cold int oggvorbis_encode_init(AVCodecContext *avccontext) {
00106 OggVorbisContext *context = avccontext->priv_data ;
00107 ogg_packet header, header_comm, header_code;
00108 uint8_t *p;
00109 unsigned int offset;
00110
00111 vorbis_info_init(&context->vi) ;
00112 if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) {
00113 av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed\n") ;
00114 return -1 ;
00115 }
00116 vorbis_analysis_init(&context->vd, &context->vi) ;
00117 vorbis_block_init(&context->vd, &context->vb) ;
00118
00119 vorbis_comment_init(&context->vc);
00120 vorbis_comment_add_tag(&context->vc, "encoder", LIBAVCODEC_IDENT) ;
00121
00122 vorbis_analysis_headerout(&context->vd, &context->vc, &header,
00123 &header_comm, &header_code);
00124
00125 avccontext->extradata_size=
00126 1 + xiph_len(header.bytes) + xiph_len(header_comm.bytes) +
00127 header_code.bytes;
00128 p = avccontext->extradata =
00129 av_malloc(avccontext->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00130 p[0] = 2;
00131 offset = 1;
00132 offset += av_xiphlacing(&p[offset], header.bytes);
00133 offset += av_xiphlacing(&p[offset], header_comm.bytes);
00134 memcpy(&p[offset], header.packet, header.bytes);
00135 offset += header.bytes;
00136 memcpy(&p[offset], header_comm.packet, header_comm.bytes);
00137 offset += header_comm.bytes;
00138 memcpy(&p[offset], header_code.packet, header_code.bytes);
00139 offset += header_code.bytes;
00140 assert(offset == avccontext->extradata_size);
00141
00142
00143
00144
00145 vorbis_comment_clear(&context->vc);
00146
00147 avccontext->frame_size = OGGVORBIS_FRAME_SIZE ;
00148
00149 avccontext->coded_frame= avcodec_alloc_frame();
00150 avccontext->coded_frame->key_frame= 1;
00151
00152 return 0 ;
00153 }
00154
00155
00156 static int oggvorbis_encode_frame(AVCodecContext *avccontext,
00157 unsigned char *packets,
00158 int buf_size, void *data)
00159 {
00160 OggVorbisContext *context = avccontext->priv_data ;
00161 ogg_packet op ;
00162 signed short *audio = data ;
00163 int l;
00164
00165 if(data) {
00166 const int samples = avccontext->frame_size;
00167 float **buffer ;
00168 int c, channels = context->vi.channels;
00169
00170 buffer = vorbis_analysis_buffer(&context->vd, samples) ;
00171 for (c = 0; c < channels; c++) {
00172 int co = (channels > 8) ? c :
00173 ff_vorbis_encoding_channel_layout_offsets[channels-1][c];
00174 for(l = 0 ; l < samples ; l++)
00175 buffer[c][l]=audio[l*channels+co]/32768.f;
00176 }
00177 vorbis_analysis_wrote(&context->vd, samples) ;
00178 } else {
00179 if(!context->eof)
00180 vorbis_analysis_wrote(&context->vd, 0) ;
00181 context->eof = 1;
00182 }
00183
00184 while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
00185 vorbis_analysis(&context->vb, NULL);
00186 vorbis_bitrate_addblock(&context->vb) ;
00187
00188 while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
00189
00190
00191 if(op.bytes==1 && op.e_o_s)
00192 continue;
00193 if (context->buffer_index + sizeof(ogg_packet) + op.bytes > BUFFER_SIZE) {
00194 av_log(avccontext, AV_LOG_ERROR, "libvorbis: buffer overflow.");
00195 return -1;
00196 }
00197 memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet));
00198 context->buffer_index += sizeof(ogg_packet);
00199 memcpy(context->buffer + context->buffer_index, op.packet, op.bytes);
00200 context->buffer_index += op.bytes;
00201
00202 }
00203 }
00204
00205 l=0;
00206 if(context->buffer_index){
00207 ogg_packet *op2= (ogg_packet*)context->buffer;
00208 op2->packet = context->buffer + sizeof(ogg_packet);
00209
00210 l= op2->bytes;
00211 avccontext->coded_frame->pts= av_rescale_q(op2->granulepos, (AVRational){1, avccontext->sample_rate}, avccontext->time_base);
00212
00213
00214 if (l > buf_size) {
00215 av_log(avccontext, AV_LOG_ERROR, "libvorbis: buffer overflow.");
00216 return -1;
00217 }
00218
00219 memcpy(packets, op2->packet, l);
00220 context->buffer_index -= l + sizeof(ogg_packet);
00221 memmove(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index);
00222
00223 }
00224
00225 return l;
00226 }
00227
00228
00229 static av_cold int oggvorbis_encode_close(AVCodecContext *avccontext) {
00230 OggVorbisContext *context = avccontext->priv_data ;
00231
00232
00233 vorbis_analysis_wrote(&context->vd, 0) ;
00234
00235 vorbis_block_clear(&context->vb);
00236 vorbis_dsp_clear(&context->vd);
00237 vorbis_info_clear(&context->vi);
00238
00239 av_freep(&avccontext->coded_frame);
00240 av_freep(&avccontext->extradata);
00241
00242 return 0 ;
00243 }
00244
00245
00246 AVCodec ff_libvorbis_encoder = {
00247 "libvorbis",
00248 AVMEDIA_TYPE_AUDIO,
00249 CODEC_ID_VORBIS,
00250 sizeof(OggVorbisContext),
00251 oggvorbis_encode_init,
00252 oggvorbis_encode_frame,
00253 oggvorbis_encode_close,
00254 .capabilities= CODEC_CAP_DELAY,
00255 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
00256 .long_name= NULL_IF_CONFIG_SMALL("libvorbis Vorbis"),
00257 .priv_class= &class,
00258 } ;