ATSParser.cpp revision 799c9682b3776a55d234396aee4a302437150c26
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "ATSParser"
19#include <utils/Log.h>
20
21#include "ATSParser.h"
22
23#include "AnotherPacketSource.h"
24#include "ESQueue.h"
25#include "include/avc_utils.h"
26
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 <media/stagefright/Utils.h>
36#include <media/IStreamSource.h>
37#include <utils/KeyedVector.h>
38
39#include <inttypes.h>
40
41namespace android {
42
43// I want the expression "y" evaluated even if verbose logging is off.
44#define MY_LOGV(x, y) \
45    do { unsigned tmp = y; ALOGV(x, tmp); } while (0)
46
47static const size_t kTSPacketSize = 188;
48
49struct ATSParser::Program : public RefBase {
50    Program(ATSParser *parser, unsigned programNumber, unsigned programMapPID);
51
52    bool parsePSISection(
53            unsigned pid, ABitReader *br, status_t *err);
54
55    bool parsePID(
56            unsigned pid, unsigned continuity_counter,
57            unsigned payload_unit_start_indicator,
58            ABitReader *br, status_t *err);
59
60    void signalDiscontinuity(
61            DiscontinuityType type, const sp<AMessage> &extra);
62
63    void signalEOS(status_t finalResult);
64
65    sp<MediaSource> getSource(SourceType type);
66    bool hasSource(SourceType type) const;
67
68    int64_t convertPTSToTimestamp(uint64_t PTS);
69
70    bool PTSTimeDeltaEstablished() const {
71        return mFirstPTSValid;
72    }
73
74    unsigned number() const { return mProgramNumber; }
75
76    void updateProgramMapPID(unsigned programMapPID) {
77        mProgramMapPID = programMapPID;
78    }
79
80    unsigned programMapPID() const {
81        return mProgramMapPID;
82    }
83
84    uint32_t parserFlags() const {
85        return mParser->mFlags;
86    }
87
88private:
89    ATSParser *mParser;
90    unsigned mProgramNumber;
91    unsigned mProgramMapPID;
92    KeyedVector<unsigned, sp<Stream> > mStreams;
93    bool mFirstPTSValid;
94    uint64_t mFirstPTS;
95    int64_t mLastRecoveredPTS;
96
97    status_t parseProgramMap(ABitReader *br);
98    int64_t recoverPTS(uint64_t PTS_33bit);
99
100    DISALLOW_EVIL_CONSTRUCTORS(Program);
101};
102
103struct ATSParser::Stream : public RefBase {
104    Stream(Program *program,
105           unsigned elementaryPID,
106           unsigned streamType,
107           unsigned PCR_PID);
108
109    unsigned type() const { return mStreamType; }
110    unsigned pid() const { return mElementaryPID; }
111    void setPID(unsigned pid) { mElementaryPID = pid; }
112
113    status_t parse(
114            unsigned continuity_counter,
115            unsigned payload_unit_start_indicator,
116            ABitReader *br);
117
118    void signalDiscontinuity(
119            DiscontinuityType type, const sp<AMessage> &extra);
120
121    void signalEOS(status_t finalResult);
122
123    sp<MediaSource> getSource(SourceType type);
124
125    bool isAudio() const;
126    bool isVideo() const;
127
128protected:
129    virtual ~Stream();
130
131private:
132    Program *mProgram;
133    unsigned mElementaryPID;
134    unsigned mStreamType;
135    unsigned mPCR_PID;
136    int32_t mExpectedContinuityCounter;
137
138    sp<ABuffer> mBuffer;
139    sp<AnotherPacketSource> mSource;
140    bool mPayloadStarted;
141
142    uint64_t mPrevPTS;
143
144    ElementaryStreamQueue *mQueue;
145
146    status_t flush();
147    status_t parsePES(ABitReader *br);
148
149    void onPayloadData(
150            unsigned PTS_DTS_flags, uint64_t PTS, uint64_t DTS,
151            const uint8_t *data, size_t size);
152
153    void extractAACFrames(const sp<ABuffer> &buffer);
154
155    DISALLOW_EVIL_CONSTRUCTORS(Stream);
156};
157
158struct ATSParser::PSISection : public RefBase {
159    PSISection();
160
161    status_t append(const void *data, size_t size);
162    void clear();
163
164    bool isComplete() const;
165    bool isEmpty() const;
166
167    const uint8_t *data() const;
168    size_t size() const;
169
170protected:
171    virtual ~PSISection();
172
173private:
174    sp<ABuffer> mBuffer;
175
176    DISALLOW_EVIL_CONSTRUCTORS(PSISection);
177};
178
179////////////////////////////////////////////////////////////////////////////////
180
181ATSParser::Program::Program(
182        ATSParser *parser, unsigned programNumber, unsigned programMapPID)
183    : mParser(parser),
184      mProgramNumber(programNumber),
185      mProgramMapPID(programMapPID),
186      mFirstPTSValid(false),
187      mFirstPTS(0),
188      mLastRecoveredPTS(0) {
189    ALOGV("new program number %u", programNumber);
190}
191
192bool ATSParser::Program::parsePSISection(
193        unsigned pid, ABitReader *br, status_t *err) {
194    *err = OK;
195
196    if (pid != mProgramMapPID) {
197        return false;
198    }
199
200    *err = parseProgramMap(br);
201
202    return true;
203}
204
205bool ATSParser::Program::parsePID(
206        unsigned pid, unsigned continuity_counter,
207        unsigned payload_unit_start_indicator,
208        ABitReader *br, status_t *err) {
209    *err = OK;
210
211    ssize_t index = mStreams.indexOfKey(pid);
212    if (index < 0) {
213        return false;
214    }
215
216    *err = mStreams.editValueAt(index)->parse(
217            continuity_counter, payload_unit_start_indicator, br);
218
219    return true;
220}
221
222void ATSParser::Program::signalDiscontinuity(
223        DiscontinuityType type, const sp<AMessage> &extra) {
224    int64_t mediaTimeUs;
225    if ((type & DISCONTINUITY_TIME)
226            && extra != NULL
227            && extra->findInt64(
228                IStreamListener::kKeyMediaTimeUs, &mediaTimeUs)) {
229        mFirstPTSValid = false;
230    }
231
232    for (size_t i = 0; i < mStreams.size(); ++i) {
233        mStreams.editValueAt(i)->signalDiscontinuity(type, extra);
234    }
235}
236
237void ATSParser::Program::signalEOS(status_t finalResult) {
238    for (size_t i = 0; i < mStreams.size(); ++i) {
239        mStreams.editValueAt(i)->signalEOS(finalResult);
240    }
241}
242
243struct StreamInfo {
244    unsigned mType;
245    unsigned mPID;
246};
247
248status_t ATSParser::Program::parseProgramMap(ABitReader *br) {
249    unsigned table_id = br->getBits(8);
250    ALOGV("  table_id = %u", table_id);
251    if (table_id != 0x02u) {
252        ALOGE("PMT data error!");
253        return ERROR_MALFORMED;
254    }
255    unsigned section_syntax_indicator = br->getBits(1);
256    ALOGV("  section_syntax_indicator = %u", section_syntax_indicator);
257    if (section_syntax_indicator != 1u) {
258        ALOGE("PMT data error!");
259        return ERROR_MALFORMED;
260    }
261
262    CHECK_EQ(br->getBits(1), 0u);
263    MY_LOGV("  reserved = %u", br->getBits(2));
264
265    unsigned section_length = br->getBits(12);
266    ALOGV("  section_length = %u", section_length);
267    CHECK_EQ(section_length & 0xc00, 0u);
268    CHECK_LE(section_length, 1021u);
269
270    MY_LOGV("  program_number = %u", br->getBits(16));
271    MY_LOGV("  reserved = %u", br->getBits(2));
272    MY_LOGV("  version_number = %u", br->getBits(5));
273    MY_LOGV("  current_next_indicator = %u", br->getBits(1));
274    MY_LOGV("  section_number = %u", br->getBits(8));
275    MY_LOGV("  last_section_number = %u", br->getBits(8));
276    MY_LOGV("  reserved = %u", br->getBits(3));
277
278    unsigned PCR_PID = br->getBits(13);
279    ALOGV("  PCR_PID = 0x%04x", PCR_PID);
280
281    MY_LOGV("  reserved = %u", br->getBits(4));
282
283    unsigned program_info_length = br->getBits(12);
284    ALOGV("  program_info_length = %u", program_info_length);
285    CHECK_EQ(program_info_length & 0xc00, 0u);
286
287    br->skipBits(program_info_length * 8);  // skip descriptors
288
289    Vector<StreamInfo> infos;
290
291    // infoBytesRemaining is the number of bytes that make up the
292    // variable length section of ES_infos. It does not include the
293    // final CRC.
294    size_t infoBytesRemaining = section_length - 9 - program_info_length - 4;
295
296    while (infoBytesRemaining > 0) {
297        CHECK_GE(infoBytesRemaining, 5u);
298
299        unsigned streamType = br->getBits(8);
300        ALOGV("    stream_type = 0x%02x", streamType);
301
302        MY_LOGV("    reserved = %u", br->getBits(3));
303
304        unsigned elementaryPID = br->getBits(13);
305        ALOGV("    elementary_PID = 0x%04x", elementaryPID);
306
307        MY_LOGV("    reserved = %u", br->getBits(4));
308
309        unsigned ES_info_length = br->getBits(12);
310        ALOGV("    ES_info_length = %u", ES_info_length);
311        CHECK_EQ(ES_info_length & 0xc00, 0u);
312
313        CHECK_GE(infoBytesRemaining - 5, ES_info_length);
314
315#if 0
316        br->skipBits(ES_info_length * 8);  // skip descriptors
317#else
318        unsigned info_bytes_remaining = ES_info_length;
319        while (info_bytes_remaining >= 2) {
320            MY_LOGV("      tag = 0x%02x", br->getBits(8));
321
322            unsigned descLength = br->getBits(8);
323            ALOGV("      len = %u", descLength);
324
325            CHECK_GE(info_bytes_remaining, 2 + descLength);
326
327            br->skipBits(descLength * 8);
328
329            info_bytes_remaining -= descLength + 2;
330        }
331        CHECK_EQ(info_bytes_remaining, 0u);
332#endif
333
334        StreamInfo info;
335        info.mType = streamType;
336        info.mPID = elementaryPID;
337        infos.push(info);
338
339        infoBytesRemaining -= 5 + ES_info_length;
340    }
341
342    CHECK_EQ(infoBytesRemaining, 0u);
343    MY_LOGV("  CRC = 0x%08x", br->getBits(32));
344
345    bool PIDsChanged = false;
346    for (size_t i = 0; i < infos.size(); ++i) {
347        StreamInfo &info = infos.editItemAt(i);
348
349        ssize_t index = mStreams.indexOfKey(info.mPID);
350
351        if (index >= 0 && mStreams.editValueAt(index)->type() != info.mType) {
352            ALOGI("uh oh. stream PIDs have changed.");
353            PIDsChanged = true;
354            break;
355        }
356    }
357
358    if (PIDsChanged) {
359#if 0
360        ALOGI("before:");
361        for (size_t i = 0; i < mStreams.size(); ++i) {
362            sp<Stream> stream = mStreams.editValueAt(i);
363
364            ALOGI("PID 0x%08x => type 0x%02x", stream->pid(), stream->type());
365        }
366
367        ALOGI("after:");
368        for (size_t i = 0; i < infos.size(); ++i) {
369            StreamInfo &info = infos.editItemAt(i);
370
371            ALOGI("PID 0x%08x => type 0x%02x", info.mPID, info.mType);
372        }
373#endif
374
375        // The only case we can recover from is if we have two streams
376        // and they switched PIDs.
377
378        bool success = false;
379
380        if (mStreams.size() == 2 && infos.size() == 2) {
381            const StreamInfo &info1 = infos.itemAt(0);
382            const StreamInfo &info2 = infos.itemAt(1);
383
384            sp<Stream> s1 = mStreams.editValueAt(0);
385            sp<Stream> s2 = mStreams.editValueAt(1);
386
387            bool caseA =
388                info1.mPID == s1->pid() && info1.mType == s2->type()
389                    && info2.mPID == s2->pid() && info2.mType == s1->type();
390
391            bool caseB =
392                info1.mPID == s2->pid() && info1.mType == s1->type()
393                    && info2.mPID == s1->pid() && info2.mType == s2->type();
394
395            if (caseA || caseB) {
396                unsigned pid1 = s1->pid();
397                unsigned pid2 = s2->pid();
398                s1->setPID(pid2);
399                s2->setPID(pid1);
400
401                mStreams.clear();
402                mStreams.add(s1->pid(), s1);
403                mStreams.add(s2->pid(), s2);
404
405                success = true;
406            }
407        }
408
409        if (!success) {
410            ALOGI("Stream PIDs changed and we cannot recover.");
411            return ERROR_MALFORMED;
412        }
413    }
414
415    for (size_t i = 0; i < infos.size(); ++i) {
416        StreamInfo &info = infos.editItemAt(i);
417
418        ssize_t index = mStreams.indexOfKey(info.mPID);
419
420        if (index < 0) {
421            sp<Stream> stream = new Stream(
422                    this, info.mPID, info.mType, PCR_PID);
423
424            mStreams.add(info.mPID, stream);
425        }
426    }
427
428    return OK;
429}
430
431int64_t ATSParser::Program::recoverPTS(uint64_t PTS_33bit) {
432    // We only have the lower 33-bit of the PTS. It could overflow within a
433    // reasonable amount of time. To handle the wrap-around, use fancy math
434    // to get an extended PTS that is within [-0xffffffff, 0xffffffff]
435    // of the latest recovered PTS.
436    mLastRecoveredPTS = static_cast<int64_t>(
437            ((mLastRecoveredPTS - PTS_33bit + 0x100000000ll)
438            & 0xfffffffe00000000ull) | PTS_33bit);
439
440    // We start from 0, but recovered PTS could be slightly below 0.
441    // Clamp it to 0 as rest of the pipeline doesn't take negative pts.
442    // (eg. video is read first and starts at 0, but audio starts at 0xfffffff0)
443    return mLastRecoveredPTS < 0ll ? 0ll : mLastRecoveredPTS;
444}
445
446sp<MediaSource> ATSParser::Program::getSource(SourceType type) {
447    size_t index = (type == AUDIO) ? 0 : 0;
448
449    for (size_t i = 0; i < mStreams.size(); ++i) {
450        sp<MediaSource> source = mStreams.editValueAt(i)->getSource(type);
451        if (source != NULL) {
452            if (index == 0) {
453                return source;
454            }
455            --index;
456        }
457    }
458
459    return NULL;
460}
461
462bool ATSParser::Program::hasSource(SourceType type) const {
463    for (size_t i = 0; i < mStreams.size(); ++i) {
464        const sp<Stream> &stream = mStreams.valueAt(i);
465        if (type == AUDIO && stream->isAudio()) {
466            return true;
467        } else if (type == VIDEO && stream->isVideo()) {
468            return true;
469        }
470    }
471
472    return false;
473}
474
475int64_t ATSParser::Program::convertPTSToTimestamp(uint64_t PTS) {
476    PTS = recoverPTS(PTS);
477
478    if (!(mParser->mFlags & TS_TIMESTAMPS_ARE_ABSOLUTE)) {
479        if (!mFirstPTSValid) {
480            mFirstPTSValid = true;
481            mFirstPTS = PTS;
482            PTS = 0;
483        } else if (PTS < mFirstPTS) {
484            PTS = 0;
485        } else {
486            PTS -= mFirstPTS;
487        }
488    }
489
490    int64_t timeUs = (PTS * 100) / 9;
491
492    if (mParser->mAbsoluteTimeAnchorUs >= 0ll) {
493        timeUs += mParser->mAbsoluteTimeAnchorUs;
494    }
495
496    if (mParser->mTimeOffsetValid) {
497        timeUs += mParser->mTimeOffsetUs;
498    }
499
500    return timeUs;
501}
502
503////////////////////////////////////////////////////////////////////////////////
504
505ATSParser::Stream::Stream(
506        Program *program,
507        unsigned elementaryPID,
508        unsigned streamType,
509        unsigned PCR_PID)
510    : mProgram(program),
511      mElementaryPID(elementaryPID),
512      mStreamType(streamType),
513      mPCR_PID(PCR_PID),
514      mExpectedContinuityCounter(-1),
515      mPayloadStarted(false),
516      mPrevPTS(0),
517      mQueue(NULL) {
518    switch (mStreamType) {
519        case STREAMTYPE_H264:
520            mQueue = new ElementaryStreamQueue(
521                    ElementaryStreamQueue::H264,
522                    (mProgram->parserFlags() & ALIGNED_VIDEO_DATA)
523                        ? ElementaryStreamQueue::kFlag_AlignedData : 0);
524            break;
525        case STREAMTYPE_MPEG2_AUDIO_ADTS:
526            mQueue = new ElementaryStreamQueue(ElementaryStreamQueue::AAC);
527            break;
528        case STREAMTYPE_MPEG1_AUDIO:
529        case STREAMTYPE_MPEG2_AUDIO:
530            mQueue = new ElementaryStreamQueue(
531                    ElementaryStreamQueue::MPEG_AUDIO);
532            break;
533
534        case STREAMTYPE_MPEG1_VIDEO:
535        case STREAMTYPE_MPEG2_VIDEO:
536            mQueue = new ElementaryStreamQueue(
537                    ElementaryStreamQueue::MPEG_VIDEO);
538            break;
539
540        case STREAMTYPE_MPEG4_VIDEO:
541            mQueue = new ElementaryStreamQueue(
542                    ElementaryStreamQueue::MPEG4_VIDEO);
543            break;
544
545        case STREAMTYPE_LPCM_AC3:
546        case STREAMTYPE_AC3:
547            mQueue = new ElementaryStreamQueue(
548                    ElementaryStreamQueue::AC3);
549            break;
550
551        default:
552            break;
553    }
554
555    ALOGV("new stream PID 0x%02x, type 0x%02x", elementaryPID, streamType);
556
557    if (mQueue != NULL) {
558        mBuffer = new ABuffer(192 * 1024);
559        mBuffer->setRange(0, 0);
560    }
561}
562
563ATSParser::Stream::~Stream() {
564    delete mQueue;
565    mQueue = NULL;
566}
567
568status_t ATSParser::Stream::parse(
569        unsigned continuity_counter,
570        unsigned payload_unit_start_indicator, ABitReader *br) {
571    if (mQueue == NULL) {
572        return OK;
573    }
574
575    if (mExpectedContinuityCounter >= 0
576            && (unsigned)mExpectedContinuityCounter != continuity_counter) {
577        ALOGI("discontinuity on stream pid 0x%04x", mElementaryPID);
578
579        mPayloadStarted = false;
580        mBuffer->setRange(0, 0);
581        mExpectedContinuityCounter = -1;
582
583#if 0
584        // Uncomment this if you'd rather see no corruption whatsoever on
585        // screen and suspend updates until we come across another IDR frame.
586
587        if (mStreamType == STREAMTYPE_H264) {
588            ALOGI("clearing video queue");
589            mQueue->clear(true /* clearFormat */);
590        }
591#endif
592
593        if (!payload_unit_start_indicator) {
594            return OK;
595        }
596    }
597
598    mExpectedContinuityCounter = (continuity_counter + 1) & 0x0f;
599
600    if (payload_unit_start_indicator) {
601        if (mPayloadStarted) {
602            // Otherwise we run the danger of receiving the trailing bytes
603            // of a PES packet that we never saw the start of and assuming
604            // we have a a complete PES packet.
605
606            status_t err = flush();
607
608            if (err != OK) {
609                return err;
610            }
611        }
612
613        mPayloadStarted = true;
614    }
615
616    if (!mPayloadStarted) {
617        return OK;
618    }
619
620    size_t payloadSizeBits = br->numBitsLeft();
621    CHECK_EQ(payloadSizeBits % 8, 0u);
622
623    size_t neededSize = mBuffer->size() + payloadSizeBits / 8;
624    if (mBuffer->capacity() < neededSize) {
625        // Increment in multiples of 64K.
626        neededSize = (neededSize + 65535) & ~65535;
627
628        ALOGI("resizing buffer to %zu bytes", neededSize);
629
630        sp<ABuffer> newBuffer = new ABuffer(neededSize);
631        memcpy(newBuffer->data(), mBuffer->data(), mBuffer->size());
632        newBuffer->setRange(0, mBuffer->size());
633        mBuffer = newBuffer;
634    }
635
636    memcpy(mBuffer->data() + mBuffer->size(), br->data(), payloadSizeBits / 8);
637    mBuffer->setRange(0, mBuffer->size() + payloadSizeBits / 8);
638
639    return OK;
640}
641
642bool ATSParser::Stream::isVideo() const {
643    switch (mStreamType) {
644        case STREAMTYPE_H264:
645        case STREAMTYPE_MPEG1_VIDEO:
646        case STREAMTYPE_MPEG2_VIDEO:
647        case STREAMTYPE_MPEG4_VIDEO:
648            return true;
649
650        default:
651            return false;
652    }
653}
654
655bool ATSParser::Stream::isAudio() const {
656    switch (mStreamType) {
657        case STREAMTYPE_MPEG1_AUDIO:
658        case STREAMTYPE_MPEG2_AUDIO:
659        case STREAMTYPE_MPEG2_AUDIO_ADTS:
660        case STREAMTYPE_LPCM_AC3:
661        case STREAMTYPE_AC3:
662            return true;
663
664        default:
665            return false;
666    }
667}
668
669void ATSParser::Stream::signalDiscontinuity(
670        DiscontinuityType type, const sp<AMessage> &extra) {
671    mExpectedContinuityCounter = -1;
672
673    if (mQueue == NULL) {
674        return;
675    }
676
677    mPayloadStarted = false;
678    mBuffer->setRange(0, 0);
679
680    bool clearFormat = false;
681    if (isAudio()) {
682        if (type & DISCONTINUITY_AUDIO_FORMAT) {
683            clearFormat = true;
684        }
685    } else {
686        if (type & DISCONTINUITY_VIDEO_FORMAT) {
687            clearFormat = true;
688        }
689    }
690
691    mQueue->clear(clearFormat);
692
693    if (type & DISCONTINUITY_TIME) {
694        uint64_t resumeAtPTS;
695        if (extra != NULL
696                && extra->findInt64(
697                    IStreamListener::kKeyResumeAtPTS,
698                    (int64_t *)&resumeAtPTS)) {
699            int64_t resumeAtMediaTimeUs =
700                mProgram->convertPTSToTimestamp(resumeAtPTS);
701
702            extra->setInt64("resume-at-mediaTimeUs", resumeAtMediaTimeUs);
703        }
704    }
705
706    if (mSource != NULL) {
707        mSource->queueDiscontinuity(type, extra, true);
708    }
709}
710
711void ATSParser::Stream::signalEOS(status_t finalResult) {
712    if (mSource != NULL) {
713        mSource->signalEOS(finalResult);
714    }
715}
716
717status_t ATSParser::Stream::parsePES(ABitReader *br) {
718    unsigned packet_startcode_prefix = br->getBits(24);
719
720    ALOGV("packet_startcode_prefix = 0x%08x", packet_startcode_prefix);
721
722    if (packet_startcode_prefix != 1) {
723        ALOGV("Supposedly payload_unit_start=1 unit does not start "
724             "with startcode.");
725
726        return ERROR_MALFORMED;
727    }
728
729    CHECK_EQ(packet_startcode_prefix, 0x000001u);
730
731    unsigned stream_id = br->getBits(8);
732    ALOGV("stream_id = 0x%02x", stream_id);
733
734    unsigned PES_packet_length = br->getBits(16);
735    ALOGV("PES_packet_length = %u", PES_packet_length);
736
737    if (stream_id != 0xbc  // program_stream_map
738            && stream_id != 0xbe  // padding_stream
739            && stream_id != 0xbf  // private_stream_2
740            && stream_id != 0xf0  // ECM
741            && stream_id != 0xf1  // EMM
742            && stream_id != 0xff  // program_stream_directory
743            && stream_id != 0xf2  // DSMCC
744            && stream_id != 0xf8) {  // H.222.1 type E
745        CHECK_EQ(br->getBits(2), 2u);
746
747        MY_LOGV("PES_scrambling_control = %u", br->getBits(2));
748        MY_LOGV("PES_priority = %u", br->getBits(1));
749        MY_LOGV("data_alignment_indicator = %u", br->getBits(1));
750        MY_LOGV("copyright = %u", br->getBits(1));
751        MY_LOGV("original_or_copy = %u", br->getBits(1));
752
753        unsigned PTS_DTS_flags = br->getBits(2);
754        ALOGV("PTS_DTS_flags = %u", PTS_DTS_flags);
755
756        unsigned ESCR_flag = br->getBits(1);
757        ALOGV("ESCR_flag = %u", ESCR_flag);
758
759        unsigned ES_rate_flag = br->getBits(1);
760        ALOGV("ES_rate_flag = %u", ES_rate_flag);
761
762        unsigned DSM_trick_mode_flag = br->getBits(1);
763        ALOGV("DSM_trick_mode_flag = %u", DSM_trick_mode_flag);
764
765        unsigned additional_copy_info_flag = br->getBits(1);
766        ALOGV("additional_copy_info_flag = %u", additional_copy_info_flag);
767
768        MY_LOGV("PES_CRC_flag = %u", br->getBits(1));
769        MY_LOGV("PES_extension_flag = %u", br->getBits(1));
770
771        unsigned PES_header_data_length = br->getBits(8);
772        ALOGV("PES_header_data_length = %u", PES_header_data_length);
773
774        unsigned optional_bytes_remaining = PES_header_data_length;
775
776        uint64_t PTS = 0, DTS = 0;
777
778        if (PTS_DTS_flags == 2 || PTS_DTS_flags == 3) {
779            CHECK_GE(optional_bytes_remaining, 5u);
780
781            if (br->getBits(4) != PTS_DTS_flags) {
782                ALOGE("PES data Error!");
783                return ERROR_MALFORMED;
784            }
785            PTS = ((uint64_t)br->getBits(3)) << 30;
786            CHECK_EQ(br->getBits(1), 1u);
787            PTS |= ((uint64_t)br->getBits(15)) << 15;
788            CHECK_EQ(br->getBits(1), 1u);
789            PTS |= br->getBits(15);
790            CHECK_EQ(br->getBits(1), 1u);
791
792            ALOGV("PTS = 0x%016" PRIx64 " (%.2f)", PTS, PTS / 90000.0);
793
794            optional_bytes_remaining -= 5;
795
796            if (PTS_DTS_flags == 3) {
797                CHECK_GE(optional_bytes_remaining, 5u);
798
799                CHECK_EQ(br->getBits(4), 1u);
800
801                DTS = ((uint64_t)br->getBits(3)) << 30;
802                CHECK_EQ(br->getBits(1), 1u);
803                DTS |= ((uint64_t)br->getBits(15)) << 15;
804                CHECK_EQ(br->getBits(1), 1u);
805                DTS |= br->getBits(15);
806                CHECK_EQ(br->getBits(1), 1u);
807
808                ALOGV("DTS = %" PRIu64, DTS);
809
810                optional_bytes_remaining -= 5;
811            }
812        }
813
814        if (ESCR_flag) {
815            CHECK_GE(optional_bytes_remaining, 6u);
816
817            br->getBits(2);
818
819            uint64_t ESCR = ((uint64_t)br->getBits(3)) << 30;
820            CHECK_EQ(br->getBits(1), 1u);
821            ESCR |= ((uint64_t)br->getBits(15)) << 15;
822            CHECK_EQ(br->getBits(1), 1u);
823            ESCR |= br->getBits(15);
824            CHECK_EQ(br->getBits(1), 1u);
825
826            ALOGV("ESCR = %" PRIu64, ESCR);
827            MY_LOGV("ESCR_extension = %u", br->getBits(9));
828
829            CHECK_EQ(br->getBits(1), 1u);
830
831            optional_bytes_remaining -= 6;
832        }
833
834        if (ES_rate_flag) {
835            CHECK_GE(optional_bytes_remaining, 3u);
836
837            CHECK_EQ(br->getBits(1), 1u);
838            MY_LOGV("ES_rate = %u", br->getBits(22));
839            CHECK_EQ(br->getBits(1), 1u);
840
841            optional_bytes_remaining -= 3;
842        }
843
844        br->skipBits(optional_bytes_remaining * 8);
845
846        // ES data follows.
847
848        if (PES_packet_length != 0) {
849            CHECK_GE(PES_packet_length, PES_header_data_length + 3);
850
851            unsigned dataLength =
852                PES_packet_length - 3 - PES_header_data_length;
853
854            if (br->numBitsLeft() < dataLength * 8) {
855                ALOGE("PES packet does not carry enough data to contain "
856                     "payload. (numBitsLeft = %zu, required = %u)",
857                     br->numBitsLeft(), dataLength * 8);
858
859                return ERROR_MALFORMED;
860            }
861
862            CHECK_GE(br->numBitsLeft(), dataLength * 8);
863
864            onPayloadData(
865                    PTS_DTS_flags, PTS, DTS, br->data(), dataLength);
866
867            br->skipBits(dataLength * 8);
868        } else {
869            onPayloadData(
870                    PTS_DTS_flags, PTS, DTS,
871                    br->data(), br->numBitsLeft() / 8);
872
873            size_t payloadSizeBits = br->numBitsLeft();
874            CHECK_EQ(payloadSizeBits % 8, 0u);
875
876            ALOGV("There's %zu bytes of payload.", payloadSizeBits / 8);
877        }
878    } else if (stream_id == 0xbe) {  // padding_stream
879        CHECK_NE(PES_packet_length, 0u);
880        br->skipBits(PES_packet_length * 8);
881    } else {
882        CHECK_NE(PES_packet_length, 0u);
883        br->skipBits(PES_packet_length * 8);
884    }
885
886    return OK;
887}
888
889status_t ATSParser::Stream::flush() {
890    if (mBuffer->size() == 0) {
891        return OK;
892    }
893
894    ALOGV("flushing stream 0x%04x size = %zu", mElementaryPID, mBuffer->size());
895
896    ABitReader br(mBuffer->data(), mBuffer->size());
897
898    status_t err = parsePES(&br);
899
900    mBuffer->setRange(0, 0);
901
902    return err;
903}
904
905void ATSParser::Stream::onPayloadData(
906        unsigned PTS_DTS_flags, uint64_t PTS, uint64_t /* DTS */,
907        const uint8_t *data, size_t size) {
908#if 0
909    ALOGI("payload streamType 0x%02x, PTS = 0x%016llx, dPTS = %lld",
910          mStreamType,
911          PTS,
912          (int64_t)PTS - mPrevPTS);
913    mPrevPTS = PTS;
914#endif
915
916    ALOGV("onPayloadData mStreamType=0x%02x", mStreamType);
917
918    int64_t timeUs = 0ll;  // no presentation timestamp available.
919    if (PTS_DTS_flags == 2 || PTS_DTS_flags == 3) {
920        timeUs = mProgram->convertPTSToTimestamp(PTS);
921    }
922
923    status_t err = mQueue->appendData(data, size, timeUs);
924
925    if (err != OK) {
926        return;
927    }
928
929    sp<ABuffer> accessUnit;
930    while ((accessUnit = mQueue->dequeueAccessUnit()) != NULL) {
931        if (mSource == NULL) {
932            sp<MetaData> meta = mQueue->getFormat();
933
934            if (meta != NULL) {
935                ALOGV("Stream PID 0x%08x of type 0x%02x now has data.",
936                     mElementaryPID, mStreamType);
937
938                const char *mime;
939                if (meta->findCString(kKeyMIMEType, &mime)
940                        && !strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_AVC)
941                        && !IsIDR(accessUnit)) {
942                    continue;
943                }
944                mSource = new AnotherPacketSource(meta);
945                mSource->queueAccessUnit(accessUnit);
946            }
947        } else if (mQueue->getFormat() != NULL) {
948            // After a discontinuity we invalidate the queue's format
949            // and won't enqueue any access units to the source until
950            // the queue has reestablished the new format.
951
952            if (mSource->getFormat() == NULL) {
953                mSource->setFormat(mQueue->getFormat());
954            }
955            mSource->queueAccessUnit(accessUnit);
956        }
957    }
958}
959
960sp<MediaSource> ATSParser::Stream::getSource(SourceType type) {
961    switch (type) {
962        case VIDEO:
963        {
964            if (isVideo()) {
965                return mSource;
966            }
967            break;
968        }
969
970        case AUDIO:
971        {
972            if (isAudio()) {
973                return mSource;
974            }
975            break;
976        }
977
978        default:
979            break;
980    }
981
982    return NULL;
983}
984
985////////////////////////////////////////////////////////////////////////////////
986
987ATSParser::ATSParser(uint32_t flags)
988    : mFlags(flags),
989      mAbsoluteTimeAnchorUs(-1ll),
990      mTimeOffsetValid(false),
991      mTimeOffsetUs(0ll),
992      mNumTSPacketsParsed(0),
993      mNumPCRs(0) {
994    mPSISections.add(0 /* PID */, new PSISection);
995}
996
997ATSParser::~ATSParser() {
998}
999
1000status_t ATSParser::feedTSPacket(const void *data, size_t size) {
1001    CHECK_EQ(size, kTSPacketSize);
1002
1003    ABitReader br((const uint8_t *)data, kTSPacketSize);
1004    return parseTS(&br);
1005}
1006
1007void ATSParser::signalDiscontinuity(
1008        DiscontinuityType type, const sp<AMessage> &extra) {
1009    int64_t mediaTimeUs;
1010    if ((type & DISCONTINUITY_TIME)
1011            && extra != NULL
1012            && extra->findInt64(
1013                IStreamListener::kKeyMediaTimeUs, &mediaTimeUs)) {
1014        mAbsoluteTimeAnchorUs = mediaTimeUs;
1015    } else if (type == DISCONTINUITY_ABSOLUTE_TIME) {
1016        int64_t timeUs;
1017        CHECK(extra->findInt64("timeUs", &timeUs));
1018
1019        CHECK(mPrograms.empty());
1020        mAbsoluteTimeAnchorUs = timeUs;
1021        return;
1022    } else if (type == DISCONTINUITY_TIME_OFFSET) {
1023        int64_t offset;
1024        CHECK(extra->findInt64("offset", &offset));
1025
1026        mTimeOffsetValid = true;
1027        mTimeOffsetUs = offset;
1028        return;
1029    }
1030
1031    for (size_t i = 0; i < mPrograms.size(); ++i) {
1032        mPrograms.editItemAt(i)->signalDiscontinuity(type, extra);
1033    }
1034}
1035
1036void ATSParser::signalEOS(status_t finalResult) {
1037    CHECK_NE(finalResult, (status_t)OK);
1038
1039    for (size_t i = 0; i < mPrograms.size(); ++i) {
1040        mPrograms.editItemAt(i)->signalEOS(finalResult);
1041    }
1042}
1043
1044void ATSParser::parseProgramAssociationTable(ABitReader *br) {
1045    unsigned table_id = br->getBits(8);
1046    ALOGV("  table_id = %u", table_id);
1047    if (table_id != 0x00u) {
1048        ALOGE("PAT data error!");
1049        return ;
1050    }
1051    unsigned section_syntax_indictor = br->getBits(1);
1052    ALOGV("  section_syntax_indictor = %u", section_syntax_indictor);
1053    CHECK_EQ(section_syntax_indictor, 1u);
1054
1055    CHECK_EQ(br->getBits(1), 0u);
1056    MY_LOGV("  reserved = %u", br->getBits(2));
1057
1058    unsigned section_length = br->getBits(12);
1059    ALOGV("  section_length = %u", section_length);
1060    CHECK_EQ(section_length & 0xc00, 0u);
1061
1062    MY_LOGV("  transport_stream_id = %u", br->getBits(16));
1063    MY_LOGV("  reserved = %u", br->getBits(2));
1064    MY_LOGV("  version_number = %u", br->getBits(5));
1065    MY_LOGV("  current_next_indicator = %u", br->getBits(1));
1066    MY_LOGV("  section_number = %u", br->getBits(8));
1067    MY_LOGV("  last_section_number = %u", br->getBits(8));
1068
1069    size_t numProgramBytes = (section_length - 5 /* header */ - 4 /* crc */);
1070    CHECK_EQ((numProgramBytes % 4), 0u);
1071
1072    for (size_t i = 0; i < numProgramBytes / 4; ++i) {
1073        unsigned program_number = br->getBits(16);
1074        ALOGV("    program_number = %u", program_number);
1075
1076        MY_LOGV("    reserved = %u", br->getBits(3));
1077
1078        if (program_number == 0) {
1079            MY_LOGV("    network_PID = 0x%04x", br->getBits(13));
1080        } else {
1081            unsigned programMapPID = br->getBits(13);
1082
1083            ALOGV("    program_map_PID = 0x%04x", programMapPID);
1084
1085            bool found = false;
1086            for (size_t index = 0; index < mPrograms.size(); ++index) {
1087                const sp<Program> &program = mPrograms.itemAt(index);
1088
1089                if (program->number() == program_number) {
1090                    program->updateProgramMapPID(programMapPID);
1091                    found = true;
1092                    break;
1093                }
1094            }
1095
1096            if (!found) {
1097                mPrograms.push(
1098                        new Program(this, program_number, programMapPID));
1099            }
1100
1101            if (mPSISections.indexOfKey(programMapPID) < 0) {
1102                mPSISections.add(programMapPID, new PSISection);
1103            }
1104        }
1105    }
1106
1107    MY_LOGV("  CRC = 0x%08x", br->getBits(32));
1108}
1109
1110status_t ATSParser::parsePID(
1111        ABitReader *br, unsigned PID,
1112        unsigned continuity_counter,
1113        unsigned payload_unit_start_indicator) {
1114    ssize_t sectionIndex = mPSISections.indexOfKey(PID);
1115
1116    if (sectionIndex >= 0) {
1117        sp<PSISection> section = mPSISections.valueAt(sectionIndex);
1118
1119        if (payload_unit_start_indicator) {
1120            if (!section->isEmpty()) {
1121                return ERROR_UNSUPPORTED;
1122            }
1123
1124            unsigned skip = br->getBits(8);
1125            br->skipBits(skip * 8);
1126        }
1127
1128        CHECK((br->numBitsLeft() % 8) == 0);
1129        status_t err = section->append(br->data(), br->numBitsLeft() / 8);
1130
1131        if (err != OK) {
1132            return err;
1133        }
1134
1135        if (!section->isComplete()) {
1136            return OK;
1137        }
1138
1139        ABitReader sectionBits(section->data(), section->size());
1140
1141        if (PID == 0) {
1142            parseProgramAssociationTable(&sectionBits);
1143        } else {
1144            bool handled = false;
1145            for (size_t i = 0; i < mPrograms.size(); ++i) {
1146                status_t err;
1147                if (!mPrograms.editItemAt(i)->parsePSISection(
1148                            PID, &sectionBits, &err)) {
1149                    continue;
1150                }
1151
1152                if (err != OK) {
1153                    return err;
1154                }
1155
1156                handled = true;
1157                break;
1158            }
1159
1160            if (!handled) {
1161                mPSISections.removeItem(PID);
1162                section.clear();
1163            }
1164        }
1165
1166        if (section != NULL) {
1167            section->clear();
1168        }
1169
1170        return OK;
1171    }
1172
1173    bool handled = false;
1174    for (size_t i = 0; i < mPrograms.size(); ++i) {
1175        status_t err;
1176        if (mPrograms.editItemAt(i)->parsePID(
1177                    PID, continuity_counter, payload_unit_start_indicator,
1178                    br, &err)) {
1179            if (err != OK) {
1180                return err;
1181            }
1182
1183            handled = true;
1184            break;
1185        }
1186    }
1187
1188    if (!handled) {
1189        ALOGV("PID 0x%04x not handled.", PID);
1190    }
1191
1192    return OK;
1193}
1194
1195void ATSParser::parseAdaptationField(ABitReader *br, unsigned PID) {
1196    unsigned adaptation_field_length = br->getBits(8);
1197
1198    if (adaptation_field_length > 0) {
1199        unsigned discontinuity_indicator = br->getBits(1);
1200
1201        if (discontinuity_indicator) {
1202            ALOGV("PID 0x%04x: discontinuity_indicator = 1 (!!!)", PID);
1203        }
1204
1205        br->skipBits(2);
1206        unsigned PCR_flag = br->getBits(1);
1207
1208        size_t numBitsRead = 4;
1209
1210        if (PCR_flag) {
1211            br->skipBits(4);
1212            uint64_t PCR_base = br->getBits(32);
1213            PCR_base = (PCR_base << 1) | br->getBits(1);
1214
1215            br->skipBits(6);
1216            unsigned PCR_ext = br->getBits(9);
1217
1218            // The number of bytes from the start of the current
1219            // MPEG2 transport stream packet up and including
1220            // the final byte of this PCR_ext field.
1221            size_t byteOffsetFromStartOfTSPacket =
1222                (188 - br->numBitsLeft() / 8);
1223
1224            uint64_t PCR = PCR_base * 300 + PCR_ext;
1225
1226            ALOGV("PID 0x%04x: PCR = 0x%016" PRIx64 " (%.2f)",
1227                  PID, PCR, PCR / 27E6);
1228
1229            // The number of bytes received by this parser up to and
1230            // including the final byte of this PCR_ext field.
1231            size_t byteOffsetFromStart =
1232                mNumTSPacketsParsed * 188 + byteOffsetFromStartOfTSPacket;
1233
1234            for (size_t i = 0; i < mPrograms.size(); ++i) {
1235                updatePCR(PID, PCR, byteOffsetFromStart);
1236            }
1237
1238            numBitsRead += 52;
1239        }
1240
1241        CHECK_GE(adaptation_field_length * 8, numBitsRead);
1242
1243        br->skipBits(adaptation_field_length * 8 - numBitsRead);
1244    }
1245}
1246
1247status_t ATSParser::parseTS(ABitReader *br) {
1248    ALOGV("---");
1249
1250    unsigned sync_byte = br->getBits(8);
1251    if (sync_byte != 0x47u) {
1252        ALOGE("[error] parseTS: return error as sync_byte=0x%x", sync_byte);
1253        return BAD_VALUE;
1254    }
1255
1256    if (br->getBits(1)) {  // transport_error_indicator
1257        // silently ignore.
1258        return OK;
1259    }
1260
1261    unsigned payload_unit_start_indicator = br->getBits(1);
1262    ALOGV("payload_unit_start_indicator = %u", payload_unit_start_indicator);
1263
1264    MY_LOGV("transport_priority = %u", br->getBits(1));
1265
1266    unsigned PID = br->getBits(13);
1267    ALOGV("PID = 0x%04x", PID);
1268
1269    MY_LOGV("transport_scrambling_control = %u", br->getBits(2));
1270
1271    unsigned adaptation_field_control = br->getBits(2);
1272    ALOGV("adaptation_field_control = %u", adaptation_field_control);
1273
1274    unsigned continuity_counter = br->getBits(4);
1275    ALOGV("PID = 0x%04x, continuity_counter = %u", PID, continuity_counter);
1276
1277    // ALOGI("PID = 0x%04x, continuity_counter = %u", PID, continuity_counter);
1278
1279    if (adaptation_field_control == 2 || adaptation_field_control == 3) {
1280        parseAdaptationField(br, PID);
1281    }
1282
1283    status_t err = OK;
1284
1285    if (adaptation_field_control == 1 || adaptation_field_control == 3) {
1286        err = parsePID(
1287                br, PID, continuity_counter, payload_unit_start_indicator);
1288    }
1289
1290    ++mNumTSPacketsParsed;
1291
1292    return err;
1293}
1294
1295sp<MediaSource> ATSParser::getSource(SourceType type) {
1296    int which = -1;  // any
1297
1298    for (size_t i = 0; i < mPrograms.size(); ++i) {
1299        const sp<Program> &program = mPrograms.editItemAt(i);
1300
1301        if (which >= 0 && (int)program->number() != which) {
1302            continue;
1303        }
1304
1305        sp<MediaSource> source = program->getSource(type);
1306
1307        if (source != NULL) {
1308            return source;
1309        }
1310    }
1311
1312    return NULL;
1313}
1314
1315bool ATSParser::hasSource(SourceType type) const {
1316    for (size_t i = 0; i < mPrograms.size(); ++i) {
1317        const sp<Program> &program = mPrograms.itemAt(i);
1318        if (program->hasSource(type)) {
1319            return true;
1320        }
1321    }
1322
1323    return false;
1324}
1325
1326bool ATSParser::PTSTimeDeltaEstablished() {
1327    if (mPrograms.isEmpty()) {
1328        return false;
1329    }
1330
1331    return mPrograms.editItemAt(0)->PTSTimeDeltaEstablished();
1332}
1333
1334void ATSParser::updatePCR(
1335        unsigned /* PID */, uint64_t PCR, size_t byteOffsetFromStart) {
1336    ALOGV("PCR 0x%016" PRIx64 " @ %zu", PCR, byteOffsetFromStart);
1337
1338    if (mNumPCRs == 2) {
1339        mPCR[0] = mPCR[1];
1340        mPCRBytes[0] = mPCRBytes[1];
1341        mSystemTimeUs[0] = mSystemTimeUs[1];
1342        mNumPCRs = 1;
1343    }
1344
1345    mPCR[mNumPCRs] = PCR;
1346    mPCRBytes[mNumPCRs] = byteOffsetFromStart;
1347    mSystemTimeUs[mNumPCRs] = ALooper::GetNowUs();
1348
1349    ++mNumPCRs;
1350
1351    if (mNumPCRs == 2) {
1352        double transportRate =
1353            (mPCRBytes[1] - mPCRBytes[0]) * 27E6 / (mPCR[1] - mPCR[0]);
1354
1355        ALOGV("transportRate = %.2f bytes/sec", transportRate);
1356    }
1357}
1358
1359////////////////////////////////////////////////////////////////////////////////
1360
1361ATSParser::PSISection::PSISection() {
1362}
1363
1364ATSParser::PSISection::~PSISection() {
1365}
1366
1367status_t ATSParser::PSISection::append(const void *data, size_t size) {
1368    if (mBuffer == NULL || mBuffer->size() + size > mBuffer->capacity()) {
1369        size_t newCapacity =
1370            (mBuffer == NULL) ? size : mBuffer->capacity() + size;
1371
1372        newCapacity = (newCapacity + 1023) & ~1023;
1373
1374        sp<ABuffer> newBuffer = new ABuffer(newCapacity);
1375
1376        if (mBuffer != NULL) {
1377            memcpy(newBuffer->data(), mBuffer->data(), mBuffer->size());
1378            newBuffer->setRange(0, mBuffer->size());
1379        } else {
1380            newBuffer->setRange(0, 0);
1381        }
1382
1383        mBuffer = newBuffer;
1384    }
1385
1386    memcpy(mBuffer->data() + mBuffer->size(), data, size);
1387    mBuffer->setRange(0, mBuffer->size() + size);
1388
1389    return OK;
1390}
1391
1392void ATSParser::PSISection::clear() {
1393    if (mBuffer != NULL) {
1394        mBuffer->setRange(0, 0);
1395    }
1396}
1397
1398bool ATSParser::PSISection::isComplete() const {
1399    if (mBuffer == NULL || mBuffer->size() < 3) {
1400        return false;
1401    }
1402
1403    unsigned sectionLength = U16_AT(mBuffer->data() + 1) & 0xfff;
1404    return mBuffer->size() >= sectionLength + 3;
1405}
1406
1407bool ATSParser::PSISection::isEmpty() const {
1408    return mBuffer == NULL || mBuffer->size() == 0;
1409}
1410
1411const uint8_t *ATSParser::PSISection::data() const {
1412    return mBuffer == NULL ? NULL : mBuffer->data();
1413}
1414
1415size_t ATSParser::PSISection::size() const {
1416    return mBuffer == NULL ? 0 : mBuffer->size();
1417}
1418
1419}  // namespace android
1420