2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "ATSParser"
19 #include <utils/Log.h>
21 #include "ATSParser.h"
23 #include "AnotherPacketSource.h"
25 #include "include/avc_utils.h"
27 #include <media/stagefright/foundation/ABitReader.h>
28 #include <media/stagefright/foundation/ABuffer.h>
29 #include <media/stagefright/foundation/ADebug.h>
30 #include <media/stagefright/foundation/AMessage.h>
31 #include <media/stagefright/foundation/hexdump.h>
32 #include <media/stagefright/MediaDefs.h>
33 #include <media/stagefright/MediaErrors.h>
34 #include <media/stagefright/MetaData.h>
35 #include <utils/KeyedVector.h>
39 // I want the expression "y" evaluated even if verbose logging is off.
40 #define MY_LOGV(x, y) \
41 do { unsigned tmp = y; LOGV(x, tmp); } while (0)
43 static const size_t kTSPacketSize = 188;
45 struct ATSParser::Program : public RefBase {
46 Program(unsigned programMapPID);
49 unsigned pid, unsigned payload_unit_start_indicator,
52 void signalDiscontinuity(bool isASeek);
54 sp<MediaSource> getSource(SourceType type);
56 int64_t convertPTSToTimestamp(uint64_t PTS);
59 unsigned mProgramMapPID;
60 KeyedVector<unsigned, sp<Stream> > mStreams;
64 void parseProgramMap(ABitReader *br);
66 DISALLOW_EVIL_CONSTRUCTORS(Program);
69 struct ATSParser::Stream : public RefBase {
70 Stream(Program *program, unsigned elementaryPID, unsigned streamType);
73 unsigned payload_unit_start_indicator,
76 void signalDiscontinuity(bool isASeek);
78 sp<MediaSource> getSource(SourceType type);
85 unsigned mElementaryPID;
89 sp<AnotherPacketSource> mSource;
92 ElementaryStreamQueue mQueue;
95 void parsePES(ABitReader *br);
98 unsigned PTS_DTS_flags, uint64_t PTS, uint64_t DTS,
99 const uint8_t *data, size_t size);
101 void extractAACFrames(const sp<ABuffer> &buffer);
103 DISALLOW_EVIL_CONSTRUCTORS(Stream);
106 ////////////////////////////////////////////////////////////////////////////////
108 ATSParser::Program::Program(unsigned programMapPID)
109 : mProgramMapPID(programMapPID),
110 mFirstPTSValid(false),
114 bool ATSParser::Program::parsePID(
115 unsigned pid, unsigned payload_unit_start_indicator,
117 if (pid == mProgramMapPID) {
118 if (payload_unit_start_indicator) {
119 unsigned skip = br->getBits(8);
120 br->skipBits(skip * 8);
127 ssize_t index = mStreams.indexOfKey(pid);
132 mStreams.editValueAt(index)->parse(
133 payload_unit_start_indicator, br);
138 void ATSParser::Program::signalDiscontinuity(bool isASeek) {
139 for (size_t i = 0; i < mStreams.size(); ++i) {
140 mStreams.editValueAt(i)->signalDiscontinuity(isASeek);
144 void ATSParser::Program::parseProgramMap(ABitReader *br) {
145 unsigned table_id = br->getBits(8);
146 LOGV(" table_id = %u", table_id);
147 CHECK_EQ(table_id, 0x02u);
149 unsigned section_syntax_indicator = br->getBits(1);
150 LOGV(" section_syntax_indicator = %u", section_syntax_indicator);
151 CHECK_EQ(section_syntax_indicator, 1u);
153 CHECK_EQ(br->getBits(1), 0u);
154 MY_LOGV(" reserved = %u", br->getBits(2));
156 unsigned section_length = br->getBits(12);
157 LOGV(" section_length = %u", section_length);
158 CHECK((section_length & 0xc00) == 0);
159 CHECK_LE(section_length, 1021u);
161 MY_LOGV(" program_number = %u", br->getBits(16));
162 MY_LOGV(" reserved = %u", br->getBits(2));
163 MY_LOGV(" version_number = %u", br->getBits(5));
164 MY_LOGV(" current_next_indicator = %u", br->getBits(1));
165 MY_LOGV(" section_number = %u", br->getBits(8));
166 MY_LOGV(" last_section_number = %u", br->getBits(8));
167 MY_LOGV(" reserved = %u", br->getBits(3));
168 MY_LOGV(" PCR_PID = 0x%04x", br->getBits(13));
169 MY_LOGV(" reserved = %u", br->getBits(4));
171 unsigned program_info_length = br->getBits(12);
172 LOGV(" program_info_length = %u", program_info_length);
173 CHECK((program_info_length & 0xc00) == 0);
175 br->skipBits(program_info_length * 8); // skip descriptors
177 // infoBytesRemaining is the number of bytes that make up the
178 // variable length section of ES_infos. It does not include the
180 size_t infoBytesRemaining = section_length - 9 - program_info_length - 4;
182 while (infoBytesRemaining > 0) {
183 CHECK_GE(infoBytesRemaining, 5u);
185 unsigned streamType = br->getBits(8);
186 LOGV(" stream_type = 0x%02x", streamType);
188 MY_LOGV(" reserved = %u", br->getBits(3));
190 unsigned elementaryPID = br->getBits(13);
191 LOGV(" elementary_PID = 0x%04x", elementaryPID);
193 MY_LOGV(" reserved = %u", br->getBits(4));
195 unsigned ES_info_length = br->getBits(12);
196 LOGV(" ES_info_length = %u", ES_info_length);
197 CHECK((ES_info_length & 0xc00) == 0);
199 CHECK_GE(infoBytesRemaining - 5, ES_info_length);
202 br->skipBits(ES_info_length * 8); // skip descriptors
204 unsigned info_bytes_remaining = ES_info_length;
205 while (info_bytes_remaining >= 2) {
206 MY_LOGV(" tag = 0x%02x", br->getBits(8));
208 unsigned descLength = br->getBits(8);
209 LOGV(" len = %u", descLength);
211 CHECK_GE(info_bytes_remaining, 2 + descLength);
213 br->skipBits(descLength * 8);
215 info_bytes_remaining -= descLength + 2;
217 CHECK_EQ(info_bytes_remaining, 0u);
220 ssize_t index = mStreams.indexOfKey(elementaryPID);
223 mStreams.add(elementaryPID,
224 new Stream(this, elementaryPID, streamType));
227 mStreams.add(elementaryPID,
228 new Stream(this, elementaryPID, streamType));
232 infoBytesRemaining -= 5 + ES_info_length;
235 CHECK_EQ(infoBytesRemaining, 0u);
237 MY_LOGV(" CRC = 0x%08x", br->getBits(32));
240 sp<MediaSource> ATSParser::Program::getSource(SourceType type) {
241 for (size_t i = 0; i < mStreams.size(); ++i) {
242 sp<MediaSource> source = mStreams.editValueAt(i)->getSource(type);
243 if (source != NULL) {
251 int64_t ATSParser::Program::convertPTSToTimestamp(uint64_t PTS) {
252 if (!mFirstPTSValid) {
253 mFirstPTSValid = true;
256 } else if (PTS < mFirstPTS) {
262 return (PTS * 100) / 9;
265 ////////////////////////////////////////////////////////////////////////////////
267 ATSParser::Stream::Stream(
268 Program *program, unsigned elementaryPID, unsigned streamType)
270 mElementaryPID(elementaryPID),
271 mStreamType(streamType),
272 mBuffer(new ABuffer(128 * 1024)),
273 mPayloadStarted(false),
274 mQueue(streamType == 0x1b
275 ? ElementaryStreamQueue::H264 : ElementaryStreamQueue::AAC) {
276 mBuffer->setRange(0, 0);
279 ATSParser::Stream::~Stream() {
282 void ATSParser::Stream::parse(
283 unsigned payload_unit_start_indicator, ABitReader *br) {
284 if (payload_unit_start_indicator) {
285 if (mPayloadStarted) {
286 // Otherwise we run the danger of receiving the trailing bytes
287 // of a PES packet that we never saw the start of and assuming
288 // we have a a complete PES packet.
293 mPayloadStarted = true;
296 if (!mPayloadStarted) {
300 size_t payloadSizeBits = br->numBitsLeft();
301 CHECK((payloadSizeBits % 8) == 0);
303 CHECK_LE(mBuffer->size() + payloadSizeBits / 8, mBuffer->capacity());
305 memcpy(mBuffer->data() + mBuffer->size(), br->data(), payloadSizeBits / 8);
306 mBuffer->setRange(0, mBuffer->size() + payloadSizeBits / 8);
309 void ATSParser::Stream::signalDiscontinuity(bool isASeek) {
310 LOGV("Stream discontinuity");
311 mPayloadStarted = false;
312 mBuffer->setRange(0, 0);
317 // This is only a "minor" discontinuity, we stay within the same
324 if (mStreamType == 0x1b && mSource != NULL) {
325 // Don't signal discontinuities on audio streams.
326 mSource->queueDiscontinuity();
330 void ATSParser::Stream::parsePES(ABitReader *br) {
331 unsigned packet_startcode_prefix = br->getBits(24);
333 LOGV("packet_startcode_prefix = 0x%08x", packet_startcode_prefix);
335 CHECK_EQ(packet_startcode_prefix, 0x000001u);
337 unsigned stream_id = br->getBits(8);
338 LOGV("stream_id = 0x%02x", stream_id);
340 unsigned PES_packet_length = br->getBits(16);
341 LOGV("PES_packet_length = %u", PES_packet_length);
343 if (stream_id != 0xbc // program_stream_map
344 && stream_id != 0xbe // padding_stream
345 && stream_id != 0xbf // private_stream_2
346 && stream_id != 0xf0 // ECM
347 && stream_id != 0xf1 // EMM
348 && stream_id != 0xff // program_stream_directory
349 && stream_id != 0xf2 // DSMCC
350 && stream_id != 0xf8) { // H.222.1 type E
351 CHECK_EQ(br->getBits(2), 2u);
353 MY_LOGV("PES_scrambling_control = %u", br->getBits(2));
354 MY_LOGV("PES_priority = %u", br->getBits(1));
355 MY_LOGV("data_alignment_indicator = %u", br->getBits(1));
356 MY_LOGV("copyright = %u", br->getBits(1));
357 MY_LOGV("original_or_copy = %u", br->getBits(1));
359 unsigned PTS_DTS_flags = br->getBits(2);
360 LOGV("PTS_DTS_flags = %u", PTS_DTS_flags);
362 unsigned ESCR_flag = br->getBits(1);
363 LOGV("ESCR_flag = %u", ESCR_flag);
365 unsigned ES_rate_flag = br->getBits(1);
366 LOGV("ES_rate_flag = %u", ES_rate_flag);
368 unsigned DSM_trick_mode_flag = br->getBits(1);
369 LOGV("DSM_trick_mode_flag = %u", DSM_trick_mode_flag);
371 unsigned additional_copy_info_flag = br->getBits(1);
372 LOGV("additional_copy_info_flag = %u", additional_copy_info_flag);
374 MY_LOGV("PES_CRC_flag = %u", br->getBits(1));
375 MY_LOGV("PES_extension_flag = %u", br->getBits(1));
377 unsigned PES_header_data_length = br->getBits(8);
378 LOGV("PES_header_data_length = %u", PES_header_data_length);
380 unsigned optional_bytes_remaining = PES_header_data_length;
382 uint64_t PTS = 0, DTS = 0;
384 if (PTS_DTS_flags == 2 || PTS_DTS_flags == 3) {
385 CHECK_GE(optional_bytes_remaining, 5u);
387 CHECK_EQ(br->getBits(4), PTS_DTS_flags);
389 PTS = ((uint64_t)br->getBits(3)) << 30;
390 CHECK_EQ(br->getBits(1), 1u);
391 PTS |= ((uint64_t)br->getBits(15)) << 15;
392 CHECK_EQ(br->getBits(1), 1u);
393 PTS |= br->getBits(15);
394 CHECK_EQ(br->getBits(1), 1u);
396 LOGV("PTS = %llu", PTS);
397 // LOGI("PTS = %.2f secs", PTS / 90000.0f);
399 optional_bytes_remaining -= 5;
401 if (PTS_DTS_flags == 3) {
402 CHECK_GE(optional_bytes_remaining, 5u);
404 CHECK_EQ(br->getBits(4), 1u);
406 DTS = ((uint64_t)br->getBits(3)) << 30;
407 CHECK_EQ(br->getBits(1), 1u);
408 DTS |= ((uint64_t)br->getBits(15)) << 15;
409 CHECK_EQ(br->getBits(1), 1u);
410 DTS |= br->getBits(15);
411 CHECK_EQ(br->getBits(1), 1u);
413 LOGV("DTS = %llu", DTS);
415 optional_bytes_remaining -= 5;
420 CHECK_GE(optional_bytes_remaining, 6u);
424 uint64_t ESCR = ((uint64_t)br->getBits(3)) << 30;
425 CHECK_EQ(br->getBits(1), 1u);
426 ESCR |= ((uint64_t)br->getBits(15)) << 15;
427 CHECK_EQ(br->getBits(1), 1u);
428 ESCR |= br->getBits(15);
429 CHECK_EQ(br->getBits(1), 1u);
431 LOGV("ESCR = %llu", ESCR);
432 MY_LOGV("ESCR_extension = %u", br->getBits(9));
434 CHECK_EQ(br->getBits(1), 1u);
436 optional_bytes_remaining -= 6;
440 CHECK_GE(optional_bytes_remaining, 3u);
442 CHECK_EQ(br->getBits(1), 1u);
443 MY_LOGV("ES_rate = %u", br->getBits(22));
444 CHECK_EQ(br->getBits(1), 1u);
446 optional_bytes_remaining -= 3;
449 br->skipBits(optional_bytes_remaining * 8);
453 if (PES_packet_length != 0) {
454 CHECK_GE(PES_packet_length, PES_header_data_length + 3);
456 unsigned dataLength =
457 PES_packet_length - 3 - PES_header_data_length;
459 CHECK_GE(br->numBitsLeft(), dataLength * 8);
462 PTS_DTS_flags, PTS, DTS, br->data(), dataLength);
464 br->skipBits(dataLength * 8);
467 PTS_DTS_flags, PTS, DTS,
468 br->data(), br->numBitsLeft() / 8);
470 size_t payloadSizeBits = br->numBitsLeft();
471 CHECK((payloadSizeBits % 8) == 0);
473 LOGV("There's %d bytes of payload.", payloadSizeBits / 8);
475 } else if (stream_id == 0xbe) { // padding_stream
476 CHECK_NE(PES_packet_length, 0u);
477 br->skipBits(PES_packet_length * 8);
479 CHECK_NE(PES_packet_length, 0u);
480 br->skipBits(PES_packet_length * 8);
484 void ATSParser::Stream::flush() {
485 if (mBuffer->size() == 0) {
489 LOGV("flushing stream 0x%04x size = %d", mElementaryPID, mBuffer->size());
491 ABitReader br(mBuffer->data(), mBuffer->size());
494 mBuffer->setRange(0, 0);
497 void ATSParser::Stream::onPayloadData(
498 unsigned PTS_DTS_flags, uint64_t PTS, uint64_t DTS,
499 const uint8_t *data, size_t size) {
500 LOGV("onPayloadData mStreamType=0x%02x", mStreamType);
502 CHECK(PTS_DTS_flags == 2 || PTS_DTS_flags == 3);
503 int64_t timeUs = mProgram->convertPTSToTimestamp(PTS);
505 status_t err = mQueue.appendData(data, size, timeUs);
506 CHECK_EQ(err, (status_t)OK);
508 sp<ABuffer> accessUnit;
509 while ((accessUnit = mQueue.dequeueAccessUnit()) != NULL) {
510 if (mSource == NULL) {
511 sp<MetaData> meta = mQueue.getFormat();
514 LOGV("created source!");
515 mSource = new AnotherPacketSource(meta);
516 mSource->queueAccessUnit(accessUnit);
518 } else if (mQueue.getFormat() != NULL) {
519 // After a discontinuity we invalidate the queue's format
520 // and won't enqueue any access units to the source until
521 // the queue has reestablished the new format.
522 mSource->queueAccessUnit(accessUnit);
527 sp<MediaSource> ATSParser::Stream::getSource(SourceType type) {
528 if ((type == AVC_VIDEO && mStreamType == 0x1b)
529 || (type == MPEG2ADTS_AUDIO && mStreamType == 0x0f)) {
536 ////////////////////////////////////////////////////////////////////////////////
538 ATSParser::ATSParser() {
541 ATSParser::~ATSParser() {
544 void ATSParser::feedTSPacket(const void *data, size_t size) {
545 CHECK_EQ(size, kTSPacketSize);
547 ABitReader br((const uint8_t *)data, kTSPacketSize);
551 void ATSParser::signalDiscontinuity(bool isASeek) {
552 for (size_t i = 0; i < mPrograms.size(); ++i) {
553 mPrograms.editItemAt(i)->signalDiscontinuity(isASeek);
557 void ATSParser::parseProgramAssociationTable(ABitReader *br) {
558 unsigned table_id = br->getBits(8);
559 LOGV(" table_id = %u", table_id);
560 CHECK_EQ(table_id, 0x00u);
562 unsigned section_syntax_indictor = br->getBits(1);
563 LOGV(" section_syntax_indictor = %u", section_syntax_indictor);
564 CHECK_EQ(section_syntax_indictor, 1u);
566 CHECK_EQ(br->getBits(1), 0u);
567 MY_LOGV(" reserved = %u", br->getBits(2));
569 unsigned section_length = br->getBits(12);
570 LOGV(" section_length = %u", section_length);
571 CHECK((section_length & 0xc00) == 0);
573 MY_LOGV(" transport_stream_id = %u", br->getBits(16));
574 MY_LOGV(" reserved = %u", br->getBits(2));
575 MY_LOGV(" version_number = %u", br->getBits(5));
576 MY_LOGV(" current_next_indicator = %u", br->getBits(1));
577 MY_LOGV(" section_number = %u", br->getBits(8));
578 MY_LOGV(" last_section_number = %u", br->getBits(8));
580 size_t numProgramBytes = (section_length - 5 /* header */ - 4 /* crc */);
581 CHECK_EQ((numProgramBytes % 4), 0u);
583 for (size_t i = 0; i < numProgramBytes / 4; ++i) {
584 unsigned program_number = br->getBits(16);
585 LOGV(" program_number = %u", program_number);
587 MY_LOGV(" reserved = %u", br->getBits(3));
589 if (program_number == 0) {
590 MY_LOGV(" network_PID = 0x%04x", br->getBits(13));
592 unsigned programMapPID = br->getBits(13);
594 LOGV(" program_map_PID = 0x%04x", programMapPID);
596 mPrograms.push(new Program(programMapPID));
600 MY_LOGV(" CRC = 0x%08x", br->getBits(32));
603 void ATSParser::parsePID(
604 ABitReader *br, unsigned PID,
605 unsigned payload_unit_start_indicator) {
607 if (payload_unit_start_indicator) {
608 unsigned skip = br->getBits(8);
609 br->skipBits(skip * 8);
611 parseProgramAssociationTable(br);
615 bool handled = false;
616 for (size_t i = 0; i < mPrograms.size(); ++i) {
617 if (mPrograms.editItemAt(i)->parsePID(
618 PID, payload_unit_start_indicator, br)) {
625 LOGV("PID 0x%04x not handled.", PID);
629 void ATSParser::parseAdaptationField(ABitReader *br) {
630 unsigned adaptation_field_length = br->getBits(8);
631 if (adaptation_field_length > 0) {
632 br->skipBits(adaptation_field_length * 8); // XXX
636 void ATSParser::parseTS(ABitReader *br) {
639 unsigned sync_byte = br->getBits(8);
640 CHECK_EQ(sync_byte, 0x47u);
642 MY_LOGV("transport_error_indicator = %u", br->getBits(1));
644 unsigned payload_unit_start_indicator = br->getBits(1);
645 LOGV("payload_unit_start_indicator = %u", payload_unit_start_indicator);
647 MY_LOGV("transport_priority = %u", br->getBits(1));
649 unsigned PID = br->getBits(13);
650 LOGV("PID = 0x%04x", PID);
652 MY_LOGV("transport_scrambling_control = %u", br->getBits(2));
654 unsigned adaptation_field_control = br->getBits(2);
655 LOGV("adaptation_field_control = %u", adaptation_field_control);
657 unsigned continuity_counter = br->getBits(4);
658 LOGV("continuity_counter = %u", continuity_counter);
660 // LOGI("PID = 0x%04x, continuity_counter = %u", PID, continuity_counter);
662 if (adaptation_field_control == 2 || adaptation_field_control == 3) {
663 parseAdaptationField(br);
666 if (adaptation_field_control == 1 || adaptation_field_control == 3) {
667 parsePID(br, PID, payload_unit_start_indicator);
671 sp<MediaSource> ATSParser::getSource(SourceType type) {
672 for (size_t i = 0; i < mPrograms.size(); ++i) {
673 sp<MediaSource> source = mPrograms.editItemAt(i)->getSource(type);
675 if (source != NULL) {
683 } // namespace android