Commit 56345663 authored by Henrik Boström's avatar Henrik Boström Committed by Commit Bot

[macOS] SampleBufferTransformer: Avoid using libyuv::MJPGSize().

I looked at the implementation of libyuv::MJPGSize() and discovered
that part of "loading" the data required validating it, which involves
reading up to 1 kB of bytes looking for an End of Image marker.

Loading and unloading it like this seems like an awful waste if all we
want to do is get the width and height...

Luckily, the CVSampleBuffer contains a format description that has the
dimensions, which I assume means getting them without reading 1 kB of
raw data.

This CL gets the dimensions that way instead of libyuv::MJPGSize().

(Note that libyuv::MJPGSize() is still used for sanity-checking inside
of the unit tests.)

Bug: chromium:1132299
Change-Id: I2c32bc319d3ae61cd6ea863ce59ab38f9fdd9d0d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2517948
Commit-Queue: Henrik Boström <hbos@chromium.org>
Reviewed-by: default avatarIlya Nikolaevskiy <ilnik@chromium.org>
Cr-Commit-Position: refs/heads/master@{#824028}
parent ea62ddbb
......@@ -256,8 +256,7 @@ void ConvertFromMjpegToNV12(uint8_t* source_buffer_data_base_address,
const NV12Planes& destination) {
// Despite libyuv::MJPGToNV12() taking both source and destination sizes as
// arguments, this function is only successful if the sizes match. So here we
// require the destination buffer's size to match the source's, which can be
// obtained using libyuv::MJPGSize().
// require the destination buffer's size to match the source's.
int result = libyuv::MJPGToNV12(
source_buffer_data_base_address, source_buffer_data_size,
destination.y_plane_data, destination.y_plane_stride,
......@@ -528,24 +527,20 @@ void SampleBufferTransformer::TransformSampleBuffer(
CMSampleBufferRef source_sample_buffer,
CVPixelBufferRef destination_pixel_buffer) {
DCHECK(transformer_ == Transformer::kLibyuv);
// Ensure source pixel format is MJPEG.
// Ensure source pixel format is MJPEG and get width and height.
CMFormatDescriptionRef source_format_description =
CMSampleBufferGetFormatDescription(source_sample_buffer);
FourCharCode source_pixel_format =
CMFormatDescriptionGetMediaSubType(source_format_description);
DCHECK(source_pixel_format == kPixelFormatMjpeg);
CMVideoDimensions source_dimensions =
CMVideoFormatDescriptionGetDimensions(source_format_description);
// Access source pixel buffer bytes.
uint8_t* source_buffer_data_base_address;
size_t source_buffer_data_size;
std::tie(source_buffer_data_base_address, source_buffer_data_size) =
GetSampleBufferBaseAddressAndSize(source_sample_buffer);
int mjpg_width;
int mjpg_height;
int result =
libyuv::MJPGSize(source_buffer_data_base_address, source_buffer_data_size,
&mjpg_width, &mjpg_height);
DCHECK(result == 0);
// Lock destination pixel buffer.
CVReturn lock_status =
......@@ -555,13 +550,15 @@ void SampleBufferTransformer::TransformSampleBuffer(
switch (destination_pixel_format_) {
case kPixelFormatI420:
TransformSampleBufferFromMjpegToI420(
source_buffer_data_base_address, source_buffer_data_size, mjpg_width,
mjpg_height, destination_pixel_buffer);
source_buffer_data_base_address, source_buffer_data_size,
source_dimensions.width, source_dimensions.height,
destination_pixel_buffer);
break;
case kPixelFormatNv12:
TransformSampleBufferFromMjpegToNV12(
source_buffer_data_base_address, source_buffer_data_size, mjpg_width,
mjpg_height, destination_pixel_buffer);
source_buffer_data_base_address, source_buffer_data_size,
source_dimensions.width, source_dimensions.height,
destination_pixel_buffer);
break;
default:
NOTREACHED();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment