VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/EbmlWriter.cpp@ 52312

Last change on this file since 52312 was 52312, checked in by vboxsync, 10 years ago

6219: New parameters related to file size / recording time limitation for VM Video Capture have been added (vcpmaxtime, vcpmaxsize and vcpoptions - special codec options in key=value format). EbmlWriter has been refactored. Removed some redundant code.

  • 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
Line 
1/* $Id: EbmlWriter.cpp 52312 2014-08-07 12:54:38Z vboxsync $ */
2/** @file
3 * EbmlWriter.cpp - EBML writer + WebM container
4 */
5
6/*
7 * Copyright (C) 2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*
19 * This code is based on:
20 *
21 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
22 * Use of this source code is governed by a BSD-style license
23 * that can be found in the LICENSE file in the root of the source
24 * tree. An additional intellectual property rights grant can be found
25 * in the file PATENTS. All contributing project authors may
26 * be found in the AUTHORS file in the root of the source tree.
27 */
28
29#include "EbmlWriter.h"
30#include <iprt/asm.h>
31#include <iprt/mem.h>
32#include <iprt/string.h>
33#include <VBox/log.h>
34
35Ebml::Ebml() {}
36
37void Ebml::init(const RTFILE &a_File)
38{
39 m_File = a_File;
40}
41
42void Ebml::write(const void *data, size_t size)
43{
44 int rc = RTFileWrite(m_File, data, size, NULL);
45 if (!RT_SUCCESS(rc)) throw rc;
46}
47
48WebMWriter::WebMWriter() :
49 m_bDebug(false),
50 m_iLastPtsMs(-1),
51 m_Framerate(),
52 m_uPositionReference(0),
53 m_uSeekInfoPos(0),
54 m_uSegmentInfoPos(0),
55 m_uTrackPos(0),
56 m_uCuePos(0),
57 m_uClusterPos(0),
58 m_uTrackIdPos(0),
59 m_uStartSegment(0),
60 m_uClusterTimecode(0),
61 m_bClusterOpen(false) {}
62
63int WebMWriter::create(const char *a_pszFilename)
64{
65 int rc = RTFileOpen(&m_File, a_pszFilename, RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
66 if (RT_SUCCESS(rc))
67 {
68 m_Ebml.init(m_File);
69 }
70 return rc;
71}
72
73void WebMWriter::close()
74{
75 RTFileClose(m_File);
76}
77
78Ebml &operator<<(Ebml &a_Ebml, const WebMWriter::SimpleBlockData &a_Data)
79{
80 a_Ebml.serializeConst<WebMWriter::Mkv::SimpleBlock>();
81 a_Ebml.write<uint32_t>(0x10000000u | (Ebml::getSizeOfUInt(a_Data.trackNumber) + 2 + 1 + a_Data.dataSize));
82 a_Ebml.serializeInteger(a_Data.trackNumber);
83 a_Ebml.write(a_Data.timeCode);
84 a_Ebml.write(a_Data.flags);
85 a_Ebml.write(a_Data.data, a_Data.dataSize);
86 return a_Ebml;
87}
88
89void WebMWriter::writeSeekInfo()
90{
91 // Save the current file pointer
92 uint64_t uPos = RTFileTell(m_File);
93 if (m_uSeekInfoPos)
94 RTFileSeek(m_File, m_uSeekInfoPos, RTFILE_SEEK_BEGIN, NULL);
95 else
96 m_uSeekInfoPos = uPos;
97
98 m_Ebml << Ebml::SubStart<SeekHead>()
99
100 << Ebml::SubStart<Seek>()
101 << Ebml::Const<SeekID, Tracks>()
102 << Ebml::Var<SeekPosition, uint64_t>(m_uTrackPos - m_uPositionReference)
103 << Ebml::SubEnd<Seek>()
104
105 << Ebml::SubStart<Seek>()
106 << Ebml::Const<SeekID, Cues>()
107 << Ebml::Var<SeekPosition, uint64_t>(m_uCuePos - m_uPositionReference)
108 << Ebml::SubEnd<Seek>()
109
110 << Ebml::SubStart<Seek>()
111 << Ebml::Const<SeekID, Info>()
112 << Ebml::Var<SeekPosition, uint64_t>(m_uSegmentInfoPos - m_uPositionReference)
113 << Ebml::SubEnd<Seek>()
114
115 << Ebml::SubEnd<SeekHead>();
116
117 int64_t iFrameTime = (int64_t)1000 * m_Framerate.den / m_Framerate.num;
118 m_uSegmentInfoPos = RTFileTell(m_File);
119
120 char szVersion[64];
121 RTStrPrintf(szVersion, sizeof(szVersion), "vpxenc%",
122 m_bDebug ? vpx_codec_version_str() : "");
123
124 m_Ebml << Ebml::SubStart<Info>()
125 << Ebml::UnsignedInteger<TimecodeScale>(1000000)
126 << Ebml::Float<Segment_Duration>(m_iLastPtsMs + iFrameTime)
127 << Ebml::String<MuxingApp>(szVersion)
128 << Ebml::String<WritingApp>(szVersion)
129 << Ebml::SubEnd<Info>();
130}
131
132int WebMWriter::writeHeader(const vpx_codec_enc_cfg_t *a_pCfg,
133 const struct vpx_rational *a_pFps)
134{
135 try
136 {
137 m_Ebml << Ebml::SubStart<EBML>()
138 << Ebml::UnsignedInteger<EBMLVersion>(1)
139 << Ebml::UnsignedInteger<EBMLReadVersion>(1)
140 << Ebml::UnsignedInteger<EBMLMaxIDLength>(4)
141 << Ebml::UnsignedInteger<EBMLMaxSizeLength>(8)
142 << Ebml::String<DocType>("webm")
143 << Ebml::UnsignedInteger<DocTypeVersion>(2)
144 << Ebml::UnsignedInteger<DocTypeReadVersion>(2)
145 << Ebml::SubEnd<EBML>();
146
147 m_Ebml << Ebml::SubStart<Segment>();
148
149 m_uPositionReference = RTFileTell(m_File);
150 m_Framerate = *a_pFps;
151
152 writeSeekInfo();
153
154 m_uTrackPos = RTFileTell(m_File);
155
156 m_Ebml << Ebml::SubStart<Tracks>()
157 << Ebml::SubStart<TrackEntry>()
158 << Ebml::UnsignedInteger<TrackNumber>(1);
159
160 m_uTrackIdPos = RTFileTell(m_File);
161
162 m_Ebml << Ebml::Var<TrackUID, uint32_t>(0)
163 << Ebml::UnsignedInteger<TrackType>(1)
164 << Ebml::String<CodecID>("V_VP8")
165 << Ebml::SubStart<Video>()
166 << Ebml::UnsignedInteger<PixelWidth>(a_pCfg->g_w)
167 << Ebml::UnsignedInteger<PixelHeight>(a_pCfg->g_h)
168 << Ebml::Float<FrameRate>((double)a_pFps->num / a_pFps->den)
169 << Ebml::SubEnd<Video>()
170 << Ebml::SubEnd<TrackEntry>()
171 << Ebml::SubEnd<Tracks>();
172 }
173 catch(int rc)
174 {
175 LogFlow(("WebMWriter::writeHeader catched"));
176 return rc;
177 }
178 return VINF_SUCCESS;
179}
180
181int WebMWriter::writeBlock(const vpx_codec_enc_cfg_t *a_pCfg,
182 const vpx_codec_cx_pkt_t *a_pPkt)
183{
184 try {
185 uint16_t uBlockTimecode = 0;
186 int64_t iPtsMs;
187 bool bStartCluster = false;
188
189 /* Calculate the PTS of this frame in milliseconds */
190 iPtsMs = a_pPkt->data.frame.pts * 1000
191 * (uint64_t) a_pCfg->g_timebase.num / a_pCfg->g_timebase.den;
192 if (iPtsMs <= m_iLastPtsMs)
193 iPtsMs = m_iLastPtsMs + 1;
194 m_iLastPtsMs = iPtsMs;
195
196 /* Calculate the relative time of this block */
197 if (iPtsMs - m_uClusterTimecode > 65536)
198 bStartCluster = 1;
199 else
200 uBlockTimecode = static_cast<uint16_t>(iPtsMs - m_uClusterTimecode);
201
202 int fKeyframe = (a_pPkt->data.frame.flags & VPX_FRAME_IS_KEY);
203 if (bStartCluster || fKeyframe)
204 {
205 if (m_bClusterOpen)
206 m_Ebml << Ebml::SubEnd<Cluster>();
207
208 /* Open a new cluster */
209 uBlockTimecode = 0;
210 m_bClusterOpen = true;
211 m_uClusterTimecode = (uint32_t)iPtsMs;
212 m_uClusterPos = RTFileTell(m_File);
213 m_Ebml << Ebml::SubStart<Cluster>()
214 << Ebml::UnsignedInteger<Timecode>(m_uClusterTimecode);
215
216 /* Save a cue point if this is a keyframe. */
217 if (fKeyframe)
218 {
219 CueEntry cue(m_uClusterTimecode, m_uClusterPos);
220 m_CueList.push_back(cue);
221 }
222 }
223
224 // Write a Simple Block
225 SimpleBlockData block(1, uBlockTimecode,
226 (fKeyframe ? 0x80 : 0) | (a_pPkt->data.frame.flags & VPX_FRAME_IS_INVISIBLE ? 0x08 : 0),
227 a_pPkt->data.frame.buf, a_pPkt->data.frame.sz);
228
229 m_Ebml << block;
230 }
231 catch(int rc)
232 {
233 LogFlow(("WebMWriter::writeBlock catched"));
234 return rc;
235 }
236 return VINF_SUCCESS;
237}
238
239int WebMWriter::writeFooter(uint32_t a_u64Hash)
240{
241 try {
242 if (m_bClusterOpen)
243 m_Ebml << Ebml::SubEnd<Cluster>();
244
245 m_uCuePos = RTFileTell(m_File);
246 m_Ebml << Ebml::SubStart<Cues>();
247 for (std::list<CueEntry>::iterator it = m_CueList.begin(); it != m_CueList.end(); ++it)
248 {
249 m_Ebml << Ebml::SubStart<CuePoint>()
250 << Ebml::UnsignedInteger<CueTime>(it->time)
251 << Ebml::SubStart<CueTrackPositions>()
252 << Ebml::Const<CueTrack, 1>()
253 << Ebml::Var<CueClusterPosition, uint64_t>(it->loc - m_uPositionReference)
254 << Ebml::SubEnd<CueTrackPositions>()
255 << Ebml::SubEnd<CuePoint>();
256 }
257 m_Ebml << Ebml::SubEnd<Cues>()
258 << Ebml::SubEnd<Segment>();
259
260 writeSeekInfo();
261
262 int rc = RTFileSeek(m_File, m_uTrackIdPos, RTFILE_SEEK_BEGIN, NULL);
263 if (!RT_SUCCESS(rc)) throw rc;
264
265 m_Ebml << Ebml::Var<TrackUID, uint32_t>(m_bDebug ? 0xDEADBEEF : a_u64Hash);
266
267 rc = RTFileSeek(m_File, 0, RTFILE_SEEK_END, NULL);
268 if (!RT_SUCCESS(rc)) throw rc;
269 }
270 catch(int rc)
271 {
272 LogFlow(("WebMWriter::writeFooterException catched"));
273 return rc;
274 }
275 return VINF_SUCCESS;
276}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette