1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2005, 2008, 2010 VLC authors and VideoLAN
5 * $Id: 44e9fa805fa7b77eb2b8b3dd8ad68a6fe981d086 $
6 *
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Steve Lhomme <steve.lhomme@free.fr>
9 *
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.
14 *
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.
19 *
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 *****************************************************************************/
24
25 #include "mkv.hpp"
26 #include "util.hpp"
27
28 #include "matroska_segment.hpp"
29 #include "demux.hpp"
30
31 #include "chapters.hpp"
32 #include "Ebml_parser.hpp"
33
34 #include "stream_io_callback.hpp"
35
36 #include <new>
37 #include <limits>
38
39 extern "C" {
40 #include "../av1_unpack.h"
41 }
42
43 #include <vlc_fs.h>
44 #include <vlc_url.h>
45
46 /*****************************************************************************
47 * Module descriptor
48 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
51
52 vlc_module_begin ()
53 set_shortname( "Matroska" )
54 set_description( N_("Matroska stream demuxer" ) )
55 set_capability( "demux", 50 )
56 set_callbacks( Open, Close )
57 set_category( CAT_INPUT )
58 set_subcategory( SUBCAT_INPUT_DEMUX )
59
60 add_bool( "mkv-use-ordered-chapters", true,
61 N_("Respect ordered chapters"),
62 N_("Play chapters in the order specified in the segment."), false );
63
64 add_bool( "mkv-use-chapter-codec", true,
65 N_("Chapter codecs"),
66 N_("Use chapter codecs found in the segment."), true );
67
68 add_bool( "mkv-preload-local-dir", true,
69 N_("Preload MKV files in the same directory"),
70 N_("Preload matroska files in the same directory to find linked segments (not good for broken files)."), false );
71
72 add_bool( "mkv-seek-percent", false,
73 N_("Seek based on percent not time"),
74 N_("Seek based on percent not time."), true );
75
76 add_bool( "mkv-use-dummy", false,
77 N_("Dummy Elements"),
78 N_("Read and discard unknown EBML elements (not good for broken files)."), true );
79
80 add_bool( "mkv-preload-clusters", false,
81 N_("Preload clusters"),
82 N_("Find all cluster positions by jumping cluster-to-cluster before playback"), true );
83
84 add_shortcut( "mka", "mkv" )
85 vlc_module_end ()
86
87 struct demux_sys_t;
88
89 static int Demux ( demux_t * );
90 static int Control( demux_t *, int, va_list );
91 static int Seek ( demux_t *, mtime_t i_mk_date, double f_percent, virtual_chapter_c *p_vchapter, bool b_precise = true );
92
93 /*****************************************************************************
94 * Open: initializes matroska demux structures
95 *****************************************************************************/
Open(vlc_object_t * p_this)96 static int Open( vlc_object_t * p_this )
97 {
98 demux_t *p_demux = (demux_t*)p_this;
99 demux_sys_t *p_sys;
100 matroska_stream_c *p_stream;
101 matroska_segment_c *p_segment;
102 const uint8_t *p_peek;
103 std::string s_path, s_filename;
104 bool b_need_preload = false;
105
106 /* peek the begining */
107 if( vlc_stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
108
109 /* is a valid file */
110 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
111 p_peek[2] != 0xdf || p_peek[3] != 0xa3 ) return VLC_EGENERIC;
112
113 /* Set the demux function */
114 p_demux->pf_demux = Demux;
115 p_demux->pf_control = Control;
116 p_demux->p_sys = p_sys = new demux_sys_t( *p_demux );
117
118 p_stream = new matroska_stream_c( p_demux->s, false );
119 if ( unlikely(p_stream == NULL) )
120 {
121 msg_Err( p_demux, "failed to create matroska_stream_c" );
122 delete p_sys;
123 return VLC_ENOMEM;
124 }
125 p_sys->streams.push_back( p_stream );
126
127 if( !p_sys->AnalyseAllSegmentsFound( p_demux, p_stream, true ) )
128 {
129 msg_Err( p_demux, "cannot find KaxSegment or missing mandatory KaxInfo" );
130 goto error;
131 }
132
133 for (size_t i=0; i<p_stream->segments.size(); i++)
134 {
135 p_stream->segments[i]->Preload();
136 b_need_preload |= p_stream->segments[i]->b_ref_external_segments;
137 if ( p_stream->segments[i]->translations.size() &&
138 p_stream->segments[i]->translations[0]->codec_id == MATROSKA_CHAPTER_CODEC_DVD &&
139 p_stream->segments[i]->families.size() )
140 b_need_preload = true;
141 }
142
143 p_segment = p_stream->segments[0];
144 if( p_segment->cluster == NULL && p_segment->stored_editions.size() == 0 )
145 {
146 msg_Err( p_demux, "cannot find any cluster or chapter, damaged file ?" );
147 goto error;
148 }
149
150 if (b_need_preload && var_InheritBool( p_demux, "mkv-preload-local-dir" ))
151 {
152 msg_Dbg( p_demux, "Preloading local dir" );
153 /* get the files from the same dir from the same family (based on p_demux->psz_path) */
154 if ( p_demux->psz_file && !strcmp( p_demux->psz_access, "file" ) )
155 {
156 // assume it's a regular file
157 // get the directory path
158 s_path = p_demux->psz_file;
159 if (s_path.at(s_path.length() - 1) == DIR_SEP_CHAR)
160 {
161 s_path = s_path.substr(0,s_path.length()-1);
162 }
163 else
164 {
165 if (s_path.find_last_of(DIR_SEP_CHAR) > 0)
166 {
167 s_path = s_path.substr(0,s_path.find_last_of(DIR_SEP_CHAR));
168 }
169 }
170
171 DIR *p_src_dir = vlc_opendir(s_path.c_str());
172
173 if (p_src_dir != NULL)
174 {
175 const char *psz_file;
176 while ((psz_file = vlc_readdir(p_src_dir)) != NULL)
177 {
178 if (strlen(psz_file) > 4)
179 {
180 s_filename = s_path + DIR_SEP_CHAR + psz_file;
181
182 #if defined(_WIN32) || defined(__OS2__)
183 if (!strcasecmp(s_filename.c_str(), p_demux->psz_file))
184 #else
185 if (!s_filename.compare(p_demux->psz_file))
186 #endif
187 {
188 continue; // don't reuse the original opened file
189 }
190
191 if (!strcasecmp(s_filename.c_str() + s_filename.length() - 4, ".mkv") ||
192 !strcasecmp(s_filename.c_str() + s_filename.length() - 4, ".mka"))
193 {
194 // test whether this file belongs to our family
195 const uint8_t *p_peek;
196 bool file_ok = false;
197 char *psz_url = vlc_path2uri( s_filename.c_str(), "file" );
198 stream_t *p_file_stream = vlc_stream_NewURL(
199 p_demux,
200 psz_url );
201 /* peek the begining */
202 if( p_file_stream &&
203 vlc_stream_Peek( p_file_stream, &p_peek, 4 ) >= 4
204 && p_peek[0] == 0x1a && p_peek[1] == 0x45 &&
205 p_peek[2] == 0xdf && p_peek[3] == 0xa3 ) file_ok = true;
206
207 if ( file_ok )
208 {
209 matroska_stream_c *p_stream = new matroska_stream_c( p_file_stream, true );
210
211 if ( !p_sys->AnalyseAllSegmentsFound( p_demux, p_stream ) )
212 {
213 msg_Dbg( p_demux, "the file '%s' will not be used", s_filename.c_str() );
214 delete p_stream;
215 }
216 else
217 {
218 p_sys->streams.push_back( p_stream );
219 }
220 }
221 else
222 {
223 if( p_file_stream ) {
224 vlc_stream_Delete( p_file_stream );
225 }
226 msg_Dbg( p_demux, "the file '%s' cannot be opened", s_filename.c_str() );
227 }
228 free( psz_url );
229 }
230 }
231 }
232 closedir( p_src_dir );
233 }
234 }
235
236 p_sys->PreloadFamily( *p_segment );
237 }
238 else if (b_need_preload)
239 msg_Warn( p_demux, "This file references other files, you may want to enable the preload of local directory");
240
241 if ( !p_sys->PreloadLinked() ||
242 !p_sys->PreparePlayback( *p_sys->p_current_vsegment, 0 ) )
243 {
244 msg_Err( p_demux, "cannot use the segment" );
245 goto error;
246 }
247
248 if (!p_sys->FreeUnused())
249 {
250 msg_Err( p_demux, "no usable segment" );
251 goto error;
252 }
253
254 p_sys->InitUi();
255
256 return VLC_SUCCESS;
257
258 error:
259 delete p_sys;
260 return VLC_EGENERIC;
261 }
262
263 /*****************************************************************************
264 * Close: frees unused data
265 *****************************************************************************/
Close(vlc_object_t * p_this)266 static void Close( vlc_object_t *p_this )
267 {
268 demux_t *p_demux = reinterpret_cast<demux_t*>( p_this );
269 demux_sys_t *p_sys = p_demux->p_sys;
270 virtual_segment_c *p_vsegment = p_sys->p_current_vsegment;
271 if( p_vsegment )
272 {
273 matroska_segment_c *p_segment = p_vsegment->CurrentSegment();
274 if( p_segment )
275 p_segment->ESDestroy();
276 }
277
278 delete p_sys;
279 }
280
281 /*****************************************************************************
282 * Control:
283 *****************************************************************************/
Control(demux_t * p_demux,int i_query,va_list args)284 static int Control( demux_t *p_demux, int i_query, va_list args )
285 {
286 demux_sys_t *p_sys = p_demux->p_sys;
287 int64_t *pi64, i64;
288 double *pf, f;
289 int i_skp;
290 size_t i_idx;
291 bool b;
292
293 vlc_meta_t *p_meta;
294 input_attachment_t ***ppp_attach;
295 int *pi_int;
296
297 switch( i_query )
298 {
299 case DEMUX_CAN_SEEK:
300 return vlc_stream_vaControl( p_demux->s, i_query, args );
301
302 case DEMUX_GET_ATTACHMENTS:
303 ppp_attach = va_arg( args, input_attachment_t*** );
304 pi_int = va_arg( args, int * );
305
306 if( p_sys->stored_attachments.size() <= 0 )
307 return VLC_EGENERIC;
308
309 *pi_int = p_sys->stored_attachments.size();
310 *ppp_attach = static_cast<input_attachment_t**>( vlc_alloc( p_sys->stored_attachments.size(),
311 sizeof(input_attachment_t*) ) );
312 if( !(*ppp_attach) )
313 return VLC_ENOMEM;
314 for( size_t i = 0; i < p_sys->stored_attachments.size(); i++ )
315 {
316 attachment_c *a = p_sys->stored_attachments[i];
317 (*ppp_attach)[i] = vlc_input_attachment_New( a->fileName(), a->mimeType(), NULL,
318 a->p_data, a->size() );
319 if( !(*ppp_attach)[i] )
320 {
321 free(*ppp_attach);
322 return VLC_ENOMEM;
323 }
324 }
325 return VLC_SUCCESS;
326
327 case DEMUX_GET_META:
328 p_meta = va_arg( args, vlc_meta_t* );
329 vlc_meta_Merge( p_meta, p_sys->meta );
330 return VLC_SUCCESS;
331
332 case DEMUX_GET_LENGTH:
333 pi64 = va_arg( args, int64_t * );
334 if( p_sys->f_duration > 0.0 )
335 *pi64 = static_cast<int64_t>( p_sys->f_duration * 1000 );
336 else
337 *pi64 = VLC_TS_INVALID;
338 return VLC_SUCCESS;
339
340 case DEMUX_GET_POSITION:
341 pf = va_arg( args, double * );
342 if ( p_sys->f_duration > 0.0 )
343 *pf = static_cast<double> (p_sys->i_pcr >= (p_sys->i_start_pts + p_sys->i_mk_chapter_time) ?
344 p_sys->i_pcr :
345 (p_sys->i_start_pts + p_sys->i_mk_chapter_time) ) / (1000.0 * p_sys->f_duration);
346 return VLC_SUCCESS;
347
348 case DEMUX_SET_POSITION:
349 if( p_sys->f_duration > 0.0 )
350 {
351 f = va_arg( args, double );
352 b = va_arg( args, int ); /* precise? */
353 return Seek( p_demux, -1, f, NULL, b );
354 }
355 return VLC_EGENERIC;
356
357 case DEMUX_GET_TIME:
358 pi64 = va_arg( args, int64_t * );
359 *pi64 = p_sys->i_pcr;
360 return VLC_SUCCESS;
361
362 case DEMUX_GET_TITLE_INFO:
363 if( p_sys->titles.size() > 1 || ( p_sys->titles.size() == 1 && p_sys->titles[0]->i_seekpoint > 0 ) )
364 {
365 input_title_t ***ppp_title = va_arg( args, input_title_t*** );
366 int *pi_int = va_arg( args, int* );
367
368 *pi_int = p_sys->titles.size();
369 *ppp_title = static_cast<input_title_t**>( vlc_alloc( p_sys->titles.size(), sizeof( input_title_t* ) ) );
370
371 for( size_t i = 0; i < p_sys->titles.size(); i++ )
372 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->titles[i] );
373 return VLC_SUCCESS;
374 }
375 return VLC_EGENERIC;
376
377 case DEMUX_SET_TITLE:
378 /* handle editions as titles */
379 i_idx = va_arg( args, int );
380 if(i_idx < p_sys->titles.size() && p_sys->titles[i_idx]->i_seekpoint)
381 {
382 const int i_edition = p_sys->p_current_vsegment->i_current_edition;
383 const int i_title = p_sys->i_current_title;
384 p_sys->p_current_vsegment->i_current_edition = i_idx;
385 p_sys->i_current_title = i_idx;
386 if( VLC_SUCCESS ==
387 Seek( p_demux, static_cast<int64_t>( p_sys->titles[i_idx]->seekpoint[0]->i_time_offset ), -1, NULL) )
388 {
389 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT|INPUT_UPDATE_TITLE;
390 p_demux->info.i_seekpoint = 0;
391 p_demux->info.i_title = i_idx;
392 p_sys->f_duration = (float) p_sys->titles[i_idx]->i_length / 1000.f;
393 return VLC_SUCCESS;
394 }
395 else
396 {
397 p_sys->p_current_vsegment->i_current_edition = i_edition;
398 p_sys->i_current_title = i_title;
399 }
400 }
401 return VLC_EGENERIC;
402
403 case DEMUX_SET_SEEKPOINT:
404 i_skp = va_arg( args, int );
405
406 // TODO change the way it works with the << & >> buttons on the UI (+1/-1 instead of a number)
407 if( p_sys->titles.size() && i_skp < p_sys->titles[p_sys->i_current_title]->i_seekpoint)
408 {
409 int i_ret = Seek( p_demux, static_cast<int64_t>( p_sys->titles[p_sys->i_current_title]->seekpoint[i_skp]->i_time_offset ), -1, NULL);
410 if( i_ret == VLC_SUCCESS )
411 {
412 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
413 p_demux->info.i_seekpoint = i_skp;
414 }
415 return i_ret;
416 }
417 return VLC_EGENERIC;
418
419 case DEMUX_GET_FPS:
420 pf = va_arg( args, double * );
421 *pf = 0.0;
422 if( p_sys->p_current_vsegment && p_sys->p_current_vsegment->CurrentSegment() )
423 {
424 typedef matroska_segment_c::tracks_map_t tracks_map_t;
425
426 const matroska_segment_c *p_segment = p_sys->p_current_vsegment->CurrentSegment();
427 for( tracks_map_t::const_iterator it = p_segment->tracks.begin(); it != p_segment->tracks.end(); ++it )
428 {
429 const mkv_track_t &track = *it->second;
430
431 if( track.fmt.i_cat == VIDEO_ES && track.fmt.video.i_frame_rate_base > 0 )
432 {
433 *pf = (double)track.fmt.video.i_frame_rate / track.fmt.video.i_frame_rate_base;
434 break;
435 }
436 }
437 }
438 return VLC_SUCCESS;
439
440 case DEMUX_SET_TIME:
441 i64 = va_arg( args, int64_t );
442 b = va_arg( args, int ); /* precise? */
443 msg_Dbg(p_demux,"SET_TIME to %" PRId64, i64 );
444 return Seek( p_demux, i64, -1, NULL, b );
445 default:
446 return VLC_EGENERIC;
447 }
448 }
449
450 /* Seek */
Seek(demux_t * p_demux,mtime_t i_mk_date,double f_percent,virtual_chapter_c * p_vchapter,bool b_precise)451 static int Seek( demux_t *p_demux, mtime_t i_mk_date, double f_percent, virtual_chapter_c *p_vchapter, bool b_precise )
452 {
453 demux_sys_t *p_sys = p_demux->p_sys;
454 virtual_segment_c *p_vsegment = p_sys->p_current_vsegment;
455 matroska_segment_c *p_segment = p_vsegment->CurrentSegment();
456
457 if( f_percent < 0 ) msg_Dbg( p_demux, "seek request to i_pos = %" PRId64, i_mk_date );
458 else msg_Dbg( p_demux, "seek request to %.2f%%", f_percent * 100 );
459
460 if( i_mk_date < 0 && f_percent < 0 )
461 {
462 msg_Warn( p_demux, "cannot seek nowhere!" );
463 return VLC_EGENERIC;
464 }
465 if( f_percent > 1.0 )
466 {
467 msg_Warn( p_demux, "cannot seek so far!" );
468 return VLC_EGENERIC;
469 }
470 if( p_sys->f_duration < 0 )
471 {
472 msg_Warn( p_demux, "cannot seek without duration!");
473 return VLC_EGENERIC;
474 }
475 if( !p_segment )
476 {
477 msg_Warn( p_demux, "cannot seek without valid segment position");
478 return VLC_EGENERIC;
479 }
480
481 /* seek without index or without date */
482 if( f_percent >= 0 && (var_InheritBool( p_demux, "mkv-seek-percent" ) || i_mk_date < 0 ))
483 {
484 i_mk_date = int64_t( f_percent * p_sys->f_duration * 1000.0 );
485 }
486 return p_vsegment->Seek( *p_demux, i_mk_date, p_vchapter, b_precise ) ? VLC_SUCCESS : VLC_EGENERIC;
487 }
488
489 /* Needed by matroska_segment::Seek() and Seek */
BlockDecode(demux_t * p_demux,KaxBlock * block,KaxSimpleBlock * simpleblock,KaxBlockAdditions * additions,mtime_t i_pts,int64_t i_duration,bool b_key_picture,bool b_discardable_picture)490 void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock,
491 KaxBlockAdditions *additions,
492 mtime_t i_pts, int64_t i_duration, bool b_key_picture,
493 bool b_discardable_picture )
494 {
495 demux_sys_t *p_sys = p_demux->p_sys;
496 matroska_segment_c *p_segment = p_sys->p_current_vsegment->CurrentSegment();
497
498 if( !p_segment ) return;
499
500 mkv_track_t *p_track = p_segment->FindTrackByBlock( block, simpleblock );
501 if( p_track == NULL )
502 {
503 msg_Err( p_demux, "invalid track number" );
504 return;
505 }
506
507 mkv_track_t &track = *p_track;
508
509 if( track.fmt.i_cat != DATA_ES && track.p_es == NULL )
510 {
511 msg_Err( p_demux, "unknown track number" );
512 return;
513 }
514
515 i_pts -= track.i_codec_delay;
516
517 if ( track.fmt.i_cat != DATA_ES )
518 {
519 bool b;
520 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, track.p_es, &b );
521
522 if( !b )
523 {
524 if( track.fmt.i_cat == VIDEO_ES || track.fmt.i_cat == AUDIO_ES )
525 track.i_last_dts = VLC_TS_INVALID;
526 return;
527 }
528 }
529
530 size_t frame_size = 0;
531 size_t block_size = 0;
532
533 if( simpleblock != NULL )
534 block_size = simpleblock->GetSize();
535 else
536 block_size = block->GetSize();
537
538 const unsigned int i_number_frames = block != NULL ? block->NumberFrames() :
539 ( simpleblock != NULL ? simpleblock->NumberFrames() : 0 );
540
541 for( unsigned int i_frame = 0; i_frame < i_number_frames; i_frame++ )
542 {
543 block_t *p_block;
544 DataBuffer *data;
545 if( simpleblock != NULL )
546 {
547 data = &simpleblock->GetBuffer(i_frame);
548 }
549 else
550 {
551 data = &block->GetBuffer(i_frame);
552 }
553 frame_size += data->Size();
554 if( !data->Buffer() || data->Size() > frame_size || frame_size > block_size )
555 {
556 msg_Warn( p_demux, "Cannot read frame (too long or no frame)" );
557 break;
558 }
559 size_t extra_data = track.fmt.i_codec == VLC_CODEC_PRORES ? 8 : 0;
560
561 if( track.i_compression_type == MATROSKA_COMPRESSION_HEADER &&
562 track.p_compression_data != NULL &&
563 track.i_encoding_scope & MATROSKA_ENCODING_SCOPE_ALL_FRAMES )
564 p_block = MemToBlock( data->Buffer(), data->Size(), track.p_compression_data->GetSize() + extra_data );
565 else if( unlikely( track.fmt.i_codec == VLC_CODEC_WAVPACK ) )
566 p_block = packetize_wavpack( track, data->Buffer(), data->Size() );
567 else
568 p_block = MemToBlock( data->Buffer(), data->Size(), extra_data );
569
570 if( p_block == NULL )
571 {
572 break;
573 }
574
575 #if defined(HAVE_ZLIB_H)
576 if( track.i_compression_type == MATROSKA_COMPRESSION_ZLIB &&
577 track.i_encoding_scope & MATROSKA_ENCODING_SCOPE_ALL_FRAMES )
578 {
579 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
580 if( p_block == NULL )
581 break;
582 }
583 else
584 #endif
585 if( track.i_compression_type == MATROSKA_COMPRESSION_HEADER &&
586 track.i_encoding_scope & MATROSKA_ENCODING_SCOPE_ALL_FRAMES )
587 {
588 memcpy( p_block->p_buffer, track.p_compression_data->GetBuffer(), track.p_compression_data->GetSize() );
589 }
590 if ( track.fmt.i_codec == VLC_CODEC_PRORES )
591 memcpy( p_block->p_buffer + 4, "icpf", 4 );
592
593 if ( b_key_picture )
594 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
595
596 switch( track.fmt.i_codec )
597 {
598 case VLC_CODEC_COOK:
599 case VLC_CODEC_ATRAC3:
600 {
601 handle_real_audio(p_demux, &track, p_block, i_pts);
602 block_Release(p_block);
603 i_pts = ( track.i_default_duration )?
604 i_pts + ( mtime_t )track.i_default_duration:
605 VLC_TS_INVALID;
606 continue;
607 }
608
609 case VLC_CODEC_WEBVTT:
610 {
611 const uint8_t *p_addition = NULL;
612 size_t i_addition = 0;
613 if(additions)
614 {
615 KaxBlockMore *blockmore = FindChild<KaxBlockMore>(*additions);
616 if(blockmore)
617 {
618 KaxBlockAdditional *addition = FindChild<KaxBlockAdditional>(*blockmore);
619 if(addition)
620 {
621 i_addition = static_cast<std::string::size_type>(addition->GetSize());
622 p_addition = reinterpret_cast<const uint8_t *>(addition->GetBuffer());
623 }
624 }
625 }
626 p_block = WEBVTT_Repack_Sample( p_block, /* D_WEBVTT -> webm */
627 !p_track->codec.compare( 0, 1, "D" ),
628 p_addition, i_addition );
629 if( !p_block )
630 continue;
631 }
632 break;
633
634 case VLC_CODEC_OPUS:
635 {
636 mtime_t i_length = i_duration * track. f_timecodescale *
637 (double) p_segment->i_timescale / 1000.0;
638 if ( i_length < 0 ) i_length = 0;
639 p_block->i_nb_samples = i_length * track.fmt.audio.i_rate
640 / CLOCK_FREQ;
641 break;
642 }
643
644 case VLC_CODEC_AV1:
645 p_block = AV1_Unpack_Sample( p_block );
646 if( unlikely( !p_block ) )
647 continue;
648 break;
649 }
650
651 if( track.fmt.i_cat != VIDEO_ES )
652 {
653 if ( track.fmt.i_cat == DATA_ES )
654 {
655 // TODO handle the start/stop times of this packet
656 if( p_block->i_size >= sizeof(pci_t))
657 p_sys->p_ev->SetPci( (const pci_t *)&p_block->p_buffer[1]);
658 block_Release( p_block );
659 return;
660 }
661 p_block->i_dts = p_block->i_pts = i_pts;
662 }
663 else
664 {
665 // correct timestamping when B frames are used
666 if( track.b_dts_only )
667 {
668 p_block->i_pts = VLC_TS_INVALID;
669 p_block->i_dts = i_pts;
670 }
671 else if( track.b_pts_only )
672 {
673 p_block->i_pts = i_pts;
674 p_block->i_dts = i_pts;
675 }
676 else
677 {
678 p_block->i_pts = i_pts;
679 // condition when the DTS is correct (keyframe or B frame == NOT P frame)
680 if ( b_key_picture || b_discardable_picture )
681 p_block->i_dts = p_block->i_pts;
682 else if ( track.i_last_dts == VLC_TS_INVALID )
683 p_block->i_dts = i_pts;
684 else
685 p_block->i_dts = std::min( i_pts, track.i_last_dts + ( mtime_t )track.i_default_duration );
686 }
687 }
688
689 send_Block( p_demux, &track, p_block, i_number_frames, i_duration );
690
691 /* use time stamp only for first block */
692 i_pts = ( track.i_default_duration )?
693 i_pts + ( mtime_t )track.i_default_duration:
694 ( track.fmt.b_packetized ) ? VLC_TS_INVALID : i_pts + 1;
695 }
696 }
697
698 /*****************************************************************************
699 * Demux: reads and demuxes data packets
700 *****************************************************************************
701 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
702 *****************************************************************************/
Demux(demux_t * p_demux)703 static int Demux( demux_t *p_demux)
704 {
705 demux_sys_t *p_sys = p_demux->p_sys;
706
707 vlc_mutex_locker demux_lock ( &p_sys->lock_demuxer );
708
709 virtual_segment_c *p_vsegment = p_sys->p_current_vsegment;
710
711 if( p_sys->i_pts >= p_sys->i_start_pts )
712 {
713 if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
714 return 1;
715 p_vsegment = p_sys->p_current_vsegment;
716 }
717
718 matroska_segment_c *p_segment = p_vsegment->CurrentSegment();
719 if ( p_segment == NULL )
720 return 0;
721
722 KaxBlock *block;
723 KaxSimpleBlock *simpleblock;
724 KaxBlockAdditions *additions;
725 int64_t i_block_duration = 0;
726 bool b_key_picture;
727 bool b_discardable_picture;
728
729 if( p_segment->BlockGet( block, simpleblock, additions,
730 &b_key_picture, &b_discardable_picture, &i_block_duration ) )
731 {
732 if ( p_vsegment->CurrentEdition() && p_vsegment->CurrentEdition()->b_ordered )
733 {
734 const virtual_chapter_c *p_chap = p_vsegment->CurrentChapter();
735 // check if there are more chapters to read
736 if ( p_chap != NULL )
737 {
738 /* TODO handle successive chapters with the same user_start_time/user_end_time
739 */
740 p_sys->i_pts = p_chap->i_mk_virtual_stop_time + VLC_TS_0;
741 p_sys->i_pts++; // trick to avoid staying on segments with no duration and no content
742
743 return 1;
744 }
745 }
746
747 msg_Warn( p_demux, "cannot get block EOF?" );
748 return 0;
749 }
750
751 {
752 mkv_track_t *p_track = p_segment->FindTrackByBlock( block, simpleblock );
753
754 if( p_track == NULL )
755 {
756 msg_Err( p_demux, "invalid track number" );
757 delete block;
758 delete additions;
759 return 0;
760 }
761
762 mkv_track_t &track = *p_track;
763
764
765 if( track.i_skip_until_fpos != std::numeric_limits<uint64_t>::max() ) {
766
767 uint64_t block_fpos = 0;
768
769 if( block ) block_fpos = block->GetElementPosition();
770 else block_fpos = simpleblock->GetElementPosition();
771
772 if ( track.i_skip_until_fpos > block_fpos )
773 {
774 delete block;
775 delete additions;
776 return 1; // this block shall be ignored
777 }
778 }
779 }
780
781 /* update pcr */
782 {
783 int64_t i_pcr = VLC_TS_INVALID;
784
785 typedef matroska_segment_c::tracks_map_t tracks_map_t;
786
787 for( tracks_map_t::const_iterator it = p_segment->tracks.begin(); it != p_segment->tracks.end(); ++it )
788 {
789 mkv_track_t &track = *it->second;
790
791 if( track.i_last_dts == VLC_TS_INVALID )
792 continue;
793
794 if( track.fmt.i_cat != VIDEO_ES && track.fmt.i_cat != AUDIO_ES )
795 continue;
796
797 if( track.i_last_dts < i_pcr || i_pcr <= VLC_TS_INVALID )
798 {
799 i_pcr = track.i_last_dts;
800 }
801 }
802
803 if( i_pcr > VLC_TS_INVALID && i_pcr > p_sys->i_pcr )
804 {
805 if( es_out_SetPCR( p_demux->out, i_pcr ) )
806 {
807 msg_Err( p_demux, "ES_OUT_SET_PCR failed, aborting." );
808 delete block;
809 delete additions;
810 return 0;
811 }
812
813 p_sys->i_pcr = i_pcr;
814 }
815 }
816
817 /* set pts */
818 {
819 p_sys->i_pts = p_sys->i_mk_chapter_time + VLC_TS_0;
820
821 if( simpleblock != NULL ) p_sys->i_pts += simpleblock->GlobalTimecode() / INT64_C( 1000 );
822 else p_sys->i_pts += block->GlobalTimecode() / INT64_C( 1000 );
823 }
824
825 if ( p_vsegment->CurrentEdition() &&
826 p_vsegment->CurrentEdition()->b_ordered &&
827 p_vsegment->CurrentChapter() == NULL )
828 {
829 /* nothing left to read in this ordered edition */
830 delete block;
831 delete additions;
832 return 0;
833 }
834
835 BlockDecode( p_demux, block, simpleblock, additions,
836 p_sys->i_pts, i_block_duration, b_key_picture, b_discardable_picture );
837
838 delete block;
839 delete additions;
840
841 return 1;
842 }
843
mkv_track_t(enum es_format_category_e es_cat)844 mkv_track_t::mkv_track_t(enum es_format_category_e es_cat) :
845 b_default(true)
846 ,b_enabled(true)
847 ,b_forced(false)
848 ,i_number(0)
849 ,i_extra_data(0)
850 ,p_extra_data(NULL)
851 ,b_dts_only(false)
852 ,b_pts_only(false)
853 ,b_no_duration(false)
854 ,i_default_duration(0)
855 ,f_timecodescale(1.0)
856 ,i_last_dts(VLC_TS_INVALID)
857 ,i_skip_until_fpos(std::numeric_limits<uint64_t>::max())
858 ,f_fps(0)
859 ,p_es(NULL)
860 ,i_original_rate(0)
861 ,i_chans_to_reorder(0)
862 ,p_sys(NULL)
863 ,b_discontinuity(false)
864 ,i_compression_type(MATROSKA_COMPRESSION_NONE)
865 ,i_encoding_scope(MATROSKA_ENCODING_SCOPE_ALL_FRAMES)
866 ,p_compression_data(NULL)
867 ,i_seek_preroll(0)
868 ,i_codec_delay(0)
869 {
870 std::memset( &pi_chan_table, 0, sizeof( pi_chan_table ) );
871
872 es_format_Init(&fmt, es_cat, 0);
873
874 switch( es_cat )
875 {
876 case AUDIO_ES:
877 fmt.audio.i_channels = 1;
878 fmt.audio.i_rate = 8000;
879 /* fall through */
880 case VIDEO_ES:
881 case SPU_ES:
882 fmt.psz_language = strdup("English");
883 break;
884 default:
885 // no language needed
886 break;
887 }
888 }
889
~mkv_track_t()890 mkv_track_t::~mkv_track_t()
891 {
892 es_format_Clean( &fmt );
893 assert(p_es == NULL); // did we leak an ES ?
894
895 free(p_extra_data);
896
897 delete p_compression_data;
898 delete p_sys;
899 }
900
matroska_stream_c(stream_t * s,bool owner)901 matroska_stream_c::matroska_stream_c( stream_t *s, bool owner )
902 :io_callback( new vlc_stream_io_callback( s, owner ) )
903 ,estream( EbmlStream( *io_callback ) )
904 {}
905
isUsed() const906 bool matroska_stream_c::isUsed() const
907 {
908 for( size_t j = 0; j < segments.size(); j++ )
909 {
910 if( segments[j]->b_preloaded )
911 return true;
912 }
913 return false;
914 }
915