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 *****************************************************************************/
28 #include <vlc_demux.h>
29 #include <vlc_codec.h>
32 /*****************************************************************************
34 *****************************************************************************/
35 static int Open ( vlc_object_t * );
36 static void Close ( vlc_object_t * );
39 set_shortname( "TTA" );
40 set_description( _("TTA demuxer") );
41 set_category( CAT_INPUT );
42 set_subcategory( SUBCAT_INPUT_DEMUX );
43 set_capability( "demux2", 145 );
45 set_callbacks( Open, Close );
46 add_shortcut( "tta" );
49 #define TTA_FRAMETIME 1.04489795918367346939
51 /*****************************************************************************
53 *****************************************************************************/
54 static int Demux ( demux_t * );
55 static int Control( demux_t *, int, va_list );
65 uint32_t *pi_seektable;
74 /*****************************************************************************
75 * Open: initializes ES structures
76 *****************************************************************************/
77 static int Open( vlc_object_t * p_this )
79 demux_t *p_demux = (demux_t*)p_this;
85 int i_seektable_size = 0, i;
86 //char psz_info[4096];
89 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
92 if( !POKE( p_peek, "TTA1", 4 ) )
94 if( !p_demux->b_force ) return VLC_EGENERIC;
97 msg_Err( p_demux, "this doesn't look like a flac stream, "
98 "continuing anyway" );
101 if( stream_Read( p_demux->s, p_header, 22 ) < 22 )
104 /* Fill p_demux fields */
105 p_demux->pf_demux = Demux;
106 p_demux->pf_control = Control;
107 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
109 /* Read the metadata */
110 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'T', 'T', 'A', '1' ) );
111 fmt.audio.i_channels = GetWLE( &p_header[6] );
112 fmt.audio.i_bitspersample = GetWLE( &p_header[8] );
113 fmt.audio.i_rate = GetDWLE( &p_header[10] );
115 p_sys->i_datalength = GetDWLE( &p_header[14] );
116 p_sys->i_framelength = TTA_FRAMETIME * fmt.audio.i_rate;
118 p_sys->i_totalframes = p_sys->i_datalength / p_sys->i_framelength +
119 ((p_sys->i_datalength % p_sys->i_framelength) ? 1 : 0);
120 p_sys->i_currentframe = 0;
122 i_seektable_size = sizeof(uint32_t)*p_sys->i_totalframes;
123 p_seektable = (uint8_t *)malloc( i_seektable_size );
124 stream_Read( p_demux->s, p_seektable, i_seektable_size );
125 p_sys->pi_seektable = (uint32_t *)malloc(i_seektable_size);
127 for( i = 0; i < p_sys->i_totalframes; i++ )
128 p_sys->pi_seektable[i] = GetDWLE( &p_seektable[i*4] );
130 stream_Read( p_demux->s, NULL, 4 ); /* CRC */
132 /* Store the header and Seektable for avcodec */
133 fmt.i_extra = 22 + (p_sys->i_totalframes * 4) + 4;
134 fmt.p_extra = malloc( fmt.i_extra );
135 memcpy( (uint8_t*)fmt.p_extra, p_header, 22 );
136 memcpy( (uint8_t*)fmt.p_extra+22, p_seektable, fmt.i_extra -22 );
138 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
140 p_sys->i_start = stream_Tell( p_demux->s );
143 /* Parse possible id3 header */
144 if( ( p_id3 = module_Need( p_demux, "meta reader", NULL, 0 ) ) )
146 p_sys->p_meta = (vlc_meta_t *)p_demux->p_private;
147 p_demux->p_private = NULL;
148 module_Unneed( p_demux, p_id3 );
152 p_sys->p_meta = vlc_meta_New();
157 /*****************************************************************************
158 * Close: frees unused data
159 *****************************************************************************/
160 static void Close( vlc_object_t * p_this )
162 demux_t *p_demux = (demux_t*)p_this;
163 demux_sys_t *p_sys = p_demux->p_sys;
168 /*****************************************************************************
170 *****************************************************************************
171 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
172 *****************************************************************************/
173 static int Demux( demux_t *p_demux )
175 demux_sys_t *p_sys = p_demux->p_sys;
178 if( p_sys->i_currentframe > p_sys->i_totalframes )
181 p_data = stream_Block( p_demux->s, p_sys->pi_seektable[p_sys->i_currentframe] );
182 if( p_data == NULL ) return 0;
183 p_data->i_dts = p_data->i_pts = (int64_t)(1 + I64C(1000000) * p_sys->i_currentframe) * TTA_FRAMETIME;
185 p_sys->i_currentframe++;
187 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
188 es_out_Send( p_demux->out, p_sys->p_es, p_data );
193 /*****************************************************************************
195 *****************************************************************************/
196 static int Control( demux_t *p_demux, int i_query, va_list args )
198 demux_sys_t *p_sys = p_demux->p_sys;
204 case DEMUX_GET_POSITION:
205 pf = (double*) va_arg( args, double* );
206 i64 = stream_Size( p_demux->s ) - p_sys->i_start;
209 *pf = (double)(stream_Tell( p_demux->s ) - p_sys->i_start )/ (double)i64;
217 case DEMUX_SET_POSITION:
218 f = (double)va_arg( args, double );
219 i64 = (int64_t)(f * (stream_Size( p_demux->s ) - p_sys->i_start));
220 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
225 for( i=0; i < p_sys->i_totalframes && tmp+p_sys->pi_seektable[i] < i64; i++)
227 tmp += p_sys->pi_seektable[i];
229 stream_Seek( p_demux->s, tmp+p_sys->i_start );
230 p_sys->i_currentframe = i;
235 case DEMUX_GET_LENGTH:
236 pi64 = (int64_t*)va_arg( args, int64_t * );
237 *pi64 = I64C(1000000) * p_sys->i_totalframes * TTA_FRAMETIME;
241 pi64 = (int64_t*)va_arg( args, int64_t * );
242 *pi64 = I64C(1000000) * p_sys->i_currentframe * TTA_FRAMETIME;