/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
 * Copyright (C) 2011 D. R. Commander.  All Rights Reserved.
 * Copyright (C) 2015-2026 m-privacy GmbH.  All Rights Reserved.
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

//
// MPCompressor compresses RGB input into an MP image and stores it in
// an underlying MemOutStream
//

#ifndef __RFB_MPCOMPRESSOR_H__
#define __RFB_MPCOMPRESSOR_H__

#include <rdr/MemOutStream.h>
#include <rfb/PixelFormat.h>
#include <rfb/Rect.h>

#include <rfb/MP.h>

#if !defined(__APPLE__) && !defined(WIN32)
#include <wels/codec_api.h>
#include <stdint.h>
#include <x264.h>
#endif

#include <zstd.h>

#define MP_HEADER_SIZE 2

struct jpeg_compress_struct;
struct MP_JPEG_ERROR_MGR;
struct MP_JPEG_DEST_MGR;

namespace rfb {

	class MPCompressor : public rdr::MemOutStream {

	public:

		MPCompressor(int bufferLen = 128*1024);
		virtual ~MPCompressor();

		bool compress(const rdr::U8 *buf, int stride, const Rect& r, const PixelFormat& pf, int mpLevel, int mpCompression);

		inline rdr::U8* getstart() { return memBuffer.data; }

		virtual inline void overrun(size_t needed, int key=0, QPrio prio=QPRIOMEDIUM) {
			MemOutStream::overrun(needed, key, prio);
		}

		static void setCpuPressure(rdr::U8 value);
		static void setSaveBandwidth(bool value);
		static void setSaveBandwidthHigh(bool value);
		static rdr::U8 getPressureLevel() {return pressureLevel;}
#if !defined(__APPLE__) && !defined(WIN32)
		static void setSupportsTightMPX264(bool value);
#endif
		rdr::U8 * getDataBuffer(const int newsize, const char * compName);
		rdr::U8 * getYuvBuffer(const int newsize, const char * compName);
		rdr::U64 totalBytes[MP_COMPRESSION_MAX+1];
		rdr::U64 totalCBytes[MP_COMPRESSION_MAX+1];
		int minCompressed[MP_COMPRESSION_MAX+1];
		int maxCompressed[MP_COMPRESSION_MAX+1];

	private:
		static rdr::U8 cpuPressure;
		static rdr::U8 pressureLevel;
		static bool saveBandwidth;
		static bool saveBandwidthHigh;
#if !defined(__APPLE__) && !defined(WIN32)
		static bool clientSupportsTightMPX264;
#endif
		unsigned compressionCount[MP_COMPRESSION_MAX+1];
		unsigned optimizationCount[MP_COMPRESSION_MAX+1];

#if !defined(__APPLE__) && !defined(WIN32)
		SEncParamBase welsParam;
		int welsVideoFormat;
		int welsComplexity;
		SSourcePicture welsPic;
		x264_param_t x264_param;
		x264_picture_t x264_pic;
		x264_picture_t x264_pic_out;
#endif

		struct jpeg_compress_struct *jpegCinfo;
		struct MP_JPEG_ERROR_MGR *jpegErr;
		struct MP_JPEG_DEST_MGR *jpegDest;

		ZSTD_CCtx *zstdCctx;

		rdr::U8 * dataBuffer;
		int dataBufferLen;
	};

} // end of namespace rfb

#endif
