2014-07-02 07:28:09 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* H.264 Bitmap Compression
|
|
|
|
*
|
|
|
|
* Copyright 2014 Mike McDonald <Mike.McDonald@software.dell.com>
|
2017-06-14 17:04:54 +03:00
|
|
|
* Copyright 2017 David Fort <contact@hardening-consulting.com>
|
2014-07-02 07:28:09 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2014-07-02 07:28:09 +04:00
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/print.h>
|
2016-03-29 23:03:15 +03:00
|
|
|
#include <winpr/library.h>
|
2014-07-02 07:28:09 +04:00
|
|
|
#include <winpr/bitstream.h>
|
2017-06-14 17:04:54 +03:00
|
|
|
#include <winpr/synch.h>
|
2014-07-02 07:28:09 +04:00
|
|
|
|
2014-09-07 01:10:27 +04:00
|
|
|
#include <freerdp/primitives.h>
|
2014-07-02 07:28:09 +04:00
|
|
|
#include <freerdp/codec/h264.h>
|
2020-10-22 19:11:21 +03:00
|
|
|
#include <freerdp/codec/yuv.h>
|
2014-09-12 16:36:29 +04:00
|
|
|
#include <freerdp/log.h>
|
|
|
|
|
2018-01-31 17:08:11 +03:00
|
|
|
#include "h264.h"
|
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#define TAG FREERDP_TAG("codec")
|
2014-07-02 07:28:09 +04:00
|
|
|
|
2018-01-31 17:08:11 +03:00
|
|
|
static BOOL avc444_ensure_buffer(H264_CONTEXT* h264, DWORD nDstHeight);
|
|
|
|
|
2017-06-14 17:14:19 +03:00
|
|
|
BOOL avc420_ensure_buffer(H264_CONTEXT* h264, UINT32 stride, UINT32 width, UINT32 height)
|
2017-04-20 13:02:04 +03:00
|
|
|
{
|
2020-11-20 14:37:21 +03:00
|
|
|
size_t x;
|
|
|
|
BOOL isNull = FALSE;
|
2021-10-19 14:42:46 +03:00
|
|
|
UINT32 pheight = height;
|
2020-11-20 14:37:21 +03:00
|
|
|
|
2017-04-20 13:02:04 +03:00
|
|
|
if (!h264)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (stride == 0)
|
|
|
|
stride = width;
|
|
|
|
|
2018-02-02 10:06:32 +03:00
|
|
|
if (stride % 16 != 0)
|
|
|
|
stride += 16 - stride % 16;
|
|
|
|
|
2021-10-19 14:42:46 +03:00
|
|
|
if (pheight % 16 != 0)
|
|
|
|
pheight += 16 - pheight % 16;
|
2018-02-02 10:06:32 +03:00
|
|
|
|
2020-11-20 14:37:21 +03:00
|
|
|
for (x = 0; x < 3; x++)
|
|
|
|
{
|
|
|
|
if (!h264->pYUVData[x] || !h264->pOldYUVData[x])
|
|
|
|
isNull = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isNull || (width != h264->width) || (height != h264->height) ||
|
|
|
|
(stride != h264->iStride[0]))
|
2017-04-20 13:02:04 +03:00
|
|
|
{
|
|
|
|
h264->iStride[0] = stride;
|
|
|
|
h264->iStride[1] = (stride + 1) / 2;
|
|
|
|
h264->iStride[2] = (stride + 1) / 2;
|
|
|
|
h264->width = width;
|
|
|
|
h264->height = height;
|
2020-11-20 14:37:21 +03:00
|
|
|
|
|
|
|
for (x = 0; x < 3; x++)
|
|
|
|
{
|
2021-10-19 14:42:46 +03:00
|
|
|
BYTE* tmp1 = _aligned_recalloc(h264->pYUVData[x], h264->iStride[x], pheight, 16);
|
|
|
|
BYTE* tmp2 = _aligned_recalloc(h264->pOldYUVData[x], h264->iStride[x], pheight, 16);
|
2020-11-20 14:37:21 +03:00
|
|
|
if (tmp1)
|
|
|
|
h264->pYUVData[x] = tmp1;
|
|
|
|
if (tmp2)
|
|
|
|
h264->pOldYUVData[x] = tmp2;
|
|
|
|
if (!tmp1 || !tmp2)
|
|
|
|
return FALSE;
|
|
|
|
}
|
2017-04-20 13:02:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
INT32 avc420_decompress(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize, BYTE* pDstData,
|
|
|
|
DWORD DstFormat, UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight,
|
2020-10-22 19:11:21 +03:00
|
|
|
const RECTANGLE_16* regionRects, UINT32 numRegionRects)
|
2016-03-02 17:16:49 +03:00
|
|
|
{
|
|
|
|
int status;
|
2020-10-22 19:11:21 +03:00
|
|
|
const BYTE* pYUVData[3];
|
2016-03-02 17:16:49 +03:00
|
|
|
|
2020-11-20 14:37:21 +03:00
|
|
|
if (!h264 || h264->Compressor)
|
2016-03-02 17:16:49 +03:00
|
|
|
return -1001;
|
|
|
|
|
2017-02-15 11:28:10 +03:00
|
|
|
status = h264->subsystem->Decompress(h264, pSrcData, SrcSize);
|
2018-08-23 14:04:42 +03:00
|
|
|
|
2016-03-02 17:16:49 +03:00
|
|
|
if (status == 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
return status;
|
|
|
|
|
2020-10-22 19:11:21 +03:00
|
|
|
pYUVData[0] = h264->pYUVData[0];
|
|
|
|
pYUVData[1] = h264->pYUVData[1];
|
|
|
|
pYUVData[2] = h264->pYUVData[2];
|
2021-10-20 18:56:41 +03:00
|
|
|
if (!yuv420_context_decode(h264->yuv, pYUVData, h264->iStride, h264->height, DstFormat,
|
|
|
|
pDstData, nDstStep, regionRects, numRegionRects))
|
2016-03-02 17:16:49 +03:00
|
|
|
return -1002;
|
|
|
|
|
2014-07-02 07:28:09 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-11-20 14:37:21 +03:00
|
|
|
static BOOL allocate_h264_metablock(UINT32 QP, RECTANGLE_16* rectangles,
|
|
|
|
RDPGFX_H264_METABLOCK* meta, size_t count)
|
|
|
|
{
|
|
|
|
size_t x;
|
2021-12-17 19:08:08 +03:00
|
|
|
|
|
|
|
/* [MS-RDPEGFX] 2.2.4.4.2 RDPGFX_AVC420_QUANT_QUALITY */
|
|
|
|
if (!meta || (QP > UINT8_MAX))
|
2020-11-20 14:37:21 +03:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
meta->regionRects = rectangles;
|
|
|
|
if (count == 0)
|
|
|
|
return TRUE;
|
|
|
|
|
2021-12-17 19:08:08 +03:00
|
|
|
if (count > UINT32_MAX)
|
|
|
|
return FALSE;
|
|
|
|
|
2020-11-20 14:37:21 +03:00
|
|
|
meta->quantQualityVals = calloc(count, sizeof(RDPGFX_H264_QUANT_QUALITY));
|
|
|
|
|
|
|
|
if (!meta->quantQualityVals || !meta->regionRects)
|
|
|
|
return FALSE;
|
2021-12-17 19:08:08 +03:00
|
|
|
meta->numRegionRects = (UINT32)count;
|
2020-11-20 14:37:21 +03:00
|
|
|
for (x = 0; x < count; x++)
|
|
|
|
{
|
|
|
|
RDPGFX_H264_QUANT_QUALITY* cur = &meta->quantQualityVals[x];
|
2021-12-17 19:08:08 +03:00
|
|
|
cur->qp = (UINT8)QP;
|
|
|
|
|
|
|
|
/* qpVal bit 6 and 7 are flags, so mask them out here.
|
|
|
|
* qualityVal is [0-100] so 100 - qpVal [0-64] is always in range */
|
|
|
|
cur->qualityVal = 100 - (QP & 0x3F);
|
2020-11-20 14:37:21 +03:00
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static INLINE BOOL diff_tile(const RECTANGLE_16* regionRect, BYTE* pYUVData[3],
|
|
|
|
BYTE* pOldYUVData[3], UINT32 const iStride[3])
|
|
|
|
{
|
|
|
|
size_t size, y;
|
|
|
|
if (!regionRect || !pYUVData || !pOldYUVData || !iStride)
|
|
|
|
return FALSE;
|
|
|
|
size = regionRect->right - regionRect->left;
|
|
|
|
if (regionRect->right > iStride[0])
|
|
|
|
return FALSE;
|
|
|
|
if (regionRect->right / 2 > iStride[1])
|
|
|
|
return FALSE;
|
|
|
|
if (regionRect->right / 2 > iStride[2])
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
for (y = regionRect->top; y < regionRect->bottom; y++)
|
|
|
|
{
|
|
|
|
const BYTE* cur0 = &pYUVData[0][y * iStride[0]];
|
|
|
|
const BYTE* cur1 = &pYUVData[1][y * iStride[1]];
|
|
|
|
const BYTE* cur2 = &pYUVData[2][y * iStride[2]];
|
|
|
|
const BYTE* old0 = &pOldYUVData[0][y * iStride[0]];
|
|
|
|
const BYTE* old1 = &pOldYUVData[1][y * iStride[1]];
|
|
|
|
const BYTE* old2 = &pOldYUVData[2][y * iStride[2]];
|
|
|
|
|
|
|
|
if (memcmp(&cur0[regionRect->left], &old0[regionRect->left], size) != 0)
|
|
|
|
return TRUE;
|
|
|
|
if (memcmp(&cur1[regionRect->left / 2], &old1[regionRect->left / 2], size / 2) != 0)
|
|
|
|
return TRUE;
|
|
|
|
if (memcmp(&cur2[regionRect->left / 2], &old2[regionRect->left / 2], size / 2) != 0)
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL detect_changes(BOOL firstFrameDone, const UINT32 QP, const RECTANGLE_16* regionRect,
|
|
|
|
BYTE* pYUVData[3], BYTE* pOldYUVData[3], UINT32 const iStride[3],
|
|
|
|
RDPGFX_H264_METABLOCK* meta)
|
|
|
|
{
|
|
|
|
size_t count = 0, wc, hc;
|
|
|
|
RECTANGLE_16* rectangles;
|
|
|
|
|
|
|
|
if (!regionRect || !pYUVData || !pOldYUVData || !iStride || !meta)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
wc = (regionRect->right - regionRect->left) / 64 + 1;
|
|
|
|
hc = (regionRect->bottom - regionRect->top) / 64 + 1;
|
|
|
|
rectangles = calloc(wc * hc, sizeof(RECTANGLE_16));
|
|
|
|
if (!rectangles)
|
|
|
|
return FALSE;
|
|
|
|
if (!firstFrameDone)
|
|
|
|
{
|
|
|
|
rectangles[0] = *regionRect;
|
|
|
|
count = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t x, y;
|
|
|
|
for (y = regionRect->top; y < regionRect->bottom; y += 64)
|
|
|
|
{
|
|
|
|
for (x = regionRect->left; x < regionRect->right; x += 64)
|
|
|
|
{
|
|
|
|
RECTANGLE_16 rect;
|
2021-12-17 19:08:08 +03:00
|
|
|
rect.left = (UINT16)MIN(UINT16_MAX, regionRect->left + x);
|
|
|
|
rect.top = (UINT16)MIN(UINT16_MAX, regionRect->top + y);
|
|
|
|
rect.right =
|
|
|
|
(UINT16)MIN(UINT16_MAX, MIN(regionRect->left + x + 64, regionRect->right));
|
|
|
|
rect.bottom =
|
|
|
|
(UINT16)MIN(UINT16_MAX, MIN(regionRect->top + y + 64, regionRect->bottom));
|
2020-11-20 14:37:21 +03:00
|
|
|
if (diff_tile(&rect, pYUVData, pOldYUVData, iStride))
|
|
|
|
rectangles[count++] = rect;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!allocate_h264_metablock(QP, rectangles, meta, count))
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
INT32 avc420_compress(H264_CONTEXT* h264, const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep,
|
2020-11-20 14:37:21 +03:00
|
|
|
UINT32 nSrcWidth, UINT32 nSrcHeight, const RECTANGLE_16* regionRect,
|
|
|
|
BYTE** ppDstData, UINT32* pDstSize, RDPGFX_H264_METABLOCK* meta)
|
2014-07-02 07:28:09 +04:00
|
|
|
{
|
2020-11-20 14:37:21 +03:00
|
|
|
size_t x;
|
2022-04-28 09:00:39 +03:00
|
|
|
INT32 rc = -1;
|
|
|
|
BYTE* pYUVData[3] = { 0 };
|
|
|
|
const BYTE* pcYUVData[3] = { 0 };
|
|
|
|
BYTE* pOldYUVData[3] = { 0 };
|
2015-02-16 12:51:20 +03:00
|
|
|
|
2020-11-20 14:37:21 +03:00
|
|
|
if (!h264 || !regionRect || !meta || !h264->Compressor)
|
2015-02-16 12:51:20 +03:00
|
|
|
return -1;
|
2016-03-02 17:16:49 +03:00
|
|
|
|
2015-02-16 12:51:20 +03:00
|
|
|
if (!h264->subsystem->Compress)
|
|
|
|
return -1;
|
|
|
|
|
2017-04-20 13:02:04 +03:00
|
|
|
if (!avc420_ensure_buffer(h264, nSrcStep, nSrcWidth, nSrcHeight))
|
2015-06-16 16:42:07 +03:00
|
|
|
return -1;
|
2016-08-03 16:29:12 +03:00
|
|
|
|
2020-11-20 14:37:21 +03:00
|
|
|
if (h264->encodingBuffer)
|
|
|
|
{
|
|
|
|
for (x = 0; x < 3; x++)
|
|
|
|
{
|
|
|
|
pYUVData[x] = h264->pYUVData[x];
|
|
|
|
pOldYUVData[x] = h264->pOldYUVData[x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (x = 0; x < 3; x++)
|
|
|
|
{
|
|
|
|
pYUVData[x] = h264->pOldYUVData[x];
|
|
|
|
pOldYUVData[x] = h264->pYUVData[x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h264->encodingBuffer = !h264->encodingBuffer;
|
|
|
|
|
|
|
|
if (!yuv420_context_encode(h264->yuv, pSrcData, nSrcStep, SrcFormat, h264->iStride, pYUVData,
|
|
|
|
regionRect, 1))
|
2022-04-28 09:00:39 +03:00
|
|
|
goto fail;
|
2018-01-31 17:08:11 +03:00
|
|
|
|
2020-11-20 14:37:21 +03:00
|
|
|
if (!detect_changes(h264->firstLumaFrameDone, h264->QP, regionRect, pYUVData, pOldYUVData,
|
|
|
|
h264->iStride, meta))
|
2022-04-28 09:00:39 +03:00
|
|
|
goto fail;
|
2017-04-20 13:02:04 +03:00
|
|
|
|
2020-11-20 14:37:21 +03:00
|
|
|
if (meta->numRegionRects == 0)
|
2022-04-28 09:00:39 +03:00
|
|
|
{
|
|
|
|
rc = 0;
|
|
|
|
goto fail;
|
|
|
|
}
|
2020-11-20 14:37:21 +03:00
|
|
|
|
|
|
|
for (x = 0; x < 3; x++)
|
|
|
|
pcYUVData[x] = pYUVData[x];
|
|
|
|
|
|
|
|
rc = h264->subsystem->Compress(h264, pcYUVData, h264->iStride, ppDstData, pDstSize);
|
|
|
|
if (rc >= 0)
|
|
|
|
h264->firstLumaFrameDone = TRUE;
|
2022-04-28 09:00:39 +03:00
|
|
|
|
|
|
|
fail:
|
|
|
|
if (rc < 0)
|
|
|
|
free_h264_metablock(meta);
|
2020-11-20 14:37:21 +03:00
|
|
|
return rc;
|
2016-03-02 17:16:49 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
INT32 avc444_compress(H264_CONTEXT* h264, const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep,
|
2020-11-20 14:37:21 +03:00
|
|
|
UINT32 nSrcWidth, UINT32 nSrcHeight, BYTE version, const RECTANGLE_16* region,
|
|
|
|
BYTE* op, BYTE** ppDstData, UINT32* pDstSize, BYTE** ppAuxDstData,
|
|
|
|
UINT32* pAuxDstSize, RDPGFX_H264_METABLOCK* meta,
|
|
|
|
RDPGFX_H264_METABLOCK* auxMeta)
|
2016-03-02 17:16:49 +03:00
|
|
|
{
|
2022-04-28 09:00:39 +03:00
|
|
|
int rc = -1;
|
2018-01-31 17:08:11 +03:00
|
|
|
BYTE* coded;
|
|
|
|
UINT32 codedSize;
|
2020-11-20 14:37:21 +03:00
|
|
|
BYTE** pYUV444Data;
|
|
|
|
BYTE** pOldYUV444Data;
|
|
|
|
BYTE** pYUVData;
|
|
|
|
BYTE** pOldYUVData;
|
2018-01-31 17:08:11 +03:00
|
|
|
|
2020-11-20 14:37:21 +03:00
|
|
|
if (!h264 || !h264->Compressor)
|
2018-01-31 17:08:11 +03:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!h264->subsystem->Compress)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!avc420_ensure_buffer(h264, nSrcStep, nSrcWidth, nSrcHeight))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!avc444_ensure_buffer(h264, nSrcHeight))
|
|
|
|
return -1;
|
|
|
|
|
2020-11-20 14:37:21 +03:00
|
|
|
if (h264->encodingBuffer)
|
|
|
|
{
|
|
|
|
pYUV444Data = h264->pOldYUV444Data;
|
|
|
|
pOldYUV444Data = h264->pYUV444Data;
|
|
|
|
pYUVData = h264->pOldYUVData;
|
|
|
|
pOldYUVData = h264->pYUVData;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pYUV444Data = h264->pYUV444Data;
|
|
|
|
pOldYUV444Data = h264->pOldYUV444Data;
|
|
|
|
pYUVData = h264->pYUVData;
|
|
|
|
pOldYUVData = h264->pOldYUVData;
|
|
|
|
}
|
|
|
|
h264->encodingBuffer = !h264->encodingBuffer;
|
2018-01-31 17:08:11 +03:00
|
|
|
|
2020-10-22 19:11:21 +03:00
|
|
|
if (!yuv444_context_encode(h264->yuv, version, pSrcData, nSrcStep, SrcFormat, h264->iStride,
|
2020-11-20 14:37:21 +03:00
|
|
|
pYUV444Data, pYUVData, region, 1))
|
2022-04-28 09:00:39 +03:00
|
|
|
goto fail;
|
2020-11-20 14:37:21 +03:00
|
|
|
|
|
|
|
if (!detect_changes(h264->firstLumaFrameDone, h264->QP, region, pYUV444Data, pOldYUV444Data,
|
|
|
|
h264->iStride, meta))
|
2022-04-28 09:00:39 +03:00
|
|
|
goto fail;
|
2020-11-20 14:37:21 +03:00
|
|
|
if (!detect_changes(h264->firstChromaFrameDone, h264->QP, region, pYUVData, pOldYUVData,
|
|
|
|
h264->iStride, auxMeta))
|
2022-04-28 09:00:39 +03:00
|
|
|
goto fail;
|
2020-11-20 14:37:21 +03:00
|
|
|
|
|
|
|
/* [MS-RDPEGFX] 2.2.4.5 RFX_AVC444_BITMAP_STREAM
|
|
|
|
* LC:
|
|
|
|
* 0 ... Luma & Chroma
|
|
|
|
* 1 ... Luma
|
|
|
|
* 2 ... Chroma
|
|
|
|
*/
|
|
|
|
if ((meta->numRegionRects > 0) && (auxMeta->numRegionRects > 0))
|
|
|
|
*op = 0;
|
|
|
|
else if (meta->numRegionRects > 0)
|
|
|
|
*op = 1;
|
|
|
|
else if (auxMeta->numRegionRects > 0)
|
|
|
|
*op = 2;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WLog_INFO(TAG, "no changes detected for luma or chroma frame");
|
2022-04-28 09:00:39 +03:00
|
|
|
rc = 0;
|
|
|
|
goto fail;
|
2020-11-20 14:37:21 +03:00
|
|
|
}
|
2018-01-31 17:08:11 +03:00
|
|
|
|
2020-11-20 14:37:21 +03:00
|
|
|
if ((*op == 0) || (*op == 1))
|
2018-08-23 14:04:42 +03:00
|
|
|
{
|
2020-11-20 14:37:21 +03:00
|
|
|
const BYTE* pcYUV444Data[3] = { pYUV444Data[0], pYUV444Data[1], pYUV444Data[2] };
|
2018-08-23 14:04:42 +03:00
|
|
|
|
2020-11-20 14:37:21 +03:00
|
|
|
if (h264->subsystem->Compress(h264, pcYUV444Data, h264->iStride, &coded, &codedSize) < 0)
|
2022-04-28 09:00:39 +03:00
|
|
|
goto fail;
|
2020-11-20 14:37:21 +03:00
|
|
|
h264->firstLumaFrameDone = TRUE;
|
|
|
|
memcpy(h264->lumaData, coded, codedSize);
|
|
|
|
*ppDstData = h264->lumaData;
|
|
|
|
*pDstSize = codedSize;
|
2018-08-23 14:04:42 +03:00
|
|
|
}
|
2018-01-31 17:08:11 +03:00
|
|
|
|
2020-11-20 14:37:21 +03:00
|
|
|
if ((*op == 0) || (*op == 2))
|
2018-08-23 14:04:42 +03:00
|
|
|
{
|
2020-11-20 14:37:21 +03:00
|
|
|
const BYTE* pcYUVData[3] = { pYUVData[0], pYUVData[1], pYUVData[2] };
|
2018-01-31 17:08:11 +03:00
|
|
|
|
2020-11-20 14:37:21 +03:00
|
|
|
if (h264->subsystem->Compress(h264, pcYUVData, h264->iStride, &coded, &codedSize) < 0)
|
2022-04-28 09:00:39 +03:00
|
|
|
goto fail;
|
2020-11-20 14:37:21 +03:00
|
|
|
h264->firstChromaFrameDone = TRUE;
|
|
|
|
*ppAuxDstData = coded;
|
|
|
|
*pAuxDstSize = codedSize;
|
2018-08-23 14:04:42 +03:00
|
|
|
}
|
2020-11-20 14:37:21 +03:00
|
|
|
|
2022-04-28 09:00:39 +03:00
|
|
|
rc = 1;
|
|
|
|
fail:
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
free_h264_metablock(meta);
|
|
|
|
free_h264_metablock(auxMeta);
|
|
|
|
}
|
|
|
|
return rc;
|
2016-03-02 17:16:49 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL avc444_ensure_buffer(H264_CONTEXT* h264, DWORD nDstHeight)
|
2016-03-02 17:16:49 +03:00
|
|
|
{
|
|
|
|
UINT32 x;
|
2017-02-15 11:28:10 +03:00
|
|
|
const UINT32* piMainStride = h264->iStride;
|
2016-03-02 17:16:49 +03:00
|
|
|
UINT32* piDstSize = h264->iYUV444Size;
|
|
|
|
UINT32* piDstStride = h264->iYUV444Stride;
|
|
|
|
BYTE** ppYUVDstData = h264->pYUV444Data;
|
2020-11-20 14:37:21 +03:00
|
|
|
BYTE** ppOldYUVDstData = h264->pOldYUV444Data;
|
2019-05-08 16:35:07 +03:00
|
|
|
const UINT32 pad = nDstHeight % 16;
|
|
|
|
UINT32 padDstHeight = nDstHeight; /* Need alignment to 16x16 blocks */
|
|
|
|
|
|
|
|
if (pad != 0)
|
|
|
|
padDstHeight += 16 - pad;
|
2016-03-02 17:16:49 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if ((piMainStride[0] != piDstStride[0]) || (piDstSize[0] != piMainStride[0] * padDstHeight))
|
2016-03-02 17:16:49 +03:00
|
|
|
{
|
2016-08-03 16:29:12 +03:00
|
|
|
for (x = 0; x < 3; x++)
|
2016-03-02 17:16:49 +03:00
|
|
|
{
|
2020-11-20 14:37:21 +03:00
|
|
|
BYTE* tmp1;
|
|
|
|
BYTE* tmp2;
|
2016-03-02 17:16:49 +03:00
|
|
|
piDstStride[x] = piMainStride[0];
|
|
|
|
piDstSize[x] = piDstStride[x] * padDstHeight;
|
2020-11-20 14:37:21 +03:00
|
|
|
tmp1 = _aligned_recalloc(ppYUVDstData[x], piDstSize[x], 1, 16);
|
|
|
|
if (tmp1)
|
|
|
|
ppYUVDstData[x] = tmp1;
|
|
|
|
tmp2 = _aligned_recalloc(ppOldYUVDstData[x], piDstSize[x], 1, 16);
|
|
|
|
if (tmp2)
|
|
|
|
ppOldYUVDstData[x] = tmp2;
|
|
|
|
if (!tmp1 || !tmp2)
|
2016-03-02 17:16:49 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
2018-01-31 17:08:11 +03:00
|
|
|
|
2020-11-20 14:37:21 +03:00
|
|
|
{
|
|
|
|
BYTE* tmp = _aligned_recalloc(h264->lumaData, piDstSize[0], 4, 16);
|
|
|
|
if (!tmp)
|
|
|
|
goto fail;
|
|
|
|
h264->lumaData = tmp;
|
|
|
|
}
|
2016-03-02 17:16:49 +03:00
|
|
|
}
|
|
|
|
|
2016-08-03 16:29:12 +03:00
|
|
|
for (x = 0; x < 3; x++)
|
2016-03-02 17:16:49 +03:00
|
|
|
{
|
2020-11-20 14:37:21 +03:00
|
|
|
if (!ppOldYUVDstData[x] || !ppYUVDstData[x] || (piDstSize[x] == 0) || (piDstStride[x] == 0))
|
2016-03-02 17:16:49 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR,
|
|
|
|
"YUV buffer not initialized! check your decoder settings");
|
2016-03-02 17:16:49 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 10:06:32 +03:00
|
|
|
if (!h264->lumaData)
|
|
|
|
goto fail;
|
|
|
|
|
2016-03-02 17:16:49 +03:00
|
|
|
return TRUE;
|
|
|
|
fail:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL avc444_process_rects(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize,
|
|
|
|
BYTE* pDstData, UINT32 DstFormat, UINT32 nDstStep,
|
|
|
|
UINT32 nDstWidth, UINT32 nDstHeight, const RECTANGLE_16* rects,
|
|
|
|
UINT32 nrRects, avc444_frame_type type)
|
2017-02-15 11:28:10 +03:00
|
|
|
{
|
2020-10-22 19:11:21 +03:00
|
|
|
const BYTE* pYUVData[3];
|
|
|
|
BYTE* pYUVDstData[3];
|
2017-02-15 11:28:10 +03:00
|
|
|
UINT32* piDstStride = h264->iYUV444Stride;
|
|
|
|
BYTE** ppYUVDstData = h264->pYUV444Data;
|
|
|
|
const UINT32* piStride = h264->iStride;
|
|
|
|
|
|
|
|
if (h264->subsystem->Decompress(h264, pSrcData, SrcSize) < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
2020-10-22 19:11:21 +03:00
|
|
|
pYUVData[0] = h264->pYUVData[0];
|
|
|
|
pYUVData[1] = h264->pYUVData[1];
|
|
|
|
pYUVData[2] = h264->pYUVData[2];
|
2017-02-15 11:28:10 +03:00
|
|
|
if (!avc444_ensure_buffer(h264, nDstHeight))
|
|
|
|
return FALSE;
|
|
|
|
|
2020-10-22 19:11:21 +03:00
|
|
|
pYUVDstData[0] = ppYUVDstData[0];
|
|
|
|
pYUVDstData[1] = ppYUVDstData[1];
|
|
|
|
pYUVDstData[2] = ppYUVDstData[2];
|
2021-12-17 19:08:08 +03:00
|
|
|
if (!yuv444_context_decode(h264->yuv, (BYTE)type, pYUVData, piStride, h264->height, pYUVDstData,
|
2021-10-20 18:56:41 +03:00
|
|
|
piDstStride, DstFormat, pDstData, nDstStep, rects, nrRects))
|
2017-02-15 11:28:10 +03:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-03-02 17:16:49 +03:00
|
|
|
#if defined(AVC444_FRAME_STAT)
|
|
|
|
static UINT64 op1 = 0;
|
|
|
|
static double op1sum = 0;
|
|
|
|
static UINT64 op2 = 0;
|
|
|
|
static double op2sum = 0;
|
|
|
|
static UINT64 op3 = 0;
|
|
|
|
static double op3sum = 0;
|
|
|
|
static double avg(UINT64* count, double old, double size)
|
|
|
|
{
|
|
|
|
double tmp = size + *count * old;
|
|
|
|
(*count)++;
|
|
|
|
tmp = tmp / *count;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-10-22 19:11:21 +03:00
|
|
|
INT32 avc444_decompress(H264_CONTEXT* h264, BYTE op, const RECTANGLE_16* regionRects,
|
2019-11-06 17:24:51 +03:00
|
|
|
UINT32 numRegionRects, const BYTE* pSrcData, UINT32 SrcSize,
|
2020-10-22 19:11:21 +03:00
|
|
|
const RECTANGLE_16* auxRegionRects, UINT32 numAuxRegionRect,
|
2019-11-06 17:24:51 +03:00
|
|
|
const BYTE* pAuxSrcData, UINT32 AuxSrcSize, BYTE* pDstData, DWORD DstFormat,
|
|
|
|
UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight, UINT32 codecId)
|
2016-03-02 17:16:49 +03:00
|
|
|
{
|
|
|
|
INT32 status = -1;
|
2019-11-06 17:24:51 +03:00
|
|
|
avc444_frame_type chroma =
|
|
|
|
(codecId == RDPGFX_CODECID_AVC444) ? AVC444_CHROMAv1 : AVC444_CHROMAv2;
|
2016-03-02 17:16:49 +03:00
|
|
|
|
2020-11-20 14:37:21 +03:00
|
|
|
if (!h264 || !regionRects || !pSrcData || !pDstData || h264->Compressor)
|
2016-03-02 17:16:49 +03:00
|
|
|
return -1001;
|
|
|
|
|
2016-08-03 16:29:12 +03:00
|
|
|
switch (op)
|
2016-03-02 17:16:49 +03:00
|
|
|
{
|
2016-08-03 16:29:12 +03:00
|
|
|
case 0: /* YUV420 in stream 1
|
2019-11-06 17:24:51 +03:00
|
|
|
* Chroma420 in stream 2 */
|
|
|
|
if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
|
|
|
|
nDstWidth, nDstHeight, regionRects, numRegionRects,
|
|
|
|
AVC444_LUMA))
|
2017-02-15 11:28:10 +03:00
|
|
|
status = -1;
|
2019-11-06 17:24:51 +03:00
|
|
|
else if (!avc444_process_rects(h264, pAuxSrcData, AuxSrcSize, pDstData, DstFormat,
|
|
|
|
nDstStep, nDstWidth, nDstHeight, auxRegionRects,
|
|
|
|
numAuxRegionRect, chroma))
|
2017-02-15 11:28:10 +03:00
|
|
|
status = -1;
|
|
|
|
else
|
|
|
|
status = 0;
|
2016-08-03 16:29:12 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* Chroma420 in stream 1 */
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
|
|
|
|
nDstWidth, nDstHeight, regionRects, numRegionRects, chroma))
|
2017-02-15 11:28:10 +03:00
|
|
|
status = -1;
|
|
|
|
else
|
|
|
|
status = 0;
|
|
|
|
|
2016-08-03 16:29:12 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* YUV420 in stream 1 */
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep,
|
|
|
|
nDstWidth, nDstHeight, regionRects, numRegionRects,
|
|
|
|
AVC444_LUMA))
|
2017-02-15 11:28:10 +03:00
|
|
|
status = -1;
|
|
|
|
else
|
|
|
|
status = 0;
|
|
|
|
|
2016-08-03 16:29:12 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* WTF? */
|
|
|
|
break;
|
2016-03-02 17:16:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(AVC444_FRAME_STAT)
|
2016-08-03 16:29:12 +03:00
|
|
|
|
|
|
|
switch (op)
|
2016-03-02 17:16:49 +03:00
|
|
|
{
|
2016-08-03 16:29:12 +03:00
|
|
|
case 0:
|
|
|
|
op1sum = avg(&op1, op1sum, SrcSize + AuxSrcSize);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
op2sum = avg(&op2, op2sum, SrcSize);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
op3sum = avg(&op3, op3sum, SrcSize);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2016-03-02 17:16:49 +03:00
|
|
|
}
|
|
|
|
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_INFO,
|
2019-11-06 17:24:51 +03:00
|
|
|
"luma=%" PRIu64 " [avg=%lf] chroma=%" PRIu64 " [avg=%lf] combined=%" PRIu64
|
|
|
|
" [avg=%lf]",
|
2018-01-09 19:22:56 +03:00
|
|
|
op1, op1sum, op2, op2sum, op3, op3sum);
|
2016-03-02 17:16:49 +03:00
|
|
|
#endif
|
2015-02-16 12:51:20 +03:00
|
|
|
return status;
|
2014-07-02 07:28:09 +04:00
|
|
|
}
|
|
|
|
|
2017-06-14 17:04:54 +03:00
|
|
|
#define MAX_SUBSYSTEMS 10
|
|
|
|
static INIT_ONCE subsystems_once = INIT_ONCE_STATIC_INIT;
|
2022-04-28 13:58:22 +03:00
|
|
|
static const H264_CONTEXT_SUBSYSTEM* subSystems[MAX_SUBSYSTEMS] = { 0 };
|
2017-06-14 17:14:19 +03:00
|
|
|
|
2018-01-31 17:08:11 +03:00
|
|
|
static BOOL CALLBACK h264_register_subsystems(PINIT_ONCE once, PVOID param, PVOID* context)
|
|
|
|
{
|
2017-06-14 17:04:54 +03:00
|
|
|
int i = 0;
|
2022-01-26 11:53:26 +03:00
|
|
|
|
|
|
|
#ifdef WITH_MEDIACODEC
|
|
|
|
{
|
|
|
|
subSystems[i] = &g_Subsystem_mediacodec;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
#endif
|
2017-06-14 17:04:54 +03:00
|
|
|
#if defined(_WIN32) && defined(WITH_MEDIA_FOUNDATION)
|
2017-07-18 08:46:51 +03:00
|
|
|
{
|
|
|
|
subSystems[i] = &g_Subsystem_MF;
|
|
|
|
i++;
|
|
|
|
}
|
2016-08-03 16:29:12 +03:00
|
|
|
#endif
|
2017-07-18 08:46:51 +03:00
|
|
|
#ifdef WITH_OPENH264
|
|
|
|
{
|
|
|
|
subSystems[i] = &g_Subsystem_OpenH264;
|
|
|
|
i++;
|
|
|
|
}
|
2017-06-14 17:04:54 +03:00
|
|
|
#endif
|
2017-07-18 08:46:51 +03:00
|
|
|
#ifdef WITH_FFMPEG
|
|
|
|
{
|
|
|
|
subSystems[i] = &g_Subsystem_libavcodec;
|
|
|
|
i++;
|
|
|
|
}
|
2016-08-03 16:29:12 +03:00
|
|
|
#endif
|
2017-07-18 08:46:51 +03:00
|
|
|
return i > 0;
|
2017-06-14 17:04:54 +03:00
|
|
|
}
|
2014-07-03 20:03:39 +04:00
|
|
|
|
2019-11-20 13:30:14 +03:00
|
|
|
static BOOL h264_context_init(H264_CONTEXT* h264)
|
2017-06-14 17:04:54 +03:00
|
|
|
{
|
2017-06-14 17:14:19 +03:00
|
|
|
int i;
|
|
|
|
|
2017-07-17 11:37:15 +03:00
|
|
|
if (!h264)
|
|
|
|
return FALSE;
|
|
|
|
|
2018-01-09 19:22:56 +03:00
|
|
|
h264->log = WLog_Get(TAG);
|
|
|
|
|
|
|
|
if (!h264->log)
|
|
|
|
return FALSE;
|
|
|
|
|
2017-07-17 11:37:15 +03:00
|
|
|
h264->subsystem = NULL;
|
2017-06-14 17:04:54 +03:00
|
|
|
InitOnceExecuteOnce(&subsystems_once, h264_register_subsystems, NULL, NULL);
|
|
|
|
|
2017-06-14 17:14:19 +03:00
|
|
|
for (i = 0; i < MAX_SUBSYSTEMS; i++)
|
2015-07-28 23:01:34 +03:00
|
|
|
{
|
2022-04-28 13:58:22 +03:00
|
|
|
const H264_CONTEXT_SUBSYSTEM* subsystem = subSystems[i];
|
2018-01-31 17:08:11 +03:00
|
|
|
|
2017-10-17 20:28:54 +03:00
|
|
|
if (!subsystem || !subsystem->Init)
|
2017-07-17 11:37:15 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (subsystem->Init(h264))
|
2017-06-14 17:04:54 +03:00
|
|
|
{
|
2017-07-17 11:37:15 +03:00
|
|
|
h264->subsystem = subsystem;
|
2017-06-14 17:04:54 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
2015-07-28 23:01:34 +03:00
|
|
|
}
|
|
|
|
|
2017-06-14 17:04:54 +03:00
|
|
|
return FALSE;
|
2014-07-02 07:28:09 +04:00
|
|
|
}
|
|
|
|
|
2016-03-01 18:17:28 +03:00
|
|
|
BOOL h264_context_reset(H264_CONTEXT* h264, UINT32 width, UINT32 height)
|
2014-09-12 22:57:44 +04:00
|
|
|
{
|
2016-03-01 18:17:28 +03:00
|
|
|
if (!h264)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
h264->width = width;
|
|
|
|
h264->height = height;
|
2022-03-29 14:55:52 +03:00
|
|
|
return yuv_context_reset(h264->yuv, width, height);
|
2014-09-12 22:57:44 +04:00
|
|
|
}
|
|
|
|
|
2014-07-02 07:28:09 +04:00
|
|
|
H264_CONTEXT* h264_context_new(BOOL Compressor)
|
|
|
|
{
|
2020-10-22 19:11:21 +03:00
|
|
|
H264_CONTEXT* h264 = (H264_CONTEXT*)calloc(1, sizeof(H264_CONTEXT));
|
|
|
|
if (!h264)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
h264->Compressor = Compressor;
|
|
|
|
if (Compressor)
|
2014-07-02 07:28:09 +04:00
|
|
|
|
|
|
|
{
|
2020-10-22 19:11:21 +03:00
|
|
|
/* Default compressor settings, may be changed by caller */
|
|
|
|
h264->BitRate = 1000000;
|
|
|
|
h264->FrameRate = 30;
|
|
|
|
}
|
2014-07-02 07:28:09 +04:00
|
|
|
|
2020-10-22 19:11:21 +03:00
|
|
|
if (!h264_context_init(h264))
|
|
|
|
goto fail;
|
2015-04-21 08:59:01 +03:00
|
|
|
|
2021-03-12 18:54:45 +03:00
|
|
|
h264->yuv = yuv_context_new(Compressor, 0);
|
2020-10-22 19:11:21 +03:00
|
|
|
if (!h264->yuv)
|
|
|
|
goto fail;
|
2014-07-02 07:28:09 +04:00
|
|
|
|
|
|
|
return h264;
|
2020-10-22 19:11:21 +03:00
|
|
|
|
|
|
|
fail:
|
|
|
|
h264_context_free(h264);
|
|
|
|
return NULL;
|
2014-07-02 07:28:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void h264_context_free(H264_CONTEXT* h264)
|
|
|
|
{
|
|
|
|
if (h264)
|
|
|
|
{
|
2020-11-20 14:37:21 +03:00
|
|
|
size_t x;
|
2021-11-02 11:06:42 +03:00
|
|
|
if (h264->subsystem)
|
|
|
|
h264->subsystem->Uninit(h264);
|
2020-11-20 14:37:21 +03:00
|
|
|
|
|
|
|
for (x = 0; x < 3; x++)
|
|
|
|
{
|
|
|
|
if (h264->Compressor)
|
|
|
|
{
|
|
|
|
_aligned_free(h264->pYUVData[x]);
|
|
|
|
_aligned_free(h264->pOldYUVData[x]);
|
|
|
|
}
|
|
|
|
_aligned_free(h264->pYUV444Data[x]);
|
|
|
|
_aligned_free(h264->pOldYUV444Data[x]);
|
|
|
|
}
|
2018-01-31 17:08:11 +03:00
|
|
|
_aligned_free(h264->lumaData);
|
2020-11-20 14:37:21 +03:00
|
|
|
|
2020-10-22 19:11:21 +03:00
|
|
|
yuv_context_free(h264->yuv);
|
2014-07-02 07:28:09 +04:00
|
|
|
free(h264);
|
|
|
|
}
|
|
|
|
}
|