Skip to content

Commit

Permalink
MythFrame: Move GetBufferSize into MythVideoFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-kendall committed Oct 13, 2020
1 parent fb526e8 commit b479423
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 30 deletions.
2 changes: 1 addition & 1 deletion mythtv/libs/libmythtv/mythavutil.cpp
Expand Up @@ -208,7 +208,7 @@ int AVPictureFill(AVFrame *pic, const VideoFrame *frame, AVPixelFormat fmt)
pic->linesize[0] = frame->pitches[0];
pic->linesize[1] = frame->pitches[1];
pic->linesize[2] = frame->pitches[2];
return static_cast<int>(GetBufferSize(frame->codec, frame->width, frame->height));
return static_cast<int>(MythVideoFrame::GetBufferSize(frame->codec, frame->width, frame->height));
}

class MythAVCopyPrivate
Expand Down
18 changes: 18 additions & 0 deletions mythtv/libs/libmythtv/mythframe.cpp
Expand Up @@ -240,6 +240,24 @@ QString MythVideoFrame::FormatDescription(VideoFrameType Type)
return "?";
}

size_t MythVideoFrame::GetBufferSize(VideoFrameType Type, int Width, int Height, int Aligned)
{
// bits per pixel div common factor
int bpp = bitsperpixel(Type) / 4;
// bits per byte div common factor
int bpb = 8 / 4;

// Align height and width. We always align height to 16 rows and the *default*
// width alignment is 64bytes, which allows SIMD operations on subsampled
// planes (i.e. 32byte alignment)
int adj_w = Aligned ? ((Width + Aligned - 1) & ~(Aligned - 1)) : Width;
int adj_h = (Height + MYTH_HEIGHT_ALIGNMENT - 1) & ~(MYTH_HEIGHT_ALIGNMENT - 1);

// Calculate rounding as necessary.
int remainder = (adj_w * adj_h * bpp) % bpb;
return static_cast<uint>((adj_w * adj_h * bpp) / bpb + (remainder ? 1 : 0));
}

uint8_t *MythVideoFrame::GetAlignedBuffer(size_t Size)
{
return static_cast<uint8_t*>(av_malloc(Size + 64));
Expand Down
22 changes: 1 addition & 21 deletions mythtv/libs/libmythtv/mythframe.h
Expand Up @@ -617,27 +617,6 @@ static inline int bitsperpixel(VideoFrameType Type)
return 8;
}

static inline size_t GetBufferSize(VideoFrameType Type, int Width, int Height,
int Aligned = MYTH_WIDTH_ALIGNMENT)
{
int type_bpp = bitsperpixel(Type);
int bpp = type_bpp / 4; /* bits per pixel div common factor */
int bpb = 8 / 4; /* bits per byte div common factor */

// make sure all our pitches are a multiple of 16 bytes
// as U and V channels are half the size of Y channel
// This allow for SSE/HW accelerated code on all planes
// If the buffer sizes are not 32 bytes aligned, adjust.
// old versions of MythTV allowed people to set invalid
// dimensions for MPEG-4 capture, no need to segfault..
int adj_w = Aligned ? ((Width + Aligned - 1) & ~(Aligned - 1)) : Width;
int adj_h = (Height + MYTH_HEIGHT_ALIGNMENT - 1) & ~(MYTH_HEIGHT_ALIGNMENT - 1);

// Calculate rounding as necessary.
int remainder = (adj_w * adj_h * bpp) % bpb;
return static_cast<uint>((adj_w * adj_h * bpp) / bpb + (remainder ? 1 : 0));
}

class MTV_PUBLIC MythVideoFrame
{
public:
Expand All @@ -649,6 +628,7 @@ class MTV_PUBLIC MythVideoFrame
static uint8_t* GetAlignedBuffer(size_t Size);
static uint8_t* GetAlignedBufferZero(size_t Size);
static uint8_t* CreateBuffer(VideoFrameType Type, int Width, int Height);
static size_t GetBufferSize(VideoFrameType Type, int Width, int Height, int Aligned = MYTH_WIDTH_ALIGNMENT);
};

#endif
4 changes: 2 additions & 2 deletions mythtv/libs/libmythtv/recorders/NuppelVideoRecorder.cpp
Expand Up @@ -750,8 +750,8 @@ void NuppelVideoRecorder::InitBuffers(void)
if (!m_videoBufferSize)
{
m_videoBufferSize = static_cast<long>(
GetBufferSize(m_pictureFormat == AV_PIX_FMT_YUV422P ? FMT_YUV422P : FMT_YV12,
m_wOut, m_hOut));
MythVideoFrame::GetBufferSize(m_pictureFormat == AV_PIX_FMT_YUV422P ? FMT_YUV422P : FMT_YV12,
m_wOut, m_hOut));
}

if (m_width >= 480 || m_height > 288)
Expand Down
Expand Up @@ -65,7 +65,7 @@ void TestCopyFrames::TestInvalidBuffers()
init(&dummy2, FMT_YV12, nullptr, 720, 576, 0);
QVERIFY(!MythVideoFrame::CopyFrame(&dummy1, &dummy2));

size_t size1 = GetBufferSize(FMT_YV12, 720, 576);
size_t size1 = MythVideoFrame::GetBufferSize(FMT_YV12, 720, 576);
unsigned char dummy = '0';
unsigned char * buf1 = &dummy;
unsigned char * buf2 = &dummy;
Expand Down Expand Up @@ -212,7 +212,7 @@ void TestCopyFrames::TestCopy()

auto getdefaultframe = [](VideoFrameType T, int W, int H, int A)
{
size_t size = GetBufferSize(T, W, H, A);
size_t size = MythVideoFrame::GetBufferSize(T, W, H, A);
VideoFrame* frame = new VideoFrame;
init(frame, T, MythVideoFrame::GetAlignedBufferZero(size), W, H, static_cast<int>(size), 1.0, 1.0, A);
return frame;
Expand Down
4 changes: 2 additions & 2 deletions mythtv/libs/libmythtv/videobuffers.cpp
Expand Up @@ -1025,7 +1025,7 @@ bool VideoBuffers::CreateBuffers(VideoFrameType Type, int Width, int Height)
}

// Software buffers
size_t bufsize = GetBufferSize(Type, Width, Height);
size_t bufsize = MythVideoFrame::GetBufferSize(Type, Width, Height);
for (uint i = 0; i < Size(); i++)
{
unsigned char *data = MythVideoFrame::GetAlignedBuffer(bufsize);
Expand Down Expand Up @@ -1070,7 +1070,7 @@ bool VideoBuffers::ReinitBuffer(VideoFrame *Frame, VideoFrameType Type, MythCode

// Find the frame
VideoFrameType old = Frame->codec;
size_t size = GetBufferSize(Type, Width, Height);
size_t size = MythVideoFrame::GetBufferSize(Type, Width, Height);
unsigned char *buf = Frame->buf;
bool newbuf = false;
if ((Frame->size != static_cast<int>(size)) || !buf)
Expand Down
4 changes: 2 additions & 2 deletions mythtv/programs/mythtranscode/transcode.cpp
Expand Up @@ -869,11 +869,11 @@ int Transcode::TranscodeFile(const QString &inputname,
// 1080i/p video is actually 1088 because of the 16x16 blocks so
// we have to fudge the output size here. nuvexport knows how to handle
// this and as of right now it is the only app that uses the fifo ability.
newSize = GetBufferSize(FMT_YV12, video_width, video_height == 1080 ? 1088 : video_height, 0 /* aligned */);
newSize = MythVideoFrame::GetBufferSize(FMT_YV12, video_width, video_height == 1080 ? 1088 : video_height, 0 /* aligned */);
}
else
{
newSize = GetBufferSize(FMT_YV12, newWidth, newHeight);
newSize = MythVideoFrame::GetBufferSize(FMT_YV12, newWidth, newHeight);
}
unsigned char *newFrame = MythVideoFrame::GetAlignedBuffer(newSize);
if (!newFrame)
Expand Down

0 comments on commit b479423

Please sign in to comment.