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/Vector.h>
     26 #include <utils/RefBase.h>
     27 
     28 namespace android {
     29 
     30 struct ABitReader;
     31 struct ABuffer;
     32 struct MediaSource;
     33 
     34 struct ATSParser : public RefBase {
     35     enum DiscontinuityType {
     36         DISCONTINUITY_NONE              = 0,
     37         DISCONTINUITY_TIME              = 1,
     38         DISCONTINUITY_AUDIO_FORMAT      = 2,
     39         DISCONTINUITY_VIDEO_FORMAT      = 4,
     40 
     41         DISCONTINUITY_SEEK              = DISCONTINUITY_TIME,
     42 
     43         // For legacy reasons this also implies a time discontinuity.
     44         DISCONTINUITY_FORMATCHANGE      =
     45             DISCONTINUITY_AUDIO_FORMAT
     46                 | DISCONTINUITY_VIDEO_FORMAT
     47                 | DISCONTINUITY_TIME,
     48     };
     49 
     50     enum Flags {
     51         // The 90kHz clock (PTS/DTS) is absolute, i.e. PTS=0 corresponds to
     52         // a media time of 0.
     53         // If this flag is _not_ specified, the first PTS encountered in a
     54         // program of this stream will be assumed to correspond to media time 0
     55         // instead.
     56         TS_TIMESTAMPS_ARE_ABSOLUTE = 1
     57     };
     58 
     59     ATSParser(uint32_t flags = 0);
     60 
     61     status_t feedTSPacket(const void *data, size_t size);
     62 
     63     void signalDiscontinuity(
     64             DiscontinuityType type, const sp<AMessage> &extra);
     65 
     66     void signalEOS(status_t finalResult);
     67 
     68     enum SourceType {
     69         VIDEO,
     70         AUDIO
     71     };
     72     sp<MediaSource> getSource(SourceType type);
     73 
     74     bool PTSTimeDeltaEstablished();
     75 
     76     enum {
     77         // From ISO/IEC 13818-1: 2000 (E), Table 2-29
     78         STREAMTYPE_RESERVED             = 0x00,
     79         STREAMTYPE_MPEG1_VIDEO          = 0x01,
     80         STREAMTYPE_MPEG2_VIDEO          = 0x02,
     81         STREAMTYPE_MPEG1_AUDIO          = 0x03,
     82         STREAMTYPE_MPEG2_AUDIO          = 0x04,
     83         STREAMTYPE_MPEG2_AUDIO_ADTS     = 0x0f,
     84         STREAMTYPE_MPEG4_VIDEO          = 0x10,
     85         STREAMTYPE_H264                 = 0x1b,
     86     };
     87 
     88 protected:
     89     virtual ~ATSParser();
     90 
     91 private:
     92     struct Program;
     93     struct Stream;
     94 
     95     uint32_t mFlags;
     96     Vector<sp<Program> > mPrograms;
     97 
     98     void parseProgramAssociationTable(ABitReader *br);
     99     void parseProgramMap(ABitReader *br);
    100     void parsePES(ABitReader *br);
    101 
    102     status_t parsePID(
    103         ABitReader *br, unsigned PID,
    104         unsigned payload_unit_start_indicator);
    105 
    106     void parseAdaptationField(ABitReader *br);
    107     status_t parseTS(ABitReader *br);
    108 
    109     DISALLOW_EVIL_CONSTRUCTORS(ATSParser);
    110 };
    111 
    112 }  // namespace android
    113 
    114 #endif  // A_TS_PARSER_H_
    115