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.

366 lines
13KB

  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. * Vorbis encoding support via libvorbisenc.
  23. * @author Mark Hills <mark@pogo.org.uk>
  24. */
  25. #include <vorbis/vorbisenc.h>
  26. #include "libavutil/fifo.h"
  27. #include "libavutil/opt.h"
  28. #include "avcodec.h"
  29. #include "bytestream.h"
  30. #include "internal.h"
  31. #include "vorbis.h"
  32. #undef NDEBUG
  33. #include <assert.h>
  34. /* Number of samples the user should send in each call.
  35. * This value is used because it is the LCD of all possible frame sizes, so
  36. * an output packet will always start at the same point as one of the input
  37. * packets.
  38. */
  39. #define OGGVORBIS_FRAME_SIZE 64
  40. #define BUFFER_SIZE (1024 * 64)
  41. typedef struct OggVorbisContext {
  42. AVClass *av_class; /**< class for AVOptions */
  43. vorbis_info vi; /**< vorbis_info used during init */
  44. vorbis_dsp_state vd; /**< DSP state used for analysis */
  45. vorbis_block vb; /**< vorbis_block used for analysis */
  46. AVFifoBuffer *pkt_fifo; /**< output packet buffer */
  47. int eof; /**< end-of-file flag */
  48. int dsp_initialized; /**< vd has been initialized */
  49. vorbis_comment vc; /**< VorbisComment info */
  50. ogg_packet op; /**< ogg packet */
  51. double iblock; /**< impulse block bias option */
  52. } OggVorbisContext;
  53. static const AVOption options[] = {
  54. { "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 },
  55. { NULL }
  56. };
  57. static const AVCodecDefault defaults[] = {
  58. { "b", "0" },
  59. { NULL },
  60. };
  61. static const AVClass class = { "libvorbis", av_default_item_name, options, LIBAVUTIL_VERSION_INT };
  62. static int vorbis_error_to_averror(int ov_err)
  63. {
  64. switch (ov_err) {
  65. case OV_EFAULT: return AVERROR_BUG;
  66. case OV_EINVAL: return AVERROR(EINVAL);
  67. case OV_EIMPL: return AVERROR(EINVAL);
  68. default: return AVERROR_UNKNOWN;
  69. }
  70. }
  71. static av_cold int oggvorbis_init_encoder(vorbis_info *vi,
  72. AVCodecContext *avctx)
  73. {
  74. OggVorbisContext *s = avctx->priv_data;
  75. double cfreq;
  76. int ret;
  77. if (avctx->flags & CODEC_FLAG_QSCALE || !avctx->bit_rate) {
  78. /* variable bitrate
  79. * NOTE: we use the oggenc range of -1 to 10 for global_quality for
  80. * user convenience, but libvorbis uses -0.1 to 1.0.
  81. */
  82. float q = avctx->global_quality / (float)FF_QP2LAMBDA;
  83. /* default to 3 if the user did not set quality or bitrate */
  84. if (!(avctx->flags & CODEC_FLAG_QSCALE))
  85. q = 3.0;
  86. if ((ret = vorbis_encode_setup_vbr(vi, avctx->channels,
  87. avctx->sample_rate,
  88. q / 10.0)))
  89. goto error;
  90. } else {
  91. int minrate = avctx->rc_min_rate > 0 ? avctx->rc_min_rate : -1;
  92. int maxrate = avctx->rc_max_rate > 0 ? avctx->rc_max_rate : -1;
  93. /* average bitrate */
  94. if ((ret = vorbis_encode_setup_managed(vi, avctx->channels,
  95. avctx->sample_rate, maxrate,
  96. avctx->bit_rate, minrate)))
  97. goto error;
  98. /* variable bitrate by estimate, disable slow rate management */
  99. if (minrate == -1 && maxrate == -1)
  100. if ((ret = vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL)))
  101. goto error; /* should not happen */
  102. }
  103. /* cutoff frequency */
  104. if (avctx->cutoff > 0) {
  105. cfreq = avctx->cutoff / 1000.0;
  106. if ((ret = vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq)))
  107. goto error; /* should not happen */
  108. }
  109. /* impulse block bias */
  110. if (s->iblock) {
  111. if ((ret = vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &s->iblock)))
  112. goto error;
  113. }
  114. if (avctx->channels == 3 &&
  115. avctx->channel_layout != (AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER) ||
  116. avctx->channels == 4 &&
  117. avctx->channel_layout != AV_CH_LAYOUT_2_2 &&
  118. avctx->channel_layout != AV_CH_LAYOUT_QUAD ||
  119. avctx->channels == 5 &&
  120. avctx->channel_layout != AV_CH_LAYOUT_5POINT0 &&
  121. avctx->channel_layout != AV_CH_LAYOUT_5POINT0_BACK ||
  122. avctx->channels == 6 &&
  123. avctx->channel_layout != AV_CH_LAYOUT_5POINT1 &&
  124. avctx->channel_layout != AV_CH_LAYOUT_5POINT1_BACK ||
  125. avctx->channels == 7 &&
  126. avctx->channel_layout != (AV_CH_LAYOUT_5POINT1|AV_CH_BACK_CENTER) ||
  127. avctx->channels == 8 &&
  128. avctx->channel_layout != AV_CH_LAYOUT_7POINT1) {
  129. if (avctx->channel_layout) {
  130. char name[32];
  131. av_get_channel_layout_string(name, sizeof(name), avctx->channels,
  132. avctx->channel_layout);
  133. av_log(avctx, AV_LOG_ERROR, "%s not supported by Vorbis: "
  134. "output stream will have incorrect "
  135. "channel layout.\n", name);
  136. } else {
  137. av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The encoder "
  138. "will use Vorbis channel layout for "
  139. "%d channels.\n", avctx->channels);
  140. }
  141. }
  142. if ((ret = vorbis_encode_setup_init(vi)))
  143. goto error;
  144. return 0;
  145. error:
  146. return vorbis_error_to_averror(ret);
  147. }
  148. /* How many bytes are needed for a buffer of length 'l' */
  149. static int xiph_len(int l)
  150. {
  151. return 1 + l / 255 + l;
  152. }
  153. static av_cold int oggvorbis_encode_close(AVCodecContext *avctx)
  154. {
  155. OggVorbisContext *s = avctx->priv_data;
  156. /* notify vorbisenc this is EOF */
  157. if (s->dsp_initialized)
  158. vorbis_analysis_wrote(&s->vd, 0);
  159. vorbis_block_clear(&s->vb);
  160. vorbis_dsp_clear(&s->vd);
  161. vorbis_info_clear(&s->vi);
  162. av_fifo_free(s->pkt_fifo);
  163. av_freep(&avctx->coded_frame);
  164. av_freep(&avctx->extradata);
  165. return 0;
  166. }
  167. static av_cold int oggvorbis_encode_init(AVCodecContext *avctx)
  168. {
  169. OggVorbisContext *s = avctx->priv_data;
  170. ogg_packet header, header_comm, header_code;
  171. uint8_t *p;
  172. unsigned int offset;
  173. int ret;
  174. vorbis_info_init(&s->vi);
  175. if ((ret = oggvorbis_init_encoder(&s->vi, avctx))) {
  176. av_log(avctx, AV_LOG_ERROR, "encoder setup failed\n");
  177. goto error;
  178. }
  179. if ((ret = vorbis_analysis_init(&s->vd, &s->vi))) {
  180. av_log(avctx, AV_LOG_ERROR, "analysis init failed\n");
  181. ret = vorbis_error_to_averror(ret);
  182. goto error;
  183. }
  184. s->dsp_initialized = 1;
  185. if ((ret = vorbis_block_init(&s->vd, &s->vb))) {
  186. av_log(avctx, AV_LOG_ERROR, "dsp init failed\n");
  187. ret = vorbis_error_to_averror(ret);
  188. goto error;
  189. }
  190. vorbis_comment_init(&s->vc);
  191. vorbis_comment_add_tag(&s->vc, "encoder", LIBAVCODEC_IDENT);
  192. if ((ret = vorbis_analysis_headerout(&s->vd, &s->vc, &header, &header_comm,
  193. &header_code))) {
  194. ret = vorbis_error_to_averror(ret);
  195. goto error;
  196. }
  197. avctx->extradata_size = 1 + xiph_len(header.bytes) +
  198. xiph_len(header_comm.bytes) +
  199. header_code.bytes;
  200. p = avctx->extradata = av_malloc(avctx->extradata_size +
  201. FF_INPUT_BUFFER_PADDING_SIZE);
  202. if (!p) {
  203. ret = AVERROR(ENOMEM);
  204. goto error;
  205. }
  206. p[0] = 2;
  207. offset = 1;
  208. offset += av_xiphlacing(&p[offset], header.bytes);
  209. offset += av_xiphlacing(&p[offset], header_comm.bytes);
  210. memcpy(&p[offset], header.packet, header.bytes);
  211. offset += header.bytes;
  212. memcpy(&p[offset], header_comm.packet, header_comm.bytes);
  213. offset += header_comm.bytes;
  214. memcpy(&p[offset], header_code.packet, header_code.bytes);
  215. offset += header_code.bytes;
  216. assert(offset == avctx->extradata_size);
  217. vorbis_comment_clear(&s->vc);
  218. avctx->frame_size = OGGVORBIS_FRAME_SIZE;
  219. s->pkt_fifo = av_fifo_alloc(BUFFER_SIZE);
  220. if (!s->pkt_fifo) {
  221. ret = AVERROR(ENOMEM);
  222. goto error;
  223. }
  224. avctx->coded_frame = avcodec_alloc_frame();
  225. if (!avctx->coded_frame) {
  226. ret = AVERROR(ENOMEM);
  227. goto error;
  228. }
  229. return 0;
  230. error:
  231. oggvorbis_encode_close(avctx);
  232. return ret;
  233. }
  234. static int oggvorbis_encode_frame(AVCodecContext *avctx, unsigned char *packets,
  235. int buf_size, void *data)
  236. {
  237. OggVorbisContext *s = avctx->priv_data;
  238. ogg_packet op;
  239. float *audio = data;
  240. int pkt_size, ret;
  241. /* send samples to libvorbis */
  242. if (data) {
  243. const int samples = avctx->frame_size;
  244. float **buffer;
  245. int c, channels = s->vi.channels;
  246. buffer = vorbis_analysis_buffer(&s->vd, samples);
  247. for (c = 0; c < channels; c++) {
  248. int i;
  249. int co = (channels > 8) ? c :
  250. ff_vorbis_encoding_channel_layout_offsets[channels - 1][c];
  251. for (i = 0; i < samples; i++)
  252. buffer[c][i] = audio[i * channels + co];
  253. }
  254. if ((ret = vorbis_analysis_wrote(&s->vd, samples)) < 0) {
  255. av_log(avctx, AV_LOG_ERROR, "error in vorbis_analysis_wrote()\n");
  256. return vorbis_error_to_averror(ret);
  257. }
  258. } else {
  259. if (!s->eof)
  260. if ((ret = vorbis_analysis_wrote(&s->vd, 0)) < 0) {
  261. av_log(avctx, AV_LOG_ERROR, "error in vorbis_analysis_wrote()\n");
  262. return vorbis_error_to_averror(ret);
  263. }
  264. s->eof = 1;
  265. }
  266. /* retrieve available packets from libvorbis */
  267. while ((ret = vorbis_analysis_blockout(&s->vd, &s->vb)) == 1) {
  268. if ((ret = vorbis_analysis(&s->vb, NULL)) < 0)
  269. break;
  270. if ((ret = vorbis_bitrate_addblock(&s->vb)) < 0)
  271. break;
  272. /* add any available packets to the output packet buffer */
  273. while ((ret = vorbis_bitrate_flushpacket(&s->vd, &op)) == 1) {
  274. if (av_fifo_space(s->pkt_fifo) < sizeof(ogg_packet) + op.bytes) {
  275. av_log(avctx, AV_LOG_ERROR, "packet buffer is too small\n");
  276. return AVERROR_BUG;
  277. }
  278. av_fifo_generic_write(s->pkt_fifo, &op, sizeof(ogg_packet), NULL);
  279. av_fifo_generic_write(s->pkt_fifo, op.packet, op.bytes, NULL);
  280. }
  281. if (ret < 0) {
  282. av_log(avctx, AV_LOG_ERROR, "error getting available packets\n");
  283. break;
  284. }
  285. }
  286. if (ret < 0) {
  287. av_log(avctx, AV_LOG_ERROR, "error getting available packets\n");
  288. return vorbis_error_to_averror(ret);
  289. }
  290. /* output then next packet from the output buffer, if available */
  291. pkt_size = 0;
  292. if (av_fifo_size(s->pkt_fifo) >= sizeof(ogg_packet)) {
  293. av_fifo_generic_read(s->pkt_fifo, &op, sizeof(ogg_packet), NULL);
  294. pkt_size = op.bytes;
  295. // FIXME: we should use the user-supplied pts and duration
  296. avctx->coded_frame->pts = ff_samples_to_time_base(avctx,
  297. op.granulepos);
  298. if (pkt_size > buf_size) {
  299. av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
  300. return AVERROR(EINVAL);
  301. }
  302. av_fifo_generic_read(s->pkt_fifo, packets, pkt_size, NULL);
  303. }
  304. return pkt_size;
  305. }
  306. AVCodec ff_libvorbis_encoder = {
  307. .name = "libvorbis",
  308. .type = AVMEDIA_TYPE_AUDIO,
  309. .id = CODEC_ID_VORBIS,
  310. .priv_data_size = sizeof(OggVorbisContext),
  311. .init = oggvorbis_encode_init,
  312. .encode = oggvorbis_encode_frame,
  313. .close = oggvorbis_encode_close,
  314. .capabilities = CODEC_CAP_DELAY,
  315. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
  316. AV_SAMPLE_FMT_NONE },
  317. .long_name = NULL_IF_CONFIG_SMALL("libvorbis Vorbis"),
  318. .priv_class = &class,
  319. .defaults = defaults,
  320. };