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