]> git.sesse.net Git - vlc/blob - modules/codec/shine.c
shine: avoid double free
[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             p_sys->p_buffer = NULL;
180         }
181
182         memcpy( p_pcm_block->p_buffer + i_buffer,
183                     p_block->p_buffer, p_sys->samples_per_frame * 4 - i_buffer );
184         p_block->p_buffer += p_sys->samples_per_frame * 4 - i_buffer;
185
186         p_block->i_buffer -= p_sys->samples_per_frame * 4 - i_buffer;
187
188         block_FifoPut( p_sys->p_fifo, p_pcm_block );
189     }
190
191     /* We hadn't enough data to make a block, put it in standby */
192     if( p_block->i_buffer )
193     {
194         uint8_t *p_tmp;
195
196         if( p_sys->i_buffer > 0 )
197             p_tmp = realloc( p_sys->p_buffer, p_block->i_buffer + p_sys->i_buffer );
198         else
199             p_tmp = malloc( p_block->i_buffer );
200
201         if( !p_tmp )
202         {
203             p_sys->i_buffer = 0;
204             free( p_sys->p_buffer );
205             p_sys->p_buffer = NULL;
206             return NULL;
207         }
208         p_sys->p_buffer = p_tmp;
209         memcpy( p_sys->p_buffer + p_sys->i_buffer,
210                     p_block->p_buffer, p_block->i_buffer );
211
212         p_sys->i_buffer += p_block->i_buffer;
213         p_block->i_buffer = 0;
214     }
215
216 buffered:
217     /* and finally get a block back */
218     return block_FifoCount( p_sys->p_fifo ) > 0 ? block_FifoGet( p_sys->p_fifo ) : NULL;
219 }
220
221 static block_t *EncodeFrame( encoder_t *p_enc, block_t *p_block )
222 {
223     if (!p_block) /* TODO: flush */
224         return NULL;
225
226     encoder_sys_t *p_sys = p_enc->p_sys;
227     block_t *p_pcm_block;
228     block_t *p_chain = NULL;
229     unsigned int i_samples = p_block->i_buffer >> 2 /* s16l stereo */;
230     mtime_t start_date = p_block->i_pts;
231     start_date -= (mtime_t)i_samples * (mtime_t)1000000 / (mtime_t)p_enc->fmt_out.audio.i_rate;
232
233     VLC_UNUSED(p_enc);
234
235     do {
236         p_pcm_block = GetPCM( p_enc, p_block );
237         if( !p_pcm_block )
238             break;
239
240         p_block = NULL; /* we don't need it anymore */
241         int16_t pcm_planar_buf[SHINE_MAX_SAMPLES * 2];
242         int16_t *pcm_planar_buf_chans[2] = {
243             &pcm_planar_buf[0],
244             &pcm_planar_buf[p_sys->samples_per_frame],
245         };
246         aout_Deinterleave( pcm_planar_buf, p_pcm_block->p_buffer,
247                 p_sys->samples_per_frame, p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.i_codec);
248
249         long written;
250         unsigned char *buf = shine_encode_buffer(p_sys->s, pcm_planar_buf_chans, &written);
251         block_Release( p_pcm_block );
252
253         if (written <= 0)
254             break;
255
256         block_t *p_mp3_block = block_Alloc( written );
257         if( !p_mp3_block )
258             break;
259
260         memcpy( p_mp3_block->p_buffer, buf, written );
261
262         /* date management */
263         p_mp3_block->i_length = p_sys->samples_per_frame * 1000000 /
264             p_enc->fmt_out.audio.i_rate;
265
266         start_date += p_mp3_block->i_length;
267         p_mp3_block->i_dts = p_mp3_block->i_pts = start_date;
268
269         p_mp3_block->i_nb_samples = p_sys->samples_per_frame;
270
271         block_ChainAppend( &p_chain, p_mp3_block );
272
273     } while( p_pcm_block );
274
275     return p_chain;
276 }
277
278 static void CloseEncoder( vlc_object_t *p_this )
279 {
280     encoder_sys_t *p_sys = ((encoder_t*)p_this)->p_sys;
281
282     vlc_mutex_lock( &entrant.lock );
283     entrant.busy = false;
284     vlc_mutex_unlock( &entrant.lock );
285
286     /* TODO: we should send the last PCM block padded with 0
287      * But we don't know if other blocks will come before it's too late */
288     if( p_sys->i_buffer )
289         free( p_sys->p_buffer );
290
291     shine_close(p_sys->s);
292
293     block_FifoRelease( p_sys->p_fifo );
294     free( p_sys );
295 }