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         if (i < mNumSubFrames && offset >= buffer->size()) {
428             ALOGW("Skip subframes after %d, total %d", (int)i, (int)mNumSubFrames);
429             break;
430         }
431     }
432 
433     if (offset < buffer->size()) {
434         ALOGI("ignoring %zu bytes of trailing data", buffer->size() - offset);
435     }
436     CHECK_LE(offset, buffer->size());
437 
438     return out;
439 }
440 
AMPEG4AudioAssembler(const sp<AMessage> & notify,const AString & params)441 AMPEG4AudioAssembler::AMPEG4AudioAssembler(
442         const sp<AMessage> &notify, const AString &params)
443     : mNotifyMsg(notify),
444       mMuxConfigPresent(false),
445       mAccessUnitRTPTime(0),
446       mNextExpectedSeqNoValid(false),
447       mNextExpectedSeqNo(0),
448       mAccessUnitDamaged(false) {
449     AString val;
450     if (!GetAttribute(params.c_str(), "cpresent", &val)) {
451         mMuxConfigPresent = true;
452     } else if (val == "0") {
453         mMuxConfigPresent = false;
454     } else {
455         CHECK(val == "1");
456         mMuxConfigPresent = true;
457     }
458 
459     CHECK(GetAttribute(params.c_str(), "config", &val));
460 
461     sp<ABuffer> config = decodeHex(val);
462     CHECK(config != NULL);
463 
464     ABitReader bits(config->data(), config->size());
465     status_t err = parseStreamMuxConfig(
466             &bits, &mNumSubFrames, &mFrameLengthType,
467             &mFixedFrameLength,
468             &mOtherDataPresent, &mOtherDataLenBits);
469 
470     if (err == ERROR_UNSUPPORTED) {
471         ALOGW("Failed to parse stream mux config, using default values for playback.");
472         mMuxConfigPresent = false;
473         mNumSubFrames = 0;
474         mFrameLengthType = 0;
475         mOtherDataPresent = false;
476         mOtherDataLenBits = 0;
477         return;
478     }
479     CHECK_EQ(err, (status_t)NO_ERROR);
480 }
481 
~AMPEG4AudioAssembler()482 AMPEG4AudioAssembler::~AMPEG4AudioAssembler() {
483 }
484 
assembleMore(const sp<ARTPSource> & source)485 ARTPAssembler::AssemblyStatus AMPEG4AudioAssembler::assembleMore(
486         const sp<ARTPSource> &source) {
487     AssemblyStatus status = addPacket(source);
488     if (status == MALFORMED_PACKET) {
489         mAccessUnitDamaged = true;
490     }
491     return status;
492 }
493 
addPacket(const sp<ARTPSource> & source)494 ARTPAssembler::AssemblyStatus AMPEG4AudioAssembler::addPacket(
495         const sp<ARTPSource> &source) {
496     List<sp<ABuffer> > *queue = source->queue();
497 
498     if (queue->empty()) {
499         return NOT_ENOUGH_DATA;
500     }
501 
502     if (mNextExpectedSeqNoValid) {
503         List<sp<ABuffer> >::iterator it = queue->begin();
504         while (it != queue->end()) {
505             if ((uint32_t)(*it)->int32Data() >= mNextExpectedSeqNo) {
506                 break;
507             }
508 
509             it = queue->erase(it);
510         }
511 
512         if (queue->empty()) {
513             return NOT_ENOUGH_DATA;
514         }
515     }
516 
517     sp<ABuffer> buffer = *queue->begin();
518 
519     if (!mNextExpectedSeqNoValid) {
520         mNextExpectedSeqNoValid = true;
521         mNextExpectedSeqNo = (uint32_t)buffer->int32Data();
522     } else if ((uint32_t)buffer->int32Data() != mNextExpectedSeqNo) {
523 #if VERBOSE
524         LOG(VERBOSE) << "Not the sequence number I expected";
525 #endif
526 
527         return WRONG_SEQUENCE_NUMBER;
528     }
529 
530     uint32_t rtpTime;
531     CHECK(buffer->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
532 
533     if (mPackets.size() > 0 && rtpTime != mAccessUnitRTPTime) {
534         submitAccessUnit();
535     }
536     mAccessUnitRTPTime = rtpTime;
537 
538     mPackets.push_back(buffer);
539 
540     queue->erase(queue->begin());
541     ++mNextExpectedSeqNo;
542 
543     return OK;
544 }
545 
submitAccessUnit()546 void AMPEG4AudioAssembler::submitAccessUnit() {
547     CHECK(!mPackets.empty());
548 
549 #if VERBOSE
550     LOG(VERBOSE) << "Access unit complete (" << mPackets.size() << " packets)";
551 #endif
552 
553     sp<ABuffer> accessUnit = MakeCompoundFromPackets(mPackets);
554     accessUnit = removeLATMFraming(accessUnit);
555     CopyTimes(accessUnit, *mPackets.begin());
556 
557 #if 0
558     printf(mAccessUnitDamaged ? "X" : ".");
559     fflush(stdout);
560 #endif
561 
562     if (mAccessUnitDamaged) {
563         accessUnit->meta()->setInt32("damaged", true);
564     }
565 
566     mPackets.clear();
567     mAccessUnitDamaged = false;
568 
569     sp<AMessage> msg = mNotifyMsg->dup();
570     msg->setBuffer("access-unit", accessUnit);
571     msg->post();
572 }
573 
packetLost()574 void AMPEG4AudioAssembler::packetLost() {
575     CHECK(mNextExpectedSeqNoValid);
576     ++mNextExpectedSeqNo;
577 
578     mAccessUnitDamaged = true;
579 }
580 
onByeReceived()581 void AMPEG4AudioAssembler::onByeReceived() {
582     sp<AMessage> msg = mNotifyMsg->dup();
583     msg->setInt32("eos", true);
584     msg->post();
585 }
586 
587 }  // namespace android
588