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