1 | /* $Id: EbmlWriter.cpp 65212 2017-01-09 15:57:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * EbmlWriter.cpp - EBML writer + WebM container
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.215389.xyz. 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 | #include <list>
|
---|
20 | #include <stack>
|
---|
21 | #include <iprt/string.h>
|
---|
22 | #include <iprt/file.h>
|
---|
23 | #include <iprt/asm.h>
|
---|
24 | #include <iprt/cdefs.h>
|
---|
25 | #include <iprt/err.h>
|
---|
26 | #include <VBox/log.h>
|
---|
27 | #include "EbmlWriter.h"
|
---|
28 | #include "EbmlIDs.h"
|
---|
29 |
|
---|
30 |
|
---|
31 | class Ebml
|
---|
32 | {
|
---|
33 | public:
|
---|
34 | typedef uint32_t EbmlClassId;
|
---|
35 |
|
---|
36 | private:
|
---|
37 |
|
---|
38 | struct EbmlSubElement
|
---|
39 | {
|
---|
40 | uint64_t offset;
|
---|
41 | EbmlClassId classId;
|
---|
42 | EbmlSubElement(uint64_t offs, EbmlClassId cid) : offset(offs), classId(cid) {}
|
---|
43 | };
|
---|
44 |
|
---|
45 | std::stack<EbmlSubElement> m_Elements;
|
---|
46 | RTFILE m_File;
|
---|
47 |
|
---|
48 | public:
|
---|
49 |
|
---|
50 | /** Creates EBML output file. */
|
---|
51 | inline int create(const char *a_pszFilename, uint64_t fOpen)
|
---|
52 | {
|
---|
53 | return RTFileOpen(&m_File, a_pszFilename, fOpen);
|
---|
54 | }
|
---|
55 |
|
---|
56 | /** Returns file size. */
|
---|
57 | inline uint64_t getFileSize()
|
---|
58 | {
|
---|
59 | return RTFileTell(m_File);
|
---|
60 | }
|
---|
61 |
|
---|
62 | /** Get reference to file descriptor */
|
---|
63 | inline const RTFILE &getFile()
|
---|
64 | {
|
---|
65 | return m_File;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /** Returns available space on storage. */
|
---|
69 | inline uint64_t getAvailableSpace()
|
---|
70 | {
|
---|
71 | RTFOFF pcbFree;
|
---|
72 | int rc = RTFileQueryFsSizes(m_File, NULL, &pcbFree, 0, 0);
|
---|
73 | return (RT_SUCCESS(rc)? (uint64_t)pcbFree : UINT64_MAX);
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** Closes the file. */
|
---|
77 | inline void close()
|
---|
78 | {
|
---|
79 | RTFileClose(m_File);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /** Starts an EBML sub-element. */
|
---|
83 | inline Ebml &subStart(EbmlClassId classId)
|
---|
84 | {
|
---|
85 | writeClassId(classId);
|
---|
86 | /* store the current file offset. */
|
---|
87 | m_Elements.push(EbmlSubElement(RTFileTell(m_File), classId));
|
---|
88 | /* Indicates that size of the element
|
---|
89 | * is unkown (as according to EBML specs).
|
---|
90 | */
|
---|
91 | writeUnsignedInteger(UINT64_C(0x01FFFFFFFFFFFFFF));
|
---|
92 | return *this;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /** Ends an EBML sub-element. */
|
---|
96 | inline Ebml &subEnd(EbmlClassId classId)
|
---|
97 | {
|
---|
98 | /* Class ID on the top of the stack should match the class ID passed
|
---|
99 | * to the function. Otherwise it may mean that we have a bug in the code.
|
---|
100 | */
|
---|
101 | if(m_Elements.empty() || m_Elements.top().classId != classId) throw VERR_INTERNAL_ERROR;
|
---|
102 |
|
---|
103 | uint64_t uPos = RTFileTell(m_File);
|
---|
104 | uint64_t uSize = uPos - m_Elements.top().offset - 8;
|
---|
105 | RTFileSeek(m_File, m_Elements.top().offset, RTFILE_SEEK_BEGIN, NULL);
|
---|
106 |
|
---|
107 | /* make sure that size will be serialized as uint64 */
|
---|
108 | writeUnsignedInteger(uSize | UINT64_C(0x0100000000000000));
|
---|
109 | RTFileSeek(m_File, uPos, RTFILE_SEEK_BEGIN, NULL);
|
---|
110 | m_Elements.pop();
|
---|
111 | return *this;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /** Serializes a null-terminated string. */
|
---|
115 | inline Ebml &serializeString(EbmlClassId classId, const char *str)
|
---|
116 | {
|
---|
117 | writeClassId(classId);
|
---|
118 | uint64_t size = strlen(str);
|
---|
119 | writeSize(size);
|
---|
120 | write(str, size);
|
---|
121 | return *this;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /* Serializes an UNSIGNED integer
|
---|
125 | * If size is zero then it will be detected automatically. */
|
---|
126 | inline Ebml &serializeUnsignedInteger(EbmlClassId classId, uint64_t parm, size_t size = 0)
|
---|
127 | {
|
---|
128 | writeClassId(classId);
|
---|
129 | if (!size) size = getSizeOfUInt(parm);
|
---|
130 | writeSize(size);
|
---|
131 | writeUnsignedInteger(parm, size);
|
---|
132 | return *this;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /** Serializes a floating point value.
|
---|
136 | *
|
---|
137 | * Only 8-bytes double precision values are supported
|
---|
138 | * by this function.
|
---|
139 | */
|
---|
140 | inline Ebml &serializeFloat(EbmlClassId classId, double value)
|
---|
141 | {
|
---|
142 | writeClassId(classId);
|
---|
143 | writeSize(sizeof(double));
|
---|
144 | writeUnsignedInteger(*reinterpret_cast<uint64_t*>(&value));
|
---|
145 | return *this;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /** Serializes binary data. */
|
---|
149 | inline Ebml &serializeData(EbmlClassId classId, const void *pvData, size_t cbData)
|
---|
150 | {
|
---|
151 | writeClassId(classId);
|
---|
152 | writeSize(cbData);
|
---|
153 | write(pvData, cbData);
|
---|
154 | return *this;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /** Writes raw data to file. */
|
---|
158 | inline void write(const void *data, size_t size)
|
---|
159 | {
|
---|
160 | int rc = RTFileWrite(m_File, data, size, NULL);
|
---|
161 | if (!RT_SUCCESS(rc)) throw rc;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /** Writes an unsigned integer of variable of fixed size. */
|
---|
165 | inline void writeUnsignedInteger(uint64_t value, size_t size = sizeof(uint64_t))
|
---|
166 | {
|
---|
167 | /* convert to big-endian */
|
---|
168 | value = RT_H2BE_U64(value);
|
---|
169 | write(reinterpret_cast<uint8_t*>(&value) + sizeof(value) - size, size);
|
---|
170 | }
|
---|
171 |
|
---|
172 | /** Writes EBML class ID to file.
|
---|
173 | *
|
---|
174 | * EBML ID already has a UTF8-like represenation
|
---|
175 | * so getSizeOfUInt is used to determine
|
---|
176 | * the number of its bytes.
|
---|
177 | */
|
---|
178 | inline void writeClassId(EbmlClassId parm)
|
---|
179 | {
|
---|
180 | writeUnsignedInteger(parm, getSizeOfUInt(parm));
|
---|
181 | }
|
---|
182 |
|
---|
183 | /** Writes data size value. */
|
---|
184 | inline void writeSize(uint64_t parm)
|
---|
185 | {
|
---|
186 | /* The following expression defines the size of the value that will be serialized
|
---|
187 | * as an EBML UTF-8 like integer (with trailing bits represeting its size):
|
---|
188 | 1xxx xxxx - value 0 to 2^7-2
|
---|
189 | 01xx xxxx xxxx xxxx - value 0 to 2^14-2
|
---|
190 | 001x xxxx xxxx xxxx xxxx xxxx - value 0 to 2^21-2
|
---|
191 | 0001 xxxx xxxx xxxx xxxx xxxx xxxx xxxx - value 0 to 2^28-2
|
---|
192 | 0000 1xxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx - value 0 to 2^35-2
|
---|
193 | 0000 01xx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx - value 0 to 2^42-2
|
---|
194 | 0000 001x xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx - value 0 to 2^49-2
|
---|
195 | 0000 0001 xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx - value 0 to 2^56-2
|
---|
196 | */
|
---|
197 | size_t size = 8 - ! (parm & (UINT64_MAX << 49)) - ! (parm & (UINT64_MAX << 42)) -
|
---|
198 | ! (parm & (UINT64_MAX << 35)) - ! (parm & (UINT64_MAX << 28)) -
|
---|
199 | ! (parm & (UINT64_MAX << 21)) - ! (parm & (UINT64_MAX << 14)) -
|
---|
200 | ! (parm & (UINT64_MAX << 7));
|
---|
201 | /* One is subtracted in order to avoid loosing significant bit when size = 8. */
|
---|
202 | uint64_t mask = RT_BIT_64(size * 8 - 1);
|
---|
203 | writeUnsignedInteger((parm & (((mask << 1) - 1) >> size)) | (mask >> (size - 1)), size);
|
---|
204 | }
|
---|
205 |
|
---|
206 | /** Size calculation for variable size UNSIGNED integer.
|
---|
207 | *
|
---|
208 | * The function defines the size of the number by trimming
|
---|
209 | * consequent trailing zero bytes starting from the most significant.
|
---|
210 | * The following statement is always true:
|
---|
211 | * 1 <= getSizeOfUInt(arg) <= 8.
|
---|
212 | *
|
---|
213 | * Every !(arg & (UINT64_MAX << X)) expression gives one
|
---|
214 | * if an only if all the bits from X to 63 are set to zero.
|
---|
215 | */
|
---|
216 | static inline size_t getSizeOfUInt(uint64_t arg)
|
---|
217 | {
|
---|
218 | return 8 - ! (arg & (UINT64_MAX << 56)) - ! (arg & (UINT64_MAX << 48)) -
|
---|
219 | ! (arg & (UINT64_MAX << 40)) - ! (arg & (UINT64_MAX << 32)) -
|
---|
220 | ! (arg & (UINT64_MAX << 24)) - ! (arg & (UINT64_MAX << 16)) -
|
---|
221 | ! (arg & (UINT64_MAX << 8));
|
---|
222 | }
|
---|
223 |
|
---|
224 | private:
|
---|
225 | void operator=(const Ebml &);
|
---|
226 |
|
---|
227 | };
|
---|
228 |
|
---|
229 | class WebMWriter_Impl
|
---|
230 | {
|
---|
231 | struct CueEntry
|
---|
232 | {
|
---|
233 | uint32_t time;
|
---|
234 | uint64_t loc;
|
---|
235 | CueEntry(uint32_t t, uint64_t l) : time(t), loc(l) {}
|
---|
236 | };
|
---|
237 |
|
---|
238 | #ifdef VBOX_WITH_AUDIO_VIDEOREC
|
---|
239 | # pragma pack(push)
|
---|
240 | # pragma pack(1)
|
---|
241 | /** Opus codec private data.
|
---|
242 | * Taken from: https://wiki.xiph.org/MatroskaOpus */
|
---|
243 | struct OpusPrivData
|
---|
244 | {
|
---|
245 | uint8_t au8Head[8] = { 0x4f, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64 };
|
---|
246 | uint8_t u8Version = 1;
|
---|
247 | uint8_t c8Channels;
|
---|
248 | uint16_t u16PreSkip = 0;
|
---|
249 | uint32_t u32SampleRate;
|
---|
250 | uint16_t u16Gain = 0;
|
---|
251 | uint8_t u8Mapping_family = 0;
|
---|
252 | };
|
---|
253 | # pragma pack(pop)
|
---|
254 | #endif /* VBOX_WITH_AUDIO_VIDEOREC */
|
---|
255 |
|
---|
256 | /** Operation mode. */
|
---|
257 | WebMWriter::Mode m_enmMode;
|
---|
258 | /** Audio codec to use. */
|
---|
259 | WebMWriter::AudioCodec m_enmAudioCodec;
|
---|
260 | /** Video codec to use. */
|
---|
261 | WebMWriter::VideoCodec m_enmVideoCodec;
|
---|
262 |
|
---|
263 | bool m_fDebug;
|
---|
264 |
|
---|
265 | /** Timestamp of initial PTS (Presentation Time Stamp). */
|
---|
266 | int64_t m_tsInitialPtsMs;
|
---|
267 | /** Timestamp of last written PTS (Presentation Time Stamp). */
|
---|
268 | int64_t m_tsLastPtsMs;
|
---|
269 |
|
---|
270 | vpx_rational_t m_Framerate;
|
---|
271 |
|
---|
272 | /** Start offset (in bytes) of current segment. */
|
---|
273 | uint64_t m_offSegCurStart;
|
---|
274 |
|
---|
275 | /** Start offset (in bytes) of seeking info segment. */
|
---|
276 | uint64_t m_offSegSeekInfoStart;
|
---|
277 | /** Offset (in bytes) for current seek info element. */
|
---|
278 | uint64_t m_offSeekInfo;
|
---|
279 |
|
---|
280 | /** Start offset (in bytes) of tracks segment. */
|
---|
281 | uint64_t m_offSegTracksStart;
|
---|
282 |
|
---|
283 | /** Absolute position of cue segment. */
|
---|
284 | uint64_t m_uCuePos;
|
---|
285 | /** List of cue points. Needed for seeking table. */
|
---|
286 | std::list<CueEntry> m_lstCue;
|
---|
287 |
|
---|
288 | uint64_t m_uTrackIdPos;
|
---|
289 |
|
---|
290 | /** Timestamp (in ms) when the current cluster has been opened. */
|
---|
291 | uint32_t m_tsClusterOpenMs;
|
---|
292 | /** Whether we're currently in an opened cluster segment. */
|
---|
293 | bool m_fClusterOpen;
|
---|
294 | /** Absolute position (in bytes) of current cluster within file.
|
---|
295 | * Needed for seeking info table. */
|
---|
296 | uint64_t m_offSegClusterStart;
|
---|
297 |
|
---|
298 | Ebml m_Ebml;
|
---|
299 |
|
---|
300 | public:
|
---|
301 |
|
---|
302 | WebMWriter_Impl() :
|
---|
303 | m_enmMode(WebMWriter::Mode_Unknown)
|
---|
304 | , m_fDebug(false)
|
---|
305 | , m_tsInitialPtsMs(-1)
|
---|
306 | , m_tsLastPtsMs(-1)
|
---|
307 | , m_Framerate()
|
---|
308 | , m_offSegCurStart(0)
|
---|
309 | , m_offSegSeekInfoStart(0)
|
---|
310 | , m_offSeekInfo(0)
|
---|
311 | , m_offSegTracksStart(0)
|
---|
312 | , m_uCuePos(0)
|
---|
313 | , m_uTrackIdPos(0)
|
---|
314 | , m_tsClusterOpenMs(0)
|
---|
315 | , m_fClusterOpen(false)
|
---|
316 | , m_offSegClusterStart(0) {}
|
---|
317 |
|
---|
318 | void writeHeader(const vpx_codec_enc_cfg_t *a_pCfg, const struct vpx_rational *a_pFps)
|
---|
319 | {
|
---|
320 | m_Ebml.subStart(EBML)
|
---|
321 | .serializeUnsignedInteger(EBMLVersion, 1)
|
---|
322 | .serializeUnsignedInteger(EBMLReadVersion, 1)
|
---|
323 | .serializeUnsignedInteger(EBMLMaxIDLength, 4)
|
---|
324 | .serializeUnsignedInteger(EBMLMaxSizeLength, 8)
|
---|
325 | .serializeString(DocType, "webm")
|
---|
326 | .serializeUnsignedInteger(DocTypeVersion, 2)
|
---|
327 | .serializeUnsignedInteger(DocTypeReadVersion, 2)
|
---|
328 | .subEnd(EBML);
|
---|
329 |
|
---|
330 | m_Ebml.subStart(Segment);
|
---|
331 |
|
---|
332 | m_Framerate = *a_pFps;
|
---|
333 |
|
---|
334 | /* Save offset of current segment. */
|
---|
335 | m_offSegCurStart = RTFileTell(m_Ebml.getFile());
|
---|
336 |
|
---|
337 | writeSeekInfo();
|
---|
338 |
|
---|
339 | /* Save offset of upcoming tracks segment. */
|
---|
340 | m_offSegTracksStart = RTFileTell(m_Ebml.getFile());
|
---|
341 |
|
---|
342 | m_Ebml.subStart(Tracks);
|
---|
343 |
|
---|
344 | /* Write video? */
|
---|
345 | if ( m_enmMode == WebMWriter::Mode_Video
|
---|
346 | || m_enmMode == WebMWriter::Mode_AudioVideo)
|
---|
347 | {
|
---|
348 | /*
|
---|
349 | * Video track.
|
---|
350 | */
|
---|
351 | m_Ebml.subStart(TrackEntry);
|
---|
352 | m_Ebml.serializeUnsignedInteger(TrackNumber, 1);
|
---|
353 |
|
---|
354 | m_uTrackIdPos = RTFileTell(m_Ebml.getFile());
|
---|
355 |
|
---|
356 | m_Ebml.serializeUnsignedInteger(TrackUID, 0 /* UID */, 4)
|
---|
357 | .serializeUnsignedInteger(TrackType, 1 /* Video */)
|
---|
358 | .serializeString(CodecID, "V_VP8")
|
---|
359 | .subStart(Video)
|
---|
360 | .serializeUnsignedInteger(PixelWidth, a_pCfg->g_w)
|
---|
361 | .serializeUnsignedInteger(PixelHeight, a_pCfg->g_h)
|
---|
362 | .serializeFloat(FrameRate, (double) a_pFps->num / a_pFps->den)
|
---|
363 | .subEnd(Video)
|
---|
364 | .subEnd(TrackEntry);
|
---|
365 | }
|
---|
366 |
|
---|
367 | #ifdef VBOX_WITH_AUDIO_VIDEOREC
|
---|
368 | if ( m_enmMode == WebMWriter::Mode_Audio
|
---|
369 | || m_enmMode == WebMWriter::Mode_AudioVideo)
|
---|
370 | {
|
---|
371 | /*
|
---|
372 | * Audio track.
|
---|
373 | */
|
---|
374 | m_Ebml.subStart(TrackEntry);
|
---|
375 | m_Ebml.serializeUnsignedInteger(TrackNumber, 2);
|
---|
376 | /** @todo Implement track's "Language" property? Currently this defaults to English ("eng"). */
|
---|
377 |
|
---|
378 | OpusPrivData opusPrivData;
|
---|
379 |
|
---|
380 | m_Ebml.serializeUnsignedInteger(TrackUID, 1 /* UID */, 4)
|
---|
381 | .serializeUnsignedInteger(TrackType, 2 /* Audio */)
|
---|
382 | .serializeString(CodecID, "A_OPUS")
|
---|
383 | .serializeData(CodecPrivate, &opusPrivData, sizeof(opusPrivData))
|
---|
384 | .subStart(Audio)
|
---|
385 | .serializeFloat(SamplingFrequency, 44100.0)
|
---|
386 | .serializeFloat(OutputSamplingFrequency, 44100.0)
|
---|
387 | .serializeUnsignedInteger(Channels, 2)
|
---|
388 | .serializeUnsignedInteger(BitDepth, 16)
|
---|
389 | .subEnd(Audio)
|
---|
390 | .subEnd(TrackEntry);
|
---|
391 | }
|
---|
392 | #endif
|
---|
393 |
|
---|
394 | m_Ebml.subEnd(Tracks);
|
---|
395 | }
|
---|
396 |
|
---|
397 | void writeBlock(const vpx_codec_enc_cfg_t *a_pCfg, const vpx_codec_cx_pkt_t *a_pPkt)
|
---|
398 | {
|
---|
399 | /* Calculate the PTS of this frame in milliseconds. */
|
---|
400 | int64_t iPtsMs = a_pPkt->data.frame.pts * 1000
|
---|
401 | * (uint64_t) a_pCfg->g_timebase.num / a_pCfg->g_timebase.den;
|
---|
402 |
|
---|
403 | if (iPtsMs <= m_tsLastPtsMs)
|
---|
404 | iPtsMs = m_tsLastPtsMs + 1;
|
---|
405 |
|
---|
406 | m_tsLastPtsMs = iPtsMs;
|
---|
407 |
|
---|
408 | if (m_tsInitialPtsMs < 0)
|
---|
409 | m_tsInitialPtsMs = m_tsLastPtsMs;
|
---|
410 |
|
---|
411 | /* Calculate the relative time of this block. */
|
---|
412 | uint16_t uBlockTimecode = 0;
|
---|
413 | bool fClusterStart = false;
|
---|
414 |
|
---|
415 | if (iPtsMs - m_tsClusterOpenMs > 65536)
|
---|
416 | fClusterStart = true;
|
---|
417 | else
|
---|
418 | uBlockTimecode = static_cast<uint16_t>(iPtsMs - m_tsClusterOpenMs);
|
---|
419 |
|
---|
420 | bool fKeyframe = RT_BOOL(a_pPkt->data.frame.flags & VPX_FRAME_IS_KEY);
|
---|
421 |
|
---|
422 | if ( fClusterStart
|
---|
423 | || fKeyframe)
|
---|
424 | {
|
---|
425 | if (m_fClusterOpen)
|
---|
426 | m_Ebml.subEnd(Cluster);
|
---|
427 |
|
---|
428 | /* Open a new cluster. */
|
---|
429 | uBlockTimecode = 0;
|
---|
430 | m_fClusterOpen = true;
|
---|
431 | m_tsClusterOpenMs = (uint32_t)iPtsMs;
|
---|
432 | m_offSegClusterStart = RTFileTell(m_Ebml.getFile());
|
---|
433 | m_Ebml.subStart(Cluster)
|
---|
434 | .serializeUnsignedInteger(Timecode, m_tsClusterOpenMs);
|
---|
435 |
|
---|
436 | /* Save a cue point if this is a keyframe. */
|
---|
437 | if (fKeyframe)
|
---|
438 | {
|
---|
439 | CueEntry cue(m_tsClusterOpenMs, m_offSegClusterStart);
|
---|
440 | m_lstCue.push_back(cue);
|
---|
441 | }
|
---|
442 | }
|
---|
443 |
|
---|
444 | /* Write a "Simple Block". */
|
---|
445 | m_Ebml.writeClassId(SimpleBlock);
|
---|
446 | m_Ebml.writeUnsignedInteger(0x10000000u | (4 + a_pPkt->data.frame.sz), 4);
|
---|
447 | m_Ebml.writeSize(1);
|
---|
448 | m_Ebml.writeUnsignedInteger(uBlockTimecode, 2);
|
---|
449 | m_Ebml.writeUnsignedInteger((fKeyframe ? 0x80 : 0) | (a_pPkt->data.frame.flags & VPX_FRAME_IS_INVISIBLE ? 0x08 : 0), 1);
|
---|
450 | m_Ebml.write(a_pPkt->data.frame.buf, a_pPkt->data.frame.sz);
|
---|
451 | }
|
---|
452 |
|
---|
453 | void writeFooter(uint32_t a_u64Hash)
|
---|
454 | {
|
---|
455 | if (m_fClusterOpen)
|
---|
456 | m_Ebml.subEnd(Cluster);
|
---|
457 |
|
---|
458 | m_uCuePos = RTFileTell(m_Ebml.getFile());
|
---|
459 | m_Ebml.subStart(Cues);
|
---|
460 | for (std::list<CueEntry>::iterator it = m_lstCue.begin(); it != m_lstCue.end(); ++it)
|
---|
461 | {
|
---|
462 | m_Ebml.subStart(CuePoint)
|
---|
463 | .serializeUnsignedInteger(CueTime, it->time)
|
---|
464 | .subStart(CueTrackPositions)
|
---|
465 | .serializeUnsignedInteger(CueTrack, 1)
|
---|
466 | .serializeUnsignedInteger(CueClusterPosition, it->loc - m_offSegCurStart, 8)
|
---|
467 | .subEnd(CueTrackPositions)
|
---|
468 | .subEnd(CuePoint);
|
---|
469 | }
|
---|
470 |
|
---|
471 | m_Ebml.subEnd(Cues)
|
---|
472 | .subEnd(Segment);
|
---|
473 |
|
---|
474 | writeSeekInfo();
|
---|
475 |
|
---|
476 | int rc = RTFileSeek(m_Ebml.getFile(), m_uTrackIdPos, RTFILE_SEEK_BEGIN, NULL);
|
---|
477 | if (!RT_SUCCESS(rc)) throw rc;
|
---|
478 |
|
---|
479 | m_Ebml.serializeUnsignedInteger(TrackUID, (m_fDebug ? 0xDEADBEEF : a_u64Hash), 4);
|
---|
480 |
|
---|
481 | rc = RTFileSeek(m_Ebml.getFile(), 0, RTFILE_SEEK_END, NULL);
|
---|
482 | if (!RT_SUCCESS(rc)) throw rc;
|
---|
483 | }
|
---|
484 |
|
---|
485 | friend class WebMWriter;
|
---|
486 |
|
---|
487 | private:
|
---|
488 |
|
---|
489 | void writeSeekInfo(void)
|
---|
490 | {
|
---|
491 | uint64_t uPos = RTFileTell(m_Ebml.getFile());
|
---|
492 | if (m_offSegSeekInfoStart)
|
---|
493 | RTFileSeek(m_Ebml.getFile(), m_offSegSeekInfoStart, RTFILE_SEEK_BEGIN, NULL);
|
---|
494 | else
|
---|
495 | m_offSegSeekInfoStart = uPos;
|
---|
496 |
|
---|
497 | m_Ebml.subStart(SeekHead)
|
---|
498 |
|
---|
499 | .subStart(Seek)
|
---|
500 | .serializeUnsignedInteger(SeekID, Tracks)
|
---|
501 | .serializeUnsignedInteger(SeekPosition, m_offSegTracksStart - m_offSegCurStart, 8)
|
---|
502 | .subEnd(Seek)
|
---|
503 |
|
---|
504 | .subStart(Seek)
|
---|
505 | .serializeUnsignedInteger(SeekID, Cues)
|
---|
506 | .serializeUnsignedInteger(SeekPosition, m_uCuePos - m_offSegCurStart, 8)
|
---|
507 | .subEnd(Seek)
|
---|
508 |
|
---|
509 | .subStart(Seek)
|
---|
510 | .serializeUnsignedInteger(SeekID, Info)
|
---|
511 | .serializeUnsignedInteger(SeekPosition, m_offSeekInfo - m_offSegCurStart, 8)
|
---|
512 | .subEnd(Seek)
|
---|
513 |
|
---|
514 | .subEnd(SeekHead);
|
---|
515 |
|
---|
516 | int64_t iFrameTime = (int64_t)1000 * m_Framerate.den / m_Framerate.num;
|
---|
517 | m_offSeekInfo = RTFileTell(m_Ebml.getFile());
|
---|
518 |
|
---|
519 | char szVersion[64];
|
---|
520 | RTStrPrintf(szVersion, sizeof(szVersion), "vpxenc%s",
|
---|
521 | m_fDebug ? "" : vpx_codec_version_str());
|
---|
522 |
|
---|
523 | m_Ebml.subStart(Info)
|
---|
524 | .serializeUnsignedInteger(TimecodeScale, 1000000)
|
---|
525 | .serializeFloat(Segment_Duration, m_tsLastPtsMs + iFrameTime - m_tsInitialPtsMs)
|
---|
526 | .serializeString(MuxingApp, szVersion)
|
---|
527 | .serializeString(WritingApp, szVersion)
|
---|
528 | .subEnd(Info);
|
---|
529 | }
|
---|
530 | };
|
---|
531 |
|
---|
532 | WebMWriter::WebMWriter(void) : m_pImpl(new WebMWriter_Impl()) {}
|
---|
533 |
|
---|
534 | WebMWriter::~WebMWriter(void)
|
---|
535 | {
|
---|
536 | if (m_pImpl)
|
---|
537 | delete m_pImpl;
|
---|
538 | }
|
---|
539 |
|
---|
540 | int WebMWriter::create(const char *a_pszFilename, uint64_t a_fOpen, WebMWriter::Mode a_enmMode,
|
---|
541 | WebMWriter::AudioCodec a_enmAudioCodec, WebMWriter::VideoCodec a_enmVideoCodec)
|
---|
542 | {
|
---|
543 | m_pImpl->m_enmMode = a_enmMode;
|
---|
544 | m_pImpl->m_enmAudioCodec = a_enmAudioCodec;
|
---|
545 | m_pImpl->m_enmVideoCodec = a_enmVideoCodec;
|
---|
546 |
|
---|
547 | return m_pImpl->m_Ebml.create(a_pszFilename, a_fOpen);
|
---|
548 | }
|
---|
549 |
|
---|
550 | void WebMWriter::close(void)
|
---|
551 | {
|
---|
552 | m_pImpl->m_Ebml.close();
|
---|
553 | }
|
---|
554 |
|
---|
555 | int WebMWriter::writeHeader(const vpx_codec_enc_cfg_t *a_pCfg, const vpx_rational *a_pFps)
|
---|
556 | {
|
---|
557 | try
|
---|
558 | {
|
---|
559 | m_pImpl->writeHeader(a_pCfg, a_pFps);
|
---|
560 | }
|
---|
561 | catch(int rc)
|
---|
562 | {
|
---|
563 | return rc;
|
---|
564 | }
|
---|
565 | return VINF_SUCCESS;
|
---|
566 | }
|
---|
567 |
|
---|
568 | int WebMWriter::writeBlock(const vpx_codec_enc_cfg_t *a_pCfg, const vpx_codec_cx_pkt_t *a_pPkt)
|
---|
569 | {
|
---|
570 | try
|
---|
571 | {
|
---|
572 | m_pImpl->writeBlock(a_pCfg, a_pPkt);
|
---|
573 | }
|
---|
574 | catch(int rc)
|
---|
575 | {
|
---|
576 | return rc;
|
---|
577 | }
|
---|
578 | return VINF_SUCCESS;
|
---|
579 | }
|
---|
580 |
|
---|
581 | int WebMWriter::writeFooter(uint32_t a_u64Hash)
|
---|
582 | {
|
---|
583 | try
|
---|
584 | {
|
---|
585 | m_pImpl->writeFooter(a_u64Hash);
|
---|
586 | }
|
---|
587 | catch(int rc)
|
---|
588 | {
|
---|
589 | return rc;
|
---|
590 | }
|
---|
591 | return VINF_SUCCESS;
|
---|
592 | }
|
---|
593 |
|
---|
594 | uint64_t WebMWriter::getFileSize()
|
---|
595 | {
|
---|
596 | return m_pImpl->m_Ebml.getFileSize();
|
---|
597 | }
|
---|
598 |
|
---|
599 | uint64_t WebMWriter::getAvailableSpace()
|
---|
600 | {
|
---|
601 | return m_pImpl->m_Ebml.getAvailableSpace();
|
---|
602 | }
|
---|
603 |
|
---|