]> git.sesse.net Git - vlc/blob - modules/packetizer/mpeg4audio.c
* all: set pf_run *only* if we accept this stream.
[vlc] / modules / packetizer / mpeg4audio.c
1 /*****************************************************************************
2  * mpeg4audio.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: mpeg4audio.c,v 1.8 2003/09/24 14:59:21 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_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_t *p_dec = (decoder_t*)p_this;
104
105     if( p_dec->p_fifo->i_fourcc != VLC_FOURCC( 'm', 'p', '4', 'a') )
106     {
107         return VLC_EGENERIC;
108     }
109
110     p_dec->pf_run = Run;
111     return VLC_SUCCESS;
112 }
113
114 /*****************************************************************************
115  * RunDecoder: this function is called just after the thread is created
116  *****************************************************************************/
117 static int Run( decoder_fifo_t *p_fifo )
118 {
119     packetizer_thread_t *p_pack;
120     int b_error;
121
122     msg_Info( p_fifo, "Running MPEG4 audio packetizer" );
123     if( !( p_pack = malloc( sizeof( packetizer_thread_t ) ) ) )
124     {
125         msg_Err( p_fifo, "out of memory" );
126         DecoderError( p_fifo );
127         return( -1 );
128     }
129     memset( p_pack, 0, sizeof( packetizer_thread_t ) );
130
131     p_pack->p_fifo = p_fifo;
132
133     if( InitThread( p_pack ) != 0 )
134     {
135         DecoderError( p_fifo );
136         return( -1 );
137     }
138
139     while( ( !p_pack->p_fifo->b_die )&&( !p_pack->p_fifo->b_error ) )
140     {
141         if( p_pack->b_adts )
142         {
143             PacketizeThreadADTS( p_pack );
144         }
145         else
146         {
147             PacketizeThreadMPEG4( p_pack );
148         }
149     }
150
151
152     if( ( b_error = p_pack->p_fifo->b_error ) )
153     {
154         DecoderError( p_pack->p_fifo );
155     }
156
157     EndThread( p_pack );
158     if( b_error )
159     {
160         return( -1 );
161     }
162
163     return( 0 );
164 }
165
166
167 #define FREE( p ) if( p ) free( p ); p = NULL
168
169 /*****************************************************************************
170  * InitThread: initialize data before entering main loop
171  *****************************************************************************/
172 static int i_sample_rates[] = 
173 {
174     96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 
175     16000, 12000, 11025, 8000,  7350,  0,     0,     0
176 };
177
178 static int InitThread( packetizer_thread_t *p_pack )
179 {
180     WAVEFORMATEX    *p_wf;
181
182     p_wf = (WAVEFORMATEX*)p_pack->p_fifo->p_waveformatex;
183
184     if( p_wf && p_wf->cbSize > 0)
185     {
186         uint8_t *p_config = (uint8_t*)&p_wf[1];
187         int     i_index;
188
189         p_pack->b_adts = 0;
190
191         i_index = ( ( p_config[0] << 1 ) | ( p_config[1] >> 7 ) )&0x0f;
192         if( i_index != 0x0f )
193         {
194             p_pack->i_sample_rate = i_sample_rates[i_index];
195             p_pack->i_frame_size  = ( ( p_config[1] >> 2 )&0x01 ) ? 960 : 1024;
196         }
197         else
198         {
199             p_pack->i_sample_rate = ( ( p_config[1]&0x7f ) << 17 ) | ( p_config[2] << 9 )| 
200                                       ( p_config[3] << 1 ) | ( p_config[4] >> 7 );
201             p_pack->i_frame_size  = ( ( p_config[4] >> 2 )&0x01 ) ? 960 : 1024;
202         }
203         msg_Dbg( p_pack->p_fifo,
204                  "aac %dHz %d samples/frame",
205                  p_pack->i_sample_rate,
206                  p_pack->i_frame_size );
207
208         p_pack->output_format.i_cat = AUDIO_ES;
209         p_pack->output_format.i_fourcc = VLC_FOURCC( 'm', 'p', '4', 'a' );
210         p_pack->output_format.i_sample_rate = p_pack->i_sample_rate;
211         p_pack->output_format.i_channels    = p_wf->nChannels;
212         p_pack->output_format.i_block_align = 0;
213         p_pack->output_format.i_bitrate     = 0;
214
215         p_pack->output_format.i_extra_data = p_wf->cbSize;
216         p_pack->output_format.p_extra_data = malloc( p_wf->cbSize );
217         memcpy( p_pack->output_format.p_extra_data,
218                 &p_wf[1],
219                 p_wf->cbSize );
220     }
221     else
222     {
223         /* we will try to create a AAC Config from adts */
224         p_pack->output_format.i_cat = UNKNOWN_ES;
225         p_pack->output_format.i_fourcc = VLC_FOURCC( 'n', 'u', 'l', 'l' );
226         p_pack->b_adts = 1;
227
228         if( InitBitstream( &p_pack->bit_stream, p_pack->p_fifo,
229                NULL, NULL ) != VLC_SUCCESS )
230         {
231             msg_Err( p_pack->p_fifo, "cannot initialize bitstream" );
232             return -1;
233         }
234
235     }
236
237     p_pack->p_sout_input =
238         sout_InputNew( p_pack->p_fifo,
239                        &p_pack->output_format );
240
241     if( !p_pack->p_sout_input )
242     {
243         msg_Err( p_pack->p_fifo,
244                  "cannot add a new stream" );
245         return( -1 );
246     }
247
248     p_pack->i_last_pts = 0;
249     return( 0 );
250 }
251
252 /*****************************************************************************
253  * PacketizeThread: packetize an unit (here copy a complete pes)
254  *****************************************************************************/
255 static void PacketizeThreadMPEG4( packetizer_thread_t *p_pack )
256 {
257     sout_buffer_t   *p_sout_buffer;
258     pes_packet_t    *p_pes;
259     ssize_t         i_size;
260     mtime_t         i_pts;
261
262     /* **** get samples count **** */
263     input_ExtractPES( p_pack->p_fifo, &p_pes );
264     if( !p_pes )
265     {
266         p_pack->p_fifo->b_error = 1;
267         return;
268     }
269 #if 0
270     if( p_pack->i_pts_start < 0 && p_pes->i_pts > 0 )
271     {
272         p_pack->i_pts_start = p_pes->i_pts;
273     }
274     p_pack->i_pts = p_pes->i_pts - p_pack->i_pts_start;
275 #endif
276
277     i_pts = p_pes->i_pts;
278
279     if( i_pts <= 0 && p_pack->i_last_pts <= 0 )
280     {
281         msg_Dbg( p_pack->p_fifo, "need a starting pts" );
282         input_DeletePES( p_pack->p_fifo->p_packets_mgt, p_pes );
283         return;
284     }
285     i_size = p_pes->i_pes_size;
286
287     if( i_size > 0 )
288     {
289         data_packet_t   *p_data;
290         ssize_t          i_buffer;
291
292         p_sout_buffer = 
293             sout_BufferNew( p_pack->p_sout_input->p_sout, i_size );
294         if( !p_sout_buffer )
295         {
296             p_pack->p_fifo->b_error = 1;
297             input_DeletePES( p_pack->p_fifo->p_packets_mgt, p_pes );
298             return;
299         }
300         /* TODO: memcpy of the pes packet */
301         for( i_buffer = 0, p_data = p_pes->p_first;
302              p_data != NULL && i_buffer < i_size;
303              p_data = p_data->p_next)
304         {
305             size_t          i_copy;
306
307             i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
308                             i_size - i_buffer );
309             if( i_copy > 0 )
310             {
311                 p_pack->p_fifo->p_vlc->pf_memcpy( p_sout_buffer->p_buffer + i_buffer,
312                                                   p_data->p_payload_start,
313                                                   i_copy );
314             }
315             i_buffer += i_copy;
316         }
317
318         if( i_pts <= 0 )
319         {
320             i_pts = p_pack->i_last_pts +
321                         (mtime_t)1000000 *
322                         (mtime_t)p_pack->i_frame_size /
323                         (mtime_t)p_pack->i_sample_rate;
324         }
325         p_pack->i_last_pts = i_pts;
326
327         p_sout_buffer->i_length = (mtime_t)1000000 *
328                                   (mtime_t)p_pack->i_frame_size /
329                                   (mtime_t)p_pack->i_sample_rate;
330         p_sout_buffer->i_bitrate = 0;
331         p_sout_buffer->i_dts = i_pts;
332         p_sout_buffer->i_pts = i_pts;
333
334         sout_InputSendBuffer( p_pack->p_sout_input,
335                                p_sout_buffer );
336     }
337
338     input_DeletePES( p_pack->p_fifo->p_packets_mgt, p_pes );
339 }
340
341
342 static void PacketizeThreadADTS( packetizer_thread_t *p_pack )
343 {
344     msg_Err( p_pack->p_fifo, "adts stream unsupported" );
345     p_pack->p_fifo->b_error = 1;
346 }
347
348 /*****************************************************************************
349  * EndThread : packetizer thread destruction
350  *****************************************************************************/
351 static void EndThread ( packetizer_thread_t *p_pack)
352 {
353     if( p_pack->p_sout_input )
354     {
355         sout_InputDelete( p_pack->p_sout_input );
356     }
357     if( p_pack->p_wf )
358     {
359         free( p_pack->p_wf );
360     }
361     free( p_pack );
362 }