]> git.sesse.net Git - vlc/blob - modules/packetizer/mpeg4audio.c
Qt4 - start to change things to match OSX interface.
[vlc] / modules / packetizer / mpeg4audio.c
1 /*****************************************************************************
2  * mpeg4audio.c: parse and packetize an MPEG 4 audio stream
3  *****************************************************************************
4  * Copyright (C) 2001, 2002, 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #include <vlc/vlc.h>
30 #include <vlc_aout.h>
31 #include <vlc_codec.h>
32 #include <vlc_block.h>
33 #include <vlc_sout.h>
34 #include <vlc_codecs.h>
35 #include <vlc_input.h>
36
37 #include "vlc_block_helper.h"
38
39 /* AAC Config in ES:
40  *
41  * AudioObjectType          5 bits
42  * samplingFrequencyIndex   4 bits
43  * if (samplingFrequencyIndex == 0xF)
44  *  samplingFrequency   24 bits
45  * channelConfiguration     4 bits
46  * GA_SpecificConfig
47  *  FrameLengthFlag         1 bit 1024 or 960
48  *  DependsOnCoreCoder      1 bit (always 0)
49  *  ExtensionFlag           1 bit (always 0)
50  */
51
52 /*****************************************************************************
53  * decoder_sys_t : decoder descriptor
54  *****************************************************************************/
55 struct decoder_sys_t
56 {
57     /*
58      * Input properties
59      */
60     int i_state;
61
62     block_bytestream_t bytestream;
63
64     /*
65      * Common properties
66      */
67     audio_date_t end_date;
68     mtime_t i_pts;
69
70     int i_frame_size;
71     unsigned int i_raw_blocks;
72     unsigned int i_channels;
73     unsigned int i_rate, i_frame_length, i_header_size;
74
75     int i_input_rate;
76 };
77
78 enum {
79
80     STATE_NOSYNC,
81     STATE_SYNC,
82     STATE_HEADER,
83     STATE_NEXT_SYNC,
84     STATE_GET_DATA,
85     STATE_SEND_DATA
86 };
87
88 static int i_sample_rates[] =
89 {
90     96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
91     16000, 12000, 11025, 8000,  7350,  0,     0,     0
92 };
93
94 #define ADTS_HEADER_SIZE 9
95
96 /****************************************************************************
97  * Local prototypes
98  ****************************************************************************/
99 static int  OpenPacketizer( vlc_object_t * );
100 static void ClosePacketizer( vlc_object_t * );
101
102 static block_t *PacketizeBlock    ( decoder_t *, block_t ** );
103 static block_t *ADTSPacketizeBlock( decoder_t *, block_t ** );
104
105 static uint8_t *GetOutBuffer ( decoder_t *, void ** );
106
107 static int ADTSSyncInfo( decoder_t *, const byte_t * p_buf,
108                          unsigned int * pi_channels,
109                          unsigned int * pi_sample_rate,
110                          unsigned int * pi_frame_length,
111                          unsigned int * pi_header_size,
112                          unsigned int * pi_raw_blocks );
113
114 /*****************************************************************************
115  * Module descriptor
116  *****************************************************************************/
117 vlc_module_begin();
118     set_category( CAT_SOUT );
119     set_subcategory( SUBCAT_SOUT_PACKETIZER );
120     set_description( _("MPEG4 audio packetizer") );
121     set_capability( "packetizer", 50 );
122     set_callbacks( OpenPacketizer, ClosePacketizer );
123 vlc_module_end();
124
125 /*****************************************************************************
126  * OpenPacketizer: probe the packetizer and return score
127  *****************************************************************************/
128 static int OpenPacketizer( vlc_object_t *p_this )
129 {
130     decoder_t *p_dec = (decoder_t*)p_this;
131     decoder_sys_t *p_sys;
132
133     if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', '4', 'a' ) )
134     {
135         return VLC_EGENERIC;
136     }
137
138     /* Allocate the memory needed to store the decoder's structure */
139     if( ( p_dec->p_sys = p_sys =
140           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
141     {
142         msg_Err( p_dec, "out of memory" );
143         return VLC_EGENERIC;
144     }
145
146     /* Misc init */
147     p_sys->i_state = STATE_NOSYNC;
148     aout_DateSet( &p_sys->end_date, 0 );
149     p_sys->bytestream = block_BytestreamInit( p_dec );
150     p_sys->i_input_rate = INPUT_RATE_DEFAULT;
151
152     /* Set output properties */
153     p_dec->fmt_out.i_cat = AUDIO_ES;
154     p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','4','a');
155
156     /* Set callback */
157     p_dec->pf_packetize = PacketizeBlock;
158
159     msg_Dbg( p_dec, "running MPEG4 audio packetizer" );
160
161     if( p_dec->fmt_in.i_extra > 0 )
162     {
163         uint8_t *p_config = (uint8_t*)p_dec->fmt_in.p_extra;
164         int     i_index;
165
166         i_index = ( ( p_config[0] << 1 ) | ( p_config[1] >> 7 ) ) & 0x0f;
167         if( i_index != 0x0f )
168         {
169             p_dec->fmt_out.audio.i_rate = i_sample_rates[i_index];
170             p_dec->fmt_out.audio.i_frame_length =
171                 (( p_config[1] >> 2 ) & 0x01) ? 960 : 1024;
172         }
173         else
174         {
175             p_dec->fmt_out.audio.i_rate = ( ( p_config[1] & 0x7f ) << 17 ) |
176                 ( p_config[2] << 9 ) | ( p_config[3] << 1 ) |
177                 ( p_config[4] >> 7 );
178             p_dec->fmt_out.audio.i_frame_length =
179                 (( p_config[4] >> 2 ) & 0x01) ? 960 : 1024;
180         }
181
182         msg_Dbg( p_dec, "AAC %dHz %d samples/frame",
183                  p_dec->fmt_out.audio.i_rate,
184                  p_dec->fmt_out.audio.i_frame_length );
185
186         aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
187
188         p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
189         p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
190         p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra );
191         memcpy( p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
192                 p_dec->fmt_in.i_extra );
193     }
194     else
195     {
196         msg_Dbg( p_dec, "no decoder specific info, must be an ADTS stream" );
197
198         aout_DateInit( &p_sys->end_date, p_dec->fmt_in.audio.i_rate );
199
200         /* We will try to create a AAC Config from adts */
201         p_dec->fmt_out.i_extra = 0;
202         p_dec->fmt_out.p_extra = NULL;
203         p_dec->pf_packetize = ADTSPacketizeBlock;
204     }
205
206     return VLC_SUCCESS;
207 }
208
209 /****************************************************************************
210  * PacketizeBlock: the whole thing
211  ****************************************************************************
212  * This function must be fed with complete frames.
213  ****************************************************************************/
214 static block_t *PacketizeBlock( decoder_t *p_dec, block_t **pp_block )
215 {
216     decoder_sys_t *p_sys = p_dec->p_sys;
217     block_t *p_block;
218
219     if( !pp_block || !*pp_block ) return NULL;
220
221     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
222     {
223         //aout_DateSet( &p_sys->end_date, 0 );
224         block_Release( *pp_block );
225         return NULL;
226     }
227
228     p_block = *pp_block;
229     *pp_block = NULL; /* Don't reuse this block */
230
231     if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
232     {
233         /* We've just started the stream, wait for the first PTS. */
234         block_Release( p_block );
235         return NULL;
236     }
237     else if( p_block->i_pts != 0 &&
238              p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
239     {
240         aout_DateSet( &p_sys->end_date, p_block->i_pts );
241     }
242
243     p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
244
245     p_block->i_length = aout_DateIncrement( &p_sys->end_date,
246         p_dec->fmt_out.audio.i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) - p_block->i_pts;
247
248     return p_block;
249 }
250
251 /****************************************************************************
252  * DTSPacketizeBlock: the whole thing
253  ****************************************************************************/
254 static block_t *ADTSPacketizeBlock( decoder_t *p_dec, block_t **pp_block )
255 {
256     decoder_sys_t *p_sys = p_dec->p_sys;
257     uint8_t p_header[ADTS_HEADER_SIZE];
258     void *p_out_buffer;
259     uint8_t *p_buf;
260
261     if( !pp_block || !*pp_block ) return NULL;
262
263     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
264     {
265         if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
266         {
267             p_sys->i_state = STATE_NOSYNC;
268             block_BytestreamFlush( &p_sys->bytestream );
269         }
270         //aout_DateSet( &p_sys->end_date, 0 );
271         block_Release( *pp_block );
272         return NULL;
273     }
274
275     if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
276     {
277         /* We've just started the stream, wait for the first PTS. */
278         block_Release( *pp_block );
279         return NULL;
280     }
281
282     if( (*pp_block)->i_rate > 0 )
283         p_sys->i_input_rate = (*pp_block)->i_rate;
284
285     block_BytestreamPush( &p_sys->bytestream, *pp_block );
286
287     while( 1 )
288     {
289         switch( p_sys->i_state )
290         {
291
292         case STATE_NOSYNC:
293             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
294                    == VLC_SUCCESS )
295             {
296                 /* Look for sync word - should be 0xfff + 2 layer bits */
297                 if( p_header[0] == 0xff && (p_header[1] & 0xf6) == 0xf0 )
298                 {
299                     p_sys->i_state = STATE_SYNC;
300                     break;
301                 }
302                 block_SkipByte( &p_sys->bytestream );
303             }
304             if( p_sys->i_state != STATE_SYNC )
305             {
306                 block_BytestreamFlush( &p_sys->bytestream );
307
308                 /* Need more data */
309                 return NULL;
310             }
311
312         case STATE_SYNC:
313             /* New frame, set the Presentation Time Stamp */
314             p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
315             if( p_sys->i_pts != 0 &&
316                 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
317             {
318                 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
319             }
320             p_sys->i_state = STATE_HEADER;
321             break;
322
323         case STATE_HEADER:
324             /* Get ADTS frame header (ADTS_HEADER_SIZE bytes) */
325             if( block_PeekBytes( &p_sys->bytestream, p_header,
326                                  ADTS_HEADER_SIZE ) != VLC_SUCCESS )
327             {
328                 /* Need more data */
329                 return NULL;
330             }
331
332             /* Check if frame is valid and get frame info */
333             p_sys->i_frame_size = ADTSSyncInfo( p_dec, p_header,
334                                                 &p_sys->i_channels,
335                                                 &p_sys->i_rate,
336                                                 &p_sys->i_frame_length,
337                                                 &p_sys->i_header_size,
338                                                 &p_sys->i_raw_blocks );
339             if( p_sys->i_frame_size <= 0 )
340             {
341                 msg_Dbg( p_dec, "emulated sync word" );
342                 block_SkipByte( &p_sys->bytestream );
343                 p_sys->i_state = STATE_NOSYNC;
344                 break;
345             }
346
347             p_sys->i_state = STATE_NEXT_SYNC;
348
349         case STATE_NEXT_SYNC:
350             /* TODO: If p_block == NULL, flush the buffer without checking the
351              * next sync word */
352
353             /* Check if next expected frame contains the sync word */
354             if( block_PeekOffsetBytes( &p_sys->bytestream, p_sys->i_frame_size
355                                        + p_sys->i_header_size, p_header, 2 )
356                 != VLC_SUCCESS )
357             {
358                 /* Need more data */
359                 return NULL;
360             }
361
362             if( p_header[0] != 0xff || (p_header[1] & 0xf6) != 0xf0 )
363             {
364                 msg_Dbg( p_dec, "emulated sync word "
365                          "(no sync on following frame)" );
366                 p_sys->i_state = STATE_NOSYNC;
367                 block_SkipByte( &p_sys->bytestream );
368                 break;
369             }
370
371             p_sys->i_state = STATE_SEND_DATA;
372             break;
373
374         case STATE_GET_DATA:
375             /* Make sure we have enough data.
376              * (Not useful if we went through NEXT_SYNC) */
377             if( block_WaitBytes( &p_sys->bytestream, p_sys->i_frame_size +
378                                  p_sys->i_header_size) != VLC_SUCCESS )
379             {
380                 /* Need more data */
381                 return NULL;
382             }
383             p_sys->i_state = STATE_SEND_DATA;
384
385         case STATE_SEND_DATA:
386             if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
387             {
388                 //p_dec->b_error = VLC_TRUE;
389                 return NULL;
390             }
391
392             /* When we reach this point we already know we have enough
393              * data available. */
394
395             /* Skip the ADTS header */
396             block_SkipBytes( &p_sys->bytestream, p_sys->i_header_size );
397
398             /* Copy the whole frame into the buffer */
399             block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
400
401             /* Make sure we don't reuse the same pts twice */
402             if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
403                 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
404
405             /* So p_block doesn't get re-added several times */
406             *pp_block = block_BytestreamPop( &p_sys->bytestream );
407
408             p_sys->i_state = STATE_NOSYNC;
409
410             return p_out_buffer;
411         }
412     }
413
414     return NULL;
415 }
416
417 /*****************************************************************************
418  * GetOutBuffer:
419  *****************************************************************************/
420 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
421 {
422     decoder_sys_t *p_sys = p_dec->p_sys;
423     block_t *p_block;
424
425     if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
426     {
427         msg_Info( p_dec, "AAC channels: %d samplerate: %d",
428                   p_sys->i_channels, p_sys->i_rate );
429
430         aout_DateInit( &p_sys->end_date, p_sys->i_rate );
431         aout_DateSet( &p_sys->end_date, p_sys->i_pts );
432     }
433
434     p_dec->fmt_out.audio.i_rate     = p_sys->i_rate;
435     p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
436     p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_frame_size;
437     p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
438
439 #if 0
440     p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
441     p_dec->fmt_out.audio.i_physical_channels =
442         p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
443 #endif
444
445     p_block = block_New( p_dec, p_sys->i_frame_size );
446     if( p_block == NULL ) return NULL;
447
448     p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
449
450     p_block->i_length = aout_DateIncrement( &p_sys->end_date,
451                             p_sys->i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) -
452                                 p_block->i_pts;
453
454     *pp_out_buffer = p_block;
455     return p_block->p_buffer;
456 }
457
458 /*****************************************************************************
459  * ClosePacketizer: clean up the packetizer
460  *****************************************************************************/
461 static void ClosePacketizer( vlc_object_t *p_this )
462 {
463     decoder_t *p_dec = (decoder_t *)p_this;
464     decoder_sys_t *p_sys = p_dec->p_sys;
465
466     block_BytestreamRelease( &p_sys->bytestream );
467
468     free( p_dec->p_sys );
469 }
470
471 /*****************************************************************************
472  * ADTSSyncInfo: parse MPEG 4 audio ADTS sync info
473  *****************************************************************************/
474 static int ADTSSyncInfo( decoder_t * p_dec, const byte_t * p_buf,
475                          unsigned int * pi_channels,
476                          unsigned int * pi_sample_rate,
477                          unsigned int * pi_frame_length,
478                          unsigned int * pi_header_size,
479                          unsigned int * pi_raw_blocks_in_frame )
480 {
481     int i_id, i_profile, i_sample_rate_idx, i_frame_size;
482     vlc_bool_t b_crc;
483
484     /* Fixed header between frames */
485     i_id = ( (p_buf[1] >> 3) & 0x01 ) ? 2 : 4;
486     b_crc = !(p_buf[1] & 0x01);
487     i_profile = p_buf[2] >> 6;
488     i_sample_rate_idx = (p_buf[2] >> 2) & 0x0f;
489     *pi_sample_rate = i_sample_rates[i_sample_rate_idx];
490     *pi_channels = ((p_buf[2] & 0x01) << 2) | ((p_buf[3] >> 6) & 0x03);
491
492     /* Variable header */
493     i_frame_size = ((p_buf[3] & 0x03) << 11) | (p_buf[4] << 3) |
494                    ((p_buf[5] >> 5) & 0x7);
495     *pi_raw_blocks_in_frame = (p_buf[6] & 0x02) + 1;
496
497     if( !*pi_sample_rate || !*pi_channels || !i_frame_size )
498     {
499         return 0;
500     }
501
502     /* Fixme */
503     *pi_frame_length = 1024;
504
505     /* Build the decoder specific info header */
506     if( !p_dec->fmt_out.i_extra )
507     {
508         p_dec->fmt_out.i_extra = 2;
509         p_dec->fmt_out.p_extra = malloc( 2 );
510         ((uint8_t *)p_dec->fmt_out.p_extra)[0] =
511             (i_profile + 1) << 3 | (i_sample_rate_idx >> 1);
512         ((uint8_t *)p_dec->fmt_out.p_extra)[1] =
513             ((i_sample_rate_idx & 0x01) << 7) | (*pi_channels <<3);
514     }
515
516     /* ADTS header length */
517     *pi_header_size = b_crc ? 9 : 7;
518
519     return i_frame_size - *pi_header_size;
520 }