libvorbisenc.c (14497B)
1 /* 2 * Copyright (c) 2002 Mark Hills <mark@pogo.org.uk> 3 * 4 * This file is part of FFmpeg. 5 * 6 * FFmpeg is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * FFmpeg is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with FFmpeg; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 */ 20 21 #include <vorbis/vorbisenc.h> 22 23 #include "libavutil/avassert.h" 24 #include "libavutil/channel_layout.h" 25 #include "libavutil/fifo.h" 26 #include "libavutil/mem.h" 27 #include "libavutil/opt.h" 28 #include "avcodec.h" 29 #include "audio_frame_queue.h" 30 #include "codec_internal.h" 31 #include "encode.h" 32 #include "version.h" 33 #include "vorbis_parser.h" 34 35 36 /* Number of samples the user should send in each call. 37 * This value is used because it is the LCD of all possible frame sizes, so 38 * an output packet will always start at the same point as one of the input 39 * packets. 40 */ 41 #define LIBVORBIS_FRAME_SIZE 64 42 43 #define BUFFER_SIZE (1024 * 64) 44 45 typedef struct LibvorbisEncContext { 46 AVClass *av_class; /**< class for AVOptions */ 47 vorbis_info vi; /**< vorbis_info used during init */ 48 vorbis_dsp_state vd; /**< DSP state used for analysis */ 49 vorbis_block vb; /**< vorbis_block used for analysis */ 50 AVFifo *pkt_fifo; /**< output packet buffer */ 51 int eof; /**< end-of-file flag */ 52 int dsp_initialized; /**< vd has been initialized */ 53 vorbis_comment vc; /**< VorbisComment info */ 54 double iblock; /**< impulse block bias option */ 55 AVVorbisParseContext *vp; /**< parse context to get durations */ 56 AudioFrameQueue afq; /**< frame queue for timestamps */ 57 } LibvorbisEncContext; 58 59 static const AVOption options[] = { 60 { "iblock", "Sets the impulse block bias", offsetof(LibvorbisEncContext, iblock), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, -15, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM }, 61 { NULL } 62 }; 63 64 static const FFCodecDefault defaults[] = { 65 { "b", "0" }, 66 { NULL }, 67 }; 68 69 static const AVClass vorbis_class = { 70 .class_name = "libvorbis", 71 .item_name = av_default_item_name, 72 .option = options, 73 .version = LIBAVUTIL_VERSION_INT, 74 }; 75 76 static const uint8_t vorbis_encoding_channel_layout_offsets[8][8] = { 77 { 0 }, 78 { 0, 1 }, 79 { 0, 2, 1 }, 80 { 0, 1, 2, 3 }, 81 { 0, 2, 1, 3, 4 }, 82 { 0, 2, 1, 4, 5, 3 }, 83 { 0, 2, 1, 5, 6, 4, 3 }, 84 { 0, 2, 1, 6, 7, 4, 5, 3 }, 85 }; 86 87 static int vorbis_error_to_averror(int ov_err) 88 { 89 switch (ov_err) { 90 case OV_EFAULT: return AVERROR_BUG; 91 case OV_EINVAL: return AVERROR(EINVAL); 92 case OV_EIMPL: return AVERROR(EINVAL); 93 default: return AVERROR_UNKNOWN; 94 } 95 } 96 97 static av_cold int libvorbis_setup(vorbis_info *vi, AVCodecContext *avctx) 98 { 99 LibvorbisEncContext *s = avctx->priv_data; 100 int channels = avctx->ch_layout.nb_channels; 101 double cfreq; 102 int ret; 103 104 if (avctx->flags & AV_CODEC_FLAG_QSCALE || !avctx->bit_rate) { 105 /* variable bitrate 106 * NOTE: we use the oggenc range of -1 to 10 for global_quality for 107 * user convenience, but libvorbis uses -0.1 to 1.0. 108 */ 109 float q = avctx->global_quality / (float)FF_QP2LAMBDA; 110 /* default to 3 if the user did not set quality or bitrate */ 111 if (!(avctx->flags & AV_CODEC_FLAG_QSCALE)) 112 q = 3.0; 113 if ((ret = vorbis_encode_setup_vbr(vi, channels, 114 avctx->sample_rate, 115 q / 10.0))) 116 goto error; 117 } else { 118 int minrate = avctx->rc_min_rate > 0 ? avctx->rc_min_rate : -1; 119 int maxrate = avctx->rc_max_rate > 0 ? avctx->rc_max_rate : -1; 120 121 /* average bitrate */ 122 if ((ret = vorbis_encode_setup_managed(vi, channels, 123 avctx->sample_rate, maxrate, 124 avctx->bit_rate, minrate))) 125 goto error; 126 127 /* variable bitrate by estimate, disable slow rate management */ 128 if (minrate == -1 && maxrate == -1) 129 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL))) 130 goto error; /* should not happen */ 131 } 132 133 /* cutoff frequency */ 134 if (avctx->cutoff > 0) { 135 cfreq = avctx->cutoff / 1000.0; 136 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq))) 137 goto error; /* should not happen */ 138 } 139 140 /* impulse block bias */ 141 if (s->iblock) { 142 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &s->iblock))) 143 goto error; 144 } 145 146 if ((channels == 3 && 147 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_SURROUND)) || 148 (channels == 4 && 149 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_2_2) && 150 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_QUAD)) || 151 (channels == 5 && 152 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0) && 153 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0_BACK)) || 154 (channels == 6 && 155 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1) && 156 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1_BACK)) || 157 (channels == 7 && 158 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_6POINT1)) || 159 (channels == 8 && 160 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_7POINT1))) { 161 if (avctx->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) { 162 char name[32]; 163 av_channel_layout_describe(&avctx->ch_layout, name, sizeof(name)); 164 av_log(avctx, AV_LOG_ERROR, "%s not supported by Vorbis: " 165 "output stream will have incorrect " 166 "channel layout.\n", name); 167 } else { 168 av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The encoder " 169 "will use Vorbis channel layout for " 170 "%d channels.\n", channels); 171 } 172 } 173 174 if ((ret = vorbis_encode_setup_init(vi))) 175 goto error; 176 177 return 0; 178 error: 179 return vorbis_error_to_averror(ret); 180 } 181 182 /* How many bytes are needed for a buffer of length 'l' */ 183 static int xiph_len(int l) 184 { 185 return 1 + l / 255 + l; 186 } 187 188 static av_cold int libvorbis_encode_close(AVCodecContext *avctx) 189 { 190 LibvorbisEncContext *s = avctx->priv_data; 191 192 /* notify vorbisenc this is EOF */ 193 if (s->dsp_initialized) 194 vorbis_analysis_wrote(&s->vd, 0); 195 196 vorbis_block_clear(&s->vb); 197 vorbis_dsp_clear(&s->vd); 198 vorbis_info_clear(&s->vi); 199 200 av_fifo_freep2(&s->pkt_fifo); 201 ff_af_queue_close(&s->afq); 202 203 av_vorbis_parse_free(&s->vp); 204 205 return 0; 206 } 207 208 static av_cold int libvorbis_encode_init(AVCodecContext *avctx) 209 { 210 LibvorbisEncContext *s = avctx->priv_data; 211 ogg_packet header, header_comm, header_code; 212 uint8_t *p; 213 unsigned int offset; 214 int ret; 215 216 vorbis_info_init(&s->vi); 217 if ((ret = libvorbis_setup(&s->vi, avctx))) { 218 av_log(avctx, AV_LOG_ERROR, "encoder setup failed\n"); 219 goto error; 220 } 221 if ((ret = vorbis_analysis_init(&s->vd, &s->vi))) { 222 av_log(avctx, AV_LOG_ERROR, "analysis init failed\n"); 223 ret = vorbis_error_to_averror(ret); 224 goto error; 225 } 226 s->dsp_initialized = 1; 227 if ((ret = vorbis_block_init(&s->vd, &s->vb))) { 228 av_log(avctx, AV_LOG_ERROR, "dsp init failed\n"); 229 ret = vorbis_error_to_averror(ret); 230 goto error; 231 } 232 233 vorbis_comment_init(&s->vc); 234 if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT)) 235 vorbis_comment_add_tag(&s->vc, "encoder", LIBAVCODEC_IDENT); 236 237 if ((ret = vorbis_analysis_headerout(&s->vd, &s->vc, &header, &header_comm, 238 &header_code))) { 239 ret = vorbis_error_to_averror(ret); 240 goto error; 241 } 242 243 avctx->extradata_size = 1 + xiph_len(header.bytes) + 244 xiph_len(header_comm.bytes) + 245 header_code.bytes; 246 p = avctx->extradata = av_malloc(avctx->extradata_size + 247 AV_INPUT_BUFFER_PADDING_SIZE); 248 if (!p) { 249 ret = AVERROR(ENOMEM); 250 goto error; 251 } 252 p[0] = 2; 253 offset = 1; 254 offset += av_xiphlacing(&p[offset], header.bytes); 255 offset += av_xiphlacing(&p[offset], header_comm.bytes); 256 memcpy(&p[offset], header.packet, header.bytes); 257 offset += header.bytes; 258 memcpy(&p[offset], header_comm.packet, header_comm.bytes); 259 offset += header_comm.bytes; 260 memcpy(&p[offset], header_code.packet, header_code.bytes); 261 offset += header_code.bytes; 262 av_assert0(offset == avctx->extradata_size); 263 264 s->vp = av_vorbis_parse_init(avctx->extradata, avctx->extradata_size); 265 if (!s->vp) { 266 av_log(avctx, AV_LOG_ERROR, "invalid extradata\n"); 267 return ret; 268 } 269 270 vorbis_comment_clear(&s->vc); 271 272 avctx->frame_size = LIBVORBIS_FRAME_SIZE; 273 ff_af_queue_init(avctx, &s->afq); 274 275 s->pkt_fifo = av_fifo_alloc2(BUFFER_SIZE, 1, 0); 276 if (!s->pkt_fifo) { 277 ret = AVERROR(ENOMEM); 278 goto error; 279 } 280 281 return 0; 282 error: 283 libvorbis_encode_close(avctx); 284 return ret; 285 } 286 287 static int libvorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, 288 const AVFrame *frame, int *got_packet_ptr) 289 { 290 LibvorbisEncContext *s = avctx->priv_data; 291 ogg_packet op; 292 int ret, duration; 293 294 /* send samples to libvorbis */ 295 if (frame) { 296 const int samples = frame->nb_samples; 297 float **buffer; 298 int c, channels = s->vi.channels; 299 300 buffer = vorbis_analysis_buffer(&s->vd, samples); 301 for (c = 0; c < channels; c++) { 302 int co = (channels > 8) ? c : 303 vorbis_encoding_channel_layout_offsets[channels - 1][c]; 304 memcpy(buffer[c], frame->extended_data[co], 305 samples * sizeof(*buffer[c])); 306 } 307 if ((ret = vorbis_analysis_wrote(&s->vd, samples)) < 0) { 308 av_log(avctx, AV_LOG_ERROR, "error in vorbis_analysis_wrote()\n"); 309 return vorbis_error_to_averror(ret); 310 } 311 if ((ret = ff_af_queue_add(&s->afq, frame)) < 0) 312 return ret; 313 } else { 314 if (!s->eof && s->afq.frame_alloc) 315 if ((ret = vorbis_analysis_wrote(&s->vd, 0)) < 0) { 316 av_log(avctx, AV_LOG_ERROR, "error in vorbis_analysis_wrote()\n"); 317 return vorbis_error_to_averror(ret); 318 } 319 s->eof = 1; 320 } 321 322 /* retrieve available packets from libvorbis */ 323 while ((ret = vorbis_analysis_blockout(&s->vd, &s->vb)) == 1) { 324 if ((ret = vorbis_analysis(&s->vb, NULL)) < 0) 325 break; 326 if ((ret = vorbis_bitrate_addblock(&s->vb)) < 0) 327 break; 328 329 /* add any available packets to the output packet buffer */ 330 while ((ret = vorbis_bitrate_flushpacket(&s->vd, &op)) == 1) { 331 if (av_fifo_can_write(s->pkt_fifo) < sizeof(ogg_packet) + op.bytes) { 332 av_log(avctx, AV_LOG_ERROR, "packet buffer is too small\n"); 333 return AVERROR_BUG; 334 } 335 av_fifo_write(s->pkt_fifo, &op, sizeof(ogg_packet)); 336 av_fifo_write(s->pkt_fifo, op.packet, op.bytes); 337 } 338 if (ret < 0) { 339 av_log(avctx, AV_LOG_ERROR, "error getting available packets\n"); 340 break; 341 } 342 } 343 if (ret < 0) { 344 av_log(avctx, AV_LOG_ERROR, "error getting available packets\n"); 345 return vorbis_error_to_averror(ret); 346 } 347 348 /* Read an available packet if possible */ 349 if (av_fifo_read(s->pkt_fifo, &op, sizeof(ogg_packet)) < 0) 350 return 0; 351 352 if ((ret = ff_get_encode_buffer(avctx, avpkt, op.bytes, 0)) < 0) 353 return ret; 354 av_fifo_read(s->pkt_fifo, avpkt->data, op.bytes); 355 356 avpkt->pts = ff_samples_to_time_base(avctx, op.granulepos); 357 358 duration = av_vorbis_parse_frame(s->vp, avpkt->data, avpkt->size); 359 if (duration > 0) { 360 /* we do not know encoder delay until we get the first packet from 361 * libvorbis, so we have to update the AudioFrameQueue counts */ 362 if (!avctx->initial_padding && s->afq.frames) { 363 avctx->initial_padding = duration; 364 av_assert0(!s->afq.remaining_delay); 365 s->afq.frames->duration += duration; 366 if (s->afq.frames->pts != AV_NOPTS_VALUE) 367 s->afq.frames->pts -= duration; 368 s->afq.remaining_samples += duration; 369 } 370 ff_af_queue_remove(&s->afq, duration, &avpkt->pts, &avpkt->duration); 371 } 372 373 *got_packet_ptr = 1; 374 return 0; 375 } 376 377 const FFCodec ff_libvorbis_encoder = { 378 .p.name = "libvorbis", 379 CODEC_LONG_NAME("libvorbis"), 380 .p.type = AVMEDIA_TYPE_AUDIO, 381 .p.id = AV_CODEC_ID_VORBIS, 382 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | 383 AV_CODEC_CAP_SMALL_LAST_FRAME, 384 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE, 385 .priv_data_size = sizeof(LibvorbisEncContext), 386 .init = libvorbis_encode_init, 387 FF_CODEC_ENCODE_CB(libvorbis_encode_frame), 388 .close = libvorbis_encode_close, 389 .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP, 390 AV_SAMPLE_FMT_NONE }, 391 .p.priv_class = &vorbis_class, 392 .defaults = defaults, 393 .p.wrapper_name = "libvorbis", 394 };