Libav 0.7.1
|
00001 /* 00002 * copyright (c) 2002 Mark Hills <mark@pogo.org.uk> 00003 * 00004 * This file is part of Libav. 00005 * 00006 * Libav 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 * Libav 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 Libav; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 #include "libavutil/mathematics.h" 00034 00035 #undef NDEBUG 00036 #include <assert.h> 00037 00038 #define OGGVORBIS_FRAME_SIZE 64 00039 00040 #define BUFFER_SIZE (1024*64) 00041 00042 typedef struct OggVorbisContext { 00043 AVClass *av_class; 00044 vorbis_info vi ; 00045 vorbis_dsp_state vd ; 00046 vorbis_block vb ; 00047 uint8_t buffer[BUFFER_SIZE]; 00048 int buffer_index; 00049 int eof; 00050 00051 /* decoder */ 00052 vorbis_comment vc ; 00053 ogg_packet op; 00054 00055 double iblock; 00056 } OggVorbisContext ; 00057 00058 static const AVOption options[]={ 00059 {"iblock", "Sets the impulse block bias", offsetof(OggVorbisContext, iblock), FF_OPT_TYPE_DOUBLE, {.dbl = 0}, -15, 0, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_ENCODING_PARAM}, 00060 {NULL} 00061 }; 00062 static const AVClass class = { "libvorbis", av_default_item_name, options, LIBAVUTIL_VERSION_INT }; 00063 00064 static av_cold int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) { 00065 OggVorbisContext *context = avccontext->priv_data ; 00066 double cfreq; 00067 00068 if(avccontext->flags & CODEC_FLAG_QSCALE) { 00069 /* variable bitrate */ 00070 if(vorbis_encode_setup_vbr(vi, avccontext->channels, 00071 avccontext->sample_rate, 00072 avccontext->global_quality / (float)FF_QP2LAMBDA / 10.0)) 00073 return -1; 00074 } else { 00075 int minrate = avccontext->rc_min_rate > 0 ? avccontext->rc_min_rate : -1; 00076 int maxrate = avccontext->rc_min_rate > 0 ? avccontext->rc_max_rate : -1; 00077 00078 /* constant bitrate */ 00079 if(vorbis_encode_setup_managed(vi, avccontext->channels, 00080 avccontext->sample_rate, minrate, avccontext->bit_rate, maxrate)) 00081 return -1; 00082 00083 /* variable bitrate by estimate, disable slow rate management */ 00084 if(minrate == -1 && maxrate == -1) 00085 if(vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL)) 00086 return -1; 00087 } 00088 00089 /* cutoff frequency */ 00090 if(avccontext->cutoff > 0) { 00091 cfreq = avccontext->cutoff / 1000.0; 00092 if(vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq)) 00093 return -1; 00094 } 00095 00096 if(context->iblock){ 00097 vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &context->iblock); 00098 } 00099 00100 return vorbis_encode_setup_init(vi); 00101 } 00102 00103 /* How many bytes are needed for a buffer of length 'l' */ 00104 static int xiph_len(int l) { return (1 + l / 255 + l); } 00105 00106 static av_cold int oggvorbis_encode_init(AVCodecContext *avccontext) { 00107 OggVorbisContext *context = avccontext->priv_data ; 00108 ogg_packet header, header_comm, header_code; 00109 uint8_t *p; 00110 unsigned int offset; 00111 00112 vorbis_info_init(&context->vi) ; 00113 if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) { 00114 av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed\n") ; 00115 return -1 ; 00116 } 00117 vorbis_analysis_init(&context->vd, &context->vi) ; 00118 vorbis_block_init(&context->vd, &context->vb) ; 00119 00120 vorbis_comment_init(&context->vc); 00121 vorbis_comment_add_tag(&context->vc, "encoder", LIBAVCODEC_IDENT) ; 00122 00123 vorbis_analysis_headerout(&context->vd, &context->vc, &header, 00124 &header_comm, &header_code); 00125 00126 avccontext->extradata_size= 00127 1 + xiph_len(header.bytes) + xiph_len(header_comm.bytes) + 00128 header_code.bytes; 00129 p = avccontext->extradata = 00130 av_malloc(avccontext->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); 00131 p[0] = 2; 00132 offset = 1; 00133 offset += av_xiphlacing(&p[offset], header.bytes); 00134 offset += av_xiphlacing(&p[offset], header_comm.bytes); 00135 memcpy(&p[offset], header.packet, header.bytes); 00136 offset += header.bytes; 00137 memcpy(&p[offset], header_comm.packet, header_comm.bytes); 00138 offset += header_comm.bytes; 00139 memcpy(&p[offset], header_code.packet, header_code.bytes); 00140 offset += header_code.bytes; 00141 assert(offset == avccontext->extradata_size); 00142 00143 /* vorbis_block_clear(&context->vb); 00144 vorbis_dsp_clear(&context->vd); 00145 vorbis_info_clear(&context->vi);*/ 00146 vorbis_comment_clear(&context->vc); 00147 00148 avccontext->frame_size = OGGVORBIS_FRAME_SIZE ; 00149 00150 avccontext->coded_frame= avcodec_alloc_frame(); 00151 avccontext->coded_frame->key_frame= 1; 00152 00153 return 0 ; 00154 } 00155 00156 00157 static int oggvorbis_encode_frame(AVCodecContext *avccontext, 00158 unsigned char *packets, 00159 int buf_size, void *data) 00160 { 00161 OggVorbisContext *context = avccontext->priv_data ; 00162 ogg_packet op ; 00163 signed short *audio = data ; 00164 int l; 00165 00166 if(data) { 00167 const int samples = avccontext->frame_size; 00168 float **buffer ; 00169 int c, channels = context->vi.channels; 00170 00171 buffer = vorbis_analysis_buffer(&context->vd, samples) ; 00172 for (c = 0; c < channels; c++) { 00173 int co = (channels > 8) ? c : 00174 ff_vorbis_encoding_channel_layout_offsets[channels-1][c]; 00175 for(l = 0 ; l < samples ; l++) 00176 buffer[c][l]=audio[l*channels+co]/32768.f; 00177 } 00178 vorbis_analysis_wrote(&context->vd, samples) ; 00179 } else { 00180 if(!context->eof) 00181 vorbis_analysis_wrote(&context->vd, 0) ; 00182 context->eof = 1; 00183 } 00184 00185 while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) { 00186 vorbis_analysis(&context->vb, NULL); 00187 vorbis_bitrate_addblock(&context->vb) ; 00188 00189 while(vorbis_bitrate_flushpacket(&context->vd, &op)) { 00190 /* i'd love to say the following line is a hack, but sadly it's 00191 * not, apparently the end of stream decision is in libogg. */ 00192 if(op.bytes==1 && op.e_o_s) 00193 continue; 00194 if (context->buffer_index + sizeof(ogg_packet) + op.bytes > BUFFER_SIZE) { 00195 av_log(avccontext, AV_LOG_ERROR, "libvorbis: buffer overflow."); 00196 return -1; 00197 } 00198 memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet)); 00199 context->buffer_index += sizeof(ogg_packet); 00200 memcpy(context->buffer + context->buffer_index, op.packet, op.bytes); 00201 context->buffer_index += op.bytes; 00202 // av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes); 00203 } 00204 } 00205 00206 l=0; 00207 if(context->buffer_index){ 00208 ogg_packet *op2= (ogg_packet*)context->buffer; 00209 op2->packet = context->buffer + sizeof(ogg_packet); 00210 00211 l= op2->bytes; 00212 avccontext->coded_frame->pts= av_rescale_q(op2->granulepos, (AVRational){1, avccontext->sample_rate}, avccontext->time_base); 00213 //FIXME we should reorder the user supplied pts and not assume that they are spaced by 1/sample_rate 00214 00215 if (l > buf_size) { 00216 av_log(avccontext, AV_LOG_ERROR, "libvorbis: buffer overflow."); 00217 return -1; 00218 } 00219 00220 memcpy(packets, op2->packet, l); 00221 context->buffer_index -= l + sizeof(ogg_packet); 00222 memmove(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index); 00223 // av_log(avccontext, AV_LOG_DEBUG, "E%d\n", l); 00224 } 00225 00226 return l; 00227 } 00228 00229 00230 static av_cold int oggvorbis_encode_close(AVCodecContext *avccontext) { 00231 OggVorbisContext *context = avccontext->priv_data ; 00232 /* ogg_packet op ; */ 00233 00234 vorbis_analysis_wrote(&context->vd, 0) ; /* notify vorbisenc this is EOF */ 00235 00236 vorbis_block_clear(&context->vb); 00237 vorbis_dsp_clear(&context->vd); 00238 vorbis_info_clear(&context->vi); 00239 00240 av_freep(&avccontext->coded_frame); 00241 av_freep(&avccontext->extradata); 00242 00243 return 0 ; 00244 } 00245 00246 00247 AVCodec ff_libvorbis_encoder = { 00248 "libvorbis", 00249 AVMEDIA_TYPE_AUDIO, 00250 CODEC_ID_VORBIS, 00251 sizeof(OggVorbisContext), 00252 oggvorbis_encode_init, 00253 oggvorbis_encode_frame, 00254 oggvorbis_encode_close, 00255 .capabilities= CODEC_CAP_DELAY, 00256 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE}, 00257 .long_name= NULL_IF_CONFIG_SMALL("libvorbis Vorbis"), 00258 .priv_class= &class, 00259 } ;