r581: Pierre's patch adding default interlacing to specific fileformats
[cinelerra_cv/mob.git] / mplexhi / bits.c
blob5048289516741ee9f78d35d2605e8166f8d605c4
1 /* putbits.c, bit-level output */
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
5 /*
6 * Disclaimer of Warranty
8 * These software programs are available to the user without any license fee or
9 * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
10 * any and all warranties, whether express, implied, or statuary, including any
11 * implied warranties or merchantability or of fitness for a particular
12 * purpose. In no event shall the copyright-holder be liable for any
13 * incidental, punitive, or consequential damages of any kind whatsoever
14 * arising from the use of these programs.
16 * This disclaimer of warranty extends to the user of these programs and user's
17 * customers, employees, agents, transferees, successors, and assigns.
19 * The MPEG Software Simulation Group does not represent or warrant that the
20 * programs furnished hereunder are free of infringement of any third-party
21 * patents.
23 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
24 * are subject to royalty fees to patent holders. Many of these patents are
25 * general enough such that they are unavoidable regardless of implementation
26 * design.
30 #include "main.h"
31 #include <stdlib.h>
32 #include <sys/param.h>
35 static int ubuffer[BUFFER_SIZE];
37 /* initialize buffer, call once before first putbits or alignbits */
38 int init_putbits(bitstream *bs, char *bs_filename)
40 if ((bs->bitfile = fopen(bs_filename, "wb")) == NULL)
42 fprintf(stderr, "Unable to open file %s for writing.\n", bs_filename);
43 return FALSE;
45 bs->bfr = (unsigned char*)malloc(BUFFER_SIZE);
46 if (!bs->bfr)
48 fclose(bs->bitfile);
49 fprintf(stderr, "Unable to allocate memory for bitstream file %s.", bs_filename);
50 return FALSE;
52 bs->bitidx = 8;
53 bs->byteidx = 0;
54 bs->totbits = 0.0;
55 bs->outbyte = 0;
56 bs->fileOutError = FALSE;
57 return TRUE;
60 void finish_putbits(bitstream *bs)
62 if (bs->bitfile)
64 if ((bs->byteidx) && (!bs->fileOutError))
65 fwrite(bs->bfr, sizeof(unsigned char), bs->byteidx, bs->bitfile);
66 fclose(bs->bitfile);
67 bs->bitfile = NULL;
69 if (bs->bfr)
71 free(bs->bfr);
72 bs->bfr = NULL;
76 static void putbyte(bitstream *bs)
78 if (!bs->fileOutError)
80 bs->bfr[bs->byteidx++] = bs->outbyte;
81 if (bs->byteidx == BUFFER_SIZE)
83 if (fwrite(bs->bfr, sizeof(unsigned char), BUFFER_SIZE, bs->bitfile) != BUFFER_SIZE)
84 bs->fileOutError = TRUE;
85 bs->byteidx = 0;
88 bs->bitidx = 8;
91 /* write rightmost n (0<=n<=32) bits of val to outfile */
92 void putbits(bitstream *bs, int val, int n)
94 int i;
95 unsigned int mask;
97 mask = 1 << (n - 1); /* selects first (leftmost) bit */
98 for (i = 0; i < n; i++)
100 bs->totbits += 1;
101 bs->outbyte <<= 1;
102 if (val & mask)
103 bs->outbyte |= 1;
104 mask >>= 1; /* select next bit */
105 bs->bitidx--;
106 if (bs->bitidx == 0) /* 8 bit buffer full */
107 putbyte(bs);
111 /* write rightmost bit of val to outfile */
112 void put1bit(bitstream *bs, int val)
114 bs->totbits += 1;
115 bs->outbyte <<= 1;
116 if (val & 0x1)
117 bs->outbyte |= 1;
118 bs->bitidx--;
119 if (bs->bitidx == 0) /* 8 bit buffer full */
120 putbyte(bs);
123 /* Prepare a upcoming undo at the beginning of a GOP */
124 void prepareundo(bitstream *bs, bitstream *undo)
126 memcpy(ubuffer, bs->bfr, BUFFER_SIZE);
127 fgetpos(bs->bitfile, &bs->actpos);
128 *undo = *bs;
131 /* Reset old status, undo all changes made up to a point */
132 void undochanges(bitstream *bs, bitstream *old)
134 memcpy(bs->bfr, ubuffer, BUFFER_SIZE);
135 fsetpos(bs->bitfile, &bs->actpos);
136 *bs = *old;
139 /* zero bit stuffing to next byte boundary (5.2.3, 6.2.1) */
140 void alignbits(bitstream *bs)
142 if (bs->bitidx != 8)
143 putbits(bs, 0, bs->bitidx);
146 /* return total number of generated bits */
147 bitcount_t bitcount(bitstream *bs)
149 return bs->totbits;
152 static int refill_buffer(bitstream *bs)
154 int i;
156 i = fread(bs->bfr, sizeof(unsigned char), BUFFER_SIZE, bs->bitfile);
157 if (!i)
159 bs->eobs = TRUE;
160 return FALSE;
162 bs->bufcount = i;
163 return TRUE;
166 /* open the device to read the bit stream from it */
167 int init_getbits(bitstream *bs, char *bs_filename)
170 if ((bs->bitfile = fopen(bs_filename, "rb")) == NULL)
172 fprintf(stderr, "Unable to open file %s for reading.\n", bs_filename);
173 return FALSE;
175 bs->bfr = (unsigned char*)malloc(BUFFER_SIZE);
176 if (!bs->bfr)
178 fclose(bs->bitfile);
179 fprintf(stderr, "Unable to allocate memory for bitstream file %s.\n", bs_filename);
180 return FALSE;
182 bs->byteidx = 0;
183 bs->bitidx = 8;
184 bs->totbits = 0.0;
185 bs->bufcount = 0;
186 bs->eobs = FALSE;
187 if (!refill_buffer(bs))
189 if (bs->eobs)
191 fprintf(stderr, "Unable to read from file %s.\n", bs_filename);
192 return FALSE;
195 return TRUE;
198 /*close the device containing the bit stream after a read process*/
199 void finish_getbits(bitstream *bs)
201 if (bs->bitfile)
202 fclose(bs->bitfile);
203 bs->bitfile = NULL;
206 int masks[8]={0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};
208 /*read 1 bit from the bit stream */
209 unsigned int get1bit(bitstream *bs)
211 unsigned int bit;
213 if (bs->eobs)
214 return FALSE;
216 bit = (bs->bfr[bs->byteidx] & masks[bs->bitidx - 1]) >> (bs->bitidx - 1);
217 bs->totbits++;
218 bs->bitidx--;
219 if (!bs->bitidx)
221 bs->bitidx = 8;
222 bs->byteidx++;
223 if (bs->byteidx == bs->bufcount)
225 if (bs->bufcount == BUFFER_SIZE)
226 refill_buffer(bs);
227 else
228 bs->eobs = TRUE;
229 bs->byteidx = 0;
233 return bit;
236 /*read N bits from the bit stream */
237 unsigned int getbits(bitstream *bs, int N)
239 unsigned int val = 0;
240 int i = N;
241 unsigned int j;
243 // Optimize: we are on byte boundary and want to read multiple of bytes!
244 if ((bs->bitidx == 8) && ((N & 7) == 0))
246 i = N >> 3;
247 while (i > 0)
249 if (bs->eobs)
250 return 0;
251 val = (val << 8) | bs->bfr[bs->byteidx];
252 bs->byteidx++;
253 bs->totbits += 8;
254 if (bs->byteidx == bs->bufcount)
256 if (bs->bufcount == BUFFER_SIZE)
257 refill_buffer(bs);
258 else
259 bs->eobs = TRUE;
260 bs->byteidx = 0;
262 i--;
265 else
267 while (i > 0)
269 if (bs->eobs)
270 return FALSE;
272 j = (bs->bfr[bs->byteidx] & masks[bs->bitidx - 1]) >> (bs->bitidx - 1);
273 bs->totbits++;
274 bs->bitidx--;
275 if (!bs->bitidx)
277 bs->bitidx = 8;
278 bs->byteidx++;
279 if (bs->byteidx == bs->bufcount)
281 if (bs->bufcount == BUFFER_SIZE)
282 refill_buffer(bs);
283 else
284 bs->eobs = TRUE;
285 bs->byteidx = 0;
288 val = (val << 1) | j;
289 i--;
292 return val;
295 /*return the status of the bit stream*/
296 /* returns 1 if end of bit stream was reached */
297 /* returns 0 if end of bit stream was not reached */
299 int end_bs(bitstream *bs)
301 return bs->eobs;
304 /*this function seeks for a byte aligned sync word (max 32 bits) in the bit stream and
305 places the bit stream pointer right after the sync.
306 This function returns 1 if the sync was found otherwise it returns 0 */
308 int seek_sync(bitstream *bs, unsigned int sync, int N)
310 unsigned int val, val1;
311 unsigned int maxi = ((1U<<N)-1); /* pow(2.0, (double)N) - 1 */;
313 while (bs->bitidx != 8)
315 get1bit(bs);
318 val = getbits(bs, N);
319 if( bs->eobs )
320 return FALSE;
322 while ((val & maxi) != sync)
324 val <<= 8;
325 val1 = getbits( bs, 8 );
326 val |= val1;
327 if( bs->eobs )
328 return FALSE;
331 return TRUE;