2 * Interface to libshine for mp3 encoding
3 * Copyright (c) 2012 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
22 #include <shine/layer3.h>
24 #include "libavutil/intreadwrite.h"
25 #include "audio_frame_queue.h"
28 #include "mpegaudio.h"
29 #include "mpegaudiodecheader.h"
31 #define BUFFER_SIZE (4096 * 20)
33 typedef struct SHINEContext {
34 shine_config_t config;
36 uint8_t buffer[BUFFER_SIZE];
41 static av_cold int libshine_encode_init(AVCodecContext *avctx)
43 SHINEContext *s = avctx->priv_data;
45 if (avctx->channels <= 0 || avctx->channels > 2){
46 av_log(avctx, AV_LOG_ERROR, "only mono or stereo is supported\n");
47 return AVERROR(EINVAL);
50 shine_set_config_mpeg_defaults(&s->config.mpeg);
52 s->config.mpeg.bitr = avctx->bit_rate / 1000;
53 s->config.mpeg.mode = avctx->channels == 2 ? STEREO : MONO;
54 s->config.wave.samplerate = avctx->sample_rate;
55 s->config.wave.channels = avctx->channels == 2 ? PCM_STEREO : PCM_MONO;
56 if (shine_check_config(s->config.wave.samplerate, s->config.mpeg.bitr) < 0) {
57 av_log(avctx, AV_LOG_ERROR, "invalid configuration\n");
58 return AVERROR(EINVAL);
60 s->shine = shine_initialise(&s->config);
62 return AVERROR(ENOMEM);
63 avctx->frame_size = shine_samples_per_pass(s->shine);
64 ff_af_queue_init(avctx, &s->afq);
68 static int libshine_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
69 const AVFrame *frame, int *got_packet_ptr)
71 SHINEContext *s = avctx->priv_data;
78 data = shine_encode_buffer(s->shine, (int16_t **)frame->data, &written);
80 data = shine_flush(s->shine, &written);
84 if (s->buffer_index + written > BUFFER_SIZE) {
85 av_log(avctx, AV_LOG_ERROR, "internal buffer too small\n");
88 memcpy(s->buffer + s->buffer_index, data, written);
89 s->buffer_index += written;
92 if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
96 if (s->buffer_index < 4 || !s->afq.frame_count)
98 if (avpriv_mpegaudio_decode_header(&hdr, AV_RB32(s->buffer))) {
99 av_log(avctx, AV_LOG_ERROR, "free format output not supported\n");
103 len = hdr.frame_size;
104 if (len <= s->buffer_index) {
105 if ((ret = ff_alloc_packet2(avctx, avpkt, len, 0)))
107 memcpy(avpkt->data, s->buffer, len);
108 s->buffer_index -= len;
109 memmove(s->buffer, s->buffer + len, s->buffer_index);
111 ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
120 static av_cold int libshine_encode_close(AVCodecContext *avctx)
122 SHINEContext *s = avctx->priv_data;
124 ff_af_queue_close(&s->afq);
125 shine_close(s->shine);
129 static const int libshine_sample_rates[] = {
130 44100, 48000, 32000, 0
133 AVCodec ff_libshine_encoder = {
135 .long_name = NULL_IF_CONFIG_SMALL("libshine MP3 (MPEG audio layer 3)"),
136 .type = AVMEDIA_TYPE_AUDIO,
137 .id = AV_CODEC_ID_MP3,
138 .priv_data_size = sizeof(SHINEContext),
139 .init = libshine_encode_init,
140 .encode2 = libshine_encode_frame,
141 .close = libshine_encode_close,
142 .capabilities = AV_CODEC_CAP_DELAY,
143 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16P,
144 AV_SAMPLE_FMT_NONE },
145 .supported_samplerates = libshine_sample_rates,
146 .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,