1 /*****************************************************************************
2 * tta.c : The Lossless True Audio parser
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_demux.h>
33 #include <vlc_codec.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close ( vlc_object_t * );
43 set_shortname( "TTA" );
44 set_description( _("TTA demuxer") );
45 set_category( CAT_INPUT );
46 set_subcategory( SUBCAT_INPUT_DEMUX );
47 set_capability( "demux", 145 );
49 set_callbacks( Open, Close );
50 add_shortcut( "tta" );
53 #define TTA_FRAMETIME 1.04489795918367346939
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Demux ( demux_t * );
59 static int Control( demux_t *, int, va_list );
69 uint32_t *pi_seektable;
78 /*****************************************************************************
79 * Open: initializes ES structures
80 *****************************************************************************/
81 static int Open( vlc_object_t * p_this )
83 demux_t *p_demux = (demux_t*)p_this;
86 const uint8_t *p_peek;
89 int i_seektable_size = 0, i;
90 //char psz_info[4096];
93 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
96 if( !POKE( p_peek, "TTA1", 4 ) )
98 if( !p_demux->b_force ) return VLC_EGENERIC;
101 msg_Err( p_demux, "this doesn't look like a flac stream, "
102 "continuing anyway" );
105 if( stream_Read( p_demux->s, p_header, 22 ) < 22 )
108 /* Fill p_demux fields */
109 p_demux->pf_demux = Demux;
110 p_demux->pf_control = Control;
111 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
115 /* Read the metadata */
116 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'T', 'T', 'A', '1' ) );
117 fmt.audio.i_channels = GetWLE( &p_header[6] );
118 fmt.audio.i_bitspersample = GetWLE( &p_header[8] );
119 fmt.audio.i_rate = GetDWLE( &p_header[10] );
121 p_sys->i_datalength = GetDWLE( &p_header[14] );
122 p_sys->i_framelength = TTA_FRAMETIME * fmt.audio.i_rate;
124 p_sys->i_totalframes = p_sys->i_datalength / p_sys->i_framelength +
125 ((p_sys->i_datalength % p_sys->i_framelength) ? 1 : 0);
126 p_sys->i_currentframe = 0;
128 i_seektable_size = sizeof(uint32_t)*p_sys->i_totalframes;
129 p_seektable = (uint8_t *)malloc( i_seektable_size );
136 stream_Read( p_demux->s, p_seektable, i_seektable_size );
137 p_sys->pi_seektable = (uint32_t *)malloc( i_seektable_size );
138 if( !p_sys->pi_seektable )
145 for( i = 0; i < p_sys->i_totalframes; i++ )
146 p_sys->pi_seektable[i] = GetDWLE( &p_seektable[i*4] );
148 stream_Read( p_demux->s, NULL, 4 ); /* CRC */
150 /* Store the header and Seektable for avcodec */
151 fmt.i_extra = 22 + (p_sys->i_totalframes * 4) + 4;
152 fmt.p_extra = malloc( fmt.i_extra );
155 free( p_sys->pi_seektable );
160 memcpy( (uint8_t*)fmt.p_extra, p_header, 22 );
161 memcpy( (uint8_t*)fmt.p_extra+22, p_seektable, fmt.i_extra -22 );
163 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
165 p_sys->i_start = stream_Tell( p_demux->s );
170 /*****************************************************************************
171 * Close: frees unused data
172 *****************************************************************************/
173 static void Close( vlc_object_t * p_this )
175 demux_t *p_demux = (demux_t*)p_this;
176 demux_sys_t *p_sys = p_demux->p_sys;
178 free( p_sys->pi_seektable );
182 /*****************************************************************************
184 *****************************************************************************
185 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
186 *****************************************************************************/
187 static int Demux( demux_t *p_demux )
189 demux_sys_t *p_sys = p_demux->p_sys;
192 if( p_sys->i_currentframe > p_sys->i_totalframes )
195 p_data = stream_Block( p_demux->s, p_sys->pi_seektable[p_sys->i_currentframe] );
196 if( p_data == NULL ) return 0;
197 p_data->i_dts = p_data->i_pts = (int64_t)(1 + I64C(1000000) * p_sys->i_currentframe) * TTA_FRAMETIME;
199 p_sys->i_currentframe++;
201 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
202 es_out_Send( p_demux->out, p_sys->p_es, p_data );
207 /*****************************************************************************
209 *****************************************************************************/
210 static int Control( demux_t *p_demux, int i_query, va_list args )
212 demux_sys_t *p_sys = p_demux->p_sys;
218 case DEMUX_GET_POSITION:
219 pf = (double*) va_arg( args, double* );
220 i64 = stream_Size( p_demux->s ) - p_sys->i_start;
223 *pf = (double)(stream_Tell( p_demux->s ) - p_sys->i_start )/ (double)i64;
231 case DEMUX_SET_POSITION:
232 f = (double)va_arg( args, double );
233 i64 = (int64_t)(f * (stream_Size( p_demux->s ) - p_sys->i_start));
234 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
239 for( i=0; i < p_sys->i_totalframes && tmp+p_sys->pi_seektable[i] < i64; i++)
241 tmp += p_sys->pi_seektable[i];
243 stream_Seek( p_demux->s, tmp+p_sys->i_start );
244 p_sys->i_currentframe = i;
249 case DEMUX_GET_LENGTH:
250 pi64 = (int64_t*)va_arg( args, int64_t * );
251 *pi64 = I64C(1000000) * p_sys->i_totalframes * TTA_FRAMETIME;
255 pi64 = (int64_t*)va_arg( args, int64_t * );
256 *pi64 = I64C(1000000) * p_sys->i_currentframe * TTA_FRAMETIME;