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 #include <stdlib.h>
00028
00029 #include <vlc/vlc.h>
00030 #include <vlc/input.h>
00031
00032
00033
00034
00035 static int Open ( vlc_object_t * );
00036 static void Close( vlc_object_t * );
00037
00038 vlc_module_begin();
00039 set_description( _("raw DV demuxer") );
00040 set_capability( "demux2", 2 );
00041 set_category( CAT_INPUT );
00042 set_subcategory( SUBCAT_INPUT_DEMUX );
00043 set_callbacks( Open, Close );
00044 add_shortcut( "rawdv" );
00045 vlc_module_end();
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 #define DV_PAL_FRAME_SIZE 144000
00067 #define DV_NTSC_FRAME_SIZE 122000
00068
00069
00070
00071
00072 typedef struct {
00073 int8_t sct;
00074 int8_t dsn;
00075 int fsc;
00076 int8_t dbn;
00077 } dv_id_t;
00078
00079 typedef struct {
00080 int dsf;
00081 int8_t apt;
00082 int tf1;
00083 int8_t ap1;
00084 int tf2;
00085 int8_t ap2;
00086 int tf3;
00087 int8_t ap3;
00088 } dv_header_t;
00089
00090 struct demux_sys_t
00091 {
00092 int frame_size;
00093
00094 es_out_id_t *p_es_video;
00095 es_format_t fmt_video;
00096
00097 es_out_id_t *p_es_audio;
00098 es_format_t fmt_audio;
00099
00100 int i_dsf;
00101 double f_rate;
00102 int i_bitrate;
00103
00104
00105 mtime_t i_pcr;
00106 };
00107
00108
00109
00110
00111 static int Demux( demux_t * );
00112 static int Control( demux_t *, int i_query, va_list args );
00113
00114 static block_t *dv_extract_audio( demux_t *p_demux,
00115 block_t* p_frame_block );
00116
00117
00118
00119
00120 static int Open( vlc_object_t * p_this )
00121 {
00122 demux_t *p_demux = (demux_t*)p_this;
00123 demux_sys_t *p_sys;
00124
00125 byte_t *p_peek, *p_peek_backup;
00126
00127 uint32_t i_dword;
00128 dv_header_t dv_header;
00129 dv_id_t dv_id;
00130 char *psz_ext;
00131
00132
00133
00134
00135
00136
00137
00138 psz_ext = strrchr( p_demux->psz_path, '.' );
00139 if( ( !psz_ext || strcasecmp( psz_ext, ".dv") ) &&
00140 strcmp(p_demux->psz_demux, "rawdv") )
00141 {
00142 return VLC_EGENERIC;
00143 }
00144
00145 if( stream_Peek( p_demux->s, &p_peek, DV_PAL_FRAME_SIZE ) <
00146 DV_NTSC_FRAME_SIZE )
00147 {
00148
00149 msg_Err( p_demux, "cannot peek()" );
00150 return VLC_EGENERIC;
00151 }
00152 p_peek_backup = p_peek;
00153
00154
00155 i_dword = GetDWBE( p_peek ); p_peek += 4;
00156 dv_id.sct = i_dword >> (32 - 3);
00157 i_dword <<= 8;
00158 dv_id.dsn = i_dword >> (32 - 4);
00159 i_dword <<= 4;
00160 dv_id.fsc = i_dword >> (32 - 1);
00161 i_dword <<= 4;
00162 dv_id.dbn = i_dword >> (32 - 8);
00163 i_dword <<= 8;
00164
00165 if( dv_id.sct != 0 )
00166 {
00167 msg_Warn( p_demux, "not a raw DV stream header" );
00168 return VLC_EGENERIC;
00169 }
00170
00171
00172 dv_header.dsf = i_dword >> (32 - 1);
00173 i_dword <<= 1;
00174 if( i_dword >> (32 - 1) )
00175 {
00176 msg_Warn( p_demux, "incorrect bit" );
00177 return VLC_EGENERIC;
00178 }
00179
00180 i_dword = GetDWBE( p_peek ); p_peek += 4;
00181 i_dword <<= 5;
00182 dv_header.apt = i_dword >> (32 - 3);
00183 i_dword <<= 3;
00184 dv_header.tf1 = i_dword >> (32 - 1);
00185 i_dword <<= 5;
00186 dv_header.ap1 = i_dword >> (32 - 3);
00187 i_dword <<= 3;
00188 dv_header.tf2 = i_dword >> (32 - 1);
00189 i_dword <<= 5;
00190 dv_header.ap2 = i_dword >> (32 - 3);
00191 i_dword <<= 3;
00192 dv_header.tf3 = i_dword >> (32 - 1);
00193 i_dword <<= 5;
00194 dv_header.ap3 = i_dword >> (32 - 3);
00195
00196 p_peek += 72;
00197
00198
00199
00200 p_demux->pf_demux = Demux;
00201 p_demux->pf_control = Control;
00202 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
00203
00204 p_sys->i_dsf = dv_header.dsf;
00205 p_sys->frame_size = dv_header.dsf ? 12 * 150 * 80 : 10 * 150 * 80;
00206 p_sys->f_rate = dv_header.dsf ? 25 : 29.97;
00207
00208 p_sys->i_pcr = 1;
00209 p_sys->p_es_video = NULL;
00210 p_sys->p_es_audio = NULL;
00211
00212 p_sys->i_bitrate = 0;
00213
00214 es_format_Init( &p_sys->fmt_video, VIDEO_ES, VLC_FOURCC('d','v','s','d') );
00215 p_sys->fmt_video.video.i_width = 720;
00216 p_sys->fmt_video.video.i_height= dv_header.dsf ? 576 : 480;;
00217
00218
00219 #if 0
00220
00221
00222 p_input->i_bufsize = p_sys->frame_size;
00223 #endif
00224
00225 p_sys->p_es_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
00226
00227
00228 p_peek = p_peek_backup + 80*6+80*16*3 + 3;
00229 if( *p_peek == 0x50 )
00230 {
00231 es_format_Init( &p_sys->fmt_audio, AUDIO_ES,
00232 VLC_FOURCC('a','r','a','w') );
00233
00234 p_sys->fmt_audio.audio.i_channels = 2;
00235 switch( (p_peek[4] >> 3) & 0x07 )
00236 {
00237 case 0:
00238 p_sys->fmt_audio.audio.i_rate = 48000;
00239 break;
00240 case 1:
00241 p_sys->fmt_audio.audio.i_rate = 44100;
00242 break;
00243 case 2:
00244 default:
00245 p_sys->fmt_audio.audio.i_rate = 32000;
00246 break;
00247 }
00248
00249
00250 p_sys->fmt_audio.audio.i_bitspersample = 16;
00251
00252 p_sys->p_es_audio = es_out_Add( p_demux->out, &p_sys->fmt_audio );
00253 }
00254
00255 return VLC_SUCCESS;
00256 }
00257
00258
00259
00260
00261 static void Close( vlc_object_t *p_this )
00262 {
00263 demux_t *p_demux = (demux_t*)p_this;
00264 demux_sys_t *p_sys = p_demux->p_sys;
00265
00266 free( p_sys );
00267 }
00268
00269
00270
00271
00272
00273
00274 static int Demux( demux_t *p_demux )
00275 {
00276 demux_sys_t *p_sys = p_demux->p_sys;
00277 block_t *p_block;
00278 vlc_bool_t b_audio = VLC_FALSE;
00279
00280
00281 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
00282
00283 if( ( p_block = stream_Block( p_demux->s, p_sys->frame_size ) ) == NULL )
00284 {
00285
00286 return 0;
00287 }
00288
00289 if( p_sys->p_es_audio )
00290 {
00291 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
00292 p_sys->p_es_audio, &b_audio );
00293 }
00294
00295 p_block->i_dts =
00296 p_block->i_pts = p_sys->i_pcr;
00297
00298 if( b_audio )
00299 {
00300 block_t *p_audio_block = dv_extract_audio( p_demux, p_block );
00301 if( p_audio_block )
00302 {
00303 p_audio_block->i_pts =
00304 p_audio_block->i_dts = p_sys->i_pcr;
00305 es_out_Send( p_demux->out, p_sys->p_es_audio, p_audio_block );
00306 }
00307 }
00308
00309 es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
00310
00311 p_sys->i_pcr += ( I64C(1000000) / p_sys->f_rate );
00312
00313 return 1;
00314 }
00315
00316
00317
00318
00319 static int Control( demux_t *p_demux, int i_query, va_list args )
00320 {
00321 demux_sys_t *p_sys = p_demux->p_sys;
00322
00323
00324 return demux2_vaControlHelper( p_demux->s,
00325 0, -1,
00326 p_sys->frame_size * p_sys->f_rate * 8,
00327 p_sys->frame_size, i_query, args );
00328 }
00329
00330 static const uint16_t dv_audio_shuffle525[10][9] = {
00331 { 0, 30, 60, 20, 50, 80, 10, 40, 70 },
00332 { 6, 36, 66, 26, 56, 86, 16, 46, 76 },
00333 { 12, 42, 72, 2, 32, 62, 22, 52, 82 },
00334 { 18, 48, 78, 8, 38, 68, 28, 58, 88 },
00335 { 24, 54, 84, 14, 44, 74, 4, 34, 64 },
00336
00337 { 1, 31, 61, 21, 51, 81, 11, 41, 71 },
00338 { 7, 37, 67, 27, 57, 87, 17, 47, 77 },
00339 { 13, 43, 73, 3, 33, 63, 23, 53, 83 },
00340 { 19, 49, 79, 9, 39, 69, 29, 59, 89 },
00341 { 25, 55, 85, 15, 45, 75, 5, 35, 65 },
00342 };
00343
00344 static const uint16_t dv_audio_shuffle625[12][9] = {
00345 { 0, 36, 72, 26, 62, 98, 16, 52, 88},
00346 { 6, 42, 78, 32, 68, 104, 22, 58, 94},
00347 { 12, 48, 84, 2, 38, 74, 28, 64, 100},
00348 { 18, 54, 90, 8, 44, 80, 34, 70, 106},
00349 { 24, 60, 96, 14, 50, 86, 4, 40, 76},
00350 { 30, 66, 102, 20, 56, 92, 10, 46, 82},
00351
00352 { 1, 37, 73, 27, 63, 99, 17, 53, 89},
00353 { 7, 43, 79, 33, 69, 105, 23, 59, 95},
00354 { 13, 49, 85, 3, 39, 75, 29, 65, 101},
00355 { 19, 55, 91, 9, 45, 81, 35, 71, 107},
00356 { 25, 61, 97, 15, 51, 87, 5, 41, 77},
00357 { 31, 67, 103, 21, 57, 93, 11, 47, 83},
00358 };
00359
00360 static inline uint16_t dv_audio_12to16( uint16_t sample )
00361 {
00362 uint16_t shift, result;
00363
00364 sample = (sample < 0x800) ? sample : sample | 0xf000;
00365 shift = (sample & 0xf00) >> 8;
00366
00367 if (shift < 0x2 || shift > 0xd) {
00368 result = sample;
00369 } else if (shift < 0x8) {
00370 shift--;
00371 result = (sample - (256 * shift)) << shift;
00372 } else {
00373 shift = 0xe - shift;
00374 result = ((sample + ((256 * shift) + 1)) << shift) - 1;
00375 }
00376
00377 return result;
00378 }
00379
00380 static block_t *dv_extract_audio( demux_t *p_demux,
00381 block_t* p_frame_block )
00382 {
00383 demux_sys_t *p_sys = p_demux->p_sys;
00384 block_t *p_block;
00385 uint8_t *p_frame, *p_buf;
00386 int i_audio_quant, i_samples, i_size, i_half_ch;
00387 const uint16_t (*audio_shuffle)[9];
00388 int i, j, d, of;
00389 uint16_t lc;
00390
00391
00392 p_buf = p_frame_block->p_buffer + 80*6+80*16*3 + 3;
00393 if( *p_buf != 0x50 ) return NULL;
00394
00395 i_audio_quant = p_buf[4] & 0x07;
00396 if( i_audio_quant > 1 )
00397 {
00398 msg_Dbg( p_demux, "Unsupported quantization for DV audio");
00399 return NULL;
00400 }
00401
00402 i_samples = p_buf[1] & 0x3f;
00403 switch( (p_buf[4] >> 3) & 0x07 )
00404 {
00405 case 0:
00406 i_size = p_sys->i_dsf ? 1896 : 1580;
00407 break;
00408 case 1:
00409 i_size = p_sys->i_dsf ? 1742 : 1452;
00410 break;
00411 case 2:
00412 default:
00413 i_size = p_sys->i_dsf ? 1264 : 1053;
00414 break;
00415 }
00416 i_size = (i_size + i_samples) * 4;
00417
00418 p_block = block_New( p_demux, i_size );
00419
00420
00421 p_frame = p_frame_block->p_buffer;
00422 audio_shuffle = p_sys->i_dsf ? dv_audio_shuffle625 : dv_audio_shuffle525;
00423 i_half_ch = (p_sys->i_dsf ? 12 : 10)/2;
00424 for( i = 0; i < (p_sys->i_dsf ? 12 : 10); i++ )
00425 {
00426 p_frame += 6 * 80;
00427
00428 if( i_audio_quant == 1 && i == i_half_ch ) break;
00429
00430 for( j = 0; j < 9; j++ )
00431 {
00432 for( d = 8; d < 80; d += 2 )
00433 {
00434 if( i_audio_quant == 0 )
00435 {
00436
00437 of = audio_shuffle[i][j] + (d - 8) / 2 *
00438 (p_sys->i_dsf ? 108 : 90);
00439
00440 if( of * 2 >= i_size ) continue;
00441
00442
00443 p_block->p_buffer[of*2] = p_frame[d+1];
00444 p_block->p_buffer[of*2+1] = p_frame[d];
00445
00446 if( p_block->p_buffer[of*2+1] == 0x80 &&
00447 p_block->p_buffer[of*2] == 0x00 )
00448 p_block->p_buffer[of*2+1] = 0;
00449 }
00450 else
00451 {
00452
00453 lc = ((uint16_t)p_frame[d] << 4) |
00454 ((uint16_t)p_frame[d+2] >> 4);
00455 lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));
00456
00457 of = audio_shuffle[i][j] + (d - 8) / 3 *
00458 (p_sys->i_dsf ? 108 : 90);
00459 if( of*2 >= i_size ) continue;
00460
00461
00462 p_block->p_buffer[of*2] = lc & 0xff;
00463 p_block->p_buffer[of*2+1] = lc >> 8;
00464 ++d;
00465 }
00466 }
00467
00468 p_frame += 16 * 80;
00469 }
00470 }
00471
00472 return p_block;
00473 }
00474
00475