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