24 changes: 16 additions & 8 deletions mythtv/libs/libmythtv/DVD/mythdvdbuffer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef MYTH_DVD_BUFFER_H_
#define MYTH_DVD_BUFFER_H_

// C++
#include <array>

// Qt
#include <QMap>
#include <QString>
Expand All @@ -20,11 +23,15 @@ extern "C" {
#include "libavcodec/avcodec.h"
}

#define DVD_BLOCK_SIZE 2048LL
#define DVD_MENU_MAX 7

class MythDVDPlayer;

using CLUTArray = std::array<uint32_t,16>;
using AlphaArray = std::array<uint8_t,4>;
using PaletteArray = std::array<uint8_t,4>;
using ColorArray = std::array<uint8_t,256>;

class MTV_PUBLIC MythDVDBuffer : public MythOpticalBuffer
{
Q_DECLARE_TR_FUNCTIONS(MythDVDBuffer)
Expand Down Expand Up @@ -135,13 +142,14 @@ class MTV_PUBLIC MythDVDBuffer : public MythOpticalBuffer
static uint GetNibble (const uint8_t *Buffer, int NibbleOffset);
static int DecodeRLE (uint8_t *Bitmap, int Linesize, int Width, int Height,
const uint8_t *Buffer, int NibbleOffset, int BufferSize);
void GuessPalette (uint32_t *RGBAPalette, const uint8_t *Palette, const uint8_t *Alpha);
void GuessPalette (uint32_t *RGBAPalette, const PaletteArray& Palette,
const AlphaArray& Alpha);
static int IsTransparent (const uint8_t *Buffer, int Pitch, int Num,
const uint8_t *Colors);
const ColorArray& Colors);
static int FindSmallestBoundingRectangle(AVSubtitle *Subtitle);

dvdnav_t *m_dvdnav { nullptr };
unsigned char m_dvdBlockWriteBuf[DVD_BLOCK_SIZE] { 0 };
DvdBuffer m_dvdBlockWriteBuf { 0 };
unsigned char *m_dvdBlockReadBuf { nullptr };
int m_dvdBlockRPos { 0 };
int m_dvdBlockWPos { 0 };
Expand Down Expand Up @@ -184,7 +192,7 @@ class MTV_PUBLIC MythDVDBuffer : public MythOpticalBuffer
bool m_seeking { false };
int64_t m_seektime { 0 };
int64_t m_currentTime { 0 };
QMap<int, int> m_seekSpeedMap;
static const QMap<int, int> kSeekSpeedMap;
QMap<int, QList<uint64_t> > m_chapterMap;
MythDVDPlayer *m_parent { nullptr };
float m_forcedAspect { -1.0F };
Expand All @@ -195,9 +203,9 @@ class MTV_PUBLIC MythDVDBuffer : public MythOpticalBuffer
int32_t m_dvdEventSize { 0 };
uint m_buttonVersion { 1 };
int m_buttonStreamID { 0 };
uint32_t m_clut[16] { 0 };
uint8_t m_buttonColor[4] { 0 };
uint8_t m_buttonAlpha[4] { 0 };
CLUTArray m_clut { 0 };
AlphaArray m_buttonColor { 0 };
PaletteArray m_buttonAlpha { 0 };
QRect m_hlButton { 0, 0, 0, 0 };
uint8_t *m_menuSpuPkt { nullptr };
int m_menuBuflength { 0 };
Expand Down
6 changes: 3 additions & 3 deletions mythtv/libs/libmythtv/DVD/mythdvdinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ void MythDVDInfo::GetNameAndSerialNum(dvdnav_t* Nav,

if (fd > 0)
{
uint8_t buf[2048];
DvdBuffer buf {};
ssize_t read = 0;
auto crc = static_cast<uint32_t>(crc32(0L, Z_NULL, 0));

while((read = MythFileRead(fd, buf, sizeof(buf))) > 0)
crc = static_cast<uint32_t>(crc32(crc, buf, static_cast<uint>(read)));
while((read = MythFileRead(fd, buf.data(), buf.size())) > 0)
crc = static_cast<uint32_t>(crc32(crc, buf.data(), static_cast<uint>(read)));

MythfileClose(fd);
Serialnum = QString("%1__gen").arg(crc, 0, 16, QChar('0'));
Expand Down
6 changes: 6 additions & 0 deletions mythtv/libs/libmythtv/DVD/mythdvdinfo.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef MYTHDVDINFO_H
#define MYTHDVDINFO_H

// C++
#include <array>

// Qt
#include <QCoreApplication>

Expand All @@ -10,6 +13,9 @@
// libdvd
#include "dvdnav/dvdnav.h"

#define DVD_BLOCK_SIZE 2048LL
using DvdBuffer = std::array<uint8_t,DVD_BLOCK_SIZE>;

class MTV_PUBLIC MythDVDInfo
{
friend class MythDVDBuffer;
Expand Down
12 changes: 6 additions & 6 deletions mythtv/libs/libmythtv/DVD/mythdvdstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,25 +124,25 @@ bool MythDVDStream::OpenFile(const QString &Filename, uint /*Retry*/)
uint32_t len = 0;

// Root menu
char name[64] = "VIDEO_TS/VIDEO_TS.VOB";
uint32_t start = UDFFindFile(m_reader, name, &len);
QString name { "VIDEO_TS/VIDEO_TS.VOB" };
uint32_t start = UDFFindFile(m_reader, qPrintable(name), &len);
if( start != 0 && len != 0 )
m_blocks.append(BlockRange(start, Len2Blocks(len), 0));

const int kTitles = 100;
for (int title = 1; title < kTitles; ++title)
{
// Menu
snprintf(name, sizeof name, "/VIDEO_TS/VTS_%02d_0.VOB", title);
start = UDFFindFile(m_reader, name, &len);
name = QString("/VIDEO_TS/VTS_%1_0.VOB").arg(title,2,10,QChar('0'));
start = UDFFindFile(m_reader, qPrintable(name), &len);
if( start != 0 && len != 0 )
m_blocks.append(BlockRange(start, Len2Blocks(len), title));

for ( int part = 1; part < 10; ++part)
{
// A/V track
snprintf(name, sizeof name, "/VIDEO_TS/VTS_%02d_%d.VOB", title, part);
start = UDFFindFile(m_reader, name, &len);
name = QString("/VIDEO_TS/VTS_%1_%2.VOB").arg(title,2,10,QChar('0')).arg(part);
start = UDFFindFile(m_reader, qPrintable(name), &len);
if( start != 0 && len != 0 )
m_blocks.append(BlockRange(start, Len2Blocks(len), title + part * kTitles));
}
Expand Down
30 changes: 15 additions & 15 deletions mythtv/libs/libmythtv/HLS/httplivestreambuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ using std::min;
// encryption related stuff
#include <openssl/aes.h>
#define AES_BLOCK_SIZE 16 // HLS only support AES-128
using aesiv_array = std::array<uint8_t,AES_BLOCK_SIZE>;
#endif

#define LOC QString("HLSBuffer: ")
Expand Down Expand Up @@ -304,13 +305,13 @@ class HLSSegment
return RET_OK;
}

int DecodeData(const uint8_t *IV)
int DecodeData(const aesiv_array &IV, bool iv_valid)
{
/* Decrypt data using AES-128 */
int aeslen = m_data.size() & ~0xf;
unsigned char iv[AES_BLOCK_SIZE];
aesiv_array iv {};
char *decrypted_data = new char[m_data.size()];
if (IV == nullptr)
if (!iv_valid)
{
/*
* If the EXT-X-KEY tag does not have the IV attribute, implementations
Expand All @@ -319,19 +320,18 @@ class HLSSegment
* representation of the sequence number SHALL be placed in a 16-octet
* buffer and padded (on the left) with zeros.
*/
memset(iv, 0, AES_BLOCK_SIZE);
iv[15] = m_id & 0xff;
iv[14] = (m_id >> 8) & 0xff;
iv[13] = (m_id >> 16) & 0xff;
iv[12] = (m_id >> 24) & 0xff;
}
else
{
memcpy(iv, IV, sizeof(iv));
std::copy(IV.cbegin(), IV.cend(), iv.begin());
}
AES_cbc_encrypt((unsigned char*)m_data.constData(),
(unsigned char*)decrypted_data, aeslen,
&m_aeskey, iv, AES_DECRYPT);
&m_aeskey, iv.data(), AES_DECRYPT);
memcpy(decrypted_data + aeslen, m_data.constData() + aeslen,
m_data.size() - aeslen);

Expand Down Expand Up @@ -407,7 +407,7 @@ class HLSStream
m_bitrate = bitrate;
m_url = uri;
#ifdef USING_LIBCRYPTO
memset(m_aesIv, 0, sizeof(m_aesIv));
m_aesIv.fill(0);
#endif
}

Expand Down Expand Up @@ -448,7 +448,7 @@ class HLSStream
#ifdef USING_LIBCRYPTO
m_keypath = rhs.m_keypath;
m_ivloaded = rhs.m_ivloaded;
memcpy(m_aesIv, rhs.m_aesIv, sizeof(m_aesIv));
m_aesIv = rhs.m_aesIv;
#endif
return *this;
}
Expand Down Expand Up @@ -682,7 +682,7 @@ class HLSStream
return RET_OK;
}
}
if (segment->DecodeData(m_ivloaded ? m_aesIv : nullptr) != RET_OK)
if (segment->DecodeData(m_aesIv, m_ivloaded) != RET_OK)
{
segment->Unlock();
return RET_ERROR;
Expand Down Expand Up @@ -835,11 +835,11 @@ class HLSStream
int padding = max(0, AES_BLOCK_SIZE - (line.size() - 2));
QByteArray ba = QByteArray(padding, 0x0);
ba.append(QByteArray::fromHex(QByteArray(line.toLatin1().constData() + 2)));
memcpy(m_aesIv, ba.constData(), ba.size());
std::copy(ba.cbegin(), ba.cend(), m_aesIv.begin());
m_ivloaded = true;
return true;
}
uint8_t *AESIV(void)
aesiv_array AESIV(void)
{
return m_aesIv;
}
Expand All @@ -851,7 +851,7 @@ class HLSStream
private:
QString m_keypath; // URL path of the encrypted key
bool m_ivloaded {false};
uint8_t m_aesIv[AES_BLOCK_SIZE]{0};// IV used when decypher the block
aesiv_array m_aesIv {0}; // IV used when decypher the block
#endif

private:
Expand Down Expand Up @@ -1726,11 +1726,11 @@ bool HLSRingBuffer::TestForHTTPLiveStreaming(const QString &filename)
AVIO_FLAG_READ, nullptr, nullptr);
if (ret >= 0)
{
unsigned char buffer[1024];
ret = ffurl_read(context, buffer, sizeof(buffer));
std::array<uint8_t,1024> buffer {};
ret = ffurl_read(context, buffer.data(), buffer.size());
if (ret > 0)
{
QByteArray ba((const char*)buffer, ret);
QByteArray ba((const char*)buffer.data(), ret);
isHLS = IsHTTPLiveStreaming(&ba);
}
ffurl_close(context);
Expand Down