Skip to content

Commit

Permalink
[WIP] Base Media File Format
Browse files Browse the repository at this point in the history
  • Loading branch information
1div0 committed Feb 16, 2021
1 parent c608148 commit f0a321d
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 100 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ option( EXIV2_ENABLE_WIN_UNICODE "Use Unicode paths (wstring) on Windows"
option( EXIV2_ENABLE_WEBREADY "Build webready support into library" OFF )
option( EXIV2_ENABLE_CURL "USE Libcurl for HttpIo (WEBREADY)" OFF )
option( EXIV2_ENABLE_SSH "USE Libssh for SshIo (WEBREADY)" OFF )
option( EXIV2_ENABLE_ISOBMFF "Build with ISO BMFF support" OFF )
option( EXIV2_ENABLE_BMFF "Build with BMFF support" OFF )

option( EXIV2_BUILD_SAMPLES "Build sample applications" ON )
option( EXIV2_BUILD_EXIV2_COMMAND "Build exiv2 command-line executable" ON )
Expand Down
4 changes: 2 additions & 2 deletions cmake/config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
// Define if you want translation of program messages to the user's native language
#cmakedefine EXV_ENABLE_NLS

// Define if you want ISO BMFF support.
#cmakedefine EXV_ENABLE_ISOBMFF
// Define if you want BMFF support.
#cmakedefine EXV_ENABLE_BMFF

// Define if you want video support.
#cmakedefine EXV_ENABLE_VIDEO
Expand Down
2 changes: 1 addition & 1 deletion cmake/generateConfigFile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if (${EXIV2_ENABLE_WEBREADY})
set(EXV_USE_SSH ${EXIV2_ENABLE_SSH})
set(EXV_USE_CURL ${EXIV2_ENABLE_CURL})
endif()
set(EXV_ENABLE_ISOBMFF ${EXIV2_ENABLE_ISOBMFF})
set(EXV_ENABLE_BMFF ${EXIV2_ENABLE_BMFF})
set(EXV_ENABLE_VIDEO ${EXIV2_ENABLE_VIDEO})
set(EXV_ENABLE_WEBREADY ${EXIV2_ENABLE_WEBREADY})
set(EXV_HAVE_LENSDATA ${EXIV2_ENABLE_LENSDATA})
Expand Down
2 changes: 1 addition & 1 deletion cmake/printSummary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ if ( EXIV2_ENABLE_EXTERNAL_XMP )
else()
OptionOutput( "XMP metadata support: " EXIV2_ENABLE_XMP )
endif()
OptionOutput( "Building ISO BMFF support: " EXIV2_ENABLE_ISOBMFF )
OptionOutput( "Building BMFF support: " EXIV2_ENABLE_BMFF )
OptionOutput( "Native language support: " EXIV2_ENABLE_NLS )
OptionOutput( "Conversion of Windows XP tags: " EXIV2_ENABLE_PRINTUCS2 )
OptionOutput( "Nikon lens database: " EXIV2_ENABLE_LENSDATA )
Expand Down
16 changes: 9 additions & 7 deletions include/exiv2/bmffimage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@
// namespace extensions
namespace Exiv2
{
EXIV2API bool enableISOBMFF(bool enable = true);
EXIV2API bool enableBMFF(bool enable = true);

// *****************************************************************************
// class definitions

// Add ISO Base Media File Format to the supported image formats
// Add Base Media File Format to the supported image formats
namespace ImageType {
const int bmff = 15; //!< ISO BMFF (bmff) image type (see class ISOBMFF)
const int bmff = 15; //!< BMFF (bmff) image type (see class BMFF)
}

/*!
@brief Class to access ISO BMFF images.
@brief Class to access BMFF images.
*/
class EXIV2API BmffImage : public Image {
public:
//! @name Creators
//@{
/*!
@brief Constructor to open a ISO/IEC BMFF image. Since the
@brief Constructor to open a BMFF image. Since the
constructor can not return a result, callers should check the
good() method after object construction to determine success
or failure.
Expand Down Expand Up @@ -108,6 +108,8 @@ namespace Exiv2
void doWriteMetadata(BasicIo& outIo);
//@}

std::string toAscii(long n);

}; // class BmffImage

// *****************************************************************************
Expand All @@ -116,12 +118,12 @@ namespace Exiv2
// These could be static private functions on Image subclasses but then
// ImageFactory needs to be made a friend.
/*!
@brief Create a new ISO BMFF instance and return an auto-pointer to it.
@brief Create a new BMFF instance and return an auto-pointer to it.
Caller owns the returned object and the auto-pointer ensures that
it will be deleted.
*/
EXIV2API Image::AutoPtr newBmffInstance(BasicIo::AutoPtr io, bool create);

//! Check if the file iIo is a ISO BMFF image.
//! Check if the file iIo is a BMFF image.
EXIV2API bool isBmffType(BasicIo& iIo, bool advance);
} // namespace Exiv2
4 changes: 2 additions & 2 deletions include/exiv2/exiv2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
#include "exiv2/image.hpp"
#include "exiv2/ini.hpp"
#include "exiv2/iptc.hpp"
#ifdef EXV_ENABLE_ISOBMFF
#ifdef EXV_ENABLE_BMFF
#include "bmffimage.hpp"
#endif// EXV_ENABLE_ISOBMFF
#endif// EXV_ENABLE_BMFF
#include "exiv2/jp2image.hpp"
#include "exiv2/jpgimage.hpp"
#include "exiv2/metadatum.hpp"
Expand Down
178 changes: 98 additions & 80 deletions src/bmffimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,42 +49,36 @@ struct BmffBoxHeader
uint32_t type;
};

#if defined(__BIG_ENDIAN__)
#define ID(string) ((string[0] << 24) | (string[1] << 16) | (string[2] << 8) | string[3])
#elif defined(__LITTLE_ENDIAN__)
#define ID(string) (string[0] | (string[1] << 8) | (string[2] << 16) | (string[3] << 24))
#else
#error "Unknown endian"
#endif

static const uint32_t ftyp = ID("ftyp"); /**< File type box */
static const uint32_t avif = ID("avif"); /**< AVIF */
static const uint32_t heic = ID("heic"); /**< HEIF */
static const uint32_t heif = ID("heif"); /**< HEIF */
static const uint32_t crx = ID("crx "); /**< Canon CR3 */
static const uint32_t moov = ID("moov"); /**< Movie */
static const uint32_t meta = ID("meta"); /**< Metadata */
static const uint32_t mdat = ID("mdat"); /**< Media data */
static const uint32_t uuid = ID("uuid"); /**< UUID */
static const uint32_t dinf = ID("dinf"); /**< Data information */
static const uint32_t iprp = ID("iprp"); /**< Item properties */
static const uint32_t ipco = ID("ipco"); /**< Item property container */
static const uint32_t iinf = ID("iinf"); /**< Item info */
static const uint32_t iloc = ID("iloc"); /**< Item location */
static const uint32_t ispe = ID("ispe"); /**< Image spatial extents */
#define TAG_ftyp ID("ftyp") /**< File type box */
#define TAG_avif ID("avif") /**< AVIF */
#define TAG_heic ID("heic") /**< HEIF */
#define TAG_heif ID("heif") /**< HEIF */
#define TAG_crx ID("crx ") /**< Canon CR3 */
#define TAG_moov ID("moov") /**< Movie */
#define TAG_meta ID("meta") /**< Metadata */
#define TAG_mdat ID("mdat") /**< Media data */
#define TAG_uuid ID("uuid") /**< UUID */
#define TAG_dinf ID("dinf") /**< Data information */
#define TAG_iprp ID("iprp") /**< Item properties */
#define TAG_ipco ID("ipco") /**< Item property container */
#define TAG_iinf ID("iinf") /**< Item info */
#define TAG_iloc ID("iloc") /**< Item location */
#define TAG_ispe ID("ispe") /**< Image spatial extents */

// *****************************************************************************
// class member definitions
namespace Exiv2
{
static bool enabled = false;

EXIV2API bool enableISOBMFF(bool enable)
EXIV2API bool enableBMFF(bool enable)
{
#ifdef EXV_ENABLE_ISOBMFF
#ifdef EXV_ENABLE_BMFF
enabled = enable;
return true;
#endif // EXV_ENABLE_ISOBMFF
#endif // EXV_ENABLE_BMFF
enable = false; // unused
return enable;
}
Expand All @@ -94,21 +88,11 @@ namespace Exiv2
{
} // BmffImage::BmffImage

static bool isBigEndian()
{
union {
uint32_t i;
char c[4];
} e = { 0x01000000 };

return e.c[0]?true:false;
}

static std::string toAscii(long n)
std::string BmffImage::toAscii(long n)
{
const char* p = (const char*) &n;
std::string result;
bool bBigEndian = isBigEndian();
bool bBigEndian = isBigEndianPlatform();
for ( int i = 0 ; i < 4 ; i++) {
result += p[ bBigEndian ? i : (3-i) ];
}
Expand All @@ -135,35 +119,36 @@ namespace Exiv2

bool superBox(uint32_t box)
{
return box == moov
|| box == dinf
|| box == iprp
|| box == ipco
|| box == meta
|| box == iinf
|| box == iloc
return box == TAG_moov
|| box == TAG_dinf
|| box == TAG_iprp
|| box == TAG_ipco
|| box == TAG_meta
|| box == TAG_iinf
|| box == TAG_iloc
;
}

bool fullBox(uint32_t box)
{
return box == meta
|| box == iinf
|| box == iloc
return box == TAG_meta
|| box == TAG_iinf
|| box == TAG_iloc
;
}

std::string BmffImage::mimeType() const
{
switch (fileType)
{
case avif:
case TAG_avif:
return "image/avif";

case heic:
case heif:
case TAG_heic:
case TAG_heif:
return "image/heif";

case crx:
case TAG_crx:
return "image/x-canon-cr3";

default:
Expand All @@ -174,7 +159,7 @@ namespace Exiv2
void BmffImage::setComment(const std::string& /*comment*/)
{
// Todo: implement me!
throw(Error(kerInvalidSettingForImage, "Image comment", "ISO BMFF"));
throw(Error(kerInvalidSettingForImage, "Image comment", "BMFF"));
} // BmffImage::setComment

void BmffImage::readMetadata()
Expand All @@ -187,7 +172,7 @@ namespace Exiv2
// Ensure that this is the correct image type
if (!isBmffType(*io_, false)) {
if (io_->error() || io_->eof()) throw Error(kerFailedToReadImageData);
throw Error(kerNotAnImage, "ISO BMFF");
throw Error(kerNotAnImage, "BMFF");
}

long position = 0;
Expand All @@ -200,58 +185,91 @@ namespace Exiv2
{
boxes_check(boxes++, boxem);
position = io_->tell();
uint32_t length = getLong((byte*)&box.length, bigEndian);
uint32_t type = getLong((byte*)&box.type, bigEndian);
box.length = getLong((byte*)&box.length, bigEndian);
box.type = getLong((byte*)&box.type, bigEndian);
#ifdef EXIV2_DEBUG_MESSAGES
std::cout << "Exiv2::BmffImage::readMetadata: "
<< "Position: " << position
<< " box type: " << toAscii(type)
<< " length: " << length
<< " box type: " << toAscii(box.type)
<< " box length: " << box.length
<< std::endl;
#endif

if (length == 0) return ;
if (box.length == 0) return ;

if (length == 1)
if (box.length == 1)
{
}

switch (box.type)
if (box.length > 8 && (position + box.length) <= io().size() )
{
case ftyp:
switch (box.type)
{
io().read((byte*)&fileType,4);
std::string brand_ = boxName(fileType);
case TAG_ftyp:
{
DataBuf data(box.length);
io().read(data.pData_, data.size_);
fileType = getLong(data.pData_, bigEndian);
#ifdef EXIV2_DEBUG_MESSAGES
std::cout << "Exiv2::BmffImage::readMetadata: "
<< "Brand: " << brand_
<< std::endl;
std::string brand_ = toAscii(fileType);
std::cout << "Exiv2::BmffImage::readMetadata: "
<< "Brand: " << brand_
<< std::endl;
#endif
break;
}
break;
}

case meta:
{
case TAG_meta:
{
DataBuf data(box.length);
io().read(data.pData_, data.size_);
#ifdef EXIV2_DEBUG_MESSAGES
std::cout << "Exiv2::BmffImage::readMetadata: metadata"
<< std::endl;
std::cout << "Exiv2::BmffImage::readMetadata: metadata "
<< data.size_ << " bytes " << std::endl;
std::cout << std::hex;
for (unsigned i = 0; i < data.size_; i++)
{
std::cout << " " << data.pData_[i];
}
std::cout << std::dec;
std::cout << std::endl;
#endif
break;
}
break;
}

case TAG_ispe:
{
DataBuf data(box.length);
io().read(data.pData_, data.size_);

uint32_t flags = getLong(data.pData_, bigEndian);
uint8_t version = (uint8_t) flags >> 24;
flags &= 0x00ffffff;
pixelWidth_ = getLong(data.pData_ + 4, bigEndian);
pixelHeight_ = getLong(data.pData_ + 8, bigEndian);
#ifdef EXIV2_DEBUG_MESSAGES
std::cout << "Exiv2::BmffImage::readMetadata: Image spatial extents "
<< "version: " << version
<< "flags: " << flags
<< std::endl;
#endif
break;
}

default:
{
default:
{
#ifdef EXIV2_DEBUG_MESSAGES
std::cout << " box type: " << toAscii(type)
<< " length: " << length
<< std::endl;
std::cout << " box type: " << toAscii(box.type)
<< " box length: " << box.length
<< std::endl;
#endif
break;
break;
}
}
}

// Move to the next box.
io_->seek(static_cast<long>(position - sizeof(box) + length), BasicIo::beg);
io_->seek(static_cast<long>(position - sizeof(box) + box.length), BasicIo::beg);
if (io_->error())
throw Error(kerFailedToReadImageData);
}
Expand Down
4 changes: 2 additions & 2 deletions src/exiv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ int main(int argc, char* const argv[])
textdomain(EXV_PACKAGE_NAME);
#endif

#ifdef EXV_ENABLE_ISOBMFF
Exiv2::enableISOBMFF();
#ifdef EXV_ENABLE_BMFF
Exiv2::enableBMFF();
#endif

// Handle command line arguments
Expand Down
Loading

0 comments on commit f0a321d

Please sign in to comment.