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.

295 lines
10KB

  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. * @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 "vorbis.h"
  30. #include "libavutil/mathematics.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 av_cold int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext)
  54. {
  55. OggVorbisContext *context = avccontext->priv_data;
  56. double cfreq;
  57. if (avccontext->flags & CODEC_FLAG_QSCALE) {
  58. /* variable bitrate */
  59. if (vorbis_encode_setup_vbr(vi, avccontext->channels,
  60. avccontext->sample_rate,
  61. avccontext->global_quality / (float)FF_QP2LAMBDA / 10.0))
  62. return -1;
  63. } else {
  64. int minrate = avccontext->rc_min_rate > 0 ? avccontext->rc_min_rate : -1;
  65. int maxrate = avccontext->rc_min_rate > 0 ? avccontext->rc_max_rate : -1;
  66. /* constant bitrate */
  67. if (vorbis_encode_setup_managed(vi, avccontext->channels,
  68. avccontext->sample_rate, minrate,
  69. avccontext->bit_rate, maxrate))
  70. return -1;
  71. /* variable bitrate by estimate, disable slow rate management */
  72. if (minrate == -1 && maxrate == -1)
  73. if (vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL))
  74. return -1;
  75. }
  76. /* cutoff frequency */
  77. if (avccontext->cutoff > 0) {
  78. cfreq = avccontext->cutoff / 1000.0;
  79. if (vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq))
  80. return -1;
  81. }
  82. if (context->iblock) {
  83. vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &context->iblock);
  84. }
  85. if (avccontext->channels == 3 &&
  86. avccontext->channel_layout != (AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER) ||
  87. avccontext->channels == 4 &&
  88. avccontext->channel_layout != AV_CH_LAYOUT_2_2 &&
  89. avccontext->channel_layout != AV_CH_LAYOUT_QUAD ||
  90. avccontext->channels == 5 &&
  91. avccontext->channel_layout != AV_CH_LAYOUT_5POINT0 &&
  92. avccontext->channel_layout != AV_CH_LAYOUT_5POINT0_BACK ||
  93. avccontext->channels == 6 &&
  94. avccontext->channel_layout != AV_CH_LAYOUT_5POINT1 &&
  95. avccontext->channel_layout != AV_CH_LAYOUT_5POINT1_BACK ||
  96. avccontext->channels == 7 &&
  97. avccontext->channel_layout != (AV_CH_LAYOUT_5POINT1|AV_CH_BACK_CENTER) ||
  98. avccontext->channels == 8 &&
  99. avccontext->channel_layout != AV_CH_LAYOUT_7POINT1) {
  100. if (avccontext->channel_layout) {
  101. char name[32];
  102. av_get_channel_layout_string(name, sizeof(name), avccontext->channels,
  103. avccontext->channel_layout);
  104. av_log(avccontext, AV_LOG_ERROR, "%s not supported by Vorbis: "
  105. "output stream will have incorrect "
  106. "channel layout.\n", name);
  107. } else {
  108. av_log(avccontext, AV_LOG_WARNING, "No channel layout specified. The encoder "
  109. "will use Vorbis channel layout for "
  110. "%d channels.\n", avccontext->channels);
  111. }
  112. }
  113. return vorbis_encode_setup_init(vi);
  114. }
  115. /* How many bytes are needed for a buffer of length 'l' */
  116. static int xiph_len(int l)
  117. {
  118. return 1 + l / 255 + l;
  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. vorbis_info_init(&context->vi);
  127. if (oggvorbis_init_encoder(&context->vi, avccontext) < 0) {
  128. av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed\n");
  129. return -1;
  130. }
  131. vorbis_analysis_init(&context->vd, &context->vi);
  132. vorbis_block_init(&context->vd, &context->vb);
  133. vorbis_comment_init(&context->vc);
  134. vorbis_comment_add_tag(&context->vc, "encoder", LIBAVCODEC_IDENT);
  135. vorbis_analysis_headerout(&context->vd, &context->vc, &header,
  136. &header_comm, &header_code);
  137. avccontext->extradata_size =
  138. 1 + xiph_len(header.bytes) + xiph_len(header_comm.bytes) +
  139. header_code.bytes;
  140. p = avccontext->extradata =
  141. av_malloc(avccontext->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  142. p[0] = 2;
  143. offset = 1;
  144. offset += av_xiphlacing(&p[offset], header.bytes);
  145. offset += av_xiphlacing(&p[offset], header_comm.bytes);
  146. memcpy(&p[offset], header.packet, header.bytes);
  147. offset += header.bytes;
  148. memcpy(&p[offset], header_comm.packet, header_comm.bytes);
  149. offset += header_comm.bytes;
  150. memcpy(&p[offset], header_code.packet, header_code.bytes);
  151. offset += header_code.bytes;
  152. assert(offset == avccontext->extradata_size);
  153. #if 0
  154. vorbis_block_clear(&context->vb);
  155. vorbis_dsp_clear(&context->vd);
  156. vorbis_info_clear(&context->vi);
  157. #endif
  158. vorbis_comment_clear(&context->vc);
  159. avccontext->frame_size = OGGVORBIS_FRAME_SIZE;
  160. avccontext->coded_frame = avcodec_alloc_frame();
  161. avccontext->coded_frame->key_frame = 1;
  162. return 0;
  163. }
  164. static int oggvorbis_encode_frame(AVCodecContext *avccontext,
  165. unsigned char *packets,
  166. int buf_size, void *data)
  167. {
  168. OggVorbisContext *context = avccontext->priv_data;
  169. ogg_packet op;
  170. signed short *audio = data;
  171. int l;
  172. if (data) {
  173. const int samples = avccontext->frame_size;
  174. float **buffer;
  175. int c, channels = context->vi.channels;
  176. buffer = vorbis_analysis_buffer(&context->vd, samples);
  177. for (c = 0; c < channels; c++) {
  178. int co = (channels > 8) ? c :
  179. ff_vorbis_encoding_channel_layout_offsets[channels - 1][c];
  180. for (l = 0; l < samples; l++)
  181. buffer[c][l] = audio[l * channels + co] / 32768.f;
  182. }
  183. vorbis_analysis_wrote(&context->vd, samples);
  184. } else {
  185. if (!context->eof)
  186. vorbis_analysis_wrote(&context->vd, 0);
  187. context->eof = 1;
  188. }
  189. while (vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
  190. vorbis_analysis(&context->vb, NULL);
  191. vorbis_bitrate_addblock(&context->vb);
  192. while (vorbis_bitrate_flushpacket(&context->vd, &op)) {
  193. /* i'd love to say the following line is a hack, but sadly it's
  194. * not, apparently the end of stream decision is in libogg. */
  195. if (op.bytes == 1 && op.e_o_s)
  196. continue;
  197. if (context->buffer_index + sizeof(ogg_packet) + op.bytes > BUFFER_SIZE) {
  198. av_log(avccontext, AV_LOG_ERROR, "libvorbis: buffer overflow.");
  199. return -1;
  200. }
  201. memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet));
  202. context->buffer_index += sizeof(ogg_packet);
  203. memcpy(context->buffer + context->buffer_index, op.packet, op.bytes);
  204. context->buffer_index += op.bytes;
  205. // av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes);
  206. }
  207. }
  208. l = 0;
  209. if (context->buffer_index) {
  210. ogg_packet *op2 = (ogg_packet *)context->buffer;
  211. op2->packet = context->buffer + sizeof(ogg_packet);
  212. l = op2->bytes;
  213. avccontext->coded_frame->pts = av_rescale_q(op2->granulepos, (AVRational) { 1, avccontext->sample_rate }, avccontext->time_base);
  214. //FIXME we should reorder the user supplied pts and not assume that they are spaced by 1/sample_rate
  215. if (l > buf_size) {
  216. av_log(avccontext, AV_LOG_ERROR, "libvorbis: buffer overflow.");
  217. return -1;
  218. }
  219. memcpy(packets, op2->packet, l);
  220. context->buffer_index -= l + sizeof(ogg_packet);
  221. memmove(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index);
  222. // av_log(avccontext, AV_LOG_DEBUG, "E%d\n", l);
  223. }
  224. return l;
  225. }
  226. static av_cold int oggvorbis_encode_close(AVCodecContext *avccontext)
  227. {
  228. OggVorbisContext *context = avccontext->priv_data;
  229. /* ogg_packet op ; */
  230. vorbis_analysis_wrote(&context->vd, 0); /* notify vorbisenc this is EOF */
  231. vorbis_block_clear(&context->vb);
  232. vorbis_dsp_clear(&context->vd);
  233. vorbis_info_clear(&context->vi);
  234. av_freep(&avccontext->coded_frame);
  235. av_freep(&avccontext->extradata);
  236. return 0;
  237. }
  238. AVCodec ff_libvorbis_encoder = {
  239. .name = "libvorbis",
  240. .type = AVMEDIA_TYPE_AUDIO,
  241. .id = CODEC_ID_VORBIS,
  242. .priv_data_size = sizeof(OggVorbisContext),
  243. .init = oggvorbis_encode_init,
  244. .encode = oggvorbis_encode_frame,
  245. .close = oggvorbis_encode_close,
  246. .capabilities = CODEC_CAP_DELAY,
  247. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
  248. .long_name = NULL_IF_CONFIG_SMALL("libvorbis Vorbis"),
  249. .priv_class = &class,
  250. };