forked from AMWA-TV/maj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MXFBuilder.java
1070 lines (920 loc) · 41.5 KB
/
MXFBuilder.java
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 2016 Richard Cartwright
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tv.amwa.maj.io.mxf;
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import tv.amwa.maj.constant.CommonConstants;
import tv.amwa.maj.exception.BadParameterException;
import tv.amwa.maj.exception.EndOfDataException;
import tv.amwa.maj.exception.IllegalPropertyValueException;
import tv.amwa.maj.exception.InsufficientSpaceException;
import tv.amwa.maj.industry.Forge;
import tv.amwa.maj.industry.MediaEngine;
import tv.amwa.maj.industry.MetadataObject;
import tv.amwa.maj.industry.PropertyValue;
import tv.amwa.maj.industry.Warehouse;
import tv.amwa.maj.industry.WeakReference;
import tv.amwa.maj.industry.WeakReferenceTarget;
import tv.amwa.maj.integer.UInt64;
import tv.amwa.maj.io.mxf.impl.DeltaEntryImpl;
import tv.amwa.maj.io.mxf.impl.IndexEntryImpl;
import tv.amwa.maj.io.mxf.impl.IndexTableSegmentImpl;
import tv.amwa.maj.io.mxf.impl.MXFFileImpl;
import tv.amwa.maj.io.mxf.impl.PartitionPackImpl;
import tv.amwa.maj.io.mxf.impl.PrimerPackImpl;
import tv.amwa.maj.io.mxf.impl.RandomIndexPackImpl;
import tv.amwa.maj.io.mxf.impl.ResolutionEntry;
import tv.amwa.maj.meta.ClassDefinition;
import tv.amwa.maj.meta.MetaDefinition;
import tv.amwa.maj.meta.PropertyDefinition;
import tv.amwa.maj.meta.TypeDefinition;
import tv.amwa.maj.meta.TypeDefinitionObjectReference;
import tv.amwa.maj.meta.TypeDefinitionStrongObjectReference;
import tv.amwa.maj.meta.impl.ClassDefinitionImpl;
import tv.amwa.maj.meta.impl.TypeDefinitionObjectReferenceImpl;
import tv.amwa.maj.meta.impl.TypeDefinitionRecordImpl;
import tv.amwa.maj.meta.impl.TypeDefinitionSetImpl;
import tv.amwa.maj.meta.impl.TypeDefinitionVariableArrayImpl;
import tv.amwa.maj.model.Preface;
import tv.amwa.maj.record.AUID;
import tv.amwa.maj.record.impl.AUIDImpl;
/**
* <p>Collection of static methods that provide the ability to build {@linkplain MXFFile MXF files}
* from in memory data models (e.g. AAF) and vice versa. Reading or writing an MXF file is boiled
* down to calling these methods in the correct order.</p>
*
* <p>In this builder, the following capability is provided for reading and writing KLV triplets:</p>
*
* <ul>
* <li>The ability to read and write keys with {@link #readKey(ByteBuffer)} and {@link #writeKey(UL, ByteBuffer)};</li>
* <li>The ability to read and write BER encoded length values with {@link #readBERLength(ByteBuffer)} and
* {@link #writeBERLength(long, int, ByteBuffer)};</li>
* <li>Two kinds of value encoding are supported:
* <ul>
* <li>Reading and writing {@linkplain FixedLengthPack fixed length packs} is supported with
* {@link #readFixedLengthPack(AUID, ByteBuffer)}, {@link #writeFixedLengthPack(FixedLengthPack, ByteBuffer)}
* and {@link #lengthOfFixedLengthPack(FixedLengthPack)};</li>
* <li>Reading and writing local sets with 2-byte keys and 2-byte values is supported with
* {@link #readLocalSet(UL, ByteBuffer, PrimerPack, Map, List)},
* {@link #writeLocalSet(MetadataObject, ByteBuffer, PrimerPack, List)} and
* {@link #lengthOfLocalSet(MetadataObject)}.</li>
* </ul></li>
* </ul>
*
* <p>When reading an MXF file, all methods report an error on bad data and try to skip over it and
* move on.</p>
*
*
*
* @see MXFFile
*
*/
public class MXFBuilder {
private MXFBuilder() { }
private static boolean mxfRegistration = false;
public final static synchronized void registerMXF() {
if (mxfRegistration) return;
MediaEngine.initializeAAF();
PartitionPackImpl.initializePackClasses();
Warehouse.lookForClass(PrimerPackImpl.class);
Warehouse.lookForClass(RandomIndexPackImpl.class);
Warehouse.lookForClass(IndexTableSegmentImpl.class);
Warehouse.registerTypes(TypeDefinitions.class, MXFConstants.RP210_NAMESPACE, MXFConstants.RP210_PREFIX);
TypeDefinitionRecordImpl.registerInterfaceMapping(DeltaEntry.class, DeltaEntryImpl.class);
TypeDefinitionRecordImpl.registerInterfaceMapping(IndexEntry.class, IndexEntryImpl.class);
mxfRegistration = true;
}
private static Set<String> propertyIgnoreList =
new HashSet<String>();
/**
* <p>Add a property to the set of properties ignored by this builder. When reading an
* MXF file containing consisting of local sets that have known issues, it is useful to
* be able to ignore certain properties by skipping over them and not producing an
* error. This method reduces noise rather than changing the ability of the builder
* to read a file.</p>
*
* <p>No check is made against any known metadata set to see if the property exists. The
* property will be ignored on the basis of its name and name only.</p>
*
* @param ignoreMe Name of a property to ignore.
*
* @throws NullPointerException Cannot set a property to ignore with a <code>null</code>
* string.
*/
public final static void ignoreProperty(
String ignoreMe)
throws NullPointerException {
if (ignoreMe == null)
throw new NullPointerException("Cannot set a property name to ignore with a null value.");
propertyIgnoreList.add(ignoreMe);
}
/**
* <p>Clear the set of ignored properties, restoring the builder to the state where it
* will attempt to raad everything.</p>
*/
public final static void clearIgnoredProperties() {
propertyIgnoreList.clear();
}
/**
* <p>Read data from a {@linkplain FixedLengthPack fixed length pack} that starts with
* the provided key and is contained in the given buffer.</p>
*
* <p>The methods reads the buffer to
* create a the {@linkplain MetadataObject metadata object} corresponding to the key. The key
* is the {@linkplain ClassDefinition#getAUID() identification} of the {@linkplain ClassDefinition class}
* defining the metadata object. To be able to instantiate the metadata object, it must have been
* registered with the {@linkplain tv.amwa.maj.industry.Warehouse warehouse} first. As the order
* of a fixed length pack is used to determine which property is which in the buffer, the implementation
* of the metadata object must also implement {@linkplain FixedLengthPack fixed length pack}.</p>
*
* <p>The key is provided to the method separately as it must have been read from the buffer to
* determine that the next item in the buffer is a fixed length pack. The buffer should contain
* have remaining to read the correct number of bytes as has been determined by reading the length of the pack
* before calling this method. Although known as a fixed length
* pack, it is possible for the last property of the pack to have variable length, as for the
* {@linkplain PartitionPack#getEssenceContainers() essence containers} of a
* {@linkplain PartitionPack partition pack}.</p>
*
* @param key Identification of the metadata object represented by the fixed length pack.
* @param buffer Buffer containing a serialization of the value of the pack. When passed
* into the method, the remaining number of bytes to read is equal to the length of the
* pack taken from the KLV coding.
* @return Metadata object created by reading the fixed length pack.
*
* @throws NullPointerException One or both of the arguments is/are <code>null</code> or
* the key is not of a known type.
* @throws EndOfDataException Reached the end of the buffer before al the properties of
* the fixed length pack have been read.
* @throws ClassCastException The metadata object registered in the warehosue with the given
* key is not a fixed length pack.
*
* @see #writeFixedLengthPack(FixedLengthPack, ByteBuffer)
* @see #lengthOfFixedLengthPack(FixedLengthPack)
* @see PartitionPack
* @see PrimerPack
* @see FixedLengthPack
* @see FixedLengthPack#getPackOrder()
*/
public final static FixedLengthPack readFixedLengthPack(
AUID key,
ByteBuffer buffer)
throws NullPointerException,
EndOfDataException,
ClassCastException {
if (buffer == null)
throw new NullPointerException("Cannot read a fixed length pack from a null buffer.");
if (key == null)
throw new NullPointerException("Cannot read a fixed length pack with a null key.");
ClassDefinition fixedLengthClass = null;
FixedLengthPack fixedLengthValue = null;
try{
fixedLengthClass = Warehouse.lookForClass(key);
fixedLengthValue = (FixedLengthPack) fixedLengthClass.createInstance();
}
catch (ClassCastException cce) {
throw new ClassCastException("The given key does not correspond to a metadata object that can be serialized as a fixed length pack. The key resolved to type " + fixedLengthClass.getName() + ".");
}
catch (NullPointerException npe) {
throw new NullPointerException("The given key of " + key.toString() + " is now known in the warehouse.");
}
for ( String propertyName : fixedLengthValue.getPackOrder()) {
try {
PropertyDefinition property = fixedLengthClass.lookupPropertyDefinition(propertyName);
TypeDefinition propertyType = property.getTypeDefinition();
PropertyValue propertyValue = propertyType.createFromBytes(buffer);
property.setPropertyValue(fixedLengthValue, propertyValue);
}
catch (BadParameterException bpe) {
continue;
}
catch (EndOfDataException eode) {
throw new EndOfDataException("Prematurely reached the end of the data buffer when reading a fixed length buffer for type " + fixedLengthClass.getName() + ".");
}
}
return fixedLengthValue;
}
/**
* <p>Write the value of the given {@linkplain FixedLengthPack fixed length pack} to the given byte buffer.
* The key and length of the pack must have been written to the buffer prior to calling this method.</p>
*
* <p>The buffer is expected to have sufficient capacity remaining. To determine how much capacity this
* is before calling this method, call {@link #lengthOfFixedLengthPack(FixedLengthPack)}.</p>
*
* <p>The properties of the fixed length pack are written to the buffer in the
* {@linkplain FixedLengthPack#getPackOrder() specified order}.</p>
*
* @param fixedLengthPack Data to write to the buffer.
* @param buffer Buffer to be written into.
*
* @throws NullPointerException One or both of the arguments is/are <code>null</code>.
* @throws InsufficientSpaceException Not enough space is available write all the data into the buffer.
* Note that this exception will be thrown part way through an attempt to write to the buffer.
*
* @see #readFixedLengthPack(AUID, ByteBuffer)
* @see #lengthOfFixedLengthPack(FixedLengthPack)
* @see PartitionPack
* @see PrimerPack
* @see FixedLengthPack
* @see FixedLengthPack#getPackOrder()
*/
public final static void writeFixedLengthPack(
FixedLengthPack fixedLengthPack,
ByteBuffer buffer)
throws NullPointerException,
InsufficientSpaceException {
if (fixedLengthPack == null)
throw new NullPointerException("Cannot write a fixed length pack from a null value.");
if (buffer == null)
throw new NullPointerException("Cannot write a fixed length pack to a null buffer.");
// FIXME this is bad ... primer writes its own key, whereas others expect key and length to have been written
if (fixedLengthPack instanceof PrimerPack) {
PrimerPackImpl.writeAsBytes((PrimerPack) fixedLengthPack, buffer);
return;
}
ClassDefinition fixedLengthClass = Warehouse.lookForClass(fixedLengthPack.getClass());
for ( String propertyName : fixedLengthPack.getPackOrder() ) {
try {
PropertyDefinition property = fixedLengthClass.lookupPropertyDefinition(propertyName);
TypeDefinition propertyType = property.getTypeDefinition();
PropertyValue propertyValue = property.getPropertyValue(fixedLengthPack);
propertyType.writeAsBytes(propertyValue, buffer);
}
catch (IllegalPropertyValueException ipve) {
throw new InternalError("Unexpected type problem dealing wih property " + propertyName +
" of class " + fixedLengthClass.getName() + " when writing bytes for a fixed length pack.");
}
catch (BadParameterException bpe) {
throw new InternalError("Unexpected failure to resolve property " + propertyName +
" of class " + fixedLengthClass.getName() + " when writing bytes for a fixed length pack.");
}
}
}
/**
* <p>Calculates the number of bytes that will be used to encode the value of the given
* {@linkplain FixedLengthPack fixed length pack}, exclusive of its key and length representation in
* the byte stream. The length calculation can be used as the encoded length and to create a buffer with
* at least enough capacity to represent the fixed length pack ready for
* {@linkplain #writeFixedLengthPack(FixedLengthPack, ByteBuffer) writing}.</p>
*
* @param fixedLengthPack Fixed length back to calculate the encoded length of.
* @return Number of bytes required to encode the given data.
*
* @throws NullPointerException Cannot calculate a length from a <code>null</code> value.
*
* @see #writeFixedLengthPack(FixedLengthPack, ByteBuffer)
* @see #readFixedLengthPack(AUID, ByteBuffer)
*/
public final static long lengthOfFixedLengthPack(
FixedLengthPack fixedLengthPack)
throws NullPointerException {
if (fixedLengthPack == null)
throw new NullPointerException("Cannot calculate the length in bytes of a null fixed length pack.");
// FIXME does give incorrect value for primer packs
ClassDefinition fixedLengthClass = Warehouse.lookForClass(fixedLengthPack.getClass());
long length = 0l;
for ( String propertyName : fixedLengthPack.getPackOrder() ) {
try {
PropertyDefinition property = fixedLengthClass.lookupPropertyDefinition(propertyName);
TypeDefinition propertyType = property.getTypeDefinition();
PropertyValue propertyValue = property.getPropertyValue(fixedLengthPack);
length += propertyType.lengthAsBytes(propertyValue);
}
catch (IllegalPropertyValueException e) {
throw new InternalError("Unexpected type problem dealing wih property " + propertyName +
" of class " + fixedLengthClass.getName() + " when calculating the length of a fixed length pack.");
}
catch (BadParameterException bpe) {
throw new InternalError("Unexpected failure to resolve property " + propertyName +
" of class " + fixedLengthClass.getName() + " when calaulating the length of a fixed length pack.");
}
}
return length;
}
/**
* <p>Create a new instance of a {@linkplain tv.amwa.maj.industry.MetadataObject metadata object}
* from the given buffer that has an encoding of the value as a local set.
* {@linkplain HeaderMetadata Header metadata} is encoded as a sequence of local sets, where each
* local set may have a variable length depending on its value.</p>
*
* <p>The key will have been read from the stream already and must correspond to the identification
* of a class registered with the {@linkplain tv.amwa.maj.industry.Warehouse warehouse}. The length
* will have been read from the stream and used to set the number of bytes available to read from the
* given buffer.<p>
*
* <p>A local set encoding is a sequence of tag-length-value triplets of each property, where the
* tags are two bytes long. The keys are {@linkplain PrimerPack#lookupUID(short) resolved} to full
* 16-byte keys using the given primer pack that has been read from the same
* {@linkplain HeaderMetadata header metadata}.</p>
*
* <p>As a local set often has references to other local sets in the same sequence that makes
* up {@linkplain HeaderMetadata header metadata}, the local set must allow for the resolution of
* the references after the creation of all of the metadata objects. The local set will add its
* identification to the <em>reference table</em> and request any subsequent <em>resolutions</em> required
* to fully complete the value.</p>
*
* <p>This method skips over the following kinds of data, returning <code>null</code>:</p>
*
* <ul>
* <li>Any local set with a key that is not known in the {@linkplain tv.amwa.maj.industry.Warehouse warehouse};</li>
* <li>Any representation of a {@linkplain tv.amwa.maj.meta.MetaDictionary meta dictionary}, such as that
* sometimes found in an MXF OP-Atom file;</li>
* <li>A local set that is a kind of {@linkplain tv.amwa.maj.meta.MetaDefinition meta definition},
* although a warning message will be generated to say if the meta definition is now known in the
* {@linkplain tv.amwa.maj.industry.Warehouse warehouse};</li>
* <li>Any properties on the {@linkplain #ignoreProperty(String) properties to be ignored list};</li>
* <li>Any properties that are not known for the class identified by the given key.</li>
* </ul>
*
* @param key Identification of the local set.
* @param buffer Buffer containing the encoding of the local set.
* @param primerPack Mapping of 2-byte tags to full 16-byte property identifications.
* @param referenceTable Mapping of local set instance identifications to values being created as
* header metadata is read in sequence.
* @param resolutions Requests for subsequent resolutions required to complete the value of
* the returned metadata object.
* @return Metadata object of the {@linkplain tv.amwa.maj.meta.ClassDefinition class} with
* the given identifier with most of its properties set according to the local set encoding
* in the given buffer.
*
* @throws Exception A problem occurred parsing the local set and so the value should be
* skipped over.
*
* @see #writeLocalSet(MetadataObject, ByteBuffer, PrimerPack, List)
* @see #writeLocalSet(PropertyValue, ByteBuffer, PrimerPack, List)
* @see #lengthOfLocalSet(MetadataObject)
* @see HeaderMetadata#getPreface()
* @see ResolutionEntry#resolve(Map)
*/
public final static MetadataObject readLocalSet(
UL key,
ByteBuffer buffer,
PrimerPack primerPack,
Map<AUIDImpl, MetadataObject> referenceTable,
List<ResolutionEntry> resolutions)
throws Exception { // TODO ruggidise all of this
int preserveLimit = buffer.limit();
ClassDefinition localSetClass = ClassDefinitionImpl.forAUID(key);
if (localSetClass == null) {
System.err.println("Unable to find a local implementation of class with id " + key.toString() +
". Skipping " + buffer.remaining() + " bytes.");
buffer.position(preserveLimit);
return null;
}
// TODO add meta dictionary support
if (localSetClass.getName().equals("MetaDictionary")) {
System.err.println("This version of MAJ does not support processing meta dictionaries in MXF files.");
buffer.position(preserveLimit);
return null;
}
MetadataObject localSetValue = localSetClass.createInstance();
if (localSetValue == null) {
System.err.println("Unable to create an instance of class " + localSetClass.getName() + " implemented by " +
localSetClass.getJavaImplementation().getName() + ". Skipping.");
buffer.position(preserveLimit);
return null;
}
if (localSetValue instanceof MetaDefinition) {
System.err.println("Skipping meta definition " + localSetClass.getName() + ".");
buffer.position(preserveLimit);
return null;
}
while (buffer.hasRemaining()) {
PropertyDefinition property = null;
AUID propertyKey = null;
short tag = buffer.getShort();
int length = 0;
try {
if (primerPack != null) {
propertyKey = primerPack.lookupUID(tag);
}
else {
property = localSetClass.lookupPropertyDefinition(tag);
propertyKey = property.getAUID();
}
short shortLength = buffer.getShort();
length = (shortLength >= 0) ? shortLength : 65536 - shortLength;
if (propertyKey == null)
throw new BadParameterException("Unable to resolve tag in primer pack.");
if (propertyKey.equals(MXFConstants.InstanceUID)) {
byte[] instanceKey = new byte[16];
buffer.get(instanceKey);
// if (localSetValue instanceof tv.amwa.maj.model.Package)
// System.out.println("Adding " + localSetClass.getName() + " package with key " + new AUIDImpl(instanceKey));
referenceTable.put(new AUIDImpl(instanceKey), localSetValue);
continue;
}
if (property == null)
property = localSetClass.lookupPropertyDefinition(propertyKey);
}
catch (BadParameterException bpe) {
if (propertyKey != null)
System.err.println("Unable to resolve tag " + Integer.toHexString(tag) + " and key " + propertyKey.toString() +
" for class " + localSetClass.getName() + ". Skipping.");
else
System.err.println("Unable to resolve tag " + Integer.toHexString(tag) +
" for class " + localSetClass.getName() + ". Skipping.");
buffer.limit(preserveLimit);
buffer.position(buffer.position() + length);
continue;
}
// System.out.println(property.getName());
// if (property.getName().equals("Delta Entry Array"))
// System.out.println("Found it!");
// if (localSetValue instanceof MetaDefinition) {
// String defName = ((MetaDefinition) localSetValue).getName();
// if (defName != null)
// System.out.println(defName + "." + property.getName());
// }
if (propertyIgnoreList.contains(property.getName())) {
System.err.println("Ignoring property " + property.getMemberOf().getName() + "." + property.getName() + ".");
buffer.limit(preserveLimit);
buffer.position(buffer.position() + length);
continue;
}
TypeDefinition propertyType = property.getTypeDefinition();
buffer.limit(buffer.position() + length);
PropertyValue propertyValue = null;
try {
propertyValue = propertyType.createFromBytes(buffer);
if (propertyValue == null)
throw new NullPointerException("Unexpected null property value created when parsing bytes.");
}
catch (Exception e) {
System.err.println(e.getClass().getName() + " thrown when parsing value of " + property.getMemberOf().getName() + "." + property.getName() + ": " + e.getMessage());
buffer.position(buffer.limit());
buffer.limit(preserveLimit);
continue;
}
switch (propertyType.getTypeCategory()) {
case StrongObjRef:
resolutions.add(new ResolutionEntry(property, localSetValue, propertyValue));
break;
case WeakObjRef:
resolutions.add(0, new ResolutionEntry(property, localSetValue, propertyValue));
break;
case Set:
if (((TypeDefinitionSetImpl) propertyType).getElementType() instanceof TypeDefinitionObjectReference)
resolutions.add(new ResolutionEntry(property, localSetValue, propertyValue));
else
property.setPropertyValue(localSetValue, propertyValue);
break;
case VariableArray:
if (((TypeDefinitionVariableArrayImpl) propertyType).getType() instanceof TypeDefinitionObjectReference)
resolutions.add(new ResolutionEntry(property, localSetValue, propertyValue));
else
property.setPropertyValue(localSetValue, propertyValue);
break;
default:
property.setPropertyValue(localSetValue, propertyValue);
break;
}
buffer.limit(preserveLimit);
}
if (localSetValue instanceof MetaDefinition) {
testMetaDictionaryEntry((MetaDefinition) localSetValue);
return null;
}
// System.out.println(localSetValue.toString());
if (localSetValue instanceof WeakReferenceTarget)
WeakReference.registerTarget((WeakReferenceTarget) localSetValue);
return localSetValue;
}
/**
* <p>Writes the given metadata object to the given buffer encoded as a local set, excluding
* its key and length that will have already been written. A new random
* {@linkplain MXFConstants#InstanceUID instance ID} will be created to identify the metadata object.
* As such, this method is suitable to write the top of a hierarchy of objects. If the object is
* referenced from another object in the same {@linkplain HeaderMetadata header metadata},
* write the reference and value to the local set with
* {@link #writeLocalSet(PropertyValue, ByteBuffer, PrimerPack, List)}.</p>
*
* <p>The buffer must have sufficient capacity and this can be ensured by calling
* {@link #lengthOfLocalSet(MetadataObject)} first.</p>
*
* <p>Any {@linkplain tv.amwa.maj.meta.TypeDefinitionStrongObjectReference strong references} made
* by this metadata object to another one must result in the referenced value being added to the
* list of <em>forward references</em>. The caller must ensure that all forward references get added
* to the list.</p>
*
* @param toWrite Metadata object to write as a local set value.
* @param buffer Buffer to write the local set data into.
* @param primerPack Lookup table of tag-to-key mappings in use for this header metadata.
* @param forwardReferences List of forward references that have yet to be written into the
* current set of header metadata.
* @return Number of local set bytes written. If the number is less than expected, this suggests that
* some of the properties could not be written into the set.
*
* @throws NullPointerException One or more of the arguments is/are <code>null</code>.
* @throws InsufficientSpaceException The given buffer does not have enough capacity to receive
* the bytes required to represent the local set.
*
* @see #readLocalSet(UL, ByteBuffer, PrimerPack, Map, List)
* @see #lengthOfLocalSet(MetadataObject)
*/
public final static long writeLocalSet(
MetadataObject toWrite,
ByteBuffer buffer,
PrimerPack primerPack,
List<PropertyValue> forwardReferences)
throws NullPointerException,
InsufficientSpaceException {
if (toWrite == null)
throw new NullPointerException("Cannot encode a null metadata object as a local set value.");
AUID instanceID = Forge.randomAUID();
// buffer.put(instanceID.getAUIDValue());
return writeToLocalSet(toWrite, instanceID, buffer, primerPack, forwardReferences);
}
/**
* <p>Writes the given {@link TypeDefinitionStrongObjectReference strongly referenced}
* value to the given buffer encoded as a local set, excluding
* its key and length that will have already been written. The local identifier of the
* strong reference will be used created to identify the metadata object that is contained
* in the value. As such, this method is suitable to write mid- and bottom tier objects in
* the object hierarchy. If the object is not referenced from another object in the same
* {@linkplain HeaderMetadata header metadata}, write the the local set with
* {@link #writeLocalSet(MetadataObject, ByteBuffer, PrimerPack, List)}.</p>
*
* <p>The buffer must have sufficient capacity and this can be ensured by calling
* {@link #lengthOfLocalSet(MetadataObject)} first.</p>
*
* <p>Any {@linkplain tv.amwa.maj.meta.TypeDefinitionStrongObjectReference strong references} made
* by this metadata object to another one must result in the referenced value being added to the
* list of <em>forward references</em>. The caller must ensure that all forward references get added
* to the list.</p>
*
* @param referencedValue Strongly referenced value to write.
* @param buffer Buffer to write the local set data into.
* @param primerPack Lookup table of tag-to-key mappings in use for this header metadata.
* @param forwardReferences List of forward references that have yet to be written into the
* current set of header metadata.
* @return Number of local set bytes written. If the number is less than expected, this suggests that
* some of the properties could not be written into the set.
*
* @throws NullPointerException One or more of the arguments is/are <code>null</code>.
* @throws InsufficientSpaceException The given buffer does not have enough capacity to receive
* the bytes required to represent the local set.
*
* @see #readLocalSet(UL, ByteBuffer, PrimerPack, Map, List)
* @see #lengthOfLocalSet(MetadataObject)
*/
public final static long writeLocalSet(
PropertyValue referencedValue,
ByteBuffer buffer,
PrimerPack primerPack,
List<PropertyValue> forwardReferences)
throws NullPointerException,
InsufficientSpaceException {
if (referencedValue == null)
throw new NullPointerException("Cannot encode a null property value object as a local set value.");
if ((referencedValue.getType() == null) ||
(!(referencedValue.getType() instanceof TypeDefinitionStrongObjectReference)))
throw new NullPointerException("The given referenced property has a null type or is not a strong reference type. Type provided was " + referencedValue.getType().getName() + ".");
if ((referencedValue.getValue() == null) ||
(!(referencedValue.getValue() instanceof MetadataObject)))
throw new NullPointerException("The given referenced proeprty has a null value or references a inappropriate type of value.");
MetadataObject toWrite = (MetadataObject) referencedValue.getValue();
AUID instanceID = TypeDefinitionObjectReferenceImpl.getLocalReference(referencedValue);
return writeToLocalSet(toWrite, instanceID, buffer, primerPack, forwardReferences);
}
private final static long writeToLocalSet(
MetadataObject toWrite,
AUID instanceID,
ByteBuffer buffer,
PrimerPack primerPack,
List<PropertyValue> forwardReferences)
throws NullPointerException,
InsufficientSpaceException {
if (buffer == null)
throw new NullPointerException("Cannot write an encoded local set to a null buffer.");
if (forwardReferences == null)
throw new NullPointerException("The list of forward references provided is null and a metadata object might need to be added.");
ClassDefinition classOfMetadata = MediaEngine.getClassDefinition(toWrite);
SortedMap<? extends PropertyDefinition,? extends PropertyValue> properties = classOfMetadata.getProperties(toWrite);
AUID classAUID = classOfMetadata.getAUID();
writeKey((UL) classAUID, buffer);
long lengthOfSetBody = lengthOfLocalSet(toWrite);
MXFBuilder.writeBERLength(lengthOfSetBody, 4, buffer);
buffer.putShort(MXFConstants.InstanceTag);
buffer.putShort((short) 16);
buffer.put(instanceID.getAUIDValue());
long bytesWritten = 20l;
for ( PropertyDefinition property : properties.keySet() ) {
if (property.getAUID().equals(CommonConstants.ObjectClassID)) continue;
PropertyValue value = properties.get(property);
Short localTag = primerPack.lookupLocalTag(property);
if (localTag == null)
continue;
long propertyLength = value.getType().lengthAsBytes(value);
if (propertyLength > 65535) {
System.err.println("Cannot write a property value longer than 65535 bytes for property " +
property.getMemberOf().getName() + "." + property.getName() + ".");
continue;
}
buffer.putShort(localTag);
buffer.putShort((short) propertyLength);
bytesWritten += 4l;
List<PropertyValue> childReferences = value.getType().writeAsBytes(value, buffer);
if (childReferences != null)
forwardReferences.addAll(childReferences);
bytesWritten += propertyLength;
}
return bytesWritten;
}
/**
* <p>Calculates the length in bytes of a local set value for the given metadata object, excluding the
* sets initial key and value. The value returned can be used to allocate sufficient capacity in a buffer to encode the value and
* to write the length part of a KLV triplet representing the value.</p>
*
* @param toWrite Metadata object to calculate the length of.
* @return Length of the given metadata object in bytes when encoded as a local set.
*
* @throws NullPointerException Cannot calculate a length for a <code>null</code> value.
*
* @see #writeBERLength(long, int, ByteBuffer)
*/
public static final @UInt64 long lengthOfLocalSet(
MetadataObject toWrite)
throws NullPointerException {
if (toWrite == null)
throw new NullPointerException("Cannot calculat the length as a local set value for a null metadata object.");
ClassDefinition classOfMetadata = MediaEngine.getClassDefinition(toWrite);
SortedMap<? extends PropertyDefinition,? extends PropertyValue> properties = classOfMetadata.getProperties(toWrite);
long localSetLength = 20l; // Length of the instance ID tag, length and value
for ( PropertyDefinition property : properties.keySet() ) {
if (property.getAUID().equals(CommonConstants.ObjectClassID)) continue;
localSetLength += 4; // Make space for the tag and length
PropertyValue value = properties.get(property);
localSetLength += value.getType().lengthAsBytes(value);
}
return localSetLength;
}
/**
* <p>Determines if the given key represents KLV fill data that can be safely skipped over.</p>
*
* @param key Key to test.
* @return Does the given key represent KLV fill data?
*
* @see #skipKLVFill(ByteBuffer)
*/
public final static boolean isKLVFill(
UL key) {
if (key == null) return false;
if (key.getData1() != MXFConstants.KLVFill.getData1()) return false;
if (key.getData2() != MXFConstants.KLVFill.getData2()) return false;
if (key.getData3() != MXFConstants.KLVFill.getData3()) return false;
byte[] firstPartTest = key.getData4();
byte[] firstPartShouldBe = MXFConstants.KLVFill.getData4();
for (int u = 0 ; u < 7 ; u++ )
if (firstPartTest[u] != firstPartShouldBe[u]) return false;
return true;
}
private final static void testMetaDictionaryEntry(
MetaDefinition metaDefinition) {
if (metaDefinition instanceof ClassDefinition) {
ClassDefinition checkForClass =
ClassDefinitionImpl.forAUID(metaDefinition.getAUID());
if (checkForClass == null)
System.err.println("Warning: A class with name " + metaDefinition.getName() + " is not known to this MAJ runtime.");
return;
}
if (metaDefinition instanceof PropertyDefinition) {
if (!ClassDefinitionImpl.isKnownProperty((PropertyDefinition) metaDefinition))
System.err.println("Warning: A property with name " + metaDefinition.getName() + " is not known to this MAJ runtime.");
return;
}
if (metaDefinition instanceof TypeDefinition) {
TypeDefinition checkForType =
Warehouse.lookForType(metaDefinition.getAUID());
if (checkForType == null)
System.err.println("Warning A type with name " + metaDefinition.getName() + " is not knowmn to this MAJ runtime.");
return;
}
return;
}
/**
* <p>Skips over KLV fill information at the current position for an MXF file.</p>
*
* @param mxfFile File with KLV fill data to skip at the current position.
* @return Number of bytes skipped.
*
* @see #skipKLVFill(ByteBuffer)
* @see #isKLVFill(UL)
*/
public static final long skipKLVFill(
MXFFile mxfFile) {
// TODO push methods from implementation into interface
if (!(mxfFile instanceof MXFFileImpl)) return 0l;
MXFFileImpl mxfFileImpl = (MXFFileImpl) mxfFile;
long startPosition = mxfFileImpl.tell();
UL key = mxfFileImpl.readKey();
if (!isKLVFill(key)) {
mxfFileImpl.seek(startPosition);
return 0;
}
long length = mxfFileImpl.readBER();
mxfFileImpl.read((int) length);
return mxfFileImpl.tell() - startPosition;
}
/**
* <p>Skip over KLV fill data at the current position for a buffer, moving the position to the first byte
* beyond the fill data, which may be the end of the buffer.</p>
*
* @param buffer Buffer with KLV fill data to skip at the current position.
* @return Number of bytes skipped over.
*
* @see #skipKLVFill(MXFFile)
* @see #isKLVFill(UL)
*/
public static final long skipKLVFill(
ByteBuffer buffer) {
int startPosition = buffer.position();
int preserveLimit = buffer.limit();
UL key = readKey(buffer);
if (!isKLVFill(key)) {
buffer.position(startPosition);
return 0l;
}
long length = readBERLength(buffer);
if (length < buffer.remaining())
buffer.position((int) (buffer.position() + length));
else
buffer.position(preserveLimit);
return (buffer.position() - startPosition);
}
/**
* <p>Reads a basic encoding rules (BER) length from the given buffer at the current
* position. For more details on BER lengths, see <a href="http://en.wikipedia.org/wiki/KLV">the
* wikipedia description of KLV data encoding</a>.</p>
*
* @param buffer Buffer to read the BER length from.
* @return Decoded length value.
*
* @throws NullPointerException The given buffer is <code>null</code>.
* @throws BufferUnderflowException Insufficient bytes remaining in the buffer to read
* the length.
*
* @see #writeBERLength(long, int, ByteBuffer)
*/
public final static long readBERLength(
ByteBuffer buffer)
throws NullPointerException,
BufferUnderflowException {
if (buffer == null)
throw new NullPointerException("Cannot read a BER length from a null buffer.");
byte first = 0;
try {
first = buffer.get();
}
catch (BufferUnderflowException bue) {
System.err.println("Incomplete BER length in buffer at 0x" + Long.toHexString(buffer.position()));
return -1l;
}
if (first >= 0) // top bit set not set
return (long) first;
int berTailLength = (int) (first & 0x7f);
byte[] lengthData = new byte[berTailLength];
if (buffer.remaining() < berTailLength) {
System.err.println("Incomplete BER length in buffer at 0x" + Long.toHexString(buffer.position()));
return -1l;
}
long lengthValue = 0l;
buffer.get(lengthData);
for ( int u = 0 ; u < lengthData.length ; u++ )
lengthValue = (lengthValue << 8) |
(((lengthData[u]) >= 0) ? lengthData[u] : 256 + lengthData[u]);
return lengthValue;
}
/**
* <p>Encodes the given length value using the basic encoding rules (BER) for lengths and writes
* it to the given buffer. For more details on BER lengths, see <a href="http://en.wikipedia.org/wiki/KLV">the
* wikipedia description of KLV data encoding</a>.</p>
*
* @param length Length value to encode.
* @param encodedBytes Number of bytes to use to encode the value.
* @param buffer Buffer to write the encoded BER value to.
*
* @throws NullPointerException The given buffer is <code>null</code>.
* @throws BufferOverflowException Insufficient bytes remaining in the buffer to write the given length
* value.
* @throws IllegalArgumentException The length value is negative or will not fit in the number
* of encoded bytes specified.
*/
public final static void writeBERLength(
long length,
int encodedBytes,
ByteBuffer buffer)
throws NullPointerException,
BufferOverflowException,
IllegalArgumentException {
if (buffer == null)
throw new NullPointerException("Cannot write a BER length to a null buffer.");
if (length < 0)
throw new IllegalArgumentException("Cannot write a negative length value.");
if ((encodedBytes < 1) || (encodedBytes > 9))
throw new IllegalArgumentException("The number of encoded bytes must be between 1 and 9.");
if (encodedBytes == 1) {
if (length > 127)
throw new IllegalArgumentException("The number of encoded bytes is 1 but the length is greater than 127.");
buffer.put((byte) length);
return;
}
encodedBytes--;
long maxValue = 2l << (encodedBytes * 8);
if (length >= maxValue)
throw new IllegalArgumentException("The given length of " + length + " is greater than the maximum value for the given number of bytes to encode at " + maxValue + ".");
// Write the length byte
buffer.put((byte) (0x80 + (byte) encodedBytes));
for ( int x = (encodedBytes - 1) ; x >= 0 ; x-- ) {
long mask = 0xff << (x * 8);
buffer.put((byte) ((length & mask) >> (x * 8)));
}
}
/**
* <p>Read a 16-byte universal label from the specified byte buffer.</p>
*
* @param keyData Buffer containing a key at the current position.
* @return Key as a universal label or <code>null</code> if insufficient data was available or
* the key is invalid.
*
* @see #writeKey(UL, ByteBuffer)
*/
public final static UL readKey(
ByteBuffer keyData) {