]> git.sesse.net Git - vlc/blob - modules/demux/aac/demux.c
moved id3 related code to a separate utility module since several
[vlc] / modules / demux / aac / demux.c
1 /*****************************************************************************
2  * demux.c : Raw aac Stream input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: demux.c,v 1.2 2002/08/24 21:35:31 sigmunau 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 <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 #include <sys/types.h>
34
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 static int  Activate ( vlc_object_t * );
39 static int  Demux ( input_thread_t * );
40
41
42 /*****************************************************************************
43  * Module descriptor
44  *****************************************************************************/
45 vlc_module_begin();
46     set_description( _("AAC stream demux" ) );
47     set_capability( "demux", 0 );
48     set_callbacks( Activate, NULL );
49     add_shortcut( "aac" );
50 vlc_module_end();
51
52 /*****************************************************************************
53  * Definitions of structures  and functions used by this plugins 
54  *****************************************************************************/
55
56 /* XXX set this to 0 to avoid problem with PS XXX */
57 /* but with some file or web radio will failed to detect */
58 /* it's you to choose */
59 #define AAC_MAXTESTPOS    0
60
61 static int i_aac_samplerate[16] =
62 {
63     96000, 88200, 64000, 48000, 44100, 32000, 
64     24000, 22050, 16000, 12000, 11025, 8000,
65     7350,  0,     0,     0
66 };
67
68 typedef struct adts_header_s
69 {
70     int i_id;
71     int i_framelength;
72     int i_layer;
73     int i_protection_absent;
74     int i_profile;
75     int i_samplerate_index;
76     int i_private_bit;
77     int i_channel_configuration;
78     int i_original;
79     int i_home;
80     int i_emphasis;
81     int i_copyright_identification_bit;
82     int i_copyright_identification_start;
83     int i_aac_frame_length;
84     int i_adts_buffer_fullness;
85     int i_no_raw_data_blocks_in_frame;
86     int i_crc_check;
87                     
88 } adts_header_t;
89
90 /* Not yet used */
91 typedef struct adif_header_s
92 {
93     int b_copyright_id_present;
94     u8  i_copyright_id[10];
95     
96     int b_original_copy;
97
98     int i_home;
99     int i_bitstream_type;
100     int i_bitrate;
101     int i_num_program_config_elements;
102     int adif_buffer_fullness;
103
104 //    program_config_element
105
106
107 } adif_header_t;
108
109 struct demux_sys_t
110 {
111     mtime_t i_pts;
112
113     es_descriptor_t         *p_es;
114
115     int                     b_adif_header;
116     adif_header_t           adif_header;
117
118     int                     b_adts_header;
119     adts_header_t           adts_header;
120
121     /* extracted information */
122     int i_samplerate_index;
123     int i_object_type;
124     int i_samplerate;
125     int i_channels;
126     int i_framelength;
127     int i_aac_frame_length;
128 };
129
130 /****************************************************************************
131  * bit_* : function to get a bitstream from a memory buffer
132  ****************************************************************************
133  *
134  ****************************************************************************/ 
135 typedef struct bit_s
136 {
137     u8 *p_buffer;
138     int i_buffer;
139     int i_mask;
140 } bit_t;
141
142 static void bit_init( bit_t *p_bit,
143                       u8    *p_buffer,
144                       int   i_buffer )
145 {
146     p_bit->p_buffer = p_buffer;
147     p_bit->i_buffer = i_buffer;
148     p_bit->i_mask = 0x80;
149 }
150
151 static u32 bit_get( bit_t *p_bit )
152 {
153     u32 i_bit;
154     if( p_bit->i_buffer <= 0 )
155     {
156         return( 0 );
157     }
158     i_bit = ( p_bit->p_buffer[0]&p_bit->i_mask ) ? 1 : 0;
159     
160     p_bit->i_mask >>= 1;
161     if( !p_bit->i_mask )
162     {
163         p_bit->p_buffer++;
164         p_bit->i_buffer--;
165         p_bit->i_mask = 0x80;
166     }
167     return( i_bit );
168 }
169
170 static u32 bit_gets( bit_t *p_bit, int i_count )
171 {
172     u32 i_bits;
173     i_bits = 0;
174     for( ; i_count > 0; i_count-- )
175     {
176         i_bits = ( i_bits << 1 )|bit_get( p_bit );
177     }
178     return( i_bits );
179 }
180
181 /*****************************************************************************
182  * Function to manipulate stream easily
183  *****************************************************************************
184  *
185  * SkipBytes : skip bytes :) not yet uoptimised ( read bytes to be skipped )
186  *
187  * ReadPes : read data and make a PES
188  * 
189  *****************************************************************************/
190 static int SkipBytes( input_thread_t *p_input, int i_size )
191 {
192     data_packet_t *p_data;
193     int i_read;
194
195     while( i_size > 0 )
196     {
197         i_read = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
198         if( i_read <= 0 )
199         {
200             return( 0 );
201         }
202         input_DeletePacket( p_input->p_method_data, p_data );
203         i_size -= i_read;
204     }
205     return( 1 );
206 }
207
208 static int ReadPES( input_thread_t *p_input, 
209                     pes_packet_t **pp_pes, 
210                     int i_size )
211 {
212     pes_packet_t *p_pes;
213
214     *pp_pes = NULL;
215         
216     if( !(p_pes = input_NewPES( p_input->p_method_data )) )
217     {
218         msg_Err( p_input, "cannot allocate new PES" );
219         return( 0 );
220     }
221
222     while( i_size > 0 )
223     {
224         data_packet_t   *p_data;
225         int i_read;
226
227         if( (i_read = input_SplitBuffer( p_input, 
228                                          &p_data, 
229                                          __MIN( i_size, 1024 ) ) ) <= 0 )
230         {
231             input_DeletePES( p_input->p_method_data, p_pes );
232             return( 0 );
233         }
234         if( !p_pes->p_first )
235         {
236             p_pes->p_first = p_data;
237             p_pes->i_nb_data = 1;
238             p_pes->i_pes_size = i_read;
239         }
240         else
241         {
242             p_pes->p_last->p_next  = p_data;
243             p_pes->i_nb_data++;
244             p_pes->i_pes_size += i_read;
245         }
246         p_pes->p_last  = p_data;
247         i_size -= i_read;
248     }
249     *pp_pes = p_pes;
250     return( 1 );
251 }
252
253 /*****************************************************************************
254  * GetADIF: find and load adif header if present
255  *****************************************************************************/
256 static int GetADIF( input_thread_t *p_input,
257                     adif_header_t  *p_adif )
258 {
259     u8  *p_peek;
260     int i_size;
261
262     if( ( i_size = input_Peek( p_input, &p_peek, 60 ) ) < 60  )
263     {
264         /* it's false, min size is 11 byte but easier ;) */
265         return( 0 );
266     }
267     if( ( p_peek[0] != 'A' )||( p_peek[1] != 'D' )||
268         ( p_peek[2] != 'I' )||( p_peek[3] != 'F' ) )
269     {
270         return( 0 );
271     }
272     
273     /* we now that we have an adif header */
274     
275 //    return( 1 );
276     return( 0 ); /* need some work */
277 }
278
279 /*****************************************************************************
280  * GetADTS: find and load adts header 
281  *****************************************************************************/
282 #define ADTS_HEADERSIZE 10 /* 8+2 for crc */
283 static int GetADTS( input_thread_t  *p_input,
284                     adts_header_t   *p_adts,
285                     int             i_max_pos,
286                     int             *pi_skip )
287 {
288     u8  *p_peek;
289     int i_size;
290     bit_t bit;
291
292     if( ( i_size = input_Peek( p_input, &p_peek, i_max_pos + ADTS_HEADERSIZE ) )
293             < ADTS_HEADERSIZE )
294     {
295         return( 0 );
296     }
297     *pi_skip = 0;
298     for( ; ; )
299     {
300         if( i_size < ADTS_HEADERSIZE )
301         {
302             return( 0 );
303         }
304         if( ( p_peek[0] != 0xff )||
305             ( ( p_peek[1]&0xf6 ) != 0xf0 ) ) /* Layer == 0 */
306         {
307             p_peek++;
308             i_size--;
309             *pi_skip += 1;
310             continue;
311         }
312         /* we have find an adts header, load it */
313         break;
314     }
315
316     bit_init( &bit, p_peek, i_size );
317     bit_gets( &bit, 12 ); /* synchro bits */
318     
319     p_adts->i_id        = bit_get( &bit );
320     p_adts->i_layer     = bit_gets( &bit, 2);
321     p_adts->i_protection_absent = bit_get( &bit );
322     p_adts->i_profile           = bit_gets( &bit, 2 );
323     p_adts->i_samplerate_index  = bit_gets( &bit, 4);
324     p_adts->i_private_bit       = bit_get( &bit );
325     p_adts->i_channel_configuration = bit_gets( &bit, 3);
326     p_adts->i_original  = bit_get( &bit );
327     p_adts->i_home      = bit_get( &bit );
328     if( p_adts->i_id == 0 )
329     {
330         p_adts->i_emphasis = bit_gets( &bit, 2);
331     }
332
333     p_adts->i_copyright_identification_bit   = bit_get( &bit );
334     p_adts->i_copyright_identification_start = bit_get( &bit );
335     p_adts->i_aac_frame_length               = bit_gets( &bit, 13 );
336     p_adts->i_adts_buffer_fullness           = bit_gets( &bit, 11 );
337     p_adts->i_no_raw_data_blocks_in_frame    = bit_gets( &bit, 2 );
338
339     if( p_adts->i_protection_absent == 0 )
340     {
341         p_adts->i_crc_check = bit_gets( &bit, 16 );
342     }
343     return( 1 );
344 }
345
346 static void ExtractConfiguration( demux_sys_t *p_aac )
347 {
348     if( p_aac->b_adif_header )
349     {
350
351     }
352     if( p_aac->b_adts_header )
353     {
354         p_aac->i_samplerate_index = p_aac->adts_header.i_samplerate_index;
355         p_aac->i_object_type = p_aac->adts_header.i_profile;
356         p_aac->i_samplerate  = i_aac_samplerate[p_aac->i_samplerate_index];
357         p_aac->i_channels    = p_aac->adts_header.i_channel_configuration;       
358         if( p_aac->i_channels > 6 )
359         {
360             /* I'm not sure of that, got from faad */
361             p_aac->i_channels = 2;
362         }
363         p_aac->i_aac_frame_length = p_aac->adts_header.i_aac_frame_length;
364         p_aac->i_framelength = 1024;
365     }
366     /* FIXME FIXME for LD, but LD = ?
367     if( p_aac->i_object_type == LD )
368     {
369         p_aac->i_framelength /= 2;
370     }
371     */
372 }
373
374 /****************************************************************************
375  * CheckPS : check if this stream could be some ps, 
376  *           yes it's ugly ...  but another idea ?
377  *
378  *           XXX it seems that aac stream always match ...
379  *
380  ****************************************************************************/
381
382 static int CheckPS( input_thread_t *p_input )
383 {
384     u8 *p_peek;
385     int i_size = input_Peek( p_input, &p_peek, 8196 );
386
387     while( i_size >  4 )
388     {
389         if( ( p_peek[0] == 0 ) && ( p_peek[1] == 0 )&&
390             ( p_peek[2] == 1 ) && ( p_peek[3] >= 0xb9 ) )
391         {
392             return( 1 ); /* Perhaps some ps stream */
393         }
394         p_peek++;
395        i_size--; 
396     }
397     return( 0 );
398 }
399
400 /*****************************************************************************
401  * Activate: initializes AAC demux structures
402  *****************************************************************************/
403 static int Activate( vlc_object_t * p_this )
404 {
405     input_thread_t * p_input = (input_thread_t *)p_this;
406     demux_sys_t * p_aac;
407     input_info_category_t * p_category;
408     module_t * p_id3;
409     
410     int i_skip;
411     int b_forced;
412
413     /* Set the demux function */
414     p_input->pf_demux = Demux;
415
416     /* Initialize access plug-in structures. */
417     if( p_input->i_mtu == 0 )
418     {
419     /* Improve speed. */
420         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
421     }
422
423     b_forced = ( ( *p_input->psz_demux )&&
424                  ( !strncmp( p_input->psz_demux, "aac", 10 ) ) ) ? 1 : 0;
425     
426     /* check if it can be a ps stream */
427     if( !b_forced && CheckPS(  p_input ) )
428     {
429         return( -1 );
430     }
431
432     /* skip possible id3 header */    
433     p_id3 = module_Need( p_input, "id3", NULL );
434     if ( p_id3 ) {
435         module_Unneed( p_input, p_id3 );
436     }
437     
438     /* allocate p_aac */
439     if( !( p_aac = malloc( sizeof( demux_sys_t ) ) ) )
440     {
441         msg_Err( p_input, "out of memory" );
442         return( -1 );
443     }
444     memset( p_aac, 0, sizeof( demux_sys_t ) );
445     
446     /* Now check for adif/adts header */
447     i_skip = 0;
448     if( GetADIF( p_input, &p_aac->adif_header ) )
449     {
450         p_aac->b_adif_header = 1;
451         msg_Err( p_input,
452                  "found ADIF header (unsupported)" );
453         free( p_aac );
454         return( -1 );
455     }
456     else
457     if( GetADTS( p_input, 
458                  &p_aac->adts_header, 
459                  b_forced ? 8000 : 0,
460                  &i_skip  ) )
461     {
462         p_aac->b_adts_header = 1;
463         msg_Info( p_input,
464                   "found ADTS header" );
465     }
466     else
467     {
468         msg_Warn( p_input,
469                   "AAC module discarded (no header found)" );
470         free( p_aac );
471         return( -1 );
472     }
473     ExtractConfiguration( p_aac );
474
475     vlc_mutex_lock( &p_input->stream.stream_lock );
476     if( input_InitStream( p_input, 0 ) == -1)
477     {
478         msg_Err( p_input, "cannot init stream" );
479         return( -1 );
480     }    
481     if( input_AddProgram( p_input, 0, 0) == NULL )
482     {
483         msg_Err( p_input, "cannot add program" );
484         return( -1 );
485     }
486     p_input->stream.pp_programs[0]->b_is_ok = 0;
487     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
488     
489     /* create our ES */ 
490     p_aac->p_es = input_AddES( p_input, 
491                                p_input->stream.p_selected_program, 
492                                1, /* id */
493                                0 );
494     if( !p_aac->p_es )
495     {
496         vlc_mutex_unlock( &p_input->stream.stream_lock );
497         msg_Err( p_input, "out of memory" );
498         return( -1 );
499     }
500     
501     p_aac->p_es->i_stream_id = 1;
502     p_aac->p_es->i_fourcc = VLC_FOURCC( 'm', 'p', '4', 'a' );
503     p_aac->p_es->i_cat = AUDIO_ES;
504     input_SelectES( p_input, p_aac->p_es );
505
506     p_input->stream.p_selected_program->b_is_ok = 1;
507     vlc_mutex_unlock( &p_input->stream.stream_lock );
508
509
510     
511     vlc_mutex_lock( &p_input->stream.stream_lock );
512     if( p_aac->b_adif_header )
513     {
514         p_input->stream.i_mux_rate = 0 / 50;
515     }
516     if( p_aac->b_adts_header )
517     {
518         p_input->stream.i_mux_rate = 0 / 50;
519     }
520     vlc_mutex_unlock( &p_input->stream.stream_lock );
521 #if 0
522     /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME */
523     /* if i don't do that, it don't work correctly but why ??? */
524     if( p_input->stream.b_seekable )
525     {
526         p_input->pf_seek( p_input, 0 );
527         input_AccessReinit( p_input );
528     }
529     /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME */
530 #endif
531
532     /* all is ok :)) */
533     if( p_aac->b_adif_header )
534     {
535     }
536
537     if( p_aac->b_adts_header )
538     {
539         msg_Dbg( p_input,
540                  "audio AAC MPEG-%d layer-%d  %d channels %dHz",
541                  p_aac->adts_header.i_id == 1 ? 2 : 4,
542                  4 - p_aac->adts_header.i_layer, /* it's always 4 in fact */
543                  p_aac->i_channels,
544                  p_aac->i_samplerate );
545
546         vlc_mutex_lock( &p_input->stream.stream_lock );
547         p_category = input_InfoCategory( p_input, "aac" );
548     
549         input_AddInfo( p_category, "input type", "MPEG-%d AAC",
550                        p_aac->adts_header.i_id == 1 ? 2 : 4 );
551
552         input_AddInfo( p_category, "layer", "%d", 
553                        4 - p_aac->adts_header.i_layer );
554         input_AddInfo( p_category, "channels", "%d", 
555                        p_aac->i_channels );
556         input_AddInfo( p_category, "sample rate", "%dHz", 
557                        p_aac->i_samplerate );
558
559         vlc_mutex_unlock( &p_input->stream.stream_lock );
560     }
561
562     p_input->p_demux_data = p_aac;
563     
564
565     return( 0 );
566 }
567
568 /*****************************************************************************
569  * Demux: reads and demuxes data packets
570  *****************************************************************************
571  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
572  *****************************************************************************/
573 static int Demux( input_thread_t * p_input )
574 {
575     int i_skip;
576     int i_found;
577     
578     pes_packet_t    *p_pes;
579     demux_sys_t *p_aac = p_input->p_demux_data;
580
581     /* Get a frame */
582     if( p_aac->b_adif_header )
583     {
584         i_skip = 0;
585         return( -1 ); /* FIXME */
586     }
587     else
588     if( p_aac->b_adts_header )
589     {
590         i_found = GetADTS( p_input, 
591                            &p_aac->adts_header,
592                            8000,
593                            &i_skip );
594     }
595     else
596     {
597         return( -1 );
598     }
599     ExtractConfiguration( p_aac );
600    
601     /* skip garbage bytes */
602     if( i_skip > 0 )
603     {
604         msg_Dbg( p_input,
605                  "skipping %d bytes",
606                  i_skip );
607         SkipBytes( p_input, i_skip );
608     }
609
610     if( !i_found )
611     {
612         msg_Info( p_input, "can't find next frame" );
613         return( 0 );
614     }
615     
616     input_ClockManageRef( p_input,
617                           p_input->stream.p_selected_program,
618                           p_aac->i_pts );
619    
620     if( !ReadPES( p_input, &p_pes, p_aac->i_aac_frame_length ) )
621     {
622         msg_Err( p_input,
623                  "cannot read data" );
624         return( -1 );
625     }
626     p_pes->i_dts =
627         p_pes->i_pts = input_ClockGetTS( p_input,
628                                          p_input->stream.p_selected_program,
629                                          p_aac->i_pts );
630
631     if( !p_aac->p_es->p_decoder_fifo )
632     {
633         msg_Err( p_input, "no audio decoder" );
634         input_DeletePES( p_input->p_method_data, p_pes );
635         return( -1 ); /* perhaps not, it's my choice */
636     }
637     else
638     {
639         input_DecodePES( p_aac->p_es->p_decoder_fifo, p_pes );
640     }
641
642     /* Update date information */
643     p_aac->i_pts += (mtime_t)90000 * 
644                     (mtime_t)p_aac->i_framelength /
645                     (mtime_t)p_aac->i_samplerate;
646
647     return( 1 );
648 }
649