1/*
2 * Copyright (C) 2010 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 "APacketSource"
19#include <utils/Log.h>
20
21#include "APacketSource.h"
22
23#include "ARawAudioAssembler.h"
24#include "ASessionDescription.h"
25
26#include "include/avc_utils.h"
27
28#include <ctype.h>
29
30#include <media/stagefright/foundation/ABitReader.h>
31#include <media/stagefright/foundation/ABuffer.h>
32#include <media/stagefright/foundation/ADebug.h>
33#include <media/stagefright/foundation/AMessage.h>
34#include <media/stagefright/foundation/AString.h>
35#include <media/stagefright/foundation/base64.h>
36#include <media/stagefright/foundation/hexdump.h>
37#include <media/stagefright/MediaDefs.h>
38#include <media/stagefright/MediaErrors.h>
39#include <media/stagefright/MetaData.h>
40#include <utils/Vector.h>
41
42namespace android {
43
44static bool GetAttribute(const char *s, const char *key, AString *value) {
45    value->clear();
46
47    size_t keyLen = strlen(key);
48
49    for (;;) {
50        while (isspace(*s)) {
51            ++s;
52        }
53
54        const char *colonPos = strchr(s, ';');
55
56        size_t len =
57            (colonPos == NULL) ? strlen(s) : colonPos - s;
58
59        if (len >= keyLen + 1 && s[keyLen] == '=' && !strncmp(s, key, keyLen)) {
60            value->setTo(&s[keyLen + 1], len - keyLen - 1);
61            return true;
62        }
63
64        if (colonPos == NULL) {
65            return false;
66        }
67
68        s = colonPos + 1;
69    }
70}
71
72static sp<ABuffer> decodeHex(const AString &s) {
73    if ((s.size() % 2) != 0) {
74        return NULL;
75    }
76
77    size_t outLen = s.size() / 2;
78    sp<ABuffer> buffer = new ABuffer(outLen);
79    uint8_t *out = buffer->data();
80
81    uint8_t accum = 0;
82    for (size_t i = 0; i < s.size(); ++i) {
83        char c = s.c_str()[i];
84        unsigned value;
85        if (c >= '0' && c <= '9') {
86            value = c - '0';
87        } else if (c >= 'a' && c <= 'f') {
88            value = c - 'a' + 10;
89        } else if (c >= 'A' && c <= 'F') {
90            value = c - 'A' + 10;
91        } else {
92            return NULL;
93        }
94
95        accum = (accum << 4) | value;
96
97        if (i & 1) {
98            *out++ = accum;
99
100            accum = 0;
101        }
102    }
103
104    return buffer;
105}
106
107static sp<ABuffer> MakeAVCCodecSpecificData(
108        const char *params, int32_t *width, int32_t *height) {
109    *width = 0;
110    *height = 0;
111
112    AString val;
113    sp<ABuffer> profileLevelID = NULL;
114    if (GetAttribute(params, "profile-level-id", &val)) {
115        profileLevelID = decodeHex(val);
116        CHECK_EQ(profileLevelID->size(), 3u);
117    }
118
119    Vector<sp<ABuffer> > paramSets;
120
121    size_t numSeqParameterSets = 0;
122    size_t totalSeqParameterSetSize = 0;
123    size_t numPicParameterSets = 0;
124    size_t totalPicParameterSetSize = 0;
125
126    if (!GetAttribute(params, "sprop-parameter-sets", &val)) {
127        return NULL;
128    }
129
130    size_t start = 0;
131    for (;;) {
132        ssize_t commaPos = val.find(",", start);
133        size_t end = (commaPos < 0) ? val.size() : commaPos;
134
135        AString nalString(val, start, end - start);
136        sp<ABuffer> nal = decodeBase64(nalString);
137        CHECK(nal != NULL);
138        CHECK_GT(nal->size(), 0u);
139        CHECK_LE(nal->size(), 65535u);
140
141        uint8_t nalType = nal->data()[0] & 0x1f;
142        if (numSeqParameterSets == 0) {
143            CHECK_EQ((unsigned)nalType, 7u);
144        } else if (numPicParameterSets > 0) {
145            CHECK_EQ((unsigned)nalType, 8u);
146        }
147        if (nalType == 7) {
148            ++numSeqParameterSets;
149            totalSeqParameterSetSize += nal->size();
150        } else  {
151            CHECK_EQ((unsigned)nalType, 8u);
152            ++numPicParameterSets;
153            totalPicParameterSetSize += nal->size();
154        }
155
156        paramSets.push(nal);
157
158        if (commaPos < 0) {
159            break;
160        }
161
162        start = commaPos + 1;
163    }
164
165    CHECK_LT(numSeqParameterSets, 32u);
166    CHECK_LE(numPicParameterSets, 255u);
167
168    size_t csdSize =
169        1 + 3 + 1 + 1
170        + 2 * numSeqParameterSets + totalSeqParameterSetSize
171        + 1 + 2 * numPicParameterSets + totalPicParameterSetSize;
172
173    sp<ABuffer> csd = new ABuffer(csdSize);
174    uint8_t *out = csd->data();
175
176    *out++ = 0x01;  // configurationVersion
177    if (profileLevelID != NULL) {
178        memcpy(out, profileLevelID->data(), 3);
179        out += 3;
180    } else {
181        *out++ = 0x42; // Baseline profile
182        *out++ = 0xE0; // Common subset for all profiles
183        *out++ = 0x0A; // Level 1
184    }
185
186    *out++ = (0x3f << 2) | 1;  // lengthSize == 2 bytes
187    *out++ = 0xe0 | numSeqParameterSets;
188
189    for (size_t i = 0; i < numSeqParameterSets; ++i) {
190        sp<ABuffer> nal = paramSets.editItemAt(i);
191
192        *out++ = nal->size() >> 8;
193        *out++ = nal->size() & 0xff;
194
195        memcpy(out, nal->data(), nal->size());
196
197        out += nal->size();
198
199        if (i == 0) {
200            FindAVCDimensions(nal, width, height);
201            ALOGI("dimensions %dx%d", *width, *height);
202        }
203    }
204
205    *out++ = numPicParameterSets;
206
207    for (size_t i = 0; i < numPicParameterSets; ++i) {
208        sp<ABuffer> nal = paramSets.editItemAt(i + numSeqParameterSets);
209
210        *out++ = nal->size() >> 8;
211        *out++ = nal->size() & 0xff;
212
213        memcpy(out, nal->data(), nal->size());
214
215        out += nal->size();
216    }
217
218    // hexdump(csd->data(), csd->size());
219
220    return csd;
221}
222
223sp<ABuffer> MakeAACCodecSpecificData(const char *params) {
224    AString val;
225    CHECK(GetAttribute(params, "config", &val));
226
227    sp<ABuffer> config = decodeHex(val);
228    CHECK(config != NULL);
229    CHECK_GE(config->size(), 4u);
230
231    const uint8_t *data = config->data();
232    uint32_t x = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
233    x = (x >> 1) & 0xffff;
234
235    static const uint8_t kStaticESDS[] = {
236        0x03, 22,
237        0x00, 0x00,     // ES_ID
238        0x00,           // streamDependenceFlag, URL_Flag, OCRstreamFlag
239
240        0x04, 17,
241        0x40,                       // Audio ISO/IEC 14496-3
242        0x00, 0x00, 0x00, 0x00,
243        0x00, 0x00, 0x00, 0x00,
244        0x00, 0x00, 0x00, 0x00,
245
246        0x05, 2,
247        // AudioSpecificInfo follows
248    };
249
250    sp<ABuffer> csd = new ABuffer(sizeof(kStaticESDS) + 2);
251    memcpy(csd->data(), kStaticESDS, sizeof(kStaticESDS));
252    csd->data()[sizeof(kStaticESDS)] = (x >> 8) & 0xff;
253    csd->data()[sizeof(kStaticESDS) + 1] = x & 0xff;
254
255    // hexdump(csd->data(), csd->size());
256
257    return csd;
258}
259
260// From mpeg4-generic configuration data.
261sp<ABuffer> MakeAACCodecSpecificData2(const char *params) {
262    AString val;
263    unsigned long objectType;
264    if (GetAttribute(params, "objectType", &val)) {
265        const char *s = val.c_str();
266        char *end;
267        objectType = strtoul(s, &end, 10);
268        CHECK(end > s && *end == '\0');
269    } else {
270        objectType = 0x40;  // Audio ISO/IEC 14496-3
271    }
272
273    CHECK(GetAttribute(params, "config", &val));
274
275    sp<ABuffer> config = decodeHex(val);
276    CHECK(config != NULL);
277
278    // Make sure size fits into a single byte and doesn't have to
279    // be encoded.
280    CHECK_LT(20 + config->size(), 128u);
281
282    static const uint8_t kStaticESDS[] = {
283        0x03, 22,
284        0x00, 0x00,     // ES_ID
285        0x00,           // streamDependenceFlag, URL_Flag, OCRstreamFlag
286
287        0x04, 17,
288        0x40,                       // Audio ISO/IEC 14496-3
289        0x00, 0x00, 0x00, 0x00,
290        0x00, 0x00, 0x00, 0x00,
291        0x00, 0x00, 0x00, 0x00,
292
293        0x05, 2,
294        // AudioSpecificInfo follows
295    };
296
297    sp<ABuffer> csd = new ABuffer(sizeof(kStaticESDS) + config->size());
298    uint8_t *dst = csd->data();
299    *dst++ = 0x03;
300    *dst++ = 20 + config->size();
301    *dst++ = 0x00;  // ES_ID
302    *dst++ = 0x00;
303    *dst++ = 0x00;  // streamDependenceFlag, URL_Flag, OCRstreamFlag
304    *dst++ = 0x04;
305    *dst++ = 15 + config->size();
306    *dst++ = objectType;
307    for (int i = 0; i < 12; ++i) { *dst++ = 0x00; }
308    *dst++ = 0x05;
309    *dst++ = config->size();
310    memcpy(dst, config->data(), config->size());
311
312    // hexdump(csd->data(), csd->size());
313
314    return csd;
315}
316
317static size_t GetSizeWidth(size_t x) {
318    size_t n = 1;
319    while (x > 127) {
320        ++n;
321        x >>= 7;
322    }
323    return n;
324}
325
326static uint8_t *EncodeSize(uint8_t *dst, size_t x) {
327    while (x > 127) {
328        *dst++ = (x & 0x7f) | 0x80;
329        x >>= 7;
330    }
331    *dst++ = x;
332    return dst;
333}
334
335static bool ExtractDimensionsMPEG4Config(
336        const sp<ABuffer> &config, int32_t *width, int32_t *height) {
337    *width = 0;
338    *height = 0;
339
340    const uint8_t *ptr = config->data();
341    size_t offset = 0;
342    bool foundVOL = false;
343    while (offset + 3 < config->size()) {
344        if (memcmp("\x00\x00\x01", &ptr[offset], 3)
345                || (ptr[offset + 3] & 0xf0) != 0x20) {
346            ++offset;
347            continue;
348        }
349
350        foundVOL = true;
351        break;
352    }
353
354    if (!foundVOL) {
355        return false;
356    }
357
358    return ExtractDimensionsFromVOLHeader(
359            &ptr[offset], config->size() - offset, width, height);
360}
361
362static sp<ABuffer> MakeMPEG4VideoCodecSpecificData(
363        const char *params, int32_t *width, int32_t *height) {
364    *width = 0;
365    *height = 0;
366
367    AString val;
368    CHECK(GetAttribute(params, "config", &val));
369
370    sp<ABuffer> config = decodeHex(val);
371    CHECK(config != NULL);
372
373    if (!ExtractDimensionsMPEG4Config(config, width, height)) {
374        return NULL;
375    }
376
377    ALOGI("VOL dimensions = %dx%d", *width, *height);
378
379    size_t len1 = config->size() + GetSizeWidth(config->size()) + 1;
380    size_t len2 = len1 + GetSizeWidth(len1) + 1 + 13;
381    size_t len3 = len2 + GetSizeWidth(len2) + 1 + 3;
382
383    sp<ABuffer> csd = new ABuffer(len3);
384    uint8_t *dst = csd->data();
385    *dst++ = 0x03;
386    dst = EncodeSize(dst, len2 + 3);
387    *dst++ = 0x00;  // ES_ID
388    *dst++ = 0x00;
389    *dst++ = 0x00;  // streamDependenceFlag, URL_Flag, OCRstreamFlag
390
391    *dst++ = 0x04;
392    dst = EncodeSize(dst, len1 + 13);
393    *dst++ = 0x01;  // Video ISO/IEC 14496-2 Simple Profile
394    for (size_t i = 0; i < 12; ++i) {
395        *dst++ = 0x00;
396    }
397
398    *dst++ = 0x05;
399    dst = EncodeSize(dst, config->size());
400    memcpy(dst, config->data(), config->size());
401    dst += config->size();
402
403    // hexdump(csd->data(), csd->size());
404
405    return csd;
406}
407
408APacketSource::APacketSource(
409        const sp<ASessionDescription> &sessionDesc, size_t index)
410    : mInitCheck(NO_INIT),
411      mFormat(new MetaData) {
412    unsigned long PT;
413    AString desc;
414    AString params;
415    sessionDesc->getFormatType(index, &PT, &desc, &params);
416
417    int64_t durationUs;
418    if (sessionDesc->getDurationUs(&durationUs)) {
419        mFormat->setInt64(kKeyDuration, durationUs);
420    } else {
421        mFormat->setInt64(kKeyDuration, -1ll);
422    }
423
424    mInitCheck = OK;
425    if (!strncmp(desc.c_str(), "H264/", 5)) {
426        mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
427
428        int32_t width, height;
429        if (!sessionDesc->getDimensions(index, PT, &width, &height)) {
430            width = -1;
431            height = -1;
432        }
433
434        int32_t encWidth, encHeight;
435        sp<ABuffer> codecSpecificData =
436            MakeAVCCodecSpecificData(params.c_str(), &encWidth, &encHeight);
437
438        if (codecSpecificData != NULL) {
439            if (width < 0) {
440                // If no explicit width/height given in the sdp, use the dimensions
441                // extracted from the first sequence parameter set.
442                width = encWidth;
443                height = encHeight;
444            }
445
446            mFormat->setData(
447                    kKeyAVCC, 0,
448                    codecSpecificData->data(), codecSpecificData->size());
449        } else if (width < 0) {
450            mInitCheck = ERROR_UNSUPPORTED;
451            return;
452        }
453
454        mFormat->setInt32(kKeyWidth, width);
455        mFormat->setInt32(kKeyHeight, height);
456    } else if (!strncmp(desc.c_str(), "H263-2000/", 10)
457            || !strncmp(desc.c_str(), "H263-1998/", 10)) {
458        mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
459
460        int32_t width, height;
461        if (!sessionDesc->getDimensions(index, PT, &width, &height)) {
462            mInitCheck = ERROR_UNSUPPORTED;
463            return;
464        }
465
466        mFormat->setInt32(kKeyWidth, width);
467        mFormat->setInt32(kKeyHeight, height);
468    } else if (!strncmp(desc.c_str(), "MP4A-LATM/", 10)) {
469        mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
470
471        int32_t sampleRate, numChannels;
472        ASessionDescription::ParseFormatDesc(
473                desc.c_str(), &sampleRate, &numChannels);
474
475        mFormat->setInt32(kKeySampleRate, sampleRate);
476        mFormat->setInt32(kKeyChannelCount, numChannels);
477
478        sp<ABuffer> codecSpecificData =
479            MakeAACCodecSpecificData(params.c_str());
480
481        mFormat->setData(
482                kKeyESDS, 0,
483                codecSpecificData->data(), codecSpecificData->size());
484    } else if (!strncmp(desc.c_str(), "AMR/", 4)) {
485        mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
486
487        int32_t sampleRate, numChannels;
488        ASessionDescription::ParseFormatDesc(
489                desc.c_str(), &sampleRate, &numChannels);
490
491        mFormat->setInt32(kKeySampleRate, sampleRate);
492        mFormat->setInt32(kKeyChannelCount, numChannels);
493
494        if (sampleRate != 8000 || numChannels != 1) {
495            mInitCheck = ERROR_UNSUPPORTED;
496        }
497    } else if (!strncmp(desc.c_str(), "AMR-WB/", 7)) {
498        mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
499
500        int32_t sampleRate, numChannels;
501        ASessionDescription::ParseFormatDesc(
502                desc.c_str(), &sampleRate, &numChannels);
503
504        mFormat->setInt32(kKeySampleRate, sampleRate);
505        mFormat->setInt32(kKeyChannelCount, numChannels);
506
507        if (sampleRate != 16000 || numChannels != 1) {
508            mInitCheck = ERROR_UNSUPPORTED;
509        }
510    } else if (!strncmp(desc.c_str(), "MP4V-ES/", 8)) {
511        mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
512
513        int32_t width, height;
514        if (!sessionDesc->getDimensions(index, PT, &width, &height)) {
515            width = -1;
516            height = -1;
517        }
518
519        int32_t encWidth, encHeight;
520        sp<ABuffer> codecSpecificData =
521            MakeMPEG4VideoCodecSpecificData(
522                    params.c_str(), &encWidth, &encHeight);
523
524        if (codecSpecificData != NULL) {
525            mFormat->setData(
526                    kKeyESDS, 0,
527                    codecSpecificData->data(), codecSpecificData->size());
528
529            if (width < 0) {
530                width = encWidth;
531                height = encHeight;
532            }
533        } else if (width < 0) {
534            mInitCheck = ERROR_UNSUPPORTED;
535            return;
536        }
537
538        mFormat->setInt32(kKeyWidth, width);
539        mFormat->setInt32(kKeyHeight, height);
540    } else if (!strncasecmp(desc.c_str(), "mpeg4-generic/", 14)) {
541        AString val;
542        if (!GetAttribute(params.c_str(), "mode", &val)
543                || (strcasecmp(val.c_str(), "AAC-lbr")
544                    && strcasecmp(val.c_str(), "AAC-hbr"))) {
545            mInitCheck = ERROR_UNSUPPORTED;
546            return;
547        }
548
549        mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
550
551        int32_t sampleRate, numChannels;
552        ASessionDescription::ParseFormatDesc(
553                desc.c_str(), &sampleRate, &numChannels);
554
555        mFormat->setInt32(kKeySampleRate, sampleRate);
556        mFormat->setInt32(kKeyChannelCount, numChannels);
557        mFormat->setInt32(kKeyIsADTS, true);
558
559        sp<ABuffer> codecSpecificData =
560            MakeAACCodecSpecificData2(params.c_str());
561
562        mFormat->setData(
563                kKeyESDS, 0,
564                codecSpecificData->data(), codecSpecificData->size());
565    } else if (ARawAudioAssembler::Supports(desc.c_str())) {
566        ARawAudioAssembler::MakeFormat(desc.c_str(), mFormat);
567    } else if (!strncasecmp("MP2T/", desc.c_str(), 5)) {
568        mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_CONTAINER_MPEG2TS);
569    } else {
570        mInitCheck = ERROR_UNSUPPORTED;
571    }
572}
573
574APacketSource::~APacketSource() {
575}
576
577status_t APacketSource::initCheck() const {
578    return mInitCheck;
579}
580
581sp<MetaData> APacketSource::getFormat() {
582    return mFormat;
583}
584
585}  // namespace android
586