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 );
197 tk->fmt.psz_language = strdup("English");
198 tk->fmt.psz_description = NULL;
200 tk->b_default = true;
201 tk->b_enabled = true;
202 tk->b_forced = false;
203 tk->b_silent = false;
204 tk->i_number = tracks.size() - 1;
205 tk->i_extra_data = 0;
206 tk->p_extra_data = NULL;
207 tk->psz_codec = NULL;
208 tk->b_dts_only = false;
209 tk->i_default_duration = 0;
210 tk->f_timecodescale = 1.0;
212 tk->b_inited = false;
214 tk->p_data_init = NULL;
216 tk->psz_codec_name = NULL;
217 tk->psz_codec_settings = NULL;
218 tk->psz_codec_info_url = NULL;
219 tk->psz_codec_download_url = NULL;
221 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
222 tk->i_encoding_scope = MATROSKA_ENCODING_SCOPE_ALL_FRAMES;
223 tk->p_compression_data = NULL;
225 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
227 for( size_t i = 0; i < m->ListSize(); i++ )
229 EbmlElement *l = (*m)[i];
231 if( MKV_IS_ID( l, KaxTrackNumber ) )
233 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
235 tk->i_number = uint32( tnum );
236 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
238 else if( MKV_IS_ID( l, KaxTrackUID ) )
240 KaxTrackUID &tuid = *(KaxTrackUID*)l;
242 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
244 else if( MKV_IS_ID( l, KaxTrackType ) )
246 const char *psz_type;
247 KaxTrackType &ttype = *(KaxTrackType*)l;
249 switch( uint8(ttype) )
253 tk->fmt.i_cat = AUDIO_ES;
254 tk->fmt.audio.i_channels = 1;
255 tk->fmt.audio.i_rate = 8000;
259 tk->fmt.i_cat = VIDEO_ES;
262 psz_type = "subtitle";
263 tk->fmt.i_cat = SPU_ES;
266 psz_type = "buttons";
267 tk->fmt.i_cat = SPU_ES;
270 psz_type = "unknown";
271 tk->fmt.i_cat = UNKNOWN_ES;
275 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
277 else if( MKV_IS_ID( l, KaxTrackFlagEnabled ) ) // UNUSED
279 KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
281 tk->b_enabled = uint32( fenb );
282 msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u", uint32( fenb ) );
284 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
286 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
288 tk->b_default = uint32( fdef );
289 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
291 else if( MKV_IS_ID( l, KaxTrackFlagForced ) ) // UNUSED
293 KaxTrackFlagForced &ffor = *(KaxTrackFlagForced*)l;
294 tk->b_forced = uint32( ffor );
296 msg_Dbg( &sys.demuxer, "| | | + Track Forced=%u", uint32( ffor ) );
298 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) ) // UNUSED
300 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
302 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
304 else if( MKV_IS_ID( l, KaxTrackMinCache ) ) // UNUSED
306 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
308 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
310 else if( MKV_IS_ID( l, KaxTrackMaxCache ) ) // UNUSED
312 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
314 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
316 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
318 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
320 tk->i_default_duration = uint64(defd);
321 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration=%"PRId64, uint64(defd) );
323 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
325 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
327 tk->f_timecodescale = float( ttcs );
328 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
330 else if( MKV_IS_ID( l, KaxMaxBlockAdditionID ) ) // UNUSED
332 KaxMaxBlockAdditionID &mbl = *(KaxMaxBlockAdditionID*)l;
334 msg_Dbg( &sys.demuxer, "| | | + Track Max BlockAdditionID=%d", uint32( mbl ) );
336 else if( MKV_IS_ID( l, KaxTrackName ) )
338 KaxTrackName &tname = *(KaxTrackName*)l;
340 tk->fmt.psz_description = ToUTF8( UTFstring( tname ) );
341 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
343 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
345 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
347 free( tk->fmt.psz_language );
348 tk->fmt.psz_language = strdup( string( lang ).c_str() );
349 msg_Dbg( &sys.demuxer,
350 "| | | + Track Language=`%s'", tk->fmt.psz_language );
352 else if( MKV_IS_ID( l, KaxCodecID ) )
354 KaxCodecID &codecid = *(KaxCodecID*)l;
356 tk->psz_codec = strdup( string( codecid ).c_str() );
357 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
359 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
361 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
363 tk->i_extra_data = cpriv.GetSize();
364 if( tk->i_extra_data > 0 )
366 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
367 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
369 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size=%"PRId64, cpriv.GetSize() );
371 else if( MKV_IS_ID( l, KaxCodecName ) )
373 KaxCodecName &cname = *(KaxCodecName*)l;
375 tk->psz_codec_name = ToUTF8( UTFstring( cname ) );
376 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
379 else if( MKV_IS_ID( l, KaxCodecDecodeAll ) ) // UNUSED
381 KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
383 msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u", uint8( cdall ) );
385 else if( MKV_IS_ID( l, KaxTrackOverlay ) ) // UNUSED
387 KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
389 msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u", uint32( tovr ) );
391 else if( MKV_IS_ID( l, KaxContentEncodings ) )
393 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
394 MkvTree( sys.demuxer, 3, "Content Encodings" );
395 if ( cencs->ListSize() > 1 )
397 msg_Err( &sys.demuxer, "Multiple Compression method not supported" );
400 for( size_t j = 0; j < cencs->ListSize(); j++ )
402 EbmlElement *l2 = (*cencs)[j];
403 if( MKV_IS_ID( l2, KaxContentEncoding ) )
405 MkvTree( sys.demuxer, 4, "Content Encoding" );
406 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
407 for( size_t k = 0; k < cenc->ListSize(); k++ )
409 EbmlElement *l3 = (*cenc)[k];
410 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
412 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
413 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
415 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
417 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
418 tk->i_encoding_scope = uint32( encscope );
419 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
421 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
423 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
424 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
426 else if( MKV_IS_ID( l3, KaxContentCompression ) )
428 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
429 MkvTree( sys.demuxer, 5, "Content Compression" );
430 //Default compression type is 0 (Zlib)
431 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
432 for( size_t n = 0; n < compr->ListSize(); n++ )
434 EbmlElement *l4 = (*compr)[n];
435 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
437 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
438 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
439 tk->i_compression_type = uint32( compalg );
440 if ( ( tk->i_compression_type != MATROSKA_COMPRESSION_ZLIB ) &&
441 ( tk->i_compression_type != MATROSKA_COMPRESSION_HEADER ) )
443 msg_Err( &sys.demuxer, "Track Compression method %d not supported", tk->i_compression_type );
447 else if( MKV_IS_ID( l4, KaxContentCompSettings ) )
449 tk->p_compression_data = new KaxContentCompSettings( *(KaxContentCompSettings*)l4 );
453 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
457 // ContentEncryption Unsupported
460 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
466 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
470 // else if( MKV_IS_ID( l, KaxCodecSettings) ) DEPRECATED by matroska
472 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
474 // tk->psz_codec_settings = ToUTF8( UTFstring( cset ) );
475 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
477 // else if( MKV_IS_ID( l, KaxCodecInfoURL) ) DEPRECATED by matroska
479 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
481 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
482 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
484 // else if( MKV_IS_ID( l, KaxCodecDownloadURL) ) DEPRECATED by matroska
486 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
488 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
489 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
491 else if( MKV_IS_ID( l, KaxTrackVideo ) )
493 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
494 unsigned int i_crop_right = 0, i_crop_left = 0, i_crop_top = 0, i_crop_bottom = 0;
495 unsigned int i_display_unit = 0, i_display_width = 0, i_display_height = 0;
497 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
500 tk->fmt.video.i_frame_rate_base = (unsigned int)(tk->i_default_duration / 1000);
501 tk->fmt.video.i_frame_rate = 1000000;
503 for( unsigned int j = 0; j < tkv->ListSize(); j++ )
505 EbmlElement *l = (*tkv)[j];
506 if( MKV_IS_ID( l, KaxVideoFlagInterlaced ) ) // UNUSED
508 KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)l;
510 msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
512 else if( MKV_IS_ID( l, KaxVideoStereoMode ) ) // UNUSED
514 KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)l;
516 msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
518 else if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
520 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
522 tk->fmt.video.i_width += uint16( vwidth );
523 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
525 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
527 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
529 tk->fmt.video.i_height += uint16( vheight );
530 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
532 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
534 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
536 i_display_width = uint16( vwidth );
537 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
539 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
541 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
543 i_display_height = uint16( vheight );
544 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
546 else if( MKV_IS_ID( l, KaxVideoPixelCropBottom ) )
548 KaxVideoPixelCropBottom &cropval = *(KaxVideoPixelCropBottom*)l;
550 i_crop_bottom = uint16( cropval );
551 msg_Dbg( &sys.demuxer, "| | | | + crop pixel bottom=%d", uint16( cropval ) );
553 else if( MKV_IS_ID( l, KaxVideoPixelCropTop ) )
555 KaxVideoPixelCropTop &cropval = *(KaxVideoPixelCropTop*)l;
557 i_crop_top = uint16( cropval );
558 msg_Dbg( &sys.demuxer, "| | | | + crop pixel top=%d", uint16( cropval ) );
560 else if( MKV_IS_ID( l, KaxVideoPixelCropRight ) )
562 KaxVideoPixelCropRight &cropval = *(KaxVideoPixelCropRight*)l;
564 i_crop_right = uint16( cropval );
565 msg_Dbg( &sys.demuxer, "| | | | + crop pixel right=%d", uint16( cropval ) );
567 else if( MKV_IS_ID( l, KaxVideoPixelCropLeft ) )
569 KaxVideoPixelCropLeft &cropval = *(KaxVideoPixelCropLeft*)l;
571 i_crop_left = uint16( cropval );
572 msg_Dbg( &sys.demuxer, "| | | | + crop pixel left=%d", uint16( cropval ) );
574 else if( MKV_IS_ID( l, KaxVideoDisplayUnit ) )
576 KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
578 i_display_unit = uint8( vdmode );
579 msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
580 i_display_unit == 0 ? "pixels" : ( i_display_unit == 1 ? "centimeters": "inches" ) );
582 else if( MKV_IS_ID( l, KaxVideoAspectRatio ) ) // UNUSED
584 KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
586 msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
588 // ColourSpace UNUSED
589 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
591 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
593 tk->f_fps = float( vfps );
594 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
596 // else if( MKV_IS_ID( l, KaxVideoGamma) ) //DEPRECATED by Matroska
598 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
600 // msg_Dbg( &sys.demuxer, " | | | + gamma=%f", float( gamma ) );
604 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
607 if( i_display_height && i_display_width )
609 tk->fmt.video.i_sar_num = i_display_width * tk->fmt.video.i_height;
610 tk->fmt.video.i_sar_den = i_display_height * tk->fmt.video.i_width;
612 if( i_crop_left || i_crop_right || i_crop_top || i_crop_bottom )
614 tk->fmt.video.i_visible_width = tk->fmt.video.i_width;
615 tk->fmt.video.i_visible_height = tk->fmt.video.i_height;
616 tk->fmt.video.i_x_offset = i_crop_left;
617 tk->fmt.video.i_y_offset = i_crop_top;
618 tk->fmt.video.i_visible_width -= i_crop_left + i_crop_right;
619 tk->fmt.video.i_visible_height -= i_crop_top + i_crop_bottom;
621 /* FIXME: i_display_* allows you to not only set DAR, but also a zoom factor.
622 we do not support this atm */
624 else if( MKV_IS_ID( l, KaxTrackAudio ) )
626 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
628 /* Initialize default values */
629 tk->fmt.audio.i_channels = 1;
630 tk->fmt.audio.i_rate = 8000;
632 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
634 for( unsigned int j = 0; j < tka->ListSize(); j++ )
636 EbmlElement *l = (*tka)[j];
638 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
640 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
642 tk->i_original_rate = tk->fmt.audio.i_rate = (int)float( afreq );
643 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
645 else if( MKV_IS_ID( l, KaxAudioOutputSamplingFreq ) )
647 KaxAudioOutputSamplingFreq &afreq = *(KaxAudioOutputSamplingFreq*)l;
649 tk->fmt.audio.i_rate = (int)float( afreq );
650 msg_Dbg( &sys.demuxer, "| | | | + aoutfreq=%d", tk->fmt.audio.i_rate );
652 else if( MKV_IS_ID( l, KaxAudioChannels ) )
654 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
656 tk->fmt.audio.i_channels = uint8( achan );
657 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
659 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
661 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
663 tk->fmt.audio.i_bitspersample = uint8( abits );
664 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
668 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
674 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
682 if( tk->i_compression_type == MATROSKA_COMPRESSION_ZLIB &&
683 tk->i_encoding_scope & MATROSKA_ENCODING_SCOPE_PRIVATE &&
684 tk->i_extra_data && tk->p_extra_data &&
685 zlib_decompress_extra( &sys.demuxer, tk) )
688 if( TrackInit( tk ) )
690 msg_Err(&sys.demuxer, "Couldn't init track %d", tk->i_number );
691 free(tk->p_extra_data);
696 tracks.push_back( tk );
700 msg_Err( &sys.demuxer, "Track Entry %d not supported", tk->i_number );
701 free(tk->p_extra_data);
706 /*****************************************************************************
708 *****************************************************************************/
709 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
712 int i_upper_level = 0;
714 /* Master elements */
715 tracks->Read( es, EBML_CONTEXT(tracks), i_upper_level, el, true );
717 for( size_t i = 0; i < tracks->ListSize(); i++ )
719 EbmlElement *l = (*tracks)[i];
721 if( MKV_IS_ID( l, KaxTrackEntry ) )
723 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
727 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
732 /*****************************************************************************
734 *****************************************************************************/
735 void matroska_segment_c::ParseInfo( KaxInfo *info )
739 int i_upper_level = 0;
741 /* Master elements */
742 m = static_cast<EbmlMaster *>(info);
743 m->Read( es, EBML_CONTEXT(info), i_upper_level, el, true );
745 for( size_t i = 0; i < m->ListSize(); i++ )
747 EbmlElement *l = (*m)[i];
749 if( MKV_IS_ID( l, KaxSegmentUID ) )
751 if ( p_segment_uid == NULL )
752 p_segment_uid = new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l));
754 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)p_segment_uid->GetBuffer() );
756 else if( MKV_IS_ID( l, KaxPrevUID ) )
758 if ( p_prev_segment_uid == NULL )
760 p_prev_segment_uid = new KaxPrevUID(*static_cast<KaxPrevUID*>(l));
761 b_ref_external_segments = true;
764 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)p_prev_segment_uid->GetBuffer() );
766 else if( MKV_IS_ID( l, KaxNextUID ) )
768 if ( p_next_segment_uid == NULL )
770 p_next_segment_uid = new KaxNextUID(*static_cast<KaxNextUID*>(l));
771 b_ref_external_segments = true;
774 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)p_next_segment_uid->GetBuffer() );
776 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
778 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
780 i_timescale = uint64(tcs);
782 msg_Dbg( &sys.demuxer, "| | + TimecodeScale=%"PRId64,
785 else if( MKV_IS_ID( l, KaxDuration ) )
787 KaxDuration &dur = *(KaxDuration*)l;
789 i_duration = mtime_t( double( dur ) );
791 msg_Dbg( &sys.demuxer, "| | + Duration=%"PRId64,
794 else if( MKV_IS_ID( l, KaxMuxingApp ) )
796 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
798 psz_muxing_application = ToUTF8( UTFstring( mapp ) );
800 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
801 psz_muxing_application );
803 else if( MKV_IS_ID( l, KaxWritingApp ) )
805 KaxWritingApp &wapp = *(KaxWritingApp*)l;
807 psz_writing_application = ToUTF8( UTFstring( wapp ) );
809 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
810 psz_writing_application );
812 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
814 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
816 psz_segment_filename = ToUTF8( UTFstring( sfn ) );
818 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
819 psz_segment_filename );
821 else if( MKV_IS_ID( l, KaxTitle ) )
823 KaxTitle &title = *(KaxTitle*)l;
825 psz_title = ToUTF8( UTFstring( title ) );
827 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
829 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
831 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
833 families.push_back( new KaxSegmentFamily(*uid) );
835 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
837 else if( MKV_IS_ID( l, KaxDateUTC ) )
839 KaxDateUTC &date = *(KaxDateUTC*)l;
844 i_date = date.GetEpochDate();
845 if( gmtime_r( &i_date, &tmres ) &&
846 strftime( buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y",
849 psz_date_utc = strdup( buffer );
850 msg_Dbg( &sys.demuxer, "| | + Date=%s", buffer );
853 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
855 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
856 chapter_translation_c *p_translate = new chapter_translation_c();
858 p_trans->Read( es, EBML_CONTEXT(p_trans), i_upper_level, el, true );
859 for( size_t j = 0; j < p_trans->ListSize(); j++ )
861 EbmlElement *l = (*p_trans)[j];
863 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
865 p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
867 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
869 p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
871 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
873 p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
877 translations.push_back( p_translate );
881 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
885 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
886 i_duration = mtime_t(f_dur);
887 if( !i_duration ) i_duration = -1;
891 /*****************************************************************************
893 *****************************************************************************/
894 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
896 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
897 for( size_t i = 0; i < ca->ListSize(); i++ )
899 EbmlElement *l = (*ca)[i];
901 if( MKV_IS_ID( l, KaxChapterUID ) )
903 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
904 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %"PRIu64"", chapters.i_uid );
906 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
908 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
909 chapters.b_display_seekpoint = uint8( flag ) == 0;
911 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
913 else if( MKV_IS_ID( l, KaxChapterSegmentUID ) )
915 chapters.p_segment_uid = new KaxChapterSegmentUID( *static_cast<KaxChapterSegmentUID*>(l) );
916 b_ref_external_segments = true;
917 msg_Dbg( &sys.demuxer, "| | | | + ChapterSegmentUID= %u", *(uint32*)chapters.p_segment_uid->GetBuffer() );
919 else if( MKV_IS_ID( l, KaxChapterSegmentEditionUID ) )
921 chapters.p_segment_edition_uid = new KaxChapterSegmentEditionUID( *static_cast<KaxChapterSegmentEditionUID*>(l) );
922 msg_Dbg( &sys.demuxer, "| | | | + ChapterSegmentEditionUID= %u",
923 #if LIBMATROSKA_VERSION < 0x010300
924 *(uint32*)chapters.p_segment_edition_uid->GetBuffer()
926 *(uint32*)chapters.p_segment_edition_uid
930 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
932 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
933 chapters.i_start_time = uint64( start ) / INT64_C(1000);
935 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %"PRId64"", chapters.i_start_time );
937 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
939 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
940 chapters.i_end_time = uint64( end ) / INT64_C(1000);
942 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %"PRId64"", chapters.i_end_time );
944 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
946 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
948 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
949 for( size_t j = 0; j < cd->ListSize(); j++ )
951 EbmlElement *l= (*cd)[j];
953 if( MKV_IS_ID( l, KaxChapterString ) )
955 KaxChapterString &name =*(KaxChapterString*)l;
956 for ( int k = 0; k < i_level; k++)
957 chapters.psz_name += '+';
958 chapters.psz_name += ' ';
959 char *psz_tmp_utf8 = ToUTF8( UTFstring( name ) );
960 chapters.psz_name += psz_tmp_utf8;
961 chapters.b_user_display = true;
963 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", psz_tmp_utf8 );
964 free( psz_tmp_utf8 );
966 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
968 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
969 const char *psz = string( lang ).c_str();
971 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
973 else if( MKV_IS_ID( l, KaxChapterCountry ) )
975 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
976 const char *psz = string( ct ).c_str();
978 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
982 else if( MKV_IS_ID( l, KaxChapterProcess ) )
984 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
986 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
987 chapter_codec_cmds_c *p_ccodec = NULL;
989 for( size_t j = 0; j < cp->ListSize(); j++ )
991 EbmlElement *k= (*cp)[j];
993 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
995 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
996 if ( uint32(*p_codec_id) == 0 )
997 p_ccodec = new matroska_script_codec_c( sys );
998 else if ( uint32(*p_codec_id) == 1 )
999 p_ccodec = new dvd_chapter_codec_c( sys );
1004 if ( p_ccodec != NULL )
1006 for( size_t j = 0; j < cp->ListSize(); j++ )
1008 EbmlElement *k= (*cp)[j];
1010 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
1012 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
1013 p_ccodec->SetPrivate( *p_private );
1015 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
1017 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
1020 chapters.codecs.push_back( p_ccodec );
1023 else if( MKV_IS_ID( l, KaxChapterAtom ) )
1025 chapter_item_c *new_sub_chapter = new chapter_item_c();
1026 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
1027 new_sub_chapter->p_parent = &chapters;
1028 chapters.sub_chapters.push_back( new_sub_chapter );
1033 /*****************************************************************************
1035 *****************************************************************************/
1036 void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
1039 int i_upper_level = 0;
1041 attachments->Read( es, EBML_CONTEXT(attachments), i_upper_level, el, true );
1043 KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
1045 while( attachedFile && ( attachedFile->GetSize() > 0 ) )
1047 KaxFileData &img_data = GetChild<KaxFileData>( *attachedFile );
1048 char *psz_tmp_utf8 = ToUTF8( UTFstring( GetChild<KaxFileName>( *attachedFile ) ) );
1049 std::string attached_filename(psz_tmp_utf8);
1051 attachment_c *new_attachment = new attachment_c( attached_filename,
1052 GetChild<KaxMimeType>( *attachedFile ),
1053 img_data.GetSize() );
1055 msg_Dbg( &sys.demuxer, "| | - %s (%s)", new_attachment->fileName(), new_attachment->mimeType() );
1057 if( new_attachment->init() )
1059 memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
1060 sys.stored_attachments.push_back( new_attachment );
1061 if( !strncmp( new_attachment->mimeType(), "image/", 6 ) )
1064 if( asprintf( &psz_url, "attachment://%s",
1065 new_attachment->fileName() ) == -1 )
1068 sys.meta = vlc_meta_New();
1069 vlc_meta_SetArtURL( sys.meta, psz_url );
1075 delete new_attachment;
1078 attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
1082 /*****************************************************************************
1084 *****************************************************************************/
1085 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
1088 int i_upper_level = 0;
1090 /* Master elements */
1091 chapters->Read( es, EBML_CONTEXT(chapters), i_upper_level, el, true );
1093 for( size_t i = 0; i < chapters->ListSize(); i++ )
1095 EbmlElement *l = (*chapters)[i];
1097 if( MKV_IS_ID( l, KaxEditionEntry ) )
1099 chapter_edition_c *p_edition = new chapter_edition_c();
1101 EbmlMaster *E = static_cast<EbmlMaster *>(l );
1102 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
1103 for( size_t j = 0; j < E->ListSize(); j++ )
1105 EbmlElement *l = (*E)[j];
1107 if( MKV_IS_ID( l, KaxChapterAtom ) )
1109 chapter_item_c *new_sub_chapter = new chapter_item_c();
1110 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
1111 p_edition->sub_chapters.push_back( new_sub_chapter );
1113 else if( MKV_IS_ID( l, KaxEditionUID ) )
1115 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
1117 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
1119 p_edition->b_ordered = var_InheritBool( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
1121 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
1123 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
1124 i_default_edition = stored_editions.size();
1126 else if( MKV_IS_ID( l, KaxEditionFlagHidden ) )
1128 // FIXME to implement
1132 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
1135 stored_editions.push_back( p_edition );
1139 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
1144 void matroska_segment_c::ParseCluster( bool b_update_start_time )
1148 int i_upper_level = 0;
1150 /* Master elements */
1151 m = static_cast<EbmlMaster *>( cluster );
1152 m->Read( es, EBML_CONTEXT(cluster), i_upper_level, el, true );
1154 for( unsigned int i = 0; i < m->ListSize(); i++ )
1156 EbmlElement *l = (*m)[i];
1158 if( MKV_IS_ID( l, KaxClusterTimecode ) )
1160 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
1162 cluster->InitTimecode( uint64( ctc ), i_timescale );
1167 if( b_update_start_time )
1168 i_start_time = cluster->GlobalTimecode() / 1000;
1172 int32_t matroska_segment_c::TrackInit( mkv_track_t * p_tk )
1174 es_format_t *p_fmt = &p_tk->fmt;
1175 if( !strcmp( p_tk->psz_codec, "V_MS/VFW/FOURCC" ) )
1177 if( p_tk->i_extra_data < (int)sizeof( VLC_BITMAPINFOHEADER ) )
1179 msg_Err( &sys.demuxer, "missing/invalid VLC_BITMAPINFOHEADER" );
1180 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1184 VLC_BITMAPINFOHEADER *p_bih = (VLC_BITMAPINFOHEADER*)p_tk->p_extra_data;
1186 p_tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
1187 p_tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
1188 p_tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
1190 p_tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( VLC_BITMAPINFOHEADER );
1191 if( p_tk->fmt.i_extra > 0 )
1193 /* Very unlikely yet possible: bug #5659*/
1194 size_t maxlen = p_tk->i_extra_data - sizeof( VLC_BITMAPINFOHEADER );
1195 p_tk->fmt.i_extra = ( (unsigned)p_tk->fmt.i_extra < maxlen )?
1196 p_tk->fmt.i_extra : maxlen;
1198 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1199 memcpy( p_tk->fmt.p_extra, &p_bih[1], p_tk->fmt.i_extra );
1202 p_tk->b_dts_only = true;
1204 else if( !strcmp( p_tk->psz_codec, "V_MPEG1" ) ||
1205 !strcmp( p_tk->psz_codec, "V_MPEG2" ) )
1207 p_tk->fmt.i_codec = VLC_CODEC_MPGV;
1208 if( p_tk->i_extra_data )
1209 fill_extra_data( p_tk, 0 );
1211 else if( !strncmp( p_tk->psz_codec, "V_THEORA", 8 ) )
1213 p_tk->fmt.i_codec = VLC_CODEC_THEORA;
1214 fill_extra_data( p_tk, 0 );
1215 p_tk->b_pts_only = true;
1217 else if( !strncmp( p_tk->psz_codec, "V_REAL/RV", 9 ) )
1219 uint8_t *p = p_tk->p_extra_data;
1221 if( !strcmp( p_tk->psz_codec, "V_REAL/RV10" ) )
1222 p_fmt->i_codec = VLC_CODEC_RV10;
1223 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV20" ) )
1224 p_fmt->i_codec = VLC_CODEC_RV20;
1225 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV30" ) )
1226 p_fmt->i_codec = VLC_CODEC_RV30;
1227 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV40" ) )
1228 p_fmt->i_codec = VLC_CODEC_RV40;
1230 /* Extract the framerate from the header */
1231 if( p_tk->i_extra_data >= 26 &&
1232 p[4] == 'V' && p[5] == 'I' && p[6] == 'D' && p[7] == 'O' &&
1233 p[8] == 'R' && p[9] == 'V' &&
1234 (p[10] == '3' || p[10] == '4') && p[11] == '0' )
1236 p_tk->fmt.video.i_frame_rate =
1237 p[22] << 24 | p[23] << 16 | p[24] << 8 | p[25] << 0;
1238 p_tk->fmt.video.i_frame_rate_base = 65536;
1241 fill_extra_data( p_tk, 26 );
1242 p_tk->b_dts_only = true;
1244 else if( !strncmp( p_tk->psz_codec, "V_DIRAC", 7 ) )
1246 p_tk->fmt.i_codec = VLC_CODEC_DIRAC;
1248 else if( !strncmp( p_tk->psz_codec, "V_VP8", 5 ) )
1250 p_tk->fmt.i_codec = VLC_CODEC_VP8;
1251 p_tk->b_pts_only = true;
1253 else if( !strncmp( p_tk->psz_codec, "V_MPEG4", 7 ) )
1255 if( !strcmp( p_tk->psz_codec, "V_MPEG4/MS/V3" ) )
1257 p_tk->fmt.i_codec = VLC_CODEC_DIV3;
1259 else if( !strncmp( p_tk->psz_codec, "V_MPEG4/ISO", 11 ) )
1261 /* A MPEG 4 codec, SP, ASP, AP or AVC */
1262 if( !strcmp( p_tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
1263 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
1265 p_tk->fmt.i_codec = VLC_CODEC_MP4V;
1266 fill_extra_data( p_tk, 0 );
1269 else if( !strcmp( p_tk->psz_codec, "V_QUICKTIME" ) )
1271 MP4_Box_t *p_box = (MP4_Box_t*)xmalloc( sizeof( MP4_Box_t ) );
1272 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1276 if( MP4_ReadBoxCommon( p_mp4_stream, p_box ) &&
1277 MP4_ReadBox_sample_vide( p_mp4_stream, p_box ) )
1279 p_tk->fmt.i_codec = p_box->i_type;
1280 p_tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
1281 p_tk->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
1282 p_tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
1283 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1284 memcpy( p_tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, p_tk->fmt.i_extra );
1285 MP4_FreeBox_sample_vide( p_box );
1291 stream_Delete( p_mp4_stream );
1293 else if( !strcmp( p_tk->psz_codec, "V_MJPEG" ) )
1295 p_tk->fmt.i_codec = VLC_CODEC_MJPG;
1297 else if( !strcmp( p_tk->psz_codec, "A_MS/ACM" ) )
1299 if( p_tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1301 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
1302 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1306 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)p_tk->p_extra_data;
1308 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_tk->fmt.i_codec, NULL );
1310 if( p_tk->fmt.i_codec == VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
1311 msg_Err( &sys.demuxer, "Unrecognized wf tag: 0x%x", GetWLE( &p_wf->wFormatTag ) );
1312 p_tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
1313 p_tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
1314 p_tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
1315 p_tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
1316 p_tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
1318 p_tk->fmt.i_extra = GetWLE( &p_wf->cbSize );
1319 if( p_tk->fmt.i_extra > 0 )
1321 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1322 memcpy( p_tk->fmt.p_extra, &p_wf[1], p_tk->fmt.i_extra );
1326 else if( !strcmp( p_tk->psz_codec, "A_MPEG/L3" ) ||
1327 !strcmp( p_tk->psz_codec, "A_MPEG/L2" ) ||
1328 !strcmp( p_tk->psz_codec, "A_MPEG/L1" ) )
1330 p_tk->fmt.i_codec = VLC_CODEC_MPGA;
1332 else if( !strcmp( p_tk->psz_codec, "A_AC3" ) )
1334 p_tk->fmt.i_codec = VLC_CODEC_A52;
1336 else if( !strcmp( p_tk->psz_codec, "A_EAC3" ) )
1338 p_tk->fmt.i_codec = VLC_CODEC_EAC3;
1340 else if( !strcmp( p_tk->psz_codec, "A_DTS" ) )
1342 p_tk->fmt.i_codec = VLC_CODEC_DTS;
1344 else if( !strcmp( p_tk->psz_codec, "A_MLP" ) )
1346 p_tk->fmt.i_codec = VLC_CODEC_MLP;
1348 else if( !strcmp( p_tk->psz_codec, "A_TRUEHD" ) )
1350 /* FIXME when more samples arrive */
1351 p_tk->fmt.i_codec = VLC_CODEC_TRUEHD;
1352 p_fmt->b_packetized = false;
1354 else if( !strcmp( p_tk->psz_codec, "A_FLAC" ) )
1356 p_tk->fmt.i_codec = VLC_CODEC_FLAC;
1357 fill_extra_data( p_tk, 0 );
1359 else if( !strcmp( p_tk->psz_codec, "A_VORBIS" ) )
1361 p_tk->fmt.i_codec = VLC_CODEC_VORBIS;
1362 fill_extra_data( p_tk, 0 );
1364 else if( !strncmp( p_tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1365 !strncmp( p_tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1367 int i_profile, i_srate, sbr = 0;
1368 static const unsigned int i_sample_rates[] =
1370 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1371 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1374 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1375 /* create data for faad (MP4DecSpecificDescrTag)*/
1377 if( !strcmp( &p_tk->psz_codec[12], "MAIN" ) )
1381 else if( !strcmp( &p_tk->psz_codec[12], "LC" ) )
1385 else if( !strcmp( &p_tk->psz_codec[12], "SSR" ) )
1389 else if( !strcmp( &p_tk->psz_codec[12], "LC/SBR" ) )
1399 for( i_srate = 0; i_srate < 13; i_srate++ )
1401 if( i_sample_rates[i_srate] == p_tk->i_original_rate )
1406 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
1408 p_tk->fmt.i_extra = sbr ? 5 : 2;
1409 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1410 ((uint8_t*)p_tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1411 ((uint8_t*)p_tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (p_tk->fmt.audio.i_channels << 3);
1414 int syncExtensionType = 0x2B7;
1416 for (iDSRI=0; iDSRI<13; iDSRI++)
1417 if( i_sample_rates[iDSRI] == p_tk->fmt.audio.i_rate )
1419 ((uint8_t*)p_tk->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
1420 ((uint8_t*)p_tk->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
1421 ((uint8_t*)p_tk->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
1424 else if( !strcmp( p_tk->psz_codec, "A_AAC" ) )
1426 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1427 fill_extra_data( p_tk, 0 );
1429 else if( !strcmp( p_tk->psz_codec, "A_WAVPACK4" ) )
1431 p_tk->fmt.i_codec = VLC_CODEC_WAVPACK;
1432 fill_extra_data( p_tk, 0 );
1434 else if( !strcmp( p_tk->psz_codec, "A_TTA1" ) )
1436 p_fmt->i_codec = VLC_CODEC_TTA;
1437 if( p_tk->i_extra_data > 0 )
1439 fill_extra_data( p_tk, 0 );
1443 p_fmt->i_extra = 30;
1444 p_fmt->p_extra = xmalloc( p_fmt->i_extra );
1445 uint8_t *p_extra = (uint8_t*)p_fmt->p_extra;
1446 memcpy( &p_extra[ 0], "TTA1", 4 );
1447 SetWLE( &p_extra[ 4], 1 );
1448 SetWLE( &p_extra[ 6], p_fmt->audio.i_channels );
1449 SetWLE( &p_extra[ 8], p_fmt->audio.i_bitspersample );
1450 SetDWLE( &p_extra[10], p_fmt->audio.i_rate );
1451 SetDWLE( &p_extra[14], 0xffffffff );
1452 memset( &p_extra[18], 0, 30 - 18 );
1455 else if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) ||
1456 !strcmp( p_tk->psz_codec, "A_PCM/INT/LIT" ) ||
1457 !strcmp( p_tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
1459 if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) )
1461 p_tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1465 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1467 p_tk->fmt.audio.i_blockalign = ( p_tk->fmt.audio.i_bitspersample + 7 ) / 8 * p_tk->fmt.audio.i_channels;
1469 else if( !strncmp( p_tk->psz_codec, "A_REAL/", 7 ) )
1471 if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
1473 p_fmt->i_codec = VLC_CODEC_RA_144;
1474 p_fmt->audio.i_channels = 1;
1475 p_fmt->audio.i_rate = 8000;
1476 p_fmt->audio.i_blockalign = 0x14;
1478 else if( p_tk->i_extra_data > 28 )
1480 uint8_t *p = p_tk->p_extra_data;
1481 if( memcmp( p, ".ra", 3 ) ) {
1482 msg_Err( &sys.demuxer, "Invalid Real ExtraData 0x%4.4s", (char *)p );
1483 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1487 real_audio_private * priv = (real_audio_private*) p_tk->p_extra_data;
1488 if( !strcmp( p_tk->psz_codec, "A_REAL/COOK" ) )
1490 p_tk->fmt.i_codec = VLC_CODEC_COOK;
1491 p_tk->fmt.audio.i_blockalign = hton16(priv->sub_packet_size);
1493 else if( !strcmp( p_tk->psz_codec, "A_REAL/ATRC" ) )
1494 p_tk->fmt.i_codec = VLC_CODEC_ATRAC3;
1495 else if( !strcmp( p_tk->psz_codec, "A_REAL/28_8" ) )
1496 p_tk->fmt.i_codec = VLC_CODEC_RA_288;
1497 /* FIXME RALF and SIPR */
1498 uint16_t version = (uint16_t) hton16(priv->version);
1500 new Cook_PrivateTrackData( hton16(priv->sub_packet_h),
1501 hton16(priv->frame_size),
1502 hton16(priv->sub_packet_size));
1503 if( unlikely( !p_tk->p_sys ) )
1506 if( unlikely( p_tk->p_sys->Init() ) )
1511 real_audio_private_v4 * v4 = (real_audio_private_v4*) priv;
1512 p_tk->fmt.audio.i_channels = hton16(v4->channels);
1513 p_tk->fmt.audio.i_bitspersample = hton16(v4->sample_size);
1514 p_tk->fmt.audio.i_rate = hton16(v4->sample_rate);
1516 else if( version == 5 )
1518 real_audio_private_v5 * v5 = (real_audio_private_v5*) priv;
1519 p_tk->fmt.audio.i_channels = hton16(v5->channels);
1520 p_tk->fmt.audio.i_bitspersample = hton16(v5->sample_size);
1521 p_tk->fmt.audio.i_rate = hton16(v5->sample_rate);
1523 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);
1525 fill_extra_data( p_tk, p_tk->fmt.i_codec == VLC_CODEC_RA_288 ? 0 : 78);
1529 else if( !strcmp( p_tk->psz_codec, "S_KATE" ) )
1531 p_tk->fmt.i_codec = VLC_CODEC_KATE;
1532 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1534 fill_extra_data( p_tk, 0 );
1536 else if( !strcmp( p_tk->psz_codec, "S_TEXT/ASCII" ) )
1538 p_fmt->i_codec = VLC_CODEC_SUBT;
1539 p_fmt->subs.psz_encoding = strdup( "ASCII" );
1541 else if( !strcmp( p_tk->psz_codec, "S_TEXT/UTF8" ) )
1543 p_tk->fmt.i_codec = VLC_CODEC_SUBT;
1544 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1546 else if( !strcmp( p_tk->psz_codec, "S_TEXT/USF" ) )
1548 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
1549 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1550 if( p_tk->i_extra_data )
1551 fill_extra_data( p_tk, 0 );
1553 else if( !strcmp( p_tk->psz_codec, "S_TEXT/SSA" ) ||
1554 !strcmp( p_tk->psz_codec, "S_TEXT/ASS" ) ||
1555 !strcmp( p_tk->psz_codec, "S_SSA" ) ||
1556 !strcmp( p_tk->psz_codec, "S_ASS" ))
1558 p_tk->fmt.i_codec = VLC_CODEC_SSA;
1559 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1560 if( p_tk->i_extra_data )
1561 fill_extra_data( p_tk, 0 );
1563 else if( !strcmp( p_tk->psz_codec, "S_VOBSUB" ) )
1565 p_tk->fmt.i_codec = VLC_CODEC_SPU;
1566 if( p_tk->i_extra_data )
1569 char *psz_buf = (char *)malloc( p_tk->i_extra_data + 1);
1570 if( psz_buf != NULL )
1572 memcpy( psz_buf, p_tk->p_extra_data , p_tk->i_extra_data );
1573 psz_buf[p_tk->i_extra_data] = '\0';
1575 psz_start = strstr( psz_buf, "size:" );
1577 vobsub_size_parse( psz_start,
1578 &p_tk->fmt.subs.spu.i_original_frame_width,
1579 &p_tk->fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
1581 msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d",
1582 p_tk->fmt.subs.spu.i_original_frame_width,
1583 p_tk->fmt.subs.spu.i_original_frame_height );
1587 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1591 psz_start = strstr( psz_buf, "palette:" );
1593 vobsub_palette_parse( psz_start, &p_tk->fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
1595 p_tk->fmt.subs.spu.palette[0] = 0xBeef;
1596 msg_Dbg( &sys.demuxer, "vobsub palette read" );
1600 msg_Warn( &sys.demuxer, "reading original palette failed" );
1606 else if( !strcmp( p_tk->psz_codec, "S_HDMV/PGS" ) )
1608 p_tk->fmt.i_codec = VLC_CODEC_BD_PG;
1610 else if( !strcmp( p_tk->psz_codec, "B_VOBBTN" ) )
1612 p_tk->fmt.i_cat = NAV_ES;
1616 msg_Err( &sys.demuxer, "unknown codec id=`%s'", p_tk->psz_codec );
1617 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );