From cdbb43ea328883261c6f71e6e44b16ae39173fa6 Mon Sep 17 00:00:00 2001 From: Pixel Date: Fri, 21 Jun 2002 23:45:51 +0000 Subject: Still working... --- Makefile | 33 +-- dteutils.cpp | 47 ----- lzss.cpp | 361 -------------------------------- lzss.h | 3 +- psxdev/Makefile | 10 + psxdev/bs.c | 348 +++++++++++++++++++++++++++++++ psxdev/bs.h | 91 +++++++++ psxdev/common.h | 47 +++++ psxdev/idctfst.c | 287 ++++++++++++++++++++++++++ psxdev/jfdctint.c | 291 ++++++++++++++++++++++++++ psxdev/table.h | 102 +++++++++ psxdev/vlc.c | 602 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ str-util.cpp | 178 +--------------- yazedc.cpp | 159 -------------- 14 files changed, 1799 insertions(+), 760 deletions(-) create mode 100644 psxdev/Makefile create mode 100644 psxdev/bs.c create mode 100644 psxdev/bs.h create mode 100644 psxdev/common.h create mode 100644 psxdev/idctfst.c create mode 100644 psxdev/jfdctint.c create mode 100644 psxdev/table.h create mode 100644 psxdev/vlc.c diff --git a/Makefile b/Makefile index 7a41281..6f57c0e 100755 --- a/Makefile +++ b/Makefile @@ -3,30 +3,33 @@ CPPFLAGS=-Wall -g -O3 -mcpu=i686 -pedantic -pedantic-errors -Werror CXX=g++ -TARGET = lzss dlzss yazedc cd-tool dte-tool str-tool +SUBDIRS = psxdev +TARGET = lzss dlzss cd-tool str-tool -all: ${TARGET} +all: subdirs ${TARGET} -lzss: lzss.cpp lzss.h generic.cpp generic.h fileutils.h fileutils.cpp - ${CXX} ${CPPFLAGS} ${LDFLAGS} -DLZSS_MAIN lzss.cpp generic.cpp fileutils.cpp -o lzss +subdirs: + for d in ${SUBDIRS} ; do (cd $$d ; make all) ; done + +lzss: lzss.o lzss.h generic.o generic.h fileutils.h fileutils.o lzss-main.o + ${CXX} ${LDFLAGS} -DLZSS_MAIN lzss.o generic.o fileutils.o lzss-main.o -o lzss dlzss: lzss ln -fs lzss dlzss -yazedc: yazedc.cpp crctables crctable.out - ${CXX} ${CPPFLAGS} ${LDFLAGS} yazedc.cpp -DMAIN -o yazedc - -cd-tool: cd-tool.cpp cdutils.cpp cdutils.h fileutils.cpp fileutils.h generic.cpp generic.h yazedc.cpp yazedc.h - ${CXX} ${CPPFLAGS} ${LDFLAGAS} cd-tool.cpp cdutils.cpp fileutils.cpp yazedc.cpp generic.cpp -o cd-tool +yazedc: yazedc.o crctables crctable.out yazedc-main.o + ${CXX} ${CPPFLAGS} ${LDFLAGS} yazedc.o yazedc-main.o -DMAIN -o yazedc -dte-tool-asm: dteutils.cpp generic.h generic.cpp fileutils.cpp fileutils.h dte-asm.S - ${CXX} ${CPPFLAGS} ${LDFLAGS} dteutils.cpp generic.cpp fileutils.cpp dte-asm.S -o dte-tool-asm -DUSEASM -DDTEMAIN +cd-tool: cd-tool.o cdutils.o cdutils.h fileutils.o fileutils.h generic.o generic.h yazedc.o yazedc.h + ${CXX} ${oFLAGS} ${LDFLAGAS} cd-tool.o cdutils.o fileutils.o yazedc.o generic.o -o cd-tool -dte-tool: dteutils.cpp generic.h generic.cpp fileutils.cpp fileutils.h - ${CXX} ${CPPFLAGS} ${LDFLAGS} dteutils.cpp generic.cpp fileutils.cpp -o dte-tool -DDTEMAIN +dte-tool: dteutils.o generic.h generic.o fileutils.o fileutils.h dtemain.o + ${CXX} ${oFLAGS} ${LDFLAGS} dteutils.o generic.o fileutils.o dtemain.o -o dte-tool -str-tool: str-util.cpp generic.h generic.cpp fileutils.cpp fileutils.h cdutils.cpp cdutils.h yazedc.cpp yazedc.h - ${CXX} ${CPPFLAGS} ${LDFLAGS} str-util.cpp generic.cpp fileutils.cpp cdutils.cpp yazedc.cpp -o str-tool -DSTR_MAIN +str-tool: str-util.o generic.h generic.o fileutils.o fileutils.h cdutils.o cdutils.h yazedc.o yazedc.h + ${CXX} ${oFLAGS} ${LDFLAGS} str-util.o generic.o fileutils.o cdutils.o yazedc.o psxdev/bs.o psxdev/idctfst.o psxdev/jfdctint.o psxdev/vlc.o -o str-tool clean: rm -f *.o ${TARGET} compil.c + for d in ${SUBDIRS} ; do (cd $$d ; make clean) ; done + diff --git a/dteutils.cpp b/dteutils.cpp index 7c5057d..4ae8668 100644 --- a/dteutils.cpp +++ b/dteutils.cpp @@ -340,50 +340,3 @@ void read_thingy_file(FILE * f) { dte_text[ptr] = dte_text[ptr + 1] = dte_text[ptr + 2] = dte_text[ptr + 3] = 0; dte_text_size = ptr; } - -#ifdef DTEMAIN - -int main(int argc, char ** argv) { - FILE * f, * t; - long old_size; - int i; - - verbosity = M_INFO; - - printm(M_STATUS, "Reading thingy table\n"); - t = fopen(argv[2], "r"); - read_thingy(t); - fclose(t); - - f = fopen(argv[1], "r"); - dte_text_size = filesize(f); - dte_text = (char *) calloc(dte_text_size + 4, 1); - printm(M_STATUS, "Reading file, size = %li\n", dte_text_size); - read_thingy_file(f); - fclose(f); - - printm(M_STATUS, "True size = %li\n", old_size = dte_text_size); - - printm(M_STATUS, "Compressing file.\n"); - dte_compress(); - - printm(M_STATUS, "Rereading file.\n"); - f = fopen(argv[1], "r"); - dte_text_size = filesize(f); - dte_text = (char *) calloc(dte_text_size + 4, 1); - read_thingy_file(f); - fclose(f); - - printm(M_STATUS, "True size = %li, real gain = %li\n", dte_text_size, old_size - dte_text_size); - - printm(M_INFO, "DTE Usage:\n"); - for (i = 0; i < 256; i++) { - printm(M_INFO, "Entry %i ('%s') used at %i\n", i, things[i], dte_usage[i]); - } - - printm(M_INFO, "Number of couples: %i\n", tnb_dte); - - free(dte_text); -} - -#endif diff --git a/lzss.cpp b/lzss.cpp index 3e50095..4f9e424 100644 --- a/lzss.cpp +++ b/lzss.cpp @@ -436,364 +436,3 @@ void lzss_comp(FILE * f_source, FILE * f_cible, long * delta) { free(c); free(r); } - -#ifdef LZSS_MAIN - -char * fn1, * fn2, * pname; - -int lga = 0; -int compress = 1; - -struct option long_options[] = { - {"1iscomp", 1, &lga, 1 }, - {"overlap", 1, &lga, 2 }, - {"16bits", 1, &lga, 11 }, - {"negative", 1, &lga, 12 }, - {"ptrb", 1, &lga, 13 }, - {"filling", 1, &lga, 14 }, - {"inverse", 1, &lga, 23 }, - {"onejump", 1, &lga, 24 }, - {"window", 1, &lga, 25 }, - {"lmask1", 1, &lga, 3 }, - {"lshft1", 1, &lga, 4 }, - {"lmask2", 1, &lga, 5 }, - {"lshft2", 1, &lga, 6 }, - {"jmask1", 1, &lga, 7 }, - {"jshft1", 1, &lga, 8 }, - {"jmask2", 1, &lga, 9 }, - {"jshft2", 1, &lga, 10 }, - {"fmask1", 1, &lga, 15 }, - {"fshft1", 1, &lga, 16 }, - {"fmask2", 1, &lga, 17 }, - {"fshft2", 1, &lga, 18 }, - {"vmask1", 1, &lga, 19 }, - {"vshft1", 1, &lga, 20 }, - {"vmask2", 1, &lga, 21 }, - {"vshft2", 1, &lga, 22 }, - {"help", 0, NULL, 'h'}, - {"scheme", 1, NULL, 's'}, - {"length", 1, NULL, 'l'}, - {"version", 0, NULL, 'V'}, - {"verbose", 0, NULL, 'v'}, - {"show", 0, NULL, 'S'}, - {"dump", 0, NULL, 'D'}, - {"compress", 0, NULL, 'c'}, - {"decompress", 0, NULL, 'd'}, - {"blocks", 0, NULL, 'b'}, - {0, 0, NULL, 0 } -}; - -void showhelp(void) { - printm(M_BARE, -"Usages:\n" -" %s <--help|-h|-H|-?> Show this help and exit.\n" -" %s <--dump> Show the built-in schemes and exit.\n" -" %s <--version|-V> Show the version and copyrights and exit.\n" -" %s [-c|-d] [-s ] [-l ] [-S] [-v] \n" -"\n" -"-c --compress Compress to \n" -"-d --decompress Decompress to \n" -"-s --scheme= Loads the built-in scheme number \n" -"-l --length= Specify the true length for decompression,\n" -" or enable the padding behavior for compression.\n" -"-S --show Show the actual scheme before processing.\n" -"-v --verbose Display a *LOT* of informations.\n" -"-b --blocks Switch to blocks decompression behaviour\n" -"\n" -"Additionnaly you have the scheme manipulation options:\n" -"--1iscomp --overlap --negative --16bits --ptrb\n" -"--filling --inverse --onejump --window\n" -"--lmask1 --lshft1 --lmask2 --lshft2 --jmask1 --jshft1 --jmask2 --jshft2\n" -"--vmask1 --vshft1 --vmask2 --vshft2 --fmask1 --fshft1 --fmask2 --fshft2\n" -"\n" -"If you don't know what they are, forget them.\n" -"\n" -"Default behavior is to %scompress.\n" -"\n", pname, pname, pname, pname, compress ? "" : "de"); -} - -void showscheme(void) { - printm(M_BARE, -"Actual scheme:\n" -"--1iscomp %i\n" -"--overlap %i\n" -"--negative %i\n" -"--16bits %i\n" -"--ptrb %i\n" -"--filling %i\n" -"--inverse %i\n" -"--onejump %i\n" -"--window %i\n" -"--lmask1 0x%02x\n" -"--lshft1 %i\n" -"--lmask2 0x%02x\n" -"--lshft2 %i\n" -"--jmask1 0x%02x\n" -"--jshft1 %i\n" -"--jmask2 0x%02x\n" -"--jshft2 %i\n" -"--fmask1 0x%02x\n" -"--fshft1 %i\n" -"--fmask2 0x%02x\n" -"--fshft2 %i\n" -"--vmask1 0x%02x\n" -"--vshft1 %i\n" -"--vmask2 0x%02x\n" -"--vshft2 %i\n" -"\n", scheme.one_is_compressed, scheme.overlap_trick, scheme.negative_trick, scheme.sixteen_bits, -scheme.ptrb, scheme.filling, scheme.bitmap_inversed, scheme.one_jump, scheme.window_start, -scheme.l_mask_1, scheme.l_shft_1, scheme.l_mask_2, scheme.l_shft_2, -scheme.j_mask_1, scheme.j_shft_1, scheme.j_mask_2, scheme.j_shft_2, -scheme.f_mask_1, scheme.f_shft_1, scheme.f_mask_2, scheme.f_shft_2, -scheme.v_mask_1, scheme.v_shft_1, scheme.v_mask_2, scheme.v_shft_2); -} - -void dump(void) { - int i; - - printm(M_BARE, "Built-in schemes:\n"); - for (i = 0; schemes[i].name; i++) { - printm(M_BARE, "%2i - %s\n", i, schemes[i].name); - } -} - -int main(int argc, char ** argv) { - long length = -1; - FILE * f1, * f2; - int p, show = 0; - int c, s, t; - - pname = strdup(argv[0]); - p = strlen(pname) - 5; - - verbosity = M_STATUS; - - if (!strcasecmp(pname + p, "dlzss")) { - compress = 0; - } - - printm(M_BARE, /* -LZSS_MAIN " compressor/decompressor version " LZSS_VERSION ",\n" */ -"Copyright (C) 2002 Nicolas \"Pixel\" Noble\n" -"This software comes with ABSOLUTELY NO WARRANTY; see COPYING for details\n" -"Thanks to Czar Dragon, for his little 'lzss' schemes FAQ.\n" -"Special thanks to Yazoo, who taught me PSX hacking.\n" -"\n"); - - while ((c = getopt_long(argc, argv, "Hhs:l:vVScdb", long_options, NULL)) != EOF) { - switch (c) { - case 0: - switch (lga) { - case 1: - t = atoi(optarg); - if ((t != 1) && (t != 0)) { - printm(M_ERROR, "Invalid value for boolean: %s\n", optarg); - } else { - scheme.one_is_compressed = t; - } - break; - case 2: - t = atoi(optarg); - if ((t != 1) && (t != 0)) { - printm(M_ERROR, "Invalid value for boolean: %s\n", optarg); - } else { - scheme.overlap_trick = t; - } - break; - case 3: - sscanf(optarg, "%i", &scheme.l_mask_1); - break; - case 4: - scheme.l_shft_1 = atoi(optarg); - break; - case 5: - sscanf(optarg, "%i", &scheme.l_mask_2); - break; - case 6: - scheme.l_shft_2 = atoi(optarg); - break; - case 7: - sscanf(optarg, "%i", &scheme.j_mask_1); - break; - case 8: - scheme.j_shft_1 = atoi(optarg); - break; - case 9: - sscanf(optarg, "%i", &scheme.j_mask_2); - break; - case 10: - scheme.j_mask_2 = atoi(optarg); - break; - case 11: - t = atoi(optarg); - if ((t != 1) && (t != 0)) { - printm(M_ERROR, "Invalid value for boolean: %s\n", optarg); - } else { - scheme.sixteen_bits = t; - } - break; - case 12: - t = atoi(optarg); - if ((t != 1) && (t != 0)) { - printm(M_ERROR, "Invalid value for boolean: %s\n", optarg); - } else { - scheme.negative_trick = t; - } - break; - case 13: - t = atoi(optarg); - if ((t != 2) && (t != 1) && (t != 0)) { - printm(M_ERROR, "Invalid value for ter: %s\n", optarg); - } else { - scheme.ptrb = t; - } - break; - case 14: - t = atoi(optarg); - if ((t != 2) && (t != 1) && (t != 0)) { - printm(M_ERROR, "Invalid value for ter: %s\n", optarg); - } else { - scheme.filling = t; - } - break; - case 15: - sscanf(optarg, "%i", &scheme.f_mask_1); - break; - case 16: - scheme.f_shft_1 = atoi(optarg); - break; - case 17: - sscanf(optarg, "%i", &scheme.f_mask_2); - break; - case 18: - scheme.f_shft_2 = atoi(optarg); - break; - case 19: - sscanf(optarg, "%i", &scheme.v_mask_1); - break; - case 20: - scheme.v_shft_1 = atoi(optarg); - break; - case 21: - sscanf(optarg, "%i", &scheme.v_mask_2); - break; - case 22: - scheme.v_mask_2 = atoi(optarg); - break; - case 23: - t = atoi(optarg); - if ((t != 1) && (t != 0)) { - printm(M_ERROR, "Invalid value for boolean: %s\n", optarg); - } else { - scheme.bitmap_inversed = t; - } - break; - case 24: - t = atoi(optarg); - if ((t != 1) && (t != 0)) { - printm(M_ERROR, "Invalid value for boolean: %s\n", optarg); - } else { - scheme.one_jump = t; - } - break; - case 25: - t = sscanf(optarg, "%i", &scheme.window_start); - break; - default: - showhelp(); - printm(M_ERROR, "Unknow option.\n"); - exit(-1); - } - break; - case '?': - case 'H': - case 'h': - showhelp(); - exit(0); - case 's': - s = atoi(optarg); - scheme = schemes[s]; - break; - case 'l': - length = atoi(optarg); - break; - case 'v': - verbosity = M_INFO; - break; - case 'V': - exit(0); - case 'S': - show = 1; - break; - case 'D': - dump(); - exit(0); - break; - case 'd': - compress = 0; - break; - case 'c': - compress = 1; - break; - case 'b': - blockb = 1; - break; - default: - showhelp(); - printm(M_ERROR, "Unknow option.\n"); - exit(-1); - } - } - - if (show) showscheme(); - - if (optind != (argc - 2)) { - if (optind > (argc - 2)) { - printm(M_ERROR, "Not enough filenames\n"); - exit(-1); - } else { - printm(M_ERROR, "Too much arguments\n"); - exit(-1); - } - } - - fn1 = argv[optind++]; - fn2 = argv[optind++]; - - if (!(f1 = fopen(fn1, "r"))) { - printm(M_ERROR, "Error opening file %s for reading.\n", fn1); - } - - if (!(f2 = fopen(fn2, "w"))) { - printm(M_ERROR, "Error opening file %s for writing.\n", fn2); - } - - if (compress) { - printm(M_STATUS, "Compressing `%s' to `%s'...\n", fn1, fn2); - } else { - printm(M_STATUS, "Decompressing `%s' to `%s'...\n", fn1, fn2); - } - - if (compress) { - if (length == -1) { - lzss_comp(f1, f2); - } else { - lzss_comp(f1, f2, &length); - } - } else { - length = lzss_decomp(f1, f2, length); - } - - printm(M_STATUS, "Done, filesize changed from %i to %i.\n", filesize(f1), filesize(f2)); - if (!bitmap_count) - bitmap_count = 8; - if (compress) - printm(M_STATUS, "Compressed %i = 0x%08x blocs, containing %i = 0x%08x chunks.\n", blk + 1, blk + 1, blk * 8 + bitmap_count, blk * 8 + bitmap_count); - - fclose(f1); - fclose(f2); - - exit(0); -} - -#endif diff --git a/lzss.h b/lzss.h index c0d05b8..a4fc71f 100644 --- a/lzss.h +++ b/lzss.h @@ -52,7 +52,8 @@ enum { extern scheme_t scheme, schemes[]; -extern int tolerate; +extern int tolerate, blockb; +extern long blk, bitmap_count; unsigned long lzss_decomp(FILE * f_source, FILE * f_cible, long true_length = -1); void lzss_comp(FILE * f_source, FILE * f_cible, long * delta = NULL); diff --git a/psxdev/Makefile b/psxdev/Makefile new file mode 100644 index 0000000..3db2a0f --- /dev/null +++ b/psxdev/Makefile @@ -0,0 +1,10 @@ +TARGETS=bs.o idctfst.o jfdctint.o vlc.o + +CC=gcc +CPPFLAGS=-march=i386 -O3 + +all: $(TARGETS) + +clean: + rm -f *.o + diff --git a/psxdev/bs.c b/psxdev/bs.c new file mode 100644 index 0000000..5d7e185 --- /dev/null +++ b/psxdev/bs.c @@ -0,0 +1,348 @@ +/* + (c)2000 by BERO bero@geocities.co.jp + + under GPL + + some changes by dbalster@psxdev.de + + - all globals now in a context (to use it as shlib) + - removed debugging printfs +*/ + +typedef struct { +/* bit i/o */ + unsigned int bitbuf; + int bitcount,bs_size,totalbit; + unsigned short *bsbuf; +/* huffman */ + int last_dc[3]; + int _type; + int rlsize; + unsigned char *iqtab; +} bs_context_t; + +#include "bs.h" +#include "common.h" + +static const char *copyright = N_("Copyright (C) 2000 by Daniel Balster "); + +enum {B,G,R}; +typedef int BLOCK; + +#define DCTSIZE2 64 +#define RGB2Y(r,g,b) ( 0.299*(r) + 0.587*(g) + 0.114*(b) ) +#define RGB2Cb(r,g,b) ( -0.16874*(r) - 0.33126*(g) +0.5*(b) ) +#define RGB2Cr(r,g,b) ( 0.5*(r) - 0.41869*(g) - 0.08131*(b) ) + +/* +16x16 RGB -> 8x8 Cb,Cr,Y0,Y1,Y2,Y3 + +[Y0][Y1] [Cb] [Cr] +[Y2][Y3] +*/ +#define Cb 0 +#define Cr DCTSIZE2 + +static void rgb2yuv (unsigned char image[][3], BLOCK *blk) +{ + int x,y,i; + int tmpblk[16*16][3],(*yuv)[3]; + BLOCK *yblk; + + yuv=tmpblk; + for(i=0;i<16*16;i++) { + yuv[0][0] = RGB2Y (image[0][R],image[0][G],image[0][B])-128; + yuv[0][1] = RGB2Cb(image[0][R],image[0][G],image[0][B]); + yuv[0][2] = RGB2Cr(image[0][R],image[0][G],image[0][B]); + yuv++; image++; + } + + yuv = tmpblk; + yblk = blk+DCTSIZE2*2; + for(y=0;y<16;y+=2,blk+=4,yblk+=8,yuv+=8+16) { + if (y==8) yblk+=DCTSIZE2; + for(x=0;x<4;x++,blk++,yblk+=2,yuv+=2) { + blk[Cb] = (yuv[0][1]+yuv[1][1]+yuv[16][1]+yuv[17][1])/4; + blk[Cr] = (yuv[0][2]+yuv[1][2]+yuv[16][2]+yuv[17][2])/4; + yblk[0] = yuv[ 0][0]; + yblk[1] = yuv[ 1][0]; + yblk[8] = yuv[16][0]; + yblk[9] = yuv[17][0]; + + blk[4+Cb] = (yuv[8+0][1]+yuv[8+1][1]+yuv[8+16][1]+yuv[8+17][1])/4; + blk[4+Cr] = (yuv[8+0][2]+yuv[8+1][2]+yuv[8+16][2]+yuv[8+17][2])/4; + yblk[DCTSIZE2+0] = yuv[8+ 0][0]; + yblk[DCTSIZE2+1] = yuv[8+ 1][0]; + yblk[DCTSIZE2+8] = yuv[8+16][0]; + yblk[DCTSIZE2+9] = yuv[8+17][0]; + } + } +} + +#undef Cb +#undef Cr + +/* bit i/o */ +#define BITBUFSIZE 16 +#define WriteWord(x) ctxt->bsbuf[ctxt->bs_size++]=(x) + +static void putbits_init (bs_context_t *ctxt) +{ + ctxt->bitbuf = 0; + ctxt->bitcount = BITBUFSIZE; + ctxt->bs_size = 0; + ctxt->totalbit = 0; +} + +static void putbits_flush (bs_context_t *ctxt) +{ + WriteWord(ctxt->bitbuf); +} + +static void putbits (bs_context_t *ctxt, unsigned int x, unsigned int n) +{ + ctxt->totalbit+=n; + + if (nbitcount) { + ctxt->bitcount-=n; + ctxt->bitbuf |= x << ctxt->bitcount; + } else { + n-=ctxt->bitcount; + WriteWord(ctxt->bitbuf | (x>>n) ); + if (nbitcount = BITBUFSIZE-n; + } else { + WriteWord( x>>(n-BITBUFSIZE) ); + ctxt->bitcount = BITBUFSIZE*2-n; + } + ctxt->bitbuf = x << ctxt->bitcount; + } +} + +typedef struct { + unsigned char code,nbits; +} huff_t; + +const static huff_t dc_y_table[] = { + {4,3},{0,2},{1,2},{5,3},{6,3},{14,4},{30,5},{62,6},{126,7},{254,8} +}; + +const static huff_t dc_c_table[] = { + {0,2},{1,2},{2,2},{6,3},{14,4},{30,5},{62,6},{126,7},{254,8},{510,9} +}; + +#include "table.h" + +static void encode_init (bs_context_t *ctxt, void *outbuf, int type, int q_scale) +{ + ctxt->_type = type; + ctxt->last_dc[0] = 0; + ctxt->last_dc[1] = 0; + ctxt->last_dc[2] = 0; + ctxt->rlsize = 0; + putbits_init(ctxt); + + ctxt->bsbuf = outbuf; + ctxt->bsbuf[1] = 0x3800; + ctxt->bsbuf[2] = q_scale; + ctxt->bsbuf[3] = type; + ctxt->bs_size+=4; +} + +static void encode_finish (bs_context_t *ctxt) +{ + putbits_flush(ctxt); + ctxt->bsbuf[0] = (((ctxt->rlsize+1)/2)+31)&~31; +} + +static void encode_dc (bs_context_t *ctxt, int n, int level) +{ + if (ctxt->_type==2) { + putbits(ctxt,level&0x3ff,10); + } else { + const huff_t *table; + int prev,cnt; + + level = level/4; + if (n<2) { + table = dc_c_table; + prev = ctxt->last_dc[n]; + ctxt->last_dc[n] = level; + } else { + table = dc_y_table; + prev = ctxt->last_dc[2]; + ctxt->last_dc[2] = level; + } + level -= prev; + if (level==0) cnt=0; + else { + int alevel = level; + if (alevel<0) alevel=-alevel; + for(cnt=8;(alevel>>cnt)==0;cnt--); + cnt++; + if (level<0) level--; + } + putbits(ctxt,table[cnt].code,table[cnt].nbits); + if (cnt) putbits(ctxt,level&((1<rlsize++; +} + +static void encode_ac (bs_context_t *ctxt, int run, int level) +{ + int abslevel,sign; + if (level>0) { + abslevel = level; + sign = 0; + } else { + abslevel = -level; + sign = 1; + } + if (run<=31 && abslevel<=maxlevel[run]) { + putbits(ctxt,huff_table[run][abslevel-1].code+sign,huff_table[run][abslevel-1].nbits); + } else { + /* ESCAPE */ + putbits(ctxt,1,6); + putbits(ctxt,(run<<10)+(level&0x3ff),16); + } + ctxt->rlsize++; +} + +static void encode_eob (bs_context_t *ctxt) +{ + putbits(ctxt, 2,2); + ctxt->rlsize++; +} + +extern DCT(BLOCK *blk); + +unsigned char zscan[DCTSIZE2] = { + 0 ,1 ,8 ,16,9 ,2 ,3 ,10, + 17,24,32,25,18,11,4 ,5 , + 12,19,26,33,40,48,41,34, + 27,20,13,6 ,7 ,14,21,28, + 35,42,49,56,57,50,43,36, + 29,22,15,23,30,37,44,51, + 58,59,52,45,38,31,39,46, + 53,60,61,54,47,55,62,63 +}; + +static unsigned char xxx_iqtab[DCTSIZE2] = { + 2,16,19,22,26,27,29,34, + 16,16,22,24,27,29,34,37, + 19,22,26,27,29,34,34,38, + 22,22,26,27,29,34,37,40, + 22,26,27,29,32,35,40,48, + 26,27,29,32,35,40,48,58, + 26,27,29,34,38,46,56,69, + 27,29,35,38,46,56,69,83 +}; + +const unsigned char *bs_iqtab (void) { return xxx_iqtab; } + +static void blk2huff (bs_context_t *ctxt,BLOCK *blk,int q_scale) +{ + int i,k,run,level; + for(i=0;i<6;i++) { + DCT(blk); + for(k=0;k>=3; + level = blk[0]/ctxt->iqtab[0]; + encode_dc(ctxt,i,level); + run = 0; + for(k=1;k<64;k++) { + level = blk[zscan[k]]*8/(ctxt->iqtab[zscan[k]]*q_scale); + if (level==0) { + run++; + } else { + encode_ac(ctxt,run,level); + run=0; + } + } + encode_eob(ctxt); + blk+=DCTSIZE2; + } +} + +u_char bs_roundtbl[256*3]; + +void bs_init (void) +{ + int i; + for(i=0;i<256;i++) { + bs_roundtbl [i]=0; + bs_roundtbl [i+256]=i; + bs_roundtbl [i+512]=255; + } +} + +int bs_encode (bs_header_t *outbuf,bs_input_image_t *img,int type,int q_scale, + unsigned char *myiqtab) +{ + unsigned char image[16][16][3]; + BLOCK blk[6][DCTSIZE2]; + bs_context_t *ctxt = malloc(sizeof(bs_context_t)); + + int x,y,xw,yw,rl; + + ctxt->iqtab = myiqtab ? myiqtab : bs_iqtab(); + + encode_init (ctxt,outbuf,type,q_scale); + + for(x=0;xwidth;x+=16) { + xw = img->width-x; if (xw>16) xw = 16; + for(y=0;yheight;y+=16) { + char *p0 = img->top + x*(img->bit)/8 + y*img->nextline; + int i,j=0; + yw = img->height-y; if (yw>16) yw = 16; + + /* get 16x16 image */ + + for(i=0;inextline; + switch(img->bit) { + case 16: + for(j=0;j>10)&31)*8; + image[i][j][G] = ((c>>5)&31)*8; + image[i][j][R] = ((c&31))*8; + p+=2; + } + break; + case 24: + for(j=0;jbs_size * 2); + free (ctxt); + + return rl; +} diff --git a/psxdev/bs.h b/psxdev/bs.h new file mode 100644 index 0000000..d8d9a6c --- /dev/null +++ b/psxdev/bs.h @@ -0,0 +1,91 @@ +/* $Id: bs.h,v 1.1 2002-06-21 23:45:51 Pixel Exp $ */ + +/* + libbs - library for the bitstream image format + + Copyright (C) 1999, 2000 by these people, who contributed to this project + + bero@geocities.co.jp + Daniel Balster + + 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., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +/* + DCT code is based on Independent JPEG Group's sotfware +*/ + +#ifndef __LIB_BS_H +#define __LIB_BS_H + +#define _GNU_SOURCE + +#include +#include + +typedef struct { + int width,height; + int bit; + int nextline; + unsigned char *top,*lpbits; +} bs_input_image_t; + +#define BS_MAGIC 0x3800 +#define BS_TYPE 2 + +typedef struct { + u_short length; + u_short magic; + u_short q_scale; + u_short type; +} bs_header_t; + +/* prototypes */ + +#ifdef __cplusplus +extern "C" { +#endif + +void bs_init (void); + +int bs_encode ( // returns BS image size in bytes + bs_header_t *outbuf, // output BS image + bs_input_image_t *img, // input image descriptor + int type, // image type (use BS_TYPE) + int q_scale, // Q scaling factor (1=best,>= lower quality) + unsigned char *myiqtab // provide own iqtab (NULL == default) + ); + +void bs_decode_rgb24 ( + unsigned char *outbuf, // output RGB bytes (width*height*3) + bs_header_t *img, // input BS image + int width, int height, // dimension of BS image + unsigned char *myiqtab + ); + +void bs_decode_rgb15 ( + unsigned short *outbuf, // output RGB bytes (width*height*2) + bs_header_t *img, // input BS image + int width, int height, // dimension of BS image + unsigned char *myiqtab + ); + +const unsigned char *bs_iqtab (void); + +#ifdef __cplusplus +} +#endif + +#endif /* __LIB_BS_H */ diff --git a/psxdev/common.h b/psxdev/common.h new file mode 100644 index 0000000..ee6e3a1 --- /dev/null +++ b/psxdev/common.h @@ -0,0 +1,47 @@ +/* $Id: common.h,v 1.1 2002-06-21 23:45:51 Pixel Exp $ */ + +/* + common stuff + + Copyright (C) 1997, 1998, 1999, 2000 by these people, who contributed to this project + + Daniel Balster + + 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., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#ifndef __COMMON_H +#define __COMMON_H + +#define _GNU_SOURCE +#define _USE_GNU + +#include + +#if ENABLE_NLS +#if HAVE_LOCALE_H +#include +#endif +#if HAVE_LIBINTL_H +#include +#endif +#define _(string) gettext(string) +#define N_(string) (string) +#else +#define _(string) (string) +#define N_(string) (string) +#endif + +#endif diff --git a/psxdev/idctfst.c b/psxdev/idctfst.c new file mode 100644 index 0000000..22faae6 --- /dev/null +++ b/psxdev/idctfst.c @@ -0,0 +1,287 @@ +/* + * jidctfst.c + * + * Copyright (C) 1994-1996, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains a fast, not so accurate integer implementation of the + * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine + * must also perform dequantization of the input coefficients. + * + * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT + * on each row (or vice versa, but it's more convenient to emit a row at + * a time). Direct algorithms are also available, but they are much more + * complex and seem not to be any faster when reduced to code. + * + * This implementation is based on Arai, Agui, and Nakajima's algorithm for + * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in + * Japanese, but the algorithm is described in the Pennebaker & Mitchell + * JPEG textbook (see REFERENCES section in file README). The following code + * is based directly on figure 4-8 in P&M. + * While an 8-point DCT cannot be done in less than 11 multiplies, it is + * possible to arrange the computation so that many of the multiplies are + * simple scalings of the final outputs. These multiplies can then be + * folded into the multiplications or divisions by the JPEG quantization + * table entries. The AA&N method leaves only 5 multiplies and 29 adds + * to be done in the DCT itself. + * The primary disadvantage of this method is that with fixed-point math, + * accuracy is lost due to imprecise representation of the scaled + * quantization values. The smaller the quantization table entry, the less + * precise the scaled value, so this implementation does worse with high- + * quality-setting files than with low-quality ones. + */ + +/* + * This module is specialized to the case DCTSIZE = 8. + */ + +/* Scaling decisions are generally the same as in the LL&M algorithm; + * see jidctint.c for more details. However, we choose to descale + * (right shift) multiplication products as soon as they are formed, + * rather than carrying additional fractional bits into subsequent additions. + * This compromises accuracy slightly, but it lets us save a few shifts. + * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples) + * everywhere except in the multiplications proper; this saves a good deal + * of work on 16-bit-int machines. + * + * The dequantized coefficients are not integers because the AA&N scaling + * factors have been incorporated. We represent them scaled up by PASS1_BITS, + * so that the first and second IDCT rounds have the same input scaling. + * For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to + * avoid a descaling shift; this compromises accuracy rather drastically + * for small quantization table entries, but it saves a lot of shifts. + * For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway, + * so we use a much larger scaling factor to preserve accuracy. + * + * A final compromise is to represent the multiplicative constants to only + * 8 fractional bits, rather than 13. This saves some shifting work on some + * machines, and may also reduce the cost of multiplication (since there + * are fewer one-bits in the constants). + */ + +#define BITS_IN_JSAMPLE 8 + +#if BITS_IN_JSAMPLE == 8 +#define CONST_BITS 8 +#define PASS1_BITS 2 +#else +#define CONST_BITS 8 +#define PASS1_BITS 1 /* lose a little precision to avoid overflow */ +#endif + +/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus + * causing a lot of useless floating-point operations at run time. + * To get around this we use the following pre-calculated constants. + * If you change CONST_BITS you may want to add appropriate values. + * (With a reasonable C compiler, you can just rely on the FIX() macro...) + */ + +#if CONST_BITS == 8 +#define FIX_1_082392200 (277) /* FIX(1.082392200) */ +#define FIX_1_414213562 (362) /* FIX(1.414213562) */ +#define FIX_1_847759065 (473) /* FIX(1.847759065) */ +#define FIX_2_613125930 (669) /* FIX(2.613125930) */ +#else +#define FIX_1_082392200 FIX(1.082392200) +#define FIX_1_414213562 FIX(1.414213562) +#define FIX_1_847759065 FIX(1.847759065) +#define FIX_2_613125930 FIX(2.613125930) +#endif + + +/* We can gain a little more speed, with a further compromise in accuracy, + * by omitting the addition in a descaling shift. This yields an incorrectly + * rounded result half the time... + */ + + +/* Multiply a DCTELEM variable by an INT32 constant, and immediately + * descale to yield a DCTELEM result. + */ + +#define MULTIPLY(var,const) (DESCALE((var) * (const), CONST_BITS)) + + +/* Dequantize a coefficient by multiplying it by the multiplier-table + * entry; produce a DCTELEM result. For 8-bit data a 16x16->16 + * multiplication will do. For 12-bit data, the multiplier table is + * declared INT32, so a 32-bit multiply will be used. + */ + +#if BITS_IN_JSAMPLE == 8 +#define DEQUANTIZE(coef,quantval) (coef) +#else +#define DEQUANTIZE(coef,quantval) \ + DESCALE((coef), IFAST_SCALE_BITS-PASS1_BITS) +#endif + + +/* Like DESCALE, but applies to a DCTELEM and produces an int. + * We assume that int right shift is unsigned if INT32 right shift is. + */ + +#define DESCALE(x,n) ((x)>>(n)) +#define RANGE(n) (n) +#define BLOCK int + +/* + * Perform dequantization and inverse DCT on one block of coefficients. + */ +#define DCTSIZE 8 +#define DCTSIZE2 64 + +static void IDCT1(BLOCK *block) +{ + int val = RANGE(DESCALE(block[0], PASS1_BITS+3)); + int i; + for(i=0;i>(n)) +#define GLOBAL +#define jpeg_fdct_islow DCT +#define SHIFT_TEMPS +//#define BITS_IN_JSAMPLE 8 +//#define MULTIPLY16C16(var,const) ((var) * (const)) + + +#ifdef DCT_ISLOW_SUPPORTED + + +/* + * This module is specialized to the case DCTSIZE = 8. + */ + +#if DCTSIZE != 8 + Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */ +#endif + + +/* + * The poop on this scaling stuff is as follows: + * + * Each 1-D DCT step produces outputs which are a factor of sqrt(N) + * larger than the true DCT outputs. The final outputs are therefore + * a factor of N larger than desired; since N=8 this can be cured by + * a simple right shift at the end of the algorithm. The advantage of + * this arrangement is that we save two multiplications per 1-D DCT, + * because the y0 and y4 outputs need not be divided by sqrt(N). + * In the IJG code, this factor of 8 is removed by the quantization step + * (in jcdctmgr.c), NOT in this module. + * + * We have to do addition and subtraction of the integer inputs, which + * is no problem, and multiplication by fractional constants, which is + * a problem to do in integer arithmetic. We multiply all the constants + * by CONST_SCALE and convert them to integer constants (thus retaining + * CONST_BITS bits of precision in the constants). After doing a + * multiplication we have to divide the product by CONST_SCALE, with proper + * rounding, to produce the correct output. This division can be done + * cheaply as a right shift of CONST_BITS bits. We postpone shifting + * as long as possible so that partial sums can be added together with + * full fractional precision. + * + * The outputs of the first pass are scaled up by PASS1_BITS bits so that + * they are represented to better-than-integral precision. These outputs + * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word + * with the recommended scaling. (For 12-bit sample data, the intermediate + * array is INT32 anyway.) + * + * To avoid overflow of the 32-bit intermediate results in pass 2, we must + * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis + * shows that the values given below are the most effective. + */ + +#if BITS_IN_JSAMPLE == 8 +#define CONST_BITS 13 +#define PASS1_BITS 2 +#else +#define CONST_BITS 13 +#define PASS1_BITS 1 /* lose a little precision to avoid overflow */ +#endif + +/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus + * causing a lot of useless floating-point operations at run time. + * To get around this we use the following pre-calculated constants. + * If you change CONST_BITS you may want to add appropriate values. + * (With a reasonable C compiler, you can just rely on the FIX() macro...) + */ + +#if CONST_BITS == 13 +#define FIX_0_298631336 ((INT32) 2446) /* FIX(0.298631336) */ +#define FIX_0_390180644 ((INT32) 3196) /* FIX(0.390180644) */ +#define FIX_0_541196100 ((INT32) 4433) /* FIX(0.541196100) */ +#define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */ +#define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */ +#define FIX_1_175875602 ((INT32) 9633) /* FIX(1.175875602) */ +#define FIX_1_501321110 ((INT32) 12299) /* FIX(1.501321110) */ +#define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */ +#define FIX_1_961570560 ((INT32) 16069) /* FIX(1.961570560) */ +#define FIX_2_053119869 ((INT32) 16819) /* FIX(2.053119869) */ +#define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */ +#define FIX_3_072711026 ((INT32) 25172) /* FIX(3.072711026) */ +#else +#define FIX_0_298631336 FIX(0.298631336) +#define FIX_0_390180644 FIX(0.390180644) +#define FIX_0_541196100 FIX(0.541196100) +#define FIX_0_765366865 FIX(0.765366865) +#define FIX_0_899976223 FIX(0.899976223) +#define FIX_1_175875602 FIX(1.175875602) +#define FIX_1_501321110 FIX(1.501321110) +#define FIX_1_847759065 FIX(1.847759065) +#define FIX_1_961570560 FIX(1.961570560) +#define FIX_2_053119869 FIX(2.053119869) +#define FIX_2_562915447 FIX(2.562915447) +#define FIX_3_072711026 FIX(3.072711026) +#endif + + +/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result. + * For 8-bit samples with the recommended scaling, all the variable + * and constant values involved are no more than 16 bits wide, so a + * 16x16->32 bit multiply can be used instead of a full 32x32 multiply. + * For 12-bit samples, a full 32-bit multiplication will be needed. + */ + +#if BITS_IN_JSAMPLE == 8 +#define MULTIPLY(var,const) MULTIPLY16C16(var,const) +#else +#define MULTIPLY(var,const) ((var) * (const)) +#endif + + +/* + * Perform the forward DCT on one block of samples. + */ + +GLOBAL void +jpeg_fdct_islow (DCTELEM * data) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; + INT32 tmp10, tmp11, tmp12, tmp13; + INT32 z1, z2, z3, z4, z5; + DCTELEM *dataptr; + int ctr; + SHIFT_TEMPS + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + + dataptr = data; + for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { + tmp0 = dataptr[0] + dataptr[7]; + tmp7 = dataptr[0] - dataptr[7]; + tmp1 = dataptr[1] + dataptr[6]; + tmp6 = dataptr[1] - dataptr[6]; + tmp2 = dataptr[2] + dataptr[5]; + tmp5 = dataptr[2] - dataptr[5]; + tmp3 = dataptr[3] + dataptr[4]; + tmp4 = dataptr[3] - dataptr[4]; + + /* Even part per LL&M figure 1 --- note that published figure is faulty; + * rotator "sqrt(2)*c1" should be "sqrt(2)*c6". + */ + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + + dataptr[0] = (DCTELEM) ((tmp10 + tmp11) << PASS1_BITS); + dataptr[4] = (DCTELEM) ((tmp10 - tmp11) << PASS1_BITS); + + z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100); + dataptr[2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp13, FIX_0_765366865), + CONST_BITS-PASS1_BITS); + dataptr[6] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, - FIX_1_847759065), + CONST_BITS-PASS1_BITS); + + /* Odd part per figure 8 --- note paper omits factor of sqrt(2). + * cK represents cos(K*pi/16). + * i0..i3 in the paper are tmp4..tmp7 here. + */ + + z1 = tmp4 + tmp7; + z2 = tmp5 + tmp6; + z3 = tmp4 + tmp6; + z4 = tmp5 + tmp7; + z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */ + + tmp4 = MULTIPLY(tmp4, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */ + tmp5 = MULTIPLY(tmp5, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */ + tmp6 = MULTIPLY(tmp6, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */ + tmp7 = MULTIPLY(tmp7, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */ + z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */ + z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */ + z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */ + z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */ + + z3 += z5; + z4 += z5; + + dataptr[7] = (DCTELEM) DESCALE(tmp4 + z1 + z3, CONST_BITS-PASS1_BITS); + dataptr[5] = (DCTELEM) DESCALE(tmp5 + z2 + z4, CONST_BITS-PASS1_BITS); + dataptr[3] = (DCTELEM) DESCALE(tmp6 + z2 + z3, CONST_BITS-PASS1_BITS); + dataptr[1] = (DCTELEM) DESCALE(tmp7 + z1 + z4, CONST_BITS-PASS1_BITS); + + dataptr += DCTSIZE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + */ + + dataptr = data; + for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7]; + tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7]; + tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6]; + tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6]; + tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5]; + tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5]; + tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4]; + tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4]; + + /* Even part per LL&M figure 1 --- note that published figure is faulty; + * rotator "sqrt(2)*c1" should be "sqrt(2)*c6". + */ + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + + dataptr[DCTSIZE*0] = (DCTELEM) DESCALE(tmp10 + tmp11, PASS1_BITS); + dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp10 - tmp11, PASS1_BITS); + + z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100); + dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp13, FIX_0_765366865), + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, - FIX_1_847759065), + CONST_BITS+PASS1_BITS); + + /* Odd part per figure 8 --- note paper omits factor of sqrt(2). + * cK represents cos(K*pi/16). + * i0..i3 in the paper are tmp4..tmp7 here. + */ + + z1 = tmp4 + tmp7; + z2 = tmp5 + tmp6; + z3 = tmp4 + tmp6; + z4 = tmp5 + tmp7; + z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */ + + tmp4 = MULTIPLY(tmp4, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */ + tmp5 = MULTIPLY(tmp5, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */ + tmp6 = MULTIPLY(tmp6, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */ + tmp7 = MULTIPLY(tmp7, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */ + z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */ + z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */ + z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */ + z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */ + + z3 += z5; + z4 += z5; + + dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp4 + z1 + z3, + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp5 + z2 + z4, + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp6 + z2 + z3, + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp7 + z1 + z4, + CONST_BITS+PASS1_BITS); + + dataptr++; /* advance pointer to next column */ + } +} + +#endif /* DCT_ISLOW_SUPPORTED */ diff --git a/psxdev/table.h b/psxdev/table.h new file mode 100644 index 0000000..3e50b18 --- /dev/null +++ b/psxdev/table.h @@ -0,0 +1,102 @@ +const static huff_t table0[]={ + {6,3},{8,5},{10,6},{12,8},{76,9},{66,9},{20,11},{58,13},{48,13},{38,13},{32,13},{52,14},{50,14},{48,14},{46,14},{62,15},{60,15},{58,15},{56,15},{54,15},{52,15},{50,15},{48,15},{46,15},{44,15},{42,15},{40,15},{38,15},{36,15},{34,15},{32,15},{48,16},{46,16},{44,16},{42,16},{40,16},{38,16},{36,16},{34,16},{32,16}, +}; +const static huff_t table1[]={ + {6,4},{12,7},{74,9},{24,11},{54,13},{44,14},{42,14},{62,16},{60,16},{58,16},{56,16},{54,16},{52,16},{50,16},{38,17},{36,17},{34,17},{32,17}, +}; +const static huff_t table2[]={ + {10,5},{8,8},{22,11},{40,13},{40,14}, +}; +const static huff_t table3[]={ + {14,6},{72,9},{56,13},{38,14}, +}; +const static huff_t table4[]={ + {12,6},{30,11},{36,13}, +}; +const static huff_t table5[]={ + {14,7},{18,11},{36,14}, +}; +const static huff_t table6[]={ + {10,7},{60,13},{40,17}, +}; +const static huff_t table7[]={ + {8,7},{42,13}, +}; +const static huff_t table8[]={ + {14,8},{34,13}, +}; +const static huff_t table9[]={ + {10,8},{34,14}, +}; +const static huff_t table10[]={ + {78,9},{32,14}, +}; +const static huff_t table11[]={ + {70,9},{52,17}, +}; +const static huff_t table12[]={ + {68,9},{50,17}, +}; +const static huff_t table13[]={ + {64,9},{48,17}, +}; +const static huff_t table14[]={ + {28,11},{46,17}, +}; +const static huff_t table15[]={ + {26,11},{44,17}, +}; +const static huff_t table16[]={ + {16,11},{42,17}, +}; +const static huff_t table17[]={ + {62,13}, +}; +const static huff_t table18[]={ + {52,13}, +}; +const static huff_t table19[]={ + {50,13}, +}; +const static huff_t table20[]={ + {46,13}, +}; +const static huff_t table21[]={ + {44,13}, +}; +const static huff_t table22[]={ + {62,14}, +}; +const static huff_t table23[]={ + {60,14}, +}; +const static huff_t table24[]={ + {58,14}, +}; +const static huff_t table25[]={ + {56,14}, +}; +const static huff_t table26[]={ + {54,14}, +}; +const static huff_t table27[]={ + {62,17}, +}; +const static huff_t table28[]={ + {60,17}, +}; +const static huff_t table29[]={ + {58,17}, +}; +const static huff_t table30[]={ + {56,17}, +}; +const static huff_t table31[]={ + {54,17}, +}; +const static huff_t *huff_table[]={ + table0,table1,table2,table3,table4,table5,table6,table7,table8,table9,table10,table11,table12,table13,table14,table15,table16,table17,table18,table19,table20,table21,table22,table23,table24,table25,table26,table27,table28,table29,table30,table31, +}; +const static int maxlevel[]={ + 40,18,5,4,3,3,3,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +}; diff --git a/psxdev/vlc.c b/psxdev/vlc.c new file mode 100644 index 0000000..aa7e610 --- /dev/null +++ b/psxdev/vlc.c @@ -0,0 +1,602 @@ +#include "bs.h" +#include + +#define SOFT + +#define CODE1(a,b,c) (((a)<<10)|((b)&0x3ff)|((c)<<16)) +/* run, level, bit */ +#define CODE(a,b,c) CODE1(a,b,c+1),CODE1(a,-b,c+1) +#define CODE0(a,b,c) CODE1(a,b,c),CODE1(a,b,c) +#define CODE2(a,b,c) CODE1(a,b,c+1),CODE1(a,b,c+1) +#define RUNOF(a) ((a)>>10) +#define VALOF(a) ((short)((a)<<6)>>6) +#define BITOF(a) ((a)>>16) +#define EOB 0xfe00 +#define ESCAPE_CODE CODE1(63,0,6) +#define EOB_CODE CODE1(63,512,2) + +/* + DC code + Y U,V +0 100 00 0 +1 00x 01x -1,1 +2 01xx 10xx -3,-2,2,3 +3 101xxx 110xxx -7..-4,4..7 +4 110xxxx 1110 -15..-8,8..15 +5 1110xxxxx 11110 -31..-16,16..31 +6 11110xxxxxx 111110 -63..-32,32..63 +7 111110 1111110 -127..-64,64..127 +8 1111110 11111110 -255..-128,128..255 + 7+8 8+8 +*/ + +/* + This table based on MPEG2DEC by MPEG Software Simulation Group +*/ + +/* Table B-14, DCT coefficients table zero, +* codes 0100 ... 1xxx (used for all other coefficients) +*/ +static const u_long VLCtabnext[12*2] = { + CODE(0,2,4), CODE(2,1,4), CODE2(1,1,3), CODE2(1,-1,3), + CODE0(63,512,2), CODE0(63,512,2), CODE0(63,512,2), CODE0(63,512,2), /*EOB*/ + CODE2(0,1,2), CODE2(0,1,2), CODE2(0,-1,2), CODE2(0,-1,2) +}; + +/* Table B-14, DCT coefficients table zero, +* codes 000001xx ... 00111xxx +*/ +static const u_long VLCtab0[60*2] = { + CODE0(63,0,6), CODE0(63,0,6),CODE0(63,0,6), CODE0(63,0,6), /* ESCAPE */ + CODE2(2,2,7), CODE2(2,-2,7), CODE2(9,1,7), CODE2(9,-1,7), + CODE2(0,4,7), CODE2(0,-4,7), CODE2(8,1,7), CODE2(8,-1,7), + CODE2(7,1,6), CODE2(7,1,6), CODE2(7,-1,6), CODE2(7,-1,6), + CODE2(6,1,6), CODE2(6,1,6), CODE2(6,-1,6), CODE2(6,-1,6), + CODE2(1,2,6), CODE2(1,2,6), CODE2(1,-2,6), CODE2(1,-2,6), + CODE2(5,1,6), CODE2(5,1,6), CODE2(5,-1,6), CODE2(5,-1,6), + CODE(13,1,8), CODE(0,6,8), CODE(12,1,8), CODE(11,1,8), + CODE(3,2,8), CODE(1,3,8), CODE(0,5,8), CODE(10,1,8), + CODE2(0,3,5), CODE2(0,3,5), CODE2(0,3,5), CODE2(0,3,5), + CODE2(0,-3,5), CODE2(0,-3,5), CODE2(0,-3,5), CODE2(0,-3,5), + CODE2(4,1,5), CODE2(4,1,5), CODE2(4,1,5), CODE2(4,1,5), + CODE2(4,-1,5), CODE2(4,-1,5), CODE2(4,-1,5), CODE2(4,-1,5), + CODE2(3,1,5), CODE2(3,1,5), CODE2(3,1,5), CODE2(3,1,5), + CODE2(3,-1,5), CODE2(3,-1,5), CODE2(3,-1,5), CODE2(3,-1,5) +}; + +/* Table B-14, DCT coefficients table zero, +* codes 0000001000 ... 0000001111 +*/ +static const u_long VLCtab1[8*2] = { + CODE(16,1,10), CODE(5,2,10), CODE(0,7,10), CODE(2,3,10), + CODE(1,4,10), CODE(15,1,10), CODE(14,1,10), CODE(4,2,10) +}; + +/* Table B-14/15, DCT coefficients table zero / one, +* codes 000000010000 ... 000000011111 +*/ +static const u_long VLCtab2[16*2] = { + CODE(0,11,12), CODE(8,2,12), CODE(4,3,12), CODE(0,10,12), + CODE(2,4,12), CODE(7,2,12), CODE(21,1,12), CODE(20,1,12), + CODE(0,9,12), CODE(19,1,12), CODE(18,1,12), CODE(1,5,12), + CODE(3,3,12), CODE(0,8,12), CODE(6,2,12), CODE(17,1,12) +}; + +/* Table B-14/15, DCT coefficients table zero / one, +* codes 0000000010000 ... 0000000011111 +*/ +static const u_long VLCtab3[16*2] = { + CODE(10,2,13), CODE(9,2,13), CODE(5,3,13), CODE(3,4,13), + CODE(2,5,13), CODE(1,7,13), CODE(1,6,13), CODE(0,15,13), + CODE(0,14,13), CODE(0,13,13), CODE(0,12,13), CODE(26,1,13), + CODE(25,1,13), CODE(24,1,13), CODE(23,1,13), CODE(22,1,13) +}; + +/* Table B-14/15, DCT coefficients table zero / one, +* codes 00000000010000 ... 00000000011111 +*/ +static const u_long VLCtab4[16*2] = { + CODE(0,31,14), CODE(0,30,14), CODE(0,29,14), CODE(0,28,14), + CODE(0,27,14), CODE(0,26,14), CODE(0,25,14), CODE(0,24,14), + CODE(0,23,14), CODE(0,22,14), CODE(0,21,14), CODE(0,20,14), + CODE(0,19,14), CODE(0,18,14), CODE(0,17,14), CODE(0,16,14) +}; + +/* Table B-14/15, DCT coefficients table zero / one, +* codes 000000000010000 ... 000000000011111 +*/ +static const u_long VLCtab5[16*2] = { + CODE(0,40,15), CODE(0,39,15), CODE(0,38,15), CODE(0,37,15), + CODE(0,36,15), CODE(0,35,15), CODE(0,34,15), CODE(0,33,15), + CODE(0,32,15), CODE(1,14,15), CODE(1,13,15), CODE(1,12,15), + CODE(1,11,15), CODE(1,10,15), CODE(1,9,15), CODE(1,8,15) +}; + +/* Table B-14/15, DCT coefficients table zero / one, +* codes 0000000000010000 ... 0000000000011111 +*/ +static const u_long VLCtab6[16*2] = { + CODE(1,18,16), CODE(1,17,16), CODE(1,16,16), CODE(1,15,16), + CODE(6,3,16), CODE(16,2,16), CODE(15,2,16), CODE(14,2,16), + CODE(13,2,16), CODE(12,2,16), CODE(11,2,16), CODE(31,1,16), + CODE(30,1,16), CODE(29,1,16), CODE(28,1,16), CODE(27,1,16) +}; + +/* + DC code + Y U,V +0 100 00 0 +1 00x 01x -1,1 +2 01xx 10xx -3,-2,2,3 +3 101xxx 110xxx -7..-4,4..7 +4 110xxxx 1110xxxx -15..-8,8..15 +5 1110xxxxx 11110xxxxx -31..-16,16..31 +6 11110xxxxxx 111110xxxxxx -63..-32,32..63 +7 111110xxxxxxx 1111110xxxxxxx -127..-64,64..127 +8 1111110xxxxxxxx 11111110xxxxxxxx -255..-128,128..255 +*/ + +static const u_long DC_Ytab0[48] = { + CODE1(0,-1,3),CODE1(0,-1,3),CODE1(0,-1,3),CODE1(0,-1,3), + CODE1(0,-1,3),CODE1(0,-1,3),CODE1(0,-1,3),CODE1(0,-1,3), + CODE1(0,1,3),CODE1(0,1,3),CODE1(0,1,3),CODE1(0,1,3), + CODE1(0,1,3),CODE1(0,1,3),CODE1(0,1,3),CODE1(0,1,3), + + CODE1(0,-3,4),CODE1(0,-3,4),CODE1(0,-3,4),CODE1(0,-3,4), + CODE1(0,-2,4),CODE1(0,-2,4),CODE1(0,-2,4),CODE1(0,-2,4), + CODE1(0,2,4),CODE1(0,2,4),CODE1(0,2,4),CODE1(0,2,4), + CODE1(0,3,4),CODE1(0,3,4),CODE1(0,3,4),CODE1(0,3,4), + + CODE1(0,0,3),CODE1(0,0,3),CODE1(0,0,3),CODE1(0,0,3), + CODE1(0,0,3),CODE1(0,0,3),CODE1(0,0,3),CODE1(0,0,3), + CODE1(0,-7,6),CODE1(0,-6,6),CODE1(0,-5,6),CODE1(0,-4,6), + CODE1(0,4,6),CODE1(0,5,6),CODE1(0,6,6),CODE1(0,7,6), + +}; + +static const u_long DC_UVtab0[56] = { + CODE1(0,0,2),CODE1(0,0,2),CODE1(0,0,2),CODE1(0,0,2), + CODE1(0,0,2),CODE1(0,0,2),CODE1(0,0,2),CODE1(0,0,2), + CODE1(0,0,2),CODE1(0,0,2),CODE1(0,0,2),CODE1(0,0,2), + CODE1(0,0,2),CODE1(0,0,2),CODE1(0,0,2),CODE1(0,0,2), + + CODE1(0,-1,3),CODE1(0,-1,3),CODE1(0,-1,3),CODE1(0,-1,3), + CODE1(0,-1,3),CODE1(0,-1,3),CODE1(0,-1,3),CODE1(0,-1,3), + CODE1(0,1,3),CODE1(0,1,3),CODE1(0,1,3),CODE1(0,1,3), + CODE1(0,1,3),CODE1(0,1,3),CODE1(0,1,3),CODE1(0,1,3), + + CODE1(0,-3,4),CODE1(0,-3,4),CODE1(0,-3,4),CODE1(0,-3,4), + CODE1(0,-2,4),CODE1(0,-2,4),CODE1(0,-2,4),CODE1(0,-2,4), + CODE1(0,2,4),CODE1(0,2,4),CODE1(0,2,4),CODE1(0,2,4), + CODE1(0,3,4),CODE1(0,3,4),CODE1(0,3,4),CODE1(0,3,4), + + CODE1(0,-7,6),CODE1(0,-6,6),CODE1(0,-5,6),CODE1(0,-4,6), + CODE1(0,4,6),CODE1(0,5,6),CODE1(0,6,6),CODE1(0,7,6), +}; + +#define DCTSIZE2 64 + +/* decode one intra coded MPEG-1 block */ + +#define Show_Bits(N) (bitbuf>>(32-(N))) +/* 最小有効bit 17 bit*/ + +#define Flush_Buffer(N) {bitbuf <<=(N);incnt +=(N);while(incnt>=0) {bitbuf |= Get_Word()<=2) { + /* Y */ + if (code2<48) { + code2 = DC_Ytab0[code2]; + code2 = (code2&0xffff0000)|((last_dc[2]+=VALOF(code2)*4)&0x3ff); + } else { + int nbit,val; + int bit = 3; + while(Show_Bits(bit)&1) { bit++;} + bit++; + nbit = bit*2-1; + val = Show_Bits(nbit)&((1<=1<<(SBIT- 2)) { + code2 = VLCtabnext[(code>>12)-8]; + if (code2==EOB_CODE) break; + } + else if (code>=1<<(SBIT- 6)) { + code2 = VLCtab0[(code>>8)-8]; + if (code2==ESCAPE_CODE) { + Flush_Buffer(6); /* ESCAPE len */ + code2 = Show_Bits(16)| (16<<16); + } + } + else if (code>=1<<(SBIT- 7)) code2 = VLCtab1[(code>>6)-16]; + else if (code>=1<<(SBIT- 8)) code2 = VLCtab2[(code>>4)-32]; + else if (code>=1<<(SBIT- 9)) code2 = VLCtab3[(code>>3)-32]; + else if (code>=1<<(SBIT-10)) code2 = VLCtab4[(code>>2)-32]; + else if (code>=1<<(SBIT-11)) code2 = VLCtab5[(code>>1)-32]; + else if (code>=1<<(SBIT-12)) code2 = VLCtab6[(code>>0)-32]; + else { + do { + *mdec_rl++=EOB; + } while(mdec_rliqtab[i] =ctxt->iq_y[i]*aanscales[i]>>(CONST_BITS-IFAST_SCALE_BITS); + } +} + +#define BLOCK long + +extern IDCT(BLOCK *blk,int k); + +u_short* rl2blk(bs_context_t *ctxt, BLOCK *blk,u_short *mdec_rl) +{ + int i,k,q_scale,rl; + memset(blk,0,6*DCTSIZE2*sizeof(BLOCK)); + for(i=0;i<6;i++) { + rl = *mdec_rl++; + q_scale = RUNOF(rl); + blk[0] = ctxt->iqtab[0]*VALOF(rl); + k = 0; + for(;;) { + rl = *mdec_rl++; + if (rl==EOB) break; + k += RUNOF(rl)+1; + blk[zscan[k]] = ctxt->iqtab[zscan[k]]*q_scale*VALOF(rl)/8; + } + + IDCT(blk,k+1); + + blk+=DCTSIZE2; + } + return mdec_rl; +} + +#define RGB15(r,g,b) ( (((b)&0xf8)<<7)|(((g)&0xf8)<<2)|((r)>>3) ) + +#define ROUND(r) bs_roundtbl[(r)+256] +#if 1 +#define SHIFT 12 +#define toFIX(a) (int)((a)*(1<>SHIFT) +#define FIX_1 toFIX(1) +#define MULR(a) toINT((a)*toFIX(1.402)) +#define MULG(a) toINT((a)*toFIX(-0.3437)) +#define MULG2(a) toINT((a)*toFIX(-0.7143)) +#define MULB(a) toINT((a)*toFIX(1.772)) +#else +#define MULR(a) 0 +#define MULG(a) 0 +#define MULG2(a) 0 +#define MULB(a) 0 +#endif + + +/* +int ROUND(int r) +{ + if (r<0) return 0; + else if (r>255) return 255; + else return r; +} +*/ + +extern u_char bs_roundtbl[256*3]; + +static void yuv2rgb15(BLOCK *blk,u_short *image) +{ + int x,yy; + BLOCK *yblk = blk+DCTSIZE2*2; + for(yy=0;yy<16;yy+=2,blk+=4,yblk+=8,image+=8+16) { + if (yy==8) yblk+=DCTSIZE2; + for(x=0;x<4;x++,blk++,yblk+=2,image+=2) { + int r0,b0,g0,y; + r0 = MULR(blk[DCTSIZE2]); /* cr */ + g0 = MULG(blk[0])+MULG2(blk[DCTSIZE2]); + b0 = MULB(blk[0]); /* cb */ + y = yblk[0]+128; + image[0] = RGB15(ROUND(r0+y),ROUND(g0+y),ROUND(b0+y)); + y = yblk[1]+128+4; + image[1] = RGB15(ROUND(r0+y),ROUND(g0+y),ROUND(b0+y)); + y = yblk[8]+128+6; + image[16] = RGB15(ROUND(r0+y),ROUND(g0+y),ROUND(b0+y)); + y = yblk[9]+128+2; + image[17] = RGB15(ROUND(r0+y),ROUND(g0+y),ROUND(b0+y)); + r0 = MULR(blk[4+DCTSIZE2]); + g0 = MULG(blk[4])+MULG2(blk[4+DCTSIZE2]); + b0 = MULB(blk[4]); + y = yblk[DCTSIZE2+0]+128; + image[8+0] = RGB15(ROUND(r0+y),ROUND(g0+y),ROUND(b0+y)); + y = yblk[DCTSIZE2+1]+128+4; + image[8+1] = RGB15(ROUND(r0+y),ROUND(g0+y),ROUND(b0+y)); + y = yblk[DCTSIZE2+8]+128+6; + image[8+16] = RGB15(ROUND(r0+y),ROUND(g0+y),ROUND(b0+y)); + y = yblk[DCTSIZE2+9]+128+2; + image[8+17] = RGB15(ROUND(r0+y),ROUND(g0+y),ROUND(b0+y)); + } + } +} + +enum {B,G,R}; + +static void yuv2rgb24(BLOCK *blk,u_char image[][3]) +{ + int x,yy; + BLOCK *yblk = blk+DCTSIZE2*2; + for(yy=0;yy<16;yy+=2,blk+=4,yblk+=8,image+=8+16) { + if (yy==8) yblk+=DCTSIZE2; + for(x=0;x<4;x++,blk++,yblk+=2,image+=2) { + int r0,b0,g0,y; + r0 = MULR(blk[DCTSIZE2]); /* cr */ + g0 = MULG(blk[0])+MULG2(blk[DCTSIZE2]); + b0 = MULB(blk[0]); /* cb */ + y = yblk[0]+128; + image[0][R] = ROUND(r0+y); + image[0][G] = ROUND(g0+y); + image[0][B] = ROUND(b0+y); + y = yblk[1]+128; + image[1][R] = ROUND(r0+y); + image[1][G] = ROUND(g0+y); + image[1][B] = ROUND(b0+y); + y = yblk[8]+128; + image[16][R] = ROUND(r0+y); + image[16][G] = ROUND(g0+y); + image[16][B] = ROUND(b0+y); + y = yblk[9]+128; + image[17][R] = ROUND(r0+y); + image[17][G] = ROUND(g0+y); + image[17][B] = ROUND(b0+y); + + r0 = MULR(blk[4+DCTSIZE2]); + g0 = MULG(blk[4])+MULG2(blk[4+DCTSIZE2]); + b0 = MULB(blk[4]); + y = yblk[DCTSIZE2+0]+128; + image[8+0][R] = ROUND(r0+y); + image[8+0][G] = ROUND(g0+y); + image[8+0][B] = ROUND(b0+y); + y = yblk[DCTSIZE2+1]+128; + image[8+1][R] = ROUND(r0+y); + image[8+1][G] = ROUND(g0+y); + image[8+1][B] = ROUND(b0+y); + y = yblk[DCTSIZE2+8]+128; + image[8+16][R] = ROUND(r0+y); + image[8+16][G] = ROUND(g0+y); + image[8+16][B] = ROUND(b0+y); + y = yblk[DCTSIZE2+9]+128; + image[8+17][R] = ROUND(r0+y); + image[8+17][G] = ROUND(g0+y); + image[8+17][B] = ROUND(b0+y); + } + } +} + +static void DecDCTReset(bs_context_t *ctxt, int mode) +{ + iqtab_init(ctxt); +} + +static void DecDCTin(bs_context_t *ctxt, u_short *mdecrl,int mode) +{ + mdecrl+=2; + ctxt->mdec_rl = mdecrl; + ctxt->rl_end = mdecrl+mdecrl[-2]*2; + ctxt->mdec_mode = mode; +} + +static void DecDCTout(bs_context_t *ctxt, u_short *image,int size) +{ + BLOCK blk[DCTSIZE2*6]; + int blocksize=16*16; + if (ctxt->mdec_mode) blocksize = 16*16*3/2; + for(;size>0;size-=blocksize/2,image+=blocksize) { + ctxt->mdec_rl = rl2blk(ctxt,blk,ctxt->mdec_rl); + if (ctxt->mdec_mode==0) yuv2rgb15(blk,image); + else yuv2rgb24(blk,image); + } +} + +void bs_decode_rgb24 ( + unsigned char *outbuf, // output RGB bytes (width*height*3) + bs_header_t *img, // input BS image + int width, int height, // dimension of BS image + unsigned char *myiqtab + ) +{ + unsigned short *buf2 = (unsigned short *) outbuf; + unsigned short *bufp = (unsigned short *) img; + bs_context_t ctxt; + unsigned short *rl,*image; + int slice,rlsize; + int mode; + int x,y; + int height2 = (height+15)&~15; + int w; + + ctxt.iq_y = myiqtab ? myiqtab : bs_iqtab(); + mode=1; + w=24; + width = width*3/2; + + image = (unsigned short *) malloc (height2*w*sizeof(short)); + rl = (unsigned short *) malloc ((bufp[0]+2)*sizeof(long)); + + DecDCTReset(&ctxt,0); + DecDCTvlc(bufp,rl); + DecDCTin(&ctxt,rl,mode); + + slice = height2*w/2; + + for(x=0;x=0;y--) + { + memcpy(dst,src,w*2); + src+=w; + dst+=width; + } + } + + free (image); + free (rl); +} + +void bs_decode_rgb15 ( + unsigned short *outbuf, // output RGB bytes (width*height*2) + bs_header_t *img, // input BS image + int width, int height, // dimension of BS image + unsigned char *myiqtab + ) +{ + unsigned short *buf2 = (unsigned short *) outbuf; + unsigned short *bufp = (unsigned short *) img; + bs_context_t ctxt; + unsigned short *rl,*image; + int slice,rlsize; + int mode; + int x,y; + int height2 = (height+15)&~15; + int w; + + ctxt.iq_y = myiqtab ? myiqtab : bs_iqtab(); + mode=0; + w=24; + + image = (unsigned short *) malloc (height2*w*sizeof(short)); + rl = (unsigned short *) malloc ((bufp[0]+2)*sizeof(long)); + + DecDCTReset(&ctxt,0); + DecDCTvlc(bufp,rl); + DecDCTin(&ctxt,rl,mode); + + slice = height2*w/2; + + for(x=0;x=0;y--) + { + memcpy(dst,src,w*2); + src+=w; + dst-=width; + } + } + + free (image); + free (rl); +} diff --git a/str-util.cpp b/str-util.cpp index cf3f6ff..3588707 100644 --- a/str-util.cpp +++ b/str-util.cpp @@ -1,5 +1,6 @@ #include #include +//#include "psxdev/bs.h" #include "fileutils.h" #include "generic.h" #include "cdutils.h" @@ -24,73 +25,6 @@ Channels : 2 bytes 30 */ -int DCT[8][8] = { - { 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, }, - { 5681, 4816, 3218, 1130, -1130, -3218, -4816, -5681, }, - { 5352, 2217, -2217, -5532, -5532, -2217, 2217, 5352, }, - { 4816, -1130, -5681, -3218, 3218, 5681, 1130, -4816, }, - { 4096, -4096, 4096, -4096, 4096, -4096, 4096, -4096, }, - { 3218, -5681, 1130, 4816, -4816, -1130, 5681, -3218, }, - { 2217, -5352, 5352, -2217, -2217, 5352, -5352, 2217, }, - { 1130, -3218, 4816, -5681, 5681, -4816, 3218, -1130, }, -}; - -int iDCT[8][8] = { - { 4096, 5681, 5352, 4816, 4096, 3218, 2217, 1130, }, - { 4096, 4816, 2217, -1130, -4096, -5681, -5352, -3218, }, - { 4096, 3218, -2217, -5681, 4096, 1130, 5352, 4816, }, - { 4096, 1130, -5532, -3218, -4096, 4816, -2217, -5681, }, - { 4096, -1130, -5532, 3218, 4096, -4816, -2217, 5681, }, - { 4096, -3218, -2217, 5681, -4096, -1130, 5352, -4816, }, - { 4096, -4816, 2217, 1130, 4096, 5681, -5352, 3218, }, - { 4096, -5681, 5352, -4816, -4096, -3218, 2217, -1130, }, -}; - -int zscan[] = { - 0, 1, 8, 16, 9, 2, 3, 10, - 17, 24, 32, 25, 18, 11, 4, 5, - 12, 19, 26, 33, 40, 48, 41, 34, - 27, 20, 13, 6, 7, 14, 21, 28, - 35, 42, 49, 56, 57, 50, 43, 36, - 29, 22, 15, 23, 30, 37, 44, 51, - 58, 59, 52, 45, 38, 31, 39, 46, - 53, 60, 61, 54, 47, 55, 62, 63, -}; - -int izscan[] = { - 0, 1, 5, 6, 14, 15, 27, 28, - 2, 4, 7, 13, 16, 26, 29, 42, - 3, 8, 12, 17, 25, 30, 41, 43, - 9, 11, 18, 24, 31, 40, 44, 53, - 10, 19, 23, 32, 39, 45, 52, 54, - 20, 22, 33, 38, 46, 51, 55, 60, - 21, 34, 37, 47, 50, 56, 59, 61, - 35, 36, 48, 49, 57, 58, 62, 63, -}; - -int iqtab[] = { - 2, 16, 19, 22, 26, 27, 29, 34, - 16, 16, 22, 24, 27, 29, 34, 37, - 19, 22, 26, 27, 29, 34, 34, 38, - 22, 22, 26, 27, 29, 34, 37, 40, - 22, 26, 27, 29, 32, 35, 40, 48, - 26, 27, 29, 32, 35, 40, 48, 58, - 26, 27, 29, 34, 38, 46, 56, 69, - 27, 29, 35, 38, 46, 56, 69, 83, -}; - -float RGB2YUV[3][3] = { - { 1.00000, 0.00000, 1.40200, }, - { 1.00000, -0.34370, -0.71430, }, - { 1.00000, 1.77200, 0.00000, }, -}; - -float YUV2RGB[3][3] = { - { 0.22900, 0.58700, 0.11400, }, - { -0.16871, -0.33130, 0.50000, }, - { 0.50000, -0.41870, -0.08130, }, -}; - struct STR_Header { Uint16 StSTATUS; Uint16 StTYPE; @@ -107,46 +41,6 @@ struct STR_Header { Byte * video = 0, * audio = 0; -void print_bits(Uint16 w, int size = 16) { - int i; - - for (i = size - 1; i >= 0; i--) { - printm(M_BARE, "%c", w & (1 << i) ? '1' : '0'); - } -} - -Uint32 get_bits(Byte * datas, int size, Uint32 * pointer) { - static int internal = 0; - static Uint16 w = 0; - Uint32 r = 0; - int os = size; - - if (internal < size) { - r = w; - size -= internal; - w = *((Uint16 *) (video + *pointer)); - printm(M_BARE, "get_bits: needs food, got 0x%04x\n", w); - *pointer += 2; -// w = swap_word(w); - internal = 16; - } - - r <<= size; - r |= ((w >> (16 - size)) & ((1 << size) - 1)); - - w <<= size; - - printm(M_BARE, "Read bits: "); - print_bits(r, os); - printm(M_BARE, "\n"); - - internal -= size; - - r &= ((1 << size) - 1); - - return r; -} - void process_one_sector(FILE * f) { Byte sector[2336]; STR_Header * h; @@ -179,75 +73,9 @@ void process_one_sector(FILE * f) { memcpy(video + h->StSECTOR_OFFSET * 2016, sector + 40, 2016); if (h->StSECTOR_SIZE == (h->StSECTOR_OFFSET + 1)) { - Uint32 pointer = 0; - Uint32 macrobloc = 0; // Frame finished. printm(M_BARE, "End of Frame.\n"); -// fwrite(video, 1, h->StSECTOR_SIZE * 2016, stdout); - - Uint16 rl_size = *((Uint16 *) (video + pointer)); - pointer += 2; - if (*((Uint16 *) (video + pointer)) != 0x3800) { - printm(M_ERROR, "Broken frame\n"); -// break; - } - pointer += 2; - - Uint16 quant = *((Uint16 *) (video + pointer)); - pointer += 2; - Uint16 ver = *((Uint16 *) (video + pointer)); - pointer += 2; - int rls = 0; - while (rls <= rl_size) { - printm(M_BARE, "Processing macrobloc %i, rlsize = %i, quant = %i, ver = %i, pointer = 0x%08x\n", macrobloc, rl_size, quant, ver, pointer); - - int16 Cb[64], Cr[64], Y0[64], Y1[64], Y2[64], Y3[64]; - int16 * blocs[6] = {Cb, Cr, Y0, Y1, Y2, Y3}; - int bloc; - - for (bloc = 0; bloc < 6; bloc++) { - int i; - - for (i = 0; i < 64; i++) { - blocs[bloc][i] = 0; - } - - int16 DC = get_bits(video, 10, &pointer); - if (DC & 0x20) - DC |= 0xc0; - - printm(M_BARE, "Processing subbloc %i, DC = %i, Quant = %i, pointer = 0x%08x\n", bloc, DC, quant, pointer); - - int inbloc = 1; - blocs[bloc][0] = DC; - - while (inbloc < 64) { - Uint8 run = get_bits(video, 6, &pointer); - int16 level; - - level = get_bits(video, 8, &pointer); - - if (level == 0) { - level = get_bits(video, 8, &pointer); - } else if (level & 0x80) { - if (level == 0x80) - level = get_bits(video, 8, &pointer); - level |= 0xff00; - } - - printm(M_BARE, "Read a runlevel: (%i, %i) = (%x %x), inbloc = %i, (pointer is %i)\n", run, level, run, level, inbloc, pointer); - rls++; - - blocs[bloc][inbloc] = level; - inbloc += run + 1; - } - - printm(M_BARE, "Finished the bloc with inbloc = %i\n", inbloc); - } - macrobloc++; - } - exit(-1); free(video); } @@ -259,12 +87,8 @@ void process_one_sector(FILE * f) { printm(M_BARE, "---------------------------------\n\n"); } -#ifdef STR_MAIN - int main(void) { while (!feof(stdin)) { process_one_sector(stdin); } } - -#endif diff --git a/yazedc.cpp b/yazedc.cpp index 9d5b838..f4bced0 100644 --- a/yazedc.cpp +++ b/yazedc.cpp @@ -856,162 +856,3 @@ int set_sector_type(int st) } /* ------------- --------------*/ -#ifdef MAIN - -#define DO_L1 1 -#define DO_L2 2 -#define DO_SUB 4 - -static const unsigned sect_size[8][2] = { -/* nothing */ -{0,0}, -/* Layer 1 decode/encode */ -{ (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR, L1_RAW*FRAMES_PER_SECTOR}, -/* Layer 2 decode/encode */ -{ 16+L2_RAW+12+L2_Q+L2_P, L2_RAW}, -/* Layer 1 and 2 decode/encode */ -{ (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR, L1_RAW*FRAMES_PER_SECTOR}, -/* Subchannel decode/encode */ -{ (LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME, - LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME}, -/* Layer 1 and subchannel decode/encode */ -{ (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR + - (LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME, - LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME + - L1_RAW*FRAMES_PER_SECTOR}, -/* Layer 2 and subchannel decode/encode */ -{ L2_RAW+L2_Q+L2_P+ - (LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME, - LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME + - L2_RAW}, -/* Layer 1, 2 and subchannel decode/encode */ -{ (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR + - (LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME, - LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME + - L1_RAW*FRAMES_PER_SECTOR}, -}; - -int main(int argc, char **argv) -{ - int encode = 1; - int mask = DO_L2; - FILE * infp; - FILE * outfp; - unsigned address = 0; - unsigned char *l1_inbuf; - unsigned char *l1_outbuf; - unsigned char *l2_inbuf; - unsigned char *l2_outbuf; - unsigned char *sub_inbuf; - unsigned char *sub_outbuf; - unsigned char *last_outbuf; - unsigned char inbuf[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME + - (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR]; - unsigned char outbuf[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME + - (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR]; - unsigned load_offset; - - l1_inbuf = l2_inbuf = sub_inbuf = inbuf; - l1_outbuf = l2_outbuf = sub_outbuf = last_outbuf = outbuf; - - infp = fopen("sectors_in", "rb"); - outfp = fopen("sectors_out", "wb"); - - sectortype= MODE_2_FORM_1; - address = 0 + 75*2; - - switch (sectortype) { - case MODE_1: - case MODE_2: - load_offset = 16; - break; - case MODE_2_FORM_1: - case MODE_2_FORM_2: - load_offset = 24; - break; - default: - load_offset = 0; - } - while(1) { - - if (1 != fread(inbuf+load_offset, - sect_size[mask][encode], 1, infp)) { perror(""); break; } - if (encode == 1) { - if (mask & DO_L2) { - switch (sectortype) { - case MODE_0: - break; - case MODE_1: - break; - case MODE_2: - if (1 != - fread(inbuf+load_offset+ - sect_size[mask][encode], - 2336 - sect_size[mask][encode], - 1, infp)) { perror(""); break; } - break; - case MODE_2_FORM_1: - break; - case MODE_2_FORM_2: - if (1 != - fread(inbuf+load_offset+ - sect_size[mask][encode], - 2324 - sect_size[mask][encode], - 1, infp)) { perror(""); break; } - break; - default: - if (1 != - fread(inbuf+load_offset+ - sect_size[mask][encode], - 2448 - sect_size[mask][encode], - 1, infp)) { perror(""); break; } - memset(inbuf,0,16); - /*memset(inbuf+16+2048,0,12+272);*/ - break; - } - do_encode_L2(l2_inbuf, MODE_1, address); - if (0) scramble_L2(l2_inbuf); - last_outbuf = l1_inbuf = l2_inbuf; - l1_outbuf = l2_inbuf; - sub_inbuf = l2_inbuf + L2_RAW; - sub_outbuf = l2_outbuf + 12 + 4+ L2_RAW+4+ 8+ L2_Q+L2_P; - } - if (mask & DO_L1) { - do_encode_L1(l1_inbuf, l1_outbuf,1,1,1,1); - last_outbuf = l1_outbuf; - sub_inbuf = l1_inbuf + L1_RAW*FRAMES_PER_SECTOR; - sub_outbuf = l1_outbuf + (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR; - } - if (mask & DO_SUB) { - do_encode_sub(sub_inbuf, sub_outbuf, 0, 0); - } - } else { - if (mask & DO_L1) { - do_decode_L1(l1_inbuf, l1_outbuf,1,1,1,1); - last_outbuf = l2_inbuf = l1_outbuf; - l2_outbuf = l1_inbuf; - sub_inbuf = l1_inbuf + (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR; - sub_outbuf = l1_outbuf + L1_RAW*FRAMES_PER_SECTOR; - } - if (mask & DO_L2) { - do_decode_L2(l2_inbuf, l2_outbuf); - last_outbuf = l2_outbuf; - sub_inbuf = l2_inbuf + L2_RAW+L2_Q+L2_P; - sub_outbuf = l2_outbuf + L2_RAW; - } - if (mask & DO_SUB) { - do_decode_sub(sub_inbuf, sub_outbuf, 1, 1); - } - } - if (1 != fwrite(last_outbuf, sect_size[mask][1 - encode], 1, outfp)) { - perror(""); - break; - } - address++; - } -#if 0 - /* flush the data from the delay lines with zeroed sectors, if necessary */ -#endif - return 0; -} -#endif -- cgit v1.2.3