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

lpcm.c

00001 /*****************************************************************************
00002  * lpcm.c: lpcm decoder/packetizer module
00003  *****************************************************************************
00004  * Copyright (C) 1999-2005 the VideoLAN team
00005  * $Id: lpcm.c 11664 2005-07-09 06:17:09Z courmisch $
00006  *
00007  * Authors: Samuel Hocevar <[email protected]>
00008  *          Henri Fallon <[email protected]>
00009  *          Christophe Massiot <[email protected]>
00010  *          Gildas Bazin <[email protected]>
00011  *
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program; if not, write to the Free Software
00024  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00025  *****************************************************************************/
00026 
00027 /*****************************************************************************
00028  * Preamble
00029  *****************************************************************************/
00030 #include <vlc/vlc.h>
00031 #include <vlc/decoder.h>
00032 
00033 /*****************************************************************************
00034  * decoder_sys_t : lpcm decoder descriptor
00035  *****************************************************************************/
00036 struct decoder_sys_t
00037 {
00038     /* Module mode */
00039     vlc_bool_t b_packetizer;
00040 
00041     /*
00042      * Output properties
00043      */
00044     audio_date_t end_date;
00045 
00046 };
00047 
00048 /*
00049  * LPCM header :
00050  * - PES header
00051  * - private stream ID (16 bits) == 0xA0 -> not in the bitstream
00052  *
00053  * - frame number (8 bits)
00054  * - unknown (16 bits) == 0x0003 ?
00055  * - unknown (4 bits)
00056  * - current frame (4 bits)
00057  * - unknown (2 bits)
00058  * - frequency (2 bits) 0 == 48 kHz, 1 == 32 kHz, 2 == ?, 3 == ?
00059  * - unknown (1 bit)
00060  * - number of channels - 1 (3 bits) 1 == 2 channels
00061  * - start code (8 bits) == 0x80
00062  */
00063 
00064 #define LPCM_HEADER_LEN 6
00065 
00066 /*****************************************************************************
00067  * Local prototypes
00068  *****************************************************************************/
00069 static int  OpenDecoder   ( vlc_object_t * );
00070 static int  OpenPacketizer( vlc_object_t * );
00071 static void CloseDecoder  ( vlc_object_t * );
00072 
00073 static void *DecodeFrame  ( decoder_t *, block_t ** );
00074 
00075 /*****************************************************************************
00076  * Module descriptor
00077  *****************************************************************************/
00078 vlc_module_begin();
00079 
00080     set_category( CAT_INPUT );
00081     set_subcategory( SUBCAT_INPUT_ACODEC );
00082     set_description( _("Linear PCM audio decoder") );
00083     set_capability( "decoder", 100 );
00084     set_callbacks( OpenDecoder, CloseDecoder );
00085 
00086     add_submodule();
00087     set_description( _("Linear PCM audio packetizer") );
00088     set_capability( "packetizer", 100 );
00089     set_callbacks( OpenPacketizer, CloseDecoder );
00090 
00091 vlc_module_end();
00092 
00093 /*****************************************************************************
00094  * OpenDecoder: probe the decoder and return score
00095  *****************************************************************************/
00096 static int OpenDecoder( vlc_object_t *p_this )
00097 {
00098     decoder_t *p_dec = (decoder_t*)p_this;
00099     decoder_sys_t *p_sys;
00100 
00101     if( p_dec->fmt_in.i_codec != VLC_FOURCC('l','p','c','m')
00102          && p_dec->fmt_in.i_codec != VLC_FOURCC('l','p','c','b') )
00103     {   
00104         return VLC_EGENERIC;
00105     }
00106 
00107     /* Allocate the memory needed to store the decoder's structure */
00108     if( ( p_dec->p_sys = p_sys =
00109           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
00110     {
00111         msg_Err( p_dec, "out of memory" );
00112         return VLC_EGENERIC;
00113     }
00114 
00115     /* Misc init */
00116     p_sys->b_packetizer = VLC_FALSE;
00117     aout_DateSet( &p_sys->end_date, 0 );
00118 
00119     /* Set output properties */
00120     p_dec->fmt_out.i_cat = AUDIO_ES;
00121 
00122     if( p_dec->fmt_out.audio.i_bitspersample == 24 )
00123     {
00124         p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','b');
00125     }
00126     else
00127     {
00128         p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','b');
00129         p_dec->fmt_out.audio.i_bitspersample = 16;
00130     }
00131 
00132     /* Set callback */
00133     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
00134         DecodeFrame;
00135     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
00136         DecodeFrame;
00137 
00138     return VLC_SUCCESS;
00139 }
00140 
00141 static int OpenPacketizer( vlc_object_t *p_this )
00142 {
00143     decoder_t *p_dec = (decoder_t*)p_this;
00144 
00145     int i_ret = OpenDecoder( p_this );
00146 
00147     if( i_ret != VLC_SUCCESS ) return i_ret;
00148 
00149     p_dec->p_sys->b_packetizer = VLC_TRUE;
00150 
00151     p_dec->fmt_out.i_codec = VLC_FOURCC('l','p','c','m');
00152 
00153     return i_ret;
00154 }
00155 
00156 /*****************************************************************************
00157  * DecodeFrame: decodes an lpcm frame.
00158  ****************************************************************************
00159  * Beware, this function must be fed with complete frames (PES packet).
00160  *****************************************************************************/
00161 static void *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
00162 {
00163     decoder_sys_t *p_sys = p_dec->p_sys;
00164     block_t       *p_block;
00165     unsigned int  i_rate = 0, i_original_channels = 0, i_channels = 0;
00166     int           i_frame_length, i_bitspersample;
00167     uint8_t       i_header;
00168 
00169     if( !pp_block || !*pp_block ) return NULL;
00170 
00171     p_block = *pp_block;
00172     *pp_block = NULL; /* So the packet doesn't get re-sent */
00173 
00174     /* Date management */
00175     if( p_block->i_pts > 0 &&
00176         p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
00177     {
00178         aout_DateSet( &p_sys->end_date, p_block->i_pts );
00179     }
00180 
00181     if( !aout_DateGet( &p_sys->end_date ) )
00182     {
00183         /* We've just started the stream, wait for the first PTS. */
00184         block_Release( p_block );
00185         return NULL;
00186     }
00187 
00188     if( p_block->i_buffer <= LPCM_HEADER_LEN )
00189     {
00190         msg_Err(p_dec, "frame is too short");
00191         block_Release( p_block );
00192         return NULL;
00193     }
00194 
00195     i_header = p_block->p_buffer[4];
00196     switch ( (i_header >> 4) & 0x3 )
00197     {
00198     case 0:
00199         i_rate = 48000;
00200         break;
00201     case 1:
00202         i_rate = 96000;
00203         break;
00204     case 2:
00205         i_rate = 44100;
00206         break;
00207     case 3:
00208         i_rate = 32000;
00209         break;
00210     }
00211 
00212     i_channels = (i_header & 0x7);
00213     switch ( i_channels )
00214     {
00215     case 0:
00216         i_original_channels = AOUT_CHAN_CENTER;
00217         break;
00218     case 1:
00219         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
00220         break;
00221     case 2:
00222         /* This is unsure. */
00223         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
00224         break;
00225     case 3:
00226         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
00227                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
00228         break;
00229     case 4:
00230         /* This is unsure. */
00231         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
00232                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
00233                                | AOUT_CHAN_LFE;
00234         break;
00235     case 5:
00236         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
00237                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
00238                                | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
00239         break;
00240     case 6:
00241         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
00242                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
00243                                | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
00244                                | AOUT_CHAN_MIDDLERIGHT;
00245         break;
00246     case 7:
00247         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
00248                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
00249                                | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
00250                                | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
00251         break;
00252     }
00253 
00254     switch ( (i_header >> 6) & 0x3 )
00255     {
00256     case 2:
00257         p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','b');
00258         p_dec->fmt_out.audio.i_bitspersample = 24;
00259         i_bitspersample = 24;
00260         break;
00261     case 1:
00262         p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','b');
00263         p_dec->fmt_out.audio.i_bitspersample = 24;
00264         i_bitspersample = 20;
00265         break;
00266     case 0:
00267     default:
00268         p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','b');
00269         p_dec->fmt_out.audio.i_bitspersample = 16;
00270         i_bitspersample = 16;
00271         break;
00272     }
00273 
00274     /* Check frame sync and drop it. */
00275     if( p_block->p_buffer[5] != 0x80 )
00276     {
00277         msg_Warn( p_dec, "no frame sync" );
00278         block_Release( p_block );
00279         return NULL;
00280     }
00281 
00282     /* Set output properties */
00283     if( p_dec->fmt_out.audio.i_rate != i_rate )
00284     {
00285         aout_DateInit( &p_sys->end_date, i_rate );
00286         aout_DateSet( &p_sys->end_date, p_block->i_pts );
00287     }
00288     p_dec->fmt_out.audio.i_rate = i_rate;
00289     p_dec->fmt_out.audio.i_channels = i_channels + 1;
00290     p_dec->fmt_out.audio.i_original_channels = i_original_channels;
00291     p_dec->fmt_out.audio.i_physical_channels
00292         = i_original_channels & AOUT_CHAN_PHYSMASK;
00293 
00294     i_frame_length = (p_block->i_buffer - LPCM_HEADER_LEN) /
00295         p_dec->fmt_out.audio.i_channels * 8 / i_bitspersample;
00296 
00297     if( p_sys->b_packetizer )
00298     {
00299         p_dec->fmt_out.i_codec = VLC_FOURCC('l','p','c','m');
00300         p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
00301         p_block->i_length =
00302             aout_DateIncrement( &p_sys->end_date, i_frame_length ) -
00303             p_block->i_pts;
00304 
00305         /* Just pass on the incoming frame */
00306         return p_block;
00307     }
00308     else
00309     {
00310         aout_buffer_t *p_aout_buffer;
00311         p_aout_buffer = p_dec->pf_aout_buffer_new( p_dec, i_frame_length );
00312         if( p_aout_buffer == NULL ) return NULL;
00313 
00314         p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
00315         p_aout_buffer->end_date =
00316             aout_DateIncrement( &p_sys->end_date, i_frame_length );
00317 
00318         p_block->p_buffer += LPCM_HEADER_LEN;
00319         p_block->i_buffer -= LPCM_HEADER_LEN;
00320 
00321         /* 20/24 bits LPCM use special packing */
00322         if( i_bitspersample == 24 )
00323         {
00324             uint8_t *p_out = p_aout_buffer->p_buffer;
00325 
00326             while( p_block->i_buffer / 12 )
00327             {
00328                 /* Sample 1 */
00329                 p_out[0] = p_block->p_buffer[0];
00330                 p_out[1] = p_block->p_buffer[1];
00331                 p_out[2] = p_block->p_buffer[8];
00332                 /* Sample 2 */
00333                 p_out[3] = p_block->p_buffer[2];
00334                 p_out[4] = p_block->p_buffer[3];
00335                 p_out[5] = p_block->p_buffer[9];
00336                 /* Sample 3 */
00337                 p_out[6] = p_block->p_buffer[4];
00338                 p_out[7] = p_block->p_buffer[5];
00339                 p_out[8] = p_block->p_buffer[10];
00340                 /* Sample 4 */
00341                 p_out[9] = p_block->p_buffer[6];
00342                 p_out[10] = p_block->p_buffer[7];
00343                 p_out[11] = p_block->p_buffer[11];
00344 
00345                 p_block->i_buffer -= 12;
00346                 p_block->p_buffer += 12;
00347                 p_out += 12;
00348             }
00349         }
00350         else if( i_bitspersample == 20 )
00351         {
00352             uint8_t *p_out = p_aout_buffer->p_buffer;
00353 
00354             while( p_block->i_buffer / 10 )
00355             {
00356                 /* Sample 1 */
00357                 p_out[0] = p_block->p_buffer[0];
00358                 p_out[1] = p_block->p_buffer[1];
00359                 p_out[2] = p_block->p_buffer[8] & 0xF0;
00360                 /* Sample 2 */
00361                 p_out[3] = p_block->p_buffer[2];
00362                 p_out[4] = p_block->p_buffer[3];
00363                 p_out[5] = p_block->p_buffer[8] << 4;
00364                 /* Sample 3 */
00365                 p_out[6] = p_block->p_buffer[4];
00366                 p_out[7] = p_block->p_buffer[5];
00367                 p_out[8] = p_block->p_buffer[9] & 0xF0;
00368                 /* Sample 4 */
00369                 p_out[9] = p_block->p_buffer[6];
00370                 p_out[10] = p_block->p_buffer[7];
00371                 p_out[11] = p_block->p_buffer[9] << 4;
00372 
00373                 p_block->i_buffer -= 10;
00374                 p_block->p_buffer += 10;
00375                 p_out += 12;
00376             }
00377         }
00378         else
00379         {
00380             memcpy( p_aout_buffer->p_buffer,
00381                     p_block->p_buffer, p_block->i_buffer );
00382         }
00383 
00384         block_Release( p_block );
00385         return p_aout_buffer;
00386     }
00387 }
00388 
00389 /*****************************************************************************
00390  * CloseDecoder : lpcm decoder destruction
00391  *****************************************************************************/
00392 static void CloseDecoder( vlc_object_t *p_this )
00393 {
00394     decoder_t *p_dec = (decoder_t*)p_this;
00395     free( p_dec->p_sys );
00396 }

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