rawdv.c 14.7 KB
Newer Older
Gildas Bazin's avatar
 
Gildas Bazin committed
1 2 3 4
/*****************************************************************************
 * rawdv.c : raw dv input module for vlc
 *****************************************************************************
 * Copyright (C) 2001 VideoLAN
5
 * $Id: rawdv.c,v 1.11 2003/11/24 00:39:01 fenrir Exp $
Gildas Bazin's avatar
 
Gildas Bazin committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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
 *
 * 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.
 * 
 * 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 <string.h>                                              /* strdup() */
#include <errno.h>

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

#include <sys/types.h>

#include <codecs.h>                        /* BITMAPINFOHEADER, WAVEFORMATEX */

/*****************************************************************************
 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.
 
 - 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;

    es_descriptor_t  *p_video_es;
    es_descriptor_t  *p_audio_es;

    /* codec specific stuff */
    BITMAPINFOHEADER *p_bih;
Gildas Bazin's avatar
 
Gildas Bazin committed
89
    WAVEFORMATEX *p_wf;
Gildas Bazin's avatar
 
Gildas Bazin committed
90 91 92 93 94 95 96 97 98 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
 *****************************************************************************/
static int  Activate  ( vlc_object_t * );
static void Deactivate( vlc_object_t * );
static int  Demux     ( input_thread_t * );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
Gildas Bazin's avatar
 
Gildas Bazin committed
109
    set_description( _("raw dv demuxer") );
Gildas Bazin's avatar
 
Gildas Bazin committed
110 111 112 113 114 115 116 117 118 119 120
    set_capability( "demux", 2 );
    set_callbacks( Activate, NULL );
    add_shortcut( "rawdv" );
vlc_module_end();

/*****************************************************************************
 * Activate: initializes raw dv demux structures
 *****************************************************************************/
static int Activate( vlc_object_t * p_this )
{
    input_thread_t *p_input = (input_thread_t *)p_this;
Gildas Bazin's avatar
 
Gildas Bazin committed
121
    byte_t         *p_peek, *p_peek_backup;
Gildas Bazin's avatar
 
Gildas Bazin committed
122 123 124 125 126 127 128 129 130 131 132 133 134
    uint32_t       i_dword;
    demux_sys_t    *p_rawdv;
    dv_header_t    dv_header;
    dv_id_t        dv_id;
    char           *psz_ext;

    /* 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 */
    psz_ext = strrchr ( p_input->psz_name, '.' );
135
    if( ( !psz_ext || strcasecmp( psz_ext, ".dv") )&&
Gildas Bazin's avatar
 
Gildas Bazin committed
136 137 138 139 140 141
        ( !p_input->psz_demux || strcmp(p_input->psz_demux, "rawdv") ) )
    {
        return -1;
    }

    p_input->pf_demux = Demux;
142
    p_input->pf_demux_control = demux_vaControlDefault;
Gildas Bazin's avatar
 
Gildas Bazin committed
143 144 145 146 147 148 149 150

    /* Have a peep at the show. */
    if( input_Peek(p_input, &p_peek, DV_PAL_FRAME_SIZE) < DV_NTSC_FRAME_SIZE )
    {
        /* Stream too short ... */
        msg_Err( p_input, "cannot peek()" );
        return -1;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
151
    p_peek_backup = p_peek;
Gildas Bazin's avatar
 
Gildas Bazin committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

    /* 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 )
    {
        msg_Warn( p_input, "not a raw dv stream header" );
        return -1;
    }

    /* fill in the dv_header_t structure */
    dv_header.dsf = i_dword >> (32 - 1);
    i_dword <<= 1;
    if( i_dword >> (32 - 1) ) /* incorrect bit */
    {
        msg_Warn( p_input, "incorrect bit" );
        return -1;
    }

    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 */


    /* Setup the structures for our demuxer */
    if( !( p_rawdv = malloc( sizeof( demux_sys_t ) ) ) )
    {
        msg_Err( p_input, "out of memory" );
        return -1;
    }
    memset( p_rawdv, 0, sizeof( demux_sys_t ) );
    p_rawdv->p_bih = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
206
    p_rawdv->p_wf = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    p_input->p_demux_data = p_rawdv;

    p_rawdv->p_bih = (BITMAPINFOHEADER *) malloc( sizeof(BITMAPINFOHEADER) );
    if( !p_rawdv->p_bih )
    {
        msg_Err( p_input, "out of memory" );
        goto error;
    }
    p_rawdv->p_bih->biSize = sizeof(BITMAPINFOHEADER);
    p_rawdv->p_bih->biCompression = VLC_FOURCC( 'd','v','s','d' );
    p_rawdv->p_bih->biSize = 40;
    p_rawdv->p_bih->biWidth = 720;
    p_rawdv->p_bih->biHeight = dv_header.dsf ? 576 : 480;
    p_rawdv->p_bih->biPlanes = 1;
    p_rawdv->p_bih->biBitCount = 24;
    p_rawdv->p_bih->biSizeImage =
        p_rawdv->p_bih->biWidth * p_rawdv->p_bih->biHeight
          * (p_rawdv->p_bih->biBitCount >> 3);

    /* Properties of our video */
    if( dv_header.dsf )
    {
        p_rawdv->frame_size = 12 * 150 * 80;
        p_rawdv->f_rate = 25;
    }
    else
    {
        p_rawdv->frame_size = 10 * 150 * 80;
        p_rawdv->f_rate = 29.97;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    /* Audio stuff */
#if 0
    p_peek = p_peek_backup + 80*6+80*16*3 + 3; /* beginning of AAUX pack */

    if( *p_peek != 0x50 || *p_peek != 0x51 )
    {
        msg_Err( p_input, "AAUX should begin with 0x50" );
    }
#endif

    p_rawdv->p_wf = (WAVEFORMATEX *)malloc( sizeof(WAVEFORMATEX) );
    if( !p_rawdv->p_wf )
    {
        msg_Err( p_input, "out of memory" );
        goto error;
    }

    p_rawdv->p_wf->wFormatTag = 0;
    p_rawdv->p_wf->nChannels = 2;
    p_rawdv->p_wf->nSamplesPerSec = 44100; /* FIXME */
    p_rawdv->p_wf->nAvgBytesPerSec = p_rawdv->f_rate * p_rawdv->frame_size;
    p_rawdv->p_wf->nBlockAlign = p_rawdv->frame_size;
    p_rawdv->p_wf->wBitsPerSample = 16;
    p_rawdv->p_wf->cbSize = 0;


Gildas Bazin's avatar
 
Gildas Bazin committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    /* necessary because input_SplitBuffer() will only get
     * INPUT_DEFAULT_BUFSIZE bytes at a time. */
    p_input->i_bufsize = p_rawdv->frame_size;

    /* Create one program */
    vlc_mutex_lock( &p_input->stream.stream_lock );
    if( input_InitStream( p_input, 0 ) == -1)
    {
        vlc_mutex_unlock( &p_input->stream.stream_lock );
        msg_Err( p_input, "cannot init stream" );
        goto error;
    }
    if( input_AddProgram( p_input, 0, 0) == NULL )
    {
        vlc_mutex_unlock( &p_input->stream.stream_lock );
        msg_Err( p_input, "cannot add program" );
        goto error;
    }
    p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
    p_input->stream.i_mux_rate = p_rawdv->frame_size * p_rawdv->f_rate;
    vlc_mutex_unlock( &p_input->stream.stream_lock );

    /* Add video stream */
    vlc_mutex_lock( &p_input->stream.stream_lock );
    p_rawdv->p_video_es = input_AddES( p_input,
                                       p_input->stream.p_selected_program,
Gildas Bazin's avatar
 
Gildas Bazin committed
290
                                       1, VIDEO_ES, NULL, 0 );
Gildas Bazin's avatar
 
Gildas Bazin committed
291 292
    p_rawdv->p_video_es->i_stream_id = 0;
    p_rawdv->p_video_es->i_fourcc = VLC_FOURCC( 'd','v','s','d' );
293
    p_rawdv->p_video_es->p_bitmapinfoheader = (void *)p_rawdv->p_bih;
Gildas Bazin's avatar
 
Gildas Bazin committed
294 295 296
    input_SelectES( p_input, p_rawdv->p_video_es );
    vlc_mutex_unlock( &p_input->stream.stream_lock );

Gildas Bazin's avatar
 
Gildas Bazin committed
297 298 299 300
    /* Add audio stream */
    vlc_mutex_lock( &p_input->stream.stream_lock );
    p_rawdv->p_audio_es = input_AddES( p_input,
                                       p_input->stream.p_selected_program,
Gildas Bazin's avatar
 
Gildas Bazin committed
301
                                       2, AUDIO_ES, NULL, 0 );
Gildas Bazin's avatar
 
Gildas Bazin committed
302 303 304 305 306 307
    p_rawdv->p_audio_es->i_stream_id = 1;
    p_rawdv->p_audio_es->i_fourcc = VLC_FOURCC( 'd','v','a','u' );
    p_rawdv->p_audio_es->p_waveformatex = (void *)p_rawdv->p_wf;
    input_SelectES( p_input, p_rawdv->p_audio_es );
    vlc_mutex_unlock( &p_input->stream.stream_lock );

Gildas Bazin's avatar
 
Gildas Bazin committed
308 309 310 311 312 313 314
    /* Init pcr */
    p_rawdv->i_pcr = 0;

    return 0;

 error:
    if( p_rawdv->p_bih ) free( p_rawdv->p_bih );
Gildas Bazin's avatar
 
Gildas Bazin committed
315
    if( p_rawdv->p_wf ) free( p_rawdv->p_wf );
Gildas Bazin's avatar
 
Gildas Bazin committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    Deactivate( (vlc_object_t *)p_input );
    return -1;
}

/*****************************************************************************
 * Deactivate: frees unused data
 *****************************************************************************/
static void Deactivate( vlc_object_t *p_this )
{
    input_thread_t *p_input = (input_thread_t *)p_this;
    demux_sys_t *p_rawdv = (demux_sys_t *)p_input->p_demux_data;

    free( p_rawdv );
}

/*****************************************************************************
 * Demux: reads and demuxes data packets
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
static int Demux( input_thread_t * p_input )
{
    demux_sys_t    *p_rawdv = (demux_sys_t *)p_input->p_demux_data;
Gildas Bazin's avatar
 
Gildas Bazin committed
339 340
    pes_packet_t   *p_pes = NULL;
    pes_packet_t   *p_audio_pes = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
    data_packet_t  *p_data;
    ssize_t        i_read;

    if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
    {
        off_t i_pos;

        msg_Warn( p_input, "synchro reinit" );

        /* If the user tried to seek in the stream, we need to make sure
         * the new position is at a DIF block boundary. */
        vlc_mutex_lock( &p_input->stream.stream_lock );
        i_pos= p_input->stream.p_selected_area->i_tell;
        vlc_mutex_unlock( &p_input->stream.stream_lock );

        if( (i_pos % p_rawdv->frame_size) &&
            p_input->stream.b_seekable &&
            p_input->stream.i_method == INPUT_METHOD_FILE )
        {
            p_input->pf_seek( p_input, (i_pos / p_rawdv->frame_size)
                              * p_rawdv->frame_size );
            input_AccessReinit( p_input );
        }
    }

    /* Call the pace control */
    input_ClockManageRef( p_input, p_input->stream.p_selected_program,
                          p_rawdv->i_pcr );

    i_read = input_SplitBuffer( p_input, &p_data, p_rawdv->frame_size );
    if( i_read <= 0 )
    {
        return i_read;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
376
    /* Build video PES packet */
377
    if( p_rawdv->p_video_es->p_dec )
Gildas Bazin's avatar
 
Gildas Bazin committed
378
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
379 380 381 382 383 384 385
        p_pes = input_NewPES( p_input->p_method_data );
        if( p_pes == NULL )
        {
            msg_Err( p_input, "out of memory" );
            input_DeletePacket( p_input->p_method_data, p_data );
            return -1;
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
386

Gildas Bazin's avatar
 
Gildas Bazin committed
387 388 389 390 391 392 393 394
        p_pes->i_rate = p_input->stream.control.i_rate;
        p_pes->p_first = p_pes->p_last = p_data;
        p_pes->i_pes_size = i_read;
        p_pes->i_nb_data = 1;
        p_pes->i_pts =
            input_ClockGetTS( p_input, p_input->stream.p_selected_program,
                              p_rawdv->i_pcr );
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
395

Gildas Bazin's avatar
 
Gildas Bazin committed
396
    /* Do the same for audio */
397
    if( p_rawdv->p_audio_es->p_dec )
Gildas Bazin's avatar
 
Gildas Bazin committed
398
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
399
        p_audio_pes = input_NewPES( p_input->p_method_data );
Gildas Bazin's avatar
 
Gildas Bazin committed
400
        if( p_audio_pes == NULL )
Gildas Bazin's avatar
 
Gildas Bazin committed
401 402 403 404 405 406 407
        {
            msg_Err( p_input, "out of memory" );
            input_DeletePacket( p_input->p_method_data, p_data );
            return -1;
        }
        p_audio_pes->i_rate = p_input->stream.control.i_rate;

408
        if( p_rawdv->p_video_es->p_dec )
Gildas Bazin's avatar
 
Gildas Bazin committed
409 410 411 412 413 414 415 416 417 418 419 420 421
            p_audio_pes->p_first = p_audio_pes->p_last =
                input_ShareBuffer( p_input->p_method_data, p_data->p_buffer );
        else
            p_audio_pes->p_first = p_audio_pes->p_last = p_data;

        p_audio_pes->p_first->p_next = p_data->p_next;
        p_audio_pes->p_first->p_payload_start = p_data->p_payload_start;
        p_audio_pes->p_first->p_payload_end = p_data->p_payload_end;
        p_audio_pes->i_pes_size = i_read;
        p_audio_pes->i_nb_data = 1;
        p_audio_pes->i_pts =
            input_ClockGetTS( p_input, p_input->stream.p_selected_program,
                              p_rawdv->i_pcr );
Gildas Bazin's avatar
 
Gildas Bazin committed
422
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
423 424

    /* Decode PES packets if stream is selected */
425 426 427 428
    if( p_rawdv->p_video_es->p_dec )
        input_DecodePES( p_rawdv->p_video_es->p_dec, p_pes );
    if( p_rawdv->p_audio_es->p_dec )
        input_DecodePES( p_rawdv->p_audio_es->p_dec, p_audio_pes );
Gildas Bazin's avatar
 
Gildas Bazin committed
429 430 431

    p_rawdv->i_pcr += ( 90000 / p_rawdv->f_rate );

432 433
    if( !p_rawdv->p_video_es->p_dec &&
        !p_rawdv->p_audio_es->p_dec )
Gildas Bazin's avatar
 
Gildas Bazin committed
434 435
        input_DeletePacket( p_input->p_method_data, p_data );

Gildas Bazin's avatar
 
Gildas Bazin committed
436 437
    return 1;
}