2 /**************************************************************************
4 * This code is developed by Adam Li. This software is an *
5 * implementation of a part of one or more MPEG-4 Video tools as *
6 * specified in ISO/IEC 14496-2 standard. Those intending to use this *
7 * software module in hardware or software products are advised that its *
8 * use may infringe existing patents or copyrights, and any such use *
9 * would be at such party's own risk. The original developer of this *
10 * software module and his/her company, and subsequent editors and their *
11 * companies (including Project Mayo), will have no liability for use of *
12 * this software or modifications or derivatives thereof. *
14 * Project Mayo gives users of the Codec a license to this software *
15 * module or modifications thereof for use in hardware or software *
16 * products claiming conformance to the MPEG-4 Video Standard as *
17 * described in the Open DivX license. *
19 * The complete Open DivX license can be found at *
20 * http://www.projectmayo.com/opendivx/license.php . *
22 **************************************************************************/
24 /**************************************************************************
28 * Copyright (C) 2001 Project Mayo
32 * DivX Advance Research Center <darc@projectmayo.com>
34 **************************************************************************/
36 #include "bitstream.h"
38 /* to mask the n least significant bits of an integer */
40 static unsigned int mask
[33] =
42 0x00000000, 0x00000001, 0x00000003, 0x00000007,
43 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
44 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
45 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,
46 0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,
47 0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
48 0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,
49 0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
54 /* static data for pointers and counters */
56 /* byte pointer to the output bitstream */
57 static unsigned char *byteptr
;
58 /* counter of how many bytes got written to the bitstream */
61 /* a one byte temporary buffer */
62 static unsigned char outbfr
;
63 /* counter of how many unused bits left in the byte buffer */
66 void Bitstream_Init(void *buffer
)
68 byteptr
= (unsigned char *)buffer
;
74 void Bitstream_PutBits(int n
, unsigned int val
)
77 //printf("Bitstream_PutBits %d %02x\n", n, val);
79 while ((diff
= n
- outcnt
) >= 0) { /* input is longer than what is left in the buffer */
80 outbfr
|= (unsigned char)(val
>> diff
);
81 /* note that the first byte of the integer is the least significant byte */
85 *(byteptr
++) = outbfr
;
91 if (n
> 0) { /* input is short enough to fit in what is left in the buffer */
92 outbfr
|= (unsigned char)(val
<< (-diff
));
100 while (outcnt
!= 8) Bitstream_PutBits(1, 1);
105 int Bitstream_NextStartCode()
109 Bitstream_PutBits(1,0);
110 while (outcnt
!= 8) Bitstream_PutBits(1, 1);
116 int Bitstream_GetLength()