1343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih/* 2343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * Copyright (C) 2014 The Android Open Source Project 3343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * 4343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * Licensed under the Apache License, Version 2.0 (the "License"); 5343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * you may not use this file except in compliance with the License. 6343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * You may obtain a copy of the License at 7343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * 8343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * http://www.apache.org/licenses/LICENSE-2.0 9343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * 10343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * Unless required by applicable law or agreed to in writing, software 11343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * distributed under the License is distributed on an "AS IS" BASIS, 12343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * See the License for the specific language governing permissions and 14343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * limitations under the License. 15343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih */ 16343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 17343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih// #define LOG_NDEBUG 0 18343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#define LOG_TAG "WebmElement" 19343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 20343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include "EbmlUtil.h" 21343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include "WebmElement.h" 22343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include "WebmConstants.h" 23343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 24343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <media/stagefright/foundation/ADebug.h> 25343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <utils/Log.h> 26343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 27343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <string.h> 28343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <unistd.h> 29343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <errno.h> 30343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <fcntl.h> 31343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <sys/mman.h> 32343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 33343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihusing namespace android; 34343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihusing namespace webm; 35343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 36343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihnamespace { 37343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 38343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihint64_t voidSize(int64_t totalSize) { 39343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih if (totalSize < 2) { 40343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return -1; 41343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 42343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih if (totalSize < 9) { 43343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return totalSize - 2; 44343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 45343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return totalSize - 9; 46343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 47343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 48343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihuint64_t childrenSum(const List<sp<WebmElement> >& children) { 49343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih uint64_t total = 0; 50343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih for (List<sp<WebmElement> >::const_iterator it = children.begin(); 51343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih it != children.end(); ++it) { 52343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih total += (*it)->totalSize(); 53343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 54343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return total; 55343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 56343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 57343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihvoid populateCommonTrackEntries( 58343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih int num, 59343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih uint64_t uid, 60343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih bool lacing, 61343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih const char *lang, 62343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih const char *codec, 63343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih TrackTypes type, 64343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih List<sp<WebmElement> > &ls) { 65343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih ls.push_back(new WebmUnsigned(kMkvTrackNumber, num)); 66343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih ls.push_back(new WebmUnsigned(kMkvTrackUid, uid)); 67343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih ls.push_back(new WebmUnsigned(kMkvFlagLacing, lacing)); 68343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih ls.push_back(new WebmString(kMkvLanguage, lang)); 69343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih ls.push_back(new WebmString(kMkvCodecId, codec)); 70343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih ls.push_back(new WebmUnsigned(kMkvTrackType, type)); 71343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 72343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 73343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 74343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihnamespace android { 75343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 76343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert ShihWebmElement::WebmElement(uint64_t id, uint64_t size) 77343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih : mId(id), mSize(size) { 78343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 79343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 80343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert ShihWebmElement::~WebmElement() { 81343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 82343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 83343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihint WebmElement::serializePayloadSize(uint8_t *buf) { 84343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return serializeCodedUnsigned(encodeUnsigned(mSize), buf); 85343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 86343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 87343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihuint64_t WebmElement::serializeInto(uint8_t *buf) { 88343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih uint8_t *cur = buf; 89343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih int head = serializeCodedUnsigned(mId, cur); 90343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih cur += head; 91343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih int neck = serializePayloadSize(cur); 92343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih cur += neck; 93343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih serializePayload(cur); 94343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih cur += mSize; 95343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return cur - buf; 96343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 97343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 98343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihuint64_t WebmElement::totalSize() { 99343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih uint8_t buf[8]; 100343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih //............... + sizeOf(encodeUnsigned(size)) 101343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return sizeOf(mId) + serializePayloadSize(buf) + mSize; 102343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 103343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 104343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihuint8_t *WebmElement::serialize(uint64_t& size) { 105343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih size = totalSize(); 106343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih uint8_t *buf = new uint8_t[size]; 107343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih serializeInto(buf); 108343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return buf; 109343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 110343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 111343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihint WebmElement::write(int fd, uint64_t& size) { 112343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih uint8_t buf[8]; 113343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih size = totalSize(); 114343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih off64_t off = ::lseek64(fd, (size - 1), SEEK_CUR) - (size - 1); 115343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih ::write(fd, buf, 1); // extend file 116343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 117343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih off64_t curOff = off + size; 118343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih off64_t alignedOff = off & ~(::sysconf(_SC_PAGE_SIZE) - 1); 119343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih off64_t mapSize = curOff - alignedOff; 120343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih off64_t pageOff = off - alignedOff; 121343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih void *dst = ::mmap64(NULL, mapSize, PROT_WRITE, MAP_SHARED, fd, alignedOff); 122b4a7a2df4c28c3f32b5d877b54831d2cc5d78f81Colin Cross if (dst == MAP_FAILED) { 123343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih ALOGE("mmap64 failed; errno = %d", errno); 124343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih ALOGE("fd %d; flags: %o", fd, ::fcntl(fd, F_GETFL, 0)); 125343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return errno; 126343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } else { 127343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih serializeInto((uint8_t*) dst + pageOff); 128343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih ::msync(dst, mapSize, MS_SYNC); 129343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return ::munmap(dst, mapSize); 130343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 131343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 132343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 133343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih//================================================================================================= 134343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 135343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert ShihWebmUnsigned::WebmUnsigned(uint64_t id, uint64_t value) 136343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih : WebmElement(id, sizeOf(value)), mValue(value) { 137343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 138343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 139343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihvoid WebmUnsigned::serializePayload(uint8_t *buf) { 140343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih serializeCodedUnsigned(mValue, buf); 141343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 142343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 143343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih//================================================================================================= 144343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 145343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert ShihWebmFloat::WebmFloat(uint64_t id, double value) 146343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih : WebmElement(id, sizeof(double)), mValue(value) { 147343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 148343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 149343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert ShihWebmFloat::WebmFloat(uint64_t id, float value) 150343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih : WebmElement(id, sizeof(float)), mValue(value) { 151343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 152343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 153343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihvoid WebmFloat::serializePayload(uint8_t *buf) { 154343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih uint64_t data; 155343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih if (mSize == sizeof(float)) { 156343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih float f = mValue; 157343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih data = *reinterpret_cast<const uint32_t*>(&f); 158343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } else { 159343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih data = *reinterpret_cast<const uint64_t*>(&mValue); 160343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 161343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih for (int i = mSize - 1; i >= 0; --i) { 162343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih buf[i] = data & 0xff; 163343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih data >>= 8; 164343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 165343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 166343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 167343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih//================================================================================================= 168343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 169343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert ShihWebmBinary::WebmBinary(uint64_t id, const sp<ABuffer> &ref) 170343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih : WebmElement(id, ref->size()), mRef(ref) { 171343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 172343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 173343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihvoid WebmBinary::serializePayload(uint8_t *buf) { 174343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih memcpy(buf, mRef->data(), mRef->size()); 175343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 176343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 177343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih//================================================================================================= 178343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 179343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert ShihWebmString::WebmString(uint64_t id, const char *str) 180343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih : WebmElement(id, strlen(str)), mStr(str) { 181343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 182343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 183343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihvoid WebmString::serializePayload(uint8_t *buf) { 184343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih memcpy(buf, mStr, strlen(mStr)); 185343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 186343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 187343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih//================================================================================================= 188343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 189343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert ShihWebmSimpleBlock::WebmSimpleBlock( 190343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih int trackNum, 191343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih int16_t relTimecode, 192343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih bool key, 193343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih const sp<ABuffer>& orig) 194343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih // ............................ trackNum*1 + timecode*2 + flags*1 195343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih // ^^^ 196343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih // Only the least significant byte of trackNum is encoded 197343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih : WebmElement(kMkvSimpleBlock, orig->size() + 4), 198343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih mTrackNum(trackNum), 199343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih mRelTimecode(relTimecode), 200343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih mKey(key), 201343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih mRef(orig) { 202343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 203343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 204343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihvoid WebmSimpleBlock::serializePayload(uint8_t *buf) { 205343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih serializeCodedUnsigned(encodeUnsigned(mTrackNum), buf); 206343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih buf[1] = (mRelTimecode & 0xff00) >> 8; 207343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih buf[2] = mRelTimecode & 0xff; 208343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih buf[3] = mKey ? 0x80 : 0; 209343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih memcpy(buf + 4, mRef->data(), mSize - 4); 210343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 211343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 212343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih//================================================================================================= 213343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 214343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert ShihEbmlVoid::EbmlVoid(uint64_t totalSize) 215343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih : WebmElement(kMkvVoid, voidSize(totalSize)), 216343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih mSizeWidth(totalSize - sizeOf(kMkvVoid) - voidSize(totalSize)) { 217343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih CHECK_GE(voidSize(totalSize), 0); 218343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 219343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 220343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihint EbmlVoid::serializePayloadSize(uint8_t *buf) { 221343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return serializeCodedUnsigned(encodeUnsigned(mSize, mSizeWidth), buf); 222343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 223343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 224343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihvoid EbmlVoid::serializePayload(uint8_t *buf) { 225343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih ::memset(buf, 0, mSize); 226343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return; 227343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 228343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 229343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih//================================================================================================= 230343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 231343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert ShihWebmMaster::WebmMaster(uint64_t id, const List<sp<WebmElement> >& children) 232343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih : WebmElement(id, childrenSum(children)), mChildren(children) { 233343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 234343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 235343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert ShihWebmMaster::WebmMaster(uint64_t id) 236343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih : WebmElement(id, 0) { 237343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 238343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 239343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihint WebmMaster::serializePayloadSize(uint8_t *buf) { 240343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih if (mSize == 0){ 241343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return serializeCodedUnsigned(kMkvUnknownLength, buf); 242343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 243343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return WebmElement::serializePayloadSize(buf); 244343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 245343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 246343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihvoid WebmMaster::serializePayload(uint8_t *buf) { 247343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih uint64_t off = 0; 248343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih for (List<sp<WebmElement> >::const_iterator it = mChildren.begin(); it != mChildren.end(); 249343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih ++it) { 250343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih sp<WebmElement> child = (*it); 251343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih child->serializeInto(buf + off); 252343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih off += child->totalSize(); 253343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 254343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 255343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 256343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih//================================================================================================= 257343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 258343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihsp<WebmElement> WebmElement::CuePointEntry(uint64_t time, int track, uint64_t off) { 259343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih List<sp<WebmElement> > cuePointEntryFields; 260343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih cuePointEntryFields.push_back(new WebmUnsigned(kMkvCueTrack, track)); 261343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih cuePointEntryFields.push_back(new WebmUnsigned(kMkvCueClusterPosition, off)); 262343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih WebmElement *cueTrackPositions = new WebmMaster(kMkvCueTrackPositions, cuePointEntryFields); 263343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 264343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih cuePointEntryFields.clear(); 265343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih cuePointEntryFields.push_back(new WebmUnsigned(kMkvCueTime, time)); 266343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih cuePointEntryFields.push_back(cueTrackPositions); 267343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return new WebmMaster(kMkvCuePoint, cuePointEntryFields); 268343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 269343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 270343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihsp<WebmElement> WebmElement::SeekEntry(uint64_t id, uint64_t off) { 271343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih List<sp<WebmElement> > seekEntryFields; 272343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih seekEntryFields.push_back(new WebmUnsigned(kMkvSeekId, id)); 273343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih seekEntryFields.push_back(new WebmUnsigned(kMkvSeekPosition, off)); 274343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return new WebmMaster(kMkvSeek, seekEntryFields); 275343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 276343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 277343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihsp<WebmElement> WebmElement::EbmlHeader( 278343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih int ver, 279343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih int readVer, 280343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih int maxIdLen, 281343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih int maxSizeLen, 282343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih int docVer, 283343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih int docReadVer) { 284343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih List<sp<WebmElement> > headerFields; 285343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih headerFields.push_back(new WebmUnsigned(kMkvEbmlVersion, ver)); 286343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih headerFields.push_back(new WebmUnsigned(kMkvEbmlReadVersion, readVer)); 287343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih headerFields.push_back(new WebmUnsigned(kMkvEbmlMaxIdlength, maxIdLen)); 288343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih headerFields.push_back(new WebmUnsigned(kMkvEbmlMaxSizeLength, maxSizeLen)); 289343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih headerFields.push_back(new WebmString(kMkvDocType, "webm")); 290343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih headerFields.push_back(new WebmUnsigned(kMkvDocTypeVersion, docVer)); 291343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih headerFields.push_back(new WebmUnsigned(kMkvDocTypeReadVersion, docReadVer)); 292343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return new WebmMaster(kMkvEbml, headerFields); 293343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 294343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 295343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihsp<WebmElement> WebmElement::SegmentInfo(uint64_t scale, double dur) { 296343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih List<sp<WebmElement> > segmentInfo; 297343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih // place duration first; easier to patch 298343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih segmentInfo.push_back(new WebmFloat(kMkvSegmentDuration, dur)); 299343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih segmentInfo.push_back(new WebmUnsigned(kMkvTimecodeScale, scale)); 300343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih segmentInfo.push_back(new WebmString(kMkvMuxingApp, "android")); 301343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih segmentInfo.push_back(new WebmString(kMkvWritingApp, "android")); 302343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return new WebmMaster(kMkvInfo, segmentInfo); 303343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 304343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 305343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihsp<WebmElement> WebmElement::AudioTrackEntry( 306343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih int chans, 307343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih double rate, 308343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih const sp<ABuffer> &buf, 309343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih int bps, 310343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih uint64_t uid, 311343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih bool lacing, 312343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih const char *lang) { 313343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih if (uid == 0) { 314343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih uid = kAudioTrackNum; 315343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 316343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 317343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih List<sp<WebmElement> > trackEntryFields; 318343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih populateCommonTrackEntries( 319343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih kAudioTrackNum, 320343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih uid, 321343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih lacing, 322343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih lang, 323343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih "A_VORBIS", 324343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih kAudioType, 325343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih trackEntryFields); 326343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 327343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih List<sp<WebmElement> > audioInfo; 328343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih audioInfo.push_back(new WebmUnsigned(kMkvChannels, chans)); 329343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih audioInfo.push_back(new WebmFloat(kMkvSamplingFrequency, rate)); 330343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih if (bps) { 331343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih WebmElement *bitDepth = new WebmUnsigned(kMkvBitDepth, bps); 332343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih audioInfo.push_back(bitDepth); 333343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 334343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 335343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih trackEntryFields.push_back(new WebmMaster(kMkvAudio, audioInfo)); 336343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih trackEntryFields.push_back(new WebmBinary(kMkvCodecPrivate, buf)); 337343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return new WebmMaster(kMkvTrackEntry, trackEntryFields); 338343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 339343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 340343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihsp<WebmElement> WebmElement::VideoTrackEntry( 341343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih uint64_t width, 342343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih uint64_t height, 343343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih uint64_t uid, 344343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih bool lacing, 345343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih const char *lang) { 346343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih if (uid == 0) { 347343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih uid = kVideoTrackNum; 348343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 349343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 350343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih List<sp<WebmElement> > trackEntryFields; 351343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih populateCommonTrackEntries( 352343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih kVideoTrackNum, 353343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih uid, 354343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih lacing, 355343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih lang, 356343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih "V_VP8", 357343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih kVideoType, 358343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih trackEntryFields); 359343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 360343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih List<sp<WebmElement> > videoInfo; 361343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih videoInfo.push_back(new WebmUnsigned(kMkvPixelWidth, width)); 362343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih videoInfo.push_back(new WebmUnsigned(kMkvPixelHeight, height)); 363343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 364343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih trackEntryFields.push_back(new WebmMaster(kMkvVideo, videoInfo)); 365343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return new WebmMaster(kMkvTrackEntry, trackEntryFields); 366343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} 367343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} /* namespace android */ 368