Skip to content

Commit

Permalink
[AVCCodecHandler] Some code cleanups
Browse files Browse the repository at this point in the history
Most rilevant change,
add our implementation of ReadGolomb
in order to remove bento4 custom patch
  • Loading branch information
CastagnaIT committed Aug 24, 2024
1 parent 2c699a5 commit 336cd62
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 44 deletions.
42 changes: 30 additions & 12 deletions src/codechandler/AVCCodecHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@

using namespace UTILS;

namespace
{
unsigned int ReadGolomb(AP4_BitReader& bits)
{
unsigned int leading_zeros = 0;
while (bits.ReadBit() == 0)
{
leading_zeros++;
if (leading_zeros > 32)
return 0; // safeguard
}
if (leading_zeros)
{
return (1 << leading_zeros) - 1 + bits.ReadBits(leading_zeros);
}
else
{
return 0;
}
}
} // unnamed namespace

AVCCodecHandler::AVCCodecHandler(AP4_SampleDescription* sd)
: CodecHandler{sd},
m_countPictureSetIds{0},
Expand Down Expand Up @@ -106,7 +128,8 @@ void AVCCodecHandler::UpdatePPSId(const AP4_DataBuffer& buffer)
if (!m_needSliceInfo)
return;

//Search the Slice header NALU
// Iterate data to find all NALU units slice headers of type 5 "Coded slice of an IDR picture"
// to get the pic_parameter_set_id value of last NALU
const AP4_Byte* data(buffer.GetData());
AP4_Size dataSize(buffer.GetDataSize());
for (; dataSize;)
Expand Down Expand Up @@ -145,25 +168,20 @@ void AVCCodecHandler::UpdatePPSId(const AP4_DataBuffer& buffer)
if (m_countPictureSetIds < 2)
m_needSliceInfo = false;

unsigned int nal_unit_type = *data & 0x1F;
unsigned int nalUnitType = *data & 0x1F;

if (
//nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_OF_NON_IDR_PICTURE ||
nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_OF_IDR_PICTURE //||
//nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_DATA_PARTITION_A ||
//nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_DATA_PARTITION_B ||
//nal_unit_type == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_DATA_PARTITION_C
)
// Following code is a simplification of AP4_AvcFrameParser::ParseSliceHeader from AP4_AvcFrameParser::Feed
if (nalUnitType == AP4_AVC_NAL_UNIT_TYPE_CODED_SLICE_OF_IDR_PICTURE)
{
AP4_DataBuffer unescaped(data, dataSize);
AP4_NalParser::Unescape(unescaped);
AP4_BitReader bits(unescaped.GetData(), unescaped.GetDataSize());

bits.SkipBits(8); // NAL Unit Type

AP4_AvcFrameParser::ReadGolomb(bits); // first_mb_in_slice
AP4_AvcFrameParser::ReadGolomb(bits); // slice_type
m_pictureId = AP4_AvcFrameParser::ReadGolomb(bits); //picture_set_id
ReadGolomb(bits); // first_mb_in_slice
ReadGolomb(bits); // slice_type
m_pictureId = ReadGolomb(bits); // pic_parameter_set_id
}
// move to the next NAL unit
data += naluSize;
Expand Down
54 changes: 28 additions & 26 deletions src/codechandler/AVCCodecHandler.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
/*
* Copyright (C) 2022 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/

#pragma once

#include "CodecHandler.h"

class ATTR_DLL_LOCAL AVCCodecHandler : public CodecHandler
{
public:
AVCCodecHandler(AP4_SampleDescription* sd);
bool ExtraDataToAnnexB() override;
void UpdatePPSId(const AP4_DataBuffer& buffer) override;
bool GetInformation(kodi::addon::InputstreamInfo& info) override;
STREAMCODEC_PROFILE GetProfile() override { return m_codecProfile; };

private:
unsigned int m_countPictureSetIds;
STREAMCODEC_PROFILE m_codecProfile;
bool m_needSliceInfo;
};
/*
* Copyright (C) 2022 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/

#pragma once

#include "CodecHandler.h"

class ATTR_DLL_LOCAL AVCCodecHandler : public CodecHandler
{
public:
AVCCodecHandler(AP4_SampleDescription* sd);
bool ExtraDataToAnnexB() override;
void UpdatePPSId(const AP4_DataBuffer& buffer) override;
bool GetInformation(kodi::addon::InputstreamInfo& info) override;
STREAMCODEC_PROFILE GetProfile() override { return m_codecProfile; };

private:
unsigned int m_countPictureSetIds;
STREAMCODEC_PROFILE m_codecProfile;
bool m_needSliceInfo;
AP4_UI08 m_pictureId{0};
AP4_UI08 m_pictureIdPrev{AP4_AVC_PPS_MAX_ID};
};
9 changes: 3 additions & 6 deletions src/codechandler/CodecHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
class ATTR_DLL_LOCAL CodecHandler
{
public:
CodecHandler(AP4_SampleDescription* sd)
: m_sampleDescription(sd), m_naluLengthSize(0), m_pictureId(0), m_pictureIdPrev(0xFF){};
virtual ~CodecHandler(){};
CodecHandler(AP4_SampleDescription* sd) : m_sampleDescription(sd) {}
virtual ~CodecHandler() = default;

virtual void UpdatePPSId(const AP4_DataBuffer& buffer) {}

Expand All @@ -46,9 +45,7 @@ class ATTR_DLL_LOCAL CodecHandler

AP4_SampleDescription* m_sampleDescription;
AP4_DataBuffer m_extraData;
AP4_UI08 m_naluLengthSize;
AP4_UI08 m_pictureId;
AP4_UI08 m_pictureIdPrev;
AP4_UI08 m_naluLengthSize{0};

protected:
bool UpdateInfoCodecName(kodi::addon::InputstreamInfo& info, const char* codecName);
Expand Down

0 comments on commit 336cd62

Please sign in to comment.