OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/webm/webm_cluster_parser.h" | 5 #include "media/webm/webm_cluster_parser.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "media/base/data_buffer.h" | 8 #include "media/base/data_buffer.h" |
9 #include "media/webm/webm_constants.h" | 9 #include "media/webm/webm_constants.h" |
10 | 10 |
11 namespace media { | 11 namespace media { |
12 | 12 |
13 static Buffer* CreateBuffer(const uint8* data, size_t size) { | 13 static Buffer* CreateBuffer(const uint8* data, size_t size) { |
14 scoped_array<uint8> buf(new uint8[size]); | 14 scoped_array<uint8> buf(new uint8[size]); |
15 memcpy(buf.get(), data, size); | 15 memcpy(buf.get(), data, size); |
16 return new DataBuffer(buf.release(), size); | 16 return new DataBuffer(buf.release(), size); |
17 } | 17 } |
18 | 18 |
19 WebMClusterParser::WebMClusterParser(int64 timecode_scale, | 19 WebMClusterParser::WebMClusterParser(int64 timecode_scale, |
20 int audio_track_num, | 20 int audio_track_num, |
21 base::TimeDelta audio_default_duration, | 21 base::TimeDelta audio_default_duration, |
22 int video_track_num, | 22 int video_track_num, |
23 base::TimeDelta video_default_duration) | 23 base::TimeDelta video_default_duration) |
24 : timecode_multiplier_(timecode_scale / 1000.0), | 24 : timecode_multiplier_(timecode_scale / 1000.0), |
25 audio_track_num_(audio_track_num), | 25 audio_track_num_(audio_track_num), |
26 audio_default_duration_(audio_default_duration), | 26 audio_default_duration_(audio_default_duration), |
27 video_track_num_(video_track_num), | 27 video_track_num_(video_track_num), |
28 video_default_duration_(video_default_duration), | 28 video_default_duration_(video_default_duration), |
29 parser_(kWebMIdCluster), | |
30 last_block_timecode_(-1), | 29 last_block_timecode_(-1), |
31 cluster_timecode_(-1) { | 30 cluster_timecode_(-1) { |
32 } | 31 } |
33 | 32 |
34 WebMClusterParser::~WebMClusterParser() {} | 33 WebMClusterParser::~WebMClusterParser() {} |
35 | 34 |
36 int WebMClusterParser::Parse(const uint8* buf, int size) { | 35 int WebMClusterParser::Parse(const uint8* buf, int size) { |
| 36 last_block_timecode_ = -1; |
| 37 cluster_timecode_ = -1; |
37 audio_buffers_.clear(); | 38 audio_buffers_.clear(); |
38 video_buffers_.clear(); | 39 video_buffers_.clear(); |
39 | 40 |
40 int result = parser_.Parse(buf, size, this); | 41 return WebMParseListElement(buf, size, kWebMIdCluster, 1, this); |
41 | |
42 if (result <= 0) | |
43 return result; | |
44 | |
45 if (parser_.IsParsingComplete()) { | |
46 // Reset the parser if we're done parsing so that | |
47 // it is ready to accept another cluster on the next | |
48 // call. | |
49 parser_.Reset(); | |
50 | |
51 last_block_timecode_ = -1; | |
52 cluster_timecode_ = -1; | |
53 } | |
54 | |
55 return result; | |
56 } | 42 } |
57 | 43 |
58 bool WebMClusterParser::OnListStart(int id) { | 44 bool WebMClusterParser::OnListStart(int id) { |
59 if (id == kWebMIdCluster) | 45 if (id == kWebMIdCluster) |
60 cluster_timecode_ = -1; | 46 cluster_timecode_ = -1; |
61 | 47 |
62 return true; | 48 return true; |
63 } | 49 } |
64 | 50 |
65 bool WebMClusterParser::OnListEnd(int id) { | 51 bool WebMClusterParser::OnListEnd(int id) { |
66 if (id == kWebMIdCluster) | 52 if (id == kWebMIdCluster) |
67 cluster_timecode_ = -1; | 53 cluster_timecode_ = -1; |
68 | 54 |
69 return true; | 55 return true; |
70 } | 56 } |
71 | 57 |
72 bool WebMClusterParser::OnUInt(int id, int64 val) { | 58 bool WebMClusterParser::OnUInt(int id, int64 val) { |
73 if (id == kWebMIdTimecode) { | 59 if (id == kWebMIdTimecode) { |
74 if (cluster_timecode_ != -1) | 60 if (cluster_timecode_ != -1) |
75 return false; | 61 return false; |
76 | 62 |
77 cluster_timecode_ = val; | 63 cluster_timecode_ = val; |
78 } | 64 } |
79 | 65 |
80 return true; | 66 return true; |
81 } | 67 } |
82 | 68 |
83 bool WebMClusterParser::OnFloat(int id, double val) { | 69 bool WebMClusterParser::OnFloat(int id, double val) { |
84 DVLOG(1) << "Unexpected float element with ID " << std::hex << id; | 70 VLOG(1) << "Unexpected float element with ID " << std::hex << id; |
85 return false; | 71 return false; |
86 } | 72 } |
87 | 73 |
88 bool WebMClusterParser::OnBinary(int id, const uint8* data, int size) { | 74 bool WebMClusterParser::OnBinary(int id, const uint8* data, int size) { |
89 DVLOG(1) << "Unexpected binary element with ID " << std::hex << id; | 75 VLOG(1) << "Unexpected binary element with ID " << std::hex << id; |
90 return false; | 76 return false; |
91 } | 77 } |
92 | 78 |
93 bool WebMClusterParser::OnString(int id, const std::string& str) { | 79 bool WebMClusterParser::OnString(int id, const std::string& str) { |
94 DVLOG(1) << "Unexpected string element with ID " << std::hex << id; | 80 VLOG(1) << "Unexpected string element with ID " << std::hex << id; |
95 return false; | 81 return false; |
96 } | 82 } |
97 | 83 |
98 bool WebMClusterParser::OnSimpleBlock(int track_num, int timecode, | 84 bool WebMClusterParser::OnSimpleBlock(int track_num, int timecode, |
99 int flags, | 85 int flags, |
100 const uint8* data, int size) { | 86 const uint8* data, int size) { |
101 if (cluster_timecode_ == -1) { | 87 if (cluster_timecode_ == -1) { |
102 DVLOG(1) << "Got SimpleBlock before cluster timecode."; | 88 VLOG(1) << "Got SimpleBlock before cluster timecode."; |
103 return false; | 89 return false; |
104 } | 90 } |
105 | 91 |
106 if (timecode < 0) { | 92 if (timecode < 0) { |
107 DVLOG(1) << "Got SimpleBlock with negative timecode offset " << timecode; | 93 VLOG(1) << "Got SimpleBlock with negative timecode offset " << timecode; |
108 return false; | 94 return false; |
109 } | 95 } |
110 | 96 |
111 if (last_block_timecode_ != -1 && timecode < last_block_timecode_) { | 97 if (last_block_timecode_ != -1 && timecode < last_block_timecode_) { |
112 DVLOG(1) << "Got SimpleBlock with a timecode before the previous block."; | 98 VLOG(1) << "Got SimpleBlock with a timecode before the previous block."; |
113 return false; | 99 return false; |
114 } | 100 } |
115 | 101 |
116 last_block_timecode_ = timecode; | 102 last_block_timecode_ = timecode; |
117 | 103 |
118 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds( | 104 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds( |
119 (cluster_timecode_ + timecode) * timecode_multiplier_); | 105 (cluster_timecode_ + timecode) * timecode_multiplier_); |
120 | 106 |
121 scoped_refptr<Buffer> buffer(CreateBuffer(data, size)); | 107 scoped_refptr<Buffer> buffer(CreateBuffer(data, size)); |
122 buffer->SetTimestamp(timestamp); | 108 buffer->SetTimestamp(timestamp); |
123 BufferQueue* queue = NULL; | 109 BufferQueue* queue = NULL; |
124 | 110 |
125 if (track_num == audio_track_num_) { | 111 if (track_num == audio_track_num_) { |
126 buffer->SetDuration(audio_default_duration_); | 112 buffer->SetDuration(audio_default_duration_); |
127 queue = &audio_buffers_; | 113 queue = &audio_buffers_; |
128 } else if (track_num == video_track_num_) { | 114 } else if (track_num == video_track_num_) { |
129 buffer->SetDuration(video_default_duration_); | 115 buffer->SetDuration(video_default_duration_); |
130 queue = &video_buffers_; | 116 queue = &video_buffers_; |
131 } else { | 117 } else { |
132 DVLOG(1) << "Unexpected track number " << track_num; | 118 VLOG(1) << "Unexpected track number " << track_num; |
133 return false; | 119 return false; |
134 } | 120 } |
135 | 121 |
136 if (!queue->empty() && | 122 if (!queue->empty() && |
137 buffer->GetTimestamp() == queue->back()->GetTimestamp()) { | 123 buffer->GetTimestamp() == queue->back()->GetTimestamp()) { |
138 DVLOG(1) << "Got SimpleBlock timecode is not strictly monotonically " | 124 VLOG(1) << "Got SimpleBlock timecode is not strictly monotonically " |
139 << "increasing for track " << track_num; | 125 << "increasing for track " << track_num; |
140 return false; | 126 return false; |
141 } | 127 } |
142 | 128 |
143 queue->push_back(buffer); | 129 queue->push_back(buffer); |
144 return true; | 130 return true; |
145 } | 131 } |
146 | 132 |
147 } // namespace media | 133 } // namespace media |
OLD | NEW |