]> git.sesse.net Git - vlc/blob - modules/packetizer/mpeg4audio.c
* v4l: updated, now it should grab (and compress if you want) the video.
[vlc] / modules / packetizer / mpeg4audio.c
1 /*****************************************************************************
2  * mpeg4audio.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: mpeg4audio.c,v 1.4 2003/03/31 03:46:11 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/aout.h>
29 #include <vlc/decoder.h>
30 #include <vlc/input.h>
31 #include <vlc/sout.h>
32 #include "codecs.h"
33 #include <stdlib.h>                                      /* malloc(), free() */
34 #include <string.h>                                              /* strdup() */
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39
40 /* AAC Config in ES:
41  *
42  * AudioObjectType          5 bits
43  * samplingFrequencyIndex   4 bits
44  * if (samplingFrequencyIndex == 0xF)
45  *  samplingFrequency   24 bits
46  * channelConfiguration     4 bits
47  * GA_SpecificConfig
48  *  FrameLengthFlag         1 bit 1024 or 960
49  *  DependsOnCoreCoder      1 bit (always 0)
50  *  ExtensionFlag           1 bit (always 0)
51  */
52
53 typedef struct packetizer_thread_s
54 {
55     /* Input properties */
56     int                     b_adts;
57
58     decoder_fifo_t          *p_fifo;
59     bit_stream_t            bit_stream;
60
61     /* Output properties */
62     sout_packetizer_input_t *p_sout_input;
63     sout_packet_format_t    output_format;
64
65 //    mtime_t                 i_pts_start;
66     mtime_t                 i_last_pts;
67
68     WAVEFORMATEX            *p_wf;
69
70     /* Extracted from AAC config */
71     int                     i_sample_rate;
72     int                     i_frame_size;   // 1024 or 960
73
74 } packetizer_thread_t;
75
76 static int  Open    ( vlc_object_t * );
77 static int  Run     ( decoder_fifo_t * );
78
79 static int  InitThread           ( packetizer_thread_t * );
80 static void PacketizeThreadMPEG4 ( packetizer_thread_t * );
81 static void PacketizeThreadADTS  ( packetizer_thread_t * );
82 static void EndThread            ( packetizer_thread_t * );
83
84 /*****************************************************************************
85  * Module descriptor
86  *****************************************************************************/
87
88 vlc_module_begin();
89     set_description( _("MPEG4 Audio packetizer") );
90     set_capability( "packetizer", 50 );
91     set_callbacks( Open, NULL );
92 vlc_module_end();
93
94
95 /*****************************************************************************
96  * OpenDecoder: probe the packetizer and return score
97  *****************************************************************************
98  * Tries to launch a decoder and return score so that the interface is able
99  * to choose.
100  *****************************************************************************/
101 static int Open( vlc_object_t *p_this )
102 {
103     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
104
105     p_fifo->pf_run = Run;
106
107     if( p_fifo->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'a') )
108     {
109         return( VLC_SUCCESS );
110     }
111     else
112     {
113         return( VLC_EGENERIC );
114     }
115 }
116
117 /*****************************************************************************
118  * RunDecoder: this function is called just after the thread is created
119  *****************************************************************************/
120 static int Run( decoder_fifo_t *p_fifo )
121 {
122     packetizer_thread_t *p_pack;
123     int b_error;
124
125     msg_Info( p_fifo, "Running MPEG4 audio packetizer" );
126     if( !( p_pack = malloc( sizeof( packetizer_thread_t ) ) ) )
127     {
128         msg_Err( p_fifo, "out of memory" );
129         DecoderError( p_fifo );
130         return( -1 );
131     }
132     memset( p_pack, 0, sizeof( packetizer_thread_t ) );
133
134     p_pack->p_fifo = p_fifo;
135
136     if( InitThread( p_pack ) != 0 )
137     {
138         DecoderError( p_fifo );
139         return( -1 );
140     }
141
142     while( ( !p_pack->p_fifo->b_die )&&( !p_pack->p_fifo->b_error ) )
143     {
144         if( p_pack->b_adts )
145         {
146             PacketizeThreadADTS( p_pack );
147         }
148         else
149         {
150             PacketizeThreadMPEG4( p_pack );
151         }
152     }
153
154
155     if( ( b_error = p_pack->p_fifo->b_error ) )
156     {
157         DecoderError( p_pack->p_fifo );
158     }
159
160     EndThread( p_pack );
161     if( b_error )
162     {
163         return( -1 );
164     }
165
166     return( 0 );
167 }
168
169
170 #define FREE( p ) if( p ) free( p ); p = NULL
171
172 /*****************************************************************************
173  * InitThread: initialize data before entering main loop
174  *****************************************************************************/
175 static int i_sample_rates[] = 
176 {
177     96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 
178     16000, 12000, 11025, 8000,  7350,  0,     0,     0
179 };
180
181 static int InitThread( packetizer_thread_t *p_pack )
182 {
183     WAVEFORMATEX    *p_wf;
184
185     p_wf = (WAVEFORMATEX*)p_pack->p_fifo->p_waveformatex;
186
187     if( p_wf && p_wf->cbSize > 0)
188     {
189         uint8_t *p_config = (uint8_t*)&p_wf[1];
190         int i_wf = sizeof( WAVEFORMATEX ) + p_wf->cbSize;
191         int i_index;
192
193
194         p_pack->p_wf = malloc( i_wf );
195         memcpy( p_pack->p_wf,
196                 p_wf,
197                 i_wf );
198         p_pack->output_format.i_cat = AUDIO_ES;
199         p_pack->output_format.i_fourcc = VLC_FOURCC( 'm', 'p', '4', 'a' );
200         p_pack->output_format.p_format = p_pack->p_wf;
201         p_pack->b_adts = 0;
202
203         i_index = ( ( p_config[0] << 1 ) | ( p_config[1] >> 7 ) )&0x0f;
204         if( i_index != 0x0f )
205         {
206             p_pack->i_sample_rate = i_sample_rates[i_index];
207             p_pack->i_frame_size  = ( ( p_config[1] >> 2 )&0x01 ) ? 960 : 1024;
208         }
209         else
210         {
211             p_pack->i_sample_rate = ( ( p_config[1]&0x7f ) << 17 ) | ( p_config[2] << 9 )| 
212                                       ( p_config[3] << 1 ) | ( p_config[4] >> 7 );
213             p_pack->i_frame_size  = ( ( p_config[4] >> 2 )&0x01 ) ? 960 : 1024;
214         }
215         msg_Dbg( p_pack->p_fifo,
216                  "aac %dHz %d samples/frame",
217                  p_pack->i_sample_rate,
218                  p_pack->i_frame_size );
219     }
220     else
221     {
222         /* we will try to create a AAC Config from adts */
223         p_pack->output_format.i_cat = UNKNOWN_ES;
224         p_pack->output_format.i_fourcc = VLC_FOURCC( 'n', 'u', 'l', 'l' );
225         p_pack->b_adts = 1;
226
227         if( InitBitstream( &p_pack->bit_stream, p_pack->p_fifo,
228                NULL, NULL ) != VLC_SUCCESS )
229         {
230             msg_Err( p_pack->p_fifo, "cannot initialize bitstream" );
231             return -1;
232         }
233
234     }
235
236     p_pack->p_sout_input =
237         sout_InputNew( p_pack->p_fifo,
238                        &p_pack->output_format );
239
240     if( !p_pack->p_sout_input )
241     {
242         msg_Err( p_pack->p_fifo,
243                  "cannot add a new stream" );
244         return( -1 );
245     }
246
247     p_pack->i_last_pts = 0;
248     return( 0 );
249 }
250
251 /*****************************************************************************
252  * PacketizeThread: packetize an unit (here copy a complete pes)
253  *****************************************************************************/
254 static void PacketizeThreadMPEG4( packetizer_thread_t *p_pack )
255 {
256     sout_buffer_t   *p_sout_buffer;
257     pes_packet_t    *p_pes;
258     ssize_t         i_size;
259     mtime_t         i_pts;
260
261     /* **** get samples count **** */
262     input_ExtractPES( p_pack->p_fifo, &p_pes );
263     if( !p_pes )
264     {
265         p_pack->p_fifo->b_error = 1;
266         return;
267     }
268 #if 0
269     if( p_pack->i_pts_start < 0 && p_pes->i_pts > 0 )
270     {
271         p_pack->i_pts_start = p_pes->i_pts;
272     }
273     p_pack->i_pts = p_pes->i_pts - p_pack->i_pts_start;
274 #endif
275
276     i_pts = p_pes->i_pts;
277
278     if( i_pts <= 0 && p_pack->i_last_pts <= 0 )
279     {
280         msg_Dbg( p_pack->p_fifo, "need a starting pts" );
281         input_DeletePES( p_pack->p_fifo->p_packets_mgt, p_pes );
282         return;
283     }
284     i_size = p_pes->i_pes_size;
285
286     if( i_size > 0 )
287     {
288         data_packet_t   *p_data;
289         ssize_t          i_buffer;
290
291         p_sout_buffer = 
292             sout_BufferNew( p_pack->p_sout_input->p_sout, i_size );
293         if( !p_sout_buffer )
294         {
295             p_pack->p_fifo->b_error = 1;
296             input_DeletePES( p_pack->p_fifo->p_packets_mgt, p_pes );
297             return;
298         }
299         /* TODO: memcpy of the pes packet */
300         for( i_buffer = 0, p_data = p_pes->p_first;
301              p_data != NULL && i_buffer < i_size;
302              p_data = p_data->p_next)
303         {
304             size_t          i_copy;
305
306             i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
307                             i_size - i_buffer );
308             if( i_copy > 0 )
309             {
310                 p_pack->p_fifo->p_vlc->pf_memcpy( p_sout_buffer->p_buffer + i_buffer,
311                                                   p_data->p_payload_start,
312                                                   i_copy );
313             }
314             i_buffer += i_copy;
315         }
316
317         if( i_pts <= 0 )
318         {
319             i_pts = p_pack->i_last_pts +
320                         (mtime_t)1000000 *
321                         (mtime_t)p_pack->i_frame_size /
322                         (mtime_t)p_pack->i_sample_rate;
323         }
324         p_pack->i_last_pts = i_pts;
325
326         p_sout_buffer->i_length = (mtime_t)1000000 *
327                                   (mtime_t)p_pack->i_frame_size /
328                                   (mtime_t)p_pack->i_sample_rate;
329         p_sout_buffer->i_bitrate = 0;
330         p_sout_buffer->i_dts = i_pts;
331         p_sout_buffer->i_pts = i_pts;
332
333         sout_InputSendBuffer( p_pack->p_sout_input,
334                                p_sout_buffer );
335     }
336
337     input_DeletePES( p_pack->p_fifo->p_packets_mgt, p_pes );
338 }
339
340
341 static void PacketizeThreadADTS( packetizer_thread_t *p_pack )
342 {
343     msg_Err( p_pack->p_fifo, "adts stream unsupported" );
344     p_pack->p_fifo->b_error = 1;
345 }
346
347 /*****************************************************************************
348  * EndThread : packetizer thread destruction
349  *****************************************************************************/
350 static void EndThread ( packetizer_thread_t *p_pack)
351 {
352     if( p_pack->p_sout_input )
353     {
354         sout_InputDelete( p_pack->p_sout_input );
355     }
356     if( p_pack->p_wf )
357     {
358         free( p_pack->p_wf );
359     }
360 }
361