Clean up *_iterator code in media/mp4.

Make functions const if needed and reformat the code using clang-format.

Change-Id: If2848ef6c3b61df5358286687d80213533125856
This commit is contained in:
Kongqun Yang 2014-02-20 18:09:35 -08:00
parent 5dd4a8cb45
commit 0a8dc6f606
8 changed files with 55 additions and 62 deletions

View File

@ -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<ChunkInfo>::const_iterator it = chunk_info_table_.begin();
it != chunk_info_table_.end(); ++it) {
last_chunk = (
(it + 1 == chunk_info_table_.end()) ?
std::numeric_limits<uint32>::max() : (it + 1)->first_chunk) - 1;
it != chunk_info_table_.end();
++it) {
last_chunk = (it + 1 == chunk_info_table_.end())
? std::numeric_limits<uint32>::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;
}

View File

@ -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:

View File

@ -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<CompositionOffset>::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<CompositionOffset>::const_iterator it =
composition_offset_table_.begin();

View File

@ -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_;

View File

@ -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<DecodingTime>::const_iterator it = decoding_time_table_.begin();
for (; it != decoding_time_table_.end(); ++it) {

View File

@ -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_;

View File

@ -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

View File

@ -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_;