xa.c 7.24 KB
Newer Older
1
/*****************************************************************************
Jean-Paul Saman's avatar
Jean-Paul Saman committed
2
 * xa.c : xa file demux module for vlc
3
 *****************************************************************************
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
4
 * Copyright (C) 2005 Rémi Denis-Courmont
5 6
 * $Id$
 *
7
 * Authors: Rémi Denis-Courmont <rem # videolan.org>
8
 *
Jean-Baptiste Kempf's avatar
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
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
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
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.
22 23 24 25 26 27
 *****************************************************************************/

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

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

32
#include <assert.h>
33
#include <vlc_common.h>
34
#include <vlc_plugin.h>
35
#include <vlc_demux.h>
36 37 38 39 40 41 42

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

43 44 45 46 47 48 49
vlc_module_begin ()
    set_description( N_("XA demuxer") )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_DEMUX )
    set_capability( "demux", 10 )
    set_callbacks( Open, Close )
vlc_module_end ()
50 51 52 53 54 55 56 57 58 59 60 61

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

struct demux_sys_t
{
    es_out_id_t     *p_es;

    unsigned int    i_data_size;
62
    unsigned int    i_block_frames;
63 64
    unsigned int    i_frame_size;
    unsigned int    i_bitrate;
65 66 67 68

    date_t          pts;
};

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
69
typedef struct xa_header_t
70
{
71
    char     xa_id[4];
72
    uint32_t iSize;
73

74 75 76 77 78 79 80
    uint16_t wFormatTag;
    uint16_t nChannels;
    uint32_t nSamplesPerSec;
    uint32_t nAvgBytesPerSec;
    uint16_t nBlockAlign;
    uint16_t wBitsPerSample;
} xa_header_t;
81

82 83
#define HEADER_LENGTH 24

84 85
static_assert(offsetof(xa_header_t, wBitsPerSample) == 22, "Bad padding");

86 87
#define FRAME_LENGTH 28 /* samples per frame */

88 89 90 91 92 93
/*****************************************************************************
 * Open: check file and initializes structures
 *****************************************************************************/
static int Open( vlc_object_t * p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
94
    const uint8_t *peek;
95 96

    /* XA file heuristic */
97
    if( vlc_stream_Peek( p_demux->s, &peek, 10 ) < 10 )
98
        return VLC_EGENERIC;
99 100
    if( memcmp( peek, "XAI", 4 ) && memcmp( peek, "XAJ", 4 ) &&
            memcmp( peek, "XA\0", 4 ) )
101 102
        return VLC_EGENERIC;
    if( GetWLE( peek + 8 ) != 1 ) /* format tag */
103 104
        return VLC_EGENERIC;

105
    demux_sys_t *p_sys = malloc( sizeof( demux_sys_t ) );
106 107 108
    if( unlikely( p_sys == NULL ) )
        return VLC_ENOMEM;

109 110 111
    /* read XA header*/
    xa_header_t xa;

112
    if( vlc_stream_Read( p_demux->s, &xa, HEADER_LENGTH ) < HEADER_LENGTH )
113 114 115 116
    {
        free( p_sys );
        return VLC_EGENERIC;
    }
117

118
    es_format_t fmt;
119
    es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_ADPCM_XA_EA );
120

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
121
    msg_Dbg( p_demux, "assuming EA ADPCM audio codec" );
122 123
    fmt.audio.i_rate = GetDWLE( &xa.nSamplesPerSec );
    fmt.audio.i_bytes_per_frame = 15 * GetWLE( &xa.nChannels );
124 125
    fmt.audio.i_frame_length = FRAME_LENGTH;

126
    fmt.audio.i_channels = GetWLE ( &xa.nChannels );
127
    fmt.audio.i_blockalign = fmt.audio.i_bytes_per_frame;
128
    fmt.audio.i_bitspersample = GetWLE( &xa.wBitsPerSample );
129 130
    fmt.i_bitrate = (fmt.audio.i_rate * fmt.audio.i_bytes_per_frame * 8)
                    / fmt.audio.i_frame_length;
131 132

    /* FIXME: better computation */
133
    p_sys->i_data_size = xa.iSize * 15 / 56;
134
    /* How many frames per block (1:1 is too CPU intensive) */
135 136 137
    p_sys->i_block_frames = fmt.audio.i_rate / (FRAME_LENGTH * 20) + 1;
    p_sys->i_frame_size = fmt.audio.i_bytes_per_frame;
    p_sys->i_bitrate = fmt.i_bitrate;
138 139 140

    msg_Dbg( p_demux, "fourcc: %4.4s, channels: %d, "
             "freq: %d Hz, bitrate: %dKo/s, blockalign: %d",
141 142
             (char *)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate,
             fmt.i_bitrate / 8192, fmt.audio.i_blockalign );
143

144 145
    if( fmt.audio.i_rate == 0 || fmt.audio.i_channels == 0
     || fmt.audio.i_bitspersample != 16 )
146 147 148 149 150
    {
        free( p_sys );
        return VLC_EGENERIC;
    }

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

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

156 157 158
    p_demux->pf_demux   = Demux;
    p_demux->pf_control = Control;
    p_demux->p_sys      = p_sys;
159 160 161 162 163 164 165 166 167 168 169 170
    return VLC_SUCCESS;
}

/*****************************************************************************
 * 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;
171
    int64_t     i_offset = vlc_stream_Tell( p_demux->s );
172
    unsigned    i_frames = p_sys->i_block_frames;
173 174

    if( p_sys->i_data_size > 0 &&
175
        (i_offset - HEADER_LENGTH) >= p_sys->i_data_size )
176 177 178 179 180
    {
        /* EOF */
        return 0;
    }

181
    p_block = vlc_stream_Block( p_demux->s, p_sys->i_frame_size * i_frames );
182 183 184 185 186 187
    if( p_block == NULL )
    {
        msg_Warn( p_demux, "cannot read data" );
        return 0;
    }

188 189
    i_frames = p_block->i_buffer / p_sys->i_frame_size;
    p_block->i_dts = p_block->i_pts = date_Get( &p_sys->pts );
190
    es_out_SetPCR( p_demux->out, p_block->i_pts );
191
    es_out_Send( p_demux->out, p_sys->p_es, p_block );
192

193
    date_Increment( &p_sys->pts, i_frames * FRAME_LENGTH );
194

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    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;

215
    return demux_vaControlHelper( p_demux->s, HEADER_LENGTH,
216
        p_sys->i_data_size ? (int64_t)HEADER_LENGTH + p_sys->i_data_size : -1,
217 218
                                  p_sys->i_bitrate, p_sys->i_frame_size,
                                  i_query, args );
219
}