dts.c 8.97 KB
Newer Older
Gildas Bazin's avatar
 
Gildas Bazin committed
1 2 3
/*****************************************************************************
 * dts.c : raw DTS stream input module for vlc
 *****************************************************************************
4
 * Copyright (C) 2001-2007 the VideoLAN team
5
 * $Id$
Gildas Bazin's avatar
 
Gildas Bazin 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
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Gildas Bazin's avatar
 
Gildas Bazin committed
22 23 24 25 26
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
27 28 29 30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

Gildas Bazin's avatar
 
Gildas Bazin committed
31
#include <vlc/vlc.h>
Clément Stenac's avatar
Clément Stenac committed
32
#include <vlc_demux.h>
Gildas Bazin's avatar
 
Gildas Bazin committed
33 34 35
#include <vlc_codec.h>

/*****************************************************************************
36
 * Module descriptor
Gildas Bazin's avatar
 
Gildas Bazin committed
37 38 39 40
 *****************************************************************************/
static int  Open  ( vlc_object_t * );
static void Close ( vlc_object_t * );

41
vlc_module_begin();
Clément Stenac's avatar
Clément Stenac committed
42 43
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_DEMUX );
44
    set_description( _("Raw DTS demuxer") );
45
    set_capability( "demux", 155 );
46 47 48 49 50 51 52 53 54
    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 );
Gildas Bazin's avatar
 
Gildas Bazin committed
55

Gildas Bazin's avatar
 
Gildas Bazin committed
56 57
struct demux_sys_t
{
58
    bool  b_start;
Gildas Bazin's avatar
 
Gildas Bazin committed
59 60 61 62
    es_out_id_t *p_es;

    /* Packetizer */
    decoder_t *p_packetizer;
Gildas Bazin's avatar
 
Gildas Bazin committed
63 64

    int i_mux_rate;
Gildas Bazin's avatar
 
Gildas Bazin committed
65 66
};

67
static int CheckSync( const uint8_t *p_peek );
Gildas Bazin's avatar
 
Gildas Bazin committed
68

69 70 71
#define DTS_PACKET_SIZE 16384
#define DTS_PROBE_SIZE (DTS_PACKET_SIZE * 4)
#define DTS_MAX_HEADER_SIZE 11
Gildas Bazin's avatar
 
Gildas Bazin committed
72

Gildas Bazin's avatar
 
Gildas Bazin committed
73 74 75 76 77
/*****************************************************************************
 * Open: initializes ES structures
 *****************************************************************************/
static int Open( vlc_object_t * p_this )
{
78 79
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
80
    const byte_t *p_peek;
81
    int          i_peek = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
82

Gildas Bazin's avatar
 
Gildas Bazin committed
83
    /* Check if we are dealing with a WAV file */
84
    if( stream_Peek( p_demux->s, &p_peek, 20 ) == 20 &&
85
        !memcmp( p_peek, "RIFF", 4 ) && !memcmp( &p_peek[8], "WAVE", 4 ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
86
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
87 88 89 90
        int i_size;

        /* Find the wave format header */
        i_peek = 20;
91
        while( memcmp( p_peek + i_peek - 8, "fmt ", 4 ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
92
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
93 94 95 96
            i_size = GetDWLE( p_peek + i_peek - 4 );
            if( i_size + i_peek > DTS_PROBE_SIZE ) return VLC_EGENERIC;
            i_peek += i_size + 8;

97
            if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
Gildas Bazin's avatar
 
Gildas Bazin committed
98
                return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
99
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
100

Gildas Bazin's avatar
 
Gildas Bazin committed
101 102 103 104
        /* 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;
105
        if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
Gildas Bazin's avatar
 
Gildas Bazin committed
106 107 108 109 110 111 112 113 114 115 116
            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 */
117
        while( memcmp( p_peek + i_peek - 8, "data", 4 ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
118 119 120 121 122
        {
            i_size = GetDWLE( p_peek + i_peek - 4 );
            if( i_size + i_peek > DTS_PROBE_SIZE ) return VLC_EGENERIC;
            i_peek += i_size + 8;

123
            if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
Gildas Bazin's avatar
 
Gildas Bazin committed
124 125
                return VLC_EGENERIC;
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
126 127 128

        /* Some DTS wav files don't begin with a sync code so we do a more
         * extensive search */
129
        i_size = stream_Peek( p_demux->s, &p_peek, DTS_PROBE_SIZE );
Gildas Bazin's avatar
 
Gildas Bazin committed
130 131 132
        i_size -= DTS_MAX_HEADER_SIZE;

        while( i_peek < i_size )
Gildas Bazin's avatar
 
Gildas Bazin committed
133
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
134 135 136 137 138
            if( CheckSync( p_peek + i_peek ) != VLC_SUCCESS )
                /* The data is stored in 16 bits words */
                i_peek += 2;
            else
                break;
Gildas Bazin's avatar
 
Gildas Bazin committed
139
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
140 141
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
142
    /* Have a peep at the show. */
143
    CHECK_PEEK( p_peek, i_peek + DTS_MAX_HEADER_SIZE * 2  );
Gildas Bazin's avatar
 
Gildas Bazin committed
144

Gildas Bazin's avatar
 
Gildas Bazin committed
145
    if( CheckSync( p_peek + i_peek ) != VLC_SUCCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
146
    {
147
        if( !p_demux->b_force )
Gildas Bazin's avatar
 
Gildas Bazin committed
148
            return VLC_EGENERIC;
149

150 151 152
        /* User forced */
        msg_Err( p_demux, "this doesn't look like a DTS audio stream, "
                 "continuing anyway" );
Gildas Bazin's avatar
 
Gildas Bazin committed
153 154
    }

155
    DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
156
 
Clément Stenac's avatar
Clément Stenac committed
157 158
    INIT_APACKETIZER( p_sys->p_packetizer, 'd','t','s',' ' );
    LOAD_PACKETIZER_OR_FAIL( p_sys->p_packetizer, "DTS" );
Gildas Bazin's avatar
 
Gildas Bazin committed
159

160
    p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_in );
Gildas Bazin's avatar
 
Gildas Bazin committed
161 162 163 164 165 166 167

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: frees unused data
 *****************************************************************************/
168
static void Close( vlc_object_t *p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
169
{
170 171
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys = p_demux->p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
172

173
    DESTROY_PACKETIZER( p_sys->p_packetizer );
Gildas Bazin's avatar
 
Gildas Bazin committed
174 175 176 177 178 179 180 181 182

    free( p_sys );
}

/*****************************************************************************
 * Demux: reads and demuxes data packets
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
183
static int Demux( demux_t *p_demux )
Gildas Bazin's avatar
 
Gildas Bazin committed
184
{
185
    demux_sys_t *p_sys = p_demux->p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
186 187
    block_t *p_block_in, *p_block_out;

188
    if( !( p_block_in = stream_Block( p_demux->s, DTS_PACKET_SIZE ) ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
189 190 191 192 193 194 195 196 197 198 199 200
    {
        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 )) )
    {
201
        p_sys->b_start = false;
Gildas Bazin's avatar
 
Gildas Bazin committed
202

Gildas Bazin's avatar
 
Gildas Bazin committed
203 204 205 206
        while( p_block_out )
        {
            block_t *p_next = p_block_out->p_next;

Gildas Bazin's avatar
 
Gildas Bazin committed
207 208
            /* We assume a constant bitrate */
            if( p_block_out->i_length )
209 210 211 212
            {
                p_sys->i_mux_rate =
                    p_block_out->i_buffer * I64C(1000000) / p_block_out->i_length;
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
213

214 215
            /* set PCR */
            es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
Gildas Bazin's avatar
 
Gildas Bazin committed
216

217
            es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
Gildas Bazin's avatar
 
Gildas Bazin committed
218 219 220 221 222 223 224

            p_block_out = p_next;
        }
    }

    return 1;
}
Gildas Bazin's avatar
 
Gildas Bazin committed
225 226 227 228

/*****************************************************************************
 * Control:
 *****************************************************************************/
229
static int Control( demux_t *p_demux, int i_query, va_list args )
Gildas Bazin's avatar
 
Gildas Bazin committed
230
{
231
    demux_sys_t *p_sys  = p_demux->p_sys;
232 233 234
    if( i_query == DEMUX_SET_TIME )
        return VLC_EGENERIC;
    else
235
        return demux_vaControlHelper( p_demux->s,
236 237
                                       0, -1,
                                       8*p_sys->i_mux_rate, 1, i_query, args );
238
}
Gildas Bazin's avatar
 
Gildas Bazin committed
239

240 241 242
/*****************************************************************************
 * CheckSync: Check if buffer starts with a DTS sync code
 *****************************************************************************/
243
static int CheckSync( const uint8_t *p_peek )
244 245 246 247 248
{
    /* 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 )
Gildas Bazin's avatar
 
Gildas Bazin committed
249
    {
250 251 252 253 254 255 256 257
        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;
Gildas Bazin's avatar
 
Gildas Bazin committed
258
    }
259 260 261 262 263 264 265 266 267 268 269 270 271 272
    /* 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;
Gildas Bazin's avatar
 
Gildas Bazin committed
273
}
274