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;
82 const uint8_t *p_peek;
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 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
145 if( !p_demux->p_private )
147 if( ( p_id3 = module_Need( p_demux, "meta reader", NULL, 0 ) ) )
149 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
150 p_sys->p_meta = p_demux_meta->p_meta;
151 p_demux->p_private = NULL;
152 module_Unneed( p_demux, p_id3 );
153 TAB_CLEAN( p_demux_meta->i_attachments, p_demux_meta->attachments );
155 free( p_demux->p_private );
158 p_sys->p_meta = vlc_meta_New();
163 /*****************************************************************************
164 * Close: frees unused data
165 *****************************************************************************/
166 static void Close( vlc_object_t * p_this )
168 demux_t *p_demux = (demux_t*)p_this;
169 demux_sys_t *p_sys = p_demux->p_sys;
174 /*****************************************************************************
176 *****************************************************************************
177 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
178 *****************************************************************************/
179 static int Demux( demux_t *p_demux )
181 demux_sys_t *p_sys = p_demux->p_sys;
184 if( p_sys->i_currentframe > p_sys->i_totalframes )
187 p_data = stream_Block( p_demux->s, p_sys->pi_seektable[p_sys->i_currentframe] );
188 if( p_data == NULL ) return 0;
189 p_data->i_dts = p_data->i_pts = (int64_t)(1 + I64C(1000000) * p_sys->i_currentframe) * TTA_FRAMETIME;
191 p_sys->i_currentframe++;
193 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
194 es_out_Send( p_demux->out, p_sys->p_es, p_data );
199 /*****************************************************************************
201 *****************************************************************************/
202 static int Control( demux_t *p_demux, int i_query, va_list args )
204 demux_sys_t *p_sys = p_demux->p_sys;
210 case DEMUX_GET_POSITION:
211 pf = (double*) va_arg( args, double* );
212 i64 = stream_Size( p_demux->s ) - p_sys->i_start;
215 *pf = (double)(stream_Tell( p_demux->s ) - p_sys->i_start )/ (double)i64;
223 case DEMUX_SET_POSITION:
224 f = (double)va_arg( args, double );
225 i64 = (int64_t)(f * (stream_Size( p_demux->s ) - p_sys->i_start));
226 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
231 for( i=0; i < p_sys->i_totalframes && tmp+p_sys->pi_seektable[i] < i64; i++)
233 tmp += p_sys->pi_seektable[i];
235 stream_Seek( p_demux->s, tmp+p_sys->i_start );
236 p_sys->i_currentframe = i;
241 case DEMUX_GET_LENGTH:
242 pi64 = (int64_t*)va_arg( args, int64_t * );
243 *pi64 = I64C(1000000) * p_sys->i_totalframes * TTA_FRAMETIME;
247 pi64 = (int64_t*)va_arg( args, int64_t * );
248 *pi64 = I64C(1000000) * p_sys->i_currentframe * TTA_FRAMETIME;