dts.c 8.94 KB
Newer Older
gbazin's avatar
 
gbazin committed
1 2 3
/*****************************************************************************
 * dts.c : raw DTS stream input module for vlc
 *****************************************************************************
4
 * Copyright (C) 2001-2007 the VideoLAN team
5
 * $Id$
gbazin's avatar
 
gbazin committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
gbazin's avatar
 
gbazin committed
22 23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <vlc/vlc.h>
zorglub's avatar
zorglub committed
28
#include <vlc_demux.h>
gbazin's avatar
 
gbazin committed
29 30 31
#include <vlc_codec.h>

/*****************************************************************************
32
 * Module descriptor
gbazin's avatar
 
gbazin committed
33 34 35 36
 *****************************************************************************/
static int  Open  ( vlc_object_t * );
static void Close ( vlc_object_t * );

37
vlc_module_begin();
zorglub's avatar
zorglub committed
38 39
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_DEMUX );
40 41 42 43 44 45 46 47 48 49 50
    set_description( _("Raw DTS demuxer") );
    set_capability( "demux2", 155 );
    set_callbacks( Open, Close );
    add_shortcut( "dts" );
vlc_module_end();

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int Demux  ( demux_t * );
static int Control( demux_t *, int, va_list );
gbazin's avatar
 
gbazin committed
51

gbazin's avatar
 
gbazin committed
52 53 54 55 56 57 58
struct demux_sys_t
{
    vlc_bool_t  b_start;
    es_out_id_t *p_es;

    /* Packetizer */
    decoder_t *p_packetizer;
gbazin's avatar
 
gbazin committed
59 60

    int i_mux_rate;
gbazin's avatar
 
gbazin committed
61 62
};

63
static int CheckSync( const uint8_t *p_peek );
gbazin's avatar
 
gbazin committed
64

65 66 67
#define DTS_PACKET_SIZE 16384
#define DTS_PROBE_SIZE (DTS_PACKET_SIZE * 4)
#define DTS_MAX_HEADER_SIZE 11
gbazin's avatar
 
gbazin committed
68

gbazin's avatar
 
gbazin committed
69 70 71 72 73
/*****************************************************************************
 * Open: initializes ES structures
 *****************************************************************************/
static int Open( vlc_object_t * p_this )
{
74 75
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
76
    const byte_t *p_peek;
77
    int          i_peek = 0;
gbazin's avatar
 
gbazin committed
78

gbazin's avatar
 
gbazin committed
79
    /* Check if we are dealing with a WAV file */
80
    if( stream_Peek( p_demux->s, &p_peek, 20 ) == 20 &&
81
        !memcmp( p_peek, "RIFF", 4 ) && !memcmp( &p_peek[8], "WAVE", 4 ) )
gbazin's avatar
 
gbazin committed
82
    {
gbazin's avatar
 
gbazin committed
83 84 85 86
        int i_size;

        /* Find the wave format header */
        i_peek = 20;
87
        while( memcmp( p_peek + i_peek - 8, "fmt ", 4 ) )
gbazin's avatar
 
gbazin committed
88
        {
gbazin's avatar
 
gbazin committed
89 90 91 92
            i_size = GetDWLE( p_peek + i_peek - 4 );
            if( i_size + i_peek > DTS_PROBE_SIZE ) return VLC_EGENERIC;
            i_peek += i_size + 8;

93
            if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
gbazin's avatar
 
gbazin committed
94
                return VLC_EGENERIC;
gbazin's avatar
 
gbazin committed
95
        }
gbazin's avatar
 
gbazin committed
96

gbazin's avatar
 
gbazin committed
97 98 99 100
        /* Sanity check the wave format header */
        i_size = GetDWLE( p_peek + i_peek - 4 );
        if( i_size + i_peek > DTS_PROBE_SIZE ) return VLC_EGENERIC;
        i_peek += i_size + 8;
101
        if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
gbazin's avatar
 
gbazin committed
102 103 104 105 106 107 108 109 110 111 112
            return VLC_EGENERIC;
        if( GetWLE( p_peek + i_peek - i_size - 8 /* wFormatTag */ ) !=
            1 /* WAVE_FORMAT_PCM */ )
            return VLC_EGENERIC;
        if( GetWLE( p_peek + i_peek - i_size - 6 /* nChannels */ ) != 2 )
            return VLC_EGENERIC;
        if( GetDWLE( p_peek + i_peek - i_size - 4 /* nSamplesPerSec */ ) !=
            44100 )
            return VLC_EGENERIC;

        /* Skip the wave header */
113
        while( memcmp( p_peek + i_peek - 8, "data", 4 ) )
gbazin's avatar
 
gbazin committed
114 115 116 117 118
        {
            i_size = GetDWLE( p_peek + i_peek - 4 );
            if( i_size + i_peek > DTS_PROBE_SIZE ) return VLC_EGENERIC;
            i_peek += i_size + 8;

119
            if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
gbazin's avatar
 
gbazin committed
120 121
                return VLC_EGENERIC;
        }
gbazin's avatar
 
gbazin committed
122 123 124

        /* Some DTS wav files don't begin with a sync code so we do a more
         * extensive search */
125
        i_size = stream_Peek( p_demux->s, &p_peek, DTS_PROBE_SIZE );
gbazin's avatar
 
gbazin committed
126 127 128
        i_size -= DTS_MAX_HEADER_SIZE;

        while( i_peek < i_size )
gbazin's avatar
 
gbazin committed
129
        {
gbazin's avatar
 
gbazin committed
130 131 132 133 134
            if( CheckSync( p_peek + i_peek ) != VLC_SUCCESS )
                /* The data is stored in 16 bits words */
                i_peek += 2;
            else
                break;
gbazin's avatar
 
gbazin committed
135
        }
gbazin's avatar
 
gbazin committed
136 137
    }

gbazin's avatar
 
gbazin committed
138
    /* Have a peep at the show. */
139
    CHECK_PEEK( p_peek, i_peek + DTS_MAX_HEADER_SIZE * 2  );
gbazin's avatar
 
gbazin committed
140

gbazin's avatar
 
gbazin committed
141
    if( CheckSync( p_peek + i_peek ) != VLC_SUCCESS )
gbazin's avatar
 
gbazin committed
142
    {
143
        if( !p_demux->b_force )
gbazin's avatar
 
gbazin committed
144
            return VLC_EGENERIC;
145

146 147 148
        /* User forced */
        msg_Err( p_demux, "this doesn't look like a DTS audio stream, "
                 "continuing anyway" );
gbazin's avatar
 
gbazin committed
149 150
    }

151
    DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
152
 
zorglub's avatar
zorglub committed
153 154
    INIT_APACKETIZER( p_sys->p_packetizer, 'd','t','s',' ' );
    LOAD_PACKETIZER_OR_FAIL( p_sys->p_packetizer, "DTS" );
gbazin's avatar
 
gbazin committed
155

156
    p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_in );
gbazin's avatar
 
gbazin committed
157 158 159 160 161 162 163

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: frees unused data
 *****************************************************************************/
164
static void Close( vlc_object_t *p_this )
gbazin's avatar
 
gbazin committed
165
{
166 167
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys = p_demux->p_sys;
gbazin's avatar
 
gbazin committed
168

169
    DESTROY_PACKETIZER( p_sys->p_packetizer );
gbazin's avatar
 
gbazin committed
170 171 172 173 174 175 176 177 178

    free( p_sys );
}

/*****************************************************************************
 * Demux: reads and demuxes data packets
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
179
static int Demux( demux_t *p_demux )
gbazin's avatar
 
gbazin committed
180
{
181
    demux_sys_t *p_sys = p_demux->p_sys;
gbazin's avatar
 
gbazin committed
182 183
    block_t *p_block_in, *p_block_out;

184
    if( !( p_block_in = stream_Block( p_demux->s, DTS_PACKET_SIZE ) ) )
gbazin's avatar
 
gbazin committed
185 186 187 188 189 190 191 192 193 194 195 196
    {
        return 0;
    }

    if( p_sys->b_start )
        p_block_in->i_pts = p_block_in->i_dts = 1;
    else
        p_block_in->i_pts = p_block_in->i_dts = 0;

    while( (p_block_out = p_sys->p_packetizer->pf_packetize(
                p_sys->p_packetizer, &p_block_in )) )
    {
gbazin's avatar
 
gbazin committed
197 198
        p_sys->b_start = VLC_FALSE;

gbazin's avatar
 
gbazin committed
199 200 201 202
        while( p_block_out )
        {
            block_t *p_next = p_block_out->p_next;

gbazin's avatar
 
gbazin committed
203 204
            /* We assume a constant bitrate */
            if( p_block_out->i_length )
205 206 207 208
            {
                p_sys->i_mux_rate =
                    p_block_out->i_buffer * I64C(1000000) / p_block_out->i_length;
            }
gbazin's avatar
 
gbazin committed
209

210 211
            /* set PCR */
            es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
gbazin's avatar
 
gbazin committed
212

213
            es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
gbazin's avatar
 
gbazin committed
214 215 216 217 218 219 220

            p_block_out = p_next;
        }
    }

    return 1;
}
gbazin's avatar
 
gbazin committed
221 222 223 224

/*****************************************************************************
 * Control:
 *****************************************************************************/
225
static int Control( demux_t *p_demux, int i_query, va_list args )
gbazin's avatar
 
gbazin committed
226
{
227
    demux_sys_t *p_sys  = p_demux->p_sys;
228 229 230 231 232 233
    if( i_query == DEMUX_SET_TIME )
        return VLC_EGENERIC;
    else
        return demux2_vaControlHelper( p_demux->s,
                                       0, -1,
                                       8*p_sys->i_mux_rate, 1, i_query, args );
234
}
gbazin's avatar
 
gbazin committed
235

236 237 238
/*****************************************************************************
 * CheckSync: Check if buffer starts with a DTS sync code
 *****************************************************************************/
239
static int CheckSync( const uint8_t *p_peek )
240 241 242 243 244
{
    /* 14 bits, little endian version of the bitstream */
    if( p_peek[0] == 0xff && p_peek[1] == 0x1f &&
        p_peek[2] == 0x00 && p_peek[3] == 0xe8 &&
        (p_peek[4] & 0xf0) == 0xf0 && p_peek[5] == 0x07 )
gbazin's avatar
 
gbazin committed
245
    {
246 247 248 249 250 251 252 253
        return VLC_SUCCESS;
    }
    /* 14 bits, big endian version of the bitstream */
    else if( p_peek[0] == 0x1f && p_peek[1] == 0xff &&
             p_peek[2] == 0xe8 && p_peek[3] == 0x00 &&
             p_peek[4] == 0x07 && (p_peek[5] & 0xf0) == 0xf0)
    {
        return VLC_SUCCESS;
gbazin's avatar
 
gbazin committed
254
    }
255 256 257 258 259 260 261 262 263 264 265 266 267 268
    /* 16 bits, big endian version of the bitstream */
    else if( p_peek[0] == 0x7f && p_peek[1] == 0xfe &&
             p_peek[2] == 0x80 && p_peek[3] == 0x01 )
    {
        return VLC_SUCCESS;
    }
    /* 16 bits, little endian version of the bitstream */
    else if( p_peek[0] == 0xfe && p_peek[1] == 0x7f &&
             p_peek[2] == 0x01 && p_peek[3] == 0x80 )
    {
        return VLC_SUCCESS;
    }

    return VLC_EGENERIC;
gbazin's avatar
 
gbazin committed
269
}
270