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/MediaSource.h> 24 #include <media/stagefright/foundation/ABase.h> 25 #include <media/stagefright/foundation/AMessage.h> 26 #include <media/stagefright/foundation/AudioPresentationInfo.h> 27 #include <utils/KeyedVector.h> 28 #include <utils/Vector.h> 29 #include <utils/RefBase.h> 30 #include <vector> 31 32 namespace android { 33 namespace hardware { 34 namespace cas { 35 namespace V1_0 { 36 struct ICas; 37 }}} 38 using hardware::cas::V1_0::ICas; 39 40 class ABitReader; 41 struct ABuffer; 42 struct AnotherPacketSource; 43 44 struct ATSParser : public RefBase { 45 enum DiscontinuityType { 46 DISCONTINUITY_NONE = 0, 47 DISCONTINUITY_TIME = 1, 48 DISCONTINUITY_AUDIO_FORMAT = 2, 49 DISCONTINUITY_VIDEO_FORMAT = 4, 50 DISCONTINUITY_ABSOLUTE_TIME = 8, 51 DISCONTINUITY_TIME_OFFSET = 16, 52 53 // For legacy reasons this also implies a time discontinuity. 54 DISCONTINUITY_FORMATCHANGE = 55 DISCONTINUITY_AUDIO_FORMAT 56 | DISCONTINUITY_VIDEO_FORMAT 57 | DISCONTINUITY_TIME, 58 DISCONTINUITY_FORMAT_ONLY = 59 DISCONTINUITY_AUDIO_FORMAT 60 | DISCONTINUITY_VIDEO_FORMAT, 61 }; 62 63 enum Flags { 64 // The 90kHz clock (PTS/DTS) is absolute, i.e. PTS=0 corresponds to 65 // a media time of 0. 66 // If this flag is _not_ specified, the first PTS encountered in a 67 // program of this stream will be assumed to correspond to media time 0 68 // instead. 69 TS_TIMESTAMPS_ARE_ABSOLUTE = 1, 70 // Video PES packets contain exactly one (aligned) access unit. 71 ALIGNED_VIDEO_DATA = 2, 72 }; 73 74 enum SourceType { 75 VIDEO = 0, 76 AUDIO = 1, 77 META = 2, 78 NUM_SOURCE_TYPES = 3 79 }; 80 81 // Event is used to signal sync point event at feedTSPacket(). 82 struct SyncEvent { 83 explicit SyncEvent(off64_t offset); 84 85 void init(off64_t offset, const sp<AnotherPacketSource> &source, 86 int64_t timeUs, SourceType type); 87 hasReturnedDataATSParser::SyncEvent88 bool hasReturnedData() const { return mHasReturnedData; } 89 void reset(); getOffsetATSParser::SyncEvent90 off64_t getOffset() const { return mOffset; } getMediaSourceATSParser::SyncEvent91 const sp<AnotherPacketSource> &getMediaSource() const { return mMediaSource; } getTimeUsATSParser::SyncEvent92 int64_t getTimeUs() const { return mTimeUs; } getTypeATSParser::SyncEvent93 SourceType getType() const { return mType; } 94 95 private: 96 bool mHasReturnedData; 97 /* 98 * mHasReturnedData == false: the current offset (or undefined if the returned data 99 has been invalidated via reset()) 100 * mHasReturnedData == true: the start offset of sync payload 101 */ 102 off64_t mOffset; 103 /* The media source object for this event. */ 104 sp<AnotherPacketSource> mMediaSource; 105 /* The timestamp of the sync frame. */ 106 int64_t mTimeUs; 107 SourceType mType; 108 }; 109 110 explicit ATSParser(uint32_t flags = 0); 111 112 status_t setMediaCas(const sp<ICas> &cas); 113 114 // Feed a TS packet into the parser. uninitialized event with the start 115 // offset of this TS packet goes in, and if the parser detects PES with 116 // a sync frame, the event will be initiailzed with the start offset of the 117 // PES. Note that the offset of the event can be different from what we fed, 118 // as a PES may consist of multiple TS packets. 119 // 120 // Even in the case feedTSPacket() returns non-OK value, event still may be 121 // initialized if the parsing failed after the detection. 122 status_t feedTSPacket( 123 const void *data, size_t size, SyncEvent *event = NULL); 124 125 void signalDiscontinuity( 126 DiscontinuityType type, const sp<AMessage> &extra); 127 128 void signalEOS(status_t finalResult); 129 130 sp<AnotherPacketSource> getSource(SourceType type); 131 bool hasSource(SourceType type) const; 132 133 bool PTSTimeDeltaEstablished(); 134 135 int64_t getFirstPTSTimeUs(); 136 137 void signalNewSampleAesKey(const sp<AMessage> &keyItem); 138 139 enum { 140 // From ISO/IEC 13818-1: 2000 (E), Table 2-29 141 STREAMTYPE_RESERVED = 0x00, 142 STREAMTYPE_MPEG1_VIDEO = 0x01, 143 STREAMTYPE_MPEG2_VIDEO = 0x02, 144 STREAMTYPE_MPEG1_AUDIO = 0x03, 145 STREAMTYPE_MPEG2_AUDIO = 0x04, 146 STREAMTYPE_PES_PRIVATE_DATA = 0x06, 147 STREAMTYPE_MPEG2_AUDIO_ADTS = 0x0f, 148 STREAMTYPE_MPEG4_VIDEO = 0x10, 149 STREAMTYPE_METADATA = 0x15, 150 STREAMTYPE_H264 = 0x1b, 151 152 // From ATSC A/53 Part 3:2009, 6.7.1 153 STREAMTYPE_AC3 = 0x81, 154 155 // Stream type 0x83 is non-standard, 156 // it could be LPCM or TrueHD AC3 157 STREAMTYPE_LPCM_AC3 = 0x83, 158 STREAMTYPE_EAC3 = 0x87, 159 160 // DTS audio stream type which contains only Core substream 161 STREAMTYPE_DTS = 0x8A, 162 163 //Sample Encrypted types 164 STREAMTYPE_H264_ENCRYPTED = 0xDB, 165 STREAMTYPE_AAC_ENCRYPTED = 0xCF, 166 STREAMTYPE_AC3_ENCRYPTED = 0xC1, 167 }; 168 169 enum { 170 // From ISO/IEC 13818-1: 2007 (E), Table 2-45 171 DESCRIPTOR_CA = 0x09, 172 173 // DVB BlueBook A038 Table 12 174 DESCRIPTOR_DTS = 0x7B, 175 DESCRIPTOR_DVB_EXTENSION = 0x7F, 176 }; 177 178 // DVB BlueBook A038 Table 109 179 enum { 180 EXT_DESCRIPTOR_DVB_AC4 = 0x15, 181 EXT_DESCRIPTOR_DVB_AUDIO_PRESELECTION = 0x19, 182 EXT_DESCRIPTOR_DVB_DTS_HD = 0x0E, 183 EXT_DESCRIPTOR_DVB_DTS_UHD = 0x21, 184 EXT_DESCRIPTOR_DVB_RESERVED_MAX = 0x7F, 185 }; 186 187 protected: 188 virtual ~ATSParser(); 189 190 private: 191 struct Program; 192 struct Stream; 193 struct PSISection; 194 struct CasManager; 195 struct CADescriptor { CADescriptorATSParser::CADescriptor196 CADescriptor() : mPID(0), mSystemID(-1) {} 197 unsigned mPID; 198 int32_t mSystemID; 199 std::vector<uint8_t> mPrivateData; 200 }; 201 202 struct StreamInfo { 203 unsigned mType; 204 unsigned mTypeExt; 205 unsigned mPID; 206 CADescriptor mCADescriptor; 207 AudioPresentationCollection mAudioPresentations; 208 }; 209 210 sp<CasManager> mCasManager; 211 212 uint32_t mFlags; 213 Vector<sp<Program> > mPrograms; 214 215 // Keyed by PID 216 KeyedVector<unsigned, sp<PSISection> > mPSISections; 217 218 int64_t mAbsoluteTimeAnchorUs; 219 220 bool mTimeOffsetValid; 221 int64_t mTimeOffsetUs; 222 int64_t mLastRecoveredPTS; 223 224 size_t mNumTSPacketsParsed; 225 226 sp<AMessage> mSampleAesKeyItem; 227 228 void parseProgramAssociationTable(ABitReader *br); 229 void parseProgramMap(ABitReader *br); 230 // Parse PES packet where br is pointing to. If the PES contains a sync 231 // frame, set event with the time and the start offset of this PES. 232 // Note that the method itself does not touch event. 233 void parsePES(ABitReader *br, SyncEvent *event); 234 235 // Strip remaining packet headers and pass to appropriate program/stream 236 // to parse the payload. If the payload turns out to be PES and contains 237 // a sync frame, event shall be set with the time and start offset of the 238 // PES. 239 // Note that the method itself does not touch event. 240 status_t parsePID( 241 ABitReader *br, unsigned PID, 242 unsigned continuity_counter, 243 unsigned payload_unit_start_indicator, 244 unsigned transport_scrambling_control, 245 unsigned random_access_indicator, 246 SyncEvent *event); 247 248 status_t parseAdaptationField( 249 ABitReader *br, unsigned PID, unsigned *random_access_indicator); 250 251 // see feedTSPacket(). 252 status_t parseTS(ABitReader *br, SyncEvent *event); 253 254 void updatePCR(unsigned PID, uint64_t PCR, uint64_t byteOffsetFromStart); 255 256 uint64_t mPCR[2]; 257 uint64_t mPCRBytes[2]; 258 int64_t mSystemTimeUs[2]; 259 size_t mNumPCRs; 260 261 DISALLOW_EVIL_CONSTRUCTORS(ATSParser); 262 }; 263 264 } // namespace android 265 266 #endif // A_TS_PARSER_H_ 267