Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / quicktime / encore50 / bitstream.c
blob017f96911475586c4c28ce15670b237636f92309
2 /**************************************************************************
3 * *
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. *
13 * *
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. *
18 * *
19 * The complete Open DivX license can be found at *
20 * http://www.projectmayo.com/opendivx/license.php . *
21 * *
22 **************************************************************************/
24 /**************************************************************************
26 * mom_bitstream.c
28 * Copyright (C) 2001 Project Mayo
30 * Adam Li
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,
50 0xffffffff
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 */
59 static int bytecnt;
61 /* a one byte temporary buffer */
62 static unsigned char outbfr;
63 /* counter of how many unused bits left in the byte buffer */
64 static int outcnt;
66 void Bitstream_Init(void *buffer)
68 byteptr = (unsigned char *)buffer;
69 bytecnt = 0;
70 outbfr = 0;
71 outcnt = 8;
74 void Bitstream_PutBits(int n, unsigned int val)
76 int diff;
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 */
82 n = diff;
83 val &= mask[n];
85 *(byteptr ++) = outbfr;
86 bytecnt++;
87 outbfr = 0;
88 outcnt = 8;
91 if (n > 0) { /* input is short enough to fit in what is left in the buffer */
92 outbfr |= (unsigned char)(val << (-diff));
93 outcnt -= n;
98 int Bitstream_Close()
100 while (outcnt != 8) Bitstream_PutBits(1, 1);
101 return bytecnt;
105 int Bitstream_NextStartCode()
107 int count = outcnt;
109 Bitstream_PutBits(1,0);
110 while (outcnt != 8) Bitstream_PutBits(1, 1);
112 return (count);
116 int Bitstream_GetLength()
118 return bytecnt;