diff options
| author | 2011-11-07 11:38:14 -0800 | |
|---|---|---|
| committer | 2011-11-07 11:38:14 -0800 | |
| commit | 43b28298a63d7aa3155353287f773df71cda3be4 (patch) | |
| tree | 07970b378da086695f15fca4a28cf616d4dabf0a | |
| parent | 3fb5048b02b01b6f5edc517ad5fb8aec79b67751 (diff) | |
Use binary search to discover closest sync sample index, replace
assertions with runtime errors in case the file's table of sync sample
indices is not sorted properly.
Change-Id: Ie4446a44e613a8d329ac680c37361d4407d22520
related-to-bug: 5549855
| -rw-r--r-- | media/libstagefright/SampleTable.cpp | 49 |
1 files changed, 25 insertions, 24 deletions
diff --git a/media/libstagefright/SampleTable.cpp b/media/libstagefright/SampleTable.cpp index 1451c166ec64..8e790fc48dbd 100644 --- a/media/libstagefright/SampleTable.cpp +++ b/media/libstagefright/SampleTable.cpp @@ -618,27 +618,26 @@ status_t SampleTable::findSyncSampleNear( } uint32_t left = 0; - while (left < mNumSyncSamples) { - uint32_t x = mSyncSamples[left]; + uint32_t right = mNumSyncSamples; + while (left < right) { + uint32_t center = left + (right - left) / 2; + uint32_t x = mSyncSamples[center]; - if (x >= start_sample_index) { + if (start_sample_index < x) { + right = center; + } else if (start_sample_index > x) { + left = center + 1; + } else { + left = center; break; } - - ++left; - } - if (left > 0) { - --left; } - uint32_t x; - if (mDataSource->readAt( - mSyncSampleOffset + 8 + left * 4, &x, 4) != 4) { - return ERROR_IO; - } + // Now ssi[left] is the sync sample index just before (or at) + // start_sample_index. + // Also start_sample_index < ssi[left + 1], if left + 1 < mNumSyncSamples. - x = ntohl(x); - --x; + uint32_t x = mSyncSamples[left]; if (left + 1 < mNumSyncSamples) { uint32_t y = mSyncSamples[left + 1]; @@ -679,15 +678,13 @@ status_t SampleTable::findSyncSampleNear( if (x > start_sample_index) { CHECK(left > 0); - if (mDataSource->readAt( - mSyncSampleOffset + 8 + (left - 1) * 4, &x, 4) != 4) { - return ERROR_IO; - } + x = mSyncSamples[left - 1]; - x = ntohl(x); - --x; - - CHECK(x <= start_sample_index); + if (x > start_sample_index) { + // The table of sync sample indices was not sorted + // properly. + return ERROR_MALFORMED; + } } break; } @@ -701,7 +698,11 @@ status_t SampleTable::findSyncSampleNear( x = mSyncSamples[left + 1]; - CHECK(x >= start_sample_index); + if (x < start_sample_index) { + // The table of sync sample indices was not sorted + // properly. + return ERROR_MALFORMED; + } } break; |