1 /*****************************************************************************
2 * matroska_segment_parse.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2010 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Steve Lhomme <steve.lhomme@free.fr>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #include "matroska_segment.hpp"
26 #include "chapters.hpp"
28 #include "Ebml_parser.hpp"
32 #include "../vobsub.h"
35 #include <vlc_codecs.h>
37 /* GetFourCC helper */
38 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
39 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
41 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
44 static inline void fill_extra_data( mkv_track_t *p_tk, unsigned int offset )
46 if(p_tk->i_extra_data <= offset) return;
47 p_tk->fmt.i_extra = p_tk->i_extra_data - offset;
48 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
49 if(!p_tk->fmt.p_extra) { p_tk->fmt.i_extra = 0; return; };
50 memcpy( p_tk->fmt.p_extra, p_tk->p_extra_data + offset, p_tk->fmt.i_extra );
53 /*****************************************************************************
54 * Some functions to manipulate memory
55 *****************************************************************************/
56 static inline char * ToUTF8( const UTFstring &u )
58 return strdup( u.GetUTF8().c_str() );
61 /*****************************************************************************
63 *****************************************************************************/
64 void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
72 stream_Control( sys.demuxer.s, STREAM_CAN_SEEK, &b_seekable );
76 ep = new EbmlParser( &es, seekhead, &sys.demuxer );
78 while( ( l = ep->Get() ) != NULL )
80 if( MKV_IS_ID( l, KaxSeek ) )
82 EbmlId id = EBML_ID(EbmlVoid);
86 msg_Dbg( &sys.demuxer, "| | + Seek" );
91 while( ( l = ep->Get() ) != NULL )
93 if( unlikely( l->GetSize() >= SIZE_MAX ) )
95 msg_Err( &sys.demuxer,"%s too big... skipping it", typeid(*l).name() );
98 if( MKV_IS_ID( l, KaxSeekID ) )
100 KaxSeekID &sid = *(KaxSeekID*)l;
101 sid.ReadData( es.I_O() );
102 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
104 else if( MKV_IS_ID( l, KaxSeekPosition ) )
106 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
107 spos.ReadData( es.I_O() );
108 i_pos = (int64_t)segment->GetGlobalPosition( uint64( spos ) );
112 /* Many mkvmerge files hit this case. It seems to be a broken SeekHead */
113 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
119 msg_Err( &sys.demuxer,"Error while reading %s", typeid(*l).name() );
125 if( id == EBML_ID(KaxCues) )
127 msg_Dbg( &sys.demuxer, "| - cues at %"PRId64, i_pos );
128 LoadSeekHeadItem( EBML_INFO(KaxCues), i_pos );
130 else if( id == EBML_ID(KaxInfo) )
132 msg_Dbg( &sys.demuxer, "| - info at %"PRId64, i_pos );
133 LoadSeekHeadItem( EBML_INFO(KaxInfo), i_pos );
135 else if( id == EBML_ID(KaxChapters) )
137 msg_Dbg( &sys.demuxer, "| - chapters at %"PRId64, i_pos );
138 LoadSeekHeadItem( EBML_INFO(KaxChapters), i_pos );
140 else if( id == EBML_ID(KaxTags) )
142 msg_Dbg( &sys.demuxer, "| - tags at %"PRId64, i_pos );
143 LoadSeekHeadItem( EBML_INFO(KaxTags), i_pos );
145 else if( id == EBML_ID(KaxSeekHead) )
147 msg_Dbg( &sys.demuxer, "| - chained seekhead at %"PRId64, i_pos );
148 LoadSeekHeadItem( EBML_INFO(KaxSeekHead), i_pos );
150 else if( id == EBML_ID(KaxTracks) )
152 msg_Dbg( &sys.demuxer, "| - tracks at %"PRId64, i_pos );
153 LoadSeekHeadItem( EBML_INFO(KaxTracks), i_pos );
155 else if( id == EBML_ID(KaxAttachments) )
157 msg_Dbg( &sys.demuxer, "| - attachments at %"PRId64, i_pos );
158 LoadSeekHeadItem( EBML_INFO(KaxAttachments), i_pos );
162 msg_Dbg( &sys.demuxer, "| - unknown seekhead reference at %"PRId64, i_pos );
167 msg_Dbg( &sys.demuxer, "| | + ParseSeekHead Unknown (%s)", typeid(*l).name() );
174 * Helper function to print the mkv parse tree
176 static void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ... )
181 msg_Err( &demuxer, "MKV tree is too deep" );
184 va_start( args, psz_format );
185 static const char psz_foo[] = "| | | | | | | | | |";
186 char *psz_foo2 = (char*)malloc( i_level * 4 + 3 + strlen( psz_format ) );
187 strncpy( psz_foo2, psz_foo, 4 * i_level );
188 psz_foo2[ 4 * i_level ] = '+';
189 psz_foo2[ 4 * i_level + 1 ] = ' ';
190 strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
191 msg_GenericVa( &demuxer,VLC_MSG_DBG, "mkv", psz_foo2, args );
197 /*****************************************************************************
199 *****************************************************************************/
200 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
202 bool bSupported = true;
205 mkv_track_t *tk = new mkv_track_t();
206 memset( tk, 0, sizeof( mkv_track_t ) );
208 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
210 tk->fmt.psz_language = strdup("English");
211 tk->fmt.psz_description = NULL;
213 tk->b_default = true;
214 tk->b_enabled = true;
215 tk->b_forced = false;
216 tk->b_silent = false;
217 tk->i_number = tracks.size() - 1;
218 tk->i_extra_data = 0;
219 tk->p_extra_data = NULL;
220 tk->psz_codec = NULL;
221 tk->b_dts_only = false;
222 tk->i_default_duration = 0;
223 tk->f_timecodescale = 1.0;
225 tk->b_inited = false;
227 tk->p_data_init = NULL;
229 tk->psz_codec_name = NULL;
230 tk->psz_codec_settings = NULL;
231 tk->psz_codec_info_url = NULL;
232 tk->psz_codec_download_url = NULL;
234 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
235 tk->i_encoding_scope = MATROSKA_ENCODING_SCOPE_ALL_FRAMES;
236 tk->p_compression_data = NULL;
238 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
240 for( size_t i = 0; i < m->ListSize(); i++ )
242 EbmlElement *l = (*m)[i];
244 if( MKV_IS_ID( l, KaxTrackNumber ) )
246 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
248 tk->i_number = uint32( tnum );
249 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
251 else if( MKV_IS_ID( l, KaxTrackUID ) )
253 KaxTrackUID &tuid = *(KaxTrackUID*)l;
255 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
257 else if( MKV_IS_ID( l, KaxTrackType ) )
259 const char *psz_type;
260 KaxTrackType &ttype = *(KaxTrackType*)l;
262 switch( uint8(ttype) )
266 tk->fmt.i_cat = AUDIO_ES;
267 tk->fmt.audio.i_channels = 1;
268 tk->fmt.audio.i_rate = 8000;
272 tk->fmt.i_cat = VIDEO_ES;
275 psz_type = "subtitle";
276 tk->fmt.i_cat = SPU_ES;
279 psz_type = "buttons";
280 tk->fmt.i_cat = SPU_ES;
283 psz_type = "unknown";
284 tk->fmt.i_cat = UNKNOWN_ES;
288 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
290 else if( MKV_IS_ID( l, KaxTrackFlagEnabled ) ) // UNUSED
292 KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
294 tk->b_enabled = uint32( fenb );
295 msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u", uint32( fenb ) );
297 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
299 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
301 tk->b_default = uint32( fdef );
302 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
304 else if( MKV_IS_ID( l, KaxTrackFlagForced ) ) // UNUSED
306 KaxTrackFlagForced &ffor = *(KaxTrackFlagForced*)l;
307 tk->b_forced = uint32( ffor );
309 msg_Dbg( &sys.demuxer, "| | | + Track Forced=%u", uint32( ffor ) );
311 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) ) // UNUSED
313 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
315 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
317 else if( MKV_IS_ID( l, KaxTrackMinCache ) ) // UNUSED
319 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
321 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
323 else if( MKV_IS_ID( l, KaxTrackMaxCache ) ) // UNUSED
325 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
327 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
329 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
331 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
333 tk->i_default_duration = uint64(defd);
334 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration=%"PRId64, uint64(defd) );
336 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
338 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
340 tk->f_timecodescale = float( ttcs );
341 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
343 else if( MKV_IS_ID( l, KaxMaxBlockAdditionID ) ) // UNUSED
345 KaxMaxBlockAdditionID &mbl = *(KaxMaxBlockAdditionID*)l;
347 msg_Dbg( &sys.demuxer, "| | | + Track Max BlockAdditionID=%d", uint32( mbl ) );
349 else if( MKV_IS_ID( l, KaxTrackName ) )
351 KaxTrackName &tname = *(KaxTrackName*)l;
353 tk->fmt.psz_description = ToUTF8( UTFstring( tname ) );
354 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
356 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
358 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
360 free( tk->fmt.psz_language );
361 tk->fmt.psz_language = strdup( string( lang ).c_str() );
362 msg_Dbg( &sys.demuxer,
363 "| | | + Track Language=`%s'", tk->fmt.psz_language );
365 else if( MKV_IS_ID( l, KaxCodecID ) )
367 KaxCodecID &codecid = *(KaxCodecID*)l;
369 tk->psz_codec = strdup( string( codecid ).c_str() );
370 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
372 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
374 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
376 tk->i_extra_data = cpriv.GetSize();
377 if( tk->i_extra_data > 0 )
379 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
380 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
382 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size=%"PRId64, cpriv.GetSize() );
384 else if( MKV_IS_ID( l, KaxCodecName ) )
386 KaxCodecName &cname = *(KaxCodecName*)l;
388 tk->psz_codec_name = ToUTF8( UTFstring( cname ) );
389 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
392 else if( MKV_IS_ID( l, KaxCodecDecodeAll ) ) // UNUSED
394 KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
396 msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u", uint8( cdall ) );
398 else if( MKV_IS_ID( l, KaxTrackOverlay ) ) // UNUSED
400 KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
402 msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u", uint32( tovr ) );
404 else if( MKV_IS_ID( l, KaxContentEncodings ) )
406 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
407 MkvTree( sys.demuxer, 3, "Content Encodings" );
408 if ( cencs->ListSize() > 1 )
410 msg_Err( &sys.demuxer, "Multiple Compression method not supported" );
413 for( size_t j = 0; j < cencs->ListSize(); j++ )
415 EbmlElement *l2 = (*cencs)[j];
416 if( MKV_IS_ID( l2, KaxContentEncoding ) )
418 MkvTree( sys.demuxer, 4, "Content Encoding" );
419 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
420 for( size_t k = 0; k < cenc->ListSize(); k++ )
422 EbmlElement *l3 = (*cenc)[k];
423 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
425 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
426 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
428 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
430 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
431 tk->i_encoding_scope = uint32( encscope );
432 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
434 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
436 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
437 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
439 else if( MKV_IS_ID( l3, KaxContentCompression ) )
441 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
442 MkvTree( sys.demuxer, 5, "Content Compression" );
443 //Default compression type is 0 (Zlib)
444 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
445 for( size_t n = 0; n < compr->ListSize(); n++ )
447 EbmlElement *l4 = (*compr)[n];
448 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
450 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
451 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
452 tk->i_compression_type = uint32( compalg );
453 if ( ( tk->i_compression_type != MATROSKA_COMPRESSION_ZLIB ) &&
454 ( tk->i_compression_type != MATROSKA_COMPRESSION_HEADER ) )
456 msg_Err( &sys.demuxer, "Track Compression method %d not supported", tk->i_compression_type );
460 else if( MKV_IS_ID( l4, KaxContentCompSettings ) )
462 tk->p_compression_data = new KaxContentCompSettings( *(KaxContentCompSettings*)l4 );
466 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
470 // ContentEncryption Unsupported
473 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
479 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
483 // else if( MKV_IS_ID( l, KaxCodecSettings) ) DEPRECATED by matroska
485 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
487 // tk->psz_codec_settings = ToUTF8( UTFstring( cset ) );
488 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
490 // else if( MKV_IS_ID( l, KaxCodecInfoURL) ) DEPRECATED by matroska
492 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
494 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
495 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
497 // else if( MKV_IS_ID( l, KaxCodecDownloadURL) ) DEPRECATED by matroska
499 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
501 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
502 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
504 else if( MKV_IS_ID( l, KaxTrackVideo ) )
506 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
507 unsigned int i_crop_right = 0, i_crop_left = 0, i_crop_top = 0, i_crop_bottom = 0;
508 unsigned int i_display_unit = 0, i_display_width = 0, i_display_height = 0;
510 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
513 tk->fmt.video.i_frame_rate_base = (unsigned int)(tk->i_default_duration / 1000);
514 tk->fmt.video.i_frame_rate = 1000000;
516 for( unsigned int j = 0; j < tkv->ListSize(); j++ )
518 EbmlElement *l = (*tkv)[j];
519 if( MKV_IS_ID( l, KaxVideoFlagInterlaced ) ) // UNUSED
521 KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)l;
523 msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
525 else if( MKV_IS_ID( l, KaxVideoStereoMode ) ) // UNUSED
527 KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)l;
529 msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
531 else if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
533 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
535 tk->fmt.video.i_width += uint16( vwidth );
536 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
538 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
540 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
542 tk->fmt.video.i_height += uint16( vheight );
543 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
545 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
547 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
549 i_display_width = uint16( vwidth );
550 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
552 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
554 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
556 i_display_height = uint16( vheight );
557 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
559 else if( MKV_IS_ID( l, KaxVideoPixelCropBottom ) )
561 KaxVideoPixelCropBottom &cropval = *(KaxVideoPixelCropBottom*)l;
563 i_crop_bottom = uint16( cropval );
564 msg_Dbg( &sys.demuxer, "| | | | + crop pixel bottom=%d", uint16( cropval ) );
566 else if( MKV_IS_ID( l, KaxVideoPixelCropTop ) )
568 KaxVideoPixelCropTop &cropval = *(KaxVideoPixelCropTop*)l;
570 i_crop_top = uint16( cropval );
571 msg_Dbg( &sys.demuxer, "| | | | + crop pixel top=%d", uint16( cropval ) );
573 else if( MKV_IS_ID( l, KaxVideoPixelCropRight ) )
575 KaxVideoPixelCropRight &cropval = *(KaxVideoPixelCropRight*)l;
577 i_crop_right = uint16( cropval );
578 msg_Dbg( &sys.demuxer, "| | | | + crop pixel right=%d", uint16( cropval ) );
580 else if( MKV_IS_ID( l, KaxVideoPixelCropLeft ) )
582 KaxVideoPixelCropLeft &cropval = *(KaxVideoPixelCropLeft*)l;
584 i_crop_left = uint16( cropval );
585 msg_Dbg( &sys.demuxer, "| | | | + crop pixel left=%d", uint16( cropval ) );
587 else if( MKV_IS_ID( l, KaxVideoDisplayUnit ) )
589 KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
591 i_display_unit = uint8( vdmode );
592 msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
593 i_display_unit == 0 ? "pixels" : ( i_display_unit == 1 ? "centimeters": "inches" ) );
595 else if( MKV_IS_ID( l, KaxVideoAspectRatio ) ) // UNUSED
597 KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
599 msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
601 // ColourSpace UNUSED
602 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
604 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
606 tk->f_fps = float( vfps );
607 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
609 // else if( MKV_IS_ID( l, KaxVideoGamma) ) //DEPRECATED by Matroska
611 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
613 // msg_Dbg( &sys.demuxer, " | | | + gamma=%f", float( gamma ) );
617 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
620 if( i_display_height && i_display_width )
622 tk->fmt.video.i_sar_num = i_display_width * tk->fmt.video.i_height;
623 tk->fmt.video.i_sar_den = i_display_height * tk->fmt.video.i_width;
625 if( i_crop_left || i_crop_right || i_crop_top || i_crop_bottom )
627 tk->fmt.video.i_visible_width = tk->fmt.video.i_width;
628 tk->fmt.video.i_visible_height = tk->fmt.video.i_height;
629 tk->fmt.video.i_x_offset = i_crop_left;
630 tk->fmt.video.i_y_offset = i_crop_top;
631 tk->fmt.video.i_visible_width -= i_crop_left + i_crop_right;
632 tk->fmt.video.i_visible_height -= i_crop_top + i_crop_bottom;
634 /* FIXME: i_display_* allows you to not only set DAR, but also a zoom factor.
635 we do not support this atm */
637 else if( MKV_IS_ID( l, KaxTrackAudio ) )
639 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
641 /* Initialize default values */
642 tk->fmt.audio.i_channels = 1;
643 tk->fmt.audio.i_rate = 8000;
645 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
647 for( unsigned int j = 0; j < tka->ListSize(); j++ )
649 EbmlElement *l = (*tka)[j];
651 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
653 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
655 tk->i_original_rate = tk->fmt.audio.i_rate = (int)float( afreq );
656 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
658 else if( MKV_IS_ID( l, KaxAudioOutputSamplingFreq ) )
660 KaxAudioOutputSamplingFreq &afreq = *(KaxAudioOutputSamplingFreq*)l;
662 tk->fmt.audio.i_rate = (int)float( afreq );
663 msg_Dbg( &sys.demuxer, "| | | | + aoutfreq=%d", tk->fmt.audio.i_rate );
665 else if( MKV_IS_ID( l, KaxAudioChannels ) )
667 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
669 tk->fmt.audio.i_channels = uint8( achan );
670 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
672 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
674 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
676 tk->fmt.audio.i_bitspersample = uint8( abits );
677 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
681 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
687 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
695 if( tk->i_compression_type == MATROSKA_COMPRESSION_ZLIB &&
696 tk->i_encoding_scope & MATROSKA_ENCODING_SCOPE_PRIVATE &&
697 tk->i_extra_data && tk->p_extra_data &&
698 zlib_decompress_extra( &sys.demuxer, tk) )
701 if( TrackInit( tk ) )
703 msg_Err(&sys.demuxer, "Couldn't init track %d", tk->i_number );
704 free(tk->p_extra_data);
709 tracks.push_back( tk );
713 msg_Err( &sys.demuxer, "Track Entry %d not supported", tk->i_number );
714 free(tk->p_extra_data);
719 /*****************************************************************************
721 *****************************************************************************/
722 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
725 int i_upper_level = 0;
727 /* Master elements */
728 if( unlikely( tracks->GetSize() >= SIZE_MAX ) )
730 msg_Err( &sys.demuxer, "Track too big, aborting" );
735 tracks->Read( es, EBML_CONTEXT(tracks), i_upper_level, el, true );
739 msg_Err( &sys.demuxer, "Couldn't read tracks" );
743 for( size_t i = 0; i < tracks->ListSize(); i++ )
745 EbmlElement *l = (*tracks)[i];
747 if( MKV_IS_ID( l, KaxTrackEntry ) )
749 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
753 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
758 /*****************************************************************************
760 *****************************************************************************/
761 void matroska_segment_c::ParseInfo( KaxInfo *info )
765 int i_upper_level = 0;
767 /* Master elements */
768 m = static_cast<EbmlMaster *>(info);
769 if( unlikely( m->GetSize() >= SIZE_MAX ) )
771 msg_Err( &sys.demuxer, "Info too big, aborting" );
776 m->Read( es, EBML_CONTEXT(info), i_upper_level, el, true );
780 msg_Err( &sys.demuxer, "Couldn't read info" );
784 for( size_t i = 0; i < m->ListSize(); i++ )
786 EbmlElement *l = (*m)[i];
788 if( MKV_IS_ID( l, KaxSegmentUID ) )
790 if ( p_segment_uid == NULL )
791 p_segment_uid = new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l));
793 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)p_segment_uid->GetBuffer() );
795 else if( MKV_IS_ID( l, KaxPrevUID ) )
797 if ( p_prev_segment_uid == NULL )
799 p_prev_segment_uid = new KaxPrevUID(*static_cast<KaxPrevUID*>(l));
800 b_ref_external_segments = true;
803 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)p_prev_segment_uid->GetBuffer() );
805 else if( MKV_IS_ID( l, KaxNextUID ) )
807 if ( p_next_segment_uid == NULL )
809 p_next_segment_uid = new KaxNextUID(*static_cast<KaxNextUID*>(l));
810 b_ref_external_segments = true;
813 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)p_next_segment_uid->GetBuffer() );
815 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
817 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
819 i_timescale = uint64(tcs);
821 msg_Dbg( &sys.demuxer, "| | + TimecodeScale=%"PRId64,
824 else if( MKV_IS_ID( l, KaxDuration ) )
826 KaxDuration &dur = *(KaxDuration*)l;
828 i_duration = mtime_t( double( dur ) );
830 msg_Dbg( &sys.demuxer, "| | + Duration=%"PRId64,
833 else if( MKV_IS_ID( l, KaxMuxingApp ) )
835 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
837 psz_muxing_application = ToUTF8( UTFstring( mapp ) );
839 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
840 psz_muxing_application );
842 else if( MKV_IS_ID( l, KaxWritingApp ) )
844 KaxWritingApp &wapp = *(KaxWritingApp*)l;
846 psz_writing_application = ToUTF8( UTFstring( wapp ) );
848 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
849 psz_writing_application );
851 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
853 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
855 psz_segment_filename = ToUTF8( UTFstring( sfn ) );
857 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
858 psz_segment_filename );
860 else if( MKV_IS_ID( l, KaxTitle ) )
862 KaxTitle &title = *(KaxTitle*)l;
864 psz_title = ToUTF8( UTFstring( title ) );
866 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
868 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
870 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
872 families.push_back( new KaxSegmentFamily(*uid) );
874 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
876 else if( MKV_IS_ID( l, KaxDateUTC ) )
878 KaxDateUTC &date = *(KaxDateUTC*)l;
883 i_date = date.GetEpochDate();
884 if( gmtime_r( &i_date, &tmres ) &&
885 strftime( buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y",
888 psz_date_utc = strdup( buffer );
889 msg_Dbg( &sys.demuxer, "| | + Date=%s", buffer );
892 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
894 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
897 if( unlikely( p_trans->GetSize() >= SIZE_MAX ) )
899 msg_Err( &sys.demuxer, "Chapter translate too big, aborting" );
903 p_trans->Read( es, EBML_CONTEXT(p_trans), i_upper_level, el, true );
904 chapter_translation_c *p_translate = new chapter_translation_c();
906 for( size_t j = 0; j < p_trans->ListSize(); j++ )
908 EbmlElement *l = (*p_trans)[j];
910 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
912 p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
914 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
916 p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
918 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
920 p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
924 translations.push_back( p_translate );
928 msg_Err( &sys.demuxer, "Error while reading Chapter Tranlate");
933 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
937 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
938 i_duration = mtime_t(f_dur);
939 if( !i_duration ) i_duration = -1;
943 /*****************************************************************************
945 *****************************************************************************/
946 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
948 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
949 for( size_t i = 0; i < ca->ListSize(); i++ )
951 EbmlElement *l = (*ca)[i];
953 if( MKV_IS_ID( l, KaxChapterUID ) )
955 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
956 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %"PRIu64"", chapters.i_uid );
958 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
960 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
961 chapters.b_display_seekpoint = uint8( flag ) == 0;
963 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
965 else if( MKV_IS_ID( l, KaxChapterSegmentUID ) )
967 chapters.p_segment_uid = new KaxChapterSegmentUID( *static_cast<KaxChapterSegmentUID*>(l) );
968 b_ref_external_segments = true;
969 msg_Dbg( &sys.demuxer, "| | | | + ChapterSegmentUID= %u", *(uint32*)chapters.p_segment_uid->GetBuffer() );
971 else if( MKV_IS_ID( l, KaxChapterSegmentEditionUID ) )
973 chapters.p_segment_edition_uid = new KaxChapterSegmentEditionUID( *static_cast<KaxChapterSegmentEditionUID*>(l) );
974 msg_Dbg( &sys.demuxer, "| | | | + ChapterSegmentEditionUID= %u",
975 #if LIBMATROSKA_VERSION < 0x010300
976 *(uint32*)chapters.p_segment_edition_uid->GetBuffer()
978 *(uint32*)chapters.p_segment_edition_uid
982 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
984 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
985 chapters.i_start_time = uint64( start ) / INT64_C(1000);
987 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %"PRId64"", chapters.i_start_time );
989 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
991 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
992 chapters.i_end_time = uint64( end ) / INT64_C(1000);
994 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %"PRId64"", chapters.i_end_time );
996 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
998 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
1000 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
1001 for( size_t j = 0; j < cd->ListSize(); j++ )
1003 EbmlElement *l= (*cd)[j];
1005 if( MKV_IS_ID( l, KaxChapterString ) )
1007 KaxChapterString &name =*(KaxChapterString*)l;
1008 for ( int k = 0; k < i_level; k++)
1009 chapters.psz_name += '+';
1010 chapters.psz_name += ' ';
1011 char *psz_tmp_utf8 = ToUTF8( UTFstring( name ) );
1012 chapters.psz_name += psz_tmp_utf8;
1013 chapters.b_user_display = true;
1015 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", psz_tmp_utf8 );
1016 free( psz_tmp_utf8 );
1018 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
1020 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
1021 const char *psz = string( lang ).c_str();
1023 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
1025 else if( MKV_IS_ID( l, KaxChapterCountry ) )
1027 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
1028 const char *psz = string( ct ).c_str();
1030 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
1034 else if( MKV_IS_ID( l, KaxChapterProcess ) )
1036 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
1038 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
1039 chapter_codec_cmds_c *p_ccodec = NULL;
1041 for( size_t j = 0; j < cp->ListSize(); j++ )
1043 EbmlElement *k= (*cp)[j];
1045 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
1047 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
1048 if ( uint32(*p_codec_id) == 0 )
1049 p_ccodec = new matroska_script_codec_c( sys );
1050 else if ( uint32(*p_codec_id) == 1 )
1051 p_ccodec = new dvd_chapter_codec_c( sys );
1056 if ( p_ccodec != NULL )
1058 for( size_t j = 0; j < cp->ListSize(); j++ )
1060 EbmlElement *k= (*cp)[j];
1062 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
1064 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
1065 p_ccodec->SetPrivate( *p_private );
1067 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
1069 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
1072 chapters.codecs.push_back( p_ccodec );
1075 else if( MKV_IS_ID( l, KaxChapterAtom ) )
1077 chapter_item_c *new_sub_chapter = new chapter_item_c();
1078 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
1079 new_sub_chapter->p_parent = &chapters;
1080 chapters.sub_chapters.push_back( new_sub_chapter );
1085 /*****************************************************************************
1087 *****************************************************************************/
1088 void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
1091 int i_upper_level = 0;
1093 if( unlikely( attachments->GetSize() >= SIZE_MAX ) )
1095 msg_Err( &sys.demuxer, "Attachments too big, aborting" );
1100 attachments->Read( es, EBML_CONTEXT(attachments), i_upper_level, el, true );
1104 msg_Err( &sys.demuxer, "Error while reading attachments" );
1108 KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
1110 while( attachedFile && ( attachedFile->GetSize() > 0 ) )
1112 KaxFileData &img_data = GetChild<KaxFileData>( *attachedFile );
1113 char *psz_tmp_utf8 = ToUTF8( UTFstring( GetChild<KaxFileName>( *attachedFile ) ) );
1114 std::string attached_filename(psz_tmp_utf8);
1116 attachment_c *new_attachment = new attachment_c( attached_filename,
1117 GetChild<KaxMimeType>( *attachedFile ),
1118 img_data.GetSize() );
1120 msg_Dbg( &sys.demuxer, "| | - %s (%s)", new_attachment->fileName(), new_attachment->mimeType() );
1122 if( new_attachment->init() )
1124 memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
1125 sys.stored_attachments.push_back( new_attachment );
1126 if( !strncmp( new_attachment->mimeType(), "image/", 6 ) )
1129 if( asprintf( &psz_url, "attachment://%s",
1130 new_attachment->fileName() ) == -1 )
1133 sys.meta = vlc_meta_New();
1134 vlc_meta_SetArtURL( sys.meta, psz_url );
1140 delete new_attachment;
1143 attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
1147 /*****************************************************************************
1149 *****************************************************************************/
1150 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
1153 int i_upper_level = 0;
1155 /* Master elements */
1156 if( unlikely( chapters->GetSize() >= SIZE_MAX ) )
1158 msg_Err( &sys.demuxer, "Chapters too big, aborting" );
1163 chapters->Read( es, EBML_CONTEXT(chapters), i_upper_level, el, true );
1167 msg_Err( &sys.demuxer, "Error while reading chapters" );
1171 for( size_t i = 0; i < chapters->ListSize(); i++ )
1173 EbmlElement *l = (*chapters)[i];
1175 if( MKV_IS_ID( l, KaxEditionEntry ) )
1177 chapter_edition_c *p_edition = new chapter_edition_c();
1179 EbmlMaster *E = static_cast<EbmlMaster *>(l );
1180 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
1181 for( size_t j = 0; j < E->ListSize(); j++ )
1183 EbmlElement *l = (*E)[j];
1185 if( MKV_IS_ID( l, KaxChapterAtom ) )
1187 chapter_item_c *new_sub_chapter = new chapter_item_c();
1188 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
1189 p_edition->sub_chapters.push_back( new_sub_chapter );
1191 else if( MKV_IS_ID( l, KaxEditionUID ) )
1193 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
1195 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
1197 p_edition->b_ordered = var_InheritBool( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
1199 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
1201 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
1202 i_default_edition = stored_editions.size();
1204 else if( MKV_IS_ID( l, KaxEditionFlagHidden ) )
1206 // FIXME to implement
1210 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
1213 stored_editions.push_back( p_edition );
1217 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
1222 void matroska_segment_c::ParseCluster( bool b_update_start_time )
1226 int i_upper_level = 0;
1228 /* Master elements */
1229 m = static_cast<EbmlMaster *>( cluster );
1230 if( unlikely( m->GetSize() >= SIZE_MAX ) )
1232 msg_Err( &sys.demuxer, "Cluster too big, aborting" );
1237 m->Read( es, EBML_CONTEXT(cluster), i_upper_level, el, true );
1241 msg_Err( &sys.demuxer, "Error while reading cluster" );
1245 for( unsigned int i = 0; i < m->ListSize(); i++ )
1247 EbmlElement *l = (*m)[i];
1249 if( MKV_IS_ID( l, KaxClusterTimecode ) )
1251 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
1253 cluster->InitTimecode( uint64( ctc ), i_timescale );
1258 if( b_update_start_time )
1259 i_start_time = cluster->GlobalTimecode() / 1000;
1263 int32_t matroska_segment_c::TrackInit( mkv_track_t * p_tk )
1265 es_format_t *p_fmt = &p_tk->fmt;
1267 if( p_tk->psz_codec == NULL )
1269 msg_Err( &sys.demuxer, "Empty codec id" );
1270 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1274 if( !strcmp( p_tk->psz_codec, "V_MS/VFW/FOURCC" ) )
1276 if( p_tk->i_extra_data < (int)sizeof( VLC_BITMAPINFOHEADER ) )
1278 msg_Err( &sys.demuxer, "missing/invalid VLC_BITMAPINFOHEADER" );
1279 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1283 VLC_BITMAPINFOHEADER *p_bih = (VLC_BITMAPINFOHEADER*)p_tk->p_extra_data;
1285 p_tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
1286 p_tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
1287 p_tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
1289 p_tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( VLC_BITMAPINFOHEADER );
1290 if( p_tk->fmt.i_extra > 0 )
1292 /* Very unlikely yet possible: bug #5659*/
1293 size_t maxlen = p_tk->i_extra_data - sizeof( VLC_BITMAPINFOHEADER );
1294 p_tk->fmt.i_extra = ( (unsigned)p_tk->fmt.i_extra < maxlen )?
1295 p_tk->fmt.i_extra : maxlen;
1297 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1298 memcpy( p_tk->fmt.p_extra, &p_bih[1], p_tk->fmt.i_extra );
1301 p_tk->b_dts_only = true;
1303 else if( !strcmp( p_tk->psz_codec, "V_MPEG1" ) ||
1304 !strcmp( p_tk->psz_codec, "V_MPEG2" ) )
1306 p_tk->fmt.i_codec = VLC_CODEC_MPGV;
1307 if( p_tk->i_extra_data )
1308 fill_extra_data( p_tk, 0 );
1310 else if( !strncmp( p_tk->psz_codec, "V_THEORA", 8 ) )
1312 p_tk->fmt.i_codec = VLC_CODEC_THEORA;
1313 fill_extra_data( p_tk, 0 );
1314 p_tk->b_pts_only = true;
1316 else if( !strncmp( p_tk->psz_codec, "V_REAL/RV", 9 ) )
1318 uint8_t *p = p_tk->p_extra_data;
1320 if( !strcmp( p_tk->psz_codec, "V_REAL/RV10" ) )
1321 p_fmt->i_codec = VLC_CODEC_RV10;
1322 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV20" ) )
1323 p_fmt->i_codec = VLC_CODEC_RV20;
1324 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV30" ) )
1325 p_fmt->i_codec = VLC_CODEC_RV30;
1326 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV40" ) )
1327 p_fmt->i_codec = VLC_CODEC_RV40;
1329 /* Extract the framerate from the header */
1330 if( p_tk->i_extra_data >= 26 &&
1331 p[4] == 'V' && p[5] == 'I' && p[6] == 'D' && p[7] == 'O' &&
1332 p[8] == 'R' && p[9] == 'V' &&
1333 (p[10] == '3' || p[10] == '4') && p[11] == '0' )
1335 p_tk->fmt.video.i_frame_rate =
1336 p[22] << 24 | p[23] << 16 | p[24] << 8 | p[25] << 0;
1337 p_tk->fmt.video.i_frame_rate_base = 65536;
1340 fill_extra_data( p_tk, 26 );
1341 p_tk->b_dts_only = true;
1343 else if( !strncmp( p_tk->psz_codec, "V_DIRAC", 7 ) )
1345 p_tk->fmt.i_codec = VLC_CODEC_DIRAC;
1347 else if( !strncmp( p_tk->psz_codec, "V_VP8", 5 ) )
1349 p_tk->fmt.i_codec = VLC_CODEC_VP8;
1350 p_tk->b_pts_only = true;
1352 else if( !strncmp( p_tk->psz_codec, "V_VP9", 5 ) )
1354 p_tk->fmt.i_codec = VLC_CODEC_VP9;
1355 fill_extra_data( p_tk, 0 );
1357 else if( !strncmp( p_tk->psz_codec, "V_MPEG4", 7 ) )
1359 if( !strcmp( p_tk->psz_codec, "V_MPEG4/MS/V3" ) )
1361 p_tk->fmt.i_codec = VLC_CODEC_DIV3;
1363 else if( !strncmp( p_tk->psz_codec, "V_MPEG4/ISO", 11 ) )
1365 /* A MPEG 4 codec, SP, ASP, AP or AVC */
1366 if( !strcmp( p_tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
1367 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
1369 p_tk->fmt.i_codec = VLC_CODEC_MP4V;
1370 fill_extra_data( p_tk, 0 );
1373 else if( !strncmp( p_tk->psz_codec, "V_MPEGH/ISO/HEVC", 16) )
1375 p_tk->fmt.i_codec = VLC_CODEC_HEVC;
1376 fill_extra_data( p_tk, 0 );
1378 else if( !strcmp( p_tk->psz_codec, "V_QUICKTIME" ) )
1380 MP4_Box_t *p_box = (MP4_Box_t*)xmalloc( sizeof( MP4_Box_t ) );
1381 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1385 if( MP4_ReadBoxCommon( p_mp4_stream, p_box ) &&
1386 MP4_ReadBox_sample_vide( p_mp4_stream, p_box ) )
1388 p_tk->fmt.i_codec = p_box->i_type;
1389 p_tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
1390 p_tk->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
1391 p_tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
1392 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1393 memcpy( p_tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, p_tk->fmt.i_extra );
1394 MP4_FreeBox_sample_vide( p_box );
1400 stream_Delete( p_mp4_stream );
1402 else if( !strcmp( p_tk->psz_codec, "V_MJPEG" ) )
1404 p_tk->fmt.i_codec = VLC_CODEC_MJPG;
1406 else if( !strcmp( p_tk->psz_codec, "A_MS/ACM" ) )
1408 if( p_tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1410 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
1411 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1415 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)p_tk->p_extra_data;
1416 if( p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1417 p_tk->i_extra_data >= sizeof(WAVEFORMATEXTENSIBLE) )
1419 WAVEFORMATEXTENSIBLE * p_wext = (WAVEFORMATEXTENSIBLE*) p_wf;
1420 sf_tag_to_fourcc( &p_wext->SubFormat, &p_tk->fmt.i_codec, NULL);
1421 /* FIXME should we use Samples and dwChannelMask?*/
1424 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_tk->fmt.i_codec, NULL );
1426 if( p_tk->fmt.i_codec == VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
1427 msg_Err( &sys.demuxer, "Unrecognized wf tag: 0x%x", GetWLE( &p_wf->wFormatTag ) );
1428 p_tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
1429 p_tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
1430 p_tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
1431 p_tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
1432 p_tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
1434 p_tk->fmt.i_extra = GetWLE( &p_wf->cbSize );
1435 if( p_tk->fmt.i_extra > 0 )
1437 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1438 memcpy( p_tk->fmt.p_extra, &p_wf[1], p_tk->fmt.i_extra );
1442 else if( !strcmp( p_tk->psz_codec, "A_MPEG/L3" ) ||
1443 !strcmp( p_tk->psz_codec, "A_MPEG/L2" ) ||
1444 !strcmp( p_tk->psz_codec, "A_MPEG/L1" ) )
1446 p_tk->fmt.i_codec = VLC_CODEC_MPGA;
1448 else if( !strcmp( p_tk->psz_codec, "A_AC3" ) )
1450 p_tk->fmt.i_codec = VLC_CODEC_A52;
1452 else if( !strcmp( p_tk->psz_codec, "A_EAC3" ) )
1454 p_tk->fmt.i_codec = VLC_CODEC_EAC3;
1456 else if( !strcmp( p_tk->psz_codec, "A_DTS" ) )
1458 p_tk->fmt.i_codec = VLC_CODEC_DTS;
1460 else if( !strcmp( p_tk->psz_codec, "A_MLP" ) )
1462 p_tk->fmt.i_codec = VLC_CODEC_MLP;
1464 else if( !strcmp( p_tk->psz_codec, "A_TRUEHD" ) )
1466 /* FIXME when more samples arrive */
1467 p_tk->fmt.i_codec = VLC_CODEC_TRUEHD;
1468 p_fmt->b_packetized = false;
1470 else if( !strcmp( p_tk->psz_codec, "A_FLAC" ) )
1472 p_tk->fmt.i_codec = VLC_CODEC_FLAC;
1473 fill_extra_data( p_tk, 8 );
1475 else if( !strcmp( p_tk->psz_codec, "A_VORBIS" ) )
1477 p_tk->fmt.i_codec = VLC_CODEC_VORBIS;
1478 fill_extra_data( p_tk, 0 );
1480 else if( !strncmp( p_tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1481 !strncmp( p_tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1483 int i_profile, i_srate, sbr = 0;
1484 static const unsigned int i_sample_rates[] =
1486 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1487 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1490 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1491 /* create data for faad (MP4DecSpecificDescrTag)*/
1493 if( !strcmp( &p_tk->psz_codec[12], "MAIN" ) )
1497 else if( !strcmp( &p_tk->psz_codec[12], "LC" ) )
1501 else if( !strcmp( &p_tk->psz_codec[12], "SSR" ) )
1505 else if( !strcmp( &p_tk->psz_codec[12], "LC/SBR" ) )
1515 for( i_srate = 0; i_srate < 13; i_srate++ )
1517 if( i_sample_rates[i_srate] == p_tk->i_original_rate )
1522 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
1524 p_tk->fmt.i_extra = sbr ? 5 : 2;
1525 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1526 ((uint8_t*)p_tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1527 ((uint8_t*)p_tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (p_tk->fmt.audio.i_channels << 3);
1530 int syncExtensionType = 0x2B7;
1532 for (iDSRI=0; iDSRI<13; iDSRI++)
1533 if( i_sample_rates[iDSRI] == p_tk->fmt.audio.i_rate )
1535 ((uint8_t*)p_tk->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
1536 ((uint8_t*)p_tk->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
1537 ((uint8_t*)p_tk->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
1540 else if( !strcmp( p_tk->psz_codec, "A_AAC" ) )
1542 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1543 fill_extra_data( p_tk, 0 );
1545 else if( !strcmp( p_tk->psz_codec, "A_ALAC" ) )
1547 p_tk->fmt.i_codec = VLC_CODEC_ALAC;
1548 fill_extra_data( p_tk, 0 );
1550 else if( !strcmp( p_tk->psz_codec, "A_WAVPACK4" ) )
1552 p_tk->fmt.i_codec = VLC_CODEC_WAVPACK;
1553 fill_extra_data( p_tk, 0 );
1555 else if( !strcmp( p_tk->psz_codec, "A_TTA1" ) )
1557 p_fmt->i_codec = VLC_CODEC_TTA;
1558 if( p_tk->i_extra_data > 0 )
1560 fill_extra_data( p_tk, 0 );
1564 p_fmt->i_extra = 30;
1565 p_fmt->p_extra = xmalloc( p_fmt->i_extra );
1566 uint8_t *p_extra = (uint8_t*)p_fmt->p_extra;
1567 memcpy( &p_extra[ 0], "TTA1", 4 );
1568 SetWLE( &p_extra[ 4], 1 );
1569 SetWLE( &p_extra[ 6], p_fmt->audio.i_channels );
1570 SetWLE( &p_extra[ 8], p_fmt->audio.i_bitspersample );
1571 SetDWLE( &p_extra[10], p_fmt->audio.i_rate );
1572 SetDWLE( &p_extra[14], 0xffffffff );
1573 memset( &p_extra[18], 0, 30 - 18 );
1576 else if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) ||
1577 !strcmp( p_tk->psz_codec, "A_PCM/INT/LIT" ) ||
1578 !strcmp( p_tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
1580 if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) )
1582 p_tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1586 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1588 p_tk->fmt.audio.i_blockalign = ( p_tk->fmt.audio.i_bitspersample + 7 ) / 8 * p_tk->fmt.audio.i_channels;
1590 else if( !strncmp( p_tk->psz_codec, "A_REAL/", 7 ) )
1592 if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
1594 p_fmt->i_codec = VLC_CODEC_RA_144;
1595 p_fmt->audio.i_channels = 1;
1596 p_fmt->audio.i_rate = 8000;
1597 p_fmt->audio.i_blockalign = 0x14;
1599 else if( p_tk->i_extra_data > 28 )
1601 uint8_t *p = p_tk->p_extra_data;
1602 if( memcmp( p, ".ra", 3 ) ) {
1603 msg_Err( &sys.demuxer, "Invalid Real ExtraData 0x%4.4s", (char *)p );
1604 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1608 real_audio_private * priv = (real_audio_private*) p_tk->p_extra_data;
1609 if( !strcmp( p_tk->psz_codec, "A_REAL/COOK" ) )
1611 p_tk->fmt.i_codec = VLC_CODEC_COOK;
1612 p_tk->fmt.audio.i_blockalign = hton16(priv->sub_packet_size);
1614 else if( !strcmp( p_tk->psz_codec, "A_REAL/ATRC" ) )
1615 p_tk->fmt.i_codec = VLC_CODEC_ATRAC3;
1616 else if( !strcmp( p_tk->psz_codec, "A_REAL/28_8" ) )
1617 p_tk->fmt.i_codec = VLC_CODEC_RA_288;
1618 /* FIXME RALF and SIPR */
1619 uint16_t version = (uint16_t) hton16(priv->version);
1621 new Cook_PrivateTrackData( hton16(priv->sub_packet_h),
1622 hton16(priv->frame_size),
1623 hton16(priv->sub_packet_size));
1624 if( unlikely( !p_tk->p_sys ) )
1627 if( unlikely( p_tk->p_sys->Init() ) )
1632 real_audio_private_v4 * v4 = (real_audio_private_v4*) priv;
1633 p_tk->fmt.audio.i_channels = hton16(v4->channels);
1634 p_tk->fmt.audio.i_bitspersample = hton16(v4->sample_size);
1635 p_tk->fmt.audio.i_rate = hton16(v4->sample_rate);
1637 else if( version == 5 )
1639 real_audio_private_v5 * v5 = (real_audio_private_v5*) priv;
1640 p_tk->fmt.audio.i_channels = hton16(v5->channels);
1641 p_tk->fmt.audio.i_bitspersample = hton16(v5->sample_size);
1642 p_tk->fmt.audio.i_rate = hton16(v5->sample_rate);
1644 msg_Dbg(&sys.demuxer, "%d channels %d bits %d Hz",p_tk->fmt.audio.i_channels, p_tk->fmt.audio.i_bitspersample, p_tk->fmt.audio.i_rate);
1646 fill_extra_data( p_tk, p_tk->fmt.i_codec == VLC_CODEC_RA_288 ? 0 : 78);
1650 else if( !strcmp( p_tk->psz_codec, "S_KATE" ) )
1652 p_tk->fmt.i_codec = VLC_CODEC_KATE;
1653 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1655 fill_extra_data( p_tk, 0 );
1657 else if( !strcmp( p_tk->psz_codec, "S_TEXT/ASCII" ) )
1659 p_fmt->i_codec = VLC_CODEC_SUBT;
1660 p_fmt->subs.psz_encoding = strdup( "ASCII" );
1662 else if( !strcmp( p_tk->psz_codec, "S_TEXT/UTF8" ) )
1664 p_tk->fmt.i_codec = VLC_CODEC_SUBT;
1665 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1667 else if( !strcmp( p_tk->psz_codec, "S_TEXT/USF" ) )
1669 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
1670 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1671 if( p_tk->i_extra_data )
1672 fill_extra_data( p_tk, 0 );
1674 else if( !strcmp( p_tk->psz_codec, "S_TEXT/SSA" ) ||
1675 !strcmp( p_tk->psz_codec, "S_TEXT/ASS" ) ||
1676 !strcmp( p_tk->psz_codec, "S_SSA" ) ||
1677 !strcmp( p_tk->psz_codec, "S_ASS" ))
1679 p_tk->fmt.i_codec = VLC_CODEC_SSA;
1680 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1681 if( p_tk->i_extra_data )
1682 fill_extra_data( p_tk, 0 );
1684 else if( !strcmp( p_tk->psz_codec, "S_VOBSUB" ) )
1686 p_tk->fmt.i_codec = VLC_CODEC_SPU;
1687 if( p_tk->i_extra_data )
1690 char *psz_buf = (char *)malloc( p_tk->i_extra_data + 1);
1691 if( psz_buf != NULL )
1693 memcpy( psz_buf, p_tk->p_extra_data , p_tk->i_extra_data );
1694 psz_buf[p_tk->i_extra_data] = '\0';
1696 psz_start = strstr( psz_buf, "size:" );
1698 vobsub_size_parse( psz_start,
1699 &p_tk->fmt.subs.spu.i_original_frame_width,
1700 &p_tk->fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
1702 msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d",
1703 p_tk->fmt.subs.spu.i_original_frame_width,
1704 p_tk->fmt.subs.spu.i_original_frame_height );
1708 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1712 psz_start = strstr( psz_buf, "palette:" );
1714 vobsub_palette_parse( psz_start, &p_tk->fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
1716 p_tk->fmt.subs.spu.palette[0] = 0xBeef;
1717 msg_Dbg( &sys.demuxer, "vobsub palette read" );
1721 msg_Warn( &sys.demuxer, "reading original palette failed" );
1727 else if( !strcmp( p_tk->psz_codec, "S_HDMV/PGS" ) )
1729 p_tk->fmt.i_codec = VLC_CODEC_BD_PG;
1731 else if( !strcmp( p_tk->psz_codec, "B_VOBBTN" ) )
1733 p_tk->fmt.i_cat = NAV_ES;
1737 msg_Err( &sys.demuxer, "unknown codec id=`%s'", p_tk->psz_codec );
1738 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );