decoder: remove unused variable
[vlc.git] / modules / demux / nsc.c
blob046ef63b953c231cd2da205506bf7fe34f81f119
1 /*****************************************************************************
2 * nsc.c: NSC file demux and encoding decoder
3 *****************************************************************************
4 * Copyright (C) 2005 VLC authors and VideoLAN
6 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
7 * based on work from Jon Lech Johansen <jon@nanocrew.net>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_demux.h>
34 #include <vlc_charset.h>
36 #include <ctype.h>
37 #define MAX_LINE 16024
39 /*****************************************************************************
40 * Module descriptor
41 *****************************************************************************/
42 static int DemuxOpen ( vlc_object_t * );
43 static void DemuxClose ( vlc_object_t * );
45 vlc_module_begin ()
46 set_description( N_("Windows Media NSC metademux") )
47 set_category( CAT_INPUT )
48 set_subcategory( SUBCAT_INPUT_DEMUX )
49 set_capability( "demux", 3 )
50 set_callbacks( DemuxOpen, DemuxClose )
51 vlc_module_end ()
53 static int Demux ( demux_t *p_demux );
54 static int Control( demux_t *p_demux, int i_query, va_list args );
56 static const unsigned char inverse[ 128 ] =
58 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
59 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
60 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
61 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
62 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF,
63 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
64 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C,
65 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
66 0xFF, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E,
67 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A,
68 0x3B, 0x3C, 0x3D, 0x3E, 0xFF, 0x3F, 0xFF, 0xFF
71 static int load_byte( unsigned char encoding_type,
72 unsigned char *output, char **input,
73 unsigned char *j, unsigned char *k )
75 *output = 0;
77 if( encoding_type == 1 )
79 if( isxdigit( (unsigned char)**input ) == 0 )
80 return -1;
82 if( isdigit( (unsigned char)**input ) == 0 )
83 *output = (toupper( (unsigned char)**input ) - 7) * 16;
84 else
85 *output = **input * 16;
87 (*input)++;
89 if( isxdigit( (unsigned char)**input ) == 0 )
90 return -1;
92 if( isdigit( (unsigned char)**input ) == 0 )
93 *output |= toupper( (unsigned char)**input ) - 0x37;
94 else
95 *output |= **input - 0x30;
97 (*input)++;
99 else if( encoding_type == 2 )
101 unsigned char **uinput = (unsigned char **)input;
103 if( **uinput > 127 || inverse[ **uinput ] == 0xFF )
104 return -1;
106 if( *k == 0 )
108 if( (*uinput)[ 1 ] > 127 || inverse[ (*uinput)[ 1 ] ] == 0xFF )
109 return -1;
111 *output = (inverse[ (*uinput)[ 0 ] ] * 4) |
112 (inverse[ (*uinput)[ 1 ] ] / 16);
114 *j = inverse[ (*uinput)[ 1 ] ] * 16;
115 *k = 4;
117 (*uinput) += 2;
119 else if( *k == 2 )
121 *output = *j | inverse[ **uinput ];
123 *j = 0;
124 *k = 0;
126 (*uinput)++;
128 else if( *k == 4 )
130 *output = (inverse[ **uinput ] / 4) | *j;
132 *j = inverse[ **uinput ] * 64;
133 *k = 2;
135 (*uinput)++;
139 return 0;
142 static char *nscdec( vlc_object_t *p_demux, char* p_encoded )
144 unsigned int i;
145 unsigned char tmp;
146 unsigned char j, k;
147 unsigned int length;
148 unsigned char encoding_type;
150 unsigned char *buf16;
151 char *buf8;
153 char *p_input = p_encoded;
155 if( strlen( p_input ) < 15 )
157 msg_Err( p_demux, "input string less than 15 characters" );
158 return NULL;
161 if( load_byte( 1, &encoding_type, &p_input, NULL, NULL ) )
163 msg_Err( p_demux, "unable to get NSC encoding type" );
164 return NULL;
167 if( encoding_type != 1 && encoding_type != 2 )
169 msg_Err( p_demux, "encoding type %d is not supported",
170 encoding_type );
171 return NULL;
174 j = k = 0;
176 if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) )
178 msg_Err( p_demux, "load_byte failed" );
179 return NULL;
182 for( i = 0; i < 4; i++ )
184 if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) )
186 msg_Err( p_demux, "load_byte failed" );
187 return NULL;
191 length = 0;
192 for( i = 4; i; i-- )
194 if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) )
196 msg_Err( p_demux, "load_byte failed" );
197 return NULL;
199 length |= (unsigned int)tmp << ((i - 1) * 8);
202 if( length == 0 )
204 msg_Err( p_demux, "Length is 0" );
205 return NULL;
208 buf16 = malloc( length );
209 if( buf16 == NULL )
210 return NULL;
212 for( i = 0; i < length; i++ )
214 if( load_byte( encoding_type, &buf16[ i ], &p_input, &j, &k ) )
216 msg_Err( p_demux, "load_byte failed" );
217 free( buf16 );
218 return NULL;
222 buf8 = FromCharset( "UTF-16LE", buf16, length );
223 free( buf16 );
224 if( buf8 == NULL )
226 msg_Err( p_demux, "iconv failed" );
227 return NULL;
229 return buf8;
232 static int DemuxOpen( vlc_object_t * p_this )
234 demux_t *p_demux = (demux_t *)p_this;
235 const uint8_t *p_peek;
236 int i_size;
238 /* Lets check the content to see if this is a NSC file */
239 i_size = vlc_stream_Peek( p_demux->s, &p_peek, MAX_LINE );
240 i_size -= sizeof("NSC Format Version=") - 1;
242 if ( i_size > 0 )
244 while ( i_size && strncasecmp( (char *)p_peek, "NSC Format Version=",
245 (int) sizeof("NSC Format Version=") - 1 ) )
247 p_peek++;
248 i_size--;
250 if ( !strncasecmp( (char *)p_peek, "NSC Format Version=",
251 (int) sizeof("NSC Format Version=") -1 ) )
253 p_demux->pf_demux = Demux;
254 p_demux->pf_control = Control;
255 return VLC_SUCCESS;
258 return VLC_EGENERIC;
262 /*****************************************************************************
263 * Deactivate: frees unused data
264 *****************************************************************************/
265 static void DemuxClose( vlc_object_t *p_this )
267 VLC_UNUSED( p_this );
268 return;
271 static int ParseLine ( demux_t *p_demux, char *psz_line )
273 char *psz_bol;
274 char *psz_value;
276 psz_bol = psz_line;
277 /* Remove unnecessary tabs or spaces at the beginning of line */
278 while( *psz_bol == ' ' || *psz_bol == '\t' ||
279 *psz_bol == '\n' || *psz_bol == '\r' )
281 psz_bol++;
283 psz_value = strchr( psz_bol, '=' );
284 if( psz_value == NULL )
286 return 0; /* a [Address] or [Formats] line or something else we will ignore */
288 *psz_value = '\0';
289 psz_value++;
291 if( !strncasecmp( psz_value, "0x", 2 ) )
293 int i_value;
294 sscanf( psz_value, "%x", &i_value );
295 msg_Dbg( p_demux, "%s = %d", psz_bol, i_value );
297 else if( !strncasecmp( psz_bol, "Format", 6 ) )
299 msg_Dbg( p_demux, "%s = asf header", psz_bol );
301 else
303 /* This should be NSC encoded strings in the values */
304 char *psz_out;
305 psz_out = nscdec( (vlc_object_t *)p_demux, psz_value );
306 if( psz_out )
308 msg_Dbg( p_demux, "%s = %s", psz_bol, psz_out );
309 free( psz_out );
312 return VLC_SUCCESS;
315 /*****************************************************************************
316 * Demux: reads and demuxes data packets
317 *****************************************************************************
318 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
319 *****************************************************************************/
320 static int Demux ( demux_t *p_demux )
322 char *psz_line;
324 while( ( psz_line = vlc_stream_ReadLine( p_demux->s ) ) )
326 ParseLine( p_demux, psz_line );
327 free( psz_line );
329 return VLC_SUCCESS;
332 static int Control( demux_t *p_demux, int i_query, va_list args )
334 VLC_UNUSED( p_demux ); VLC_UNUSED( i_query ); VLC_UNUSED( args );
335 //FIXME
336 return VLC_EGENERIC;