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 "AMPEG4AudioAssembler"
19 
20 #include "AMPEG4AudioAssembler.h"
21 
22 #include "ARTPSource.h"
23 
24 #include <media/stagefright/foundation/hexdump.h>
25 #include <media/stagefright/foundation/ABitReader.h>
26 #include <media/stagefright/foundation/ABuffer.h>
27 #include <media/stagefright/foundation/ADebug.h>
28 #include <media/stagefright/foundation/AMessage.h>
29 #include <media/stagefright/MediaErrors.h>
30 
31 #include <ctype.h>
32 
33 namespace android {
34 
GetAttribute(const char * s,const char * key,AString * value)35 static bool GetAttribute(const char *s, const char *key, AString *value) {
36     value->clear();
37 
38     size_t keyLen = strlen(key);
39 
40     for (;;) {
41         while (isspace(*s)) {
42             ++s;
43         }
44 
45         const char *colonPos = strchr(s, ';');
46 
47         size_t len =
48             (colonPos == NULL) ? strlen(s) : colonPos - s;
49 
50         if (len >= keyLen + 1 && s[keyLen] == '=' && !strncmp(s, key, keyLen)) {
51             value->setTo(&s[keyLen + 1], len - keyLen - 1);
52             return true;
53         }
54 
55         if (colonPos == NULL) {
56             return false;
57         }
58 
59         s = colonPos + 1;
60     }
61 }
62 
decodeHex(const AString & s)63 static sp<ABuffer> decodeHex(const AString &s) {
64     if ((s.size() % 2) != 0) {
65         return NULL;
66     }
67 
68     size_t outLen = s.size() / 2;
69     sp<ABuffer> buffer = new ABuffer(outLen);
70     uint8_t *out = buffer->data();
71 
72     uint8_t accum = 0;
73     for (size_t i = 0; i < s.size(); ++i) {
74         char c = s.c_str()[i];
75         unsigned value;
76         if (c >= '0' && c <= '9') {
77             value = c - '0';
78         } else if (c >= 'a' && c <= 'f') {
79             value = c - 'a' + 10;
80         } else if (c >= 'A' && c <= 'F') {
81             value = c - 'A' + 10;
82         } else {
83             return NULL;
84         }
85 
86         accum = (accum << 4) | value;
87 
88         if (i & 1) {
89             *out++ = accum;
90 
91             accum = 0;
92         }
93     }
94 
95     return buffer;
96 }
97 
parseAudioObjectType(ABitReader * bits,unsigned * audioObjectType)98 static status_t parseAudioObjectType(
99         ABitReader *bits, unsigned *audioObjectType) {
100     *audioObjectType = bits->getBits(5);
101     if ((*audioObjectType) == 31) {
102         *audioObjectType = 32 + bits->getBits(6);
103     }
104 
105     return OK;
106 }
107 
parseGASpecificConfig(ABitReader * bits,unsigned audioObjectType,unsigned channelConfiguration)108 static status_t parseGASpecificConfig(
109         ABitReader *bits,
110         unsigned audioObjectType, unsigned channelConfiguration) {
111     unsigned frameLengthFlag __unused = bits->getBits(1);
112     unsigned dependsOnCoreCoder = bits->getBits(1);
113     if (dependsOnCoreCoder) {
114         /* unsigned coreCoderDelay = */bits->getBits(1);
115     }
116     unsigned extensionFlag = bits->getBits(1);
117 
118     if (!channelConfiguration) {
119         // program_config_element
120         return ERROR_UNSUPPORTED;  // XXX to be implemented
121     }
122 
123     if (audioObjectType == 6 || audioObjectType == 20) {
124         /* unsigned layerNr = */bits->getBits(3);
125     }
126 
127     if (extensionFlag) {
128         if (audioObjectType == 22) {
129             /* unsigned numOfSubFrame = */bits->getBits(5);
130             /* unsigned layerLength = */bits->getBits(11);
131         } else if (audioObjectType == 17 || audioObjectType == 19
132                 || audioObjectType == 20 || audioObjectType == 23) {
133             /* unsigned aacSectionDataResilienceFlag = */bits->getBits(1);
134             /* unsigned aacScalefactorDataResilienceFlag = */bits->getBits(1);
135             /* unsigned aacSpectralDataResilienceFlag = */bits->getBits(1);
136         }
137 
138         unsigned extensionFlag3 = bits->getBits(1);
139         CHECK_EQ(extensionFlag3, 0u);  // TBD in version 3
140     }
141 
142     return OK;
143 }
144 
parseAudioSpecificConfig(ABitReader * bits,sp<ABuffer> * asc)145 static status_t parseAudioSpecificConfig(ABitReader *bits, sp<ABuffer> *asc) {
146     const uint8_t *dataStart = bits->data();
147     size_t totalNumBits = bits->numBitsLeft();
148 
149     unsigned audioObjectType;
150     CHECK_EQ(parseAudioObjectType(bits, &audioObjectType), (status_t)OK);
151 
152     unsigned samplingFreqIndex = bits->getBits(4);
153     if (samplingFreqIndex == 0x0f) {
154         /* unsigned samplingFrequency = */bits->getBits(24);
155     }
156 
157     unsigned channelConfiguration = bits->getBits(4);
158 
159     unsigned extensionAudioObjectType = 0;
160     unsigned sbrPresent = 0;
161 
162     if (audioObjectType == 5) {
163         extensionAudioObjectType = audioObjectType;
164         sbrPresent = 1;
165         unsigned extensionSamplingFreqIndex = bits->getBits(4);
166         if (extensionSamplingFreqIndex == 0x0f) {
167             /* unsigned extensionSamplingFrequency = */bits->getBits(24);
168         }
169         CHECK_EQ(parseAudioObjectType(bits, &audioObjectType), (status_t)OK);
170     }
171 
172     CHECK((audioObjectType >= 1 && audioObjectType <= 4)
173         || (audioObjectType >= 6 && audioObjectType <= 7)
174         || audioObjectType == 17
175         || (audioObjectType >= 19 && audioObjectType <= 23));
176 
177     CHECK_EQ(parseGASpecificConfig(
178                 bits, audioObjectType, channelConfiguration), (status_t)OK);
179 
180     if (audioObjectType == 17
181             || (audioObjectType >= 19 && audioObjectType <= 27)) {
182         unsigned epConfig = bits->getBits(2);
183         if (epConfig == 2 || epConfig == 3) {
184             // ErrorProtectionSpecificConfig
185             return ERROR_UNSUPPORTED;  // XXX to be implemented
186 
187             if (epConfig == 3) {
188                 unsigned directMapping = bits->getBits(1);
189                 CHECK_EQ(directMapping, 1u);
190             }
191         }
192     }
193 
194     if (extensionAudioObjectType != 5 && bits->numBitsLeft() >= 16) {
195         size_t numBitsLeftAtStart = bits->numBitsLeft();
196 
197         unsigned syncExtensionType = bits->getBits(11);
198         if (syncExtensionType == 0x2b7) {
199             ALOGI("found syncExtension");
200 
201             CHECK_EQ(parseAudioObjectType(bits, &extensionAudioObjectType),
202                      (status_t)OK);
203 
204             sbrPresent = bits->getBits(1);
205 
206             if (sbrPresent == 1) {
207                 unsigned extensionSamplingFreqIndex = bits->getBits(4);
208                 if (extensionSamplingFreqIndex == 0x0f) {
209                     /* unsigned extensionSamplingFrequency = */bits->getBits(24);
210                 }
211             }
212 
213             size_t numBitsInExtension =
214                 numBitsLeftAtStart - bits->numBitsLeft();
215 
216             if (numBitsInExtension & 7) {
217                 // Apparently an extension is always considered an even
218                 // multiple of 8 bits long.
219 
220                 ALOGI("Skipping %zu bits after sync extension",
221                      8 - (numBitsInExtension & 7));
222 
223                 bits->skipBits(8 - (numBitsInExtension & 7));
224             }
225         } else {
226             bits->putBits(syncExtensionType, 11);
227         }
228     }
229 
230     if (asc != NULL) {
231         size_t bitpos = totalNumBits & 7;
232 
233         ABitReader bs(dataStart, (totalNumBits + 7) / 8);
234 
235         totalNumBits -= bits->numBitsLeft();
236 
237         size_t numBytes = (totalNumBits + 7) / 8;
238 
239         *asc = new ABuffer(numBytes);
240 
241         if (bitpos & 7) {
242             bs.skipBits(8 - (bitpos & 7));
243         }
244 
245         uint8_t *dstPtr = (*asc)->data();
246         while (numBytes > 0) {
247             *dstPtr++ = bs.getBits(8);
248             --numBytes;
249         }
250     }
251 
252     return OK;
253 }
254 
parseStreamMuxConfig(ABitReader * bits,unsigned * numSubFrames,unsigned * frameLengthType,ssize_t * fixedFrameLength,bool * otherDataPresent,unsigned * otherDataLenBits)255 static status_t parseStreamMuxConfig(
256         ABitReader *bits,
257         unsigned *numSubFrames,
258         unsigned *frameLengthType,
259         ssize_t *fixedFrameLength,
260         bool *otherDataPresent,
261         unsigned *otherDataLenBits) {
262     unsigned audioMuxVersion = bits->getBits(1);
263 
264     unsigned audioMuxVersionA = 0;
265     if (audioMuxVersion == 1) {
266         audioMuxVersionA = bits->getBits(1);
267     }
268 
269     CHECK_EQ(audioMuxVersionA, 0u);  // otherwise future spec
270 
271     if (audioMuxVersion != 0) {
272         return ERROR_UNSUPPORTED;  // XXX to be implemented;
273     }
274     CHECK_EQ(audioMuxVersion, 0u);  // XXX to be implemented
275 
276     unsigned allStreamsSameTimeFraming = bits->getBits(1);
277     CHECK_EQ(allStreamsSameTimeFraming, 1u);  // There's only one stream.
278 
279     *numSubFrames = bits->getBits(6);
280     unsigned numProgram = bits->getBits(4);
281     CHECK_EQ(numProgram, 0u);  // disabled in RTP LATM
282 
283     unsigned numLayer = bits->getBits(3);
284     CHECK_EQ(numLayer, 0u);  // disabled in RTP LATM
285 
286     if (audioMuxVersion == 0) {
287         // AudioSpecificConfig
288         CHECK_EQ(parseAudioSpecificConfig(bits, NULL /* asc */), (status_t)OK);
289     } else {
290         TRESPASS();  // XXX to be implemented
291     }
292 
293     *frameLengthType = bits->getBits(3);
294     *fixedFrameLength = -1;
295 
296     switch (*frameLengthType) {
297         case 0:
298         {
299             /* unsigned bufferFullness = */bits->getBits(8);
300 
301             // The "coreFrameOffset" does not apply since there's only
302             // a single layer.
303             break;
304         }
305 
306         case 1:
307         {
308             *fixedFrameLength = bits->getBits(9);
309             break;
310         }
311 
312         case 2:
313         {
314             return ERROR_UNSUPPORTED;
315         }
316 
317         case 3:
318         case 4:
319         case 5:
320         {
321             /* unsigned CELPframeLengthTableIndex = */bits->getBits(6);
322             break;
323         }
324 
325         case 6:
326         case 7:
327         {
328             /* unsigned HVXCframeLengthTableIndex = */bits->getBits(1);
329             break;
330         }
331 
332         default:
333             break;
334     }
335 
336     *otherDataPresent = bits->getBits(1);
337     *otherDataLenBits = 0;
338     if (*otherDataPresent) {
339         if (audioMuxVersion == 1) {
340             TRESPASS();  // XXX to be implemented
341         } else {
342             *otherDataLenBits = 0;
343 
344             unsigned otherDataLenEsc;
345             do {
346                 (*otherDataLenBits) <<= 8;
347                 otherDataLenEsc = bits->getBits(1);
348                 unsigned otherDataLenTmp = bits->getBits(8);
349                 (*otherDataLenBits) += otherDataLenTmp;
350             } while (otherDataLenEsc);
351         }
352     }
353 
354     unsigned crcCheckPresent = bits->getBits(1);
355     if (crcCheckPresent) {
356         /* unsigned crcCheckSum = */bits->getBits(8);
357     }
358 
359     return OK;
360 }
361 
removeLATMFraming(const sp<ABuffer> & buffer)362 sp<ABuffer> AMPEG4AudioAssembler::removeLATMFraming(const sp<ABuffer> &buffer) {
363     CHECK(!mMuxConfigPresent);  // XXX to be implemented
364 
365     sp<ABuffer> out = new ABuffer(buffer->size());
366     out->setRange(0, 0);
367 
368     size_t offset = 0;
369     uint8_t *ptr = buffer->data();
370 
371     for (size_t i = 0; i <= mNumSubFrames; ++i) {
372         // parse PayloadLengthInfo
373 
374         unsigned payloadLength = 0;
375 
376         switch (mFrameLengthType) {
377             case 0:
378             {
379                 unsigned muxSlotLengthBytes = 0;
380                 unsigned tmp;
381                 do {
382                     if (offset >= buffer->size()) {
383                         ALOGW("Malformed buffer received");
384                         return out;
385                     }
386                     tmp = ptr[offset++];
387                     muxSlotLengthBytes += tmp;
388                 } while (tmp == 0xff);
389 
390                 payloadLength = muxSlotLengthBytes;
391                 break;
392             }
393 
394             case 2:
395             {
396                 // reserved
397 
398                 TRESPASS();
399                 break;
400             }
401 
402             default:
403             {
404                 CHECK_GE(mFixedFrameLength, 0);
405 
406                 payloadLength = mFixedFrameLength;
407                 break;
408             }
409         }
410 
411         CHECK_LT(offset, buffer->size());
412         CHECK_LE(payloadLength, buffer->size() - offset);
413 
414         memcpy(out->data() + out->size(), &ptr[offset], payloadLength);
415         out->setRange(0, out->size() + payloadLength);
416 
417         offset += payloadLength;
418 
419         if (mOtherDataPresent) {
420             // We want to stay byte-aligned.
421 
422             CHECK((mOtherDataLenBits % 8) == 0);
423             CHECK_LE(offset + (mOtherDataLenBits / 8), buffer->size());
424             offset += mOtherDataLenBits / 8;
425         }
426     }
427 
428     if (offset < buffer->size()) {
429         ALOGI("ignoring %zu bytes of trailing data", buffer->size() - offset);
430     }
431     CHECK_LE(offset, buffer->size());
432 
433     return out;
434 }
435 
AMPEG4AudioAssembler(const sp<AMessage> & notify,const AString & params)436 AMPEG4AudioAssembler::AMPEG4AudioAssembler(
437         const sp<AMessage> &notify, const AString &params)
438     : mNotifyMsg(notify),
439       mMuxConfigPresent(false),
440       mAccessUnitRTPTime(0),
441       mNextExpectedSeqNoValid(false),
442       mNextExpectedSeqNo(0),
443       mAccessUnitDamaged(false) {
444     AString val;
445     if (!GetAttribute(params.c_str(), "cpresent", &val)) {
446         mMuxConfigPresent = true;
447     } else if (val == "0") {
448         mMuxConfigPresent = false;
449     } else {
450         CHECK(val == "1");
451         mMuxConfigPresent = true;
452     }
453 
454     CHECK(GetAttribute(params.c_str(), "config", &val));
455 
456     sp<ABuffer> config = decodeHex(val);
457     CHECK(config != NULL);
458 
459     ABitReader bits(config->data(), config->size());
460     status_t err = parseStreamMuxConfig(
461             &bits, &mNumSubFrames, &mFrameLengthType,
462             &mFixedFrameLength,
463             &mOtherDataPresent, &mOtherDataLenBits);
464 
465     if (err == ERROR_UNSUPPORTED) {
466         ALOGW("Failed to parse stream mux config, using default values for playback.");
467         mMuxConfigPresent = false;
468         mNumSubFrames = 0;
469         mFrameLengthType = 0;
470         mOtherDataPresent = false;
471         mOtherDataLenBits = 0;
472         return;
473     }
474     CHECK_EQ(err, (status_t)NO_ERROR);
475 }
476 
~AMPEG4AudioAssembler()477 AMPEG4AudioAssembler::~AMPEG4AudioAssembler() {
478 }
479 
assembleMore(const sp<ARTPSource> & source)480 ARTPAssembler::AssemblyStatus AMPEG4AudioAssembler::assembleMore(
481         const sp<ARTPSource> &source) {
482     AssemblyStatus status = addPacket(source);
483     if (status == MALFORMED_PACKET) {
484         mAccessUnitDamaged = true;
485     }
486     return status;
487 }
488 
addPacket(const sp<ARTPSource> & source)489 ARTPAssembler::AssemblyStatus AMPEG4AudioAssembler::addPacket(
490         const sp<ARTPSource> &source) {
491     List<sp<ABuffer> > *queue = source->queue();
492 
493     if (queue->empty()) {
494         return NOT_ENOUGH_DATA;
495     }
496 
497     if (mNextExpectedSeqNoValid) {
498         List<sp<ABuffer> >::iterator it = queue->begin();
499         while (it != queue->end()) {
500             if ((uint32_t)(*it)->int32Data() >= mNextExpectedSeqNo) {
501                 break;
502             }
503 
504             it = queue->erase(it);
505         }
506 
507         if (queue->empty()) {
508             return NOT_ENOUGH_DATA;
509         }
510     }
511 
512     sp<ABuffer> buffer = *queue->begin();
513 
514     if (!mNextExpectedSeqNoValid) {
515         mNextExpectedSeqNoValid = true;
516         mNextExpectedSeqNo = (uint32_t)buffer->int32Data();
517     } else if ((uint32_t)buffer->int32Data() != mNextExpectedSeqNo) {
518 #if VERBOSE
519         LOG(VERBOSE) << "Not the sequence number I expected";
520 #endif
521 
522         return WRONG_SEQUENCE_NUMBER;
523     }
524 
525     uint32_t rtpTime;
526     CHECK(buffer->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
527 
528     if (mPackets.size() > 0 && rtpTime != mAccessUnitRTPTime) {
529         submitAccessUnit();
530     }
531     mAccessUnitRTPTime = rtpTime;
532 
533     mPackets.push_back(buffer);
534 
535     queue->erase(queue->begin());
536     ++mNextExpectedSeqNo;
537 
538     return OK;
539 }
540 
submitAccessUnit()541 void AMPEG4AudioAssembler::submitAccessUnit() {
542     CHECK(!mPackets.empty());
543 
544 #if VERBOSE
545     LOG(VERBOSE) << "Access unit complete (" << mPackets.size() << " packets)";
546 #endif
547 
548     sp<ABuffer> accessUnit = MakeCompoundFromPackets(mPackets);
549     accessUnit = removeLATMFraming(accessUnit);
550     CopyTimes(accessUnit, *mPackets.begin());
551 
552 #if 0
553     printf(mAccessUnitDamaged ? "X" : ".");
554     fflush(stdout);
555 #endif
556 
557     if (mAccessUnitDamaged) {
558         accessUnit->meta()->setInt32("damaged", true);
559     }
560 
561     mPackets.clear();
562     mAccessUnitDamaged = false;
563 
564     sp<AMessage> msg = mNotifyMsg->dup();
565     msg->setBuffer("access-unit", accessUnit);
566     msg->post();
567 }
568 
packetLost()569 void AMPEG4AudioAssembler::packetLost() {
570     CHECK(mNextExpectedSeqNoValid);
571     ++mNextExpectedSeqNo;
572 
573     mAccessUnitDamaged = true;
574 }
575 
onByeReceived()576 void AMPEG4AudioAssembler::onByeReceived() {
577     sp<AMessage> msg = mNotifyMsg->dup();
578     msg->setInt32("eos", true);
579     msg->post();
580 }
581 
582 }  // namespace android
583