VirtualBox

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

最後變更 在這個檔案從46578是 46578,由 vboxsync 提交於 12 年 前

Main/VPX: show how to set kf_max_dist

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
  • 屬性 svn:mergeinfo 設為 (切換已刪除的分支)
檔案大小: 27.4 KB
 
1/* $Id: VideoRec.cpp 46578 2013-06-17 10:05:38Z vboxsync $ */
2/** @file
3 * Encodes the screen content in VPX format.
4 */
5
6/*
7 * Copyright (C) 2012-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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#include <VBox/log.h>
20#include <iprt/asm.h>
21#include <iprt/assert.h>
22#include <iprt/semaphore.h>
23#include <iprt/thread.h>
24
25#include <VBox/com/VirtualBox.h>
26#include <VBox/com/com.h>
27#include <VBox/com/string.h>
28
29#include "EbmlWriter.h"
30#include "VideoRec.h"
31
32#define VPX_CODEC_DISABLE_COMPAT 1
33#include <vpx/vp8cx.h>
34#include <vpx/vpx_image.h>
35
36/** Default VPX codec to use */
37#define DEFAULTCODEC (vpx_codec_vp8_cx())
38
39static int videoRecEncodeAndWrite(PVIDEORECSTREAM pStrm);
40static int videoRecRGBToYUV(PVIDEORECSTREAM pStrm);
41
42/* state to synchronized between threads */
43enum
44{
45 VIDREC_UNINITIALIZED = 0,
46 /* initialized, idle */
47 VIDREC_IDLE = 1,
48 /* currently in VideoRecCopyToIntBuf(), delay termination */
49 VIDREC_COPYING = 2,
50 /* signal that we are terminating */
51 VIDREC_TERMINATING = 3
52};
53
54/* Must be always accessible and therefore cannot be part of VIDEORECCONTEXT */
55static uint32_t g_enmState = VIDREC_UNINITIALIZED;
56
57
58typedef struct VIDEORECSTREAM
59{
60 /* container context */
61 EbmlGlobal Ebml;
62 /* VPX codec context */
63 vpx_codec_ctx_t VpxCodec;
64 /* VPX configuration */
65 vpx_codec_enc_cfg_t VpxConfig;
66 /* X resolution */
67 uint32_t uTargetWidth;
68 /* Y resolution */
69 uint32_t uTargetHeight;
70 /* X resolution of the last encoded picture */
71 uint32_t uLastSourceWidth;
72 /* Y resolution of the last encoded picture */
73 uint32_t uLastSourceHeight;
74 /* current frame number */
75 uint32_t cFrame;
76 /* RGB buffer containing the most recent frame of the framebuffer */
77 uint8_t *pu8RgbBuf;
78 /* YUV buffer the encode function fetches the frame from */
79 uint8_t *pu8YuvBuf;
80 /* VPX image context */
81 vpx_image_t VpxRawImage;
82 /* true if video recording is enabled */
83 bool fEnabled;
84 /* true if the RGB buffer is filled */
85 bool fRgbFilled;
86 /* pixel format of the current frame */
87 uint32_t u32PixelFormat;
88 /* minimal delay between two frames */
89 uint32_t uDelay;
90 /* time stamp of the last frame we encoded */
91 uint64_t u64LastTimeStamp;
92 /* time stamp of the current frame */
93 uint64_t u64TimeStamp;
94} VIDEORECSTREAM;
95
96typedef struct VIDEORECCONTEXT
97{
98 /* semaphore to signal the encoding worker thread */
99 RTSEMEVENT WaitEvent;
100 /* semaphore required during termination */
101 RTSEMEVENT TermEvent;
102 /* true if video recording is enabled */
103 bool fEnabled;
104 /* worker thread */
105 RTTHREAD Thread;
106 /* number of stream contexts */
107 uint32_t cScreens;
108 /* video recording stream contexts */
109 VIDEORECSTREAM Strm[1];
110} VIDEORECCONTEXT;
111
112
113/**
114 * Iterator class for running through a BGRA32 image buffer and converting
115 * it to RGB.
116 */
117class ColorConvBGRA32Iter
118{
119private:
120 enum { PIX_SIZE = 4 };
121public:
122 ColorConvBGRA32Iter(unsigned aWidth, unsigned aHeight, uint8_t *aBuf)
123 {
124 LogFlow(("width = %d height=%d aBuf=%lx\n", aWidth, aHeight, aBuf));
125 mPos = 0;
126 mSize = aWidth * aHeight * PIX_SIZE;
127 mBuf = aBuf;
128 }
129 /**
130 * Convert the next pixel to RGB.
131 * @returns true on success, false if we have reached the end of the buffer
132 * @param aRed where to store the red value
133 * @param aGreen where to store the green value
134 * @param aBlue where to store the blue value
135 */
136 bool getRGB(unsigned *aRed, unsigned *aGreen, unsigned *aBlue)
137 {
138 bool rc = false;
139 if (mPos + PIX_SIZE <= mSize)
140 {
141 *aRed = mBuf[mPos + 2];
142 *aGreen = mBuf[mPos + 1];
143 *aBlue = mBuf[mPos ];
144 mPos += PIX_SIZE;
145 rc = true;
146 }
147 return rc;
148 }
149
150 /**
151 * Skip forward by a certain number of pixels
152 * @param aPixels how many pixels to skip
153 */
154 void skip(unsigned aPixels)
155 {
156 mPos += PIX_SIZE * aPixels;
157 }
158private:
159 /** Size of the picture buffer */
160 unsigned mSize;
161 /** Current position in the picture buffer */
162 unsigned mPos;
163 /** Address of the picture buffer */
164 uint8_t *mBuf;
165};
166
167/**
168 * Iterator class for running through an BGR24 image buffer and converting
169 * it to RGB.
170 */
171class ColorConvBGR24Iter
172{
173private:
174 enum { PIX_SIZE = 3 };
175public:
176 ColorConvBGR24Iter(unsigned aWidth, unsigned aHeight, uint8_t *aBuf)
177 {
178 mPos = 0;
179 mSize = aWidth * aHeight * PIX_SIZE;
180 mBuf = aBuf;
181 }
182 /**
183 * Convert the next pixel to RGB.
184 * @returns true on success, false if we have reached the end of the buffer
185 * @param aRed where to store the red value
186 * @param aGreen where to store the green value
187 * @param aBlue where to store the blue value
188 */
189 bool getRGB(unsigned *aRed, unsigned *aGreen, unsigned *aBlue)
190 {
191 bool rc = false;
192 if (mPos + PIX_SIZE <= mSize)
193 {
194 *aRed = mBuf[mPos + 2];
195 *aGreen = mBuf[mPos + 1];
196 *aBlue = mBuf[mPos ];
197 mPos += PIX_SIZE;
198 rc = true;
199 }
200 return rc;
201 }
202
203 /**
204 * Skip forward by a certain number of pixels
205 * @param aPixels how many pixels to skip
206 */
207 void skip(unsigned aPixels)
208 {
209 mPos += PIX_SIZE * aPixels;
210 }
211private:
212 /** Size of the picture buffer */
213 unsigned mSize;
214 /** Current position in the picture buffer */
215 unsigned mPos;
216 /** Address of the picture buffer */
217 uint8_t *mBuf;
218};
219
220/**
221 * Iterator class for running through an BGR565 image buffer and converting
222 * it to RGB.
223 */
224class ColorConvBGR565Iter
225{
226private:
227 enum { PIX_SIZE = 2 };
228public:
229 ColorConvBGR565Iter(unsigned aWidth, unsigned aHeight, uint8_t *aBuf)
230 {
231 mPos = 0;
232 mSize = aWidth * aHeight * PIX_SIZE;
233 mBuf = aBuf;
234 }
235 /**
236 * Convert the next pixel to RGB.
237 * @returns true on success, false if we have reached the end of the buffer
238 * @param aRed where to store the red value
239 * @param aGreen where to store the green value
240 * @param aBlue where to store the blue value
241 */
242 bool getRGB(unsigned *aRed, unsigned *aGreen, unsigned *aBlue)
243 {
244 bool rc = false;
245 if (mPos + PIX_SIZE <= mSize)
246 {
247 unsigned uFull = (((unsigned) mBuf[mPos + 1]) << 8)
248 | ((unsigned) mBuf[mPos]);
249 *aRed = (uFull >> 8) & ~7;
250 *aGreen = (uFull >> 3) & ~3 & 0xff;
251 *aBlue = (uFull << 3) & ~7 & 0xff;
252 mPos += PIX_SIZE;
253 rc = true;
254 }
255 return rc;
256 }
257
258 /**
259 * Skip forward by a certain number of pixels
260 * @param aPixels how many pixels to skip
261 */
262 void skip(unsigned aPixels)
263 {
264 mPos += PIX_SIZE * aPixels;
265 }
266private:
267 /** Size of the picture buffer */
268 unsigned mSize;
269 /** Current position in the picture buffer */
270 unsigned mPos;
271 /** Address of the picture buffer */
272 uint8_t *mBuf;
273};
274
275/**
276 * Convert an image to YUV420p format
277 * @returns true on success, false on failure
278 * @param aWidth width of image
279 * @param aHeight height of image
280 * @param aDestBuf an allocated memory buffer large enough to hold the
281 * destination image (i.e. width * height * 12bits)
282 * @param aSrcBuf the source image as an array of bytes
283 */
284template <class T>
285inline bool colorConvWriteYUV420p(unsigned aWidth, unsigned aHeight,
286 uint8_t *aDestBuf, uint8_t *aSrcBuf)
287{
288 AssertReturn(0 == (aWidth & 1), false);
289 AssertReturn(0 == (aHeight & 1), false);
290 bool rc = true;
291 T iter1(aWidth, aHeight, aSrcBuf);
292 T iter2 = iter1;
293 iter2.skip(aWidth);
294 unsigned cPixels = aWidth * aHeight;
295 unsigned offY = 0;
296 unsigned offU = cPixels;
297 unsigned offV = cPixels + cPixels / 4;
298 for (unsigned i = 0; (i < aHeight / 2) && rc; ++i)
299 {
300 for (unsigned j = 0; (j < aWidth / 2) && rc; ++j)
301 {
302 unsigned red, green, blue, u, v;
303 rc = iter1.getRGB(&red, &green, &blue);
304 if (rc)
305 {
306 aDestBuf[offY] = ((66 * red + 129 * green + 25 * blue + 128) >> 8) + 16;
307 u = (((-38 * red - 74 * green + 112 * blue + 128) >> 8) + 128) / 4;
308 v = (((112 * red - 94 * green - 18 * blue + 128) >> 8) + 128) / 4;
309 rc = iter1.getRGB(&red, &green, &blue);
310 }
311 if (rc)
312 {
313 aDestBuf[offY + 1] = ((66 * red + 129 * green + 25 * blue + 128) >> 8) + 16;
314 u += (((-38 * red - 74 * green + 112 * blue + 128) >> 8) + 128) / 4;
315 v += (((112 * red - 94 * green - 18 * blue + 128) >> 8) + 128) / 4;
316 rc = iter2.getRGB(&red, &green, &blue);
317 }
318 if (rc)
319 {
320 aDestBuf[offY + aWidth] = ((66 * red + 129 * green + 25 * blue + 128) >> 8) + 16;
321 u += (((-38 * red - 74 * green + 112 * blue + 128) >> 8) + 128) / 4;
322 v += (((112 * red - 94 * green - 18 * blue + 128) >> 8) + 128) / 4;
323 rc = iter2.getRGB(&red, &green, &blue);
324 }
325 if (rc)
326 {
327 aDestBuf[offY + aWidth + 1] = ((66 * red + 129 * green + 25 * blue + 128) >> 8) + 16;
328 u += (((-38 * red - 74 * green + 112 * blue + 128) >> 8) + 128) / 4;
329 v += (((112 * red - 94 * green - 18 * blue + 128) >> 8) + 128) / 4;
330 aDestBuf[offU] = u;
331 aDestBuf[offV] = v;
332 offY += 2;
333 ++offU;
334 ++offV;
335 }
336 }
337 if (rc)
338 {
339 iter1.skip(aWidth);
340 iter2.skip(aWidth);
341 offY += aWidth;
342 }
343 }
344 return rc;
345}
346
347/**
348 * Convert an image to RGB24 format
349 * @returns true on success, false on failure
350 * @param aWidth width of image
351 * @param aHeight height of image
352 * @param aDestBuf an allocated memory buffer large enough to hold the
353 * destination image (i.e. width * height * 12bits)
354 * @param aSrcBuf the source image as an array of bytes
355 */
356template <class T>
357inline bool colorConvWriteRGB24(unsigned aWidth, unsigned aHeight,
358 uint8_t *aDestBuf, uint8_t *aSrcBuf)
359{
360 enum { PIX_SIZE = 3 };
361 bool rc = true;
362 AssertReturn(0 == (aWidth & 1), false);
363 AssertReturn(0 == (aHeight & 1), false);
364 T iter(aWidth, aHeight, aSrcBuf);
365 unsigned cPixels = aWidth * aHeight;
366 for (unsigned i = 0; i < cPixels && rc; ++i)
367 {
368 unsigned red, green, blue;
369 rc = iter.getRGB(&red, &green, &blue);
370 if (rc)
371 {
372 aDestBuf[i * PIX_SIZE ] = red;
373 aDestBuf[i * PIX_SIZE + 1] = green;
374 aDestBuf[i * PIX_SIZE + 2] = blue;
375 }
376 }
377 return rc;
378}
379
380/**
381 * Worker thread for all streams.
382 *
383 * RGB/YUV conversion and encoding.
384 */
385static DECLCALLBACK(int) videoRecThread(RTTHREAD Thread, void *pvUser)
386{
387 PVIDEORECCONTEXT pCtx = (PVIDEORECCONTEXT)pvUser;
388 for (;;)
389 {
390 int rc = RTSemEventWait(pCtx->WaitEvent, RT_INDEFINITE_WAIT);
391 AssertRCBreak(rc);
392
393 if (ASMAtomicReadU32(&g_enmState) == VIDREC_TERMINATING)
394 break;
395 for (unsigned uScreen = 0; uScreen < pCtx->cScreens; uScreen++)
396 {
397 PVIDEORECSTREAM pStrm = &pCtx->Strm[uScreen];
398 if ( pStrm->fEnabled
399 && ASMAtomicReadBool(&pStrm->fRgbFilled))
400 {
401 rc = videoRecRGBToYUV(pStrm);
402 ASMAtomicWriteBool(&pStrm->fRgbFilled, false);
403 if (RT_SUCCESS(rc))
404 rc = videoRecEncodeAndWrite(pStrm);
405 if (RT_FAILURE(rc))
406 {
407 static unsigned cErrors = 100;
408 if (cErrors > 0)
409 {
410 LogRel(("Error %Rrc encoding / writing video frame\n", rc));
411 cErrors--;
412 }
413 }
414 }
415 }
416 }
417
418 return VINF_SUCCESS;
419}
420
421/**
422 * VideoRec utility function to create video recording context.
423 *
424 * @returns IPRT status code.
425 * @param ppCtx Video recording context
426 * @param cScreens Number of screens.
427 */
428int VideoRecContextCreate(PVIDEORECCONTEXT *ppCtx, uint32_t cScreens)
429{
430 Assert(ASMAtomicReadU32(&g_enmState) == VIDREC_UNINITIALIZED);
431
432 PVIDEORECCONTEXT pCtx = (PVIDEORECCONTEXT)RTMemAllocZ(RT_OFFSETOF(VIDEORECCONTEXT, Strm[cScreens]));
433 *ppCtx = pCtx;
434 AssertPtrReturn(pCtx, VERR_NO_MEMORY);
435
436 pCtx->cScreens = cScreens;
437 for (unsigned uScreen = 0; uScreen < cScreens; uScreen++)
438 pCtx->Strm[uScreen].Ebml.last_pts_ms = -1;
439
440 int rc = RTSemEventCreate(&pCtx->WaitEvent);
441 AssertRCReturn(rc, rc);
442
443 rc = RTSemEventCreate(&pCtx->TermEvent);
444 AssertRCReturn(rc, rc);
445
446 rc = RTThreadCreate(&pCtx->Thread, videoRecThread, (void*)pCtx, 0,
447 RTTHREADTYPE_MAIN_WORKER, RTTHREADFLAGS_WAITABLE, "VideoRec");
448 AssertRCReturn(rc, rc);
449
450 ASMAtomicWriteU32(&g_enmState, VIDREC_IDLE);
451 return VINF_SUCCESS;
452}
453
454/**
455 * VideoRec utility function to initialize video recording context.
456 *
457 * @returns IPRT status code.
458 * @param pCtx Pointer to video recording context to initialize Framebuffer width.
459 * @param uScreeen Screen number.
460 * @param strFile File to save the recorded data
461 * @param uTargetWidth Width of the target image in the video recoriding file (movie)
462 * @param uTargetHeight Height of the target image in video recording file.
463 */
464int VideoRecStrmInit(PVIDEORECCONTEXT pCtx, uint32_t uScreen, const char *pszFile,
465 uint32_t uWidth, uint32_t uHeight, uint32_t uRate, uint32_t uFps)
466{
467 AssertPtrReturn(pCtx, VERR_INVALID_PARAMETER);
468 AssertReturn(uScreen < pCtx->cScreens, VERR_INVALID_PARAMETER);
469
470 PVIDEORECSTREAM pStrm = &pCtx->Strm[uScreen];
471 pStrm->uTargetWidth = uWidth;
472 pStrm->uTargetHeight = uHeight;
473 pStrm->pu8RgbBuf = (uint8_t *)RTMemAllocZ(uWidth * uHeight * 4);
474 AssertReturn(pStrm->pu8RgbBuf, VERR_NO_MEMORY);
475
476 int rc = RTFileOpen(&pStrm->Ebml.file, pszFile,
477 RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
478 if (RT_FAILURE(rc))
479 {
480 LogFlow(("Failed to open the video capture output File (%Rrc)\n", rc));
481 return rc;
482 }
483
484 vpx_codec_err_t rcv = vpx_codec_enc_config_default(DEFAULTCODEC, &pStrm->VpxConfig, 0);
485 if (rcv != VPX_CODEC_OK)
486 {
487 LogFlow(("Failed to configure codec\n", vpx_codec_err_to_string(rcv)));
488 return VERR_INVALID_PARAMETER;
489 }
490
491 /* target bitrate in kilobits per second */
492 pStrm->VpxConfig.rc_target_bitrate = uRate;
493 /* frame width */
494 pStrm->VpxConfig.g_w = uWidth;
495 /* frame height */
496 pStrm->VpxConfig.g_h = uHeight;
497 /* 1ms per frame */
498 pStrm->VpxConfig.g_timebase.num = 1;
499 pStrm->VpxConfig.g_timebase.den = 1000;
500 /* disable multithreading */
501 pStrm->VpxConfig.g_threads = 0;
502// pStrm->VpxConfig.kf_max_dist = 1;
503 pStrm->uDelay = 1000 / uFps;
504
505 struct vpx_rational arg_framerate = { 30, 1 };
506 rc = Ebml_WriteWebMFileHeader(&pStrm->Ebml, &pStrm->VpxConfig, &arg_framerate);
507 AssertRCReturn(rc, rc);
508
509 /* Initialize codec */
510 rcv = vpx_codec_enc_init(&pStrm->VpxCodec, DEFAULTCODEC, &pStrm->VpxConfig, 0);
511 if (rcv != VPX_CODEC_OK)
512 {
513 LogFlow(("Failed to initialize VP8 encoder %s", vpx_codec_err_to_string(rcv)));
514 return VERR_INVALID_PARAMETER;
515 }
516
517 if (!vpx_img_alloc(&pStrm->VpxRawImage, VPX_IMG_FMT_I420, uWidth, uHeight, 1))
518 {
519 LogFlow(("Failed to allocate image %dx%d", uWidth, uHeight));
520 return VERR_NO_MEMORY;
521 }
522 pStrm->pu8YuvBuf = pStrm->VpxRawImage.planes[0];
523
524 pCtx->fEnabled = true;
525 pStrm->fEnabled = true;
526 return VINF_SUCCESS;
527}
528
529/**
530 * VideoRec utility function to close the video recording context.
531 *
532 * @param pCtx Pointer to video recording context.
533 */
534void VideoRecContextClose(PVIDEORECCONTEXT pCtx)
535{
536 if (!pCtx)
537 return;
538
539 uint32_t enmState = VIDREC_IDLE;
540 for (;;)
541 {
542 if (ASMAtomicCmpXchgExU32(&g_enmState, VIDREC_TERMINATING, enmState, &enmState))
543 break;
544 if (enmState == VIDREC_UNINITIALIZED)
545 return;
546 }
547 if (enmState == VIDREC_COPYING)
548 {
549 int rc = RTSemEventWait(pCtx->TermEvent, RT_INDEFINITE_WAIT);
550 AssertRC(rc);
551 }
552
553 RTSemEventSignal(pCtx->WaitEvent);
554 RTThreadWait(pCtx->Thread, 10000, NULL);
555 RTSemEventDestroy(pCtx->WaitEvent);
556 RTSemEventDestroy(pCtx->TermEvent);
557
558 for (unsigned uScreen = 0; uScreen < pCtx->cScreens; uScreen++)
559 {
560 PVIDEORECSTREAM pStrm = &pCtx->Strm[uScreen];
561 if (pStrm->fEnabled)
562 {
563 if (pStrm->Ebml.file != NIL_RTFILE)
564 {
565 int rc = Ebml_WriteWebMFileFooter(&pStrm->Ebml, 0);
566 AssertRC(rc);
567 RTFileClose(pStrm->Ebml.file);
568 pStrm->Ebml.file = NIL_RTFILE;
569 }
570 if (pStrm->Ebml.cue_list)
571 {
572 RTMemFree(pStrm->Ebml.cue_list);
573 pStrm->Ebml.cue_list = NULL;
574 }
575 vpx_img_free(&pStrm->VpxRawImage);
576 vpx_codec_err_t rcv = vpx_codec_destroy(&pStrm->VpxCodec);
577 Assert(rcv == VPX_CODEC_OK);
578 RTMemFree(pStrm->pu8RgbBuf);
579 pStrm->pu8RgbBuf = NULL;
580 }
581 }
582
583 RTMemFree(pCtx);
584
585 ASMAtomicWriteU32(&g_enmState, VIDREC_UNINITIALIZED);
586}
587
588/**
589 * VideoRec utility function to check if recording is enabled.
590 *
591 * @returns true if recording is enabled
592 * @param pCtx Pointer to video recording context.
593 */
594bool VideoRecIsEnabled(PVIDEORECCONTEXT pCtx)
595{
596 uint32_t enmState = ASMAtomicReadU32(&g_enmState);
597 return ( enmState == VIDREC_IDLE
598 || enmState == VIDREC_COPYING);
599}
600
601/**
602 * VideoRec utility function to encode the source image and write the encoded
603 * image to target file.
604 *
605 * @returns IPRT status code.
606 * @param pCtx Pointer to video recording context.
607 * @param uSourceWidth Width of the source image.
608 * @param uSourceHeight Height of the source image.
609 */
610static int videoRecEncodeAndWrite(PVIDEORECSTREAM pStrm)
611{
612 /* presentation time stamp */
613 vpx_codec_pts_t pts = pStrm->u64TimeStamp;
614 vpx_codec_err_t rcv = vpx_codec_encode(&pStrm->VpxCodec,
615 &pStrm->VpxRawImage,
616 pts /* time stamp */,
617 10 /* how long to show this frame */,
618 0 /* flags */,
619 VPX_DL_REALTIME /* deadline */);
620 if (rcv != VPX_CODEC_OK)
621 {
622 LogFlow(("Failed to encode:%s\n", vpx_codec_err_to_string(rcv)));
623 return VERR_GENERAL_FAILURE;
624 }
625
626 vpx_codec_iter_t iter = NULL;
627 int rc = VERR_NO_DATA;
628 for (;;)
629 {
630 const vpx_codec_cx_pkt_t *pkt = vpx_codec_get_cx_data(&pStrm->VpxCodec, &iter);
631 if (!pkt)
632 break;
633 switch (pkt->kind)
634 {
635 case VPX_CODEC_CX_FRAME_PKT:
636 rc = Ebml_WriteWebMBlock(&pStrm->Ebml, &pStrm->VpxConfig, pkt);
637 break;
638 default:
639 LogFlow(("Unexpected CODEC Packet.\n"));
640 break;
641 }
642 }
643
644 pStrm->cFrame++;
645 return rc;
646}
647
648/**
649 * VideoRec utility function to convert RGB to YUV.
650 *
651 * @returns IPRT status code.
652 * @param pCtx Pointer to video recording context.
653 */
654static int videoRecRGBToYUV(PVIDEORECSTREAM pStrm)
655{
656 switch (pStrm->u32PixelFormat)
657 {
658 case VPX_IMG_FMT_RGB32:
659 LogFlow(("32 bit\n"));
660 if (!colorConvWriteYUV420p<ColorConvBGRA32Iter>(pStrm->uTargetWidth,
661 pStrm->uTargetHeight,
662 pStrm->pu8YuvBuf,
663 pStrm->pu8RgbBuf))
664 return VERR_GENERAL_FAILURE;
665 break;
666 case VPX_IMG_FMT_RGB24:
667 LogFlow(("24 bit\n"));
668 if (!colorConvWriteYUV420p<ColorConvBGR24Iter>(pStrm->uTargetWidth,
669 pStrm->uTargetHeight,
670 pStrm->pu8YuvBuf,
671 pStrm->pu8RgbBuf))
672 return VERR_GENERAL_FAILURE;
673 break;
674 case VPX_IMG_FMT_RGB565:
675 LogFlow(("565 bit\n"));
676 if (!colorConvWriteYUV420p<ColorConvBGR565Iter>(pStrm->uTargetWidth,
677 pStrm->uTargetHeight,
678 pStrm->pu8YuvBuf,
679 pStrm->pu8RgbBuf))
680 return VERR_GENERAL_FAILURE;
681 break;
682 default:
683 return VERR_GENERAL_FAILURE;
684 }
685 return VINF_SUCCESS;
686}
687
688/**
689 * VideoRec utility function to copy a source image (FrameBuf) to the intermediate
690 * RGB buffer. This function is executed only once per time.
691 *
692 * @thread EMT
693 *
694 * @returns IPRT status code.
695 * @param pCtx Pointer to the video recording context.
696 * @param uScreen Screen number.
697 * @param x Starting x coordinate of the source buffer (Framebuffer).
698 * @param y Starting y coordinate of the source buffer (Framebuffer).
699 * @param uPixelFormat Pixel Format.
700 * @param uBitsPerPixel Bits Per Pixel
701 * @param uBytesPerLine Bytes per source scanlineName.
702 * @param uSourceWidth Width of the source image (framebuffer).
703 * @param uSourceHeight Height of the source image (framebuffer).
704 * @param pu8BufAddr Pointer to source image(framebuffer).
705 * @param u64TimeStamp Time stamp (milliseconds).
706 */
707int VideoRecCopyToIntBuf(PVIDEORECCONTEXT pCtx, uint32_t uScreen, uint32_t x, uint32_t y,
708 uint32_t uPixelFormat, uint32_t uBitsPerPixel, uint32_t uBytesPerLine,
709 uint32_t uSourceWidth, uint32_t uSourceHeight, uint8_t *pu8BufAddr,
710 uint64_t u64TimeStamp)
711{
712 /* Do not execute during termination and guard against termination */
713 if (!ASMAtomicCmpXchgU32(&g_enmState, VIDREC_COPYING, VIDREC_IDLE))
714 return VINF_TRY_AGAIN;
715
716 int rc = VINF_SUCCESS;
717 do
718 {
719 AssertPtrBreakStmt(pu8BufAddr, rc = VERR_INVALID_PARAMETER);
720 AssertBreakStmt(uSourceWidth, rc = VERR_INVALID_PARAMETER);
721 AssertBreakStmt(uSourceHeight, rc = VERR_INVALID_PARAMETER);
722 AssertBreakStmt(uScreen < pCtx->cScreens, rc = VERR_INVALID_PARAMETER);
723
724 PVIDEORECSTREAM pStrm = &pCtx->Strm[uScreen];
725 if (!pStrm->fEnabled)
726 {
727 rc = VINF_TRY_AGAIN; /* not (yet) enabled */
728 break;
729 }
730 if (u64TimeStamp < pStrm->u64LastTimeStamp + pStrm->uDelay)
731 {
732 rc = VINF_TRY_AGAIN; /* respect maximum frames per second */
733 break;
734 }
735 if (ASMAtomicReadBool(&pStrm->fRgbFilled))
736 {
737 rc = VERR_TRY_AGAIN; /* previous frame not yet encoded */
738 break;
739 }
740
741 pStrm->u64LastTimeStamp = u64TimeStamp;
742
743 int xDiff = ((int)pStrm->uTargetWidth - (int)uSourceWidth) / 2;
744 uint32_t w = uSourceWidth;
745 if ((int)w + xDiff + (int)x <= 0) /* nothing visible */
746 {
747 rc = VERR_INVALID_PARAMETER;
748 break;
749 }
750
751 uint32_t destX;
752 if ((int)x < -xDiff)
753 {
754 w += xDiff + x;
755 x = -xDiff;
756 destX = 0;
757 }
758 else
759 destX = x + xDiff;
760
761 uint32_t h = uSourceHeight;
762 int yDiff = ((int)pStrm->uTargetHeight - (int)uSourceHeight) / 2;
763 if ((int)h + yDiff + (int)y <= 0) /* nothing visible */
764 {
765 rc = VERR_INVALID_PARAMETER;
766 break;
767 }
768
769 uint32_t destY;
770 if ((int)y < -yDiff)
771 {
772 h += yDiff + (int)y;
773 y = -yDiff;
774 destY = 0;
775 }
776 else
777 destY = y + yDiff;
778
779 if ( destX > pStrm->uTargetWidth
780 || destY > pStrm->uTargetHeight)
781 {
782 rc = VERR_INVALID_PARAMETER; /* nothing visible */
783 break;
784 }
785
786 if (destX + w > pStrm->uTargetWidth)
787 w = pStrm->uTargetWidth - destX;
788
789 if (destY + h > pStrm->uTargetHeight)
790 h = pStrm->uTargetHeight - destY;
791
792 /* Calculate bytes per pixel */
793 uint32_t bpp = 1;
794 if (uPixelFormat == FramebufferPixelFormat_FOURCC_RGB)
795 {
796 switch (uBitsPerPixel)
797 {
798 case 32:
799 pStrm->u32PixelFormat = VPX_IMG_FMT_RGB32;
800 bpp = 4;
801 break;
802 case 24:
803 pStrm->u32PixelFormat = VPX_IMG_FMT_RGB24;
804 bpp = 3;
805 break;
806 case 16:
807 pStrm->u32PixelFormat = VPX_IMG_FMT_RGB565;
808 bpp = 2;
809 break;
810 default:
811 AssertMsgFailed(("Unknown color depth! mBitsPerPixel=%d\n", uBitsPerPixel));
812 break;
813 }
814 }
815 else
816 AssertMsgFailed(("Unknown pixel format! mPixelFormat=%d\n", uPixelFormat));
817
818 /* One of the dimensions of the current frame is smaller than before so
819 * clear the entire buffer to prevent artifacts from the previous frame */
820 if ( uSourceWidth < pStrm->uLastSourceWidth
821 || uSourceHeight < pStrm->uLastSourceHeight)
822 memset(pStrm->pu8RgbBuf, 0, pStrm->uTargetWidth * pStrm->uTargetHeight * 4);
823
824 pStrm->uLastSourceWidth = uSourceWidth;
825 pStrm->uLastSourceHeight = uSourceHeight;
826
827 /* Calculate start offset in source and destination buffers */
828 uint32_t offSrc = y * uBytesPerLine + x * bpp;
829 uint32_t offDst = (destY * pStrm->uTargetWidth + destX) * bpp;
830 /* do the copy */
831 for (unsigned int i = 0; i < h; i++)
832 {
833 /* Overflow check */
834 Assert(offSrc + w * bpp <= uSourceHeight * uBytesPerLine);
835 Assert(offDst + w * bpp <= pStrm->uTargetHeight * pStrm->uTargetWidth * bpp);
836 memcpy(pStrm->pu8RgbBuf + offDst, pu8BufAddr + offSrc, w * bpp);
837 offSrc += uBytesPerLine;
838 offDst += pStrm->uTargetWidth * bpp;
839 }
840
841 pStrm->u64TimeStamp = u64TimeStamp;
842
843 ASMAtomicWriteBool(&pStrm->fRgbFilled, true);
844 RTSemEventSignal(pCtx->WaitEvent);
845 } while (0);
846
847 if (!ASMAtomicCmpXchgU32(&g_enmState, VIDREC_IDLE, VIDREC_COPYING))
848 {
849 rc = RTSemEventSignal(pCtx->TermEvent);
850 AssertRC(rc);
851 }
852
853 return rc;
854}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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