# HG changeset patch # User Matti Hamalainen # Date 1543856549 -7200 # Node ID c67d863384a5f31f1ee2a95b4510d2a287e1bb1d # Parent 416af5a842ec65dc761b5ccf56018cc66d83b6d1 Changes to the BR1 encoder test. diff -r 416af5a842ec -r c67d863384a5 tests/encbr1test.c --- a/tests/encbr1test.c Mon Dec 03 19:01:07 2018 +0200 +++ b/tests/encbr1test.c Mon Dec 03 19:02:29 2018 +0200 @@ -1,9 +1,10 @@ #include "dmtool.h" #include "dmlib.h" #include "dmres.h" +#include "dmmutex.h" -#define BR_DEBUG +#define BR_DEBUG 1 //#define BR_WRITE enum @@ -13,58 +14,71 @@ }; -BOOL dmIFFEncodeByteRun1Flush( - DMResource *fp, const int mode, const BOOL flush, - size_t *l_offs, const size_t offs, const Uint8 *buf, - const Uint8 data, unsigned int *r_count) -{ -#ifdef BR_DEBUG - if (flush) - printf("FLUSH:\n"); -#endif +#if BR_DEBUG == 1 +static int mcount = 0; +static int moffs = 0; - if (mode == DMODE_LIT) - { - size_t l_count = offs - *l_offs; - if (l_count > *r_count || flush) - { - size_t count = l_count - *r_count; - Uint8 tmp = count - 1; - -#ifdef BR_DEBUG - printf("L: "); - for (size_t n = 0; n < count; n++) - printf("%02x ", buf[*l_offs + n]); - printf(" [%" DM_PRIu_SIZE_T " -> %d]\n", count, tmp); +static void mprintbyte(const Uint8 data) +{ + if (mcount == 0) printf("%04x | ", moffs); + printf("%02x ", data); + if (mcount++ >= 15) { printf("\n"); mcount = 0; } + moffs++; +} #endif -#ifdef BR_WRITE - if (!dmf_write_byte(fp, tmp) || - !dmf_write_str(fp, buf + *l_offs, count)) - return FALSE; -#endif - } - (*r_count)++; - } - else - { - if (*r_count > 0) - { - unsigned int count = *r_count; - Uint8 tmp = ((Uint8) count - 2) ^ 0xff; + +BOOL dmIFFEncodeByteRun1LIT(DMResource *fp, + const Uint8 *buf, const size_t offs, + const size_t count) +{ + if (count <= 0) + return TRUE; + + Uint8 tmp = count - 1; + + #if BR_DEBUG == 2 + printf("L: %02x ", tmp); + for (size_t n = 0; n < count; n++) + printf("%02x ", buf[offs + n]); + printf("[%" DM_PRIu_SIZE_T "]\n", count); + #elif BR_DEBUG == 1 + mprintbyte(tmp); + for (size_t n = 0; n < count; n++) + mprintbyte(buf[offs + n]); + #endif -#ifdef BR_DEBUG - printf("R: %02x x %x [%02x]\n", data, count, tmp); -#endif -#ifdef BR_WRITE - if (!dmf_write_byte(fp, tmp) || - !dmf_write_byte(fp, data)) - return FALSE; -#endif - *r_count = 0; - } - *l_offs = offs; - } + #ifdef BR_WRITE + if (!dmf_write_byte(fp, tmp) || + !dmf_write_str(fp, buf + offs, count)) + return FALSE; + #endif + + return TRUE; +} + + +BOOL dmIFFEncodeByteRun1RLE(DMResource *fp, + const Uint8 *buf, const size_t offs, + const size_t count) +{ + if (count <= 0) + return TRUE; + + Uint8 tmp = ((Uint8) count - 2) ^ 0xff; + Uint8 data = buf[offs]; + + #if BR_DEBUG == 2 + printf("R: %02x %02x [%" DM_PRIu_SIZE_T "]\n", tmp, data, count); + #elif BR_DEBUG == 1 + mprintbyte(tmp); + mprintbyte(data); + #endif + #ifdef BR_WRITE + if (!dmf_write_byte(fp, tmp) || + !dmf_write_byte(fp, data)) + return FALSE; + #endif return TRUE; } @@ -72,97 +86,153 @@ BOOL dmIFFEncodeByteRun1Row(DMResource *fp, const Uint8 *buf, const size_t bufLen) { - unsigned int r_count = 0; int prev = -1, mode = DMODE_LIT; - size_t offs, l_offs = 0; + size_t offs, l_offs, r_offs; + BOOL ret = TRUE; -#ifdef BR_DEBUG - printf("\n\nByteRUN1ROW: %" DM_PRIu_SIZE_T " = $%" DM_PRIx_SIZE_T "\n", bufLen, bufLen); -#endif - for (offs = 0; offs < bufLen; offs++) + #if BR_DEBUG == 1 + mcount = 0; + #endif + + for (offs = l_offs = r_offs = 0; offs < bufLen; offs++) { Uint8 data = buf[offs]; - int next_mode; - BOOL flush; + BOOL flush = FALSE; + int pmode = mode; if (data == prev) { - r_count++; - next_mode = DMODE_RLE; + if (mode == DMODE_LIT && + offs - r_offs >= 2) + { + ret = dmIFFEncodeByteRun1LIT(fp, buf, l_offs, r_offs - l_offs); + mode = DMODE_RLE; + } } else { - next_mode = DMODE_LIT; + if (mode != DMODE_LIT) + { + ret = dmIFFEncodeByteRun1RLE(fp, buf, r_offs, offs - r_offs); + mode = DMODE_LIT; + l_offs = offs; + } + r_offs = offs; + } + + if (!ret) + goto out; + +#if BR_DEBUG == 2 + printf("%04" DM_PRIx_SIZE_T ": %02x | r_count=%" DM_PRIu_SIZE_T ", l_count=%" DM_PRIu_SIZE_T ", mode=%s | [%04" DM_PRIx_SIZE_T ", %04" DM_PRIx_SIZE_T "]", + offs, data, offs - r_offs, offs - l_offs, + mode == DMODE_RLE ? "RLE" : "LIT", + r_offs, l_offs); +#endif + + // NOTE! RLE max is 128, checked against DP2e + // Not sure about LIT max yet + if ((pmode == DMODE_RLE && offs - r_offs >= 128) || + (pmode == DMODE_LIT && offs - l_offs >= 128)) + { + flush = TRUE; +#if BR_DEBUG == 2 + printf(" "); +#endif } -//#ifdef BR_DEBUG -#if 0 - printf("%08" DM_PRIx_SIZE_T ": %02x | r_count=%x, l_count=%" DM_PRIx_SIZE_T ", mode=%s [%08" DM_PRIx_SIZE_T "]\n", - offs, data, r_count, offs - l_offs + 1, mode == DMODE_RLE ? "RLE" : "LIT", l_offs); + // Check for last byte of input + if (offs == bufLen - 1) + { + offs++; + flush = TRUE; + pmode = mode; +#if BR_DEBUG == 2 + printf(" "); +#endif + } + +#if BR_DEBUG == 2 + printf("\n"); #endif - flush = offs - l_offs >= 128 || r_count >= 128; - if ((next_mode != mode || flush) && - !dmIFFEncodeByteRun1Flush(fp, mode, flush, &l_offs, offs, buf, prev, &r_count)) - return FALSE; + if (flush) + { + if (pmode == DMODE_RLE) + ret = dmIFFEncodeByteRun1RLE(fp, buf, r_offs, offs - r_offs); + else + ret = dmIFFEncodeByteRun1LIT(fp, buf, l_offs, offs - l_offs); - mode = next_mode; + r_offs = l_offs = offs; + mode = DMODE_LIT; + + if (!ret) + goto out; + } + prev = data; } -#ifdef BR_DEBUG - printf("END\n"); -#endif - - if (!dmIFFEncodeByteRun1Flush(fp, mode, TRUE, &l_offs, offs, buf, prev, &r_count)) - return FALSE; - - return TRUE; +out: + return ret; } int main(int argc, char *argv[]) { + DMResource *fp = NULL; (void) argc; (void) argv; - Uint8 test[] = +#ifdef BR_WRITE + dmf_open_stdio_stream(stderr, &fp); +#endif + + static const Uint8 test[] = { - 1, - 0, 0, 0, - 2, - 0, 0, 0, 0, - 3, 3, 3, - 2, 2, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 1, - 0, 0, 0, 0, 0, 0, 0, 0, - - 1, 2, 3, 4, 5, 6, 7, 8, - - 0, 0, 0, 0, 0, - - 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -// 15, + 2, 2, 3, 4, 5, 5, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1 - //0, 0, 0, 0, - //1 + 1, 3, + 0, 0, 0, 1, }; - dmIFFEncodeByteRun1Row(NULL, test, sizeof(test)); + { + int cnt = 0; + for (size_t xc = 0; xc < sizeof(test); xc++) + { + if (cnt == 0) printf("%04x | ", xc); + printf("%02x ", *(test + xc)); + if (cnt++ >= 15) { printf("\n"); cnt = 0; } + } + printf("\n\n"); + + dmIFFEncodeByteRun1Row(fp, test, sizeof(test)); + printf("\n--\n"); + } + +#ifdef BR_WRITE + dmf_close(fp); +#endif + return 0; }