You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

337 lines
11KB

  1. /*
  2. * copyright (c) 2002 Mark Hills <mark@pogo.org.uk>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Vorbis encoding support via libvorbisenc.
  23. * @author Mark Hills <mark@pogo.org.uk>
  24. */
  25. #include <vorbis/vorbisenc.h>
  26. #include "libavutil/fifo.h"
  27. #include "libavutil/opt.h"
  28. #include "avcodec.h"
  29. #include "bytestream.h"
  30. #include "internal.h"
  31. #include "vorbis.h"
  32. #undef NDEBUG
  33. #include <assert.h>
  34. /* Number of samples the user should send in each call.
  35. * This value is used because it is the LCD of all possible frame sizes, so
  36. * an output packet will always start at the same point as one of the input
  37. * packets.
  38. */
  39. #define OGGVORBIS_FRAME_SIZE 64
  40. #define BUFFER_SIZE (1024 * 64)
  41. typedef struct OggVorbisContext {
  42. AVClass *av_class; /**< class for AVOptions */
  43. vorbis_info vi; /**< vorbis_info used during init */
  44. vorbis_dsp_state vd; /**< DSP state used for analysis */
  45. vorbis_block vb; /**< vorbis_block used for analysis */
  46. AVFifoBuffer *pkt_fifo; /**< output packet buffer */
  47. int eof; /**< end-of-file flag */
  48. int dsp_initialized; /**< vd has been initialized */
  49. vorbis_comment vc; /**< VorbisComment info */
  50. ogg_packet op; /**< ogg packet */
  51. double iblock; /**< impulse block bias option */
  52. } OggVorbisContext;
  53. static const AVOption options[] = {
  54. { "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 },
  55. { NULL }
  56. };
  57. static const AVCodecDefault defaults[] = {
  58. { "b", "0" },
  59. { NULL },
  60. };
  61. static const AVClass class = { "libvorbis", av_default_item_name, options, LIBAVUTIL_VERSION_INT };
  62. static int vorbis_error_to_averror(int ov_err)
  63. {
  64. switch (ov_err) {
  65. case OV_EFAULT: return AVERROR_BUG;
  66. case OV_EINVAL: return AVERROR(EINVAL);
  67. case OV_EIMPL: return AVERROR(EINVAL);
  68. default: return AVERROR_UNKNOWN;
  69. }
  70. }
  71. static av_cold int oggvorbis_init_encoder(vorbis_info *vi,
  72. AVCodecContext *avctx)
  73. {
  74. OggVorbisContext *s = avctx->priv_data;
  75. double cfreq;
  76. int ret;
  77. if (avctx->flags & CODEC_FLAG_QSCALE || !avctx->bit_rate) {
  78. /* variable bitrate
  79. * NOTE: we use the oggenc range of -1 to 10 for global_quality for
  80. * user convenience, but libvorbis uses -0.1 to 1.0.
  81. */
  82. float q = avctx->global_quality / (float)FF_QP2LAMBDA;
  83. /* default to 3 if the user did not set quality or bitrate */
  84. if (!(avctx->flags & CODEC_FLAG_QSCALE))
  85. q = 3.0;
  86. if ((ret = vorbis_encode_setup_vbr(vi, avctx->channels,
  87. avctx->sample_rate,
  88. q / 10.0)))
  89. goto error;
  90. } else {
  91. int minrate = avctx->rc_min_rate > 0 ? avctx->rc_min_rate : -1;
  92. int maxrate = avctx->rc_max_rate > 0 ? avctx->rc_max_rate : -1;
  93. /* average bitrate */
  94. if ((ret = vorbis_encode_setup_managed(vi, avctx->channels,
  95. avctx->sample_rate, maxrate,
  96. avctx->bit_rate, minrate)))
  97. goto error;
  98. /* variable bitrate by estimate, disable slow rate management */
  99. if (minrate == -1 && maxrate == -1)
  100. if ((ret = vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL)))
  101. goto error;
  102. }
  103. /* cutoff frequency */
  104. if (avctx->cutoff > 0) {
  105. cfreq = avctx->cutoff / 1000.0;
  106. if ((ret = vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq)))
  107. goto error;
  108. }
  109. /* impulse block bias */
  110. if (s->iblock) {
  111. if ((ret = vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &s->iblock)))
  112. goto error;
  113. }
  114. if ((ret = vorbis_encode_setup_init(vi)))
  115. goto error;
  116. return 0;
  117. error:
  118. return vorbis_error_to_averror(ret);
  119. }
  120. /* How many bytes are needed for a buffer of length 'l' */
  121. static int xiph_len(int l)
  122. {
  123. return 1 + l / 255 + l;
  124. }
  125. static av_cold int oggvorbis_encode_close(AVCodecContext *avctx)
  126. {
  127. OggVorbisContext *s = avctx->priv_data;
  128. /* notify vorbisenc this is EOF */
  129. if (s->dsp_initialized)
  130. vorbis_analysis_wrote(&s->vd, 0);
  131. vorbis_block_clear(&s->vb);
  132. vorbis_dsp_clear(&s->vd);
  133. vorbis_info_clear(&s->vi);
  134. av_fifo_free(s->pkt_fifo);
  135. av_freep(&avctx->coded_frame);
  136. av_freep(&avctx->extradata);
  137. return 0;
  138. }
  139. static av_cold int oggvorbis_encode_init(AVCodecContext *avctx)
  140. {
  141. OggVorbisContext *s = avctx->priv_data;
  142. ogg_packet header, header_comm, header_code;
  143. uint8_t *p;
  144. unsigned int offset;
  145. int ret;
  146. vorbis_info_init(&s->vi);
  147. if ((ret = oggvorbis_init_encoder(&s->vi, avctx))) {
  148. av_log(avctx, AV_LOG_ERROR, "encoder setup failed\n");
  149. goto error;
  150. }
  151. if ((ret = vorbis_analysis_init(&s->vd, &s->vi))) {
  152. av_log(avctx, AV_LOG_ERROR, "analysis init failed\n");
  153. ret = vorbis_error_to_averror(ret);
  154. goto error;
  155. }
  156. s->dsp_initialized = 1;
  157. if ((ret = vorbis_block_init(&s->vd, &s->vb))) {
  158. av_log(avctx, AV_LOG_ERROR, "dsp init failed\n");
  159. ret = vorbis_error_to_averror(ret);
  160. goto error;
  161. }
  162. vorbis_comment_init(&s->vc);
  163. vorbis_comment_add_tag(&s->vc, "encoder", LIBAVCODEC_IDENT);
  164. if ((ret = vorbis_analysis_headerout(&s->vd, &s->vc, &header, &header_comm,
  165. &header_code))) {
  166. ret = vorbis_error_to_averror(ret);
  167. goto error;
  168. }
  169. avctx->extradata_size = 1 + xiph_len(header.bytes) +
  170. xiph_len(header_comm.bytes) +
  171. header_code.bytes;
  172. p = avctx->extradata = av_malloc(avctx->extradata_size +
  173. FF_INPUT_BUFFER_PADDING_SIZE);
  174. if (!p) {
  175. ret = AVERROR(ENOMEM);
  176. goto error;
  177. }
  178. p[0] = 2;
  179. offset = 1;
  180. offset += av_xiphlacing(&p[offset], header.bytes);
  181. offset += av_xiphlacing(&p[offset], header_comm.bytes);
  182. memcpy(&p[offset], header.packet, header.bytes);
  183. offset += header.bytes;
  184. memcpy(&p[offset], header_comm.packet, header_comm.bytes);
  185. offset += header_comm.bytes;
  186. memcpy(&p[offset], header_code.packet, header_code.bytes);
  187. offset += header_code.bytes;
  188. assert(offset == avctx->extradata_size);
  189. vorbis_comment_clear(&s->vc);
  190. avctx->frame_size = OGGVORBIS_FRAME_SIZE;
  191. s->pkt_fifo = av_fifo_alloc(BUFFER_SIZE);
  192. if (!s->pkt_fifo) {
  193. ret = AVERROR(ENOMEM);
  194. goto error;
  195. }
  196. avctx->coded_frame = avcodec_alloc_frame();
  197. if (!avctx->coded_frame) {
  198. ret = AVERROR(ENOMEM);
  199. goto error;
  200. }
  201. return 0;
  202. error:
  203. oggvorbis_encode_close(avctx);
  204. return ret;
  205. }
  206. static int oggvorbis_encode_frame(AVCodecContext *avctx, unsigned char *packets,
  207. int buf_size, void *data)
  208. {
  209. OggVorbisContext *s = avctx->priv_data;
  210. ogg_packet op;
  211. float *audio = data;
  212. int pkt_size, ret;
  213. /* send samples to libvorbis */
  214. if (data) {
  215. const int samples = avctx->frame_size;
  216. float **buffer;
  217. int c, channels = s->vi.channels;
  218. buffer = vorbis_analysis_buffer(&s->vd, samples);
  219. for (c = 0; c < channels; c++) {
  220. int i;
  221. int co = (channels > 8) ? c :
  222. ff_vorbis_encoding_channel_layout_offsets[channels - 1][c];
  223. for (i = 0; i < samples; i++)
  224. buffer[c][i] = audio[i * channels + co];
  225. }
  226. if ((ret = vorbis_analysis_wrote(&s->vd, samples)) < 0) {
  227. av_log(avctx, AV_LOG_ERROR, "error in vorbis_analysis_wrote()\n");
  228. return vorbis_error_to_averror(ret);
  229. }
  230. } else {
  231. if (!s->eof)
  232. if ((ret = vorbis_analysis_wrote(&s->vd, 0)) < 0) {
  233. av_log(avctx, AV_LOG_ERROR, "error in vorbis_analysis_wrote()\n");
  234. return vorbis_error_to_averror(ret);
  235. }
  236. s->eof = 1;
  237. }
  238. /* retrieve available packets from libvorbis */
  239. while ((ret = vorbis_analysis_blockout(&s->vd, &s->vb)) == 1) {
  240. if ((ret = vorbis_analysis(&s->vb, NULL)) < 0)
  241. break;
  242. if ((ret = vorbis_bitrate_addblock(&s->vb)) < 0)
  243. break;
  244. /* add any available packets to the output packet buffer */
  245. while ((ret = vorbis_bitrate_flushpacket(&s->vd, &op)) == 1) {
  246. if (av_fifo_space(s->pkt_fifo) < sizeof(ogg_packet) + op.bytes) {
  247. av_log(avctx, AV_LOG_ERROR, "packet buffer is too small");
  248. return AVERROR_BUG;
  249. }
  250. av_fifo_generic_write(s->pkt_fifo, &op, sizeof(ogg_packet), NULL);
  251. av_fifo_generic_write(s->pkt_fifo, op.packet, op.bytes, NULL);
  252. }
  253. if (ret < 0) {
  254. av_log(avctx, AV_LOG_ERROR, "error getting available packets\n");
  255. break;
  256. }
  257. }
  258. if (ret < 0) {
  259. av_log(avctx, AV_LOG_ERROR, "error getting available packets\n");
  260. return vorbis_error_to_averror(ret);
  261. }
  262. /* output then next packet from the output buffer, if available */
  263. pkt_size = 0;
  264. if (av_fifo_size(s->pkt_fifo) >= sizeof(ogg_packet)) {
  265. av_fifo_generic_read(s->pkt_fifo, &op, sizeof(ogg_packet), NULL);
  266. pkt_size = op.bytes;
  267. // FIXME: we should use the user-supplied pts and duration
  268. avctx->coded_frame->pts = ff_samples_to_time_base(avctx,
  269. op.granulepos);
  270. if (pkt_size > buf_size) {
  271. av_log(avctx, AV_LOG_ERROR, "output buffer is too small");
  272. return AVERROR(EINVAL);
  273. }
  274. av_fifo_generic_read(s->pkt_fifo, packets, pkt_size, NULL);
  275. }
  276. return pkt_size;
  277. }
  278. AVCodec ff_libvorbis_encoder = {
  279. .name = "libvorbis",
  280. .type = AVMEDIA_TYPE_AUDIO,
  281. .id = CODEC_ID_VORBIS,
  282. .priv_data_size = sizeof(OggVorbisContext),
  283. .init = oggvorbis_encode_init,
  284. .encode = oggvorbis_encode_frame,
  285. .close = oggvorbis_encode_close,
  286. .capabilities = CODEC_CAP_DELAY,
  287. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
  288. AV_SAMPLE_FMT_NONE },
  289. .long_name = NULL_IF_CONFIG_SMALL("libvorbis Vorbis"),
  290. .priv_class = &class,
  291. .defaults = defaults,
  292. };