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 *****************************************************************************/
28 #include <vlc_demux.h>
31 * - complete support (add support for rtjpeg and raw)
32 * - better seek support (to key frame)
33 * - control GET_LENGTH (harder, unless we have an extended header+index)
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open ( vlc_object_t
* );
41 static void Close ( vlc_object_t
* );
44 set_category( CAT_INPUT
);
45 set_subcategory( SUBCAT_INPUT_DEMUX
);
46 set_description( _("Nuv demuxer") );
47 set_capability( "demux2", 145 );
48 set_callbacks( Open
, Close
);
49 add_shortcut( "nuv" );
52 /*****************************************************************************
54 *****************************************************************************/
55 static int Demux ( demux_t
* );
56 static int Control( demux_t
*, int, va_list );
64 } demux_index_entry_t
;
71 demux_index_entry_t
*idx
;
75 static void demux_IndexInit( demux_index_t
* );
76 static void demux_IndexClean( demux_index_t
* );
77 static void demux_IndexAppend( demux_index_t
*,
78 int64_t i_time
, int64_t i_offset
);
79 /* Convert a time into offset */
80 static int64_t demux_IndexConvertTime( demux_index_t
*, int64_t i_time
);
81 /* Find the nearest offset in the index */
82 static int64_t demux_IndexFindOffset( demux_index_t
*, int64_t i_offset
);
88 char id
[12]; /* "NuppelVideo\0" or "MythTVVideo\0" */
89 char version
[5]; /* "x.xx\0" */
96 char i_mode
; /* P progressive, I interlaced */
98 double d_aspect
; /* 1.0 squared pixel */
101 int i_video_blocks
; /* 0 no video, -1 unknown */
105 int i_keyframe_distance
;
111 char i_type
; /* A: audio, V: video, S: sync; T: test
112 R: Seekpoint (string:RTjjjjjjjj)
113 D: Extra data for codec */
114 char i_compression
; /* V: 0 uncompressed
119 A: 0 uncompressed (44100 1-bits, 2ch)
125 N null frame loudless
127 S: B audio and vdeo sync point
128 A audio sync info (timecode == effective
130 V next video sync (timecode == next video
132 S audio,video,text correlation */
133 char i_keyframe
; /* 0 keyframe, else no no key frame */
134 uint8_t i_filters
; /* 0x01: gauss 5 pixel (8,2,2,2,2)/16
135 0x02: gauss 5 pixel (8,1,1,1,1)/12
136 0x04: cartoon filter */
138 int i_timecode
; /* ms */
140 int i_length
; /* V,A,T: length of following data
141 S: length of packet correl */
144 /* FIXME Not sure of this one */
148 vlc_fourcc_t i_video_fcc
;
150 vlc_fourcc_t i_audio_fcc
;
151 int i_audio_sample_rate
;
152 int i_audio_bits_per_sample
;
153 int i_audio_channels
;
154 int i_audio_compression_ratio
;
156 int i_rtjpeg_quality
;
157 int i_rtjpeg_luma_filter
;
158 int i_rtjpeg_chroma_filter
;
163 int64_t i_seekable_offset
;
164 int64_t i_keyframe_adjust_offset
;
171 extended_header_t exh
;
174 es_out_id_t
*p_es_video
;
178 es_out_id_t
*p_es_audio
;
184 static int HeaderLoad( demux_t
*, header_t
*h
);
185 static int FrameHeaderLoad( demux_t
*, frame_header_t
*h
);
186 static int ExtendedHeaderLoad( demux_t
*, extended_header_t
*h
);
188 /*****************************************************************************
189 * Open: initializes ES structures
190 *****************************************************************************/
191 static int Open( vlc_object_t
* p_this
)
193 demux_t
*p_demux
= (demux_t
*)p_this
;
197 vlc_bool_t b_extended
;
200 if( stream_Peek( p_demux
->s
, &p_peek
, 12 ) != 12 ||
201 ( strncmp( (char *)p_peek
, "MythTVVideo", 11 ) &&
202 strncmp( (char *)p_peek
, "NuppelVideo", 11 ) ) )
205 p_sys
= malloc( sizeof( demux_sys_t
) );
206 memset( p_sys
, 0, sizeof( demux_sys_t
) );
207 p_sys
->p_es_video
= NULL
;
208 p_sys
->p_es_audio
= NULL
;
209 p_sys
->p_extra_f
= NULL
;
211 demux_IndexInit( &p_sys
->idx
);
213 if( HeaderLoad( p_demux
, &p_sys
->hdr
) )
217 if( FrameHeaderLoad( p_demux
, &fh
) || fh
.i_type
!= 'D' )
219 if( fh
.i_length
> 0 )
221 if( fh
.i_compression
== 'F' )
223 /* ffmpeg extra data */
224 p_sys
->i_extra_f
= fh
.i_length
;
225 p_sys
->p_extra_f
= malloc( fh
.i_length
);
226 if( stream_Read( p_demux
->s
,
227 p_sys
->p_extra_f
, fh
.i_length
) != fh
.i_length
)
232 /* TODO handle rtjpeg */
233 msg_Warn( p_demux
, "unsupported 'D' frame (c=%c)", fh
.i_compression
);
234 if( stream_Read( p_demux
->s
, NULL
, fh
.i_length
) != fh
.i_length
)
239 /* Check and load extented */
240 if( stream_Peek( p_demux
->s
, &p_peek
, 1 ) != 1 )
242 if( p_peek
[0] == 'X' )
244 b_extended
= VLC_TRUE
;
246 if( FrameHeaderLoad( p_demux
, &fh
) )
248 if( fh
.i_length
!= 512 )
251 if( ExtendedHeaderLoad( p_demux
, &p_sys
->exh
) )
257 b_extended
= VLC_FALSE
;
259 /* XXX: for now only file with extended chunk are supported
260 * why: because else we need to have support for rtjpeg+stupid nuv shit */
261 msg_Err( p_demux
, "incomplete NUV support (upload samples)" );
265 /* Create audio/video (will work only with extended header and audio=mp3 */
266 if( p_sys
->hdr
.i_video_blocks
!= 0 )
270 es_format_Init( &fmt
, VIDEO_ES
, p_sys
->exh
.i_video_fcc
);
271 fmt
.video
.i_width
= p_sys
->hdr
.i_width
;
272 fmt
.video
.i_height
= p_sys
->hdr
.i_height
;
273 fmt
.i_extra
= p_sys
->i_extra_f
;
274 fmt
.p_extra
= p_sys
->p_extra_f
;
276 p_sys
->p_es_video
= es_out_Add( p_demux
->out
, &fmt
);
278 if( p_sys
->hdr
.i_audio_blocks
!= 0 )
282 es_format_Init( &fmt
, AUDIO_ES
, VLC_FOURCC('m','p','g','a') );
283 fmt
.audio
.i_rate
= p_sys
->exh
.i_audio_sample_rate
;
284 fmt
.audio
.i_bitspersample
= p_sys
->exh
.i_audio_bits_per_sample
;
286 p_sys
->p_es_audio
= es_out_Add( p_demux
->out
, &fmt
);
288 if( p_sys
->hdr
.i_text_blocks
!= 0 )
290 msg_Warn( p_demux
, "text not yet supported (upload samples)" );
294 /* Fill p_demux fields */
295 p_demux
->pf_demux
= Demux
;
296 p_demux
->pf_control
= Control
;
297 p_demux
->p_sys
= p_sys
;
302 msg_Warn( p_demux
, "cannot load Nuv file" );
307 /*****************************************************************************
308 * Close: frees unused data
309 *****************************************************************************/
310 static void Close( vlc_object_t
* p_this
)
312 demux_t
*p_demux
= (demux_t
*)p_this
;
313 demux_sys_t
*p_sys
= p_demux
->p_sys
;
315 if( p_sys
->p_extra_f
)
316 free( p_sys
->p_extra_f
);
317 demux_IndexClean( &p_sys
->idx
);
321 /*****************************************************************************
322 * Demux: reads and demuxes data packets
323 *****************************************************************************
324 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
325 *****************************************************************************/
326 static int Demux( demux_t
*p_demux
)
328 demux_sys_t
*p_sys
= p_demux
->p_sys
;
337 if( FrameHeaderLoad( p_demux
, &fh
) )
340 if( fh
.i_type
== 'A' || fh
.i_type
== 'V' )
343 /* TODO add support for some block type */
345 if( fh
.i_type
!= 'R' )
347 stream_Read( p_demux
->s
, NULL
, fh
.i_length
);
352 p_data
= stream_Block( p_demux
->s
, fh
.i_length
);
353 p_data
->i_dts
= (int64_t)fh
.i_timecode
* 1000;
354 p_data
->i_pts
= (fh
.i_type
== 'V') ? 0 : p_data
->i_dts
;
357 demux_IndexAppend( &p_sys
->idx
, p_data
->i_dts
, stream_Tell(p_demux
->s
) );
360 if( p_data
->i_dts
> p_sys
->i_pcr
)
362 p_sys
->i_pcr
= p_data
->i_dts
;
363 es_out_Control( p_demux
->out
, ES_OUT_SET_PCR
, p_sys
->i_pcr
);
366 if( fh
.i_type
== 'A' && p_sys
->p_es_audio
)
368 if( fh
.i_compression
== '3' )
369 es_out_Send( p_demux
->out
, p_sys
->p_es_audio
, p_data
);
372 msg_Dbg( p_demux
, "unsupported compression %c for audio (upload samples)", fh
.i_compression
);
373 block_Release( p_data
);
376 else if( fh
.i_type
== 'V' && p_sys
->p_es_video
)
378 if( fh
.i_compression
>= '4' )
379 es_out_Send( p_demux
->out
, p_sys
->p_es_video
, p_data
);
382 msg_Dbg( p_demux
, "unsupported compression %c for video (upload samples)", fh
.i_compression
);
383 block_Release( p_data
);
388 block_Release( p_data
);
394 /*****************************************************************************
396 *****************************************************************************/
397 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
399 demux_sys_t
*p_sys
= p_demux
->p_sys
;
407 case DEMUX_GET_POSITION
:
408 pf
= (double*)va_arg( args
, double * );
409 i64
= stream_Size( p_demux
->s
);
411 *pf
= (double)stream_Tell( p_demux
->s
) / (double)i64
;
416 case DEMUX_SET_POSITION
:
420 f
= (double)va_arg( args
, double );
421 i_pos
= stream_Size( p_demux
->s
) * f
;
423 i64
= demux_IndexFindOffset( &p_sys
->idx
, i_pos
);
428 return stream_Seek( p_demux
->s
, i64
);
430 if( p_sys
->idx
.i_idx
> 0 )
432 if( stream_Seek( p_demux
->s
, p_sys
->idx
.idx
[p_sys
->idx
.i_idx
-1].i_offset
) )
437 if( stream_Seek( p_demux
->s
, 0 ) )
441 while( !p_demux
->b_die
)
446 if( ( i_tell
= stream_Tell( p_demux
->s
) ) >= i_pos
)
449 if( FrameHeaderLoad( p_demux
, &fh
) )
452 if( fh
.i_type
== 'A' || fh
.i_type
== 'V' )
453 demux_IndexAppend( &p_sys
->idx
,(int64_t)fh
.i_timecode
*1000, i_tell
);
455 if( fh
.i_type
!= 'R' )
456 stream_Read( p_demux
->s
, NULL
, fh
.i_length
);
463 pi64
= (int64_t*)va_arg( args
, int64_t * );
464 *pi64
= p_sys
->i_pcr
>= 0 ? p_sys
->i_pcr
: 0;
470 i64
= (int64_t)va_arg( args
, int64_t );
472 i_pos
= demux_IndexConvertTime( &p_sys
->idx
, i64
);
476 if( stream_Seek( p_demux
->s
, i_pos
) )
481 while( !p_demux
->b_die
)
485 if( FrameHeaderLoad( p_demux
, &fh
) )
488 if( fh
.i_type
== 'A' || fh
.i_type
== 'V' )
490 int64_t i_time
= (int64_t)fh
.i_timecode
*1000;
491 int64_t i_tell
= stream_Tell(p_demux
->s
)-12;
493 demux_IndexAppend( &p_sys
->idx
, i_time
, i_tell
);
496 return stream_Seek( p_demux
->s
, i_tell
);
498 if( fh
.i_type
!= 'R' )
499 stream_Read( p_demux
->s
, NULL
, fh
.i_length
);
504 case DEMUX_GET_LENGTH
:
505 pi64
= (int64_t*)va_arg( args
, int64_t * );
509 pf
= (double*)va_arg( args
, double * );
510 *pf
= p_sys
->hdr
.d_fps
;
520 /*****************************************************************************
522 *****************************************************************************/
523 static inline void GetDoubleLE( double *pd
, void *src
)
525 /* FIXME works only if sizeof(double) == 8 */
526 #ifdef WORDS_BIGENDIAN
527 uint8_t *p
= (uint8_t*)pd
, *q
= (uint8_t*)src
;
529 for( i
= 0; i
< 8; i
++ )
532 memcpy( pd
, src
, 8 );
538 static int HeaderLoad( demux_t
*p_demux
, header_t
*h
)
542 if( stream_Read( p_demux
->s
, buffer
, 72 ) != 72 )
545 /* XXX: they are alignment to take care of (another broken format) */
546 memcpy( h
->id
, &buffer
[ 0], 12 );
547 memcpy( h
->version
, &buffer
[12], 5 );
548 h
->i_width
= GetDWLE( &buffer
[20] );
549 h
->i_height
= GetDWLE( &buffer
[24] );
550 h
->i_width_desired
= GetDWLE( &buffer
[28] );
551 h
->i_height_desired
= GetDWLE( &buffer
[32] );
552 h
->i_mode
= buffer
[36];
553 GetDoubleLE( &h
->d_aspect
, &buffer
[40] );
554 GetDoubleLE( &h
->d_fps
, &buffer
[48] );
555 h
->i_video_blocks
= GetDWLE( &buffer
[56] );
556 h
->i_audio_blocks
= GetDWLE( &buffer
[60] );
557 h
->i_text_blocks
= GetDWLE( &buffer
[64] );
558 h
->i_keyframe_distance
= GetDWLE( &buffer
[68] );
560 msg_Dbg( p_demux
, "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d",
561 h
->id
, h
->version
, h
->i_width
, h
->i_height
, h
->d_aspect
,
562 h
->d_fps
, h
->i_video_blocks
, h
->i_audio_blocks
, h
->i_text_blocks
,
563 h
->i_keyframe_distance
);
570 static int FrameHeaderLoad( demux_t
*p_demux
, frame_header_t
*h
)
574 if( stream_Read( p_demux
->s
, buffer
, 12 ) != 12 )
577 h
->i_type
= buffer
[0];
578 h
->i_compression
= buffer
[1];
579 h
->i_keyframe
= buffer
[2];
580 h
->i_filters
= buffer
[3];
582 h
->i_timecode
= GetDWLE( &buffer
[4] );
583 h
->i_length
= GetDWLE( &buffer
[8] );
585 msg_Dbg( p_demux
, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
587 h
->i_compression
? h
->i_compression
: ' ',
588 h
->i_keyframe
? h
->i_keyframe
: ' ',
590 h
->i_timecode
, h
->i_length
);
595 static int ExtendedHeaderLoad( demux_t
*p_demux
, extended_header_t
*h
)
599 if( stream_Read( p_demux
->s
, buffer
, 512 ) != 512 )
602 h
->i_version
= GetDWLE( &buffer
[0] );
603 h
->i_video_fcc
= VLC_FOURCC( buffer
[4], buffer
[5], buffer
[6], buffer
[7] );
604 h
->i_audio_fcc
= VLC_FOURCC( buffer
[8], buffer
[9], buffer
[10], buffer
[11] );
605 h
->i_audio_sample_rate
= GetDWLE( &buffer
[12] );
606 h
->i_audio_bits_per_sample
= GetDWLE( &buffer
[16] );
607 h
->i_audio_channels
= GetDWLE( &buffer
[20] );
608 h
->i_audio_compression_ratio
= GetDWLE( &buffer
[24] );
609 h
->i_audio_quality
= GetDWLE( &buffer
[28] );
610 h
->i_rtjpeg_quality
= GetDWLE( &buffer
[32] );
611 h
->i_rtjpeg_luma_filter
= GetDWLE( &buffer
[36] );
612 h
->i_rtjpeg_chroma_filter
= GetDWLE( &buffer
[40] );
613 h
->i_lavc_bitrate
= GetDWLE( &buffer
[44] );
614 h
->i_lavc_qmin
= GetDWLE( &buffer
[48] );
615 h
->i_lavc_qmin
= GetDWLE( &buffer
[52] );
616 h
->i_lavc_maxqdiff
= GetDWLE( &buffer
[56] );
617 h
->i_seekable_offset
= GetQWLE( &buffer
[60] );
618 h
->i_keyframe_adjust_offset
= GetQWLE( &buffer
[68] );
620 msg_Dbg( p_demux
, "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
621 "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld",
623 (char*)&h
->i_video_fcc
,
624 (char*)&h
->i_audio_fcc
, h
->i_audio_sample_rate
, h
->i_audio_bits_per_sample
, h
->i_audio_channels
,
625 h
->i_audio_compression_ratio
, h
->i_audio_quality
,
626 h
->i_rtjpeg_quality
, h
->i_rtjpeg_luma_filter
, h
->i_rtjpeg_chroma_filter
,
627 h
->i_lavc_bitrate
, h
->i_lavc_qmin
, h
->i_lavc_qmax
, h
->i_lavc_maxqdiff
,
628 h
->i_seekable_offset
, h
->i_keyframe_adjust_offset
);
633 /*****************************************************************************/
634 #define DEMUX_INDEX_SIZE_MAX (100000)
635 static void demux_IndexInit( demux_index_t
*p_idx
)
638 p_idx
->i_idx_max
= 0;
641 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
;