]> git.sesse.net Git - vlc/blob - modules/demux/tta.c
e9c686637506651fd5a0f3d23776ff28283a6761
[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 #include <vlc/vlc.h>
28 #include <vlc_demux.h>
29 #include <vlc_codec.h>
30 #include <math.h>
31
32 /*****************************************************************************
33  * Module descriptor
34  *****************************************************************************/
35 static int  Open  ( vlc_object_t * );
36 static void Close ( vlc_object_t * );
37
38 vlc_module_begin();
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 );
44
45     set_callbacks( Open, Close );
46     add_shortcut( "tta" );
47 vlc_module_end();
48
49 #define TTA_FRAMETIME 1.04489795918367346939
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 static int Demux  ( demux_t * );
55 static int Control( demux_t *, int, va_list );
56
57 struct demux_sys_t
58 {
59     /* */
60     es_out_id_t *p_es;
61
62     /* */
63     int      i_totalframes;
64     int      i_currentframe;
65     uint32_t *pi_seektable;
66     int      i_datalength;
67     int      i_framelength;
68
69     /* */
70     vlc_meta_t     *p_meta;
71     int64_t        i_start;
72 };
73
74 /*****************************************************************************
75  * Open: initializes ES structures
76  *****************************************************************************/
77 static int Open( vlc_object_t * p_this )
78 {
79     demux_t     *p_demux = (demux_t*)p_this;
80     demux_sys_t *p_sys;
81     es_format_t fmt;
82     uint8_t     *p_peek;
83     uint8_t     p_header[22];
84     uint8_t     *p_seektable;
85     int         i_seektable_size = 0, i;
86     //char        psz_info[4096];
87     //module_t    *p_id3;
88
89     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
90         return VLC_EGENERIC;
91
92     if( !POKE( p_peek, "TTA1", 4 ) )
93     {
94         if( !p_demux->b_force ) return VLC_EGENERIC;
95
96         /* User forced */
97         msg_Err( p_demux, "this doesn't look like a flac stream, "
98                  "continuing anyway" );
99     }
100
101     if( stream_Read( p_demux->s, p_header, 22 ) < 22 )
102         return VLC_EGENERIC;
103
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 ) );
108     
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] );
114
115     p_sys->i_datalength = GetDWLE( &p_header[14] );
116     p_sys->i_framelength = TTA_FRAMETIME * fmt.audio.i_rate;
117
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;
121
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);
126
127     for( i = 0; i < p_sys->i_totalframes; i++ )
128         p_sys->pi_seektable[i] = GetDWLE( &p_seektable[i*4] );
129
130     stream_Read( p_demux->s, NULL, 4 ); /* CRC */
131
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 );
137
138     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
139     free( p_seektable );
140     p_sys->i_start = stream_Tell( p_demux->s );
141     
142 #if 0
143     /* Parse possible id3 header */
144     if( ( p_id3 = module_Need( p_demux, "meta reader", NULL, 0 ) ) )
145     {
146         p_sys->p_meta = (vlc_meta_t *)p_demux->p_private;
147         p_demux->p_private = NULL;
148         module_Unneed( p_demux, p_id3 );
149     }
150
151     if( !p_sys->p_meta )
152         p_sys->p_meta = vlc_meta_New();
153 #endif
154     return VLC_SUCCESS;
155 }
156
157 /*****************************************************************************
158  * Close: frees unused data
159  *****************************************************************************/
160 static void Close( vlc_object_t * p_this )
161 {
162     demux_t        *p_demux = (demux_t*)p_this;
163     demux_sys_t    *p_sys = p_demux->p_sys;
164
165     free( p_sys );
166 }
167
168 /*****************************************************************************
169  * Demux:
170  *****************************************************************************
171  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
172  *****************************************************************************/
173 static int Demux( demux_t *p_demux )
174 {
175     demux_sys_t *p_sys = p_demux->p_sys;
176     block_t     *p_data;
177
178     if( p_sys->i_currentframe > p_sys->i_totalframes )
179         return 0;
180
181     p_data = stream_Block( p_demux->s, p_sys->pi_seektable[p_sys->i_currentframe] );
182     if( p_data == NULL ) return 0;
183     p_data->i_dts = p_data->i_pts = (int64_t)(1 + I64C(1000000) * p_sys->i_currentframe) * TTA_FRAMETIME;
184
185     p_sys->i_currentframe++;
186
187     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
188     es_out_Send( p_demux->out, p_sys->p_es, p_data );
189
190     return 1;
191 }
192
193 /*****************************************************************************
194  * Control:
195  *****************************************************************************/
196 static int Control( demux_t *p_demux, int i_query, va_list args )
197 {
198     demux_sys_t *p_sys = p_demux->p_sys;
199     double   f, *pf;
200     int64_t i64, *pi64;
201
202     switch( i_query )
203     {
204         case DEMUX_GET_POSITION:
205             pf = (double*) va_arg( args, double* );
206             i64 = stream_Size( p_demux->s ) - p_sys->i_start;
207             if( i64 > 0 )
208             {
209                 *pf = (double)(stream_Tell( p_demux->s ) - p_sys->i_start )/ (double)i64;
210             }
211             else
212             {
213                 *pf = 0.0;
214             }
215             return VLC_SUCCESS;
216
217         case DEMUX_SET_POSITION:
218             f = (double)va_arg( args, double );
219             i64 = (int64_t)(f * (stream_Size( p_demux->s ) - p_sys->i_start));
220             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
221             if( i64 > 0 )
222             {
223                 int64_t tmp = 0;
224                 int     i;
225                 for( i=0; i < p_sys->i_totalframes && tmp+p_sys->pi_seektable[i] < i64; i++)
226                 {
227                     tmp += p_sys->pi_seektable[i];
228                 }
229                 stream_Seek( p_demux->s, tmp+p_sys->i_start );
230                 p_sys->i_currentframe = i;
231                 return VLC_SUCCESS;
232             }
233             return VLC_EGENERIC;
234          
235         case DEMUX_GET_LENGTH:
236             pi64 = (int64_t*)va_arg( args, int64_t * );
237             *pi64 = I64C(1000000) * p_sys->i_totalframes * TTA_FRAMETIME;
238             return VLC_SUCCESS;
239
240         case DEMUX_GET_TIME:
241             pi64 = (int64_t*)va_arg( args, int64_t * );
242             *pi64 = I64C(1000000) * p_sys->i_currentframe * TTA_FRAMETIME;
243             return VLC_SUCCESS;
244             
245         default:
246             return VLC_EGENERIC;
247     }
248 }
249