rawdv.c 10.9 KB
Newer Older
Gildas Bazin's avatar
 
Gildas Bazin committed
1 2 3
/*****************************************************************************
 * rawdv.c : raw dv input module for vlc
 *****************************************************************************
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
4 5
 * Copyright (C) 2001-2003 VideoLAN
 * $Id: rawdv.c,v 1.14 2004/01/25 20:05:28 hartman Exp $
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 40 41 42 43
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

vlc_module_begin();
    set_description( _("raw dv demuxer") );
    set_capability( "demux", 2 );
    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 98 99 100 101 102 103 104 105 106 107 108 109 110

    double f_rate;
    int    i_bitrate;

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

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Demux     ( input_thread_t * );

/*****************************************************************************
111
 * Open: initializes raw dv demux structures
Gildas Bazin's avatar
 
Gildas Bazin committed
112
 *****************************************************************************/
113
static int Open( vlc_object_t * p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
114 115
{
    input_thread_t *p_input = (input_thread_t *)p_this;
116 117
    demux_sys_t    *p_sys;

Gildas Bazin's avatar
 
Gildas Bazin committed
118
    byte_t         *p_peek, *p_peek_backup;
119

Gildas Bazin's avatar
 
Gildas Bazin committed
120 121 122 123 124 125 126 127 128 129 130 131
    uint32_t       i_dword;
    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, '.' );
132
    if( ( !psz_ext || strcasecmp( psz_ext, ".dv") )&&
Gildas Bazin's avatar
 
Gildas Bazin committed
133 134
        ( !p_input->psz_demux || strcmp(p_input->psz_demux, "rawdv") ) )
    {
135
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
136 137
    }

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

    /* 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" );
160
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
161 162 163 164 165 166 167 168
    }

    /* 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" );
169
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    }

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


191 192 193 194
    /* Set p_input field */
    p_input->pf_demux = Demux;
    p_input->pf_demux_control = demux_vaControlDefault;
    p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
Gildas Bazin's avatar
 
Gildas Bazin committed
195

196 197 198 199 200 201 202 203 204 205 206 207
    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;

    es_format_Init( &p_sys->fmt_video, VIDEO_ES, VLC_FOURCC( 'd','v','s','d' ) );
    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
208

Gildas Bazin's avatar
 
Gildas Bazin committed
209 210 211 212 213 214 215 216 217 218
    /* 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

219 220 221 222 223 224
    es_format_Init( &p_sys->fmt_audio, AUDIO_ES, VLC_FOURCC( 'd','v','a','u' ) );
    p_sys->fmt_audio.audio.i_channels = 2;
    p_sys->fmt_audio.audio.i_rate = 44100;  /* FIXME */
    p_sys->fmt_audio.audio.i_bitspersample = 16;
    p_sys->fmt_audio.audio.i_blockalign = p_sys->frame_size;    /* ??? */
    p_sys->fmt_audio.i_bitrate = p_sys->f_rate * p_sys->frame_size; /* ??? */
Gildas Bazin's avatar
 
Gildas Bazin committed
225

Gildas Bazin's avatar
 
Gildas Bazin committed
226 227
    /* necessary because input_SplitBuffer() will only get
     * INPUT_DEFAULT_BUFSIZE bytes at a time. */
228
    p_input->i_bufsize = p_sys->frame_size;
Gildas Bazin's avatar
 
Gildas Bazin committed
229 230 231 232 233 234

    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" );
235 236
        free( p_sys );
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
237
    }
238
    p_input->stream.i_mux_rate = p_sys->frame_size * p_sys->f_rate;
Gildas Bazin's avatar
 
Gildas Bazin committed
239 240
    vlc_mutex_unlock( &p_input->stream.stream_lock );

241 242
    p_sys->p_es_video = es_out_Add( p_input->p_es_out, &p_sys->fmt_video );
    p_sys->p_es_audio = es_out_Add( p_input->p_es_out, &p_sys->fmt_audio );
Gildas Bazin's avatar
 
Gildas Bazin committed
243

244
    return VLC_SUCCESS;
Gildas Bazin's avatar
 
Gildas Bazin committed
245 246 247
}

/*****************************************************************************
248
 * Close: frees unused data
Gildas Bazin's avatar
 
Gildas Bazin committed
249
 *****************************************************************************/
250
static void Close( vlc_object_t *p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
251 252
{
    input_thread_t *p_input = (input_thread_t *)p_this;
253
    demux_sys_t    *p_sys = p_input->p_demux_data;
Gildas Bazin's avatar
 
Gildas Bazin committed
254

255
    free( p_sys );
Gildas Bazin's avatar
 
Gildas Bazin committed
256 257 258 259 260 261 262 263 264
}

/*****************************************************************************
 * 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 )
{
265 266 267 268
    demux_sys_t    *p_sys = p_input->p_demux_data;
    block_t        *p_block;
    vlc_bool_t     b_audio, b_video;

Gildas Bazin's avatar
 
Gildas Bazin committed
269 270 271

    if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
    {
272
        off_t i_pos = stream_Tell( p_input->s );
Gildas Bazin's avatar
 
Gildas Bazin committed
273 274 275 276 277

        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. */
278
        if( i_pos % p_sys->frame_size > 0 )
Gildas Bazin's avatar
 
Gildas Bazin committed
279
        {
280 281 282 283 284 285 286
            i_pos += p_sys->frame_size - i_pos % p_sys->frame_size;

            if( stream_Seek( p_input->s, i_pos ) )
            {
                msg_Warn( p_input, "cannot resynch after seek (EOF?)" );
                return -1;
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
287 288 289 290 291
        }
    }

    /* Call the pace control */
    input_ClockManageRef( p_input, p_input->stream.p_selected_program,
292
                          p_sys->i_pcr );
Gildas Bazin's avatar
 
Gildas Bazin committed
293

294
    if( ( p_block = stream_Block( p_input->s, p_sys->frame_size ) ) == NULL )
Gildas Bazin's avatar
 
Gildas Bazin committed
295
    {
296 297
        /* EOF */
        return 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
298 299
    }

300
    es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE,
301
                    p_sys->p_es_audio, &b_audio );
302
    es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE,
303 304 305 306 307 308 309
                    p_sys->p_es_video, &b_video );

    p_block->i_dts =
    p_block->i_pts = input_ClockGetTS( p_input,
                                       p_input->stream.p_selected_program,
                                       p_sys->i_pcr );
    if( b_audio && b_video )
Gildas Bazin's avatar
 
Gildas Bazin committed
310
    {
311 312 313 314
        block_t *p_dup = block_Duplicate( p_block );

        es_out_Send( p_input->p_es_out, p_sys->p_es_video, p_block );
        if( p_dup )
Gildas Bazin's avatar
 
Gildas Bazin committed
315
        {
316
            es_out_Send( p_input->p_es_out, p_sys->p_es_video, p_dup );
Gildas Bazin's avatar
 
Gildas Bazin committed
317 318
        }
    }
319
    else if( b_audio )
Gildas Bazin's avatar
 
Gildas Bazin committed
320
    {
321 322 323 324 325 326 327 328 329
        es_out_Send( p_input->p_es_out, p_sys->p_es_audio, p_block );
    }
    else if( b_video )
    {
        es_out_Send( p_input->p_es_out, p_sys->p_es_video, p_block );
    }
    else
    {
        block_Release( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
330
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
331

332
    p_sys->i_pcr += ( 90000 / p_sys->f_rate );
Gildas Bazin's avatar
 
Gildas Bazin committed
333

Gildas Bazin's avatar
 
Gildas Bazin committed
334 335
    return 1;
}
336