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