OLD | NEW |
(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 #if defined(USE_PROPRIETARY_CODECS) && defined(ENABLE_HEVC_DEMUXING) |
| 5 |
| 6 #include "media/formats/mp4/hevc.h" |
| 7 |
| 8 #include <algorithm> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/logging.h" |
| 12 #include "media/base/decrypt_config.h" |
| 13 #include "media/filters/h265_parser.h" |
| 14 #include "media/formats/mp4/avc.h" |
| 15 #include "media/formats/mp4/box_definitions.h" |
| 16 #include "media/formats/mp4/box_reader.h" |
| 17 |
| 18 namespace media { |
| 19 namespace mp4 { |
| 20 |
| 21 static const uint8 kAnnexBStartCode[] = {0, 0, 0, 1}; |
| 22 static const int kAnnexBStartCodeSize = 4; |
| 23 |
| 24 bool HEVC::InsertParamSetsAnnexB( |
| 25 const HEVCDecoderConfigurationRecord& hevc_config, |
| 26 std::vector<uint8>* buffer, |
| 27 std::vector<SubsampleEntry>* subsamples) { |
| 28 DCHECK(HEVC::IsValidAnnexB(*buffer, *subsamples)); |
| 29 |
| 30 scoped_ptr<H265Parser> parser(new H265Parser()); |
| 31 const uint8* start = &(*buffer)[0]; |
| 32 parser->SetEncryptedStream(start, buffer->size(), *subsamples); |
| 33 |
| 34 H265NALU nalu; |
| 35 if (parser->AdvanceToNextNALU(&nalu) != H265Parser::kOk) |
| 36 return false; |
| 37 |
| 38 std::vector<uint8>::iterator config_insert_point = buffer->begin(); |
| 39 |
| 40 if (nalu.nal_unit_type == H265NALU::AUD_NUT) { |
| 41 // Move insert point to just after the AUD. |
| 42 config_insert_point += (nalu.data + nalu.size) - start; |
| 43 } |
| 44 |
| 45 // Clear |parser| and |start| since they aren't needed anymore and |
| 46 // will hold stale pointers once the insert happens. |
| 47 parser.reset(); |
| 48 start = NULL; |
| 49 |
| 50 std::vector<uint8> param_sets; |
| 51 RCHECK(HEVC::ConvertConfigToAnnexB(hevc_config, ¶m_sets)); |
| 52 DVLOG(4) << __FUNCTION__ << " converted hvcC to AnnexB " |
| 53 << " size=" << param_sets.size() << " inserted at " |
| 54 << (int)(config_insert_point - buffer->begin()); |
| 55 |
| 56 if (subsamples && !subsamples->empty()) { |
| 57 int subsample_index = AVC::FindSubsampleIndex(*buffer, subsamples, |
| 58 &(*config_insert_point)); |
| 59 // Update the size of the subsample where SPS/PPS is to be inserted. |
| 60 (*subsamples)[subsample_index].clear_bytes += param_sets.size(); |
| 61 } |
| 62 |
| 63 buffer->insert(config_insert_point, |
| 64 param_sets.begin(), param_sets.end()); |
| 65 |
| 66 DCHECK(HEVC::IsValidAnnexB(*buffer, *subsamples)); |
| 67 return true; |
| 68 } |
| 69 |
| 70 bool HEVC::ConvertConfigToAnnexB( |
| 71 const HEVCDecoderConfigurationRecord& hevc_config, |
| 72 std::vector<uint8>* buffer) { |
| 73 DCHECK(buffer->empty()); |
| 74 buffer->clear(); |
| 75 |
| 76 for (size_t j = 0; j < hevc_config.arrays.size(); j++) { |
| 77 uint8 naluType = hevc_config.arrays[j].first_byte & 0x3f; |
| 78 for (size_t i = 0; i < hevc_config.arrays[j].units.size(); ++i) { |
| 79 DVLOG(3) << __FUNCTION__ << " naluType=" << (int)naluType |
| 80 << " size=" << hevc_config.arrays[j].units[i].size(); |
| 81 buffer->insert(buffer->end(), kAnnexBStartCode, |
| 82 kAnnexBStartCode + kAnnexBStartCodeSize); |
| 83 buffer->insert(buffer->end(), hevc_config.arrays[j].units[i].begin(), |
| 84 hevc_config.arrays[j].units[i].end()); |
| 85 } |
| 86 } |
| 87 |
| 88 return true; |
| 89 } |
| 90 |
| 91 // Verifies AnnexB NALU order according to section 7.4.2.4.4 of ISO/IEC 23008-2. |
| 92 bool HEVC::IsValidAnnexB(const std::vector<uint8>& buffer, |
| 93 const std::vector<SubsampleEntry>& subsamples) { |
| 94 return IsValidAnnexB(&buffer[0], buffer.size(), subsamples); |
| 95 } |
| 96 |
| 97 bool HEVC::IsValidAnnexB(const uint8* buffer, size_t size, |
| 98 const std::vector<SubsampleEntry>& subsamples) { |
| 99 DCHECK(buffer); |
| 100 |
| 101 if (size == 0) |
| 102 return true; |
| 103 |
| 104 // TODO(servolk): Implement this |
| 105 return true; |
| 106 } |
| 107 } // namespace mp4 |
| 108 } // namespace media |
| 109 |
| 110 #endif |
OLD | NEW |