Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Tools

MPEG-2 and Video Compression


Motion Estimation and Compensation. Motion estimation by the encoder is very computationally intensive, since it generally requires repeated evaluation of the effectiveness of candidate motion compensation vectors. However the possible motion vectors are chosen, using a fast evaluation function speeds up the algorithm. The Intel IPP functions ippiSAD16x16, ippiSqrDiff16x16, and ippiSqrDiff16x16 compare blocks from one frame against motion-compensated blocks in a reference frame. ippiSAD calculates the sum of absolute differences between the pixels, while ippiSqrDiff calculates the sum of squared differences. The Intel IPP sample uses the former.

Color Conversion. The standard Intel IPP color conversion functions include conversions to and from YCbCr 4:2:2, 4:2:0, and 4:4:4. Because they are in the general color conversion set, these functions are called RGBToYUV422 / YUV422ToRGB, RGBToYUV420 / YUV420ToRGB, and RGBToYUV / YUVToRGB. These functions support interleaved and planar YCbCr data. Listing 5 shows a conversion of decoded MPEG-2 pixels into RGB for display.

src[0] = frame->Y_comp_data +
		pContext->Video[0].frame_buffer.video_memory_offset;
	src[1] = frame->V_comp_data +
		pContext-Video[0].frame_buffer.video_memory_offset/4;
	src[2] = frame->U_comp_data + 
		pContext->Video[0].frame_buffer.video_memory_offset/4;
	srcStep[0] = frame->Y_comp_pitch;
	srcStep[1] = pitch_UV;
	srcStep[2] = pitch_UV;
	ippiYUV420ToRGB_8u_P3AC4R(src, srcStep, video_memory +
		pContext->Video[0].frame_buffer.video_memory_offset/4, 
		roi.width<<2, roi);

Listing 5: Converting YCbCr 4:2:0 to RGB for Display

Once the encoder has finished searching the space of possible motion vectors, it can use the many ippiGetDiffS functions to find the difference between the current frame and the reference frame after motion compensation.

Both the encoder and decoder need a motion compensation algorithm. Intel IPP-based algorithms can use ippiMC or ippiAdd to combine the reference frame with the decoded difference information. Listing 4 shows such an algorithm for a macroblock from a 4:2:0 B-frame.

// Determine whether shift is half or full pel
	//  in horizontal and vertical directions
	// Motion vectors are in half-pels in bitstream
	// The bit code generated is:
	// FF = 0000b; FH = 0100b; HF = 1000b; HH = 1100b
	flag1 = pContext->macroblock.prediction_type |
		((pContext->macroblock.vector[0]  & 1) << 3) |
		((pContext->macroblock.vector[1]  & 1) << 2);
	flag2 = pContext->macroblock.prediction_type|
		((pContext->macroblock.vector[0]  & 2) << 2) |
		((pContext->macroblock.vector[1]  & 2) << 1);
	flag3 = pContext->macroblock.prediction_type|
		((pContext->macroblock.vector[2]  & 1) << 3) |
		((pContext->macroblock.vector[3]  & 1) << 2);
	flag4 = pContext->macroblock.prediction_type|
		((pContext->macroblock.vector[2]  & 2) << 2) |
		((pContext->macroblock.vector[3]  & 2) << 1);

	// Convert motion vectors from half-pels to full-pel
	// also convert for chroma subsampling
	// down, previous frame
	vector_luma[1] = pContext->macroblock.vector[1] >>1;
	vector_chroma[1] = pContext->macroblock.vector[1] >>2;

	// right, previous frame
	vector_luma[0] = pContext->macroblock.vector[0] >> 1;
	vector_chroma[0] = pContext->macroblock.vector[0] >> 2;

	// down, subsequent frame
	vector_luma[3] = pContext->macroblock.vector[3] >> 1;
	vector_chroma[3] = pContext->macroblock.vector[3] >> 2;

	// right, subsequent frame
	vector_luma[2] = pContext->macroblock.vector[2] >> 1;
	vector_chroma[2] = pContext->macroblock.vector[2] >> 2;

	offs1 = 
		(pContext->macroblock.motion_vertical_field_select[0] +
			vector_luma[1] + pContext->row_l) * pitch_y +
			vector_luma[0] + pContext->col_l,
 
	offs2 =
		(pContext->macroblock.motion_vertical_field_select[1] +
			vector_luma[3] + pContext->row_l) * pitch_y +
			vector_luma[2] + pContext->col_l,

	i = ippiMC16x16B_8u_C1(
		ref_Y_data1	+ offs1, ptc_y, flag1,
		ref_Y_data2 + offs2, ptc_y, flag3,
		pContext->block.idct, 32,
		frame->Y_comp_data + pContext->offset_l,
		ptc_y, 0);
	assert(i == ippStsOk);

	offs1 =
		(pContext->macroblock.motion_vertical_field_select[0] + 
			vector_chroma[1] + pContext->row_c		)* 			pitch_uv +
			vector_chroma[0] + pContext->col_c;

	offs2 =
		(pContext->macroblock.motion_vertical_field_select[1] +
			vector_chroma[3] + pContext->row_c		)* 			pitch_uv +
			vector_chroma[2] + pContext->col_c;

	i = ippiMC8x8B_8u_C1(
		ref_U_data1 + offs1, ptc_uv, flag2,
		ref_U_data2 + offs2, ptc_uv, flag4,
		pContext->block.idct+256,16,
		frame->U_comp_data + pContext->offset_c,
		ptc_uv, 0);
	assert(i == ippStsOk);
	i = ippiMC8x8B_8u_C1(
		ref_V_data1 + offs1, ptc_uv,flag2,
		ref_V_data2 + offs2, ptc_uv,flag4,
		pContext->block.idct+320,16,
		frame->V_comp_data + pContext->offset_c,
		ptc_uv, 0);
	assert(i == ippStsOk);
Listing 4: MPEG-2 Bidirectional Motion Compensation

The first step is to convert the motion vectors from half-pel accuracy to full-pel accuracy, because the half-pel information is passed into the ippiMC functions as a flag. The code drops the least-significant bit of each motion vector and uses it to generate this flag. The starting point of each reference block is then offset vertically and horizontally by the amount of the motion vector.

Because this code handles bi-directional prediction, the code repeats all these steps for two separate motion vectors and two separate reference frames. This is the last decoding step, so the code places the result directly in the YCbCr output frame.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.