2014-06-04 04:51:28 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* Graphics Pipeline Extension
|
|
|
|
*
|
|
|
|
* Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2015-06-09 16:22:26 +03:00
|
|
|
* Copyright 2015 Thincast Technologies GmbH
|
|
|
|
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
2014-06-04 04:51:28 +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/stream.h>
|
2014-09-12 18:19:32 +04:00
|
|
|
#include <freerdp/log.h>
|
2014-06-04 04:51:28 +04:00
|
|
|
|
2014-07-09 01:16:13 +04:00
|
|
|
#include "rdpgfx_common.h"
|
|
|
|
|
2014-06-04 04:51:28 +04:00
|
|
|
#include "rdpgfx_codec.h"
|
|
|
|
|
2014-09-12 18:19:32 +04:00
|
|
|
#define TAG CHANNELS_TAG("rdpgfx.client")
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2015-09-03 13:00:22 +03:00
|
|
|
static UINT rdpgfx_read_h264_metablock(RDPGFX_PLUGIN* gfx, wStream* s, RDPGFX_H264_METABLOCK* meta)
|
2014-07-09 01:16:13 +04:00
|
|
|
{
|
|
|
|
UINT32 index;
|
|
|
|
RDPGFX_RECT16* regionRect;
|
|
|
|
RDPGFX_H264_QUANT_QUALITY* quantQualityVal;
|
2015-09-03 13:00:22 +03:00
|
|
|
UINT error = ERROR_INVALID_DATA;
|
2014-07-09 01:16:13 +04:00
|
|
|
|
2014-12-07 02:27:06 +03:00
|
|
|
meta->regionRects = NULL;
|
|
|
|
meta->quantQualityVals = NULL;
|
|
|
|
|
2014-07-09 01:37:29 +04:00
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
2015-06-09 16:22:26 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "not enough data!");
|
2015-09-03 13:00:22 +03:00
|
|
|
goto error_out;
|
2015-06-09 16:22:26 +03:00
|
|
|
}
|
2014-07-09 01:16:13 +04:00
|
|
|
|
2014-07-09 01:37:29 +04:00
|
|
|
Stream_Read_UINT32(s, meta->numRegionRects); /* numRegionRects (4 bytes) */
|
2014-07-09 01:16:13 +04:00
|
|
|
|
2014-07-09 01:37:29 +04:00
|
|
|
if (Stream_GetRemainingLength(s) < (meta->numRegionRects * 8))
|
2015-06-09 16:22:26 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "not enough data!");
|
2015-09-03 13:00:22 +03:00
|
|
|
goto error_out;
|
2015-06-09 16:22:26 +03:00
|
|
|
}
|
2014-07-09 01:16:13 +04:00
|
|
|
|
2014-07-09 01:37:29 +04:00
|
|
|
meta->regionRects = (RDPGFX_RECT16*) malloc(meta->numRegionRects * sizeof(RDPGFX_RECT16));
|
2014-07-09 01:16:13 +04:00
|
|
|
|
2014-07-09 01:37:29 +04:00
|
|
|
if (!meta->regionRects)
|
2015-06-09 16:22:26 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "malloc failed!");
|
2015-09-03 13:00:22 +03:00
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto error_out;
|
2015-06-09 16:22:26 +03:00
|
|
|
}
|
2014-07-09 01:16:13 +04:00
|
|
|
|
2014-07-09 01:37:29 +04:00
|
|
|
meta->quantQualityVals = (RDPGFX_H264_QUANT_QUALITY*) malloc(meta->numRegionRects * sizeof(RDPGFX_H264_QUANT_QUALITY));
|
|
|
|
|
|
|
|
if (!meta->quantQualityVals)
|
2015-06-09 16:22:26 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "malloc failed!");
|
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto error_out;
|
|
|
|
}
|
2014-07-09 01:16:13 +04:00
|
|
|
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_DBG(TAG, "H264_METABLOCK: numRegionRects: %d", (int) meta->numRegionRects);
|
2014-07-09 01:37:29 +04:00
|
|
|
|
|
|
|
for (index = 0; index < meta->numRegionRects; index++)
|
2014-07-09 01:16:13 +04:00
|
|
|
{
|
2014-07-09 01:37:29 +04:00
|
|
|
regionRect = &(meta->regionRects[index]);
|
2015-06-09 16:22:26 +03:00
|
|
|
if ((error = rdpgfx_read_rect16(s, regionRect)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "rdpgfx_read_rect16 failed with error %lu!", error);
|
|
|
|
goto error_out;
|
|
|
|
}
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_DBG(TAG, "regionRects[%d]: left: %d top: %d right: %d bottom: %d",
|
|
|
|
index, regionRect->left, regionRect->top, regionRect->right, regionRect->bottom);
|
2014-07-09 01:16:13 +04:00
|
|
|
}
|
|
|
|
|
2014-07-09 01:37:29 +04:00
|
|
|
if (Stream_GetRemainingLength(s) < (meta->numRegionRects * 2))
|
2015-06-09 16:22:26 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "not enough data!");
|
2015-09-03 13:00:22 +03:00
|
|
|
error = ERROR_INVALID_DATA;
|
|
|
|
goto error_out;
|
2015-06-09 16:22:26 +03:00
|
|
|
}
|
2014-07-09 01:37:29 +04:00
|
|
|
|
|
|
|
for (index = 0; index < meta->numRegionRects; index++)
|
2014-07-09 01:16:13 +04:00
|
|
|
{
|
2014-07-09 01:37:29 +04:00
|
|
|
quantQualityVal = &(meta->quantQualityVals[index]);
|
2014-07-09 01:16:13 +04:00
|
|
|
Stream_Read_UINT8(s, quantQualityVal->qpVal); /* qpVal (1 byte) */
|
|
|
|
Stream_Read_UINT8(s, quantQualityVal->qualityVal); /* qualityVal (1 byte) */
|
|
|
|
|
|
|
|
quantQualityVal->qp = quantQualityVal->qpVal & 0x3F;
|
|
|
|
quantQualityVal->r = (quantQualityVal->qpVal >> 6) & 1;
|
|
|
|
quantQualityVal->p = (quantQualityVal->qpVal >> 7) & 1;
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_DBG(TAG, "quantQualityVals[%d]: qp: %d r: %d p: %d qualityVal: %d",
|
|
|
|
index, quantQualityVal->qp, quantQualityVal->r, quantQualityVal->p, quantQualityVal->qualityVal);
|
2014-07-09 01:16:13 +04:00
|
|
|
}
|
|
|
|
|
2015-06-09 16:22:26 +03:00
|
|
|
return CHANNEL_RC_OK;
|
|
|
|
error_out:
|
|
|
|
free(meta->regionRects);
|
|
|
|
meta->regionRects = NULL;
|
|
|
|
free(meta->quantQualityVals);
|
|
|
|
meta->quantQualityVals = NULL;
|
|
|
|
return error;
|
2014-07-09 01:16:13 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2015-09-03 13:00:22 +03:00
|
|
|
static UINT rdpgfx_decode_h264(RDPGFX_PLUGIN* gfx, RDPGFX_SURFACE_COMMAND* cmd)
|
2014-06-04 04:51:28 +04:00
|
|
|
{
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error;
|
2014-07-09 01:16:13 +04:00
|
|
|
wStream* s;
|
2014-07-09 01:37:29 +04:00
|
|
|
RDPGFX_H264_BITMAP_STREAM h264;
|
|
|
|
RdpgfxClientContext* context = (RdpgfxClientContext*) gfx->iface.pInterface;
|
2014-07-09 01:16:13 +04:00
|
|
|
|
|
|
|
s = Stream_New(cmd->data, cmd->length);
|
|
|
|
|
2014-07-09 01:37:29 +04:00
|
|
|
if (!s)
|
2015-06-09 16:22:26 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2014-07-09 01:37:29 +04:00
|
|
|
|
2015-06-09 16:22:26 +03:00
|
|
|
if ((error = rdpgfx_read_h264_metablock(gfx, s, &(h264.meta))))
|
2014-11-17 01:26:56 +03:00
|
|
|
{
|
2015-06-09 16:22:26 +03:00
|
|
|
WLog_ERR(TAG, "rdpgfx_read_h264_metablock failed with error %lu!", error);
|
|
|
|
return error;
|
2014-11-17 01:26:56 +03:00
|
|
|
}
|
2014-07-09 01:37:29 +04:00
|
|
|
|
|
|
|
h264.data = Stream_Pointer(s);
|
|
|
|
h264.length = (UINT32) Stream_GetRemainingLength(s);
|
2014-07-09 01:16:13 +04:00
|
|
|
|
|
|
|
Stream_Free(s, FALSE);
|
|
|
|
|
2014-07-09 01:37:29 +04:00
|
|
|
cmd->extra = (void*) &h264;
|
|
|
|
|
2015-06-09 16:22:26 +03:00
|
|
|
if (context)
|
2014-07-09 01:37:29 +04:00
|
|
|
{
|
2015-06-09 16:22:26 +03:00
|
|
|
IFCALLRET(context->SurfaceCommand, error, context, cmd);
|
|
|
|
if (error)
|
|
|
|
WLog_ERR(TAG, "context->SurfaceCommand failed with error %lu", error);
|
2014-07-09 01:37:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
free(h264.meta.regionRects);
|
|
|
|
free(h264.meta.quantQualityVals);
|
|
|
|
|
2015-06-09 16:22:26 +03:00
|
|
|
return error;
|
2014-06-04 04:51:28 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
UINT rdpgfx_decode(RDPGFX_PLUGIN* gfx, RDPGFX_SURFACE_COMMAND* cmd)
|
2014-06-04 04:51:28 +04:00
|
|
|
{
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2015-09-03 13:00:22 +03:00
|
|
|
RdpgfxClientContext* context = (RdpgfxClientContext*) gfx->iface.pInterface;
|
2014-06-04 04:51:28 +04:00
|
|
|
|
|
|
|
switch (cmd->codecId)
|
|
|
|
{
|
|
|
|
case RDPGFX_CODECID_H264:
|
2015-06-09 16:22:26 +03:00
|
|
|
if ((error = rdpgfx_decode_h264(gfx, cmd)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "rdpgfx_decode_h264 failed with error %lu", error);
|
|
|
|
return error;
|
|
|
|
}
|
2014-06-04 04:51:28 +04:00
|
|
|
break;
|
|
|
|
|
2015-09-03 13:00:22 +03:00
|
|
|
default:
|
|
|
|
if (context)
|
2015-06-09 16:22:26 +03:00
|
|
|
{
|
2015-09-03 13:00:22 +03:00
|
|
|
IFCALLRET(context->SurfaceCommand, error, context, cmd);
|
|
|
|
if (error)
|
|
|
|
WLog_ERR(TAG, "context->SurfaceCommand failed with error %lu", error);
|
2015-06-09 16:22:26 +03:00
|
|
|
}
|
2014-06-04 04:51:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-06-09 16:22:26 +03:00
|
|
|
return error;
|
2014-06-04 04:51:28 +04:00
|
|
|
}
|