forked from cinecert/asdcplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MXF.cpp
executable file
·2112 lines (1772 loc) · 61.3 KB
/
MXF.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
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2005-2022, John Hurst
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*! \file MXF.cpp
\version $Id$
\brief MXF objects
*/
#include "MXF.h"
#include "Metadata.h"
#include <KM_log.h>
using Kumu::DefaultLogSink;
using Kumu::GenRandomValue;
// index segments must be < 64K
// NOTE: this value may too high if advanced index entry elements are used.
const ui32_t CBRIndexEntriesPerSegment = 5000;
//------------------------------------------------------------------------------------------
//
//
ASDCP::Result_t
ASDCP::MXF::SeekToRIP(const Kumu::IFileReader& Reader)
{
Kumu::fpos_t end_pos;
// go to the end - 4 bytes
Result_t result = Reader.Seek(0, Kumu::SP_END);
if ( ASDCP_SUCCESS(result) )
result = Reader.Tell(&end_pos);
if ( ASDCP_SUCCESS(result)
&& end_pos < (SMPTE_UL_LENGTH+MXF_BER_LENGTH) )
{
DefaultLogSink().Error("File is smaller than an empty KLV packet.\n");
result = RESULT_FAIL;
}
if ( ASDCP_SUCCESS(result) )
result = Reader.Seek(end_pos - 4);
// get the ui32_t RIP length
ui32_t read_count;
byte_t intbuf[MXF_BER_LENGTH];
ui32_t rip_size = 0;
if ( ASDCP_SUCCESS(result) )
{
result = Reader.Read(intbuf, MXF_BER_LENGTH, &read_count);
if ( ASDCP_SUCCESS(result) && read_count != 4 )
{
DefaultLogSink().Error("RIP contains fewer than four bytes.\n");
result = RESULT_FAIL;
}
}
if ( ASDCP_SUCCESS(result) )
{
rip_size = KM_i32_BE(Kumu::cp2i<ui32_t>(intbuf));
if ( rip_size > end_pos ) // RIP can't be bigger than the file
{
DefaultLogSink().Error("RIP size impossibly large.\n");
return RESULT_FAIL;
}
}
// reposition to start of RIP
if ( ASDCP_SUCCESS(result) )
result = Reader.Seek(end_pos - rip_size);
return result;
}
//
bool
ASDCP::MXF::RIP::GetPairBySID(ui32_t SID, PartitionPair& outPair) const
{
RIP::const_pair_iterator i;
for ( i = PairArray.begin(); i != PairArray.end(); ++i )
{
if ( i->BodySID == SID )
{
outPair = *i;
return true;
}
}
return false;
}
//
ASDCP::Result_t
ASDCP::MXF::RIP::InitFromFile(const Kumu::IFileReader& Reader)
{
assert(m_Dict);
Result_t result = KLVFilePacket::InitFromFile(Reader, m_Dict->ul(MDD_RandomIndexMetadata));
if ( ASDCP_SUCCESS(result) )
{
if (m_ValueLength < 4)
{
DefaultLogSink().Error("RIP is too short.\n");
return RESULT_KLV_CODING(__LINE__, __FILE__);
}
Kumu::MemIOReader MemRDR(m_ValueStart, m_ValueLength - 4);
result = PairArray.Unarchive(&MemRDR) ? RESULT_OK : RESULT_KLV_CODING(__LINE__, __FILE__);
}
if ( ASDCP_FAILURE(result) )
DefaultLogSink().Error("Failed to initialize RIP.\n");
return result;
}
//
ASDCP::Result_t
ASDCP::MXF::RIP::WriteToFile(Kumu::FileWriter& Writer)
{
assert(m_Dict);
ASDCP::FrameBuffer Buffer;
ui32_t RIPSize = ( PairArray.size() * (sizeof(ui32_t) + sizeof(ui64_t)) ) + 4;
Result_t result = Buffer.Capacity(RIPSize);
if ( ASDCP_SUCCESS(result) )
result = WriteKLToFile(Writer, m_Dict->ul(MDD_RandomIndexMetadata), RIPSize);
if ( ASDCP_SUCCESS(result) )
{
result = RESULT_KLV_CODING(__LINE__, __FILE__);
Kumu::MemIOWriter MemWRT(Buffer.Data(), Buffer.Capacity());
if ( PairArray.Archive(&MemWRT) )
if ( MemWRT.WriteUi32BE(RIPSize + 20) )
{
Buffer.Size(MemWRT.Length());
result = RESULT_OK;
}
}
if ( ASDCP_SUCCESS(result) )
result = Writer.Write(Buffer.RoData(), Buffer.Size());
return result;
}
//
void
ASDCP::MXF::RIP::Dump(FILE* stream)
{
if ( stream == 0 )
stream = stderr;
KLVFilePacket::Dump(stream, *m_Dict, false);
PairArray.Dump(stream, false);
}
//------------------------------------------------------------------------------------------
//
//
ASDCP::MXF::Partition::PacketList::~PacketList() {
while ( ! m_List.empty() )
{
delete m_List.back();
m_List.pop_back();
}
}
//
void
ASDCP::MXF::Partition::PacketList::AddPacket(InterchangeObject* ThePacket) // takes ownership
{
assert(ThePacket);
m_Map.insert(std::map<UUID, InterchangeObject*>::value_type(ThePacket->InstanceUID, ThePacket));
m_List.push_back(ThePacket);
}
//
ASDCP::Result_t
ASDCP::MXF::Partition::PacketList::GetMDObjectByID(const UUID& ObjectID, InterchangeObject** Object)
{
ASDCP_TEST_NULL(Object);
std::map<UUID, InterchangeObject*>::iterator mi = m_Map.find(ObjectID);
if ( mi == m_Map.end() )
{
*Object = 0;
return RESULT_FAIL;
}
*Object = (*mi).second;
return RESULT_OK;
}
//
ASDCP::Result_t
ASDCP::MXF::Partition::PacketList::DeleteMDObjectByID(const UUID& ObjectID)
{
std::map<UUID, InterchangeObject*>::iterator mi = m_Map.find(ObjectID);
if ( mi == m_Map.end() )
{
return RESULT_FAIL;
}
delete (*mi).second;
m_Map.erase(mi);
return RESULT_OK;
}
//
ASDCP::Result_t
ASDCP::MXF::Partition::PacketList::GetMDObjectByType(const byte_t* ObjectID, InterchangeObject** Object)
{
ASDCP_TEST_NULL(ObjectID);
ASDCP_TEST_NULL(Object);
std::list<InterchangeObject*>::iterator li;
*Object = 0;
for ( li = m_List.begin(); li != m_List.end(); li++ )
{
if ( (*li)->HasUL(ObjectID) )
{
*Object = *li;
return RESULT_OK;
}
}
return RESULT_FAIL;
}
//
ASDCP::Result_t
ASDCP::MXF::Partition::PacketList::GetMDObjectsByType(const byte_t* ObjectID, std::list<InterchangeObject*>& ObjectList)
{
ASDCP_TEST_NULL(ObjectID);
std::list<InterchangeObject*>::iterator li;
for ( li = m_List.begin(); li != m_List.end(); li++ )
{
if ( (*li)->HasUL(ObjectID) )
ObjectList.push_back(*li);
}
return ObjectList.empty() ? RESULT_FAIL : RESULT_OK;
}
//------------------------------------------------------------------------------------------
//
ASDCP::MXF::Partition::Partition(const Dictionary* d) :
m_Dict(d),
MajorVersion(1), MinorVersion(2),
KAGSize(1), ThisPartition(0), PreviousPartition(0),
FooterPartition(0), HeaderByteCount(0), IndexByteCount(0), IndexSID(0),
BodyOffset(0), BodySID(0)
{
assert(d);
m_PacketList = new PacketList;
}
ASDCP::MXF::Partition::~Partition()
{
}
// takes ownership
void
ASDCP::MXF::Partition::AddChildObject(InterchangeObject* Object)
{
assert(Object);
if ( ! Object->InstanceUID.HasValue() )
GenRandomValue(Object->InstanceUID);
m_PacketList->AddPacket(Object);
}
//
ASDCP::Result_t
ASDCP::MXF::Partition::InitFromFile(const Kumu::IFileReader& Reader)
{
Result_t result = KLVFilePacket::InitFromFile(Reader);
// test the UL
// could be one of several values
if ( ASDCP_SUCCESS(result) )
result = ASDCP::MXF::Partition::InitFromBuffer(m_ValueStart, m_ValueLength);
return result;
}
//
ASDCP::Result_t
ASDCP::MXF::Partition::InitFromBuffer(const byte_t* p, ui32_t l)
{
Kumu::MemIOReader MemRDR(p, l);
Result_t result = RESULT_KLV_CODING(__LINE__, __FILE__);
if ( MemRDR.ReadUi16BE(&MajorVersion) )
if ( MemRDR.ReadUi16BE(&MinorVersion) )
if ( MemRDR.ReadUi32BE(&KAGSize) )
if ( MemRDR.ReadUi64BE(&ThisPartition) )
if ( MemRDR.ReadUi64BE(&PreviousPartition) )
if ( MemRDR.ReadUi64BE(&FooterPartition) )
if ( MemRDR.ReadUi64BE(&HeaderByteCount) )
if ( MemRDR.ReadUi64BE(&IndexByteCount) )
if ( MemRDR.ReadUi32BE(&IndexSID) )
if ( MemRDR.ReadUi64BE(&BodyOffset) )
if ( MemRDR.ReadUi32BE(&BodySID) )
if ( OperationalPattern.Unarchive(&MemRDR) )
if ( EssenceContainers.Unarchive(&MemRDR) )
result = RESULT_OK;
if ( ASDCP_FAILURE(result) )
DefaultLogSink().Error("Failed to initialize Partition.\n");
return result;
}
//
ASDCP::Result_t
ASDCP::MXF::Partition::WriteToFile(Kumu::FileWriter& Writer, UL& PartitionLabel)
{
ASDCP::FrameBuffer Buffer;
Result_t result = Buffer.Capacity(1024);
if ( ASDCP_SUCCESS(result) )
{
Kumu::MemIOWriter MemWRT(Buffer.Data(), Buffer.Capacity());
result = RESULT_KLV_CODING(__LINE__, __FILE__);
if ( MemWRT.WriteUi16BE(MajorVersion) )
if ( MemWRT.WriteUi16BE(MinorVersion) )
if ( MemWRT.WriteUi32BE(KAGSize) )
if ( MemWRT.WriteUi64BE(ThisPartition) )
if ( MemWRT.WriteUi64BE(PreviousPartition) )
if ( MemWRT.WriteUi64BE(FooterPartition) )
if ( MemWRT.WriteUi64BE(HeaderByteCount) )
if ( MemWRT.WriteUi64BE(IndexByteCount) )
if ( MemWRT.WriteUi32BE(IndexSID) )
if ( MemWRT.WriteUi64BE(BodyOffset) )
if ( MemWRT.WriteUi32BE(BodySID) )
if ( OperationalPattern.Archive(&MemWRT) )
if ( EssenceContainers.Archive(&MemWRT) )
{
Buffer.Size(MemWRT.Length());
result = RESULT_OK;
}
}
if ( ASDCP_SUCCESS(result) )
{
ui32_t write_count;
result = WriteKLToFile(Writer, PartitionLabel.Value(), Buffer.Size());
if ( ASDCP_SUCCESS(result) )
result = Writer.Write(Buffer.RoData(), Buffer.Size(), &write_count);
}
return result;
}
//
ui32_t
ASDCP::MXF::Partition::ArchiveSize()
{
return ( kl_length
+ sizeof(ui16_t) + sizeof(ui16_t)
+ sizeof(ui32_t)
+ sizeof(ui64_t) + sizeof(ui64_t) + sizeof(ui64_t) + sizeof(ui64_t) + sizeof(ui64_t)
+ sizeof(ui32_t)
+ sizeof(ui64_t)
+ sizeof(ui32_t)
+ SMPTE_UL_LENGTH
+ sizeof(ui32_t) + sizeof(ui32_t) + ( UUIDlen * EssenceContainers.size() ) );
}
//
void
ASDCP::MXF::Partition::Dump(FILE* stream)
{
char identbuf[IdentBufferLen];
if ( stream == 0 )
stream = stderr;
KLVFilePacket::Dump(stream, *m_Dict, false);
fprintf(stream, " MajorVersion = %hu\n", MajorVersion);
fprintf(stream, " MinorVersion = %hu\n", MinorVersion);
fprintf(stream, " KAGSize = %u\n", KAGSize);
fprintf(stream, " ThisPartition = %s\n", ui64sz(ThisPartition, identbuf));
fprintf(stream, " PreviousPartition = %s\n", ui64sz(PreviousPartition, identbuf));
fprintf(stream, " FooterPartition = %s\n", ui64sz(FooterPartition, identbuf));
fprintf(stream, " HeaderByteCount = %s\n", ui64sz(HeaderByteCount, identbuf));
fprintf(stream, " IndexByteCount = %s\n", ui64sz(IndexByteCount, identbuf));
fprintf(stream, " IndexSID = %u\n", IndexSID);
fprintf(stream, " BodyOffset = %s\n", ui64sz(BodyOffset, identbuf));
fprintf(stream, " BodySID = %u\n", BodySID);
fprintf(stream, " OperationalPattern = %s\n", OperationalPattern.EncodeString(identbuf, IdentBufferLen));
fputs("Essence Containers:\n", stream); EssenceContainers.Dump(stream);
}
//------------------------------------------------------------------------------------------
//
class ASDCP::MXF::Primer::h__PrimerLookup : public std::map<UL, TagValue>
{
public:
void InitWithBatch(ASDCP::MXF::Batch<ASDCP::MXF::Primer::LocalTagEntry>& Batch)
{
ASDCP::MXF::Batch<ASDCP::MXF::Primer::LocalTagEntry>::iterator i = Batch.begin();
for ( ; i != Batch.end(); i++ )
insert(std::map<UL, TagValue>::value_type((*i).UL, (*i).Tag));
}
};
//
ASDCP::MXF::Primer::Primer(const Dictionary* d) : m_LocalTag(0xff), m_Dict(d) {
m_UL = m_Dict->ul(MDD_Primer);
}
//
ASDCP::MXF::Primer::~Primer() {}
//
void
ASDCP::MXF::Primer::ClearTagList()
{
LocalTagEntryBatch.clear();
m_Lookup = new h__PrimerLookup;
}
//
ASDCP::Result_t
ASDCP::MXF::Primer::InitFromBuffer(const byte_t* p, ui32_t l)
{
assert(m_Dict);
Result_t result = KLVPacket::InitFromBuffer(p, l, m_Dict->ul(MDD_Primer));
if ( ASDCP_SUCCESS(result) )
{
if (m_ValueStart + m_ValueLength > p + l)
{
DefaultLogSink().Error("Primer entry too long.\n");
return RESULT_KLV_CODING(__LINE__, __FILE__);
}
Kumu::MemIOReader MemRDR(m_ValueStart, m_ValueLength);
result = LocalTagEntryBatch.Unarchive(&MemRDR) ? RESULT_OK : RESULT_KLV_CODING(__LINE__, __FILE__);
}
if ( ASDCP_SUCCESS(result) )
{
m_Lookup = new h__PrimerLookup;
m_Lookup->InitWithBatch(LocalTagEntryBatch);
}
if ( ASDCP_FAILURE(result) )
DefaultLogSink().Error("Failed to initialize Primer.\n");
return result;
}
//
ASDCP::Result_t
ASDCP::MXF::Primer::WriteToFile(Kumu::FileWriter& Writer)
{
ASDCP::FrameBuffer Buffer;
Result_t result = Buffer.Capacity(128*1024);
if ( ASDCP_SUCCESS(result) )
result = WriteToBuffer(Buffer);
if ( ASDCP_SUCCESS(result) )
result = Writer.Write(Buffer.RoData(), Buffer.Size());
return result;
}
//
ASDCP::Result_t
ASDCP::MXF::Primer::WriteToBuffer(ASDCP::FrameBuffer& Buffer)
{
assert(m_Dict);
ASDCP::FrameBuffer LocalTagBuffer;
Kumu::MemIOWriter MemWRT(Buffer.Data() + kl_length, Buffer.Capacity() - kl_length);
Result_t result = LocalTagEntryBatch.Archive(&MemWRT) ? RESULT_OK : RESULT_KLV_CODING(__LINE__, __FILE__);
if ( ASDCP_SUCCESS(result) )
{
ui32_t packet_length = MemWRT.Length();
result = WriteKLToBuffer(Buffer, packet_length);
if ( ASDCP_SUCCESS(result) )
Buffer.Size(Buffer.Size() + packet_length);
}
return result;
}
//
ASDCP::Result_t
ASDCP::MXF::Primer::InsertTag(const MDDEntry& Entry, ASDCP::TagValue& Tag)
{
assert(m_Lookup);
UL TestUL(Entry.ul);
std::map<UL, TagValue>::iterator i = m_Lookup->find(TestUL);
if ( i == m_Lookup->end() )
{
if ( Entry.tag.a == 0 && Entry.tag.b == 0 )
{
Tag.a = 0xff;
Tag.b = m_LocalTag--;
}
else
{
Tag.a = Entry.tag.a;
Tag.b = Entry.tag.b;
}
LocalTagEntry TmpEntry;
TmpEntry.UL = TestUL;
TmpEntry.Tag = Tag;
LocalTagEntryBatch.insert(TmpEntry);
m_Lookup->insert(std::map<UL, TagValue>::value_type(TmpEntry.UL, TmpEntry.Tag));
}
else
{
Tag = (*i).second;
}
return RESULT_OK;
}
//
ASDCP::Result_t
ASDCP::MXF::Primer::TagForKey(const ASDCP::UL& Key, ASDCP::TagValue& Tag)
{
if ( !m_Lookup || m_Lookup.empty() )
{
DefaultLogSink().Error("Primer lookup is empty\n");
return RESULT_FAIL;
}
std::map<UL, TagValue>::iterator i = m_Lookup->find(Key);
if ( i == m_Lookup->end() )
return RESULT_FALSE;
Tag = (*i).second;
return RESULT_OK;
}
//
void
ASDCP::MXF::Primer::Dump(FILE* stream)
{
assert(m_Dict);
char identbuf[IdentBufferLen];
if ( stream == 0 )
stream = stderr;
KLVPacket::Dump(stream, *m_Dict, false);
fprintf(stream, "Primer: %u %s\n",
(ui32_t)LocalTagEntryBatch.size(),
( LocalTagEntryBatch.size() == 1 ? "entry" : "entries" ));
Batch<LocalTagEntry>::iterator i = LocalTagEntryBatch.begin();
for ( ; i != LocalTagEntryBatch.end(); i++ )
{
const MDDEntry* Entry = m_Dict->FindULAnyVersion((*i).UL.Value());
fprintf(stream, " %s %s\n", (*i).EncodeString(identbuf, IdentBufferLen), (Entry ? Entry->name : "Unknown"));
}
}
//------------------------------------------------------------------------------------------
//
//
ASDCP::MXF::Preface::Preface(const Dictionary* d) :
InterchangeObject(d), Version(258)
{
assert(m_Dict);
m_UL = m_Dict->Type(MDD_Preface).ul;
ObjectModelVersion = 0;
}
ASDCP::MXF::Preface::Preface(const Preface& rhs) : InterchangeObject(rhs.m_Dict)
{
assert(m_Dict);
m_UL = rhs.m_UL;
Copy(rhs);
}
//
void
ASDCP::MXF::Preface::Copy(const Preface& rhs)
{
InterchangeObject::Copy(rhs);
LastModifiedDate = rhs.LastModifiedDate;
Version = rhs.Version;
ObjectModelVersion = rhs.ObjectModelVersion;
PrimaryPackage = rhs.PrimaryPackage;
Identifications = rhs.Identifications;
ContentStorage = rhs.ContentStorage;
OperationalPattern = rhs.OperationalPattern;
EssenceContainers = rhs.EssenceContainers;
DMSchemes = rhs.DMSchemes;
ApplicationSchemes = rhs.ApplicationSchemes;
ConformsToSpecifications = rhs.ConformsToSpecifications;
}
//
ASDCP::MXF::InterchangeObject*
ASDCP::MXF::Preface::Clone() const
{
return new Preface(*this);
}
//
ASDCP::Result_t
ASDCP::MXF::Preface::InitFromTLVSet(TLVReader& TLVSet)
{
Result_t result = InterchangeObject::InitFromTLVSet(TLVSet);
if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadObject(OBJ_READ_ARGS(Preface, LastModifiedDate));
if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadUi16(OBJ_READ_ARGS(Preface, Version));
if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadUi32(OBJ_READ_ARGS_OPT(Preface, ObjectModelVersion));
if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadObject(OBJ_READ_ARGS_OPT(Preface, PrimaryPackage));
if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadObject(OBJ_READ_ARGS(Preface, Identifications));
if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadObject(OBJ_READ_ARGS(Preface, ContentStorage));
if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadObject(OBJ_READ_ARGS(Preface, OperationalPattern));
if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadObject(OBJ_READ_ARGS(Preface, EssenceContainers));
if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadObject(OBJ_READ_ARGS(Preface, DMSchemes));
if ( ASDCP_SUCCESS(result) )
{
result = TLVSet.ReadObject(OBJ_READ_ARGS_OPT(Preface, ApplicationSchemes));
ApplicationSchemes.set_has_value( result == RESULT_OK);
}
if ( ASDCP_SUCCESS(result) )
{
result = TLVSet.ReadObject(OBJ_READ_ARGS_OPT(Preface, ConformsToSpecifications));
ConformsToSpecifications.set_has_value( result == RESULT_OK);
}
return result;
}
//
ASDCP::Result_t
ASDCP::MXF::Preface::WriteToTLVSet(TLVWriter& TLVSet)
{
Result_t result = InterchangeObject::WriteToTLVSet(TLVSet);
if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteObject(OBJ_WRITE_ARGS(Preface, LastModifiedDate));
if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteUi16(OBJ_WRITE_ARGS(Preface, Version));
if ( ASDCP_SUCCESS(result) && ! ObjectModelVersion.empty() ) result = TLVSet.WriteUi32(OBJ_WRITE_ARGS_OPT(Preface, ObjectModelVersion));
if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteObject(OBJ_WRITE_ARGS_OPT(Preface, PrimaryPackage));
if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteObject(OBJ_WRITE_ARGS(Preface, Identifications));
if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteObject(OBJ_WRITE_ARGS(Preface, ContentStorage));
if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteObject(OBJ_WRITE_ARGS(Preface, OperationalPattern));
if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteObject(OBJ_WRITE_ARGS(Preface, EssenceContainers));
if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteObject(OBJ_WRITE_ARGS(Preface, DMSchemes));
if ( ASDCP_SUCCESS(result) && !ApplicationSchemes.empty() )
{
result = TLVSet.WriteObject(OBJ_WRITE_ARGS_OPT(Preface, ApplicationSchemes));
}
if ( ASDCP_SUCCESS(result) && !ConformsToSpecifications.empty())
{
result = TLVSet.WriteObject(OBJ_WRITE_ARGS_OPT(Preface, ConformsToSpecifications));
}
return result;
}
//
ASDCP::Result_t
ASDCP::MXF::Preface::InitFromBuffer(const byte_t* p, ui32_t l)
{
return InterchangeObject::InitFromBuffer(p, l);
}
//
ASDCP::Result_t
ASDCP::MXF::Preface::WriteToBuffer(ASDCP::FrameBuffer& Buffer)
{
return InterchangeObject::WriteToBuffer(Buffer);
}
//
void
ASDCP::MXF::Preface::Dump(FILE* stream)
{
char identbuf[IdentBufferLen];
if ( stream == 0 )
stream = stderr;
InterchangeObject::Dump(stream);
fprintf(stream, " %22s = %s\n", "LastModifiedDate", LastModifiedDate.EncodeString(identbuf, IdentBufferLen));
fprintf(stream, " %22s = %hu\n", "Version", Version);
if ( ! ObjectModelVersion.empty() )
fprintf(stream, " %22s = %u\n", "ObjectModelVersion", ObjectModelVersion.get());
if ( ! PrimaryPackage.empty() )
fprintf(stream, " %22s = %s\n", "PrimaryPackage", PrimaryPackage.get().EncodeHex(identbuf, IdentBufferLen));
fprintf(stream, " %22s:\n", "Identifications"); Identifications.Dump(stream);
fprintf(stream, " %22s = %s\n", "ContentStorage", ContentStorage.EncodeHex(identbuf, IdentBufferLen));
fprintf(stream, " %22s = %s\n", "OperationalPattern", OperationalPattern.EncodeString(identbuf, IdentBufferLen));
fprintf(stream, " %22s:\n", "EssenceContainers"); EssenceContainers.Dump(stream);
fprintf(stream, " %22s:\n", "DMSchemes"); DMSchemes.Dump(stream);
if ( ! ApplicationSchemes.empty() )
{
fprintf(stream, " %22s:\n", "ApplicationSchemes"); ApplicationSchemes.get().Dump(stream);
}
if ( ! ConformsToSpecifications.empty() )
{
fprintf(stream, " %22s:\n", "ConformsToSpecifications"); ConformsToSpecifications.get().Dump(stream);
}
}
//------------------------------------------------------------------------------------------
//
ASDCP::MXF::OP1aHeader::OP1aHeader(const Dictionary* d) :
Partition(d), m_Primer(d), m_Preface(0)
{
assert(m_Dict);
}
ASDCP::MXF::OP1aHeader::~OP1aHeader() {}
//
ASDCP::Result_t
ASDCP::MXF::OP1aHeader::InitFromFile(const Kumu::IFileReader& Reader)
{
Result_t result = Partition::InitFromFile(Reader);
if ( ASDCP_FAILURE(result) )
return result;
if ( m_Dict == &DefaultCompositeDict() )
{
// select more explicit dictionary if one is available
if ( OperationalPattern.MatchExact(MXFInterop_OPAtom_Entry().ul) )
{
m_Dict = &DefaultInteropDict();
}
else if ( OperationalPattern.MatchExact(SMPTE_390_OPAtom_Entry().ul) )
{
m_Dict = &DefaultSMPTEDict();
}
}
// slurp up the remainder of the header
if ( HeaderByteCount == 0 )
{
DefaultLogSink().Warn("MXF file contents incomplete.\n");
return RESULT_KLV_CODING(__LINE__, __FILE__);
}
else if ( HeaderByteCount < 1024 )
{
DefaultLogSink().Warn("Improbably small HeaderByteCount value: %qu\n", HeaderByteCount);
}
else if (HeaderByteCount > ( 4 * Kumu::Megabyte ) )
{
DefaultLogSink().Warn("Improbably huge HeaderByteCount value: %qu\n", HeaderByteCount);
}
result = m_HeaderData.Capacity(Kumu::xmin(4*Kumu::Megabyte, static_cast<ui32_t>(HeaderByteCount)));
if ( ASDCP_SUCCESS(result) )
{
ui32_t read_count;
result = Reader.Read(m_HeaderData.Data(), m_HeaderData.Capacity(), &read_count);
if ( ASDCP_FAILURE(result) )
{
DefaultLogSink().Error("OP1aHeader::InitFromFile, read failed.\n");
return result;
}
if ( read_count != m_HeaderData.Capacity() )
{
DefaultLogSink().Error("Short read of OP-Atom header metadata; wanted %u, got %u.\n",
m_HeaderData.Capacity(), read_count);
return RESULT_KLV_CODING(__LINE__, __FILE__);
}
}
if ( ASDCP_SUCCESS(result) )
result = InitFromBuffer(m_HeaderData.RoData(), m_HeaderData.Capacity());
return result;
}
//
ASDCP::Result_t
ASDCP::MXF::OP1aHeader::InitFromPartitionBuffer(const byte_t* p, ui32_t l)
{
Result_t result = KLVPacket::InitFromBuffer(p, l);
if ( ASDCP_SUCCESS(result) )
result = Partition::InitFromBuffer(m_ValueStart, m_ValueLength); // test UL and OP
if ( ASDCP_SUCCESS(result) )
{
ui32_t pp_len = KLVPacket::PacketLength();
result = InitFromBuffer(p + pp_len, l - pp_len);
}
return result;
}
//
ASDCP::Result_t
ASDCP::MXF::OP1aHeader::InitFromBuffer(const byte_t* p, ui32_t l)
{
assert(m_Dict);
Result_t result = RESULT_OK;
const byte_t* end_p = p + l;
while ( ASDCP_SUCCESS(result) && p < end_p )
{
// parse the packets and index them by uid, discard KLVFill items
InterchangeObject* object = CreateObject(m_Dict, p);
assert(object);
object->m_Lookup = &m_Primer;
result = object->InitFromBuffer(p, end_p - p);
const byte_t* redo_p = p;
p += object->PacketLength();
if ( ASDCP_SUCCESS(result) )
{
if ( object->IsA(m_Dict->ul(MDD_KLVFill)) )
{
delete object;
if ( p > end_p )
{
DefaultLogSink().Error("Fill item short read: %d.\n", p - end_p);
}
}
else if ( object->IsA(m_Dict->ul(MDD_Primer)) ) // TODO: only one primer should be found
{
delete object;
result = m_Primer.InitFromBuffer(redo_p, end_p - redo_p);
}
else
{
m_PacketList->AddPacket(object); // takes ownership
if ( object->IsA(m_Dict->ul(MDD_Preface)) && m_Preface == 0 )
m_Preface = (Preface*)object;
}
}
else
{
DefaultLogSink().Error("Error initializing OP1a header packet.\n");
// Kumu::hexdump(p-object->PacketLength(), object->PacketLength());
delete object;
}
}
return result;
}
//
ASDCP::Result_t
ASDCP::MXF::OP1aHeader::GetMDObjectByID(const UUID& ObjectID, InterchangeObject** Object)
{
return m_PacketList->GetMDObjectByID(ObjectID, Object);
}
//
ASDCP::Result_t
ASDCP::MXF::OP1aHeader::DeleteMDObjectByID(const UUID& ObjectID)
{
return m_PacketList->DeleteMDObjectByID(ObjectID);
}
//
ASDCP::Result_t
ASDCP::MXF::OP1aHeader::GetMDObjectByType(const byte_t* ObjectID, InterchangeObject** Object)
{
InterchangeObject* TmpObject;
if ( Object == 0 )
Object = &TmpObject;
return m_PacketList->GetMDObjectByType(ObjectID, Object);
}
//
ASDCP::Result_t
ASDCP::MXF::OP1aHeader::GetMDObjectsByType(const byte_t* ObjectID, std::list<InterchangeObject*>& ObjectList)
{
return m_PacketList->GetMDObjectsByType(ObjectID, ObjectList);
}
//
ASDCP::MXF::Identification*
ASDCP::MXF::OP1aHeader::GetIdentification()
{
InterchangeObject* Object;
if ( ASDCP_SUCCESS(GetMDObjectByType(OBJ_TYPE_ARGS(Identification), &Object)) )
return (Identification*)Object;
return 0;
}
//
ASDCP::MXF::SourcePackage*
ASDCP::MXF::OP1aHeader::GetSourcePackage()
{
InterchangeObject* Object;
if ( ASDCP_SUCCESS(GetMDObjectByType(OBJ_TYPE_ARGS(SourcePackage), &Object)) )
return (SourcePackage*)Object;
return 0;
}
//
ASDCP::Result_t
ASDCP::MXF::OP1aHeader::WriteToFile(Kumu::FileWriter& Writer, ui32_t HeaderSize)
{
assert(m_Dict);
if ( m_Preface == 0 )
return RESULT_STATE;
if ( HeaderSize < 4096 )
{
DefaultLogSink().Error("HeaderSize %u is too small. Must be >= 4096\n", HeaderSize);
return RESULT_PARAM;
}
ASDCP::FrameBuffer HeaderBuffer;
HeaderByteCount = HeaderSize - ArchiveSize();
assert (HeaderByteCount <= 0xFFFFFFFFL);
Result_t result = HeaderBuffer.Capacity((ui32_t) HeaderByteCount);
m_Preface->m_Lookup = &m_Primer;
std::list<InterchangeObject*>::iterator pl_i = m_PacketList->m_List.begin();
for ( ; pl_i != m_PacketList->m_List.end() && ASDCP_SUCCESS(result); pl_i++ )
{
InterchangeObject* object = *pl_i;
object->m_Lookup = &m_Primer;
ASDCP::FrameBuffer WriteWrapper;
WriteWrapper.SetData(HeaderBuffer.Data() + HeaderBuffer.Size(),
HeaderBuffer.Capacity() - HeaderBuffer.Size());
result = object->WriteToBuffer(WriteWrapper);
HeaderBuffer.Size(HeaderBuffer.Size() + WriteWrapper.Size());
}
if ( ASDCP_SUCCESS(result) )
{
UL TmpUL(m_Dict->ul(MDD_ClosedCompleteHeader));
result = Partition::WriteToFile(Writer, TmpUL);
}
if ( ASDCP_SUCCESS(result) )