ESQueue.cpp revision a093f92042cf65060d1474c0fe116c12f8981717
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
31#include "include/avc_utils.h"
32
33namespace android {
34
35ElementaryStreamQueue::ElementaryStreamQueue(Mode mode)
36    : mMode(mode) {
37}
38
39sp<MetaData> ElementaryStreamQueue::getFormat() {
40    return mFormat;
41}
42
43void ElementaryStreamQueue::clear() {
44    if (mBuffer != NULL) {
45        mBuffer->setRange(0, 0);
46    }
47
48    mRangeInfos.clear();
49    mFormat.clear();
50}
51
52static bool IsSeeminglyValidADTSHeader(const uint8_t *ptr, size_t size) {
53    if (size < 3) {
54        // Not enough data to verify header.
55        return false;
56    }
57
58    if (ptr[0] != 0xff || (ptr[1] >> 4) != 0x0f) {
59        return false;
60    }
61
62    unsigned layer = (ptr[1] >> 1) & 3;
63
64    if (layer != 0) {
65        return false;
66    }
67
68    unsigned ID = (ptr[1] >> 3) & 1;
69    unsigned profile_ObjectType = ptr[2] >> 6;
70
71    if (ID == 1 && profile_ObjectType == 3) {
72        // MPEG-2 profile 3 is reserved.
73        return false;
74    }
75
76    return true;
77}
78
79status_t ElementaryStreamQueue::appendData(
80        const void *data, size_t size, int64_t timeUs) {
81    if (mBuffer == NULL || mBuffer->size() == 0) {
82        switch (mMode) {
83            case H264:
84            {
85#if 0
86                if (size < 4 || memcmp("\x00\x00\x00\x01", data, 4)) {
87                    return ERROR_MALFORMED;
88                }
89#else
90                uint8_t *ptr = (uint8_t *)data;
91
92                ssize_t startOffset = -1;
93                for (size_t i = 0; i + 3 < size; ++i) {
94                    if (!memcmp("\x00\x00\x00\x01", &ptr[i], 4)) {
95                        startOffset = i;
96                        break;
97                    }
98                }
99
100                if (startOffset < 0) {
101                    return ERROR_MALFORMED;
102                }
103
104                if (startOffset > 0) {
105                    LOGI("found something resembling an H.264 syncword at "
106                         "offset %ld",
107                         startOffset);
108                }
109
110                data = &ptr[startOffset];
111                size -= startOffset;
112#endif
113                break;
114            }
115
116            case AAC:
117            {
118                uint8_t *ptr = (uint8_t *)data;
119
120#if 0
121                if (size < 2 || ptr[0] != 0xff || (ptr[1] >> 4) != 0x0f) {
122                    return ERROR_MALFORMED;
123                }
124#else
125                ssize_t startOffset = -1;
126                for (size_t i = 0; i < size; ++i) {
127                    if (IsSeeminglyValidADTSHeader(&ptr[i], size - i)) {
128                        startOffset = i;
129                        break;
130                    }
131                }
132
133                if (startOffset < 0) {
134                    return ERROR_MALFORMED;
135                }
136
137                if (startOffset > 0) {
138                    LOGI("found something resembling an AAC syncword at offset %ld",
139                         startOffset);
140                }
141
142                data = &ptr[startOffset];
143                size -= startOffset;
144#endif
145                break;
146            }
147
148            default:
149                TRESPASS();
150                break;
151        }
152    }
153
154    size_t neededSize = (mBuffer == NULL ? 0 : mBuffer->size()) + size;
155    if (mBuffer == NULL || neededSize > mBuffer->capacity()) {
156        neededSize = (neededSize + 65535) & ~65535;
157
158        LOGV("resizing buffer to size %d", neededSize);
159
160        sp<ABuffer> buffer = new ABuffer(neededSize);
161        if (mBuffer != NULL) {
162            memcpy(buffer->data(), mBuffer->data(), mBuffer->size());
163            buffer->setRange(0, mBuffer->size());
164        } else {
165            buffer->setRange(0, 0);
166        }
167
168        mBuffer = buffer;
169    }
170
171    memcpy(mBuffer->data() + mBuffer->size(), data, size);
172    mBuffer->setRange(0, mBuffer->size() + size);
173
174    RangeInfo info;
175    info.mLength = size;
176    info.mTimestampUs = timeUs;
177    mRangeInfos.push_back(info);
178
179#if 0
180    if (mMode == AAC) {
181        LOGI("size = %d, timeUs = %.2f secs", size, timeUs / 1E6);
182        hexdump(data, size);
183    }
184#endif
185
186    return OK;
187}
188
189sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnit() {
190    if (mMode == H264) {
191        return dequeueAccessUnitH264();
192    } else {
193        CHECK_EQ((unsigned)mMode, (unsigned)AAC);
194        return dequeueAccessUnitAAC();
195    }
196}
197
198sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitAAC() {
199    Vector<size_t> ranges;
200    Vector<size_t> frameOffsets;
201    Vector<size_t> frameSizes;
202    size_t auSize = 0;
203
204    size_t offset = 0;
205    while (offset + 7 <= mBuffer->size()) {
206        ABitReader bits(mBuffer->data() + offset, mBuffer->size() - offset);
207
208        // adts_fixed_header
209
210        CHECK_EQ(bits.getBits(12), 0xfffu);
211        bits.skipBits(3);  // ID, layer
212        bool protection_absent = bits.getBits(1) != 0;
213
214        if (mFormat == NULL) {
215            unsigned profile = bits.getBits(2);
216            CHECK_NE(profile, 3u);
217            unsigned sampling_freq_index = bits.getBits(4);
218            bits.getBits(1);  // private_bit
219            unsigned channel_configuration = bits.getBits(3);
220            CHECK_NE(channel_configuration, 0u);
221            bits.skipBits(2);  // original_copy, home
222
223            mFormat = MakeAACCodecSpecificData(
224                    profile, sampling_freq_index, channel_configuration);
225        } else {
226            // profile_ObjectType, sampling_frequency_index, private_bits,
227            // channel_configuration, original_copy, home
228            bits.skipBits(12);
229        }
230
231        // adts_variable_header
232
233        // copyright_identification_bit, copyright_identification_start
234        bits.skipBits(2);
235
236        unsigned aac_frame_length = bits.getBits(13);
237
238        bits.skipBits(11);  // adts_buffer_fullness
239
240        unsigned number_of_raw_data_blocks_in_frame = bits.getBits(2);
241
242        if (number_of_raw_data_blocks_in_frame != 0) {
243            // To be implemented.
244            TRESPASS();
245        }
246
247        if (offset + aac_frame_length > mBuffer->size()) {
248            break;
249        }
250
251        size_t headerSize = protection_absent ? 7 : 9;
252
253        ranges.push(aac_frame_length);
254        frameOffsets.push(offset + headerSize);
255        frameSizes.push(aac_frame_length - headerSize);
256        auSize += aac_frame_length - headerSize;
257
258        offset += aac_frame_length;
259    }
260
261    if (offset == 0) {
262        return NULL;
263    }
264
265    int64_t timeUs = -1;
266
267    for (size_t i = 0; i < ranges.size(); ++i) {
268        int64_t tmpUs = fetchTimestamp(ranges.itemAt(i));
269
270        if (i == 0) {
271            timeUs = tmpUs;
272        }
273    }
274
275    sp<ABuffer> accessUnit = new ABuffer(auSize);
276    size_t dstOffset = 0;
277    for (size_t i = 0; i < frameOffsets.size(); ++i) {
278        size_t frameOffset = frameOffsets.itemAt(i);
279
280        memcpy(accessUnit->data() + dstOffset,
281               mBuffer->data() + frameOffset,
282               frameSizes.itemAt(i));
283
284        dstOffset += frameSizes.itemAt(i);
285    }
286
287    memmove(mBuffer->data(), mBuffer->data() + offset,
288            mBuffer->size() - offset);
289    mBuffer->setRange(0, mBuffer->size() - offset);
290
291    if (timeUs >= 0) {
292        accessUnit->meta()->setInt64("time", timeUs);
293    } else {
294        LOGW("no time for AAC access unit");
295    }
296
297    return accessUnit;
298}
299
300int64_t ElementaryStreamQueue::fetchTimestamp(size_t size) {
301    int64_t timeUs = -1;
302    bool first = true;
303
304    while (size > 0) {
305        CHECK(!mRangeInfos.empty());
306
307        RangeInfo *info = &*mRangeInfos.begin();
308
309        if (first) {
310            timeUs = info->mTimestampUs;
311            first = false;
312        }
313
314        if (info->mLength > size) {
315            info->mLength -= size;
316
317            if (first) {
318                info->mTimestampUs = -1;
319            }
320
321            size = 0;
322        } else {
323            size -= info->mLength;
324
325            mRangeInfos.erase(mRangeInfos.begin());
326            info = NULL;
327        }
328    }
329
330    return timeUs;
331}
332
333// static
334sp<MetaData> ElementaryStreamQueue::MakeAACCodecSpecificData(
335        unsigned profile, unsigned sampling_freq_index,
336        unsigned channel_configuration) {
337    sp<MetaData> meta = new MetaData;
338    meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
339
340    CHECK_LE(sampling_freq_index, 11u);
341    static const int32_t kSamplingFreq[] = {
342        96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
343        16000, 12000, 11025, 8000
344    };
345    meta->setInt32(kKeySampleRate, kSamplingFreq[sampling_freq_index]);
346    meta->setInt32(kKeyChannelCount, channel_configuration);
347
348    static const uint8_t kStaticESDS[] = {
349        0x03, 22,
350        0x00, 0x00,     // ES_ID
351        0x00,           // streamDependenceFlag, URL_Flag, OCRstreamFlag
352
353        0x04, 17,
354        0x40,                       // Audio ISO/IEC 14496-3
355        0x00, 0x00, 0x00, 0x00,
356        0x00, 0x00, 0x00, 0x00,
357        0x00, 0x00, 0x00, 0x00,
358
359        0x05, 2,
360        // AudioSpecificInfo follows
361
362        // oooo offf fccc c000
363        // o - audioObjectType
364        // f - samplingFreqIndex
365        // c - channelConfig
366    };
367    sp<ABuffer> csd = new ABuffer(sizeof(kStaticESDS) + 2);
368    memcpy(csd->data(), kStaticESDS, sizeof(kStaticESDS));
369
370    csd->data()[sizeof(kStaticESDS)] =
371        ((profile + 1) << 3) | (sampling_freq_index >> 1);
372
373    csd->data()[sizeof(kStaticESDS) + 1] =
374        ((sampling_freq_index << 7) & 0x80) | (channel_configuration << 3);
375
376    meta->setData(kKeyESDS, 0, csd->data(), csd->size());
377
378    return meta;
379}
380
381struct NALPosition {
382    size_t nalOffset;
383    size_t nalSize;
384};
385
386sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitH264() {
387    const uint8_t *data = mBuffer->data();
388    size_t size = mBuffer->size();
389
390    Vector<NALPosition> nals;
391
392    size_t totalSize = 0;
393
394    status_t err;
395    const uint8_t *nalStart;
396    size_t nalSize;
397    bool foundSlice = false;
398    while ((err = getNextNALUnit(&data, &size, &nalStart, &nalSize)) == OK) {
399        CHECK_GT(nalSize, 0u);
400
401        unsigned nalType = nalStart[0] & 0x1f;
402        bool flush = false;
403
404        if (nalType == 1 || nalType == 5) {
405            if (foundSlice) {
406                ABitReader br(nalStart + 1, nalSize);
407                unsigned first_mb_in_slice = parseUE(&br);
408
409                if (first_mb_in_slice == 0) {
410                    // This slice starts a new frame.
411
412                    flush = true;
413                }
414            }
415
416            foundSlice = true;
417        } else if ((nalType == 9 || nalType == 7) && foundSlice) {
418            // Access unit delimiter and SPS will be associated with the
419            // next frame.
420
421            flush = true;
422        }
423
424        if (flush) {
425            // The access unit will contain all nal units up to, but excluding
426            // the current one, separated by 0x00 0x00 0x00 0x01 startcodes.
427
428            size_t auSize = 4 * nals.size() + totalSize;
429            sp<ABuffer> accessUnit = new ABuffer(auSize);
430
431#if !LOG_NDEBUG
432            AString out;
433#endif
434
435            size_t dstOffset = 0;
436            for (size_t i = 0; i < nals.size(); ++i) {
437                const NALPosition &pos = nals.itemAt(i);
438
439                unsigned nalType = mBuffer->data()[pos.nalOffset] & 0x1f;
440
441#if !LOG_NDEBUG
442                char tmp[128];
443                sprintf(tmp, "0x%02x", nalType);
444                if (i > 0) {
445                    out.append(", ");
446                }
447                out.append(tmp);
448#endif
449
450                memcpy(accessUnit->data() + dstOffset, "\x00\x00\x00\x01", 4);
451
452                memcpy(accessUnit->data() + dstOffset + 4,
453                       mBuffer->data() + pos.nalOffset,
454                       pos.nalSize);
455
456                dstOffset += pos.nalSize + 4;
457            }
458
459            LOGV("accessUnit contains nal types %s", out.c_str());
460
461            const NALPosition &pos = nals.itemAt(nals.size() - 1);
462            size_t nextScan = pos.nalOffset + pos.nalSize;
463
464            memmove(mBuffer->data(),
465                    mBuffer->data() + nextScan,
466                    mBuffer->size() - nextScan);
467
468            mBuffer->setRange(0, mBuffer->size() - nextScan);
469
470            int64_t timeUs = fetchTimestamp(nextScan);
471            CHECK_GE(timeUs, 0ll);
472
473            accessUnit->meta()->setInt64("time", timeUs);
474
475            if (mFormat == NULL) {
476                mFormat = MakeAVCCodecSpecificData(accessUnit);
477            }
478
479            return accessUnit;
480        }
481
482        NALPosition pos;
483        pos.nalOffset = nalStart - mBuffer->data();
484        pos.nalSize = nalSize;
485
486        nals.push(pos);
487
488        totalSize += nalSize;
489    }
490    CHECK_EQ(err, (status_t)-EAGAIN);
491
492    return NULL;
493}
494
495}  // namespace android
496