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" );
89 while( ( l = ep->Get() ) != NULL )
91 if( MKV_IS_ID( l, KaxSeekID ) )
93 KaxSeekID &sid = *(KaxSeekID*)l;
94 sid.ReadData( es.I_O() );
95 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
97 else if( MKV_IS_ID( l, KaxSeekPosition ) )
99 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
100 spos.ReadData( es.I_O() );
101 i_pos = (int64_t)segment->GetGlobalPosition( uint64( spos ) );
105 /* Many mkvmerge files hit this case. It seems to be a broken SeekHead */
106 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
113 if( id == EBML_ID(KaxCues) )
115 msg_Dbg( &sys.demuxer, "| - cues at %"PRId64, i_pos );
116 LoadSeekHeadItem( EBML_INFO(KaxCues), i_pos );
118 else if( id == EBML_ID(KaxInfo) )
120 msg_Dbg( &sys.demuxer, "| - info at %"PRId64, i_pos );
121 LoadSeekHeadItem( EBML_INFO(KaxInfo), i_pos );
123 else if( id == EBML_ID(KaxChapters) )
125 msg_Dbg( &sys.demuxer, "| - chapters at %"PRId64, i_pos );
126 LoadSeekHeadItem( EBML_INFO(KaxChapters), i_pos );
128 else if( id == EBML_ID(KaxTags) )
130 msg_Dbg( &sys.demuxer, "| - tags at %"PRId64, i_pos );
131 LoadSeekHeadItem( EBML_INFO(KaxTags), i_pos );
133 else if( id == EBML_ID(KaxSeekHead) )
135 msg_Dbg( &sys.demuxer, "| - chained seekhead at %"PRId64, i_pos );
136 LoadSeekHeadItem( EBML_INFO(KaxSeekHead), i_pos );
138 else if( id == EBML_ID(KaxTracks) )
140 msg_Dbg( &sys.demuxer, "| - tracks at %"PRId64, i_pos );
141 LoadSeekHeadItem( EBML_INFO(KaxTracks), i_pos );
143 else if( id == EBML_ID(KaxAttachments) )
145 msg_Dbg( &sys.demuxer, "| - attachments at %"PRId64, i_pos );
146 LoadSeekHeadItem( EBML_INFO(KaxAttachments), i_pos );
150 msg_Dbg( &sys.demuxer, "| - unknown seekhead reference at %"PRId64, i_pos );
155 msg_Dbg( &sys.demuxer, "| | + ParseSeekHead Unknown (%s)", typeid(*l).name() );
162 * Helper function to print the mkv parse tree
164 static void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ... )
169 msg_Err( &demuxer, "MKV tree is too deep" );
172 va_start( args, psz_format );
173 static const char psz_foo[] = "| | | | | | | | | |";
174 char *psz_foo2 = (char*)malloc( i_level * 4 + 3 + strlen( psz_format ) );
175 strncpy( psz_foo2, psz_foo, 4 * i_level );
176 psz_foo2[ 4 * i_level ] = '+';
177 psz_foo2[ 4 * i_level + 1 ] = ' ';
178 strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
179 msg_GenericVa( &demuxer,VLC_MSG_DBG, "mkv", psz_foo2, args );
185 /*****************************************************************************
187 *****************************************************************************/
188 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
190 bool bSupported = true;
193 mkv_track_t *tk = new mkv_track_t();
194 memset( tk, 0, sizeof( mkv_track_t ) );
196 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
198 tk->fmt.psz_language = strdup("English");
199 tk->fmt.psz_description = NULL;
201 tk->b_default = true;
202 tk->b_enabled = true;
203 tk->b_forced = false;
204 tk->b_silent = false;
205 tk->i_number = tracks.size() - 1;
206 tk->i_extra_data = 0;
207 tk->p_extra_data = NULL;
208 tk->psz_codec = NULL;
209 tk->b_dts_only = false;
210 tk->i_default_duration = 0;
211 tk->f_timecodescale = 1.0;
213 tk->b_inited = false;
215 tk->p_data_init = NULL;
217 tk->psz_codec_name = NULL;
218 tk->psz_codec_settings = NULL;
219 tk->psz_codec_info_url = NULL;
220 tk->psz_codec_download_url = NULL;
222 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
223 tk->i_encoding_scope = MATROSKA_ENCODING_SCOPE_ALL_FRAMES;
224 tk->p_compression_data = NULL;
226 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
228 for( size_t i = 0; i < m->ListSize(); i++ )
230 EbmlElement *l = (*m)[i];
232 if( MKV_IS_ID( l, KaxTrackNumber ) )
234 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
236 tk->i_number = uint32( tnum );
237 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
239 else if( MKV_IS_ID( l, KaxTrackUID ) )
241 KaxTrackUID &tuid = *(KaxTrackUID*)l;
243 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
245 else if( MKV_IS_ID( l, KaxTrackType ) )
247 const char *psz_type;
248 KaxTrackType &ttype = *(KaxTrackType*)l;
250 switch( uint8(ttype) )
254 tk->fmt.i_cat = AUDIO_ES;
255 tk->fmt.audio.i_channels = 1;
256 tk->fmt.audio.i_rate = 8000;
260 tk->fmt.i_cat = VIDEO_ES;
263 psz_type = "subtitle";
264 tk->fmt.i_cat = SPU_ES;
267 psz_type = "buttons";
268 tk->fmt.i_cat = SPU_ES;
271 psz_type = "unknown";
272 tk->fmt.i_cat = UNKNOWN_ES;
276 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
278 else if( MKV_IS_ID( l, KaxTrackFlagEnabled ) ) // UNUSED
280 KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
282 tk->b_enabled = uint32( fenb );
283 msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u", uint32( fenb ) );
285 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
287 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
289 tk->b_default = uint32( fdef );
290 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
292 else if( MKV_IS_ID( l, KaxTrackFlagForced ) ) // UNUSED
294 KaxTrackFlagForced &ffor = *(KaxTrackFlagForced*)l;
295 tk->b_forced = uint32( ffor );
297 msg_Dbg( &sys.demuxer, "| | | + Track Forced=%u", uint32( ffor ) );
299 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) ) // UNUSED
301 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
303 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
305 else if( MKV_IS_ID( l, KaxTrackMinCache ) ) // UNUSED
307 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
309 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
311 else if( MKV_IS_ID( l, KaxTrackMaxCache ) ) // UNUSED
313 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
315 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
317 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
319 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
321 tk->i_default_duration = uint64(defd);
322 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration=%"PRId64, uint64(defd) );
324 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
326 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
328 tk->f_timecodescale = float( ttcs );
329 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
331 else if( MKV_IS_ID( l, KaxMaxBlockAdditionID ) ) // UNUSED
333 KaxMaxBlockAdditionID &mbl = *(KaxMaxBlockAdditionID*)l;
335 msg_Dbg( &sys.demuxer, "| | | + Track Max BlockAdditionID=%d", uint32( mbl ) );
337 else if( MKV_IS_ID( l, KaxTrackName ) )
339 KaxTrackName &tname = *(KaxTrackName*)l;
341 tk->fmt.psz_description = ToUTF8( UTFstring( tname ) );
342 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
344 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
346 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
348 free( tk->fmt.psz_language );
349 tk->fmt.psz_language = strdup( string( lang ).c_str() );
350 msg_Dbg( &sys.demuxer,
351 "| | | + Track Language=`%s'", tk->fmt.psz_language );
353 else if( MKV_IS_ID( l, KaxCodecID ) )
355 KaxCodecID &codecid = *(KaxCodecID*)l;
357 tk->psz_codec = strdup( string( codecid ).c_str() );
358 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
360 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
362 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
364 tk->i_extra_data = cpriv.GetSize();
365 if( tk->i_extra_data > 0 )
367 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
368 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
370 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size=%"PRId64, cpriv.GetSize() );
372 else if( MKV_IS_ID( l, KaxCodecName ) )
374 KaxCodecName &cname = *(KaxCodecName*)l;
376 tk->psz_codec_name = ToUTF8( UTFstring( cname ) );
377 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
380 else if( MKV_IS_ID( l, KaxCodecDecodeAll ) ) // UNUSED
382 KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
384 msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u", uint8( cdall ) );
386 else if( MKV_IS_ID( l, KaxTrackOverlay ) ) // UNUSED
388 KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
390 msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u", uint32( tovr ) );
392 else if( MKV_IS_ID( l, KaxContentEncodings ) )
394 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
395 MkvTree( sys.demuxer, 3, "Content Encodings" );
396 if ( cencs->ListSize() > 1 )
398 msg_Err( &sys.demuxer, "Multiple Compression method not supported" );
401 for( size_t j = 0; j < cencs->ListSize(); j++ )
403 EbmlElement *l2 = (*cencs)[j];
404 if( MKV_IS_ID( l2, KaxContentEncoding ) )
406 MkvTree( sys.demuxer, 4, "Content Encoding" );
407 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
408 for( size_t k = 0; k < cenc->ListSize(); k++ )
410 EbmlElement *l3 = (*cenc)[k];
411 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
413 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
414 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
416 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
418 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
419 tk->i_encoding_scope = uint32( encscope );
420 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
422 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
424 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
425 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
427 else if( MKV_IS_ID( l3, KaxContentCompression ) )
429 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
430 MkvTree( sys.demuxer, 5, "Content Compression" );
431 //Default compression type is 0 (Zlib)
432 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
433 for( size_t n = 0; n < compr->ListSize(); n++ )
435 EbmlElement *l4 = (*compr)[n];
436 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
438 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
439 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
440 tk->i_compression_type = uint32( compalg );
441 if ( ( tk->i_compression_type != MATROSKA_COMPRESSION_ZLIB ) &&
442 ( tk->i_compression_type != MATROSKA_COMPRESSION_HEADER ) )
444 msg_Err( &sys.demuxer, "Track Compression method %d not supported", tk->i_compression_type );
448 else if( MKV_IS_ID( l4, KaxContentCompSettings ) )
450 tk->p_compression_data = new KaxContentCompSettings( *(KaxContentCompSettings*)l4 );
454 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
458 // ContentEncryption Unsupported
461 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
467 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
471 // else if( MKV_IS_ID( l, KaxCodecSettings) ) DEPRECATED by matroska
473 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
475 // tk->psz_codec_settings = ToUTF8( UTFstring( cset ) );
476 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
478 // else if( MKV_IS_ID( l, KaxCodecInfoURL) ) DEPRECATED by matroska
480 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
482 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
483 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
485 // else if( MKV_IS_ID( l, KaxCodecDownloadURL) ) DEPRECATED by matroska
487 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
489 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
490 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
492 else if( MKV_IS_ID( l, KaxTrackVideo ) )
494 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
495 unsigned int i_crop_right = 0, i_crop_left = 0, i_crop_top = 0, i_crop_bottom = 0;
496 unsigned int i_display_unit = 0, i_display_width = 0, i_display_height = 0;
498 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
501 tk->fmt.video.i_frame_rate_base = (unsigned int)(tk->i_default_duration / 1000);
502 tk->fmt.video.i_frame_rate = 1000000;
504 for( unsigned int j = 0; j < tkv->ListSize(); j++ )
506 EbmlElement *l = (*tkv)[j];
507 if( MKV_IS_ID( l, KaxVideoFlagInterlaced ) ) // UNUSED
509 KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)l;
511 msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
513 else if( MKV_IS_ID( l, KaxVideoStereoMode ) ) // UNUSED
515 KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)l;
517 msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
519 else if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
521 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
523 tk->fmt.video.i_width += uint16( vwidth );
524 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
526 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
528 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
530 tk->fmt.video.i_height += uint16( vheight );
531 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
533 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
535 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
537 i_display_width = uint16( vwidth );
538 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
540 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
542 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
544 i_display_height = uint16( vheight );
545 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
547 else if( MKV_IS_ID( l, KaxVideoPixelCropBottom ) )
549 KaxVideoPixelCropBottom &cropval = *(KaxVideoPixelCropBottom*)l;
551 i_crop_bottom = uint16( cropval );
552 msg_Dbg( &sys.demuxer, "| | | | + crop pixel bottom=%d", uint16( cropval ) );
554 else if( MKV_IS_ID( l, KaxVideoPixelCropTop ) )
556 KaxVideoPixelCropTop &cropval = *(KaxVideoPixelCropTop*)l;
558 i_crop_top = uint16( cropval );
559 msg_Dbg( &sys.demuxer, "| | | | + crop pixel top=%d", uint16( cropval ) );
561 else if( MKV_IS_ID( l, KaxVideoPixelCropRight ) )
563 KaxVideoPixelCropRight &cropval = *(KaxVideoPixelCropRight*)l;
565 i_crop_right = uint16( cropval );
566 msg_Dbg( &sys.demuxer, "| | | | + crop pixel right=%d", uint16( cropval ) );
568 else if( MKV_IS_ID( l, KaxVideoPixelCropLeft ) )
570 KaxVideoPixelCropLeft &cropval = *(KaxVideoPixelCropLeft*)l;
572 i_crop_left = uint16( cropval );
573 msg_Dbg( &sys.demuxer, "| | | | + crop pixel left=%d", uint16( cropval ) );
575 else if( MKV_IS_ID( l, KaxVideoDisplayUnit ) )
577 KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
579 i_display_unit = uint8( vdmode );
580 msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
581 i_display_unit == 0 ? "pixels" : ( i_display_unit == 1 ? "centimeters": "inches" ) );
583 else if( MKV_IS_ID( l, KaxVideoAspectRatio ) ) // UNUSED
585 KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
587 msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
589 // ColourSpace UNUSED
590 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
592 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
594 tk->f_fps = float( vfps );
595 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
597 // else if( MKV_IS_ID( l, KaxVideoGamma) ) //DEPRECATED by Matroska
599 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
601 // msg_Dbg( &sys.demuxer, " | | | + gamma=%f", float( gamma ) );
605 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
608 if( i_display_height && i_display_width )
610 tk->fmt.video.i_sar_num = i_display_width * tk->fmt.video.i_height;
611 tk->fmt.video.i_sar_den = i_display_height * tk->fmt.video.i_width;
613 if( i_crop_left || i_crop_right || i_crop_top || i_crop_bottom )
615 tk->fmt.video.i_visible_width = tk->fmt.video.i_width;
616 tk->fmt.video.i_visible_height = tk->fmt.video.i_height;
617 tk->fmt.video.i_x_offset = i_crop_left;
618 tk->fmt.video.i_y_offset = i_crop_top;
619 tk->fmt.video.i_visible_width -= i_crop_left + i_crop_right;
620 tk->fmt.video.i_visible_height -= i_crop_top + i_crop_bottom;
622 /* FIXME: i_display_* allows you to not only set DAR, but also a zoom factor.
623 we do not support this atm */
625 else if( MKV_IS_ID( l, KaxTrackAudio ) )
627 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
629 /* Initialize default values */
630 tk->fmt.audio.i_channels = 1;
631 tk->fmt.audio.i_rate = 8000;
633 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
635 for( unsigned int j = 0; j < tka->ListSize(); j++ )
637 EbmlElement *l = (*tka)[j];
639 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
641 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
643 tk->i_original_rate = tk->fmt.audio.i_rate = (int)float( afreq );
644 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
646 else if( MKV_IS_ID( l, KaxAudioOutputSamplingFreq ) )
648 KaxAudioOutputSamplingFreq &afreq = *(KaxAudioOutputSamplingFreq*)l;
650 tk->fmt.audio.i_rate = (int)float( afreq );
651 msg_Dbg( &sys.demuxer, "| | | | + aoutfreq=%d", tk->fmt.audio.i_rate );
653 else if( MKV_IS_ID( l, KaxAudioChannels ) )
655 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
657 tk->fmt.audio.i_channels = uint8( achan );
658 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
660 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
662 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
664 tk->fmt.audio.i_bitspersample = uint8( abits );
665 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
669 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
675 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
683 if( tk->i_compression_type == MATROSKA_COMPRESSION_ZLIB &&
684 tk->i_encoding_scope & MATROSKA_ENCODING_SCOPE_PRIVATE &&
685 tk->i_extra_data && tk->p_extra_data &&
686 zlib_decompress_extra( &sys.demuxer, tk) )
689 if( TrackInit( tk ) )
691 msg_Err(&sys.demuxer, "Couldn't init track %d", tk->i_number );
692 free(tk->p_extra_data);
697 tracks.push_back( tk );
701 msg_Err( &sys.demuxer, "Track Entry %d not supported", tk->i_number );
702 free(tk->p_extra_data);
707 /*****************************************************************************
709 *****************************************************************************/
710 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
713 int i_upper_level = 0;
715 /* Master elements */
716 tracks->Read( es, EBML_CONTEXT(tracks), i_upper_level, el, true );
718 for( size_t i = 0; i < tracks->ListSize(); i++ )
720 EbmlElement *l = (*tracks)[i];
722 if( MKV_IS_ID( l, KaxTrackEntry ) )
724 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
728 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
733 /*****************************************************************************
735 *****************************************************************************/
736 void matroska_segment_c::ParseInfo( KaxInfo *info )
740 int i_upper_level = 0;
742 /* Master elements */
743 m = static_cast<EbmlMaster *>(info);
744 m->Read( es, EBML_CONTEXT(info), i_upper_level, el, true );
746 for( size_t i = 0; i < m->ListSize(); i++ )
748 EbmlElement *l = (*m)[i];
750 if( MKV_IS_ID( l, KaxSegmentUID ) )
752 if ( p_segment_uid == NULL )
753 p_segment_uid = new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l));
755 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)p_segment_uid->GetBuffer() );
757 else if( MKV_IS_ID( l, KaxPrevUID ) )
759 if ( p_prev_segment_uid == NULL )
761 p_prev_segment_uid = new KaxPrevUID(*static_cast<KaxPrevUID*>(l));
762 b_ref_external_segments = true;
765 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)p_prev_segment_uid->GetBuffer() );
767 else if( MKV_IS_ID( l, KaxNextUID ) )
769 if ( p_next_segment_uid == NULL )
771 p_next_segment_uid = new KaxNextUID(*static_cast<KaxNextUID*>(l));
772 b_ref_external_segments = true;
775 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)p_next_segment_uid->GetBuffer() );
777 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
779 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
781 i_timescale = uint64(tcs);
783 msg_Dbg( &sys.demuxer, "| | + TimecodeScale=%"PRId64,
786 else if( MKV_IS_ID( l, KaxDuration ) )
788 KaxDuration &dur = *(KaxDuration*)l;
790 i_duration = mtime_t( double( dur ) );
792 msg_Dbg( &sys.demuxer, "| | + Duration=%"PRId64,
795 else if( MKV_IS_ID( l, KaxMuxingApp ) )
797 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
799 psz_muxing_application = ToUTF8( UTFstring( mapp ) );
801 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
802 psz_muxing_application );
804 else if( MKV_IS_ID( l, KaxWritingApp ) )
806 KaxWritingApp &wapp = *(KaxWritingApp*)l;
808 psz_writing_application = ToUTF8( UTFstring( wapp ) );
810 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
811 psz_writing_application );
813 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
815 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
817 psz_segment_filename = ToUTF8( UTFstring( sfn ) );
819 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
820 psz_segment_filename );
822 else if( MKV_IS_ID( l, KaxTitle ) )
824 KaxTitle &title = *(KaxTitle*)l;
826 psz_title = ToUTF8( UTFstring( title ) );
828 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
830 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
832 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
834 families.push_back( new KaxSegmentFamily(*uid) );
836 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
838 else if( MKV_IS_ID( l, KaxDateUTC ) )
840 KaxDateUTC &date = *(KaxDateUTC*)l;
845 i_date = date.GetEpochDate();
846 if( gmtime_r( &i_date, &tmres ) &&
847 strftime( buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y",
850 psz_date_utc = strdup( buffer );
851 msg_Dbg( &sys.demuxer, "| | + Date=%s", buffer );
854 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
856 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
857 chapter_translation_c *p_translate = new chapter_translation_c();
859 p_trans->Read( es, EBML_CONTEXT(p_trans), i_upper_level, el, true );
860 for( size_t j = 0; j < p_trans->ListSize(); j++ )
862 EbmlElement *l = (*p_trans)[j];
864 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
866 p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
868 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
870 p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
872 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
874 p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
878 translations.push_back( p_translate );
882 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
886 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
887 i_duration = mtime_t(f_dur);
888 if( !i_duration ) i_duration = -1;
892 /*****************************************************************************
894 *****************************************************************************/
895 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
897 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
898 for( size_t i = 0; i < ca->ListSize(); i++ )
900 EbmlElement *l = (*ca)[i];
902 if( MKV_IS_ID( l, KaxChapterUID ) )
904 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
905 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %"PRIu64"", chapters.i_uid );
907 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
909 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
910 chapters.b_display_seekpoint = uint8( flag ) == 0;
912 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
914 else if( MKV_IS_ID( l, KaxChapterSegmentUID ) )
916 chapters.p_segment_uid = new KaxChapterSegmentUID( *static_cast<KaxChapterSegmentUID*>(l) );
917 b_ref_external_segments = true;
918 msg_Dbg( &sys.demuxer, "| | | | + ChapterSegmentUID= %u", *(uint32*)chapters.p_segment_uid->GetBuffer() );
920 else if( MKV_IS_ID( l, KaxChapterSegmentEditionUID ) )
922 chapters.p_segment_edition_uid = new KaxChapterSegmentEditionUID( *static_cast<KaxChapterSegmentEditionUID*>(l) );
923 msg_Dbg( &sys.demuxer, "| | | | + ChapterSegmentEditionUID= %u",
924 #if LIBMATROSKA_VERSION < 0x010300
925 *(uint32*)chapters.p_segment_edition_uid->GetBuffer()
927 *(uint32*)chapters.p_segment_edition_uid
931 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
933 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
934 chapters.i_start_time = uint64( start ) / INT64_C(1000);
936 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %"PRId64"", chapters.i_start_time );
938 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
940 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
941 chapters.i_end_time = uint64( end ) / INT64_C(1000);
943 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %"PRId64"", chapters.i_end_time );
945 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
947 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
949 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
950 for( size_t j = 0; j < cd->ListSize(); j++ )
952 EbmlElement *l= (*cd)[j];
954 if( MKV_IS_ID( l, KaxChapterString ) )
956 KaxChapterString &name =*(KaxChapterString*)l;
957 for ( int k = 0; k < i_level; k++)
958 chapters.psz_name += '+';
959 chapters.psz_name += ' ';
960 char *psz_tmp_utf8 = ToUTF8( UTFstring( name ) );
961 chapters.psz_name += psz_tmp_utf8;
962 chapters.b_user_display = true;
964 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", psz_tmp_utf8 );
965 free( psz_tmp_utf8 );
967 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
969 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
970 const char *psz = string( lang ).c_str();
972 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
974 else if( MKV_IS_ID( l, KaxChapterCountry ) )
976 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
977 const char *psz = string( ct ).c_str();
979 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
983 else if( MKV_IS_ID( l, KaxChapterProcess ) )
985 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
987 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
988 chapter_codec_cmds_c *p_ccodec = NULL;
990 for( size_t j = 0; j < cp->ListSize(); j++ )
992 EbmlElement *k= (*cp)[j];
994 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
996 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
997 if ( uint32(*p_codec_id) == 0 )
998 p_ccodec = new matroska_script_codec_c( sys );
999 else if ( uint32(*p_codec_id) == 1 )
1000 p_ccodec = new dvd_chapter_codec_c( sys );
1005 if ( p_ccodec != NULL )
1007 for( size_t j = 0; j < cp->ListSize(); j++ )
1009 EbmlElement *k= (*cp)[j];
1011 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
1013 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
1014 p_ccodec->SetPrivate( *p_private );
1016 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
1018 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
1021 chapters.codecs.push_back( p_ccodec );
1024 else if( MKV_IS_ID( l, KaxChapterAtom ) )
1026 chapter_item_c *new_sub_chapter = new chapter_item_c();
1027 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
1028 new_sub_chapter->p_parent = &chapters;
1029 chapters.sub_chapters.push_back( new_sub_chapter );
1034 /*****************************************************************************
1036 *****************************************************************************/
1037 void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
1040 int i_upper_level = 0;
1042 attachments->Read( es, EBML_CONTEXT(attachments), i_upper_level, el, true );
1044 KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
1046 while( attachedFile && ( attachedFile->GetSize() > 0 ) )
1048 KaxFileData &img_data = GetChild<KaxFileData>( *attachedFile );
1049 char *psz_tmp_utf8 = ToUTF8( UTFstring( GetChild<KaxFileName>( *attachedFile ) ) );
1050 std::string attached_filename(psz_tmp_utf8);
1052 attachment_c *new_attachment = new attachment_c( attached_filename,
1053 GetChild<KaxMimeType>( *attachedFile ),
1054 img_data.GetSize() );
1056 msg_Dbg( &sys.demuxer, "| | - %s (%s)", new_attachment->fileName(), new_attachment->mimeType() );
1058 if( new_attachment->init() )
1060 memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
1061 sys.stored_attachments.push_back( new_attachment );
1062 if( !strncmp( new_attachment->mimeType(), "image/", 6 ) )
1065 if( asprintf( &psz_url, "attachment://%s",
1066 new_attachment->fileName() ) == -1 )
1069 sys.meta = vlc_meta_New();
1070 vlc_meta_SetArtURL( sys.meta, psz_url );
1076 delete new_attachment;
1079 attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
1083 /*****************************************************************************
1085 *****************************************************************************/
1086 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
1089 int i_upper_level = 0;
1091 /* Master elements */
1092 chapters->Read( es, EBML_CONTEXT(chapters), i_upper_level, el, true );
1094 for( size_t i = 0; i < chapters->ListSize(); i++ )
1096 EbmlElement *l = (*chapters)[i];
1098 if( MKV_IS_ID( l, KaxEditionEntry ) )
1100 chapter_edition_c *p_edition = new chapter_edition_c();
1102 EbmlMaster *E = static_cast<EbmlMaster *>(l );
1103 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
1104 for( size_t j = 0; j < E->ListSize(); j++ )
1106 EbmlElement *l = (*E)[j];
1108 if( MKV_IS_ID( l, KaxChapterAtom ) )
1110 chapter_item_c *new_sub_chapter = new chapter_item_c();
1111 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
1112 p_edition->sub_chapters.push_back( new_sub_chapter );
1114 else if( MKV_IS_ID( l, KaxEditionUID ) )
1116 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
1118 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
1120 p_edition->b_ordered = var_InheritBool( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
1122 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
1124 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
1125 i_default_edition = stored_editions.size();
1127 else if( MKV_IS_ID( l, KaxEditionFlagHidden ) )
1129 // FIXME to implement
1133 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
1136 stored_editions.push_back( p_edition );
1140 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
1145 void matroska_segment_c::ParseCluster( bool b_update_start_time )
1149 int i_upper_level = 0;
1151 /* Master elements */
1152 m = static_cast<EbmlMaster *>( cluster );
1153 m->Read( es, EBML_CONTEXT(cluster), i_upper_level, el, true );
1155 for( unsigned int i = 0; i < m->ListSize(); i++ )
1157 EbmlElement *l = (*m)[i];
1159 if( MKV_IS_ID( l, KaxClusterTimecode ) )
1161 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
1163 cluster->InitTimecode( uint64( ctc ), i_timescale );
1168 if( b_update_start_time )
1169 i_start_time = cluster->GlobalTimecode() / 1000;
1173 int32_t matroska_segment_c::TrackInit( mkv_track_t * p_tk )
1175 es_format_t *p_fmt = &p_tk->fmt;
1176 if( !strcmp( p_tk->psz_codec, "V_MS/VFW/FOURCC" ) )
1178 if( p_tk->i_extra_data < (int)sizeof( VLC_BITMAPINFOHEADER ) )
1180 msg_Err( &sys.demuxer, "missing/invalid VLC_BITMAPINFOHEADER" );
1181 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1185 VLC_BITMAPINFOHEADER *p_bih = (VLC_BITMAPINFOHEADER*)p_tk->p_extra_data;
1187 p_tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
1188 p_tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
1189 p_tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
1191 p_tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( VLC_BITMAPINFOHEADER );
1192 if( p_tk->fmt.i_extra > 0 )
1194 /* Very unlikely yet possible: bug #5659*/
1195 size_t maxlen = p_tk->i_extra_data - sizeof( VLC_BITMAPINFOHEADER );
1196 p_tk->fmt.i_extra = ( (unsigned)p_tk->fmt.i_extra < maxlen )?
1197 p_tk->fmt.i_extra : maxlen;
1199 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1200 memcpy( p_tk->fmt.p_extra, &p_bih[1], p_tk->fmt.i_extra );
1203 p_tk->b_dts_only = true;
1205 else if( !strcmp( p_tk->psz_codec, "V_MPEG1" ) ||
1206 !strcmp( p_tk->psz_codec, "V_MPEG2" ) )
1208 p_tk->fmt.i_codec = VLC_CODEC_MPGV;
1209 if( p_tk->i_extra_data )
1210 fill_extra_data( p_tk, 0 );
1212 else if( !strncmp( p_tk->psz_codec, "V_THEORA", 8 ) )
1214 p_tk->fmt.i_codec = VLC_CODEC_THEORA;
1215 fill_extra_data( p_tk, 0 );
1216 p_tk->b_pts_only = true;
1218 else if( !strncmp( p_tk->psz_codec, "V_REAL/RV", 9 ) )
1220 uint8_t *p = p_tk->p_extra_data;
1222 if( !strcmp( p_tk->psz_codec, "V_REAL/RV10" ) )
1223 p_fmt->i_codec = VLC_CODEC_RV10;
1224 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV20" ) )
1225 p_fmt->i_codec = VLC_CODEC_RV20;
1226 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV30" ) )
1227 p_fmt->i_codec = VLC_CODEC_RV30;
1228 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV40" ) )
1229 p_fmt->i_codec = VLC_CODEC_RV40;
1231 /* Extract the framerate from the header */
1232 if( p_tk->i_extra_data >= 26 &&
1233 p[4] == 'V' && p[5] == 'I' && p[6] == 'D' && p[7] == 'O' &&
1234 p[8] == 'R' && p[9] == 'V' &&
1235 (p[10] == '3' || p[10] == '4') && p[11] == '0' )
1237 p_tk->fmt.video.i_frame_rate =
1238 p[22] << 24 | p[23] << 16 | p[24] << 8 | p[25] << 0;
1239 p_tk->fmt.video.i_frame_rate_base = 65536;
1242 fill_extra_data( p_tk, 26 );
1243 p_tk->b_dts_only = true;
1245 else if( !strncmp( p_tk->psz_codec, "V_DIRAC", 7 ) )
1247 p_tk->fmt.i_codec = VLC_CODEC_DIRAC;
1249 else if( !strncmp( p_tk->psz_codec, "V_VP8", 5 ) )
1251 p_tk->fmt.i_codec = VLC_CODEC_VP8;
1252 p_tk->b_pts_only = true;
1254 else if( !strncmp( p_tk->psz_codec, "V_MPEG4", 7 ) )
1256 if( !strcmp( p_tk->psz_codec, "V_MPEG4/MS/V3" ) )
1258 p_tk->fmt.i_codec = VLC_CODEC_DIV3;
1260 else if( !strncmp( p_tk->psz_codec, "V_MPEG4/ISO", 11 ) )
1262 /* A MPEG 4 codec, SP, ASP, AP or AVC */
1263 if( !strcmp( p_tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
1264 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
1266 p_tk->fmt.i_codec = VLC_CODEC_MP4V;
1267 fill_extra_data( p_tk, 0 );
1270 else if( !strcmp( p_tk->psz_codec, "V_QUICKTIME" ) )
1272 MP4_Box_t *p_box = (MP4_Box_t*)xmalloc( sizeof( MP4_Box_t ) );
1273 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1277 if( MP4_ReadBoxCommon( p_mp4_stream, p_box ) &&
1278 MP4_ReadBox_sample_vide( p_mp4_stream, p_box ) )
1280 p_tk->fmt.i_codec = p_box->i_type;
1281 p_tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
1282 p_tk->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
1283 p_tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
1284 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1285 memcpy( p_tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, p_tk->fmt.i_extra );
1286 MP4_FreeBox_sample_vide( p_box );
1292 stream_Delete( p_mp4_stream );
1294 else if( !strcmp( p_tk->psz_codec, "V_MJPEG" ) )
1296 p_tk->fmt.i_codec = VLC_CODEC_MJPG;
1298 else if( !strcmp( p_tk->psz_codec, "A_MS/ACM" ) )
1300 if( p_tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1302 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
1303 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1307 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)p_tk->p_extra_data;
1309 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_tk->fmt.i_codec, NULL );
1311 if( p_tk->fmt.i_codec == VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
1312 msg_Err( &sys.demuxer, "Unrecognized wf tag: 0x%x", GetWLE( &p_wf->wFormatTag ) );
1313 p_tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
1314 p_tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
1315 p_tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
1316 p_tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
1317 p_tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
1319 p_tk->fmt.i_extra = GetWLE( &p_wf->cbSize );
1320 if( p_tk->fmt.i_extra > 0 )
1322 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1323 memcpy( p_tk->fmt.p_extra, &p_wf[1], p_tk->fmt.i_extra );
1327 else if( !strcmp( p_tk->psz_codec, "A_MPEG/L3" ) ||
1328 !strcmp( p_tk->psz_codec, "A_MPEG/L2" ) ||
1329 !strcmp( p_tk->psz_codec, "A_MPEG/L1" ) )
1331 p_tk->fmt.i_codec = VLC_CODEC_MPGA;
1333 else if( !strcmp( p_tk->psz_codec, "A_AC3" ) )
1335 p_tk->fmt.i_codec = VLC_CODEC_A52;
1337 else if( !strcmp( p_tk->psz_codec, "A_EAC3" ) )
1339 p_tk->fmt.i_codec = VLC_CODEC_EAC3;
1341 else if( !strcmp( p_tk->psz_codec, "A_DTS" ) )
1343 p_tk->fmt.i_codec = VLC_CODEC_DTS;
1345 else if( !strcmp( p_tk->psz_codec, "A_MLP" ) )
1347 p_tk->fmt.i_codec = VLC_CODEC_MLP;
1349 else if( !strcmp( p_tk->psz_codec, "A_TRUEHD" ) )
1351 /* FIXME when more samples arrive */
1352 p_tk->fmt.i_codec = VLC_CODEC_TRUEHD;
1353 p_fmt->b_packetized = false;
1355 else if( !strcmp( p_tk->psz_codec, "A_FLAC" ) )
1357 p_tk->fmt.i_codec = VLC_CODEC_FLAC;
1358 fill_extra_data( p_tk, 0 );
1360 else if( !strcmp( p_tk->psz_codec, "A_VORBIS" ) )
1362 p_tk->fmt.i_codec = VLC_CODEC_VORBIS;
1363 fill_extra_data( p_tk, 0 );
1365 else if( !strncmp( p_tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1366 !strncmp( p_tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1368 int i_profile, i_srate, sbr = 0;
1369 static const unsigned int i_sample_rates[] =
1371 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1372 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1375 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1376 /* create data for faad (MP4DecSpecificDescrTag)*/
1378 if( !strcmp( &p_tk->psz_codec[12], "MAIN" ) )
1382 else if( !strcmp( &p_tk->psz_codec[12], "LC" ) )
1386 else if( !strcmp( &p_tk->psz_codec[12], "SSR" ) )
1390 else if( !strcmp( &p_tk->psz_codec[12], "LC/SBR" ) )
1400 for( i_srate = 0; i_srate < 13; i_srate++ )
1402 if( i_sample_rates[i_srate] == p_tk->i_original_rate )
1407 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
1409 p_tk->fmt.i_extra = sbr ? 5 : 2;
1410 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1411 ((uint8_t*)p_tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1412 ((uint8_t*)p_tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (p_tk->fmt.audio.i_channels << 3);
1415 int syncExtensionType = 0x2B7;
1417 for (iDSRI=0; iDSRI<13; iDSRI++)
1418 if( i_sample_rates[iDSRI] == p_tk->fmt.audio.i_rate )
1420 ((uint8_t*)p_tk->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
1421 ((uint8_t*)p_tk->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
1422 ((uint8_t*)p_tk->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
1425 else if( !strcmp( p_tk->psz_codec, "A_AAC" ) )
1427 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1428 fill_extra_data( p_tk, 0 );
1430 else if( !strcmp( p_tk->psz_codec, "A_WAVPACK4" ) )
1432 p_tk->fmt.i_codec = VLC_CODEC_WAVPACK;
1433 fill_extra_data( p_tk, 0 );
1435 else if( !strcmp( p_tk->psz_codec, "A_TTA1" ) )
1437 p_fmt->i_codec = VLC_CODEC_TTA;
1438 if( p_tk->i_extra_data > 0 )
1440 fill_extra_data( p_tk, 0 );
1444 p_fmt->i_extra = 30;
1445 p_fmt->p_extra = xmalloc( p_fmt->i_extra );
1446 uint8_t *p_extra = (uint8_t*)p_fmt->p_extra;
1447 memcpy( &p_extra[ 0], "TTA1", 4 );
1448 SetWLE( &p_extra[ 4], 1 );
1449 SetWLE( &p_extra[ 6], p_fmt->audio.i_channels );
1450 SetWLE( &p_extra[ 8], p_fmt->audio.i_bitspersample );
1451 SetDWLE( &p_extra[10], p_fmt->audio.i_rate );
1452 SetDWLE( &p_extra[14], 0xffffffff );
1453 memset( &p_extra[18], 0, 30 - 18 );
1456 else if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) ||
1457 !strcmp( p_tk->psz_codec, "A_PCM/INT/LIT" ) ||
1458 !strcmp( p_tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
1460 if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) )
1462 p_tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1466 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1468 p_tk->fmt.audio.i_blockalign = ( p_tk->fmt.audio.i_bitspersample + 7 ) / 8 * p_tk->fmt.audio.i_channels;
1470 else if( !strncmp( p_tk->psz_codec, "A_REAL/", 7 ) )
1472 if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
1474 p_fmt->i_codec = VLC_CODEC_RA_144;
1475 p_fmt->audio.i_channels = 1;
1476 p_fmt->audio.i_rate = 8000;
1477 p_fmt->audio.i_blockalign = 0x14;
1479 else if( p_tk->i_extra_data > 28 )
1481 uint8_t *p = p_tk->p_extra_data;
1482 if( memcmp( p, ".ra", 3 ) ) {
1483 msg_Err( &sys.demuxer, "Invalid Real ExtraData 0x%4.4s", (char *)p );
1484 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1488 real_audio_private * priv = (real_audio_private*) p_tk->p_extra_data;
1489 if( !strcmp( p_tk->psz_codec, "A_REAL/COOK" ) )
1491 p_tk->fmt.i_codec = VLC_CODEC_COOK;
1492 p_tk->fmt.audio.i_blockalign = hton16(priv->sub_packet_size);
1494 else if( !strcmp( p_tk->psz_codec, "A_REAL/ATRC" ) )
1495 p_tk->fmt.i_codec = VLC_CODEC_ATRAC3;
1496 else if( !strcmp( p_tk->psz_codec, "A_REAL/28_8" ) )
1497 p_tk->fmt.i_codec = VLC_CODEC_RA_288;
1498 /* FIXME RALF and SIPR */
1499 uint16_t version = (uint16_t) hton16(priv->version);
1501 new Cook_PrivateTrackData( hton16(priv->sub_packet_h),
1502 hton16(priv->frame_size),
1503 hton16(priv->sub_packet_size));
1504 if( unlikely( !p_tk->p_sys ) )
1507 if( unlikely( p_tk->p_sys->Init() ) )
1512 real_audio_private_v4 * v4 = (real_audio_private_v4*) priv;
1513 p_tk->fmt.audio.i_channels = hton16(v4->channels);
1514 p_tk->fmt.audio.i_bitspersample = hton16(v4->sample_size);
1515 p_tk->fmt.audio.i_rate = hton16(v4->sample_rate);
1517 else if( version == 5 )
1519 real_audio_private_v5 * v5 = (real_audio_private_v5*) priv;
1520 p_tk->fmt.audio.i_channels = hton16(v5->channels);
1521 p_tk->fmt.audio.i_bitspersample = hton16(v5->sample_size);
1522 p_tk->fmt.audio.i_rate = hton16(v5->sample_rate);
1524 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);
1526 fill_extra_data( p_tk, p_tk->fmt.i_codec == VLC_CODEC_RA_288 ? 0 : 78);
1530 else if( !strcmp( p_tk->psz_codec, "S_KATE" ) )
1532 p_tk->fmt.i_codec = VLC_CODEC_KATE;
1533 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1535 fill_extra_data( p_tk, 0 );
1537 else if( !strcmp( p_tk->psz_codec, "S_TEXT/ASCII" ) )
1539 p_fmt->i_codec = VLC_CODEC_SUBT;
1540 p_fmt->subs.psz_encoding = strdup( "ASCII" );
1542 else if( !strcmp( p_tk->psz_codec, "S_TEXT/UTF8" ) )
1544 p_tk->fmt.i_codec = VLC_CODEC_SUBT;
1545 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1547 else if( !strcmp( p_tk->psz_codec, "S_TEXT/USF" ) )
1549 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
1550 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1551 if( p_tk->i_extra_data )
1552 fill_extra_data( p_tk, 0 );
1554 else if( !strcmp( p_tk->psz_codec, "S_TEXT/SSA" ) ||
1555 !strcmp( p_tk->psz_codec, "S_TEXT/ASS" ) ||
1556 !strcmp( p_tk->psz_codec, "S_SSA" ) ||
1557 !strcmp( p_tk->psz_codec, "S_ASS" ))
1559 p_tk->fmt.i_codec = VLC_CODEC_SSA;
1560 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1561 if( p_tk->i_extra_data )
1562 fill_extra_data( p_tk, 0 );
1564 else if( !strcmp( p_tk->psz_codec, "S_VOBSUB" ) )
1566 p_tk->fmt.i_codec = VLC_CODEC_SPU;
1567 if( p_tk->i_extra_data )
1570 char *psz_buf = (char *)malloc( p_tk->i_extra_data + 1);
1571 if( psz_buf != NULL )
1573 memcpy( psz_buf, p_tk->p_extra_data , p_tk->i_extra_data );
1574 psz_buf[p_tk->i_extra_data] = '\0';
1576 psz_start = strstr( psz_buf, "size:" );
1578 vobsub_size_parse( psz_start,
1579 &p_tk->fmt.subs.spu.i_original_frame_width,
1580 &p_tk->fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
1582 msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d",
1583 p_tk->fmt.subs.spu.i_original_frame_width,
1584 p_tk->fmt.subs.spu.i_original_frame_height );
1588 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1592 psz_start = strstr( psz_buf, "palette:" );
1594 vobsub_palette_parse( psz_start, &p_tk->fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
1596 p_tk->fmt.subs.spu.palette[0] = 0xBeef;
1597 msg_Dbg( &sys.demuxer, "vobsub palette read" );
1601 msg_Warn( &sys.demuxer, "reading original palette failed" );
1607 else if( !strcmp( p_tk->psz_codec, "S_HDMV/PGS" ) )
1609 p_tk->fmt.i_codec = VLC_CODEC_BD_PG;
1611 else if( !strcmp( p_tk->psz_codec, "B_VOBBTN" ) )
1613 p_tk->fmt.i_cat = NAV_ES;
1617 msg_Err( &sys.demuxer, "unknown codec id=`%s'", p_tk->psz_codec );
1618 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );