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.

328 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/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. 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_freep(&avctx->coded_frame);
  135. av_freep(&avctx->extradata);
  136. return 0;
  137. }
  138. static av_cold int oggvorbis_encode_init(AVCodecContext *avctx)
  139. {
  140. OggVorbisContext *s = avctx->priv_data;
  141. ogg_packet header, header_comm, header_code;
  142. uint8_t *p;
  143. unsigned int offset;
  144. int ret;
  145. vorbis_info_init(&s->vi);
  146. if ((ret = oggvorbis_init_encoder(&s->vi, avctx))) {
  147. av_log(avctx, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed\n");
  148. goto error;
  149. }
  150. if ((ret = vorbis_analysis_init(&s->vd, &s->vi))) {
  151. ret = vorbis_error_to_averror(ret);
  152. goto error;
  153. }
  154. s->dsp_initialized = 1;
  155. if ((ret = vorbis_block_init(&s->vd, &s->vb))) {
  156. ret = vorbis_error_to_averror(ret);
  157. goto error;
  158. }
  159. vorbis_comment_init(&s->vc);
  160. vorbis_comment_add_tag(&s->vc, "encoder", LIBAVCODEC_IDENT);
  161. if ((ret = vorbis_analysis_headerout(&s->vd, &s->vc, &header, &header_comm,
  162. &header_code))) {
  163. ret = vorbis_error_to_averror(ret);
  164. goto error;
  165. }
  166. avctx->extradata_size = 1 + xiph_len(header.bytes) +
  167. xiph_len(header_comm.bytes) +
  168. header_code.bytes;
  169. p = avctx->extradata = av_malloc(avctx->extradata_size +
  170. FF_INPUT_BUFFER_PADDING_SIZE);
  171. if (!p) {
  172. ret = AVERROR(ENOMEM);
  173. goto error;
  174. }
  175. p[0] = 2;
  176. offset = 1;
  177. offset += av_xiphlacing(&p[offset], header.bytes);
  178. offset += av_xiphlacing(&p[offset], header_comm.bytes);
  179. memcpy(&p[offset], header.packet, header.bytes);
  180. offset += header.bytes;
  181. memcpy(&p[offset], header_comm.packet, header_comm.bytes);
  182. offset += header_comm.bytes;
  183. memcpy(&p[offset], header_code.packet, header_code.bytes);
  184. offset += header_code.bytes;
  185. assert(offset == avctx->extradata_size);
  186. vorbis_comment_clear(&s->vc);
  187. avctx->frame_size = OGGVORBIS_FRAME_SIZE;
  188. avctx->coded_frame = avcodec_alloc_frame();
  189. if (!avctx->coded_frame) {
  190. ret = AVERROR(ENOMEM);
  191. goto error;
  192. }
  193. return 0;
  194. error:
  195. oggvorbis_encode_close(avctx);
  196. return ret;
  197. }
  198. static int oggvorbis_encode_frame(AVCodecContext *avctx, unsigned char *packets,
  199. int buf_size, void *data)
  200. {
  201. OggVorbisContext *s = avctx->priv_data;
  202. ogg_packet op;
  203. float *audio = data;
  204. int pkt_size, ret;
  205. /* send samples to libvorbis */
  206. if (data) {
  207. const int samples = avctx->frame_size;
  208. float **buffer;
  209. int c, channels = s->vi.channels;
  210. buffer = vorbis_analysis_buffer(&s->vd, samples);
  211. for (c = 0; c < channels; c++) {
  212. int i;
  213. int co = (channels > 8) ? c :
  214. ff_vorbis_encoding_channel_layout_offsets[channels - 1][c];
  215. for (i = 0; i < samples; i++)
  216. buffer[c][i] = audio[i * channels + co];
  217. }
  218. if ((ret = vorbis_analysis_wrote(&s->vd, samples)) < 0)
  219. return vorbis_error_to_averror(ret);
  220. } else {
  221. if (!s->eof)
  222. if ((ret = vorbis_analysis_wrote(&s->vd, 0)) < 0)
  223. return vorbis_error_to_averror(ret);
  224. s->eof = 1;
  225. }
  226. /* retrieve available packets from libvorbis */
  227. while ((ret = vorbis_analysis_blockout(&s->vd, &s->vb)) == 1) {
  228. if ((ret = vorbis_analysis(&s->vb, NULL)) < 0)
  229. break;
  230. if ((ret = vorbis_bitrate_addblock(&s->vb)) < 0)
  231. break;
  232. /* add any available packets to the output packet buffer */
  233. while ((ret = vorbis_bitrate_flushpacket(&s->vd, &op)) == 1) {
  234. if (s->buffer_index + sizeof(ogg_packet) + op.bytes > BUFFER_SIZE) {
  235. av_log(avctx, AV_LOG_ERROR, "libvorbis: buffer overflow.");
  236. return -1;
  237. }
  238. memcpy(s->buffer + s->buffer_index, &op, sizeof(ogg_packet));
  239. s->buffer_index += sizeof(ogg_packet);
  240. memcpy(s->buffer + s->buffer_index, op.packet, op.bytes);
  241. s->buffer_index += op.bytes;
  242. }
  243. if (ret < 0)
  244. break;
  245. }
  246. if (ret < 0)
  247. return vorbis_error_to_averror(ret);
  248. /* output then next packet from the output buffer, if available */
  249. pkt_size = 0;
  250. if (s->buffer_index) {
  251. ogg_packet *op2 = (ogg_packet *)s->buffer;
  252. op2->packet = s->buffer + sizeof(ogg_packet);
  253. pkt_size = op2->bytes;
  254. // FIXME: we should use the user-supplied pts and duration
  255. avctx->coded_frame->pts = ff_samples_to_time_base(avctx,
  256. op2->granulepos);
  257. if (pkt_size > buf_size) {
  258. av_log(avctx, AV_LOG_ERROR, "libvorbis: buffer overflow.");
  259. return -1;
  260. }
  261. memcpy(packets, op2->packet, pkt_size);
  262. s->buffer_index -= pkt_size + sizeof(ogg_packet);
  263. memmove(s->buffer, s->buffer + pkt_size + sizeof(ogg_packet),
  264. s->buffer_index);
  265. }
  266. return pkt_size;
  267. }
  268. AVCodec ff_libvorbis_encoder = {
  269. .name = "libvorbis",
  270. .type = AVMEDIA_TYPE_AUDIO,
  271. .id = CODEC_ID_VORBIS,
  272. .priv_data_size = sizeof(OggVorbisContext),
  273. .init = oggvorbis_encode_init,
  274. .encode = oggvorbis_encode_frame,
  275. .close = oggvorbis_encode_close,
  276. .capabilities = CODEC_CAP_DELAY,
  277. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
  278. AV_SAMPLE_FMT_NONE },
  279. .long_name = NULL_IF_CONFIG_SMALL("libvorbis Vorbis"),
  280. .priv_class = &class,
  281. .defaults = defaults,
  282. };