Skip to content

Commit 14398d2

Browse files
committed
fix: update hdr yuv decode
1 parent 884f85a commit 14398d2

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

iOS/APIExample/APIExample/Common/ExternalVideo/AgoraYUVImageSourcePush.m

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ - (CVPixelBufferRef)yuvToHDRPixelBufferWithWidth:(NSInteger)width height:(NSInte
127127
(NSString *)kCVPixelBufferCGBitmapContextCompatibilityKey: @YES,
128128
(NSString *)kCVPixelBufferWidthKey: @(width),
129129
(NSString *)kCVPixelBufferHeightKey: @(height),
130-
(NSString *)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_420YpCbCr10BiPlanarFullRange),
130+
(NSString *)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange),
131131
(NSString *)kCVPixelBufferMetalCompatibilityKey: @YES,
132132
(NSString *)kCVPixelBufferPoolAllocationThresholdKey: @1
133133
};
@@ -136,7 +136,7 @@ - (CVPixelBufferRef)yuvToHDRPixelBufferWithWidth:(NSInteger)width height:(NSInte
136136
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault,
137137
width,
138138
height,
139-
kCVPixelFormatType_420YpCbCr10BiPlanarFullRange,
139+
kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange,
140140
(__bridge CFDictionaryRef)pixelBufferAttributes,
141141
&pixelBuffer);
142142

@@ -197,7 +197,7 @@ - (CVPixelBufferRef)yuvToHDRPixelBufferWithWidth:(NSInteger)width height:(NSInte
197197
free(uPlane);
198198
free(vPlane);
199199
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
200-
CVPixelBufferRelease(pixelBuffer);
200+
CVPixelBufferRelease(pixelBuffer);
201201
return NULL;
202202
}
203203

@@ -212,7 +212,8 @@ - (CVPixelBufferRef)yuvToHDRPixelBufferWithWidth:(NSInteger)width height:(NSInte
212212

213213
// Normalize Y data to [0, 65535]
214214
for (size_t i = 0; i < width * height; i++) {
215-
yBuffer[i] = (uint16_t)(yBuffer[i] * (65535.0 / 1023.0)); // Normalize Y component
215+
// yBuffer[i] = (uint16_t)(yBuffer[i] * (65535.0 / 1023.0)); // Full Range
216+
yBuffer[i] = (uint16_t)((yBuffer[i] - 64) * (65535.0 / (940 - 64))); // Video Range
216217
}
217218

218219
// Process U and V data
@@ -225,11 +226,13 @@ - (CVPixelBufferRef)yuvToHDRPixelBufferWithWidth:(NSInteger)width height:(NSInte
225226

226227
// Normalize U component
227228
uint16_t uValue = uPlane[i]; // U
228-
uValue = (uint16_t)((uValue - 512) * (65535.0 / 511.0) + 32768);
229+
// uValue = (uint16_t)((uValue - 512) * (65535.0 / 511.0) + 32768);
230+
uValue = (uint16_t)((uValue - 64) * (65535.0 / (960 - 64)));
229231

230232
// Normalize V component
231233
uint16_t vValue = vPlane[i]; // V
232-
vValue = (uint16_t)((vValue - 512) * (65535.0 / 511.0) + 32768);
234+
// vValue = (uint16_t)((vValue - 512) * (65535.0 / 511.0) + 32768);
235+
vValue = (uint16_t)((vValue - 64) * (65535.0 / (960 - 64)));
233236

234237
// Write back normalized values
235238
uvPlane[i * 2] = uValue;

macOS/APIExample/Common/ExternalVideo/AgoraYUVImageSourcePush.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ - (CVPixelBufferRef)yuvToHDRPixelBufferWithWidth:(NSInteger)width height:(NSInte
211211

212212
// Normalize Y data to [0, 65535]
213213
for (size_t i = 0; i < width * height; i++) {
214-
yBuffer[i] = (uint16_t)(yBuffer[i] * (65535.0 / 1023.0)); // Normalize Y component
214+
// yBuffer[i] = (uint16_t)(yBuffer[i] * (65535.0 / 1023.0)); // Full Range
215+
yBuffer[i] = (uint16_t)((yBuffer[i] - 64) * (65535.0 / (940 - 64))); // Video Range
215216
}
216217

217218
// Process U and V data
@@ -224,11 +225,13 @@ - (CVPixelBufferRef)yuvToHDRPixelBufferWithWidth:(NSInteger)width height:(NSInte
224225

225226
// Normalize U component
226227
uint16_t uValue = uPlane[i]; // U
227-
uValue = (uint16_t)((uValue - 512) * (65535.0 / 511.0) + 32768);
228+
// uValue = (uint16_t)((uValue - 512) * (65535.0 / 511.0) + 32768);
229+
uValue = (uint16_t)((uValue - 64) * (65535.0 / (960 - 64)));
228230

229231
// Normalize V component
230232
uint16_t vValue = vPlane[i]; // V
231-
vValue = (uint16_t)((vValue - 512) * (65535.0 / 511.0) + 32768);
233+
// vValue = (uint16_t)((vValue - 512) * (65535.0 / 511.0) + 32768);
234+
vValue = (uint16_t)((vValue - 64) * (65535.0 / (960 - 64)));
232235

233236
// Write back normalized values
234237
uvPlane[i * 2] = uValue;

0 commit comments

Comments
 (0)