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.
5 #include "media/formats/mp2t/timestamp_unroller.h"
7 #include "base/logging.h"
12 TimestampUnroller::TimestampUnroller()
13 : is_previous_timestamp_valid_(false),
14 previous_unrolled_timestamp_(0) {
17 TimestampUnroller::~TimestampUnroller() {
20 int64
TimestampUnroller::GetUnrolledTimestamp(int64 timestamp
) {
21 // Mpeg2 TS timestamps have an accuracy of 33 bits.
24 // |timestamp| has a precision of |nbits|
25 // so make sure the highest bits are set to 0.
26 DCHECK_EQ((timestamp
>> nbits
), 0);
28 if (!is_previous_timestamp_valid_
) {
29 previous_unrolled_timestamp_
= timestamp
;
30 is_previous_timestamp_valid_
= true;
34 // |timestamp| is known modulo 2^33, so estimate the highest bits
35 // to minimize the discontinuity with the previous unrolled timestamp.
36 // Three possibilities are considered to estimate the missing high bits
37 // of |timestamp|. If the bits of the previous unrolled timestamp are
38 // {b63, b62, ..., b0} and bits of |timestamp| are {0, ..., 0, a32, ..., a0}
39 // then the 3 possibilities are:
40 // 1) t1 = {b63, ..., b33, a32, ..., a0} (apply the same offset multiple
41 // of 2^33 as the one used for the previous timestamp)
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
48 // values during that process.
49 // - possible overflows are not considered here since 64 bits on a 90kHz
50 // timescale is way enough to represent several years of playback.
51 int64 previous_unrolled_time_high
=
52 (previous_unrolled_timestamp_
>> nbits
);
53 int64 time0
= ((previous_unrolled_time_high
- 1) << nbits
) | timestamp
;
54 int64 time1
= ((previous_unrolled_time_high
+ 0) << nbits
) | timestamp
;
55 int64 time2
= ((previous_unrolled_time_high
+ 1) << nbits
) | timestamp
;
57 // Select the min absolute difference with the current time
58 // so as to ensure time continuity.
59 int64 diff0
= time0
- previous_unrolled_timestamp_
;
60 int64 diff1
= time1
- previous_unrolled_timestamp_
;
61 int64 diff2
= time2
- previous_unrolled_timestamp_
;
72 unrolled_time
= time1
;
75 unrolled_time
= time0
;
79 unrolled_time
= time2
;
81 // Update the state of the timestamp unroller.
82 previous_unrolled_timestamp_
= unrolled_time
;
87 void TimestampUnroller::Reset() {
88 is_previous_timestamp_valid_
= false;
89 previous_unrolled_timestamp_
= 0;