Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: media/formats/mp2t/timestamp_unroller.cc

Issue 539343002: Make the timestamp unroll offset consistent accross PES pids. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Prevent negative timestamps. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 min_unrolled_timestamp_(0) {
16 }
17
18 TimestampUnroller::~TimestampUnroller() {
19 }
20
21 int64 TimestampUnroller::GetUnrolledTimestamp(int64 timestamp) {
22 // Mpeg2 TS timestamps have an accuracy of 33 bits.
23 const int nbits = 33;
24
25 // |timestamp| has a precision of |nbits|
26 // so make sure the highest bits are set to 0.
27 DCHECK_EQ((timestamp >> nbits), 0);
28
29 if (!is_previous_timestamp_valid_) {
30 previous_unrolled_timestamp_ = timestamp;
31 min_unrolled_timestamp_ = timestamp;
32 is_previous_timestamp_valid_ = true;
33 return timestamp;
34 }
35
36 // Consider 3 possibilities to estimate the missing high bits of |time|.
37 int64 previous_unrolled_time_high =
38 (previous_unrolled_timestamp_ >> nbits);
39 int64 time0 = ((previous_unrolled_time_high - 1) << nbits) | timestamp;
40 int64 time1 = ((previous_unrolled_time_high + 0) << nbits) | timestamp;
41 int64 time2 = ((previous_unrolled_time_high + 1) << nbits) | timestamp;
42
43 // Select the min absolute difference with the current time
44 // so as to ensure time continuity.
45 int64 diff0 = time0 - previous_unrolled_timestamp_;
46 int64 diff1 = time1 - previous_unrolled_timestamp_;
47 int64 diff2 = time2 - previous_unrolled_timestamp_;
48 if (diff0 < 0)
49 diff0 = -diff0;
50 if (diff1 < 0)
51 diff1 = -diff1;
52 if (diff2 < 0)
53 diff2 = -diff2;
54
55 int64 unrolled_time;
56 int64 min_diff;
57 if (diff1 < diff0) {
58 unrolled_time = time1;
59 min_diff = diff1;
60 } else {
61 unrolled_time = time0;
62 min_diff = diff0;
63 }
64 if (diff2 < min_diff)
65 unrolled_time = time2;
66
67 // Update the state of the timestamp unroller.
68 previous_unrolled_timestamp_ = unrolled_time;
69 if (unrolled_time < min_unrolled_timestamp_)
70 min_unrolled_timestamp_ = unrolled_time;
71
72 return unrolled_time;
73 }
74
75 void TimestampUnroller::Reset() {
76 is_previous_timestamp_valid_ = false;
77 previous_unrolled_timestamp_ = 0;
78 min_unrolled_timestamp_ = 0;
79 }
80
81 } // namespace mp2t
82 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698