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.

108 lines
2.7KB

  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. /**
  20. * @file mp3lameaudio.c
  21. * Interface to libmp3lame for mp3 encoding.
  22. */
  23. #include "avcodec.h"
  24. #include "mpegaudio.h"
  25. #include <lame/lame.h>
  26. typedef struct Mp3AudioContext {
  27. lame_global_flags *gfp;
  28. int stereo;
  29. } Mp3AudioContext;
  30. static int MP3lame_encode_init(AVCodecContext *avctx)
  31. {
  32. Mp3AudioContext *s = avctx->priv_data;
  33. if (avctx->channels > 2)
  34. return -1;
  35. s->stereo = avctx->channels > 1 ? 1 : 0;
  36. if ((s->gfp = lame_init()) == NULL)
  37. goto err;
  38. lame_set_in_samplerate(s->gfp, avctx->sample_rate);
  39. lame_set_out_samplerate(s->gfp, avctx->sample_rate);
  40. lame_set_num_channels(s->gfp, avctx->channels);
  41. /* lame 3.91 dies on quality != 5 */
  42. lame_set_quality(s->gfp, 5);
  43. /* lame 3.91 doesn't work in mono */
  44. lame_set_mode(s->gfp, JOINT_STEREO);
  45. lame_set_brate(s->gfp, avctx->bit_rate/1000);
  46. if (lame_init_params(s->gfp) < 0)
  47. goto err_close;
  48. avctx->frame_size = MPA_FRAME_SIZE;
  49. avctx->coded_frame= avcodec_alloc_frame();
  50. avctx->coded_frame->key_frame= 1;
  51. return 0;
  52. err_close:
  53. lame_close(s->gfp);
  54. err:
  55. return -1;
  56. }
  57. int MP3lame_encode_frame(AVCodecContext *avctx,
  58. unsigned char *frame, int buf_size, void *data)
  59. {
  60. Mp3AudioContext *s = avctx->priv_data;
  61. int num;
  62. /* lame 3.91 dies on '1-channel interleaved' data */
  63. if (s->stereo) {
  64. num = lame_encode_buffer_interleaved(s->gfp, data,
  65. MPA_FRAME_SIZE, frame, buf_size);
  66. } else {
  67. num = lame_encode_buffer(s->gfp, data, data, MPA_FRAME_SIZE,
  68. frame, buf_size);
  69. }
  70. return num;
  71. }
  72. int MP3lame_encode_close(AVCodecContext *avctx)
  73. {
  74. Mp3AudioContext *s = avctx->priv_data;
  75. av_freep(&avctx->coded_frame);
  76. lame_close(s->gfp);
  77. return 0;
  78. }
  79. AVCodec mp3lame_encoder = {
  80. "mp3",
  81. CODEC_TYPE_AUDIO,
  82. CODEC_ID_MP3,
  83. sizeof(Mp3AudioContext),
  84. MP3lame_encode_init,
  85. MP3lame_encode_frame,
  86. MP3lame_encode_close
  87. };