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.

305 lines
9.8KB

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