| 1 | /***************************************************************************** |
| 2 | * Copyright (C) 2013 x265 project |
| 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; |
| 29 | using namespace std; |
| 30 | |
| 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 | { |
| 43 | frameSize += (uint32_t)((width >> x265_cli_csps[colorSpace].width[i]) * (height >> x265_cli_csps[colorSpace].height[i])); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | YUVOutput::~YUVOutput() |
| 48 | { |
| 49 | ofs.close(); |
| 50 | delete [] buf; |
| 51 | } |
| 52 | |
| 53 | bool YUVOutput::writePicture(const x265_picture& pic) |
| 54 | { |
| 55 | uint64_t fileOffset = pic.poc; |
| 56 | fileOffset *= frameSize; |
| 57 | |
| 58 | X265_CHECK(pic.colorSpace == colorSpace, "invalid color space\n"); |
| 59 | X265_CHECK(pic.bitDepth == (int)depth, "invalid bit depth\n"); |
| 60 | |
| 61 | #if HIGH_BIT_DEPTH |
| 62 | if (depth == 8) |
| 63 | { |
| 64 | int shift = pic.bitDepth - 8; |
| 65 | ofs.seekp((std::streamoff)fileOffset); |
| 66 | for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++) |
| 67 | { |
| 68 | uint16_t *src = (uint16_t*)pic.planes[i]; |
| 69 | for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++) |
| 70 | { |
| 71 | for (int w = 0; w < width >> x265_cli_csps[colorSpace].width[i]; w++) |
| 72 | { |
| 73 | buf[w] = (char)(src[w] >> shift); |
| 74 | } |
| 75 | |
| 76 | ofs.write(buf, width >> x265_cli_csps[colorSpace].width[i]); |
| 77 | src += pic.stride[i] / sizeof(*src); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | ofs.seekp((std::streamoff)(fileOffset * 2)); |
| 84 | for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++) |
| 85 | { |
| 86 | uint16_t *src = (uint16_t*)pic.planes[i]; |
| 87 | for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++) |
| 88 | { |
| 89 | ofs.write((const char*)src, (width * 2) >> x265_cli_csps[colorSpace].width[i]); |
| 90 | src += pic.stride[i] / sizeof(*src); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | #else // if HIGH_BIT_DEPTH |
| 95 | ofs.seekp((std::streamoff)fileOffset); |
| 96 | for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++) |
| 97 | { |
| 98 | char *src = (char*)pic.planes[i]; |
| 99 | for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++) |
| 100 | { |
| 101 | ofs.write(src, width >> x265_cli_csps[colorSpace].width[i]); |
| 102 | src += pic.stride[i] / sizeof(*src); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | #endif // if HIGH_BIT_DEPTH |
| 107 | |
| 108 | return true; |
| 109 | } |