contrib/gpg-error: simplify darwin triplet handling
[vlc.git] / modules / demux / tta.c
blob81c162cd254caa9715ceb218355862c1eda1f2a8
1 /*****************************************************************************
2 * tta.c : The Lossless True Audio parser
3 *****************************************************************************
4 * Copyright (C) 2006 VLC authors and VideoLAN
6 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_demux.h>
33 #include <vlc_codec.h>
34 #include <math.h>
35 #include <limits.h>
37 /*****************************************************************************
38 * Module descriptor
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close ( vlc_object_t * );
43 vlc_module_begin ()
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" )
52 vlc_module_end ()
54 #define TTA_FRAMETIME 1.04489795918367346939
56 /*****************************************************************************
57 * Local prototypes
58 *****************************************************************************/
59 static int Demux ( demux_t * );
60 static int Control( demux_t *, int, va_list );
62 typedef struct
64 /* */
65 es_out_id_t *p_es;
67 /* */
68 uint32_t i_totalframes;
69 uint32_t i_currentframe;
70 uint32_t *pi_seektable;
71 uint32_t i_datalength;
72 int i_framelength;
74 /* */
75 vlc_meta_t *p_meta;
76 int64_t i_start;
77 } demux_sys_t;
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;
85 demux_sys_t *p_sys;
86 es_format_t fmt;
87 const uint8_t *p_peek;
88 uint8_t p_header[22];
89 uint8_t *p_fullheader;
90 int i_seektable_size = 0;
91 //char psz_info[4096];
92 //module_t *p_id3;
94 if( vlc_stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
95 return VLC_EGENERIC;
97 if( memcmp( p_peek, "TTA1", 4 ) )
99 if( !p_demux->obj.force )
100 return VLC_EGENERIC;
102 /* User forced */
103 msg_Err( p_demux, "this doesn't look like a true-audio stream, "
104 "continuing anyway" );
107 if( vlc_stream_Read( p_demux->s, p_header, 22 ) < 22 )
108 return VLC_EGENERIC;
110 /* Fill p_demux fields */
111 p_demux->pf_demux = Demux;
112 p_demux->pf_control = Control;
113 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
114 if( !p_sys )
115 return VLC_ENOMEM;
117 p_sys->pi_seektable = NULL;
119 /* Read the metadata */
120 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_TTA );
121 fmt.audio.i_channels = GetWLE( &p_header[6] );
122 fmt.audio.i_bitspersample = GetWLE( &p_header[8] );
123 fmt.audio.i_rate = GetDWLE( &p_header[10] );
124 if( fmt.audio.i_rate == 0 || /* Avoid divide by 0 */
125 fmt.audio.i_rate > ( 1 << 20 ) /* Avoid i_framelength overflow */ )
127 msg_Warn( p_demux, "Wrong sample rate" );
128 goto error;
131 p_sys->i_datalength = GetDWLE( &p_header[14] );
132 p_sys->i_framelength = TTA_FRAMETIME * fmt.audio.i_rate;
134 p_sys->i_totalframes = p_sys->i_datalength / p_sys->i_framelength +
135 ((p_sys->i_datalength % p_sys->i_framelength) != 0);
136 p_sys->i_currentframe = 0;
137 if( (INT_MAX - 22 - 4) / sizeof(uint32_t) < p_sys->i_totalframes )
138 goto error;
140 i_seektable_size = sizeof(uint32_t)*p_sys->i_totalframes;
142 /* Store the header and Seektable for avcodec */
143 fmt.i_extra = 22 + i_seektable_size + 4;
144 fmt.p_extra = p_fullheader = malloc( fmt.i_extra );
145 if( !p_fullheader )
147 fmt.i_extra = 0;
148 goto error;
151 memcpy( p_fullheader, p_header, 22 );
152 p_fullheader += 22;
153 if( vlc_stream_Read( p_demux->s, p_fullheader, i_seektable_size )
154 != i_seektable_size )
155 goto error;
157 p_sys->pi_seektable = calloc( p_sys->i_totalframes, sizeof(uint32_t) );
158 if( !p_sys->pi_seektable )
159 goto error;
160 for( uint32_t i = 0; i < p_sys->i_totalframes; i++ )
162 p_sys->pi_seektable[i] = GetDWLE( p_fullheader );
163 p_fullheader += 4;
166 if( 4 != vlc_stream_Read( p_demux->s, p_fullheader, 4 ) ) /* CRC */
167 goto error;
168 p_fullheader += 4;
170 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
171 p_sys->i_start = p_fullheader - (uint8_t *)fmt.p_extra;
172 es_format_Clean( &fmt );
174 return VLC_SUCCESS;
175 error:
176 es_format_Clean( &fmt );
177 Close( p_this );
178 return VLC_EGENERIC;
181 /*****************************************************************************
182 * Close: frees unused data
183 *****************************************************************************/
184 static void Close( vlc_object_t * p_this )
186 demux_t *p_demux = (demux_t*)p_this;
187 demux_sys_t *p_sys = p_demux->p_sys;
189 free( p_sys->pi_seektable );
190 free( p_sys );
193 /*****************************************************************************
194 * Demux:
195 *****************************************************************************
196 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
197 *****************************************************************************/
198 static int Demux( demux_t *p_demux )
200 demux_sys_t *p_sys = p_demux->p_sys;
201 block_t *p_data;
203 if( p_sys->i_currentframe >= p_sys->i_totalframes )
204 return VLC_DEMUXER_EOF;
206 p_data = vlc_stream_Block( p_demux->s,
207 p_sys->pi_seektable[p_sys->i_currentframe] );
208 if( p_data == NULL )
209 return VLC_DEMUXER_EOF;
210 p_data->i_dts = p_data->i_pts = VLC_TICK_0 + vlc_tick_from_sec( p_sys->i_currentframe * TTA_FRAMETIME );
212 p_sys->i_currentframe++;
214 es_out_SetPCR( p_demux->out, p_data->i_dts );
215 if( p_sys->p_es )
216 es_out_Send( p_demux->out, p_sys->p_es, p_data );
218 return VLC_DEMUXER_SUCCESS;
221 /*****************************************************************************
222 * Control:
223 *****************************************************************************/
224 static int Control( demux_t *p_demux, int i_query, va_list args )
226 demux_sys_t *p_sys = p_demux->p_sys;
227 double f, *pf;
228 int64_t i64;
230 switch( i_query )
232 case DEMUX_CAN_SEEK:
233 return vlc_stream_vaControl( p_demux->s, i_query, args );
235 case DEMUX_GET_POSITION:
236 pf = va_arg( args, double * );
237 i64 = stream_Size( p_demux->s ) - p_sys->i_start;
238 if( i64 > 0 )
240 *pf = (double)(vlc_stream_Tell( p_demux->s ) - p_sys->i_start )/ (double)i64;
242 else
244 *pf = 0.0;
246 return VLC_SUCCESS;
248 case DEMUX_SET_POSITION:
249 f = va_arg( args, double );
250 i64 = (int64_t)(f * (stream_Size( p_demux->s ) - p_sys->i_start));
251 if( i64 > 0 )
253 int64_t tmp = 0;
254 uint32_t i;
255 for( i=0; i < p_sys->i_totalframes && tmp+p_sys->pi_seektable[i] < i64; i++)
257 tmp += p_sys->pi_seektable[i];
259 if( vlc_stream_Seek( p_demux->s, tmp+p_sys->i_start ) )
260 return VLC_EGENERIC;
261 p_sys->i_currentframe = i;
262 return VLC_SUCCESS;
264 return VLC_EGENERIC;
266 case DEMUX_GET_LENGTH:
267 *va_arg( args, vlc_tick_t * ) =
268 vlc_tick_from_sec( p_sys->i_totalframes * TTA_FRAMETIME );
269 return VLC_SUCCESS;
271 case DEMUX_GET_TIME:
272 *va_arg( args, vlc_tick_t * ) = vlc_tick_from_sec( p_sys->i_currentframe * TTA_FRAMETIME );
273 return VLC_SUCCESS;
275 case DEMUX_CAN_PAUSE:
276 case DEMUX_SET_PAUSE_STATE:
277 case DEMUX_CAN_CONTROL_PACE:
278 case DEMUX_GET_PTS_DELAY:
279 return demux_vaControlHelper( p_demux->s, 0, p_sys->i_datalength,
280 0, p_sys->i_framelength, i_query, args );
282 default:
283 return VLC_EGENERIC;