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 "ESQueue"
19#include <media/stagefright/foundation/ADebug.h>
20
21#include "ESQueue.h"
22
23#include <media/stagefright/foundation/hexdump.h>
24#include <media/stagefright/foundation/ABitReader.h>
25#include <media/stagefright/foundation/ABuffer.h>
26#include <media/stagefright/foundation/AMessage.h>
27#include <media/stagefright/MediaErrors.h>
28#include <media/stagefright/MediaDefs.h>
29#include <media/stagefright/MetaData.h>
30#include <media/stagefright/Utils.h>
31
32#include "include/avc_utils.h"
33
34#include <inttypes.h>
35#include <netinet/in.h>
36
37namespace android {
38
39ElementaryStreamQueue::ElementaryStreamQueue(Mode mode, uint32_t flags)
40    : mMode(mode),
41      mFlags(flags),
42      mEOSReached(false) {
43}
44
45sp<MetaData> ElementaryStreamQueue::getFormat() {
46    return mFormat;
47}
48
49void ElementaryStreamQueue::clear(bool clearFormat) {
50    if (mBuffer != NULL) {
51        mBuffer->setRange(0, 0);
52    }
53
54    mRangeInfos.clear();
55
56    if (clearFormat) {
57        mFormat.clear();
58    }
59
60    mEOSReached = false;
61}
62
63// Parse AC3 header assuming the current ptr is start position of syncframe,
64// update metadata only applicable, and return the payload size
65static unsigned parseAC3SyncFrame(
66        const uint8_t *ptr, size_t size, sp<MetaData> *metaData) {
67    static const unsigned channelCountTable[] = {2, 1, 2, 3, 3, 4, 4, 5};
68    static const unsigned samplingRateTable[] = {48000, 44100, 32000};
69
70    static const unsigned frameSizeTable[19][3] = {
71        { 64, 69, 96 },
72        { 80, 87, 120 },
73        { 96, 104, 144 },
74        { 112, 121, 168 },
75        { 128, 139, 192 },
76        { 160, 174, 240 },
77        { 192, 208, 288 },
78        { 224, 243, 336 },
79        { 256, 278, 384 },
80        { 320, 348, 480 },
81        { 384, 417, 576 },
82        { 448, 487, 672 },
83        { 512, 557, 768 },
84        { 640, 696, 960 },
85        { 768, 835, 1152 },
86        { 896, 975, 1344 },
87        { 1024, 1114, 1536 },
88        { 1152, 1253, 1728 },
89        { 1280, 1393, 1920 },
90    };
91
92    ABitReader bits(ptr, size);
93    if (bits.numBitsLeft() < 16) {
94        return 0;
95    }
96    if (bits.getBits(16) != 0x0B77) {
97        return 0;
98    }
99
100    if (bits.numBitsLeft() < 16 + 2 + 6 + 5 + 3 + 3) {
101        ALOGV("Not enough bits left for further parsing");
102        return 0;
103    }
104    bits.skipBits(16);  // crc1
105
106    unsigned fscod = bits.getBits(2);
107    if (fscod == 3) {
108        ALOGW("Incorrect fscod in AC3 header");
109        return 0;
110    }
111
112    unsigned frmsizecod = bits.getBits(6);
113    if (frmsizecod > 37) {
114        ALOGW("Incorrect frmsizecod in AC3 header");
115        return 0;
116    }
117
118    unsigned bsid = bits.getBits(5);
119    if (bsid > 8) {
120        ALOGW("Incorrect bsid in AC3 header. Possibly E-AC-3?");
121        return 0;
122    }
123
124    unsigned bsmod __unused = bits.getBits(3);
125    unsigned acmod = bits.getBits(3);
126    unsigned cmixlev __unused = 0;
127    unsigned surmixlev __unused = 0;
128    unsigned dsurmod __unused = 0;
129
130    if ((acmod & 1) > 0 && acmod != 1) {
131        if (bits.numBitsLeft() < 2) {
132            return 0;
133        }
134        cmixlev = bits.getBits(2);
135    }
136    if ((acmod & 4) > 0) {
137        if (bits.numBitsLeft() < 2) {
138            return 0;
139        }
140        surmixlev = bits.getBits(2);
141    }
142    if (acmod == 2) {
143        if (bits.numBitsLeft() < 2) {
144            return 0;
145        }
146        dsurmod = bits.getBits(2);
147    }
148
149    if (bits.numBitsLeft() < 1) {
150        return 0;
151    }
152    unsigned lfeon = bits.getBits(1);
153
154    unsigned samplingRate = samplingRateTable[fscod];
155    unsigned payloadSize = frameSizeTable[frmsizecod >> 1][fscod];
156    if (fscod == 1) {
157        payloadSize += frmsizecod & 1;
158    }
159    payloadSize <<= 1;  // convert from 16-bit words to bytes
160
161    unsigned channelCount = channelCountTable[acmod] + lfeon;
162
163    if (metaData != NULL) {
164        (*metaData)->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AC3);
165        (*metaData)->setInt32(kKeyChannelCount, channelCount);
166        (*metaData)->setInt32(kKeySampleRate, samplingRate);
167    }
168
169    return payloadSize;
170}
171
172static bool IsSeeminglyValidAC3Header(const uint8_t *ptr, size_t size) {
173    return parseAC3SyncFrame(ptr, size, NULL) > 0;
174}
175
176static bool IsSeeminglyValidADTSHeader(
177        const uint8_t *ptr, size_t size, size_t *frameLength) {
178    if (size < 7) {
179        // Not enough data to verify header.
180        return false;
181    }
182
183    if (ptr[0] != 0xff || (ptr[1] >> 4) != 0x0f) {
184        return false;
185    }
186
187    unsigned layer = (ptr[1] >> 1) & 3;
188
189    if (layer != 0) {
190        return false;
191    }
192
193    unsigned ID = (ptr[1] >> 3) & 1;
194    unsigned profile_ObjectType = ptr[2] >> 6;
195
196    if (ID == 1 && profile_ObjectType == 3) {
197        // MPEG-2 profile 3 is reserved.
198        return false;
199    }
200
201    size_t frameLengthInHeader =
202            ((ptr[3] & 3) << 11) + (ptr[4] << 3) + ((ptr[5] >> 5) & 7);
203    if (frameLengthInHeader > size) {
204        return false;
205    }
206
207    *frameLength = frameLengthInHeader;
208    return true;
209}
210
211static bool IsSeeminglyValidMPEGAudioHeader(const uint8_t *ptr, size_t size) {
212    if (size < 3) {
213        // Not enough data to verify header.
214        return false;
215    }
216
217    if (ptr[0] != 0xff || (ptr[1] >> 5) != 0x07) {
218        return false;
219    }
220
221    unsigned ID = (ptr[1] >> 3) & 3;
222
223    if (ID == 1) {
224        return false;  // reserved
225    }
226
227    unsigned layer = (ptr[1] >> 1) & 3;
228
229    if (layer == 0) {
230        return false;  // reserved
231    }
232
233    unsigned bitrateIndex = (ptr[2] >> 4);
234
235    if (bitrateIndex == 0x0f) {
236        return false;  // reserved
237    }
238
239    unsigned samplingRateIndex = (ptr[2] >> 2) & 3;
240
241    if (samplingRateIndex == 3) {
242        return false;  // reserved
243    }
244
245    return true;
246}
247
248status_t ElementaryStreamQueue::appendData(
249        const void *data, size_t size, int64_t timeUs) {
250
251    if (mEOSReached) {
252        ALOGE("appending data after EOS");
253        return ERROR_MALFORMED;
254    }
255    if (mBuffer == NULL || mBuffer->size() == 0) {
256        switch (mMode) {
257            case H264:
258            case MPEG_VIDEO:
259            {
260#if 0
261                if (size < 4 || memcmp("\x00\x00\x00\x01", data, 4)) {
262                    return ERROR_MALFORMED;
263                }
264#else
265                uint8_t *ptr = (uint8_t *)data;
266
267                ssize_t startOffset = -1;
268                for (size_t i = 0; i + 2 < size; ++i) {
269                    if (!memcmp("\x00\x00\x01", &ptr[i], 3)) {
270                        startOffset = i;
271                        break;
272                    }
273                }
274
275                if (startOffset < 0) {
276                    return ERROR_MALFORMED;
277                }
278
279                if (startOffset > 0) {
280                    ALOGI("found something resembling an H.264/MPEG syncword "
281                          "at offset %zd",
282                          startOffset);
283                }
284
285                data = &ptr[startOffset];
286                size -= startOffset;
287#endif
288                break;
289            }
290
291            case MPEG4_VIDEO:
292            {
293#if 0
294                if (size < 3 || memcmp("\x00\x00\x01", data, 3)) {
295                    return ERROR_MALFORMED;
296                }
297#else
298                uint8_t *ptr = (uint8_t *)data;
299
300                ssize_t startOffset = -1;
301                for (size_t i = 0; i + 2 < size; ++i) {
302                    if (!memcmp("\x00\x00\x01", &ptr[i], 3)) {
303                        startOffset = i;
304                        break;
305                    }
306                }
307
308                if (startOffset < 0) {
309                    return ERROR_MALFORMED;
310                }
311
312                if (startOffset > 0) {
313                    ALOGI("found something resembling an H.264/MPEG syncword "
314                          "at offset %zd",
315                          startOffset);
316                }
317
318                data = &ptr[startOffset];
319                size -= startOffset;
320#endif
321                break;
322            }
323
324            case AAC:
325            {
326                uint8_t *ptr = (uint8_t *)data;
327
328#if 0
329                if (size < 2 || ptr[0] != 0xff || (ptr[1] >> 4) != 0x0f) {
330                    return ERROR_MALFORMED;
331                }
332#else
333                ssize_t startOffset = -1;
334                size_t frameLength;
335                for (size_t i = 0; i < size; ++i) {
336                    if (IsSeeminglyValidADTSHeader(
337                            &ptr[i], size - i, &frameLength)) {
338                        startOffset = i;
339                        break;
340                    }
341                }
342
343                if (startOffset < 0) {
344                    return ERROR_MALFORMED;
345                }
346
347                if (startOffset > 0) {
348                    ALOGI("found something resembling an AAC syncword at "
349                          "offset %zd",
350                          startOffset);
351                }
352
353                if (frameLength != size - startOffset) {
354                    ALOGV("First ADTS AAC frame length is %zd bytes, "
355                          "while the buffer size is %zd bytes.",
356                          frameLength, size - startOffset);
357                }
358
359                data = &ptr[startOffset];
360                size -= startOffset;
361#endif
362                break;
363            }
364
365            case AC3:
366            {
367                uint8_t *ptr = (uint8_t *)data;
368
369                ssize_t startOffset = -1;
370                for (size_t i = 0; i < size; ++i) {
371                    if (IsSeeminglyValidAC3Header(&ptr[i], size - i)) {
372                        startOffset = i;
373                        break;
374                    }
375                }
376
377                if (startOffset < 0) {
378                    return ERROR_MALFORMED;
379                }
380
381                if (startOffset > 0) {
382                    ALOGI("found something resembling an AC3 syncword at "
383                          "offset %zd",
384                          startOffset);
385                }
386
387                data = &ptr[startOffset];
388                size -= startOffset;
389                break;
390            }
391
392            case MPEG_AUDIO:
393            {
394                uint8_t *ptr = (uint8_t *)data;
395
396                ssize_t startOffset = -1;
397                for (size_t i = 0; i < size; ++i) {
398                    if (IsSeeminglyValidMPEGAudioHeader(&ptr[i], size - i)) {
399                        startOffset = i;
400                        break;
401                    }
402                }
403
404                if (startOffset < 0) {
405                    return ERROR_MALFORMED;
406                }
407
408                if (startOffset > 0) {
409                    ALOGI("found something resembling an MPEG audio "
410                          "syncword at offset %zd",
411                          startOffset);
412                }
413
414                data = &ptr[startOffset];
415                size -= startOffset;
416                break;
417            }
418
419            case PCM_AUDIO:
420            case METADATA:
421            {
422                break;
423            }
424
425            default:
426                ALOGE("Unknown mode: %d", mMode);
427                return ERROR_MALFORMED;
428        }
429    }
430
431    size_t neededSize = (mBuffer == NULL ? 0 : mBuffer->size()) + size;
432    if (mBuffer == NULL || neededSize > mBuffer->capacity()) {
433        neededSize = (neededSize + 65535) & ~65535;
434
435        ALOGV("resizing buffer to size %zu", neededSize);
436
437        sp<ABuffer> buffer = new ABuffer(neededSize);
438        if (mBuffer != NULL) {
439            memcpy(buffer->data(), mBuffer->data(), mBuffer->size());
440            buffer->setRange(0, mBuffer->size());
441        } else {
442            buffer->setRange(0, 0);
443        }
444
445        mBuffer = buffer;
446    }
447
448    memcpy(mBuffer->data() + mBuffer->size(), data, size);
449    mBuffer->setRange(0, mBuffer->size() + size);
450
451    RangeInfo info;
452    info.mLength = size;
453    info.mTimestampUs = timeUs;
454    mRangeInfos.push_back(info);
455
456#if 0
457    if (mMode == AAC) {
458        ALOGI("size = %zu, timeUs = %.2f secs", size, timeUs / 1E6);
459        hexdump(data, size);
460    }
461#endif
462
463    return OK;
464}
465
466sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnit() {
467    if ((mFlags & kFlag_AlignedData) && mMode == H264) {
468        if (mRangeInfos.empty()) {
469            return NULL;
470        }
471
472        RangeInfo info = *mRangeInfos.begin();
473        mRangeInfos.erase(mRangeInfos.begin());
474
475        sp<ABuffer> accessUnit = new ABuffer(info.mLength);
476        memcpy(accessUnit->data(), mBuffer->data(), info.mLength);
477        accessUnit->meta()->setInt64("timeUs", info.mTimestampUs);
478
479        memmove(mBuffer->data(),
480                mBuffer->data() + info.mLength,
481                mBuffer->size() - info.mLength);
482
483        mBuffer->setRange(0, mBuffer->size() - info.mLength);
484
485        if (mFormat == NULL) {
486            mFormat = MakeAVCCodecSpecificData(accessUnit);
487        }
488
489        return accessUnit;
490    }
491
492    switch (mMode) {
493        case H264:
494            return dequeueAccessUnitH264();
495        case AAC:
496            return dequeueAccessUnitAAC();
497        case AC3:
498            return dequeueAccessUnitAC3();
499        case MPEG_VIDEO:
500            return dequeueAccessUnitMPEGVideo();
501        case MPEG4_VIDEO:
502            return dequeueAccessUnitMPEG4Video();
503        case PCM_AUDIO:
504            return dequeueAccessUnitPCMAudio();
505        case METADATA:
506            return dequeueAccessUnitMetadata();
507        default:
508            if (mMode != MPEG_AUDIO) {
509                ALOGE("Unknown mode");
510                return NULL;
511            }
512            return dequeueAccessUnitMPEGAudio();
513    }
514}
515
516sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitAC3() {
517    unsigned syncStartPos = 0;  // in bytes
518    unsigned payloadSize = 0;
519    sp<MetaData> format = new MetaData;
520    while (true) {
521        if (syncStartPos + 2 >= mBuffer->size()) {
522            return NULL;
523        }
524
525        payloadSize = parseAC3SyncFrame(
526                mBuffer->data() + syncStartPos,
527                mBuffer->size() - syncStartPos,
528                &format);
529        if (payloadSize > 0) {
530            break;
531        }
532        ++syncStartPos;
533    }
534
535    if (mBuffer->size() < syncStartPos + payloadSize) {
536        ALOGV("Not enough buffer size for AC3");
537        return NULL;
538    }
539
540    if (mFormat == NULL) {
541        mFormat = format;
542    }
543
544    sp<ABuffer> accessUnit = new ABuffer(syncStartPos + payloadSize);
545    memcpy(accessUnit->data(), mBuffer->data(), syncStartPos + payloadSize);
546
547    int64_t timeUs = fetchTimestamp(syncStartPos + payloadSize);
548    if (timeUs < 0ll) {
549        ALOGE("negative timeUs");
550        return NULL;
551    }
552    accessUnit->meta()->setInt64("timeUs", timeUs);
553    accessUnit->meta()->setInt32("isSync", 1);
554
555    memmove(
556            mBuffer->data(),
557            mBuffer->data() + syncStartPos + payloadSize,
558            mBuffer->size() - syncStartPos - payloadSize);
559
560    mBuffer->setRange(0, mBuffer->size() - syncStartPos - payloadSize);
561
562    return accessUnit;
563}
564
565sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitPCMAudio() {
566    if (mBuffer->size() < 4) {
567        return NULL;
568    }
569
570    ABitReader bits(mBuffer->data(), 4);
571    if (bits.getBits(8) != 0xa0) {
572        ALOGE("Unexpected bit values");
573        return NULL;
574    }
575    unsigned numAUs = bits.getBits(8);
576    bits.skipBits(8);
577    unsigned quantization_word_length __unused = bits.getBits(2);
578    unsigned audio_sampling_frequency = bits.getBits(3);
579    unsigned num_channels = bits.getBits(3);
580
581    if (audio_sampling_frequency != 2) {
582        ALOGE("Wrong sampling freq");
583        return NULL;
584    }
585    if (num_channels != 1u) {
586        ALOGE("Wrong channel #");
587        return NULL;
588    }
589
590    if (mFormat == NULL) {
591        mFormat = new MetaData;
592        mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
593        mFormat->setInt32(kKeyChannelCount, 2);
594        mFormat->setInt32(kKeySampleRate, 48000);
595        mFormat->setInt32(kKeyPcmEncoding, kAudioEncodingPcm16bit);
596    }
597
598    static const size_t kFramesPerAU = 80;
599    size_t frameSize = 2 /* numChannels */ * sizeof(int16_t);
600
601    size_t payloadSize = numAUs * frameSize * kFramesPerAU;
602
603    if (mBuffer->size() < 4 + payloadSize) {
604        return NULL;
605    }
606
607    sp<ABuffer> accessUnit = new ABuffer(payloadSize);
608    memcpy(accessUnit->data(), mBuffer->data() + 4, payloadSize);
609
610    int64_t timeUs = fetchTimestamp(payloadSize + 4);
611    if (timeUs < 0ll) {
612        ALOGE("Negative timeUs");
613        return NULL;
614    }
615    accessUnit->meta()->setInt64("timeUs", timeUs);
616    accessUnit->meta()->setInt32("isSync", 1);
617
618    int16_t *ptr = (int16_t *)accessUnit->data();
619    for (size_t i = 0; i < payloadSize / sizeof(int16_t); ++i) {
620        ptr[i] = ntohs(ptr[i]);
621    }
622
623    memmove(
624            mBuffer->data(),
625            mBuffer->data() + 4 + payloadSize,
626            mBuffer->size() - 4 - payloadSize);
627
628    mBuffer->setRange(0, mBuffer->size() - 4 - payloadSize);
629
630    return accessUnit;
631}
632
633sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitAAC() {
634    if (mBuffer->size() == 0) {
635        return NULL;
636    }
637
638    if (mRangeInfos.empty()) {
639        return NULL;
640    }
641
642    const RangeInfo &info = *mRangeInfos.begin();
643    if (mBuffer->size() < info.mLength) {
644        return NULL;
645    }
646
647    if (info.mTimestampUs < 0ll) {
648        ALOGE("Negative info.mTimestampUs");
649        return NULL;
650    }
651
652    // The idea here is consume all AAC frames starting at offsets before
653    // info.mLength so we can assign a meaningful timestamp without
654    // having to interpolate.
655    // The final AAC frame may well extend into the next RangeInfo but
656    // that's ok.
657    size_t offset = 0;
658    while (offset < info.mLength) {
659        if (offset + 7 > mBuffer->size()) {
660            return NULL;
661        }
662
663        ABitReader bits(mBuffer->data() + offset, mBuffer->size() - offset);
664
665        // adts_fixed_header
666
667        if (bits.getBits(12) != 0xfffu) {
668            ALOGE("Wrong atds_fixed_header");
669            return NULL;
670        }
671        bits.skipBits(3);  // ID, layer
672        bool protection_absent __unused = bits.getBits(1) != 0;
673
674        if (mFormat == NULL) {
675            unsigned profile = bits.getBits(2);
676            if (profile == 3u) {
677                ALOGE("profile should not be 3");
678                return NULL;
679            }
680            unsigned sampling_freq_index = bits.getBits(4);
681            bits.getBits(1);  // private_bit
682            unsigned channel_configuration = bits.getBits(3);
683            if (channel_configuration == 0u) {
684                ALOGE("channel_config should not be 0");
685                return NULL;
686            }
687            bits.skipBits(2);  // original_copy, home
688
689            mFormat = MakeAACCodecSpecificData(
690                    profile, sampling_freq_index, channel_configuration);
691
692            mFormat->setInt32(kKeyIsADTS, true);
693
694            int32_t sampleRate;
695            int32_t numChannels;
696            if (!mFormat->findInt32(kKeySampleRate, &sampleRate)) {
697                ALOGE("SampleRate not found");
698                return NULL;
699            }
700            if (!mFormat->findInt32(kKeyChannelCount, &numChannels)) {
701                ALOGE("ChannelCount not found");
702                return NULL;
703            }
704
705            ALOGI("found AAC codec config (%d Hz, %d channels)",
706                 sampleRate, numChannels);
707        } else {
708            // profile_ObjectType, sampling_frequency_index, private_bits,
709            // channel_configuration, original_copy, home
710            bits.skipBits(12);
711        }
712
713        // adts_variable_header
714
715        // copyright_identification_bit, copyright_identification_start
716        bits.skipBits(2);
717
718        unsigned aac_frame_length = bits.getBits(13);
719        if (aac_frame_length == 0){
720            ALOGE("b/62673179, Invalid AAC frame length!");
721            android_errorWriteLog(0x534e4554, "62673179");
722            return NULL;
723        }
724
725        bits.skipBits(11);  // adts_buffer_fullness
726
727        unsigned number_of_raw_data_blocks_in_frame = bits.getBits(2);
728
729        if (number_of_raw_data_blocks_in_frame != 0) {
730            // To be implemented.
731            ALOGE("Should not reach here.");
732            return NULL;
733        }
734
735        if (offset + aac_frame_length > mBuffer->size()) {
736            return NULL;
737        }
738
739        size_t headerSize __unused = protection_absent ? 7 : 9;
740
741        offset += aac_frame_length;
742    }
743
744    int64_t timeUs = fetchTimestamp(offset);
745
746    sp<ABuffer> accessUnit = new ABuffer(offset);
747    memcpy(accessUnit->data(), mBuffer->data(), offset);
748
749    memmove(mBuffer->data(), mBuffer->data() + offset,
750            mBuffer->size() - offset);
751    mBuffer->setRange(0, mBuffer->size() - offset);
752
753    accessUnit->meta()->setInt64("timeUs", timeUs);
754    accessUnit->meta()->setInt32("isSync", 1);
755
756    return accessUnit;
757}
758
759int64_t ElementaryStreamQueue::fetchTimestamp(size_t size) {
760    int64_t timeUs = -1;
761    bool first = true;
762
763    while (size > 0) {
764        if (mRangeInfos.empty()) {
765            return timeUs;
766        }
767
768        RangeInfo *info = &*mRangeInfos.begin();
769
770        if (first) {
771            timeUs = info->mTimestampUs;
772            first = false;
773        }
774
775        if (info->mLength > size) {
776            info->mLength -= size;
777            size = 0;
778        } else {
779            size -= info->mLength;
780
781            mRangeInfos.erase(mRangeInfos.begin());
782            info = NULL;
783        }
784
785    }
786
787    if (timeUs == 0ll) {
788        ALOGV("Returning 0 timestamp");
789    }
790
791    return timeUs;
792}
793
794sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitH264() {
795    const uint8_t *data = mBuffer->data();
796
797    size_t size = mBuffer->size();
798    Vector<NALPosition> nals;
799
800    size_t totalSize = 0;
801    size_t seiCount = 0;
802
803    status_t err;
804    const uint8_t *nalStart;
805    size_t nalSize;
806    bool foundSlice = false;
807    bool foundIDR = false;
808    while ((err = getNextNALUnit(&data, &size, &nalStart, &nalSize)) == OK) {
809        if (nalSize == 0) continue;
810
811        unsigned nalType = nalStart[0] & 0x1f;
812        bool flush = false;
813
814        if (nalType == 1 || nalType == 5) {
815            if (nalType == 5) {
816                foundIDR = true;
817            }
818            if (foundSlice) {
819                ABitReader br(nalStart + 1, nalSize);
820                unsigned first_mb_in_slice = parseUE(&br);
821
822                if (first_mb_in_slice == 0) {
823                    // This slice starts a new frame.
824
825                    flush = true;
826                }
827            }
828
829            foundSlice = true;
830        } else if ((nalType == 9 || nalType == 7) && foundSlice) {
831            // Access unit delimiter and SPS will be associated with the
832            // next frame.
833
834            flush = true;
835        } else if (nalType == 6 && nalSize > 0) {
836            // found non-zero sized SEI
837            ++seiCount;
838        }
839
840        if (flush) {
841            // The access unit will contain all nal units up to, but excluding
842            // the current one, separated by 0x00 0x00 0x00 0x01 startcodes.
843
844            size_t auSize = 4 * nals.size() + totalSize;
845            sp<ABuffer> accessUnit = new ABuffer(auSize);
846            sp<ABuffer> sei;
847
848            if (seiCount > 0) {
849                sei = new ABuffer(seiCount * sizeof(NALPosition));
850                accessUnit->meta()->setBuffer("sei", sei);
851            }
852
853#if !LOG_NDEBUG
854            AString out;
855#endif
856
857            size_t dstOffset = 0;
858            size_t seiIndex = 0;
859            for (size_t i = 0; i < nals.size(); ++i) {
860                const NALPosition &pos = nals.itemAt(i);
861
862                unsigned nalType = mBuffer->data()[pos.nalOffset] & 0x1f;
863
864                if (nalType == 6 && pos.nalSize > 0) {
865                    if (seiIndex >= sei->size() / sizeof(NALPosition)) {
866                        ALOGE("Wrong seiIndex");
867                        return NULL;
868                    }
869                    NALPosition &seiPos = ((NALPosition *)sei->data())[seiIndex++];
870                    seiPos.nalOffset = dstOffset + 4;
871                    seiPos.nalSize = pos.nalSize;
872                }
873
874#if !LOG_NDEBUG
875                char tmp[128];
876                sprintf(tmp, "0x%02x", nalType);
877                if (i > 0) {
878                    out.append(", ");
879                }
880                out.append(tmp);
881#endif
882
883                memcpy(accessUnit->data() + dstOffset, "\x00\x00\x00\x01", 4);
884
885                memcpy(accessUnit->data() + dstOffset + 4,
886                       mBuffer->data() + pos.nalOffset,
887                       pos.nalSize);
888
889                dstOffset += pos.nalSize + 4;
890            }
891
892#if !LOG_NDEBUG
893            ALOGV("accessUnit contains nal types %s", out.c_str());
894#endif
895
896            const NALPosition &pos = nals.itemAt(nals.size() - 1);
897            size_t nextScan = pos.nalOffset + pos.nalSize;
898
899            memmove(mBuffer->data(),
900                    mBuffer->data() + nextScan,
901                    mBuffer->size() - nextScan);
902
903            mBuffer->setRange(0, mBuffer->size() - nextScan);
904
905            int64_t timeUs = fetchTimestamp(nextScan);
906            if (timeUs < 0ll) {
907                ALOGE("Negative timeUs");
908                return NULL;
909            }
910
911            accessUnit->meta()->setInt64("timeUs", timeUs);
912            if (foundIDR) {
913                accessUnit->meta()->setInt32("isSync", 1);
914            }
915
916            if (mFormat == NULL) {
917                mFormat = MakeAVCCodecSpecificData(accessUnit);
918            }
919
920            return accessUnit;
921        }
922
923        NALPosition pos;
924        pos.nalOffset = nalStart - mBuffer->data();
925        pos.nalSize = nalSize;
926
927        nals.push(pos);
928
929        totalSize += nalSize;
930    }
931    if (err != (status_t)-EAGAIN) {
932        ALOGE("Unexpeted err");
933        return NULL;
934    }
935
936    return NULL;
937}
938
939sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitMPEGAudio() {
940    const uint8_t *data = mBuffer->data();
941    size_t size = mBuffer->size();
942
943    if (size < 4) {
944        return NULL;
945    }
946
947    uint32_t header = U32_AT(data);
948
949    size_t frameSize;
950    int samplingRate, numChannels, bitrate, numSamples;
951    if (!GetMPEGAudioFrameSize(
952                header, &frameSize, &samplingRate, &numChannels,
953                &bitrate, &numSamples)) {
954        ALOGE("Failed to get audio frame size");
955        return NULL;
956    }
957
958    if (size < frameSize) {
959        return NULL;
960    }
961
962    unsigned layer = 4 - ((header >> 17) & 3);
963
964    sp<ABuffer> accessUnit = new ABuffer(frameSize);
965    memcpy(accessUnit->data(), data, frameSize);
966
967    memmove(mBuffer->data(),
968            mBuffer->data() + frameSize,
969            mBuffer->size() - frameSize);
970
971    mBuffer->setRange(0, mBuffer->size() - frameSize);
972
973    int64_t timeUs = fetchTimestamp(frameSize);
974    if (timeUs < 0ll) {
975        ALOGE("Negative timeUs");
976        return NULL;
977    }
978
979    accessUnit->meta()->setInt64("timeUs", timeUs);
980    accessUnit->meta()->setInt32("isSync", 1);
981
982    if (mFormat == NULL) {
983        mFormat = new MetaData;
984
985        switch (layer) {
986            case 1:
987                mFormat->setCString(
988                        kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_I);
989                break;
990            case 2:
991                mFormat->setCString(
992                        kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II);
993                break;
994            case 3:
995                mFormat->setCString(
996                        kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG);
997                break;
998            default:
999                return NULL;
1000        }
1001
1002        mFormat->setInt32(kKeySampleRate, samplingRate);
1003        mFormat->setInt32(kKeyChannelCount, numChannels);
1004    }
1005
1006    return accessUnit;
1007}
1008
1009static void EncodeSize14(uint8_t **_ptr, size_t size) {
1010    if (size > 0x3fff) {
1011        ALOGE("Wrong size");
1012        return;
1013    }
1014
1015    uint8_t *ptr = *_ptr;
1016
1017    *ptr++ = 0x80 | (size >> 7);
1018    *ptr++ = size & 0x7f;
1019
1020    *_ptr = ptr;
1021}
1022
1023static sp<ABuffer> MakeMPEGVideoESDS(const sp<ABuffer> &csd) {
1024    sp<ABuffer> esds = new ABuffer(csd->size() + 25);
1025
1026    uint8_t *ptr = esds->data();
1027    *ptr++ = 0x03;
1028    EncodeSize14(&ptr, 22 + csd->size());
1029
1030    *ptr++ = 0x00;  // ES_ID
1031    *ptr++ = 0x00;
1032
1033    *ptr++ = 0x00;  // streamDependenceFlag, URL_Flag, OCRstreamFlag
1034
1035    *ptr++ = 0x04;
1036    EncodeSize14(&ptr, 16 + csd->size());
1037
1038    *ptr++ = 0x40;  // Audio ISO/IEC 14496-3
1039
1040    for (size_t i = 0; i < 12; ++i) {
1041        *ptr++ = 0x00;
1042    }
1043
1044    *ptr++ = 0x05;
1045    EncodeSize14(&ptr, csd->size());
1046
1047    memcpy(ptr, csd->data(), csd->size());
1048
1049    return esds;
1050}
1051
1052sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitMPEGVideo() {
1053    const uint8_t *data = mBuffer->data();
1054    size_t size = mBuffer->size();
1055
1056    Vector<size_t> userDataPositions;
1057
1058    bool sawPictureStart = false;
1059    int pprevStartCode = -1;
1060    int prevStartCode = -1;
1061    int currentStartCode = -1;
1062    bool gopFound = false;
1063    bool isClosedGop = false;
1064    bool brokenLink = false;
1065
1066    size_t offset = 0;
1067    while (offset + 3 < size) {
1068        if (memcmp(&data[offset], "\x00\x00\x01", 3)) {
1069            ++offset;
1070            continue;
1071        }
1072
1073        pprevStartCode = prevStartCode;
1074        prevStartCode = currentStartCode;
1075        currentStartCode = data[offset + 3];
1076
1077        if (currentStartCode == 0xb3 && mFormat == NULL) {
1078            memmove(mBuffer->data(), mBuffer->data() + offset, size - offset);
1079            size -= offset;
1080            (void)fetchTimestamp(offset);
1081            offset = 0;
1082            mBuffer->setRange(0, size);
1083        }
1084
1085        if ((prevStartCode == 0xb3 && currentStartCode != 0xb5)
1086                || (pprevStartCode == 0xb3 && prevStartCode == 0xb5)) {
1087            // seqHeader without/with extension
1088
1089            if (mFormat == NULL) {
1090                if (size < 7u) {
1091                    ALOGE("Size too small");
1092                    return NULL;
1093                }
1094
1095                unsigned width =
1096                    (data[4] << 4) | data[5] >> 4;
1097
1098                unsigned height =
1099                    ((data[5] & 0x0f) << 8) | data[6];
1100
1101                mFormat = new MetaData;
1102                mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG2);
1103                mFormat->setInt32(kKeyWidth, width);
1104                mFormat->setInt32(kKeyHeight, height);
1105
1106                ALOGI("found MPEG2 video codec config (%d x %d)", width, height);
1107
1108                sp<ABuffer> csd = new ABuffer(offset);
1109                memcpy(csd->data(), data, offset);
1110
1111                memmove(mBuffer->data(),
1112                        mBuffer->data() + offset,
1113                        mBuffer->size() - offset);
1114
1115                mBuffer->setRange(0, mBuffer->size() - offset);
1116                size -= offset;
1117                (void)fetchTimestamp(offset);
1118                offset = 0;
1119
1120                // hexdump(csd->data(), csd->size());
1121
1122                sp<ABuffer> esds = MakeMPEGVideoESDS(csd);
1123                mFormat->setData(
1124                        kKeyESDS, kTypeESDS, esds->data(), esds->size());
1125
1126                return NULL;
1127            }
1128        }
1129
1130        if (mFormat != NULL && currentStartCode == 0xb8) {
1131            // GOP layer
1132            if (offset + 7 >= size) {
1133                ALOGE("Size too small");
1134                return NULL;
1135            }
1136            gopFound = true;
1137            isClosedGop = (data[offset + 7] & 0x40) != 0;
1138            brokenLink = (data[offset + 7] & 0x20) != 0;
1139        }
1140
1141        if (mFormat != NULL && currentStartCode == 0xb2) {
1142            userDataPositions.add(offset);
1143        }
1144
1145        if (mFormat != NULL && currentStartCode == 0x00) {
1146            // Picture start
1147
1148            if (!sawPictureStart) {
1149                sawPictureStart = true;
1150            } else {
1151                sp<ABuffer> accessUnit = new ABuffer(offset);
1152                memcpy(accessUnit->data(), data, offset);
1153
1154                memmove(mBuffer->data(),
1155                        mBuffer->data() + offset,
1156                        mBuffer->size() - offset);
1157
1158                mBuffer->setRange(0, mBuffer->size() - offset);
1159
1160                int64_t timeUs = fetchTimestamp(offset);
1161                if (timeUs < 0ll) {
1162                    ALOGE("Negative timeUs");
1163                    return NULL;
1164                }
1165
1166                offset = 0;
1167
1168                accessUnit->meta()->setInt64("timeUs", timeUs);
1169                if (gopFound && (!brokenLink || isClosedGop)) {
1170                    accessUnit->meta()->setInt32("isSync", 1);
1171                }
1172
1173                ALOGV("returning MPEG video access unit at time %" PRId64 " us",
1174                      timeUs);
1175
1176                // hexdump(accessUnit->data(), accessUnit->size());
1177
1178                if (userDataPositions.size() > 0) {
1179                    sp<ABuffer> mpegUserData =
1180                        new ABuffer(userDataPositions.size() * sizeof(size_t));
1181                    if (mpegUserData != NULL && mpegUserData->data() != NULL) {
1182                        for (size_t i = 0; i < userDataPositions.size(); ++i) {
1183                            memcpy(
1184                                    mpegUserData->data() + i * sizeof(size_t),
1185                                    &userDataPositions[i], sizeof(size_t));
1186                        }
1187                        accessUnit->meta()->setBuffer("mpegUserData", mpegUserData);
1188                    }
1189                }
1190
1191                return accessUnit;
1192            }
1193        }
1194
1195        ++offset;
1196    }
1197
1198    return NULL;
1199}
1200
1201static ssize_t getNextChunkSize(
1202        const uint8_t *data, size_t size) {
1203    static const char kStartCode[] = "\x00\x00\x01";
1204
1205    if (size < 3) {
1206        return -EAGAIN;
1207    }
1208
1209    if (memcmp(kStartCode, data, 3)) {
1210        return -EAGAIN;
1211    }
1212
1213    size_t offset = 3;
1214    while (offset + 2 < size) {
1215        if (!memcmp(&data[offset], kStartCode, 3)) {
1216            return offset;
1217        }
1218
1219        ++offset;
1220    }
1221
1222    return -EAGAIN;
1223}
1224
1225sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitMPEG4Video() {
1226    uint8_t *data = mBuffer->data();
1227    size_t size = mBuffer->size();
1228
1229    enum {
1230        SKIP_TO_VISUAL_OBJECT_SEQ_START,
1231        EXPECT_VISUAL_OBJECT_START,
1232        EXPECT_VO_START,
1233        EXPECT_VOL_START,
1234        WAIT_FOR_VOP_START,
1235        SKIP_TO_VOP_START,
1236
1237    } state;
1238
1239    if (mFormat == NULL) {
1240        state = SKIP_TO_VISUAL_OBJECT_SEQ_START;
1241    } else {
1242        state = SKIP_TO_VOP_START;
1243    }
1244
1245    int32_t width = -1, height = -1;
1246
1247    size_t offset = 0;
1248    ssize_t chunkSize;
1249    while ((chunkSize = getNextChunkSize(
1250                    &data[offset], size - offset)) > 0) {
1251        bool discard = false;
1252
1253        unsigned chunkType = data[offset + 3];
1254
1255        switch (state) {
1256            case SKIP_TO_VISUAL_OBJECT_SEQ_START:
1257            {
1258                if (chunkType == 0xb0) {
1259                    // Discard anything before this marker.
1260
1261                    state = EXPECT_VISUAL_OBJECT_START;
1262                } else {
1263                    discard = true;
1264                }
1265                break;
1266            }
1267
1268            case EXPECT_VISUAL_OBJECT_START:
1269            {
1270                if (chunkType != 0xb5) {
1271                    ALOGE("Unexpected chunkType");
1272                    return NULL;
1273                }
1274                state = EXPECT_VO_START;
1275                break;
1276            }
1277
1278            case EXPECT_VO_START:
1279            {
1280                if (chunkType > 0x1f) {
1281                    ALOGE("Unexpected chunkType");
1282                    return NULL;
1283                }
1284                state = EXPECT_VOL_START;
1285                break;
1286            }
1287
1288            case EXPECT_VOL_START:
1289            {
1290                if ((chunkType & 0xf0) != 0x20) {
1291                    ALOGE("Wrong chunkType");
1292                    return NULL;
1293                }
1294
1295                if (!ExtractDimensionsFromVOLHeader(
1296                            &data[offset], chunkSize,
1297                            &width, &height)) {
1298                    ALOGE("Failed to get dimension");
1299                    return NULL;
1300                }
1301
1302                state = WAIT_FOR_VOP_START;
1303                break;
1304            }
1305
1306            case WAIT_FOR_VOP_START:
1307            {
1308                if (chunkType == 0xb3 || chunkType == 0xb6) {
1309                    // group of VOP or VOP start.
1310
1311                    mFormat = new MetaData;
1312                    mFormat->setCString(
1313                            kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
1314
1315                    mFormat->setInt32(kKeyWidth, width);
1316                    mFormat->setInt32(kKeyHeight, height);
1317
1318                    ALOGI("found MPEG4 video codec config (%d x %d)",
1319                         width, height);
1320
1321                    sp<ABuffer> csd = new ABuffer(offset);
1322                    memcpy(csd->data(), data, offset);
1323
1324                    // hexdump(csd->data(), csd->size());
1325
1326                    sp<ABuffer> esds = MakeMPEGVideoESDS(csd);
1327                    mFormat->setData(
1328                            kKeyESDS, kTypeESDS,
1329                            esds->data(), esds->size());
1330
1331                    discard = true;
1332                    state = SKIP_TO_VOP_START;
1333                }
1334
1335                break;
1336            }
1337
1338            case SKIP_TO_VOP_START:
1339            {
1340                if (chunkType == 0xb6) {
1341                    int vopCodingType = (data[offset + 4] & 0xc0) >> 6;
1342
1343                    offset += chunkSize;
1344
1345                    sp<ABuffer> accessUnit = new ABuffer(offset);
1346                    memcpy(accessUnit->data(), data, offset);
1347
1348                    memmove(data, &data[offset], size - offset);
1349                    size -= offset;
1350                    mBuffer->setRange(0, size);
1351
1352                    int64_t timeUs = fetchTimestamp(offset);
1353                    if (timeUs < 0ll) {
1354                        ALOGE("Negative timeus");
1355                        return NULL;
1356                    }
1357
1358                    offset = 0;
1359
1360                    accessUnit->meta()->setInt64("timeUs", timeUs);
1361                    if (vopCodingType == 0) {  // intra-coded VOP
1362                        accessUnit->meta()->setInt32("isSync", 1);
1363                    }
1364
1365                    ALOGV("returning MPEG4 video access unit at time %" PRId64 " us",
1366                         timeUs);
1367
1368                    // hexdump(accessUnit->data(), accessUnit->size());
1369
1370                    return accessUnit;
1371                } else if (chunkType != 0xb3) {
1372                    offset += chunkSize;
1373                    discard = true;
1374                }
1375
1376                break;
1377            }
1378
1379            default:
1380                ALOGE("Unknown state: %d", state);
1381                return NULL;
1382        }
1383
1384        if (discard) {
1385            (void)fetchTimestamp(offset);
1386            memmove(data, &data[offset], size - offset);
1387            size -= offset;
1388            offset = 0;
1389            mBuffer->setRange(0, size);
1390        } else {
1391            offset += chunkSize;
1392        }
1393    }
1394
1395    return NULL;
1396}
1397
1398void ElementaryStreamQueue::signalEOS() {
1399    if (!mEOSReached) {
1400        if (mMode == MPEG_VIDEO) {
1401            const char *theEnd = "\x00\x00\x01\x00";
1402            appendData(theEnd, 4, 0);
1403        }
1404        mEOSReached = true;
1405    } else {
1406        ALOGW("EOS already signaled");
1407    }
1408}
1409
1410sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitMetadata() {
1411    size_t size = mBuffer->size();
1412    if (!size) {
1413        return NULL;
1414    }
1415
1416    sp<ABuffer> accessUnit = new ABuffer(size);
1417    int64_t timeUs = fetchTimestamp(size);
1418    accessUnit->meta()->setInt64("timeUs", timeUs);
1419
1420    memcpy(accessUnit->data(), mBuffer->data(), size);
1421    mBuffer->setRange(0, 0);
1422
1423    if (mFormat == NULL) {
1424        mFormat = new MetaData;
1425        mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_DATA_TIMED_ID3);
1426    }
1427
1428    return accessUnit;
1429}
1430
1431}  // namespace android
1432