00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <vlc/vlc.h>
00031 #include <vlc/decoder.h>
00032
00033
00034
00035
00036 struct decoder_sys_t
00037 {
00038
00039 vlc_bool_t b_packetizer;
00040
00041
00042
00043
00044 audio_date_t end_date;
00045
00046 };
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 #define LPCM_HEADER_LEN 6
00065
00066
00067
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
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
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
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
00116 p_sys->b_packetizer = VLC_FALSE;
00117 aout_DateSet( &p_sys->end_date, 0 );
00118
00119
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
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
00158
00159
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;
00173
00174
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 }