slice.c 15.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 *  slice.c
 *
 *  Copyright (C) Aaron Holtzman <aholtzma@ess.engr.uvic.ca> - Nov 1999
 *
 *  Decodes an MPEG-2 video stream.
 *
 *  This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
 *	
 *  mpeg2dec 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, or (at your option)
 *  any later version.
 *   
 *  mpeg2dec 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 GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 
 *
 */
 
#include <stdio.h>
#include <stdlib.h>
28
#include <string.h>
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

#include "config.h"
#include "mpeg2.h"
#include "mpeg2_internal.h"

#include "motion_comp.h"
#include "bitstream.h"
#include "idct.h"
#include "stats.h"


//storage for dct coded blocks plus one row and column of overshoot
static sint_16 y_blocks[4 * 64 ] ALIGN_16_BYTE;
static sint_16 cr_blocks[64] ALIGN_16_BYTE;
static sint_16 cb_blocks[64] ALIGN_16_BYTE;

//XXX put these on the stack in slice_process?
static slice_t slice;
static macroblock_t mb;


//XXX move these data structures into slice.c
typedef struct 
{
  char run, level, len;
} DCTtab;

extern DCTtab DCTtabfirst[],DCTtabnext[],DCTtab0[],DCTtab1[];
extern DCTtab DCTtab2[],DCTtab3[],DCTtab4[],DCTtab5[],DCTtab6[];
extern DCTtab DCTtab0a[],DCTtab1a[];


uint_32 non_linear_quantizer_scale[32] =
{
63 64 65 66
	 0, 1, 2, 3, 4, 5, 6, 7,
	 8,10,12,14,16,18,20,22,
	24,28,32,36,40,44,48,52,
	56,64,72,80,88,96,104,112
67 68 69 70 71 72 73 74 75
};


void
slice_get_slice_header(const picture_t *picture, slice_t *slice)
{

	uint_32 intra_slice_flag;

76
	slice->quantizer_scale_code = bitstream_get(5);
77 78

	if (picture->q_scale_type)
79
	slice->quantizer_scale = non_linear_quantizer_scale[slice->quantizer_scale_code];
80
	else
81
	slice->quantizer_scale = slice->quantizer_scale_code << 1 ;
82 83
		

84 85
	if ((intra_slice_flag = bitstream_get(1)))
	{
86
		//Ignore the value of intra_slice
87
		bitstream_get(1);
88

89
		slice->slice_picture_id_enable = bitstream_get(1);
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
		slice->slice_picture_id = bitstream_get(6);

		//Ignore all the extra_data
		while(bitstream_get(1))
			bitstream_flush(8);
	}

	//reset intra dc predictor
	slice->dc_dct_pred[0]=slice->dc_dct_pred[1]=slice->dc_dct_pred[2]= 
			1<<(picture->intra_dc_precision + 7) ;

	stats_slice_header(slice);
}


//This goes into vlc.c when it gets written
inline uint_32
slice_get_block_coeff(uint_16 *run, sint_16 *val, uint_16 non_intra_dc,uint_16 intra_vlc_format)
{
109 110
	uint_32 code;
	DCTtab *tab;
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

	//this routines handles intra AC and non-intra AC/DC coefficients
	code = bitstream_show(16);
 
	//FIXME use a pointer to the right vlc format table based on
	//intra_vlc_format
	if (code>=16384 && !intra_vlc_format)
	{
		if (non_intra_dc)
			tab = &DCTtabfirst[(code>>12)-4];
		else
			tab = &DCTtabnext[(code>>12)-4];
	}
	else if (code>=1024)
	{
		if (intra_vlc_format)
			tab = &DCTtab0a[(code>>8)-4];
		else
			tab = &DCTtab0[(code>>8)-4];
	}
	else if (code>=512)
	{
		if (intra_vlc_format)
			tab = &DCTtab1a[(code>>6)-8];
		else
			tab = &DCTtab1[(code>>6)-8];
	}
	else if (code>=256)
		tab = &DCTtab2[(code>>4)-16];
	else if (code>=128)
		tab = &DCTtab3[(code>>3)-16];
	else if (code>=64)
		tab = &DCTtab4[(code>>2)-16];
	else if (code>=32)
		tab = &DCTtab5[(code>>1)-16];
	else if (code>=16)
		tab = &DCTtab6[code-16];
	else
	{
		fprintf(stderr,"(vlc) invalid huffman code 0x%x in vlc_get_block_coeff()\n",code);
		exit(1);
		return 0;
	}

	bitstream_flush(tab->len);

	if (tab->run==64) // end_of_block 
		return 0;

	if (tab->run==65) /* escape */
	{
		*run = bitstream_get(6);

		*val = bitstream_get(12);

		if(*val >= 2048)
			*val =  *val - 4096;
	}
	else
	{
		*run = tab->run;
		*val = tab->level;
		 
		if(bitstream_get(1)) //sign bit
			*val = -*val;
	}
	return 1;
}


static void
slice_get_intra_block(const picture_t *picture,slice_t *slice,sint_16 *dest,uint_32 cc)
{
	uint_32 i = 1;
	uint_32 j;
	uint_16 run;
	sint_16 val;
	const uint_8 *scan = picture->scan;
	uint_8 *quant_matrix = picture->intra_quantizer_matrix;
	sint_16 quantizer_scale = slice->quantizer_scale;

	//Get the intra DC coefficient and inverse quantize it
193 194 195 196
	if (cc == 0)
		dest[0] = (slice->dc_dct_pred[0] += Get_Luma_DC_dct_diff()) << (3 - picture->intra_dc_precision);
	else 
		dest[0] = (slice->dc_dct_pred[cc]+= Get_Chroma_DC_dct_diff()) << (3 - picture->intra_dc_precision);
197 198 199 200 201 202 203

	dest[0] <<= 4;

	i = 1;
	while((slice_get_block_coeff(&run,&val,0,picture->intra_vlc_format)))
	{
		i += run;
204 205
		j = scan[i++];
		dest[j] = (val * quantizer_scale * quant_matrix[j]);
206 207 208
		//FIXME put mismatch control back in
	}
}
209

210 211 212 213 214 215 216 217 218 219 220 221 222 223
static void
slice_get_non_intra_block(const picture_t *picture,slice_t *slice,sint_16 *dest,uint_32 cc)
{
	uint_32 i = 0;
	uint_32 j;
	uint_16 run;
	sint_16 val;
	const uint_8 *scan = picture->scan;
	uint_8 *quant_matrix = picture->non_intra_quantizer_matrix;
	sint_16 quantizer_scale = slice->quantizer_scale;

	while((slice_get_block_coeff(&run,&val,i==0,0)))
	{
		i += run;
224 225
		j = scan[i++];
		dest[j] = (((val<<1) + (val>>15))* quantizer_scale * quant_matrix[j]) >> 1;
226 227 228 229 230 231 232
	}
}

//This should inline easily into slice_get_motion_vector
static inline sint_16 compute_motion_vector(sint_16 vec,uint_16 r_size,sint_16 motion_code,
		sint_16 motion_residual)
{
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
	sint_16 lim;

	lim = 16<<r_size;

	if (motion_code>0)
	{
		vec+= ((motion_code-1)<<r_size) + motion_residual + 1;
		if (vec>=lim)
			vec-= lim + lim;
	}
	else if (motion_code<0) 
	{
		vec-= ((-motion_code-1)<<r_size) + motion_residual + 1;
		if (vec<-lim)
			vec+= lim + lim;
	}
249 250 251 252 253 254
	return vec;
}

static void slice_get_motion_vector(sint_16 *prev_mv, sint_16 *curr_mv,const uint_8 *f_code,
		macroblock_t *mb)
{
255 256
	sint_16 motion_code, motion_residual;
	sint_16 r_size;
257 258 259

	//fprintf(stderr,"motion_vec: h_r_size %d v_r_size %d\n",f_code[0],f_code[1]);

260
	// horizontal component
261 262 263 264 265 266
	r_size = f_code[0];
	motion_code = Get_motion_code();
	motion_residual =  0;
	if (r_size!=0 && motion_code!=0) 
		motion_residual = bitstream_get(r_size);

267
	curr_mv[0] = compute_motion_vector(prev_mv[0],r_size,motion_code,motion_residual);
268 269 270
	prev_mv[0] = curr_mv[0];

	//XXX dmvectors are unsed right now...
271 272
	if (mb->dmv)
		mb->dmvector[0] = Get_dmvector();
273

274
	// vertical component 
275
	r_size = f_code[1];
276
	motion_code     = Get_motion_code();
277 278 279 280
	motion_residual =  0;
	if (r_size!=0 && motion_code!=0) 
		motion_residual =  bitstream_get(r_size);

281 282 283 284
	if (mb->mvscale)
		prev_mv[1] >>= 1; 

	curr_mv[1] = compute_motion_vector(prev_mv[1],r_size,motion_code,motion_residual);
285

286 287
	if (mb->mvscale)
		curr_mv[1] <<= 1;
288

289
	prev_mv[1] = curr_mv[1];
290 291

	//XXX dmvectors are unsed right now...
292 293
	if (mb->dmv)
		mb->dmvector[1] = Get_dmvector();
294 295 296 297 298 299 300 301
}

//These next two functions are very similar except that they
//don't have to switch between forward and backward data structures.
//The jury is still out on whether is was worth it.
static void slice_get_forward_motion_vectors(const picture_t *picture,slice_t *slice, 
		macroblock_t *mb)
{
302 303 304 305 306 307
	if (mb->motion_vector_count==1)
	{
		if (mb->mv_format==MV_FIELD && !mb->dmv)
		{
			fprintf(stderr,"field based mv\n");
			mb->f_motion_vertical_field_select[1] = 
308
				mb->f_motion_vertical_field_select[0] = bitstream_get(1);
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
		}

		slice_get_motion_vector(slice->f_pmv[0],mb->f_motion_vectors[0],picture->f_code[0],mb);

		/* update other motion vector predictors */
		slice->f_pmv[1][0] = slice->f_pmv[0][0];
		slice->f_pmv[1][1] = slice->f_pmv[0][1];
	}
	else
	{
		mb->f_motion_vertical_field_select[0] = bitstream_get(1);
		slice_get_motion_vector(slice->f_pmv[0],mb->f_motion_vectors[0],picture->f_code[0],mb);

		mb->f_motion_vertical_field_select[1] = bitstream_get(1);
		slice_get_motion_vector(slice->f_pmv[1],mb->f_motion_vectors[1],picture->f_code[0],mb);
	}
325 326 327 328 329
}

static void slice_get_backward_motion_vectors(const picture_t *picture,slice_t *slice, 
		macroblock_t *mb)
{
330 331 332 333 334 335
	if (mb->motion_vector_count==1)
	{
		if (mb->mv_format==MV_FIELD && !mb->dmv)
		{
			fprintf(stderr,"field based mv\n");
			mb->b_motion_vertical_field_select[1] = 
336
				mb->b_motion_vertical_field_select[0] = bitstream_get(1);
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
		}

		slice_get_motion_vector(slice->b_pmv[0],mb->b_motion_vectors[0],picture->f_code[1],mb);

		/* update other motion vector predictors */
		slice->b_pmv[1][0] = slice->b_pmv[0][0];
		slice->b_pmv[1][1] = slice->b_pmv[0][1];
	}
	else
	{
		mb->b_motion_vertical_field_select[0] = bitstream_get(1);
		slice_get_motion_vector(slice->b_pmv[0],mb->b_motion_vectors[0],picture->f_code[1],mb);

		mb->b_motion_vertical_field_select[1] = bitstream_get(1);
		slice_get_motion_vector(slice->b_pmv[1],mb->b_motion_vectors[1],picture->f_code[1],mb);
	}
353 354 355 356 357 358 359 360 361 362 363
}

inline void slice_reset_pmv(slice_t *slice)
{
	memset(slice->b_pmv,0,sizeof(sint_16) * 4);
	memset(slice->f_pmv,0,sizeof(sint_16) * 4);
}

void
slice_get_macroblock(const picture_t *picture,slice_t* slice, macroblock_t *mb)
{
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
	uint_32 quantizer_scale_code;
	uint_32 picture_structure = picture->picture_structure;

	// get macroblock_type 
	mb->macroblock_type = Get_macroblock_type(picture->picture_coding_type);

	// get frame/field motion type 
	if (mb->macroblock_type & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD))
	{
		if (picture_structure == FRAME_PICTURE) // frame_motion_type 
			mb->motion_type = picture->frame_pred_frame_dct ? MC_FRAME : bitstream_get(2);
		else // field_motion_type 
			mb->motion_type = bitstream_get(2);
	}
	else if ((mb->macroblock_type & MACROBLOCK_INTRA) && picture->concealment_motion_vectors)
	{
		// concealment motion vectors 
		mb->motion_type = (picture_structure==FRAME_PICTURE) ? MC_FRAME : MC_FIELD;
	}

	// derive motion_vector_count, mv_format and dmv, (table 6-17, 6-18)
	if (picture_structure==FRAME_PICTURE)
	{
		mb->motion_vector_count = (mb->motion_type==MC_FIELD) ? 2 : 1;
		mb->mv_format = (mb->motion_type==MC_FRAME) ? MV_FRAME : MV_FIELD;
	}
	else
	{
		mb->motion_vector_count = (mb->motion_type==MC_16X8) ? 2 : 1;
		mb->mv_format = MV_FIELD;
	}

	mb->dmv = (mb->motion_type==MC_DMV); // dual prime
397 398

	//Set if we need to scale motion vector prediction by 1/2
399
	mb->mvscale = ((mb->mv_format==MV_FIELD) && (picture_structure==FRAME_PICTURE));
400

401
	// get dct_type (frame DCT / field DCT) 
402 403 404 405 406 407 408 409
	if( (picture_structure==FRAME_PICTURE) && (!picture->frame_pred_frame_dct) && 
			(mb->macroblock_type & (MACROBLOCK_PATTERN|MACROBLOCK_INTRA)) )
		mb->dct_type =  bitstream_get(1);
	else
		mb->dct_type =  0;


	if (mb->macroblock_type & MACROBLOCK_QUANT)
410 411
	{
		quantizer_scale_code = bitstream_get(5);
412 413 414 415 416

		//The quantizer scale code propogates up to the slice level
		if(picture->q_scale_type)
			slice->quantizer_scale = non_linear_quantizer_scale[quantizer_scale_code];
		else
417 418
			slice->quantizer_scale = quantizer_scale_code << 1;
	}
419 420
		

421
	// 6.3.17.2 Motion vectors 
422

423 424
	//decode forward motion vectors 
	if ((mb->macroblock_type & MACROBLOCK_MOTION_FORWARD) || 
425
			((mb->macroblock_type & MACROBLOCK_INTRA) && picture->concealment_motion_vectors))
426
			slice_get_forward_motion_vectors(picture,slice,mb);
427

428 429 430
	//decode backward motion vectors 
	if (mb->macroblock_type & MACROBLOCK_MOTION_BACKWARD)
			slice_get_backward_motion_vectors(picture,slice,mb);
431

432 433
	if ((mb->macroblock_type & MACROBLOCK_INTRA) && picture->concealment_motion_vectors)
		bitstream_flush(1); // remove marker_bit 
434

435
	//6.3.17.4 Coded block pattern 
436
	mb->coded_block_pattern = 0;
437 438
	if (mb->macroblock_type & MACROBLOCK_PATTERN)
		mb->coded_block_pattern = Get_coded_block_pattern();
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473

	if (mb->macroblock_type & MACROBLOCK_INTRA)
	{
		mb->coded_block_pattern = 0x3f;

		// Decode lum blocks 
		slice_get_intra_block(picture,slice,&mb->y_blocks[0*64],0);
		slice_get_intra_block(picture,slice,&mb->y_blocks[1*64],0);
		slice_get_intra_block(picture,slice,&mb->y_blocks[2*64],0);
		slice_get_intra_block(picture,slice,&mb->y_blocks[3*64],0);
		// Decode chroma blocks 
		slice_get_intra_block(picture,slice,mb->cr_blocks,1);
		slice_get_intra_block(picture,slice,mb->cb_blocks,2);
	}
	//coded_block_pattern is set only if there are blocks in bitstream
	else if(mb->coded_block_pattern)
	{
		// Decode lum blocks 
		if (mb->coded_block_pattern & 0x20)
			slice_get_non_intra_block(picture,slice,&mb->y_blocks[0*64],0);
		if (mb->coded_block_pattern & 0x10)
			slice_get_non_intra_block(picture,slice,&mb->y_blocks[1*64],0);
		if (mb->coded_block_pattern & 0x08)
			slice_get_non_intra_block(picture,slice,&mb->y_blocks[2*64],0);
		if (mb->coded_block_pattern & 0x04)
			slice_get_non_intra_block(picture,slice,&mb->y_blocks[3*64],0);
		
		// Decode chroma blocks 
		if (mb->coded_block_pattern & 0x2)
			slice_get_non_intra_block(picture,slice,mb->cr_blocks,1);

		if (mb->coded_block_pattern & 0x1)
			slice_get_non_intra_block(picture,slice,mb->cb_blocks,2);
	}

474 475
	// 7.2.1 DC coefficients in intra blocks 
	if (!(mb->macroblock_type & MACROBLOCK_INTRA))
476 477 478 479 480 481 482 483
	{
		//FIXME this looks suspicious...should be reset to 2^(intra_dc_precision+7)
		//
		//lets see if it works
		slice->dc_dct_pred[0]=slice->dc_dct_pred[1]=slice->dc_dct_pred[2]= 
			1<<(picture->intra_dc_precision + 7) ;
	}

484 485
	//7.6.3.4 Resetting motion vector predictors 
	if ((mb->macroblock_type & MACROBLOCK_INTRA) && !picture->concealment_motion_vectors)
486 487 488
		slice_reset_pmv(slice);


489 490 491 492 493 494 495
	// special "No_MC" macroblock_type case 
	// 7.6.3.5 Prediction in P pictures 
	if ((picture->picture_coding_type==P_TYPE) 
		&& !(mb->macroblock_type & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_INTRA)))
	{
		// non-intra mb without forward mv in a P picture 
		// 7.6.3.4 Resetting motion vector predictors 
496 497 498 499
		slice_reset_pmv(slice);
		memset(mb->f_motion_vectors[0],0,8);
		mb->macroblock_type |= MACROBLOCK_MOTION_FORWARD;

500 501 502 503 504 505 506 507 508 509
		//6.3.17.1 Macroblock modes, frame_motion_type 
		if (picture->picture_structure==FRAME_PICTURE)
			mb->motion_type = MC_FRAME;
		else
		{
			mb->motion_type = MC_FIELD;
			// predict from field of same parity 
			mb->f_motion_vertical_field_select[0]= (picture->picture_structure==BOTTOM_FIELD);
		}
	}
510 511 512 513 514 515 516 517 518 519
}

void
slice_init(void)
{
	mb.y_blocks = y_blocks;
	mb.cr_blocks = cr_blocks;
	mb.cb_blocks = cb_blocks;
}

520

521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
uint_32
slice_process(picture_t *picture,uint_8 *slice_data)
{
	uint_32 mba;      
	uint_32 mba_inc;
	uint_32 prev_macroblock_type = 0; 
	uint_32 mb_width = picture->coded_picture_width >> 4;
	uint_32 i;

	mba = ((slice_data[0] &0xff) - 1) * mb_width - 1;

	slice_reset_pmv(&slice);
	slice_get_slice_header(picture,&slice);
	do
	{
		mba_inc = Get_macroblock_address_increment();

		if(mba_inc > 1)
		{
			//FIXME: this should be a function in slice.c instead
			//reset intra dc predictor on skipped block
			slice.dc_dct_pred[0]=slice.dc_dct_pred[1]=slice.dc_dct_pred[2]=
				1<<(picture->intra_dc_precision + 7);

			mb.coded_block_pattern = 0;
			mb.skipped = 1;

			//handling of skipped mb's differs between P_TYPE and B_TYPE
			//pictures
			if(picture->picture_coding_type == P_TYPE)
			{
				slice_reset_pmv(&slice);
				memset(mb.f_motion_vectors[0],0,8);
				mb.macroblock_type = MACROBLOCK_MOTION_FORWARD;

				for(i=0; i< mba_inc - 1; i++)
				{
					mb.mba = ++mba;
					motion_comp(picture,&mb);
				}
			}
			else
			{
				memcpy(mb.f_motion_vectors[0],slice.f_pmv,8);
				memcpy(mb.b_motion_vectors[0],slice.b_pmv,8);
				mb.macroblock_type = prev_macroblock_type;

				for(i=0; i< mba_inc - 1; i++)
				{
					mb.mba = ++mba;
					motion_comp(picture,&mb);
				}
			}
			mb.skipped = 0;
		}
		
		mb.mba = ++mba; 

		slice_get_macroblock(picture,&slice,&mb);

		//we store the last macroblock mv flags, as skipped b-frame blocks
		//inherit them
		prev_macroblock_type = mb.macroblock_type & (MACROBLOCK_MOTION_FORWARD | MACROBLOCK_MOTION_BACKWARD);

		idct(&mb);
		motion_comp(picture,&mb);

	}
	while(bitstream_show(23));

	return (mba >= picture->last_mba);
}