/src/vlc/modules/demux/tta.c
Line | Count | Source |
1 | | /***************************************************************************** |
2 | | * tta.c : The Lossless True Audio parser |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2006 VLC authors and VideoLAN |
5 | | * |
6 | | * Authors: Derk-Jan Hartman <hartman at videolan dot org> |
7 | | * |
8 | | * This program is free software; you can redistribute it and/or modify it |
9 | | * under the terms of the GNU Lesser General Public License as published by |
10 | | * the Free Software Foundation; either version 2.1 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public License |
19 | | * along with this program; if not, write to the Free Software Foundation, |
20 | | * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
21 | | *****************************************************************************/ |
22 | | |
23 | | /***************************************************************************** |
24 | | * Preamble |
25 | | *****************************************************************************/ |
26 | | #ifdef HAVE_CONFIG_H |
27 | | # include "config.h" |
28 | | #endif |
29 | | |
30 | | #include <vlc_common.h> |
31 | | #include <vlc_plugin.h> |
32 | | #include <vlc_demux.h> |
33 | | #include <vlc_codec.h> |
34 | | #include <math.h> |
35 | | #include <limits.h> |
36 | | |
37 | | /***************************************************************************** |
38 | | * Module descriptor |
39 | | *****************************************************************************/ |
40 | | static int Open ( vlc_object_t * ); |
41 | | static void Close ( vlc_object_t * ); |
42 | | |
43 | 150 | vlc_module_begin () |
44 | 75 | set_shortname( "TTA" ) |
45 | 75 | set_description( N_("TTA demuxer") ) |
46 | 75 | set_subcategory( SUBCAT_INPUT_DEMUX ) |
47 | 75 | set_capability( "demux", 145 ) |
48 | | |
49 | 150 | set_callbacks( Open, Close ) |
50 | 75 | add_shortcut( "tta" ) |
51 | 75 | vlc_module_end () |
52 | | |
53 | 503 | #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 | | typedef struct |
62 | | { |
63 | | /* */ |
64 | | es_out_id_t *p_es; |
65 | | |
66 | | /* */ |
67 | | uint32_t i_totalframes; |
68 | | uint32_t i_currentframe; |
69 | | uint32_t *pi_seektable; |
70 | | uint32_t i_datalength; |
71 | | int i_framelength; |
72 | | |
73 | | /* */ |
74 | | vlc_meta_t *p_meta; |
75 | | int64_t i_start; |
76 | | } demux_sys_t; |
77 | | |
78 | | /***************************************************************************** |
79 | | * Open: initializes ES structures |
80 | | *****************************************************************************/ |
81 | | static int Open( vlc_object_t * p_this ) |
82 | 2.35k | { |
83 | 2.35k | demux_t *p_demux = (demux_t*)p_this; |
84 | 2.35k | demux_sys_t *p_sys; |
85 | 2.35k | es_format_t fmt; |
86 | 2.35k | const uint8_t *p_peek; |
87 | 2.35k | uint8_t p_header[22]; |
88 | 2.35k | uint8_t *p_fullheader; |
89 | 2.35k | int i_seektable_size = 0; |
90 | | //char psz_info[4096]; |
91 | | //module_t *p_id3; |
92 | | |
93 | 2.35k | if( vlc_stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) |
94 | 4 | return VLC_EGENERIC; |
95 | | |
96 | 2.35k | if( memcmp( p_peek, "TTA1", 4 ) ) |
97 | 2.04k | { |
98 | 2.04k | if( !p_demux->obj.force ) |
99 | 1.75k | return VLC_EGENERIC; |
100 | | |
101 | | /* User forced */ |
102 | 289 | msg_Err( p_demux, "this doesn't look like a true-audio stream, " |
103 | 289 | "continuing anyway" ); |
104 | 289 | } |
105 | | |
106 | 593 | if( vlc_stream_Read( p_demux->s, p_header, 22 ) < 22 ) |
107 | 48 | return VLC_EGENERIC; |
108 | | |
109 | | /* Fill p_demux fields */ |
110 | 545 | p_demux->pf_demux = Demux; |
111 | 545 | p_demux->pf_control = Control; |
112 | 545 | p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) ); |
113 | 545 | if( !p_sys ) |
114 | 0 | return VLC_ENOMEM; |
115 | | |
116 | 545 | p_sys->pi_seektable = NULL; |
117 | | |
118 | | /* Read the metadata */ |
119 | 545 | es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_TTA ); |
120 | 545 | fmt.audio.i_channels = GetWLE( &p_header[6] ); |
121 | 545 | fmt.audio.i_bitspersample = GetWLE( &p_header[8] ); |
122 | 545 | fmt.audio.i_rate = GetDWLE( &p_header[10] ); |
123 | 545 | if( fmt.audio.i_rate == 0 || /* Avoid divide by 0 */ |
124 | 544 | fmt.audio.i_rate > ( 1 << 20 ) /* Avoid i_framelength overflow */ ) |
125 | 42 | { |
126 | 42 | msg_Warn( p_demux, "Wrong sample rate" ); |
127 | 42 | goto error; |
128 | 42 | } |
129 | | |
130 | 503 | p_sys->i_datalength = GetDWLE( &p_header[14] ); |
131 | 503 | p_sys->i_framelength = TTA_FRAMETIME * fmt.audio.i_rate; |
132 | | |
133 | 503 | p_sys->i_totalframes = p_sys->i_datalength / p_sys->i_framelength + |
134 | 503 | ((p_sys->i_datalength % p_sys->i_framelength) != 0); |
135 | 503 | p_sys->i_currentframe = 0; |
136 | 503 | if( (INT_MAX - 22 - 4) / sizeof(*p_sys->pi_seektable) < p_sys->i_totalframes ) |
137 | 6 | goto error; |
138 | | |
139 | 497 | i_seektable_size = sizeof(*p_sys->pi_seektable)*p_sys->i_totalframes; |
140 | | |
141 | | /* Store the header and Seektable for avcodec */ |
142 | 497 | fmt.i_extra = 22 + i_seektable_size + 4; |
143 | 497 | fmt.p_extra = p_fullheader = malloc( fmt.i_extra ); |
144 | 497 | if( !p_fullheader ) |
145 | 0 | { |
146 | 0 | fmt.i_extra = 0; |
147 | 0 | goto error; |
148 | 0 | } |
149 | | |
150 | 497 | memcpy( p_fullheader, p_header, 22 ); |
151 | 497 | p_fullheader += 22; |
152 | 497 | if( vlc_stream_Read( p_demux->s, p_fullheader, i_seektable_size ) |
153 | 497 | != i_seektable_size ) |
154 | 109 | goto error; |
155 | | |
156 | 388 | p_sys->pi_seektable = calloc( p_sys->i_totalframes, sizeof(*p_sys->pi_seektable) ); |
157 | 388 | if( !p_sys->pi_seektable ) |
158 | 0 | goto error; |
159 | 564k | for( uint32_t i = 0; i < p_sys->i_totalframes; i++ ) |
160 | 564k | { |
161 | 564k | p_sys->pi_seektable[i] = GetDWLE( p_fullheader ); |
162 | 564k | p_fullheader += 4; |
163 | 564k | } |
164 | | |
165 | 388 | if( 4 != vlc_stream_Read( p_demux->s, p_fullheader, 4 ) ) /* CRC */ |
166 | 18 | goto error; |
167 | 370 | p_fullheader += 4; |
168 | | |
169 | 370 | p_sys->p_es = es_out_Add( p_demux->out, &fmt ); |
170 | 370 | if( unlikely(!p_sys->p_es) ) |
171 | 0 | goto error; |
172 | | |
173 | 370 | p_sys->i_start = p_fullheader - (uint8_t *)fmt.p_extra; |
174 | 370 | es_format_Clean( &fmt ); |
175 | | |
176 | 370 | return VLC_SUCCESS; |
177 | 175 | error: |
178 | 175 | es_format_Clean( &fmt ); |
179 | 175 | Close( p_this ); |
180 | 175 | return VLC_EGENERIC; |
181 | 370 | } |
182 | | |
183 | | /***************************************************************************** |
184 | | * Close: frees unused data |
185 | | *****************************************************************************/ |
186 | | static void Close( vlc_object_t * p_this ) |
187 | 545 | { |
188 | 545 | demux_t *p_demux = (demux_t*)p_this; |
189 | 545 | demux_sys_t *p_sys = p_demux->p_sys; |
190 | | |
191 | 545 | free( p_sys->pi_seektable ); |
192 | 545 | free( p_sys ); |
193 | 545 | } |
194 | | |
195 | | /***************************************************************************** |
196 | | * Demux: |
197 | | ***************************************************************************** |
198 | | * Returns -1 in case of error, 0 in case of EOF, 1 otherwise |
199 | | *****************************************************************************/ |
200 | | static int Demux( demux_t *p_demux ) |
201 | 46.9k | { |
202 | 46.9k | demux_sys_t *p_sys = p_demux->p_sys; |
203 | 46.9k | block_t *p_data; |
204 | | |
205 | 46.9k | if( p_sys->i_currentframe >= p_sys->i_totalframes ) |
206 | 155 | return VLC_DEMUXER_EOF; |
207 | | |
208 | 46.7k | p_data = vlc_stream_Block( p_demux->s, |
209 | 46.7k | p_sys->pi_seektable[p_sys->i_currentframe] ); |
210 | 46.7k | if( p_data == NULL ) |
211 | 215 | return VLC_DEMUXER_EOF; |
212 | 46.5k | p_data->i_dts = p_data->i_pts = VLC_TICK_0 + vlc_tick_from_sec( p_sys->i_currentframe * TTA_FRAMETIME ); |
213 | | |
214 | 46.5k | p_sys->i_currentframe++; |
215 | | |
216 | 46.5k | es_out_SetPCR( p_demux->out, p_data->i_dts ); |
217 | 46.5k | if( p_sys->p_es ) |
218 | 46.5k | es_out_Send( p_demux->out, p_sys->p_es, p_data ); |
219 | | |
220 | 46.5k | return VLC_DEMUXER_SUCCESS; |
221 | 46.7k | } |
222 | | |
223 | | /***************************************************************************** |
224 | | * Control: |
225 | | *****************************************************************************/ |
226 | | static int Control( demux_t *p_demux, int i_query, va_list args ) |
227 | 0 | { |
228 | 0 | demux_sys_t *p_sys = p_demux->p_sys; |
229 | 0 | double f, *pf; |
230 | 0 | int64_t i64; |
231 | |
|
232 | 0 | switch( i_query ) |
233 | 0 | { |
234 | 0 | case DEMUX_CAN_SEEK: |
235 | 0 | return vlc_stream_vaControl( p_demux->s, i_query, args ); |
236 | | |
237 | 0 | case DEMUX_GET_POSITION: |
238 | 0 | pf = va_arg( args, double * ); |
239 | 0 | i64 = stream_Size( p_demux->s ) - p_sys->i_start; |
240 | 0 | if( i64 > 0 ) |
241 | 0 | { |
242 | 0 | *pf = (double)(vlc_stream_Tell( p_demux->s ) - p_sys->i_start )/ (double)i64; |
243 | 0 | } |
244 | 0 | else |
245 | 0 | { |
246 | 0 | *pf = 0.0; |
247 | 0 | } |
248 | 0 | return VLC_SUCCESS; |
249 | | |
250 | 0 | case DEMUX_SET_POSITION: |
251 | 0 | f = va_arg( args, double ); |
252 | 0 | i64 = (int64_t)(f * (stream_Size( p_demux->s ) - p_sys->i_start)); |
253 | 0 | if( i64 > 0 ) |
254 | 0 | { |
255 | 0 | int64_t tmp = 0; |
256 | 0 | uint32_t i; |
257 | 0 | for( i=0; i < p_sys->i_totalframes && tmp+p_sys->pi_seektable[i] < i64; i++) |
258 | 0 | { |
259 | 0 | tmp += p_sys->pi_seektable[i]; |
260 | 0 | } |
261 | 0 | if( vlc_stream_Seek( p_demux->s, tmp+p_sys->i_start ) ) |
262 | 0 | return VLC_EGENERIC; |
263 | 0 | p_sys->i_currentframe = i; |
264 | 0 | return VLC_SUCCESS; |
265 | 0 | } |
266 | 0 | return VLC_EGENERIC; |
267 | | |
268 | 0 | case DEMUX_GET_LENGTH: |
269 | 0 | *va_arg( args, vlc_tick_t * ) = |
270 | 0 | vlc_tick_from_sec( p_sys->i_totalframes * TTA_FRAMETIME ); |
271 | 0 | return VLC_SUCCESS; |
272 | | |
273 | 0 | case DEMUX_GET_TIME: |
274 | 0 | *va_arg( args, vlc_tick_t * ) = vlc_tick_from_sec( p_sys->i_currentframe * TTA_FRAMETIME ); |
275 | 0 | return VLC_SUCCESS; |
276 | | |
277 | 0 | case DEMUX_CAN_PAUSE: |
278 | 0 | case DEMUX_SET_PAUSE_STATE: |
279 | 0 | case DEMUX_CAN_CONTROL_PACE: |
280 | 0 | case DEMUX_GET_PTS_DELAY: |
281 | 0 | return demux_vaControlHelper( p_demux->s, 0, p_sys->i_datalength, |
282 | 0 | 0, p_sys->i_framelength, i_query, args ); |
283 | | |
284 | 0 | default: |
285 | 0 | return VLC_EGENERIC; |
286 | 0 | } |
287 | 0 | } |
288 | | |