00001 /************************************************************************ 00002 * 00003 * vid_putbits.c, bitstream handling for tmn (TMN encoder) 00004 * Copyright (C) 1995, 1996 Telenor R&D, Norway 00005 * Karl Olav Lillevold <Karl.Lillevold@nta.no> 00006 * 00007 * Contacts: 00008 * Karl Olav Lillevold <Karl.Lillevold@nta.no>, or 00009 * Robert Danielsen <Robert.Danielsen@nta.no> 00010 * 00011 * Telenor Research and Development http://www.nta.no/brukere/DVC/ 00012 * P.O.Box 83 tel.: +47 63 84 84 00 00013 * N-2007 Kjeller, Norway fax.: +47 63 81 00 76 00014 * 00015 ************************************************************************/ 00016 00017 /* 00018 * Disclaimer of Warranty 00019 * 00020 * These software programs are available to the user without any 00021 * license fee or royalty on an "as is" basis. Telenor Research and 00022 * Development disclaims any and all warranties, whether express, 00023 * implied, or statuary, including any implied warranties or 00024 * merchantability or of fitness for a particular purpose. In no 00025 * event shall the copyright-holder be liable for any incidental, 00026 * punitive, or consequential damages of any kind whatsoever arising 00027 * from the use of these programs. 00028 * 00029 * This disclaimer of warranty extends to the user of these programs 00030 * and user's customers, employees, agents, transferees, successors, 00031 * and assigns. 00032 * 00033 * Telenor Research and Development does not represent or warrant that 00034 * the programs furnished hereunder are free of infringement of any 00035 * third-party patents. 00036 * 00037 * Commercial implementations of H.263, including shareware, are 00038 * subject to royalty fees to patent holders. Many of these patents 00039 * are general enough such that they are unavoidable regardless of 00040 * implementation design. 00041 * */ 00042 00043 00044 /************************************************************************ 00045 * Based on functions in the MPEG-2 software encoder by the 00046 * MPEG-2 Software Simulation Group, modified by 00047 * 00048 * Robert Danielsen, <Robert.Danielsen@nta.no> 00049 ************************************************************************/ 00050 00051 00052 /* 00053 * Modified for use with H223/A MUX on 8/23/96 by Brendan Dowling 00054 */ 00055 00056 #include "vid_sim.h" 00057 #include "video_codec.h" 00058 #include "al_sending.h" 00059 00060 extern video_codec *VidSt; 00061 00062 /* initialize buffer, call once before first putbits or alignbits */ 00063 void 00064 initbits() 00065 { 00066 (VidSt->outcnt) = 8; 00067 (VidSt->bytecnt) = 0; 00068 } 00069 00070 /* write rightmost n (0<=n<=32) bits of val to outfile */ 00071 00072 void 00073 putbits (int n, int val) 00074 { 00075 int i; 00076 unsigned int mask; 00077 char bitstring[32]; 00078 00079 if ((VidSt->trace)) { 00080 if (n > 0) { 00081 BitPrint(n,val,bitstring); 00082 fprintf((VidSt->tf),bitstring); 00083 } 00084 } 00085 00086 mask = 1 << (n-1); /* selects first (leftmost) bit */ 00087 00088 for (i=0; i<n; i++) { 00089 (VidSt->outbfr) <<= 1; 00090 00091 if (val & mask) 00092 (VidSt->outbfr)|= 1; 00093 00094 mask >>= 1; /* select next bit */ 00095 (VidSt->outcnt)--; 00096 00097 if ((VidSt->outcnt)==0) /* 8 bit buffer full */ { 00098 putc((VidSt->outbfr),(VidSt->streamfile)); 00099 00100 if (al_send_byte((VidSt->output), (VidSt->outbfr)) == 0) 00101 printf("!: \"%s\" overwrote AL-buffer!\n", (VidSt->label)); 00102 00103 (VidSt->simbuffer)++; 00104 00105 (VidSt->outcnt) = 8; 00106 (VidSt->bytecnt)++; 00107 } 00108 } 00109 } 00110 00111 00112 /* zero bit stuffing to next byte boundary (5.2.3, 6.2.1) */ 00113 00114 int 00115 alignbits () 00116 { 00117 int ret_value; 00118 00119 if ((VidSt->outcnt)!=8) { 00120 ret_value = (VidSt->outcnt); /* outcnt is reset in call to putbits () */ 00121 putbits ((VidSt->outcnt), 0); 00122 return ret_value; 00123 } 00124 else 00125 return 0; 00126 } 00127 00128 /* return total number of generated bits */ 00129 int 00130 bitcount() 00131 { 00132 return 8*(VidSt->bytecnt) + (8-(VidSt->outcnt)); 00133 } 00134 00135 /* convert to binary number */ 00136 void 00137 BitPrint(int length, int val, char *bit) 00138 { 00139 int m; 00140 00141 m = length; 00142 bit[0] = '"'; 00143 while (m--) { 00144 bit[length-m] = (val & (1<<m)) ? '1' : '0'; 00145 } 00146 bit[length+1] = '"'; 00147 bit[length+2] = '\n'; 00148 bit[length+3] = '\0'; 00149 return; 00150 } 00151