]> git.sesse.net Git - vlc/blob - modules/codec/shine/shine_mod.c
Merge branch 1.0-bugfix
[vlc] / modules / codec / shine / shine_mod.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
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 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 General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, 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_aout.h>
34 #include <vlc_block.h>
35 #include <vlc_block_helper.h>
36 #include <vlc_bits.h>
37
38 #include <assert.h>
39 #include <inttypes.h>
40
41 /* shine.c uses a lot of static variables, so we include the C file to keep
42  * the scope.
43  * Note that it makes this decoder non reentrant, this is why we set
44  * b_reentrant to VLC_FALSE in the module initialisation */
45 #include "shine.c"
46
47 struct encoder_sys_t
48 {
49     block_fifo_t *p_fifo;
50
51     unsigned int i_buffer;
52     uint8_t *p_buffer;
53 };
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 static int  OpenEncoder   ( vlc_object_t * );
59 static void CloseEncoder  ( vlc_object_t * );
60
61 static block_t *EncodeFrame  ( encoder_t *, aout_buffer_t * );
62
63 vlc_module_begin();
64     set_category( CAT_INPUT );
65     set_subcategory( SUBCAT_INPUT_ACODEC );
66     set_description( _("MP3 fixed point audio encoder") );
67     set_capability( "encoder", 50 );
68     set_callbacks( OpenEncoder, CloseEncoder );
69 vlc_module_end();
70
71 static int OpenEncoder( vlc_object_t *p_this )
72 {
73     encoder_t *p_enc = (encoder_t*)p_this;
74     encoder_sys_t *p_sys;
75
76     /* FIXME: what about layers 1 and 2 ? shine is an 'MP3' encoder */
77     if( p_enc->fmt_out.i_codec != VLC_CODEC_MP3 ||
78         p_enc->fmt_out.audio.i_channels > 2 )
79         return VLC_EGENERIC;
80
81     /* Shine is strict on its input */
82     if( p_enc->fmt_in.audio.i_channels != 2 )
83     {
84         msg_Err( p_enc, "Only stereo input is accepted, rejecting %d channels",
85             p_enc->fmt_in.audio.i_channels );
86         return VLC_EGENERIC;
87     }
88
89     if( p_enc->fmt_out.i_bitrate <= 0 )
90     {
91         msg_Err( p_enc, "unknown bitrate" );
92         return VLC_EGENERIC;
93     }
94
95     msg_Dbg( p_enc, "bitrate %d, samplerate %d, channels %d",
96              p_enc->fmt_out.i_bitrate, p_enc->fmt_out.audio.i_rate,
97              p_enc->fmt_out.audio.i_channels );
98
99     p_enc->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
100     if( !p_sys )
101         return VLC_ENOMEM;
102
103     if( !( p_sys->p_fifo = block_FifoNew() ) )
104     {
105         free( p_sys );
106         return VLC_ENOMEM;
107     }
108
109     init_mp3_encoder_engine( p_enc->fmt_out.audio.i_rate,
110         p_enc->fmt_out.audio.i_channels, p_enc->fmt_out.i_bitrate / 1000 );
111
112     p_enc->pf_encode_audio = EncodeFrame;
113     p_enc->fmt_out.i_cat = AUDIO_ES;
114
115     return VLC_SUCCESS;
116 }
117
118 /* We split/pack PCM blocks to a fixed size: pcm_chunk_size bytes */
119 static block_t *GetPCM( encoder_t *p_enc, aout_buffer_t *p_block )
120 {
121     encoder_sys_t *p_sys = p_enc->p_sys;
122     block_t *p_pcm_block;
123
124     if( !p_block ) goto buffered; /* just return a block if we can */
125
126     /* Put the PCM samples sent by VLC in the Fifo */
127     while( p_sys->i_buffer + p_block->i_nb_bytes >= pcm_chunk_size )
128     {
129         unsigned int i_buffer = 0;
130         p_pcm_block = block_New( p_enc, pcm_chunk_size );
131         if( !p_pcm_block )
132             break;
133
134         if( p_sys->i_buffer )
135         {
136             vlc_memcpy( p_pcm_block->p_buffer, p_sys->p_buffer, p_sys->i_buffer );
137
138             i_buffer = p_sys->i_buffer;
139             p_sys->i_buffer = 0;
140             free( p_sys->p_buffer );
141         }
142
143         vlc_memcpy( p_pcm_block->p_buffer + i_buffer,
144                     p_block->p_buffer, pcm_chunk_size - i_buffer );
145         p_block->p_buffer += pcm_chunk_size - i_buffer;
146
147         p_block->i_nb_bytes -= pcm_chunk_size - i_buffer;
148
149         block_FifoPut( p_sys->p_fifo, p_pcm_block );
150     }
151
152     /* We hadn't enough data to make a block, put it in standby */
153     if( p_block->i_nb_bytes )
154     {
155         uint8_t *p_tmp;
156
157         if( p_sys->i_buffer > 0 )
158             p_tmp = realloc( p_sys->p_buffer, p_block->i_nb_bytes + p_sys->i_buffer );
159         else
160             p_tmp = malloc( p_block->i_nb_bytes );
161
162         if( !p_tmp )
163         {
164             p_sys->i_buffer = 0;
165             free( p_sys->p_buffer );
166             p_sys->p_buffer = NULL;
167             return NULL;
168         }
169         p_sys->p_buffer = p_tmp;
170         vlc_memcpy( p_sys->p_buffer + p_sys->i_buffer,
171                     p_block->p_buffer, p_block->i_nb_bytes );
172
173         p_sys->i_buffer += p_block->i_nb_bytes;
174         p_block->i_nb_bytes = 0;
175     }
176
177 buffered:
178     /* and finally get a block back */
179     return block_FifoCount( p_sys->p_fifo ) > 0 ? block_FifoGet( p_sys->p_fifo ) : NULL;
180 }
181
182 static block_t *EncodeFrame( encoder_t *p_enc, aout_buffer_t *p_block )
183 {
184     encoder_sys_t *p_sys = (encoder_sys_t *)p_enc->p_sys;
185     block_t *p_pcm_block;
186     block_t *p_chain = NULL;
187     unsigned int i_samples = p_block->i_nb_bytes >> 2 /* s16l stereo */;
188     mtime_t start_date = p_block->start_date;
189     start_date -= (mtime_t)i_samples * (mtime_t)1000000 / (mtime_t)p_enc->fmt_out.audio.i_rate;
190
191     do {
192         p_pcm_block = GetPCM( p_enc, p_block );
193         if( !p_pcm_block )
194             break;
195
196         p_block = NULL; /* we don't need it anymore */
197
198         uint32_t enc_buffer[16384]; /* storage for 65536 Bytes XXX: too much */
199         struct enc_chunk_hdr *chunk = (void*) enc_buffer;
200         chunk->enc_data = ENC_CHUNK_SKIP_HDR(chunk->enc_data, chunk);
201
202         encode_frame( (char*)p_pcm_block->p_buffer, chunk );
203         block_Release( p_pcm_block );
204
205         block_t *p_mp3_block = block_New( p_enc, chunk->enc_size );
206         if( !p_mp3_block )
207             break;
208
209         vlc_memcpy( p_mp3_block->p_buffer, chunk->enc_data, chunk->enc_size );
210
211         /* date management */
212         p_mp3_block->i_length = SAMP_PER_FRAME1 * 1000000 /
213             p_enc->fmt_out.audio.i_rate;
214
215         start_date += p_mp3_block->i_length;
216         p_mp3_block->i_dts = p_mp3_block->i_pts = start_date;
217
218         p_mp3_block->i_samples = SAMP_PER_FRAME1;
219
220         block_ChainAppend( &p_chain, p_mp3_block );
221
222     } while( p_pcm_block );
223
224     return p_chain;
225 }
226
227 static void CloseEncoder( vlc_object_t *p_this )
228 {
229     encoder_sys_t *p_sys = ((encoder_t*)p_this)->p_sys;
230
231     /* TODO: we should send the last PCM block padded with 0
232      * But we don't know if other blocks will come before it's too late */
233     if( p_sys->i_buffer )
234         free( p_sys->p_buffer );
235
236     block_FifoRelease( p_sys->p_fifo );
237     free( p_sys );
238 }