Home | History | Annotate | Download | only in mpeg2ts
      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 
     29 namespace android {
     30 
     31 struct ABitReader;
     32 struct ABuffer;
     33 struct MediaSource;
     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     };
     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     bool hasSource(SourceType type) const;
     78 
     79     bool PTSTimeDeltaEstablished();
     80 
     81     enum {
     82         // From ISO/IEC 13818-1: 2000 (E), Table 2-29
     83         STREAMTYPE_RESERVED             = 0x00,
     84         STREAMTYPE_MPEG1_VIDEO          = 0x01,
     85         STREAMTYPE_MPEG2_VIDEO          = 0x02,
     86         STREAMTYPE_MPEG1_AUDIO          = 0x03,
     87         STREAMTYPE_MPEG2_AUDIO          = 0x04,
     88         STREAMTYPE_MPEG2_AUDIO_ADTS     = 0x0f,
     89         STREAMTYPE_MPEG4_VIDEO          = 0x10,
     90         STREAMTYPE_H264                 = 0x1b,
     91 
     92         // From ATSC A/53 Part 3:2009, 6.7.1
     93         STREAMTYPE_AC3                  = 0x81,
     94 
     95         // Stream type 0x83 is non-standard,
     96         // it could be LPCM or TrueHD AC3
     97         STREAMTYPE_LPCM_AC3             = 0x83,
     98     };
     99 
    100 protected:
    101     virtual ~ATSParser();
    102 
    103 private:
    104     struct Program;
    105     struct Stream;
    106     struct PSISection;
    107 
    108     uint32_t mFlags;
    109     Vector<sp<Program> > mPrograms;
    110 
    111     // Keyed by PID
    112     KeyedVector<unsigned, sp<PSISection> > mPSISections;
    113 
    114     int64_t mAbsoluteTimeAnchorUs;
    115 
    116     bool mTimeOffsetValid;
    117     int64_t mTimeOffsetUs;
    118 
    119     size_t mNumTSPacketsParsed;
    120 
    121     void parseProgramAssociationTable(ABitReader *br);
    122     void parseProgramMap(ABitReader *br);
    123     void parsePES(ABitReader *br);
    124 
    125     status_t parsePID(
    126         ABitReader *br, unsigned PID,
    127         unsigned continuity_counter,
    128         unsigned payload_unit_start_indicator);
    129 
    130     void parseAdaptationField(ABitReader *br, unsigned PID);
    131     status_t parseTS(ABitReader *br);
    132 
    133     void updatePCR(unsigned PID, uint64_t PCR, size_t byteOffsetFromStart);
    134 
    135     uint64_t mPCR[2];
    136     size_t mPCRBytes[2];
    137     int64_t mSystemTimeUs[2];
    138     size_t mNumPCRs;
    139 
    140     DISALLOW_EVIL_CONSTRUCTORS(ATSParser);
    141 };
    142 
    143 }  // namespace android
    144 
    145 #endif  // A_TS_PARSER_H_
    146