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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "NuPlayerCCDecoder"
19 #include <utils/Log.h>
20 #include <inttypes.h>
21 
22 #include "avc_utils.h"
23 #include "NuPlayerCCDecoder.h"
24 
25 #include <media/stagefright/foundation/ABitReader.h>
26 #include <media/stagefright/foundation/ABuffer.h>
27 #include <media/stagefright/foundation/ADebug.h>
28 #include <media/stagefright/foundation/AMessage.h>
29 #include <media/stagefright/MediaDefs.h>
30 
31 namespace android {
32 
33 struct CCData {
CCDataandroid::CCData34     CCData(uint8_t type, uint8_t data1, uint8_t data2)
35         : mType(type), mData1(data1), mData2(data2) {
36     }
getChannelandroid::CCData37     bool getChannel(size_t *channel) const {
38         if (mData1 >= 0x10 && mData1 <= 0x1f) {
39             *channel = (mData1 >= 0x18 ? 1 : 0) + (mType ? 2 : 0);
40             return true;
41         }
42         return false;
43     }
44 
45     uint8_t mType;
46     uint8_t mData1;
47     uint8_t mData2;
48 };
49 
isNullPad(CCData * cc)50 static bool isNullPad(CCData *cc) {
51     return cc->mData1 < 0x10 && cc->mData2 < 0x10;
52 }
53 
54 static void dumpBytePair(const sp<ABuffer> &ccBuf) __attribute__ ((unused));
dumpBytePair(const sp<ABuffer> & ccBuf)55 static void dumpBytePair(const sp<ABuffer> &ccBuf) {
56     size_t offset = 0;
57     AString out;
58 
59     while (offset < ccBuf->size()) {
60         char tmp[128];
61 
62         CCData *cc = (CCData *) (ccBuf->data() + offset);
63 
64         if (isNullPad(cc)) {
65             // 1 null pad or XDS metadata, ignore
66             offset += sizeof(CCData);
67             continue;
68         }
69 
70         if (cc->mData1 >= 0x20 && cc->mData1 <= 0x7f) {
71             // 2 basic chars
72             sprintf(tmp, "[%d]Basic: %c %c", cc->mType, cc->mData1, cc->mData2);
73         } else if ((cc->mData1 == 0x11 || cc->mData1 == 0x19)
74                  && cc->mData2 >= 0x30 && cc->mData2 <= 0x3f) {
75             // 1 special char
76             sprintf(tmp, "[%d]Special: %02x %02x", cc->mType, cc->mData1, cc->mData2);
77         } else if ((cc->mData1 == 0x12 || cc->mData1 == 0x1A)
78                  && cc->mData2 >= 0x20 && cc->mData2 <= 0x3f){
79             // 1 Spanish/French char
80             sprintf(tmp, "[%d]Spanish: %02x %02x", cc->mType, cc->mData1, cc->mData2);
81         } else if ((cc->mData1 == 0x13 || cc->mData1 == 0x1B)
82                  && cc->mData2 >= 0x20 && cc->mData2 <= 0x3f){
83             // 1 Portuguese/German/Danish char
84             sprintf(tmp, "[%d]German: %02x %02x", cc->mType, cc->mData1, cc->mData2);
85         } else if ((cc->mData1 == 0x11 || cc->mData1 == 0x19)
86                  && cc->mData2 >= 0x20 && cc->mData2 <= 0x2f){
87             // Mid-Row Codes (Table 69)
88             sprintf(tmp, "[%d]Mid-row: %02x %02x", cc->mType, cc->mData1, cc->mData2);
89         } else if (((cc->mData1 == 0x14 || cc->mData1 == 0x1c)
90                   && cc->mData2 >= 0x20 && cc->mData2 <= 0x2f)
91                   ||
92                    ((cc->mData1 == 0x17 || cc->mData1 == 0x1f)
93                   && cc->mData2 >= 0x21 && cc->mData2 <= 0x23)){
94             // Misc Control Codes (Table 70)
95             sprintf(tmp, "[%d]Ctrl: %02x %02x", cc->mType, cc->mData1, cc->mData2);
96         } else if ((cc->mData1 & 0x70) == 0x10
97                 && (cc->mData2 & 0x40) == 0x40
98                 && ((cc->mData1 & 0x07) || !(cc->mData2 & 0x20)) ) {
99             // Preamble Address Codes (Table 71)
100             sprintf(tmp, "[%d]PAC: %02x %02x", cc->mType, cc->mData1, cc->mData2);
101         } else {
102             sprintf(tmp, "[%d]Invalid: %02x %02x", cc->mType, cc->mData1, cc->mData2);
103         }
104 
105         if (out.size() > 0) {
106             out.append(", ");
107         }
108 
109         out.append(tmp);
110 
111         offset += sizeof(CCData);
112     }
113 
114     ALOGI("%s", out.c_str());
115 }
116 
CCDecoder(const sp<AMessage> & notify)117 NuPlayer::CCDecoder::CCDecoder(const sp<AMessage> &notify)
118     : mNotify(notify),
119       mCurrentChannel(0),
120       mSelectedTrack(-1) {
121       for (size_t i = 0; i < sizeof(mTrackIndices)/sizeof(mTrackIndices[0]); ++i) {
122           mTrackIndices[i] = -1;
123       }
124 }
125 
getTrackCount() const126 size_t NuPlayer::CCDecoder::getTrackCount() const {
127     return mFoundChannels.size();
128 }
129 
getTrackInfo(size_t index) const130 sp<AMessage> NuPlayer::CCDecoder::getTrackInfo(size_t index) const {
131     if (!isTrackValid(index)) {
132         return NULL;
133     }
134 
135     sp<AMessage> format = new AMessage();
136 
137     format->setInt32("type", MEDIA_TRACK_TYPE_SUBTITLE);
138     format->setString("language", "und");
139     format->setString("mime", MEDIA_MIMETYPE_TEXT_CEA_608);
140     //CC1, field 0 channel 0
141     bool isDefaultAuto = (mFoundChannels[index] == 0);
142     format->setInt32("auto", isDefaultAuto);
143     format->setInt32("default", isDefaultAuto);
144     format->setInt32("forced", 0);
145 
146     return format;
147 }
148 
selectTrack(size_t index,bool select)149 status_t NuPlayer::CCDecoder::selectTrack(size_t index, bool select) {
150     if (!isTrackValid(index)) {
151         return BAD_VALUE;
152     }
153 
154     if (select) {
155         if (mSelectedTrack == (ssize_t)index) {
156             ALOGE("track %zu already selected", index);
157             return BAD_VALUE;
158         }
159         ALOGV("selected track %zu", index);
160         mSelectedTrack = index;
161     } else {
162         if (mSelectedTrack != (ssize_t)index) {
163             ALOGE("track %zu is not selected", index);
164             return BAD_VALUE;
165         }
166         ALOGV("unselected track %zu", index);
167         mSelectedTrack = -1;
168     }
169 
170     return OK;
171 }
172 
isSelected() const173 bool NuPlayer::CCDecoder::isSelected() const {
174     return mSelectedTrack >= 0 && mSelectedTrack < (int32_t) getTrackCount();
175 }
176 
isTrackValid(size_t index) const177 bool NuPlayer::CCDecoder::isTrackValid(size_t index) const {
178     return index < getTrackCount();
179 }
180 
getTrackIndex(size_t channel) const181 int32_t NuPlayer::CCDecoder::getTrackIndex(size_t channel) const {
182     if (channel < sizeof(mTrackIndices)/sizeof(mTrackIndices[0])) {
183         return mTrackIndices[channel];
184     }
185     return -1;
186 }
187 
188 // returns true if a new CC track is found
extractFromSEI(const sp<ABuffer> & accessUnit)189 bool NuPlayer::CCDecoder::extractFromSEI(const sp<ABuffer> &accessUnit) {
190     sp<ABuffer> sei;
191     if (!accessUnit->meta()->findBuffer("sei", &sei) || sei == NULL) {
192         return false;
193     }
194 
195     int64_t timeUs;
196     CHECK(accessUnit->meta()->findInt64("timeUs", &timeUs));
197 
198     bool trackAdded = false;
199 
200     const NALPosition *nal = (NALPosition *) sei->data();
201 
202     for (size_t i = 0; i < sei->size() / sizeof(NALPosition); ++i, ++nal) {
203         trackAdded |= parseSEINalUnit(
204                 timeUs, accessUnit->data() + nal->nalOffset, nal->nalSize);
205     }
206 
207     return trackAdded;
208 }
209 
210 // returns true if a new CC track is found
parseSEINalUnit(int64_t timeUs,const uint8_t * nalStart,size_t nalSize)211 bool NuPlayer::CCDecoder::parseSEINalUnit(
212         int64_t timeUs, const uint8_t *nalStart, size_t nalSize) {
213     unsigned nalType = nalStart[0] & 0x1f;
214 
215     // the buffer should only have SEI in it
216     if (nalType != 6) {
217         return false;
218     }
219 
220     bool trackAdded = false;
221     NALBitReader br(nalStart + 1, nalSize - 1);
222     // sei_message()
223     while (br.atLeastNumBitsLeft(16)) { // at least 16-bit for sei_message()
224         uint32_t payload_type = 0;
225         size_t payload_size = 0;
226         uint8_t last_byte;
227 
228         do {
229             last_byte = br.getBits(8);
230             payload_type += last_byte;
231         } while (last_byte == 0xFF);
232 
233         do {
234             last_byte = br.getBits(8);
235             payload_size += last_byte;
236         } while (last_byte == 0xFF);
237 
238         // sei_payload()
239         if (payload_type == 4) {
240             bool isCC = false;
241             if (payload_size > 1 + 2 + 4 + 1) {
242                 // user_data_registered_itu_t_t35()
243 
244                 // ATSC A/72: 6.4.2
245                 uint8_t itu_t_t35_country_code = br.getBits(8);
246                 uint16_t itu_t_t35_provider_code = br.getBits(16);
247                 uint32_t user_identifier = br.getBits(32);
248                 uint8_t user_data_type_code = br.getBits(8);
249 
250                 payload_size -= 1 + 2 + 4 + 1;
251 
252                 isCC = itu_t_t35_country_code == 0xB5
253                         && itu_t_t35_provider_code == 0x0031
254                         && user_identifier == 'GA94'
255                         && user_data_type_code == 0x3;
256             }
257 
258             if (isCC && payload_size > 2) {
259                 // MPEG_cc_data()
260                 // ATSC A/53 Part 4: 6.2.3.1
261                 br.skipBits(1); //process_em_data_flag
262                 bool process_cc_data_flag = br.getBits(1);
263                 br.skipBits(1); //additional_data_flag
264                 size_t cc_count = br.getBits(5);
265                 br.skipBits(8); // em_data;
266                 payload_size -= 2;
267 
268                 if (process_cc_data_flag) {
269                     AString out;
270 
271                     sp<ABuffer> ccBuf = new ABuffer(cc_count * sizeof(CCData));
272                     ccBuf->setRange(0, 0);
273 
274                     for (size_t i = 0; i < cc_count && payload_size >= 3; i++) {
275                         uint8_t marker = br.getBits(5);
276                         CHECK_EQ(marker, 0x1f);
277 
278                         bool cc_valid = br.getBits(1);
279                         uint8_t cc_type = br.getBits(2);
280                         // remove odd parity bit
281                         uint8_t cc_data_1 = br.getBits(8) & 0x7f;
282                         uint8_t cc_data_2 = br.getBits(8) & 0x7f;
283 
284                         payload_size -= 3;
285 
286                         if (cc_valid
287                                 && (cc_type == 0 || cc_type == 1)) {
288                             CCData cc(cc_type, cc_data_1, cc_data_2);
289                             if (!isNullPad(&cc)) {
290                                 size_t channel;
291                                 if (cc.getChannel(&channel) && getTrackIndex(channel) < 0) {
292                                     mTrackIndices[channel] = mFoundChannels.size();
293                                     mFoundChannels.push_back(channel);
294                                     trackAdded = true;
295                                 }
296                                 memcpy(ccBuf->data() + ccBuf->size(),
297                                         (void *)&cc, sizeof(cc));
298                                 ccBuf->setRange(0, ccBuf->size() + sizeof(CCData));
299                             }
300                         }
301                     }
302 
303                     mCCMap.add(timeUs, ccBuf);
304                     break;
305                 }
306             } else {
307                 ALOGV("Malformed SEI payload type 4");
308             }
309         } else {
310             ALOGV("Unsupported SEI payload type %d", payload_type);
311         }
312 
313         // skipping remaining bits of this payload
314         br.skipBits(payload_size * 8);
315     }
316 
317     return trackAdded;
318 }
319 
filterCCBuf(const sp<ABuffer> & ccBuf,size_t index)320 sp<ABuffer> NuPlayer::CCDecoder::filterCCBuf(
321         const sp<ABuffer> &ccBuf, size_t index) {
322     sp<ABuffer> filteredCCBuf = new ABuffer(ccBuf->size());
323     filteredCCBuf->setRange(0, 0);
324 
325     size_t cc_count = ccBuf->size() / sizeof(CCData);
326     const CCData* cc_data = (const CCData*)ccBuf->data();
327     for (size_t i = 0; i < cc_count; ++i) {
328         size_t channel;
329         if (cc_data[i].getChannel(&channel)) {
330             mCurrentChannel = channel;
331         }
332         if (mCurrentChannel == mFoundChannels[index]) {
333             memcpy(filteredCCBuf->data() + filteredCCBuf->size(),
334                     (void *)&cc_data[i], sizeof(CCData));
335             filteredCCBuf->setRange(0, filteredCCBuf->size() + sizeof(CCData));
336         }
337     }
338 
339     return filteredCCBuf;
340 }
341 
decode(const sp<ABuffer> & accessUnit)342 void NuPlayer::CCDecoder::decode(const sp<ABuffer> &accessUnit) {
343     if (extractFromSEI(accessUnit)) {
344         ALOGI("Found CEA-608 track");
345         sp<AMessage> msg = mNotify->dup();
346         msg->setInt32("what", kWhatTrackAdded);
347         msg->post();
348     }
349     // TODO: extract CC from other sources
350 }
351 
display(int64_t timeUs)352 void NuPlayer::CCDecoder::display(int64_t timeUs) {
353     if (!isTrackValid(mSelectedTrack)) {
354         ALOGE("Could not find current track(index=%d)", mSelectedTrack);
355         return;
356     }
357 
358     ssize_t index = mCCMap.indexOfKey(timeUs);
359     if (index < 0) {
360         ALOGV("cc for timestamp %" PRId64 " not found", timeUs);
361         return;
362     }
363 
364     sp<ABuffer> ccBuf = filterCCBuf(mCCMap.valueAt(index), mSelectedTrack);
365 
366     if (ccBuf->size() > 0) {
367 #if 0
368         dumpBytePair(ccBuf);
369 #endif
370 
371         ccBuf->meta()->setInt32("trackIndex", mSelectedTrack);
372         ccBuf->meta()->setInt64("timeUs", timeUs);
373         ccBuf->meta()->setInt64("durationUs", 0ll);
374 
375         sp<AMessage> msg = mNotify->dup();
376         msg->setInt32("what", kWhatClosedCaptionData);
377         msg->setBuffer("buffer", ccBuf);
378         msg->post();
379     }
380 
381     // remove all entries before timeUs
382     mCCMap.removeItemsAt(0, index + 1);
383 }
384 
flush()385 void NuPlayer::CCDecoder::flush() {
386     mCCMap.clear();
387 }
388 
389 }  // namespace android
390 
391