media/libmkv/EbmlBufferWriter.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libmkv/EbmlBufferWriter.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,79 @@
     1.4 +// #include <strmif.h>
     1.5 +#include "EbmlBufferWriter.h"
     1.6 +#include "EbmlWriter.h"
     1.7 +// #include <cassert>
     1.8 +// #include <limits>
     1.9 +// #include <malloc.h>  //_alloca
    1.10 +#include <stdlib.h>
    1.11 +#include <wchar.h>
    1.12 +#include <string.h>
    1.13 +
    1.14 +void
    1.15 +Ebml_Serialize(EbmlGlobal *glob, const void *buffer_in, int buffer_size, unsigned long len)
    1.16 +{
    1.17 +  /* buffer_size:
    1.18 +   * 1 - int8_t;
    1.19 +   * 2 - int16_t;
    1.20 +   * 3 - int32_t;
    1.21 +   * 4 - int64_t;
    1.22 +   */
    1.23 +  long i;
    1.24 +  for(i = len-1; i >= 0; i--) {
    1.25 +    unsigned char x;
    1.26 +    if (buffer_size == 1) {
    1.27 +      x = (char)(*(const int8_t *)buffer_in >> (i * 8));
    1.28 +	} else if (buffer_size == 2) {
    1.29 +      x = (char)(*(const int16_t *)buffer_in >> (i * 8));
    1.30 +	} else if (buffer_size == 4) {
    1.31 +      x = (char)(*(const int32_t *)buffer_in >> (i * 8));
    1.32 +	} else if (buffer_size == 8) {
    1.33 +      x = (char)(*(const int64_t *)buffer_in >> (i * 8));
    1.34 +	}
    1.35 +    Ebml_Write(glob, &x, 1);
    1.36 +  }
    1.37 +}
    1.38 +
    1.39 +void Ebml_Write(EbmlGlobal *glob, const void *buffer_in, unsigned long len) {
    1.40 +  unsigned char *src = glob->buf;
    1.41 +  src += glob->offset;
    1.42 +  memcpy(src, buffer_in, len);
    1.43 +  glob->offset += len;
    1.44 +}
    1.45 +
    1.46 +static void _Serialize(EbmlGlobal *glob, const unsigned char *p, const unsigned char *q) {
    1.47 +  while (q != p) {
    1.48 +    --q;
    1.49 +
    1.50 +    memcpy(&(glob->buf[glob->offset]), q, 1);
    1.51 +    glob->offset++;
    1.52 +  }
    1.53 +}
    1.54 +
    1.55 +/*
    1.56 +void Ebml_Serialize(EbmlGlobal *glob, const void *buffer_in, unsigned long len) {
    1.57 +  // assert(buf);
    1.58 +
    1.59 +  const unsigned char *const p = (const unsigned char *)(buffer_in);
    1.60 +  const unsigned char *const q = p + len;
    1.61 +
    1.62 +  _Serialize(glob, p, q);
    1.63 +}
    1.64 +*/
    1.65 +
    1.66 +void Ebml_StartSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc, unsigned long class_id) {
    1.67 +  unsigned long long unknownLen = 0x01FFFFFFFFFFFFFFLL;
    1.68 +  Ebml_WriteID(glob, class_id);
    1.69 +  ebmlLoc->offset = glob->offset;
    1.70 +  // todo this is always taking 8 bytes, this may need later optimization
    1.71 +  Ebml_Serialize(glob, (void *)&unknownLen,sizeof(unknownLen), 8); // this is a key that says lenght unknown
    1.72 +}
    1.73 +
    1.74 +void Ebml_EndSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc) {
    1.75 +  unsigned long long size = glob->offset - ebmlLoc->offset - 8;
    1.76 +  unsigned long long curOffset = glob->offset;
    1.77 +  glob->offset = ebmlLoc->offset;
    1.78 +  size |=  0x0100000000000000LL;
    1.79 +  Ebml_Serialize(glob, &size,sizeof(size), 8);
    1.80 +  glob->offset = curOffset;
    1.81 +}
    1.82 +

mercurial