ATSParser.h revision fef808d42a9c94b0b5ef3c3d5fb0a090edbc42da
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#ifndef A_TS_PARSER_H_
18
19#define A_TS_PARSER_H_
20
21#include <sys/types.h>
22
23#include <media/stagefright/foundation/ABase.h>
24#include <media/stagefright/foundation/AMessage.h>
25#include <utils/KeyedVector.h>
26#include <utils/Vector.h>
27#include <utils/RefBase.h>
28
29namespace android {
30
31struct ABitReader;
32struct ABuffer;
33struct MediaSource;
34
35struct ATSParser : public RefBase {
36    enum DiscontinuityType {
37        DISCONTINUITY_NONE              = 0,
38        DISCONTINUITY_TIME              = 1,
39        DISCONTINUITY_AUDIO_FORMAT      = 2,
40        DISCONTINUITY_VIDEO_FORMAT      = 4,
41        DISCONTINUITY_ABSOLUTE_TIME     = 8,
42        DISCONTINUITY_TIME_OFFSET       = 16,
43
44        // For legacy reasons this also implies a time discontinuity.
45        DISCONTINUITY_FORMATCHANGE      =
46            DISCONTINUITY_AUDIO_FORMAT
47                | DISCONTINUITY_VIDEO_FORMAT
48                | DISCONTINUITY_TIME,
49    };
50
51    enum Flags {
52        // The 90kHz clock (PTS/DTS) is absolute, i.e. PTS=0 corresponds to
53        // a media time of 0.
54        // If this flag is _not_ specified, the first PTS encountered in a
55        // program of this stream will be assumed to correspond to media time 0
56        // instead.
57        TS_TIMESTAMPS_ARE_ABSOLUTE = 1,
58        // Video PES packets contain exactly one (aligned) access unit.
59        ALIGNED_VIDEO_DATA         = 2,
60    };
61
62    ATSParser(uint32_t flags = 0);
63
64    status_t feedTSPacket(const void *data, size_t size);
65
66    void signalDiscontinuity(
67            DiscontinuityType type, const sp<AMessage> &extra);
68
69    void signalEOS(status_t finalResult);
70
71    enum SourceType {
72        VIDEO = 0,
73        AUDIO = 1,
74        NUM_SOURCE_TYPES = 2
75    };
76    sp<MediaSource> getSource(SourceType type);
77
78    bool PTSTimeDeltaEstablished();
79
80    enum {
81        // From ISO/IEC 13818-1: 2000 (E), Table 2-29
82        STREAMTYPE_RESERVED             = 0x00,
83        STREAMTYPE_MPEG1_VIDEO          = 0x01,
84        STREAMTYPE_MPEG2_VIDEO          = 0x02,
85        STREAMTYPE_MPEG1_AUDIO          = 0x03,
86        STREAMTYPE_MPEG2_AUDIO          = 0x04,
87        STREAMTYPE_MPEG2_AUDIO_ADTS     = 0x0f,
88        STREAMTYPE_MPEG4_VIDEO          = 0x10,
89        STREAMTYPE_H264                 = 0x1b,
90
91        // From ATSC A/53 Part 3:2009, 6.7.1
92        STREAMTYPE_AC3                  = 0x81,
93
94        // Stream type 0x83 is non-standard,
95        // it could be LPCM or TrueHD AC3
96        STREAMTYPE_LPCM_AC3             = 0x83,
97    };
98
99protected:
100    virtual ~ATSParser();
101
102private:
103    struct Program;
104    struct Stream;
105    struct PSISection;
106
107    uint32_t mFlags;
108    Vector<sp<Program> > mPrograms;
109
110    // Keyed by PID
111    KeyedVector<unsigned, sp<PSISection> > mPSISections;
112
113    int64_t mAbsoluteTimeAnchorUs;
114
115    bool mTimeOffsetValid;
116    int64_t mTimeOffsetUs;
117
118    size_t mNumTSPacketsParsed;
119
120    void parseProgramAssociationTable(ABitReader *br);
121    void parseProgramMap(ABitReader *br);
122    void parsePES(ABitReader *br);
123
124    status_t parsePID(
125        ABitReader *br, unsigned PID,
126        unsigned continuity_counter,
127        unsigned payload_unit_start_indicator);
128
129    void parseAdaptationField(ABitReader *br, unsigned PID);
130    status_t parseTS(ABitReader *br);
131
132    void updatePCR(unsigned PID, uint64_t PCR, size_t byteOffsetFromStart);
133
134    uint64_t mPCR[2];
135    size_t mPCRBytes[2];
136    int64_t mSystemTimeUs[2];
137    size_t mNumPCRs;
138
139    DISALLOW_EVIL_CONSTRUCTORS(ATSParser);
140};
141
142}  // namespace android
143
144#endif  // A_TS_PARSER_H_
145