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 <cstddef>
10#include <cstdint>
11#include <vector>
12
13#include <absl/log/check.h>
14#include <absl/log/log.h>
15
16#include <packager/media/base/rcheck.h>
17#include <packager/media/codecs/avc_decoder_configuration_record.h>
18#include <packager/media/codecs/h264_parser.h>
19#include <packager/media/codecs/h265_parser.h>
20#include <packager/media/codecs/hevc_decoder_configuration_record.h>
21#include <packager/media/codecs/nalu_reader.h>
22
23namespace shaka {
24namespace media {
25
26namespace {
27
28size_t NumBitsToNumBytes(size_t size_in_bits) {
29 // Round-up division.
30 return (size_in_bits + 7) >> 3;
31}
32
33} // namespace
34
35H264VideoSliceHeaderParser::H264VideoSliceHeaderParser() {}
36H264VideoSliceHeaderParser::~H264VideoSliceHeaderParser() {}
37
39 const std::vector<uint8_t>& decoder_configuration) {
41 RCHECK(config.Parse(decoder_configuration));
42
43 for (size_t i = 0; i < config.nalu_count(); i++) {
44 int id;
45 const Nalu& nalu = config.nalu(i);
46 if (nalu.type() == Nalu::H264_SPS) {
47 RCHECK(parser_.ParseSps(nalu, &id) == H264Parser::kOk);
48 } else if (nalu.type() == Nalu::H264_PPS) {
49 RCHECK(parser_.ParsePps(nalu, &id) == H264Parser::kOk);
50 }
51 }
52
53 return true;
54}
55
57 const std::vector<uint8_t>& layered_decoder_configuration) {
58 return true;
59}
60
62 int id;
63 switch (nalu.type()) {
64 case Nalu::H264_SPS:
65 return parser_.ParseSps(nalu, &id) == H264Parser::kOk;
66 case Nalu::H264_PPS:
67 return parser_.ParsePps(nalu, &id) == H264Parser::kOk;
68 default:
69 return true;
70 }
71}
72
74 DCHECK(nalu.is_video_slice());
75 H264SliceHeader slice_header;
76 if (parser_.ParseSliceHeader(nalu, &slice_header) != H264Parser::kOk)
77 return -1;
78
79 return NumBitsToNumBytes(slice_header.header_bit_size);
80}
81
82H265VideoSliceHeaderParser::H265VideoSliceHeaderParser() {}
83H265VideoSliceHeaderParser::~H265VideoSliceHeaderParser() {}
84
85bool H265VideoSliceHeaderParser::ParseParameterSets(
86 const HEVCDecoderConfigurationRecord& config) {
87 int id;
88 for (size_t i = 0; i < config.nalu_count(); i++) {
89 const Nalu& nalu = config.nalu(i);
90 if (nalu.type() == Nalu::H265_SPS) {
91 RCHECK(parser_.ParseSps(nalu, &id) == H265Parser::kOk);
92 } else if (nalu.type() == Nalu::H265_PPS) {
93 RCHECK(parser_.ParsePps(nalu, &id) == H265Parser::kOk);
94 } else if (nalu.type() == Nalu::H265_VPS) {
95 RCHECK(parser_.ParseVps(nalu, &id) == H265Parser::kOk);
96 } else {
97 VLOG(1) << "Ignoring decoder configuration Nalu of unknown type "
98 << nalu.type();
99 }
100 }
101
102 return true;
103}
104
106 const std::vector<uint8_t>& decoder_configuration) {
108 RCHECK(hevc_config.Parse(decoder_configuration));
109 return ParseParameterSets(hevc_config);
110}
111
113 const std::vector<uint8_t>& layered_decoder_configuration) {
114 if (layered_decoder_configuration.size() > 0) {
116 lhevc_config.SetParser(&parser_);
117 RCHECK(lhevc_config.ParseLHEVCConfig(layered_decoder_configuration));
118 return ParseParameterSets(lhevc_config);
119 } else {
120 return true;
121 }
122}
123
125 int id;
126 switch (nalu.type()) {
127 case Nalu::H265_SPS:
128 return parser_.ParseSps(nalu, &id) == H265Parser::kOk;
129 case Nalu::H265_PPS:
130 return parser_.ParsePps(nalu, &id) == H265Parser::kOk;
131 case Nalu::H265_VPS:
132 return parser_.ParseVps(nalu, &id) == H265Parser::kOk;
133 default:
134 return true;
135 }
136}
137
139 DCHECK(nalu.is_video_slice());
140 H265SliceHeader slice_header;
141 if (parser_.ParseSliceHeader(nalu, &slice_header) != H265Parser::kOk)
142 return -1;
143
144 return NumBitsToNumBytes(slice_header.header_bit_size);
145}
146
147} // namespace media
148} // namespace shaka
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.