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/foundation/ByteUtils.h>
28 #include <media/stagefright/foundation/avc_utils.h>
29 #include <media/stagefright/MediaErrors.h>
30 #include <media/stagefright/MediaDefs.h>
31 #include <media/stagefright/MetaData.h>
32 #include <media/stagefright/MetaDataUtils.h>
33 #include <media/cas/DescramblerAPI.h>
34 #include <media/hardware/CryptoAPI.h>
35
36 #include <inttypes.h>
37 #include <netinet/in.h>
38
39 #ifdef ENABLE_CRYPTO
40 #include "HlsSampleDecryptor.h"
41 #endif
42
43 namespace android {
44
ElementaryStreamQueue(Mode mode,uint32_t flags)45 ElementaryStreamQueue::ElementaryStreamQueue(Mode mode, uint32_t flags)
46 : mMode(mode),
47 mFlags(flags),
48 mEOSReached(false),
49 mCASystemId(0),
50 mAUIndex(0) {
51
52 ALOGV("ElementaryStreamQueue(%p) mode %x flags %x isScrambled %d isSampleEncrypted %d",
53 this, mode, flags, isScrambled(), isSampleEncrypted());
54
55 // Create the decryptor anyway since we don't know the use-case unless key is provided
56 // Won't decrypt if key info not available (e.g., scanner/extractor just parsing ts files)
57 mSampleDecryptor = isSampleEncrypted() ?
58 #ifdef ENABLE_CRYPTO
59 new HlsSampleDecryptor
60 #else
61 new SampleDecryptor
62 #endif
63 : NULL;
64 }
65
getFormat()66 sp<MetaData> ElementaryStreamQueue::getFormat() {
67 return mFormat;
68 }
69
clear(bool clearFormat)70 void ElementaryStreamQueue::clear(bool clearFormat) {
71 if (mBuffer != NULL) {
72 mBuffer->setRange(0, 0);
73 }
74
75 mRangeInfos.clear();
76
77 if (mScrambledBuffer != NULL) {
78 mScrambledBuffer->setRange(0, 0);
79 }
80 mScrambledRangeInfos.clear();
81
82 if (clearFormat) {
83 mFormat.clear();
84 }
85
86 mEOSReached = false;
87 }
88
isScrambled() const89 bool ElementaryStreamQueue::isScrambled() const {
90 return (mFlags & kFlag_ScrambledData) != 0;
91 }
92
setCasInfo(int32_t systemId,const std::vector<uint8_t> & sessionId)93 void ElementaryStreamQueue::setCasInfo(
94 int32_t systemId, const std::vector<uint8_t> &sessionId) {
95 mCASystemId = systemId;
96 mCasSessionId = sessionId;
97 }
98
readVariableBits(ABitReader & bits,int32_t nbits)99 static int32_t readVariableBits(ABitReader &bits, int32_t nbits) {
100 int32_t value = 0;
101 int32_t more_bits = 1;
102
103 while (more_bits) {
104 value += bits.getBits(nbits);
105 more_bits = bits.getBits(1);
106 if (!more_bits)
107 break;
108 value++;
109 value <<= nbits;
110 }
111 return value;
112 }
113
114 // Parse AC3 header assuming the current ptr is start position of syncframe,
115 // update metadata only applicable, and return the payload size
parseAC3SyncFrame(const uint8_t * ptr,size_t size,sp<MetaData> * metaData)116 static unsigned parseAC3SyncFrame(
117 const uint8_t *ptr, size_t size, sp<MetaData> *metaData) {
118 static const unsigned channelCountTable[] = {2, 1, 2, 3, 3, 4, 4, 5};
119 static const unsigned samplingRateTable[] = {48000, 44100, 32000};
120
121 static const unsigned frameSizeTable[19][3] = {
122 { 64, 69, 96 },
123 { 80, 87, 120 },
124 { 96, 104, 144 },
125 { 112, 121, 168 },
126 { 128, 139, 192 },
127 { 160, 174, 240 },
128 { 192, 208, 288 },
129 { 224, 243, 336 },
130 { 256, 278, 384 },
131 { 320, 348, 480 },
132 { 384, 417, 576 },
133 { 448, 487, 672 },
134 { 512, 557, 768 },
135 { 640, 696, 960 },
136 { 768, 835, 1152 },
137 { 896, 975, 1344 },
138 { 1024, 1114, 1536 },
139 { 1152, 1253, 1728 },
140 { 1280, 1393, 1920 },
141 };
142
143 ABitReader bits(ptr, size);
144 if (bits.numBitsLeft() < 16) {
145 return 0;
146 }
147 if (bits.getBits(16) != 0x0B77) {
148 return 0;
149 }
150
151 if (bits.numBitsLeft() < 16 + 2 + 6 + 5 + 3 + 3) {
152 ALOGV("Not enough bits left for further parsing");
153 return 0;
154 }
155 bits.skipBits(16); // crc1
156
157 unsigned fscod = bits.getBits(2);
158 if (fscod == 3) {
159 ALOGW("Incorrect fscod in AC3 header");
160 return 0;
161 }
162
163 unsigned frmsizecod = bits.getBits(6);
164 if (frmsizecod > 37) {
165 ALOGW("Incorrect frmsizecod in AC3 header");
166 return 0;
167 }
168
169 unsigned bsid = bits.getBits(5);
170 if (bsid > 8) {
171 ALOGW("Incorrect bsid in AC3 header. Possibly E-AC-3?");
172 return 0;
173 }
174
175 bits.skipBits(3); // bsmod
176 unsigned acmod = bits.getBits(3);
177
178 if ((acmod & 1) > 0 && acmod != 1) {
179 if (bits.numBitsLeft() < 2) {
180 return 0;
181 }
182 bits.skipBits(2); //cmixlev
183 }
184 if ((acmod & 4) > 0) {
185 if (bits.numBitsLeft() < 2) {
186 return 0;
187 }
188 bits.skipBits(2); //surmixlev
189 }
190 if (acmod == 2) {
191 if (bits.numBitsLeft() < 2) {
192 return 0;
193 }
194 bits.skipBits(2); //dsurmod
195 }
196
197 if (bits.numBitsLeft() < 1) {
198 return 0;
199 }
200 unsigned lfeon = bits.getBits(1);
201
202 unsigned samplingRate = samplingRateTable[fscod];
203 unsigned payloadSize = frameSizeTable[frmsizecod >> 1][fscod];
204 if (fscod == 1) {
205 payloadSize += frmsizecod & 1;
206 }
207 payloadSize <<= 1; // convert from 16-bit words to bytes
208
209 unsigned channelCount = channelCountTable[acmod] + lfeon;
210
211 if (metaData != NULL) {
212 (*metaData)->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AC3);
213 (*metaData)->setInt32(kKeyChannelCount, channelCount);
214 (*metaData)->setInt32(kKeySampleRate, samplingRate);
215 }
216
217 return payloadSize;
218 }
219
220 // Parse EAC3 header assuming the current ptr is start position of syncframe,
221 // update metadata only applicable, and return the payload size
222 // ATSC A/52:2012 E2.3.1
parseEAC3SyncFrame(const uint8_t * ptr,size_t size,sp<MetaData> * metaData)223 static unsigned parseEAC3SyncFrame(
224 const uint8_t *ptr, size_t size, sp<MetaData> *metaData) {
225 static const unsigned channelCountTable[] = {2, 1, 2, 3, 3, 4, 4, 5};
226 static const unsigned samplingRateTable[] = {48000, 44100, 32000};
227 static const unsigned samplingRateTable2[] = {24000, 22050, 16000};
228
229 ABitReader bits(ptr, size);
230 if (bits.numBitsLeft() < 16) {
231 ALOGE("Not enough bits left for further parsing");
232 return 0;
233 }
234 if (bits.getBits(16) != 0x0B77) {
235 ALOGE("No valid sync word in EAC3 header");
236 return 0;
237 }
238
239 // we parse up to bsid so there needs to be at least that many bits
240 if (bits.numBitsLeft() < 2 + 3 + 11 + 2 + 2 + 3 + 1 + 5) {
241 ALOGE("Not enough bits left for further parsing");
242 return 0;
243 }
244
245 unsigned strmtyp = bits.getBits(2);
246 if (strmtyp == 3) {
247 ALOGE("Incorrect strmtyp in EAC3 header");
248 return 0;
249 }
250
251 unsigned substreamid = bits.getBits(3);
252 // only the first independent stream is supported
253 if ((strmtyp == 0 || strmtyp == 2) && substreamid != 0)
254 return 0;
255
256 unsigned frmsiz = bits.getBits(11);
257 unsigned fscod = bits.getBits(2);
258
259 unsigned samplingRate = 0;
260 if (fscod == 0x3) {
261 unsigned fscod2 = bits.getBits(2);
262 if (fscod2 == 3) {
263 ALOGW("Incorrect fscod2 in EAC3 header");
264 return 0;
265 }
266 samplingRate = samplingRateTable2[fscod2];
267 } else {
268 samplingRate = samplingRateTable[fscod];
269 bits.skipBits(2); // numblkscod
270 }
271
272 unsigned acmod = bits.getBits(3);
273 unsigned lfeon = bits.getBits(1);
274 unsigned bsid = bits.getBits(5);
275 if (bsid < 11 || bsid > 16) {
276 ALOGW("Incorrect bsid in EAC3 header. Could be AC-3 or some unknown EAC3 format");
277 return 0;
278 }
279
280 // we currently only support the first independant stream
281 if (metaData != NULL && (strmtyp == 0 || strmtyp == 2)) {
282 unsigned channelCount = channelCountTable[acmod] + lfeon;
283 ALOGV("EAC3 channelCount = %d", channelCount);
284 ALOGV("EAC3 samplingRate = %d", samplingRate);
285 (*metaData)->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_EAC3);
286 (*metaData)->setInt32(kKeyChannelCount, channelCount);
287 (*metaData)->setInt32(kKeySampleRate, samplingRate);
288 (*metaData)->setInt32(kKeyIsSyncFrame, 1);
289 }
290
291 unsigned payloadSize = frmsiz + 1;
292 payloadSize <<= 1; // convert from 16-bit words to bytes
293
294 return payloadSize;
295 }
296
297 // Parse AC4 header assuming the current ptr is start position of syncframe
298 // and update frameSize and metadata.
parseAC4SyncFrame(const uint8_t * ptr,size_t size,unsigned & frameSize,sp<MetaData> * metaData)299 static status_t parseAC4SyncFrame(
300 const uint8_t *ptr, size_t size, unsigned &frameSize, sp<MetaData> *metaData) {
301 // ETSI TS 103 190-2 V1.1.1 (2015-09), Annex C
302 // The sync_word can be either 0xAC40 or 0xAC41.
303 static const int kSyncWordAC40 = 0xAC40;
304 static const int kSyncWordAC41 = 0xAC41;
305
306 size_t headerSize = 0;
307 ABitReader bits(ptr, size);
308 int32_t syncWord = bits.getBits(16);
309 if ((syncWord != kSyncWordAC40) && (syncWord != kSyncWordAC41)) {
310 ALOGE("Invalid syncword in AC4 header");
311 return ERROR_MALFORMED;
312 }
313 headerSize += 2;
314
315 frameSize = bits.getBits(16);
316 headerSize += 2;
317 if (frameSize == 0xFFFF) {
318 frameSize = bits.getBits(24);
319 headerSize += 3;
320 }
321
322 if (frameSize == 0) {
323 ALOGE("Invalid frame size in AC4 header");
324 return ERROR_MALFORMED;
325 }
326 frameSize += headerSize;
327 // If the sync_word is 0xAC41, a crc_word is also transmitted.
328 if (syncWord == kSyncWordAC41) {
329 frameSize += 2; // crc_word
330 }
331 ALOGV("AC4 frameSize = %u", frameSize);
332
333 // ETSI TS 103 190-2 V1.1.1 6.2.1.1
334 uint32_t bitstreamVersion = bits.getBits(2);
335 if (bitstreamVersion == 3) {
336 bitstreamVersion += readVariableBits(bits, 2);
337 }
338
339 bits.skipBits(10); // Sequence Counter
340
341 uint32_t bWaitFrames = bits.getBits(1);
342 if (bWaitFrames) {
343 uint32_t waitFrames = bits.getBits(3);
344 if (waitFrames > 0) {
345 bits.skipBits(2); // br_code;
346 }
347 }
348
349 // ETSI TS 103 190 V1.1.1 Table 82
350 bool fsIndex = bits.getBits(1);
351 uint32_t samplingRate = fsIndex ? 48000 : 44100;
352
353 if (metaData != NULL) {
354 ALOGV("dequeueAccessUnitAC4 Setting mFormat");
355 (*metaData)->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AC4);
356 (*metaData)->setInt32(kKeyIsSyncFrame, 1);
357 // [FIXME] AC4 channel count is defined per presentation. Provide a default channel count
358 // as stereo for the entire stream.
359 (*metaData)->setInt32(kKeyChannelCount, 2);
360 (*metaData)->setInt32(kKeySampleRate, samplingRate);
361 }
362 return OK;
363 }
364
365 #define RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bitstream, size) \
366 do { \
367 if ((bitstream).numBitsLeft() < (size)) { \
368 ALOGE("Not enough bits left for further parsing"); \
369 return ERROR_MALFORMED; } \
370 } while (0)
371
372 // Parse DTS Digital Surround and DTS Express(LBR) stream header
parseDTSHDSyncFrame(const uint8_t * ptr,size_t size,unsigned & frameSize,sp<MetaData> * metaData)373 static status_t parseDTSHDSyncFrame(
374 const uint8_t *ptr, size_t size, unsigned &frameSize, sp<MetaData> *metaData) {
375 static const unsigned channelCountTable[] = {1, 2, 2, 2, 2, 3, 3, 4,
376 4, 5, 6, 6, 6, 7, 8, 8};
377 static const unsigned samplingRateTableCoreSS[] = {0, 8000, 16000, 32000, 0, 0, 11025, 22050,
378 44100, 0, 0, 12000, 24000, 48000, 0, 0};
379 static const unsigned samplingRateTableExtSS[] = {8000, 16000, 32000, 64000, 128000,
380 22050, 44100, 88200, 176400, 352800,
381 12000, 24000, 48000, 96000, 192000, 384000};
382
383 const uint32_t DTSHD_SYNC_CORE_16BIT_BE = 0x7ffe8001;
384 const uint32_t DTSHD_SYNC_EXSS_16BIT_BE = 0x64582025;
385
386 uint32_t numChannels = 0, samplingRate = 0;
387 bool isLBR = false;
388
389 ABitReader bits(ptr, size);
390
391 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, 32);
392 uint32_t dtshdSyncWord = bits.getBits(32);
393
394 // Expecting DTS Digital Surround or DTS Express(LBR) streams only
395 if (dtshdSyncWord == DTSHD_SYNC_CORE_16BIT_BE) { // DTS Digital Surround Header
396 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, (1 + 5 + 1 + 7 + 14 + 6 + 4 + 15 + 2));
397
398 // FTYPE, SHORT, CRC, NBLKS
399 bits.skipBits(1 + 5 + 1 + 7);
400
401 frameSize = bits.getBits(14) + 1;
402 uint32_t amode = bits.getBits(6);
403 uint32_t freqIndex = bits.getBits(4);
404
405 // RATE, FIXEDBIT, DYNF, TIMEF, AUXF, HDCD, EXT_AUDIO_ID, EXT_AUDIO, ASPF
406 bits.skipBits(5 + 1 + 1 + 1 + 1 + 1 + 3 + 1 + 1);
407
408 uint32_t lfeFlag = bits.getBits(2);
409 numChannels = (amode <= 15) ? channelCountTable[amode] : 0;
410 numChannels += ((lfeFlag == 1) || (lfeFlag == 2)) ? 1 : 0;
411 samplingRate = (freqIndex <= 15) ? samplingRateTableCoreSS[freqIndex] : 0;
412
413 isLBR = false;
414 } else if (dtshdSyncWord == DTSHD_SYNC_EXSS_16BIT_BE) { // DTS Express(LBR) Header
415 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, (8 + 2 + 1));
416
417 uint32_t extHeadersize, extSSFsize;
418 uint32_t numAudioPresent = 1, numAssets = 1;
419 uint32_t nuActiveExSSMask[8];
420
421 // userDefinedBits
422 bits.skipBits(8);
423
424 uint32_t extSSIndex = bits.getBits(2);
425 uint32_t headerSizeType = bits.getBits(1);
426
427 if (headerSizeType == 0) {
428 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, (8 + 16));
429
430 extHeadersize = bits.getBits(8) + 1;
431 extSSFsize = bits.getBits(16) + 1;
432 } else {
433 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, (12 + 20));
434
435 extHeadersize = bits.getBits(12) + 1;
436 extSSFsize = bits.getBits(20) + 1;
437 }
438
439 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, (1));
440
441 uint32_t staticFieldsPresent = bits.getBits(1);
442
443 if (staticFieldsPresent) {
444 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, (2 + 3 + 1));
445
446 // nuRefClockCode, nuExSSFrameDurationCode
447 bits.skipBits(2 + 3);
448
449 if (bits.getBits(1)) {
450 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, (32 + 4));
451
452 bits.skipBits(32 + 4);
453 }
454
455 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, (3 + 3));
456
457 // numAudioPresent, numAssets
458 bits.skipBits(3 + 3);
459
460 for (uint32_t nAuPr = 0; nAuPr < numAudioPresent; nAuPr++) {
461 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, (extSSIndex + 1));
462
463 nuActiveExSSMask[nAuPr] = bits.getBits(extSSIndex + 1);
464 }
465
466 for (uint32_t nAuPr = 0; nAuPr < numAudioPresent; nAuPr++) {
467 for (uint32_t nSS = 0; nSS < extSSIndex + 1; nSS++) {
468 if (((nuActiveExSSMask[nAuPr] >> nSS) & 0x1) == 1) {
469 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, 8);
470
471 // nuActiveAssetMask
472 bits.skipBits(8);
473 }
474 }
475 }
476
477 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, 1);
478
479 // bMixMetadataEnbl
480 if (bits.getBits(1)) {
481 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, (2 + 2 + 2));
482
483 // nuMixMetadataAdjLevel
484 bits.skipBits(2);
485
486 uint32_t bits4MixOutMask = (bits.getBits(2) + 1) << 2;
487 uint32_t numMixOutConfigs = bits.getBits(2) + 1;
488
489 for (int ns = 0; ns < numMixOutConfigs; ns++) {
490 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, bits4MixOutMask);
491
492 // nuMixOutChMask
493 bits.skipBits(bits4MixOutMask);
494 }
495 }
496 }
497
498 for (int nAst = 0; nAst < numAssets; nAst++) {
499 int bits4ExSSFsize = (headerSizeType == 0) ? 16 : 20;
500
501 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, bits4ExSSFsize);
502
503 bits.skipBits(bits4ExSSFsize);
504 }
505
506 /* Asset descriptor */
507 for (int nAst = 0; nAst < numAssets; nAst++) {
508 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, (9 + 3));
509
510 // nuAssetDescriptFsize, nuAssetIndex
511 bits.skipBits(9 + 3);
512
513 if (staticFieldsPresent) {
514 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, 1);
515
516 // bAssetTypeDescrPresent
517 if (bits.getBits(1)) {
518 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, 4);
519
520 // nuAssetTypeDescriptor
521 bits.skipBits(4);
522 }
523
524 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, 1);
525
526 // bLanguageDescrPresent
527 if (bits.getBits(1)) {
528 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, 24);
529
530 // LanguageDescriptor
531 bits.skipBits(24);
532 }
533
534 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, 1);
535
536 // bInfoTextPresent
537 if (bits.getBits(1)) {
538 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, 10);
539
540 uint32_t nuInfoTextByteSize = bits.getBits(10) + 1;
541
542 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, (nuInfoTextByteSize * 8));
543
544 // InfoTextString
545 bits.skipBits(nuInfoTextByteSize * 8);
546 }
547
548 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, (5 + 4 + 8));
549
550 // nuBitResolution
551 bits.skipBits(5);
552
553 samplingRate = samplingRateTableExtSS[bits.getBits(4)];
554 numChannels = bits.getBits(8) + 1;
555 }
556 }
557
558 frameSize = extHeadersize + extSSFsize;
559 isLBR = true;
560 } else {
561 ALOGE("No valid sync word in DTS/DTSHD header");
562 return ERROR_MALFORMED;
563 }
564
565 if (metaData != NULL) {
566 if (isLBR) {
567 (*metaData)->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_DTS_HD);
568 (*metaData)->setInt32(kKeyAudioProfile, 0x2); // CodecProfileLevel.DTS_HDProfileLBR
569 } else {
570 (*metaData)->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_DTS);
571 }
572 (*metaData)->setInt32(kKeyChannelCount, numChannels);
573 (*metaData)->setInt32(kKeySampleRate, samplingRate);
574 }
575 return OK;
576 }
577
extractVarLenBitFields(ABitReader * bits,size_t * bitsUsed,uint32_t * value,unsigned ucTable[],bool extractAndAddFlag)578 static status_t extractVarLenBitFields(
579 ABitReader *bits, size_t *bitsUsed, uint32_t *value,
580 unsigned ucTable[], bool extractAndAddFlag) {
581
582 static const unsigned bitsUsedTbl[8] = {1, 1, 1, 1, 2, 2, 3, 3}; // prefix code lengths
583 static const unsigned indexTbl[8] = {0, 0, 0, 0, 1, 1, 2, 3}; // code to prefix code index map
584
585 /* Clone the bitstream */
586 ABitReader bitStream(bits->data(), bits->numBitsLeft() / 8);
587 ABitReader bitstreamClone(bits->data(), bits->numBitsLeft() / 8);
588
589 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bitstreamClone, 3);
590
591 unsigned code = bitstreamClone.getBits(3);
592 unsigned totalBitsUsed = bitsUsedTbl[code];
593 unsigned unIndex = indexTbl[code];
594
595 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bitStream, totalBitsUsed);
596
597 bitStream.skipBits(totalBitsUsed);
598
599 uint32_t unValue = 0;
600 if (ucTable[unIndex] > 0) {
601 if (extractAndAddFlag) {
602 for (unsigned un = 0; un < unIndex; un++) {
603 unValue += (1 << ucTable[un]);
604 }
605
606 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bitStream, ucTable[unIndex]);
607
608 unValue += bitStream.getBits(ucTable[unIndex]);
609 totalBitsUsed += ucTable[unIndex];
610 } else {
611 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bitStream, ucTable[unIndex]);
612
613 unValue += bitStream.getBits(ucTable[unIndex]);
614 totalBitsUsed += ucTable[unIndex];
615 }
616 }
617
618 *bitsUsed = (size_t)totalBitsUsed;
619 *value = unValue;
620 return OK;
621 }
622
623 // Parse DTS UHD Profile-2 stream header
parseDTSUHDSyncFrame(const uint8_t * ptr,size_t size,unsigned & frameSize,sp<MetaData> * metaData)624 static status_t parseDTSUHDSyncFrame(
625 const uint8_t *ptr, size_t size, unsigned &frameSize, sp<MetaData> *metaData) {
626
627 static const uint32_t DTSUHD_SYNC_CORE_16BIT_BE = 0x40411BF2;
628 static const uint32_t DTSUHD_NONSYNC_CORE_16BIT_BE = 0x71C442E8;
629
630 unsigned audioSamplRate = 0;
631
632 ABitReader bits(ptr, size);
633
634 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, 32);
635
636 uint32_t syncWord = bits.getBits(32);
637
638 bool isSyncFrameFlag = false;
639 switch (syncWord) {
640 case DTSUHD_SYNC_CORE_16BIT_BE:
641 isSyncFrameFlag = true;
642 break;
643 case DTSUHD_NONSYNC_CORE_16BIT_BE:
644 isSyncFrameFlag = false;
645 break;
646 default:
647 ALOGE("No valid sync word in DTSUHD header");
648 return ERROR_MALFORMED; // invalid sync word
649 }
650
651 unsigned uctable1[4] = { 5, 8, 10, 12 };
652 uint32_t sizeOfFTOCPayload = 0;
653 size_t nuBitsUsed = 0;
654 status_t status = OK;
655
656 status = extractVarLenBitFields(&bits, &nuBitsUsed, &sizeOfFTOCPayload, uctable1, true);
657
658 if (status != OK) {
659 ALOGE("Failed to extractVarLenBitFields from DTSUHD header");
660 return ERROR_MALFORMED;
661 }
662
663 bits.skipBits(nuBitsUsed);
664
665 if (isSyncFrameFlag) {
666 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, (1 + 2 + 3 + 2 + 1));
667
668 // FullChannelBasedMixFlag, ETSI TS 103 491 V1.2.1, Section 6.4.6.1
669 if (!(bits.getBits(1))) {
670 // This implementation only supports full channel mask-based
671 // audio presentation (i.e. 2.0, 5.1, 11.1 mix without objects)
672 ALOGE("Objects not supported, only DTSUHD full channel mask-based mix");
673 return ERROR_MALFORMED;
674 }
675
676 // BaseDuration, FrameDuration
677 bits.skipBits(2 + 3);
678
679 unsigned clockRateIndex = bits.getBits(2);
680 unsigned clockRateHertz = 0;
681
682 switch (clockRateIndex) {
683 case 0:
684 clockRateHertz = 32000;
685 break;
686 case 1:
687 clockRateHertz = 44100;
688 break;
689 case 2:
690 clockRateHertz = 48000;
691 break;
692 default:
693 ALOGE("Invalid clockRateIndex in DTSUHD header");
694 return ERROR_MALFORMED;
695 }
696
697 if (bits.getBits(1)) {
698 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, (32 + 4));
699
700 bits.skipBits(32 + 4);
701 }
702
703 RETURN_ERROR_IF_NOT_ENOUGH_BYTES_LEFT(bits, 2);
704
705 unsigned samplRateMultiplier = (1 << bits.getBits(2));
706 audioSamplRate = clockRateHertz * samplRateMultiplier;
707 }
708
709 uint32_t chunkPayloadBytes = 0;
710 int numOfMDChunks = isSyncFrameFlag ? 1 : 0; // Metadata chunks
711 for (int nmdc = 0; nmdc < numOfMDChunks; nmdc++) {
712 unsigned uctable2[4] = {6, 9, 12, 15};
713 uint32_t nuMDChunkSize = 0;
714 nuBitsUsed = 0;
715
716 status = extractVarLenBitFields(&bits, &nuBitsUsed, &nuMDChunkSize, uctable2, true);
717 if (status != OK) {
718 ALOGE("Failed to extractVarLenBitFields from DTSUHD header");
719 return ERROR_MALFORMED;
720 }
721
722 bits.skipBits(nuBitsUsed);
723
724 if (nuMDChunkSize > 32767) {
725 ALOGE("Unsupported number of metadata chunks in DTSUHD header");
726 return ERROR_MALFORMED;
727 }
728 chunkPayloadBytes += nuMDChunkSize;
729 }
730
731 // Ony one audio chunk is supported
732 int numAudioChunks = 1;
733 for (int nac = 0; nac < numAudioChunks; nac++) {
734 uint32_t acID = 256, nuAudioChunkSize = 0;
735
736 // isSyncFrameFlag means that ACID is present
737 if (isSyncFrameFlag) {
738 unsigned uctable3[4] = {2, 4, 6, 8};
739 nuBitsUsed = 0;
740
741 status = extractVarLenBitFields(&bits, &nuBitsUsed, &acID, uctable3, true);
742
743 if (status != OK) {
744 ALOGE("Failed to extractVarLenBitFields from DTSUHD header");
745 return ERROR_MALFORMED;
746 }
747
748 bits.skipBits(nuBitsUsed);
749 }
750
751 nuBitsUsed = 0;
752 if (acID == 0) {
753 nuAudioChunkSize = 0;
754 } else {
755 unsigned uctable4[4] = {9, 11, 13, 16};
756
757 status = extractVarLenBitFields(&bits, &nuBitsUsed, &nuAudioChunkSize, uctable4, true);
758
759 if (status != OK) {
760 ALOGE("Failed to extractVarLenBitFields from DTSUHD header");
761 return ERROR_MALFORMED;
762 }
763 }
764
765 if (nuAudioChunkSize > 65535){
766 ALOGE("Unsupported number of audio chunks in DTSUHD header");
767 return ERROR_MALFORMED;
768 }
769
770 chunkPayloadBytes += nuAudioChunkSize;
771 }
772
773 frameSize = (sizeOfFTOCPayload + 1) + chunkPayloadBytes;
774
775 if (metaData != NULL) {
776 (*metaData)->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_DTS_UHD);
777 (*metaData)->setInt32(kKeyAudioProfile, 0x2); // CodecProfileLevel.DTS_UHDProfileP2
778 (*metaData)->setInt32(kKeyChannelCount, 2); // Setting default channel count as stereo
779 (*metaData)->setInt32(kKeySampleRate, audioSamplRate);
780 }
781
782 return OK;
783 }
784
isSeeminglyValidDTSHDHeader(const uint8_t * ptr,size_t size,unsigned & frameSize)785 static status_t isSeeminglyValidDTSHDHeader(const uint8_t *ptr, size_t size,unsigned &frameSize)
786 {
787 return parseDTSHDSyncFrame(ptr, size, frameSize, NULL);
788 }
789
isSeeminglyValidDTSUHDHeader(const uint8_t * ptr,size_t size,unsigned & frameSize)790 static status_t isSeeminglyValidDTSUHDHeader(const uint8_t *ptr, size_t size,unsigned &frameSize)
791 {
792 return parseDTSUHDSyncFrame(ptr, size, frameSize, NULL);
793 }
794
IsSeeminglyValidAC4Header(const uint8_t * ptr,size_t size,unsigned & frameSize)795 static status_t IsSeeminglyValidAC4Header(const uint8_t *ptr, size_t size, unsigned &frameSize) {
796 return parseAC4SyncFrame(ptr, size, frameSize, NULL);
797 }
798
IsSeeminglyValidADTSHeader(const uint8_t * ptr,size_t size,size_t * frameLength)799 static bool IsSeeminglyValidADTSHeader(
800 const uint8_t *ptr, size_t size, size_t *frameLength) {
801 if (size < 7) {
802 // Not enough data to verify header.
803 return false;
804 }
805
806 if (ptr[0] != 0xff || (ptr[1] >> 4) != 0x0f) {
807 return false;
808 }
809
810 unsigned layer = (ptr[1] >> 1) & 3;
811
812 if (layer != 0) {
813 return false;
814 }
815
816 unsigned ID = (ptr[1] >> 3) & 1;
817 unsigned profile_ObjectType = ptr[2] >> 6;
818
819 if (ID == 1 && profile_ObjectType == 3) {
820 // MPEG-2 profile 3 is reserved.
821 return false;
822 }
823
824 size_t frameLengthInHeader =
825 ((ptr[3] & 3) << 11) + (ptr[4] << 3) + ((ptr[5] >> 5) & 7);
826 if (frameLengthInHeader > size) {
827 return false;
828 }
829
830 *frameLength = frameLengthInHeader;
831 return true;
832 }
833
IsSeeminglyValidMPEGAudioHeader(const uint8_t * ptr,size_t size)834 static bool IsSeeminglyValidMPEGAudioHeader(const uint8_t *ptr, size_t size) {
835 if (size < 3) {
836 // Not enough data to verify header.
837 return false;
838 }
839
840 if (ptr[0] != 0xff || (ptr[1] >> 5) != 0x07) {
841 return false;
842 }
843
844 unsigned ID = (ptr[1] >> 3) & 3;
845
846 if (ID == 1) {
847 return false; // reserved
848 }
849
850 unsigned layer = (ptr[1] >> 1) & 3;
851
852 if (layer == 0) {
853 return false; // reserved
854 }
855
856 unsigned bitrateIndex = (ptr[2] >> 4);
857
858 if (bitrateIndex == 0x0f) {
859 return false; // reserved
860 }
861
862 unsigned samplingRateIndex = (ptr[2] >> 2) & 3;
863
864 if (samplingRateIndex == 3) {
865 return false; // reserved
866 }
867
868 return true;
869 }
870
appendData(const void * data,size_t size,int64_t timeUs,int32_t payloadOffset,uint32_t pesScramblingControl)871 status_t ElementaryStreamQueue::appendData(
872 const void *data, size_t size, int64_t timeUs,
873 int32_t payloadOffset, uint32_t pesScramblingControl) {
874
875 if (mEOSReached) {
876 ALOGE("appending data after EOS");
877 return ERROR_MALFORMED;
878 }
879
880 if (!isScrambled() && (mBuffer == NULL || mBuffer->size() == 0)) {
881 switch (mMode) {
882 case H264:
883 case MPEG_VIDEO:
884 {
885 #if 0
886 if (size < 4 || memcmp("\x00\x00\x00\x01", data, 4)) {
887 return ERROR_MALFORMED;
888 }
889 #else
890 uint8_t *ptr = (uint8_t *)data;
891
892 ssize_t startOffset = -1;
893 for (size_t i = 0; i + 2 < size; ++i) {
894 if (!memcmp("\x00\x00\x01", &ptr[i], 3)) {
895 startOffset = i;
896 break;
897 }
898 }
899
900 if (startOffset < 0) {
901 return ERROR_MALFORMED;
902 }
903
904 if (mFormat == NULL && startOffset > 0) {
905 ALOGI("found something resembling an H.264/MPEG syncword "
906 "at offset %zd",
907 startOffset);
908 }
909
910 data = &ptr[startOffset];
911 size -= startOffset;
912 #endif
913 break;
914 }
915
916 case MPEG4_VIDEO:
917 {
918 #if 0
919 if (size < 3 || memcmp("\x00\x00\x01", data, 3)) {
920 return ERROR_MALFORMED;
921 }
922 #else
923 uint8_t *ptr = (uint8_t *)data;
924
925 ssize_t startOffset = -1;
926 for (size_t i = 0; i + 2 < size; ++i) {
927 if (!memcmp("\x00\x00\x01", &ptr[i], 3)) {
928 startOffset = i;
929 break;
930 }
931 }
932
933 if (startOffset < 0) {
934 return ERROR_MALFORMED;
935 }
936
937 if (startOffset > 0) {
938 ALOGI("found something resembling an H.264/MPEG syncword "
939 "at offset %zd",
940 startOffset);
941 }
942
943 data = &ptr[startOffset];
944 size -= startOffset;
945 #endif
946 break;
947 }
948
949 case AAC:
950 {
951 uint8_t *ptr = (uint8_t *)data;
952
953 #if 0
954 if (size < 2 || ptr[0] != 0xff || (ptr[1] >> 4) != 0x0f) {
955 return ERROR_MALFORMED;
956 }
957 #else
958 ssize_t startOffset = -1;
959 size_t frameLength;
960 for (size_t i = 0; i < size; ++i) {
961 if (IsSeeminglyValidADTSHeader(
962 &ptr[i], size - i, &frameLength)) {
963 startOffset = i;
964 break;
965 }
966 }
967
968 if (startOffset < 0) {
969 return ERROR_MALFORMED;
970 }
971
972 if (startOffset > 0) {
973 ALOGI("found something resembling an AAC syncword at "
974 "offset %zd",
975 startOffset);
976 }
977
978 if (frameLength != size - startOffset) {
979 ALOGV("First ADTS AAC frame length is %zd bytes, "
980 "while the buffer size is %zd bytes.",
981 frameLength, size - startOffset);
982 }
983
984 data = &ptr[startOffset];
985 size -= startOffset;
986 #endif
987 break;
988 }
989
990 case AC3:
991 case EAC3:
992 {
993 uint8_t *ptr = (uint8_t *)data;
994
995 ssize_t startOffset = -1;
996 for (size_t i = 0; i < size; ++i) {
997 unsigned payloadSize = 0;
998 if (mMode == AC3) {
999 payloadSize = parseAC3SyncFrame(&ptr[i], size - i, NULL);
1000 } else if (mMode == EAC3) {
1001 payloadSize = parseEAC3SyncFrame(&ptr[i], size - i, NULL);
1002 }
1003 if (payloadSize > 0) {
1004 startOffset = i;
1005 break;
1006 }
1007 }
1008
1009 if (startOffset < 0) {
1010 return ERROR_MALFORMED;
1011 }
1012
1013 if (startOffset > 0) {
1014 ALOGI("found something resembling an (E)AC3 syncword at "
1015 "offset %zd",
1016 startOffset);
1017 }
1018
1019 data = &ptr[startOffset];
1020 size -= startOffset;
1021 break;
1022 }
1023
1024 case AC4:
1025 {
1026 uint8_t *ptr = (uint8_t *)data;
1027 unsigned frameSize = 0;
1028 ssize_t startOffset = -1;
1029
1030 // A valid AC4 stream should have minimum of 7 bytes in its buffer.
1031 // (Sync header 4 bytes + AC4 toc 3 bytes)
1032 if (size < 7) {
1033 return ERROR_MALFORMED;
1034 }
1035 for (size_t i = 0; i < size; ++i) {
1036 if (IsSeeminglyValidAC4Header(&ptr[i], size - i, frameSize) == OK) {
1037 startOffset = i;
1038 break;
1039 }
1040 }
1041
1042 if (startOffset < 0) {
1043 return ERROR_MALFORMED;
1044 }
1045
1046 if (startOffset > 0) {
1047 ALOGI("found something resembling an AC4 syncword at "
1048 "offset %zd",
1049 startOffset);
1050 }
1051 if (frameSize != size - startOffset) {
1052 ALOGV("AC4 frame size is %u bytes, while the buffer size is %zd bytes.",
1053 frameSize, size - startOffset);
1054 }
1055
1056 data = &ptr[startOffset];
1057 size -= startOffset;
1058 break;
1059 }
1060
1061 case MPEG_AUDIO:
1062 {
1063 uint8_t *ptr = (uint8_t *)data;
1064
1065 ssize_t startOffset = -1;
1066 for (size_t i = 0; i < size; ++i) {
1067 if (IsSeeminglyValidMPEGAudioHeader(&ptr[i], size - i)) {
1068 startOffset = i;
1069 break;
1070 }
1071 }
1072
1073 if (startOffset < 0) {
1074 return ERROR_MALFORMED;
1075 }
1076
1077 if (startOffset > 0) {
1078 ALOGI("found something resembling an MPEG audio "
1079 "syncword at offset %zd",
1080 startOffset);
1081 }
1082
1083 data = &ptr[startOffset];
1084 size -= startOffset;
1085 break;
1086 }
1087
1088 case DTS: // Checking for DTS or DTS-HD syncword
1089 case DTS_HD:
1090 {
1091 uint8_t *ptr = (uint8_t *)data;
1092 unsigned frameSize = 0;
1093 ssize_t startOffset = -1;
1094
1095 for (size_t i = 0; i < size; ++i) {
1096 if (isSeeminglyValidDTSHDHeader(&ptr[i], size - i, frameSize) == OK) {
1097 startOffset = i;
1098 break;
1099 }
1100 }
1101
1102 if (startOffset < 0) {
1103 return ERROR_MALFORMED;
1104 }
1105 if (startOffset > 0) {
1106 ALOGI("found something resembling a DTS-HD syncword at "
1107 "offset %zd",
1108 startOffset);
1109 }
1110
1111 if (frameSize != size - startOffset) {
1112 ALOGV("DTS-HD frame size is %u bytes, while the buffer size is %zd bytes.",
1113 frameSize, size - startOffset);
1114 }
1115
1116 data = &ptr[startOffset];
1117 size -= startOffset;
1118 break;
1119 }
1120
1121 case DTS_UHD:
1122 {
1123 uint8_t *ptr = (uint8_t *)data;
1124 ssize_t startOffset = -1;
1125 unsigned frameSize = 0;
1126
1127 for (size_t i = 0; i < size; ++i) {
1128 if (isSeeminglyValidDTSUHDHeader(&ptr[i], size - i, frameSize) == OK) {
1129 startOffset = i;
1130 break;
1131 }
1132 }
1133
1134 if (startOffset < 0) {
1135 return ERROR_MALFORMED;
1136 }
1137 if (startOffset >= 0) {
1138 ALOGI("found something resembling a DTS UHD syncword"
1139 "syncword at offset %zd",
1140 startOffset);
1141 }
1142
1143 if (frameSize != size - startOffset) {
1144 ALOGV("DTS-UHD frame size is %u bytes, while the buffer size is %zd bytes.",
1145 frameSize, size - startOffset);
1146 }
1147 data = &ptr[startOffset];
1148 size -= startOffset;
1149 break;
1150 }
1151
1152 case PCM_AUDIO:
1153 case METADATA:
1154 {
1155 break;
1156 }
1157
1158 default:
1159 ALOGE("Unknown mode: %d", mMode);
1160 return ERROR_MALFORMED;
1161 }
1162 }
1163
1164 size_t neededSize = (mBuffer == NULL ? 0 : mBuffer->size()) + size;
1165 if (mBuffer == NULL || neededSize > mBuffer->capacity()) {
1166 neededSize = (neededSize + 65535) & ~65535;
1167
1168 ALOGV("resizing buffer to size %zu", neededSize);
1169
1170 sp<ABuffer> buffer = new ABuffer(neededSize);
1171 if (mBuffer != NULL) {
1172 memcpy(buffer->data(), mBuffer->data(), mBuffer->size());
1173 buffer->setRange(0, mBuffer->size());
1174 } else {
1175 buffer->setRange(0, 0);
1176 }
1177
1178 mBuffer = buffer;
1179 }
1180
1181 memcpy(mBuffer->data() + mBuffer->size(), data, size);
1182 mBuffer->setRange(0, mBuffer->size() + size);
1183
1184 RangeInfo info;
1185 info.mLength = size;
1186 info.mTimestampUs = timeUs;
1187 info.mPesOffset = payloadOffset;
1188 info.mPesScramblingControl = pesScramblingControl;
1189 mRangeInfos.push_back(info);
1190
1191 #if 0
1192 if (mMode == AAC) {
1193 ALOGI("size = %zu, timeUs = %.2f secs", size, timeUs / 1E6);
1194 hexdump(data, size);
1195 }
1196 #endif
1197
1198 return OK;
1199 }
1200
appendScrambledData(const void * data,size_t size,size_t leadingClearBytes,int32_t keyId,bool isSync,sp<ABuffer> clearSizes,sp<ABuffer> encSizes)1201 void ElementaryStreamQueue::appendScrambledData(
1202 const void *data, size_t size,
1203 size_t leadingClearBytes,
1204 int32_t keyId, bool isSync,
1205 sp<ABuffer> clearSizes, sp<ABuffer> encSizes) {
1206 if (!isScrambled()) {
1207 return;
1208 }
1209
1210 size_t neededSize = (mScrambledBuffer == NULL ? 0 : mScrambledBuffer->size()) + size;
1211 if (mScrambledBuffer == NULL || neededSize > mScrambledBuffer->capacity()) {
1212 neededSize = (neededSize + 65535) & ~65535;
1213
1214 ALOGI("resizing scrambled buffer to size %zu", neededSize);
1215
1216 sp<ABuffer> buffer = new ABuffer(neededSize);
1217 if (mScrambledBuffer != NULL) {
1218 memcpy(buffer->data(), mScrambledBuffer->data(), mScrambledBuffer->size());
1219 buffer->setRange(0, mScrambledBuffer->size());
1220 } else {
1221 buffer->setRange(0, 0);
1222 }
1223
1224 mScrambledBuffer = buffer;
1225 }
1226 memcpy(mScrambledBuffer->data() + mScrambledBuffer->size(), data, size);
1227 mScrambledBuffer->setRange(0, mScrambledBuffer->size() + size);
1228
1229 ScrambledRangeInfo scrambledInfo;
1230 scrambledInfo.mLength = size;
1231 scrambledInfo.mLeadingClearBytes = leadingClearBytes;
1232 scrambledInfo.mKeyId = keyId;
1233 scrambledInfo.mIsSync = isSync;
1234 scrambledInfo.mClearSizes = clearSizes;
1235 scrambledInfo.mEncSizes = encSizes;
1236
1237 ALOGV("[stream %d] appending scrambled range: size=%zu", mMode, size);
1238
1239 mScrambledRangeInfos.push_back(scrambledInfo);
1240 }
1241
dequeueScrambledAccessUnit()1242 sp<ABuffer> ElementaryStreamQueue::dequeueScrambledAccessUnit() {
1243 size_t nextScan = mBuffer->size();
1244 int32_t pesOffset = 0, pesScramblingControl = 0;
1245 int64_t timeUs = fetchTimestamp(nextScan, &pesOffset, &pesScramblingControl);
1246 if (timeUs < 0ll) {
1247 ALOGE("Negative timeUs");
1248 return NULL;
1249 }
1250
1251 // return scrambled unit
1252 int32_t keyId = pesScramblingControl, isSync = 0, scrambledLength = 0;
1253 sp<ABuffer> clearSizes, encSizes;
1254 size_t leadingClearBytes;
1255 while (mScrambledRangeInfos.size() > mRangeInfos.size()) {
1256 auto it = mScrambledRangeInfos.begin();
1257 ALOGV("[stream %d] fetching scrambled range: size=%zu", mMode, it->mLength);
1258
1259 if (scrambledLength > 0) {
1260 // This shouldn't happen since we always dequeue the entire PES.
1261 ALOGW("Discarding srambled length %d", scrambledLength);
1262 }
1263 scrambledLength = it->mLength;
1264
1265 // TODO: handle key id change, use first non-zero keyId for now
1266 if (keyId == 0) {
1267 keyId = it->mKeyId;
1268 }
1269 clearSizes = it->mClearSizes;
1270 encSizes = it->mEncSizes;
1271 isSync = it->mIsSync;
1272 leadingClearBytes = it->mLeadingClearBytes;
1273 mScrambledRangeInfos.erase(it);
1274 }
1275 if (scrambledLength == 0) {
1276 ALOGE("[stream %d] empty scrambled unit!", mMode);
1277 return NULL;
1278 }
1279
1280 // Retrieve the leading clear bytes info, and use it to set the clear
1281 // range on mBuffer. Note that the leading clear bytes includes the
1282 // PES header portion, while mBuffer doesn't.
1283 if ((int32_t)leadingClearBytes > pesOffset) {
1284 mBuffer->setRange(0, leadingClearBytes - pesOffset);
1285 } else {
1286 mBuffer->setRange(0, 0);
1287 }
1288
1289 // Try to parse formats, and if unavailable set up a dummy format.
1290 // Only support the following modes for scrambled content for now.
1291 // (will be expanded later).
1292 if (mFormat == NULL) {
1293 mFormat = new MetaData;
1294 switch (mMode) {
1295 case H264:
1296 {
1297 if (!MakeAVCCodecSpecificData(
1298 *mFormat, mBuffer->data(), mBuffer->size())) {
1299 ALOGI("Creating dummy AVC format for scrambled content");
1300
1301 mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
1302 mFormat->setInt32(kKeyWidth, 1280);
1303 mFormat->setInt32(kKeyHeight, 720);
1304 }
1305 break;
1306 }
1307 case AAC:
1308 {
1309 if (!MakeAACCodecSpecificData(
1310 *mFormat, mBuffer->data(), mBuffer->size())) {
1311 ALOGI("Creating dummy AAC format for scrambled content");
1312
1313 MakeAACCodecSpecificData(*mFormat,
1314 1 /*profile*/, 7 /*sampling_freq_index*/, 1 /*channel_config*/);
1315 mFormat->setInt32(kKeyIsADTS, true);
1316 }
1317
1318 break;
1319 }
1320 case MPEG_VIDEO:
1321 {
1322 ALOGI("Creating dummy MPEG format for scrambled content");
1323
1324 mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG2);
1325 mFormat->setInt32(kKeyWidth, 1280);
1326 mFormat->setInt32(kKeyHeight, 720);
1327 break;
1328 }
1329 default:
1330 {
1331 ALOGE("Unknown mode for scrambled content");
1332 return NULL;
1333 }
1334 }
1335
1336 // for MediaExtractor.CasInfo
1337 mFormat->setInt32(kKeyCASystemID, mCASystemId);
1338 mFormat->setData(kKeyCASessionID,
1339 0, mCasSessionId.data(), mCasSessionId.size());
1340 }
1341
1342 mBuffer->setRange(0, 0);
1343
1344 // copy into scrambled access unit
1345 sp<ABuffer> scrambledAccessUnit = ABuffer::CreateAsCopy(
1346 mScrambledBuffer->data(), scrambledLength);
1347
1348 scrambledAccessUnit->meta()->setInt64("timeUs", timeUs);
1349 if (isSync) {
1350 scrambledAccessUnit->meta()->setInt32("isSync", 1);
1351 }
1352
1353 // fill in CryptoInfo fields for AnotherPacketSource::read()
1354 // MediaCas doesn't use cryptoMode, but set to non-zero value here.
1355 scrambledAccessUnit->meta()->setInt32(
1356 "cryptoMode", CryptoPlugin::kMode_AES_CTR);
1357 scrambledAccessUnit->meta()->setInt32("cryptoKey", keyId);
1358 scrambledAccessUnit->meta()->setBuffer("clearBytes", clearSizes);
1359 scrambledAccessUnit->meta()->setBuffer("encBytes", encSizes);
1360 scrambledAccessUnit->meta()->setInt32("pesOffset", pesOffset);
1361
1362 memmove(mScrambledBuffer->data(),
1363 mScrambledBuffer->data() + scrambledLength,
1364 mScrambledBuffer->size() - scrambledLength);
1365
1366 mScrambledBuffer->setRange(0, mScrambledBuffer->size() - scrambledLength);
1367
1368 ALOGV("[stream %d] dequeued scrambled AU: timeUs=%lld, size=%zu",
1369 mMode, (long long)timeUs, scrambledAccessUnit->size());
1370
1371 return scrambledAccessUnit;
1372 }
1373
dequeueAccessUnit()1374 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnit() {
1375 if (isScrambled()) {
1376 return dequeueScrambledAccessUnit();
1377 }
1378
1379 if ((mFlags & kFlag_AlignedData) && mMode == H264) {
1380 if (mRangeInfos.empty()) {
1381 return NULL;
1382 }
1383
1384 RangeInfo info = *mRangeInfos.begin();
1385 mRangeInfos.erase(mRangeInfos.begin());
1386
1387 sp<ABuffer> accessUnit = new ABuffer(info.mLength);
1388 memcpy(accessUnit->data(), mBuffer->data(), info.mLength);
1389 accessUnit->meta()->setInt64("timeUs", info.mTimestampUs);
1390
1391 memmove(mBuffer->data(),
1392 mBuffer->data() + info.mLength,
1393 mBuffer->size() - info.mLength);
1394
1395 mBuffer->setRange(0, mBuffer->size() - info.mLength);
1396
1397 if (mFormat == NULL) {
1398 mFormat = new MetaData;
1399 if (!MakeAVCCodecSpecificData(*mFormat, accessUnit->data(), accessUnit->size())) {
1400 mFormat.clear();
1401 }
1402 }
1403
1404 return accessUnit;
1405 }
1406
1407 switch (mMode) {
1408 case H264:
1409 return dequeueAccessUnitH264();
1410 case AAC:
1411 return dequeueAccessUnitAAC();
1412 case AC3:
1413 case EAC3:
1414 return dequeueAccessUnitEAC3();
1415 case AC4:
1416 return dequeueAccessUnitAC4();
1417 case MPEG_VIDEO:
1418 return dequeueAccessUnitMPEGVideo();
1419 case MPEG4_VIDEO:
1420 return dequeueAccessUnitMPEG4Video();
1421 case PCM_AUDIO:
1422 return dequeueAccessUnitPCMAudio();
1423 case METADATA:
1424 return dequeueAccessUnitMetadata();
1425 case DTS: // Using same dequeue function for both DTS and DTS-HD types.
1426 case DTS_HD:
1427 return dequeueAccessUnitDTSOrDTSHD();
1428 case DTS_UHD:
1429 return dequeueAccessUnitDTSUHD();
1430 default:
1431 if (mMode != MPEG_AUDIO) {
1432 ALOGE("Unknown mode");
1433 return NULL;
1434 }
1435 return dequeueAccessUnitMPEGAudio();
1436 }
1437 }
1438
dequeueAccessUnitDTSOrDTSHD()1439 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitDTSOrDTSHD() {
1440 unsigned syncStartPos = 0; // in bytes
1441 unsigned payloadSize = 0;
1442 sp<MetaData> format = new MetaData;
1443
1444 ALOGV("dequeueAccessUnitDTSOrDTSHD[%d]: mBuffer %p(%zu)", mAUIndex,
1445 mBuffer->data(), mBuffer->size());
1446
1447 while (true) {
1448 if (syncStartPos + 4 >= mBuffer->size()) {
1449 return NULL;
1450 }
1451 uint8_t *ptr = mBuffer->data() + syncStartPos;
1452 size_t size = mBuffer->size() - syncStartPos;
1453 status_t status = parseDTSHDSyncFrame(ptr, size, payloadSize, &format);
1454 if (status == 0) {
1455 break;
1456 }
1457 ++syncStartPos;
1458 }
1459
1460 if (mBuffer->size() < syncStartPos + payloadSize) {
1461 ALOGV("Not enough buffer size for DTS/DTS-HD");
1462 return NULL;
1463 }
1464
1465 if (mFormat == NULL) {
1466 mFormat = format;
1467 }
1468
1469 int64_t timeUs = fetchTimestamp(syncStartPos + payloadSize);
1470 if (timeUs < 0LL) {
1471 ALOGE("negative timeUs");
1472 return NULL;
1473 }
1474 mAUIndex++;
1475
1476 sp<ABuffer> accessUnit = new ABuffer(syncStartPos + payloadSize);
1477 memcpy(accessUnit->data(), mBuffer->data(), syncStartPos + payloadSize);
1478
1479 accessUnit->meta()->setInt64("timeUs", timeUs);
1480 accessUnit->meta()->setInt32("isSync", 1);
1481
1482 memmove(
1483 mBuffer->data(),
1484 mBuffer->data() + syncStartPos + payloadSize,
1485 mBuffer->size() - syncStartPos - payloadSize);
1486
1487 mBuffer->setRange(0, mBuffer->size() - syncStartPos - payloadSize);
1488
1489 return accessUnit;
1490 }
1491
dequeueAccessUnitDTSUHD()1492 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitDTSUHD()
1493 {
1494 unsigned syncStartPos = 0; // in bytes
1495 unsigned payloadSize = 0;
1496 sp<MetaData> format = new MetaData;
1497
1498 ALOGV("dequeueAccessUnitDTSUHD[%d]: mBuffer %p(%zu)", mAUIndex,
1499 mBuffer->data(), mBuffer->size());
1500
1501 while (true) {
1502 if (syncStartPos + 4 >= mBuffer->size()) {
1503 return NULL;
1504 }
1505 uint8_t *ptr = mBuffer->data() + syncStartPos;
1506 size_t size = mBuffer->size() - syncStartPos;
1507 status_t status = parseDTSUHDSyncFrame(ptr, size, payloadSize, &format);
1508 if (status == 0) {
1509 break;
1510 }
1511 ++syncStartPos;
1512 }
1513
1514 if (mBuffer->size() < syncStartPos + payloadSize) {
1515 ALOGV("Not enough buffer size for DTS-UHD");
1516 return NULL;
1517 }
1518
1519 if (mFormat == NULL) {
1520 mFormat = format;
1521 }
1522
1523 int64_t timeUs = fetchTimestamp(syncStartPos + payloadSize);
1524 if (timeUs < 0LL) {
1525 ALOGE("negative timeUs");
1526 return NULL;
1527 }
1528 mAUIndex++;
1529
1530 sp<ABuffer> accessUnit = new ABuffer(syncStartPos + payloadSize);
1531 memcpy(accessUnit->data(), mBuffer->data(), syncStartPos + payloadSize);
1532
1533 accessUnit->meta()->setInt64("timeUs", timeUs);
1534 accessUnit->meta()->setInt32("isSync", 1);
1535
1536 memmove(
1537 mBuffer->data(),
1538 mBuffer->data() + syncStartPos + payloadSize,
1539 mBuffer->size() - syncStartPos - payloadSize);
1540
1541 mBuffer->setRange(0, mBuffer->size() - syncStartPos - payloadSize);
1542
1543 return accessUnit;
1544 }
1545
dequeueAccessUnitEAC3()1546 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitEAC3() {
1547 unsigned syncStartPos = 0; // in bytes
1548 unsigned payloadSize = 0;
1549 sp<MetaData> format = new MetaData;
1550
1551 ALOGV("dequeueAccessUnitEAC3[%d]: mBuffer %p(%zu)", mAUIndex,
1552 mBuffer->data(), mBuffer->size());
1553
1554 while (true) {
1555 if (syncStartPos + 2 >= mBuffer->size()) {
1556 return NULL;
1557 }
1558
1559 uint8_t *ptr = mBuffer->data() + syncStartPos;
1560 size_t size = mBuffer->size() - syncStartPos;
1561 if (mMode == AC3) {
1562 payloadSize = parseAC3SyncFrame(ptr, size, &format);
1563 } else if (mMode == EAC3) {
1564 payloadSize = parseEAC3SyncFrame(ptr, size, &format);
1565 }
1566 if (payloadSize > 0) {
1567 break;
1568 }
1569
1570 ALOGV("dequeueAccessUnitEAC3[%d]: syncStartPos %u payloadSize %u",
1571 mAUIndex, syncStartPos, payloadSize);
1572
1573 ++syncStartPos;
1574 }
1575
1576 if (mBuffer->size() < syncStartPos + payloadSize) {
1577 ALOGV("Not enough buffer size for E/AC3");
1578 return NULL;
1579 }
1580
1581 if (mFormat == NULL) {
1582 mFormat = format;
1583 }
1584
1585 int64_t timeUs = fetchTimestamp(syncStartPos + payloadSize);
1586 if (timeUs < 0ll) {
1587 ALOGE("negative timeUs");
1588 return NULL;
1589 }
1590
1591 // Not decrypting if key info not available (e.g., scanner/extractor parsing ts files)
1592 if (mSampleDecryptor != NULL) {
1593 if (mMode == AC3) {
1594 mSampleDecryptor->processAC3(mBuffer->data() + syncStartPos, payloadSize);
1595 } else if (mMode == EAC3) {
1596 ALOGE("EAC3 AU is encrypted and decryption is not supported");
1597 return NULL;
1598 }
1599 }
1600 mAUIndex++;
1601
1602 sp<ABuffer> accessUnit = new ABuffer(syncStartPos + payloadSize);
1603 memcpy(accessUnit->data(), mBuffer->data(), syncStartPos + payloadSize);
1604
1605 accessUnit->meta()->setInt64("timeUs", timeUs);
1606 accessUnit->meta()->setInt32("isSync", 1);
1607
1608 memmove(
1609 mBuffer->data(),
1610 mBuffer->data() + syncStartPos + payloadSize,
1611 mBuffer->size() - syncStartPos - payloadSize);
1612
1613 mBuffer->setRange(0, mBuffer->size() - syncStartPos - payloadSize);
1614
1615 return accessUnit;
1616 }
1617
dequeueAccessUnitAC4()1618 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitAC4() {
1619 unsigned syncStartPos = 0;
1620 unsigned payloadSize = 0;
1621 sp<MetaData> format = new MetaData;
1622 ALOGV("dequeueAccessUnit_AC4[%d]: mBuffer %p(%zu)", mAUIndex, mBuffer->data(), mBuffer->size());
1623
1624 // A valid AC4 stream should have minimum of 7 bytes in its buffer.
1625 // (Sync header 4 bytes + AC4 toc 3 bytes)
1626 if (mBuffer->size() < 7) {
1627 return NULL;
1628 }
1629
1630 while (true) {
1631 if (syncStartPos + 2 >= mBuffer->size()) {
1632 return NULL;
1633 }
1634
1635 status_t status = parseAC4SyncFrame(
1636 mBuffer->data() + syncStartPos,
1637 mBuffer->size() - syncStartPos,
1638 payloadSize,
1639 &format);
1640 if (status == OK) {
1641 break;
1642 }
1643
1644 ALOGV("dequeueAccessUnit_AC4[%d]: syncStartPos %u payloadSize %u",
1645 mAUIndex, syncStartPos, payloadSize);
1646
1647 ++syncStartPos;
1648 }
1649
1650 if (mBuffer->size() < syncStartPos + payloadSize) {
1651 ALOGV("Not enough buffer size for AC4");
1652 return NULL;
1653 }
1654
1655 if (mFormat == NULL) {
1656 mFormat = format;
1657 }
1658
1659 int64_t timeUs = fetchTimestamp(syncStartPos + payloadSize);
1660 if (timeUs < 0ll) {
1661 ALOGE("negative timeUs");
1662 return NULL;
1663 }
1664 mAUIndex++;
1665
1666 sp<ABuffer> accessUnit = new ABuffer(syncStartPos + payloadSize);
1667 memcpy(accessUnit->data(), mBuffer->data(), syncStartPos + payloadSize);
1668
1669 accessUnit->meta()->setInt64("timeUs", timeUs);
1670 accessUnit->meta()->setInt32("isSync", 1);
1671
1672 memmove(
1673 mBuffer->data(),
1674 mBuffer->data() + syncStartPos + payloadSize,
1675 mBuffer->size() - syncStartPos - payloadSize);
1676
1677 mBuffer->setRange(0, mBuffer->size() - syncStartPos - payloadSize);
1678 return accessUnit;
1679 }
1680
dequeueAccessUnitPCMAudio()1681 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitPCMAudio() {
1682 if (mBuffer->size() < 4) {
1683 return NULL;
1684 }
1685
1686 ABitReader bits(mBuffer->data(), 4);
1687 if (bits.getBits(8) != 0xa0) {
1688 ALOGE("Unexpected bit values");
1689 return NULL;
1690 }
1691 unsigned numAUs = bits.getBits(8);
1692 bits.skipBits(8);
1693 bits.skipBits(2); // quantization_word_length
1694 unsigned audio_sampling_frequency = bits.getBits(3);
1695 unsigned num_channels = bits.getBits(3);
1696
1697 if (audio_sampling_frequency != 2) {
1698 ALOGE("Wrong sampling freq");
1699 return NULL;
1700 }
1701 if (num_channels != 1u) {
1702 ALOGE("Wrong channel #");
1703 return NULL;
1704 }
1705
1706 if (mFormat == NULL) {
1707 mFormat = new MetaData;
1708 mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
1709 mFormat->setInt32(kKeyChannelCount, 2);
1710 mFormat->setInt32(kKeySampleRate, 48000);
1711 mFormat->setInt32(kKeyPcmEncoding, kAudioEncodingPcm16bit);
1712 }
1713
1714 static const size_t kFramesPerAU = 80;
1715 size_t frameSize = 2 /* numChannels */ * sizeof(int16_t);
1716
1717 size_t payloadSize = numAUs * frameSize * kFramesPerAU;
1718
1719 if (mBuffer->size() < 4 + payloadSize) {
1720 return NULL;
1721 }
1722
1723 sp<ABuffer> accessUnit = new ABuffer(payloadSize);
1724 memcpy(accessUnit->data(), mBuffer->data() + 4, payloadSize);
1725
1726 int64_t timeUs = fetchTimestamp(payloadSize + 4);
1727 if (timeUs < 0LL) {
1728 ALOGE("Negative timeUs");
1729 return NULL;
1730 }
1731 accessUnit->meta()->setInt64("timeUs", timeUs);
1732 accessUnit->meta()->setInt32("isSync", 1);
1733
1734 int16_t *ptr = (int16_t *)accessUnit->data();
1735 for (size_t i = 0; i < payloadSize / sizeof(int16_t); ++i) {
1736 ptr[i] = ntohs(ptr[i]);
1737 }
1738
1739 memmove(
1740 mBuffer->data(),
1741 mBuffer->data() + 4 + payloadSize,
1742 mBuffer->size() - 4 - payloadSize);
1743
1744 mBuffer->setRange(0, mBuffer->size() - 4 - payloadSize);
1745
1746 return accessUnit;
1747 }
1748
dequeueAccessUnitAAC()1749 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitAAC() {
1750 if (mBuffer->size() == 0) {
1751 return NULL;
1752 }
1753
1754 if (mRangeInfos.empty()) {
1755 return NULL;
1756 }
1757
1758 const RangeInfo &info = *mRangeInfos.begin();
1759 if (info.mLength == 0 || mBuffer->size() < info.mLength) {
1760 return NULL;
1761 }
1762
1763 if (info.mTimestampUs < 0LL) {
1764 ALOGE("Negative info.mTimestampUs");
1765 return NULL;
1766 }
1767
1768 ALOGV("dequeueAccessUnit_AAC[%d]: mBuffer %zu info.mLength %zu",
1769 mAUIndex, mBuffer->size(), info.mLength);
1770
1771 struct ADTSPosition {
1772 size_t offset;
1773 size_t headerSize;
1774 size_t length;
1775 };
1776
1777 Vector<ADTSPosition> frames;
1778
1779 // The idea here is consume all AAC frames starting at offsets before
1780 // info.mLength so we can assign a meaningful timestamp without
1781 // having to interpolate.
1782 // The final AAC frame may well extend into the next RangeInfo but
1783 // that's ok.
1784 size_t offset = 0;
1785 while (offset < info.mLength) {
1786 if (offset + 7 > mBuffer->size()) {
1787 return NULL;
1788 }
1789
1790 ABitReader bits(mBuffer->data() + offset, mBuffer->size() - offset);
1791
1792 // adts_fixed_header
1793
1794 if (bits.getBits(12) != 0xfffu) {
1795 ALOGE("Wrong atds_fixed_header");
1796 return NULL;
1797 }
1798 bits.skipBits(3); // ID, layer
1799 bool protection_absent = bits.getBits(1) != 0;
1800
1801 if (mFormat == NULL) {
1802 mFormat = new MetaData;
1803 if (!MakeAACCodecSpecificData(
1804 *mFormat, mBuffer->data() + offset, mBuffer->size() - offset)) {
1805 return NULL;
1806 }
1807
1808 int32_t sampleRate;
1809 int32_t numChannels;
1810 if (!mFormat->findInt32(kKeySampleRate, &sampleRate)) {
1811 ALOGE("SampleRate not found");
1812 return NULL;
1813 }
1814 if (!mFormat->findInt32(kKeyChannelCount, &numChannels)) {
1815 ALOGE("ChannelCount not found");
1816 return NULL;
1817 }
1818
1819 ALOGI("found AAC codec config (%d Hz, %d channels)",
1820 sampleRate, numChannels);
1821 }
1822
1823 // profile_ObjectType, sampling_frequency_index, private_bits,
1824 // channel_configuration, original_copy, home
1825 bits.skipBits(12);
1826
1827 // adts_variable_header
1828
1829 // copyright_identification_bit, copyright_identification_start
1830 bits.skipBits(2);
1831
1832 unsigned aac_frame_length = bits.getBits(13);
1833 if (aac_frame_length == 0){
1834 ALOGE("b/62673179, Invalid AAC frame length!");
1835 android_errorWriteLog(0x534e4554, "62673179");
1836 return NULL;
1837 }
1838
1839 bits.skipBits(11); // adts_buffer_fullness
1840
1841 unsigned number_of_raw_data_blocks_in_frame = bits.getBits(2);
1842
1843 if (number_of_raw_data_blocks_in_frame != 0) {
1844 // To be implemented.
1845 ALOGE("Should not reach here.");
1846 return NULL;
1847 }
1848
1849 if (offset + aac_frame_length > mBuffer->size()) {
1850 return NULL;
1851 }
1852
1853 size_t headerSize = protection_absent ? 7 : 9;
1854
1855 // tracking the frame positions first then decrypt only if an accessUnit to be generated
1856 if (mSampleDecryptor != NULL) {
1857 ADTSPosition frame = {
1858 .offset = offset,
1859 .headerSize = headerSize,
1860 .length = aac_frame_length
1861 };
1862
1863 frames.push(frame);
1864 }
1865
1866 offset += aac_frame_length;
1867 }
1868
1869 // Decrypting only if the loop didn't exit early and an accessUnit is about to be generated
1870 // Not decrypting if key info not available (e.g., scanner/extractor parsing ts files)
1871 if (mSampleDecryptor != NULL) {
1872 for (size_t frameId = 0; frameId < frames.size(); frameId++) {
1873 const ADTSPosition &frame = frames.itemAt(frameId);
1874
1875 mSampleDecryptor->processAAC(frame.headerSize,
1876 mBuffer->data() + frame.offset, frame.length);
1877 // ALOGV("dequeueAccessUnitAAC[%zu]: while offset %zu headerSize %zu frame_len %zu",
1878 // frameId, frame.offset, frame.headerSize, frame.length);
1879 }
1880 }
1881 mAUIndex++;
1882
1883 int64_t timeUs = fetchTimestamp(offset);
1884
1885 sp<ABuffer> accessUnit = new ABuffer(offset);
1886 memcpy(accessUnit->data(), mBuffer->data(), offset);
1887
1888 memmove(mBuffer->data(), mBuffer->data() + offset,
1889 mBuffer->size() - offset);
1890 mBuffer->setRange(0, mBuffer->size() - offset);
1891
1892 accessUnit->meta()->setInt64("timeUs", timeUs);
1893 accessUnit->meta()->setInt32("isSync", 1);
1894
1895 return accessUnit;
1896 }
1897
fetchTimestamp(size_t size,int32_t * pesOffset,int32_t * pesScramblingControl)1898 int64_t ElementaryStreamQueue::fetchTimestamp(
1899 size_t size, int32_t *pesOffset, int32_t *pesScramblingControl) {
1900 int64_t timeUs = -1;
1901 bool first = true;
1902
1903 while (size > 0) {
1904 if (mRangeInfos.empty()) {
1905 return timeUs;
1906 }
1907
1908 RangeInfo *info = &*mRangeInfos.begin();
1909
1910 if (first) {
1911 timeUs = info->mTimestampUs;
1912 if (pesOffset != NULL) {
1913 *pesOffset = info->mPesOffset;
1914 }
1915 if (pesScramblingControl != NULL) {
1916 *pesScramblingControl = info->mPesScramblingControl;
1917 }
1918 first = false;
1919 }
1920
1921 if (info->mLength > size) {
1922 info->mLength -= size;
1923 size = 0;
1924 } else {
1925 size -= info->mLength;
1926
1927 mRangeInfos.erase(mRangeInfos.begin());
1928 info = NULL;
1929 }
1930
1931 }
1932
1933 if (timeUs == 0LL) {
1934 ALOGV("Returning 0 timestamp");
1935 }
1936
1937 return timeUs;
1938 }
1939
dequeueAccessUnitH264()1940 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitH264() {
1941 const uint8_t *data = mBuffer->data();
1942
1943 size_t size = mBuffer->size();
1944 Vector<NALPosition> nals;
1945
1946 size_t totalSize = 0;
1947 size_t seiCount = 0;
1948
1949 status_t err;
1950 const uint8_t *nalStart;
1951 size_t nalSize;
1952 bool foundSlice = false;
1953 bool foundIDR = false;
1954
1955 ALOGV("dequeueAccessUnit_H264[%d] %p/%zu", mAUIndex, data, size);
1956
1957 while ((err = getNextNALUnit(&data, &size, &nalStart, &nalSize)) == OK) {
1958 if (nalSize == 0) continue;
1959
1960 unsigned nalType = nalStart[0] & 0x1f;
1961 bool flush = false;
1962
1963 if (nalType == 1 || nalType == 5) {
1964 if (nalType == 5) {
1965 foundIDR = true;
1966 }
1967 if (foundSlice) {
1968 //TODO: Shouldn't this have been called with nalSize-1?
1969 ABitReader br(nalStart + 1, nalSize);
1970 unsigned first_mb_in_slice = parseUE(&br);
1971
1972 if (first_mb_in_slice == 0) {
1973 // This slice starts a new frame.
1974
1975 flush = true;
1976 }
1977 }
1978
1979 foundSlice = true;
1980 } else if ((nalType == 9 || nalType == 7) && foundSlice) {
1981 // Access unit delimiter and SPS will be associated with the
1982 // next frame.
1983
1984 flush = true;
1985 } else if (nalType == 6 && nalSize > 0) {
1986 // found non-zero sized SEI
1987 ++seiCount;
1988 }
1989
1990 if (flush) {
1991 // The access unit will contain all nal units up to, but excluding
1992 // the current one, separated by 0x00 0x00 0x00 0x01 startcodes.
1993
1994 size_t auSize = 4 * nals.size() + totalSize;
1995 sp<ABuffer> accessUnit = new ABuffer(auSize);
1996 sp<ABuffer> sei;
1997
1998 if (seiCount > 0) {
1999 sei = new ABuffer(seiCount * sizeof(NALPosition));
2000 accessUnit->meta()->setBuffer("sei", sei);
2001 }
2002
2003 #if !LOG_NDEBUG
2004 AString out;
2005 #endif
2006
2007 size_t dstOffset = 0;
2008 size_t seiIndex = 0;
2009 size_t shrunkBytes = 0;
2010 for (size_t i = 0; i < nals.size(); ++i) {
2011 const NALPosition &pos = nals.itemAt(i);
2012
2013 unsigned nalType = mBuffer->data()[pos.nalOffset] & 0x1f;
2014
2015 if (nalType == 6 && pos.nalSize > 0) {
2016 if (seiIndex >= sei->size() / sizeof(NALPosition)) {
2017 ALOGE("Wrong seiIndex");
2018 return NULL;
2019 }
2020 NALPosition &seiPos = ((NALPosition *)sei->data())[seiIndex++];
2021 seiPos.nalOffset = dstOffset + 4;
2022 seiPos.nalSize = pos.nalSize;
2023 }
2024
2025 #if !LOG_NDEBUG
2026 char tmp[128];
2027 sprintf(tmp, "0x%02x", nalType);
2028 if (i > 0) {
2029 out.append(", ");
2030 }
2031 out.append(tmp);
2032 #endif
2033
2034 memcpy(accessUnit->data() + dstOffset, "\x00\x00\x00\x01", 4);
2035
2036 if (mSampleDecryptor != NULL && (nalType == 1 || nalType == 5)) {
2037 uint8_t *nalData = mBuffer->data() + pos.nalOffset;
2038 size_t newSize = mSampleDecryptor->processNal(nalData, pos.nalSize);
2039 // Note: the data can shrink due to unescaping, but it can never grow
2040 if (newSize > pos.nalSize) {
2041 // don't log unless verbose, since this can get called a lot if
2042 // the caller is trying to resynchronize
2043 ALOGV("expected sample size < %u, got %zu", pos.nalSize, newSize);
2044 return NULL;
2045 }
2046 memcpy(accessUnit->data() + dstOffset + 4,
2047 nalData,
2048 newSize);
2049 dstOffset += newSize + 4;
2050
2051 size_t thisShrunkBytes = pos.nalSize - newSize;
2052 //ALOGV("dequeueAccessUnitH264[%d]: nalType: %d -> %zu (%zu)",
2053 // nalType, (int)pos.nalSize, newSize, thisShrunkBytes);
2054
2055 shrunkBytes += thisShrunkBytes;
2056 }
2057 else {
2058 memcpy(accessUnit->data() + dstOffset + 4,
2059 mBuffer->data() + pos.nalOffset,
2060 pos.nalSize);
2061
2062 dstOffset += pos.nalSize + 4;
2063 //ALOGV("dequeueAccessUnitH264 [%d] %d @%d",
2064 // nalType, (int)pos.nalSize, (int)pos.nalOffset);
2065 }
2066 }
2067
2068 #if !LOG_NDEBUG
2069 ALOGV("accessUnit contains nal types %s", out.c_str());
2070 #endif
2071
2072 const NALPosition &pos = nals.itemAt(nals.size() - 1);
2073 size_t nextScan = pos.nalOffset + pos.nalSize;
2074
2075 memmove(mBuffer->data(),
2076 mBuffer->data() + nextScan,
2077 mBuffer->size() - nextScan);
2078
2079 mBuffer->setRange(0, mBuffer->size() - nextScan);
2080
2081 int64_t timeUs = fetchTimestamp(nextScan);
2082 if (timeUs < 0LL) {
2083 ALOGE("Negative timeUs");
2084 return NULL;
2085 }
2086
2087 accessUnit->meta()->setInt64("timeUs", timeUs);
2088 if (foundIDR) {
2089 accessUnit->meta()->setInt32("isSync", 1);
2090 }
2091
2092 if (mFormat == NULL) {
2093 mFormat = new MetaData;
2094 if (!MakeAVCCodecSpecificData(*mFormat,
2095 accessUnit->data(),
2096 accessUnit->size())) {
2097 mFormat.clear();
2098 }
2099 }
2100
2101 if (mSampleDecryptor != NULL && shrunkBytes > 0) {
2102 size_t adjustedSize = accessUnit->size() - shrunkBytes;
2103 ALOGV("dequeueAccessUnitH264[%d]: AU size adjusted %zu -> %zu",
2104 mAUIndex, accessUnit->size(), adjustedSize);
2105 accessUnit->setRange(0, adjustedSize);
2106 }
2107
2108 ALOGV("dequeueAccessUnitH264[%d]: AU %p(%zu) dstOffset:%zu, nals:%zu, totalSize:%zu ",
2109 mAUIndex, accessUnit->data(), accessUnit->size(),
2110 dstOffset, nals.size(), totalSize);
2111 mAUIndex++;
2112
2113 return accessUnit;
2114 }
2115
2116 NALPosition pos;
2117 pos.nalOffset = nalStart - mBuffer->data();
2118 pos.nalSize = nalSize;
2119
2120 nals.push(pos);
2121
2122 totalSize += nalSize;
2123 }
2124 if (err != (status_t)-EAGAIN) {
2125 ALOGE("Unexpeted err");
2126 return NULL;
2127 }
2128
2129 return NULL;
2130 }
2131
dequeueAccessUnitMPEGAudio()2132 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitMPEGAudio() {
2133 const uint8_t *data = mBuffer->data();
2134 size_t size = mBuffer->size();
2135
2136 if (size < 4) {
2137 return NULL;
2138 }
2139
2140 uint32_t header = U32_AT(data);
2141
2142 size_t frameSize;
2143 int samplingRate, numChannels, bitrate, numSamples;
2144 if (!GetMPEGAudioFrameSize(
2145 header, &frameSize, &samplingRate, &numChannels,
2146 &bitrate, &numSamples)) {
2147 ALOGE("Failed to get audio frame size");
2148 mBuffer->setRange(0, 0);
2149 return NULL;
2150 }
2151
2152 if (size < frameSize) {
2153 return NULL;
2154 }
2155
2156 unsigned layer = 4 - ((header >> 17) & 3);
2157
2158 sp<ABuffer> accessUnit = new ABuffer(frameSize);
2159 memcpy(accessUnit->data(), data, frameSize);
2160
2161 memmove(mBuffer->data(),
2162 mBuffer->data() + frameSize,
2163 mBuffer->size() - frameSize);
2164
2165 mBuffer->setRange(0, mBuffer->size() - frameSize);
2166
2167 int64_t timeUs = fetchTimestamp(frameSize);
2168 if (timeUs < 0LL) {
2169 ALOGE("Negative timeUs");
2170 return NULL;
2171 }
2172
2173 if (mFormat != NULL) {
2174 const char *mime;
2175 if (mFormat->findCString(kKeyMIMEType, &mime)) {
2176 if ((layer == 1) && strcmp (mime, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_I)) {
2177 ALOGE("Audio layer is not MPEG_LAYER_I");
2178 return NULL;
2179 } else if ((layer == 2) && strcmp (mime, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II)) {
2180 ALOGE("Audio layer is not MPEG_LAYER_II");
2181 return NULL;
2182 } else if ((layer == 3) && strcmp (mime, MEDIA_MIMETYPE_AUDIO_MPEG)) {
2183 ALOGE("Audio layer is not AUDIO_MPEG");
2184 return NULL;
2185 }
2186 }
2187 }
2188
2189 accessUnit->meta()->setInt64("timeUs", timeUs);
2190 accessUnit->meta()->setInt32("isSync", 1);
2191
2192 if (mFormat == NULL) {
2193 mFormat = new MetaData;
2194
2195 switch (layer) {
2196 case 1:
2197 mFormat->setCString(
2198 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_I);
2199 break;
2200 case 2:
2201 mFormat->setCString(
2202 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II);
2203 break;
2204 case 3:
2205 mFormat->setCString(
2206 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG);
2207 break;
2208 default:
2209 return NULL;
2210 }
2211
2212 mFormat->setInt32(kKeySampleRate, samplingRate);
2213 mFormat->setInt32(kKeyChannelCount, numChannels);
2214 }
2215
2216 return accessUnit;
2217 }
2218
EncodeSize14(uint8_t ** _ptr,size_t size)2219 static void EncodeSize14(uint8_t **_ptr, size_t size) {
2220 if (size > 0x3fff) {
2221 ALOGE("Wrong size");
2222 return;
2223 }
2224
2225 uint8_t *ptr = *_ptr;
2226
2227 *ptr++ = 0x80 | (size >> 7);
2228 *ptr++ = size & 0x7f;
2229
2230 *_ptr = ptr;
2231 }
2232
MakeMPEGVideoESDS(const sp<ABuffer> & csd)2233 static sp<ABuffer> MakeMPEGVideoESDS(const sp<ABuffer> &csd) {
2234 sp<ABuffer> esds = new ABuffer(csd->size() + 25);
2235
2236 uint8_t *ptr = esds->data();
2237 *ptr++ = 0x03;
2238 EncodeSize14(&ptr, 22 + csd->size());
2239
2240 *ptr++ = 0x00; // ES_ID
2241 *ptr++ = 0x00;
2242
2243 *ptr++ = 0x00; // streamDependenceFlag, URL_Flag, OCRstreamFlag
2244
2245 *ptr++ = 0x04;
2246 EncodeSize14(&ptr, 16 + csd->size());
2247
2248 *ptr++ = 0x40; // Audio ISO/IEC 14496-3
2249
2250 for (size_t i = 0; i < 12; ++i) {
2251 *ptr++ = 0x00;
2252 }
2253
2254 *ptr++ = 0x05;
2255 EncodeSize14(&ptr, csd->size());
2256
2257 memcpy(ptr, csd->data(), csd->size());
2258
2259 return esds;
2260 }
2261
dequeueAccessUnitMPEGVideo()2262 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitMPEGVideo() {
2263 const uint8_t *data = mBuffer->data();
2264 size_t size = mBuffer->size();
2265
2266 Vector<size_t> userDataPositions;
2267
2268 bool sawPictureStart = false;
2269 int pprevStartCode = -1;
2270 int prevStartCode = -1;
2271 int currentStartCode = -1;
2272 bool gopFound = false;
2273 bool isClosedGop = false;
2274 bool brokenLink = false;
2275
2276 size_t offset = 0;
2277 while (offset + 3 < size) {
2278 if (memcmp(&data[offset], "\x00\x00\x01", 3)) {
2279 ++offset;
2280 continue;
2281 }
2282
2283 pprevStartCode = prevStartCode;
2284 prevStartCode = currentStartCode;
2285 currentStartCode = data[offset + 3];
2286
2287 if (currentStartCode == 0xb3 && mFormat == NULL) {
2288 memmove(mBuffer->data(), mBuffer->data() + offset, size - offset);
2289 size -= offset;
2290 (void)fetchTimestamp(offset);
2291 offset = 0;
2292 mBuffer->setRange(0, size);
2293 }
2294
2295 if ((prevStartCode == 0xb3 && currentStartCode != 0xb5)
2296 || (pprevStartCode == 0xb3 && prevStartCode == 0xb5)) {
2297 // seqHeader without/with extension
2298
2299 if (mFormat == NULL) {
2300 if (size < 7u) {
2301 ALOGE("Size too small");
2302 return NULL;
2303 }
2304
2305 unsigned width =
2306 (data[4] << 4) | data[5] >> 4;
2307
2308 unsigned height =
2309 ((data[5] & 0x0f) << 8) | data[6];
2310
2311 mFormat = new MetaData;
2312 mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG2);
2313 mFormat->setInt32(kKeyWidth, width);
2314 mFormat->setInt32(kKeyHeight, height);
2315
2316 ALOGI("found MPEG2 video codec config (%d x %d)", width, height);
2317
2318 sp<ABuffer> csd = new ABuffer(offset);
2319 memcpy(csd->data(), data, offset);
2320
2321 memmove(mBuffer->data(),
2322 mBuffer->data() + offset,
2323 mBuffer->size() - offset);
2324
2325 mBuffer->setRange(0, mBuffer->size() - offset);
2326 size -= offset;
2327 (void)fetchTimestamp(offset);
2328 offset = 0;
2329
2330 // hexdump(csd->data(), csd->size());
2331
2332 sp<ABuffer> esds = MakeMPEGVideoESDS(csd);
2333 mFormat->setData(
2334 kKeyESDS, kTypeESDS, esds->data(), esds->size());
2335
2336 return NULL;
2337 }
2338 }
2339
2340 if (mFormat != NULL && currentStartCode == 0xb8) {
2341 // GOP layer
2342 if (offset + 7 >= size) {
2343 ALOGE("Size too small");
2344 return NULL;
2345 }
2346 gopFound = true;
2347 isClosedGop = (data[offset + 7] & 0x40) != 0;
2348 brokenLink = (data[offset + 7] & 0x20) != 0;
2349 }
2350
2351 if (mFormat != NULL && currentStartCode == 0xb2) {
2352 userDataPositions.add(offset);
2353 }
2354
2355 if (mFormat != NULL && currentStartCode == 0x00) {
2356 // Picture start
2357
2358 if (!sawPictureStart) {
2359 sawPictureStart = true;
2360 } else {
2361 sp<ABuffer> accessUnit = new ABuffer(offset);
2362 memcpy(accessUnit->data(), data, offset);
2363
2364 memmove(mBuffer->data(),
2365 mBuffer->data() + offset,
2366 mBuffer->size() - offset);
2367
2368 mBuffer->setRange(0, mBuffer->size() - offset);
2369
2370 int64_t timeUs = fetchTimestamp(offset);
2371 if (timeUs < 0LL) {
2372 ALOGE("Negative timeUs");
2373 return NULL;
2374 }
2375
2376 offset = 0;
2377
2378 accessUnit->meta()->setInt64("timeUs", timeUs);
2379 if (gopFound && (!brokenLink || isClosedGop)) {
2380 accessUnit->meta()->setInt32("isSync", 1);
2381 }
2382
2383 ALOGV("returning MPEG video access unit at time %" PRId64 " us",
2384 timeUs);
2385
2386 // hexdump(accessUnit->data(), accessUnit->size());
2387
2388 if (userDataPositions.size() > 0) {
2389 sp<ABuffer> mpegUserData =
2390 new ABuffer(userDataPositions.size() * sizeof(size_t));
2391 if (mpegUserData != NULL && mpegUserData->data() != NULL) {
2392 for (size_t i = 0; i < userDataPositions.size(); ++i) {
2393 memcpy(
2394 mpegUserData->data() + i * sizeof(size_t),
2395 &userDataPositions[i], sizeof(size_t));
2396 }
2397 accessUnit->meta()->setBuffer("mpeg-user-data", mpegUserData);
2398 }
2399 }
2400
2401 return accessUnit;
2402 }
2403 }
2404
2405 ++offset;
2406 }
2407
2408 return NULL;
2409 }
2410
getNextChunkSize(const uint8_t * data,size_t size)2411 static ssize_t getNextChunkSize(
2412 const uint8_t *data, size_t size) {
2413 static const char kStartCode[] = "\x00\x00\x01";
2414
2415 // per ISO/IEC 14496-2 6.2.1, a chunk has a 3-byte prefix + 1-byte start code
2416 // we need at least <prefix><start><next prefix> to successfully scan
2417 if (size < 3 + 1 + 3) {
2418 return -EAGAIN;
2419 }
2420
2421 if (memcmp(kStartCode, data, 3)) {
2422 return -EAGAIN;
2423 }
2424
2425 size_t offset = 4;
2426 while (offset + 2 < size) {
2427 if (!memcmp(&data[offset], kStartCode, 3)) {
2428 return offset;
2429 }
2430
2431 ++offset;
2432 }
2433
2434 return -EAGAIN;
2435 }
2436
dequeueAccessUnitMPEG4Video()2437 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitMPEG4Video() {
2438 uint8_t *data = mBuffer->data();
2439 size_t size = mBuffer->size();
2440
2441 enum {
2442 SKIP_TO_VISUAL_OBJECT_SEQ_START,
2443 EXPECT_VISUAL_OBJECT_START,
2444 EXPECT_VO_START,
2445 EXPECT_VOL_START,
2446 WAIT_FOR_VOP_START,
2447 SKIP_TO_VOP_START,
2448
2449 } state;
2450
2451 if (mFormat == NULL) {
2452 state = SKIP_TO_VISUAL_OBJECT_SEQ_START;
2453 } else {
2454 state = SKIP_TO_VOP_START;
2455 }
2456
2457 int32_t width = -1, height = -1;
2458
2459 size_t offset = 0;
2460 ssize_t chunkSize;
2461 while ((chunkSize = getNextChunkSize(
2462 &data[offset], size - offset)) > 0) {
2463 bool discard = false;
2464
2465 unsigned chunkType = data[offset + 3];
2466
2467 switch (state) {
2468 case SKIP_TO_VISUAL_OBJECT_SEQ_START:
2469 {
2470 if (chunkType == 0xb0) {
2471 // Discard anything before this marker.
2472
2473 state = EXPECT_VISUAL_OBJECT_START;
2474 } else {
2475 discard = true;
2476 offset += chunkSize;
2477 ALOGW("b/74114680, advance to next chunk");
2478 android_errorWriteLog(0x534e4554, "74114680");
2479 }
2480 break;
2481 }
2482
2483 case EXPECT_VISUAL_OBJECT_START:
2484 {
2485 if (chunkType != 0xb5) {
2486 ALOGE("Unexpected chunkType");
2487 return NULL;
2488 }
2489 state = EXPECT_VO_START;
2490 break;
2491 }
2492
2493 case EXPECT_VO_START:
2494 {
2495 if (chunkType > 0x1f) {
2496 ALOGE("Unexpected chunkType");
2497 return NULL;
2498 }
2499 state = EXPECT_VOL_START;
2500 break;
2501 }
2502
2503 case EXPECT_VOL_START:
2504 {
2505 if ((chunkType & 0xf0) != 0x20) {
2506 ALOGE("Wrong chunkType");
2507 return NULL;
2508 }
2509
2510 if (!ExtractDimensionsFromVOLHeader(
2511 &data[offset], chunkSize,
2512 &width, &height)) {
2513 ALOGE("Failed to get dimension");
2514 return NULL;
2515 }
2516
2517 state = WAIT_FOR_VOP_START;
2518 break;
2519 }
2520
2521 case WAIT_FOR_VOP_START:
2522 {
2523 if (chunkType == 0xb3 || chunkType == 0xb6) {
2524 // group of VOP or VOP start.
2525
2526 mFormat = new MetaData;
2527 mFormat->setCString(
2528 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
2529
2530 mFormat->setInt32(kKeyWidth, width);
2531 mFormat->setInt32(kKeyHeight, height);
2532
2533 ALOGI("found MPEG4 video codec config (%d x %d)",
2534 width, height);
2535
2536 sp<ABuffer> csd = new ABuffer(offset);
2537 memcpy(csd->data(), data, offset);
2538
2539 // hexdump(csd->data(), csd->size());
2540
2541 sp<ABuffer> esds = MakeMPEGVideoESDS(csd);
2542 mFormat->setData(
2543 kKeyESDS, kTypeESDS,
2544 esds->data(), esds->size());
2545
2546 discard = true;
2547 state = SKIP_TO_VOP_START;
2548 }
2549
2550 break;
2551 }
2552
2553 case SKIP_TO_VOP_START:
2554 {
2555 if (chunkType == 0xb6) {
2556 int vopCodingType = (data[offset + 4] & 0xc0) >> 6;
2557
2558 offset += chunkSize;
2559
2560 sp<ABuffer> accessUnit = new ABuffer(offset);
2561 memcpy(accessUnit->data(), data, offset);
2562
2563 memmove(data, &data[offset], size - offset);
2564 size -= offset;
2565 mBuffer->setRange(0, size);
2566
2567 int64_t timeUs = fetchTimestamp(offset);
2568 if (timeUs < 0LL) {
2569 ALOGE("Negative timeus");
2570 return NULL;
2571 }
2572
2573 offset = 0;
2574
2575 accessUnit->meta()->setInt64("timeUs", timeUs);
2576 if (vopCodingType == 0) { // intra-coded VOP
2577 accessUnit->meta()->setInt32("isSync", 1);
2578 }
2579
2580 ALOGV("returning MPEG4 video access unit at time %" PRId64 " us",
2581 timeUs);
2582
2583 // hexdump(accessUnit->data(), accessUnit->size());
2584
2585 return accessUnit;
2586 } else if (chunkType != 0xb3) {
2587 offset += chunkSize;
2588 discard = true;
2589 }
2590
2591 break;
2592 }
2593
2594 default:
2595 ALOGE("Unknown state: %d", state);
2596 return NULL;
2597 }
2598
2599 if (discard) {
2600 (void)fetchTimestamp(offset);
2601 memmove(data, &data[offset], size - offset);
2602 size -= offset;
2603 offset = 0;
2604 mBuffer->setRange(0, size);
2605 } else {
2606 offset += chunkSize;
2607 }
2608 }
2609
2610 return NULL;
2611 }
2612
signalEOS()2613 void ElementaryStreamQueue::signalEOS() {
2614 if (!mEOSReached) {
2615 if (mMode == MPEG_VIDEO) {
2616 const char *theEnd = "\x00\x00\x01\x00";
2617 appendData(theEnd, 4, 0);
2618 }
2619 mEOSReached = true;
2620 } else {
2621 ALOGW("EOS already signaled");
2622 }
2623 }
2624
dequeueAccessUnitMetadata()2625 sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitMetadata() {
2626 size_t size = mBuffer->size();
2627 if (!size) {
2628 return NULL;
2629 }
2630
2631 sp<ABuffer> accessUnit = new ABuffer(size);
2632 int64_t timeUs = fetchTimestamp(size);
2633 accessUnit->meta()->setInt64("timeUs", timeUs);
2634
2635 memcpy(accessUnit->data(), mBuffer->data(), size);
2636 mBuffer->setRange(0, 0);
2637
2638 if (mFormat == NULL) {
2639 mFormat = new MetaData;
2640 mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_DATA_TIMED_ID3);
2641 }
2642
2643 return accessUnit;
2644 }
2645
signalNewSampleAesKey(const sp<AMessage> & keyItem)2646 void ElementaryStreamQueue::signalNewSampleAesKey(const sp<AMessage> &keyItem) {
2647 if (mSampleDecryptor == NULL) {
2648 ALOGE("signalNewSampleAesKey: Stream %x is not encrypted; keyItem: %p",
2649 mMode, keyItem.get());
2650 return;
2651 }
2652
2653 mSampleDecryptor->signalNewSampleAesKey(keyItem);
2654 }
2655
2656
2657 } // namespace android
2658