• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/libvorbis.c

Go to the documentation of this file.
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), AV_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 {
00066     OggVorbisContext *context = avccontext->priv_data;
00067     double cfreq;
00068 
00069     if (avccontext->flags & CODEC_FLAG_QSCALE) {
00070         /* variable bitrate */
00071         if (vorbis_encode_setup_vbr(vi, avccontext->channels,
00072                                     avccontext->sample_rate,
00073                                     avccontext->global_quality / (float)FF_QP2LAMBDA / 10.0))
00074             return -1;
00075     } else {
00076         int minrate = avccontext->rc_min_rate > 0 ? avccontext->rc_min_rate : -1;
00077         int maxrate = avccontext->rc_min_rate > 0 ? avccontext->rc_max_rate : -1;
00078 
00079         /* constant bitrate */
00080         if (vorbis_encode_setup_managed(vi, avccontext->channels,
00081                                         avccontext->sample_rate, minrate,
00082                                         avccontext->bit_rate, maxrate))
00083             return -1;
00084 
00085         /* variable bitrate by estimate, disable slow rate management */
00086         if (minrate == -1 && maxrate == -1)
00087             if (vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL))
00088                 return -1;
00089     }
00090 
00091     /* cutoff frequency */
00092     if (avccontext->cutoff > 0) {
00093         cfreq = avccontext->cutoff / 1000.0;
00094         if (vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq))
00095             return -1;
00096     }
00097 
00098     if (context->iblock) {
00099         vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &context->iblock);
00100     }
00101 
00102     return vorbis_encode_setup_init(vi);
00103 }
00104 
00105 /* How many bytes are needed for a buffer of length 'l' */
00106 static int xiph_len(int l)
00107 {
00108     return 1 + l / 255 + l;
00109 }
00110 
00111 static av_cold int oggvorbis_encode_init(AVCodecContext *avccontext)
00112 {
00113     OggVorbisContext *context = avccontext->priv_data;
00114     ogg_packet header, header_comm, header_code;
00115     uint8_t *p;
00116     unsigned int offset;
00117 
00118     vorbis_info_init(&context->vi);
00119     if (oggvorbis_init_encoder(&context->vi, avccontext) < 0) {
00120         av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed\n");
00121         return -1;
00122     }
00123     vorbis_analysis_init(&context->vd, &context->vi);
00124     vorbis_block_init(&context->vd, &context->vb);
00125 
00126     vorbis_comment_init(&context->vc);
00127     vorbis_comment_add_tag(&context->vc, "encoder", LIBAVCODEC_IDENT);
00128 
00129     vorbis_analysis_headerout(&context->vd, &context->vc, &header,
00130                               &header_comm, &header_code);
00131 
00132     avccontext->extradata_size =
00133         1 + xiph_len(header.bytes) + xiph_len(header_comm.bytes) +
00134         header_code.bytes;
00135     p = avccontext->extradata =
00136             av_malloc(avccontext->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00137     p[0]    = 2;
00138     offset  = 1;
00139     offset += av_xiphlacing(&p[offset], header.bytes);
00140     offset += av_xiphlacing(&p[offset], header_comm.bytes);
00141     memcpy(&p[offset], header.packet, header.bytes);
00142     offset += header.bytes;
00143     memcpy(&p[offset], header_comm.packet, header_comm.bytes);
00144     offset += header_comm.bytes;
00145     memcpy(&p[offset], header_code.packet, header_code.bytes);
00146     offset += header_code.bytes;
00147     assert(offset == avccontext->extradata_size);
00148 
00149 #if 0
00150     vorbis_block_clear(&context->vb);
00151     vorbis_dsp_clear(&context->vd);
00152     vorbis_info_clear(&context->vi);
00153 #endif
00154     vorbis_comment_clear(&context->vc);
00155 
00156     avccontext->frame_size = OGGVORBIS_FRAME_SIZE;
00157 
00158     avccontext->coded_frame = avcodec_alloc_frame();
00159     avccontext->coded_frame->key_frame = 1;
00160 
00161     return 0;
00162 }
00163 
00164 static int oggvorbis_encode_frame(AVCodecContext *avccontext,
00165                                   unsigned char *packets,
00166                                   int buf_size, void *data)
00167 {
00168     OggVorbisContext *context = avccontext->priv_data;
00169     ogg_packet op;
00170     signed short *audio = data;
00171     int l;
00172 
00173     if (data) {
00174         const int samples = avccontext->frame_size;
00175         float **buffer;
00176         int c, channels = context->vi.channels;
00177 
00178         buffer = vorbis_analysis_buffer(&context->vd, samples);
00179         for (c = 0; c < channels; c++) {
00180             int co = (channels > 8) ? c :
00181                      ff_vorbis_encoding_channel_layout_offsets[channels - 1][c];
00182             for (l = 0; l < samples; l++)
00183                 buffer[c][l] = audio[l * channels + co] / 32768.f;
00184         }
00185         vorbis_analysis_wrote(&context->vd, samples);
00186     } else {
00187         if (!context->eof)
00188             vorbis_analysis_wrote(&context->vd, 0);
00189         context->eof = 1;
00190     }
00191 
00192     while (vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
00193         vorbis_analysis(&context->vb, NULL);
00194         vorbis_bitrate_addblock(&context->vb);
00195 
00196         while (vorbis_bitrate_flushpacket(&context->vd, &op)) {
00197             /* i'd love to say the following line is a hack, but sadly it's
00198              * not, apparently the end of stream decision is in libogg. */
00199             if (op.bytes == 1 && op.e_o_s)
00200                 continue;
00201             if (context->buffer_index + sizeof(ogg_packet) + op.bytes > BUFFER_SIZE) {
00202                 av_log(avccontext, AV_LOG_ERROR, "libvorbis: buffer overflow.");
00203                 return -1;
00204             }
00205             memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet));
00206             context->buffer_index += sizeof(ogg_packet);
00207             memcpy(context->buffer + context->buffer_index, op.packet, op.bytes);
00208             context->buffer_index += op.bytes;
00209 //            av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes);
00210         }
00211     }
00212 
00213     l = 0;
00214     if (context->buffer_index) {
00215         ogg_packet *op2 = (ogg_packet *)context->buffer;
00216         op2->packet = context->buffer + sizeof(ogg_packet);
00217 
00218         l = op2->bytes;
00219         avccontext->coded_frame->pts = av_rescale_q(op2->granulepos, (AVRational) { 1, avccontext->sample_rate }, avccontext->time_base);
00220         //FIXME we should reorder the user supplied pts and not assume that they are spaced by 1/sample_rate
00221 
00222         if (l > buf_size) {
00223             av_log(avccontext, AV_LOG_ERROR, "libvorbis: buffer overflow.");
00224             return -1;
00225         }
00226 
00227         memcpy(packets, op2->packet, l);
00228         context->buffer_index -= l + sizeof(ogg_packet);
00229         memmove(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index);
00230 //        av_log(avccontext, AV_LOG_DEBUG, "E%d\n", l);
00231     }
00232 
00233     return l;
00234 }
00235 
00236 static av_cold int oggvorbis_encode_close(AVCodecContext *avccontext)
00237 {
00238     OggVorbisContext *context = avccontext->priv_data;
00239 /*  ogg_packet op ; */
00240 
00241     vorbis_analysis_wrote(&context->vd, 0);  /* notify vorbisenc this is EOF */
00242 
00243     vorbis_block_clear(&context->vb);
00244     vorbis_dsp_clear(&context->vd);
00245     vorbis_info_clear(&context->vi);
00246 
00247     av_freep(&avccontext->coded_frame);
00248     av_freep(&avccontext->extradata);
00249 
00250     return 0;
00251 }
00252 
00253 AVCodec ff_libvorbis_encoder = {
00254     .name           = "libvorbis",
00255     .type           = AVMEDIA_TYPE_AUDIO,
00256     .id             = CODEC_ID_VORBIS,
00257     .priv_data_size = sizeof(OggVorbisContext),
00258     .init           = oggvorbis_encode_init,
00259     .encode         = oggvorbis_encode_frame,
00260     .close          = oggvorbis_encode_close,
00261     .capabilities   = CODEC_CAP_DELAY,
00262     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
00263     .long_name      = NULL_IF_CONFIG_SMALL("libvorbis Vorbis"),
00264     .priv_class     = &class,
00265 };
Generated on Sun Apr 22 2012 21:54:02 for Libav by doxygen 1.7.1