Home | History | Annotate | Download | only in libmkv
      1 // Copyright (c) 2010 The WebM project authors. All Rights Reserved.
      2 //
      3 // Use of this source code is governed by a BSD-style license
      4 // that can be found in the LICENSE file in the root of the source
      5 // tree. An additional intellectual property rights grant can be found
      6 // in the file PATENTS.  All contributing project authors may
      7 // be found in the AUTHORS file in the root of the source tree.
      8 
      9 
     10 #include "EbmlWriter.h"
     11 #include <stdlib.h>
     12 #include <wchar.h>
     13 #include <string.h>
     14 #if defined(_MSC_VER)
     15 #define LITERALU64(n) n
     16 #else
     17 #define LITERALU64(n) n##LLU
     18 #endif
     19 
     20 void Ebml_WriteLen(EbmlGlobal *glob, long long val)
     21 {
     22     //TODO check and make sure we are not > than 0x0100000000000000LLU
     23     unsigned char size = 8; //size in bytes to output
     24     unsigned long long minVal = LITERALU64(0x00000000000000ff); //mask to compare for byte size
     25 
     26     for (size = 1; size < 8; size ++)
     27     {
     28         if (val < minVal)
     29             break;
     30 
     31         minVal = (minVal << 7);
     32     }
     33 
     34     val |= (LITERALU64(0x000000000000080) << ((size - 1) * 7));
     35 
     36     Ebml_Serialize(glob, (void *) &val, size);
     37 }
     38 
     39 void Ebml_WriteString(EbmlGlobal *glob, const char *str)
     40 {
     41     const size_t size_ = strlen(str);
     42     const unsigned long long  size = size_;
     43     Ebml_WriteLen(glob, size);
     44     //TODO: it's not clear from the spec whether the nul terminator
     45     //should be serialized too.  For now we omit the null terminator.
     46     Ebml_Write(glob, str, size);
     47 }
     48 
     49 void Ebml_WriteUTF8(EbmlGlobal *glob, const wchar_t *wstr)
     50 {
     51     const size_t strlen = wcslen(wstr);
     52 
     53     //TODO: it's not clear from the spec whether the nul terminator
     54     //should be serialized too.  For now we include it.
     55     const unsigned long long  size = strlen;
     56 
     57     Ebml_WriteLen(glob, size);
     58     Ebml_Write(glob, wstr, size);
     59 }
     60 
     61 void Ebml_WriteID(EbmlGlobal *glob, unsigned long class_id)
     62 {
     63     if (class_id >= 0x01000000)
     64         Ebml_Serialize(glob, (void *)&class_id, 4);
     65     else if (class_id >= 0x00010000)
     66         Ebml_Serialize(glob, (void *)&class_id, 3);
     67     else if (class_id >= 0x00000100)
     68         Ebml_Serialize(glob, (void *)&class_id, 2);
     69     else
     70         Ebml_Serialize(glob, (void *)&class_id, 1);
     71 }
     72 void Ebml_SerializeUnsigned64(EbmlGlobal *glob, unsigned long class_id, uint64_t ui)
     73 {
     74     unsigned char sizeSerialized = 8 | 0x80;
     75     Ebml_WriteID(glob, class_id);
     76     Ebml_Serialize(glob, &sizeSerialized, 1);
     77     Ebml_Serialize(glob, &ui, 8);
     78 }
     79 
     80 void Ebml_SerializeUnsigned(EbmlGlobal *glob, unsigned long class_id, unsigned long ui)
     81 {
     82     unsigned char size = 8; //size in bytes to output
     83     unsigned char sizeSerialized = 0;
     84     unsigned long minVal;
     85 
     86     Ebml_WriteID(glob, class_id);
     87     minVal = 0x7fLU; //mask to compare for byte size
     88 
     89     for (size = 1; size < 4; size ++)
     90     {
     91         if (ui < minVal)
     92         {
     93             break;
     94         }
     95 
     96         minVal <<= 7;
     97     }
     98 
     99     sizeSerialized = 0x80 | size;
    100     Ebml_Serialize(glob, &sizeSerialized, 1);
    101     Ebml_Serialize(glob, &ui, size);
    102 }
    103 //TODO: perhaps this is a poor name for this id serializer helper function
    104 void Ebml_SerializeBinary(EbmlGlobal *glob, unsigned long class_id, unsigned long bin)
    105 {
    106     int size;
    107     for (size=4; size > 1; size--)
    108     {
    109         if (bin & 0x000000ff << ((size-1) * 8))
    110             break;
    111     }
    112     Ebml_WriteID(glob, class_id);
    113     Ebml_WriteLen(glob, size);
    114     Ebml_WriteID(glob, bin);
    115 }
    116 
    117 void Ebml_SerializeFloat(EbmlGlobal *glob, unsigned long class_id, double d)
    118 {
    119     unsigned char len = 0x88;
    120 
    121     Ebml_WriteID(glob, class_id);
    122     Ebml_Serialize(glob, &len, 1);
    123     Ebml_Serialize(glob,  &d, 8);
    124 }
    125 
    126 void Ebml_WriteSigned16(EbmlGlobal *glob, short val)
    127 {
    128     signed long out = ((val & 0x003FFFFF) | 0x00200000) << 8;
    129     Ebml_Serialize(glob, &out, 3);
    130 }
    131 
    132 void Ebml_SerializeString(EbmlGlobal *glob, unsigned long class_id, const char *s)
    133 {
    134     Ebml_WriteID(glob, class_id);
    135     Ebml_WriteString(glob, s);
    136 }
    137 
    138 void Ebml_SerializeUTF8(EbmlGlobal *glob, unsigned long class_id, wchar_t *s)
    139 {
    140     Ebml_WriteID(glob,  class_id);
    141     Ebml_WriteUTF8(glob,  s);
    142 }
    143 
    144 void Ebml_SerializeData(EbmlGlobal *glob, unsigned long class_id, unsigned char *data, unsigned long data_length)
    145 {
    146     unsigned char size = 4;
    147     Ebml_WriteID(glob, class_id);
    148     Ebml_WriteLen(glob, data_length);
    149     Ebml_Write(glob,  data, data_length);
    150 }
    151 
    152 void Ebml_WriteVoid(EbmlGlobal *glob, unsigned long vSize)
    153 {
    154     unsigned char tmp = 0;
    155     unsigned long i = 0;
    156 
    157     Ebml_WriteID(glob, 0xEC);
    158     Ebml_WriteLen(glob, vSize);
    159 
    160     for (i = 0; i < vSize; i++)
    161     {
    162         Ebml_Write(glob, &tmp, 1);
    163     }
    164 }
    165 
    166 //TODO Serialize Date
    167