1 | /* $Id: WebMWriter.cpp 75030 2018-10-24 11:43:09Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * WebMWriter.cpp - WebM container handling.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2018 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 | * For more information, see:
|
---|
20 | * - https://w3c.github.io/media-source/webm-byte-stream-format.html
|
---|
21 | * - https://www.webmproject.org/docs/container/#muxer-guidelines
|
---|
22 | */
|
---|
23 |
|
---|
24 | #define LOG_GROUP LOG_GROUP_MAIN_DISPLAY
|
---|
25 | #include "LoggingNew.h"
|
---|
26 |
|
---|
27 | #include <list>
|
---|
28 | #include <map>
|
---|
29 | #include <queue>
|
---|
30 | #include <stack>
|
---|
31 |
|
---|
32 | #include <math.h> /* For lround.h. */
|
---|
33 |
|
---|
34 | #include <iprt/asm.h>
|
---|
35 | #include <iprt/buildconfig.h>
|
---|
36 | #include <iprt/cdefs.h>
|
---|
37 | #include <iprt/critsect.h>
|
---|
38 | #include <iprt/err.h>
|
---|
39 | #include <iprt/file.h>
|
---|
40 | #include <iprt/rand.h>
|
---|
41 | #include <iprt/string.h>
|
---|
42 |
|
---|
43 | #include <VBox/log.h>
|
---|
44 | #include <VBox/version.h>
|
---|
45 |
|
---|
46 | #include "WebMWriter.h"
|
---|
47 | #include "EBML_MKV.h"
|
---|
48 |
|
---|
49 |
|
---|
50 | WebMWriter::WebMWriter(void)
|
---|
51 | {
|
---|
52 | /* Size (in bytes) of time code to write. We use 2 bytes (16 bit) by default. */
|
---|
53 | m_cbTimecode = 2;
|
---|
54 | m_uTimecodeMax = UINT16_MAX;
|
---|
55 |
|
---|
56 | m_fInTracksSection = false;
|
---|
57 | }
|
---|
58 |
|
---|
59 | WebMWriter::~WebMWriter(void)
|
---|
60 | {
|
---|
61 | Close();
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Opens (creates) an output file using an already open file handle.
|
---|
66 | *
|
---|
67 | * @returns VBox status code.
|
---|
68 | * @param a_pszFilename Name of the file the file handle points at.
|
---|
69 | * @param a_phFile Pointer to open file handle to use.
|
---|
70 | * @param a_enmAudioCodec Audio codec to use.
|
---|
71 | * @param a_enmVideoCodec Video codec to use.
|
---|
72 | */
|
---|
73 | int WebMWriter::OpenEx(const char *a_pszFilename, PRTFILE a_phFile,
|
---|
74 | WebMWriter::AudioCodec a_enmAudioCodec, WebMWriter::VideoCodec a_enmVideoCodec)
|
---|
75 | {
|
---|
76 | try
|
---|
77 | {
|
---|
78 | m_enmAudioCodec = a_enmAudioCodec;
|
---|
79 | m_enmVideoCodec = a_enmVideoCodec;
|
---|
80 |
|
---|
81 | LogFunc(("Creating '%s'\n", a_pszFilename));
|
---|
82 |
|
---|
83 | int rc = createEx(a_pszFilename, a_phFile);
|
---|
84 | if (RT_SUCCESS(rc))
|
---|
85 | {
|
---|
86 | rc = init();
|
---|
87 | if (RT_SUCCESS(rc))
|
---|
88 | rc = writeHeader();
|
---|
89 | }
|
---|
90 | }
|
---|
91 | catch(int rc)
|
---|
92 | {
|
---|
93 | return rc;
|
---|
94 | }
|
---|
95 | return VINF_SUCCESS;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Opens an output file.
|
---|
100 | *
|
---|
101 | * @returns VBox status code.
|
---|
102 | * @param a_pszFilename Name of the file to create.
|
---|
103 | * @param a_fOpen File open mode of type RTFILE_O_.
|
---|
104 | * @param a_enmAudioCodec Audio codec to use.
|
---|
105 | * @param a_enmVideoCodec Video codec to use.
|
---|
106 | */
|
---|
107 | int WebMWriter::Open(const char *a_pszFilename, uint64_t a_fOpen,
|
---|
108 | WebMWriter::AudioCodec a_enmAudioCodec, WebMWriter::VideoCodec a_enmVideoCodec)
|
---|
109 | {
|
---|
110 | try
|
---|
111 | {
|
---|
112 | m_enmAudioCodec = a_enmAudioCodec;
|
---|
113 | m_enmVideoCodec = a_enmVideoCodec;
|
---|
114 |
|
---|
115 | LogFunc(("Creating '%s'\n", a_pszFilename));
|
---|
116 |
|
---|
117 | int rc = create(a_pszFilename, a_fOpen);
|
---|
118 | if (RT_SUCCESS(rc))
|
---|
119 | {
|
---|
120 | rc = init();
|
---|
121 | if (RT_SUCCESS(rc))
|
---|
122 | rc = writeHeader();
|
---|
123 | }
|
---|
124 | }
|
---|
125 | catch(int rc)
|
---|
126 | {
|
---|
127 | return rc;
|
---|
128 | }
|
---|
129 | return VINF_SUCCESS;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Closes the WebM file and drains all queues.
|
---|
134 | *
|
---|
135 | * @returns IPRT status code.
|
---|
136 | */
|
---|
137 | int WebMWriter::Close(void)
|
---|
138 | {
|
---|
139 | LogFlowFuncEnter();
|
---|
140 |
|
---|
141 | if (!isOpen())
|
---|
142 | return VINF_SUCCESS;
|
---|
143 |
|
---|
144 | /* Make sure to drain all queues. */
|
---|
145 | processQueues(&CurSeg.queueBlocks, true /* fForce */);
|
---|
146 |
|
---|
147 | writeFooter();
|
---|
148 |
|
---|
149 | WebMTracks::iterator itTrack = CurSeg.mapTracks.begin();
|
---|
150 | while (itTrack != CurSeg.mapTracks.end())
|
---|
151 | {
|
---|
152 | WebMTrack *pTrack = itTrack->second;
|
---|
153 | if (pTrack) /* Paranoia. */
|
---|
154 | delete pTrack;
|
---|
155 |
|
---|
156 | CurSeg.mapTracks.erase(itTrack);
|
---|
157 |
|
---|
158 | itTrack = CurSeg.mapTracks.begin();
|
---|
159 | }
|
---|
160 |
|
---|
161 | Assert(CurSeg.queueBlocks.Map.size() == 0);
|
---|
162 | Assert(CurSeg.mapTracks.size() == 0);
|
---|
163 |
|
---|
164 | Utf8Str strFileName = getFileName().c_str();
|
---|
165 |
|
---|
166 | close();
|
---|
167 |
|
---|
168 | int rc = VINF_SUCCESS;
|
---|
169 |
|
---|
170 | /* If no clusters (= data) was written, delete the file again. */
|
---|
171 | if (!CurSeg.cClusters)
|
---|
172 | rc = RTFileDelete(strFileName.c_str());
|
---|
173 |
|
---|
174 | LogFlowFuncLeaveRC(rc);
|
---|
175 | return rc;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Adds an audio track.
|
---|
180 | *
|
---|
181 | * @returns IPRT status code.
|
---|
182 | * @param uHz Input sampling rate.
|
---|
183 | * Must be supported by the selected audio codec.
|
---|
184 | * @param cChannels Number of input audio channels.
|
---|
185 | * @param cBits Number of input bits per channel.
|
---|
186 | * @param puTrack Track number on successful creation. Optional.
|
---|
187 | */
|
---|
188 | int WebMWriter::AddAudioTrack(uint16_t uHz, uint8_t cChannels, uint8_t cBits, uint8_t *puTrack)
|
---|
189 | {
|
---|
190 | #ifdef VBOX_WITH_LIBOPUS
|
---|
191 | AssertReturn(uHz, VERR_INVALID_PARAMETER);
|
---|
192 | AssertReturn(cBits, VERR_INVALID_PARAMETER);
|
---|
193 | AssertReturn(cChannels, VERR_INVALID_PARAMETER);
|
---|
194 |
|
---|
195 | /*
|
---|
196 | * Adjust the handed-in Hz rate to values which are supported by the Opus codec.
|
---|
197 | *
|
---|
198 | * Only the following values are supported by an Opus standard build
|
---|
199 | * -- every other rate only is supported by a custom build.
|
---|
200 | *
|
---|
201 | * See opus_encoder_create() for more information.
|
---|
202 | */
|
---|
203 | if (uHz > 24000) uHz = 48000;
|
---|
204 | else if (uHz > 16000) uHz = 24000;
|
---|
205 | else if (uHz > 12000) uHz = 16000;
|
---|
206 | else if (uHz > 8000 ) uHz = 12000;
|
---|
207 | else uHz = 8000;
|
---|
208 |
|
---|
209 | /* Some players (e.g. Firefox with Nestegg) rely on track numbers starting at 1.
|
---|
210 | * Using a track number 0 will show those files as being corrupted. */
|
---|
211 | const uint8_t uTrack = (uint8_t)CurSeg.mapTracks.size() + 1;
|
---|
212 |
|
---|
213 | subStart(MkvElem_TrackEntry);
|
---|
214 |
|
---|
215 | serializeUnsignedInteger(MkvElem_TrackNumber, (uint8_t)uTrack);
|
---|
216 | serializeString (MkvElem_Language, "und" /* "Undefined"; see ISO-639-2. */);
|
---|
217 | serializeUnsignedInteger(MkvElem_FlagLacing, (uint8_t)0);
|
---|
218 |
|
---|
219 | WebMTrack *pTrack = new WebMTrack(WebMTrackType_Audio, uTrack, RTFileTell(getFile()));
|
---|
220 |
|
---|
221 | pTrack->Audio.uHz = uHz;
|
---|
222 | pTrack->Audio.msPerBlock = 20; /** Opus uses 20ms by default. Make this configurable? */
|
---|
223 | pTrack->Audio.framesPerBlock = uHz / (1000 /* s in ms */ / pTrack->Audio.msPerBlock);
|
---|
224 |
|
---|
225 | WEBMOPUSPRIVDATA opusPrivData(uHz, cChannels);
|
---|
226 |
|
---|
227 | LogFunc(("Opus @ %RU16Hz (%RU16ms + %RU16 frames per block)\n",
|
---|
228 | pTrack->Audio.uHz, pTrack->Audio.msPerBlock, pTrack->Audio.framesPerBlock));
|
---|
229 |
|
---|
230 | serializeUnsignedInteger(MkvElem_TrackUID, pTrack->uUUID, 4)
|
---|
231 | .serializeUnsignedInteger(MkvElem_TrackType, 2 /* Audio */)
|
---|
232 | .serializeString(MkvElem_CodecID, "A_OPUS")
|
---|
233 | .serializeData(MkvElem_CodecPrivate, &opusPrivData, sizeof(opusPrivData))
|
---|
234 | .serializeUnsignedInteger(MkvElem_CodecDelay, 0)
|
---|
235 | .serializeUnsignedInteger(MkvElem_SeekPreRoll, 80 * 1000000) /* 80ms in ns. */
|
---|
236 | .subStart(MkvElem_Audio)
|
---|
237 | .serializeFloat(MkvElem_SamplingFrequency, (float)uHz)
|
---|
238 | .serializeUnsignedInteger(MkvElem_Channels, cChannels)
|
---|
239 | .serializeUnsignedInteger(MkvElem_BitDepth, cBits)
|
---|
240 | .subEnd(MkvElem_Audio)
|
---|
241 | .subEnd(MkvElem_TrackEntry);
|
---|
242 |
|
---|
243 | CurSeg.mapTracks[uTrack] = pTrack;
|
---|
244 |
|
---|
245 | if (puTrack)
|
---|
246 | *puTrack = uTrack;
|
---|
247 |
|
---|
248 | return VINF_SUCCESS;
|
---|
249 | #else
|
---|
250 | RT_NOREF(uHz, cChannels, cBits, puTrack);
|
---|
251 | return VERR_NOT_SUPPORTED;
|
---|
252 | #endif
|
---|
253 | }
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Adds a video track.
|
---|
257 | *
|
---|
258 | * @returns IPRT status code.
|
---|
259 | * @param uWidth Width (in pixels) of the video track.
|
---|
260 | * @param uHeight Height (in pixels) of the video track.
|
---|
261 | * @param uFPS FPS (Frames Per Second) of the video track.
|
---|
262 | * @param puTrack Track number of the added video track on success. Optional.
|
---|
263 | */
|
---|
264 | int WebMWriter::AddVideoTrack(uint16_t uWidth, uint16_t uHeight, uint32_t uFPS, uint8_t *puTrack)
|
---|
265 | {
|
---|
266 | #ifdef VBOX_WITH_LIBVPX
|
---|
267 | /* Some players (e.g. Firefox with Nestegg) rely on track numbers starting at 1.
|
---|
268 | * Using a track number 0 will show those files as being corrupted. */
|
---|
269 | const uint8_t uTrack = (uint8_t)CurSeg.mapTracks.size() + 1;
|
---|
270 |
|
---|
271 | subStart(MkvElem_TrackEntry);
|
---|
272 |
|
---|
273 | serializeUnsignedInteger(MkvElem_TrackNumber, (uint8_t)uTrack);
|
---|
274 | serializeString (MkvElem_Language, "und" /* "Undefined"; see ISO-639-2. */);
|
---|
275 | serializeUnsignedInteger(MkvElem_FlagLacing, (uint8_t)0);
|
---|
276 |
|
---|
277 | WebMTrack *pTrack = new WebMTrack(WebMTrackType_Video, uTrack, RTFileTell(getFile()));
|
---|
278 |
|
---|
279 | /** @todo Resolve codec type. */
|
---|
280 | serializeUnsignedInteger(MkvElem_TrackUID, pTrack->uUUID /* UID */, 4)
|
---|
281 | .serializeUnsignedInteger(MkvElem_TrackType, 1 /* Video */)
|
---|
282 | .serializeString(MkvElem_CodecID, "V_VP8")
|
---|
283 | .subStart(MkvElem_Video)
|
---|
284 | .serializeUnsignedInteger(MkvElem_PixelWidth, uWidth)
|
---|
285 | .serializeUnsignedInteger(MkvElem_PixelHeight, uHeight)
|
---|
286 | /* Some players rely on the FPS rate for timing calculations.
|
---|
287 | * So make sure to *always* include that. */
|
---|
288 | .serializeFloat (MkvElem_FrameRate, (float)uFPS)
|
---|
289 | .subEnd(MkvElem_Video);
|
---|
290 |
|
---|
291 | subEnd(MkvElem_TrackEntry);
|
---|
292 |
|
---|
293 | CurSeg.mapTracks[uTrack] = pTrack;
|
---|
294 |
|
---|
295 | if (puTrack)
|
---|
296 | *puTrack = uTrack;
|
---|
297 |
|
---|
298 | return VINF_SUCCESS;
|
---|
299 | #else
|
---|
300 | RT_NOREF(uWidth, uHeight, dbFPS, puTrack);
|
---|
301 | return VERR_NOT_SUPPORTED;
|
---|
302 | #endif
|
---|
303 | }
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * Gets file name.
|
---|
307 | *
|
---|
308 | * @returns File name as UTF-8 string.
|
---|
309 | */
|
---|
310 | const Utf8Str& WebMWriter::GetFileName(void)
|
---|
311 | {
|
---|
312 | return getFileName();
|
---|
313 | }
|
---|
314 |
|
---|
315 | /**
|
---|
316 | * Gets current output file size.
|
---|
317 | *
|
---|
318 | * @returns File size in bytes.
|
---|
319 | */
|
---|
320 | uint64_t WebMWriter::GetFileSize(void)
|
---|
321 | {
|
---|
322 | return getFileSize();
|
---|
323 | }
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Gets current free storage space available for the file.
|
---|
327 | *
|
---|
328 | * @returns Available storage free space.
|
---|
329 | */
|
---|
330 | uint64_t WebMWriter::GetAvailableSpace(void)
|
---|
331 | {
|
---|
332 | return getAvailableSpace();
|
---|
333 | }
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * Takes care of the initialization of the instance.
|
---|
337 | *
|
---|
338 | * @returns IPRT status code.
|
---|
339 | */
|
---|
340 | int WebMWriter::init(void)
|
---|
341 | {
|
---|
342 | return CurSeg.init();
|
---|
343 | }
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * Takes care of the destruction of the instance.
|
---|
347 | */
|
---|
348 | void WebMWriter::destroy(void)
|
---|
349 | {
|
---|
350 | CurSeg.destroy();
|
---|
351 | }
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Writes the WebM file header.
|
---|
355 | *
|
---|
356 | * @returns IPRT status code.
|
---|
357 | */
|
---|
358 | int WebMWriter::writeHeader(void)
|
---|
359 | {
|
---|
360 | LogFunc(("Header @ %RU64\n", RTFileTell(getFile())));
|
---|
361 |
|
---|
362 | subStart(MkvElem_EBML)
|
---|
363 | .serializeUnsignedInteger(MkvElem_EBMLVersion, 1)
|
---|
364 | .serializeUnsignedInteger(MkvElem_EBMLReadVersion, 1)
|
---|
365 | .serializeUnsignedInteger(MkvElem_EBMLMaxIDLength, 4)
|
---|
366 | .serializeUnsignedInteger(MkvElem_EBMLMaxSizeLength, 8)
|
---|
367 | .serializeString(MkvElem_DocType, "webm")
|
---|
368 | .serializeUnsignedInteger(MkvElem_DocTypeVersion, 2)
|
---|
369 | .serializeUnsignedInteger(MkvElem_DocTypeReadVersion, 2)
|
---|
370 | .subEnd(MkvElem_EBML);
|
---|
371 |
|
---|
372 | subStart(MkvElem_Segment);
|
---|
373 |
|
---|
374 | /* Save offset of current segment. */
|
---|
375 | CurSeg.offStart = RTFileTell(getFile());
|
---|
376 |
|
---|
377 | writeSeekHeader();
|
---|
378 |
|
---|
379 | /* Save offset of upcoming tracks segment. */
|
---|
380 | CurSeg.offTracks = RTFileTell(getFile());
|
---|
381 |
|
---|
382 | /* The tracks segment starts right after this header. */
|
---|
383 | subStart(MkvElem_Tracks);
|
---|
384 | m_fInTracksSection = true;
|
---|
385 |
|
---|
386 | return VINF_SUCCESS;
|
---|
387 | }
|
---|
388 |
|
---|
389 | /**
|
---|
390 | * Writes a simple block into the EBML structure.
|
---|
391 | *
|
---|
392 | * @returns IPRT status code.
|
---|
393 | * @param a_pTrack Track the simple block is assigned to.
|
---|
394 | * @param a_pBlock Simple block to write.
|
---|
395 | */
|
---|
396 | int WebMWriter::writeSimpleBlockEBML(WebMTrack *a_pTrack, WebMSimpleBlock *a_pBlock)
|
---|
397 | {
|
---|
398 | #ifdef LOG_ENABLED
|
---|
399 | WebMCluster &Cluster = CurSeg.CurCluster;
|
---|
400 |
|
---|
401 | Log3Func(("[T%RU8C%RU64] Off=%RU64, AbsPTSMs=%RU64, RelToClusterMs=%RU16, %zu bytes\n",
|
---|
402 | a_pTrack->uTrack, Cluster.uID, RTFileTell(getFile()),
|
---|
403 | a_pBlock->Data.tcAbsPTSMs, a_pBlock->Data.tcRelToClusterMs, a_pBlock->Data.cb));
|
---|
404 | #endif
|
---|
405 | /*
|
---|
406 | * Write a "Simple Block".
|
---|
407 | */
|
---|
408 | writeClassId(MkvElem_SimpleBlock);
|
---|
409 | /* Block size. */
|
---|
410 | writeUnsignedInteger(0x10000000u | ( 1 /* Track number size. */
|
---|
411 | + m_cbTimecode /* Timecode size .*/
|
---|
412 | + 1 /* Flags size. */
|
---|
413 | + a_pBlock->Data.cb /* Actual frame data size. */), 4);
|
---|
414 | /* Track number. */
|
---|
415 | writeSize(a_pTrack->uTrack);
|
---|
416 | /* Timecode (relative to cluster opening timecode). */
|
---|
417 | writeUnsignedInteger(a_pBlock->Data.tcRelToClusterMs, m_cbTimecode);
|
---|
418 | /* Flags. */
|
---|
419 | writeUnsignedInteger(a_pBlock->Data.fFlags, 1);
|
---|
420 | /* Frame data. */
|
---|
421 | write(a_pBlock->Data.pv, a_pBlock->Data.cb);
|
---|
422 |
|
---|
423 | return VINF_SUCCESS;
|
---|
424 | }
|
---|
425 |
|
---|
426 | /**
|
---|
427 | * Writes a simple block and enqueues it into the segment's render queue.
|
---|
428 | *
|
---|
429 | * @returns IPRT status code.
|
---|
430 | * @param a_pTrack Track the simple block is assigned to.
|
---|
431 | * @param a_pBlock Simple block to write and enqueue.
|
---|
432 | */
|
---|
433 | int WebMWriter::writeSimpleBlockQueued(WebMTrack *a_pTrack, WebMSimpleBlock *a_pBlock)
|
---|
434 | {
|
---|
435 | RT_NOREF(a_pTrack);
|
---|
436 |
|
---|
437 | int rc = VINF_SUCCESS;
|
---|
438 |
|
---|
439 | try
|
---|
440 | {
|
---|
441 | const WebMTimecodeAbs tcAbsPTS = a_pBlock->Data.tcAbsPTSMs;
|
---|
442 |
|
---|
443 | /* Check if the current segment already has been started.
|
---|
444 | * If not, use the current (absolute) time stamp for it. */
|
---|
445 | if (CurSeg.tcAbsStartMs == 0)
|
---|
446 | {
|
---|
447 | CurSeg.tcAbsStartMs = tcAbsPTS;
|
---|
448 | LogFunc(("Segment started @ %RU64ms\n", CurSeg.tcAbsStartMs));
|
---|
449 | }
|
---|
450 |
|
---|
451 | /* See if we already have an entry for the specified timecode in our queue. */
|
---|
452 | WebMBlockMap::iterator itQueue = CurSeg.queueBlocks.Map.find(tcAbsPTS);
|
---|
453 | if (itQueue != CurSeg.queueBlocks.Map.end()) /* Use existing queue. */
|
---|
454 | {
|
---|
455 | WebMTimecodeBlocks &Blocks = itQueue->second;
|
---|
456 | Blocks.Enqueue(a_pBlock);
|
---|
457 | }
|
---|
458 | else /* Create a new timecode entry. */
|
---|
459 | {
|
---|
460 | WebMTimecodeBlocks Blocks;
|
---|
461 | Blocks.Enqueue(a_pBlock);
|
---|
462 |
|
---|
463 | CurSeg.queueBlocks.Map[tcAbsPTS] = Blocks;
|
---|
464 | }
|
---|
465 |
|
---|
466 | processQueues(&CurSeg.queueBlocks, false /* fForce */);
|
---|
467 | }
|
---|
468 | catch(...)
|
---|
469 | {
|
---|
470 | delete a_pBlock;
|
---|
471 | a_pBlock = NULL;
|
---|
472 |
|
---|
473 | rc = VERR_NO_MEMORY;
|
---|
474 | }
|
---|
475 |
|
---|
476 | return rc;
|
---|
477 | }
|
---|
478 |
|
---|
479 | #ifdef VBOX_WITH_LIBVPX
|
---|
480 | /**
|
---|
481 | * Writes VPX (VP8 video) simple data block.
|
---|
482 | *
|
---|
483 | * @returns IPRT status code.
|
---|
484 | * @param a_pTrack Track ID to write data to.
|
---|
485 | * @param a_pCfg VPX encoder configuration to use.
|
---|
486 | * @param a_pPkt VPX packet video data packet to write.
|
---|
487 | */
|
---|
488 | int WebMWriter::writeSimpleBlockVP8(WebMTrack *a_pTrack, const vpx_codec_enc_cfg_t *a_pCfg, const vpx_codec_cx_pkt_t *a_pPkt)
|
---|
489 | {
|
---|
490 | RT_NOREF(a_pTrack);
|
---|
491 |
|
---|
492 | /* Calculate the absolute PTS of this frame (in ms). */
|
---|
493 | WebMTimecodeAbs tcAbsPTSMs = a_pPkt->data.frame.pts * 1000
|
---|
494 | * (uint64_t) a_pCfg->g_timebase.num / a_pCfg->g_timebase.den;
|
---|
495 | if ( tcAbsPTSMs
|
---|
496 | && tcAbsPTSMs <= a_pTrack->tcAbsLastWrittenMs)
|
---|
497 | {
|
---|
498 | tcAbsPTSMs = a_pTrack->tcAbsLastWrittenMs + 1;
|
---|
499 | }
|
---|
500 |
|
---|
501 | const bool fKeyframe = RT_BOOL(a_pPkt->data.frame.flags & VPX_FRAME_IS_KEY);
|
---|
502 |
|
---|
503 | uint8_t fFlags = VBOX_WEBM_BLOCK_FLAG_NONE;
|
---|
504 | if (fKeyframe)
|
---|
505 | fFlags |= VBOX_WEBM_BLOCK_FLAG_KEY_FRAME;
|
---|
506 | if (a_pPkt->data.frame.flags & VPX_FRAME_IS_INVISIBLE)
|
---|
507 | fFlags |= VBOX_WEBM_BLOCK_FLAG_INVISIBLE;
|
---|
508 |
|
---|
509 | return writeSimpleBlockQueued(a_pTrack,
|
---|
510 | new WebMSimpleBlock(a_pTrack,
|
---|
511 | tcAbsPTSMs, a_pPkt->data.frame.buf, a_pPkt->data.frame.sz, fFlags));
|
---|
512 | }
|
---|
513 | #endif /* VBOX_WITH_LIBVPX */
|
---|
514 |
|
---|
515 | #ifdef VBOX_WITH_LIBOPUS
|
---|
516 | /**
|
---|
517 | * Writes an Opus (audio) simple data block.
|
---|
518 | *
|
---|
519 | * @returns IPRT status code.
|
---|
520 | * @param a_pTrack Track ID to write data to.
|
---|
521 | * @param pvData Pointer to simple data block to write.
|
---|
522 | * @param cbData Size (in bytes) of simple data block to write.
|
---|
523 | * @param tcAbsPTSMs Absolute PTS of simple data block.
|
---|
524 | *
|
---|
525 | * @remarks Audio blocks that have same absolute timecode as video blocks SHOULD be written before the video blocks.
|
---|
526 | */
|
---|
527 | int WebMWriter::writeSimpleBlockOpus(WebMTrack *a_pTrack, const void *pvData, size_t cbData, WebMTimecodeAbs tcAbsPTSMs)
|
---|
528 | {
|
---|
529 | AssertPtrReturn(a_pTrack, VERR_INVALID_POINTER);
|
---|
530 | AssertPtrReturn(pvData, VERR_INVALID_POINTER);
|
---|
531 | AssertReturn(cbData, VERR_INVALID_PARAMETER);
|
---|
532 |
|
---|
533 | /* Every Opus frame is a key frame. */
|
---|
534 | const uint8_t fFlags = VBOX_WEBM_BLOCK_FLAG_KEY_FRAME;
|
---|
535 |
|
---|
536 | return writeSimpleBlockQueued(a_pTrack,
|
---|
537 | new WebMSimpleBlock(a_pTrack, tcAbsPTSMs, pvData, cbData, fFlags));
|
---|
538 | }
|
---|
539 | #endif /* VBOX_WITH_LIBOPUS */
|
---|
540 |
|
---|
541 | /**
|
---|
542 | * Writes a data block to the specified track.
|
---|
543 | *
|
---|
544 | * @returns IPRT status code.
|
---|
545 | * @param uTrack Track ID to write data to.
|
---|
546 | * @param pvData Pointer to data block to write.
|
---|
547 | * @param cbData Size (in bytes) of data block to write.
|
---|
548 | */
|
---|
549 | int WebMWriter::WriteBlock(uint8_t uTrack, const void *pvData, size_t cbData)
|
---|
550 | {
|
---|
551 | RT_NOREF(cbData); /* Only needed for assertions for now. */
|
---|
552 |
|
---|
553 | int rc = RTCritSectEnter(&CurSeg.CritSect);
|
---|
554 | AssertRC(rc);
|
---|
555 |
|
---|
556 | WebMTracks::iterator itTrack = CurSeg.mapTracks.find(uTrack);
|
---|
557 | if (itTrack == CurSeg.mapTracks.end())
|
---|
558 | {
|
---|
559 | RTCritSectLeave(&CurSeg.CritSect);
|
---|
560 | return VERR_NOT_FOUND;
|
---|
561 | }
|
---|
562 |
|
---|
563 | WebMTrack *pTrack = itTrack->second;
|
---|
564 | AssertPtr(pTrack);
|
---|
565 |
|
---|
566 | if (m_fInTracksSection)
|
---|
567 | {
|
---|
568 | subEnd(MkvElem_Tracks);
|
---|
569 | m_fInTracksSection = false;
|
---|
570 | }
|
---|
571 |
|
---|
572 | switch (pTrack->enmType)
|
---|
573 | {
|
---|
574 |
|
---|
575 | case WebMTrackType_Audio:
|
---|
576 | {
|
---|
577 | #ifdef VBOX_WITH_LIBOPUS
|
---|
578 | if (m_enmAudioCodec == WebMWriter::AudioCodec_Opus)
|
---|
579 | {
|
---|
580 | Assert(cbData == sizeof(WebMWriter::BlockData_Opus));
|
---|
581 | WebMWriter::BlockData_Opus *pData = (WebMWriter::BlockData_Opus *)pvData;
|
---|
582 | rc = writeSimpleBlockOpus(pTrack, pData->pvData, pData->cbData, pData->uPTSMs);
|
---|
583 | }
|
---|
584 | else
|
---|
585 | #endif /* VBOX_WITH_LIBOPUS */
|
---|
586 | rc = VERR_NOT_SUPPORTED;
|
---|
587 | break;
|
---|
588 | }
|
---|
589 |
|
---|
590 | case WebMTrackType_Video:
|
---|
591 | {
|
---|
592 | #ifdef VBOX_WITH_LIBVPX
|
---|
593 | if (m_enmVideoCodec == WebMWriter::VideoCodec_VP8)
|
---|
594 | {
|
---|
595 | Assert(cbData == sizeof(WebMWriter::BlockData_VP8));
|
---|
596 | WebMWriter::BlockData_VP8 *pData = (WebMWriter::BlockData_VP8 *)pvData;
|
---|
597 | rc = writeSimpleBlockVP8(pTrack, pData->pCfg, pData->pPkt);
|
---|
598 | }
|
---|
599 | else
|
---|
600 | #endif /* VBOX_WITH_LIBVPX */
|
---|
601 | rc = VERR_NOT_SUPPORTED;
|
---|
602 | break;
|
---|
603 | }
|
---|
604 |
|
---|
605 | default:
|
---|
606 | rc = VERR_NOT_SUPPORTED;
|
---|
607 | break;
|
---|
608 | }
|
---|
609 |
|
---|
610 | int rc2 = RTCritSectLeave(&CurSeg.CritSect);
|
---|
611 | AssertRC(rc2);
|
---|
612 |
|
---|
613 | return rc;
|
---|
614 | }
|
---|
615 |
|
---|
616 | /**
|
---|
617 | * Processes a render queue.
|
---|
618 | *
|
---|
619 | * @returns IPRT status code.
|
---|
620 | * @param pQueue Queue to process.
|
---|
621 | * @param fForce Whether forcing to process the render queue or not.
|
---|
622 | * Needed to drain the queues when terminating.
|
---|
623 | */
|
---|
624 | int WebMWriter::processQueues(WebMQueue *pQueue, bool fForce)
|
---|
625 | {
|
---|
626 | if (pQueue->tsLastProcessedMs == 0)
|
---|
627 | pQueue->tsLastProcessedMs = RTTimeMilliTS();
|
---|
628 |
|
---|
629 | if (!fForce)
|
---|
630 | {
|
---|
631 | /* Only process when we reached a certain threshold. */
|
---|
632 | if (RTTimeMilliTS() - pQueue->tsLastProcessedMs < 5000 /* ms */ /** @todo Make this configurable */)
|
---|
633 | return VINF_SUCCESS;
|
---|
634 | }
|
---|
635 |
|
---|
636 | WebMCluster &Cluster = CurSeg.CurCluster;
|
---|
637 |
|
---|
638 | /* Iterate through the block map. */
|
---|
639 | WebMBlockMap::iterator it = pQueue->Map.begin();
|
---|
640 | while (it != CurSeg.queueBlocks.Map.end())
|
---|
641 | {
|
---|
642 | WebMTimecodeAbs mapAbsPTSMs = it->first;
|
---|
643 | WebMTimecodeBlocks mapBlocks = it->second;
|
---|
644 |
|
---|
645 | /* Whether to start a new cluster or not. */
|
---|
646 | bool fClusterStart = false;
|
---|
647 |
|
---|
648 | /* No blocks written yet? Start a new cluster. */
|
---|
649 | if (Cluster.cBlocks == 0)
|
---|
650 | fClusterStart = true;
|
---|
651 |
|
---|
652 | /* Did we reach the maximum a cluster can hold? Use a new cluster then. */
|
---|
653 | if (mapAbsPTSMs - Cluster.tcAbsStartMs > VBOX_WEBM_CLUSTER_MAX_LEN_MS)
|
---|
654 | fClusterStart = true;
|
---|
655 |
|
---|
656 | /* If the block map indicates that a cluster is needed for this timecode, create one. */
|
---|
657 | if (mapBlocks.fClusterNeeded)
|
---|
658 | fClusterStart = true;
|
---|
659 |
|
---|
660 | if ( fClusterStart
|
---|
661 | && !mapBlocks.fClusterStarted)
|
---|
662 | {
|
---|
663 | if (Cluster.fOpen) /* Close current cluster first. */
|
---|
664 | {
|
---|
665 | /* Make sure that the current cluster contained some data. */
|
---|
666 | Assert(Cluster.offStart);
|
---|
667 | Assert(Cluster.cBlocks);
|
---|
668 |
|
---|
669 | subEnd(MkvElem_Cluster);
|
---|
670 | Cluster.fOpen = false;
|
---|
671 | }
|
---|
672 |
|
---|
673 | Cluster.fOpen = true;
|
---|
674 | Cluster.uID = CurSeg.cClusters;
|
---|
675 | Cluster.tcAbsStartMs = mapAbsPTSMs;
|
---|
676 | Cluster.offStart = RTFileTell(getFile());
|
---|
677 | Cluster.cBlocks = 0;
|
---|
678 |
|
---|
679 | Log2Func(("[C%RU64] Start @ %RU64ms (map TC is %RU64) / %RU64 bytes\n",
|
---|
680 | Cluster.uID, Cluster.tcAbsStartMs, mapAbsPTSMs, Cluster.offStart));
|
---|
681 |
|
---|
682 | /* Insert cue points for all tracks if a new cluster has been started. */
|
---|
683 | WebMCuePoint *pCuePoint = new WebMCuePoint(Cluster.tcAbsStartMs);
|
---|
684 |
|
---|
685 | WebMTracks::iterator itTrack = CurSeg.mapTracks.begin();
|
---|
686 | while (itTrack != CurSeg.mapTracks.end())
|
---|
687 | {
|
---|
688 | pCuePoint->Pos[itTrack->first] = new WebMCueTrackPosEntry(Cluster.offStart);
|
---|
689 | ++itTrack;
|
---|
690 | }
|
---|
691 |
|
---|
692 | CurSeg.lstCuePoints.push_back(pCuePoint);
|
---|
693 |
|
---|
694 | subStart(MkvElem_Cluster)
|
---|
695 | .serializeUnsignedInteger(MkvElem_Timecode, Cluster.tcAbsStartMs);
|
---|
696 |
|
---|
697 | CurSeg.cClusters++;
|
---|
698 |
|
---|
699 | mapBlocks.fClusterStarted = true;
|
---|
700 | }
|
---|
701 |
|
---|
702 | /* Iterate through all blocks related to the current timecode. */
|
---|
703 | while (!mapBlocks.Queue.empty())
|
---|
704 | {
|
---|
705 | WebMSimpleBlock *pBlock = mapBlocks.Queue.front();
|
---|
706 | AssertPtr(pBlock);
|
---|
707 |
|
---|
708 | WebMTrack *pTrack = pBlock->pTrack;
|
---|
709 | AssertPtr(pTrack);
|
---|
710 |
|
---|
711 | /* Calculate the block's relative time code to the current cluster's starting time code. */
|
---|
712 | Assert(pBlock->Data.tcAbsPTSMs >= Cluster.tcAbsStartMs);
|
---|
713 | pBlock->Data.tcRelToClusterMs = pBlock->Data.tcAbsPTSMs - Cluster.tcAbsStartMs;
|
---|
714 |
|
---|
715 | int rc2 = writeSimpleBlockEBML(pTrack, pBlock);
|
---|
716 | AssertRC(rc2);
|
---|
717 |
|
---|
718 | Cluster.cBlocks++;
|
---|
719 |
|
---|
720 | pTrack->cTotalBlocks++;
|
---|
721 | pTrack->tcAbsLastWrittenMs = pBlock->Data.tcAbsPTSMs;
|
---|
722 |
|
---|
723 | if (CurSeg.tcAbsLastWrittenMs < pTrack->tcAbsLastWrittenMs)
|
---|
724 | CurSeg.tcAbsLastWrittenMs = pTrack->tcAbsLastWrittenMs;
|
---|
725 |
|
---|
726 | /* Save a cue point if this is a keyframe (if no new cluster has been started,
|
---|
727 | * as this implies that a cue point already is present. */
|
---|
728 | if ( !fClusterStart
|
---|
729 | && (pBlock->Data.fFlags & VBOX_WEBM_BLOCK_FLAG_KEY_FRAME))
|
---|
730 | {
|
---|
731 | /* Insert cue points for all tracks if a new cluster has been started. */
|
---|
732 | WebMCuePoint *pCuePoint = new WebMCuePoint(CurSeg.tcAbsLastWrittenMs);
|
---|
733 |
|
---|
734 | WebMTracks::iterator itTrack = CurSeg.mapTracks.begin();
|
---|
735 | while (itTrack != CurSeg.mapTracks.end())
|
---|
736 | {
|
---|
737 | pCuePoint->Pos[itTrack->first] = new WebMCueTrackPosEntry(Cluster.offStart);
|
---|
738 | ++itTrack;
|
---|
739 | }
|
---|
740 |
|
---|
741 | CurSeg.lstCuePoints.push_back(pCuePoint);
|
---|
742 | }
|
---|
743 |
|
---|
744 | delete pBlock;
|
---|
745 | pBlock = NULL;
|
---|
746 |
|
---|
747 | mapBlocks.Queue.pop();
|
---|
748 | }
|
---|
749 |
|
---|
750 | Assert(mapBlocks.Queue.empty());
|
---|
751 |
|
---|
752 | CurSeg.queueBlocks.Map.erase(it);
|
---|
753 |
|
---|
754 | it = CurSeg.queueBlocks.Map.begin();
|
---|
755 | }
|
---|
756 |
|
---|
757 | Assert(CurSeg.queueBlocks.Map.empty());
|
---|
758 |
|
---|
759 | pQueue->tsLastProcessedMs = RTTimeMilliTS();
|
---|
760 |
|
---|
761 | return VINF_SUCCESS;
|
---|
762 | }
|
---|
763 |
|
---|
764 | /**
|
---|
765 | * Writes the WebM footer.
|
---|
766 | *
|
---|
767 | * @returns IPRT status code.
|
---|
768 | */
|
---|
769 | int WebMWriter::writeFooter(void)
|
---|
770 | {
|
---|
771 | AssertReturn(isOpen(), VERR_WRONG_ORDER);
|
---|
772 |
|
---|
773 | if (m_fInTracksSection)
|
---|
774 | {
|
---|
775 | subEnd(MkvElem_Tracks);
|
---|
776 | m_fInTracksSection = false;
|
---|
777 | }
|
---|
778 |
|
---|
779 | if (CurSeg.CurCluster.fOpen)
|
---|
780 | {
|
---|
781 | subEnd(MkvElem_Cluster);
|
---|
782 | CurSeg.CurCluster.fOpen = false;
|
---|
783 | }
|
---|
784 |
|
---|
785 | /*
|
---|
786 | * Write Cues element.
|
---|
787 | */
|
---|
788 | CurSeg.offCues = RTFileTell(getFile());
|
---|
789 | LogFunc(("Cues @ %RU64\n", CurSeg.offCues));
|
---|
790 |
|
---|
791 | subStart(MkvElem_Cues);
|
---|
792 |
|
---|
793 | WebMCuePointList::iterator itCuePoint = CurSeg.lstCuePoints.begin();
|
---|
794 | while (itCuePoint != CurSeg.lstCuePoints.end())
|
---|
795 | {
|
---|
796 | WebMCuePoint *pCuePoint = (*itCuePoint);
|
---|
797 | AssertPtr(pCuePoint);
|
---|
798 |
|
---|
799 | LogFunc(("CuePoint @ %RU64: %zu tracks, tcAbs=%RU64)\n",
|
---|
800 | RTFileTell(getFile()), pCuePoint->Pos.size(), pCuePoint->tcAbs));
|
---|
801 |
|
---|
802 | subStart(MkvElem_CuePoint)
|
---|
803 | .serializeUnsignedInteger(MkvElem_CueTime, pCuePoint->tcAbs);
|
---|
804 |
|
---|
805 | WebMCueTrackPosMap::iterator itTrackPos = pCuePoint->Pos.begin();
|
---|
806 | while (itTrackPos != pCuePoint->Pos.end())
|
---|
807 | {
|
---|
808 | WebMCueTrackPosEntry *pTrackPos = itTrackPos->second;
|
---|
809 | AssertPtr(pTrackPos);
|
---|
810 |
|
---|
811 | LogFunc(("TrackPos (track #%RU32) @ %RU64, offCluster=%RU64)\n",
|
---|
812 | itTrackPos->first, RTFileTell(getFile()), pTrackPos->offCluster));
|
---|
813 |
|
---|
814 | subStart(MkvElem_CueTrackPositions)
|
---|
815 | .serializeUnsignedInteger(MkvElem_CueTrack, itTrackPos->first)
|
---|
816 | .serializeUnsignedInteger(MkvElem_CueClusterPosition, pTrackPos->offCluster - CurSeg.offStart, 8)
|
---|
817 | .subEnd(MkvElem_CueTrackPositions);
|
---|
818 |
|
---|
819 | ++itTrackPos;
|
---|
820 | }
|
---|
821 |
|
---|
822 | subEnd(MkvElem_CuePoint);
|
---|
823 |
|
---|
824 | ++itCuePoint;
|
---|
825 | }
|
---|
826 |
|
---|
827 | subEnd(MkvElem_Cues);
|
---|
828 | subEnd(MkvElem_Segment);
|
---|
829 |
|
---|
830 | /*
|
---|
831 | * Re-Update seek header with final information.
|
---|
832 | */
|
---|
833 |
|
---|
834 | writeSeekHeader();
|
---|
835 |
|
---|
836 | return RTFileSeek(getFile(), 0, RTFILE_SEEK_END, NULL);
|
---|
837 | }
|
---|
838 |
|
---|
839 | /**
|
---|
840 | * Writes the segment's seek header.
|
---|
841 | */
|
---|
842 | void WebMWriter::writeSeekHeader(void)
|
---|
843 | {
|
---|
844 | if (CurSeg.offSeekInfo)
|
---|
845 | RTFileSeek(getFile(), CurSeg.offSeekInfo, RTFILE_SEEK_BEGIN, NULL);
|
---|
846 | else
|
---|
847 | CurSeg.offSeekInfo = RTFileTell(getFile());
|
---|
848 |
|
---|
849 | LogFunc(("Seek Headeder @ %RU64\n", CurSeg.offSeekInfo));
|
---|
850 |
|
---|
851 | subStart(MkvElem_SeekHead);
|
---|
852 |
|
---|
853 | subStart(MkvElem_Seek)
|
---|
854 | .serializeUnsignedInteger(MkvElem_SeekID, MkvElem_Tracks)
|
---|
855 | .serializeUnsignedInteger(MkvElem_SeekPosition, CurSeg.offTracks - CurSeg.offStart, 8)
|
---|
856 | .subEnd(MkvElem_Seek);
|
---|
857 |
|
---|
858 | if (CurSeg.offCues)
|
---|
859 | LogFunc(("Updating Cues @ %RU64\n", CurSeg.offCues));
|
---|
860 |
|
---|
861 | subStart(MkvElem_Seek)
|
---|
862 | .serializeUnsignedInteger(MkvElem_SeekID, MkvElem_Cues)
|
---|
863 | .serializeUnsignedInteger(MkvElem_SeekPosition, CurSeg.offCues - CurSeg.offStart, 8)
|
---|
864 | .subEnd(MkvElem_Seek);
|
---|
865 |
|
---|
866 | subStart(MkvElem_Seek)
|
---|
867 | .serializeUnsignedInteger(MkvElem_SeekID, MkvElem_Info)
|
---|
868 | .serializeUnsignedInteger(MkvElem_SeekPosition, CurSeg.offInfo - CurSeg.offStart, 8)
|
---|
869 | .subEnd(MkvElem_Seek);
|
---|
870 |
|
---|
871 | subEnd(MkvElem_SeekHead);
|
---|
872 |
|
---|
873 | /*
|
---|
874 | * Write the segment's info element.
|
---|
875 | */
|
---|
876 |
|
---|
877 | /* Save offset of the segment's info element. */
|
---|
878 | CurSeg.offInfo = RTFileTell(getFile());
|
---|
879 |
|
---|
880 | LogFunc(("Info @ %RU64\n", CurSeg.offInfo));
|
---|
881 |
|
---|
882 | char szMux[64];
|
---|
883 | RTStrPrintf(szMux, sizeof(szMux),
|
---|
884 | #ifdef VBOX_WITH_LIBVPX
|
---|
885 | "vpxenc%s", vpx_codec_version_str());
|
---|
886 | #else
|
---|
887 | "unknown");
|
---|
888 | #endif
|
---|
889 | char szApp[64];
|
---|
890 | RTStrPrintf(szApp, sizeof(szApp), VBOX_PRODUCT " %sr%u", VBOX_VERSION_STRING, RTBldCfgRevision());
|
---|
891 |
|
---|
892 | const WebMTimecodeAbs tcAbsDurationMs = CurSeg.tcAbsLastWrittenMs - CurSeg.tcAbsStartMs;
|
---|
893 |
|
---|
894 | if (!CurSeg.lstCuePoints.empty())
|
---|
895 | {
|
---|
896 | LogFunc(("tcAbsDurationMs=%RU64\n", tcAbsDurationMs));
|
---|
897 | AssertMsg(tcAbsDurationMs, ("Segment seems to be empty (duration is 0)\n"));
|
---|
898 | }
|
---|
899 |
|
---|
900 | subStart(MkvElem_Info)
|
---|
901 | .serializeUnsignedInteger(MkvElem_TimecodeScale, CurSeg.uTimecodeScaleFactor)
|
---|
902 | .serializeFloat(MkvElem_Segment_Duration, tcAbsDurationMs)
|
---|
903 | .serializeString(MkvElem_MuxingApp, szMux)
|
---|
904 | .serializeString(MkvElem_WritingApp, szApp)
|
---|
905 | .subEnd(MkvElem_Info);
|
---|
906 | }
|
---|
907 |
|
---|