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
|
/*****************************************************************************
* tta.c : The Lossless True Audio parser
*****************************************************************************
* Copyright (C) 2006 VLC authors and VideoLAN
* $Id: be48a9a52b3756bfc2db5e669f6820ddda5b69ef $
*
* Authors: Derk-Jan Hartman <hartman at videolan dot org>
*
* 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
* (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 Lesser General Public License for more details.
*
* 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.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_demux.h>
#include <vlc_codec.h>
#include <math.h>
/*****************************************************************************
* Module descriptor
*****************************************************************************/
static int Open ( vlc_object_t * );
static void Close ( vlc_object_t * );
vlc_module_begin ()
set_shortname( "TTA" )
set_description( N_("TTA demuxer") )
set_category( CAT_INPUT )
set_subcategory( SUBCAT_INPUT_DEMUX )
set_capability( "demux", 145 )
set_callbacks( Open, Close )
add_shortcut( "tta" )
vlc_module_end ()
#define TTA_FRAMETIME 1.04489795918367346939
/*****************************************************************************
* Local prototypes
*****************************************************************************/
static int Demux ( demux_t * );
static int Control( demux_t *, int, va_list );
struct demux_sys_t
{
/* */
es_out_id_t *p_es;
/* */
uint32_t i_totalframes;
uint32_t i_currentframe;
uint32_t *pi_seektable;
uint32_t i_datalength;
int i_framelength;
/* */
vlc_meta_t *p_meta;
int64_t i_start;
};
/*****************************************************************************
* Open: initializes ES structures
*****************************************************************************/
static int Open( vlc_object_t * p_this )
{
demux_t *p_demux = (demux_t*)p_this;
demux_sys_t *p_sys;
es_format_t fmt;
const uint8_t *p_peek;
uint8_t p_header[22];
uint8_t *p_fullheader;
int i_seektable_size = 0;
//char psz_info[4096];
//module_t *p_id3;
if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
return VLC_EGENERIC;
if( memcmp( p_peek, "TTA1", 4 ) )
{
if( !p_demux->b_force )
return VLC_EGENERIC;
/* User forced */
msg_Err( p_demux, "this doesn't look like a true-audio stream, "
"continuing anyway" );
}
if( stream_Read( p_demux->s, p_header, 22 ) < 22 )
return VLC_EGENERIC;
/* Fill p_demux fields */
p_demux->pf_demux = Demux;
p_demux->pf_control = Control;
p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
if( !p_sys )
return VLC_ENOMEM;
p_sys->pi_seektable = NULL;
/* Read the metadata */
es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_TTA );
fmt.audio.i_channels = GetWLE( &p_header[6] );
fmt.audio.i_bitspersample = GetWLE( &p_header[8] );
fmt.audio.i_rate = GetDWLE( &p_header[10] );
if( fmt.audio.i_rate == 0 || /* Avoid divide by 0 */
fmt.audio.i_rate > ( 1 << 20 ) /* Avoid i_framelength overflow */ )
{
msg_Warn( p_demux, "Wrong sample rate" );
goto error;
}
p_sys->i_datalength = GetDWLE( &p_header[14] );
p_sys->i_framelength = TTA_FRAMETIME * fmt.audio.i_rate;
p_sys->i_totalframes = p_sys->i_datalength / p_sys->i_framelength +
((p_sys->i_datalength % p_sys->i_framelength) != 0);
p_sys->i_currentframe = 0;
if( p_sys->i_totalframes > (1 << 29))
goto error;
i_seektable_size = sizeof(uint32_t)*p_sys->i_totalframes;
/* Store the header and Seektable for avcodec */
fmt.i_extra = 22 + i_seektable_size + 4;
fmt.p_extra = p_fullheader = malloc( fmt.i_extra );
if( !p_fullheader )
goto error;
memcpy( p_fullheader, p_header, 22 );
p_fullheader += 22;
if( stream_Read( p_demux->s, p_fullheader, i_seektable_size )
!= i_seektable_size )
goto error;
p_sys->pi_seektable = calloc( p_sys->i_totalframes, sizeof(uint32_t) );
if( !p_sys->pi_seektable )
goto error;
for( uint32_t i = 0; i < p_sys->i_totalframes; i++ )
{
p_sys->pi_seektable[i] = GetDWLE( p_fullheader );
p_fullheader += 4;
}
stream_Read( p_demux->s, p_fullheader, 4 ); /* CRC */
p_fullheader += 4;
p_sys->p_es = es_out_Add( p_demux->out, &fmt );
p_sys->i_start = p_fullheader - (uint8_t *)fmt.p_extra;
return VLC_SUCCESS;
error:
es_format_Clean( &fmt );
Close( p_this );
return VLC_EGENERIC;
}
/*****************************************************************************
* Close: frees unused data
*****************************************************************************/
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->pi_seektable );
free( p_sys );
}
/*****************************************************************************
* Demux:
*****************************************************************************
* 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_data;
if( p_sys->i_currentframe >= p_sys->i_totalframes )
return 0;
p_data = stream_Block( p_demux->s, p_sys->pi_seektable[p_sys->i_currentframe] );
if( p_data == NULL ) return 0;
p_data->i_dts = p_data->i_pts = VLC_TS_0 + (int64_t)(INT64_C(1000000) * p_sys->i_currentframe) * TTA_FRAMETIME;
p_sys->i_currentframe++;
es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
es_out_Send( p_demux->out, p_sys->p_es, p_data );
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 i64, *pi64;
switch( i_query )
{
case DEMUX_GET_POSITION:
pf = (double*) va_arg( args, double* );
i64 = stream_Size( p_demux->s ) - p_sys->i_start;
if( i64 > 0 )
{
*pf = (double)(stream_Tell( p_demux->s ) - p_sys->i_start )/ (double)i64;
}
else
{
*pf = 0.0;
}
return VLC_SUCCESS;
case DEMUX_SET_POSITION:
f = (double)va_arg( args, double );
i64 = (int64_t)(f * (stream_Size( p_demux->s ) - p_sys->i_start));
if( i64 > 0 )
{
int64_t tmp = 0;
uint32_t i;
for( i=0; i < p_sys->i_totalframes && tmp+p_sys->pi_seektable[i] < i64; i++)
{
tmp += p_sys->pi_seektable[i];
}
stream_Seek( p_demux->s, tmp+p_sys->i_start );
p_sys->i_currentframe = i;
return VLC_SUCCESS;
}
return VLC_EGENERIC;
case DEMUX_GET_LENGTH:
pi64 = (int64_t*)va_arg( args, int64_t * );
*pi64 = INT64_C(1000000) * p_sys->i_totalframes * TTA_FRAMETIME;
return VLC_SUCCESS;
case DEMUX_GET_TIME:
pi64 = (int64_t*)va_arg( args, int64_t * );
*pi64 = INT64_C(1000000) * p_sys->i_currentframe * TTA_FRAMETIME;
return VLC_SUCCESS;
default:
return VLC_EGENERIC;
}
}
|