Skip to content

Commit

Permalink
Convert to std::array and std::string. (mythframe and opengl)
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxdude42 committed Aug 28, 2020
1 parent 56b7bfe commit 6300d08
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 115 deletions.
4 changes: 2 additions & 2 deletions mythtv/libs/libmythtv/DetectLetterbox.cpp
Expand Up @@ -35,8 +35,8 @@ void DetectLetterbox::Detect(VideoFrame *Frame)
return;

unsigned char *buf = Frame->buf;
int *pitches = Frame->pitches;
int *offsets = Frame->offsets;
const FramePitches pitches = Frame->pitches;
const FrameOffsets offsets = Frame->offsets;
const int height = Frame->height;

// If the black bars is larger than this limit we switch to Half or Full Mode
Expand Down
8 changes: 4 additions & 4 deletions mythtv/libs/libmythtv/mythavutil.cpp
Expand Up @@ -268,18 +268,18 @@ void MythAVCopy::FillFrame(VideoFrame *frame, const AVFrame *pic, int pitch,
{
int chroma_pitch = pitch >> 1;
int chroma_height = height >> 1;
int offsets[3] =
FrameOffsets offsets
{ 0,
pitch * height,
pitch * height + chroma_pitch * chroma_height };
int pitches[3] = { pitch, chroma_pitch, chroma_pitch };
FramePitches pitches { pitch, chroma_pitch, chroma_pitch };

init(frame, FMT_YV12, pic->data[0], width, height, size, pitches, offsets);
}
else if (pix_fmt == AV_PIX_FMT_NV12)
{
int offsets[3] = { 0, pitch * height, 0 };
int pitches[3] = { pitch, pitch, 0 };
FrameOffsets offsets { 0, pitch * height, 0 };
FramePitches pitches { pitch, pitch, 0 };

init(frame, FMT_NV12, pic->data[0], width, height, size, pitches, offsets);
}
Expand Down
81 changes: 40 additions & 41 deletions mythtv/libs/libmythtv/mythframe.h
@@ -1,24 +1,18 @@
#ifndef MYTHFRAME_H
#define MYTHFRAME_H

#ifdef __cplusplus
#include <array>
#include <cstdint>
#include <cstring>
#else
#include <stdint.h>
#include <string.h>
#endif
#include <vector>

#include "mythtvexp.h" // for MTV_PUBLIC
#include "mythaverror.h"

extern "C" {
#include "libavcodec/avcodec.h"
}

#ifdef __cplusplus
extern "C" {
#endif

#define MYTH_WIDTH_ALIGNMENT 64
#define MYTH_HEIGHT_ALIGNMENT 16
enum VideoFrameType
Expand Down Expand Up @@ -134,6 +128,9 @@ inline MythDeintType operator| (MythDeintType a, MythDeintType b) { return stati
inline MythDeintType operator& (MythDeintType a, MythDeintType b) { return static_cast<MythDeintType>(static_cast<int>(a) & static_cast<int>(b)); }
inline MythDeintType operator~ (MythDeintType a) { return static_cast<MythDeintType>(~(static_cast<int>(a))); }

using FramePitches = std::array<int,3>;
using FrameOffsets = std::array<int,3>;

struct VideoFrame
{
VideoFrameType codec;
Expand All @@ -149,7 +146,7 @@ struct VideoFrame
long long frameCounter; ///< raw frame counter/ticker for discontinuity checks
long long timecode;
int64_t disp_timecode;
unsigned char *priv[4]; ///< random empty storage
std::array<uint8_t*,4> priv; ///< random empty storage
int interlaced_frame; ///< 1 if interlaced. 0 if not interlaced. -1 if unknown.
bool top_field_first; ///< true if top field is first.
bool interlaced_reversed; /// true for user override of scan
Expand All @@ -158,8 +155,8 @@ struct VideoFrame
bool forcekey; ///< hardware encoded .nuv
bool dummy;
bool pause_frame;
int pitches[3]; ///< Y, U, & V pitches
int offsets[3]; ///< Y, U, & V offsets
FramePitches pitches; ///< Y, U, & V pitches
FrameOffsets offsets; ///< Y, U, & V offsets
int pix_fmt;
int sw_pix_fmt;
bool directrendering; ///< true if managed by FFmpeg
Expand All @@ -180,14 +177,8 @@ struct VideoFrame

using VideoFrameVec = std::vector<VideoFrameType>;

#ifdef __cplusplus
}
#endif

int MTV_PUBLIC ColorDepth(int Format);

#ifdef __cplusplus

MythDeintType MTV_PUBLIC GetSingleRateOption(const VideoFrame* Frame, MythDeintType Type, MythDeintType Override = DEINT_NONE);
MythDeintType MTV_PUBLIC GetDoubleRateOption(const VideoFrame* Frame, MythDeintType Type, MythDeintType Override = DEINT_NONE);
MythDeintType MTV_PUBLIC GetDeinterlacer(MythDeintType Option);
Expand Down Expand Up @@ -220,21 +211,21 @@ class MTV_PUBLIC MythUSWCCopy
void MTV_PUBLIC framecopy(VideoFrame *dst, const VideoFrame *src,
bool useSSE = true);

static inline void init(VideoFrame *vf, VideoFrameType _codec,
unsigned char *_buf, int _width, int _height, int _size,
const int *p = nullptr,
const int *o = nullptr,
float _aspect = -1.0F, double _rate = -1.0F,
int _aligned = MYTH_WIDTH_ALIGNMENT);
static inline void clear(VideoFrame *vf);
static inline int bitsperpixel(VideoFrameType type);
static inline int pitch_for_plane(VideoFrameType Type, int Width, uint Plane);
static inline int height_for_plane(VideoFrameType Type, int Height, uint Plane);

using mavtriplet = std::array<int,3>;
using mavtripletptr = mavtriplet::const_pointer;

static inline void init(VideoFrame *vf, VideoFrameType _codec,
unsigned char *_buf, int _width, int _height,
int _size, const int *p, const int *o,
float _aspect, double _rate, int _aligned)
int _size,
const FramePitches p,
const FrameOffsets o,
bool arrays_valid = true,
float _aspect = -1.0F, double _rate = -1.0F,
int _aligned = MYTH_WIDTH_ALIGNMENT)
{
vf->bpp = bitsperpixel(_codec);
vf->codec = _codec;
Expand Down Expand Up @@ -271,7 +262,7 @@ static inline void init(VideoFrame *vf, VideoFrameType _codec,
vf->deinterlace_allowed = DEINT_NONE;
vf->deinterlace_inuse = DEINT_NONE;
vf->deinterlace_inuse2x = false;
memset(vf->priv, 0, 4 * sizeof(unsigned char *));
vf->priv.fill(nullptr);

int width_aligned = 0;
int height_aligned = (_height + MYTH_HEIGHT_ALIGNMENT - 1) & ~(MYTH_HEIGHT_ALIGNMENT -1);
Expand All @@ -280,19 +271,19 @@ static inline void init(VideoFrame *vf, VideoFrameType _codec,
else
width_aligned = (_width + _aligned - 1) & ~(_aligned - 1);

if (p)
if (arrays_valid)
{
memcpy(vf->pitches, p, 3 * sizeof(int));
vf->pitches = p;
}
else
{
for (uint i = 0; i < 3; ++i)
vf->pitches[i] = pitch_for_plane(_codec, width_aligned, i);
}

if (o)
if (arrays_valid)
{
memcpy(vf->offsets, o, 3 * sizeof(int));
vf->offsets = o;
}
else
{
Expand Down Expand Up @@ -344,6 +335,15 @@ static inline void init(VideoFrame *vf, VideoFrameType _codec,
}
}

static inline void init(VideoFrame *vf, VideoFrameType _codec,
unsigned char *_buf, int _width, int _height, int _size,
float _aspect = -1.0F, double _rate = -1.0F,
int _aligned = MYTH_WIDTH_ALIGNMENT)
{
init(vf, _codec, _buf, _width, _height, _size, {}, {}, false,
_aspect, _rate, _aligned);
}

static inline int pitch_for_plane(VideoFrameType Type, int Width, uint Plane)
{
switch (Type)
Expand Down Expand Up @@ -708,11 +708,11 @@ static inline void copybuffer(VideoFrame *dst, uint8_t *buffer,
VideoFrame framein {};
int chroma_pitch = pitch >> 1;
int chroma_height = dst->height >> 1;
int offsets[3] =
FrameOffsets offsets
{ 0,
pitch * dst->height,
pitch * dst->height + chroma_pitch * chroma_height };
int pitches[3] = { pitch, chroma_pitch, chroma_pitch };
FramePitches pitches { pitch, chroma_pitch, chroma_pitch };

init(&framein, type, buffer, dst->width, dst->height, dst->size,
pitches, offsets);
Expand All @@ -721,8 +721,8 @@ static inline void copybuffer(VideoFrame *dst, uint8_t *buffer,
else if (type == FMT_NV12)
{
VideoFrame framein {};
int offsets[3] = { 0, pitch * dst->height, 0 };
int pitches[3] = { pitch, pitch, 0 };
FrameOffsets offsets { 0, pitch * dst->height, 0 };
FramePitches pitches { pitch, pitch, 0 };

init(&framein, type, buffer, dst->width, dst->height, dst->size,
pitches, offsets);
Expand All @@ -741,11 +741,11 @@ static inline void copybuffer(uint8_t *dstbuffer, const VideoFrame *src,
VideoFrame frameout {};
int chroma_pitch = pitch >> 1;
int chroma_height = src->height >> 1;
int offsets[3] =
FrameOffsets offsets
{ 0,
pitch * src->height,
pitch * src->height + chroma_pitch * chroma_height };
int pitches[3] = { pitch, chroma_pitch, chroma_pitch };
FramePitches pitches { pitch, chroma_pitch, chroma_pitch };

init(&frameout, type, dstbuffer, src->width, src->height, src->size,
pitches, offsets);
Expand All @@ -754,14 +754,13 @@ static inline void copybuffer(uint8_t *dstbuffer, const VideoFrame *src,
else if (type == FMT_NV12)
{
VideoFrame frameout {};
int offsets[3] = { 0, pitch * src->height, 0 };
int pitches[3] = { pitch, pitch, 0 };
FrameOffsets offsets { 0, pitch * src->height, 0 };
FramePitches pitches { pitch, pitch, 0 };

init(&frameout, type, dstbuffer, src->width, src->height, src->size,
pitches, offsets);
copy(&frameout, src);
}
}
#endif /* __cplusplus */

#endif /* MYTHFRAME_H */
1 change: 1 addition & 0 deletions mythtv/libs/libmythtv/opengl/mythopengltonemap.cpp
Expand Up @@ -100,6 +100,7 @@ MythVideoTexture* MythOpenGLTonemap::Map(vector<MythVideoTexture *> &Inputs, QSi
return nullptr;
}
m_render->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_storageBuffer);
// Data structure passed to kernel. NOLINTNEXTLINE(modernize-avoid-c-arrays)
struct dummy { float a[2] {0.0F}; uint32_t b {0}; uint32_t c {0}; uint32_t d {0}; } buffer;
m_render->glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(dummy), &buffer, GL_STREAM_COPY);
m_render->glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
Expand Down
38 changes: 17 additions & 21 deletions mythtv/libs/libmythtv/opengl/mythopenglvideo.cpp
Expand Up @@ -563,8 +563,8 @@ void MythOpenGLVideo::ResetFrameFormat(void)
for (auto & shader : m_shaders)
if (shader)
m_render->DeleteShaderProgram(shader);
memset(m_shaders, 0, sizeof(m_shaders));
memset(m_shaderCost, 1, sizeof(m_shaderCost));
m_shaders.fill(nullptr);
m_shaderCost.fill(1);
MythVideoTexture::DeleteTextures(m_render, m_inputTextures);
MythVideoTexture::DeleteTextures(m_render, m_prevTextures);
MythVideoTexture::DeleteTextures(m_render, m_nextTextures);
Expand Down Expand Up @@ -659,21 +659,19 @@ void MythOpenGLVideo::ProcessFrame(VideoFrame *Frame, FrameScanType Scan)
if (m_deinterlacer == DEINT_BASIC)
{
// first field. Fake the pitches
int pitches[3];
memcpy(pitches, Frame->pitches, sizeof(int) * 3);
FramePitches pitches = Frame->pitches;
Frame->pitches[0] = Frame->pitches[0] << 1;
Frame->pitches[1] = Frame->pitches[1] << 1;
Frame->pitches[2] = Frame->pitches[2] << 1;
MythVideoTexture::UpdateTextures(m_render, Frame, m_inputTextures);
// second field. Fake the offsets as well.
int offsets[3];
memcpy(offsets, Frame->offsets, sizeof(int) * 3);
FrameOffsets offsets = Frame->offsets;
Frame->offsets[0] = Frame->offsets[0] + pitches[0];
Frame->offsets[1] = Frame->offsets[1] + pitches[1];
Frame->offsets[2] = Frame->offsets[2] + pitches[2];
MythVideoTexture::UpdateTextures(m_render, Frame, m_nextTextures);
memcpy(Frame->pitches, pitches, sizeof(int) * 3);
memcpy(Frame->offsets, offsets, sizeof(int) * 3);
Frame->pitches = pitches;
Frame->offsets = offsets;
}
else
{
Expand Down Expand Up @@ -941,12 +939,11 @@ void MythOpenGLVideo::PrepareFrame(VideoFrame *Frame, bool TopFieldFirst, FrameS
m_render->SetViewPort(vrect);

// bind correct textures
MythGLTexture* textures[MAX_VIDEO_TEXTURES];
uint numtextures = 0;
BindTextures(deinterlacing, inputtextures, &textures[0], numtextures);
vector<MythGLTexture*> textures {};
BindTextures(deinterlacing, inputtextures, textures);

// render
m_render->DrawBitmap(textures, numtextures, m_frameBuffer,
m_render->DrawBitmap(textures, m_frameBuffer,
trect2, vrect, m_shaders[program], 0);
nexttexture = m_frameBufferTexture;
}
Expand Down Expand Up @@ -982,9 +979,8 @@ void MythOpenGLVideo::PrepareFrame(VideoFrame *Frame, bool TopFieldFirst, FrameS
}

// bind correct textures
MythGLTexture* textures[MAX_VIDEO_TEXTURES];
uint numtextures = 0;
BindTextures(deinterlacing, inputtextures, &textures[0], numtextures);
vector<MythGLTexture*> textures;
BindTextures(deinterlacing, inputtextures, textures);

// rotation
if (Frame)
Expand All @@ -1000,7 +996,7 @@ void MythOpenGLVideo::PrepareFrame(VideoFrame *Frame, bool TopFieldFirst, FrameS
}

// draw
m_render->DrawBitmap(textures, numtextures, nullptr, trect,
m_render->DrawBitmap(textures, nullptr, trect,
m_displayVideoRect, m_shaders[program], m_lastRotation);

// disable scissoring
Expand All @@ -1023,7 +1019,7 @@ void MythOpenGLVideo::ResetTextures(void)
}

void MythOpenGLVideo::BindTextures(bool Deinterlacing, vector<MythVideoTexture*> &Current,
MythGLTexture **Textures, uint &TextureCount)
vector<MythGLTexture *>&Textures)
{
bool usecurrent = true;
if (Deinterlacing)
Expand All @@ -1042,17 +1038,17 @@ void MythOpenGLVideo::BindTextures(bool Deinterlacing, vector<MythVideoTexture*>
vector<MythVideoTexture*> &prev = m_prevTextures[0]->m_valid ? m_prevTextures : current;

for (uint i = 0; i < count; ++i)
Textures[TextureCount++] = reinterpret_cast<MythGLTexture*>(prev[i]);
Textures.push_back(reinterpret_cast<MythGLTexture*>(prev[i]));
for (uint i = 0; i < count; ++i)
Textures[TextureCount++] = reinterpret_cast<MythGLTexture*>(current[i]);
Textures.push_back(reinterpret_cast<MythGLTexture*>(current[i]));
for (uint i = 0; i < count; ++i)
Textures[TextureCount++] = reinterpret_cast<MythGLTexture*>(m_nextTextures[i]);
Textures.push_back(reinterpret_cast<MythGLTexture*>(m_nextTextures[i]));
}
}

if (usecurrent)
for (auto & texture : Current)
Textures[TextureCount++] = reinterpret_cast<MythGLTexture*>(texture);
Textures.push_back(reinterpret_cast<MythGLTexture*>(texture));
}

QString MythOpenGLVideo::TypeToProfile(VideoFrameType Type)
Expand Down
6 changes: 3 additions & 3 deletions mythtv/libs/libmythtv/opengl/mythopenglvideo.h
Expand Up @@ -81,7 +81,7 @@ class MythOpenGLVideo : public QObject
QSize Size, GLenum TextureTarget);
bool CreateVideoShader(VideoShaderType Type, MythDeintType Deint = DEINT_NONE);
void BindTextures(bool Deinterlacing, vector<MythVideoTexture*> &Current,
MythGLTexture** Textures, uint &TextureCount);
vector<MythGLTexture *>&Textures);
bool AddDeinterlacer(const VideoFrame *Frame, FrameScanType Scan,
MythDeintType Filter = DEINT_SHADER, bool CreateReferences = true);
QOpenGLFramebufferObject* CreateVideoFrameBuffer(VideoFrameType OutputType, QSize Size);
Expand All @@ -103,8 +103,8 @@ class MythOpenGLVideo : public QObject
MythDeintType m_fallbackDeinterlacer { MythDeintType::DEINT_NONE }; ///< Only used if there are insufficient texture units (for kernel)
VideoColourSpace *m_videoColourSpace;
bool m_viewportControl; ///< Video has control over view port
QOpenGLShaderProgram* m_shaders[ShaderCount] { nullptr };
int m_shaderCost[ShaderCount] { 1 };
std::array<QOpenGLShaderProgram*,ShaderCount> m_shaders { nullptr };
std::array<int,ShaderCount> m_shaderCost { 1 };
vector<MythVideoTexture*> m_inputTextures; ///< Current textures with raw video data
vector<MythVideoTexture*> m_prevTextures; ///< Previous textures with raw video data
vector<MythVideoTexture*> m_nextTextures; ///< Next textures with raw video data
Expand Down

0 comments on commit 6300d08

Please sign in to comment.