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 <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 
     72         bool isInit() { return mInit; }
     73         off64_t getOffset() { return mOffset; }
     74         const sp<MediaSource> &getMediaSource() { return mMediaSource; }
     75         int64_t getTimeUs() { return mTimeUs; }
     76 
     77     private:
     78         bool mInit;
     79         /*
     80          * mInit == false: the current offset
     81          * mInit == true: the start offset of sync payload
     82          */
     83         off64_t mOffset;
     84         /* The media source object for this event. */
     85         sp<MediaSource> mMediaSource;
     86         /* The timestamp of the sync frame. */
     87         int64_t mTimeUs;
     88     };
     89 
     90     ATSParser(uint32_t flags = 0);
     91 
     92     // Feed a TS packet into the parser. uninitialized event with the start
     93     // offset of this TS packet goes in, and if the parser detects PES with
     94     // a sync frame, the event will be initiailzed with the start offset of the
     95     // PES. Note that the offset of the event can be different from what we fed,
     96     // as a PES may consist of multiple TS packets.
     97     //
     98     // Even in the case feedTSPacket() returns non-OK value, event still may be
     99     // initialized if the parsing failed after the detection.
    100     status_t feedTSPacket(
    101             const void *data, size_t size, SyncEvent *event = NULL);
    102 
    103     void signalDiscontinuity(
    104             DiscontinuityType type, const sp<AMessage> &extra);
    105 
    106     void signalEOS(status_t finalResult);
    107 
    108     enum SourceType {
    109         VIDEO = 0,
    110         AUDIO = 1,
    111         META  = 2,
    112         NUM_SOURCE_TYPES = 3
    113     };
    114     sp<MediaSource> getSource(SourceType type);
    115     bool hasSource(SourceType type) const;
    116 
    117     bool PTSTimeDeltaEstablished();
    118 
    119     enum {
    120         // From ISO/IEC 13818-1: 2000 (E), Table 2-29
    121         STREAMTYPE_RESERVED             = 0x00,
    122         STREAMTYPE_MPEG1_VIDEO          = 0x01,
    123         STREAMTYPE_MPEG2_VIDEO          = 0x02,
    124         STREAMTYPE_MPEG1_AUDIO          = 0x03,
    125         STREAMTYPE_MPEG2_AUDIO          = 0x04,
    126         STREAMTYPE_MPEG2_AUDIO_ADTS     = 0x0f,
    127         STREAMTYPE_MPEG4_VIDEO          = 0x10,
    128         STREAMTYPE_METADATA             = 0x15,
    129         STREAMTYPE_H264                 = 0x1b,
    130 
    131         // From ATSC A/53 Part 3:2009, 6.7.1
    132         STREAMTYPE_AC3                  = 0x81,
    133 
    134         // Stream type 0x83 is non-standard,
    135         // it could be LPCM or TrueHD AC3
    136         STREAMTYPE_LPCM_AC3             = 0x83,
    137     };
    138 
    139 protected:
    140     virtual ~ATSParser();
    141 
    142 private:
    143     struct Program;
    144     struct Stream;
    145     struct PSISection;
    146 
    147     uint32_t mFlags;
    148     Vector<sp<Program> > mPrograms;
    149 
    150     // Keyed by PID
    151     KeyedVector<unsigned, sp<PSISection> > mPSISections;
    152 
    153     int64_t mAbsoluteTimeAnchorUs;
    154 
    155     bool mTimeOffsetValid;
    156     int64_t mTimeOffsetUs;
    157     int64_t mLastRecoveredPTS;
    158 
    159     size_t mNumTSPacketsParsed;
    160 
    161     void parseProgramAssociationTable(ABitReader *br);
    162     void parseProgramMap(ABitReader *br);
    163     // Parse PES packet where br is pointing to. If the PES contains a sync
    164     // frame, set event with the time and the start offset of this PES.
    165     // Note that the method itself does not touch event.
    166     void parsePES(ABitReader *br, SyncEvent *event);
    167 
    168     // Strip remaining packet headers and pass to appropriate program/stream
    169     // to parse the payload. If the payload turns out to be PES and contains
    170     // a sync frame, event shall be set with the time and start offset of the
    171     // PES.
    172     // Note that the method itself does not touch event.
    173     status_t parsePID(
    174         ABitReader *br, unsigned PID,
    175         unsigned continuity_counter,
    176         unsigned payload_unit_start_indicator,
    177         SyncEvent *event);
    178 
    179     status_t parseAdaptationField(ABitReader *br, unsigned PID);
    180     // see feedTSPacket().
    181     status_t parseTS(ABitReader *br, SyncEvent *event);
    182 
    183     void updatePCR(unsigned PID, uint64_t PCR, size_t byteOffsetFromStart);
    184 
    185     uint64_t mPCR[2];
    186     size_t mPCRBytes[2];
    187     int64_t mSystemTimeUs[2];
    188     size_t mNumPCRs;
    189 
    190     DISALLOW_EVIL_CONSTRUCTORS(ATSParser);
    191 };
    192 
    193 }  // namespace android
    194 
    195 #endif  // A_TS_PARSER_H_
    196