Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static FillItem fromTriplet(Triplet triplet) throws KLVException {

FillItem fi = new FillItem();

if (!getKey().equalsIgnoreVersion(triplet.getKey())) {
if (!isInstance(triplet.getKey())) {
return null;
}

Expand Down
61 changes: 56 additions & 5 deletions java-common/src/main/java/com/sandflow/smpte/mxf/MXFFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@

package com.sandflow.smpte.mxf;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.SeekableByteChannel;

import com.sandflow.smpte.klv.KLVDataInput;
import com.sandflow.smpte.klv.LocalTagRegister;
import com.sandflow.smpte.klv.MemoryTriplet;
import com.sandflow.smpte.klv.Triplet;
import com.sandflow.smpte.klv.exceptions.KLVException;
Expand Down Expand Up @@ -180,6 +182,53 @@ public static EssenceElementInfo nextElement(InputStream is) throws IOException,
return new EssenceElementInfo(elementKey, elementLength, sid);
}

/**
* Skips triplets until a Partition Pack is found, and reads it
*
* @param kis KLV input
* @return First Partition Pack read, or null if the end of the input is
* reached before one is found
* @throws IOException
* @throws KLVException
*/
public static PartitionPack skipToPartitionPack(KLVDataInput kis) throws IOException, KLVException {
try {
while (true) {
PartitionPack pp = PartitionPack.fromTriplet(kis.readTriplet());

if (pp != null) {
return pp;
}
}
} catch (EOFException e) {
return null;
}
}

/**
* Skips any Fill Items that follow a Partition Pack, and reads the Primer Pack.
*
* @param kis Input positioned after a Partition Pack
* @return Local Tag Register, or null if the first non-Fill Item is not a
* Primer Pack or the end of the input is reached
* @throws IOException
* @throws KLVException
*/
public static LocalTagRegister skipToPrimerPack(KLVDataInput kis) throws IOException, KLVException {
try {
while (true) {
kis.resetCount();
Triplet t = kis.readTriplet();

if (!FillItem.isInstance(t.getKey())) {
return PrimerPack.createLocalTagRegister(t);
}
}
} catch (EOFException e) {
return null;
}
}

/**
* Seeks to the footer partition, assuming the current position of the
* channel is within the run-in (SMPTE ST 377-1 Section 6.5), the footer
Expand All @@ -197,9 +246,11 @@ public static long seekFooterPartition(SeekableByteChannel mxffile) throws IOExc

KLVDataInput kis = new KLVDataInput(Channels.newInputStream(mxffile));

Triplet t = kis.readTriplet();
Triplet t;

if (t == null) {
try {
t = kis.readTriplet();
} catch (EOFException e) {
return -1;
}

Expand Down Expand Up @@ -232,9 +283,9 @@ public static long seekFooterPartition(SeekableByteChannel mxffile) throws IOExc

kis = new KLVDataInput(Channels.newInputStream(mxffile));

t = kis.readTriplet();

if (t == null) {
try {
t = kis.readTriplet();
} catch (EOFException e) {
return -1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@
import com.sandflow.smpte.klv.Triplet;
import com.sandflow.smpte.klv.exceptions.KLVException;
import com.sandflow.smpte.mxf.FillItem;
import com.sandflow.smpte.mxf.HeaderMetadataSet;
import com.sandflow.smpte.mxf.MXFDataInput;
import com.sandflow.smpte.mxf.MXFEvent;
import com.sandflow.smpte.mxf.MXFException;
import com.sandflow.smpte.mxf.MXFFiles;
import com.sandflow.smpte.mxf.PartitionPack;
import com.sandflow.smpte.mxf.PrimerPack;
import com.sandflow.smpte.regxml.dict.DefinitionResolver;
import com.sandflow.smpte.regxml.dict.definitions.ClassDefinition;
import com.sandflow.smpte.regxml.dict.definitions.Definition;
Expand All @@ -65,21 +66,6 @@ public class MXFFragmentBuilder {

private static final UL PREFACE_KEY = UL.fromURN("urn:smpte:ul:060e2b34.027f0101.0d010101.01012f00");

private static final UL INSTANCE_UID_ITEM_UL = UL.fromURN("urn:smpte:ul:060e2b34.01010101.01011502.00000000");

/**
* Returns the Instance ID (InstanceUID property) of a Group, or null if the
* Group does not carry one.
*/
private static UUID getInstanceID(Group group) {
for (Triplet t : group.getItems()) {
if (INSTANCE_UID_ITEM_UL.equalsIgnoreVersion(t.getKey())) {
return new UUID(t.getValue());
}
}
return null;
}

/**
* Returns a DOM Document Fragment containing a RegXML Fragment rooted at
* the first Header Metadata object with a class that descends from the
Expand Down Expand Up @@ -115,14 +101,7 @@ public static DocumentFragment fromInputStream(
/* look for the partition pack */
MXFDataInput kis = new MXFDataInput(mxfpartition);

PartitionPack pp = null;

for (Triplet t; (t = kis.readTriplet()) != null;) {

if ((pp = PartitionPack.fromTriplet(t)) != null) {
break;
}
}
PartitionPack pp = MXFFiles.skipToPartitionPack(kis);

if (pp == null) {

Expand All @@ -132,21 +111,8 @@ public static DocumentFragment fromInputStream(

}

/* start counting header metadata bytes */
kis.resetCount();

/* look for the primer pack */
LocalTagRegister localreg = null;

for (Triplet t; (t = kis.readTriplet()) != null; kis.resetCount()) {

/* skip fill items, if any */
if (!FillItem.getKey().equalsIgnoreVersion(t.getKey())) {
localreg = PrimerPack.createLocalTagRegister(t);
break;
}

}
/* look for the primer pack and start counting header metadata bytes */
LocalTagRegister localreg = MXFFiles.skipToPrimerPack(kis);

if (localreg == null) {

Expand All @@ -159,9 +125,9 @@ public static DocumentFragment fromInputStream(
ArrayList<Group> gs = new ArrayList<>();
HashMap<UUID, Group> setresolver = new HashMap<>();

for (Triplet t;
kis.getReadCount() < pp.getHeaderByteCount()
&& (t = kis.readTriplet()) != null;) {
while (kis.getReadCount() < pp.getHeaderByteCount()) {

Triplet t = kis.readTriplet();

if (INDEX_TABLE_SEGMENT_UL.equalsIgnoreVersion(t.getKey())) {

Expand All @@ -174,23 +140,23 @@ public static DocumentFragment fromInputStream(

break;

} else if (FillItem.getKey().equalsIgnoreVersion(t.getKey())) {
} else if (FillItem.isInstance(t.getKey())) {

/* skip fill items */
continue;
}

try {
Group g = Set.fromLocalSet(t, localreg);
Set s = Set.fromLocalSet(t, localreg);

if (g != null) {
if (s != null) {

gs.add(g);
gs.add(s);

UUID instanceID = getInstanceID(g);
UUID instanceID = HeaderMetadataSet.getInstanceID(s);

if (instanceID != null) {
setresolver.put(instanceID, g);
setresolver.put(instanceID, s);
}

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

package com.sandflow.smpte.mxf;

import java.io.EOFException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -178,9 +179,10 @@ public RandomAccessFileInfo(RandomAccessInputSource raip, EventHandler evthandle
raip.position(raip.size() - ripSize);

RandomIndexPack rip = null;
Triplet t = mis.readTriplet();
if (t != null) {
rip = RandomIndexPack.fromTriplet(t);
try {
rip = RandomIndexPack.fromTriplet(mis.readTriplet());
} catch (EOFException e) {
/* reported below */
}
if (rip == null) {
/* no RIP where it should be */
Expand All @@ -203,9 +205,10 @@ public RandomAccessFileInfo(RandomAccessInputSource raip, EventHandler evthandle
/* seek to and read partition */
raip.position(rip.getOffsets().get(i).getOffset());
PartitionPack pp = null;
t = mis.readTriplet();
if (t != null) {
pp = PartitionPack.fromTriplet(t);
try {
pp = PartitionPack.fromTriplet(mis.readTriplet());
} catch (EOFException e) {
/* reported below */
}
if (pp == null) {
/* no partition pack where expected */
Expand Down Expand Up @@ -301,7 +304,7 @@ public RandomAccessFileInfo(RandomAccessInputSource raip, EventHandler evthandle
Triplet it = mis.readTriplet();

/* skip fill items that pad the index byte count to the KAG */
if (FillItem.getKey().equalsIgnoreVersion(it.getKey())) {
if (FillItem.isInstance(it.getKey())) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,8 @@ private static Preface readHeaderMetadataFrom(InputStream is, long headerByteCou
throws IOException, KLVException, MXFException {
MXFDataInput mis = new MXFDataInput(is);

/* look for the primer pack */
LocalTagRegister localreg = null;
for (Triplet t; (t = mis.readTriplet()) != null; mis.resetCount()) {

/* skip fill items, if any */
if (!FillItem.getKey().equalsIgnoreVersion(t.getKey())) {
localreg = PrimerPack.createLocalTagRegister(t);
break;
}
}
/* look for the primer pack and start counting header metadata bytes */
LocalTagRegister localreg = MXFFiles.skipToPrimerPack(mis);

if (localreg == null) {
MXFException.handle(evthandler, new MXFEvent(
Expand Down Expand Up @@ -154,12 +146,7 @@ public StreamingFileInfo(InputStream is, EventHandler evthandler)
MXFDataInput mis = new MXFDataInput(is);

/* look for the header partition pack */
PartitionPack pp = null;
for (Triplet t; (t = mis.readTriplet()) != null;) {
if ((pp = PartitionPack.fromTriplet(t)) != null) {
break;
}
}
PartitionPack pp = MXFFiles.skipToPartitionPack(mis);

if (pp == null) {
MXFException.handle(evthandler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
import com.sandflow.smpte.mxf.MXFInputContext;
import com.sandflow.smpte.mxf.MXFDataInput;
import com.sandflow.smpte.mxf.MXFException;
import com.sandflow.smpte.mxf.MXFFiles;
import com.sandflow.smpte.mxf.PartitionPack;
import com.sandflow.smpte.mxf.PrimerPack;
import com.sandflow.smpte.mxf.RandomIndexPack;
import com.sandflow.smpte.mxf.StaticLocalTags;
import com.sandflow.smpte.mxf.types.IndexTableSegment;
Expand Down Expand Up @@ -155,16 +155,8 @@ public boolean handle(Event evt) {
/* read header metadata */
if (pp.getHeaderByteCount() > 0) {

/* look for the primer pack */
LocalTagRegister localreg = null;
for (Triplet t; (t = mis.readTriplet()) != null; mis.resetCount()) {

/* skip fill items, if any */
if (!FillItem.getKey().equalsIgnoreVersion(t.getKey())) {
localreg = PrimerPack.createLocalTagRegister(t);
break;
}
}
/* look for the primer pack and start counting header metadata bytes */
LocalTagRegister localreg = MXFFiles.skipToPrimerPack(mis);

/*
* capture all local sets within the header metadata
Expand Down Expand Up @@ -219,7 +211,7 @@ public void handleEvent(Event evt) throws MXFException {
while (mis.getReadCount() < pp.getIndexByteCount()) {
Triplet t = mis.readTriplet();

if (FillItem.getKey().equalsIgnoreVersion(t.getKey())) {
if (FillItem.isInstance(t.getKey())) {
continue;
}

Expand Down
Loading