VirtualBox

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

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

VideoRec: Put VPX codec data into VIDEORECCODEC union.

  • 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: 32.7 KB
Line 
1/* $Id: VideoRec.cpp 65176 2017-01-06 10:17:41Z 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.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#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 RTMemFree(pStream);
528 pStream = NULL;
529
530 it = pCtx->vecStreams.erase(it);
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 for (VideoRecStreams::iterator it = pCtx->vecStreams.begin(); it != pCtx->vecStreams.end(); it++)
571 {
572 PVIDEORECSTREAM pStream = (*it);
573
574 if (pStream->fEnabled)
575 {
576 AssertPtr(pStream->pEBML);
577 int rc = pStream->pEBML->writeFooter(0);
578 AssertRC(rc);
579
580 pStream->pEBML->close();
581
582 vpx_img_free(&pStream->Codec.VPX.RawImage);
583 vpx_codec_err_t rcv = vpx_codec_destroy(&pStream->Codec.VPX.CodecCtx);
584 Assert(rcv == VPX_CODEC_OK); RT_NOREF(rcv);
585
586 if (pStream->pu8RgbBuf)
587 {
588 RTMemFree(pStream->pu8RgbBuf);
589 pStream->pu8RgbBuf = NULL;
590 }
591 }
592
593 delete pStream->pEBML;
594 }
595
596 RTMemFree(pCtx);
597
598 ASMAtomicWriteU32(&g_enmState, VIDREC_UNINITIALIZED);
599}
600
601/**
602 * VideoRec utility function to initialize video recording context.
603 *
604 * @returns IPRT status code.
605 * @param pCtx Pointer to video recording context to initialize Framebuffer width.
606 * @param uScreen Screen number.
607 * @param pszFile File to save the recorded data
608 * @param uWidth Width of the target image in the video recoriding file (movie)
609 * @param uHeight Height of the target image in video recording file.
610 * @param uRate Rate.
611 * @param uFps FPS.
612 * @param uMaxTime
613 * @param uMaxFileSize
614 * @param pszOptions
615 */
616int VideoRecStreamInit(PVIDEORECCONTEXT pCtx, uint32_t uScreen, const char *pszFile,
617 uint32_t uWidth, uint32_t uHeight, uint32_t uRate, uint32_t uFps,
618 uint32_t uMaxTime, uint32_t uMaxFileSize, const char *pszOptions)
619{
620 AssertPtrReturn(pCtx, VERR_INVALID_PARAMETER);
621 AssertReturn(uScreen < pCtx->vecStreams.size(), VERR_INVALID_PARAMETER);
622
623 pCtx->u64MaxTimeStamp = (uMaxTime > 0 ? RTTimeProgramMilliTS() + uMaxTime * 1000 : 0);
624 pCtx->uMaxFileSize = uMaxFileSize;
625
626 PVIDEORECSTREAM pStream = pCtx->vecStreams.at(uScreen);
627
628 pStream->uTargetWidth = uWidth;
629 pStream->uTargetHeight = uHeight;
630 pStream->pu8RgbBuf = (uint8_t *)RTMemAllocZ(uWidth * uHeight * 4);
631 AssertReturn(pStream->pu8RgbBuf, VERR_NO_MEMORY);
632 pStream->uEncoderDeadline = VPX_DL_REALTIME;
633
634 /* Play safe: the file must not exist, overwriting is potentially
635 * hazardous as nothing prevents the user from picking a file name of some
636 * other important file, causing unintentional data loss. */
637
638 int rc = pStream->pEBML->create(pszFile);
639 if (RT_FAILURE(rc))
640 {
641 LogRel(("Failed to create the video capture output file \"%s\" (%Rrc)\n", pszFile, rc));
642 return rc;
643 }
644
645 vpx_codec_err_t rcv = vpx_codec_enc_config_default(DEFAULTCODEC, &pStream->Codec.VPX.Config, 0);
646 if (rcv != VPX_CODEC_OK)
647 {
648 LogFlow(("Failed to configure codec: %s\n", vpx_codec_err_to_string(rcv)));
649 return VERR_INVALID_PARAMETER;
650 }
651
652 com::Utf8Str options(pszOptions);
653 size_t pos = 0;
654
655 do {
656
657 com::Utf8Str key, value;
658 pos = options.parseKeyValue(key, value, pos);
659
660 if (key == "quality")
661 {
662 if (value == "realtime")
663 {
664 pStream->uEncoderDeadline = VPX_DL_REALTIME;
665 }
666 else if (value == "good")
667 {
668 pStream->uEncoderDeadline = 1000000 / uFps;
669 }
670 else if (value == "best")
671 {
672 pStream->uEncoderDeadline = VPX_DL_BEST_QUALITY;
673 }
674 else
675 {
676 LogRel(("Settings quality deadline to = %s\n", value.c_str()));
677 pStream->uEncoderDeadline = value.toUInt32();
678 }
679 }
680 else LogRel(("Getting unknown option: %s=%s\n", key.c_str(), value.c_str()));
681
682 } while(pos != com::Utf8Str::npos);
683
684 /* target bitrate in kilobits per second */
685 pStream->Codec.VPX.Config.rc_target_bitrate = uRate;
686 /* frame width */
687 pStream->Codec.VPX.Config.g_w = uWidth;
688 /* frame height */
689 pStream->Codec.VPX.Config.g_h = uHeight;
690 /* 1ms per frame */
691 pStream->Codec.VPX.Config.g_timebase.num = 1;
692 pStream->Codec.VPX.Config.g_timebase.den = 1000;
693 /* disable multithreading */
694 pStream->Codec.VPX.Config.g_threads = 0;
695
696 pStream->uDelay = 1000 / uFps;
697
698 struct vpx_rational arg_framerate = { (int)uFps, 1 };
699 rc = pStream->pEBML->writeHeader(&pStream->Codec.VPX.Config, &arg_framerate);
700 AssertRCReturn(rc, rc);
701
702 /* Initialize codec */
703 rcv = vpx_codec_enc_init(&pStream->Codec.VPX.CodecCtx, DEFAULTCODEC, &pStream->Codec.VPX.Config, 0);
704 if (rcv != VPX_CODEC_OK)
705 {
706 LogFlow(("Failed to initialize VP8 encoder %s", vpx_codec_err_to_string(rcv)));
707 return VERR_INVALID_PARAMETER;
708 }
709
710 if (!vpx_img_alloc(&pStream->Codec.VPX.RawImage, VPX_IMG_FMT_I420, uWidth, uHeight, 1))
711 {
712 LogFlow(("Failed to allocate image %dx%d", uWidth, uHeight));
713 return VERR_NO_MEMORY;
714 }
715 pStream->pu8YuvBuf = pStream->Codec.VPX.RawImage.planes[0];
716
717 pCtx->fEnabled = true;
718 pStream->fEnabled = true;
719 return VINF_SUCCESS;
720}
721
722/**
723 * VideoRec utility function to check if recording is enabled.
724 *
725 * @returns true if recording is enabled
726 * @param pCtx Pointer to video recording context.
727 */
728bool VideoRecIsEnabled(PVIDEORECCONTEXT pCtx)
729{
730 RT_NOREF(pCtx);
731 uint32_t enmState = ASMAtomicReadU32(&g_enmState);
732 return ( enmState == VIDREC_IDLE
733 || enmState == VIDREC_COPYING);
734}
735
736/**
737 * VideoRec utility function to check if recording engine is ready to accept a new frame
738 * for the given screen.
739 *
740 * @returns true if recording engine is ready
741 * @param pCtx Pointer to video recording context.
742 * @param uScreen Screen ID.
743 * @param u64TimeStamp Current time stamp.
744 */
745bool VideoRecIsReady(PVIDEORECCONTEXT pCtx, uint32_t uScreen, uint64_t u64TimeStamp)
746{
747 uint32_t enmState = ASMAtomicReadU32(&g_enmState);
748 if (enmState != VIDREC_IDLE)
749 return false;
750
751 PVIDEORECSTREAM pStream = pCtx->vecStreams.at(uScreen);
752 if (!pStream->fEnabled)
753 return false;
754
755 if (u64TimeStamp < pStream->u64LastTimeStamp + pStream->uDelay)
756 return false;
757
758 if (ASMAtomicReadBool(&pStream->fRgbFilled))
759 return false;
760
761 return true;
762}
763
764/**
765 * VideoRec utility function to check if the file size has reached
766 * specified limits (if any).
767 *
768 * @returns true if any limit has been reached.
769 * @param pCtx Pointer to video recording context.
770 * @param uScreen Screen ID.
771 * @param u64TimeStamp Current time stamp.
772 */
773
774bool VideoRecIsFull(PVIDEORECCONTEXT pCtx, uint32_t uScreen, uint64_t u64TimeStamp)
775{
776 PVIDEORECSTREAM pStream = pCtx->vecStreams.at(uScreen);
777 if(!pStream->fEnabled)
778 return false;
779
780 if(pCtx->u64MaxTimeStamp > 0 && u64TimeStamp >= pCtx->u64MaxTimeStamp)
781 return true;
782
783 if (pCtx->uMaxFileSize > 0)
784 {
785 uint64_t sizeInMB = pStream->pEBML->getFileSize() / (1024 * 1024);
786 if(sizeInMB >= pCtx->uMaxFileSize)
787 return true;
788 }
789 /* Check for available free disk space */
790 if (pStream->pEBML->getAvailableSpace() < 0x100000)
791 {
792 LogRel(("Storage has not enough free space available, stopping video capture\n"));
793 return true;
794 }
795
796 return false;
797}
798
799/**
800 * VideoRec utility function to encode the source image and write the encoded
801 * image to target file.
802 *
803 * @returns IPRT status code.
804 * @param pStream Stream to encode and write.
805 */
806static int videoRecEncodeAndWrite(PVIDEORECSTREAM pStream)
807{
808 /* presentation time stamp */
809 vpx_codec_pts_t pts = pStream->u64TimeStamp;
810 vpx_codec_err_t rcv = vpx_codec_encode(&pStream->Codec.VPX.CodecCtx,
811 &pStream->Codec.VPX.RawImage,
812 pts /* time stamp */,
813 pStream->uDelay /* how long to show this frame */,
814 0 /* flags */,
815 pStream->uEncoderDeadline /* quality setting */);
816 if (rcv != VPX_CODEC_OK)
817 {
818 LogFlow(("Failed to encode:%s\n", vpx_codec_err_to_string(rcv)));
819 return VERR_GENERAL_FAILURE;
820 }
821
822 vpx_codec_iter_t iter = NULL;
823 int rc = VERR_NO_DATA;
824 for (;;)
825 {
826 const vpx_codec_cx_pkt_t *pkt = vpx_codec_get_cx_data(&pStream->Codec.VPX.CodecCtx, &iter);
827 if (!pkt)
828 break;
829 switch (pkt->kind)
830 {
831 case VPX_CODEC_CX_FRAME_PKT:
832 rc = pStream->pEBML->writeBlock(&pStream->Codec.VPX.Config, pkt);
833 break;
834 default:
835 LogFlow(("Unexpected CODEC Packet.\n"));
836 break;
837 }
838 }
839
840 pStream->cFrame++;
841 return rc;
842}
843
844/**
845 * VideoRec utility function to convert RGB to YUV.
846 *
847 * @returns IPRT status code.
848 * @param pStrm Strm.
849 */
850static int videoRecRGBToYUV(PVIDEORECSTREAM pStrm)
851{
852 switch (pStrm->u32PixelFormat)
853 {
854 case VPX_IMG_FMT_RGB32:
855 LogFlow(("32 bit\n"));
856 if (!colorConvWriteYUV420p<ColorConvBGRA32Iter>(pStrm->uTargetWidth,
857 pStrm->uTargetHeight,
858 pStrm->pu8YuvBuf,
859 pStrm->pu8RgbBuf))
860 return VERR_GENERAL_FAILURE;
861 break;
862 case VPX_IMG_FMT_RGB24:
863 LogFlow(("24 bit\n"));
864 if (!colorConvWriteYUV420p<ColorConvBGR24Iter>(pStrm->uTargetWidth,
865 pStrm->uTargetHeight,
866 pStrm->pu8YuvBuf,
867 pStrm->pu8RgbBuf))
868 return VERR_GENERAL_FAILURE;
869 break;
870 case VPX_IMG_FMT_RGB565:
871 LogFlow(("565 bit\n"));
872 if (!colorConvWriteYUV420p<ColorConvBGR565Iter>(pStrm->uTargetWidth,
873 pStrm->uTargetHeight,
874 pStrm->pu8YuvBuf,
875 pStrm->pu8RgbBuf))
876 return VERR_GENERAL_FAILURE;
877 break;
878 default:
879 return VERR_GENERAL_FAILURE;
880 }
881 return VINF_SUCCESS;
882}
883
884/**
885 * VideoRec utility function to copy a source image (FrameBuf) to the intermediate
886 * RGB buffer. This function is executed only once per time.
887 *
888 * @thread EMT
889 *
890 * @returns IPRT status code.
891 * @param pCtx Pointer to the video recording context.
892 * @param uScreen Screen number.
893 * @param x Starting x coordinate of the source buffer (Framebuffer).
894 * @param y Starting y coordinate of the source buffer (Framebuffer).
895 * @param uPixelFormat Pixel Format.
896 * @param uBitsPerPixel Bits Per Pixel
897 * @param uBytesPerLine Bytes per source scanlineName.
898 * @param uSourceWidth Width of the source image (framebuffer).
899 * @param uSourceHeight Height of the source image (framebuffer).
900 * @param pu8BufAddr Pointer to source image(framebuffer).
901 * @param u64TimeStamp Time stamp (milliseconds).
902 */
903int VideoRecCopyToIntBuf(PVIDEORECCONTEXT pCtx, uint32_t uScreen, uint32_t x, uint32_t y,
904 uint32_t uPixelFormat, uint32_t uBitsPerPixel, uint32_t uBytesPerLine,
905 uint32_t uSourceWidth, uint32_t uSourceHeight, uint8_t *pu8BufAddr,
906 uint64_t u64TimeStamp)
907{
908 /* Do not execute during termination and guard against termination */
909 if (!ASMAtomicCmpXchgU32(&g_enmState, VIDREC_COPYING, VIDREC_IDLE))
910 return VINF_TRY_AGAIN;
911
912 int rc = VINF_SUCCESS;
913 do
914 {
915 AssertPtrBreakStmt(pu8BufAddr, rc = VERR_INVALID_PARAMETER);
916 AssertBreakStmt(uSourceWidth, rc = VERR_INVALID_PARAMETER);
917 AssertBreakStmt(uSourceHeight, rc = VERR_INVALID_PARAMETER);
918 AssertBreakStmt(uScreen < pCtx->vecStreams.size(), rc = VERR_INVALID_PARAMETER);
919
920 PVIDEORECSTREAM pStream = pCtx->vecStreams.at(uScreen);
921 if (!pStream->fEnabled)
922 {
923 rc = VINF_TRY_AGAIN; /* not (yet) enabled */
924 break;
925 }
926 if (u64TimeStamp < pStream->u64LastTimeStamp + pStream->uDelay)
927 {
928 rc = VINF_TRY_AGAIN; /* respect maximum frames per second */
929 break;
930 }
931 if (ASMAtomicReadBool(&pStream->fRgbFilled))
932 {
933 rc = VERR_TRY_AGAIN; /* previous frame not yet encoded */
934 break;
935 }
936
937 pStream->u64LastTimeStamp = u64TimeStamp;
938
939 int xDiff = ((int)pStream->uTargetWidth - (int)uSourceWidth) / 2;
940 uint32_t w = uSourceWidth;
941 if ((int)w + xDiff + (int)x <= 0) /* nothing visible */
942 {
943 rc = VERR_INVALID_PARAMETER;
944 break;
945 }
946
947 uint32_t destX;
948 if ((int)x < -xDiff)
949 {
950 w += xDiff + x;
951 x = -xDiff;
952 destX = 0;
953 }
954 else
955 destX = x + xDiff;
956
957 uint32_t h = uSourceHeight;
958 int yDiff = ((int)pStream->uTargetHeight - (int)uSourceHeight) / 2;
959 if ((int)h + yDiff + (int)y <= 0) /* nothing visible */
960 {
961 rc = VERR_INVALID_PARAMETER;
962 break;
963 }
964
965 uint32_t destY;
966 if ((int)y < -yDiff)
967 {
968 h += yDiff + (int)y;
969 y = -yDiff;
970 destY = 0;
971 }
972 else
973 destY = y + yDiff;
974
975 if ( destX > pStream->uTargetWidth
976 || destY > pStream->uTargetHeight)
977 {
978 rc = VERR_INVALID_PARAMETER; /* nothing visible */
979 break;
980 }
981
982 if (destX + w > pStream->uTargetWidth)
983 w = pStream->uTargetWidth - destX;
984
985 if (destY + h > pStream->uTargetHeight)
986 h = pStream->uTargetHeight - destY;
987
988 /* Calculate bytes per pixel */
989 uint32_t bpp = 1;
990 if (uPixelFormat == BitmapFormat_BGR)
991 {
992 switch (uBitsPerPixel)
993 {
994 case 32:
995 pStream->u32PixelFormat = VPX_IMG_FMT_RGB32;
996 bpp = 4;
997 break;
998 case 24:
999 pStream->u32PixelFormat = VPX_IMG_FMT_RGB24;
1000 bpp = 3;
1001 break;
1002 case 16:
1003 pStream->u32PixelFormat = VPX_IMG_FMT_RGB565;
1004 bpp = 2;
1005 break;
1006 default:
1007 AssertMsgFailed(("Unknown color depth! mBitsPerPixel=%d\n", uBitsPerPixel));
1008 break;
1009 }
1010 }
1011 else
1012 AssertMsgFailed(("Unknown pixel format! mPixelFormat=%d\n", uPixelFormat));
1013
1014 /* One of the dimensions of the current frame is smaller than before so
1015 * clear the entire buffer to prevent artifacts from the previous frame */
1016 if ( uSourceWidth < pStream->uLastSourceWidth
1017 || uSourceHeight < pStream->uLastSourceHeight)
1018 memset(pStream->pu8RgbBuf, 0, pStream->uTargetWidth * pStream->uTargetHeight * 4);
1019
1020 pStream->uLastSourceWidth = uSourceWidth;
1021 pStream->uLastSourceHeight = uSourceHeight;
1022
1023 /* Calculate start offset in source and destination buffers */
1024 uint32_t offSrc = y * uBytesPerLine + x * bpp;
1025 uint32_t offDst = (destY * pStream->uTargetWidth + destX) * bpp;
1026 /* do the copy */
1027 for (unsigned int i = 0; i < h; i++)
1028 {
1029 /* Overflow check */
1030 Assert(offSrc + w * bpp <= uSourceHeight * uBytesPerLine);
1031 Assert(offDst + w * bpp <= pStream->uTargetHeight * pStream->uTargetWidth * bpp);
1032 memcpy(pStream->pu8RgbBuf + offDst, pu8BufAddr + offSrc, w * bpp);
1033 offSrc += uBytesPerLine;
1034 offDst += pStream->uTargetWidth * bpp;
1035 }
1036
1037 pStream->u64TimeStamp = u64TimeStamp;
1038
1039 ASMAtomicWriteBool(&pStream->fRgbFilled, true);
1040 RTSemEventSignal(pCtx->WaitEvent);
1041 } while (0);
1042
1043 if (!ASMAtomicCmpXchgU32(&g_enmState, VIDREC_IDLE, VIDREC_COPYING))
1044 {
1045 rc = RTSemEventSignal(pCtx->TermEvent);
1046 AssertRC(rc);
1047 }
1048
1049 return rc;
1050}
Note: See TracBrowser for help on using the repository browser.

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