• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 #include <shine/layer3.h>
23 
24 #include "libavutil/channel_layout.h"
25 #include "libavutil/intreadwrite.h"
26 #include "audio_frame_queue.h"
27 #include "avcodec.h"
28 #include "codec_internal.h"
29 #include "encode.h"
30 #include "mpegaudio.h"
31 #include "mpegaudiodecheader.h"
32 
33 #define BUFFER_SIZE (4096 * 20)
34 
35 typedef struct SHINEContext {
36     shine_config_t  config;
37     shine_t         shine;
38     uint8_t         buffer[BUFFER_SIZE];
39     int             buffer_index;
40     AudioFrameQueue afq;
41 } SHINEContext;
42 
libshine_encode_init(AVCodecContext * avctx)43 static av_cold int libshine_encode_init(AVCodecContext *avctx)
44 {
45     SHINEContext *s = avctx->priv_data;
46 
47     if (avctx->ch_layout.nb_channels <= 0 || avctx->ch_layout.nb_channels > 2){
48         av_log(avctx, AV_LOG_ERROR, "only mono or stereo is supported\n");
49         return AVERROR(EINVAL);
50     }
51 
52     shine_set_config_mpeg_defaults(&s->config.mpeg);
53     if (avctx->bit_rate)
54         s->config.mpeg.bitr = avctx->bit_rate / 1000;
55     s->config.mpeg.mode = avctx->ch_layout.nb_channels == 2 ? STEREO : MONO;
56     s->config.wave.samplerate = avctx->sample_rate;
57     s->config.wave.channels   = avctx->ch_layout.nb_channels == 2 ? PCM_STEREO : PCM_MONO;
58     if (shine_check_config(s->config.wave.samplerate, s->config.mpeg.bitr) < 0) {
59         av_log(avctx, AV_LOG_ERROR, "invalid configuration\n");
60         return AVERROR(EINVAL);
61     }
62     s->shine = shine_initialise(&s->config);
63     if (!s->shine)
64         return AVERROR(ENOMEM);
65     avctx->frame_size = shine_samples_per_pass(s->shine);
66     ff_af_queue_init(avctx, &s->afq);
67     return 0;
68 }
69 
libshine_encode_frame(AVCodecContext * avctx,AVPacket * avpkt,const AVFrame * frame,int * got_packet_ptr)70 static int libshine_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
71                                  const AVFrame *frame, int *got_packet_ptr)
72 {
73     SHINEContext *s = avctx->priv_data;
74     MPADecodeHeader hdr;
75     unsigned char *data;
76     int written;
77     int ret, len;
78 
79     if (frame)
80         data = shine_encode_buffer(s->shine, (int16_t **)frame->data, &written);
81     else
82         data = shine_flush(s->shine, &written);
83     if (written < 0)
84         return -1;
85     if (written > 0) {
86         if (s->buffer_index + written > BUFFER_SIZE) {
87             av_log(avctx, AV_LOG_ERROR, "internal buffer too small\n");
88             return AVERROR_BUG;
89         }
90         memcpy(s->buffer + s->buffer_index, data, written);
91         s->buffer_index += written;
92     }
93     if (frame) {
94         if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
95             return ret;
96     }
97 
98     if (s->buffer_index < 4 || !s->afq.frame_count)
99         return 0;
100     if (avpriv_mpegaudio_decode_header(&hdr, AV_RB32(s->buffer))) {
101         av_log(avctx, AV_LOG_ERROR, "free format output not supported\n");
102         return -1;
103     }
104 
105     len = hdr.frame_size;
106     if (len <= s->buffer_index) {
107         if ((ret = ff_get_encode_buffer(avctx, avpkt, len, 0)))
108             return ret;
109         memcpy(avpkt->data, s->buffer, len);
110         s->buffer_index -= len;
111         memmove(s->buffer, s->buffer + len, s->buffer_index);
112 
113         ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
114                            &avpkt->duration);
115 
116         *got_packet_ptr = 1;
117     }
118     return 0;
119 }
120 
libshine_encode_close(AVCodecContext * avctx)121 static av_cold int libshine_encode_close(AVCodecContext *avctx)
122 {
123     SHINEContext *s = avctx->priv_data;
124 
125     ff_af_queue_close(&s->afq);
126     shine_close(s->shine);
127     return 0;
128 }
129 
130 static const int libshine_sample_rates[] = {
131     44100, 48000, 32000, 0
132 };
133 
134 const FFCodec ff_libshine_encoder = {
135     .p.name                = "libshine",
136     .p.long_name           = NULL_IF_CONFIG_SMALL("libshine MP3 (MPEG audio layer 3)"),
137     .p.type                = AVMEDIA_TYPE_AUDIO,
138     .p.id                  = AV_CODEC_ID_MP3,
139     .p.capabilities        = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
140     .priv_data_size        = sizeof(SHINEContext),
141     .init                  = libshine_encode_init,
142     FF_CODEC_ENCODE_CB(libshine_encode_frame),
143     .close                 = libshine_encode_close,
144     .p.sample_fmts         = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16P,
145                                                             AV_SAMPLE_FMT_NONE },
146     .p.supported_samplerates = libshine_sample_rates,
147 #if FF_API_OLD_CHANNEL_LAYOUT
148     .p.channel_layouts     = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
149                                                   AV_CH_LAYOUT_STEREO,
150                                                   0 },
151 #endif
152     .p.ch_layouts          = (const AVChannelLayout[]) { AV_CHANNEL_LAYOUT_MONO,
153                                                          AV_CHANNEL_LAYOUT_STEREO,
154                                                          { 0 },
155     },
156     .p.wrapper_name        = "libshine",
157 };
158