This source file includes following definitions.
- frameSize
- writePicture
#include "common.h"
#include "output.h"
#include "yuv.h"
using namespace X265_NS;
using namespace std;
YUVOutput::YUVOutput(const char *filename, int w, int h, uint32_t d, int csp)
: width(w)
, height(h)
, depth(d)
, colorSpace(csp)
, frameSize(0)
{
ofs.open(filename, ios::binary | ios::out);
buf = new char[width];
for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++)
frameSize += (uint32_t)((width >> x265_cli_csps[colorSpace].width[i]) * (height >> x265_cli_csps[colorSpace].height[i]));
}
YUVOutput::~YUVOutput()
{
ofs.close();
delete [] buf;
}
bool YUVOutput::writePicture(const x265_picture& pic)
{
uint64_t fileOffset = pic.poc;
fileOffset *= frameSize;
X265_CHECK(pic.colorSpace == colorSpace, "invalid chroma subsampling\n");
X265_CHECK(pic.bitDepth == (int)depth, "invalid bit depth\n");
#if HIGH_BIT_DEPTH
if (depth == 8)
{
int shift = pic.bitDepth - 8;
ofs.seekp((std::streamoff)fileOffset);
for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++)
{
uint16_t *src = (uint16_t*)pic.planes[i];
for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++)
{
for (int w = 0; w < width >> x265_cli_csps[colorSpace].width[i]; w++)
buf[w] = (char)(src[w] >> shift);
ofs.write(buf, width >> x265_cli_csps[colorSpace].width[i]);
src += pic.stride[i] / sizeof(*src);
}
}
}
else
{
ofs.seekp((std::streamoff)(fileOffset * 2));
for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++)
{
uint16_t *src = (uint16_t*)pic.planes[i];
for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++)
{
ofs.write((const char*)src, (width * 2) >> x265_cli_csps[colorSpace].width[i]);
src += pic.stride[i] / sizeof(*src);
}
}
}
#else
ofs.seekp((std::streamoff)fileOffset);
for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++)
{
char *src = (char*)pic.planes[i];
for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++)
{
ofs.write(src, width >> x265_cli_csps[colorSpace].width[i]);
src += pic.stride[i] / sizeof(*src);
}
}
#endif
return true;
}