⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bitstream.c

📁 用MPEG-4对YUV视频文件编码压缩成divx视频文件
💻 C
字号:
 /******************************************************************************
  *                                                                            *
  *  This file is part of XviD, a free MPEG-4 video encoder/decoder            *
  *                                                                            *
  *  XviD is an implementation of a part of one or more MPEG-4 Video tools     *
  *  as specified in ISO/IEC 14496-2 standard.  Those intending to use this    *
  *  software module in hardware or software products are advised that its     *
  *  use may infringe existing patents or copyrights, and any such use         *
  *  would be at such party's own risk.  The original developer of this        *
  *  software module and his/her company, and subsequent editors and their     *
  *  companies, will have no liability for use of this software or             *
  *  modifications or derivatives thereof.                                     *
  *                                                                            *
  *  XviD is free software; you can redistribute it and/or modify it           *
  *  under the terms of the GNU General Public License as published by         *
  *  the Free Software Foundation; either version 2 of the License, or         *
  *  (at your option) any later version.                                       *
  *                                                                            *
  *  XviD is distributed in the hope that it will be useful, but               *
  *  WITHOUT ANY WARRANTY; without even the implied warranty of                *
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
  *  GNU General Public License for more details.                              *
  *                                                                            *
  *  You should have received a copy of the GNU General Public License         *
  *  along with this program; if not, write to the Free Software               *
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  *
  *                                                                            *
  ******************************************************************************/

 /******************************************************************************
  *                                                                            *
  *  bitstream.c                                                               *
  *                                                                            *
  *  Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au>                    *
  *                                                                            *
  *  For more information visit the XviD homepage: http://www.xvid.org         *
  *                                                                            *
  ******************************************************************************/

 /******************************************************************************
  *                                                                            *
  *  Revision history:                                                         *
  *                                                                            *
  *  11.07.2002 add VOP width & height return to dec when dec->width           *
  *             or dec->height is 0  (for use in examples/ex1.c)               *
  *             MinChen <chenm001@163.com>                                     *
  *  22.05.2002 bs_put_matrix fix                                              *
  *  20.05.2002 added BitstreamWriteUserData                                   *
  *  19.06.2002  Fix a little bug in use custom quant matrix                   *
  *              MinChen <chenm001@163.com>                                    *
  *  08.05.2002  add low_delay support for B_VOP decode                        *
  *              MinChen <chenm001@163.com>                                    *
  *  06.05.2002 low_delay                                                      *
  *  06.05.2002 fixed fincr/fbase error                                        *
  *  01.05.2002 added BVOP support to BitstreamWriteVopHeader                  *
  *  15.04.2002 rewrite log2bin use asm386  By MinChen <chenm001@163.com>      *
  *  26.03.2002 interlacing support                                            *
  *  03.03.2002 qmatrix writing                                                *
  *  03.03.2002 merged BITREADER and BITWRITER                                 *
  *      30.02.2002     intra_dc_threshold support                             *
  *      04.12.2001     support for additional headers                         *
  *      16.12.2001     inital version                                         *
  *
  ******************************************************************************/


#include "bitstream.h"
#include "zigzag.h"
#include "../quant/quant_matrix.h"


/*!
 ************************************************************************
 * \brief
 *    write custom quant matrix into bitstream
 *    zigzag scan the quant matrix
 *	  
 ************************************************************************
 */
static void
bs_put_matrix(Bitstream * bs,
			  const int16_t * matrix)
{
	int i, j;
	const int last = matrix[scan_tables[0][63]];

	for (j = 63; j > 0 && matrix[scan_tables[0][j - 1]] == last; j--);

	for (i = 0; i <= j; i++) {
		BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8);
	}

	if (j < 63) {
		BitstreamPutBits(bs, 0, 8);
	}
}

/*!
 ************************************************************************
 * \brief
 *    write vol header
 *	  
 ************************************************************************
 */
void
BitstreamWriteVolHeader(Bitstream * const bs,
						const MBParam * pParam,
						const FRAMEINFO * frame)
{
	/* video object_start_code & vo_id    */
	BitstreamPad(bs);
	BitstreamPutBits(bs, VO_START_CODE, 27);
	BitstreamPutBits(bs, 0, 5);

	/* video_object_layer_start_code & vol_id*/
	BitstreamPutBits(bs, VOL_START_CODE, 28);
	BitstreamPutBits(bs, 0, 4);

	BitstreamPutBit(bs, 0);		/* random_accessible_vol*/
	BitstreamPutBits(bs, 0, 8);	/* video_object_type_indication*/
	BitstreamPutBit(bs, 0);		/* is_object_layer_identified (0=not given)*/
	BitstreamPutBits(bs, 1, 4);	/* aspect_ratio_info (1=1:1)*/

	BitstreamPutBits(bs, 0, 1);	/* vol_control_parameters (0=not given)*/

	BitstreamPutBits(bs, 0, 2);	/* video_object_layer_shape (0=rectangular)*/

	WRITE_MARKER();

	BitstreamPutBits(bs, 2, 16);

	WRITE_MARKER();

	BitstreamPutBit(bs, 0);		/* fixed_vop_rate = 0*/


	WRITE_MARKER();
	BitstreamPutBits(bs, pParam->width, 13);	/* width*/
	WRITE_MARKER();
	BitstreamPutBits(bs, pParam->height, 13);	/* height*/
	WRITE_MARKER();

/*	BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING);*/	/* interlace */
    BitstreamPutBit(bs,0);      /* disable interlace */
	BitstreamPutBit(bs, 1);		/* obmc_disable (overlapped block motion compensation)*/
	BitstreamPutBit(bs, 0);		/* sprite_enable  */
	BitstreamPutBit(bs, 0);		/* not_in_bit  */

	/* quant_type   0=h.263  1=mpeg4(quantizer tables)*/
	BitstreamPutBit(bs, pParam->m_quant_type);

	if (pParam->m_quant_type) {
		BitstreamPutBit(bs, get_intra_matrix_status());	/* load_intra_quant_mat*/
		if (get_intra_matrix_status()) {
			bs_put_matrix(bs, get_intra_matrix());
		}

		BitstreamPutBit(bs, get_inter_matrix_status());	/* load_inter_quant_mat*/
		if (get_inter_matrix_status()) {
			bs_put_matrix(bs, get_inter_matrix());
		}

	}

   BitstreamPutBits(bs,12,4);

}

/*!
 ************************************************************************
 * \brief
 *    write vop header
 *	  
 ************************************************************************
 */
/*
  NOTE: doesnt handle bother with time_base & time_inc
  time_base = n seconds since last resync (eg. last iframe)
  time_inc = nth of a second since last resync
  (decoder uses these values to determine precise time since last resync)
*/
void
BitstreamWriteVopHeader(Bitstream * const bs,
						const MBParam * pParam,
						const FRAMEINFO * frame,
						int vop_coded             /* no use */
						)
{

	BitstreamPad(bs);
	BitstreamPutBits(bs, VOP_START_CODE, 32);

	BitstreamPutBits(bs, frame->coding_type, 2);

	/* time_base = 0  write n x PutBit(1), PutBit(0)*/

	BitstreamPutBits(bs, 0, 1);


	WRITE_MARKER();


	BitstreamPutBits(bs, 1, 1);


	WRITE_MARKER();
/* whq,can be deleted ,2002.12.21 */
	if (!vop_coded) {
		BitstreamPutBits(bs, 0, 1);
		return;
	}

	BitstreamPutBits(bs, 1, 1);	/* vop_coded    */

	if (frame->coding_type == P_VOP)
		BitstreamPutBits(bs, frame->rounding_type, 1);

	BitstreamPutBits(bs, 0, 3);	/* intra_dc_vlc_threshold*/
/* whq,can be deleted ,2002.12.21 */
/*	if (frame->global_flags & XVID_INTERLACING) {*/
/*		BitstreamPutBit(bs, 1);	*//* top field first  */
/*		BitstreamPutBit(bs, 0);*//* alternate vertical scan*/
/*	}*/

	BitstreamPutBits(bs, frame->quant, 5);	/* quantizer*/

	if (frame->coding_type != I_VOP)
		BitstreamPutBits(bs, frame->fcode, 3);	/* forward_fixed_code*/

	/* backward_fixed_code */
/*
	if (frame->coding_type == B_VOP)
		BitstreamPutBits(bs, frame->bcode, 3);	
*/
}

/*!
 ************************************************************************
 * \brief
 *    write user data
 *	  
 ************************************************************************
 */
/* whq,2002.12.21,this function is not called */
void 
BitstreamWriteUserData(Bitstream * const bs, 
						uint8_t * data, 
						const int length)
{
	int i;

	BitstreamPad(bs);
	BitstreamPutBits(bs, USERDATA_START_CODE, 32);

	for (i = 0; i < length; i++) {
		BitstreamPutBits(bs, data[i], 8);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -