rawdv.c 10.1 KB
Newer Older
Gildas Bazin's avatar
 
Gildas Bazin committed
1
/*****************************************************************************
2
 * rawdv.c : raw DV input module for vlc
Gildas Bazin's avatar
 
Gildas Bazin committed
3
 *****************************************************************************
4
 * Copyright (C) 2001-2007 the VideoLAN team
5
 * $Id$
Gildas Bazin's avatar
 
Gildas Bazin committed
6 7
 *
 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8
 *          Paul Corke <paul dot corke at datatote dot co dot uk>
Gildas Bazin's avatar
 
Gildas Bazin committed
9 10 11 12 13
 *
 * 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.
14
 *
Gildas Bazin's avatar
 
Gildas Bazin committed
15 16 17 18 19 20 21
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Gildas Bazin's avatar
 
Gildas Bazin committed
23 24 25 26 27 28
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

29 30 31 32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <vlc_common.h>
34
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
35
#include <vlc_demux.h>
Gildas Bazin's avatar
 
Gildas Bazin committed
36

37 38
#include "rawdv.h"

39 40 41
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
42 43 44 45
#define HURRYUP_TEXT N_( "Hurry up" )
#define HURRYUP_LONGTEXT N_( "The demuxer will advance timestamps if the " \
                "input can't keep up with the rate." )

46 47 48
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

49 50 51 52 53 54
vlc_module_begin ()
    set_shortname( "DV" )
    set_description( N_("DV (Digital Video) demuxer") )
    set_capability( "demux", 3 )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_DEMUX )
55
    add_bool( "rawdv-hurry-up", false, HURRYUP_TEXT, HURRYUP_LONGTEXT, false )
56 57 58
    set_callbacks( Open, Close )
    add_shortcut( "rawdv" )
vlc_module_end ()
Gildas Bazin's avatar
 
Gildas Bazin committed
59 60 61 62 63 64 65


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

Gildas Bazin's avatar
 
Gildas Bazin committed
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 92 93 94 95 96 97 98 99
 - 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.

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

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

100 101
    es_out_id_t *p_es_video;
    es_format_t  fmt_video;
Gildas Bazin's avatar
 
Gildas Bazin committed
102

103 104
    es_out_id_t *p_es_audio;
    es_format_t  fmt_audio;
Gildas Bazin's avatar
 
Gildas Bazin committed
105

Gildas Bazin's avatar
 
Gildas Bazin committed
106
    int    i_dsf;
Gildas Bazin's avatar
 
Gildas Bazin committed
107 108 109 110 111
    double f_rate;
    int    i_bitrate;

    /* program clock reference (in units of 90kHz) */
    mtime_t i_pcr;
112
    bool b_hurry_up;
Gildas Bazin's avatar
 
Gildas Bazin committed
113 114 115 116 117
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
118 119 120
static int Demux( demux_t * );
static int Control( demux_t *, int i_query, va_list args );

Gildas Bazin's avatar
 
Gildas Bazin committed
121
/*****************************************************************************
122
 * Open: initializes raw DV demux structures
Gildas Bazin's avatar
 
Gildas Bazin committed
123
 *****************************************************************************/
124
static int Open( vlc_object_t * p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
125
{
126 127
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
128

129
    const uint8_t *p_peek, *p_peek_backup;
130

131 132 133
    uint32_t    i_dword;
    dv_header_t dv_header;
    dv_id_t     dv_id;
Gildas Bazin's avatar
 
Gildas Bazin committed
134

135 136
    /* 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
Gildas Bazin's avatar
 
Gildas Bazin committed
137 138 139
     * we'll rely on the file extension to trigger this demux. Alternatively,
     * it is possible to force this demux. */

140
    /* Check for DV file extension */
141
    if( !demux_IsPathExtension( p_demux, ".dv" ) && !p_demux->b_force )
142
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
143

144
    if( stream_Peek( p_demux->s, &p_peek, DV_PAL_FRAME_SIZE ) <
Gildas Bazin's avatar
 
Gildas Bazin committed
145
        DV_NTSC_FRAME_SIZE )
Gildas Bazin's avatar
 
Gildas Bazin committed
146 147
    {
        /* Stream too short ... */
148
        msg_Err( p_demux, "cannot peek()" );
149
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
150
    }
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

    /* 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 )
    {
166
        msg_Warn( p_demux, "not a raw DV stream header" );
167
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
168 169 170 171 172 173 174
    }

    /* fill in the dv_header_t structure */
    dv_header.dsf = i_dword >> (32 - 1);
    i_dword <<= 1;
    if( i_dword >> (32 - 1) ) /* incorrect bit */
    {
175
        msg_Warn( p_demux, "incorrect bit" );
176
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
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 ) );
201 202 203 204 205
    if( !p_sys )
        return VLC_ENOMEM;

    p_sys->b_hurry_up = var_CreateGetBool( p_demux, "rawdv-hurry-up" );
    msg_Dbg( p_demux, "Realtime DV Source: %s", (p_sys->b_hurry_up)?"Yes":"No" );
Gildas Bazin's avatar
 
Gildas Bazin committed
206

Gildas Bazin's avatar
 
Gildas Bazin committed
207
    p_sys->i_dsf = dv_header.dsf;
208 209
    p_sys->frame_size = dv_header.dsf ? DV_PAL_FRAME_SIZE
                                      : DV_NTSC_FRAME_SIZE;
210 211
    p_sys->f_rate = dv_header.dsf ? 25 : 29.97;

212
    p_sys->i_pcr = 0;
213 214 215 216 217
    p_sys->p_es_video = NULL;
    p_sys->p_es_audio = NULL;

    p_sys->i_bitrate = 0;

218
    es_format_Init( &p_sys->fmt_video, VIDEO_ES, VLC_CODEC_DV );
219 220
    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
221

222 223
    /* FIXME FIXME */
#if 0
Gildas Bazin's avatar
 
Gildas Bazin committed
224 225
    /* necessary because input_SplitBuffer() will only get
     * INPUT_DEFAULT_BUFSIZE bytes at a time. */
226
    p_input->i_bufsize = p_sys->frame_size;
227
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
228

229
    p_sys->p_es_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
Gildas Bazin's avatar
 
Gildas Bazin committed
230 231 232 233 234

    /* Audio stuff */
    p_peek = p_peek_backup + 80*6+80*16*3 + 3; /* beginning of AAUX pack */
    if( *p_peek == 0x50 )
    {
235
        dv_get_audio_format( &p_sys->fmt_audio, &p_peek[1] );
236
        p_sys->p_es_audio = es_out_Add( p_demux->out, &p_sys->fmt_audio );
Gildas Bazin's avatar
 
Gildas Bazin committed
237
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
238

239
    return VLC_SUCCESS;
Gildas Bazin's avatar
 
Gildas Bazin committed
240 241 242
}

/*****************************************************************************
243
 * Close: frees unused data
Gildas Bazin's avatar
 
Gildas Bazin committed
244
 *****************************************************************************/
245
static void Close( vlc_object_t *p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
246
{
247 248
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys  = p_demux->p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
249

250
    var_Destroy( p_demux, "rawdv-hurry-up");
251
    free( p_sys );
Gildas Bazin's avatar
 
Gildas Bazin committed
252 253 254 255 256 257 258
}

/*****************************************************************************
 * Demux: reads and demuxes data packets
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
259
static int Demux( demux_t *p_demux )
Gildas Bazin's avatar
 
Gildas Bazin committed
260
{
261 262
    demux_sys_t *p_sys  = p_demux->p_sys;
    block_t     *p_block;
263
    bool  b_audio = false;
Gildas Bazin's avatar
 
Gildas Bazin committed
264

265 266 267 268 269 270
    if( p_sys->b_hurry_up )
    {
         /* 3 frames */
        p_sys->i_pcr = mdate() + (p_sys->i_dsf ? 120000 : 90000);
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
271
    /* Call the pace control */
272
    es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
273 274
    p_block = stream_Block( p_demux->s, p_sys->frame_size );
    if( p_block == NULL )
Gildas Bazin's avatar
 
Gildas Bazin committed
275
    {
276 277
        /* EOF */
        return 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
278 279
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
280 281
    if( p_sys->p_es_audio )
    {
282
        es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
Gildas Bazin's avatar
 
Gildas Bazin committed
283 284 285
                        p_sys->p_es_audio, &b_audio );
    }

286
    p_block->i_dts =
287
    p_block->i_pts = VLC_TS_0 + p_sys->i_pcr;
288

Gildas Bazin's avatar
 
Gildas Bazin committed
289
    if( b_audio )
Gildas Bazin's avatar
 
Gildas Bazin committed
290
    {
291
        block_t *p_audio_block = dv_extract_audio( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
292
        if( p_audio_block )
293
            es_out_Send( p_demux->out, p_sys->p_es_audio, p_audio_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
294
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
295

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

298 299
    if( !p_sys->b_hurry_up )
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
300
        p_sys->i_pcr += ( INT64_C(1000000) / p_sys->f_rate );
301
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
302

Gildas Bazin's avatar
 
Gildas Bazin committed
303 304
    return 1;
}
305

306 307 308 309 310 311 312 313
/*****************************************************************************
 * 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 */
314
    return demux_vaControlHelper( p_demux->s,
315 316 317 318 319
                                   0, -1,
                                   p_sys->frame_size * p_sys->f_rate * 8,
                                   p_sys->frame_size, i_query, args );
}