VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/VideoRec.cpp@ 65212

Last change on this file since 65212 was 65212, checked in by vboxsync, 8 years ago

VideoRec: Update.

  • 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/EncodeAndWrite.cpp58652,​70973
    /branches/VBox-3.2/src/VBox/Frontends/VBoxHeadless/VideoCapture/EncodeAndWrite.cpp66309,​66318
    /branches/VBox-4.0/src/VBox/Frontends/VBoxHeadless/VideoCapture/EncodeAndWrite.cpp70873
    /branches/VBox-4.1/src/VBox/Frontends/VBoxHeadless/VideoCapture/EncodeAndWrite.cpp74233
    /branches/VBox-4.2/src/VBox/Main/src-client/VideoRec.cpp91503-91504,​91506-91508,​91510,​91514-91515,​91521
    /branches/VBox-4.3/src/VBox/Main/src-client/VideoRec.cpp91223
    /branches/VBox-4.3/trunk/src/VBox/Main/src-client/VideoRec.cpp91223
    /branches/dsen/gui/src/VBox/Frontends/VBoxHeadless/VideoCapture/EncodeAndWrite.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/EncodeAndWrite.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/EncodeAndWrite.cpp79645-79692
File size: 33.3 KB
Line 
1/* $Id: VideoRec.cpp 65212 2017-01-09 15:57:02Z vboxsync $ */
2/** @file
3 * Encodes the screen content in VPX format.
4 */
5
6/*
7 * Copyright (C) 2012-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#define LOG_GROUP LOG_GROUP_MAIN
19
20#include <vector>
21
22#include <VBox/log.h>
23#include <iprt/asm.h>
24#include <iprt/assert.h>
25#include <iprt/semaphore.h>
26#include <iprt/thread.h>
27#include <iprt/time.h>
28
29#include <VBox/com/VirtualBox.h>
30#include <VBox/com/com.h>
31#include <VBox/com/string.h>
32
33#include "EbmlWriter.h"
34#include "VideoRec.h"
35
36#define VPX_CODEC_DISABLE_COMPAT 1
37#include <vpx/vp8cx.h>
38#include <vpx/vpx_image.h>
39
40/** Default VPX codec to use. */
41#define DEFAULTCODEC (vpx_codec_vp8_cx())
42
43static int videoRecEncodeAndWrite(PVIDEORECSTREAM pStrm);
44static int videoRecRGBToYUV(PVIDEORECSTREAM pStrm);
45
46/**
47 * Enumeration for a video recording state.
48 */
49enum
50{
51 /** Not initialized. */
52 VIDREC_UNINITIALIZED = 0,
53 /** Initialized, idle. */
54 VIDREC_IDLE = 1,
55 /** Currently in VideoRecCopyToIntBuf(), delay termination. */
56 VIDREC_COPYING = 2,
57 /** Signal that we are terminating. */
58 VIDREC_TERMINATING = 3
59};
60
61/* Must be always accessible and therefore cannot be part of VIDEORECCONTEXT */
62static uint32_t g_enmState = VIDREC_UNINITIALIZED;
63
64/**
65 * Structure for keeping specific video recording codec data.
66 */
67typedef struct VIDEORECCODEC
68{
69 union
70 {
71 struct
72 {
73 /** VPX codec context. */
74 vpx_codec_ctx_t CodecCtx;
75 /** VPX codec configuration. */
76 vpx_codec_enc_cfg_t Config;
77 /** VPX image context. */
78 vpx_image_t RawImage;
79 } VPX;
80 };
81} VIDEORECCODEC, *PVIDEORECCODEC;
82
83/**
84 * Strucutre for maintaining a video recording stream.
85 */
86typedef struct VIDEORECSTREAM
87{
88 /** Container context. */
89 WebMWriter *pEBML;
90 /** Codec data. */
91 VIDEORECCODEC Codec;
92 /** Target X resolution (in pixels). */
93 uint32_t uTargetWidth;
94 /** Target Y resolution (in pixels). */
95 uint32_t uTargetHeight;
96 /** X resolution of the last encoded frame. */
97 uint32_t uLastSourceWidth;
98 /** Y resolution of the last encoded frame. */
99 uint32_t uLastSourceHeight;
100 /** Current frame number. */
101 uint64_t cFrame;
102 /** RGB buffer containing the most recent frame of the framebuffer. */
103 uint8_t *pu8RgbBuf;
104 /** YUV buffer the encode function fetches the frame from. */
105 uint8_t *pu8YuvBuf;
106 /** Whether video recording is enabled or not. */
107 bool fEnabled;
108 /** Whether the RGB buffer is filled or not. */
109 bool fRgbFilled;
110 /** Pixel format of the current frame. */
111 uint32_t u32PixelFormat;
112 /** Minimal delay between two frames. */
113 uint32_t uDelay;
114 /** Time stamp of the last frame we encoded. */
115 uint64_t u64LastTimeStamp;
116 /** Time stamp of the current frame. */
117 uint64_t u64TimeStamp;
118 /** Encoder deadline. */
119 unsigned int uEncoderDeadline;
120} VIDEORECSTREAM, *PVIDEORECSTREAM;
121
122/** Vector of video recording streams. */
123typedef std::vector <PVIDEORECSTREAM> VideoRecStreams;
124
125/**
126 * Structure for keeping a video recording context.
127 */
128typedef struct VIDEORECCONTEXT
129{
130 /** Semaphore to signal the encoding worker thread. */
131 RTSEMEVENT WaitEvent;
132 /** Semaphore required during termination. */
133 RTSEMEVENT TermEvent;
134 /** Whether video recording is enabled or not. */
135 bool fEnabled;
136 /** Worker thread. */
137 RTTHREAD Thread;
138 /** Maximal time stamp. */
139 uint64_t u64MaxTimeStamp;
140 /** Maximal file size in MB. */
141 uint32_t uMaxFileSize;
142 /** Vector of current video recording stream contexts. */
143 VideoRecStreams vecStreams;
144} VIDEORECCONTEXT, *PVIDEORECCONTEXT;
145
146
147/**
148 * Iterator class for running through a BGRA32 image buffer and converting
149 * it to RGB.
150 */
151class ColorConvBGRA32Iter
152{
153private:
154 enum { PIX_SIZE = 4 };
155public:
156 ColorConvBGRA32Iter(unsigned aWidth, unsigned aHeight, uint8_t *aBuf)
157 {
158 LogFlow(("width = %d height=%d aBuf=%lx\n", aWidth, aHeight, aBuf));
159 mPos = 0;
160 mSize = aWidth * aHeight * PIX_SIZE;
161 mBuf = aBuf;
162 }
163 /**
164 * Convert the next pixel to RGB.
165 * @returns true on success, false if we have reached the end of the buffer
166 * @param aRed where to store the red value
167 * @param aGreen where to store the green value
168 * @param aBlue where to store the blue value
169 */
170 bool getRGB(unsigned *aRed, unsigned *aGreen, unsigned *aBlue)
171 {
172 bool rc = false;
173 if (mPos + PIX_SIZE <= mSize)
174 {
175 *aRed = mBuf[mPos + 2];
176 *aGreen = mBuf[mPos + 1];
177 *aBlue = mBuf[mPos ];
178 mPos += PIX_SIZE;
179 rc = true;
180 }
181 return rc;
182 }
183
184 /**
185 * Skip forward by a certain number of pixels
186 * @param aPixels how many pixels to skip
187 */
188 void skip(unsigned aPixels)
189 {
190 mPos += PIX_SIZE * aPixels;
191 }
192private:
193 /** Size of the picture buffer */
194 unsigned mSize;
195 /** Current position in the picture buffer */
196 unsigned mPos;
197 /** Address of the picture buffer */
198 uint8_t *mBuf;
199};
200
201/**
202 * Iterator class for running through an BGR24 image buffer and converting
203 * it to RGB.
204 */
205class ColorConvBGR24Iter
206{
207private:
208 enum { PIX_SIZE = 3 };
209public:
210 ColorConvBGR24Iter(unsigned aWidth, unsigned aHeight, uint8_t *aBuf)
211 {
212 mPos = 0;
213 mSize = aWidth * aHeight * PIX_SIZE;
214 mBuf = aBuf;
215 }
216 /**
217 * Convert the next pixel to RGB.
218 * @returns true on success, false if we have reached the end of the buffer
219 * @param aRed where to store the red value
220 * @param aGreen where to store the green value
221 * @param aBlue where to store the blue value
222 */
223 bool getRGB(unsigned *aRed, unsigned *aGreen, unsigned *aBlue)
224 {
225 bool rc = false;
226 if (mPos + PIX_SIZE <= mSize)
227 {
228 *aRed = mBuf[mPos + 2];
229 *aGreen = mBuf[mPos + 1];
230 *aBlue = mBuf[mPos ];
231 mPos += PIX_SIZE;
232 rc = true;
233 }
234 return rc;
235 }
236
237 /**
238 * Skip forward by a certain number of pixels
239 * @param aPixels how many pixels to skip
240 */
241 void skip(unsigned aPixels)
242 {
243 mPos += PIX_SIZE * aPixels;
244 }
245private:
246 /** Size of the picture buffer */
247 unsigned mSize;
248 /** Current position in the picture buffer */
249 unsigned mPos;
250 /** Address of the picture buffer */
251 uint8_t *mBuf;
252};
253
254/**
255 * Iterator class for running through an BGR565 image buffer and converting
256 * it to RGB.
257 */
258class ColorConvBGR565Iter
259{
260private:
261 enum { PIX_SIZE = 2 };
262public:
263 ColorConvBGR565Iter(unsigned aWidth, unsigned aHeight, uint8_t *aBuf)
264 {
265 mPos = 0;
266 mSize = aWidth * aHeight * PIX_SIZE;
267 mBuf = aBuf;
268 }
269 /**
270 * Convert the next pixel to RGB.
271 * @returns true on success, false if we have reached the end of the buffer
272 * @param aRed where to store the red value
273 * @param aGreen where to store the green value
274 * @param aBlue where to store the blue value
275 */
276 bool getRGB(unsigned *aRed, unsigned *aGreen, unsigned *aBlue)
277 {
278 bool rc = false;
279 if (mPos + PIX_SIZE <= mSize)
280 {
281 unsigned uFull = (((unsigned) mBuf[mPos + 1]) << 8)
282 | ((unsigned) mBuf[mPos]);
283 *aRed = (uFull >> 8) & ~7;
284 *aGreen = (uFull >> 3) & ~3 & 0xff;
285 *aBlue = (uFull << 3) & ~7 & 0xff;
286 mPos += PIX_SIZE;
287 rc = true;
288 }
289 return rc;
290 }
291
292 /**
293 * Skip forward by a certain number of pixels
294 * @param aPixels how many pixels to skip
295 */
296 void skip(unsigned aPixels)
297 {
298 mPos += PIX_SIZE * aPixels;
299 }
300private:
301 /** Size of the picture buffer */
302 unsigned mSize;
303 /** Current position in the picture buffer */
304 unsigned mPos;
305 /** Address of the picture buffer */
306 uint8_t *mBuf;
307};
308
309/**
310 * Convert an image to YUV420p format
311 * @returns true on success, false on failure
312 * @param aWidth width of image
313 * @param aHeight height of image
314 * @param aDestBuf an allocated memory buffer large enough to hold the
315 * destination image (i.e. width * height * 12bits)
316 * @param aSrcBuf the source image as an array of bytes
317 */
318template <class T>
319inline bool colorConvWriteYUV420p(unsigned aWidth, unsigned aHeight, uint8_t *aDestBuf, uint8_t *aSrcBuf)
320{
321 AssertReturn(!(aWidth & 1), false);
322 AssertReturn(!(aHeight & 1), false);
323 bool fRc = true;
324 T iter1(aWidth, aHeight, aSrcBuf);
325 T iter2 = iter1;
326 iter2.skip(aWidth);
327 unsigned cPixels = aWidth * aHeight;
328 unsigned offY = 0;
329 unsigned offU = cPixels;
330 unsigned offV = cPixels + cPixels / 4;
331 unsigned const cyHalf = aHeight / 2;
332 unsigned const cxHalf = aWidth / 2;
333 for (unsigned i = 0; i < cyHalf && fRc; ++i)
334 {
335 for (unsigned j = 0; j < cxHalf; ++j)
336 {
337 unsigned red, green, blue;
338 fRc = iter1.getRGB(&red, &green, &blue);
339 AssertReturn(fRc, false);
340 aDestBuf[offY] = ((66 * red + 129 * green + 25 * blue + 128) >> 8) + 16;
341 unsigned u = (((-38 * red - 74 * green + 112 * blue + 128) >> 8) + 128) / 4;
342 unsigned v = (((112 * red - 94 * green - 18 * blue + 128) >> 8) + 128) / 4;
343
344 fRc = iter1.getRGB(&red, &green, &blue);
345 AssertReturn(fRc, false);
346 aDestBuf[offY + 1] = ((66 * red + 129 * green + 25 * blue + 128) >> 8) + 16;
347 u += (((-38 * red - 74 * green + 112 * blue + 128) >> 8) + 128) / 4;
348 v += (((112 * red - 94 * green - 18 * blue + 128) >> 8) + 128) / 4;
349
350 fRc = iter2.getRGB(&red, &green, &blue);
351 AssertReturn(fRc, false);
352 aDestBuf[offY + aWidth] = ((66 * red + 129 * green + 25 * blue + 128) >> 8) + 16;
353 u += (((-38 * red - 74 * green + 112 * blue + 128) >> 8) + 128) / 4;
354 v += (((112 * red - 94 * green - 18 * blue + 128) >> 8) + 128) / 4;
355
356 fRc = iter2.getRGB(&red, &green, &blue);
357 AssertReturn(fRc, false);
358 aDestBuf[offY + aWidth + 1] = ((66 * red + 129 * green + 25 * blue + 128) >> 8) + 16;
359 u += (((-38 * red - 74 * green + 112 * blue + 128) >> 8) + 128) / 4;
360 v += (((112 * red - 94 * green - 18 * blue + 128) >> 8) + 128) / 4;
361
362 aDestBuf[offU] = u;
363 aDestBuf[offV] = v;
364 offY += 2;
365 ++offU;
366 ++offV;
367 }
368
369 iter1.skip(aWidth);
370 iter2.skip(aWidth);
371 offY += aWidth;
372 }
373
374 return true;
375}
376
377/**
378 * Convert an image to RGB24 format
379 * @returns true on success, false on failure
380 * @param aWidth width of image
381 * @param aHeight height of image
382 * @param aDestBuf an allocated memory buffer large enough to hold the
383 * destination image (i.e. width * height * 12bits)
384 * @param aSrcBuf the source image as an array of bytes
385 */
386template <class T>
387inline bool colorConvWriteRGB24(unsigned aWidth, unsigned aHeight,
388 uint8_t *aDestBuf, uint8_t *aSrcBuf)
389{
390 enum { PIX_SIZE = 3 };
391 bool rc = true;
392 AssertReturn(0 == (aWidth & 1), false);
393 AssertReturn(0 == (aHeight & 1), false);
394 T iter(aWidth, aHeight, aSrcBuf);
395 unsigned cPixels = aWidth * aHeight;
396 for (unsigned i = 0; i < cPixels && rc; ++i)
397 {
398 unsigned red, green, blue;
399 rc = iter.getRGB(&red, &green, &blue);
400 if (rc)
401 {
402 aDestBuf[i * PIX_SIZE ] = red;
403 aDestBuf[i * PIX_SIZE + 1] = green;
404 aDestBuf[i * PIX_SIZE + 2] = blue;
405 }
406 }
407 return rc;
408}
409
410/**
411 * Worker thread for all streams of a video recording context.
412 *
413 * Does RGB/YUV conversion and encoding.
414 */
415static DECLCALLBACK(int) videoRecThread(RTTHREAD hThreadSelf, void *pvUser)
416{
417 RT_NOREF(hThreadSelf);
418 PVIDEORECCONTEXT pCtx = (PVIDEORECCONTEXT)pvUser;
419 for (;;)
420 {
421 int rc = RTSemEventWait(pCtx->WaitEvent, RT_INDEFINITE_WAIT);
422 AssertRCBreak(rc);
423
424 if (ASMAtomicReadU32(&g_enmState) == VIDREC_TERMINATING)
425 break;
426
427 for (VideoRecStreams::iterator it = pCtx->vecStreams.begin(); it != pCtx->vecStreams.end(); it++)
428 {
429 PVIDEORECSTREAM pStream = (*it);
430
431 if ( pStream->fEnabled
432 && ASMAtomicReadBool(&pStream->fRgbFilled))
433 {
434 rc = videoRecRGBToYUV(pStream);
435
436 ASMAtomicWriteBool(&pStream->fRgbFilled, false);
437
438 if (RT_SUCCESS(rc))
439 rc = videoRecEncodeAndWrite(pStream);
440
441 if (RT_FAILURE(rc))
442 {
443 static unsigned cErrors = 100;
444 if (cErrors > 0)
445 {
446 LogRel(("Error %Rrc encoding / writing video frame\n", rc));
447 cErrors--;
448 }
449 }
450 }
451 }
452 }
453
454 return VINF_SUCCESS;
455}
456
457/**
458 * Creates a video recording context.
459 *
460 * @returns IPRT status code.
461 * @param cScreens Number of screens to create context for.
462 * @param ppCtx Pointer to created video recording context on success.
463 */
464int VideoRecContextCreate(uint32_t cScreens, PVIDEORECCONTEXT *ppCtx)
465{
466 AssertReturn(cScreens, VERR_INVALID_PARAMETER);
467 AssertPtrReturn(ppCtx, VERR_INVALID_POINTER);
468
469 Assert(ASMAtomicReadU32(&g_enmState) == VIDREC_UNINITIALIZED);
470
471 int rc = VINF_SUCCESS;
472
473 PVIDEORECCONTEXT pCtx = (PVIDEORECCONTEXT)RTMemAllocZ(sizeof(VIDEORECCONTEXT));
474 if (!pCtx)
475 return VERR_NO_MEMORY;
476
477 for (uint32_t uScreen = 0; uScreen < cScreens; uScreen++)
478 {
479 PVIDEORECSTREAM pStream = (PVIDEORECSTREAM)RTMemAllocZ(sizeof(VIDEORECSTREAM));
480 if (!pStream)
481 {
482 rc = VERR_NO_MEMORY;
483 break;
484 }
485
486 try
487 {
488 pCtx->vecStreams.push_back(pStream);
489
490 pStream->pEBML = new WebMWriter();
491 }
492 catch (std::bad_alloc)
493 {
494 rc = VERR_NO_MEMORY;
495 break;
496 }
497 }
498
499 if (RT_SUCCESS(rc))
500 {
501 rc = RTSemEventCreate(&pCtx->WaitEvent);
502 AssertRCReturn(rc, rc);
503
504 rc = RTSemEventCreate(&pCtx->TermEvent);
505 AssertRCReturn(rc, rc);
506
507 rc = RTThreadCreate(&pCtx->Thread, videoRecThread, (void*)pCtx, 0,
508 RTTHREADTYPE_MAIN_WORKER, RTTHREADFLAGS_WAITABLE, "VideoRec");
509 AssertRCReturn(rc, rc);
510
511 ASMAtomicWriteU32(&g_enmState, VIDREC_IDLE);
512
513 if (ppCtx)
514 *ppCtx = pCtx;
515 }
516 else
517 {
518 /* Roll back allocations on error. */
519 VideoRecStreams::iterator it = pCtx->vecStreams.begin();
520 while (it != pCtx->vecStreams.end())
521 {
522 PVIDEORECSTREAM pStream = (*it);
523
524 if (pStream->pEBML)
525 delete pStream->pEBML;
526
527 it = pCtx->vecStreams.erase(it);
528
529 RTMemFree(pStream);
530 pStream = NULL;
531 }
532
533 Assert(pCtx->vecStreams.empty());
534 }
535
536 return rc;
537}
538
539/**
540 * Destroys a video recording context.
541 *
542 * @param pCtx Video recording context to destroy.
543 */
544void VideoRecContextDestroy(PVIDEORECCONTEXT pCtx)
545{
546 if (!pCtx)
547 return;
548
549 uint32_t enmState = VIDREC_IDLE;
550
551 for (;;) /** @todo r=andy Remove busy waiting! */
552 {
553 if (ASMAtomicCmpXchgExU32(&g_enmState, VIDREC_TERMINATING, enmState, &enmState))
554 break;
555 if (enmState == VIDREC_UNINITIALIZED)
556 return;
557 }
558
559 if (enmState == VIDREC_COPYING)
560 {
561 int rc = RTSemEventWait(pCtx->TermEvent, RT_INDEFINITE_WAIT);
562 AssertRC(rc);
563 }
564
565 RTSemEventSignal(pCtx->WaitEvent);
566 RTThreadWait(pCtx->Thread, 10 * 1000, NULL);
567 RTSemEventDestroy(pCtx->WaitEvent);
568 RTSemEventDestroy(pCtx->TermEvent);
569
570 VideoRecStreams::iterator it = pCtx->vecStreams.begin();
571 while (it != pCtx->vecStreams.end())
572 {
573 PVIDEORECSTREAM pStream = (*it);
574
575 if (pStream->fEnabled)
576 {
577 AssertPtr(pStream->pEBML);
578 int rc = pStream->pEBML->writeFooter(0);
579 AssertRC(rc);
580
581 pStream->pEBML->close();
582
583 vpx_img_free(&pStream->Codec.VPX.RawImage);
584 vpx_codec_err_t rcv = vpx_codec_destroy(&pStream->Codec.VPX.CodecCtx);
585 Assert(rcv == VPX_CODEC_OK); RT_NOREF(rcv);
586
587 if (pStream->pu8RgbBuf)
588 {
589 RTMemFree(pStream->pu8RgbBuf);
590 pStream->pu8RgbBuf = NULL;
591 }
592 }
593
594 if (pStream->pEBML)
595 {
596 delete pStream->pEBML;
597 pStream->pEBML = NULL;
598 }
599
600 it = pCtx->vecStreams.erase(it);
601
602 RTMemFree(pStream);
603 pStream = NULL;
604 }
605
606 Assert(pCtx->vecStreams.empty());
607 RTMemFree(pCtx);
608
609 ASMAtomicWriteU32(&g_enmState, VIDREC_UNINITIALIZED);
610}
611
612/**
613 * VideoRec utility function to initialize video recording context.
614 *
615 * @returns IPRT status code.
616 * @param pCtx Pointer to video recording context to initialize Framebuffer width.
617 * @param uScreen Screen number.
618 * @param pszFile File to save the recorded data
619 * @param uWidth Width of the target image in the video recoriding file (movie)
620 * @param uHeight Height of the target image in video recording file.
621 * @param uRate Rate.
622 * @param uFps FPS.
623 * @param uMaxTime
624 * @param uMaxFileSize
625 * @param pszOptions
626 */
627int VideoRecStreamInit(PVIDEORECCONTEXT pCtx, uint32_t uScreen, const char *pszFile,
628 uint32_t uWidth, uint32_t uHeight, uint32_t uRate, uint32_t uFps,
629 uint32_t uMaxTime, uint32_t uMaxFileSize, const char *pszOptions)
630{
631 AssertPtrReturn(pCtx, VERR_INVALID_PARAMETER);
632 AssertReturn(uScreen < pCtx->vecStreams.size(), VERR_INVALID_PARAMETER);
633
634 pCtx->u64MaxTimeStamp = (uMaxTime > 0 ? RTTimeProgramMilliTS() + uMaxTime * 1000 : 0);
635 pCtx->uMaxFileSize = uMaxFileSize;
636
637 PVIDEORECSTREAM pStream = pCtx->vecStreams.at(uScreen);
638
639 pStream->uTargetWidth = uWidth;
640 pStream->uTargetHeight = uHeight;
641 pStream->pu8RgbBuf = (uint8_t *)RTMemAllocZ(uWidth * uHeight * 4);
642 AssertReturn(pStream->pu8RgbBuf, VERR_NO_MEMORY);
643 pStream->uEncoderDeadline = VPX_DL_REALTIME;
644
645 /* Play safe: the file must not exist, overwriting is potentially
646 * hazardous as nothing prevents the user from picking a file name of some
647 * other important file, causing unintentional data loss. */
648
649 /** @todo Make mode configurable. */
650#ifdef VBOX_WITH_AUDIO_VIDEOREC
651 WebMWriter::Mode enmMode = WebMWriter::Mode_AudioVideo;
652#else
653 WebMWriter::Mode enmMode = WebMWriter::Mode_Video;
654#endif
655
656 int rc = pStream->pEBML->create(pszFile, RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE, enmMode,
657 WebMWriter::AudioCodec_Opus, WebMWriter::VideoCodec_VP8);
658 if (RT_FAILURE(rc))
659 {
660 LogRel(("Failed to create the video capture output file \"%s\" (%Rrc)\n", pszFile, rc));
661 return rc;
662 }
663
664 vpx_codec_err_t rcv = vpx_codec_enc_config_default(DEFAULTCODEC, &pStream->Codec.VPX.Config, 0);
665 if (rcv != VPX_CODEC_OK)
666 {
667 LogFlow(("Failed to configure codec: %s\n", vpx_codec_err_to_string(rcv)));
668 return VERR_INVALID_PARAMETER;
669 }
670
671 com::Utf8Str options(pszOptions);
672 size_t pos = 0;
673
674 do {
675
676 com::Utf8Str key, value;
677 pos = options.parseKeyValue(key, value, pos);
678
679 if (key == "quality")
680 {
681 if (value == "realtime")
682 {
683 pStream->uEncoderDeadline = VPX_DL_REALTIME;
684 }
685 else if (value == "good")
686 {
687 pStream->uEncoderDeadline = 1000000 / uFps;
688 }
689 else if (value == "best")
690 {
691 pStream->uEncoderDeadline = VPX_DL_BEST_QUALITY;
692 }
693 else
694 {
695 LogRel(("Settings quality deadline to = %s\n", value.c_str()));
696 pStream->uEncoderDeadline = value.toUInt32();
697 }
698 }
699 else LogRel(("Getting unknown option: %s=%s\n", key.c_str(), value.c_str()));
700
701 } while(pos != com::Utf8Str::npos);
702
703 /* target bitrate in kilobits per second */
704 pStream->Codec.VPX.Config.rc_target_bitrate = uRate;
705 /* frame width */
706 pStream->Codec.VPX.Config.g_w = uWidth;
707 /* frame height */
708 pStream->Codec.VPX.Config.g_h = uHeight;
709 /* 1ms per frame */
710 pStream->Codec.VPX.Config.g_timebase.num = 1;
711 pStream->Codec.VPX.Config.g_timebase.den = 1000;
712 /* disable multithreading */
713 pStream->Codec.VPX.Config.g_threads = 0;
714
715 pStream->uDelay = 1000 / uFps;
716
717 struct vpx_rational arg_framerate = { (int)uFps, 1 };
718 rc = pStream->pEBML->writeHeader(&pStream->Codec.VPX.Config, &arg_framerate);
719 AssertRCReturn(rc, rc);
720
721 /* Initialize codec */
722 rcv = vpx_codec_enc_init(&pStream->Codec.VPX.CodecCtx, DEFAULTCODEC, &pStream->Codec.VPX.Config, 0);
723 if (rcv != VPX_CODEC_OK)
724 {
725 LogFlow(("Failed to initialize VP8 encoder %s", vpx_codec_err_to_string(rcv)));
726 return VERR_INVALID_PARAMETER;
727 }
728
729 if (!vpx_img_alloc(&pStream->Codec.VPX.RawImage, VPX_IMG_FMT_I420, uWidth, uHeight, 1))
730 {
731 LogFlow(("Failed to allocate image %dx%d", uWidth, uHeight));
732 return VERR_NO_MEMORY;
733 }
734 pStream->pu8YuvBuf = pStream->Codec.VPX.RawImage.planes[0];
735
736 pCtx->fEnabled = true;
737 pStream->fEnabled = true;
738 return VINF_SUCCESS;
739}
740
741/**
742 * VideoRec utility function to check if recording is enabled.
743 *
744 * @returns true if recording is enabled
745 * @param pCtx Pointer to video recording context.
746 */
747bool VideoRecIsEnabled(PVIDEORECCONTEXT pCtx)
748{
749 RT_NOREF(pCtx);
750 uint32_t enmState = ASMAtomicReadU32(&g_enmState);
751 return ( enmState == VIDREC_IDLE
752 || enmState == VIDREC_COPYING);
753}
754
755/**
756 * VideoRec utility function to check if recording engine is ready to accept a new frame
757 * for the given screen.
758 *
759 * @returns true if recording engine is ready
760 * @param pCtx Pointer to video recording context.
761 * @param uScreen Screen ID.
762 * @param u64TimeStamp Current time stamp.
763 */
764bool VideoRecIsReady(PVIDEORECCONTEXT pCtx, uint32_t uScreen, uint64_t u64TimeStamp)
765{
766 uint32_t enmState = ASMAtomicReadU32(&g_enmState);
767 if (enmState != VIDREC_IDLE)
768 return false;
769
770 PVIDEORECSTREAM pStream = pCtx->vecStreams.at(uScreen);
771 if (!pStream->fEnabled)
772 return false;
773
774 if (u64TimeStamp < pStream->u64LastTimeStamp + pStream->uDelay)
775 return false;
776
777 if (ASMAtomicReadBool(&pStream->fRgbFilled))
778 return false;
779
780 return true;
781}
782
783/**
784 * VideoRec utility function to check if the file size has reached
785 * specified limits (if any).
786 *
787 * @returns true if any limit has been reached.
788 * @param pCtx Pointer to video recording context.
789 * @param uScreen Screen ID.
790 * @param u64TimeStamp Current time stamp.
791 */
792
793bool VideoRecIsFull(PVIDEORECCONTEXT pCtx, uint32_t uScreen, uint64_t u64TimeStamp)
794{
795 PVIDEORECSTREAM pStream = pCtx->vecStreams.at(uScreen);
796 if(!pStream->fEnabled)
797 return false;
798
799 if(pCtx->u64MaxTimeStamp > 0 && u64TimeStamp >= pCtx->u64MaxTimeStamp)
800 return true;
801
802 if (pCtx->uMaxFileSize > 0)
803 {
804 uint64_t sizeInMB = pStream->pEBML->getFileSize() / (1024 * 1024);
805 if(sizeInMB >= pCtx->uMaxFileSize)
806 return true;
807 }
808 /* Check for available free disk space */
809 if (pStream->pEBML->getAvailableSpace() < 0x100000)
810 {
811 LogRel(("Storage has not enough free space available, stopping video capture\n"));
812 return true;
813 }
814
815 return false;
816}
817
818/**
819 * VideoRec utility function to encode the source image and write the encoded
820 * image to target file.
821 *
822 * @returns IPRT status code.
823 * @param pStream Stream to encode and write.
824 */
825static int videoRecEncodeAndWrite(PVIDEORECSTREAM pStream)
826{
827 /* presentation time stamp */
828 vpx_codec_pts_t pts = pStream->u64TimeStamp;
829 vpx_codec_err_t rcv = vpx_codec_encode(&pStream->Codec.VPX.CodecCtx,
830 &pStream->Codec.VPX.RawImage,
831 pts /* time stamp */,
832 pStream->uDelay /* how long to show this frame */,
833 0 /* flags */,
834 pStream->uEncoderDeadline /* quality setting */);
835 if (rcv != VPX_CODEC_OK)
836 {
837 LogFlow(("Failed to encode:%s\n", vpx_codec_err_to_string(rcv)));
838 return VERR_GENERAL_FAILURE;
839 }
840
841 vpx_codec_iter_t iter = NULL;
842 int rc = VERR_NO_DATA;
843 for (;;)
844 {
845 const vpx_codec_cx_pkt_t *pkt = vpx_codec_get_cx_data(&pStream->Codec.VPX.CodecCtx, &iter);
846 if (!pkt)
847 break;
848 switch (pkt->kind)
849 {
850 case VPX_CODEC_CX_FRAME_PKT:
851 rc = pStream->pEBML->writeBlock(&pStream->Codec.VPX.Config, pkt);
852 break;
853 default:
854 LogFlow(("Unexpected CODEC Packet.\n"));
855 break;
856 }
857 }
858
859 pStream->cFrame++;
860 return rc;
861}
862
863/**
864 * VideoRec utility function to convert RGB to YUV.
865 *
866 * @returns IPRT status code.
867 * @param pStrm Strm.
868 */
869static int videoRecRGBToYUV(PVIDEORECSTREAM pStrm)
870{
871 switch (pStrm->u32PixelFormat)
872 {
873 case VPX_IMG_FMT_RGB32:
874 LogFlow(("32 bit\n"));
875 if (!colorConvWriteYUV420p<ColorConvBGRA32Iter>(pStrm->uTargetWidth,
876 pStrm->uTargetHeight,
877 pStrm->pu8YuvBuf,
878 pStrm->pu8RgbBuf))
879 return VERR_GENERAL_FAILURE;
880 break;
881 case VPX_IMG_FMT_RGB24:
882 LogFlow(("24 bit\n"));
883 if (!colorConvWriteYUV420p<ColorConvBGR24Iter>(pStrm->uTargetWidth,
884 pStrm->uTargetHeight,
885 pStrm->pu8YuvBuf,
886 pStrm->pu8RgbBuf))
887 return VERR_GENERAL_FAILURE;
888 break;
889 case VPX_IMG_FMT_RGB565:
890 LogFlow(("565 bit\n"));
891 if (!colorConvWriteYUV420p<ColorConvBGR565Iter>(pStrm->uTargetWidth,
892 pStrm->uTargetHeight,
893 pStrm->pu8YuvBuf,
894 pStrm->pu8RgbBuf))
895 return VERR_GENERAL_FAILURE;
896 break;
897 default:
898 return VERR_GENERAL_FAILURE;
899 }
900 return VINF_SUCCESS;
901}
902
903/**
904 * VideoRec utility function to copy a source image (FrameBuf) to the intermediate
905 * RGB buffer. This function is executed only once per time.
906 *
907 * @thread EMT
908 *
909 * @returns IPRT status code.
910 * @param pCtx Pointer to the video recording context.
911 * @param uScreen Screen number.
912 * @param x Starting x coordinate of the source buffer (Framebuffer).
913 * @param y Starting y coordinate of the source buffer (Framebuffer).
914 * @param uPixelFormat Pixel Format.
915 * @param uBitsPerPixel Bits Per Pixel
916 * @param uBytesPerLine Bytes per source scanlineName.
917 * @param uSourceWidth Width of the source image (framebuffer).
918 * @param uSourceHeight Height of the source image (framebuffer).
919 * @param pu8BufAddr Pointer to source image(framebuffer).
920 * @param u64TimeStamp Time stamp (milliseconds).
921 */
922int VideoRecCopyToIntBuf(PVIDEORECCONTEXT pCtx, uint32_t uScreen, uint32_t x, uint32_t y,
923 uint32_t uPixelFormat, uint32_t uBitsPerPixel, uint32_t uBytesPerLine,
924 uint32_t uSourceWidth, uint32_t uSourceHeight, uint8_t *pu8BufAddr,
925 uint64_t u64TimeStamp)
926{
927 /* Do not execute during termination and guard against termination */
928 if (!ASMAtomicCmpXchgU32(&g_enmState, VIDREC_COPYING, VIDREC_IDLE))
929 return VINF_TRY_AGAIN;
930
931 int rc = VINF_SUCCESS;
932 do
933 {
934 AssertPtrBreakStmt(pu8BufAddr, rc = VERR_INVALID_PARAMETER);
935 AssertBreakStmt(uSourceWidth, rc = VERR_INVALID_PARAMETER);
936 AssertBreakStmt(uSourceHeight, rc = VERR_INVALID_PARAMETER);
937 AssertBreakStmt(uScreen < pCtx->vecStreams.size(), rc = VERR_INVALID_PARAMETER);
938
939 PVIDEORECSTREAM pStream = pCtx->vecStreams.at(uScreen);
940 if (!pStream->fEnabled)
941 {
942 rc = VINF_TRY_AGAIN; /* not (yet) enabled */
943 break;
944 }
945 if (u64TimeStamp < pStream->u64LastTimeStamp + pStream->uDelay)
946 {
947 rc = VINF_TRY_AGAIN; /* respect maximum frames per second */
948 break;
949 }
950 if (ASMAtomicReadBool(&pStream->fRgbFilled))
951 {
952 rc = VERR_TRY_AGAIN; /* previous frame not yet encoded */
953 break;
954 }
955
956 pStream->u64LastTimeStamp = u64TimeStamp;
957
958 int xDiff = ((int)pStream->uTargetWidth - (int)uSourceWidth) / 2;
959 uint32_t w = uSourceWidth;
960 if ((int)w + xDiff + (int)x <= 0) /* nothing visible */
961 {
962 rc = VERR_INVALID_PARAMETER;
963 break;
964 }
965
966 uint32_t destX;
967 if ((int)x < -xDiff)
968 {
969 w += xDiff + x;
970 x = -xDiff;
971 destX = 0;
972 }
973 else
974 destX = x + xDiff;
975
976 uint32_t h = uSourceHeight;
977 int yDiff = ((int)pStream->uTargetHeight - (int)uSourceHeight) / 2;
978 if ((int)h + yDiff + (int)y <= 0) /* nothing visible */
979 {
980 rc = VERR_INVALID_PARAMETER;
981 break;
982 }
983
984 uint32_t destY;
985 if ((int)y < -yDiff)
986 {
987 h += yDiff + (int)y;
988 y = -yDiff;
989 destY = 0;
990 }
991 else
992 destY = y + yDiff;
993
994 if ( destX > pStream->uTargetWidth
995 || destY > pStream->uTargetHeight)
996 {
997 rc = VERR_INVALID_PARAMETER; /* nothing visible */
998 break;
999 }
1000
1001 if (destX + w > pStream->uTargetWidth)
1002 w = pStream->uTargetWidth - destX;
1003
1004 if (destY + h > pStream->uTargetHeight)
1005 h = pStream->uTargetHeight - destY;
1006
1007 /* Calculate bytes per pixel */
1008 uint32_t bpp = 1;
1009 if (uPixelFormat == BitmapFormat_BGR)
1010 {
1011 switch (uBitsPerPixel)
1012 {
1013 case 32:
1014 pStream->u32PixelFormat = VPX_IMG_FMT_RGB32;
1015 bpp = 4;
1016 break;
1017 case 24:
1018 pStream->u32PixelFormat = VPX_IMG_FMT_RGB24;
1019 bpp = 3;
1020 break;
1021 case 16:
1022 pStream->u32PixelFormat = VPX_IMG_FMT_RGB565;
1023 bpp = 2;
1024 break;
1025 default:
1026 AssertMsgFailed(("Unknown color depth! mBitsPerPixel=%d\n", uBitsPerPixel));
1027 break;
1028 }
1029 }
1030 else
1031 AssertMsgFailed(("Unknown pixel format! mPixelFormat=%d\n", uPixelFormat));
1032
1033 /* One of the dimensions of the current frame is smaller than before so
1034 * clear the entire buffer to prevent artifacts from the previous frame */
1035 if ( uSourceWidth < pStream->uLastSourceWidth
1036 || uSourceHeight < pStream->uLastSourceHeight)
1037 memset(pStream->pu8RgbBuf, 0, pStream->uTargetWidth * pStream->uTargetHeight * 4);
1038
1039 pStream->uLastSourceWidth = uSourceWidth;
1040 pStream->uLastSourceHeight = uSourceHeight;
1041
1042 /* Calculate start offset in source and destination buffers */
1043 uint32_t offSrc = y * uBytesPerLine + x * bpp;
1044 uint32_t offDst = (destY * pStream->uTargetWidth + destX) * bpp;
1045 /* do the copy */
1046 for (unsigned int i = 0; i < h; i++)
1047 {
1048 /* Overflow check */
1049 Assert(offSrc + w * bpp <= uSourceHeight * uBytesPerLine);
1050 Assert(offDst + w * bpp <= pStream->uTargetHeight * pStream->uTargetWidth * bpp);
1051 memcpy(pStream->pu8RgbBuf + offDst, pu8BufAddr + offSrc, w * bpp);
1052 offSrc += uBytesPerLine;
1053 offDst += pStream->uTargetWidth * bpp;
1054 }
1055
1056 pStream->u64TimeStamp = u64TimeStamp;
1057
1058 ASMAtomicWriteBool(&pStream->fRgbFilled, true);
1059 RTSemEventSignal(pCtx->WaitEvent);
1060 } while (0);
1061
1062 if (!ASMAtomicCmpXchgU32(&g_enmState, VIDREC_IDLE, VIDREC_COPYING))
1063 {
1064 rc = RTSemEventSignal(pCtx->TermEvent);
1065 AssertRC(rc);
1066 }
1067
1068 return rc;
1069}
Note: See TracBrowser for help on using the repository browser.

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