| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/formats/mp2t/timestamp_unroller.h" | 5 #include "media/formats/mp2t/timestamp_unroller.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 namespace media { | 9 namespace media { |
| 10 namespace mp2t { | 10 namespace mp2t { |
| 11 | 11 |
| 12 TimestampUnroller::TimestampUnroller() | 12 TimestampUnroller::TimestampUnroller() |
| 13 : is_previous_timestamp_valid_(false), | 13 : is_previous_timestamp_valid_(false), |
| 14 previous_unrolled_timestamp_(0) { | 14 previous_unrolled_timestamp_(0) { |
| 15 } | 15 } |
| 16 | 16 |
| 17 TimestampUnroller::~TimestampUnroller() { | 17 TimestampUnroller::~TimestampUnroller() { |
| 18 } | 18 } |
| 19 | 19 |
| 20 int64 TimestampUnroller::GetUnrolledTimestamp(int64 timestamp) { | 20 int64_t TimestampUnroller::GetUnrolledTimestamp(int64_t timestamp) { |
| 21 // Mpeg2 TS timestamps have an accuracy of 33 bits. | 21 // Mpeg2 TS timestamps have an accuracy of 33 bits. |
| 22 const int nbits = 33; | 22 const int nbits = 33; |
| 23 | 23 |
| 24 // |timestamp| has a precision of |nbits| | 24 // |timestamp| has a precision of |nbits| |
| 25 // so make sure the highest bits are set to 0. | 25 // so make sure the highest bits are set to 0. |
| 26 DCHECK_EQ((timestamp >> nbits), 0); | 26 DCHECK_EQ((timestamp >> nbits), 0); |
| 27 | 27 |
| 28 if (!is_previous_timestamp_valid_) { | 28 if (!is_previous_timestamp_valid_) { |
| 29 previous_unrolled_timestamp_ = timestamp; | 29 previous_unrolled_timestamp_ = timestamp; |
| 30 is_previous_timestamp_valid_ = true; | 30 is_previous_timestamp_valid_ = true; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 41 // of 2^33 as the one used for the previous timestamp) | 41 // of 2^33 as the one used for the previous timestamp) |
| 42 // 2) t0 = t1 - 2^33 | 42 // 2) t0 = t1 - 2^33 |
| 43 // 3) t2 = t1 + 2^33 | 43 // 3) t2 = t1 + 2^33 |
| 44 // | 44 // |
| 45 // A few remarks: | 45 // A few remarks: |
| 46 // - the purpose of the timestamp unroller is only to unroll timestamps | 46 // - the purpose of the timestamp unroller is only to unroll timestamps |
| 47 // in such a way timestamp continuity is satisfied. It can generate negative | 47 // in such a way timestamp continuity is satisfied. It can generate negative |
| 48 // values during that process. | 48 // values during that process. |
| 49 // - possible overflows are not considered here since 64 bits on a 90kHz | 49 // - possible overflows are not considered here since 64 bits on a 90kHz |
| 50 // timescale is way enough to represent several years of playback. | 50 // timescale is way enough to represent several years of playback. |
| 51 int64 previous_unrolled_time_high = | 51 int64_t previous_unrolled_time_high = (previous_unrolled_timestamp_ >> nbits); |
| 52 (previous_unrolled_timestamp_ >> nbits); | 52 int64_t time0 = ((previous_unrolled_time_high - 1) << nbits) | timestamp; |
| 53 int64 time0 = ((previous_unrolled_time_high - 1) << nbits) | timestamp; | 53 int64_t time1 = ((previous_unrolled_time_high + 0) << nbits) | timestamp; |
| 54 int64 time1 = ((previous_unrolled_time_high + 0) << nbits) | timestamp; | 54 int64_t time2 = ((previous_unrolled_time_high + 1) << nbits) | timestamp; |
| 55 int64 time2 = ((previous_unrolled_time_high + 1) << nbits) | timestamp; | |
| 56 | 55 |
| 57 // Select the min absolute difference with the current time | 56 // Select the min absolute difference with the current time |
| 58 // so as to ensure time continuity. | 57 // so as to ensure time continuity. |
| 59 int64 diff0 = time0 - previous_unrolled_timestamp_; | 58 int64_t diff0 = time0 - previous_unrolled_timestamp_; |
| 60 int64 diff1 = time1 - previous_unrolled_timestamp_; | 59 int64_t diff1 = time1 - previous_unrolled_timestamp_; |
| 61 int64 diff2 = time2 - previous_unrolled_timestamp_; | 60 int64_t diff2 = time2 - previous_unrolled_timestamp_; |
| 62 if (diff0 < 0) | 61 if (diff0 < 0) |
| 63 diff0 = -diff0; | 62 diff0 = -diff0; |
| 64 if (diff1 < 0) | 63 if (diff1 < 0) |
| 65 diff1 = -diff1; | 64 diff1 = -diff1; |
| 66 if (diff2 < 0) | 65 if (diff2 < 0) |
| 67 diff2 = -diff2; | 66 diff2 = -diff2; |
| 68 | 67 |
| 69 int64 unrolled_time; | 68 int64_t unrolled_time; |
| 70 int64 min_diff; | 69 int64_t min_diff; |
| 71 if (diff1 < diff0) { | 70 if (diff1 < diff0) { |
| 72 unrolled_time = time1; | 71 unrolled_time = time1; |
| 73 min_diff = diff1; | 72 min_diff = diff1; |
| 74 } else { | 73 } else { |
| 75 unrolled_time = time0; | 74 unrolled_time = time0; |
| 76 min_diff = diff0; | 75 min_diff = diff0; |
| 77 } | 76 } |
| 78 if (diff2 < min_diff) | 77 if (diff2 < min_diff) |
| 79 unrolled_time = time2; | 78 unrolled_time = time2; |
| 80 | 79 |
| 81 // Update the state of the timestamp unroller. | 80 // Update the state of the timestamp unroller. |
| 82 previous_unrolled_timestamp_ = unrolled_time; | 81 previous_unrolled_timestamp_ = unrolled_time; |
| 83 | 82 |
| 84 return unrolled_time; | 83 return unrolled_time; |
| 85 } | 84 } |
| 86 | 85 |
| 87 void TimestampUnroller::Reset() { | 86 void TimestampUnroller::Reset() { |
| 88 is_previous_timestamp_valid_ = false; | 87 is_previous_timestamp_valid_ = false; |
| 89 previous_unrolled_timestamp_ = 0; | 88 previous_unrolled_timestamp_ = 0; |
| 90 } | 89 } |
| 91 | 90 |
| 92 } // namespace mp2t | 91 } // namespace mp2t |
| 93 } // namespace media | 92 } // namespace media |
| OLD | NEW |