Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/avcpp/codeccontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,17 +893,28 @@ std::pair<int, const error_category *> CodecContext2::decodeCommon(AVFrame *outF
if (!decodeProc)
return make_error_pair(Errors::CodecInvalidDecodeProc);

if (offset && inPacket.size() && offset >= inPacket.size())
return make_error_pair(Errors::CodecDecodingOffsetToLarge);

// sizeof(AVPacket) is not a part of the public API
std::unique_ptr<AVPacket, av::SmartDeleter> pkt;
auto processPkt = inPacket.raw();
frameFinished = 0;

AVPacket pkt = *inPacket.raw();
pkt.data += offset;
pkt.size -= offset;
if (inPacket.raw() && offset) {
if (offset && inPacket.size() && offset >= inPacket.size())
return make_error_pair(Errors::CodecDecodingOffsetToLarge);

std::error_code ec;
pkt.reset(inPacket.makeRef(ec));
if (ec) {
return {ec.value(), &ec.category()};
}

pkt->data += offset;
pkt->size -= offset;
processPkt = pkt.get();
}

int decoded = decodeProc(m_raw, outFrame, &frameFinished, &pkt);
return make_error_pair(decoded);
// not a "flush" internally: just read-out Frame from output queue
return make_error_pair(decodeProc(m_raw, outFrame, &frameFinished, processPkt));
}

std::pair<int, const error_category *> CodecContext2::encodeCommon(Packet &outPacket, const AVFrame *inFrame, int &gotPacket, int (*encodeProc)(AVCodecContext *, AVPacket *, const AVFrame *, int *)) noexcept
Expand Down Expand Up @@ -1028,7 +1039,7 @@ CodecContext2::decodeCommon(T &outFrame,

// Dial with PTS/DTS in packet/stream timebase

if (inPacket.timeBase() != Rational())
if (inPacket.raw() && inPacket.timeBase() != Rational())
outFrame.setTimeBase(inPacket.timeBase());
#if AVCPP_HAS_AVFORMAT
else
Expand All @@ -1051,7 +1062,7 @@ CodecContext2::decodeCommon(T &outFrame,
// Convert to decoder/frame time base. Seems not nessesary.
outFrame.setTimeBase(timeBase());

if (inPacket)
if (inPacket.raw() && inPacket)
outFrame.setStreamIndex(inPacket.streamIndex());
#if AVCPP_HAS_AVFORMAT
else
Expand Down
8 changes: 6 additions & 2 deletions src/avcpp/frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,12 +646,16 @@ bool FrameCommon::isComplete() const { return m_isComplete; }
bool FrameCommon::isValid() const {
return (!isNull() &&
((m_raw->data[0] && m_raw->linesize[0]) ||
((m_raw->format == AV_PIX_FMT_VAAPI) && ((intptr_t)m_raw->data[3] > 0)))
);
((m_raw->hw_frames_ctx) && ((intptr_t)m_raw->data[3] > 0))));
}

FrameCommon::operator bool() const { return isValid() && isComplete(); }

bool FrameCommon::isHwFrame() const
{
return !isNull() && !!m_raw->hw_frames_ctx;
}

uint8_t *FrameCommon::data(size_t plane) {
if (!m_raw || plane >= size_t(AV_NUM_DATA_POINTERS + m_raw->nb_extended_buf))
return nullptr;
Expand Down
2 changes: 2 additions & 0 deletions src/avcpp/frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class FrameCommon : public FFWrapperPtr<AVFrame>

operator bool() const;

bool isHwFrame() const;

uint8_t *data(size_t plane = 0);
const uint8_t *data(size_t plane = 0) const;

Expand Down
16 changes: 11 additions & 5 deletions src/avcpp/packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@ using namespace std;

namespace av {

#if AVCPP_API_AVCODEC_NEW_INIT_PACKET
Packet::Packet(std::nullptr_t)
: FFWrapperPtr<AVPacket>(nullptr),
m_completeFlag(false),
m_timeBase(0,0)
{
}
#endif

Packet::Packet()
: m_completeFlag(false),
m_timeBase(0, 0)
{
#if AVCPP_API_AVCODEC_NEW_INIT_PACKET
m_raw = av_packet_alloc();
#else
av_init_packet(raw());
#endif

raw()->stream_index = -1; // no stream

m_completeFlag = false;
m_timeBase = Rational(0, 0);

}

Packet::Packet(const Packet &packet)
Expand Down
4 changes: 4 additions & 0 deletions src/avcpp/packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ class Packet :
*/
struct wrap_data_static {};

#if AVCPP_API_AVCODEC_NEW_INIT_PACKET
explicit Packet(std::nullptr_t); // explicit nullptr packet. There is no extra internal checks, so be careful
#endif

Packet();
Packet(const Packet &packet, OptionalErrorCode ec);
Packet(const Packet &packet);
Expand Down
3 changes: 2 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ add_executable(test_executor
PixelSampleFormat.cpp
Common.cpp
Buffer.cpp
FormatCustomIO_test.cpp)
FormatCustomIO_test.cpp
CodecContext.cpp)
target_link_libraries(test_executor PUBLIC Catch2::Catch2WithMain avcpp::avcpp)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../catch2/contrib")
Expand Down
67 changes: 67 additions & 0 deletions tests/CodecContext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
//
// TODO: test incomplete. Sounds like RAWVIDEO decoder does not support multiple frames per-packet: only first one
// consumed
//
//

#include <catch2/catch_test_macros.hpp>

#include "avcpp/avconfig.h"

#include <vector>

#include "avcpp/packet.h"
#include "avcpp/codeccontext.h"

namespace {
constexpr std::size_t W = 320;
constexpr std::size_t H = 240;
const av::PixelFormat FMT{AV_PIX_FMT_GRAY8};
constexpr std::size_t FRAMES = 4;
constexpr std::size_t FRAME_SIZE = W * H;
constexpr std::size_t FRAMES_SIZE = FRAME_SIZE * FRAMES;
}

TEST_CASE("CodecContext")
{
SECTION("Decode packet with multiple frames") {

std::vector<uint8_t> block;
block.reserve(FRAMES_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
block.resize(FRAMES_SIZE); // Gray

av::Codec vcodec = av::findDecodingCodec(AVCodecID::AV_CODEC_ID_RAWVIDEO);
av::VideoDecoderContext vdec{vcodec};
vdec.setHeight(H);
vdec.setWidth(W);
vdec.setPixelFormat(FMT);
vdec.setTimeBase({1, 1});
vdec.open();

for (auto i = 0; i < FRAMES; ++i) {
std::fill_n(block.data() + i * FRAME_SIZE, FRAME_SIZE, (i + 1) * 50);
}

//
av::Packet pkt{block, av::Packet::wrap_data_static{}};
pkt.setTimeBase(vdec.timeBase());

static std::size_t ITERS = 4;
std::size_t framesCount = 0;
for (auto i = 0; i < ITERS; ++i) {
pkt.setPts({i, {1,1}});
auto frame = vdec.decode(pkt);
if (!frame) // TBD
break;
// in case, when multiple frames per packet
while (frame) {
std::cout << "Frame: " << int(frame.data()[0]) << ", cnt=" << (framesCount++) << std::endl;
frame = vdec.decode(av::Packet{nullptr});
}
}

// TBD: rawvideo does not support multiple frames per packet.
// CHECK(framesCount == ITERS * FRAMES);
}
}
6 changes: 6 additions & 0 deletions tests/Packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ TEST_CASE("Packet define", "[Packet][Construct]")
CHECK(pkt.refCount() == 1);
}

SECTION("Nullptr packet") {
av::Packet pkt{nullptr};
CHECK(pkt.raw() == nullptr);
// any other operation not permitted
}

SECTION("setPts/Dts") {
{
av::Packet pkt;
Expand Down
Loading