1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gertjan Van Droogenbroeck <gertjanvd _PLUS_ vlc _AT_ gmail _DOT_ com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t
* );
44 static void Close ( vlc_object_t
* );
47 set_category( CAT_INPUT
)
48 set_subcategory( SUBCAT_INPUT_DEMUX
)
49 set_description( N_("Nuv demuxer") )
50 set_capability( "demux", 145 )
51 set_callbacks( Open
, Close
)
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Demux ( demux_t
* );
59 static int Control( demux_t
*, int, va_list );
67 } demux_index_entry_t
;
74 demux_index_entry_t
*idx
;
78 static void demux_IndexInit( demux_index_t
* );
79 static void demux_IndexClean( demux_index_t
* );
80 static void demux_IndexAppend( demux_index_t
*,
81 int64_t i_time
, int64_t i_offset
);
82 /* Convert a time into offset */
83 static int64_t demux_IndexConvertTime( demux_index_t
*, int64_t i_time
);
84 /* Find the nearest offset in the index */
85 static int64_t demux_IndexFindOffset( demux_index_t
*, int64_t i_offset
);
91 char id
[12]; /* "NuppelVideo\0" or "MythTVVideo\0" */
92 char version
[5]; /* "x.xx\0" */
99 char i_mode
; /* P progressive, I interlaced */
101 double d_aspect
; /* 1.0 squared pixel */
104 int i_video_blocks
; /* 0 no video, -1 unknown */
108 int i_keyframe_distance
;
112 #define NUV_FH_SIZE 12
115 char i_type
; /* A: audio, V: video, S: sync; T: test
116 R: Seekpoint (string:RTjjjjjjjj)
117 D: Extra data for codec
118 X: extended data Q: seektable */
119 char i_compression
; /* V: 0 uncompressed
124 A: 0 uncompressed (44100 1-bits, 2ch)
130 N null frame loudless
132 S: B audio and vdeo sync point
133 A audio sync info (timecode == effective
135 V next video sync (timecode == next video
137 S audio,video,text correlation */
138 char i_keyframe
; /* 0 keyframe, else no no key frame */
139 uint8_t i_filters
; /* 0x01: gauss 5 pixel (8,2,2,2,2)/16
140 0x02: gauss 5 pixel (8,1,1,1,1)/12
141 0x04: cartoon filter */
143 int i_timecode
; /* ms */
145 int i_length
; /* V,A,T: length of following data
146 S: length of packet correl */
152 vlc_fourcc_t i_video_fcc
;
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
;
160 int i_rtjpeg_quality
;
161 int i_rtjpeg_luma_filter
;
162 int i_rtjpeg_chroma_filter
;
167 int64_t i_seektable_offset
;
168 int64_t i_keyframe_adjust_offset
;
175 extended_header_t exh
;
178 es_out_id_t
*p_es_video
;
182 es_out_id_t
*p_es_audio
;
188 /* frameheader buffer */
189 uint8_t fh_buffer
[NUV_FH_SIZE
];
190 int64_t i_total_frames
;
191 int64_t i_total_length
;
192 /* first frame position (used for calculating size without seektable) */
193 int i_first_frame_offset
;
196 static int HeaderLoad( demux_t
*, header_t
*h
);
197 static int FrameHeaderLoad( demux_t
*, frame_header_t
*h
);
198 static int ExtendedHeaderLoad( demux_t
*, extended_header_t
*h
);
199 static int SeekTableLoad( demux_t
*, demux_sys_t
* );
200 static int ControlSetPosition( demux_t
*p_demux
, int64_t i_pos
, bool b_guess
);
202 /*****************************************************************************
203 * Open: initializes ES structures
204 *****************************************************************************/
205 static int Open( vlc_object_t
* p_this
)
207 demux_t
*p_demux
= (demux_t
*)p_this
;
209 const uint8_t *p_peek
;
213 if( stream_Peek( p_demux
->s
, &p_peek
, 12 ) != 12 ||
214 ( strncmp( (char *)p_peek
, "MythTVVideo", 11 ) &&
215 strncmp( (char *)p_peek
, "NuppelVideo", 11 ) ) )
218 p_sys
= malloc( sizeof( demux_sys_t
) );
221 memset( p_sys
, 0, sizeof( demux_sys_t
) );
222 p_sys
->p_es_video
= NULL
;
223 p_sys
->p_es_audio
= NULL
;
224 p_sys
->p_extra_f
= NULL
;
226 p_sys
->b_index
= false;
227 p_sys
->i_total_frames
= -1;
228 p_sys
->i_total_length
= -1;
229 demux_IndexInit( &p_sys
->idx
);
231 p_demux
->p_sys
= p_sys
;
233 /* Info about the stream */
234 stream_Control( p_demux
->s
, STREAM_CAN_SEEK
, &p_sys
->b_seekable
);
236 if( p_sys
->b_seekable
)
237 msg_Dbg( p_demux
, "stream is seekable" );
239 msg_Dbg( p_demux
, "stream is NOT seekable" );
242 if( HeaderLoad( p_demux
, &p_sys
->hdr
) )
246 if( FrameHeaderLoad( p_demux
, &fh
) || fh
.i_type
!= 'D' )
248 if( fh
.i_length
> 0 )
250 if( fh
.i_compression
== 'F' || fh
.i_compression
== 'R' )
252 /* ffmpeg extra data */
253 p_sys
->i_extra_f
= fh
.i_length
;
254 p_sys
->p_extra_f
= malloc( fh
.i_length
);
255 if( p_sys
->p_extra_f
== NULL
|| stream_Read( p_demux
->s
,
256 p_sys
->p_extra_f
, fh
.i_length
) != fh
.i_length
)
261 msg_Warn( p_demux
, "unsupported 'D' frame (c=%c)", fh
.i_compression
);
262 if( stream_Read( p_demux
->s
, NULL
, fh
.i_length
) != fh
.i_length
)
267 /* Check and load extented */
268 if( stream_Peek( p_demux
->s
, &p_peek
, 1 ) != 1 )
270 if( p_peek
[0] == 'X' )
272 if( FrameHeaderLoad( p_demux
, &fh
) )
274 if( fh
.i_length
!= 512 )
277 if( ExtendedHeaderLoad( p_demux
, &p_sys
->exh
) )
280 if( !p_sys
->b_seekable
)
281 msg_Warn( p_demux
, "stream is not seekable, skipping seektable" );
282 else if( SeekTableLoad( p_demux
, p_sys
) )
284 p_sys
->b_index
= false;
285 msg_Warn( p_demux
, "Seektable is broken, seek won't be accurate" );
290 /* XXX: for now only file with extended chunk are supported
291 * why: because else we need to have support for rtjpeg+stupid nuv shit */
292 msg_Err( p_demux
, "VLC doesn't support NUV without extended chunks (please upload samples)" );
296 /* Create audio/video (will work only with extended header and audio=mp3 */
297 if( p_sys
->hdr
.i_video_blocks
!= 0 )
301 es_format_Init( &fmt
, VIDEO_ES
, p_sys
->exh
.i_video_fcc
);
302 fmt
.video
.i_width
= p_sys
->hdr
.i_width
;
303 fmt
.video
.i_height
= p_sys
->hdr
.i_height
;
304 fmt
.i_extra
= p_sys
->i_extra_f
;
305 fmt
.p_extra
= p_sys
->p_extra_f
;
306 fmt
.video
.i_sar_num
= p_sys
->hdr
.d_aspect
* fmt
.video
.i_height
;
307 fmt
.video
.i_sar_den
= fmt
.video
.i_width
;
309 p_sys
->p_es_video
= es_out_Add( p_demux
->out
, &fmt
);
311 if( p_sys
->hdr
.i_audio_blocks
!= 0 )
315 es_format_Init( &fmt
, AUDIO_ES
, VLC_CODEC_MPGA
);
316 fmt
.audio
.i_rate
= p_sys
->exh
.i_audio_sample_rate
;
317 fmt
.audio
.i_bitspersample
= p_sys
->exh
.i_audio_bits_per_sample
;
319 p_sys
->p_es_audio
= es_out_Add( p_demux
->out
, &fmt
);
321 if( p_sys
->hdr
.i_text_blocks
!= 0 )
323 msg_Warn( p_demux
, "text not yet supported (upload samples)" );
326 p_sys
->i_first_frame_offset
= stream_Tell( p_demux
->s
);
328 /* Fill p_demux fields */
329 p_demux
->pf_demux
= Demux
;
330 p_demux
->pf_control
= Control
;
335 msg_Warn( p_demux
, "cannot load Nuv file" );
336 p_demux
->p_sys
= NULL
;
341 /*****************************************************************************
342 * Close: frees unused data
343 *****************************************************************************/
344 static void Close( vlc_object_t
* p_this
)
346 demux_t
*p_demux
= (demux_t
*)p_this
;
347 demux_sys_t
*p_sys
= p_demux
->p_sys
;
349 free( p_sys
->p_extra_f
);
350 demux_IndexClean( &p_sys
->idx
);
354 /*****************************************************************************
355 * Demux: reads and demuxes data packets
356 *****************************************************************************
357 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
358 *****************************************************************************/
359 static int Demux( demux_t
*p_demux
)
361 demux_sys_t
*p_sys
= p_demux
->p_sys
;
367 if( !vlc_object_alive (p_demux
) )
370 if( FrameHeaderLoad( p_demux
, &fh
) )
373 if( fh
.i_type
== 'A' || fh
.i_type
== 'V' )
376 /* TODO add support for some block type */
378 if( fh
.i_type
!= 'R' && fh
.i_length
> 0 )
380 if( stream_Read( p_demux
->s
, NULL
, fh
.i_length
) != fh
.i_length
)
386 if( ( p_data
= stream_Block( p_demux
->s
, fh
.i_length
) ) == NULL
)
389 p_data
->i_dts
= VLC_TS_0
+ (int64_t)fh
.i_timecode
* 1000;
390 p_data
->i_pts
= (fh
.i_type
== 'V') ? VLC_TS_INVALID
: p_data
->i_dts
;
392 /* only add keyframes to index */
393 if( !fh
.i_keyframe
&& !p_sys
->b_index
)
394 demux_IndexAppend( &p_sys
->idx
,
395 p_data
->i_dts
- VLC_TS_0
,
396 stream_Tell(p_demux
->s
) - NUV_FH_SIZE
);
399 if( p_sys
->i_pcr
< 0 || p_sys
->i_pcr
< p_data
->i_dts
- VLC_TS_0
)
401 p_sys
->i_pcr
= p_data
->i_dts
- VLC_TS_0
;
402 es_out_Control( p_demux
->out
, ES_OUT_SET_PCR
, VLC_TS_0
+ p_sys
->i_pcr
);
405 if( fh
.i_type
== 'A' && p_sys
->p_es_audio
)
407 if( fh
.i_compression
== '3' )
408 es_out_Send( p_demux
->out
, p_sys
->p_es_audio
, p_data
);
411 msg_Dbg( p_demux
, "unsupported compression %c for audio (upload samples)", fh
.i_compression
);
412 block_Release( p_data
);
415 else if( fh
.i_type
== 'V' && p_sys
->p_es_video
)
417 if( fh
.i_compression
>='0' && fh
.i_compression
<='3' )
419 /* for rtjpeg data, the header is also needed */
420 p_data
= block_Realloc( p_data
, NUV_FH_SIZE
, fh
.i_length
);
421 memcpy( p_data
->p_buffer
, p_sys
->fh_buffer
, NUV_FH_SIZE
);
423 /* 0,1,2,3 -> rtjpeg, >=4 mpeg4 */
424 if( fh
.i_compression
>= '0' )
425 es_out_Send( p_demux
->out
, p_sys
->p_es_video
, p_data
);
428 msg_Dbg( p_demux
, "unsupported compression %c for video (upload samples)", fh
.i_compression
);
429 block_Release( p_data
);
434 block_Release( p_data
);
440 /*****************************************************************************
442 *****************************************************************************/
443 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
445 demux_sys_t
*p_sys
= p_demux
->p_sys
;
453 case DEMUX_GET_POSITION
:
454 pf
= (double*)va_arg( args
, double * );
456 if( p_sys
->i_total_length
> 0 && p_sys
->i_pcr
>= 0 )
458 *pf
= (double)p_sys
->i_pcr
/ (double)p_sys
->i_total_length
;
462 i64
= stream_Size( p_demux
->s
);
465 const double f_current
= stream_Tell( p_demux
->s
);
466 *pf
= f_current
/ (double)i64
;
475 case DEMUX_SET_POSITION
:
479 f
= (double)va_arg( args
, double );
483 /* first try to see if we can seek based on time (== GET_LENGTH works) */
484 if( p_sys
->i_total_length
> 0 && ( i_pos
= demux_IndexConvertTime( &p_sys
->idx
, p_sys
->i_total_length
* f
) ) > 0 )
485 return ControlSetPosition( p_demux
, i_pos
, false );
487 /* if not search based on total stream size */
488 else if( ( i_pos
= demux_IndexFindOffset( &p_sys
->idx
, stream_Size( p_demux
->s
) * f
) ) >= 0 )
489 return ControlSetPosition( p_demux
, i_pos
, false );
491 else if( ( i_pos
= p_sys
->i_first_frame_offset
+ ( stream_Size( p_demux
->s
) - p_sys
->i_first_frame_offset
) * f
) >= 0 )
492 return ControlSetPosition( p_demux
, i_pos
, true );
499 pi64
= (int64_t*)va_arg( args
, int64_t * );
500 *pi64
= p_sys
->i_pcr
>= 0 ? p_sys
->i_pcr
: 0;
506 i64
= (int64_t)va_arg( args
, int64_t );
510 i_pos
= demux_IndexConvertTime( &p_sys
->idx
, i64
);
514 return ControlSetPosition( p_demux
, i_pos
, false );
517 case DEMUX_GET_LENGTH
:
518 pi64
= (int64_t*)va_arg( args
, int64_t * );
519 if( p_sys
->i_total_length
>= 0 )
521 *pi64
= p_sys
->i_total_length
;
524 else if( stream_Tell( p_demux
->s
) > p_sys
->i_first_frame_offset
)
526 /* This should give an approximation of the total duration */
527 *pi64
= (double)( stream_Size( p_demux
->s
) - p_sys
->i_first_frame_offset
) /
528 (double)( stream_Tell( p_demux
->s
) - p_sys
->i_first_frame_offset
)
529 * (double)( p_sys
->i_pcr
>= 0 ? p_sys
->i_pcr
: 0 );
536 pf
= (double*)va_arg( args
, double * );
537 *pf
= p_sys
->hdr
.d_fps
;
546 static int ControlSetPosition( demux_t
*p_demux
, int64_t i_pos
, bool b_guess
)
548 demux_sys_t
*p_sys
= p_demux
->p_sys
;
553 /* if we can seek in the stream */
554 if( p_sys
->b_seekable
&& !b_guess
)
556 if( stream_Seek( p_demux
->s
, i_pos
) )
562 if( i_pos
> stream_Tell( p_demux
->s
) )
564 msg_Dbg( p_demux
, "unable to seek, skipping frames (slow)" );
568 msg_Warn( p_demux
, "unable to seek, only forward seeking is possible" );
574 while( vlc_object_alive (p_demux
) )
579 if( ( i_tell
= stream_Tell( p_demux
->s
) ) >= i_pos
)
582 if( FrameHeaderLoad( p_demux
, &fh
) )
585 if( fh
.i_type
== 'A' || fh
.i_type
== 'V' )
587 if( !fh
.i_keyframe
&& !p_sys
->b_index
)
588 demux_IndexAppend( &p_sys
->idx
,(int64_t)fh
.i_timecode
*1000, i_tell
);
591 if( fh
.i_type
!= 'R' && fh
.i_length
> 0 )
593 if( stream_Read( p_demux
->s
, NULL
, fh
.i_length
) != fh
.i_length
)
601 /*****************************************************************************
603 *****************************************************************************/
604 static inline void GetDoubleLE( double *pd
, void *src
)
606 /* FIXME works only if sizeof(double) == 8 */
607 #ifdef WORDS_BIGENDIAN
608 uint8_t *p
= (uint8_t*)pd
, *q
= (uint8_t*)src
;
610 for( i
= 0; i
< 8; i
++ )
613 memcpy( pd
, src
, 8 );
619 static int HeaderLoad( demux_t
*p_demux
, header_t
*h
)
623 if( stream_Read( p_demux
->s
, buffer
, 72 ) != 72 )
626 /* XXX: they are alignment to take care of (another broken format) */
627 memcpy( h
->id
, &buffer
[ 0], 12 );
628 memcpy( h
->version
, &buffer
[12], 5 );
629 h
->i_width
= GetDWLE( &buffer
[20] );
630 h
->i_height
= GetDWLE( &buffer
[24] );
631 h
->i_width_desired
= GetDWLE( &buffer
[28] );
632 h
->i_height_desired
= GetDWLE( &buffer
[32] );
633 h
->i_mode
= buffer
[36];
634 GetDoubleLE( &h
->d_aspect
, &buffer
[40] );
635 GetDoubleLE( &h
->d_fps
, &buffer
[48] );
636 h
->i_video_blocks
= GetDWLE( &buffer
[56] );
637 h
->i_audio_blocks
= GetDWLE( &buffer
[60] );
638 h
->i_text_blocks
= GetDWLE( &buffer
[64] );
639 h
->i_keyframe_distance
= GetDWLE( &buffer
[68] );
641 msg_Dbg( p_demux
, "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d",
642 h
->id
, h
->version
, h
->i_width
, h
->i_height
, h
->d_aspect
,
643 h
->d_fps
, h
->i_video_blocks
, h
->i_audio_blocks
, h
->i_text_blocks
,
644 h
->i_keyframe_distance
);
651 static int FrameHeaderLoad( demux_t
*p_demux
, frame_header_t
*h
)
653 uint8_t* buffer
= p_demux
->p_sys
->fh_buffer
;
655 if( stream_Read( p_demux
->s
, buffer
, 12 ) != 12 )
658 h
->i_type
= buffer
[0];
659 h
->i_compression
= buffer
[1];
660 h
->i_keyframe
= buffer
[2];
661 h
->i_filters
= buffer
[3];
663 h
->i_timecode
= GetDWLE( &buffer
[4] );
664 h
->i_length
= GetDWLE( &buffer
[8] );
666 msg_Dbg( p_demux
, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
668 h
->i_compression
? h
->i_compression
: ' ',
669 h
->i_keyframe
? h
->i_keyframe
: ' ',
671 h
->i_timecode
, h
->i_length
);
676 static int ExtendedHeaderLoad( demux_t
*p_demux
, extended_header_t
*h
)
680 if( stream_Read( p_demux
->s
, buffer
, 512 ) != 512 )
683 h
->i_version
= GetDWLE( &buffer
[0] );
684 h
->i_video_fcc
= VLC_FOURCC( buffer
[4], buffer
[5], buffer
[6], buffer
[7] );
685 h
->i_audio_fcc
= VLC_FOURCC( buffer
[8], buffer
[9], buffer
[10], buffer
[11] );
686 h
->i_audio_sample_rate
= GetDWLE( &buffer
[12] );
687 h
->i_audio_bits_per_sample
= GetDWLE( &buffer
[16] );
688 h
->i_audio_channels
= GetDWLE( &buffer
[20] );
689 h
->i_audio_compression_ratio
= GetDWLE( &buffer
[24] );
690 h
->i_audio_quality
= GetDWLE( &buffer
[28] );
691 h
->i_rtjpeg_quality
= GetDWLE( &buffer
[32] );
692 h
->i_rtjpeg_luma_filter
= GetDWLE( &buffer
[36] );
693 h
->i_rtjpeg_chroma_filter
= GetDWLE( &buffer
[40] );
694 h
->i_lavc_bitrate
= GetDWLE( &buffer
[44] );
695 h
->i_lavc_qmin
= GetDWLE( &buffer
[48] );
696 h
->i_lavc_qmin
= GetDWLE( &buffer
[52] );
697 h
->i_lavc_maxqdiff
= GetDWLE( &buffer
[56] );
698 h
->i_seektable_offset
= GetQWLE( &buffer
[60] );
699 h
->i_keyframe_adjust_offset
= GetQWLE( &buffer
[68] );
701 msg_Dbg( p_demux
, "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
702 "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%"PRIi64
" keyfao=%"PRIi64
,
704 (char*)&h
->i_video_fcc
,
705 (char*)&h
->i_audio_fcc
, h
->i_audio_sample_rate
, h
->i_audio_bits_per_sample
, h
->i_audio_channels
,
706 h
->i_audio_compression_ratio
, h
->i_audio_quality
,
707 h
->i_rtjpeg_quality
, h
->i_rtjpeg_luma_filter
, h
->i_rtjpeg_chroma_filter
,
708 h
->i_lavc_bitrate
, h
->i_lavc_qmin
, h
->i_lavc_qmax
, h
->i_lavc_maxqdiff
,
709 h
->i_seektable_offset
, h
->i_keyframe_adjust_offset
);
717 int64_t i_file_offset;
718 int32_t i_keyframe_number;
723 int32_t i_keyframe_number;
727 static int SeekTableLoad( demux_t
*p_demux
, demux_sys_t
*p_sys
)
730 int64_t i_original_pos
;
731 int64_t i_time
, i_offset
;
732 int keyframe
, last_keyframe
= 0, frame
= 0, kfa_entry_id
= 0;
734 if( p_sys
->exh
.i_seektable_offset
<= 0 )
737 /* Save current position */
738 i_original_pos
= stream_Tell( p_demux
->s
);
740 msg_Dbg( p_demux
, "current offset %"PRIi64
, i_original_pos
);
742 msg_Dbg( p_demux
, "seeking in stream to %"PRIi64
, p_sys
->exh
.i_seektable_offset
);
744 if( stream_Seek( p_demux
->s
, p_sys
->exh
.i_seektable_offset
) )
747 if( FrameHeaderLoad( p_demux
, &fh
) )
750 if( fh
.i_type
!= 'Q' )
752 msg_Warn( p_demux
, "invalid seektable, frame type=%c", fh
.i_type
);
753 stream_Seek( p_demux
->s
, i_original_pos
);
758 uint8_t *p_seek_table
= malloc( fh
.i_length
);
759 if( p_seek_table
== NULL
)
762 if( stream_Read( p_demux
->s
, p_seek_table
, fh
.i_length
) != fh
.i_length
)
764 free( p_seek_table
);
767 const int32_t i_seek_elements
= fh
.i_length
/ 12;
769 /* Get keyframe adjust offsets */
770 int32_t i_kfa_elements
;
771 uint8_t *p_kfa_table
;
773 if( p_sys
->exh
.i_keyframe_adjust_offset
> 0 )
775 msg_Dbg( p_demux
, "seeking in stream to %"PRIi64
, p_sys
->exh
.i_keyframe_adjust_offset
);
776 if( stream_Seek( p_demux
->s
, p_sys
->exh
.i_keyframe_adjust_offset
) )
778 free( p_seek_table
);
782 if( FrameHeaderLoad( p_demux
, &fh
) )
784 free( p_seek_table
);
788 if( fh
.i_type
== 'K' && fh
.i_length
>= 8 )
790 p_kfa_table
= malloc( fh
.i_length
);
792 if( p_kfa_table
== NULL
)
794 free( p_seek_table
);
798 if( stream_Read( p_demux
->s
, p_kfa_table
, fh
.i_length
) != fh
.i_length
)
800 free( p_seek_table
);
805 i_kfa_elements
= fh
.i_length
/ 8;
818 if( i_kfa_elements
> 0 )
819 msg_Warn( p_demux
, "untested keyframe adjust support, upload samples" );
821 for( int32_t j
= 0; j
< i_seek_elements
; j
++)
824 uint8_t* p
= p_seek_table
+ j
* 12;
825 msg_Dbg( p_demux
, "%x %x %x %x %x %x %x %x %x %x %x %x",
826 p
[0], p
[1], p
[2], p
[3], p
[4], p
[5], p
[6], p
[7], p
[8], p
[9], p
[10], p
[11]);
828 keyframe
= GetDWLE( p_seek_table
+ j
* 12 + 8 );
830 frame
+= (keyframe
- last_keyframe
) * p_sys
->hdr
.i_keyframe_distance
;
832 if( kfa_entry_id
< i_kfa_elements
&& *(int32_t*)(p_kfa_table
+ kfa_entry_id
* 12 + 4) == j
)
834 frame
-= *(int32_t*)(p_kfa_table
+ kfa_entry_id
* 12);
835 msg_Dbg( p_demux
, "corrected keyframe %d with current frame number %d (corrected with %d)",
836 keyframe
, frame
, *(int32_t*)(p_kfa_table
+ kfa_entry_id
* 12) );
840 i_time
= (double)( (int64_t)frame
* 1000000 ) / p_sys
->hdr
.d_fps
;
841 i_offset
= GetQWLE( p_seek_table
+ j
* 12 );
843 if( i_offset
== 0 && i_time
!= 0 )
844 msg_Dbg( p_demux
, "invalid file offset %d %"PRIi64
, keyframe
, i_offset
);
847 demux_IndexAppend( &p_sys
->idx
, i_time
, i_offset
);
849 msg_Dbg( p_demux
, "adding entry position %d %"PRIi64
" file offset %"PRIi64
, keyframe
, i_time
, i_offset
);
853 last_keyframe
= keyframe
;
856 p_sys
->i_total_frames
= (int64_t)frame
;
858 p_sys
->b_index
= true;
860 p_sys
->i_total_length
= p_sys
->i_total_frames
* 1000000 / p_sys
->hdr
.d_fps
;
862 msg_Dbg( p_demux
, "index table loaded (%d elements)", i_seek_elements
);
865 free ( p_kfa_table
);
867 free ( p_seek_table
);
869 /* Restore stream position */
870 if( stream_Seek( p_demux
->s
, i_original_pos
) )
877 /*****************************************************************************/
878 #define DEMUX_INDEX_SIZE_MAX (100000)
879 static void demux_IndexInit( demux_index_t
*p_idx
)
882 p_idx
->i_idx_max
= 0;
885 static void demux_IndexClean( demux_index_t
*p_idx
)
890 static void demux_IndexAppend( demux_index_t
*p_idx
,
891 int64_t i_time
, int64_t i_offset
)
893 /* Be sure to append new entry (we don't insert point) */
894 if( p_idx
->i_idx
> 0 && p_idx
->idx
[p_idx
->i_idx
-1].i_time
>= i_time
)
898 if( p_idx
->i_idx
>= p_idx
->i_idx_max
)
900 if( p_idx
->i_idx
>= DEMUX_INDEX_SIZE_MAX
)
902 /* Avoid too big index */
903 const int64_t i_length
= p_idx
->idx
[p_idx
->i_idx
-1].i_time
-
904 p_idx
->idx
[0].i_time
;
905 const int i_count
= DEMUX_INDEX_SIZE_MAX
/2;
908 /* We try to reduce the resolution of the index by a factor 2 */
909 for( i
= 1, j
= 1; i
< p_idx
->i_idx
; i
++ )
911 if( p_idx
->idx
[i
].i_time
< j
* i_length
/ i_count
)
914 p_idx
->idx
[j
++] = p_idx
->idx
[i
];
918 if( p_idx
->i_idx
> 3 * DEMUX_INDEX_SIZE_MAX
/ 4 )
920 /* We haven't created enough space
921 * (This method won't create a good index but work for sure) */
922 for( i
= 0; i
< p_idx
->i_idx
/2; i
++ )
923 p_idx
->idx
[i
] = p_idx
->idx
[2*i
];
929 p_idx
->i_idx_max
+= 1000;
930 p_idx
->idx
= xrealloc( p_idx
->idx
,
931 p_idx
->i_idx_max
*sizeof(demux_index_entry_t
));
936 p_idx
->idx
[p_idx
->i_idx
].i_time
= i_time
;
937 p_idx
->idx
[p_idx
->i_idx
].i_offset
= i_offset
;
941 static int64_t demux_IndexConvertTime( demux_index_t
*p_idx
, int64_t i_time
)
944 int i_max
= p_idx
->i_idx
-1;
947 if( p_idx
->i_idx
<= 0 )
950 /* Special border case */
951 if( i_time
<= p_idx
->idx
[0].i_time
)
952 return p_idx
->idx
[0].i_offset
;
953 if( i_time
>= p_idx
->idx
[i_max
].i_time
)
954 return p_idx
->idx
[i_max
].i_offset
;
961 if( i_max
- i_min
<= 1 )
964 i_med
= (i_min
+i_max
)/2;
965 if( p_idx
->idx
[i_med
].i_time
< i_time
)
967 else if( p_idx
->idx
[i_med
].i_time
> i_time
)
970 return p_idx
->idx
[i_med
].i_offset
;
973 /* return nearest in time */
974 if( i_time
- p_idx
->idx
[i_min
].i_time
< p_idx
->idx
[i_max
].i_time
- i_time
)
975 return p_idx
->idx
[i_min
].i_offset
;
977 return p_idx
->idx
[i_max
].i_offset
;
981 static int64_t demux_IndexFindOffset( demux_index_t
*p_idx
, int64_t i_offset
)
984 int i_max
= p_idx
->i_idx
-1;
987 if( p_idx
->i_idx
<= 0 )
990 /* Special border case */
991 if( i_offset
<= p_idx
->idx
[0].i_offset
)
992 return p_idx
->idx
[0].i_offset
;
993 if( i_offset
== p_idx
->idx
[i_max
].i_offset
)
994 return p_idx
->idx
[i_max
].i_offset
;
995 if( i_offset
> p_idx
->idx
[i_max
].i_offset
)
1003 if( i_max
- i_min
<= 1 )
1006 i_med
= (i_min
+i_max
)/2;
1007 if( p_idx
->idx
[i_med
].i_offset
< i_offset
)
1009 else if( p_idx
->idx
[i_med
].i_offset
> i_offset
)
1012 return p_idx
->idx
[i_med
].i_offset
;
1015 /* return nearest */
1016 if( i_offset
- p_idx
->idx
[i_min
].i_offset
< p_idx
->idx
[i_max
].i_offset
- i_offset
)
1017 return p_idx
->idx
[i_min
].i_offset
;
1019 return p_idx
->idx
[i_max
].i_offset
;