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.

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