-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #314 from PaytonGarland/heif-impl
Basic HEIF Implementation
- Loading branch information
Showing
25 changed files
with
1,499 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2002-2017 Drew Noakes | ||
* | ||
* 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. | ||
* | ||
* More information about this project is available at: | ||
* | ||
* https://drewnoakes.com/code/exif/ | ||
* https://github.com/drewnoakes/metadata-extractor | ||
*/ | ||
package com.drew.imaging.heif; | ||
|
||
import com.drew.lang.SequentialReader; | ||
import com.drew.lang.annotations.NotNull; | ||
import com.drew.metadata.Metadata; | ||
import com.drew.metadata.heif.HeifDirectory; | ||
import com.drew.metadata.heif.boxes.Box; | ||
|
||
import java.io.IOException; | ||
|
||
public abstract class HeifHandler<T extends HeifDirectory> | ||
{ | ||
protected Metadata metadata; | ||
protected T directory; | ||
|
||
public HeifHandler(Metadata metadata) | ||
{ | ||
this.metadata = metadata; | ||
this.directory = getDirectory(); | ||
metadata.addDirectory(directory); | ||
} | ||
|
||
protected abstract T getDirectory(); | ||
|
||
protected abstract boolean shouldAcceptBox(@NotNull Box box); | ||
|
||
protected abstract boolean shouldAcceptContainer(@NotNull Box box); | ||
|
||
protected abstract HeifHandler processBox(@NotNull Box box, @NotNull byte[] payload) throws IOException; | ||
|
||
/** | ||
* There is potential for a box to both contain other boxes and contain information, so this method will | ||
* handle those occurences. | ||
*/ | ||
protected abstract void processContainer(@NotNull Box box, @NotNull SequentialReader reader) throws IOException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2002-2017 Drew Noakes | ||
* | ||
* 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. | ||
* | ||
* More information about this project is available at: | ||
* | ||
* https://drewnoakes.com/code/exif/ | ||
* https://github.com/drewnoakes/metadata-extractor | ||
*/ | ||
package com.drew.imaging.heif; | ||
|
||
import com.drew.imaging.mp4.Mp4Reader; | ||
import com.drew.lang.annotations.NotNull; | ||
import com.drew.metadata.Metadata; | ||
import com.drew.metadata.heif.HeifBoxHandler; | ||
import com.drew.metadata.mp4.Mp4BoxHandler; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.zip.DataFormatException; | ||
|
||
public class HeifMetadataReader | ||
{ | ||
@NotNull | ||
public static Metadata readMetadata(@NotNull InputStream inputStream) throws IOException | ||
{ | ||
try { | ||
Metadata metadata = new Metadata(); | ||
new HeifReader().extract(metadata, inputStream, new HeifBoxHandler(metadata)); | ||
return metadata; | ||
} catch (DataFormatException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2002-2017 Drew Noakes | ||
* | ||
* 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. | ||
* | ||
* More information about this project is available at: | ||
* | ||
* https://drewnoakes.com/code/exif/ | ||
* https://github.com/drewnoakes/metadata-extractor | ||
*/ | ||
package com.drew.imaging.heif; | ||
|
||
import com.drew.lang.StreamReader; | ||
import com.drew.metadata.Metadata; | ||
import com.drew.metadata.heif.boxes.Box; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.zip.DataFormatException; | ||
|
||
public class HeifReader | ||
{ | ||
public void extract(Metadata metadata, InputStream inputStream, HeifHandler handler) throws IOException, DataFormatException | ||
{ | ||
StreamReader reader = new StreamReader(inputStream); | ||
reader.setMotorolaByteOrder(true); | ||
|
||
processBoxes(reader, -1, handler); | ||
} | ||
|
||
private void processBoxes(StreamReader reader, long atomEnd, HeifHandler handler) | ||
{ | ||
try { | ||
while ((atomEnd == -1) ? true : reader.getPosition() < atomEnd) { | ||
|
||
Box box = new Box(reader); | ||
|
||
// Determine if fourCC is container/atom and process accordingly | ||
// Unknown atoms will be skipped | ||
|
||
if (handler.shouldAcceptContainer(box)) { | ||
handler.processContainer(box, reader); | ||
processBoxes(reader, box.size + reader.getPosition() - 8, handler); | ||
} else if (handler.shouldAcceptBox(box)) { | ||
handler = handler.processBox(box, reader.getBytes((int)box.size - 8)); | ||
} else if (box.size > 1) { | ||
reader.skip(box.size - 8); | ||
} else if (box.size == -1) { | ||
break; | ||
} | ||
} | ||
} catch (IOException e) { | ||
// Currently, reader relies on IOException to end | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright 2002-2017 Drew Noakes | ||
* | ||
* 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. | ||
* | ||
* More information about this project is available at: | ||
* | ||
* https://drewnoakes.com/code/exif/ | ||
* https://github.com/drewnoakes/metadata-extractor | ||
*/ | ||
package com.drew.metadata.heif; | ||
|
||
import com.drew.imaging.heif.HeifHandler; | ||
import com.drew.lang.SequentialByteArrayReader; | ||
import com.drew.lang.SequentialReader; | ||
import com.drew.lang.annotations.NotNull; | ||
import com.drew.metadata.Metadata; | ||
import com.drew.metadata.heif.boxes.*; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Payton Garland | ||
*/ | ||
public class HeifBoxHandler extends HeifHandler<HeifDirectory> | ||
{ | ||
HandlerBox handlerBox; | ||
|
||
private HeifHandlerFactory handlerFactory = new HeifHandlerFactory(this); | ||
|
||
public HeifBoxHandler(Metadata metadata) | ||
{ | ||
super(metadata); | ||
} | ||
|
||
@Override | ||
protected HeifDirectory getDirectory() | ||
{ | ||
return new HeifDirectory(); | ||
} | ||
|
||
@Override | ||
public boolean shouldAcceptBox(@NotNull Box box) | ||
{ | ||
List<String> boxes = Arrays.asList(HeifBoxTypes.BOX_FILE_TYPE, | ||
HeifBoxTypes.BOX_HANDLER, | ||
HeifBoxTypes.BOX_HVC1); | ||
|
||
return boxes.contains(box.type); | ||
} | ||
|
||
@Override | ||
public boolean shouldAcceptContainer(Box box) | ||
{ | ||
return box.type.equals(HeifContainerTypes.BOX_METADATA) | ||
|| box.type.equals(HeifContainerTypes.BOX_IMAGE_PROPERTY) | ||
|| box.type.equals(HeifContainerTypes.BOX_ITEM_PROPERTY); | ||
} | ||
|
||
@Override | ||
public HeifHandler processBox(@NotNull Box box, @NotNull byte[] payload) throws IOException | ||
{ | ||
if (payload != null) { | ||
SequentialReader reader = new SequentialByteArrayReader(payload); | ||
if (box.type.equals(HeifBoxTypes.BOX_FILE_TYPE)) { | ||
processFileType(reader, box); | ||
}else if (box.type.equals(HeifBoxTypes.BOX_HANDLER)) { | ||
handlerBox = new HandlerBox(reader, box); | ||
return handlerFactory.getHandler(handlerBox, metadata); | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
public void processContainer(@NotNull Box box, @NotNull SequentialReader reader) throws IOException | ||
{ | ||
if (box.type.equals(HeifContainerTypes.BOX_METADATA)) { | ||
new FullBox(reader, box); | ||
} | ||
} | ||
|
||
private void processFileType(@NotNull SequentialReader reader, @NotNull Box box) throws IOException | ||
{ | ||
FileTypeBox fileTypeBox = new FileTypeBox(reader, box); | ||
fileTypeBox.addMetadata(directory); | ||
if (!fileTypeBox.getCompatibleBrands().contains("mif1")) { | ||
directory.addError("File Type Box does not contain required brand, mif1"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2002-2017 Drew Noakes | ||
* | ||
* 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. | ||
* | ||
* More information about this project is available at: | ||
* | ||
* https://drewnoakes.com/code/exif/ | ||
* https://github.com/drewnoakes/metadata-extractor | ||
*/ | ||
package com.drew.metadata.heif; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* @author Payton Garland | ||
*/ | ||
public class HeifBoxTypes | ||
{ | ||
public static final String BOX_FILE_TYPE = "ftyp"; | ||
public static final String BOX_PRIMARY_ITEM = "pitm"; | ||
public static final String BOX_ITEM_PROTECTION = "ipro"; | ||
public static final String BOX_ITEM_INFO = "iinf"; | ||
public static final String BOX_ITEM_LOCATION = "iloc"; | ||
public static final String BOX_HANDLER = "hdlr"; | ||
public static final String BOX_HVC1 = "hvc1"; | ||
public static final String BOX_IMAGE_SPATIAL_EXTENTS = "ispe"; | ||
public static final String BOX_AUXILIARY_TYPE_PROPERTY = "auxC"; | ||
public static final String BOX_HEVC_CONFIGURATION = "hvcC"; | ||
|
||
public static ArrayList<String> _boxList = new ArrayList<String>(); | ||
|
||
static { | ||
_boxList.add(BOX_FILE_TYPE); | ||
_boxList.add(BOX_ITEM_PROTECTION); | ||
_boxList.add(BOX_PRIMARY_ITEM); | ||
_boxList.add(BOX_ITEM_INFO); | ||
_boxList.add(BOX_ITEM_LOCATION); | ||
_boxList.add(BOX_HANDLER); | ||
_boxList.add(BOX_HVC1); | ||
_boxList.add(BOX_IMAGE_SPATIAL_EXTENTS); | ||
_boxList.add(BOX_AUXILIARY_TYPE_PROPERTY); | ||
_boxList.add(BOX_HEVC_CONFIGURATION); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2002-2017 Drew Noakes | ||
* | ||
* 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. | ||
* | ||
* More information about this project is available at: | ||
* | ||
* https://drewnoakes.com/code/exif/ | ||
* https://github.com/drewnoakes/metadata-extractor | ||
*/ | ||
package com.drew.metadata.heif; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* @author Payton Garland | ||
*/ | ||
public class HeifContainerTypes | ||
{ | ||
public static final String BOX_METADATA = "meta"; | ||
public static final String BOX_IMAGE_PROPERTY = "iprp"; | ||
public static final String BOX_ITEM_PROPERTY = "ipco"; | ||
|
||
public static ArrayList<String> _containerList = new ArrayList<String>(); | ||
|
||
static { | ||
_containerList.add(BOX_METADATA); | ||
_containerList.add(BOX_IMAGE_PROPERTY); | ||
_containerList.add(BOX_ITEM_PROPERTY); | ||
} | ||
} |
Oops, something went wrong.