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 #include <stdlib.h>
00029 #include <string.h>
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
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 struct decoder_sys_t
00057 {
00058
00059
00060
00061 int i_state;
00062
00063 block_bytestream_t bytestream;
00064
00065
00066
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
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
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
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
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
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
00150 p_dec->fmt_out.i_cat = AUDIO_ES;
00151 p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','4','a');
00152
00153
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
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
00206
00207
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;
00218
00219 if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
00220 {
00221
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
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
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
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
00287 return NULL;
00288 }
00289
00290 case STATE_SYNC:
00291
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
00303 if( block_PeekBytes( &p_sys->bytestream, p_header,
00304 ADTS_HEADER_SIZE ) != VLC_SUCCESS )
00305 {
00306
00307 return NULL;
00308 }
00309
00310
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
00329
00330
00331
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
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
00354
00355 if( block_WaitBytes( &p_sys->bytestream, p_sys->i_frame_size +
00356 p_sys->i_header_size) != VLC_SUCCESS )
00357 {
00358
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
00367 return NULL;
00368 }
00369
00370
00371
00372
00373
00374 block_SkipBytes( &p_sys->bytestream, p_sys->i_header_size );
00375
00376
00377 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
00378
00379
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
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
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
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
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
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
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
00480 *pi_frame_length = 1024;
00481
00482
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
00494 *pi_header_size = b_crc ? 9 : 7;
00495
00496 return i_frame_size - *pi_header_size;
00497 }