Chromium Code Reviews| Index: media/formats/mp4/hevc.cc |
| diff --git a/media/formats/mp4/hevc.cc b/media/formats/mp4/hevc.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e032fd74787973a125247384410343b056a05a15 |
| --- /dev/null |
| +++ b/media/formats/mp4/hevc.cc |
| @@ -0,0 +1,135 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/formats/mp4/hevc.h" |
| + |
| +#include <algorithm> |
| +#include <vector> |
| + |
| +#include "base/logging.h" |
| +#include "media/base/decrypt_config.h" |
| +#include "media/filters/h265_parser.h" |
| +#include "media/formats/mp4/avc.h" |
| +#include "media/formats/mp4/box_definitions.h" |
| +#include "media/formats/mp4/box_reader.h" |
| + |
| +namespace media { |
| +namespace mp4 { |
| + |
| +static const uint8 kAnnexBStartCode[] = {0, 0, 0, 1}; |
| +static const int kAnnexBStartCodeSize = 4; |
| + |
| +bool HEVC::InsertParamSetsAnnexB( |
| + const HEVCDecoderConfigurationRecord& hevc_config, |
| + std::vector<uint8>* buffer, |
| + std::vector<SubsampleEntry>* subsamples) { |
| + DCHECK(HEVC::IsValidAnnexB(*buffer, *subsamples)); |
| + |
| + scoped_ptr<H265Parser> parser(new H265Parser()); |
| + const uint8* start = &(*buffer)[0]; |
| + parser->SetEncryptedStream(start, buffer->size(), *subsamples); |
| + |
| + H265NALU nalu; |
| + if (parser->AdvanceToNextNALU(&nalu) != H265Parser::kOk) |
| + return false; |
| + |
| + std::vector<uint8>::iterator config_insert_point = buffer->begin(); |
| + |
| + if (nalu.nal_unit_type == H265NALU::AUD_NUT) { |
| + // Move insert point to just after the AUD. |
| + config_insert_point += (nalu.data + nalu.size) - start; |
| + } |
| + |
| + // Clear |parser| and |start| since they aren't needed anymore and |
| + // will hold stale pointers once the insert happens. |
| + parser.reset(); |
| + start = NULL; |
| + |
| + std::vector<uint8> param_sets; |
| + RCHECK(HEVC::ConvertConfigToAnnexB(hevc_config, ¶m_sets)); |
| + DVLOG(4) << __FUNCTION__ << " converted hvcC to AnnexB " |
| + << " size=" << param_sets.size() << " inserted at " |
| + << (int)(config_insert_point - buffer->begin()); |
| + |
| + if (subsamples && !subsamples->empty()) { |
| + int subsample_index = AVC::FindSubsampleIndex(*buffer, subsamples, |
| + &(*config_insert_point)); |
| + // Update the size of the subsample where SPS/PPS is to be inserted. |
| + (*subsamples)[subsample_index].clear_bytes += param_sets.size(); |
| + } |
| + |
| + buffer->insert(config_insert_point, |
| + param_sets.begin(), param_sets.end()); |
| + |
| + DCHECK(HEVC::IsValidAnnexB(*buffer, *subsamples)); |
| + return true; |
| +} |
| + |
| +bool HEVC::ConvertConfigToAnnexB( |
| + const HEVCDecoderConfigurationRecord& hevc_config, |
| + std::vector<uint8>* buffer) { |
| + DCHECK(buffer->empty()); |
| + buffer->clear(); |
| + |
| + for (size_t j = 0; j < hevc_config.arrays.size(); j++) { |
| + uint8 naluType = hevc_config.arrays[j].first_byte & 0x3f; |
| + for (size_t i = 0; i < hevc_config.arrays[j].units.size(); ++i) { |
| + DVLOG(3) << __FUNCTION__ << " naluType=" << (int)naluType |
| + << " size=" << hevc_config.arrays[j].units[i].size(); |
| + buffer->insert(buffer->end(), kAnnexBStartCode, |
| + kAnnexBStartCode + kAnnexBStartCodeSize); |
| + buffer->insert(buffer->end(), hevc_config.arrays[j].units[i].begin(), |
| + hevc_config.arrays[j].units[i].end()); |
| + } |
| + } |
| + |
| + return true; |
| +} |
| + |
| +// Verifies AnnexB NALU order according to section 7.4.2.4.4 of ISO/IEC 23008-2. |
| +bool HEVC::IsValidAnnexB(const std::vector<uint8>& buffer, |
| + const std::vector<SubsampleEntry>& subsamples) { |
| + return IsValidAnnexB(&buffer[0], buffer.size(), subsamples); |
| +} |
| + |
| +bool HEVC::IsValidAnnexB(const uint8* buffer, size_t size, |
| + const std::vector<SubsampleEntry>& subsamples) { |
| + DCHECK(buffer); |
| + |
| + if (size == 0) |
| + return true; |
| + |
| + // TODO(servolk): Implement this |
|
wolenetz
2015/09/02 20:43:18
nit: reference a bug?
servolk
2015/09/03 00:17:51
Done, crbug.com/527595
|
| + return true; |
| +} |
| + |
| +HEVCBitstreamConverter::HEVCBitstreamConverter( |
| + scoped_ptr<HEVCDecoderConfigurationRecord> hevc_config) |
| + : hevc_config_(hevc_config.Pass()) { |
| + DCHECK(hevc_config_); |
| +} |
| + |
| +HEVCBitstreamConverter::~HEVCBitstreamConverter() { |
| +} |
| + |
| +bool HEVCBitstreamConverter::ConvertFrame( |
| + std::vector<uint8>* frame_buf, |
| + bool is_keyframe, |
| + std::vector<SubsampleEntry>* subsamples) const { |
| + RCHECK(AVC::ConvertFrameToAnnexB(hevc_config_->lengthSizeMinusOne + 1, |
| + frame_buf, subsamples)); |
| + |
| + if (is_keyframe) { |
| + // If this is a keyframe, we (re-)inject HEVC params headers at the start of |
| + // a frame. If subsample info is present, we also update the clear byte |
| + // count for that first subsample. |
| + RCHECK(HEVC::InsertParamSetsAnnexB(*hevc_config_, frame_buf, subsamples)); |
| + } |
| + |
| + DCHECK(HEVC::IsValidAnnexB(*frame_buf, *subsamples)); |
| + return true; |
| +} |
| + |
| +} // namespace mp4 |
| +} // namespace media |