1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libmkv/EbmlWriter.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,165 @@ 1.4 +/* 1.5 + * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license 1.8 + * that can be found in the LICENSE file in the root of the source 1.9 + * tree. An additional intellectual property rights grant can be found 1.10 + * in the file PATENTS. All contributing project authors may 1.11 + * be found in the AUTHORS file in the root of the source tree. 1.12 + */ 1.13 +#include "EbmlWriter.h" 1.14 +#include <stdlib.h> 1.15 +#include <wchar.h> 1.16 +#include <string.h> 1.17 +#include <limits.h> 1.18 +#include "EbmlBufferWriter.h" 1.19 +#if defined(_MSC_VER) 1.20 +#define LITERALU64(n) n 1.21 +#else 1.22 +#define LITERALU64(n) n##LLU 1.23 +#endif 1.24 + 1.25 +void Ebml_WriteLen(EbmlGlobal *glob, int64_t val) { 1.26 + /* TODO check and make sure we are not > than 0x0100000000000000LLU */ 1.27 + unsigned char size = 8; /* size in bytes to output */ 1.28 + 1.29 + /* mask to compare for byte size */ 1.30 + int64_t minVal = 0xff; 1.31 + 1.32 + for (size = 1; size < 8; size ++) { 1.33 + if (val < minVal) 1.34 + break; 1.35 + 1.36 + minVal = (minVal << 7); 1.37 + } 1.38 + 1.39 + val |= (((uint64_t)0x80) << ((size - 1) * 7)); 1.40 + 1.41 + Ebml_Serialize(glob, (void *) &val, sizeof(val), size); 1.42 +} 1.43 + 1.44 +void Ebml_WriteString(EbmlGlobal *glob, const char *str) { 1.45 + const size_t size_ = strlen(str); 1.46 + const uint64_t size = size_; 1.47 + Ebml_WriteLen(glob, size); 1.48 + /* TODO: it's not clear from the spec whether the nul terminator 1.49 + * should be serialized too. For now we omit the null terminator. 1.50 + */ 1.51 + Ebml_Write(glob, str, (unsigned long)size); 1.52 +} 1.53 + 1.54 +void Ebml_WriteUTF8(EbmlGlobal *glob, const wchar_t *wstr) { 1.55 + const size_t strlen = wcslen(wstr); 1.56 + 1.57 + /* TODO: it's not clear from the spec whether the nul terminator 1.58 + * should be serialized too. For now we include it. 1.59 + */ 1.60 + const uint64_t size = strlen; 1.61 + 1.62 + Ebml_WriteLen(glob, size); 1.63 + Ebml_Write(glob, wstr, (unsigned long)size); 1.64 +} 1.65 + 1.66 +void Ebml_WriteID(EbmlGlobal *glob, unsigned long class_id) { 1.67 + int len; 1.68 + 1.69 + if (class_id >= 0x01000000) 1.70 + len = 4; 1.71 + else if (class_id >= 0x00010000) 1.72 + len = 3; 1.73 + else if (class_id >= 0x00000100) 1.74 + len = 2; 1.75 + else 1.76 + len = 1; 1.77 + 1.78 + Ebml_Serialize(glob, (void *)&class_id, sizeof(class_id), len); 1.79 +} 1.80 + 1.81 +void Ebml_SerializeUnsigned32(EbmlGlobal *glob, unsigned long class_id, uint32_t ui) { 1.82 + unsigned char sizeSerialized = 8 | 0x80; 1.83 + Ebml_WriteID(glob, class_id); 1.84 + Ebml_Serialize(glob, &sizeSerialized, sizeof(sizeSerialized), 1); 1.85 + Ebml_Serialize(glob, &ui, sizeof(ui), 4); 1.86 +} 1.87 + 1.88 +void Ebml_SerializeUnsigned64(EbmlGlobal *glob, unsigned long class_id, uint64_t ui) { 1.89 + unsigned char sizeSerialized = 8 | 0x80; 1.90 + Ebml_WriteID(glob, class_id); 1.91 + Ebml_Serialize(glob, &sizeSerialized, sizeof(sizeSerialized), 1); 1.92 + Ebml_Serialize(glob, &ui, sizeof(ui), 8); 1.93 +} 1.94 + 1.95 +void Ebml_SerializeUnsigned(EbmlGlobal *glob, unsigned long class_id, unsigned long ui) { 1.96 + unsigned char size = 8; /* size in bytes to output */ 1.97 + unsigned char sizeSerialized = 0; 1.98 + unsigned long minVal; 1.99 + 1.100 + Ebml_WriteID(glob, class_id); 1.101 + minVal = 0x7fLU; /* mask to compare for byte size */ 1.102 + 1.103 + for (size = 1; size < 4; size ++) { 1.104 + if (ui < minVal) { 1.105 + break; 1.106 + } 1.107 + 1.108 + minVal <<= 7; 1.109 + } 1.110 + 1.111 + sizeSerialized = 0x80 | size; 1.112 + Ebml_Serialize(glob, &sizeSerialized, sizeof(sizeSerialized), 1); 1.113 + Ebml_Serialize(glob, &ui, sizeof(ui), size); 1.114 +} 1.115 +/* TODO: perhaps this is a poor name for this id serializer helper function */ 1.116 +void Ebml_SerializeBinary(EbmlGlobal *glob, unsigned long class_id, unsigned long bin) { 1.117 + int size; 1.118 + for (size = 4; size > 1; size--) { 1.119 + if (bin & (unsigned int)0x000000ff << ((size - 1) * 8)) 1.120 + break; 1.121 + } 1.122 + Ebml_WriteID(glob, class_id); 1.123 + Ebml_WriteLen(glob, size); 1.124 + Ebml_WriteID(glob, bin); 1.125 +} 1.126 + 1.127 +void Ebml_SerializeFloat(EbmlGlobal *glob, unsigned long class_id, double d) { 1.128 + unsigned char len = 0x88; 1.129 + 1.130 + Ebml_WriteID(glob, class_id); 1.131 + Ebml_Serialize(glob, &len, sizeof(len), 1); 1.132 + Ebml_Serialize(glob, &d, sizeof(d), 8); 1.133 +} 1.134 + 1.135 +void Ebml_WriteSigned16(EbmlGlobal *glob, short val) { 1.136 + signed long out = ((val & 0x003FFFFF) | 0x00200000) << 8; 1.137 + Ebml_Serialize(glob, &out, sizeof(out), 3); 1.138 +} 1.139 + 1.140 +void Ebml_SerializeString(EbmlGlobal *glob, unsigned long class_id, const char *s) { 1.141 + Ebml_WriteID(glob, class_id); 1.142 + Ebml_WriteString(glob, s); 1.143 +} 1.144 + 1.145 +void Ebml_SerializeUTF8(EbmlGlobal *glob, unsigned long class_id, wchar_t *s) { 1.146 + Ebml_WriteID(glob, class_id); 1.147 + Ebml_WriteUTF8(glob, s); 1.148 +} 1.149 + 1.150 +void Ebml_SerializeData(EbmlGlobal *glob, unsigned long class_id, unsigned char *data, unsigned long data_length) { 1.151 + Ebml_WriteID(glob, class_id); 1.152 + Ebml_WriteLen(glob, data_length); 1.153 + Ebml_Write(glob, data, data_length); 1.154 +} 1.155 + 1.156 +void Ebml_WriteVoid(EbmlGlobal *glob, unsigned long vSize) { 1.157 + unsigned char tmp = 0; 1.158 + unsigned long i = 0; 1.159 + 1.160 + Ebml_WriteID(glob, 0xEC); 1.161 + Ebml_WriteLen(glob, vSize); 1.162 + 1.163 + for (i = 0; i < vSize; i++) { 1.164 + Ebml_Write(glob, &tmp, 1); 1.165 + } 1.166 +} 1.167 + 1.168 +/* TODO Serialize Date */