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 "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
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 const char * error(int oggerr, int *averr)
00072 {
00073 switch (oggerr) {
00074 case OV_EFAULT: *averr = AVERROR(EFAULT); return "internal error";
00075 case OV_EIMPL: *averr = AVERROR(EINVAL); return "not supported";
00076 case OV_EINVAL: *averr = AVERROR(EINVAL); return "invalid request";
00077 default: *averr = AVERROR(EINVAL); return "unknown error";
00078 }
00079 }
00080
00081 static av_cold int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext)
00082 {
00083 OggVorbisContext *context = avccontext->priv_data;
00084 double cfreq;
00085
00086 if (avccontext->flags & CODEC_FLAG_QSCALE || !avccontext->bit_rate) {
00087
00088
00089
00090
00091 float q = avccontext->global_quality / (float)FF_QP2LAMBDA;
00092
00093 if (!(avccontext->flags & CODEC_FLAG_QSCALE))
00094 q = 3.0;
00095 if (vorbis_encode_setup_vbr(vi, avccontext->channels,
00096 avccontext->sample_rate,
00097 q / 10.0))
00098 return -1;
00099 } else {
00100 int minrate = avccontext->rc_min_rate > 0 ? avccontext->rc_min_rate : -1;
00101 int maxrate = avccontext->rc_max_rate > 0 ? avccontext->rc_max_rate : -1;
00102
00103
00104 if (vorbis_encode_setup_managed(vi, avccontext->channels,
00105 avccontext->sample_rate, maxrate,
00106 avccontext->bit_rate, minrate))
00107 return -1;
00108
00109
00110 if (minrate == -1 && maxrate == -1)
00111 if (vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL))
00112 return AVERROR(EINVAL);
00113 }
00114
00115
00116 if (avccontext->cutoff > 0) {
00117 cfreq = avccontext->cutoff / 1000.0;
00118 if (vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq))
00119 return AVERROR(EINVAL);
00120 }
00121
00122 if (context->iblock) {
00123 vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &context->iblock);
00124 }
00125
00126 if (avccontext->channels == 3 &&
00127 avccontext->channel_layout != (AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER) ||
00128 avccontext->channels == 4 &&
00129 avccontext->channel_layout != AV_CH_LAYOUT_2_2 &&
00130 avccontext->channel_layout != AV_CH_LAYOUT_QUAD ||
00131 avccontext->channels == 5 &&
00132 avccontext->channel_layout != AV_CH_LAYOUT_5POINT0 &&
00133 avccontext->channel_layout != AV_CH_LAYOUT_5POINT0_BACK ||
00134 avccontext->channels == 6 &&
00135 avccontext->channel_layout != AV_CH_LAYOUT_5POINT1 &&
00136 avccontext->channel_layout != AV_CH_LAYOUT_5POINT1_BACK ||
00137 avccontext->channels == 7 &&
00138 avccontext->channel_layout != (AV_CH_LAYOUT_5POINT1|AV_CH_BACK_CENTER) ||
00139 avccontext->channels == 8 &&
00140 avccontext->channel_layout != AV_CH_LAYOUT_7POINT1) {
00141 if (avccontext->channel_layout) {
00142 char name[32];
00143 av_get_channel_layout_string(name, sizeof(name), avccontext->channels,
00144 avccontext->channel_layout);
00145 av_log(avccontext, AV_LOG_ERROR, "%s not supported by Vorbis: "
00146 "output stream will have incorrect "
00147 "channel layout.\n", name);
00148 } else {
00149 av_log(avccontext, AV_LOG_WARNING, "No channel layout specified. The encoder "
00150 "will use Vorbis channel layout for "
00151 "%d channels.\n", avccontext->channels);
00152 }
00153 }
00154
00155 return vorbis_encode_setup_init(vi);
00156 }
00157
00158
00159 static int xiph_len(int l)
00160 {
00161 return 1 + l / 255 + l;
00162 }
00163
00164 static av_cold int oggvorbis_encode_init(AVCodecContext *avccontext)
00165 {
00166 OggVorbisContext *context = avccontext->priv_data;
00167 ogg_packet header, header_comm, header_code;
00168 uint8_t *p;
00169 unsigned int offset;
00170 int r;
00171
00172 vorbis_info_init(&context->vi);
00173 r = oggvorbis_init_encoder(&context->vi, avccontext);
00174 if (r < 0) {
00175 av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init failed\n");
00176 return r;
00177 }
00178 vorbis_analysis_init(&context->vd, &context->vi);
00179 vorbis_block_init(&context->vd, &context->vb);
00180
00181 vorbis_comment_init(&context->vc);
00182 vorbis_comment_add_tag(&context->vc, "encoder", LIBAVCODEC_IDENT);
00183
00184 vorbis_analysis_headerout(&context->vd, &context->vc, &header,
00185 &header_comm, &header_code);
00186
00187 avccontext->extradata_size =
00188 1 + xiph_len(header.bytes) + xiph_len(header_comm.bytes) +
00189 header_code.bytes;
00190 p = avccontext->extradata =
00191 av_malloc(avccontext->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00192 p[0] = 2;
00193 offset = 1;
00194 offset += av_xiphlacing(&p[offset], header.bytes);
00195 offset += av_xiphlacing(&p[offset], header_comm.bytes);
00196 memcpy(&p[offset], header.packet, header.bytes);
00197 offset += header.bytes;
00198 memcpy(&p[offset], header_comm.packet, header_comm.bytes);
00199 offset += header_comm.bytes;
00200 memcpy(&p[offset], header_code.packet, header_code.bytes);
00201 offset += header_code.bytes;
00202 assert(offset == avccontext->extradata_size);
00203
00204 #if 0
00205 vorbis_block_clear(&context->vb);
00206 vorbis_dsp_clear(&context->vd);
00207 vorbis_info_clear(&context->vi);
00208 #endif
00209 vorbis_comment_clear(&context->vc);
00210
00211 avccontext->frame_size = OGGVORBIS_FRAME_SIZE;
00212
00213 avccontext->coded_frame = avcodec_alloc_frame();
00214 avccontext->coded_frame->key_frame = 1;
00215
00216 return 0;
00217 }
00218
00219 static int oggvorbis_encode_frame(AVCodecContext *avccontext,
00220 unsigned char *packets,
00221 int buf_size, void *data)
00222 {
00223 OggVorbisContext *context = avccontext->priv_data;
00224 ogg_packet op;
00225 signed short *audio = data;
00226 int l;
00227
00228 if (data) {
00229 const int samples = avccontext->frame_size;
00230 float **buffer;
00231 int c, channels = context->vi.channels;
00232
00233 buffer = vorbis_analysis_buffer(&context->vd, samples);
00234 for (c = 0; c < channels; c++) {
00235 int co = (channels > 8) ? c :
00236 ff_vorbis_encoding_channel_layout_offsets[channels - 1][c];
00237 for (l = 0; l < samples; l++)
00238 buffer[c][l] = audio[l * channels + co] / 32768.f;
00239 }
00240 vorbis_analysis_wrote(&context->vd, samples);
00241 } else {
00242 if (!context->eof)
00243 vorbis_analysis_wrote(&context->vd, 0);
00244 context->eof = 1;
00245 }
00246
00247 while (vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
00248 vorbis_analysis(&context->vb, NULL);
00249 vorbis_bitrate_addblock(&context->vb);
00250
00251 while (vorbis_bitrate_flushpacket(&context->vd, &op)) {
00252
00253
00254 if (op.bytes == 1 && op.e_o_s)
00255 continue;
00256 if (context->buffer_index + sizeof(ogg_packet) + op.bytes > BUFFER_SIZE) {
00257 av_log(avccontext, AV_LOG_ERROR, "libvorbis: buffer overflow.\n");
00258 return AVERROR(EINVAL);
00259 }
00260 memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet));
00261 context->buffer_index += sizeof(ogg_packet);
00262 memcpy(context->buffer + context->buffer_index, op.packet, op.bytes);
00263 context->buffer_index += op.bytes;
00264
00265 }
00266 }
00267
00268 l = 0;
00269 if (context->buffer_index) {
00270 ogg_packet *op2 = (ogg_packet *)context->buffer;
00271 op2->packet = context->buffer + sizeof(ogg_packet);
00272
00273 l = op2->bytes;
00274 avccontext->coded_frame->pts = av_rescale_q(op2->granulepos, (AVRational) { 1, avccontext->sample_rate }, avccontext->time_base);
00275
00276
00277 if (l > buf_size) {
00278 av_log(avccontext, AV_LOG_ERROR, "libvorbis: buffer overflow.\n");
00279 return AVERROR(EINVAL);
00280 }
00281
00282 memcpy(packets, op2->packet, l);
00283 context->buffer_index -= l + sizeof(ogg_packet);
00284 memmove(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index);
00285
00286 }
00287
00288 return l;
00289 }
00290
00291 static av_cold int oggvorbis_encode_close(AVCodecContext *avccontext)
00292 {
00293 OggVorbisContext *context = avccontext->priv_data;
00294
00295
00296 vorbis_analysis_wrote(&context->vd, 0);
00297
00298 vorbis_block_clear(&context->vb);
00299 vorbis_dsp_clear(&context->vd);
00300 vorbis_info_clear(&context->vi);
00301
00302 av_freep(&avccontext->coded_frame);
00303 av_freep(&avccontext->extradata);
00304
00305 return 0;
00306 }
00307
00308 AVCodec ff_libvorbis_encoder = {
00309 .name = "libvorbis",
00310 .type = AVMEDIA_TYPE_AUDIO,
00311 .id = CODEC_ID_VORBIS,
00312 .priv_data_size = sizeof(OggVorbisContext),
00313 .init = oggvorbis_encode_init,
00314 .encode = oggvorbis_encode_frame,
00315 .close = oggvorbis_encode_close,
00316 .capabilities = CODEC_CAP_DELAY,
00317 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
00318 .long_name = NULL_IF_CONFIG_SMALL("libvorbis Vorbis"),
00319 .priv_class = &class,
00320 .defaults = defaults,
00321 };