aiff.c 10.1 KB
Newer Older
Laurent Aimar's avatar
Laurent Aimar committed
1 2 3
/*****************************************************************************
 * aiff.c: Audio Interchange File Format demuxer
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2004-2007 VLC authors and VideoLAN
5
 * $Id$
Laurent Aimar's avatar
Laurent Aimar committed
6 7 8
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
Laurent Aimar's avatar
Laurent Aimar committed
12 13 14 15
 * (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
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
Laurent Aimar's avatar
Laurent Aimar committed
18
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Laurent Aimar's avatar
Laurent Aimar committed
22 23 24 25 26 27
 *****************************************************************************/

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

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

32
#include <vlc_common.h>
33
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
34
#include <vlc_demux.h>
Laurent Aimar's avatar
Laurent Aimar committed
35 36 37 38 39 40 41 42 43 44 45

/* TODO:
 *  - ...
 */

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

46 47 48 49 50 51 52 53
vlc_module_begin ()
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_DEMUX )
    set_description( N_("AIFF demuxer" ) )
    set_capability( "demux", 10 )
    set_callbacks( Open, Close )
    add_shortcut( "aiff" )
vlc_module_end ()
Laurent Aimar's avatar
Laurent Aimar committed
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

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/

struct demux_sys_t
{
    es_format_t  fmt;
    es_out_id_t *es;

    int64_t     i_ssnd_pos;
    int64_t     i_ssnd_size;
    int         i_ssnd_offset;
    int         i_ssnd_blocksize;

    /* real data start */
    int64_t     i_ssnd_start;
    int64_t     i_ssnd_end;

    int         i_ssnd_fsize;

    int64_t     i_time;
};

static int Demux  ( demux_t *p_demux );
static int Control( demux_t *p_demux, int i_query, va_list args );

/* GetF80BE: read a 80 bits float in big endian */
82
static unsigned int GetF80BE( const uint8_t p[10] )
Laurent Aimar's avatar
Laurent Aimar committed
83 84 85 86 87
{
    unsigned int i_mantissa = GetDWBE( &p[2] );
    int          i_exp = 30 - p[1];
    unsigned int i_last = 0;

88
    while( i_exp-- > 0 )
Laurent Aimar's avatar
Laurent Aimar committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    {
        i_last = i_mantissa;
        i_mantissa >>= 1;
    }
    if( i_last&0x01 )
    {
        i_mantissa++;
    }
    return i_mantissa;
}

/*****************************************************************************
 * Open
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;

108
    const uint8_t *p_peek;
Laurent Aimar's avatar
Laurent Aimar committed
109

110 111
    if( stream_Peek( p_demux->s, &p_peek, 12 ) < 12 )
        return VLC_EGENERIC;
Laurent Aimar's avatar
Laurent Aimar committed
112
    if( memcmp( p_peek, "FORM", 4 ) || memcmp( &p_peek[8], "AIFF", 4 ) )
Laurent Aimar's avatar
Laurent Aimar committed
113 114 115 116 117 118
        return VLC_EGENERIC;

    /* skip aiff header */
    stream_Read( p_demux->s, NULL, 12 );

    /* Fill p_demux field */
119
    DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
Laurent Aimar's avatar
Laurent Aimar committed
120
    es_format_Init( &p_sys->fmt, UNKNOWN_ES, 0 );
121
    p_sys->i_time = 0;
Laurent Aimar's avatar
Laurent Aimar committed
122 123 124 125 126 127
    p_sys->i_ssnd_pos = -1;

    for( ;; )
    {
        uint32_t i_size;

Laurent Aimar's avatar
Laurent Aimar committed
128 129 130
        if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
            goto error;

Laurent Aimar's avatar
Laurent Aimar committed
131 132 133 134
        i_size = GetDWBE( &p_peek[4] );

        msg_Dbg( p_demux, "chunk fcc=%4.4s size=%d", p_peek, i_size );

135
        if( !memcmp( p_peek, "COMM", 4 ) )
Laurent Aimar's avatar
Laurent Aimar committed
136
        {
Laurent Aimar's avatar
Laurent Aimar committed
137 138 139
            if( stream_Peek( p_demux->s, &p_peek, 18+8 ) < 18+8 )
                goto error;

Laurent Aimar's avatar
Laurent Aimar committed
140 141 142 143 144 145
            es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
            p_sys->fmt.audio.i_channels = GetWBE( &p_peek[8] );
            p_sys->fmt.audio.i_bitspersample = GetWBE( &p_peek[14] );
            p_sys->fmt.audio.i_rate     = GetF80BE( &p_peek[16] );

            msg_Dbg( p_demux, "COMM: channels=%d samples_frames=%d bits=%d rate=%d",
146 147
                     GetWBE( &p_peek[8] ), GetDWBE( &p_peek[10] ), GetWBE( &p_peek[14] ),
                     GetF80BE( &p_peek[16] ) );
Laurent Aimar's avatar
Laurent Aimar committed
148
        }
149
        else if( !memcmp( p_peek, "SSND", 4 ) )
Laurent Aimar's avatar
Laurent Aimar committed
150
        {
Laurent Aimar's avatar
Laurent Aimar committed
151 152 153
            if( stream_Peek( p_demux->s, &p_peek, 8+8 ) < 8+8 )
                goto error;

Laurent Aimar's avatar
Laurent Aimar committed
154
            p_sys->i_ssnd_pos = stream_Tell( p_demux->s );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
155
            p_sys->i_ssnd_size = i_size;
Laurent Aimar's avatar
Laurent Aimar committed
156 157 158 159 160 161 162 163 164 165 166 167 168
            p_sys->i_ssnd_offset = GetDWBE( &p_peek[8] );
            p_sys->i_ssnd_blocksize = GetDWBE( &p_peek[12] );

            msg_Dbg( p_demux, "SSND: (offset=%d blocksize=%d)",
                     p_sys->i_ssnd_offset, p_sys->i_ssnd_blocksize );
        }
        if( p_sys->i_ssnd_pos >= 12 && p_sys->fmt.i_cat == AUDIO_ES )
        {
            /* We have found the 2 needed chunks */
            break;
        }

        /* Skip this chunk */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
169
        i_size += 8;
170 171
        if( (i_size % 2) != 0 )
            i_size++;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
172
        if( stream_Read( p_demux->s, NULL, i_size ) != (int)i_size )
Laurent Aimar's avatar
Laurent Aimar committed
173 174 175 176 177 178 179 180 181 182
        {
            msg_Warn( p_demux, "incomplete file" );
            goto error;
        }
    }

    p_sys->i_ssnd_start = p_sys->i_ssnd_pos + 16 + p_sys->i_ssnd_offset;
    p_sys->i_ssnd_end   = p_sys->i_ssnd_start + p_sys->i_ssnd_size;

    p_sys->i_ssnd_fsize = p_sys->fmt.audio.i_channels *
183
                          ((p_sys->fmt.audio.i_bitspersample + 7) / 8);
Laurent Aimar's avatar
Laurent Aimar committed
184

185
    if( p_sys->i_ssnd_fsize <= 0 || p_sys->fmt.audio.i_rate == 0 )
Laurent Aimar's avatar
Laurent Aimar committed
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
    {
        msg_Err( p_demux, "invalid audio parameters" );
        goto error;
    }

    if( p_sys->i_ssnd_size <= 0 )
    {
        /* unknown */
        p_sys->i_ssnd_end = 0;
    }

    /* seek into SSND chunk */
    if( stream_Seek( p_demux->s, p_sys->i_ssnd_start ) )
    {
        msg_Err( p_demux, "cannot seek to data chunk" );
        goto error;
    }

    /* */
    p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );

    return VLC_SUCCESS;

error:
    free( p_sys );
    return VLC_EGENERIC;
}

/*****************************************************************************
 * Close
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys = p_demux->p_sys;

    free( p_sys );
}


/*****************************************************************************
 * Demux:
 *****************************************************************************/
static int Demux( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    int64_t     i_tell = stream_Tell( p_demux->s );

    block_t     *p_block;
    int         i_read;

    if( p_sys->i_ssnd_end > 0 && i_tell >= p_sys->i_ssnd_end )
    {
        /* EOF */
        return 0;
    }

    /* Set PCR */
244
    es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_time);
Laurent Aimar's avatar
Laurent Aimar committed
245 246 247 248 249 250 251 252 253 254 255 256 257

    /* we will read 100ms at once */
    i_read = p_sys->i_ssnd_fsize * ( p_sys->fmt.audio.i_rate / 10 );
    if( p_sys->i_ssnd_end > 0 && p_sys->i_ssnd_end - i_tell < i_read )
    {
        i_read = p_sys->i_ssnd_end - i_tell;
    }
    if( ( p_block = stream_Block( p_demux->s, i_read ) ) == NULL )
    {
        return 0;
    }

    p_block->i_dts =
258
    p_block->i_pts = VLC_TS_0 + p_sys->i_time;
Laurent Aimar's avatar
Laurent Aimar committed
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 303 304 305 306 307 308 309 310 311 312

    p_sys->i_time += (int64_t)1000000 *
                     p_block->i_buffer /
                     p_sys->i_ssnd_fsize /
                     p_sys->fmt.audio.i_rate;

    /* */
    es_out_Send( p_demux->out, p_sys->es, p_block );
    return 1;
}

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

    switch( i_query )
    {
        case DEMUX_GET_POSITION:
        {
            int64_t i_start = p_sys->i_ssnd_start;
            int64_t i_end   = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
            int64_t i_tell  = stream_Tell( p_demux->s );

            pf = (double*) va_arg( args, double* );

            if( i_start < i_end )
            {
                *pf = (double)(i_tell - i_start)/(double)(i_end - i_start);
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;
        }

        case DEMUX_SET_POSITION:
        {
            int64_t i_start = p_sys->i_ssnd_start;
            int64_t i_end  = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );

            f = (double) va_arg( args, double );

            if( i_start < i_end )
            {
                int     i_frame = (f * ( i_end - i_start )) / p_sys->i_ssnd_fsize;
                int64_t i_new   = i_start + i_frame * p_sys->i_ssnd_fsize;

                if( stream_Seek( p_demux->s, i_new ) )
                {
                    return VLC_EGENERIC;
                }
313
                p_sys->i_time = (int64_t)1000000 * i_frame / p_sys->fmt.audio.i_rate;
Laurent Aimar's avatar
Laurent Aimar committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;
        }

        case DEMUX_GET_TIME:
            pi64 = (int64_t*)va_arg( args, int64_t * );
            *pi64 = p_sys->i_time;
            return VLC_SUCCESS;

        case DEMUX_GET_LENGTH:
        {
            int64_t i_end  = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );

            pi64 = (int64_t*)va_arg( args, int64_t * );
            if( p_sys->i_ssnd_start < i_end )
            {
                *pi64 = (int64_t)1000000 * ( i_end - p_sys->i_ssnd_start ) / p_sys->i_ssnd_fsize / p_sys->fmt.audio.i_rate;
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;
        }
        case DEMUX_SET_TIME:
        case DEMUX_GET_FPS:
        default:
            return VLC_EGENERIC;
    }
}