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.

103 lines
2.6KB

  1. /*
  2. * Interface to libmp3lame for mp3 encoding
  3. * Copyright (c) 2002 Lennert Buytenhek <buytenh@gnu.org>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. #include "mpegaudio.h"
  21. #include <lame/lame.h>
  22. typedef struct Mp3AudioContext {
  23. lame_global_flags *gfp;
  24. int stereo;
  25. } Mp3AudioContext;
  26. static int MP3lame_encode_init(AVCodecContext *avctx)
  27. {
  28. Mp3AudioContext *s = avctx->priv_data;
  29. if (avctx->channels > 2)
  30. return -1;
  31. s->stereo = avctx->channels > 1 ? 1 : 0;
  32. if ((s->gfp = lame_init()) == NULL)
  33. goto err;
  34. lame_set_in_samplerate(s->gfp, avctx->sample_rate);
  35. lame_set_out_samplerate(s->gfp, avctx->sample_rate);
  36. lame_set_num_channels(s->gfp, avctx->channels);
  37. /* lame 3.91 dies on quality != 5 */
  38. lame_set_quality(s->gfp, 5);
  39. /* lame 3.91 doesn't work in mono */
  40. lame_set_mode(s->gfp, JOINT_STEREO);
  41. lame_set_brate(s->gfp, avctx->bit_rate/1000);
  42. if (lame_init_params(s->gfp) < 0)
  43. goto err_close;
  44. avctx->frame_size = MPA_FRAME_SIZE;
  45. avctx->coded_frame= avcodec_alloc_frame();
  46. avctx->coded_frame->key_frame= 1;
  47. return 0;
  48. err_close:
  49. lame_close(s->gfp);
  50. err:
  51. return -1;
  52. }
  53. int MP3lame_encode_frame(AVCodecContext *avctx,
  54. unsigned char *frame, int buf_size, void *data)
  55. {
  56. Mp3AudioContext *s = avctx->priv_data;
  57. int num;
  58. /* lame 3.91 dies on '1-channel interleaved' data */
  59. if (s->stereo) {
  60. num = lame_encode_buffer_interleaved(s->gfp, data,
  61. MPA_FRAME_SIZE, frame, buf_size);
  62. } else {
  63. num = lame_encode_buffer(s->gfp, data, data, MPA_FRAME_SIZE,
  64. frame, buf_size);
  65. }
  66. return num;
  67. }
  68. int MP3lame_encode_close(AVCodecContext *avctx)
  69. {
  70. Mp3AudioContext *s = avctx->priv_data;
  71. av_freep(&avctx->coded_frame);
  72. lame_close(s->gfp);
  73. return 0;
  74. }
  75. AVCodec mp3lame_encoder = {
  76. "mp3",
  77. CODEC_TYPE_AUDIO,
  78. CODEC_ID_MP3LAME,
  79. sizeof(Mp3AudioContext),
  80. MP3lame_encode_init,
  81. MP3lame_encode_frame,
  82. MP3lame_encode_close
  83. };