OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/formats/mp2t/timestamp_unroller.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 namespace media { |
| 10 namespace mp2t { |
| 11 |
| 12 TimestampUnroller::TimestampUnroller() |
| 13 : is_previous_timestamp_valid_(false), |
| 14 previous_unrolled_timestamp_(0) { |
| 15 } |
| 16 |
| 17 TimestampUnroller::~TimestampUnroller() { |
| 18 } |
| 19 |
| 20 int64 TimestampUnroller::GetUnrolledTimestamp(int64 timestamp) { |
| 21 // Mpeg2 TS timestamps have an accuracy of 33 bits. |
| 22 const int nbits = 33; |
| 23 |
| 24 // |timestamp| has a precision of |nbits| |
| 25 // so make sure the highest bits are set to 0. |
| 26 DCHECK_EQ((timestamp >> nbits), 0); |
| 27 |
| 28 if (!is_previous_timestamp_valid_) { |
| 29 previous_unrolled_timestamp_ = timestamp; |
| 30 is_previous_timestamp_valid_ = true; |
| 31 return timestamp; |
| 32 } |
| 33 |
| 34 // Consider 3 possibilities to estimate the missing high bits of |time|. |
| 35 int64 previous_unrolled_time_high = |
| 36 (previous_unrolled_timestamp_ >> nbits); |
| 37 int64 time0 = ((previous_unrolled_time_high - 1) << nbits) | timestamp; |
| 38 int64 time1 = ((previous_unrolled_time_high + 0) << nbits) | timestamp; |
| 39 int64 time2 = ((previous_unrolled_time_high + 1) << nbits) | timestamp; |
| 40 |
| 41 // Select the min absolute difference with the current time |
| 42 // so as to ensure time continuity. |
| 43 int64 diff0 = time0 - previous_unrolled_timestamp_; |
| 44 int64 diff1 = time1 - previous_unrolled_timestamp_; |
| 45 int64 diff2 = time2 - previous_unrolled_timestamp_; |
| 46 if (diff0 < 0) |
| 47 diff0 = -diff0; |
| 48 if (diff1 < 0) |
| 49 diff1 = -diff1; |
| 50 if (diff2 < 0) |
| 51 diff2 = -diff2; |
| 52 |
| 53 int64 unrolled_time; |
| 54 int64 min_diff; |
| 55 if (diff1 < diff0) { |
| 56 unrolled_time = time1; |
| 57 min_diff = diff1; |
| 58 } else { |
| 59 unrolled_time = time0; |
| 60 min_diff = diff0; |
| 61 } |
| 62 if (diff2 < min_diff) |
| 63 unrolled_time = time2; |
| 64 |
| 65 // Update the state of the timestamp unroller. |
| 66 previous_unrolled_timestamp_ = unrolled_time; |
| 67 |
| 68 return unrolled_time; |
| 69 } |
| 70 |
| 71 void TimestampUnroller::Reset() { |
| 72 is_previous_timestamp_valid_ = false; |
| 73 previous_unrolled_timestamp_ = 0; |
| 74 } |
| 75 |
| 76 } // namespace mp2t |
| 77 } // namespace media |
OLD | NEW |