You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

491 lines
9.0 KiB

#include "h264.h"
#include <cstdio>
#include <cstring>
namespace mtx {
namespace h264 {
namespace {
// Exp-Golomb bit reader over an RBSP (emulation prevention bytes removed)
class BitReader
{
public:
explicit BitReader(const Bytes & d) : m_d(d) {}
bool bit(uint32_t & v)
{
if (m_pos >= m_d.size() * 8)
{
return false;
}
v = (m_d[m_pos >> 3] >> (7 - (m_pos & 7))) & 1;
m_pos++;
return true;
}
bool bits(int n, uint32_t & v)
{
v = 0;
for (int i = 0; i < n; i++)
{
uint32_t b;
if (! bit(b))
{
return false;
}
v = (v << 1) | b;
}
return true;
}
bool ue(uint32_t & v)
{
int tZeros = 0;
uint32_t b = 0;
while (true)
{
if (! bit(b))
{
return false;
}
if (b)
{
break;
}
if (++tZeros > 31)
{
return false;
}
}
uint32_t tRest = 0;
if (! bits(tZeros, tRest))
{
return false;
}
v = ((1u << tZeros) - 1) + tRest;
return true;
}
bool se(int32_t & v)
{
uint32_t u;
if (! ue(u))
{
return false;
}
v = (u & 1) ? (int32_t) ((u + 1) / 2) : -(int32_t) (u / 2);
return true;
}
private:
const Bytes & m_d;
size_t m_pos = 0;
};
Bytes rbsp(const Bytes & nal)
{
Bytes tOut;
tOut.reserve(nal.size());
int tZeros = 0;
for (size_t i = 0; i < nal.size(); i++)
{
if (tZeros >= 2 && nal[i] == 3)
{
tZeros = 0;
continue;
}
tZeros = (nal[i] == 0) ? tZeros + 1 : 0;
tOut.push_back(nal[i]);
}
return tOut;
}
bool skipScalingList(BitReader & r, int size)
{
int32_t tLast = 8, tNext = 8;
for (int j = 0; j < size; j++)
{
if (tNext != 0)
{
int32_t tDelta;
if (! r.se(tDelta))
{
return false;
}
tNext = (tLast + tDelta + 256) % 256;
}
tLast = (tNext == 0) ? tLast : tNext;
}
return true;
}
} // namespace
bool parseSps(const Bytes & sps, SpsInfo & info)
{
if (sps.size() < 4 || naluType(sps) != NALU_SPS)
{
return false;
}
Bytes tR = rbsp(sps);
BitReader r(tR);
uint32_t v, tForbidden;
r.bits(8, tForbidden); // NAL header
uint32_t tProfile, tConstr, tLevel;
if (! r.bits(8, tProfile) || ! r.bits(8, tConstr) || ! r.bits(8, tLevel) || ! r.ue(v))
{
return false;
}
info.profileIdc = (int) tProfile;
info.constraints = (int) tConstr;
info.levelIdc = (int) tLevel;
uint32_t tChroma = 1;
if (tProfile == 100 || tProfile == 110 || tProfile == 122 || tProfile == 244 || tProfile == 44 || tProfile == 83 || tProfile == 86 || tProfile == 118 ||
tProfile == 128 || tProfile == 138 || tProfile == 139 || tProfile == 134 || tProfile == 135)
{
if (! r.ue(tChroma))
{
return false;
}
if (tChroma == 3)
{
r.bit(v); // separate_colour_plane_flag
}
r.ue(v); // bit_depth_luma
r.ue(v); // bit_depth_chroma
r.bit(v); // qpprime_y_zero_transform_bypass
uint32_t tScaling;
if (! r.bit(tScaling))
{
return false;
}
if (tScaling)
{
int tCount = (tChroma != 3) ? 8 : 12;
for (int i = 0; i < tCount; i++)
{
uint32_t tPresent;
if (! r.bit(tPresent))
{
return false;
}
if (tPresent && ! skipScalingList(r, i < 6 ? 16 : 64))
{
return false;
}
}
}
}
r.ue(v); // log2_max_frame_num_minus4
uint32_t tPocType;
if (! r.ue(tPocType))
{
return false;
}
if (tPocType == 0)
{
r.ue(v);
}
else if (tPocType == 1)
{
int32_t s;
r.bit(v);
r.se(s);
r.se(s);
uint32_t tCycle;
if (! r.ue(tCycle) || tCycle > 255)
{
return false;
}
for (uint32_t i = 0; i < tCycle; i++)
{
r.se(s);
}
}
r.ue(v); // max_num_ref_frames
r.bit(v); // gaps_in_frame_num_allowed
uint32_t tWmbs, tHmbs, tFrameMbsOnly;
if (! r.ue(tWmbs) || ! r.ue(tHmbs) || ! r.bit(tFrameMbsOnly))
{
return false;
}
if (! tFrameMbsOnly)
{
r.bit(v); // mb_adaptive_frame_field
}
r.bit(v); // direct_8x8_inference
uint32_t tCrop, cl = 0, cr = 0, ct = 0, cb = 0;
if (! r.bit(tCrop))
{
return false;
}
if (tCrop)
{
r.ue(cl);
r.ue(cr);
r.ue(ct);
r.ue(cb);
}
int tCropX = (tChroma == 0 || tChroma == 3) ? 1 : 2;
int tCropY = ((tChroma == 1) ? 2 : 1) * (2 - (int) tFrameMbsOnly);
info.width = (int) (tWmbs + 1) * 16 - (int) (cl + cr) * tCropX;
info.height = (int) (2 - tFrameMbsOnly) * (int) (tHmbs + 1) * 16 - (int) (ct + cb) * tCropY;
return info.width > 0 && info.height > 0;
}
std::string profileLevelId(const Bytes & sps)
{
if (sps.size() < 4)
{
return "42e01f";
}
char tB[8];
snprintf(tB, sizeof(tB), "%02x%02x%02x", sps[1], sps[2], sps[3]);
return tB;
}
static int b64val(char c)
{
if (c >= 'A' && c <= 'Z')
{
return c - 'A';
}
if (c >= 'a' && c <= 'z')
{
return c - 'a' + 26;
}
if (c >= '0' && c <= '9')
{
return c - '0' + 52;
}
if (c == '+' || c == '-')
{
return 62;
}
if (c == '/' || c == '_')
{
return 63;
}
return -1;
}
std::vector<Bytes> decodeSpropParameterSets(const char * sprop)
{
std::vector<Bytes> tOut;
if (sprop == nullptr)
{
return tOut;
}
Bytes tCur;
uint32_t tAcc = 0;
int tBits = 0;
for (const char * p = sprop;; p++)
{
if (*p == ',' || *p == 0)
{
if (! tCur.empty())
{
tOut.push_back(tCur);
}
tCur.clear();
tAcc = 0;
tBits = 0;
if (*p == 0)
{
break;
}
continue;
}
int v = b64val(*p);
if (v < 0)
{
continue; // '=' padding, spaces
}
tAcc = (tAcc << 6) | (uint32_t) v;
tBits += 6;
if (tBits >= 8)
{
tBits -= 8;
tCur.push_back((uint8_t) ((tAcc >> tBits) & 0xff));
}
}
return tOut;
}
Bytes toAnnexB(const std::vector<Bytes> & nalus)
{
Bytes tOut;
for (const Bytes & n : nalus)
{
static const uint8_t tSc[4] = {0, 0, 0, 1};
tOut.insert(tOut.end(), tSc, tSc + 4);
tOut.insert(tOut.end(), n.begin(), n.end());
}
return tOut;
}
Bytes toAvcc(const std::vector<Bytes> & nalus)
{
Bytes tOut;
for (const Bytes & n : nalus)
{
uint32_t s = (uint32_t) n.size();
tOut.push_back((uint8_t) (s >> 24));
tOut.push_back((uint8_t) (s >> 16));
tOut.push_back((uint8_t) (s >> 8));
tOut.push_back((uint8_t) s);
tOut.insert(tOut.end(), n.begin(), n.end());
}
return tOut;
}
Bytes buildAvcC(const Bytes & sps, const Bytes & pps)
{
Bytes r;
r.push_back(1);
r.push_back(sps.size() > 1 ? sps[1] : 0x42);
r.push_back(sps.size() > 2 ? sps[2] : 0xe0);
r.push_back(sps.size() > 3 ? sps[3] : 0x1f);
r.push_back(0xff); // 4 byte NAL lengths
r.push_back(0xe1); // 1 SPS
r.push_back((uint8_t) (sps.size() >> 8));
r.push_back((uint8_t) sps.size());
r.insert(r.end(), sps.begin(), sps.end());
r.push_back(1); // 1 PPS
r.push_back((uint8_t) (pps.size() >> 8));
r.push_back((uint8_t) pps.size());
r.insert(r.end(), pps.begin(), pps.end());
return r;
}
std::vector<Bytes> splitAnnexB(const uint8_t * data, size_t size)
{
std::vector<Bytes> tOut;
if (data == nullptr)
{
return tOut;
}
size_t tStart = std::string::npos; // first byte of the current NAL unit
size_t i = 0;
auto tEmit = [&](size_t end) {
// trailing zero bytes belong to the next start code (00 00 00 01)
while (end > tStart && data[end - 1] == 0)
{
end--;
}
if (end > tStart && (data[tStart] & 0x1f) != NALU_AUD)
{
tOut.emplace_back(data + tStart, data + end);
}
};
while (i + 2 < size)
{
if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 1)
{
if (tStart != std::string::npos)
{
tEmit(i);
}
i += 3;
tStart = i;
continue;
}
i++;
}
if (tStart != std::string::npos)
{
tEmit(size);
}
else if (size > 0)
{
tOut.emplace_back(data, data + size); // no start code: one bare NAL unit
}
return tOut;
}
std::vector<Bytes> splitAvcc(const uint8_t * data, size_t size, int lengthSize)
{
std::vector<Bytes> tOut;
if (data == nullptr || lengthSize < 1 || lengthSize > 4)
{
return tOut;
}
size_t i = 0;
while (i + (size_t) lengthSize <= size)
{
size_t tLen = 0;
for (int k = 0; k < lengthSize; k++)
{
tLen = (tLen << 8) | data[i + k];
}
i += lengthSize;
if (tLen == 0 || i + tLen > size)
{
break;
}
if ((data[i] & 0x1f) != NALU_AUD)
{
tOut.emplace_back(data + i, data + i + tLen);
}
i += tLen;
}
return tOut;
}
std::vector<Bytes> parameterSetsFromExtradata(const uint8_t * data, size_t size, int * avccLengthSize)
{
std::vector<Bytes> tOut;
if (avccLengthSize)
{
*avccLengthSize = 0;
}
if (data == nullptr || size < 4)
{
return tOut;
}
if (data[0] != 1)
{
// Annex-B extradata (RTSP / raw H264 sources)
for (Bytes & n : splitAnnexB(data, size))
{
int t = naluType(n);
if (t == NALU_SPS || t == NALU_PPS)
{
tOut.push_back(std::move(n));
}
}
return tOut;
}
// AVCDecoderConfigurationRecord
if (size < 7)
{
return tOut;
}
if (avccLengthSize)
{
*avccLengthSize = (data[4] & 0x03) + 1;
}
size_t i = 5;
for (int tKind = 0; tKind < 2; tKind++)
{
if (i >= size)
{
break;
}
int tCount = (tKind == 0) ? (data[i] & 0x1f) : data[i];
i++;
for (int k = 0; k < tCount && i + 2 <= size; k++)
{
size_t tLen = ((size_t) data[i] << 8) | data[i + 1];
i += 2;
if (i + tLen > size)
{
return tOut;
}
tOut.emplace_back(data + i, data + i + tLen);
i += tLen;
}
}
return tOut;
}
} // namespace h264
} // namespace mtx