]> git.sesse.net Git - vlc/blob - modules/demux/aac/demux.c
52a44ef94fc80aa65c64847da814dd1ca13416ac
[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.7 2003/03/30 18:14:37 gbazin 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 static void Deactivate  ( vlc_object_t * );
41
42 /*****************************************************************************
43  * Module descriptor
44  *****************************************************************************/
45 vlc_module_begin();
46     set_description( _("AAC stream demuxer" ) );
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 #define FREE( p ) if( p ) { free( p );  (p) = NULL; }
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     uint8_t 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     uint8_t *p_buffer;
138     int i_buffer;
139     int i_mask;
140 } bit_t;
141
142 static void bit_init( bit_t *p_bit,
143                       uint8_t    *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 uint32_t bit_get( bit_t *p_bit )
152 {
153     uint32_t 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 uint32_t bit_gets( bit_t *p_bit, int i_count )
171 {
172     uint32_t 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     uint8_t  *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     uint8_t  *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     uint8_t *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         vlc_mutex_unlock( &p_input->stream.stream_lock );
479         msg_Err( p_input, "cannot init stream" );
480         return( -1 );
481     }
482     if( input_AddProgram( p_input, 0, 0) == NULL )
483     {
484         vlc_mutex_unlock( &p_input->stream.stream_lock );
485         msg_Err( p_input, "cannot add program" );
486         return( -1 );
487     }
488     p_input->stream.pp_programs[0]->b_is_ok = 0;
489     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
490
491     /* create our ES */
492     p_aac->p_es = input_AddES( p_input,
493                                p_input->stream.p_selected_program, 
494                                1, /* id */
495                                0 );
496     if( !p_aac->p_es )
497     {
498         vlc_mutex_unlock( &p_input->stream.stream_lock );
499         msg_Err( p_input, "out of memory" );
500         return( -1 );
501     }
502
503     p_aac->p_es->i_stream_id = 1;
504     p_aac->p_es->i_fourcc = VLC_FOURCC( 'm', 'p', '4', 'a' );
505     p_aac->p_es->i_cat = AUDIO_ES;
506     input_SelectES( p_input, p_aac->p_es );
507
508     p_input->stream.p_selected_program->b_is_ok = 1;
509     vlc_mutex_unlock( &p_input->stream.stream_lock );
510
511
512
513     vlc_mutex_lock( &p_input->stream.stream_lock );
514     if( p_aac->b_adif_header )
515     {
516         p_input->stream.i_mux_rate = 0 / 50;
517     }
518     if( p_aac->b_adts_header )
519     {
520         p_input->stream.i_mux_rate = 0 / 50;
521     }
522     vlc_mutex_unlock( &p_input->stream.stream_lock );
523 #if 0
524     /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME */
525     /* if i don't do that, it don't work correctly but why ??? */
526     if( p_input->stream.b_seekable )
527     {
528         p_input->pf_seek( p_input, 0 );
529         input_AccessReinit( p_input );
530     }
531     /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME */
532 #endif
533
534     /* all is ok :)) */
535     if( p_aac->b_adif_header )
536     {
537     }
538
539     if( p_aac->b_adts_header )
540     {
541         msg_Dbg( p_input,
542                  "audio AAC MPEG-%d layer-%d  %d channels %dHz",
543                  p_aac->adts_header.i_id == 1 ? 2 : 4,
544                  4 - p_aac->adts_header.i_layer, /* it's always 4 in fact */
545                  p_aac->i_channels,
546                  p_aac->i_samplerate );
547
548         vlc_mutex_lock( &p_input->stream.stream_lock );
549         p_category = input_InfoCategory( p_input, _("Aac") );
550
551         input_AddInfo( p_category, _("Input Type"), "MPEG-%d AAC",
552                        p_aac->adts_header.i_id == 1 ? 2 : 4 );
553
554         input_AddInfo( p_category, _("Layer"), "%d", 
555                        4 - p_aac->adts_header.i_layer );
556         input_AddInfo( p_category, _("Channels"), "%d", 
557                        p_aac->i_channels );
558         input_AddInfo( p_category, _("Sample Rate"), "%dHz", 
559                        p_aac->i_samplerate );
560
561         vlc_mutex_unlock( &p_input->stream.stream_lock );
562     }
563
564     p_input->p_demux_data = p_aac;
565
566
567     return( 0 );
568 }
569
570 /*****************************************************************************
571  * Demux: reads and demuxes data packets
572  *****************************************************************************
573  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
574  *****************************************************************************/
575 static int Demux( input_thread_t * p_input )
576 {
577     int i_skip;
578     int i_found;
579
580     pes_packet_t    *p_pes;
581     demux_sys_t *p_aac = p_input->p_demux_data;
582
583     /* Get a frame */
584     if( p_aac->b_adif_header )
585     {
586         i_skip = 0;
587         return( -1 ); /* FIXME */
588     }
589     else
590     if( p_aac->b_adts_header )
591     {
592         i_found = GetADTS( p_input,
593                            &p_aac->adts_header,
594                            8000,
595                            &i_skip );
596     }
597     else
598     {
599         return( -1 );
600     }
601     ExtractConfiguration( p_aac );
602
603     /* skip garbage bytes */
604     if( i_skip > 0 )
605     {
606         msg_Dbg( p_input,
607                  "skipping %d bytes",
608                  i_skip );
609         SkipBytes( p_input, i_skip );
610     }
611
612     if( !i_found )
613     {
614         msg_Info( p_input, "can't find next frame" );
615         return( 0 );
616     }
617
618     input_ClockManageRef( p_input,
619                           p_input->stream.p_selected_program,
620                           p_aac->i_pts );
621
622     if( !ReadPES( p_input, &p_pes, p_aac->i_aac_frame_length ) )
623     {
624         msg_Warn( p_input,
625                  "cannot read data" );
626         return( -1 );
627     }
628     p_pes->i_dts =
629         p_pes->i_pts = input_ClockGetTS( p_input,
630                                          p_input->stream.p_selected_program,
631                                          p_aac->i_pts );
632
633     if( !p_aac->p_es->p_decoder_fifo )
634     {
635         msg_Err( p_input, "no audio decoder" );
636         input_DeletePES( p_input->p_method_data, p_pes );
637         return( -1 ); /* perhaps not, it's my choice */
638     }
639     else
640     {
641         input_DecodePES( p_aac->p_es->p_decoder_fifo, p_pes );
642     }
643
644     /* Update date information */
645     p_aac->i_pts += (mtime_t)90000 *
646                     (mtime_t)p_aac->i_framelength /
647                     (mtime_t)p_aac->i_samplerate;
648
649     return( 1 );
650 }
651
652 static void Deactivate( vlc_object_t * p_this )
653 {
654     input_thread_t *p_input = (input_thread_t*)p_this;
655 //    demux_sys_t    *p_aac = p_input->p_demux_data;
656
657     FREE( p_input->p_demux_data );
658 }
659