* all: added a nuv demuxer (limited to mpeg4 video and mp3 audio).
[vlc.git] / modules / demux / nuv.c
1 /*****************************************************************************
2  * nuv.c:
3  *****************************************************************************
4  * Copyright (C) 2005 VideoLAN
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 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29
30 /* TODO:
31  *  - complete support (add support for rtjpeg and raw)
32  *  - better seek support (to key frame)
33  *  - control GET_LENGTH (harder, unless we have an extended header+index)
34  *  - test
35  */
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 static int  Open  ( vlc_object_t * );
41 static void Close ( vlc_object_t * );
42
43 vlc_module_begin();
44     set_category( CAT_INPUT );
45     set_subcategory( SUBCAT_INPUT_DEMUX );
46     set_description( _("Nuv demuxer") );
47     set_capability( "demux2", 145 );
48     set_callbacks( Open, Close );
49     add_shortcut( "nuv" );
50 vlc_module_end();
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 static int Demux  ( demux_t * );
56 static int Control( demux_t *, int, va_list );
57
58 /* */
59 typedef struct
60 {
61     int64_t i_time;
62     int64_t i_offset;
63
64 } demux_index_entry_t;
65
66 typedef struct
67 {
68     int i_idx;
69     int i_idx_max;
70
71     demux_index_entry_t *idx;
72 } demux_index_t;
73
74
75 static void demux_IndexInit( demux_index_t * );
76 static void demux_IndexClean( demux_index_t * );
77 static void demux_IndexAppend( demux_index_t *,
78                                int64_t i_time, int64_t i_offset );
79 /* Convert a time into offset */
80 static int64_t demux_IndexConvertTime( demux_index_t *, int64_t i_time );
81 /* Find the nearest offset in the index */
82 static int64_t demux_IndexFindOffset( demux_index_t *, int64_t i_offset );
83
84
85 /* */
86 typedef struct
87 {
88     char id[12];       /* "NuppelVideo\0" or "MythTVVideo\0" */
89     char version[5];    /* "x.xx\0" */
90
91     int  i_width;
92     int  i_height;
93     int  i_width_desired;
94     int  i_height_desired;
95
96     char i_mode;            /* P progressive, I interlaced */
97
98     double  d_aspect;       /* 1.0 squared pixel */
99     double  d_fps;
100
101     int     i_video_blocks; /* 0 no video, -1 unknown */
102     int     i_audio_blocks;
103     int     i_text_blocks;
104
105     int     i_keyframe_distance;
106
107 } header_t;
108
109 typedef struct
110 {
111     char i_type;        /* A: audio, V: video, S: sync; T: test
112                            R: Seekpoint (string:RTjjjjjjjj)
113                            D: Extra data for codec */
114     char i_compression; /* V: 0 uncompressed
115                               1 RTJpeg
116                               2 RTJpeg+lzo
117                               N black frame
118                               L copy last
119                            A: 0 uncompressed (44100 1-bits, 2ch)
120                               1 lzo
121                               2 layer 2
122                               3 layer 3
123                               F flac
124                               S shorten
125                               N null frame loudless
126                               L copy last
127                             S: B audio and vdeo sync point
128                                A audio sync info (timecode == effective
129                                     dsp frequency*100)
130                                V next video sync (timecode == next video
131                                     frame num)
132                                S audio,video,text correlation */
133     char i_keyframe;    /* 0 keyframe, else no no key frame */
134     uint8_t i_filters;  /* 0x01: gauss 5 pixel (8,2,2,2,2)/16
135                            0x02: gauss 5 pixel (8,1,1,1,1)/12
136                            0x04: cartoon filter */
137
138     int i_timecode;     /* ms */
139
140     int i_length;       /* V,A,T: length of following data
141                            S: length of packet correl */
142 } frame_header_t;
143
144 /* FIXME Not sure of this one */
145 typedef struct
146 {
147     int             i_version;
148     vlc_fourcc_t    i_video_fcc;
149
150     vlc_fourcc_t    i_audio_fcc;
151     int             i_audio_sample_rate;
152     int             i_audio_bits_per_sample;
153     int             i_audio_channels;
154     int             i_audio_compression_ratio;
155     int             i_audio_quality;
156     int             i_rtjpeg_quality;
157     int             i_rtjpeg_luma_filter;
158     int             i_rtjpeg_chroma_filter;
159     int             i_lavc_bitrate;
160     int             i_lavc_qmin;
161     int             i_lavc_qmax;
162     int             i_lavc_maxqdiff;
163     int64_t         i_seekable_offset;
164     int64_t         i_keyframe_adjust_offset;
165
166 } extended_header_t;
167
168 struct demux_sys_t
169 {
170     header_t          hdr;
171     extended_header_t exh;
172
173     int64_t     i_pcr;
174     es_out_id_t *p_es_video;
175     int         i_extra_f;
176     uint8_t     *p_extra_f;
177
178     es_out_id_t *p_es_audio;
179
180     /* index */
181     demux_index_t idx;
182 };
183
184 static int HeaderLoad( demux_t *, header_t *h );
185 static int FrameHeaderLoad( demux_t *, frame_header_t *h );
186 static int ExtendedHeaderLoad( demux_t *, extended_header_t *h );
187
188 /*****************************************************************************
189  * Open: initializes ES structures
190  *****************************************************************************/
191 static int Open( vlc_object_t * p_this )
192 {
193     demux_t     *p_demux = (demux_t*)p_this;
194     demux_sys_t *p_sys;
195     uint8_t     *p_peek;
196     frame_header_t fh;
197     vlc_bool_t  b_extended;
198
199     /* Check id */
200     if( stream_Peek( p_demux->s, &p_peek, 12 ) != 12 ||
201         ( strncmp( p_peek, "MythTVVideo", 11 ) &&
202           strncmp( p_peek, "NuppelVideo", 11 ) ) )
203         return VLC_EGENERIC;
204
205     p_sys = malloc( sizeof( demux_sys_t ) );
206     memset( p_sys, 0, sizeof( demux_sys_t ) );
207     p_sys->p_es_video = NULL;
208     p_sys->p_es_audio = NULL;
209     p_sys->p_extra_f = NULL;
210     p_sys->i_pcr = -1;
211     demux_IndexInit( &p_sys->idx );
212
213     if( HeaderLoad( p_demux, &p_sys->hdr ) )
214         goto error;
215
216     /* Load 'D' */
217     if( FrameHeaderLoad( p_demux, &fh ) || fh.i_type != 'D' )
218         goto error;
219     if( fh.i_length > 0 )
220     {
221         if( fh.i_compression == 'F' )
222         {
223             /* ffmpeg extra data */
224             p_sys->i_extra_f = fh.i_length;
225             p_sys->p_extra_f = malloc( fh.i_length );
226             if( stream_Read( p_demux->s,
227                              p_sys->p_extra_f, fh.i_length ) != fh.i_length )
228                 goto error;
229         }
230         else
231         {
232             /* TODO handle rtjpeg */
233             msg_Warn( p_demux, "unsuported 'D' frame (c=%c)", fh.i_compression );
234             if( stream_Read( p_demux->s, NULL, fh.i_length ) != fh.i_length )
235                 goto error;
236         }
237     }
238
239     /* Check and load extented */
240     if( stream_Peek( p_demux->s, &p_peek, 1 ) != 1 )
241         goto error;
242     if( p_peek[0] == 'X' )
243     {
244         b_extended = VLC_TRUE;
245
246         if( FrameHeaderLoad( p_demux, &fh ) )
247             goto error;
248         if( fh.i_length != 512 )
249             goto error;
250
251         if( ExtendedHeaderLoad( p_demux, &p_sys->exh ) )
252             goto error;
253
254     }
255     else
256     {
257         b_extended = VLC_FALSE;
258
259         /* XXX: for now only file with extended chunk are supported
260          * why: because else we need to have support for rtjpeg+stupid nuv shit */
261         msg_Err( p_demux, "incomplete NUV support (upload samples)" );
262         goto error;
263     }
264
265     /* Create audio/video (will work only with extended header and audio=mp3 */
266     if( p_sys->hdr.i_video_blocks != 0 )
267     {
268         es_format_t fmt;
269
270         es_format_Init( &fmt, VIDEO_ES, p_sys->exh.i_video_fcc );
271         fmt.video.i_width = p_sys->hdr.i_width;
272         fmt.video.i_height = p_sys->hdr.i_height;
273         fmt.i_extra = p_sys->i_extra_f;
274         fmt.p_extra = p_sys->p_extra_f;
275
276         p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
277     }
278     if( p_sys->hdr.i_audio_blocks != 0 )
279     {
280         es_format_t fmt;
281
282         es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('m','p','g','a') );
283         fmt.audio.i_rate = p_sys->exh.i_audio_sample_rate;
284         fmt.audio.i_bitspersample = p_sys->exh.i_audio_bits_per_sample;
285
286         p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
287     }
288     if( p_sys->hdr.i_text_blocks != 0 )
289     {
290         msg_Warn( p_demux, "text not yet supported (upload samples)" );
291     }
292
293
294     /* Fill p_demux fields */
295     p_demux->pf_demux = Demux;
296     p_demux->pf_control = Control;
297     p_demux->p_sys = p_sys;
298
299     return VLC_SUCCESS;
300
301 error:
302     msg_Warn( p_demux, "cannot load Nuv file" );
303     free( p_sys );
304     return VLC_EGENERIC;
305 }
306
307 /*****************************************************************************
308  * Close: frees unused data
309  *****************************************************************************/
310 static void Close( vlc_object_t * p_this )
311 {
312     demux_t        *p_demux = (demux_t*)p_this;
313     demux_sys_t    *p_sys = p_demux->p_sys;
314
315     if( p_sys->p_extra_f )
316         free( p_sys->p_extra_f );
317     demux_IndexClean( &p_sys->idx );
318     free( p_sys );
319 }
320
321 /*****************************************************************************
322  * Demux: reads and demuxes data packets
323  *****************************************************************************
324  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
325  *****************************************************************************/
326 static int Demux( demux_t *p_demux )
327 {
328     demux_sys_t *p_sys = p_demux->p_sys;
329     frame_header_t fh;
330     block_t *p_data;
331
332     for( ;; )
333     {
334         if( p_demux->b_die )
335             return -1;
336
337         if( FrameHeaderLoad( p_demux, &fh ) )
338             return 0;
339
340         if( fh.i_type == 'A' || fh.i_type == 'V' )
341             break;
342
343         /* TODO add support for some block type */
344
345         if( fh.i_type != 'R' )
346         {
347             stream_Read( p_demux->s, NULL, fh.i_length );
348         }
349     }
350
351     /* */
352     p_data = stream_Block( p_demux->s, fh.i_length );
353     p_data->i_dts = (int64_t)fh.i_timecode * 1000;
354     p_data->i_pts = (fh.i_type == 'V') ? 0 : p_data->i_dts;
355
356     /* */
357     demux_IndexAppend( &p_sys->idx, p_data->i_dts, stream_Tell(p_demux->s) );
358
359     /* */
360     if( p_data->i_dts > p_sys->i_pcr )
361     {
362         p_sys->i_pcr = p_data->i_dts;
363         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
364     }
365
366     if( fh.i_type == 'A' && p_sys->p_es_audio )
367     {
368         if( fh.i_compression == '3' )
369             es_out_Send( p_demux->out, p_sys->p_es_audio, p_data );
370         else
371         {
372             msg_Dbg( p_demux, "unsupported compression %c for audio (upload samples)", fh.i_compression );
373             block_Release( p_data );
374         }
375     }
376     else if( fh.i_type == 'V' && p_sys->p_es_video )
377     {
378         if( fh.i_compression >= '4' )
379             es_out_Send( p_demux->out, p_sys->p_es_video, p_data );
380         else
381         {
382             msg_Dbg( p_demux, "unsupported compression %c for video (upload samples)", fh.i_compression );
383             block_Release( p_data );
384         }
385     }
386     else
387     {
388         block_Release( p_data );
389     }
390
391     return 1;
392 }
393
394 /*****************************************************************************
395  * Control:
396  *****************************************************************************/
397 static int Control( demux_t *p_demux, int i_query, va_list args )
398 {
399     demux_sys_t *p_sys  = p_demux->p_sys;
400
401     double   f, *pf;
402     int64_t i64, *pi64;
403
404     switch( i_query )
405     {
406
407         case DEMUX_GET_POSITION:
408             pf = (double*)va_arg( args, double * );
409             i64 = stream_Size( p_demux->s );
410             if( i64 > 0 )
411                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
412             else
413                 *pf = 0.0;
414             return VLC_SUCCESS;
415
416         case DEMUX_SET_POSITION:
417         {
418             int64_t i_pos;
419
420             f = (double)va_arg( args, double );
421             i_pos = stream_Size( p_demux->s ) * f;
422
423             i64 = demux_IndexFindOffset( &p_sys->idx, i_pos );
424
425             p_sys->i_pcr = -1;
426
427             if( i64 >= 0 )
428                 return stream_Seek( p_demux->s, i64 );
429
430             if( p_sys->idx.i_idx > 0 )
431             {
432                 if( stream_Seek( p_demux->s, p_sys->idx.idx[p_sys->idx.i_idx-1].i_offset ) )
433                     return VLC_EGENERIC;
434             }
435             else
436             {
437                 if( stream_Seek( p_demux->s, 0 ) )
438                     return VLC_EGENERIC;
439             }
440
441             while( !p_demux->b_die )
442             {
443                 frame_header_t fh;
444                 int64_t i_tell;
445
446                 if( ( i_tell = stream_Tell( p_demux->s ) ) >= i_pos )
447                     break;
448
449                 if( FrameHeaderLoad( p_demux, &fh ) )
450                     return VLC_EGENERIC;
451
452                 if( fh.i_type == 'A' || fh.i_type == 'V' )
453                     demux_IndexAppend( &p_sys->idx,(int64_t)fh.i_timecode*1000, i_tell );
454
455                 if( fh.i_type != 'R' )
456                     stream_Read( p_demux->s, NULL, fh.i_length );
457             }
458             return VLC_SUCCESS;
459         }
460
461
462         case DEMUX_GET_TIME:
463             pi64 = (int64_t*)va_arg( args, int64_t * );
464             *pi64 = p_sys->i_pcr >= 0 ? p_sys->i_pcr : 0;
465             return VLC_SUCCESS;
466
467         case DEMUX_SET_TIME:
468         {
469             int64_t i_pos;
470             i64 = (int64_t)va_arg( args, int64_t );
471
472             i_pos = demux_IndexConvertTime( &p_sys->idx, i64 );
473             if( i_pos < 0 )
474                 return VLC_EGENERIC;
475
476             if( stream_Seek( p_demux->s, i_pos ) )
477                 return VLC_EGENERIC;
478
479             p_sys->i_pcr = -1;
480
481             while( !p_demux->b_die )
482             {
483                 frame_header_t fh;
484
485                 if( FrameHeaderLoad( p_demux, &fh ) )
486                     return VLC_EGENERIC;
487
488                 if( fh.i_type == 'A' || fh.i_type == 'V' )
489                 {
490                     int64_t i_time = (int64_t)fh.i_timecode*1000;
491                     int64_t i_tell = stream_Tell(p_demux->s)-12;
492
493                     demux_IndexAppend( &p_sys->idx, i_time, i_tell );
494
495                     if( i_time >= i64 )
496                         return stream_Seek( p_demux->s, i_tell );
497                 }
498                 if( fh.i_type != 'R' )
499                     stream_Read( p_demux->s, NULL, fh.i_length );
500             }
501             return VLC_SUCCESS;
502         }
503
504         case DEMUX_GET_LENGTH:
505             pi64 = (int64_t*)va_arg( args, int64_t * );
506             return VLC_EGENERIC;
507
508         case DEMUX_GET_FPS:
509             pf = (double*)va_arg( args, double * );
510             *pf = p_sys->hdr.d_fps;
511             return VLC_SUCCESS;
512
513         case DEMUX_GET_META:
514         default:
515             return VLC_EGENERIC;
516
517     }
518 }
519
520 /*****************************************************************************
521  *
522  *****************************************************************************/
523 static inline void GetDoubleLE( double *pd, void *src )
524 {
525     /* FIXME works only if sizeof(double) == 8 */
526 #ifdef WORDS_BIGENDIAN
527     uint8_t *p = (uint8_t*)pd;
528     int i;
529     for( i = 0; i < 8; i++ )
530         p[i] = src[7-i];
531 #else
532     memcpy( pd, src, 8 );
533 #endif
534 }
535
536 /* HeaderLoad:
537  */
538 static int HeaderLoad( demux_t *p_demux, header_t *h )
539 {
540     uint8_t buffer[72];
541
542     if( stream_Read( p_demux->s, buffer, 72 ) != 72 )
543         return VLC_EGENERIC;
544
545     /* XXX: they are alignment to take care of (another broken format) */
546     memcpy( h->id,      &buffer[ 0], 12 );
547     memcpy( h->version, &buffer[12], 5 );
548     h->i_width = GetDWLE( &buffer[20] );
549     h->i_height = GetDWLE( &buffer[24] );
550     h->i_width_desired = GetDWLE( &buffer[28] );
551     h->i_height_desired = GetDWLE( &buffer[32] );
552     h->i_mode = buffer[36];
553     GetDoubleLE( &h->d_aspect, &buffer[40] );
554     GetDoubleLE( &h->d_fps, &buffer[48] );
555     h->i_video_blocks = GetDWLE( &buffer[56] );
556     h->i_audio_blocks = GetDWLE( &buffer[60] );
557     h->i_text_blocks = GetDWLE( &buffer[64] );
558     h->i_keyframe_distance = GetDWLE( &buffer[68] );
559 #if 0
560     msg_Dbg( p_demux, "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d",
561              h->id, h->version, h->i_width, h->i_height, h->d_aspect,
562              h->d_fps, h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks,
563              h->i_keyframe_distance );
564 #endif
565     return VLC_SUCCESS;
566 }
567
568 /* FrameHeaderLoad:
569  */
570 static int FrameHeaderLoad( demux_t *p_demux, frame_header_t *h )
571 {
572     uint8_t buffer[12];
573
574     if( stream_Read( p_demux->s, buffer, 12 ) != 12 )
575         return VLC_EGENERIC;
576
577     h->i_type = buffer[0];
578     h->i_compression = buffer[1];
579     h->i_keyframe = buffer[2];
580     h->i_filters = buffer[3];
581
582     h->i_timecode = GetDWLE( &buffer[4] );
583     h->i_length = GetDWLE( &buffer[8] );
584 #if 0
585     msg_Dbg( p_demux, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
586              h->i_type,
587              h->i_compression ? h->i_compression : ' ',
588              h->i_keyframe ? h->i_keyframe : ' ',
589              h->i_filters,
590              h->i_timecode, h->i_length );
591 #endif
592     return VLC_SUCCESS;
593 }
594
595 static int ExtendedHeaderLoad( demux_t *p_demux, extended_header_t *h )
596 {
597     uint8_t buffer[512];
598
599     if( stream_Read( p_demux->s, buffer, 512 ) != 512 )
600         return VLC_EGENERIC;
601
602     h->i_version = GetDWLE( &buffer[0] );
603     h->i_video_fcc = VLC_FOURCC( buffer[4], buffer[5], buffer[6], buffer[7] );
604     h->i_audio_fcc = VLC_FOURCC( buffer[8], buffer[9], buffer[10], buffer[11] );
605     h->i_audio_sample_rate = GetDWLE( &buffer[12] );
606     h->i_audio_bits_per_sample = GetDWLE( &buffer[16] );
607     h->i_audio_channels = GetDWLE( &buffer[20] );
608     h->i_audio_compression_ratio = GetDWLE( &buffer[24] );
609     h->i_audio_quality = GetDWLE( &buffer[28] );
610     h->i_rtjpeg_quality = GetDWLE( &buffer[32] );
611     h->i_rtjpeg_luma_filter = GetDWLE( &buffer[36] );
612     h->i_rtjpeg_chroma_filter = GetDWLE( &buffer[40] );
613     h->i_lavc_bitrate = GetDWLE( &buffer[44] );
614     h->i_lavc_qmin = GetDWLE( &buffer[48] );
615     h->i_lavc_qmin = GetDWLE( &buffer[52] );
616     h->i_lavc_maxqdiff = GetDWLE( &buffer[56] );
617     h->i_seekable_offset = GetQWLE( &buffer[60] );
618     h->i_keyframe_adjust_offset= GetQWLE( &buffer[68] );
619 #if 0
620     msg_Dbg( p_demux, "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
621                       "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld",
622              h->i_version,
623              (char*)&h->i_video_fcc,
624              (char*)&h->i_audio_fcc, h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
625              h->i_audio_compression_ratio, h->i_audio_quality,
626              h->i_rtjpeg_quality, h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter,
627              h->i_lavc_bitrate, h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff,
628              h->i_seekable_offset, h->i_keyframe_adjust_offset );
629 #endif
630     return VLC_SUCCESS;
631 }
632
633 /*****************************************************************************/
634 #define DEMUX_INDEX_SIZE_MAX (100000)
635 static void demux_IndexInit( demux_index_t *p_idx )
636 {
637     p_idx->i_idx = 0;
638     p_idx->i_idx_max = 0;
639     p_idx->idx = NULL;
640 }
641 static void demux_IndexClean( demux_index_t *p_idx )
642 {
643     if( p_idx->idx )
644     {
645         free( p_idx->idx );
646         p_idx->idx = NULL;
647     }
648 }
649 static void demux_IndexAppend( demux_index_t *p_idx,
650                                int64_t i_time, int64_t i_offset )
651 {
652     /* Be sure to append new entry (we don't insert point) */
653     if( p_idx->i_idx > 0 && p_idx->idx[p_idx->i_idx-1].i_time >= i_time )
654         return;
655
656     /* */
657     if( p_idx->i_idx >= p_idx->i_idx_max )
658     {
659         if( p_idx->i_idx >= DEMUX_INDEX_SIZE_MAX )
660         {
661             /* Avoid too big index */
662             const int64_t i_length = p_idx->idx[p_idx->i_idx-1].i_time -
663                                                         p_idx->idx[0].i_time;
664             const int i_count = DEMUX_INDEX_SIZE_MAX/2;
665             int i, j;
666
667             /* We try to reduce the resolution of the index by a factor 2 */
668             for( i = 1, j = 1; i < p_idx->i_idx; i++ )
669             {
670                 if( p_idx->idx[i].i_time < j * i_length / i_count )
671                     continue;
672
673                 p_idx->idx[j++] = p_idx->idx[i];
674             }
675             p_idx->i_idx = j;
676
677             if( p_idx->i_idx > 3 * DEMUX_INDEX_SIZE_MAX / 4 )
678             {
679                 /* We haven't created enough space
680                  * (This method won't create a good index but work for sure) */
681                 for( i = 0; i < p_idx->i_idx/2; i++ )
682                     p_idx->idx[i] = p_idx->idx[2*i];
683                 p_idx->i_idx /= 2;
684             }
685         }
686         else
687         {
688             p_idx->i_idx_max += 1000;
689             p_idx->idx = realloc( p_idx->idx,
690                                   p_idx->i_idx_max*sizeof(demux_index_entry_t));
691         }
692     }
693
694     /* */
695     p_idx->idx[p_idx->i_idx].i_time = i_time;
696     p_idx->idx[p_idx->i_idx].i_offset = i_offset;
697
698     p_idx->i_idx++;
699 }
700 static int64_t demux_IndexConvertTime( demux_index_t *p_idx, int64_t i_time )
701 {
702     int i_min = 0;
703     int i_max = p_idx->i_idx-1;
704
705     /* Empty index */
706     if( p_idx->i_idx <= 0 )
707         return -1;
708
709     /* Special border case */
710     if( i_time <= p_idx->idx[0].i_time )
711         return p_idx->idx[0].i_offset;
712     if( i_time >= p_idx->idx[i_max].i_time )
713         return p_idx->idx[i_max].i_offset;
714
715     /* Dicho */
716     for( ;; )
717     {
718         int i_med;
719
720         if( i_max - i_min <= 1 )
721             break;
722
723         i_med = (i_min+i_max)/2;
724         if( p_idx->idx[i_med].i_time < i_time )
725             i_min = i_med;
726         else if( p_idx->idx[i_med].i_time > i_time )
727             i_max = i_med;
728         else
729             return p_idx->idx[i_med].i_offset;
730     }
731
732     /* return nearest in time */
733     if( i_time - p_idx->idx[i_min].i_time < p_idx->idx[i_max].i_time - i_time )
734         return p_idx->idx[i_min].i_offset;
735     else
736         return p_idx->idx[i_max].i_offset;
737 }
738
739
740 static int64_t demux_IndexFindOffset( demux_index_t *p_idx, int64_t i_offset )
741 {
742     int i_min = 0;
743     int i_max = p_idx->i_idx-1;
744
745     /* Empty index */
746     if( p_idx->i_idx <= 0 )
747         return -1;
748
749     /* Special border case */
750     if( i_offset <= p_idx->idx[0].i_offset )
751         return p_idx->idx[0].i_offset;
752     if( i_offset == p_idx->idx[i_max].i_offset )
753         return p_idx->idx[i_max].i_offset;
754     if( i_offset > p_idx->idx[i_max].i_offset )
755         return -1;
756
757     /* Dicho */
758     for( ;; )
759     {
760         int i_med;
761
762         if( i_max - i_min <= 1 )
763             break;
764
765         i_med = (i_min+i_max)/2;
766         if( p_idx->idx[i_med].i_offset < i_offset )
767             i_min = i_med;
768         else if( p_idx->idx[i_med].i_offset > i_offset )
769             i_max = i_med;
770         else
771             return p_idx->idx[i_med].i_offset;
772     }
773
774     /* return nearest */
775     if( i_offset - p_idx->idx[i_min].i_offset < p_idx->idx[i_max].i_offset - i_offset )
776         return p_idx->idx[i_min].i_offset;
777     else
778         return p_idx->idx[i_max].i_offset;
779 }