]> git.sesse.net Git - vlc/blob - modules/demux/nuv.c
09e4928ff551c03f441462cd48efef6aee4f51a2
[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( "demux", 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     bool  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 = 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 = 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     free( p_sys->p_extra_f );
320     demux_IndexClean( &p_sys->idx );
321     free( p_sys );
322 }
323
324 /*****************************************************************************
325  * Demux: reads and demuxes data packets
326  *****************************************************************************
327  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
328  *****************************************************************************/
329 static int Demux( demux_t *p_demux )
330 {
331     demux_sys_t *p_sys = p_demux->p_sys;
332     frame_header_t fh;
333     block_t *p_data;
334
335     for( ;; )
336     {
337         if( p_demux->b_die )
338             return -1;
339
340         if( FrameHeaderLoad( p_demux, &fh ) )
341             return 0;
342
343         if( fh.i_type == 'A' || fh.i_type == 'V' )
344             break;
345
346         /* TODO add support for some block type */
347
348         if( fh.i_type != 'R' )
349         {
350             stream_Read( p_demux->s, NULL, fh.i_length );
351         }
352     }
353
354     /* */
355     p_data = stream_Block( p_demux->s, fh.i_length );
356     p_data->i_dts = (int64_t)fh.i_timecode * 1000;
357     p_data->i_pts = (fh.i_type == 'V') ? 0 : p_data->i_dts;
358
359     /* */
360     demux_IndexAppend( &p_sys->idx, p_data->i_dts, stream_Tell(p_demux->s) );
361
362     /* */
363     if( p_data->i_dts > p_sys->i_pcr )
364     {
365         p_sys->i_pcr = p_data->i_dts;
366         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
367     }
368
369     if( fh.i_type == 'A' && p_sys->p_es_audio )
370     {
371         if( fh.i_compression == '3' )
372             es_out_Send( p_demux->out, p_sys->p_es_audio, p_data );
373         else
374         {
375             msg_Dbg( p_demux, "unsupported compression %c for audio (upload samples)", fh.i_compression );
376             block_Release( p_data );
377         }
378     }
379     else if( fh.i_type == 'V' && p_sys->p_es_video )
380     {
381         if( fh.i_compression >= '4' )
382             es_out_Send( p_demux->out, p_sys->p_es_video, p_data );
383         else
384         {
385             msg_Dbg( p_demux, "unsupported compression %c for video (upload samples)", fh.i_compression );
386             block_Release( p_data );
387         }
388     }
389     else
390     {
391         block_Release( p_data );
392     }
393
394     return 1;
395 }
396
397 /*****************************************************************************
398  * Control:
399  *****************************************************************************/
400 static int Control( demux_t *p_demux, int i_query, va_list args )
401 {
402     demux_sys_t *p_sys  = p_demux->p_sys;
403
404     double   f, *pf;
405     int64_t i64, *pi64;
406
407     switch( i_query )
408     {
409
410         case DEMUX_GET_POSITION:
411             pf = (double*)va_arg( args, double * );
412             i64 = stream_Size( p_demux->s );
413             if( i64 > 0 )
414                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
415             else
416                 *pf = 0.0;
417             return VLC_SUCCESS;
418
419         case DEMUX_SET_POSITION:
420         {
421             int64_t i_pos;
422
423             f = (double)va_arg( args, double );
424             i_pos = stream_Size( p_demux->s ) * f;
425
426             i64 = demux_IndexFindOffset( &p_sys->idx, i_pos );
427
428             p_sys->i_pcr = -1;
429
430             if( i64 >= 0 )
431                 return stream_Seek( p_demux->s, i64 );
432
433             if( p_sys->idx.i_idx > 0 )
434             {
435                 if( stream_Seek( p_demux->s, p_sys->idx.idx[p_sys->idx.i_idx-1].i_offset ) )
436                     return VLC_EGENERIC;
437             }
438             else
439             {
440                 if( stream_Seek( p_demux->s, 0 ) )
441                     return VLC_EGENERIC;
442             }
443
444             while( !p_demux->b_die )
445             {
446                 frame_header_t fh;
447                 int64_t i_tell;
448
449                 if( ( i_tell = stream_Tell( p_demux->s ) ) >= i_pos )
450                     break;
451
452                 if( FrameHeaderLoad( p_demux, &fh ) )
453                     return VLC_EGENERIC;
454
455                 if( fh.i_type == 'A' || fh.i_type == 'V' )
456                     demux_IndexAppend( &p_sys->idx,(int64_t)fh.i_timecode*1000, i_tell );
457
458                 if( fh.i_type != 'R' )
459                     stream_Read( p_demux->s, NULL, fh.i_length );
460             }
461             return VLC_SUCCESS;
462         }
463
464
465         case DEMUX_GET_TIME:
466             pi64 = (int64_t*)va_arg( args, int64_t * );
467             *pi64 = p_sys->i_pcr >= 0 ? p_sys->i_pcr : 0;
468             return VLC_SUCCESS;
469
470         case DEMUX_SET_TIME:
471         {
472             int64_t i_pos;
473             i64 = (int64_t)va_arg( args, int64_t );
474
475             i_pos = demux_IndexConvertTime( &p_sys->idx, i64 );
476             if( i_pos < 0 )
477                 return VLC_EGENERIC;
478
479             if( stream_Seek( p_demux->s, i_pos ) )
480                 return VLC_EGENERIC;
481
482             p_sys->i_pcr = -1;
483
484             while( !p_demux->b_die )
485             {
486                 frame_header_t fh;
487
488                 if( FrameHeaderLoad( p_demux, &fh ) )
489                     return VLC_EGENERIC;
490
491                 if( fh.i_type == 'A' || fh.i_type == 'V' )
492                 {
493                     int64_t i_time = (int64_t)fh.i_timecode*1000;
494                     int64_t i_tell = stream_Tell(p_demux->s)-12;
495
496                     demux_IndexAppend( &p_sys->idx, i_time, i_tell );
497
498                     if( i_time >= i64 )
499                         return stream_Seek( p_demux->s, i_tell );
500                 }
501                 if( fh.i_type != 'R' )
502                     stream_Read( p_demux->s, NULL, fh.i_length );
503             }
504             return VLC_SUCCESS;
505         }
506
507         case DEMUX_GET_LENGTH:
508             pi64 = (int64_t*)va_arg( args, int64_t * );
509             return VLC_EGENERIC;
510
511         case DEMUX_GET_FPS:
512             pf = (double*)va_arg( args, double * );
513             *pf = p_sys->hdr.d_fps;
514             return VLC_SUCCESS;
515
516         case DEMUX_GET_META:
517         default:
518             return VLC_EGENERIC;
519
520     }
521 }
522
523 /*****************************************************************************
524  *
525  *****************************************************************************/
526 static inline void GetDoubleLE( double *pd, void *src )
527 {
528     /* FIXME works only if sizeof(double) == 8 */
529 #ifdef WORDS_BIGENDIAN
530     uint8_t *p = (uint8_t*)pd, *q = (uint8_t*)src;
531     int i;
532     for( i = 0; i < 8; i++ )
533         p[i] = q[7-i];
534 #else
535     memcpy( pd, src, 8 );
536 #endif
537 }
538
539 /* HeaderLoad:
540  */
541 static int HeaderLoad( demux_t *p_demux, header_t *h )
542 {
543     uint8_t buffer[72];
544
545     if( stream_Read( p_demux->s, buffer, 72 ) != 72 )
546         return VLC_EGENERIC;
547
548     /* XXX: they are alignment to take care of (another broken format) */
549     memcpy( h->id,      &buffer[ 0], 12 );
550     memcpy( h->version, &buffer[12], 5 );
551     h->i_width = GetDWLE( &buffer[20] );
552     h->i_height = GetDWLE( &buffer[24] );
553     h->i_width_desired = GetDWLE( &buffer[28] );
554     h->i_height_desired = GetDWLE( &buffer[32] );
555     h->i_mode = buffer[36];
556     GetDoubleLE( &h->d_aspect, &buffer[40] );
557     GetDoubleLE( &h->d_fps, &buffer[48] );
558     h->i_video_blocks = GetDWLE( &buffer[56] );
559     h->i_audio_blocks = GetDWLE( &buffer[60] );
560     h->i_text_blocks = GetDWLE( &buffer[64] );
561     h->i_keyframe_distance = GetDWLE( &buffer[68] );
562 #if 0
563     msg_Dbg( p_demux, "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d",
564              h->id, h->version, h->i_width, h->i_height, h->d_aspect,
565              h->d_fps, h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks,
566              h->i_keyframe_distance );
567 #endif
568     return VLC_SUCCESS;
569 }
570
571 /* FrameHeaderLoad:
572  */
573 static int FrameHeaderLoad( demux_t *p_demux, frame_header_t *h )
574 {
575     uint8_t buffer[12];
576
577     if( stream_Read( p_demux->s, buffer, 12 ) != 12 )
578         return VLC_EGENERIC;
579
580     h->i_type = buffer[0];
581     h->i_compression = buffer[1];
582     h->i_keyframe = buffer[2];
583     h->i_filters = buffer[3];
584
585     h->i_timecode = GetDWLE( &buffer[4] );
586     h->i_length = GetDWLE( &buffer[8] );
587 #if 0
588     msg_Dbg( p_demux, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
589              h->i_type,
590              h->i_compression ? h->i_compression : ' ',
591              h->i_keyframe ? h->i_keyframe : ' ',
592              h->i_filters,
593              h->i_timecode, h->i_length );
594 #endif
595     return VLC_SUCCESS;
596 }
597
598 static int ExtendedHeaderLoad( demux_t *p_demux, extended_header_t *h )
599 {
600     uint8_t buffer[512];
601
602     if( stream_Read( p_demux->s, buffer, 512 ) != 512 )
603         return VLC_EGENERIC;
604
605     h->i_version = GetDWLE( &buffer[0] );
606     h->i_video_fcc = VLC_FOURCC( buffer[4], buffer[5], buffer[6], buffer[7] );
607     h->i_audio_fcc = VLC_FOURCC( buffer[8], buffer[9], buffer[10], buffer[11] );
608     h->i_audio_sample_rate = GetDWLE( &buffer[12] );
609     h->i_audio_bits_per_sample = GetDWLE( &buffer[16] );
610     h->i_audio_channels = GetDWLE( &buffer[20] );
611     h->i_audio_compression_ratio = GetDWLE( &buffer[24] );
612     h->i_audio_quality = GetDWLE( &buffer[28] );
613     h->i_rtjpeg_quality = GetDWLE( &buffer[32] );
614     h->i_rtjpeg_luma_filter = GetDWLE( &buffer[36] );
615     h->i_rtjpeg_chroma_filter = GetDWLE( &buffer[40] );
616     h->i_lavc_bitrate = GetDWLE( &buffer[44] );
617     h->i_lavc_qmin = GetDWLE( &buffer[48] );
618     h->i_lavc_qmin = GetDWLE( &buffer[52] );
619     h->i_lavc_maxqdiff = GetDWLE( &buffer[56] );
620     h->i_seekable_offset = GetQWLE( &buffer[60] );
621     h->i_keyframe_adjust_offset= GetQWLE( &buffer[68] );
622 #if 0
623     msg_Dbg( p_demux, "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
624                       "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld",
625              h->i_version,
626              (char*)&h->i_video_fcc,
627              (char*)&h->i_audio_fcc, h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
628              h->i_audio_compression_ratio, h->i_audio_quality,
629              h->i_rtjpeg_quality, h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter,
630              h->i_lavc_bitrate, h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff,
631              h->i_seekable_offset, h->i_keyframe_adjust_offset );
632 #endif
633     return VLC_SUCCESS;
634 }
635
636 /*****************************************************************************/
637 #define DEMUX_INDEX_SIZE_MAX (100000)
638 static void demux_IndexInit( demux_index_t *p_idx )
639 {
640     p_idx->i_idx = 0;
641     p_idx->i_idx_max = 0;
642     p_idx->idx = NULL;
643 }
644 static void demux_IndexClean( demux_index_t *p_idx )
645 {
646     free( p_idx->idx );
647     p_idx->idx = NULL;
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 }