2012-03-16 08:04:38 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2012-03-16 08:04:38 +04:00
|
|
|
* NSCodec Encoder
|
|
|
|
*
|
|
|
|
* Copyright 2012 Vic Lee
|
2016-04-05 18:07:45 +03:00
|
|
|
* Copyright 2016 Armin Novak <armin.novak@thincast.com>
|
|
|
|
* Copyright 2016 Thincast Technologies GmbH
|
2012-03-16 08:04:38 +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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-03-16 08:04:38 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-08-15 21:13:02 +04:00
|
|
|
|
2013-08-15 04:57:16 +04:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
2012-03-16 08:04:38 +04:00
|
|
|
#include <freerdp/codec/nsc.h>
|
2016-04-05 18:07:45 +03:00
|
|
|
#include <freerdp/codec/color.h>
|
2012-03-16 08:04:38 +04:00
|
|
|
|
|
|
|
#include "nsc_types.h"
|
|
|
|
#include "nsc_encode.h"
|
|
|
|
|
2019-10-04 11:13:43 +03:00
|
|
|
struct _NSC_MESSAGE
|
|
|
|
{
|
|
|
|
UINT32 x;
|
|
|
|
UINT32 y;
|
|
|
|
UINT32 width;
|
|
|
|
UINT32 height;
|
|
|
|
const BYTE* data;
|
|
|
|
UINT32 scanline;
|
|
|
|
BYTE* PlaneBuffer;
|
|
|
|
UINT32 MaxPlaneSize;
|
|
|
|
BYTE* PlaneBuffers[5];
|
|
|
|
UINT32 OrgByteCount[4];
|
|
|
|
|
|
|
|
UINT32 LumaPlaneByteCount;
|
|
|
|
UINT32 OrangeChromaPlaneByteCount;
|
|
|
|
UINT32 GreenChromaPlaneByteCount;
|
|
|
|
UINT32 AlphaPlaneByteCount;
|
|
|
|
UINT8 ColorLossLevel;
|
|
|
|
UINT8 ChromaSubsamplingLevel;
|
|
|
|
};
|
|
|
|
typedef struct _NSC_MESSAGE NSC_MESSAGE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL nsc_write_message(NSC_CONTEXT* context, wStream* s, const NSC_MESSAGE* message);
|
2019-10-04 11:13:43 +03:00
|
|
|
|
2016-10-04 11:05:27 +03:00
|
|
|
static BOOL nsc_context_initialize_encode(NSC_CONTEXT* context)
|
2012-03-16 08:04:38 +04:00
|
|
|
{
|
|
|
|
int i;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 length;
|
|
|
|
UINT32 tempWidth;
|
|
|
|
UINT32 tempHeight;
|
2012-03-16 08:04:38 +04:00
|
|
|
tempWidth = ROUND_UP_TO(context->width, 8);
|
|
|
|
tempHeight = ROUND_UP_TO(context->height, 2);
|
|
|
|
/* The maximum length a decoded plane can reach in all cases */
|
2012-03-18 08:02:28 +04:00
|
|
|
length = tempWidth * tempHeight + 16;
|
2013-08-15 04:57:16 +04:00
|
|
|
|
2013-08-15 07:16:13 +04:00
|
|
|
if (length > context->priv->PlaneBuffersLength)
|
2012-03-16 08:04:38 +04:00
|
|
|
{
|
2012-03-18 08:02:28 +04:00
|
|
|
for (i = 0; i < 5; i++)
|
2016-10-04 11:05:27 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
BYTE* tmp = (BYTE*)realloc(context->priv->PlaneBuffers[i], length);
|
2018-10-22 17:52:21 +03:00
|
|
|
|
2018-03-06 17:32:04 +03:00
|
|
|
if (!tmp)
|
2016-10-04 11:05:27 +03:00
|
|
|
goto fail;
|
2018-03-06 17:32:04 +03:00
|
|
|
|
|
|
|
context->priv->PlaneBuffers[i] = tmp;
|
2016-10-04 11:05:27 +03:00
|
|
|
}
|
|
|
|
|
2013-08-15 07:16:13 +04:00
|
|
|
context->priv->PlaneBuffersLength = length;
|
2012-03-16 08:04:38 +04:00
|
|
|
}
|
|
|
|
|
2014-09-24 04:00:26 +04:00
|
|
|
if (context->ChromaSubsamplingLevel)
|
2012-03-16 08:04:38 +04:00
|
|
|
{
|
2012-03-18 08:02:28 +04:00
|
|
|
context->OrgByteCount[0] = tempWidth * context->height;
|
|
|
|
context->OrgByteCount[1] = tempWidth * tempHeight / 4;
|
|
|
|
context->OrgByteCount[2] = tempWidth * tempHeight / 4;
|
|
|
|
context->OrgByteCount[3] = context->width * context->height;
|
2012-03-16 08:04:38 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-18 08:02:28 +04:00
|
|
|
context->OrgByteCount[0] = context->width * context->height;
|
|
|
|
context->OrgByteCount[1] = context->width * context->height;
|
|
|
|
context->OrgByteCount[2] = context->width * context->height;
|
|
|
|
context->OrgByteCount[3] = context->width * context->height;
|
|
|
|
}
|
2016-10-04 11:05:27 +03:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
fail:
|
|
|
|
|
|
|
|
if (length > context->priv->PlaneBuffersLength)
|
|
|
|
{
|
|
|
|
for (i = 0; i < 5; i++)
|
|
|
|
free(context->priv->PlaneBuffers[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
2012-03-18 08:02:28 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL nsc_encode_argb_to_aycocg(NSC_CONTEXT* context, const BYTE* data, UINT32 scanline)
|
2012-03-18 08:02:28 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 x;
|
|
|
|
UINT16 y;
|
|
|
|
UINT16 rw;
|
|
|
|
BYTE ccl;
|
2016-04-05 18:07:45 +03:00
|
|
|
const BYTE* src;
|
2017-03-28 17:18:00 +03:00
|
|
|
BYTE* yplane = NULL;
|
|
|
|
BYTE* coplane = NULL;
|
|
|
|
BYTE* cgplane = NULL;
|
|
|
|
BYTE* aplane = NULL;
|
2012-10-09 11:01:37 +04:00
|
|
|
INT16 r_val;
|
|
|
|
INT16 g_val;
|
|
|
|
INT16 b_val;
|
|
|
|
BYTE a_val;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 tempWidth;
|
2018-10-22 17:52:21 +03:00
|
|
|
|
2012-03-18 08:02:28 +04:00
|
|
|
tempWidth = ROUND_UP_TO(context->width, 8);
|
2014-09-24 04:00:26 +04:00
|
|
|
rw = (context->ChromaSubsamplingLevel ? tempWidth : context->width);
|
|
|
|
ccl = context->ColorLossLevel;
|
2012-03-18 08:02:28 +04:00
|
|
|
|
|
|
|
for (y = 0; y < context->height; y++)
|
|
|
|
{
|
2013-08-15 21:13:02 +04:00
|
|
|
src = data + (context->height - 1 - y) * scanline;
|
2013-08-15 07:16:13 +04:00
|
|
|
yplane = context->priv->PlaneBuffers[0] + y * rw;
|
|
|
|
coplane = context->priv->PlaneBuffers[1] + y * rw;
|
|
|
|
cgplane = context->priv->PlaneBuffers[2] + y * rw;
|
|
|
|
aplane = context->priv->PlaneBuffers[3] + y * context->width;
|
2013-08-15 04:57:16 +04:00
|
|
|
|
2012-03-18 08:02:28 +04:00
|
|
|
for (x = 0; x < context->width; x++)
|
|
|
|
{
|
2017-01-10 12:23:49 +03:00
|
|
|
switch (context->format)
|
2012-03-18 08:02:28 +04:00
|
|
|
{
|
2016-10-11 18:31:12 +03:00
|
|
|
case PIXEL_FORMAT_BGRX32:
|
|
|
|
b_val = *src++;
|
|
|
|
g_val = *src++;
|
|
|
|
r_val = *src++;
|
|
|
|
src++;
|
|
|
|
a_val = 0xFF;
|
|
|
|
break;
|
|
|
|
|
2016-04-05 18:07:45 +03:00
|
|
|
case PIXEL_FORMAT_BGRA32:
|
2012-03-18 08:02:28 +04:00
|
|
|
b_val = *src++;
|
|
|
|
g_val = *src++;
|
|
|
|
r_val = *src++;
|
|
|
|
a_val = *src++;
|
|
|
|
break;
|
2013-08-15 04:57:16 +04:00
|
|
|
|
2016-10-11 18:31:12 +03:00
|
|
|
case PIXEL_FORMAT_RGBX32:
|
|
|
|
r_val = *src++;
|
|
|
|
g_val = *src++;
|
|
|
|
b_val = *src++;
|
|
|
|
src++;
|
|
|
|
a_val = 0xFF;
|
|
|
|
break;
|
|
|
|
|
2016-04-05 18:07:45 +03:00
|
|
|
case PIXEL_FORMAT_RGBA32:
|
2012-03-18 08:02:28 +04:00
|
|
|
r_val = *src++;
|
|
|
|
g_val = *src++;
|
|
|
|
b_val = *src++;
|
|
|
|
a_val = *src++;
|
|
|
|
break;
|
2013-08-15 04:57:16 +04:00
|
|
|
|
2016-04-05 18:07:45 +03:00
|
|
|
case PIXEL_FORMAT_BGR24:
|
2012-03-18 08:02:28 +04:00
|
|
|
b_val = *src++;
|
|
|
|
g_val = *src++;
|
|
|
|
r_val = *src++;
|
|
|
|
a_val = 0xFF;
|
|
|
|
break;
|
2013-08-15 04:57:16 +04:00
|
|
|
|
2016-04-05 18:07:45 +03:00
|
|
|
case PIXEL_FORMAT_RGB24:
|
2012-03-18 08:02:28 +04:00
|
|
|
r_val = *src++;
|
|
|
|
g_val = *src++;
|
|
|
|
b_val = *src++;
|
|
|
|
a_val = 0xFF;
|
|
|
|
break;
|
2013-08-15 04:57:16 +04:00
|
|
|
|
2016-04-05 18:07:45 +03:00
|
|
|
case PIXEL_FORMAT_BGR16:
|
|
|
|
b_val = (INT16)(((*(src + 1)) & 0xF8) | ((*(src + 1)) >> 5));
|
|
|
|
g_val = (INT16)((((*(src + 1)) & 0x07) << 5) | (((*src) & 0xE0) >> 3));
|
|
|
|
r_val = (INT16)((((*src) & 0x1F) << 3) | (((*src) >> 2) & 0x07));
|
2012-03-18 08:02:28 +04:00
|
|
|
a_val = 0xFF;
|
|
|
|
src += 2;
|
|
|
|
break;
|
2013-08-15 04:57:16 +04:00
|
|
|
|
2016-04-05 18:07:45 +03:00
|
|
|
case PIXEL_FORMAT_RGB16:
|
|
|
|
r_val = (INT16)(((*(src + 1)) & 0xF8) | ((*(src + 1)) >> 5));
|
|
|
|
g_val = (INT16)((((*(src + 1)) & 0x07) << 5) | (((*src) & 0xE0) >> 3));
|
|
|
|
b_val = (INT16)((((*src) & 0x1F) << 3) | (((*src) >> 2) & 0x07));
|
2012-03-18 08:02:28 +04:00
|
|
|
a_val = 0xFF;
|
|
|
|
src += 2;
|
|
|
|
break;
|
2013-08-15 04:57:16 +04:00
|
|
|
|
2016-04-05 18:07:45 +03:00
|
|
|
case PIXEL_FORMAT_A4:
|
2019-11-06 17:24:51 +03:00
|
|
|
{
|
|
|
|
int shift;
|
|
|
|
BYTE idx;
|
|
|
|
shift = (7 - (x % 8));
|
|
|
|
idx = ((*src) >> shift) & 1;
|
|
|
|
idx |= (((*(src + 1)) >> shift) & 1) << 1;
|
|
|
|
idx |= (((*(src + 2)) >> shift) & 1) << 2;
|
|
|
|
idx |= (((*(src + 3)) >> shift) & 1) << 3;
|
|
|
|
idx *= 3;
|
|
|
|
r_val = (INT16)context->palette[idx];
|
|
|
|
g_val = (INT16)context->palette[idx + 1];
|
|
|
|
b_val = (INT16)context->palette[idx + 2];
|
|
|
|
|
|
|
|
if (shift == 0)
|
|
|
|
src += 4;
|
|
|
|
}
|
2016-04-05 18:07:45 +03:00
|
|
|
|
2012-03-18 08:02:28 +04:00
|
|
|
a_val = 0xFF;
|
|
|
|
break;
|
2013-08-15 04:57:16 +04:00
|
|
|
|
2016-04-05 18:07:45 +03:00
|
|
|
case PIXEL_FORMAT_RGB8:
|
2019-11-06 17:24:51 +03:00
|
|
|
{
|
|
|
|
int idx = (*src) * 3;
|
|
|
|
r_val = (INT16)context->palette[idx];
|
|
|
|
g_val = (INT16)context->palette[idx + 1];
|
|
|
|
b_val = (INT16)context->palette[idx + 2];
|
|
|
|
src++;
|
|
|
|
}
|
2016-04-05 18:07:45 +03:00
|
|
|
|
2012-03-18 08:02:28 +04:00
|
|
|
a_val = 0xFF;
|
|
|
|
break;
|
2013-08-15 04:57:16 +04:00
|
|
|
|
2012-03-18 08:02:28 +04:00
|
|
|
default:
|
|
|
|
r_val = g_val = b_val = a_val = 0;
|
|
|
|
break;
|
|
|
|
}
|
2013-08-15 04:57:16 +04:00
|
|
|
|
2016-04-05 18:07:45 +03:00
|
|
|
*yplane++ = (BYTE)((r_val >> 2) + (g_val >> 1) + (b_val >> 2));
|
2012-03-18 08:02:28 +04:00
|
|
|
/* Perform color loss reduction here */
|
2016-04-05 18:07:45 +03:00
|
|
|
*coplane++ = (BYTE)((r_val - b_val) >> ccl);
|
|
|
|
*cgplane++ = (BYTE)((-(r_val >> 1) + g_val - (b_val >> 1)) >> ccl);
|
2012-03-18 08:02:28 +04:00
|
|
|
*aplane++ = a_val;
|
|
|
|
}
|
2013-08-15 04:57:16 +04:00
|
|
|
|
2014-09-24 04:00:26 +04:00
|
|
|
if (context->ChromaSubsamplingLevel && (x % 2) == 1)
|
2012-03-18 08:02:28 +04:00
|
|
|
{
|
|
|
|
*yplane = *(yplane - 1);
|
|
|
|
*coplane = *(coplane - 1);
|
|
|
|
*cgplane = *(cgplane - 1);
|
|
|
|
}
|
|
|
|
}
|
2013-08-15 04:57:16 +04:00
|
|
|
|
2014-09-24 04:00:26 +04:00
|
|
|
if (context->ChromaSubsamplingLevel && (y % 2) == 1)
|
2012-03-18 08:02:28 +04:00
|
|
|
{
|
2017-11-07 15:52:09 +03:00
|
|
|
yplane = context->priv->PlaneBuffers[0] + y * rw;
|
|
|
|
coplane = context->priv->PlaneBuffers[1] + y * rw;
|
|
|
|
cgplane = context->priv->PlaneBuffers[2] + y * rw;
|
|
|
|
CopyMemory(yplane, yplane - rw, rw);
|
|
|
|
CopyMemory(coplane, coplane - rw, rw);
|
|
|
|
CopyMemory(cgplane, cgplane - rw, rw);
|
2012-03-18 08:02:28 +04:00
|
|
|
}
|
2018-10-22 17:52:21 +03:00
|
|
|
|
|
|
|
return TRUE;
|
2012-03-18 08:02:28 +04:00
|
|
|
}
|
|
|
|
|
2018-10-22 17:52:21 +03:00
|
|
|
static BOOL nsc_encode_subsampling(NSC_CONTEXT* context)
|
2012-03-18 08:02:28 +04:00
|
|
|
{
|
2021-01-25 12:06:01 +03:00
|
|
|
UINT32 y;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 tempWidth;
|
|
|
|
UINT32 tempHeight;
|
2018-10-22 17:52:21 +03:00
|
|
|
|
|
|
|
if (!context)
|
|
|
|
return FALSE;
|
|
|
|
|
2012-03-18 08:02:28 +04:00
|
|
|
tempWidth = ROUND_UP_TO(context->width, 8);
|
|
|
|
tempHeight = ROUND_UP_TO(context->height, 2);
|
|
|
|
|
2018-10-22 17:52:21 +03:00
|
|
|
if (tempHeight == 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (tempWidth > context->priv->PlaneBuffersLength / tempHeight)
|
|
|
|
return FALSE;
|
|
|
|
|
2021-02-24 15:53:43 +03:00
|
|
|
for (y = 0; y<tempHeight>> 1; y++)
|
2012-03-18 08:02:28 +04:00
|
|
|
{
|
2021-01-25 12:06:01 +03:00
|
|
|
UINT32 x;
|
2018-10-22 17:52:21 +03:00
|
|
|
BYTE* co_dst = context->priv->PlaneBuffers[1] + y * (tempWidth >> 1);
|
|
|
|
BYTE* cg_dst = context->priv->PlaneBuffers[2] + y * (tempWidth >> 1);
|
2019-11-06 17:24:51 +03:00
|
|
|
const INT8* co_src0 = (INT8*)context->priv->PlaneBuffers[1] + (y << 1) * tempWidth;
|
2018-10-22 17:52:21 +03:00
|
|
|
const INT8* co_src1 = co_src0 + tempWidth;
|
2019-11-06 17:24:51 +03:00
|
|
|
const INT8* cg_src0 = (INT8*)context->priv->PlaneBuffers[2] + (y << 1) * tempWidth;
|
2018-10-22 17:52:21 +03:00
|
|
|
const INT8* cg_src1 = cg_src0 + tempWidth;
|
2013-08-15 04:57:16 +04:00
|
|
|
|
2021-02-24 15:53:43 +03:00
|
|
|
for (x = 0; x<tempWidth>> 1; x++)
|
2012-03-18 08:02:28 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
*co_dst++ = (BYTE)(((INT16)*co_src0 + (INT16) * (co_src0 + 1) + (INT16)*co_src1 +
|
|
|
|
(INT16) * (co_src1 + 1)) >>
|
|
|
|
2);
|
|
|
|
*cg_dst++ = (BYTE)(((INT16)*cg_src0 + (INT16) * (cg_src0 + 1) + (INT16)*cg_src1 +
|
|
|
|
(INT16) * (cg_src1 + 1)) >>
|
|
|
|
2);
|
2012-03-18 08:02:28 +04:00
|
|
|
co_src0 += 2;
|
|
|
|
co_src1 += 2;
|
|
|
|
cg_src0 += 2;
|
|
|
|
cg_src1 += 2;
|
|
|
|
}
|
2012-03-16 08:04:38 +04:00
|
|
|
}
|
2018-10-22 17:52:21 +03:00
|
|
|
|
|
|
|
return TRUE;
|
2012-03-16 08:04:38 +04:00
|
|
|
}
|
|
|
|
|
2018-10-22 17:52:21 +03:00
|
|
|
BOOL nsc_encode(NSC_CONTEXT* context, const BYTE* bmpdata, UINT32 rowstride)
|
2012-03-16 08:04:38 +04:00
|
|
|
{
|
2018-10-22 17:52:21 +03:00
|
|
|
if (!context || !bmpdata || (rowstride == 0))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!nsc_encode_argb_to_aycocg(context, bmpdata, rowstride))
|
|
|
|
return FALSE;
|
2013-08-15 04:57:16 +04:00
|
|
|
|
2014-09-24 04:00:26 +04:00
|
|
|
if (context->ChromaSubsamplingLevel)
|
2012-03-18 08:02:28 +04:00
|
|
|
{
|
2018-10-22 17:52:21 +03:00
|
|
|
if (!nsc_encode_subsampling(context))
|
|
|
|
return FALSE;
|
2012-03-18 08:02:28 +04:00
|
|
|
}
|
2018-10-22 17:52:21 +03:00
|
|
|
|
|
|
|
return TRUE;
|
2012-03-18 08:02:28 +04:00
|
|
|
}
|
|
|
|
|
2018-10-22 17:52:21 +03:00
|
|
|
static UINT32 nsc_rle_encode(const BYTE* in, BYTE* out, UINT32 originalSize)
|
2012-03-18 08:02:28 +04:00
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 left;
|
|
|
|
UINT32 runlength = 1;
|
2013-08-15 04:57:16 +04:00
|
|
|
UINT32 planeSize = 0;
|
|
|
|
left = originalSize;
|
2016-04-05 18:07:45 +03:00
|
|
|
|
2012-03-18 08:02:28 +04:00
|
|
|
/**
|
|
|
|
* We quit the loop if the running compressed size is larger than the original.
|
|
|
|
* In such cases data will be sent uncompressed.
|
|
|
|
*/
|
2013-08-15 04:57:16 +04:00
|
|
|
while (left > 4 && planeSize < originalSize - 4)
|
2012-03-18 08:02:28 +04:00
|
|
|
{
|
|
|
|
if (left > 5 && *in == *(in + 1))
|
|
|
|
{
|
|
|
|
runlength++;
|
|
|
|
}
|
|
|
|
else if (runlength == 1)
|
|
|
|
{
|
|
|
|
*out++ = *in;
|
2013-08-15 04:57:16 +04:00
|
|
|
planeSize++;
|
2012-03-18 08:02:28 +04:00
|
|
|
}
|
|
|
|
else if (runlength < 256)
|
|
|
|
{
|
|
|
|
*out++ = *in;
|
|
|
|
*out++ = *in;
|
|
|
|
*out++ = runlength - 2;
|
|
|
|
runlength = 1;
|
2013-08-15 04:57:16 +04:00
|
|
|
planeSize += 3;
|
2012-03-18 08:02:28 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*out++ = *in;
|
|
|
|
*out++ = *in;
|
|
|
|
*out++ = 0xFF;
|
|
|
|
*out++ = (runlength & 0x000000FF);
|
|
|
|
*out++ = (runlength & 0x0000FF00) >> 8;
|
|
|
|
*out++ = (runlength & 0x00FF0000) >> 16;
|
|
|
|
*out++ = (runlength & 0xFF000000) >> 24;
|
|
|
|
runlength = 1;
|
2013-08-15 04:57:16 +04:00
|
|
|
planeSize += 7;
|
2012-03-18 08:02:28 +04:00
|
|
|
}
|
2016-04-05 18:07:45 +03:00
|
|
|
|
2012-03-18 08:02:28 +04:00
|
|
|
in++;
|
|
|
|
left--;
|
|
|
|
}
|
|
|
|
|
2013-08-15 04:57:16 +04:00
|
|
|
if (planeSize < originalSize - 4)
|
|
|
|
CopyMemory(out, in, 4);
|
|
|
|
|
|
|
|
planeSize += 4;
|
|
|
|
return planeSize;
|
2012-03-18 08:02:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void nsc_rle_compress_data(NSC_CONTEXT* context)
|
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 i;
|
2013-08-15 04:57:16 +04:00
|
|
|
UINT32 planeSize;
|
|
|
|
UINT32 originalSize;
|
2012-03-18 08:02:28 +04:00
|
|
|
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
{
|
2013-08-15 04:57:16 +04:00
|
|
|
originalSize = context->OrgByteCount[i];
|
|
|
|
|
|
|
|
if (originalSize == 0)
|
2012-03-18 08:02:28 +04:00
|
|
|
{
|
2013-08-15 04:57:16 +04:00
|
|
|
planeSize = 0;
|
2012-03-18 08:02:28 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-05 18:07:45 +03:00
|
|
|
planeSize = nsc_rle_encode(context->priv->PlaneBuffers[i],
|
|
|
|
context->priv->PlaneBuffers[4], originalSize);
|
2013-08-15 04:57:16 +04:00
|
|
|
|
|
|
|
if (planeSize < originalSize)
|
2016-04-05 18:07:45 +03:00
|
|
|
CopyMemory(context->priv->PlaneBuffers[i], context->priv->PlaneBuffers[4],
|
|
|
|
planeSize);
|
2012-03-18 08:02:28 +04:00
|
|
|
else
|
2013-08-15 04:57:16 +04:00
|
|
|
planeSize = originalSize;
|
2012-03-18 08:02:28 +04:00
|
|
|
}
|
|
|
|
|
2014-09-24 04:00:26 +04:00
|
|
|
context->PlaneByteCount[i] = planeSize;
|
2012-03-18 08:02:28 +04:00
|
|
|
}
|
2012-03-16 08:04:38 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:30:14 +03:00
|
|
|
static UINT32 nsc_compute_byte_count(NSC_CONTEXT* context, UINT32* ByteCount, UINT32 width,
|
|
|
|
UINT32 height)
|
2013-08-15 07:16:13 +04:00
|
|
|
{
|
|
|
|
UINT32 tempWidth;
|
|
|
|
UINT32 tempHeight;
|
|
|
|
UINT32 maxPlaneSize;
|
|
|
|
tempWidth = ROUND_UP_TO(width, 8);
|
|
|
|
tempHeight = ROUND_UP_TO(height, 2);
|
|
|
|
maxPlaneSize = tempWidth * tempHeight + 16;
|
|
|
|
|
2014-09-24 04:00:26 +04:00
|
|
|
if (context->ChromaSubsamplingLevel)
|
2013-08-15 07:16:13 +04:00
|
|
|
{
|
|
|
|
ByteCount[0] = tempWidth * height;
|
|
|
|
ByteCount[1] = tempWidth * tempHeight / 4;
|
|
|
|
ByteCount[2] = tempWidth * tempHeight / 4;
|
|
|
|
ByteCount[3] = width * height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ByteCount[0] = width * height;
|
|
|
|
ByteCount[1] = width * height;
|
|
|
|
ByteCount[2] = width * height;
|
|
|
|
ByteCount[3] = width * height;
|
|
|
|
}
|
|
|
|
|
|
|
|
return maxPlaneSize;
|
|
|
|
}
|
|
|
|
|
2019-10-04 11:13:43 +03:00
|
|
|
BOOL nsc_write_message(NSC_CONTEXT* context, wStream* s, const NSC_MESSAGE* message)
|
2013-08-15 21:13:02 +04:00
|
|
|
{
|
2014-09-24 04:00:26 +04:00
|
|
|
UINT32 totalPlaneByteCount;
|
2019-11-06 17:24:51 +03:00
|
|
|
totalPlaneByteCount = message->LumaPlaneByteCount + message->OrangeChromaPlaneByteCount +
|
2016-04-05 18:07:45 +03:00
|
|
|
message->GreenChromaPlaneByteCount + message->AlphaPlaneByteCount;
|
2014-09-24 04:00:26 +04:00
|
|
|
|
2015-03-30 18:15:45 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(s, 20 + totalPlaneByteCount))
|
2019-10-04 11:13:43 +03:00
|
|
|
return FALSE;
|
2016-04-05 18:07:45 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Write_UINT32(s, message->LumaPlaneByteCount); /* LumaPlaneByteCount (4 bytes) */
|
|
|
|
Stream_Write_UINT32(
|
|
|
|
s, message->OrangeChromaPlaneByteCount); /* OrangeChromaPlaneByteCount (4 bytes) */
|
|
|
|
Stream_Write_UINT32(
|
|
|
|
s, message->GreenChromaPlaneByteCount); /* GreenChromaPlaneByteCount (4 bytes) */
|
|
|
|
Stream_Write_UINT32(s, message->AlphaPlaneByteCount); /* AlphaPlaneByteCount (4 bytes) */
|
|
|
|
Stream_Write_UINT8(s, message->ColorLossLevel); /* ColorLossLevel (1 byte) */
|
|
|
|
Stream_Write_UINT8(s, message->ChromaSubsamplingLevel); /* ChromaSubsamplingLevel (1 byte) */
|
|
|
|
Stream_Write_UINT16(s, 0); /* Reserved (2 bytes) */
|
2013-08-15 07:16:13 +04:00
|
|
|
|
2014-09-24 04:00:26 +04:00
|
|
|
if (message->LumaPlaneByteCount)
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Write(s, message->PlaneBuffers[0], message->LumaPlaneByteCount); /* LumaPlane */
|
2014-09-24 04:00:26 +04:00
|
|
|
|
|
|
|
if (message->OrangeChromaPlaneByteCount)
|
2016-04-05 18:07:45 +03:00
|
|
|
Stream_Write(s, message->PlaneBuffers[1],
|
|
|
|
message->OrangeChromaPlaneByteCount); /* OrangeChromaPlane */
|
2014-09-24 04:00:26 +04:00
|
|
|
|
|
|
|
if (message->GreenChromaPlaneByteCount)
|
2016-04-05 18:07:45 +03:00
|
|
|
Stream_Write(s, message->PlaneBuffers[2],
|
|
|
|
message->GreenChromaPlaneByteCount); /* GreenChromaPlane */
|
2014-09-24 04:00:26 +04:00
|
|
|
|
|
|
|
if (message->AlphaPlaneByteCount)
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Write(s, message->PlaneBuffers[3], message->AlphaPlaneByteCount); /* AlphaPlane */
|
2013-08-15 07:16:13 +04:00
|
|
|
|
2016-04-05 18:07:45 +03:00
|
|
|
return TRUE;
|
2013-08-15 21:13:02 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
BOOL nsc_compose_message(NSC_CONTEXT* context, wStream* s, const BYTE* data, UINT32 width,
|
|
|
|
UINT32 height, UINT32 scanline)
|
2012-03-16 08:04:38 +04:00
|
|
|
{
|
2019-10-04 11:13:43 +03:00
|
|
|
BOOL rc;
|
|
|
|
NSC_MESSAGE message = { 0 };
|
|
|
|
|
|
|
|
if (!context || !s || !data)
|
|
|
|
return FALSE;
|
|
|
|
|
2012-03-16 08:04:38 +04:00
|
|
|
context->width = width;
|
|
|
|
context->height = height;
|
2016-10-04 11:05:27 +03:00
|
|
|
|
|
|
|
if (!nsc_context_initialize_encode(context))
|
|
|
|
return FALSE;
|
|
|
|
|
2012-03-18 08:02:28 +04:00
|
|
|
/* ARGB to AYCoCg conversion, chroma subsampling and colorloss reduction */
|
2018-02-15 12:19:15 +03:00
|
|
|
PROFILER_ENTER(context->priv->prof_nsc_encode)
|
2019-10-04 11:13:43 +03:00
|
|
|
rc = context->encode(context, data, scanline);
|
2018-02-15 12:19:15 +03:00
|
|
|
PROFILER_EXIT(context->priv->prof_nsc_encode)
|
2019-10-04 11:13:43 +03:00
|
|
|
if (!rc)
|
|
|
|
return FALSE;
|
|
|
|
|
2012-03-18 08:02:28 +04:00
|
|
|
/* RLE encode */
|
2018-02-15 12:19:15 +03:00
|
|
|
PROFILER_ENTER(context->priv->prof_nsc_rle_compress_data)
|
2012-03-18 08:02:28 +04:00
|
|
|
nsc_rle_compress_data(context);
|
2018-02-15 12:19:15 +03:00
|
|
|
PROFILER_EXIT(context->priv->prof_nsc_rle_compress_data)
|
2019-10-04 11:13:43 +03:00
|
|
|
message.PlaneBuffers[0] = context->priv->PlaneBuffers[0];
|
|
|
|
message.PlaneBuffers[1] = context->priv->PlaneBuffers[1];
|
|
|
|
message.PlaneBuffers[2] = context->priv->PlaneBuffers[2];
|
|
|
|
message.PlaneBuffers[3] = context->priv->PlaneBuffers[3];
|
|
|
|
message.LumaPlaneByteCount = context->PlaneByteCount[0];
|
|
|
|
message.OrangeChromaPlaneByteCount = context->PlaneByteCount[1];
|
|
|
|
message.GreenChromaPlaneByteCount = context->PlaneByteCount[2];
|
|
|
|
message.AlphaPlaneByteCount = context->PlaneByteCount[3];
|
|
|
|
message.ColorLossLevel = context->ColorLossLevel;
|
|
|
|
message.ChromaSubsamplingLevel = context->ChromaSubsamplingLevel;
|
|
|
|
return nsc_write_message(context, s, &message);
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
BOOL nsc_decompose_message(NSC_CONTEXT* context, wStream* s, BYTE* bmpdata, UINT32 x, UINT32 y,
|
|
|
|
UINT32 width, UINT32 height, UINT32 rowstride, UINT32 format,
|
|
|
|
UINT32 flip)
|
2019-10-04 11:13:43 +03:00
|
|
|
{
|
|
|
|
size_t size = Stream_GetRemainingLength(s);
|
|
|
|
if (size > UINT32_MAX)
|
|
|
|
return FALSE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!nsc_process_message(context, (UINT16)GetBitsPerPixel(context->format), width, height,
|
|
|
|
Stream_Pointer(s), (UINT32)size, bmpdata, format, rowstride, x, y,
|
|
|
|
width, height, flip))
|
2019-10-04 11:13:43 +03:00
|
|
|
return FALSE;
|
|
|
|
Stream_Seek(s, size);
|
|
|
|
return TRUE;
|
2012-03-16 08:04:38 +04:00
|
|
|
}
|