| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/formats/mp4/hevc.h" | 5 #include "media/formats/mp4/hevc.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 lengthSizeMinusOne(0), | 37 lengthSizeMinusOne(0), |
| 38 numOfArrays(0) {} | 38 numOfArrays(0) {} |
| 39 | 39 |
| 40 HEVCDecoderConfigurationRecord::~HEVCDecoderConfigurationRecord() {} | 40 HEVCDecoderConfigurationRecord::~HEVCDecoderConfigurationRecord() {} |
| 41 FourCC HEVCDecoderConfigurationRecord::BoxType() const { return FOURCC_HVCC; } | 41 FourCC HEVCDecoderConfigurationRecord::BoxType() const { return FOURCC_HVCC; } |
| 42 | 42 |
| 43 bool HEVCDecoderConfigurationRecord::Parse(BoxReader* reader) { | 43 bool HEVCDecoderConfigurationRecord::Parse(BoxReader* reader) { |
| 44 return ParseInternal(reader, reader->media_log()); | 44 return ParseInternal(reader, reader->media_log()); |
| 45 } | 45 } |
| 46 | 46 |
| 47 bool HEVCDecoderConfigurationRecord::Parse(const uint8* data, int data_size) { | 47 bool HEVCDecoderConfigurationRecord::Parse(const uint8_t* data, int data_size) { |
| 48 BufferReader reader(data, data_size); | 48 BufferReader reader(data, data_size); |
| 49 return ParseInternal(&reader, new MediaLog()); | 49 return ParseInternal(&reader, new MediaLog()); |
| 50 } | 50 } |
| 51 | 51 |
| 52 HEVCDecoderConfigurationRecord::HVCCNALArray::HVCCNALArray() | 52 HEVCDecoderConfigurationRecord::HVCCNALArray::HVCCNALArray() |
| 53 : first_byte(0) {} | 53 : first_byte(0) {} |
| 54 | 54 |
| 55 HEVCDecoderConfigurationRecord::HVCCNALArray::~HVCCNALArray() {} | 55 HEVCDecoderConfigurationRecord::HVCCNALArray::~HVCCNALArray() {} |
| 56 | 56 |
| 57 bool HEVCDecoderConfigurationRecord::ParseInternal( | 57 bool HEVCDecoderConfigurationRecord::ParseInternal( |
| 58 BufferReader* reader, | 58 BufferReader* reader, |
| 59 const scoped_refptr<MediaLog>& media_log) { | 59 const scoped_refptr<MediaLog>& media_log) { |
| 60 uint8 profile_indication = 0; | 60 uint8_t profile_indication = 0; |
| 61 uint32 general_constraint_indicator_flags_hi = 0; | 61 uint32_t general_constraint_indicator_flags_hi = 0; |
| 62 uint16 general_constraint_indicator_flags_lo = 0; | 62 uint16_t general_constraint_indicator_flags_lo = 0; |
| 63 uint8 misc = 0; | 63 uint8_t misc = 0; |
| 64 RCHECK(reader->Read1(&configurationVersion) && configurationVersion == 1 && | 64 RCHECK(reader->Read1(&configurationVersion) && configurationVersion == 1 && |
| 65 reader->Read1(&profile_indication) && | 65 reader->Read1(&profile_indication) && |
| 66 reader->Read4(&general_profile_compatibility_flags) && | 66 reader->Read4(&general_profile_compatibility_flags) && |
| 67 reader->Read4(&general_constraint_indicator_flags_hi) && | 67 reader->Read4(&general_constraint_indicator_flags_hi) && |
| 68 reader->Read2(&general_constraint_indicator_flags_lo) && | 68 reader->Read2(&general_constraint_indicator_flags_lo) && |
| 69 reader->Read1(&general_level_idc) && | 69 reader->Read1(&general_level_idc) && |
| 70 reader->Read2(&min_spatial_segmentation_idc) && | 70 reader->Read2(&min_spatial_segmentation_idc) && |
| 71 reader->Read1(¶llelismType) && | 71 reader->Read1(¶llelismType) && |
| 72 reader->Read1(&chromaFormat) && | 72 reader->Read1(&chromaFormat) && |
| 73 reader->Read1(&bitDepthLumaMinus8) && | 73 reader->Read1(&bitDepthLumaMinus8) && |
| (...skipping 16 matching lines...) Expand all Loading... |
| 90 bitDepthLumaMinus8 &= 7; | 90 bitDepthLumaMinus8 &= 7; |
| 91 bitDepthChromaMinus8 &= 7; | 91 bitDepthChromaMinus8 &= 7; |
| 92 | 92 |
| 93 constantFrameRate = misc >> 6; | 93 constantFrameRate = misc >> 6; |
| 94 numTemporalLayers = (misc >> 3) & 7; | 94 numTemporalLayers = (misc >> 3) & 7; |
| 95 temporalIdNested = (misc >> 2) & 1; | 95 temporalIdNested = (misc >> 2) & 1; |
| 96 lengthSizeMinusOne = misc & 3; | 96 lengthSizeMinusOne = misc & 3; |
| 97 | 97 |
| 98 DVLOG(2) << __FUNCTION__ << " numOfArrays=" << (int)numOfArrays; | 98 DVLOG(2) << __FUNCTION__ << " numOfArrays=" << (int)numOfArrays; |
| 99 arrays.resize(numOfArrays); | 99 arrays.resize(numOfArrays); |
| 100 for (uint32 j = 0; j < numOfArrays; j++) { | 100 for (uint32_t j = 0; j < numOfArrays; j++) { |
| 101 RCHECK(reader->Read1(&arrays[j].first_byte)); | 101 RCHECK(reader->Read1(&arrays[j].first_byte)); |
| 102 uint16 numNalus = 0; | 102 uint16_t numNalus = 0; |
| 103 RCHECK(reader->Read2(&numNalus)); | 103 RCHECK(reader->Read2(&numNalus)); |
| 104 arrays[j].units.resize(numNalus); | 104 arrays[j].units.resize(numNalus); |
| 105 for (uint32 i = 0; i < numNalus; ++i) { | 105 for (uint32_t i = 0; i < numNalus; ++i) { |
| 106 uint16 naluLength = 0; | 106 uint16_t naluLength = 0; |
| 107 RCHECK(reader->Read2(&naluLength) && | 107 RCHECK(reader->Read2(&naluLength) && |
| 108 reader->ReadVec(&arrays[j].units[i], naluLength)); | 108 reader->ReadVec(&arrays[j].units[i], naluLength)); |
| 109 DVLOG(4) << __FUNCTION__ << " naluType=" | 109 DVLOG(4) << __FUNCTION__ << " naluType=" |
| 110 << (int)(arrays[j].first_byte & 0x3f) | 110 << (int)(arrays[j].first_byte & 0x3f) |
| 111 << " size=" << arrays[j].units[i].size(); | 111 << " size=" << arrays[j].units[i].size(); |
| 112 } | 112 } |
| 113 } | 113 } |
| 114 | 114 |
| 115 if (media_log.get()) { | 115 if (media_log.get()) { |
| 116 MEDIA_LOG(INFO, media_log) << "Video codec: hevc"; | 116 MEDIA_LOG(INFO, media_log) << "Video codec: hevc"; |
| 117 } | 117 } |
| 118 | 118 |
| 119 return true; | 119 return true; |
| 120 } | 120 } |
| 121 | 121 |
| 122 static const uint8 kAnnexBStartCode[] = {0, 0, 0, 1}; | 122 static const uint8_t kAnnexBStartCode[] = {0, 0, 0, 1}; |
| 123 static const int kAnnexBStartCodeSize = 4; | 123 static const int kAnnexBStartCodeSize = 4; |
| 124 | 124 |
| 125 bool HEVC::InsertParamSetsAnnexB( | 125 bool HEVC::InsertParamSetsAnnexB( |
| 126 const HEVCDecoderConfigurationRecord& hevc_config, | 126 const HEVCDecoderConfigurationRecord& hevc_config, |
| 127 std::vector<uint8>* buffer, | 127 std::vector<uint8_t>* buffer, |
| 128 std::vector<SubsampleEntry>* subsamples) { | 128 std::vector<SubsampleEntry>* subsamples) { |
| 129 DCHECK(HEVC::IsValidAnnexB(*buffer, *subsamples)); | 129 DCHECK(HEVC::IsValidAnnexB(*buffer, *subsamples)); |
| 130 | 130 |
| 131 scoped_ptr<H265Parser> parser(new H265Parser()); | 131 scoped_ptr<H265Parser> parser(new H265Parser()); |
| 132 const uint8* start = &(*buffer)[0]; | 132 const uint8_t* start = &(*buffer)[0]; |
| 133 parser->SetEncryptedStream(start, buffer->size(), *subsamples); | 133 parser->SetEncryptedStream(start, buffer->size(), *subsamples); |
| 134 | 134 |
| 135 H265NALU nalu; | 135 H265NALU nalu; |
| 136 if (parser->AdvanceToNextNALU(&nalu) != H265Parser::kOk) | 136 if (parser->AdvanceToNextNALU(&nalu) != H265Parser::kOk) |
| 137 return false; | 137 return false; |
| 138 | 138 |
| 139 std::vector<uint8>::iterator config_insert_point = buffer->begin(); | 139 std::vector<uint8_t>::iterator config_insert_point = buffer->begin(); |
| 140 | 140 |
| 141 if (nalu.nal_unit_type == H265NALU::AUD_NUT) { | 141 if (nalu.nal_unit_type == H265NALU::AUD_NUT) { |
| 142 // Move insert point to just after the AUD. | 142 // Move insert point to just after the AUD. |
| 143 config_insert_point += (nalu.data + nalu.size) - start; | 143 config_insert_point += (nalu.data + nalu.size) - start; |
| 144 } | 144 } |
| 145 | 145 |
| 146 // Clear |parser| and |start| since they aren't needed anymore and | 146 // Clear |parser| and |start| since they aren't needed anymore and |
| 147 // will hold stale pointers once the insert happens. | 147 // will hold stale pointers once the insert happens. |
| 148 parser.reset(); | 148 parser.reset(); |
| 149 start = NULL; | 149 start = NULL; |
| 150 | 150 |
| 151 std::vector<uint8> param_sets; | 151 std::vector<uint8_t> param_sets; |
| 152 RCHECK(HEVC::ConvertConfigToAnnexB(hevc_config, ¶m_sets)); | 152 RCHECK(HEVC::ConvertConfigToAnnexB(hevc_config, ¶m_sets)); |
| 153 DVLOG(4) << __FUNCTION__ << " converted hvcC to AnnexB " | 153 DVLOG(4) << __FUNCTION__ << " converted hvcC to AnnexB " |
| 154 << " size=" << param_sets.size() << " inserted at " | 154 << " size=" << param_sets.size() << " inserted at " |
| 155 << (int)(config_insert_point - buffer->begin()); | 155 << (int)(config_insert_point - buffer->begin()); |
| 156 | 156 |
| 157 if (subsamples && !subsamples->empty()) { | 157 if (subsamples && !subsamples->empty()) { |
| 158 int subsample_index = AVC::FindSubsampleIndex(*buffer, subsamples, | 158 int subsample_index = AVC::FindSubsampleIndex(*buffer, subsamples, |
| 159 &(*config_insert_point)); | 159 &(*config_insert_point)); |
| 160 // Update the size of the subsample where SPS/PPS is to be inserted. | 160 // Update the size of the subsample where SPS/PPS is to be inserted. |
| 161 (*subsamples)[subsample_index].clear_bytes += param_sets.size(); | 161 (*subsamples)[subsample_index].clear_bytes += param_sets.size(); |
| 162 } | 162 } |
| 163 | 163 |
| 164 buffer->insert(config_insert_point, | 164 buffer->insert(config_insert_point, |
| 165 param_sets.begin(), param_sets.end()); | 165 param_sets.begin(), param_sets.end()); |
| 166 | 166 |
| 167 DCHECK(HEVC::IsValidAnnexB(*buffer, *subsamples)); | 167 DCHECK(HEVC::IsValidAnnexB(*buffer, *subsamples)); |
| 168 return true; | 168 return true; |
| 169 } | 169 } |
| 170 | 170 |
| 171 bool HEVC::ConvertConfigToAnnexB( | 171 bool HEVC::ConvertConfigToAnnexB( |
| 172 const HEVCDecoderConfigurationRecord& hevc_config, | 172 const HEVCDecoderConfigurationRecord& hevc_config, |
| 173 std::vector<uint8>* buffer) { | 173 std::vector<uint8_t>* buffer) { |
| 174 DCHECK(buffer->empty()); | 174 DCHECK(buffer->empty()); |
| 175 buffer->clear(); | 175 buffer->clear(); |
| 176 | 176 |
| 177 for (size_t j = 0; j < hevc_config.arrays.size(); j++) { | 177 for (size_t j = 0; j < hevc_config.arrays.size(); j++) { |
| 178 uint8 naluType = hevc_config.arrays[j].first_byte & 0x3f; | 178 uint8_t naluType = hevc_config.arrays[j].first_byte & 0x3f; |
| 179 for (size_t i = 0; i < hevc_config.arrays[j].units.size(); ++i) { | 179 for (size_t i = 0; i < hevc_config.arrays[j].units.size(); ++i) { |
| 180 DVLOG(3) << __FUNCTION__ << " naluType=" << (int)naluType | 180 DVLOG(3) << __FUNCTION__ << " naluType=" << (int)naluType |
| 181 << " size=" << hevc_config.arrays[j].units[i].size(); | 181 << " size=" << hevc_config.arrays[j].units[i].size(); |
| 182 buffer->insert(buffer->end(), kAnnexBStartCode, | 182 buffer->insert(buffer->end(), kAnnexBStartCode, |
| 183 kAnnexBStartCode + kAnnexBStartCodeSize); | 183 kAnnexBStartCode + kAnnexBStartCodeSize); |
| 184 buffer->insert(buffer->end(), hevc_config.arrays[j].units[i].begin(), | 184 buffer->insert(buffer->end(), hevc_config.arrays[j].units[i].begin(), |
| 185 hevc_config.arrays[j].units[i].end()); | 185 hevc_config.arrays[j].units[i].end()); |
| 186 } | 186 } |
| 187 } | 187 } |
| 188 | 188 |
| 189 return true; | 189 return true; |
| 190 } | 190 } |
| 191 | 191 |
| 192 // Verifies AnnexB NALU order according to section 7.4.2.4.4 of ISO/IEC 23008-2. | 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, | 193 bool HEVC::IsValidAnnexB(const std::vector<uint8_t>& buffer, |
| 194 const std::vector<SubsampleEntry>& subsamples) { | 194 const std::vector<SubsampleEntry>& subsamples) { |
| 195 return IsValidAnnexB(&buffer[0], buffer.size(), subsamples); | 195 return IsValidAnnexB(&buffer[0], buffer.size(), subsamples); |
| 196 } | 196 } |
| 197 | 197 |
| 198 bool HEVC::IsValidAnnexB(const uint8* buffer, size_t size, | 198 bool HEVC::IsValidAnnexB(const uint8_t* buffer, |
| 199 size_t size, |
| 199 const std::vector<SubsampleEntry>& subsamples) { | 200 const std::vector<SubsampleEntry>& subsamples) { |
| 200 DCHECK(buffer); | 201 DCHECK(buffer); |
| 201 | 202 |
| 202 if (size == 0) | 203 if (size == 0) |
| 203 return true; | 204 return true; |
| 204 | 205 |
| 205 // TODO(servolk): Implement this, see crbug.com/527595 | 206 // TODO(servolk): Implement this, see crbug.com/527595 |
| 206 return true; | 207 return true; |
| 207 } | 208 } |
| 208 | 209 |
| 209 HEVCBitstreamConverter::HEVCBitstreamConverter( | 210 HEVCBitstreamConverter::HEVCBitstreamConverter( |
| 210 scoped_ptr<HEVCDecoderConfigurationRecord> hevc_config) | 211 scoped_ptr<HEVCDecoderConfigurationRecord> hevc_config) |
| 211 : hevc_config_(hevc_config.Pass()) { | 212 : hevc_config_(hevc_config.Pass()) { |
| 212 DCHECK(hevc_config_); | 213 DCHECK(hevc_config_); |
| 213 } | 214 } |
| 214 | 215 |
| 215 HEVCBitstreamConverter::~HEVCBitstreamConverter() { | 216 HEVCBitstreamConverter::~HEVCBitstreamConverter() { |
| 216 } | 217 } |
| 217 | 218 |
| 218 bool HEVCBitstreamConverter::ConvertFrame( | 219 bool HEVCBitstreamConverter::ConvertFrame( |
| 219 std::vector<uint8>* frame_buf, | 220 std::vector<uint8_t>* frame_buf, |
| 220 bool is_keyframe, | 221 bool is_keyframe, |
| 221 std::vector<SubsampleEntry>* subsamples) const { | 222 std::vector<SubsampleEntry>* subsamples) const { |
| 222 RCHECK(AVC::ConvertFrameToAnnexB(hevc_config_->lengthSizeMinusOne + 1, | 223 RCHECK(AVC::ConvertFrameToAnnexB(hevc_config_->lengthSizeMinusOne + 1, |
| 223 frame_buf, subsamples)); | 224 frame_buf, subsamples)); |
| 224 | 225 |
| 225 if (is_keyframe) { | 226 if (is_keyframe) { |
| 226 // If this is a keyframe, we (re-)inject HEVC params headers at the start of | 227 // 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 // a frame. If subsample info is present, we also update the clear byte |
| 228 // count for that first subsample. | 229 // count for that first subsample. |
| 229 RCHECK(HEVC::InsertParamSetsAnnexB(*hevc_config_, frame_buf, subsamples)); | 230 RCHECK(HEVC::InsertParamSetsAnnexB(*hevc_config_, frame_buf, subsamples)); |
| 230 } | 231 } |
| 231 | 232 |
| 232 DCHECK(HEVC::IsValidAnnexB(*frame_buf, *subsamples)); | 233 DCHECK(HEVC::IsValidAnnexB(*frame_buf, *subsamples)); |
| 233 return true; | 234 return true; |
| 234 } | 235 } |
| 235 | 236 |
| 236 } // namespace mp4 | 237 } // namespace mp4 |
| 237 } // namespace media | 238 } // namespace media |
| OLD | NEW |