mirror of
https://git.code.sf.net/p/fuse-emulator/fuse-utils
synced 2025-04-19 08:42:15 +03:00
Extract compressed block reading routines
This commit is contained in:
parent
b9618c45cb
commit
925833a699
@ -53,6 +53,7 @@ AM_CPPFLAGS = $(LIBSPECTRUM_CFLAGS) $(AUDIOFILE_CFLAGS) $(GLIB_CFLAGS)
|
||||
createhdf_SOURCES = ide.c createhdf.c
|
||||
|
||||
fmfconv_SOURCES = fmfconv.c \
|
||||
fmfconv_compr.c \
|
||||
fmfconv_output/fmfconv_aiff.c \
|
||||
fmfconv_output/fmfconv_au.c \
|
||||
fmfconv_output/fmfconv_avi.c \
|
||||
@ -127,7 +128,7 @@ noinst_HEADERS = compat.h ide.h utils.h audio2tape.h importer/interpolator.h \
|
||||
converter/findsync1.h converter/getpulse1.h \
|
||||
converter/getpulse2.h converter/getsync2.h \
|
||||
converter/romloader.h converter/romloaderstate.h \
|
||||
compat/getopt.h fmfconv.h movie_tables.h
|
||||
compat/getopt.h fmfconv.h fmfconv_compr.h movie_tables.h
|
||||
|
||||
noinst_LIBRARIES =
|
||||
|
||||
|
92
fmfconv.c
92
fmfconv.c
@ -55,6 +55,7 @@
|
||||
|
||||
#include "compat.h"
|
||||
#include "fmfconv.h"
|
||||
#include "fmfconv_compr.h"
|
||||
#include "movie_tables.h"
|
||||
|
||||
#define PROGRAM_NAME "fmfconv"
|
||||
@ -64,15 +65,6 @@
|
||||
|
||||
const char *progname;
|
||||
|
||||
#ifdef USE_ZLIB
|
||||
#define ZBUF_SIZE 1024
|
||||
#define ZBUF_INP_SIZE 512
|
||||
z_stream zstream; /* zlib struct */
|
||||
unsigned char zbuf_o[ZBUF_SIZE]; /* zlib output buffer */
|
||||
unsigned char zbuf_i[ZBUF_INP_SIZE]; /* zlib input buffer */
|
||||
int fmf_compr_feof = 0;
|
||||
#endif /* USE_ZLIB */
|
||||
|
||||
#ifdef USE_LIBJPEG
|
||||
extern int jpg_dctfloat;
|
||||
extern int jpg_idctfast;
|
||||
@ -207,7 +199,6 @@ int out_header_ok = 0; /* output header ok? */
|
||||
int out_chn = 2, out_rte = -1, out_fsz, out_len; /* by default convert sound to 2 channel (STEREO) */
|
||||
|
||||
/* fmf variables */
|
||||
int fmf_compr = 0; /* fmf compressed or not */
|
||||
int fmf_little_endian = 0; /* little endian PCM */
|
||||
int snd_little_endian = -1; /* little endian PCM */
|
||||
libspectrum_qword fmf_slice_no = 0;
|
||||
@ -294,77 +285,6 @@ fread_buff( libspectrum_byte *ptr, size_t size, int how ) /* read from inp, how
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifdef USE_ZLIB
|
||||
static int
|
||||
feof_compr( FILE *f )
|
||||
{
|
||||
if( fmf_compr ) {
|
||||
if( fmf_compr_feof )
|
||||
return 1;
|
||||
if( zstream.avail_in > 0 || zstream.avail_out != ZBUF_SIZE )
|
||||
return 0;
|
||||
fmf_compr_feof = feof( f );
|
||||
return fmf_compr_feof;
|
||||
} else {
|
||||
return feof( f );
|
||||
}
|
||||
}
|
||||
|
||||
int fget_read = 0;
|
||||
|
||||
static int
|
||||
fgetc_compr( FILE *f )
|
||||
{
|
||||
int s;
|
||||
|
||||
if( fmf_compr ) {
|
||||
if( zstream.avail_out == ZBUF_SIZE ) {
|
||||
fget_read++;
|
||||
zstream.next_out = zbuf_o;
|
||||
do {
|
||||
if( zstream.avail_in == 0 && !fmf_compr_feof ) {
|
||||
zstream.avail_in = fread( zbuf_i, 1, ZBUF_INP_SIZE, inp_file );
|
||||
zstream.next_in = zbuf_i;
|
||||
if( zstream.avail_in == 0 )
|
||||
fmf_compr_feof = 1;
|
||||
}
|
||||
s = inflate( &zstream, fmf_compr_feof ? Z_FINISH : Z_SYNC_FLUSH );
|
||||
} while ( zstream.avail_out != 0 && s == Z_OK );
|
||||
zstream.next_out = zbuf_o;
|
||||
}
|
||||
if( zstream.avail_out == ZBUF_SIZE ) /* end of file */
|
||||
return -1;
|
||||
zstream.avail_out++;
|
||||
return *( zstream.next_out++ ); /* !!! we use it for own purpose */
|
||||
} else {
|
||||
return fgetc( f );
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
fread_compr( void *buff, size_t n, size_t m, FILE *f )
|
||||
{
|
||||
size_t i;
|
||||
int d;
|
||||
char *b = buff;
|
||||
|
||||
if( fmf_compr ) {
|
||||
for( i = n * m; i > 0; i-- ) {
|
||||
if( ( d = fgetc_compr( f ) ) == -1 ) /****FIXME may too slow */
|
||||
return ( n * m - i ) / n;
|
||||
*b++ = d;
|
||||
}
|
||||
return m;
|
||||
} else {
|
||||
return fread( b, n, m, f );
|
||||
}
|
||||
}
|
||||
#else /* USE_ZLIB */
|
||||
#define fgetc_compr fgetc
|
||||
#define fread_compr fread
|
||||
#define feof_compr feof
|
||||
#endif /* USE_ZLIB */
|
||||
|
||||
libspectrum_dword
|
||||
swap_endian_dword( libspectrum_dword d )
|
||||
{
|
||||
@ -2183,13 +2103,7 @@ main( int argc, char *argv[] )
|
||||
else if( sound_stereo == 0 ) out_chn = 1;
|
||||
|
||||
#ifdef USE_ZLIB
|
||||
zstream.zalloc = Z_NULL;
|
||||
zstream.zfree = Z_NULL;
|
||||
zstream.opaque = Z_NULL;
|
||||
zstream.avail_in = 0;
|
||||
zstream.next_in = Z_NULL;
|
||||
inflateInit( &zstream );
|
||||
zstream.avail_out = ZBUF_SIZE;
|
||||
fmf_compr_init();
|
||||
#endif /* USE_ZLIB */
|
||||
|
||||
#ifdef HAVE_SIGNAL
|
||||
@ -2266,7 +2180,7 @@ main( int argc, char *argv[] )
|
||||
}
|
||||
|
||||
#ifdef USE_ZLIB
|
||||
inflateEnd( &zstream );
|
||||
fmf_compr_end();
|
||||
#endif /* USE_ZLIB */
|
||||
|
||||
if( ( out_t >= TYPE_SCR && out_t <= TYPE_JPEG ) && out_name ) unlink( out_name );
|
||||
|
131
fmfconv_compr.c
Normal file
131
fmfconv_compr.c
Normal file
@ -0,0 +1,131 @@
|
||||
/* fmfconv_compr.c: Convert .fmf movie files - zlib compression routines
|
||||
Copyright (c) 2004-2015 Gergely Szasz
|
||||
Copyright (c) 2014-2016 Sergio Baldovi
|
||||
|
||||
This program 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 program 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 program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
Author contact information:
|
||||
|
||||
E-mail: szaszg@hu.inter.net
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "fmfconv_compr.h"
|
||||
|
||||
#ifdef HAVE_ZLIB_H
|
||||
#include <zlib.h>
|
||||
|
||||
#define ZBUF_SIZE 1024
|
||||
#define ZBUF_INP_SIZE 512
|
||||
z_stream zstream; /* zlib struct */
|
||||
unsigned char zbuf_o[ZBUF_SIZE]; /* zlib output buffer */
|
||||
unsigned char zbuf_i[ZBUF_INP_SIZE]; /* zlib input buffer */
|
||||
int fmf_compr_feof = 0;
|
||||
#endif
|
||||
|
||||
/* fmf variables */
|
||||
int fmf_compr = 0; /* fmf compressed or not */
|
||||
|
||||
#ifdef HAVE_ZLIB_H
|
||||
void
|
||||
fmf_compr_init( void )
|
||||
{
|
||||
zstream.zalloc = Z_NULL;
|
||||
zstream.zfree = Z_NULL;
|
||||
zstream.opaque = Z_NULL;
|
||||
zstream.avail_in = 0;
|
||||
zstream.next_in = Z_NULL;
|
||||
inflateInit( &zstream );
|
||||
zstream.avail_out = ZBUF_SIZE;
|
||||
}
|
||||
|
||||
void
|
||||
fmf_compr_end( void )
|
||||
{
|
||||
inflateEnd( &zstream );
|
||||
}
|
||||
|
||||
int
|
||||
feof_compr( FILE *f )
|
||||
{
|
||||
if( fmf_compr ) {
|
||||
if( fmf_compr_feof )
|
||||
return 1;
|
||||
if( zstream.avail_in > 0 || zstream.avail_out != ZBUF_SIZE )
|
||||
return 0;
|
||||
fmf_compr_feof = feof( f );
|
||||
return fmf_compr_feof;
|
||||
} else {
|
||||
return feof( f );
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
fgetc_compr( FILE *f )
|
||||
{
|
||||
int s;
|
||||
|
||||
if( fmf_compr ) {
|
||||
if( zstream.avail_out == ZBUF_SIZE ) {
|
||||
zstream.next_out = zbuf_o;
|
||||
do {
|
||||
if( zstream.avail_in == 0 && !fmf_compr_feof ) {
|
||||
zstream.avail_in = fread( zbuf_i, 1, ZBUF_INP_SIZE, f );
|
||||
zstream.next_in = zbuf_i;
|
||||
if( zstream.avail_in == 0 )
|
||||
fmf_compr_feof = 1;
|
||||
}
|
||||
s = inflate( &zstream, fmf_compr_feof ? Z_FINISH : Z_SYNC_FLUSH );
|
||||
} while ( zstream.avail_out != 0 && s == Z_OK );
|
||||
zstream.next_out = zbuf_o;
|
||||
}
|
||||
if( zstream.avail_out == ZBUF_SIZE ) /* end of file */
|
||||
return -1;
|
||||
zstream.avail_out++;
|
||||
return *( zstream.next_out++ ); /* !!! we use it for own purpose */
|
||||
} else {
|
||||
return fgetc( f );
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
fread_compr( void *buff, size_t n, size_t m, FILE *f )
|
||||
{
|
||||
size_t i;
|
||||
int d;
|
||||
char *b = buff;
|
||||
|
||||
if( fmf_compr ) {
|
||||
for( i = n * m; i > 0; i-- ) {
|
||||
if( ( d = fgetc_compr( f ) ) == -1 ) /****FIXME may too slow */
|
||||
return ( n * m - i ) / n;
|
||||
*b++ = d;
|
||||
}
|
||||
return m;
|
||||
} else {
|
||||
return fread( b, n, m, f );
|
||||
}
|
||||
}
|
||||
#endif /* HAVE_ZLIB_H */
|
||||
|
||||
int
|
||||
fget_word(FILE *input)
|
||||
{
|
||||
return fgetc_compr(input) + (fgetc_compr(input) << 8);
|
||||
}
|
47
fmfconv_compr.h
Normal file
47
fmfconv_compr.h
Normal file
@ -0,0 +1,47 @@
|
||||
/* fmfconv_compr.h: Convert .fmf movie files - zlib compression routines
|
||||
Copyright (c) 2004-2015 Gergely Szasz
|
||||
Copyright (c) 2014-2016 Sergio Baldovi
|
||||
|
||||
This program 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 program 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 program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
Author contact information:
|
||||
|
||||
E-mail: szaszg@hu.inter.net
|
||||
*/
|
||||
|
||||
#ifndef FMFCONV_COMPR_H
|
||||
#define FMFCONV_COMPR_H
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef HAVE_ZLIB_H
|
||||
int feof_compr( FILE *f );
|
||||
int fgetc_compr( FILE *f );
|
||||
int fread_compr( void *buff, size_t n, size_t m, FILE *f );
|
||||
void fmf_compr_init( void );
|
||||
void fmf_compr_end( void );
|
||||
#else /* HAVE_ZLIB_H */
|
||||
#define fgetc_compr fgetc
|
||||
#define fread_compr fread
|
||||
#define feof_compr feof
|
||||
#endif /* HAVE_ZLIB_H */
|
||||
|
||||
int fget_word(FILE *input);
|
||||
|
||||
extern int fmf_compr; /* fmf compressed or not */
|
||||
|
||||
#endif /* #ifndef FMFCONV_COMPR_H */
|
Loading…
x
Reference in New Issue
Block a user