OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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/mpeg2/mpeg2ts_psi.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "media/base/bit_reader.h" | |
9 #include "media/mpeg2/mpeg2ts_crc.h" | |
10 | |
11 namespace media { | |
12 namespace mpeg2ts { | |
13 | |
14 Mpeg2TsPsiParser::Mpeg2TsPsiParser() | |
15 : wait_for_pusi_(true), | |
16 leading_bytes_to_discard_(0) { | |
17 } | |
18 | |
19 Mpeg2TsPsiParser::~Mpeg2TsPsiParser() { | |
20 } | |
21 | |
22 bool Mpeg2TsPsiParser::Parse(bool payload_unit_start_indicator, | |
23 const uint8* buf, int size) { | |
24 return ParseInternal(payload_unit_start_indicator, buf, size); | |
25 } | |
26 | |
27 bool Mpeg2TsPsiParser::ParseInternal(bool payload_unit_start_indicator, | |
damienv1
2013/09/10 04:10:02
Could remove ParseInternal function (since Parse i
damienv1
2013/09/10 21:03:48
Done.
| |
28 const uint8* buf, int size) { | |
29 // Ignore partial PSI. | |
30 if (wait_for_pusi_ && !payload_unit_start_indicator) | |
31 return true; | |
32 | |
33 if (payload_unit_start_indicator) { | |
34 // Reset the state of the PSI section. | |
35 ResetPsiState(); | |
36 | |
37 // Update the state. | |
38 wait_for_pusi_ = false; | |
39 DCHECK_GE(size, 1); | |
40 int pointer_field = buf[0]; | |
41 leading_bytes_to_discard_ = pointer_field; | |
42 buf++; | |
43 size--; | |
44 } | |
45 | |
46 // Discard some leading bytes if needed. | |
47 if (leading_bytes_to_discard_ > 0) { | |
48 int nbytes_to_discard = std::min(leading_bytes_to_discard_, size); | |
49 buf += nbytes_to_discard; | |
50 size -= nbytes_to_discard; | |
51 leading_bytes_to_discard_ -= nbytes_to_discard; | |
52 } | |
53 if (size == 0) | |
54 return true; | |
55 | |
56 // Add the data to the parser state. | |
57 psi_byte_queue_.Push(buf, size); | |
58 int raw_psi_size; | |
59 const uint8* raw_psi; | |
60 psi_byte_queue_.Peek(&raw_psi, &raw_psi_size); | |
61 | |
62 // Check whether we have enough data to start parsing. | |
63 if (raw_psi_size < 3) | |
64 return true; | |
65 int section_length = | |
66 ((static_cast<int>(raw_psi[1]) << 8) | | |
67 (static_cast<int>(raw_psi[2]))) & 0xfff; | |
68 if (section_length >= 1021) | |
69 return false; | |
70 int psi_length = section_length + 3; | |
71 if (raw_psi_size < psi_length) { | |
72 // Don't throw an error when there is not enough data, | |
73 // just wait for more data to come. | |
74 return true; | |
75 } | |
76 | |
77 // There should not be any trailing bytes after a PMT. | |
78 // Instead, the pointer field should be used to stuff bytes. | |
79 DVLOG_IF(1, raw_psi_size > psi_length) | |
80 << "Trailing bytes after a PSI section: " | |
81 << psi_length << " vs " << raw_psi_size; | |
82 | |
83 // Verify the CRC. | |
84 Mpeg2TsCrc crc; | |
85 for (int k = 0; k < psi_length; k++) | |
86 crc.Update(raw_psi[k]); | |
87 if (!crc.IsValid()) | |
88 return false; | |
89 | |
90 // Parse the PSI section. | |
91 BitReader bit_reader(raw_psi, raw_psi_size); | |
92 bool status = ParsePsiSection(&bit_reader); | |
93 if (status) | |
94 ResetPsiState(); | |
95 | |
96 return status; | |
97 } | |
98 | |
99 void Mpeg2TsPsiParser::Flush() { | |
100 } | |
101 | |
102 void Mpeg2TsPsiParser::Reset() { | |
103 ResetPsiSection(); | |
104 ResetPsiState(); | |
105 } | |
106 | |
107 void Mpeg2TsPsiParser::ResetPsiState() { | |
108 wait_for_pusi_ = true; | |
109 psi_byte_queue_.Reset(); | |
110 leading_bytes_to_discard_ = 0; | |
111 } | |
112 | |
113 } // namespace mpeg2ts | |
114 } // namespace media | |
115 | |
OLD | NEW |