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 #include <media/cas/DescramblerAPI.h>
32 #include <media/hardware/CryptoAPI.h>
33 
34 #include "include/avc_utils.h"
35 
36 #include <inttypes.h>
37 #include <netinet/in.h>
38 
39 namespace android {
40 
ElementaryStreamQueue(Mode mode,uint32_t flags)41 ElementaryStreamQueue::ElementaryStreamQueue(Mode mode, uint32_t flags)
42     : mMode(mode),
43       mFlags(flags),
44       mEOSReached(false),
45       mCASystemId(0),
46       mAUIndex(0) {
47 
48     ALOGV("ElementaryStreamQueue(%p) mode %x  flags %x  isScrambled %d  isSampleEncrypted %d",
49             this, mode, flags, isScrambled(), isSampleEncrypted());
50 
51     // Create the decryptor anyway since we don't know the use-case unless key is provided
52     // Won't decrypt if key info not available (e.g., scanner/extractor just parsing ts files)
53     mSampleDecryptor = isSampleEncrypted() ? new HlsSampleDecryptor : NULL;
54 }
55 
getFormat()56 sp<MetaData> ElementaryStreamQueue::getFormat() {
57     return mFormat;
58 }
59 
clear(bool clearFormat)60 void ElementaryStreamQueue::clear(bool clearFormat) {
61     if (mBuffer != NULL) {
62         mBuffer->setRange(0, 0);
63     }
64 
65     mRangeInfos.clear();
66 
67     if (mScrambledBuffer != NULL) {
68         mScrambledBuffer->setRange(0, 0);
69     }
70     mScrambledRangeInfos.clear();
71 
72     if (clearFormat) {
73         mFormat.clear();
74     }
75 
76     mEOSReached = false;
77 }
78 
isScrambled() const79 bool ElementaryStreamQueue::isScrambled() const {
80     return (mFlags & kFlag_ScrambledData) != 0;
81 }
82 
setCasInfo(int32_t systemId,const std::vector<uint8_t> & sessionId)83 void ElementaryStreamQueue::setCasInfo(
84         int32_t systemId, const std::vector<uint8_t> &sessionId) {
85     mCASystemId = systemId;
86     mCasSessionId = sessionId;
87 }
88 
89 // Parse AC3 header assuming the current ptr is start position of syncframe,
90 // update metadata only applicable, and return the payload size
parseAC3SyncFrame(const uint8_t * ptr,size_t size,sp<MetaData> * metaData)91 static unsigned parseAC3SyncFrame(
92         const uint8_t *ptr, size_t size, sp<MetaData> *metaData) {
93     static const unsigned channelCountTable[] = {2, 1, 2, 3, 3, 4, 4, 5};
94     static const unsigned samplingRateTable[] = {48000, 44100, 32000};
95 
96     static const unsigned frameSizeTable[19][3] = {
97         { 64, 69, 96 },
98         { 80, 87, 120 },
99         { 96, 104, 144 },
100         { 112, 121, 168 },
101         { 128, 139, 192 },
102         { 160, 174, 240 },
103         { 192, 208, 288 },
104         { 224, 243, 336 },
105         { 256, 278, 384 },
106         { 320, 348, 480 },
107         { 384, 417, 576 },
108         { 448, 487, 672 },
109         { 512, 557, 768 },
110         { 640, 696, 960 },
111         { 768, 835, 1152 },
112         { 896, 975, 1344 },
113         { 1024, 1114, 1536 },
114         { 1152, 1253, 1728 },
115         { 1280, 1393, 1920 },
116     };
117 
118     ABitReader bits(ptr, size);
119     if (bits.numBitsLeft() < 16) {
120         return 0;
121     }
122     if (bits.getBits(16) != 0x0B77) {
123         return 0;
124     }
125 
126     if (bits.numBitsLeft() < 16 + 2 + 6 + 5 + 3 + 3) {
127         ALOGV("Not enough bits left for further parsing");
128         return 0;
129     }
130     bits.skipBits(16);  // crc1
131 
132     unsigned fscod = bits.getBits(2);
133     if (fscod == 3) {
134         ALOGW("Incorrect fscod in AC3 header");
135         return 0;
136     }
137 
138     unsigned frmsizecod = bits.getBits(6);
139     if (frmsizecod > 37) {
140         ALOGW("Incorrect frmsizecod in AC3 header");
141         return 0;
142     }
143 
144     unsigned bsid = bits.getBits(5);
145     if (bsid > 8) {
146         ALOGW("Incorrect bsid in AC3 header. Possibly E-AC-3?");
147         return 0;
148     }
149 
150     unsigned bsmod __unused = bits.getBits(3);
151     unsigned acmod = bits.getBits(3);
152     unsigned cmixlev __unused = 0;
153     unsigned surmixlev __unused = 0;
154     unsigned dsurmod __unused = 0;
155 
156     if ((acmod & 1) > 0 && acmod != 1) {
157         if (bits.numBitsLeft() < 2) {
158             return 0;
159         }
160         cmixlev = bits.getBits(2);
161     }
162     if ((acmod & 4) > 0) {
163         if (bits.numBitsLeft() < 2) {
164             return 0;
165         }
166         surmixlev = bits.getBits(2);
167     }
168     if (acmod == 2) {
169         if (bits.numBitsLeft() < 2) {
170             return 0;
171         }
172         dsurmod = bits.getBits(2);
173     }
174 
175     if (bits.numBitsLeft() < 1) {
176         return 0;
177     }
178     unsigned lfeon = bits.getBits(1);
179 
180     unsigned samplingRate = samplingRateTable[fscod];
181     unsigned payloadSize = frameSizeTable[frmsizecod >> 1][fscod];
182     if (fscod == 1) {
183         payloadSize += frmsizecod & 1;
184     }
185     payloadSize <<= 1;  // convert from 16-bit words to bytes
186 
187     unsigned channelCount = channelCountTable[acmod] + lfeon;
188 
189     if (metaData != NULL) {
190         (*metaData)->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AC3);
191         (*metaData)->setInt32(kKeyChannelCount, channelCount);
192         (*metaData)->setInt32(kKeySampleRate, samplingRate);
193     }
194 
195     return payloadSize;
196 }
197 
IsSeeminglyValidAC3Header(const uint8_t * ptr,size_t size)198 static bool IsSeeminglyValidAC3Header(const uint8_t *ptr, size_t size) {
199     return parseAC3SyncFrame(ptr, size, NULL) > 0;
200 }
201 
IsSeeminglyValidADTSHeader(const uint8_t * ptr,size_t size,size_t * frameLength)202 static bool IsSeeminglyValidADTSHeader(
203         const uint8_t *ptr, size_t size, size_t *frameLength) {
204     if (size < 7) {
205         // Not enough data to verify header.
206         return false;
207     }
208 
209     if (ptr[0] != 0xff || (ptr[1] >> 4) != 0x0f) {
210         return false;
211     }
212 
213     unsigned layer = (ptr[1] >> 1) & 3;
214 
215     if (layer != 0) {
216         return false;
217     }
218 
219     unsigned ID = (ptr[1] >> 3) & 1;
220     unsigned profile_ObjectType = ptr[2] >> 6;
221 
222     if (ID == 1 && profile_ObjectType == 3) {
223         // MPEG-2 profile 3 is reserved.
224         return false;
225     }
226 
227     size_t frameLengthInHeader =
228             ((ptr[3] & 3) << 11) + (ptr[4] << 3) + ((ptr[5] >> 5) & 7);
229     if (frameLengthInHeader > size) {
230         return false;
231     }
232 
233     *frameLength = frameLengthInHeader;
234     return true;
235 }
236 
IsSeeminglyValidMPEGAudioHeader(const uint8_t * ptr,size_t size)237 static bool IsSeeminglyValidMPEGAudioHeader(const uint8_t *ptr, size_t size) {
238     if (size < 3) {
239         // Not enough data to verify header.
240         return false;
241     }
242 
243     if (ptr[0] != 0xff || (ptr[1] >> 5) != 0x07) {
244         return false;
245     }
246 
247     unsigned ID = (ptr[1] >> 3) & 3;
248 
249     if (ID == 1) {
250         return false;  // reserved
251     }
252 
253     unsigned layer = (ptr[1] >> 1) & 3;
254 
255     if (layer == 0) {
256         return false;  // reserved
257     }
258 
259     unsigned bitrateIndex = (ptr[2] >> 4);
260 
261     if (bitrateIndex == 0x0f) {
262         return false;  // reserved
263     }
264 
265     unsigned samplingRateIndex = (ptr[2] >> 2) & 3;
266 
267     if (samplingRateIndex == 3) {
268         return false;  // reserved
269     }
270 
271     return true;
272 }
273 
appendData(const void * data,size_t size,int64_t timeUs,int32_t payloadOffset,uint32_t pesScramblingControl)274 status_t ElementaryStreamQueue::appendData(
275         const void *data, size_t size, int64_t timeUs,
276         int32_t payloadOffset, uint32_t pesScramblingControl) {
277 
278     if (mEOSReached) {
279         ALOGE("appending data after EOS");
280         return ERROR_MALFORMED;
281     }
282     if (mBuffer == NULL || mBuffer->size() == 0) {
283         switch (mMode) {
284             case H264:
285             case MPEG_VIDEO:
286             {
287 #if 0
288                 if (size < 4 || memcmp("\x00\x00\x00\x01", data, 4)) {
289                     return ERROR_MALFORMED;
290                 }
291 #else
292                 uint8_t *ptr = (uint8_t *)data;
293 
294                 ssize_t startOffset = -1;
295                 for (size_t i = 0; i + 2 < size; ++i) {
296                     if (!memcmp("\x00\x00\x01", &ptr[i], 3)) {
297                         startOffset = i;
298                         break;
299                     }
300                 }
301 
302                 if (startOffset < 0) {
303                     return ERROR_MALFORMED;
304                 }
305 
306                 if (mFormat == NULL && startOffset > 0) {
307                     ALOGI("found something resembling an H.264/MPEG syncword "
308                           "at offset %zd",
309                           startOffset);
310                 }
311 
312                 data = &ptr[startOffset];
313                 size -= startOffset;
314 #endif
315                 break;
316             }
317 
318             case MPEG4_VIDEO:
319             {
320 #if 0
321                 if (size < 3 || memcmp("\x00\x00\x01", data, 3)) {
322                     return ERROR_MALFORMED;
323                 }
324 #else
325                 uint8_t *ptr = (uint8_t *)data;
326 
327                 ssize_t startOffset = -1;
328                 for (size_t i = 0; i + 2 < size; ++i) {
329                     if (!memcmp("\x00\x00\x01", &ptr[i], 3)) {
330                         startOffset = i;
331                         break;
332                     }
333                 }
334 
335                 if (startOffset < 0) {
336                     return ERROR_MALFORMED;
337                 }
338 
339                 if (startOffset > 0) {
340                     ALOGI("found something resembling an H.264/MPEG syncword "
341                           "at offset %zd",
342                           startOffset);
343                 }
344 
345                 data = &ptr[startOffset];
346                 size -= startOffset;
347 #endif
348                 break;
349             }
350 
351             case AAC:
352             {
353                 uint8_t *ptr = (uint8_t *)data;
354 
355 #if 0
356                 if (size < 2 || ptr[0] != 0xff || (ptr[1] >> 4) != 0x0f) {
357                     return ERROR_MALFORMED;
358                 }
359 #else
360                 ssize_t startOffset = -1;
361                 size_t frameLength;
362                 for (size_t i = 0; i < size; ++i) {
363                     if (IsSeeminglyValidADTSHeader(
364                             &ptr[i], size - i, &frameLength)) {
365                         startOffset = i;
366                         break;
367                     }
368                 }
369 
370                 if (startOffset < 0) {
371                     return ERROR_MALFORMED;
372                 }
373 
374                 if (startOffset > 0) {
375                     ALOGI("found something resembling an AAC syncword at "
376                           "offset %zd",
377                           startOffset);
378                 }
379 
380                 if (frameLength != size - startOffset) {
381                     ALOGV("First ADTS AAC frame length is %zd bytes, "
382                           "while the buffer size is %zd bytes.",
383                           frameLength, size - startOffset);
384                 }
385 
386                 data = &ptr[startOffset];
387                 size -= startOffset;
388 #endif
389                 break;
390             }
391 
392             case AC3:
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 (IsSeeminglyValidAC3Header(&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 AC3 syncword at "
410                           "offset %zd",
411                           startOffset);
412                 }
413 
414                 data = &ptr[startOffset];
415                 size -= startOffset;
416                 break;
417             }
418 
419             case MPEG_AUDIO:
420             {
421                 uint8_t *ptr = (uint8_t *)data;
422 
423                 ssize_t startOffset = -1;
424                 for (size_t i = 0; i < size; ++i) {
425                     if (IsSeeminglyValidMPEGAudioHeader(&ptr[i], size - i)) {
426                         startOffset = i;
427                         break;
428                     }
429                 }
430 
431                 if (startOffset < 0) {
432                     return ERROR_MALFORMED;
433                 }
434 
435                 if (startOffset > 0) {
436                     ALOGI("found something resembling an MPEG audio "
437                           "syncword at offset %zd",
438                           startOffset);
439                 }
440 
441                 data = &ptr[startOffset];
442                 size -= startOffset;
443                 break;
444             }
445 
446             case PCM_AUDIO:
447             case METADATA:
448             {
449                 break;
450             }
451 
452             default:
453                 ALOGE("Unknown mode: %d", mMode);
454                 return ERROR_MALFORMED;
455         }
456     }
457 
458     size_t neededSize = (mBuffer == NULL ? 0 : mBuffer->size()) + size;
459     if (mBuffer == NULL || neededSize > mBuffer->capacity()) {
460         neededSize = (neededSize + 65535) & ~65535;
461 
462         ALOGV("resizing buffer to size %zu", neededSize);
463 
464         sp<ABuffer> buffer = new ABuffer(neededSize);
465         if (mBuffer != NULL) {
466             memcpy(buffer->data(), mBuffer->data(), mBuffer->size());
467             buffer->setRange(0, mBuffer->size());
468         } else {
469             buffer->setRange(0, 0);
470         }
471 
472         mBuffer = buffer;
473     }
474 
475     memcpy(mBuffer->data() + mBuffer->size(), data, size);
476     mBuffer->setRange(0, mBuffer->size() + size);
477 
478     RangeInfo info;
479     info.mLength = size;
480     info.mTimestampUs = timeUs;
481     info.mPesOffset = payloadOffset;
482     info.mPesScramblingControl = pesScramblingControl;
483     mRangeInfos.push_back(info);
484 
485 #if 0
486     if (mMode == AAC) {
487         ALOGI("size = %zu, timeUs = %.2f secs", size, timeUs / 1E6);
488         hexdump(data, size);
489     }
490 #endif
491 
492     return OK;
493 }
494 
appendScrambledData(const void * data,size_t size,int32_t keyId,bool isSync,sp<ABuffer> clearSizes,sp<ABuffer> encSizes)495 void ElementaryStreamQueue::appendScrambledData(
496         const void *data, size_t size,
497         int32_t keyId, bool isSync,
498         sp<ABuffer> clearSizes, sp<ABuffer> encSizes) {
499     if (!isScrambled()) {
500         return;
501     }
502 
503     size_t neededSize = (mScrambledBuffer == NULL ? 0 : mScrambledBuffer->size()) + size;
504     if (mScrambledBuffer == NULL || neededSize > mScrambledBuffer->capacity()) {
505         neededSize = (neededSize + 65535) & ~65535;
506 
507         ALOGI("resizing scrambled buffer to size %zu", neededSize);
508 
509         sp<ABuffer> buffer = new ABuffer(neededSize);
510         if (mScrambledBuffer != NULL) {
511             memcpy(buffer->data(), mScrambledBuffer->data(), mScrambledBuffer->size());
512             buffer->setRange(0, mScrambledBuffer->size());
513         } else {
514             buffer->setRange(0, 0);
515         }
516 
517         mScrambledBuffer = buffer;
518     }
519     memcpy(mScrambledBuffer->data() + mScrambledBuffer->size(), data, size);
520     mScrambledBuffer->setRange(0, mScrambledBuffer->size() + size);
521 
522     ScrambledRangeInfo scrambledInfo;
523     scrambledInfo.mLength = size;
524     scrambledInfo.mKeyId = keyId;
525     scrambledInfo.mIsSync = isSync;
526     scrambledInfo.mClearSizes = clearSizes;
527     scrambledInfo.mEncSizes = encSizes;
528 
529     ALOGV("[stream %d] appending scrambled range: size=%zu", mMode, size);
530 
531     mScrambledRangeInfos.push_back(scrambledInfo);
532 }
533 
dequeueScrambledAccessUnit()534 sp<ABuffer> ElementaryStreamQueue::dequeueScrambledAccessUnit() {
535     size_t nextScan = mBuffer->size();
536     mBuffer->setRange(0, 0);
537     int32_t pesOffset = 0, pesScramblingControl = 0;
538     int64_t timeUs = fetchTimestamp(nextScan, &pesOffset, &pesScramblingControl);
539     if (timeUs < 0ll) {
540         ALOGE("Negative timeUs");
541         return NULL;
542     }
543 
544     // return scrambled unit
545     int32_t keyId = pesScramblingControl, isSync = 0, scrambledLength = 0;
546     sp<ABuffer> clearSizes, encSizes;
547     while (mScrambledRangeInfos.size() > mRangeInfos.size()) {
548         auto it = mScrambledRangeInfos.begin();
549         ALOGV("[stream %d] fetching scrambled range: size=%zu", mMode, it->mLength);
550 
551         if (scrambledLength > 0) {
552             // This shouldn't happen since we always dequeue the entire PES.
553             ALOGW("Discarding srambled length %d", scrambledLength);
554         }
555         scrambledLength = it->mLength;
556 
557         // TODO: handle key id change, use first non-zero keyId for now
558         if (keyId == 0) {
559             keyId = it->mKeyId;
560         }
561         clearSizes = it->mClearSizes;
562         encSizes = it->mEncSizes;
563         isSync = it->mIsSync;
564         mScrambledRangeInfos.erase(it);
565     }
566     if (scrambledLength == 0) {
567         ALOGE("[stream %d] empty scrambled unit!", mMode);
568         return NULL;
569     }
570 
571     // skip the PES header, and copy the rest into scrambled access unit
572     sp<ABuffer> scrambledAccessUnit = ABuffer::CreateAsCopy(
573             mScrambledBuffer->data() + pesOffset,
574             scrambledLength - pesOffset);
575 
576     // fix up first sample size after skipping the PES header
577     if (pesOffset > 0) {
578         int32_t &firstClearSize = *(int32_t*)clearSizes->data();
579         int32_t &firstEncSize = *(int32_t*)encSizes->data();
580         // Cut away the PES header
581         if (firstClearSize >= pesOffset) {
582             // This is for TS-level scrambling, we descrambled the first
583             // (or it was clear to begin with)
584             firstClearSize -= pesOffset;
585         } else if (firstEncSize >= pesOffset) {
586             // This can only be PES-level scrambling
587             firstEncSize -= pesOffset;
588         }
589     }
590 
591     scrambledAccessUnit->meta()->setInt64("timeUs", timeUs);
592     if (isSync) {
593         scrambledAccessUnit->meta()->setInt32("isSync", 1);
594     }
595 
596     // fill in CryptoInfo fields for AnotherPacketSource::read()
597     // MediaCas doesn't use cryptoMode, but set to non-zero value here.
598     scrambledAccessUnit->meta()->setInt32(
599             "cryptoMode", CryptoPlugin::kMode_AES_CTR);
600     scrambledAccessUnit->meta()->setInt32("cryptoKey", keyId);
601     scrambledAccessUnit->meta()->setBuffer("clearBytes", clearSizes);
602     scrambledAccessUnit->meta()->setBuffer("encBytes", encSizes);
603 
604     memmove(mScrambledBuffer->data(),
605             mScrambledBuffer->data() + scrambledLength,
606             mScrambledBuffer->size() - scrambledLength);
607 
608     mScrambledBuffer->setRange(0, mScrambledBuffer->size() - scrambledLength);
609 
610     ALOGV("[stream %d] dequeued scrambled AU: timeUs=%lld, size=%zu",
611             mMode, (long long)timeUs, scrambledAccessUnit->size());
612 
613     return scrambledAccessUnit;
614 }
615 
dequeueAccessUnit()616 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnit() {
617     if ((mFlags & kFlag_AlignedData) && mMode == H264 && !isScrambled()) {
618         if (mRangeInfos.empty()) {
619             return NULL;
620         }
621 
622         RangeInfo info = *mRangeInfos.begin();
623         mRangeInfos.erase(mRangeInfos.begin());
624 
625         sp<ABuffer> accessUnit = new ABuffer(info.mLength);
626         memcpy(accessUnit->data(), mBuffer->data(), info.mLength);
627         accessUnit->meta()->setInt64("timeUs", info.mTimestampUs);
628 
629         memmove(mBuffer->data(),
630                 mBuffer->data() + info.mLength,
631                 mBuffer->size() - info.mLength);
632 
633         mBuffer->setRange(0, mBuffer->size() - info.mLength);
634 
635         if (mFormat == NULL) {
636             mFormat = MakeAVCCodecSpecificData(accessUnit);
637         }
638 
639         return accessUnit;
640     }
641 
642     switch (mMode) {
643         case H264:
644             return dequeueAccessUnitH264();
645         case AAC:
646             return dequeueAccessUnitAAC();
647         case AC3:
648             return dequeueAccessUnitAC3();
649         case MPEG_VIDEO:
650             return dequeueAccessUnitMPEGVideo();
651         case MPEG4_VIDEO:
652             return dequeueAccessUnitMPEG4Video();
653         case PCM_AUDIO:
654             return dequeueAccessUnitPCMAudio();
655         case METADATA:
656             return dequeueAccessUnitMetadata();
657         default:
658             if (mMode != MPEG_AUDIO) {
659                 ALOGE("Unknown mode");
660                 return NULL;
661             }
662             return dequeueAccessUnitMPEGAudio();
663     }
664 }
665 
dequeueAccessUnitAC3()666 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitAC3() {
667     unsigned syncStartPos = 0;  // in bytes
668     unsigned payloadSize = 0;
669     sp<MetaData> format = new MetaData;
670 
671     ALOGV("dequeueAccessUnit_AC3[%d]: mBuffer %p(%zu)", mAUIndex, mBuffer->data(), mBuffer->size());
672 
673     while (true) {
674         if (syncStartPos + 2 >= mBuffer->size()) {
675             return NULL;
676         }
677 
678         payloadSize = parseAC3SyncFrame(
679                 mBuffer->data() + syncStartPos,
680                 mBuffer->size() - syncStartPos,
681                 &format);
682         if (payloadSize > 0) {
683             break;
684         }
685 
686         ALOGV("dequeueAccessUnit_AC3[%d]: syncStartPos %u payloadSize %u",
687                 mAUIndex, syncStartPos, payloadSize);
688 
689         ++syncStartPos;
690     }
691 
692     if (mBuffer->size() < syncStartPos + payloadSize) {
693         ALOGV("Not enough buffer size for AC3");
694         return NULL;
695     }
696 
697     if (mFormat == NULL) {
698         mFormat = format;
699     }
700 
701 
702     int64_t timeUs = fetchTimestamp(syncStartPos + payloadSize);
703     if (timeUs < 0ll) {
704         ALOGE("negative timeUs");
705         return NULL;
706     }
707 
708     // Not decrypting if key info not available (e.g., scanner/extractor parsing ts files)
709     if (mSampleDecryptor != NULL) {
710         mSampleDecryptor->processAC3(mBuffer->data() + syncStartPos, payloadSize);
711     }
712     mAUIndex++;
713 
714     sp<ABuffer> accessUnit = new ABuffer(syncStartPos + payloadSize);
715     memcpy(accessUnit->data(), mBuffer->data(), syncStartPos + payloadSize);
716 
717     accessUnit->meta()->setInt64("timeUs", timeUs);
718     accessUnit->meta()->setInt32("isSync", 1);
719 
720     memmove(
721             mBuffer->data(),
722             mBuffer->data() + syncStartPos + payloadSize,
723             mBuffer->size() - syncStartPos - payloadSize);
724 
725     mBuffer->setRange(0, mBuffer->size() - syncStartPos - payloadSize);
726 
727     return accessUnit;
728 }
729 
dequeueAccessUnitPCMAudio()730 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitPCMAudio() {
731     if (mBuffer->size() < 4) {
732         return NULL;
733     }
734 
735     ABitReader bits(mBuffer->data(), 4);
736     if (bits.getBits(8) != 0xa0) {
737         ALOGE("Unexpected bit values");
738         return NULL;
739     }
740     unsigned numAUs = bits.getBits(8);
741     bits.skipBits(8);
742     unsigned quantization_word_length __unused = bits.getBits(2);
743     unsigned audio_sampling_frequency = bits.getBits(3);
744     unsigned num_channels = bits.getBits(3);
745 
746     if (audio_sampling_frequency != 2) {
747         ALOGE("Wrong sampling freq");
748         return NULL;
749     }
750     if (num_channels != 1u) {
751         ALOGE("Wrong channel #");
752         return NULL;
753     }
754 
755     if (mFormat == NULL) {
756         mFormat = new MetaData;
757         mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
758         mFormat->setInt32(kKeyChannelCount, 2);
759         mFormat->setInt32(kKeySampleRate, 48000);
760         mFormat->setInt32(kKeyPcmEncoding, kAudioEncodingPcm16bit);
761     }
762 
763     static const size_t kFramesPerAU = 80;
764     size_t frameSize = 2 /* numChannels */ * sizeof(int16_t);
765 
766     size_t payloadSize = numAUs * frameSize * kFramesPerAU;
767 
768     if (mBuffer->size() < 4 + payloadSize) {
769         return NULL;
770     }
771 
772     sp<ABuffer> accessUnit = new ABuffer(payloadSize);
773     memcpy(accessUnit->data(), mBuffer->data() + 4, payloadSize);
774 
775     int64_t timeUs = fetchTimestamp(payloadSize + 4);
776     if (timeUs < 0ll) {
777         ALOGE("Negative timeUs");
778         return NULL;
779     }
780     accessUnit->meta()->setInt64("timeUs", timeUs);
781     accessUnit->meta()->setInt32("isSync", 1);
782 
783     int16_t *ptr = (int16_t *)accessUnit->data();
784     for (size_t i = 0; i < payloadSize / sizeof(int16_t); ++i) {
785         ptr[i] = ntohs(ptr[i]);
786     }
787 
788     memmove(
789             mBuffer->data(),
790             mBuffer->data() + 4 + payloadSize,
791             mBuffer->size() - 4 - payloadSize);
792 
793     mBuffer->setRange(0, mBuffer->size() - 4 - payloadSize);
794 
795     return accessUnit;
796 }
797 
dequeueAccessUnitAAC()798 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitAAC() {
799     if (mBuffer->size() == 0) {
800         return NULL;
801     }
802 
803     if (mRangeInfos.empty()) {
804         return NULL;
805     }
806 
807     const RangeInfo &info = *mRangeInfos.begin();
808     if (mBuffer->size() < info.mLength) {
809         return NULL;
810     }
811 
812     if (info.mTimestampUs < 0ll) {
813         ALOGE("Negative info.mTimestampUs");
814         return NULL;
815     }
816 
817     ALOGV("dequeueAccessUnit_AAC[%d]: mBuffer %zu info.mLength %zu",
818             mAUIndex, mBuffer->size(), info.mLength);
819 
820     struct ADTSPosition {
821         size_t offset;
822         size_t headerSize;
823         size_t length;
824     };
825 
826     Vector<ADTSPosition> frames;
827 
828     // The idea here is consume all AAC frames starting at offsets before
829     // info.mLength so we can assign a meaningful timestamp without
830     // having to interpolate.
831     // The final AAC frame may well extend into the next RangeInfo but
832     // that's ok.
833     size_t offset = 0;
834     while (offset < info.mLength) {
835         if (offset + 7 > mBuffer->size()) {
836             return NULL;
837         }
838 
839         ABitReader bits(mBuffer->data() + offset, mBuffer->size() - offset);
840 
841         // adts_fixed_header
842 
843         if (bits.getBits(12) != 0xfffu) {
844             ALOGE("Wrong atds_fixed_header");
845             return NULL;
846         }
847         bits.skipBits(3);  // ID, layer
848         bool protection_absent = bits.getBits(1) != 0;
849 
850         if (mFormat == NULL) {
851             unsigned profile = bits.getBits(2);
852             if (profile == 3u) {
853                 ALOGE("profile should not be 3");
854                 return NULL;
855             }
856             unsigned sampling_freq_index = bits.getBits(4);
857             bits.getBits(1);  // private_bit
858             unsigned channel_configuration = bits.getBits(3);
859             if (channel_configuration == 0u) {
860                 ALOGE("channel_config should not be 0");
861                 return NULL;
862             }
863             bits.skipBits(2);  // original_copy, home
864 
865             mFormat = MakeAACCodecSpecificData(
866                     profile, sampling_freq_index, channel_configuration);
867 
868             mFormat->setInt32(kKeyIsADTS, true);
869 
870             int32_t sampleRate;
871             int32_t numChannels;
872             if (!mFormat->findInt32(kKeySampleRate, &sampleRate)) {
873                 ALOGE("SampleRate not found");
874                 return NULL;
875             }
876             if (!mFormat->findInt32(kKeyChannelCount, &numChannels)) {
877                 ALOGE("ChannelCount not found");
878                 return NULL;
879             }
880 
881             ALOGI("found AAC codec config (%d Hz, %d channels)",
882                  sampleRate, numChannels);
883         } else {
884             // profile_ObjectType, sampling_frequency_index, private_bits,
885             // channel_configuration, original_copy, home
886             bits.skipBits(12);
887         }
888 
889         // adts_variable_header
890 
891         // copyright_identification_bit, copyright_identification_start
892         bits.skipBits(2);
893 
894         unsigned aac_frame_length = bits.getBits(13);
895 
896         bits.skipBits(11);  // adts_buffer_fullness
897 
898         unsigned number_of_raw_data_blocks_in_frame = bits.getBits(2);
899 
900         if (number_of_raw_data_blocks_in_frame != 0) {
901             // To be implemented.
902             ALOGE("Should not reach here.");
903             return NULL;
904         }
905 
906         if (offset + aac_frame_length > mBuffer->size()) {
907             return NULL;
908         }
909 
910         size_t headerSize = protection_absent ? 7 : 9;
911 
912         // tracking the frame positions first then decrypt only if an accessUnit to be generated
913         if (mSampleDecryptor != NULL) {
914             ADTSPosition frame = {
915                 .offset     = offset,
916                 .headerSize = headerSize,
917                 .length     = aac_frame_length
918             };
919 
920             frames.push(frame);
921         }
922 
923         offset += aac_frame_length;
924     }
925 
926     // Decrypting only if the loop didn't exit early and an accessUnit is about to be generated
927     // Not decrypting if key info not available (e.g., scanner/extractor parsing ts files)
928     if (mSampleDecryptor != NULL) {
929         for (size_t frameId = 0; frameId < frames.size(); frameId++) {
930             const ADTSPosition &frame = frames.itemAt(frameId);
931 
932             mSampleDecryptor->processAAC(frame.headerSize,
933                     mBuffer->data() + frame.offset, frame.length);
934 //            ALOGV("dequeueAccessUnitAAC[%zu]: while offset %zu headerSize %zu frame_len %zu",
935 //                    frameId, frame.offset, frame.headerSize, frame.length);
936         }
937     }
938     mAUIndex++;
939 
940     int64_t timeUs = fetchTimestamp(offset);
941 
942     sp<ABuffer> accessUnit = new ABuffer(offset);
943     memcpy(accessUnit->data(), mBuffer->data(), offset);
944 
945     memmove(mBuffer->data(), mBuffer->data() + offset,
946             mBuffer->size() - offset);
947     mBuffer->setRange(0, mBuffer->size() - offset);
948 
949     accessUnit->meta()->setInt64("timeUs", timeUs);
950     accessUnit->meta()->setInt32("isSync", 1);
951 
952     return accessUnit;
953 }
954 
fetchTimestamp(size_t size,int32_t * pesOffset,int32_t * pesScramblingControl)955 int64_t ElementaryStreamQueue::fetchTimestamp(
956         size_t size, int32_t *pesOffset, int32_t *pesScramblingControl) {
957     int64_t timeUs = -1;
958     bool first = true;
959 
960     while (size > 0) {
961         if (mRangeInfos.empty()) {
962             return timeUs;
963         }
964 
965         RangeInfo *info = &*mRangeInfos.begin();
966 
967         if (first) {
968             timeUs = info->mTimestampUs;
969             if (pesOffset != NULL) {
970                 *pesOffset = info->mPesOffset;
971             }
972             if (pesScramblingControl != NULL) {
973                 *pesScramblingControl = info->mPesScramblingControl;
974             }
975             first = false;
976         }
977 
978         if (info->mLength > size) {
979             info->mLength -= size;
980             size = 0;
981         } else {
982             size -= info->mLength;
983 
984             mRangeInfos.erase(mRangeInfos.begin());
985             info = NULL;
986         }
987 
988     }
989 
990     if (timeUs == 0ll) {
991         ALOGV("Returning 0 timestamp");
992     }
993 
994     return timeUs;
995 }
996 
dequeueAccessUnitH264()997 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitH264() {
998     if (isScrambled()) {
999         if (mBuffer == NULL || mBuffer->size() == 0) {
1000             return NULL;
1001         }
1002         if (mFormat == NULL) {
1003             mFormat = MakeAVCCodecSpecificData(mBuffer);
1004             if (mFormat == NULL) {
1005                 ALOGI("Creating dummy AVC format for scrambled content");
1006                 mFormat = new MetaData;
1007                 mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
1008                 mFormat->setInt32(kKeyWidth, 1280);
1009                 mFormat->setInt32(kKeyHeight, 720);
1010             }
1011             // for MediaExtractor.CasInfo
1012             mFormat->setInt32(kKeyCASystemID, mCASystemId);
1013             mFormat->setData(kKeyCASessionID, 0,
1014                     mCasSessionId.data(), mCasSessionId.size());
1015         }
1016         return dequeueScrambledAccessUnit();
1017     }
1018 
1019     const uint8_t *data = mBuffer->data();
1020 
1021     size_t size = mBuffer->size();
1022     Vector<NALPosition> nals;
1023 
1024     size_t totalSize = 0;
1025     size_t seiCount = 0;
1026 
1027     status_t err;
1028     const uint8_t *nalStart;
1029     size_t nalSize;
1030     bool foundSlice = false;
1031     bool foundIDR = false;
1032 
1033     ALOGV("dequeueAccessUnit_H264[%d] %p/%zu", mAUIndex, data, size);
1034 
1035     while ((err = getNextNALUnit(&data, &size, &nalStart, &nalSize)) == OK) {
1036         if (nalSize == 0) continue;
1037 
1038         unsigned nalType = nalStart[0] & 0x1f;
1039         bool flush = false;
1040 
1041         if (nalType == 1 || nalType == 5) {
1042             if (nalType == 5) {
1043                 foundIDR = true;
1044             }
1045             if (foundSlice) {
1046                 //TODO: Shouldn't this have been called with nalSize-1?
1047                 ABitReader br(nalStart + 1, nalSize);
1048                 unsigned first_mb_in_slice = parseUE(&br);
1049 
1050                 if (first_mb_in_slice == 0) {
1051                     // This slice starts a new frame.
1052 
1053                     flush = true;
1054                 }
1055             }
1056 
1057             foundSlice = true;
1058         } else if ((nalType == 9 || nalType == 7) && foundSlice) {
1059             // Access unit delimiter and SPS will be associated with the
1060             // next frame.
1061 
1062             flush = true;
1063         } else if (nalType == 6 && nalSize > 0) {
1064             // found non-zero sized SEI
1065             ++seiCount;
1066         }
1067 
1068         if (flush) {
1069             // The access unit will contain all nal units up to, but excluding
1070             // the current one, separated by 0x00 0x00 0x00 0x01 startcodes.
1071 
1072             size_t auSize = 4 * nals.size() + totalSize;
1073             sp<ABuffer> accessUnit = new ABuffer(auSize);
1074             sp<ABuffer> sei;
1075 
1076             if (seiCount > 0) {
1077                 sei = new ABuffer(seiCount * sizeof(NALPosition));
1078                 accessUnit->meta()->setBuffer("sei", sei);
1079             }
1080 
1081 #if !LOG_NDEBUG
1082             AString out;
1083 #endif
1084 
1085             size_t dstOffset = 0;
1086             size_t seiIndex = 0;
1087             size_t shrunkBytes = 0;
1088             for (size_t i = 0; i < nals.size(); ++i) {
1089                 const NALPosition &pos = nals.itemAt(i);
1090 
1091                 unsigned nalType = mBuffer->data()[pos.nalOffset] & 0x1f;
1092 
1093                 if (nalType == 6 && pos.nalSize > 0) {
1094                     if (seiIndex >= sei->size() / sizeof(NALPosition)) {
1095                         ALOGE("Wrong seiIndex");
1096                         return NULL;
1097                     }
1098                     NALPosition &seiPos = ((NALPosition *)sei->data())[seiIndex++];
1099                     seiPos.nalOffset = dstOffset + 4;
1100                     seiPos.nalSize = pos.nalSize;
1101                 }
1102 
1103 #if !LOG_NDEBUG
1104                 char tmp[128];
1105                 sprintf(tmp, "0x%02x", nalType);
1106                 if (i > 0) {
1107                     out.append(", ");
1108                 }
1109                 out.append(tmp);
1110 #endif
1111 
1112                 memcpy(accessUnit->data() + dstOffset, "\x00\x00\x00\x01", 4);
1113 
1114                 if (mSampleDecryptor != NULL && (nalType == 1 || nalType == 5)) {
1115                     uint8_t *nalData = mBuffer->data() + pos.nalOffset;
1116                     size_t newSize = mSampleDecryptor->processNal(nalData, pos.nalSize);
1117                     // Note: the data can shrink due to unescaping
1118                     memcpy(accessUnit->data() + dstOffset + 4,
1119                             nalData,
1120                             newSize);
1121                     dstOffset += newSize + 4;
1122 
1123                     size_t thisShrunkBytes = pos.nalSize - newSize;
1124                     //ALOGV("dequeueAccessUnitH264[%d]: nalType: %d -> %zu (%zu)",
1125                     //        nalType, (int)pos.nalSize, newSize, thisShrunkBytes);
1126 
1127                     shrunkBytes += thisShrunkBytes;
1128                 }
1129                 else {
1130                     memcpy(accessUnit->data() + dstOffset + 4,
1131                             mBuffer->data() + pos.nalOffset,
1132                             pos.nalSize);
1133 
1134                     dstOffset += pos.nalSize + 4;
1135                     //ALOGV("dequeueAccessUnitH264 [%d] %d @%d",
1136                     //        nalType, (int)pos.nalSize, (int)pos.nalOffset);
1137                 }
1138             }
1139 
1140 #if !LOG_NDEBUG
1141             ALOGV("accessUnit contains nal types %s", out.c_str());
1142 #endif
1143 
1144             const NALPosition &pos = nals.itemAt(nals.size() - 1);
1145             size_t nextScan = pos.nalOffset + pos.nalSize;
1146 
1147             memmove(mBuffer->data(),
1148                     mBuffer->data() + nextScan,
1149                     mBuffer->size() - nextScan);
1150 
1151             mBuffer->setRange(0, mBuffer->size() - nextScan);
1152 
1153             int64_t timeUs = fetchTimestamp(nextScan);
1154             if (timeUs < 0ll) {
1155                 ALOGE("Negative timeUs");
1156                 return NULL;
1157             }
1158 
1159             accessUnit->meta()->setInt64("timeUs", timeUs);
1160             if (foundIDR) {
1161                 accessUnit->meta()->setInt32("isSync", 1);
1162             }
1163 
1164             if (mFormat == NULL) {
1165                 mFormat = MakeAVCCodecSpecificData(accessUnit);
1166             }
1167 
1168             if (mSampleDecryptor != NULL && shrunkBytes > 0) {
1169                 size_t adjustedSize = accessUnit->size() - shrunkBytes;
1170                 ALOGV("dequeueAccessUnitH264[%d]: AU size adjusted %zu -> %zu",
1171                         mAUIndex, accessUnit->size(), adjustedSize);
1172                 accessUnit->setRange(0, adjustedSize);
1173             }
1174 
1175             ALOGV("dequeueAccessUnitH264[%d]: AU %p(%zu) dstOffset:%zu, nals:%zu, totalSize:%zu ",
1176                     mAUIndex, accessUnit->data(), accessUnit->size(),
1177                     dstOffset, nals.size(), totalSize);
1178             mAUIndex++;
1179 
1180             return accessUnit;
1181         }
1182 
1183         NALPosition pos;
1184         pos.nalOffset = nalStart - mBuffer->data();
1185         pos.nalSize = nalSize;
1186 
1187         nals.push(pos);
1188 
1189         totalSize += nalSize;
1190     }
1191     if (err != (status_t)-EAGAIN) {
1192         ALOGE("Unexpeted err");
1193         return NULL;
1194     }
1195 
1196     return NULL;
1197 }
1198 
dequeueAccessUnitMPEGAudio()1199 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitMPEGAudio() {
1200     const uint8_t *data = mBuffer->data();
1201     size_t size = mBuffer->size();
1202 
1203     if (size < 4) {
1204         return NULL;
1205     }
1206 
1207     uint32_t header = U32_AT(data);
1208 
1209     size_t frameSize;
1210     int samplingRate, numChannels, bitrate, numSamples;
1211     if (!GetMPEGAudioFrameSize(
1212                 header, &frameSize, &samplingRate, &numChannels,
1213                 &bitrate, &numSamples)) {
1214         ALOGE("Failed to get audio frame size");
1215         return NULL;
1216     }
1217 
1218     if (size < frameSize) {
1219         return NULL;
1220     }
1221 
1222     unsigned layer = 4 - ((header >> 17) & 3);
1223 
1224     sp<ABuffer> accessUnit = new ABuffer(frameSize);
1225     memcpy(accessUnit->data(), data, frameSize);
1226 
1227     memmove(mBuffer->data(),
1228             mBuffer->data() + frameSize,
1229             mBuffer->size() - frameSize);
1230 
1231     mBuffer->setRange(0, mBuffer->size() - frameSize);
1232 
1233     int64_t timeUs = fetchTimestamp(frameSize);
1234     if (timeUs < 0ll) {
1235         ALOGE("Negative timeUs");
1236         return NULL;
1237     }
1238 
1239     accessUnit->meta()->setInt64("timeUs", timeUs);
1240     accessUnit->meta()->setInt32("isSync", 1);
1241 
1242     if (mFormat == NULL) {
1243         mFormat = new MetaData;
1244 
1245         switch (layer) {
1246             case 1:
1247                 mFormat->setCString(
1248                         kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_I);
1249                 break;
1250             case 2:
1251                 mFormat->setCString(
1252                         kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II);
1253                 break;
1254             case 3:
1255                 mFormat->setCString(
1256                         kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG);
1257                 break;
1258             default:
1259                 return NULL;
1260         }
1261 
1262         mFormat->setInt32(kKeySampleRate, samplingRate);
1263         mFormat->setInt32(kKeyChannelCount, numChannels);
1264     }
1265 
1266     return accessUnit;
1267 }
1268 
EncodeSize14(uint8_t ** _ptr,size_t size)1269 static void EncodeSize14(uint8_t **_ptr, size_t size) {
1270     if (size > 0x3fff) {
1271         ALOGE("Wrong size");
1272         return;
1273     }
1274 
1275     uint8_t *ptr = *_ptr;
1276 
1277     *ptr++ = 0x80 | (size >> 7);
1278     *ptr++ = size & 0x7f;
1279 
1280     *_ptr = ptr;
1281 }
1282 
MakeMPEGVideoESDS(const sp<ABuffer> & csd)1283 static sp<ABuffer> MakeMPEGVideoESDS(const sp<ABuffer> &csd) {
1284     sp<ABuffer> esds = new ABuffer(csd->size() + 25);
1285 
1286     uint8_t *ptr = esds->data();
1287     *ptr++ = 0x03;
1288     EncodeSize14(&ptr, 22 + csd->size());
1289 
1290     *ptr++ = 0x00;  // ES_ID
1291     *ptr++ = 0x00;
1292 
1293     *ptr++ = 0x00;  // streamDependenceFlag, URL_Flag, OCRstreamFlag
1294 
1295     *ptr++ = 0x04;
1296     EncodeSize14(&ptr, 16 + csd->size());
1297 
1298     *ptr++ = 0x40;  // Audio ISO/IEC 14496-3
1299 
1300     for (size_t i = 0; i < 12; ++i) {
1301         *ptr++ = 0x00;
1302     }
1303 
1304     *ptr++ = 0x05;
1305     EncodeSize14(&ptr, csd->size());
1306 
1307     memcpy(ptr, csd->data(), csd->size());
1308 
1309     return esds;
1310 }
1311 
dequeueAccessUnitMPEGVideo()1312 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitMPEGVideo() {
1313     if (isScrambled()) {
1314         if (mBuffer == NULL || mBuffer->size() == 0) {
1315             return NULL;
1316         }
1317         if (mFormat == NULL) {
1318             ALOGI("Creating dummy MPEG format for scrambled content");
1319             mFormat = new MetaData;
1320             mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG2);
1321             mFormat->setInt32(kKeyWidth, 1280);
1322             mFormat->setInt32(kKeyHeight, 720);
1323 
1324             // for MediaExtractor.CasInfo
1325             mFormat->setInt32(kKeyCASystemID, mCASystemId);
1326             mFormat->setData(kKeyCASessionID, 0,
1327                     mCasSessionId.data(), mCasSessionId.size());
1328         }
1329         return dequeueScrambledAccessUnit();
1330     }
1331 
1332     const uint8_t *data = mBuffer->data();
1333     size_t size = mBuffer->size();
1334 
1335     Vector<size_t> userDataPositions;
1336 
1337     bool sawPictureStart = false;
1338     int pprevStartCode = -1;
1339     int prevStartCode = -1;
1340     int currentStartCode = -1;
1341     bool gopFound = false;
1342     bool isClosedGop = false;
1343     bool brokenLink = false;
1344 
1345     size_t offset = 0;
1346     while (offset + 3 < size) {
1347         if (memcmp(&data[offset], "\x00\x00\x01", 3)) {
1348             ++offset;
1349             continue;
1350         }
1351 
1352         pprevStartCode = prevStartCode;
1353         prevStartCode = currentStartCode;
1354         currentStartCode = data[offset + 3];
1355 
1356         if (currentStartCode == 0xb3 && mFormat == NULL) {
1357             memmove(mBuffer->data(), mBuffer->data() + offset, size - offset);
1358             size -= offset;
1359             (void)fetchTimestamp(offset);
1360             offset = 0;
1361             mBuffer->setRange(0, size);
1362         }
1363 
1364         if ((prevStartCode == 0xb3 && currentStartCode != 0xb5)
1365                 || (pprevStartCode == 0xb3 && prevStartCode == 0xb5)) {
1366             // seqHeader without/with extension
1367 
1368             if (mFormat == NULL) {
1369                 if (size < 7u) {
1370                     ALOGE("Size too small");
1371                     return NULL;
1372                 }
1373 
1374                 unsigned width =
1375                     (data[4] << 4) | data[5] >> 4;
1376 
1377                 unsigned height =
1378                     ((data[5] & 0x0f) << 8) | data[6];
1379 
1380                 mFormat = new MetaData;
1381                 mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG2);
1382                 mFormat->setInt32(kKeyWidth, width);
1383                 mFormat->setInt32(kKeyHeight, height);
1384 
1385                 ALOGI("found MPEG2 video codec config (%d x %d)", width, height);
1386 
1387                 sp<ABuffer> csd = new ABuffer(offset);
1388                 memcpy(csd->data(), data, offset);
1389 
1390                 memmove(mBuffer->data(),
1391                         mBuffer->data() + offset,
1392                         mBuffer->size() - offset);
1393 
1394                 mBuffer->setRange(0, mBuffer->size() - offset);
1395                 size -= offset;
1396                 (void)fetchTimestamp(offset);
1397                 offset = 0;
1398 
1399                 // hexdump(csd->data(), csd->size());
1400 
1401                 sp<ABuffer> esds = MakeMPEGVideoESDS(csd);
1402                 mFormat->setData(
1403                         kKeyESDS, kTypeESDS, esds->data(), esds->size());
1404 
1405                 return NULL;
1406             }
1407         }
1408 
1409         if (mFormat != NULL && currentStartCode == 0xb8) {
1410             // GOP layer
1411             if (offset + 7 >= size) {
1412                 ALOGE("Size too small");
1413                 return NULL;
1414             }
1415             gopFound = true;
1416             isClosedGop = (data[offset + 7] & 0x40) != 0;
1417             brokenLink = (data[offset + 7] & 0x20) != 0;
1418         }
1419 
1420         if (mFormat != NULL && currentStartCode == 0xb2) {
1421             userDataPositions.add(offset);
1422         }
1423 
1424         if (mFormat != NULL && currentStartCode == 0x00) {
1425             // Picture start
1426 
1427             if (!sawPictureStart) {
1428                 sawPictureStart = true;
1429             } else {
1430                 sp<ABuffer> accessUnit = new ABuffer(offset);
1431                 memcpy(accessUnit->data(), data, offset);
1432 
1433                 memmove(mBuffer->data(),
1434                         mBuffer->data() + offset,
1435                         mBuffer->size() - offset);
1436 
1437                 mBuffer->setRange(0, mBuffer->size() - offset);
1438 
1439                 int64_t timeUs = fetchTimestamp(offset);
1440                 if (timeUs < 0ll) {
1441                     ALOGE("Negative timeUs");
1442                     return NULL;
1443                 }
1444 
1445                 offset = 0;
1446 
1447                 accessUnit->meta()->setInt64("timeUs", timeUs);
1448                 if (gopFound && (!brokenLink || isClosedGop)) {
1449                     accessUnit->meta()->setInt32("isSync", 1);
1450                 }
1451 
1452                 ALOGV("returning MPEG video access unit at time %" PRId64 " us",
1453                       timeUs);
1454 
1455                 // hexdump(accessUnit->data(), accessUnit->size());
1456 
1457                 if (userDataPositions.size() > 0) {
1458                     sp<ABuffer> mpegUserData =
1459                         new ABuffer(userDataPositions.size() * sizeof(size_t));
1460                     if (mpegUserData != NULL && mpegUserData->data() != NULL) {
1461                         for (size_t i = 0; i < userDataPositions.size(); ++i) {
1462                             memcpy(
1463                                     mpegUserData->data() + i * sizeof(size_t),
1464                                     &userDataPositions[i], sizeof(size_t));
1465                         }
1466                         accessUnit->meta()->setBuffer("mpegUserData", mpegUserData);
1467                     }
1468                 }
1469 
1470                 return accessUnit;
1471             }
1472         }
1473 
1474         ++offset;
1475     }
1476 
1477     return NULL;
1478 }
1479 
getNextChunkSize(const uint8_t * data,size_t size)1480 static ssize_t getNextChunkSize(
1481         const uint8_t *data, size_t size) {
1482     static const char kStartCode[] = "\x00\x00\x01";
1483 
1484     if (size < 3) {
1485         return -EAGAIN;
1486     }
1487 
1488     if (memcmp(kStartCode, data, 3)) {
1489         return -EAGAIN;
1490     }
1491 
1492     size_t offset = 3;
1493     while (offset + 2 < size) {
1494         if (!memcmp(&data[offset], kStartCode, 3)) {
1495             return offset;
1496         }
1497 
1498         ++offset;
1499     }
1500 
1501     return -EAGAIN;
1502 }
1503 
dequeueAccessUnitMPEG4Video()1504 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitMPEG4Video() {
1505     uint8_t *data = mBuffer->data();
1506     size_t size = mBuffer->size();
1507 
1508     enum {
1509         SKIP_TO_VISUAL_OBJECT_SEQ_START,
1510         EXPECT_VISUAL_OBJECT_START,
1511         EXPECT_VO_START,
1512         EXPECT_VOL_START,
1513         WAIT_FOR_VOP_START,
1514         SKIP_TO_VOP_START,
1515 
1516     } state;
1517 
1518     if (mFormat == NULL) {
1519         state = SKIP_TO_VISUAL_OBJECT_SEQ_START;
1520     } else {
1521         state = SKIP_TO_VOP_START;
1522     }
1523 
1524     int32_t width = -1, height = -1;
1525 
1526     size_t offset = 0;
1527     ssize_t chunkSize;
1528     while ((chunkSize = getNextChunkSize(
1529                     &data[offset], size - offset)) > 0) {
1530         bool discard = false;
1531 
1532         unsigned chunkType = data[offset + 3];
1533 
1534         switch (state) {
1535             case SKIP_TO_VISUAL_OBJECT_SEQ_START:
1536             {
1537                 if (chunkType == 0xb0) {
1538                     // Discard anything before this marker.
1539 
1540                     state = EXPECT_VISUAL_OBJECT_START;
1541                 } else {
1542                     discard = true;
1543                 }
1544                 break;
1545             }
1546 
1547             case EXPECT_VISUAL_OBJECT_START:
1548             {
1549                 if (chunkType != 0xb5) {
1550                     ALOGE("Unexpected chunkType");
1551                     return NULL;
1552                 }
1553                 state = EXPECT_VO_START;
1554                 break;
1555             }
1556 
1557             case EXPECT_VO_START:
1558             {
1559                 if (chunkType > 0x1f) {
1560                     ALOGE("Unexpected chunkType");
1561                     return NULL;
1562                 }
1563                 state = EXPECT_VOL_START;
1564                 break;
1565             }
1566 
1567             case EXPECT_VOL_START:
1568             {
1569                 if ((chunkType & 0xf0) != 0x20) {
1570                     ALOGE("Wrong chunkType");
1571                     return NULL;
1572                 }
1573 
1574                 if (!ExtractDimensionsFromVOLHeader(
1575                             &data[offset], chunkSize,
1576                             &width, &height)) {
1577                     ALOGE("Failed to get dimension");
1578                     return NULL;
1579                 }
1580 
1581                 state = WAIT_FOR_VOP_START;
1582                 break;
1583             }
1584 
1585             case WAIT_FOR_VOP_START:
1586             {
1587                 if (chunkType == 0xb3 || chunkType == 0xb6) {
1588                     // group of VOP or VOP start.
1589 
1590                     mFormat = new MetaData;
1591                     mFormat->setCString(
1592                             kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
1593 
1594                     mFormat->setInt32(kKeyWidth, width);
1595                     mFormat->setInt32(kKeyHeight, height);
1596 
1597                     ALOGI("found MPEG4 video codec config (%d x %d)",
1598                          width, height);
1599 
1600                     sp<ABuffer> csd = new ABuffer(offset);
1601                     memcpy(csd->data(), data, offset);
1602 
1603                     // hexdump(csd->data(), csd->size());
1604 
1605                     sp<ABuffer> esds = MakeMPEGVideoESDS(csd);
1606                     mFormat->setData(
1607                             kKeyESDS, kTypeESDS,
1608                             esds->data(), esds->size());
1609 
1610                     discard = true;
1611                     state = SKIP_TO_VOP_START;
1612                 }
1613 
1614                 break;
1615             }
1616 
1617             case SKIP_TO_VOP_START:
1618             {
1619                 if (chunkType == 0xb6) {
1620                     int vopCodingType = (data[offset + 4] & 0xc0) >> 6;
1621 
1622                     offset += chunkSize;
1623 
1624                     sp<ABuffer> accessUnit = new ABuffer(offset);
1625                     memcpy(accessUnit->data(), data, offset);
1626 
1627                     memmove(data, &data[offset], size - offset);
1628                     size -= offset;
1629                     mBuffer->setRange(0, size);
1630 
1631                     int64_t timeUs = fetchTimestamp(offset);
1632                     if (timeUs < 0ll) {
1633                         ALOGE("Negative timeus");
1634                         return NULL;
1635                     }
1636 
1637                     offset = 0;
1638 
1639                     accessUnit->meta()->setInt64("timeUs", timeUs);
1640                     if (vopCodingType == 0) {  // intra-coded VOP
1641                         accessUnit->meta()->setInt32("isSync", 1);
1642                     }
1643 
1644                     ALOGV("returning MPEG4 video access unit at time %" PRId64 " us",
1645                          timeUs);
1646 
1647                     // hexdump(accessUnit->data(), accessUnit->size());
1648 
1649                     return accessUnit;
1650                 } else if (chunkType != 0xb3) {
1651                     offset += chunkSize;
1652                     discard = true;
1653                 }
1654 
1655                 break;
1656             }
1657 
1658             default:
1659                 ALOGE("Unknown state: %d", state);
1660                 return NULL;
1661         }
1662 
1663         if (discard) {
1664             (void)fetchTimestamp(offset);
1665             memmove(data, &data[offset], size - offset);
1666             size -= offset;
1667             offset = 0;
1668             mBuffer->setRange(0, size);
1669         } else {
1670             offset += chunkSize;
1671         }
1672     }
1673 
1674     return NULL;
1675 }
1676 
signalEOS()1677 void ElementaryStreamQueue::signalEOS() {
1678     if (!mEOSReached) {
1679         if (mMode == MPEG_VIDEO) {
1680             const char *theEnd = "\x00\x00\x01\x00";
1681             appendData(theEnd, 4, 0);
1682         }
1683         mEOSReached = true;
1684     } else {
1685         ALOGW("EOS already signaled");
1686     }
1687 }
1688 
dequeueAccessUnitMetadata()1689 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitMetadata() {
1690     size_t size = mBuffer->size();
1691     if (!size) {
1692         return NULL;
1693     }
1694 
1695     sp<ABuffer> accessUnit = new ABuffer(size);
1696     int64_t timeUs = fetchTimestamp(size);
1697     accessUnit->meta()->setInt64("timeUs", timeUs);
1698 
1699     memcpy(accessUnit->data(), mBuffer->data(), size);
1700     mBuffer->setRange(0, 0);
1701 
1702     if (mFormat == NULL) {
1703         mFormat = new MetaData;
1704         mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_DATA_TIMED_ID3);
1705     }
1706 
1707     return accessUnit;
1708 }
1709 
signalNewSampleAesKey(const sp<AMessage> & keyItem)1710 void ElementaryStreamQueue::signalNewSampleAesKey(const sp<AMessage> &keyItem) {
1711     if (mSampleDecryptor == NULL) {
1712         ALOGE("signalNewSampleAesKey: Stream %x is not encrypted; keyItem: %p",
1713                 mMode, keyItem.get());
1714         return;
1715     }
1716 
1717     mSampleDecryptor->signalNewSampleAesKey(keyItem);
1718 }
1719 
1720 
1721 }  // namespace android
1722