NuPlayerCCDecoder.cpp revision 303a2c2b7c321947fa6032893de3ed8a3d6e93ee
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
31namespace android {
32
33struct CCData {
34    CCData(uint8_t type, uint8_t data1, uint8_t data2)
35        : mType(type), mData1(data1), mData2(data2) {
36    }
37    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
50static bool isNullPad(CCData *cc) {
51    return cc->mData1 < 0x10 && cc->mData2 < 0x10;
52}
53
54static void dumpBytePair(const sp<ABuffer> &ccBuf) __attribute__ ((unused));
55static 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
117NuPlayer::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
126size_t NuPlayer::CCDecoder::getTrackCount() const {
127    return mFoundChannels.size();
128}
129
130sp<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
149status_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
173bool NuPlayer::CCDecoder::isSelected() const {
174    return mSelectedTrack >= 0 && mSelectedTrack < (int32_t) getTrackCount();
175}
176
177bool NuPlayer::CCDecoder::isTrackValid(size_t index) const {
178    return index < getTrackCount();
179}
180
181int32_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
189bool 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
211bool 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        if (payload_size > SIZE_MAX / 8
239                || !br.atLeastNumBitsLeft(payload_size * 8)) {
240            ALOGV("Malformed SEI payload");
241            break;
242        }
243
244        // sei_payload()
245        if (payload_type == 4) {
246            bool isCC = false;
247            if (payload_size > 1 + 2 + 4 + 1) {
248                // user_data_registered_itu_t_t35()
249
250                // ATSC A/72: 6.4.2
251                uint8_t itu_t_t35_country_code = br.getBits(8);
252                uint16_t itu_t_t35_provider_code = br.getBits(16);
253                uint32_t user_identifier = br.getBits(32);
254                uint8_t user_data_type_code = br.getBits(8);
255
256                payload_size -= 1 + 2 + 4 + 1;
257
258                isCC = itu_t_t35_country_code == 0xB5
259                        && itu_t_t35_provider_code == 0x0031
260                        && user_identifier == 'GA94'
261                        && user_data_type_code == 0x3;
262            }
263
264            if (isCC && payload_size > 2) {
265                // MPEG_cc_data()
266                // ATSC A/53 Part 4: 6.2.3.1
267                br.skipBits(1); //process_em_data_flag
268                bool process_cc_data_flag = br.getBits(1);
269                br.skipBits(1); //additional_data_flag
270                size_t cc_count = br.getBits(5);
271                br.skipBits(8); // em_data;
272                payload_size -= 2;
273
274                if (process_cc_data_flag) {
275                    AString out;
276
277                    sp<ABuffer> ccBuf = new ABuffer(cc_count * sizeof(CCData));
278                    ccBuf->setRange(0, 0);
279
280                    for (size_t i = 0; i < cc_count && payload_size >= 3; i++) {
281                        uint8_t marker = br.getBits(5);
282                        CHECK_EQ(marker, 0x1f);
283
284                        bool cc_valid = br.getBits(1);
285                        uint8_t cc_type = br.getBits(2);
286                        // remove odd parity bit
287                        uint8_t cc_data_1 = br.getBits(8) & 0x7f;
288                        uint8_t cc_data_2 = br.getBits(8) & 0x7f;
289
290                        payload_size -= 3;
291
292                        if (cc_valid
293                                && (cc_type == 0 || cc_type == 1)) {
294                            CCData cc(cc_type, cc_data_1, cc_data_2);
295                            if (!isNullPad(&cc)) {
296                                size_t channel;
297                                if (cc.getChannel(&channel) && getTrackIndex(channel) < 0) {
298                                    mTrackIndices[channel] = mFoundChannels.size();
299                                    mFoundChannels.push_back(channel);
300                                    trackAdded = true;
301                                }
302                                memcpy(ccBuf->data() + ccBuf->size(),
303                                        (void *)&cc, sizeof(cc));
304                                ccBuf->setRange(0, ccBuf->size() + sizeof(CCData));
305                            }
306                        }
307                    }
308
309                    mCCMap.add(timeUs, ccBuf);
310                    break;
311                }
312            } else {
313                ALOGV("Malformed SEI payload type 4");
314            }
315        } else {
316            ALOGV("Unsupported SEI payload type %d", payload_type);
317        }
318
319        // skipping remaining bits of this payload
320        br.skipBits(payload_size * 8);
321    }
322
323    return trackAdded;
324}
325
326sp<ABuffer> NuPlayer::CCDecoder::filterCCBuf(
327        const sp<ABuffer> &ccBuf, size_t index) {
328    sp<ABuffer> filteredCCBuf = new ABuffer(ccBuf->size());
329    filteredCCBuf->setRange(0, 0);
330
331    size_t cc_count = ccBuf->size() / sizeof(CCData);
332    const CCData* cc_data = (const CCData*)ccBuf->data();
333    for (size_t i = 0; i < cc_count; ++i) {
334        size_t channel;
335        if (cc_data[i].getChannel(&channel)) {
336            mCurrentChannel = channel;
337        }
338        if (mCurrentChannel == mFoundChannels[index]) {
339            memcpy(filteredCCBuf->data() + filteredCCBuf->size(),
340                    (void *)&cc_data[i], sizeof(CCData));
341            filteredCCBuf->setRange(0, filteredCCBuf->size() + sizeof(CCData));
342        }
343    }
344
345    return filteredCCBuf;
346}
347
348void NuPlayer::CCDecoder::decode(const sp<ABuffer> &accessUnit) {
349    if (extractFromSEI(accessUnit)) {
350        ALOGI("Found CEA-608 track");
351        sp<AMessage> msg = mNotify->dup();
352        msg->setInt32("what", kWhatTrackAdded);
353        msg->post();
354    }
355    // TODO: extract CC from other sources
356}
357
358void NuPlayer::CCDecoder::display(int64_t timeUs) {
359    if (!isTrackValid(mSelectedTrack)) {
360        ALOGE("Could not find current track(index=%d)", mSelectedTrack);
361        return;
362    }
363
364    ssize_t index = mCCMap.indexOfKey(timeUs);
365    if (index < 0) {
366        ALOGV("cc for timestamp %" PRId64 " not found", timeUs);
367        return;
368    }
369
370    sp<ABuffer> ccBuf = filterCCBuf(mCCMap.valueAt(index), mSelectedTrack);
371
372    if (ccBuf->size() > 0) {
373#if 0
374        dumpBytePair(ccBuf);
375#endif
376
377        ccBuf->meta()->setInt32("trackIndex", mSelectedTrack);
378        ccBuf->meta()->setInt64("timeUs", timeUs);
379        ccBuf->meta()->setInt64("durationUs", 0ll);
380
381        sp<AMessage> msg = mNotify->dup();
382        msg->setInt32("what", kWhatClosedCaptionData);
383        msg->setBuffer("buffer", ccBuf);
384        msg->post();
385    }
386
387    // remove all entries before timeUs
388    mCCMap.removeItemsAt(0, index + 1);
389}
390
391void NuPlayer::CCDecoder::flush() {
392    mCCMap.clear();
393}
394
395}  // namespace android
396
397