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