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.

311 lines
10KB

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