Index: trunk/src/VBox/Main/src-client/EbmlWriter.cpp
===================================================================
--- trunk/src/VBox/Main/src-client/EbmlWriter.cpp	(revision 45914)
+++ trunk/src/VBox/Main/src-client/EbmlWriter.cpp	(revision 45922)
@@ -28,21 +28,39 @@
 
 #include "EbmlWriter.h"
+#include <iprt/asm.h>
 #include <iprt/mem.h>
 #include <iprt/string.h>
 #include <VBox/log.h>
 
-
-int Ebml_Write(EbmlGlobal *glob, const void *buffer_in, uint64_t len)
-{
-    return RTFileWrite(glob->file, buffer_in, len, NULL);
-}
-
-int Ebml_Serialize(EbmlGlobal *glob, const void *buffer_in, uint64_t len)
-{
-    const unsigned char *q = (const unsigned char *)buffer_in + len - 1;
-
-    for (; len; len--)
-    {
-        int rc = Ebml_Write(glob, q--, 1);
+static int ebml_Write(EbmlGlobal *glob, const void *pv, size_t cb)
+{
+    return RTFileWrite(glob->file, pv, cb, NULL);
+}
+
+static int ebml_WriteU8(EbmlGlobal *glob, uint8_t u8)
+{
+    return ebml_Write(glob, &u8, 1);
+}
+
+static int ebml_WriteU16(EbmlGlobal *glob, uint16_t u16)
+{
+    return ebml_Write(glob, &u16, 2);
+}
+
+static int ebml_WriteU32(EbmlGlobal *glob, uint32_t u32)
+{
+    return ebml_Write(glob, &u32, 4);
+}
+
+static int ebml_WriteU64(EbmlGlobal *glob, uint64_t u64)
+{
+    return ebml_Write(glob, &u64, 8);
+}
+
+static int ebml_Serialize(EbmlGlobal *glob, const uint8_t *pb, size_t cb)
+{
+    for (; cb; cb--)
+    {
+        int rc = ebml_WriteU8(glob, pb[cb-1]);
         if (RT_FAILURE(rc))
             return rc;
@@ -51,15 +69,28 @@
 }
 
-int Ebml_SerializeUnsigned32(EbmlGlobal *glob, uint32_t class_id, uint64_t ui)
-{
-    unsigned char sizeSerialized = 4 | 0x80;
-    int rc = Ebml_WriteID(glob, class_id);
-    rc = Ebml_Serialize(glob, &sizeSerialized, 1);
-    if (RT_SUCCESS(rc))
-        rc = Ebml_Serialize(glob, &ui, 4);
-    return rc;
-}
-
-int Ebml_StartSubElement(EbmlGlobal *glob, uint64_t *ebmlLoc, uint32_t class_id)
+static int ebml_WriteID(EbmlGlobal *glob, uint32_t class_id)
+{
+    int rc;
+    if (class_id >= 0x01000000)
+        rc = ebml_WriteU32(glob, RT_H2BE_U32(class_id));
+    else if (class_id >= 0x00010000)
+        rc = ebml_Serialize(glob, (uint8_t*)&class_id, 3);
+    else if (class_id >= 0x00000100)
+        rc = ebml_WriteU16(glob, RT_H2BE_U16((uint16_t)class_id));
+    else
+        rc = ebml_WriteU8(glob, (uint8_t)class_id);
+    return rc;
+}
+
+static int ebml_SerializeUnsigned32(EbmlGlobal *glob, uint32_t class_id, uint32_t ui)
+{
+    int rc = ebml_WriteID(glob, class_id);
+    rc = ebml_WriteU8(glob, 4 | 0x80);
+    if (RT_SUCCESS(rc))
+        rc = ebml_WriteU32(glob, RT_H2BE_U32(ui));
+    return rc;
+}
+
+static int ebml_StartSubElement(EbmlGlobal *glob, uint64_t *ebmlLoc, uint32_t class_id)
 {
     // todo this is always taking 8 bytes, this may need later optimization
@@ -67,10 +98,10 @@
     uint64_t unknownLen =  UINT64_C(0x01FFFFFFFFFFFFFF);
 
-    Ebml_WriteID(glob, class_id);
+    ebml_WriteID(glob, class_id);
     *ebmlLoc = RTFileTell(glob->file);
-    return Ebml_Serialize(glob, &unknownLen, 8);
-}
-
-int Ebml_EndSubElement(EbmlGlobal *glob, uint64_t *ebmlLoc)
+    return ebml_WriteU64(glob, RT_H2BE_U64(unknownLen));
+}
+
+static int ebml_EndSubElement(EbmlGlobal *glob, uint64_t ebmlLoc)
 {
     /* Save the current file pointer */
@@ -78,10 +109,10 @@
 
     /* Calculate the size of this element */
-    uint64_t size  = pos - *ebmlLoc - 8;
+    uint64_t size  = pos - ebmlLoc - 8;
     size |= UINT64_C(0x0100000000000000);
 
     /* Seek back to the beginning of the element and write the new size */
-    RTFileSeek(glob->file, *ebmlLoc, RTFILE_SEEK_BEGIN, NULL);
-    int rc = Ebml_Serialize(glob, &size, 8);
+    RTFileSeek(glob->file, ebmlLoc, RTFILE_SEEK_BEGIN, NULL);
+    int rc = ebml_WriteU64(glob, RT_H2BE_U64(size));
 
     /* Reset the file pointer */
@@ -91,8 +122,8 @@
 }
 
-int Ebml_WriteLen(EbmlGlobal *glob, uint64_t val)
+static int ebml_WriteLen(EbmlGlobal *glob, uint64_t val)
 {
     //TODO check and make sure we are not > than 0x0100000000000000LLU
-    unsigned char size = 8; //size in bytes to output
+    size_t size = 8;
     uint64_t minVal = UINT64_C(0x00000000000000ff); //mask to compare for byte size
 
@@ -107,40 +138,25 @@
     val |= (UINT64_C(0x000000000000080) << ((size - 1) * 7));
 
-    return Ebml_Serialize(glob, (void *) &val, size);
-}
-
-int Ebml_WriteString(EbmlGlobal *glob, const char *str)
-{
-    const size_t size_ = strlen(str);
-    const uint64_t size = size_;
-    int rc = Ebml_WriteLen(glob, size);
+    return ebml_Serialize(glob, (uint8_t *)&val, size);
+}
+
+static int ebml_WriteString(EbmlGlobal *glob, const char *str)
+{
+    const size_t cb = strlen(str);
+    int rc = ebml_WriteLen(glob, cb);
     //TODO: it's not clear from the spec whether the nul terminator
     //should be serialized too.  For now we omit the null terminator.
     if (RT_SUCCESS(rc))
-        rc = Ebml_Write(glob, str, size);
-    return rc;
-}
-
-int Ebml_WriteID(EbmlGlobal *glob, uint32_t class_id)
-{
-    int rc;
-    if (class_id >= 0x01000000)
-        rc = Ebml_Serialize(glob, (void *)&class_id, 4);
-    else if (class_id >= 0x00010000)
-        rc = Ebml_Serialize(glob, (void *)&class_id, 3);
-    else if (class_id >= 0x00000100)
-        rc = Ebml_Serialize(glob, (void *)&class_id, 2);
-    else
-        rc = Ebml_Serialize(glob, (void *)&class_id, 1);
-    return rc;
-}
+        rc = ebml_Write(glob, str, cb);
+    return rc;
+}
+
 int Ebml_SerializeUnsigned64(EbmlGlobal *glob, uint32_t class_id, uint64_t ui)
 {
-    unsigned char sizeSerialized = 8 | 0x80;
-    int rc = Ebml_WriteID(glob, class_id);
-    if (RT_SUCCESS(rc))
-        rc = Ebml_Serialize(glob, &sizeSerialized, 1);
-    if (RT_SUCCESS(rc))
-        rc = Ebml_Serialize(glob, &ui, 8);
+    int rc = ebml_WriteID(glob, class_id);
+    if (RT_SUCCESS(rc))
+        rc = ebml_WriteU8(glob, 8 | 0x80);
+    if (RT_SUCCESS(rc))
+        rc = ebml_WriteU64(glob, RT_H2BE_U64(ui));
     return rc;
 }
@@ -148,13 +164,10 @@
 int Ebml_SerializeUnsigned(EbmlGlobal *glob, uint32_t class_id, uint32_t ui)
 {
-    unsigned char size = 8; //size in bytes to output
-    unsigned char sizeSerialized = 0;
-    uint32_t minVal;
-
-    int rc = Ebml_WriteID(glob, class_id);
+    int rc = ebml_WriteID(glob, class_id);
     if (RT_FAILURE(rc))
         return rc;
-    minVal = 0x7fLU; //mask to compare for byte size
-
+
+    uint32_t minVal = 0x7fLU; //mask to compare for byte size
+    size_t size = 8; //size in bytes to output
     for (size = 1; size < 4; size ++)
     {
@@ -165,8 +178,7 @@
     }
 
-    sizeSerialized = 0x80 | size;
-    rc = Ebml_Serialize(glob, &sizeSerialized, 1);
-    if (RT_SUCCESS(rc))
-        rc = Ebml_Serialize(glob, &ui, size);
+    rc = ebml_WriteU8(glob, 0x80 | size);
+    if (RT_SUCCESS(rc))
+        rc = ebml_Serialize(glob, (uint8_t*)&ui, size);
     return rc;
 }
@@ -181,9 +193,9 @@
             break;
     }
-    int rc = Ebml_WriteID(glob, class_id);
-    if (RT_SUCCESS(rc))
-        rc = Ebml_WriteLen(glob, size);
-    if (RT_SUCCESS(rc))
-        rc = Ebml_WriteID(glob, bin);
+    int rc = ebml_WriteID(glob, class_id);
+    if (RT_SUCCESS(rc))
+        rc = ebml_WriteLen(glob, size);
+    if (RT_SUCCESS(rc))
+        rc = ebml_WriteID(glob, bin);
     return rc;
 }
@@ -191,11 +203,9 @@
 int Ebml_SerializeFloat(EbmlGlobal *glob, uint32_t class_id, double d)
 {
-    unsigned char len = 0x88;
-
-    int rc = Ebml_WriteID(glob, class_id);
-    if (RT_SUCCESS(rc))
-        rc = Ebml_Serialize(glob, &len, 1);
-    if (RT_SUCCESS(rc))
-        rc = Ebml_Serialize(glob,  &d, 8);
+    int rc = ebml_WriteID(glob, class_id);
+    if (RT_SUCCESS(rc))
+        rc = ebml_WriteU8(glob, 0x80 | 8);
+    if (RT_SUCCESS(rc))
+        rc = ebml_WriteU64(glob, RT_H2BE_U64(*(uint64_t*)&d));
     return rc;
 }
@@ -203,7 +213,7 @@
 int Ebml_SerializeString(EbmlGlobal *glob, uint32_t class_id, const char *s)
 {
-    int rc = Ebml_WriteID(glob, class_id);
-    if (RT_SUCCESS(rc))
-        rc = Ebml_WriteString(glob, s);
+    int rc = ebml_WriteID(glob, class_id);
+    if (RT_SUCCESS(rc))
+        rc = ebml_WriteString(glob, s);
     return rc;
 }
@@ -213,5 +223,5 @@
     uint64_t offset = pos - ebml->position_reference;
     uint64_t start;
-    int rc = Ebml_StartSubElement(ebml, &start, Seek);
+    int rc = ebml_StartSubElement(ebml, &start, Seek);
     if (RT_SUCCESS(rc))
         rc = Ebml_SerializeBinary(ebml, SeekID, id);
@@ -219,5 +229,5 @@
         rc = Ebml_SerializeUnsigned64(ebml, SeekPosition, offset);
     if (RT_SUCCESS(rc))
-        rc = Ebml_EndSubElement(ebml, &start);
+        rc = ebml_EndSubElement(ebml, start);
     return rc;
 }
@@ -225,9 +235,8 @@
 int Ebml_WriteWebMSeekInfo(EbmlGlobal *ebml)
 {
-    uint64_t pos;
     int rc = VINF_SUCCESS;
 
     /* Save the current file pointer */
-    pos = RTFileTell(ebml->file);
+    uint64_t pos = RTFileTell(ebml->file);
 
     if (ebml->seek_info_pos)
@@ -240,13 +249,13 @@
 
         if (RT_SUCCESS(rc))
-            rc = Ebml_StartSubElement(ebml, &start, SeekHead);
+            rc = ebml_StartSubElement(ebml, &start, SeekHead);
         if (RT_SUCCESS(rc))
             rc = Ebml_WriteWebMSeekElement(ebml, Tracks, ebml->track_pos);
         if (RT_SUCCESS(rc))
-            rc = Ebml_WriteWebMSeekElement(ebml, Cues,   ebml->cue_pos);
-        if (RT_SUCCESS(rc))
-            rc = Ebml_WriteWebMSeekElement(ebml, Info,   ebml->segment_info_pos);
-        if (RT_SUCCESS(rc))
-            rc = Ebml_EndSubElement(ebml, &start);
+            rc = Ebml_WriteWebMSeekElement(ebml, Cues, ebml->cue_pos);
+        if (RT_SUCCESS(rc))
+            rc = Ebml_WriteWebMSeekElement(ebml, Info, ebml->segment_info_pos);
+        if (RT_SUCCESS(rc))
+            rc = ebml_EndSubElement(ebml, start);
     }
     {
@@ -255,9 +264,8 @@
         uint64_t frame_time;
 
-        frame_time = (uint64_t)1000 * ebml->framerate.den
-                     / ebml->framerate.num;
+        frame_time = (uint64_t)1000 * ebml->framerate.den / ebml->framerate.num;
         ebml->segment_info_pos = RTFileTell(ebml->file);
         if (RT_SUCCESS(rc))
-            rc = Ebml_StartSubElement(ebml, &startInfo, Info);
+            rc = ebml_StartSubElement(ebml, &startInfo, Info);
         if (RT_SUCCESS(rc))
             rc = Ebml_SerializeUnsigned(ebml, TimecodeScale, 1000000);
@@ -269,9 +277,9 @@
                     ebml->debug ? vpx_codec_version_str() : "");
         if (RT_SUCCESS(rc))
-            rc = Ebml_SerializeString(ebml, 0x4D80, szVersion);
-        if (RT_SUCCESS(rc))
-            rc = Ebml_SerializeString(ebml, 0x5741, szVersion);
-        if (RT_SUCCESS(rc))
-            rc = Ebml_EndSubElement(ebml, &startInfo);
+            rc = Ebml_SerializeString(ebml, MuxingApp, szVersion);
+        if (RT_SUCCESS(rc))
+            rc = Ebml_SerializeString(ebml, WritingApp, szVersion);
+        if (RT_SUCCESS(rc))
+            rc = ebml_EndSubElement(ebml, startInfo);
     }
     return rc;
@@ -285,25 +293,25 @@
     {
         uint64_t start;
-        rc = Ebml_StartSubElement(glob, &start, EBML);
+        rc = ebml_StartSubElement(glob, &start, EBML);
         if (RT_SUCCESS(rc))
             rc = Ebml_SerializeUnsigned(glob, EBMLVersion, 1);
         if (RT_SUCCESS(rc))
-            rc = Ebml_SerializeUnsigned(glob, EBMLReadVersion, 1); //EBML Read Version
-        if (RT_SUCCESS(rc))
-            rc = Ebml_SerializeUnsigned(glob, EBMLMaxIDLength, 4); //EBML Max ID Length
-        if (RT_SUCCESS(rc))
-            rc = Ebml_SerializeUnsigned(glob, EBMLMaxSizeLength, 8); //EBML Max Size Length
-        if (RT_SUCCESS(rc))
-            rc = Ebml_SerializeString(glob, DocType, "webm"); //Doc Type
-        if (RT_SUCCESS(rc))
-            rc = Ebml_SerializeUnsigned(glob, DocTypeVersion, 2); //Doc Type Version
-        if (RT_SUCCESS(rc))
-            rc = Ebml_SerializeUnsigned(glob, DocTypeReadVersion, 2); //Doc Type Read Version
-        if (RT_SUCCESS(rc))
-            rc = Ebml_EndSubElement(glob, &start);
-    }
-    {
-        if (RT_SUCCESS(rc))
-            rc = Ebml_StartSubElement(glob, &glob->startSegment, Segment); //segment
+            rc = Ebml_SerializeUnsigned(glob, EBMLReadVersion, 1);
+        if (RT_SUCCESS(rc))
+            rc = Ebml_SerializeUnsigned(glob, EBMLMaxIDLength, 4);
+        if (RT_SUCCESS(rc))
+            rc = Ebml_SerializeUnsigned(glob, EBMLMaxSizeLength, 8);
+        if (RT_SUCCESS(rc))
+            rc = Ebml_SerializeString(glob, DocType, "webm");
+        if (RT_SUCCESS(rc))
+            rc = Ebml_SerializeUnsigned(glob, DocTypeVersion, 2);
+        if (RT_SUCCESS(rc))
+            rc = Ebml_SerializeUnsigned(glob, DocTypeReadVersion, 2);
+        if (RT_SUCCESS(rc))
+            rc = ebml_EndSubElement(glob, start);
+    }
+    {
+        if (RT_SUCCESS(rc))
+            rc = ebml_StartSubElement(glob, &glob->startSegment, Segment);
         glob->position_reference = RTFileTell(glob->file);
         glob->framerate = *fps;
@@ -314,40 +322,39 @@
             glob->track_pos = RTFileTell(glob->file);
             if (RT_SUCCESS(rc))
-                rc = Ebml_StartSubElement(glob, &trackStart, Tracks);
+                rc = ebml_StartSubElement(glob, &trackStart, Tracks);
             {
-                unsigned int trackNumber = 1;
-                uint64_t     trackID = 0;
+                uint32_t trackNumber = 1;
+                uint32_t trackID = 0;
 
                 uint64_t start;
                 if (RT_SUCCESS(rc))
-                    rc = Ebml_StartSubElement(glob, &start, TrackEntry);
+                    rc = ebml_StartSubElement(glob, &start, TrackEntry);
                 if (RT_SUCCESS(rc))
                     rc = Ebml_SerializeUnsigned(glob, TrackNumber, trackNumber);
                 glob->track_id_pos = RTFileTell(glob->file);
-                Ebml_SerializeUnsigned32(glob, TrackUID, trackID);
+                ebml_SerializeUnsigned32(glob, TrackUID, trackID);
                 Ebml_SerializeUnsigned(glob, TrackType, 1); //video is always 1
                 Ebml_SerializeString(glob, CodecID, "V_VP8");
                 {
-                    unsigned int pixelWidth = cfg->g_w;
-                    unsigned int pixelHeight = cfg->g_h;
-                    double       frameRate   = (double)fps->num/(double)fps->den;
-
                     uint64_t videoStart;
                     if (RT_SUCCESS(rc))
-                        rc = Ebml_StartSubElement(glob, &videoStart, Video);
+                        rc = ebml_StartSubElement(glob, &videoStart, Video);
+                    uint32_t pixelWidth = cfg->g_w;
                     if (RT_SUCCESS(rc))
                         rc = Ebml_SerializeUnsigned(glob, PixelWidth, pixelWidth);
+                    uint32_t pixelHeight = cfg->g_h;
                     if (RT_SUCCESS(rc))
                         rc = Ebml_SerializeUnsigned(glob, PixelHeight, pixelHeight);
+                    double   frameRate   = (double)fps->num / (double)fps->den;
                     if (RT_SUCCESS(rc))
                         rc = Ebml_SerializeFloat(glob, FrameRate, frameRate);
                     if (RT_SUCCESS(rc))
-                        rc = Ebml_EndSubElement(glob, &videoStart); //Video
+                        rc = ebml_EndSubElement(glob, videoStart);
                 }
                 if (RT_SUCCESS(rc))
-                    rc = Ebml_EndSubElement(glob, &start); //Track Entry
+                    rc = ebml_EndSubElement(glob, start); //Track Entry
             }
             if (RT_SUCCESS(rc))
-                rc = Ebml_EndSubElement(glob, &trackStart);
+                rc = ebml_EndSubElement(glob, trackStart);
         }
         // segment element is open
@@ -360,11 +367,8 @@
                         const vpx_codec_cx_pkt_t  *pkt)
 {
-    uint64_t       block_length;
-    unsigned char  track_number;
-    unsigned short block_timecode = 0;
-    unsigned char  flags;
-    int64_t        pts_ms;
-    int            start_cluster = 0, is_keyframe;
-    int            rc = VINF_SUCCESS;
+    uint16_t block_timecode = 0;
+    int64_t  pts_ms;
+    int      start_cluster = 0;
+    int      rc = VINF_SUCCESS;
 
     /* Calculate the PTS of this frame in milliseconds */
@@ -379,11 +383,11 @@
         start_cluster = 1;
     else
-        block_timecode = (unsigned short)(pts_ms - glob->cluster_timecode);
-
-    is_keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY);
-    if (start_cluster || is_keyframe)
+        block_timecode = (uint16_t)(pts_ms - glob->cluster_timecode);
+
+    int fKeyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY);
+    if (start_cluster || fKeyframe)
     {
         if (glob->cluster_open)
-            rc = Ebml_EndSubElement(glob, &glob->startCluster);
+            rc = ebml_EndSubElement(glob, glob->startCluster);
 
         /* Open the new cluster */
@@ -393,10 +397,10 @@
         glob->cluster_pos = RTFileTell(glob->file);
         if (RT_SUCCESS(rc))
-            rc = Ebml_StartSubElement(glob, &glob->startCluster, Cluster); //cluster
+            rc = ebml_StartSubElement(glob, &glob->startCluster, Cluster); //cluster
         if (RT_SUCCESS(rc))
             rc = Ebml_SerializeUnsigned(glob, Timecode, glob->cluster_timecode);
 
         /* Save a cue point if this is a keyframe. */
-        if (is_keyframe)
+        if (fKeyframe)
         {
             struct cue_entry *cue;
@@ -412,33 +416,31 @@
     /* Write the Simple Block */
     if (RT_SUCCESS(rc))
-        rc = Ebml_WriteID(glob, SimpleBlock);
-
-    block_length = pkt->data.frame.sz + 4;
+        rc = ebml_WriteID(glob, SimpleBlock);
+
+    uint64_t block_length = pkt->data.frame.sz + 4;
     block_length |= 0x10000000;
     if (RT_SUCCESS(rc))
-        rc = Ebml_Serialize(glob, &block_length, 4);
-
-    track_number = 1;
-    track_number |= 0x80;
-    if (RT_SUCCESS(rc))
-        rc = Ebml_Write(glob, &track_number, 1);
-
-    if (RT_SUCCESS(rc))
-        rc = Ebml_Serialize(glob, &block_timecode, 2);
-
-    flags = 0;
-    if (is_keyframe)
+        rc = ebml_WriteU32(glob, RT_H2BE_U32(block_length));
+
+    uint8_t track_number = 0x80 | 1;
+    if (RT_SUCCESS(rc))
+        rc = ebml_WriteU8(glob, track_number);
+
+    if (RT_SUCCESS(rc))
+        rc = ebml_WriteU16(glob, RT_H2BE_U16(block_timecode));
+
+    uint8_t flags = 0;
+    if (fKeyframe)
         flags |= 0x80;
     if (pkt->data.frame.flags & VPX_FRAME_IS_INVISIBLE)
         flags |= 0x08;
     if (RT_SUCCESS(rc))
-        rc = Ebml_Write(glob, &flags, 1);
-
-    if (RT_SUCCESS(rc))
-        rc = Ebml_Write(glob, pkt->data.frame.buf, pkt->data.frame.sz);
-
-    return rc;
-}
-
+        rc = ebml_WriteU8(glob, flags);
+
+    if (RT_SUCCESS(rc))
+        rc = ebml_Write(glob, pkt->data.frame.buf, pkt->data.frame.sz);
+
+    return rc;
+}
 
 int Ebml_WriteWebMFileFooter(EbmlGlobal *glob, long hash)
@@ -446,14 +448,13 @@
     int rc = VINF_SUCCESS;
     if (glob->cluster_open)
-        rc = Ebml_EndSubElement(glob, &glob->startCluster);
+        rc = ebml_EndSubElement(glob, glob->startCluster);
 
     {
         uint64_t start;
-        unsigned int i;
 
         glob->cue_pos = RTFileTell(glob->file);
         if (RT_SUCCESS(rc))
-            rc = Ebml_StartSubElement(glob, &start, Cues);
-        for (i=0; i<glob->cues; i++)
+            rc = ebml_StartSubElement(glob, &start, Cues);
+        for (unsigned i = 0;  i < glob->cues; i++)
         {
             struct cue_entry *cue = &glob->cue_list[i];
@@ -461,5 +462,5 @@
 
             if (RT_SUCCESS(rc))
-                rc = Ebml_StartSubElement(glob, &startSub, CuePoint);
+                rc = ebml_StartSubElement(glob, &startSub, CuePoint);
             {
                 uint64_t startSubsub;
@@ -468,5 +469,5 @@
                     rc = Ebml_SerializeUnsigned(glob, CueTime, cue->time);
                 if (RT_SUCCESS(rc))
-                    rc = Ebml_StartSubElement(glob, &startSubsub, CueTrackPositions);
+                    rc = ebml_StartSubElement(glob, &startSubsub, CueTrackPositions);
                 if (RT_SUCCESS(rc))
                     rc = Ebml_SerializeUnsigned(glob, CueTrack, 1);
@@ -476,15 +477,15 @@
                 //Ebml_SerializeUnsigned(glob, CueBlockNumber, cue->blockNumber);
                 if (RT_SUCCESS(rc))
-                    rc = Ebml_EndSubElement(glob, &startSubsub);
+                    rc = ebml_EndSubElement(glob, startSubsub);
             }
             if (RT_SUCCESS(rc))
-                rc = Ebml_EndSubElement(glob, &startSub);
+                rc = ebml_EndSubElement(glob, startSub);
         }
         if (RT_SUCCESS(rc))
-            rc = Ebml_EndSubElement(glob, &start);
-    }
-
-    if (RT_SUCCESS(rc))
-        rc = Ebml_EndSubElement(glob, &glob->startSegment);
+            rc = ebml_EndSubElement(glob, start);
+    }
+
+    if (RT_SUCCESS(rc))
+        rc = ebml_EndSubElement(glob, glob->startSegment);
 
     /* Patch up the seek info block */
@@ -496,5 +497,5 @@
         rc = RTFileSeek(glob->file, glob->track_id_pos, RTFILE_SEEK_BEGIN, NULL);
     if (RT_SUCCESS(rc))
-        rc  = Ebml_SerializeUnsigned32(glob, TrackUID, glob->debug ? 0xDEADBEEF : hash);
+        rc  = ebml_SerializeUnsigned32(glob, TrackUID, glob->debug ? 0xDEADBEEF : hash);
 
     if (RT_SUCCESS(rc))
Index: trunk/src/VBox/Main/src-client/EbmlWriter.h
===================================================================
--- trunk/src/VBox/Main/src-client/EbmlWriter.h	(revision 45914)
+++ trunk/src/VBox/Main/src-client/EbmlWriter.h	(revision 45922)
@@ -283,20 +283,4 @@
 };
 
-int Ebml_Serialize(EbmlGlobal *glob, const void *, uint64_t);
-int Ebml_SerializeUnsigned32(EbmlGlobal *glob, uint32_t class_id, uint64_t ui);
-int Ebml_Write(EbmlGlobal *glob, const void *, uint64_t);
-int Ebml_StartSubElement(EbmlGlobal *glob, uint64_t *ebmlLoc, uint32_t class_id);
-int Ebml_EndSubElement(EbmlGlobal *glob, uint64_t *ebmlLoc);
-
-int Ebml_WriteLen(EbmlGlobal *glob, uint64_t val);
-int Ebml_WriteString(EbmlGlobal *glob, const char *str);
-int Ebml_WriteID(EbmlGlobal *glob, uint32_t class_id);
-int Ebml_SerializeUnsigned64(EbmlGlobal *glob, uint32_t class_id, uint64_t ui);
-int Ebml_SerializeUnsigned(EbmlGlobal *glob, uint32_t class_id, uint32_t ui);
-int Ebml_SerializeBinary(EbmlGlobal *glob, uint32_t class_id, uint32_t ui);
-int Ebml_SerializeFloat(EbmlGlobal *glob, uint32_t class_id, double d);
-
-int Ebml_SerializeString(EbmlGlobal *glob, uint32_t class_id, const char *s);
-
 int Ebml_WriteWebMSeekElement(EbmlGlobal *ebml, uint32_t id, uint64_t pos);
 int Ebml_WriteWebMSeekInfo(EbmlGlobal *ebml);
Index: trunk/src/VBox/Main/src-client/VideoRec.cpp
===================================================================
--- trunk/src/VBox/Main/src-client/VideoRec.cpp	(revision 45914)
+++ trunk/src/VBox/Main/src-client/VideoRec.cpp	(revision 45922)
@@ -405,5 +405,7 @@
             ASMAtomicWriteBool(&pVideoRecCtx->fRgbFilled, false);
             if (RT_SUCCESS(rc))
-                videoRecEncodeAndWrite(pVideoRecCtx);
+                rc = videoRecEncodeAndWrite(pVideoRecCtx);
+           if (RT_FAILURE(rc))
+                LogRel(("Error %Rrc encoding video frame\n", rc));
         }
     }
@@ -554,9 +556,9 @@
     vpx_codec_pts_t pts = pVideoRecCtx->u64TimeStamp;
     vpx_codec_err_t rcv = vpx_codec_encode(&pVideoRecCtx->VpxCodec,
-                                             &pVideoRecCtx->VpxRawImage,
-                                             pts /* time stamp */,
-                                             10 /* how long to show this frame */,
-                                             0 /* flags */,
-                                             VPX_DL_REALTIME /* deadline */);
+                                           &pVideoRecCtx->VpxRawImage,
+                                           pts /* time stamp */,
+                                           10  /* how long to show this frame */,
+                                           0   /* flags */,
+                                           VPX_DL_REALTIME /* deadline */);
     if (rcv != VPX_CODEC_OK)
     {
@@ -566,10 +568,10 @@
 
     vpx_codec_iter_t iter = NULL;
-    int rc = VINF_SUCCESS;
+    int rc = VERR_NO_DATA;
     for (;;)
     {
         const vpx_codec_cx_pkt_t *pkt = vpx_codec_get_cx_data(&pVideoRecCtx->VpxCodec, &iter);
         if (!pkt)
-            return VERR_NO_DATA;
+            break;
         switch (pkt->kind)
         {
