diff options
author | kqyang@chromium.org <kqyang@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-08 04:38:06 +0000 |
---|---|---|
committer | kqyang@chromium.org <kqyang@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-08 04:38:06 +0000 |
commit | f56428e5470d87c518715fac596a556d9f2b89e7 (patch) | |
tree | 049c565a0c62bec9b9074abc3f9a0ea5402f9781 /media | |
parent | f86201baaca2d61774bd2433ce49645e7807d7c2 (diff) | |
download | chromium_src-f56428e5470d87c518715fac596a556d9f2b89e7.zip chromium_src-f56428e5470d87c518715fac596a556d9f2b89e7.tar.gz chromium_src-f56428e5470d87c518715fac596a556d9f2b89e7.tar.bz2 |
Add a wrapper class for SampleToGroup iterator
SampleToGroupIterator provides convenient functions to iterate through
the compressed SampleToGroup table.
BUG=367366
Review URL: https://codereview.chromium.org/263763006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269031 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media')
-rw-r--r-- | media/formats/mp4/sample_to_group_iterator.cc | 47 | ||||
-rw-r--r-- | media/formats/mp4/sample_to_group_iterator.h | 49 | ||||
-rw-r--r-- | media/formats/mp4/sample_to_group_iterator_unittest.cc | 65 | ||||
-rw-r--r-- | media/media.gyp | 3 |
4 files changed, 164 insertions, 0 deletions
diff --git a/media/formats/mp4/sample_to_group_iterator.cc b/media/formats/mp4/sample_to_group_iterator.cc new file mode 100644 index 0000000..01c7072 --- /dev/null +++ b/media/formats/mp4/sample_to_group_iterator.cc @@ -0,0 +1,47 @@ +// Copyright 2014 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/sample_to_group_iterator.h" + +#include "base/logging.h" + +namespace media { +namespace mp4 { + +SampleToGroupIterator::SampleToGroupIterator( + const SampleToGroup& sample_to_group) + : remaining_samples_(0), + sample_to_group_table_(sample_to_group.entries), + iterator_(sample_to_group_table_.begin()) { + // Handle the case that the table contains an entry with sample count 0. + while (iterator_ != sample_to_group_table_.end()) { + remaining_samples_ = iterator_->sample_count; + if (remaining_samples_ > 0) + break; + ++iterator_; + } +} + +SampleToGroupIterator::~SampleToGroupIterator() {} + +bool SampleToGroupIterator::Advance() { + DCHECK(IsValid()); + + --remaining_samples_; + // Handle the case that the table contains an entry with sample count 0. + while (remaining_samples_ == 0) { + ++iterator_; + if (iterator_ == sample_to_group_table_.end()) + return false; + remaining_samples_ = iterator_->sample_count; + } + return true; +} + +bool SampleToGroupIterator::IsValid() const { + return remaining_samples_ > 0; +} + +} // namespace mp4 +} // namespace media diff --git a/media/formats/mp4/sample_to_group_iterator.h b/media/formats/mp4/sample_to_group_iterator.h new file mode 100644 index 0000000..c2ea60f --- /dev/null +++ b/media/formats/mp4/sample_to_group_iterator.h @@ -0,0 +1,49 @@ +// Copyright 2014 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. + +#ifndef MEDIA_FORMATS_MP4_SAMPLE_TO_GROUP_ITERATOR_H_ +#define MEDIA_FORMATS_MP4_SAMPLE_TO_GROUP_ITERATOR_H_ + +#include <vector> + +#include "media/formats/mp4/box_definitions.h" + +namespace media { +namespace mp4 { + +// Sample To Group Box ('sbgp') can be used to find the group that a sample +// belongs to and the associated description of that sample group. It is +// compactly coded though. This class implements the iterator to iterate +// through the compressed table to get the associated sample group description +// index. +class MEDIA_EXPORT SampleToGroupIterator { + public: + explicit SampleToGroupIterator(const SampleToGroup& sample_to_group); + ~SampleToGroupIterator(); + + // Advances the iterator to refer to the next sample. Return status + // indicating whether the sample is still valid. + bool Advance(); + + // Returns whether the current sample is valid. + bool IsValid() const; + + // Returns group description index for current sample. + uint32 group_description_index() const { + return iterator_->group_description_index; + } + + private: + // Track how many samples remaining for current table entry. + uint32 remaining_samples_; + const std::vector<SampleToGroupEntry>& sample_to_group_table_; + std::vector<SampleToGroupEntry>::const_iterator iterator_; + + DISALLOW_COPY_AND_ASSIGN(SampleToGroupIterator); +}; + +} // namespace mp4 +} // namespace media + +#endif // MEDIA_FORMATS_MP4_SAMPLE_TO_GROUP_ITERATOR_H_ diff --git a/media/formats/mp4/sample_to_group_iterator_unittest.cc b/media/formats/mp4/sample_to_group_iterator_unittest.cc new file mode 100644 index 0000000..3e8148c --- /dev/null +++ b/media/formats/mp4/sample_to_group_iterator_unittest.cc @@ -0,0 +1,65 @@ +// Copyright 2014 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/sample_to_group_iterator.h" + +#include "base/memory/scoped_ptr.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace media { +namespace mp4 { + +namespace { +const SampleToGroupEntry kCompactSampleToGroupTable[] = + {{10, 8}, {9, 5}, {25, 7}, {48, 63}, {8, 2}}; +} // namespace + +class SampleToGroupIteratorTest : public testing::Test { + public: + SampleToGroupIteratorTest() { + // Build sample group description index table from kSampleToGroupTable. + for (size_t i = 0; i < arraysize(kCompactSampleToGroupTable); ++i) { + for (uint32 j = 0; j < kCompactSampleToGroupTable[i].sample_count; ++j) { + sample_to_group_table_.push_back( + kCompactSampleToGroupTable[i].group_description_index); + } + } + + sample_to_group_.entries.assign( + kCompactSampleToGroupTable, + kCompactSampleToGroupTable + arraysize(kCompactSampleToGroupTable)); + sample_to_group_iterator_.reset( + new SampleToGroupIterator(sample_to_group_)); + } + + protected: + std::vector<uint32> sample_to_group_table_; + SampleToGroup sample_to_group_; + scoped_ptr<SampleToGroupIterator> sample_to_group_iterator_; + + private: + DISALLOW_COPY_AND_ASSIGN(SampleToGroupIteratorTest); +}; + +TEST_F(SampleToGroupIteratorTest, EmptyTable) { + SampleToGroup sample_to_group; + SampleToGroupIterator iterator(sample_to_group); + EXPECT_FALSE(iterator.IsValid()); +} + +TEST_F(SampleToGroupIteratorTest, Advance) { + ASSERT_EQ(sample_to_group_table_[0], + sample_to_group_iterator_->group_description_index()); + for (uint32 sample = 1; sample < sample_to_group_table_.size(); ++sample) { + ASSERT_TRUE(sample_to_group_iterator_->Advance()); + ASSERT_EQ(sample_to_group_table_[sample], + sample_to_group_iterator_->group_description_index()); + ASSERT_TRUE(sample_to_group_iterator_->IsValid()); + } + ASSERT_FALSE(sample_to_group_iterator_->Advance()); + ASSERT_FALSE(sample_to_group_iterator_->IsValid()); +} + +} // namespace mp4 +} // namespace media diff --git a/media/media.gyp b/media/media.gyp index 9da80d8..a799028 100644 --- a/media/media.gyp +++ b/media/media.gyp @@ -881,6 +881,8 @@ 'formats/mp4/es_descriptor.h', 'formats/mp4/mp4_stream_parser.cc', 'formats/mp4/mp4_stream_parser.h', + 'formats/mp4/sample_to_group_iterator.cc', + 'formats/mp4/sample_to_group_iterator.h', 'formats/mp4/track_run_iterator.cc', 'formats/mp4/track_run_iterator.h', 'formats/mpeg/adts_constants.cc', @@ -1175,6 +1177,7 @@ 'formats/mp4/box_reader_unittest.cc', 'formats/mp4/es_descriptor_unittest.cc', 'formats/mp4/mp4_stream_parser_unittest.cc', + 'formats/mp4/sample_to_group_iterator_unittest.cc', 'formats/mp4/track_run_iterator_unittest.cc', 'formats/mpeg/adts_stream_parser_unittest.cc', 'formats/mpeg/mp3_stream_parser_unittest.cc', |