]> git.sesse.net Git - vlc/blob - modules/demux/tta.c
demuxers: remove the need for input_thread_t by using the new "meta-preparsed" input...
[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     const 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     return VLC_SUCCESS;
143 }
144
145 /*****************************************************************************
146  * Close: frees unused data
147  *****************************************************************************/
148 static void Close( vlc_object_t * p_this )
149 {
150     demux_t        *p_demux = (demux_t*)p_this;
151     demux_sys_t    *p_sys = p_demux->p_sys;
152
153     free( p_sys );
154 }
155
156 /*****************************************************************************
157  * Demux:
158  *****************************************************************************
159  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
160  *****************************************************************************/
161 static int Demux( demux_t *p_demux )
162 {
163     demux_sys_t *p_sys = p_demux->p_sys;
164     block_t     *p_data;
165
166     if( p_sys->i_currentframe > p_sys->i_totalframes )
167         return 0;
168
169     p_data = stream_Block( p_demux->s, p_sys->pi_seektable[p_sys->i_currentframe] );
170     if( p_data == NULL ) return 0;
171     p_data->i_dts = p_data->i_pts = (int64_t)(1 + I64C(1000000) * p_sys->i_currentframe) * TTA_FRAMETIME;
172
173     p_sys->i_currentframe++;
174
175     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
176     es_out_Send( p_demux->out, p_sys->p_es, p_data );
177
178     return 1;
179 }
180
181 /*****************************************************************************
182  * Control:
183  *****************************************************************************/
184 static int Control( demux_t *p_demux, int i_query, va_list args )
185 {
186     demux_sys_t *p_sys = p_demux->p_sys;
187     double   f, *pf;
188     int64_t i64, *pi64;
189
190     switch( i_query )
191     {
192         case DEMUX_GET_POSITION:
193             pf = (double*) va_arg( args, double* );
194             i64 = stream_Size( p_demux->s ) - p_sys->i_start;
195             if( i64 > 0 )
196             {
197                 *pf = (double)(stream_Tell( p_demux->s ) - p_sys->i_start )/ (double)i64;
198             }
199             else
200             {
201                 *pf = 0.0;
202             }
203             return VLC_SUCCESS;
204
205         case DEMUX_SET_POSITION:
206             f = (double)va_arg( args, double );
207             i64 = (int64_t)(f * (stream_Size( p_demux->s ) - p_sys->i_start));
208             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
209             if( i64 > 0 )
210             {
211                 int64_t tmp = 0;
212                 int     i;
213                 for( i=0; i < p_sys->i_totalframes && tmp+p_sys->pi_seektable[i] < i64; i++)
214                 {
215                     tmp += p_sys->pi_seektable[i];
216                 }
217                 stream_Seek( p_demux->s, tmp+p_sys->i_start );
218                 p_sys->i_currentframe = i;
219                 return VLC_SUCCESS;
220             }
221             return VLC_EGENERIC;
222
223         case DEMUX_GET_LENGTH:
224             pi64 = (int64_t*)va_arg( args, int64_t * );
225             *pi64 = I64C(1000000) * p_sys->i_totalframes * TTA_FRAMETIME;
226             return VLC_SUCCESS;
227
228         case DEMUX_GET_TIME:
229             pi64 = (int64_t*)va_arg( args, int64_t * );
230             *pi64 = I64C(1000000) * p_sys->i_currentframe * TTA_FRAMETIME;
231             return VLC_SUCCESS;
232
233         default:
234             return VLC_EGENERIC;
235     }
236 }
237