mpeg software simulation group 比特写入文件程序阅读笔记

/* putbits.c, bit-level output */

/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */

/*
* Disclaimer of Warranty
*
* These software programs are available to the user without any license fee or
* royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
* any and all warranties, whether express, implied, or statuary, including any
* implied warranties or merchantability or of fitness for a particular
* purpose. In no event shall the copyright-holder be liable for any
* incidental, punitive, or consequential damages of any kind whatsoever
* arising from the use of these programs.
*
* This disclaimer of warranty extends to the user of these programs and user's
* customers, employees, agents, transferees, successors, and assigns.
*
* The MPEG Software Simulation Group does not represent or warrant that the
* programs furnished hereunder are free of infringement of any third-party
* patents.
*
* Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
* are subject to royalty fees to patent holders. Many of these patents are
* general enough such that they are unavoidable regardless of implementation
* design.
*
*/

#include <stdio.h>
#include "config.h"
#include "putbits.h"

extern FILE *outfile; /* the only global var we need here */

/* private data */
static unsigned char outbfr;
static int outcnt;
static int bytecnt;

/* initialize buffer, call once before first putbits or alignbits */

//在调用写入bits时候要调用的。

//outcnt is 8 means the data filed ofunsigned char is 8 or every time we will wite 8 bits

//bytecnt =0 ,这两个都是静态变量,整个程序执行期间的数据都有效。
void initbits()
{
outcnt = 8;
bytecnt = 0;
}

/* write rightmost n (0<=n<=32) bits of val to outfile */

//写入变量val 的最右面的n个bits
void putbits(int val,int n)
{
int i;
unsigned int mask;//临时变量,用于测试val从最右面起的第n为是否为1。

mask = 1 << (n-1); /* selects first (leftmost) bit *///准备要写入数据的第一位

for (i=0; i<n; i++)
{
outbfr <<= 1;

if (val & mask)
outbfr|= 1;

mask >>= 1; /* select next bit */
outcnt--;

if (outcnt==0) /* 8 bit buffer full */
{
putc(outbfr,outfile);
outcnt = 8;
bytecnt++;
}
}
}

/* zero bit stuffing to next byte boundary (5.2.3, 6.2.1) */
void alignbits()
{
if (outcnt!=8)
putbits(0,outcnt);
}

/* return total number of generated bits */
int bitcount()
{
return 8*bytecnt + (8-outcnt);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值