DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs
ts_section_pes.cc
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 "packager/media/formats/mp2t/ts_section_pes.h"
6 
7 #include "packager/base/logging.h"
8 #include "packager/base/strings/string_number_conversions.h"
9 #include "packager/media/base/bit_reader.h"
10 #include "packager/media/base/timestamp.h"
11 #include "packager/media/formats/mp2t/es_parser.h"
12 #include "packager/media/formats/mp2t/mp2t_common.h"
13 
14 static const int kPesStartCode = 0x000001;
15 
16 // Given that |time| is coded using 33 bits,
17 // UnrollTimestamp returns the corresponding unrolled timestamp.
18 // The unrolled timestamp is defined by:
19 // |time| + k * (2 ^ 33)
20 // where k is estimated so that the unrolled timestamp
21 // is as close as possible to |previous_unrolled_time|.
22 static int64_t UnrollTimestamp(int64_t previous_unrolled_time, int64_t time) {
23  // Mpeg2 TS timestamps have an accuracy of 33 bits.
24  const int nbits = 33;
25 
26  // |timestamp| has a precision of |nbits|
27  // so make sure the highest bits are set to 0.
28  DCHECK_EQ((time >> nbits), 0);
29 
30  // Consider 3 possibilities to estimate the missing high bits of |time|.
31  int64_t previous_unrolled_time_high = (previous_unrolled_time >> nbits);
32  int64_t time0 = ((previous_unrolled_time_high - 1) << nbits) | time;
33  int64_t time1 = ((previous_unrolled_time_high + 0) << nbits) | time;
34  int64_t time2 = ((previous_unrolled_time_high + 1) << nbits) | time;
35 
36  // Select the min absolute difference with the current time
37  // so as to ensure time continuity.
38  int64_t diff0 = time0 - previous_unrolled_time;
39  int64_t diff1 = time1 - previous_unrolled_time;
40  int64_t diff2 = time2 - previous_unrolled_time;
41  if (diff0 < 0)
42  diff0 = -diff0;
43  if (diff1 < 0)
44  diff1 = -diff1;
45  if (diff2 < 0)
46  diff2 = -diff2;
47 
48  int64_t unrolled_time;
49  int64_t min_diff;
50  if (diff1 < diff0) {
51  unrolled_time = time1;
52  min_diff = diff1;
53  } else {
54  unrolled_time = time0;
55  min_diff = diff0;
56  }
57  if (diff2 < min_diff)
58  unrolled_time = time2;
59 
60  return unrolled_time;
61 }
62 
63 static bool IsTimestampSectionValid(int64_t timestamp_section) {
64  // |pts_section| has 40 bits:
65  // - starting with either '0010' or '0011' or '0001'
66  // - and ending with a marker bit.
67  // See ITU H.222 standard - PES section.
68 
69  // Verify that all the marker bits are set to one.
70  return ((timestamp_section & 0x1) != 0) &&
71  ((timestamp_section & 0x10000) != 0) &&
72  ((timestamp_section & 0x100000000LL) != 0);
73 }
74 
75 static int64_t ConvertTimestampSectionToTimestamp(int64_t timestamp_section) {
76  return (((timestamp_section >> 33) & 0x7) << 30) |
77  (((timestamp_section >> 17) & 0x7fff) << 15) |
78  (((timestamp_section >> 1) & 0x7fff) << 0);
79 }
80 
81 namespace edash_packager {
82 namespace media {
83 namespace mp2t {
84 
85 TsSectionPes::TsSectionPes(scoped_ptr<EsParser> es_parser)
86  : es_parser_(es_parser.release()),
87  wait_for_pusi_(true),
88  previous_pts_valid_(false),
89  previous_pts_(0),
90  previous_dts_valid_(false),
91  previous_dts_(0) {
92  DCHECK(es_parser_);
93 }
94 
95 TsSectionPes::~TsSectionPes() {
96 }
97 
98 bool TsSectionPes::Parse(bool payload_unit_start_indicator,
99  const uint8_t* buf,
100  int size) {
101  // Ignore partial PES.
102  if (wait_for_pusi_ && !payload_unit_start_indicator)
103  return true;
104 
105  bool parse_result = true;
106  if (payload_unit_start_indicator) {
107  // Try emitting a packet since we might have a pending PES packet
108  // with an undefined size.
109  // In this case, a unit is emitted when the next unit is coming.
110  int raw_pes_size;
111  const uint8_t* raw_pes;
112  pes_byte_queue_.Peek(&raw_pes, &raw_pes_size);
113  if (raw_pes_size > 0)
114  parse_result = Emit(true);
115 
116  // Reset the state.
117  ResetPesState();
118 
119  // Update the state.
120  wait_for_pusi_ = false;
121  }
122 
123  // Add the data to the parser state.
124  if (size > 0)
125  pes_byte_queue_.Push(buf, size);
126 
127  // Try emitting the current PES packet.
128  return (parse_result && Emit(false));
129 }
130 
131 void TsSectionPes::Flush() {
132  // Try emitting a packet since we might have a pending PES packet
133  // with an undefined size.
134  Emit(true);
135 
136  // Flush the underlying ES parser.
137  es_parser_->Flush();
138 }
139 
140 void TsSectionPes::Reset() {
141  ResetPesState();
142 
143  previous_pts_valid_ = false;
144  previous_pts_ = 0;
145  previous_dts_valid_ = false;
146  previous_dts_ = 0;
147 
148  es_parser_->Reset();
149 }
150 
151 bool TsSectionPes::Emit(bool emit_for_unknown_size) {
152  int raw_pes_size;
153  const uint8_t* raw_pes;
154  pes_byte_queue_.Peek(&raw_pes, &raw_pes_size);
155 
156  // A PES should be at least 6 bytes.
157  // Wait for more data to come if not enough bytes.
158  if (raw_pes_size < 6)
159  return true;
160 
161  // Check whether we have enough data to start parsing.
162  int pes_packet_length =
163  (static_cast<int>(raw_pes[4]) << 8) |
164  (static_cast<int>(raw_pes[5]));
165  if ((pes_packet_length == 0 && !emit_for_unknown_size) ||
166  (pes_packet_length != 0 && raw_pes_size < pes_packet_length + 6)) {
167  // Wait for more data to come either because:
168  // - there are not enough bytes,
169  // - or the PES size is unknown and the "force emit" flag is not set.
170  // (PES size might be unknown for video PES packet).
171  return true;
172  }
173  DVLOG(LOG_LEVEL_PES) << "pes_packet_length=" << pes_packet_length;
174 
175  // Parse the packet.
176  bool parse_result = ParseInternal(raw_pes, raw_pes_size);
177 
178  // Reset the state.
179  ResetPesState();
180 
181  return parse_result;
182 }
183 
184 bool TsSectionPes::ParseInternal(const uint8_t* raw_pes, int raw_pes_size) {
185  BitReader bit_reader(raw_pes, raw_pes_size);
186 
187  // Read up to the pes_packet_length (6 bytes).
188  int packet_start_code_prefix;
189  int stream_id;
190  int pes_packet_length;
191  RCHECK(bit_reader.ReadBits(24, &packet_start_code_prefix));
192  RCHECK(bit_reader.ReadBits(8, &stream_id));
193  RCHECK(bit_reader.ReadBits(16, &pes_packet_length));
194 
195  RCHECK(packet_start_code_prefix == kPesStartCode);
196  DVLOG(LOG_LEVEL_PES) << "stream_id=" << std::hex << stream_id << std::dec;
197  if (pes_packet_length == 0)
198  pes_packet_length = bit_reader.bits_available() / 8;
199 
200  // Ignore the PES for unknown stream IDs.
201  // See ITU H.222 Table 2-22 "Stream_id assignments"
202  bool is_audio_stream_id = ((stream_id & 0xe0) == 0xc0);
203  bool is_video_stream_id = ((stream_id & 0xf0) == 0xe0);
204  if (!is_audio_stream_id && !is_video_stream_id)
205  return true;
206 
207  // Read up to "pes_header_data_length".
208  int dummy_2;
209  int PES_scrambling_control;
210  int PES_priority;
211  int data_alignment_indicator;
212  int copyright;
213  int original_or_copy;
214  int pts_dts_flags;
215  int escr_flag;
216  int es_rate_flag;
217  int dsm_trick_mode_flag;
218  int additional_copy_info_flag;
219  int pes_crc_flag;
220  int pes_extension_flag;
221  int pes_header_data_length;
222  RCHECK(bit_reader.ReadBits(2, &dummy_2));
223  RCHECK(dummy_2 == 0x2);
224  RCHECK(bit_reader.ReadBits(2, &PES_scrambling_control));
225  RCHECK(bit_reader.ReadBits(1, &PES_priority));
226  RCHECK(bit_reader.ReadBits(1, &data_alignment_indicator));
227  RCHECK(bit_reader.ReadBits(1, &copyright));
228  RCHECK(bit_reader.ReadBits(1, &original_or_copy));
229  RCHECK(bit_reader.ReadBits(2, &pts_dts_flags));
230  RCHECK(bit_reader.ReadBits(1, &escr_flag));
231  RCHECK(bit_reader.ReadBits(1, &es_rate_flag));
232  RCHECK(bit_reader.ReadBits(1, &dsm_trick_mode_flag));
233  RCHECK(bit_reader.ReadBits(1, &additional_copy_info_flag));
234  RCHECK(bit_reader.ReadBits(1, &pes_crc_flag));
235  RCHECK(bit_reader.ReadBits(1, &pes_extension_flag));
236  RCHECK(bit_reader.ReadBits(8, &pes_header_data_length));
237  int pes_header_start_size = bit_reader.bits_available() / 8;
238 
239  // Compute the size and the offset of the ES payload.
240  // "6" for the 6 bytes read before and including |pes_packet_length|.
241  // "3" for the 3 bytes read before and including |pes_header_data_length|.
242  int es_size = pes_packet_length - 3 - pes_header_data_length;
243  int es_offset = 6 + 3 + pes_header_data_length;
244  RCHECK(es_size >= 0);
245  RCHECK(es_offset + es_size <= raw_pes_size);
246 
247  // Read the timing information section.
248  bool is_pts_valid = false;
249  bool is_dts_valid = false;
250  int64_t pts_section = 0;
251  int64_t dts_section = 0;
252  if (pts_dts_flags == 0x2) {
253  RCHECK(bit_reader.ReadBits(40, &pts_section));
254  RCHECK((((pts_section >> 36) & 0xf) == 0x2) &&
255  IsTimestampSectionValid(pts_section));
256  is_pts_valid = true;
257  }
258  if (pts_dts_flags == 0x3) {
259  RCHECK(bit_reader.ReadBits(40, &pts_section));
260  RCHECK(bit_reader.ReadBits(40, &dts_section));
261  RCHECK((((pts_section >> 36) & 0xf) == 0x3) &&
262  IsTimestampSectionValid(pts_section));
263  RCHECK((((dts_section >> 36) & 0xf) == 0x1) &&
264  IsTimestampSectionValid(dts_section));
265  is_pts_valid = true;
266  is_dts_valid = true;
267  }
268 
269  // Convert and unroll the timestamps.
270  int64_t media_pts(kNoTimestamp);
271  int64_t media_dts(kNoTimestamp);
272  if (is_pts_valid) {
273  int64_t pts = ConvertTimestampSectionToTimestamp(pts_section);
274  if (previous_pts_valid_)
275  pts = UnrollTimestamp(previous_pts_, pts);
276  previous_pts_ = pts;
277  previous_pts_valid_ = true;
278  media_pts = pts;
279  }
280  if (is_dts_valid) {
281  int64_t dts = ConvertTimestampSectionToTimestamp(dts_section);
282  if (previous_dts_valid_)
283  dts = UnrollTimestamp(previous_dts_, dts);
284  previous_dts_ = dts;
285  previous_dts_valid_ = true;
286  media_dts = dts;
287  }
288 
289  // Discard the rest of the PES packet header.
290  DCHECK_EQ(bit_reader.bits_available() % 8, 0);
291  int pes_header_remaining_size = pes_header_data_length -
292  (pes_header_start_size - bit_reader.bits_available() / 8);
293  RCHECK(pes_header_remaining_size >= 0);
294 
295  // Read the PES packet.
296  DVLOG(LOG_LEVEL_PES)
297  << "Emit a reassembled PES:"
298  << " size=" << es_size
299  << " pts=" << media_pts
300  << " dts=" << media_dts
301  << " data_alignment_indicator=" << data_alignment_indicator;
302  return es_parser_->Parse(&raw_pes[es_offset], es_size, media_pts, media_dts);
303 }
304 
305 void TsSectionPes::ResetPesState() {
306  pes_byte_queue_.Reset();
307  wait_for_pusi_ = true;
308 }
309 
310 } // namespace mp2t
311 } // namespace media
312 } // namespace edash_packager
void Push(const uint8_t *data, int size)
Append new bytes to the end of the queue.
Definition: byte_queue.cc:29
void Reset()
Reset the queue to the empty state.
Definition: byte_queue.cc:24
void Peek(const uint8_t **data, int *size) const
Definition: byte_queue.cc:63