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.

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