d3d11_deinterlace: fix decoder config mismatching
[vlc.git] / modules / demux / tta.c
blob2da7460cc9e6e330c72743baa9ab934e4aea84ff
1 /*****************************************************************************
2 * tta.c : The Lossless True Audio parser
3 *****************************************************************************
4 * Copyright (C) 2006 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_demux.h>
34 #include <vlc_codec.h>
35 #include <math.h>
36 #include <limits.h>
38 /*****************************************************************************
39 * Module descriptor
40 *****************************************************************************/
41 static int Open ( vlc_object_t * );
42 static void Close ( vlc_object_t * );
44 vlc_module_begin ()
45 set_shortname( "TTA" )
46 set_description( N_("TTA demuxer") )
47 set_category( CAT_INPUT )
48 set_subcategory( SUBCAT_INPUT_DEMUX )
49 set_capability( "demux", 145 )
51 set_callbacks( Open, Close )
52 add_shortcut( "tta" )
53 vlc_module_end ()
55 #define TTA_FRAMETIME 1.04489795918367346939
57 /*****************************************************************************
58 * Local prototypes
59 *****************************************************************************/
60 static int Demux ( demux_t * );
61 static int Control( demux_t *, int, va_list );
63 struct demux_sys_t
65 /* */
66 es_out_id_t *p_es;
68 /* */
69 uint32_t i_totalframes;
70 uint32_t i_currentframe;
71 uint32_t *pi_seektable;
72 uint32_t i_datalength;
73 int i_framelength;
75 /* */
76 vlc_meta_t *p_meta;
77 int64_t i_start;
80 /*****************************************************************************
81 * Open: initializes ES structures
82 *****************************************************************************/
83 static int Open( vlc_object_t * p_this )
85 demux_t *p_demux = (demux_t*)p_this;
86 demux_sys_t *p_sys;
87 es_format_t fmt;
88 const uint8_t *p_peek;
89 uint8_t p_header[22];
90 uint8_t *p_fullheader;
91 int i_seektable_size = 0;
92 //char psz_info[4096];
93 //module_t *p_id3;
95 if( vlc_stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
96 return VLC_EGENERIC;
98 if( memcmp( p_peek, "TTA1", 4 ) )
100 if( !p_demux->obj.force )
101 return VLC_EGENERIC;
103 /* User forced */
104 msg_Err( p_demux, "this doesn't look like a true-audio stream, "
105 "continuing anyway" );
108 if( vlc_stream_Read( p_demux->s, p_header, 22 ) < 22 )
109 return VLC_EGENERIC;
111 /* Fill p_demux fields */
112 p_demux->pf_demux = Demux;
113 p_demux->pf_control = Control;
114 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
115 if( !p_sys )
116 return VLC_ENOMEM;
118 p_sys->pi_seektable = NULL;
120 /* Read the metadata */
121 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_TTA );
122 fmt.audio.i_channels = GetWLE( &p_header[6] );
123 fmt.audio.i_bitspersample = GetWLE( &p_header[8] );
124 fmt.audio.i_rate = GetDWLE( &p_header[10] );
125 if( fmt.audio.i_rate == 0 || /* Avoid divide by 0 */
126 fmt.audio.i_rate > ( 1 << 20 ) /* Avoid i_framelength overflow */ )
128 msg_Warn( p_demux, "Wrong sample rate" );
129 goto error;
132 p_sys->i_datalength = GetDWLE( &p_header[14] );
133 p_sys->i_framelength = TTA_FRAMETIME * fmt.audio.i_rate;
135 p_sys->i_totalframes = p_sys->i_datalength / p_sys->i_framelength +
136 ((p_sys->i_datalength % p_sys->i_framelength) != 0);
137 p_sys->i_currentframe = 0;
138 if( (INT_MAX - 22 - 4) / sizeof(uint32_t) < p_sys->i_totalframes )
139 goto error;
141 i_seektable_size = sizeof(uint32_t)*p_sys->i_totalframes;
143 /* Store the header and Seektable for avcodec */
144 fmt.i_extra = 22 + i_seektable_size + 4;
145 fmt.p_extra = p_fullheader = malloc( fmt.i_extra );
146 if( !p_fullheader )
148 fmt.i_extra = 0;
149 goto error;
152 memcpy( p_fullheader, p_header, 22 );
153 p_fullheader += 22;
154 if( vlc_stream_Read( p_demux->s, p_fullheader, i_seektable_size )
155 != i_seektable_size )
156 goto error;
158 p_sys->pi_seektable = calloc( p_sys->i_totalframes, sizeof(uint32_t) );
159 if( !p_sys->pi_seektable )
160 goto error;
161 for( uint32_t i = 0; i < p_sys->i_totalframes; i++ )
163 p_sys->pi_seektable[i] = GetDWLE( p_fullheader );
164 p_fullheader += 4;
167 if( 4 != vlc_stream_Read( p_demux->s, p_fullheader, 4 ) ) /* CRC */
168 goto error;
169 p_fullheader += 4;
171 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
172 p_sys->i_start = p_fullheader - (uint8_t *)fmt.p_extra;
173 es_format_Clean( &fmt );
175 return VLC_SUCCESS;
176 error:
177 es_format_Clean( &fmt );
178 Close( p_this );
179 return VLC_EGENERIC;
182 /*****************************************************************************
183 * Close: frees unused data
184 *****************************************************************************/
185 static void Close( vlc_object_t * p_this )
187 demux_t *p_demux = (demux_t*)p_this;
188 demux_sys_t *p_sys = p_demux->p_sys;
190 free( p_sys->pi_seektable );
191 free( p_sys );
194 /*****************************************************************************
195 * Demux:
196 *****************************************************************************
197 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
198 *****************************************************************************/
199 static int Demux( demux_t *p_demux )
201 demux_sys_t *p_sys = p_demux->p_sys;
202 block_t *p_data;
204 if( p_sys->i_currentframe >= p_sys->i_totalframes )
205 return 0;
207 p_data = vlc_stream_Block( p_demux->s,
208 p_sys->pi_seektable[p_sys->i_currentframe] );
209 if( p_data == NULL ) return 0;
210 p_data->i_dts = p_data->i_pts = VLC_TS_0 + (int64_t)(INT64_C(1000000) * 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 1;
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, *pi64;
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 pi64 = va_arg( args, int64_t * );
268 *pi64 = INT64_C(1000000) * p_sys->i_totalframes * TTA_FRAMETIME;
269 return VLC_SUCCESS;
271 case DEMUX_GET_TIME:
272 pi64 = va_arg( args, int64_t * );
273 *pi64 = INT64_C(1000000) * p_sys->i_currentframe * TTA_FRAMETIME;
274 return VLC_SUCCESS;
276 default:
277 return VLC_EGENERIC;