-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add calculations for YUV / planar images #479
Open
anishmgoyal
wants to merge
5
commits into
google:main
Choose a base branch
from
anishmgoyal:addYUVPlanarCalculations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,16 +12,20 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "ppx/graphics_util.h" | ||
|
||
#include <algorithm> | ||
|
||
#include "ppx/generate_mip_shader_VK.h" | ||
#include "ppx/generate_mip_shader_DX.h" | ||
#include "ppx/graphics_util.h" | ||
#include "ppx/bitmap.h" | ||
#include "ppx/fs.h" | ||
#include "ppx/mipmap.h" | ||
#include "ppx/timer.h" | ||
#include "ppx/grfx/grfx_buffer.h" | ||
#include "ppx/grfx/grfx_command.h" | ||
#include "ppx/grfx/grfx_device.h" | ||
#include "ppx/grfx/grfx_format.h" | ||
#include "ppx/grfx/grfx_image.h" | ||
#include "ppx/grfx/grfx_queue.h" | ||
#include "ppx/grfx/grfx_util.h" | ||
|
@@ -31,6 +35,175 @@ | |
namespace ppx { | ||
namespace grfx_util { | ||
|
||
namespace { | ||
|
||
// Start planar image helper functions | ||
Keenuts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Gets the height of a single plane, in terms of number of pixels represented. | ||
// This doesn't directly correlate to the number of bits / bytes for the plane's | ||
// height. The value returned can be used in a copy-image-to-buffer command. | ||
// plane: The plane to get the height for (containing information about the | ||
// color components represented in the plane). | ||
// subsampling: The type of subsampling applied to chroma values for the image | ||
// (e.g. 444, 422, 420). | ||
// imageHeight: The height of the image, in pixels, with no subsampling applied. | ||
uint32_t GetPlaneHeightInPixels( | ||
const grfx::FormatPlaneDesc::Plane& plane, | ||
grfx::FormatChromaSubsampling subsampling, | ||
uint32_t imageHeight) | ||
{ | ||
bool hasColSubsampling = (subsampling == grfx::FORMAT_CHROMA_SUBSAMPLING_420); | ||
Keenuts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bool hasChromaValue = false; | ||
bool hasLumaValue = false; | ||
for (const grfx::FormatPlaneDesc::Member& member : plane.members) { | ||
if (member.type == grfx::FORMAT_PLANE_CHROMA_TYPE_CHROMA) { | ||
hasChromaValue = true; | ||
} | ||
else if (member.type == grfx::FORMAT_PLANE_CHROMA_TYPE_LUMA) { | ||
hasLumaValue = true; | ||
anishmgoyal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
else { | ||
PPX_LOG_WARN("Member " << member.component << "has unknown chroma type."); | ||
} | ||
} | ||
|
||
if (hasColSubsampling && hasChromaValue) { | ||
// Note: you never have subsampling on the height axis of the image in | ||
// a plane if luma values are present, since luma values usually aren't | ||
// subsampled. You might have subsampling on the width axis, but that | ||
// would essentially mean you get two luma values, and one of each | ||
// chroma value, in a block of four. | ||
if (hasLumaValue) { | ||
PPX_LOG_WARN( | ||
"Frame size will be inaccurate, there is vertical subsampling " | ||
"with both chroma and luma values present on a single plane, " | ||
"which is not supported!"); | ||
} | ||
|
||
// If we're subsampling at 4:2:0, the image will have half its height. | ||
return imageHeight / 2; | ||
} | ||
return imageHeight; | ||
Keenuts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Gets the width of a single plane, in terms of number of pixels represented. | ||
// This doesn't directly correlate to the number of bits / bytes for the plane's | ||
// height. The value returned can be used in a copy-image-to-buffer command. | ||
// plane: The plane to get the width for (containing information about the | ||
// color components represented in the plane). | ||
// subsampling: The type of subsampling applied to chroma values for the image | ||
// (e.g. 444, 422, 420). | ||
// imageWidth: The width of the image, in pixels, with no subsampling applied. | ||
uint32_t GetPlaneWidthInPixels( | ||
const grfx::FormatPlaneDesc::Plane& plane, | ||
grfx::FormatChromaSubsampling subsampling, | ||
uint32_t imageWidth) | ||
{ | ||
bool hasRowSubsampling = (subsampling == grfx::FORMAT_CHROMA_SUBSAMPLING_420) || | ||
(subsampling == grfx::FORMAT_CHROMA_SUBSAMPLING_422); | ||
bool hasChromaValue = false; | ||
for (const grfx::FormatPlaneDesc::Member& member : plane.members) { | ||
if (member.type == grfx::FORMAT_PLANE_CHROMA_TYPE_CHROMA) { | ||
hasChromaValue = true; | ||
break; | ||
} | ||
} | ||
Keenuts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (hasRowSubsampling && hasChromaValue) { | ||
// Note: even if the layer has a luma value, generally, in the case of | ||
// buffer copies, the width is treated as a half width, if we're | ||
// subsampling at 4:2:0 or 4:2:2, and are looking at a plane with chroma | ||
// values. | ||
return imageWidth / 2; | ||
} | ||
return imageWidth; | ||
Keenuts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Gets the size of an image plane in bytes. | ||
// plane: The plane to get information for. (Contains information about the | ||
// color components represented by this plane, and their bit counts). | ||
// subsampling: The type of chroma subsampling applied to this image (e.g. | ||
// 444, 422, 420). | ||
// width: The width of the image, in pixels, with no subsampling applied. | ||
// height: The height of the image, in pixels, with no subsampling applied. | ||
uint32_t GetPlaneSizeInBytes( | ||
const grfx::FormatPlaneDesc::Plane& plane, | ||
grfx::FormatChromaSubsampling subsampling, | ||
uint32_t width, | ||
uint32_t height) | ||
{ | ||
bool hasColSubsampling = (subsampling == grfx::FORMAT_CHROMA_SUBSAMPLING_420); | ||
bool hasRowSubsampling = hasColSubsampling || (subsampling == grfx::FORMAT_CHROMA_SUBSAMPLING_422); | ||
bool hasChromaValue = false; | ||
bool hasLumaValue = false; | ||
uint32_t rowBitFactor = 0; | ||
for (const grfx::FormatPlaneDesc::Member& member : plane.members) { | ||
if (member.type == grfx::FORMAT_PLANE_CHROMA_TYPE_CHROMA) { | ||
hasChromaValue = true; | ||
} | ||
else if (member.type == grfx::FORMAT_PLANE_CHROMA_TYPE_LUMA) { | ||
hasLumaValue = true; | ||
} | ||
else { | ||
PPX_LOG_WARN("Member " << member.component << "has unknown chroma type."); | ||
} | ||
|
||
// We only subsample chroma values. | ||
if (member.type == grfx::FORMAT_PLANE_CHROMA_TYPE_CHROMA && hasRowSubsampling) { | ||
rowBitFactor += member.bitCount / 2; | ||
} | ||
else { | ||
rowBitFactor += member.bitCount; | ||
} | ||
} | ||
|
||
if (hasColSubsampling && hasChromaValue) { | ||
// Note: you never have subsampling on the height axis of the image in | ||
// a plane if luma values are present, since luma values usually aren't | ||
// subsampled. You might have subsampling on the width axis, but that | ||
// would essentially mean you get two luma values, and one of each | ||
// chroma value, in a block of four. | ||
if (hasLumaValue) { | ||
PPX_LOG_WARN( | ||
"Frame size will be inaccurate, there is vertical subsampling " | ||
"with both chroma and luma values present on a single plane, " | ||
"which is not supported!"); | ||
} | ||
|
||
return (width * rowBitFactor * (height / 2)) / 8; | ||
} | ||
|
||
// No subsampling for height, OR this plane is of luma values (which are | ||
// not subsampled). | ||
return (width * rowBitFactor * height) / 8; | ||
} | ||
|
||
// Gets the total size of a planar image in bytes, by calculating the size of | ||
// each plane individually. | ||
// formatDesc: Information about the image format, such as the components | ||
// represented, etc. | ||
// planeDesc: Information about the components in the current image plane. | ||
// width: The width of the image, in pixels, with no subsampling applied. | ||
// height: The height of the image, in pixels, with no subsampling applied. | ||
uint32_t GetPlanarImageSizeInBytes( | ||
const grfx::FormatDesc& formatDesc, | ||
const grfx::FormatPlaneDesc& planeDesc, | ||
uint32_t width, | ||
uint32_t height) | ||
{ | ||
grfx::FormatChromaSubsampling subsampling = formatDesc.chromaSubsampling; | ||
|
||
uint32_t imageSize = 0; | ||
for (const grfx::FormatPlaneDesc::Plane& plane : planeDesc.planes) { | ||
imageSize += GetPlaneSizeInBytes(plane, subsampling, width, height); | ||
} | ||
return imageSize; | ||
} | ||
|
||
// End planar image helper functions | ||
|
||
} // namespace | ||
|
||
grfx::Format ToGrfxFormat(Bitmap::Format value) | ||
{ | ||
// clang-format off | ||
|
@@ -1510,5 +1683,65 @@ Result CreateMeshFromFile( | |
return ppx::SUCCESS; | ||
} | ||
|
||
// ------------------------------------------------------------------------------------------------- | ||
|
||
Result LoadFramesFromRawVideo( | ||
const std::filesystem::path& path, | ||
grfx::Format format, | ||
uint32_t width, | ||
uint32_t height, | ||
std::vector<std::vector<char>>* pFrames) | ||
{ | ||
PPX_ASSERT_NULL_ARG(pFrames); | ||
|
||
const grfx::FormatDesc* formatDesc = grfx::GetFormatDescription(format); | ||
if (formatDesc == nullptr) { | ||
PPX_LOG_ERROR("Failed to fetch information for texture format " << format); | ||
return ppx::ERROR_FAILED; | ||
} | ||
|
||
uint32_t frameSize = 0; // As measured in bytes, not pixels. | ||
if (formatDesc->isPlanar) { | ||
std::optional<grfx::FormatPlaneDesc> formatPlanes = grfx::GetFormatPlaneDescription(format); | ||
PPX_ASSERT_MSG(formatPlanes.has_value(), "No planes found for format " << format); | ||
frameSize = GetPlanarImageSizeInBytes(*formatDesc, *formatPlanes, width, height); | ||
} | ||
else { | ||
frameSize = formatDesc->bytesPerTexel * width * height; | ||
} | ||
|
||
ppx::fs::File file; | ||
if (!file.Open(path)) { | ||
PPX_ASSERT_MSG(false, "Cannot open the file!"); | ||
return ppx::ERROR_FAILED; | ||
} | ||
const size_t fileSize = file.GetLength(); | ||
|
||
std::vector<char> buffer(frameSize); | ||
size_t totalRead = 0; | ||
while (totalRead < fileSize) { | ||
const size_t bytesRead = file.Read(buffer.data(), frameSize); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this safe to do after a std::move operation? Would buffer have frameSize capacity? |
||
if (bytesRead < frameSize) { | ||
// If we didn't read as many bytes as we expected to, and we haven't | ||
// reached the end of the file, this is an error. | ||
if (totalRead + bytesRead < fileSize) { | ||
PPX_ASSERT_MSG( | ||
false, | ||
"Unable to load video frame; expected " << frameSize << " but read " << bytesRead << "bytes (previously read " << totalRead << ")."); | ||
return ppx::ERROR_FAILED; | ||
} | ||
// Otherwise, fill the rest of the buffer with 0s. | ||
else { | ||
PPX_LOG_WARN("Read " << bytesRead << " bytes for the last frame of the video at " << path << "; filling the rest of the frame with 0s."); | ||
std::fill(buffer.begin() + bytesRead, buffer.end(), 0); | ||
} | ||
} | ||
pFrames->push_back(std::move(buffer)); | ||
totalRead += bytesRead; | ||
} | ||
|
||
return ppx::SUCCESS; | ||
} | ||
|
||
} // namespace grfx_util | ||
} // namespace ppx |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Returns if the operation succeeded"? Did you mean to say "Returns X", cause couldn't it return error code as well?