GF_Err gf_lz_compress_payload(u8 **data, u32 data_len, u32 *max_size)
{
	u32 block_size, comp_size;
	lzma_options_lzma opt_lzma2;
	lzma_stream strm = LZMA_STREAM_INIT;

	lzma_lzma_preset(&opt_lzma2, LZMA_PRESET_DEFAULT);
	lzma_filter filters[] = {
			{ .id = LZMA_FILTER_X86, .options = NULL },
			{ .id = LZMA_FILTER_LZMA2, .options = &opt_lzma2 },
			{ .id = LZMA_VLI_UNKNOWN, .options = NULL },
	};
	lzma_ret ret = lzma_stream_encoder(&strm, filters, LZMA_CHECK_NONE);
	if (ret != LZMA_OK) return GF_IO_ERR;

	block_size = data_len*LZMA_COMPRESS_SAFE;
	if (block_size < 64) block_size = 64;
	char *dest = (char *)gf_malloc(sizeof(char)*block_size);


	strm.next_in = (const uint8_t *) (*data);
	strm.avail_in = data_len;
	strm.next_out = (uint8_t *) dest;
	strm.avail_out = block_size;

	ret = lzma_code(&strm, LZMA_FINISH);
	if ((ret != LZMA_OK) && (ret != LZMA_STREAM_END)) {
		GF_LOG(GF_LOG_ERROR, GF_LOG_CORE, ("[LZMA] compressed data failure, code %d\n", ret ));
		return GF_IO_ERR;
	}
	comp_size = block_size - strm.avail_out;

	if (data_len < comp_size) {
		GF_LOG(GF_LOG_INFO, GF_LOG_CORE, ("[LZMA] compressed data (%d) larger than input (%d)\n", (u32) comp_size, (u32) data_len ));
	}

	if (*max_size < comp_size) {
		*max_size = block_size;
		*data = (char*)gf_realloc(*data, block_size * sizeof(char));
	}

	memcpy((*data) , dest, sizeof(char) * comp_size);
	*max_size = (u32) comp_size;
	gf_free(dest);

	lzma_end(&strm);
	return GF_OK;
}
GF_Err gf_lz_compress_payload(u8 **data, u32 data_len, u32 *max_size)
{
	u32 block_size, comp_size;
	lzma_options_lzma opt_lzma2;
	lzma_stream strm = LZMA_STREAM_INIT;

	lzma_lzma_preset(&opt_lzma2, LZMA_PRESET_DEFAULT);
	lzma_filter filters[] = {
			{ .id = LZMA_FILTER_X86, .options = NULL },
			{ .id = LZMA_FILTER_LZMA2, .options = &opt_lzma2 },
			{ .id = LZMA_VLI_UNKNOWN, .options = NULL },
	};
	lzma_ret ret = lzma_stream_encoder(&strm, filters, LZMA_CHECK_NONE);
	if (ret != LZMA_OK) return GF_IO_ERR;

	block_size = data_len*LZMA_COMPRESS_SAFE;
	if (block_size < 64) block_size = 64;
	char *dest = (char *)gf_malloc(sizeof(char)*block_size);


	strm.next_in = (const uint8_t *) (*data);
	strm.avail_in = data_len;
	strm.next_out = (uint8_t *) dest;
	strm.avail_out = block_size;

	ret = lzma_code(&strm, LZMA_FINISH);
	if ((ret != LZMA_OK) && (ret != LZMA_STREAM_END)) {
		GF_LOG(GF_LOG_ERROR, GF_LOG_CORE, ("[LZMA] compressed data failure, code %d\n", ret ));
		return GF_IO_ERR;
	}
	comp_size = block_size - strm.avail_out;

	if (data_len < comp_size) {
		GF_LOG(GF_LOG_INFO, GF_LOG_CORE, ("[LZMA] compressed data (%d) larger than input (%d)\n", (u32) comp_size, (u32) data_len ));
	}

	if (*max_size < comp_size) {
		*max_size = block_size;
		*data = (char*)gf_realloc(*data, block_size * sizeof(char));
	}

	memcpy((*data) , dest, sizeof(char) * comp_size);
	*max_size = (u32) comp_size;
	gf_free(dest);

	lzma_end(&strm);
	return GF_OK;
}