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

📄 text_bits.c

📁 MPEG4视频编解码内有divx(编码)
💻 C
📖 第 1 页 / 共 2 页
字号:

							/* subtraction of Max Run, last = 0 */
							int run_minus_max;

							if (prev_level == 0)
							{
								fprintf (stdout, "ERROR(CodeCoeff-second esc): level is %d\n", prev_level);
								exit(-1);
							}

							if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
								run_minus_max = prev_run - (intra_max_run0[prev_level]+1);
							else
								run_minus_max = prev_run - (inter_max_run0[prev_level]+1);

												  /* boon 120697 */
							if (run_minus_max < 64)
							{
								/* Separate tables for Intra luminance blocks */
								if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
								{
									length = PutRunCoeff_Intra(run_minus_max, prev_level, 0, bitstream);
								}
								else
								{
									length = PutRunCoeff_Inter(run_minus_max, prev_level, 0, bitstream);
								}
							} else
							length = 0;
						}
						else length = 0;
					}

					/* Third escape mode. FLC */
					if (length == 0)
					{							  /* Escape coding */

						if (prev_s == 1)
						{
							/* Modified due to N2171 Cl. 2.2.14 MW 25-MAR-1998 */
							/* prev_level = (prev_level^0xff)+1; */
							prev_level = (prev_level^0xfff)+1;
						}
						BitstreamPutBits(bitstream, 3L, 7L);
												  /* boon */
						BitstreamPutBits(bitstream, 3L, 2L);

												  /* last */
						BitstreamPutBits(bitstream, 0L, 1L);
												  /* run */
						BitstreamPutBits(bitstream, (long)(prev_run), 6L);

						/* 11.08.98 Sven Brandau:  "insert marker_bit" due to N2339, Clause 2.1.21 */
						BitstreamPutBits(bitstream, MARKER_BIT, 1);

						/* Modified due to N2171 Cl. 2.2.14 MW 25-MAR-1998 */
						/* BitstreamPutBits(bitstream, (long)(prev_level), 8L); */
						/* bits += 24; */
												  /* level */
						BitstreamPutBits(bitstream, (long)(prev_level), 12L);

						/* 11.08.98 Sven Brandau:  "insert marker_bit" due to N2339, Clause 2.1.21 */
						BitstreamPutBits(bitstream, MARKER_BIT, 1);

						/*bits += 28;*/
						bits += 30;
					}
					else
					{
						BitstreamPutBits(bitstream, (long)(prev_s), 1L);
						bits += length + 1;
					}
				}

				prev_run = run; prev_s = s;
				prev_level = level; prev_ind = ind;

				run = first = 0;
			}
		}
	}

	/* Encode the last coeff */

	if (!first)
	{
		prev_ind = prev_ind | 1<<12;			  /* last coeff */

		if ((prev_run < 64) &&
			(((prev_level < 4) && (Mode != MODE_INTRA &&
			Mode != MODE_INTRA_Q))
			|| ((prev_level < 9) && ((Mode == MODE_INTRA) ||
			(Mode == MODE_INTRA_Q)))))
		{
			/* Separate tables for Intra luminance blocks */
			if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
			{
				length = PutCoeff_Intra(prev_run, prev_level, 1,
					bitstream);
			}
			else
			{
				length = PutCoeff_Inter(prev_run, prev_level, 1,
					bitstream);
			}
		}
		else
			length = 0;

		/* First escape mode. Level offset */
		if (length == 0)
		{
			if ( prev_run < 64 )
			{

				/* subtraction of Max level, last = 0 */
				int level_minus_max;

				if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
					level_minus_max = prev_level - intra_max_level[1][prev_run];
				else
					level_minus_max = prev_level - inter_max_level[1][prev_run];

				if (  ( (level_minus_max < 4) && (Mode != MODE_INTRA && Mode != MODE_INTRA_Q) ) ||
					( (level_minus_max < 9) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q) ) )
				{
					/* Separate tables for Intra luminance blocks */
					if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
					{
						length = PutLevelCoeff_Intra(prev_run, level_minus_max, 1, bitstream);
					}
					else
					{
						length = PutLevelCoeff_Inter(prev_run, level_minus_max, 1, bitstream);
					}
				} else
				length = 0;
			}
			else length = 0;
		}

		/* Second escape mode. Run offset */
		if (length == 0)
		{
			if (((prev_level < 4) && (Mode != MODE_INTRA && Mode != MODE_INTRA_Q))||
				((prev_level < 9) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)) )
			{

				/* subtraction of Max Run, last = 1 */
				int run_minus_max;

				if (prev_level == 0)
				{
					fprintf (stdout, "ERROR(CodeCoeff-second esc): level is %d\n", prev_level);
					exit(-1);
				}

				if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
					run_minus_max = prev_run - (intra_max_run1[prev_level]+1);
				else
					run_minus_max = prev_run - (inter_max_run1[prev_level]+1);

				if (run_minus_max < 64)			  /* boon 120697 */
				{
					/* Separate tables for Intra luminance blocks */
					if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
					{
						length = PutRunCoeff_Intra(run_minus_max, prev_level, 1, bitstream);
					}
					else
					{
						length = PutRunCoeff_Inter(run_minus_max, prev_level, 1, bitstream);
					}
				} else
				length = 0;
			}
			else length = 0;
		}

		/* Third escape mode. FLC */
		if (length == 0)
		{										  /* Escape coding */

			if (prev_s == 1)
			{
				/* Modified due to N2171 Cl. 2.2.14 MW 25-MAR-1998 */
				/* prev_level = (prev_level^0xff)+1; */
				prev_level = (prev_level^0xfff)+1;
			}
			BitstreamPutBits(bitstream, 3L, 7L);
			BitstreamPutBits(bitstream, 3L, 2L);  /* boon */

			BitstreamPutBits(bitstream, 1L, 1L);  /* last */
			BitstreamPutBits(bitstream, (long)(prev_run), 6L);

			/* 11.08.98 Sven Brandau:  "insert marker_bit" due to N2339, Clause 2.1.21 */
			BitstreamPutBits(bitstream, MARKER_BIT, 1);

			/* Modified due to N2171 Cl. 2.2.14 MW 25-MAR-1998 */
			/* BitstreamPutBits(bitstream, (long)(prev_level), 8L); */
			/* bits += 24; */
												  /* level */
			BitstreamPutBits(bitstream, (long)(prev_level), 12L);

			/* 11.08.98 Sven Brandau:  "insert marker_bit" due to N2339, Clause 2.1.21 */
			BitstreamPutBits(bitstream, MARKER_BIT, 1);

			/*bits += 28;*/
			bits += 30;

		}
		else
		{
			BitstreamPutBits(bitstream, (long)(prev_s), 1L);
			bits += length + 1;
		}
	}

	return bits;
}


/***********************************************************CommentBegin******
 *
 * -- CodeCoeff_RVLC -- RVLC encoding of quantized DCT coefficients
 *
 * Purpose :
 *	RVLC encoding of quantized DCT coefficients, except for
 *      INTRADC in case of Intra macroblocks
 *
 * Arguments in :
 *	Int Mode : encoding mode
 * 	Int *qcoeff: pointer to quantized dct coefficients
 * 	Int block : number of the block in the macroblock (0-5)
 * 	Int ncoeffs : the number of coefficients per block
 *
 * Arguments out :
 *	Image *bitstream : pointer to the output bitstream
 *
 * Return values :
 *	Int bits : number of bits added to the bitstream (?)
 *
 ***********************************************************CommentEnd********/

Int CodeCoeff_RVLC(Int j_start, Int Mode, Int qcoeff[], Int block, Int ncoeffs, Image *bitstream)
{
	Int j, bits;
	Int prev_run, run, prev_level, level, first;
	Int prev_ind, ind, prev_s, s, length;

	run = bits = 0;
	first = 1;
	prev_run = prev_level = prev_ind = level = s = prev_s = ind = 0;

	for (j = j_start; j< ncoeffs; j++)
	{
		/* The INTRADC encoding for INTRA Macroblocks is done in
		   Bits_CountCoeff */

		{
			/* do not enter this part for INTRADC coding for INTRA macro-
			   blocks */

			/* encode AC coeff */

			s = 0;

			/* Increment run if coeff is zero */

			if ((level = qcoeff[j]) == 0)
			{
				run++;
			}
			else
			{
				/* code run & level and count bits */

				if (level < 0)
				{
					s = 1;
					level = -level;
				}

				ind = level | run<<4;
				ind = ind | 0<<12;				  /* Not last coeff */
				if (!first)
				{
					/* Encode the previous ind */

					if (prev_level  < 28 && prev_run < 39)
						/* Separate tables for Intra luminance blocks */
						if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
					{
						length = PutCoeff_Intra_RVLC(prev_run, prev_level, 0,
							bitstream);
					}
					else
					{
						length = PutCoeff_Inter_RVLC(prev_run, prev_level, 0,
							bitstream);
					}
					else
						length = 0;

					if (length == 0)
					{							  /* Escape coding */

												  /* ESCAPE */
						BitstreamPutBits(bitstream, 1L, 5L);

												  /* LAST   */
						BitstreamPutBits(bitstream, 0L, 1L);

						BitstreamPutBits(bitstream,
							(long)(prev_run), 6L);/* RUN 	  */

						/* 11.08.98 Sven Brandau:  "changed length for LEVEL (11 bit)" due to N2339, Clause 2.1.21 */
						/* 11.08.98 Sven Brandau:  "insert marker_bit befor and after LEVEL" due to N2339, Clause 2.1.21 */
						/* BitstreamPutBits(bitstream,
								 (long)(prev_level), 7L);*/
						/* LEVEL */
						BitstreamPutBits( bitstream, MARKER_BIT, 1 );
												  /* LEVEL */
						BitstreamPutBits( bitstream, (long)(prev_level), 11L);
						BitstreamPutBits( bitstream, MARKER_BIT, 1 );

												  /* ESCAPE */
						BitstreamPutBits(bitstream, 0L, 4L);

												  /* ESCAPE's */
						BitstreamPutBits(bitstream,
							(long)(prev_s),1L);

						bits += 5 + 1 + 6 + 1 + 11 + 1 + 4 + 1;
						/* bits += 24; */
					}
					else
					{
						BitstreamPutBits(bitstream,
							(long)(prev_s), 1L);
						bits += length + 1;
					}
				}

				prev_run = run; prev_s = s;
				prev_level = level; prev_ind = ind;

				run = first = 0;
			}
		}
	}

	/* Encode the last coeff */

	if (!first)
	{
		prev_ind = prev_ind | 1<<12;			  /* last coeff */

		if (prev_level  < 5 && prev_run < 45)
			/* Separate tables for Intra luminance blocks */
			if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
		{
			length = PutCoeff_Intra_RVLC(prev_run, prev_level, 1,
				bitstream);
		}
		else
		{
			length = PutCoeff_Inter_RVLC(prev_run, prev_level, 1,
				bitstream);
		}
		else
			length = 0;

		if (length == 0)
		{										  /* Escape coding */

			BitstreamPutBits(bitstream, 1L, 5L);  /* ESCAPE	*/

			BitstreamPutBits(bitstream, 1L, 1L);  /* LAST		*/

												  /* RUN		*/
			BitstreamPutBits(bitstream, (long)(prev_run), 6L);

			/* 11.08.98 Sven Brandau:  "changed length for LEVEL (11 bit)" due to N2339, Clause 2.1.21	     */
			/* 11.08.98 Sven Brandau:  "insert marker_bit befor and after LEVEL" due to N2339, Clause 2.1.21 */
			/* BitstreamPutBits(bitstream, (long)(prev_level), 7L);*/
			BitstreamPutBits( bitstream, MARKER_BIT, 1 );
												  /* LEVEL 	*/
			BitstreamPutBits( bitstream, (long)(prev_level), 11L);
			BitstreamPutBits( bitstream, MARKER_BIT, 1 );

			BitstreamPutBits(bitstream, 0L, 4L);  /* ESCAPE 	*/

												  /* ESCAPE's 	*/
			BitstreamPutBits(bitstream, (long)(prev_s), 1L);

			bits += 24;

		}
		else
		{
			BitstreamPutBits(bitstream, (long)(prev_s), 1L);
			bits += length + 1;
		}
	}

	return bits;
}


/***********************************************************CommentBegin******
 *
 * -- Bits_Reset -- To reset the structure bits
 *
 * Purpose :
 *	To reset the structure bits, used for counting the number
 * 	of texture bits
 *
 * Arguments in :
 *	Bits* bits : a pointer to the struct Bits
 *
 ***********************************************************CommentEnd********/

void
Bits_Reset (Bits *bits)
{
	memset(bits, 0, sizeof(Bits));
}

⌨️ 快捷键说明

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