1 | /* $Id: EbmlWriter.cpp 52316 2014-08-07 14:11:55Z 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 |
|
---|
35 | Ebml::Ebml() {}
|
---|
36 |
|
---|
37 | void Ebml::init(const RTFILE &a_File)
|
---|
38 | {
|
---|
39 | m_File = a_File;
|
---|
40 | }
|
---|
41 |
|
---|
42 | void 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 |
|
---|
48 | uint64_t WebMWriter::getFileSize()
|
---|
49 | {
|
---|
50 | return RTFileTell(m_File);
|
---|
51 | }
|
---|
52 |
|
---|
53 | WebMWriter::WebMWriter() :
|
---|
54 | m_bDebug(false),
|
---|
55 | m_iLastPtsMs(-1),
|
---|
56 | m_Framerate(),
|
---|
57 | m_uPositionReference(0),
|
---|
58 | m_uSeekInfoPos(0),
|
---|
59 | m_uSegmentInfoPos(0),
|
---|
60 | m_uTrackPos(0),
|
---|
61 | m_uCuePos(0),
|
---|
62 | m_uClusterPos(0),
|
---|
63 | m_uTrackIdPos(0),
|
---|
64 | m_uStartSegment(0),
|
---|
65 | m_uClusterTimecode(0),
|
---|
66 | m_bClusterOpen(false) {}
|
---|
67 |
|
---|
68 | int WebMWriter::create(const char *a_pszFilename)
|
---|
69 | {
|
---|
70 | int rc = RTFileOpen(&m_File, a_pszFilename, RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
|
---|
71 | if (RT_SUCCESS(rc))
|
---|
72 | {
|
---|
73 | m_Ebml.init(m_File);
|
---|
74 | }
|
---|
75 | return rc;
|
---|
76 | }
|
---|
77 |
|
---|
78 | void WebMWriter::close()
|
---|
79 | {
|
---|
80 | RTFileClose(m_File);
|
---|
81 | }
|
---|
82 |
|
---|
83 | Ebml &operator<<(Ebml &a_Ebml, const WebMWriter::SimpleBlockData &a_Data)
|
---|
84 | {
|
---|
85 | a_Ebml.serializeConst<WebMWriter::SimpleBlock>();
|
---|
86 | a_Ebml.write<uint32_t>(0x10000000u | (Ebml::getSizeOfUInt(a_Data.trackNumber) + 2 + 1 + a_Data.dataSize));
|
---|
87 | a_Ebml.serializeInteger(a_Data.trackNumber);
|
---|
88 | a_Ebml.write(a_Data.timeCode);
|
---|
89 | a_Ebml.write(a_Data.flags);
|
---|
90 | a_Ebml.write(a_Data.data, a_Data.dataSize);
|
---|
91 | return a_Ebml;
|
---|
92 | }
|
---|
93 |
|
---|
94 | void WebMWriter::writeSeekInfo()
|
---|
95 | {
|
---|
96 | // Save the current file pointer
|
---|
97 | uint64_t uPos = RTFileTell(m_File);
|
---|
98 | if (m_uSeekInfoPos)
|
---|
99 | RTFileSeek(m_File, m_uSeekInfoPos, RTFILE_SEEK_BEGIN, NULL);
|
---|
100 | else
|
---|
101 | m_uSeekInfoPos = uPos;
|
---|
102 |
|
---|
103 | m_Ebml << Ebml::SubStart<SeekHead>()
|
---|
104 |
|
---|
105 | << Ebml::SubStart<Seek>()
|
---|
106 | << Ebml::Const<SeekID, Tracks>()
|
---|
107 | << Ebml::Var<SeekPosition, uint64_t>(m_uTrackPos - m_uPositionReference)
|
---|
108 | << Ebml::SubEnd<Seek>()
|
---|
109 |
|
---|
110 | << Ebml::SubStart<Seek>()
|
---|
111 | << Ebml::Const<SeekID, Cues>()
|
---|
112 | << Ebml::Var<SeekPosition, uint64_t>(m_uCuePos - m_uPositionReference)
|
---|
113 | << Ebml::SubEnd<Seek>()
|
---|
114 |
|
---|
115 | << Ebml::SubStart<Seek>()
|
---|
116 | << Ebml::Const<SeekID, Info>()
|
---|
117 | << Ebml::Var<SeekPosition, uint64_t>(m_uSegmentInfoPos - m_uPositionReference)
|
---|
118 | << Ebml::SubEnd<Seek>()
|
---|
119 |
|
---|
120 | << Ebml::SubEnd<SeekHead>();
|
---|
121 |
|
---|
122 | int64_t iFrameTime = (int64_t)1000 * m_Framerate.den / m_Framerate.num;
|
---|
123 | m_uSegmentInfoPos = RTFileTell(m_File);
|
---|
124 |
|
---|
125 | char szVersion[64];
|
---|
126 | RTStrPrintf(szVersion, sizeof(szVersion), "vpxenc%",
|
---|
127 | m_bDebug ? vpx_codec_version_str() : "");
|
---|
128 |
|
---|
129 | m_Ebml << Ebml::SubStart<Info>()
|
---|
130 | << Ebml::UnsignedInteger<TimecodeScale>(1000000)
|
---|
131 | << Ebml::Float<Segment_Duration>(m_iLastPtsMs + iFrameTime)
|
---|
132 | << Ebml::String<MuxingApp>(szVersion)
|
---|
133 | << Ebml::String<WritingApp>(szVersion)
|
---|
134 | << Ebml::SubEnd<Info>();
|
---|
135 | }
|
---|
136 |
|
---|
137 | int WebMWriter::writeHeader(const vpx_codec_enc_cfg_t *a_pCfg,
|
---|
138 | const struct vpx_rational *a_pFps)
|
---|
139 | {
|
---|
140 | try
|
---|
141 | {
|
---|
142 | m_Ebml << Ebml::SubStart<EBML>()
|
---|
143 | << Ebml::UnsignedInteger<EBMLVersion>(1)
|
---|
144 | << Ebml::UnsignedInteger<EBMLReadVersion>(1)
|
---|
145 | << Ebml::UnsignedInteger<EBMLMaxIDLength>(4)
|
---|
146 | << Ebml::UnsignedInteger<EBMLMaxSizeLength>(8)
|
---|
147 | << Ebml::String<DocType>("webm")
|
---|
148 | << Ebml::UnsignedInteger<DocTypeVersion>(2)
|
---|
149 | << Ebml::UnsignedInteger<DocTypeReadVersion>(2)
|
---|
150 | << Ebml::SubEnd<EBML>();
|
---|
151 |
|
---|
152 | m_Ebml << Ebml::SubStart<Segment>();
|
---|
153 |
|
---|
154 | m_uPositionReference = RTFileTell(m_File);
|
---|
155 | m_Framerate = *a_pFps;
|
---|
156 |
|
---|
157 | writeSeekInfo();
|
---|
158 |
|
---|
159 | m_uTrackPos = RTFileTell(m_File);
|
---|
160 |
|
---|
161 | m_Ebml << Ebml::SubStart<Tracks>()
|
---|
162 | << Ebml::SubStart<TrackEntry>()
|
---|
163 | << Ebml::UnsignedInteger<TrackNumber>(1);
|
---|
164 |
|
---|
165 | m_uTrackIdPos = RTFileTell(m_File);
|
---|
166 |
|
---|
167 | m_Ebml << Ebml::Var<TrackUID, uint32_t>(0)
|
---|
168 | << Ebml::UnsignedInteger<TrackType>(1)
|
---|
169 | << Ebml::String<CodecID>("V_VP8")
|
---|
170 | << Ebml::SubStart<Video>()
|
---|
171 | << Ebml::UnsignedInteger<PixelWidth>(a_pCfg->g_w)
|
---|
172 | << Ebml::UnsignedInteger<PixelHeight>(a_pCfg->g_h)
|
---|
173 | << Ebml::Float<FrameRate>((double)a_pFps->num / a_pFps->den)
|
---|
174 | << Ebml::SubEnd<Video>()
|
---|
175 | << Ebml::SubEnd<TrackEntry>()
|
---|
176 | << Ebml::SubEnd<Tracks>();
|
---|
177 | }
|
---|
178 | catch(int rc)
|
---|
179 | {
|
---|
180 | LogFlow(("WebMWriter::writeHeader catched"));
|
---|
181 | return rc;
|
---|
182 | }
|
---|
183 | return VINF_SUCCESS;
|
---|
184 | }
|
---|
185 |
|
---|
186 | int WebMWriter::writeBlock(const vpx_codec_enc_cfg_t *a_pCfg,
|
---|
187 | const vpx_codec_cx_pkt_t *a_pPkt)
|
---|
188 | {
|
---|
189 | try {
|
---|
190 | uint16_t uBlockTimecode = 0;
|
---|
191 | int64_t iPtsMs;
|
---|
192 | bool bStartCluster = false;
|
---|
193 |
|
---|
194 | /* Calculate the PTS of this frame in milliseconds */
|
---|
195 | iPtsMs = a_pPkt->data.frame.pts * 1000
|
---|
196 | * (uint64_t) a_pCfg->g_timebase.num / a_pCfg->g_timebase.den;
|
---|
197 | if (iPtsMs <= m_iLastPtsMs)
|
---|
198 | iPtsMs = m_iLastPtsMs + 1;
|
---|
199 | m_iLastPtsMs = iPtsMs;
|
---|
200 |
|
---|
201 | /* Calculate the relative time of this block */
|
---|
202 | if (iPtsMs - m_uClusterTimecode > 65536)
|
---|
203 | bStartCluster = 1;
|
---|
204 | else
|
---|
205 | uBlockTimecode = static_cast<uint16_t>(iPtsMs - m_uClusterTimecode);
|
---|
206 |
|
---|
207 | int fKeyframe = (a_pPkt->data.frame.flags & VPX_FRAME_IS_KEY);
|
---|
208 | if (bStartCluster || fKeyframe)
|
---|
209 | {
|
---|
210 | if (m_bClusterOpen)
|
---|
211 | m_Ebml << Ebml::SubEnd<Cluster>();
|
---|
212 |
|
---|
213 | /* Open a new cluster */
|
---|
214 | uBlockTimecode = 0;
|
---|
215 | m_bClusterOpen = true;
|
---|
216 | m_uClusterTimecode = (uint32_t)iPtsMs;
|
---|
217 | m_uClusterPos = RTFileTell(m_File);
|
---|
218 | m_Ebml << Ebml::SubStart<Cluster>()
|
---|
219 | << Ebml::UnsignedInteger<Timecode>(m_uClusterTimecode);
|
---|
220 |
|
---|
221 | /* Save a cue point if this is a keyframe. */
|
---|
222 | if (fKeyframe)
|
---|
223 | {
|
---|
224 | CueEntry cue(m_uClusterTimecode, m_uClusterPos);
|
---|
225 | m_CueList.push_back(cue);
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | // Write a Simple Block
|
---|
230 | SimpleBlockData block(1, uBlockTimecode,
|
---|
231 | (fKeyframe ? 0x80 : 0) | (a_pPkt->data.frame.flags & VPX_FRAME_IS_INVISIBLE ? 0x08 : 0),
|
---|
232 | a_pPkt->data.frame.buf, a_pPkt->data.frame.sz);
|
---|
233 |
|
---|
234 | m_Ebml << block;
|
---|
235 | }
|
---|
236 | catch(int rc)
|
---|
237 | {
|
---|
238 | LogFlow(("WebMWriter::writeBlock catched"));
|
---|
239 | return rc;
|
---|
240 | }
|
---|
241 | return VINF_SUCCESS;
|
---|
242 | }
|
---|
243 |
|
---|
244 | int WebMWriter::writeFooter(uint32_t a_u64Hash)
|
---|
245 | {
|
---|
246 | try {
|
---|
247 | if (m_bClusterOpen)
|
---|
248 | m_Ebml << Ebml::SubEnd<Cluster>();
|
---|
249 |
|
---|
250 | m_uCuePos = RTFileTell(m_File);
|
---|
251 | m_Ebml << Ebml::SubStart<Cues>();
|
---|
252 | for (std::list<CueEntry>::iterator it = m_CueList.begin(); it != m_CueList.end(); ++it)
|
---|
253 | {
|
---|
254 | m_Ebml << Ebml::SubStart<CuePoint>()
|
---|
255 | << Ebml::UnsignedInteger<CueTime>(it->time)
|
---|
256 | << Ebml::SubStart<CueTrackPositions>()
|
---|
257 | << Ebml::Const<CueTrack, 1>()
|
---|
258 | << Ebml::Var<CueClusterPosition, uint64_t>(it->loc - m_uPositionReference)
|
---|
259 | << Ebml::SubEnd<CueTrackPositions>()
|
---|
260 | << Ebml::SubEnd<CuePoint>();
|
---|
261 | }
|
---|
262 | m_Ebml << Ebml::SubEnd<Cues>()
|
---|
263 | << Ebml::SubEnd<Segment>();
|
---|
264 |
|
---|
265 | writeSeekInfo();
|
---|
266 |
|
---|
267 | int rc = RTFileSeek(m_File, m_uTrackIdPos, RTFILE_SEEK_BEGIN, NULL);
|
---|
268 | if (!RT_SUCCESS(rc)) throw rc;
|
---|
269 |
|
---|
270 | m_Ebml << Ebml::Var<TrackUID, uint32_t>(m_bDebug ? 0xDEADBEEF : a_u64Hash);
|
---|
271 |
|
---|
272 | rc = RTFileSeek(m_File, 0, RTFILE_SEEK_END, NULL);
|
---|
273 | if (!RT_SUCCESS(rc)) throw rc;
|
---|
274 | }
|
---|
275 | catch(int rc)
|
---|
276 | {
|
---|
277 | LogFlow(("WebMWriter::writeFooterException catched"));
|
---|
278 | return rc;
|
---|
279 | }
|
---|
280 | return VINF_SUCCESS;
|
---|
281 | }
|
---|