/***************************************************************** * tmn (TMN encoder) * Copyright (C) 1995 Telenor R&D * Karl Olav Lillevold * * These routines are written by Andy C. Hung * *****************************************************************/ /************************************************************* Copyright (C) 1990, 1991, 1993 Andy C. Hung, all rights reserved. PUBLIC DOMAIN LICENSE: Stanford University Portable Video Research Group. If you use this software, you agree to the following: This program package is purely experimental, and is licensed "as is". Permission is granted to use, modify, and distribute this program without charge for any purpose, provided this license/ disclaimer notice appears in the copies. No warranty or maintenance is given, either expressed or implied. In no event shall the author(s) be liable to you or a third party for any special, incidental, consequential, or other damages, arising out of the use or inability to use the program for any purpose (or the loss of data), even if we have been advised of such possibilities. Any public reference or advertisement of this source code should refer to it as the Portable Video Research Group (PVRG) code, and not by any author(s) (or Stanford University) name. *************************************************************/ /* ************************************************************ stream.c This file handles all of the bit-level stream commands. ************************************************************ */ /*LABEL stream.c */ #include"sim.h" extern char *BitPrint(int length, int val); long mwtell(); static FILE *swout; static FILE *en_swout; static int current_write_byte; static int write_position; static int en_write_byte; static int en_write_position; int bit_set_mask[] = {0x00000001,0x00000002,0x00000004,0x00000008, 0x00000010,0x00000020,0x00000040,0x00000080, 0x00000100,0x00000200,0x00000400,0x00000800, 0x00001000,0x00002000,0x00004000,0x00008000, 0x00010000,0x00020000,0x00040000,0x00080000, 0x00100000,0x00200000,0x00400000,0x00800000, 0x01000000,0x02000000,0x04000000,0x08000000, 0x10000000,0x20000000,0x40000000,0x80000000}; #ifdef DEBUG_BITSTREAM #define mput1()\ {current_write_byte|=bit_set_mask[write_position--];fprintf(stderr,"1");fflush(stderr);\ if (write_position<0) {putc(current_write_byte,swout); write_position=7;current_write_byte=0;}} #define mput0()\ {write_position--;fprintf(stderr,"0");fflush(stderr);if(write_position<0){putc(current_write_byte,swout);write_position=7;current_write_byte=0;}} #endif #ifndef DEBUG_BITSTREAM #define mput1()\ {current_write_byte|=bit_set_mask[write_position--];\ if (write_position<0) {putc(current_write_byte,swout); write_position=7;current_write_byte=0;}} #define mput0()\ {write_position--;if(write_position<0){putc(current_write_byte,swout);write_position=7;current_write_byte=0;}} #endif #define en_mput1()\ {en_write_byte|=bit_set_mask[en_write_position--];\ if (en_write_position<0) {putc(en_write_byte,en_swout); en_write_position=7;en_write_byte=0;}} #define en_mput0()\ {en_write_position--;if(en_write_position<0){putc(en_write_byte,en_swout);en_write_position=7;en_write_byte=0;}} /* mwopen() opens a bit stream for writing. */ void mwopen(char *filename) { current_write_byte=0; write_position=7; if ((swout = fopen(filename,"wb+")) == NULL) { printf("Cannot Open Output File\n"); exit(-1); } } void en_mwopen(char *filename) { en_write_byte=0; en_write_position=7; if ((en_swout = fopen(filename,"wb+")) == NULL) { printf("Cannot Open Output File\n"); exit(-1); } } /* mwclose() closes the write bit-stream. It flushes the remaining byte with ones, consistent with -1 returned on EOF. */ void mwclose() { while(write_position!=7) { mput1(); } fflush(swout); fclose(swout); } void en_mwclose() { while(en_write_position!=7) { en_mput1(); } fflush(en_swout); fclose(en_swout); } /* zeroflush() flushes out the rest of the byte with 0's. returns number og bits written to bitstream (kol) */ int zeroflush() { int bits = 0; /* printf("WP: %d\n",write_position);*/ while (write_position!=7) { mput0(); bits++; } return bits; } int en_zeroflush() { int bits = 0; /* printf("WP: %d\n",en_write_position);*/ while (en_write_position!=7) { en_mput0(); bits++; } return bits; } /* mputb() puts a bit to the write stream. */ void mputb(int b) { char *bit; if (trace) { bit = BitPrint(1,b); fprintf(tf,"at %d, wrote %s = %d\n", (int)mwtell(), bit, b); free(bit); } if (b) {mput1();} else {mput0();} } void en_mputb(int b) { char *bit; if (b) {en_mput1();} else {en_mput0();} } /* mputv() puts a n bits to the stream from byte b. */ void mputv(int n,int b) { char *bit; if (trace) { bit = BitPrint(n, b); fprintf(tf,"at %d, wrote %s = %d\n", (int)mwtell(), bit, b); free(bit); } while(n--) { if(b&bit_set_mask[n]) {mput1();} else {mput0();} } } void en_mputv(int n,int b) { char *bit; while(n--) { if(b&bit_set_mask[n]) {en_mput1();} else {en_mput0();} } } /* mwtell() returns the position in bits of the write stream. */ long mwtell() { return((ftell(swout)<<3) + (7 - write_position)); } /* mwseek() seeks to a specific bit position on the write stream. */ void mwseek(long distance) { int Length; if (write_position != 7) {putc(current_write_byte,swout);} fseek(swout,0,2L); Length = ftell(swout); fseek(swout,(distance+7)>>3,0L); if ((Length << 3) <= distance) /* Make sure we read clean stuff */ { current_write_byte = 0; write_position = 7 - (distance & 0x7); } else { current_write_byte = getc(swout); write_position = 7 - (distance & 0x7); fseek(swout,(distance+7)>>3,0L); } }