]> git.sesse.net Git - vlc/blob - modules/codec/shine.c
e65b737df26a5d24caf11cbe1a8c46d3aa096d64
[vlc] / modules / codec / shine.c
1 /*****************************************************************************
2  * shine_mod.c: MP3 encoder using Shine, a fixed point implementation
3  *****************************************************************************
4  * Copyright (C) 2008-2009 M2X
5  *
6  * Authors: Rafaël Carré <rcarre@m2x.nl>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_codec.h>
33 #include <vlc_block.h>
34 #include <vlc_block_helper.h>
35 #include <vlc_bits.h>
36 #include <vlc_aout.h>
37
38 #include <assert.h>
39 #include <inttypes.h>
40
41 #include <shine/layer3.h>
42
43 struct encoder_sys_t
44 {
45     shine_t s;
46     unsigned int samples_per_frame;
47     block_fifo_t *p_fifo;
48
49     unsigned int i_buffer;
50     uint8_t *p_buffer;
51 };
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56 static int  OpenEncoder   ( vlc_object_t * );
57 static void CloseEncoder  ( vlc_object_t * );
58
59 static block_t *EncodeFrame  ( encoder_t *, block_t * );
60
61 vlc_module_begin();
62     set_category( CAT_INPUT );
63     set_subcategory( SUBCAT_INPUT_ACODEC );
64     set_description( _("MP3 fixed point audio encoder") );
65     set_capability( "encoder", 50 );
66     set_callbacks( OpenEncoder, CloseEncoder );
67 vlc_module_end();
68
69 static struct
70 {
71     bool busy;
72     vlc_mutex_t lock;
73 } entrant = { false, VLC_STATIC_MUTEX, };
74
75 static int OpenEncoder( vlc_object_t *p_this )
76 {
77     encoder_t *p_enc = (encoder_t*)p_this;
78     encoder_sys_t *p_sys;
79
80     /* shine is an 'MP3' encoder */
81     if( (p_enc->fmt_out.i_codec != VLC_CODEC_MP3 && p_enc->fmt_out.i_codec != VLC_CODEC_MPGA) ||
82         p_enc->fmt_out.audio.i_channels > 2 )
83         return VLC_EGENERIC;
84
85     /* Shine is strict on its input */
86     if( p_enc->fmt_in.audio.i_channels != 2 )
87     {
88         msg_Err( p_enc, "Only stereo input is accepted, rejecting %d channels",
89             p_enc->fmt_in.audio.i_channels );
90         return VLC_EGENERIC;
91     }
92
93     if( p_enc->fmt_out.i_bitrate <= 0 )
94     {
95         msg_Err( p_enc, "unknown bitrate" );
96         return VLC_EGENERIC;
97     }
98
99     msg_Dbg( p_enc, "bitrate %d, samplerate %d, channels %d",
100              p_enc->fmt_out.i_bitrate, p_enc->fmt_out.audio.i_rate,
101              p_enc->fmt_out.audio.i_channels );
102
103     vlc_mutex_lock( &entrant.lock );
104     if( entrant.busy )
105     {
106         msg_Err( p_enc, "encoder already in progress" );
107         vlc_mutex_unlock( &entrant.lock );
108         return VLC_EGENERIC;
109     }
110     entrant.busy = true;
111     vlc_mutex_unlock( &entrant.lock );
112
113     p_enc->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
114     if( !p_sys )
115         goto enomem;
116
117     if( !( p_sys->p_fifo = block_FifoNew() ) )
118     {
119         free( p_sys );
120         goto enomem;
121     }
122
123     shine_config_t cfg = {
124         .wave = {
125             .channels = p_enc->fmt_out.audio.i_channels,
126             .samplerate = p_enc->fmt_out.audio.i_rate,
127         },
128     };
129
130     shine_set_config_mpeg_defaults(&cfg.mpeg);
131     cfg.mpeg.bitr = p_enc->fmt_out.i_bitrate / 1000;
132  
133     if (shine_check_config(cfg.wave.samplerate, cfg.mpeg.bitr) == -1) {
134         msg_Err(p_enc, "Invalid bitrate %d\n", cfg.mpeg.bitr);
135         free(p_sys);
136         return VLC_EGENERIC;
137     }
138
139     p_sys->s = shine_initialise(&cfg);
140     p_sys->samples_per_frame = shine_samples_per_pass(p_sys->s);
141
142     p_enc->pf_encode_audio = EncodeFrame;
143     p_enc->fmt_out.i_cat = AUDIO_ES;
144
145     p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
146
147     return VLC_SUCCESS;
148
149 enomem:
150     vlc_mutex_lock( &entrant.lock );
151     entrant.busy = false;
152     vlc_mutex_unlock( &entrant.lock );
153     return VLC_ENOMEM;
154 }
155
156 /* We split/pack PCM blocks to a fixed size: p_sys->samples_per_frame * 4 bytes */
157 static block_t *GetPCM( encoder_t *p_enc, block_t *p_block )
158 {
159     encoder_sys_t *p_sys = p_enc->p_sys;
160     block_t *p_pcm_block;
161
162     if( !p_block ) goto buffered; /* just return a block if we can */
163
164     /* Put the PCM samples sent by VLC in the Fifo */
165     while( p_sys->i_buffer + p_block->i_buffer >= p_sys->samples_per_frame * 4 )
166     {
167         unsigned int i_buffer = 0;
168         p_pcm_block = block_Alloc( p_sys->samples_per_frame * 4 );
169         if( !p_pcm_block )
170             break;
171
172         if( p_sys->i_buffer )
173         {
174             memcpy( p_pcm_block->p_buffer, p_sys->p_buffer, p_sys->i_buffer );
175
176             i_buffer = p_sys->i_buffer;
177             p_sys->i_buffer = 0;
178             free( p_sys->p_buffer );
179         }
180
181         memcpy( p_pcm_block->p_buffer + i_buffer,
182                     p_block->p_buffer, p_sys->samples_per_frame * 4 - i_buffer );
183         p_block->p_buffer += p_sys->samples_per_frame * 4 - i_buffer;
184
185         p_block->i_buffer -= p_sys->samples_per_frame * 4 - i_buffer;
186
187         block_FifoPut( p_sys->p_fifo, p_pcm_block );
188     }
189
190     /* We hadn't enough data to make a block, put it in standby */
191     if( p_block->i_buffer )
192     {
193         uint8_t *p_tmp;
194
195         if( p_sys->i_buffer > 0 )
196             p_tmp = realloc( p_sys->p_buffer, p_block->i_buffer + p_sys->i_buffer );
197         else
198             p_tmp = malloc( p_block->i_buffer );
199
200         if( !p_tmp )
201         {
202             p_sys->i_buffer = 0;
203             free( p_sys->p_buffer );
204             p_sys->p_buffer = NULL;
205             return NULL;
206         }
207         p_sys->p_buffer = p_tmp;
208         memcpy( p_sys->p_buffer + p_sys->i_buffer,
209                     p_block->p_buffer, p_block->i_buffer );
210
211         p_sys->i_buffer += p_block->i_buffer;
212         p_block->i_buffer = 0;
213     }
214
215 buffered:
216     /* and finally get a block back */
217     return block_FifoCount( p_sys->p_fifo ) > 0 ? block_FifoGet( p_sys->p_fifo ) : NULL;
218 }
219
220 static block_t *EncodeFrame( encoder_t *p_enc, block_t *p_block )
221 {
222     if (!p_block) /* TODO: flush */
223         return NULL;
224
225     encoder_sys_t *p_sys = p_enc->p_sys;
226     block_t *p_pcm_block;
227     block_t *p_chain = NULL;
228     unsigned int i_samples = p_block->i_buffer >> 2 /* s16l stereo */;
229     mtime_t start_date = p_block->i_pts;
230     start_date -= (mtime_t)i_samples * (mtime_t)1000000 / (mtime_t)p_enc->fmt_out.audio.i_rate;
231
232     VLC_UNUSED(p_enc);
233
234     do {
235         p_pcm_block = GetPCM( p_enc, p_block );
236         if( !p_pcm_block )
237             break;
238
239         p_block = NULL; /* we don't need it anymore */
240         int16_t pcm_planar_buf[SHINE_MAX_SAMPLES * 2];
241         int16_t *pcm_planar_buf_chans[2] = {
242             &pcm_planar_buf[0],
243             &pcm_planar_buf[p_sys->samples_per_frame],
244         };
245         aout_Deinterleave( pcm_planar_buf, p_pcm_block->p_buffer,
246                 p_sys->samples_per_frame, p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.i_codec);
247
248         long written;
249         unsigned char *buf = shine_encode_buffer(p_sys->s, pcm_planar_buf_chans, &written);
250         block_Release( p_pcm_block );
251
252         if (written <= 0)
253             break;
254
255         block_t *p_mp3_block = block_Alloc( written );
256         if( !p_mp3_block )
257             break;
258
259         memcpy( p_mp3_block->p_buffer, buf, written );
260
261         /* date management */
262         p_mp3_block->i_length = p_sys->samples_per_frame * 1000000 /
263             p_enc->fmt_out.audio.i_rate;
264
265         start_date += p_mp3_block->i_length;
266         p_mp3_block->i_dts = p_mp3_block->i_pts = start_date;
267
268         p_mp3_block->i_nb_samples = p_sys->samples_per_frame;
269
270         block_ChainAppend( &p_chain, p_mp3_block );
271
272     } while( p_pcm_block );
273
274     return p_chain;
275 }
276
277 static void CloseEncoder( vlc_object_t *p_this )
278 {
279     encoder_sys_t *p_sys = ((encoder_t*)p_this)->p_sys;
280
281     vlc_mutex_lock( &entrant.lock );
282     entrant.busy = false;
283     vlc_mutex_unlock( &entrant.lock );
284
285     /* TODO: we should send the last PCM block padded with 0
286      * But we don't know if other blocks will come before it's too late */
287     if( p_sys->i_buffer )
288         free( p_sys->p_buffer );
289
290     shine_close(p_sys->s);
291
292     block_FifoRelease( p_sys->p_fifo );
293     free( p_sys );
294 }