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 int id;
51 switch (nalu.type()) {
52 case Nalu::H264_SPS:
53 return parser_.ParseSps(nalu, &id) == H264Parser::kOk;
54 case Nalu::H264_PPS:
55 return parser_.ParsePps(nalu, &id) == H264Parser::kOk;
56 default:
57 return true;
58 }
59}
60
62 DCHECK(nalu.is_video_slice());
63 H264SliceHeader slice_header;
64 if (parser_.ParseSliceHeader(nalu, &slice_header) != H264Parser::kOk)
65 return -1;
66
67 return NumBitsToNumBytes(slice_header.header_bit_size);
68}
69
70H265VideoSliceHeaderParser::H265VideoSliceHeaderParser() {}
71H265VideoSliceHeaderParser::~H265VideoSliceHeaderParser() {}
72
74 const std::vector<uint8_t>& decoder_configuration) {
75 int id;
77 RCHECK(hevc_config.Parse(decoder_configuration));
78
79 for (size_t i = 0; i < hevc_config.nalu_count(); i++) {
80 const Nalu& nalu = hevc_config.nalu(i);
81 if (nalu.type() == Nalu::H265_SPS) {
82 RCHECK(parser_.ParseSps(nalu, &id) == H265Parser::kOk);
83 } else if (nalu.type() == Nalu::H265_PPS) {
84 RCHECK(parser_.ParsePps(nalu, &id) == H265Parser::kOk);
85 } else if (nalu.type() == Nalu::H265_VPS) {
86 // Ignore since it does not affect video slice header parsing.
87 } else {
88 VLOG(1) << "Ignoring decoder configuration Nalu of unknown type "
89 << nalu.type();
90 }
91 }
92
93 return true;
94}
95
97 int id;
98 switch (nalu.type()) {
99 case Nalu::H265_SPS:
100 return parser_.ParseSps(nalu, &id) == H265Parser::kOk;
101 case Nalu::H265_PPS:
102 return parser_.ParsePps(nalu, &id) == H265Parser::kOk;
103 case Nalu::H265_VPS:
104 // Ignore since it does not affect video slice header parsing.
105 return true;
106 default:
107 return true;
108 }
109}
110
112 DCHECK(nalu.is_video_slice());
113 H265SliceHeader slice_header;
114 if (parser_.ParseSliceHeader(nalu, &slice_header) != H265Parser::kOk)
115 return -1;
116
117 return NumBitsToNumBytes(slice_header.header_bit_size);
118}
119
120} // namespace media
121} // namespace shaka
122
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.
Result ParseSps(const Nalu &nalu, int *sps_id)
Result ParsePps(const Nalu &nalu, int *pps_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 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.