voc.c 10.1 KB
Newer Older
1 2 3 4 5 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 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 206 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 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 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 290 291 292 293 294 295 296 297 298 299 300 301 302
/*****************************************************************************
 * voc.c : Creative Voice File (.VOC) demux module for vlc
 *****************************************************************************
 * Copyright (C) 2005 VideoLAN
 * $Id: xa.c 10182 2005-03-07 15:04:53Z courmisch $
 *
 * Authors: Remi Denis-Courmont <rem # via.ecp.fr>
 *
 * 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 <vlc/vlc.h>
#include <vlc/input.h>
#include <vlc/aout.h>

#include <codecs.h>

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

vlc_module_begin();
    set_description( _("VOC demuxer") );
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_DEMUX );
    set_capability( "demux2", 10 );
    set_callbacks( Open, Close );
vlc_module_end();

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

struct demux_sys_t
{
    es_format_t     fmt;
    es_out_id_t     *p_es;

#if 0
    int64_t         i_data_offset;
    unsigned int    i_data_size;
#endif

    int32_t         i_block_size;

    date_t          pts;
};

static const char ct_header[] = "Creative Voice File\x1a";

/*****************************************************************************
 * Open: check file and initializes structures
 *****************************************************************************/
static int Open( vlc_object_t * p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
    uint8_t     *p_buf, buf[8];
    uint16_t    i_data_offset, i_version;

    if( stream_Peek( p_demux->s, &p_buf, 26 ) < 26 )
        return VLC_EGENERIC;

    if( memcmp( p_buf, ct_header, 20 ) )
        return VLC_EGENERIC;
    p_buf += 20;

    i_data_offset = GetWLE( p_buf );
    if ( i_data_offset < 26 /* not enough room for full VOC header */ )
        return VLC_EGENERIC;
    p_buf += 2;

    i_version = GetWLE( p_buf );
    if( ( i_version != 0x10A ) && ( i_version != 0x114 ) )
        return VLC_EGENERIC; /* unknown VOC version */
    p_buf += 2;

    if( GetWLE( p_buf ) != (uint16_t)(0x1234 + ~i_version) )
        return VLC_EGENERIC;

    /* We have a valid VOC header */
    msg_Dbg( p_demux, "CT Voice file v%d.%d", i_version >> 8,
             i_version & 0xff );

    /* skip VOC header */
    if( stream_Read( p_demux->s, NULL, i_data_offset ) < i_data_offset )
        return VLC_EGENERIC;

    /* read first block header */
    /* FIXME: we only support one-block files at the moment */
    if( stream_Read( p_demux->s, buf, 4 ) < 4 )
        return VLC_EGENERIC;

    p_demux->pf_demux   = Demux;
    p_demux->pf_control = Control;
    p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );

    p_sys->i_block_size = GetDWLE( buf ) >> 8;
    msg_Dbg( p_demux, "block size: %u bytes", p_sys->i_block_size );

    es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );

    switch( *buf )
    {
        case 1:
            if( p_sys->i_block_size < 2 )
                goto error;
            p_sys->i_block_size -= 2;

            if( stream_Read( p_demux->s, buf, 2 ) < 2 )
                goto error;

            p_sys->fmt.audio.i_rate = 1000000L / (256L - buf[0]);
            if( buf[1] )
            {
                msg_Err( p_demux, "Unsupported compression" );
                goto error;
            }

            p_sys->fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
            p_sys->fmt.audio.i_bytes_per_frame = 1;
            p_sys->fmt.audio.i_frame_length = 1;
            p_sys->fmt.audio.i_channels = 1;
            p_sys->fmt.audio.i_blockalign = 1;
            p_sys->fmt.audio.i_bitspersample = 8;
            p_sys->fmt.i_bitrate = p_sys->fmt.audio.i_rate * 8;
            break;

        case 9:
            if( p_sys->i_block_size < 12 )
                goto error;
            p_sys->i_block_size -= 12;

            if( ( stream_Read( p_demux->s, buf, 8 ) < 8 )
             || ( stream_Read( p_demux->s, NULL, 4 ) < 4 ) )
                goto error;

            p_sys->fmt.audio.i_rate = GetDWLE( buf );
            p_sys->fmt.audio.i_bitspersample = buf[4];
            p_sys->fmt.audio.i_channels = buf[5];

            switch( GetWLE( &buf[6] ) ) /* format */
            {
                case 0x0000: /* PCM */
                    switch( p_sys->fmt.audio.i_bitspersample )
                    {
                        case 8:
                            p_sys->fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
                            break;

                        case 16:
                            p_sys->fmt.i_codec = VLC_FOURCC('u','1','6','l');
                            break;

                        default:
                            msg_Err( p_demux, "Unsupported bit res.: %u bits",
                                     p_sys->fmt.audio.i_bitspersample );
                            return VLC_EGENERIC;
                    }
                    break;

                case 0x0004: /* signed */
                    switch( p_sys->fmt.audio.i_bitspersample )
                    {
                        case 8:
                            p_sys->fmt.i_codec = VLC_FOURCC('s','8',' ',' ');
                            break;

                        case 16:
                            p_sys->fmt.i_codec = VLC_FOURCC('s','1','6','l');
                            break;

                        default:
                            msg_Err( p_demux, "Unsupported bit res.: %u bits",
                                     p_sys->fmt.audio.i_bitspersample );
                            return VLC_EGENERIC;
                    }
                    break;

                default: 
                    msg_Err( p_demux, "Unsupported compression" );
                    goto error;
            }

            p_sys->fmt.audio.i_bytes_per_frame = p_sys->fmt.audio.i_channels
                * (p_sys->fmt.audio.i_bitspersample / 8);
            p_sys->fmt.audio.i_frame_length = 1;
            p_sys->fmt.audio.i_blockalign = p_sys->fmt.audio.i_bytes_per_frame;
            p_sys->fmt.i_bitrate = 8 * p_sys->fmt.audio.i_rate
                                     * p_sys->fmt.audio.i_bytes_per_frame;
            break;

        default:
            msg_Dbg( p_demux, "Unsupported block type %u", (unsigned)*p_buf);
            return VLC_EGENERIC;
    }
    p_sys->fmt.i_extra = 0;
    p_sys->fmt.p_extra = NULL;

    msg_Dbg( p_demux, "fourcc: %4.4s, channels: %d, "
             "freq: %d Hz, bitrate: %dKo/s, blockalign: %d, bits/samples: %d",
             (char *)&p_sys->fmt.i_codec, p_sys->fmt.audio.i_channels,
             p_sys->fmt.audio.i_rate, p_sys->fmt.i_bitrate / 8192,
             p_sys->fmt.audio.i_blockalign, p_sys->fmt.audio.i_bitspersample
             );

    p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );

    date_Init( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
    date_Set( &p_sys->pts, 1 );

    return VLC_SUCCESS;

error:
    msg_Err( p_demux, "Unsupported or malformatted file" );
    free( p_sys );
    return VLC_EGENERIC;
}

/*****************************************************************************
 * Demux: read packet and send them to decoders
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
static int Demux( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    block_t     *p_block;
#if 0
    int64_t     i_offset;

    i_offset = stream_Tell( p_demux->s );

    if( p_sys->i_data_size > 0 &&
        i_offset >= p_sys->i_data_offset + p_sys->i_data_size )
    {
        /* EOF */
        return 0;
    }
#endif
    p_block = stream_Block( p_demux->s, p_sys->fmt.audio.i_bytes_per_frame );
    if( p_block == NULL )
    {
        msg_Warn( p_demux, "cannot read data" );
        return 0;
    }

    p_block->i_dts = p_block->i_pts =
        date_Increment( &p_sys->pts, p_sys->fmt.audio.i_frame_length );

    es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );

    es_out_Send( p_demux->out, p_sys->p_es, p_block );

    return 1;
}

/*****************************************************************************
 * Close: frees unused data
 *****************************************************************************/
static void Close ( vlc_object_t * p_this )
{
    demux_sys_t *p_sys  = ((demux_t *)p_this)->p_sys;

    free( p_sys );
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
    demux_sys_t *p_sys  = p_demux->p_sys;

    if( i_query == DEMUX_SET_POSITION )
        return VLC_EGENERIC; /* not implemented yet */

    return demux2_vaControlHelper( p_demux->s, 0, -1, p_sys->fmt.i_bitrate,
                                   p_sys->fmt.audio.i_blockalign,
                                   i_query, args );
}