]> git.sesse.net Git - vlc/blob - modules/demux/tta.c
Check malloc return value and fix a memory leak.
[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     if( !p_sys )
113         return VLC_EGENERIC;
114
115     /* Read the metadata */
116     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'T', 'T', 'A', '1' ) );
117     fmt.audio.i_channels = GetWLE( &p_header[6] );
118     fmt.audio.i_bitspersample = GetWLE( &p_header[8] );
119     fmt.audio.i_rate = GetDWLE( &p_header[10] );
120
121     p_sys->i_datalength = GetDWLE( &p_header[14] );
122     p_sys->i_framelength = TTA_FRAMETIME * fmt.audio.i_rate;
123
124     p_sys->i_totalframes = p_sys->i_datalength / p_sys->i_framelength +
125                           ((p_sys->i_datalength % p_sys->i_framelength) ? 1 : 0);
126     p_sys->i_currentframe = 0;
127
128     i_seektable_size = sizeof(uint32_t)*p_sys->i_totalframes;
129     p_seektable = (uint8_t *)malloc( i_seektable_size );
130     if( !p_seektable )
131     {
132         free( p_sys );
133         return VLC_EGENERIC;
134     }
135
136     stream_Read( p_demux->s, p_seektable, i_seektable_size );
137     p_sys->pi_seektable = (uint32_t *)malloc( i_seektable_size );
138     if( !p_sys->pi_seektable )
139     {
140         free( p_seektable );
141         free( p_sys );
142         return VLC_EGENERIC;
143     }
144
145     for( i = 0; i < p_sys->i_totalframes; i++ )
146         p_sys->pi_seektable[i] = GetDWLE( &p_seektable[i*4] );
147
148     stream_Read( p_demux->s, NULL, 4 ); /* CRC */
149
150     /* Store the header and Seektable for avcodec */
151     fmt.i_extra = 22 + (p_sys->i_totalframes * 4) + 4;
152     fmt.p_extra = malloc( fmt.i_extra );
153     if( !fmt.p_extra )
154     {
155         free( p_sys->pi_seektable );
156         free( p_seektable );
157         free( p_sys );
158         return VLC_EGENERIC;
159     }
160     memcpy( (uint8_t*)fmt.p_extra, p_header, 22 );
161     memcpy( (uint8_t*)fmt.p_extra+22, p_seektable, fmt.i_extra -22 );
162
163     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
164     free( p_seektable );
165     p_sys->i_start = stream_Tell( p_demux->s );
166
167     return VLC_SUCCESS;
168 }
169
170 /*****************************************************************************
171  * Close: frees unused data
172  *****************************************************************************/
173 static void Close( vlc_object_t * p_this )
174 {
175     demux_t        *p_demux = (demux_t*)p_this;
176     demux_sys_t    *p_sys = p_demux->p_sys;
177
178     free( p_sys->pi_seektable );
179     free( p_sys );
180 }
181
182 /*****************************************************************************
183  * Demux:
184  *****************************************************************************
185  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
186  *****************************************************************************/
187 static int Demux( demux_t *p_demux )
188 {
189     demux_sys_t *p_sys = p_demux->p_sys;
190     block_t     *p_data;
191
192     if( p_sys->i_currentframe > p_sys->i_totalframes )
193         return 0;
194
195     p_data = stream_Block( p_demux->s, p_sys->pi_seektable[p_sys->i_currentframe] );
196     if( p_data == NULL ) return 0;
197     p_data->i_dts = p_data->i_pts = (int64_t)(1 + I64C(1000000) * p_sys->i_currentframe) * TTA_FRAMETIME;
198
199     p_sys->i_currentframe++;
200
201     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
202     es_out_Send( p_demux->out, p_sys->p_es, p_data );
203
204     return 1;
205 }
206
207 /*****************************************************************************
208  * Control:
209  *****************************************************************************/
210 static int Control( demux_t *p_demux, int i_query, va_list args )
211 {
212     demux_sys_t *p_sys = p_demux->p_sys;
213     double   f, *pf;
214     int64_t i64, *pi64;
215
216     switch( i_query )
217     {
218         case DEMUX_GET_POSITION:
219             pf = (double*) va_arg( args, double* );
220             i64 = stream_Size( p_demux->s ) - p_sys->i_start;
221             if( i64 > 0 )
222             {
223                 *pf = (double)(stream_Tell( p_demux->s ) - p_sys->i_start )/ (double)i64;
224             }
225             else
226             {
227                 *pf = 0.0;
228             }
229             return VLC_SUCCESS;
230
231         case DEMUX_SET_POSITION:
232             f = (double)va_arg( args, double );
233             i64 = (int64_t)(f * (stream_Size( p_demux->s ) - p_sys->i_start));
234             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
235             if( i64 > 0 )
236             {
237                 int64_t tmp = 0;
238                 int     i;
239                 for( i=0; i < p_sys->i_totalframes && tmp+p_sys->pi_seektable[i] < i64; i++)
240                 {
241                     tmp += p_sys->pi_seektable[i];
242                 }
243                 stream_Seek( p_demux->s, tmp+p_sys->i_start );
244                 p_sys->i_currentframe = i;
245                 return VLC_SUCCESS;
246             }
247             return VLC_EGENERIC;
248
249         case DEMUX_GET_LENGTH:
250             pi64 = (int64_t*)va_arg( args, int64_t * );
251             *pi64 = I64C(1000000) * p_sys->i_totalframes * TTA_FRAMETIME;
252             return VLC_SUCCESS;
253
254         case DEMUX_GET_TIME:
255             pi64 = (int64_t*)va_arg( args, int64_t * );
256             *pi64 = I64C(1000000) * p_sys->i_currentframe * TTA_FRAMETIME;
257             return VLC_SUCCESS;
258
259         default:
260             return VLC_EGENERIC;
261     }
262 }
263