Skip to content

Commit 6be3d77

Browse files
krajunvzlatinski
authored andcommitted
Add missing copy of input data from file to memory for YUV444P
Also add support frame count calculations for YUV444P and YUV422P Signed-off-by: Raju Konda <kraju@nvidia.com>
1 parent d6fb173 commit 6be3d77

File tree

3 files changed

+99
-6
lines changed

3 files changed

+99
-6
lines changed

common/libs/VkCodecUtils/YCbCrConvUtilsCpu.h

+45
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,51 @@ class YCbCrConvUtilsCpu
240240

241241
return 0;
242242
}
243+
244+
static int I444ToP444(const planeType* src_y,
245+
int src_stride_y,
246+
const planeType* src_u,
247+
int src_stride_u,
248+
const planeType* src_v,
249+
int src_stride_v,
250+
planeType* dst_y,
251+
int dst_stride_y,
252+
planeType* dst_uv,
253+
int dst_stride_uv,
254+
int width,
255+
int height,
256+
int shiftBits = 0) {
257+
if (!src_y || !src_u || !src_v || !dst_y || !dst_uv || width <= 0 || height == 0) {
258+
return -1;
259+
}
260+
261+
// Convert strides from bytes to elements
262+
src_stride_y /= (int)sizeof(planeType);
263+
dst_stride_y /= (int)sizeof(planeType);
264+
src_stride_u /= (int)sizeof(planeType);
265+
src_stride_v /= (int)sizeof(planeType);
266+
dst_stride_uv /= (int)sizeof(planeType);
267+
268+
// Handle negative height (image inversion)
269+
if (height < 0) {
270+
height = -height;
271+
src_y = src_y + (height - 1) * src_stride_y;
272+
src_u = src_u + (height - 1) * src_stride_u;
273+
src_v = src_v + (height - 1) * src_stride_v;
274+
src_stride_y = -src_stride_y;
275+
src_stride_u = -src_stride_u;
276+
src_stride_v = -src_stride_v;
277+
}
278+
279+
// Copy Y plane at full resolution
280+
CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height, shiftBits);
281+
282+
// Merge U and V planes at full resolution
283+
MergeUVPlane(src_u, src_stride_u, src_v, src_stride_v, dst_uv, dst_stride_uv,
284+
width, height, shiftBits);
285+
286+
return 0;
287+
}
243288
};
244289

245290
#endif /* _VKCODECUTILS_YCBCRCONVUTILSCPU_H_ */

vk_video_encoder/libs/VkVideoEncoder/VkEncoderConfig.h

+15-2
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,23 @@ class EncoderInputFileHandler
393393

394394
uint32_t GetFrameCount(uint32_t width, uint32_t height, uint8_t bpp, VkVideoChromaSubsamplingFlagBitsKHR chromaSubsampling) {
395395
uint8_t nBytes = (uint8_t)(bpp + 7) / 8;
396-
double samplingFactor = 1.5;
396+
double samplingFactor = 1.5; // Default for 420
397397
switch (chromaSubsampling)
398398
{
399+
case VK_VIDEO_CHROMA_SUBSAMPLING_MONOCHROME_BIT_KHR:
400+
samplingFactor = 1.0; // Only Y component
401+
break;
399402
case VK_VIDEO_CHROMA_SUBSAMPLING_420_BIT_KHR:
400-
samplingFactor = 1.5;
403+
samplingFactor = 1.5; // Y + 1/4 U + 1/4 V = 1.5
404+
break;
405+
case VK_VIDEO_CHROMA_SUBSAMPLING_422_BIT_KHR:
406+
samplingFactor = 2.0; // Y + 1/2 U + 1/2 V = 2.0
407+
break;
408+
case VK_VIDEO_CHROMA_SUBSAMPLING_444_BIT_KHR:
409+
samplingFactor = 3.0; // Full Y + full U + full V = 3.0
401410
break;
402411
default:
412+
assert(!"Unknown chroma subsampling");
403413
break;
404414
}
405415
uint32_t frameSize = (uint32_t)(width * height * nBytes * samplingFactor);
@@ -929,6 +939,9 @@ struct EncoderConfig : public VkVideoRefCountBase {
929939
return VK_ERROR_INVALID_VIDEO_STD_PARAMETERS_KHR;
930940
}
931941

942+
// Copy chroma subsampling from input to encoder config
943+
encodeChromaSubsampling = input.chromaSubsampling;
944+
932945
if ((encodeWidth == 0) || (encodeWidth > input.width)) {
933946
encodeWidth = input.width;
934947
}

vk_video_encoder/libs/VkVideoEncoder/VkVideoEncoder.cpp

+39-4
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,24 @@ VkResult VkVideoEncoder::LoadNextFrame(VkSharedBaseObj<VkVideoEncodeFrameInfo>&
170170
int yCbCrConvResult = 0;
171171
if (m_encoderConfig->input.bpp == 8) {
172172

173-
// Load current 8-bit frame from file and convert to NV12
174-
yCbCrConvResult = YCbCrConvUtilsCpu<uint8_t>::I420ToNV12(
173+
if (m_encoderConfig->encodeChromaSubsampling == VK_VIDEO_CHROMA_SUBSAMPLING_444_BIT_KHR) {
174+
// Load current 8-bit frame from file and convert to 2-plane YUV444
175+
yCbCrConvResult = YCbCrConvUtilsCpu<uint8_t>::I444ToP444(
176+
pInputFrameData + m_encoderConfig->input.planeLayouts[0].offset, // src_y
177+
(int)m_encoderConfig->input.planeLayouts[0].rowPitch, // src_stride_y
178+
pInputFrameData + m_encoderConfig->input.planeLayouts[1].offset, // src_u
179+
(int)m_encoderConfig->input.planeLayouts[1].rowPitch, // src_stride_u
180+
pInputFrameData + m_encoderConfig->input.planeLayouts[2].offset, // src_v
181+
(int)m_encoderConfig->input.planeLayouts[2].rowPitch, // src_stride_v
182+
writeImagePtr + dstSubresourceLayout[0].offset, // dst_y
183+
(int)dstSubresourceLayout[0].rowPitch, // dst_stride_y
184+
writeImagePtr + dstSubresourceLayout[1].offset, // dst_uv
185+
(int)dstSubresourceLayout[1].rowPitch, // dst_stride_uv
186+
std::min(m_encoderConfig->encodeWidth, m_encoderConfig->input.width), // width
187+
std::min(m_encoderConfig->encodeHeight, m_encoderConfig->input.height)); // height
188+
} else {
189+
// Load current 8-bit frame from file and convert to NV12
190+
yCbCrConvResult = YCbCrConvUtilsCpu<uint8_t>::I420ToNV12(
175191
pInputFrameData + m_encoderConfig->input.planeLayouts[0].offset, // src_y,
176192
(int)m_encoderConfig->input.planeLayouts[0].rowPitch, // src_stride_y,
177193
pInputFrameData + m_encoderConfig->input.planeLayouts[1].offset, // src_u,
@@ -184,6 +200,7 @@ VkResult VkVideoEncoder::LoadNextFrame(VkSharedBaseObj<VkVideoEncodeFrameInfo>&
184200
(int)dstSubresourceLayout[1].rowPitch, // dst_stride_uv,
185201
std::min(m_encoderConfig->encodeWidth, m_encoderConfig->input.width), // width
186202
std::min(m_encoderConfig->encodeHeight, m_encoderConfig->input.height)); // height
203+
}
187204

188205
} else if (m_encoderConfig->input.bpp == 10) { // 10-bit - actually 16-bit only for now.
189206

@@ -194,8 +211,25 @@ VkResult VkVideoEncoder::LoadNextFrame(VkSharedBaseObj<VkVideoEncodeFrameInfo>&
194211
shiftBits = 16 - m_encoderConfig->input.bpp;
195212
}
196213

197-
// Load current 10-bit frame from file and convert to P010/P016
198-
yCbCrConvResult = YCbCrConvUtilsCpu<uint16_t>::I420ToNV12(
214+
if (m_encoderConfig->encodeChromaSubsampling == VK_VIDEO_CHROMA_SUBSAMPLING_444_BIT_KHR) {
215+
// Load current 10-bit frame from file and convert to 2-plane YUV444
216+
yCbCrConvResult = YCbCrConvUtilsCpu<uint16_t>::I444ToP444(
217+
(const uint16_t*)(pInputFrameData + m_encoderConfig->input.planeLayouts[0].offset), // src_y
218+
(int)m_encoderConfig->input.planeLayouts[0].rowPitch, // src_stride_y
219+
(const uint16_t*)(pInputFrameData + m_encoderConfig->input.planeLayouts[1].offset), // src_u
220+
(int)m_encoderConfig->input.planeLayouts[1].rowPitch, // src_stride_u
221+
(const uint16_t*)(pInputFrameData + m_encoderConfig->input.planeLayouts[2].offset), // src_v
222+
(int)m_encoderConfig->input.planeLayouts[2].rowPitch, // src_stride_v
223+
(uint16_t*)(writeImagePtr + dstSubresourceLayout[0].offset), // dst_y
224+
(int)dstSubresourceLayout[0].rowPitch, // dst_stride_y
225+
(uint16_t*)(writeImagePtr + dstSubresourceLayout[1].offset), // dst_uv
226+
(int)dstSubresourceLayout[1].rowPitch, // dst_stride_uv
227+
std::min(m_encoderConfig->encodeWidth, m_encoderConfig->input.width), // width
228+
std::min(m_encoderConfig->encodeHeight, m_encoderConfig->input.height), // height
229+
shiftBits);
230+
} else {
231+
// Load current 10-bit frame from file and convert to P010/P016
232+
yCbCrConvResult = YCbCrConvUtilsCpu<uint16_t>::I420ToNV12(
199233
(const uint16_t*)(pInputFrameData + m_encoderConfig->input.planeLayouts[0].offset), // src_y,
200234
(int)m_encoderConfig->input.planeLayouts[0].rowPitch, // src_stride_y,
201235
(const uint16_t*)(pInputFrameData + m_encoderConfig->input.planeLayouts[1].offset), // src_u,
@@ -209,6 +243,7 @@ VkResult VkVideoEncoder::LoadNextFrame(VkSharedBaseObj<VkVideoEncodeFrameInfo>&
209243
std::min(m_encoderConfig->encodeWidth, m_encoderConfig->input.width), // width
210244
std::min(m_encoderConfig->encodeHeight, m_encoderConfig->input.height), // height
211245
shiftBits);
246+
}
212247

213248
} else {
214249
assert(!"Requested bit-depth is not supported!");

0 commit comments

Comments
 (0)