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.

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