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