| 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 "y4m.h" |
| 27 | |
| 28 | using namespace x265; |
| 29 | using namespace std; |
| 30 | |
| 31 | Y4MOutput::Y4MOutput(const char *filename, int w, int h, uint32_t fpsNum, uint32_t fpsDenom, int csp) |
| 32 | : width(w) |
| 33 | , height(h) |
| 34 | , colorSpace(csp) |
| 35 | , frameSize(0) |
| 36 | { |
| 37 | ofs.open(filename, ios::binary | ios::out); |
| 38 | buf = new char[width]; |
| 39 | |
| 40 | const char *cf = (csp >= X265_CSP_I444) ? "444" : (csp >= X265_CSP_I422) ? "422" : "420"; |
| 41 | |
| 42 | if (ofs) |
| 43 | { |
| 44 | ofs << "YUV4MPEG2 W" << width << " H" << height << " F" << fpsNum << ":" << fpsDenom << " Ip" << " C" << cf << "\n"; |
| 45 | header = ofs.tellp(); |
| 46 | } |
| 47 | |
| 48 | for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++) |
| 49 | { |
| 50 | frameSize += (uint32_t)((width >> x265_cli_csps[colorSpace].width[i]) * (height >> x265_cli_csps[colorSpace].height[i])); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | Y4MOutput::~Y4MOutput() |
| 55 | { |
| 56 | ofs.close(); |
| 57 | delete [] buf; |
| 58 | } |
| 59 | |
| 60 | bool Y4MOutput::writePicture(const x265_picture& pic) |
| 61 | { |
| 62 | std::ofstream::pos_type outPicPos = header; |
| 63 | outPicPos += (uint64_t)pic.poc * (6 + frameSize); |
| 64 | ofs.seekp(outPicPos); |
| 65 | ofs << "FRAME\n"; |
| 66 | |
| 67 | #if HIGH_BIT_DEPTH |
| 68 | if (pic.bitDepth > 8 && pic.poc == 0) |
| 69 | { |
| 70 | x265_log(NULL, X265_LOG_WARNING, "y4m: down-shifting reconstructed pixels to 8 bits\n"); |
| 71 | } |
| 72 | #else |
| 73 | if (pic.bitDepth > 8 && pic.poc == 0) |
| 74 | { |
| 75 | x265_log(NULL, X265_LOG_WARNING, "y4m: forcing reconstructed pixels to 8 bits\n"); |
| 76 | } |
| 77 | #endif |
| 78 | |
| 79 | X265_CHECK(pic.colorSpace == colorSpace, "invalid color space\n"); |
| 80 | |
| 81 | #if HIGH_BIT_DEPTH |
| 82 | |
| 83 | // encoder gave us short pixels, downshift, then write |
| 84 | X265_CHECK(pic.bitDepth > 8, "invalid bit depth\n"); |
| 85 | int shift = pic.bitDepth - 8; |
| 86 | for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++) |
| 87 | { |
| 88 | uint16_t *src = (uint16_t*)pic.planes[i]; |
| 89 | for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++) |
| 90 | { |
| 91 | for (int w = 0; w < width >> x265_cli_csps[colorSpace].width[i]; w++) |
| 92 | { |
| 93 | buf[w] = (char)(src[w] >> shift); |
| 94 | } |
| 95 | |
| 96 | ofs.write(buf, width >> x265_cli_csps[colorSpace].width[i]); |
| 97 | src += pic.stride[i] / sizeof(*src); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | #else // if HIGH_BIT_DEPTH |
| 102 | |
| 103 | X265_CHECK(pic.bitDepth == 8, "invalid bit depth\n"); |
| 104 | for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++) |
| 105 | { |
| 106 | char *src = (char*)pic.planes[i]; |
| 107 | for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++) |
| 108 | { |
| 109 | ofs.write(src, width >> x265_cli_csps[colorSpace].width[i]); |
| 110 | src += pic.stride[i] / sizeof(*src); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | #endif // if HIGH_BIT_DEPTH |
| 115 | |
| 116 | return true; |
| 117 | } |