]> git.sesse.net Git - vlc/blobdiff - modules/codec/lpcm.c
Use _WIN32 rather than WIN32 (same for WIN64)
[vlc] / modules / codec / lpcm.c
index cfba3fd5e20784b5bc4b26daf5f07c5639ab066b..402cd738fb64304a2feb7ee0b47872f85ea71213 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * lpcm.c: lpcm decoder/packetizer module
  *****************************************************************************
- * Copyright (C) 1999-2008 the VideoLAN team
+ * Copyright (C) 1999-2008 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Samuel Hocevar <sam@zoy.org>
  *          Lauren Aimar <fenrir _AT_ videolan _DOT_ org >
  *          Steinar H. Gunderson <steinar+vlc@gunderson.no>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -37,6 +37,7 @@
 #include <vlc_plugin.h>
 #include <vlc_codec.h>
 #include <vlc_aout.h>
+#include <unistd.h>
 #include <assert.h>
 
 /*****************************************************************************
@@ -49,7 +50,7 @@ static void CloseCommon   ( vlc_object_t * );
 #ifdef ENABLE_SOUT
 static int  OpenEncoder   ( vlc_object_t * );
 static void CloseEncoder  ( vlc_object_t * );
-static block_t *EncodeFrames( encoder_t *, aout_buffer_t * );
+static block_t *EncodeFrames( encoder_t *, block_t * );
 #endif
 
 vlc_module_begin ()
@@ -134,7 +135,7 @@ struct encoder_sys_t
  * - padding(variable)
  *
  * LPCM BD header :
- * - unkown (16 bits)
+ * - unknown (16 bits)
  * - number of channels (4 bits)
  * - frequency (4 bits)
  * - bits per sample (2 bits)
@@ -162,14 +163,14 @@ typedef struct
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-static void *DecodeFrame  ( decoder_t *, block_t ** );
+static block_t *DecodeFrame  ( decoder_t *, block_t ** );
 
 /* */
 static int VobHeader( unsigned *pi_rate,
                       unsigned *pi_channels, unsigned *pi_original_channels,
                       unsigned *pi_bits,
                       const uint8_t *p_header );
-static void VobExtract( aout_buffer_t *, block_t *, unsigned i_bits );
+static void VobExtract( block_t *, block_t *, unsigned i_bits );
 /* */
 static int AobHeader( unsigned *pi_rate,
                       unsigned *pi_channels, unsigned *pi_layout,
@@ -177,7 +178,7 @@ static int AobHeader( unsigned *pi_rate,
                       unsigned *pi_padding,
                       aob_group_t g[2],
                       const uint8_t *p_header );
-static void AobExtract( aout_buffer_t *, block_t *, unsigned i_bits, aob_group_t p_group[2] );
+static void AobExtract( block_t *, block_t *, unsigned i_bits, aob_group_t p_group[2] );
 /* */
 static int BdHeader( unsigned *pi_rate,
                      unsigned *pi_channels,
@@ -185,7 +186,7 @@ static int BdHeader( unsigned *pi_rate,
                      unsigned *pi_original_channels,
                      unsigned *pi_bits,
                      const uint8_t *p_header );
-static void BdExtract( aout_buffer_t *, block_t *, unsigned, unsigned, unsigned, unsigned );
+static void BdExtract( block_t *, block_t *, unsigned, unsigned, unsigned, unsigned );
 
 
 /*****************************************************************************
@@ -255,21 +256,19 @@ static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
         {
         case 24:
         case 20:
-            p_dec->fmt_out.i_codec = VLC_CODEC_S24B;
-            p_dec->fmt_out.audio.i_bitspersample = 24;
+            p_dec->fmt_out.i_codec = VLC_CODEC_S32N;
+            p_dec->fmt_out.audio.i_bitspersample = 32;
             break;
         default:
-            p_dec->fmt_out.i_codec = VLC_CODEC_S16B;
+            p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
             p_dec->fmt_out.audio.i_bitspersample = 16;
             break;
         }
     }
 
     /* Set callback */
-    p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
-        DecodeFrame;
-    p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
-        DecodeFrame;
+    p_dec->pf_decode_audio = DecodeFrame;
+    p_dec->pf_packetize    = DecodeFrame;
 
     return VLC_SUCCESS;
 }
@@ -287,7 +286,7 @@ static int OpenPacketizer( vlc_object_t *p_this )
  ****************************************************************************
  * Beware, this function must be fed with complete frames (PES packet).
  *****************************************************************************/
-static void *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
+static block_t *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
     block_t       *p_block;
@@ -359,7 +358,7 @@ static void *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
     p_dec->fmt_out.audio.i_rate = i_rate;
     p_dec->fmt_out.audio.i_channels = i_channels;
     p_dec->fmt_out.audio.i_original_channels = i_original_channels;
-    p_dec->fmt_out.audio.i_physical_channels = i_original_channels & AOUT_CHAN_PHYSMASK;
+    p_dec->fmt_out.audio.i_physical_channels = i_original_channels;
 
     i_frame_length = (p_block->i_buffer - p_sys->i_header_size - i_padding) /
                      (i_channels + i_channels_padding) * 8 / i_bits;
@@ -379,17 +378,17 @@ static void *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
         /* */
         if( i_bits == 16 )
         {
-            p_dec->fmt_out.i_codec = VLC_CODEC_S16B;
+            p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
             p_dec->fmt_out.audio.i_bitspersample = 16;
         }
         else
         {
-            p_dec->fmt_out.i_codec = VLC_CODEC_S24B;
-            p_dec->fmt_out.audio.i_bitspersample = 24;
+            p_dec->fmt_out.i_codec = VLC_CODEC_S32N;
+            p_dec->fmt_out.audio.i_bitspersample = 32;
         }
 
         /* */
-        aout_buffer_t *p_aout_buffer;
+        block_t *p_aout_buffer;
         p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_frame_length );
         if( !p_aout_buffer )
             return NULL;
@@ -466,10 +465,8 @@ static int OpenEncoder( vlc_object_t *p_this )
 
     /* In DVD LCPM, a frame is always 150 PTS ticks. */
     p_sys->i_frame_samples = p_enc->fmt_in.audio.i_rate * 150 / 90000;
-    p_sys->p_buffer = (uint8_t *)malloc(
-        p_sys->i_frame_samples *
-        p_enc->fmt_in.audio.i_channels *
-        p_enc->fmt_in.audio.i_bitspersample);
+    p_sys->p_buffer = xmalloc(p_sys->i_frame_samples
+                            * p_enc->fmt_in.audio.i_channels * 16);
     p_sys->i_buffer_used = 0;
     p_sys->i_frame_num = 0;
 
@@ -480,7 +477,7 @@ static int OpenEncoder( vlc_object_t *p_this )
     p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
 
     p_enc->fmt_in.audio.i_bitspersample = 16;
-    p_enc->fmt_in.i_codec = VLC_CODEC_S16B;
+    p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
 
     p_enc->fmt_out.i_bitrate =
         p_enc->fmt_in.audio.i_channels *
@@ -507,7 +504,7 @@ static void CloseEncoder ( vlc_object_t *p_this )
 /*****************************************************************************
  * EncodeFrames: encode zero or more LCPM audio packets
  *****************************************************************************/
-static block_t *EncodeFrames( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
+static block_t *EncodeFrames( encoder_t *p_enc, block_t *p_aout_buf )
 {
     encoder_sys_t *p_sys = p_enc->p_sys;
     block_t *p_first_block = NULL, *p_last_block = NULL;
@@ -544,7 +541,7 @@ static block_t *EncodeFrames( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
 
     for ( int i = 0; i < i_num_frames; ++i )
     {
-        block_t *p_block = block_New( p_enc, i_frame_size );
+        block_t *p_block = block_Alloc( i_frame_size );
         if( !p_block )
             return NULL;
 
@@ -560,8 +557,15 @@ static block_t *EncodeFrames( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
         const int i_kept_bytes = p_sys->i_buffer_used * p_sys->i_channels * 2;
         const int i_consume_bytes = i_consume_samples * p_sys->i_channels * 2;
 
+#ifdef WORDS_BIGENDIAN
         memcpy( frame + 6, p_sys->p_buffer, i_kept_bytes );
-        memcpy( frame + 6 + i_kept_bytes, p_aout_buf->p_buffer + i_bytes_consumed, i_consume_bytes );
+        memcpy( frame + 6 + i_kept_bytes, p_aout_buf->p_buffer + i_bytes_consumed,
+                i_consume_bytes );
+#else
+        swab( p_sys->p_buffer, frame + 6, i_kept_bytes );
+        swab( p_aout_buf->p_buffer + i_bytes_consumed, frame + 6 + i_kept_bytes,
+              i_consume_bytes );
+#endif
 
         p_sys->i_frame_num++;
         p_sys->i_buffer_used = 0;
@@ -913,71 +917,75 @@ static int BdHeader( unsigned *pi_rate,
     return 0;
 }
 
-static void VobExtract( aout_buffer_t *p_aout_buffer, block_t *p_block,
+static void VobExtract( block_t *p_aout_buffer, block_t *p_block,
                         unsigned i_bits )
 {
-    uint8_t *p_out = p_aout_buffer->p_buffer;
-
     /* 20/24 bits LPCM use special packing */
     if( i_bits == 24 )
     {
+        uint32_t *p_out = (uint32_t *)p_aout_buffer->p_buffer;
+
         while( p_block->i_buffer / 12 )
         {
             /* Sample 1 */
-            p_out[0] = p_block->p_buffer[0];
-            p_out[1] = p_block->p_buffer[1];
-            p_out[2] = p_block->p_buffer[8];
+            *(p_out++) = (p_block->p_buffer[ 0] << 24)
+                       | (p_block->p_buffer[ 1] << 16)
+                       | (p_block->p_buffer[ 8] <<  8);
             /* Sample 2 */
-            p_out[3] = p_block->p_buffer[2];
-            p_out[4] = p_block->p_buffer[3];
-            p_out[5] = p_block->p_buffer[9];
+            *(p_out++) = (p_block->p_buffer[ 2] << 24)
+                       | (p_block->p_buffer[ 3] << 16)
+                       | (p_block->p_buffer[ 9] <<  8);
             /* Sample 3 */
-            p_out[6] = p_block->p_buffer[4];
-            p_out[7] = p_block->p_buffer[5];
-            p_out[8] = p_block->p_buffer[10];
+            *(p_out++) = (p_block->p_buffer[ 4] << 24)
+                       | (p_block->p_buffer[ 5] << 16)
+                       | (p_block->p_buffer[10] <<  8);
             /* Sample 4 */
-            p_out[9] = p_block->p_buffer[6];
-            p_out[10] = p_block->p_buffer[7];
-            p_out[11] = p_block->p_buffer[11];
+            *(p_out++) = (p_block->p_buffer[ 6] << 24)
+                       | (p_block->p_buffer[ 7] << 16)
+                       | (p_block->p_buffer[11] <<  8);
 
             p_block->i_buffer -= 12;
             p_block->p_buffer += 12;
-            p_out += 12;
         }
     }
     else if( i_bits == 20 )
     {
+        uint32_t *p_out = (uint32_t *)p_aout_buffer->p_buffer;
+
         while( p_block->i_buffer / 10 )
         {
             /* Sample 1 */
-            p_out[0] = p_block->p_buffer[0];
-            p_out[1] = p_block->p_buffer[1];
-            p_out[2] = p_block->p_buffer[8] & 0xF0;
+            *(p_out++) = ( p_block->p_buffer[0]         << 24)
+                       | ( p_block->p_buffer[1]         << 16)
+                       | ((p_block->p_buffer[8] & 0xF0) <<  8);
             /* Sample 2 */
-            p_out[3] = p_block->p_buffer[2];
-            p_out[4] = p_block->p_buffer[3];
-            p_out[5] = p_block->p_buffer[8] << 4;
+            *(p_out++) = ( p_block->p_buffer[2]         << 24)
+                       | ( p_block->p_buffer[3]         << 16)
+                       | ((p_block->p_buffer[8] & 0x0F) << 12);
             /* Sample 3 */
-            p_out[6] = p_block->p_buffer[4];
-            p_out[7] = p_block->p_buffer[5];
-            p_out[8] = p_block->p_buffer[9] & 0xF0;
+            *(p_out++) = ( p_block->p_buffer[4]         << 24)
+                       | ( p_block->p_buffer[5]         << 16)
+                       | ((p_block->p_buffer[9] & 0xF0) <<  8);
             /* Sample 4 */
-            p_out[9] = p_block->p_buffer[6];
-            p_out[10] = p_block->p_buffer[7];
-            p_out[11] = p_block->p_buffer[9] << 4;
+            *(p_out++) = ( p_block->p_buffer[6]         << 24)
+                       | ( p_block->p_buffer[7]         << 16)
+                       | ((p_block->p_buffer[9] & 0x0F) << 12);
 
             p_block->i_buffer -= 10;
             p_block->p_buffer += 10;
-            p_out += 12;
         }
     }
     else
     {
         assert( i_bits == 16 );
-        memcpy( p_out, p_block->p_buffer, p_block->i_buffer );
+#ifdef WORDS_BIGENDIAN
+        memcpy( p_aout_buffer->p_buffer, p_block->p_buffer, p_block->i_buffer );
+#else
+        swab( p_block->p_buffer, p_aout_buffer->p_buffer, p_block->i_buffer );
+#endif
     }
 }
-static void AobExtract( aout_buffer_t *p_aout_buffer,
+static void AobExtract( block_t *p_aout_buffer,
                         block_t *p_block, unsigned i_bits, aob_group_t p_group[2] )
 {
     const unsigned i_channels = p_group[0].i_channels +
@@ -1035,7 +1043,7 @@ static void AobExtract( aout_buffer_t *p_aout_buffer,
 
     }
 }
-static void BdExtract( aout_buffer_t *p_aout_buffer, block_t *p_block,
+static void BdExtract( block_t *p_aout_buffer, block_t *p_block,
                        unsigned i_frame_length,
                        unsigned i_channels, unsigned i_channels_padding,
                        unsigned i_bits )
@@ -1046,7 +1054,11 @@ static void BdExtract( aout_buffer_t *p_aout_buffer, block_t *p_block,
         uint8_t *p_dst = p_aout_buffer->p_buffer;
         while( i_frame_length > 0 )
         {
+#ifdef WORDS_BIGENDIAN
             memcpy( p_dst, p_src, i_channels * i_bits / 8 );
+#else
+            swab( p_dst, p_src, i_channels * i_bits / 8 );
+#endif
             p_src += (i_channels + i_channels_padding) * i_bits / 8;
             p_dst += (i_channels +                  0) * i_bits / 8;
             i_frame_length--;
@@ -1054,7 +1066,11 @@ static void BdExtract( aout_buffer_t *p_aout_buffer, block_t *p_block,
     }
     else
     {
+#ifdef WORDS_BIGENDIAN
         memcpy( p_aout_buffer->p_buffer, p_block->p_buffer, p_block->i_buffer );
+#else
+        swab( p_block->p_buffer, p_aout_buffer->p_buffer, p_block->i_buffer );
+#endif
     }
 }