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

rawdv.c

00001 /*****************************************************************************
00002  * rawdv.c : raw DV input module for vlc
00003  *****************************************************************************
00004  * Copyright (C) 2001-2004 the VideoLAN team
00005  * $Id: rawdv.c 11664 2005-07-09 06:17:09Z courmisch $
00006  *
00007  * Authors: Gildas Bazin <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Preamble
00026  *****************************************************************************/
00027 #include <stdlib.h>                                      /* malloc(), free() */
00028 
00029 #include <vlc/vlc.h>
00030 #include <vlc/input.h>
00031 
00032 /*****************************************************************************
00033  * Module descriptor
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  A little bit of background information (copied over from libdv glossary).
00050 
00051  - DIF block: A block of 80 bytes. This is the basic data framing unit of the
00052        DVC tape format, analogous to sectors of hard disc.
00053 
00054  - Video Section: Each DIF sequence contains a video section, consisting of
00055        135 DIF blocks, which are further subdivided into Video Segments.
00056 
00057  - Video Segment: A video segment consists of 5 DIF blocks, each corresponding
00058        to a single compressed macroblock.
00059 
00060 *****************************************************************************/
00061 
00062 
00063 /*****************************************************************************
00064  * Constants
00065  *****************************************************************************/
00066 #define DV_PAL_FRAME_SIZE  144000
00067 #define DV_NTSC_FRAME_SIZE 122000
00068 
00069 /*****************************************************************************
00070  * Definitions of structures used by this plugin
00071  *****************************************************************************/
00072 typedef struct {
00073     int8_t sct;      /* Section type (header,subcode,aux,audio,video) */
00074     int8_t dsn;      /* DIF sequence number (0-12) */
00075     int    fsc;      /* First (0)/Second channel (1) */
00076     int8_t dbn;      /* DIF block number (0-134) */
00077 } dv_id_t;
00078 
00079 typedef struct {
00080     int    dsf;      /* DIF sequence flag: 525/60 (0) or 625,50 (1) */
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     /* program clock reference (in units of 90kHz) */
00105     mtime_t i_pcr;
00106 };
00107 
00108 /*****************************************************************************
00109  * Local prototypes
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  * Open: initializes raw DV demux structures
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     /* It isn't easy to recognize a raw DV stream. The chances that we'll
00133      * mistake a stream from another type for a raw DV stream are too high, so
00134      * we'll rely on the file extension to trigger this demux. Alternatively,
00135      * it is possible to force this demux. */
00136 
00137     /* Check for DV file extension */
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         /* Stream too short ... */
00149         msg_Err( p_demux, "cannot peek()" );
00150         return VLC_EGENERIC;
00151     }
00152     p_peek_backup = p_peek;
00153 
00154     /* fill in the dv_id_t structure */
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     /* fill in the dv_header_t structure */
00172     dv_header.dsf = i_dword >> (32 - 1);
00173     i_dword <<= 1;
00174     if( i_dword >> (32 - 1) ) /* incorrect bit */
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;                                  /* skip rest of DIF block */
00197 
00198 
00199     /* Set p_input field */
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     /* FIXME FIXME */
00219 #if 0
00220     /* necessary because input_SplitBuffer() will only get
00221      * INPUT_DEFAULT_BUFSIZE bytes at a time. */
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     /* Audio stuff */
00228     p_peek = p_peek_backup + 80*6+80*16*3 + 3; /* beginning of AAUX pack */
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         /* 12 bits non-linear will be converted to 16 bits linear */
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  * Close: frees unused data
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  * Demux: reads and demuxes data packets
00271  *****************************************************************************
00272  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
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     /* Call the pace control */
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         /* EOF */
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  * Control:
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     /* XXX: DEMUX_SET_TIME is precise here */
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 }, /* 1st channel */
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 }, /* 2nd channel */
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}, /* 1st channel */
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}, /* 2nd channel */
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     /* Beginning of AAUX pack */
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; /* 0 - 16bit, 1 - 12bit */
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; /* samples in this frame - min samples */
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; /* 2ch, 2bytes */
00417 
00418     p_block = block_New( p_demux, i_size );
00419 
00420     /* for each DIF segment */
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; /* skip DIF segment header */
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                     /* 16bit quantization */
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                     /* big endian */
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                     /* 12bit quantization */
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                     /* big endian */
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; /* 15 Video DIFs + 1 Audio DIF */
00469         }
00470     }
00471 
00472     return p_block;
00473 }
00474 
00475 

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