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/fifo.h"
00030 #include "libavutil/opt.h"
00031 #include "avcodec.h"
00032 #include "audio_frame_queue.h"
00033 #include "bytestream.h"
00034 #include "internal.h"
00035 #include "vorbis.h"
00036 #include "vorbis_parser.h"
00037
00038 #undef NDEBUG
00039 #include <assert.h>
00040
00041
00042
00043
00044
00045
00046 #define OGGVORBIS_FRAME_SIZE 64
00047
00048 #define BUFFER_SIZE (1024 * 64)
00049
00050 typedef struct OggVorbisContext {
00051 AVClass *av_class;
00052 AVFrame frame;
00053 vorbis_info vi;
00054 vorbis_dsp_state vd;
00055 vorbis_block vb;
00056 AVFifoBuffer *pkt_fifo;
00057 int eof;
00058 int dsp_initialized;
00059 vorbis_comment vc;
00060 ogg_packet op;
00061 double iblock;
00062 VorbisParseContext vp;
00063 AudioFrameQueue afq;
00064 } OggVorbisContext;
00065
00066 static const AVOption options[] = {
00067 { "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 },
00068 { NULL }
00069 };
00070
00071 static const AVCodecDefault defaults[] = {
00072 { "b", "0" },
00073 { NULL },
00074 };
00075
00076 static const AVClass class = { "libvorbis", av_default_item_name, options, LIBAVUTIL_VERSION_INT };
00077
00078
00079 static int vorbis_error_to_averror(int ov_err)
00080 {
00081 switch (ov_err) {
00082 case OV_EFAULT: return AVERROR_BUG;
00083 case OV_EINVAL: return AVERROR(EINVAL);
00084 case OV_EIMPL: return AVERROR(EINVAL);
00085 default: return AVERROR_UNKNOWN;
00086 }
00087 }
00088
00089 static av_cold int oggvorbis_init_encoder(vorbis_info *vi,
00090 AVCodecContext *avctx)
00091 {
00092 OggVorbisContext *s = avctx->priv_data;
00093 double cfreq;
00094 int ret;
00095
00096 if (avctx->flags & CODEC_FLAG_QSCALE || !avctx->bit_rate) {
00097
00098
00099
00100
00101 float q = avctx->global_quality / (float)FF_QP2LAMBDA;
00102
00103 if (!(avctx->flags & CODEC_FLAG_QSCALE))
00104 q = 3.0;
00105 if ((ret = vorbis_encode_setup_vbr(vi, avctx->channels,
00106 avctx->sample_rate,
00107 q / 10.0)))
00108 goto error;
00109 } else {
00110 int minrate = avctx->rc_min_rate > 0 ? avctx->rc_min_rate : -1;
00111 int maxrate = avctx->rc_max_rate > 0 ? avctx->rc_max_rate : -1;
00112
00113
00114 if ((ret = vorbis_encode_setup_managed(vi, avctx->channels,
00115 avctx->sample_rate, maxrate,
00116 avctx->bit_rate, minrate)))
00117 goto error;
00118
00119
00120 if (minrate == -1 && maxrate == -1)
00121 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL)))
00122 goto error;
00123 }
00124
00125
00126 if (avctx->cutoff > 0) {
00127 cfreq = avctx->cutoff / 1000.0;
00128 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq)))
00129 goto error;
00130 }
00131
00132
00133 if (s->iblock) {
00134 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &s->iblock)))
00135 goto error;
00136 }
00137
00138 if (avctx->channels == 3 &&
00139 avctx->channel_layout != (AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER) ||
00140 avctx->channels == 4 &&
00141 avctx->channel_layout != AV_CH_LAYOUT_2_2 &&
00142 avctx->channel_layout != AV_CH_LAYOUT_QUAD ||
00143 avctx->channels == 5 &&
00144 avctx->channel_layout != AV_CH_LAYOUT_5POINT0 &&
00145 avctx->channel_layout != AV_CH_LAYOUT_5POINT0_BACK ||
00146 avctx->channels == 6 &&
00147 avctx->channel_layout != AV_CH_LAYOUT_5POINT1 &&
00148 avctx->channel_layout != AV_CH_LAYOUT_5POINT1_BACK ||
00149 avctx->channels == 7 &&
00150 avctx->channel_layout != (AV_CH_LAYOUT_5POINT1|AV_CH_BACK_CENTER) ||
00151 avctx->channels == 8 &&
00152 avctx->channel_layout != AV_CH_LAYOUT_7POINT1) {
00153 if (avctx->channel_layout) {
00154 char name[32];
00155 av_get_channel_layout_string(name, sizeof(name), avctx->channels,
00156 avctx->channel_layout);
00157 av_log(avctx, AV_LOG_ERROR, "%s not supported by Vorbis: "
00158 "output stream will have incorrect "
00159 "channel layout.\n", name);
00160 } else {
00161 av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The encoder "
00162 "will use Vorbis channel layout for "
00163 "%d channels.\n", avctx->channels);
00164 }
00165 }
00166
00167 if ((ret = vorbis_encode_setup_init(vi)))
00168 goto error;
00169
00170 return 0;
00171 error:
00172 return vorbis_error_to_averror(ret);
00173 }
00174
00175
00176 static int xiph_len(int l)
00177 {
00178 return 1 + l / 255 + l;
00179 }
00180
00181 static av_cold int oggvorbis_encode_close(AVCodecContext *avctx)
00182 {
00183 OggVorbisContext *s = avctx->priv_data;
00184
00185
00186 if (s->dsp_initialized)
00187 vorbis_analysis_wrote(&s->vd, 0);
00188
00189 vorbis_block_clear(&s->vb);
00190 vorbis_dsp_clear(&s->vd);
00191 vorbis_info_clear(&s->vi);
00192
00193 av_fifo_free(s->pkt_fifo);
00194 ff_af_queue_close(&s->afq);
00195 #if FF_API_OLD_ENCODE_AUDIO
00196 av_freep(&avctx->coded_frame);
00197 #endif
00198 av_freep(&avctx->extradata);
00199
00200 return 0;
00201 }
00202
00203 static av_cold int oggvorbis_encode_init(AVCodecContext *avctx)
00204 {
00205 OggVorbisContext *s = avctx->priv_data;
00206 ogg_packet header, header_comm, header_code;
00207 uint8_t *p;
00208 unsigned int offset;
00209 int ret;
00210
00211 vorbis_info_init(&s->vi);
00212 if ((ret = oggvorbis_init_encoder(&s->vi, avctx))) {
00213 av_log(avctx, AV_LOG_ERROR, "encoder setup failed\n");
00214 goto error;
00215 }
00216 if ((ret = vorbis_analysis_init(&s->vd, &s->vi))) {
00217 av_log(avctx, AV_LOG_ERROR, "analysis init failed\n");
00218 ret = vorbis_error_to_averror(ret);
00219 goto error;
00220 }
00221 s->dsp_initialized = 1;
00222 if ((ret = vorbis_block_init(&s->vd, &s->vb))) {
00223 av_log(avctx, AV_LOG_ERROR, "dsp init failed\n");
00224 ret = vorbis_error_to_averror(ret);
00225 goto error;
00226 }
00227
00228 vorbis_comment_init(&s->vc);
00229 if (!(avctx->flags & CODEC_FLAG_BITEXACT))
00230 vorbis_comment_add_tag(&s->vc, "encoder", LIBAVCODEC_IDENT);
00231
00232 if ((ret = vorbis_analysis_headerout(&s->vd, &s->vc, &header, &header_comm,
00233 &header_code))) {
00234 ret = vorbis_error_to_averror(ret);
00235 goto error;
00236 }
00237
00238 avctx->extradata_size = 1 + xiph_len(header.bytes) +
00239 xiph_len(header_comm.bytes) +
00240 header_code.bytes;
00241 p = avctx->extradata = av_malloc(avctx->extradata_size +
00242 FF_INPUT_BUFFER_PADDING_SIZE);
00243 if (!p) {
00244 ret = AVERROR(ENOMEM);
00245 goto error;
00246 }
00247 p[0] = 2;
00248 offset = 1;
00249 offset += av_xiphlacing(&p[offset], header.bytes);
00250 offset += av_xiphlacing(&p[offset], header_comm.bytes);
00251 memcpy(&p[offset], header.packet, header.bytes);
00252 offset += header.bytes;
00253 memcpy(&p[offset], header_comm.packet, header_comm.bytes);
00254 offset += header_comm.bytes;
00255 memcpy(&p[offset], header_code.packet, header_code.bytes);
00256 offset += header_code.bytes;
00257 assert(offset == avctx->extradata_size);
00258
00259 if ((ret = avpriv_vorbis_parse_extradata(avctx, &s->vp)) < 0) {
00260 av_log(avctx, AV_LOG_ERROR, "invalid extradata\n");
00261 return ret;
00262 }
00263
00264 vorbis_comment_clear(&s->vc);
00265
00266 avctx->frame_size = OGGVORBIS_FRAME_SIZE;
00267 ff_af_queue_init(avctx, &s->afq);
00268
00269 s->pkt_fifo = av_fifo_alloc(BUFFER_SIZE);
00270 if (!s->pkt_fifo) {
00271 ret = AVERROR(ENOMEM);
00272 goto error;
00273 }
00274
00275 #if FF_API_OLD_ENCODE_AUDIO
00276 avctx->coded_frame = avcodec_alloc_frame();
00277 if (!avctx->coded_frame) {
00278 ret = AVERROR(ENOMEM);
00279 goto error;
00280 }
00281 #endif
00282
00283 return 0;
00284 error:
00285 oggvorbis_encode_close(avctx);
00286 return ret;
00287 }
00288
00289 static int oggvorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
00290 const AVFrame *frame, int *got_packet_ptr)
00291 {
00292 OggVorbisContext *s = avctx->priv_data;
00293 ogg_packet op;
00294 int ret, duration;
00295
00296
00297 if (frame) {
00298 const float *audio = (const float *)frame->data[0];
00299 const int samples = frame->nb_samples;
00300 float **buffer;
00301 int c, channels = s->vi.channels;
00302
00303 buffer = vorbis_analysis_buffer(&s->vd, samples);
00304 for (c = 0; c < channels; c++) {
00305 int i;
00306 int co = (channels > 8) ? c :
00307 ff_vorbis_encoding_channel_layout_offsets[channels - 1][c];
00308 for (i = 0; i < samples; i++)
00309 buffer[c][i] = audio[i * channels + co];
00310 }
00311 if ((ret = vorbis_analysis_wrote(&s->vd, samples)) < 0) {
00312 av_log(avctx, AV_LOG_ERROR, "error in vorbis_analysis_wrote()\n");
00313 return vorbis_error_to_averror(ret);
00314 }
00315 if ((ret = ff_af_queue_add(&s->afq, frame) < 0))
00316 return ret;
00317 } else {
00318 if (!s->eof)
00319 if ((ret = vorbis_analysis_wrote(&s->vd, 0)) < 0) {
00320 av_log(avctx, AV_LOG_ERROR, "error in vorbis_analysis_wrote()\n");
00321 return vorbis_error_to_averror(ret);
00322 }
00323 s->eof = 1;
00324 }
00325
00326
00327 while ((ret = vorbis_analysis_blockout(&s->vd, &s->vb)) == 1) {
00328 if ((ret = vorbis_analysis(&s->vb, NULL)) < 0)
00329 break;
00330 if ((ret = vorbis_bitrate_addblock(&s->vb)) < 0)
00331 break;
00332
00333
00334 while ((ret = vorbis_bitrate_flushpacket(&s->vd, &op)) == 1) {
00335 if (av_fifo_space(s->pkt_fifo) < sizeof(ogg_packet) + op.bytes) {
00336 av_log(avctx, AV_LOG_ERROR, "packet buffer is too small\n");
00337 return AVERROR_BUG;
00338 }
00339 av_fifo_generic_write(s->pkt_fifo, &op, sizeof(ogg_packet), NULL);
00340 av_fifo_generic_write(s->pkt_fifo, op.packet, op.bytes, NULL);
00341 }
00342 if (ret < 0) {
00343 av_log(avctx, AV_LOG_ERROR, "error getting available packets\n");
00344 break;
00345 }
00346 }
00347 if (ret < 0) {
00348 av_log(avctx, AV_LOG_ERROR, "error getting available packets\n");
00349 return vorbis_error_to_averror(ret);
00350 }
00351
00352
00353 if (av_fifo_size(s->pkt_fifo) < sizeof(ogg_packet))
00354 return 0;
00355
00356 av_fifo_generic_read(s->pkt_fifo, &op, sizeof(ogg_packet), NULL);
00357
00358 if ((ret = ff_alloc_packet2(avctx, avpkt, op.bytes)))
00359 return ret;
00360 av_fifo_generic_read(s->pkt_fifo, avpkt->data, op.bytes, NULL);
00361
00362 avpkt->pts = ff_samples_to_time_base(avctx, op.granulepos);
00363
00364 duration = avpriv_vorbis_parse_frame(&s->vp, avpkt->data, avpkt->size);
00365 if (duration > 0) {
00366
00367
00368 if (!avctx->delay) {
00369 avctx->delay = duration;
00370 s->afq.remaining_delay += duration;
00371 s->afq.remaining_samples += duration;
00372 }
00373 ff_af_queue_remove(&s->afq, duration, &avpkt->pts, &avpkt->duration);
00374 }
00375
00376 *got_packet_ptr = 1;
00377 return 0;
00378 }
00379
00380 AVCodec ff_libvorbis_encoder = {
00381 .name = "libvorbis",
00382 .type = AVMEDIA_TYPE_AUDIO,
00383 .id = CODEC_ID_VORBIS,
00384 .priv_data_size = sizeof(OggVorbisContext),
00385 .init = oggvorbis_encode_init,
00386 .encode2 = oggvorbis_encode_frame,
00387 .close = oggvorbis_encode_close,
00388 .capabilities = CODEC_CAP_DELAY,
00389 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
00390 AV_SAMPLE_FMT_NONE },
00391 .long_name = NULL_IF_CONFIG_SMALL("libvorbis Vorbis"),
00392 .priv_class = &class,
00393 .defaults = defaults,
00394 };
00395
00396 static int oggvorbis_decode_init(AVCodecContext *avccontext) {
00397 OggVorbisContext *context = avccontext->priv_data ;
00398 uint8_t *p= avccontext->extradata;
00399 int i, hsizes[3];
00400 unsigned char *headers[3], *extradata = avccontext->extradata;
00401
00402 vorbis_info_init(&context->vi) ;
00403 vorbis_comment_init(&context->vc) ;
00404
00405 if(! avccontext->extradata_size || ! p) {
00406 av_log(avccontext, AV_LOG_ERROR, "vorbis extradata absent\n");
00407 return -1;
00408 }
00409
00410 if(p[0] == 0 && p[1] == 30) {
00411 for(i = 0; i < 3; i++){
00412 hsizes[i] = bytestream_get_be16(&p);
00413 headers[i] = p;
00414 p += hsizes[i];
00415 }
00416 } else if(*p == 2) {
00417 unsigned int offset = 1;
00418 p++;
00419 for(i=0; i<2; i++) {
00420 hsizes[i] = 0;
00421 while((*p == 0xFF) && (offset < avccontext->extradata_size)) {
00422 hsizes[i] += 0xFF;
00423 offset++;
00424 p++;
00425 }
00426 if(offset >= avccontext->extradata_size - 1) {
00427 av_log(avccontext, AV_LOG_ERROR,
00428 "vorbis header sizes damaged\n");
00429 return -1;
00430 }
00431 hsizes[i] += *p;
00432 offset++;
00433 p++;
00434 }
00435 hsizes[2] = avccontext->extradata_size - hsizes[0]-hsizes[1]-offset;
00436 #if 0
00437 av_log(avccontext, AV_LOG_DEBUG,
00438 "vorbis header sizes: %d, %d, %d, / extradata_len is %d \n",
00439 hsizes[0], hsizes[1], hsizes[2], avccontext->extradata_size);
00440 #endif
00441 headers[0] = extradata + offset;
00442 headers[1] = extradata + offset + hsizes[0];
00443 headers[2] = extradata + offset + hsizes[0] + hsizes[1];
00444 } else {
00445 av_log(avccontext, AV_LOG_ERROR,
00446 "vorbis initial header len is wrong: %d\n", *p);
00447 return -1;
00448 }
00449
00450 for(i=0; i<3; i++){
00451 context->op.b_o_s= i==0;
00452 context->op.bytes = hsizes[i];
00453 context->op.packet = headers[i];
00454 if(vorbis_synthesis_headerin(&context->vi, &context->vc, &context->op)<0){
00455 av_log(avccontext, AV_LOG_ERROR, "%d. vorbis header damaged\n", i+1);
00456 return -1;
00457 }
00458 }
00459
00460 avccontext->channels = context->vi.channels;
00461 avccontext->sample_rate = context->vi.rate;
00462 avccontext->time_base= (AVRational){1, avccontext->sample_rate};
00463
00464 vorbis_synthesis_init(&context->vd, &context->vi);
00465 vorbis_block_init(&context->vd, &context->vb);
00466
00467 return 0 ;
00468 }
00469
00470
00471 static inline int conv(int samples, float **pcm, char *buf, int channels) {
00472 int i, j;
00473 ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
00474 float *mono ;
00475
00476 for(i = 0 ; i < channels ; i++){
00477 ptr = &data[i];
00478 mono = pcm[i] ;
00479
00480 for(j = 0 ; j < samples ; j++) {
00481 *ptr = av_clip_int16(mono[j] * 32767.f);
00482 ptr += channels;
00483 }
00484 }
00485
00486 return 0 ;
00487 }
00488
00489 static int oggvorbis_decode_frame(AVCodecContext *avccontext, void *data,
00490 int *got_frame_ptr, AVPacket *avpkt)
00491 {
00492 OggVorbisContext *context = avccontext->priv_data ;
00493 float **pcm ;
00494 ogg_packet *op= &context->op;
00495 int samples, total_samples, total_bytes;
00496 int ret;
00497 int16_t *output;
00498
00499 if(!avpkt->size){
00500
00501 return 0;
00502 }
00503
00504 context->frame.nb_samples = 8192*4;
00505 if ((ret = avccontext->get_buffer(avccontext, &context->frame)) < 0) {
00506 av_log(avccontext, AV_LOG_ERROR, "get_buffer() failed\n");
00507 return ret;
00508 }
00509 output = (int16_t *)context->frame.data[0];
00510
00511
00512 op->packet = avpkt->data;
00513 op->bytes = avpkt->size;
00514
00515
00516
00517
00518
00519
00520
00521 if(vorbis_synthesis(&context->vb, op) == 0)
00522 vorbis_synthesis_blockin(&context->vd, &context->vb) ;
00523
00524 total_samples = 0 ;
00525 total_bytes = 0 ;
00526
00527 while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
00528 conv(samples, pcm, (char*)output + total_bytes, context->vi.channels) ;
00529 total_bytes += samples * 2 * context->vi.channels ;
00530 total_samples += samples ;
00531 vorbis_synthesis_read(&context->vd, samples) ;
00532 }
00533
00534 context->frame.nb_samples = total_samples;
00535 *got_frame_ptr = 1;
00536 *(AVFrame *)data = context->frame;
00537 return avpkt->size;
00538 }
00539
00540
00541 static int oggvorbis_decode_close(AVCodecContext *avccontext) {
00542 OggVorbisContext *context = avccontext->priv_data ;
00543
00544 vorbis_info_clear(&context->vi) ;
00545 vorbis_comment_clear(&context->vc) ;
00546
00547 return 0 ;
00548 }
00549
00550
00551 AVCodec ff_libvorbis_decoder = {
00552 .name = "libvorbis",
00553 .type = AVMEDIA_TYPE_AUDIO,
00554 .id = CODEC_ID_VORBIS,
00555 .priv_data_size = sizeof(OggVorbisContext),
00556 .init = oggvorbis_decode_init,
00557 .decode = oggvorbis_decode_frame,
00558 .close = oggvorbis_decode_close,
00559 .capabilities = CODEC_CAP_DELAY,
00560 } ;
00561