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