1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
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 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_demux.h>
35 * - complete support (add support for rtjpeg and raw)
36 * - better seek support (to key frame)
37 * - control GET_LENGTH (harder, unless we have an extended header+index)
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close ( vlc_object_t * );
48 set_category( CAT_INPUT );
49 set_subcategory( SUBCAT_INPUT_DEMUX );
50 set_description( _("Nuv demuxer") );
51 set_capability( "demux2", 145 );
52 set_callbacks( Open, Close );
53 add_shortcut( "nuv" );
56 /*****************************************************************************
58 *****************************************************************************/
59 static int Demux ( demux_t * );
60 static int Control( demux_t *, int, va_list );
68 } demux_index_entry_t;
75 demux_index_entry_t *idx;
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 );
92 char id[12]; /* "NuppelVideo\0" or "MythTVVideo\0" */
93 char version[5]; /* "x.xx\0" */
100 char i_mode; /* P progressive, I interlaced */
102 double d_aspect; /* 1.0 squared pixel */
105 int i_video_blocks; /* 0 no video, -1 unknown */
109 int i_keyframe_distance;
115 char i_type; /* A: audio, V: video, S: sync; T: test
116 R: Seekpoint (string:RTjjjjjjjj)
117 D: Extra data for codec */
118 char i_compression; /* V: 0 uncompressed
123 A: 0 uncompressed (44100 1-bits, 2ch)
129 N null frame loudless
131 S: B audio and vdeo sync point
132 A audio sync info (timecode == effective
134 V next video sync (timecode == next video
136 S audio,video,text correlation */
137 char i_keyframe; /* 0 keyframe, else no no key frame */
138 uint8_t i_filters; /* 0x01: gauss 5 pixel (8,2,2,2,2)/16
139 0x02: gauss 5 pixel (8,1,1,1,1)/12
140 0x04: cartoon filter */
142 int i_timecode; /* ms */
144 int i_length; /* V,A,T: length of following data
145 S: length of packet correl */
148 /* FIXME Not sure of this one */
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_seekable_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 static int HeaderLoad( demux_t *, header_t *h );
189 static int FrameHeaderLoad( demux_t *, frame_header_t *h );
190 static int ExtendedHeaderLoad( demux_t *, extended_header_t *h );
192 /*****************************************************************************
193 * Open: initializes ES structures
194 *****************************************************************************/
195 static int Open( vlc_object_t * p_this )
197 demux_t *p_demux = (demux_t*)p_this;
199 const uint8_t *p_peek;
201 vlc_bool_t b_extended;
204 if( stream_Peek( p_demux->s, &p_peek, 12 ) != 12 ||
205 ( strncmp( (char *)p_peek, "MythTVVideo", 11 ) &&
206 strncmp( (char *)p_peek, "NuppelVideo", 11 ) ) )
209 p_sys = malloc( sizeof( demux_sys_t ) );
210 memset( p_sys, 0, sizeof( demux_sys_t ) );
211 p_sys->p_es_video = NULL;
212 p_sys->p_es_audio = NULL;
213 p_sys->p_extra_f = NULL;
215 demux_IndexInit( &p_sys->idx );
217 if( HeaderLoad( p_demux, &p_sys->hdr ) )
221 if( FrameHeaderLoad( p_demux, &fh ) || fh.i_type != 'D' )
223 if( fh.i_length > 0 )
225 if( fh.i_compression == 'F' )
227 /* ffmpeg extra data */
228 p_sys->i_extra_f = fh.i_length;
229 p_sys->p_extra_f = malloc( fh.i_length );
230 if( stream_Read( p_demux->s,
231 p_sys->p_extra_f, fh.i_length ) != fh.i_length )
236 /* TODO handle rtjpeg */
237 msg_Warn( p_demux, "unsupported 'D' frame (c=%c)", fh.i_compression );
238 if( stream_Read( p_demux->s, NULL, fh.i_length ) != fh.i_length )
243 /* Check and load extented */
244 if( stream_Peek( p_demux->s, &p_peek, 1 ) != 1 )
246 if( p_peek[0] == 'X' )
248 b_extended = VLC_TRUE;
250 if( FrameHeaderLoad( p_demux, &fh ) )
252 if( fh.i_length != 512 )
255 if( ExtendedHeaderLoad( p_demux, &p_sys->exh ) )
261 b_extended = VLC_FALSE;
263 /* XXX: for now only file with extended chunk are supported
264 * why: because else we need to have support for rtjpeg+stupid nuv shit */
265 msg_Err( p_demux, "incomplete NUV support (upload samples)" );
269 /* Create audio/video (will work only with extended header and audio=mp3 */
270 if( p_sys->hdr.i_video_blocks != 0 )
274 es_format_Init( &fmt, VIDEO_ES, p_sys->exh.i_video_fcc );
275 fmt.video.i_width = p_sys->hdr.i_width;
276 fmt.video.i_height = p_sys->hdr.i_height;
277 fmt.i_extra = p_sys->i_extra_f;
278 fmt.p_extra = p_sys->p_extra_f;
280 p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
282 if( p_sys->hdr.i_audio_blocks != 0 )
286 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('m','p','g','a') );
287 fmt.audio.i_rate = p_sys->exh.i_audio_sample_rate;
288 fmt.audio.i_bitspersample = p_sys->exh.i_audio_bits_per_sample;
290 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
292 if( p_sys->hdr.i_text_blocks != 0 )
294 msg_Warn( p_demux, "text not yet supported (upload samples)" );
298 /* Fill p_demux fields */
299 p_demux->pf_demux = Demux;
300 p_demux->pf_control = Control;
301 p_demux->p_sys = p_sys;
306 msg_Warn( p_demux, "cannot load Nuv file" );
311 /*****************************************************************************
312 * Close: frees unused data
313 *****************************************************************************/
314 static void Close( vlc_object_t * p_this )
316 demux_t *p_demux = (demux_t*)p_this;
317 demux_sys_t *p_sys = p_demux->p_sys;
319 free( p_sys->p_extra_f );
320 demux_IndexClean( &p_sys->idx );
324 /*****************************************************************************
325 * Demux: reads and demuxes data packets
326 *****************************************************************************
327 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
328 *****************************************************************************/
329 static int Demux( demux_t *p_demux )
331 demux_sys_t *p_sys = p_demux->p_sys;
340 if( FrameHeaderLoad( p_demux, &fh ) )
343 if( fh.i_type == 'A' || fh.i_type == 'V' )
346 /* TODO add support for some block type */
348 if( fh.i_type != 'R' )
350 stream_Read( p_demux->s, NULL, fh.i_length );
355 p_data = stream_Block( p_demux->s, fh.i_length );
356 p_data->i_dts = (int64_t)fh.i_timecode * 1000;
357 p_data->i_pts = (fh.i_type == 'V') ? 0 : p_data->i_dts;
360 demux_IndexAppend( &p_sys->idx, p_data->i_dts, stream_Tell(p_demux->s) );
363 if( p_data->i_dts > p_sys->i_pcr )
365 p_sys->i_pcr = p_data->i_dts;
366 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
369 if( fh.i_type == 'A' && p_sys->p_es_audio )
371 if( fh.i_compression == '3' )
372 es_out_Send( p_demux->out, p_sys->p_es_audio, p_data );
375 msg_Dbg( p_demux, "unsupported compression %c for audio (upload samples)", fh.i_compression );
376 block_Release( p_data );
379 else if( fh.i_type == 'V' && p_sys->p_es_video )
381 if( fh.i_compression >= '4' )
382 es_out_Send( p_demux->out, p_sys->p_es_video, p_data );
385 msg_Dbg( p_demux, "unsupported compression %c for video (upload samples)", fh.i_compression );
386 block_Release( p_data );
391 block_Release( p_data );
397 /*****************************************************************************
399 *****************************************************************************/
400 static int Control( demux_t *p_demux, int i_query, va_list args )
402 demux_sys_t *p_sys = p_demux->p_sys;
410 case DEMUX_GET_POSITION:
411 pf = (double*)va_arg( args, double * );
412 i64 = stream_Size( p_demux->s );
414 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
419 case DEMUX_SET_POSITION:
423 f = (double)va_arg( args, double );
424 i_pos = stream_Size( p_demux->s ) * f;
426 i64 = demux_IndexFindOffset( &p_sys->idx, i_pos );
431 return stream_Seek( p_demux->s, i64 );
433 if( p_sys->idx.i_idx > 0 )
435 if( stream_Seek( p_demux->s, p_sys->idx.idx[p_sys->idx.i_idx-1].i_offset ) )
440 if( stream_Seek( p_demux->s, 0 ) )
444 while( !p_demux->b_die )
449 if( ( i_tell = stream_Tell( p_demux->s ) ) >= i_pos )
452 if( FrameHeaderLoad( p_demux, &fh ) )
455 if( fh.i_type == 'A' || fh.i_type == 'V' )
456 demux_IndexAppend( &p_sys->idx,(int64_t)fh.i_timecode*1000, i_tell );
458 if( fh.i_type != 'R' )
459 stream_Read( p_demux->s, NULL, fh.i_length );
466 pi64 = (int64_t*)va_arg( args, int64_t * );
467 *pi64 = p_sys->i_pcr >= 0 ? p_sys->i_pcr : 0;
473 i64 = (int64_t)va_arg( args, int64_t );
475 i_pos = demux_IndexConvertTime( &p_sys->idx, i64 );
479 if( stream_Seek( p_demux->s, i_pos ) )
484 while( !p_demux->b_die )
488 if( FrameHeaderLoad( p_demux, &fh ) )
491 if( fh.i_type == 'A' || fh.i_type == 'V' )
493 int64_t i_time = (int64_t)fh.i_timecode*1000;
494 int64_t i_tell = stream_Tell(p_demux->s)-12;
496 demux_IndexAppend( &p_sys->idx, i_time, i_tell );
499 return stream_Seek( p_demux->s, i_tell );
501 if( fh.i_type != 'R' )
502 stream_Read( p_demux->s, NULL, fh.i_length );
507 case DEMUX_GET_LENGTH:
508 pi64 = (int64_t*)va_arg( args, int64_t * );
512 pf = (double*)va_arg( args, double * );
513 *pf = p_sys->hdr.d_fps;
523 /*****************************************************************************
525 *****************************************************************************/
526 static inline void GetDoubleLE( double *pd, void *src )
528 /* FIXME works only if sizeof(double) == 8 */
529 #ifdef WORDS_BIGENDIAN
530 uint8_t *p = (uint8_t*)pd, *q = (uint8_t*)src;
532 for( i = 0; i < 8; i++ )
535 memcpy( pd, src, 8 );
541 static int HeaderLoad( demux_t *p_demux, header_t *h )
545 if( stream_Read( p_demux->s, buffer, 72 ) != 72 )
548 /* XXX: they are alignment to take care of (another broken format) */
549 memcpy( h->id, &buffer[ 0], 12 );
550 memcpy( h->version, &buffer[12], 5 );
551 h->i_width = GetDWLE( &buffer[20] );
552 h->i_height = GetDWLE( &buffer[24] );
553 h->i_width_desired = GetDWLE( &buffer[28] );
554 h->i_height_desired = GetDWLE( &buffer[32] );
555 h->i_mode = buffer[36];
556 GetDoubleLE( &h->d_aspect, &buffer[40] );
557 GetDoubleLE( &h->d_fps, &buffer[48] );
558 h->i_video_blocks = GetDWLE( &buffer[56] );
559 h->i_audio_blocks = GetDWLE( &buffer[60] );
560 h->i_text_blocks = GetDWLE( &buffer[64] );
561 h->i_keyframe_distance = GetDWLE( &buffer[68] );
563 msg_Dbg( p_demux, "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d",
564 h->id, h->version, h->i_width, h->i_height, h->d_aspect,
565 h->d_fps, h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks,
566 h->i_keyframe_distance );
573 static int FrameHeaderLoad( demux_t *p_demux, frame_header_t *h )
577 if( stream_Read( p_demux->s, buffer, 12 ) != 12 )
580 h->i_type = buffer[0];
581 h->i_compression = buffer[1];
582 h->i_keyframe = buffer[2];
583 h->i_filters = buffer[3];
585 h->i_timecode = GetDWLE( &buffer[4] );
586 h->i_length = GetDWLE( &buffer[8] );
588 msg_Dbg( p_demux, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
590 h->i_compression ? h->i_compression : ' ',
591 h->i_keyframe ? h->i_keyframe : ' ',
593 h->i_timecode, h->i_length );
598 static int ExtendedHeaderLoad( demux_t *p_demux, extended_header_t *h )
602 if( stream_Read( p_demux->s, buffer, 512 ) != 512 )
605 h->i_version = GetDWLE( &buffer[0] );
606 h->i_video_fcc = VLC_FOURCC( buffer[4], buffer[5], buffer[6], buffer[7] );
607 h->i_audio_fcc = VLC_FOURCC( buffer[8], buffer[9], buffer[10], buffer[11] );
608 h->i_audio_sample_rate = GetDWLE( &buffer[12] );
609 h->i_audio_bits_per_sample = GetDWLE( &buffer[16] );
610 h->i_audio_channels = GetDWLE( &buffer[20] );
611 h->i_audio_compression_ratio = GetDWLE( &buffer[24] );
612 h->i_audio_quality = GetDWLE( &buffer[28] );
613 h->i_rtjpeg_quality = GetDWLE( &buffer[32] );
614 h->i_rtjpeg_luma_filter = GetDWLE( &buffer[36] );
615 h->i_rtjpeg_chroma_filter = GetDWLE( &buffer[40] );
616 h->i_lavc_bitrate = GetDWLE( &buffer[44] );
617 h->i_lavc_qmin = GetDWLE( &buffer[48] );
618 h->i_lavc_qmin = GetDWLE( &buffer[52] );
619 h->i_lavc_maxqdiff = GetDWLE( &buffer[56] );
620 h->i_seekable_offset = GetQWLE( &buffer[60] );
621 h->i_keyframe_adjust_offset= GetQWLE( &buffer[68] );
623 msg_Dbg( p_demux, "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
624 "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld",
626 (char*)&h->i_video_fcc,
627 (char*)&h->i_audio_fcc, h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
628 h->i_audio_compression_ratio, h->i_audio_quality,
629 h->i_rtjpeg_quality, h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter,
630 h->i_lavc_bitrate, h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff,
631 h->i_seekable_offset, h->i_keyframe_adjust_offset );
636 /*****************************************************************************/
637 #define DEMUX_INDEX_SIZE_MAX (100000)
638 static void demux_IndexInit( demux_index_t *p_idx )
641 p_idx->i_idx_max = 0;
644 static void demux_IndexClean( demux_index_t *p_idx )
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 )
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;
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 )
673 p_idx->idx[j++] = p_idx->idx[i];
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];
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));
695 p_idx->idx[p_idx->i_idx].i_time = i_time;
696 p_idx->idx[p_idx->i_idx].i_offset = i_offset;
700 static int64_t demux_IndexConvertTime( demux_index_t *p_idx, int64_t i_time )
703 int i_max = p_idx->i_idx-1;
706 if( p_idx->i_idx <= 0 )
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;
720 if( i_max - i_min <= 1 )
723 i_med = (i_min+i_max)/2;
724 if( p_idx->idx[i_med].i_time < i_time )
726 else if( p_idx->idx[i_med].i_time > i_time )
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;
736 return p_idx->idx[i_max].i_offset;
740 static int64_t demux_IndexFindOffset( demux_index_t *p_idx, int64_t i_offset )
743 int i_max = p_idx->i_idx-1;
746 if( p_idx->i_idx <= 0 )
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 )
762 if( i_max - i_min <= 1 )
765 i_med = (i_min+i_max)/2;
766 if( p_idx->idx[i_med].i_offset < i_offset )
768 else if( p_idx->idx[i_med].i_offset > i_offset )
771 return p_idx->idx[i_med].i_offset;
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;
778 return p_idx->idx[i_max].i_offset;