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 <media/stagefright/MediaSource.h>
26 #include <utils/KeyedVector.h>
27 #include <utils/Vector.h>
28 #include <utils/RefBase.h>
29 
30 namespace android {
31 
32 class ABitReader;
33 struct ABuffer;
34 
35 struct 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         DISCONTINUITY_FORMAT_ONLY       =
50             DISCONTINUITY_AUDIO_FORMAT
51                 | DISCONTINUITY_VIDEO_FORMAT,
52     };
53 
54     enum Flags {
55         // The 90kHz clock (PTS/DTS) is absolute, i.e. PTS=0 corresponds to
56         // a media time of 0.
57         // If this flag is _not_ specified, the first PTS encountered in a
58         // program of this stream will be assumed to correspond to media time 0
59         // instead.
60         TS_TIMESTAMPS_ARE_ABSOLUTE = 1,
61         // Video PES packets contain exactly one (aligned) access unit.
62         ALIGNED_VIDEO_DATA         = 2,
63     };
64 
65     // Event is used to signal sync point event at feedTSPacket().
66     struct SyncEvent {
67         SyncEvent(off64_t offset);
68 
69         void init(off64_t offset, const sp<MediaSource> &source,
70                 int64_t timeUs);
71 
hasReturnedDataATSParser::SyncEvent72         bool hasReturnedData() const { return mHasReturnedData; }
73         void reset();
getOffsetATSParser::SyncEvent74         off64_t getOffset() const { return mOffset; }
getMediaSourceATSParser::SyncEvent75         const sp<MediaSource> &getMediaSource() const { return mMediaSource; }
getTimeUsATSParser::SyncEvent76         int64_t getTimeUs() const { return mTimeUs; }
77 
78     private:
79         bool mHasReturnedData;
80         /*
81          * mHasReturnedData == false: the current offset (or undefined if the returned data
82                                       has been invalidated via reset())
83          * mHasReturnedData == true: the start offset of sync payload
84          */
85         off64_t mOffset;
86         /* The media source object for this event. */
87         sp<MediaSource> mMediaSource;
88         /* The timestamp of the sync frame. */
89         int64_t mTimeUs;
90     };
91 
92     ATSParser(uint32_t flags = 0);
93 
94     // Feed a TS packet into the parser. uninitialized event with the start
95     // offset of this TS packet goes in, and if the parser detects PES with
96     // a sync frame, the event will be initiailzed with the start offset of the
97     // PES. Note that the offset of the event can be different from what we fed,
98     // as a PES may consist of multiple TS packets.
99     //
100     // Even in the case feedTSPacket() returns non-OK value, event still may be
101     // initialized if the parsing failed after the detection.
102     status_t feedTSPacket(
103             const void *data, size_t size, SyncEvent *event = NULL);
104 
105     void signalDiscontinuity(
106             DiscontinuityType type, const sp<AMessage> &extra);
107 
108     void signalEOS(status_t finalResult);
109 
110     enum SourceType {
111         VIDEO = 0,
112         AUDIO = 1,
113         META  = 2,
114         NUM_SOURCE_TYPES = 3
115     };
116     sp<MediaSource> getSource(SourceType type);
117     bool hasSource(SourceType type) const;
118 
119     bool PTSTimeDeltaEstablished();
120 
121     enum {
122         // From ISO/IEC 13818-1: 2000 (E), Table 2-29
123         STREAMTYPE_RESERVED             = 0x00,
124         STREAMTYPE_MPEG1_VIDEO          = 0x01,
125         STREAMTYPE_MPEG2_VIDEO          = 0x02,
126         STREAMTYPE_MPEG1_AUDIO          = 0x03,
127         STREAMTYPE_MPEG2_AUDIO          = 0x04,
128         STREAMTYPE_MPEG2_AUDIO_ADTS     = 0x0f,
129         STREAMTYPE_MPEG4_VIDEO          = 0x10,
130         STREAMTYPE_METADATA             = 0x15,
131         STREAMTYPE_H264                 = 0x1b,
132 
133         // From ATSC A/53 Part 3:2009, 6.7.1
134         STREAMTYPE_AC3                  = 0x81,
135 
136         // Stream type 0x83 is non-standard,
137         // it could be LPCM or TrueHD AC3
138         STREAMTYPE_LPCM_AC3             = 0x83,
139     };
140 
141 protected:
142     virtual ~ATSParser();
143 
144 private:
145     struct Program;
146     struct Stream;
147     struct PSISection;
148 
149     uint32_t mFlags;
150     Vector<sp<Program> > mPrograms;
151 
152     // Keyed by PID
153     KeyedVector<unsigned, sp<PSISection> > mPSISections;
154 
155     int64_t mAbsoluteTimeAnchorUs;
156 
157     bool mTimeOffsetValid;
158     int64_t mTimeOffsetUs;
159     int64_t mLastRecoveredPTS;
160 
161     size_t mNumTSPacketsParsed;
162 
163     void parseProgramAssociationTable(ABitReader *br);
164     void parseProgramMap(ABitReader *br);
165     // Parse PES packet where br is pointing to. If the PES contains a sync
166     // frame, set event with the time and the start offset of this PES.
167     // Note that the method itself does not touch event.
168     void parsePES(ABitReader *br, SyncEvent *event);
169 
170     // Strip remaining packet headers and pass to appropriate program/stream
171     // to parse the payload. If the payload turns out to be PES and contains
172     // a sync frame, event shall be set with the time and start offset of the
173     // PES.
174     // Note that the method itself does not touch event.
175     status_t parsePID(
176         ABitReader *br, unsigned PID,
177         unsigned continuity_counter,
178         unsigned payload_unit_start_indicator,
179         SyncEvent *event);
180 
181     status_t parseAdaptationField(ABitReader *br, unsigned PID);
182     // see feedTSPacket().
183     status_t parseTS(ABitReader *br, SyncEvent *event);
184 
185     void updatePCR(unsigned PID, uint64_t PCR, uint64_t byteOffsetFromStart);
186 
187     uint64_t mPCR[2];
188     uint64_t mPCRBytes[2];
189     int64_t mSystemTimeUs[2];
190     size_t mNumPCRs;
191 
192     DISALLOW_EVIL_CONSTRUCTORS(ATSParser);
193 };
194 
195 }  // namespace android
196 
197 #endif  // A_TS_PARSER_H_
198