Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: media/formats/mp4/hevc.cc

Issue 816353010: Implemented HEVC video demuxing and parsing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moved back h265_parser_unittest.cc in media/BUILD.gn Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/formats/mp4/hevc.h ('k') | media/formats/mp4/mp4_stream_parser.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "media/formats/mp4/hevc.h"
6
7 #include <algorithm>
8 #include <vector>
9
10 #include "base/logging.h"
11 #include "media/base/decrypt_config.h"
12 #include "media/filters/h265_parser.h"
13 #include "media/formats/mp4/avc.h"
14 #include "media/formats/mp4/box_definitions.h"
15 #include "media/formats/mp4/box_reader.h"
16
17 namespace media {
18 namespace mp4 {
19
20 HEVCDecoderConfigurationRecord::HEVCDecoderConfigurationRecord()
21 : configurationVersion(0),
22 general_profile_space(0),
23 general_tier_flag(0),
24 general_profile_idc(0),
25 general_profile_compatibility_flags(0),
26 general_constraint_indicator_flags(0),
27 general_level_idc(0),
28 min_spatial_segmentation_idc(0),
29 parallelismType(0),
30 chromaFormat(0),
31 bitDepthLumaMinus8(0),
32 bitDepthChromaMinus8(0),
33 avgFrameRate(0),
34 constantFrameRate(0),
35 numTemporalLayers(0),
36 temporalIdNested(0),
37 lengthSizeMinusOne(0),
38 numOfArrays(0) {}
39
40 HEVCDecoderConfigurationRecord::~HEVCDecoderConfigurationRecord() {}
41 FourCC HEVCDecoderConfigurationRecord::BoxType() const { return FOURCC_HVCC; }
42
43 bool HEVCDecoderConfigurationRecord::Parse(BoxReader* reader) {
44 return ParseInternal(reader, reader->media_log());
45 }
46
47 bool HEVCDecoderConfigurationRecord::Parse(const uint8* data, int data_size) {
48 BufferReader reader(data, data_size);
49 return ParseInternal(&reader, new MediaLog());
50 }
51
52 HEVCDecoderConfigurationRecord::HVCCNALArray::HVCCNALArray()
53 : first_byte(0) {}
54
55 HEVCDecoderConfigurationRecord::HVCCNALArray::~HVCCNALArray() {}
56
57 bool HEVCDecoderConfigurationRecord::ParseInternal(
58 BufferReader* reader,
59 const scoped_refptr<MediaLog>& media_log) {
60 uint8 profile_indication = 0;
61 uint32 general_constraint_indicator_flags_hi = 0;
62 uint16 general_constraint_indicator_flags_lo = 0;
63 uint8 misc = 0;
64 RCHECK(reader->Read1(&configurationVersion) && configurationVersion == 1 &&
65 reader->Read1(&profile_indication) &&
66 reader->Read4(&general_profile_compatibility_flags) &&
67 reader->Read4(&general_constraint_indicator_flags_hi) &&
68 reader->Read2(&general_constraint_indicator_flags_lo) &&
69 reader->Read1(&general_level_idc) &&
70 reader->Read2(&min_spatial_segmentation_idc) &&
71 reader->Read1(&parallelismType) &&
72 reader->Read1(&chromaFormat) &&
73 reader->Read1(&bitDepthLumaMinus8) &&
74 reader->Read1(&bitDepthChromaMinus8) &&
75 reader->Read2(&avgFrameRate) &&
76 reader->Read1(&misc) &&
77 reader->Read1(&numOfArrays));
78
79 general_profile_space = profile_indication >> 6;
80 general_tier_flag = (profile_indication >> 5) & 1;
81 general_profile_idc = profile_indication & 0x1f;
82
83 general_constraint_indicator_flags = general_constraint_indicator_flags_hi;
84 general_constraint_indicator_flags <<= 16;
85 general_constraint_indicator_flags |= general_constraint_indicator_flags_lo;
86
87 min_spatial_segmentation_idc &= 0xfff;
88 parallelismType &= 3;
89 chromaFormat &= 3;
90 bitDepthLumaMinus8 &= 7;
91 bitDepthChromaMinus8 &= 7;
92
93 constantFrameRate = misc >> 6;
94 numTemporalLayers = (misc >> 3) & 7;
95 temporalIdNested = (misc >> 2) & 1;
96 lengthSizeMinusOne = misc & 3;
97
98 DVLOG(2) << __FUNCTION__ << " numOfArrays=" << (int)numOfArrays;
99 arrays.resize(numOfArrays);
100 for (uint32 j = 0; j < numOfArrays; j++) {
101 RCHECK(reader->Read1(&arrays[j].first_byte));
102 uint16 numNalus = 0;
103 RCHECK(reader->Read2(&numNalus));
104 arrays[j].units.resize(numNalus);
105 for (uint32 i = 0; i < numNalus; ++i) {
106 uint16 naluLength = 0;
107 RCHECK(reader->Read2(&naluLength) &&
108 reader->ReadVec(&arrays[j].units[i], naluLength));
109 DVLOG(4) << __FUNCTION__ << " naluType="
110 << (int)(arrays[j].first_byte & 0x3f)
111 << " size=" << arrays[j].units[i].size();
112 }
113 }
114
115 if (media_log.get()) {
116 MEDIA_LOG(INFO, media_log) << "Video codec: hevc";
117 }
118
119 return true;
120 }
121
122 static const uint8 kAnnexBStartCode[] = {0, 0, 0, 1};
123 static const int kAnnexBStartCodeSize = 4;
124
125 bool HEVC::InsertParamSetsAnnexB(
126 const HEVCDecoderConfigurationRecord& hevc_config,
127 std::vector<uint8>* buffer,
128 std::vector<SubsampleEntry>* subsamples) {
129 DCHECK(HEVC::IsValidAnnexB(*buffer, *subsamples));
130
131 scoped_ptr<H265Parser> parser(new H265Parser());
132 const uint8* start = &(*buffer)[0];
133 parser->SetEncryptedStream(start, buffer->size(), *subsamples);
134
135 H265NALU nalu;
136 if (parser->AdvanceToNextNALU(&nalu) != H265Parser::kOk)
137 return false;
138
139 std::vector<uint8>::iterator config_insert_point = buffer->begin();
140
141 if (nalu.nal_unit_type == H265NALU::AUD_NUT) {
142 // Move insert point to just after the AUD.
143 config_insert_point += (nalu.data + nalu.size) - start;
144 }
145
146 // Clear |parser| and |start| since they aren't needed anymore and
147 // will hold stale pointers once the insert happens.
148 parser.reset();
149 start = NULL;
150
151 std::vector<uint8> param_sets;
152 RCHECK(HEVC::ConvertConfigToAnnexB(hevc_config, &param_sets));
153 DVLOG(4) << __FUNCTION__ << " converted hvcC to AnnexB "
154 << " size=" << param_sets.size() << " inserted at "
155 << (int)(config_insert_point - buffer->begin());
156
157 if (subsamples && !subsamples->empty()) {
158 int subsample_index = AVC::FindSubsampleIndex(*buffer, subsamples,
159 &(*config_insert_point));
160 // Update the size of the subsample where SPS/PPS is to be inserted.
161 (*subsamples)[subsample_index].clear_bytes += param_sets.size();
162 }
163
164 buffer->insert(config_insert_point,
165 param_sets.begin(), param_sets.end());
166
167 DCHECK(HEVC::IsValidAnnexB(*buffer, *subsamples));
168 return true;
169 }
170
171 bool HEVC::ConvertConfigToAnnexB(
172 const HEVCDecoderConfigurationRecord& hevc_config,
173 std::vector<uint8>* buffer) {
174 DCHECK(buffer->empty());
175 buffer->clear();
176
177 for (size_t j = 0; j < hevc_config.arrays.size(); j++) {
178 uint8 naluType = hevc_config.arrays[j].first_byte & 0x3f;
179 for (size_t i = 0; i < hevc_config.arrays[j].units.size(); ++i) {
180 DVLOG(3) << __FUNCTION__ << " naluType=" << (int)naluType
181 << " size=" << hevc_config.arrays[j].units[i].size();
182 buffer->insert(buffer->end(), kAnnexBStartCode,
183 kAnnexBStartCode + kAnnexBStartCodeSize);
184 buffer->insert(buffer->end(), hevc_config.arrays[j].units[i].begin(),
185 hevc_config.arrays[j].units[i].end());
186 }
187 }
188
189 return true;
190 }
191
192 // Verifies AnnexB NALU order according to section 7.4.2.4.4 of ISO/IEC 23008-2.
193 bool HEVC::IsValidAnnexB(const std::vector<uint8>& buffer,
194 const std::vector<SubsampleEntry>& subsamples) {
195 return IsValidAnnexB(&buffer[0], buffer.size(), subsamples);
196 }
197
198 bool HEVC::IsValidAnnexB(const uint8* buffer, size_t size,
199 const std::vector<SubsampleEntry>& subsamples) {
200 DCHECK(buffer);
201
202 if (size == 0)
203 return true;
204
205 // TODO(servolk): Implement this, see crbug.com/527595
206 return true;
207 }
208
209 HEVCBitstreamConverter::HEVCBitstreamConverter(
210 scoped_ptr<HEVCDecoderConfigurationRecord> hevc_config)
211 : hevc_config_(hevc_config.Pass()) {
212 DCHECK(hevc_config_);
213 }
214
215 HEVCBitstreamConverter::~HEVCBitstreamConverter() {
216 }
217
218 bool HEVCBitstreamConverter::ConvertFrame(
219 std::vector<uint8>* frame_buf,
220 bool is_keyframe,
221 std::vector<SubsampleEntry>* subsamples) const {
222 RCHECK(AVC::ConvertFrameToAnnexB(hevc_config_->lengthSizeMinusOne + 1,
223 frame_buf, subsamples));
224
225 if (is_keyframe) {
226 // If this is a keyframe, we (re-)inject HEVC params headers at the start of
227 // a frame. If subsample info is present, we also update the clear byte
228 // count for that first subsample.
229 RCHECK(HEVC::InsertParamSetsAnnexB(*hevc_config_, frame_buf, subsamples));
230 }
231
232 DCHECK(HEVC::IsValidAnnexB(*frame_buf, *subsamples));
233 return true;
234 }
235
236 } // namespace mp4
237 } // namespace media
OLDNEW
« no previous file with comments | « media/formats/mp4/hevc.h ('k') | media/formats/mp4/mp4_stream_parser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698