1
/* putbits.c, bit-level output */
3
/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
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
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
29
/* Modifications and enhancements (C) 2000/2001 Andrew Stevens */
31
/* These modifications are free software; you can redistribute it
32
* and/or modify it under the terms of the GNU General Public License
33
* as published by the Free Software Foundation; either version 2 of
34
* the License, or (at your option) any later version.
36
* This program is distributed in the hope that it will be useful,
37
* but WITHOUT ANY WARRANTY; without even the implied warranty of
38
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39
* General Public License for more details.
41
* You should have received a copy of the GNU General Public License
42
* along with this program; if not, write to the Free Software
43
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
53
extern void ad_putchar(unsigned int outbfr);
55
static unsigned int outbfr;
58
static int64_t bytecnt;
60
/* initialize buffer, call once before first putbits or alignbits */
65
bytecnt = BITCOUNT_OFFSET/8LL;
69
/* write rightmost n (0<=n<=32) bits of val to outfile */
70
void putbits(uint32_t val, int n)
72
val = (n == 32) ? val : (val & (~(0xffffffffU << n)));
75
outbfr = (outbfr << outcnt ) | (val >> (n-outcnt));
83
outbfr = (outbfr<<n) | val;
88
/* zero bit stuffing to next byte boundary (5.2.3, 6.2.1) */
95
/* return total number of generated bits */
96
int64_t bitcount(void)
98
return 8LL*bytecnt + (8-outcnt);