1cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber/*
2cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber * Copyright (C) 2010 The Android Open Source Project
3cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber *
4cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
5cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber * you may not use this file except in compliance with the License.
6cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber * You may obtain a copy of the License at
7cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber *
8cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
9cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber *
10cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber * Unless required by applicable law or agreed to in writing, software
11cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
12cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber * See the License for the specific language governing permissions and
14cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber * limitations under the License.
15cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber */
16cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
176e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber//#define LOG_NDEBUG 0
186e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber#define LOG_TAG "ATSParser"
196e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber#include <utils/Log.h>
206e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber
21cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include "ATSParser.h"
22cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
23cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include "AnotherPacketSource.h"
246a63a939601645404fd98f58c19cc38ca818d99eAndreas Huber#include "ESQueue.h"
2585f12e9b9062402d6110df3f7099707912040edbAndreas Huber#include "include/avc_utils.h"
26cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
2785f12e9b9062402d6110df3f7099707912040edbAndreas Huber#include <media/stagefright/foundation/ABitReader.h>
28cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include <media/stagefright/foundation/ABuffer.h>
29cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include <media/stagefright/foundation/ADebug.h>
30cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include <media/stagefright/foundation/AMessage.h>
31cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include <media/stagefright/foundation/hexdump.h>
32cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include <media/stagefright/MediaDefs.h>
33cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include <media/stagefright/MediaErrors.h>
34cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include <media/stagefright/MetaData.h>
358dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber#include <media/stagefright/Utils.h>
3632f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber#include <media/IStreamSource.h>
37cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include <utils/KeyedVector.h>
38cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
39cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Hubernamespace android {
40cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
416e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber// I want the expression "y" evaluated even if verbose logging is off.
426e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber#define MY_LOGV(x, y) \
433856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    do { unsigned tmp = y; ALOGV(x, tmp); } while (0)
446e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber
45cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huberstatic const size_t kTSPacketSize = 188;
46cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
47cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huberstruct ATSParser::Program : public RefBase {
48386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    Program(ATSParser *parser, unsigned programNumber, unsigned programMapPID);
49cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
508dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    bool parsePSISection(
518dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber            unsigned pid, ABitReader *br, status_t *err);
528dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
53cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    bool parsePID(
5487f2a558dd12043631e12c361abef301bf603140Andreas Huber            unsigned pid, unsigned continuity_counter,
5587f2a558dd12043631e12c361abef301bf603140Andreas Huber            unsigned payload_unit_start_indicator,
5606528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            ABitReader *br, status_t *err);
57cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
5832f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber    void signalDiscontinuity(
5932f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber            DiscontinuityType type, const sp<AMessage> &extra);
6032f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber
61f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    void signalEOS(status_t finalResult);
622a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber
63cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    sp<MediaSource> getSource(SourceType type);
64cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
65bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber    int64_t convertPTSToTimestamp(uint64_t PTS);
66bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber
6743c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber    bool PTSTimeDeltaEstablished() const {
6843c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber        return mFirstPTSValid;
6943c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber    }
7043c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber
71386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    unsigned number() const { return mProgramNumber; }
72386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
73386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    void updateProgramMapPID(unsigned programMapPID) {
74386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        mProgramMapPID = programMapPID;
75386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    }
76386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
778dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    unsigned programMapPID() const {
788dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        return mProgramMapPID;
798dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    }
808dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
8187f2a558dd12043631e12c361abef301bf603140Andreas Huber    uint32_t parserFlags() const {
8287f2a558dd12043631e12c361abef301bf603140Andreas Huber        return mParser->mFlags;
8387f2a558dd12043631e12c361abef301bf603140Andreas Huber    }
8487f2a558dd12043631e12c361abef301bf603140Andreas Huber
85cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huberprivate:
86f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    ATSParser *mParser;
87386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    unsigned mProgramNumber;
88cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned mProgramMapPID;
89cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    KeyedVector<unsigned, sp<Stream> > mStreams;
90bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber    bool mFirstPTSValid;
91bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber    uint64_t mFirstPTS;
92cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
9306528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber    status_t parseProgramMap(ABitReader *br);
94cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
95cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(Program);
96cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber};
97cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
98cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huberstruct ATSParser::Stream : public RefBase {
9987f2a558dd12043631e12c361abef301bf603140Andreas Huber    Stream(Program *program,
10087f2a558dd12043631e12c361abef301bf603140Andreas Huber           unsigned elementaryPID,
10187f2a558dd12043631e12c361abef301bf603140Andreas Huber           unsigned streamType,
10287f2a558dd12043631e12c361abef301bf603140Andreas Huber           unsigned PCR_PID);
103cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
104bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    unsigned type() const { return mStreamType; }
105bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    unsigned pid() const { return mElementaryPID; }
106bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    void setPID(unsigned pid) { mElementaryPID = pid; }
107bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber
1085403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber    status_t parse(
10987f2a558dd12043631e12c361abef301bf603140Andreas Huber            unsigned continuity_counter,
110cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            unsigned payload_unit_start_indicator,
111cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            ABitReader *br);
112cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
11332f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber    void signalDiscontinuity(
11432f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber            DiscontinuityType type, const sp<AMessage> &extra);
11532f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber
116f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    void signalEOS(status_t finalResult);
1172a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber
118cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    sp<MediaSource> getSource(SourceType type);
119cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
120cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huberprotected:
121cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    virtual ~Stream();
122cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
123cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huberprivate:
124bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber    Program *mProgram;
125cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned mElementaryPID;
126cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned mStreamType;
12787f2a558dd12043631e12c361abef301bf603140Andreas Huber    unsigned mPCR_PID;
12887f2a558dd12043631e12c361abef301bf603140Andreas Huber    int32_t mExpectedContinuityCounter;
129cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
130cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    sp<ABuffer> mBuffer;
131cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    sp<AnotherPacketSource> mSource;
132cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    bool mPayloadStarted;
133cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
13490a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    uint64_t mPrevPTS;
13590a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
136386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    ElementaryStreamQueue *mQueue;
1376a63a939601645404fd98f58c19cc38ca818d99eAndreas Huber
1385403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber    status_t flush();
1395403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber    status_t parsePES(ABitReader *br);
140cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
141cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    void onPayloadData(
142cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            unsigned PTS_DTS_flags, uint64_t PTS, uint64_t DTS,
143cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            const uint8_t *data, size_t size);
144cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
14582f7321b03eec1e40af9d681370f754ee0279582Andreas Huber    void extractAACFrames(const sp<ABuffer> &buffer);
14682f7321b03eec1e40af9d681370f754ee0279582Andreas Huber
1476e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    bool isAudio() const;
1486e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    bool isVideo() const;
1496e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber
150cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(Stream);
151cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber};
152cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1538dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huberstruct ATSParser::PSISection : public RefBase {
1548dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    PSISection();
1558dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
1568dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    status_t append(const void *data, size_t size);
1578dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    void clear();
1588dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
1598dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    bool isComplete() const;
1608dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    bool isEmpty() const;
1618dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
1628dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    const uint8_t *data() const;
1638dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    size_t size() const;
1648dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
1658dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huberprotected:
1668dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    virtual ~PSISection();
1678dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
1688dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huberprivate:
1698dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    sp<ABuffer> mBuffer;
1708dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
1718dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    DISALLOW_EVIL_CONSTRUCTORS(PSISection);
1728dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber};
1738dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
174cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber////////////////////////////////////////////////////////////////////////////////
175cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
176386d609dc513e838c7e7c4c46c604493ccd560beAndreas HuberATSParser::Program::Program(
177386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        ATSParser *parser, unsigned programNumber, unsigned programMapPID)
178f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    : mParser(parser),
179386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber      mProgramNumber(programNumber),
180f933441648ef6a71dee783d733aac17b9508b452Andreas Huber      mProgramMapPID(programMapPID),
181bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber      mFirstPTSValid(false),
182bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber      mFirstPTS(0) {
1833856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("new program number %u", programNumber);
184cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
185cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1868dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huberbool ATSParser::Program::parsePSISection(
1878dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        unsigned pid, ABitReader *br, status_t *err) {
18806528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber    *err = OK;
18906528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
1908dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    if (pid != mProgramMapPID) {
1918dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        return false;
1928dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    }
193cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
1948dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    *err = parseProgramMap(br);
19506528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
1968dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    return true;
1978dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber}
1988dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber
1998dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huberbool ATSParser::Program::parsePID(
20087f2a558dd12043631e12c361abef301bf603140Andreas Huber        unsigned pid, unsigned continuity_counter,
20187f2a558dd12043631e12c361abef301bf603140Andreas Huber        unsigned payload_unit_start_indicator,
2028dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber        ABitReader *br, status_t *err) {
2038dfa228201131da0bf3ba1d74c819c27c0500f6bAndreas Huber    *err = OK;
204cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
205cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    ssize_t index = mStreams.indexOfKey(pid);
206cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    if (index < 0) {
207cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        return false;
208cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
209cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
2105403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber    *err = mStreams.editValueAt(index)->parse(
21187f2a558dd12043631e12c361abef301bf603140Andreas Huber            continuity_counter, payload_unit_start_indicator, br);
212cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
213cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    return true;
214cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
215cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
21632f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Hubervoid ATSParser::Program::signalDiscontinuity(
21732f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber        DiscontinuityType type, const sp<AMessage> &extra) {
2182a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber    for (size_t i = 0; i < mStreams.size(); ++i) {
21932f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber        mStreams.editValueAt(i)->signalDiscontinuity(type, extra);
220f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
221f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
222f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
223f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ATSParser::Program::signalEOS(status_t finalResult) {
224f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    for (size_t i = 0; i < mStreams.size(); ++i) {
225f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        mStreams.editValueAt(i)->signalEOS(finalResult);
2262a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber    }
2272a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber}
2282a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber
229bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huberstruct StreamInfo {
230bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    unsigned mType;
231bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    unsigned mPID;
232bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber};
233bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber
23406528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huberstatus_t ATSParser::Program::parseProgramMap(ABitReader *br) {
235cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned table_id = br->getBits(8);
2363856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("  table_id = %u", table_id);
237cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    CHECK_EQ(table_id, 0x02u);
238cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
2396e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    unsigned section_syntax_indicator = br->getBits(1);
2403856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("  section_syntax_indicator = %u", section_syntax_indicator);
2416e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    CHECK_EQ(section_syntax_indicator, 1u);
242cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
243cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    CHECK_EQ(br->getBits(1), 0u);
2446e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  reserved = %u", br->getBits(2));
245cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
246cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned section_length = br->getBits(12);
2473856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("  section_length = %u", section_length);
248f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ(section_length & 0xc00, 0u);
249cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    CHECK_LE(section_length, 1021u);
250cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
2516e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  program_number = %u", br->getBits(16));
2526e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  reserved = %u", br->getBits(2));
2536e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  version_number = %u", br->getBits(5));
2546e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  current_next_indicator = %u", br->getBits(1));
2556e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  section_number = %u", br->getBits(8));
2566e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  last_section_number = %u", br->getBits(8));
2576e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  reserved = %u", br->getBits(3));
25887f2a558dd12043631e12c361abef301bf603140Andreas Huber
25987f2a558dd12043631e12c361abef301bf603140Andreas Huber    unsigned PCR_PID = br->getBits(13);
26087f2a558dd12043631e12c361abef301bf603140Andreas Huber    ALOGV("  PCR_PID = 0x%04x", PCR_PID);
26187f2a558dd12043631e12c361abef301bf603140Andreas Huber
2626e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  reserved = %u", br->getBits(4));
263cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
264cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned program_info_length = br->getBits(12);
2653856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("  program_info_length = %u", program_info_length);
266f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ(program_info_length & 0xc00, 0u);
267cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
268cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    br->skipBits(program_info_length * 8);  // skip descriptors
269cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
270bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    Vector<StreamInfo> infos;
271bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber
272cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    // infoBytesRemaining is the number of bytes that make up the
273cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    // variable length section of ES_infos. It does not include the
274cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    // final CRC.
275cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    size_t infoBytesRemaining = section_length - 9 - program_info_length - 4;
276cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
277cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    while (infoBytesRemaining > 0) {
278cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        CHECK_GE(infoBytesRemaining, 5u);
279cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
280cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned streamType = br->getBits(8);
2813856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("    stream_type = 0x%02x", streamType);
282cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
2836e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber        MY_LOGV("    reserved = %u", br->getBits(3));
284cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
285cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned elementaryPID = br->getBits(13);
2863856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("    elementary_PID = 0x%04x", elementaryPID);
287cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
2886e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber        MY_LOGV("    reserved = %u", br->getBits(4));
289cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
290cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned ES_info_length = br->getBits(12);
2913856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("    ES_info_length = %u", ES_info_length);
292f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        CHECK_EQ(ES_info_length & 0xc00, 0u);
293cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
294cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        CHECK_GE(infoBytesRemaining - 5, ES_info_length);
295cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
296cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#if 0
297cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        br->skipBits(ES_info_length * 8);  // skip descriptors
298cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#else
299cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned info_bytes_remaining = ES_info_length;
300cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        while (info_bytes_remaining >= 2) {
3016e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber            MY_LOGV("      tag = 0x%02x", br->getBits(8));
302cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
303cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            unsigned descLength = br->getBits(8);
3043856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("      len = %u", descLength);
305cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
306cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_GE(info_bytes_remaining, 2 + descLength);
307cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
308cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            br->skipBits(descLength * 8);
309cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
310cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            info_bytes_remaining -= descLength + 2;
311cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        }
312cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        CHECK_EQ(info_bytes_remaining, 0u);
313cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#endif
314cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
315bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        StreamInfo info;
316bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        info.mType = streamType;
317bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        info.mPID = elementaryPID;
318bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        infos.push(info);
319cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
320cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        infoBytesRemaining -= 5 + ES_info_length;
321cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
322cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
323cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    CHECK_EQ(infoBytesRemaining, 0u);
3246e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    MY_LOGV("  CRC = 0x%08x", br->getBits(32));
325bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber
326bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    bool PIDsChanged = false;
327bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    for (size_t i = 0; i < infos.size(); ++i) {
328bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        StreamInfo &info = infos.editItemAt(i);
329bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber
330bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        ssize_t index = mStreams.indexOfKey(info.mPID);
331bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber
332bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        if (index >= 0 && mStreams.editValueAt(index)->type() != info.mType) {
333df64d15042bbd5e0e4933ac49bf3c177dd94752cSteve Block            ALOGI("uh oh. stream PIDs have changed.");
334bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber            PIDsChanged = true;
335bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber            break;
336bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        }
337bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    }
338bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber
339bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    if (PIDsChanged) {
34006528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber#if 0
341df64d15042bbd5e0e4933ac49bf3c177dd94752cSteve Block        ALOGI("before:");
34206528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        for (size_t i = 0; i < mStreams.size(); ++i) {
34306528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            sp<Stream> stream = mStreams.editValueAt(i);
34406528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
345df64d15042bbd5e0e4933ac49bf3c177dd94752cSteve Block            ALOGI("PID 0x%08x => type 0x%02x", stream->pid(), stream->type());
34606528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        }
34706528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
348df64d15042bbd5e0e4933ac49bf3c177dd94752cSteve Block        ALOGI("after:");
34906528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        for (size_t i = 0; i < infos.size(); ++i) {
35006528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            StreamInfo &info = infos.editItemAt(i);
35106528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
352df64d15042bbd5e0e4933ac49bf3c177dd94752cSteve Block            ALOGI("PID 0x%08x => type 0x%02x", info.mPID, info.mType);
35306528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        }
35406528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber#endif
35506528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
35606528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        // The only case we can recover from is if we have two streams
35706528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        // and they switched PIDs.
35806528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
35906528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        bool success = false;
36006528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
36106528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        if (mStreams.size() == 2 && infos.size() == 2) {
36206528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            const StreamInfo &info1 = infos.itemAt(0);
36306528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            const StreamInfo &info2 = infos.itemAt(1);
36406528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
36506528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            sp<Stream> s1 = mStreams.editValueAt(0);
36606528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            sp<Stream> s2 = mStreams.editValueAt(1);
36706528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
36806528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            bool caseA =
36906528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                info1.mPID == s1->pid() && info1.mType == s2->type()
37006528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                    && info2.mPID == s2->pid() && info2.mType == s1->type();
37106528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
37206528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            bool caseB =
37306528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                info1.mPID == s2->pid() && info1.mType == s1->type()
37406528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                    && info2.mPID == s1->pid() && info2.mType == s2->type();
37506528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
37606528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            if (caseA || caseB) {
37706528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                unsigned pid1 = s1->pid();
37806528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                unsigned pid2 = s2->pid();
37906528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                s1->setPID(pid2);
38006528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                s2->setPID(pid1);
38106528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
38206528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                mStreams.clear();
38306528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                mStreams.add(s1->pid(), s1);
38406528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                mStreams.add(s2->pid(), s2);
38506528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
38606528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                success = true;
38706528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            }
38806528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        }
38906528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
39006528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        if (!success) {
391df64d15042bbd5e0e4933ac49bf3c177dd94752cSteve Block            ALOGI("Stream PIDs changed and we cannot recover.");
39206528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber            return ERROR_MALFORMED;
39306528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber        }
394bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    }
395bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber
396bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    for (size_t i = 0; i < infos.size(); ++i) {
397bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        StreamInfo &info = infos.editItemAt(i);
398bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber
399bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        ssize_t index = mStreams.indexOfKey(info.mPID);
400bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber
401bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        if (index < 0) {
40287f2a558dd12043631e12c361abef301bf603140Andreas Huber            sp<Stream> stream = new Stream(
40387f2a558dd12043631e12c361abef301bf603140Andreas Huber                    this, info.mPID, info.mType, PCR_PID);
40487f2a558dd12043631e12c361abef301bf603140Andreas Huber
405bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber            mStreams.add(info.mPID, stream);
406bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        }
407bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber    }
40806528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
40906528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber    return OK;
410cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
411cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
412cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Hubersp<MediaSource> ATSParser::Program::getSource(SourceType type) {
413386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    size_t index = (type == AUDIO) ? 0 : 0;
414decd96988e495133e4a1728f612d4c9fdb4d218eAndreas Huber
415cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    for (size_t i = 0; i < mStreams.size(); ++i) {
416cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        sp<MediaSource> source = mStreams.editValueAt(i)->getSource(type);
417cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        if (source != NULL) {
418decd96988e495133e4a1728f612d4c9fdb4d218eAndreas Huber            if (index == 0) {
419decd96988e495133e4a1728f612d4c9fdb4d218eAndreas Huber                return source;
420decd96988e495133e4a1728f612d4c9fdb4d218eAndreas Huber            }
421decd96988e495133e4a1728f612d4c9fdb4d218eAndreas Huber            --index;
422cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        }
423cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
424cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
425cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    return NULL;
426cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
427cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
428bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huberint64_t ATSParser::Program::convertPTSToTimestamp(uint64_t PTS) {
429c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas Huber    if (!(mParser->mFlags & TS_TIMESTAMPS_ARE_ABSOLUTE)) {
430c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas Huber        if (!mFirstPTSValid) {
431c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas Huber            mFirstPTSValid = true;
432c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas Huber            mFirstPTS = PTS;
433c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas Huber            PTS = 0;
434c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas Huber        } else if (PTS < mFirstPTS) {
435c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas Huber            PTS = 0;
436c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas Huber        } else {
437c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas Huber            PTS -= mFirstPTS;
438c4c17d47b674b425fb6c399822c0ab3258543c0aAndreas Huber        }
439bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber    }
440bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber
44187f2a558dd12043631e12c361abef301bf603140Andreas Huber    int64_t timeUs = (PTS * 100) / 9;
44287f2a558dd12043631e12c361abef301bf603140Andreas Huber
44387f2a558dd12043631e12c361abef301bf603140Andreas Huber    if (mParser->mAbsoluteTimeAnchorUs >= 0ll) {
44487f2a558dd12043631e12c361abef301bf603140Andreas Huber        timeUs += mParser->mAbsoluteTimeAnchorUs;
44587f2a558dd12043631e12c361abef301bf603140Andreas Huber    }
44687f2a558dd12043631e12c361abef301bf603140Andreas Huber
44787f2a558dd12043631e12c361abef301bf603140Andreas Huber    return timeUs;
448bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber}
449bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber
450cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber////////////////////////////////////////////////////////////////////////////////
451cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
452bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas HuberATSParser::Stream::Stream(
45387f2a558dd12043631e12c361abef301bf603140Andreas Huber        Program *program,
45487f2a558dd12043631e12c361abef301bf603140Andreas Huber        unsigned elementaryPID,
45587f2a558dd12043631e12c361abef301bf603140Andreas Huber        unsigned streamType,
45687f2a558dd12043631e12c361abef301bf603140Andreas Huber        unsigned PCR_PID)
457bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber    : mProgram(program),
458bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber      mElementaryPID(elementaryPID),
459cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber      mStreamType(streamType),
46087f2a558dd12043631e12c361abef301bf603140Andreas Huber      mPCR_PID(PCR_PID),
46187f2a558dd12043631e12c361abef301bf603140Andreas Huber      mExpectedContinuityCounter(-1),
4626a63a939601645404fd98f58c19cc38ca818d99eAndreas Huber      mPayloadStarted(false),
46390a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mPrevPTS(0),
464386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber      mQueue(NULL) {
465386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    switch (mStreamType) {
466386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        case STREAMTYPE_H264:
46787f2a558dd12043631e12c361abef301bf603140Andreas Huber            mQueue = new ElementaryStreamQueue(
46887f2a558dd12043631e12c361abef301bf603140Andreas Huber                    ElementaryStreamQueue::H264,
46987f2a558dd12043631e12c361abef301bf603140Andreas Huber                    (mProgram->parserFlags() & ALIGNED_VIDEO_DATA)
47087f2a558dd12043631e12c361abef301bf603140Andreas Huber                        ? ElementaryStreamQueue::kFlag_AlignedData : 0);
471386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            break;
4726e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        case STREAMTYPE_MPEG2_AUDIO_ADTS:
473386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            mQueue = new ElementaryStreamQueue(ElementaryStreamQueue::AAC);
474386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            break;
475386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        case STREAMTYPE_MPEG1_AUDIO:
476386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        case STREAMTYPE_MPEG2_AUDIO:
477386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            mQueue = new ElementaryStreamQueue(
478386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber                    ElementaryStreamQueue::MPEG_AUDIO);
479386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            break;
480386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
481386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        case STREAMTYPE_MPEG1_VIDEO:
482386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        case STREAMTYPE_MPEG2_VIDEO:
483386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            mQueue = new ElementaryStreamQueue(
484386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber                    ElementaryStreamQueue::MPEG_VIDEO);
485386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            break;
486386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
487386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        case STREAMTYPE_MPEG4_VIDEO:
488386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            mQueue = new ElementaryStreamQueue(
489386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber                    ElementaryStreamQueue::MPEG4_VIDEO);
490386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            break;
491386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
49290a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber        case STREAMTYPE_PCM_AUDIO:
49390a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber            mQueue = new ElementaryStreamQueue(
49490a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber                    ElementaryStreamQueue::PCM_AUDIO);
49590a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber            break;
49690a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
497386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        default:
498386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber            break;
499386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    }
500386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
5013856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("new stream PID 0x%02x, type 0x%02x", elementaryPID, streamType);
50218ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber
50318ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber    if (mQueue != NULL) {
50418ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber        mBuffer = new ABuffer(192 * 1024);
50518ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber        mBuffer->setRange(0, 0);
50618ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber    }
507cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
508cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
509cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas HuberATSParser::Stream::~Stream() {
510386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    delete mQueue;
511386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    mQueue = NULL;
512cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
513cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
5145403129e2a2f44620f2ac8109889e5a61be08732Andreas Huberstatus_t ATSParser::Stream::parse(
51587f2a558dd12043631e12c361abef301bf603140Andreas Huber        unsigned continuity_counter,
516cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned payload_unit_start_indicator, ABitReader *br) {
51718ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber    if (mQueue == NULL) {
5185403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber        return OK;
51918ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber    }
52018ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber
52187f2a558dd12043631e12c361abef301bf603140Andreas Huber    if (mExpectedContinuityCounter >= 0
52287f2a558dd12043631e12c361abef301bf603140Andreas Huber            && (unsigned)mExpectedContinuityCounter != continuity_counter) {
52387f2a558dd12043631e12c361abef301bf603140Andreas Huber        ALOGI("discontinuity on stream pid 0x%04x", mElementaryPID);
52487f2a558dd12043631e12c361abef301bf603140Andreas Huber
52587f2a558dd12043631e12c361abef301bf603140Andreas Huber        mPayloadStarted = false;
52687f2a558dd12043631e12c361abef301bf603140Andreas Huber        mBuffer->setRange(0, 0);
52787f2a558dd12043631e12c361abef301bf603140Andreas Huber        mExpectedContinuityCounter = -1;
52887f2a558dd12043631e12c361abef301bf603140Andreas Huber
52987f2a558dd12043631e12c361abef301bf603140Andreas Huber        return OK;
53087f2a558dd12043631e12c361abef301bf603140Andreas Huber    }
53187f2a558dd12043631e12c361abef301bf603140Andreas Huber
53287f2a558dd12043631e12c361abef301bf603140Andreas Huber    mExpectedContinuityCounter = (continuity_counter + 1) & 0x0f;
53387f2a558dd12043631e12c361abef301bf603140Andreas Huber
534cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    if (payload_unit_start_indicator) {
535cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        if (mPayloadStarted) {
536cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            // Otherwise we run the danger of receiving the trailing bytes
537cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            // of a PES packet that we never saw the start of and assuming
538cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            // we have a a complete PES packet.
539cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
5405403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber            status_t err = flush();
5415403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber
5425403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber            if (err != OK) {
5435403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber                return err;
5445403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber            }
545cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        }
546cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
547cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        mPayloadStarted = true;
548cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
549cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
550cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    if (!mPayloadStarted) {
5515403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber        return OK;
552cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
553cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
554cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    size_t payloadSizeBits = br->numBitsLeft();
555f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ(payloadSizeBits % 8, 0u);
556cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
5573e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber    size_t neededSize = mBuffer->size() + payloadSizeBits / 8;
5583e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber    if (mBuffer->capacity() < neededSize) {
5593e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber        // Increment in multiples of 64K.
5603e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber        neededSize = (neededSize + 65535) & ~65535;
5613e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber
562df64d15042bbd5e0e4933ac49bf3c177dd94752cSteve Block        ALOGI("resizing buffer to %d bytes", neededSize);
5633e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber
5643e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber        sp<ABuffer> newBuffer = new ABuffer(neededSize);
5653e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber        memcpy(newBuffer->data(), mBuffer->data(), mBuffer->size());
5663e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber        newBuffer->setRange(0, mBuffer->size());
5673e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber        mBuffer = newBuffer;
5683e57322b332214e3cb1874e67a5704c9b2b5f6ecAndreas Huber    }
569cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
570cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    memcpy(mBuffer->data() + mBuffer->size(), br->data(), payloadSizeBits / 8);
571cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    mBuffer->setRange(0, mBuffer->size() + payloadSizeBits / 8);
5725403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber
5735403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber    return OK;
574cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
575cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
5766e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huberbool ATSParser::Stream::isVideo() const {
5776e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    switch (mStreamType) {
5786e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        case STREAMTYPE_H264:
5796e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        case STREAMTYPE_MPEG1_VIDEO:
5806e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        case STREAMTYPE_MPEG2_VIDEO:
5816e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        case STREAMTYPE_MPEG4_VIDEO:
5826e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber            return true;
5836e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber
5846e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        default:
5856e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber            return false;
5866e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    }
5876e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber}
5886e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber
5896e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huberbool ATSParser::Stream::isAudio() const {
5906e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    switch (mStreamType) {
5916e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        case STREAMTYPE_MPEG1_AUDIO:
5926e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        case STREAMTYPE_MPEG2_AUDIO:
5936e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        case STREAMTYPE_MPEG2_AUDIO_ADTS:
59490a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber        case STREAMTYPE_PCM_AUDIO:
5956e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber            return true;
5966e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber
5976e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        default:
5986e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber            return false;
5996e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    }
6006e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber}
6016e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber
60232f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Hubervoid ATSParser::Stream::signalDiscontinuity(
60332f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber        DiscontinuityType type, const sp<AMessage> &extra) {
6040389cc09f7b90f155a8942a0d2e1925cad1dbe2dMarco Nelissen    mExpectedContinuityCounter = -1;
6050389cc09f7b90f155a8942a0d2e1925cad1dbe2dMarco Nelissen
60618ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber    if (mQueue == NULL) {
60718ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber        return;
60818ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber    }
60918ac5407da14dad9731f40ffc9a56bee73830019Andreas Huber
6102a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber    mPayloadStarted = false;
6112a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber    mBuffer->setRange(0, 0);
6122a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber
6136e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    bool clearFormat = false;
6146e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    if (isAudio()) {
6156e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        if (type & DISCONTINUITY_AUDIO_FORMAT) {
6166e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber            clearFormat = true;
6176e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        }
6186e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    } else {
6196e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        if (type & DISCONTINUITY_VIDEO_FORMAT) {
6206e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber            clearFormat = true;
6216e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        }
6226e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    }
623f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
6246e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    mQueue->clear(clearFormat);
62532f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber
6266e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    if (type & DISCONTINUITY_TIME) {
6276e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        uint64_t resumeAtPTS;
6286e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        if (extra != NULL
6296e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber                && extra->findInt64(
6306e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber                    IStreamListener::kKeyResumeAtPTS,
6316e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber                    (int64_t *)&resumeAtPTS)) {
6326e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber            int64_t resumeAtMediaTimeUs =
6336e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber                mProgram->convertPTSToTimestamp(resumeAtPTS);
63432f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber
6356e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber            extra->setInt64("resume-at-mediatimeUs", resumeAtMediaTimeUs);
636e332a9181cf6a3155ed1a0fd2afc212ccb1f2753Andreas Huber        }
6376e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    }
638f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
6396e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber    if (mSource != NULL) {
6406e3d311b6631b12aac2879d1b08c3534aece78b1Andreas Huber        mSource->queueDiscontinuity(type, extra);
641bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber    }
642f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
643bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber
644f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ATSParser::Stream::signalEOS(status_t finalResult) {
645f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (mSource != NULL) {
646f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        mSource->signalEOS(finalResult);
6472a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber    }
6482a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber}
6492a4d22d79e927f2245537921e10fc5fda1c47a29Andreas Huber
6505403129e2a2f44620f2ac8109889e5a61be08732Andreas Huberstatus_t ATSParser::Stream::parsePES(ABitReader *br) {
651cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned packet_startcode_prefix = br->getBits(24);
652cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
6533856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("packet_startcode_prefix = 0x%08x", packet_startcode_prefix);
654cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
655386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    if (packet_startcode_prefix != 1) {
6563856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("Supposedly payload_unit_start=1 unit does not start "
657386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber             "with startcode.");
6585403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber
6595403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber        return ERROR_MALFORMED;
660386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    }
661386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber
662cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    CHECK_EQ(packet_startcode_prefix, 0x000001u);
663cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
664cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned stream_id = br->getBits(8);
6653856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("stream_id = 0x%02x", stream_id);
666cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
667cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    unsigned PES_packet_length = br->getBits(16);
6683856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("PES_packet_length = %u", PES_packet_length);
669cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
670cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    if (stream_id != 0xbc  // program_stream_map
671cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            && stream_id != 0xbe  // padding_stream
672cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            && stream_id != 0xbf  // private_stream_2
673cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            && stream_id != 0xf0  // ECM
674cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            && stream_id != 0xf1  // EMM
675cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            && stream_id != 0xff  // program_stream_directory
676cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            && stream_id != 0xf2  // DSMCC
677cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            && stream_id != 0xf8) {  // H.222.1 type E
678cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        CHECK_EQ(br->getBits(2), 2u);
679cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
6806e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber        MY_LOGV("PES_scrambling_control = %u", br->getBits(2));
6816e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber        MY_LOGV("PES_priority = %u", br->getBits(1));
6826e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber        MY_LOGV("data_alignment_indicator = %u", br->getBits(1));
6836e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber        MY_LOGV("copyright = %u", br->getBits(1));
6846e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber        MY_LOGV("original_or_copy = %u", br->getBits(1));
685cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
686cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned PTS_DTS_flags = br->getBits(2);
6873856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("PTS_DTS_flags = %u", PTS_DTS_flags);
688cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
689cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned ESCR_flag = br->getBits(1);
6903856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("ESCR_flag = %u", ESCR_flag);
691cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
692cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned ES_rate_flag = br->getBits(1);
6933856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("ES_rate_flag = %u", ES_rate_flag);
694cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
695cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned DSM_trick_mode_flag = br->getBits(1);
6963856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("DSM_trick_mode_flag = %u", DSM_trick_mode_flag);
697cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
698cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned additional_copy_info_flag = br->getBits(1);
6993856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("additional_copy_info_flag = %u", additional_copy_info_flag);
700cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
7016e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber        MY_LOGV("PES_CRC_flag = %u", br->getBits(1));
7026e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber        MY_LOGV("PES_extension_flag = %u", br->getBits(1));
703cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
704cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned PES_header_data_length = br->getBits(8);
7053856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("PES_header_data_length = %u", PES_header_data_length);
706cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
707cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned optional_bytes_remaining = PES_header_data_length;
708cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
709cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        uint64_t PTS = 0, DTS = 0;
710cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
711cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        if (PTS_DTS_flags == 2 || PTS_DTS_flags == 3) {
712cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_GE(optional_bytes_remaining, 5u);
713cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
714cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_EQ(br->getBits(4), PTS_DTS_flags);
715cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
716cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            PTS = ((uint64_t)br->getBits(3)) << 30;
717cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_EQ(br->getBits(1), 1u);
718cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            PTS |= ((uint64_t)br->getBits(15)) << 15;
719cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_EQ(br->getBits(1), 1u);
720cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            PTS |= br->getBits(15);
721cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_EQ(br->getBits(1), 1u);
722cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
72387f2a558dd12043631e12c361abef301bf603140Andreas Huber            ALOGV("PTS = 0x%016llx (%.2f)", PTS, PTS / 90000.0);
724cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
725cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            optional_bytes_remaining -= 5;
726cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
727cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            if (PTS_DTS_flags == 3) {
728cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber                CHECK_GE(optional_bytes_remaining, 5u);
729cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
730cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber                CHECK_EQ(br->getBits(4), 1u);
731cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
732cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber                DTS = ((uint64_t)br->getBits(3)) << 30;
733cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber                CHECK_EQ(br->getBits(1), 1u);
734cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber                DTS |= ((uint64_t)br->getBits(15)) << 15;
735cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber                CHECK_EQ(br->getBits(1), 1u);
736cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber                DTS |= br->getBits(15);
737cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber                CHECK_EQ(br->getBits(1), 1u);
738cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
7393856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("DTS = %llu", DTS);
740cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
741cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber                optional_bytes_remaining -= 5;
742cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            }
743cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        }
744cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
745cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        if (ESCR_flag) {
746cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_GE(optional_bytes_remaining, 6u);
747cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
748cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            br->getBits(2);
749cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
750cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            uint64_t ESCR = ((uint64_t)br->getBits(3)) << 30;
751cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_EQ(br->getBits(1), 1u);
752cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            ESCR |= ((uint64_t)br->getBits(15)) << 15;
753cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_EQ(br->getBits(1), 1u);
754cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            ESCR |= br->getBits(15);
755cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_EQ(br->getBits(1), 1u);
756cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
7573856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("ESCR = %llu", ESCR);
7586e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber            MY_LOGV("ESCR_extension = %u", br->getBits(9));
759cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
760cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_EQ(br->getBits(1), 1u);
761cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
762cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            optional_bytes_remaining -= 6;
763cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        }
764cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
765cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        if (ES_rate_flag) {
766cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_GE(optional_bytes_remaining, 3u);
767cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
768cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_EQ(br->getBits(1), 1u);
7696e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber            MY_LOGV("ES_rate = %u", br->getBits(22));
770cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_EQ(br->getBits(1), 1u);
771cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
772cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            optional_bytes_remaining -= 3;
773cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        }
774cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
775cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        br->skipBits(optional_bytes_remaining * 8);
776cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
777cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        // ES data follows.
778cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
779cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        if (PES_packet_length != 0) {
780cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            CHECK_GE(PES_packet_length, PES_header_data_length + 3);
781cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
782cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            unsigned dataLength =
783cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber                PES_packet_length - 3 - PES_header_data_length;
784cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
7855403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber            if (br->numBitsLeft() < dataLength * 8) {
78629357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block                ALOGE("PES packet does not carry enough data to contain "
7875403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber                     "payload. (numBitsLeft = %d, required = %d)",
7885403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber                     br->numBitsLeft(), dataLength * 8);
7895403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber
7905403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber                return ERROR_MALFORMED;
7915403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber            }
7925403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber
7930da4dab0a45a2bc1d95cbc6ef6a4850ed2569584Andreas Huber            CHECK_GE(br->numBitsLeft(), dataLength * 8);
7940da4dab0a45a2bc1d95cbc6ef6a4850ed2569584Andreas Huber
7950da4dab0a45a2bc1d95cbc6ef6a4850ed2569584Andreas Huber            onPayloadData(
7960da4dab0a45a2bc1d95cbc6ef6a4850ed2569584Andreas Huber                    PTS_DTS_flags, PTS, DTS, br->data(), dataLength);
797cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
798cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            br->skipBits(dataLength * 8);
799cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        } else {
8000da4dab0a45a2bc1d95cbc6ef6a4850ed2569584Andreas Huber            onPayloadData(
8010da4dab0a45a2bc1d95cbc6ef6a4850ed2569584Andreas Huber                    PTS_DTS_flags, PTS, DTS,
8020da4dab0a45a2bc1d95cbc6ef6a4850ed2569584Andreas Huber                    br->data(), br->numBitsLeft() / 8);
8030da4dab0a45a2bc1d95cbc6ef6a4850ed2569584Andreas Huber
804cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber            size_t payloadSizeBits = br->numBitsLeft();
805f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ(payloadSizeBits % 8, 0u);
806cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
8073856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("There's %d bytes of payload.", payloadSizeBits / 8);
808cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        }
809cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    } else if (stream_id == 0xbe) {  // padding_stream
810cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        CHECK_NE(PES_packet_length, 0u);
811cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        br->skipBits(PES_packet_length * 8);
812cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    } else {
813cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        CHECK_NE(PES_packet_length, 0u);
814cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        br->skipBits(PES_packet_length * 8);
815cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
8165403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber
8175403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber    return OK;
818cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
819cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
8205403129e2a2f44620f2ac8109889e5a61be08732Andreas Huberstatus_t ATSParser::Stream::flush() {
821cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    if (mBuffer->size() == 0) {
8225403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber        return OK;
823cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    }
824cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
8253856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("flushing stream 0x%04x size = %d", mElementaryPID, mBuffer->size());
826cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
827cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    ABitReader br(mBuffer->data(), mBuffer->size());
8285403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber
8295403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber    status_t err = parsePES(&br);
830cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
831cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    mBuffer->setRange(0, 0);
8325403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber
8335403129e2a2f44620f2ac8109889e5a61be08732Andreas Huber    return err;
834cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber}
835cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
836cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Hubervoid ATSParser::Stream::onPayloadData(
837cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        unsigned PTS_DTS_flags, uint64_t PTS, uint64_t DTS,
838cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber        const uint8_t *data, size_t size) {
83990a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#if 0
84090a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    ALOGI("payload streamType 0x%02x, PTS = 0x%016llx, dPTS = %lld",
84190a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber          mStreamType,
84290a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber          PTS,
84390a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber          (int64_t)PTS - mPrevPTS);
84490a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    mPrevPTS = PTS;
84590a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#endif
84690a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
8473856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("onPayloadData mStreamType=0x%02x", mStreamType);
848cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber
84998a46cf165d8de3779874eba01803bbc485f45b9Andreas Huber