]> git.sesse.net Git - vlc/blobdiff - modules/packetizer/mpeg4audio.c
vlc_bits: Differentiate between writable bits stream and read only.
[vlc] / modules / packetizer / mpeg4audio.c
index 2bf711bf5e5da99b127be4dba02082280a56f4ab..2a9b5c61added8cf3a030bf1c068608697e69c41 100644 (file)
@@ -37,7 +37,7 @@
 #include <vlc_block.h>
 #include <vlc_bits.h>
 
-#include "vlc_block_helper.h"
+#include <vlc_block_helper.h>
 
 #include <assert.h>
 
@@ -63,6 +63,7 @@ typedef struct
     int i_samplerate;
     int i_channel;
     int i_sbr;          // 0: no sbr, 1: sbr, -1: unknown
+    int i_ps;           // 0: no ps,  1: ps,  -1: unknown
 
     struct
     {
@@ -125,7 +126,7 @@ struct decoder_sys_t
     /*
      * Common properties
      */
-    audio_date_t end_date;
+    date_t  end_date;
     mtime_t i_pts;
 
     int i_frame_size;
@@ -192,7 +193,7 @@ static int OpenPacketizer( vlc_object_t *p_this )
     decoder_t *p_dec = (decoder_t*)p_this;
     decoder_sys_t *p_sys;
 
-    if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', '4', 'a' ) )
+    if( p_dec->fmt_in.i_codec != VLC_CODEC_MP4A )
     {
         return VLC_EGENERIC;
     }
@@ -204,13 +205,13 @@ static int OpenPacketizer( vlc_object_t *p_this )
 
     /* Misc init */
     p_sys->i_state = STATE_NOSYNC;
-    aout_DateSet( &p_sys->end_date, 0 );
+    date_Set( &p_sys->end_date, 0 );
     p_sys->bytestream = block_BytestreamInit();
     p_sys->b_latm_cfg = false;
 
     /* Set output properties */
     p_dec->fmt_out.i_cat = AUDIO_ES;
-    p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','4','a');
+    p_dec->fmt_out.i_codec = VLC_CODEC_MP4A;
 
     msg_Dbg( p_dec, "running MPEG4 audio packetizer" );
 
@@ -242,7 +243,7 @@ static int OpenPacketizer( vlc_object_t *p_this )
                  p_dec->fmt_out.audio.i_rate,
                  p_dec->fmt_out.audio.i_frame_length );
 
-        aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
+        date_Init( &p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1 );
 
         p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
         p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra );
@@ -262,7 +263,7 @@ static int OpenPacketizer( vlc_object_t *p_this )
     {
         msg_Dbg( p_dec, "no decoder specific info, must be an ADTS or LOAS stream" );
 
-        aout_DateInit( &p_sys->end_date, p_dec->fmt_in.audio.i_rate );
+        date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
 
         /* We will try to create a AAC Config from adts/loas */
         p_dec->fmt_out.i_extra = 0;
@@ -290,7 +291,7 @@ static block_t *PacketizeRawBlock( decoder_t *p_dec, block_t **pp_block )
 
     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
     {
-        aout_DateSet( &p_sys->end_date, 0 );
+        date_Set( &p_sys->end_date, 0 );
         block_Release( *pp_block );
         return NULL;
     }
@@ -298,21 +299,21 @@ static block_t *PacketizeRawBlock( decoder_t *p_dec, block_t **pp_block )
     p_block = *pp_block;
     *pp_block = NULL; /* Don't reuse this block */
 
-    if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
+    if( !date_Get( &p_sys->end_date ) && !p_block->i_pts )
     {
         /* We've just started the stream, wait for the first PTS. */
         block_Release( p_block );
         return NULL;
     }
     else if( p_block->i_pts != 0 &&
-             p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
+             p_block->i_pts != date_Get( &p_sys->end_date ) )
     {
-        aout_DateSet( &p_sys->end_date, p_block->i_pts );
+        date_Set( &p_sys->end_date, p_block->i_pts );
     }
 
-    p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
+    p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
 
-    p_block->i_length = aout_DateIncrement( &p_sys->end_date,
+    p_block->i_length = date_Increment( &p_sys->end_date,
         p_dec->fmt_out.audio.i_frame_length ) - p_block->i_pts;
 
     return p_block;
@@ -429,88 +430,88 @@ static int LOASSyncInfo( uint8_t p_header[LOAS_HEADER_SIZE], unsigned int *pi_he
     return ( ( p_header[1] & 0x1f ) << 8 ) + p_header[2];
 }
 
-static int Mpeg4GAProgramConfigElement( bs_t *s )
+static int Mpeg4GAProgramConfigElement( bsw_t *s )
 {
     /* TODO compute channels count ? */
-    int i_tag = bs_read( s, 4 );
+    int i_tag = bsw_read( s, 4 );
     if( i_tag != 0x05 )
         return -1;
-    bs_skip( s, 2 + 4 ); // object type + sampling index
-    int i_num_front = bs_read( s, 4 );
-    int i_num_side = bs_read( s, 4 );
-    int i_num_back = bs_read( s, 4 );
-    int i_num_lfe = bs_read( s, 2 );
-    int i_num_assoc_data = bs_read( s, 3 );
-    int i_num_valid_cc = bs_read( s, 4 );
-
-    if( bs_read1(s) )
-        bs_skip( s, 4 ); // mono downmix
-    if( bs_read1(s) )
-        bs_skip( s, 4 ); // stereo downmix
-    if( bs_read1(s) )
-        bs_skip( s, 2+1 ); // matrix downmix + pseudo_surround
-
-    bs_skip( s, i_num_front * (1+4) );
-    bs_skip( s, i_num_side * (1+4) );
-    bs_skip( s, i_num_back * (1+4) );
-    bs_skip( s, i_num_lfe * (4) );
-    bs_skip( s, i_num_assoc_data * (4) );
-    bs_skip( s, i_num_valid_cc * (5) );
-    bs_align( s );
-    int i_comment = bs_read( s, 8 );
-    bs_skip( s, i_comment * 8 );
+    bsw_skip( s, 2 + 4 ); // object type + sampling index
+    int i_num_front = bsw_read( s, 4 );
+    int i_num_side = bsw_read( s, 4 );
+    int i_num_back = bsw_read( s, 4 );
+    int i_num_lfe = bsw_read( s, 2 );
+    int i_num_assoc_data = bsw_read( s, 3 );
+    int i_num_valid_cc = bsw_read( s, 4 );
+
+    if( bsw_read1(s) )
+        bsw_skip( s, 4 ); // mono downmix
+    if( bsw_read1(s) )
+        bsw_skip( s, 4 ); // stereo downmix
+    if( bsw_read1(s) )
+        bsw_skip( s, 2+1 ); // matrix downmix + pseudo_surround
+
+    bsw_skip( s, i_num_front * (1+4) );
+    bsw_skip( s, i_num_side * (1+4) );
+    bsw_skip( s, i_num_back * (1+4) );
+    bsw_skip( s, i_num_lfe * (4) );
+    bsw_skip( s, i_num_assoc_data * (4) );
+    bsw_skip( s, i_num_valid_cc * (5) );
+    bsw_align( s );
+    int i_comment = bsw_read( s, 8 );
+    bsw_skip( s, i_comment * 8 );
     return 0;
 }
 
-static int Mpeg4GASpecificConfig( mpeg4_cfg_t *p_cfg, bs_t *s )
+static int Mpeg4GASpecificConfig( mpeg4_cfg_t *p_cfg, bsw_t *s )
 {
-    p_cfg->i_frame_length = bs_read1(s) ? 960 : 1024;
+    p_cfg->i_frame_length = bsw_read1(s) ? 960 : 1024;
 
-    if( bs_read1( s ) )     // depend on core coder
-        bs_skip( s, 14 );   // core coder delay
+    if( bsw_read1( s ) )     // depend on core coder
+        bsw_skip( s, 14 );   // core coder delay
 
-    int i_extension_flag = bs_read1( s );
+    int i_extension_flag = bsw_read1( s );
     if( p_cfg->i_channel == 0 )
     {
         Mpeg4GAProgramConfigElement( s );
     }
     if( p_cfg->i_object_type == 6 || p_cfg->i_object_type == 20 )
-        bs_skip( s, 3 );    // layer
+        bsw_skip( s, 3 );    // layer
 
     if( i_extension_flag )
     {
         if( p_cfg->i_object_type == 22 )
         {
-            bs_skip( s, 5 + 11 );   // numOfSubFrame + layer length
+            bsw_skip( s, 5 + 11 );   // numOfSubFrame + layer length
         }
         if( p_cfg->i_object_type == 17 || p_cfg->i_object_type == 19 ||
             p_cfg->i_object_type == 20 || p_cfg->i_object_type == 23 )
         {
-            bs_skip( s, 1+1+1 );    // ER data : section scale spectral */
+            bsw_skip( s, 1+1+1 );    // ER data : section scale spectral */
         }
-        if( bs_read1( s ) )     // extension 3
+        if( bsw_read1( s ) )     // extension 3
             fprintf( stderr, "Mpeg4GASpecificConfig: error 1\n" );
     }
     return 0;
 }
 
-static int Mpeg4ReadAudioObjectType( bs_t *s )
+static int Mpeg4ReadAudioObjectType( bsw_t *s )
 {
-    int i_type = bs_read( s, 5 );
-    if( i_type == 0x1f )
-        i_type += bs_read( s, 6 );
+    int i_type = bsw_read( s, 5 );
+    if( i_type == 31 )
+        i_type = 32 + bsw_read( s, 6 );
     return i_type;
 }
 
-static int Mpeg4ReadAudioSamplerate( bs_t *s )
+static int Mpeg4ReadAudioSamplerate( bsw_t *s )
 {
-    int i_index = bs_read( s, 4 );
+    int i_index = bsw_read( s, 4 );
     if( i_index != 0x0f )
         return pi_sample_rates[i_index];
-    return bs_read( s, 24 );
+    return bsw_read( s, 24 );
 }
 
-static int Mpeg4ReadAudioSpecificInfo( mpeg4_cfg_t *p_cfg, int *pi_extra, uint8_t *p_extra, bs_t *s, int i_max_size )
+static int Mpeg4ReadAudioSpecificInfo( mpeg4_cfg_t *p_cfg, int *pi_extra, uint8_t *p_extra, bsw_t *s, int i_max_size )
 {
 #if 0
     static const char *ppsz_otype[] = {
@@ -527,13 +528,13 @@ static int Mpeg4ReadAudioSpecificInfo( mpeg4_cfg_t *p_cfg, int *pi_extra, uint8_
         "ER AAC LTP", "ER AAC Scalable", "ER TwinVQ", "ER BSAC", "ER AAC LD",
         "ER CELP", "ER HVXC", "ER HILN", "ER Parametric",
         "SSC",
-        "Reserved", "Reserved", "Escape",
+        "PS", "Reserved", "Escape",
         "Layer 1", "Layer 2", "Layer 3",
         "DST",
     };
 #endif
-    const int i_pos_start = bs_pos( s );
-    bs_t s_sav = *s;
+    const int i_pos_start = bsw_pos( s );
+    bsw_t s_sav = *s;
     int i_bits;
     int i;
 
@@ -543,19 +544,22 @@ static int Mpeg4ReadAudioSpecificInfo( mpeg4_cfg_t *p_cfg, int *pi_extra, uint8_
     p_cfg->i_object_type = Mpeg4ReadAudioObjectType( s );
     p_cfg->i_samplerate = Mpeg4ReadAudioSamplerate( s );
 
-    p_cfg->i_channel = bs_read( s, 4 );
+    p_cfg->i_channel = bsw_read( s, 4 );
     if( p_cfg->i_channel == 7 )
         p_cfg->i_channel = 8; // 7.1
     else if( p_cfg->i_channel >= 8 )
         p_cfg->i_channel = -1;
 
     p_cfg->i_sbr = -1;
+    p_cfg->i_ps  = -1;
     p_cfg->extension.i_object_type = 0;
     p_cfg->extension.i_samplerate = 0;
-    if( p_cfg->i_object_type == 5 )
+    if( p_cfg->i_object_type == 5 || p_cfg->i_object_type == 29 )
     {
         p_cfg->i_sbr = 1;
-        p_cfg->extension.i_object_type = p_cfg->i_object_type;
+        if( p_cfg->i_object_type == 29 )
+           p_cfg->i_ps = 1;
+        p_cfg->extension.i_object_type = 5;
         p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate( s );
 
         p_cfg->i_object_type = Mpeg4ReadAudioObjectType( s );
@@ -598,6 +602,9 @@ static int Mpeg4ReadAudioSpecificInfo( mpeg4_cfg_t *p_cfg, int *pi_extra, uint8_
     case 35:
         // DSTSpecificConfig();
         break;
+    case 36:
+        // ALSSpecificConfig();
+        break;
     default:
         // error
         break;
@@ -607,14 +614,14 @@ static int Mpeg4ReadAudioSpecificInfo( mpeg4_cfg_t *p_cfg, int *pi_extra, uint8_
     case 17: case 19: case 20: case 21: case 22: case 23:
     case 24: case 25: case 26: case 27:
     {
-        int epConfig = bs_read( s, 2 );
+        int epConfig = bsw_read( s, 2 );
         if( epConfig == 2 || epConfig == 3 )
         {
             //ErrorProtectionSpecificConfig();
         }
         if( epConfig == 3 )
         {
-            int directMapping = bs_read1( s );
+            int directMapping = bsw_read1( s );
             if( directMapping )
             {
                 // tbd ...
@@ -626,53 +633,59 @@ static int Mpeg4ReadAudioSpecificInfo( mpeg4_cfg_t *p_cfg, int *pi_extra, uint8_
         break;
     }
 
-    if( p_cfg->extension.i_object_type != 5 && i_max_size > 0 && i_max_size - (bs_pos(s) - i_pos_start) >= 16 && 
-        bs_read( s, 11 ) == 0x2b7 )
+    if( p_cfg->extension.i_object_type != 5 && i_max_size > 0 && i_max_size - (bsw_pos(s) - i_pos_start) >= 16 &&
+        bsw_read( s, 11 ) == 0x2b7 )
     {
         p_cfg->extension.i_object_type = Mpeg4ReadAudioObjectType( s );
         if( p_cfg->extension.i_object_type == 5 )
         {
-            p_cfg->i_sbr  = bs_read1( s );
+            p_cfg->i_sbr  = bsw_read1( s );
             if( p_cfg->i_sbr == 1 )
+            {
                 p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate( s );
+                if( i_max_size > 0 && i_max_size - (bsw_pos(s) - i_pos_start) >= 12 && bsw_read( s, 11 ) == 0x548 )
+                {
+                   p_cfg->i_ps = bsw_read1( s );
+                }
+            }
         }
     }
 
     //fprintf( stderr, "Mpeg4ReadAudioSpecificInfo: t=%s(%d)f=%d c=%d sbr=%d\n",
     //         ppsz_otype[p_cfg->i_object_type], p_cfg->i_object_type, p_cfg->i_samplerate, p_cfg->i_channel, p_cfg->i_sbr );
 
-    i_bits = bs_pos(s) - i_pos_start;
+    i_bits = bsw_pos(s) - i_pos_start;
 
-    *pi_extra = ( i_bits + 7 ) / 8;
-    for( i = 0; i < __MIN( LATM_MAX_EXTRA_SIZE, *pi_extra ); i++ )
+    *pi_extra = __MIN( ( i_bits + 7 ) / 8, LATM_MAX_EXTRA_SIZE );
+    for( i = 0; i < *pi_extra; i++ )
     {
         const int i_read = __MIN( 8, i_bits - 8*i );
-        p_extra[i] = bs_read( &s_sav, i_read ) << (8-i_read);
+        p_extra[i] = bsw_read( &s_sav, i_read ) << (8-i_read);
     }
     return i_bits;
 }
 
-static int LatmGetValue( bs_t *s )
+static int LatmGetValue( bsw_t *s )
 {
-    int i_bytes = bs_read( s, 2 );
+    int i_bytes = bsw_read( s, 2 );
     int v = 0;
     int i;
     for( i = 0; i < i_bytes; i++ )
-        v = (v << 8) + bs_read( s, 8 );
+        v = (v << 8) + bsw_read( s, 8 );
 
     return v;
 }
 
-static int LatmReadStreamMuxConfiguration( latm_mux_t *m, bs_t *s )
+static int LatmReadStreamMuxConfiguration( latm_mux_t *m, bsw_t *s )
 {
     int i_mux_version;
     int i_mux_versionA;
     int i_program;
 
-    i_mux_version = bs_read( s, 1 );
+    i_mux_version = bsw_read( s, 1 );
     i_mux_versionA = 0;
     if( i_mux_version )
-        i_mux_versionA = bs_read( s, 1 );
+        i_mux_versionA = bsw_read( s, 1 );
 
     if( i_mux_versionA != 0 ) /* support only A=0 */
         return -1;
@@ -687,15 +700,15 @@ static int LatmReadStreamMuxConfiguration( latm_mux_t *m, bs_t *s )
         }
     }
 
-    m->b_same_time_framing = bs_read1( s );
-    m->i_sub_frames = 1 + bs_read( s, 6 );
-    m->i_programs = 1 + bs_read( s, 4 );
+    m->b_same_time_framing = bsw_read1( s );
+    m->i_sub_frames = 1 + bsw_read( s, 6 );
+    m->i_programs = 1 + bsw_read( s, 4 );
 
     for( i_program = 0; i_program < m->i_programs; i_program++ )
     {
         int i_layer;
 
-        m->pi_layers[i_program] = 1+bs_read( s, 3 );
+        m->pi_layers[i_program] = 1+bsw_read( s, 3 );
 
         for( i_layer = 0; i_layer < m->pi_layers[i_program]; i_layer++ )
         {
@@ -708,7 +721,7 @@ static int LatmReadStreamMuxConfiguration( latm_mux_t *m, bs_t *s )
 
             b_previous_cfg = false;
             if( i_program != 0 || i_layer != 0 )
-                b_previous_cfg = bs_read1( s );
+                b_previous_cfg = bsw_read1( s );
 
             if( b_previous_cfg )
             {
@@ -722,33 +735,33 @@ static int LatmReadStreamMuxConfiguration( latm_mux_t *m, bs_t *s )
                     i_cfg_size = LatmGetValue(s);
                 i_cfg_size -= Mpeg4ReadAudioSpecificInfo( &st->cfg, &st->i_extra, st->extra, s, i_cfg_size );
                 if( i_cfg_size > 0 )
-                    bs_skip( s, i_cfg_size );
+                    bsw_skip( s, i_cfg_size );
             }
 
-            st->i_frame_length_type = bs_read( s, 3 );
+            st->i_frame_length_type = bsw_read( s, 3 );
             switch( st->i_frame_length_type )
             {
             case 0:
             {
-                bs_skip( s, 8 ); /* latmBufferFullnes */
+                bsw_skip( s, 8 ); /* latmBufferFullnes */
                 if( !m->b_same_time_framing )
                 {
                     if( st->cfg.i_object_type == 6 || st->cfg.i_object_type == 20 ||
                         st->cfg.i_object_type == 8 || st->cfg.i_object_type == 24 )
                     {
-                        bs_skip( s, 6 ); /* eFrameOffset */
+                        bsw_skip( s, 6 ); /* eFrameOffset */
                     }
                 }
                 break;
             }
             case 1:
-                st->i_frame_length = bs_read( s, 9 );
+                st->i_frame_length = bsw_read( s, 9 );
                 break;
             case 3: case 4: case 5:
-                st->i_frame_length_index = bs_read( s, 6 ); // celp
+                st->i_frame_length_index = bsw_read( s, 6 ); // celp
                 break;
             case 6: case 7:
-                st->i_frame_length_index = bs_read( s, 1 ); // hvxc
+                st->i_frame_length_index = bsw_read( s, 1 ); // hvxc
             default:
                 break;
             }
@@ -758,7 +771,7 @@ static int LatmReadStreamMuxConfiguration( latm_mux_t *m, bs_t *s )
     }
 
     /* other data */
-    if( bs_read1( s ) )
+    if( bsw_read1( s ) )
     {
         if( i_mux_version == 1 )
         {
@@ -768,16 +781,16 @@ static int LatmReadStreamMuxConfiguration( latm_mux_t *m, bs_t *s )
         {
             int b_continue;
             do {
-                b_continue = bs_read1(s);
-                m->i_other_data = (m->i_other_data << 8) + bs_read( s, 8 );
+                b_continue = bsw_read1(s);
+                m->i_other_data = (m->i_other_data << 8) + bsw_read( s, 8 );
             } while( b_continue );
         }
     }
 
     /* crc */
     m->i_crc = -1;
-    if( bs_read1( s ) )
-        m->i_crc = bs_read( s, 8 );
+    if( bsw_read1( s ) )
+        m->i_crc = bsw_read( s, 8 );
 
     return 0;
 }
@@ -785,14 +798,14 @@ static int LatmReadStreamMuxConfiguration( latm_mux_t *m, bs_t *s )
 static int LOASParse( decoder_t *p_dec, uint8_t *p_buffer, int i_buffer )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
-    bs_t s;
+    bsw_t s;
     int i_sub;
     int i_accumulated = 0;
 
-    bs_init( &s, p_buffer, i_buffer );
+    bsw_init_writable( &s, p_buffer, i_buffer );
 
     /* Read the stream mux configuration if present */
-    if( !bs_read1( &s ) )
+    if( !bsw_read1( &s ) )
     {
         if( !LatmReadStreamMuxConfiguration( &p_sys->latm, &s ) &&
             p_sys->latm.i_streams > 0 )
@@ -845,7 +858,7 @@ static int LOASParse( decoder_t *p_dec, uint8_t *p_buffer, int i_buffer )
                         int i_payload = 0;
                         for( ;; )
                         {
-                            int i_tmp = bs_read( &s, 8 );
+                            int i_tmp = bsw_read( &s, 8 );
                             i_payload += i_tmp;
                             if( i_tmp != 255 )
                                 break;
@@ -860,7 +873,7 @@ static int LOASParse( decoder_t *p_dec, uint8_t *p_buffer, int i_buffer )
                              ( st->i_frame_length_type == 5 ) ||
                              ( st->i_frame_length_type == 7 ) )
                     {
-                        bs_skip( &s, 2 ); // muxSlotLengthCoded
+                        bsw_skip( &s, 2 ); // muxSlotLengthCoded
                         pi_payload[i_program][i_layer] = 0; /* TODO */
                     }
                     else
@@ -885,13 +898,13 @@ static int LOASParse( decoder_t *p_dec, uint8_t *p_buffer, int i_buffer )
 
                     /* FIXME that's slow (and a bit ugly to write in place) */
                     for( i = 0; i < pi_payload[i_program][i_layer]; i++ )
-                        p_buffer[i_accumulated++] = bs_read( &s, 8 );
+                        p_buffer[i_accumulated++] = bsw_read( &s, 8 );
                 }
             }
         }
         else
         {
-            const int i_chunks = bs_read( &s, 4 );
+            const int i_chunks = bsw_read( &s, 4 );
             int pi_program[16];
             int pi_layer[16];
             int i_chunk;
@@ -900,7 +913,7 @@ static int LOASParse( decoder_t *p_dec, uint8_t *p_buffer, int i_buffer )
 
             for( i_chunk = 0; i_chunk < i_chunks; i_chunk++ )
             {
-                const int streamIndex = bs_read( &s, 4 );
+                const int streamIndex = bsw_read( &s, 4 );
                 latm_stream_t *st = &p_sys->latm.stream[streamIndex];
                 const int i_program = st->i_program;
                 const int i_layer = st->i_layer;
@@ -913,13 +926,13 @@ static int LOASParse( decoder_t *p_dec, uint8_t *p_buffer, int i_buffer )
                     int i_payload = 0;
                     for( ;; )
                     {
-                        int i_tmp = bs_read( &s, 8 );
+                        int i_tmp = bsw_read( &s, 8 );
                         i_payload += i_tmp;
                         if( i_tmp != 255 )
                             break;
                     }
                     pi_payload[i_program][i_layer] = i_payload;
-                    bs_skip( &s, 1 ); // auEndFlag
+                    bsw_skip( &s, 1 ); // auEndFlag
                 }
                 else if( st->i_frame_length_type == 1 )
                 {
@@ -929,7 +942,7 @@ static int LOASParse( decoder_t *p_dec, uint8_t *p_buffer, int i_buffer )
                          ( st->i_frame_length_type == 5 ) ||
                          ( st->i_frame_length_type == 7 ) )
                 {
-                    bs_read( &s, 2 ); // muxSlotLengthCoded
+                    bsw_read( &s, 2 ); // muxSlotLengthCoded
                 }
                 else
                 {
@@ -949,7 +962,7 @@ static int LOASParse( decoder_t *p_dec, uint8_t *p_buffer, int i_buffer )
     {
         /* Other data XXX we just ignore them */
     }
-    bs_align( &s );
+    bsw_align( &s );
 
     return i_accumulated;
 }
@@ -972,14 +985,14 @@ static block_t *PacketizeStreamBlock( decoder_t *p_dec, block_t **pp_block )
         if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
         {
             p_sys->i_state = STATE_NOSYNC;
-            block_BytestreamFlush( &p_sys->bytestream );
+            block_BytestreamEmpty( &p_sys->bytestream );
         }
-        //aout_DateSet( &p_sys->end_date, 0 );
+        date_Set( &p_sys->end_date, 0 );
         block_Release( *pp_block );
         return NULL;
     }
 
-    if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
+    if( !date_Get( &p_sys->end_date ) && !(*pp_block)->i_pts )
     {
         /* We've just started the stream, wait for the first PTS. */
         block_Release( *pp_block );
@@ -1030,9 +1043,9 @@ static block_t *PacketizeStreamBlock( decoder_t *p_dec, block_t **pp_block )
             /* New frame, set the Presentation Time Stamp */
             p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
             if( p_sys->i_pts != 0 &&
-                p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
+                p_sys->i_pts != date_Get( &p_sys->end_date ) )
             {
-                aout_DateSet( &p_sys->end_date, p_sys->i_pts );
+                date_Set( &p_sys->end_date, p_sys->i_pts );
             }
             p_sys->i_state = STATE_HEADER;
             break;
@@ -1192,8 +1205,8 @@ static void SetupOutput( decoder_t *p_dec, block_t *p_block )
         msg_Info( p_dec, "AAC channels: %d samplerate: %d",
                   p_sys->i_channels, p_sys->i_rate );
 
-        aout_DateInit( &p_sys->end_date, p_sys->i_rate );
-        aout_DateSet( &p_sys->end_date, p_sys->i_pts );
+        date_Init( &p_sys->end_date, p_sys->i_rate, 1 );
+        date_Set( &p_sys->end_date, p_sys->i_pts );
     }
 
     p_dec->fmt_out.audio.i_rate     = p_sys->i_rate;
@@ -1207,10 +1220,10 @@ static void SetupOutput( decoder_t *p_dec, block_t *p_block )
         p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
 #endif
 
-    p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
+    p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
 
     p_block->i_length =
-        aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length ) - p_block->i_pts;
+        date_Increment( &p_sys->end_date, p_sys->i_frame_length ) - p_block->i_pts;
 }
 
 /*****************************************************************************