Skip to content

Commit

Permalink
MythVideoFrame: Refactor CopyFrame
Browse files Browse the repository at this point in the history
Refs #261
  • Loading branch information
mark-kendall committed Oct 13, 2020
1 parent 210687f commit 440b991
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 61 deletions.
80 changes: 40 additions & 40 deletions mythtv/libs/libmythtv/mythframe.cpp
Expand Up @@ -267,13 +267,13 @@ void MythVideoFrame::ClearBufferToBlank()
}
}

bool MythVideoFrame::CopyFrame(MythVideoFrame *To, MythVideoFrame *From)
bool MythVideoFrame::CopyFrame(MythVideoFrame *From)
{
// Sanity checks
if (!(To && From) || (To == From))
if (!From || (this == From))
return false;

if (To->codec != From->codec)
if (codec != From->codec)
{
LOG(VB_GENERAL, LOG_ERR, "Cannot copy frames of differing types");
return false;
Expand All @@ -285,22 +285,22 @@ bool MythVideoFrame::CopyFrame(MythVideoFrame *To, MythVideoFrame *From)
return false;
}

if (!((To->width > 0) && (To->height > 0) &&
(To->width == From->width) && (To->height == From->height)))
if (!((width > 0) && (height > 0) &&
(width == From->width) && (height == From->height)))
{
LOG(VB_GENERAL, LOG_ERR, "Invalid frame sizes");
return false;
}

if (!(To->buf && From->buf && (To->buf != From->buf)))
if (!(buf && From->buf && (buf != From->buf)))
{
LOG(VB_GENERAL, LOG_ERR, "Invalid frames for copying");
return false;
}

// N.B. Minimum based on zero width alignment but will apply height alignment
size_t minsize = GetBufferSize(To->codec, To->width, To->height, 0);
if ((To->size < minsize) || (From->size < minsize))
size_t minsize = GetBufferSize(codec, width, height, 0);
if ((size < minsize) || (From->size < minsize))
{
LOG(VB_GENERAL, LOG_ERR, "Invalid buffer size");
return false;
Expand All @@ -313,7 +313,7 @@ bool MythVideoFrame::CopyFrame(MythVideoFrame *To, MythVideoFrame *From)
uint count = GetNumPlanes(From->codec);
for (uint plane = 0; plane < count; plane++)
{
CopyPlane(To->buf + To->offsets[plane], To->pitches[plane],
CopyPlane(buf + offsets[plane], pitches[plane],
From->buf + From->offsets[plane], From->pitches[plane],
GetPitchForPlane(From->codec, From->width, plane),
GetHeightForPlane(From->codec, From->height, plane));
Expand All @@ -323,37 +323,37 @@ bool MythVideoFrame::CopyFrame(MythVideoFrame *To, MythVideoFrame *From)
// Not copied: codec, width, height - should already be the same
// Not copied: buf, size, pitches, offsets - should/could be different
// Not copied: priv - hardware frames only
To->aspect = From->aspect;
To->frame_rate = From->frame_rate;
To->bpp = From->bpp;
To->frameNumber = From->frameNumber;
To->frameCounter = From->frameCounter;
To->timecode = From->timecode;
To->disp_timecode = From->disp_timecode;
To->interlaced_frame = From->interlaced_frame;
To->top_field_first = From->top_field_first;
To->interlaced_reversed = From->interlaced_reversed;
To->new_gop = From->new_gop;
To->repeat_pict = From->repeat_pict;
To->forcekey = From->forcekey;
To->dummy = From->dummy;
To->pause_frame = From->pause_frame;
To->pix_fmt = From->pix_fmt;
To->sw_pix_fmt = From->sw_pix_fmt;
To->directrendering = From->directrendering;
To->colorspace = From->colorspace;
To->colorrange = From->colorrange;
To->colorprimaries = From->colorprimaries;
To->colortransfer = From->colortransfer;
To->chromalocation = From->chromalocation;
To->colorshifted = From->colorshifted;
To->already_deinterlaced = From->already_deinterlaced;
To->rotation = From->rotation;
To->deinterlace_single = From->deinterlace_single;
To->deinterlace_double = From->deinterlace_double;
To->deinterlace_allowed = From->deinterlace_allowed;
To->deinterlace_inuse = From->deinterlace_inuse;
To->deinterlace_inuse2x = From->deinterlace_inuse2x;
aspect = From->aspect;
frame_rate = From->frame_rate;
bpp = From->bpp;
frameNumber = From->frameNumber;
frameCounter = From->frameCounter;
timecode = From->timecode;
disp_timecode = From->disp_timecode;
interlaced_frame = From->interlaced_frame;
top_field_first = From->top_field_first;
interlaced_reversed = From->interlaced_reversed;
new_gop = From->new_gop;
repeat_pict = From->repeat_pict;
forcekey = From->forcekey;
dummy = From->dummy;
pause_frame = From->pause_frame;
pix_fmt = From->pix_fmt;
sw_pix_fmt = From->sw_pix_fmt;
directrendering = From->directrendering;
colorspace = From->colorspace;
colorrange = From->colorrange;
colorprimaries = From->colorprimaries;
colortransfer = From->colortransfer;
chromalocation = From->chromalocation;
colorshifted = From->colorshifted;
already_deinterlaced = From->already_deinterlaced;
rotation = From->rotation;
deinterlace_single = From->deinterlace_single;
deinterlace_double = From->deinterlace_double;
deinterlace_allowed = From->deinterlace_allowed;
deinterlace_inuse = From->deinterlace_inuse;
deinterlace_inuse2x = From->deinterlace_inuse2x;

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythtv/mythframe.h
Expand Up @@ -100,6 +100,7 @@ class MTV_PUBLIC MythVideoFrame

void ClearMetadata();
void ClearBufferToBlank();
bool CopyFrame(MythVideoFrame* From);

VideoFrameType codec { FMT_NONE };
uint8_t* buf { nullptr };
Expand Down Expand Up @@ -144,7 +145,6 @@ class MTV_PUBLIC MythVideoFrame
MythDeintType deinterlace_inuse { DEINT_NONE };
bool deinterlace_inuse2x { false };

static bool CopyFrame(MythVideoFrame* To, MythVideoFrame* From);
static void CopyPlane(uint8_t* To, int ToPitch,
const uint8_t* From, int FromPitch,
int PlaneWidth, int PlaneHeight);
Expand Down
4 changes: 2 additions & 2 deletions mythtv/libs/libmythtv/mythvideooutnull.cpp
Expand Up @@ -191,13 +191,13 @@ void MythVideoOutputNull::UpdatePauseFrame(int64_t& DisplayTimecode, FrameScanTy
used = m_videoBuffers.Head(kVideoBuffer_used);

if (used)
MythVideoFrame::CopyFrame(&m_avPauseFrame, used);
m_avPauseFrame.CopyFrame(used);
m_videoBuffers.EndLock();

if (!used)
{
m_videoBuffers.GetScratchFrame()->frameNumber = m_framesPlayed - 1;
MythVideoFrame::CopyFrame(m_videoBuffers.GetScratchFrame(), &m_avPauseFrame);
m_videoBuffers.GetScratchFrame()->CopyFrame(&m_avPauseFrame);
}

DisplayTimecode = m_avPauseFrame.disp_timecode;
Expand Down
34 changes: 16 additions & 18 deletions mythtv/libs/libmythtv/test/test_copyframes/test_copyframes.cpp
Expand Up @@ -9,10 +9,8 @@ void TestCopyFrames::initTestCase(void)
void TestCopyFrames::TestInvalidFrames()
{
MythVideoFrame dummy;
QVERIFY(!MythVideoFrame::CopyFrame(nullptr, nullptr));
QVERIFY(!MythVideoFrame::CopyFrame(nullptr, &dummy));
QVERIFY(!MythVideoFrame::CopyFrame(&dummy, nullptr));
QVERIFY(!MythVideoFrame::CopyFrame(&dummy, &dummy));
QVERIFY(!dummy.CopyFrame(nullptr));
QVERIFY(!dummy.CopyFrame(&dummy));
}

void TestCopyFrames::TestInvalidFormats()
Expand All @@ -21,13 +19,13 @@ void TestCopyFrames::TestInvalidFormats()
MythVideoFrame dummy2;
dummy1.codec = FMT_YV12;
dummy2.codec = FMT_NV12;
QVERIFY(!MythVideoFrame::CopyFrame(&dummy1, &dummy2));
QVERIFY(!dummy1.CopyFrame(&dummy2));
dummy1.codec = FMT_NONE;
dummy2.codec = FMT_NONE;
QVERIFY(!MythVideoFrame::CopyFrame(&dummy1, &dummy2));
QVERIFY(!dummy1.CopyFrame(&dummy2));
dummy1.codec = FMT_VAAPI;
dummy2.codec = FMT_VAAPI;
QVERIFY(!MythVideoFrame::CopyFrame(&dummy1, &dummy2));
QVERIFY(!dummy1.CopyFrame(&dummy2));
}

void TestCopyFrames::TestInvalidSizes()
Expand All @@ -41,27 +39,27 @@ void TestCopyFrames::TestInvalidSizes()
dummy1.height = 576;
dummy2.height = 500;
// Different heights
QVERIFY(!MythVideoFrame::CopyFrame(&dummy1, &dummy2));
QVERIFY(!dummy1.CopyFrame(&dummy2));
// Different widths
dummy2.height = 576;
dummy2.width = 1024;
QVERIFY(!MythVideoFrame::CopyFrame(&dummy1, &dummy2));
QVERIFY(!dummy1.CopyFrame(&dummy2));
// Invalid height
dummy2.width = 720;
dummy1.height = 0;
QVERIFY(!MythVideoFrame::CopyFrame(&dummy1, &dummy2));
QVERIFY(!dummy1.CopyFrame(&dummy2));
// Invalid width
dummy1.height = 576;
dummy2.width = 0;
QVERIFY(!MythVideoFrame::CopyFrame(&dummy1, &dummy2));
QVERIFY(!dummy1.CopyFrame(&dummy2));
}

void TestCopyFrames::TestInvalidBuffers()
{
// Both null buffers
MythVideoFrame dummy1(FMT_YV12, nullptr, 0, 720, 576);
MythVideoFrame dummy2(FMT_YV12, nullptr, 0, 720, 576);
QVERIFY(!MythVideoFrame::CopyFrame(&dummy1, &dummy2));
QVERIFY(!dummy1.CopyFrame(&dummy2));

size_t size1 = MythVideoFrame::GetBufferSize(FMT_YV12, 720, 576);
unsigned char dummy = '0';
Expand All @@ -70,18 +68,18 @@ void TestCopyFrames::TestInvalidBuffers()

// One null buffer
dummy1.buf = buf1;
QVERIFY(!MythVideoFrame::CopyFrame(&dummy1, &dummy2));
QVERIFY(!dummy1.CopyFrame(&dummy2));
// Two buffers, both zero sized
dummy2.buf = buf2;
QVERIFY(!MythVideoFrame::CopyFrame(&dummy1, &dummy2));
QVERIFY(!dummy1.CopyFrame(&dummy2));
// Same buffer
dummy2.buf = buf1;
QVERIFY(!MythVideoFrame::CopyFrame(&dummy1, &dummy2));
QVERIFY(!dummy1.CopyFrame(&dummy2));
// Invalid size
dummy2.buf = buf2;
dummy1.size = 16;
dummy2.size = size1;
QVERIFY(!MythVideoFrame::CopyFrame(&dummy1, &dummy2));
QVERIFY(!dummy1.CopyFrame(&dummy2));
dummy1.buf = nullptr;
dummy2.buf = nullptr;
}
Expand Down Expand Up @@ -233,7 +231,7 @@ void TestCopyFrames::TestCopy()
for (auto alignment : alignments)
{
auto * to = getdefaultframe(type, width, height, alignment);
QVERIFY(MythVideoFrame::CopyFrame(to, frame));
QVERIFY(to->CopyFrame(frame));
QCOMPARE(sum, GetSum(to));
delete to;
}
Expand All @@ -243,7 +241,7 @@ void TestCopyFrames::TestCopy()
{
auto * from = getdefaultframe(type, width, height, alignment);
auto newsum = FillRandom(from);
QVERIFY(MythVideoFrame::CopyFrame(frame, from));
QVERIFY(frame->CopyFrame(from));
QCOMPARE(newsum, GetSum(frame));
delete from;
}
Expand Down

0 comments on commit 440b991

Please sign in to comment.