summaryrefslogtreecommitdiffstats
path: root/media/libmedia/CharacterEncodingDetector.cpp
blob: eb091ac0715ecb9fab0b9981de6f358ebc407e7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "CharacterEncodingDector"
#include <utils/Log.h>

#include "CharacterEncodingDetector.h"
#include "CharacterEncodingDetectorTables.h"

#include "utils/Vector.h"
#include "StringArray.h"

#include "unicode/ucnv.h"
#include "unicode/ucsdet.h"
#include "unicode/ustring.h"

namespace android {

CharacterEncodingDetector::CharacterEncodingDetector() {

    UErrorCode status = U_ZERO_ERROR;
    mUtf8Conv = ucnv_open("UTF-8", &status);
    if (U_FAILURE(status)) {
        ALOGE("could not create UConverter for UTF-8");
        mUtf8Conv = NULL;
    }
}

CharacterEncodingDetector::~CharacterEncodingDetector() {
    ucnv_close(mUtf8Conv);
}

void CharacterEncodingDetector::addTag(const char *name, const char *value) {
    mNames.push_back(name);
    mValues.push_back(value);
}

size_t CharacterEncodingDetector::size() {
    return mNames.size();
}

status_t CharacterEncodingDetector::getTag(int index, const char **name, const char**value) {
    if (index >= mNames.size()) {
        return BAD_VALUE;
    }

    *name = mNames.getEntry(index);
    *value = mValues.getEntry(index);
    return OK;
}

static bool isPrintableAscii(const char *value, size_t len) {
    for (size_t i = 0; i < len; i++) {
        if ((value[i] & 0x80) || value[i] < 0x20 || value[i] == 0x7f) {
            return false;
        }
    }
    return true;
}

void CharacterEncodingDetector::detectAndConvert() {

    int size = mNames.size();
    ALOGV("%d tags before conversion", size);
    for (int i = 0; i < size; i++) {
        ALOGV("%s: %s", mNames.getEntry(i), mValues.getEntry(i));
    }

    if (size && mUtf8Conv) {

        UErrorCode status = U_ZERO_ERROR;
        UCharsetDetector *csd = ucsdet_open(&status);
        const UCharsetMatch *ucm;

        // try combined detection of artist/album/title etc.
        char buf[1024];
        buf[0] = 0;
        int idx;
        for (int i = 0; i < size; i++) {
            const char *name = mNames.getEntry(i);
            const char *value = mValues.getEntry(i);
            if (!isPrintableAscii(value, strlen(value)) && (
                        !strcmp(name, "artist") ||
                        !strcmp(name, "albumartist") ||
                        !strcmp(name, "composer") ||
                        !strcmp(name, "genre") ||
                        !strcmp(name, "album") ||
                        !strcmp(name, "title"))) {
                strlcat(buf, value, sizeof(buf));
                // separate tags by space so ICU's ngram detector can do its job
                strlcat(buf, " ", sizeof(buf));
            }
        }
        ucsdet_setText(csd, buf, strlen(buf), &status);

        int32_t matches;
        const UCharsetMatch** ucma = ucsdet_detectAll(csd, &matches, &status);
        const char *combinedenc = "???";

        const UCharsetMatch* bestCombinedMatch = getPreferred(buf, strlen(buf), ucma, matches);

        if (bestCombinedMatch != NULL) {
            combinedenc = ucsdet_getName(bestCombinedMatch, &status);
        }

        for (int i = 0; i < size; i++) {
            const char *name = mNames.getEntry(i);
            uint8_t* src = (uint8_t *)mValues.getEntry(i);
            int len = strlen((char *)src);
            uint8_t* dest = src;

            ALOGV("@@@ checking %s", name);
            const char *s = mValues.getEntry(i);
            int32_t inputLength = strlen(s);
            const char *enc;

            if (!strcmp(name, "artist") ||
                    !strcmp(name, "albumartist") ||
                    !strcmp(name, "composer") ||
                    !strcmp(name, "genre") ||
                    !strcmp(name, "album") ||
                    !strcmp(name, "title")) {
                // use encoding determined from the combination of artist/album/title etc.
                enc = combinedenc;
            } else {
                ucsdet_setText(csd, s, inputLength, &status);
                ucm = ucsdet_detect(csd, &status);
                if (!ucm) {
                    mValues.setEntry(i, "???");
                    continue;
                }
                enc = ucsdet_getName(ucm, &status);
                ALOGV("@@@@ recognized charset: %s for %s confidence %d",
                        enc, mNames.getEntry(i), ucsdet_getConfidence(ucm, &status));
            }

            if (strcmp(enc,"UTF-8") != 0) {
                // only convert if the source encoding isn't already UTF-8
                ALOGV("@@@ using converter %s for %s", enc, mNames.getEntry(i));
                UConverter *conv = ucnv_open(enc, &status);
                if (U_FAILURE(status)) {
                    ALOGE("could not create UConverter for %s", enc);
                    continue;
                }

                // convert from native encoding to UTF-8
                const char* source = mValues.getEntry(i);
                int targetLength = len * 3 + 1;
                char* buffer = new char[targetLength];
                // don't normally check for NULL, but in this case targetLength may be large
                if (!buffer)
                    break;
                char* target = buffer;

                ucnv_convertEx(mUtf8Conv, conv, &target, target + targetLength,
                        &source, source + strlen(source),
                        NULL, NULL, NULL, NULL, TRUE, TRUE, &status);

                if (U_FAILURE(status)) {
                    ALOGE("ucnv_convertEx failed: %d", status);
                    mValues.setEntry(i, "???");
                } else {
                    // zero terminate
                    *target = 0;
                    mValues.setEntry(i, buffer);
                }

                delete[] buffer;

                ucnv_close(conv);
            }
        }

        for (int i = size - 1; i >= 0; --i) {
            if (strlen(mValues.getEntry(i)) == 0) {
                ALOGV("erasing %s because entry is empty", mNames.getEntry(i));
                mNames.erase(i);
                mValues.erase(i);
            }
        }

        ucsdet_close(csd);
    }
}

/*
 * When ICU detects multiple encoding matches, apply additional heuristics to determine
 * which one is the best match, since ICU can't always be trusted to make the right choice.
 *
 * What this method does is:
 * - decode the input using each of the matches found
 * - recalculate the starting confidence level for multibyte encodings using a different
 *   algorithm and larger frequent character lists than ICU
 * - devalue encoding where the conversion contains unlikely characters (symbols, reserved, etc)
 * - pick the highest match
 */
const UCharsetMatch *CharacterEncodingDetector::getPreferred(
        const char *input, size_t len, const UCharsetMatch** ucma, size_t nummatches) {

    Vector<const UCharsetMatch*> matches;
    UErrorCode status = U_ZERO_ERROR;

    ALOGV("%d matches", nummatches);
    for (size_t i = 0; i < nummatches; i++) {
        const char *encname = ucsdet_getName(ucma[i], &status);
        int confidence = ucsdet_getConfidence(ucma[i], &status);
        ALOGV("%d: %s %d", i, encname, confidence);
        matches.push_back(ucma[i]);
    }

    size_t num = matches.size();
    if (num == 0) {
        return NULL;
    }
    if (num == 1) {
        return matches[0];
    }

    ALOGV("considering %d matches", num);

    // keep track of how many "special" characters result when converting the input using each
    // encoding
    Vector<int> newconfidence;
    for (size_t i = 0; i < num; i++) {
        const uint16_t *freqdata = NULL;
        float freqcoverage = 0;
        status = U_ZERO_ERROR;
        const char *encname = ucsdet_getName(matches[i], &status);
        int confidence = ucsdet_getConfidence(matches[i], &status);
        if (!strcmp("GB18030", encname)) {
            freqdata = frequent_zhCN;
            freqcoverage = frequent_zhCN_coverage;
        } else if (!strcmp("Big5", encname)) {
            freqdata = frequent_zhTW;
            freqcoverage = frequent_zhTW_coverage;
        } else if (!strcmp("EUC-KR", encname)) {
            freqdata = frequent_ko;
            freqcoverage = frequent_ko_coverage;
        } else if (!strcmp("EUC-JP", encname)) {
            freqdata = frequent_ja;
            freqcoverage = frequent_ja_coverage;
        } else if (!strcmp("Shift_JIS", encname)) {
            freqdata = frequent_ja;
            freqcoverage = frequent_ja_coverage;
        }

        ALOGV("%d: %s %d", i, encname, confidence);
        UConverter *conv = ucnv_open(encname, &status);
        const char *source = input;
        const char *sourceLimit = input + len;
        status = U_ZERO_ERROR;
        int demerit = 0;
        int frequentchars = 0;
        int totalchars = 0;
        while (true) {
            // demerit the current encoding for each "special" character found after conversion.
            // The amount of demerit is somewhat arbitrarily chosen.
            int inchar;
            if (source != sourceLimit) {
                inchar = (source[0] << 8) + source[1];
            }
            UChar32 c = ucnv_getNextUChar(conv, &source, sourceLimit, &status);
            if (!U_SUCCESS(status)) {
                break;
            }
            if (c < 0x20 || (c >= 0x7f && c <= 0x009f)) {
                ALOGV("control character %x", c);
                demerit += 100;
            } else if ((c >= 0xa0 && c <= 0xbe)         // symbols, superscripts
                    || (c == 0xd7) || (c == 0xf7)       // multiplication and division signs
                    || (c >= 0x2000 && c <= 0x209f)) {  // punctuation, superscripts
                ALOGV("unlikely character %x", c);
                demerit += 10;
            } else if (c >= 0xe000 && c <= 0xf8ff) {
                ALOGV("private use character %x", c);
                demerit += 30;
            } else if (c >= 0x2190 && c <= 0x2bff) {
                // this range comprises various symbol ranges that are unlikely to appear in
                // music file metadata.
                ALOGV("symbol %x", c);
                demerit += 10;
            } else if (c == 0xfffd) {
                ALOGV("replacement character");
                demerit += 50;
            } else if (c >= 0xfff0 && c <= 0xfffc) {
                ALOGV("unicode special %x", c);
                demerit += 50;
            } else if (freqdata != NULL) {
                totalchars++;
                if (isFrequent(freqdata, c)) {
                    frequentchars++;
                }
            }
        }
        if (freqdata != NULL && totalchars != 0) {
            int myconfidence = 10 + float((100 * frequentchars) / totalchars) / freqcoverage;
            ALOGV("ICU confidence: %d, my confidence: %d (%d %d)", confidence, myconfidence,
                    totalchars, frequentchars);
            if (myconfidence > 100) myconfidence = 100;
            if (myconfidence < 0) myconfidence = 0;
            confidence = myconfidence;
        }
        ALOGV("%d-%d=%d", confidence, demerit, confidence - demerit);
        newconfidence.push_back(confidence - demerit);
        ucnv_close(conv);
        if (i == 0 && (confidence - demerit) == 100) {
            // no need to check any further, we'll end up using this match anyway
            break;
        }
    }

    // find match with highest confidence after adjusting for unlikely characters
    int highest = newconfidence[0];
    size_t highestidx = 0;
    num = newconfidence.size();
    for (size_t i = 1; i < num; i++) {
        if (newconfidence[i] > highest) {
            highest = newconfidence[i];
            highestidx = i;
        }
    }
    status = U_ZERO_ERROR;
    ALOGV("selecting '%s' w/ %d confidence", ucsdet_getName(matches[highestidx], &status), highest);
    return matches[highestidx];
}


bool CharacterEncodingDetector::isFrequent(const uint16_t *values, uint32_t c) {

    int start = 0;
    int end = 511; // All the tables have 512 entries
    int mid = (start+end)/2;

    while(start <= end) {
        if(c == values[mid]) {
            return true;
        } else if (c > values[mid]) {
            start = mid + 1;
        } else {
            end = mid - 1;
        }

        mid = (start + end) / 2;
    }

    return false;
}


}  // namespace android