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