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.

322 lines
11KB

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