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