Skip to content

Commit b2557df

Browse files
committed
Make ArrayView default contructable and add Packet::freeSideData()
1 parent a97c82b commit b2557df

6 files changed

Lines changed: 48 additions & 0 deletions

File tree

src/avcpp/avutils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ template<typename T, typename U, class Policy>
438438
class ArrayView : public std::ranges::view_interface<ArrayView<T, U, Policy>>
439439
{
440440
public:
441+
constexpr ArrayView() = default;
441442
constexpr ArrayView(T *ptr, Policy policy)
442443
: m_ptr(ptr), m_policy(std::move(policy))
443444
{}

src/avcpp/frame.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,11 +816,15 @@ FrameSideData FrameCommon::allocateSideData(AVFrameSideDataType type, std::size_
816816

817817
ArrayView<const AVFrameSideData *, FrameSideData, size_t> FrameCommon::sideData() const noexcept
818818
{
819+
if (!m_raw) [[unlikely]]
820+
return {};
819821
return make_array_view_size<FrameSideData>((const AVFrameSideData**)m_raw->side_data, m_raw->nb_side_data);
820822
}
821823

822824
ArrayView<AVFrameSideData *, FrameSideData, size_t> FrameCommon::sideData() noexcept
823825
{
826+
if (!m_raw) [[unlikely]]
827+
return {};
824828
return make_array_view_size<FrameSideData>(m_raw->side_data, m_raw->nb_side_data);
825829
}
826830

src/avcpp/packet.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,14 +400,25 @@ PacketSideData Packet::sideDataIndex(std::size_t index) noexcept
400400

401401
ArrayView<AVPacketSideData, PacketSideData, std::size_t> Packet::sideData() noexcept
402402
{
403+
if (!m_raw) [[unlikely]]
404+
return {};
403405
return make_array_view_size<PacketSideData>(m_raw->side_data, m_raw->side_data_elems);
404406
}
405407

406408
ArrayView<const AVPacketSideData, PacketSideData, std::size_t> Packet::sideData() const noexcept
407409
{
410+
if (!m_raw) [[unlikely]]
411+
return {};
408412
return make_array_view_size<PacketSideData>((const AVPacketSideData*)m_raw->side_data, m_raw->side_data_elems);
409413
}
410414

415+
void Packet::freeSideData() noexcept
416+
{
417+
if (!m_raw) [[unlikely]]
418+
return;
419+
av_packet_free_side_data(m_raw);
420+
}
421+
411422
size_t Packet::sideDataCount() const noexcept
412423
{
413424
return m_raw ? m_raw->side_data_elems : 0;

src/avcpp/packet.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ class Packet :
173173
ArrayView<AVPacketSideData, PacketSideData, std::size_t> sideData() noexcept;
174174
ArrayView<const AVPacketSideData, PacketSideData, std::size_t> sideData() const noexcept;
175175

176+
void freeSideData() noexcept;
177+
176178
/**
177179
* Add side data of the given type into packet. Data will be cloned.
178180
* @param type

tests/Frame.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ TEST_CASE("Core functionality", "[Frame]")
121121
REQUIRE(sd.empty());
122122
}
123123

124+
// Iterate on null frame
125+
{
126+
av::VideoFrame frame;
127+
// its a hack to gen null for the nested m_raw
128+
//auto ptr = frame.raw();
129+
frame.reset();
130+
REQUIRE(frame.raw() == nullptr);
131+
132+
int count{};
133+
for (auto&& sd : frame.sideData()) {
134+
++count;
135+
}
136+
REQUIRE(count == 0);
137+
}
138+
124139
const int view_id = 31337;
125140
const int64_t gop_time = 11223344;
126141

tests/Packet.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,21 @@ TEST_CASE("Packet define", "[Packet][Construct]")
194194
REQUIRE(sd.empty());
195195
}
196196

197+
// Iterate on null packet
198+
{
199+
av::Packet pkt;
200+
// its a hack to gen null for the nested m_raw
201+
//auto ptr = frame.raw();
202+
pkt.reset();
203+
REQUIRE(pkt.raw() == nullptr);
204+
205+
int count{};
206+
for (auto&& sd : pkt.sideData()) {
207+
++count;
208+
}
209+
REQUIRE(count == 0);
210+
}
211+
197212
std::string meta = "field=val;field2=val2";
198213

199214
{

0 commit comments

Comments
 (0)