Skip to content
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

feat: port media/codecs to CMake #1143

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
.settings
build/
/packager/docs/
.vscode/
1 change: 1 addition & 0 deletions packager/media/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ add_subdirectory(base)
add_subdirectory(origin)
add_subdirectory(replicator)
add_subdirectory(test)
add_subdirectory(codecs)
3 changes: 2 additions & 1 deletion packager/media/base/aes_cryptor_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "glog/logging.h"
#include "packager/media/base/aes_decryptor.h"
#include "packager/media/base/aes_encryptor.h"
#include "packager/utils/bytes_to_string_view.h"

namespace {

Expand Down Expand Up @@ -203,7 +204,7 @@ TEST_F(AesCtrEncryptorTest, GenerateRandomIv) {
ASSERT_TRUE(AesCryptor::GenerateRandomIv(FOURCC_cenc, &iv));
ASSERT_EQ(kCencIvSize, iv.size());
LOG(INFO) << "Random IV: "
<< absl::BytesToHexString(std::string(iv.begin(), iv.end()));
<< absl::BytesToHexString(byte_vector_to_string_view(iv));
}

TEST_F(AesCtrEncryptorTest, UnsupportedKeySize) {
Expand Down
2 changes: 1 addition & 1 deletion packager/media/base/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#endif

#ifndef FALLTHROUGH_INTENDED
#define FALLTHROUGH_INTENDED
#define FALLTHROUGH_INTENDED [[fallthrough]]
#endif

#endif // PACKAGER_MEDIA_BASE_MACROS_H_
5 changes: 3 additions & 2 deletions packager/media/base/raw_key_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "glog/logging.h"
#include "packager/media/base/key_source.h"
#include "packager/status/status_macros.h"
#include "packager/utils/bytes_to_string_view.h"

namespace {
const char kEmptyDrmLabel[] = "";
Expand Down Expand Up @@ -56,9 +57,9 @@ Status RawKeySource::GetKey(const std::vector<uint8_t>& key_id,
return Status::OK;
}
}
std::string key_id_str(key_id.begin(), key_id.end());
return Status(error::INTERNAL_ERROR,
"Key for key_id=" + absl::BytesToHexString(key_id_str) +
"Key for key_id=" +
absl::BytesToHexString(byte_vector_to_string_view(key_id)) +
joeyparrish marked this conversation as resolved.
Show resolved Hide resolved
" was not found.");
}

Expand Down
69 changes: 69 additions & 0 deletions packager/media/codecs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Copyright 2022 Google LLC. All rights reserved.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd

add_library(codecs STATIC
aac_audio_specific_config.cc
ac3_audio_util.cc
av1_codec_configuration_record.cc
av1_parser.cc
avc_decoder_configuration_record.cc
decoder_configuration_record.cc
dovi_decoder_configuration_record.cc
ec3_audio_util.cc
ac4_audio_util.cc
es_descriptor.cc
h264_byte_to_unit_stream_converter.cc
h264_parser.cc
h265_byte_to_unit_stream_converter.cc
h265_parser.cc
h26x_bit_reader.cc
h26x_byte_to_unit_stream_converter.cc
hevc_decoder_configuration_record.cc
hls_audio_util.cc
nal_unit_to_byte_stream_converter.cc
nalu_reader.cc
video_slice_header_parser.cc
vp_codec_configuration_record.cc
vp8_parser.cc
vp9_parser.cc
)

target_link_libraries(codecs
media_base)

add_executable(codecs_unittest
aac_audio_specific_config_unittest.cc
ac3_audio_util_unittest.cc
av1_codec_configuration_record_unittest.cc
av1_parser_unittest.cc
avc_decoder_configuration_record_unittest.cc
dovi_decoder_configuration_record_unittest.cc
ec3_audio_util_unittest.cc
ac4_audio_util_unittest.cc
es_descriptor_unittest.cc
h264_byte_to_unit_stream_converter_unittest.cc
h264_parser_unittest.cc
h265_byte_to_unit_stream_converter_unittest.cc
h265_parser_unittest.cc
h26x_bit_reader_unittest.cc
hevc_decoder_configuration_record_unittest.cc
hls_audio_util_unittest.cc
nal_unit_to_byte_stream_converter_unittest.cc
nalu_reader_unittest.cc
video_slice_header_parser_unittest.cc
vp_codec_configuration_record_unittest.cc
vp8_parser_unittest.cc
vp9_parser_unittest.cc
)

target_link_libraries(codecs_unittest
codecs
gmock
gtest
gtest_main
test_data_util)

add_test(NAME codecs_unittest COMMAND codecs_unittest)
10 changes: 5 additions & 5 deletions packager/media/codecs/aac_audio_specific_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <algorithm>

#include "packager/base/logging.h"
#include "glog/logging.h"
#include "packager/media/base/bit_reader.h"
#include "packager/media/base/rcheck.h"

Expand Down Expand Up @@ -70,7 +70,7 @@ bool AACAudioSpecificConfig::Parse(const std::vector<uint8_t>& data) {
RCHECK(reader.ReadBits(24, &frequency_));
RCHECK(reader.ReadBits(4, &channel_config_));

RCHECK(channel_config_ < arraysize(kChannelConfigs));
RCHECK(channel_config_ < std::size(kChannelConfigs));
num_channels_ = kChannelConfigs[channel_config_];

// Read extension configuration.
Expand Down Expand Up @@ -120,15 +120,15 @@ bool AACAudioSpecificConfig::Parse(const std::vector<uint8_t>& data) {
}

if (frequency_ == 0) {
RCHECK(frequency_index_ < arraysize(kSampleRates));
RCHECK(frequency_index_ < std::size(kSampleRates));
frequency_ = kSampleRates[frequency_index_];
}

if (extension_frequency_ == 0 && extension_frequency_index != 0xff) {
RCHECK(extension_frequency_index < arraysize(kSampleRates));
RCHECK(extension_frequency_index < std::size(kSampleRates));
extension_frequency_ = kSampleRates[extension_frequency_index];
}

if (audio_object_type_ == AOT_USAC) {
return frequency_ != 0 && num_channels_ != 0 && channel_config_ <= 7;
} else {
Expand Down
6 changes: 4 additions & 2 deletions packager/media/codecs/ac3_audio_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

#include "packager/media/codecs/ac3_audio_util.h"

#include "packager/base/strings/string_number_conversions.h"
#include "absl/strings/escaping.h"
#include "packager/media/base/bit_reader.h"
#include "packager/media/base/rcheck.h"
#include "packager/utils/bytes_to_string_view.h"

namespace shaka {
namespace media {
Expand Down Expand Up @@ -42,7 +43,8 @@ size_t GetAc3NumChannels(const std::vector<uint8_t>& ac3_data) {
bool lfe_channel_on;
if (!ExtractAc3Data(ac3_data, &audio_coding_mode, &lfe_channel_on)) {
LOG(WARNING) << "Seeing invalid AC3 data: "
<< base::HexEncode(ac3_data.data(), ac3_data.size());
<< absl::BytesToHexString(
byte_vector_to_string_view(ac3_data));
return 0;
}
return kAc3NumChannelsTable[audio_coding_mode] + (lfe_channel_on ? 1 : 0);
Expand Down
18 changes: 12 additions & 6 deletions packager/media/codecs/ac4_audio_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

#include "packager/media/codecs/ac4_audio_util.h"

#include "packager/base/macros.h"
#include "packager/base/strings/string_number_conversions.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_format.h"
#include "packager/macros.h"
#include "packager/media/base/bit_reader.h"
#include "packager/media/base/rcheck.h"
#include "packager/utils/bytes_to_string_view.h"

namespace shaka {
namespace media {
Expand Down Expand Up @@ -442,7 +444,8 @@ bool CalculateAC4ChannelMask(const std::vector<uint8_t>& ac4_data,
&mdcompat, &pre_channel_mask, &dolby_ims_indicator,
&dolby_cbi_indicator)) {
LOG(WARNING) << "Seeing invalid AC4 data: "
<< base::HexEncode(ac4_data.data(), ac4_data.size());
<< absl::BytesToHexString(
byte_vector_to_string_view(ac4_data));
return false;
}

Expand All @@ -467,7 +470,8 @@ bool CalculateAC4ChannelMPEGValue(const std::vector<uint8_t>& ac4_data,
&mdcompat, &pre_channel_mask, &dolby_ims_indicator,
&dolby_cbi_indicator)) {
LOG(WARNING) << "Seeing invalid AC4 data: "
<< base::HexEncode(ac4_data.data(), ac4_data.size());
<< absl::BytesToHexString(
byte_vector_to_string_view(ac4_data));
return false;
}

Expand All @@ -488,7 +492,8 @@ bool GetAc4CodecInfo(const std::vector<uint8_t>& ac4_data,
&mdcompat, &pre_channel_mask, &dolby_ims_indicator,
&dolby_cbi_indicator)) {
LOG(WARNING) << "Seeing invalid AC4 data: "
<< base::HexEncode(ac4_data.data(), ac4_data.size());
<< absl::BytesToHexString(
byte_vector_to_string_view(ac4_data));
return false;
}

Expand Down Expand Up @@ -517,7 +522,8 @@ bool GetAc4ImmersiveInfo(const std::vector<uint8_t>& ac4_data,
&mdcompat, &pre_channel_mask, ac4_ims_flag,
ac4_cbi_flag)) {
LOG(WARNING) << "Seeing invalid AC4 data: "
<< base::HexEncode(ac4_data.data(), ac4_data.size());
<< absl::BytesToHexString(
byte_vector_to_string_view(ac4_data));
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions packager/media/codecs/av1_codec_configuration_record.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "packager/media/codecs/av1_codec_configuration_record.h"

#include "packager/base/strings/stringprintf.h"
#include "absl/strings/str_format.h"
#include "packager/media/base/bit_reader.h"
#include "packager/media/base/rcheck.h"

Expand Down Expand Up @@ -84,8 +84,8 @@ bool AV1CodecConfigurationRecord::Parse(const uint8_t* data, size_t data_size) {
// Since some of the optional fields (e.g. colorPrimaries) are not present in
// AV1CodecConfigurationRecord, we omit all the optional fields.
std::string AV1CodecConfigurationRecord::GetCodecString() const {
return base::StringPrintf("av01.%d.%02d%c.%02d", profile_, level_,
tier_ ? 'H' : 'M', bit_depth_);
return absl::StrFormat("av01.%d.%02d%c.%02d", profile_, level_,
tier_ ? 'H' : 'M', bit_depth_);
}

} // namespace media
Expand Down
6 changes: 3 additions & 3 deletions packager/media/codecs/av1_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <algorithm>

#include "packager/base/logging.h"
#include "glog/logging.h"
#include "packager/media/base/bit_reader.h"
#include "packager/media/base/rcheck.h"

Expand Down Expand Up @@ -1481,7 +1481,7 @@ bool AV1Parser::SkipGlobalMotionParams(bool frame_is_intra,

// 5.9.25. Global param syntax.
bool AV1Parser::SkipGlobalParam(int type,
int ref,
int /*ref*/,
int idx,
bool allow_high_precision_mv,
BitReader* reader) {
Expand Down Expand Up @@ -1810,7 +1810,7 @@ bool AV1Parser::SetFrameRefs(int last_frame_idx, int gold_frame_idx) {
static const int kRefFrameList[] = {
LAST2_FRAME, LAST3_FRAME, BWDREF_FRAME, ALTREF2_FRAME, ALTREF_FRAME,
};
static_assert(arraysize(kRefFrameList) == kRefsPerFrame - 2,
static_assert(std::size(kRefFrameList) == kRefsPerFrame - 2,
"Unexpected kRefFrameList size.");
for (const int ref_frame : kRefFrameList) {
if (frame_header_.ref_frame_idx[ref_frame - LAST_FRAME] < 0) {
Expand Down
9 changes: 6 additions & 3 deletions packager/media/codecs/avc_decoder_configuration_record.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

#include "packager/media/codecs/avc_decoder_configuration_record.h"

#include "packager/base/strings/string_number_conversions.h"
#include "packager/base/strings/string_util.h"
#include "absl/strings/ascii.h"
#include "absl/strings/escaping.h"
#include "packager/macros.h"
#include "packager/media/base/buffer_reader.h"
#include "packager/media/base/rcheck.h"
#include "packager/media/codecs/h264_parser.h"
#include "packager/utils/bytes_to_string_view.h"

namespace shaka {
namespace media {
Expand Down Expand Up @@ -123,7 +125,8 @@ std::string AVCDecoderConfigurationRecord::GetCodecString(
const uint8_t bytes[] = {profile_indication, profile_compatibility,
avc_level};
return FourCCToString(codec_fourcc) + "." +
base::ToLowerASCII(base::HexEncode(bytes, arraysize(bytes)));
absl::AsciiStrToLower(absl::BytesToHexString(
byte_array_to_string_view(bytes, std::size(bytes))));
}

} // namespace media
Expand Down
3 changes: 2 additions & 1 deletion packager/media/codecs/avc_decoder_configuration_record.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
#define PACKAGER_MEDIA_CODECS_AVC_DECODER_CONFIGURATION_RECORD_H_

#include <stdint.h>

#include <string>
#include <vector>

#include "packager/base/macros.h"
#include "packager/macros.h"
#include "packager/media/base/fourccs.h"
#include "packager/media/codecs/decoder_configuration_record.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ TEST(AVCDecoderConfigurationRecordTest, Success) {

AVCDecoderConfigurationRecord avc_config;
ASSERT_TRUE(avc_config.Parse(kAvcDecoderConfigurationData,
arraysize(kAvcDecoderConfigurationData)));
std::size(kAvcDecoderConfigurationData)));

EXPECT_EQ(1u, avc_config.version());
EXPECT_EQ(0x64, avc_config.profile_indication());
Expand Down Expand Up @@ -81,7 +81,7 @@ TEST(AVCDecoderConfigurationRecordTest, SuccessWithSPSExtension) {

AVCDecoderConfigurationRecord avc_config;
ASSERT_TRUE(avc_config.Parse(kAvcDecoderConfigurationData,
arraysize(kAvcDecoderConfigurationData)));
std::size(kAvcDecoderConfigurationData)));

EXPECT_EQ(1u, avc_config.version());
EXPECT_EQ(0x64, avc_config.profile_indication());
Expand Down Expand Up @@ -121,7 +121,7 @@ TEST(AVCDecoderConfigurationRecordTest, SuccessWithTransferCharacteristics) {

AVCDecoderConfigurationRecord avc_config;
ASSERT_TRUE(avc_config.Parse(kAvcDecoderConfigurationData,
arraysize(kAvcDecoderConfigurationData)));
std::size(kAvcDecoderConfigurationData)));

EXPECT_EQ(1u, avc_config.version());
EXPECT_EQ(0x64, avc_config.profile_indication());
Expand Down Expand Up @@ -153,7 +153,7 @@ TEST(AVCDecoderConfigurationRecordTest, SuccessWithNoParameterSets) {

AVCDecoderConfigurationRecord avc_config;
ASSERT_TRUE(avc_config.Parse(kAvcDecoderConfigurationData,
arraysize(kAvcDecoderConfigurationData)));
std::size(kAvcDecoderConfigurationData)));

EXPECT_EQ(1u, avc_config.version());
EXPECT_EQ(0x64, avc_config.profile_indication());
Expand Down Expand Up @@ -191,15 +191,15 @@ TEST(AVCDecoderConfigurationRecordTest, FailsOnInvalidNaluLengthSize) {

AVCDecoderConfigurationRecord avc_config;
ASSERT_FALSE(avc_config.Parse(kAvcDecoderConfigurationData,
arraysize(kAvcDecoderConfigurationData)));
std::size(kAvcDecoderConfigurationData)));
}

TEST(AVCDecoderConfigurationRecordTest, FailOnInsufficientData) {
const uint8_t kAvcDecoderConfigurationData[] = {0x01, 0x64, 0x00, 0x1E};

AVCDecoderConfigurationRecord avc_config;
ASSERT_FALSE(avc_config.Parse(kAvcDecoderConfigurationData,
arraysize(kAvcDecoderConfigurationData)));
std::size(kAvcDecoderConfigurationData)));
}

TEST(AVCDecoderConfigurationRecordTest, GetCodecString) {
Expand Down
Loading