7 #include <packager/media/codecs/h264_byte_to_unit_stream_converter.h>
11 #include <absl/log/check.h>
12 #include <absl/log/log.h>
14 #include <packager/media/base/buffer_writer.h>
15 #include <packager/media/codecs/h264_parser.h>
22 const H264Sps* ParseSpsFromBytes(
const std::vector<uint8_t> sps,
25 if (!nalu.Initialize(Nalu::kH264, sps.data(), sps.size()))
29 if (parser->ParseSps(nalu, &sps_id) != H264Parser::kOk)
31 return parser->GetSps(sps_id);
39 H26xStreamFormat stream_format)
42 H264ByteToUnitStreamConverter::~H264ByteToUnitStreamConverter() {}
45 std::vector<uint8_t>* decoder_config)
const {
46 DCHECK(decoder_config);
47 if ((last_sps_.size() < 4) || last_pps_.empty()) {
60 uint8_t reserved_and_length_size_minus_one(0xff);
61 buffer.
AppendInt(reserved_and_length_size_minus_one);
62 uint8_t reserved_and_num_sps(0xe1);
64 buffer.
AppendInt(
static_cast<uint16_t
>(last_sps_.size()));
65 buffer.AppendVector(last_sps_);
68 buffer.
AppendInt(
static_cast<uint16_t
>(last_pps_.size()));
69 buffer.AppendVector(last_pps_);
71 uint8_t profile_indication = last_sps_[1];
72 if (profile_indication == 100 || profile_indication == 110 ||
73 profile_indication == 122 || profile_indication == 144) {
76 const H264Sps* sps = ParseSpsFromBytes(last_sps_, &parser);
80 uint8_t reserved_chroma_format = 0xfc | (sps->chroma_format_idc);
82 uint8_t reserved_bit_depth_luma_minus8 = 0xf8 | sps->bit_depth_luma_minus8;
83 buffer.
AppendInt(reserved_bit_depth_luma_minus8);
84 uint8_t reserved_bit_depth_chroma_minus8 = 0xf8 | sps->bit_depth_chroma_minus8;
85 buffer.
AppendInt(reserved_bit_depth_chroma_minus8);
87 if (last_sps_ext_.empty()) {
88 uint8_t num_sps_ext(0);
91 uint8_t num_sps_ext(1);
93 buffer.AppendVector(last_sps_ext_);
97 buffer.SwapBuffer(decoder_config);
101 bool H264ByteToUnitStreamConverter::ProcessNalu(
const Nalu& nalu) {
105 const uint8_t* nalu_ptr = nalu.
data();
108 switch (nalu.
type()) {
110 if (strip_parameter_set_nalus())
111 WarnIfNotMatch(nalu.
type(), nalu_ptr, nalu_size, last_sps_);
113 last_sps_.assign(nalu_ptr, nalu_ptr + nalu_size);
114 return strip_parameter_set_nalus();
115 case Nalu::H264_SPSExtension:
116 if (strip_parameter_set_nalus())
117 WarnIfNotMatch(nalu.
type(), nalu_ptr, nalu_size, last_sps_ext_);
119 last_sps_ext_.assign(nalu_ptr, nalu_ptr + nalu_size);
120 return strip_parameter_set_nalus();
122 if (strip_parameter_set_nalus())
123 WarnIfNotMatch(nalu.
type(), nalu_ptr, nalu_size, last_pps_);
125 last_pps_.assign(nalu_ptr, nalu_ptr + nalu_size);
126 return strip_parameter_set_nalus();
All the methods that are virtual are virtual for mocking.