Shaka Packager SDK
Loading...
Searching...
No Matches
video_slice_header_parser.cc
1// Copyright 2016 Google LLC. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file or at
5// https://developers.google.com/open-source/licenses/bsd
6
7#include <packager/media/codecs/video_slice_header_parser.h>
8
9#include <absl/log/check.h>
10
11#include <packager/macros/logging.h>
12#include <packager/media/base/rcheck.h>
13#include <packager/media/codecs/avc_decoder_configuration_record.h>
14#include <packager/media/codecs/hevc_decoder_configuration_record.h>
15
16namespace shaka {
17namespace media {
18
19namespace {
20
21size_t NumBitsToNumBytes(size_t size_in_bits) {
22 // Round-up division.
23 return (size_in_bits + 7) >> 3;
24}
25
26} // namespace
27
28H264VideoSliceHeaderParser::H264VideoSliceHeaderParser() {}
29H264VideoSliceHeaderParser::~H264VideoSliceHeaderParser() {}
30
32 const std::vector<uint8_t>& decoder_configuration) {
34 RCHECK(config.Parse(decoder_configuration));
35
36 for (size_t i = 0; i < config.nalu_count(); i++) {
37 int id;
38 const Nalu& nalu = config.nalu(i);
39 if (nalu.type() == Nalu::H264_SPS) {
40 RCHECK(parser_.ParseSps(nalu, &id) == H264Parser::kOk);
41 } else if (nalu.type() == Nalu::H264_PPS) {
42 RCHECK(parser_.ParsePps(nalu, &id) == H264Parser::kOk);
43 }
44 }
45
46 return true;
47}
48
50 const std::vector<uint8_t>& layered_decoder_configuration) {
51 return true;
52}
53
55 int id;
56 switch (nalu.type()) {
57 case Nalu::H264_SPS:
58 return parser_.ParseSps(nalu, &id) == H264Parser::kOk;
59 case Nalu::H264_PPS:
60 return parser_.ParsePps(nalu, &id) == H264Parser::kOk;
61 default:
62 return true;
63 }
64}
65
67 DCHECK(nalu.is_video_slice());
68 H264SliceHeader slice_header;
69 if (parser_.ParseSliceHeader(nalu, &slice_header) != H264Parser::kOk)
70 return -1;
71
72 return NumBitsToNumBytes(slice_header.header_bit_size);
73}
74
75H265VideoSliceHeaderParser::H265VideoSliceHeaderParser() {}
76H265VideoSliceHeaderParser::~H265VideoSliceHeaderParser() {}
77
78bool H265VideoSliceHeaderParser::ParseParameterSets(
79 const HEVCDecoderConfigurationRecord& config) {
80 int id;
81 for (size_t i = 0; i < config.nalu_count(); i++) {
82 const Nalu& nalu = config.nalu(i);
83 if (nalu.type() == Nalu::H265_SPS) {
84 RCHECK(parser_.ParseSps(nalu, &id) == H265Parser::kOk);
85 } else if (nalu.type() == Nalu::H265_PPS) {
86 RCHECK(parser_.ParsePps(nalu, &id) == H265Parser::kOk);
87 } else if (nalu.type() == Nalu::H265_VPS) {
88 RCHECK(parser_.ParseVps(nalu, &id) == H265Parser::kOk);
89 } else {
90 VLOG(1) << "Ignoring decoder configuration Nalu of unknown type "
91 << nalu.type();
92 }
93 }
94
95 return true;
96}
97
99 const std::vector<uint8_t>& decoder_configuration) {
101 RCHECK(hevc_config.Parse(decoder_configuration));
102 return ParseParameterSets(hevc_config);
103}
104
106 const std::vector<uint8_t>& layered_decoder_configuration) {
107 if (layered_decoder_configuration.size() > 0) {
109 lhevc_config.SetParser(&parser_);
110 RCHECK(lhevc_config.ParseLHEVCConfig(layered_decoder_configuration));
111 return ParseParameterSets(lhevc_config);
112 } else {
113 return true;
114 }
115}
116
118 int id;
119 switch (nalu.type()) {
120 case Nalu::H265_SPS:
121 return parser_.ParseSps(nalu, &id) == H265Parser::kOk;
122 case Nalu::H265_PPS:
123 return parser_.ParsePps(nalu, &id) == H265Parser::kOk;
124 case Nalu::H265_VPS:
125 return parser_.ParseVps(nalu, &id) == H265Parser::kOk;
126 default:
127 return true;
128 }
129}
130
132 DCHECK(nalu.is_video_slice());
133 H265SliceHeader slice_header;
134 if (parser_.ParseSliceHeader(nalu, &slice_header) != H265Parser::kOk)
135 return -1;
136
137 return NumBitsToNumBytes(slice_header.header_bit_size);
138}
139
140} // namespace media
141} // namespace shaka
142
Class for parsing AVC decoder configuration record.
bool Parse(const std::vector< uint8_t > &data)
bool Initialize(const std::vector< uint8_t > &decoder_configuration) override
int64_t GetHeaderSize(const Nalu &nalu) override
Gets the header size of the given NALU. Returns < 0 on error.
bool InitializeLayered(const std::vector< uint8_t > &decoder_configuration) override
Result ParseSps(const Nalu &nalu, int *sps_id)
Result ParsePps(const Nalu &nalu, int *pps_id)
Result ParseVps(const Nalu &nalu, int *vps_id)
Result ParseSliceHeader(const Nalu &nalu, H265SliceHeader *slice_header)
int64_t GetHeaderSize(const Nalu &nalu) override
Gets the header size of the given NALU. Returns < 0 on error.
bool InitializeLayered(const std::vector< uint8_t > &decoder_configuration) override
bool Initialize(const std::vector< uint8_t > &decoder_configuration) override
Class for parsing HEVC decoder configuration record.
bool is_video_slice() const
Slice data partition NALs are not considered as slice NALs.
All the methods that are virtual are virtual for mocking.