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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#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>
|
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
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (height % 16 != 0)
|
|
|
|
height += 16 - height % 16;
|
|
|
|
|
2017-04-20 13:02:04 +03:00
|
|
|
if (!h264->pYUVData[0] || !h264->pYUVData[1] || !h264->pYUVData[2] ||
|
2018-01-31 17:08:11 +03:00
|
|
|
(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;
|
|
|
|
_aligned_free(h264->pYUVData[0]);
|
|
|
|
_aligned_free(h264->pYUVData[1]);
|
|
|
|
_aligned_free(h264->pYUVData[2]);
|
|
|
|
h264->pYUVData[0] = _aligned_malloc(h264->iStride[0] * height, 16);
|
|
|
|
h264->pYUVData[1] = _aligned_malloc(h264->iStride[1] * height, 16);
|
|
|
|
h264->pYUVData[2] = _aligned_malloc(h264->iStride[2] * height, 16);
|
|
|
|
|
|
|
|
if (!h264->pYUVData[0] || !h264->pYUVData[1] || !h264->pYUVData[2])
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-07-14 17:59:57 +04:00
|
|
|
|
2016-03-02 17:16:49 +03:00
|
|
|
static BOOL check_rect(const H264_CONTEXT* h264, const RECTANGLE_16* rect,
|
2016-08-03 16:29:12 +03:00
|
|
|
UINT32 nDstWidth, UINT32 nDstHeight)
|
2014-07-14 17:59:57 +04:00
|
|
|
{
|
2016-03-02 17:16:49 +03:00
|
|
|
/* Check, if the output rectangle is valid in decoded h264 frame. */
|
|
|
|
if ((rect->right > h264->width) || (rect->left > h264->width))
|
|
|
|
return FALSE;
|
2016-08-03 16:29:12 +03:00
|
|
|
|
2016-03-02 17:16:49 +03:00
|
|
|
if ((rect->top > h264->height) || (rect->bottom > h264->height))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Check, if the output rectangle is valid in destination buffer. */
|
|
|
|
if ((rect->right > nDstWidth) || (rect->left > nDstWidth))
|
|
|
|
return FALSE;
|
2016-08-03 16:29:12 +03:00
|
|
|
|
2016-03-02 17:16:49 +03:00
|
|
|
if ((rect->bottom > nDstHeight) || (rect->top > nDstHeight))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL avc_yuv_to_rgb(H264_CONTEXT* h264, const RECTANGLE_16* regionRects,
|
2016-08-03 16:29:12 +03:00
|
|
|
UINT32 numRegionRects, UINT32 nDstWidth,
|
|
|
|
UINT32 nDstHeight, UINT32 nDstStep, BYTE* pDstData,
|
|
|
|
DWORD DstFormat, BOOL use444)
|
2016-03-02 17:16:49 +03:00
|
|
|
{
|
|
|
|
UINT32 x;
|
2014-09-03 00:16:56 +04:00
|
|
|
BYTE* pDstPoint;
|
2014-09-10 03:15:07 +04:00
|
|
|
prim_size_t roi;
|
2017-07-17 10:28:08 +03:00
|
|
|
INT32 width, height;
|
2016-03-02 17:16:49 +03:00
|
|
|
const BYTE* pYUVPoint[3];
|
2015-07-28 23:01:34 +03:00
|
|
|
primitives_t* prims = primitives_get();
|
2014-07-14 17:59:57 +04:00
|
|
|
|
2016-08-03 16:29:12 +03:00
|
|
|
for (x = 0; x < numRegionRects; x++)
|
2016-03-02 17:16:49 +03:00
|
|
|
{
|
|
|
|
const RECTANGLE_16* rect = &(regionRects[x]);
|
|
|
|
const UINT32* iStride;
|
|
|
|
BYTE** ppYUVData;
|
2014-09-03 00:16:56 +04:00
|
|
|
|
2016-03-02 17:16:49 +03:00
|
|
|
if (use444)
|
|
|
|
{
|
|
|
|
iStride = h264->iYUV444Stride;
|
|
|
|
ppYUVData = h264->pYUV444Data;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-15 11:28:10 +03:00
|
|
|
iStride = h264->iStride;
|
|
|
|
ppYUVData = h264->pYUVData;
|
2016-03-02 17:16:49 +03:00
|
|
|
}
|
2014-09-10 03:15:07 +04:00
|
|
|
|
2016-03-02 17:16:49 +03:00
|
|
|
if (!check_rect(h264, rect, nDstWidth, nDstHeight))
|
2016-08-10 11:29:59 +03:00
|
|
|
return FALSE;
|
2015-01-22 15:22:53 +03:00
|
|
|
|
2014-09-10 03:15:07 +04:00
|
|
|
width = rect->right - rect->left;
|
|
|
|
height = rect->bottom - rect->top;
|
2014-09-03 00:16:56 +04:00
|
|
|
pDstPoint = pDstData + rect->top * nDstStep + rect->left * 4;
|
2016-03-02 17:16:49 +03:00
|
|
|
pYUVPoint[0] = ppYUVData[0] + rect->top * iStride[0] + rect->left;
|
|
|
|
pYUVPoint[1] = ppYUVData[1];
|
|
|
|
pYUVPoint[2] = ppYUVData[2];
|
2016-08-03 16:29:12 +03:00
|
|
|
|
2016-03-02 17:16:49 +03:00
|
|
|
if (use444)
|
|
|
|
{
|
|
|
|
pYUVPoint[1] += rect->top * iStride[1] + rect->left;
|
|
|
|
pYUVPoint[2] += rect->top * iStride[2] + rect->left;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-03 16:29:12 +03:00
|
|
|
pYUVPoint[1] += rect->top / 2 * iStride[1] + rect->left / 2;
|
|
|
|
pYUVPoint[2] += rect->top / 2 * iStride[2] + rect->left / 2;
|
2016-03-02 17:16:49 +03:00
|
|
|
}
|
2014-09-03 00:16:56 +04:00
|
|
|
|
2014-09-10 03:15:07 +04:00
|
|
|
roi.width = width;
|
|
|
|
roi.height = height;
|
2014-09-08 18:56:45 +04:00
|
|
|
|
2016-03-02 17:16:49 +03:00
|
|
|
if (use444)
|
|
|
|
{
|
|
|
|
if (prims->YUV444ToRGB_8u_P3AC4R(
|
2016-08-03 16:29:12 +03:00
|
|
|
pYUVPoint, iStride, pDstPoint, nDstStep,
|
|
|
|
DstFormat, &roi) != PRIMITIVES_SUCCESS)
|
2016-03-02 17:16:49 +03:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-05 18:07:45 +03:00
|
|
|
if (prims->YUV420ToRGB_8u_P3AC4R(
|
2016-08-03 16:29:12 +03:00
|
|
|
pYUVPoint, iStride, pDstPoint, nDstStep,
|
|
|
|
DstFormat, &roi) != PRIMITIVES_SUCCESS)
|
2016-03-02 17:16:49 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
2014-09-03 00:16:56 +04:00
|
|
|
}
|
|
|
|
|
2016-03-02 17:16:49 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2017-01-27 13:23:08 +03:00
|
|
|
INT32 avc420_decompress(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize,
|
2016-08-03 16:29:12 +03:00
|
|
|
BYTE* pDstData, DWORD DstFormat, UINT32 nDstStep,
|
|
|
|
UINT32 nDstWidth, UINT32 nDstHeight,
|
|
|
|
RECTANGLE_16* regionRects, UINT32 numRegionRects)
|
2016-03-02 17:16:49 +03:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if (!h264)
|
|
|
|
return -1001;
|
|
|
|
|
2017-02-15 11:28:10 +03:00
|
|
|
status = h264->subsystem->Decompress(h264, pSrcData, SrcSize);
|
2016-03-02 17:16:49 +03:00
|
|
|
if (status == 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
if (!avc_yuv_to_rgb(h264, regionRects, numRegionRects, nDstWidth,
|
2016-08-03 16:29:12 +03:00
|
|
|
nDstHeight, nDstStep, pDstData, DstFormat, FALSE))
|
2016-03-02 17:16:49 +03:00
|
|
|
return -1002;
|
|
|
|
|
2014-07-02 07:28:09 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-01-27 13:23:08 +03:00
|
|
|
INT32 avc420_compress(H264_CONTEXT* h264, const BYTE* pSrcData, DWORD SrcFormat,
|
2016-08-03 16:29:12 +03:00
|
|
|
UINT32 nSrcStep, UINT32 nSrcWidth, UINT32 nSrcHeight,
|
|
|
|
BYTE** ppDstData, UINT32* pDstSize)
|
2014-07-02 07:28:09 +04:00
|
|
|
{
|
2015-02-16 12:51:20 +03:00
|
|
|
prim_size_t roi;
|
2015-07-28 23:01:34 +03:00
|
|
|
primitives_t* prims = primitives_get();
|
2015-02-16 12:51:20 +03:00
|
|
|
|
|
|
|
if (!h264)
|
|
|
|
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
|
|
|
|
2015-02-16 12:51:20 +03:00
|
|
|
roi.width = nSrcWidth;
|
|
|
|
roi.height = nSrcHeight;
|
2018-01-31 17:08:11 +03:00
|
|
|
|
2017-04-20 13:02:04 +03:00
|
|
|
if (prims->RGBToYUV420_8u_P3AC4R(pSrcData, SrcFormat, nSrcStep, h264->pYUVData, h264->iStride,
|
2018-01-31 17:08:11 +03:00
|
|
|
&roi) != PRIMITIVES_SUCCESS)
|
2017-04-20 13:02:04 +03:00
|
|
|
return -1;
|
|
|
|
|
2018-01-31 17:08:11 +03:00
|
|
|
return h264->subsystem->Compress(h264, h264->pYUVData, h264->iStride, ppDstData, pDstSize);
|
2016-03-02 17:16:49 +03:00
|
|
|
}
|
|
|
|
|
2017-01-27 13:23:08 +03:00
|
|
|
INT32 avc444_compress(H264_CONTEXT* h264, const BYTE* pSrcData, DWORD SrcFormat,
|
2016-08-03 16:29:12 +03:00
|
|
|
UINT32 nSrcStep, UINT32 nSrcWidth, UINT32 nSrcHeight,
|
2018-01-31 17:08:11 +03:00
|
|
|
BYTE version, BYTE* op, BYTE** ppDstData, UINT32* pDstSize,
|
2016-08-03 16:29:12 +03:00
|
|
|
BYTE** ppAuxDstData, UINT32* pAuxDstSize)
|
2016-03-02 17:16:49 +03:00
|
|
|
{
|
2018-01-31 17:08:11 +03:00
|
|
|
prim_size_t roi;
|
|
|
|
primitives_t* prims = primitives_get();
|
|
|
|
BYTE* coded;
|
|
|
|
UINT32 codedSize;
|
|
|
|
|
|
|
|
if (!h264)
|
|
|
|
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;
|
|
|
|
|
|
|
|
roi.width = nSrcWidth;
|
|
|
|
roi.height = nSrcHeight;
|
|
|
|
|
|
|
|
switch (version)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
if (prims->RGBToAVC444YUV(pSrcData, SrcFormat, nSrcStep, h264->pYUV444Data, h264->iStride,
|
|
|
|
h264->pYUVData, h264->iStride, &roi) != PRIMITIVES_SUCCESS)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
if (prims->RGBToAVC444YUVv2(pSrcData, SrcFormat, nSrcStep, h264->pYUV444Data, h264->iStride,
|
|
|
|
h264->pYUVData, h264->iStride, &roi) != PRIMITIVES_SUCCESS)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (h264->subsystem->Compress(h264, h264->pYUV444Data, h264->iStride, &coded, &codedSize) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memcpy(h264->lumaData, coded, codedSize);
|
|
|
|
*ppDstData = h264->lumaData;
|
|
|
|
*pDstSize = codedSize;
|
|
|
|
|
|
|
|
if (h264->subsystem->Compress(h264, h264->pYUVData, h264->iStride, &coded, &codedSize) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*ppAuxDstData = coded;
|
|
|
|
*pAuxDstSize = codedSize;
|
|
|
|
*op = 0;
|
|
|
|
return 0;
|
2016-03-02 17:16:49 +03:00
|
|
|
}
|
|
|
|
|
2017-02-15 11:28:10 +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;
|
|
|
|
UINT32 padDstHeight = nDstHeight + 16; /* Need alignment to 16x16 blocks */
|
|
|
|
|
|
|
|
if ((piMainStride[0] != piDstStride[0]) ||
|
|
|
|
(piDstSize[0] != piMainStride[0] * padDstHeight))
|
|
|
|
{
|
2016-08-03 16:29:12 +03:00
|
|
|
for (x = 0; x < 3; x++)
|
2016-03-02 17:16:49 +03:00
|
|
|
{
|
|
|
|
piDstStride[x] = piMainStride[0];
|
|
|
|
piDstSize[x] = piDstStride[x] * padDstHeight;
|
2017-02-14 14:12:24 +03:00
|
|
|
_aligned_free(ppYUVDstData[x]);
|
|
|
|
ppYUVDstData[x] = _aligned_malloc(piDstSize[x], 16);
|
2016-03-02 17:16:49 +03:00
|
|
|
|
2017-02-14 14:12:24 +03:00
|
|
|
if (!ppYUVDstData[x])
|
2016-03-02 17:16:49 +03:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
memset(ppYUVDstData[x], 0, piDstSize[x]);
|
|
|
|
}
|
2018-01-31 17:08:11 +03:00
|
|
|
|
|
|
|
_aligned_free(h264->lumaData);
|
|
|
|
h264->lumaData = _aligned_malloc(piDstSize[0] * 4, 16);
|
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
|
|
|
{
|
|
|
|
if (!ppYUVDstData[x] || (piDstSize[x] == 0) || (piDstStride[x] == 0))
|
|
|
|
{
|
2018-01-09 19:22:56 +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:
|
2017-02-14 14:12:24 +03:00
|
|
|
_aligned_free(ppYUVDstData[0]);
|
|
|
|
_aligned_free(ppYUVDstData[1]);
|
|
|
|
_aligned_free(ppYUVDstData[2]);
|
2018-01-31 17:08:11 +03:00
|
|
|
_aligned_free(h264->lumaData);
|
2016-03-02 17:16:49 +03:00
|
|
|
ppYUVDstData[0] = NULL;
|
|
|
|
ppYUVDstData[1] = NULL;
|
|
|
|
ppYUVDstData[2] = NULL;
|
2018-01-31 17:08:11 +03:00
|
|
|
h264->lumaData = NULL;
|
2016-03-02 17:16:49 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2017-02-15 11:28:10 +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,
|
2017-04-10 18:16:57 +03:00
|
|
|
avc444_frame_type type)
|
2017-02-15 11:28:10 +03:00
|
|
|
{
|
|
|
|
const primitives_t* prims = primitives_get();
|
|
|
|
UINT32 x;
|
|
|
|
UINT32* piDstStride = h264->iYUV444Stride;
|
|
|
|
BYTE** ppYUVDstData = h264->pYUV444Data;
|
|
|
|
const UINT32* piStride = h264->iStride;
|
2017-04-10 18:16:57 +03:00
|
|
|
const BYTE** ppYUVData = (const BYTE**)h264->pYUVData;
|
2017-02-15 11:28:10 +03:00
|
|
|
|
|
|
|
if (h264->subsystem->Decompress(h264, pSrcData, SrcSize) < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!avc444_ensure_buffer(h264, nDstHeight))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
for (x = 0; x < nrRects; x++)
|
|
|
|
{
|
|
|
|
const RECTANGLE_16* rect = &rects[x];
|
2017-06-01 14:01:23 +03:00
|
|
|
const UINT32 alignedWidth = h264->width + ((h264->width % 16 != 0) ? 16 - h264->width % 16 : 0);
|
|
|
|
const UINT32 alignedHeight = h264->height + ((h264->height % 16 != 0) ? 16 - h264->height % 16 : 0);
|
2017-02-15 11:28:10 +03:00
|
|
|
|
|
|
|
if (!check_rect(h264, rect, nDstWidth, nDstHeight))
|
|
|
|
continue;
|
|
|
|
|
2017-04-10 18:16:57 +03:00
|
|
|
if (prims->YUV420CombineToYUV444(type, ppYUVData, piStride,
|
2017-06-01 14:01:23 +03:00
|
|
|
alignedWidth, alignedHeight,
|
2017-04-10 18:16:57 +03:00
|
|
|
ppYUVDstData, piDstStride,
|
|
|
|
rect) != PRIMITIVES_SUCCESS)
|
|
|
|
return FALSE;
|
2017-02-15 11:28:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!avc_yuv_to_rgb(h264, rects, nrRects, nDstWidth,
|
|
|
|
nDstHeight, nDstStep, pDstData, DstFormat, TRUE))
|
|
|
|
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
|
|
|
|
|
|
|
|
INT32 avc444_decompress(H264_CONTEXT* h264, BYTE op,
|
2016-08-03 16:29:12 +03:00
|
|
|
RECTANGLE_16* regionRects, UINT32 numRegionRects,
|
2017-01-27 13:23:08 +03:00
|
|
|
const BYTE* pSrcData, UINT32 SrcSize,
|
2016-08-03 16:29:12 +03:00
|
|
|
RECTANGLE_16* auxRegionRects, UINT32 numAuxRegionRect,
|
2017-01-27 13:23:08 +03:00
|
|
|
const BYTE* pAuxSrcData, UINT32 AuxSrcSize,
|
2016-08-03 16:29:12 +03:00
|
|
|
BYTE* pDstData, DWORD DstFormat,
|
2017-04-10 18:16:57 +03:00
|
|
|
UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight,
|
|
|
|
UINT32 codecId)
|
2016-03-02 17:16:49 +03:00
|
|
|
{
|
|
|
|
INT32 status = -1;
|
2017-04-10 18:16:57 +03:00
|
|
|
avc444_frame_type chroma = (codecId == RDPGFX_CODECID_AVC444) ? AVC444_CHROMAv1 : AVC444_CHROMAv2;
|
2016-03-02 17:16:49 +03:00
|
|
|
|
|
|
|
if (!h264 || !regionRects ||
|
|
|
|
!pSrcData || !pDstData)
|
|
|
|
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
|
2016-03-02 17:16:49 +03:00
|
|
|
* Chroma420 in stream 2 */
|
2017-02-15 11:28:10 +03:00
|
|
|
if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep, nDstWidth,
|
|
|
|
nDstHeight,
|
2017-04-10 18:16:57 +03:00
|
|
|
regionRects, numRegionRects, AVC444_LUMA))
|
2017-02-15 11:28:10 +03:00
|
|
|
status = -1;
|
|
|
|
else if (!avc444_process_rects(h264, pAuxSrcData, AuxSrcSize, pDstData, DstFormat, nDstStep,
|
|
|
|
nDstWidth, nDstHeight,
|
2017-04-10 18:16:57 +03:00
|
|
|
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 */
|
2017-02-15 11:28:10 +03:00
|
|
|
if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep, nDstWidth,
|
|
|
|
nDstHeight,
|
2017-04-10 18:16:57 +03:00
|
|
|
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 */
|
2017-02-15 11:28:10 +03:00
|
|
|
if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep, nDstWidth,
|
|
|
|
nDstHeight,
|
2017-04-10 18:16:57 +03:00
|
|
|
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,
|
|
|
|
"luma=%"PRIu64" [avg=%lf] chroma=%"PRIu64" [avg=%lf] combined=%"PRIu64" [avg=%lf]",
|
|
|
|
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;
|
2018-01-31 17:08:11 +03:00
|
|
|
static H264_CONTEXT_SUBSYSTEM* subSystems[MAX_SUBSYSTEMS];
|
2016-08-03 16:29:12 +03:00
|
|
|
|
2017-06-14 17:14:19 +03:00
|
|
|
#if defined(_WIN32) && defined(WITH_MEDIA_FOUNDATION)
|
|
|
|
extern H264_CONTEXT_SUBSYSTEM g_Subsystem_MF;
|
|
|
|
#endif
|
|
|
|
|
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;
|
2017-06-14 17:14:19 +03:00
|
|
|
ZeroMemory(subSystems, sizeof(subSystems));
|
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
|
|
|
|
{
|
|
|
|
extern H264_CONTEXT_SUBSYSTEM g_Subsystem_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
|
|
|
|
{
|
|
|
|
extern H264_CONTEXT_SUBSYSTEM g_Subsystem_libavcodec;
|
|
|
|
subSystems[i] = &g_Subsystem_libavcodec;
|
|
|
|
i++;
|
|
|
|
}
|
2017-06-14 17:04:54 +03:00
|
|
|
#endif
|
|
|
|
#ifdef WITH_X264
|
2017-07-18 08:46:51 +03:00
|
|
|
{
|
|
|
|
extern H264_CONTEXT_SUBSYSTEM g_Subsystem_x264;
|
|
|
|
subSystems[i] = &g_Subsystem_x264;
|
|
|
|
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
|
|
|
|
2016-08-03 16:29:12 +03:00
|
|
|
|
2017-06-14 17:04:54 +03:00
|
|
|
BOOL h264_context_init(H264_CONTEXT* h264)
|
|
|
|
{
|
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
|
|
|
{
|
2017-07-19 12:36:28 +03:00
|
|
|
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;
|
|
|
|
return TRUE;
|
2014-09-12 22:57:44 +04:00
|
|
|
}
|
|
|
|
|
2014-07-02 07:28:09 +04:00
|
|
|
H264_CONTEXT* h264_context_new(BOOL Compressor)
|
|
|
|
{
|
2014-07-04 11:16:55 +04:00
|
|
|
H264_CONTEXT* h264;
|
2014-07-02 07:28:09 +04:00
|
|
|
h264 = (H264_CONTEXT*) calloc(1, sizeof(H264_CONTEXT));
|
|
|
|
|
|
|
|
if (h264)
|
|
|
|
{
|
|
|
|
h264->Compressor = Compressor;
|
|
|
|
|
2015-04-21 08:59:01 +03:00
|
|
|
if (Compressor)
|
|
|
|
{
|
|
|
|
/* Default compressor settings, may be changed by caller */
|
|
|
|
h264->BitRate = 1000000;
|
|
|
|
h264->FrameRate = 30;
|
|
|
|
}
|
|
|
|
|
2014-09-06 03:11:03 +04:00
|
|
|
if (!h264_context_init(h264))
|
2014-07-14 17:59:57 +04:00
|
|
|
{
|
|
|
|
free(h264);
|
|
|
|
return NULL;
|
2014-07-02 07:28:09 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return h264;
|
|
|
|
}
|
|
|
|
|
|
|
|
void h264_context_free(H264_CONTEXT* h264)
|
|
|
|
{
|
|
|
|
if (h264)
|
|
|
|
{
|
2014-09-06 03:11:03 +04:00
|
|
|
h264->subsystem->Uninit(h264);
|
2017-02-14 14:12:24 +03:00
|
|
|
_aligned_free(h264->pYUV444Data[0]);
|
|
|
|
_aligned_free(h264->pYUV444Data[1]);
|
|
|
|
_aligned_free(h264->pYUV444Data[2]);
|
2018-01-31 17:08:11 +03:00
|
|
|
_aligned_free(h264->lumaData);
|
2014-07-02 07:28:09 +04:00
|
|
|
free(h264);
|
|
|
|
}
|
|
|
|
}
|