VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/EBMLWriter.cpp

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
  • Property svn:mergeinfo set to (toggle deleted branches)
    /branches/VBox-3.0/src/VBox/Frontends/VBoxHeadless/VideoCapture/EbmlWriter.cpp58652,​70973
    /branches/VBox-3.2/src/VBox/Frontends/VBoxHeadless/VideoCapture/EbmlWriter.cpp66309,​66318
    /branches/VBox-4.0/src/VBox/Frontends/VBoxHeadless/VideoCapture/EbmlWriter.cpp70873
    /branches/VBox-4.1/src/VBox/Frontends/VBoxHeadless/VideoCapture/EbmlWriter.cpp74233
    /branches/VBox-4.2/src/VBox/Main/src-client/EbmlWriter.cpp91503-91504,​91506-91508,​91510,​91514-91515,​91521
    /branches/VBox-4.3/src/VBox/Main/src-client/EbmlWriter.cpp91223
    /branches/VBox-4.3/trunk/src/VBox/Main/src-client/EbmlWriter.cpp91223
    /branches/dsen/gui/src/VBox/Frontends/VBoxHeadless/VideoCapture/EbmlWriter.cpp79076-79078,​79089,​79109-79110,​79112-79113,​79127-79130,​79134,​79141,​79151,​79155,​79157-79159,​79193,​79197
    /branches/dsen/gui2/src/VBox/Frontends/VBoxHeadless/VideoCapture/EbmlWriter.cpp79224,​79228,​79233,​79235,​79258,​79262-79263,​79273,​79341,​79345,​79354,​79357,​79387-79388,​79559-79569,​79572-79573,​79578,​79581-79582,​79590-79591,​79598-79599,​79602-79603,​79605-79606,​79632,​79635,​79637,​79644
    /branches/dsen/gui3/src/VBox/Frontends/VBoxHeadless/VideoCapture/EbmlWriter.cpp79645-79692
File size: 8.6 KB
RevLine 
[45913]1/* $Id: EBMLWriter.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
[69683]3 * EBMLWriter.cpp - EBML writer implementation.
[45913]4 */
5
6/*
[98103]7 * Copyright (C) 2013-2023 Oracle and/or its affiliates.
[45913]8 *
[96407]9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
[45913]26 */
27
[68451]28/**
29 * For more information, see:
30 * - https://w3c.github.io/media-source/webm-byte-stream-format.html
31 * - https://www.webmproject.org/docs/container/#muxer-guidelines
32 */
33
[69683]34#ifdef LOG_GROUP
35# undef LOG_GROUP
36#endif
[67914]37#define LOG_GROUP LOG_GROUP_MAIN_DISPLAY
38#include "LoggingNew.h"
[45913]39
[52791]40#include <list>
[65330]41#include <map>
[69189]42#include <queue>
[52791]43#include <stack>
[65389]44
45#include <math.h> /* For lround.h. */
46
[52791]47#include <iprt/asm.h>
[65307]48#include <iprt/buildconfig.h>
[52791]49#include <iprt/cdefs.h>
[69189]50#include <iprt/critsect.h>
[76474]51#include <iprt/errcore.h>
[65256]52#include <iprt/file.h>
53#include <iprt/rand.h>
54#include <iprt/string.h>
55
[52791]56#include <VBox/log.h>
[65307]57#include <VBox/version.h>
[65256]58
[69194]59#include "EBMLWriter.h"
[69195]60#include "EBML_MKV.h"
[45913]61
[68796]62/** No flags set. */
63#define VBOX_EBMLWRITER_FLAG_NONE 0
64/** The file handle was inherited. */
65#define VBOX_EBMLWRITER_FLAG_HANDLE_INHERITED RT_BIT(0)
[52316]66
[69683]67/** Creates an EBML output file using an existing, open file handle. */
68int EBMLWriter::createEx(const char *a_pszFile, PRTFILE phFile)
[52325]69{
[69683]70 AssertPtrReturn(phFile, VERR_INVALID_POINTER);
[52325]71
[69683]72 m_hFile = *phFile;
73 m_fFlags |= VBOX_EBMLWRITER_FLAG_HANDLE_INHERITED;
74 m_strFile = a_pszFile;
[45922]75
[69683]76 return VINF_SUCCESS;
77}
[52791]78
[69683]79/** Creates an EBML output file using a file name. */
80int EBMLWriter::create(const char *a_pszFile, uint64_t fOpen)
81{
[94953]82 int vrc = RTFileOpen(&m_hFile, a_pszFile, fOpen);
83 if (RT_SUCCESS(vrc))
[68796]84 m_strFile = a_pszFile;
85
[94953]86 return vrc;
[69683]87}
[68796]88
[69683]89/** Returns available space on storage. */
90uint64_t EBMLWriter::getAvailableSpace(void)
91{
92 RTFOFF pcbFree;
[94953]93 int vrc = RTFileQueryFsSizes(m_hFile, NULL, &pcbFree, 0, 0);
94 return (RT_SUCCESS(vrc)? (uint64_t)pcbFree : UINT64_MAX);
[69683]95}
[68332]96
[69683]97/** Closes the file. */
98void EBMLWriter::close(void)
99{
100 if (!isOpen())
101 return;
[45913]102
[69683]103 AssertMsg(m_Elements.size() == 0,
104 ("%zu elements are not closed yet (next element to close is 0x%x)\n",
105 m_Elements.size(), m_Elements.top().classId));
[68332]106
[69683]107 if (!(m_fFlags & VBOX_EBMLWRITER_FLAG_HANDLE_INHERITED))
[52791]108 {
[69683]109 RTFileClose(m_hFile);
110 m_hFile = NIL_RTFILE;
[52791]111 }
[45922]112
[69683]113 m_fFlags = VBOX_EBMLWRITER_FLAG_NONE;
114 m_strFile = "";
115}
[45913]116
[69683]117/** Starts an EBML sub-element. */
118EBMLWriter& EBMLWriter::subStart(EbmlClassId classId)
119{
120 writeClassId(classId);
121 /* store the current file offset. */
122 m_Elements.push(EbmlSubElement(RTFileTell(m_hFile), classId));
123 /* Indicates that size of the element
124 * is unkown (as according to EBML specs).
[65256]125 */
[69683]126 writeUnsignedInteger(UINT64_C(0x01FFFFFFFFFFFFFF));
127 return *this;
128}
[65256]129
[69683]130/** Ends an EBML sub-element. */
131EBMLWriter& EBMLWriter::subEnd(EbmlClassId classId)
132{
[65261]133#ifdef VBOX_STRICT
[69683]134 /* Class ID on the top of the stack should match the class ID passed
135 * to the function. Otherwise it may mean that we have a bug in the code.
[52791]136 */
[69683]137 AssertMsg(!m_Elements.empty(), ("No elements to close anymore\n"));
138 AssertMsg(m_Elements.top().classId == classId,
139 ("Ending sub element 0x%x is in wrong order (next to close is 0x%x)\n", classId, m_Elements.top().classId));
[65259]140#else
[69683]141 RT_NOREF(classId);
[65259]142#endif
[65256]143
[69683]144 uint64_t uPos = RTFileTell(m_hFile);
145 uint64_t uSize = uPos - m_Elements.top().offset - 8;
146 RTFileSeek(m_hFile, m_Elements.top().offset, RTFILE_SEEK_BEGIN, NULL);
[68451]147
[69683]148 /* Make sure that size will be serialized as uint64_t. */
149 writeUnsignedInteger(uSize | UINT64_C(0x0100000000000000));
150 RTFileSeek(m_hFile, uPos, RTFILE_SEEK_BEGIN, NULL);
151 m_Elements.pop();
152 return *this;
153}
[68451]154
[69683]155/** Serializes a null-terminated string. */
156EBMLWriter& EBMLWriter::serializeString(EbmlClassId classId, const char *str)
[52791]157{
[69683]158 writeClassId(classId);
159 uint64_t size = strlen(str);
160 writeSize(size);
161 write(str, size);
162 return *this;
[52791]163}
164
[69683]165/** Serializes an UNSIGNED integer.
166 * If size is zero then it will be detected automatically. */
167EBMLWriter& EBMLWriter::serializeUnsignedInteger(EbmlClassId classId, uint64_t parm, size_t size /* = 0 */)
[68796]168{
[69683]169 writeClassId(classId);
170 if (!size) size = getSizeOfUInt(parm);
171 writeSize(size);
172 writeUnsignedInteger(parm, size);
173 return *this;
[68796]174}
175
[69683]176/** Serializes a floating point value.
177 *
178 * Only 8-bytes double precision values are supported
179 * by this function.
180 */
181EBMLWriter& EBMLWriter::serializeFloat(EbmlClassId classId, float value)
[52791]182{
[69683]183 writeClassId(classId);
184 Assert(sizeof(uint32_t) == sizeof(float));
185 writeSize(sizeof(float));
186
187 union
[65256]188 {
[69683]189 float f;
190 uint8_t u8[4];
191 } u;
[65197]192
[69683]193 u.f = value;
[52791]194
[69683]195 for (int i = 3; i >= 0; i--) /* Converts values to big endian. */
196 write(&u.u8[i], 1);
[52791]197
[69683]198 return *this;
[52791]199}
200
[69683]201/** Serializes binary data. */
202EBMLWriter& EBMLWriter::serializeData(EbmlClassId classId, const void *pvData, size_t cbData)
[52791]203{
[69683]204 writeClassId(classId);
205 writeSize(cbData);
206 write(pvData, cbData);
207 return *this;
[65256]208}
209
[69683]210/** Writes raw data to file. */
211int EBMLWriter::write(const void *data, size_t size)
[65256]212{
[69683]213 return RTFileWrite(m_hFile, data, size, NULL);
[65256]214}
215
[69683]216/** Writes an unsigned integer of variable of fixed size. */
217void EBMLWriter::writeUnsignedInteger(uint64_t value, size_t size /* = sizeof(uint64_t) */)
[65256]218{
[69683]219 /* convert to big-endian */
220 value = RT_H2BE_U64(value);
221 write(reinterpret_cast<uint8_t*>(&value) + sizeof(value) - size, size);
[52791]222}
223
[69683]224/** Writes EBML class ID to file.
225 *
226 * EBML ID already has a UTF8-like represenation
227 * so getSizeOfUInt is used to determine
228 * the number of its bytes.
229 */
230void EBMLWriter::writeClassId(EbmlClassId parm)
[68332]231{
[69683]232 writeUnsignedInteger(parm, getSizeOfUInt(parm));
[68332]233}
234
[69683]235/** Writes data size value. */
236void EBMLWriter::writeSize(uint64_t parm)
[52791]237{
[69683]238 /* The following expression defines the size of the value that will be serialized
239 * as an EBML UTF-8 like integer (with trailing bits represeting its size):
240 1xxx xxxx - value 0 to 2^7-2
241 01xx xxxx xxxx xxxx - value 0 to 2^14-2
242 001x xxxx xxxx xxxx xxxx xxxx - value 0 to 2^21-2
243 0001 xxxx xxxx xxxx xxxx xxxx xxxx xxxx - value 0 to 2^28-2
244 0000 1xxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx - value 0 to 2^35-2
245 0000 01xx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx - value 0 to 2^42-2
246 0000 001x xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx - value 0 to 2^49-2
247 0000 0001 xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx - value 0 to 2^56-2
248 */
249 size_t size = 8 - ! (parm & (UINT64_MAX << 49)) - ! (parm & (UINT64_MAX << 42)) -
250 ! (parm & (UINT64_MAX << 35)) - ! (parm & (UINT64_MAX << 28)) -
251 ! (parm & (UINT64_MAX << 21)) - ! (parm & (UINT64_MAX << 14)) -
252 ! (parm & (UINT64_MAX << 7));
253 /* One is subtracted in order to avoid loosing significant bit when size = 8. */
254 uint64_t mask = RT_BIT_64(size * 8 - 1);
255 writeUnsignedInteger((parm & (((mask << 1) - 1) >> size)) | (mask >> (size - 1)), size);
[52791]256}
257
[69683]258/** Size calculation for variable size UNSIGNED integer.
259 *
260 * The function defines the size of the number by trimming
261 * consequent trailing zero bytes starting from the most significant.
262 * The following statement is always true:
263 * 1 <= getSizeOfUInt(arg) <= 8.
264 *
265 * Every !(arg & (UINT64_MAX << X)) expression gives one
266 * if an only if all the bits from X to 63 are set to zero.
267 */
268size_t EBMLWriter::getSizeOfUInt(uint64_t arg)
[52791]269{
[69683]270 return 8 - ! (arg & (UINT64_MAX << 56)) - ! (arg & (UINT64_MAX << 48)) -
271 ! (arg & (UINT64_MAX << 40)) - ! (arg & (UINT64_MAX << 32)) -
272 ! (arg & (UINT64_MAX << 24)) - ! (arg & (UINT64_MAX << 16)) -
273 ! (arg & (UINT64_MAX << 8));
[52791]274}
[63160]275
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use