]> git.sesse.net Git - vlc/blob - src/input/stream.c
* UTF16 and UTF32 conversion to UTF8 for stream_ReadLine(). (fixes #304)
[vlc] / src / input / stream.c
1 /*****************************************************************************
2  * stream.c
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include <stdlib.h>
25 #include <vlc/vlc.h>
26 #include <vlc/input.h>
27
28 #include "input_internal.h"
29
30 /* TODO:
31  *  - tune the 2 methods
32  *  - compute cost for seek
33  *  - improve stream mode seeking with closest segments
34  *  - ...
35  */
36
37 /* Two methods:
38  *  - using pf_block
39  *      One linked list of data read
40  *  - using pf_read
41  *      More complex scheme using mutliple track to avoid seeking
42  */
43
44 /* How many tracks we have, currently only used for stream mode */
45 #ifdef OPTIMIZE_MEMORY
46 #   define STREAM_CACHE_TRACK 1
47     /* Max size of our cache 128Ko per track */
48 #   define STREAM_CACHE_SIZE  (STREAM_CACHE_TRACK*1024*128)
49 #else
50 #   define STREAM_CACHE_TRACK 3
51     /* Max size of our cache 4Mo per track */
52 #   define STREAM_CACHE_SIZE  (4*STREAM_CACHE_TRACK*1024*1024)
53 #endif
54
55 /* How many data we try to prebuffer */
56 #define STREAM_CACHE_PREBUFFER_SIZE (32767)
57 /* Maximum time we take to pre-buffer */
58 #define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)
59
60 /* Method1: Simple, for pf_block.
61  *  We get blocks and put them in the linked list.
62  *  We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
63  */
64 #define STREAM_DATA_WAIT 40000       /* Time between before a pf_block retry */
65
66 /* Method2: A bit more complex, for pf_read
67  *  - We use ring buffers, only one if unseekable, all if seekable
68  *  - Upon seek date current ring, then search if one ring match the pos,
69  *      yes: switch to it, seek the access to match the end of the ring
70  *      no: search the ring with i_end the closer to i_pos,
71  *          if close enough, read data and use this ring
72  *          else use the oldest ring, seek and use it.
73  *
74  *  TODO: - with access non seekable: use all space available for only one ring, but
75  *          we have to support seekable/non-seekable switch on the fly.
76  *        - compute a good value for i_read_size
77  *        - ?
78  */
79 #define STREAM_READ_ATONCE 32767
80 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
81
82 typedef struct
83 {
84     int64_t i_date;
85
86     int64_t i_start;
87     int64_t i_end;
88
89     uint8_t *p_buffer;
90
91 } stream_track_t;
92
93 typedef struct
94 {
95     char     *psz_path;
96     int64_t  i_size;
97
98 } access_entry_t;
99
100 struct stream_sys_t
101 {
102     access_t    *p_access;
103
104     vlc_bool_t  b_block;    /* Block method (1) or stream */
105
106     int64_t     i_pos;      /* Current reading offset */
107
108     /* Method 1: pf_block */
109     struct
110     {
111         int64_t i_start;        /* Offset of block for p_first */
112         int     i_offset;       /* Offset for data in p_current */
113         block_t *p_current;     /* Current block */
114
115         int     i_size;         /* Total amount of data in the list */
116         block_t *p_first;
117         block_t **pp_last;
118
119     } block;
120
121     /* Method 2: for pf_read */
122     struct
123     {
124         int i_offset;   /* Buffer offset in the current track */
125         int i_tk;       /* Current track */
126         stream_track_t tk[STREAM_CACHE_TRACK];
127
128         /* Global buffer */
129         uint8_t *p_buffer;
130
131         /* */
132         int i_used; /* Used since last read */
133         int i_read_size;
134
135     } stream;
136
137     /* Peek temporary buffer */
138     int     i_peek;
139     uint8_t *p_peek;
140
141     /* Stat for both method */
142     struct
143     {
144         vlc_bool_t b_fastseek;  /* From access */
145
146         /* Stat about reading data */
147         int64_t i_read_count;
148         int64_t i_bytes;
149         int64_t i_read_time;
150
151         /* Stat about seek */
152         int     i_seek_count;
153         int64_t i_seek_time;
154
155     } stat;
156
157     /* Streams list */
158     int            i_list;
159     access_entry_t **list;
160     int            i_list_index;
161     access_t       *p_list_access;
162
163     /* Preparse mode ? */
164     vlc_bool_t      b_quick;
165 };
166
167 /* Method 1: */
168 static int  AStreamReadBlock( stream_t *s, void *p_read, int i_read );
169 static int  AStreamPeekBlock( stream_t *s, uint8_t **p_peek, int i_read );
170 static int  AStreamSeekBlock( stream_t *s, int64_t i_pos );
171 static void AStreamPrebufferBlock( stream_t *s );
172 static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof );
173
174 /* Method 2 */
175 static int  AStreamReadStream( stream_t *s, void *p_read, int i_read );
176 static int  AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read );
177 static int  AStreamSeekStream( stream_t *s, int64_t i_pos );
178 static void AStreamPrebufferStream( stream_t *s );
179 static int  AReadStream( stream_t *s, void *p_read, int i_read );
180
181 /* Common */
182 static int AStreamControl( stream_t *s, int i_query, va_list );
183 static void AStreamDestroy( stream_t *s );
184 static void UStreamDestroy( stream_t *s );
185 static int  ASeek( stream_t *s, int64_t i_pos );
186
187
188 /****************************************************************************
189  * stream_UrlNew: create a stream from a access
190  ****************************************************************************/
191 stream_t *__stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
192 {
193     char *psz_access, *psz_demux, *psz_path, *psz_dup;
194     access_t *p_access;
195     stream_t *p_res;
196
197     if( !psz_url ) return 0;
198
199     psz_dup = strdup( psz_url );
200     MRLSplit( p_parent, psz_dup, &psz_access, &psz_demux, &psz_path );
201
202     /* Now try a real access */
203     p_access = access2_New( p_parent, psz_access, psz_demux, psz_path, 0 );
204     free( psz_dup );
205
206     if( p_access == NULL )
207     {
208         msg_Err( p_parent, "no suitable access module for `%s'", psz_url );
209         return NULL;
210     }
211
212     if( !( p_res = stream_AccessNew( p_access, VLC_TRUE ) ) )
213     {
214         access2_Delete( p_access );
215         return NULL;
216     }
217
218     p_res->pf_destroy = UStreamDestroy;
219     return p_res;
220 }
221
222 stream_t *stream_AccessNew( access_t *p_access, vlc_bool_t b_quick )
223 {
224     stream_t *s = vlc_object_create( p_access, VLC_OBJECT_STREAM );
225     stream_sys_t *p_sys;
226     char *psz_list;
227
228     if( !s ) return NULL;
229
230     /* Attach it now, needed for b_die */
231     vlc_object_attach( s, p_access );
232
233     s->pf_block  = NULL;
234     s->pf_read   = NULL;    /* Set up later */
235     s->pf_peek   = NULL;
236     s->pf_control= AStreamControl;
237     s->pf_destroy = AStreamDestroy;
238
239     s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
240
241     /* UTF16 and UTF32 text file conversion */
242     s->i_char_width = 1;
243     s->b_little_endian = VLC_FALSE;
244
245     /* Common field */
246     p_sys->p_access = p_access;
247     p_sys->b_block = p_access->pf_block ? VLC_TRUE : VLC_FALSE;
248     p_sys->i_pos = p_access->info.i_pos;
249
250     /* Stats */
251     access2_Control( p_access, ACCESS_CAN_FASTSEEK, &p_sys->stat.b_fastseek );
252     p_sys->stat.i_bytes = 0;
253     p_sys->stat.i_read_time = 0;
254     p_sys->stat.i_read_count = 0;
255     p_sys->stat.i_seek_count = 0;
256     p_sys->stat.i_seek_time = 0;
257
258     p_sys->i_list = 0;
259     p_sys->list = 0;
260     p_sys->i_list_index = 0;
261     p_sys->p_list_access = 0;
262
263     p_sys->b_quick = b_quick;
264
265     /* Get the additional list of inputs if any (for concatenation) */
266     if( (psz_list = var_CreateGetString( s, "input-list" )) && *psz_list )
267     {
268         access_entry_t *p_entry = malloc( sizeof(access_entry_t) );
269         char *psz_name, *psz_parser = psz_name = psz_list;
270
271         p_sys->p_list_access = p_access;
272         p_entry->i_size = p_access->info.i_size;
273         p_entry->psz_path = strdup( p_access->psz_path );
274         TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
275         msg_Dbg( p_access, "adding file `%s', ("I64Fd" bytes)",
276                  p_entry->psz_path, p_access->info.i_size );
277
278         while( psz_name && *psz_name )
279         {
280             psz_parser = strchr( psz_name, ',' );
281             if( psz_parser ) *psz_parser = 0;
282
283             psz_name = strdup( psz_name );
284             if( psz_name )
285             {
286                 access_t *p_tmp = access2_New( p_access, p_access->psz_access,
287                                                0, psz_name, 0 );
288
289                 if( !p_tmp )
290                 {
291                     psz_name = psz_parser;
292                     if( psz_name ) psz_name++;
293                     continue;
294                 }
295
296                 msg_Dbg( p_access, "adding file `%s', ("I64Fd" bytes)",
297                          psz_name, p_tmp->info.i_size );
298
299                 p_entry = malloc( sizeof(access_entry_t) );
300                 p_entry->i_size = p_tmp->info.i_size;
301                 p_entry->psz_path = psz_name;
302                 TAB_APPEND( p_sys->i_list, p_sys->list, p_entry );
303
304                 access2_Delete( p_tmp );
305             }
306
307             psz_name = psz_parser;
308             if( psz_name ) psz_name++;
309         }
310     }
311     if( psz_list ) free( psz_list );
312
313     /* Peek */
314     p_sys->i_peek = 0;
315     p_sys->p_peek = NULL;
316
317     if( p_sys->b_block )
318     {
319         s->pf_read = AStreamReadBlock;
320         s->pf_peek = AStreamPeekBlock;
321
322         /* Init all fields of p_sys->block */
323         p_sys->block.i_start = p_sys->i_pos;
324         p_sys->block.i_offset = 0;
325         p_sys->block.p_current = NULL;
326         p_sys->block.i_size = 0;
327         p_sys->block.p_first = NULL;
328         p_sys->block.pp_last = &p_sys->block.p_first;
329
330         /* Do the prebuffering */
331         AStreamPrebufferBlock( s );
332
333         if( p_sys->block.i_size <= 0 )
334         {
335             msg_Err( s, "cannot pre fill buffer" );
336             goto error;
337         }
338     }
339     else
340     {
341         int i;
342
343         s->pf_read = AStreamReadStream;
344         s->pf_peek = AStreamPeekStream;
345
346         /* Allocate/Setup our tracks */
347         p_sys->stream.i_offset = 0;
348         p_sys->stream.i_tk     = 0;
349         p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
350         p_sys->stream.i_used   = 0;
351         access2_Control( p_access, ACCESS_GET_MTU,
352                          &p_sys->stream.i_read_size );
353         if( p_sys->stream.i_read_size <= 0 )
354             p_sys->stream.i_read_size = STREAM_READ_ATONCE;
355         else if( p_sys->stream.i_read_size <= 256 )
356             p_sys->stream.i_read_size = 256;
357
358         for( i = 0; i < STREAM_CACHE_TRACK; i++ )
359         {
360             p_sys->stream.tk[i].i_date  = 0;
361             p_sys->stream.tk[i].i_start = p_sys->i_pos;
362             p_sys->stream.tk[i].i_end   = p_sys->i_pos;
363             p_sys->stream.tk[i].p_buffer=
364                 &p_sys->stream.p_buffer[i * STREAM_CACHE_TRACK_SIZE];
365         }
366
367         /* Do the prebuffering */
368         AStreamPrebufferStream( s );
369
370         if( p_sys->stream.tk[p_sys->stream.i_tk].i_end <= 0 )
371         {
372             msg_Err( s, "cannot pre fill buffer" );
373             goto error;
374         }
375     }
376
377     return s;
378
379 error:
380     if( p_sys->b_block )
381     {
382         /* Nothing yet */
383     }
384     else
385     {
386         free( p_sys->stream.p_buffer );
387     }
388     free( s->p_sys );
389     vlc_object_detach( s );
390     vlc_object_destroy( s );
391     return NULL;
392 }
393
394 /****************************************************************************
395  * AStreamDestroy:
396  ****************************************************************************/
397 static void AStreamDestroy( stream_t *s )
398 {
399     stream_sys_t *p_sys = s->p_sys;
400
401     vlc_object_detach( s );
402
403     if( p_sys->b_block ) block_ChainRelease( p_sys->block.p_first );
404     else free( p_sys->stream.p_buffer );
405
406     if( p_sys->p_peek ) free( p_sys->p_peek );
407
408     if( p_sys->p_list_access && p_sys->p_list_access != p_sys->p_access )
409         access2_Delete( p_sys->p_list_access );
410
411     while( p_sys->i_list-- )
412     {
413         free( p_sys->list[p_sys->i_list]->psz_path );
414         free( p_sys->list[p_sys->i_list] );
415         if( !p_sys->i_list ) free( p_sys->list );
416     }
417
418     free( s->p_sys );
419     vlc_object_destroy( s );
420 }
421
422 static void UStreamDestroy( stream_t *s )
423 {
424     access_t *p_access = (access_t*)vlc_object_find( s, VLC_OBJECT_ACCESS, FIND_PARENT );
425     AStreamDestroy( s );
426     vlc_object_release( p_access );
427     access2_Delete( p_access );
428 }
429
430 /****************************************************************************
431  * stream_AccessReset:
432  ****************************************************************************/
433 void stream_AccessReset( stream_t *s )
434 {
435     stream_sys_t *p_sys = s->p_sys;
436
437     p_sys->i_pos = p_sys->p_access->info.i_pos;
438
439     if( p_sys->b_block )
440     {
441         block_ChainRelease( p_sys->block.p_first );
442
443         /* Init all fields of p_sys->block */
444         p_sys->block.i_start = p_sys->i_pos;
445         p_sys->block.i_offset = 0;
446         p_sys->block.p_current = NULL;
447         p_sys->block.i_size = 0;
448         p_sys->block.p_first = NULL;
449         p_sys->block.pp_last = &p_sys->block.p_first;
450
451         /* Do the prebuffering */
452         AStreamPrebufferBlock( s );
453     }
454     else
455     {
456         int i;
457
458         /* Setup our tracks */
459         p_sys->stream.i_offset = 0;
460         p_sys->stream.i_tk     = 0;
461         p_sys->stream.i_used   = 0;
462
463         for( i = 0; i < STREAM_CACHE_TRACK; i++ )
464         {
465             p_sys->stream.tk[i].i_date  = 0;
466             p_sys->stream.tk[i].i_start = p_sys->i_pos;
467             p_sys->stream.tk[i].i_end   = p_sys->i_pos;
468         }
469
470         /* Do the prebuffering */
471         AStreamPrebufferStream( s );
472     }
473 }
474
475 /****************************************************************************
476  * stream_AccessUpdate:
477  ****************************************************************************/
478 void stream_AccessUpdate( stream_t *s )
479 {
480     stream_sys_t *p_sys = s->p_sys;
481
482     p_sys->i_pos = p_sys->p_access->info.i_pos;
483
484     if( p_sys->i_list )
485     {
486         int i;
487         for( i = 0; i < p_sys->i_list_index; i++ )
488         {
489             p_sys->i_pos += p_sys->list[i]->i_size;
490         }
491     }
492 }
493
494 /****************************************************************************
495  * AStreamControl:
496  ****************************************************************************/
497 static int AStreamControl( stream_t *s, int i_query, va_list args )
498 {
499     stream_sys_t *p_sys = s->p_sys;
500     access_t     *p_access = p_sys->p_access;
501
502     vlc_bool_t *p_bool;
503     int64_t    *pi_64, i_64;
504     int        i_int;
505
506     switch( i_query )
507     {
508         case STREAM_GET_SIZE:
509             pi_64 = (int64_t*)va_arg( args, int64_t * );
510             if( s->p_sys->i_list )
511             {
512                 int i;
513                 *pi_64 = 0;
514                 for( i = 0; i < s->p_sys->i_list; i++ )
515                     *pi_64 += s->p_sys->list[i]->i_size;
516                 break;
517             }
518             *pi_64 = p_access->info.i_size;
519             break;
520
521         case STREAM_CAN_SEEK:
522             p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
523             access2_Control( p_access, ACCESS_CAN_SEEK, p_bool );
524             break;
525
526         case STREAM_CAN_FASTSEEK:
527             p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
528             access2_Control( p_access, ACCESS_CAN_FASTSEEK, p_bool );
529             break;
530
531         case STREAM_GET_POSITION:
532             pi_64 = (int64_t*)va_arg( args, int64_t * );
533             *pi_64 = p_sys->i_pos;
534             break;
535
536         case STREAM_SET_POSITION:
537             i_64 = (int64_t)va_arg( args, int64_t );
538             if( p_sys->b_block )
539                 return AStreamSeekBlock( s, i_64 );
540             else
541                 return AStreamSeekStream( s, i_64 );
542
543         case STREAM_GET_MTU:
544             return VLC_EGENERIC;
545
546         case STREAM_CONTROL_ACCESS:
547             i_int = (int) va_arg( args, int );
548             if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
549                 i_int != ACCESS_SET_PRIVATE_ID_CA &&
550                 i_int != ACCESS_GET_PRIVATE_ID_STATE )
551             {
552                 msg_Err( s, "Hey, what are you thinking ?"
553                             "DON'T USE STREAM_CONTROL_ACCESS !!!" );
554                 return VLC_EGENERIC;
555             }
556             return access2_vaControl( p_access, i_int, args );
557
558         default:
559             msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
560             return VLC_EGENERIC;
561     }
562     return VLC_SUCCESS;
563 }
564
565
566
567 /****************************************************************************
568  * Method 1:
569  ****************************************************************************/
570 static void AStreamPrebufferBlock( stream_t *s )
571 {
572     stream_sys_t *p_sys = s->p_sys;
573     access_t     *p_access = p_sys->p_access;
574
575     int64_t i_first = 0;
576     int64_t i_start;
577
578     msg_Dbg( s, "pre buffering" );
579     i_start = mdate();
580     for( ;; )
581     {
582         int64_t i_date = mdate();
583         vlc_bool_t b_eof;
584         block_t *b;
585
586         if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
587             ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
588         {
589             int64_t i_byterate;
590
591             /* Update stat */
592             p_sys->stat.i_bytes = p_sys->block.i_size;
593             p_sys->stat.i_read_time = i_date - i_start;
594             i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
595                          (p_sys->stat.i_read_time + 1);
596
597             msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
598                      I64Fd" kbytes/s",
599                      p_sys->stat.i_bytes,
600                      p_sys->stat.i_read_time / I64C(1000000),
601                      i_byterate / 1024 );
602             break;
603         }
604
605         /* Fetch a block */
606         if( ( b = AReadBlock( s, &b_eof ) ) == NULL )
607         {
608             if( b_eof ) break;
609
610             msleep( STREAM_DATA_WAIT );
611             continue;
612         }
613
614         while( b )
615         {
616             /* Append the block */
617             p_sys->block.i_size += b->i_buffer;
618             *p_sys->block.pp_last = b;
619             p_sys->block.pp_last = &b->p_next;
620
621             p_sys->stat.i_read_count++;
622             b = b->p_next;
623         }
624
625         if( p_access->info.b_prebuffered ) 
626         {
627             /* Access has already prebufferred - update stats and exit */
628             p_sys->stat.i_bytes = p_sys->block.i_size;
629             p_sys->stat.i_read_time = mdate() - i_start;
630             break;
631         }
632
633         if( i_first == 0 )
634         {
635             i_first = mdate();
636             msg_Dbg( s, "received first data for our buffer");
637         }
638
639     }
640
641     p_sys->block.p_current = p_sys->block.p_first;
642 }
643
644 static int AStreamRefillBlock( stream_t *s );
645
646 static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
647 {
648     stream_sys_t *p_sys = s->p_sys;
649
650     uint8_t *p_data= (uint8_t*)p_read;
651     int     i_data = 0;
652
653     /* It means EOF */
654     if( p_sys->block.p_current == NULL )
655         return 0;
656
657     if( p_read == NULL )
658     {
659         /* seek within this stream if possible, else use plain old read and discard */
660         stream_sys_t *p_sys = s->p_sys;
661         access_t     *p_access = p_sys->p_access;
662         vlc_bool_t   b_aseek;
663         access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
664         if( b_aseek )
665             return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
666     }
667
668     while( i_data < i_read )
669     {
670         int i_current =
671             p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
672         int i_copy = __MIN( i_current, i_read - i_data);
673
674         /* Copy data */
675         if( p_data )
676         {
677             memcpy( p_data,
678                     &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
679                     i_copy );
680             p_data += i_copy;
681         }
682         i_data += i_copy;
683
684         p_sys->block.i_offset += i_copy;
685         if( p_sys->block.i_offset >= p_sys->block.p_current->i_buffer )
686         {
687             /* Current block is now empty, switch to next */
688             if( p_sys->block.p_current )
689             {
690                 p_sys->block.i_offset = 0;
691                 p_sys->block.p_current = p_sys->block.p_current->p_next;
692             }
693             /*Get a new block */
694             if( AStreamRefillBlock( s ) )
695             {
696                 break;
697             }
698         }
699     }
700
701     p_sys->i_pos += i_data;
702     return i_data;
703 }
704
705 static int AStreamPeekBlock( stream_t *s, uint8_t **pp_peek, int i_read )
706 {
707     stream_sys_t *p_sys = s->p_sys;
708     uint8_t *p_data;
709     int      i_data = 0;
710     block_t *b;
711     int      i_offset;
712
713     if( p_sys->block.p_current == NULL ) return 0; /* EOF */
714
715     /* We can directly give a pointer over our buffer */
716     if( i_read <= p_sys->block.p_current->i_buffer - p_sys->block.i_offset )
717     {
718         *pp_peek = &p_sys->block.p_current->p_buffer[p_sys->block.i_offset];
719         return i_read;
720     }
721
722     /* We need to create a local copy */
723     if( p_sys->i_peek < i_read )
724     {
725         p_sys->p_peek = realloc( p_sys->p_peek, i_read );
726         if( !p_sys->p_peek )
727         {
728             p_sys->i_peek = 0;
729             return 0;
730         }
731         p_sys->i_peek = i_read;
732     }
733
734     /* Fill enough data */
735     while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
736            < i_read )
737     {
738         block_t **pp_last = p_sys->block.pp_last;
739
740         if( AStreamRefillBlock( s ) ) break;
741
742         /* Our buffer are probably filled enough, don't try anymore */
743         if( pp_last == p_sys->block.pp_last ) break;
744     }
745
746     /* Copy what we have */
747     b = p_sys->block.p_current;
748     i_offset = p_sys->block.i_offset;
749     p_data = p_sys->p_peek;
750
751     while( b && i_data < i_read )
752     {
753         int i_current = b->i_buffer - i_offset;
754         int i_copy = __MIN( i_current, i_read - i_data );
755
756         memcpy( p_data, &b->p_buffer[i_offset], i_copy );
757         i_data += i_copy;
758         p_data += i_copy;
759         i_offset += i_copy;
760
761         if( i_offset >= b->i_buffer )
762         {
763             i_offset = 0;
764             b = b->p_next;
765         }
766     }
767
768     *pp_peek = p_sys->p_peek;
769     return i_data;
770 }
771
772 static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
773 {
774     stream_sys_t *p_sys = s->p_sys;
775     access_t   *p_access = p_sys->p_access;
776     int64_t    i_offset = i_pos - p_sys->block.i_start;
777     vlc_bool_t b_seek;
778
779     /* We already have thoses data, just update p_current/i_offset */
780     if( i_offset >= 0 && i_offset < p_sys->block.i_size )
781     {
782         block_t *b = p_sys->block.p_first;
783         int i_current = 0;
784
785         while( i_current + b->i_buffer < i_offset )
786         {
787             i_current += b->i_buffer;
788             b = b->p_next;
789         }
790
791         p_sys->block.p_current = b;
792         p_sys->block.i_offset = i_offset - i_current;
793
794         p_sys->i_pos = i_pos;
795
796         return VLC_SUCCESS;
797     }
798
799     /* We may need to seek or to read data */
800     if( i_offset < 0 )
801     {
802         vlc_bool_t b_aseek;
803         access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
804
805         if( !b_aseek )
806         {
807             msg_Err( s, "backward seek impossible (access non seekable)" );
808             return VLC_EGENERIC;
809         }
810
811         b_seek = VLC_TRUE;
812     }
813     else
814     {
815         vlc_bool_t b_aseek, b_aseekfast;
816
817         access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
818         access2_Control( p_access, ACCESS_CAN_FASTSEEK, &b_aseekfast );
819
820         if( !b_aseek )
821         {
822             b_seek = VLC_FALSE;
823             msg_Warn( s, I64Fd" bytes need to be skipped "
824                       "(access non seekable)",
825                       i_offset - p_sys->block.i_size );
826         }
827         else
828         {
829             int64_t i_skip = i_offset - p_sys->block.i_size;
830
831             /* Avg bytes per packets */
832             int i_avg = p_sys->stat.i_bytes / p_sys->stat.i_read_count;
833             /* TODO compute a seek cost instead of fixed threshold */
834             int i_th = b_aseekfast ? 1 : 5;
835
836             if( i_skip <= i_th * i_avg &&
837                 i_skip < STREAM_CACHE_SIZE )
838                 b_seek = VLC_FALSE;
839             else
840                 b_seek = VLC_TRUE;
841
842             msg_Dbg( s, "b_seek=%d th*avg=%d skip="I64Fd,
843                      b_seek, i_th*i_avg, i_skip );
844         }
845     }
846
847     if( b_seek )
848     {
849         int64_t i_start, i_end;
850         /* Do the access seek */
851         i_start = mdate();
852         if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
853         i_end = mdate();
854
855         /* Release data */
856         block_ChainRelease( p_sys->block.p_first );
857
858         /* Reinit */
859         p_sys->block.i_start = p_sys->i_pos = i_pos;
860         p_sys->block.i_offset = 0;
861         p_sys->block.p_current = NULL;
862         p_sys->block.i_size = 0;
863         p_sys->block.p_first = NULL;
864         p_sys->block.pp_last = &p_sys->block.p_first;
865
866         /* Refill a block */
867         if( AStreamRefillBlock( s ) )
868         {
869             msg_Err( s, "cannot re fill buffer" );
870             return VLC_EGENERIC;
871         }
872         /* Update stat */
873         p_sys->stat.i_seek_time += i_end - i_start;
874         p_sys->stat.i_seek_count++;
875         return VLC_SUCCESS;
876     }
877     else
878     {
879         /* Read enought data */
880         while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
881         {
882             if( AStreamRefillBlock( s ) )
883             {
884                 msg_Err( s, "can't read enough data in seek" );
885                 return VLC_EGENERIC;
886             }
887             while( p_sys->block.p_current &&
888                    p_sys->i_pos + p_sys->block.p_current->i_buffer < i_pos )
889             {
890                 p_sys->i_pos += p_sys->block.p_current->i_buffer;
891                 p_sys->block.p_current = p_sys->block.p_current->p_next;
892             }
893         }
894
895         p_sys->block.i_offset = i_pos - p_sys->i_pos;
896         p_sys->i_pos = i_pos;
897
898         /* TODO read data */
899         return VLC_SUCCESS;
900     }
901
902     return VLC_EGENERIC;
903 }
904
905 static int AStreamRefillBlock( stream_t *s )
906 {
907     stream_sys_t *p_sys = s->p_sys;
908     int64_t      i_start, i_stop;
909     block_t      *b;
910
911     /* Release data */
912     while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
913            p_sys->block.p_first != p_sys->block.p_current )
914     {
915         block_t *b = p_sys->block.p_first;
916
917         p_sys->block.i_start += b->i_buffer;
918         p_sys->block.i_size  -= b->i_buffer;
919         p_sys->block.p_first  = b->p_next;
920
921         block_Release( b );
922     }
923     if( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
924         p_sys->block.p_current == p_sys->block.p_first &&
925         p_sys->block.p_current->p_next )    /* At least 2 packets */
926     {
927         /* Enough data, don't read more */
928         return VLC_SUCCESS;
929     }
930
931     /* Now read a new block */
932     i_start = mdate();
933     for( ;; )
934     {
935         vlc_bool_t b_eof;
936
937         if( s->b_die ) return VLC_EGENERIC;
938
939
940         /* Fetch a block */
941         if( ( b = AReadBlock( s, &b_eof ) ) ) break;
942
943         if( b_eof ) return VLC_EGENERIC;
944
945         msleep( STREAM_DATA_WAIT );
946     }
947
948     while( b )
949     {
950         i_stop = mdate();
951
952         /* Append the block */
953         p_sys->block.i_size += b->i_buffer;
954         *p_sys->block.pp_last = b;
955         p_sys->block.pp_last = &b->p_next;
956
957         /* Fix p_current */
958         if( p_sys->block.p_current == NULL )
959             p_sys->block.p_current = b;
960
961         /* Update stat */
962         p_sys->stat.i_bytes += b->i_buffer;
963         p_sys->stat.i_read_time += i_stop - i_start;
964         p_sys->stat.i_read_count++;
965
966         b = b->p_next;
967         i_start = mdate();
968     }
969     return VLC_SUCCESS;
970 }
971
972
973 /****************************************************************************
974  * Method 2:
975  ****************************************************************************/
976 static int AStreamRefillStream( stream_t *s );
977
978 static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
979 {
980     stream_sys_t *p_sys = s->p_sys;
981     stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
982
983     uint8_t *p_data = (uint8_t *)p_read;
984     int      i_data = 0;
985
986     if( tk->i_start >= tk->i_end ) return 0; /* EOF */
987
988     if( p_read == NULL )
989     {
990         /* seek within this stream if possible, else use plain old read and discard */
991         stream_sys_t *p_sys = s->p_sys;
992         access_t     *p_access = p_sys->p_access;
993         vlc_bool_t   b_aseek;
994         access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
995         if( b_aseek )
996             return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
997     }
998
999 #if 0
1000     msg_Dbg( s, "AStreamReadStream: %d pos="I64Fd" tk=%d start="I64Fd
1001              " offset=%d end="I64Fd,
1002              i_read, p_sys->i_pos, p_sys->stream.i_tk,
1003              tk->i_start, p_sys->stream.i_offset, tk->i_end );
1004 #endif
1005
1006     while( i_data < i_read )
1007     {
1008         int i_off = (tk->i_start + p_sys->stream.i_offset) %
1009                     STREAM_CACHE_TRACK_SIZE;
1010         int i_current =
1011             __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
1012                    STREAM_CACHE_TRACK_SIZE - i_off );
1013         int i_copy = __MIN( i_current, i_read - i_data );
1014
1015         if( i_copy <= 0 ) break; /* EOF */
1016
1017         /* Copy data */
1018         /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
1019         if( p_data )
1020         {
1021             memcpy( p_data, &tk->p_buffer[i_off], i_copy );
1022             p_data += i_copy;
1023         }
1024         i_data += i_copy;
1025         p_sys->stream.i_offset += i_copy;
1026
1027         /* Update pos now */
1028         p_sys->i_pos += i_copy;
1029
1030         /* */
1031         p_sys->stream.i_used += i_copy;
1032         if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
1033             p_sys->stream.i_used >= p_sys->stream.i_read_size )
1034         {
1035             if( AStreamRefillStream( s ) )
1036             {
1037                 /* EOF */
1038                 if( tk->i_start >= tk->i_end ) break;
1039             }
1040         }
1041     }
1042
1043     return i_data;
1044 }
1045
1046 static int AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read )
1047 {
1048     stream_sys_t *p_sys = s->p_sys;
1049     stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1050     int64_t i_off;
1051
1052     if( tk->i_start >= tk->i_end ) return 0; /* EOF */
1053
1054 #if 0
1055     msg_Dbg( s, "AStreamPeekStream: %d pos="I64Fd" tk=%d "
1056              "start="I64Fd" offset=%d end="I64Fd,
1057              i_read, p_sys->i_pos, p_sys->stream.i_tk,
1058              tk->i_start, p_sys->stream.i_offset, tk->i_end );
1059 #endif
1060
1061     /* Avoid problem, but that should *never* happen */
1062     if( i_read > STREAM_CACHE_TRACK_SIZE / 2 )
1063         i_read = STREAM_CACHE_TRACK_SIZE / 2;
1064
1065     while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1066     {
1067         if( p_sys->stream.i_used <= 1 )
1068         {
1069             /* Be sure we will read something */
1070             p_sys->stream.i_used += i_read -
1071                 (tk->i_end - tk->i_start - p_sys->stream.i_offset);
1072         }
1073         if( AStreamRefillStream( s ) ) break;
1074     }
1075
1076     if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
1077         i_read = tk->i_end - tk->i_start - p_sys->stream.i_offset;
1078
1079     /* Now, direct pointer or a copy ? */
1080     i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
1081     if( i_off + i_read <= STREAM_CACHE_TRACK_SIZE )
1082     {
1083         *pp_peek = &tk->p_buffer[i_off];
1084         return i_read;
1085     }
1086
1087     if( p_sys->i_peek < i_read )
1088     {
1089         p_sys->p_peek = realloc( p_sys->p_peek, i_read );
1090         if( !p_sys->p_peek )
1091         {
1092             p_sys->i_peek = 0;
1093             return 0;
1094         }
1095         p_sys->i_peek = i_read;
1096     }
1097
1098     memcpy( p_sys->p_peek, &tk->p_buffer[i_off],
1099             STREAM_CACHE_TRACK_SIZE - i_off );
1100     memcpy( &p_sys->p_peek[STREAM_CACHE_TRACK_SIZE - i_off],
1101             &tk->p_buffer[0], i_read - (STREAM_CACHE_TRACK_SIZE - i_off) );
1102
1103     *pp_peek = p_sys->p_peek;
1104     return i_read;
1105 }
1106
1107 static int AStreamSeekStream( stream_t *s, int64_t i_pos )
1108 {
1109     stream_sys_t *p_sys = s->p_sys;
1110     access_t     *p_access = p_sys->p_access;
1111     vlc_bool_t   b_aseek;
1112     vlc_bool_t   b_afastseek;
1113     int i_maxth;
1114     int i_new;
1115     int i;
1116
1117 #if 0
1118     msg_Dbg( s, "AStreamSeekStream: to "I64Fd" pos="I64Fd
1119              "tk=%d start="I64Fd" offset=%d end="I64Fd,
1120              i_pos, p_sys->i_pos, p_sys->stream.i_tk,
1121              p_sys->stream.tk[p_sys->stream.i_tk].i_start,
1122              p_sys->stream.i_offset,
1123              p_sys->stream.tk[p_sys->stream.i_tk].i_end );
1124 #endif
1125
1126
1127     /* Seek in our current track ? */
1128     if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
1129         i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
1130     {
1131         //msg_Dbg( s, "AStreamSeekStream: current track" );
1132         p_sys->i_pos = i_pos;
1133         p_sys->stream.i_offset = i_pos - p_sys->stream.tk[p_sys->stream.i_tk].i_start;
1134         return VLC_SUCCESS;
1135     }
1136
1137     access2_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
1138     if( !b_aseek )
1139     {
1140         /* We can't do nothing */
1141         msg_Dbg( s, "AStreamSeekStream: can't seek" );
1142         return VLC_EGENERIC;
1143     }
1144
1145     /* Date the current track */
1146     p_sys->stream.tk[p_sys->stream.i_tk].i_date = mdate();
1147
1148     /* Try to reuse already read data */
1149     for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1150     {
1151         stream_track_t *tk = &p_sys->stream.tk[i];
1152
1153         if( i_pos >= tk->i_start && i_pos <= tk->i_end )
1154         {
1155 #if 0
1156             msg_Dbg( s, "AStreamSeekStream: reusing %d start="I64Fd
1157                      " end="I64Fd, i, tk->i_start, tk->i_end );
1158 #endif
1159             /* Seek at the end of the buffer */
1160             if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
1161
1162             /* That's it */
1163             p_sys->i_pos = i_pos;
1164             p_sys->stream.i_tk = i;
1165             p_sys->stream.i_offset = i_pos - tk->i_start;
1166
1167             if( p_sys->stream.i_used < 1024 )
1168                 p_sys->stream.i_used = 1024;
1169
1170             if( AStreamRefillStream( s ) && i_pos == tk->i_end )
1171                 return VLC_EGENERIC;
1172
1173             return VLC_SUCCESS;
1174         }
1175     }
1176
1177     access2_Control( p_access, ACCESS_CAN_SEEK, &b_afastseek );
1178     /* FIXME compute seek cost (instead of static 'stupid' value) */
1179     i_maxth = __MIN( p_sys->stream.i_read_size, STREAM_READ_ATONCE / 2 );
1180     if( !b_afastseek )
1181         i_maxth *= 3;
1182
1183     /* FIXME TODO */
1184 #if 0
1185     /* Search closest segment TODO */
1186     for( i = 0; i < STREAM_CACHE_TRACK; i++ )
1187     {
1188         stream_track_t *tk = &p_sys->stream.tk[i];
1189
1190         if( i_pos + i_maxth >= tk->i_start )
1191         {
1192             msg_Dbg( s, "good segment before current pos, TODO" );
1193         }
1194         if( i_pos - i_maxth <= tk->i_end )
1195         {
1196             msg_Dbg( s, "good segment after current pos, TODO" );
1197         }
1198     }
1199 #endif
1200
1201     /* Nothing good, seek and choose oldest segment */
1202     if( ASeek( s, i_pos ) ) return VLC_EGENERIC;
1203     p_sys->i_pos = i_pos;
1204
1205     i_new = 0;
1206     for( i = 1; i < STREAM_CACHE_TRACK; i++ )
1207     {
1208         if( p_sys->stream.tk[i].i_date < p_sys->stream.tk[i_new].i_date )
1209             i_new = i;
1210     }
1211
1212     /* Reset the segment */
1213     p_sys->stream.i_tk     = i_new;
1214     p_sys->stream.i_offset =  0;
1215     p_sys->stream.tk[i_new].i_start = i_pos;
1216     p_sys->stream.tk[i_new].i_end   = i_pos;
1217
1218     /* Read data */
1219     if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2 )
1220         p_sys->stream.i_used = STREAM_READ_ATONCE / 2;
1221
1222     if( AStreamRefillStream( s ) )
1223         return VLC_EGENERIC;
1224
1225     return VLC_SUCCESS;
1226 }
1227
1228 static int AStreamRefillStream( stream_t *s )
1229 {
1230     stream_sys_t *p_sys = s->p_sys;
1231     stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1232
1233     /* We read but won't increase i_start after initial start + offset */
1234     int i_toread =
1235         __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
1236                (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
1237     vlc_bool_t b_read = VLC_FALSE;
1238     int64_t i_start, i_stop;
1239
1240     if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
1241
1242     /* msg_Dbg( s, "AStreamRefillStream: toread=%d", i_toread ); */
1243
1244     i_start = mdate();
1245     while( i_toread > 0 )
1246     {
1247         int i_off = tk->i_end % STREAM_CACHE_TRACK_SIZE;
1248         int i_read;
1249
1250         if( s->b_die )
1251             return VLC_EGENERIC;
1252
1253         i_read = __MIN( i_toread, STREAM_CACHE_TRACK_SIZE - i_off );
1254         i_read = AReadStream( s, &tk->p_buffer[i_off], i_read );
1255
1256         /* msg_Dbg( s, "AStreamRefillStream: read=%d", i_read ); */
1257         if( i_read <  0 )
1258         {
1259             msleep( STREAM_DATA_WAIT );
1260             continue;
1261         }
1262         else if( i_read == 0 )
1263         {
1264             if( !b_read ) return VLC_EGENERIC;
1265             return VLC_SUCCESS;
1266         }
1267         b_read = VLC_TRUE;
1268
1269         /* Update end */
1270         tk->i_end += i_read;
1271
1272         /* Windows of STREAM_CACHE_TRACK_SIZE */
1273         if( tk->i_end - tk->i_start > STREAM_CACHE_TRACK_SIZE )
1274         {
1275             int i_invalid = tk->i_end - tk->i_start - STREAM_CACHE_TRACK_SIZE;
1276
1277             tk->i_start += i_invalid;
1278             p_sys->stream.i_offset -= i_invalid;
1279         }
1280
1281         i_toread -= i_read;
1282         p_sys->stream.i_used -= i_read;
1283
1284         p_sys->stat.i_bytes += i_read;
1285         p_sys->stat.i_read_count++;
1286     }
1287     i_stop = mdate();
1288
1289     p_sys->stat.i_read_time += i_stop - i_start;
1290
1291     return VLC_SUCCESS;
1292 }
1293
1294 static void AStreamPrebufferStream( stream_t *s )
1295 {
1296     stream_sys_t *p_sys = s->p_sys;
1297     access_t     *p_access = p_sys->p_access;
1298
1299     int64_t i_first = 0;
1300     int64_t i_start;
1301     int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
1302         ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
1303           STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
1304
1305     msg_Dbg( s, "pre buffering" );
1306     i_start = mdate();
1307     for( ;; )
1308     {
1309         stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
1310
1311         int64_t i_date = mdate();
1312         int i_read;
1313
1314         if( s->b_die || tk->i_end >= i_prebuffer ||
1315             (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
1316         {
1317             int64_t i_byterate;
1318
1319             /* Update stat */
1320             p_sys->stat.i_bytes = tk->i_end - tk->i_start;
1321             p_sys->stat.i_read_time = i_date - i_start;
1322             i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
1323                          (p_sys->stat.i_read_time+1);
1324
1325             msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
1326                      I64Fd" kbytes/s",
1327                      p_sys->stat.i_bytes,
1328                      p_sys->stat.i_read_time / I64C(1000000),
1329                      i_byterate / 1024 );
1330             break;
1331         }
1332
1333         /* */
1334         i_read = STREAM_CACHE_TRACK_SIZE - tk->i_end;
1335         i_read = __MIN( p_sys->stream.i_read_size, i_read );
1336         i_read = AReadStream( s, &tk->p_buffer[tk->i_end], i_read );
1337         if( i_read <  0 )
1338         {
1339             msleep( STREAM_DATA_WAIT );
1340             continue;
1341         }
1342         else if( i_read == 0 )
1343         {
1344             /* EOF */
1345             break;
1346         }
1347
1348         if( i_first == 0 )
1349         {
1350             i_first = mdate();
1351             msg_Dbg( s, "received first data for our buffer");
1352         }
1353
1354         tk->i_end += i_read;
1355
1356         p_sys->stat.i_read_count++;
1357     }
1358 }
1359
1360
1361 /****************************************************************************
1362  * stream_ReadLine:
1363  ****************************************************************************/
1364 /**
1365  * Read from the stream untill first newline.
1366  * \param s Stream handle to read from
1367  * \return A pointer to the allocated output string. You need to free this when you are done.
1368  */
1369 #define STREAM_PROBE_LINE 2048
1370 #define STREAM_LINE_MAX (2048*100)
1371 char * stream_ReadLine( stream_t *s )
1372 {
1373     char *p_line = NULL;
1374     int i_line = 0, i_read = 0;
1375
1376     while( i_read < STREAM_LINE_MAX )
1377     {
1378         char *psz_eol;
1379         uint8_t *p_data;
1380         int i_data;
1381         int64_t i_pos;
1382
1383         /* Probe new data */
1384         i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
1385         if( i_data <= 0 ) break; /* No more data */
1386
1387         /* BOM detection */
1388         i_pos = stream_Tell( s );
1389         if( i_pos == 0 && i_data > 4 )
1390         {
1391             int i_bom_size = 0;
1392             char *psz_encoding = NULL;
1393             
1394             if( p_data[0] == 0xEF && p_data[1] == 0xBB && p_data[2] == 0xBF )
1395             {
1396                 psz_encoding = strdup( "UTF-8" );
1397                 i_bom_size = 3;
1398             }
1399             else if( p_data[0] == 0x00 && p_data[1] == 0x00 )
1400             {
1401                 if( p_data[2] == 0xFE && p_data[3] == 0xFF )
1402                 {
1403                     psz_encoding = strdup( "UTF-32BE" );
1404                     s->i_char_width = 4;
1405                     i_bom_size = 4;
1406                 }
1407             }
1408             else if( p_data[0] == 0xFF && p_data[1] == 0xFE )
1409             {
1410                 if( p_data[2] == 0x00 && p_data[3] == 0x00 )
1411                 {
1412                     psz_encoding = strdup( "UTF-32LE" );
1413                     s->i_char_width = 4;
1414                     s->b_little_endian = VLC_TRUE;
1415                     i_bom_size = 4;
1416                 }
1417                 else
1418                 {
1419                     psz_encoding = strdup( "UTF-16LE" );
1420                     s->b_little_endian = VLC_TRUE;
1421                     s->i_char_width = 2;
1422                     i_bom_size = 2;
1423                 }
1424             }
1425             else if( p_data[0] == 0xFE && p_data[1] == 0xFF )
1426             {
1427                 psz_encoding = strdup( "UTF-16BE" );
1428                 s->i_char_width = 2;
1429                 i_bom_size = 2;
1430             }
1431             /* Seek past the offset */
1432             stream_Seek( s, i_bom_size );
1433             p_data += i_bom_size;
1434             i_data -= i_bom_size;
1435
1436             /* Open the converter if we need it */
1437             if( psz_encoding != NULL )
1438             {
1439                 msg_Dbg( s, "%s BOM detected", psz_encoding );
1440                 s->conv = vlc_iconv_open( "UTF-8", psz_encoding );
1441                 if( s->conv == (vlc_iconv_t)-1 )
1442                 {
1443                     msg_Err( s, "iconv_open failed" );
1444                 }
1445                 if( psz_encoding ) free( psz_encoding );
1446             }
1447         }
1448
1449         if( i_data % s->i_char_width )
1450         {
1451             msg_Warn( s, "the read is not i_char_width compatible");
1452         }
1453
1454         /* Check if there is an EOL */
1455         if( ( psz_eol = memchr( p_data, '\n', i_data ) ) )
1456         {
1457             if( s->b_little_endian == VLC_TRUE && s->i_char_width > 1 )
1458             {
1459                 psz_eol += ( s->i_char_width - 1 );
1460             }
1461             i_data = (psz_eol - (char *)p_data) + 1;
1462             p_line = realloc( p_line, i_line + i_data + s->i_char_width ); /* add \0 */
1463             i_data = stream_Read( s, &p_line[i_line], i_data );
1464             if( i_data <= 0 ) break; /* Hmmm */
1465             i_line += i_data - s->i_char_width; /* skip \n */;
1466             i_read += i_data;
1467
1468             /* We have our line */
1469             break;
1470         }
1471
1472         /* Read data (+1 for easy \0 append) */
1473         p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + s->i_char_width );
1474         i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
1475         if( i_data <= 0 ) break; /* Hmmm */
1476         i_line += i_data;
1477         i_read += i_data;
1478     }
1479
1480     if( i_read > 0 )
1481     {
1482         int j;
1483         for( j = 0; j < s->i_char_width; j++ )
1484         {
1485             p_line[i_line + j] = '\0';
1486         }
1487         i_line += s->i_char_width; /* the added \0 */
1488         if( s->i_char_width > 1 )
1489         {
1490             size_t i_in = 0, i_out = 0;
1491             char * p_in = NULL;
1492             char * p_out = NULL;
1493             char * psz_new_line = NULL;
1494             
1495             /* iconv */
1496             psz_new_line = malloc( i_line );
1497             
1498             i_in = i_out = (size_t)i_line;
1499             p_in = p_line;
1500             p_out = psz_new_line;
1501             
1502             if( vlc_iconv( s->conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
1503             {
1504                 msg_Err( s, "iconv failed" );
1505                 msg_Dbg( s, "original: %d, in %d, out %d", i_line, (int)i_in, (int)i_out );
1506             }
1507             if( p_line ) free( p_line );
1508             p_line = psz_new_line;
1509             i_line = (size_t)i_line - i_out; /* does not include \0 */
1510         }
1511
1512         /* Remove trailing LF/CR */
1513         while( i_line > 0 && ( p_line[i_line-2] == '\r' ||
1514             p_line[i_line-2] == '\n') ) i_line--;
1515
1516         /* Make sure the \0 is there */
1517         p_line[i_line-1] = '\0';
1518
1519         return p_line;
1520     }
1521
1522     /* We failed to read any data, probably EOF */
1523     if( p_line ) free( p_line );
1524     vlc_iconv_close( s->conv );
1525     return NULL;
1526 }
1527
1528 /****************************************************************************
1529  * Access reading/seeking wrappers to handle concatenated streams.
1530  ****************************************************************************/
1531 static int AReadStream( stream_t *s, void *p_read, int i_read )
1532 {
1533     stream_sys_t *p_sys = s->p_sys;
1534     access_t *p_access = p_sys->p_access;
1535     int i_read_orig = i_read;
1536
1537     if( !p_sys->i_list )
1538     {
1539         i_read = p_access->pf_read( p_access, p_read, i_read );
1540         return i_read;
1541     }
1542
1543     i_read = p_sys->p_list_access->pf_read( p_sys->p_list_access, p_read,
1544                                             i_read );
1545
1546     /* If we reached an EOF then switch to the next stream in the list */
1547     if( i_read == 0 && p_sys->i_list_index + 1 < p_sys->i_list )
1548     {
1549         char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1550         access_t *p_list_access;
1551
1552         msg_Dbg( s, "opening input `%s'", psz_name );
1553
1554         p_list_access = access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1555
1556         if( !p_list_access ) return 0;
1557
1558         if( p_sys->p_list_access != p_access )
1559             access2_Delete( p_sys->p_list_access );
1560
1561         p_sys->p_list_access = p_list_access;
1562
1563         /* We have to read some data */
1564         return AReadStream( s, p_read, i_read_orig );
1565     }
1566
1567     return i_read;
1568 }
1569
1570 static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof )
1571 {
1572     stream_sys_t *p_sys = s->p_sys;
1573     access_t *p_access = p_sys->p_access;
1574     block_t *p_block;
1575     vlc_bool_t b_eof;
1576
1577     if( !p_sys->i_list )
1578     {
1579         p_block = p_access->pf_block( p_access );
1580         if( pb_eof ) *pb_eof = p_access->info.b_eof;
1581         return p_block;
1582     }
1583
1584     p_block = p_sys->p_list_access->pf_block( p_access );
1585     b_eof = p_sys->p_list_access->info.b_eof;
1586     if( pb_eof ) *pb_eof = b_eof;
1587
1588     /* If we reached an EOF then switch to the next stream in the list */
1589     if( !p_block && b_eof && p_sys->i_list_index + 1 < p_sys->i_list )
1590     {
1591         char *psz_name = p_sys->list[++p_sys->i_list_index]->psz_path;
1592         access_t *p_list_access;
1593
1594         msg_Dbg( s, "opening input `%s'", psz_name );
1595
1596         p_list_access = access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1597
1598         if( !p_list_access ) return 0;
1599
1600         if( p_sys->p_list_access != p_access )
1601             access2_Delete( p_sys->p_list_access );
1602
1603         p_sys->p_list_access = p_list_access;
1604
1605         /* We have to read some data */
1606         return AReadBlock( s, pb_eof );
1607     }
1608
1609     return p_block;
1610 }
1611
1612 static int ASeek( stream_t *s, int64_t i_pos )
1613 {
1614     stream_sys_t *p_sys = s->p_sys;
1615     access_t *p_access = p_sys->p_access;
1616
1617     /* Check which stream we need to access */
1618     if( p_sys->i_list )
1619     {
1620         int i;
1621         char *psz_name;
1622         int64_t i_size = 0;
1623         access_t *p_list_access = 0;
1624
1625         for( i = 0; i < p_sys->i_list - 1; i++ )
1626         {
1627             if( i_pos < p_sys->list[i]->i_size + i_size ) break;
1628             i_size += p_sys->list[i]->i_size;
1629         }
1630         psz_name = p_sys->list[i]->psz_path;
1631
1632         if( i != p_sys->i_list_index )
1633             msg_Dbg( s, "opening input `%s'", psz_name );
1634
1635         if( i != p_sys->i_list_index && i != 0 )
1636         {
1637             p_list_access =
1638                 access2_New( s, p_access->psz_access, 0, psz_name, 0 );
1639         }
1640         else if( i != p_sys->i_list_index )
1641         {
1642             p_list_access = p_access;
1643         }
1644
1645         if( p_list_access )
1646         {
1647             if( p_sys->p_list_access != p_access )
1648                 access2_Delete( p_sys->p_list_access );
1649
1650             p_sys->p_list_access = p_list_access;
1651         }
1652
1653         p_sys->i_list_index = i;
1654         return p_sys->p_list_access->pf_seek( p_sys->p_list_access,
1655                                               i_pos - i_size );
1656     }
1657
1658     return p_access->pf_seek( p_access, i_pos );
1659 }