chromecast: test encoder modules
[vlc.git] / modules / demux / nuv.c
blob40f7b4b60a8bf8d627f8e5fceec986f9f2663e0f
1 /*****************************************************************************
2 * nuv.c:
3 *****************************************************************************
4 * Copyright (C) 2005 VLC authors and VideoLAN
5 * $Id$
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35 #include <limits.h>
37 /* TODO:
38 * - test
41 /*****************************************************************************
42 * Module descriptor
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close ( vlc_object_t * );
47 vlc_module_begin ()
48 set_category( CAT_INPUT )
49 set_subcategory( SUBCAT_INPUT_DEMUX )
50 set_description( N_("Nuv demuxer") )
51 set_capability( "demux", 145 )
52 set_callbacks( Open, Close )
53 add_shortcut( "nuv" )
54 vlc_module_end ()
56 /*****************************************************************************
57 * Local prototypes
58 *****************************************************************************/
59 static int Demux ( demux_t * );
60 static int Control( demux_t *, int, va_list );
62 /* */
63 typedef struct
65 int64_t i_time;
66 int64_t i_offset;
68 } demux_index_entry_t;
70 typedef struct
72 int i_idx;
73 int i_idx_max;
75 demux_index_entry_t *idx;
76 } demux_index_t;
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 );
89 /* */
90 typedef struct
92 char id[12]; /* "NuppelVideo\0" or "MythTVVideo\0" */
93 char version[5]; /* "x.xx\0" */
95 int i_width;
96 int i_height;
97 int i_width_desired;
98 int i_height_desired;
100 char i_mode; /* P progressive, I interlaced */
102 double d_aspect; /* 1.0 squared pixel */
103 double d_fps;
105 int i_video_blocks; /* 0 no video, -1 unknown */
106 int i_audio_blocks;
107 int i_text_blocks;
109 int i_keyframe_distance;
111 } header_t;
113 #define NUV_FH_SIZE 12
114 typedef struct
116 char i_type; /* A: audio, V: video, S: sync; T: test
117 R: Seekpoint (string:RTjjjjjjjj)
118 D: Extra data for codec
119 X: extended data Q: seektable */
120 char i_compression; /* V: 0 uncompressed
121 1 RTJpeg
122 2 RTJpeg+lzo
123 N black frame
124 L copy last
125 A: 0 uncompressed (44100 1-bits, 2ch)
126 1 lzo
127 2 layer 2
128 3 layer 3
129 F flac
130 S shorten
131 N null frame loudless
132 L copy last
133 S: B audio and vdeo sync point
134 A audio sync info (timecode == effective
135 dsp frequency*100)
136 V next video sync (timecode == next video
137 frame num)
138 S audio,video,text correlation */
139 char i_keyframe; /* 0 keyframe, else no no key frame */
140 uint8_t i_filters; /* 0x01: gauss 5 pixel (8,2,2,2,2)/16
141 0x02: gauss 5 pixel (8,1,1,1,1)/12
142 0x04: cartoon filter */
144 int i_timecode; /* ms */
146 int i_length; /* V,A,T: length of following data
147 S: length of packet correl */
148 } frame_header_t;
150 typedef struct
152 int i_version;
153 vlc_fourcc_t i_video_fcc;
155 vlc_fourcc_t i_audio_fcc;
156 int i_audio_sample_rate;
157 int i_audio_bits_per_sample;
158 int i_audio_channels;
159 int i_audio_compression_ratio;
160 int i_audio_quality;
161 int i_rtjpeg_quality;
162 int i_rtjpeg_luma_filter;
163 int i_rtjpeg_chroma_filter;
164 int i_lavc_bitrate;
165 int i_lavc_qmin;
166 int i_lavc_qmax;
167 int i_lavc_maxqdiff;
168 int64_t i_seektable_offset;
169 int64_t i_keyframe_adjust_offset;
171 } extended_header_t;
173 struct demux_sys_t
175 header_t hdr;
176 extended_header_t exh;
178 int64_t i_pcr;
179 es_out_id_t *p_es_video;
180 int i_extra_f;
181 uint8_t *p_extra_f;
183 es_out_id_t *p_es_audio;
185 /* index */
186 demux_index_t idx;
187 bool b_index;
188 bool b_seekable;
189 /* frameheader buffer */
190 uint8_t fh_buffer[NUV_FH_SIZE];
191 int64_t i_total_frames;
192 int64_t i_total_length;
193 /* first frame position (used for calculating size without seektable) */
194 int i_first_frame_offset;
197 static int HeaderLoad( demux_t *, header_t *h );
198 static int FrameHeaderLoad( demux_t *, frame_header_t *h );
199 static int ExtendedHeaderLoad( demux_t *, extended_header_t *h );
200 static int SeekTableLoad( demux_t *, demux_sys_t * );
201 static int ControlSetPosition( demux_t *p_demux, int64_t i_pos, bool b_guess );
203 /*****************************************************************************
204 * Open: initializes ES structures
205 *****************************************************************************/
206 static int Open( vlc_object_t * p_this )
208 demux_t *p_demux = (demux_t*)p_this;
209 demux_sys_t *p_sys;
210 const uint8_t *p_peek;
211 frame_header_t fh;
213 /* Check id */
214 if( vlc_stream_Peek( p_demux->s, &p_peek, 12 ) != 12 ||
215 ( strncmp( (char *)p_peek, "MythTVVideo", 11 ) &&
216 strncmp( (char *)p_peek, "NuppelVideo", 11 ) ) )
217 return VLC_EGENERIC;
219 p_sys = malloc( sizeof( demux_sys_t ) );
220 if( p_sys == NULL )
221 return VLC_ENOMEM;
222 memset( p_sys, 0, sizeof( demux_sys_t ) );
223 p_sys->p_es_video = NULL;
224 p_sys->p_es_audio = NULL;
225 p_sys->p_extra_f = NULL;
226 p_sys->i_pcr = -1;
227 p_sys->b_index = false;
228 p_sys->i_total_frames = -1;
229 p_sys->i_total_length = -1;
230 demux_IndexInit( &p_sys->idx );
232 p_demux->p_sys = p_sys;
234 /* Info about the stream */
235 vlc_stream_Control( p_demux->s, STREAM_CAN_SEEK, &p_sys->b_seekable );
236 #if 0
237 if( p_sys->b_seekable )
238 msg_Dbg( p_demux, "stream is seekable" );
239 else
240 msg_Dbg( p_demux, "stream is NOT seekable" );
241 #endif
243 if( HeaderLoad( p_demux, &p_sys->hdr ) )
244 goto error;
246 /* Load 'D' */
247 if( FrameHeaderLoad( p_demux, &fh ) || fh.i_type != 'D' )
248 goto error;
249 if( fh.i_length > 0 )
251 if( fh.i_compression == 'F' || fh.i_compression == 'R' )
253 /* libavcodec extra data */
254 p_sys->i_extra_f = fh.i_length;
255 p_sys->p_extra_f = malloc( fh.i_length );
256 if( p_sys->p_extra_f == NULL || vlc_stream_Read( p_demux->s,
257 p_sys->p_extra_f, fh.i_length ) != fh.i_length )
258 goto error;
260 else
262 msg_Warn( p_demux, "unsupported 'D' frame (c=%c)", fh.i_compression );
263 if( vlc_stream_Read( p_demux->s, NULL,
264 fh.i_length ) != fh.i_length )
265 goto error;
269 /* Check and load extented */
270 if( vlc_stream_Peek( p_demux->s, &p_peek, 1 ) != 1 )
271 goto error;
272 if( p_peek[0] == 'X' )
274 if( FrameHeaderLoad( p_demux, &fh ) )
275 goto error;
276 if( fh.i_length != 512 )
277 goto error;
279 if( ExtendedHeaderLoad( p_demux, &p_sys->exh ) )
280 goto error;
282 if( !p_sys->b_seekable )
283 msg_Warn( p_demux, "stream is not seekable, skipping seektable" );
284 else if( SeekTableLoad( p_demux, p_sys ) )
286 p_sys->b_index = false;
287 msg_Warn( p_demux, "Seektable is broken, seek won't be accurate" );
290 else
292 /* XXX: for now only file with extended chunk are supported
293 * why: because else we need to have support for rtjpeg+stupid nuv shit */
294 msg_Err( p_demux, "VLC doesn't support NUV without extended chunks (please upload samples)" );
295 goto error;
298 /* Create audio/video (will work only with extended header and audio=mp3 */
299 if( p_sys->hdr.i_video_blocks != 0 )
301 es_format_t fmt;
303 es_format_Init( &fmt, VIDEO_ES, p_sys->exh.i_video_fcc );
304 fmt.video.i_width = p_sys->hdr.i_width;
305 fmt.video.i_height = p_sys->hdr.i_height;
306 fmt.video.i_visible_width = fmt.video.i_width;
307 fmt.video.i_visible_height = fmt.video.i_height;
308 fmt.i_extra = p_sys->i_extra_f;
309 fmt.p_extra = p_sys->p_extra_f;
310 fmt.video.i_sar_num = p_sys->hdr.d_aspect * fmt.video.i_height;
311 fmt.video.i_sar_den = fmt.video.i_width;
313 p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
315 if( p_sys->hdr.i_audio_blocks != 0 )
317 es_format_t fmt;
319 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_MPGA );
320 fmt.audio.i_rate = p_sys->exh.i_audio_sample_rate;
321 fmt.audio.i_bitspersample = p_sys->exh.i_audio_bits_per_sample;
323 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
325 if( p_sys->hdr.i_text_blocks != 0 )
327 msg_Warn( p_demux, "text not yet supported (upload samples)" );
330 p_sys->i_first_frame_offset = vlc_stream_Tell( p_demux->s );
332 /* Fill p_demux fields */
333 p_demux->pf_demux = Demux;
334 p_demux->pf_control = Control;
336 return VLC_SUCCESS;
338 error:
339 msg_Warn( p_demux, "cannot load Nuv file" );
340 Close( p_this );
341 p_demux->p_sys = NULL;
342 return VLC_EGENERIC;
345 /*****************************************************************************
346 * Close: frees unused data
347 *****************************************************************************/
348 static void Close( vlc_object_t * p_this )
350 demux_t *p_demux = (demux_t*)p_this;
351 demux_sys_t *p_sys = p_demux->p_sys;
353 free( p_sys->p_extra_f );
354 demux_IndexClean( &p_sys->idx );
355 free( p_sys );
358 /*****************************************************************************
359 * Demux: reads and demuxes data packets
360 *****************************************************************************
361 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
362 *****************************************************************************/
363 static int Demux( demux_t *p_demux )
365 demux_sys_t *p_sys = p_demux->p_sys;
366 frame_header_t fh;
367 block_t *p_data;
369 for( ;; )
371 if( FrameHeaderLoad( p_demux, &fh ) )
372 return VLC_DEMUXER_EOF;
374 if( fh.i_type == 'A' || fh.i_type == 'V' )
375 break;
377 /* TODO add support for some block type */
379 if( fh.i_type != 'R' && fh.i_length > 0 )
381 if( vlc_stream_Read( p_demux->s, NULL,
382 fh.i_length ) != fh.i_length )
383 return VLC_DEMUXER_EGENERIC;
387 /* */
388 if( ( p_data = vlc_stream_Block( p_demux->s, fh.i_length ) ) == NULL )
389 return VLC_DEMUXER_EOF;
391 p_data->i_dts = VLC_TS_0 + (int64_t)fh.i_timecode * 1000;
392 p_data->i_pts = (fh.i_type == 'V') ? VLC_TS_INVALID : p_data->i_dts;
394 /* only add keyframes to index */
395 if( !fh.i_keyframe && !p_sys->b_index )
396 demux_IndexAppend( &p_sys->idx,
397 p_data->i_dts - VLC_TS_0,
398 vlc_stream_Tell(p_demux->s) - NUV_FH_SIZE );
400 /* */
401 if( p_sys->i_pcr < 0 || p_sys->i_pcr < p_data->i_dts - VLC_TS_0 )
403 p_sys->i_pcr = p_data->i_dts - VLC_TS_0;
404 es_out_SetPCR( p_demux->out, VLC_TS_0 + p_sys->i_pcr );
407 if( fh.i_type == 'A' && p_sys->p_es_audio )
409 if( fh.i_compression == '3' )
410 es_out_Send( p_demux->out, p_sys->p_es_audio, p_data );
411 else
413 msg_Dbg( p_demux, "unsupported compression %c for audio (upload samples)", fh.i_compression );
414 block_Release( p_data );
417 else if( fh.i_type == 'V' && p_sys->p_es_video )
419 if( fh.i_compression >='0' && fh.i_compression <='3' )
421 /* for rtjpeg data, the header is also needed */
422 p_data = block_Realloc( p_data, NUV_FH_SIZE, fh.i_length );
423 if( unlikely(!p_data) )
424 return VLC_DEMUXER_EGENERIC;
425 memcpy( p_data->p_buffer, p_sys->fh_buffer, NUV_FH_SIZE );
427 /* 0,1,2,3 -> rtjpeg, >=4 mpeg4 */
428 if( fh.i_compression >= '0' )
429 es_out_Send( p_demux->out, p_sys->p_es_video, p_data );
430 else
432 msg_Dbg( p_demux, "unsupported compression %c for video (upload samples)", fh.i_compression );
433 block_Release( p_data );
436 else
438 block_Release( p_data );
441 return VLC_DEMUXER_SUCCESS;
444 /*****************************************************************************
445 * Control:
446 *****************************************************************************/
447 static int Control( demux_t *p_demux, int i_query, va_list args )
449 demux_sys_t *p_sys = p_demux->p_sys;
451 double f, *pf;
452 int64_t i64, *pi64;
454 switch( i_query )
456 case DEMUX_CAN_SEEK:
457 *va_arg( args, bool * ) = p_sys->b_seekable;
458 return VLC_SUCCESS;
460 case DEMUX_GET_POSITION:
461 pf = va_arg( args, double * );
463 if( p_sys->i_total_length > 0 && p_sys->i_pcr >= 0 )
465 *pf = (double)p_sys->i_pcr / (double)p_sys->i_total_length;
467 else
469 i64 = stream_Size( p_demux->s );
470 if( i64 > 0 )
472 const double f_current = vlc_stream_Tell( p_demux->s );
473 *pf = f_current / (double)i64;
475 else
477 *pf = 0.0;
480 return VLC_SUCCESS;
482 case DEMUX_SET_POSITION:
484 int64_t i_pos;
486 f = va_arg( args, double );
488 p_sys->i_pcr = -1;
490 /* first try to see if we can seek based on time (== GET_LENGTH works) */
491 if( p_sys->i_total_length > 0 && ( i_pos = demux_IndexConvertTime( &p_sys->idx, p_sys->i_total_length * f ) ) > 0 )
492 return ControlSetPosition( p_demux, i_pos, false );
494 /* if not search based on total stream size */
495 else if( ( i_pos = demux_IndexFindOffset( &p_sys->idx, stream_Size( p_demux->s ) * f ) ) >= 0 )
496 return ControlSetPosition( p_demux, i_pos, false );
498 else if( ( i_pos = p_sys->i_first_frame_offset + ( stream_Size( p_demux->s ) - p_sys->i_first_frame_offset ) * f ) >= 0 )
499 return ControlSetPosition( p_demux, i_pos, true );
501 else
502 return VLC_EGENERIC;
505 case DEMUX_GET_TIME:
506 pi64 = va_arg( args, int64_t * );
507 *pi64 = p_sys->i_pcr >= 0 ? p_sys->i_pcr : 0;
508 return VLC_SUCCESS;
510 case DEMUX_SET_TIME:
512 int64_t i_pos;
513 i64 = va_arg( args, int64_t );
515 p_sys->i_pcr = -1;
517 i_pos = demux_IndexConvertTime( &p_sys->idx, i64 );
518 if( i_pos < 0 )
519 return VLC_EGENERIC;
520 else
521 return ControlSetPosition( p_demux, i_pos, false );
524 case DEMUX_GET_LENGTH:
525 pi64 = va_arg( args, int64_t * );
526 if( p_sys->i_total_length >= 0 )
528 *pi64 = p_sys->i_total_length;
529 return VLC_SUCCESS;
531 else if( vlc_stream_Tell( p_demux->s ) > p_sys->i_first_frame_offset )
533 /* This should give an approximation of the total duration */
534 *pi64 = (double)( stream_Size( p_demux->s ) - p_sys->i_first_frame_offset ) /
535 (double)( vlc_stream_Tell( p_demux->s ) - p_sys->i_first_frame_offset )
536 * (double)( p_sys->i_pcr >= 0 ? p_sys->i_pcr : 0 );
537 return VLC_SUCCESS;
539 else
540 return VLC_EGENERIC;
542 case DEMUX_GET_FPS:
543 pf = va_arg( args, double * );
544 *pf = p_sys->hdr.d_fps;
545 return VLC_SUCCESS;
547 case DEMUX_GET_META:
548 return VLC_EGENERIC;
550 case DEMUX_CAN_PAUSE:
551 case DEMUX_SET_PAUSE_STATE:
552 case DEMUX_CAN_CONTROL_PACE:
553 case DEMUX_GET_PTS_DELAY:
554 return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
556 default:
557 return VLC_EGENERIC;
561 static int ControlSetPosition( demux_t *p_demux, int64_t i_pos, bool b_guess )
563 demux_sys_t *p_sys = p_demux->p_sys;
565 if( i_pos < 0 )
566 return VLC_EGENERIC;
568 /* if we can seek in the stream */
569 if( p_sys->b_seekable && !b_guess )
571 if( vlc_stream_Seek( p_demux->s, i_pos ) )
572 return VLC_EGENERIC;
574 else
576 /* forward seek */
577 if( i_pos > vlc_stream_Tell( p_demux->s ) )
579 msg_Dbg( p_demux, "unable to seek, skipping frames (slow)" );
581 else
583 msg_Warn( p_demux, "unable to seek, only forward seeking is possible" );
585 return VLC_EGENERIC;
589 for( ;; )
591 frame_header_t fh;
592 int64_t i_tell;
594 if( ( i_tell = vlc_stream_Tell( p_demux->s ) ) >= i_pos )
595 break;
597 if( FrameHeaderLoad( p_demux, &fh ) )
598 return VLC_EGENERIC;
600 if( fh.i_type == 'A' || fh.i_type == 'V' )
602 if( !fh.i_keyframe && !p_sys->b_index )
603 demux_IndexAppend( &p_sys->idx,(int64_t)fh.i_timecode*1000, i_tell );
606 if( fh.i_type != 'R' && fh.i_length > 0 )
608 if( vlc_stream_Read( p_demux->s, NULL,
609 fh.i_length ) != fh.i_length )
610 return VLC_EGENERIC;
614 return VLC_SUCCESS;
617 /*****************************************************************************
619 *****************************************************************************/
620 static inline void GetDoubleLE( double *pd, void *src )
622 /* FIXME works only if sizeof(double) == 8 */
623 #ifdef WORDS_BIGENDIAN
624 uint8_t *p = (uint8_t*)pd, *q = (uint8_t*)src;
625 int i;
626 for( i = 0; i < 8; i++ )
627 p[i] = q[7-i];
628 #else
629 memcpy( pd, src, 8 );
630 #endif
633 /* HeaderLoad:
635 static int HeaderLoad( demux_t *p_demux, header_t *h )
637 uint8_t buffer[72];
639 if( vlc_stream_Read( p_demux->s, buffer, 72 ) != 72 )
640 return VLC_EGENERIC;
642 /* XXX: they are alignment to take care of (another broken format) */
643 memcpy( h->id, &buffer[ 0], 12 );
644 memcpy( h->version, &buffer[12], 5 );
645 h->i_width = GetDWLE( &buffer[20] );
646 h->i_height = GetDWLE( &buffer[24] );
647 h->i_width_desired = GetDWLE( &buffer[28] );
648 h->i_height_desired = GetDWLE( &buffer[32] );
649 h->i_mode = buffer[36];
650 GetDoubleLE( &h->d_aspect, &buffer[40] );
651 GetDoubleLE( &h->d_fps, &buffer[48] );
652 h->i_video_blocks = GetDWLE( &buffer[56] );
653 h->i_audio_blocks = GetDWLE( &buffer[60] );
654 h->i_text_blocks = GetDWLE( &buffer[64] );
655 h->i_keyframe_distance = GetDWLE( &buffer[68] );
656 #if 0
657 msg_Dbg( p_demux, "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d",
658 h->id, h->version, h->i_width, h->i_height, h->d_aspect,
659 h->d_fps, h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks,
660 h->i_keyframe_distance );
661 #endif
663 return (h->d_fps) ? VLC_SUCCESS : VLC_EGENERIC;
666 /* FrameHeaderLoad:
668 static int FrameHeaderLoad( demux_t *p_demux, frame_header_t *h )
670 demux_sys_t *p_sys = p_demux->p_sys;
671 uint8_t* buffer = p_sys->fh_buffer;
673 if( vlc_stream_Read( p_demux->s, buffer, 12 ) != 12 )
674 return VLC_EGENERIC;
676 h->i_type = buffer[0];
677 h->i_compression = buffer[1];
678 h->i_keyframe = buffer[2];
679 h->i_filters = buffer[3];
681 h->i_timecode = GetDWLE( &buffer[4] );
682 h->i_length = GetDWLE( &buffer[8] );
683 #if 0
684 msg_Dbg( p_demux, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
685 h->i_type,
686 h->i_compression ? h->i_compression : ' ',
687 h->i_keyframe ? h->i_keyframe : ' ',
688 h->i_filters,
689 h->i_timecode, h->i_length );
690 #endif
691 return VLC_SUCCESS;
694 static int ExtendedHeaderLoad( demux_t *p_demux, extended_header_t *h )
696 uint8_t buffer[512];
698 if( vlc_stream_Read( p_demux->s, buffer, 512 ) != 512 )
699 return VLC_EGENERIC;
701 h->i_version = GetDWLE( &buffer[0] );
702 h->i_video_fcc = VLC_FOURCC( buffer[4], buffer[5], buffer[6], buffer[7] );
703 h->i_audio_fcc = VLC_FOURCC( buffer[8], buffer[9], buffer[10], buffer[11] );
704 h->i_audio_sample_rate = GetDWLE( &buffer[12] );
705 h->i_audio_bits_per_sample = GetDWLE( &buffer[16] );
706 h->i_audio_channels = GetDWLE( &buffer[20] );
707 h->i_audio_compression_ratio = GetDWLE( &buffer[24] );
708 h->i_audio_quality = GetDWLE( &buffer[28] );
709 h->i_rtjpeg_quality = GetDWLE( &buffer[32] );
710 h->i_rtjpeg_luma_filter = GetDWLE( &buffer[36] );
711 h->i_rtjpeg_chroma_filter = GetDWLE( &buffer[40] );
712 h->i_lavc_bitrate = GetDWLE( &buffer[44] );
713 h->i_lavc_qmin = GetDWLE( &buffer[48] );
714 h->i_lavc_qmin = GetDWLE( &buffer[52] );
715 h->i_lavc_maxqdiff = GetDWLE( &buffer[56] );
716 h->i_seektable_offset = GetQWLE( &buffer[60] );
717 h->i_keyframe_adjust_offset= GetQWLE( &buffer[68] );
718 #if 0
719 msg_Dbg( p_demux, "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
720 "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%"PRIi64" keyfao=%"PRIi64,
721 h->i_version,
722 (char*)&h->i_video_fcc,
723 (char*)&h->i_audio_fcc, h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
724 h->i_audio_compression_ratio, h->i_audio_quality,
725 h->i_rtjpeg_quality, h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter,
726 h->i_lavc_bitrate, h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff,
727 h->i_seektable_offset, h->i_keyframe_adjust_offset );
728 #endif
729 return VLC_SUCCESS;
733 typedef struct
735 int64_t i_file_offset;
736 int32_t i_keyframe_number;
737 } seektable_entry_t;
738 typedef struct
740 int32_t i_adjust;
741 int32_t i_keyframe_number;
742 } kfatable_entry_t;
745 static int SeekTableLoad( demux_t *p_demux, demux_sys_t *p_sys )
747 frame_header_t fh;
748 int64_t i_original_pos;
749 int64_t i_time, i_offset;
750 int keyframe, last_keyframe = 0, frame = 0, kfa_entry_id = 0;
752 if( p_sys->exh.i_seektable_offset <= 0 )
753 return VLC_SUCCESS;
755 /* Save current position */
756 i_original_pos = vlc_stream_Tell( p_demux->s );
757 #if 0
758 msg_Dbg( p_demux, "current offset %"PRIi64, i_original_pos );
760 msg_Dbg( p_demux, "seeking in stream to %"PRIi64, p_sys->exh.i_seektable_offset );
761 #endif
762 if( vlc_stream_Seek( p_demux->s, p_sys->exh.i_seektable_offset ) )
763 return VLC_EGENERIC;
765 if( FrameHeaderLoad( p_demux, &fh ) )
766 return VLC_EGENERIC;
768 if( fh.i_type != 'Q' )
770 msg_Warn( p_demux, "invalid seektable, frame type=%c", fh.i_type );
771 vlc_stream_Seek( p_demux->s, i_original_pos );
772 return VLC_EGENERIC;
775 /* */
776 uint8_t *p_seek_table = malloc( fh.i_length );
777 if( p_seek_table == NULL )
778 return VLC_ENOMEM;
780 if( vlc_stream_Read( p_demux->s, p_seek_table,
781 fh.i_length ) != fh.i_length )
783 free( p_seek_table );
784 return VLC_EGENERIC;
786 const int32_t i_seek_elements = fh.i_length / 12;
788 /* Get keyframe adjust offsets */
789 int32_t i_kfa_elements = 0;
790 uint8_t *p_kfa_table = NULL;
792 if( p_sys->exh.i_keyframe_adjust_offset > 0 )
794 msg_Dbg( p_demux, "seeking in stream to %"PRIi64, p_sys->exh.i_keyframe_adjust_offset );
795 if( vlc_stream_Seek( p_demux->s, p_sys->exh.i_keyframe_adjust_offset ) )
797 free( p_seek_table );
798 return VLC_EGENERIC;
801 if( FrameHeaderLoad( p_demux, &fh ) )
803 free( p_seek_table );
804 return VLC_EGENERIC;
807 if( fh.i_type == 'K' && fh.i_length >= 8 )
809 p_kfa_table = malloc( fh.i_length );
811 if( p_kfa_table == NULL )
813 free( p_seek_table );
814 return VLC_ENOMEM;
817 if( vlc_stream_Read( p_demux->s, p_kfa_table,
818 fh.i_length ) != fh.i_length )
820 free( p_seek_table );
821 free( p_kfa_table );
822 return VLC_EGENERIC;
825 i_kfa_elements = fh.i_length / 8;
829 if( i_kfa_elements > 0 )
830 msg_Warn( p_demux, "untested keyframe adjust support, upload samples" );
832 for( int32_t j = 0; j < i_seek_elements; j++)
834 #if 0
835 uint8_t* p = p_seek_table + j * 12;
836 msg_Dbg( p_demux, "%x %x %x %x %x %x %x %x %x %x %x %x",
837 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11]);
838 #endif
839 keyframe = GetDWLE( p_seek_table + j * 12 + 8 );
841 frame += (keyframe - last_keyframe) * p_sys->hdr.i_keyframe_distance;
843 if( kfa_entry_id < i_kfa_elements && *(int32_t*)(p_kfa_table + kfa_entry_id * 12 + 4) == j )
845 frame -= *(int32_t*)(p_kfa_table + kfa_entry_id * 12);
846 msg_Dbg( p_demux, "corrected keyframe %d with current frame number %d (corrected with %d)",
847 keyframe, frame, *(int32_t*)(p_kfa_table + kfa_entry_id * 12) );
848 kfa_entry_id++;
851 i_time = (double)( (int64_t)frame * 1000000 ) / p_sys->hdr.d_fps;
852 i_offset = GetQWLE( p_seek_table + j * 12 );
854 if( i_offset == 0 && i_time != 0 )
855 msg_Dbg( p_demux, "invalid file offset %d %"PRIi64, keyframe, i_offset );
856 else
858 demux_IndexAppend( &p_sys->idx, i_time , i_offset );
859 #if 0
860 msg_Dbg( p_demux, "adding entry position %d %"PRIi64 " file offset %"PRIi64, keyframe, i_time, i_offset );
861 #endif
864 last_keyframe = keyframe;
867 p_sys->i_total_frames = (int64_t)frame;
869 p_sys->b_index = true;
871 p_sys->i_total_length = p_sys->i_total_frames * 1000000 / p_sys->hdr.d_fps;
873 msg_Dbg( p_demux, "index table loaded (%d elements)", i_seek_elements );
875 if( i_kfa_elements )
876 free ( p_kfa_table );
878 free ( p_seek_table );
880 /* Restore stream position */
881 if( vlc_stream_Seek( p_demux->s, i_original_pos ) )
882 return VLC_EGENERIC;
884 return VLC_SUCCESS;
888 /*****************************************************************************/
889 #define DEMUX_INDEX_SIZE_MAX (100000)
890 static void demux_IndexInit( demux_index_t *p_idx )
892 p_idx->i_idx = 0;
893 p_idx->i_idx_max = 0;
894 p_idx->idx = NULL;
896 static void demux_IndexClean( demux_index_t *p_idx )
898 free( p_idx->idx );
899 p_idx->idx = NULL;
901 static void demux_IndexAppend( demux_index_t *p_idx,
902 int64_t i_time, int64_t i_offset )
904 /* Be sure to append new entry (we don't insert point) */
905 if( p_idx->i_idx > 0 && p_idx->idx[p_idx->i_idx-1].i_time >= i_time )
906 return;
908 /* */
909 if( p_idx->i_idx >= p_idx->i_idx_max )
911 if( p_idx->i_idx >= DEMUX_INDEX_SIZE_MAX )
913 /* Avoid too big index */
914 const int64_t i_length = p_idx->idx[p_idx->i_idx-1].i_time -
915 p_idx->idx[0].i_time;
916 const int i_count = DEMUX_INDEX_SIZE_MAX/2;
917 int i, j;
919 /* We try to reduce the resolution of the index by a factor 2 */
920 for( i = 1, j = 1; i < p_idx->i_idx; i++ )
922 if( p_idx->idx[i].i_time < j * i_length / i_count )
923 continue;
925 p_idx->idx[j++] = p_idx->idx[i];
927 p_idx->i_idx = j;
929 if( p_idx->i_idx > 3 * DEMUX_INDEX_SIZE_MAX / 4 )
931 /* We haven't created enough space
932 * (This method won't create a good index but work for sure) */
933 for( i = 0; i < p_idx->i_idx/2; i++ )
934 p_idx->idx[i] = p_idx->idx[2*i];
935 p_idx->i_idx /= 2;
938 else
940 if(INT_MAX - 1000 < p_idx->i_idx_max ||
941 (SIZE_MAX / sizeof(demux_index_entry_t)) - p_idx->i_idx_max < 1000)
942 return;
943 size_t i_realloc = (1000 + p_idx->i_idx_max) * sizeof(demux_index_entry_t);
944 demux_index_entry_t *p_realloc = realloc( p_idx->idx, i_realloc );
945 if( !p_realloc )
946 return;
947 p_idx->i_idx_max += 1000;
948 p_idx->idx = p_realloc;
952 /* */
953 p_idx->idx[p_idx->i_idx].i_time = i_time;
954 p_idx->idx[p_idx->i_idx].i_offset = i_offset;
956 p_idx->i_idx++;
958 static int64_t demux_IndexConvertTime( demux_index_t *p_idx, int64_t i_time )
960 int i_min = 0;
961 int i_max = p_idx->i_idx-1;
963 /* Empty index */
964 if( p_idx->i_idx <= 0 )
965 return -1;
967 /* Special border case */
968 if( i_time <= p_idx->idx[0].i_time )
969 return p_idx->idx[0].i_offset;
970 if( i_time >= p_idx->idx[i_max].i_time )
971 return p_idx->idx[i_max].i_offset;
973 /* Dicho */
974 for( ;; )
976 int i_med;
978 if( i_max - i_min <= 1 )
979 break;
981 i_med = (i_min+i_max)/2;
982 if( p_idx->idx[i_med].i_time < i_time )
983 i_min = i_med;
984 else if( p_idx->idx[i_med].i_time > i_time )
985 i_max = i_med;
986 else
987 return p_idx->idx[i_med].i_offset;
990 /* return nearest in time */
991 if( i_time - p_idx->idx[i_min].i_time < p_idx->idx[i_max].i_time - i_time )
992 return p_idx->idx[i_min].i_offset;
993 else
994 return p_idx->idx[i_max].i_offset;
998 static int64_t demux_IndexFindOffset( demux_index_t *p_idx, int64_t i_offset )
1000 int i_min = 0;
1001 int i_max = p_idx->i_idx-1;
1003 /* Empty index */
1004 if( p_idx->i_idx <= 0 )
1005 return -1;
1007 /* Special border case */
1008 if( i_offset <= p_idx->idx[0].i_offset )
1009 return p_idx->idx[0].i_offset;
1010 if( i_offset == p_idx->idx[i_max].i_offset )
1011 return p_idx->idx[i_max].i_offset;
1012 if( i_offset > p_idx->idx[i_max].i_offset )
1013 return -1;
1015 /* Dicho */
1016 for( ;; )
1018 int i_med;
1020 if( i_max - i_min <= 1 )
1021 break;
1023 i_med = (i_min+i_max)/2;
1024 if( p_idx->idx[i_med].i_offset < i_offset )
1025 i_min = i_med;
1026 else if( p_idx->idx[i_med].i_offset > i_offset )
1027 i_max = i_med;
1028 else
1029 return p_idx->idx[i_med].i_offset;
1032 /* return nearest */
1033 if( i_offset - p_idx->idx[i_min].i_offset < p_idx->idx[i_max].i_offset - i_offset )
1034 return p_idx->idx[i_min].i_offset;
1035 else
1036 return p_idx->idx[i_max].i_offset;