7#include <packager/media/codecs/h26x_byte_to_unit_stream_converter.h>
15#include <absl/flags/flag.h>
16#include <absl/log/check.h>
17#include <absl/log/log.h>
18#include <absl/strings/escaping.h>
20#include <packager/media/base/buffer_writer.h>
21#include <packager/media/base/video_stream_info.h>
22#include <packager/media/codecs/nalu_reader.h>
23#include <packager/utils/bytes_to_string_view.h>
28 strip_parameter_set_nalus,
30 "When converting from NAL byte stream (AnnexB stream) to NAL unit "
31 "stream, this flag determines whether to strip parameter sets NAL "
32 "units, i.e. SPS/PPS for H264 and SPS/PPS/VPS for H265, from the "
33 "frames. Note that avc1/hvc1 is generated if this flag is enabled; "
34 "otherwise avc3/hev1 is generated.");
42const size_t kStreamConversionOverhead = 100;
49 absl::GetFlag(FLAGS_strip_parameter_set_nalus)
50 ? H26xStreamFormat::kNalUnitStreamWithoutParameterSetNalus
51 : H26xStreamFormat::kNalUnitStreamWithParameterSetNalus) {}
55 H26xStreamFormat stream_format)
56 : type_(type), stream_format_(stream_format) {}
58H26xByteToUnitStreamConverter::~H26xByteToUnitStreamConverter() {}
61 const uint8_t* input_frame,
62 size_t input_frame_size,
63 std::vector<uint8_t>* output_frame) {
67 BufferWriter output_buffer(input_frame_size + kStreamConversionOverhead);
70 NaluReader reader(type_, kIsAnnexbByteStream, input_frame, input_frame_size);
72 LOG(ERROR) <<
"H.26x byte stream frame did not begin with start code.";
76 while (reader.
Advance(&nalu) == NaluReader::kOk) {
78 DCHECK_LE(nalu_size, std::numeric_limits<uint32_t>::max());
80 if (ProcessNalu(nalu))
84 output_buffer.
AppendInt(
static_cast<uint32_t
>(nalu_size));
85 output_buffer.AppendArray(nalu.
data(), nalu_size);
88 output_buffer.SwapBuffer(output_frame);
92void H26xByteToUnitStreamConverter::WarnIfNotMatch(
94 const uint8_t* nalu_ptr,
96 const std::vector<uint8_t>& vector) {
99 if (vector.size() != nalu_size ||
100 memcmp(vector.data(), nalu_ptr, nalu_size) != 0) {
101 LOG(WARNING) <<
"Seeing varying NAL unit of type " << nalu_type
102 <<
". You may need to set --strip_parameter_set_nalus=false "
103 "during packaging to generate a playable stream.";
107 << 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.