Adding a transcoding stream interface for in-memory compression.

This commit is contained in:
Gregor Richards 2016-11-26 09:19:46 -05:00
parent da0b958aeb
commit f27476b4ef
4 changed files with 485 additions and 0 deletions

View File

@ -0,0 +1,97 @@
/* Copyright (C) 2016 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (trans_stream.h).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef LIBRETRO_SDK_TRANS_STREAM_H__
#define LIBRETRO_SDK_TRANS_STREAM_H__
#include <stdint.h>
#include <stddef.h>
#include <boolean.h>
#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#endif
#include <retro_miscellaneous.h>
enum trans_stream_error
{
TRANS_STREAM_ERROR_NONE = 0,
TRANS_STREAM_ERROR_ALLOCATION_FAILURE,
TRANS_STREAM_ERROR_INVALID,
TRANS_STREAM_ERROR_BUFFER_FULL,
TRANS_STREAM_ERROR_OTHER
};
struct trans_stream_backend
{
const char *ident;
const struct trans_stream_backend *reverse;
/* Create a stream data structure */
void *(*stream_new)(void);
/* Free it */
void (*stream_free)(void *);
/* Set our input source */
void (*set_in)(void *, const uint8_t *, uint32_t);
/* Set our output target */
void (*set_out)(void *, uint8_t *, uint32_t);
/* Perform a transcoding, flushing/finalizing if asked to. Writes out how
* many bytes were read and written. Error target optional. */
bool (*trans)(void *, bool, uint32_t *, uint32_t *, enum trans_stream_error *);
};
/**
* trans_stream_trans_full:
* @backend : transcoding backend
* @data : (optional) existing stream data, or a target
* for the new stream data to be saved
* @in : input data
* @in_size : input size
* @out : output data
* @out_size : output size
* @error : (optional) output for error code
*
* Perform a full transcoding from a source to a destination.
*/
bool trans_stream_trans_full(
struct trans_stream_backend *backend, void **data,
const uint8_t *in, uint32_t in_size,
uint8_t *out, uint32_t out_size,
enum trans_stream_error *error);
const struct trans_stream_backend* trans_stream_get_zlib_deflate_backend(void);
const struct trans_stream_backend* trans_stream_get_zlib_inflate_backend(void);
const struct trans_stream_backend* trans_stream_get_pipe_backend(void);
extern const struct trans_stream_backend zlib_deflate_backend;
extern const struct trans_stream_backend zlib_inflate_backend;
extern const struct trans_stream_backend pipe_backend;
#endif

View File

@ -0,0 +1,95 @@
/* Copyright (C) 2016 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (trans_stream.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <streams/trans_stream.h>
/**
* trans_stream_trans_full:
* @data : (optional) existing stream data, or a target
* for the new stream data to be saved
* @in : input data
* @in_size : input size
* @out : output data
* @out_size : output size
* @error : (optional) output for error code
*
* Perform a full transcoding from a source to a destination.
*/
bool trans_stream_trans_full(
struct trans_stream_backend *backend, void **data,
const uint8_t *in, uint32_t in_size,
uint8_t *out, uint32_t out_size,
enum trans_stream_error *error)
{
void *rdata;
bool ret;
uint32_t rd, wn;
if (data && *data)
{
rdata = *data;
}
else
{
rdata = backend->stream_new();
if (!rdata)
{
if (error)
*error = TRANS_STREAM_ERROR_ALLOCATION_FAILURE;
return false;
}
}
backend->set_in(rdata, in, in_size);
backend->set_out(rdata, out, out_size);
ret = backend->trans(rdata, true, &rd, &wn, error);
if (data)
*data = rdata;
else
backend->stream_free(rdata);
return ret;
}
const struct trans_stream_backend* trans_stream_get_zlib_deflate_backend(void)
{
#if HAVE_ZLIB
return &zlib_deflate_backend;
#else
return NULL;
#endif
}
const struct trans_stream_backend* trans_stream_get_zlib_inflate_backend(void)
{
#if HAVE_ZLIB
return &zlib_inflate_backend;
#else
return NULL;
#endif
}
const struct trans_stream_backend* trans_stream_get_pipe_backend(void)
{
return &pipe_backend;
}

View File

@ -0,0 +1,94 @@
/* Copyright (C) 2016 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (trans_stream_zlib.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <streams/trans_stream.h>
struct pipe_trans_stream
{
const uint8_t *in;
uint8_t *out;
uint32_t in_size, out_size;
};
static void *pipe_stream_new(void)
{
return (struct pipe_trans_stream*)calloc(1, sizeof(struct pipe_trans_stream));
}
static void pipe_stream_free(void *data)
{
free(data);
}
static void pipe_set_in(void *data, const uint8_t *in, uint32_t in_size)
{
struct pipe_trans_stream *p = (struct pipe_trans_stream *) data;
p->in = in;
p->in_size = in_size;
}
static void pipe_set_out(void *data, uint8_t *out, uint32_t out_size)
{
struct pipe_trans_stream *p = (struct pipe_trans_stream *) data;
p->out = out;
p->out_size = out_size;
}
static bool pipe_trans(
void *data, bool flush,
uint32_t *rd, uint32_t *wn,
enum trans_stream_error *error)
{
struct pipe_trans_stream *p = (struct pipe_trans_stream *) data;
if (p->out_size < p->in_size)
{
memcpy(p->out, p->in, p->out_size);
*rd = *wn = p->out_size;
p->in += p->out_size;
p->out += p->out_size;
*error = TRANS_STREAM_ERROR_BUFFER_FULL;
return false;
}
else
{
memcpy(p->out, p->in, p->in_size);
*rd = *wn = p->in_size;
p->in += p->in_size;
p->out += p->in_size;
*error = TRANS_STREAM_ERROR_NONE;
return true;
}
}
const struct trans_stream_backend pipe_backend = {
"pipe",
&pipe_backend,
pipe_stream_new,
pipe_stream_free,
pipe_set_in,
pipe_set_out,
pipe_trans
};

View File

@ -0,0 +1,199 @@
/* Copyright (C) 2016 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (trans_stream_zlib.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <compat/zlib.h>
#include <streams/trans_stream.h>
struct zlib_trans_stream
{
z_stream z;
bool inited;
};
static void *zlib_stream_new(void)
{
return (struct zlib_trans_stream*)calloc(1, sizeof(struct zlib_trans_stream));
}
static void zlib_deflate_stream_free(void *data)
{
struct zlib_trans_stream *z = (struct zlib_trans_stream *) data;
if (z->inited)
deflateEnd(&z->z);
free(z);
}
static void zlib_inflate_stream_free(void *data)
{
struct zlib_trans_stream *z = (struct zlib_trans_stream *) data;
if (z->inited)
inflateEnd(&z->z);
free(z);
}
static void zlib_deflate_set_in(void *data, const uint8_t *in, uint32_t in_size)
{
struct zlib_trans_stream *z = (struct zlib_trans_stream *) data;
z->z.next_in = (uint8_t *) in;
z->z.avail_in = in_size;
if (!z->inited)
{
deflateInit(&z->z, Z_DEFAULT_COMPRESSION);
z->inited = true;
}
}
static void zlib_inflate_set_in(void *data, const uint8_t *in, uint32_t in_size)
{
struct zlib_trans_stream *z = (struct zlib_trans_stream *) data;
z->z.next_in = (uint8_t *) in;
z->z.avail_in = in_size;
if (!z->inited)
{
deflateInit(&z->z, Z_DEFAULT_COMPRESSION);
z->inited = true;
}
}
static void zlib_set_out(void *data, uint8_t *out, uint32_t out_size)
{
struct zlib_trans_stream *z = (struct zlib_trans_stream *) data;
z->z.next_out = out;
z->z.avail_out = out_size;
}
static bool zlib_deflate_trans(
void *data, bool flush,
uint32_t *rd, uint32_t *wn,
enum trans_stream_error *error)
{
int ret;
uint32_t pre_avail_in, pre_avail_out;
struct zlib_trans_stream *zt = (struct zlib_trans_stream *) data;
z_stream *z = &zt->z;
if (!zt->inited)
{
deflateInit(z, Z_DEFAULT_COMPRESSION);
zt->inited = true;
}
pre_avail_in = z->avail_in;
pre_avail_out = z->avail_out;
ret = deflate(z, flush ? Z_FINISH : Z_NO_FLUSH);
if (ret != Z_OK && ret != Z_STREAM_END)
{
if (error)
*error = TRANS_STREAM_ERROR_OTHER;
return false;
}
*error = TRANS_STREAM_ERROR_NONE;
if (z->avail_out == 0)
{
/* Filled buffer, maybe an error */
if (z->avail_in != 0)
*error = TRANS_STREAM_ERROR_BUFFER_FULL;
}
*rd = z->avail_in - pre_avail_in;
*wn = z->avail_out - pre_avail_out;
if (flush && !*error)
{
deflateEnd(z);
zt->inited = false;
}
return !*error;
}
static bool zlib_inflate_trans(
void *data, bool flush,
uint32_t *rd, uint32_t *wn,
enum trans_stream_error *error)
{
int ret;
uint32_t pre_avail_in, pre_avail_out;
struct zlib_trans_stream *zt = (struct zlib_trans_stream *) data;
z_stream *z = &zt->z;
if (!zt->inited)
{
inflateInit(z);
zt->inited = true;
}
pre_avail_in = z->avail_in;
pre_avail_out = z->avail_out;
ret = inflate(z, flush ? Z_FINISH : Z_NO_FLUSH);
if (ret != Z_OK && ret != Z_STREAM_END)
{
if (error)
*error = TRANS_STREAM_ERROR_OTHER;
return false;
}
*error = TRANS_STREAM_ERROR_NONE;
if (z->avail_out == 0)
{
/* Filled buffer, maybe an error */
if (z->avail_in != 0)
*error = TRANS_STREAM_ERROR_BUFFER_FULL;
}
*rd = z->avail_in - pre_avail_in;
*wn = z->avail_out - pre_avail_out;
if (flush && !*error)
{
inflateEnd(z);
zt->inited = false;
}
return !*error;
}
const struct trans_stream_backend zlib_deflate_backend = {
"zlib_deflate",
&zlib_inflate_backend,
zlib_stream_new,
zlib_deflate_stream_free,
zlib_deflate_set_in,
zlib_set_out,
zlib_deflate_trans
};
const struct trans_stream_backend zlib_inflate_backend = {
"zlib_inflate",
&zlib_deflate_backend,
zlib_stream_new,
zlib_inflate_stream_free,
zlib_inflate_set_in,
zlib_set_out,
zlib_inflate_trans
};