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( MKV_IS_ID( l, KaxSeekID ) )
95 KaxSeekID &sid = *(KaxSeekID*)l;
96 sid.ReadData( es.I_O() );
97 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
99 else if( MKV_IS_ID( l, KaxSeekPosition ) )
101 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
102 spos.ReadData( es.I_O() );
103 i_pos = (int64_t)segment->GetGlobalPosition( uint64( spos ) );
107 /* Many mkvmerge files hit this case. It seems to be a broken SeekHead */
108 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
114 msg_Err( &sys.demuxer,"Error while reading %s", typeid(*l).name() );
120 if( id == EBML_ID(KaxCues) )
122 msg_Dbg( &sys.demuxer, "| - cues at %"PRId64, i_pos );
123 LoadSeekHeadItem( EBML_INFO(KaxCues), i_pos );
125 else if( id == EBML_ID(KaxInfo) )
127 msg_Dbg( &sys.demuxer, "| - info at %"PRId64, i_pos );
128 LoadSeekHeadItem( EBML_INFO(KaxInfo), i_pos );
130 else if( id == EBML_ID(KaxChapters) )
132 msg_Dbg( &sys.demuxer, "| - chapters at %"PRId64, i_pos );
133 LoadSeekHeadItem( EBML_INFO(KaxChapters), i_pos );
135 else if( id == EBML_ID(KaxTags) )
137 msg_Dbg( &sys.demuxer, "| - tags at %"PRId64, i_pos );
138 LoadSeekHeadItem( EBML_INFO(KaxTags), i_pos );
140 else if( id == EBML_ID(KaxSeekHead) )
142 msg_Dbg( &sys.demuxer, "| - chained seekhead at %"PRId64, i_pos );
143 LoadSeekHeadItem( EBML_INFO(KaxSeekHead), i_pos );
145 else if( id == EBML_ID(KaxTracks) )
147 msg_Dbg( &sys.demuxer, "| - tracks at %"PRId64, i_pos );
148 LoadSeekHeadItem( EBML_INFO(KaxTracks), i_pos );
150 else if( id == EBML_ID(KaxAttachments) )
152 msg_Dbg( &sys.demuxer, "| - attachments at %"PRId64, i_pos );
153 LoadSeekHeadItem( EBML_INFO(KaxAttachments), i_pos );
157 msg_Dbg( &sys.demuxer, "| - unknown seekhead reference at %"PRId64, i_pos );
162 msg_Dbg( &sys.demuxer, "| | + ParseSeekHead Unknown (%s)", typeid(*l).name() );
169 * Helper function to print the mkv parse tree
171 static void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ... )
176 msg_Err( &demuxer, "MKV tree is too deep" );
179 va_start( args, psz_format );
180 static const char psz_foo[] = "| | | | | | | | | |";
181 char *psz_foo2 = (char*)malloc( i_level * 4 + 3 + strlen( psz_format ) );
182 strncpy( psz_foo2, psz_foo, 4 * i_level );
183 psz_foo2[ 4 * i_level ] = '+';
184 psz_foo2[ 4 * i_level + 1 ] = ' ';
185 strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
186 msg_GenericVa( &demuxer,VLC_MSG_DBG, "mkv", psz_foo2, args );
192 /*****************************************************************************
194 *****************************************************************************/
195 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
197 bool bSupported = true;
200 mkv_track_t *tk = new mkv_track_t();
201 memset( tk, 0, sizeof( mkv_track_t ) );
203 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
205 tk->fmt.psz_language = strdup("English");
206 tk->fmt.psz_description = NULL;
208 tk->b_default = true;
209 tk->b_enabled = true;
210 tk->b_forced = false;
211 tk->b_silent = false;
212 tk->i_number = tracks.size() - 1;
213 tk->i_extra_data = 0;
214 tk->p_extra_data = NULL;
215 tk->psz_codec = NULL;
216 tk->b_dts_only = false;
217 tk->i_default_duration = 0;
218 tk->f_timecodescale = 1.0;
220 tk->b_inited = false;
222 tk->p_data_init = NULL;
224 tk->psz_codec_name = NULL;
225 tk->psz_codec_settings = NULL;
226 tk->psz_codec_info_url = NULL;
227 tk->psz_codec_download_url = NULL;
229 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
230 tk->i_encoding_scope = MATROSKA_ENCODING_SCOPE_ALL_FRAMES;
231 tk->p_compression_data = NULL;
233 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
235 for( size_t i = 0; i < m->ListSize(); i++ )
237 EbmlElement *l = (*m)[i];
239 if( MKV_IS_ID( l, KaxTrackNumber ) )
241 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
243 tk->i_number = uint32( tnum );
244 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
246 else if( MKV_IS_ID( l, KaxTrackUID ) )
248 KaxTrackUID &tuid = *(KaxTrackUID*)l;
250 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
252 else if( MKV_IS_ID( l, KaxTrackType ) )
254 const char *psz_type;
255 KaxTrackType &ttype = *(KaxTrackType*)l;
257 switch( uint8(ttype) )
261 tk->fmt.i_cat = AUDIO_ES;
262 tk->fmt.audio.i_channels = 1;
263 tk->fmt.audio.i_rate = 8000;
267 tk->fmt.i_cat = VIDEO_ES;
270 psz_type = "subtitle";
271 tk->fmt.i_cat = SPU_ES;
274 psz_type = "buttons";
275 tk->fmt.i_cat = SPU_ES;
278 psz_type = "unknown";
279 tk->fmt.i_cat = UNKNOWN_ES;
283 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
285 else if( MKV_IS_ID( l, KaxTrackFlagEnabled ) ) // UNUSED
287 KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
289 tk->b_enabled = uint32( fenb );
290 msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u", uint32( fenb ) );
292 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
294 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
296 tk->b_default = uint32( fdef );
297 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
299 else if( MKV_IS_ID( l, KaxTrackFlagForced ) ) // UNUSED
301 KaxTrackFlagForced &ffor = *(KaxTrackFlagForced*)l;
302 tk->b_forced = uint32( ffor );
304 msg_Dbg( &sys.demuxer, "| | | + Track Forced=%u", uint32( ffor ) );
306 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) ) // UNUSED
308 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
310 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
312 else if( MKV_IS_ID( l, KaxTrackMinCache ) ) // UNUSED
314 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
316 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
318 else if( MKV_IS_ID( l, KaxTrackMaxCache ) ) // UNUSED
320 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
322 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
324 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
326 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
328 tk->i_default_duration = uint64(defd);
329 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration=%"PRId64, uint64(defd) );
331 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
333 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
335 tk->f_timecodescale = float( ttcs );
336 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
338 else if( MKV_IS_ID( l, KaxMaxBlockAdditionID ) ) // UNUSED
340 KaxMaxBlockAdditionID &mbl = *(KaxMaxBlockAdditionID*)l;
342 msg_Dbg( &sys.demuxer, "| | | + Track Max BlockAdditionID=%d", uint32( mbl ) );
344 else if( MKV_IS_ID( l, KaxTrackName ) )
346 KaxTrackName &tname = *(KaxTrackName*)l;
348 tk->fmt.psz_description = ToUTF8( UTFstring( tname ) );
349 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
351 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
353 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
355 free( tk->fmt.psz_language );
356 tk->fmt.psz_language = strdup( string( lang ).c_str() );
357 msg_Dbg( &sys.demuxer,
358 "| | | + Track Language=`%s'", tk->fmt.psz_language );
360 else if( MKV_IS_ID( l, KaxCodecID ) )
362 KaxCodecID &codecid = *(KaxCodecID*)l;
364 tk->psz_codec = strdup( string( codecid ).c_str() );
365 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
367 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
369 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
371 tk->i_extra_data = cpriv.GetSize();
372 if( tk->i_extra_data > 0 )
374 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
375 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
377 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size=%"PRId64, cpriv.GetSize() );
379 else if( MKV_IS_ID( l, KaxCodecName ) )
381 KaxCodecName &cname = *(KaxCodecName*)l;
383 tk->psz_codec_name = ToUTF8( UTFstring( cname ) );
384 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
387 else if( MKV_IS_ID( l, KaxCodecDecodeAll ) ) // UNUSED
389 KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
391 msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u", uint8( cdall ) );
393 else if( MKV_IS_ID( l, KaxTrackOverlay ) ) // UNUSED
395 KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
397 msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u", uint32( tovr ) );
399 else if( MKV_IS_ID( l, KaxContentEncodings ) )
401 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
402 MkvTree( sys.demuxer, 3, "Content Encodings" );
403 if ( cencs->ListSize() > 1 )
405 msg_Err( &sys.demuxer, "Multiple Compression method not supported" );
408 for( size_t j = 0; j < cencs->ListSize(); j++ )
410 EbmlElement *l2 = (*cencs)[j];
411 if( MKV_IS_ID( l2, KaxContentEncoding ) )
413 MkvTree( sys.demuxer, 4, "Content Encoding" );
414 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
415 for( size_t k = 0; k < cenc->ListSize(); k++ )
417 EbmlElement *l3 = (*cenc)[k];
418 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
420 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
421 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
423 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
425 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
426 tk->i_encoding_scope = uint32( encscope );
427 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
429 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
431 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
432 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
434 else if( MKV_IS_ID( l3, KaxContentCompression ) )
436 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
437 MkvTree( sys.demuxer, 5, "Content Compression" );
438 //Default compression type is 0 (Zlib)
439 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
440 for( size_t n = 0; n < compr->ListSize(); n++ )
442 EbmlElement *l4 = (*compr)[n];
443 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
445 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
446 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
447 tk->i_compression_type = uint32( compalg );
448 if ( ( tk->i_compression_type != MATROSKA_COMPRESSION_ZLIB ) &&
449 ( tk->i_compression_type != MATROSKA_COMPRESSION_HEADER ) )
451 msg_Err( &sys.demuxer, "Track Compression method %d not supported", tk->i_compression_type );
455 else if( MKV_IS_ID( l4, KaxContentCompSettings ) )
457 tk->p_compression_data = new KaxContentCompSettings( *(KaxContentCompSettings*)l4 );
461 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
465 // ContentEncryption Unsupported
468 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
474 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
478 // else if( MKV_IS_ID( l, KaxCodecSettings) ) DEPRECATED by matroska
480 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
482 // tk->psz_codec_settings = ToUTF8( UTFstring( cset ) );
483 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
485 // else if( MKV_IS_ID( l, KaxCodecInfoURL) ) DEPRECATED by matroska
487 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
489 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
490 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
492 // else if( MKV_IS_ID( l, KaxCodecDownloadURL) ) DEPRECATED by matroska
494 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
496 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
497 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
499 else if( MKV_IS_ID( l, KaxTrackVideo ) )
501 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
502 unsigned int i_crop_right = 0, i_crop_left = 0, i_crop_top = 0, i_crop_bottom = 0;
503 unsigned int i_display_unit = 0, i_display_width = 0, i_display_height = 0;
505 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
508 tk->fmt.video.i_frame_rate_base = (unsigned int)(tk->i_default_duration / 1000);
509 tk->fmt.video.i_frame_rate = 1000000;
511 for( unsigned int j = 0; j < tkv->ListSize(); j++ )
513 EbmlElement *l = (*tkv)[j];
514 if( MKV_IS_ID( l, KaxVideoFlagInterlaced ) ) // UNUSED
516 KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)l;
518 msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
520 else if( MKV_IS_ID( l, KaxVideoStereoMode ) ) // UNUSED
522 KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)l;
524 msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
526 else if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
528 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
530 tk->fmt.video.i_width += uint16( vwidth );
531 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
533 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
535 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
537 tk->fmt.video.i_height += uint16( vheight );
538 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
540 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
542 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
544 i_display_width = uint16( vwidth );
545 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
547 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
549 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
551 i_display_height = uint16( vheight );
552 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
554 else if( MKV_IS_ID( l, KaxVideoPixelCropBottom ) )
556 KaxVideoPixelCropBottom &cropval = *(KaxVideoPixelCropBottom*)l;
558 i_crop_bottom = uint16( cropval );
559 msg_Dbg( &sys.demuxer, "| | | | + crop pixel bottom=%d", uint16( cropval ) );
561 else if( MKV_IS_ID( l, KaxVideoPixelCropTop ) )
563 KaxVideoPixelCropTop &cropval = *(KaxVideoPixelCropTop*)l;
565 i_crop_top = uint16( cropval );
566 msg_Dbg( &sys.demuxer, "| | | | + crop pixel top=%d", uint16( cropval ) );
568 else if( MKV_IS_ID( l, KaxVideoPixelCropRight ) )
570 KaxVideoPixelCropRight &cropval = *(KaxVideoPixelCropRight*)l;
572 i_crop_right = uint16( cropval );
573 msg_Dbg( &sys.demuxer, "| | | | + crop pixel right=%d", uint16( cropval ) );
575 else if( MKV_IS_ID( l, KaxVideoPixelCropLeft ) )
577 KaxVideoPixelCropLeft &cropval = *(KaxVideoPixelCropLeft*)l;
579 i_crop_left = uint16( cropval );
580 msg_Dbg( &sys.demuxer, "| | | | + crop pixel left=%d", uint16( cropval ) );
582 else if( MKV_IS_ID( l, KaxVideoDisplayUnit ) )
584 KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
586 i_display_unit = uint8( vdmode );
587 msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
588 i_display_unit == 0 ? "pixels" : ( i_display_unit == 1 ? "centimeters": "inches" ) );
590 else if( MKV_IS_ID( l, KaxVideoAspectRatio ) ) // UNUSED
592 KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
594 msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
596 // ColourSpace UNUSED
597 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
599 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
601 tk->f_fps = float( vfps );
602 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
604 // else if( MKV_IS_ID( l, KaxVideoGamma) ) //DEPRECATED by Matroska
606 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
608 // msg_Dbg( &sys.demuxer, " | | | + gamma=%f", float( gamma ) );
612 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
615 if( i_display_height && i_display_width )
617 tk->fmt.video.i_sar_num = i_display_width * tk->fmt.video.i_height;
618 tk->fmt.video.i_sar_den = i_display_height * tk->fmt.video.i_width;
620 if( i_crop_left || i_crop_right || i_crop_top || i_crop_bottom )
622 tk->fmt.video.i_visible_width = tk->fmt.video.i_width;
623 tk->fmt.video.i_visible_height = tk->fmt.video.i_height;
624 tk->fmt.video.i_x_offset = i_crop_left;
625 tk->fmt.video.i_y_offset = i_crop_top;
626 tk->fmt.video.i_visible_width -= i_crop_left + i_crop_right;
627 tk->fmt.video.i_visible_height -= i_crop_top + i_crop_bottom;
629 /* FIXME: i_display_* allows you to not only set DAR, but also a zoom factor.
630 we do not support this atm */
632 else if( MKV_IS_ID( l, KaxTrackAudio ) )
634 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
636 /* Initialize default values */
637 tk->fmt.audio.i_channels = 1;
638 tk->fmt.audio.i_rate = 8000;
640 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
642 for( unsigned int j = 0; j < tka->ListSize(); j++ )
644 EbmlElement *l = (*tka)[j];
646 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
648 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
650 tk->i_original_rate = tk->fmt.audio.i_rate = (int)float( afreq );
651 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
653 else if( MKV_IS_ID( l, KaxAudioOutputSamplingFreq ) )
655 KaxAudioOutputSamplingFreq &afreq = *(KaxAudioOutputSamplingFreq*)l;
657 tk->fmt.audio.i_rate = (int)float( afreq );
658 msg_Dbg( &sys.demuxer, "| | | | + aoutfreq=%d", tk->fmt.audio.i_rate );
660 else if( MKV_IS_ID( l, KaxAudioChannels ) )
662 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
664 tk->fmt.audio.i_channels = uint8( achan );
665 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
667 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
669 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
671 tk->fmt.audio.i_bitspersample = uint8( abits );
672 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
676 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
682 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
690 if( tk->i_compression_type == MATROSKA_COMPRESSION_ZLIB &&
691 tk->i_encoding_scope & MATROSKA_ENCODING_SCOPE_PRIVATE &&
692 tk->i_extra_data && tk->p_extra_data &&
693 zlib_decompress_extra( &sys.demuxer, tk) )
696 if( TrackInit( tk ) )
698 msg_Err(&sys.demuxer, "Couldn't init track %d", tk->i_number );
699 free(tk->p_extra_data);
704 tracks.push_back( tk );
708 msg_Err( &sys.demuxer, "Track Entry %d not supported", tk->i_number );
709 free(tk->p_extra_data);
714 /*****************************************************************************
716 *****************************************************************************/
717 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
720 int i_upper_level = 0;
722 /* Master elements */
725 tracks->Read( es, EBML_CONTEXT(tracks), i_upper_level, el, true );
729 msg_Err( &sys.demuxer, "Couldn't read tracks" );
733 for( size_t i = 0; i < tracks->ListSize(); i++ )
735 EbmlElement *l = (*tracks)[i];
737 if( MKV_IS_ID( l, KaxTrackEntry ) )
739 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
743 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
748 /*****************************************************************************
750 *****************************************************************************/
751 void matroska_segment_c::ParseInfo( KaxInfo *info )
755 int i_upper_level = 0;
757 /* Master elements */
758 m = static_cast<EbmlMaster *>(info);
761 m->Read( es, EBML_CONTEXT(info), i_upper_level, el, true );
765 msg_Err( &sys.demuxer, "Couldn't read info" );
769 for( size_t i = 0; i < m->ListSize(); i++ )
771 EbmlElement *l = (*m)[i];
773 if( MKV_IS_ID( l, KaxSegmentUID ) )
775 if ( p_segment_uid == NULL )
776 p_segment_uid = new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l));
778 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)p_segment_uid->GetBuffer() );
780 else if( MKV_IS_ID( l, KaxPrevUID ) )
782 if ( p_prev_segment_uid == NULL )
784 p_prev_segment_uid = new KaxPrevUID(*static_cast<KaxPrevUID*>(l));
785 b_ref_external_segments = true;
788 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)p_prev_segment_uid->GetBuffer() );
790 else if( MKV_IS_ID( l, KaxNextUID ) )
792 if ( p_next_segment_uid == NULL )
794 p_next_segment_uid = new KaxNextUID(*static_cast<KaxNextUID*>(l));
795 b_ref_external_segments = true;
798 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)p_next_segment_uid->GetBuffer() );
800 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
802 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
804 i_timescale = uint64(tcs);
806 msg_Dbg( &sys.demuxer, "| | + TimecodeScale=%"PRId64,
809 else if( MKV_IS_ID( l, KaxDuration ) )
811 KaxDuration &dur = *(KaxDuration*)l;
813 i_duration = mtime_t( double( dur ) );
815 msg_Dbg( &sys.demuxer, "| | + Duration=%"PRId64,
818 else if( MKV_IS_ID( l, KaxMuxingApp ) )
820 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
822 psz_muxing_application = ToUTF8( UTFstring( mapp ) );
824 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
825 psz_muxing_application );
827 else if( MKV_IS_ID( l, KaxWritingApp ) )
829 KaxWritingApp &wapp = *(KaxWritingApp*)l;
831 psz_writing_application = ToUTF8( UTFstring( wapp ) );
833 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
834 psz_writing_application );
836 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
838 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
840 psz_segment_filename = ToUTF8( UTFstring( sfn ) );
842 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
843 psz_segment_filename );
845 else if( MKV_IS_ID( l, KaxTitle ) )
847 KaxTitle &title = *(KaxTitle*)l;
849 psz_title = ToUTF8( UTFstring( title ) );
851 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
853 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
855 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
857 families.push_back( new KaxSegmentFamily(*uid) );
859 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
861 else if( MKV_IS_ID( l, KaxDateUTC ) )
863 KaxDateUTC &date = *(KaxDateUTC*)l;
868 i_date = date.GetEpochDate();
869 if( gmtime_r( &i_date, &tmres ) &&
870 strftime( buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y",
873 psz_date_utc = strdup( buffer );
874 msg_Dbg( &sys.demuxer, "| | + Date=%s", buffer );
877 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
879 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
882 p_trans->Read( es, EBML_CONTEXT(p_trans), i_upper_level, el, true );
883 chapter_translation_c *p_translate = new chapter_translation_c();
885 for( size_t j = 0; j < p_trans->ListSize(); j++ )
887 EbmlElement *l = (*p_trans)[j];
889 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
891 p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
893 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
895 p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
897 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
899 p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
903 translations.push_back( p_translate );
907 msg_Err( &sys.demuxer, "Error while reading Chapter Tranlate");
912 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
916 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
917 i_duration = mtime_t(f_dur);
918 if( !i_duration ) i_duration = -1;
922 /*****************************************************************************
924 *****************************************************************************/
925 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
927 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
928 for( size_t i = 0; i < ca->ListSize(); i++ )
930 EbmlElement *l = (*ca)[i];
932 if( MKV_IS_ID( l, KaxChapterUID ) )
934 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
935 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %"PRIu64"", chapters.i_uid );
937 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
939 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
940 chapters.b_display_seekpoint = uint8( flag ) == 0;
942 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
944 else if( MKV_IS_ID( l, KaxChapterSegmentUID ) )
946 chapters.p_segment_uid = new KaxChapterSegmentUID( *static_cast<KaxChapterSegmentUID*>(l) );
947 b_ref_external_segments = true;
948 msg_Dbg( &sys.demuxer, "| | | | + ChapterSegmentUID= %u", *(uint32*)chapters.p_segment_uid->GetBuffer() );
950 else if( MKV_IS_ID( l, KaxChapterSegmentEditionUID ) )
952 chapters.p_segment_edition_uid = new KaxChapterSegmentEditionUID( *static_cast<KaxChapterSegmentEditionUID*>(l) );
953 msg_Dbg( &sys.demuxer, "| | | | + ChapterSegmentEditionUID= %u",
954 #if LIBMATROSKA_VERSION < 0x010300
955 *(uint32*)chapters.p_segment_edition_uid->GetBuffer()
957 *(uint32*)chapters.p_segment_edition_uid
961 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
963 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
964 chapters.i_start_time = uint64( start ) / INT64_C(1000);
966 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %"PRId64"", chapters.i_start_time );
968 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
970 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
971 chapters.i_end_time = uint64( end ) / INT64_C(1000);
973 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %"PRId64"", chapters.i_end_time );
975 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
977 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
979 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
980 for( size_t j = 0; j < cd->ListSize(); j++ )
982 EbmlElement *l= (*cd)[j];
984 if( MKV_IS_ID( l, KaxChapterString ) )
986 KaxChapterString &name =*(KaxChapterString*)l;
987 for ( int k = 0; k < i_level; k++)
988 chapters.psz_name += '+';
989 chapters.psz_name += ' ';
990 char *psz_tmp_utf8 = ToUTF8( UTFstring( name ) );
991 chapters.psz_name += psz_tmp_utf8;
992 chapters.b_user_display = true;
994 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", psz_tmp_utf8 );
995 free( psz_tmp_utf8 );
997 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
999 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
1000 const char *psz = string( lang ).c_str();
1002 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
1004 else if( MKV_IS_ID( l, KaxChapterCountry ) )
1006 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
1007 const char *psz = string( ct ).c_str();
1009 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
1013 else if( MKV_IS_ID( l, KaxChapterProcess ) )
1015 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
1017 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
1018 chapter_codec_cmds_c *p_ccodec = NULL;
1020 for( size_t j = 0; j < cp->ListSize(); j++ )
1022 EbmlElement *k= (*cp)[j];
1024 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
1026 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
1027 if ( uint32(*p_codec_id) == 0 )
1028 p_ccodec = new matroska_script_codec_c( sys );
1029 else if ( uint32(*p_codec_id) == 1 )
1030 p_ccodec = new dvd_chapter_codec_c( sys );
1035 if ( p_ccodec != NULL )
1037 for( size_t j = 0; j < cp->ListSize(); j++ )
1039 EbmlElement *k= (*cp)[j];
1041 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
1043 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
1044 p_ccodec->SetPrivate( *p_private );
1046 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
1048 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
1051 chapters.codecs.push_back( p_ccodec );
1054 else if( MKV_IS_ID( l, KaxChapterAtom ) )
1056 chapter_item_c *new_sub_chapter = new chapter_item_c();
1057 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
1058 new_sub_chapter->p_parent = &chapters;
1059 chapters.sub_chapters.push_back( new_sub_chapter );
1064 /*****************************************************************************
1066 *****************************************************************************/
1067 void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
1070 int i_upper_level = 0;
1074 attachments->Read( es, EBML_CONTEXT(attachments), i_upper_level, el, true );
1078 msg_Err( &sys.demuxer, "Error while reading attachments" );
1082 KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
1084 while( attachedFile && ( attachedFile->GetSize() > 0 ) )
1086 KaxFileData &img_data = GetChild<KaxFileData>( *attachedFile );
1087 char *psz_tmp_utf8 = ToUTF8( UTFstring( GetChild<KaxFileName>( *attachedFile ) ) );
1088 std::string attached_filename(psz_tmp_utf8);
1090 attachment_c *new_attachment = new attachment_c( attached_filename,
1091 GetChild<KaxMimeType>( *attachedFile ),
1092 img_data.GetSize() );
1094 msg_Dbg( &sys.demuxer, "| | - %s (%s)", new_attachment->fileName(), new_attachment->mimeType() );
1096 if( new_attachment->init() )
1098 memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
1099 sys.stored_attachments.push_back( new_attachment );
1100 if( !strncmp( new_attachment->mimeType(), "image/", 6 ) )
1103 if( asprintf( &psz_url, "attachment://%s",
1104 new_attachment->fileName() ) == -1 )
1107 sys.meta = vlc_meta_New();
1108 vlc_meta_SetArtURL( sys.meta, psz_url );
1114 delete new_attachment;
1117 attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
1121 /*****************************************************************************
1123 *****************************************************************************/
1124 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
1127 int i_upper_level = 0;
1129 /* Master elements */
1132 chapters->Read( es, EBML_CONTEXT(chapters), i_upper_level, el, true );
1136 msg_Err( &sys.demuxer, "Error while reading chapters" );
1140 for( size_t i = 0; i < chapters->ListSize(); i++ )
1142 EbmlElement *l = (*chapters)[i];
1144 if( MKV_IS_ID( l, KaxEditionEntry ) )
1146 chapter_edition_c *p_edition = new chapter_edition_c();
1148 EbmlMaster *E = static_cast<EbmlMaster *>(l );
1149 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
1150 for( size_t j = 0; j < E->ListSize(); j++ )
1152 EbmlElement *l = (*E)[j];
1154 if( MKV_IS_ID( l, KaxChapterAtom ) )
1156 chapter_item_c *new_sub_chapter = new chapter_item_c();
1157 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
1158 p_edition->sub_chapters.push_back( new_sub_chapter );
1160 else if( MKV_IS_ID( l, KaxEditionUID ) )
1162 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
1164 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
1166 p_edition->b_ordered = var_InheritBool( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
1168 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
1170 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
1171 i_default_edition = stored_editions.size();
1173 else if( MKV_IS_ID( l, KaxEditionFlagHidden ) )
1175 // FIXME to implement
1179 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
1182 stored_editions.push_back( p_edition );
1186 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
1191 void matroska_segment_c::ParseCluster( bool b_update_start_time )
1195 int i_upper_level = 0;
1197 /* Master elements */
1198 m = static_cast<EbmlMaster *>( cluster );
1201 m->Read( es, EBML_CONTEXT(cluster), i_upper_level, el, true );
1205 msg_Err( &sys.demuxer, "Error while reading cluster" );
1209 for( unsigned int i = 0; i < m->ListSize(); i++ )
1211 EbmlElement *l = (*m)[i];
1213 if( MKV_IS_ID( l, KaxClusterTimecode ) )
1215 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
1217 cluster->InitTimecode( uint64( ctc ), i_timescale );
1222 if( b_update_start_time )
1223 i_start_time = cluster->GlobalTimecode() / 1000;
1227 int32_t matroska_segment_c::TrackInit( mkv_track_t * p_tk )
1229 es_format_t *p_fmt = &p_tk->fmt;
1230 if( !strcmp( p_tk->psz_codec, "V_MS/VFW/FOURCC" ) )
1232 if( p_tk->i_extra_data < (int)sizeof( VLC_BITMAPINFOHEADER ) )
1234 msg_Err( &sys.demuxer, "missing/invalid VLC_BITMAPINFOHEADER" );
1235 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1239 VLC_BITMAPINFOHEADER *p_bih = (VLC_BITMAPINFOHEADER*)p_tk->p_extra_data;
1241 p_tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
1242 p_tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
1243 p_tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
1245 p_tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( VLC_BITMAPINFOHEADER );
1246 if( p_tk->fmt.i_extra > 0 )
1248 /* Very unlikely yet possible: bug #5659*/
1249 size_t maxlen = p_tk->i_extra_data - sizeof( VLC_BITMAPINFOHEADER );
1250 p_tk->fmt.i_extra = ( (unsigned)p_tk->fmt.i_extra < maxlen )?
1251 p_tk->fmt.i_extra : maxlen;
1253 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1254 memcpy( p_tk->fmt.p_extra, &p_bih[1], p_tk->fmt.i_extra );
1257 p_tk->b_dts_only = true;
1259 else if( !strcmp( p_tk->psz_codec, "V_MPEG1" ) ||
1260 !strcmp( p_tk->psz_codec, "V_MPEG2" ) )
1262 p_tk->fmt.i_codec = VLC_CODEC_MPGV;
1263 if( p_tk->i_extra_data )
1264 fill_extra_data( p_tk, 0 );
1266 else if( !strncmp( p_tk->psz_codec, "V_THEORA", 8 ) )
1268 p_tk->fmt.i_codec = VLC_CODEC_THEORA;
1269 fill_extra_data( p_tk, 0 );
1270 p_tk->b_pts_only = true;
1272 else if( !strncmp( p_tk->psz_codec, "V_REAL/RV", 9 ) )
1274 uint8_t *p = p_tk->p_extra_data;
1276 if( !strcmp( p_tk->psz_codec, "V_REAL/RV10" ) )
1277 p_fmt->i_codec = VLC_CODEC_RV10;
1278 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV20" ) )
1279 p_fmt->i_codec = VLC_CODEC_RV20;
1280 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV30" ) )
1281 p_fmt->i_codec = VLC_CODEC_RV30;
1282 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV40" ) )
1283 p_fmt->i_codec = VLC_CODEC_RV40;
1285 /* Extract the framerate from the header */
1286 if( p_tk->i_extra_data >= 26 &&
1287 p[4] == 'V' && p[5] == 'I' && p[6] == 'D' && p[7] == 'O' &&
1288 p[8] == 'R' && p[9] == 'V' &&
1289 (p[10] == '3' || p[10] == '4') && p[11] == '0' )
1291 p_tk->fmt.video.i_frame_rate =
1292 p[22] << 24 | p[23] << 16 | p[24] << 8 | p[25] << 0;
1293 p_tk->fmt.video.i_frame_rate_base = 65536;
1296 fill_extra_data( p_tk, 26 );
1297 p_tk->b_dts_only = true;
1299 else if( !strncmp( p_tk->psz_codec, "V_DIRAC", 7 ) )
1301 p_tk->fmt.i_codec = VLC_CODEC_DIRAC;
1303 else if( !strncmp( p_tk->psz_codec, "V_VP8", 5 ) )
1305 p_tk->fmt.i_codec = VLC_CODEC_VP8;
1306 p_tk->b_pts_only = true;
1308 else if( !strncmp( p_tk->psz_codec, "V_MPEG4", 7 ) )
1310 if( !strcmp( p_tk->psz_codec, "V_MPEG4/MS/V3" ) )
1312 p_tk->fmt.i_codec = VLC_CODEC_DIV3;
1314 else if( !strncmp( p_tk->psz_codec, "V_MPEG4/ISO", 11 ) )
1316 /* A MPEG 4 codec, SP, ASP, AP or AVC */
1317 if( !strcmp( p_tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
1318 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
1320 p_tk->fmt.i_codec = VLC_CODEC_MP4V;
1321 fill_extra_data( p_tk, 0 );
1324 else if( !strcmp( p_tk->psz_codec, "V_QUICKTIME" ) )
1326 MP4_Box_t *p_box = (MP4_Box_t*)xmalloc( sizeof( MP4_Box_t ) );
1327 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1331 if( MP4_ReadBoxCommon( p_mp4_stream, p_box ) &&
1332 MP4_ReadBox_sample_vide( p_mp4_stream, p_box ) )
1334 p_tk->fmt.i_codec = p_box->i_type;
1335 p_tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
1336 p_tk->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
1337 p_tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
1338 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1339 memcpy( p_tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, p_tk->fmt.i_extra );
1340 MP4_FreeBox_sample_vide( p_box );
1346 stream_Delete( p_mp4_stream );
1348 else if( !strcmp( p_tk->psz_codec, "V_MJPEG" ) )
1350 p_tk->fmt.i_codec = VLC_CODEC_MJPG;
1352 else if( !strcmp( p_tk->psz_codec, "A_MS/ACM" ) )
1354 if( p_tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1356 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
1357 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1361 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)p_tk->p_extra_data;
1362 if( p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1363 p_tk->i_extra_data >= sizeof(WAVEFORMATEXTENSIBLE) )
1365 WAVEFORMATEXTENSIBLE * p_wext = (WAVEFORMATEXTENSIBLE*) p_wf;
1366 sf_tag_to_fourcc( &p_wext->SubFormat, &p_tk->fmt.i_codec, NULL);
1367 /* FIXME should we use Samples and dwChannelMask?*/
1370 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_tk->fmt.i_codec, NULL );
1372 if( p_tk->fmt.i_codec == VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
1373 msg_Err( &sys.demuxer, "Unrecognized wf tag: 0x%x", GetWLE( &p_wf->wFormatTag ) );
1374 p_tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
1375 p_tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
1376 p_tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
1377 p_tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
1378 p_tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
1380 p_tk->fmt.i_extra = GetWLE( &p_wf->cbSize );
1381 if( p_tk->fmt.i_extra > 0 )
1383 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1384 memcpy( p_tk->fmt.p_extra, &p_wf[1], p_tk->fmt.i_extra );
1388 else if( !strcmp( p_tk->psz_codec, "A_MPEG/L3" ) ||
1389 !strcmp( p_tk->psz_codec, "A_MPEG/L2" ) ||
1390 !strcmp( p_tk->psz_codec, "A_MPEG/L1" ) )
1392 p_tk->fmt.i_codec = VLC_CODEC_MPGA;
1394 else if( !strcmp( p_tk->psz_codec, "A_AC3" ) )
1396 p_tk->fmt.i_codec = VLC_CODEC_A52;
1398 else if( !strcmp( p_tk->psz_codec, "A_EAC3" ) )
1400 p_tk->fmt.i_codec = VLC_CODEC_EAC3;
1402 else if( !strcmp( p_tk->psz_codec, "A_DTS" ) )
1404 p_tk->fmt.i_codec = VLC_CODEC_DTS;
1406 else if( !strcmp( p_tk->psz_codec, "A_MLP" ) )
1408 p_tk->fmt.i_codec = VLC_CODEC_MLP;
1410 else if( !strcmp( p_tk->psz_codec, "A_TRUEHD" ) )
1412 /* FIXME when more samples arrive */
1413 p_tk->fmt.i_codec = VLC_CODEC_TRUEHD;
1414 p_fmt->b_packetized = false;
1416 else if( !strcmp( p_tk->psz_codec, "A_FLAC" ) )
1418 p_tk->fmt.i_codec = VLC_CODEC_FLAC;
1419 fill_extra_data( p_tk, 0 );
1421 else if( !strcmp( p_tk->psz_codec, "A_VORBIS" ) )
1423 p_tk->fmt.i_codec = VLC_CODEC_VORBIS;
1424 fill_extra_data( p_tk, 0 );
1426 else if( !strncmp( p_tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1427 !strncmp( p_tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1429 int i_profile, i_srate, sbr = 0;
1430 static const unsigned int i_sample_rates[] =
1432 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1433 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1436 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1437 /* create data for faad (MP4DecSpecificDescrTag)*/
1439 if( !strcmp( &p_tk->psz_codec[12], "MAIN" ) )
1443 else if( !strcmp( &p_tk->psz_codec[12], "LC" ) )
1447 else if( !strcmp( &p_tk->psz_codec[12], "SSR" ) )
1451 else if( !strcmp( &p_tk->psz_codec[12], "LC/SBR" ) )
1461 for( i_srate = 0; i_srate < 13; i_srate++ )
1463 if( i_sample_rates[i_srate] == p_tk->i_original_rate )
1468 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
1470 p_tk->fmt.i_extra = sbr ? 5 : 2;
1471 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1472 ((uint8_t*)p_tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1473 ((uint8_t*)p_tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (p_tk->fmt.audio.i_channels << 3);
1476 int syncExtensionType = 0x2B7;
1478 for (iDSRI=0; iDSRI<13; iDSRI++)
1479 if( i_sample_rates[iDSRI] == p_tk->fmt.audio.i_rate )
1481 ((uint8_t*)p_tk->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
1482 ((uint8_t*)p_tk->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
1483 ((uint8_t*)p_tk->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
1486 else if( !strcmp( p_tk->psz_codec, "A_AAC" ) )
1488 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1489 fill_extra_data( p_tk, 0 );
1491 else if( !strcmp( p_tk->psz_codec, "A_WAVPACK4" ) )
1493 p_tk->fmt.i_codec = VLC_CODEC_WAVPACK;
1494 fill_extra_data( p_tk, 0 );
1496 else if( !strcmp( p_tk->psz_codec, "A_TTA1" ) )
1498 p_fmt->i_codec = VLC_CODEC_TTA;
1499 if( p_tk->i_extra_data > 0 )
1501 fill_extra_data( p_tk, 0 );
1505 p_fmt->i_extra = 30;
1506 p_fmt->p_extra = xmalloc( p_fmt->i_extra );
1507 uint8_t *p_extra = (uint8_t*)p_fmt->p_extra;
1508 memcpy( &p_extra[ 0], "TTA1", 4 );
1509 SetWLE( &p_extra[ 4], 1 );
1510 SetWLE( &p_extra[ 6], p_fmt->audio.i_channels );
1511 SetWLE( &p_extra[ 8], p_fmt->audio.i_bitspersample );
1512 SetDWLE( &p_extra[10], p_fmt->audio.i_rate );
1513 SetDWLE( &p_extra[14], 0xffffffff );
1514 memset( &p_extra[18], 0, 30 - 18 );
1517 else if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) ||
1518 !strcmp( p_tk->psz_codec, "A_PCM/INT/LIT" ) ||
1519 !strcmp( p_tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
1521 if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) )
1523 p_tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1527 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1529 p_tk->fmt.audio.i_blockalign = ( p_tk->fmt.audio.i_bitspersample + 7 ) / 8 * p_tk->fmt.audio.i_channels;
1531 else if( !strncmp( p_tk->psz_codec, "A_REAL/", 7 ) )
1533 if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
1535 p_fmt->i_codec = VLC_CODEC_RA_144;
1536 p_fmt->audio.i_channels = 1;
1537 p_fmt->audio.i_rate = 8000;
1538 p_fmt->audio.i_blockalign = 0x14;
1540 else if( p_tk->i_extra_data > 28 )
1542 uint8_t *p = p_tk->p_extra_data;
1543 if( memcmp( p, ".ra", 3 ) ) {
1544 msg_Err( &sys.demuxer, "Invalid Real ExtraData 0x%4.4s", (char *)p );
1545 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1549 real_audio_private * priv = (real_audio_private*) p_tk->p_extra_data;
1550 if( !strcmp( p_tk->psz_codec, "A_REAL/COOK" ) )
1552 p_tk->fmt.i_codec = VLC_CODEC_COOK;
1553 p_tk->fmt.audio.i_blockalign = hton16(priv->sub_packet_size);
1555 else if( !strcmp( p_tk->psz_codec, "A_REAL/ATRC" ) )
1556 p_tk->fmt.i_codec = VLC_CODEC_ATRAC3;
1557 else if( !strcmp( p_tk->psz_codec, "A_REAL/28_8" ) )
1558 p_tk->fmt.i_codec = VLC_CODEC_RA_288;
1559 /* FIXME RALF and SIPR */
1560 uint16_t version = (uint16_t) hton16(priv->version);
1562 new Cook_PrivateTrackData( hton16(priv->sub_packet_h),
1563 hton16(priv->frame_size),
1564 hton16(priv->sub_packet_size));
1565 if( unlikely( !p_tk->p_sys ) )
1568 if( unlikely( p_tk->p_sys->Init() ) )
1573 real_audio_private_v4 * v4 = (real_audio_private_v4*) priv;
1574 p_tk->fmt.audio.i_channels = hton16(v4->channels);
1575 p_tk->fmt.audio.i_bitspersample = hton16(v4->sample_size);
1576 p_tk->fmt.audio.i_rate = hton16(v4->sample_rate);
1578 else if( version == 5 )
1580 real_audio_private_v5 * v5 = (real_audio_private_v5*) priv;
1581 p_tk->fmt.audio.i_channels = hton16(v5->channels);
1582 p_tk->fmt.audio.i_bitspersample = hton16(v5->sample_size);
1583 p_tk->fmt.audio.i_rate = hton16(v5->sample_rate);
1585 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);
1587 fill_extra_data( p_tk, p_tk->fmt.i_codec == VLC_CODEC_RA_288 ? 0 : 78);
1591 else if( !strcmp( p_tk->psz_codec, "S_KATE" ) )
1593 p_tk->fmt.i_codec = VLC_CODEC_KATE;
1594 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1596 fill_extra_data( p_tk, 0 );
1598 else if( !strcmp( p_tk->psz_codec, "S_TEXT/ASCII" ) )
1600 p_fmt->i_codec = VLC_CODEC_SUBT;
1601 p_fmt->subs.psz_encoding = strdup( "ASCII" );
1603 else if( !strcmp( p_tk->psz_codec, "S_TEXT/UTF8" ) )
1605 p_tk->fmt.i_codec = VLC_CODEC_SUBT;
1606 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1608 else if( !strcmp( p_tk->psz_codec, "S_TEXT/USF" ) )
1610 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
1611 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1612 if( p_tk->i_extra_data )
1613 fill_extra_data( p_tk, 0 );
1615 else if( !strcmp( p_tk->psz_codec, "S_TEXT/SSA" ) ||
1616 !strcmp( p_tk->psz_codec, "S_TEXT/ASS" ) ||
1617 !strcmp( p_tk->psz_codec, "S_SSA" ) ||
1618 !strcmp( p_tk->psz_codec, "S_ASS" ))
1620 p_tk->fmt.i_codec = VLC_CODEC_SSA;
1621 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1622 if( p_tk->i_extra_data )
1623 fill_extra_data( p_tk, 0 );
1625 else if( !strcmp( p_tk->psz_codec, "S_VOBSUB" ) )
1627 p_tk->fmt.i_codec = VLC_CODEC_SPU;
1628 if( p_tk->i_extra_data )
1631 char *psz_buf = (char *)malloc( p_tk->i_extra_data + 1);
1632 if( psz_buf != NULL )
1634 memcpy( psz_buf, p_tk->p_extra_data , p_tk->i_extra_data );
1635 psz_buf[p_tk->i_extra_data] = '\0';
1637 psz_start = strstr( psz_buf, "size:" );
1639 vobsub_size_parse( psz_start,
1640 &p_tk->fmt.subs.spu.i_original_frame_width,
1641 &p_tk->fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
1643 msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d",
1644 p_tk->fmt.subs.spu.i_original_frame_width,
1645 p_tk->fmt.subs.spu.i_original_frame_height );
1649 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1653 psz_start = strstr( psz_buf, "palette:" );
1655 vobsub_palette_parse( psz_start, &p_tk->fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
1657 p_tk->fmt.subs.spu.palette[0] = 0xBeef;
1658 msg_Dbg( &sys.demuxer, "vobsub palette read" );
1662 msg_Warn( &sys.demuxer, "reading original palette failed" );
1668 else if( !strcmp( p_tk->psz_codec, "S_HDMV/PGS" ) )
1670 p_tk->fmt.i_codec = VLC_CODEC_BD_PG;
1672 else if( !strcmp( p_tk->psz_codec, "B_VOBBTN" ) )
1674 p_tk->fmt.i_cat = NAV_ES;
1678 msg_Err( &sys.demuxer, "unknown codec id=`%s'", p_tk->psz_codec );
1679 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );