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/ts_section_pes.h"
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "media/base/bit_reader.h"
10 #include "media/base/buffers.h"
11 #include "media/formats/mp2t/es_parser.h"
12 #include "media/formats/mp2t/mp2t_common.h"
13 #include "media/formats/mp2t/timestamp_unroller.h"
15 static const int kPesStartCode
= 0x000001;
17 static bool IsTimestampSectionValid(int64 timestamp_section
) {
18 // |pts_section| has 40 bits:
19 // - starting with either '0010' or '0011' or '0001'
20 // - and ending with a marker bit.
21 // See ITU H.222 standard - PES section.
23 // Verify that all the marker bits are set to one.
24 return ((timestamp_section
& 0x1) != 0) &&
25 ((timestamp_section
& 0x10000) != 0) &&
26 ((timestamp_section
& 0x100000000) != 0);
29 static int64
ConvertTimestampSectionToTimestamp(int64 timestamp_section
) {
30 return (((timestamp_section
>> 33) & 0x7) << 30) |
31 (((timestamp_section
>> 17) & 0x7fff) << 15) |
32 (((timestamp_section
>> 1) & 0x7fff) << 0);
38 TsSectionPes::TsSectionPes(scoped_ptr
<EsParser
> es_parser
,
39 TimestampUnroller
* timestamp_unroller
)
40 : es_parser_(es_parser
.release()),
42 timestamp_unroller_(timestamp_unroller
) {
44 DCHECK(timestamp_unroller_
);
47 TsSectionPes::~TsSectionPes() {
50 bool TsSectionPes::Parse(bool payload_unit_start_indicator
,
51 const uint8
* buf
, int size
) {
52 // Ignore partial PES.
53 if (wait_for_pusi_
&& !payload_unit_start_indicator
)
56 bool parse_result
= true;
57 if (payload_unit_start_indicator
) {
58 // Try emitting a packet since we might have a pending PES packet
59 // with an undefined size.
60 // In this case, a unit is emitted when the next unit is coming.
63 pes_byte_queue_
.Peek(&raw_pes
, &raw_pes_size
);
65 parse_result
= Emit(true);
71 wait_for_pusi_
= false;
74 // Add the data to the parser state.
76 pes_byte_queue_
.Push(buf
, size
);
78 // Try emitting the current PES packet.
79 return (parse_result
&& Emit(false));
82 void TsSectionPes::Flush() {
83 // Try emitting a packet since we might have a pending PES packet
84 // with an undefined size.
87 // Flush the underlying ES parser.
91 void TsSectionPes::Reset() {
96 bool TsSectionPes::Emit(bool emit_for_unknown_size
) {
99 pes_byte_queue_
.Peek(&raw_pes
, &raw_pes_size
);
101 // A PES should be at least 6 bytes.
102 // Wait for more data to come if not enough bytes.
103 if (raw_pes_size
< 6)
106 // Check whether we have enough data to start parsing.
107 int pes_packet_length
=
108 (static_cast<int>(raw_pes
[4]) << 8) |
109 (static_cast<int>(raw_pes
[5]));
110 if ((pes_packet_length
== 0 && !emit_for_unknown_size
) ||
111 (pes_packet_length
!= 0 && raw_pes_size
< pes_packet_length
+ 6)) {
112 // Wait for more data to come either because:
113 // - there are not enough bytes,
114 // - or the PES size is unknown and the "force emit" flag is not set.
115 // (PES size might be unknown for video PES packet).
118 DVLOG(LOG_LEVEL_PES
) << "pes_packet_length=" << pes_packet_length
;
121 bool parse_result
= ParseInternal(raw_pes
, raw_pes_size
);
129 bool TsSectionPes::ParseInternal(const uint8
* raw_pes
, int raw_pes_size
) {
130 BitReader
bit_reader(raw_pes
, raw_pes_size
);
132 // Read up to the pes_packet_length (6 bytes).
133 int packet_start_code_prefix
;
135 int pes_packet_length
;
136 RCHECK(bit_reader
.ReadBits(24, &packet_start_code_prefix
));
137 RCHECK(bit_reader
.ReadBits(8, &stream_id
));
138 RCHECK(bit_reader
.ReadBits(16, &pes_packet_length
));
140 RCHECK(packet_start_code_prefix
== kPesStartCode
);
141 DVLOG(LOG_LEVEL_PES
) << "stream_id=" << std::hex
<< stream_id
<< std::dec
;
142 if (pes_packet_length
== 0)
143 pes_packet_length
= bit_reader
.bits_available() / 8;
145 // Ignore the PES for unknown stream IDs.
146 // See ITU H.222 Table 2-22 "Stream_id assignments"
147 bool is_audio_stream_id
= ((stream_id
& 0xe0) == 0xc0);
148 bool is_video_stream_id
= ((stream_id
& 0xf0) == 0xe0);
149 // According to ETSI DVB standard (ETSI TS 101 154) section 4.1.6.1
150 // AC-3 and DTS audio streams may have stream_id 0xbd. These streams
151 // have the same syntax as regular audio streams.
152 bool is_private_stream_1
= (stream_id
== 0xbd);
153 if (!is_audio_stream_id
&& !is_video_stream_id
&& !is_private_stream_1
) {
154 DVLOG(LOG_LEVEL_PES
) << "Dropped TsPacket for stream_id=0x"
155 << std::hex
<< stream_id
<< std::dec
;
159 // Read up to "pes_header_data_length".
161 int PES_scrambling_control
;
163 int data_alignment_indicator
;
165 int original_or_copy
;
169 int dsm_trick_mode_flag
;
170 int additional_copy_info_flag
;
172 int pes_extension_flag
;
173 int pes_header_data_length
;
174 RCHECK(bit_reader
.ReadBits(2, &dummy_2
));
175 RCHECK(dummy_2
== 0x2);
176 RCHECK(bit_reader
.ReadBits(2, &PES_scrambling_control
));
177 RCHECK(bit_reader
.ReadBits(1, &PES_priority
));
178 RCHECK(bit_reader
.ReadBits(1, &data_alignment_indicator
));
179 RCHECK(bit_reader
.ReadBits(1, ©right
));
180 RCHECK(bit_reader
.ReadBits(1, &original_or_copy
));
181 RCHECK(bit_reader
.ReadBits(2, &pts_dts_flags
));
182 RCHECK(bit_reader
.ReadBits(1, &escr_flag
));
183 RCHECK(bit_reader
.ReadBits(1, &es_rate_flag
));
184 RCHECK(bit_reader
.ReadBits(1, &dsm_trick_mode_flag
));
185 RCHECK(bit_reader
.ReadBits(1, &additional_copy_info_flag
));
186 RCHECK(bit_reader
.ReadBits(1, &pes_crc_flag
));
187 RCHECK(bit_reader
.ReadBits(1, &pes_extension_flag
));
188 RCHECK(bit_reader
.ReadBits(8, &pes_header_data_length
));
189 int pes_header_start_size
= bit_reader
.bits_available() / 8;
191 // Compute the size and the offset of the ES payload.
192 // "6" for the 6 bytes read before and including |pes_packet_length|.
193 // "3" for the 3 bytes read before and including |pes_header_data_length|.
194 int es_size
= pes_packet_length
- 3 - pes_header_data_length
;
195 int es_offset
= 6 + 3 + pes_header_data_length
;
196 RCHECK(es_size
>= 0);
197 RCHECK(es_offset
+ es_size
<= raw_pes_size
);
199 // Read the timing information section.
200 bool is_pts_valid
= false;
201 bool is_dts_valid
= false;
202 int64 pts_section
= 0;
203 int64 dts_section
= 0;
204 if (pts_dts_flags
== 0x2) {
205 RCHECK(bit_reader
.ReadBits(40, &pts_section
));
206 RCHECK((((pts_section
>> 36) & 0xf) == 0x2) &&
207 IsTimestampSectionValid(pts_section
));
210 if (pts_dts_flags
== 0x3) {
211 RCHECK(bit_reader
.ReadBits(40, &pts_section
));
212 RCHECK(bit_reader
.ReadBits(40, &dts_section
));
213 RCHECK((((pts_section
>> 36) & 0xf) == 0x3) &&
214 IsTimestampSectionValid(pts_section
));
215 RCHECK((((dts_section
>> 36) & 0xf) == 0x1) &&
216 IsTimestampSectionValid(dts_section
));
221 // Convert and unroll the timestamps.
222 base::TimeDelta
media_pts(kNoTimestamp());
223 DecodeTimestamp
media_dts(kNoDecodeTimestamp());
225 int64 pts
= timestamp_unroller_
->GetUnrolledTimestamp(
226 ConvertTimestampSectionToTimestamp(pts_section
));
227 media_pts
= base::TimeDelta::FromMicroseconds((1000 * pts
) / 90);
230 int64 dts
= timestamp_unroller_
->GetUnrolledTimestamp(
231 ConvertTimestampSectionToTimestamp(dts_section
));
232 media_dts
= DecodeTimestamp::FromMicroseconds((1000 * dts
) / 90);
235 // Discard the rest of the PES packet header.
236 // TODO(damienv): check if some info of the PES packet header are useful.
237 DCHECK_EQ(bit_reader
.bits_available() % 8, 0);
238 int pes_header_remaining_size
= pes_header_data_length
-
239 (pes_header_start_size
- bit_reader
.bits_available() / 8);
240 RCHECK(pes_header_remaining_size
>= 0);
242 // Read the PES packet.
244 << "Emit a reassembled PES:"
245 << " size=" << es_size
246 << " pts=" << media_pts
.InMilliseconds()
247 << " dts=" << media_dts
.InMilliseconds()
248 << " data_alignment_indicator=" << data_alignment_indicator
;
249 return es_parser_
->Parse(&raw_pes
[es_offset
], es_size
, media_pts
, media_dts
);
252 void TsSectionPes::ResetPesState() {
253 pes_byte_queue_
.Reset();
254 wait_for_pusi_
= true;