1 /*
2  * Copyright 2014 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 NUPLAYER_CCDECODER_H_
18 
19 #define NUPLAYER_CCDECODER_H_
20 
21 #include "NuPlayer.h"
22 
23 namespace android {
24 
25 struct NuPlayer::CCDecoder : public RefBase {
26     enum {
27         kWhatClosedCaptionData,
28         kWhatTrackAdded,
29     };
30 
31     enum {
32         kTrackTypeCEA608,
33         kTrackTypeCEA708,
34     };
35 
36     CCDecoder(const sp<AMessage> &notify);
37 
38     size_t getTrackCount() const;
39     sp<AMessage> getTrackInfo(size_t index) const;
40     status_t selectTrack(size_t index, bool select);
41     bool isSelected() const;
42     void decode(const sp<ABuffer> &accessUnit);
43     void display(int64_t timeUs);
44     void flush();
45 
46 private:
47     // CC track identifier.
48     struct CCTrack {
CCTrackCCDecoder::CCTrack49         CCTrack() : mTrackType(0), mTrackChannel(0) { }
50 
CCTrackCCDecoder::CCTrack51         CCTrack(const int32_t trackType, const size_t trackChannel)
52             : mTrackType(trackType), mTrackChannel(trackChannel) { }
53 
54         int32_t mTrackType;
55         size_t mTrackChannel;
56 
57         // The ordering of CCTracks is to build a map of track to index.
58         // It is necessary to find the index of the matched CCTrack when CC data comes.
59         int compare(const NuPlayer::CCDecoder::CCTrack& rhs) const;
60         inline bool operator<(const NuPlayer::CCDecoder::CCTrack& rhs) const;
61         inline bool operator==(const NuPlayer::CCDecoder::CCTrack& rhs) const;
62         inline bool operator!=(const NuPlayer::CCDecoder::CCTrack& rhs) const;
63     };
64 
65     sp<AMessage> mNotify;
66     KeyedVector<int64_t, sp<ABuffer> > mCCMap;
67     ssize_t mSelectedTrack;
68     KeyedVector<CCTrack, size_t> mTrackIndices;
69     Vector<CCTrack> mTracks;
70 
71     // CEA-608 closed caption
72     size_t mLine21Channels[2]; // The current channels of NTSC_CC_FIELD_{1, 2}
73 
74     // CEA-708 closed caption
75     sp<ABuffer> mDTVCCPacket;
76 
77     bool isTrackValid(size_t index) const;
78     size_t getTrackIndex(int32_t trackType, size_t channel, bool *trackAdded);
79 
80     // Extract from H.264 SEIs
81     bool extractFromSEI(const sp<ABuffer> &accessUnit);
82     bool parseSEINalUnit(int64_t timeUs, const uint8_t *data, size_t size);
83 
84     // Extract from MPEG user data
85     bool extractFromMPEGUserData(const sp<ABuffer> &accessUnit);
86     bool parseMPEGUserDataUnit(int64_t timeUs, const uint8_t *data, size_t size);
87 
88     // Extract CC tracks from MPEG_cc_data
89     bool parseMPEGCCData(int64_t timeUs, const uint8_t *data, size_t size);
90     bool parseDTVCCPacket(int64_t timeUs, const uint8_t *data, size_t size);
91 
92     DISALLOW_EVIL_CONSTRUCTORS(CCDecoder);
93 };
94 
95 }  // namespace android
96 
97 #endif  // NUPLAYER_CCDECODER_H_
98