📄 nuv.c
字号:
return 1;}/***************************************************************************** * Control: *****************************************************************************/static int Control( demux_t *p_demux, int i_query, va_list args ){ demux_sys_t *p_sys = p_demux->p_sys; double f, *pf; int64_t i64, *pi64; switch( i_query ) { case DEMUX_GET_POSITION: pf = (double*)va_arg( args, double * ); i64 = stream_Size( p_demux->s ); if( i64 > 0 ) *pf = (double)stream_Tell( p_demux->s ) / (double)i64; else *pf = 0.0; return VLC_SUCCESS; case DEMUX_SET_POSITION: { int64_t i_pos; f = (double)va_arg( args, double ); i_pos = stream_Size( p_demux->s ) * f; i64 = demux_IndexFindOffset( &p_sys->idx, i_pos ); p_sys->i_pcr = -1; if( i64 >= 0 ) return stream_Seek( p_demux->s, i64 ); if( p_sys->idx.i_idx > 0 ) { if( stream_Seek( p_demux->s, p_sys->idx.idx[p_sys->idx.i_idx-1].i_offset ) ) return VLC_EGENERIC; } else { if( stream_Seek( p_demux->s, 0 ) ) return VLC_EGENERIC; } while( !p_demux->b_die ) { frame_header_t fh; int64_t i_tell; if( ( i_tell = stream_Tell( p_demux->s ) ) >= i_pos ) break; if( FrameHeaderLoad( p_demux, &fh ) ) return VLC_EGENERIC; if( fh.i_type == 'A' || fh.i_type == 'V' ) demux_IndexAppend( &p_sys->idx,(int64_t)fh.i_timecode*1000, i_tell ); if( fh.i_type != 'R' ) stream_Read( p_demux->s, NULL, fh.i_length ); } return VLC_SUCCESS; } case DEMUX_GET_TIME: pi64 = (int64_t*)va_arg( args, int64_t * ); *pi64 = p_sys->i_pcr >= 0 ? p_sys->i_pcr : 0; return VLC_SUCCESS; case DEMUX_SET_TIME: { int64_t i_pos; i64 = (int64_t)va_arg( args, int64_t ); i_pos = demux_IndexConvertTime( &p_sys->idx, i64 ); if( i_pos < 0 ) return VLC_EGENERIC; if( stream_Seek( p_demux->s, i_pos ) ) return VLC_EGENERIC; p_sys->i_pcr = -1; while( !p_demux->b_die ) { frame_header_t fh; if( FrameHeaderLoad( p_demux, &fh ) ) return VLC_EGENERIC; if( fh.i_type == 'A' || fh.i_type == 'V' ) { int64_t i_time = (int64_t)fh.i_timecode*1000; int64_t i_tell = stream_Tell(p_demux->s)-12; demux_IndexAppend( &p_sys->idx, i_time, i_tell ); if( i_time >= i64 ) return stream_Seek( p_demux->s, i_tell ); } if( fh.i_type != 'R' ) stream_Read( p_demux->s, NULL, fh.i_length ); } return VLC_SUCCESS; } case DEMUX_GET_LENGTH: pi64 = (int64_t*)va_arg( args, int64_t * ); return VLC_EGENERIC; case DEMUX_GET_FPS: pf = (double*)va_arg( args, double * ); *pf = p_sys->hdr.d_fps; return VLC_SUCCESS; case DEMUX_GET_META: default: return VLC_EGENERIC; }}/***************************************************************************** * *****************************************************************************/static inline void GetDoubleLE( double *pd, void *src ){ /* FIXME works only if sizeof(double) == 8 */#ifdef WORDS_BIGENDIAN uint8_t *p = (uint8_t*)pd, *q = (uint8_t*)src; int i; for( i = 0; i < 8; i++ ) p[i] = q[7-i];#else memcpy( pd, src, 8 );#endif}/* HeaderLoad: */static int HeaderLoad( demux_t *p_demux, header_t *h ){ uint8_t buffer[72]; if( stream_Read( p_demux->s, buffer, 72 ) != 72 ) return VLC_EGENERIC; /* XXX: they are alignment to take care of (another broken format) */ memcpy( h->id, &buffer[ 0], 12 ); memcpy( h->version, &buffer[12], 5 ); h->i_width = GetDWLE( &buffer[20] ); h->i_height = GetDWLE( &buffer[24] ); h->i_width_desired = GetDWLE( &buffer[28] ); h->i_height_desired = GetDWLE( &buffer[32] ); h->i_mode = buffer[36]; GetDoubleLE( &h->d_aspect, &buffer[40] ); GetDoubleLE( &h->d_fps, &buffer[48] ); h->i_video_blocks = GetDWLE( &buffer[56] ); h->i_audio_blocks = GetDWLE( &buffer[60] ); h->i_text_blocks = GetDWLE( &buffer[64] ); h->i_keyframe_distance = GetDWLE( &buffer[68] );#if 0 msg_Dbg( p_demux, "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d", h->id, h->version, h->i_width, h->i_height, h->d_aspect, h->d_fps, h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks, h->i_keyframe_distance );#endif return VLC_SUCCESS;}/* FrameHeaderLoad: */static int FrameHeaderLoad( demux_t *p_demux, frame_header_t *h ){ uint8_t buffer[12]; if( stream_Read( p_demux->s, buffer, 12 ) != 12 ) return VLC_EGENERIC; h->i_type = buffer[0]; h->i_compression = buffer[1]; h->i_keyframe = buffer[2]; h->i_filters = buffer[3]; h->i_timecode = GetDWLE( &buffer[4] ); h->i_length = GetDWLE( &buffer[8] );#if 0 msg_Dbg( p_demux, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d", h->i_type, h->i_compression ? h->i_compression : ' ', h->i_keyframe ? h->i_keyframe : ' ', h->i_filters, h->i_timecode, h->i_length );#endif return VLC_SUCCESS;}static int ExtendedHeaderLoad( demux_t *p_demux, extended_header_t *h ){ uint8_t buffer[512]; if( stream_Read( p_demux->s, buffer, 512 ) != 512 ) return VLC_EGENERIC; h->i_version = GetDWLE( &buffer[0] ); h->i_video_fcc = VLC_FOURCC( buffer[4], buffer[5], buffer[6], buffer[7] ); h->i_audio_fcc = VLC_FOURCC( buffer[8], buffer[9], buffer[10], buffer[11] ); h->i_audio_sample_rate = GetDWLE( &buffer[12] ); h->i_audio_bits_per_sample = GetDWLE( &buffer[16] ); h->i_audio_channels = GetDWLE( &buffer[20] ); h->i_audio_compression_ratio = GetDWLE( &buffer[24] ); h->i_audio_quality = GetDWLE( &buffer[28] ); h->i_rtjpeg_quality = GetDWLE( &buffer[32] ); h->i_rtjpeg_luma_filter = GetDWLE( &buffer[36] ); h->i_rtjpeg_chroma_filter = GetDWLE( &buffer[40] ); h->i_lavc_bitrate = GetDWLE( &buffer[44] ); h->i_lavc_qmin = GetDWLE( &buffer[48] ); h->i_lavc_qmin = GetDWLE( &buffer[52] ); h->i_lavc_maxqdiff = GetDWLE( &buffer[56] ); h->i_seekable_offset = GetQWLE( &buffer[60] ); h->i_keyframe_adjust_offset= GetQWLE( &buffer[68] );#if 0 msg_Dbg( p_demux, "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d" "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld", h->i_version, (char*)&h->i_video_fcc, (char*)&h->i_audio_fcc, h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels, h->i_audio_compression_ratio, h->i_audio_quality, h->i_rtjpeg_quality, h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter, h->i_lavc_bitrate, h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff, h->i_seekable_offset, h->i_keyframe_adjust_offset );#endif return VLC_SUCCESS;}/*****************************************************************************/#define DEMUX_INDEX_SIZE_MAX (100000)static void demux_IndexInit( demux_index_t *p_idx ){ p_idx->i_idx = 0; p_idx->i_idx_max = 0; p_idx->idx = NULL;}static void demux_IndexClean( demux_index_t *p_idx ){ if( p_idx->idx ) { free( p_idx->idx ); p_idx->idx = NULL; }}static void demux_IndexAppend( demux_index_t *p_idx, int64_t i_time, int64_t i_offset ){ /* Be sure to append new entry (we don't insert point) */ if( p_idx->i_idx > 0 && p_idx->idx[p_idx->i_idx-1].i_time >= i_time ) return; /* */ if( p_idx->i_idx >= p_idx->i_idx_max ) { if( p_idx->i_idx >= DEMUX_INDEX_SIZE_MAX ) { /* Avoid too big index */ const int64_t i_length = p_idx->idx[p_idx->i_idx-1].i_time - p_idx->idx[0].i_time; const int i_count = DEMUX_INDEX_SIZE_MAX/2; int i, j; /* We try to reduce the resolution of the index by a factor 2 */ for( i = 1, j = 1; i < p_idx->i_idx; i++ ) { if( p_idx->idx[i].i_time < j * i_length / i_count ) continue; p_idx->idx[j++] = p_idx->idx[i]; } p_idx->i_idx = j; if( p_idx->i_idx > 3 * DEMUX_INDEX_SIZE_MAX / 4 ) { /* We haven't created enough space * (This method won't create a good index but work for sure) */ for( i = 0; i < p_idx->i_idx/2; i++ ) p_idx->idx[i] = p_idx->idx[2*i]; p_idx->i_idx /= 2; } } else { p_idx->i_idx_max += 1000; p_idx->idx = realloc( p_idx->idx, p_idx->i_idx_max*sizeof(demux_index_entry_t)); } } /* */ p_idx->idx[p_idx->i_idx].i_time = i_time; p_idx->idx[p_idx->i_idx].i_offset = i_offset; p_idx->i_idx++;}static int64_t demux_IndexConvertTime( demux_index_t *p_idx, int64_t i_time ){ int i_min = 0; int i_max = p_idx->i_idx-1; /* Empty index */ if( p_idx->i_idx <= 0 ) return -1; /* Special border case */ if( i_time <= p_idx->idx[0].i_time ) return p_idx->idx[0].i_offset; if( i_time >= p_idx->idx[i_max].i_time ) return p_idx->idx[i_max].i_offset; /* Dicho */ for( ;; ) { int i_med; if( i_max - i_min <= 1 ) break; i_med = (i_min+i_max)/2; if( p_idx->idx[i_med].i_time < i_time ) i_min = i_med; else if( p_idx->idx[i_med].i_time > i_time ) i_max = i_med; else return p_idx->idx[i_med].i_offset; } /* return nearest in time */ if( i_time - p_idx->idx[i_min].i_time < p_idx->idx[i_max].i_time - i_time ) return p_idx->idx[i_min].i_offset; else return p_idx->idx[i_max].i_offset;}static int64_t demux_IndexFindOffset( demux_index_t *p_idx, int64_t i_offset ){ int i_min = 0; int i_max = p_idx->i_idx-1; /* Empty index */ if( p_idx->i_idx <= 0 ) return -1; /* Special border case */ if( i_offset <= p_idx->idx[0].i_offset ) return p_idx->idx[0].i_offset; if( i_offset == p_idx->idx[i_max].i_offset ) return p_idx->idx[i_max].i_offset; if( i_offset > p_idx->idx[i_max].i_offset ) return -1; /* Dicho */ for( ;; ) { int i_med; if( i_max - i_min <= 1 ) break; i_med = (i_min+i_max)/2; if( p_idx->idx[i_med].i_offset < i_offset ) i_min = i_med; else if( p_idx->idx[i_med].i_offset > i_offset ) i_max = i_med; else return p_idx->idx[i_med].i_offset; } /* return nearest */ if( i_offset - p_idx->idx[i_min].i_offset < p_idx->idx[i_max].i_offset - i_offset ) return p_idx->idx[i_min].i_offset; else return p_idx->idx[i_max].i_offset;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -