]> git.sesse.net Git - vlc/blob - modules/demux/tta.c
41ab605cdcd8842d4334bb6a1123d1b953586393
[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/vlc.h>
32 #include <vlc_demux.h>
33 #include <vlc_codec.h>
34 #include <math.h>
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 static int  Open  ( vlc_object_t * );
40 static void Close ( vlc_object_t * );
41
42 vlc_module_begin();
43     set_shortname( "TTA" );
44     set_description( _("TTA demuxer") );
45     set_category( CAT_INPUT );
46     set_subcategory( SUBCAT_INPUT_DEMUX );
47     set_capability( "demux2", 145 );
48
49     set_callbacks( Open, Close );
50     add_shortcut( "tta" );
51 vlc_module_end();
52
53 #define TTA_FRAMETIME 1.04489795918367346939
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 static int Demux  ( demux_t * );
59 static int Control( demux_t *, int, va_list );
60
61 struct demux_sys_t
62 {
63     /* */
64     es_out_id_t *p_es;
65
66     /* */
67     int      i_totalframes;
68     int      i_currentframe;
69     uint32_t *pi_seektable;
70     int      i_datalength;
71     int      i_framelength;
72
73     /* */
74     vlc_meta_t     *p_meta;
75     int64_t        i_start;
76 };
77
78 /*****************************************************************************
79  * Open: initializes ES structures
80  *****************************************************************************/
81 static int Open( vlc_object_t * p_this )
82 {
83     demux_t     *p_demux = (demux_t*)p_this;
84     demux_sys_t *p_sys;
85     es_format_t fmt;
86     const uint8_t *p_peek;
87     uint8_t     p_header[22];
88     uint8_t     *p_seektable;
89     int         i_seektable_size = 0, i;
90     //char        psz_info[4096];
91     //module_t    *p_id3;
92
93     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
94         return VLC_EGENERIC;
95
96     if( !POKE( p_peek, "TTA1", 4 ) )
97     {
98         if( !p_demux->b_force ) return VLC_EGENERIC;
99
100         /* User forced */
101         msg_Err( p_demux, "this doesn't look like a flac stream, "
102                  "continuing anyway" );
103     }
104
105     if( stream_Read( p_demux->s, p_header, 22 ) < 22 )
106         return VLC_EGENERIC;
107
108     /* Fill p_demux fields */
109     p_demux->pf_demux = Demux;
110     p_demux->pf_control = Control;
111     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
112
113     /* Read the metadata */
114     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'T', 'T', 'A', '1' ) );
115     fmt.audio.i_channels = GetWLE( &p_header[6] );
116     fmt.audio.i_bitspersample = GetWLE( &p_header[8] );
117     fmt.audio.i_rate = GetDWLE( &p_header[10] );
118
119     p_sys->i_datalength = GetDWLE( &p_header[14] );
120     p_sys->i_framelength = TTA_FRAMETIME * fmt.audio.i_rate;
121
122     p_sys->i_totalframes = p_sys->i_datalength / p_sys->i_framelength +
123                           ((p_sys->i_datalength % p_sys->i_framelength) ? 1 : 0);
124     p_sys->i_currentframe = 0;
125
126     i_seektable_size = sizeof(uint32_t)*p_sys->i_totalframes;
127     p_seektable = (uint8_t *)malloc( i_seektable_size );
128     stream_Read( p_demux->s, p_seektable, i_seektable_size );
129     p_sys->pi_seektable = (uint32_t *)malloc(i_seektable_size);
130
131     for( i = 0; i < p_sys->i_totalframes; i++ )
132         p_sys->pi_seektable[i] = GetDWLE( &p_seektable[i*4] );
133
134     stream_Read( p_demux->s, NULL, 4 ); /* CRC */
135
136     /* Store the header and Seektable for avcodec */
137     fmt.i_extra = 22 + (p_sys->i_totalframes * 4) + 4;
138     fmt.p_extra = malloc( fmt.i_extra );
139     memcpy( (uint8_t*)fmt.p_extra, p_header, 22 );
140     memcpy( (uint8_t*)fmt.p_extra+22, p_seektable, fmt.i_extra -22 );
141
142     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
143     free( p_seektable );
144     p_sys->i_start = stream_Tell( p_demux->s );
145
146     return VLC_SUCCESS;
147 }
148
149 /*****************************************************************************
150  * Close: frees unused data
151  *****************************************************************************/
152 static void Close( vlc_object_t * p_this )
153 {
154     demux_t        *p_demux = (demux_t*)p_this;
155     demux_sys_t    *p_sys = p_demux->p_sys;
156
157     free( p_sys );
158 }
159
160 /*****************************************************************************
161  * Demux:
162  *****************************************************************************
163  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
164  *****************************************************************************/
165 static int Demux( demux_t *p_demux )
166 {
167     demux_sys_t *p_sys = p_demux->p_sys;
168     block_t     *p_data;
169
170     if( p_sys->i_currentframe > p_sys->i_totalframes )
171         return 0;
172
173     p_data = stream_Block( p_demux->s, p_sys->pi_seektable[p_sys->i_currentframe] );
174     if( p_data == NULL ) return 0;
175     p_data->i_dts = p_data->i_pts = (int64_t)(1 + I64C(1000000) * p_sys->i_currentframe) * TTA_FRAMETIME;
176
177     p_sys->i_currentframe++;
178
179     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
180     es_out_Send( p_demux->out, p_sys->p_es, p_data );
181
182     return 1;
183 }
184
185 /*****************************************************************************
186  * Control:
187  *****************************************************************************/
188 static int Control( demux_t *p_demux, int i_query, va_list args )
189 {
190     demux_sys_t *p_sys = p_demux->p_sys;
191     double   f, *pf;
192     int64_t i64, *pi64;
193
194     switch( i_query )
195     {
196         case DEMUX_GET_POSITION:
197             pf = (double*) va_arg( args, double* );
198             i64 = stream_Size( p_demux->s ) - p_sys->i_start;
199             if( i64 > 0 )
200             {
201                 *pf = (double)(stream_Tell( p_demux->s ) - p_sys->i_start )/ (double)i64;
202             }
203             else
204             {
205                 *pf = 0.0;
206             }
207             return VLC_SUCCESS;
208
209         case DEMUX_SET_POSITION:
210             f = (double)va_arg( args, double );
211             i64 = (int64_t)(f * (stream_Size( p_demux->s ) - p_sys->i_start));
212             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
213             if( i64 > 0 )
214             {
215                 int64_t tmp = 0;
216                 int     i;
217                 for( i=0; i < p_sys->i_totalframes && tmp+p_sys->pi_seektable[i] < i64; i++)
218                 {
219                     tmp += p_sys->pi_seektable[i];
220                 }
221                 stream_Seek( p_demux->s, tmp+p_sys->i_start );
222                 p_sys->i_currentframe = i;
223                 return VLC_SUCCESS;
224             }
225             return VLC_EGENERIC;
226
227         case DEMUX_GET_LENGTH:
228             pi64 = (int64_t*)va_arg( args, int64_t * );
229             *pi64 = I64C(1000000) * p_sys->i_totalframes * TTA_FRAMETIME;
230             return VLC_SUCCESS;
231
232         case DEMUX_GET_TIME:
233             pi64 = (int64_t*)va_arg( args, int64_t * );
234             *pi64 = I64C(1000000) * p_sys->i_currentframe * TTA_FRAMETIME;
235             return VLC_SUCCESS;
236
237         default:
238             return VLC_EGENERIC;
239     }
240 }
241