1 /*****************************************************************************
2 * Copyright (C) 2013-2020 MulticoreWare, Inc
3 *
4 * Authors: Steve Borho <steve@borho.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
19 *
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
23
24 #include "common.h"
25 #include "output.h"
26 #include "yuv.h"
27
28 using namespace X265_NS;
29 using namespace std;
30
YUVOutput(const char * filename,int w,int h,uint32_t d,int csp)31 YUVOutput::YUVOutput(const char *filename, int w, int h, uint32_t d, int csp)
32 : width(w)
33 , height(h)
34 , depth(d)
35 , colorSpace(csp)
36 , frameSize(0)
37 {
38 ofs.open(filename, ios::binary | ios::out);
39 buf = new char[width];
40
41 for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++)
42 frameSize += (uint32_t)((width >> x265_cli_csps[colorSpace].width[i]) * (height >> x265_cli_csps[colorSpace].height[i]));
43 }
44
~YUVOutput()45 YUVOutput::~YUVOutput()
46 {
47 ofs.close();
48 delete [] buf;
49 }
50
writePicture(const x265_picture & pic)51 bool YUVOutput::writePicture(const x265_picture& pic)
52 {
53 uint64_t fileOffset = pic.poc;
54 fileOffset *= frameSize;
55
56 X265_CHECK(pic.colorSpace == colorSpace, "invalid chroma subsampling\n");
57 X265_CHECK(pic.bitDepth == (int)depth, "invalid bit depth\n");
58
59 #if HIGH_BIT_DEPTH
60 if (depth == 8)
61 {
62 int shift = pic.bitDepth - 8;
63 ofs.seekp((std::streamoff)fileOffset);
64 for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++)
65 {
66 uint16_t *src = (uint16_t*)pic.planes[i];
67 for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++)
68 {
69 for (int w = 0; w < width >> x265_cli_csps[colorSpace].width[i]; w++)
70 buf[w] = (char)(src[w] >> shift);
71
72 ofs.write(buf, width >> x265_cli_csps[colorSpace].width[i]);
73 src += pic.stride[i] / sizeof(*src);
74 }
75 }
76 }
77 else
78 {
79 ofs.seekp((std::streamoff)(fileOffset * 2));
80 for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++)
81 {
82 uint16_t *src = (uint16_t*)pic.planes[i];
83 for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++)
84 {
85 ofs.write((const char*)src, (width * 2) >> x265_cli_csps[colorSpace].width[i]);
86 src += pic.stride[i] / sizeof(*src);
87 }
88 }
89 }
90 #else // if HIGH_BIT_DEPTH
91 ofs.seekp((std::streamoff)fileOffset);
92 for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++)
93 {
94 char *src = (char*)pic.planes[i];
95 for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++)
96 {
97 ofs.write(src, width >> x265_cli_csps[colorSpace].width[i]);
98 src += pic.stride[i] / sizeof(*src);
99 }
100 }
101
102 #endif // if HIGH_BIT_DEPTH
103
104 return true;
105 }
106