ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
libmpeg.c
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright (c) 2006-2007 Eugene Plotnikov <e-plotnikov@operamail.com>
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
9 # Based on refernce software of MSSG
10 */
11 
12 #include <malloc.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <kernel.h>
16 
17 #include "libmpeg.h"
18 #include "libmpeg_internal.h"
19 
21 static s64* s_pCurPTS;
22 
23 static void ( *LumaOp[ 8 ] ) ( _MPEGMotion* ) = {
26 };
27 
28 static void ( *ChromaOp[ 8 ] ) ( void ) = {
31 };
32 
33 static void ( *PutBlockOp[ 3 ] ) ( _MPEGMotions* ) = {
35 };
36 
37 static void ( *AddBlockOp[ 2 ][ 2 ] ) ( _MPEGMotions* ) = {
40 };
41 
42 static float s_FrameRate[ 16 ] = {
43  0.0F, ( ( 23.0F * 1000.0F ) / 1001.0F ),
44  24.0F, 25.0F, ( ( 30.0F * 1000.0F ) / 1001.0F ),
45  30.0F, 50.0F, ( ( 60.0F * 1000.0F ) / 1001.0F ),
46  60.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F
47 };
48 
49 static void* ( *_init_cb ) ( void*, MPEGSequenceInfo* );
50 static void* s_pInitCBParam;
51 
52 static void* _init_seq ( void );
53 static void _destroy_seq ( void );
54 
55 static int _get_hdr ( void );
56 
57 static void _seq_header ( void );
58 static void _gop_header ( void );
59 static void _pic_header ( void );
60 static void _ext_and_ud ( void );
61 static void _ext_unknown ( void );
62 static void _ext_seq ( void );
63 static void _ext_seq_dsp ( void );
64 static void _ext_qnt_mtx ( void );
65 static void _ext_cpy_rgt ( void );
66 static void _ext_seq_scl ( void );
67 static void _ext_pic_dsp ( void );
68 static void _ext_pic_cod ( void );
69 static void _ext_pic_ssc ( void );
70 static void _ext_pic_tsc ( void );
71 static void _xtra_bitinf ( void );
72 
73 static int _get_next_picture ( void*, s64* );
74 static int _get_first_picture ( void*, s64* );
75 
76 int ( *MPEG_Picture ) ( void*, s64* );
77 
78 static void ( *DoMC ) ( void );
79 
80 static void _mpeg12_picture_data ( void );
81 static int _mpeg12_slice ( int );
82 static int _mpeg12_dec_mb ( int*, int*, int[ 2 ][ 2 ][ 2 ], int [ 2 ][ 2 ], int[ 2 ] );
83 
85  int ( *apDataCB ) ( void* ), void* apDataCBParam,
86  void* ( *apInitCB ) ( void*, MPEGSequenceInfo* ), void* apInitCBParam,
87  s64* apCurPTS
88  ) {
89 
90  memset ( &s_MPEG12Ctx, 0, sizeof ( s_MPEG12Ctx ) );
91 
92  _MPEG_Initialize ( &s_MPEG12Ctx, apDataCB, apDataCBParam, &s_MPEG12Ctx.m_SI.m_fEOF );
93 
94  _init_cb = apInitCB;
95  s_pInitCBParam = apInitCBParam;
96  s_pCurPTS = apCurPTS;
97 
101  s_MPEG12Ctx.m_SI.m_Level = -1;
104  s_MPEG12Ctx.m_fMPEG2 = 0;
105 
106  s_MPEG12Ctx.m_MC[ 0 ].m_pSPRBlk = ( void* )0x70000000;
107  s_MPEG12Ctx.m_MC[ 0 ].m_pSPRRes = ( void* )0x70000300;
108  s_MPEG12Ctx.m_MC[ 0 ].m_pSPRMC = ( void* )0x70000600;
109  s_MPEG12Ctx.m_MC[ 1 ].m_pSPRBlk = ( void* )0x70001E00;
110  s_MPEG12Ctx.m_MC[ 1 ].m_pSPRRes = ( void* )0x70002100;
111  s_MPEG12Ctx.m_MC[ 1 ].m_pSPRMC = ( void* )0x70002400;
112 
114 
115 } /* end MPEG_Initialize */
116 
117 void MPEG_Destroy ( void ) {
118 
119  _destroy_seq ();
120  _MPEG_Destroy ();
121 
122 } /* end MPEG_Destroy */
123 
124 static void* _init_seq ( void ) {
125 
126  int i, lSize, lMBWidth, lMBHeight;
127 
128  if ( !s_MPEG12Ctx.m_fMPEG2 ) {
132  } /* end if */
133 
134  lMBWidth = ( s_MPEG12Ctx.m_SI.m_Width + 15 ) / 16;
135  lMBHeight = s_MPEG12Ctx.m_fMPEG2 && !s_MPEG12Ctx.m_fProgSeq ? 2 * ( ( s_MPEG12Ctx.m_SI.m_Height + 31 ) / 32 )
136  : ( s_MPEG12Ctx.m_SI.m_Height + 15 ) / 16;
137 
138  if ( lMBWidth != s_MPEG12Ctx.m_MBWidth ||
139  lMBHeight != s_MPEG12Ctx.m_MBHeight
140  ) {
141 
143 
144  s_MPEG12Ctx.m_MBWidth = lMBWidth;
145  s_MPEG12Ctx.m_MBHeight = lMBHeight;
146  s_MPEG12Ctx.m_SI.m_Width = lMBWidth << 4;
147  s_MPEG12Ctx.m_SI.m_Height = lMBHeight << 4;
148 
149  s_MPEG12Ctx.m_MBStride = lMBWidth * sizeof ( _MPEGMacroBlock8 );
150 
151  lSize = lMBWidth * ( lMBHeight + 1 ) * sizeof ( _MPEGMacroBlock8 ) + sizeof ( _MPEGMacroBlock8 );
152 
153  s_MPEG12Ctx.m_pFwdFrame = ( _MPEGMacroBlock8* )memalign ( 64, lSize );
154  s_MPEG12Ctx.m_pBckFrame = ( _MPEGMacroBlock8* )memalign ( 64, lSize );
155  s_MPEG12Ctx.m_pAuxFrame = ( _MPEGMacroBlock8* )memalign ( 64, lSize );
156 
157  s_MPEG12Ctx.m_pMBXY = ( _MPEGMBXY* )malloc (
159  );
160 
161  for ( i = 0; i < s_MPEG12Ctx.m_MBCount; ++i ) {
162  s_MPEG12Ctx.m_pMBXY[ i ].m_X = i % lMBWidth;
163  s_MPEG12Ctx.m_pMBXY[ i ].m_Y = i / lMBWidth;
164  } /* end for */
165 
166  } /* end if */
167 
169 
170 } /* end _init_seq */
171 
172 static void _destroy_seq ( void ) {
173 
175 
176  if ( s_MPEG12Ctx.m_pAuxFrame ) {
177  free ( s_MPEG12Ctx.m_pAuxFrame );
179  } /* end if */
180 
181  if ( s_MPEG12Ctx.m_pBckFrame ) {
182  free ( s_MPEG12Ctx.m_pBckFrame );
184  } /* end if */
185 
186  if ( s_MPEG12Ctx.m_pFwdFrame ) {
187  free ( s_MPEG12Ctx.m_pFwdFrame );
189  } /* end if */
190 
191  if ( s_MPEG12Ctx.m_pMBXY ) {
192  free ( s_MPEG12Ctx.m_pMBXY );
194  } /* end if */
195 
198 
199 } /* end _destroy_seq */
200 
201 static int _get_hdr ( void ) {
202 
203  unsigned int lCode;
204 
205  while ( 1 ) {
206 
207  lCode = _MPEG_NextStartCode (); _MPEG_GetBits ( 32 );
208 
209  switch ( lCode ) {
210 
211  case _MPEG_CODE_SEQ_HDR:
212  _seq_header ();
213  break;
214 
216  _gop_header ();
217  break;
218 
220  _pic_header ();
221  return 1;
222 
223  case _MPEG_CODE_SEQ_END:
225  return 0;
226  break;
227 
228  } /* end switch */
229 
230  } /* end while */
231 
232 } /* end _get_hdr */
233 
234 static void _seq_header ( void ) {
235 
238 
239  _MPEG_GetBits ( 4 ); /* aspect_ratio_information */
240  s_MPEG12Ctx.m_SI.m_MSPerFrame = ( int )(
241  ( 1000.0F / s_FrameRate[ s_MPEG12Ctx.m_FRCode = _MPEG_GetBits ( 4 ) ] ) + 0.5F
242  );
243  _MPEG_GetBits ( 18 ); /* bit_rate_value */
244  _MPEG_GetBits ( 1 ); /* marker_bit */
245  _MPEG_GetBits ( 10 ); /* vbv_buffer_size */
246  _MPEG_GetBits ( 1 ); /* constrained_parameters_flag */
247 
248  if ( _MPEG_GetBits ( 1 ) )
249  _MPEG_SetQM ( 0 );
250  else _MPEG_SetDefQM ( 0 );
251 
252  if ( _MPEG_GetBits ( 1 ) )
253  _MPEG_SetQM ( 1 );
254  else _MPEG_SetDefQM ( 1 );
255 
256  _ext_and_ud ();
257 
258 } /* end _seq_header */
259 
260 static void _gop_header ( void ) {
261 #ifdef _DEBUG
262  _MPEG_GetBits ( 1 ); /* drop_flag */
263  _MPEG_GetBits ( 5 ); /* hour */
264  _MPEG_GetBits ( 6 ); /* minute */
265  _MPEG_GetBits ( 1 ); /* marker_bit */
266  _MPEG_GetBits ( 6 ); /* sec */
267  _MPEG_GetBits ( 6 ); /* frame */
268  _MPEG_GetBits ( 1 ); /* closed_gop */
269  _MPEG_GetBits ( 1 ); /* broken_link */
270 #else
271  _MPEG_GetBits ( 27 );
272 #endif /* _DEBUG */
273  _ext_and_ud ();
274 
275 } /* end _gop_header */
276 
277 static void _pic_header ( void ) {
278 
279  unsigned int lPicCT;
280 
281  _MPEG_GetBits ( 10 ); /* temporal_reference */
282  lPicCT = _MPEG_GetBits ( 3 );
283  _MPEG_GetBits ( 16 ); /* vbv_delay */
284 
285  if ( lPicCT == _MPEG_PT_P || lPicCT == _MPEG_PT_B ) {
288  } /* end if */
289 
290  if ( lPicCT == _MPEG_PT_B ) {
293  } /* end if */
294 
295  _xtra_bitinf ();
296  _ext_and_ud ();
297 
299 
300 } /* end _pic_header */
301 
302 static void _ext_and_ud ( void ) {
303 
304  int lCode, lXID;
305 
306  lCode = _MPEG_NextStartCode ();
307 
308  while ( lCode == _MPEG_CODE_EXTENSION || lCode == _MPEG_CODE_USER_DATA ) {
309 
310  if ( lCode == _MPEG_CODE_EXTENSION ) {
311 
312  _MPEG_GetBits ( 32 );
313  lXID = _MPEG_GetBits ( 4 );
314 
315  switch ( lXID ) {
316 
317  case _MPEG_XID_0:
318  _ext_unknown ();
319  break;
320 
321  case _MPEG_XID_SEQUENCE:
322  _ext_seq ();
323  break;
324 
325  case _MPEG_XID_DISPLAY:
326  _ext_seq_dsp ();
327  break;
328 
329  case _MPEG_XID_QMATRIX:
330  _ext_qnt_mtx ();
331  break;
332 
333  case _MPEG_XID_COPYRIGHT:
334  _ext_cpy_rgt ();
335  break;
336 
337  case _MPEG_XID_SCALABLE:
338  _ext_seq_scl ();
339  break;
340 
341  case _MPEG_XID_6:
342  _ext_unknown ();
343  break;
344 
345  case _MPEG_XID_PIC_DSP:
346  _ext_pic_dsp ();
347  break;
348 
349  case _MPEG_XID_PIC_COD:
350  _ext_pic_cod ();
351  break;
352 
353  case _MPEG_XID_PIC_SSC:
354  _ext_pic_ssc ();
355  break;
356 
357  case _MPEG_XID_PIC_TSC:
358  _ext_pic_tsc ();
359  break;
360 
361  } /* end switch */
362 
363  lCode = _MPEG_NextStartCode ();
364 
365  } else { /* user data */
366 
367  _MPEG_GetBits ( 32 );
368  lCode = _MPEG_NextStartCode ();
369 
370  } /* end else */
371 
372  } /* end while */
373 
374 } /* end _ext_and_ud */
375 
376 static void _ext_unknown ( void ) {
377 
378 
379 } /* end _ext_unknown */
380 
381 static void _ext_seq ( void ) {
382 
383  int lHSzX;
384  int lVSzX;
385  int lProfLevel;
386  int lFRXn, lFRXd;
387 
388  s_MPEG12Ctx.m_fMPEG2 = 1;
389 
390  *( volatile unsigned int* )0x10002010 &= 0xFF7FFFFF;
391 
392  lProfLevel = _MPEG_GetBits ( 8 );
395  lHSzX = _MPEG_GetBits ( 2 );
396  lVSzX = _MPEG_GetBits ( 2 );
397 #ifdef _DEBUG
398  _MPEG_GetBits ( 12 ); /* bit_rate_extension */
399  _MPEG_GetBits ( 1 ); /* marker_bit */
400  _MPEG_GetBits ( 8 ); /* vbv_buffer_size_extension */
401  _MPEG_GetBits ( 1 ); /* low_delay */
402  lFRXn = _MPEG_GetBits ( 2 );
403  lFRXd = _MPEG_GetBits ( 5 );
404 #else
405  _MPEG_GetBits ( 22 );
406  lFRXn = _MPEG_GetBits ( 2 );
407  lFRXd = _MPEG_GetBits ( 5 );
408 #endif /* _DEBUG */
409  s_MPEG12Ctx.m_SI.m_MSPerFrame = ( int )(
410  ( 1000.0F / (
411  s_FrameRate[ s_MPEG12Ctx.m_FRCode ] * ( ( lFRXn + 1.0F ) / ( lFRXd + 1.0F ) )
412  )
413  ) + 0.5F
414  );
415 
416  if( ( lProfLevel >> 7 ) & 1 ) {
417 
418  if ( ( lProfLevel & 15 ) == 5 ) {
419 
422 
424 
425  } else {
426 
427  s_MPEG12Ctx.m_SI.m_Profile = lProfLevel >> 4;
428  s_MPEG12Ctx.m_SI.m_Level = lProfLevel & 0xF;
429 
430  } /* end else */
431 
432  s_MPEG12Ctx.m_SI.m_Width = ( lHSzX << 12 ) | ( s_MPEG12Ctx.m_SI.m_Width & 0x0FFF );
433  s_MPEG12Ctx.m_SI.m_Height = ( lVSzX << 12 ) | ( s_MPEG12Ctx.m_SI.m_Height & 0x0FFF );
434 
435 } /* end _ext_seq */
436 
437 static void _ext_seq_dsp ( void ) {
438 
440 
441  if ( _MPEG_GetBits ( 1 ) ) { /* color_description */
442 #ifdef _DEBUG
443  _MPEG_GetBits ( 8 ); /* color_primaries */
444  _MPEG_GetBits ( 8 ); /* transfer_characteristics */
445  _MPEG_GetBits ( 8 ); /* matrix_coefficients */
446 #else
447  _MPEG_GetBits ( 24 );
448 #endif /* _DEBUG */
449  } /* end if */
450 #ifdef _DEBUG
451  _MPEG_GetBits ( 14 ); /* display_horizontal_size */
452  _MPEG_GetBits ( 1 ); /* marker_bit */
453  _MPEG_GetBits ( 14 ); /* display_vertical_size */
454 #else
455  _MPEG_GetBits ( 29 );
456 #endif /* _DEBUG */
457 } /* end _ext_seq_dsp */
458 
459 static void _ext_qnt_mtx ( void ) {
460 
461  int i;
462 
463  if ( _MPEG_GetBits ( 1 ) ) _MPEG_SetQM ( 0 );
464  if ( _MPEG_GetBits ( 1 ) ) _MPEG_SetQM ( 1 );
465 
466  if ( _MPEG_GetBits ( 1 ) ) for ( i = 0; i < 16; ++i ) _MPEG_GetBits ( 32 );
467  if ( _MPEG_GetBits ( 1 ) ) for ( i = 0; i < 16; ++i ) _MPEG_GetBits ( 32 );
468 
469 } /* end _ext_qnt_mtx */
470 
471 static void _ext_cpy_rgt ( void ) {
472 #ifdef _DEBUG
473  _MPEG_GetBits ( 1 ); /* copyright_flag */
474  _MPEG_GetBits ( 8 ); /* copyright_identifier */
475  _MPEG_GetBits ( 1 ); /* original_or_copy */
476  _MPEG_GetBits ( 7 ); /* reserved_data */
477  _MPEG_GetBits ( 1 ); /* marker_bit */
478  _MPEG_GetBits ( 20 ); /* copyright_number_1 */
479  _MPEG_GetBits ( 1 ); /* marker_bit */
480  _MPEG_GetBits ( 22 ); /* copyright_number_2 */
481  _MPEG_GetBits ( 1 ); /* marker_bit */
482  _MPEG_GetBits ( 22 ); /* copyright_number_3 */
483 #else
484  _MPEG_GetBits ( 32 );
485  _MPEG_GetBits ( 32 );
486  _MPEG_GetBits ( 20 );
487 #endif /* _DEBUG */
488 } /* end _ext_cop_rgt */
489 
490 static void _ext_seq_scl ( void ) {
491 
492 } /* end _ext_seq_scl */
493 
494 static void _ext_pic_dsp ( void ) {
495 
496  int i;
497  int lnFCO;
498 
499  if ( s_MPEG12Ctx.m_fProgSeq ) {
500 
501  if ( s_MPEG12Ctx.m_fRepFF )
502  lnFCO = s_MPEG12Ctx.m_fTopFF ? 3 : 2;
503  else lnFCO = 1;
504 
505  } else {
506 
508  lnFCO = 1;
509  else lnFCO = s_MPEG12Ctx.m_fRepFF ? 3 : 2;
510 
511  } /* end else */
512 
513  for ( i = 0; i < lnFCO; ++i ) {
514  _MPEG_GetBits ( 16 ); /* frame_center_horizontal_offset[ i ] */
515  _MPEG_GetBits ( 1 ); /* marker_bit */
516  _MPEG_GetBits ( 16 ); /* frame_center_vertical_offset [ i ] */
517  _MPEG_GetBits ( 1 ); /* marker_bit */
518  } /* end for */
519 
520 } /* end _ext_pic_dsp */
521 
522 static void _ext_pic_cod ( void ) {
523 
524  s_MPEG12Ctx.m_FCode[ 0 ][ 0 ] = _MPEG_GetBits ( 4 );
525  s_MPEG12Ctx.m_FCode[ 0 ][ 1 ] = _MPEG_GetBits ( 4 );
526  s_MPEG12Ctx.m_FCode[ 1 ][ 0 ] = _MPEG_GetBits ( 4 );
527  s_MPEG12Ctx.m_FCode[ 1 ][ 1 ] = _MPEG_GetBits ( 4 );
528  _MPEG_SetIDCP ();
535 #ifdef _DEBUG
536  _MPEG_GetBits ( 1 ); /* chroma_420_type */
537  _MPEG_GetBits ( 1 ); /* progressive_frame */
538 #else
539  _MPEG_GetBits ( 2 );
540 #endif /* _DEBUG */
541  if ( _MPEG_GetBits ( 1 ) ) { /* composite_display_flag */
542 #ifdef _DEBUG
543  _MPEG_GetBits ( 1 ); /* v_axis */
544  _MPEG_GetBits ( 3 ); /* field_sequence */
545  _MPEG_GetBits ( 1 ); /* sub_carrier */
546  _MPEG_GetBits ( 7 ); /* burst_amplitude */
547  _MPEG_GetBits ( 8 ); /* sub_carrier_phase */
548 #else
549  _MPEG_GetBits ( 20 );
550 #endif /* _DEBUG */
551  } /* end if */
552 
553 } /* end _ext_pic_cod */
554 
555 static void _ext_pic_ssc ( void ) {
556 
557 } /* end _ext_pic_ssc */
558 
559 static void _ext_pic_tsc ( void ) {
560 
561 } /* end _ext_pic_tsc */
562 
563 static void _xtra_bitinf ( void ) {
564 
565  while ( _MPEG_GetBits ( 1 ) ) _MPEG_GetBits ( 8 );
566 
567 } /* end _xtra_bitinf */
568 
569 static int _get_first_picture ( void* apData, s64* apPTS ) {
570 
571  int retVal = _get_hdr ();
572 
573  if ( retVal ) {
574 
576 
577  apData = _init_seq ();
579 
581 
583 
585 
586  retVal = _get_next_picture ( apData, apPTS );
587 
588  } /* end if */
589 
590  return retVal;
591 
592 } /* end _get_first_picture */
593 
594 static int _get_next_picture ( void* apData, s64* apPTS ) {
595 
596  int retVal;
597 
598  while ( 1 ) {
599 
600  if ( ( retVal = _get_hdr () ) ) {
601 
602  int lfPic = 0;
603 
605 
607 
608  void* lpData;
609 
611  lpData = s_MPEG12Ctx.m_pAuxFrame;
612  *apPTS = s_MPEG12Ctx.m_AuxPTS;
613  } else {
614  lpData = s_MPEG12Ctx.m_pFwdFrame;
615  *apPTS = s_MPEG12Ctx.m_FwdPTS;
616  } /* end else */
617 
618  lfPic = _MPEG_CSCImage ( lpData, apData, s_MPEG12Ctx.m_MBCount );
619 
620  } /* end if */
621 
623 
625 
626  if ( lfPic ) break;
627 
628  } else break;
629 
630  } /* end while */
631 
632  return retVal;
633 
634 } /* end _get_next_picture */
635 
636 static void _mpeg12_do_next_mc ( void ) {
637 
638  _MPEGMotions* lpMotions = &s_MPEG12Ctx.m_MC[ !s_MPEG12Ctx.m_CurMC ];
639  _MPEGMotion* lpMotion = &lpMotions -> m_Motion[ 0 ];
640 
641  while ( lpMotion -> MC_Luma ) {
642 
643  _MPEG_do_mc ( lpMotion );
644  ++lpMotion;
645 
646  } /* end while */
647 
648  lpMotions -> BlockOp ( lpMotions );
649 
650 } /* end _mpeg12_do_next_mc */
651 
652 static void _mpeg12_do_first_mc ( void ) {
653 
655 
656 } /* end _mpeg12_do_first_mc */
657 
658 static void _mpeg12_picture_data ( void ) {
659 
661  _MPEGMacroBlock8* lpMB;
662  s64 lPTS;
663 
665 
669  } else {
670  if ( !s_MPEG12Ctx.m_fSecField ) {
671  lpMB = s_MPEG12Ctx.m_pFwdFrame;
672  lPTS = s_MPEG12Ctx.m_FwdPTS;
675  s_MPEG12Ctx.m_pBckFrame = lpMB;
676  s_MPEG12Ctx.m_BckPTS = lPTS;
677  } /* end if */
680  } /* end else */
681  s_MPEG12Ctx.m_pCurFrameY = ( unsigned char* )s_MPEG12Ctx.m_pCurFrame;
682  s_MPEG12Ctx.m_pCurFrameCbCr = ( unsigned char* )s_MPEG12Ctx.m_pCurFrame + 256;
686  } /* end if */
687 
688  if ( s_MPEG12Ctx.m_PictStruct != _MPEG_PS_FRAME ) lMBAMax >>= 1;
689 
690  s_MPEG12Ctx.m_CurMC = 0;
692 
693  while ( _mpeg12_slice ( lMBAMax ) >= 0 );
694 
695  _MPEG_WaitBDEC ();
696  DoMC ();
697 
698 } /* end _mpeg12_picture_data */
699 
701  int* apPred, int aRSize, int aMotionCode, int aMotionResidual, int aFullPelVector
702  ) {
703 
704  int lLim = 16 << aRSize;
705  int lVec = aFullPelVector ? *apPred >> 1 : *apPred;
706 
707  if ( aMotionCode > 0 ) {
708 
709  lVec += ( ( aMotionCode - 1 ) << aRSize ) + aMotionResidual + 1;
710 
711  if ( lVec >= lLim ) lVec -= lLim + lLim;
712 
713  } else if ( aMotionCode < 0 ) {
714 
715  lVec -= ( ( -aMotionCode - 1 ) << aRSize ) + aMotionResidual + 1;
716 
717  if ( lVec < -lLim ) lVec += lLim + lLim;
718 
719  } /* end if */
720 
721  *apPred = aFullPelVector ? lVec << 1 : lVec;
722 
723 } /* end _mpeg12_decode_motion_vector */
724 
725 static void _mpeg12_dual_prime_vector ( int aDMV[][ 2 ], int* apDMVector, int aMVX, int aMVY ) {
726 
728 
729  if ( s_MPEG12Ctx.m_fTopFF ) {
730 
731  aDMV[ 0 ][ 0 ] = ( ( aMVX + ( aMVX > 0 ) ) >> 1 ) + apDMVector[ 0 ];
732  aDMV[ 0 ][ 1 ] = ( ( aMVY + ( aMVY > 0 ) ) >> 1 ) + apDMVector[ 1 ] - 1;
733 
734  aDMV[ 1 ][ 0 ] = ( ( 3 * aMVX + ( aMVX > 0 ) ) >> 1 ) + apDMVector[ 0 ];
735  aDMV[ 1 ][ 1 ] = ( ( 3 * aMVY + ( aMVY > 0 ) ) >> 1 ) + apDMVector[ 1 ] + 1;
736 
737  } else {
738 
739  aDMV[ 0 ][ 0 ] = ( ( 3 * aMVX + ( aMVX > 0 ) ) >> 1 ) + apDMVector[ 0 ];
740  aDMV[ 0 ][ 1 ] = ( ( 3 * aMVY + ( aMVY > 0 ) ) >> 1 ) + apDMVector[ 1 ] - 1;
741 
742  aDMV[ 1 ][ 0 ] = ( ( aMVX + ( aMVX > 0 ) ) >> 1 ) + apDMVector[ 0 ];
743  aDMV[ 1 ][ 1 ] = ( ( aMVY + ( aMVY > 0 ) ) >> 1 ) + apDMVector[ 1 ] + 1;
744 
745  } /* end else */
746 
747  } else {
748 
749  aDMV[ 0 ][ 0 ] = ( ( aMVX + ( aMVX > 0 ) ) >> 1 ) + apDMVector[ 0 ];
750  aDMV[ 0 ][ 1 ] = ( ( aMVY + ( aMVY > 0 ) ) >> 1 ) + apDMVector[ 1 ];
751 
753  --aDMV[ 0 ][ 1 ];
754  else ++aDMV[ 0 ][ 1 ];
755 
756  } /* end else */
757 
758 } /* end _mpeg12_dual_prime_vector */
759 
761  int* apPMV, int* apDMVector, int aHRSize, int aVRSize, int aDMV, int aMVScale, int aFullPelVector
762  ) {
763 
764  int lMotionCode = _MPEG_GetMotionCode ();
765  int lMotionResidual = aHRSize && lMotionCode ? _MPEG_GetBits ( aHRSize ) : 0;
766 
768  &apPMV[ 0 ], aHRSize, lMotionCode, lMotionResidual, aFullPelVector
769  );
770 
771  if ( aDMV ) apDMVector[ 0 ] = _MPEG_GetDMVector ();
772 
773  s_MPEG12Ctx.m_fError = lMotionCode == -32768;
774 
775  lMotionCode = _MPEG_GetMotionCode ();
776  lMotionResidual = aVRSize && lMotionCode ? _MPEG_GetBits ( aVRSize ) : 0;
777 
778  if ( aMVScale ) apPMV[ 1 ] >>= 1;
779 
781  &apPMV[ 1 ], aVRSize, lMotionCode, lMotionResidual, aFullPelVector
782  );
783 
784  if ( aMVScale ) apPMV[ 1 ] <<= 1;
785 
786  if ( aDMV ) apDMVector[ 1 ] = _MPEG_GetDMVector ();
787 
788  s_MPEG12Ctx.m_fError = lMotionCode == -32768;
789 
790 } /* end _mpeg12_motion_vector */
791 
793  int aPMV[ 2 ][ 2 ][ 2 ], int aDMVector[ 2 ], int aMVFS[ 2 ][ 2 ], int aS, int anMV,
794  int aMVFmt, int aHRSize, int aVRSize, int aDMV, int aMVScale
795  ) {
796 
797  if ( anMV == 1 ) {
798 
799  if ( aMVFmt == _MPEG_MV_FIELD && !aDMV ) aMVFS[ 1 ][ aS ] = aMVFS[ 0 ][ aS ] = _MPEG_GetBits ( 1 );
800 
801  _mpeg12_motion_vector ( aPMV[ 0 ][ aS ], aDMVector, aHRSize, aVRSize, aDMV, aMVScale, 0 );
802 
803  aPMV[ 1 ][ aS ][ 0 ] = aPMV[ 0 ][ aS ][ 0 ];
804  aPMV[ 1 ][ aS ][ 1 ] = aPMV[ 0 ][ aS ][ 1 ];
805 
806  } else {
807 
808  aMVFS[ 0 ][ aS ] = _MPEG_GetBits ( 1 );
809  _mpeg12_motion_vector ( aPMV[ 0 ][ aS ], aDMVector, aHRSize, aVRSize, aDMV, aMVScale, 0 );
810 
811  aMVFS[ 1 ][ aS ] = _MPEG_GetBits ( 1 );
812  _mpeg12_motion_vector ( aPMV[ 1 ][ aS ], aDMVector, aHRSize, aVRSize, aDMV, aMVScale, 0 );
813 
814  } /* end else */
815 
816 } /* end _mpeg12_motion_vectors */
817 
818 static int _mpeg12_dec_mb (
819  int* apMBType, int* apMotionType,
820  int aPMV[ 2 ][ 2 ][ 2 ], int aMVFS[ 2 ][ 2 ], int aDMVector[ 2 ]
821  ) {
822 
823  int lMBType;
824  int lDMV;
825  int lMVScale;
826  int lnMV;
827  int lMVFmt;
828  int lDCType;
829  int lMotionType;
830  int lfIntra;
831 
832  lMotionType = 0;
833  lMBType = _MPEG_GetMBType ();
834 
835  if ( !lMBType ) return 0;
836 
837  lfIntra = lMBType & _MPEG_MBT_INTRA;
838 
839  if ( lMBType & ( _MPEG_MBT_MOTION_FORWARD | _MPEG_MBT_MOTION_BACKWARD ) ) {
840 
842  lMotionType = s_MPEG12Ctx.m_fFPFrmDCT ? _MPEG_MC_FRAME : _MPEG_GetBits ( 2 );
843  else lMotionType = _MPEG_GetBits ( 2 );
844 
845  } else if ( lfIntra && s_MPEG12Ctx.m_fConsMV )
847 
849  lnMV = lMotionType == _MPEG_MC_FIELD ? 2 : 1;
850  lMVFmt = lMotionType == _MPEG_MC_FRAME ? _MPEG_MV_FRAME : _MPEG_MV_FIELD;
851  } else {
852  lnMV = ( lMotionType == _MPEG_MC_16X8 ) ? 2 : 1;
853  lMVFmt = _MPEG_MV_FIELD;
854  } /* end else */
855 
856  lDMV = lMotionType == _MPEG_MC_DMV;
857  lMVScale = lMVFmt == _MPEG_MV_FIELD && s_MPEG12Ctx.m_PictStruct == _MPEG_PS_FRAME;
858  lDCType = s_MPEG12Ctx.m_PictStruct == _MPEG_PS_FRAME &&
860  lMBType & ( _MPEG_MBT_PATTERN | _MPEG_MBT_INTRA ) ? _MPEG_GetBits ( 1 ) : 0;
861 
862  if ( lMBType & _MPEG_MBT_QUANT ) s_MPEG12Ctx.m_QScale = _MPEG_GetBits ( 5 );
863 
864  if ( ( lMBType & _MPEG_MBT_MOTION_FORWARD ) ||
865  ( lfIntra && s_MPEG12Ctx.m_fConsMV )
866  ) {
867 
868  if ( s_MPEG12Ctx.m_fMPEG2 )
870  aPMV, aDMVector, aMVFS, 0, lnMV, lMVFmt,
871  s_MPEG12Ctx.m_FCode[ 0 ][ 0 ] - 1, s_MPEG12Ctx.m_FCode[ 0 ][ 1 ] - 1, lDMV, lMVScale
872  );
873  else _mpeg12_motion_vector (
874  aPMV[ 0 ][ 0 ], aDMVector, s_MPEG12Ctx.m_FwdFCode - 1, s_MPEG12Ctx.m_FwdFCode - 1,
876  );
877  } /* end if */
878 
879  if ( s_MPEG12Ctx.m_fError ) return 0;
880 
881  if ( lMBType & _MPEG_MBT_MOTION_BACKWARD ) {
882 
883  if ( s_MPEG12Ctx.m_fMPEG2 )
885  aPMV, aDMVector, aMVFS, 1, lnMV, lMVFmt,
886  s_MPEG12Ctx.m_FCode[ 1 ][ 0 ] - 1, s_MPEG12Ctx.m_FCode[ 1 ][ 1 ] - 1, 0, lMVScale
887  );
888  else _mpeg12_motion_vector (
889  aPMV[ 0 ][ 1 ], aDMVector, s_MPEG12Ctx.m_BckFCode - 1, s_MPEG12Ctx.m_BckFCode - 1,
891  );
892  } /* end if */
893 
894  if ( s_MPEG12Ctx.m_fError ) return 0;
895 
896  if ( lfIntra && s_MPEG12Ctx.m_fConsMV ) _MPEG_GetBits ( 1 );
897 
898  if ( lMBType & ( _MPEG_MBT_INTRA | _MPEG_MBT_PATTERN ) )
899  _MPEG_BDEC ( lfIntra, s_MPEG12Ctx.m_fDCRst, lDCType, s_MPEG12Ctx.m_QScale, s_MPEG12Ctx.m_pCurMotions -> m_pSPRBlk );
900 
901  s_MPEG12Ctx.m_fDCRst = !lfIntra;
902 
903  if ( lfIntra && !s_MPEG12Ctx.m_fConsMV )
904  aPMV[ 0 ][ 0 ][ 0 ] = aPMV[ 0 ][ 0 ][ 1 ] =
905  aPMV[ 1 ][ 0 ][ 0 ] = aPMV[ 1 ][ 0 ][ 1 ] =
906  aPMV[ 0 ][ 1 ][ 0 ] = aPMV[ 0 ][ 1 ][ 1 ] =
907  aPMV[ 1 ][ 1 ][ 0 ] = aPMV[ 1 ][ 1 ][ 1 ] = 0;
908 
910  !( lMBType & ( _MPEG_MBT_MOTION_FORWARD | _MPEG_MBT_INTRA ) )
911  ) {
912 
913  aPMV[ 0 ][ 0 ][ 0 ] = aPMV[ 0 ][ 0 ][ 1 ] =
914  aPMV[ 1 ][ 0 ][ 0 ] = aPMV[ 1 ][ 0 ][ 1 ] = 0;
915 
917  lMotionType = _MPEG_MC_FRAME;
918  else {
919  lMotionType = _MPEG_MC_FIELD;
920  aMVFS[ 0 ][ 0 ] = s_MPEG12Ctx.m_PictStruct == _MPEG_PS_BOTTOM_FIELD;
921  } /* end else */
922 
923  } /* end if */
924 
925  *apMBType = lMBType;
926  *apMotionType = lMotionType;
927 
928  return 1;
929 
930 } /* end _mpeg12_dec_mb */
931 
932 static void _mpeg12_get_ref (
933  _MPEGMacroBlock8* apMBSrc, int aX, int anY,
934  int aDX, int aDY, int aH, int aFSrc, int aFDst, int afAvg
935  ) {
936 
937  int lfInt = ( aH & 8 ) >> 3;
938  int lDXY = ( ( aDY & 1 ) << 1 ) | ( aDX & 1 );
939  int lUVXY = ( ( aDX & 2 ) >> 1 ) | ( aDY & 2 );
940  int lSrcX = aX + ( aDX >> 1 );
941  int lSrcY = ( ( anY + ( aDY >> 1 ) ) << lfInt ) + aFSrc;
942  int lMBX = lSrcX >> 4;
943  int lMBY = lSrcY >> 4;
944  _MPEGMotion* lpMotion = &s_MPEG12Ctx.m_pCurMotions -> m_Motion[ s_MPEG12Ctx.m_pCurMotions -> m_nMotions++ ];
945 
946  afAvg <<= 2;
947 
948  __asm__ __volatile__(
949  ".set noat\n\t"
950  "pnor $v0, $zero, $zero\n\t"
951  "ld $at, %4\n\t"
952  "pextlw %0, %3, %0\n\t"
953  "paddw $at, $at, $v0\n\t"
954  "pmaxw %0, %0, $zero\n\t"
955  "pminw %0, %0, $at\n\t"
956  "dsrl32 %1, %0, 0\n\t"
957  "sll %0, %0, 0\n\t"
958  ".set at\n\t"
959  : "=r"( lMBX ), "=r"( lMBY ) : "r"( lMBX ), "r"( lMBY ), "m"( s_MPEG12Ctx.m_MBWidth ) : "at", "v0"
960  );
961 
962  lpMotion -> m_pSrc = ( unsigned char* )( apMBSrc + lMBX + lMBY * s_MPEG12Ctx.m_MBWidth );
963  lpMotion -> m_pDstY = ( short* )( s_MPEG12Ctx.m_pCurMotions -> m_pSPRRes + ( aFDst << 5 ) );
964  lpMotion -> m_pDstCbCr = ( short* )( s_MPEG12Ctx.m_pCurMotions -> m_pSPRRes + 512 + ( aFDst << 3 ) );
965  lpMotion -> m_X = lSrcX & 0xF;
966  lpMotion -> m_Y = lSrcY & 0xF;
967  lpMotion -> m_H = aH;
968  lpMotion -> m_fInt = lfInt;
969  lpMotion -> m_Field = aFSrc;
970  lpMotion -> MC_Luma = LumaOp [ lDXY + afAvg ];
971  lpMotion -> MC_Chroma = ChromaOp[ lUVXY + afAvg ];
972 
973 } /* end _mpeg12_get_ref */
974 
975 static void _mpeg12_get_refs (
976  int aBX, int aBY, int aMBType, int aMotionType, int aPMV[ 2 ][ 2 ][ 2 ],
977  int aMVFS[ 2 ][ 2 ], int aDMVector[ 2 ]
978  ) {
979 
980  int lCurField;
981  int lDMV[ 2 ][ 2 ];
982  int lfAdd = 0;
983 
984  s_MPEG12Ctx.m_pCurMotions -> m_nMotions = 0;
985 
986  if ( ( aMBType & _MPEG_MBT_MOTION_FORWARD ) ||
988  ) {
989 
991 
992  if ( ( aMotionType == _MPEG_MC_FRAME ) ||
993  !( aMBType & _MPEG_MBT_MOTION_FORWARD )
994  ) {
995 
997  s_MPEG12Ctx.m_pFwdFrame, aBX, aBY, aPMV[ 0 ][ 0 ][ 0 ], aPMV[ 0 ][ 0 ][ 1 ],
998  16, 0, 0, 0
999  );
1000 
1001  } else if ( aMotionType == _MPEG_MC_FIELD ) {
1002 
1003  _mpeg12_get_ref (
1004  s_MPEG12Ctx.m_pFwdFrame, aBX, aBY >> 1, aPMV[ 0 ][ 0 ][ 0 ], aPMV[ 0 ][ 0 ][ 1 ] >> 1,
1005  8, aMVFS[ 0 ][ 0 ], 0, 0
1006  );
1007  _mpeg12_get_ref (
1008  s_MPEG12Ctx.m_pFwdFrame, aBX, aBY >> 1, aPMV[ 1 ][ 0 ][ 0 ], aPMV[ 1 ][ 0 ][ 1 ] >> 1,
1009  8, aMVFS[ 1 ][ 0 ], 8, 0
1010  );
1011 
1012  } else if ( aMotionType == _MPEG_MC_DMV ) {
1013 
1014  _mpeg12_dual_prime_vector ( lDMV, aDMVector, aPMV[ 0 ][ 0 ][ 0 ], aPMV[ 0 ][ 0 ][ 1 ] >> 1 );
1015 
1016  _mpeg12_get_ref (
1017  s_MPEG12Ctx.m_pFwdFrame, aBX, aBY >> 1, aPMV[ 0 ][ 0 ][ 0 ], aPMV[ 0 ][ 0 ][ 1 ] >> 1,
1018  8, 0, 0, 0
1019  );
1020  _mpeg12_get_ref (
1021  s_MPEG12Ctx.m_pFwdFrame, aBX, aBY >> 1, lDMV[ 0 ][ 0 ], lDMV[ 0 ][ 1 ],
1022  8, 1, 0, 1
1023  );
1024  _mpeg12_get_ref (
1025  s_MPEG12Ctx.m_pFwdFrame, aBX, aBY >> 1, aPMV[ 0 ][ 0 ][ 0 ], aPMV[ 0 ][ 0 ][ 1 ] >> 1,
1026  8, 1, 8, 0
1027  );
1028  _mpeg12_get_ref (
1029  s_MPEG12Ctx.m_pFwdFrame, aBX, aBY >> 1, lDMV[ 1 ][ 0 ], lDMV[ 1 ][ 1 ],
1030  8, 0, 8, 1
1031  );
1032 
1033  } /* end if */
1034 
1035  } else {
1036 
1037  _MPEGMacroBlock8* lpMBSrc;
1038 
1039  lCurField = ( s_MPEG12Ctx.m_PictStruct == _MPEG_PS_BOTTOM_FIELD );
1040  lpMBSrc = ( s_MPEG12Ctx.m_PictCodingType == _MPEG_PT_P ) &&
1042  ( lCurField != aMVFS[ 0 ][ 0 ] ) ? s_MPEG12Ctx.m_pBckFrame
1044 
1045  if ( ( aMotionType == _MPEG_MC_FIELD ) || !( aMBType & _MPEG_MBT_MOTION_FORWARD ) ) {
1046 
1047  _mpeg12_get_ref (
1048  lpMBSrc, aBX, aBY, aPMV[ 0 ][ 0 ][ 0 ], aPMV[ 0 ][ 0 ][ 1 ], 8,
1049  aMVFS[ 0 ][ 0 ], 0, 0
1050  );
1051  _mpeg12_get_ref (
1052  lpMBSrc, aBX, aBY + 8, aPMV[ 0 ][ 0 ][ 0 ], aPMV[ 0 ][ 0 ][ 1 ], 8,
1053  aMVFS[ 0 ][ 0 ], 8, 0
1054  );
1055 
1056  } else if ( aMotionType == _MPEG_MC_16X8 ) {
1057 
1058  _mpeg12_get_ref (
1059  lpMBSrc, aBX, aBY, aPMV[ 0 ][ 0 ][ 0 ], aPMV[ 0 ][ 0 ][ 1 ], 8,
1060  aMVFS[ 0 ][ 0 ], 0, 0
1061  );
1062 
1063  lpMBSrc = ( s_MPEG12Ctx.m_PictCodingType == _MPEG_PT_P ) &&
1065  ( lCurField != aMVFS[ 1 ][ 0 ] ) ? s_MPEG12Ctx.m_pBckFrame
1067  _mpeg12_get_ref (
1068  lpMBSrc, aBX, aBY + 8, aPMV[ 1 ][ 0 ][ 0 ], aPMV[ 1 ][ 0 ][ 1 ], 8,
1069  aMVFS[ 1 ][ 0 ], 8, 0
1070  );
1071 
1072  } else if ( aMotionType == _MPEG_MC_DMV ) {
1073 
1075 
1076  _mpeg12_dual_prime_vector ( lDMV, aDMVector, aPMV[ 0 ][ 0 ][ 0 ], aPMV[ 0 ][ 0 ][ 1 ] );
1077 
1078  _mpeg12_get_ref (
1079  s_MPEG12Ctx.m_pFwdFrame, aBX, aBY, aPMV[ 0 ][ 0 ][ 0 ], aPMV[ 0 ][ 0 ][ 1 ], 16,
1080  lCurField, 0, 0
1081  );
1082  _mpeg12_get_ref (
1083  lpMBSrc, aBX, aBY, lDMV[ 0 ][ 0 ], lDMV[ 0 ][ 1 ], 16, !lCurField, 1, 1
1084  );
1085 
1086  } /* end if */
1087 
1088  } /* end else */
1089 
1090  lfAdd = 1;
1091 
1092  } /* end if */
1093 
1094  if ( aMBType & _MPEG_MBT_MOTION_BACKWARD ) {
1095 
1097 
1098  if ( aMotionType == _MPEG_MC_FRAME ) {
1099 
1100  _mpeg12_get_ref (
1101  s_MPEG12Ctx.m_pBckFrame, aBX, aBY, aPMV[ 0 ][ 1 ][ 0 ], aPMV[ 0 ][ 1 ][ 1 ],
1102  16, 0, 0, lfAdd
1103  );
1104 
1105  } else {
1106 
1107  _mpeg12_get_ref (
1108  s_MPEG12Ctx.m_pBckFrame, aBX, aBY >> 1, aPMV[ 0 ][ 1 ][ 0 ], aPMV[ 0 ][ 1 ][ 1 ] >> 1,
1109  8, aMVFS[ 0 ][ 1 ], 0, lfAdd
1110  );
1111  _mpeg12_get_ref (
1112  s_MPEG12Ctx.m_pBckFrame, aBX, aBY >> 1, aPMV[ 1 ][ 1 ][ 0 ], aPMV[ 1 ][ 1 ][ 1 ] >> 1,
1113  8, aMVFS[ 1 ][ 1 ], 8, lfAdd
1114  );
1115 
1116  } /* end else */
1117 
1118  } else {
1119 
1120  if ( aMotionType == _MPEG_MC_FIELD ) {
1121 
1122  _mpeg12_get_ref (
1123  s_MPEG12Ctx.m_pBckFrame, aBX, aBY, aPMV[ 0 ][ 1 ][ 0 ], aPMV[ 0 ][ 1 ][ 1 ], 8,
1124  aMVFS[ 0 ][ 1 ], 0, lfAdd
1125  );
1126  _mpeg12_get_ref (
1127  s_MPEG12Ctx.m_pBckFrame, aBX, aBY + 8, aPMV[ 0 ][ 1 ][ 0 ], aPMV[ 0 ][ 1 ][ 1 ], 8,
1128  aMVFS[ 0 ][ 1 ], 8, lfAdd
1129  );
1130 
1131  } else if ( aMotionType == _MPEG_MC_16X8 ) {
1132 
1133  _mpeg12_get_ref (
1134  s_MPEG12Ctx.m_pBckFrame, aBX, aBY, aPMV[ 0 ][ 1 ][ 0 ], aPMV[ 0 ][ 1 ][ 1 ],
1135  8, aMVFS[ 0 ][ 1 ], 0, lfAdd
1136  );
1137  _mpeg12_get_ref (
1138  s_MPEG12Ctx.m_pBckFrame, aBX, aBY + 8, aPMV[ 1 ][ 1 ][ 0 ], aPMV[ 1 ][ 1 ][ 1 ],
1139  8, aMVFS[ 1 ][ 1 ], 8, lfAdd
1140  );
1141 
1142  } /* end if */
1143 
1144  } /* end else */
1145 
1146  } /* end if */
1147 
1149  ( _MPEGMacroBlock8* )s_MPEG12Ctx.m_pCurMotions -> m_pSPRMC,
1150  &s_MPEG12Ctx.m_pCurMotions -> m_Motion[ 0 ],
1152  );
1153 
1154 } /* end _mpeg12_get_refs */
1155 
1156 static void _mpeg2_mc (
1157  int aMBA, int aMBType, int aMotionType, int aPMV[ 2 ][ 2 ][ 2 ],
1158  int aMVFS[ 2 ][ 2 ], int aDMVector[ 2 ], int aMBAI
1159  ){
1160 
1161  int lBX, lBY;
1162  int lfField;
1163  int lfIntra;
1164  int lOffset;
1165  int lfNoSkip;
1166  int lfFiledMV;
1167 
1168  lBX = s_MPEG12Ctx.m_pMBXY[ aMBA ].m_X;
1169  lBY = s_MPEG12Ctx.m_pMBXY[ aMBA ].m_Y;
1171  lOffset = ( lBY * ( s_MPEG12Ctx.m_MBWidth << lfField ) + lBX ) * sizeof ( _MPEGMacroBlock8 );
1172  lfIntra = aMBType & _MPEG_MBT_INTRA;
1173  lfNoSkip = aMBAI == 1;
1174  lfFiledMV = aMotionType & _MPEG_MC_FIELD;
1175 
1177  s_MPEG12Ctx.m_pCurMotions -> m_pMBDstY = UNCACHED_SEG( s_MPEG12Ctx.m_pCurFrameY + lOffset );
1178  s_MPEG12Ctx.m_pCurMotions -> m_pMBDstCbCr = UNCACHED_SEG( s_MPEG12Ctx.m_pCurFrameCbCr + lOffset );
1179 
1180  lBX <<= 4;
1181  lBY <<= 4;
1182 
1183  if ( !lfIntra ) {
1184  _mpeg12_get_refs ( lBX, lBY, aMBType, aMotionType, aPMV, aMVFS, aDMVector );
1185  if ( lfNoSkip && ( aMBType & _MPEG_MBT_PATTERN ) )
1186  s_MPEG12Ctx.m_pCurMotions -> BlockOp = AddBlockOp[ lfField ][ lfFiledMV ];
1187  else {
1188  s_MPEG12Ctx.m_pCurMotions -> m_pSrc = s_MPEG12Ctx.m_pCurMotions -> m_pSPRRes;
1189  s_MPEG12Ctx.m_pCurMotions -> BlockOp = PutBlockOp[ !lfField + ( lfNoSkip && lfFiledMV && !lfField ) ];
1190  } /* end else */
1191  } else {
1192  s_MPEG12Ctx.m_pCurMotions -> m_Motion[ 0 ].MC_Luma = NULL;
1193  s_MPEG12Ctx.m_pCurMotions -> m_pSrc = s_MPEG12Ctx.m_pCurMotions -> m_pSPRBlk;
1194  s_MPEG12Ctx.m_pCurMotions -> BlockOp = PutBlockOp[ !lfField ];
1195  } /* end else */
1196 
1197 } /* end _mpeg2_mc */
1198 
1199 static int _mpeg12_slice ( int aMBAMax ) {
1200 
1201  int lPMV [ 2 ][ 2 ][ 2 ];
1202  int lMVFS [ 2 ][ 2 ];
1203  int lDMVector[ 2 ];
1204  int lMBA, lMBAI, lMBType, lMotionType;
1205  int retVal;
1206 
1207  s_MPEG12Ctx.m_fError = 0;
1208  lMBType = _MPEG_NextStartCode ();
1209 
1210  if ( lMBType < _MPEG_CODE_SLICE_MIN || lMBType > _MPEG_CODE_SLICE_MAX ) return -1;
1211 
1212  _MPEG_GetBits ( 32 );
1213 
1215 
1216  if ( _MPEG_GetBits ( 1 ) ) {
1217  _MPEG_GetBits ( 8 );
1218  _xtra_bitinf ();
1219  } /* end if */
1220 
1221  lMBAI = _MPEG_GetMBAI ();
1222 
1223  if ( lMBAI ) {
1224 
1225  lMBA = ( ( lMBType & 255 ) - 1 ) * s_MPEG12Ctx.m_MBWidth + lMBAI - 1;
1226 
1227  lMBAI =
1228  s_MPEG12Ctx.m_fDCRst = 1;
1229 
1230  lPMV[ 0 ][ 0 ][ 0 ] = lPMV[ 0 ][ 0 ][ 1 ] = lPMV[ 1 ][ 0 ][ 0 ] = lPMV[ 1 ][ 0 ][ 1 ] = 0;
1231  lPMV[ 0 ][ 1 ][ 0 ] = lPMV[ 0 ][ 1 ][ 1 ] = lPMV[ 1 ][ 1 ][ 0 ] = lPMV[ 1 ][ 1 ][ 1 ] = 0;
1232 
1233  } else return 0;
1234 
1235  while ( 1 ) {
1236 
1238 
1239  if ( lMBA >= aMBAMax || !_MPEG_WaitBDEC () ) return -1;
1240 
1241  if ( !lMBAI ) {
1242 
1243  if ( !_MPEG_ShowBits ( 23 ) || s_MPEG12Ctx.m_fError ) {
1244 resync:
1245  s_MPEG12Ctx.m_fError = 0;
1246  return 0;
1247 
1248  } else {
1249 
1250  lMBAI = _MPEG_GetMBAI ();
1251 
1252  if ( !lMBAI ) goto resync;
1253 
1254  } /* end else */
1255 
1256  } /* end if */
1257 
1258  if ( lMBA >= aMBAMax ) return -1;
1259 
1260  if ( lMBAI == 1 ) {
1261 
1262  retVal = _mpeg12_dec_mb ( &lMBType, &lMotionType, lPMV, lMVFS, lDMVector );
1263 
1264  if ( retVal < 0 ) return retVal;
1265  if ( !retVal ) goto resync;
1266 
1267  } else { /* skipped macroblock */
1268 
1269  s_MPEG12Ctx.m_fDCRst = 1;
1270 
1272  lPMV[ 0 ][ 0 ][ 0 ] = lPMV[ 0 ][ 0 ][ 1 ] =
1273  lPMV[ 1 ][ 0 ][ 0 ] = lPMV[ 1 ][ 0 ][ 1 ] = 0;
1275  lMotionType = _MPEG_MC_FRAME;
1276  else {
1277  lMotionType = _MPEG_MC_FIELD;
1278  lMVFS[ 0 ][ 0 ] =
1279  lMVFS[ 0 ][ 1 ] = s_MPEG12Ctx.m_PictStruct == _MPEG_PS_BOTTOM_FIELD;
1280  } /* end else */
1281 
1282  lMBType &= ~_MPEG_MBT_INTRA;
1283 
1284  } /* end else */
1285 
1286  _mpeg2_mc ( lMBA, lMBType, lMotionType, lPMV, lMVFS, lDMVector, lMBAI );
1287 
1288  DoMC ();
1289 
1290  ++lMBA;
1291  --lMBAI;
1292 
1293  s_MPEG12Ctx.m_CurMC ^= 1;
1294 
1295  if ( lMBA >= aMBAMax ) return -1;
1296 
1297  } /* end while */
1298 
1299 } /* end _mpeg12_slice */
#define UNCACHED_SEG(x)
Definition: kernel.h:35
static void * _init_seq(void)
Definition: libmpeg.c:124
static void *(* _init_cb)(void *, MPEGSequenceInfo *)
Definition: libmpeg.c:49
static int _get_first_picture(void *, s64 *)
Definition: libmpeg.c:569
static void _ext_pic_tsc(void)
Definition: libmpeg.c:559
static void _mpeg12_do_next_mc(void)
Definition: libmpeg.c:636
void MPEG_Destroy(void)
Definition: libmpeg.c:117
static void _mpeg12_motion_vector(int *apPMV, int *apDMVector, int aHRSize, int aVRSize, int aDMV, int aMVScale, int aFullPelVector)
Definition: libmpeg.c:760
static void _xtra_bitinf(void)
Definition: libmpeg.c:563
static int _mpeg12_dec_mb(int *, int *, int[2][2][2], int[2][2], int[2])
Definition: libmpeg.c:818
static void _ext_unknown(void)
Definition: libmpeg.c:376
int(* MPEG_Picture)(void *, s64 *)
Definition: libmpeg.c:76
static void _pic_header(void)
Definition: libmpeg.c:277
static void _ext_seq_scl(void)
Definition: libmpeg.c:490
static void _ext_cpy_rgt(void)
Definition: libmpeg.c:471
static void _ext_pic_dsp(void)
Definition: libmpeg.c:494
static void * s_pInitCBParam
Definition: libmpeg.c:50
static void(* LumaOp[8])(_MPEGMotion *)
Definition: libmpeg.c:23
static void _ext_pic_ssc(void)
Definition: libmpeg.c:555
static void _ext_seq(void)
Definition: libmpeg.c:381
static void(* PutBlockOp[3])(_MPEGMotions *)
Definition: libmpeg.c:33
static void _mpeg2_mc(int aMBA, int aMBType, int aMotionType, int aPMV[2][2][2], int aMVFS[2][2], int aDMVector[2], int aMBAI)
Definition: libmpeg.c:1156
static int _get_hdr(void)
Definition: libmpeg.c:201
static void _ext_and_ud(void)
Definition: libmpeg.c:302
static void _ext_qnt_mtx(void)
Definition: libmpeg.c:459
static void _mpeg12_dual_prime_vector(int aDMV[][2], int *apDMVector, int aMVX, int aMVY)
Definition: libmpeg.c:725
static void _ext_seq_dsp(void)
Definition: libmpeg.c:437
static void _mpeg12_motion_vectors(int aPMV[2][2][2], int aDMVector[2], int aMVFS[2][2], int aS, int anMV, int aMVFmt, int aHRSize, int aVRSize, int aDMV, int aMVScale)
Definition: libmpeg.c:792
static void(* ChromaOp[8])(void)
Definition: libmpeg.c:28
static void(* DoMC)(void)
Definition: libmpeg.c:78
static void _mpeg12_do_first_mc(void)
Definition: libmpeg.c:652
static _MPEGContext s_MPEG12Ctx
Definition: libmpeg.c:20
static void(* AddBlockOp[2][2])(_MPEGMotions *)
Definition: libmpeg.c:37
static int _get_next_picture(void *, s64 *)
Definition: libmpeg.c:594
void MPEG_Initialize(int(*apDataCB)(void *), void *apDataCBParam, void *(*apInitCB)(void *, MPEGSequenceInfo *), void *apInitCBParam, s64 *apCurPTS)
Definition: libmpeg.c:84
static void _destroy_seq(void)
Definition: libmpeg.c:172
static void _mpeg12_picture_data(void)
Definition: libmpeg.c:658
static void _mpeg12_decode_motion_vector(int *apPred, int aRSize, int aMotionCode, int aMotionResidual, int aFullPelVector)
Definition: libmpeg.c:700
static void _seq_header(void)
Definition: libmpeg.c:234
static void _ext_pic_cod(void)
Definition: libmpeg.c:522
static void _gop_header(void)
Definition: libmpeg.c:260
static float s_FrameRate[16]
Definition: libmpeg.c:42
static int _mpeg12_slice(int)
Definition: libmpeg.c:1199
static void _mpeg12_get_refs(int aBX, int aBY, int aMBType, int aMotionType, int aPMV[2][2][2], int aMVFS[2][2], int aDMVector[2])
Definition: libmpeg.c:975
static s64 * s_pCurPTS
Definition: libmpeg.c:21
static void _mpeg12_get_ref(_MPEGMacroBlock8 *apMBSrc, int aX, int anY, int aDX, int aDY, int aH, int aFSrc, int aFDst, int afAvg)
Definition: libmpeg.c:932
#define MPEG_LEVEL_MAIN
Definition: libmpeg.h:32
#define MPEG_PROFILE_422
Definition: libmpeg.h:25
#define MPEG_VIDEO_FORMAT_UNSPEC
Definition: libmpeg.h:42
#define MPEG_CHROMA_FORMAT_420
Definition: libmpeg.h:21
void _MPEG_avg_luma_Y(_MPEGMotion *)
void _MPEG_dma_ref_image(_MPEGMacroBlock8 *, _MPEGMotion *, int, int)
#define _MPEG_CODE_EXTENSION
#define _MPEG_MV_FRAME
#define _MPEG_PS_TOP_FIELD
void _MPEG_Destroy(void)
#define _MPEG_PS_BOTTOM_FIELD
#define _MPEG_XID_PIC_COD
#define _MPEG_MBT_QUANT
void _MPEG_avg_chroma_Y(void)
void _MPEG_put_luma_Y(_MPEGMotion *)
void _MPEG_put_block_il(_MPEGMotions *)
#define _MPEG_PT_P
#define _MPEG_XID_QMATRIX
int _MPEG_GetMotionCode(void)
#define _MPEG_MC_FIELD
#define _MPEG_CODE_USER_DATA
#define _MPEG_MC_FRAME
void _MPEG_avg_luma(_MPEGMotion *)
int _MPEG_WaitBDEC(void)
unsigned int _MPEG_ShowBits(unsigned int)
#define _MPEG_MBT_MOTION_BACKWARD
void _MPEG_put_block_fr(_MPEGMotions *)
void _MPEG_SetQSTIVFAS(void)
void _MPEG_put_chroma_Y(void)
#define _MPEG_XID_PIC_SSC
#define _MPEG_MC_DMV
void _MPEG_SetDefQM(int)
#define _MPEG_XID_SEQUENCE
int _MPEG_GetDMVector(void)
void _MPEG_put_block_fl(_MPEGMotions *)
void _MPEG_put_luma(_MPEGMotion *)
#define _MPEG_PS_FRAME
#define _MPEG_PT_B
#define _MPEG_XID_PIC_TSC
void _MPEG_put_luma_X(_MPEGMotion *)
void _MPEG_SetIDCP(void)
#define _MPEG_CODE_PIC_START
void _MPEG_put_luma_XY(_MPEGMotion *)
#define _MPEG_MBT_MOTION_FORWARD
unsigned int _MPEG_NextStartCode(void)
#define _MPEG_CODE_SEQ_END
void _MPEG_avg_chroma_X(void)
#define _MPEG_MBT_INTRA
void _MPEG_avg_luma_X(_MPEGMotion *)
int _MPEG_CSCImage(void *, void *, int)
void _MPEG_avg_chroma_XY(void)
void _MPEG_Initialize(_MPEGContext *, int(*)(void *), void *, int *)
#define _MPEG_XID_6
int _MPEG_GetMBAI(void)
void _MPEG_put_chroma_XY(void)
#define _MPEG_MBT_PATTERN
#define _MPEG_XID_SCALABLE
#define _MPEG_MV_FIELD
int _MPEG_GetMBType(void)
void _MPEG_add_block_frfr(_MPEGMotions *)
#define _MPEG_CODE_SEQ_HDR
#define _MPEG_CODE_GRP_START
void _MPEG_SetPCT(unsigned int)
void _MPEG_add_block_ilfl(_MPEGMotions *)
void _MPEG_SetQM(int)
void _MPEG_put_chroma_X(void)
void _MPEG_BDEC(int, int, int, int, void *)
void _MPEG_avg_chroma(void)
void _MPEG_add_block_frfl(_MPEGMotions *)
unsigned int _MPEG_GetBits(unsigned int)
#define _MPEG_CODE_SLICE_MAX
#define _MPEG_MC_16X8
#define _MPEG_XID_0
#define _MPEG_XID_COPYRIGHT
void _MPEG_avg_luma_XY(_MPEGMotion *)
#define _MPEG_XID_PIC_DSP
void _MPEG_do_mc(_MPEGMotion *)
#define _MPEG_XID_DISPLAY
void _MPEG_put_chroma(void)
int m_MSPerFrame
Definition: libmpeg.h:53
_MPEGMacroBlock8 * m_pAuxFrame
_MPEGMacroBlock8 * m_pCurFrame
_MPEGMBXY * m_pMBXY
int m_FCode[2][2]
_MPEGMotions * m_pCurMotions
MPEGSequenceInfo m_SI
_MPEGMacroBlock8 * m_pBckFrame
_MPEGMacroBlock8 * m_pFwdFrame
_MPEGMotions m_MC[2]
unsigned char * m_pCurFrameY
unsigned char * m_pCurFrameCbCr
unsigned char m_Y
unsigned char m_X
unsigned char * m_pSPRBlk
unsigned char * m_pSPRRes
unsigned char * m_pSPRMC
#define NULL
Definition: tamtypes.h:91
signed long s64
Definition: tamtypes.h:62