ffmpeg: targetos must now be in lower case.
[vlc.git] / modules / demux / nuv.c
blobcfadb583b76b491065de89c322b9b403b9139520
1 /*****************************************************************************
2 * nuv.c:
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
5 * $Id$
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc_demux.h>
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
37 /*****************************************************************************
38 * Module descriptor
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close ( vlc_object_t * );
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();
52 /*****************************************************************************
53 * Local prototypes
54 *****************************************************************************/
55 static int Demux ( demux_t * );
56 static int Control( demux_t *, int, va_list );
58 /* */
59 typedef struct
61 int64_t i_time;
62 int64_t i_offset;
64 } demux_index_entry_t;
66 typedef struct
68 int i_idx;
69 int i_idx_max;
71 demux_index_entry_t *idx;
72 } demux_index_t;
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 );
85 /* */
86 typedef struct
88 char id[12]; /* "NuppelVideo\0" or "MythTVVideo\0" */
89 char version[5]; /* "x.xx\0" */
91 int i_width;
92 int i_height;
93 int i_width_desired;
94 int i_height_desired;
96 char i_mode; /* P progressive, I interlaced */
98 double d_aspect; /* 1.0 squared pixel */
99 double d_fps;
101 int i_video_blocks; /* 0 no video, -1 unknown */
102 int i_audio_blocks;
103 int i_text_blocks;
105 int i_keyframe_distance;
107 } header_t;
109 typedef struct
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 */
138 int i_timecode; /* ms */
140 int i_length; /* V,A,T: length of following data
141 S: length of packet correl */
142 } frame_header_t;
144 /* FIXME Not sure of this one */
145 typedef struct
147 int i_version;
148 vlc_fourcc_t i_video_fcc;
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;
166 } extended_header_t;
168 struct demux_sys_t
170 header_t hdr;
171 extended_header_t exh;
173 int64_t i_pcr;
174 es_out_id_t *p_es_video;
175 int i_extra_f;
176 uint8_t *p_extra_f;
178 es_out_id_t *p_es_audio;
180 /* index */
181 demux_index_t idx;
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 );
188 /*****************************************************************************
189 * Open: initializes ES structures
190 *****************************************************************************/
191 static int Open( vlc_object_t * p_this )
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;
199 /* Check id */
200 if( stream_Peek( p_demux->s, &p_peek, 12 ) != 12 ||
201 ( strncmp( (char *)p_peek, "MythTVVideo", 11 ) &&
202 strncmp( (char *)p_peek, "NuppelVideo", 11 ) ) )
203 return VLC_EGENERIC;
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 );
213 if( HeaderLoad( p_demux, &p_sys->hdr ) )
214 goto error;
216 /* Load 'D' */
217 if( FrameHeaderLoad( p_demux, &fh ) || fh.i_type != 'D' )
218 goto error;
219 if( fh.i_length > 0 )
221 if( fh.i_compression == 'F' )
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;
230 else
232 /* TODO handle rtjpeg */
233 msg_Warn( p_demux, "unsupported 'D' frame (c=%c)", fh.i_compression );
234 if( stream_Read( p_demux->s, NULL, fh.i_length ) != fh.i_length )
235 goto error;
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' )
244 b_extended = VLC_TRUE;
246 if( FrameHeaderLoad( p_demux, &fh ) )
247 goto error;
248 if( fh.i_length != 512 )
249 goto error;
251 if( ExtendedHeaderLoad( p_demux, &p_sys->exh ) )
252 goto error;
255 else
257 b_extended = VLC_FALSE;
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;
265 /* Create audio/video (will work only with extended header and audio=mp3 */
266 if( p_sys->hdr.i_video_blocks != 0 )
268 es_format_t fmt;
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;
276 p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
278 if( p_sys->hdr.i_audio_blocks != 0 )
280 es_format_t fmt;
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;
286 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
288 if( p_sys->hdr.i_text_blocks != 0 )
290 msg_Warn( p_demux, "text not yet supported (upload samples)" );
294 /* Fill p_demux fields */
295 p_demux->pf_demux = Demux;
296 p_demux->pf_control = Control;
297 p_demux->p_sys = p_sys;
299 return VLC_SUCCESS;
301 error:
302 msg_Warn( p_demux, "cannot load Nuv file" );
303 free( p_sys );
304 return VLC_EGENERIC;
307 /*****************************************************************************
308 * Close: frees unused data
309 *****************************************************************************/
310 static void Close( vlc_object_t * p_this )
312 demux_t *p_demux = (demux_t*)p_this;
313 demux_sys_t *p_sys = p_demux->p_sys;
315 if( p_sys->p_extra_f )
316 free( p_sys->p_extra_f );
317 demux_IndexClean( &p_sys->idx );
318 free( p_sys );
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 )
328 demux_sys_t *p_sys = p_demux->p_sys;
329 frame_header_t fh;
330 block_t *p_data;
332 for( ;; )
334 if( p_demux->b_die )
335 return -1;
337 if( FrameHeaderLoad( p_demux, &fh ) )
338 return 0;
340 if( fh.i_type == 'A' || fh.i_type == 'V' )
341 break;
343 /* TODO add support for some block type */
345 if( fh.i_type != 'R' )
347 stream_Read( p_demux->s, NULL, fh.i_length );
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;
356 /* */
357 demux_IndexAppend( &p_sys->idx, p_data->i_dts, stream_Tell(p_demux->s) );
359 /* */
360 if( p_data->i_dts > p_sys->i_pcr )
362 p_sys->i_pcr = p_data->i_dts;
363 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
366 if( fh.i_type == 'A' && p_sys->p_es_audio )
368 if( fh.i_compression == '3' )
369 es_out_Send( p_demux->out, p_sys->p_es_audio, p_data );
370 else
372 msg_Dbg( p_demux, "unsupported compression %c for audio (upload samples)", fh.i_compression );
373 block_Release( p_data );
376 else if( fh.i_type == 'V' && p_sys->p_es_video )
378 if( fh.i_compression >= '4' )
379 es_out_Send( p_demux->out, p_sys->p_es_video, p_data );
380 else
382 msg_Dbg( p_demux, "unsupported compression %c for video (upload samples)", fh.i_compression );
383 block_Release( p_data );
386 else
388 block_Release( p_data );
391 return 1;
394 /*****************************************************************************
395 * Control:
396 *****************************************************************************/
397 static int Control( demux_t *p_demux, int i_query, va_list args )
399 demux_sys_t *p_sys = p_demux->p_sys;
401 double f, *pf;
402 int64_t i64, *pi64;
404 switch( i_query )
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;
416 case DEMUX_SET_POSITION:
418 int64_t i_pos;
420 f = (double)va_arg( args, double );
421 i_pos = stream_Size( p_demux->s ) * f;
423 i64 = demux_IndexFindOffset( &p_sys->idx, i_pos );
425 p_sys->i_pcr = -1;
427 if( i64 >= 0 )
428 return stream_Seek( p_demux->s, i64 );
430 if( p_sys->idx.i_idx > 0 )
432 if( stream_Seek( p_demux->s, p_sys->idx.idx[p_sys->idx.i_idx-1].i_offset ) )
433 return VLC_EGENERIC;
435 else
437 if( stream_Seek( p_demux->s, 0 ) )
438 return VLC_EGENERIC;
441 while( !p_demux->b_die )
443 frame_header_t fh;
444 int64_t i_tell;
446 if( ( i_tell = stream_Tell( p_demux->s ) ) >= i_pos )
447 break;
449 if( FrameHeaderLoad( p_demux, &fh ) )
450 return VLC_EGENERIC;
452 if( fh.i_type == 'A' || fh.i_type == 'V' )
453 demux_IndexAppend( &p_sys->idx,(int64_t)fh.i_timecode*1000, i_tell );
455 if( fh.i_type != 'R' )
456 stream_Read( p_demux->s, NULL, fh.i_length );
458 return VLC_SUCCESS;
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;
467 case DEMUX_SET_TIME:
469 int64_t i_pos;
470 i64 = (int64_t)va_arg( args, int64_t );
472 i_pos = demux_IndexConvertTime( &p_sys->idx, i64 );
473 if( i_pos < 0 )
474 return VLC_EGENERIC;
476 if( stream_Seek( p_demux->s, i_pos ) )
477 return VLC_EGENERIC;
479 p_sys->i_pcr = -1;
481 while( !p_demux->b_die )
483 frame_header_t fh;
485 if( FrameHeaderLoad( p_demux, &fh ) )
486 return VLC_EGENERIC;
488 if( fh.i_type == 'A' || fh.i_type == 'V' )
490 int64_t i_time = (int64_t)fh.i_timecode*1000;
491 int64_t i_tell = stream_Tell(p_demux->s)-12;
493 demux_IndexAppend( &p_sys->idx, i_time, i_tell );
495 if( i_time >= i64 )
496 return stream_Seek( p_demux->s, i_tell );
498 if( fh.i_type != 'R' )
499 stream_Read( p_demux->s, NULL, fh.i_length );
501 return VLC_SUCCESS;
504 case DEMUX_GET_LENGTH:
505 pi64 = (int64_t*)va_arg( args, int64_t * );
506 return VLC_EGENERIC;
508 case DEMUX_GET_FPS:
509 pf = (double*)va_arg( args, double * );
510 *pf = p_sys->hdr.d_fps;
511 return VLC_SUCCESS;
513 case DEMUX_GET_META:
514 default:
515 return VLC_EGENERIC;
520 /*****************************************************************************
522 *****************************************************************************/
523 static inline void GetDoubleLE( double *pd, void *src )
525 /* FIXME works only if sizeof(double) == 8 */
526 #ifdef WORDS_BIGENDIAN
527 uint8_t *p = (uint8_t*)pd, *q = (uint8_t*)src;
528 int i;
529 for( i = 0; i < 8; i++ )
530 p[i] = q[7-i];
531 #else
532 memcpy( pd, src, 8 );
533 #endif
536 /* HeaderLoad:
538 static int HeaderLoad( demux_t *p_demux, header_t *h )
540 uint8_t buffer[72];
542 if( stream_Read( p_demux->s, buffer, 72 ) != 72 )
543 return VLC_EGENERIC;
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;
568 /* FrameHeaderLoad:
570 static int FrameHeaderLoad( demux_t *p_demux, frame_header_t *h )
572 uint8_t buffer[12];
574 if( stream_Read( p_demux->s, buffer, 12 ) != 12 )
575 return VLC_EGENERIC;
577 h->i_type = buffer[0];
578 h->i_compression = buffer[1];
579 h->i_keyframe = buffer[2];
580 h->i_filters = buffer[3];
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;
595 static int ExtendedHeaderLoad( demux_t *p_demux, extended_header_t *h )
597 uint8_t buffer[512];
599 if( stream_Read( p_demux->s, buffer, 512 ) != 512 )
600 return VLC_EGENERIC;
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;
633 /*****************************************************************************/
634 #define DEMUX_INDEX_SIZE_MAX (100000)
635 static void demux_IndexInit( demux_index_t *p_idx )
637 p_idx->i_idx = 0;
638 p_idx->i_idx_max = 0;
639 p_idx->idx = NULL;
641 static void demux_IndexClean( demux_index_t *p_idx )
643 if( p_idx->idx )
645 free( p_idx->idx );
646 p_idx->idx = NULL;
649 static void demux_IndexAppend( demux_index_t *p_idx,
650 int64_t i_time, int64_t i_offset )
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;
656 /* */
657 if( p_idx->i_idx >= p_idx->i_idx_max )
659 if( p_idx->i_idx >= DEMUX_INDEX_SIZE_MAX )
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;
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++ )
670 if( p_idx->idx[i].i_time < j * i_length / i_count )
671 continue;
673 p_idx->idx[j++] = p_idx->idx[i];
675 p_idx->i_idx = j;
677 if( p_idx->i_idx > 3 * DEMUX_INDEX_SIZE_MAX / 4 )
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;
686 else
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));
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;
698 p_idx->i_idx++;
700 static int64_t demux_IndexConvertTime( demux_index_t *p_idx, int64_t i_time )
702 int i_min = 0;
703 int i_max = p_idx->i_idx-1;
705 /* Empty index */
706 if( p_idx->i_idx <= 0 )
707 return -1;
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;
715 /* Dicho */
716 for( ;; )
718 int i_med;
720 if( i_max - i_min <= 1 )
721 break;
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;
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;
740 static int64_t demux_IndexFindOffset( demux_index_t *p_idx, int64_t i_offset )
742 int i_min = 0;
743 int i_max = p_idx->i_idx-1;
745 /* Empty index */
746 if( p_idx->i_idx <= 0 )
747 return -1;
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;
757 /* Dicho */
758 for( ;; )
760 int i_med;
762 if( i_max - i_min <= 1 )
763 break;
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;
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;