rawdv.c 14.8 KB
Newer Older
Gildas Bazin's avatar
 
Gildas Bazin committed
1 2 3
/*****************************************************************************
 * rawdv.c : raw dv input module for vlc
 *****************************************************************************
4 5
 * Copyright (C) 2001-2004 VideoLAN
 * $Id$
Gildas Bazin's avatar
 
Gildas Bazin committed
6 7 8 9 10 11 12
 *
 * Authors: Gildas Bazin <gbazin@netcourrier.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
13
 *
Gildas Bazin's avatar
 
Gildas Bazin committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */

#include <vlc/vlc.h>
#include <vlc/input.h>

32 33 34 35 36 37 38 39
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

vlc_module_begin();
    set_description( _("raw dv demuxer") );
40
    set_capability( "demux2", 2 );
41 42 43
    set_callbacks( Open, Close );
    add_shortcut( "rawdv" );
vlc_module_end();
Gildas Bazin's avatar
 
Gildas Bazin committed
44 45 46 47 48 49 50


/*****************************************************************************
 A little bit of background information (copied over from libdv glossary).

 - DIF block: A block of 80 bytes. This is the basic data framing unit of the
       DVC tape format, analogous to sectors of hard disc.
51

Gildas Bazin's avatar
 
Gildas Bazin committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
 - Video Section: Each DIF sequence contains a video section, consisting of
       135 DIF blocks, which are further subdivided into Video Segments.

 - Video Segment: A video segment consists of 5 DIF blocks, each corresponding
       to a single compressed macroblock.

*****************************************************************************/


/*****************************************************************************
 * Constants
 *****************************************************************************/
#define DV_PAL_FRAME_SIZE  144000
#define DV_NTSC_FRAME_SIZE 122000

/*****************************************************************************
 * Definitions of structures used by this plugin
 *****************************************************************************/
typedef struct {
    int8_t sct;      /* Section type (header,subcode,aux,audio,video) */
    int8_t dsn;      /* DIF sequence number (0-12) */
    int    fsc;      /* First (0)/Second channel (1) */
    int8_t dbn;      /* DIF block number (0-134) */
} dv_id_t;

typedef struct {
    int    dsf;      /* DIF sequence flag: 525/60 (0) or 625,50 (1) */
    int8_t apt;
    int    tf1;
    int8_t ap1;
    int    tf2;
    int8_t ap2;
    int    tf3;
    int8_t ap3;
} dv_header_t;

struct demux_sys_t
{
    int    frame_size;

92 93
    es_out_id_t *p_es_video;
    es_format_t  fmt_video;
Gildas Bazin's avatar
 
Gildas Bazin committed
94

95 96
    es_out_id_t *p_es_audio;
    es_format_t  fmt_audio;
Gildas Bazin's avatar
 
Gildas Bazin committed
97

Gildas Bazin's avatar
 
Gildas Bazin committed
98
    int    i_dsf;
Gildas Bazin's avatar
 
Gildas Bazin committed
99 100 101 102 103 104 105 106 107 108
    double f_rate;
    int    i_bitrate;

    /* program clock reference (in units of 90kHz) */
    mtime_t i_pcr;
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
109 110 111 112 113
static int Demux( demux_t * );
static int Control( demux_t *, int i_query, va_list args );

static block_t *dv_extract_audio( demux_t *p_demux,
                                  block_t* p_frame_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
114 115

/*****************************************************************************
116
 * Open: initializes raw dv demux structures
Gildas Bazin's avatar
 
Gildas Bazin committed
117
 *****************************************************************************/
118
static int Open( vlc_object_t * p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
119
{
120 121
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
122

123
    byte_t      *p_peek, *p_peek_backup;
124

125 126 127 128
    uint32_t    i_dword;
    dv_header_t dv_header;
    dv_id_t     dv_id;
    char        *psz_ext;
Gildas Bazin's avatar
 
Gildas Bazin committed
129 130 131 132 133 134 135

    /* It isn't easy to recognize a raw dv stream. The chances that we'll
     * mistake a stream from another type for a raw dv stream are too high, so
     * we'll rely on the file extension to trigger this demux. Alternatively,
     * it is possible to force this demux. */

    /* Check for dv file extension */
136 137 138
    psz_ext = strrchr( p_demux->psz_path, '.' );
    if( ( !psz_ext || strcasecmp( psz_ext, ".dv") ) &&
        strcmp(p_demux->psz_demux, "rawdv") )
Gildas Bazin's avatar
 
Gildas Bazin committed
139
    {
140
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
141 142
    }

143
    if( stream_Peek( p_demux->s, &p_peek, DV_PAL_FRAME_SIZE ) <
Gildas Bazin's avatar
 
Gildas Bazin committed
144
        DV_NTSC_FRAME_SIZE )
Gildas Bazin's avatar
 
Gildas Bazin committed
145 146
    {
        /* Stream too short ... */
147
        msg_Err( p_demux, "cannot peek()" );
148
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
149
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
150
    p_peek_backup = p_peek;
Gildas Bazin's avatar
 
Gildas Bazin committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164

    /* fill in the dv_id_t structure */
    i_dword = GetDWBE( p_peek ); p_peek += 4;
    dv_id.sct = i_dword >> (32 - 3);
    i_dword <<= 8;
    dv_id.dsn = i_dword >> (32 - 4);
    i_dword <<= 4;
    dv_id.fsc = i_dword >> (32 - 1);
    i_dword <<= 4;
    dv_id.dbn = i_dword >> (32 - 8);
    i_dword <<= 8;

    if( dv_id.sct != 0 )
    {
165
        msg_Warn( p_demux, "not a raw dv stream header" );
166
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
167 168 169 170 171 172 173
    }

    /* fill in the dv_header_t structure */
    dv_header.dsf = i_dword >> (32 - 1);
    i_dword <<= 1;
    if( i_dword >> (32 - 1) ) /* incorrect bit */
    {
174
        msg_Warn( p_demux, "incorrect bit" );
175
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    }

    i_dword = GetDWBE( p_peek ); p_peek += 4;
    i_dword <<= 5;
    dv_header.apt = i_dword >> (32 - 3);
    i_dword <<= 3;
    dv_header.tf1 = i_dword >> (32 - 1);
    i_dword <<= 5;
    dv_header.ap1 = i_dword >> (32 - 3);
    i_dword <<= 3;
    dv_header.tf2 = i_dword >> (32 - 1);
    i_dword <<= 5;
    dv_header.ap2 = i_dword >> (32 - 3);
    i_dword <<= 3;
    dv_header.tf3 = i_dword >> (32 - 1);
    i_dword <<= 5;
    dv_header.ap3 = i_dword >> (32 - 3);

    p_peek += 72;                                  /* skip rest of DIF block */


197
    /* Set p_input field */
198 199 200
    p_demux->pf_demux   = Demux;
    p_demux->pf_control = Control;
    p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
Gildas Bazin's avatar
 
Gildas Bazin committed
201

Gildas Bazin's avatar
 
Gildas Bazin committed
202
    p_sys->i_dsf = dv_header.dsf;
203 204 205 206 207 208 209 210 211
    p_sys->frame_size = dv_header.dsf ? 12 * 150 * 80 : 10 * 150 * 80;
    p_sys->f_rate = dv_header.dsf ? 25 : 29.97;

    p_sys->i_pcr = 0;
    p_sys->p_es_video = NULL;
    p_sys->p_es_audio = NULL;

    p_sys->i_bitrate = 0;

Gildas Bazin's avatar
 
Gildas Bazin committed
212
    es_format_Init( &p_sys->fmt_video, VIDEO_ES, VLC_FOURCC('d','v','s','d') );
213 214
    p_sys->fmt_video.video.i_width = 720;
    p_sys->fmt_video.video.i_height= dv_header.dsf ? 576 : 480;;
Gildas Bazin's avatar
 
Gildas Bazin committed
215

216 217
    /* FIXME FIXME */
#if 0
Gildas Bazin's avatar
 
Gildas Bazin committed
218 219
    /* necessary because input_SplitBuffer() will only get
     * INPUT_DEFAULT_BUFSIZE bytes at a time. */
220
    p_input->i_bufsize = p_sys->frame_size;
221
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
222

223
    p_sys->p_es_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
Gildas Bazin's avatar
 
Gildas Bazin committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249

    /* Audio stuff */
    p_peek = p_peek_backup + 80*6+80*16*3 + 3; /* beginning of AAUX pack */
    if( *p_peek == 0x50 )
    {
        es_format_Init( &p_sys->fmt_audio, AUDIO_ES,
                        VLC_FOURCC('a','r','a','w') );

        p_sys->fmt_audio.audio.i_channels = 2;
        switch( (p_peek[4] >> 3) & 0x07 )
        {
        case 0:
            p_sys->fmt_audio.audio.i_rate = 48000;
            break;
        case 1:
            p_sys->fmt_audio.audio.i_rate = 44100;
            break;
        case 2:
        default:
            p_sys->fmt_audio.audio.i_rate = 32000;
            break;
        }

        /* 12 bits non-linear will be converted to 16 bits linear */
        p_sys->fmt_audio.audio.i_bitspersample = 16;

250
        p_sys->p_es_audio = es_out_Add( p_demux->out, &p_sys->fmt_audio );
Gildas Bazin's avatar
 
Gildas Bazin committed
251
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
252

253
    return VLC_SUCCESS;
Gildas Bazin's avatar
 
Gildas Bazin committed
254 255 256
}

/*****************************************************************************
257
 * Close: frees unused data
Gildas Bazin's avatar
 
Gildas Bazin committed
258
 *****************************************************************************/
259
static void Close( vlc_object_t *p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
260
{
261 262
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys  = p_demux->p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
263

264
    free( p_sys );
Gildas Bazin's avatar
 
Gildas Bazin committed
265 266 267 268 269 270 271
}

/*****************************************************************************
 * Demux: reads and demuxes data packets
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
272
static int Demux( demux_t *p_demux )
Gildas Bazin's avatar
 
Gildas Bazin committed
273
{
274 275 276
    demux_sys_t *p_sys  = p_demux->p_sys;
    block_t     *p_block;
    vlc_bool_t  b_audio = VLC_FALSE;
Gildas Bazin's avatar
 
Gildas Bazin committed
277 278

    /* Call the pace control */
279
    es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
Gildas Bazin's avatar
 
Gildas Bazin committed
280

281
    if( ( p_block = stream_Block( p_demux->s, p_sys->frame_size ) ) == NULL )
Gildas Bazin's avatar
 
Gildas Bazin committed
282
    {
283 284
        /* EOF */
        return 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
285 286
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
287 288
    if( p_sys->p_es_audio )
    {
289
        es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
Gildas Bazin's avatar
 
Gildas Bazin committed
290 291 292
                        p_sys->p_es_audio, &b_audio );
    }

293
    p_block->i_dts =
294 295
    p_block->i_pts = p_sys->i_pcr;

Gildas Bazin's avatar
 
Gildas Bazin committed
296
    if( b_audio )
Gildas Bazin's avatar
 
Gildas Bazin committed
297
    {
298
        block_t *p_audio_block = dv_extract_audio( p_demux, p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
299
        if( p_audio_block )
Gildas Bazin's avatar
 
Gildas Bazin committed
300
        {
301 302 303
            p_audio_block->i_pts =
            p_audio_block->i_dts = p_sys->i_pcr;
            es_out_Send( p_demux->out, p_sys->p_es_audio, p_audio_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
304 305
        }
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
306

307
    es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
308

309
    p_sys->i_pcr += ( I64C(1000000) / p_sys->f_rate );
Gildas Bazin's avatar
 
Gildas Bazin committed
310

Gildas Bazin's avatar
 
Gildas Bazin committed
311 312
    return 1;
}
313

314 315 316 317 318 319 320 321 322 323 324 325 326 327
/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
    demux_sys_t *p_sys  = p_demux->p_sys;

    /* XXX: DEMUX_SET_TIME is precise here */
    return demux2_vaControlHelper( p_demux->s,
                                   0, -1,
                                   p_sys->frame_size * p_sys->f_rate * 8,
                                   p_sys->frame_size, i_query, args );
}

Gildas Bazin's avatar
 
Gildas Bazin committed
328 329 330 331 332 333
static const uint16_t dv_audio_shuffle525[10][9] = {
  {  0, 30, 60, 20, 50, 80, 10, 40, 70 }, /* 1st channel */
  {  6, 36, 66, 26, 56, 86, 16, 46, 76 },
  { 12, 42, 72,  2, 32, 62, 22, 52, 82 },
  { 18, 48, 78,  8, 38, 68, 28, 58, 88 },
  { 24, 54, 84, 14, 44, 74,  4, 34, 64 },
334

Gildas Bazin's avatar
 
Gildas Bazin committed
335 336 337 338 339 340 341 342 343 344 345 346
  {  1, 31, 61, 21, 51, 81, 11, 41, 71 }, /* 2nd channel */
  {  7, 37, 67, 27, 57, 87, 17, 47, 77 },
  { 13, 43, 73,  3, 33, 63, 23, 53, 83 },
  { 19, 49, 79,  9, 39, 69, 29, 59, 89 },
  { 25, 55, 85, 15, 45, 75,  5, 35, 65 },
};

static const uint16_t dv_audio_shuffle625[12][9] = {
  {   0,  36,  72,  26,  62,  98,  16,  52,  88}, /* 1st channel */
  {   6,  42,  78,  32,  68, 104,  22,  58,  94},
  {  12,  48,  84,   2,  38,  74,  28,  64, 100},
  {  18,  54,  90,   8,  44,  80,  34,  70, 106},
347
  {  24,  60,  96,  14,  50,  86,   4,  40,  76},
Gildas Bazin's avatar
 
Gildas Bazin committed
348
  {  30,  66, 102,  20,  56,  92,  10,  46,  82},
349

Gildas Bazin's avatar
 
Gildas Bazin committed
350 351 352 353
  {   1,  37,  73,  27,  63,  99,  17,  53,  89}, /* 2nd channel */
  {   7,  43,  79,  33,  69, 105,  23,  59,  95},
  {  13,  49,  85,   3,  39,  75,  29,  65, 101},
  {  19,  55,  91,   9,  45,  81,  35,  71, 107},
354
  {  25,  61,  97,  15,  51,  87,   5,  41,  77},
Gildas Bazin's avatar
 
Gildas Bazin committed
355 356 357 358 359 360
  {  31,  67, 103,  21,  57,  93,  11,  47,  83},
};

static inline uint16_t dv_audio_12to16( uint16_t sample )
{
    uint16_t shift, result;
361

Gildas Bazin's avatar
 
Gildas Bazin committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    sample = (sample < 0x800) ? sample : sample | 0xf000;
    shift = (sample & 0xf00) >> 8;

    if (shift < 0x2 || shift > 0xd) {
        result = sample;
    } else if (shift < 0x8) {
        shift--;
        result = (sample - (256 * shift)) << shift;
    } else {
        shift = 0xe - shift;
        result = ((sample + ((256 * shift) + 1)) << shift) - 1;
    }

    return result;
}

378
static block_t *dv_extract_audio( demux_t *p_demux,
Gildas Bazin's avatar
 
Gildas Bazin committed
379 380
                                  block_t* p_frame_block )
{
381
    demux_sys_t *p_sys  = p_demux->p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395
    block_t *p_block;
    uint8_t *p_frame, *p_buf;
    int i_audio_quant, i_samples, i_size, i_half_ch;
    const uint16_t (*audio_shuffle)[9];
    int i, j, d, of;
    uint16_t lc;

    /* Beginning of AAUX pack */
    p_buf = p_frame_block->p_buffer + 80*6+80*16*3 + 3;
    if( *p_buf != 0x50 ) return NULL;

    i_audio_quant = p_buf[4] & 0x07; /* 0 - 16bit, 1 - 12bit */
    if( i_audio_quant > 1 )
    {
396
        msg_Dbg( p_demux, "Unsupported quantization for DV audio");
Gildas Bazin's avatar
 
Gildas Bazin committed
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
        return NULL;
    }

    i_samples = p_buf[1] & 0x3f; /* samples in this frame - min samples */
    switch( (p_buf[4] >> 3) & 0x07 )
    {
    case 0:
        i_size = p_sys->i_dsf ? 1896 : 1580;
        break;
    case 1:
        i_size = p_sys->i_dsf ? 1742 : 1452;
        break;
    case 2:
    default:
        i_size = p_sys->i_dsf ? 1264 : 1053;
        break;
    }
    i_size = (i_size + i_samples) * 4; /* 2ch, 2bytes */

416
    p_block = block_New( p_demux, i_size );
Gildas Bazin's avatar
 
Gildas Bazin committed
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471

    /* for each DIF segment */
    p_frame = p_frame_block->p_buffer;
    audio_shuffle = p_sys->i_dsf ? dv_audio_shuffle625 : dv_audio_shuffle525;
    i_half_ch = (p_sys->i_dsf ? 12 : 10)/2;
    for( i = 0; i < (p_sys->i_dsf ? 12 : 10); i++ )
    {
        p_frame += 6 * 80; /* skip DIF segment header */

        if( i_audio_quant == 1 && i == i_half_ch ) break;

        for( j = 0; j < 9; j++ )
        {
            for( d = 8; d < 80; d += 2 )
            {
                if( i_audio_quant == 0 )
                {
                    /* 16bit quantization */
                    of = audio_shuffle[i][j] + (d - 8) / 2 *
                           (p_sys->i_dsf ? 108 : 90);

                    if( of * 2 >= i_size ) continue;

                    /* big endian */
                    p_block->p_buffer[of*2] = p_frame[d+1];
                    p_block->p_buffer[of*2+1] = p_frame[d];

                    if( p_block->p_buffer[of*2+1] == 0x80 &&
                        p_block->p_buffer[of*2] == 0x00 )
                        p_block->p_buffer[of*2+1] = 0;
                }
                else
                {
                    /* 12bit quantization */
                    lc = ((uint16_t)p_frame[d] << 4) | 
                         ((uint16_t)p_frame[d+2] >> 4);
                    lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));

                    of = audio_shuffle[i][j] + (d - 8) / 3 *
                         (p_sys->i_dsf ? 108 : 90);
                    if( of*2 >= i_size ) continue;

                    /* big endian */
                    p_block->p_buffer[of*2] = lc & 0xff;
                    p_block->p_buffer[of*2+1] = lc >> 8;
                    ++d;
                }
            }

            p_frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
        }
    }

    return p_block;
}
472 473