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