]> git.sesse.net Git - vlc/blob - modules/demux/tta.c
42fda085f9708d3cad36a4218b3826b07e7b6d66
[vlc] / modules / demux / tta.c
1 /*****************************************************************************
2  * tta.c : The Lossless True Audio parser
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
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
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 static int  Open  ( vlc_object_t * );
41 static void Close ( vlc_object_t * );
42
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 );
49
50     set_callbacks( Open, Close );
51     add_shortcut( "tta" );
52 vlc_module_end();
53
54 #define TTA_FRAMETIME 1.04489795918367346939
55
56 /*****************************************************************************
57  * Local prototypes
58  *****************************************************************************/
59 static int Demux  ( demux_t * );
60 static int Control( demux_t *, int, va_list );
61
62 struct demux_sys_t
63 {
64     /* */
65     es_out_id_t *p_es;
66
67     /* */
68     int      i_totalframes;
69     int      i_currentframe;
70     uint32_t *pi_seektable;
71     int      i_datalength;
72     int      i_framelength;
73
74     /* */
75     vlc_meta_t     *p_meta;
76     int64_t        i_start;
77 };
78
79 /*****************************************************************************
80  * Open: initializes ES structures
81  *****************************************************************************/
82 static int Open( vlc_object_t * p_this )
83 {
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_seektable;
90     int         i_seektable_size = 0, i;
91     //char        psz_info[4096];
92     //module_t    *p_id3;
93
94     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
95         return VLC_EGENERIC;
96
97     if( !POKE( p_peek, "TTA1", 4 ) )
98     {
99         if( !p_demux->b_force ) return VLC_EGENERIC;
100
101         /* User forced */
102         msg_Err( p_demux, "this doesn't look like a flac stream, "
103                  "continuing anyway" );
104     }
105
106     if( stream_Read( p_demux->s, p_header, 22 ) < 22 )
107         return VLC_EGENERIC;
108
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 ) );
113     if( !p_sys )
114         return VLC_ENOMEM;
115
116     /* Read the metadata */
117     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'T', 'T', 'A', '1' ) );
118     fmt.audio.i_channels = GetWLE( &p_header[6] );
119     fmt.audio.i_bitspersample = GetWLE( &p_header[8] );
120     fmt.audio.i_rate = GetDWLE( &p_header[10] );
121
122     p_sys->i_datalength = GetDWLE( &p_header[14] );
123     p_sys->i_framelength = TTA_FRAMETIME * fmt.audio.i_rate;
124
125     p_sys->i_totalframes = p_sys->i_datalength / p_sys->i_framelength +
126                           ((p_sys->i_datalength % p_sys->i_framelength) ? 1 : 0);
127     p_sys->i_currentframe = 0;
128
129     i_seektable_size = sizeof(uint32_t)*p_sys->i_totalframes;
130     p_seektable = (uint8_t *)malloc( i_seektable_size );
131     if( !p_seektable )
132     {
133         free( p_sys );
134         return VLC_ENOMEM;
135     }
136
137     stream_Read( p_demux->s, p_seektable, i_seektable_size );
138     p_sys->pi_seektable = (uint32_t *)malloc( i_seektable_size );
139     if( !p_sys->pi_seektable )
140     {
141         free( p_seektable );
142         free( p_sys );
143         return VLC_ENOMEM;
144     }
145
146     for( i = 0; i < p_sys->i_totalframes; i++ )
147         p_sys->pi_seektable[i] = GetDWLE( &p_seektable[i*4] );
148
149     stream_Read( p_demux->s, NULL, 4 ); /* CRC */
150
151     /* Store the header and Seektable for avcodec */
152     fmt.i_extra = 22 + (p_sys->i_totalframes * 4) + 4;
153     fmt.p_extra = malloc( fmt.i_extra );
154     if( !fmt.p_extra )
155     {
156         free( p_sys->pi_seektable );
157         free( p_seektable );
158         free( p_sys );
159         return VLC_ENOMEM;
160     }
161     memcpy( (uint8_t*)fmt.p_extra, p_header, 22 );
162     memcpy( (uint8_t*)fmt.p_extra+22, p_seektable, fmt.i_extra -22 );
163
164     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
165     free( p_seektable );
166     p_sys->i_start = stream_Tell( p_demux->s );
167
168     return VLC_SUCCESS;
169 }
170
171 /*****************************************************************************
172  * Close: frees unused data
173  *****************************************************************************/
174 static void Close( vlc_object_t * p_this )
175 {
176     demux_t        *p_demux = (demux_t*)p_this;
177     demux_sys_t    *p_sys = p_demux->p_sys;
178
179     free( p_sys->pi_seektable );
180     free( p_sys );
181 }
182
183 /*****************************************************************************
184  * Demux:
185  *****************************************************************************
186  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
187  *****************************************************************************/
188 static int Demux( demux_t *p_demux )
189 {
190     demux_sys_t *p_sys = p_demux->p_sys;
191     block_t     *p_data;
192
193     if( p_sys->i_currentframe > p_sys->i_totalframes )
194         return 0;
195
196     p_data = stream_Block( p_demux->s, p_sys->pi_seektable[p_sys->i_currentframe] );
197     if( p_data == NULL ) return 0;
198     p_data->i_dts = p_data->i_pts = (int64_t)(1 + INT64_C(1000000) * p_sys->i_currentframe) * TTA_FRAMETIME;
199
200     p_sys->i_currentframe++;
201
202     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
203     es_out_Send( p_demux->out, p_sys->p_es, p_data );
204
205     return 1;
206 }
207
208 /*****************************************************************************
209  * Control:
210  *****************************************************************************/
211 static int Control( demux_t *p_demux, int i_query, va_list args )
212 {
213     demux_sys_t *p_sys = p_demux->p_sys;
214     double   f, *pf;
215     int64_t i64, *pi64;
216
217     switch( i_query )
218     {
219         case DEMUX_GET_POSITION:
220             pf = (double*) va_arg( args, double* );
221             i64 = stream_Size( p_demux->s ) - p_sys->i_start;
222             if( i64 > 0 )
223             {
224                 *pf = (double)(stream_Tell( p_demux->s ) - p_sys->i_start )/ (double)i64;
225             }
226             else
227             {
228                 *pf = 0.0;
229             }
230             return VLC_SUCCESS;
231
232         case DEMUX_SET_POSITION:
233             f = (double)va_arg( args, double );
234             i64 = (int64_t)(f * (stream_Size( p_demux->s ) - p_sys->i_start));
235             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
236             if( i64 > 0 )
237             {
238                 int64_t tmp = 0;
239                 int     i;
240                 for( i=0; i < p_sys->i_totalframes && tmp+p_sys->pi_seektable[i] < i64; i++)
241                 {
242                     tmp += p_sys->pi_seektable[i];
243                 }
244                 stream_Seek( p_demux->s, tmp+p_sys->i_start );
245                 p_sys->i_currentframe = i;
246                 return VLC_SUCCESS;
247             }
248             return VLC_EGENERIC;
249
250         case DEMUX_GET_LENGTH:
251             pi64 = (int64_t*)va_arg( args, int64_t * );
252             *pi64 = INT64_C(1000000) * p_sys->i_totalframes * TTA_FRAMETIME;
253             return VLC_SUCCESS;
254
255         case DEMUX_GET_TIME:
256             pi64 = (int64_t*)va_arg( args, int64_t * );
257             *pi64 = INT64_C(1000000) * p_sys->i_currentframe * TTA_FRAMETIME;
258             return VLC_SUCCESS;
259
260         default:
261             return VLC_EGENERIC;
262     }
263 }
264