Reland "Parse encoded frame QP if not provided by encoder"

This reverts commit 727d2afc4330efebc904e0e4f366e885d7b08787.

Reason for revert: Use thread-safe wrapper for H264 parser.

Original change's description:
> Revert "Parse encoded frame QP if not provided by encoder"
>
> This reverts commit 8639673f0c098efc294a7593fa3bd98e28ab7508.
>
> Reason for revert: linux_tsan fails https://ci.chromium.org/ui/p/webrtc/builders/ci/Linux%20Tsan%20v2/25329/overview
>
> Original change's description:
> > Parse encoded frame QP if not provided by encoder
> >
> > Bug: webrtc:12542
> > Change-Id: Ic70b46e226f158db7a478a9f20e1f940804febba
> > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/210966
> > Commit-Queue: Sergey Silkin <ssilkin@webrtc.org>
> > Reviewed-by: Åsa Persson <asapersson@webrtc.org>
> > Cr-Commit-Position: refs/heads/master@{#33434}
>
> TBR=asapersson@webrtc.org,ssilkin@webrtc.org
>
> Change-Id: Ie251d8f70f8e87fd86b63730aefd2ef3f941e4bb
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: webrtc:12542
> Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/211355
> Reviewed-by: Sergey Silkin <ssilkin@webrtc.org>
> Commit-Queue: Sergey Silkin <ssilkin@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#33441}

# Not skipping CQ checks because this is a reland.

Bug: webrtc:12542
Change-Id: Ib7601fd6f2f26bceddbea2b4ba54d67a281f3a59
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/211660
Reviewed-by: Sergey Silkin <ssilkin@webrtc.org>
Reviewed-by: Åsa Persson <asapersson@webrtc.org>
Commit-Queue: Sergey Silkin <ssilkin@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#33458}
This commit is contained in:
Sergey Silkin
2021-03-15 10:12:57 +01:00
committed by Commit Bot
parent b6bc357a1e
commit 0e42cf703b
7 changed files with 318 additions and 10 deletions

View File

@ -323,6 +323,8 @@ rtc_library("video_coding_utility") {
"utility/ivf_file_reader.h",
"utility/ivf_file_writer.cc",
"utility/ivf_file_writer.h",
"utility/qp_parser.cc",
"utility/qp_parser.h",
"utility/quality_scaler.cc",
"utility/quality_scaler.h",
"utility/simulcast_rate_allocator.cc",
@ -344,6 +346,7 @@ rtc_library("video_coding_utility") {
"../../api/video:video_adaptation",
"../../api/video:video_bitrate_allocation",
"../../api/video:video_bitrate_allocator",
"../../api/video:video_codec_constants",
"../../api/video:video_frame",
"../../api/video_codecs:video_codecs_api",
"../../common_video",
@ -357,6 +360,7 @@ rtc_library("video_coding_utility") {
"../../rtc_base/experiments:quality_scaling_experiment",
"../../rtc_base/experiments:rate_control_settings",
"../../rtc_base/experiments:stable_target_rate_experiment",
"../../rtc_base/synchronization:mutex",
"../../rtc_base/system:arch",
"../../rtc_base/system:file_wrapper",
"../../rtc_base/system:no_unique_address",
@ -969,6 +973,7 @@ if (rtc_include_tests) {
"utility/framerate_controller_unittest.cc",
"utility/ivf_file_reader_unittest.cc",
"utility/ivf_file_writer_unittest.cc",
"utility/qp_parser_unittest.cc",
"utility/quality_scaler_unittest.cc",
"utility/simulcast_rate_allocator_unittest.cc",
"video_codec_initializer_unittest.cc",

View File

@ -0,0 +1,53 @@
/*
* Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/video_coding/utility/qp_parser.h"
#include "modules/video_coding/utility/vp8_header_parser.h"
#include "modules/video_coding/utility/vp9_uncompressed_header_parser.h"
namespace webrtc {
absl::optional<uint32_t> QpParser::Parse(VideoCodecType codec_type,
size_t spatial_idx,
const uint8_t* frame_data,
size_t frame_size) {
if (frame_data == nullptr || frame_size == 0 ||
spatial_idx >= kMaxSimulcastStreams) {
return absl::nullopt;
}
if (codec_type == kVideoCodecVP8) {
int qp = -1;
if (vp8::GetQp(frame_data, frame_size, &qp)) {
return qp;
}
} else if (codec_type == kVideoCodecVP9) {
int qp = -1;
if (vp9::GetQp(frame_data, frame_size, &qp)) {
return qp;
}
} else if (codec_type == kVideoCodecH264) {
return h264_parsers_[spatial_idx].Parse(frame_data, frame_size);
}
return absl::nullopt;
}
absl::optional<uint32_t> QpParser::H264QpParser::Parse(
const uint8_t* frame_data,
size_t frame_size) {
MutexLock lock(&mutex_);
bitstream_parser_.ParseBitstream(
rtc::ArrayView<const uint8_t>(frame_data, frame_size));
return bitstream_parser_.GetLastSliceQp();
}
} // namespace webrtc

View File

@ -0,0 +1,45 @@
/*
* Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MODULES_VIDEO_CODING_UTILITY_QP_PARSER_H_
#define MODULES_VIDEO_CODING_UTILITY_QP_PARSER_H_
#include "absl/types/optional.h"
#include "api/video/video_codec_constants.h"
#include "api/video/video_codec_type.h"
#include "common_video/h264/h264_bitstream_parser.h"
#include "rtc_base/synchronization/mutex.h"
namespace webrtc {
class QpParser {
public:
absl::optional<uint32_t> Parse(VideoCodecType codec_type,
size_t spatial_idx,
const uint8_t* frame_data,
size_t frame_size);
private:
// A thread safe wrapper for H264 bitstream parser.
class H264QpParser {
public:
absl::optional<uint32_t> Parse(const uint8_t* frame_data,
size_t frame_size);
private:
Mutex mutex_;
H264BitstreamParser bitstream_parser_ RTC_GUARDED_BY(mutex_);
};
H264QpParser h264_parsers_[kMaxSimulcastStreams];
};
} // namespace webrtc
#endif // MODULES_VIDEO_CODING_UTILITY_QP_PARSER_H_

View File

@ -0,0 +1,118 @@
/*
* Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/video_coding/utility/qp_parser.h"
#include <stddef.h>
#include "test/gtest.h"
namespace webrtc {
namespace {
// ffmpeg -s 16x16 -f rawvideo -pix_fmt rgb24 -r 30 -i /dev/zero -c:v libvpx
// -qmin 20 -qmax 20 -crf 20 -frames:v 1 -y out.ivf
const uint8_t kCodedFrameVp8Qp25[] = {
0x10, 0x02, 0x00, 0x9d, 0x01, 0x2a, 0x10, 0x00, 0x10, 0x00,
0x02, 0x47, 0x08, 0x85, 0x85, 0x88, 0x85, 0x84, 0x88, 0x0c,
0x82, 0x00, 0x0c, 0x0d, 0x60, 0x00, 0xfe, 0xfc, 0x5c, 0xd0};
// ffmpeg -s 16x16 -f rawvideo -pix_fmt rgb24 -r 30 -i /dev/zero -c:v libvpx-vp9
// -qmin 24 -qmax 24 -crf 24 -frames:v 1 -y out.ivf
const uint8_t kCodedFrameVp9Qp96[] = {
0xa2, 0x49, 0x83, 0x42, 0xe0, 0x00, 0xf0, 0x00, 0xf6, 0x00,
0x38, 0x24, 0x1c, 0x18, 0xc0, 0x00, 0x00, 0x30, 0x70, 0x00,
0x00, 0x4a, 0xa7, 0xff, 0xfc, 0xb9, 0x01, 0xbf, 0xff, 0xff,
0x97, 0x20, 0xdb, 0xff, 0xff, 0xcb, 0x90, 0x5d, 0x40};
// ffmpeg -s 16x16 -f rawvideo -pix_fmt yuv420p -r 30 -i /dev/zero -c:v libx264
// -qmin 38 -qmax 38 -crf 38 -profile:v baseline -frames:v 2 -y out.264
const uint8_t kCodedFrameH264SpsPpsIdrQp38[] = {
0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0xc0, 0x0a, 0xd9, 0x1e, 0x84,
0x00, 0x00, 0x03, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0xf0, 0x3c,
0x48, 0x99, 0x20, 0x00, 0x00, 0x00, 0x01, 0x68, 0xcb, 0x80, 0xc4,
0xb2, 0x00, 0x00, 0x01, 0x65, 0x88, 0x84, 0xf1, 0x18, 0xa0, 0x00,
0x20, 0x5b, 0x1c, 0x00, 0x04, 0x07, 0xe3, 0x80, 0x00, 0x80, 0xfe};
const uint8_t kCodedFrameH264SpsPpsIdrQp49[] = {
0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0xc0, 0x0a, 0xd9, 0x1e, 0x84,
0x00, 0x00, 0x03, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0xf0, 0x3c,
0x48, 0x99, 0x20, 0x00, 0x00, 0x00, 0x01, 0x68, 0xcb, 0x80, 0x5d,
0x2c, 0x80, 0x00, 0x00, 0x01, 0x65, 0x88, 0x84, 0xf1, 0x18, 0xa0,
0x00, 0x5e, 0x38, 0x00, 0x08, 0x03, 0xc7, 0x00, 0x01, 0x00, 0x7c};
const uint8_t kCodedFrameH264InterSliceQpDelta0[] = {0x00, 0x00, 0x00, 0x01,
0x41, 0x9a, 0x39, 0xea};
} // namespace
TEST(QpParserTest, ParseQpVp8) {
QpParser parser;
absl::optional<uint32_t> qp = parser.Parse(
kVideoCodecVP8, 0, kCodedFrameVp8Qp25, sizeof(kCodedFrameVp8Qp25));
EXPECT_EQ(qp, 25u);
}
TEST(QpParserTest, ParseQpVp9) {
QpParser parser;
absl::optional<uint32_t> qp = parser.Parse(
kVideoCodecVP9, 0, kCodedFrameVp9Qp96, sizeof(kCodedFrameVp9Qp96));
EXPECT_EQ(qp, 96u);
}
TEST(QpParserTest, ParseQpH264) {
QpParser parser;
absl::optional<uint32_t> qp = parser.Parse(
VideoCodecType::kVideoCodecH264, 0, kCodedFrameH264SpsPpsIdrQp38,
sizeof(kCodedFrameH264SpsPpsIdrQp38));
EXPECT_EQ(qp, 38u);
qp = parser.Parse(kVideoCodecH264, 1, kCodedFrameH264SpsPpsIdrQp49,
sizeof(kCodedFrameH264SpsPpsIdrQp49));
EXPECT_EQ(qp, 49u);
qp = parser.Parse(kVideoCodecH264, 0, kCodedFrameH264InterSliceQpDelta0,
sizeof(kCodedFrameH264InterSliceQpDelta0));
EXPECT_EQ(qp, 38u);
qp = parser.Parse(kVideoCodecH264, 1, kCodedFrameH264InterSliceQpDelta0,
sizeof(kCodedFrameH264InterSliceQpDelta0));
EXPECT_EQ(qp, 49u);
}
TEST(QpParserTest, ParseQpUnsupportedCodecType) {
QpParser parser;
absl::optional<uint32_t> qp = parser.Parse(
kVideoCodecGeneric, 0, kCodedFrameVp8Qp25, sizeof(kCodedFrameVp8Qp25));
EXPECT_FALSE(qp.has_value());
}
TEST(QpParserTest, ParseQpNullData) {
QpParser parser;
absl::optional<uint32_t> qp = parser.Parse(kVideoCodecVP8, 0, nullptr, 100);
EXPECT_FALSE(qp.has_value());
}
TEST(QpParserTest, ParseQpEmptyData) {
QpParser parser;
absl::optional<uint32_t> qp =
parser.Parse(kVideoCodecVP8, 0, kCodedFrameVp8Qp25, 0);
EXPECT_FALSE(qp.has_value());
}
TEST(QpParserTest, ParseQpSpatialIdxExceedsMax) {
QpParser parser;
absl::optional<uint32_t> qp =
parser.Parse(kVideoCodecVP8, kMaxSimulcastStreams, kCodedFrameVp8Qp25,
sizeof(kCodedFrameVp8Qp25));
EXPECT_FALSE(qp.has_value());
}
} // namespace webrtc