qt: playlist: use item title if available
[vlc.git] / modules / demux / nuv.c
blob0b0600d50875cc321e8ee1d472b62fc606f70fe8
1 /*****************************************************************************
2 * nuv.c:
3 *****************************************************************************
4 * Copyright (C) 2005 VLC authors and VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Gertjan Van Droogenbroeck <gertjanvd _PLUS_ vlc _AT_ gmail _DOT_ com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <math.h>
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
36 #include <limits.h>
38 /* TODO:
39 * - test
42 /*****************************************************************************
43 * Module descriptor
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close ( vlc_object_t * );
48 vlc_module_begin ()
49 set_category( CAT_INPUT )
50 set_subcategory( SUBCAT_INPUT_DEMUX )
51 set_description( N_("Nuv demuxer") )
52 set_capability( "demux", 145 )
53 set_callbacks( Open, Close )
54 add_shortcut( "nuv" )
55 vlc_module_end ()
57 /*****************************************************************************
58 * Local prototypes
59 *****************************************************************************/
60 static int Demux ( demux_t * );
61 static int Control( demux_t *, int, va_list );
63 /* */
64 typedef struct
66 vlc_tick_t i_time;
67 uint64_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 uint64_t demux_IndexConvertTime(demux_index_t *, vlc_tick_t time);
85 /* Find the nearest offset in the index */
86 static uint64_t demux_IndexFindOffset(demux_index_t *, uint64_t 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 typedef struct
175 header_t hdr;
176 extended_header_t exh;
178 vlc_tick_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 vlc_tick_t i_total_length;
193 /* first frame position (used for calculating size without seektable) */
194 uint64_t i_first_frame_offset;
195 } demux_sys_t;
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 *demux, uint64_t offset, bool 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 int id = 0;
300 if( p_sys->hdr.i_video_blocks != 0 )
302 es_format_t fmt;
304 es_format_Init( &fmt, VIDEO_ES, p_sys->exh.i_video_fcc );
305 fmt.video.i_width = p_sys->hdr.i_width;
306 fmt.video.i_height = p_sys->hdr.i_height;
307 fmt.video.i_visible_width = fmt.video.i_width;
308 fmt.video.i_visible_height = fmt.video.i_height;
309 fmt.i_extra = p_sys->i_extra_f;
310 fmt.p_extra = p_sys->p_extra_f;
311 fmt.video.i_sar_num = p_sys->hdr.d_aspect * fmt.video.i_height;
312 fmt.video.i_sar_den = fmt.video.i_width;
314 fmt.i_id = id++;
315 p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
317 if( p_sys->hdr.i_audio_blocks != 0 )
319 es_format_t fmt;
321 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_MPGA );
322 fmt.audio.i_rate = p_sys->exh.i_audio_sample_rate;
323 fmt.audio.i_bitspersample = p_sys->exh.i_audio_bits_per_sample;
325 fmt.i_id = id++;
326 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
328 if( p_sys->hdr.i_text_blocks != 0 )
330 msg_Warn( p_demux, "text not yet supported (upload samples)" );
333 p_sys->i_first_frame_offset = vlc_stream_Tell( p_demux->s );
335 /* Fill p_demux fields */
336 p_demux->pf_demux = Demux;
337 p_demux->pf_control = Control;
339 return VLC_SUCCESS;
341 error:
342 msg_Warn( p_demux, "cannot load Nuv file" );
343 Close( p_this );
344 p_demux->p_sys = NULL;
345 return VLC_EGENERIC;
348 /*****************************************************************************
349 * Close: frees unused data
350 *****************************************************************************/
351 static void Close( vlc_object_t * p_this )
353 demux_t *p_demux = (demux_t*)p_this;
354 demux_sys_t *p_sys = p_demux->p_sys;
356 free( p_sys->p_extra_f );
357 demux_IndexClean( &p_sys->idx );
358 free( p_sys );
361 /*****************************************************************************
362 * Demux: reads and demuxes data packets
363 *****************************************************************************
364 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
365 *****************************************************************************/
366 static int Demux( demux_t *p_demux )
368 demux_sys_t *p_sys = p_demux->p_sys;
369 frame_header_t fh;
370 block_t *p_data;
372 for( ;; )
374 if( FrameHeaderLoad( p_demux, &fh ) )
375 return VLC_DEMUXER_EOF;
377 if( fh.i_type == 'A' || fh.i_type == 'V' )
378 break;
380 /* TODO add support for some block type */
382 if( fh.i_type != 'R' && fh.i_length > 0 )
384 if( vlc_stream_Read( p_demux->s, NULL,
385 fh.i_length ) != fh.i_length )
386 return VLC_DEMUXER_EGENERIC;
390 /* */
391 if( ( p_data = vlc_stream_Block( p_demux->s, fh.i_length ) ) == NULL )
392 return VLC_DEMUXER_EOF;
394 p_data->i_dts = VLC_TICK_0 + (int64_t)fh.i_timecode * 1000;
395 p_data->i_pts = (fh.i_type == 'V') ? VLC_TICK_INVALID : p_data->i_dts;
397 /* only add keyframes to index */
398 if( !fh.i_keyframe && !p_sys->b_index )
399 demux_IndexAppend( &p_sys->idx,
400 p_data->i_dts - VLC_TICK_0,
401 vlc_stream_Tell(p_demux->s) - NUV_FH_SIZE );
403 /* */
404 if( p_sys->i_pcr < 0 || p_sys->i_pcr < p_data->i_dts - VLC_TICK_0 )
406 p_sys->i_pcr = p_data->i_dts - VLC_TICK_0;
407 es_out_SetPCR( p_demux->out, VLC_TICK_0 + p_sys->i_pcr );
410 if( fh.i_type == 'A' && p_sys->p_es_audio )
412 if( fh.i_compression == '3' )
413 es_out_Send( p_demux->out, p_sys->p_es_audio, p_data );
414 else
416 msg_Dbg( p_demux, "unsupported compression %c for audio (upload samples)", fh.i_compression );
417 block_Release( p_data );
420 else if( fh.i_type == 'V' && p_sys->p_es_video )
422 if( fh.i_compression >='0' && fh.i_compression <='3' )
424 /* for rtjpeg data, the header is also needed */
425 p_data = block_Realloc( p_data, NUV_FH_SIZE, fh.i_length );
426 if( unlikely(!p_data) )
427 return VLC_DEMUXER_EGENERIC;
428 memcpy( p_data->p_buffer, p_sys->fh_buffer, NUV_FH_SIZE );
430 /* 0,1,2,3 -> rtjpeg, >=4 mpeg4 */
431 if( fh.i_compression >= '0' )
432 es_out_Send( p_demux->out, p_sys->p_es_video, p_data );
433 else
435 msg_Dbg( p_demux, "unsupported compression %c for video (upload samples)", fh.i_compression );
436 block_Release( p_data );
439 else
441 block_Release( p_data );
444 return VLC_DEMUXER_SUCCESS;
447 /*****************************************************************************
448 * Control:
449 *****************************************************************************/
450 static int Control( demux_t *p_demux, int i_query, va_list args )
452 demux_sys_t *p_sys = p_demux->p_sys;
453 double *pf;
454 int64_t i64;
456 switch( i_query )
458 case DEMUX_CAN_SEEK:
459 *va_arg( args, bool * ) = p_sys->b_seekable;
460 return VLC_SUCCESS;
462 case DEMUX_GET_POSITION:
463 pf = va_arg( args, double * );
465 if( p_sys->i_total_length > 0 && p_sys->i_pcr >= 0 )
467 *pf = (double)p_sys->i_pcr / (double)p_sys->i_total_length;
469 else
471 i64 = stream_Size( p_demux->s );
472 if( i64 > 0 )
474 const double f_current = vlc_stream_Tell( p_demux->s );
475 *pf = f_current / (double)i64;
477 else
479 *pf = 0.0;
482 return VLC_SUCCESS;
484 case DEMUX_SET_POSITION:
486 double f = va_arg(args, double);
487 uint64_t offset;
489 p_sys->i_pcr = -1;
491 /* first try to see if we can seek based on time (== GET_LENGTH works) */
492 if (p_sys->i_total_length > 0) {
493 vlc_tick_t t = llround(p_sys->i_total_length * f);
495 offset = demux_IndexConvertTime(&p_sys->idx, t);
496 if (offset != UINT64_C(-1))
497 return ControlSetPosition(p_demux, offset, false);
500 /* if not search based on total stream size */
501 offset = stream_Size(p_demux->s) * f;
502 offset = demux_IndexFindOffset(&p_sys->idx, offset);
503 if (offset != UINT64_C(-1))
504 return ControlSetPosition(p_demux, offset, false);
506 offset = p_sys->i_first_frame_offset
507 + (uint64_t)((stream_Size(p_demux->s)
508 - p_sys->i_first_frame_offset) * f);
509 return ControlSetPosition(p_demux, offset, true);
512 case DEMUX_GET_TIME:
513 *va_arg( args, vlc_tick_t * ) = __MAX(p_sys->i_pcr, 0);
514 return VLC_SUCCESS;
516 case DEMUX_SET_TIME:
518 uint64_t i_pos;
520 p_sys->i_pcr = -1;
522 i_pos = demux_IndexConvertTime( &p_sys->idx, va_arg( args, vlc_tick_t ) );
523 if (i_pos != UINT64_C(-1))
524 return ControlSetPosition( p_demux, i_pos, false );
525 return VLC_EGENERIC;
528 case DEMUX_GET_LENGTH:
529 if( p_sys->i_total_length >= 0 )
531 *va_arg( args, vlc_tick_t * ) = p_sys->i_total_length;
532 return VLC_SUCCESS;
534 else if( vlc_stream_Tell( p_demux->s ) > p_sys->i_first_frame_offset )
536 /* This should give an approximation of the total duration */
537 if (p_sys->i_pcr <= 0)
538 *va_arg( args, vlc_tick_t * ) = 0;
539 else
540 *va_arg( args, vlc_tick_t * ) = p_sys->i_pcr *
541 (double)( stream_Size( p_demux->s ) - p_sys->i_first_frame_offset ) /
542 (double)( vlc_stream_Tell( p_demux->s ) - p_sys->i_first_frame_offset );
544 return VLC_SUCCESS;
546 else
547 return VLC_EGENERIC;
549 case DEMUX_GET_FPS:
550 pf = va_arg( args, double * );
551 *pf = p_sys->hdr.d_fps;
552 return VLC_SUCCESS;
554 case DEMUX_GET_META:
555 return VLC_EGENERIC;
557 case DEMUX_CAN_PAUSE:
558 case DEMUX_SET_PAUSE_STATE:
559 case DEMUX_CAN_CONTROL_PACE:
560 case DEMUX_GET_PTS_DELAY:
561 return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
563 default:
564 return VLC_EGENERIC;
569 static int ControlSetPosition(demux_t *p_demux, uint64_t offset, bool b_guess)
571 demux_sys_t *p_sys = p_demux->p_sys;
573 /* if we can seek in the stream */
574 if( p_sys->b_seekable && !b_guess )
576 if (vlc_stream_Seek(p_demux->s, offset))
577 return VLC_EGENERIC;
579 else
581 /* forward seek */
582 if (offset > vlc_stream_Tell(p_demux->s))
584 msg_Dbg( p_demux, "unable to seek, skipping frames (slow)" );
586 else
588 msg_Warn( p_demux, "unable to seek, only forward seeking is possible" );
590 return VLC_EGENERIC;
594 for( ;; )
596 frame_header_t fh;
597 uint64_t i_tell;
599 if ((i_tell = vlc_stream_Tell(p_demux->s)) >= offset)
600 break;
602 if( FrameHeaderLoad( p_demux, &fh ) )
603 return VLC_EGENERIC;
605 if( fh.i_type == 'A' || fh.i_type == 'V' )
607 if( !fh.i_keyframe && !p_sys->b_index )
608 demux_IndexAppend( &p_sys->idx,(int64_t)fh.i_timecode*1000, i_tell );
611 if( fh.i_type != 'R' && fh.i_length > 0 )
613 if( vlc_stream_Read( p_demux->s, NULL,
614 fh.i_length ) != fh.i_length )
615 return VLC_EGENERIC;
619 return VLC_SUCCESS;
622 /*****************************************************************************
624 *****************************************************************************/
625 static inline void GetDoubleLE( double *pd, void *src )
627 /* FIXME works only if sizeof(double) == 8 */
628 #ifdef WORDS_BIGENDIAN
629 uint8_t *p = (uint8_t*)pd, *q = (uint8_t*)src;
630 int i;
631 for( i = 0; i < 8; i++ )
632 p[i] = q[7-i];
633 #else
634 memcpy( pd, src, 8 );
635 #endif
638 /* HeaderLoad:
640 static int HeaderLoad( demux_t *p_demux, header_t *h )
642 uint8_t buffer[72];
644 if( vlc_stream_Read( p_demux->s, buffer, 72 ) != 72 )
645 return VLC_EGENERIC;
647 /* XXX: they are alignment to take care of (another broken format) */
648 memcpy( h->id, &buffer[ 0], 12 );
649 memcpy( h->version, &buffer[12], 5 );
650 h->i_width = GetDWLE( &buffer[20] );
651 h->i_height = GetDWLE( &buffer[24] );
652 h->i_width_desired = GetDWLE( &buffer[28] );
653 h->i_height_desired = GetDWLE( &buffer[32] );
654 h->i_mode = buffer[36];
655 GetDoubleLE( &h->d_aspect, &buffer[40] );
656 GetDoubleLE( &h->d_fps, &buffer[48] );
657 h->i_video_blocks = GetDWLE( &buffer[56] );
658 h->i_audio_blocks = GetDWLE( &buffer[60] );
659 h->i_text_blocks = GetDWLE( &buffer[64] );
660 h->i_keyframe_distance = GetDWLE( &buffer[68] );
661 #if 0
662 msg_Dbg( p_demux, "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d",
663 h->id, h->version, h->i_width, h->i_height, h->d_aspect,
664 h->d_fps, h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks,
665 h->i_keyframe_distance );
666 #endif
668 return (h->d_fps) ? VLC_SUCCESS : VLC_EGENERIC;
671 /* FrameHeaderLoad:
673 static int FrameHeaderLoad( demux_t *p_demux, frame_header_t *h )
675 demux_sys_t *p_sys = p_demux->p_sys;
676 uint8_t* buffer = p_sys->fh_buffer;
678 if( vlc_stream_Read( p_demux->s, buffer, 12 ) != 12 )
679 return VLC_EGENERIC;
681 h->i_type = buffer[0];
682 h->i_compression = buffer[1];
683 h->i_keyframe = buffer[2];
684 h->i_filters = buffer[3];
686 h->i_timecode = GetDWLE( &buffer[4] );
687 h->i_length = GetDWLE( &buffer[8] );
688 #if 0
689 msg_Dbg( p_demux, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
690 h->i_type,
691 h->i_compression ? h->i_compression : ' ',
692 h->i_keyframe ? h->i_keyframe : ' ',
693 h->i_filters,
694 h->i_timecode, h->i_length );
695 #endif
696 return VLC_SUCCESS;
699 static int ExtendedHeaderLoad( demux_t *p_demux, extended_header_t *h )
701 uint8_t buffer[512];
703 if( vlc_stream_Read( p_demux->s, buffer, 512 ) != 512 )
704 return VLC_EGENERIC;
706 h->i_version = GetDWLE( &buffer[0] );
707 h->i_video_fcc = VLC_FOURCC( buffer[4], buffer[5], buffer[6], buffer[7] );
708 h->i_audio_fcc = VLC_FOURCC( buffer[8], buffer[9], buffer[10], buffer[11] );
709 h->i_audio_sample_rate = GetDWLE( &buffer[12] );
710 h->i_audio_bits_per_sample = GetDWLE( &buffer[16] );
711 h->i_audio_channels = GetDWLE( &buffer[20] );
712 h->i_audio_compression_ratio = GetDWLE( &buffer[24] );
713 h->i_audio_quality = GetDWLE( &buffer[28] );
714 h->i_rtjpeg_quality = GetDWLE( &buffer[32] );
715 h->i_rtjpeg_luma_filter = GetDWLE( &buffer[36] );
716 h->i_rtjpeg_chroma_filter = GetDWLE( &buffer[40] );
717 h->i_lavc_bitrate = GetDWLE( &buffer[44] );
718 h->i_lavc_qmin = GetDWLE( &buffer[48] );
719 h->i_lavc_qmin = GetDWLE( &buffer[52] );
720 h->i_lavc_maxqdiff = GetDWLE( &buffer[56] );
721 h->i_seektable_offset = GetQWLE( &buffer[60] );
722 h->i_keyframe_adjust_offset= GetQWLE( &buffer[68] );
723 #if 0
724 msg_Dbg( p_demux, "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
725 "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%"PRIi64" keyfao=%"PRIi64,
726 h->i_version,
727 (char*)&h->i_video_fcc,
728 (char*)&h->i_audio_fcc, h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
729 h->i_audio_compression_ratio, h->i_audio_quality,
730 h->i_rtjpeg_quality, h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter,
731 h->i_lavc_bitrate, h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff,
732 h->i_seektable_offset, h->i_keyframe_adjust_offset );
733 #endif
734 return VLC_SUCCESS;
738 typedef struct
740 int64_t i_file_offset;
741 int32_t i_keyframe_number;
742 } seektable_entry_t;
743 typedef struct
745 int32_t i_adjust;
746 int32_t i_keyframe_number;
747 } kfatable_entry_t;
750 static int SeekTableLoad( demux_t *p_demux, demux_sys_t *p_sys )
752 frame_header_t fh;
753 int64_t i_original_pos;
754 int64_t i_time, i_offset;
755 int keyframe, last_keyframe = 0, frame = 0, kfa_entry_id = 0;
757 if( p_sys->exh.i_seektable_offset <= 0 )
758 return VLC_SUCCESS;
760 /* Save current position */
761 i_original_pos = vlc_stream_Tell( p_demux->s );
762 #if 0
763 msg_Dbg( p_demux, "current offset %"PRIi64, i_original_pos );
765 msg_Dbg( p_demux, "seeking in stream to %"PRIi64, p_sys->exh.i_seektable_offset );
766 #endif
767 if( vlc_stream_Seek( p_demux->s, p_sys->exh.i_seektable_offset ) )
768 return VLC_EGENERIC;
770 if( FrameHeaderLoad( p_demux, &fh ) )
771 return VLC_EGENERIC;
773 if( fh.i_type != 'Q' )
775 msg_Warn( p_demux, "invalid seektable, frame type=%c", fh.i_type );
776 return VLC_EGENERIC;
779 /* */
780 uint8_t *p_seek_table = malloc( fh.i_length );
781 if( p_seek_table == NULL )
782 return VLC_ENOMEM;
784 if( vlc_stream_Read( p_demux->s, p_seek_table,
785 fh.i_length ) != fh.i_length )
787 free( p_seek_table );
788 return VLC_EGENERIC;
790 const int32_t i_seek_elements = fh.i_length / 12;
792 /* Get keyframe adjust offsets */
793 int32_t i_kfa_elements = 0;
794 uint8_t *p_kfa_table = NULL;
796 if( p_sys->exh.i_keyframe_adjust_offset > 0 )
798 msg_Dbg( p_demux, "seeking in stream to %"PRIi64, p_sys->exh.i_keyframe_adjust_offset );
799 if( vlc_stream_Seek( p_demux->s, p_sys->exh.i_keyframe_adjust_offset ) )
801 free( p_seek_table );
802 return VLC_EGENERIC;
805 if( FrameHeaderLoad( p_demux, &fh ) )
807 free( p_seek_table );
808 return VLC_EGENERIC;
811 if( fh.i_type == 'K' && fh.i_length >= 8 )
813 p_kfa_table = malloc( fh.i_length );
815 if( p_kfa_table == NULL )
817 free( p_seek_table );
818 return VLC_ENOMEM;
821 if( vlc_stream_Read( p_demux->s, p_kfa_table,
822 fh.i_length ) != fh.i_length )
824 free( p_seek_table );
825 free( p_kfa_table );
826 return VLC_EGENERIC;
829 i_kfa_elements = fh.i_length / 8;
833 if( i_kfa_elements > 0 )
834 msg_Warn( p_demux, "untested keyframe adjust support, upload samples" );
836 for( int32_t j = 0; j < i_seek_elements; j++)
838 #if 0
839 uint8_t* p = p_seek_table + j * 12;
840 msg_Dbg( p_demux, "%x %x %x %x %x %x %x %x %x %x %x %x",
841 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11]);
842 #endif
843 keyframe = GetDWLE( p_seek_table + j * 12 + 8 );
845 frame += (keyframe - last_keyframe) * p_sys->hdr.i_keyframe_distance;
847 if( kfa_entry_id < i_kfa_elements && *(int32_t*)(p_kfa_table + kfa_entry_id * 12 + 4) == j )
849 frame -= *(int32_t*)(p_kfa_table + kfa_entry_id * 12);
850 msg_Dbg( p_demux, "corrected keyframe %d with current frame number %d (corrected with %d)",
851 keyframe, frame, *(int32_t*)(p_kfa_table + kfa_entry_id * 12) );
852 kfa_entry_id++;
855 i_offset = GetQWLE( p_seek_table + j * 12 );
857 if( i_offset == 0 && frame != 0 )
858 msg_Dbg( p_demux, "invalid file offset %d %"PRIi64, keyframe, i_offset );
859 else
861 i_time = (double)( (vlc_tick_t)frame * CLOCK_FREQ ) / p_sys->hdr.d_fps;
862 demux_IndexAppend( &p_sys->idx, i_time , i_offset );
863 #if 0
864 msg_Dbg( p_demux, "adding entry position %d %"PRIi64 " file offset %"PRIi64, keyframe, i_time, i_offset );
865 #endif
868 last_keyframe = keyframe;
871 p_sys->i_total_frames = (int64_t)frame;
873 p_sys->b_index = true;
875 p_sys->i_total_length = p_sys->i_total_frames * CLOCK_FREQ / p_sys->hdr.d_fps;
877 msg_Dbg( p_demux, "index table loaded (%d elements)", i_seek_elements );
879 if( i_kfa_elements )
880 free ( p_kfa_table );
882 free ( p_seek_table );
884 /* Restore stream position */
885 if( vlc_stream_Seek( p_demux->s, i_original_pos ) )
886 return VLC_EGENERIC;
888 return VLC_SUCCESS;
892 /*****************************************************************************/
893 #define DEMUX_INDEX_SIZE_MAX (100000)
894 static void demux_IndexInit( demux_index_t *p_idx )
896 p_idx->i_idx = 0;
897 p_idx->i_idx_max = 0;
898 p_idx->idx = NULL;
900 static void demux_IndexClean( demux_index_t *p_idx )
902 free( p_idx->idx );
903 p_idx->idx = NULL;
905 static void demux_IndexAppend( demux_index_t *p_idx,
906 int64_t i_time, int64_t i_offset )
908 /* Be sure to append new entry (we don't insert point) */
909 if( p_idx->i_idx > 0 && p_idx->idx[p_idx->i_idx-1].i_time >= i_time )
910 return;
912 /* */
913 if( p_idx->i_idx >= p_idx->i_idx_max )
915 if( p_idx->i_idx >= DEMUX_INDEX_SIZE_MAX )
917 /* Avoid too big index */
918 const int64_t i_length = p_idx->idx[p_idx->i_idx-1].i_time -
919 p_idx->idx[0].i_time;
920 const int i_count = DEMUX_INDEX_SIZE_MAX/2;
921 int i, j;
923 /* We try to reduce the resolution of the index by a factor 2 */
924 for( i = 1, j = 1; i < p_idx->i_idx; i++ )
926 if( p_idx->idx[i].i_time < j * i_length / i_count )
927 continue;
929 p_idx->idx[j++] = p_idx->idx[i];
931 p_idx->i_idx = j;
933 if( p_idx->i_idx > 3 * DEMUX_INDEX_SIZE_MAX / 4 )
935 /* We haven't created enough space
936 * (This method won't create a good index but work for sure) */
937 for( i = 0; i < p_idx->i_idx/2; i++ )
938 p_idx->idx[i] = p_idx->idx[2*i];
939 p_idx->i_idx /= 2;
942 else
944 if(INT_MAX - 1000 < p_idx->i_idx_max ||
945 (SIZE_MAX / sizeof(demux_index_entry_t)) - p_idx->i_idx_max < 1000)
946 return;
947 size_t i_realloc = (1000 + p_idx->i_idx_max) * sizeof(demux_index_entry_t);
948 demux_index_entry_t *p_realloc = realloc( p_idx->idx, i_realloc );
949 if( !p_realloc )
950 return;
951 p_idx->i_idx_max += 1000;
952 p_idx->idx = p_realloc;
956 /* */
957 p_idx->idx[p_idx->i_idx].i_time = i_time;
958 p_idx->idx[p_idx->i_idx].i_offset = i_offset;
960 p_idx->i_idx++;
963 static uint64_t demux_IndexConvertTime(demux_index_t *p_idx,
964 vlc_tick_t i_time)
966 int i_min = 0;
967 int i_max = p_idx->i_idx-1;
969 /* Empty index */
970 if( p_idx->i_idx <= 0 )
971 return -1;
973 /* Special border case */
974 if( i_time <= p_idx->idx[0].i_time )
975 return p_idx->idx[0].i_offset;
976 if( i_time >= p_idx->idx[i_max].i_time )
977 return p_idx->idx[i_max].i_offset;
979 /* Dicho */
980 for( ;; )
982 int i_med;
984 if( i_max - i_min <= 1 )
985 break;
987 i_med = (i_min+i_max)/2;
988 if( p_idx->idx[i_med].i_time < i_time )
989 i_min = i_med;
990 else if( p_idx->idx[i_med].i_time > i_time )
991 i_max = i_med;
992 else
993 return p_idx->idx[i_med].i_offset;
996 /* return nearest in time */
997 if( i_time - p_idx->idx[i_min].i_time < p_idx->idx[i_max].i_time - i_time )
998 return p_idx->idx[i_min].i_offset;
999 else
1000 return p_idx->idx[i_max].i_offset;
1004 static uint64_t demux_IndexFindOffset(demux_index_t *p_idx, uint64_t i_offset)
1006 int i_min = 0;
1007 int i_max = p_idx->i_idx-1;
1009 /* Empty index */
1010 if( p_idx->i_idx <= 0 )
1011 return -1;
1013 /* Special border case */
1014 if( i_offset <= p_idx->idx[0].i_offset )
1015 return p_idx->idx[0].i_offset;
1016 if( i_offset == p_idx->idx[i_max].i_offset )
1017 return p_idx->idx[i_max].i_offset;
1018 if( i_offset > p_idx->idx[i_max].i_offset )
1019 return -1;
1021 /* Dicho */
1022 for( ;; )
1024 int i_med;
1026 if( i_max - i_min <= 1 )
1027 break;
1029 i_med = (i_min+i_max)/2;
1030 if( p_idx->idx[i_med].i_offset < i_offset )
1031 i_min = i_med;
1032 else if( p_idx->idx[i_med].i_offset > i_offset )
1033 i_max = i_med;
1034 else
1035 return p_idx->idx[i_med].i_offset;
1038 /* return nearest */
1039 if( i_offset - p_idx->idx[i_min].i_offset < p_idx->idx[i_max].i_offset - i_offset )
1040 return p_idx->idx[i_min].i_offset;
1041 else
1042 return p_idx->idx[i_max].i_offset;