00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <vorbis/vorbisenc.h>
00022
00023 #include "libavutil/avassert.h"
00024 #include "libavutil/fifo.h"
00025 #include "libavutil/opt.h"
00026 #include "avcodec.h"
00027 #include "audio_frame_queue.h"
00028 #include "internal.h"
00029 #include "vorbis.h"
00030 #include "vorbis_parser.h"
00031
00032
00033
00034
00035
00036
00037
00038 #define OGGVORBIS_FRAME_SIZE 64
00039
00040 #define BUFFER_SIZE (1024 * 64)
00041
00042 typedef struct OggVorbisEncContext {
00043 AVClass *av_class;
00044 AVFrame frame;
00045 vorbis_info vi;
00046 vorbis_dsp_state vd;
00047 vorbis_block vb;
00048 AVFifoBuffer *pkt_fifo;
00049 int eof;
00050 int dsp_initialized;
00051 vorbis_comment vc;
00052 double iblock;
00053 VorbisParseContext vp;
00054 AudioFrameQueue afq;
00055 } OggVorbisEncContext;
00056
00057 static const AVOption options[] = {
00058 { "iblock", "Sets the impulse block bias", offsetof(OggVorbisEncContext, iblock), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, -15, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
00059 { NULL }
00060 };
00061
00062 static const AVCodecDefault defaults[] = {
00063 { "b", "0" },
00064 { NULL },
00065 };
00066
00067 static const AVClass class = {
00068 .class_name = "libvorbis",
00069 .item_name = av_default_item_name,
00070 .option = options,
00071 .version = LIBAVUTIL_VERSION_INT,
00072 };
00073
00074 static int vorbis_error_to_averror(int ov_err)
00075 {
00076 switch (ov_err) {
00077 case OV_EFAULT: return AVERROR_BUG;
00078 case OV_EINVAL: return AVERROR(EINVAL);
00079 case OV_EIMPL: return AVERROR(EINVAL);
00080 default: return AVERROR_UNKNOWN;
00081 }
00082 }
00083
00084 static av_cold int oggvorbis_init_encoder(vorbis_info *vi,
00085 AVCodecContext *avctx)
00086 {
00087 OggVorbisEncContext *s = avctx->priv_data;
00088 double cfreq;
00089 int ret;
00090
00091 if (avctx->flags & CODEC_FLAG_QSCALE || !avctx->bit_rate) {
00092
00093
00094
00095
00096 float q = avctx->global_quality / (float)FF_QP2LAMBDA;
00097
00098 if (!(avctx->flags & CODEC_FLAG_QSCALE))
00099 q = 3.0;
00100 if ((ret = vorbis_encode_setup_vbr(vi, avctx->channels,
00101 avctx->sample_rate,
00102 q / 10.0)))
00103 goto error;
00104 } else {
00105 int minrate = avctx->rc_min_rate > 0 ? avctx->rc_min_rate : -1;
00106 int maxrate = avctx->rc_max_rate > 0 ? avctx->rc_max_rate : -1;
00107
00108
00109 if ((ret = vorbis_encode_setup_managed(vi, avctx->channels,
00110 avctx->sample_rate, maxrate,
00111 avctx->bit_rate, minrate)))
00112 goto error;
00113
00114
00115 if (minrate == -1 && maxrate == -1)
00116 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL)))
00117 goto error;
00118 }
00119
00120
00121 if (avctx->cutoff > 0) {
00122 cfreq = avctx->cutoff / 1000.0;
00123 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq)))
00124 goto error;
00125 }
00126
00127
00128 if (s->iblock) {
00129 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &s->iblock)))
00130 goto error;
00131 }
00132
00133 if (avctx->channels == 3 &&
00134 avctx->channel_layout != (AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER) ||
00135 avctx->channels == 4 &&
00136 avctx->channel_layout != AV_CH_LAYOUT_2_2 &&
00137 avctx->channel_layout != AV_CH_LAYOUT_QUAD ||
00138 avctx->channels == 5 &&
00139 avctx->channel_layout != AV_CH_LAYOUT_5POINT0 &&
00140 avctx->channel_layout != AV_CH_LAYOUT_5POINT0_BACK ||
00141 avctx->channels == 6 &&
00142 avctx->channel_layout != AV_CH_LAYOUT_5POINT1 &&
00143 avctx->channel_layout != AV_CH_LAYOUT_5POINT1_BACK ||
00144 avctx->channels == 7 &&
00145 avctx->channel_layout != (AV_CH_LAYOUT_5POINT1|AV_CH_BACK_CENTER) ||
00146 avctx->channels == 8 &&
00147 avctx->channel_layout != AV_CH_LAYOUT_7POINT1) {
00148 if (avctx->channel_layout) {
00149 char name[32];
00150 av_get_channel_layout_string(name, sizeof(name), avctx->channels,
00151 avctx->channel_layout);
00152 av_log(avctx, AV_LOG_ERROR, "%s not supported by Vorbis: "
00153 "output stream will have incorrect "
00154 "channel layout.\n", name);
00155 } else {
00156 av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The encoder "
00157 "will use Vorbis channel layout for "
00158 "%d channels.\n", avctx->channels);
00159 }
00160 }
00161
00162 if ((ret = vorbis_encode_setup_init(vi)))
00163 goto error;
00164
00165 return 0;
00166 error:
00167 return vorbis_error_to_averror(ret);
00168 }
00169
00170
00171 static int xiph_len(int l)
00172 {
00173 return 1 + l / 255 + l;
00174 }
00175
00176 static av_cold int oggvorbis_encode_close(AVCodecContext *avctx)
00177 {
00178 OggVorbisEncContext *s = avctx->priv_data;
00179
00180
00181 if (s->dsp_initialized)
00182 vorbis_analysis_wrote(&s->vd, 0);
00183
00184 vorbis_block_clear(&s->vb);
00185 vorbis_dsp_clear(&s->vd);
00186 vorbis_info_clear(&s->vi);
00187
00188 av_fifo_free(s->pkt_fifo);
00189 ff_af_queue_close(&s->afq);
00190 #if FF_API_OLD_ENCODE_AUDIO
00191 av_freep(&avctx->coded_frame);
00192 #endif
00193 av_freep(&avctx->extradata);
00194
00195 return 0;
00196 }
00197
00198 static av_cold int oggvorbis_encode_init(AVCodecContext *avctx)
00199 {
00200 OggVorbisEncContext *s = avctx->priv_data;
00201 ogg_packet header, header_comm, header_code;
00202 uint8_t *p;
00203 unsigned int offset;
00204 int ret;
00205
00206 vorbis_info_init(&s->vi);
00207 if ((ret = oggvorbis_init_encoder(&s->vi, avctx))) {
00208 av_log(avctx, AV_LOG_ERROR, "encoder setup failed\n");
00209 goto error;
00210 }
00211 if ((ret = vorbis_analysis_init(&s->vd, &s->vi))) {
00212 av_log(avctx, AV_LOG_ERROR, "analysis init failed\n");
00213 ret = vorbis_error_to_averror(ret);
00214 goto error;
00215 }
00216 s->dsp_initialized = 1;
00217 if ((ret = vorbis_block_init(&s->vd, &s->vb))) {
00218 av_log(avctx, AV_LOG_ERROR, "dsp init failed\n");
00219 ret = vorbis_error_to_averror(ret);
00220 goto error;
00221 }
00222
00223 vorbis_comment_init(&s->vc);
00224 if (!(avctx->flags & CODEC_FLAG_BITEXACT))
00225 vorbis_comment_add_tag(&s->vc, "encoder", LIBAVCODEC_IDENT);
00226
00227 if ((ret = vorbis_analysis_headerout(&s->vd, &s->vc, &header, &header_comm,
00228 &header_code))) {
00229 ret = vorbis_error_to_averror(ret);
00230 goto error;
00231 }
00232
00233 avctx->extradata_size = 1 + xiph_len(header.bytes) +
00234 xiph_len(header_comm.bytes) +
00235 header_code.bytes;
00236 p = avctx->extradata = av_malloc(avctx->extradata_size +
00237 FF_INPUT_BUFFER_PADDING_SIZE);
00238 if (!p) {
00239 ret = AVERROR(ENOMEM);
00240 goto error;
00241 }
00242 p[0] = 2;
00243 offset = 1;
00244 offset += av_xiphlacing(&p[offset], header.bytes);
00245 offset += av_xiphlacing(&p[offset], header_comm.bytes);
00246 memcpy(&p[offset], header.packet, header.bytes);
00247 offset += header.bytes;
00248 memcpy(&p[offset], header_comm.packet, header_comm.bytes);
00249 offset += header_comm.bytes;
00250 memcpy(&p[offset], header_code.packet, header_code.bytes);
00251 offset += header_code.bytes;
00252 av_assert0(offset == avctx->extradata_size);
00253
00254 if ((ret = avpriv_vorbis_parse_extradata(avctx, &s->vp)) < 0) {
00255 av_log(avctx, AV_LOG_ERROR, "invalid extradata\n");
00256 return ret;
00257 }
00258
00259 vorbis_comment_clear(&s->vc);
00260
00261 avctx->frame_size = OGGVORBIS_FRAME_SIZE;
00262 ff_af_queue_init(avctx, &s->afq);
00263
00264 s->pkt_fifo = av_fifo_alloc(BUFFER_SIZE);
00265 if (!s->pkt_fifo) {
00266 ret = AVERROR(ENOMEM);
00267 goto error;
00268 }
00269
00270 #if FF_API_OLD_ENCODE_AUDIO
00271 avctx->coded_frame = avcodec_alloc_frame();
00272 if (!avctx->coded_frame) {
00273 ret = AVERROR(ENOMEM);
00274 goto error;
00275 }
00276 #endif
00277
00278 return 0;
00279 error:
00280 oggvorbis_encode_close(avctx);
00281 return ret;
00282 }
00283
00284 static int oggvorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
00285 const AVFrame *frame, int *got_packet_ptr)
00286 {
00287 OggVorbisEncContext *s = avctx->priv_data;
00288 ogg_packet op;
00289 int ret, duration;
00290
00291
00292 if (frame) {
00293 const int samples = frame->nb_samples;
00294 float **buffer;
00295 int c, channels = s->vi.channels;
00296
00297 buffer = vorbis_analysis_buffer(&s->vd, samples);
00298 for (c = 0; c < channels; c++) {
00299 int co = (channels > 8) ? c :
00300 ff_vorbis_encoding_channel_layout_offsets[channels - 1][c];
00301 memcpy(buffer[c], frame->extended_data[co],
00302 samples * sizeof(*buffer[c]));
00303 }
00304 if ((ret = vorbis_analysis_wrote(&s->vd, samples)) < 0) {
00305 av_log(avctx, AV_LOG_ERROR, "error in vorbis_analysis_wrote()\n");
00306 return vorbis_error_to_averror(ret);
00307 }
00308 if ((ret = ff_af_queue_add(&s->afq, frame) < 0))
00309 return ret;
00310 } else {
00311 if (!s->eof)
00312 if ((ret = vorbis_analysis_wrote(&s->vd, 0)) < 0) {
00313 av_log(avctx, AV_LOG_ERROR, "error in vorbis_analysis_wrote()\n");
00314 return vorbis_error_to_averror(ret);
00315 }
00316 s->eof = 1;
00317 }
00318
00319
00320 while ((ret = vorbis_analysis_blockout(&s->vd, &s->vb)) == 1) {
00321 if ((ret = vorbis_analysis(&s->vb, NULL)) < 0)
00322 break;
00323 if ((ret = vorbis_bitrate_addblock(&s->vb)) < 0)
00324 break;
00325
00326
00327 while ((ret = vorbis_bitrate_flushpacket(&s->vd, &op)) == 1) {
00328 if (av_fifo_space(s->pkt_fifo) < sizeof(ogg_packet) + op.bytes) {
00329 av_log(avctx, AV_LOG_ERROR, "packet buffer is too small\n");
00330 return AVERROR_BUG;
00331 }
00332 av_fifo_generic_write(s->pkt_fifo, &op, sizeof(ogg_packet), NULL);
00333 av_fifo_generic_write(s->pkt_fifo, op.packet, op.bytes, NULL);
00334 }
00335 if (ret < 0) {
00336 av_log(avctx, AV_LOG_ERROR, "error getting available packets\n");
00337 break;
00338 }
00339 }
00340 if (ret < 0) {
00341 av_log(avctx, AV_LOG_ERROR, "error getting available packets\n");
00342 return vorbis_error_to_averror(ret);
00343 }
00344
00345
00346 if (av_fifo_size(s->pkt_fifo) < sizeof(ogg_packet))
00347 return 0;
00348
00349 av_fifo_generic_read(s->pkt_fifo, &op, sizeof(ogg_packet), NULL);
00350
00351 if ((ret = ff_alloc_packet2(avctx, avpkt, op.bytes)))
00352 return ret;
00353 av_fifo_generic_read(s->pkt_fifo, avpkt->data, op.bytes, NULL);
00354
00355 avpkt->pts = ff_samples_to_time_base(avctx, op.granulepos);
00356
00357 duration = avpriv_vorbis_parse_frame(&s->vp, avpkt->data, avpkt->size);
00358 if (duration > 0) {
00359
00360
00361 if (!avctx->delay && s->afq.frames) {
00362 avctx->delay = duration;
00363 av_assert0(!s->afq.remaining_delay);
00364 s->afq.frames->duration += duration;
00365 s->afq.frames->pts -= duration;
00366 s->afq.remaining_samples += duration;
00367 }
00368 ff_af_queue_remove(&s->afq, duration, &avpkt->pts, &avpkt->duration);
00369 }
00370
00371 *got_packet_ptr = 1;
00372 return 0;
00373 }
00374
00375 AVCodec ff_libvorbis_encoder = {
00376 .name = "libvorbis",
00377 .type = AVMEDIA_TYPE_AUDIO,
00378 .id = AV_CODEC_ID_VORBIS,
00379 .priv_data_size = sizeof(OggVorbisEncContext),
00380 .init = oggvorbis_encode_init,
00381 .encode2 = oggvorbis_encode_frame,
00382 .close = oggvorbis_encode_close,
00383 .capabilities = CODEC_CAP_DELAY,
00384 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
00385 AV_SAMPLE_FMT_NONE },
00386 .long_name = NULL_IF_CONFIG_SMALL("libvorbis"),
00387 .priv_class = &class,
00388 .defaults = defaults,
00389 };