Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

mpeg4audio.c

00001 /*****************************************************************************
00002  * mpeg4audio.c: parse and packetize an MPEG 4 audio stream
00003  *****************************************************************************
00004  * Copyright (C) 2001, 2002 the VideoLAN team
00005  * $Id: mpeg4audio.c 11664 2005-07-09 06:17:09Z courmisch $
00006  *
00007  * Authors: Laurent Aimar <[email protected]>
00008  *          Gildas Bazin <[email protected]>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 
00025 /*****************************************************************************
00026  * Preamble
00027  *****************************************************************************/
00028 #include <stdlib.h>                                      /* malloc(), free() */
00029 #include <string.h>                                              /* strdup() */
00030 
00031 #include <vlc/vlc.h>
00032 #include <vlc/aout.h>
00033 #include <vlc/decoder.h>
00034 #include <vlc/input.h>
00035 #include <vlc/sout.h>
00036 #include "codecs.h"
00037 
00038 #include "vlc_block_helper.h"
00039 
00040 /* AAC Config in ES:
00041  *
00042  * AudioObjectType          5 bits
00043  * samplingFrequencyIndex   4 bits
00044  * if (samplingFrequencyIndex == 0xF)
00045  *  samplingFrequency   24 bits
00046  * channelConfiguration     4 bits
00047  * GA_SpecificConfig
00048  *  FrameLengthFlag         1 bit 1024 or 960
00049  *  DependsOnCoreCoder      1 bit (always 0)
00050  *  ExtensionFlag           1 bit (always 0)
00051  */
00052 
00053 /*****************************************************************************
00054  * decoder_sys_t : decoder descriptor
00055  *****************************************************************************/
00056 struct decoder_sys_t
00057 {
00058     /*
00059      * Input properties
00060      */
00061     int i_state;
00062 
00063     block_bytestream_t bytestream;
00064 
00065     /*
00066      * Common properties
00067      */
00068     audio_date_t end_date;
00069     mtime_t i_pts;
00070 
00071     int i_frame_size, i_raw_blocks;
00072     unsigned int i_channels;
00073     unsigned int i_rate, i_frame_length, i_header_size;
00074 };
00075 
00076 enum {
00077 
00078     STATE_NOSYNC,
00079     STATE_SYNC,
00080     STATE_HEADER,
00081     STATE_NEXT_SYNC,
00082     STATE_GET_DATA,
00083     STATE_SEND_DATA
00084 };
00085 
00086 static int i_sample_rates[] =
00087 {
00088     96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
00089     16000, 12000, 11025, 8000,  7350,  0,     0,     0
00090 };
00091 
00092 #define ADTS_HEADER_SIZE 9
00093 
00094 /****************************************************************************
00095  * Local prototypes
00096  ****************************************************************************/
00097 static int  OpenPacketizer( vlc_object_t * );
00098 static void ClosePacketizer( vlc_object_t * );
00099 
00100 static block_t *PacketizeBlock    ( decoder_t *, block_t ** );
00101 static block_t *ADTSPacketizeBlock( decoder_t *, block_t ** );
00102 
00103 static uint8_t *GetOutBuffer ( decoder_t *, void ** );
00104 
00105 static int ADTSSyncInfo( decoder_t *, const byte_t * p_buf,
00106                          unsigned int * pi_channels,
00107                          unsigned int * pi_sample_rate,
00108                          unsigned int * pi_frame_length,
00109                          unsigned int * pi_header_size,
00110                          unsigned int * pi_raw_blocks );
00111 
00112 /*****************************************************************************
00113  * Module descriptor
00114  *****************************************************************************/
00115 vlc_module_begin();
00116     set_category( CAT_SOUT );
00117     set_subcategory( SUBCAT_SOUT_PACKETIZER );
00118     set_description( _("MPEG4 audio packetizer") );
00119     set_capability( "packetizer", 50 );
00120     set_callbacks( OpenPacketizer, ClosePacketizer );
00121 vlc_module_end();
00122 
00123 /*****************************************************************************
00124  * OpenPacketizer: probe the packetizer and return score
00125  *****************************************************************************/
00126 static int OpenPacketizer( vlc_object_t *p_this )
00127 {
00128     decoder_t *p_dec = (decoder_t*)p_this;
00129     decoder_sys_t *p_sys;
00130 
00131     if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', '4', 'a' ) )
00132     {
00133         return VLC_EGENERIC;
00134     }
00135 
00136     /* Allocate the memory needed to store the decoder's structure */
00137     if( ( p_dec->p_sys = p_sys =
00138           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
00139     {
00140         msg_Err( p_dec, "out of memory" );
00141         return VLC_EGENERIC;
00142     }
00143 
00144     /* Misc init */
00145     p_sys->i_state = STATE_NOSYNC;
00146     aout_DateSet( &p_sys->end_date, 0 );
00147     p_sys->bytestream = block_BytestreamInit( p_dec );
00148 
00149     /* Set output properties */
00150     p_dec->fmt_out.i_cat = AUDIO_ES;
00151     p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','4','a');
00152 
00153     /* Set callback */
00154     p_dec->pf_packetize = PacketizeBlock;
00155 
00156     msg_Info( p_dec, "running MPEG4 audio packetizer" );
00157 
00158     if( p_dec->fmt_in.i_extra > 0 )
00159     {
00160         uint8_t *p_config = (uint8_t*)p_dec->fmt_in.p_extra;
00161         int     i_index;
00162 
00163         i_index = ( ( p_config[0] << 1 ) | ( p_config[1] >> 7 ) ) & 0x0f;
00164         if( i_index != 0x0f )
00165         {
00166             p_dec->fmt_out.audio.i_rate = i_sample_rates[i_index];
00167             p_dec->fmt_out.audio.i_frame_length =
00168                 (( p_config[1] >> 2 ) & 0x01) ? 960 : 1024;
00169         }
00170         else
00171         {
00172             p_dec->fmt_out.audio.i_rate = ( ( p_config[1] & 0x7f ) << 17 ) |
00173                 ( p_config[2] << 9 ) | ( p_config[3] << 1 ) |
00174                 ( p_config[4] >> 7 );
00175             p_dec->fmt_out.audio.i_frame_length =
00176                 (( p_config[4] >> 2 ) & 0x01) ? 960 : 1024;
00177         }
00178 
00179         msg_Dbg( p_dec, "AAC %dHz %d samples/frame",
00180                  p_dec->fmt_out.audio.i_rate,
00181                  p_dec->fmt_out.audio.i_frame_length );
00182 
00183         aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
00184 
00185         p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
00186         p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
00187         p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra );
00188         memcpy( p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
00189                 p_dec->fmt_in.i_extra );
00190     }
00191     else
00192     {
00193         msg_Dbg( p_dec, "no decoder specific info, must be an ADTS stream" );
00194 
00195         /* We will try to create a AAC Config from adts */
00196         p_dec->fmt_out.i_extra = 0;
00197         p_dec->fmt_out.p_extra = NULL;
00198         p_dec->pf_packetize = ADTSPacketizeBlock;
00199     }
00200 
00201     return VLC_SUCCESS;
00202 }
00203 
00204 /****************************************************************************
00205  * PacketizeBlock: the whole thing
00206  ****************************************************************************
00207  * This function must be fed with complete frames.
00208  ****************************************************************************/
00209 static block_t *PacketizeBlock( decoder_t *p_dec, block_t **pp_block )
00210 {
00211     decoder_sys_t *p_sys = p_dec->p_sys;
00212     block_t *p_block;
00213 
00214     if( !pp_block || !*pp_block ) return NULL;
00215 
00216     p_block = *pp_block;
00217     *pp_block = NULL; /* Don't reuse this block */
00218 
00219     if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
00220     {
00221         /* We've just started the stream, wait for the first PTS. */
00222         block_Release( p_block );
00223         return NULL;
00224     }
00225     else if( p_block->i_pts != 0 &&
00226              p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
00227     {
00228         aout_DateSet( &p_sys->end_date, p_block->i_pts );
00229     }
00230 
00231     p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
00232 
00233     p_block->i_length = aout_DateIncrement( &p_sys->end_date,
00234         p_dec->fmt_out.audio.i_frame_length ) - p_block->i_pts;
00235 
00236     return p_block;
00237 }
00238 
00239 /****************************************************************************
00240  * DTSPacketizeBlock: the whole thing
00241  ****************************************************************************/
00242 static block_t *ADTSPacketizeBlock( decoder_t *p_dec, block_t **pp_block )
00243 {
00244     decoder_sys_t *p_sys = p_dec->p_sys;
00245     uint8_t p_header[ADTS_HEADER_SIZE];
00246     void *p_out_buffer;
00247     uint8_t *p_buf;
00248 
00249     if( !pp_block || !*pp_block ) return NULL;
00250 
00251     if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
00252     {
00253         /* We've just started the stream, wait for the first PTS. */
00254         block_Release( *pp_block );
00255         return NULL;
00256     }
00257 
00258     if( (*pp_block)->i_flags&BLOCK_FLAG_DISCONTINUITY )
00259     {
00260         p_sys->i_state = STATE_NOSYNC;
00261     }
00262 
00263     block_BytestreamPush( &p_sys->bytestream, *pp_block );
00264 
00265     while( 1 )
00266     {
00267         switch( p_sys->i_state )
00268         {
00269 
00270         case STATE_NOSYNC:
00271             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
00272                    == VLC_SUCCESS )
00273             {
00274                 /* Look for sync word - should be 0xfff + 2 layer bits */
00275                 if( p_header[0] == 0xff && (p_header[1] & 0xf6) == 0xf0 )
00276                 {
00277                     p_sys->i_state = STATE_SYNC;
00278                     break;
00279                 }
00280                 block_SkipByte( &p_sys->bytestream );
00281             }
00282             if( p_sys->i_state != STATE_SYNC )
00283             {
00284                 block_BytestreamFlush( &p_sys->bytestream );
00285 
00286                 /* Need more data */
00287                 return NULL;
00288             }
00289 
00290         case STATE_SYNC:
00291             /* New frame, set the Presentation Time Stamp */
00292             p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
00293             if( p_sys->i_pts != 0 &&
00294                 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
00295             {
00296                 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
00297             }
00298             p_sys->i_state = STATE_HEADER;
00299             break;
00300 
00301         case STATE_HEADER:
00302             /* Get ADTS frame header (ADTS_HEADER_SIZE bytes) */
00303             if( block_PeekBytes( &p_sys->bytestream, p_header,
00304                                  ADTS_HEADER_SIZE ) != VLC_SUCCESS )
00305             {
00306                 /* Need more data */
00307                 return NULL;
00308             }
00309 
00310             /* Check if frame is valid and get frame info */
00311             p_sys->i_frame_size = ADTSSyncInfo( p_dec, p_header,
00312                                                 &p_sys->i_channels,
00313                                                 &p_sys->i_rate,
00314                                                 &p_sys->i_frame_length,
00315                                                 &p_sys->i_header_size,
00316                                                 &p_sys->i_raw_blocks );
00317             if( p_sys->i_frame_size <= 0 )
00318             {
00319                 msg_Dbg( p_dec, "emulated sync word" );
00320                 block_SkipByte( &p_sys->bytestream );
00321                 p_sys->i_state = STATE_NOSYNC;
00322                 break;
00323             }
00324 
00325             p_sys->i_state = STATE_NEXT_SYNC;
00326 
00327         case STATE_NEXT_SYNC:
00328             /* TODO: If p_block == NULL, flush the buffer without checking the
00329              * next sync word */
00330 
00331             /* Check if next expected frame contains the sync word */
00332             if( block_PeekOffsetBytes( &p_sys->bytestream, p_sys->i_frame_size
00333                                        + p_sys->i_header_size, p_header, 2 )
00334                 != VLC_SUCCESS )
00335             {
00336                 /* Need more data */
00337                 return NULL;
00338             }
00339 
00340             if( p_header[0] != 0xff || (p_header[1] & 0xf6) != 0xf0 )
00341             {
00342                 msg_Dbg( p_dec, "emulated sync word "
00343                          "(no sync on following frame)" );
00344                 p_sys->i_state = STATE_NOSYNC;
00345                 block_SkipByte( &p_sys->bytestream );
00346                 break;
00347             }
00348 
00349             p_sys->i_state = STATE_SEND_DATA;
00350             break;
00351 
00352         case STATE_GET_DATA:
00353             /* Make sure we have enough data.
00354              * (Not useful if we went through NEXT_SYNC) */
00355             if( block_WaitBytes( &p_sys->bytestream, p_sys->i_frame_size +
00356                                  p_sys->i_header_size) != VLC_SUCCESS )
00357             {
00358                 /* Need more data */
00359                 return NULL;
00360             }
00361             p_sys->i_state = STATE_SEND_DATA;
00362 
00363         case STATE_SEND_DATA:
00364             if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
00365             {
00366                 //p_dec->b_error = VLC_TRUE;
00367                 return NULL;
00368             }
00369 
00370             /* When we reach this point we already know we have enough
00371              * data available. */
00372 
00373             /* Skip the ADTS header */
00374             block_SkipBytes( &p_sys->bytestream, p_sys->i_header_size );
00375 
00376             /* Copy the whole frame into the buffer */
00377             block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
00378 
00379             /* Make sure we don't reuse the same pts twice */
00380             if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
00381                 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
00382 
00383             /* So p_block doesn't get re-added several times */
00384             *pp_block = block_BytestreamPop( &p_sys->bytestream );
00385 
00386             p_sys->i_state = STATE_NOSYNC;
00387 
00388             return p_out_buffer;
00389         }
00390     }
00391 
00392     return NULL;
00393 }
00394 
00395 /*****************************************************************************
00396  * GetOutBuffer:
00397  *****************************************************************************/
00398 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
00399 {
00400     decoder_sys_t *p_sys = p_dec->p_sys;
00401     block_t *p_block;
00402 
00403     if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
00404     {
00405         msg_Info( p_dec, "AAC channels: %d samplerate: %d",
00406                   p_sys->i_channels, p_sys->i_rate );
00407 
00408         aout_DateInit( &p_sys->end_date, p_sys->i_rate );
00409         aout_DateSet( &p_sys->end_date, p_sys->i_pts );
00410     }
00411 
00412     p_dec->fmt_out.audio.i_rate     = p_sys->i_rate;
00413     p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
00414     p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_frame_size;
00415     p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
00416 
00417 #if 0
00418     p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
00419     p_dec->fmt_out.audio.i_physical_channels =
00420         p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
00421 #endif
00422 
00423     p_block = block_New( p_dec, p_sys->i_frame_size );
00424     if( p_block == NULL ) return NULL;
00425 
00426     p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
00427 
00428     p_block->i_length = aout_DateIncrement( &p_sys->end_date,
00429                             p_sys->i_frame_length ) - p_block->i_pts;
00430 
00431     *pp_out_buffer = p_block;
00432     return p_block->p_buffer;
00433 }
00434 
00435 /*****************************************************************************
00436  * ClosePacketizer: clean up the packetizer
00437  *****************************************************************************/
00438 static void ClosePacketizer( vlc_object_t *p_this )
00439 {
00440     decoder_t *p_dec = (decoder_t *)p_this;
00441     decoder_sys_t *p_sys = p_dec->p_sys;
00442 
00443     block_BytestreamRelease( &p_sys->bytestream );
00444 
00445     free( p_dec->p_sys );
00446 }
00447 
00448 /*****************************************************************************
00449  * ADTSSyncInfo: parse MPEG 4 audio ADTS sync info
00450  *****************************************************************************/
00451 static int ADTSSyncInfo( decoder_t * p_dec, const byte_t * p_buf,
00452                          unsigned int * pi_channels,
00453                          unsigned int * pi_sample_rate,
00454                          unsigned int * pi_frame_length,
00455                          unsigned int * pi_header_size,
00456                          unsigned int * pi_raw_blocks_in_frame )
00457 {
00458     int i_id, i_profile, i_sample_rate_idx, i_frame_size;
00459     vlc_bool_t b_crc;
00460 
00461     /* Fixed header between frames */
00462     i_id = ( (p_buf[1] >> 3) & 0x01 ) ? 2 : 4;
00463     b_crc = !(p_buf[1] & 0x01);
00464     i_profile = p_buf[2] >> 6;
00465     i_sample_rate_idx = (p_buf[2] >> 2) & 0x0f;
00466     *pi_sample_rate = i_sample_rates[i_sample_rate_idx];
00467     *pi_channels = ((p_buf[2] & 0x01) << 2) | ((p_buf[3] >> 6) & 0x03);
00468 
00469     /* Variable header */
00470     i_frame_size = ((p_buf[3] & 0x03) << 11) | (p_buf[4] << 3) |
00471                    ((p_buf[5] >> 5) & 0x7);
00472     *pi_raw_blocks_in_frame = (p_buf[6] & 0x02) + 1;
00473 
00474     if( !*pi_sample_rate || !*pi_channels || !i_frame_size )
00475     {
00476         return 0;
00477     }
00478 
00479     /* Fixme */
00480     *pi_frame_length = 1024;
00481 
00482     /* Build the decoder specific info header */
00483     if( !p_dec->fmt_out.i_extra )
00484     {
00485         p_dec->fmt_out.i_extra = 2;
00486         p_dec->fmt_out.p_extra = malloc( 2 );
00487         ((uint8_t *)p_dec->fmt_out.p_extra)[0] =
00488             (i_profile + 1) << 3 | (i_sample_rate_idx >> 1);
00489         ((uint8_t *)p_dec->fmt_out.p_extra)[1] =
00490             ((i_sample_rate_idx & 0x01) << 7) | (*pi_channels <<3);
00491     }
00492 
00493     /* ADTS header length */
00494     *pi_header_size = b_crc ? 9 : 7;
00495 
00496     return i_frame_size - *pi_header_size;
00497 }

Generated on Tue Dec 20 10:14:49 2005 for vlc-0.8.4a by  doxygen 1.4.2