-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMatroskaMuxer.cpp
845 lines (678 loc) · 25.6 KB
/
MatroskaMuxer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
/*
* General Matroska Muxer Interface
*
* Copyright (C) 2003-2004, Jory Stone <jcsston @ toughguy.net>
* Copyright (C) 2003, John Cannon <spyder482 @ yahoo.com>
* Copyright (C) 2002, Steve Lhomme <steve.lhomme @ free.fr>
*
* SOMa is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* SOMa is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SOMa; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*!
\version $Id: MatroskaMuxer.cpp,v 1.1 2004/03/24 05:46:51 jcsston Exp $
\author Jory Stone <jcsston @ toughguy.net>
\brief
\par CVS Log
$Log: MatroskaMuxer.cpp,v $
Revision 1.1 2004/03/24 05:46:51 jcsston
Initial Commit
Revision 1.2 2004/01/04 04:16:01 jcsston
It actual works!
Revision 1.1 2004/01/04 01:07:31 jcsston
Finally some matroska muxing code
*/
#include "MatroskaMuxer.h"
using namespace LIBEBML_NAMESPACE;
using namespace LIBMATROSKA_NAMESPACE;
MatroskaMuxer::MatroskaMuxer()
{
m_outputFile = NULL;
framesMuxed = 0;
clustersMuxed = 0;
m_LaceCount = 0;
currentClusterBaseTimecode = 0;
currentClusterSize = 0;
MySeekHead = &GetChild<KaxSeekHead>(FileSegment);
MyDummy = &GetChild<EbmlVoid>(FileSegment);
// Setup the track element
m_Tracks = &GetChild<KaxTracks>(FileSegment);
MySeekHeadOverFill = &GetChild<KaxSeekHead>(FileSegment);
m_UseLacing = true;
m_WriteDuration = false;
clust = NULL;
m_MyKaxBlockGroup = NULL;
m_AllCues = NULL;
m_prevTimecode = 0xFFFFFFFFFFFFFFFF;
m_prevTrack = 0xFFFF;
m_lastTimecode = 0;
m_timecodeScale = TIMECODE_SCALE;
Set_ClusterLimits();
Set_WritingApp(L"C++ Interface");
};
MatroskaMuxer::~MatroskaMuxer() {
};
int MatroskaMuxer::Set_OutputFilename(const std::string &ouputFilename)
{
if (m_outputFile != NULL)
delete m_outputFile;
m_outputFile = new FileIOCallback(ouputFilename.c_str(), MODE_WRITE);
if (m_outputFile == NULL)
return -1;
return 0;
}
int MatroskaMuxer::Set_WritingApp(const UTFstring &writingApp)
{
m_WritingApp.SetUTF8(writingApp.GetUTF8() + " - MatroskaMuxer 1.0");
return 0;
}
int MatroskaMuxer::Set_Lacing(bool bUseLacing)
{
m_UseLacing = bUseLacing;
return 0;
};
int MatroskaMuxer::Set_WriteDuration(bool bWriteDuration)
{
m_WriteDuration = bWriteDuration;
return 0;
};
int MatroskaMuxer::Set_ClusterLimits(uint32 maxKBSize, uint32 maxMSLength)
{
m_maxBytesPerCluster = maxKBSize * 1024;
m_maxTimePerCluster = maxMSLength;
return 0;
};
int MatroskaMuxer::Set_TimecodeScale(uint64 timecodeScale)
{
m_timecodeScale = timecodeScale;
unsigned int Index1;
for (Index1 = 0; Index1 < m_Tracks->ListSize(); Index1++)
((KaxTrackEntry *)(*m_Tracks)[Index1])->SetGlobalTimecodeScale(m_timecodeScale);
Set_ClusterLimits();
return 0;
};
int MatroskaMuxer::Set_Track_Type(uint8 trackNo, int trackType)
{
KaxTrackEntry *track = _LookupTrackNo(trackNo);
if (track == NULL)
return -1;
*(static_cast<EbmlUInteger *>(&GetChild<KaxTrackType>(*track))) = trackType;
if (m_UseLacing && (trackType == track_audio))
Set_Track_Lacing(trackNo, true);
else
Set_Track_Lacing(trackNo, false);
return 0;
};
int MatroskaMuxer::Set_Track_CodecID(uint8 trackNo, const std::string &codecID)
{
KaxTrackEntry *track = _LookupTrackNo(trackNo);
if (track == NULL)
return -1;
*(static_cast<EbmlString *>(&GetChild<KaxCodecID>(*track))) = codecID;
return 0;
};
int MatroskaMuxer::Set_Track_CodecPrivate(uint8 trackNo, const binary *codecPrivate, uint32 codecPrivateSize)
{
KaxTrackEntry *track = _LookupTrackNo(trackNo);
if (track == NULL)
return -1;
KaxCodecPrivate &codecP = GetChild<KaxCodecPrivate>(*track);
codecP.CopyBuffer(codecPrivate, codecPrivateSize);
return 0;
};
int MatroskaMuxer::Set_Track_DefaultDuration(uint8 trackNo, uint64 defaultDuration)
{
KaxTrackEntry *track = _LookupTrackNo(trackNo);
if (track == NULL)
return -1;
*(static_cast<EbmlUInteger *>(&GetChild<KaxTrackDefaultDuration>(*track))) = defaultDuration;
return 0;
};
int MatroskaMuxer::Set_Track_Language(uint8 trackNo, const std::string &lang)
{
KaxTrackEntry *track = _LookupTrackNo(trackNo);
if (track == NULL)
return -1;
*(static_cast<EbmlString *>(&GetChild<KaxTrackLanguage>(*track))) = lang;
return 0;
};
int MatroskaMuxer::Set_Track_Video(uint8 trackNo, uint32 width, uint32 height, uint32 displayWidth, uint32 displayHeight)
{
KaxTrackEntry *track = _LookupTrackNo(trackNo);
if (track == NULL)
return -1;
Set_Track_Type(trackNo, track_video);
KaxTrackVideo & MyTrackVideo = GetChild<KaxTrackVideo>(*track);
if (width != 0) {
KaxVideoPixelWidth & MyTrackVideoPixelWidth = GetChild<KaxVideoPixelWidth>(MyTrackVideo);
*static_cast<EbmlUInteger *>(&MyTrackVideoPixelWidth) = width;
}
if (height != 0) {
KaxVideoPixelHeight & MyTrackVideoPixelHeight = GetChild<KaxVideoPixelHeight>(MyTrackVideo);
*static_cast<EbmlUInteger *>(&MyTrackVideoPixelHeight) = height;
}
// Get ready to write the display sizes
KaxVideoDisplayWidth & MyTrackVideoDisplayWidth = GetChild<KaxVideoDisplayWidth>(MyTrackVideo);
KaxVideoDisplayHeight & MyTrackVideoDisplayHeight = GetChild<KaxVideoDisplayHeight>(MyTrackVideo);
// Use the PixelWidth if the DisplayWidth is not set
if (displayWidth != 0) {
*static_cast<EbmlUInteger *>(&MyTrackVideoDisplayWidth) = displayWidth;
} else {
*static_cast<EbmlUInteger *>(&MyTrackVideoDisplayWidth) = width;
}
// Do the same here, except for the Height
if (displayHeight != 0) {
*static_cast<EbmlUInteger *>(&MyTrackVideoDisplayHeight) = displayHeight;
} else {
*static_cast<EbmlUInteger *>(&MyTrackVideoDisplayHeight) = height;
}
return 0;
};
int MatroskaMuxer::Set_Track_Audio(uint8 trackNo, uint8 channels, double sampleRate, uint16 bitdepth, double outputSampleRate)
{
KaxTrackEntry *track = _LookupTrackNo(trackNo);
if (track == NULL)
return -1;
Set_Track_Type(trackNo, track_audio);
KaxTrackAudio &MyTrackAudio = GetChild<KaxTrackAudio>(*track);
if (channels != 0) {
KaxAudioChannels & MyTrackAudioChannels = GetChild<KaxAudioChannels>(MyTrackAudio);
*(static_cast<EbmlUInteger *>(&MyTrackAudioChannels)) = channels;
}
if (sampleRate != 0) {
KaxAudioSamplingFreq & MyTrackFreq = GetChild<KaxAudioSamplingFreq>(MyTrackAudio);
*(static_cast<EbmlFloat *>(&MyTrackFreq)) = sampleRate;
MyTrackFreq.ValidateSize();
}
if (outputSampleRate != 0) {
KaxAudioOutputSamplingFreq & MyTrackAudioOutputSamplingFreq = GetChild<KaxAudioOutputSamplingFreq>(MyTrackAudio);
*(static_cast<EbmlFloat *>(&MyTrackAudioOutputSamplingFreq)) = outputSampleRate;
MyTrackAudioOutputSamplingFreq.ValidateSize();
}
if (bitdepth != 0) {
KaxAudioBitDepth & MyTrackAudioBitDepth = GetChild<KaxAudioBitDepth>(MyTrackAudio);
*(static_cast<EbmlUInteger *>(&MyTrackAudioBitDepth)) = bitdepth;
}
return 0;
};
int MatroskaMuxer::Set_Track_Lacing(uint8 trackNo, bool bUseLacing)
{
KaxTrackEntry *track = _LookupTrackNo(trackNo);
if (track == NULL)
return -1;
*(static_cast<EbmlUInteger *>(&GetChild<KaxTrackFlagLacing>(*track))) = bUseLacing;
return 0;
};
int MatroskaMuxer::Set_Tag(uint8 trackNo, const char *name, const char *value)
{
MatroskaTagInfo *currentTags = NULL;
if (trackNo > 0) {
KaxTrackEntry *entry = _LookupTrackNo(trackNo);
if (entry == NULL)
return -1;
KaxTrackUID &trackUID = GetChild<KaxTrackUID>(*entry);
currentTags = m_Tags.FindTagWithTrackUID((uint64)trackUID);
if (currentTags == NULL) {
MatroskaTagInfo tempTag;
tempTag.targetTrackUID = (uint64)trackUID;
m_Tags.push_back(tempTag);
currentTags = &m_Tags.at(m_Tags.size()-1);
}
} else {
currentTags = m_Tags.FindTagWithTrackUID(0);
if (currentTags == NULL) {
MatroskaTagInfo tempTag;
tempTag.targetTrackUID = 0;
m_Tags.push_back(tempTag);
currentTags = &m_Tags.at(m_Tags.size()-1);
}
}
currentTags->SetTagValue(name, value);
return 0;
};
int MatroskaMuxer::Get_Track_CodecID(uint8 trackNo, std::string &codecID)
{
KaxTrackEntry *track = _LookupTrackNo(trackNo);
if (track == NULL)
return -1;
codecID = *(static_cast<EbmlString *>(&GetChild<KaxCodecID>(*track)));
return 0;
}
KaxTrackEntry *MatroskaMuxer::_LookupTrackNo(uint8 trackNo)
{
KaxTrackEntry *entry = NULL;
if (trackNo == 0)
return NULL;
trackNo--;
if (m_Tracks->ListSize() > trackNo) {
entry = static_cast<KaxTrackEntry *>((*m_Tracks)[trackNo]);
assert(entry != NULL);
} else {
if (m_Tracks->ListSize() > 0) {
entry = &GetNextChild<KaxTrackEntry>(*m_Tracks, static_cast<KaxTrackEntry &>(*(*m_Tracks)[m_Tracks->ListSize()-1]));
} else {
entry = &GetChild<KaxTrackEntry>(*m_Tracks);
}
assert(entry != NULL);
KaxTrackNumber & MyTrack1Number = GetChild<KaxTrackNumber>(*entry);
*(static_cast<EbmlUInteger *>(&MyTrack1Number)) = trackNo + 1;
KaxTrackUID & MyKaxTrack1UID = GetChild<KaxTrackUID>(*entry);
*(static_cast<EbmlUInteger *>(&MyKaxTrack1UID)) = rand();
*(static_cast<EbmlString *>(&GetChild<KaxTrackLanguage>(*entry))) = "eng";
entry->SetGlobalTimecodeScale(m_timecodeScale);
m_Queue.resize(trackNo+1);
for (size_t it = 0; it < m_Queue.size(); it++)
m_Queue.at(it).trackNo = it+1;
}
return entry;
};
bool MatroskaMuxer::WriteHeaders() {
try {
//wxLogStatus(_T("MatroskaMuxer::WriteHeaders()"));
// write output file
EbmlHead FileHead;
EDocType & MyDocType = GetChild<EDocType>(FileHead);
*static_cast<EbmlString *>(&MyDocType) = "matroska";
EDocTypeVersion & MyDocTypeVer = GetChild<EDocTypeVersion>(FileHead);
*(static_cast<EbmlUInteger *>(&MyDocTypeVer)) = 1;
EDocTypeReadVersion & MyDocTypeReadVer = GetChild<EDocTypeReadVersion>(FileHead);
*(static_cast<EbmlUInteger *>(&MyDocTypeReadVer)) = 1;
FileHead.Render(*m_outputFile);
// Total segment size is unknown and will always be
// So we alloc a fixed amount of space (5 bytes) that we can render right away
uint64 SegmentSize = FileSegment.WriteHead(*m_outputFile, 5);
// Write a dummy element to reserve space for the seekhead
MyDummy->SetSize(2000);
MyDummy->Render(*m_outputFile);
//Add the Info segment
KaxInfo & MyInfos = GetChild<KaxInfo>(FileSegment);
//Add info tag for writing app
KaxWritingApp & MyKaxWritingApp = GetChild<KaxWritingApp>(MyInfos);
*static_cast<EbmlUnicodeString *>(&MyKaxWritingApp) = m_WritingApp.c_str();
std::string muxingapp;
muxingapp.append("libebml v");
muxingapp.append(LIBEBML_NAMESPACE::EbmlCodeVersion);
muxingapp.append(" + libmatroska v");
muxingapp.append(LIBMATROSKA_NAMESPACE::KaxCodeVersion);
UTFstring muxingappW;
muxingappW.SetUTF8(muxingapp);
KaxMuxingApp & MyKaxMuxingApp = GetChild<KaxMuxingApp>(MyInfos);
*static_cast<EbmlUnicodeString *>(&MyKaxMuxingApp) = muxingappW.c_str();
KaxDateUTC &MyKaxDateUTC = GetChild<KaxDateUTC>(MyInfos);
MyKaxDateUTC.SetEpochDate(time(NULL));
//Add timecode scale
KaxTimecodeScale & TimeScale = GetChild<KaxTimecodeScale>(MyInfos);
*(static_cast<EbmlUInteger *>(&TimeScale)) = m_timecodeScale;
KaxDuration &Duration = GetChild<KaxDuration>(MyInfos);
*(static_cast<EbmlFloat *>(&Duration)) = 10000;
KaxSegmentUID & MyKaxSegmentUID = GetChild<KaxSegmentUID>(MyInfos);
// Now we need to make a 128-bits UID
uint32 *segmentUID = new uint32[4];
srand((unsigned int)time(NULL));
segmentUID[0] = rand();
srand((unsigned int)time(NULL));
segmentUID[1] = rand();
srand((unsigned int)time(NULL));
segmentUID[2] = rand();
srand((unsigned int)time(NULL));
segmentUID[3] = rand();
MyKaxSegmentUID.SetBuffer((const binary *)segmentUID, sizeof(segmentUID)*4);
uint32 InfoSize = MyInfos.Render(*m_outputFile);
MySeekHead->IndexThis(MyInfos, FileSegment);
// Setup the Cues but don't render them yet
m_AllCues = &GetChild<KaxCues>(FileSegment);
m_AllCues->SetGlobalTimecodeScale(m_timecodeScale);
uint64 TrackSize = m_Tracks->Render(*m_outputFile);
MySeekHead->IndexThis(*m_Tracks, FileSegment);
return true;
}catch (...) {
//wxLogMessage(_T("MatroskaMuxer: Failed to write headers"));
return false;
}
return false;
};
bool MatroskaMuxer::_WriteAttachments() {
/* try {
//wxLogStatus(_T("Matroska Writer: Writing Attachments..."));
if (inputAttachments->size() > 0) {
KaxAttachments & MyAttachments = GetChild<KaxAttachments>(FileSegment);
KaxAttached *lastAttachment = NULL;
for (uint16 a = 0; a < inputAttachments->size(); a++) {
MatroskaAttachment *currentAttachment = inputAttachments->at(a);
KaxAttached *MyAttachedFile = NULL;
if (lastAttachment != NULL) {
MyAttachedFile = &GetNextChild<KaxAttached>(MyAttachments, *lastAttachment);
lastAttachment = MyAttachedFile;
} else {
MyAttachedFile = &GetChild<KaxAttached>(MyAttachments);
lastAttachment = MyAttachedFile;
}
KaxFileUID & MyKaxFileUID = GetChild<KaxFileUID>(*MyAttachedFile);
*static_cast<EbmlUInteger *>(&MyKaxFileUID) = currentAttachment->UID;
KaxFileName & MyAttachedFile_Filename = GetChild<KaxFileName>(*MyAttachedFile);
*static_cast<EbmlUnicodeString *>(&MyAttachedFile_Filename) = (const wchar_t*)wxConvLibc.cWX2WC(currentAttachment->FileName.c_str());
KaxMimeType & MyAttachedFile_MimieType = GetChild<KaxMimeType>(*MyAttachedFile);
*static_cast<EbmlString *>(&MyAttachedFile_MimieType) = (const char *)wxConvLibc.cWX2MB(currentAttachment->MimeType.c_str());
KaxFileDescription & MyKaxFileDescription = GetChild<KaxFileDescription>(*MyAttachedFile);
*static_cast<EbmlUnicodeString *>(&MyKaxFileDescription) = (const wchar_t*)wxConvLibc.cWX2WC(currentAttachment->Description.c_str());
KaxFileData & MyAttachedFile_FileData = GetChild<KaxFileData>(*MyAttachedFile);
MyAttachedFile_FileData.CopyBuffer(currentAttachment->GetAttachmentData(), currentAttachment->SourceDataLength);
}
MyAttachments.Render(*m_outputFile);
}
return true;
} catch (std::exception &ex) {
wxString errMsg;
errMsg = _T("Matroska Writer: std::exception in MatroskaWriter::WriteAttachments() - ");
errMsg += wxString(ex.what(), wxConvUTF8);
wxLogError(errMsg);
}catch (...) {
wxLogError(_T("Matroska Writer: Failed to write attachments."));
return false;
}*/
return false;
};
bool MatroskaMuxer::_WriteTags() {
try {
//wxLogStatus(_T("MatroskaMuxer::WriteTags()"));
KaxTags & MyKaxTags = GetChild<KaxTags>(FileSegment);
//Start going through the list and adding tags
KaxTag *MyKaxTag_last = NULL;
for(size_t current_tag_track = 0; current_tag_track < m_Tags.size(); current_tag_track++) {
KaxTag *MyKaxTag = NULL;
if (MyKaxTag_last != NULL)
{
MyKaxTag = &GetNextChild<KaxTag>(MyKaxTags, *MyKaxTag_last);
MyKaxTag_last = MyKaxTag;
}else {
MyKaxTag = &GetChild<KaxTag>(MyKaxTags);
MyKaxTag_last = MyKaxTag;
}
MatroskaTagInfo ¤tTag = m_Tags.at(current_tag_track);
//The Targets group
KaxTagTargets & MyKaxTagTargets = GetChild<KaxTagTargets>(*MyKaxTag);
if (currentTag.targetTrackUID != 0) {
KaxTagTrackUID & MyKaxTagTrackUID = GetChild<KaxTagTrackUID>(MyKaxTagTargets);
*static_cast<EbmlUInteger *>(&MyKaxTagTrackUID) = currentTag.targetTrackUID;
}
if (currentTag.targetChapterUID != 0) {
KaxTagChapterUID & MyKaxTagChapterUID = GetChild<KaxTagChapterUID>(MyKaxTagTargets);
*static_cast<EbmlUInteger *>(&MyKaxTagChapterUID) = currentTag.targetChapterUID;
}
if (currentTag.targetAttachmentUID != 0) {
KaxTagAttachmentUID & MyKaxTagAttachmentUID = GetChild<KaxTagAttachmentUID>(MyKaxTagTargets);
*static_cast<EbmlUInteger *>(&MyKaxTagAttachmentUID) = currentTag.targetAttachmentUID;
}
// Add the millions of simple tags we can have ;)
KaxTagSimple *MySimpleTag_last = NULL;
for(size_t st = 0; st < currentTag.tags.size(); st++) {
KaxTagSimple *MySimpleTag = NULL;
if (MySimpleTag_last != NULL)
{
MySimpleTag = &GetNextChild<KaxTagSimple>(*MyKaxTag, *MySimpleTag_last);
MySimpleTag_last = MySimpleTag;
}else {
MySimpleTag = &GetChild<KaxTagSimple>(*MyKaxTag);
MySimpleTag_last = MySimpleTag;
}
MatroskaSimpleTag ¤tSimpleTag = currentTag.tags.at(st);
KaxTagName & MyKaxTagName = GetChild<KaxTagName>(*MySimpleTag);
*static_cast<EbmlUnicodeString *>(&MyKaxTagName) = currentSimpleTag.name;
KaxTagString & MyKaxTagString = GetChild<KaxTagString>(*MySimpleTag);
*static_cast<EbmlUnicodeString *>(&MyKaxTagString) = currentSimpleTag.value;
}
}
MyKaxTags.Render(*m_outputFile);
MySeekHead->IndexThis(MyKaxTags, FileSegment);
return true;
} catch (std::exception &ex) {
//wxString errMsg;
//errMsg = _T("Matroska Writer: std::exception in MatroskaWriter::WriteTags() - ");
//errMsg += wxString(ex.what(), wxConvUTF8);
//wxLogError(errMsg);
}catch (...) {
//wxLogError(_T("Matroska Writer: Failed to write tags."));
}
return false;
};
int MatroskaMuxer::_UpdateDuration(double newDuration)
{
KaxInfo & MyInfos = GetChild<KaxInfo>(FileSegment);
KaxDuration &Duration = GetChild<KaxDuration>(MyInfos);
*(static_cast<EbmlFloat *>(&Duration)) = newDuration;
MyInfos.UpdateSize();
uint64 origPos = m_outputFile->getFilePointer();
m_outputFile->setFilePointer(MyInfos.GetElementPosition());
MyInfos.Render(*m_outputFile);
m_outputFile->setFilePointer(origPos);
return 0;
};
int MatroskaMuxer::_AddFrame(uint16 trackNo, uint64 timecode, uint32 duration, SimpleDataBuffer *data, std::vector<int32> *references) {
KaxTrackEntry *track = _LookupTrackNo(trackNo);
if (track == NULL)
return -1;
_ChangeClusters(timecode);
if ((m_MyKaxBlockGroup == NULL) || (m_prevTrack != trackNo) || (m_LaceCount)) {
m_MyKaxBlockGroup = &clust->GetNewBlock();
m_LaceCount = 0;
m_MyKaxBlockGroup->SetParent(*clust);
}
KaxBlock &MyKaxBlock = GetChild<KaxBlock>(*m_MyKaxBlockGroup);
MyKaxBlock.SetParent(*clust);
uint8 UseLacing = (uint8)GetChild<KaxTrackFlagLacing>(*track);
if (UseLacing)
m_LaceCount = !MyKaxBlock.AddFrame(*track, timecode * m_timecodeScale, *data, LACING_AUTO);
else
MyKaxBlock.AddFrame(*track, timecode * m_timecodeScale, *data, LACING_NONE);
currentClusterSize += data->Size();
if (UseLacing) {
m_prevTrack = trackNo;
}
if (timecode+duration > m_lastTimecode)
m_lastTimecode = timecode+duration;
// We only need to add the BlockDuration element if the Block Duration is different from the default
if (m_WriteDuration && (duration != 0) && (duration != (uint32)GetChild<KaxTrackDefaultDuration>(*track))) {
KaxBlockDuration &MyKaxBlockDuration = GetChild<KaxBlockDuration>(*m_MyKaxBlockGroup);
*(static_cast<EbmlUInteger *>(&MyKaxBlockDuration)) = duration;
}
if ((references != NULL) && (references->size() > 0)) {
KaxTimecodeReferenceBlock &MyKaxReferenceBlock = GetChild<KaxTimecodeReferenceBlock>(*m_MyKaxBlockGroup);
MyKaxReferenceBlock.SetTimecode(references->at(0));
// More than one reference?
if (references->size() > 1) {
KaxTimecodeReferenceBlock *lastReferenceBlock = &MyKaxReferenceBlock;
for (size_t ref = 1; ref < references->size(); ref++) {
KaxTimecodeReferenceBlock *newReferenceBlock = &GetNextChild<KaxTimecodeReferenceBlock>(*m_MyKaxBlockGroup, *lastReferenceBlock);
newReferenceBlock->SetTimecode(references->at(ref));
lastReferenceBlock = newReferenceBlock;
}
}
} else {
if ((uint8)GetChild<KaxTrackType>(*track) == track_video)
m_AllCues->AddBlockGroup(*m_MyKaxBlockGroup);
}
return 0;
};
int MatroskaMuxer::AddFrame(uint16 trackNo, uint64 timecode, uint32 duration, const void *data, uint32 dataSize, int32 reference1, int32 reference2)
{
if ((trackNo > 0) && (trackNo <= m_Queue.size()))
{
MatroskaFrameQueue &trackQueue = m_Queue.at(trackNo-1);
MatroskaFrame *frame = new MatroskaFrame();
frame->timecode = timecode;
frame->duration = duration;
if (reference1 != 0) {
if (reference1 == REFERENCE_PREV_FRAME) {
if (!trackQueue.empty()) {
MatroskaFrame *prevFrame = trackQueue.front();
reference1 = (int64)prevFrame->timecode - (int64)timecode;
frame->references.push_back(reference1);
}
} else {
frame->references.push_back(reference1);
}
}
if (reference2 != 0)
frame->references.push_back(reference2);
binary *buffer = new binary[dataSize];
memcpy(buffer, data, dataSize);
frame->buffer = new SimpleDataBuffer(buffer, dataSize, 0);
trackQueue.push(frame);
while (_ProcessQueue() != -1);
return 0;
}
return -1;
};
int MatroskaMuxer::_ProcessQueue()
{
MatroskaFrame *nextFrame = NULL;
MatroskaFrameQueue *sourceQueue = NULL;
// Go through the track queues
for (size_t it = 0; it < m_Queue.size(); it++) {
MatroskaFrameQueue *inQueue = &m_Queue.at(it);
// If queue has at least 1+ frames in the queue
if (inQueue->size() > 1) {
// Check if we NEED a frame or the frame in the queue has a earlier timecode than the frame we have
if ((nextFrame == NULL) ||
(((MatroskaFrame *)inQueue->front())->timecode < nextFrame->timecode))
{
// It does, so we store this frame
nextFrame = ((MatroskaFrame *)inQueue->front());
// Store the source track queue (so we can take this frame out of the tracks queue)
sourceQueue = inQueue;
}
}
}
if (nextFrame != NULL) {
// Ok we have the frame with the smallest timecode
// Take it out of it's source track queue
sourceQueue->pop();
int ret = _AddFrame(sourceQueue->trackNo, nextFrame->timecode, nextFrame->duration, nextFrame->buffer, &nextFrame->references);
delete nextFrame;
return ret;
} else {
// No more frames
return -1;
}
return 0;
}
int MatroskaMuxer::_FlushQueue()
{
MatroskaFrame *nextFrame = NULL;
MatroskaFrameQueue *sourceQueue = NULL;
bool bFlushed = false;
while (!bFlushed) {
// Go through the track queues
for (size_t it = 0; it < m_Queue.size(); it++) {
MatroskaFrameQueue *inQueue = &m_Queue.at(it);
// If queue has at least 1+ frames in the queue
if (inQueue->size() > 0) {
// Check if we NEED a frame or the frame in the queue has a earlier timecode than the frame we have
if ((nextFrame == NULL) ||
(((MatroskaFrame *)inQueue->front())->timecode < nextFrame->timecode))
{
// It does, so we store this frame
nextFrame = ((MatroskaFrame *)inQueue->front());
// Store the source track queue (so we can take this frame out of the tracks queue)
sourceQueue = inQueue;
}
}
}
if (nextFrame != NULL) {
// Ok we have the frame with the smallest timecode
// Take it out of it's source track queue
sourceQueue->pop();
int ret = _AddFrame(sourceQueue->trackNo, nextFrame->timecode, nextFrame->duration, nextFrame->buffer, &nextFrame->references);
delete nextFrame;
nextFrame = NULL;
} else {
// No more frames
bFlushed = true;
}
}
return 0;
}
bool MatroskaMuxer::_ChangeClusters(uint64 timecode, bool bForce) {
if (!clust || (bForce || ((timecode - currentClusterBaseTimecode) >= m_maxTimePerCluster) || (currentClusterSize >= m_maxBytesPerCluster))) {
#ifdef _DEBUG_CLUSTER_LOG
if ((timecode - currentClusterBaseTimecode) >= m_maxTimePerCluster) {
printf("Limited by time");
}
if (currentClusterSize >= m_maxBytesPerCluster) {
printf("Limited by size");
}
#endif
//Preform the cluster cleanup
if (clust) {
clust->Render(*m_outputFile, *m_AllCues);
clust->ReleaseFrames();
// We leave 75 bytes of space reserved for the first seek head
// Each entry is about 15 bytes, so we leave space for 4 entries to fit easily
if (MySeekHead->GetSize()+75 < MyDummy->GetSize()) {
MySeekHead->IndexThis(*clust, FileSegment);
MySeekHead->UpdateSize();
} else {
// The first one is full, we add the clusters to the linked seekhead, at the end of the file
MySeekHeadOverFill->IndexThis(*clust, FileSegment);
}
delete clust;
clust = NULL;
currentClusterSize = 0;
// We have to start a new blockgroup
m_MyKaxBlockGroup = NULL;
}
++clustersMuxed;
clust = new KaxCluster();
clust->SetParent(FileSegment);
clust->SetGlobalTimecodeScale(m_timecodeScale);
clust->SetPreviousTimecode(currentClusterBaseTimecode, m_timecodeScale);
clust->InitTimecode(timecode, m_timecodeScale);
//Set the timecode for this cluster
KaxClusterTimecode & MyClusterTimeCode = GetChild<KaxClusterTimecode>(*clust);
*(static_cast<EbmlUInteger *>(&MyClusterTimeCode)) = timecode;// * m_timecodeScale;
//Store the starting timecode of this cluster
currentClusterBaseTimecode = timecode;
}
return true;
};
int MatroskaMuxer::CloseFile() {
//wxLogStatus(_T("MatroskaMuxer::CloseFile()"));
_FlushQueue();
if (clust) {
clust->Render(*m_outputFile, *m_AllCues);
clust->ReleaseFrames();
delete clust;
clust = NULL;
}
if ((m_AllCues != NULL) && (m_AllCues->ListSize() > 0)) {
m_AllCues->Render(*m_outputFile);
MySeekHead->IndexThis(*m_AllCues, FileSegment);
}
_UpdateDuration((double)(int64)m_lastTimecode);
// If we never used the Overfill seekhead there isn't much use of rendering it :P
if (MySeekHeadOverFill->ListSize() != 0) {
MySeekHeadOverFill->Render(*m_outputFile);
// Index the seekhead that is at the end in the one at the beginning
// creating a 'linked-list' type of seekheads
MySeekHead->IndexThis(*MySeekHeadOverFill, FileSegment);
}
_WriteTags();
MyDummy->ReplaceWith(*MySeekHead, *m_outputFile);
m_outputFile->setFilePointer(0, seek_end);
if (FileSegment.ForceSize(m_outputFile->getFilePointer() -
FileSegment.GetElementPosition() -
FileSegment.HeadSize()))
FileSegment.OverwriteHead(*m_outputFile);
return 0;
};