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