Skip to content

Commit

Permalink
evc: add decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
bradh committed Oct 30, 2024
1 parent 76af5e7 commit 657bd9d
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 8 deletions.
2 changes: 2 additions & 0 deletions libheif/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ set(libheif_sources
codecs/avc_dec.cc
codecs/evc_boxes.cc
codecs/evc_boxes.h
codecs/evc_dec.h
codecs/evc_dec.cc
image-items/mask_image.h
image-items/mask_image.cc
image-items/image_item.h
Expand Down
7 changes: 6 additions & 1 deletion libheif/codecs/decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
#include "jpeg_boxes.h"
#include "jpeg2000_boxes.h"
#include "codecs/uncompressed/unc_dec.h"

#include "codecs/evc_dec.h"
#include "evc_boxes.h"

void DataExtent::set_from_image_item(std::shared_ptr<HeifFile> file, heif_item_id item)
{
Expand Down Expand Up @@ -132,6 +133,10 @@ std::shared_ptr<Decoder> Decoder::alloc_for_infe_type(const HeifContext* ctx, he
return std::make_shared<Decoder_uncompressed>(uncC,cmpd);
}
#endif
case fourcc("evc1"): {
auto evcC = ctx->get_heif_file()->get_property<Box_evcC>(id);
return std::make_shared<Decoder_EVC>(evcC);
}
case fourcc("mski"): {
return nullptr; // do we need a decoder for this?
}
Expand Down
21 changes: 17 additions & 4 deletions libheif/codecs/evc_boxes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ std::string Box_evcC::get_chroma_format_as_text() const
{
switch (m_configuration.chroma_format_idc)
{
case 0:
case CHROMA_FORMAT_MONOCHROME:
return std::string("Monochrome");
case 1:
case CHROMA_FORMAT_420:
return std::string("4:2:0");
case 2:
case CHROMA_FORMAT_422:
return std::string("4:2:2");
case 3:
case CHROMA_FORMAT_444:
return std::string("4:4:4");
default:
return std::string("Invalid");
Expand Down Expand Up @@ -169,3 +169,16 @@ Error Box_evcC::write(StreamWriter& writer) const

return Error::Ok;
}

void Box_evcC::get_header_nals(std::vector<uint8_t>& data) const
{
for (const auto& array : m_nal_array) {
for (const auto& nalu : array.nal_units) {
data.push_back((nalu.size() >> 24) & 0xFF);
data.push_back((nalu.size() >> 16) & 0xFF);
data.push_back((nalu.size() >> 8) & 0xFF);
data.push_back((nalu.size() >> 0) & 0xFF);
data.insert(data.end(), nalu.begin(), nalu.end());
}
}
}
10 changes: 7 additions & 3 deletions libheif/codecs/evc_boxes.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@
#include <string>
#include <vector>

// #include "image-items/image_item.h"


class Box_evcC : public Box {
public:
Box_evcC() { set_short_type(fourcc("evcC")); }

bool is_essential() const override { return true; }

static const uint8_t CHROMA_FORMAT_MONOCHROME = 0;
static const uint8_t CHROMA_FORMAT_420 = 1;
static const uint8_t CHROMA_FORMAT_422 = 2;
static const uint8_t CHROMA_FORMAT_444 = 3;

struct configuration {
uint8_t configurationVersion = 1;
uint8_t profile_idc;
Expand Down Expand Up @@ -63,6 +65,8 @@ class Box_evcC : public Box {

Error write(StreamWriter &writer) const override;

void get_header_nals(std::vector<uint8_t>& data) const;

protected:
Error parse(BitstreamRange &range, const heif_security_limits* limits) override;

Expand Down
74 changes: 74 additions & 0 deletions libheif/codecs/evc_dec.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* HEIF codec.
* Copyright (c) 2024 Dirk Farin <[email protected]>
*
* This file is part of libheif.
*
* libheif is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* libheif is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libheif. If not, see <http://www.gnu.org/licenses/>.
*/

#include "evc_dec.h"
#include "evc_boxes.h"
#include "error.h"
#include "context.h"

#include <string>


Result<std::vector<uint8_t>> Decoder_EVC::read_bitstream_configuration_data() const
{
std::vector<uint8_t> data;
m_evcC->get_header_nals(data);
return data;
}


int Decoder_EVC::get_luma_bits_per_pixel() const
{
return m_evcC->get_configuration().bit_depth_luma;
}


int Decoder_EVC::get_chroma_bits_per_pixel() const
{
return m_evcC->get_configuration().bit_depth_chroma;
}


Error Decoder_EVC::get_coded_image_colorspace(heif_colorspace* out_colorspace, heif_chroma* out_chroma) const
{
switch (m_evcC->get_configuration().chroma_format_idc) {
case Box_evcC::CHROMA_FORMAT_MONOCHROME:
*out_chroma = heif_chroma_monochrome;
*out_colorspace = heif_colorspace_monochrome;
return Error::Ok;
case Box_evcC::CHROMA_FORMAT_420:
*out_chroma = heif_chroma_420;
*out_colorspace = heif_colorspace_YCbCr;
return Error::Ok;
case Box_evcC::CHROMA_FORMAT_422:
*out_chroma = heif_chroma_422;
*out_colorspace = heif_colorspace_YCbCr;
return Error::Ok;
case Box_evcC::CHROMA_FORMAT_444:
*out_chroma = heif_chroma_444;
*out_colorspace = heif_colorspace_YCbCr;
return Error::Ok;
default:
*out_chroma = heif_chroma_undefined;
*out_colorspace = heif_colorspace_undefined;
return Error{heif_error_Invalid_input, heif_suberror_Decompression_invalid_data, "unsupported (impossible?) EVC chroma value"};
}
}

53 changes: 53 additions & 0 deletions libheif/codecs/evc_dec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* HEIF codec.
* Copyright (c) 2024 Dirk Farin <[email protected]>
*
* This file is part of libheif.
*
* libheif is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* libheif is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libheif. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef HEIF_EVC_DEC_H
#define HEIF_EVC_DEC_H

#include "libheif/heif.h"
#include "error.h"

#include <memory>
#include <vector>
#include <codecs/decoder.h>

class Box_evcC;


class Decoder_EVC : public Decoder
{
public:
explicit Decoder_EVC(const std::shared_ptr<const Box_evcC>& evcC) : m_evcC(evcC) {}

heif_compression_format get_compression_format() const override { return heif_compression_EVC; }

int get_luma_bits_per_pixel() const override;

int get_chroma_bits_per_pixel() const override;

Error get_coded_image_colorspace(heif_colorspace*, heif_chroma*) const override;

Result<std::vector<uint8_t>> read_bitstream_configuration_data() const override;

private:
const std::shared_ptr<const Box_evcC> m_evcC;
};

#endif

0 comments on commit 657bd9d

Please sign in to comment.