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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_demux.h>
36 * - complete support (add support for rtjpeg and raw)
37 * - better seek support (to key frame)
38 * - control GET_LENGTH (harder, unless we have an extended header+index)
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close ( vlc_object_t * );
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" );
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Demux ( demux_t * );
61 static int Control( demux_t *, int, va_list );
69 } demux_index_entry_t;
76 demux_index_entry_t *idx;
80 static void demux_IndexInit( demux_index_t * );
81 static void demux_IndexClean( demux_index_t * );
82 static void demux_IndexAppend( demux_index_t *,
83 int64_t i_time, int64_t i_offset );
84 /* Convert a time into offset */
85 static int64_t demux_IndexConvertTime( demux_index_t *, int64_t i_time );
86 /* Find the nearest offset in the index */
87 static int64_t demux_IndexFindOffset( demux_index_t *, int64_t i_offset );
93 char id[12]; /* "NuppelVideo\0" or "MythTVVideo\0" */
94 char version[5]; /* "x.xx\0" */
101 char i_mode; /* P progressive, I interlaced */
103 double d_aspect; /* 1.0 squared pixel */
106 int i_video_blocks; /* 0 no video, -1 unknown */
110 int i_keyframe_distance;
116 char i_type; /* A: audio, V: video, S: sync; T: test
117 R: Seekpoint (string:RTjjjjjjjj)
118 D: Extra data for codec */
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 */
149 /* FIXME Not sure of this one */
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;
161 int i_rtjpeg_quality;
162 int i_rtjpeg_luma_filter;
163 int i_rtjpeg_chroma_filter;
168 int64_t i_seekable_offset;
169 int64_t i_keyframe_adjust_offset;
176 extended_header_t exh;
179 es_out_id_t *p_es_video;
183 es_out_id_t *p_es_audio;
189 static int HeaderLoad( demux_t *, header_t *h );
190 static int FrameHeaderLoad( demux_t *, frame_header_t *h );
191 static int ExtendedHeaderLoad( demux_t *, extended_header_t *h );
193 /*****************************************************************************
194 * Open: initializes ES structures
195 *****************************************************************************/
196 static int Open( vlc_object_t * p_this )
198 demux_t *p_demux = (demux_t*)p_this;
200 const uint8_t *p_peek;
205 if( stream_Peek( p_demux->s, &p_peek, 12 ) != 12 ||
206 ( strncmp( (char *)p_peek, "MythTVVideo", 11 ) &&
207 strncmp( (char *)p_peek, "NuppelVideo", 11 ) ) )
210 p_sys = malloc( sizeof( demux_sys_t ) );
211 memset( p_sys, 0, sizeof( demux_sys_t ) );
212 p_sys->p_es_video = NULL;
213 p_sys->p_es_audio = NULL;
214 p_sys->p_extra_f = NULL;
216 demux_IndexInit( &p_sys->idx );
218 if( HeaderLoad( p_demux, &p_sys->hdr ) )
222 if( FrameHeaderLoad( p_demux, &fh ) || fh.i_type != 'D' )
224 if( fh.i_length > 0 )
226 if( fh.i_compression == 'F' )
228 /* ffmpeg extra data */
229 p_sys->i_extra_f = fh.i_length;
230 p_sys->p_extra_f = malloc( fh.i_length );
231 if( stream_Read( p_demux->s,
232 p_sys->p_extra_f, fh.i_length ) != fh.i_length )
237 /* TODO handle rtjpeg */
238 msg_Warn( p_demux, "unsupported 'D' frame (c=%c)", fh.i_compression );
239 if( stream_Read( p_demux->s, NULL, fh.i_length ) != fh.i_length )
244 /* Check and load extented */
245 if( stream_Peek( p_demux->s, &p_peek, 1 ) != 1 )
247 if( p_peek[0] == 'X' )
251 if( FrameHeaderLoad( p_demux, &fh ) )
253 if( fh.i_length != 512 )
256 if( ExtendedHeaderLoad( p_demux, &p_sys->exh ) )
264 /* XXX: for now only file with extended chunk are supported
265 * why: because else we need to have support for rtjpeg+stupid nuv shit */
266 msg_Err( p_demux, "incomplete NUV support (upload samples)" );
270 /* Create audio/video (will work only with extended header and audio=mp3 */
271 if( p_sys->hdr.i_video_blocks != 0 )
275 es_format_Init( &fmt, VIDEO_ES, p_sys->exh.i_video_fcc );
276 fmt.video.i_width = p_sys->hdr.i_width;
277 fmt.video.i_height = p_sys->hdr.i_height;
278 fmt.i_extra = p_sys->i_extra_f;
279 fmt.p_extra = p_sys->p_extra_f;
281 p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
283 if( p_sys->hdr.i_audio_blocks != 0 )
287 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('m','p','g','a') );
288 fmt.audio.i_rate = p_sys->exh.i_audio_sample_rate;
289 fmt.audio.i_bitspersample = p_sys->exh.i_audio_bits_per_sample;
291 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
293 if( p_sys->hdr.i_text_blocks != 0 )
295 msg_Warn( p_demux, "text not yet supported (upload samples)" );
299 /* Fill p_demux fields */
300 p_demux->pf_demux = Demux;
301 p_demux->pf_control = Control;
302 p_demux->p_sys = p_sys;
307 msg_Warn( p_demux, "cannot load Nuv file" );
312 /*****************************************************************************
313 * Close: frees unused data
314 *****************************************************************************/
315 static void Close( vlc_object_t * p_this )
317 demux_t *p_demux = (demux_t*)p_this;
318 demux_sys_t *p_sys = p_demux->p_sys;
320 free( p_sys->p_extra_f );
321 demux_IndexClean( &p_sys->idx );
325 /*****************************************************************************
326 * Demux: reads and demuxes data packets
327 *****************************************************************************
328 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
329 *****************************************************************************/
330 static int Demux( demux_t *p_demux )
332 demux_sys_t *p_sys = p_demux->p_sys;
338 if( !vlc_object_alive (p_demux) )
341 if( FrameHeaderLoad( p_demux, &fh ) )
344 if( fh.i_type == 'A' || fh.i_type == 'V' )
347 /* TODO add support for some block type */
349 if( fh.i_type != 'R' )
351 stream_Read( p_demux->s, NULL, fh.i_length );
356 p_data = stream_Block( p_demux->s, fh.i_length );
357 p_data->i_dts = (int64_t)fh.i_timecode * 1000;
358 p_data->i_pts = (fh.i_type == 'V') ? 0 : p_data->i_dts;
361 demux_IndexAppend( &p_sys->idx, p_data->i_dts, stream_Tell(p_demux->s) );
364 if( p_data->i_dts > p_sys->i_pcr )
366 p_sys->i_pcr = p_data->i_dts;
367 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
370 if( fh.i_type == 'A' && p_sys->p_es_audio )
372 if( fh.i_compression == '3' )
373 es_out_Send( p_demux->out, p_sys->p_es_audio, p_data );
376 msg_Dbg( p_demux, "unsupported compression %c for audio (upload samples)", fh.i_compression );
377 block_Release( p_data );
380 else if( fh.i_type == 'V' && p_sys->p_es_video )
382 if( fh.i_compression >= '4' )
383 es_out_Send( p_demux->out, p_sys->p_es_video, p_data );
386 msg_Dbg( p_demux, "unsupported compression %c for video (upload samples)", fh.i_compression );
387 block_Release( p_data );
392 block_Release( p_data );
398 /*****************************************************************************
400 *****************************************************************************/
401 static int Control( demux_t *p_demux, int i_query, va_list args )
403 demux_sys_t *p_sys = p_demux->p_sys;
411 case DEMUX_GET_POSITION:
412 pf = (double*)va_arg( args, double * );
413 i64 = stream_Size( p_demux->s );
415 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
420 case DEMUX_SET_POSITION:
424 f = (double)va_arg( args, double );
425 i_pos = stream_Size( p_demux->s ) * f;
427 i64 = demux_IndexFindOffset( &p_sys->idx, i_pos );
432 return stream_Seek( p_demux->s, i64 );
434 if( p_sys->idx.i_idx > 0 )
436 if( stream_Seek( p_demux->s, p_sys->idx.idx[p_sys->idx.i_idx-1].i_offset ) )
441 if( stream_Seek( p_demux->s, 0 ) )
445 while( vlc_object_alive (p_demux) )
450 if( ( i_tell = stream_Tell( p_demux->s ) ) >= i_pos )
453 if( FrameHeaderLoad( p_demux, &fh ) )
456 if( fh.i_type == 'A' || fh.i_type == 'V' )
457 demux_IndexAppend( &p_sys->idx,(int64_t)fh.i_timecode*1000, i_tell );
459 if( fh.i_type != 'R' )
460 stream_Read( p_demux->s, NULL, fh.i_length );
467 pi64 = (int64_t*)va_arg( args, int64_t * );
468 *pi64 = p_sys->i_pcr >= 0 ? p_sys->i_pcr : 0;
474 i64 = (int64_t)va_arg( args, int64_t );
476 i_pos = demux_IndexConvertTime( &p_sys->idx, i64 );
480 if( stream_Seek( p_demux->s, i_pos ) )
485 while( vlc_object_alive (p_demux) )
489 if( FrameHeaderLoad( p_demux, &fh ) )
492 if( fh.i_type == 'A' || fh.i_type == 'V' )
494 int64_t i_time = (int64_t)fh.i_timecode*1000;
495 int64_t i_tell = stream_Tell(p_demux->s)-12;
497 demux_IndexAppend( &p_sys->idx, i_time, i_tell );
500 return stream_Seek( p_demux->s, i_tell );
502 if( fh.i_type != 'R' )
503 stream_Read( p_demux->s, NULL, fh.i_length );
508 case DEMUX_GET_LENGTH:
509 pi64 = (int64_t*)va_arg( args, int64_t * );
513 pf = (double*)va_arg( args, double * );
514 *pf = p_sys->hdr.d_fps;
524 /*****************************************************************************
526 *****************************************************************************/
527 static inline void GetDoubleLE( double *pd, void *src )
529 /* FIXME works only if sizeof(double) == 8 */
530 #ifdef WORDS_BIGENDIAN
531 uint8_t *p = (uint8_t*)pd, *q = (uint8_t*)src;
533 for( i = 0; i < 8; i++ )
536 memcpy( pd, src, 8 );
542 static int HeaderLoad( demux_t *p_demux, header_t *h )
546 if( stream_Read( p_demux->s, buffer, 72 ) != 72 )
549 /* XXX: they are alignment to take care of (another broken format) */
550 memcpy( h->id, &buffer[ 0], 12 );
551 memcpy( h->version, &buffer[12], 5 );
552 h->i_width = GetDWLE( &buffer[20] );
553 h->i_height = GetDWLE( &buffer[24] );
554 h->i_width_desired = GetDWLE( &buffer[28] );
555 h->i_height_desired = GetDWLE( &buffer[32] );
556 h->i_mode = buffer[36];
557 GetDoubleLE( &h->d_aspect, &buffer[40] );
558 GetDoubleLE( &h->d_fps, &buffer[48] );
559 h->i_video_blocks = GetDWLE( &buffer[56] );
560 h->i_audio_blocks = GetDWLE( &buffer[60] );
561 h->i_text_blocks = GetDWLE( &buffer[64] );
562 h->i_keyframe_distance = GetDWLE( &buffer[68] );
564 msg_Dbg( p_demux, "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d",
565 h->id, h->version, h->i_width, h->i_height, h->d_aspect,
566 h->d_fps, h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks,
567 h->i_keyframe_distance );
574 static int FrameHeaderLoad( demux_t *p_demux, frame_header_t *h )
578 if( stream_Read( p_demux->s, buffer, 12 ) != 12 )
581 h->i_type = buffer[0];
582 h->i_compression = buffer[1];
583 h->i_keyframe = buffer[2];
584 h->i_filters = buffer[3];
586 h->i_timecode = GetDWLE( &buffer[4] );
587 h->i_length = GetDWLE( &buffer[8] );
589 msg_Dbg( p_demux, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
591 h->i_compression ? h->i_compression : ' ',
592 h->i_keyframe ? h->i_keyframe : ' ',
594 h->i_timecode, h->i_length );
599 static int ExtendedHeaderLoad( demux_t *p_demux, extended_header_t *h )
603 if( stream_Read( p_demux->s, buffer, 512 ) != 512 )
606 h->i_version = GetDWLE( &buffer[0] );
607 h->i_video_fcc = VLC_FOURCC( buffer[4], buffer[5], buffer[6], buffer[7] );
608 h->i_audio_fcc = VLC_FOURCC( buffer[8], buffer[9], buffer[10], buffer[11] );
609 h->i_audio_sample_rate = GetDWLE( &buffer[12] );
610 h->i_audio_bits_per_sample = GetDWLE( &buffer[16] );
611 h->i_audio_channels = GetDWLE( &buffer[20] );
612 h->i_audio_compression_ratio = GetDWLE( &buffer[24] );
613 h->i_audio_quality = GetDWLE( &buffer[28] );
614 h->i_rtjpeg_quality = GetDWLE( &buffer[32] );
615 h->i_rtjpeg_luma_filter = GetDWLE( &buffer[36] );
616 h->i_rtjpeg_chroma_filter = GetDWLE( &buffer[40] );
617 h->i_lavc_bitrate = GetDWLE( &buffer[44] );
618 h->i_lavc_qmin = GetDWLE( &buffer[48] );
619 h->i_lavc_qmin = GetDWLE( &buffer[52] );
620 h->i_lavc_maxqdiff = GetDWLE( &buffer[56] );
621 h->i_seekable_offset = GetQWLE( &buffer[60] );
622 h->i_keyframe_adjust_offset= GetQWLE( &buffer[68] );
624 msg_Dbg( p_demux, "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
625 "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld",
627 (char*)&h->i_video_fcc,
628 (char*)&h->i_audio_fcc, h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
629 h->i_audio_compression_ratio, h->i_audio_quality,
630 h->i_rtjpeg_quality, h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter,
631 h->i_lavc_bitrate, h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff,
632 h->i_seekable_offset, h->i_keyframe_adjust_offset );
637 /*****************************************************************************/
638 #define DEMUX_INDEX_SIZE_MAX (100000)
639 static void demux_IndexInit( demux_index_t *p_idx )
642 p_idx->i_idx_max = 0;
645 static void demux_IndexClean( demux_index_t *p_idx )
650 static void demux_IndexAppend( demux_index_t *p_idx,
651 int64_t i_time, int64_t i_offset )
653 /* Be sure to append new entry (we don't insert point) */
654 if( p_idx->i_idx > 0 && p_idx->idx[p_idx->i_idx-1].i_time >= i_time )
658 if( p_idx->i_idx >= p_idx->i_idx_max )
660 if( p_idx->i_idx >= DEMUX_INDEX_SIZE_MAX )
662 /* Avoid too big index */
663 const int64_t i_length = p_idx->idx[p_idx->i_idx-1].i_time -
664 p_idx->idx[0].i_time;
665 const int i_count = DEMUX_INDEX_SIZE_MAX/2;
668 /* We try to reduce the resolution of the index by a factor 2 */
669 for( i = 1, j = 1; i < p_idx->i_idx; i++ )
671 if( p_idx->idx[i].i_time < j * i_length / i_count )
674 p_idx->idx[j++] = p_idx->idx[i];
678 if( p_idx->i_idx > 3 * DEMUX_INDEX_SIZE_MAX / 4 )
680 /* We haven't created enough space
681 * (This method won't create a good index but work for sure) */
682 for( i = 0; i < p_idx->i_idx/2; i++ )
683 p_idx->idx[i] = p_idx->idx[2*i];
689 p_idx->i_idx_max += 1000;
690 p_idx->idx = realloc( p_idx->idx,
691 p_idx->i_idx_max*sizeof(demux_index_entry_t));
696 p_idx->idx[p_idx->i_idx].i_time = i_time;
697 p_idx->idx[p_idx->i_idx].i_offset = i_offset;
701 static int64_t demux_IndexConvertTime( demux_index_t *p_idx, int64_t i_time )
704 int i_max = p_idx->i_idx-1;
707 if( p_idx->i_idx <= 0 )
710 /* Special border case */
711 if( i_time <= p_idx->idx[0].i_time )
712 return p_idx->idx[0].i_offset;
713 if( i_time >= p_idx->idx[i_max].i_time )
714 return p_idx->idx[i_max].i_offset;
721 if( i_max - i_min <= 1 )
724 i_med = (i_min+i_max)/2;
725 if( p_idx->idx[i_med].i_time < i_time )
727 else if( p_idx->idx[i_med].i_time > i_time )
730 return p_idx->idx[i_med].i_offset;
733 /* return nearest in time */
734 if( i_time - p_idx->idx[i_min].i_time < p_idx->idx[i_max].i_time - i_time )
735 return p_idx->idx[i_min].i_offset;
737 return p_idx->idx[i_max].i_offset;
741 static int64_t demux_IndexFindOffset( demux_index_t *p_idx, int64_t i_offset )
744 int i_max = p_idx->i_idx-1;
747 if( p_idx->i_idx <= 0 )
750 /* Special border case */
751 if( i_offset <= p_idx->idx[0].i_offset )
752 return p_idx->idx[0].i_offset;
753 if( i_offset == p_idx->idx[i_max].i_offset )
754 return p_idx->idx[i_max].i_offset;
755 if( i_offset > p_idx->idx[i_max].i_offset )
763 if( i_max - i_min <= 1 )
766 i_med = (i_min+i_max)/2;
767 if( p_idx->idx[i_med].i_offset < i_offset )
769 else if( p_idx->idx[i_med].i_offset > i_offset )
772 return p_idx->idx[i_med].i_offset;
776 if( i_offset - p_idx->idx[i_min].i_offset < p_idx->idx[i_max].i_offset - i_offset )
777 return p_idx->idx[i_min].i_offset;
779 return p_idx->idx[i_max].i_offset;