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