VirtualBox

Changeset 65421 in vbox for trunk/src


Ignore:
Timestamp:
Jan 24, 2017 1:55:14 PM (8 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
113034
Message:

VideoRec: Group video parameters, a bit of renaming.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/src-client/VideoRec.cpp

    r65418 r65421  
    120120    /** Screen ID. */
    121121    uint16_t            uScreen;
    122     /** Target X resolution (in pixels). */
    123     uint32_t            uDstWidth;
    124     /** Target Y resolution (in pixels). */
    125     uint32_t            uDstHeight;
    126     /** X resolution of the last encoded frame. */
    127     uint32_t            uSrcLastWidth;
    128     /** Y resolution of the last encoded frame. */
    129     uint32_t            uSrcLastHeight;
    130     /** Current frame number. */
    131     uint64_t            cFrame;
    132     /** RGB buffer containing the most recent frame of the framebuffer. */
    133     uint8_t            *pu8RgbBuf;
    134     /** YUV buffer the encode function fetches the frame from. */
    135     uint8_t            *pu8YuvBuf;
    136122    /** Whether video recording is enabled or not. */
    137123    bool                fEnabled;
    138     /** Whether the RGB buffer is filled or not. */
    139     bool                fRgbFilled;
    140     /** Pixel format of the current frame. */
    141     uint32_t            u32PixelFormat;
    142     /** Minimal delay between two frames. */
    143     uint32_t            uDelay;
    144124    /** Time stamp (in ms) of the last frame we encoded. */
    145125    uint64_t            uLastTimeStampMs;
    146126    /** Time stamp (in ms) of the current frame. */
    147127    uint64_t            uCurTimeStampMs;
    148     /** Encoder deadline. */
    149     unsigned int        uEncoderDeadline;
     128
     129    struct
     130    {
     131        /** Target X resolution (in pixels). */
     132        uint32_t            uDstWidth;
     133        /** Target Y resolution (in pixels). */
     134        uint32_t            uDstHeight;
     135        /** X resolution of the last encoded frame. */
     136        uint32_t            uSrcLastWidth;
     137        /** Y resolution of the last encoded frame. */
     138        uint32_t            uSrcLastHeight;
     139        /** Current frame number. */
     140        uint64_t            cFrame;
     141        /** RGB buffer containing the most recent frame of the framebuffer. */
     142        uint8_t            *pu8RgbBuf;
     143        /** YUV buffer the encode function fetches the frame from. */
     144        uint8_t            *pu8YuvBuf;
     145        /** Whether the RGB buffer is filled or not. */
     146        bool                fRgbFilled;
     147        /** Pixel format of the current frame. */
     148        uint32_t            uPixelFormat;
     149        /** Minimal delay between two frames. */
     150        uint32_t            uDelay;
     151        /** Encoder deadline. */
     152        unsigned int        uEncoderDeadline;
     153    } Video;
     154
    150155} VIDEORECSTREAM, *PVIDEORECSTREAM;
    151156
     
    461466
    462467            if (   pStream->fEnabled
    463                 && ASMAtomicReadBool(&pStream->fRgbFilled))
     468                && ASMAtomicReadBool(&pStream->Video.fRgbFilled))
    464469            {
    465470                rc = videoRecRGBToYUV(pStream);
    466471
    467                 ASMAtomicWriteBool(&pStream->fRgbFilled, false);
     472                ASMAtomicWriteBool(&pStream->Video.fRgbFilled, false);
    468473
    469474                if (RT_SUCCESS(rc))
     
    472477                if (RT_FAILURE(rc))
    473478                {
    474                     static unsigned cErrors = 100;
    475                     if (cErrors > 0)
     479                    static unsigned s_cErrEnc = 100;
     480                    if (s_cErrEnc > 0)
    476481                    {
    477482                        LogRel(("VideoRec: Error %Rrc encoding / writing video frame\n", rc));
    478                         cErrors--;
     483                        s_cErrEnc--;
    479484                    }
    480485                }
     
    615620            Assert(rcv == VPX_CODEC_OK); RT_NOREF(rcv);
    616621
    617             if (pStream->pu8RgbBuf)
     622            if (pStream->Video.pu8RgbBuf)
    618623            {
    619                 RTMemFree(pStream->pu8RgbBuf);
    620                 pStream->pu8RgbBuf = NULL;
     624                RTMemFree(pStream->Video.pu8RgbBuf);
     625                pStream->Video.pu8RgbBuf = NULL;
    621626            }
    622627
     
    701706    pCtx->uMaxSizeMB = uMaxSizeMB;
    702707
    703     pStream->uDstWidth  = uWidth;
    704     pStream->uDstHeight = uHeight;
    705     pStream->pu8RgbBuf = (uint8_t *)RTMemAllocZ(uWidth * uHeight * 4);
    706     AssertReturn(pStream->pu8RgbBuf, VERR_NO_MEMORY);
     708    pStream->Video.uDstWidth  = uWidth;
     709    pStream->Video.uDstHeight = uHeight;
     710    pStream->Video.pu8RgbBuf = (uint8_t *)RTMemAllocZ(uWidth * uHeight * 4);
     711    AssertReturn(pStream->Video.pu8RgbBuf, VERR_NO_MEMORY);
    707712
    708713    /* Play safe: the file must not exist, overwriting is potentially
     
    711716
    712717#ifdef VBOX_WITH_LIBVPX
    713     pStream->uEncoderDeadline = VPX_DL_REALTIME;
     718    pStream->Video.uEncoderDeadline = VPX_DL_REALTIME;
    714719
    715720    vpx_codec_err_t rcv = vpx_codec_enc_config_default(DEFAULTCODEC, &pStream->Codec.VPX.Config, 0);
     
    738743            {
    739744#ifdef VBOX_WITH_LIBVPX
    740                 pStream->uEncoderDeadline = VPX_DL_REALTIME;
     745                pStream->Video.uEncoderDeadline = VPX_DL_REALTIME;
    741746#endif
    742747            }
    743748            else if (value.compare("good", Utf8Str::CaseInsensitive) == 0)
    744749            {
    745                 pStream->uEncoderDeadline = 1000000 / uFPS;
     750                pStream->Video.uEncoderDeadline = 1000000 / uFPS;
    746751            }
    747752            else if (value.compare("best", Utf8Str::CaseInsensitive) == 0)
    748753            {
    749754#ifdef VBOX_WITH_LIBVPX
    750                 pStream->uEncoderDeadline = VPX_DL_BEST_QUALITY;
     755                pStream->Video.uEncoderDeadline = VPX_DL_BEST_QUALITY;
    751756#endif
    752757            }
     
    754759            {
    755760                LogRel(("VideoRec: Setting quality deadline to '%s'\n", value.c_str()));
    756                 pStream->uEncoderDeadline = value.toUInt32();
     761                pStream->Video.uEncoderDeadline = value.toUInt32();
    757762            }
    758763        }
     
    796801    }
    797802
    798     pStream->uDelay = 1000 / uFPS;
     803    pStream->Video.uDelay = 1000 / uFPS;
    799804
    800805    if (fHasVideoTrack)
     
    852857    }
    853858
    854     pStream->pu8YuvBuf = pStream->Codec.VPX.RawImage.planes[0];
     859    pStream->Video.pu8YuvBuf = pStream->Codec.VPX.RawImage.planes[0];
    855860#endif
    856861
     
    899904    }
    900905
    901     if (u64TimeStampMs < pStream->uLastTimeStampMs + pStream->uDelay)
     906    if (u64TimeStampMs < pStream->uLastTimeStampMs + pStream->Video.uDelay)
    902907        return false;
    903908
    904     if (ASMAtomicReadBool(&pStream->fRgbFilled))
     909    if (ASMAtomicReadBool(&pStream->Video.fRgbFilled))
    905910        return false;
    906911
     
    961966
    962967#ifdef VBOX_WITH_LIBVPX
    963     /* presentation time stamp */
     968    /* Presentation Time Stamp (PTS). */
    964969    vpx_codec_pts_t pts = pStream->uCurTimeStampMs;
    965970    vpx_codec_err_t rcv = vpx_codec_encode(&pStream->Codec.VPX.CodecCtx,
    966971                                           &pStream->Codec.VPX.RawImage,
    967                                            pts /* time stamp */,
    968                                            pStream->uDelay  /* how long to show this frame */,
    969                                            0   /* flags */,
    970                                            pStream->uEncoderDeadline /* quality setting */);
     972                                           pts                           /* Time stamp */,
     973                                           pStream->Video.uDelay               /* How long to show this frame */,
     974                                           0                             /* Flags */,
     975                                           pStream->Video.uEncoderDeadline     /* Quality setting */);
    971976    if (rcv != VPX_CODEC_OK)
    972977    {
    973         LogFlow(("Failed to encode:%s\n", vpx_codec_err_to_string(rcv)));
     978        LogFunc(("Failed to encode video frame: %s\n", vpx_codec_err_to_string(rcv)));
    974979        return VERR_GENERAL_FAILURE;
    975980    }
     
    994999            default:
    9951000                AssertFailed();
    996                 LogFunc(("Unexpected CODEC packet kind %ld\n", pPacket->kind));
     1001                LogFunc(("Unexpected video packet type %ld\n", pPacket->kind));
    9971002                break;
    9981003        }
    9991004    }
    10001005
    1001     pStream->cFrame++;
     1006    pStream->Video.cFrame++;
    10021007#else
    10031008    RT_NOREF(pStream);
     
    10131018 * @param   pStrm      Strm.
    10141019 */
    1015 static int videoRecRGBToYUV(PVIDEORECSTREAM pStrm)
    1016 {
    1017     switch (pStrm->u32PixelFormat)
     1020static int videoRecRGBToYUV(PVIDEORECSTREAM pStream)
     1021{
     1022    switch (pStream->Video.uPixelFormat)
    10181023    {
    10191024        case VIDEORECPIXELFMT_RGB32:
    10201025            LogFlow(("32 bit\n"));
    1021             if (!colorConvWriteYUV420p<ColorConvBGRA32Iter>(pStrm->uDstWidth,
    1022                                                             pStrm->uDstHeight,
    1023                                                             pStrm->pu8YuvBuf,
    1024                                                             pStrm->pu8RgbBuf))
     1026            if (!colorConvWriteYUV420p<ColorConvBGRA32Iter>(pStream->Video.uDstWidth,
     1027                                                            pStream->Video.uDstHeight,
     1028                                                            pStream->Video.pu8YuvBuf,
     1029                                                            pStream->Video.pu8RgbBuf))
    10251030                return VERR_INVALID_PARAMETER;
    10261031            break;
    10271032        case VIDEORECPIXELFMT_RGB24:
    10281033            LogFlow(("24 bit\n"));
    1029             if (!colorConvWriteYUV420p<ColorConvBGR24Iter>(pStrm->uDstWidth,
    1030                                                            pStrm->uDstHeight,
    1031                                                            pStrm->pu8YuvBuf,
    1032                                                            pStrm->pu8RgbBuf))
     1034            if (!colorConvWriteYUV420p<ColorConvBGR24Iter>(pStream->Video.uDstWidth,
     1035                                                           pStream->Video.uDstHeight,
     1036                                                           pStream->Video.pu8YuvBuf,
     1037                                                           pStream->Video.pu8RgbBuf))
    10331038                return VERR_INVALID_PARAMETER;
    10341039            break;
    10351040        case VIDEORECPIXELFMT_RGB565:
    10361041            LogFlow(("565 bit\n"));
    1037             if (!colorConvWriteYUV420p<ColorConvBGR565Iter>(pStrm->uDstWidth,
    1038                                                             pStrm->uDstHeight,
    1039                                                             pStrm->pu8YuvBuf,
    1040                                                             pStrm->pu8RgbBuf))
     1042            if (!colorConvWriteYUV420p<ColorConvBGR565Iter>(pStream->Video.uDstWidth,
     1043                                                            pStream->Video.uDstHeight,
     1044                                                            pStream->Video.pu8YuvBuf,
     1045                                                            pStream->Video.pu8RgbBuf))
    10411046                return VERR_INVALID_PARAMETER;
    10421047            break;
     
    10861091                           uint64_t uTimeStampMs)
    10871092{
    1088     /* Do not execute during termination and guard against termination */
     1093    /* Do not execute during termination and guard against termination. */
    10891094    if (!ASMAtomicCmpXchgU32(&g_enmState, VIDEORECSTS_BUSY, VIDEORECSTS_IDLE))
    10901095        return VINF_TRY_AGAIN;
     
    11101115            break;
    11111116        }
    1112         if (uTimeStampMs < pStream->uLastTimeStampMs + pStream->uDelay)
     1117        if (uTimeStampMs < pStream->uLastTimeStampMs + pStream->Video.uDelay)
    11131118        {
    11141119            rc = VINF_TRY_AGAIN; /* respect maximum frames per second */
    11151120            break;
    11161121        }
    1117         if (ASMAtomicReadBool(&pStream->fRgbFilled))
     1122        if (ASMAtomicReadBool(&pStream->Video.fRgbFilled))
    11181123        {
    11191124            rc = VERR_TRY_AGAIN; /* previous frame not yet encoded */
     
    11231128        pStream->uLastTimeStampMs = uTimeStampMs;
    11241129
    1125         int xDiff = ((int)pStream->uDstWidth - (int)uSrcWidth) / 2;
     1130        int xDiff = ((int)pStream->Video.uDstWidth - (int)uSrcWidth) / 2;
    11261131        uint32_t w = uSrcWidth;
    11271132        if ((int)w + xDiff + (int)x <= 0)  /* nothing visible */
     
    11421147
    11431148        uint32_t h = uSrcHeight;
    1144         int yDiff = ((int)pStream->uDstHeight - (int)uSrcHeight) / 2;
     1149        int yDiff = ((int)pStream->Video.uDstHeight - (int)uSrcHeight) / 2;
    11451150        if ((int)h + yDiff + (int)y <= 0)  /* nothing visible */
    11461151        {
     
    11591164            destY = y + yDiff;
    11601165
    1161         if (   destX > pStream->uDstWidth
    1162             || destY > pStream->uDstHeight)
     1166        if (   destX > pStream->Video.uDstWidth
     1167            || destY > pStream->Video.uDstHeight)
    11631168        {
    11641169            rc = VERR_INVALID_PARAMETER;  /* nothing visible */
     
    11661171        }
    11671172
    1168         if (destX + w > pStream->uDstWidth)
    1169             w = pStream->uDstWidth - destX;
    1170 
    1171         if (destY + h > pStream->uDstHeight)
    1172             h = pStream->uDstHeight - destY;
    1173 
    1174         /* Calculate bytes per pixel */
     1173        if (destX + w > pStream->Video.uDstWidth)
     1174            w = pStream->Video.uDstWidth - destX;
     1175
     1176        if (destY + h > pStream->Video.uDstHeight)
     1177            h = pStream->Video.uDstHeight - destY;
     1178
     1179        /* Calculate bytes per pixel. */
    11751180        uint32_t bpp = 1;
    11761181        if (uPixelFormat == BitmapFormat_BGR)
     
    11791184            {
    11801185                case 32:
    1181                     pStream->u32PixelFormat = VIDEORECPIXELFMT_RGB32;
     1186                    pStream->Video.uPixelFormat = VIDEORECPIXELFMT_RGB32;
    11821187                    bpp = 4;
    11831188                    break;
    11841189                case 24:
    1185                     pStream->u32PixelFormat = VIDEORECPIXELFMT_RGB24;
     1190                    pStream->Video.uPixelFormat = VIDEORECPIXELFMT_RGB24;
    11861191                    bpp = 3;
    11871192                    break;
    11881193                case 16:
    1189                     pStream->u32PixelFormat = VIDEORECPIXELFMT_RGB565;
     1194                    pStream->Video.uPixelFormat = VIDEORECPIXELFMT_RGB565;
    11901195                    bpp = 2;
    11911196                    break;
     
    11961201        }
    11971202        else
    1198             AssertMsgFailed(("Unknown pixel format! mPixelFormat=%d\n", uPixelFormat));
     1203            AssertMsgFailed(("Unknown pixel format! mPixelFormat=%d\n", pStream->Video.uPixelFormat));
    11991204
    12001205        /* One of the dimensions of the current frame is smaller than before so
    1201          * clear the entire buffer to prevent artifacts from the previous frame */
    1202         if (   uSrcWidth  < pStream->uSrcLastWidth
    1203             || uSrcHeight < pStream->uSrcLastHeight)
    1204             memset(pStream->pu8RgbBuf, 0, pStream->uDstWidth * pStream->uDstHeight * 4);
    1205 
    1206         pStream->uSrcLastWidth  = uSrcWidth;
    1207         pStream->uSrcLastHeight = uSrcHeight;
    1208 
    1209         /* Calculate start offset in source and destination buffers */
     1206         * clear the entire buffer to prevent artifacts from the previous frame. */
     1207        if (   uSrcWidth  < pStream->Video.uSrcLastWidth
     1208            || uSrcHeight < pStream->Video.uSrcLastHeight)
     1209            memset(pStream->Video.pu8RgbBuf, 0, pStream->Video.uDstWidth * pStream->Video.uDstHeight * 4);
     1210
     1211        pStream->Video.uSrcLastWidth  = uSrcWidth;
     1212        pStream->Video.uSrcLastHeight = uSrcHeight;
     1213
     1214        /* Calculate start offset in source and destination buffers. */
    12101215        uint32_t offSrc = y * uBytesPerLine + x * bpp;
    1211         uint32_t offDst = (destY * pStream->uDstWidth + destX) * bpp;
    1212         /* do the copy */
     1216        uint32_t offDst = (destY * pStream->Video.uDstWidth + destX) * bpp;
     1217
     1218        /* Do the copy. */
    12131219        for (unsigned int i = 0; i < h; i++)
    12141220        {
    12151221            /* Overflow check */
    12161222            Assert(offSrc + w * bpp <= uSrcHeight * uBytesPerLine);
    1217             Assert(offDst + w * bpp <= pStream->uDstHeight * pStream->uDstWidth * bpp);
    1218             memcpy(pStream->pu8RgbBuf + offDst, puSrcData + offSrc, w * bpp);
     1223            Assert(offDst + w * bpp <= pStream->Video.uDstHeight * pStream->Video.uDstWidth * bpp);
     1224            memcpy(pStream->Video.pu8RgbBuf + offDst, puSrcData + offSrc, w * bpp);
    12191225            offSrc += uBytesPerLine;
    1220             offDst += pStream->uDstWidth * bpp;
     1226            offDst += pStream->Video.uDstWidth * bpp;
    12211227        }
    12221228
    12231229        pStream->uCurTimeStampMs = uTimeStampMs;
    12241230
    1225         ASMAtomicWriteBool(&pStream->fRgbFilled, true);
     1231        ASMAtomicWriteBool(&pStream->Video.fRgbFilled, true);
    12261232        RTSemEventSignal(pCtx->WaitEvent);
     1233
    12271234    } while (0);
    12281235
Note: See TracChangeset for help on using the changeset viewer.

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