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