Commit bde9a576 authored by Dean Liao's avatar Dean Liao Committed by Commit Bot

media/base: Add FourccToString().

In Chrome media codebase, it sometimes outputs fourcc value to log.
However, we need to table lookup to know which format it refers to.
If we convert the 32bit value into four character code (what fourcc
stands for), it is easier to get what the format is.

BUG=chromium:882339
TEST=Build and run media_unittests --gtest_filter=VideoTypesTest*

Change-Id: I806699ff93646640b69d328e2dffae0ba4e84067
Reviewed-on: https://chromium-review.googlesource.com/1216763
Commit-Queue: Shuo-Peng Liao <deanliao@google.com>
Reviewed-by: default avatarDale Curtis <dalecurtis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#590571}
parent af3570fe
......@@ -523,6 +523,7 @@ source_set("unit_tests") {
"video_frame_layout_unittest.cc",
"video_frame_pool_unittest.cc",
"video_frame_unittest.cc",
"video_types_unittest.cc",
"video_util_unittest.cc",
"wall_clock_time_source_unittest.cc",
]
......
......@@ -5,6 +5,7 @@
#include "media/base/video_types.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
namespace media {
......@@ -67,6 +68,17 @@ std::string VideoPixelFormatToString(VideoPixelFormat format) {
return "";
}
std::string FourccToString(uint32_t fourcc) {
std::string result = "0000";
for (size_t i = 0; i < 4; ++i, fourcc >>= 8) {
const char c = static_cast<char>(fourcc & 0xFF);
if (c <= 0x1f || c >= 0x7f)
return base::StringPrintf("0x%x", fourcc);
result[i] = c;
}
return result;
}
bool IsYuvPlanar(VideoPixelFormat format) {
switch (format) {
case PIXEL_FORMAT_YV12:
......
......@@ -5,6 +5,7 @@
#ifndef MEDIA_BASE_VIDEO_TYPES_H_
#define MEDIA_BASE_VIDEO_TYPES_H_
#include <stdint.h>
#include <string>
#include "build/build_config.h"
......@@ -87,6 +88,12 @@ enum ColorSpace {
// Returns the name of a Format as a string.
MEDIA_EXPORT std::string VideoPixelFormatToString(VideoPixelFormat format);
// Returns human readable fourcc string.
// If any of the four characters is non-printable, it outputs
// "0x<32-bit integer in hex>", e.g. FourccToString(0x66616b00) returns
// "0x66616b00".
MEDIA_EXPORT std::string FourccToString(uint32_t fourcc);
// Returns true if |format| is a YUV format with multiple planes.
MEDIA_EXPORT bool IsYuvPlanar(VideoPixelFormat format);
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/base/video_types.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
TEST(VideoTypesTest, FourccToString) {
// Test fourccc code used in V4L2Device::V4L2PixFmtToVideoPixelFormat().
// V4L2_PIX_FMT_NV12
EXPECT_EQ("NV12", FourccToString(0x3231564e));
// V4L2_PIX_FMT_NV12M
EXPECT_EQ("NM12", FourccToString(0x32314d4e));
// V4L2_PIX_FMT_MT21
EXPECT_EQ("MT21", FourccToString(0x3132544d));
// V4L2_PIX_FMT_YUV420
EXPECT_EQ("YU12", FourccToString(0x32315559));
// V4L2_PIX_FMT_YUV420M
EXPECT_EQ("YM12", FourccToString(0x32314d59));
// V4L2_PIX_FMT_YVU420
EXPECT_EQ("YV12", FourccToString(0x32315659));
// V4L2_PIX_FMT_YUV422M
EXPECT_EQ("YM16", FourccToString(0x36314d59));
// V4L2_PIX_FMT_RGB32
EXPECT_EQ("RGB4", FourccToString(0x34424752));
}
TEST(VideoTypesTest, FourccToStringHasUnprintableChar) {
EXPECT_EQ("0x66616b00", FourccToString(0x66616b00));
}
} // namespace media
......@@ -15,9 +15,9 @@
#include "base/bind.h"
#include "base/posix/eintr_wrapper.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "media/base/bind_to_current_loop.h"
#include "media/base/video_types.h"
#include "media/capture/video/blob_utils.h"
#include "media/capture/video/linux/video_capture_device_linux.h"
......@@ -119,12 +119,6 @@ void FillV4L2RequestBuffer(v4l2_requestbuffers* request_buffer, int count) {
request_buffer->count = count;
}
// Returns the input |fourcc| as a std::string four char representation.
std::string FourccToString(uint32_t fourcc) {
return base::StringPrintf("%c%c%c%c", fourcc & 0xFF, (fourcc >> 8) & 0xFF,
(fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
}
// Determines if |control_id| is special, i.e. controls another one's state.
bool IsSpecialControl(int control_id) {
switch (control_id) {
......
......@@ -24,6 +24,7 @@
#include "base/strings/stringprintf.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "media/base/video_types.h"
#include "media/gpu/buildflags.h"
#include "media/gpu/v4l2/generic_v4l2_device.h"
#include "ui/gfx/native_pixmap.h"
......@@ -154,7 +155,7 @@ bool GenericV4L2Device::Open(Type type, uint32_t v4l2_pixfmt) {
std::string path = GetDevicePathFor(type, v4l2_pixfmt);
if (path.empty()) {
VLOGF(1) << "No devices supporting " << std::hex << "0x" << v4l2_pixfmt
VLOGF(1) << "No devices supporting " << FourccToString(v4l2_pixfmt)
<< " for type: " << static_cast<int>(type);
return false;
}
......
......@@ -8,6 +8,7 @@
#include "base/numerics/safe_conversions.h"
#include "build/build_config.h"
#include "media/base/video_types.h"
#include "media/gpu/v4l2/generic_v4l2_device.h"
#if defined(ARCH_CPU_ARMEL)
#include "media/gpu/v4l2/tegra_v4l2_device.h"
......@@ -189,7 +190,7 @@ std::vector<VideoCodecProfile> V4L2Device::V4L2PixFmtToVideoCodecProfiles(
}
default:
VLOGF(1) << "Unhandled pixelformat " << std::hex << "0x" << pix_fmt;
VLOGF(1) << "Unhandled pixelformat " << FourccToString(pix_fmt);
return profiles;
}
......@@ -220,7 +221,7 @@ uint32_t V4L2Device::V4L2PixFmtToDrmFormat(uint32_t format) {
return DRM_FORMAT_MT21;
default:
DVLOGF(1) << "Unrecognized format " << std::hex << "0x" << format;
DVLOGF(1) << "Unrecognized format " << FourccToString(format);
return 0;
}
}
......@@ -415,13 +416,13 @@ void V4L2Device::GetSupportedResolution(uint32_t pixelformat,
if (max_resolution->IsEmpty()) {
max_resolution->SetSize(1920, 1088);
VLOGF(1) << "GetSupportedResolution failed to get maximum resolution for "
<< "fourcc " << std::hex << pixelformat << ", fall back to "
<< "fourcc " << FourccToString(pixelformat) << ", fall back to "
<< max_resolution->ToString();
}
if (min_resolution->IsEmpty()) {
min_resolution->SetSize(16, 16);
VLOGF(1) << "GetSupportedResolution failed to get minimum resolution for "
<< "fourcc " << std::hex << pixelformat << ", fall back to "
<< "fourcc " << FourccToString(pixelformat) << ", fall back to "
<< min_resolution->ToString();
}
}
......
......@@ -18,6 +18,7 @@
#include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "media/base/scopedfd_helper.h"
#include "media/base/video_types.h"
#include "media/gpu/v4l2/v4l2_image_processor.h"
#define DVLOGF(level) DVLOG(level) << __func__ << "(): "
......@@ -150,7 +151,7 @@ bool V4L2ImageProcessor::Initialize(VideoPixelFormat input_format,
if (!device_->Open(V4L2Device::Type::kImageProcessor, input_format_fourcc_)) {
VLOGF(1) << "Failed to open device for input format: "
<< VideoPixelFormatToString(input_format)
<< " fourcc: " << std::hex << "0x" << input_format_fourcc_;
<< " fourcc: " << FourccToString(input_format_fourcc_);
return false;
}
......
......@@ -30,6 +30,7 @@
#include "media/base/bind_to_current_loop.h"
#include "media/base/media_switches.h"
#include "media/base/unaligned_shared_memory.h"
#include "media/base/video_types.h"
#include "ui/gl/gl_context.h"
#include "ui/gl/gl_image.h"
#include "ui/gl/scoped_binders.h"
......@@ -534,7 +535,7 @@ bool V4L2SliceVideoDecodeAccelerator::Initialize(const Config& config,
if (!device_->Open(V4L2Device::Type::kDecoder, input_format_fourcc_)) {
VLOGF(1) << "Failed to open device for profile: " << config.profile
<< " fourcc: " << std::hex << "0x" << input_format_fourcc_;
<< " fourcc: " << FourccToString(input_format_fourcc_);
return false;
}
......
......@@ -26,6 +26,7 @@
#include "media/base/media_switches.h"
#include "media/base/scopedfd_helper.h"
#include "media/base/unaligned_shared_memory.h"
#include "media/base/video_types.h"
#include "media/gpu/v4l2/v4l2_image_processor.h"
#include "media/video/h264_parser.h"
#include "ui/gfx/geometry/rect.h"
......@@ -241,7 +242,7 @@ bool V4L2VideoDecodeAccelerator::Initialize(const Config& config,
if (!device_->Open(V4L2Device::Type::kDecoder, input_format_fourcc_)) {
VLOGF(1) << "Failed to open device for profile: " << config.profile
<< " fourcc: " << std::hex << "0x" << input_format_fourcc_;
<< " fourcc: " << FourccToString(input_format_fourcc_);
return false;
}
......
......@@ -25,6 +25,7 @@
#include "media/base/bitstream_buffer.h"
#include "media/base/scopedfd_helper.h"
#include "media/base/unaligned_shared_memory.h"
#include "media/base/video_types.h"
#include "media/gpu/v4l2/v4l2_image_processor.h"
#include "media/video/h264_parser.h"
......@@ -172,8 +173,8 @@ bool V4L2VideoEncodeAccelerator::Initialize(const Config& config,
if (!device_->Open(V4L2Device::Type::kEncoder, output_format_fourcc_)) {
VLOGF(1) << "Failed to open device for profile="
<< GetProfileName(config.output_profile) << ", fourcc=0x"
<< std::hex << output_format_fourcc_;
<< GetProfileName(config.output_profile)
<< ", fourcc=" << FourccToString(output_format_fourcc_);
return false;
}
......@@ -1104,7 +1105,8 @@ bool V4L2VideoEncodeAccelerator::NegotiateInputFormat(
input_format =
V4L2Device::V4L2PixFmtToVideoPixelFormat(input_format_fourcc);
if (input_format == PIXEL_FORMAT_UNKNOWN) {
VLOGF(1) << "Unsupported input format" << input_format_fourcc;
VLOGF(1) << "Unsupported input format: "
<< FourccToString(input_format_fourcc);
return false;
}
......
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