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     explicit 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     ssize_t getSelectedTrack(media_track_type type) const;
42     bool isSelected() const;
43     void decode(const sp<ABuffer> &accessUnit);
44     void display(int64_t timeUs);
45     void flush();
46 
47 private:
48     // CC track identifier.
49     struct CCTrack {
CCTrackCCDecoder::CCTrack50         CCTrack() : mTrackType(0), mTrackChannel(0) { }
51 
CCTrackCCDecoder::CCTrack52         CCTrack(const int32_t trackType, const size_t trackChannel)
53             : mTrackType(trackType), mTrackChannel(trackChannel) { }
54 
55         int32_t mTrackType;
56         size_t mTrackChannel;
57 
58         // The ordering of CCTracks is to build a map of track to index.
59         // It is necessary to find the index of the matched CCTrack when CC data comes.
60         int compare(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         inline bool operator!=(const NuPlayer::CCDecoder::CCTrack& rhs) const;
64     };
65 
66     sp<AMessage> mNotify;
67     KeyedVector<int64_t, sp<ABuffer> > mCCMap;
68     ssize_t mSelectedTrack;
69     KeyedVector<CCTrack, size_t> mTrackIndices;
70     Vector<CCTrack> mTracks;
71 
72     // CEA-608 closed caption
73     size_t mLine21Channels[2]; // The current channels of NTSC_CC_FIELD_{1, 2}
74 
75     // CEA-708 closed caption
76     sp<ABuffer> mDTVCCPacket;
77 
78     bool isTrackValid(size_t index) const;
79     size_t getTrackIndex(int32_t trackType, size_t channel, bool *trackAdded);
80 
81     // Extract from H.264 SEIs
82     bool extractFromSEI(const sp<ABuffer> &accessUnit);
83     bool parseSEINalUnit(int64_t timeUs, const uint8_t *data, size_t size);
84 
85     // Extract from MPEG user data
86     bool extractFromMPEGUserData(const sp<ABuffer> &accessUnit);
87     bool parseMPEGUserDataUnit(int64_t timeUs, const uint8_t *data, size_t size);
88 
89     // Extract CC tracks from MPEG_cc_data
90     bool parseMPEGCCData(int64_t timeUs, const uint8_t *data, size_t size);
91     bool parseDTVCCPacket(int64_t timeUs, const uint8_t *data, size_t size);
92 
93     DISALLOW_EVIL_CONSTRUCTORS(CCDecoder);
94 };
95 
96 }  // namespace android
97 
98 #endif  // NUPLAYER_CCDECODER_H_
99