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.

104 lines
2.6KB

  1. /*
  2. * Interface to libmp3lame for mp3 encoding
  3. * Copyright (c) 2002 Lennert Buytenhek <buytenh@gnu.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 first_frame;
  25. int stereo;
  26. } Mp3AudioContext;
  27. static int MP3lame_encode_init(AVCodecContext *avctx)
  28. {
  29. Mp3AudioContext *s = avctx->priv_data;
  30. if (avctx->channels > 2)
  31. return -1;
  32. s->first_frame = 1;
  33. s->stereo = avctx->channels > 1 ? 1 : 0;
  34. if ((s->gfp = lame_init()) == NULL)
  35. goto err;
  36. lame_set_in_samplerate(s->gfp, avctx->sample_rate);
  37. lame_set_out_samplerate(s->gfp, avctx->sample_rate);
  38. lame_set_num_channels(s->gfp, avctx->channels);
  39. /* lame 3.91 dies on quality != 5 */
  40. lame_set_quality(s->gfp, 5);
  41. /* lame 3.91 doesn't work in mono */
  42. lame_set_mode(s->gfp, JOINT_STEREO);
  43. lame_set_brate(s->gfp, avctx->bit_rate/1000);
  44. if (lame_init_params(s->gfp) < 0)
  45. goto err_close;
  46. avctx->frame_size = MPA_FRAME_SIZE;
  47. avctx->key_frame = 1;
  48. return 0;
  49. err_close:
  50. lame_close(s->gfp);
  51. err:
  52. return -1;
  53. }
  54. int MP3lame_encode_frame(AVCodecContext *avctx,
  55. unsigned char *frame, int buf_size, void *data)
  56. {
  57. Mp3AudioContext *s = avctx->priv_data;
  58. int num;
  59. /* lame 3.91 dies on '1-channel interleaved' data */
  60. if (s->stereo) {
  61. num = lame_encode_buffer_interleaved(s->gfp, data,
  62. MPA_FRAME_SIZE, frame, buf_size);
  63. } else {
  64. num = lame_encode_buffer(s->gfp, data, data, MPA_FRAME_SIZE,
  65. frame, buf_size);
  66. }
  67. /* lame 3.91 outputs the first frame as garbage */
  68. if (s->first_frame)
  69. s->first_frame = num = 0;
  70. return num;
  71. }
  72. int MP3lame_encode_close(AVCodecContext *avctx)
  73. {
  74. Mp3AudioContext *s = avctx->priv_data;
  75. lame_close(s->gfp);
  76. return 0;
  77. }
  78. AVCodec mp3lame_encoder = {
  79. "mp3",
  80. CODEC_TYPE_AUDIO,
  81. CODEC_ID_MP3LAME,
  82. sizeof(Mp3AudioContext),
  83. MP3lame_encode_init,
  84. MP3lame_encode_frame,
  85. MP3lame_encode_close
  86. };