7 #include <packager/media/codecs/h26x_byte_to_unit_stream_converter.h>
11 #include <absl/flags/flag.h>
12 #include <absl/log/check.h>
13 #include <absl/log/log.h>
14 #include <absl/strings/escaping.h>
16 #include <packager/macros/logging.h>
17 #include <packager/media/base/buffer_writer.h>
18 #include <packager/utils/bytes_to_string_view.h>
23 strip_parameter_set_nalus,
25 "When converting from NAL byte stream (AnnexB stream) to NAL unit "
26 "stream, this flag determines whether to strip parameter sets NAL "
27 "units, i.e. SPS/PPS for H264 and SPS/PPS/VPS for H265, from the "
28 "frames. Note that avc1/hvc1 is generated if this flag is enabled; "
29 "otherwise avc3/hev1 is generated.");
37 const size_t kStreamConversionOverhead = 100;
44 absl::GetFlag(FLAGS_strip_parameter_set_nalus)
45 ? H26xStreamFormat::kNalUnitStreamWithoutParameterSetNalus
46 : H26xStreamFormat::kNalUnitStreamWithParameterSetNalus) {}
50 H26xStreamFormat stream_format)
51 : type_(type), stream_format_(stream_format) {}
53 H26xByteToUnitStreamConverter::~H26xByteToUnitStreamConverter() {}
56 const uint8_t* input_frame,
57 size_t input_frame_size,
58 std::vector<uint8_t>* output_frame) {
62 BufferWriter output_buffer(input_frame_size + kStreamConversionOverhead);
65 NaluReader reader(type_, kIsAnnexbByteStream, input_frame, input_frame_size);
67 LOG(ERROR) <<
"H.26x byte stream frame did not begin with start code.";
71 while (reader.
Advance(&nalu) == NaluReader::kOk) {
73 DCHECK_LE(nalu_size, std::numeric_limits<uint32_t>::max());
75 if (ProcessNalu(nalu))
79 output_buffer.
AppendInt(
static_cast<uint32_t
>(nalu_size));
80 output_buffer.AppendArray(nalu.
data(), nalu_size);
83 output_buffer.SwapBuffer(output_frame);
87 void H26xByteToUnitStreamConverter::WarnIfNotMatch(
89 const uint8_t* nalu_ptr,
91 const std::vector<uint8_t>& vector) {
94 if (vector.size() != nalu_size ||
95 memcmp(vector.data(), nalu_ptr, nalu_size) != 0) {
96 LOG(WARNING) <<
"Seeing varying NAL unit of type " << nalu_type
97 <<
". You may need to set --strip_parameter_set_nalus=false "
98 "during packaging to generate a playable stream.";
102 << absl::BytesToHexString(
All the methods that are virtual are virtual for mocking.
std::string_view byte_vector_to_string_view(const std::vector< uint8_t > &bytes)
Convert byte vector to string_view.
std::string_view byte_array_to_string_view(const uint8_t *bytes, size_t bytes_size)
Convert byte array to string_view.