00001 00002 /************************************************************************** 00003 * * 00004 * This code is developed by Adam Li. This software is an * 00005 * implementation of a part of one or more MPEG-4 Video tools as * 00006 * specified in ISO/IEC 14496-2 standard. Those intending to use this * 00007 * software module in hardware or software products are advised that its * 00008 * use may infringe existing patents or copyrights, and any such use * 00009 * would be at such party's own risk. The original developer of this * 00010 * software module and his/her company, and subsequent editors and their * 00011 * companies (including Project Mayo), will have no liability for use of * 00012 * this software or modifications or derivatives thereof. * 00013 * * 00014 * Project Mayo gives users of the Codec a license to this software * 00015 * module or modifications thereof for use in hardware or software * 00016 * products claiming conformance to the MPEG-4 Video Standard as * 00017 * described in the Open DivX license. * 00018 * * 00019 * The complete Open DivX license can be found at * 00020 * http://www.projectmayo.com/opendivx/license.php . * 00021 * * 00022 **************************************************************************/ 00023 00024 /************************************************************************** 00025 * 00026 * mom_bitstream.c 00027 * 00028 * Copyright (C) 2001 Project Mayo 00029 * 00030 * Adam Li 00031 * 00032 * DivX Advance Research Center <darc@projectmayo.com> 00033 * 00034 **************************************************************************/ 00035 00036 #include "bitstream.h" 00037 00038 /* to mask the n least significant bits of an integer */ 00039 00040 static unsigned int mask[33] = 00041 { 00042 0x00000000, 0x00000001, 0x00000003, 0x00000007, 00043 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f, 00044 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff, 00045 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, 00046 0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff, 00047 0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff, 00048 0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff, 00049 0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff, 00050 0xffffffff 00051 }; 00052 00053 00054 /* static data for pointers and counters */ 00055 00056 /* byte pointer to the output bitstream */ 00057 static unsigned char *byteptr; 00058 /* counter of how many bytes got written to the bitstream */ 00059 static int bytecnt; 00060 00061 /* a one byte temporary buffer */ 00062 static unsigned char outbfr; 00063 /* counter of how many unused bits left in the byte buffer */ 00064 static int outcnt; 00065 00066 void Bitstream_Init(void *buffer) 00067 { 00068 byteptr = (unsigned char *)buffer; 00069 bytecnt = 0; 00070 outbfr = 0; 00071 outcnt = 8; 00072 } 00073 00074 void Bitstream_PutBits(int n, unsigned int val) 00075 { 00076 int diff; 00077 //printf("Bitstream_PutBits %d %02x\n", n, val); 00078 00079 while ((diff = n - outcnt) >= 0) { /* input is longer than what is left in the buffer */ 00080 outbfr |= (unsigned char)(val >> diff); 00081 /* note that the first byte of the integer is the least significant byte */ 00082 n = diff; 00083 val &= mask[n]; 00084 00085 *(byteptr ++) = outbfr; 00086 bytecnt++; 00087 outbfr = 0; 00088 outcnt = 8; 00089 } 00090 00091 if (n > 0) { /* input is short enough to fit in what is left in the buffer */ 00092 outbfr |= (unsigned char)(val << (-diff)); 00093 outcnt -= n; 00094 } 00095 } 00096 00097 00098 int Bitstream_Close() 00099 { 00100 while (outcnt != 8) Bitstream_PutBits(1, 1); 00101 return bytecnt; 00102 } 00103 00104 00105 int Bitstream_NextStartCode() 00106 { 00107 int count = outcnt; 00108 00109 Bitstream_PutBits(1,0); 00110 while (outcnt != 8) Bitstream_PutBits(1, 1); 00111 00112 return (count); 00113 } 00114 00115 00116 int Bitstream_GetLength() 00117 { 00118 return bytecnt; 00119 } 00120 00121
1.5.5