From 0a8dc6f606c30c3656996c2efffac445691025fb Mon Sep 17 00:00:00 2001 From: Kongqun Yang Date: Thu, 20 Feb 2014 18:09:35 -0800 Subject: [PATCH] Clean up *_iterator code in media/mp4. Make functions const if needed and reformat the code using clang-format. Change-Id: If2848ef6c3b61df5358286687d80213533125856 --- media/mp4/chunk_info_iterator.cc | 27 +++++++++++++----------- media/mp4/chunk_info_iterator.h | 21 +++++++----------- media/mp4/composition_offset_iterator.cc | 13 ++++++------ media/mp4/composition_offset_iterator.h | 10 ++++----- media/mp4/decoding_time_iterator.cc | 21 +++++++++--------- media/mp4/decoding_time_iterator.h | 10 ++++----- media/mp4/sync_sample_iterator.cc | 11 +++++----- media/mp4/sync_sample_iterator.h | 4 ++-- 8 files changed, 55 insertions(+), 62 deletions(-) diff --git a/media/mp4/chunk_info_iterator.cc b/media/mp4/chunk_info_iterator.cc index 0edb841849..afdbb9192a 100644 --- a/media/mp4/chunk_info_iterator.cc +++ b/media/mp4/chunk_info_iterator.cc @@ -40,24 +40,27 @@ bool ChunkInfoIterator::AdvanceSample() { return true; } -bool ChunkInfoIterator::IsValid() { - return iterator_ != chunk_info_table_.end() - && chunk_sample_index_ < iterator_->samples_per_chunk; +bool ChunkInfoIterator::IsValid() const { + return iterator_ != chunk_info_table_.end() && + chunk_sample_index_ < iterator_->samples_per_chunk; } -uint32 ChunkInfoIterator::NumSamples(uint32 start_chunk, uint32 end_chunk) { - DCHECK(start_chunk <= end_chunk); +uint32 ChunkInfoIterator::NumSamples(uint32 start_chunk, + uint32 end_chunk) const { + DCHECK_LE(start_chunk, end_chunk); + uint32 last_chunk = 0; uint32 num_samples = 0; for (std::vector::const_iterator it = chunk_info_table_.begin(); - it != chunk_info_table_.end(); ++it) { - last_chunk = ( - (it + 1 == chunk_info_table_.end()) ? - std::numeric_limits::max() : (it + 1)->first_chunk) - 1; + it != chunk_info_table_.end(); + ++it) { + last_chunk = (it + 1 == chunk_info_table_.end()) + ? std::numeric_limits::max() + : (it + 1)->first_chunk - 1; if (last_chunk >= start_chunk) { - num_samples += (std::min(end_chunk, last_chunk) - - std::max(start_chunk, it->first_chunk) + 1) - * it->samples_per_chunk; + num_samples += (std::min(end_chunk, last_chunk) - + std::max(start_chunk, it->first_chunk) + 1) * + it->samples_per_chunk; if (last_chunk >= end_chunk) break; } diff --git a/media/mp4/chunk_info_iterator.h b/media/mp4/chunk_info_iterator.h index 32b4ecb38f..1719f4c990 100644 --- a/media/mp4/chunk_info_iterator.h +++ b/media/mp4/chunk_info_iterator.h @@ -31,32 +31,27 @@ class ChunkInfoIterator { bool AdvanceSample(); // Return whether the current chunk is valid. - bool IsValid(); + bool IsValid() const; // Return current chunk. - uint32 current_chunk() { - return current_chunk_; - } + uint32 current_chunk() const { return current_chunk_; } // Return samples per chunk for current chunk. - uint32 samples_per_chunk() { - return iterator_->samples_per_chunk; - } + uint32 samples_per_chunk() const { return iterator_->samples_per_chunk; } // Return sample description index for current chunk. - uint32 sample_description_index() { + uint32 sample_description_index() const { return iterator_->sample_description_index; } // Return number of samples from start_chunk to end_chunk, both 1-based, // inclusive. - uint32 NumSamples(uint32 start_chunk, uint32 end_chunk); + uint32 NumSamples(uint32 start_chunk, uint32 end_chunk) const; // Return the last first_chunk in chunk_info_table. - uint32 LastFirstChunk() { - if (chunk_info_table_.size() == 0) - return 0; - return (chunk_info_table_.end() - 1)->first_chunk; + uint32 LastFirstChunk() const { + return chunk_info_table_.empty() ? 0 + : chunk_info_table_.back().first_chunk; } private: diff --git a/media/mp4/composition_offset_iterator.cc b/media/mp4/composition_offset_iterator.cc index c8d37015c8..c013b2e15d 100644 --- a/media/mp4/composition_offset_iterator.cc +++ b/media/mp4/composition_offset_iterator.cc @@ -15,8 +15,7 @@ CompositionOffsetIterator::CompositionOffsetIterator( const CompositionTimeToSample& composition_time_to_sample) : sample_index_(0), composition_offset_table_(composition_time_to_sample.composition_offset), - iterator_(composition_offset_table_.begin()) { -} + iterator_(composition_offset_table_.begin()) {} bool CompositionOffsetIterator::AdvanceSample() { ++sample_index_; @@ -29,12 +28,12 @@ bool CompositionOffsetIterator::AdvanceSample() { return true; } -bool CompositionOffsetIterator::IsValid() { - return iterator_ != composition_offset_table_.end() - && sample_index_ < iterator_->sample_count; +bool CompositionOffsetIterator::IsValid() const { + return iterator_ != composition_offset_table_.end() && + sample_index_ < iterator_->sample_count; } -uint32 CompositionOffsetIterator::SampleOffset(uint32 sample) { +uint32 CompositionOffsetIterator::SampleOffset(uint32 sample) const { uint32 current_sample = 0; std::vector::const_iterator it = composition_offset_table_.begin(); @@ -47,7 +46,7 @@ uint32 CompositionOffsetIterator::SampleOffset(uint32 sample) { return 0; } -uint32 CompositionOffsetIterator::NumSamples() { +uint32 CompositionOffsetIterator::NumSamples() const { uint32 num_samples = 0; std::vector::const_iterator it = composition_offset_table_.begin(); diff --git a/media/mp4/composition_offset_iterator.h b/media/mp4/composition_offset_iterator.h index 4d3f4d7cd2..9bd0e9802f 100644 --- a/media/mp4/composition_offset_iterator.h +++ b/media/mp4/composition_offset_iterator.h @@ -29,18 +29,16 @@ class CompositionOffsetIterator { bool AdvanceSample(); // Return whether the current sample is valid. - bool IsValid(); + bool IsValid() const; // Return sample offset for current sample. - uint32 sample_offset() { - return iterator_->sample_offset; - } + uint32 sample_offset() const { return iterator_->sample_offset; } // Return sample offset @ sample, 1-based. - uint32 SampleOffset(uint32 sample); + uint32 SampleOffset(uint32 sample) const; // Return total number of samples. - uint32 NumSamples(); + uint32 NumSamples() const; private: uint32 sample_index_; diff --git a/media/mp4/decoding_time_iterator.cc b/media/mp4/decoding_time_iterator.cc index 0a96d19826..6250a55f31 100644 --- a/media/mp4/decoding_time_iterator.cc +++ b/media/mp4/decoding_time_iterator.cc @@ -17,8 +17,7 @@ DecodingTimeIterator::DecodingTimeIterator( const DecodingTimeToSample& decoding_time_to_sample) : sample_index_(0), decoding_time_table_(decoding_time_to_sample.decoding_time), - iterator_(decoding_time_table_.begin()) { -} + iterator_(decoding_time_table_.begin()) {} bool DecodingTimeIterator::AdvanceSample() { ++sample_index_; @@ -31,13 +30,14 @@ bool DecodingTimeIterator::AdvanceSample() { return true; } -bool DecodingTimeIterator::IsValid() { - return iterator_ != decoding_time_table_.end() - && sample_index_ < iterator_->sample_count; +bool DecodingTimeIterator::IsValid() const { + return iterator_ != decoding_time_table_.end() && + sample_index_ < iterator_->sample_count; } -uint64 DecodingTimeIterator::Duration(uint32 start_sample, uint32 end_sample) { - DCHECK(start_sample <= end_sample); +uint64 DecodingTimeIterator::Duration(uint32 start_sample, + uint32 end_sample) const { + DCHECK_LE(start_sample, end_sample); uint32 current_sample = 0; uint32 prev_sample = 0; uint64 duration = 0; @@ -45,8 +45,9 @@ uint64 DecodingTimeIterator::Duration(uint32 start_sample, uint32 end_sample) { for (; it != decoding_time_table_.end(); ++it) { current_sample += it->sample_count; if (current_sample >= start_sample) { - duration += (std::min(end_sample, current_sample) - - std::max(start_sample, prev_sample + 1) + 1) * it->sample_delta; + duration += (std::min(end_sample, current_sample) - + std::max(start_sample, prev_sample + 1) + 1) * + it->sample_delta; if (current_sample >= end_sample) break; } @@ -55,7 +56,7 @@ uint64 DecodingTimeIterator::Duration(uint32 start_sample, uint32 end_sample) { return duration; } -uint32 DecodingTimeIterator::NumSamples() { +uint32 DecodingTimeIterator::NumSamples() const { uint32 num_samples = 0; std::vector::const_iterator it = decoding_time_table_.begin(); for (; it != decoding_time_table_.end(); ++it) { diff --git a/media/mp4/decoding_time_iterator.h b/media/mp4/decoding_time_iterator.h index 4333a88623..4ee7018b40 100644 --- a/media/mp4/decoding_time_iterator.h +++ b/media/mp4/decoding_time_iterator.h @@ -29,18 +29,16 @@ class DecodingTimeIterator { bool AdvanceSample(); // Return whether the current sample is valid. - bool IsValid(); + bool IsValid() const; // Return sample delta for current sample. - uint32 sample_delta() { - return iterator_->sample_delta; - } + uint32 sample_delta() const { return iterator_->sample_delta; } // Return duration from start_sample to end_sample, both 1-based, inclusive. - uint64 Duration(uint32 start_sample, uint32 end_sample); + uint64 Duration(uint32 start_sample, uint32 end_sample) const; // Return total number of samples in the table. - uint32 NumSamples(); + uint32 NumSamples() const; private: uint32 sample_index_; diff --git a/media/mp4/sync_sample_iterator.cc b/media/mp4/sync_sample_iterator.cc index b284f22891..c68a297d63 100644 --- a/media/mp4/sync_sample_iterator.cc +++ b/media/mp4/sync_sample_iterator.cc @@ -15,8 +15,7 @@ SyncSampleIterator::SyncSampleIterator(const SyncSample& sync_sample) : sample_number_(1), sync_sample_vector_(sync_sample.sample_number), iterator_(sync_sample_vector_.begin()), - is_empty_(iterator_ == sync_sample_vector_.end()) { -} + is_empty_(iterator_ == sync_sample_vector_.end()) {} bool SyncSampleIterator::AdvanceSample() { if (iterator_ != sync_sample_vector_.end() && sample_number_ == *iterator_) @@ -25,19 +24,19 @@ bool SyncSampleIterator::AdvanceSample() { return true; } -bool SyncSampleIterator::IsSyncSample() { +bool SyncSampleIterator::IsSyncSample() const { // If the sync sample box is not present, every sample is a sync sample. if (is_empty_) return true; return iterator_ != sync_sample_vector_.end() && sample_number_ == *iterator_; } -bool SyncSampleIterator::IsSyncSample(uint32 sample) { +bool SyncSampleIterator::IsSyncSample(uint32 sample) const { // If the sync sample box is not present, every sample is a sync sample. if (is_empty_) return true; - return std::binary_search(sync_sample_vector_.begin(), - sync_sample_vector_.end(), sample); + return std::binary_search( + sync_sample_vector_.begin(), sync_sample_vector_.end(), sample); } } // namespace mp4 diff --git a/media/mp4/sync_sample_iterator.h b/media/mp4/sync_sample_iterator.h index a59694f351..71d479ec3b 100644 --- a/media/mp4/sync_sample_iterator.h +++ b/media/mp4/sync_sample_iterator.h @@ -27,10 +27,10 @@ class SyncSampleIterator { bool AdvanceSample(); // Return whether the current sample is a sync sample. - bool IsSyncSample(); + bool IsSyncSample() const; // Return whether sample (1-based) is a sync sample. - bool IsSyncSample(uint32 sample); + bool IsSyncSample(uint32 sample) const; private: uint32 sample_number_;