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