1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
4 * Authors: Steve Borho <steve@borho.org>
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.
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.
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.
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
31 Y4MOutput::Y4MOutput(const char *filename
, int w
, int h
, uint32_t fpsNum
, uint32_t fpsDenom
, int csp
)
37 ofs
.open(filename
, ios::binary
| ios::out
);
38 buf
= new char[width
];
40 const char *cf
= (csp
>= X265_CSP_I444
) ? "444" : (csp
>= X265_CSP_I422
) ? "422" : "420";
44 ofs
<< "YUV4MPEG2 W" << width
<< " H" << height
<< " F" << fpsNum
<< ":" << fpsDenom
<< " Ip" << " C" << cf
<< "\n";
48 for (int i
= 0; i
< x265_cli_csps
[colorSpace
].planes
; i
++)
50 frameSize
+= (uint32_t)((width
>> x265_cli_csps
[colorSpace
].width
[i
]) * (height
>> x265_cli_csps
[colorSpace
].height
[i
]));
54 Y4MOutput::~Y4MOutput()
60 bool Y4MOutput::writePicture(const x265_picture
& pic
)
62 std::ofstream::pos_type outPicPos
= header
;
63 outPicPos
+= (uint64_t)pic
.poc
* (6 + frameSize
);
68 if (pic
.bitDepth
> 8 && pic
.poc
== 0)
70 x265_log(NULL
, X265_LOG_WARNING
, "y4m: down-shifting reconstructed pixels to 8 bits\n");
73 if (pic
.bitDepth
> 8 && pic
.poc
== 0)
75 x265_log(NULL
, X265_LOG_WARNING
, "y4m: forcing reconstructed pixels to 8 bits\n");
79 X265_CHECK(pic
.colorSpace
== colorSpace
, "invalid color space\n");
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
++)
88 uint16_t *src
= (uint16_t*)pic
.planes
[i
];
89 for (int h
= 0; h
< height
>> x265_cli_csps
[colorSpace
].height
[i
]; h
++)
91 for (int w
= 0; w
< width
>> x265_cli_csps
[colorSpace
].width
[i
]; w
++)
93 buf
[w
] = (char)(src
[w
] >> shift
);
96 ofs
.write(buf
, width
>> x265_cli_csps
[colorSpace
].width
[i
]);
97 src
+= pic
.stride
[i
] / sizeof(*src
);
101 #else // if HIGH_BIT_DEPTH
103 X265_CHECK(pic
.bitDepth
== 8, "invalid bit depth\n");
104 for (int i
= 0; i
< x265_cli_csps
[colorSpace
].planes
; i
++)
106 char *src
= (char*)pic
.planes
[i
];
107 for (int h
= 0; h
< height
>> x265_cli_csps
[colorSpace
].height
[i
]; h
++)
109 ofs
.write(src
, width
>> x265_cli_csps
[colorSpace
].width
[i
]);
110 src
+= pic
.stride
[i
] / sizeof(*src
);
114 #endif // if HIGH_BIT_DEPTH