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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_demux.h>
34 #include <vlc_codec.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close ( vlc_object_t * );
44 set_shortname( "TTA" );
45 set_description( N_("TTA demuxer") );
46 set_category( CAT_INPUT );
47 set_subcategory( SUBCAT_INPUT_DEMUX );
48 set_capability( "demux", 145 );
50 set_callbacks( Open, Close );
51 add_shortcut( "tta" );
54 #define TTA_FRAMETIME 1.04489795918367346939
56 /*****************************************************************************
58 *****************************************************************************/
59 static int Demux ( demux_t * );
60 static int Control( demux_t *, int, va_list );
68 uint32_t i_totalframes;
69 uint32_t i_currentframe;
70 uint32_t *pi_seektable;
71 uint32_t i_datalength;
79 /*****************************************************************************
80 * Open: initializes ES structures
81 *****************************************************************************/
82 static int Open( vlc_object_t * p_this )
84 demux_t *p_demux = (demux_t*)p_this;
87 const uint8_t *p_peek;
89 uint8_t *p_fullheader;
90 int i_seektable_size = 0;
91 //char psz_info[4096];
94 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
97 if( !POKE( p_peek, "TTA1", 4 ) )
99 if( !p_demux->b_force ) return VLC_EGENERIC;
102 msg_Err( p_demux, "this doesn't look like a true-audio stream, "
103 "continuing anyway" );
106 if( stream_Read( p_demux->s, p_header, 22 ) < 22 )
109 /* Fill p_demux fields */
110 p_demux->pf_demux = Demux;
111 p_demux->pf_control = Control;
112 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
116 p_sys->pi_seektable = NULL;
118 /* Read the metadata */
119 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'T', 'T', 'A', '1' ) );
120 fmt.audio.i_channels = GetWLE( &p_header[6] );
121 fmt.audio.i_bitspersample = GetWLE( &p_header[8] );
122 fmt.audio.i_rate = GetDWLE( &p_header[10] );
123 if( fmt.audio.i_rate == 0 || /* Avoid divide by 0 */
124 fmt.audio.i_rate > ( 1 << 20 ) /* Avoid i_framelength overflow */ )
126 msg_Warn( p_demux, "Wrong sample rate" );
130 p_sys->i_datalength = GetDWLE( &p_header[14] );
131 p_sys->i_framelength = TTA_FRAMETIME * fmt.audio.i_rate;
133 p_sys->i_totalframes = p_sys->i_datalength / p_sys->i_framelength +
134 ((p_sys->i_datalength % p_sys->i_framelength) != 0);
135 p_sys->i_currentframe = 0;
136 if( p_sys->i_totalframes > (1 << 29))
139 i_seektable_size = sizeof(uint32_t)*p_sys->i_totalframes;
141 /* Store the header and Seektable for avcodec */
142 fmt.i_extra = 22 + i_seektable_size + 4;
143 fmt.p_extra = p_fullheader = malloc( fmt.i_extra );
147 memcpy( p_fullheader, p_header, 22 );
149 if( stream_Read( p_demux->s, p_fullheader, i_seektable_size )
150 != i_seektable_size )
153 p_sys->pi_seektable = calloc( p_sys->i_totalframes, sizeof(uint32_t) );
154 if( !p_sys->pi_seektable )
156 for( uint32_t i = 0; i < p_sys->i_totalframes; i++ )
158 p_sys->pi_seektable[i] = GetDWLE( p_fullheader );
162 stream_Read( p_demux->s, p_fullheader, 4 ); /* CRC */
165 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
166 p_sys->i_start = p_fullheader - (uint8_t *)fmt.p_extra;
170 es_format_Clean( &fmt );
175 /*****************************************************************************
176 * Close: frees unused data
177 *****************************************************************************/
178 static void Close( vlc_object_t * p_this )
180 demux_t *p_demux = (demux_t*)p_this;
181 demux_sys_t *p_sys = p_demux->p_sys;
183 free( p_sys->pi_seektable );
187 /*****************************************************************************
189 *****************************************************************************
190 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
191 *****************************************************************************/
192 static int Demux( demux_t *p_demux )
194 demux_sys_t *p_sys = p_demux->p_sys;
197 if( p_sys->i_currentframe > p_sys->i_totalframes )
200 p_data = stream_Block( p_demux->s, p_sys->pi_seektable[p_sys->i_currentframe] );
201 if( p_data == NULL ) return 0;
202 p_data->i_dts = p_data->i_pts = (int64_t)(1 + INT64_C(1000000) * p_sys->i_currentframe) * TTA_FRAMETIME;
204 p_sys->i_currentframe++;
206 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
207 es_out_Send( p_demux->out, p_sys->p_es, p_data );
212 /*****************************************************************************
214 *****************************************************************************/
215 static int Control( demux_t *p_demux, int i_query, va_list args )
217 demux_sys_t *p_sys = p_demux->p_sys;
223 case DEMUX_GET_POSITION:
224 pf = (double*) va_arg( args, double* );
225 i64 = stream_Size( p_demux->s ) - p_sys->i_start;
228 *pf = (double)(stream_Tell( p_demux->s ) - p_sys->i_start )/ (double)i64;
236 case DEMUX_SET_POSITION:
237 f = (double)va_arg( args, double );
238 i64 = (int64_t)(f * (stream_Size( p_demux->s ) - p_sys->i_start));
243 for( i=0; i < p_sys->i_totalframes && tmp+p_sys->pi_seektable[i] < i64; i++)
245 tmp += p_sys->pi_seektable[i];
247 stream_Seek( p_demux->s, tmp+p_sys->i_start );
248 p_sys->i_currentframe = i;
253 case DEMUX_GET_LENGTH:
254 pi64 = (int64_t*)va_arg( args, int64_t * );
255 *pi64 = INT64_C(1000000) * p_sys->i_totalframes * TTA_FRAMETIME;
259 pi64 = (int64_t*)va_arg( args, int64_t * );
260 *pi64 = INT64_C(1000000) * p_sys->i_currentframe * TTA_FRAMETIME;