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.

150 lines
4.9KB

  1. /*
  2. * Interface to libshine for mp3 encoding
  3. * Copyright (c) 2012 Paul B Mahol
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <shine/layer3.h>
  22. #include "libavutil/intreadwrite.h"
  23. #include "audio_frame_queue.h"
  24. #include "avcodec.h"
  25. #include "internal.h"
  26. #include "mpegaudio.h"
  27. #include "mpegaudiodecheader.h"
  28. #define BUFFER_SIZE (4096 * 20)
  29. typedef struct SHINEContext {
  30. shine_config_t config;
  31. shine_t shine;
  32. uint8_t buffer[BUFFER_SIZE];
  33. int buffer_index;
  34. AudioFrameQueue afq;
  35. } SHINEContext;
  36. static av_cold int libshine_encode_init(AVCodecContext *avctx)
  37. {
  38. SHINEContext *s = avctx->priv_data;
  39. if (avctx->channels <= 0 || avctx->channels > 2){
  40. av_log(avctx, AV_LOG_ERROR, "only mono or stereo is supported\n");
  41. return AVERROR(EINVAL);
  42. }
  43. shine_set_config_mpeg_defaults(&s->config.mpeg);
  44. if (avctx->bit_rate)
  45. s->config.mpeg.bitr = avctx->bit_rate / 1000;
  46. if (shine_find_bitrate_index(s->config.mpeg.bitr) < 0) {
  47. av_log(avctx, AV_LOG_ERROR, "invalid bitrate\n");
  48. return AVERROR(EINVAL);
  49. }
  50. s->config.mpeg.mode = avctx->channels == 2 ? STEREO : MONO;
  51. s->config.wave.samplerate = avctx->sample_rate;
  52. s->config.wave.channels = avctx->channels == 2 ? PCM_STEREO : PCM_MONO;
  53. s->shine = shine_initialise(&s->config);
  54. if (!s->shine)
  55. return AVERROR(ENOMEM);
  56. avctx->frame_size = samp_per_frame;
  57. ff_af_queue_init(avctx, &s->afq);
  58. return 0;
  59. }
  60. static int libshine_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  61. const AVFrame *frame, int *got_packet_ptr)
  62. {
  63. SHINEContext *s = avctx->priv_data;
  64. MPADecodeHeader hdr;
  65. unsigned char *data;
  66. long written;
  67. int ret, len;
  68. if (frame)
  69. data = shine_encode_frame(s->shine, frame->data[0], &written);
  70. else
  71. data = shine_flush(s->shine, &written);
  72. if (written < 0)
  73. return -1;
  74. if (written > 0) {
  75. if (s->buffer_index + written > BUFFER_SIZE) {
  76. av_log(avctx, AV_LOG_ERROR, "internal buffer too small\n");
  77. return AVERROR_BUG;
  78. }
  79. memcpy(s->buffer + s->buffer_index, data, written);
  80. s->buffer_index += written;
  81. }
  82. if (frame) {
  83. if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
  84. return ret;
  85. }
  86. if (s->buffer_index < 4 || !s->afq.frame_count)
  87. return 0;
  88. if (avpriv_mpegaudio_decode_header(&hdr, AV_RB32(s->buffer))) {
  89. av_log(avctx, AV_LOG_ERROR, "free format output not supported\n");
  90. return -1;
  91. }
  92. len = hdr.frame_size;
  93. if (len <= s->buffer_index) {
  94. if ((ret = ff_alloc_packet2(avctx, avpkt, len)))
  95. return ret;
  96. memcpy(avpkt->data, s->buffer, len);
  97. s->buffer_index -= len;
  98. memmove(s->buffer, s->buffer + len, s->buffer_index);
  99. ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
  100. &avpkt->duration);
  101. avpkt->size = len;
  102. *got_packet_ptr = 1;
  103. }
  104. return 0;
  105. }
  106. static av_cold int libshine_encode_close(AVCodecContext *avctx)
  107. {
  108. SHINEContext *s = avctx->priv_data;
  109. ff_af_queue_close(&s->afq);
  110. shine_close(s->shine);
  111. return 0;
  112. }
  113. static const int libshine_sample_rates[] = {
  114. 44100, 48000, 32000, 0
  115. };
  116. AVCodec ff_libshine_encoder = {
  117. .name = "libshine",
  118. .type = AVMEDIA_TYPE_AUDIO,
  119. .id = CODEC_ID_MP3,
  120. .priv_data_size = sizeof(SHINEContext),
  121. .init = libshine_encode_init,
  122. .encode2 = libshine_encode_frame,
  123. .close = libshine_encode_close,
  124. .capabilities = CODEC_CAP_DELAY,
  125. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16P,
  126. AV_SAMPLE_FMT_NONE },
  127. .supported_samplerates = libshine_sample_rates,
  128. .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
  129. AV_CH_LAYOUT_STEREO,
  130. 0 },
  131. .long_name = NULL_IF_CONFIG_SMALL("libshine MP3 (MPEG audio layer 3)"),
  132. };